Merge with trank @ 137446
[official-gcc.git] / gcc / ipa-type-escape.c
blob229d8b2e9a56495d899c929bf2934afd6f39a1d8
1 /* Type based alias analysis.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation,
3 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 "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 /* Scratch bitmap for avoiding work. */
64 static bitmap been_there_done_that;
65 static bitmap bitmap_tmp;
67 /* There are two levels of escape that types can undergo.
69 EXPOSED_PARAMETER - some instance of the variable is
70 passed by value into an externally visible function or some
71 instance of the variable is passed out of an externally visible
72 function as a return value. In this case any of the fields of the
73 variable that are pointer types end up having their types marked as
74 FULL_ESCAPE.
76 FULL_ESCAPE - when bad things happen to good types. One of the
77 following things happens to the type: (a) either an instance of the
78 variable has its address passed to an externally visible function,
79 (b) the address is taken and some bad cast happens to the address
80 or (c) explicit arithmetic is done to the address.
83 enum escape_t
85 EXPOSED_PARAMETER,
86 FULL_ESCAPE
89 /* The following two bit vectors global_types_* correspond to
90 previous cases above. During the analysis phase, a bit is set in
91 one of these vectors if an operation of the offending class is
92 discovered to happen on the associated type. */
94 static bitmap global_types_exposed_parameter;
95 static bitmap global_types_full_escape;
97 /* All of the types seen in this compilation unit. */
98 static bitmap global_types_seen;
99 /* Reverse map to take a canon uid and map it to a canon type. Uid's
100 are never manipulated unless they are associated with a canon
101 type. */
102 static splay_tree uid_to_canon_type;
104 /* Internal structure of type mapping code. This maps a canon type
105 name to its canon type. */
106 static splay_tree all_canon_types;
108 /* Map from type clones to the single canon type. */
109 static splay_tree type_to_canon_type;
111 /* A splay tree of bitmaps. An element X in the splay tree has a bit
112 set in its bitmap at TYPE_UID (TYPE_MAIN_VARIANT (Y)) if there was
113 an operation in the program of the form "&X.Y". */
114 static splay_tree uid_to_addressof_down_map;
116 /* A splay tree of bitmaps. An element Y in the splay tree has a bit
117 set in its bitmap at TYPE_UID (TYPE_MAIN_VARIANT (X)) if there was
118 an operation in the program of the form "&X.Y". */
119 static splay_tree uid_to_addressof_up_map;
121 /* Tree to hold the subtype maps used to mark subtypes of escaped
122 types. */
123 static splay_tree uid_to_subtype_map;
125 /* Records tree nodes seen in cgraph_create_edges. Simply using
126 walk_tree_without_duplicates doesn't guarantee each node is visited
127 once because it gets a new htab upon each recursive call from
128 scan_for_refs. */
129 static struct pointer_set_t *visited_nodes;
131 /* Visited stmts by walk_use_def_chains function because it's called
132 recursively. */
133 static struct pointer_set_t *visited_stmts;
135 static bitmap_obstack ipa_obstack;
137 /* Static functions from this file that are used
138 before being defined. */
139 static unsigned int look_for_casts (tree lhs ATTRIBUTE_UNUSED, tree);
140 static bool is_cast_from_non_pointer (tree, tree, void *);
142 /* Get the name of TYPE or return the string "<UNNAMED>". */
143 static const char*
144 get_name_of_type (tree type)
146 tree name = TYPE_NAME (type);
148 if (!name)
149 /* Unnamed type, do what you like here. */
150 return "<UNNAMED>";
152 /* It will be a TYPE_DECL in the case of a typedef, otherwise, an
153 identifier_node */
154 if (TREE_CODE (name) == TYPE_DECL)
156 /* Each DECL has a DECL_NAME field which contains an
157 IDENTIFIER_NODE. (Some decls, most often labels, may have
158 zero as the DECL_NAME). */
159 if (DECL_NAME (name))
160 return IDENTIFIER_POINTER (DECL_NAME (name));
161 else
162 /* Unnamed type, do what you like here. */
163 return "<UNNAMED>";
165 else if (TREE_CODE (name) == IDENTIFIER_NODE)
166 return IDENTIFIER_POINTER (name);
167 else
168 return "<UNNAMED>";
171 struct type_brand_s
173 const char* name;
174 int seq;
177 /* Splay tree comparison function on type_brand_s structures. */
179 static int
180 compare_type_brand (splay_tree_key sk1, splay_tree_key sk2)
182 struct type_brand_s * k1 = (struct type_brand_s *) sk1;
183 struct type_brand_s * k2 = (struct type_brand_s *) sk2;
185 int value = strcmp(k1->name, k2->name);
186 if (value == 0)
187 return k2->seq - k1->seq;
188 else
189 return value;
192 /* All of the "unique_type" code is a hack to get around the sleazy
193 implementation used to compile more than file. Currently gcc does
194 not get rid of multiple instances of the same type that have been
195 collected from different compilation units. */
196 /* This is a trivial algorithm for removing duplicate types. This
197 would not work for any language that used structural equivalence as
198 the basis of its type system. */
199 /* Return TYPE if no type compatible with TYPE has been seen so far,
200 otherwise return a type compatible with TYPE that has already been
201 processed. */
203 static tree
204 discover_unique_type (tree type)
206 struct type_brand_s * brand = XNEW (struct type_brand_s);
207 int i = 0;
208 splay_tree_node result;
210 brand->name = get_name_of_type (type);
212 while (1)
214 brand->seq = i++;
215 result = splay_tree_lookup (all_canon_types, (splay_tree_key) brand);
217 if (result)
219 /* Create an alias since this is just the same as
220 other_type. */
221 tree other_type = (tree) result->value;
222 if (types_compatible_p (type, other_type))
224 free (brand);
225 /* Insert this new type as an alias for other_type. */
226 splay_tree_insert (type_to_canon_type,
227 (splay_tree_key) type,
228 (splay_tree_value) other_type);
229 return other_type;
231 /* Not compatible, look for next instance with same name. */
233 else
235 /* No more instances, create new one since this is the first
236 time we saw this type. */
237 brand->seq = i++;
238 /* Insert the new brand. */
239 splay_tree_insert (all_canon_types,
240 (splay_tree_key) brand,
241 (splay_tree_value) type);
243 /* Insert this new type as an alias for itself. */
244 splay_tree_insert (type_to_canon_type,
245 (splay_tree_key) type,
246 (splay_tree_value) type);
248 /* Insert the uid for reverse lookup; */
249 splay_tree_insert (uid_to_canon_type,
250 (splay_tree_key) TYPE_UID (type),
251 (splay_tree_value) type);
253 bitmap_set_bit (global_types_seen, TYPE_UID (type));
254 return type;
259 /* Return true if TYPE is one of the type classes that we are willing
260 to analyze. This skips the goofy types like arrays of pointers to
261 methods. */
262 static bool
263 type_to_consider (tree type)
265 /* Strip the *'s off. */
266 type = TYPE_MAIN_VARIANT (type);
267 while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
268 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
270 switch (TREE_CODE (type))
272 case BOOLEAN_TYPE:
273 case COMPLEX_TYPE:
274 case ENUMERAL_TYPE:
275 case INTEGER_TYPE:
276 case QUAL_UNION_TYPE:
277 case REAL_TYPE:
278 case FIXED_POINT_TYPE:
279 case RECORD_TYPE:
280 case UNION_TYPE:
281 case VECTOR_TYPE:
282 case VOID_TYPE:
283 return true;
285 default:
286 return false;
290 /* Get the canon type of TYPE. If SEE_THRU_PTRS is true, remove all
291 the POINTER_TOs and if SEE_THRU_ARRAYS is true, remove all of the
292 ARRAY_OFs and POINTER_TOs. */
294 static tree
295 get_canon_type (tree type, bool see_thru_ptrs, bool see_thru_arrays)
297 splay_tree_node result;
298 /* Strip the *'s off. */
299 if (!type || !type_to_consider (type))
300 return NULL;
302 type = TYPE_MAIN_VARIANT (type);
303 if (see_thru_arrays)
304 while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
305 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
307 else if (see_thru_ptrs)
308 while (POINTER_TYPE_P (type))
309 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
311 result = splay_tree_lookup(type_to_canon_type, (splay_tree_key) type);
313 if (result == NULL)
314 return discover_unique_type (type);
315 else return (tree) result->value;
318 /* Same as GET_CANON_TYPE, except return the TYPE_ID rather than the
319 TYPE. */
321 static int
322 get_canon_type_uid (tree type, bool see_thru_ptrs, bool see_thru_arrays)
324 type = get_canon_type (type, see_thru_ptrs, see_thru_arrays);
325 if (type)
326 return TYPE_UID(type);
327 else return 0;
330 /* Return 0 if TYPE is a record or union type. Return a positive
331 number if TYPE is a pointer to a record or union. The number is
332 the number of pointer types stripped to get to the record or union
333 type. Return -1 if TYPE is none of the above. */
336 ipa_type_escape_star_count_of_interesting_type (tree type)
338 int count = 0;
339 /* Strip the *'s off. */
340 if (!type)
341 return -1;
342 type = TYPE_MAIN_VARIANT (type);
343 while (POINTER_TYPE_P (type))
345 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
346 count++;
349 /* We are interested in records, and unions only. */
350 if (TREE_CODE (type) == RECORD_TYPE
351 || TREE_CODE (type) == QUAL_UNION_TYPE
352 || TREE_CODE (type) == UNION_TYPE)
353 return count;
354 else
355 return -1;
359 /* Return 0 if TYPE is a record or union type. Return a positive
360 number if TYPE is a pointer to a record or union. The number is
361 the number of pointer types stripped to get to the record or union
362 type. Return -1 if TYPE is none of the above. */
365 ipa_type_escape_star_count_of_interesting_or_array_type (tree type)
367 int count = 0;
368 /* Strip the *'s off. */
369 if (!type)
370 return -1;
371 type = TYPE_MAIN_VARIANT (type);
372 while (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
374 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
375 count++;
378 /* We are interested in records, and unions only. */
379 if (TREE_CODE (type) == RECORD_TYPE
380 || TREE_CODE (type) == QUAL_UNION_TYPE
381 || TREE_CODE (type) == UNION_TYPE)
382 return count;
383 else
384 return -1;
388 /* Return true if the record, or union TYPE passed in escapes this
389 compilation unit. Note that all of the pointer-to's are removed
390 before testing since these may not be correct. */
392 bool
393 ipa_type_escape_type_contained_p (tree type)
395 if (!initialized)
396 return false;
397 return !bitmap_bit_p (global_types_full_escape,
398 get_canon_type_uid (type, true, false));
401 /* Return true if a modification to a field of type FIELD_TYPE cannot
402 clobber a record of RECORD_TYPE. */
404 bool
405 ipa_type_escape_field_does_not_clobber_p (tree record_type, tree field_type)
407 splay_tree_node result;
408 int uid;
410 if (!initialized)
411 return false;
413 /* Strip off all of the pointer tos on the record type. Strip the
414 same number of pointer tos from the field type. If the field
415 type has fewer, it could not have been aliased. */
416 record_type = TYPE_MAIN_VARIANT (record_type);
417 field_type = TYPE_MAIN_VARIANT (field_type);
418 while (POINTER_TYPE_P (record_type))
420 record_type = TYPE_MAIN_VARIANT (TREE_TYPE (record_type));
421 if (POINTER_TYPE_P (field_type))
422 field_type = TYPE_MAIN_VARIANT (TREE_TYPE (field_type));
423 else
424 /* However, if field_type is a union, this quick test is not
425 correct since one of the variants of the union may be a
426 pointer to type and we cannot see across that here. So we
427 just strip the remaining pointer tos off the record type
428 and fall thru to the more precise code. */
429 if (TREE_CODE (field_type) == QUAL_UNION_TYPE
430 || TREE_CODE (field_type) == UNION_TYPE)
432 while (POINTER_TYPE_P (record_type))
433 record_type = TYPE_MAIN_VARIANT (TREE_TYPE (record_type));
434 break;
436 else
437 return true;
440 record_type = get_canon_type (record_type, true, true);
441 /* The record type must be contained. The field type may
442 escape. */
443 if (!ipa_type_escape_type_contained_p (record_type))
444 return false;
446 uid = TYPE_UID (record_type);
447 result = splay_tree_lookup (uid_to_addressof_down_map, (splay_tree_key) uid);
449 if (result)
451 bitmap field_type_map = (bitmap) result->value;
452 uid = get_canon_type_uid (field_type, true, true);
453 /* If the bit is there, the address was taken. If not, it
454 wasn't. */
455 return !bitmap_bit_p (field_type_map, uid);
457 else
458 /* No bitmap means no addresses were taken. */
459 return true;
463 /* Add TYPE to the suspect type set. Return true if the bit needed to
464 be marked. */
466 static tree
467 mark_type (tree type, enum escape_t escape_status)
469 bitmap map = NULL;
470 int uid;
472 type = get_canon_type (type, true, true);
473 if (!type)
474 return NULL;
476 switch (escape_status)
478 case EXPOSED_PARAMETER:
479 map = global_types_exposed_parameter;
480 break;
481 case FULL_ESCAPE:
482 map = global_types_full_escape;
483 break;
486 uid = TYPE_UID (type);
487 if (bitmap_bit_p (map, uid))
488 return type;
489 else
491 bitmap_set_bit (map, uid);
492 if (escape_status == FULL_ESCAPE)
494 /* Efficiency hack. When things are bad, do not mess around
495 with this type anymore. */
496 bitmap_set_bit (global_types_exposed_parameter, uid);
499 return type;
502 /* Add interesting TYPE to the suspect type set. If the set is
503 EXPOSED_PARAMETER and the TYPE is a pointer type, the set is
504 changed to FULL_ESCAPE. */
506 static void
507 mark_interesting_type (tree type, enum escape_t escape_status)
509 if (!type) return;
510 if (ipa_type_escape_star_count_of_interesting_type (type) >= 0)
512 if ((escape_status == EXPOSED_PARAMETER)
513 && POINTER_TYPE_P (type))
514 /* EXPOSED_PARAMETERs are only structs or unions are passed by
515 value. Anything passed by reference to an external
516 function fully exposes the type. */
517 mark_type (type, FULL_ESCAPE);
518 else
519 mark_type (type, escape_status);
523 /* Return true if PARENT is supertype of CHILD. Both types must be
524 known to be structures or unions. */
526 static bool
527 parent_type_p (tree parent, tree child)
529 int i;
530 tree binfo, base_binfo;
531 if (TYPE_BINFO (parent))
532 for (binfo = TYPE_BINFO (parent), i = 0;
533 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
535 tree binfotype = BINFO_TYPE (base_binfo);
536 if (binfotype == child)
537 return true;
538 else if (parent_type_p (binfotype, child))
539 return true;
541 if (TREE_CODE (parent) == UNION_TYPE
542 || TREE_CODE (parent) == QUAL_UNION_TYPE)
544 tree field;
545 /* Search all of the variants in the union to see if one of them
546 is the child. */
547 for (field = TYPE_FIELDS (parent);
548 field;
549 field = TREE_CHAIN (field))
551 tree field_type;
552 if (TREE_CODE (field) != FIELD_DECL)
553 continue;
555 field_type = TREE_TYPE (field);
556 if (field_type == child)
557 return true;
560 /* If we did not find it, recursively ask the variants if one of
561 their children is the child type. */
562 for (field = TYPE_FIELDS (parent);
563 field;
564 field = TREE_CHAIN (field))
566 tree field_type;
567 if (TREE_CODE (field) != FIELD_DECL)
568 continue;
570 field_type = TREE_TYPE (field);
571 if (TREE_CODE (field_type) == RECORD_TYPE
572 || TREE_CODE (field_type) == QUAL_UNION_TYPE
573 || TREE_CODE (field_type) == UNION_TYPE)
574 if (parent_type_p (field_type, child))
575 return true;
579 if (TREE_CODE (parent) == RECORD_TYPE)
581 tree field;
582 for (field = TYPE_FIELDS (parent);
583 field;
584 field = TREE_CHAIN (field))
586 tree field_type;
587 if (TREE_CODE (field) != FIELD_DECL)
588 continue;
590 field_type = TREE_TYPE (field);
591 if (field_type == child)
592 return true;
593 /* You can only cast to the first field so if it does not
594 match, quit. */
595 if (TREE_CODE (field_type) == RECORD_TYPE
596 || TREE_CODE (field_type) == QUAL_UNION_TYPE
597 || TREE_CODE (field_type) == UNION_TYPE)
599 if (parent_type_p (field_type, child))
600 return true;
601 else
602 break;
606 return false;
609 /* Return the number of pointer tos for TYPE and return TYPE with all
610 of these stripped off. */
612 static int
613 count_stars (tree* type_ptr)
615 tree type = *type_ptr;
616 int i = 0;
617 type = TYPE_MAIN_VARIANT (type);
618 while (POINTER_TYPE_P (type))
620 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
621 i++;
624 *type_ptr = type;
625 return i;
628 enum cast_type {
629 CT_UP = 0x1,
630 CT_DOWN = 0x2,
631 CT_SIDEWAYS = 0x4,
632 CT_USELESS = 0x8,
633 CT_FROM_P_BAD = 0x10,
634 CT_FROM_NON_P = 0x20,
635 CT_TO_NON_INTER = 0x40,
636 CT_FROM_MALLOC = 0x80,
637 CT_NO_CAST = 0x100
640 /* Check the cast FROM_TYPE to TO_TYPE. This function requires that
641 the two types have already passed the
642 ipa_type_escape_star_count_of_interesting_type test. */
644 static enum cast_type
645 check_cast_type (tree to_type, tree from_type)
647 int to_stars = count_stars (&to_type);
648 int from_stars = count_stars (&from_type);
649 if (to_stars != from_stars)
650 return CT_SIDEWAYS;
652 if (to_type == from_type)
653 return CT_USELESS;
655 if (parent_type_p (to_type, from_type)) return CT_UP;
656 if (parent_type_p (from_type, to_type)) return CT_DOWN;
657 return CT_SIDEWAYS;
660 /* This function returns nonzero if VAR is result of call
661 to malloc function. */
663 static bool
664 is_malloc_result (tree var)
666 tree def_stmt;
667 tree rhs;
668 int flags;
670 if (!var)
671 return false;
673 if (SSA_NAME_IS_DEFAULT_DEF (var))
674 return false;
676 def_stmt = SSA_NAME_DEF_STMT (var);
678 if (TREE_CODE (def_stmt) != GIMPLE_MODIFY_STMT)
679 return false;
681 if (var != GIMPLE_STMT_OPERAND (def_stmt, 0))
682 return false;
684 rhs = get_call_expr_in (def_stmt);
686 if (!rhs)
687 return false;
689 flags = call_expr_flags (rhs);
691 return ((flags & ECF_MALLOC) != 0);
695 /* Check a cast FROM this variable, TO_TYPE. Mark the escaping types
696 if appropriate. Returns cast_type as detected. */
698 static enum cast_type
699 check_cast (tree to_type, tree from)
701 tree from_type = get_canon_type (TREE_TYPE (from), false, false);
702 bool to_interesting_type, from_interesting_type;
703 enum cast_type cast = CT_NO_CAST;
705 to_type = get_canon_type (to_type, false, false);
706 if (!from_type || !to_type || from_type == to_type)
707 return cast;
709 to_interesting_type =
710 ipa_type_escape_star_count_of_interesting_type (to_type) >= 0;
711 from_interesting_type =
712 ipa_type_escape_star_count_of_interesting_type (from_type) >= 0;
714 if (to_interesting_type)
715 if (from_interesting_type)
717 /* Both types are interesting. This can be one of four types
718 of cast: useless, up, down, or sideways. We do not care
719 about up or useless. Sideways casts are always bad and
720 both sides get marked as escaping. Downcasts are not
721 interesting here because if type is marked as escaping, all
722 of its subtypes escape. */
723 cast = check_cast_type (to_type, from_type);
724 switch (cast)
726 case CT_UP:
727 case CT_USELESS:
728 case CT_DOWN:
729 break;
731 case CT_SIDEWAYS:
732 mark_type (to_type, FULL_ESCAPE);
733 mark_type (from_type, FULL_ESCAPE);
734 break;
736 default:
737 break;
740 else
742 /* This code excludes two cases from marking as escaped:
744 1. if this is a cast of index of array of structures/unions
745 that happens before accessing array element, we should not
746 mark it as escaped.
747 2. if this is a cast from the local that is a result from a
748 call to malloc, do not mark the cast as bad.
752 if (POINTER_TYPE_P (to_type) && !POINTER_TYPE_P (from_type))
753 cast = CT_FROM_NON_P;
754 else if (TREE_CODE (from) == SSA_NAME
755 && is_malloc_result (from))
756 cast = CT_FROM_MALLOC;
757 else
759 cast = CT_FROM_P_BAD;
760 mark_type (to_type, FULL_ESCAPE);
763 else if (from_interesting_type)
765 mark_type (from_type, FULL_ESCAPE);
766 cast = CT_TO_NON_INTER;
769 return cast;
772 typedef struct cast
774 int type;
775 tree stmt;
776 }cast_t;
778 /* This function is a callback for walk_tree called from
779 is_cast_from_non_pointer. The data->type is set to be:
781 0 - if there is no cast
782 number - the number of casts from non-pointer type
783 -1 - if there is a cast that makes the type to escape
785 If data->type = number, then data->stmt will contain the
786 last casting stmt met in traversing. */
788 static tree
789 is_cast_from_non_pointer_1 (tree *tp, int *walk_subtrees, void *data)
791 tree def_stmt = *tp;
794 if (pointer_set_insert (visited_stmts, def_stmt))
796 *walk_subtrees = 0;
797 return NULL;
800 switch (TREE_CODE (def_stmt))
802 case GIMPLE_MODIFY_STMT:
804 use_operand_p use_p;
805 ssa_op_iter iter;
806 tree lhs = GIMPLE_STMT_OPERAND (def_stmt, 0);
807 tree rhs = GIMPLE_STMT_OPERAND (def_stmt, 1);
809 unsigned int cast = look_for_casts (lhs, rhs);
810 /* Check that only one cast happened, and it's of
811 non-pointer type. */
812 if ((cast & CT_FROM_NON_P) == (CT_FROM_NON_P)
813 && (cast & ~(CT_FROM_NON_P)) == 0)
815 ((cast_t *)data)->stmt = def_stmt;
816 ((cast_t *)data)->type++;
818 FOR_EACH_SSA_USE_OPERAND (use_p, def_stmt, iter, SSA_OP_ALL_USES)
820 walk_use_def_chains (USE_FROM_PTR (use_p), is_cast_from_non_pointer,
821 data, false);
822 if (((cast_t*)data)->type == -1)
823 return def_stmt;
827 /* Check that there is no cast, or cast is not harmful. */
828 else if ((cast & CT_NO_CAST) == (CT_NO_CAST)
829 || (cast & CT_DOWN) == (CT_DOWN)
830 || (cast & CT_UP) == (CT_UP)
831 || (cast & CT_USELESS) == (CT_USELESS)
832 || (cast & CT_FROM_MALLOC) == (CT_FROM_MALLOC))
834 FOR_EACH_SSA_USE_OPERAND (use_p, def_stmt, iter, SSA_OP_ALL_USES)
836 walk_use_def_chains (USE_FROM_PTR (use_p), is_cast_from_non_pointer,
837 data, false);
838 if (((cast_t*)data)->type == -1)
839 return def_stmt;
843 /* The cast is harmful. */
844 else
846 ((cast_t *)data)->type = -1;
847 return def_stmt;
850 *walk_subtrees = 0;
852 break;
854 default:
856 *walk_subtrees = 0;
857 break;
861 return NULL;
864 /* This function is a callback for walk_use_def_chains function called
865 from is_array_access_through_pointer_and_index. */
867 static bool
868 is_cast_from_non_pointer (tree var, tree def_stmt, void *data)
871 if (!def_stmt || !var)
872 return false;
874 if (TREE_CODE (def_stmt) == PHI_NODE)
875 return false;
877 if (SSA_NAME_IS_DEFAULT_DEF (var))
878 return false;
880 walk_tree (&def_stmt, is_cast_from_non_pointer_1, data, NULL);
881 if (((cast_t*)data)->type == -1)
882 return true;
884 return false;
887 /* When array element a_p[i] is accessed through the pointer a_p
888 and index i, it's translated into the following sequence
889 in gimple:
891 i.1_5 = (unsigned int) i_1;
892 D.1605_6 = i.1_5 * 16;
893 D.1606_7 = (struct str_t *) D.1605_6;
894 a_p.2_8 = a_p;
895 D.1608_9 = D.1606_7 + a_p.2_8;
897 OP0 and OP1 are of the same pointer types and stand for
898 D.1606_7 and a_p.2_8 or vise versa.
900 This function checks that:
902 1. one of OP0 and OP1 (D.1606_7) has passed only one cast from
903 non-pointer type (D.1606_7 = (struct str_t *) D.1605_6;).
905 2. one of OP0 and OP1 which has passed the cast from
906 non-pointer type (D.1606_7), is actually generated by multiplication of
907 index by size of type to which both OP0 and OP1 point to
908 (in this case D.1605_6 = i.1_5 * 16; ).
910 3. an address of def of the var to which was made cast (D.1605_6)
911 was not taken.(How can it happen?)
913 The following items are checked implicitly by the end of algorithm:
915 4. one of OP0 and OP1 (a_p.2_8) have never been cast
916 (because if it was cast to pointer type, its type, that is also
917 the type of OP0 and OP1, will be marked as escaped during
918 analysis of casting stmt (when check_cast() is called
919 from scan_for_refs for this stmt)).
921 5. defs of OP0 and OP1 are not passed into externally visible function
922 (because if they are passed then their type, that is also the type of OP0
923 and OP1, will be marked and escaped during check_call function called from
924 scan_for_refs with call stmt).
926 In total, 1-5 guaranty that it's an access to array by pointer and index.
930 bool
931 is_array_access_through_pointer_and_index (enum tree_code code, tree op0,
932 tree op1, tree *base, tree *offset,
933 tree *offset_cast_stmt)
935 tree before_cast, before_cast_def_stmt;
936 cast_t op0_cast, op1_cast;
938 *base = NULL;
939 *offset = NULL;
940 *offset_cast_stmt = NULL;
942 /* Check 1. */
943 if (code == POINTER_PLUS_EXPR)
945 tree op0type = TYPE_MAIN_VARIANT (TREE_TYPE (op0));
946 tree op1type = TYPE_MAIN_VARIANT (TREE_TYPE (op1));
948 /* One of op0 and op1 is of pointer type and the other is numerical. */
949 if (POINTER_TYPE_P (op0type) && NUMERICAL_TYPE_CHECK (op1type))
951 *base = op0;
952 *offset = op1;
954 else if (POINTER_TYPE_P (op1type) && NUMERICAL_TYPE_CHECK (op0type))
956 *base = op1;
957 *offset = op0;
959 else
960 return false;
962 else
964 /* Init data for walk_use_def_chains function. */
965 op0_cast.type = op1_cast.type = 0;
966 op0_cast.stmt = op1_cast.stmt = NULL;
968 visited_stmts = pointer_set_create ();
969 walk_use_def_chains (op0, is_cast_from_non_pointer,(void *)(&op0_cast),
970 false);
971 pointer_set_destroy (visited_stmts);
973 visited_stmts = pointer_set_create ();
974 walk_use_def_chains (op1, is_cast_from_non_pointer,(void *)(&op1_cast),
975 false);
976 pointer_set_destroy (visited_stmts);
978 if (op0_cast.type == 1 && op1_cast.type == 0)
980 *base = op1;
981 *offset = op0;
982 *offset_cast_stmt = op0_cast.stmt;
984 else if (op0_cast.type == 0 && op1_cast.type == 1)
986 *base = op0;
987 *offset = op1;
988 *offset_cast_stmt = op1_cast.stmt;
990 else
991 return false;
994 /* Check 2.
995 offset_cast_stmt is of the form:
996 D.1606_7 = (struct str_t *) D.1605_6; */
998 if (*offset_cast_stmt)
1000 before_cast = SINGLE_SSA_TREE_OPERAND (*offset_cast_stmt, SSA_OP_USE);
1001 if (!before_cast)
1002 return false;
1004 if (SSA_NAME_IS_DEFAULT_DEF (before_cast))
1005 return false;
1007 before_cast_def_stmt = SSA_NAME_DEF_STMT (before_cast);
1008 if (!before_cast_def_stmt)
1009 return false;
1011 else
1012 before_cast_def_stmt = SSA_NAME_DEF_STMT (*offset);
1014 /* before_cast_def_stmt should be of the form:
1015 D.1605_6 = i.1_5 * 16; */
1017 if (TREE_CODE (before_cast_def_stmt) == GIMPLE_MODIFY_STMT)
1019 tree lhs = GIMPLE_STMT_OPERAND (before_cast_def_stmt,0);
1020 tree rhs = GIMPLE_STMT_OPERAND (before_cast_def_stmt,1);
1022 /* We expect temporary here. */
1023 if (!is_gimple_reg (lhs))
1024 return false;
1026 if (TREE_CODE (rhs) == MULT_EXPR)
1028 tree arg0 = TREE_OPERAND (rhs, 0);
1029 tree arg1 = TREE_OPERAND (rhs, 1);
1030 tree unit_size =
1031 TYPE_SIZE_UNIT (TREE_TYPE (TYPE_MAIN_VARIANT (TREE_TYPE (op0))));
1033 if (!(CONSTANT_CLASS_P (arg0)
1034 && simple_cst_equal (arg0,unit_size))
1035 && !(CONSTANT_CLASS_P (arg1)
1036 && simple_cst_equal (arg1,unit_size)))
1037 return false;
1039 else
1040 return false;
1042 else
1043 return false;
1045 /* Check 3.
1046 check that address of D.1605_6 was not taken.
1047 FIXME: if D.1605_6 is gimple reg than it cannot be addressable. */
1049 return true;
1052 /* Register the parameter and return types of function FN. The type
1053 ESCAPES if the function is visible outside of the compilation
1054 unit. */
1055 static void
1056 check_function_parameter_and_return_types (tree fn, bool escapes)
1058 tree arg;
1060 if (TYPE_ARG_TYPES (TREE_TYPE (fn)))
1062 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1063 arg && TREE_VALUE (arg) != void_type_node;
1064 arg = TREE_CHAIN (arg))
1066 tree type = get_canon_type (TREE_VALUE (arg), false, false);
1067 if (escapes)
1068 mark_interesting_type (type, EXPOSED_PARAMETER);
1071 else
1073 /* FIXME - According to Geoff Keating, we should never have to
1074 do this; the front ends should always process the arg list
1075 from the TYPE_ARG_LIST. However, Geoff is wrong, this code
1076 does seem to be live. */
1078 for (arg = DECL_ARGUMENTS (fn); arg; arg = TREE_CHAIN (arg))
1080 tree type = get_canon_type (TREE_TYPE (arg), false, false);
1081 if (escapes)
1082 mark_interesting_type (type, EXPOSED_PARAMETER);
1085 if (escapes)
1087 tree type = get_canon_type (TREE_TYPE (TREE_TYPE (fn)), false, false);
1088 mark_interesting_type (type, EXPOSED_PARAMETER);
1092 /* Return true if the variable T is the right kind of static variable to
1093 perform compilation unit scope escape analysis. */
1095 static inline void
1096 has_proper_scope_for_analysis (tree t)
1098 /* If the variable has the "used" attribute, treat it as if it had a
1099 been touched by the devil. */
1100 tree type = get_canon_type (TREE_TYPE (t), false, false);
1101 if (!type) return;
1103 if (lookup_attribute ("used", DECL_ATTRIBUTES (t)))
1105 mark_interesting_type (type, FULL_ESCAPE);
1106 return;
1109 /* Do not want to do anything with volatile except mark any
1110 function that uses one to be not const or pure. */
1111 if (TREE_THIS_VOLATILE (t))
1112 return;
1114 /* Do not care about a local automatic that is not static. */
1115 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
1116 return;
1118 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
1120 /* If the front end set the variable to be READONLY and
1121 constant, we can allow this variable in pure or const
1122 functions but the scope is too large for our analysis to set
1123 these bits ourselves. */
1125 if (TREE_READONLY (t)
1126 && DECL_INITIAL (t)
1127 && is_gimple_min_invariant (DECL_INITIAL (t)))
1128 ; /* Read of a constant, do not change the function state. */
1129 else
1131 /* The type escapes for all public and externs. */
1132 mark_interesting_type (type, FULL_ESCAPE);
1137 /* If T is a VAR_DECL for a static that we are interested in, add the
1138 uid to the bitmap. */
1140 static void
1141 check_operand (tree t)
1143 if (!t) return;
1145 /* This is an assignment from a function, register the types as
1146 escaping. */
1147 if (TREE_CODE (t) == FUNCTION_DECL)
1148 check_function_parameter_and_return_types (t, true);
1150 else if (TREE_CODE (t) == VAR_DECL)
1151 has_proper_scope_for_analysis (t);
1154 /* Examine tree T for references. */
1156 static void
1157 check_tree (tree t)
1159 if ((TREE_CODE (t) == EXC_PTR_EXPR) || (TREE_CODE (t) == FILTER_EXPR))
1160 return;
1162 /* We want to catch here also REALPART_EXPR and IMAGEPART_EXPR,
1163 but they already included in handled_component_p. */
1164 while (handled_component_p (t))
1166 if (TREE_CODE (t) == ARRAY_REF)
1167 check_operand (TREE_OPERAND (t, 1));
1168 t = TREE_OPERAND (t, 0);
1171 if (INDIRECT_REF_P (t))
1172 /* || TREE_CODE (t) == MEM_REF) */
1173 check_tree (TREE_OPERAND (t, 0));
1175 if (SSA_VAR_P (t) || (TREE_CODE (t) == FUNCTION_DECL))
1176 check_operand (t);
1179 /* Create an address_of edge FROM_TYPE.TO_TYPE. */
1180 static void
1181 mark_interesting_addressof (tree to_type, tree from_type)
1183 int from_uid;
1184 int to_uid;
1185 bitmap type_map;
1186 splay_tree_node result;
1188 from_type = get_canon_type (from_type, false, false);
1189 to_type = get_canon_type (to_type, false, false);
1191 if (!from_type || !to_type)
1192 return;
1194 from_uid = TYPE_UID (from_type);
1195 to_uid = TYPE_UID (to_type);
1197 gcc_assert (ipa_type_escape_star_count_of_interesting_type (from_type) == 0);
1199 /* Process the Y into X map pointer. */
1200 result = splay_tree_lookup (uid_to_addressof_down_map,
1201 (splay_tree_key) from_uid);
1203 if (result)
1204 type_map = (bitmap) result->value;
1205 else
1207 type_map = BITMAP_ALLOC (&ipa_obstack);
1208 splay_tree_insert (uid_to_addressof_down_map,
1209 from_uid,
1210 (splay_tree_value)type_map);
1212 bitmap_set_bit (type_map, TYPE_UID (to_type));
1214 /* Process the X into Y reverse map pointer. */
1215 result =
1216 splay_tree_lookup (uid_to_addressof_up_map, (splay_tree_key) to_uid);
1218 if (result)
1219 type_map = (bitmap) result->value;
1220 else
1222 type_map = BITMAP_ALLOC (&ipa_obstack);
1223 splay_tree_insert (uid_to_addressof_up_map,
1224 to_uid,
1225 (splay_tree_value)type_map);
1227 bitmap_set_bit (type_map, TYPE_UID (from_type));
1230 /* Scan tree T to see if there are any addresses taken in within T. */
1232 static void
1233 look_for_address_of (tree t)
1235 if (TREE_CODE (t) == ADDR_EXPR)
1237 tree x = get_base_var (t);
1238 tree cref = TREE_OPERAND (t, 0);
1240 /* If we have an expression of the form "&a.b.c.d", mark a.b,
1241 b.c and c.d. as having its address taken. */
1242 tree fielddecl = NULL_TREE;
1243 while (cref!= x)
1245 if (TREE_CODE (cref) == COMPONENT_REF)
1247 fielddecl = TREE_OPERAND (cref, 1);
1248 mark_interesting_addressof (TREE_TYPE (fielddecl),
1249 DECL_FIELD_CONTEXT (fielddecl));
1251 else if (TREE_CODE (cref) == ARRAY_REF)
1252 get_canon_type (TREE_TYPE (cref), false, false);
1254 cref = TREE_OPERAND (cref, 0);
1257 if (TREE_CODE (x) == VAR_DECL)
1258 has_proper_scope_for_analysis (x);
1263 /* Scan tree T to see if there are any casts within it.
1264 LHS Is the LHS of the expression involving the cast. */
1266 static unsigned int
1267 look_for_casts (tree lhs ATTRIBUTE_UNUSED, tree t)
1269 unsigned int cast = 0;
1272 if (is_gimple_cast (t) || TREE_CODE (t) == VIEW_CONVERT_EXPR)
1274 tree castfromvar = TREE_OPERAND (t, 0);
1275 cast = cast | check_cast (TREE_TYPE (t), castfromvar);
1277 else
1278 while (handled_component_p (t))
1280 t = TREE_OPERAND (t, 0);
1281 if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
1283 /* This may be some part of a component ref.
1284 IE it may be a.b.VIEW_CONVERT_EXPR<weird_type>(c).d, AFAIK.
1285 castfromref will give you a.b.c, not a. */
1286 tree castfromref = TREE_OPERAND (t, 0);
1287 cast = cast | check_cast (TREE_TYPE (t), castfromref);
1289 else if (TREE_CODE (t) == COMPONENT_REF)
1290 get_canon_type (TREE_TYPE (TREE_OPERAND (t, 1)), false, false);
1293 if (!cast)
1294 cast = CT_NO_CAST;
1295 return cast;
1298 /* Check to see if T is a read or address of operation on a static var
1299 we are interested in analyzing. */
1301 static void
1302 check_rhs_var (tree t)
1304 look_for_address_of (t);
1305 check_tree(t);
1308 /* Check to see if T is an assignment to a static var we are
1309 interested in analyzing. */
1311 static void
1312 check_lhs_var (tree t)
1314 check_tree(t);
1317 /* This is a scaled down version of get_asm_expr_operands from
1318 tree_ssa_operands.c. The version there runs much later and assumes
1319 that aliasing information is already available. Here we are just
1320 trying to find if the set of inputs and outputs contain references
1321 or address of operations to local. FN is the function being
1322 analyzed and STMT is the actual asm statement. */
1324 static void
1325 get_asm_expr_operands (tree stmt)
1327 int noutputs = list_length (ASM_OUTPUTS (stmt));
1328 const char **oconstraints
1329 = (const char **) alloca ((noutputs) * sizeof (const char *));
1330 int i;
1331 tree link;
1332 const char *constraint;
1333 bool allows_mem, allows_reg, is_inout;
1335 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
1337 oconstraints[i] = constraint
1338 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1339 parse_output_constraint (&constraint, i, 0, 0,
1340 &allows_mem, &allows_reg, &is_inout);
1342 check_lhs_var (TREE_VALUE (link));
1345 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1347 constraint
1348 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1349 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1350 oconstraints, &allows_mem, &allows_reg);
1352 check_rhs_var (TREE_VALUE (link));
1355 /* There is no code here to check for asm memory clobbers. The
1356 casual maintainer might think that such code would be necessary,
1357 but that appears to be wrong. In other parts of the compiler,
1358 the asm memory clobbers are assumed to only clobber variables
1359 that are addressable. All types with addressable instances are
1360 assumed to already escape. So, we are protected here. */
1363 /* Check the parameters of a function call to CALL_EXPR to mark the
1364 types that pass across the function boundary. Also check to see if
1365 this is either an indirect call, a call outside the compilation
1366 unit. */
1368 static void
1369 check_call (tree call_expr)
1371 tree operand;
1372 tree callee_t = get_callee_fndecl (call_expr);
1373 struct cgraph_node* callee;
1374 enum availability avail = AVAIL_NOT_AVAILABLE;
1375 call_expr_arg_iterator iter;
1377 FOR_EACH_CALL_EXPR_ARG (operand, iter, call_expr)
1378 check_rhs_var (operand);
1380 if (callee_t)
1382 tree arg_type;
1383 tree last_arg_type = NULL;
1384 callee = cgraph_node(callee_t);
1385 avail = cgraph_function_body_availability (callee);
1387 /* Check that there are no implicit casts in the passing of
1388 parameters. */
1389 if (TYPE_ARG_TYPES (TREE_TYPE (callee_t)))
1391 for (arg_type = TYPE_ARG_TYPES (TREE_TYPE (callee_t)),
1392 operand = first_call_expr_arg (call_expr, &iter);
1393 arg_type && TREE_VALUE (arg_type) != void_type_node;
1394 arg_type = TREE_CHAIN (arg_type),
1395 operand = next_call_expr_arg (&iter))
1397 if (operand)
1399 last_arg_type = TREE_VALUE(arg_type);
1400 check_cast (last_arg_type, operand);
1402 else
1403 /* The code reaches here for some unfortunate
1404 builtin functions that do not have a list of
1405 argument types. */
1406 break;
1409 else
1411 /* FIXME - According to Geoff Keating, we should never
1412 have to do this; the front ends should always process
1413 the arg list from the TYPE_ARG_LIST. */
1414 for (arg_type = DECL_ARGUMENTS (callee_t),
1415 operand = first_call_expr_arg (call_expr, &iter);
1416 arg_type;
1417 arg_type = TREE_CHAIN (arg_type),
1418 operand = next_call_expr_arg (&iter))
1420 if (operand)
1422 last_arg_type = TREE_TYPE(arg_type);
1423 check_cast (last_arg_type, operand);
1425 else
1426 /* The code reaches here for some unfortunate
1427 builtin functions that do not have a list of
1428 argument types. */
1429 break;
1433 /* In the case where we have a var_args function, we need to
1434 check the remaining parameters against the last argument. */
1435 arg_type = last_arg_type;
1436 for (;
1437 operand != NULL_TREE;
1438 operand = next_call_expr_arg (&iter))
1440 if (arg_type)
1441 check_cast (arg_type, operand);
1442 else
1444 /* The code reaches here for some unfortunate
1445 builtin functions that do not have a list of
1446 argument types. Most of these functions have
1447 been marked as having their parameters not
1448 escape, but for the rest, the type is doomed. */
1449 tree type = get_canon_type (TREE_TYPE (operand), false, false);
1450 mark_interesting_type (type, FULL_ESCAPE);
1455 /* The callee is either unknown (indirect call) or there is just no
1456 scannable code for it (external call) . We look to see if there
1457 are any bits available for the callee (such as by declaration or
1458 because it is builtin) and process solely on the basis of those
1459 bits. */
1461 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
1463 /* If this is a direct call to an external function, mark all of
1464 the parameter and return types. */
1465 FOR_EACH_CALL_EXPR_ARG (operand, iter, call_expr)
1467 tree type = get_canon_type (TREE_TYPE (operand), false, false);
1468 mark_interesting_type (type, EXPOSED_PARAMETER);
1471 if (callee_t)
1473 tree type =
1474 get_canon_type (TREE_TYPE (TREE_TYPE (callee_t)), false, false);
1475 mark_interesting_type (type, EXPOSED_PARAMETER);
1480 /* CODE is the operation on OP0 and OP1. OP0 is the operand that we
1481 *know* is a pointer type. OP1 may be a pointer type. */
1482 static bool
1483 okay_pointer_operation (enum tree_code code, tree op0, tree op1)
1485 tree op0type = TYPE_MAIN_VARIANT (TREE_TYPE (op0));
1487 switch (code)
1489 case MULT_EXPR:
1490 /* Multiplication does not change alignment. */
1491 return true;
1492 break;
1493 case MINUS_EXPR:
1494 case PLUS_EXPR:
1495 case POINTER_PLUS_EXPR:
1497 tree base, offset, offset_cast_stmt;
1499 if (POINTER_TYPE_P (op0type)
1500 && TREE_CODE (op0) == SSA_NAME
1501 && TREE_CODE (op1) == SSA_NAME
1502 && is_array_access_through_pointer_and_index (code, op0, op1,
1503 &base,
1504 &offset,
1505 &offset_cast_stmt))
1506 return true;
1507 else
1509 tree size_of_op0_points_to = TYPE_SIZE_UNIT (TREE_TYPE (op0type));
1511 if (CONSTANT_CLASS_P (op1)
1512 && size_of_op0_points_to
1513 && multiple_of_p (TREE_TYPE (size_of_op0_points_to),
1514 op1, size_of_op0_points_to))
1515 return true;
1517 if (CONSTANT_CLASS_P (op0)
1518 && size_of_op0_points_to
1519 && multiple_of_p (TREE_TYPE (size_of_op0_points_to),
1520 op0, size_of_op0_points_to))
1521 return true;
1524 break;
1525 default:
1526 return false;
1528 return false;
1531 /* TP is the part of the tree currently under the microscope.
1532 WALK_SUBTREES is part of the walk_tree api but is unused here.
1533 DATA is cgraph_node of the function being walked. */
1535 /* FIXME: When this is converted to run over SSA form, this code
1536 should be converted to use the operand scanner. */
1538 static tree
1539 scan_for_refs (tree *tp, int *walk_subtrees, void *data)
1541 struct cgraph_node *fn = (struct cgraph_node *) data;
1542 tree t = *tp;
1544 switch (TREE_CODE (t))
1546 case VAR_DECL:
1547 if (DECL_INITIAL (t))
1548 walk_tree (&DECL_INITIAL (t), scan_for_refs, fn, visited_nodes);
1549 *walk_subtrees = 0;
1550 break;
1552 case GIMPLE_MODIFY_STMT:
1554 /* First look on the lhs and see what variable is stored to */
1555 tree lhs = GIMPLE_STMT_OPERAND (t, 0);
1556 tree rhs = GIMPLE_STMT_OPERAND (t, 1);
1558 check_lhs_var (lhs);
1559 check_cast (TREE_TYPE (lhs), rhs);
1561 /* For the purposes of figuring out what the cast affects */
1563 /* Next check the operands on the rhs to see if they are ok. */
1564 switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
1566 case tcc_binary:
1568 tree op0 = TREE_OPERAND (rhs, 0);
1569 tree type0 = get_canon_type (TREE_TYPE (op0), false, false);
1570 tree op1 = TREE_OPERAND (rhs, 1);
1571 tree type1 = get_canon_type (TREE_TYPE (op1), false, false);
1573 /* If this is pointer arithmetic of any bad sort, then
1574 we need to mark the types as bad. For binary
1575 operations, no binary operator we currently support
1576 is always "safe" in regard to what it would do to
1577 pointers for purposes of determining which types
1578 escape, except operations of the size of the type.
1579 It is possible that min and max under the right set
1580 of circumstances and if the moon is in the correct
1581 place could be safe, but it is hard to see how this
1582 is worth the effort. */
1584 if (type0 && POINTER_TYPE_P (type0)
1585 && !okay_pointer_operation (TREE_CODE (rhs), op0, op1))
1586 mark_interesting_type (type0, FULL_ESCAPE);
1587 if (type1 && POINTER_TYPE_P (type1)
1588 && !okay_pointer_operation (TREE_CODE (rhs), op1, op0))
1589 mark_interesting_type (type1, FULL_ESCAPE);
1591 look_for_casts (lhs, op0);
1592 look_for_casts (lhs, op1);
1593 check_rhs_var (op0);
1594 check_rhs_var (op1);
1596 break;
1597 case tcc_unary:
1599 tree op0 = TREE_OPERAND (rhs, 0);
1600 tree type0 = get_canon_type (TREE_TYPE (op0), false, false);
1601 /* For unary operations, if the operation is NEGATE or
1602 ABS on a pointer, this is also considered pointer
1603 arithmetic and thus, bad for business. */
1604 if (type0 && (TREE_CODE (op0) == NEGATE_EXPR
1605 || TREE_CODE (op0) == ABS_EXPR)
1606 && POINTER_TYPE_P (type0))
1608 mark_interesting_type (type0, FULL_ESCAPE);
1610 check_rhs_var (op0);
1611 look_for_casts (lhs, op0);
1612 look_for_casts (lhs, rhs);
1615 break;
1616 case tcc_reference:
1617 look_for_casts (lhs, rhs);
1618 check_rhs_var (rhs);
1619 break;
1620 case tcc_declaration:
1621 check_rhs_var (rhs);
1622 break;
1623 case tcc_expression:
1624 switch (TREE_CODE (rhs))
1626 case ADDR_EXPR:
1627 look_for_casts (lhs, TREE_OPERAND (rhs, 0));
1628 check_rhs_var (rhs);
1629 break;
1630 default:
1631 break;
1633 break;
1634 case tcc_vl_exp:
1635 switch (TREE_CODE (rhs))
1637 case CALL_EXPR:
1638 /* If this is a call to malloc, squirrel away the
1639 result so we do mark the resulting cast as being
1640 bad. */
1641 check_call (rhs);
1642 break;
1643 default:
1644 break;
1646 break;
1647 default:
1648 break;
1650 *walk_subtrees = 0;
1652 break;
1654 case ADDR_EXPR:
1655 /* This case is here to find addresses on rhs of constructors in
1656 decl_initial of static variables. */
1657 check_rhs_var (t);
1658 *walk_subtrees = 0;
1659 break;
1661 case CALL_EXPR:
1662 check_call (t);
1663 *walk_subtrees = 0;
1664 break;
1666 case ASM_EXPR:
1667 get_asm_expr_operands (t);
1668 *walk_subtrees = 0;
1669 break;
1671 default:
1672 break;
1674 return NULL;
1678 /* The init routine for analyzing global static variable usage. See
1679 comments at top for description. */
1680 static void
1681 ipa_init (void)
1683 bitmap_obstack_initialize (&ipa_obstack);
1684 global_types_exposed_parameter = BITMAP_ALLOC (&ipa_obstack);
1685 global_types_full_escape = BITMAP_ALLOC (&ipa_obstack);
1686 global_types_seen = BITMAP_ALLOC (&ipa_obstack);
1688 uid_to_canon_type = splay_tree_new (splay_tree_compare_ints, 0, 0);
1689 all_canon_types = splay_tree_new (compare_type_brand, 0, 0);
1690 type_to_canon_type = splay_tree_new (splay_tree_compare_pointers, 0, 0);
1691 uid_to_subtype_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1692 uid_to_addressof_down_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1693 uid_to_addressof_up_map = splay_tree_new (splay_tree_compare_ints, 0, 0);
1695 /* There are some shared nodes, in particular the initializers on
1696 static declarations. We do not need to scan them more than once
1697 since all we would be interested in are the addressof
1698 operations. */
1699 visited_nodes = pointer_set_create ();
1700 initialized = true;
1703 /* Check out the rhs of a static or global initialization VNODE to see
1704 if any of them contain addressof operations. Note that some of
1705 these variables may not even be referenced in the code in this
1706 compilation unit but their right hand sides may contain references
1707 to variables defined within this unit. */
1709 static void
1710 analyze_variable (struct varpool_node *vnode)
1712 tree global = vnode->decl;
1713 tree type = get_canon_type (TREE_TYPE (global), false, false);
1715 /* If this variable has exposure beyond the compilation unit, add
1716 its type to the global types. */
1718 if (vnode->externally_visible)
1719 mark_interesting_type (type, FULL_ESCAPE);
1721 gcc_assert (TREE_CODE (global) == VAR_DECL);
1723 if (DECL_INITIAL (global))
1724 walk_tree (&DECL_INITIAL (global), scan_for_refs, NULL, visited_nodes);
1727 /* This is the main routine for finding the reference patterns for
1728 global variables within a function FN. */
1730 static void
1731 analyze_function (struct cgraph_node *fn)
1733 tree decl = fn->decl;
1734 check_function_parameter_and_return_types (decl,
1735 fn->local.externally_visible);
1736 if (dump_file)
1737 fprintf (dump_file, "\n local analysis of %s", cgraph_node_name (fn));
1740 struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
1741 basic_block this_block;
1743 FOR_EACH_BB_FN (this_block, this_cfun)
1745 block_stmt_iterator bsi;
1746 for (bsi = bsi_start (this_block); !bsi_end_p (bsi); bsi_next (&bsi))
1747 walk_tree (bsi_stmt_ptr (bsi), scan_for_refs,
1748 fn, visited_nodes);
1752 /* There may be const decls with interesting right hand sides. */
1753 if (DECL_STRUCT_FUNCTION (decl))
1755 tree step;
1756 for (step = DECL_STRUCT_FUNCTION (decl)->local_decls;
1757 step;
1758 step = TREE_CHAIN (step))
1760 tree var = TREE_VALUE (step);
1761 if (TREE_CODE (var) == VAR_DECL
1762 && DECL_INITIAL (var)
1763 && !TREE_STATIC (var))
1764 walk_tree (&DECL_INITIAL (var), scan_for_refs,
1765 fn, visited_nodes);
1766 get_canon_type (TREE_TYPE (var), false, false);
1773 /* Convert a type_UID into a type. */
1774 static tree
1775 type_for_uid (int uid)
1777 splay_tree_node result =
1778 splay_tree_lookup (uid_to_canon_type, (splay_tree_key) uid);
1780 if (result)
1781 return (tree) result->value;
1782 else return NULL;
1785 /* Return a bitmap with the subtypes of the type for UID. If it
1786 does not exist, return either NULL or a new bitmap depending on the
1787 value of CREATE. */
1789 static bitmap
1790 subtype_map_for_uid (int uid, bool create)
1792 splay_tree_node result = splay_tree_lookup (uid_to_subtype_map,
1793 (splay_tree_key) uid);
1795 if (result)
1796 return (bitmap) result->value;
1797 else if (create)
1799 bitmap subtype_map = BITMAP_ALLOC (&ipa_obstack);
1800 splay_tree_insert (uid_to_subtype_map,
1801 uid,
1802 (splay_tree_value)subtype_map);
1803 return subtype_map;
1805 else return NULL;
1808 /* Mark all of the supertypes and field types of TYPE as being seen.
1809 Also accumulate the subtypes for each type so that
1810 close_types_full_escape can mark a subtype as escaping if the
1811 supertype escapes. */
1813 static void
1814 close_type_seen (tree type)
1816 tree field;
1817 int i, uid;
1818 tree binfo, base_binfo;
1820 /* See thru all pointer tos and array ofs. */
1821 type = get_canon_type (type, true, true);
1822 if (!type)
1823 return;
1825 uid = TYPE_UID (type);
1827 if (bitmap_bit_p (been_there_done_that, uid))
1828 return;
1829 bitmap_set_bit (been_there_done_that, uid);
1831 /* If we are doing a language with a type hierarchy, mark all of
1832 the superclasses. */
1833 if (TYPE_BINFO (type))
1834 for (binfo = TYPE_BINFO (type), i = 0;
1835 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1837 tree binfo_type = BINFO_TYPE (base_binfo);
1838 bitmap subtype_map = subtype_map_for_uid
1839 (TYPE_UID (TYPE_MAIN_VARIANT (binfo_type)), true);
1840 bitmap_set_bit (subtype_map, uid);
1841 close_type_seen (get_canon_type (binfo_type, true, true));
1844 /* If the field is a struct or union type, mark all of the
1845 subfields. */
1846 for (field = TYPE_FIELDS (type);
1847 field;
1848 field = TREE_CHAIN (field))
1850 tree field_type;
1851 if (TREE_CODE (field) != FIELD_DECL)
1852 continue;
1854 field_type = TREE_TYPE (field);
1855 if (ipa_type_escape_star_count_of_interesting_or_array_type (field_type) >= 0)
1856 close_type_seen (get_canon_type (field_type, true, true));
1860 /* Take a TYPE that has been passed by value to an external function
1861 and mark all of the fields that have pointer types as escaping. For
1862 any of the non pointer types that are structures or unions,
1863 recurse. TYPE is never a pointer type. */
1865 static void
1866 close_type_exposed_parameter (tree type)
1868 tree field;
1869 int uid;
1871 type = get_canon_type (type, false, false);
1872 if (!type)
1873 return;
1874 uid = TYPE_UID (type);
1875 gcc_assert (!POINTER_TYPE_P (type));
1877 if (bitmap_bit_p (been_there_done_that, uid))
1878 return;
1879 bitmap_set_bit (been_there_done_that, uid);
1881 /* If the field is a struct or union type, mark all of the
1882 subfields. */
1883 for (field = TYPE_FIELDS (type);
1884 field;
1885 field = TREE_CHAIN (field))
1887 tree field_type;
1889 if (TREE_CODE (field) != FIELD_DECL)
1890 continue;
1892 field_type = get_canon_type (TREE_TYPE (field), false, false);
1893 mark_interesting_type (field_type, EXPOSED_PARAMETER);
1895 /* Only recurse for non pointer types of structures and unions. */
1896 if (ipa_type_escape_star_count_of_interesting_type (field_type) == 0)
1897 close_type_exposed_parameter (field_type);
1901 /* The next function handles the case where a type fully escapes.
1902 This means that not only does the type itself escape,
1904 a) the type of every field recursively escapes
1905 b) the type of every subtype escapes as well as the super as well
1906 as all of the pointer to types for each field.
1908 Note that pointer to types are not marked as escaping. If the
1909 pointed to type escapes, the pointer to type also escapes.
1911 Take a TYPE that has had the address taken for an instance of it
1912 and mark all of the types for its fields as having their addresses
1913 taken. */
1915 static void
1916 close_type_full_escape (tree type)
1918 tree field;
1919 unsigned int i;
1920 int uid;
1921 tree binfo, base_binfo;
1922 bitmap_iterator bi;
1923 bitmap subtype_map;
1924 splay_tree_node address_result;
1926 /* Strip off any pointer or array types. */
1927 type = get_canon_type (type, true, true);
1928 if (!type)
1929 return;
1930 uid = TYPE_UID (type);
1932 if (bitmap_bit_p (been_there_done_that, uid))
1933 return;
1934 bitmap_set_bit (been_there_done_that, uid);
1936 subtype_map = subtype_map_for_uid (uid, false);
1938 /* If we are doing a language with a type hierarchy, mark all of
1939 the superclasses. */
1940 if (TYPE_BINFO (type))
1941 for (binfo = TYPE_BINFO (type), i = 0;
1942 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1944 tree binfotype = BINFO_TYPE (base_binfo);
1945 binfotype = mark_type (binfotype, FULL_ESCAPE);
1946 close_type_full_escape (binfotype);
1949 /* Mark as escaped any types that have been down casted to
1950 this type. */
1951 if (subtype_map)
1952 EXECUTE_IF_SET_IN_BITMAP (subtype_map, 0, i, bi)
1954 tree subtype = type_for_uid (i);
1955 subtype = mark_type (subtype, FULL_ESCAPE);
1956 close_type_full_escape (subtype);
1959 /* If the field is a struct or union type, mark all of the
1960 subfields. */
1961 for (field = TYPE_FIELDS (type);
1962 field;
1963 field = TREE_CHAIN (field))
1965 tree field_type;
1966 if (TREE_CODE (field) != FIELD_DECL)
1967 continue;
1969 field_type = TREE_TYPE (field);
1970 if (ipa_type_escape_star_count_of_interesting_or_array_type (field_type) >= 0)
1972 field_type = mark_type (field_type, FULL_ESCAPE);
1973 close_type_full_escape (field_type);
1977 /* For all of the types A that contain this type B and were part of
1978 an expression like "&...A.B...", mark the A's as escaping. */
1979 address_result = splay_tree_lookup (uid_to_addressof_up_map,
1980 (splay_tree_key) uid);
1981 if (address_result)
1983 bitmap containing_classes = (bitmap) address_result->value;
1984 EXECUTE_IF_SET_IN_BITMAP (containing_classes, 0, i, bi)
1986 close_type_full_escape (type_for_uid (i));
1991 /* Transitively close the addressof bitmap for the type with UID.
1992 This means that if we had a.b and b.c, a would have both b and c in
1993 its maps. */
1995 static bitmap
1996 close_addressof_down (int uid)
1998 bitmap_iterator bi;
1999 splay_tree_node result =
2000 splay_tree_lookup (uid_to_addressof_down_map, (splay_tree_key) uid);
2001 bitmap map = NULL;
2002 bitmap new_map;
2003 unsigned int i;
2005 if (result)
2006 map = (bitmap) result->value;
2007 else
2008 return NULL;
2010 if (bitmap_bit_p (been_there_done_that, uid))
2011 return map;
2012 bitmap_set_bit (been_there_done_that, uid);
2014 /* If the type escapes, get rid of the addressof map, it will not be
2015 needed. */
2016 if (bitmap_bit_p (global_types_full_escape, uid))
2018 BITMAP_FREE (map);
2019 splay_tree_remove (uid_to_addressof_down_map, (splay_tree_key) uid);
2020 return NULL;
2023 /* The new_map will have all of the bits for the enclosed fields and
2024 will have the unique id version of the old map. */
2025 new_map = BITMAP_ALLOC (&ipa_obstack);
2027 EXECUTE_IF_SET_IN_BITMAP (map, 0, i, bi)
2029 bitmap submap = close_addressof_down (i);
2030 bitmap_set_bit (new_map, i);
2031 if (submap)
2032 bitmap_ior_into (new_map, submap);
2034 result->value = (splay_tree_value) new_map;
2036 BITMAP_FREE (map);
2037 return new_map;
2041 /* The main entry point for type escape analysis. */
2043 static unsigned int
2044 type_escape_execute (void)
2046 struct cgraph_node *node;
2047 struct varpool_node *vnode;
2048 unsigned int i;
2049 bitmap_iterator bi;
2050 splay_tree_node result;
2052 ipa_init ();
2054 /* Process all of the variables first. */
2055 FOR_EACH_STATIC_VARIABLE (vnode)
2056 analyze_variable (vnode);
2058 /* Process all of the functions next.
2060 We do not want to process any of the clones so we check that this
2061 is a master clone. However, we do need to process any
2062 AVAIL_OVERWRITABLE functions (these are never clones) because
2063 they may cause a type variable to escape.
2065 for (node = cgraph_nodes; node; node = node->next)
2066 if (node->analyzed
2067 && (cgraph_is_master_clone (node)
2068 || (cgraph_function_body_availability (node) == AVAIL_OVERWRITABLE)))
2069 analyze_function (node);
2072 pointer_set_destroy (visited_nodes);
2073 visited_nodes = NULL;
2075 /* Do all of the closures to discover which types escape the
2076 compilation unit. */
2078 been_there_done_that = BITMAP_ALLOC (&ipa_obstack);
2079 bitmap_tmp = BITMAP_ALLOC (&ipa_obstack);
2081 /* Examine the types that we have directly seen in scanning the code
2082 and add to that any contained types or superclasses. */
2084 bitmap_copy (bitmap_tmp, global_types_seen);
2085 EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
2087 tree type = type_for_uid (i);
2088 /* Only look at records and unions and pointer tos. */
2089 if (ipa_type_escape_star_count_of_interesting_or_array_type (type) >= 0)
2090 close_type_seen (type);
2092 bitmap_clear (been_there_done_that);
2094 /* Examine all of the types passed by value and mark any enclosed
2095 pointer types as escaping. */
2096 bitmap_copy (bitmap_tmp, global_types_exposed_parameter);
2097 EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
2099 close_type_exposed_parameter (type_for_uid (i));
2101 bitmap_clear (been_there_done_that);
2103 /* Close the types for escape. If something escapes, then any
2104 enclosed types escape as well as any subtypes. */
2105 bitmap_copy (bitmap_tmp, global_types_full_escape);
2106 EXECUTE_IF_SET_IN_BITMAP (bitmap_tmp, 0, i, bi)
2108 close_type_full_escape (type_for_uid (i));
2110 bitmap_clear (been_there_done_that);
2112 /* Before this pass, the uid_to_addressof_down_map for type X
2113 contained an entry for Y if there had been an operation of the
2114 form &X.Y. This step adds all of the fields contained within Y
2115 (recursively) to X's map. */
2117 result = splay_tree_min (uid_to_addressof_down_map);
2118 while (result)
2120 int uid = result->key;
2121 /* Close the addressof map, i.e. copy all of the transitive
2122 substructures up to this level. */
2123 close_addressof_down (uid);
2124 result = splay_tree_successor (uid_to_addressof_down_map, uid);
2127 /* Do not need the array types and pointer types in the persistent
2128 data structures. */
2129 result = splay_tree_min (all_canon_types);
2130 while (result)
2132 tree type = (tree) result->value;
2133 tree key = (tree) result->key;
2134 if (POINTER_TYPE_P (type)
2135 || TREE_CODE (type) == ARRAY_TYPE)
2137 splay_tree_remove (all_canon_types, (splay_tree_key) result->key);
2138 splay_tree_remove (type_to_canon_type, (splay_tree_key) type);
2139 splay_tree_remove (uid_to_canon_type, (splay_tree_key) TYPE_UID (type));
2140 bitmap_clear_bit (global_types_seen, TYPE_UID (type));
2142 result = splay_tree_successor (all_canon_types, (splay_tree_key) key);
2145 if (dump_file)
2147 EXECUTE_IF_SET_IN_BITMAP (global_types_seen, 0, i, bi)
2149 /* The pointer types are in the global_types_full_escape
2150 bitmap but not in the backwards map. They also contain
2151 no useful information since they are not marked. */
2152 tree type = type_for_uid (i);
2153 fprintf(dump_file, "type %d ", i);
2154 print_generic_expr (dump_file, type, 0);
2155 if (bitmap_bit_p (global_types_full_escape, i))
2156 fprintf(dump_file, " escaped\n");
2157 else
2158 fprintf(dump_file, " contained\n");
2162 /* Get rid of uid_to_addressof_up_map and its bitmaps. */
2163 result = splay_tree_min (uid_to_addressof_up_map);
2164 while (result)
2166 int uid = (int)result->key;
2167 bitmap bm = (bitmap)result->value;
2169 BITMAP_FREE (bm);
2170 splay_tree_remove (uid_to_addressof_up_map, (splay_tree_key) uid);
2171 result = splay_tree_successor (uid_to_addressof_up_map, uid);
2174 /* Get rid of the subtype map. */
2175 result = splay_tree_min (uid_to_subtype_map);
2176 while (result)
2178 bitmap b = (bitmap)result->value;
2179 BITMAP_FREE(b);
2180 splay_tree_remove (uid_to_subtype_map, result->key);
2181 result = splay_tree_min (uid_to_subtype_map);
2183 splay_tree_delete (uid_to_subtype_map);
2184 uid_to_subtype_map = NULL;
2186 BITMAP_FREE (global_types_exposed_parameter);
2187 BITMAP_FREE (been_there_done_that);
2188 BITMAP_FREE (bitmap_tmp);
2189 return 0;
2192 static bool
2193 gate_type_escape_vars (void)
2195 return (flag_unit_at_a_time != 0 && flag_ipa_type_escape
2196 /* Don't bother doing anything if the program has errors. */
2197 && !(errorcount || sorrycount));
2200 struct simple_ipa_opt_pass pass_ipa_type_escape =
2203 SIMPLE_IPA_PASS,
2204 "type-escape-var", /* name */
2205 gate_type_escape_vars, /* gate */
2206 type_escape_execute, /* execute */
2207 NULL, /* sub */
2208 NULL, /* next */
2209 0, /* static_pass_number */
2210 TV_IPA_TYPE_ESCAPE, /* tv_id */
2211 0, /* properties_required */
2212 0, /* properties_provided */
2213 0, /* properties_destroyed */
2214 0, /* todo_flags_start */
2215 0 /* todo_flags_finish */