2 Copyright (C) 2019-2021 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"
67 /* class region and its various subclasses. */
73 delete m_cached_offset
;
76 /* Compare REG1 and REG2 by id. */
79 region::cmp_ids (const region
*reg1
, const region
*reg2
)
81 return (long)reg1
->get_id () - (long)reg2
->get_id ();
84 /* Determine the base region for this region: when considering bindings
85 for this region, the base region is the ancestor which identifies
86 which cluster they should be partitioned into.
87 Regions within the same struct/union/array are in the same cluster.
88 Different decls are in different clusters. */
91 region::get_base_region () const
93 const region
*iter
= this;
96 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. */
134 /* Return true if this region is ELDER or one of its descendents. */
137 region::descendent_of_p (const region
*elder
) const
139 const region
*iter
= this;
144 if (iter
->get_kind () == RK_CAST
)
145 iter
= iter
->dyn_cast_cast_region ()->get_original_region ();
147 iter
= iter
->get_parent_region ();
152 /* If this region is a frame_region, or a descendent of one, return it.
153 Otherwise return NULL. */
156 region::maybe_get_frame_region () const
158 const region
*iter
= this;
161 if (const frame_region
*frame_reg
= iter
->dyn_cast_frame_region ())
163 if (iter
->get_kind () == RK_CAST
)
164 iter
= iter
->dyn_cast_cast_region ()->get_original_region ();
166 iter
= iter
->get_parent_region ();
171 /* Get the memory space of this region. */
174 region::get_memory_space () const
176 const region
*iter
= this;
179 switch (iter
->get_kind ())
184 return MEMSPACE_GLOBALS
;
188 return MEMSPACE_CODE
;
192 return MEMSPACE_STACK
;
194 case RK_HEAP_ALLOCATED
:
195 return MEMSPACE_HEAP
;
197 return MEMSPACE_READONLY_DATA
;
199 if (iter
->get_kind () == RK_CAST
)
200 iter
= iter
->dyn_cast_cast_region ()->get_original_region ();
202 iter
= iter
->get_parent_region ();
204 return MEMSPACE_UNKNOWN
;
207 /* Subroutine for use by region_model_manager::get_or_create_initial_value.
208 Return true if this region has an initial_svalue.
209 Return false if attempting to use INIT_VAL(this_region) should give
210 the "UNINITIALIZED" poison value. */
213 region::can_have_initial_svalue_p () const
215 const region
*base_reg
= get_base_region ();
217 /* Check for memory spaces that are uninitialized by default. */
218 enum memory_space mem_space
= base_reg
->get_memory_space ();
223 case MEMSPACE_UNKNOWN
:
225 case MEMSPACE_GLOBALS
:
226 case MEMSPACE_READONLY_DATA
:
227 /* Such regions have initial_svalues. */
231 /* Heap allocations are uninitialized by default. */
235 if (tree decl
= base_reg
->maybe_get_decl ())
237 /* See the assertion in frame_region::get_region_for_local for the
238 tree codes we need to handle here. */
239 switch (TREE_CODE (decl
))
245 /* Parameters have initial values. */
250 /* Function locals don't have initial values. */
255 tree ssa_name
= decl
;
256 /* SSA names that are the default defn of a PARM_DECL
257 have initial_svalues; other SSA names don't. */
258 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name
)
259 && SSA_NAME_VAR (ssa_name
)
260 && TREE_CODE (SSA_NAME_VAR (ssa_name
)) == PARM_DECL
)
268 /* If we have an on-stack region that isn't associated with a decl
269 or SSA name, then we have VLA/alloca, which is uninitialized. */
274 /* If this region is a decl_region, return the decl.
275 Otherwise return NULL. */
278 region::maybe_get_decl () const
280 if (const decl_region
*decl_reg
= dyn_cast_decl_region ())
281 return decl_reg
->get_decl ();
285 /* Get the region_offset for this region (calculating it on the
286 first call and caching it internally). */
289 region::get_offset () const
292 m_cached_offset
= new region_offset (calc_offset ());
293 return *m_cached_offset
;
296 /* Base class implementation of region::get_byte_size vfunc.
297 If the size of this region (in bytes) is known statically, write it to *OUT
299 Otherwise return false. */
302 region::get_byte_size (byte_size_t
*out
) const
304 tree type
= get_type ();
306 /* Bail out e.g. for heap-allocated regions. */
310 HOST_WIDE_INT bytes
= int_size_in_bytes (type
);
317 /* Base implementation of region::get_byte_size_sval vfunc. */
320 region::get_byte_size_sval (region_model_manager
*mgr
) const
322 tree type
= get_type ();
324 /* Bail out e.g. for heap-allocated regions. */
326 return mgr
->get_or_create_unknown_svalue (size_type_node
);
328 HOST_WIDE_INT bytes
= int_size_in_bytes (type
);
330 return mgr
->get_or_create_unknown_svalue (size_type_node
);
332 tree byte_size
= size_in_bytes (type
);
333 if (TREE_TYPE (byte_size
) != size_type_node
)
334 byte_size
= fold_build1 (NOP_EXPR
, size_type_node
, byte_size
);
335 return mgr
->get_or_create_constant_svalue (byte_size
);
338 /* Attempt to get the size of TYPE in bits.
339 If successful, return true and write the size to *OUT.
340 Otherwise return false. */
343 int_size_in_bits (const_tree type
, bit_size_t
*out
)
345 if (INTEGRAL_TYPE_P (type
))
347 *out
= TYPE_PRECISION (type
);
351 tree sz
= TYPE_SIZE (type
);
352 if (sz
&& tree_fits_uhwi_p (sz
))
354 *out
= TREE_INT_CST_LOW (sz
);
361 /* If the size of this region (in bits) is known statically, write it to *OUT
363 Otherwise return false. */
366 region::get_bit_size (bit_size_t
*out
) const
368 tree type
= get_type ();
370 /* Bail out e.g. for heap-allocated regions. */
374 return int_size_in_bits (type
, out
);
377 /* Get the field within RECORD_TYPE at BIT_OFFSET. */
380 get_field_at_bit_offset (tree record_type
, bit_offset_t bit_offset
)
382 gcc_assert (TREE_CODE (record_type
) == RECORD_TYPE
);
386 /* Find the first field that has an offset > BIT_OFFSET,
387 then return the one preceding it.
388 Skip other trees within the chain, such as FUNCTION_DECLs. */
389 tree last_field
= NULL_TREE
;
390 for (tree iter
= TYPE_FIELDS (record_type
); iter
!= NULL_TREE
;
391 iter
= DECL_CHAIN (iter
))
393 if (TREE_CODE (iter
) == FIELD_DECL
)
395 int iter_field_offset
= int_bit_position (iter
);
396 if (bit_offset
< iter_field_offset
)
404 /* Populate *OUT with descendent regions of type TYPE that match
405 RELATIVE_BIT_OFFSET and SIZE_IN_BITS within this region. */
408 region::get_subregions_for_binding (region_model_manager
*mgr
,
409 bit_offset_t relative_bit_offset
,
410 bit_size_t size_in_bits
,
412 auto_vec
<const region
*> *out
) const
414 if (get_type () == NULL_TREE
|| type
== NULL_TREE
)
416 if (relative_bit_offset
== 0
417 && types_compatible_p (get_type (), type
))
419 out
->safe_push (this);
422 switch (TREE_CODE (get_type ()))
426 tree element_type
= TREE_TYPE (get_type ());
427 HOST_WIDE_INT hwi_byte_size
= int_size_in_bytes (element_type
);
428 if (hwi_byte_size
> 0)
430 HOST_WIDE_INT bits_per_element
431 = hwi_byte_size
<< LOG2_BITS_PER_UNIT
;
432 HOST_WIDE_INT element_index
433 = (relative_bit_offset
.to_shwi () / bits_per_element
);
434 tree element_index_cst
435 = build_int_cst (integer_type_node
, element_index
);
436 HOST_WIDE_INT inner_bit_offset
437 = relative_bit_offset
.to_shwi () % bits_per_element
;
438 const region
*subregion
= mgr
->get_element_region
440 mgr
->get_or_create_constant_svalue (element_index_cst
));
441 subregion
->get_subregions_for_binding (mgr
, inner_bit_offset
,
442 size_in_bits
, type
, out
);
448 /* The bit offset might be *within* one of the fields (such as
449 with nested structs).
450 So we want to find the enclosing field, adjust the offset,
452 if (tree field
= get_field_at_bit_offset (get_type (),
453 relative_bit_offset
))
455 int field_bit_offset
= int_bit_position (field
);
456 const region
*subregion
= mgr
->get_field_region (this, field
);
457 subregion
->get_subregions_for_binding
458 (mgr
, relative_bit_offset
- field_bit_offset
,
459 size_in_bits
, type
, out
);
465 for (tree field
= TYPE_FIELDS (get_type ()); field
!= NULL_TREE
;
466 field
= DECL_CHAIN (field
))
468 if (TREE_CODE (field
) != FIELD_DECL
)
470 const region
*subregion
= mgr
->get_field_region (this, field
);
471 subregion
->get_subregions_for_binding (mgr
,
485 /* Walk from this region up to the base region within its cluster, calculating
486 the offset relative to the base region, either as an offset in bits,
487 or a symbolic offset. */
490 region::calc_offset () const
492 const region
*iter_region
= this;
493 bit_offset_t accum_bit_offset
= 0;
497 switch (iter_region
->get_kind ())
501 const field_region
*field_reg
502 = (const field_region
*)iter_region
;
503 iter_region
= iter_region
->get_parent_region ();
505 bit_offset_t rel_bit_offset
;
506 if (!field_reg
->get_relative_concrete_offset (&rel_bit_offset
))
507 return region_offset::make_symbolic (iter_region
);
508 accum_bit_offset
+= rel_bit_offset
;
514 const element_region
*element_reg
515 = (const element_region
*)iter_region
;
516 iter_region
= iter_region
->get_parent_region ();
518 bit_offset_t rel_bit_offset
;
519 if (!element_reg
->get_relative_concrete_offset (&rel_bit_offset
))
520 return region_offset::make_symbolic (iter_region
);
521 accum_bit_offset
+= rel_bit_offset
;
527 const offset_region
*offset_reg
528 = (const offset_region
*)iter_region
;
529 iter_region
= iter_region
->get_parent_region ();
531 bit_offset_t rel_bit_offset
;
532 if (!offset_reg
->get_relative_concrete_offset (&rel_bit_offset
))
533 return region_offset::make_symbolic (iter_region
);
534 accum_bit_offset
+= rel_bit_offset
;
539 iter_region
= iter_region
->get_parent_region ();
544 const cast_region
*cast_reg
545 = as_a
<const cast_region
*> (iter_region
);
546 iter_region
= cast_reg
->get_original_region ();
551 return region_offset::make_concrete (iter_region
, accum_bit_offset
);
554 return region_offset::make_concrete (iter_region
, accum_bit_offset
);
557 /* Base implementation of region::get_relative_concrete_offset vfunc. */
560 region::get_relative_concrete_offset (bit_offset_t
*) const
565 /* Copy from SRC_REG to DST_REG, using CTXT for any issues that occur. */
568 region_model::copy_region (const region
*dst_reg
, const region
*src_reg
,
569 region_model_context
*ctxt
)
571 gcc_assert (dst_reg
);
572 gcc_assert (src_reg
);
573 if (dst_reg
== src_reg
)
576 const svalue
*sval
= get_store_value (src_reg
, ctxt
);
577 set_value (dst_reg
, sval
, ctxt
);
580 /* Dump a description of this region to stderr. */
583 region::dump (bool simple
) const
586 pp_format_decoder (&pp
) = default_tree_printer
;
587 pp_show_color (&pp
) = pp_show_color (global_dc
->printer
);
588 pp
.buffer
->stream
= stderr
;
589 dump_to_pp (&pp
, simple
);
594 /* Return a new json::string describing the region. */
597 region::to_json () const
599 label_text desc
= get_desc (true);
600 json::value
*reg_js
= new json::string (desc
.m_buffer
);
605 /* Generate a description of this region. */
607 DEBUG_FUNCTION label_text
608 region::get_desc (bool simple
) const
611 pp_format_decoder (&pp
) = default_tree_printer
;
612 dump_to_pp (&pp
, simple
);
613 return label_text::take (xstrdup (pp_formatted_text (&pp
)));
616 /* Base implementation of region::accept vfunc.
617 Subclass implementations should chain up to this. */
620 region::accept (visitor
*v
) const
622 v
->visit_region (this);
624 m_parent
->accept (v
);
627 /* Return true if this is a symbolic region for deferencing an
629 We shouldn't attempt to bind values for this region (but
630 can unbind values for other regions). */
633 region::symbolic_for_unknown_ptr_p () const
635 if (const symbolic_region
*sym_reg
= dyn_cast_symbolic_region ())
636 if (sym_reg
->get_pointer ()->get_kind () == SK_UNKNOWN
)
643 region::region (complexity c
, unsigned id
, const region
*parent
, tree type
)
644 : m_complexity (c
), m_id (id
), m_parent (parent
), m_type (type
),
645 m_cached_offset (NULL
)
647 gcc_assert (type
== NULL_TREE
|| TYPE_P (type
));
650 /* Comparator for use by vec<const region *>::qsort,
651 using their IDs to order them. */
654 region::cmp_ptr_ptr (const void *p1
, const void *p2
)
656 const region
* const *reg1
= (const region
* const *)p1
;
657 const region
* const *reg2
= (const region
* const *)p2
;
659 return cmp_ids (*reg1
, *reg2
);
662 /* Determine if a pointer to this region must be non-NULL.
664 Generally, pointers to regions must be non-NULL, but pointers
665 to symbolic_regions might, in fact, be NULL.
667 This allows us to simulate functions like malloc and calloc with:
668 - only one "outcome" from each statement,
669 - the idea that the pointer is on the heap if non-NULL
670 - the possibility that the pointer could be NULL
671 - the idea that successive values returned from malloc are non-equal
672 - to be able to zero-fill for calloc. */
675 region::non_null_p () const
682 /* Are we within a symbolic_region? If so, it could be NULL, and we
683 have to fall back on the constraints. */
685 case RK_HEAP_ALLOCATED
:
690 /* Return true iff this region is defined in terms of SVAL. */
693 region::involves_p (const svalue
*sval
) const
695 if (const symbolic_region
*symbolic_reg
= dyn_cast_symbolic_region ())
697 if (symbolic_reg
->get_pointer ()->involves_p (sval
))
704 /* Comparator for trees to impose a deterministic ordering on
708 tree_cmp (const_tree t1
, const_tree t2
)
713 /* Test tree codes first. */
714 if (TREE_CODE (t1
) != TREE_CODE (t2
))
715 return TREE_CODE (t1
) - TREE_CODE (t2
);
717 /* From this point on, we know T1 and T2 have the same tree code. */
721 if (DECL_NAME (t1
) && DECL_NAME (t2
))
722 return strcmp (IDENTIFIER_POINTER (DECL_NAME (t1
)),
723 IDENTIFIER_POINTER (DECL_NAME (t2
)));
728 else if (DECL_NAME (t2
))
731 return DECL_UID (t1
) - DECL_UID (t2
);
735 switch (TREE_CODE (t1
))
739 if (SSA_NAME_VAR (t1
) && SSA_NAME_VAR (t2
))
741 int var_cmp
= tree_cmp (SSA_NAME_VAR (t1
), SSA_NAME_VAR (t2
));
744 return SSA_NAME_VERSION (t1
) - SSA_NAME_VERSION (t2
);
748 if (SSA_NAME_VAR (t1
))
750 else if (SSA_NAME_VAR (t2
))
753 return SSA_NAME_VERSION (t1
) - SSA_NAME_VERSION (t2
);
759 return tree_int_cst_compare (t1
, t2
);
763 const real_value
*rv1
= TREE_REAL_CST_PTR (t1
);
764 const real_value
*rv2
= TREE_REAL_CST_PTR (t2
);
765 if (real_compare (UNORDERED_EXPR
, rv1
, rv2
))
767 /* Impose an arbitrary order on NaNs relative to other NaNs
769 if (int cmp_isnan
= real_isnan (rv1
) - real_isnan (rv2
))
771 if (int cmp_issignaling_nan
772 = real_issignaling_nan (rv1
) - real_issignaling_nan (rv2
))
773 return cmp_issignaling_nan
;
774 return real_isneg (rv1
) - real_isneg (rv2
);
776 if (real_compare (LT_EXPR
, rv1
, rv2
))
778 if (real_compare (GT_EXPR
, rv1
, rv2
))
784 return strcmp (TREE_STRING_POINTER (t1
),
785 TREE_STRING_POINTER (t2
));
797 /* qsort comparator for trees to impose a deterministic ordering on
801 tree_cmp (const void *p1
, const void *p2
)
803 const_tree t1
= *(const_tree
const *)p1
;
804 const_tree t2
= *(const_tree
const *)p2
;
806 return tree_cmp (t1
, t2
);
809 /* class frame_region : public space_region. */
811 frame_region::~frame_region ()
813 for (map_t::iterator iter
= m_locals
.begin ();
814 iter
!= m_locals
.end ();
816 delete (*iter
).second
;
820 frame_region::accept (visitor
*v
) const
824 m_calling_frame
->accept (v
);
827 /* Implementation of region::dump_to_pp vfunc for frame_region. */
830 frame_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
833 pp_printf (pp
, "frame: %qs@%i", function_name (m_fun
), get_stack_depth ());
835 pp_printf (pp
, "frame_region(%qs, index: %i, depth: %i)",
836 function_name (m_fun
), m_index
, get_stack_depth ());
840 frame_region::get_region_for_local (region_model_manager
*mgr
,
843 // TODO: could also check that VAR_DECLs are locals
844 gcc_assert (TREE_CODE (expr
) == PARM_DECL
845 || TREE_CODE (expr
) == VAR_DECL
846 || TREE_CODE (expr
) == SSA_NAME
847 || TREE_CODE (expr
) == RESULT_DECL
);
849 /* Ideally we'd use mutable here. */
850 map_t
&mutable_locals
= const_cast <map_t
&> (m_locals
);
852 if (decl_region
**slot
= mutable_locals
.get (expr
))
855 = new decl_region (mgr
->alloc_region_id (), this, expr
);
856 mutable_locals
.put (expr
, reg
);
860 /* class globals_region : public space_region. */
862 /* Implementation of region::dump_to_pp vfunc for globals_region. */
865 globals_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
868 pp_string (pp
, "::");
870 pp_string (pp
, "globals");
873 /* class code_region : public map_region. */
875 /* Implementation of region::dump_to_pp vfunc for code_region. */
878 code_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
881 pp_string (pp
, "code region");
883 pp_string (pp
, "code_region()");
886 /* class function_region : public region. */
888 /* Implementation of region::dump_to_pp vfunc for function_region. */
891 function_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
895 dump_quoted_tree (pp
, m_fndecl
);
899 pp_string (pp
, "function_region(");
900 dump_quoted_tree (pp
, m_fndecl
);
905 /* class label_region : public region. */
907 /* Implementation of region::dump_to_pp vfunc for label_region. */
910 label_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
914 dump_quoted_tree (pp
, m_label
);
918 pp_string (pp
, "label_region(");
919 dump_quoted_tree (pp
, m_label
);
924 /* class stack_region : public region. */
926 /* Implementation of region::dump_to_pp vfunc for stack_region. */
929 stack_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
932 pp_string (pp
, "stack region");
934 pp_string (pp
, "stack_region()");
937 /* class heap_region : public region. */
939 /* Implementation of region::dump_to_pp vfunc for heap_region. */
942 heap_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
945 pp_string (pp
, "heap region");
947 pp_string (pp
, "heap_region()");
950 /* class root_region : public region. */
952 /* root_region's ctor. */
954 root_region::root_region (unsigned id
)
955 : region (complexity (1, 1), id
, NULL
, NULL_TREE
)
959 /* Implementation of region::dump_to_pp vfunc for root_region. */
962 root_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
965 pp_string (pp
, "root region");
967 pp_string (pp
, "root_region()");
970 /* class symbolic_region : public map_region. */
972 /* symbolic_region's ctor. */
974 symbolic_region::symbolic_region (unsigned id
, region
*parent
,
975 const svalue
*sval_ptr
)
976 : region (complexity::from_pair (parent
, sval_ptr
), id
, parent
,
977 TREE_TYPE (sval_ptr
->get_type ())),
978 m_sval_ptr (sval_ptr
)
982 /* Implementation of region::accept vfunc for symbolic_region. */
985 symbolic_region::accept (visitor
*v
) const
988 m_sval_ptr
->accept (v
);
991 /* Implementation of region::dump_to_pp vfunc for symbolic_region. */
994 symbolic_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
998 pp_string (pp
, "(*");
999 m_sval_ptr
->dump_to_pp (pp
, simple
);
1000 pp_string (pp
, ")");
1004 pp_string (pp
, "symbolic_region(");
1005 get_parent_region ()->dump_to_pp (pp
, simple
);
1006 pp_string (pp
, ", ");
1007 print_quoted_type (pp
, get_type ());
1008 pp_string (pp
, ", ");
1009 m_sval_ptr
->dump_to_pp (pp
, simple
);
1010 pp_string (pp
, ")");
1014 /* class decl_region : public region. */
1016 /* Implementation of region::dump_to_pp vfunc for decl_region. */
1019 decl_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1022 pp_printf (pp
, "%E", m_decl
);
1025 pp_string (pp
, "decl_region(");
1026 get_parent_region ()->dump_to_pp (pp
, simple
);
1027 pp_string (pp
, ", ");
1028 print_quoted_type (pp
, get_type ());
1029 pp_printf (pp
, ", %qE)", m_decl
);
1033 /* Get the stack depth for the frame containing this decl, or 0
1037 decl_region::get_stack_depth () const
1039 if (get_parent_region () == NULL
)
1041 if (const frame_region
*frame_reg
1042 = get_parent_region ()->dyn_cast_frame_region ())
1043 return frame_reg
->get_stack_depth ();
1047 /* If the underlying decl is in the global constant pool,
1048 return an svalue representing the constant value.
1049 Otherwise return NULL. */
1052 decl_region::maybe_get_constant_value (region_model_manager
*mgr
) const
1054 if (TREE_CODE (m_decl
) == VAR_DECL
1055 && DECL_IN_CONSTANT_POOL (m_decl
)
1056 && DECL_INITIAL (m_decl
)
1057 && TREE_CODE (DECL_INITIAL (m_decl
)) == CONSTRUCTOR
)
1058 return get_svalue_for_constructor (DECL_INITIAL (m_decl
), mgr
);
1062 /* Get an svalue for CTOR, a CONSTRUCTOR for this region's decl. */
1065 decl_region::get_svalue_for_constructor (tree ctor
,
1066 region_model_manager
*mgr
) const
1068 gcc_assert (!TREE_CLOBBER_P (ctor
));
1070 /* Create a binding map, applying ctor to it, using this
1071 decl_region as the base region when building child regions
1072 for offset calculations. */
1074 if (!map
.apply_ctor_to_region (this, ctor
, mgr
))
1075 return mgr
->get_or_create_unknown_svalue (get_type ());
1077 /* Return a compound svalue for the map we built. */
1078 return mgr
->get_or_create_compound_svalue (get_type (), map
);
1081 /* For use on decl_regions for global variables.
1083 Get an svalue for the initial value of this region at entry to
1084 "main" (either based on DECL_INITIAL, or implicit initialization to
1087 Return NULL if there is a problem. */
1090 decl_region::get_svalue_for_initializer (region_model_manager
*mgr
) const
1092 tree init
= DECL_INITIAL (m_decl
);
1095 /* If we have an "extern" decl then there may be an initializer in
1097 if (DECL_EXTERNAL (m_decl
))
1100 /* Implicit initialization to zero; use a compound_svalue for it.
1101 Doing so requires that we have a concrete binding for this region,
1102 which can fail if we have a region with unknown size
1103 (e.g. "extern const char arr[];"). */
1104 const binding_key
*binding
1105 = binding_key::make (mgr
->get_store_manager (), this);
1106 if (binding
->symbolic_p ())
1109 binding_cluster
c (this);
1110 c
.zero_fill_region (mgr
->get_store_manager (), this);
1111 return mgr
->get_or_create_compound_svalue (TREE_TYPE (m_decl
),
1115 /* LTO can write out error_mark_node as the DECL_INITIAL for simple scalar
1116 values (to avoid writing out an extra section). */
1117 if (init
== error_mark_node
)
1120 if (TREE_CODE (init
) == CONSTRUCTOR
)
1121 return get_svalue_for_constructor (init
, mgr
);
1123 /* Reuse the get_rvalue logic from region_model. */
1124 region_model
m (mgr
);
1125 return m
.get_rvalue (path_var (init
, 0), NULL
);
1128 /* class field_region : public region. */
1130 /* Implementation of region::dump_to_pp vfunc for field_region. */
1133 field_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1137 get_parent_region ()->dump_to_pp (pp
, simple
);
1138 pp_string (pp
, ".");
1139 pp_printf (pp
, "%E", m_field
);
1143 pp_string (pp
, "field_region(");
1144 get_parent_region ()->dump_to_pp (pp
, simple
);
1145 pp_string (pp
, ", ");
1146 print_quoted_type (pp
, get_type ());
1147 pp_printf (pp
, ", %qE)", m_field
);
1151 /* Implementation of region::get_relative_concrete_offset vfunc
1152 for field_region. */
1155 field_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1157 /* Compare with e.g. gimple-fold.c's
1158 fold_nonarray_ctor_reference. */
1159 tree byte_offset
= DECL_FIELD_OFFSET (m_field
);
1160 if (TREE_CODE (byte_offset
) != INTEGER_CST
)
1162 tree field_offset
= DECL_FIELD_BIT_OFFSET (m_field
);
1163 /* Compute bit offset of the field. */
1164 offset_int bitoffset
1165 = (wi::to_offset (field_offset
)
1166 + (wi::to_offset (byte_offset
) << LOG2_BITS_PER_UNIT
));
1171 /* class element_region : public region. */
1173 /* Implementation of region::accept vfunc for element_region. */
1176 element_region::accept (visitor
*v
) const
1179 m_index
->accept (v
);
1182 /* Implementation of region::dump_to_pp vfunc for element_region. */
1185 element_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1189 //pp_string (pp, "(");
1190 get_parent_region ()->dump_to_pp (pp
, simple
);
1191 pp_string (pp
, "[");
1192 m_index
->dump_to_pp (pp
, simple
);
1193 pp_string (pp
, "]");
1194 //pp_string (pp, ")");
1198 pp_string (pp
, "element_region(");
1199 get_parent_region ()->dump_to_pp (pp
, simple
);
1200 pp_string (pp
, ", ");
1201 print_quoted_type (pp
, get_type ());
1202 pp_string (pp
, ", ");
1203 m_index
->dump_to_pp (pp
, simple
);
1204 pp_printf (pp
, ")");
1208 /* Implementation of region::get_relative_concrete_offset vfunc
1209 for element_region. */
1212 element_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1214 if (tree idx_cst
= m_index
->maybe_get_constant ())
1216 gcc_assert (TREE_CODE (idx_cst
) == INTEGER_CST
);
1218 tree elem_type
= get_type ();
1219 offset_int element_idx
= wi::to_offset (idx_cst
);
1221 /* First, use int_size_in_bytes, to reject the case where we
1222 have an incomplete type, or a non-constant value. */
1223 HOST_WIDE_INT hwi_byte_size
= int_size_in_bytes (elem_type
);
1224 if (hwi_byte_size
> 0)
1226 offset_int element_bit_size
1227 = hwi_byte_size
<< LOG2_BITS_PER_UNIT
;
1228 offset_int element_bit_offset
1229 = element_idx
* element_bit_size
;
1230 *out
= element_bit_offset
;
1237 /* class offset_region : public region. */
1239 /* Implementation of region::accept vfunc for offset_region. */
1242 offset_region::accept (visitor
*v
) const
1245 m_byte_offset
->accept (v
);
1248 /* Implementation of region::dump_to_pp vfunc for offset_region. */
1251 offset_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1255 //pp_string (pp, "(");
1256 get_parent_region ()->dump_to_pp (pp
, simple
);
1257 pp_string (pp
, "+");
1258 m_byte_offset
->dump_to_pp (pp
, simple
);
1259 //pp_string (pp, ")");
1263 pp_string (pp
, "offset_region(");
1264 get_parent_region ()->dump_to_pp (pp
, simple
);
1265 pp_string (pp
, ", ");
1266 print_quoted_type (pp
, get_type ());
1267 pp_string (pp
, ", ");
1268 m_byte_offset
->dump_to_pp (pp
, simple
);
1269 pp_printf (pp
, ")");
1273 /* Implementation of region::get_relative_concrete_offset vfunc
1274 for offset_region. */
1277 offset_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1279 if (tree byte_offset_cst
= m_byte_offset
->maybe_get_constant ())
1281 gcc_assert (TREE_CODE (byte_offset_cst
) == INTEGER_CST
);
1282 /* Use a signed value for the byte offset, to handle
1283 negative offsets. */
1284 HOST_WIDE_INT byte_offset
1285 = wi::to_offset (byte_offset_cst
).to_shwi ();
1286 HOST_WIDE_INT bit_offset
= byte_offset
* BITS_PER_UNIT
;
1293 /* class sized_region : public region. */
1295 /* Implementation of region::accept vfunc for sized_region. */
1298 sized_region::accept (visitor
*v
) const
1301 m_byte_size_sval
->accept (v
);
1304 /* Implementation of region::dump_to_pp vfunc for sized_region. */
1307 sized_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1311 pp_string (pp
, "SIZED_REG(");
1312 get_parent_region ()->dump_to_pp (pp
, simple
);
1313 pp_string (pp
, ", ");
1314 m_byte_size_sval
->dump_to_pp (pp
, simple
);
1315 pp_string (pp
, ")");
1319 pp_string (pp
, "sized_region(");
1320 get_parent_region ()->dump_to_pp (pp
, simple
);
1321 pp_string (pp
, ", ");
1322 m_byte_size_sval
->dump_to_pp (pp
, simple
);
1323 pp_printf (pp
, ")");
1327 /* Implementation of region::get_byte_size vfunc for sized_region. */
1330 sized_region::get_byte_size (byte_size_t
*out
) const
1332 if (tree cst
= m_byte_size_sval
->maybe_get_constant ())
1334 gcc_assert (TREE_CODE (cst
) == INTEGER_CST
);
1335 *out
= tree_to_uhwi (cst
);
1341 /* Implementation of region::get_bit_size vfunc for sized_region. */
1344 sized_region::get_bit_size (bit_size_t
*out
) const
1346 byte_size_t byte_size
;
1347 if (!get_byte_size (&byte_size
))
1349 *out
= byte_size
* BITS_PER_UNIT
;
1353 /* class cast_region : public region. */
1355 /* Implementation of region::accept vfunc for cast_region. */
1358 cast_region::accept (visitor
*v
) const
1361 m_original_region
->accept (v
);
1364 /* Implementation of region::dump_to_pp vfunc for cast_region. */
1367 cast_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1371 pp_string (pp
, "CAST_REG(");
1372 print_quoted_type (pp
, get_type ());
1373 pp_string (pp
, ", ");
1374 m_original_region
->dump_to_pp (pp
, simple
);
1375 pp_string (pp
, ")");
1379 pp_string (pp
, "cast_region(");
1380 m_original_region
->dump_to_pp (pp
, simple
);
1381 pp_string (pp
, ", ");
1382 print_quoted_type (pp
, get_type ());
1383 pp_printf (pp
, ")");
1387 /* class heap_allocated_region : public region. */
1389 /* Implementation of region::dump_to_pp vfunc for heap_allocated_region. */
1392 heap_allocated_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1395 pp_printf (pp
, "HEAP_ALLOCATED_REGION(%i)", get_id ());
1397 pp_printf (pp
, "heap_allocated_region(%i)", get_id ());
1400 /* class alloca_region : public region. */
1402 /* Implementation of region::dump_to_pp vfunc for alloca_region. */
1405 alloca_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1408 pp_string (pp
, "ALLOCA_REGION");
1410 pp_string (pp
, "alloca_region()");
1413 /* class string_region : public region. */
1415 /* Implementation of region::dump_to_pp vfunc for string_region. */
1418 string_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1421 dump_tree (pp
, m_string_cst
);
1424 pp_string (pp
, "string_region(");
1425 dump_tree (pp
, m_string_cst
);
1426 if (!flag_dump_noaddr
)
1428 pp_string (pp
, " (");
1429 pp_pointer (pp
, m_string_cst
);
1430 pp_string (pp
, "))");
1435 /* class unknown_region : public region. */
1437 /* Implementation of region::dump_to_pp vfunc for unknown_region. */
1440 unknown_region::dump_to_pp (pretty_printer
*pp
, bool /*simple*/) const
1442 pp_string (pp
, "UNKNOWN_REGION");
1447 #endif /* #if ENABLE_ANALYZER */