2010-11-11 Jakub Jelinek <jakub@redhat.com>
[official-gcc.git] / gcc / ipa-type-escape.c
blob132a575390a77384008d12d1d9c976ef6f9feb14
1 /* Escape analysis for types.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010
3 Free Software Foundation, Inc.
4 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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 "splay-tree.h"
47 #include "ggc.h"
48 #include "ipa-utils.h"
49 #include "ipa-type-escape.h"
50 #include "gimple.h"
51 #include "cgraph.h"
52 #include "output.h"
53 #include "flags.h"
54 #include "timevar.h"
55 #include "diagnostic.h"
56 #include "tree-pretty-print.h"
57 #include "langhooks.h"
59 /* Some of the aliasing is called very early, before this phase is
60 called. To assure that this is not a problem, we keep track of if
61 this phase has been run. */
62 static bool initialized = false;
64 /* Scratch bitmap for avoiding work. */
65 static bitmap been_there_done_that;
66 static bitmap bitmap_tmp;
68 /* There are two levels of escape that types can undergo.
70 EXPOSED_PARAMETER - some instance of the variable is
71 passed by value into an externally visible function or some
72 instance of the variable is passed out of an externally visible
73 function as a return value. In this case any of the fields of the
74 variable that are pointer types end up having their types marked as
75 FULL_ESCAPE.
77 FULL_ESCAPE - when bad things happen to good types. One of the
78 following things happens to the type: (a) either an instance of the
79 variable has its address passed to an externally visible function,
80 (b) the address is taken and some bad cast happens to the address
81 or (c) explicit arithmetic is done to the address.
84 enum escape_t
86 EXPOSED_PARAMETER,
87 FULL_ESCAPE
90 /* The following two bit vectors global_types_* correspond to
91 previous cases above. During the analysis phase, a bit is set in
92 one of these vectors if an operation of the offending class is
93 discovered to happen on the associated type. */
95 static bitmap global_types_exposed_parameter;
96 static bitmap global_types_full_escape;
98 /* All of the types seen in this compilation unit. */
99 static bitmap global_types_seen;
100 /* Reverse map to take a canon uid and map it to a canon type. Uid's
101 are never manipulated unless they are associated with a canon
102 type. */
103 static splay_tree uid_to_canon_type;
105 /* Internal structure of type mapping code. This maps a canon type
106 name to its canon type. */
107 static splay_tree all_canon_types;
109 /* Map from type clones to the single canon type. */
110 static splay_tree type_to_canon_type;
112 /* A splay tree of bitmaps. An element X in the splay tree has a bit
113 set in its bitmap at TYPE_UID (TYPE_MAIN_VARIANT (Y)) if there was
114 an operation in the program of the form "&X.Y". */
115 static splay_tree uid_to_addressof_down_map;
117 /* A splay tree of bitmaps. An element Y in the splay tree has a bit
118 set in its bitmap at TYPE_UID (TYPE_MAIN_VARIANT (X)) if there was
119 an operation in the program of the form "&X.Y". */
120 static splay_tree uid_to_addressof_up_map;
122 /* Tree to hold the subtype maps used to mark subtypes of escaped
123 types. */
124 static splay_tree uid_to_subtype_map;
126 /* Records tree nodes seen in cgraph_create_edges. Simply using
127 walk_tree_without_duplicates doesn't guarantee each node is visited
128 once because it gets a new htab upon each recursive call from
129 scan_for_refs. */
130 static struct pointer_set_t *visited_nodes;
132 /* Visited stmts by walk_use_def_chains function because it's called
133 recursively. */
134 static struct pointer_set_t *visited_stmts;
136 static bitmap_obstack ipa_obstack;
138 /* Static functions from this file that are used
139 before being defined. */
140 static unsigned int look_for_casts (tree);
141 static bool is_cast_from_non_pointer (tree, gimple, void *);
143 /* Get the name of TYPE or return the string "<UNNAMED>". */
144 static const char*
145 get_name_of_type (tree type)
147 tree name = TYPE_NAME (type);
149 if (!name)
150 /* Unnamed type, do what you like here. */
151 return "<UNNAMED>";
153 /* It will be a TYPE_DECL in the case of a typedef, otherwise, an
154 identifier_node */
155 if (TREE_CODE (name) == TYPE_DECL)
157 /* Each DECL has a DECL_NAME field which contains an
158 IDENTIFIER_NODE. (Some decls, most often labels, may have
159 zero as the DECL_NAME). */
160 if (DECL_NAME (name))
161 return IDENTIFIER_POINTER (DECL_NAME (name));
162 else
163 /* Unnamed type, do what you like here. */
164 return "<UNNAMED>";
166 else if (TREE_CODE (name) == IDENTIFIER_NODE)
167 return IDENTIFIER_POINTER (name);
168 else
169 return "<UNNAMED>";
172 struct type_brand_s
174 const char* name;
175 int seq;
178 /* Splay tree comparison function on type_brand_s structures. */
180 static int
181 compare_type_brand (splay_tree_key sk1, splay_tree_key sk2)
183 struct type_brand_s * k1 = (struct type_brand_s *) sk1;
184 struct type_brand_s * k2 = (struct type_brand_s *) sk2;
186 int value = strcmp(k1->name, k2->name);
187 if (value == 0)
188 return k2->seq - k1->seq;
189 else
190 return value;
193 /* All of the "unique_type" code is a hack to get around the sleazy
194 implementation used to compile more than file. Currently gcc does
195 not get rid of multiple instances of the same type that have been
196 collected from different compilation units. */
197 /* This is a trivial algorithm for removing duplicate types. This
198 would not work for any language that used structural equivalence as
199 the basis of its type system. */
200 /* Return TYPE if no type compatible with TYPE has been seen so far,
201 otherwise return a type compatible with TYPE that has already been
202 processed. */
204 static tree
205 discover_unique_type (tree type)
207 struct type_brand_s * brand = XNEW (struct type_brand_s);
208 int i = 0;
209 splay_tree_node result;
211 brand->name = get_name_of_type (type);
213 while (1)
215 brand->seq = i++;
216 result = splay_tree_lookup (all_canon_types, (splay_tree_key) brand);
218 if (result)
220 /* Create an alias since this is just the same as
221 other_type. */
222 tree other_type = (tree) result->value;
223 if (types_compatible_p (type, other_type))
225 free (brand);
226 /* Insert this new type as an alias for other_type. */
227 splay_tree_insert (type_to_canon_type,
228 (splay_tree_key) type,
229 (splay_tree_value) other_type);
230 return other_type;
232 /* Not compatible, look for next instance with same name. */
234 else
236 /* No more instances, create new one since this is the first
237 time we saw this type. */
238 brand->seq = i++;
239 /* Insert the new brand. */
240 splay_tree_insert (all_canon_types,
241 (splay_tree_key) brand,
242 (splay_tree_value) type);
244 /* Insert this new type as an alias for itself. */
245 splay_tree_insert (type_to_canon_type,
246 (splay_tree_key) type,
247 (splay_tree_value) type);
249 /* Insert the uid for reverse lookup; */
250 splay_tree_insert (uid_to_canon_type,
251 (splay_tree_key) TYPE_UID (type),
252 (splay_tree_value) type);
254 bitmap_set_bit (global_types_seen, TYPE_UID (type));
255 return type;
260 /* Return true if TYPE is one of the type classes that we are willing
261 to analyze. This skips the goofy types like arrays of pointers to
262 methods. */
263 static bool
264 type_to_consider (tree type)
266 /* Strip the *'s off. */
267 type = TYPE_MAIN_VARIANT (type);
268 while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
269 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
271 switch (TREE_CODE (type))
273 case BOOLEAN_TYPE:
274 case COMPLEX_TYPE:
275 case ENUMERAL_TYPE:
276 case INTEGER_TYPE:
277 case QUAL_UNION_TYPE:
278 case REAL_TYPE:
279 case FIXED_POINT_TYPE:
280 case RECORD_TYPE:
281 case UNION_TYPE:
282 case VECTOR_TYPE:
283 case VOID_TYPE:
284 return true;
286 default:
287 return false;
291 /* Get the canon type of TYPE. If SEE_THRU_PTRS is true, remove all
292 the POINTER_TOs and if SEE_THRU_ARRAYS is true, remove all of the
293 ARRAY_OFs and POINTER_TOs. */
295 static tree
296 get_canon_type (tree type, bool see_thru_ptrs, bool see_thru_arrays)
298 splay_tree_node result;
299 /* Strip the *'s off. */
300 if (!type || !type_to_consider (type))
301 return NULL;
303 type = TYPE_MAIN_VARIANT (type);
304 if (see_thru_arrays)
305 while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
306 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
308 else if (see_thru_ptrs)
309 while (POINTER_TYPE_P (type))
310 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
312 result = splay_tree_lookup (type_to_canon_type, (splay_tree_key) type);
314 if (result == NULL)
315 return discover_unique_type (type);
316 else return (tree) result->value;
319 /* Same as GET_CANON_TYPE, except return the TYPE_ID rather than the
320 TYPE. */
322 static int
323 get_canon_type_uid (tree type, bool see_thru_ptrs, bool see_thru_arrays)
325 type = get_canon_type (type, see_thru_ptrs, see_thru_arrays);
326 if (type)
327 return TYPE_UID(type);
328 else return 0;
331 /* Return 0 if TYPE is a record or union type. Return a positive
332 number if TYPE is a pointer to a record or union. The number is
333 the number of pointer types stripped to get to the record or union
334 type. Return -1 if TYPE is none of the above. */
337 ipa_type_escape_star_count_of_interesting_type (tree type)
339 int count = 0;
340 /* Strip the *'s off. */
341 if (!type)
342 return -1;
343 type = TYPE_MAIN_VARIANT (type);
344 while (POINTER_TYPE_P (type))
346 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
347 count++;
350 /* We are interested in records, and unions only. */
351 if (TREE_CODE (type) == RECORD_TYPE
352 || TREE_CODE (type) == QUAL_UNION_TYPE
353 || TREE_CODE (type) == UNION_TYPE)
354 return count;
355 else
356 return -1;
360 /* Return 0 if TYPE is a record or union type. Return a positive
361 number if TYPE is a pointer to a record or union. The number is
362 the number of pointer types stripped to get to the record or union
363 type. Return -1 if TYPE is none of the above. */
366 ipa_type_escape_star_count_of_interesting_or_array_type (tree type)
368 int count = 0;
369 /* Strip the *'s off. */
370 if (!type)
371 return -1;
372 type = TYPE_MAIN_VARIANT (type);
373 while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
375 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
376 count++;
379 /* We are interested in records, and unions only. */
380 if (TREE_CODE (type) == RECORD_TYPE
381 || TREE_CODE (type) == QUAL_UNION_TYPE
382 || TREE_CODE (type) == UNION_TYPE)
383 return count;
384 else
385 return -1;
389 /* Return true if the record, or union TYPE passed in escapes this
390 compilation unit. Note that all of the pointer-to's are removed
391 before testing since these may not be correct. */
393 bool
394 ipa_type_escape_type_contained_p (tree type)
396 if (!initialized)
397 return false;
398 return !bitmap_bit_p (global_types_full_escape,
399 get_canon_type_uid (type, true, false));
402 /* Return true if a modification to a field of type FIELD_TYPE cannot
403 clobber a record of RECORD_TYPE. */
405 bool
406 ipa_type_escape_field_does_not_clobber_p (tree record_type, tree field_type)
408 splay_tree_node result;
409 int uid;
411 if (!initialized)
412 return false;
414 /* Strip off all of the pointer tos on the record type. Strip the
415 same number of pointer tos from the field type. If the field
416 type has fewer, it could not have been aliased. */
417 record_type = TYPE_MAIN_VARIANT (record_type);
418 field_type = TYPE_MAIN_VARIANT (field_type);
419 while (POINTER_TYPE_P (record_type))
421 record_type = TYPE_MAIN_VARIANT (TREE_TYPE (record_type));
422 if (POINTER_TYPE_P (field_type))
423 field_type = TYPE_MAIN_VARIANT (TREE_TYPE (field_type));
424 else
425 /* However, if field_type is a union, this quick test is not
426 correct since one of the variants of the union may be a
427 pointer to type and we cannot see across that here. So we
428 just strip the remaining pointer tos off the record type
429 and fall thru to the more precise code. */
430 if (TREE_CODE (field_type) == QUAL_UNION_TYPE
431 || TREE_CODE (field_type) == UNION_TYPE)
433 while (POINTER_TYPE_P (record_type))
434 record_type = TYPE_MAIN_VARIANT (TREE_TYPE (record_type));
435 break;
437 else
438 return true;
441 record_type = get_canon_type (record_type, true, true);
442 /* The record type must be contained. The field type may
443 escape. */
444 if (!ipa_type_escape_type_contained_p (record_type))
445 return false;
447 uid = TYPE_UID (record_type);
448 result = splay_tree_lookup (uid_to_addressof_down_map, (splay_tree_key) uid);
450 if (result)
452 bitmap field_type_map = (bitmap) result->value;
453 uid = get_canon_type_uid (field_type, true, true);
454 /* If the bit is there, the address was taken. If not, it
455 wasn't. */
456 return !bitmap_bit_p (field_type_map, uid);
458 else
459 /* No bitmap means no addresses were taken. */
460 return true;
464 /* Add TYPE to the suspect type set. Return true if the bit needed to
465 be marked. */
467 static tree
468 mark_type (tree type, enum escape_t escape_status)
470 bitmap map = NULL;
471 int uid;
473 type = get_canon_type (type, true, true);
474 if (!type)
475 return NULL;
477 switch (escape_status)
479 case EXPOSED_PARAMETER:
480 map = global_types_exposed_parameter;
481 break;
482 case FULL_ESCAPE:
483 map = global_types_full_escape;
484 break;
487 uid = TYPE_UID (type);
488 if (!bitmap_set_bit (map, uid))
489 return type;
490 else 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);
495 return type;
498 /* Add interesting TYPE to the suspect type set. If the set is
499 EXPOSED_PARAMETER and the TYPE is a pointer type, the set is
500 changed to FULL_ESCAPE. */
502 static void
503 mark_interesting_type (tree type, enum escape_t escape_status)
505 if (!type) return;
506 if (ipa_type_escape_star_count_of_interesting_type (type) >= 0)
508 if ((escape_status == EXPOSED_PARAMETER)
509 && POINTER_TYPE_P (type))
510 /* EXPOSED_PARAMETERs are only structs or unions are passed by
511 value. Anything passed by reference to an external
512 function fully exposes the type. */
513 mark_type (type, FULL_ESCAPE);
514 else
515 mark_type (type, escape_status);
519 /* Return true if PARENT is supertype of CHILD. Both types must be
520 known to be structures or unions. */
522 static bool
523 parent_type_p (tree parent, tree child)
525 int i;
526 tree binfo, base_binfo;
527 if (TYPE_BINFO (parent))
528 for (binfo = TYPE_BINFO (parent), i = 0;
529 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
531 tree binfotype = BINFO_TYPE (base_binfo);
532 if (binfotype == child)
533 return true;
534 else if (parent_type_p (binfotype, child))
535 return true;
537 if (TREE_CODE (parent) == UNION_TYPE
538 || TREE_CODE (parent) == QUAL_UNION_TYPE)
540 tree field;
541 /* Search all of the variants in the union to see if one of them
542 is the child. */
543 for (field = TYPE_FIELDS (parent);
544 field;
545 field = TREE_CHAIN (field))
547 tree field_type;
548 if (TREE_CODE (field) != FIELD_DECL)
549 continue;
551 field_type = TREE_TYPE (field);
552 if (field_type == child)
553 return true;
556 /* If we did not find it, recursively ask the variants if one of
557 their children is the child type. */
558 for (field = TYPE_FIELDS (parent);
559 field;
560 field = TREE_CHAIN (field))
562 tree field_type;
563 if (TREE_CODE (field) != FIELD_DECL)
564 continue;
566 field_type = TREE_TYPE (field);
567 if (TREE_CODE (field_type) == RECORD_TYPE
568 || TREE_CODE (field_type) == QUAL_UNION_TYPE
569 || TREE_CODE (field_type) == UNION_TYPE)
570 if (parent_type_p (field_type, child))
571 return true;
575 if (TREE_CODE (parent) == RECORD_TYPE)
577 tree field;
578 for (field = TYPE_FIELDS (parent);
579 field;
580 field = TREE_CHAIN (field))
582 tree field_type;
583 if (TREE_CODE (field) != FIELD_DECL)
584 continue;
586 field_type = TREE_TYPE (field);
587 if (field_type == child)
588 return true;
589 /* You can only cast to the first field so if it does not
590 match, quit. */
591 if (TREE_CODE (field_type) == RECORD_TYPE
592 || TREE_CODE (field_type) == QUAL_UNION_TYPE
593 || TREE_CODE (field_type) == UNION_TYPE)
595 if (parent_type_p (field_type, child))
596 return true;
597 else
598 break;
602 return false;
605 /* Return the number of pointer tos for TYPE and return TYPE with all
606 of these stripped off. */
608 static int
609 count_stars (tree* type_ptr)
611 tree type = *type_ptr;
612 int i = 0;
613 type = TYPE_MAIN_VARIANT (type);
614 while (POINTER_TYPE_P (type))
616 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
617 i++;
620 *type_ptr = type;
621 return i;
624 enum cast_type {
625 CT_UP = 0x1,
626 CT_DOWN = 0x2,
627 CT_SIDEWAYS = 0x4,
628 CT_USELESS = 0x8,
629 CT_FROM_P_BAD = 0x10,
630 CT_FROM_NON_P = 0x20,
631 CT_TO_NON_INTER = 0x40,
632 CT_FROM_MALLOC = 0x80,
633 CT_NO_CAST = 0x100
636 /* Check the cast FROM_TYPE to TO_TYPE. This function requires that
637 the two types have already passed the
638 ipa_type_escape_star_count_of_interesting_type test. */
640 static enum cast_type
641 check_cast_type (tree to_type, tree from_type)
643 int to_stars = count_stars (&to_type);
644 int from_stars = count_stars (&from_type);
645 if (to_stars != from_stars)
646 return CT_SIDEWAYS;
648 if (to_type == from_type)
649 return CT_USELESS;
651 if (parent_type_p (to_type, from_type)) return CT_UP;
652 if (parent_type_p (from_type, to_type)) return CT_DOWN;
653 return CT_SIDEWAYS;
656 /* This function returns nonzero if VAR is result of call
657 to malloc function. */
659 static bool
660 is_malloc_result (tree var)
662 gimple def_stmt;
664 if (!var)
665 return false;
667 if (SSA_NAME_IS_DEFAULT_DEF (var))
668 return false;
670 def_stmt = SSA_NAME_DEF_STMT (var);
672 if (!is_gimple_call (def_stmt))
673 return false;
675 if (var != gimple_call_lhs (def_stmt))
676 return false;
678 return ((gimple_call_flags (def_stmt) & ECF_MALLOC) != 0);
682 /* Check a cast FROM this variable, TO_TYPE. Mark the escaping types
683 if appropriate. Returns cast_type as detected. */
685 static enum cast_type
686 check_cast (tree to_type, tree from)
688 tree from_type = get_canon_type (TREE_TYPE (from), false, false);
689 bool to_interesting_type, from_interesting_type;
690 enum cast_type cast = CT_NO_CAST;
692 to_type = get_canon_type (to_type, false, false);
693 if (!from_type || !to_type || from_type == to_type)
694 return cast;
696 to_interesting_type =
697 ipa_type_escape_star_count_of_interesting_type (to_type) >= 0;
698 from_interesting_type =
699 ipa_type_escape_star_count_of_interesting_type (from_type) >= 0;
701 if (to_interesting_type)
702 if (from_interesting_type)
704 /* Both types are interesting. This can be one of four types
705 of cast: useless, up, down, or sideways. We do not care
706 about up or useless. Sideways casts are always bad and
707 both sides get marked as escaping. Downcasts are not
708 interesting here because if type is marked as escaping, all
709 of its subtypes escape. */
710 cast = check_cast_type (to_type, from_type);
711 switch (cast)
713 case CT_UP:
714 case CT_USELESS:
715 case CT_DOWN:
716 break;
718 case CT_SIDEWAYS:
719 mark_type (to_type, FULL_ESCAPE);
720 mark_type (from_type, FULL_ESCAPE);
721 break;
723 default:
724 break;
727 else
729 /* This code excludes two cases from marking as escaped:
731 1. if this is a cast of index of array of structures/unions
732 that happens before accessing array element, we should not
733 mark it as escaped.
734 2. if this is a cast from the local that is a result from a
735 call to malloc, do not mark the cast as bad.
739 if (POINTER_TYPE_P (to_type) && !POINTER_TYPE_P (from_type))
740 cast = CT_FROM_NON_P;
741 else if (TREE_CODE (from) == SSA_NAME
742 && is_malloc_result (from))
743 cast = CT_FROM_MALLOC;
744 else
746 cast = CT_FROM_P_BAD;
747 mark_type (to_type, FULL_ESCAPE);
750 else if (from_interesting_type)
752 mark_type (from_type, FULL_ESCAPE);
753 cast = CT_TO_NON_INTER;
756 return cast;
760 /* Scan assignment statement S to see if there are any casts within it. */
762 static unsigned int
763 look_for_casts_stmt (gimple s)
765 unsigned int cast = 0;
767 gcc_assert (is_gimple_assign (s));
769 if (gimple_assign_cast_p (s))
771 tree castfromvar = gimple_assign_rhs1 (s);
772 cast |= check_cast (TREE_TYPE (gimple_assign_lhs (s)), castfromvar);
774 else
776 size_t i;
777 for (i = 0; i < gimple_num_ops (s); i++)
778 cast |= look_for_casts (gimple_op (s, i));
781 if (!cast)
782 cast = CT_NO_CAST;
784 return cast;
788 typedef struct cast
790 int type;
791 gimple stmt;
792 } cast_t;
794 /* This function is a callback for walk_use_def_chains function called
795 from is_array_access_through_pointer_and_index. */
797 static bool
798 is_cast_from_non_pointer (tree var, gimple def_stmt, void *data)
800 if (!def_stmt || !var)
801 return false;
803 if (gimple_code (def_stmt) == GIMPLE_PHI)
804 return false;
806 if (SSA_NAME_IS_DEFAULT_DEF (var))
807 return false;
809 if (is_gimple_assign (def_stmt))
811 use_operand_p use_p;
812 ssa_op_iter iter;
813 unsigned int cast = look_for_casts_stmt (def_stmt);
815 /* Check that only one cast happened, and it's of non-pointer
816 type. */
817 if ((cast & CT_FROM_NON_P) == (CT_FROM_NON_P)
818 && (cast & ~(CT_FROM_NON_P)) == 0)
820 ((cast_t *)data)->stmt = def_stmt;
821 ((cast_t *)data)->type++;
823 FOR_EACH_SSA_USE_OPERAND (use_p, def_stmt, iter, SSA_OP_ALL_USES)
825 walk_use_def_chains (USE_FROM_PTR (use_p),
826 is_cast_from_non_pointer, data, false);
827 if (((cast_t*)data)->type == -1)
828 break;
831 /* Check that there is no cast, or cast is not harmful. */
832 else if ((cast & CT_NO_CAST) == (CT_NO_CAST)
833 || (cast & CT_DOWN) == (CT_DOWN)
834 || (cast & CT_UP) == (CT_UP)
835 || (cast & CT_USELESS) == (CT_USELESS)
836 || (cast & CT_FROM_MALLOC) == (CT_FROM_MALLOC))
838 FOR_EACH_SSA_USE_OPERAND (use_p, def_stmt, iter, SSA_OP_ALL_USES)
840 walk_use_def_chains (USE_FROM_PTR (use_p),
841 is_cast_from_non_pointer, data, false);
842 if (((cast_t*)data)->type == -1)
843 break;
846 /* The cast is harmful. */
847 else
848 ((cast_t *)data)->type = -1;
851 if (((cast_t*)data)->type == -1)
852 return true;
854 return false;
857 /* When array element a_p[i] is accessed through the pointer a_p
858 and index i, it's translated into the following sequence
859 in gimple:
861 i.1_5 = (unsigned int) i_1;
862 D.1605_6 = i.1_5 * 16;
863 D.1606_7 = (struct str_t *) D.1605_6;
864 a_p.2_8 = a_p;
865 D.1608_9 = D.1606_7 + a_p.2_8;
867 OP0 and OP1 are of the same pointer types and stand for
868 D.1606_7 and a_p.2_8 or vise versa.
870 This function checks that:
872 1. one of OP0 and OP1 (D.1606_7) has passed only one cast from
873 non-pointer type (D.1606_7 = (struct str_t *) D.1605_6;).
875 2. one of OP0 and OP1 which has passed the cast from
876 non-pointer type (D.1606_7), is actually generated by multiplication of
877 index by size of type to which both OP0 and OP1 point to
878 (in this case D.1605_6 = i.1_5 * 16; ).
880 3. an address of def of the var to which was made cast (D.1605_6)
881 was not taken.(How can it happen?)
883 The following items are checked implicitly by the end of algorithm:
885 4. one of OP0 and OP1 (a_p.2_8) have never been cast
886 (because if it was cast to pointer type, its type, that is also
887 the type of OP0 and OP1, will be marked as escaped during
888 analysis of casting stmt (when check_cast() is called
889 from scan_for_refs for this stmt)).
891 5. defs of OP0 and OP1 are not passed into externally visible function
892 (because if they are passed then their type, that is also the type of OP0
893 and OP1, will be marked and escaped during check_call function called from
894 scan_for_refs with call stmt).
896 In total, 1-5 guaranty that it's an access to array by pointer and index.
900 bool
901 is_array_access_through_pointer_and_index (enum tree_code code, tree op0,
902 tree op1, tree *base, tree *offset,
903 gimple *offset_cast_stmt)
905 tree before_cast;
906 gimple before_cast_def_stmt;
907 cast_t op0_cast, op1_cast;
909 *base = NULL;
910 *offset = NULL;
911 *offset_cast_stmt = NULL;
913 /* Check 1. */
914 if (code == POINTER_PLUS_EXPR)
916 tree op0type = TYPE_MAIN_VARIANT (TREE_TYPE (op0));
917 tree op1type = TYPE_MAIN_VARIANT (TREE_TYPE (op1));
919 /* One of op0 and op1 is of pointer type and the other is numerical. */
920 if (POINTER_TYPE_P (op0type) && NUMERICAL_TYPE_CHECK (op1type))
922 *base = op0;
923 *offset = op1;
925 else if (POINTER_TYPE_P (op1type) && NUMERICAL_TYPE_CHECK (op0type))
927 *base = op1;
928 *offset = op0;
930 else
931 return false;
933 else
935 /* Init data for walk_use_def_chains function. */
936 op0_cast.type = op1_cast.type = 0;
937 op0_cast.stmt = op1_cast.stmt = NULL;
939 visited_stmts = pointer_set_create ();
940 walk_use_def_chains (op0, is_cast_from_non_pointer,(void *)(&op0_cast),
941 false);
942 pointer_set_destroy (visited_stmts);
944 visited_stmts = pointer_set_create ();
945 walk_use_def_chains (op1, is_cast_from_non_pointer,(void *)(&op1_cast),
946 false);
947 pointer_set_destroy (visited_stmts);
949 if (op0_cast.type == 1 && op1_cast.type == 0)
951 *base = op1;
952 *offset = op0;
953 *offset_cast_stmt = op0_cast.stmt;
955 else if (op0_cast.type == 0 && op1_cast.type == 1)
957 *base = op0;
958 *offset = op1;
959 *offset_cast_stmt = op1_cast.stmt;
961 else
962 return false;
965 /* Check 2.
966 offset_cast_stmt is of the form:
967 D.1606_7 = (struct str_t *) D.1605_6; */
969 if (*offset_cast_stmt)
971 before_cast = SINGLE_SSA_TREE_OPERAND (*offset_cast_stmt, SSA_OP_USE);
972 if (!before_cast)
973 return false;
975 if (SSA_NAME_IS_DEFAULT_DEF (before_cast))
976 return false;
978 before_cast_def_stmt = SSA_NAME_DEF_STMT (before_cast);
979 if (!before_cast_def_stmt)
980 return false;
982 else
983 before_cast_def_stmt = SSA_NAME_DEF_STMT (*offset);
985 /* before_cast_def_stmt should be of the form:
986 D.1605_6 = i.1_5 * 16; */
988 if (is_gimple_assign (before_cast_def_stmt))
990 /* We expect temporary here. */
991 if (!is_gimple_reg (gimple_assign_lhs (before_cast_def_stmt)))
992 return false;
994 if (gimple_assign_rhs_code (before_cast_def_stmt) == MULT_EXPR)
996 tree arg0 = gimple_assign_rhs1 (before_cast_def_stmt);
997 tree arg1 = gimple_assign_rhs2 (before_cast_def_stmt);
998 tree unit_size =
999 TYPE_SIZE_UNIT (TREE_TYPE (TYPE_MAIN_VARIANT (TREE_TYPE (op0))));
1001 if (!(CONSTANT_CLASS_P (arg0)
1002 && simple_cst_equal (arg0, unit_size))
1003 && !(CONSTANT_CLASS_P (arg1)
1004 && simple_cst_equal (arg1, unit_size)))
1005 return false;
1007 else
1008 return false;
1010 else
1011 return false;
1013 /* Check 3.
1014 check that address of D.1605_6 was not taken.
1015 FIXME: if D.1605_6 is gimple reg than it cannot be addressable. */
1017 return true;
1020 /* Register the parameter and return types of function FN. The type
1021 ESCAPES if the function is visible outside of the compilation
1022 unit. */
1023 static void
1024 check_function_parameter_and_return_types (tree fn, bool escapes)
1026 tree arg;
1028 if (TYPE_ARG_TYPES (TREE_TYPE (fn)))
1030 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1031 arg && TREE_VALUE (arg) != void_type_node;
1032 arg = TREE_CHAIN (arg))
1034 tree type = get_canon_type (TREE_VALUE (arg), false, false);
1035 if (escapes)
1036 mark_interesting_type (type, EXPOSED_PARAMETER);
1039 else
1041 /* FIXME - According to Geoff Keating, we should never have to
1042 do this; the front ends should always process the arg list
1043 from the TYPE_ARG_LIST. However, Geoff is wrong, this code
1044 does seem to be live. */
1046 for (arg = DECL_ARGUMENTS (fn); arg; arg = DECL_CHAIN (arg))
1048 tree type = get_canon_type (TREE_TYPE (arg), false, false);
1049 if (escapes)
1050 mark_interesting_type (type, EXPOSED_PARAMETER);
1053 if (escapes)
1055 tree type = get_canon_type (TREE_TYPE (TREE_TYPE (fn)), false, false);
1056 mark_interesting_type (type, EXPOSED_PARAMETER);
1060 /* Return true if the variable T is the right kind of static variable to
1061 perform compilation unit scope escape analysis. */
1063 static inline void
1064 has_proper_scope_for_analysis (tree t)
1066 /* If the variable has the "used" attribute, treat it as if it had a
1067 been touched by the devil. */
1068 tree type = get_canon_type (TREE_TYPE (t), false, false);
1069 if (!type) return;
1071 if (DECL_PRESERVE_P (t))
1073 mark_interesting_type (type, FULL_ESCAPE);
1074 return;
1077 /* Do not want to do anything with volatile except mark any
1078 function that uses one to be not const or pure. */
1079 if (TREE_THIS_VOLATILE (t))
1080 return;
1082 /* Do not care about a local automatic that is not static. */
1083 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
1084 return;
1086 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
1088 /* If the front end set the variable to be READONLY and
1089 constant, we can allow this variable in pure or const
1090 functions but the scope is too large for our analysis to set
1091 these bits ourselves. */
1093 if (TREE_READONLY (t)
1094 && DECL_INITIAL (t)
1095 && is_gimple_min_invariant (DECL_INITIAL (t)))
1096 ; /* Read of a constant, do not change the function state. */
1097 else
1099 /* The type escapes for all public and externs. */
1100 mark_interesting_type (type, FULL_ESCAPE);
1105 /* If T is a VAR_DECL for a static that we are interested in, add the
1106 uid to the bitmap. */
1108 static void
1109 check_operand (tree t)
1111 if (!t) return;
1113 /* This is an assignment from a function, register the types as
1114 escaping. */
1115 if (TREE_CODE (t) == FUNCTION_DECL)
1116 check_function_parameter_and_return_types (t, true);
1118 else if (TREE_CODE (t) == VAR_DECL)
1119 has_proper_scope_for_analysis (t);
1122 /* Examine tree T for references. */
1124 static void
1125 check_tree (tree t)
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))
1142 check_operand (t);
1143 if (DECL_P (t) && DECL_INITIAL (t))
1144 check_tree (DECL_INITIAL (t));
1148 /* Create an address_of edge FROM_TYPE.TO_TYPE. */
1149 static void
1150 mark_interesting_addressof (tree to_type, tree from_type)
1152 int from_uid;
1153 int to_uid;
1154 bitmap type_map;
1155 splay_tree_node result;
1157 from_type = get_canon_type (from_type, false, false);
1158 to_type = get_canon_type (to_type, false, false);
1160 if (!from_type || !to_type)
1161 return;
1163 from_uid = TYPE_UID (from_type);
1164 to_uid = TYPE_UID (to_type);
1166 gcc_assert (ipa_type_escape_star_count_of_interesting_type (from_type) == 0);
1168 /* Process the Y into X map pointer. */
1169 result = splay_tree_lookup (uid_to_addressof_down_map,
1170 (splay_tree_key) from_uid);
1172 if (result)
1173 type_map = (bitmap) result->value;
1174 else
1176 type_map = BITMAP_ALLOC (&ipa_obstack);
1177 splay_tree_insert (uid_to_addressof_down_map,
1178 from_uid,
1179 (splay_tree_value)type_map);
1181 bitmap_set_bit (type_map, TYPE_UID (to_type));
1183 /* Process the X into Y reverse map pointer. */
1184 result =
1185 splay_tree_lookup (uid_to_addressof_up_map, (splay_tree_key) to_uid);
1187 if (result)
1188 type_map = (bitmap) result->value;
1189 else
1191 type_map = BITMAP_ALLOC (&ipa_obstack);
1192 splay_tree_insert (uid_to_addressof_up_map,
1193 to_uid,
1194 (splay_tree_value)type_map);
1196 bitmap_set_bit (type_map, TYPE_UID (from_type));
1199 /* Scan tree T to see if there are any addresses taken in within T. */
1201 static void
1202 look_for_address_of (tree t)
1204 if (TREE_CODE (t) == ADDR_EXPR)
1206 tree x = get_base_var (t);
1207 tree cref = TREE_OPERAND (t, 0);
1209 /* If we have an expression of the form "&a.b.c.d", mark a.b,
1210 b.c and c.d. as having its address taken. */
1211 tree fielddecl = NULL_TREE;
1212 while (cref!= x)
1214 if (TREE_CODE (cref) == COMPONENT_REF)
1216 fielddecl = TREE_OPERAND (cref, 1);
1217 mark_interesting_addressof (TREE_TYPE (fielddecl),
1218 DECL_FIELD_CONTEXT (fielddecl));
1220 else if (TREE_CODE (cref) == ARRAY_REF)
1221 get_canon_type (TREE_TYPE (cref), false, false);
1223 cref = TREE_OPERAND (cref, 0);
1226 if (TREE_CODE (x) == VAR_DECL)
1227 has_proper_scope_for_analysis (x);
1232 /* Scan tree T to see if there are any casts within it. */
1234 static unsigned int
1235 look_for_casts (tree t)
1237 unsigned int cast = 0;
1239 if (is_gimple_cast (t) || TREE_CODE (t) == VIEW_CONVERT_EXPR)
1241 tree castfromvar = TREE_OPERAND (t, 0);
1242 cast = cast | check_cast (TREE_TYPE (t), castfromvar);
1244 else
1245 while (handled_component_p (t))
1247 t = TREE_OPERAND (t, 0);
1248 if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
1250 /* This may be some part of a component ref.
1251 IE it may be a.b.VIEW_CONVERT_EXPR<weird_type>(c).d, AFAIK.
1252 castfromref will give you a.b.c, not a. */
1253 tree castfromref = TREE_OPERAND (t, 0);
1254 cast = cast | check_cast (TREE_TYPE (t), castfromref);
1256 else if (TREE_CODE (t) == COMPONENT_REF)
1257 get_canon_type (TREE_TYPE (TREE_OPERAND (t, 1)), false, false);
1260 if (!cast)
1261 cast = CT_NO_CAST;
1262 return cast;
1265 /* Check to see if T is a read or address of operation on a static var
1266 we are interested in analyzing. */
1268 static void
1269 check_rhs_var (tree t)
1271 look_for_address_of (t);
1272 check_tree (t);
1275 /* Check to see if T is an assignment to a static var we are
1276 interested in analyzing. */
1278 static void
1279 check_lhs_var (tree t)
1281 check_tree (t);
1284 /* This is a scaled down version of get_asm_expr_operands from
1285 tree_ssa_operands.c. The version there runs much later and assumes
1286 that aliasing information is already available. Here we are just
1287 trying to find if the set of inputs and outputs contain references
1288 or address of operations to local. FN is the function being
1289 analyzed and STMT is the actual asm statement. */
1291 static void
1292 check_asm (gimple stmt)
1294 size_t i;
1296 for (i = 0; i < gimple_asm_noutputs (stmt); i++)
1297 check_lhs_var (gimple_asm_output_op (stmt, i));
1299 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
1300 check_rhs_var (gimple_asm_input_op (stmt, i));
1302 /* There is no code here to check for asm memory clobbers. The
1303 casual maintainer might think that such code would be necessary,
1304 but that appears to be wrong. In other parts of the compiler,
1305 the asm memory clobbers are assumed to only clobber variables
1306 that are addressable. All types with addressable instances are
1307 assumed to already escape. So, we are protected here. */
1311 /* Check the parameters of function call to CALL to mark the
1312 types that pass across the function boundary. Also check to see if
1313 this is either an indirect call, a call outside the compilation
1314 unit. */
1316 static void
1317 check_call (gimple call)
1319 tree callee_t = gimple_call_fndecl (call);
1320 struct cgraph_node* callee;
1321 enum availability avail = AVAIL_NOT_AVAILABLE;
1322 size_t i;
1324 for (i = 0; i < gimple_call_num_args (call); i++)
1325 check_rhs_var (gimple_call_arg (call, i));
1327 if (callee_t)
1329 tree arg_type;
1330 tree last_arg_type = NULL;
1331 callee = cgraph_node(callee_t);
1332 avail = cgraph_function_body_availability (callee);
1334 /* Check that there are no implicit casts in the passing of
1335 parameters. */
1336 if (TYPE_ARG_TYPES (TREE_TYPE (callee_t)))
1338 for (arg_type = TYPE_ARG_TYPES (TREE_TYPE (callee_t)), i = 0;
1339 arg_type && TREE_VALUE (arg_type) != void_type_node
1340 && i < gimple_call_num_args (call);
1341 arg_type = TREE_CHAIN (arg_type), i++)
1343 tree operand = gimple_call_arg (call, i);
1344 if (operand)
1346 last_arg_type = TREE_VALUE(arg_type);
1347 check_cast (last_arg_type, operand);
1349 else
1350 /* The code reaches here for some unfortunate
1351 builtin functions that do not have a list of
1352 argument types. */
1353 break;
1356 else
1358 /* FIXME - According to Geoff Keating, we should never
1359 have to do this; the front ends should always process
1360 the arg list from the TYPE_ARG_LIST. */
1361 for (arg_type = DECL_ARGUMENTS (callee_t), i = 0;
1362 arg_type && i < gimple_call_num_args (call);
1363 arg_type = TREE_CHAIN (arg_type), i++)
1365 tree operand = gimple_call_arg (call, i);
1366 if (operand)
1368 last_arg_type = TREE_TYPE (arg_type);
1369 check_cast (last_arg_type, operand);
1371 else
1372 /* The code reaches here for some unfortunate
1373 builtin functions that do not have a list of
1374 argument types. */
1375 break;
1379 /* In the case where we have a var_args function, we need to
1380 check the remaining parameters against the last argument. */
1381 arg_type = last_arg_type;
1382 for ( ; i < gimple_call_num_args (call); i++)
1384 tree operand = gimple_call_arg (call, i);
1385 if (arg_type)
1386 check_cast (arg_type, operand);
1387 else
1389 /* The code reaches here for some unfortunate
1390 builtin functions that do not have a list of
1391 argument types. Most of these functions have
1392 been marked as having their parameters not
1393 escape, but for the rest, the type is doomed. */
1394 tree type = get_canon_type (TREE_TYPE (operand), false, false);
1395 mark_interesting_type (type, FULL_ESCAPE);
1400 /* The callee is either unknown (indirect call) or there is just no
1401 scannable code for it (external call) . We look to see if there
1402 are any bits available for the callee (such as by declaration or
1403 because it is builtin) and process solely on the basis of those
1404 bits. */
1405 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
1407 /* If this is a direct call to an external function, mark all of
1408 the parameter and return types. */
1409 for (i = 0; i < gimple_call_num_args (call); i++)
1411 tree operand = gimple_call_arg (call, i);
1412 tree type = get_canon_type (TREE_TYPE (operand), false, false);
1413 mark_interesting_type (type, EXPOSED_PARAMETER);
1416 if (callee_t)
1418 tree type =
1419 get_canon_type (TREE_TYPE (TREE_TYPE (callee_t)), false, false);
1420 mark_interesting_type (type, EXPOSED_PARAMETER);
1425 /* CODE is the operation on OP0 and OP1. OP0 is the operand that we
1426 *know* is a pointer type. OP1 may be a pointer type. */
1427 static bool
1428 okay_pointer_operation (enum tree_code code, tree op0, tree op1)
1430 tree op0type = TYPE_MAIN_VARIANT (TREE_TYPE (op0));
1432 switch (code)
1434 case MULT_EXPR:
1435 /* Multiplication does not change alignment. */
1436 return true;
1437 break;
1438 case MINUS_EXPR:
1439 case PLUS_EXPR:
1440 case POINTER_PLUS_EXPR:
1442 tree base, offset;
1443 gimple offset_cast_stmt;
1445 if (POINTER_TYPE_P (op0type)
1446 && TREE_CODE (op0) == SSA_NAME
1447 && TREE_CODE (op1) == SSA_NAME
1448 && is_array_access_through_pointer_and_index (code, op0, op1,
1449 &base,
1450 &offset,
1451 &offset_cast_stmt))
1452 return true;
1453 else
1455 tree size_of_op0_points_to = TYPE_SIZE_UNIT (TREE_TYPE (op0type));
1457 if (CONSTANT_CLASS_P (op1)
1458 && size_of_op0_points_to
1459 && multiple_of_p (TREE_TYPE (size_of_op0_points_to),
1460 op1, size_of_op0_points_to))
1461 return true;
1463 if (CONSTANT_CLASS_P (op0)
1464 && size_of_op0_points_to
1465 && multiple_of_p (TREE_TYPE (size_of_op0_points_to),
1466 op0, size_of_op0_points_to))
1467 return true;
1470 break;
1471 default:
1472 return false;
1474 return false;
1479 /* Helper for scan_for_refs. Check the operands of an assignment to
1480 mark types that may escape. */
1482 static void
1483 check_assign (gimple t)
1485 /* First look on the lhs and see what variable is stored to */
1486 check_lhs_var (gimple_assign_lhs (t));
1488 /* For the purposes of figuring out what the cast affects */
1490 /* Next check the operands on the rhs to see if they are ok. */
1491 switch (TREE_CODE_CLASS (gimple_assign_rhs_code (t)))
1493 case tcc_binary:
1495 tree op0 = gimple_assign_rhs1 (t);
1496 tree type0 = get_canon_type (TREE_TYPE (op0), false, false);
1497 tree op1 = gimple_assign_rhs2 (t);
1498 tree type1 = get_canon_type (TREE_TYPE (op1), false, false);
1500 /* If this is pointer arithmetic of any bad sort, then
1501 we need to mark the types as bad. For binary
1502 operations, no binary operator we currently support
1503 is always "safe" in regard to what it would do to
1504 pointers for purposes of determining which types
1505 escape, except operations of the size of the type.
1506 It is possible that min and max under the right set
1507 of circumstances and if the moon is in the correct
1508 place could be safe, but it is hard to see how this
1509 is worth the effort. */
1510 if (type0 && POINTER_TYPE_P (type0)
1511 && !okay_pointer_operation (gimple_assign_rhs_code (t), op0, op1))
1512 mark_interesting_type (type0, FULL_ESCAPE);
1514 if (type1 && POINTER_TYPE_P (type1)
1515 && !okay_pointer_operation (gimple_assign_rhs_code (t), op1, op0))
1516 mark_interesting_type (type1, FULL_ESCAPE);
1518 look_for_casts (op0);
1519 look_for_casts (op1);
1520 check_rhs_var (op0);
1521 check_rhs_var (op1);
1523 break;
1525 case tcc_unary:
1527 tree op0 = gimple_assign_rhs1 (t);
1528 tree type0 = get_canon_type (TREE_TYPE (op0), false, false);
1530 /* For unary operations, if the operation is NEGATE or ABS on
1531 a pointer, this is also considered pointer arithmetic and
1532 thus, bad for business. */
1533 if (type0
1534 && POINTER_TYPE_P (type0)
1535 && (TREE_CODE (op0) == NEGATE_EXPR
1536 || TREE_CODE (op0) == ABS_EXPR))
1537 mark_interesting_type (type0, FULL_ESCAPE);
1539 check_rhs_var (op0);
1540 look_for_casts (op0);
1542 break;
1544 case tcc_reference:
1545 look_for_casts (gimple_assign_rhs1 (t));
1546 check_rhs_var (gimple_assign_rhs1 (t));
1547 break;
1549 case tcc_declaration:
1550 check_rhs_var (gimple_assign_rhs1 (t));
1551 break;
1553 case tcc_expression:
1554 if (gimple_assign_rhs_code (t) == ADDR_EXPR)
1556 tree rhs = gimple_assign_rhs1 (t);
1557 look_for_casts (TREE_OPERAND (rhs, 0));
1558 check_rhs_var (rhs);
1560 break;
1562 default:
1563 break;
1568 /* Scan statement T for references to types and mark anything
1569 interesting. */
1571 static void
1572 scan_for_refs (gimple t)
1574 switch (gimple_code (t))
1576 case GIMPLE_ASSIGN:
1577 check_assign (t);
1578 break;
1580 case GIMPLE_CALL:
1581 /* If this is a call to malloc, squirrel away the result so we
1582 do mark the resulting cast as being bad. */
1583 check_call (t);
1584 break;
1586 case GIMPLE_ASM:
1587 check_asm (t);
1588 break;
1590 default:
1591 break;
1594 return;
1598 /* The init routine for analyzing global static variable usage. See
1599 comments at top for description. */
1600 static void
1601 ipa_init (void)
1603 bitmap_obstack_initialize (&ipa_obstack);
1604 global_types_exposed_parameter = BITMAP_ALLOC (&ipa_obstack);
1605 global_types_full_escape = BITMAP_ALLOC (&ipa_obstack);
1606 global_types_seen = BITMAP_ALLOC (&ipa_obstack);
1608 uid_to_canon_type = splay_tree_new (splay_tree_compare_ints, 0, 0);
1609 all_canon_types = splay_tree_new (compare_type_brand, 0, 0);
1610 type_to_canon_type = splay_tree_new (splay_tree_compare_pointers, 0, 0);
1611 uid_to_subtype_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1612 uid_to_addressof_down_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1613 uid_to_addressof_up_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1615 /* There are some shared nodes, in particular the initializers on
1616 static declarations. We do not need to scan them more than once
1617 since all we would be interested in are the addressof
1618 operations. */
1619 visited_nodes = pointer_set_create ();
1620 initialized = true;
1623 /* Check out the rhs of a static or global initialization VNODE to see
1624 if any of them contain addressof operations. Note that some of
1625 these variables may not even be referenced in the code in this
1626 compilation unit but their right hand sides may contain references
1627 to variables defined within this unit. */
1629 static void
1630 analyze_variable (struct varpool_node *vnode)
1632 tree global = vnode->decl;
1633 tree type = get_canon_type (TREE_TYPE (global), false, false);
1635 /* If this variable has exposure beyond the compilation unit, add
1636 its type to the global types. */
1638 if (vnode->externally_visible)
1639 mark_interesting_type (type, FULL_ESCAPE);
1641 gcc_assert (TREE_CODE (global) == VAR_DECL);
1643 if (DECL_INITIAL (global))
1644 check_tree (DECL_INITIAL (global));
1647 /* This is the main routine for finding the reference patterns for
1648 global variables within a function FN. */
1650 static void
1651 analyze_function (struct cgraph_node *fn)
1653 tree decl = fn->decl;
1654 check_function_parameter_and_return_types (decl,
1655 fn->local.externally_visible);
1656 if (dump_file)
1657 fprintf (dump_file, "\n local analysis of %s", cgraph_node_name (fn));
1660 struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
1661 basic_block this_block;
1663 FOR_EACH_BB_FN (this_block, this_cfun)
1665 gimple_stmt_iterator gsi;
1666 for (gsi = gsi_start_bb (this_block); !gsi_end_p (gsi); gsi_next (&gsi))
1667 scan_for_refs (gsi_stmt (gsi));
1671 /* There may be const decls with interesting right hand sides. */
1672 if (DECL_STRUCT_FUNCTION (decl))
1674 tree var;
1675 unsigned ix;
1677 FOR_EACH_LOCAL_DECL (DECL_STRUCT_FUNCTION (decl), ix, var)
1679 if (TREE_CODE (var) == VAR_DECL
1680 && DECL_INITIAL (var)
1681 && !TREE_STATIC (var))
1682 check_tree (DECL_INITIAL (var));
1683 get_canon_type (TREE_TYPE (var), false, false);
1690 /* Convert a type_UID into a type. */
1691 static tree
1692 type_for_uid (int uid)
1694 splay_tree_node result =
1695 splay_tree_lookup (uid_to_canon_type, (splay_tree_key) uid);
1697 if (result)
1698 return (tree) result->value;
1699 else return NULL;
1702 /* Return a bitmap with the subtypes of the type for UID. If it
1703 does not exist, return either NULL or a new bitmap depending on the
1704 value of CREATE. */
1706 static bitmap
1707 subtype_map_for_uid (int uid, bool create)
1709 splay_tree_node result = splay_tree_lookup (uid_to_subtype_map,
1710 (splay_tree_key) uid);
1712 if (result)
1713 return (bitmap) result->value;
1714 else if (create)
1716 bitmap subtype_map = BITMAP_ALLOC (&ipa_obstack);
1717 splay_tree_insert (uid_to_subtype_map,
1718 uid,
1719 (splay_tree_value)subtype_map);
1720 return subtype_map;
1722 else return NULL;
1725 /* Mark all of the supertypes and field types of TYPE as being seen.
1726 Also accumulate the subtypes for each type so that
1727 close_types_full_escape can mark a subtype as escaping if the
1728 supertype escapes. */
1730 static void
1731 close_type_seen (tree type)
1733 tree field;
1734 int i, uid;
1735 tree binfo, base_binfo;
1737 /* See thru all pointer tos and array ofs. */
1738 type = get_canon_type (type, true, true);
1739 if (!type)
1740 return;
1742 uid = TYPE_UID (type);
1744 if (!bitmap_set_bit (been_there_done_that, uid))
1745 return;
1747 /* If we are doing a language with a type hierarchy, mark all of
1748 the superclasses. */
1749 if (TYPE_BINFO (type))
1750 for (binfo = TYPE_BINFO (type), i = 0;
1751 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1753 tree binfo_type = BINFO_TYPE (base_binfo);
1754 bitmap subtype_map = subtype_map_for_uid
1755 (TYPE_UID (TYPE_MAIN_VARIANT (binfo_type)), true);
1756 bitmap_set_bit (subtype_map, uid);
1757 close_type_seen (get_canon_type (binfo_type, true, true));
1760 /* If the field is a struct or union type, mark all of the
1761 subfields. */
1762 for (field = TYPE_FIELDS (type);
1763 field;
1764 field = DECL_CHAIN (field))
1766 tree field_type;
1767 if (TREE_CODE (field) != FIELD_DECL)
1768 continue;
1770 field_type = TREE_TYPE (field);
1771 if (ipa_type_escape_star_count_of_interesting_or_array_type (field_type) >= 0)
1772 close_type_seen (get_canon_type (field_type, true, true));
1776 /* Take a TYPE that has been passed by value to an external function
1777 and mark all of the fields that have pointer types as escaping. For
1778 any of the non pointer types that are structures or unions,
1779 recurse. TYPE is never a pointer type. */
1781 static void
1782 close_type_exposed_parameter (tree type)
1784 tree field;
1785 int uid;
1787 type = get_canon_type (type, false, false);
1788 if (!type)
1789 return;
1790 uid = TYPE_UID (type);
1791 gcc_assert (!POINTER_TYPE_P (type));
1793 if (!bitmap_set_bit (been_there_done_that, uid))
1794 return;
1796 /* If the field is a struct or union type, mark all of the
1797 subfields. */
1798 for (field = TYPE_FIELDS (type);
1799 field;
1800 field = TREE_CHAIN (field))
1802 tree field_type;
1804 if (TREE_CODE (field) != FIELD_DECL)
1805 continue;
1807 field_type = get_canon_type (TREE_TYPE (field), false, false);
1808 mark_interesting_type (field_type, EXPOSED_PARAMETER);
1810 /* Only recurse for non pointer types of structures and unions. */
1811 if (ipa_type_escape_star_count_of_interesting_type (field_type) == 0)
1812 close_type_exposed_parameter (field_type);
1816 /* The next function handles the case where a type fully escapes.
1817 This means that not only does the type itself escape,
1819 a) the type of every field recursively escapes
1820 b) the type of every subtype escapes as well as the super as well
1821 as all of the pointer to types for each field.
1823 Note that pointer to types are not marked as escaping. If the
1824 pointed to type escapes, the pointer to type also escapes.
1826 Take a TYPE that has had the address taken for an instance of it
1827 and mark all of the types for its fields as having their addresses
1828 taken. */
1830 static void
1831 close_type_full_escape (tree type)
1833 tree field;
1834 unsigned int i;
1835 int uid;
1836 tree binfo, base_binfo;
1837 bitmap_iterator bi;
1838 bitmap subtype_map;
1839 splay_tree_node address_result;
1841 /* Strip off any pointer or array types. */
1842 type = get_canon_type (type, true, true);
1843 if (!type)
1844 return;
1845 uid = TYPE_UID (type);
1847 if (!bitmap_set_bit (been_there_done_that, uid))
1848 return;
1850 subtype_map = subtype_map_for_uid (uid, false);
1852 /* If we are doing a language with a type hierarchy, mark all of
1853 the superclasses. */
1854 if (TYPE_BINFO (type))
1855 for (binfo = TYPE_BINFO (type), i = 0;
1856 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1858 tree binfotype = BINFO_TYPE (base_binfo);
1859 binfotype = mark_type (binfotype, FULL_ESCAPE);
1860 close_type_full_escape (binfotype);
1863 /* Mark as escaped any types that have been down casted to
1864 this type. */
1865 if (subtype_map)
1866 EXECUTE_IF_SET_IN_BITMAP (subtype_map, 0, i, bi)
1868 tree subtype = type_for_uid (i);
1869 subtype = mark_type (subtype, FULL_ESCAPE);
1870 close_type_full_escape (subtype);
1873 /* If the field is a struct or union type, mark all of the
1874 subfields. */
1875 for (field = TYPE_FIELDS (type);
1876 field;
1877 field = TREE_CHAIN (field))
1879 tree field_type;
1880 if (TREE_CODE (field) != FIELD_DECL)
1881 continue;
1883 field_type = TREE_TYPE (field);
1884 if (ipa_type_escape_star_count_of_interesting_or_array_type (field_type) >= 0)
1886 field_type = mark_type (field_type, FULL_ESCAPE);
1887 close_type_full_escape (field_type);
1891 /* For all of the types A that contain this type B and were part of
1892 an expression like "&...A.B...", mark the A's as escaping. */
1893 address_result = splay_tree_lookup (uid_to_addressof_up_map,
1894 (splay_tree_key) uid);
1895 if (address_result)
1897 bitmap containing_classes = (bitmap) address_result->value;
1898 EXECUTE_IF_SET_IN_BITMAP (containing_classes, 0, i, bi)
1900 close_type_full_escape (type_for_uid (i));
1905 /* Transitively close the addressof bitmap for the type with UID.
1906 This means that if we had a.b and b.c, a would have both b and c in
1907 its maps. */
1909 static bitmap
1910 close_addressof_down (int uid)
1912 bitmap_iterator bi;
1913 splay_tree_node result =
1914 splay_tree_lookup (uid_to_addressof_down_map, (splay_tree_key) uid);
1915 bitmap map = NULL;
1916 bitmap new_map;
1917 unsigned int i;
1919 if (result)
1920 map = (bitmap) result->value;
1921 else
1922 return NULL;
1924 if (!bitmap_set_bit (been_there_done_that, uid))
1925 return map;
1927 /* If the type escapes, get rid of the addressof map, it will not be
1928 needed. */
1929 if (bitmap_bit_p (global_types_full_escape, uid))
1931 BITMAP_FREE (map);
1932 splay_tree_remove (uid_to_addressof_down_map, (splay_tree_key) uid);
1933 return NULL;
1936 /* The new_map will have all of the bits for the enclosed fields and
1937 will have the unique id version of the old map. */
1938 new_map = BITMAP_ALLOC (&ipa_obstack);
1940 EXECUTE_IF_SET_IN_BITMAP (map, 0, i, bi)
1942 bitmap submap = close_addressof_down (i);
1943 bitmap_set_bit (new_map, i);
1944 if (submap)
1945 bitmap_ior_into (new_map, submap);
1947 result->value = (splay_tree_value) new_map;
1949 BITMAP_FREE (map);
1950 return new_map;
1954 /* The main entry point for type escape analysis. */
1956 static unsigned int
1957 type_escape_execute (void)
1959 struct cgraph_node *node;
1960 struct varpool_node *vnode;
1961 unsigned int i;
1962 bitmap_iterator bi;
1963 splay_tree_node result;
1965 ipa_init ();
1967 /* Process all of the variables first. */
1968 FOR_EACH_STATIC_VARIABLE (vnode)
1969 analyze_variable (vnode);
1971 /* Process all of the functions next.
1973 We do not want to process any of the clones so we check that this
1974 is a master clone. However, we do need to process any
1975 AVAIL_OVERWRITABLE functions (these are never clones) because
1976 they may cause a type variable to escape.
1978 for (node = cgraph_nodes; node; node = node->next)
1979 if (node->analyzed && !node->clone_of)
1980 analyze_function (node);
1983 pointer_set_destroy (visited_nodes);
1984 visited_nodes = NULL;
1986 /* Do all of the closures to discover which types escape the
1987 compilation unit. */
1989 been_there_done_that = BITMAP_ALLOC (&ipa_obstack);
1990 bitmap_tmp = BITMAP_ALLOC (&ipa_obstack);
1992 /* Examine the types that we have directly seen in scanning the code
1993 and add to that any contained types or superclasses. */
1995 bitmap_copy (bitmap_tmp, global_types_seen);
1996 EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
1998 tree type = type_for_uid (i);
1999 /* Only look at records and unions and pointer tos. */
2000 if (ipa_type_escape_star_count_of_interesting_or_array_type (type) >= 0)
2001 close_type_seen (type);
2003 bitmap_clear (been_there_done_that);
2005 /* Examine all of the types passed by value and mark any enclosed
2006 pointer types as escaping. */
2007 bitmap_copy (bitmap_tmp, global_types_exposed_parameter);
2008 EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
2010 close_type_exposed_parameter (type_for_uid (i));
2012 bitmap_clear (been_there_done_that);
2014 /* Close the types for escape. If something escapes, then any
2015 enclosed types escape as well as any subtypes. */
2016 bitmap_copy (bitmap_tmp, global_types_full_escape);
2017 EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
2019 close_type_full_escape (type_for_uid (i));
2021 bitmap_clear (been_there_done_that);
2023 /* Before this pass, the uid_to_addressof_down_map for type X
2024 contained an entry for Y if there had been an operation of the
2025 form &X.Y. This step adds all of the fields contained within Y
2026 (recursively) to X's map. */
2028 result = splay_tree_min (uid_to_addressof_down_map);
2029 while (result)
2031 int uid = result->key;
2032 /* Close the addressof map, i.e. copy all of the transitive
2033 substructures up to this level. */
2034 close_addressof_down (uid);
2035 result = splay_tree_successor (uid_to_addressof_down_map, uid);
2038 /* Do not need the array types and pointer types in the persistent
2039 data structures. */
2040 result = splay_tree_min (all_canon_types);
2041 while (result)
2043 tree type = (tree) result->value;
2044 tree key = (tree) result->key;
2045 if (POINTER_TYPE_P (type)
2046 || TREE_CODE (type) == ARRAY_TYPE)
2048 splay_tree_remove (all_canon_types, (splay_tree_key) result->key);
2049 splay_tree_remove (type_to_canon_type, (splay_tree_key) type);
2050 splay_tree_remove (uid_to_canon_type, (splay_tree_key) TYPE_UID (type));
2051 bitmap_clear_bit (global_types_seen, TYPE_UID (type));
2053 result = splay_tree_successor (all_canon_types, (splay_tree_key) key);
2056 if (dump_file)
2058 EXECUTE_IF_SET_IN_BITMAP (global_types_seen, 0, i, bi)
2060 /* The pointer types are in the global_types_full_escape
2061 bitmap but not in the backwards map. They also contain
2062 no useful information since they are not marked. */
2063 tree type = type_for_uid (i);
2064 fprintf(dump_file, "type %d ", i);
2065 print_generic_expr (dump_file, type, 0);
2066 if (bitmap_bit_p (global_types_full_escape, i))
2067 fprintf(dump_file, " escaped\n");
2068 else
2069 fprintf(dump_file, " contained\n");
2073 /* Get rid of uid_to_addressof_up_map and its bitmaps. */
2074 result = splay_tree_min (uid_to_addressof_up_map);
2075 while (result)
2077 int uid = (int)result->key;
2078 bitmap bm = (bitmap)result->value;
2080 BITMAP_FREE (bm);
2081 splay_tree_remove (uid_to_addressof_up_map, (splay_tree_key) uid);
2082 result = splay_tree_successor (uid_to_addressof_up_map, uid);
2085 /* Get rid of the subtype map. */
2086 result = splay_tree_min (uid_to_subtype_map);
2087 while (result)
2089 bitmap b = (bitmap)result->value;
2090 BITMAP_FREE(b);
2091 splay_tree_remove (uid_to_subtype_map, result->key);
2092 result = splay_tree_min (uid_to_subtype_map);
2094 splay_tree_delete (uid_to_subtype_map);
2095 uid_to_subtype_map = NULL;
2097 BITMAP_FREE (global_types_exposed_parameter);
2098 BITMAP_FREE (been_there_done_that);
2099 BITMAP_FREE (bitmap_tmp);
2100 return 0;
2103 static bool
2104 gate_type_escape_vars (void)
2106 return flag_ipa_struct_reorg && flag_whole_program && (optimize > 0);
2109 struct simple_ipa_opt_pass pass_ipa_type_escape =
2112 SIMPLE_IPA_PASS,
2113 "type-escape-var", /* name */
2114 gate_type_escape_vars, /* gate */
2115 type_escape_execute, /* execute */
2116 NULL, /* sub */
2117 NULL, /* next */
2118 0, /* static_pass_number */
2119 TV_IPA_TYPE_ESCAPE, /* tv_id */
2120 0, /* properties_required */
2121 0, /* properties_provided */
2122 0, /* properties_destroyed */
2123 0, /* todo_flags_start */
2124 0 /* todo_flags_finish */