* ipa-type-escape.c (look_for_casts): Use get_base_var.
[official-gcc.git] / gcc / ipa-type-escape.c
blob5f0bb4d43d8bd064b37417442e60c5e2ecba1bc1
1 /* Type based alias analysis.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 This file is part of GCC.
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.
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.
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. */
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.
28 A great deal of freedom in compilation is allowed for the instances
29 of those types that pass these conditions.
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. */
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"
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;
63 /* This bitmap contains the set of local vars that are the lhs of
64 calls to mallocs. These variables, when seen on the rhs as part of
65 a cast, the cast are not marked as doing bad things to the type
66 even though they are generally of the form
67 "foo = (type_of_foo)void_temp". */
68 static bitmap results_of_malloc;
70 /* Scratch bitmap for avoiding work. */
71 static bitmap been_there_done_that;
72 static bitmap bitmap_tmp;
74 /* There are two levels of escape that types can undergo.
76 EXPOSED_PARAMETER - some instance of the variable is
77 passed by value into an externally visible function or some
78 instance of the variable is passed out of an externally visible
79 function as a return value. In this case any of the fields of the
80 variable that are pointer types end up having their types marked as
81 FULL_ESCAPE.
83 FULL_ESCAPE - when bad things happen to good types. One of the
84 following things happens to the type: (a) either an instance of the
85 variable has its address passed to an externally visible function,
86 (b) the address is taken and some bad cast happens to the address
87 or (c) explicit arithmetic is done to the address.
90 enum escape_t
92 EXPOSED_PARAMETER,
93 FULL_ESCAPE
96 /* The following two bit vectors global_types_* correspond to
97 previous cases above. During the analysis phase, a bit is set in
98 one of these vectors if an operation of the offending class is
99 discovered to happen on the associated type. */
101 static bitmap global_types_exposed_parameter;
102 static bitmap global_types_full_escape;
104 /* All of the types seen in this compilation unit. */
105 static bitmap global_types_seen;
106 /* Reverse map to take a canon uid and map it to a canon type. Uid's
107 are never manipulated unless they are associated with a canon
108 type. */
109 static splay_tree uid_to_canon_type;
111 /* Internal structure of type mapping code. This maps a canon type
112 name to its canon type. */
113 static splay_tree all_canon_types;
115 /* Map from type clones to the single canon type. */
116 static splay_tree type_to_canon_type;
118 /* A splay tree of bitmaps. An element X in the splay tree has a bit
119 set in its bitmap at TYPE_UID (TYPE_MAIN_VARIANT (Y)) if there was
120 an operation in the program of the form "&X.Y". */
121 static splay_tree uid_to_addressof_down_map;
123 /* A splay tree of bitmaps. An element Y in the splay tree has a bit
124 set in its bitmap at TYPE_UID (TYPE_MAIN_VARIANT (X)) if there was
125 an operation in the program of the form "&X.Y". */
126 static splay_tree uid_to_addressof_up_map;
128 /* Tree to hold the subtype maps used to mark subtypes of escaped
129 types. */
130 static splay_tree uid_to_subtype_map;
132 /* Records tree nodes seen in cgraph_create_edges. Simply using
133 walk_tree_without_duplicates doesn't guarantee each node is visited
134 once because it gets a new htab upon each recursive call from
135 scan_for_refs. */
136 static struct pointer_set_t *visited_nodes;
138 static bitmap_obstack ipa_obstack;
140 /* Get the name of TYPE or return the string "<UNNAMED>". */
141 static char*
142 get_name_of_type (tree type)
144 tree name = TYPE_NAME (type);
146 if (!name)
147 /* Unnamed type, do what you like here. */
148 return (char*)"<UNNAMED>";
150 /* It will be a TYPE_DECL in the case of a typedef, otherwise, an
151 identifier_node */
152 if (TREE_CODE (name) == TYPE_DECL)
154 /* Each DECL has a DECL_NAME field which contains an
155 IDENTIFIER_NODE. (Some decls, most often labels, may have
156 zero as the DECL_NAME). */
157 if (DECL_NAME (name))
158 return (char*)IDENTIFIER_POINTER (DECL_NAME (name));
159 else
160 /* Unnamed type, do what you like here. */
161 return (char*)"<UNNAMED>";
163 else if (TREE_CODE (name) == IDENTIFIER_NODE)
164 return (char*)IDENTIFIER_POINTER (name);
165 else
166 return (char*)"<UNNAMED>";
169 struct type_brand_s
171 char* name;
172 int seq;
175 /* Splay tree comparison function on type_brand_s structures. */
177 static int
178 compare_type_brand (splay_tree_key sk1, splay_tree_key sk2)
180 struct type_brand_s * k1 = (struct type_brand_s *) sk1;
181 struct type_brand_s * k2 = (struct type_brand_s *) sk2;
183 int value = strcmp(k1->name, k2->name);
184 if (value == 0)
185 return k2->seq - k1->seq;
186 else
187 return value;
190 /* All of the "unique_type" code is a hack to get around the sleazy
191 implementation used to compile more than file. Currently gcc does
192 not get rid of multiple instances of the same type that have been
193 collected from different compilation units. */
194 /* This is a trivial algorithm for removing duplicate types. This
195 would not work for any language that used structural equivalence as
196 the basis of its type system. */
197 /* Return either TYPE if this is first time TYPE has been seen an
198 compatible TYPE that has already been processed. */
200 static tree
201 discover_unique_type (tree type)
203 struct type_brand_s * brand = xmalloc (sizeof (struct type_brand_s));
204 int i = 0;
205 splay_tree_node result;
207 brand->name = get_name_of_type (type);
209 while (1)
211 brand->seq = i++;
212 result = splay_tree_lookup (all_canon_types, (splay_tree_key) brand);
214 if (result)
216 /* Create an alias since this is just the same as
217 other_type. */
218 tree other_type = (tree) result->value;
219 if (lang_hooks.types_compatible_p (type, other_type) == 1)
221 free (brand);
222 /* Insert this new type as an alias for other_type. */
223 splay_tree_insert (type_to_canon_type,
224 (splay_tree_key) type,
225 (splay_tree_value) other_type);
226 return other_type;
228 /* Not compatible, look for next instance with same name. */
230 else
232 /* No more instances, create new one since this is the first
233 time we saw this type. */
234 brand->seq = i++;
235 /* Insert the new brand. */
236 splay_tree_insert (all_canon_types,
237 (splay_tree_key) brand,
238 (splay_tree_value) type);
240 /* Insert this new type as an alias for itself. */
241 splay_tree_insert (type_to_canon_type,
242 (splay_tree_key) type,
243 (splay_tree_value) type);
245 /* Insert the uid for reverse lookup; */
246 splay_tree_insert (uid_to_canon_type,
247 (splay_tree_key) TYPE_UID (type),
248 (splay_tree_value) type);
250 bitmap_set_bit (global_types_seen, TYPE_UID (type));
251 return type;
256 /* Return true if TYPE is one of the type classes that we are willing
257 to analyze. This skips the goofy types like arrays of pointers to
258 methods. */
259 static bool
260 type_to_consider (tree type)
262 /* Strip the *'s off. */
263 type = TYPE_MAIN_VARIANT (type);
264 while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
265 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
267 switch (TREE_CODE (type))
269 case BOOLEAN_TYPE:
270 case CHAR_TYPE:
271 case COMPLEX_TYPE:
272 case ENUMERAL_TYPE:
273 case INTEGER_TYPE:
274 case QUAL_UNION_TYPE:
275 case REAL_TYPE:
276 case RECORD_TYPE:
277 case UNION_TYPE:
278 case VECTOR_TYPE:
279 case VOID_TYPE:
280 return true;
282 default:
283 return false;
287 /* Get the canon type of TYPE. If SEE_THRU_PTRS is true, remove all
288 the POINTER_TOs and if SEE_THRU_ARRAYS is true, remove all of the
289 ARRAY_OFs and POINTER_TOs. */
291 static tree
292 get_canon_type (tree type, bool see_thru_ptrs, bool see_thru_arrays)
294 splay_tree_node result;
295 /* Strip the *'s off. */
296 if (!type || !type_to_consider (type))
297 return NULL;
299 type = TYPE_MAIN_VARIANT (type);
300 if (see_thru_arrays)
301 while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
302 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
304 else if (see_thru_ptrs)
305 while (POINTER_TYPE_P (type))
306 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
308 result = splay_tree_lookup(type_to_canon_type, (splay_tree_key) type);
310 if (result == NULL)
311 return discover_unique_type (type);
312 else return (tree) result->value;
315 /* Same as GET_CANON_TYPE, except return the TYPE_ID rather than the
316 TYPE. */
318 static int
319 get_canon_type_uid (tree type, bool see_thru_ptrs, bool see_thru_arrays)
321 type = get_canon_type (type, see_thru_ptrs, see_thru_arrays);
322 if (type)
323 return TYPE_UID(type);
324 else return 0;
327 /* Return 0 if TYPE is a record or union type. Return a positive
328 number if TYPE is a pointer to a record or union. The number is
329 the number of pointer types stripped to get to the record or union
330 type. Return -1 if TYPE is none of the above. */
333 ipa_type_escape_star_count_of_interesting_type (tree type)
335 int count = 0;
336 /* Strip the *'s off. */
337 if (!type)
338 return -1;
339 type = TYPE_MAIN_VARIANT (type);
340 while (POINTER_TYPE_P (type))
342 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
343 count++;
346 /* We are interested in records, and unions only. */
347 if (TREE_CODE (type) == RECORD_TYPE
348 || TREE_CODE (type) == QUAL_UNION_TYPE
349 || TREE_CODE (type) == UNION_TYPE)
350 return count;
351 else
352 return -1;
356 /* Return 0 if TYPE is a record or union type. Return a positive
357 number if TYPE is a pointer to a record or union. The number is
358 the number of pointer types stripped to get to the record or union
359 type. Return -1 if TYPE is none of the above. */
362 ipa_type_escape_star_count_of_interesting_or_array_type (tree type)
364 int count = 0;
365 /* Strip the *'s off. */
366 if (!type)
367 return -1;
368 type = TYPE_MAIN_VARIANT (type);
369 while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
371 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
372 count++;
375 /* We are interested in records, and unions only. */
376 if (TREE_CODE (type) == RECORD_TYPE
377 || TREE_CODE (type) == QUAL_UNION_TYPE
378 || TREE_CODE (type) == UNION_TYPE)
379 return count;
380 else
381 return -1;
385 /* Return true if the record, or union TYPE passed in escapes this
386 compilation unit. Note that all of the pointer-to's are removed
387 before testing since these may not be correct. */
389 bool
390 ipa_type_escape_type_contained_p (tree type)
392 if (!initialized)
393 return false;
394 return !bitmap_bit_p (global_types_full_escape,
395 get_canon_type_uid (type, true, false));
398 /* Return true a modification to a field of type FIELD_TYPE cannot
399 clobber a record of RECORD_TYPE. */
401 bool
402 ipa_type_escape_field_does_not_clobber_p (tree record_type, tree field_type)
404 splay_tree_node result;
405 int uid;
407 if (!initialized)
408 return false;
410 /* Strip off all of the pointer tos on the record type. Strip the
411 same number of pointer tos from the field type. If the field
412 type has fewer, it could not have been aliased. */
413 record_type = TYPE_MAIN_VARIANT (record_type);
414 field_type = TYPE_MAIN_VARIANT (field_type);
415 while (POINTER_TYPE_P (record_type))
417 record_type = TYPE_MAIN_VARIANT (TREE_TYPE (record_type));
418 if (POINTER_TYPE_P (field_type))
419 field_type = TYPE_MAIN_VARIANT (TREE_TYPE (field_type));
420 else
421 /* However, if field_type is a union, this quick test is not
422 correct since one of the variants of the union may be a
423 pointer to type and we cannot see across that here. So we
424 just strip the remaining pointer tos off the record type
425 and fall thru to the more precise code. */
426 if (TREE_CODE (field_type) == QUAL_UNION_TYPE
427 || TREE_CODE (field_type) == UNION_TYPE)
429 while (POINTER_TYPE_P (record_type))
430 record_type = TYPE_MAIN_VARIANT (TREE_TYPE (record_type));
431 break;
433 else
434 return true;
437 record_type = get_canon_type (record_type, true, true);
438 /* The record type must be contained. The field type may
439 escape. */
440 if (!ipa_type_escape_type_contained_p (record_type))
441 return false;
443 uid = TYPE_UID (record_type);
444 result = splay_tree_lookup (uid_to_addressof_down_map, (splay_tree_key) uid);
446 if (result)
448 bitmap field_type_map = (bitmap) result->value;
449 uid = get_canon_type_uid (field_type, true, true);
450 /* If the bit is there, the address was taken. If not, it
451 wasn't. */
452 return !bitmap_bit_p (field_type_map, uid);
454 else
455 /* No bitmap means no addresses were taken. */
456 return true;
460 /* Add TYPE to the suspect type set. Return true if the bit needed to
461 be marked. */
463 static tree
464 mark_type (tree type, enum escape_t escape_status)
466 bitmap map = NULL;
467 int uid;
469 type = get_canon_type (type, true, true);
470 if (!type)
471 return NULL;
473 switch (escape_status)
475 case EXPOSED_PARAMETER:
476 map = global_types_exposed_parameter;
477 break;
478 case FULL_ESCAPE:
479 map = global_types_full_escape;
480 break;
483 uid = TYPE_UID (type);
484 if (bitmap_bit_p (map, uid))
485 return type;
486 else
488 bitmap_set_bit (map, uid);
489 if (escape_status == FULL_ESCAPE)
491 /* Efficiency hack. When things are bad, do not mess around
492 with this type anymore. */
493 bitmap_set_bit (global_types_exposed_parameter, uid);
496 return type;
499 /* Add interesting TYPE to the suspect type set. If the set is
500 EXPOSED_PARAMETER and the TYPE is a pointer type, the set is
501 changed to FULL_ESCAPE. */
503 static void
504 mark_interesting_type (tree type, enum escape_t escape_status)
506 if (!type) return;
507 if (ipa_type_escape_star_count_of_interesting_type (type) >= 0)
509 if ((escape_status == EXPOSED_PARAMETER)
510 && POINTER_TYPE_P (type))
511 /* EXPOSED_PARAMETERs are only structs or unions are passed by
512 value. Anything passed by reference to an external
513 function fully exposes the type. */
514 mark_type (type, FULL_ESCAPE);
515 else
516 mark_type (type, escape_status);
520 /* Return true if PARENT is supertype of CHILD. Both types must be
521 known to be structures or unions. */
523 static bool
524 parent_type_p (tree parent, tree child)
526 int i;
527 tree binfo, base_binfo;
528 if (TYPE_BINFO (parent))
529 for (binfo = TYPE_BINFO (parent), i = 0;
530 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
532 tree binfotype = BINFO_TYPE (base_binfo);
533 if (binfotype == child)
534 return true;
535 else if (parent_type_p (binfotype, child))
536 return true;
538 if (TREE_CODE (parent) == UNION_TYPE
539 || TREE_CODE (parent) == QUAL_UNION_TYPE)
541 tree field;
542 /* Search all of the variants in the union to see if one of them
543 is the child. */
544 for (field = TYPE_FIELDS (parent);
545 field;
546 field = TREE_CHAIN (field))
548 tree field_type;
549 if (TREE_CODE (field) != FIELD_DECL)
550 continue;
552 field_type = TREE_TYPE (field);
553 if (field_type == child)
554 return true;
557 /* If we did not find it, recursively ask the variants if one of
558 their children is the child type. */
559 for (field = TYPE_FIELDS (parent);
560 field;
561 field = TREE_CHAIN (field))
563 tree field_type;
564 if (TREE_CODE (field) != FIELD_DECL)
565 continue;
567 field_type = TREE_TYPE (field);
568 if (TREE_CODE (field_type) == RECORD_TYPE
569 || TREE_CODE (field_type) == QUAL_UNION_TYPE
570 || TREE_CODE (field_type) == UNION_TYPE)
571 if (parent_type_p (field_type, child))
572 return true;
576 if (TREE_CODE (parent) == RECORD_TYPE)
578 tree field;
579 for (field = TYPE_FIELDS (parent);
580 field;
581 field = TREE_CHAIN (field))
583 tree field_type;
584 if (TREE_CODE (field) != FIELD_DECL)
585 continue;
587 field_type = TREE_TYPE (field);
588 if (field_type == child)
589 return true;
590 /* You can only cast to the first field so if it does not
591 match, quit. */
592 if (TREE_CODE (field_type) == RECORD_TYPE
593 || TREE_CODE (field_type) == QUAL_UNION_TYPE
594 || TREE_CODE (field_type) == UNION_TYPE)
596 if (parent_type_p (field_type, child))
597 return true;
598 else
599 break;
603 return false;
606 /* Return the number of pointer tos for TYPE and return TYPE with all
607 of these stripped off. */
609 static int
610 count_stars (tree* type_ptr)
612 tree type = *type_ptr;
613 int i = 0;
614 type = TYPE_MAIN_VARIANT (type);
615 while (POINTER_TYPE_P (type))
617 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
618 i++;
621 *type_ptr = type;
622 return i;
625 enum cast_type {
626 CT_UP,
627 CT_DOWN,
628 CT_SIDEWAYS,
629 CT_USELESS
632 /* Check the cast FROM_TYPE to TO_TYPE. This function requires that
633 the two types have already passed the
634 ipa_type_escape_star_count_of_interesting_type test. */
636 static enum cast_type
637 check_cast_type (tree to_type, tree from_type)
639 int to_stars = count_stars (&to_type);
640 int from_stars = count_stars (&from_type);
641 if (to_stars != from_stars)
642 return CT_SIDEWAYS;
644 if (to_type == from_type)
645 return CT_USELESS;
647 if (parent_type_p (to_type, from_type)) return CT_UP;
648 if (parent_type_p (from_type, to_type)) return CT_DOWN;
649 return CT_SIDEWAYS;
652 /* Check a cast FROM this variable, TO_TYPE. Mark the escaping types
653 if appropriate. */
654 static void
655 check_cast (tree to_type, tree from)
657 tree from_type = get_canon_type (TREE_TYPE (from), false, false);
658 bool to_interesting_type, from_interesting_type;
660 to_type = get_canon_type (to_type, false, false);
661 if (!from_type || !to_type || from_type == to_type)
662 return;
664 to_interesting_type =
665 ipa_type_escape_star_count_of_interesting_type (to_type) >= 0;
666 from_interesting_type =
667 ipa_type_escape_star_count_of_interesting_type (from_type) >= 0;
669 if (to_interesting_type)
670 if (from_interesting_type)
672 /* Both types are interesting. This can be one of four types
673 of cast: useless, up, down, or sideways. We do not care
674 about up or useless. Sideways casts are always bad and
675 both sides get marked as escaping. Downcasts are not
676 interesting here because if type is marked as escaping, all
677 of its subtypes escape. */
678 switch (check_cast_type (to_type, from_type))
680 case CT_UP:
681 case CT_USELESS:
682 case CT_DOWN:
683 break;
685 case CT_SIDEWAYS:
686 mark_type (to_type, FULL_ESCAPE);
687 mark_type (from_type, FULL_ESCAPE);
688 break;
691 else
693 /* If this is a cast from the local that is a result from a
694 call to malloc, do not mark the cast as bad. */
695 if (DECL_P (from) && !bitmap_bit_p (results_of_malloc, DECL_UID (from)))
696 mark_type (to_type, FULL_ESCAPE);
698 else if (from_interesting_type)
699 mark_type (from_type, FULL_ESCAPE);
702 /* Register the parameter and return types of function FN. The type
703 ESCAPES if the function is visible outside of the compilation
704 unit. */
705 static void
706 check_function_parameter_and_return_types (tree fn, bool escapes)
708 tree arg;
710 if (TYPE_ARG_TYPES (TREE_TYPE (fn)))
712 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
713 arg && TREE_VALUE (arg) != void_type_node;
714 arg = TREE_CHAIN (arg))
716 tree type = get_canon_type (TREE_VALUE (arg), false, false);
717 if (escapes)
718 mark_interesting_type (type, EXPOSED_PARAMETER);
721 else
723 /* FIXME - According to Geoff Keating, we should never have to
724 do this; the front ends should always process the arg list
725 from the TYPE_ARG_LIST. However, Geoff is wrong, this code
726 does seem to be live. */
728 for (arg = DECL_ARGUMENTS (fn); arg; arg = TREE_CHAIN (arg))
730 tree type = get_canon_type (TREE_TYPE (arg), false, false);
731 if (escapes)
732 mark_interesting_type (type, EXPOSED_PARAMETER);
735 if (escapes)
737 tree type = get_canon_type (TREE_TYPE (TREE_TYPE (fn)), false, false);
738 mark_interesting_type (type, EXPOSED_PARAMETER);
742 /* Return true if the variable T is the right kind of static variable to
743 perform compilation unit scope escape analysis. */
745 static inline void
746 has_proper_scope_for_analysis (tree t)
748 /* If the variable has the "used" attribute, treat it as if it had a
749 been touched by the devil. */
750 tree type = get_canon_type (TREE_TYPE (t), false, false);
751 if (!type) return;
753 if (lookup_attribute ("used", DECL_ATTRIBUTES (t)))
755 mark_interesting_type (type, FULL_ESCAPE);
756 return;
759 /* Do not want to do anything with volatile except mark any
760 function that uses one to be not const or pure. */
761 if (TREE_THIS_VOLATILE (t))
762 return;
764 /* Do not care about a local automatic that is not static. */
765 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
766 return;
768 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
770 /* If the front end set the variable to be READONLY and
771 constant, we can allow this variable in pure or const
772 functions but the scope is too large for our analysis to set
773 these bits ourselves. */
775 if (TREE_READONLY (t)
776 && DECL_INITIAL (t)
777 && is_gimple_min_invariant (DECL_INITIAL (t)))
778 ; /* Read of a constant, do not change the function state. */
779 else
781 /* The type escapes for all public and externs. */
782 mark_interesting_type (type, FULL_ESCAPE);
787 /* If T is a VAR_DECL for a static that we are interested in, add the
788 uid to the bitmap. */
790 static void
791 check_operand (tree t)
793 if (!t) return;
795 /* This is an assignment from a function, register the types as
796 escaping. */
797 if (TREE_CODE (t) == FUNCTION_DECL)
798 check_function_parameter_and_return_types (t, true);
800 else if (TREE_CODE (t) == SSA_NAME)
801 has_proper_scope_for_analysis (SSA_NAME_VAR (t));
802 else if (TREE_CODE (t) == VAR_DECL)
803 has_proper_scope_for_analysis (t);
806 /* Examine tree T for references. */
808 static void
809 check_tree (tree t)
811 if ((TREE_CODE (t) == EXC_PTR_EXPR) || (TREE_CODE (t) == FILTER_EXPR))
812 return;
814 while (TREE_CODE (t) == REALPART_EXPR
815 || TREE_CODE (t) == IMAGPART_EXPR
816 || handled_component_p (t))
818 if (TREE_CODE (t) == ARRAY_REF)
819 check_operand (TREE_OPERAND (t, 1));
820 t = TREE_OPERAND (t, 0);
823 if (INDIRECT_REF_P (t))
824 /* || TREE_CODE (t) == MEM_REF) */
825 check_tree (TREE_OPERAND (t, 0));
827 if (TREE_CODE (t) == SSA_NAME)
828 t = SSA_NAME_VAR (t);
830 if (SSA_VAR_P (t) || (TREE_CODE (t) == FUNCTION_DECL))
831 check_operand (t);
834 /* Create an address_of edge FROM_TYPE.TO_TYPE. */
835 static void
836 mark_interesting_addressof (tree to_type, tree from_type)
838 int from_uid;
839 int to_uid;
840 bitmap type_map;
841 splay_tree_node result;
843 from_type = get_canon_type (from_type, false, false);
844 to_type = get_canon_type (to_type, false, false);
846 if (!from_type || !to_type)
847 return;
849 from_uid = TYPE_UID (from_type);
850 to_uid = TYPE_UID (to_type);
852 gcc_assert (ipa_type_escape_star_count_of_interesting_type (from_type) == 0);
854 /* Process the Y into X map pointer. */
855 result = splay_tree_lookup (uid_to_addressof_down_map,
856 (splay_tree_key) from_uid);
858 if (result)
859 type_map = (bitmap) result->value;
860 else
862 type_map = BITMAP_ALLOC (&ipa_obstack);
863 splay_tree_insert (uid_to_addressof_down_map,
864 from_uid,
865 (splay_tree_value)type_map);
867 bitmap_set_bit (type_map, TYPE_UID (to_type));
869 /* Process the X into Y reverse map pointer. */
870 result =
871 splay_tree_lookup (uid_to_addressof_up_map, (splay_tree_key) to_uid);
873 if (result)
874 type_map = (bitmap) result->value;
875 else
877 type_map = BITMAP_ALLOC (&ipa_obstack);
878 splay_tree_insert (uid_to_addressof_up_map,
879 to_uid,
880 (splay_tree_value)type_map);
882 bitmap_set_bit (type_map, TYPE_UID (to_type));
885 /* Scan tree T to see if there are any addresses taken in within T. */
887 static void
888 look_for_address_of (tree t)
890 if (TREE_CODE (t) == ADDR_EXPR)
892 tree x = get_base_var (t);
893 tree cref = TREE_OPERAND (t, 0);
895 /* If we have an expression of the form "&a.b.c.d", mark a.b,
896 b.c and c.d. as having its address taken. */
897 tree fielddecl = NULL_TREE;
898 while (cref!= x)
900 if (TREE_CODE (cref) == COMPONENT_REF)
902 fielddecl = TREE_OPERAND (cref, 1);
903 mark_interesting_addressof (TREE_TYPE (fielddecl),
904 DECL_FIELD_CONTEXT (fielddecl));
906 else if (TREE_CODE (cref) == ARRAY_REF)
907 get_canon_type (TREE_TYPE (cref), false, false);
909 cref = TREE_OPERAND (cref, 0);
912 if (TREE_CODE (x) == SSA_NAME)
913 has_proper_scope_for_analysis (SSA_NAME_VAR (x));
914 if (TREE_CODE (x) == VAR_DECL)
915 has_proper_scope_for_analysis (x);
920 /* Scan tree T to see if there are any casts within it.
921 LHS Is the LHS of the expression involving the cast. */
923 static void
924 look_for_casts (tree lhs __attribute__((unused)), tree t)
926 if (is_gimple_cast (t) || TREE_CODE (t) == VIEW_CONVERT_EXPR)
928 tree castfromvar = TREE_OPERAND (t, 0);
929 check_cast (TREE_TYPE (t), castfromvar);
931 else if (TREE_CODE (t) == COMPONENT_REF
932 || TREE_CODE (t) == INDIRECT_REF
933 || TREE_CODE (t) == BIT_FIELD_REF)
935 tree base = get_base_var (t);
936 while (t != base)
938 t = TREE_OPERAND (t, 0);
939 if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
941 /* This may be some part of a component ref.
942 IE it may be a.b.VIEW_CONVERT_EXPR<weird_type>(c).d, AFAIK.
943 castfromref will give you a.b.c, not a. */
944 tree castfromref = TREE_OPERAND (t, 0);
945 check_cast (TREE_TYPE (t), castfromref);
947 else if (TREE_CODE (t) == COMPONENT_REF)
948 get_canon_type (TREE_TYPE (TREE_OPERAND (t, 1)), false, false);
953 /* Check to see if T is a read or address of operation on a static var
954 we are interested in analyzing. */
956 static void
957 check_rhs_var (tree t)
959 look_for_address_of (t);
960 check_tree(t);
963 /* Check to see if T is an assignment to a static var we are
964 interested in analyzing. */
966 static void
967 check_lhs_var (tree t)
969 check_tree(t);
972 /* This is a scaled down version of get_asm_expr_operands from
973 tree_ssa_operands.c. The version there runs much later and assumes
974 that aliasing information is already available. Here we are just
975 trying to find if the set of inputs and outputs contain references
976 or address of operations to local. FN is the function being
977 analyzed and STMT is the actual asm statement. */
979 static void
980 get_asm_expr_operands (tree stmt)
982 int noutputs = list_length (ASM_OUTPUTS (stmt));
983 const char **oconstraints
984 = (const char **) alloca ((noutputs) * sizeof (const char *));
985 int i;
986 tree link;
987 const char *constraint;
988 bool allows_mem, allows_reg, is_inout;
990 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
992 oconstraints[i] = constraint
993 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
994 parse_output_constraint (&constraint, i, 0, 0,
995 &allows_mem, &allows_reg, &is_inout);
997 check_lhs_var (TREE_VALUE (link));
1000 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1002 constraint
1003 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1004 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1005 oconstraints, &allows_mem, &allows_reg);
1007 check_rhs_var (TREE_VALUE (link));
1010 /* There is no code here to check for asm memory clobbers. The
1011 casual maintainer might think that such code would be necessary,
1012 but that appears to be wrong. In other parts of the compiler,
1013 the asm memory clobbers are assumed to only clobber variables
1014 that are addressable. All types with addressable instances are
1015 assumed to already escape. So, we are protected here. */
1018 /* Check the parameters of a function call to CALL_EXPR to mark the
1019 types that pass across the function boundary. Also check to see if
1020 this is either an indirect call, a call outside the compilation
1021 unit. */
1023 static bool
1024 check_call (tree call_expr)
1026 int flags = call_expr_flags(call_expr);
1027 tree operand_list = TREE_OPERAND (call_expr, 1);
1028 tree operand;
1029 tree callee_t = get_callee_fndecl (call_expr);
1030 tree argument;
1031 struct cgraph_node* callee;
1032 enum availability avail = AVAIL_NOT_AVAILABLE;
1034 for (operand = operand_list;
1035 operand != NULL_TREE;
1036 operand = TREE_CHAIN (operand))
1038 tree argument = TREE_VALUE (operand);
1039 check_rhs_var (argument);
1042 if (callee_t)
1044 tree arg_type;
1045 tree last_arg_type = NULL;
1046 callee = cgraph_node(callee_t);
1047 avail = cgraph_function_body_availability (callee);
1049 /* Check that there are no implicit casts in the passing of
1050 parameters. */
1051 if (TYPE_ARG_TYPES (TREE_TYPE (callee_t)))
1053 operand = operand_list;
1054 for (arg_type = TYPE_ARG_TYPES (TREE_TYPE (callee_t));
1055 arg_type && TREE_VALUE (arg_type) != void_type_node;
1056 arg_type = TREE_CHAIN (arg_type))
1058 if (operand)
1060 argument = TREE_VALUE (operand);
1061 last_arg_type = TREE_VALUE(arg_type);
1062 check_cast (last_arg_type, argument);
1063 operand = TREE_CHAIN (operand);
1065 else
1066 /* The code reaches here for some unfortunate
1067 builtin functions that do not have a list of
1068 argument types. */
1069 break;
1072 else
1074 /* FIXME - According to Geoff Keating, we should never
1075 have to do this; the front ends should always process
1076 the arg list from the TYPE_ARG_LIST. */
1077 operand = operand_list;
1078 for (arg_type = DECL_ARGUMENTS (callee_t);
1079 arg_type;
1080 arg_type = TREE_CHAIN (arg_type))
1082 if (operand)
1084 argument = TREE_VALUE (operand);
1085 last_arg_type = TREE_TYPE(arg_type);
1086 check_cast (last_arg_type, argument);
1087 operand = TREE_CHAIN (operand);
1089 else
1090 /* The code reaches here for some unfortunate
1091 builtin functions that do not have a list of
1092 argument types. */
1093 break;
1097 /* In the case where we have a var_args function, we need to
1098 check the remaining parameters against the last argument. */
1099 arg_type = last_arg_type;
1100 for (;
1101 operand != NULL_TREE;
1102 operand = TREE_CHAIN (operand))
1104 argument = TREE_VALUE (operand);
1105 if (arg_type)
1106 check_cast (arg_type, argument);
1107 else
1109 /* The code reaches here for some unfortunate
1110 builtin functions that do not have a list of
1111 argument types. Most of these functions have
1112 been marked as having their parameters not
1113 escape, but for the rest, the type is doomed. */
1114 tree type = get_canon_type (TREE_TYPE (argument), false, false);
1115 mark_interesting_type (type, FULL_ESCAPE);
1120 /* The callee is either unknown (indirect call) or there is just no
1121 scannable code for it (external call) . We look to see if there
1122 are any bits available for the callee (such as by declaration or
1123 because it is builtin) and process solely on the basis of those
1124 bits. */
1126 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
1128 /* If this is a direct call to an external function, mark all of
1129 the parameter and return types. */
1130 for (operand = operand_list;
1131 operand != NULL_TREE;
1132 operand = TREE_CHAIN (operand))
1134 tree type =
1135 get_canon_type (TREE_TYPE (TREE_VALUE (operand)), false, false);
1136 mark_interesting_type (type, EXPOSED_PARAMETER);
1139 if (callee_t)
1141 tree type =
1142 get_canon_type (TREE_TYPE (TREE_TYPE (callee_t)), false, false);
1143 mark_interesting_type (type, EXPOSED_PARAMETER);
1146 return (flags & ECF_MALLOC);
1149 /* CODE is the operation on OP0 and OP1. OP0 is the operand that we
1150 *know* is a pointer type. OP1 may be a pointer type. */
1151 static bool
1152 okay_pointer_operation (enum tree_code code, tree op0, tree op1)
1154 tree op0type = TYPE_MAIN_VARIANT (TREE_TYPE (op0));
1155 tree op1type = TYPE_MAIN_VARIANT (TREE_TYPE (op1));
1156 if (POINTER_TYPE_P (op1type))
1157 return false;
1158 switch (code)
1160 case MULT_EXPR:
1161 case PLUS_EXPR:
1162 case MINUS_EXPR:
1163 /* TODO: Handle multiples of op0 size as well */
1164 if (operand_equal_p (size_in_bytes (op0type), op1, 0))
1165 return true;
1166 /* fallthrough */
1168 default:
1169 return false;
1171 return false;
1174 /* TP is the part of the tree currently under the microscope.
1175 WALK_SUBTREES is part of the walk_tree api but is unused here.
1176 DATA is cgraph_node of the function being walked. */
1178 /* FIXME: When this is converted to run over SSA form, this code
1179 should be converted to use the operand scanner. */
1181 static tree
1182 scan_for_refs (tree *tp, int *walk_subtrees, void *data)
1184 struct cgraph_node *fn = data;
1185 tree t = *tp;
1187 switch (TREE_CODE (t))
1189 case VAR_DECL:
1190 if (DECL_INITIAL (t))
1191 walk_tree (&DECL_INITIAL (t), scan_for_refs, fn, visited_nodes);
1192 *walk_subtrees = 0;
1193 break;
1195 case MODIFY_EXPR:
1197 /* First look on the lhs and see what variable is stored to */
1198 tree lhs = TREE_OPERAND (t, 0);
1199 tree rhs = TREE_OPERAND (t, 1);
1201 check_lhs_var (lhs);
1202 check_cast (TREE_TYPE (lhs), rhs);
1204 /* For the purposes of figuring out what the cast affects */
1206 /* Next check the operands on the rhs to see if they are ok. */
1207 switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
1209 case tcc_binary:
1211 tree op0 = TREE_OPERAND (rhs, 0);
1212 tree type0 = get_canon_type (TREE_TYPE (op0), false, false);
1213 tree op1 = TREE_OPERAND (rhs, 1);
1214 tree type1 = get_canon_type (TREE_TYPE (op1), false, false);
1216 /* If this is pointer arithmetic of any bad sort, then
1217 we need to mark the types as bad. For binary
1218 operations, no binary operator we currently support
1219 is always "safe" in regard to what it would do to
1220 pointers for purposes of determining which types
1221 escape, except operations of the size of the type.
1222 It is possible that min and max under the right set
1223 of circumstances and if the moon is in the correct
1224 place could be safe, but it is hard to see how this
1225 is worth the effort. */
1227 if (type0 && POINTER_TYPE_P (type0)
1228 && !okay_pointer_operation (TREE_CODE (rhs), op0, op1))
1229 mark_interesting_type (type0, FULL_ESCAPE);
1230 if (type1 && POINTER_TYPE_P (type1)
1231 && !okay_pointer_operation (TREE_CODE (rhs), op1, op0))
1232 mark_interesting_type (type1, FULL_ESCAPE);
1234 look_for_casts (lhs, op0);
1235 look_for_casts (lhs, op1);
1236 check_rhs_var (op0);
1237 check_rhs_var (op1);
1239 break;
1240 case tcc_unary:
1242 tree op0 = TREE_OPERAND (rhs, 0);
1243 tree type0 = get_canon_type (TREE_TYPE (op0), false, false);
1244 /* For unary operations, if the operation is NEGATE or
1245 ABS on a pointer, this is also considered pointer
1246 arithmetic and thus, bad for business. */
1247 if (type0 && (TREE_CODE (op0) == NEGATE_EXPR
1248 || TREE_CODE (op0) == ABS_EXPR)
1249 && POINTER_TYPE_P (type0))
1251 mark_interesting_type (type0, FULL_ESCAPE);
1253 check_rhs_var (op0);
1254 look_for_casts (lhs, op0);
1255 look_for_casts (lhs, rhs);
1258 break;
1259 case tcc_reference:
1260 look_for_casts (lhs, rhs);
1261 check_rhs_var (rhs);
1262 break;
1263 case tcc_declaration:
1264 check_rhs_var (rhs);
1265 break;
1266 case tcc_expression:
1267 switch (TREE_CODE (rhs))
1269 case ADDR_EXPR:
1270 look_for_casts (lhs, TREE_OPERAND (rhs, 0));
1271 check_rhs_var (rhs);
1272 break;
1273 case CALL_EXPR:
1274 /* If this is a call to malloc, squirrel away the
1275 result so we do mark the resulting cast as being
1276 bad. */
1277 if (check_call (rhs))
1279 if (TREE_CODE (lhs) == SSA_NAME)
1280 lhs = SSA_NAME_VAR (lhs);
1281 bitmap_set_bit (results_of_malloc, DECL_UID (lhs));
1283 break;
1284 default:
1285 break;
1287 break;
1288 default:
1289 break;
1291 *walk_subtrees = 0;
1293 break;
1295 case ADDR_EXPR:
1296 /* This case is here to find addresses on rhs of constructors in
1297 decl_initial of static variables. */
1298 check_rhs_var (t);
1299 *walk_subtrees = 0;
1300 break;
1302 case CALL_EXPR:
1303 check_call (t);
1304 *walk_subtrees = 0;
1305 break;
1307 case ASM_EXPR:
1308 get_asm_expr_operands (t);
1309 *walk_subtrees = 0;
1310 break;
1312 default:
1313 break;
1315 return NULL;
1319 /* The init routine for analyzing global static variable usage. See
1320 comments at top for description. */
1321 static void
1322 ipa_init (void)
1324 bitmap_obstack_initialize (&ipa_obstack);
1325 global_types_exposed_parameter = BITMAP_ALLOC (&ipa_obstack);
1326 global_types_full_escape = BITMAP_ALLOC (&ipa_obstack);
1327 global_types_seen = BITMAP_ALLOC (&ipa_obstack);
1328 results_of_malloc = BITMAP_ALLOC (&ipa_obstack);
1330 uid_to_canon_type = splay_tree_new (splay_tree_compare_ints, 0, 0);
1331 all_canon_types = splay_tree_new (compare_type_brand, 0, 0);
1332 type_to_canon_type = splay_tree_new (splay_tree_compare_pointers, 0, 0);
1333 uid_to_subtype_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1334 uid_to_addressof_down_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1335 uid_to_addressof_up_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1337 /* There are some shared nodes, in particular the initializers on
1338 static declarations. We do not need to scan them more than once
1339 since all we would be interested in are the addressof
1340 operations. */
1341 visited_nodes = pointer_set_create ();
1342 initialized = true;
1345 /* Check out the rhs of a static or global initialization VNODE to see
1346 if any of them contain addressof operations. Note that some of
1347 these variables may not even be referenced in the code in this
1348 compilation unit but their right hand sides may contain references
1349 to variables defined within this unit. */
1351 static void
1352 analyze_variable (struct cgraph_varpool_node *vnode)
1354 tree global = vnode->decl;
1355 tree type = get_canon_type (TREE_TYPE (global), false, false);
1357 /* If this variable has exposure beyond the compilation unit, add
1358 its type to the global types. */
1360 if (vnode->externally_visible)
1361 mark_interesting_type (type, FULL_ESCAPE);
1363 if (TREE_CODE (global) == VAR_DECL)
1365 if (DECL_INITIAL (global))
1366 walk_tree (&DECL_INITIAL (global), scan_for_refs,
1367 NULL, visited_nodes);
1369 else abort();
1372 /* This is the main routine for finding the reference patterns for
1373 global variables within a function FN. */
1375 static void
1376 analyze_function (struct cgraph_node *fn)
1378 tree decl = fn->decl;
1379 check_function_parameter_and_return_types (decl,
1380 fn->local.externally_visible);
1381 if (dump_file)
1382 fprintf (dump_file, "\n local analysis of %s", cgraph_node_name (fn));
1385 struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
1386 basic_block this_block;
1388 FOR_EACH_BB_FN (this_block, this_cfun)
1390 block_stmt_iterator bsi;
1391 for (bsi = bsi_start (this_block); !bsi_end_p (bsi); bsi_next (&bsi))
1392 walk_tree (bsi_stmt_ptr (bsi), scan_for_refs,
1393 fn, visited_nodes);
1397 /* There may be const decls with interesting right hand sides. */
1398 if (DECL_STRUCT_FUNCTION (decl))
1400 tree step;
1401 for (step = DECL_STRUCT_FUNCTION (decl)->unexpanded_var_list;
1402 step;
1403 step = TREE_CHAIN (step))
1405 tree var = TREE_VALUE (step);
1406 if (TREE_CODE (var) == VAR_DECL
1407 && DECL_INITIAL (var)
1408 && !TREE_STATIC (var))
1409 walk_tree (&DECL_INITIAL (var), scan_for_refs,
1410 fn, visited_nodes);
1411 get_canon_type (TREE_TYPE (var), false, false);
1418 /* Convert a type_UID into a type. */
1419 static tree
1420 type_for_uid (int uid)
1422 splay_tree_node result =
1423 splay_tree_lookup (uid_to_canon_type, (splay_tree_key) uid);
1425 if (result)
1426 return (tree) result->value;
1427 else return NULL;
1430 /* Return the a bitmap with the subtypes of the type for UID. If it
1431 does not exist, return either NULL or a new bitmap depending on the
1432 value of CREATE. */
1434 static bitmap
1435 subtype_map_for_uid (int uid, bool create)
1437 splay_tree_node result = splay_tree_lookup (uid_to_subtype_map,
1438 (splay_tree_key) uid);
1440 if (result)
1441 return (bitmap) result->value;
1442 else if (create)
1444 bitmap subtype_map = BITMAP_ALLOC (&ipa_obstack);
1445 splay_tree_insert (uid_to_subtype_map,
1446 uid,
1447 (splay_tree_value)subtype_map);
1448 return subtype_map;
1450 else return NULL;
1453 /* Mark all of the supertypes and field types of TYPE as being seen.
1454 Also accumulate the subtypes for each type so that
1455 close_types_full_escape can mark a subtype as escaping if the
1456 supertype escapes. */
1458 static void
1459 close_type_seen (tree type)
1461 tree field;
1462 int i, uid;
1463 tree binfo, base_binfo;
1465 /* See thru all pointer tos and array ofs. */
1466 type = get_canon_type (type, true, true);
1467 if (!type)
1468 return;
1470 uid = TYPE_UID (type);
1472 if (bitmap_bit_p (been_there_done_that, uid))
1473 return;
1474 bitmap_set_bit (been_there_done_that, uid);
1476 /* If we are doing a language with a type hierarchy, mark all of
1477 the superclasses. */
1478 if (TYPE_BINFO (type))
1479 for (binfo = TYPE_BINFO (type), i = 0;
1480 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1482 tree binfo_type = BINFO_TYPE (base_binfo);
1483 bitmap subtype_map = subtype_map_for_uid
1484 (TYPE_UID (TYPE_MAIN_VARIANT (binfo_type)), true);
1485 bitmap_set_bit (subtype_map, uid);
1486 close_type_seen (get_canon_type (binfo_type, true, true));
1489 /* If the field is a struct or union type, mark all of the
1490 subfields. */
1491 for (field = TYPE_FIELDS (type);
1492 field;
1493 field = TREE_CHAIN (field))
1495 tree field_type;
1496 if (TREE_CODE (field) != FIELD_DECL)
1497 continue;
1499 field_type = TREE_TYPE (field);
1500 if (ipa_type_escape_star_count_of_interesting_or_array_type (field_type) >= 0)
1501 close_type_seen (get_canon_type (field_type, true, true));
1505 /* Take a TYPE that has been passed by value to an external function
1506 and mark all of the fields that have pointer types as escaping. For
1507 any of the non pointer types that are structures or unions,
1508 recurse. TYPE is never a pointer type. */
1510 static void
1511 close_type_exposed_parameter (tree type)
1513 tree field;
1514 int uid;
1516 type = get_canon_type (type, false, false);
1517 if (!type)
1518 return;
1519 uid = TYPE_UID (type);
1520 gcc_assert (!POINTER_TYPE_P (type));
1522 if (bitmap_bit_p (been_there_done_that, uid))
1523 return;
1524 bitmap_set_bit (been_there_done_that, uid);
1526 /* If the field is a struct or union type, mark all of the
1527 subfields. */
1528 for (field = TYPE_FIELDS (type);
1529 field;
1530 field = TREE_CHAIN (field))
1532 tree field_type;
1534 if (TREE_CODE (field) != FIELD_DECL)
1535 continue;
1537 field_type = get_canon_type (TREE_TYPE (field), false, false);
1538 mark_interesting_type (field_type, EXPOSED_PARAMETER);
1540 /* Only recurse for non pointer types of structures and unions. */
1541 if (ipa_type_escape_star_count_of_interesting_type (field_type) == 0)
1542 close_type_exposed_parameter (field_type);
1546 /* The next function handles the case where a type fully escapes.
1547 This means that not only does the type itself escape,
1549 a) the type of every field recursively escapes
1550 b) the type of every subtype escapes as well as the super as well
1551 as all of the pointer to types for each field.
1553 Note that pointer to types are not marked as escaping. If the
1554 pointed to type escapes, the pointer to type also escapes.
1556 Take a TYPE that has had the address taken for an instance of it
1557 and mark all of the types for its fields as having their addresses
1558 taken. */
1560 static void
1561 close_type_full_escape (tree type)
1563 tree field;
1564 unsigned int i;
1565 int uid;
1566 tree binfo, base_binfo;
1567 bitmap_iterator bi;
1568 bitmap subtype_map;
1569 splay_tree_node address_result;
1571 /* Strip off any pointer or array types. */
1572 type = get_canon_type (type, true, true);
1573 if (!type)
1574 return;
1575 uid = TYPE_UID (type);
1577 if (bitmap_bit_p (been_there_done_that, uid))
1578 return;
1579 bitmap_set_bit (been_there_done_that, uid);
1581 subtype_map = subtype_map_for_uid (uid, false);
1583 /* If we are doing a language with a type hierarchy, mark all of
1584 the superclasses. */
1585 if (TYPE_BINFO (type))
1586 for (binfo = TYPE_BINFO (type), i = 0;
1587 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1589 tree binfotype = BINFO_TYPE (base_binfo);
1590 binfotype = mark_type (binfotype, FULL_ESCAPE);
1591 close_type_full_escape (binfotype);
1594 /* Mark as escaped any types that have been down casted to
1595 this type. */
1596 if (subtype_map)
1597 EXECUTE_IF_SET_IN_BITMAP (subtype_map, 0, i, bi)
1599 tree subtype = type_for_uid (i);
1600 subtype = mark_type (subtype, FULL_ESCAPE);
1601 close_type_full_escape (subtype);
1604 /* If the field is a struct or union type, mark all of the
1605 subfields. */
1606 for (field = TYPE_FIELDS (type);
1607 field;
1608 field = TREE_CHAIN (field))
1610 tree field_type;
1611 if (TREE_CODE (field) != FIELD_DECL)
1612 continue;
1614 field_type = TREE_TYPE (field);
1615 if (ipa_type_escape_star_count_of_interesting_or_array_type (field_type) >= 0)
1617 field_type = mark_type (field_type, FULL_ESCAPE);
1618 close_type_full_escape (field_type);
1622 /* For all of the types A that contain this type B and were part of
1623 an expression like "&...A.B...", mark the A's as escaping. */
1624 address_result = splay_tree_lookup (uid_to_addressof_up_map,
1625 (splay_tree_key) uid);
1626 if (address_result)
1628 bitmap containing_classes = (bitmap) address_result->value;
1629 EXECUTE_IF_SET_IN_BITMAP (containing_classes, 0, i, bi)
1631 close_type_full_escape (type_for_uid (i));
1636 /* Transitively close the addressof bitmap for the type with UID.
1637 This means that if we had a.b and b.c, a would have both b and c in
1638 its maps. */
1640 static bitmap
1641 close_addressof_down (int uid)
1643 bitmap_iterator bi;
1644 splay_tree_node result =
1645 splay_tree_lookup (uid_to_addressof_down_map, (splay_tree_key) uid);
1646 bitmap map = NULL;
1647 bitmap new_map;
1648 unsigned int i;
1650 if (result)
1651 map = (bitmap) result->value;
1652 else
1653 return NULL;
1655 if (bitmap_bit_p (been_there_done_that, uid))
1656 return map;
1657 bitmap_set_bit (been_there_done_that, uid);
1659 /* If the type escapes, get rid of the addressof map, it will not be
1660 needed. */
1661 if (bitmap_bit_p (global_types_full_escape, uid))
1663 BITMAP_FREE (map);
1664 splay_tree_remove (uid_to_addressof_down_map, (splay_tree_key) uid);
1665 return NULL;
1668 /* The new_map will have all of the bits for the enclosed fields and
1669 will have the unique id version of the old map. */
1670 new_map = BITMAP_ALLOC (&ipa_obstack);
1672 EXECUTE_IF_SET_IN_BITMAP (map, 0, i, bi)
1674 bitmap submap = close_addressof_down (i);
1675 bitmap_set_bit (new_map, i);
1676 if (submap)
1677 bitmap_ior_into (new_map, submap);
1679 result->value = (splay_tree_value) new_map;
1681 BITMAP_FREE (map);
1682 return new_map;
1686 /* The main entry point for type escape analysis. */
1688 static void
1689 type_escape_execute (void)
1691 struct cgraph_node *node;
1692 struct cgraph_varpool_node *vnode;
1693 unsigned int i;
1694 bitmap_iterator bi;
1695 splay_tree_node result;
1697 ipa_init ();
1699 /* Process all of the variables first. */
1700 for (vnode = cgraph_varpool_nodes_queue; vnode; vnode = vnode->next_needed)
1701 analyze_variable (vnode);
1703 /* Process all of the functions. next
1705 We do not want to process any of the clones so we check that this
1706 is a master clone. However, we do need to process any
1707 AVAIL_OVERWRITABLE functions (these are never clones) because
1708 they may cause a type variable to escape.
1710 for (node = cgraph_nodes; node; node = node->next)
1711 if (node->analyzed
1712 && (cgraph_is_master_clone (node)
1713 || (cgraph_function_body_availability (node) == AVAIL_OVERWRITABLE)))
1714 analyze_function (node);
1717 pointer_set_destroy (visited_nodes);
1718 visited_nodes = NULL;
1720 /* Do all of the closures to discover which types escape the
1721 compilation unit. */
1723 been_there_done_that = BITMAP_ALLOC (&ipa_obstack);
1724 bitmap_tmp = BITMAP_ALLOC (&ipa_obstack);
1726 /* Examine the types that we have directly seen in scanning the code
1727 and add to that any contained types or superclasses. */
1729 bitmap_copy (bitmap_tmp, global_types_seen);
1730 EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
1732 tree type = type_for_uid (i);
1733 /* Only look at records and unions and pointer tos. */
1734 if (ipa_type_escape_star_count_of_interesting_or_array_type (type) >= 0)
1735 close_type_seen (type);
1737 bitmap_clear (been_there_done_that);
1739 /* Examine all of the types passed by value and mark any enclosed
1740 pointer types as escaping. */
1741 bitmap_copy (bitmap_tmp, global_types_exposed_parameter);
1742 EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
1744 close_type_exposed_parameter (type_for_uid (i));
1746 bitmap_clear (been_there_done_that);
1748 /* Close the types for escape. If something escapes, then any
1749 enclosed types escape as well as any subtypes. */
1750 bitmap_copy (bitmap_tmp, global_types_full_escape);
1751 EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
1753 close_type_full_escape (type_for_uid (i));
1755 bitmap_clear (been_there_done_that);
1757 /* Before this pass, the uid_to_addressof_down_map for type X
1758 contained an entry for Y if there had been an operation of the
1759 form &X.Y. This step adds all of the fields contained within Y
1760 (recursively) to X's map. */
1762 result = splay_tree_min (uid_to_addressof_down_map);
1763 while (result)
1765 int uid = result->key;
1766 /* Close the addressof map, i.e. copy all of the transitive
1767 substructures up to this level. */
1768 close_addressof_down (uid);
1769 result = splay_tree_successor (uid_to_addressof_down_map, uid);
1772 /* Do not need the array types and pointer types in the persistent
1773 data structures. */
1774 result = splay_tree_min (all_canon_types);
1775 while (result)
1777 tree type = (tree) result->value;
1778 tree key = (tree) result->key;
1779 if (POINTER_TYPE_P (type)
1780 || TREE_CODE (type) == ARRAY_TYPE)
1782 splay_tree_remove (all_canon_types, (splay_tree_key) result->key);
1783 splay_tree_remove (type_to_canon_type, (splay_tree_key) type);
1784 splay_tree_remove (uid_to_canon_type, (splay_tree_key) TYPE_UID (type));
1785 bitmap_clear_bit (global_types_seen, TYPE_UID (type));
1787 result = splay_tree_successor (all_canon_types, (splay_tree_key) key);
1790 /* { */
1791 /* FILE * tmp = dump_file; */
1792 /* dump_file = stderr; */
1793 if (dump_file)
1795 EXECUTE_IF_SET_IN_BITMAP (global_types_seen, 0, i, bi)
1797 /* The pointer types are in the global_types_full_escape
1798 bitmap but not in the backwards map. They also contain
1799 no useful information since they are not marked. */
1800 tree type = type_for_uid (i);
1801 fprintf(dump_file, "type %d ", i);
1802 print_generic_expr (dump_file, type, 0);
1803 if (bitmap_bit_p (global_types_full_escape, i))
1804 fprintf(dump_file, " escaped\n");
1805 else
1806 fprintf(dump_file, " contained\n");
1809 /* dump_file = tmp; */
1810 /* } */
1812 /* Get rid of uid_to_addressof_up_map and its bitmaps. */
1813 result = splay_tree_min (uid_to_addressof_up_map);
1814 while (result)
1816 int uid = (int)result->key;
1817 bitmap bm = (bitmap)result->value;
1819 BITMAP_FREE (bm);
1820 splay_tree_remove (uid_to_addressof_up_map, (splay_tree_key) uid);
1821 result = splay_tree_successor (uid_to_addressof_up_map, uid);
1824 /* Get rid of the subtype map. */
1825 result = splay_tree_min (uid_to_subtype_map);
1826 while (result)
1828 bitmap b = (bitmap)result->value;
1829 BITMAP_FREE(b);
1830 splay_tree_remove (uid_to_subtype_map, result->key);
1831 result = splay_tree_min (uid_to_subtype_map);
1833 splay_tree_delete (uid_to_subtype_map);
1834 uid_to_subtype_map = NULL;
1836 BITMAP_FREE (global_types_exposed_parameter);
1837 BITMAP_FREE (been_there_done_that);
1838 BITMAP_FREE (bitmap_tmp);
1839 BITMAP_FREE (results_of_malloc);
1842 static bool
1843 gate_type_escape_vars (void)
1845 return (flag_unit_at_a_time != 0 && flag_ipa_type_escape
1846 /* Don't bother doing anything if the program has errors. */
1847 && !(errorcount || sorrycount));
1850 struct tree_opt_pass pass_ipa_type_escape =
1852 "type-escape-var", /* name */
1853 gate_type_escape_vars, /* gate */
1854 type_escape_execute, /* execute */
1855 NULL, /* sub */
1856 NULL, /* next */
1857 0, /* static_pass_number */
1858 TV_IPA_TYPE_ESCAPE, /* tv_id */
1859 0, /* properties_required */
1860 0, /* properties_provided */
1861 0, /* properties_destroyed */
1862 0, /* todo_flags_start */
1863 0, /* todo_flags_finish */
1864 0 /* letter */