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/>. */
22 #define INCLUDE_MEMORY
24 #include "coretypes.h"
26 #include "diagnostic-core.h"
27 #include "gimple-pretty-print.h"
29 #include "basic-block.h"
31 #include "gimple-iterator.h"
32 #include "diagnostic-core.h"
37 #include "stringpool.h"
40 #include "fold-const.h"
41 #include "tree-pretty-print.h"
42 #include "diagnostic-color.h"
43 #include "diagnostic-metadata.h"
45 #include "analyzer/analyzer.h"
46 #include "analyzer/analyzer-logging.h"
47 #include "ordered-hash-map.h"
52 #include "analyzer/supergraph.h"
54 #include "analyzer/call-string.h"
55 #include "analyzer/program-point.h"
56 #include "analyzer/store.h"
57 #include "analyzer/region.h"
58 #include "analyzer/region-model.h"
59 #include "analyzer/sm.h"
60 #include "analyzer/program-state.h"
66 /* class region and its various subclasses. */
72 delete m_cached_offset
;
75 /* Compare REG1 and REG2 by id. */
78 region::cmp_ids (const region
*reg1
, const region
*reg2
)
80 return (long)reg1
->get_id () - (long)reg2
->get_id ();
83 /* Determine the base region for this region: when considering bindings
84 for this region, the base region is the ancestor which identifies
85 which cluster they should be partitioned into.
86 Regions within the same struct/union/array are in the same cluster.
87 Different decls are in different clusters. */
90 region::get_base_region () const
92 const region
*iter
= this;
95 switch (iter
->get_kind ())
102 iter
= iter
->get_parent_region ();
105 iter
= iter
->dyn_cast_cast_region ()->get_original_region ();
114 /* Return true if get_base_region() == this for this region. */
117 region::base_region_p () const
121 /* Region kinds representing a descendent of a base region. */
135 /* Return true if this region is ELDER or one of its descendents. */
138 region::descendent_of_p (const region
*elder
) const
140 const region
*iter
= this;
145 if (iter
->get_kind () == RK_CAST
)
146 iter
= iter
->dyn_cast_cast_region ()->get_original_region ();
148 iter
= iter
->get_parent_region ();
153 /* If this region is a frame_region, or a descendent of one, return it.
154 Otherwise return NULL. */
157 region::maybe_get_frame_region () const
159 const region
*iter
= this;
162 if (const frame_region
*frame_reg
= iter
->dyn_cast_frame_region ())
164 if (iter
->get_kind () == RK_CAST
)
165 iter
= iter
->dyn_cast_cast_region ()->get_original_region ();
167 iter
= iter
->get_parent_region ();
172 /* Get the memory space of this region. */
175 region::get_memory_space () const
177 const region
*iter
= this;
180 switch (iter
->get_kind ())
185 return MEMSPACE_GLOBALS
;
189 return MEMSPACE_CODE
;
193 return MEMSPACE_STACK
;
195 case RK_HEAP_ALLOCATED
:
196 return MEMSPACE_HEAP
;
198 return MEMSPACE_READONLY_DATA
;
200 if (iter
->get_kind () == RK_CAST
)
201 iter
= iter
->dyn_cast_cast_region ()->get_original_region ();
203 iter
= iter
->get_parent_region ();
205 return MEMSPACE_UNKNOWN
;
208 /* Subroutine for use by region_model_manager::get_or_create_initial_value.
209 Return true if this region has an initial_svalue.
210 Return false if attempting to use INIT_VAL(this_region) should give
211 the "UNINITIALIZED" poison value. */
214 region::can_have_initial_svalue_p () const
216 const region
*base_reg
= get_base_region ();
218 /* Check for memory spaces that are uninitialized by default. */
219 enum memory_space mem_space
= base_reg
->get_memory_space ();
224 case MEMSPACE_UNKNOWN
:
226 case MEMSPACE_GLOBALS
:
227 case MEMSPACE_READONLY_DATA
:
228 /* Such regions have initial_svalues. */
232 /* Heap allocations are uninitialized by default. */
236 if (tree decl
= base_reg
->maybe_get_decl ())
238 /* See the assertion in frame_region::get_region_for_local for the
239 tree codes we need to handle here. */
240 switch (TREE_CODE (decl
))
246 /* Parameters have initial values. */
251 /* Function locals don't have initial values. */
256 tree ssa_name
= decl
;
257 /* SSA names that are the default defn of a PARM_DECL
258 have initial_svalues; other SSA names don't. */
259 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name
)
260 && SSA_NAME_VAR (ssa_name
)
261 && TREE_CODE (SSA_NAME_VAR (ssa_name
)) == PARM_DECL
)
269 /* If we have an on-stack region that isn't associated with a decl
270 or SSA name, then we have VLA/alloca, which is uninitialized. */
275 /* If this region is a decl_region, return the decl.
276 Otherwise return NULL. */
279 region::maybe_get_decl () const
281 if (const decl_region
*decl_reg
= dyn_cast_decl_region ())
282 return decl_reg
->get_decl ();
286 /* Get the region_offset for this region (calculating it on the
287 first call and caching it internally). */
290 region::get_offset (region_model_manager
*mgr
) const
293 m_cached_offset
= new region_offset (calc_offset (mgr
));
294 return *m_cached_offset
;
297 /* Base class implementation of region::get_byte_size vfunc.
298 If the size of this region (in bytes) is known statically, write it to *OUT
300 Otherwise return false. */
303 region::get_byte_size (byte_size_t
*out
) const
305 tree type
= get_type ();
307 /* Bail out e.g. for heap-allocated regions. */
311 HOST_WIDE_INT bytes
= int_size_in_bytes (type
);
318 /* Base implementation of region::get_byte_size_sval vfunc. */
321 region::get_byte_size_sval (region_model_manager
*mgr
) const
323 tree type
= get_type ();
325 /* Bail out e.g. for heap-allocated regions. */
327 return mgr
->get_or_create_unknown_svalue (size_type_node
);
329 HOST_WIDE_INT bytes
= int_size_in_bytes (type
);
331 return mgr
->get_or_create_unknown_svalue (size_type_node
);
333 tree byte_size
= size_in_bytes (type
);
334 if (TREE_TYPE (byte_size
) != size_type_node
)
335 byte_size
= fold_build1 (NOP_EXPR
, size_type_node
, byte_size
);
336 return mgr
->get_or_create_constant_svalue (byte_size
);
339 /* Attempt to get the size of TYPE in bits.
340 If successful, return true and write the size to *OUT.
341 Otherwise return false. */
344 int_size_in_bits (const_tree type
, bit_size_t
*out
)
346 if (INTEGRAL_TYPE_P (type
))
348 *out
= TYPE_PRECISION (type
);
352 tree sz
= TYPE_SIZE (type
);
353 if (sz
&& tree_fits_uhwi_p (sz
))
355 *out
= TREE_INT_CST_LOW (sz
);
362 /* If the size of this region (in bits) is known statically, write it to *OUT
364 Otherwise return false. */
367 region::get_bit_size (bit_size_t
*out
) const
369 tree type
= get_type ();
371 /* Bail out e.g. for heap-allocated regions. */
375 return int_size_in_bits (type
, out
);
378 /* Get the field within RECORD_TYPE at BIT_OFFSET. */
381 get_field_at_bit_offset (tree record_type
, bit_offset_t bit_offset
)
383 gcc_assert (TREE_CODE (record_type
) == RECORD_TYPE
);
387 /* Find the first field that has an offset > BIT_OFFSET,
388 then return the one preceding it.
389 Skip other trees within the chain, such as FUNCTION_DECLs. */
390 tree last_field
= NULL_TREE
;
391 for (tree iter
= TYPE_FIELDS (record_type
); iter
!= NULL_TREE
;
392 iter
= DECL_CHAIN (iter
))
394 if (TREE_CODE (iter
) == FIELD_DECL
)
396 int iter_field_offset
= int_bit_position (iter
);
397 if (bit_offset
< iter_field_offset
)
405 /* Populate *OUT with descendent regions of type TYPE that match
406 RELATIVE_BIT_OFFSET and SIZE_IN_BITS within this region. */
409 region::get_subregions_for_binding (region_model_manager
*mgr
,
410 bit_offset_t relative_bit_offset
,
411 bit_size_t size_in_bits
,
413 auto_vec
<const region
*> *out
) const
415 if (get_type () == NULL_TREE
|| type
== NULL_TREE
)
417 if (relative_bit_offset
== 0
418 && types_compatible_p (get_type (), type
))
420 out
->safe_push (this);
423 switch (TREE_CODE (get_type ()))
427 tree element_type
= TREE_TYPE (get_type ());
428 HOST_WIDE_INT hwi_byte_size
= int_size_in_bytes (element_type
);
429 if (hwi_byte_size
> 0)
431 HOST_WIDE_INT bits_per_element
432 = hwi_byte_size
<< LOG2_BITS_PER_UNIT
;
433 HOST_WIDE_INT element_index
434 = (relative_bit_offset
.to_shwi () / bits_per_element
);
435 tree element_index_cst
436 = build_int_cst (integer_type_node
, element_index
);
437 HOST_WIDE_INT inner_bit_offset
438 = relative_bit_offset
.to_shwi () % bits_per_element
;
439 const region
*subregion
= mgr
->get_element_region
441 mgr
->get_or_create_constant_svalue (element_index_cst
));
442 subregion
->get_subregions_for_binding (mgr
, inner_bit_offset
,
443 size_in_bits
, type
, out
);
449 /* The bit offset might be *within* one of the fields (such as
450 with nested structs).
451 So we want to find the enclosing field, adjust the offset,
453 if (tree field
= get_field_at_bit_offset (get_type (),
454 relative_bit_offset
))
456 int field_bit_offset
= int_bit_position (field
);
457 const region
*subregion
= mgr
->get_field_region (this, field
);
458 subregion
->get_subregions_for_binding
459 (mgr
, relative_bit_offset
- field_bit_offset
,
460 size_in_bits
, type
, out
);
466 for (tree field
= TYPE_FIELDS (get_type ()); field
!= NULL_TREE
;
467 field
= DECL_CHAIN (field
))
469 if (TREE_CODE (field
) != FIELD_DECL
)
471 const region
*subregion
= mgr
->get_field_region (this, field
);
472 subregion
->get_subregions_for_binding (mgr
,
486 /* Walk from this region up to the base region within its cluster, calculating
487 the offset relative to the base region, either as an offset in bits,
488 or a symbolic offset. */
491 region::calc_offset (region_model_manager
*mgr
) const
493 const region
*iter_region
= this;
494 bit_offset_t accum_bit_offset
= 0;
495 const svalue
*accum_byte_sval
= NULL
;
499 switch (iter_region
->get_kind ())
508 = iter_region
->get_relative_symbolic_offset (mgr
);
510 = mgr
->get_or_create_binop (sval
->get_type (), PLUS_EXPR
,
511 accum_byte_sval
, sval
);
512 iter_region
= iter_region
->get_parent_region ();
516 bit_offset_t rel_bit_offset
;
517 if (iter_region
->get_relative_concrete_offset (&rel_bit_offset
))
519 accum_bit_offset
+= rel_bit_offset
;
520 iter_region
= iter_region
->get_parent_region ();
524 /* If the iter_region is not concrete anymore, convert the
525 accumulated bits to a svalue in bytes and revisit the
526 iter_region collecting the symbolic value. */
527 byte_offset_t byte_offset
= accum_bit_offset
/ BITS_PER_UNIT
;
528 tree offset_tree
= wide_int_to_tree (integer_type_node
,
531 = mgr
->get_or_create_constant_svalue (offset_tree
);
536 iter_region
= iter_region
->get_parent_region ();
541 const cast_region
*cast_reg
542 = as_a
<const cast_region
*> (iter_region
);
543 iter_region
= cast_reg
->get_original_region ();
548 return accum_byte_sval
549 ? region_offset::make_symbolic (iter_region
,
551 : region_offset::make_concrete (iter_region
,
556 return accum_byte_sval
? region_offset::make_symbolic (iter_region
,
558 : region_offset::make_concrete (iter_region
,
562 /* Base implementation of region::get_relative_concrete_offset vfunc. */
565 region::get_relative_concrete_offset (bit_offset_t
*) const
570 /* Base implementation of region::get_relative_symbolic_offset vfunc. */
573 region::get_relative_symbolic_offset (region_model_manager
*mgr
) const
575 return mgr
->get_or_create_unknown_svalue (integer_type_node
);
578 /* Attempt to get the position and size of this region expressed as a
579 concrete range of bytes relative to its parent.
580 If successful, return true and write to *OUT.
581 Otherwise return false. */
584 region::get_relative_concrete_byte_range (byte_range
*out
) const
586 /* We must have a concrete offset relative to the parent. */
587 bit_offset_t rel_bit_offset
;
588 if (!get_relative_concrete_offset (&rel_bit_offset
))
590 /* ...which must be a whole number of bytes. */
591 if (rel_bit_offset
% BITS_PER_UNIT
!= 0)
593 byte_offset_t start_byte_offset
= rel_bit_offset
/ BITS_PER_UNIT
;
595 /* We must have a concrete size, which must be a whole number
597 byte_size_t num_bytes
;
598 if (!get_byte_size (&num_bytes
))
602 *out
= byte_range (start_byte_offset
, num_bytes
);
606 /* Dump a description of this region to stderr. */
609 region::dump (bool simple
) const
612 pp_format_decoder (&pp
) = default_tree_printer
;
613 pp_show_color (&pp
) = pp_show_color (global_dc
->printer
);
614 pp
.buffer
->stream
= stderr
;
615 dump_to_pp (&pp
, simple
);
620 /* Return a new json::string describing the region. */
623 region::to_json () const
625 label_text desc
= get_desc (true);
626 json::value
*reg_js
= new json::string (desc
.get ());
630 /* Generate a description of this region. */
632 DEBUG_FUNCTION label_text
633 region::get_desc (bool simple
) const
636 pp_format_decoder (&pp
) = default_tree_printer
;
637 dump_to_pp (&pp
, simple
);
638 return label_text::take (xstrdup (pp_formatted_text (&pp
)));
641 /* Base implementation of region::accept vfunc.
642 Subclass implementations should chain up to this. */
645 region::accept (visitor
*v
) const
647 v
->visit_region (this);
649 m_parent
->accept (v
);
652 /* Return true if this is a symbolic region for deferencing an
654 We shouldn't attempt to bind values for this region (but
655 can unbind values for other regions). */
658 region::symbolic_for_unknown_ptr_p () const
660 if (const symbolic_region
*sym_reg
= dyn_cast_symbolic_region ())
661 if (sym_reg
->get_pointer ()->get_kind () == SK_UNKNOWN
)
666 /* Return true if this is a symbolic region. */
669 region::symbolic_p () const
671 return get_kind () == RK_SYMBOLIC
;
674 /* Return true if this is a region for a decl with name DECL_NAME.
675 Intended for use when debugging (for assertions and conditional
679 region::is_named_decl_p (const char *decl_name
) const
681 if (tree decl
= maybe_get_decl ())
683 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (decl
)), decl_name
))
690 region::region (complexity c
, unsigned id
, const region
*parent
, tree type
)
691 : m_complexity (c
), m_id (id
), m_parent (parent
), m_type (type
),
692 m_cached_offset (NULL
)
694 gcc_assert (type
== NULL_TREE
|| TYPE_P (type
));
697 /* Comparator for use by vec<const region *>::qsort,
698 using their IDs to order them. */
701 region::cmp_ptr_ptr (const void *p1
, const void *p2
)
703 const region
* const *reg1
= (const region
* const *)p1
;
704 const region
* const *reg2
= (const region
* const *)p2
;
706 return cmp_ids (*reg1
, *reg2
);
709 /* Determine if a pointer to this region must be non-NULL.
711 Generally, pointers to regions must be non-NULL, but pointers
712 to symbolic_regions might, in fact, be NULL.
714 This allows us to simulate functions like malloc and calloc with:
715 - only one "outcome" from each statement,
716 - the idea that the pointer is on the heap if non-NULL
717 - the possibility that the pointer could be NULL
718 - the idea that successive values returned from malloc are non-equal
719 - to be able to zero-fill for calloc. */
722 region::non_null_p () const
729 /* Are we within a symbolic_region? If so, it could be NULL, and we
730 have to fall back on the constraints. */
732 case RK_HEAP_ALLOCATED
:
737 /* Return true iff this region is defined in terms of SVAL. */
740 region::involves_p (const svalue
*sval
) const
742 if (const symbolic_region
*symbolic_reg
= dyn_cast_symbolic_region ())
744 if (symbolic_reg
->get_pointer ()->involves_p (sval
))
751 /* Comparator for trees to impose a deterministic ordering on
755 tree_cmp (const_tree t1
, const_tree t2
)
760 /* Test tree codes first. */
761 if (TREE_CODE (t1
) != TREE_CODE (t2
))
762 return TREE_CODE (t1
) - TREE_CODE (t2
);
764 /* From this point on, we know T1 and T2 have the same tree code. */
768 if (DECL_NAME (t1
) && DECL_NAME (t2
))
769 return strcmp (IDENTIFIER_POINTER (DECL_NAME (t1
)),
770 IDENTIFIER_POINTER (DECL_NAME (t2
)));
775 else if (DECL_NAME (t2
))
778 return DECL_UID (t1
) - DECL_UID (t2
);
782 switch (TREE_CODE (t1
))
786 if (SSA_NAME_VAR (t1
) && SSA_NAME_VAR (t2
))
788 int var_cmp
= tree_cmp (SSA_NAME_VAR (t1
), SSA_NAME_VAR (t2
));
791 return SSA_NAME_VERSION (t1
) - SSA_NAME_VERSION (t2
);
795 if (SSA_NAME_VAR (t1
))
797 else if (SSA_NAME_VAR (t2
))
800 return SSA_NAME_VERSION (t1
) - SSA_NAME_VERSION (t2
);
806 return tree_int_cst_compare (t1
, t2
);
810 const real_value
*rv1
= TREE_REAL_CST_PTR (t1
);
811 const real_value
*rv2
= TREE_REAL_CST_PTR (t2
);
812 if (real_compare (UNORDERED_EXPR
, rv1
, rv2
))
814 /* Impose an arbitrary order on NaNs relative to other NaNs
816 if (int cmp_isnan
= real_isnan (rv1
) - real_isnan (rv2
))
818 if (int cmp_issignaling_nan
819 = real_issignaling_nan (rv1
) - real_issignaling_nan (rv2
))
820 return cmp_issignaling_nan
;
821 return real_isneg (rv1
) - real_isneg (rv2
);
823 if (real_compare (LT_EXPR
, rv1
, rv2
))
825 if (real_compare (GT_EXPR
, rv1
, rv2
))
831 return strcmp (TREE_STRING_POINTER (t1
),
832 TREE_STRING_POINTER (t2
));
844 /* qsort comparator for trees to impose a deterministic ordering on
848 tree_cmp (const void *p1
, const void *p2
)
850 const_tree t1
= *(const_tree
const *)p1
;
851 const_tree t2
= *(const_tree
const *)p2
;
853 return tree_cmp (t1
, t2
);
856 /* class frame_region : public space_region. */
858 frame_region::~frame_region ()
860 for (map_t::iterator iter
= m_locals
.begin ();
861 iter
!= m_locals
.end ();
863 delete (*iter
).second
;
867 frame_region::accept (visitor
*v
) const
871 m_calling_frame
->accept (v
);
874 /* Implementation of region::dump_to_pp vfunc for frame_region. */
877 frame_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
880 pp_printf (pp
, "frame: %qs@%i", function_name (m_fun
), get_stack_depth ());
882 pp_printf (pp
, "frame_region(%qs, index: %i, depth: %i)",
883 function_name (m_fun
), m_index
, get_stack_depth ());
887 frame_region::get_region_for_local (region_model_manager
*mgr
,
889 const region_model_context
*ctxt
) const
893 /* Verify that EXPR is a local or SSA name, and that it's for the
894 correct function for this stack frame. */
895 gcc_assert (TREE_CODE (expr
) == PARM_DECL
896 || TREE_CODE (expr
) == VAR_DECL
897 || TREE_CODE (expr
) == SSA_NAME
898 || TREE_CODE (expr
) == RESULT_DECL
);
899 switch (TREE_CODE (expr
))
904 gcc_assert (!is_global_var (expr
));
908 gcc_assert (DECL_CONTEXT (expr
) == m_fun
->decl
);
912 if (tree var
= SSA_NAME_VAR (expr
))
915 gcc_assert (DECL_CONTEXT (var
) == m_fun
->decl
);
918 if (const extrinsic_state
*ext_state
= ctxt
->get_ext_state ())
919 if (const supergraph
*sg
920 = ext_state
->get_engine ()->get_supergraph ())
922 const gimple
*def_stmt
= SSA_NAME_DEF_STMT (expr
);
923 const supernode
*snode
924 = sg
->get_supernode_for_stmt (def_stmt
);
925 gcc_assert (snode
->get_function () == m_fun
);
932 /* Ideally we'd use mutable here. */
933 map_t
&mutable_locals
= const_cast <map_t
&> (m_locals
);
935 if (decl_region
**slot
= mutable_locals
.get (expr
))
938 = new decl_region (mgr
->alloc_region_id (), this, expr
);
939 mutable_locals
.put (expr
, reg
);
943 /* class globals_region : public space_region. */
945 /* Implementation of region::dump_to_pp vfunc for globals_region. */
948 globals_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
951 pp_string (pp
, "::");
953 pp_string (pp
, "globals");
956 /* class code_region : public map_region. */
958 /* Implementation of region::dump_to_pp vfunc for code_region. */
961 code_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
964 pp_string (pp
, "code region");
966 pp_string (pp
, "code_region()");
969 /* class function_region : public region. */
971 /* Implementation of region::dump_to_pp vfunc for function_region. */
974 function_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
978 dump_quoted_tree (pp
, m_fndecl
);
982 pp_string (pp
, "function_region(");
983 dump_quoted_tree (pp
, m_fndecl
);
988 /* class label_region : public region. */
990 /* Implementation of region::dump_to_pp vfunc for label_region. */
993 label_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
997 dump_quoted_tree (pp
, m_label
);
1001 pp_string (pp
, "label_region(");
1002 dump_quoted_tree (pp
, m_label
);
1003 pp_string (pp
, ")");
1007 /* class stack_region : public region. */
1009 /* Implementation of region::dump_to_pp vfunc for stack_region. */
1012 stack_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1015 pp_string (pp
, "stack region");
1017 pp_string (pp
, "stack_region()");
1020 /* class heap_region : public region. */
1022 /* Implementation of region::dump_to_pp vfunc for heap_region. */
1025 heap_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1028 pp_string (pp
, "heap region");
1030 pp_string (pp
, "heap_region()");
1033 /* class root_region : public region. */
1035 /* root_region's ctor. */
1037 root_region::root_region (unsigned id
)
1038 : region (complexity (1, 1), id
, NULL
, NULL_TREE
)
1042 /* Implementation of region::dump_to_pp vfunc for root_region. */
1045 root_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1048 pp_string (pp
, "root region");
1050 pp_string (pp
, "root_region()");
1053 /* class symbolic_region : public map_region. */
1055 /* symbolic_region's ctor. */
1057 symbolic_region::symbolic_region (unsigned id
, region
*parent
,
1058 const svalue
*sval_ptr
)
1059 : region (complexity::from_pair (parent
, sval_ptr
), id
, parent
,
1060 (sval_ptr
->get_type ()
1061 ? TREE_TYPE (sval_ptr
->get_type ())
1063 m_sval_ptr (sval_ptr
)
1067 /* Implementation of region::accept vfunc for symbolic_region. */
1070 symbolic_region::accept (visitor
*v
) const
1073 m_sval_ptr
->accept (v
);
1076 /* Implementation of region::dump_to_pp vfunc for symbolic_region. */
1079 symbolic_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1083 pp_string (pp
, "(*");
1084 m_sval_ptr
->dump_to_pp (pp
, simple
);
1085 pp_string (pp
, ")");
1089 pp_string (pp
, "symbolic_region(");
1090 get_parent_region ()->dump_to_pp (pp
, simple
);
1093 pp_string (pp
, ", ");
1094 print_quoted_type (pp
, get_type ());
1096 pp_string (pp
, ", ");
1097 m_sval_ptr
->dump_to_pp (pp
, simple
);
1098 pp_string (pp
, ")");
1102 /* class decl_region : public region. */
1104 /* Implementation of region::dump_to_pp vfunc for decl_region. */
1107 decl_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1110 pp_printf (pp
, "%E", m_decl
);
1113 pp_string (pp
, "decl_region(");
1114 get_parent_region ()->dump_to_pp (pp
, simple
);
1115 pp_string (pp
, ", ");
1116 print_quoted_type (pp
, get_type ());
1117 pp_printf (pp
, ", %qE)", m_decl
);
1121 /* Get the stack depth for the frame containing this decl, or 0
1125 decl_region::get_stack_depth () const
1127 if (get_parent_region () == NULL
)
1129 if (const frame_region
*frame_reg
1130 = get_parent_region ()->dyn_cast_frame_region ())
1131 return frame_reg
->get_stack_depth ();
1135 /* If the underlying decl is in the global constant pool,
1136 return an svalue representing the constant value.
1137 Otherwise return NULL. */
1140 decl_region::maybe_get_constant_value (region_model_manager
*mgr
) const
1142 if (TREE_CODE (m_decl
) == VAR_DECL
1143 && DECL_IN_CONSTANT_POOL (m_decl
)
1144 && DECL_INITIAL (m_decl
)
1145 && TREE_CODE (DECL_INITIAL (m_decl
)) == CONSTRUCTOR
)
1146 return get_svalue_for_constructor (DECL_INITIAL (m_decl
), mgr
);
1150 /* Get an svalue for CTOR, a CONSTRUCTOR for this region's decl. */
1153 decl_region::get_svalue_for_constructor (tree ctor
,
1154 region_model_manager
*mgr
) const
1156 gcc_assert (!TREE_CLOBBER_P (ctor
));
1158 /* Create a binding map, applying ctor to it, using this
1159 decl_region as the base region when building child regions
1160 for offset calculations. */
1162 if (!map
.apply_ctor_to_region (this, ctor
, mgr
))
1163 return mgr
->get_or_create_unknown_svalue (get_type ());
1165 /* Return a compound svalue for the map we built. */
1166 return mgr
->get_or_create_compound_svalue (get_type (), map
);
1169 /* For use on decl_regions for global variables.
1171 Get an svalue for the initial value of this region at entry to
1172 "main" (either based on DECL_INITIAL, or implicit initialization to
1175 Return NULL if there is a problem. */
1178 decl_region::get_svalue_for_initializer (region_model_manager
*mgr
) const
1180 tree init
= DECL_INITIAL (m_decl
);
1183 /* If we have an "extern" decl then there may be an initializer in
1185 if (DECL_EXTERNAL (m_decl
))
1188 /* Implicit initialization to zero; use a compound_svalue for it.
1189 Doing so requires that we have a concrete binding for this region,
1190 which can fail if we have a region with unknown size
1191 (e.g. "extern const char arr[];"). */
1192 const binding_key
*binding
1193 = binding_key::make (mgr
->get_store_manager (), this);
1194 if (binding
->symbolic_p ())
1197 /* If we don't care about tracking the content of this region, then
1198 it's unused, and the value doesn't matter. */
1202 binding_cluster
c (this);
1203 c
.zero_fill_region (mgr
->get_store_manager (), this);
1204 return mgr
->get_or_create_compound_svalue (TREE_TYPE (m_decl
),
1208 /* LTO can write out error_mark_node as the DECL_INITIAL for simple scalar
1209 values (to avoid writing out an extra section). */
1210 if (init
== error_mark_node
)
1213 if (TREE_CODE (init
) == CONSTRUCTOR
)
1214 return get_svalue_for_constructor (init
, mgr
);
1216 /* Reuse the get_rvalue logic from region_model. */
1217 region_model
m (mgr
);
1218 return m
.get_rvalue (path_var (init
, 0), NULL
);
1221 /* Subroutine of symnode_requires_tracking_p; return true if REF
1222 might imply that we should be tracking the value of its decl. */
1225 ipa_ref_requires_tracking (ipa_ref
*ref
)
1227 /* If we have a load/store/alias of the symbol, then we'll track
1228 the decl's value. */
1229 if (ref
->use
!= IPA_REF_ADDR
)
1232 if (ref
->stmt
== NULL
)
1235 switch (ref
->stmt
->code
)
1241 cgraph_node
*caller_cnode
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1242 if (caller_cnode
== NULL
)
1244 cgraph_edge
*edge
= caller_cnode
->get_edge (ref
->stmt
);
1247 if (edge
->callee
== NULL
)
1248 return true; /* e.g. call through function ptr. */
1249 if (edge
->callee
->definition
)
1251 /* If we get here, then this ref is a pointer passed to
1252 a function we don't have the definition for. */
1258 const gasm
*asm_stmt
= as_a
<const gasm
*> (ref
->stmt
);
1259 if (gimple_asm_noutputs (asm_stmt
) > 0)
1261 if (gimple_asm_nclobbers (asm_stmt
) > 0)
1263 /* If we get here, then this ref is the decl being passed
1264 by pointer to asm with no outputs. */
1271 /* Determine if the decl for SYMNODE should have binding_clusters
1272 in our state objects; return false to optimize away tracking
1273 certain decls in our state objects, as an optimization. */
1276 symnode_requires_tracking_p (symtab_node
*symnode
)
1278 gcc_assert (symnode
);
1279 if (symnode
->externally_visible
)
1281 tree context_fndecl
= DECL_CONTEXT (symnode
->decl
);
1282 if (context_fndecl
== NULL
)
1284 if (TREE_CODE (context_fndecl
) != FUNCTION_DECL
)
1286 for (auto ref
: symnode
->ref_list
.referring
)
1287 if (ipa_ref_requires_tracking (ref
))
1290 /* If we get here, then we don't have uses of this decl that require
1291 tracking; we never read from it or write to it explicitly. */
1295 /* Subroutine of decl_region ctor: determine whether this decl_region
1296 can have binding_clusters; return false to optimize away tracking
1297 of certain decls in our state objects, as an optimization. */
1300 decl_region::calc_tracked_p (tree decl
)
1302 /* Precondition of symtab_node::get. */
1303 if (TREE_CODE (decl
) == VAR_DECL
1304 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
) || in_lto_p
))
1305 if (symtab_node
*symnode
= symtab_node::get (decl
))
1306 return symnode_requires_tracking_p (symnode
);
1310 /* class field_region : public region. */
1312 /* Implementation of region::dump_to_pp vfunc for field_region. */
1315 field_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1319 get_parent_region ()->dump_to_pp (pp
, simple
);
1320 pp_string (pp
, ".");
1321 pp_printf (pp
, "%E", m_field
);
1325 pp_string (pp
, "field_region(");
1326 get_parent_region ()->dump_to_pp (pp
, simple
);
1327 pp_string (pp
, ", ");
1328 print_quoted_type (pp
, get_type ());
1329 pp_printf (pp
, ", %qE)", m_field
);
1333 /* Implementation of region::get_relative_concrete_offset vfunc
1334 for field_region. */
1337 field_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1339 /* Compare with e.g. gimple-fold.cc's
1340 fold_nonarray_ctor_reference. */
1341 tree byte_offset
= DECL_FIELD_OFFSET (m_field
);
1342 if (TREE_CODE (byte_offset
) != INTEGER_CST
)
1344 tree field_offset
= DECL_FIELD_BIT_OFFSET (m_field
);
1345 /* Compute bit offset of the field. */
1346 offset_int bitoffset
1347 = (wi::to_offset (field_offset
)
1348 + (wi::to_offset (byte_offset
) << LOG2_BITS_PER_UNIT
));
1354 /* Implementation of region::get_relative_symbolic_offset vfunc
1356 If known, the returned svalue is equal to the offset converted to bytes and
1360 field_region::get_relative_symbolic_offset (region_model_manager
*mgr
) const
1363 if (get_relative_concrete_offset (&out
))
1366 = wide_int_to_tree (integer_type_node
, out
/ BITS_PER_UNIT
);
1367 return mgr
->get_or_create_constant_svalue (cst_tree
);
1369 return mgr
->get_or_create_unknown_svalue (integer_type_node
);
1372 /* class element_region : public region. */
1374 /* Implementation of region::accept vfunc for element_region. */
1377 element_region::accept (visitor
*v
) const
1380 m_index
->accept (v
);
1383 /* Implementation of region::dump_to_pp vfunc for element_region. */
1386 element_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1390 //pp_string (pp, "(");
1391 get_parent_region ()->dump_to_pp (pp
, simple
);
1392 pp_string (pp
, "[");
1393 m_index
->dump_to_pp (pp
, simple
);
1394 pp_string (pp
, "]");
1395 //pp_string (pp, ")");
1399 pp_string (pp
, "element_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_index
->dump_to_pp (pp
, simple
);
1405 pp_printf (pp
, ")");
1409 /* Implementation of region::get_relative_concrete_offset vfunc
1410 for element_region. */
1413 element_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1415 if (tree idx_cst
= m_index
->maybe_get_constant ())
1417 gcc_assert (TREE_CODE (idx_cst
) == INTEGER_CST
);
1419 tree elem_type
= get_type ();
1420 offset_int element_idx
= wi::to_offset (idx_cst
);
1422 /* First, use int_size_in_bytes, to reject the case where we
1423 have an incomplete type, or a non-constant value. */
1424 HOST_WIDE_INT hwi_byte_size
= int_size_in_bytes (elem_type
);
1425 if (hwi_byte_size
> 0)
1427 offset_int element_bit_size
1428 = hwi_byte_size
<< LOG2_BITS_PER_UNIT
;
1429 offset_int element_bit_offset
1430 = element_idx
* element_bit_size
;
1431 *out
= element_bit_offset
;
1438 /* Implementation of region::get_relative_symbolic_offset vfunc
1439 for element_region. */
1442 element_region::get_relative_symbolic_offset (region_model_manager
*mgr
) const
1444 tree elem_type
= get_type ();
1446 /* First, use int_size_in_bytes, to reject the case where we
1447 have an incomplete type, or a non-constant value. */
1448 HOST_WIDE_INT hwi_byte_size
= int_size_in_bytes (elem_type
);
1449 if (hwi_byte_size
> 0)
1451 tree byte_size_tree
= wide_int_to_tree (integer_type_node
,
1453 const svalue
*byte_size_sval
1454 = mgr
->get_or_create_constant_svalue (byte_size_tree
);
1455 return mgr
->get_or_create_binop (integer_type_node
, MULT_EXPR
,
1456 m_index
, byte_size_sval
);
1458 return mgr
->get_or_create_unknown_svalue (integer_type_node
);
1461 /* class offset_region : public region. */
1463 /* Implementation of region::accept vfunc for offset_region. */
1466 offset_region::accept (visitor
*v
) const
1469 m_byte_offset
->accept (v
);
1472 /* Implementation of region::dump_to_pp vfunc for offset_region. */
1475 offset_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1479 //pp_string (pp, "(");
1480 get_parent_region ()->dump_to_pp (pp
, simple
);
1481 pp_string (pp
, "+");
1482 m_byte_offset
->dump_to_pp (pp
, simple
);
1483 //pp_string (pp, ")");
1487 pp_string (pp
, "offset_region(");
1488 get_parent_region ()->dump_to_pp (pp
, simple
);
1489 pp_string (pp
, ", ");
1490 print_quoted_type (pp
, get_type ());
1491 pp_string (pp
, ", ");
1492 m_byte_offset
->dump_to_pp (pp
, simple
);
1493 pp_printf (pp
, ")");
1497 /* Implementation of region::get_relative_concrete_offset vfunc
1498 for offset_region. */
1501 offset_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1503 if (tree byte_offset_cst
= m_byte_offset
->maybe_get_constant ())
1505 gcc_assert (TREE_CODE (byte_offset_cst
) == INTEGER_CST
);
1506 /* Use a signed value for the byte offset, to handle
1507 negative offsets. */
1508 HOST_WIDE_INT byte_offset
1509 = wi::to_offset (byte_offset_cst
).to_shwi ();
1510 HOST_WIDE_INT bit_offset
= byte_offset
* BITS_PER_UNIT
;
1517 /* Implementation of region::get_relative_symbolic_offset vfunc
1518 for offset_region. */
1521 offset_region::get_relative_symbolic_offset (region_model_manager
*mgr
1522 ATTRIBUTE_UNUSED
) const
1524 return get_byte_offset ();
1527 /* Implementation of region::get_byte_size_sval vfunc for offset_region. */
1530 offset_region::get_byte_size_sval (region_model_manager
*mgr
) const
1532 tree offset_cst
= get_byte_offset ()->maybe_get_constant ();
1533 byte_size_t byte_size
;
1534 /* If the offset points in the middle of the region,
1535 return the remaining bytes. */
1536 if (get_byte_size (&byte_size
) && offset_cst
)
1538 byte_size_t offset
= wi::to_offset (offset_cst
);
1539 byte_range
r (0, byte_size
);
1540 if (r
.contains_p (offset
))
1542 tree remaining_byte_size
= wide_int_to_tree (size_type_node
,
1543 byte_size
- offset
);
1544 return mgr
->get_or_create_constant_svalue (remaining_byte_size
);
1548 return region::get_byte_size_sval (mgr
);
1551 /* class sized_region : public region. */
1553 /* Implementation of region::accept vfunc for sized_region. */
1556 sized_region::accept (visitor
*v
) const
1559 m_byte_size_sval
->accept (v
);
1562 /* Implementation of region::dump_to_pp vfunc for sized_region. */
1565 sized_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1569 pp_string (pp
, "SIZED_REG(");
1570 get_parent_region ()->dump_to_pp (pp
, simple
);
1571 pp_string (pp
, ", ");
1572 m_byte_size_sval
->dump_to_pp (pp
, simple
);
1573 pp_string (pp
, ")");
1577 pp_string (pp
, "sized_region(");
1578 get_parent_region ()->dump_to_pp (pp
, simple
);
1579 pp_string (pp
, ", ");
1580 m_byte_size_sval
->dump_to_pp (pp
, simple
);
1581 pp_printf (pp
, ")");
1585 /* Implementation of region::get_byte_size vfunc for sized_region. */
1588 sized_region::get_byte_size (byte_size_t
*out
) const
1590 if (tree cst
= m_byte_size_sval
->maybe_get_constant ())
1592 gcc_assert (TREE_CODE (cst
) == INTEGER_CST
);
1593 *out
= tree_to_uhwi (cst
);
1599 /* Implementation of region::get_bit_size vfunc for sized_region. */
1602 sized_region::get_bit_size (bit_size_t
*out
) const
1604 byte_size_t byte_size
;
1605 if (!get_byte_size (&byte_size
))
1607 *out
= byte_size
* BITS_PER_UNIT
;
1611 /* class cast_region : public region. */
1613 /* Implementation of region::accept vfunc for cast_region. */
1616 cast_region::accept (visitor
*v
) const
1619 m_original_region
->accept (v
);
1622 /* Implementation of region::dump_to_pp vfunc for cast_region. */
1625 cast_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1629 pp_string (pp
, "CAST_REG(");
1630 print_quoted_type (pp
, get_type ());
1631 pp_string (pp
, ", ");
1632 m_original_region
->dump_to_pp (pp
, simple
);
1633 pp_string (pp
, ")");
1637 pp_string (pp
, "cast_region(");
1638 m_original_region
->dump_to_pp (pp
, simple
);
1639 pp_string (pp
, ", ");
1640 print_quoted_type (pp
, get_type ());
1641 pp_printf (pp
, ")");
1645 /* Implementation of region::get_relative_concrete_offset vfunc
1649 cast_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1655 /* class heap_allocated_region : public region. */
1657 /* Implementation of region::dump_to_pp vfunc for heap_allocated_region. */
1660 heap_allocated_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1663 pp_printf (pp
, "HEAP_ALLOCATED_REGION(%i)", get_id ());
1665 pp_printf (pp
, "heap_allocated_region(%i)", get_id ());
1668 /* class alloca_region : public region. */
1670 /* Implementation of region::dump_to_pp vfunc for alloca_region. */
1673 alloca_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1676 pp_printf (pp
, "ALLOCA_REGION(%i)", get_id ());
1678 pp_printf (pp
, "alloca_region(%i)", get_id ());
1681 /* class string_region : public region. */
1683 /* Implementation of region::dump_to_pp vfunc for string_region. */
1686 string_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1689 dump_tree (pp
, m_string_cst
);
1692 pp_string (pp
, "string_region(");
1693 dump_tree (pp
, m_string_cst
);
1694 if (!flag_dump_noaddr
)
1696 pp_string (pp
, " (");
1697 pp_pointer (pp
, m_string_cst
);
1698 pp_string (pp
, "))");
1703 /* class bit_range_region : public region. */
1705 /* Implementation of region::dump_to_pp vfunc for bit_range_region. */
1708 bit_range_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1712 pp_string (pp
, "BIT_RANGE_REG(");
1713 get_parent_region ()->dump_to_pp (pp
, simple
);
1714 pp_string (pp
, ", ");
1715 m_bits
.dump_to_pp (pp
);
1716 pp_string (pp
, ")");
1720 pp_string (pp
, "bit_range_region(");
1721 get_parent_region ()->dump_to_pp (pp
, simple
);
1722 pp_string (pp
, ", ");
1723 m_bits
.dump_to_pp (pp
);
1724 pp_printf (pp
, ")");
1728 /* Implementation of region::get_byte_size vfunc for bit_range_region. */
1731 bit_range_region::get_byte_size (byte_size_t
*out
) const
1733 if (m_bits
.m_size_in_bits
% BITS_PER_UNIT
== 0)
1735 *out
= m_bits
.m_size_in_bits
/ BITS_PER_UNIT
;
1741 /* Implementation of region::get_bit_size vfunc for bit_range_region. */
1744 bit_range_region::get_bit_size (bit_size_t
*out
) const
1746 *out
= m_bits
.m_size_in_bits
;
1750 /* Implementation of region::get_byte_size_sval vfunc for bit_range_region. */
1753 bit_range_region::get_byte_size_sval (region_model_manager
*mgr
) const
1755 if (m_bits
.m_size_in_bits
% BITS_PER_UNIT
!= 0)
1756 return mgr
->get_or_create_unknown_svalue (size_type_node
);
1758 HOST_WIDE_INT num_bytes
= m_bits
.m_size_in_bits
.to_shwi () / BITS_PER_UNIT
;
1759 return mgr
->get_or_create_int_cst (size_type_node
, num_bytes
);
1762 /* Implementation of region::get_relative_concrete_offset vfunc for
1763 bit_range_region. */
1766 bit_range_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1768 *out
= m_bits
.get_start_bit_offset ();
1772 /* Implementation of region::get_relative_symbolic_offset vfunc for
1774 The returned svalue is equal to the offset converted to bytes and
1778 bit_range_region::get_relative_symbolic_offset (region_model_manager
*mgr
)
1781 byte_offset_t start_byte
= m_bits
.get_start_bit_offset () / BITS_PER_UNIT
;
1782 tree start_bit_tree
= wide_int_to_tree (integer_type_node
, start_byte
);
1783 return mgr
->get_or_create_constant_svalue (start_bit_tree
);
1786 /* class var_arg_region : public region. */
1789 var_arg_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1793 pp_string (pp
, "VAR_ARG_REG(");
1794 get_parent_region ()->dump_to_pp (pp
, simple
);
1795 pp_printf (pp
, ", arg_idx: %d)", m_idx
);
1799 pp_string (pp
, "var_arg_region(");
1800 get_parent_region ()->dump_to_pp (pp
, simple
);
1801 pp_printf (pp
, ", arg_idx: %d)", m_idx
);
1805 /* Get the frame_region for this var_arg_region. */
1807 const frame_region
*
1808 var_arg_region::get_frame_region () const
1810 gcc_assert (get_parent_region ());
1811 return as_a
<const frame_region
*> (get_parent_region ());
1814 /* class unknown_region : public region. */
1816 /* Implementation of region::dump_to_pp vfunc for unknown_region. */
1819 unknown_region::dump_to_pp (pretty_printer
*pp
, bool /*simple*/) const
1821 pp_string (pp
, "UNKNOWN_REGION");
1826 #endif /* #if ENABLE_ANALYZER */