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
.m_buffer
);
597 /* Generate a description of this region. */
599 DEBUG_FUNCTION label_text
600 region::get_desc (bool simple
) const
603 pp_format_decoder (&pp
) = default_tree_printer
;
604 dump_to_pp (&pp
, simple
);
605 return label_text::take (xstrdup (pp_formatted_text (&pp
)));
608 /* Base implementation of region::accept vfunc.
609 Subclass implementations should chain up to this. */
612 region::accept (visitor
*v
) const
614 v
->visit_region (this);
616 m_parent
->accept (v
);
619 /* Return true if this is a symbolic region for deferencing an
621 We shouldn't attempt to bind values for this region (but
622 can unbind values for other regions). */
625 region::symbolic_for_unknown_ptr_p () const
627 if (const symbolic_region
*sym_reg
= dyn_cast_symbolic_region ())
628 if (sym_reg
->get_pointer ()->get_kind () == SK_UNKNOWN
)
633 /* Return true if this is a region for a decl with name DECL_NAME.
634 Intended for use when debugging (for assertions and conditional
638 region::is_named_decl_p (const char *decl_name
) const
640 if (tree decl
= maybe_get_decl ())
642 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (decl
)), decl_name
))
649 region::region (complexity c
, unsigned id
, const region
*parent
, tree type
)
650 : m_complexity (c
), m_id (id
), m_parent (parent
), m_type (type
),
651 m_cached_offset (NULL
)
653 gcc_assert (type
== NULL_TREE
|| TYPE_P (type
));
656 /* Comparator for use by vec<const region *>::qsort,
657 using their IDs to order them. */
660 region::cmp_ptr_ptr (const void *p1
, const void *p2
)
662 const region
* const *reg1
= (const region
* const *)p1
;
663 const region
* const *reg2
= (const region
* const *)p2
;
665 return cmp_ids (*reg1
, *reg2
);
668 /* Determine if a pointer to this region must be non-NULL.
670 Generally, pointers to regions must be non-NULL, but pointers
671 to symbolic_regions might, in fact, be NULL.
673 This allows us to simulate functions like malloc and calloc with:
674 - only one "outcome" from each statement,
675 - the idea that the pointer is on the heap if non-NULL
676 - the possibility that the pointer could be NULL
677 - the idea that successive values returned from malloc are non-equal
678 - to be able to zero-fill for calloc. */
681 region::non_null_p () const
688 /* Are we within a symbolic_region? If so, it could be NULL, and we
689 have to fall back on the constraints. */
691 case RK_HEAP_ALLOCATED
:
696 /* Return true iff this region is defined in terms of SVAL. */
699 region::involves_p (const svalue
*sval
) const
701 if (const symbolic_region
*symbolic_reg
= dyn_cast_symbolic_region ())
703 if (symbolic_reg
->get_pointer ()->involves_p (sval
))
710 /* Comparator for trees to impose a deterministic ordering on
714 tree_cmp (const_tree t1
, const_tree t2
)
719 /* Test tree codes first. */
720 if (TREE_CODE (t1
) != TREE_CODE (t2
))
721 return TREE_CODE (t1
) - TREE_CODE (t2
);
723 /* From this point on, we know T1 and T2 have the same tree code. */
727 if (DECL_NAME (t1
) && DECL_NAME (t2
))
728 return strcmp (IDENTIFIER_POINTER (DECL_NAME (t1
)),
729 IDENTIFIER_POINTER (DECL_NAME (t2
)));
734 else if (DECL_NAME (t2
))
737 return DECL_UID (t1
) - DECL_UID (t2
);
741 switch (TREE_CODE (t1
))
745 if (SSA_NAME_VAR (t1
) && SSA_NAME_VAR (t2
))
747 int var_cmp
= tree_cmp (SSA_NAME_VAR (t1
), SSA_NAME_VAR (t2
));
750 return SSA_NAME_VERSION (t1
) - SSA_NAME_VERSION (t2
);
754 if (SSA_NAME_VAR (t1
))
756 else if (SSA_NAME_VAR (t2
))
759 return SSA_NAME_VERSION (t1
) - SSA_NAME_VERSION (t2
);
765 return tree_int_cst_compare (t1
, t2
);
769 const real_value
*rv1
= TREE_REAL_CST_PTR (t1
);
770 const real_value
*rv2
= TREE_REAL_CST_PTR (t2
);
771 if (real_compare (UNORDERED_EXPR
, rv1
, rv2
))
773 /* Impose an arbitrary order on NaNs relative to other NaNs
775 if (int cmp_isnan
= real_isnan (rv1
) - real_isnan (rv2
))
777 if (int cmp_issignaling_nan
778 = real_issignaling_nan (rv1
) - real_issignaling_nan (rv2
))
779 return cmp_issignaling_nan
;
780 return real_isneg (rv1
) - real_isneg (rv2
);
782 if (real_compare (LT_EXPR
, rv1
, rv2
))
784 if (real_compare (GT_EXPR
, rv1
, rv2
))
790 return strcmp (TREE_STRING_POINTER (t1
),
791 TREE_STRING_POINTER (t2
));
803 /* qsort comparator for trees to impose a deterministic ordering on
807 tree_cmp (const void *p1
, const void *p2
)
809 const_tree t1
= *(const_tree
const *)p1
;
810 const_tree t2
= *(const_tree
const *)p2
;
812 return tree_cmp (t1
, t2
);
815 /* class frame_region : public space_region. */
817 frame_region::~frame_region ()
819 for (map_t::iterator iter
= m_locals
.begin ();
820 iter
!= m_locals
.end ();
822 delete (*iter
).second
;
826 frame_region::accept (visitor
*v
) const
830 m_calling_frame
->accept (v
);
833 /* Implementation of region::dump_to_pp vfunc for frame_region. */
836 frame_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
839 pp_printf (pp
, "frame: %qs@%i", function_name (m_fun
), get_stack_depth ());
841 pp_printf (pp
, "frame_region(%qs, index: %i, depth: %i)",
842 function_name (m_fun
), m_index
, get_stack_depth ());
846 frame_region::get_region_for_local (region_model_manager
*mgr
,
848 const region_model_context
*ctxt
) const
852 /* Verify that EXPR is a local or SSA name, and that it's for the
853 correct function for this stack frame. */
854 gcc_assert (TREE_CODE (expr
) == PARM_DECL
855 || TREE_CODE (expr
) == VAR_DECL
856 || TREE_CODE (expr
) == SSA_NAME
857 || TREE_CODE (expr
) == RESULT_DECL
);
858 switch (TREE_CODE (expr
))
863 gcc_assert (!is_global_var (expr
));
867 gcc_assert (DECL_CONTEXT (expr
) == m_fun
->decl
);
871 if (tree var
= SSA_NAME_VAR (expr
))
874 gcc_assert (DECL_CONTEXT (var
) == m_fun
->decl
);
877 if (const extrinsic_state
*ext_state
= ctxt
->get_ext_state ())
878 if (const supergraph
*sg
879 = ext_state
->get_engine ()->get_supergraph ())
881 const gimple
*def_stmt
= SSA_NAME_DEF_STMT (expr
);
882 const supernode
*snode
883 = sg
->get_supernode_for_stmt (def_stmt
);
884 gcc_assert (snode
->get_function () == m_fun
);
891 /* Ideally we'd use mutable here. */
892 map_t
&mutable_locals
= const_cast <map_t
&> (m_locals
);
894 if (decl_region
**slot
= mutable_locals
.get (expr
))
897 = new decl_region (mgr
->alloc_region_id (), this, expr
);
898 mutable_locals
.put (expr
, reg
);
902 /* class globals_region : public space_region. */
904 /* Implementation of region::dump_to_pp vfunc for globals_region. */
907 globals_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
910 pp_string (pp
, "::");
912 pp_string (pp
, "globals");
915 /* class code_region : public map_region. */
917 /* Implementation of region::dump_to_pp vfunc for code_region. */
920 code_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
923 pp_string (pp
, "code region");
925 pp_string (pp
, "code_region()");
928 /* class function_region : public region. */
930 /* Implementation of region::dump_to_pp vfunc for function_region. */
933 function_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
937 dump_quoted_tree (pp
, m_fndecl
);
941 pp_string (pp
, "function_region(");
942 dump_quoted_tree (pp
, m_fndecl
);
947 /* class label_region : public region. */
949 /* Implementation of region::dump_to_pp vfunc for label_region. */
952 label_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
956 dump_quoted_tree (pp
, m_label
);
960 pp_string (pp
, "label_region(");
961 dump_quoted_tree (pp
, m_label
);
966 /* class stack_region : public region. */
968 /* Implementation of region::dump_to_pp vfunc for stack_region. */
971 stack_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
974 pp_string (pp
, "stack region");
976 pp_string (pp
, "stack_region()");
979 /* class heap_region : public region. */
981 /* Implementation of region::dump_to_pp vfunc for heap_region. */
984 heap_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
987 pp_string (pp
, "heap region");
989 pp_string (pp
, "heap_region()");
992 /* class root_region : public region. */
994 /* root_region's ctor. */
996 root_region::root_region (unsigned id
)
997 : region (complexity (1, 1), id
, NULL
, NULL_TREE
)
1001 /* Implementation of region::dump_to_pp vfunc for root_region. */
1004 root_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1007 pp_string (pp
, "root region");
1009 pp_string (pp
, "root_region()");
1012 /* class symbolic_region : public map_region. */
1014 /* symbolic_region's ctor. */
1016 symbolic_region::symbolic_region (unsigned id
, region
*parent
,
1017 const svalue
*sval_ptr
)
1018 : region (complexity::from_pair (parent
, sval_ptr
), id
, parent
,
1019 (sval_ptr
->get_type ()
1020 ? TREE_TYPE (sval_ptr
->get_type ())
1022 m_sval_ptr (sval_ptr
)
1026 /* Implementation of region::accept vfunc for symbolic_region. */
1029 symbolic_region::accept (visitor
*v
) const
1032 m_sval_ptr
->accept (v
);
1035 /* Implementation of region::dump_to_pp vfunc for symbolic_region. */
1038 symbolic_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1042 pp_string (pp
, "(*");
1043 m_sval_ptr
->dump_to_pp (pp
, simple
);
1044 pp_string (pp
, ")");
1048 pp_string (pp
, "symbolic_region(");
1049 get_parent_region ()->dump_to_pp (pp
, simple
);
1052 pp_string (pp
, ", ");
1053 print_quoted_type (pp
, get_type ());
1055 pp_string (pp
, ", ");
1056 m_sval_ptr
->dump_to_pp (pp
, simple
);
1057 pp_string (pp
, ")");
1061 /* class decl_region : public region. */
1063 /* Implementation of region::dump_to_pp vfunc for decl_region. */
1066 decl_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1069 pp_printf (pp
, "%E", m_decl
);
1072 pp_string (pp
, "decl_region(");
1073 get_parent_region ()->dump_to_pp (pp
, simple
);
1074 pp_string (pp
, ", ");
1075 print_quoted_type (pp
, get_type ());
1076 pp_printf (pp
, ", %qE)", m_decl
);
1080 /* Get the stack depth for the frame containing this decl, or 0
1084 decl_region::get_stack_depth () const
1086 if (get_parent_region () == NULL
)
1088 if (const frame_region
*frame_reg
1089 = get_parent_region ()->dyn_cast_frame_region ())
1090 return frame_reg
->get_stack_depth ();
1094 /* If the underlying decl is in the global constant pool,
1095 return an svalue representing the constant value.
1096 Otherwise return NULL. */
1099 decl_region::maybe_get_constant_value (region_model_manager
*mgr
) const
1101 if (TREE_CODE (m_decl
) == VAR_DECL
1102 && DECL_IN_CONSTANT_POOL (m_decl
)
1103 && DECL_INITIAL (m_decl
)
1104 && TREE_CODE (DECL_INITIAL (m_decl
)) == CONSTRUCTOR
)
1105 return get_svalue_for_constructor (DECL_INITIAL (m_decl
), mgr
);
1109 /* Get an svalue for CTOR, a CONSTRUCTOR for this region's decl. */
1112 decl_region::get_svalue_for_constructor (tree ctor
,
1113 region_model_manager
*mgr
) const
1115 gcc_assert (!TREE_CLOBBER_P (ctor
));
1117 /* Create a binding map, applying ctor to it, using this
1118 decl_region as the base region when building child regions
1119 for offset calculations. */
1121 if (!map
.apply_ctor_to_region (this, ctor
, mgr
))
1122 return mgr
->get_or_create_unknown_svalue (get_type ());
1124 /* Return a compound svalue for the map we built. */
1125 return mgr
->get_or_create_compound_svalue (get_type (), map
);
1128 /* For use on decl_regions for global variables.
1130 Get an svalue for the initial value of this region at entry to
1131 "main" (either based on DECL_INITIAL, or implicit initialization to
1134 Return NULL if there is a problem. */
1137 decl_region::get_svalue_for_initializer (region_model_manager
*mgr
) const
1139 tree init
= DECL_INITIAL (m_decl
);
1142 /* If we have an "extern" decl then there may be an initializer in
1144 if (DECL_EXTERNAL (m_decl
))
1147 /* Implicit initialization to zero; use a compound_svalue for it.
1148 Doing so requires that we have a concrete binding for this region,
1149 which can fail if we have a region with unknown size
1150 (e.g. "extern const char arr[];"). */
1151 const binding_key
*binding
1152 = binding_key::make (mgr
->get_store_manager (), this);
1153 if (binding
->symbolic_p ())
1156 binding_cluster
c (this);
1157 c
.zero_fill_region (mgr
->get_store_manager (), this);
1158 return mgr
->get_or_create_compound_svalue (TREE_TYPE (m_decl
),
1162 /* LTO can write out error_mark_node as the DECL_INITIAL for simple scalar
1163 values (to avoid writing out an extra section). */
1164 if (init
== error_mark_node
)
1167 if (TREE_CODE (init
) == CONSTRUCTOR
)
1168 return get_svalue_for_constructor (init
, mgr
);
1170 /* Reuse the get_rvalue logic from region_model. */
1171 region_model
m (mgr
);
1172 return m
.get_rvalue (path_var (init
, 0), NULL
);
1175 /* Subroutine of symnode_requires_tracking_p; return true if REF
1176 might imply that we should be tracking the value of its decl. */
1179 ipa_ref_requires_tracking (ipa_ref
*ref
)
1181 /* If we have a load/store/alias of the symbol, then we'll track
1182 the decl's value. */
1183 if (ref
->use
!= IPA_REF_ADDR
)
1186 if (ref
->stmt
== NULL
)
1189 switch (ref
->stmt
->code
)
1195 cgraph_node
*caller_cnode
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1196 if (caller_cnode
== NULL
)
1198 cgraph_edge
*edge
= caller_cnode
->get_edge (ref
->stmt
);
1201 if (edge
->callee
== NULL
)
1202 return true; /* e.g. call through function ptr. */
1203 if (edge
->callee
->definition
)
1205 /* If we get here, then this ref is a pointer passed to
1206 a function we don't have the definition for. */
1212 const gasm
*asm_stmt
= as_a
<const gasm
*> (ref
->stmt
);
1213 if (gimple_asm_noutputs (asm_stmt
) > 0)
1215 if (gimple_asm_nclobbers (asm_stmt
) > 0)
1217 /* If we get here, then this ref is the decl being passed
1218 by pointer to asm with no outputs. */
1225 /* Determine if the decl for SYMNODE should have binding_clusters
1226 in our state objects; return false to optimize away tracking
1227 certain decls in our state objects, as an optimization. */
1230 symnode_requires_tracking_p (symtab_node
*symnode
)
1232 gcc_assert (symnode
);
1233 if (symnode
->externally_visible
)
1235 tree context_fndecl
= DECL_CONTEXT (symnode
->decl
);
1236 if (context_fndecl
== NULL
)
1238 if (TREE_CODE (context_fndecl
) != FUNCTION_DECL
)
1240 for (auto ref
: symnode
->ref_list
.referring
)
1241 if (ipa_ref_requires_tracking (ref
))
1244 /* If we get here, then we don't have uses of this decl that require
1245 tracking; we never read from it or write to it explicitly. */
1249 /* Subroutine of decl_region ctor: determine whether this decl_region
1250 can have binding_clusters; return false to optimize away tracking
1251 of certain decls in our state objects, as an optimization. */
1254 decl_region::calc_tracked_p (tree decl
)
1256 /* Precondition of symtab_node::get. */
1257 if (TREE_CODE (decl
) == VAR_DECL
1258 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
) || in_lto_p
))
1259 if (symtab_node
*symnode
= symtab_node::get (decl
))
1260 return symnode_requires_tracking_p (symnode
);
1264 /* class field_region : public region. */
1266 /* Implementation of region::dump_to_pp vfunc for field_region. */
1269 field_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1273 get_parent_region ()->dump_to_pp (pp
, simple
);
1274 pp_string (pp
, ".");
1275 pp_printf (pp
, "%E", m_field
);
1279 pp_string (pp
, "field_region(");
1280 get_parent_region ()->dump_to_pp (pp
, simple
);
1281 pp_string (pp
, ", ");
1282 print_quoted_type (pp
, get_type ());
1283 pp_printf (pp
, ", %qE)", m_field
);
1287 /* Implementation of region::get_relative_concrete_offset vfunc
1288 for field_region. */
1291 field_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1293 /* Compare with e.g. gimple-fold.cc's
1294 fold_nonarray_ctor_reference. */
1295 tree byte_offset
= DECL_FIELD_OFFSET (m_field
);
1296 if (TREE_CODE (byte_offset
) != INTEGER_CST
)
1298 tree field_offset
= DECL_FIELD_BIT_OFFSET (m_field
);
1299 /* Compute bit offset of the field. */
1300 offset_int bitoffset
1301 = (wi::to_offset (field_offset
)
1302 + (wi::to_offset (byte_offset
) << LOG2_BITS_PER_UNIT
));
1307 /* class element_region : public region. */
1309 /* Implementation of region::accept vfunc for element_region. */
1312 element_region::accept (visitor
*v
) const
1315 m_index
->accept (v
);
1318 /* Implementation of region::dump_to_pp vfunc for element_region. */
1321 element_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1325 //pp_string (pp, "(");
1326 get_parent_region ()->dump_to_pp (pp
, simple
);
1327 pp_string (pp
, "[");
1328 m_index
->dump_to_pp (pp
, simple
);
1329 pp_string (pp
, "]");
1330 //pp_string (pp, ")");
1334 pp_string (pp
, "element_region(");
1335 get_parent_region ()->dump_to_pp (pp
, simple
);
1336 pp_string (pp
, ", ");
1337 print_quoted_type (pp
, get_type ());
1338 pp_string (pp
, ", ");
1339 m_index
->dump_to_pp (pp
, simple
);
1340 pp_printf (pp
, ")");
1344 /* Implementation of region::get_relative_concrete_offset vfunc
1345 for element_region. */
1348 element_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1350 if (tree idx_cst
= m_index
->maybe_get_constant ())
1352 gcc_assert (TREE_CODE (idx_cst
) == INTEGER_CST
);
1354 tree elem_type
= get_type ();
1355 offset_int element_idx
= wi::to_offset (idx_cst
);
1357 /* First, use int_size_in_bytes, to reject the case where we
1358 have an incomplete type, or a non-constant value. */
1359 HOST_WIDE_INT hwi_byte_size
= int_size_in_bytes (elem_type
);
1360 if (hwi_byte_size
> 0)
1362 offset_int element_bit_size
1363 = hwi_byte_size
<< LOG2_BITS_PER_UNIT
;
1364 offset_int element_bit_offset
1365 = element_idx
* element_bit_size
;
1366 *out
= element_bit_offset
;
1373 /* class offset_region : public region. */
1375 /* Implementation of region::accept vfunc for offset_region. */
1378 offset_region::accept (visitor
*v
) const
1381 m_byte_offset
->accept (v
);
1384 /* Implementation of region::dump_to_pp vfunc for offset_region. */
1387 offset_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1391 //pp_string (pp, "(");
1392 get_parent_region ()->dump_to_pp (pp
, simple
);
1393 pp_string (pp
, "+");
1394 m_byte_offset
->dump_to_pp (pp
, simple
);
1395 //pp_string (pp, ")");
1399 pp_string (pp
, "offset_region(");
1400 get_parent_region ()->dump_to_pp (pp
, simple
);
1401 pp_string (pp
, ", ");
1402 print_quoted_type (pp
, get_type ());
1403 pp_string (pp
, ", ");
1404 m_byte_offset
->dump_to_pp (pp
, simple
);
1405 pp_printf (pp
, ")");
1409 /* Implementation of region::get_relative_concrete_offset vfunc
1410 for offset_region. */
1413 offset_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1415 if (tree byte_offset_cst
= m_byte_offset
->maybe_get_constant ())
1417 gcc_assert (TREE_CODE (byte_offset_cst
) == INTEGER_CST
);
1418 /* Use a signed value for the byte offset, to handle
1419 negative offsets. */
1420 HOST_WIDE_INT byte_offset
1421 = wi::to_offset (byte_offset_cst
).to_shwi ();
1422 HOST_WIDE_INT bit_offset
= byte_offset
* BITS_PER_UNIT
;
1429 /* class sized_region : public region. */
1431 /* Implementation of region::accept vfunc for sized_region. */
1434 sized_region::accept (visitor
*v
) const
1437 m_byte_size_sval
->accept (v
);
1440 /* Implementation of region::dump_to_pp vfunc for sized_region. */
1443 sized_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1447 pp_string (pp
, "SIZED_REG(");
1448 get_parent_region ()->dump_to_pp (pp
, simple
);
1449 pp_string (pp
, ", ");
1450 m_byte_size_sval
->dump_to_pp (pp
, simple
);
1451 pp_string (pp
, ")");
1455 pp_string (pp
, "sized_region(");
1456 get_parent_region ()->dump_to_pp (pp
, simple
);
1457 pp_string (pp
, ", ");
1458 m_byte_size_sval
->dump_to_pp (pp
, simple
);
1459 pp_printf (pp
, ")");
1463 /* Implementation of region::get_byte_size vfunc for sized_region. */
1466 sized_region::get_byte_size (byte_size_t
*out
) const
1468 if (tree cst
= m_byte_size_sval
->maybe_get_constant ())
1470 gcc_assert (TREE_CODE (cst
) == INTEGER_CST
);
1471 *out
= tree_to_uhwi (cst
);
1477 /* Implementation of region::get_bit_size vfunc for sized_region. */
1480 sized_region::get_bit_size (bit_size_t
*out
) const
1482 byte_size_t byte_size
;
1483 if (!get_byte_size (&byte_size
))
1485 *out
= byte_size
* BITS_PER_UNIT
;
1489 /* class cast_region : public region. */
1491 /* Implementation of region::accept vfunc for cast_region. */
1494 cast_region::accept (visitor
*v
) const
1497 m_original_region
->accept (v
);
1500 /* Implementation of region::dump_to_pp vfunc for cast_region. */
1503 cast_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1507 pp_string (pp
, "CAST_REG(");
1508 print_quoted_type (pp
, get_type ());
1509 pp_string (pp
, ", ");
1510 m_original_region
->dump_to_pp (pp
, simple
);
1511 pp_string (pp
, ")");
1515 pp_string (pp
, "cast_region(");
1516 m_original_region
->dump_to_pp (pp
, simple
);
1517 pp_string (pp
, ", ");
1518 print_quoted_type (pp
, get_type ());
1519 pp_printf (pp
, ")");
1523 /* class heap_allocated_region : public region. */
1525 /* Implementation of region::dump_to_pp vfunc for heap_allocated_region. */
1528 heap_allocated_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1531 pp_printf (pp
, "HEAP_ALLOCATED_REGION(%i)", get_id ());
1533 pp_printf (pp
, "heap_allocated_region(%i)", get_id ());
1536 /* class alloca_region : public region. */
1538 /* Implementation of region::dump_to_pp vfunc for alloca_region. */
1541 alloca_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1544 pp_printf (pp
, "ALLOCA_REGION(%i)", get_id ());
1546 pp_printf (pp
, "alloca_region(%i)", get_id ());
1549 /* class string_region : public region. */
1551 /* Implementation of region::dump_to_pp vfunc for string_region. */
1554 string_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1557 dump_tree (pp
, m_string_cst
);
1560 pp_string (pp
, "string_region(");
1561 dump_tree (pp
, m_string_cst
);
1562 if (!flag_dump_noaddr
)
1564 pp_string (pp
, " (");
1565 pp_pointer (pp
, m_string_cst
);
1566 pp_string (pp
, "))");
1571 /* class bit_range_region : public region. */
1573 /* Implementation of region::dump_to_pp vfunc for bit_range_region. */
1576 bit_range_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1580 pp_string (pp
, "BIT_RANGE_REG(");
1581 get_parent_region ()->dump_to_pp (pp
, simple
);
1582 pp_string (pp
, ", ");
1583 m_bits
.dump_to_pp (pp
);
1584 pp_string (pp
, ")");
1588 pp_string (pp
, "bit_range_region(");
1589 get_parent_region ()->dump_to_pp (pp
, simple
);
1590 pp_string (pp
, ", ");
1591 m_bits
.dump_to_pp (pp
);
1592 pp_printf (pp
, ")");
1596 /* Implementation of region::get_byte_size vfunc for bit_range_region. */
1599 bit_range_region::get_byte_size (byte_size_t
*out
) const
1601 if (m_bits
.m_size_in_bits
% BITS_PER_UNIT
== 0)
1603 *out
= m_bits
.m_size_in_bits
/ BITS_PER_UNIT
;
1609 /* Implementation of region::get_bit_size vfunc for bit_range_region. */
1612 bit_range_region::get_bit_size (bit_size_t
*out
) const
1614 *out
= m_bits
.m_size_in_bits
;
1618 /* Implementation of region::get_byte_size_sval vfunc for bit_range_region. */
1621 bit_range_region::get_byte_size_sval (region_model_manager
*mgr
) const
1623 if (m_bits
.m_size_in_bits
% BITS_PER_UNIT
!= 0)
1624 return mgr
->get_or_create_unknown_svalue (size_type_node
);
1626 HOST_WIDE_INT num_bytes
= m_bits
.m_size_in_bits
.to_shwi () / BITS_PER_UNIT
;
1627 return mgr
->get_or_create_int_cst (size_type_node
, num_bytes
);
1630 /* Implementation of region::get_relative_concrete_offset vfunc for
1631 bit_range_region. */
1634 bit_range_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1636 *out
= m_bits
.get_start_bit_offset ();
1640 /* class var_arg_region : public region. */
1643 var_arg_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1647 pp_string (pp
, "VAR_ARG_REG(");
1648 get_parent_region ()->dump_to_pp (pp
, simple
);
1649 pp_printf (pp
, ", arg_idx: %d)", m_idx
);
1653 pp_string (pp
, "var_arg_region(");
1654 get_parent_region ()->dump_to_pp (pp
, simple
);
1655 pp_printf (pp
, ", arg_idx: %d)", m_idx
);
1659 /* Get the frame_region for this var_arg_region. */
1661 const frame_region
*
1662 var_arg_region::get_frame_region () const
1664 gcc_assert (get_parent_region ());
1665 return as_a
<const frame_region
*> (get_parent_region ());
1668 /* class unknown_region : public region. */
1670 /* Implementation of region::dump_to_pp vfunc for unknown_region. */
1673 unknown_region::dump_to_pp (pretty_printer
*pp
, bool /*simple*/) const
1675 pp_string (pp
, "UNKNOWN_REGION");
1680 #endif /* #if ENABLE_ANALYZER */