1 /* Type based alias analysis.
2 Copyright (C) 2004, 2005, 2006 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
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
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
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
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. */
38 #include "coretypes.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"
47 #include "ipa-utils.h"
48 #include "ipa-type-escape.h"
50 #include "tree-gimple.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 /* Scratch bitmap for avoiding work. */
64 static bitmap been_there_done_that;
65 static bitmap bitmap_tmp;
67 /* There are two levels of escape that types can undergo.
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
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.
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. */
94 static bitmap global_types_exposed_parameter;
95 static bitmap global_types_full_escape;
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
102 static splay_tree uid_to_canon_type;
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;
108 /* Map from type clones to the single canon type. */
109 static splay_tree type_to_canon_type;
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;
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;
121 /* Tree to hold the subtype maps used to mark subtypes of escaped
123 static splay_tree uid_to_subtype_map;
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
129 static struct pointer_set_t *visited_nodes;
131 /* Visited stmts by walk_use_def_chains function because it's called
133 static struct pointer_set_t *visited_stmts;
135 static bitmap_obstack ipa_obstack;
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 *);
142 /* Get the name of TYPE or return the string "<UNNAMED>". */
144 get_name_of_type (tree type)
146 tree name = TYPE_NAME (type);
149 /* Unnamed type, do what you like here. */
150 return (char*)"<UNNAMED>";
152 /* It will be a TYPE_DECL in the case of a typedef, otherwise, an
154 if (TREE_CODE (name) == TYPE_DECL)
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));
162 /* Unnamed type, do what you like here. */
163 return (char*)"<UNNAMED>";
165 else if (TREE_CODE (name) == IDENTIFIER_NODE)
166 return (char*)IDENTIFIER_POINTER (name);
168 return (char*)"<UNNAMED>";
177 /* Splay tree comparison function on type_brand_s structures. */
180 compare_type_brand (splay_tree_key sk1, splay_tree_key sk2)
182 struct type_brand_s * k1 = (struct type_brand_s *) sk1;
183 struct type_brand_s * k2 = (struct type_brand_s *) sk2;
185 int value = strcmp(k1->name, k2->name);
187 return k2->seq - k1->seq;
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. */
203 discover_unique_type (tree type)
205 struct type_brand_s * brand = XNEW (struct type_brand_s);
207 splay_tree_node result;
209 brand->name = get_name_of_type (type);
214 result = splay_tree_lookup (all_canon_types, (splay_tree_key) brand);
218 /* Create an alias since this is just the same as
220 tree other_type = (tree) result->value;
221 if (lang_hooks.types_compatible_p (type, other_type) == 1)
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);
230 /* Not compatible, look for next instance with same name. */
234 /* No more instances, create new one since this is the first
235 time we saw this type. */
237 /* Insert the new brand. */
238 splay_tree_insert (all_canon_types,
239 (splay_tree_key) brand,
240 (splay_tree_value) type);
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);
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);
252 bitmap_set_bit (global_types_seen, TYPE_UID (type));
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
262 type_to_consider (tree type)
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));
269 switch (TREE_CODE (type))
275 case QUAL_UNION_TYPE:
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. */
293 get_canon_type (tree type, bool see_thru_ptrs, bool see_thru_arrays)
295 splay_tree_node result;
296 /* Strip the *'s off. */
297 if (!type || !type_to_consider (type))
300 type = TYPE_MAIN_VARIANT (type);
302 while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
303 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
305 else if (see_thru_ptrs)
306 while (POINTER_TYPE_P (type))
307 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
309 result = splay_tree_lookup(type_to_canon_type, (splay_tree_key) type);
312 return discover_unique_type (type);
313 else return (tree) result->value;
316 /* Same as GET_CANON_TYPE, except return the TYPE_ID rather than the
320 get_canon_type_uid (tree type, bool see_thru_ptrs, bool see_thru_arrays)
322 type = get_canon_type (type, see_thru_ptrs, see_thru_arrays);
324 return TYPE_UID(type);
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. */
334 ipa_type_escape_star_count_of_interesting_type (tree type)
337 /* Strip the *'s off. */
340 type = TYPE_MAIN_VARIANT (type);
341 while (POINTER_TYPE_P (type))
343 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
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)
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. */
363 ipa_type_escape_star_count_of_interesting_or_array_type (tree type)
366 /* Strip the *'s off. */
369 type = TYPE_MAIN_VARIANT (type);
370 while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
372 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
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)
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. */
391 ipa_type_escape_type_contained_p (tree type)
395 return !bitmap_bit_p (global_types_full_escape,
396 get_canon_type_uid (type, true, false));
399 /* Return true if a modification to a field of type FIELD_TYPE cannot
400 clobber a record of RECORD_TYPE. */
403 ipa_type_escape_field_does_not_clobber_p (tree record_type, tree field_type)
405 splay_tree_node result;
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))
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));
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)
430 while (POINTER_TYPE_P (record_type))
431 record_type = TYPE_MAIN_VARIANT (TREE_TYPE (record_type));
438 record_type = get_canon_type (record_type, true, true);
439 /* The record type must be contained. The field type may
441 if (!ipa_type_escape_type_contained_p (record_type))
444 uid = TYPE_UID (record_type);
445 result = splay_tree_lookup (uid_to_addressof_down_map, (splay_tree_key) uid);
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
453 return !bitmap_bit_p (field_type_map, uid);
456 /* No bitmap means no addresses were taken. */
461 /* Add TYPE to the suspect type set. Return true if the bit needed to
465 mark_type (tree type, enum escape_t escape_status)
470 type = get_canon_type (type, true, true);
474 switch (escape_status)
476 case EXPOSED_PARAMETER:
477 map = global_types_exposed_parameter;
480 map = global_types_full_escape;
484 uid = TYPE_UID (type);
485 if (bitmap_bit_p (map, uid))
489 bitmap_set_bit (map, uid);
490 if (escape_status == FULL_ESCAPE)
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);
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. */
505 mark_interesting_type (tree type, enum escape_t escape_status)
508 if (ipa_type_escape_star_count_of_interesting_type (type) >= 0)
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);
517 mark_type (type, escape_status);
521 /* Return true if PARENT is supertype of CHILD. Both types must be
522 known to be structures or unions. */
525 parent_type_p (tree parent, tree child)
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++)
533 tree binfotype = BINFO_TYPE (base_binfo);
534 if (binfotype == child)
536 else if (parent_type_p (binfotype, child))
539 if (TREE_CODE (parent) == UNION_TYPE
540 || TREE_CODE (parent) == QUAL_UNION_TYPE)
543 /* Search all of the variants in the union to see if one of them
545 for (field = TYPE_FIELDS (parent);
547 field = TREE_CHAIN (field))
550 if (TREE_CODE (field) != FIELD_DECL)
553 field_type = TREE_TYPE (field);
554 if (field_type == child)
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);
562 field = TREE_CHAIN (field))
565 if (TREE_CODE (field) != FIELD_DECL)
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))
577 if (TREE_CODE (parent) == RECORD_TYPE)
580 for (field = TYPE_FIELDS (parent);
582 field = TREE_CHAIN (field))
585 if (TREE_CODE (field) != FIELD_DECL)
588 field_type = TREE_TYPE (field);
589 if (field_type == child)
591 /* You can only cast to the first field so if it does not
593 if (TREE_CODE (field_type) == RECORD_TYPE
594 || TREE_CODE (field_type) == QUAL_UNION_TYPE
595 || TREE_CODE (field_type) == UNION_TYPE)
597 if (parent_type_p (field_type, child))
607 /* Return the number of pointer tos for TYPE and return TYPE with all
608 of these stripped off. */
611 count_stars (tree* type_ptr)
613 tree type = *type_ptr;
615 type = TYPE_MAIN_VARIANT (type);
616 while (POINTER_TYPE_P (type))
618 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
631 CT_FROM_P_BAD = 0x10,
632 CT_FROM_NON_P = 0x20,
633 CT_TO_NON_INTER = 0x40,
634 CT_FROM_MALLOC = 0x80,
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. */
642 static enum cast_type
643 check_cast_type (tree to_type, tree from_type)
645 int to_stars = count_stars (&to_type);
646 int from_stars = count_stars (&from_type);
647 if (to_stars != from_stars)
650 if (to_type == from_type)
653 if (parent_type_p (to_type, from_type)) return CT_UP;
654 if (parent_type_p (from_type, to_type)) return CT_DOWN;
658 /* This function returns non-zero if VAR is result of call
659 to malloc function. */
662 is_malloc_result (tree var)
671 if (SSA_NAME_IS_DEFAULT_DEF (var))
674 def_stmt = SSA_NAME_DEF_STMT (var);
676 if (TREE_CODE (def_stmt) != GIMPLE_MODIFY_STMT)
679 if (var != GIMPLE_STMT_OPERAND (def_stmt, 0))
682 rhs = get_call_expr_in (def_stmt);
687 flags = call_expr_flags (rhs);
689 return ((flags & ECF_MALLOC) != 0);
693 /* Check a cast FROM this variable, TO_TYPE. Mark the escaping types
694 if appropriate. Returns cast_type as detected. */
696 static enum cast_type
697 check_cast (tree to_type, tree from)
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;
703 to_type = get_canon_type (to_type, false, false);
704 if (!from_type || !to_type || from_type == to_type)
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;
712 if (to_interesting_type)
713 if (from_interesting_type)
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);
730 mark_type (to_type, FULL_ESCAPE);
731 mark_type (from_type, FULL_ESCAPE);
740 /* This code excludes two cases from marking as escaped:
742 1. if this is a cast of index of array of structures/unions
743 that happens before accessing array element, we should not
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.
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;
757 cast = CT_FROM_P_BAD;
758 mark_type (to_type, FULL_ESCAPE);
761 else if (from_interesting_type)
763 mark_type (from_type, FULL_ESCAPE);
764 cast = CT_TO_NON_INTER;
776 /* This function is a callback for walk_tree called from
777 is_cast_from_non_pointer. The data->type is set to be:
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
783 If data->type = number, then data->stmt will contain the
784 last casting stmt met in traversing. */
787 is_cast_from_non_pointer_1 (tree *tp, int *walk_subtrees, void *data)
792 if (pointer_set_insert (visited_stmts, def_stmt))
798 switch (TREE_CODE (def_stmt))
800 case GIMPLE_MODIFY_STMT:
804 tree lhs = GIMPLE_STMT_OPERAND (def_stmt, 0);
805 tree rhs = GIMPLE_STMT_OPERAND (def_stmt, 1);
807 unsigned int cast = look_for_casts (lhs, rhs);
808 /* Check that only one cast happened, and it's of
810 if ((cast & CT_FROM_NON_P) == (CT_FROM_NON_P)
811 && (cast & ~(CT_FROM_NON_P)) == 0)
813 ((cast_t *)data)->stmt = def_stmt;
814 ((cast_t *)data)->type++;
816 FOR_EACH_SSA_USE_OPERAND (use_p, def_stmt, iter, SSA_OP_ALL_USES)
818 walk_use_def_chains (USE_FROM_PTR (use_p), is_cast_from_non_pointer,
820 if (((cast_t*)data)->type == -1)
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))
832 FOR_EACH_SSA_USE_OPERAND (use_p, def_stmt, iter, SSA_OP_ALL_USES)
834 walk_use_def_chains (USE_FROM_PTR (use_p), is_cast_from_non_pointer,
836 if (((cast_t*)data)->type == -1)
841 /* The cast is harmful. */
844 ((cast_t *)data)->type = -1;
862 /* This function is a callback for walk_use_def_chains function called
863 from is_array_access_through_pointer_and_index. */
866 is_cast_from_non_pointer (tree var, tree def_stmt, void *data)
869 if (!def_stmt || !var)
872 if (TREE_CODE (def_stmt) == PHI_NODE)
875 if (SSA_NAME_IS_DEFAULT_DEF (var))
878 walk_tree (&def_stmt, is_cast_from_non_pointer_1, data, NULL);
879 if (((cast_t*)data)->type == -1)
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
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;
893 D.1608_9 = D.1606_7 + a_p.2_8;
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.
898 This function checks that:
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;).
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; ).
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?)
911 The following items are checked implicitly by the end of algorithm:
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)).
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).
924 In total, 1-5 guaranty that it's an access to array by pointer and index.
929 is_array_access_through_pointer_and_index (tree op0, tree op1)
931 tree base, offset, offset_cast_stmt;
932 tree before_cast, before_cast_def_stmt;
933 cast_t op0_cast, op1_cast;
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;
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);
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);
949 if (op0_cast.type == 1 && op1_cast.type == 0)
953 offset_cast_stmt = op0_cast.stmt;
955 else if (op0_cast.type == 0 && op1_cast.type == 1)
959 offset_cast_stmt = op1_cast.stmt;
965 offset_cast_stmt is of the form:
966 D.1606_7 = (struct str_t *) D.1605_6; */
968 before_cast = SINGLE_SSA_TREE_OPERAND (offset_cast_stmt, SSA_OP_USE);
972 if (SSA_NAME_IS_DEFAULT_DEF(before_cast))
975 before_cast_def_stmt = SSA_NAME_DEF_STMT (before_cast);
976 if (!before_cast_def_stmt)
979 /* before_cast_def_stmt should be of the form:
980 D.1605_6 = i.1_5 * 16; */
982 if (TREE_CODE (before_cast_def_stmt) == GIMPLE_MODIFY_STMT)
984 tree lhs = GIMPLE_STMT_OPERAND (before_cast_def_stmt,0);
985 tree rhs = GIMPLE_STMT_OPERAND (before_cast_def_stmt,1);
987 /* We expect temporary here. */
988 if (!is_gimple_reg (lhs))
991 if (TREE_CODE (rhs) == MULT_EXPR)
993 tree arg0 = TREE_OPERAND (rhs, 0);
994 tree arg1 = TREE_OPERAND (rhs, 1);
996 TYPE_SIZE_UNIT (TREE_TYPE (TYPE_MAIN_VARIANT (TREE_TYPE (op0))));
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)))
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. */
1017 /* Register the parameter and return types of function FN. The type
1018 ESCAPES if the function is visible outside of the compilation
1021 check_function_parameter_and_return_types (tree fn, bool escapes)
1025 if (TYPE_ARG_TYPES (TREE_TYPE (fn)))
1027 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1028 arg && TREE_VALUE (arg) != void_type_node;
1029 arg = TREE_CHAIN (arg))
1031 tree type = get_canon_type (TREE_VALUE (arg), false, false);
1033 mark_interesting_type (type, EXPOSED_PARAMETER);
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. */
1043 for (arg = DECL_ARGUMENTS (fn); arg; arg = TREE_CHAIN (arg))
1045 tree type = get_canon_type (TREE_TYPE (arg), false, false);
1047 mark_interesting_type (type, EXPOSED_PARAMETER);
1052 tree type = get_canon_type (TREE_TYPE (TREE_TYPE (fn)), false, false);
1053 mark_interesting_type (type, EXPOSED_PARAMETER);
1057 /* Return true if the variable T is the right kind of static variable to
1058 perform compilation unit scope escape analysis. */
1061 has_proper_scope_for_analysis (tree t)
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);
1068 if (lookup_attribute ("used", DECL_ATTRIBUTES (t)))
1070 mark_interesting_type (type, FULL_ESCAPE);
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))
1079 /* Do not care about a local automatic that is not static. */
1080 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
1083 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
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. */
1090 if (TREE_READONLY (t)
1092 && is_gimple_min_invariant (DECL_INITIAL (t)))
1093 ; /* Read of a constant, do not change the function state. */
1096 /* The type escapes for all public and externs. */
1097 mark_interesting_type (type, FULL_ESCAPE);
1102 /* If T is a VAR_DECL for a static that we are interested in, add the
1103 uid to the bitmap. */
1106 check_operand (tree t)
1110 /* This is an assignment from a function, register the types as
1112 if (TREE_CODE (t) == FUNCTION_DECL)
1113 check_function_parameter_and_return_types (t, true);
1115 else if (TREE_CODE (t) == VAR_DECL)
1116 has_proper_scope_for_analysis (t);
1119 /* Examine tree T for references. */
1124 if ((TREE_CODE (t) == EXC_PTR_EXPR) || (TREE_CODE (t) == FILTER_EXPR))
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))
1131 if (TREE_CODE (t) == ARRAY_REF)
1132 check_operand (TREE_OPERAND (t, 1));
1133 t = TREE_OPERAND (t, 0);
1136 if (INDIRECT_REF_P (t))
1137 /* || TREE_CODE (t) == MEM_REF) */
1138 check_tree (TREE_OPERAND (t, 0));
1140 if (SSA_VAR_P (t) || (TREE_CODE (t) == FUNCTION_DECL))
1144 /* Create an address_of edge FROM_TYPE.TO_TYPE. */
1146 mark_interesting_addressof (tree to_type, tree from_type)
1151 splay_tree_node result;
1153 from_type = get_canon_type (from_type, false, false);
1154 to_type = get_canon_type (to_type, false, false);
1156 if (!from_type || !to_type)
1159 from_uid = TYPE_UID (from_type);
1160 to_uid = TYPE_UID (to_type);
1162 gcc_assert (ipa_type_escape_star_count_of_interesting_type (from_type) == 0);
1164 /* Process the Y into X map pointer. */
1165 result = splay_tree_lookup (uid_to_addressof_down_map,
1166 (splay_tree_key) from_uid);
1169 type_map = (bitmap) result->value;
1172 type_map = BITMAP_ALLOC (&ipa_obstack);
1173 splay_tree_insert (uid_to_addressof_down_map,
1175 (splay_tree_value)type_map);
1177 bitmap_set_bit (type_map, TYPE_UID (to_type));
1179 /* Process the X into Y reverse map pointer. */
1181 splay_tree_lookup (uid_to_addressof_up_map, (splay_tree_key) to_uid);
1184 type_map = (bitmap) result->value;
1187 type_map = BITMAP_ALLOC (&ipa_obstack);
1188 splay_tree_insert (uid_to_addressof_up_map,
1190 (splay_tree_value)type_map);
1192 bitmap_set_bit (type_map, TYPE_UID (from_type));
1195 /* Scan tree T to see if there are any addresses taken in within T. */
1198 look_for_address_of (tree t)
1200 if (TREE_CODE (t) == ADDR_EXPR)
1202 tree x = get_base_var (t);
1203 tree cref = TREE_OPERAND (t, 0);
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;
1210 if (TREE_CODE (cref) == COMPONENT_REF)
1212 fielddecl = TREE_OPERAND (cref, 1);
1213 mark_interesting_addressof (TREE_TYPE (fielddecl),
1214 DECL_FIELD_CONTEXT (fielddecl));
1216 else if (TREE_CODE (cref) == ARRAY_REF)
1217 get_canon_type (TREE_TYPE (cref), false, false);
1219 cref = TREE_OPERAND (cref, 0);
1222 if (TREE_CODE (x) == VAR_DECL)
1223 has_proper_scope_for_analysis (x);
1228 /* Scan tree T to see if there are any casts within it.
1229 LHS Is the LHS of the expression involving the cast. */
1232 look_for_casts (tree lhs ATTRIBUTE_UNUSED, tree t)
1234 unsigned int cast = 0;
1237 if (is_gimple_cast (t) || TREE_CODE (t) == VIEW_CONVERT_EXPR)
1239 tree castfromvar = TREE_OPERAND (t, 0);
1240 cast = cast | check_cast (TREE_TYPE (t), castfromvar);
1242 else if (TREE_CODE (t) == COMPONENT_REF
1243 || TREE_CODE (t) == INDIRECT_REF
1244 || TREE_CODE (t) == BIT_FIELD_REF)
1246 tree base = get_base_address (t);
1249 t = TREE_OPERAND (t, 0);
1250 if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
1252 /* This may be some part of a component ref.
1253 IE it may be a.b.VIEW_CONVERT_EXPR<weird_type>(c).d, AFAIK.
1254 castfromref will give you a.b.c, not a. */
1255 tree castfromref = TREE_OPERAND (t, 0);
1256 cast = cast | check_cast (TREE_TYPE (t), castfromref);
1258 else if (TREE_CODE (t) == COMPONENT_REF)
1259 get_canon_type (TREE_TYPE (TREE_OPERAND (t, 1)), false, false);
1268 /* Check to see if T is a read or address of operation on a static var
1269 we are interested in analyzing. */
1272 check_rhs_var (tree t)
1274 look_for_address_of (t);
1278 /* Check to see if T is an assignment to a static var we are
1279 interested in analyzing. */
1282 check_lhs_var (tree t)
1287 /* This is a scaled down version of get_asm_expr_operands from
1288 tree_ssa_operands.c. The version there runs much later and assumes
1289 that aliasing information is already available. Here we are just
1290 trying to find if the set of inputs and outputs contain references
1291 or address of operations to local. FN is the function being
1292 analyzed and STMT is the actual asm statement. */
1295 get_asm_expr_operands (tree stmt)
1297 int noutputs = list_length (ASM_OUTPUTS (stmt));
1298 const char **oconstraints
1299 = (const char **) alloca ((noutputs) * sizeof (const char *));
1302 const char *constraint;
1303 bool allows_mem, allows_reg, is_inout;
1305 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
1307 oconstraints[i] = constraint
1308 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1309 parse_output_constraint (&constraint, i, 0, 0,
1310 &allows_mem, &allows_reg, &is_inout);
1312 check_lhs_var (TREE_VALUE (link));
1315 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1318 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1319 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1320 oconstraints, &allows_mem, &allows_reg);
1322 check_rhs_var (TREE_VALUE (link));
1325 /* There is no code here to check for asm memory clobbers. The
1326 casual maintainer might think that such code would be necessary,
1327 but that appears to be wrong. In other parts of the compiler,
1328 the asm memory clobbers are assumed to only clobber variables
1329 that are addressable. All types with addressable instances are
1330 assumed to already escape. So, we are protected here. */
1333 /* Check the parameters of a function call to CALL_EXPR to mark the
1334 types that pass across the function boundary. Also check to see if
1335 this is either an indirect call, a call outside the compilation
1339 check_call (tree call_expr)
1342 tree callee_t = get_callee_fndecl (call_expr);
1343 struct cgraph_node* callee;
1344 enum availability avail = AVAIL_NOT_AVAILABLE;
1345 call_expr_arg_iterator iter;
1347 FOR_EACH_CALL_EXPR_ARG (operand, iter, call_expr)
1348 check_rhs_var (operand);
1353 tree last_arg_type = NULL;
1354 callee = cgraph_node(callee_t);
1355 avail = cgraph_function_body_availability (callee);
1357 /* Check that there are no implicit casts in the passing of
1359 if (TYPE_ARG_TYPES (TREE_TYPE (callee_t)))
1361 for (arg_type = TYPE_ARG_TYPES (TREE_TYPE (callee_t)),
1362 operand = first_call_expr_arg (call_expr, &iter);
1363 arg_type && TREE_VALUE (arg_type) != void_type_node;
1364 arg_type = TREE_CHAIN (arg_type),
1365 operand = next_call_expr_arg (&iter))
1369 last_arg_type = TREE_VALUE(arg_type);
1370 check_cast (last_arg_type, operand);
1373 /* The code reaches here for some unfortunate
1374 builtin functions that do not have a list of
1381 /* FIXME - According to Geoff Keating, we should never
1382 have to do this; the front ends should always process
1383 the arg list from the TYPE_ARG_LIST. */
1384 for (arg_type = DECL_ARGUMENTS (callee_t),
1385 operand = first_call_expr_arg (call_expr, &iter);
1387 arg_type = TREE_CHAIN (arg_type),
1388 operand = next_call_expr_arg (&iter))
1392 last_arg_type = TREE_TYPE(arg_type);
1393 check_cast (last_arg_type, operand);
1396 /* The code reaches here for some unfortunate
1397 builtin functions that do not have a list of
1403 /* In the case where we have a var_args function, we need to
1404 check the remaining parameters against the last argument. */
1405 arg_type = last_arg_type;
1407 operand != NULL_TREE;
1408 operand = next_call_expr_arg (&iter))
1411 check_cast (arg_type, operand);
1414 /* The code reaches here for some unfortunate
1415 builtin functions that do not have a list of
1416 argument types. Most of these functions have
1417 been marked as having their parameters not
1418 escape, but for the rest, the type is doomed. */
1419 tree type = get_canon_type (TREE_TYPE (operand), false, false);
1420 mark_interesting_type (type, FULL_ESCAPE);
1425 /* The callee is either unknown (indirect call) or there is just no
1426 scannable code for it (external call) . We look to see if there
1427 are any bits available for the callee (such as by declaration or
1428 because it is builtin) and process solely on the basis of those
1431 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
1433 /* If this is a direct call to an external function, mark all of
1434 the parameter and return types. */
1435 FOR_EACH_CALL_EXPR_ARG (operand, iter, call_expr)
1437 tree type = get_canon_type (TREE_TYPE (operand), false, false);
1438 mark_interesting_type (type, EXPOSED_PARAMETER);
1444 get_canon_type (TREE_TYPE (TREE_TYPE (callee_t)), false, false);
1445 mark_interesting_type (type, EXPOSED_PARAMETER);
1450 /* CODE is the operation on OP0 and OP1. OP0 is the operand that we
1451 *know* is a pointer type. OP1 may be a pointer type. */
1453 okay_pointer_operation (enum tree_code code, tree op0, tree op1)
1455 tree op0type = TYPE_MAIN_VARIANT (TREE_TYPE (op0));
1456 tree op1type = TYPE_MAIN_VARIANT (TREE_TYPE (op1));
1461 /* Multiplication does not change alignment. */
1467 if (POINTER_TYPE_P (op1type)
1468 && TREE_CODE (op0) == SSA_NAME
1469 && TREE_CODE (op1) == SSA_NAME
1470 && is_array_access_through_pointer_and_index (op0, op1))
1474 tree size_of_op0_points_to = TYPE_SIZE_UNIT (TREE_TYPE (op0type));
1476 if (CONSTANT_CLASS_P (op1)
1477 && size_of_op0_points_to
1478 && multiple_of_p (TREE_TYPE (size_of_op0_points_to),
1479 op1, size_of_op0_points_to))
1482 if (CONSTANT_CLASS_P (op0)
1483 && size_of_op0_points_to
1484 && multiple_of_p (TREE_TYPE (size_of_op0_points_to),
1485 op0, size_of_op0_points_to))
1496 /* TP is the part of the tree currently under the microscope.
1497 WALK_SUBTREES is part of the walk_tree api but is unused here.
1498 DATA is cgraph_node of the function being walked. */
1500 /* FIXME: When this is converted to run over SSA form, this code
1501 should be converted to use the operand scanner. */
1504 scan_for_refs (tree *tp, int *walk_subtrees, void *data)
1506 struct cgraph_node *fn = data;
1509 switch (TREE_CODE (t))
1512 if (DECL_INITIAL (t))
1513 walk_tree (&DECL_INITIAL (t), scan_for_refs, fn, visited_nodes);
1517 case GIMPLE_MODIFY_STMT:
1519 /* First look on the lhs and see what variable is stored to */
1520 tree lhs = GIMPLE_STMT_OPERAND (t, 0);
1521 tree rhs = GIMPLE_STMT_OPERAND (t, 1);
1523 check_lhs_var (lhs);
1524 check_cast (TREE_TYPE (lhs), rhs);
1526 /* For the purposes of figuring out what the cast affects */
1528 /* Next check the operands on the rhs to see if they are ok. */
1529 switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
1533 tree op0 = TREE_OPERAND (rhs, 0);
1534 tree type0 = get_canon_type (TREE_TYPE (op0), false, false);
1535 tree op1 = TREE_OPERAND (rhs, 1);
1536 tree type1 = get_canon_type (TREE_TYPE (op1), false, false);
1538 /* If this is pointer arithmetic of any bad sort, then
1539 we need to mark the types as bad. For binary
1540 operations, no binary operator we currently support
1541 is always "safe" in regard to what it would do to
1542 pointers for purposes of determining which types
1543 escape, except operations of the size of the type.
1544 It is possible that min and max under the right set
1545 of circumstances and if the moon is in the correct
1546 place could be safe, but it is hard to see how this
1547 is worth the effort. */
1549 if (type0 && POINTER_TYPE_P (type0)
1550 && !okay_pointer_operation (TREE_CODE (rhs), op0, op1))
1551 mark_interesting_type (type0, FULL_ESCAPE);
1552 if (type1 && POINTER_TYPE_P (type1)
1553 && !okay_pointer_operation (TREE_CODE (rhs), op1, op0))
1554 mark_interesting_type (type1, FULL_ESCAPE);
1556 look_for_casts (lhs, op0);
1557 look_for_casts (lhs, op1);
1558 check_rhs_var (op0);
1559 check_rhs_var (op1);
1564 tree op0 = TREE_OPERAND (rhs, 0);
1565 tree type0 = get_canon_type (TREE_TYPE (op0), false, false);
1566 /* For unary operations, if the operation is NEGATE or
1567 ABS on a pointer, this is also considered pointer
1568 arithmetic and thus, bad for business. */
1569 if (type0 && (TREE_CODE (op0) == NEGATE_EXPR
1570 || TREE_CODE (op0) == ABS_EXPR)
1571 && POINTER_TYPE_P (type0))
1573 mark_interesting_type (type0, FULL_ESCAPE);
1575 check_rhs_var (op0);
1576 look_for_casts (lhs, op0);
1577 look_for_casts (lhs, rhs);
1582 look_for_casts (lhs, rhs);
1583 check_rhs_var (rhs);
1585 case tcc_declaration:
1586 check_rhs_var (rhs);
1588 case tcc_expression:
1589 switch (TREE_CODE (rhs))
1592 look_for_casts (lhs, TREE_OPERAND (rhs, 0));
1593 check_rhs_var (rhs);
1600 switch (TREE_CODE (rhs))
1603 /* If this is a call to malloc, squirrel away the
1604 result so we do mark the resulting cast as being
1620 /* This case is here to find addresses on rhs of constructors in
1621 decl_initial of static variables. */
1632 get_asm_expr_operands (t);
1643 /* The init routine for analyzing global static variable usage. See
1644 comments at top for description. */
1648 bitmap_obstack_initialize (&ipa_obstack);
1649 global_types_exposed_parameter = BITMAP_ALLOC (&ipa_obstack);
1650 global_types_full_escape = BITMAP_ALLOC (&ipa_obstack);
1651 global_types_seen = BITMAP_ALLOC (&ipa_obstack);
1653 uid_to_canon_type = splay_tree_new (splay_tree_compare_ints, 0, 0);
1654 all_canon_types = splay_tree_new (compare_type_brand, 0, 0);
1655 type_to_canon_type = splay_tree_new (splay_tree_compare_pointers, 0, 0);
1656 uid_to_subtype_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1657 uid_to_addressof_down_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1658 uid_to_addressof_up_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1660 /* There are some shared nodes, in particular the initializers on
1661 static declarations. We do not need to scan them more than once
1662 since all we would be interested in are the addressof
1664 visited_nodes = pointer_set_create ();
1668 /* Check out the rhs of a static or global initialization VNODE to see
1669 if any of them contain addressof operations. Note that some of
1670 these variables may not even be referenced in the code in this
1671 compilation unit but their right hand sides may contain references
1672 to variables defined within this unit. */
1675 analyze_variable (struct varpool_node *vnode)
1677 tree global = vnode->decl;
1678 tree type = get_canon_type (TREE_TYPE (global), false, false);
1680 /* If this variable has exposure beyond the compilation unit, add
1681 its type to the global types. */
1683 if (vnode->externally_visible)
1684 mark_interesting_type (type, FULL_ESCAPE);
1686 gcc_assert (TREE_CODE (global) == VAR_DECL);
1688 if (DECL_INITIAL (global))
1689 walk_tree (&DECL_INITIAL (global), scan_for_refs, NULL, visited_nodes);
1692 /* This is the main routine for finding the reference patterns for
1693 global variables within a function FN. */
1696 analyze_function (struct cgraph_node *fn)
1698 tree decl = fn->decl;
1699 check_function_parameter_and_return_types (decl,
1700 fn->local.externally_visible);
1702 fprintf (dump_file, "\n local analysis of %s", cgraph_node_name (fn));
1705 struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
1706 basic_block this_block;
1708 FOR_EACH_BB_FN (this_block, this_cfun)
1710 block_stmt_iterator bsi;
1711 for (bsi = bsi_start (this_block); !bsi_end_p (bsi); bsi_next (&bsi))
1712 walk_tree (bsi_stmt_ptr (bsi), scan_for_refs,
1717 /* There may be const decls with interesting right hand sides. */
1718 if (DECL_STRUCT_FUNCTION (decl))
1721 for (step = DECL_STRUCT_FUNCTION (decl)->unexpanded_var_list;
1723 step = TREE_CHAIN (step))
1725 tree var = TREE_VALUE (step);
1726 if (TREE_CODE (var) == VAR_DECL
1727 && DECL_INITIAL (var)
1728 && !TREE_STATIC (var))
1729 walk_tree (&DECL_INITIAL (var), scan_for_refs,
1731 get_canon_type (TREE_TYPE (var), false, false);
1738 /* Convert a type_UID into a type. */
1740 type_for_uid (int uid)
1742 splay_tree_node result =
1743 splay_tree_lookup (uid_to_canon_type, (splay_tree_key) uid);
1746 return (tree) result->value;
1750 /* Return the a bitmap with the subtypes of the type for UID. If it
1751 does not exist, return either NULL or a new bitmap depending on the
1755 subtype_map_for_uid (int uid, bool create)
1757 splay_tree_node result = splay_tree_lookup (uid_to_subtype_map,
1758 (splay_tree_key) uid);
1761 return (bitmap) result->value;
1764 bitmap subtype_map = BITMAP_ALLOC (&ipa_obstack);
1765 splay_tree_insert (uid_to_subtype_map,
1767 (splay_tree_value)subtype_map);
1773 /* Mark all of the supertypes and field types of TYPE as being seen.
1774 Also accumulate the subtypes for each type so that
1775 close_types_full_escape can mark a subtype as escaping if the
1776 supertype escapes. */
1779 close_type_seen (tree type)
1783 tree binfo, base_binfo;
1785 /* See thru all pointer tos and array ofs. */
1786 type = get_canon_type (type, true, true);
1790 uid = TYPE_UID (type);
1792 if (bitmap_bit_p (been_there_done_that, uid))
1794 bitmap_set_bit (been_there_done_that, uid);
1796 /* If we are doing a language with a type hierarchy, mark all of
1797 the superclasses. */
1798 if (TYPE_BINFO (type))
1799 for (binfo = TYPE_BINFO (type), i = 0;
1800 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1802 tree binfo_type = BINFO_TYPE (base_binfo);
1803 bitmap subtype_map = subtype_map_for_uid
1804 (TYPE_UID (TYPE_MAIN_VARIANT (binfo_type)), true);
1805 bitmap_set_bit (subtype_map, uid);
1806 close_type_seen (get_canon_type (binfo_type, true, true));
1809 /* If the field is a struct or union type, mark all of the
1811 for (field = TYPE_FIELDS (type);
1813 field = TREE_CHAIN (field))
1816 if (TREE_CODE (field) != FIELD_DECL)
1819 field_type = TREE_TYPE (field);
1820 if (ipa_type_escape_star_count_of_interesting_or_array_type (field_type) >= 0)
1821 close_type_seen (get_canon_type (field_type, true, true));
1825 /* Take a TYPE that has been passed by value to an external function
1826 and mark all of the fields that have pointer types as escaping. For
1827 any of the non pointer types that are structures or unions,
1828 recurse. TYPE is never a pointer type. */
1831 close_type_exposed_parameter (tree type)
1836 type = get_canon_type (type, false, false);
1839 uid = TYPE_UID (type);
1840 gcc_assert (!POINTER_TYPE_P (type));
1842 if (bitmap_bit_p (been_there_done_that, uid))
1844 bitmap_set_bit (been_there_done_that, uid);
1846 /* If the field is a struct or union type, mark all of the
1848 for (field = TYPE_FIELDS (type);
1850 field = TREE_CHAIN (field))
1854 if (TREE_CODE (field) != FIELD_DECL)
1857 field_type = get_canon_type (TREE_TYPE (field), false, false);
1858 mark_interesting_type (field_type, EXPOSED_PARAMETER);
1860 /* Only recurse for non pointer types of structures and unions. */
1861 if (ipa_type_escape_star_count_of_interesting_type (field_type) == 0)
1862 close_type_exposed_parameter (field_type);
1866 /* The next function handles the case where a type fully escapes.
1867 This means that not only does the type itself escape,
1869 a) the type of every field recursively escapes
1870 b) the type of every subtype escapes as well as the super as well
1871 as all of the pointer to types for each field.
1873 Note that pointer to types are not marked as escaping. If the
1874 pointed to type escapes, the pointer to type also escapes.
1876 Take a TYPE that has had the address taken for an instance of it
1877 and mark all of the types for its fields as having their addresses
1881 close_type_full_escape (tree type)
1886 tree binfo, base_binfo;
1889 splay_tree_node address_result;
1891 /* Strip off any pointer or array types. */
1892 type = get_canon_type (type, true, true);
1895 uid = TYPE_UID (type);
1897 if (bitmap_bit_p (been_there_done_that, uid))
1899 bitmap_set_bit (been_there_done_that, uid);
1901 subtype_map = subtype_map_for_uid (uid, false);
1903 /* If we are doing a language with a type hierarchy, mark all of
1904 the superclasses. */
1905 if (TYPE_BINFO (type))
1906 for (binfo = TYPE_BINFO (type), i = 0;
1907 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1909 tree binfotype = BINFO_TYPE (base_binfo);
1910 binfotype = mark_type (binfotype, FULL_ESCAPE);
1911 close_type_full_escape (binfotype);
1914 /* Mark as escaped any types that have been down casted to
1917 EXECUTE_IF_SET_IN_BITMAP (subtype_map, 0, i, bi)
1919 tree subtype = type_for_uid (i);
1920 subtype = mark_type (subtype, FULL_ESCAPE);
1921 close_type_full_escape (subtype);
1924 /* If the field is a struct or union type, mark all of the
1926 for (field = TYPE_FIELDS (type);
1928 field = TREE_CHAIN (field))
1931 if (TREE_CODE (field) != FIELD_DECL)
1934 field_type = TREE_TYPE (field);
1935 if (ipa_type_escape_star_count_of_interesting_or_array_type (field_type) >= 0)
1937 field_type = mark_type (field_type, FULL_ESCAPE);
1938 close_type_full_escape (field_type);
1942 /* For all of the types A that contain this type B and were part of
1943 an expression like "&...A.B...", mark the A's as escaping. */
1944 address_result = splay_tree_lookup (uid_to_addressof_up_map,
1945 (splay_tree_key) uid);
1948 bitmap containing_classes = (bitmap) address_result->value;
1949 EXECUTE_IF_SET_IN_BITMAP (containing_classes, 0, i, bi)
1951 close_type_full_escape (type_for_uid (i));
1956 /* Transitively close the addressof bitmap for the type with UID.
1957 This means that if we had a.b and b.c, a would have both b and c in
1961 close_addressof_down (int uid)
1964 splay_tree_node result =
1965 splay_tree_lookup (uid_to_addressof_down_map, (splay_tree_key) uid);
1971 map = (bitmap) result->value;
1975 if (bitmap_bit_p (been_there_done_that, uid))
1977 bitmap_set_bit (been_there_done_that, uid);
1979 /* If the type escapes, get rid of the addressof map, it will not be
1981 if (bitmap_bit_p (global_types_full_escape, uid))
1984 splay_tree_remove (uid_to_addressof_down_map, (splay_tree_key) uid);
1988 /* The new_map will have all of the bits for the enclosed fields and
1989 will have the unique id version of the old map. */
1990 new_map = BITMAP_ALLOC (&ipa_obstack);
1992 EXECUTE_IF_SET_IN_BITMAP (map, 0, i, bi)
1994 bitmap submap = close_addressof_down (i);
1995 bitmap_set_bit (new_map, i);
1997 bitmap_ior_into (new_map, submap);
1999 result->value = (splay_tree_value) new_map;
2006 /* The main entry point for type escape analysis. */
2009 type_escape_execute (void)
2011 struct cgraph_node *node;
2012 struct varpool_node *vnode;
2015 splay_tree_node result;
2019 /* Process all of the variables first. */
2020 FOR_EACH_STATIC_VARIABLE (vnode)
2021 analyze_variable (vnode);
2023 /* Process all of the functions. next
2025 We do not want to process any of the clones so we check that this
2026 is a master clone. However, we do need to process any
2027 AVAIL_OVERWRITABLE functions (these are never clones) because
2028 they may cause a type variable to escape.
2030 for (node = cgraph_nodes; node; node = node->next)
2032 && (cgraph_is_master_clone (node)
2033 || (cgraph_function_body_availability (node) == AVAIL_OVERWRITABLE)))
2034 analyze_function (node);
2037 pointer_set_destroy (visited_nodes);
2038 visited_nodes = NULL;
2040 /* Do all of the closures to discover which types escape the
2041 compilation unit. */
2043 been_there_done_that = BITMAP_ALLOC (&ipa_obstack);
2044 bitmap_tmp = BITMAP_ALLOC (&ipa_obstack);
2046 /* Examine the types that we have directly seen in scanning the code
2047 and add to that any contained types or superclasses. */
2049 bitmap_copy (bitmap_tmp, global_types_seen);
2050 EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
2052 tree type = type_for_uid (i);
2053 /* Only look at records and unions and pointer tos. */
2054 if (ipa_type_escape_star_count_of_interesting_or_array_type (type) >= 0)
2055 close_type_seen (type);
2057 bitmap_clear (been_there_done_that);
2059 /* Examine all of the types passed by value and mark any enclosed
2060 pointer types as escaping. */
2061 bitmap_copy (bitmap_tmp, global_types_exposed_parameter);
2062 EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
2064 close_type_exposed_parameter (type_for_uid (i));
2066 bitmap_clear (been_there_done_that);
2068 /* Close the types for escape. If something escapes, then any
2069 enclosed types escape as well as any subtypes. */
2070 bitmap_copy (bitmap_tmp, global_types_full_escape);
2071 EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
2073 close_type_full_escape (type_for_uid (i));
2075 bitmap_clear (been_there_done_that);
2077 /* Before this pass, the uid_to_addressof_down_map for type X
2078 contained an entry for Y if there had been an operation of the
2079 form &X.Y. This step adds all of the fields contained within Y
2080 (recursively) to X's map. */
2082 result = splay_tree_min (uid_to_addressof_down_map);
2085 int uid = result->key;
2086 /* Close the addressof map, i.e. copy all of the transitive
2087 substructures up to this level. */
2088 close_addressof_down (uid);
2089 result = splay_tree_successor (uid_to_addressof_down_map, uid);
2092 /* Do not need the array types and pointer types in the persistent
2094 result = splay_tree_min (all_canon_types);
2097 tree type = (tree) result->value;
2098 tree key = (tree) result->key;
2099 if (POINTER_TYPE_P (type)
2100 || TREE_CODE (type) == ARRAY_TYPE)
2102 splay_tree_remove (all_canon_types, (splay_tree_key) result->key);
2103 splay_tree_remove (type_to_canon_type, (splay_tree_key) type);
2104 splay_tree_remove (uid_to_canon_type, (splay_tree_key) TYPE_UID (type));
2105 bitmap_clear_bit (global_types_seen, TYPE_UID (type));
2107 result = splay_tree_successor (all_canon_types, (splay_tree_key) key);
2112 EXECUTE_IF_SET_IN_BITMAP (global_types_seen, 0, i, bi)
2114 /* The pointer types are in the global_types_full_escape
2115 bitmap but not in the backwards map. They also contain
2116 no useful information since they are not marked. */
2117 tree type = type_for_uid (i);
2118 fprintf(dump_file, "type %d ", i);
2119 print_generic_expr (dump_file, type, 0);
2120 if (bitmap_bit_p (global_types_full_escape, i))
2121 fprintf(dump_file, " escaped\n");
2123 fprintf(dump_file, " contained\n");
2127 /* Get rid of uid_to_addressof_up_map and its bitmaps. */
2128 result = splay_tree_min (uid_to_addressof_up_map);
2131 int uid = (int)result->key;
2132 bitmap bm = (bitmap)result->value;
2135 splay_tree_remove (uid_to_addressof_up_map, (splay_tree_key) uid);
2136 result = splay_tree_successor (uid_to_addressof_up_map, uid);
2139 /* Get rid of the subtype map. */
2140 result = splay_tree_min (uid_to_subtype_map);
2143 bitmap b = (bitmap)result->value;
2145 splay_tree_remove (uid_to_subtype_map, result->key);
2146 result = splay_tree_min (uid_to_subtype_map);
2148 splay_tree_delete (uid_to_subtype_map);
2149 uid_to_subtype_map = NULL;
2151 BITMAP_FREE (global_types_exposed_parameter);
2152 BITMAP_FREE (been_there_done_that);
2153 BITMAP_FREE (bitmap_tmp);
2158 gate_type_escape_vars (void)
2160 return (flag_unit_at_a_time != 0 && flag_ipa_type_escape
2161 /* Don't bother doing anything if the program has errors. */
2162 && !(errorcount || sorrycount));
2165 struct tree_opt_pass pass_ipa_type_escape =
2167 "type-escape-var", /* name */
2168 gate_type_escape_vars, /* gate */
2169 type_escape_execute, /* execute */
2172 0, /* static_pass_number */
2173 TV_IPA_TYPE_ESCAPE, /* tv_id */
2174 0, /* properties_required */
2175 0, /* properties_provided */
2176 0, /* properties_destroyed */
2177 0, /* todo_flags_start */
2178 0, /* todo_flags_finish */