c-decl.c (merge_decls): Use DECL_USER_ALIGN() on olddecl to update the respective...
[official-gcc.git] / gcc / tree-ssa-alias-warnings.c
blob35117b35b20dde1fcbc65f56d86656a8e135e20a
1 /* Strict aliasing checks.
2 Copyright (C) 2007 Free Software Foundation, Inc.
3 Contributed by Silvius Rus <rus@google.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "alloc-pool.h"
26 #include "tree.h"
27 #include "tree-dump.h"
28 #include "tree-flow.h"
29 #include "params.h"
30 #include "function.h"
31 #include "expr.h"
32 #include "toplev.h"
33 #include "diagnostic.h"
34 #include "tree-ssa-structalias.h"
35 #include "tree-ssa-propagate.h"
36 #include "langhooks.h"
38 /* Module to issue a warning when a program uses data through a type
39 different from the type through which the data were defined.
40 Implements -Wstrict-aliasing and -Wstrict-aliasing=n.
41 These checks only happen when -fstrict-aliasing is present.
43 The idea is to use the compiler to identify occurrences of nonstandard
44 aliasing, and report them to programmers. Programs free of such aliasing
45 are more portable, maintainable, and can usually be optimized better.
47 The current, as of April 2007, C and C++ language standards forbid
48 accessing data of type A through an lvalue of another type B,
49 with certain exceptions. See the C Standard ISO/IEC 9899:1999,
50 section 6.5, paragraph 7, and the C++ Standard ISO/IEC 14882:1998,
51 section 3.10, paragraph 15.
53 Example 1:*a is used as int but was defined as a float, *b.
54 int* a = ...;
55 float* b = reinterpret_cast<float*> (a);
56 *b = 2.0;
57 return *a
59 Unfortunately, the problem is in general undecidable if we take into
60 account arithmetic expressions such as array indices or pointer arithmetic.
61 (It is at least as hard as Peano arithmetic decidability.)
62 Even ignoring arithmetic, the problem is still NP-hard, because it is
63 at least as hard as flow-insensitive may-alias analysis, which was proved
64 NP-hard by Horwitz et al, TOPLAS 1997.
66 It is clear that we need to choose some heuristics.
67 Unfortunately, various users have different goals which correspond to
68 different time budgets so a common approach will not suit all.
69 We present the user with three effort/accuracy levels. By accuracy, we mean
70 a common-sense mix of low count of false positives with a
71 reasonably low number of false negatives. We are heavily biased
72 towards a low count of false positives.
73 The effort (compilation time) is likely to increase with the level.
75 -Wstrict-aliasing=1
76 ===================
77 Most aggressive, least accurate. Possibly useful when higher levels
78 do not warn but -fstrict-aliasing still breaks the code, as
79 it has very few false negatives.
80 Warn for all bad pointer conversions, even if never dereferenced.
81 Implemented in the front end (c-common.c).
82 Uses alias_sets_might_conflict to compare types.
84 -Wstrict-aliasing=2
85 ===================
86 Aggressive, not too precise.
87 May still have many false positives (not as many as level 1 though),
88 and few false negatives (but possibly more than level 1).
89 Runs only in the front end. Uses alias_sets_might_conflict to
90 compare types. Does not check for pointer dereferences.
91 Only warns when an address is taken. Warns about incomplete type punning.
93 -Wstrict-aliasing=3 (default)
94 ===================
95 Should have very few false positives and few false negatives.
96 Takes care of the common punn+dereference pattern in the front end:
97 *(int*)&some_float.
98 Takes care of multiple statement cases in the back end,
99 using flow-sensitive points-to information (-O required).
100 Uses alias_sets_conflict_p to compare types and only warns
101 when the converted pointer is dereferenced.
102 Does not warn about incomplete type punning.
104 Future improvements can be included by adding higher levels.
106 In summary, expression level analysis is performed in the front-end,
107 and multiple-statement analysis is performed in the backend.
108 The remainder of this discussion is only about the backend analysis.
110 This implementation uses flow-sensitive points-to information.
111 Flow-sensitivity refers to accesses to the pointer, and not the object
112 pointed. For instance, we do not warn about the following case.
114 Example 2.
115 int* a = (int*)malloc (...);
116 float* b = reinterpret_cast<float*> (a);
117 *b = 2.0;
118 a = (int*)malloc (...);
119 return *a;
121 In SSA, it becomes clear that the INT value *A_2 referenced in the
122 return statement is not aliased to the FLOAT defined through *B_1.
123 int* a_1 = (int*)malloc (...);
124 float* b_1 = reinterpret_cast<float*> (a_1);
125 *b_1 = 2.0;
126 a_2 = (int*)malloc (...);
127 return *a_2;
130 Algorithm Outline
131 =================
133 ForEach (ptr, object) in the points-to table
134 If (incompatible_types (*ptr, object))
135 If (referenced (ptr, current function)
136 and referenced (object, current function))
137 Issue warning (ptr, object, reference locations)
139 The complexity is:
140 O (sizeof (points-to table)
141 + sizeof (function body) * lookup_time (points-to table))
143 Pointer dereference locations are looked up on demand. The search is
144 a single scan of the function body, in which all references to pointers
145 and objects in the points-to table are recorded. However, this dominant
146 time factor occurs rarely, only when cross-type aliasing was detected.
149 Limitations of the Proposed Implementation
150 ==========================================
152 1. We do not catch the following case, because -fstrict-aliasing will
153 associate different tags with MEM while building points-to information,
154 thus before we get to analyze it.
155 XXX: this could be solved by either running with -fno-strict-aliasing
156 or by recording the points-to information before splitting the original
157 tag based on type.
159 Example 3.
160 void* mem = malloc (...);
161 int* pi = reinterpret_cast<int*> (mem);
162 float* b = reinterpret_cast<float*> (mem);
163 *b = 2.0;
164 return *pi+1;
166 2. We do not check whether the two conflicting (de)references can
167 reach each other in the control flow sense. If we fixed limitation
168 1, we would wrongly issue a warning in the following case.
170 Example 4.
171 void* raw = malloc (...);
172 if (...) {
173 float* b = reinterpret_cast<float*> (raw);
174 *b = 2.0;
175 return (int)*b;
176 } else {
177 int* a = reinterpret_cast<int*> (raw);
178 *a = 1;
179 return *a;
181 3. Only simple types are compared, thus no structures, unions or classes
182 are analyzed. A first attempt to deal with structures introduced much
183 complication and has not showed much improvement in preliminary tests,
184 so it was left out.
186 4. All analysis is intraprocedural. */
189 /* Local declarations. */
190 static void find_references_in_function (void);
194 /* Get main type of tree TYPE, stripping array dimensions and qualifiers. */
196 static tree
197 get_main_type (tree type)
199 while (TREE_CODE (type) == ARRAY_TYPE)
200 type = TREE_TYPE (type);
201 return TYPE_MAIN_VARIANT (type);
205 /* Get the type of the given object. If IS_PTR is true, get the type of the
206 object pointed to or referenced by OBJECT instead.
207 For arrays, return the element type. Ignore all qualifiers. */
209 static tree
210 get_otype (tree object, bool is_ptr)
212 tree otype = TREE_TYPE (object);
214 if (is_ptr)
216 gcc_assert (POINTER_TYPE_P (otype));
217 otype = TREE_TYPE (otype);
219 return get_main_type (otype);
223 /* Return true if tree TYPE is struct, class or union. */
225 static bool
226 struct_class_union_p (tree type)
228 return (TREE_CODE (type) == RECORD_TYPE
229 || TREE_CODE (type) == UNION_TYPE
230 || TREE_CODE (type) == QUAL_UNION_TYPE);
235 /* Keep data during a search for an aliasing site.
236 RHS = object or pointer aliased. No LHS is specified because we are only
237 looking in the UseDef paths of a given variable, so LHS will always be
238 an SSA name of the same variable.
239 When IS_RHS_POINTER = true, we are looking for ... = RHS. Otherwise,
240 we are looking for ... = &RHS.
241 SITE is the output of a search, non-NULL if the search succeeded. */
243 struct alias_match
245 tree rhs;
246 bool is_rhs_pointer;
247 tree site;
251 /* Callback for find_alias_site. Return true if the right hand site
252 of STMT matches DATA. */
254 static bool
255 find_alias_site_helper (tree var ATTRIBUTE_UNUSED, tree stmt, void *data)
257 struct alias_match *match = (struct alias_match *) data;
258 tree rhs_pointer = get_rhs (stmt);
259 tree to_match = NULL_TREE;
261 while (TREE_CODE (rhs_pointer) == NOP_EXPR
262 || TREE_CODE (rhs_pointer) == CONVERT_EXPR
263 || TREE_CODE (rhs_pointer) == VIEW_CONVERT_EXPR)
264 rhs_pointer = TREE_OPERAND (rhs_pointer, 0);
266 if (!rhs_pointer)
267 /* Not a type conversion. */
268 return false;
270 if (TREE_CODE (rhs_pointer) == ADDR_EXPR && !match->is_rhs_pointer)
271 to_match = TREE_OPERAND (rhs_pointer, 0);
272 else if (POINTER_TYPE_P (rhs_pointer) && match->is_rhs_pointer)
273 to_match = rhs_pointer;
275 if (to_match != match->rhs)
276 /* Type conversion, but not a name match. */
277 return false;
279 /* Found it. */
280 match->site = stmt;
281 return true;
285 /* Find the statement where OBJECT1 gets aliased to OBJECT2.
286 If IS_PTR2 is true, consider OBJECT2 to be the name of a pointer or
287 reference rather than the actual aliased object.
288 For now, just implement the case where OBJECT1 is an SSA name defined
289 by a PHI statement. */
291 static tree
292 find_alias_site (tree object1, bool is_ptr1 ATTRIBUTE_UNUSED,
293 tree object2, bool is_ptr2)
295 struct alias_match match;
297 match.rhs = object2;
298 match.is_rhs_pointer = is_ptr2;
299 match.site = NULL_TREE;
301 if (TREE_CODE (object1) != SSA_NAME)
302 return NULL_TREE;
304 walk_use_def_chains (object1, find_alias_site_helper, &match, false);
305 return match.site;
309 /* Structure to store temporary results when trying to figure out whether
310 an object is referenced. Just its presence in the text is not enough,
311 as we may just be taking its address. */
313 struct match_info
315 tree object;
316 bool is_ptr;
317 /* The difference between the number of references to OBJECT
318 and the number of occurrences of &OBJECT. */
319 int found;
323 /* Return the base if EXPR is an SSA name. Return EXPR otherwise. */
325 static tree
326 get_ssa_base (tree expr)
328 if (TREE_CODE (expr) == SSA_NAME)
329 return SSA_NAME_VAR (expr);
330 else
331 return expr;
335 /* Record references to objects and pointer dereferences across some piece of
336 code. The number of references is recorded for each item.
337 References to an object just to take its address are not counted.
338 For instance, if PTR is a pointer and OBJ is an object:
339 1. Expression &obj + *ptr will have the following reference match structure:
340 ptrs: <ptr, 1>
341 objs: <ptr, 1>
342 OBJ does not appear as referenced because we just take its address.
343 2. Expression ptr + *ptr will have the following reference match structure:
344 ptrs: <ptr, 1>
345 objs: <ptr, 2>
346 PTR shows up twice as an object, but is dereferenced only once.
348 The elements of the hash tables are tree_map objects. */
349 struct reference_matches
351 htab_t ptrs;
352 htab_t objs;
356 /* Return the match, if any. Otherwise, return NULL_TREE. It will
357 return NULL_TREE even when a match was found, if the value associated
358 to KEY is NULL_TREE. */
360 static inline tree
361 match (htab_t ref_map, tree key)
363 struct tree_map *found;
364 void **slot = NULL;
365 slot = htab_find_slot (ref_map, &key, NO_INSERT);
367 if (!slot)
368 return NULL_TREE;
370 found = (struct tree_map *) *slot;
371 return found->to;
375 /* Set the entry corresponding to KEY, but only if the entry
376 already exists and its value is NULL_TREE. Otherwise, do nothing. */
378 static inline void
379 maybe_add_match (htab_t ref_map, struct tree_map *key)
381 struct tree_map *found = (struct tree_map *) htab_find (ref_map, key);
383 if (found && !found->to)
384 found->to = key->to;
388 /* Add an entry to HT, with key T and value NULL_TREE. */
390 static void
391 add_key (htab_t ht, tree t, alloc_pool references_pool)
393 void **slot;
394 struct tree_map *tp = (struct tree_map *) pool_alloc (references_pool);
396 tp->base.from = t;
397 tp->to = NULL_TREE;
398 slot = htab_find_slot (ht, &t, INSERT);
399 *slot = (void *) tp;
403 /* Some memory to keep the objects in the reference table. */
405 static alloc_pool ref_table_alloc_pool = NULL;
408 /* Get some memory to keep the objects in the reference table. */
410 static inline alloc_pool
411 reference_table_alloc_pool (bool build)
413 if (ref_table_alloc_pool || !build)
414 return ref_table_alloc_pool;
416 ref_table_alloc_pool =
417 create_alloc_pool ("ref_table_alloc_pool", sizeof (struct tree_map), 20);
419 return ref_table_alloc_pool;
423 /* Initialize the reference table by adding all pointers in the points-to
424 table as keys, and NULL_TREE as associated values. */
426 static struct reference_matches *
427 build_reference_table (void)
429 unsigned int i;
430 struct reference_matches *ref_table = NULL;
431 alloc_pool references_pool = reference_table_alloc_pool (true);
433 ref_table = XNEW (struct reference_matches);
434 ref_table->objs = htab_create (10, tree_map_base_hash, tree_map_eq, NULL);
435 ref_table->ptrs = htab_create (10, tree_map_base_hash, tree_map_eq, NULL);
437 for (i = 1; i < num_ssa_names; i++)
439 tree ptr = ssa_name (i);
440 struct ptr_info_def *pi;
442 if (ptr == NULL_TREE)
443 continue;
445 pi = SSA_NAME_PTR_INFO (ptr);
447 if (!SSA_NAME_IN_FREE_LIST (ptr) && pi && pi->name_mem_tag)
449 /* Add pointer to the interesting dereference list. */
450 add_key (ref_table->ptrs, ptr, references_pool);
452 /* Add all aliased names to the interesting reference list. */
453 if (pi->pt_vars)
455 referenced_var_iterator ri;
456 tree alias;
458 FOR_EACH_REFERENCED_VAR_IN_BITMAP (pi->pt_vars, alias, ri)
459 add_key (ref_table->objs, alias, references_pool);
464 return ref_table;
468 /* Reference table. */
470 static struct reference_matches *ref_table = NULL;
473 /* Clean up the reference table if allocated. */
475 static void
476 maybe_free_reference_table (void)
478 if (ref_table)
480 htab_delete (ref_table->ptrs);
481 htab_delete (ref_table->objs);
482 free (ref_table);
483 ref_table = NULL;
486 if (ref_table_alloc_pool)
488 free_alloc_pool (ref_table_alloc_pool);
489 ref_table_alloc_pool = NULL;
494 /* Get the reference table. Initialize it if needed. */
496 static inline struct reference_matches *
497 reference_table (bool build)
499 if (ref_table || !build)
500 return ref_table;
502 ref_table = build_reference_table ();
503 find_references_in_function ();
504 return ref_table;
508 /* Callback for find_references_in_function.
509 Check whether *TP is an object reference or pointer dereference for the
510 variables given in ((struct match_info*)DATA)->OBJS or
511 ((struct match_info*)DATA)->PTRS. The total number of references
512 is stored in the same structures. */
514 static tree
515 find_references_in_tree_helper (tree *tp,
516 int *walk_subtrees ATTRIBUTE_UNUSED,
517 void *data)
519 struct tree_map match;
520 static int parent_tree_code = ERROR_MARK;
522 /* Do not report references just for the purpose of taking an address.
523 XXX: we rely on the fact that the tree walk is in preorder
524 and that ADDR_EXPR is not a leaf, thus cannot be carried over across
525 walks. */
526 if (parent_tree_code == ADDR_EXPR)
527 goto finish;
529 match.to = (tree) data;
531 if (TREE_CODE (*tp) == INDIRECT_REF)
533 match.base.from = TREE_OPERAND (*tp, 0);
534 maybe_add_match (reference_table (true)->ptrs, &match);
536 else
538 match.base.from = *tp;
539 maybe_add_match (reference_table (true)->objs, &match);
542 finish:
543 parent_tree_code = TREE_CODE (*tp);
544 return NULL_TREE;
548 /* Find all the references to aliased variables in the current function. */
550 static void
551 find_references_in_function (void)
553 basic_block bb;
554 block_stmt_iterator i;
556 FOR_EACH_BB (bb)
557 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
558 walk_tree (bsi_stmt_ptr (i), find_references_in_tree_helper,
559 (void *) *bsi_stmt_ptr (i), NULL);
563 /* Find the reference site for OBJECT.
564 If IS_PTR is true, look for dereferences of OBJECT instead.
565 XXX: only the first site is returned in the current
566 implementation. If there are no matching sites, return NULL_TREE. */
568 static tree
569 reference_site (tree object, bool is_ptr)
571 if (is_ptr)
572 return match (reference_table (true)->ptrs, object);
573 else
574 return match (reference_table (true)->objs, object);
578 /* Try to get more location info when something is missing.
579 OBJECT1 and OBJECT2 are aliased names. If IS_PTR1 or IS_PTR2, the alias
580 is on the memory referenced or pointed to by OBJECT1 and OBJECT2.
581 ALIAS_SITE, DEREF_SITE1 and DEREF_SITE2 are the statements where the
582 alias takes place (some pointer assignment usually) and where the
583 alias is referenced through OBJECT1 and OBJECT2 respectively.
584 REF_TYPE1 and REF_TYPE2 will return the type of the reference at the
585 respective sites. Only the first matching reference is returned for
586 each name. If no statement is found, the function header is returned. */
588 static void
589 maybe_find_missing_stmts (tree object1, bool is_ptr1,
590 tree object2, bool is_ptr2,
591 tree *alias_site,
592 tree *deref_site1,
593 tree *deref_site2)
595 if (object1 && object2)
597 if (!*alias_site || !EXPR_HAS_LOCATION (*alias_site))
598 *alias_site = find_alias_site (object1, is_ptr1, object2, is_ptr2);
600 if (!*deref_site1 || !EXPR_HAS_LOCATION (*deref_site1))
601 *deref_site1 = reference_site (object1, is_ptr1);
603 if (!*deref_site2 || !EXPR_HAS_LOCATION (*deref_site2))
604 *deref_site2 = reference_site (object2, is_ptr2);
607 /* If we could not find the alias site, set it to one of the dereference
608 sites, if available. */
609 if (!*alias_site)
611 if (*deref_site1)
612 *alias_site = *deref_site1;
613 else if (*deref_site2)
614 *alias_site = *deref_site2;
617 /* If we could not find the dereference sites, set them to the alias site,
618 if known. */
619 if (!*deref_site1 && *alias_site)
620 *deref_site1 = *alias_site;
621 if (!*deref_site2 && *alias_site)
622 *deref_site2 = *alias_site;
626 /* Callback for find_first_artificial_name.
627 Find out if there are no artificial names at tree node *T. */
629 static tree
630 ffan_walker (tree *t,
631 int *go_below ATTRIBUTE_UNUSED,
632 void *data ATTRIBUTE_UNUSED)
634 if (DECL_P (*t) && !MTAG_P (*t) && DECL_ARTIFICIAL (*t))
635 return *t;
636 else
637 return NULL_TREE;
640 /* Return the first artificial name within EXPR, or NULL_TREE if
641 none exists. */
643 static tree
644 find_first_artificial_name (tree expr)
646 return walk_tree_without_duplicates (&expr, ffan_walker, NULL);
650 /* Get a name from the original program for VAR. */
652 static const char *
653 get_var_name (tree var)
655 if (TREE_CODE (var) == SSA_NAME)
656 return get_var_name (get_ssa_base (var));
658 if (find_first_artificial_name (var))
659 return "{unknown}";
661 if (TREE_CODE (var) == VAR_DECL || TREE_CODE (var) == PARM_DECL)
662 if (DECL_NAME (var))
663 return IDENTIFIER_POINTER (DECL_NAME (var));
665 return "{unknown}";
669 /* Return "*" if OBJECT is not the actual alias but a pointer to it, or
670 "" otherwise.
671 IS_PTR is true when OBJECT is not the actual alias.
672 In addition to checking IS_PTR, we also make sure that OBJECT is a pointer
673 since IS_PTR would also be true for C++ references, but we should only
674 print a * before a pointer and not before a reference. */
676 static const char *
677 get_maybe_star_prefix (tree object, bool is_ptr)
679 gcc_assert (object);
680 return (is_ptr
681 && TREE_CODE (TREE_TYPE (object)) == POINTER_TYPE) ? "*" : "";
685 /* Callback for contains_node_type_p.
686 Returns true if *T has tree code *(int*)DATA. */
688 static tree
689 contains_node_type_p_callback (tree *t,
690 int *go_below ATTRIBUTE_UNUSED,
691 void *data)
693 return ((int) TREE_CODE (*t) == *((int *) data)) ? *t : NULL_TREE;
697 /* Return true if T contains a node with tree code TYPE. */
699 static bool
700 contains_node_type_p (tree t, int type)
702 return (walk_tree_without_duplicates (&t, contains_node_type_p_callback,
703 (void *) &type)
704 != NULL_TREE);
708 /* Return true if a warning was issued in the front end at STMT. */
710 static bool
711 already_warned_in_frontend_p (tree stmt)
713 tree rhs_pointer;
715 if (stmt == NULL_TREE)
716 return false;
718 rhs_pointer = get_rhs (stmt);
720 if ((TREE_CODE (rhs_pointer) == NOP_EXPR
721 || TREE_CODE (rhs_pointer) == CONVERT_EXPR
722 || TREE_CODE (rhs_pointer) == VIEW_CONVERT_EXPR)
723 && TREE_NO_WARNING (rhs_pointer))
724 return true;
725 else
726 return false;
730 /* Return true if and only if TYPE is a function or method pointer type,
731 or pointer to a pointer to ... to a function or method. */
733 static bool
734 is_method_pointer (tree type)
736 while (TREE_CODE (type) == POINTER_TYPE)
737 type = TREE_TYPE (type);
738 return TREE_CODE (type) == METHOD_TYPE || TREE_CODE (type) == FUNCTION_TYPE;
742 /* Issue a -Wstrict-aliasing warning.
743 OBJECT1 and OBJECT2 are aliased names.
744 If IS_PTR1 and/or IS_PTR2 is true, then the corresponding name
745 OBJECT1/OBJECT2 is a pointer or reference to the aliased memory,
746 rather than actual storage.
747 ALIAS_SITE is a statement where the alias took place. In the most common
748 case, that is where a pointer was assigned to the address of an object. */
750 static bool
751 strict_aliasing_warn (tree alias_site,
752 tree object1, bool is_ptr1,
753 tree object2, bool is_ptr2,
754 bool filter_artificials)
756 tree ref_site1 = NULL_TREE;
757 tree ref_site2 = NULL_TREE;
758 const char *name1;
759 const char *name2;
760 location_t alias_loc;
761 location_t ref1_loc;
762 location_t ref2_loc;
763 gcc_assert (object1);
764 gcc_assert (object2);
765 name1 = get_var_name (object1);
766 name2 = get_var_name (object2);
769 if (is_method_pointer (get_main_type (TREE_TYPE (object2))))
770 return false;
772 maybe_find_missing_stmts (object1, is_ptr1, object2, is_ptr2, &alias_site,
773 &ref_site1, &ref_site2);
775 if (EXPR_HAS_LOCATION (alias_site))
776 alias_loc = EXPR_LOCATION (alias_site);
777 else
778 return false;
780 if (EXPR_HAS_LOCATION (ref_site1))
781 ref1_loc = EXPR_LOCATION (ref_site1);
782 else
783 ref1_loc = alias_loc;
785 if (EXPR_HAS_LOCATION (ref_site2))
786 ref2_loc = EXPR_LOCATION (ref_site2);
787 else
788 ref2_loc = alias_loc;
790 if (already_warned_in_frontend_p (alias_site))
791 return false;
793 /* If they are not SSA names, but contain SSA names, drop the warning
794 because it cannot be displayed well.
795 Also drop it if they both contain artificials.
796 XXX: this is a hack, must figure out a better way to display them. */
797 if (filter_artificials)
798 if ((find_first_artificial_name (get_ssa_base (object1))
799 && find_first_artificial_name (get_ssa_base (object2)))
800 || (TREE_CODE (object1) != SSA_NAME
801 && contains_node_type_p (object1, SSA_NAME))
802 || (TREE_CODE (object2) != SSA_NAME
803 && contains_node_type_p (object2, SSA_NAME)))
804 return false;
807 /* XXX: In the following format string, %s:%d should be replaced by %H.
808 However, in my tests only the first %H printed ok, while the
809 second and third were printed as blanks. */
810 warning (OPT_Wstrict_aliasing,
811 "%Hlikely type-punning may break strict-aliasing rules: "
812 "object %<%s%s%> of main type %qT is referenced at or around "
813 "%s:%d and may be "
814 "aliased to object %<%s%s%> of main type %qT which is referenced "
815 "at or around %s:%d.",
816 &alias_loc,
817 get_maybe_star_prefix (object1, is_ptr1),
818 name1, get_otype (object1, is_ptr1),
819 LOCATION_FILE (ref1_loc), LOCATION_LINE (ref1_loc),
820 get_maybe_star_prefix (object2, is_ptr2),
821 name2, get_otype (object2, is_ptr2),
822 LOCATION_FILE (ref2_loc), LOCATION_LINE (ref2_loc));
824 return true;
829 /* Return true when any objects of TYPE1 and TYPE2 respectively
830 may not be aliased according to the language standard. */
832 static bool
833 nonstandard_alias_types_p (tree type1, tree type2)
835 alias_set_type set1;
836 alias_set_type set2;
838 if (VOID_TYPE_P (type1) || VOID_TYPE_P (type2))
839 return false;
841 set1 = get_alias_set (type1);
842 set2 = get_alias_set (type2);
843 return !alias_sets_conflict_p (set1, set2);
848 /* Returns true when *PTR may not be aliased to ALIAS.
849 See C standard 6.5p7 and C++ standard 3.10p15.
850 If PTR_PTR is true, ALIAS represents a pointer or reference to the
851 aliased storage rather than its actual name. */
853 static bool
854 nonstandard_alias_p (tree ptr, tree alias, bool ptr_ptr)
856 /* Find the types to compare. */
857 tree ptr_type = get_otype (ptr, true);
858 tree alias_type = get_otype (alias, ptr_ptr);
860 /* XXX: for now, say it's OK if the alias escapes.
861 Not sure this is needed in general, but otherwise GCC will not
862 bootstrap. */
863 if (var_ann (get_ssa_base (alias))->escape_mask != NO_ESCAPE)
864 return false;
866 /* XXX: don't get into structures for now. It brings much complication
867 and little benefit. */
868 if (struct_class_union_p (ptr_type) || struct_class_union_p (alias_type))
869 return false;
871 /* If they are both SSA names of artificials, let it go, the warning
872 is too confusing. */
873 if (find_first_artificial_name (ptr) && find_first_artificial_name (alias))
874 return false;
876 /* Compare the types. */
877 return nonstandard_alias_types_p (ptr_type, alias_type);
881 /* Return true when we should skip analysis for pointer PTR based on the
882 fact that their alias information *PI is not considered relevant. */
884 static bool
885 skip_this_pointer (tree ptr ATTRIBUTE_UNUSED, struct ptr_info_def *pi)
887 /* If it is not dereferenced, it is not a problem (locally). */
888 if (!pi->is_dereferenced)
889 return true;
891 /* This would probably cause too many false positives. */
892 if (pi->value_escapes_p || pi->pt_anything)
893 return true;
895 return false;
899 /* Find aliasing to named objects for pointer PTR. */
901 static void
902 dsa_named_for (tree ptr)
904 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
906 if (pi)
908 if (skip_this_pointer (ptr, pi))
909 return;
911 /* For all the variables it could be aliased to. */
912 if (pi->pt_vars)
914 referenced_var_iterator ri;
915 tree alias;
917 FOR_EACH_REFERENCED_VAR_IN_BITMAP (pi->pt_vars, alias, ri)
918 if (nonstandard_alias_p (ptr, alias, false))
919 strict_aliasing_warn (SSA_NAME_DEF_STMT (ptr),
920 ptr, true, alias, false, true);
926 /* Detect and report strict aliasing violation of named objects. */
928 static void
929 detect_strict_aliasing_named (void)
931 unsigned int i;
933 for (i = 1; i < num_ssa_names; i++)
935 tree ptr = ssa_name (i);
936 struct ptr_info_def *pi;
938 if (ptr == NULL_TREE)
939 continue;
941 pi = SSA_NAME_PTR_INFO (ptr);
943 if (!SSA_NAME_IN_FREE_LIST (ptr) && pi && pi->name_mem_tag)
944 dsa_named_for (ptr);
949 /* Return false only the first time I see each instance of FUNC. */
951 static bool
952 processed_func_p (tree func)
954 static htab_t seen = NULL;
955 void **slot = NULL;
957 if (!seen)
958 seen = htab_create (10, tree_map_base_hash, tree_map_eq, NULL);
960 slot = htab_find_slot (seen, &func, INSERT);
961 gcc_assert (slot);
963 if (*slot)
964 return true;
966 gcc_assert (slot);
967 *slot = &func;
968 return false;
972 /* Detect and warn about type-punning using points-to information. */
974 void
975 strict_aliasing_warning_backend (void)
977 if (flag_strict_aliasing && warn_strict_aliasing == 3
978 && !processed_func_p (current_function_decl))
980 detect_strict_aliasing_named ();
981 maybe_free_reference_table ();