Radar 8710868: Build a separate version of libcc_kext.a for each ARM slice.
[llvm-gcc-4.2.git] / gcc / ipa-type-escape.c
blob495193d3f5f0ef220cc3be23bf76d5770b873bcb
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
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 /* This pass determines which types in the program contain only
23 instances that are completely encapsulated by the compilation unit.
24 Those types that are encapsulated must also pass the further
25 requirement that there be no bad operations on any instances of
26 those types.
28 A great deal of freedom in compilation is allowed for the instances
29 of those types that pass these conditions.
32 /* The code in this module is called by the ipa pass manager. It
33 should be one of the later passes since its information is used by
34 the rest of the compilation. */
36 #include "config.h"
37 #include "system.h"
38 #include "coretypes.h"
39 #include "tm.h"
40 #include "tree.h"
41 #include "tree-flow.h"
42 #include "tree-inline.h"
43 #include "tree-pass.h"
44 #include "langhooks.h"
45 #include "pointer-set.h"
46 #include "ggc.h"
47 #include "ipa-utils.h"
48 #include "ipa-type-escape.h"
49 #include "c-common.h"
50 #include "tree-gimple.h"
51 #include "cgraph.h"
52 #include "output.h"
53 #include "flags.h"
54 #include "timevar.h"
55 #include "diagnostic.h"
56 #include "langhooks.h"
58 /* Some of the aliasing is called very early, before this phase is
59 called. To assure that this is not a problem, we keep track of if
60 this phase has been run. */
61 static bool initialized = false;
63 /* This bitmap contains the set of local vars that are the lhs of
64 calls to mallocs. These variables, when seen on the rhs as part of
65 a cast, the cast are not marked as doing bad things to the type
66 even though they are generally of the form
67 "foo = (type_of_foo)void_temp". */
68 static bitmap results_of_malloc;
70 /* Scratch bitmap for avoiding work. */
71 static bitmap been_there_done_that;
72 static bitmap bitmap_tmp;
74 /* There are two levels of escape that types can undergo.
76 EXPOSED_PARAMETER - some instance of the variable is
77 passed by value into an externally visible function or some
78 instance of the variable is passed out of an externally visible
79 function as a return value. In this case any of the fields of the
80 variable that are pointer types end up having their types marked as
81 FULL_ESCAPE.
83 FULL_ESCAPE - when bad things happen to good types. One of the
84 following things happens to the type: (a) either an instance of the
85 variable has its address passed to an externally visible function,
86 (b) the address is taken and some bad cast happens to the address
87 or (c) explicit arithmetic is done to the address.
90 enum escape_t
92 EXPOSED_PARAMETER,
93 FULL_ESCAPE
96 /* The following two bit vectors global_types_* correspond to
97 previous cases above. During the analysis phase, a bit is set in
98 one of these vectors if an operation of the offending class is
99 discovered to happen on the associated type. */
101 static bitmap global_types_exposed_parameter;
102 static bitmap global_types_full_escape;
104 /* All of the types seen in this compilation unit. */
105 static bitmap global_types_seen;
106 /* Reverse map to take a canon uid and map it to a canon type. Uid's
107 are never manipulated unless they are associated with a canon
108 type. */
109 static splay_tree uid_to_canon_type;
111 /* Internal structure of type mapping code. This maps a canon type
112 name to its canon type. */
113 static splay_tree all_canon_types;
115 /* Map from type clones to the single canon type. */
116 static splay_tree type_to_canon_type;
118 /* A splay tree of bitmaps. An element X in the splay tree has a bit
119 set in its bitmap at TYPE_UID (TYPE_MAIN_VARIANT (Y)) if there was
120 an operation in the program of the form "&X.Y". */
121 static splay_tree uid_to_addressof_down_map;
123 /* A splay tree of bitmaps. An element Y in the splay tree has a bit
124 set in its bitmap at TYPE_UID (TYPE_MAIN_VARIANT (X)) if there was
125 an operation in the program of the form "&X.Y". */
126 static splay_tree uid_to_addressof_up_map;
128 /* Tree to hold the subtype maps used to mark subtypes of escaped
129 types. */
130 static splay_tree uid_to_subtype_map;
132 /* Records tree nodes seen in cgraph_create_edges. Simply using
133 walk_tree_without_duplicates doesn't guarantee each node is visited
134 once because it gets a new htab upon each recursive call from
135 scan_for_refs. */
136 static struct pointer_set_t *visited_nodes;
138 static bitmap_obstack ipa_obstack;
140 /* Get the name of TYPE or return the string "<UNNAMED>". */
141 static char*
142 get_name_of_type (tree type)
144 tree name = TYPE_NAME (type);
146 if (!name)
147 /* Unnamed type, do what you like here. */
148 return (char*)"<UNNAMED>";
150 /* It will be a TYPE_DECL in the case of a typedef, otherwise, an
151 identifier_node */
152 if (TREE_CODE (name) == TYPE_DECL)
154 /* Each DECL has a DECL_NAME field which contains an
155 IDENTIFIER_NODE. (Some decls, most often labels, may have
156 zero as the DECL_NAME). */
157 if (DECL_NAME (name))
158 return (char*)IDENTIFIER_POINTER (DECL_NAME (name));
159 else
160 /* Unnamed type, do what you like here. */
161 return (char*)"<UNNAMED>";
163 else if (TREE_CODE (name) == IDENTIFIER_NODE)
164 return (char*)IDENTIFIER_POINTER (name);
165 else
166 return (char*)"<UNNAMED>";
169 struct type_brand_s
171 char* name;
172 int seq;
175 /* Splay tree comparison function on type_brand_s structures. */
177 static int
178 compare_type_brand (splay_tree_key sk1, splay_tree_key sk2)
180 struct type_brand_s * k1 = (struct type_brand_s *) sk1;
181 struct type_brand_s * k2 = (struct type_brand_s *) sk2;
183 int value = strcmp(k1->name, k2->name);
184 if (value == 0)
185 return k2->seq - k1->seq;
186 else
187 return value;
190 /* All of the "unique_type" code is a hack to get around the sleazy
191 implementation used to compile more than file. Currently gcc does
192 not get rid of multiple instances of the same type that have been
193 collected from different compilation units. */
194 /* This is a trivial algorithm for removing duplicate types. This
195 would not work for any language that used structural equivalence as
196 the basis of its type system. */
197 /* Return either TYPE if this is first time TYPE has been seen an
198 compatible TYPE that has already been processed. */
200 static tree
201 discover_unique_type (tree type)
203 struct type_brand_s * brand = XNEW (struct type_brand_s);
204 int i = 0;
205 splay_tree_node result;
207 brand->name = get_name_of_type (type);
209 while (1)
211 brand->seq = i++;
212 result = splay_tree_lookup (all_canon_types, (splay_tree_key) brand);
214 if (result)
216 /* Create an alias since this is just the same as
217 other_type. */
218 tree other_type = (tree) result->value;
219 if (lang_hooks.types_compatible_p (type, other_type) == 1)
221 free (brand);
222 /* Insert this new type as an alias for other_type. */
223 splay_tree_insert (type_to_canon_type,
224 (splay_tree_key) type,
225 (splay_tree_value) other_type);
226 return other_type;
228 /* Not compatible, look for next instance with same name. */
230 else
232 /* No more instances, create new one since this is the first
233 time we saw this type. */
234 brand->seq = i++;
235 /* Insert the new brand. */
236 splay_tree_insert (all_canon_types,
237 (splay_tree_key) brand,
238 (splay_tree_value) type);
240 /* Insert this new type as an alias for itself. */
241 splay_tree_insert (type_to_canon_type,
242 (splay_tree_key) type,
243 (splay_tree_value) type);
245 /* Insert the uid for reverse lookup; */
246 splay_tree_insert (uid_to_canon_type,
247 (splay_tree_key) TYPE_UID (type),
248 (splay_tree_value) type);
250 bitmap_set_bit (global_types_seen, TYPE_UID (type));
251 return type;
256 /* Return true if TYPE is one of the type classes that we are willing
257 to analyze. This skips the goofy types like arrays of pointers to
258 methods. */
259 static bool
260 type_to_consider (tree type)
262 /* Strip the *'s off. */
263 type = TYPE_MAIN_VARIANT (type);
264 while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
265 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
267 switch (TREE_CODE (type))
269 case BOOLEAN_TYPE:
270 case COMPLEX_TYPE:
271 case ENUMERAL_TYPE:
272 case INTEGER_TYPE:
273 case QUAL_UNION_TYPE:
274 case REAL_TYPE:
275 case RECORD_TYPE:
276 case UNION_TYPE:
277 case VECTOR_TYPE:
278 case VOID_TYPE:
279 return true;
281 default:
282 return false;
286 /* Get the canon type of TYPE. If SEE_THRU_PTRS is true, remove all
287 the POINTER_TOs and if SEE_THRU_ARRAYS is true, remove all of the
288 ARRAY_OFs and POINTER_TOs. */
290 static tree
291 get_canon_type (tree type, bool see_thru_ptrs, bool see_thru_arrays)
293 splay_tree_node result;
294 /* Strip the *'s off. */
295 if (!type || !type_to_consider (type))
296 return NULL;
298 type = TYPE_MAIN_VARIANT (type);
299 if (see_thru_arrays)
300 while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
301 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
303 else if (see_thru_ptrs)
304 while (POINTER_TYPE_P (type))
305 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
307 result = splay_tree_lookup(type_to_canon_type, (splay_tree_key) type);
309 if (result == NULL)
310 return discover_unique_type (type);
311 else return (tree) result->value;
314 /* Same as GET_CANON_TYPE, except return the TYPE_ID rather than the
315 TYPE. */
317 static int
318 get_canon_type_uid (tree type, bool see_thru_ptrs, bool see_thru_arrays)
320 type = get_canon_type (type, see_thru_ptrs, see_thru_arrays);
321 if (type)
322 return TYPE_UID(type);
323 else return 0;
326 /* Return 0 if TYPE is a record or union type. Return a positive
327 number if TYPE is a pointer to a record or union. The number is
328 the number of pointer types stripped to get to the record or union
329 type. Return -1 if TYPE is none of the above. */
332 ipa_type_escape_star_count_of_interesting_type (tree type)
334 int count = 0;
335 /* Strip the *'s off. */
336 if (!type)
337 return -1;
338 type = TYPE_MAIN_VARIANT (type);
339 while (POINTER_TYPE_P (type))
341 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
342 count++;
345 /* We are interested in records, and unions only. */
346 if (TREE_CODE (type) == RECORD_TYPE
347 || TREE_CODE (type) == QUAL_UNION_TYPE
348 || TREE_CODE (type) == UNION_TYPE)
349 return count;
350 else
351 return -1;
355 /* Return 0 if TYPE is a record or union type. Return a positive
356 number if TYPE is a pointer to a record or union. The number is
357 the number of pointer types stripped to get to the record or union
358 type. Return -1 if TYPE is none of the above. */
361 ipa_type_escape_star_count_of_interesting_or_array_type (tree type)
363 int count = 0;
364 /* Strip the *'s off. */
365 if (!type)
366 return -1;
367 type = TYPE_MAIN_VARIANT (type);
368 while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
370 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
371 count++;
374 /* We are interested in records, and unions only. */
375 if (TREE_CODE (type) == RECORD_TYPE
376 || TREE_CODE (type) == QUAL_UNION_TYPE
377 || TREE_CODE (type) == UNION_TYPE)
378 return count;
379 else
380 return -1;
384 /* Return true if the record, or union TYPE passed in escapes this
385 compilation unit. Note that all of the pointer-to's are removed
386 before testing since these may not be correct. */
388 bool
389 ipa_type_escape_type_contained_p (tree type)
391 if (!initialized)
392 return false;
393 return !bitmap_bit_p (global_types_full_escape,
394 get_canon_type_uid (type, true, false));
397 /* Return true if a modification to a field of type FIELD_TYPE cannot
398 clobber a record of RECORD_TYPE. */
400 bool
401 ipa_type_escape_field_does_not_clobber_p (tree record_type, tree field_type)
403 splay_tree_node result;
404 int uid;
406 if (!initialized)
407 return false;
409 /* Strip off all of the pointer tos on the record type. Strip the
410 same number of pointer tos from the field type. If the field
411 type has fewer, it could not have been aliased. */
412 record_type = TYPE_MAIN_VARIANT (record_type);
413 field_type = TYPE_MAIN_VARIANT (field_type);
414 while (POINTER_TYPE_P (record_type))
416 record_type = TYPE_MAIN_VARIANT (TREE_TYPE (record_type));
417 if (POINTER_TYPE_P (field_type))
418 field_type = TYPE_MAIN_VARIANT (TREE_TYPE (field_type));
419 else
420 /* However, if field_type is a union, this quick test is not
421 correct since one of the variants of the union may be a
422 pointer to type and we cannot see across that here. So we
423 just strip the remaining pointer tos off the record type
424 and fall thru to the more precise code. */
425 if (TREE_CODE (field_type) == QUAL_UNION_TYPE
426 || TREE_CODE (field_type) == UNION_TYPE)
428 while (POINTER_TYPE_P (record_type))
429 record_type = TYPE_MAIN_VARIANT (TREE_TYPE (record_type));
430 break;
432 else
433 return true;
436 record_type = get_canon_type (record_type, true, true);
437 /* The record type must be contained. The field type may
438 escape. */
439 if (!ipa_type_escape_type_contained_p (record_type))
440 return false;
442 uid = TYPE_UID (record_type);
443 result = splay_tree_lookup (uid_to_addressof_down_map, (splay_tree_key) uid);
445 if (result)
447 bitmap field_type_map = (bitmap) result->value;
448 uid = get_canon_type_uid (field_type, true, true);
449 /* If the bit is there, the address was taken. If not, it
450 wasn't. */
451 return !bitmap_bit_p (field_type_map, uid);
453 else
454 /* No bitmap means no addresses were taken. */
455 return true;
459 /* Add TYPE to the suspect type set. Return true if the bit needed to
460 be marked. */
462 static tree
463 mark_type (tree type, enum escape_t escape_status)
465 bitmap map = NULL;
466 int uid;
468 type = get_canon_type (type, true, true);
469 if (!type)
470 return NULL;
472 switch (escape_status)
474 case EXPOSED_PARAMETER:
475 map = global_types_exposed_parameter;
476 break;
477 case FULL_ESCAPE:
478 map = global_types_full_escape;
479 break;
482 uid = TYPE_UID (type);
483 if (bitmap_bit_p (map, uid))
484 return type;
485 else
487 bitmap_set_bit (map, uid);
488 if (escape_status == FULL_ESCAPE)
490 /* Efficiency hack. When things are bad, do not mess around
491 with this type anymore. */
492 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 /* APPLE LOCAL begin 6107012 */
523 static struct pointer_set_t GTY(()) *parent_type_p_pset;
524 /* APPLE LOCAL end 6107012 */
526 static bool
527 parent_type_p (tree parent, tree child)
529 int i;
530 tree binfo, base_binfo;
532 /* APPLE LOCAL begin 6107012 */
533 /* Don't walk the same tree twice. */
534 if (parent_type_p_pset && pointer_set_insert (parent_type_p_pset, parent))
535 return false;
536 /* APPLE LOCAL end 6107012 */
538 if (TYPE_BINFO (parent))
539 for (binfo = TYPE_BINFO (parent), i = 0;
540 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
542 tree binfotype = BINFO_TYPE (base_binfo);
543 if (binfotype == child)
544 return true;
545 else if (parent_type_p (binfotype, child))
546 return true;
548 if (TREE_CODE (parent) == UNION_TYPE
549 || TREE_CODE (parent) == QUAL_UNION_TYPE)
551 tree field;
552 /* Search all of the variants in the union to see if one of them
553 is the child. */
554 for (field = TYPE_FIELDS (parent);
555 field;
556 field = TREE_CHAIN (field))
558 tree field_type;
559 if (TREE_CODE (field) != FIELD_DECL)
560 continue;
562 field_type = TREE_TYPE (field);
563 if (field_type == child)
564 return true;
567 /* If we did not find it, recursively ask the variants if one of
568 their children is the child type. */
569 for (field = TYPE_FIELDS (parent);
570 field;
571 field = TREE_CHAIN (field))
573 tree field_type;
574 if (TREE_CODE (field) != FIELD_DECL)
575 continue;
577 field_type = TREE_TYPE (field);
578 if (TREE_CODE (field_type) == RECORD_TYPE
579 || TREE_CODE (field_type) == QUAL_UNION_TYPE
580 || TREE_CODE (field_type) == UNION_TYPE)
581 if (parent_type_p (field_type, child))
582 return true;
586 if (TREE_CODE (parent) == RECORD_TYPE)
588 tree field;
589 for (field = TYPE_FIELDS (parent);
590 field;
591 field = TREE_CHAIN (field))
593 tree field_type;
594 if (TREE_CODE (field) != FIELD_DECL)
595 continue;
597 field_type = TREE_TYPE (field);
598 if (field_type == child)
599 return true;
600 /* You can only cast to the first field so if it does not
601 match, quit. */
602 if (TREE_CODE (field_type) == RECORD_TYPE
603 || TREE_CODE (field_type) == QUAL_UNION_TYPE
604 || TREE_CODE (field_type) == UNION_TYPE)
606 if (parent_type_p (field_type, child))
607 return true;
608 else
609 break;
613 return false;
616 /* Return the number of pointer tos for TYPE and return TYPE with all
617 of these stripped off. */
619 static int
620 count_stars (tree* type_ptr)
622 tree type = *type_ptr;
623 int i = 0;
624 type = TYPE_MAIN_VARIANT (type);
625 while (POINTER_TYPE_P (type))
627 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
628 i++;
631 *type_ptr = type;
632 return i;
635 enum cast_type {
636 CT_UP,
637 CT_DOWN,
638 CT_SIDEWAYS,
639 CT_USELESS
642 /* Check the cast FROM_TYPE to TO_TYPE. This function requires that
643 the two types have already passed the
644 ipa_type_escape_star_count_of_interesting_type test. */
646 static enum cast_type
647 check_cast_type (tree to_type, tree from_type)
649 int to_stars = count_stars (&to_type);
650 int from_stars = count_stars (&from_type);
651 if (to_stars != from_stars)
652 return CT_SIDEWAYS;
654 if (to_type == from_type)
655 return CT_USELESS;
657 /* APPLE LOCAL 6107012 */
658 parent_type_p_pset = pointer_set_create ();
659 if (parent_type_p (to_type, from_type)) return CT_UP;
660 /* APPLE LOCAL begin 6107012 */
661 pointer_set_destroy (parent_type_p_pset);
662 parent_type_p_pset = pointer_set_create ();
663 /* APPLE LOCAL end 6107012 */
664 if (parent_type_p (from_type, to_type)) return CT_DOWN;
665 /* APPLE LOCAL begin 6107012 */
666 pointer_set_destroy (parent_type_p_pset);
667 parent_type_p_pset = (struct pointer_set_t *)0;
668 /* APPLE LOCAL end 6107012 */
669 return CT_SIDEWAYS;
672 /* Check a cast FROM this variable, TO_TYPE. Mark the escaping types
673 if appropriate. */
674 static void
675 check_cast (tree to_type, tree from)
677 tree from_type = get_canon_type (TREE_TYPE (from), false, false);
678 bool to_interesting_type, from_interesting_type;
680 to_type = get_canon_type (to_type, false, false);
681 if (!from_type || !to_type || from_type == to_type)
682 return;
684 to_interesting_type =
685 ipa_type_escape_star_count_of_interesting_type (to_type) >= 0;
686 from_interesting_type =
687 ipa_type_escape_star_count_of_interesting_type (from_type) >= 0;
689 if (to_interesting_type)
690 if (from_interesting_type)
692 /* Both types are interesting. This can be one of four types
693 of cast: useless, up, down, or sideways. We do not care
694 about up or useless. Sideways casts are always bad and
695 both sides get marked as escaping. Downcasts are not
696 interesting here because if type is marked as escaping, all
697 of its subtypes escape. */
698 switch (check_cast_type (to_type, from_type))
700 case CT_UP:
701 case CT_USELESS:
702 case CT_DOWN:
703 break;
705 case CT_SIDEWAYS:
706 mark_type (to_type, FULL_ESCAPE);
707 mark_type (from_type, FULL_ESCAPE);
708 break;
711 else
713 /* If this is a cast from the local that is a result from a
714 call to malloc, do not mark the cast as bad. */
715 if (DECL_P (from) && !bitmap_bit_p (results_of_malloc, DECL_UID (from)))
716 mark_type (to_type, FULL_ESCAPE);
718 else if (from_interesting_type)
719 mark_type (from_type, FULL_ESCAPE);
722 /* Register the parameter and return types of function FN. The type
723 ESCAPES if the function is visible outside of the compilation
724 unit. */
725 static void
726 check_function_parameter_and_return_types (tree fn, bool escapes)
728 tree arg;
730 if (TYPE_ARG_TYPES (TREE_TYPE (fn)))
732 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
733 arg && TREE_VALUE (arg) != void_type_node;
734 arg = TREE_CHAIN (arg))
736 tree type = get_canon_type (TREE_VALUE (arg), false, false);
737 if (escapes)
738 mark_interesting_type (type, EXPOSED_PARAMETER);
741 else
743 /* FIXME - According to Geoff Keating, we should never have to
744 do this; the front ends should always process the arg list
745 from the TYPE_ARG_LIST. However, Geoff is wrong, this code
746 does seem to be live. */
748 for (arg = DECL_ARGUMENTS (fn); arg; arg = TREE_CHAIN (arg))
750 tree type = get_canon_type (TREE_TYPE (arg), false, false);
751 if (escapes)
752 mark_interesting_type (type, EXPOSED_PARAMETER);
755 if (escapes)
757 tree type = get_canon_type (TREE_TYPE (TREE_TYPE (fn)), false, false);
758 mark_interesting_type (type, EXPOSED_PARAMETER);
762 /* Return true if the variable T is the right kind of static variable to
763 perform compilation unit scope escape analysis. */
765 static inline void
766 has_proper_scope_for_analysis (tree t)
768 /* If the variable has the "used" attribute, treat it as if it had a
769 been touched by the devil. */
770 tree type = get_canon_type (TREE_TYPE (t), false, false);
771 if (!type) return;
773 if (lookup_attribute ("used", DECL_ATTRIBUTES (t)))
775 mark_interesting_type (type, FULL_ESCAPE);
776 return;
779 /* Do not want to do anything with volatile except mark any
780 function that uses one to be not const or pure. */
781 if (TREE_THIS_VOLATILE (t))
782 return;
784 /* Do not care about a local automatic that is not static. */
785 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
786 return;
788 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
790 /* If the front end set the variable to be READONLY and
791 constant, we can allow this variable in pure or const
792 functions but the scope is too large for our analysis to set
793 these bits ourselves. */
795 if (TREE_READONLY (t)
796 && DECL_INITIAL (t)
797 && is_gimple_min_invariant (DECL_INITIAL (t)))
798 ; /* Read of a constant, do not change the function state. */
799 else
801 /* The type escapes for all public and externs. */
802 mark_interesting_type (type, FULL_ESCAPE);
807 /* If T is a VAR_DECL for a static that we are interested in, add the
808 uid to the bitmap. */
810 static void
811 check_operand (tree t)
813 if (!t) return;
815 /* This is an assignment from a function, register the types as
816 escaping. */
817 if (TREE_CODE (t) == FUNCTION_DECL)
818 check_function_parameter_and_return_types (t, true);
820 else if (TREE_CODE (t) == VAR_DECL)
821 has_proper_scope_for_analysis (t);
824 /* Examine tree T for references. */
826 static void
827 check_tree (tree t)
829 if ((TREE_CODE (t) == EXC_PTR_EXPR) || (TREE_CODE (t) == FILTER_EXPR))
830 return;
832 while (TREE_CODE (t) == REALPART_EXPR
833 || TREE_CODE (t) == IMAGPART_EXPR
834 || handled_component_p (t))
836 if (TREE_CODE (t) == ARRAY_REF)
837 check_operand (TREE_OPERAND (t, 1));
838 t = TREE_OPERAND (t, 0);
841 if (INDIRECT_REF_P (t))
842 /* || TREE_CODE (t) == MEM_REF) */
843 check_tree (TREE_OPERAND (t, 0));
845 if (SSA_VAR_P (t) || (TREE_CODE (t) == FUNCTION_DECL))
846 check_operand (t);
849 /* Create an address_of edge FROM_TYPE.TO_TYPE. */
850 static void
851 mark_interesting_addressof (tree to_type, tree from_type)
853 int from_uid;
854 int to_uid;
855 bitmap type_map;
856 splay_tree_node result;
858 from_type = get_canon_type (from_type, false, false);
859 to_type = get_canon_type (to_type, false, false);
861 if (!from_type || !to_type)
862 return;
864 from_uid = TYPE_UID (from_type);
865 to_uid = TYPE_UID (to_type);
867 gcc_assert (ipa_type_escape_star_count_of_interesting_type (from_type) == 0);
869 /* Process the Y into X map pointer. */
870 result = splay_tree_lookup (uid_to_addressof_down_map,
871 (splay_tree_key) from_uid);
873 if (result)
874 type_map = (bitmap) result->value;
875 else
877 type_map = BITMAP_ALLOC (&ipa_obstack);
878 splay_tree_insert (uid_to_addressof_down_map,
879 from_uid,
880 (splay_tree_value)type_map);
882 bitmap_set_bit (type_map, TYPE_UID (to_type));
884 /* Process the X into Y reverse map pointer. */
885 result =
886 splay_tree_lookup (uid_to_addressof_up_map, (splay_tree_key) to_uid);
888 if (result)
889 type_map = (bitmap) result->value;
890 else
892 type_map = BITMAP_ALLOC (&ipa_obstack);
893 splay_tree_insert (uid_to_addressof_up_map,
894 to_uid,
895 (splay_tree_value)type_map);
897 bitmap_set_bit (type_map, TYPE_UID (to_type));
900 /* Scan tree T to see if there are any addresses taken in within T. */
902 static void
903 look_for_address_of (tree t)
905 if (TREE_CODE (t) == ADDR_EXPR)
907 tree x = get_base_var (t);
908 tree cref = TREE_OPERAND (t, 0);
910 /* If we have an expression of the form "&a.b.c.d", mark a.b,
911 b.c and c.d. as having its address taken. */
912 tree fielddecl = NULL_TREE;
913 while (cref!= x)
915 if (TREE_CODE (cref) == COMPONENT_REF)
917 fielddecl = TREE_OPERAND (cref, 1);
918 mark_interesting_addressof (TREE_TYPE (fielddecl),
919 DECL_FIELD_CONTEXT (fielddecl));
921 else if (TREE_CODE (cref) == ARRAY_REF)
922 get_canon_type (TREE_TYPE (cref), false, false);
924 cref = TREE_OPERAND (cref, 0);
927 if (TREE_CODE (x) == VAR_DECL)
928 has_proper_scope_for_analysis (x);
933 /* Scan tree T to see if there are any casts within it.
934 LHS Is the LHS of the expression involving the cast. */
936 static void
937 look_for_casts (tree lhs __attribute__((unused)), tree t)
939 if (is_gimple_cast (t) || TREE_CODE (t) == VIEW_CONVERT_EXPR)
941 tree castfromvar = TREE_OPERAND (t, 0);
942 check_cast (TREE_TYPE (t), castfromvar);
944 else if (TREE_CODE (t) == COMPONENT_REF
945 || TREE_CODE (t) == INDIRECT_REF
946 || TREE_CODE (t) == BIT_FIELD_REF)
948 tree base = get_base_address (t);
949 while (t != base)
951 t = TREE_OPERAND (t, 0);
952 if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
954 /* This may be some part of a component ref.
955 IE it may be a.b.VIEW_CONVERT_EXPR<weird_type>(c).d, AFAIK.
956 castfromref will give you a.b.c, not a. */
957 tree castfromref = TREE_OPERAND (t, 0);
958 check_cast (TREE_TYPE (t), castfromref);
960 else if (TREE_CODE (t) == COMPONENT_REF)
961 get_canon_type (TREE_TYPE (TREE_OPERAND (t, 1)), false, false);
966 /* Check to see if T is a read or address of operation on a static var
967 we are interested in analyzing. */
969 static void
970 check_rhs_var (tree t)
972 look_for_address_of (t);
973 check_tree(t);
976 /* Check to see if T is an assignment to a static var we are
977 interested in analyzing. */
979 static void
980 check_lhs_var (tree t)
982 check_tree(t);
985 /* This is a scaled down version of get_asm_expr_operands from
986 tree_ssa_operands.c. The version there runs much later and assumes
987 that aliasing information is already available. Here we are just
988 trying to find if the set of inputs and outputs contain references
989 or address of operations to local. FN is the function being
990 analyzed and STMT is the actual asm statement. */
992 static void
993 get_asm_expr_operands (tree stmt)
995 int noutputs = list_length (ASM_OUTPUTS (stmt));
996 const char **oconstraints
997 = (const char **) alloca ((noutputs) * sizeof (const char *));
998 int i;
999 tree link;
1000 const char *constraint;
1001 bool allows_mem, allows_reg, is_inout;
1003 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
1005 oconstraints[i] = constraint
1006 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1007 parse_output_constraint (&constraint, i, 0, 0,
1008 &allows_mem, &allows_reg, &is_inout);
1010 check_lhs_var (TREE_VALUE (link));
1013 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1015 constraint
1016 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1017 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1018 oconstraints, &allows_mem, &allows_reg);
1020 check_rhs_var (TREE_VALUE (link));
1023 /* There is no code here to check for asm memory clobbers. The
1024 casual maintainer might think that such code would be necessary,
1025 but that appears to be wrong. In other parts of the compiler,
1026 the asm memory clobbers are assumed to only clobber variables
1027 that are addressable. All types with addressable instances are
1028 assumed to already escape. So, we are protected here. */
1031 /* Check the parameters of a function call to CALL_EXPR to mark the
1032 types that pass across the function boundary. Also check to see if
1033 this is either an indirect call, a call outside the compilation
1034 unit. */
1036 static bool
1037 check_call (tree call_expr)
1039 int flags = call_expr_flags(call_expr);
1040 tree operand_list = TREE_OPERAND (call_expr, 1);
1041 tree operand;
1042 tree callee_t = get_callee_fndecl (call_expr);
1043 tree argument;
1044 struct cgraph_node* callee;
1045 enum availability avail = AVAIL_NOT_AVAILABLE;
1047 for (operand = operand_list;
1048 operand != NULL_TREE;
1049 operand = TREE_CHAIN (operand))
1051 tree argument = TREE_VALUE (operand);
1052 check_rhs_var (argument);
1055 if (callee_t)
1057 tree arg_type;
1058 tree last_arg_type = NULL;
1059 callee = cgraph_node(callee_t);
1060 avail = cgraph_function_body_availability (callee);
1062 /* Check that there are no implicit casts in the passing of
1063 parameters. */
1064 if (TYPE_ARG_TYPES (TREE_TYPE (callee_t)))
1066 operand = operand_list;
1067 for (arg_type = TYPE_ARG_TYPES (TREE_TYPE (callee_t));
1068 arg_type && TREE_VALUE (arg_type) != void_type_node;
1069 arg_type = TREE_CHAIN (arg_type))
1071 if (operand)
1073 argument = TREE_VALUE (operand);
1074 last_arg_type = TREE_VALUE(arg_type);
1075 check_cast (last_arg_type, argument);
1076 operand = TREE_CHAIN (operand);
1078 else
1079 /* The code reaches here for some unfortunate
1080 builtin functions that do not have a list of
1081 argument types. */
1082 break;
1085 else
1087 /* FIXME - According to Geoff Keating, we should never
1088 have to do this; the front ends should always process
1089 the arg list from the TYPE_ARG_LIST. */
1090 operand = operand_list;
1091 for (arg_type = DECL_ARGUMENTS (callee_t);
1092 arg_type;
1093 arg_type = TREE_CHAIN (arg_type))
1095 if (operand)
1097 argument = TREE_VALUE (operand);
1098 last_arg_type = TREE_TYPE(arg_type);
1099 check_cast (last_arg_type, argument);
1100 operand = TREE_CHAIN (operand);
1102 else
1103 /* The code reaches here for some unfortunate
1104 builtin functions that do not have a list of
1105 argument types. */
1106 break;
1110 /* In the case where we have a var_args function, we need to
1111 check the remaining parameters against the last argument. */
1112 arg_type = last_arg_type;
1113 for (;
1114 operand != NULL_TREE;
1115 operand = TREE_CHAIN (operand))
1117 argument = TREE_VALUE (operand);
1118 if (arg_type)
1119 check_cast (arg_type, argument);
1120 else
1122 /* The code reaches here for some unfortunate
1123 builtin functions that do not have a list of
1124 argument types. Most of these functions have
1125 been marked as having their parameters not
1126 escape, but for the rest, the type is doomed. */
1127 tree type = get_canon_type (TREE_TYPE (argument), false, false);
1128 mark_interesting_type (type, FULL_ESCAPE);
1133 /* The callee is either unknown (indirect call) or there is just no
1134 scannable code for it (external call) . We look to see if there
1135 are any bits available for the callee (such as by declaration or
1136 because it is builtin) and process solely on the basis of those
1137 bits. */
1139 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
1141 /* If this is a direct call to an external function, mark all of
1142 the parameter and return types. */
1143 for (operand = operand_list;
1144 operand != NULL_TREE;
1145 operand = TREE_CHAIN (operand))
1147 tree type =
1148 get_canon_type (TREE_TYPE (TREE_VALUE (operand)), false, false);
1149 mark_interesting_type (type, EXPOSED_PARAMETER);
1152 if (callee_t)
1154 tree type =
1155 get_canon_type (TREE_TYPE (TREE_TYPE (callee_t)), false, false);
1156 mark_interesting_type (type, EXPOSED_PARAMETER);
1159 return (flags & ECF_MALLOC);
1162 /* CODE is the operation on OP0 and OP1. OP0 is the operand that we
1163 *know* is a pointer type. OP1 may be a pointer type. */
1164 static bool
1165 okay_pointer_operation (enum tree_code code, tree op0, tree op1)
1167 tree op0type = TYPE_MAIN_VARIANT (TREE_TYPE (op0));
1168 tree op1type = TYPE_MAIN_VARIANT (TREE_TYPE (op1));
1169 if (POINTER_TYPE_P (op1type))
1170 return false;
1171 switch (code)
1173 case MULT_EXPR:
1174 case PLUS_EXPR:
1175 case MINUS_EXPR:
1176 /* TODO: Handle multiples of op0 size as well */
1177 if (operand_equal_p (size_in_bytes (op0type), op1, 0))
1178 return true;
1179 /* fallthrough */
1181 default:
1182 return false;
1184 return false;
1187 /* TP is the part of the tree currently under the microscope.
1188 WALK_SUBTREES is part of the walk_tree api but is unused here.
1189 DATA is cgraph_node of the function being walked. */
1191 /* FIXME: When this is converted to run over SSA form, this code
1192 should be converted to use the operand scanner. */
1194 static tree
1195 scan_for_refs (tree *tp, int *walk_subtrees, void *data)
1197 struct cgraph_node *fn = data;
1198 tree t = *tp;
1200 switch (TREE_CODE (t))
1202 case VAR_DECL:
1203 if (DECL_INITIAL (t))
1204 walk_tree (&DECL_INITIAL (t), scan_for_refs, fn, visited_nodes);
1205 *walk_subtrees = 0;
1206 break;
1208 case MODIFY_EXPR:
1210 /* First look on the lhs and see what variable is stored to */
1211 tree lhs = TREE_OPERAND (t, 0);
1212 tree rhs = TREE_OPERAND (t, 1);
1214 check_lhs_var (lhs);
1215 check_cast (TREE_TYPE (lhs), rhs);
1217 /* For the purposes of figuring out what the cast affects */
1219 /* Next check the operands on the rhs to see if they are ok. */
1220 switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
1222 case tcc_binary:
1224 tree op0 = TREE_OPERAND (rhs, 0);
1225 tree type0 = get_canon_type (TREE_TYPE (op0), false, false);
1226 tree op1 = TREE_OPERAND (rhs, 1);
1227 tree type1 = get_canon_type (TREE_TYPE (op1), false, false);
1229 /* If this is pointer arithmetic of any bad sort, then
1230 we need to mark the types as bad. For binary
1231 operations, no binary operator we currently support
1232 is always "safe" in regard to what it would do to
1233 pointers for purposes of determining which types
1234 escape, except operations of the size of the type.
1235 It is possible that min and max under the right set
1236 of circumstances and if the moon is in the correct
1237 place could be safe, but it is hard to see how this
1238 is worth the effort. */
1240 if (type0 && POINTER_TYPE_P (type0)
1241 && !okay_pointer_operation (TREE_CODE (rhs), op0, op1))
1242 mark_interesting_type (type0, FULL_ESCAPE);
1243 if (type1 && POINTER_TYPE_P (type1)
1244 && !okay_pointer_operation (TREE_CODE (rhs), op1, op0))
1245 mark_interesting_type (type1, FULL_ESCAPE);
1247 look_for_casts (lhs, op0);
1248 look_for_casts (lhs, op1);
1249 check_rhs_var (op0);
1250 check_rhs_var (op1);
1252 break;
1253 case tcc_unary:
1255 tree op0 = TREE_OPERAND (rhs, 0);
1256 tree type0 = get_canon_type (TREE_TYPE (op0), false, false);
1257 /* For unary operations, if the operation is NEGATE or
1258 ABS on a pointer, this is also considered pointer
1259 arithmetic and thus, bad for business. */
1260 if (type0 && (TREE_CODE (op0) == NEGATE_EXPR
1261 || TREE_CODE (op0) == ABS_EXPR)
1262 && POINTER_TYPE_P (type0))
1264 mark_interesting_type (type0, FULL_ESCAPE);
1266 check_rhs_var (op0);
1267 look_for_casts (lhs, op0);
1268 look_for_casts (lhs, rhs);
1271 break;
1272 case tcc_reference:
1273 look_for_casts (lhs, rhs);
1274 check_rhs_var (rhs);
1275 break;
1276 case tcc_declaration:
1277 check_rhs_var (rhs);
1278 break;
1279 case tcc_expression:
1280 switch (TREE_CODE (rhs))
1282 case ADDR_EXPR:
1283 look_for_casts (lhs, TREE_OPERAND (rhs, 0));
1284 check_rhs_var (rhs);
1285 break;
1286 case CALL_EXPR:
1287 /* If this is a call to malloc, squirrel away the
1288 result so we do mark the resulting cast as being
1289 bad. */
1290 if (check_call (rhs))
1291 bitmap_set_bit (results_of_malloc, DECL_UID (lhs));
1292 break;
1293 default:
1294 break;
1296 break;
1297 default:
1298 break;
1300 *walk_subtrees = 0;
1302 break;
1304 case ADDR_EXPR:
1305 /* This case is here to find addresses on rhs of constructors in
1306 decl_initial of static variables. */
1307 check_rhs_var (t);
1308 *walk_subtrees = 0;
1309 break;
1311 case CALL_EXPR:
1312 check_call (t);
1313 *walk_subtrees = 0;
1314 break;
1316 case ASM_EXPR:
1317 get_asm_expr_operands (t);
1318 *walk_subtrees = 0;
1319 break;
1321 default:
1322 break;
1324 return NULL;
1328 /* The init routine for analyzing global static variable usage. See
1329 comments at top for description. */
1330 static void
1331 ipa_init (void)
1333 bitmap_obstack_initialize (&ipa_obstack);
1334 global_types_exposed_parameter = BITMAP_ALLOC (&ipa_obstack);
1335 global_types_full_escape = BITMAP_ALLOC (&ipa_obstack);
1336 global_types_seen = BITMAP_ALLOC (&ipa_obstack);
1337 results_of_malloc = BITMAP_ALLOC (&ipa_obstack);
1339 uid_to_canon_type = splay_tree_new (splay_tree_compare_ints, 0, 0);
1340 all_canon_types = splay_tree_new (compare_type_brand, 0, 0);
1341 type_to_canon_type = splay_tree_new (splay_tree_compare_pointers, 0, 0);
1342 uid_to_subtype_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1343 uid_to_addressof_down_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1344 uid_to_addressof_up_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1346 /* There are some shared nodes, in particular the initializers on
1347 static declarations. We do not need to scan them more than once
1348 since all we would be interested in are the addressof
1349 operations. */
1350 visited_nodes = pointer_set_create ();
1351 initialized = true;
1354 /* Check out the rhs of a static or global initialization VNODE to see
1355 if any of them contain addressof operations. Note that some of
1356 these variables may not even be referenced in the code in this
1357 compilation unit but their right hand sides may contain references
1358 to variables defined within this unit. */
1360 static void
1361 analyze_variable (struct cgraph_varpool_node *vnode)
1363 tree global = vnode->decl;
1364 tree type = get_canon_type (TREE_TYPE (global), false, false);
1366 /* If this variable has exposure beyond the compilation unit, add
1367 its type to the global types. */
1369 if (vnode->externally_visible)
1370 mark_interesting_type (type, FULL_ESCAPE);
1372 gcc_assert (TREE_CODE (global) == VAR_DECL);
1374 if (DECL_INITIAL (global))
1375 walk_tree (&DECL_INITIAL (global), scan_for_refs, NULL, visited_nodes);
1378 /* This is the main routine for finding the reference patterns for
1379 global variables within a function FN. */
1381 static void
1382 analyze_function (struct cgraph_node *fn)
1384 tree decl = fn->decl;
1385 check_function_parameter_and_return_types (decl,
1386 fn->local.externally_visible);
1387 if (dump_file)
1388 fprintf (dump_file, "\n local analysis of %s", cgraph_node_name (fn));
1391 struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
1392 basic_block this_block;
1394 FOR_EACH_BB_FN (this_block, this_cfun)
1396 block_stmt_iterator bsi;
1397 for (bsi = bsi_start (this_block); !bsi_end_p (bsi); bsi_next (&bsi))
1398 walk_tree (bsi_stmt_ptr (bsi), scan_for_refs,
1399 fn, visited_nodes);
1403 /* There may be const decls with interesting right hand sides. */
1404 if (DECL_STRUCT_FUNCTION (decl))
1406 tree step;
1407 for (step = DECL_STRUCT_FUNCTION (decl)->unexpanded_var_list;
1408 step;
1409 step = TREE_CHAIN (step))
1411 tree var = TREE_VALUE (step);
1412 if (TREE_CODE (var) == VAR_DECL
1413 && DECL_INITIAL (var)
1414 && !TREE_STATIC (var))
1415 walk_tree (&DECL_INITIAL (var), scan_for_refs,
1416 fn, visited_nodes);
1417 get_canon_type (TREE_TYPE (var), false, false);
1424 /* Convert a type_UID into a type. */
1425 static tree
1426 type_for_uid (int uid)
1428 splay_tree_node result =
1429 splay_tree_lookup (uid_to_canon_type, (splay_tree_key) uid);
1431 if (result)
1432 return (tree) result->value;
1433 else return NULL;
1436 /* Return the a bitmap with the subtypes of the type for UID. If it
1437 does not exist, return either NULL or a new bitmap depending on the
1438 value of CREATE. */
1440 static bitmap
1441 subtype_map_for_uid (int uid, bool create)
1443 splay_tree_node result = splay_tree_lookup (uid_to_subtype_map,
1444 (splay_tree_key) uid);
1446 if (result)
1447 return (bitmap) result->value;
1448 else if (create)
1450 bitmap subtype_map = BITMAP_ALLOC (&ipa_obstack);
1451 splay_tree_insert (uid_to_subtype_map,
1452 uid,
1453 (splay_tree_value)subtype_map);
1454 return subtype_map;
1456 else return NULL;
1459 /* Mark all of the supertypes and field types of TYPE as being seen.
1460 Also accumulate the subtypes for each type so that
1461 close_types_full_escape can mark a subtype as escaping if the
1462 supertype escapes. */
1464 static void
1465 close_type_seen (tree type)
1467 tree field;
1468 int i, uid;
1469 tree binfo, base_binfo;
1471 /* See thru all pointer tos and array ofs. */
1472 type = get_canon_type (type, true, true);
1473 if (!type)
1474 return;
1476 uid = TYPE_UID (type);
1478 if (bitmap_bit_p (been_there_done_that, uid))
1479 return;
1480 bitmap_set_bit (been_there_done_that, uid);
1482 /* If we are doing a language with a type hierarchy, mark all of
1483 the superclasses. */
1484 if (TYPE_BINFO (type))
1485 for (binfo = TYPE_BINFO (type), i = 0;
1486 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1488 tree binfo_type = BINFO_TYPE (base_binfo);
1489 bitmap subtype_map = subtype_map_for_uid
1490 (TYPE_UID (TYPE_MAIN_VARIANT (binfo_type)), true);
1491 bitmap_set_bit (subtype_map, uid);
1492 close_type_seen (get_canon_type (binfo_type, true, true));
1495 /* If the field is a struct or union type, mark all of the
1496 subfields. */
1497 for (field = TYPE_FIELDS (type);
1498 field;
1499 field = TREE_CHAIN (field))
1501 tree field_type;
1502 if (TREE_CODE (field) != FIELD_DECL)
1503 continue;
1505 field_type = TREE_TYPE (field);
1506 if (ipa_type_escape_star_count_of_interesting_or_array_type (field_type) >= 0)
1507 close_type_seen (get_canon_type (field_type, true, true));
1511 /* Take a TYPE that has been passed by value to an external function
1512 and mark all of the fields that have pointer types as escaping. For
1513 any of the non pointer types that are structures or unions,
1514 recurse. TYPE is never a pointer type. */
1516 static void
1517 close_type_exposed_parameter (tree type)
1519 tree field;
1520 int uid;
1522 type = get_canon_type (type, false, false);
1523 if (!type)
1524 return;
1525 uid = TYPE_UID (type);
1526 gcc_assert (!POINTER_TYPE_P (type));
1528 if (bitmap_bit_p (been_there_done_that, uid))
1529 return;
1530 bitmap_set_bit (been_there_done_that, uid);
1532 /* If the field is a struct or union type, mark all of the
1533 subfields. */
1534 for (field = TYPE_FIELDS (type);
1535 field;
1536 field = TREE_CHAIN (field))
1538 tree field_type;
1540 if (TREE_CODE (field) != FIELD_DECL)
1541 continue;
1543 field_type = get_canon_type (TREE_TYPE (field), false, false);
1544 mark_interesting_type (field_type, EXPOSED_PARAMETER);
1546 /* Only recurse for non pointer types of structures and unions. */
1547 if (ipa_type_escape_star_count_of_interesting_type (field_type) == 0)
1548 close_type_exposed_parameter (field_type);
1552 /* The next function handles the case where a type fully escapes.
1553 This means that not only does the type itself escape,
1555 a) the type of every field recursively escapes
1556 b) the type of every subtype escapes as well as the super as well
1557 as all of the pointer to types for each field.
1559 Note that pointer to types are not marked as escaping. If the
1560 pointed to type escapes, the pointer to type also escapes.
1562 Take a TYPE that has had the address taken for an instance of it
1563 and mark all of the types for its fields as having their addresses
1564 taken. */
1566 static void
1567 close_type_full_escape (tree type)
1569 tree field;
1570 unsigned int i;
1571 int uid;
1572 tree binfo, base_binfo;
1573 bitmap_iterator bi;
1574 bitmap subtype_map;
1575 splay_tree_node address_result;
1577 /* Strip off any pointer or array types. */
1578 type = get_canon_type (type, true, true);
1579 if (!type)
1580 return;
1581 uid = TYPE_UID (type);
1583 if (bitmap_bit_p (been_there_done_that, uid))
1584 return;
1585 bitmap_set_bit (been_there_done_that, uid);
1587 subtype_map = subtype_map_for_uid (uid, false);
1589 /* If we are doing a language with a type hierarchy, mark all of
1590 the superclasses. */
1591 if (TYPE_BINFO (type))
1592 for (binfo = TYPE_BINFO (type), i = 0;
1593 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1595 tree binfotype = BINFO_TYPE (base_binfo);
1596 binfotype = mark_type (binfotype, FULL_ESCAPE);
1597 close_type_full_escape (binfotype);
1600 /* Mark as escaped any types that have been down casted to
1601 this type. */
1602 if (subtype_map)
1603 EXECUTE_IF_SET_IN_BITMAP (subtype_map, 0, i, bi)
1605 tree subtype = type_for_uid (i);
1606 subtype = mark_type (subtype, FULL_ESCAPE);
1607 close_type_full_escape (subtype);
1610 /* If the field is a struct or union type, mark all of the
1611 subfields. */
1612 for (field = TYPE_FIELDS (type);
1613 field;
1614 field = TREE_CHAIN (field))
1616 tree field_type;
1617 if (TREE_CODE (field) != FIELD_DECL)
1618 continue;
1620 field_type = TREE_TYPE (field);
1621 if (ipa_type_escape_star_count_of_interesting_or_array_type (field_type) >= 0)
1623 field_type = mark_type (field_type, FULL_ESCAPE);
1624 close_type_full_escape (field_type);
1628 /* For all of the types A that contain this type B and were part of
1629 an expression like "&...A.B...", mark the A's as escaping. */
1630 address_result = splay_tree_lookup (uid_to_addressof_up_map,
1631 (splay_tree_key) uid);
1632 if (address_result)
1634 bitmap containing_classes = (bitmap) address_result->value;
1635 EXECUTE_IF_SET_IN_BITMAP (containing_classes, 0, i, bi)
1637 close_type_full_escape (type_for_uid (i));
1642 /* Transitively close the addressof bitmap for the type with UID.
1643 This means that if we had a.b and b.c, a would have both b and c in
1644 its maps. */
1646 static bitmap
1647 close_addressof_down (int uid)
1649 bitmap_iterator bi;
1650 splay_tree_node result =
1651 splay_tree_lookup (uid_to_addressof_down_map, (splay_tree_key) uid);
1652 bitmap map = NULL;
1653 bitmap new_map;
1654 unsigned int i;
1656 if (result)
1657 map = (bitmap) result->value;
1658 else
1659 return NULL;
1661 if (bitmap_bit_p (been_there_done_that, uid))
1662 return map;
1663 bitmap_set_bit (been_there_done_that, uid);
1665 /* If the type escapes, get rid of the addressof map, it will not be
1666 needed. */
1667 if (bitmap_bit_p (global_types_full_escape, uid))
1669 BITMAP_FREE (map);
1670 splay_tree_remove (uid_to_addressof_down_map, (splay_tree_key) uid);
1671 return NULL;
1674 /* The new_map will have all of the bits for the enclosed fields and
1675 will have the unique id version of the old map. */
1676 new_map = BITMAP_ALLOC (&ipa_obstack);
1678 EXECUTE_IF_SET_IN_BITMAP (map, 0, i, bi)
1680 bitmap submap = close_addressof_down (i);
1681 bitmap_set_bit (new_map, i);
1682 if (submap)
1683 bitmap_ior_into (new_map, submap);
1685 result->value = (splay_tree_value) new_map;
1687 BITMAP_FREE (map);
1688 return new_map;
1692 /* The main entry point for type escape analysis. */
1694 static unsigned int
1695 type_escape_execute (void)
1697 struct cgraph_node *node;
1698 struct cgraph_varpool_node *vnode;
1699 unsigned int i;
1700 bitmap_iterator bi;
1701 splay_tree_node result;
1703 ipa_init ();
1705 /* Process all of the variables first. */
1706 for (vnode = cgraph_varpool_nodes_queue; vnode; vnode = vnode->next_needed)
1707 analyze_variable (vnode);
1709 /* Process all of the functions. next
1711 We do not want to process any of the clones so we check that this
1712 is a master clone. However, we do need to process any
1713 AVAIL_OVERWRITABLE functions (these are never clones) because
1714 they may cause a type variable to escape.
1716 for (node = cgraph_nodes; node; node = node->next)
1717 if (node->analyzed
1718 && (cgraph_is_master_clone (node)
1719 || (cgraph_function_body_availability (node) == AVAIL_OVERWRITABLE)))
1720 analyze_function (node);
1723 pointer_set_destroy (visited_nodes);
1724 visited_nodes = NULL;
1726 /* Do all of the closures to discover which types escape the
1727 compilation unit. */
1729 been_there_done_that = BITMAP_ALLOC (&ipa_obstack);
1730 bitmap_tmp = BITMAP_ALLOC (&ipa_obstack);
1732 /* Examine the types that we have directly seen in scanning the code
1733 and add to that any contained types or superclasses. */
1735 bitmap_copy (bitmap_tmp, global_types_seen);
1736 EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
1738 tree type = type_for_uid (i);
1739 /* Only look at records and unions and pointer tos. */
1740 if (ipa_type_escape_star_count_of_interesting_or_array_type (type) >= 0)
1741 close_type_seen (type);
1743 bitmap_clear (been_there_done_that);
1745 /* Examine all of the types passed by value and mark any enclosed
1746 pointer types as escaping. */
1747 bitmap_copy (bitmap_tmp, global_types_exposed_parameter);
1748 EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
1750 close_type_exposed_parameter (type_for_uid (i));
1752 bitmap_clear (been_there_done_that);
1754 /* Close the types for escape. If something escapes, then any
1755 enclosed types escape as well as any subtypes. */
1756 bitmap_copy (bitmap_tmp, global_types_full_escape);
1757 EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
1759 close_type_full_escape (type_for_uid (i));
1761 bitmap_clear (been_there_done_that);
1763 /* Before this pass, the uid_to_addressof_down_map for type X
1764 contained an entry for Y if there had been an operation of the
1765 form &X.Y. This step adds all of the fields contained within Y
1766 (recursively) to X's map. */
1768 result = splay_tree_min (uid_to_addressof_down_map);
1769 while (result)
1771 int uid = result->key;
1772 /* Close the addressof map, i.e. copy all of the transitive
1773 substructures up to this level. */
1774 close_addressof_down (uid);
1775 result = splay_tree_successor (uid_to_addressof_down_map, uid);
1778 /* Do not need the array types and pointer types in the persistent
1779 data structures. */
1780 result = splay_tree_min (all_canon_types);
1781 while (result)
1783 tree type = (tree) result->value;
1784 tree key = (tree) result->key;
1785 if (POINTER_TYPE_P (type)
1786 || TREE_CODE (type) == ARRAY_TYPE)
1788 splay_tree_remove (all_canon_types, (splay_tree_key) result->key);
1789 splay_tree_remove (type_to_canon_type, (splay_tree_key) type);
1790 splay_tree_remove (uid_to_canon_type, (splay_tree_key) TYPE_UID (type));
1791 bitmap_clear_bit (global_types_seen, TYPE_UID (type));
1793 result = splay_tree_successor (all_canon_types, (splay_tree_key) key);
1796 if (dump_file)
1798 EXECUTE_IF_SET_IN_BITMAP (global_types_seen, 0, i, bi)
1800 /* The pointer types are in the global_types_full_escape
1801 bitmap but not in the backwards map. They also contain
1802 no useful information since they are not marked. */
1803 tree type = type_for_uid (i);
1804 fprintf(dump_file, "type %d ", i);
1805 print_generic_expr (dump_file, type, 0);
1806 if (bitmap_bit_p (global_types_full_escape, i))
1807 fprintf(dump_file, " escaped\n");
1808 else
1809 fprintf(dump_file, " contained\n");
1813 /* Get rid of uid_to_addressof_up_map and its bitmaps. */
1814 result = splay_tree_min (uid_to_addressof_up_map);
1815 while (result)
1817 int uid = (int)result->key;
1818 bitmap bm = (bitmap)result->value;
1820 BITMAP_FREE (bm);
1821 splay_tree_remove (uid_to_addressof_up_map, (splay_tree_key) uid);
1822 result = splay_tree_successor (uid_to_addressof_up_map, uid);
1825 /* Get rid of the subtype map. */
1826 result = splay_tree_min (uid_to_subtype_map);
1827 while (result)
1829 bitmap b = (bitmap)result->value;
1830 BITMAP_FREE(b);
1831 splay_tree_remove (uid_to_subtype_map, result->key);
1832 result = splay_tree_min (uid_to_subtype_map);
1834 splay_tree_delete (uid_to_subtype_map);
1835 uid_to_subtype_map = NULL;
1837 BITMAP_FREE (global_types_exposed_parameter);
1838 BITMAP_FREE (been_there_done_that);
1839 BITMAP_FREE (bitmap_tmp);
1840 BITMAP_FREE (results_of_malloc);
1841 return 0;
1844 static bool
1845 gate_type_escape_vars (void)
1847 return (flag_unit_at_a_time != 0 && flag_ipa_type_escape
1848 /* Don't bother doing anything if the program has errors. */
1849 && !(errorcount || sorrycount));
1852 struct tree_opt_pass pass_ipa_type_escape =
1854 "type-escape-var", /* name */
1855 gate_type_escape_vars, /* gate */
1856 type_escape_execute, /* execute */
1857 NULL, /* sub */
1858 NULL, /* next */
1859 0, /* static_pass_number */
1860 TV_IPA_TYPE_ESCAPE, /* tv_id */
1861 0, /* properties_required */
1862 0, /* properties_provided */
1863 0, /* properties_destroyed */
1864 0, /* todo_flags_start */
1865 0, /* todo_flags_finish */
1866 0 /* letter */