OSDN Git Service

* gcc.dg/torture/pr26565.c: Expect warning on packed field for
[pf3gnuchains/gcc-fork.git] / gcc / ipa-type-escape.c
1 /* Type based alias analysis.
2    Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
3    Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 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 the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22 /* This pass determines which types in the program contain only
23    instances that are completely encapsulated by the compilation unit.
24    Those types that are encapsulated must also pass the further
25    requirement that there be no bad operations on any instances of
26    those types.
27
28    A great deal of freedom in compilation is allowed for the instances
29    of those types that pass these conditions.
30 */
31
32 /* The code in this module is called by the ipa pass manager. It
33    should be one of the later passes since its information is used by
34    the rest of the compilation. */
35
36 #include "config.h"
37 #include "system.h"
38 #include "coretypes.h"
39 #include "tm.h"
40 #include "tree.h"
41 #include "tree-flow.h"
42 #include "tree-inline.h"
43 #include "tree-pass.h"
44 #include "langhooks.h"
45 #include "pointer-set.h"
46 #include "ggc.h"
47 #include "ipa-utils.h"
48 #include "ipa-type-escape.h"
49 #include "c-common.h"
50 #include "tree-gimple.h"
51 #include "cgraph.h"
52 #include "output.h"
53 #include "flags.h"
54 #include "timevar.h"
55 #include "diagnostic.h"
56 #include "langhooks.h"
57
58 /* Some of the aliasing is called very early, before this phase is
59    called.  To assure that this is not a problem, we keep track of if
60    this phase has been run.  */
61 static bool initialized = false;
62
63 /* Scratch bitmap for avoiding work. */
64 static bitmap been_there_done_that;
65 static bitmap bitmap_tmp;
66
67 /* There are two levels of escape that types can undergo.
68
69    EXPOSED_PARAMETER - some instance of the variable is
70    passed by value into an externally visible function or some
71    instance of the variable is passed out of an externally visible
72    function as a return value.  In this case any of the fields of the
73    variable that are pointer types end up having their types marked as
74    FULL_ESCAPE.
75
76    FULL_ESCAPE - when bad things happen to good types. One of the
77    following things happens to the type: (a) either an instance of the
78    variable has its address passed to an externally visible function,
79    (b) the address is taken and some bad cast happens to the address
80    or (c) explicit arithmetic is done to the address.
81 */
82
83 enum escape_t
84 {
85   EXPOSED_PARAMETER,
86   FULL_ESCAPE
87 };
88
89 /* The following two bit vectors global_types_* correspond to
90    previous cases above.  During the analysis phase, a bit is set in
91    one of these vectors if an operation of the offending class is
92    discovered to happen on the associated type.  */
93  
94 static bitmap global_types_exposed_parameter;
95 static bitmap global_types_full_escape;
96
97 /* All of the types seen in this compilation unit. */
98 static bitmap global_types_seen;
99 /* Reverse map to take a canon uid and map it to a canon type.  Uid's
100    are never manipulated unless they are associated with a canon
101    type.  */
102 static splay_tree uid_to_canon_type;
103
104 /* Internal structure of type mapping code.  This maps a canon type
105    name to its canon type.  */
106 static splay_tree all_canon_types;
107
108 /* Map from type clones to the single canon type.  */
109 static splay_tree type_to_canon_type;
110
111 /* A splay tree of bitmaps.  An element X in the splay tree has a bit
112    set in its bitmap at TYPE_UID (TYPE_MAIN_VARIANT (Y)) if there was
113    an operation in the program of the form "&X.Y".  */
114 static splay_tree uid_to_addressof_down_map;
115
116 /* A splay tree of bitmaps.  An element Y in the splay tree has a bit
117    set in its bitmap at TYPE_UID (TYPE_MAIN_VARIANT (X)) if there was
118    an operation in the program of the form "&X.Y".  */
119 static splay_tree uid_to_addressof_up_map;
120
121 /* Tree to hold the subtype maps used to mark subtypes of escaped
122    types.  */
123 static splay_tree uid_to_subtype_map;
124
125 /* Records tree nodes seen in cgraph_create_edges.  Simply using
126    walk_tree_without_duplicates doesn't guarantee each node is visited
127    once because it gets a new htab upon each recursive call from
128    scan_for_refs.  */
129 static struct pointer_set_t *visited_nodes;
130
131 /* Visited stmts by walk_use_def_chains function because it's called
132    recursively.  */
133 static struct pointer_set_t *visited_stmts;
134
135 static bitmap_obstack ipa_obstack;
136
137 /* Static functions from this file that are used 
138    before being defined.  */
139 static unsigned int look_for_casts (tree lhs ATTRIBUTE_UNUSED, tree);
140 static bool is_cast_from_non_pointer (tree, tree, void *);
141
142 /* Get the name of TYPE or return the string "<UNNAMED>".  */
143 static char*
144 get_name_of_type (tree type)
145 {
146   tree name = TYPE_NAME (type);
147   
148   if (!name)
149     /* Unnamed type, do what you like here.  */
150     return (char*)"<UNNAMED>";
151   
152   /* It will be a TYPE_DECL in the case of a typedef, otherwise, an
153      identifier_node */
154   if (TREE_CODE (name) == TYPE_DECL)
155     {
156       /*  Each DECL has a DECL_NAME field which contains an
157           IDENTIFIER_NODE.  (Some decls, most often labels, may have
158           zero as the DECL_NAME).  */
159       if (DECL_NAME (name))
160         return (char*)IDENTIFIER_POINTER (DECL_NAME (name));
161       else
162         /* Unnamed type, do what you like here.  */
163         return (char*)"<UNNAMED>";
164     }
165   else if (TREE_CODE (name) == IDENTIFIER_NODE)
166     return (char*)IDENTIFIER_POINTER (name);
167   else 
168     return (char*)"<UNNAMED>";
169 }
170
171 struct type_brand_s 
172 {
173   char* name;
174   int seq;
175 };
176
177 /* Splay tree comparison function on type_brand_s structures.  */
178
179 static int 
180 compare_type_brand (splay_tree_key sk1, splay_tree_key sk2)
181 {
182   struct type_brand_s * k1 = (struct type_brand_s *) sk1;
183   struct type_brand_s * k2 = (struct type_brand_s *) sk2;
184
185   int value = strcmp(k1->name, k2->name);
186   if (value == 0)
187     return k2->seq - k1->seq;
188   else 
189     return value;
190 }
191
192 /* All of the "unique_type" code is a hack to get around the sleazy
193    implementation used to compile more than file.  Currently gcc does
194    not get rid of multiple instances of the same type that have been
195    collected from different compilation units.  */
196 /* This is a trivial algorithm for removing duplicate types.  This
197    would not work for any language that used structural equivalence as
198    the basis of its type system.  */
199 /* Return either TYPE if this is first time TYPE has been seen an
200    compatible TYPE that has already been processed.  */ 
201
202 static tree
203 discover_unique_type (tree type)
204 {
205   struct type_brand_s * brand = XNEW (struct type_brand_s);
206   int i = 0;
207   splay_tree_node result;
208
209   brand->name = get_name_of_type (type);
210
211   while (1)
212     {
213       brand->seq = i++;
214       result = splay_tree_lookup (all_canon_types, (splay_tree_key) brand);
215
216       if (result)
217         {
218           /* Create an alias since this is just the same as
219              other_type.  */
220           tree other_type = (tree) result->value;
221           if (lang_hooks.types_compatible_p (type, other_type) == 1)
222             {
223               free (brand);
224               /* Insert this new type as an alias for other_type.  */
225               splay_tree_insert (type_to_canon_type,
226                                  (splay_tree_key) type,
227                                  (splay_tree_value) other_type);
228               return other_type;
229             }
230           /* Not compatible, look for next instance with same name.  */
231         }
232       else
233         {
234           /* No more instances, create new one since this is the first
235              time we saw this type.  */
236           brand->seq = i++;
237           /* Insert the new brand.  */
238           splay_tree_insert (all_canon_types,
239                              (splay_tree_key) brand,
240                              (splay_tree_value) type);
241
242           /* Insert this new type as an alias for itself.  */
243           splay_tree_insert (type_to_canon_type,
244                              (splay_tree_key) type,
245                              (splay_tree_value) type);
246
247           /* Insert the uid for reverse lookup; */
248           splay_tree_insert (uid_to_canon_type,
249                              (splay_tree_key) TYPE_UID (type),
250                              (splay_tree_value) type);
251
252           bitmap_set_bit (global_types_seen, TYPE_UID (type));
253           return type;
254         }
255     }
256 }
257
258 /* Return true if TYPE is one of the type classes that we are willing
259    to analyze.  This skips the goofy types like arrays of pointers to
260    methods. */
261 static bool
262 type_to_consider (tree type)
263 {
264   /* Strip the *'s off.  */
265   type = TYPE_MAIN_VARIANT (type);
266   while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
267     type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
268
269   switch (TREE_CODE (type))
270     {
271     case BOOLEAN_TYPE:
272     case COMPLEX_TYPE:
273     case ENUMERAL_TYPE:
274     case INTEGER_TYPE:
275     case QUAL_UNION_TYPE:
276     case REAL_TYPE:
277     case RECORD_TYPE:
278     case UNION_TYPE:
279     case VECTOR_TYPE:
280     case VOID_TYPE:
281       return true;
282   
283     default:
284       return false;
285     }
286 }
287
288 /* Get the canon type of TYPE.  If SEE_THRU_PTRS is true, remove all
289    the POINTER_TOs and if SEE_THRU_ARRAYS is true, remove all of the
290    ARRAY_OFs and POINTER_TOs.  */
291
292 static tree 
293 get_canon_type (tree type, bool see_thru_ptrs, bool see_thru_arrays)
294 {
295   splay_tree_node result;
296   /* Strip the *'s off.  */
297   if (!type || !type_to_consider (type))
298     return NULL;
299
300   type = TYPE_MAIN_VARIANT (type);
301   if (see_thru_arrays) 
302     while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
303       type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
304
305   else if (see_thru_ptrs) 
306     while (POINTER_TYPE_P (type))
307         type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
308
309   result = splay_tree_lookup(type_to_canon_type, (splay_tree_key) type);
310   
311   if (result == NULL)
312     return discover_unique_type (type);
313   else return (tree) result->value;
314 }
315
316 /* Same as GET_CANON_TYPE, except return the TYPE_ID rather than the
317    TYPE.  */
318
319 static int
320 get_canon_type_uid (tree type, bool see_thru_ptrs, bool see_thru_arrays)
321 {
322   type = get_canon_type (type, see_thru_ptrs, see_thru_arrays);
323   if (type)
324     return TYPE_UID(type);
325   else return 0;
326 }
327
328 /* Return 0 if TYPE is a record or union type.  Return a positive
329    number if TYPE is a pointer to a record or union.  The number is
330    the number of pointer types stripped to get to the record or union
331    type.  Return -1 if TYPE is none of the above.  */
332  
333 int
334 ipa_type_escape_star_count_of_interesting_type (tree type) 
335 {
336   int count = 0;
337   /* Strip the *'s off.  */
338   if (!type)
339     return -1;
340   type = TYPE_MAIN_VARIANT (type);
341   while (POINTER_TYPE_P (type))
342     {
343       type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
344       count++;
345     }
346
347   /* We are interested in records, and unions only.  */
348   if (TREE_CODE (type) == RECORD_TYPE 
349       || TREE_CODE (type) == QUAL_UNION_TYPE 
350       || TREE_CODE (type) == UNION_TYPE)
351     return count;
352   else 
353     return -1;
354
355
356
357 /* Return 0 if TYPE is a record or union type.  Return a positive
358    number if TYPE is a pointer to a record or union.  The number is
359    the number of pointer types stripped to get to the record or union
360    type.  Return -1 if TYPE is none of the above.  */
361  
362 int
363 ipa_type_escape_star_count_of_interesting_or_array_type (tree type) 
364 {
365   int count = 0;
366   /* Strip the *'s off.  */
367   if (!type)
368     return -1;
369   type = TYPE_MAIN_VARIANT (type);
370   while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
371     {
372       type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
373       count++;
374     }
375
376   /* We are interested in records, and unions only.  */
377   if (TREE_CODE (type) == RECORD_TYPE 
378       || TREE_CODE (type) == QUAL_UNION_TYPE 
379       || TREE_CODE (type) == UNION_TYPE)
380     return count;
381   else 
382     return -1;
383
384  
385  
386 /* Return true if the record, or union TYPE passed in escapes this
387    compilation unit. Note that all of the pointer-to's are removed
388    before testing since these may not be correct.  */
389
390 bool
391 ipa_type_escape_type_contained_p (tree type)
392 {
393   if (!initialized)
394     return false;
395   return !bitmap_bit_p (global_types_full_escape, 
396                         get_canon_type_uid (type, true, false));
397 }
398
399 /* Return true if a modification to a field of type FIELD_TYPE cannot
400    clobber a record of RECORD_TYPE.  */
401
402 bool 
403 ipa_type_escape_field_does_not_clobber_p (tree record_type, tree field_type)
404
405   splay_tree_node result;
406   int uid;
407   
408   if (!initialized)
409     return false;
410
411   /* Strip off all of the pointer tos on the record type.  Strip the
412      same number of pointer tos from the field type.  If the field
413      type has fewer, it could not have been aliased. */
414   record_type = TYPE_MAIN_VARIANT (record_type);
415   field_type = TYPE_MAIN_VARIANT (field_type);
416   while (POINTER_TYPE_P (record_type))
417     {
418       record_type = TYPE_MAIN_VARIANT (TREE_TYPE (record_type));
419       if (POINTER_TYPE_P (field_type)) 
420         field_type = TYPE_MAIN_VARIANT (TREE_TYPE (field_type));
421       else 
422         /* However, if field_type is a union, this quick test is not
423            correct since one of the variants of the union may be a
424            pointer to type and we cannot see across that here.  So we
425            just strip the remaining pointer tos off the record type
426            and fall thru to the more precise code.  */
427         if (TREE_CODE (field_type) == QUAL_UNION_TYPE 
428             || TREE_CODE (field_type) == UNION_TYPE)
429           {
430             while (POINTER_TYPE_P (record_type))
431               record_type = TYPE_MAIN_VARIANT (TREE_TYPE (record_type));
432             break;
433           } 
434         else 
435           return true;
436     }
437   
438   record_type = get_canon_type (record_type, true, true);
439   /* The record type must be contained.  The field type may
440      escape.  */
441   if (!ipa_type_escape_type_contained_p (record_type))
442     return false;
443
444   uid = TYPE_UID (record_type);
445   result = splay_tree_lookup (uid_to_addressof_down_map, (splay_tree_key) uid);
446   
447   if (result) 
448     {
449       bitmap field_type_map = (bitmap) result->value;
450       uid = get_canon_type_uid (field_type, true, true);
451       /* If the bit is there, the address was taken. If not, it
452          wasn't.  */
453       return !bitmap_bit_p (field_type_map, uid);
454     }
455   else
456     /* No bitmap means no addresses were taken.  */
457     return true;
458 }
459
460
461 /* Add TYPE to the suspect type set. Return true if the bit needed to
462    be marked.  */
463
464 static tree
465 mark_type (tree type, enum escape_t escape_status)
466 {
467   bitmap map = NULL;
468   int uid;
469
470   type = get_canon_type (type, true, true);
471   if (!type) 
472     return NULL;
473
474   switch (escape_status) 
475     {
476     case EXPOSED_PARAMETER:
477       map = global_types_exposed_parameter;
478       break;
479     case FULL_ESCAPE:
480       map = global_types_full_escape;
481       break;
482     }
483
484   uid = TYPE_UID (type);
485   if (bitmap_bit_p (map, uid))
486     return type;
487   else
488     {
489       bitmap_set_bit (map, uid);
490       if (escape_status == FULL_ESCAPE)
491         {
492           /* Efficiency hack. When things are bad, do not mess around
493              with this type anymore.  */
494           bitmap_set_bit (global_types_exposed_parameter, uid);
495         }      
496     }
497   return type;
498 }
499
500 /* Add interesting TYPE to the suspect type set. If the set is
501    EXPOSED_PARAMETER and the TYPE is a pointer type, the set is
502    changed to FULL_ESCAPE.  */
503
504 static void 
505 mark_interesting_type (tree type, enum escape_t escape_status)
506 {
507   if (!type) return;
508   if (ipa_type_escape_star_count_of_interesting_type (type) >= 0)
509     {
510       if ((escape_status == EXPOSED_PARAMETER)
511           && POINTER_TYPE_P (type))
512         /* EXPOSED_PARAMETERs are only structs or unions are passed by
513            value.  Anything passed by reference to an external
514            function fully exposes the type.  */
515         mark_type (type, FULL_ESCAPE);
516       else
517         mark_type (type, escape_status);
518     }
519 }
520
521 /* Return true if PARENT is supertype of CHILD.  Both types must be
522    known to be structures or unions. */
523  
524 static bool
525 parent_type_p (tree parent, tree child)
526 {
527   int i;
528   tree binfo, base_binfo;
529   if (TYPE_BINFO (parent)) 
530     for (binfo = TYPE_BINFO (parent), i = 0;
531          BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
532       {
533         tree binfotype = BINFO_TYPE (base_binfo);
534         if (binfotype == child) 
535           return true;
536         else if (parent_type_p (binfotype, child))
537           return true;
538       }
539   if (TREE_CODE (parent) == UNION_TYPE
540       || TREE_CODE (parent) == QUAL_UNION_TYPE) 
541     {
542       tree field;
543       /* Search all of the variants in the union to see if one of them
544          is the child.  */
545       for (field = TYPE_FIELDS (parent);
546            field;
547            field = TREE_CHAIN (field))
548         {
549           tree field_type;
550           if (TREE_CODE (field) != FIELD_DECL)
551             continue;
552           
553           field_type = TREE_TYPE (field);
554           if (field_type == child) 
555             return true;
556         }
557
558       /* If we did not find it, recursively ask the variants if one of
559          their children is the child type.  */
560       for (field = TYPE_FIELDS (parent);
561            field;
562            field = TREE_CHAIN (field))
563         {
564           tree field_type;
565           if (TREE_CODE (field) != FIELD_DECL)
566             continue;
567           
568           field_type = TREE_TYPE (field);
569           if (TREE_CODE (field_type) == RECORD_TYPE 
570               || TREE_CODE (field_type) == QUAL_UNION_TYPE 
571               || TREE_CODE (field_type) == UNION_TYPE)
572             if (parent_type_p (field_type, child)) 
573               return true;
574         }
575     }
576   
577   if (TREE_CODE (parent) == RECORD_TYPE)
578     {
579       tree field;
580       for (field = TYPE_FIELDS (parent);
581            field;
582            field = TREE_CHAIN (field))
583         {
584           tree field_type;
585           if (TREE_CODE (field) != FIELD_DECL)
586             continue;
587           
588           field_type = TREE_TYPE (field);
589           if (field_type == child) 
590             return true;
591           /* You can only cast to the first field so if it does not
592              match, quit.  */
593           if (TREE_CODE (field_type) == RECORD_TYPE 
594               || TREE_CODE (field_type) == QUAL_UNION_TYPE 
595               || TREE_CODE (field_type) == UNION_TYPE)
596             {
597               if (parent_type_p (field_type, child)) 
598                 return true;
599               else 
600                 break;
601             }
602         }
603     }
604   return false;
605 }
606
607 /* Return the number of pointer tos for TYPE and return TYPE with all
608    of these stripped off.  */
609
610 static int 
611 count_stars (tree* type_ptr)
612 {
613   tree type = *type_ptr;
614   int i = 0;
615   type = TYPE_MAIN_VARIANT (type);
616   while (POINTER_TYPE_P (type))
617     {
618       type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
619       i++;
620     }
621
622   *type_ptr = type;
623   return i;
624 }
625
626 enum cast_type {
627   CT_UP = 0x1,
628   CT_DOWN = 0x2,
629   CT_SIDEWAYS = 0x4,
630   CT_USELESS = 0x8,
631   CT_FROM_P_BAD = 0x10,
632   CT_FROM_NON_P = 0x20,
633   CT_TO_NON_INTER = 0x40,
634   CT_FROM_MALLOC = 0x80,
635   CT_NO_CAST = 0x100
636 };
637
638 /* Check the cast FROM_TYPE to TO_TYPE.  This function requires that
639    the two types have already passed the
640    ipa_type_escape_star_count_of_interesting_type test.  */
641
642 static enum cast_type
643 check_cast_type (tree to_type, tree from_type)
644 {
645   int to_stars = count_stars (&to_type);
646   int from_stars = count_stars (&from_type);
647   if (to_stars != from_stars) 
648     return CT_SIDEWAYS;
649
650   if (to_type == from_type)
651     return CT_USELESS;
652
653   if (parent_type_p (to_type, from_type)) return CT_UP;
654   if (parent_type_p (from_type, to_type)) return CT_DOWN;
655   return CT_SIDEWAYS;
656 }     
657
658 /* This function returns non-zero if VAR is result of call 
659    to malloc function.  */
660
661 static bool
662 is_malloc_result (tree var)
663 {
664   tree def_stmt;
665   tree rhs;
666   int flags;
667
668   if (!var)
669     return false;
670   
671   if (SSA_NAME_IS_DEFAULT_DEF (var))
672     return false;
673
674   def_stmt = SSA_NAME_DEF_STMT (var);
675   
676   if (TREE_CODE (def_stmt) != GIMPLE_MODIFY_STMT)
677     return false;
678
679   if (var != GIMPLE_STMT_OPERAND (def_stmt, 0))
680     return false;
681
682   rhs = get_call_expr_in (def_stmt);
683
684   if (!rhs)
685     return false;
686
687   flags = call_expr_flags (rhs);
688     
689   return ((flags & ECF_MALLOC) != 0);
690
691 }
692
693 /* Check a cast FROM this variable, TO_TYPE.  Mark the escaping types
694    if appropriate. Returns cast_type as detected.  */
695  
696 static enum cast_type
697 check_cast (tree to_type, tree from) 
698 {
699   tree from_type = get_canon_type (TREE_TYPE (from), false, false);
700   bool to_interesting_type, from_interesting_type;
701   enum cast_type cast = CT_NO_CAST;
702
703   to_type = get_canon_type (to_type, false, false);
704   if (!from_type || !to_type || from_type == to_type)
705     return cast;
706
707   to_interesting_type = 
708     ipa_type_escape_star_count_of_interesting_type (to_type) >= 0;
709   from_interesting_type = 
710     ipa_type_escape_star_count_of_interesting_type (from_type) >= 0;
711
712   if (to_interesting_type) 
713     if (from_interesting_type)
714       {
715         /* Both types are interesting. This can be one of four types
716            of cast: useless, up, down, or sideways.  We do not care
717            about up or useless.  Sideways casts are always bad and
718            both sides get marked as escaping.  Downcasts are not
719            interesting here because if type is marked as escaping, all
720            of its subtypes escape.  */
721         cast = check_cast_type (to_type, from_type);
722         switch (cast) 
723           {
724           case CT_UP:
725           case CT_USELESS:
726           case CT_DOWN:
727             break;
728
729           case CT_SIDEWAYS:
730             mark_type (to_type, FULL_ESCAPE);
731             mark_type (from_type, FULL_ESCAPE);
732             break;
733
734           default:
735             break;
736           }
737       }
738     else
739       {
740         /* This code excludes two cases from marking as escaped:
741            
742         1. if this is a cast of index of array of structures/unions
743         that happens before accessing array element, we should not 
744         mark it as escaped.
745         2. if this is a cast from the local that is a result from a
746         call to malloc, do not mark the cast as bad.  
747
748         */
749         
750         if (POINTER_TYPE_P (to_type) && !POINTER_TYPE_P (from_type))
751           cast = CT_FROM_NON_P;
752         else if (TREE_CODE (from) == SSA_NAME 
753                  && is_malloc_result (from))
754           cast = CT_FROM_MALLOC;
755         else
756           {
757             cast = CT_FROM_P_BAD;
758             mark_type (to_type, FULL_ESCAPE);
759           }
760       }
761   else if (from_interesting_type)
762     {
763       mark_type (from_type, FULL_ESCAPE);
764       cast = CT_TO_NON_INTER;
765     }
766
767   return cast;
768 }
769
770 typedef struct cast 
771 {
772   int type;
773   tree stmt;
774 }cast_t;
775
776 /* This function is a callback for walk_tree called from 
777    is_cast_from_non_pointer. The data->type is set to be:
778
779    0      - if there is no cast
780    number - the number of casts from non-pointer type
781    -1     - if there is a cast that makes the type to escape
782
783    If data->type = number, then data->stmt will contain the 
784    last casting stmt met in traversing.  */
785
786 static tree
787 is_cast_from_non_pointer_1 (tree *tp, int *walk_subtrees, void *data)
788 {
789   tree def_stmt = *tp;
790
791
792   if (pointer_set_insert (visited_stmts, def_stmt))
793     {
794       *walk_subtrees = 0;
795       return NULL;
796     }
797   
798   switch (TREE_CODE (def_stmt))
799     {
800     case GIMPLE_MODIFY_STMT:
801       {
802         use_operand_p use_p; 
803         ssa_op_iter iter;
804         tree lhs = GIMPLE_STMT_OPERAND (def_stmt, 0);
805         tree rhs = GIMPLE_STMT_OPERAND (def_stmt, 1);
806
807         unsigned int cast = look_for_casts (lhs, rhs);
808         /* Check that only one cast happened, and it's of 
809            non-pointer type.  */
810         if ((cast & CT_FROM_NON_P) == (CT_FROM_NON_P) 
811             && (cast & ~(CT_FROM_NON_P)) == 0)
812           {
813             ((cast_t *)data)->stmt = def_stmt;
814             ((cast_t *)data)->type++;
815
816             FOR_EACH_SSA_USE_OPERAND (use_p, def_stmt, iter, SSA_OP_ALL_USES)
817               {
818                 walk_use_def_chains (USE_FROM_PTR (use_p), is_cast_from_non_pointer, 
819                                      data, false);
820                 if (((cast_t*)data)->type == -1)
821                   return def_stmt;
822               }
823           }
824
825         /* Check that there is no cast, or cast is not harmful. */
826         else if ((cast & CT_NO_CAST) == (CT_NO_CAST)
827                  || (cast & CT_DOWN) == (CT_DOWN)
828                  || (cast & CT_UP) == (CT_UP)
829                  || (cast & CT_USELESS) == (CT_USELESS)
830                  || (cast & CT_FROM_MALLOC) == (CT_FROM_MALLOC))
831           {
832             FOR_EACH_SSA_USE_OPERAND (use_p, def_stmt, iter, SSA_OP_ALL_USES)
833               {
834                 walk_use_def_chains (USE_FROM_PTR (use_p), is_cast_from_non_pointer, 
835                                      data, false);
836                 if (((cast_t*)data)->type == -1)
837                   return def_stmt;
838               }     
839           }
840
841         /* The cast is harmful.  */
842         else
843           {
844             ((cast_t *)data)->type = -1;
845             return def_stmt;
846           }
847
848         *walk_subtrees = 0;
849       }     
850       break;
851
852     default:
853       {
854         *walk_subtrees = 0;
855         break;
856       }
857     }
858
859   return NULL;
860 }
861
862 /* This function is a callback for walk_use_def_chains function called 
863    from is_array_access_through_pointer_and_index.  */
864
865 static bool
866 is_cast_from_non_pointer (tree var, tree def_stmt, void *data)
867 {
868
869   if (!def_stmt || !var)
870     return false;
871   
872   if (TREE_CODE (def_stmt) == PHI_NODE)
873     return false;
874
875   if (SSA_NAME_IS_DEFAULT_DEF (var))
876       return false;
877
878   walk_tree (&def_stmt, is_cast_from_non_pointer_1, data, NULL);
879   if (((cast_t*)data)->type == -1)
880     return true;
881   
882   return false;
883 }
884
885 /* When array element a_p[i] is accessed through the pointer a_p 
886    and index i, it's translated into the following sequence
887    in gimple:
888
889   i.1_5 = (unsigned int) i_1;
890   D.1605_6 = i.1_5 * 16;
891   D.1606_7 = (struct str_t *) D.1605_6;
892   a_p.2_8 = a_p;
893   D.1608_9 = D.1606_7 + a_p.2_8;
894
895   OP0 and OP1 are of the same pointer types and stand for 
896   D.1606_7 and a_p.2_8 or vise versa.
897
898   This function checks that:
899
900   1. one of OP0 and OP1 (D.1606_7) has passed only one cast from 
901   non-pointer type (D.1606_7 = (struct str_t *) D.1605_6;).
902
903   2. one of OP0 and OP1 which has passed the cast from 
904   non-pointer type (D.1606_7), is actually generated by multiplication of 
905   index by size of type to which both OP0 and OP1 point to
906   (in this case D.1605_6 = i.1_5 * 16; ).
907
908   3. an address of def of the var to which was made cast (D.1605_6) 
909   was not taken.(How can it happen?)
910
911   The following items are checked implicitly by the end of algorithm:
912
913   4. one of OP0 and OP1 (a_p.2_8) have never been cast 
914   (because if it was cast to pointer type, its type, that is also 
915   the type of OP0 and OP1, will be marked as escaped during 
916   analysis of casting stmt (when check_cast() is called 
917   from scan_for_refs for this stmt)).   
918
919   5. defs of OP0 and OP1 are not passed into externally visible function
920   (because if they are passed then their type, that is also the type of OP0
921   and OP1, will be marked and escaped during check_call function called from 
922   scan_for_refs with call stmt).
923
924   In total, 1-5 guaranty that it's an access to array by pointer and index. 
925
926 */
927
928 static bool
929 is_array_access_through_pointer_and_index (tree op0, tree op1)
930 {
931   tree base, offset, offset_cast_stmt;
932   tree before_cast, before_cast_def_stmt;
933   cast_t op0_cast, op1_cast;
934
935   /* Check 1.  */
936
937   /* Init data for walk_use_def_chains function.  */
938   op0_cast.type = op1_cast.type = 0;
939   op0_cast.stmt = op1_cast.stmt = NULL;
940
941   visited_stmts = pointer_set_create ();
942   walk_use_def_chains (op0, is_cast_from_non_pointer,(void *)(&op0_cast), false);
943   pointer_set_destroy (visited_stmts);
944
945   visited_stmts = pointer_set_create ();  
946   walk_use_def_chains (op1, is_cast_from_non_pointer,(void *)(&op1_cast), false);
947   pointer_set_destroy (visited_stmts);
948
949   if (op0_cast.type == 1 && op1_cast.type == 0)
950     {
951       base = op1;
952       offset = op0;
953       offset_cast_stmt = op0_cast.stmt;
954     }
955   else if (op0_cast.type == 0 && op1_cast.type == 1)
956     {
957       base = op0;
958       offset = op1;      
959       offset_cast_stmt = op1_cast.stmt;
960     }
961   else
962     return false;
963
964   /* Check 2.  
965      offset_cast_stmt is of the form: 
966      D.1606_7 = (struct str_t *) D.1605_6;  */
967
968   before_cast = SINGLE_SSA_TREE_OPERAND (offset_cast_stmt, SSA_OP_USE);
969   if (!before_cast)
970     return false;
971   
972   if (SSA_NAME_IS_DEFAULT_DEF(before_cast))
973     return false;
974   
975   before_cast_def_stmt = SSA_NAME_DEF_STMT (before_cast);
976   if (!before_cast_def_stmt)
977     return false;
978
979   /* before_cast_def_stmt should be of the form:
980      D.1605_6 = i.1_5 * 16; */
981   
982   if (TREE_CODE (before_cast_def_stmt) == GIMPLE_MODIFY_STMT)
983     {
984       tree lhs = GIMPLE_STMT_OPERAND (before_cast_def_stmt,0);
985       tree rhs = GIMPLE_STMT_OPERAND (before_cast_def_stmt,1);
986
987       /* We expect temporary here.  */
988       if (!is_gimple_reg (lhs)) 
989         return false;
990
991       if (TREE_CODE (rhs) == MULT_EXPR)
992         {
993           tree arg0 = TREE_OPERAND (rhs, 0);
994           tree arg1 = TREE_OPERAND (rhs, 1);
995           tree unit_size = 
996             TYPE_SIZE_UNIT (TREE_TYPE (TYPE_MAIN_VARIANT (TREE_TYPE (op0))));
997
998           if (!(CONSTANT_CLASS_P (arg0) 
999               && simple_cst_equal (arg0,unit_size))
1000               && !(CONSTANT_CLASS_P (arg1) 
1001               && simple_cst_equal (arg1,unit_size)))
1002             return false;                          
1003         }
1004       else
1005         return false;
1006     }
1007   else
1008     return false;
1009
1010   /* Check 3.
1011      check that address of D.1605_6 was not taken.
1012      FIXME: if D.1605_6 is gimple reg than it cannot be addressable.  */
1013
1014   return true;
1015 }
1016
1017 /* Register the parameter and return types of function FN.  The type
1018    ESCAPES if the function is visible outside of the compilation
1019    unit.  */
1020 static void 
1021 check_function_parameter_and_return_types (tree fn, bool escapes) 
1022 {
1023   tree arg;
1024   
1025   if (TYPE_ARG_TYPES (TREE_TYPE (fn)))
1026     {
1027       for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1028            arg && TREE_VALUE (arg) != void_type_node;
1029            arg = TREE_CHAIN (arg))
1030         {
1031           tree type = get_canon_type (TREE_VALUE (arg), false, false);
1032           if (escapes)
1033             mark_interesting_type (type, EXPOSED_PARAMETER);
1034         }
1035     }
1036   else
1037     {
1038       /* FIXME - According to Geoff Keating, we should never have to
1039          do this; the front ends should always process the arg list
1040          from the TYPE_ARG_LIST. However, Geoff is wrong, this code
1041          does seem to be live.  */
1042
1043       for (arg = DECL_ARGUMENTS (fn); arg; arg = TREE_CHAIN (arg))
1044         {
1045           tree type = get_canon_type (TREE_TYPE (arg), false, false);
1046           if (escapes)
1047             mark_interesting_type (type, EXPOSED_PARAMETER);
1048         }
1049     }
1050   if (escapes)
1051     {
1052       tree type = get_canon_type (TREE_TYPE (TREE_TYPE (fn)), false, false);
1053       mark_interesting_type (type, EXPOSED_PARAMETER); 
1054     }
1055 }
1056
1057 /* Return true if the variable T is the right kind of static variable to
1058    perform compilation unit scope escape analysis.  */
1059
1060 static inline void
1061 has_proper_scope_for_analysis (tree t)
1062 {
1063   /* If the variable has the "used" attribute, treat it as if it had a
1064      been touched by the devil.  */
1065   tree type = get_canon_type (TREE_TYPE (t), false, false);
1066   if (!type) return;
1067
1068   if (lookup_attribute ("used", DECL_ATTRIBUTES (t)))
1069     {
1070       mark_interesting_type (type, FULL_ESCAPE);
1071       return;
1072     }
1073
1074   /* Do not want to do anything with volatile except mark any
1075      function that uses one to be not const or pure.  */
1076   if (TREE_THIS_VOLATILE (t)) 
1077     return;
1078
1079   /* Do not care about a local automatic that is not static.  */
1080   if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
1081     return;
1082
1083   if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
1084     {
1085       /* If the front end set the variable to be READONLY and
1086          constant, we can allow this variable in pure or const
1087          functions but the scope is too large for our analysis to set
1088          these bits ourselves.  */
1089       
1090       if (TREE_READONLY (t)
1091           && DECL_INITIAL (t)
1092           && is_gimple_min_invariant (DECL_INITIAL (t)))
1093         ; /* Read of a constant, do not change the function state.  */
1094       else 
1095         {
1096           /* The type escapes for all public and externs. */
1097           mark_interesting_type (type, FULL_ESCAPE);
1098         }
1099     }
1100 }
1101
1102 /* If T is a VAR_DECL for a static that we are interested in, add the
1103    uid to the bitmap.  */
1104
1105 static void
1106 check_operand (tree t)
1107 {
1108   if (!t) return;
1109
1110   /* This is an assignment from a function, register the types as
1111      escaping.  */
1112   if (TREE_CODE (t) == FUNCTION_DECL)
1113     check_function_parameter_and_return_types (t, true);
1114
1115   else if (TREE_CODE (t) == VAR_DECL)
1116     has_proper_scope_for_analysis (t); 
1117 }
1118
1119 /* Examine tree T for references.   */
1120
1121 static void
1122 check_tree (tree t)
1123 {
1124   if ((TREE_CODE (t) == EXC_PTR_EXPR) || (TREE_CODE (t) == FILTER_EXPR))
1125     return;
1126
1127   /* We want to catch here also REALPART_EXPR and IMAGEPART_EXPR,
1128      but they already included in handled_component_p.  */
1129   while (handled_component_p (t))
1130     {
1131       if (TREE_CODE (t) == ARRAY_REF)
1132         check_operand (TREE_OPERAND (t, 1));
1133       t = TREE_OPERAND (t, 0);
1134     }
1135
1136   if (INDIRECT_REF_P (t))
1137 /*  || TREE_CODE (t) == MEM_REF) */
1138     check_tree (TREE_OPERAND (t, 0));
1139
1140   if (SSA_VAR_P (t) || (TREE_CODE (t) == FUNCTION_DECL))
1141     check_operand (t);
1142 }
1143
1144 /* Create an address_of edge FROM_TYPE.TO_TYPE.  */
1145 static void
1146 mark_interesting_addressof (tree to_type, tree from_type)
1147 {
1148   int from_uid;
1149   int to_uid;
1150   bitmap type_map;
1151   splay_tree_node result; 
1152
1153   from_type = get_canon_type (from_type, false, false);
1154   to_type = get_canon_type (to_type, false, false);
1155   
1156   if (!from_type || !to_type)
1157     return;
1158
1159   from_uid = TYPE_UID (from_type);
1160   to_uid = TYPE_UID (to_type);
1161
1162   gcc_assert (ipa_type_escape_star_count_of_interesting_type (from_type) == 0);
1163   
1164   /* Process the Y into X map pointer.  */
1165   result = splay_tree_lookup (uid_to_addressof_down_map, 
1166                               (splay_tree_key) from_uid);
1167   
1168   if (result) 
1169     type_map = (bitmap) result->value;  
1170   else 
1171     {
1172       type_map = BITMAP_ALLOC (&ipa_obstack);
1173       splay_tree_insert (uid_to_addressof_down_map,
1174                          from_uid, 
1175                          (splay_tree_value)type_map);
1176     }
1177   bitmap_set_bit (type_map, TYPE_UID (to_type));
1178   
1179   /* Process the X into Y reverse map pointer.  */
1180   result = 
1181     splay_tree_lookup (uid_to_addressof_up_map, (splay_tree_key) to_uid);
1182   
1183   if (result) 
1184     type_map = (bitmap) result->value;  
1185   else 
1186     {
1187       type_map = BITMAP_ALLOC (&ipa_obstack);
1188       splay_tree_insert (uid_to_addressof_up_map,
1189                          to_uid, 
1190                          (splay_tree_value)type_map);
1191     }
1192   bitmap_set_bit (type_map, TYPE_UID (from_type)); 
1193 }
1194
1195 /* Scan tree T to see if there are any addresses taken in within T.  */
1196
1197 static void 
1198 look_for_address_of (tree t)
1199 {
1200   if (TREE_CODE (t) == ADDR_EXPR)
1201     {
1202       tree x = get_base_var (t);
1203       tree cref = TREE_OPERAND (t, 0);
1204
1205       /* If we have an expression of the form "&a.b.c.d", mark a.b,
1206          b.c and c.d. as having its address taken.  */ 
1207       tree fielddecl = NULL_TREE;
1208       while (cref!= x)
1209         {
1210           if (TREE_CODE (cref) == COMPONENT_REF)
1211             {
1212               fielddecl =  TREE_OPERAND (cref, 1);
1213               mark_interesting_addressof (TREE_TYPE (fielddecl), 
1214                                           DECL_FIELD_CONTEXT (fielddecl));
1215             }
1216           else if (TREE_CODE (cref) == ARRAY_REF)
1217             get_canon_type (TREE_TYPE (cref), false, false);
1218
1219           cref = TREE_OPERAND (cref, 0);
1220         }
1221
1222       if (TREE_CODE (x) == VAR_DECL) 
1223         has_proper_scope_for_analysis (x);
1224     }
1225 }
1226
1227
1228 /* Scan tree T to see if there are any casts within it.
1229    LHS Is the LHS of the expression involving the cast.  */
1230
1231 static unsigned int 
1232 look_for_casts (tree lhs ATTRIBUTE_UNUSED, tree t)
1233 {
1234   unsigned int cast = 0;
1235
1236
1237   if (is_gimple_cast (t) || TREE_CODE (t) == VIEW_CONVERT_EXPR)
1238     {
1239       tree castfromvar = TREE_OPERAND (t, 0);
1240       cast = cast | check_cast (TREE_TYPE (t), castfromvar);
1241     }
1242   else 
1243     while (handled_component_p (t))
1244       {
1245         t = TREE_OPERAND (t, 0);
1246         if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
1247           {
1248             /* This may be some part of a component ref.
1249                IE it may be a.b.VIEW_CONVERT_EXPR<weird_type>(c).d, AFAIK.
1250                castfromref will give you a.b.c, not a. */
1251             tree castfromref = TREE_OPERAND (t, 0);
1252             cast = cast | check_cast (TREE_TYPE (t), castfromref);
1253           }
1254         else if (TREE_CODE (t) == COMPONENT_REF)
1255           get_canon_type (TREE_TYPE (TREE_OPERAND (t, 1)), false, false);
1256       }
1257
1258   if (!cast)
1259     cast = CT_NO_CAST;
1260   return cast;
1261
1262
1263 /* Check to see if T is a read or address of operation on a static var
1264    we are interested in analyzing.  */
1265
1266 static void
1267 check_rhs_var (tree t)
1268 {
1269   look_for_address_of (t);
1270   check_tree(t);
1271 }
1272
1273 /* Check to see if T is an assignment to a static var we are
1274    interested in analyzing.  */
1275
1276 static void
1277 check_lhs_var (tree t)
1278 {
1279   check_tree(t);
1280 }
1281
1282 /* This is a scaled down version of get_asm_expr_operands from
1283    tree_ssa_operands.c.  The version there runs much later and assumes
1284    that aliasing information is already available. Here we are just
1285    trying to find if the set of inputs and outputs contain references
1286    or address of operations to local.  FN is the function being
1287    analyzed and STMT is the actual asm statement.  */
1288
1289 static void
1290 get_asm_expr_operands (tree stmt)
1291 {
1292   int noutputs = list_length (ASM_OUTPUTS (stmt));
1293   const char **oconstraints
1294     = (const char **) alloca ((noutputs) * sizeof (const char *));
1295   int i;
1296   tree link;
1297   const char *constraint;
1298   bool allows_mem, allows_reg, is_inout;
1299   
1300   for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
1301     {
1302       oconstraints[i] = constraint
1303         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1304       parse_output_constraint (&constraint, i, 0, 0,
1305                                &allows_mem, &allows_reg, &is_inout);
1306       
1307       check_lhs_var (TREE_VALUE (link));
1308     }
1309
1310   for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1311     {
1312       constraint
1313         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1314       parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1315                               oconstraints, &allows_mem, &allows_reg);
1316       
1317       check_rhs_var (TREE_VALUE (link));
1318     }
1319   
1320   /* There is no code here to check for asm memory clobbers.  The
1321      casual maintainer might think that such code would be necessary,
1322      but that appears to be wrong.  In other parts of the compiler,
1323      the asm memory clobbers are assumed to only clobber variables
1324      that are addressable.  All types with addressable instances are
1325      assumed to already escape.  So, we are protected here.  */
1326 }
1327
1328 /* Check the parameters of a function call to CALL_EXPR to mark the
1329    types that pass across the function boundary.  Also check to see if
1330    this is either an indirect call, a call outside the compilation
1331    unit.  */
1332
1333 static void
1334 check_call (tree call_expr) 
1335 {
1336   tree operand;
1337   tree callee_t = get_callee_fndecl (call_expr);
1338   struct cgraph_node* callee;
1339   enum availability avail = AVAIL_NOT_AVAILABLE;
1340   call_expr_arg_iterator iter;
1341
1342   FOR_EACH_CALL_EXPR_ARG (operand, iter, call_expr)
1343     check_rhs_var (operand);
1344   
1345   if (callee_t)
1346     {
1347       tree arg_type;
1348       tree last_arg_type = NULL;
1349       callee = cgraph_node(callee_t);
1350       avail = cgraph_function_body_availability (callee);
1351       
1352       /* Check that there are no implicit casts in the passing of
1353          parameters.  */
1354       if (TYPE_ARG_TYPES (TREE_TYPE (callee_t)))
1355         {
1356           for (arg_type = TYPE_ARG_TYPES (TREE_TYPE (callee_t)),
1357                  operand = first_call_expr_arg (call_expr, &iter);
1358                arg_type && TREE_VALUE (arg_type) != void_type_node;
1359                arg_type = TREE_CHAIN (arg_type),
1360                  operand = next_call_expr_arg (&iter))
1361             {
1362               if (operand)
1363                 {
1364                   last_arg_type = TREE_VALUE(arg_type);
1365                   check_cast (last_arg_type, operand);
1366                 }
1367               else 
1368                 /* The code reaches here for some unfortunate
1369                    builtin functions that do not have a list of
1370                    argument types.  */
1371                 break; 
1372             }
1373         } 
1374       else  
1375         { 
1376           /* FIXME - According to Geoff Keating, we should never
1377              have to do this; the front ends should always process
1378              the arg list from the TYPE_ARG_LIST. */
1379           for (arg_type = DECL_ARGUMENTS (callee_t),
1380                  operand = first_call_expr_arg (call_expr, &iter);
1381                arg_type;
1382                arg_type = TREE_CHAIN (arg_type),
1383                  operand = next_call_expr_arg (&iter))
1384             {
1385               if (operand)
1386                 {
1387                   last_arg_type = TREE_TYPE(arg_type);
1388                   check_cast (last_arg_type, operand);
1389                 } 
1390               else 
1391                 /* The code reaches here for some unfortunate
1392                    builtin functions that do not have a list of
1393                    argument types.  */
1394                 break; 
1395             }
1396         }
1397       
1398       /* In the case where we have a var_args function, we need to
1399          check the remaining parameters against the last argument.  */
1400       arg_type = last_arg_type;
1401       for (;
1402            operand != NULL_TREE;
1403            operand = next_call_expr_arg (&iter))
1404         {
1405           if (arg_type)
1406             check_cast (arg_type, operand);
1407           else 
1408             {
1409               /* The code reaches here for some unfortunate
1410                  builtin functions that do not have a list of
1411                  argument types.  Most of these functions have
1412                  been marked as having their parameters not
1413                  escape, but for the rest, the type is doomed.  */
1414               tree type = get_canon_type (TREE_TYPE (operand), false, false);
1415               mark_interesting_type (type, FULL_ESCAPE);
1416             }
1417         }
1418     }
1419
1420   /* The callee is either unknown (indirect call) or there is just no
1421      scannable code for it (external call) .  We look to see if there
1422      are any bits available for the callee (such as by declaration or
1423      because it is builtin) and process solely on the basis of those
1424      bits. */
1425
1426   if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
1427     {
1428       /* If this is a direct call to an external function, mark all of
1429          the parameter and return types.  */
1430       FOR_EACH_CALL_EXPR_ARG (operand, iter, call_expr)
1431         {
1432           tree type = get_canon_type (TREE_TYPE (operand), false, false);
1433           mark_interesting_type (type, EXPOSED_PARAMETER);
1434     }
1435           
1436       if (callee_t) 
1437         {
1438           tree type = 
1439             get_canon_type (TREE_TYPE (TREE_TYPE (callee_t)), false, false);
1440           mark_interesting_type (type, EXPOSED_PARAMETER);
1441         }
1442     }
1443 }
1444
1445 /* CODE is the operation on OP0 and OP1.  OP0 is the operand that we
1446    *know* is a pointer type.  OP1 may be a pointer type.  */
1447 static bool 
1448 okay_pointer_operation (enum tree_code code, tree op0, tree op1)
1449 {
1450   tree op0type = TYPE_MAIN_VARIANT (TREE_TYPE (op0));
1451   tree op1type = TYPE_MAIN_VARIANT (TREE_TYPE (op1));
1452
1453   switch (code)
1454     {
1455     case MULT_EXPR:
1456       /* Multiplication does not change alignment.  */
1457       return true;
1458       break;
1459     case MINUS_EXPR:
1460     case PLUS_EXPR:
1461       {
1462         if (POINTER_TYPE_P (op1type)
1463             && TREE_CODE (op0) == SSA_NAME 
1464             && TREE_CODE (op1) == SSA_NAME 
1465             && is_array_access_through_pointer_and_index (op0, op1))
1466           return true;
1467         else
1468           {
1469             tree size_of_op0_points_to = TYPE_SIZE_UNIT (TREE_TYPE (op0type));
1470             
1471             if (CONSTANT_CLASS_P (op1)
1472                 && size_of_op0_points_to
1473                 && multiple_of_p (TREE_TYPE (size_of_op0_points_to), 
1474                                   op1, size_of_op0_points_to))
1475               return true;
1476
1477             if (CONSTANT_CLASS_P (op0) 
1478                 && size_of_op0_points_to
1479                 && multiple_of_p (TREE_TYPE (size_of_op0_points_to), 
1480                                   op0, size_of_op0_points_to))
1481               return true;          
1482           }
1483       }
1484       break;
1485     default:
1486       return false;
1487     }
1488   return false;
1489 }
1490
1491 /* TP is the part of the tree currently under the microscope.
1492    WALK_SUBTREES is part of the walk_tree api but is unused here.
1493    DATA is cgraph_node of the function being walked.  */
1494
1495 /* FIXME: When this is converted to run over SSA form, this code
1496    should be converted to use the operand scanner.  */
1497
1498 static tree
1499 scan_for_refs (tree *tp, int *walk_subtrees, void *data)
1500 {
1501   struct cgraph_node *fn = data;
1502   tree t = *tp;
1503
1504   switch (TREE_CODE (t))  
1505     {
1506     case VAR_DECL:
1507       if (DECL_INITIAL (t))
1508         walk_tree (&DECL_INITIAL (t), scan_for_refs, fn, visited_nodes);
1509       *walk_subtrees = 0;
1510       break;
1511
1512     case GIMPLE_MODIFY_STMT:
1513       {
1514         /* First look on the lhs and see what variable is stored to */
1515         tree lhs = GIMPLE_STMT_OPERAND (t, 0);
1516         tree rhs = GIMPLE_STMT_OPERAND (t, 1);
1517
1518         check_lhs_var (lhs);
1519         check_cast (TREE_TYPE (lhs), rhs);
1520
1521         /* For the purposes of figuring out what the cast affects */
1522
1523         /* Next check the operands on the rhs to see if they are ok. */
1524         switch (TREE_CODE_CLASS (TREE_CODE (rhs))) 
1525           {
1526           case tcc_binary:          
1527             {
1528               tree op0 = TREE_OPERAND (rhs, 0);
1529               tree type0 = get_canon_type (TREE_TYPE (op0), false, false);
1530               tree op1 = TREE_OPERAND (rhs, 1);
1531               tree type1 = get_canon_type (TREE_TYPE (op1), false, false);
1532  
1533               /* If this is pointer arithmetic of any bad sort, then
1534                  we need to mark the types as bad.  For binary
1535                  operations, no binary operator we currently support
1536                  is always "safe" in regard to what it would do to
1537                  pointers for purposes of determining which types
1538                  escape, except operations of the size of the type.
1539                  It is possible that min and max under the right set
1540                  of circumstances and if the moon is in the correct
1541                  place could be safe, but it is hard to see how this
1542                  is worth the effort.  */
1543  
1544               if (type0 && POINTER_TYPE_P (type0)
1545                   && !okay_pointer_operation (TREE_CODE (rhs), op0, op1))
1546                 mark_interesting_type (type0, FULL_ESCAPE);
1547               if (type1 && POINTER_TYPE_P (type1)
1548                   && !okay_pointer_operation (TREE_CODE (rhs), op1, op0))
1549                 mark_interesting_type (type1, FULL_ESCAPE);
1550               
1551               look_for_casts (lhs, op0);
1552               look_for_casts (lhs, op1);
1553               check_rhs_var (op0);
1554               check_rhs_var (op1);
1555             }
1556             break;
1557           case tcc_unary:
1558             {
1559               tree op0 = TREE_OPERAND (rhs, 0);
1560               tree type0 = get_canon_type (TREE_TYPE (op0), false, false);
1561               /* For unary operations, if the operation is NEGATE or
1562                  ABS on a pointer, this is also considered pointer
1563                  arithmetic and thus, bad for business.  */
1564               if (type0 && (TREE_CODE (op0) == NEGATE_EXPR
1565                    || TREE_CODE (op0) == ABS_EXPR)
1566                   && POINTER_TYPE_P (type0))
1567                 {
1568                   mark_interesting_type (type0, FULL_ESCAPE);
1569                 }
1570               check_rhs_var (op0);
1571               look_for_casts (lhs, op0);
1572               look_for_casts (lhs, rhs);
1573             }
1574
1575             break;
1576           case tcc_reference:
1577             look_for_casts (lhs, rhs);
1578             check_rhs_var (rhs);
1579             break;
1580           case tcc_declaration:
1581             check_rhs_var (rhs);
1582             break;
1583           case tcc_expression:
1584             switch (TREE_CODE (rhs)) 
1585               {
1586               case ADDR_EXPR:
1587                 look_for_casts (lhs, TREE_OPERAND (rhs, 0));
1588                 check_rhs_var (rhs);
1589                 break;
1590               default:
1591                 break;
1592               }
1593             break;
1594           case tcc_vl_exp:
1595             switch (TREE_CODE (rhs))
1596               {
1597               case CALL_EXPR:
1598                 /* If this is a call to malloc, squirrel away the
1599                    result so we do mark the resulting cast as being
1600                    bad.  */
1601                 check_call (rhs);
1602                 break;
1603               default:
1604                 break;
1605               }
1606             break;
1607           default:
1608             break;
1609           }
1610         *walk_subtrees = 0;
1611       }
1612       break;
1613
1614     case ADDR_EXPR:
1615       /* This case is here to find addresses on rhs of constructors in
1616          decl_initial of static variables. */
1617       check_rhs_var (t);
1618       *walk_subtrees = 0;
1619       break;
1620
1621     case CALL_EXPR: 
1622       check_call (t);
1623       *walk_subtrees = 0;
1624       break;
1625       
1626     case ASM_EXPR:
1627       get_asm_expr_operands (t);
1628       *walk_subtrees = 0;
1629       break;
1630       
1631     default:
1632       break;
1633     }
1634   return NULL;
1635 }
1636
1637
1638 /* The init routine for analyzing global static variable usage.  See
1639    comments at top for description.  */
1640 static void 
1641 ipa_init (void) 
1642 {
1643   bitmap_obstack_initialize (&ipa_obstack);
1644   global_types_exposed_parameter = BITMAP_ALLOC (&ipa_obstack);
1645   global_types_full_escape = BITMAP_ALLOC (&ipa_obstack);
1646   global_types_seen = BITMAP_ALLOC (&ipa_obstack);
1647
1648   uid_to_canon_type = splay_tree_new (splay_tree_compare_ints, 0, 0);
1649   all_canon_types = splay_tree_new (compare_type_brand, 0, 0);
1650   type_to_canon_type = splay_tree_new (splay_tree_compare_pointers, 0, 0);
1651   uid_to_subtype_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1652   uid_to_addressof_down_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1653   uid_to_addressof_up_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1654
1655   /* There are some shared nodes, in particular the initializers on
1656      static declarations.  We do not need to scan them more than once
1657      since all we would be interested in are the addressof
1658      operations.  */
1659   visited_nodes = pointer_set_create ();
1660   initialized = true;
1661 }
1662
1663 /* Check out the rhs of a static or global initialization VNODE to see
1664    if any of them contain addressof operations.  Note that some of
1665    these variables may  not even be referenced in the code in this
1666    compilation unit but their right hand sides may contain references
1667    to variables defined within this unit.  */
1668
1669 static void 
1670 analyze_variable (struct varpool_node *vnode)
1671 {
1672   tree global = vnode->decl;
1673   tree type = get_canon_type (TREE_TYPE (global), false, false);
1674
1675   /* If this variable has exposure beyond the compilation unit, add
1676      its type to the global types.  */
1677
1678   if (vnode->externally_visible)
1679     mark_interesting_type (type, FULL_ESCAPE);
1680
1681   gcc_assert (TREE_CODE (global) == VAR_DECL);
1682
1683   if (DECL_INITIAL (global))
1684     walk_tree (&DECL_INITIAL (global), scan_for_refs, NULL, visited_nodes);
1685 }
1686
1687 /* This is the main routine for finding the reference patterns for
1688    global variables within a function FN.  */
1689
1690 static void
1691 analyze_function (struct cgraph_node *fn)
1692 {
1693   tree decl = fn->decl;
1694   check_function_parameter_and_return_types (decl, 
1695                                              fn->local.externally_visible);
1696   if (dump_file)
1697     fprintf (dump_file, "\n local analysis of %s", cgraph_node_name (fn));
1698   
1699   {
1700     struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
1701     basic_block this_block;
1702
1703     FOR_EACH_BB_FN (this_block, this_cfun)
1704       {
1705         block_stmt_iterator bsi;
1706         for (bsi = bsi_start (this_block); !bsi_end_p (bsi); bsi_next (&bsi))
1707           walk_tree (bsi_stmt_ptr (bsi), scan_for_refs, 
1708                      fn, visited_nodes);
1709       }
1710   }
1711
1712   /* There may be const decls with interesting right hand sides.  */
1713   if (DECL_STRUCT_FUNCTION (decl))
1714     {
1715       tree step;
1716       for (step = DECL_STRUCT_FUNCTION (decl)->unexpanded_var_list;
1717            step;
1718            step = TREE_CHAIN (step))
1719         {
1720           tree var = TREE_VALUE (step);
1721           if (TREE_CODE (var) == VAR_DECL 
1722               && DECL_INITIAL (var)
1723               && !TREE_STATIC (var))
1724             walk_tree (&DECL_INITIAL (var), scan_for_refs, 
1725                        fn, visited_nodes);
1726           get_canon_type (TREE_TYPE (var), false, false);
1727         }
1728     }
1729 }
1730
1731 \f
1732
1733 /* Convert a type_UID into a type.  */
1734 static tree
1735 type_for_uid (int uid)
1736 {
1737   splay_tree_node result = 
1738     splay_tree_lookup (uid_to_canon_type, (splay_tree_key) uid);
1739   
1740   if (result)
1741     return (tree) result->value;  
1742   else return NULL;
1743 }
1744
1745 /* Return the a bitmap with the subtypes of the type for UID.  If it
1746    does not exist, return either NULL or a new bitmap depending on the
1747    value of CREATE.  */ 
1748
1749 static bitmap
1750 subtype_map_for_uid (int uid, bool create)
1751 {
1752   splay_tree_node result = splay_tree_lookup (uid_to_subtype_map, 
1753                               (splay_tree_key) uid);
1754   
1755   if (result) 
1756     return (bitmap) result->value;  
1757   else if (create)
1758     {
1759       bitmap subtype_map = BITMAP_ALLOC (&ipa_obstack);
1760       splay_tree_insert (uid_to_subtype_map,
1761                          uid, 
1762                          (splay_tree_value)subtype_map);
1763       return subtype_map;
1764     }
1765   else return NULL;
1766 }
1767
1768 /* Mark all of the supertypes and field types of TYPE as being seen.
1769    Also accumulate the subtypes for each type so that
1770    close_types_full_escape can mark a subtype as escaping if the
1771    supertype escapes.  */
1772
1773 static void
1774 close_type_seen (tree type)
1775 {
1776   tree field;
1777   int i, uid;
1778   tree binfo, base_binfo;
1779
1780   /* See thru all pointer tos and array ofs. */
1781   type = get_canon_type (type, true, true);
1782   if (!type)
1783     return;
1784
1785   uid = TYPE_UID (type);
1786
1787   if (bitmap_bit_p (been_there_done_that, uid))
1788     return;
1789   bitmap_set_bit (been_there_done_that, uid);
1790
1791   /* If we are doing a language with a type hierarchy, mark all of
1792      the superclasses.  */
1793   if (TYPE_BINFO (type)) 
1794     for (binfo = TYPE_BINFO (type), i = 0;
1795          BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1796       {
1797         tree binfo_type = BINFO_TYPE (base_binfo);
1798         bitmap subtype_map = subtype_map_for_uid 
1799           (TYPE_UID (TYPE_MAIN_VARIANT (binfo_type)), true);
1800         bitmap_set_bit (subtype_map, uid);
1801         close_type_seen (get_canon_type (binfo_type, true, true));
1802       }
1803       
1804   /* If the field is a struct or union type, mark all of the
1805      subfields.  */
1806   for (field = TYPE_FIELDS (type);
1807        field;
1808        field = TREE_CHAIN (field))
1809     {
1810       tree field_type;
1811       if (TREE_CODE (field) != FIELD_DECL)
1812         continue;
1813
1814       field_type = TREE_TYPE (field);
1815       if (ipa_type_escape_star_count_of_interesting_or_array_type (field_type) >= 0)
1816         close_type_seen (get_canon_type (field_type, true, true));
1817     }
1818 }
1819
1820 /* Take a TYPE that has been passed by value to an external function
1821    and mark all of the fields that have pointer types as escaping. For
1822    any of the non pointer types that are structures or unions,
1823    recurse.  TYPE is never a pointer type.  */ 
1824
1825 static void
1826 close_type_exposed_parameter (tree type)
1827 {
1828   tree field;
1829   int uid;
1830
1831   type = get_canon_type (type, false, false);
1832   if (!type)
1833     return;
1834   uid = TYPE_UID (type);
1835   gcc_assert (!POINTER_TYPE_P (type));
1836
1837   if (bitmap_bit_p (been_there_done_that, uid))
1838     return;
1839   bitmap_set_bit (been_there_done_that, uid);
1840
1841   /* If the field is a struct or union type, mark all of the
1842      subfields.  */
1843   for (field = TYPE_FIELDS (type);
1844        field;
1845        field = TREE_CHAIN (field))
1846     {
1847       tree field_type;
1848
1849       if (TREE_CODE (field) != FIELD_DECL)
1850         continue;
1851
1852       field_type = get_canon_type (TREE_TYPE (field), false, false);
1853       mark_interesting_type (field_type, EXPOSED_PARAMETER);
1854
1855       /* Only recurse for non pointer types of structures and unions.  */
1856       if (ipa_type_escape_star_count_of_interesting_type (field_type) == 0) 
1857         close_type_exposed_parameter (field_type);
1858     }
1859 }
1860
1861 /* The next function handles the case where a type fully escapes.
1862    This means that not only does the type itself escape, 
1863
1864    a) the type of every field recursively escapes
1865    b) the type of every subtype escapes as well as the super as well
1866    as all of the pointer to types for each field.
1867
1868    Note that pointer to types are not marked as escaping.  If the
1869    pointed to type escapes, the pointer to type also escapes.
1870
1871    Take a TYPE that has had the address taken for an instance of it
1872    and mark all of the types for its fields as having their addresses
1873    taken. */ 
1874
1875 static void
1876 close_type_full_escape (tree type)
1877 {
1878   tree field;
1879   unsigned int i;
1880   int uid;
1881   tree binfo, base_binfo;
1882   bitmap_iterator bi;
1883   bitmap subtype_map;
1884   splay_tree_node address_result; 
1885
1886   /* Strip off any pointer or array types.  */
1887   type = get_canon_type (type, true, true);
1888   if (!type)
1889     return;
1890   uid = TYPE_UID (type);
1891
1892   if (bitmap_bit_p (been_there_done_that, uid))
1893     return;
1894   bitmap_set_bit (been_there_done_that, uid);
1895
1896   subtype_map = subtype_map_for_uid (uid, false);
1897
1898   /* If we are doing a language with a type hierarchy, mark all of
1899      the superclasses.  */
1900   if (TYPE_BINFO (type)) 
1901     for (binfo = TYPE_BINFO (type), i = 0;
1902          BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1903       {
1904         tree binfotype = BINFO_TYPE (base_binfo);
1905         binfotype = mark_type (binfotype, FULL_ESCAPE);
1906         close_type_full_escape (binfotype);
1907       }
1908       
1909   /* Mark as escaped any types that have been down casted to
1910      this type. */
1911   if (subtype_map)
1912     EXECUTE_IF_SET_IN_BITMAP (subtype_map, 0, i, bi)
1913       {
1914         tree subtype = type_for_uid (i); 
1915         subtype = mark_type (subtype, FULL_ESCAPE);
1916         close_type_full_escape (subtype);
1917       }
1918
1919   /* If the field is a struct or union type, mark all of the
1920      subfields.  */
1921   for (field = TYPE_FIELDS (type);
1922        field;
1923        field = TREE_CHAIN (field))
1924     {
1925       tree field_type;
1926       if (TREE_CODE (field) != FIELD_DECL)
1927         continue;
1928
1929       field_type = TREE_TYPE (field);
1930       if (ipa_type_escape_star_count_of_interesting_or_array_type (field_type) >= 0)
1931         {
1932           field_type = mark_type (field_type, FULL_ESCAPE);
1933           close_type_full_escape (field_type);
1934         }
1935     }
1936
1937   /* For all of the types A that contain this type B and were part of
1938      an expression like "&...A.B...", mark the A's as escaping.  */
1939   address_result = splay_tree_lookup (uid_to_addressof_up_map, 
1940                                       (splay_tree_key) uid);
1941   if (address_result)
1942     {
1943       bitmap containing_classes = (bitmap) address_result->value;
1944       EXECUTE_IF_SET_IN_BITMAP (containing_classes, 0, i, bi)
1945         {
1946           close_type_full_escape (type_for_uid (i));
1947         }
1948     }
1949 }
1950
1951 /* Transitively close the addressof bitmap for the type with UID.
1952    This means that if we had a.b and b.c, a would have both b and c in
1953    its maps.  */ 
1954
1955 static bitmap
1956 close_addressof_down (int uid) 
1957 {
1958   bitmap_iterator bi;
1959   splay_tree_node result = 
1960     splay_tree_lookup (uid_to_addressof_down_map, (splay_tree_key) uid);
1961   bitmap map = NULL;
1962   bitmap new_map;
1963   unsigned int i;
1964   
1965   if (result) 
1966     map = (bitmap) result->value;
1967   else 
1968     return NULL;
1969
1970   if (bitmap_bit_p (been_there_done_that, uid))
1971     return map;
1972   bitmap_set_bit (been_there_done_that, uid);
1973
1974   /* If the type escapes, get rid of the addressof map, it will not be
1975      needed.  */
1976   if (bitmap_bit_p (global_types_full_escape, uid))
1977     {
1978       BITMAP_FREE (map);
1979       splay_tree_remove (uid_to_addressof_down_map, (splay_tree_key) uid);
1980       return NULL;
1981     }
1982
1983   /* The new_map will have all of the bits for the enclosed fields and
1984      will have the unique id version of the old map.  */
1985   new_map = BITMAP_ALLOC (&ipa_obstack);
1986
1987   EXECUTE_IF_SET_IN_BITMAP (map, 0, i, bi)
1988     {
1989       bitmap submap = close_addressof_down (i);
1990       bitmap_set_bit (new_map, i);
1991       if (submap) 
1992         bitmap_ior_into (new_map, submap);
1993     }      
1994   result->value = (splay_tree_value) new_map;
1995
1996   BITMAP_FREE (map);
1997   return new_map;
1998 }
1999
2000 \f
2001 /* The main entry point for type escape analysis.  */
2002
2003 static unsigned int
2004 type_escape_execute (void)
2005 {
2006   struct cgraph_node *node;
2007   struct varpool_node *vnode;
2008   unsigned int i;
2009   bitmap_iterator bi;
2010   splay_tree_node result;
2011
2012   ipa_init ();
2013
2014   /* Process all of the variables first.  */
2015   FOR_EACH_STATIC_VARIABLE (vnode)
2016     analyze_variable (vnode);
2017
2018   /* Process all of the functions. next
2019
2020      We do not want to process any of the clones so we check that this
2021      is a master clone.  However, we do need to process any
2022      AVAIL_OVERWRITABLE functions (these are never clones) because
2023      they may cause a type variable to escape.  
2024   */
2025   for (node = cgraph_nodes; node; node = node->next)
2026     if (node->analyzed 
2027         && (cgraph_is_master_clone (node)
2028             || (cgraph_function_body_availability (node) == AVAIL_OVERWRITABLE)))
2029       analyze_function (node);
2030
2031
2032   pointer_set_destroy (visited_nodes);
2033   visited_nodes = NULL;
2034
2035   /* Do all of the closures to discover which types escape the
2036      compilation unit.  */
2037
2038   been_there_done_that = BITMAP_ALLOC (&ipa_obstack);
2039   bitmap_tmp = BITMAP_ALLOC (&ipa_obstack);
2040
2041   /* Examine the types that we have directly seen in scanning the code
2042      and add to that any contained types or superclasses.  */
2043
2044   bitmap_copy (bitmap_tmp, global_types_seen);
2045   EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
2046     {
2047       tree type = type_for_uid (i);
2048       /* Only look at records and unions and pointer tos.  */
2049       if (ipa_type_escape_star_count_of_interesting_or_array_type (type) >= 0)
2050         close_type_seen (type);
2051     }
2052   bitmap_clear (been_there_done_that);
2053
2054   /* Examine all of the types passed by value and mark any enclosed
2055      pointer types as escaping.  */
2056   bitmap_copy (bitmap_tmp, global_types_exposed_parameter);
2057   EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
2058     {
2059       close_type_exposed_parameter (type_for_uid (i));
2060     }
2061   bitmap_clear (been_there_done_that);
2062
2063   /* Close the types for escape.  If something escapes, then any
2064      enclosed types escape as well as any subtypes.  */
2065   bitmap_copy (bitmap_tmp, global_types_full_escape);
2066   EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
2067     {
2068       close_type_full_escape (type_for_uid (i));
2069     }
2070   bitmap_clear (been_there_done_that);
2071
2072   /* Before this pass, the uid_to_addressof_down_map for type X
2073      contained an entry for Y if there had been an operation of the
2074      form &X.Y.  This step adds all of the fields contained within Y
2075      (recursively) to X's map.  */
2076   
2077   result = splay_tree_min (uid_to_addressof_down_map);
2078   while (result)
2079     {
2080       int uid = result->key;
2081       /* Close the addressof map, i.e. copy all of the transitive
2082          substructures up to this level.  */
2083       close_addressof_down (uid);
2084       result = splay_tree_successor (uid_to_addressof_down_map, uid);
2085     }
2086
2087   /* Do not need the array types and pointer types in the persistent
2088      data structures.  */
2089   result = splay_tree_min (all_canon_types);
2090   while (result)
2091     {
2092       tree type = (tree) result->value;
2093       tree key = (tree) result->key;
2094       if (POINTER_TYPE_P (type) 
2095           || TREE_CODE (type) == ARRAY_TYPE)
2096         {
2097           splay_tree_remove (all_canon_types, (splay_tree_key) result->key);
2098           splay_tree_remove (type_to_canon_type, (splay_tree_key) type);
2099           splay_tree_remove (uid_to_canon_type, (splay_tree_key) TYPE_UID (type));
2100           bitmap_clear_bit (global_types_seen, TYPE_UID (type));
2101         }
2102       result = splay_tree_successor (all_canon_types, (splay_tree_key) key);
2103     }
2104
2105   if (dump_file)
2106     { 
2107       EXECUTE_IF_SET_IN_BITMAP (global_types_seen, 0, i, bi)
2108         {
2109           /* The pointer types are in the global_types_full_escape
2110              bitmap but not in the backwards map.  They also contain
2111              no useful information since they are not marked.  */
2112           tree type = type_for_uid (i);
2113           fprintf(dump_file, "type %d ", i);
2114           print_generic_expr (dump_file, type, 0);
2115           if (bitmap_bit_p (global_types_full_escape, i))
2116             fprintf(dump_file, " escaped\n");
2117           else 
2118             fprintf(dump_file, " contained\n");
2119         }
2120     }
2121
2122   /* Get rid of uid_to_addressof_up_map and its bitmaps.  */
2123   result = splay_tree_min (uid_to_addressof_up_map);
2124   while (result)
2125     {
2126       int uid = (int)result->key;
2127       bitmap bm = (bitmap)result->value;
2128
2129       BITMAP_FREE (bm);
2130       splay_tree_remove (uid_to_addressof_up_map, (splay_tree_key) uid);
2131       result = splay_tree_successor (uid_to_addressof_up_map, uid);
2132     }
2133
2134   /* Get rid of the subtype map.  */
2135   result = splay_tree_min (uid_to_subtype_map);
2136   while (result)
2137     {
2138       bitmap b = (bitmap)result->value;
2139       BITMAP_FREE(b);
2140       splay_tree_remove (uid_to_subtype_map, result->key);
2141       result = splay_tree_min (uid_to_subtype_map);
2142     }
2143   splay_tree_delete (uid_to_subtype_map);
2144   uid_to_subtype_map = NULL;
2145
2146   BITMAP_FREE (global_types_exposed_parameter);
2147   BITMAP_FREE (been_there_done_that);
2148   BITMAP_FREE (bitmap_tmp);
2149   return 0;
2150 }
2151
2152 static bool
2153 gate_type_escape_vars (void)
2154 {
2155   return (flag_unit_at_a_time != 0 && flag_ipa_type_escape
2156           /* Don't bother doing anything if the program has errors.  */
2157           && !(errorcount || sorrycount));
2158 }
2159
2160 struct tree_opt_pass pass_ipa_type_escape =
2161 {
2162   "type-escape-var",                    /* name */
2163   gate_type_escape_vars,                /* gate */
2164   type_escape_execute,                  /* execute */
2165   NULL,                                 /* sub */
2166   NULL,                                 /* next */
2167   0,                                    /* static_pass_number */
2168   TV_IPA_TYPE_ESCAPE,                   /* tv_id */
2169   0,                                    /* properties_required */
2170   0,                                    /* properties_provided */
2171   0,                                    /* properties_destroyed */
2172   0,                                    /* todo_flags_start */
2173   0,                                    /* todo_flags_finish */
2174   0                                     /* letter */
2175 };
2176