2 Copyright (C) 2019-2022 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 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/>. */
23 #include "coretypes.h"
25 #include "diagnostic-core.h"
26 #include "gimple-pretty-print.h"
28 #include "basic-block.h"
30 #include "gimple-iterator.h"
31 #include "diagnostic-core.h"
36 #include "stringpool.h"
39 #include "fold-const.h"
40 #include "tree-pretty-print.h"
41 #include "diagnostic-color.h"
42 #include "diagnostic-metadata.h"
48 #include "analyzer/analyzer.h"
49 #include "analyzer/analyzer-logging.h"
50 #include "ordered-hash-map.h"
55 #include "analyzer/supergraph.h"
57 #include "analyzer/call-string.h"
58 #include "analyzer/program-point.h"
59 #include "analyzer/store.h"
60 #include "analyzer/region.h"
61 #include "analyzer/region-model.h"
62 #include "analyzer/sm.h"
63 #include "analyzer/program-state.h"
69 /* class region and its various subclasses. */
75 delete m_cached_offset
;
78 /* Compare REG1 and REG2 by id. */
81 region::cmp_ids (const region
*reg1
, const region
*reg2
)
83 return (long)reg1
->get_id () - (long)reg2
->get_id ();
86 /* Determine the base region for this region: when considering bindings
87 for this region, the base region is the ancestor which identifies
88 which cluster they should be partitioned into.
89 Regions within the same struct/union/array are in the same cluster.
90 Different decls are in different clusters. */
93 region::get_base_region () const
95 const region
*iter
= this;
98 switch (iter
->get_kind ())
105 iter
= iter
->get_parent_region ();
108 iter
= iter
->dyn_cast_cast_region ()->get_original_region ();
117 /* Return true if get_base_region() == this for this region. */
120 region::base_region_p () const
124 /* Region kinds representing a descendent of a base region. */
138 /* Return true if this region is ELDER or one of its descendents. */
141 region::descendent_of_p (const region
*elder
) const
143 const region
*iter
= this;
148 if (iter
->get_kind () == RK_CAST
)
149 iter
= iter
->dyn_cast_cast_region ()->get_original_region ();
151 iter
= iter
->get_parent_region ();
156 /* If this region is a frame_region, or a descendent of one, return it.
157 Otherwise return NULL. */
160 region::maybe_get_frame_region () const
162 const region
*iter
= this;
165 if (const frame_region
*frame_reg
= iter
->dyn_cast_frame_region ())
167 if (iter
->get_kind () == RK_CAST
)
168 iter
= iter
->dyn_cast_cast_region ()->get_original_region ();
170 iter
= iter
->get_parent_region ();
175 /* Get the memory space of this region. */
178 region::get_memory_space () const
180 const region
*iter
= this;
183 switch (iter
->get_kind ())
188 return MEMSPACE_GLOBALS
;
192 return MEMSPACE_CODE
;
196 return MEMSPACE_STACK
;
198 case RK_HEAP_ALLOCATED
:
199 return MEMSPACE_HEAP
;
201 return MEMSPACE_READONLY_DATA
;
203 if (iter
->get_kind () == RK_CAST
)
204 iter
= iter
->dyn_cast_cast_region ()->get_original_region ();
206 iter
= iter
->get_parent_region ();
208 return MEMSPACE_UNKNOWN
;
211 /* Subroutine for use by region_model_manager::get_or_create_initial_value.
212 Return true if this region has an initial_svalue.
213 Return false if attempting to use INIT_VAL(this_region) should give
214 the "UNINITIALIZED" poison value. */
217 region::can_have_initial_svalue_p () const
219 const region
*base_reg
= get_base_region ();
221 /* Check for memory spaces that are uninitialized by default. */
222 enum memory_space mem_space
= base_reg
->get_memory_space ();
227 case MEMSPACE_UNKNOWN
:
229 case MEMSPACE_GLOBALS
:
230 case MEMSPACE_READONLY_DATA
:
231 /* Such regions have initial_svalues. */
235 /* Heap allocations are uninitialized by default. */
239 if (tree decl
= base_reg
->maybe_get_decl ())
241 /* See the assertion in frame_region::get_region_for_local for the
242 tree codes we need to handle here. */
243 switch (TREE_CODE (decl
))
249 /* Parameters have initial values. */
254 /* Function locals don't have initial values. */
259 tree ssa_name
= decl
;
260 /* SSA names that are the default defn of a PARM_DECL
261 have initial_svalues; other SSA names don't. */
262 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name
)
263 && SSA_NAME_VAR (ssa_name
)
264 && TREE_CODE (SSA_NAME_VAR (ssa_name
)) == PARM_DECL
)
272 /* If we have an on-stack region that isn't associated with a decl
273 or SSA name, then we have VLA/alloca, which is uninitialized. */
278 /* If this region is a decl_region, return the decl.
279 Otherwise return NULL. */
282 region::maybe_get_decl () const
284 if (const decl_region
*decl_reg
= dyn_cast_decl_region ())
285 return decl_reg
->get_decl ();
289 /* Get the region_offset for this region (calculating it on the
290 first call and caching it internally). */
293 region::get_offset () const
296 m_cached_offset
= new region_offset (calc_offset ());
297 return *m_cached_offset
;
300 /* Base class implementation of region::get_byte_size vfunc.
301 If the size of this region (in bytes) is known statically, write it to *OUT
303 Otherwise return false. */
306 region::get_byte_size (byte_size_t
*out
) const
308 tree type
= get_type ();
310 /* Bail out e.g. for heap-allocated regions. */
314 HOST_WIDE_INT bytes
= int_size_in_bytes (type
);
321 /* Base implementation of region::get_byte_size_sval vfunc. */
324 region::get_byte_size_sval (region_model_manager
*mgr
) const
326 tree type
= get_type ();
328 /* Bail out e.g. for heap-allocated regions. */
330 return mgr
->get_or_create_unknown_svalue (size_type_node
);
332 HOST_WIDE_INT bytes
= int_size_in_bytes (type
);
334 return mgr
->get_or_create_unknown_svalue (size_type_node
);
336 tree byte_size
= size_in_bytes (type
);
337 if (TREE_TYPE (byte_size
) != size_type_node
)
338 byte_size
= fold_build1 (NOP_EXPR
, size_type_node
, byte_size
);
339 return mgr
->get_or_create_constant_svalue (byte_size
);
342 /* Attempt to get the size of TYPE in bits.
343 If successful, return true and write the size to *OUT.
344 Otherwise return false. */
347 int_size_in_bits (const_tree type
, bit_size_t
*out
)
349 if (INTEGRAL_TYPE_P (type
))
351 *out
= TYPE_PRECISION (type
);
355 tree sz
= TYPE_SIZE (type
);
356 if (sz
&& tree_fits_uhwi_p (sz
))
358 *out
= TREE_INT_CST_LOW (sz
);
365 /* If the size of this region (in bits) is known statically, write it to *OUT
367 Otherwise return false. */
370 region::get_bit_size (bit_size_t
*out
) const
372 tree type
= get_type ();
374 /* Bail out e.g. for heap-allocated regions. */
378 return int_size_in_bits (type
, out
);
381 /* Get the field within RECORD_TYPE at BIT_OFFSET. */
384 get_field_at_bit_offset (tree record_type
, bit_offset_t bit_offset
)
386 gcc_assert (TREE_CODE (record_type
) == RECORD_TYPE
);
390 /* Find the first field that has an offset > BIT_OFFSET,
391 then return the one preceding it.
392 Skip other trees within the chain, such as FUNCTION_DECLs. */
393 tree last_field
= NULL_TREE
;
394 for (tree iter
= TYPE_FIELDS (record_type
); iter
!= NULL_TREE
;
395 iter
= DECL_CHAIN (iter
))
397 if (TREE_CODE (iter
) == FIELD_DECL
)
399 int iter_field_offset
= int_bit_position (iter
);
400 if (bit_offset
< iter_field_offset
)
408 /* Populate *OUT with descendent regions of type TYPE that match
409 RELATIVE_BIT_OFFSET and SIZE_IN_BITS within this region. */
412 region::get_subregions_for_binding (region_model_manager
*mgr
,
413 bit_offset_t relative_bit_offset
,
414 bit_size_t size_in_bits
,
416 auto_vec
<const region
*> *out
) const
418 if (get_type () == NULL_TREE
|| type
== NULL_TREE
)
420 if (relative_bit_offset
== 0
421 && types_compatible_p (get_type (), type
))
423 out
->safe_push (this);
426 switch (TREE_CODE (get_type ()))
430 tree element_type
= TREE_TYPE (get_type ());
431 HOST_WIDE_INT hwi_byte_size
= int_size_in_bytes (element_type
);
432 if (hwi_byte_size
> 0)
434 HOST_WIDE_INT bits_per_element
435 = hwi_byte_size
<< LOG2_BITS_PER_UNIT
;
436 HOST_WIDE_INT element_index
437 = (relative_bit_offset
.to_shwi () / bits_per_element
);
438 tree element_index_cst
439 = build_int_cst (integer_type_node
, element_index
);
440 HOST_WIDE_INT inner_bit_offset
441 = relative_bit_offset
.to_shwi () % bits_per_element
;
442 const region
*subregion
= mgr
->get_element_region
444 mgr
->get_or_create_constant_svalue (element_index_cst
));
445 subregion
->get_subregions_for_binding (mgr
, inner_bit_offset
,
446 size_in_bits
, type
, out
);
452 /* The bit offset might be *within* one of the fields (such as
453 with nested structs).
454 So we want to find the enclosing field, adjust the offset,
456 if (tree field
= get_field_at_bit_offset (get_type (),
457 relative_bit_offset
))
459 int field_bit_offset
= int_bit_position (field
);
460 const region
*subregion
= mgr
->get_field_region (this, field
);
461 subregion
->get_subregions_for_binding
462 (mgr
, relative_bit_offset
- field_bit_offset
,
463 size_in_bits
, type
, out
);
469 for (tree field
= TYPE_FIELDS (get_type ()); field
!= NULL_TREE
;
470 field
= DECL_CHAIN (field
))
472 if (TREE_CODE (field
) != FIELD_DECL
)
474 const region
*subregion
= mgr
->get_field_region (this, field
);
475 subregion
->get_subregions_for_binding (mgr
,
489 /* Walk from this region up to the base region within its cluster, calculating
490 the offset relative to the base region, either as an offset in bits,
491 or a symbolic offset. */
494 region::calc_offset () const
496 const region
*iter_region
= this;
497 bit_offset_t accum_bit_offset
= 0;
501 switch (iter_region
->get_kind ())
508 bit_offset_t rel_bit_offset
;
509 if (!iter_region
->get_relative_concrete_offset (&rel_bit_offset
))
510 return region_offset::make_symbolic
511 (iter_region
->get_parent_region ());
512 accum_bit_offset
+= rel_bit_offset
;
513 iter_region
= iter_region
->get_parent_region ();
518 iter_region
= iter_region
->get_parent_region ();
523 const cast_region
*cast_reg
524 = as_a
<const cast_region
*> (iter_region
);
525 iter_region
= cast_reg
->get_original_region ();
530 return region_offset::make_concrete (iter_region
, accum_bit_offset
);
533 return region_offset::make_concrete (iter_region
, accum_bit_offset
);
536 /* Base implementation of region::get_relative_concrete_offset vfunc. */
539 region::get_relative_concrete_offset (bit_offset_t
*) const
544 /* Attempt to get the position and size of this region expressed as a
545 concrete range of bytes relative to its parent.
546 If successful, return true and write to *OUT.
547 Otherwise return false. */
550 region::get_relative_concrete_byte_range (byte_range
*out
) const
552 /* We must have a concrete offset relative to the parent. */
553 bit_offset_t rel_bit_offset
;
554 if (!get_relative_concrete_offset (&rel_bit_offset
))
556 /* ...which must be a whole number of bytes. */
557 if (rel_bit_offset
% BITS_PER_UNIT
!= 0)
559 byte_offset_t start_byte_offset
= rel_bit_offset
/ BITS_PER_UNIT
;
561 /* We must have a concrete size, which must be a whole number
563 byte_size_t num_bytes
;
564 if (!get_byte_size (&num_bytes
))
568 *out
= byte_range (start_byte_offset
, num_bytes
);
572 /* Dump a description of this region to stderr. */
575 region::dump (bool simple
) const
578 pp_format_decoder (&pp
) = default_tree_printer
;
579 pp_show_color (&pp
) = pp_show_color (global_dc
->printer
);
580 pp
.buffer
->stream
= stderr
;
581 dump_to_pp (&pp
, simple
);
586 /* Return a new json::string describing the region. */
589 region::to_json () const
591 label_text desc
= get_desc (true);
592 json::value
*reg_js
= new json::string (desc
.get ());
596 /* Generate a description of this region. */
598 DEBUG_FUNCTION label_text
599 region::get_desc (bool simple
) const
602 pp_format_decoder (&pp
) = default_tree_printer
;
603 dump_to_pp (&pp
, simple
);
604 return label_text::take (xstrdup (pp_formatted_text (&pp
)));
607 /* Base implementation of region::accept vfunc.
608 Subclass implementations should chain up to this. */
611 region::accept (visitor
*v
) const
613 v
->visit_region (this);
615 m_parent
->accept (v
);
618 /* Return true if this is a symbolic region for deferencing an
620 We shouldn't attempt to bind values for this region (but
621 can unbind values for other regions). */
624 region::symbolic_for_unknown_ptr_p () const
626 if (const symbolic_region
*sym_reg
= dyn_cast_symbolic_region ())
627 if (sym_reg
->get_pointer ()->get_kind () == SK_UNKNOWN
)
632 /* Return true if this is a region for a decl with name DECL_NAME.
633 Intended for use when debugging (for assertions and conditional
637 region::is_named_decl_p (const char *decl_name
) const
639 if (tree decl
= maybe_get_decl ())
641 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (decl
)), decl_name
))
648 region::region (complexity c
, unsigned id
, const region
*parent
, tree type
)
649 : m_complexity (c
), m_id (id
), m_parent (parent
), m_type (type
),
650 m_cached_offset (NULL
)
652 gcc_assert (type
== NULL_TREE
|| TYPE_P (type
));
655 /* Comparator for use by vec<const region *>::qsort,
656 using their IDs to order them. */
659 region::cmp_ptr_ptr (const void *p1
, const void *p2
)
661 const region
* const *reg1
= (const region
* const *)p1
;
662 const region
* const *reg2
= (const region
* const *)p2
;
664 return cmp_ids (*reg1
, *reg2
);
667 /* Determine if a pointer to this region must be non-NULL.
669 Generally, pointers to regions must be non-NULL, but pointers
670 to symbolic_regions might, in fact, be NULL.
672 This allows us to simulate functions like malloc and calloc with:
673 - only one "outcome" from each statement,
674 - the idea that the pointer is on the heap if non-NULL
675 - the possibility that the pointer could be NULL
676 - the idea that successive values returned from malloc are non-equal
677 - to be able to zero-fill for calloc. */
680 region::non_null_p () const
687 /* Are we within a symbolic_region? If so, it could be NULL, and we
688 have to fall back on the constraints. */
690 case RK_HEAP_ALLOCATED
:
695 /* Return true iff this region is defined in terms of SVAL. */
698 region::involves_p (const svalue
*sval
) const
700 if (const symbolic_region
*symbolic_reg
= dyn_cast_symbolic_region ())
702 if (symbolic_reg
->get_pointer ()->involves_p (sval
))
709 /* Comparator for trees to impose a deterministic ordering on
713 tree_cmp (const_tree t1
, const_tree t2
)
718 /* Test tree codes first. */
719 if (TREE_CODE (t1
) != TREE_CODE (t2
))
720 return TREE_CODE (t1
) - TREE_CODE (t2
);
722 /* From this point on, we know T1 and T2 have the same tree code. */
726 if (DECL_NAME (t1
) && DECL_NAME (t2
))
727 return strcmp (IDENTIFIER_POINTER (DECL_NAME (t1
)),
728 IDENTIFIER_POINTER (DECL_NAME (t2
)));
733 else if (DECL_NAME (t2
))
736 return DECL_UID (t1
) - DECL_UID (t2
);
740 switch (TREE_CODE (t1
))
744 if (SSA_NAME_VAR (t1
) && SSA_NAME_VAR (t2
))
746 int var_cmp
= tree_cmp (SSA_NAME_VAR (t1
), SSA_NAME_VAR (t2
));
749 return SSA_NAME_VERSION (t1
) - SSA_NAME_VERSION (t2
);
753 if (SSA_NAME_VAR (t1
))
755 else if (SSA_NAME_VAR (t2
))
758 return SSA_NAME_VERSION (t1
) - SSA_NAME_VERSION (t2
);
764 return tree_int_cst_compare (t1
, t2
);
768 const real_value
*rv1
= TREE_REAL_CST_PTR (t1
);
769 const real_value
*rv2
= TREE_REAL_CST_PTR (t2
);
770 if (real_compare (UNORDERED_EXPR
, rv1
, rv2
))
772 /* Impose an arbitrary order on NaNs relative to other NaNs
774 if (int cmp_isnan
= real_isnan (rv1
) - real_isnan (rv2
))
776 if (int cmp_issignaling_nan
777 = real_issignaling_nan (rv1
) - real_issignaling_nan (rv2
))
778 return cmp_issignaling_nan
;
779 return real_isneg (rv1
) - real_isneg (rv2
);
781 if (real_compare (LT_EXPR
, rv1
, rv2
))
783 if (real_compare (GT_EXPR
, rv1
, rv2
))
789 return strcmp (TREE_STRING_POINTER (t1
),
790 TREE_STRING_POINTER (t2
));
802 /* qsort comparator for trees to impose a deterministic ordering on
806 tree_cmp (const void *p1
, const void *p2
)
808 const_tree t1
= *(const_tree
const *)p1
;
809 const_tree t2
= *(const_tree
const *)p2
;
811 return tree_cmp (t1
, t2
);
814 /* class frame_region : public space_region. */
816 frame_region::~frame_region ()
818 for (map_t::iterator iter
= m_locals
.begin ();
819 iter
!= m_locals
.end ();
821 delete (*iter
).second
;
825 frame_region::accept (visitor
*v
) const
829 m_calling_frame
->accept (v
);
832 /* Implementation of region::dump_to_pp vfunc for frame_region. */
835 frame_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
838 pp_printf (pp
, "frame: %qs@%i", function_name (m_fun
), get_stack_depth ());
840 pp_printf (pp
, "frame_region(%qs, index: %i, depth: %i)",
841 function_name (m_fun
), m_index
, get_stack_depth ());
845 frame_region::get_region_for_local (region_model_manager
*mgr
,
847 const region_model_context
*ctxt
) const
851 /* Verify that EXPR is a local or SSA name, and that it's for the
852 correct function for this stack frame. */
853 gcc_assert (TREE_CODE (expr
) == PARM_DECL
854 || TREE_CODE (expr
) == VAR_DECL
855 || TREE_CODE (expr
) == SSA_NAME
856 || TREE_CODE (expr
) == RESULT_DECL
);
857 switch (TREE_CODE (expr
))
862 gcc_assert (!is_global_var (expr
));
866 gcc_assert (DECL_CONTEXT (expr
) == m_fun
->decl
);
870 if (tree var
= SSA_NAME_VAR (expr
))
873 gcc_assert (DECL_CONTEXT (var
) == m_fun
->decl
);
876 if (const extrinsic_state
*ext_state
= ctxt
->get_ext_state ())
877 if (const supergraph
*sg
878 = ext_state
->get_engine ()->get_supergraph ())
880 const gimple
*def_stmt
= SSA_NAME_DEF_STMT (expr
);
881 const supernode
*snode
882 = sg
->get_supernode_for_stmt (def_stmt
);
883 gcc_assert (snode
->get_function () == m_fun
);
890 /* Ideally we'd use mutable here. */
891 map_t
&mutable_locals
= const_cast <map_t
&> (m_locals
);
893 if (decl_region
**slot
= mutable_locals
.get (expr
))
896 = new decl_region (mgr
->alloc_region_id (), this, expr
);
897 mutable_locals
.put (expr
, reg
);
901 /* class globals_region : public space_region. */
903 /* Implementation of region::dump_to_pp vfunc for globals_region. */
906 globals_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
909 pp_string (pp
, "::");
911 pp_string (pp
, "globals");
914 /* class code_region : public map_region. */
916 /* Implementation of region::dump_to_pp vfunc for code_region. */
919 code_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
922 pp_string (pp
, "code region");
924 pp_string (pp
, "code_region()");
927 /* class function_region : public region. */
929 /* Implementation of region::dump_to_pp vfunc for function_region. */
932 function_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
936 dump_quoted_tree (pp
, m_fndecl
);
940 pp_string (pp
, "function_region(");
941 dump_quoted_tree (pp
, m_fndecl
);
946 /* class label_region : public region. */
948 /* Implementation of region::dump_to_pp vfunc for label_region. */
951 label_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
955 dump_quoted_tree (pp
, m_label
);
959 pp_string (pp
, "label_region(");
960 dump_quoted_tree (pp
, m_label
);
965 /* class stack_region : public region. */
967 /* Implementation of region::dump_to_pp vfunc for stack_region. */
970 stack_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
973 pp_string (pp
, "stack region");
975 pp_string (pp
, "stack_region()");
978 /* class heap_region : public region. */
980 /* Implementation of region::dump_to_pp vfunc for heap_region. */
983 heap_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
986 pp_string (pp
, "heap region");
988 pp_string (pp
, "heap_region()");
991 /* class root_region : public region. */
993 /* root_region's ctor. */
995 root_region::root_region (unsigned id
)
996 : region (complexity (1, 1), id
, NULL
, NULL_TREE
)
1000 /* Implementation of region::dump_to_pp vfunc for root_region. */
1003 root_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1006 pp_string (pp
, "root region");
1008 pp_string (pp
, "root_region()");
1011 /* class symbolic_region : public map_region. */
1013 /* symbolic_region's ctor. */
1015 symbolic_region::symbolic_region (unsigned id
, region
*parent
,
1016 const svalue
*sval_ptr
)
1017 : region (complexity::from_pair (parent
, sval_ptr
), id
, parent
,
1018 (sval_ptr
->get_type ()
1019 ? TREE_TYPE (sval_ptr
->get_type ())
1021 m_sval_ptr (sval_ptr
)
1025 /* Implementation of region::accept vfunc for symbolic_region. */
1028 symbolic_region::accept (visitor
*v
) const
1031 m_sval_ptr
->accept (v
);
1034 /* Implementation of region::dump_to_pp vfunc for symbolic_region. */
1037 symbolic_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1041 pp_string (pp
, "(*");
1042 m_sval_ptr
->dump_to_pp (pp
, simple
);
1043 pp_string (pp
, ")");
1047 pp_string (pp
, "symbolic_region(");
1048 get_parent_region ()->dump_to_pp (pp
, simple
);
1051 pp_string (pp
, ", ");
1052 print_quoted_type (pp
, get_type ());
1054 pp_string (pp
, ", ");
1055 m_sval_ptr
->dump_to_pp (pp
, simple
);
1056 pp_string (pp
, ")");
1060 /* class decl_region : public region. */
1062 /* Implementation of region::dump_to_pp vfunc for decl_region. */
1065 decl_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1068 pp_printf (pp
, "%E", m_decl
);
1071 pp_string (pp
, "decl_region(");
1072 get_parent_region ()->dump_to_pp (pp
, simple
);
1073 pp_string (pp
, ", ");
1074 print_quoted_type (pp
, get_type ());
1075 pp_printf (pp
, ", %qE)", m_decl
);
1079 /* Get the stack depth for the frame containing this decl, or 0
1083 decl_region::get_stack_depth () const
1085 if (get_parent_region () == NULL
)
1087 if (const frame_region
*frame_reg
1088 = get_parent_region ()->dyn_cast_frame_region ())
1089 return frame_reg
->get_stack_depth ();
1093 /* If the underlying decl is in the global constant pool,
1094 return an svalue representing the constant value.
1095 Otherwise return NULL. */
1098 decl_region::maybe_get_constant_value (region_model_manager
*mgr
) const
1100 if (TREE_CODE (m_decl
) == VAR_DECL
1101 && DECL_IN_CONSTANT_POOL (m_decl
)
1102 && DECL_INITIAL (m_decl
)
1103 && TREE_CODE (DECL_INITIAL (m_decl
)) == CONSTRUCTOR
)
1104 return get_svalue_for_constructor (DECL_INITIAL (m_decl
), mgr
);
1108 /* Get an svalue for CTOR, a CONSTRUCTOR for this region's decl. */
1111 decl_region::get_svalue_for_constructor (tree ctor
,
1112 region_model_manager
*mgr
) const
1114 gcc_assert (!TREE_CLOBBER_P (ctor
));
1116 /* Create a binding map, applying ctor to it, using this
1117 decl_region as the base region when building child regions
1118 for offset calculations. */
1120 if (!map
.apply_ctor_to_region (this, ctor
, mgr
))
1121 return mgr
->get_or_create_unknown_svalue (get_type ());
1123 /* Return a compound svalue for the map we built. */
1124 return mgr
->get_or_create_compound_svalue (get_type (), map
);
1127 /* For use on decl_regions for global variables.
1129 Get an svalue for the initial value of this region at entry to
1130 "main" (either based on DECL_INITIAL, or implicit initialization to
1133 Return NULL if there is a problem. */
1136 decl_region::get_svalue_for_initializer (region_model_manager
*mgr
) const
1138 tree init
= DECL_INITIAL (m_decl
);
1141 /* If we have an "extern" decl then there may be an initializer in
1143 if (DECL_EXTERNAL (m_decl
))
1146 /* Implicit initialization to zero; use a compound_svalue for it.
1147 Doing so requires that we have a concrete binding for this region,
1148 which can fail if we have a region with unknown size
1149 (e.g. "extern const char arr[];"). */
1150 const binding_key
*binding
1151 = binding_key::make (mgr
->get_store_manager (), this);
1152 if (binding
->symbolic_p ())
1155 /* If we don't care about tracking the content of this region, then
1156 it's unused, and the value doesn't matter. */
1160 binding_cluster
c (this);
1161 c
.zero_fill_region (mgr
->get_store_manager (), this);
1162 return mgr
->get_or_create_compound_svalue (TREE_TYPE (m_decl
),
1166 /* LTO can write out error_mark_node as the DECL_INITIAL for simple scalar
1167 values (to avoid writing out an extra section). */
1168 if (init
== error_mark_node
)
1171 if (TREE_CODE (init
) == CONSTRUCTOR
)
1172 return get_svalue_for_constructor (init
, mgr
);
1174 /* Reuse the get_rvalue logic from region_model. */
1175 region_model
m (mgr
);
1176 return m
.get_rvalue (path_var (init
, 0), NULL
);
1179 /* Subroutine of symnode_requires_tracking_p; return true if REF
1180 might imply that we should be tracking the value of its decl. */
1183 ipa_ref_requires_tracking (ipa_ref
*ref
)
1185 /* If we have a load/store/alias of the symbol, then we'll track
1186 the decl's value. */
1187 if (ref
->use
!= IPA_REF_ADDR
)
1190 if (ref
->stmt
== NULL
)
1193 switch (ref
->stmt
->code
)
1199 cgraph_node
*caller_cnode
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1200 if (caller_cnode
== NULL
)
1202 cgraph_edge
*edge
= caller_cnode
->get_edge (ref
->stmt
);
1205 if (edge
->callee
== NULL
)
1206 return true; /* e.g. call through function ptr. */
1207 if (edge
->callee
->definition
)
1209 /* If we get here, then this ref is a pointer passed to
1210 a function we don't have the definition for. */
1216 const gasm
*asm_stmt
= as_a
<const gasm
*> (ref
->stmt
);
1217 if (gimple_asm_noutputs (asm_stmt
) > 0)
1219 if (gimple_asm_nclobbers (asm_stmt
) > 0)
1221 /* If we get here, then this ref is the decl being passed
1222 by pointer to asm with no outputs. */
1229 /* Determine if the decl for SYMNODE should have binding_clusters
1230 in our state objects; return false to optimize away tracking
1231 certain decls in our state objects, as an optimization. */
1234 symnode_requires_tracking_p (symtab_node
*symnode
)
1236 gcc_assert (symnode
);
1237 if (symnode
->externally_visible
)
1239 tree context_fndecl
= DECL_CONTEXT (symnode
->decl
);
1240 if (context_fndecl
== NULL
)
1242 if (TREE_CODE (context_fndecl
) != FUNCTION_DECL
)
1244 for (auto ref
: symnode
->ref_list
.referring
)
1245 if (ipa_ref_requires_tracking (ref
))
1248 /* If we get here, then we don't have uses of this decl that require
1249 tracking; we never read from it or write to it explicitly. */
1253 /* Subroutine of decl_region ctor: determine whether this decl_region
1254 can have binding_clusters; return false to optimize away tracking
1255 of certain decls in our state objects, as an optimization. */
1258 decl_region::calc_tracked_p (tree decl
)
1260 /* Precondition of symtab_node::get. */
1261 if (TREE_CODE (decl
) == VAR_DECL
1262 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
) || in_lto_p
))
1263 if (symtab_node
*symnode
= symtab_node::get (decl
))
1264 return symnode_requires_tracking_p (symnode
);
1268 /* class field_region : public region. */
1270 /* Implementation of region::dump_to_pp vfunc for field_region. */
1273 field_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1277 get_parent_region ()->dump_to_pp (pp
, simple
);
1278 pp_string (pp
, ".");
1279 pp_printf (pp
, "%E", m_field
);
1283 pp_string (pp
, "field_region(");
1284 get_parent_region ()->dump_to_pp (pp
, simple
);
1285 pp_string (pp
, ", ");
1286 print_quoted_type (pp
, get_type ());
1287 pp_printf (pp
, ", %qE)", m_field
);
1291 /* Implementation of region::get_relative_concrete_offset vfunc
1292 for field_region. */
1295 field_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1297 /* Compare with e.g. gimple-fold.cc's
1298 fold_nonarray_ctor_reference. */
1299 tree byte_offset
= DECL_FIELD_OFFSET (m_field
);
1300 if (TREE_CODE (byte_offset
) != INTEGER_CST
)
1302 tree field_offset
= DECL_FIELD_BIT_OFFSET (m_field
);
1303 /* Compute bit offset of the field. */
1304 offset_int bitoffset
1305 = (wi::to_offset (field_offset
)
1306 + (wi::to_offset (byte_offset
) << LOG2_BITS_PER_UNIT
));
1311 /* class element_region : public region. */
1313 /* Implementation of region::accept vfunc for element_region. */
1316 element_region::accept (visitor
*v
) const
1319 m_index
->accept (v
);
1322 /* Implementation of region::dump_to_pp vfunc for element_region. */
1325 element_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1329 //pp_string (pp, "(");
1330 get_parent_region ()->dump_to_pp (pp
, simple
);
1331 pp_string (pp
, "[");
1332 m_index
->dump_to_pp (pp
, simple
);
1333 pp_string (pp
, "]");
1334 //pp_string (pp, ")");
1338 pp_string (pp
, "element_region(");
1339 get_parent_region ()->dump_to_pp (pp
, simple
);
1340 pp_string (pp
, ", ");
1341 print_quoted_type (pp
, get_type ());
1342 pp_string (pp
, ", ");
1343 m_index
->dump_to_pp (pp
, simple
);
1344 pp_printf (pp
, ")");
1348 /* Implementation of region::get_relative_concrete_offset vfunc
1349 for element_region. */
1352 element_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1354 if (tree idx_cst
= m_index
->maybe_get_constant ())
1356 gcc_assert (TREE_CODE (idx_cst
) == INTEGER_CST
);
1358 tree elem_type
= get_type ();
1359 offset_int element_idx
= wi::to_offset (idx_cst
);
1361 /* First, use int_size_in_bytes, to reject the case where we
1362 have an incomplete type, or a non-constant value. */
1363 HOST_WIDE_INT hwi_byte_size
= int_size_in_bytes (elem_type
);
1364 if (hwi_byte_size
> 0)
1366 offset_int element_bit_size
1367 = hwi_byte_size
<< LOG2_BITS_PER_UNIT
;
1368 offset_int element_bit_offset
1369 = element_idx
* element_bit_size
;
1370 *out
= element_bit_offset
;
1377 /* class offset_region : public region. */
1379 /* Implementation of region::accept vfunc for offset_region. */
1382 offset_region::accept (visitor
*v
) const
1385 m_byte_offset
->accept (v
);
1388 /* Implementation of region::dump_to_pp vfunc for offset_region. */
1391 offset_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1395 //pp_string (pp, "(");
1396 get_parent_region ()->dump_to_pp (pp
, simple
);
1397 pp_string (pp
, "+");
1398 m_byte_offset
->dump_to_pp (pp
, simple
);
1399 //pp_string (pp, ")");
1403 pp_string (pp
, "offset_region(");
1404 get_parent_region ()->dump_to_pp (pp
, simple
);
1405 pp_string (pp
, ", ");
1406 print_quoted_type (pp
, get_type ());
1407 pp_string (pp
, ", ");
1408 m_byte_offset
->dump_to_pp (pp
, simple
);
1409 pp_printf (pp
, ")");
1413 /* Implementation of region::get_relative_concrete_offset vfunc
1414 for offset_region. */
1417 offset_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1419 if (tree byte_offset_cst
= m_byte_offset
->maybe_get_constant ())
1421 gcc_assert (TREE_CODE (byte_offset_cst
) == INTEGER_CST
);
1422 /* Use a signed value for the byte offset, to handle
1423 negative offsets. */
1424 HOST_WIDE_INT byte_offset
1425 = wi::to_offset (byte_offset_cst
).to_shwi ();
1426 HOST_WIDE_INT bit_offset
= byte_offset
* BITS_PER_UNIT
;
1433 /* class sized_region : public region. */
1435 /* Implementation of region::accept vfunc for sized_region. */
1438 sized_region::accept (visitor
*v
) const
1441 m_byte_size_sval
->accept (v
);
1444 /* Implementation of region::dump_to_pp vfunc for sized_region. */
1447 sized_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1451 pp_string (pp
, "SIZED_REG(");
1452 get_parent_region ()->dump_to_pp (pp
, simple
);
1453 pp_string (pp
, ", ");
1454 m_byte_size_sval
->dump_to_pp (pp
, simple
);
1455 pp_string (pp
, ")");
1459 pp_string (pp
, "sized_region(");
1460 get_parent_region ()->dump_to_pp (pp
, simple
);
1461 pp_string (pp
, ", ");
1462 m_byte_size_sval
->dump_to_pp (pp
, simple
);
1463 pp_printf (pp
, ")");
1467 /* Implementation of region::get_byte_size vfunc for sized_region. */
1470 sized_region::get_byte_size (byte_size_t
*out
) const
1472 if (tree cst
= m_byte_size_sval
->maybe_get_constant ())
1474 gcc_assert (TREE_CODE (cst
) == INTEGER_CST
);
1475 *out
= tree_to_uhwi (cst
);
1481 /* Implementation of region::get_bit_size vfunc for sized_region. */
1484 sized_region::get_bit_size (bit_size_t
*out
) const
1486 byte_size_t byte_size
;
1487 if (!get_byte_size (&byte_size
))
1489 *out
= byte_size
* BITS_PER_UNIT
;
1493 /* class cast_region : public region. */
1495 /* Implementation of region::accept vfunc for cast_region. */
1498 cast_region::accept (visitor
*v
) const
1501 m_original_region
->accept (v
);
1504 /* Implementation of region::dump_to_pp vfunc for cast_region. */
1507 cast_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1511 pp_string (pp
, "CAST_REG(");
1512 print_quoted_type (pp
, get_type ());
1513 pp_string (pp
, ", ");
1514 m_original_region
->dump_to_pp (pp
, simple
);
1515 pp_string (pp
, ")");
1519 pp_string (pp
, "cast_region(");
1520 m_original_region
->dump_to_pp (pp
, simple
);
1521 pp_string (pp
, ", ");
1522 print_quoted_type (pp
, get_type ());
1523 pp_printf (pp
, ")");
1527 /* class heap_allocated_region : public region. */
1529 /* Implementation of region::dump_to_pp vfunc for heap_allocated_region. */
1532 heap_allocated_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1535 pp_printf (pp
, "HEAP_ALLOCATED_REGION(%i)", get_id ());
1537 pp_printf (pp
, "heap_allocated_region(%i)", get_id ());
1540 /* class alloca_region : public region. */
1542 /* Implementation of region::dump_to_pp vfunc for alloca_region. */
1545 alloca_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1548 pp_printf (pp
, "ALLOCA_REGION(%i)", get_id ());
1550 pp_printf (pp
, "alloca_region(%i)", get_id ());
1553 /* class string_region : public region. */
1555 /* Implementation of region::dump_to_pp vfunc for string_region. */
1558 string_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1561 dump_tree (pp
, m_string_cst
);
1564 pp_string (pp
, "string_region(");
1565 dump_tree (pp
, m_string_cst
);
1566 if (!flag_dump_noaddr
)
1568 pp_string (pp
, " (");
1569 pp_pointer (pp
, m_string_cst
);
1570 pp_string (pp
, "))");
1575 /* class bit_range_region : public region. */
1577 /* Implementation of region::dump_to_pp vfunc for bit_range_region. */
1580 bit_range_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1584 pp_string (pp
, "BIT_RANGE_REG(");
1585 get_parent_region ()->dump_to_pp (pp
, simple
);
1586 pp_string (pp
, ", ");
1587 m_bits
.dump_to_pp (pp
);
1588 pp_string (pp
, ")");
1592 pp_string (pp
, "bit_range_region(");
1593 get_parent_region ()->dump_to_pp (pp
, simple
);
1594 pp_string (pp
, ", ");
1595 m_bits
.dump_to_pp (pp
);
1596 pp_printf (pp
, ")");
1600 /* Implementation of region::get_byte_size vfunc for bit_range_region. */
1603 bit_range_region::get_byte_size (byte_size_t
*out
) const
1605 if (m_bits
.m_size_in_bits
% BITS_PER_UNIT
== 0)
1607 *out
= m_bits
.m_size_in_bits
/ BITS_PER_UNIT
;
1613 /* Implementation of region::get_bit_size vfunc for bit_range_region. */
1616 bit_range_region::get_bit_size (bit_size_t
*out
) const
1618 *out
= m_bits
.m_size_in_bits
;
1622 /* Implementation of region::get_byte_size_sval vfunc for bit_range_region. */
1625 bit_range_region::get_byte_size_sval (region_model_manager
*mgr
) const
1627 if (m_bits
.m_size_in_bits
% BITS_PER_UNIT
!= 0)
1628 return mgr
->get_or_create_unknown_svalue (size_type_node
);
1630 HOST_WIDE_INT num_bytes
= m_bits
.m_size_in_bits
.to_shwi () / BITS_PER_UNIT
;
1631 return mgr
->get_or_create_int_cst (size_type_node
, num_bytes
);
1634 /* Implementation of region::get_relative_concrete_offset vfunc for
1635 bit_range_region. */
1638 bit_range_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1640 *out
= m_bits
.get_start_bit_offset ();
1644 /* class var_arg_region : public region. */
1647 var_arg_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1651 pp_string (pp
, "VAR_ARG_REG(");
1652 get_parent_region ()->dump_to_pp (pp
, simple
);
1653 pp_printf (pp
, ", arg_idx: %d)", m_idx
);
1657 pp_string (pp
, "var_arg_region(");
1658 get_parent_region ()->dump_to_pp (pp
, simple
);
1659 pp_printf (pp
, ", arg_idx: %d)", m_idx
);
1663 /* Get the frame_region for this var_arg_region. */
1665 const frame_region
*
1666 var_arg_region::get_frame_region () const
1668 gcc_assert (get_parent_region ());
1669 return as_a
<const frame_region
*> (get_parent_region ());
1672 /* class unknown_region : public region. */
1674 /* Implementation of region::dump_to_pp vfunc for unknown_region. */
1677 unknown_region::dump_to_pp (pretty_printer
*pp
, bool /*simple*/) const
1679 pp_string (pp
, "UNKNOWN_REGION");
1684 #endif /* #if ENABLE_ANALYZER */