2 Copyright (C) 2019-2024 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"
44 #include "analyzer/analyzer.h"
45 #include "analyzer/analyzer-logging.h"
46 #include "ordered-hash-map.h"
51 #include "analyzer/supergraph.h"
53 #include "analyzer/call-string.h"
54 #include "analyzer/program-point.h"
55 #include "analyzer/store.h"
56 #include "analyzer/region.h"
57 #include "analyzer/region-model.h"
58 #include "analyzer/sm.h"
59 #include "analyzer/program-state.h"
66 region_offset::make_byte_offset (const region
*base_region
,
67 const svalue
*num_bytes_sval
)
69 if (tree num_bytes_cst
= num_bytes_sval
->maybe_get_constant ())
71 gcc_assert (TREE_CODE (num_bytes_cst
) == INTEGER_CST
);
72 bit_offset_t num_bits
= wi::to_offset (num_bytes_cst
) * BITS_PER_UNIT
;
73 return make_concrete (base_region
, num_bits
);
77 return make_symbolic (base_region
, num_bytes_sval
);
82 region_offset::calc_symbolic_bit_offset (region_model_manager
*mgr
) const
86 const svalue
*bits_per_byte
87 = mgr
->get_or_create_int_cst (NULL_TREE
, BITS_PER_UNIT
);
88 return *mgr
->get_or_create_binop (NULL_TREE
, MULT_EXPR
,
89 m_sym_offset
, bits_per_byte
);
92 return *mgr
->get_or_create_int_cst (NULL_TREE
, m_offset
);
96 region_offset::calc_symbolic_byte_offset (region_model_manager
*mgr
) const
102 byte_offset_t concrete_byte_offset
;
103 if (get_concrete_byte_offset (&concrete_byte_offset
))
104 return mgr
->get_or_create_int_cst (size_type_node
,
105 concrete_byte_offset
);
107 /* Can't handle bitfields; return UNKNOWN. */
108 return mgr
->get_or_create_unknown_svalue (size_type_node
);
113 region_offset::dump_to_pp (pretty_printer
*pp
, bool simple
) const
117 /* We don't bother showing the base region. */
118 pp_string (pp
, "byte ");
119 m_sym_offset
->dump_to_pp (pp
, simple
);
123 if (m_offset
% BITS_PER_UNIT
== 0)
125 pp_string (pp
, "byte ");
126 pp_wide_int (pp
, m_offset
/ BITS_PER_UNIT
, SIGNED
);
130 pp_string (pp
, "bit ");
131 pp_wide_int (pp
, m_offset
, SIGNED
);
137 region_offset::dump (bool simple
) const
140 pp_format_decoder (&pp
) = default_tree_printer
;
141 pp_show_color (&pp
) = pp_show_color (global_dc
->printer
);
142 pp
.buffer
->stream
= stderr
;
143 dump_to_pp (&pp
, simple
);
148 /* An svalue that matches the pattern (BASE * FACTOR) + OFFSET
149 where FACTOR or OFFSET could be the identity (represented as NULL). */
153 linear_op (const svalue
*base
,
154 const svalue
*factor
,
155 const svalue
*offset
)
156 : m_base (base
), m_factor (factor
), m_offset (offset
)
160 bool maybe_get_cst_factor (bit_offset_t
*out
) const
162 if (m_factor
== nullptr)
167 if (tree cst_factor
= m_factor
->maybe_get_constant ())
169 *out
= wi::to_offset (cst_factor
);
175 bool maybe_get_cst_offset (bit_offset_t
*out
) const
177 if (m_offset
== nullptr)
182 if (tree cst_offset
= m_offset
->maybe_get_constant ())
184 *out
= wi::to_offset (cst_offset
);
191 less (const linear_op
&a
, const linear_op
&b
)
194 if (a
.m_base
== b
.m_base
)
196 bit_offset_t a_wi_factor
;
197 bit_offset_t b_wi_factor
;
198 if (a
.maybe_get_cst_factor (&a_wi_factor
)
199 && b
.maybe_get_cst_factor (&b_wi_factor
))
201 if (a_wi_factor
!= b_wi_factor
)
202 return tristate (a_wi_factor
< b_wi_factor
);
205 bit_offset_t a_wi_offset
;
206 bit_offset_t b_wi_offset
;
207 if (a
.maybe_get_cst_offset (&a_wi_offset
)
208 && b
.maybe_get_cst_offset (&b_wi_offset
))
209 return tristate (a_wi_offset
< b_wi_offset
);
213 return tristate::unknown ();
217 le (const linear_op
&a
, const linear_op
&b
)
220 if (a
.m_base
== b
.m_base
)
222 bit_offset_t a_wi_factor
;
223 bit_offset_t b_wi_factor
;
224 if (a
.maybe_get_cst_factor (&a_wi_factor
)
225 && b
.maybe_get_cst_factor (&b_wi_factor
))
227 if (a_wi_factor
!= b_wi_factor
)
228 return tristate (a_wi_factor
<= b_wi_factor
);
231 bit_offset_t a_wi_offset
;
232 bit_offset_t b_wi_offset
;
233 if (a
.maybe_get_cst_offset (&a_wi_offset
)
234 && b
.maybe_get_cst_offset (&b_wi_offset
))
235 return tristate (a_wi_offset
<= b_wi_offset
);
239 return tristate::unknown ();
243 from_svalue (const svalue
&sval
, linear_op
*out
)
245 switch (sval
.get_kind ())
251 const binop_svalue
&binop_sval ((const binop_svalue
&)sval
);
252 if (binop_sval
.get_op () == MULT_EXPR
)
254 *out
= linear_op (binop_sval
.get_arg0 (),
255 binop_sval
.get_arg1 (),
259 else if (binop_sval
.get_op () == PLUS_EXPR
)
261 if (binop_sval
.get_arg0 ()->get_kind () == SK_BINOP
)
263 const binop_svalue
&inner_binop_sval
264 ((const binop_svalue
&)*binop_sval
.get_arg0 ());
265 if (inner_binop_sval
.get_op () == MULT_EXPR
)
267 *out
= linear_op (inner_binop_sval
.get_arg0 (),
268 inner_binop_sval
.get_arg1 (),
269 binop_sval
.get_arg1 ());
274 *out
= linear_op (binop_sval
.get_arg0 (),
276 binop_sval
.get_arg1 ());
285 const svalue
*m_base
;
286 const svalue
*m_factor
;
287 const svalue
*m_offset
;
291 operator< (const region_offset
&a
, const region_offset
&b
)
297 /* Symbolic vs symbolic. */
298 const svalue
&a_sval
= *a
.get_symbolic_byte_offset ();
299 const svalue
&b_sval
= *b
.get_symbolic_byte_offset ();
301 linear_op
op_a (NULL
, NULL
, NULL
);
302 linear_op
op_b (NULL
, NULL
, NULL
);
303 if (linear_op::from_svalue (a_sval
, &op_a
)
304 && linear_op::from_svalue (b_sval
, &op_b
))
306 tristate ts
= linear_op::less (op_a
, op_b
);
309 else if (ts
.is_false ())
312 /* Use svalue's deterministic order, for now. */
313 return (svalue::cmp_ptr (a
.get_symbolic_byte_offset (),
314 b
.get_symbolic_byte_offset ())
318 /* Symbolic vs concrete: put all symbolic after all concrete. */
324 /* Concrete vs symbolic: put all concrete before all symbolic. */
327 /* Concrete vs concrete. */
328 return a
.get_bit_offset () < b
.get_bit_offset ();
333 operator<= (const region_offset
&a
, const region_offset
&b
)
339 /* Symbolic vs symbolic. */
340 const svalue
&a_sval
= *a
.get_symbolic_byte_offset ();
341 const svalue
&b_sval
= *b
.get_symbolic_byte_offset ();
343 linear_op
op_a (NULL
, NULL
, NULL
);
344 linear_op
op_b (NULL
, NULL
, NULL
);
345 if (linear_op::from_svalue (a_sval
, &op_a
)
346 && linear_op::from_svalue (b_sval
, &op_b
))
348 tristate ts
= linear_op::le (op_a
, op_b
);
351 else if (ts
.is_false ())
354 /* Use svalue's deterministic order, for now. */
355 return (svalue::cmp_ptr (a
.get_symbolic_byte_offset (),
356 b
.get_symbolic_byte_offset ())
360 /* Symbolic vs concrete: put all symbolic after all concrete. */
366 /* Concrete vs symbolic: put all concrete before all symbolic. */
369 /* Concrete vs concrete. */
370 return a
.get_bit_offset () <= b
.get_bit_offset ();
375 operator> (const region_offset
&a
, const region_offset
&b
)
381 operator>= (const region_offset
&a
, const region_offset
&b
)
387 strip_types (const region_offset
&offset
, region_model_manager
&mgr
)
389 if (offset
.symbolic_p ())
390 return region_offset::make_symbolic
391 (offset
.get_base_region (),
392 strip_types (offset
.get_symbolic_byte_offset (),
398 /* class region and its various subclasses. */
404 delete m_cached_offset
;
407 /* Determine the base region for this region: when considering bindings
408 for this region, the base region is the ancestor which identifies
409 which cluster they should be partitioned into.
410 Regions within the same struct/union/array are in the same cluster.
411 Different decls are in different clusters. */
414 region::get_base_region () const
416 const region
*iter
= this;
419 switch (iter
->get_kind ())
426 iter
= iter
->get_parent_region ();
429 iter
= iter
->dyn_cast_cast_region ()->get_original_region ();
438 /* Return true if get_base_region() == this for this region. */
441 region::base_region_p () const
445 /* Region kinds representing a descendent of a base region. */
459 /* Return true if this region is ELDER or one of its descendents. */
462 region::descendent_of_p (const region
*elder
) const
464 const region
*iter
= this;
469 if (iter
->get_kind () == RK_CAST
)
470 iter
= iter
->dyn_cast_cast_region ()->get_original_region ();
472 iter
= iter
->get_parent_region ();
477 /* If this region is a frame_region, or a descendent of one, return it.
478 Otherwise return NULL. */
481 region::maybe_get_frame_region () const
483 const region
*iter
= this;
486 if (const frame_region
*frame_reg
= iter
->dyn_cast_frame_region ())
488 if (iter
->get_kind () == RK_CAST
)
489 iter
= iter
->dyn_cast_cast_region ()->get_original_region ();
491 iter
= iter
->get_parent_region ();
496 /* Get the memory space of this region. */
499 region::get_memory_space () const
501 const region
*iter
= this;
504 switch (iter
->get_kind ())
509 return MEMSPACE_GLOBALS
;
513 return MEMSPACE_CODE
;
517 return MEMSPACE_STACK
;
519 case RK_HEAP_ALLOCATED
:
520 return MEMSPACE_HEAP
;
522 return MEMSPACE_READONLY_DATA
;
524 return MEMSPACE_PRIVATE
;
526 if (iter
->get_kind () == RK_CAST
)
527 iter
= iter
->dyn_cast_cast_region ()->get_original_region ();
529 iter
= iter
->get_parent_region ();
531 return MEMSPACE_UNKNOWN
;
534 /* Subroutine for use by region_model_manager::get_or_create_initial_value.
535 Return true if this region has an initial_svalue.
536 Return false if attempting to use INIT_VAL(this_region) should give
537 the "UNINITIALIZED" poison value. */
540 region::can_have_initial_svalue_p () const
542 const region
*base_reg
= get_base_region ();
544 /* Check for memory spaces that are uninitialized by default. */
545 enum memory_space mem_space
= base_reg
->get_memory_space ();
550 case MEMSPACE_UNKNOWN
:
552 case MEMSPACE_GLOBALS
:
553 case MEMSPACE_READONLY_DATA
:
554 case MEMSPACE_PRIVATE
:
555 /* Such regions have initial_svalues. */
559 /* Heap allocations are uninitialized by default. */
563 if (tree decl
= base_reg
->maybe_get_decl ())
565 /* See the assertion in frame_region::get_region_for_local for the
566 tree codes we need to handle here. */
567 switch (TREE_CODE (decl
))
573 /* Parameters have initial values. */
578 /* Function locals don't have initial values. */
583 tree ssa_name
= decl
;
584 /* SSA names that are the default defn of a PARM_DECL
585 have initial_svalues; other SSA names don't. */
586 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name
)
587 && SSA_NAME_VAR (ssa_name
)
588 && TREE_CODE (SSA_NAME_VAR (ssa_name
)) == PARM_DECL
)
596 /* If we have an on-stack region that isn't associated with a decl
597 or SSA name, then we have VLA/alloca, which is uninitialized. */
602 /* For regions within a global decl, get the svalue for the initial
603 value of this region when the program starts, caching the result. */
606 region::get_initial_value_at_main (region_model_manager
*mgr
) const
608 if (!m_cached_init_sval_at_main
)
609 m_cached_init_sval_at_main
= calc_initial_value_at_main (mgr
);
610 return m_cached_init_sval_at_main
;
613 /* Implementation of region::get_initial_value_at_main. */
616 region::calc_initial_value_at_main (region_model_manager
*mgr
) const
618 const decl_region
*base_reg
= get_base_region ()->dyn_cast_decl_region ();
619 gcc_assert (base_reg
);
621 /* Attempt to get the initializer value for base_reg. */
622 if (const svalue
*base_reg_init
623 = base_reg
->get_svalue_for_initializer (mgr
))
625 if (this == base_reg
)
626 return base_reg_init
;
629 /* Get the value for REG within base_reg_init. */
630 binding_cluster
c (base_reg
);
631 c
.bind (mgr
->get_store_manager (), base_reg
, base_reg_init
);
633 = c
.get_any_binding (mgr
->get_store_manager (), this);
637 sval
= mgr
->get_or_create_cast (get_type (), sval
);
643 /* Otherwise, return INIT_VAL(REG). */
644 return mgr
->get_or_create_initial_value (this);
647 /* If this region is a decl_region, return the decl.
648 Otherwise return NULL. */
651 region::maybe_get_decl () const
653 if (const decl_region
*decl_reg
= dyn_cast_decl_region ())
654 return decl_reg
->get_decl ();
658 /* Get the region_offset for this region (calculating it on the
659 first call and caching it internally). */
662 region::get_offset (region_model_manager
*mgr
) const
665 m_cached_offset
= new region_offset (calc_offset (mgr
));
666 return *m_cached_offset
;
669 /* Get the region_offset for immediately beyond this region. */
672 region::get_next_offset (region_model_manager
*mgr
) const
674 region_offset start
= get_offset (mgr
);
677 if (get_bit_size (&bit_size
))
679 if (start
.concrete_p ())
681 bit_offset_t next_bit_offset
= start
.get_bit_offset () + bit_size
;
682 return region_offset::make_concrete (start
.get_base_region (),
687 const svalue
*start_byte_offset_sval
= start
.calc_symbolic_byte_offset (mgr
);
688 const svalue
*byte_size_sval
= get_byte_size_sval (mgr
);
689 const svalue
*sum_sval
690 = mgr
->get_or_create_binop (size_type_node
,
692 start_byte_offset_sval
,
694 return region_offset::make_symbolic (start
.get_base_region (),
698 /* Base class implementation of region::get_byte_size vfunc.
699 If the size of this region (in bytes) is known statically, write it to *OUT
701 Otherwise return false. */
704 region::get_byte_size (byte_size_t
*out
) const
706 tree type
= get_type ();
708 /* Bail out e.g. for heap-allocated regions. */
712 HOST_WIDE_INT bytes
= int_size_in_bytes (type
);
719 /* Base implementation of region::get_byte_size_sval vfunc. */
722 region::get_byte_size_sval (region_model_manager
*mgr
) const
724 tree type
= get_type ();
726 /* Bail out e.g. for heap-allocated regions. */
728 return mgr
->get_or_create_unknown_svalue (size_type_node
);
730 HOST_WIDE_INT bytes
= int_size_in_bytes (type
);
732 return mgr
->get_or_create_unknown_svalue (size_type_node
);
734 tree byte_size
= size_in_bytes (type
);
735 if (TREE_TYPE (byte_size
) != size_type_node
)
736 byte_size
= fold_build1 (NOP_EXPR
, size_type_node
, byte_size
);
737 return mgr
->get_or_create_constant_svalue (byte_size
);
740 /* Attempt to get the size of TYPE in bits.
741 If successful, return true and write the size to *OUT.
742 Otherwise return false. */
745 int_size_in_bits (const_tree type
, bit_size_t
*out
)
747 if (INTEGRAL_TYPE_P (type
))
749 *out
= TYPE_PRECISION (type
);
753 tree sz
= TYPE_SIZE (type
);
755 && tree_fits_uhwi_p (sz
)
756 /* If the size is zero, then we may have a zero-sized
757 array; handle such cases by returning false. */
758 && !integer_zerop (sz
))
760 *out
= TREE_INT_CST_LOW (sz
);
767 /* Base implementation of region::get_bit_size_sval vfunc. */
770 region::get_bit_size_sval (region_model_manager
*mgr
) const
772 tree type
= get_type ();
774 /* Bail out e.g. for heap-allocated regions. */
776 return mgr
->get_or_create_unknown_svalue (size_type_node
);
779 if (!int_size_in_bits (type
, &bits
))
780 return mgr
->get_or_create_unknown_svalue (size_type_node
);
782 return mgr
->get_or_create_int_cst (size_type_node
, bits
);
785 /* If the size of this region (in bits) is known statically, write it to *OUT
787 Otherwise return false. */
790 region::get_bit_size (bit_size_t
*out
) const
792 tree type
= get_type ();
794 /* Bail out e.g. for heap-allocated regions. */
798 return int_size_in_bits (type
, out
);
801 /* Get the field within RECORD_TYPE at BIT_OFFSET. */
804 get_field_at_bit_offset (tree record_type
, bit_offset_t bit_offset
)
806 gcc_assert (TREE_CODE (record_type
) == RECORD_TYPE
);
810 /* Find the first field that has an offset > BIT_OFFSET,
811 then return the one preceding it.
812 Skip other trees within the chain, such as FUNCTION_DECLs. */
813 tree last_field
= NULL_TREE
;
814 for (tree iter
= TYPE_FIELDS (record_type
); iter
!= NULL_TREE
;
815 iter
= DECL_CHAIN (iter
))
817 if (TREE_CODE (iter
) == FIELD_DECL
)
819 int iter_field_offset
= int_bit_position (iter
);
820 if (bit_offset
< iter_field_offset
)
828 /* Populate *OUT with descendent regions of type TYPE that match
829 RELATIVE_BIT_OFFSET and SIZE_IN_BITS within this region. */
832 region::get_subregions_for_binding (region_model_manager
*mgr
,
833 bit_offset_t relative_bit_offset
,
834 bit_size_t size_in_bits
,
836 auto_vec
<const region
*> *out
) const
838 if (get_type () == NULL_TREE
|| type
== NULL_TREE
)
840 if (relative_bit_offset
== 0
841 && types_compatible_p (get_type (), type
))
843 out
->safe_push (this);
846 switch (TREE_CODE (get_type ()))
850 tree element_type
= TREE_TYPE (get_type ());
851 HOST_WIDE_INT hwi_byte_size
= int_size_in_bytes (element_type
);
852 if (hwi_byte_size
> 0)
854 HOST_WIDE_INT bits_per_element
855 = hwi_byte_size
<< LOG2_BITS_PER_UNIT
;
856 HOST_WIDE_INT element_index
857 = (relative_bit_offset
.to_shwi () / bits_per_element
);
858 tree element_index_cst
859 = build_int_cst (integer_type_node
, element_index
);
860 HOST_WIDE_INT inner_bit_offset
861 = relative_bit_offset
.to_shwi () % bits_per_element
;
862 const region
*subregion
= mgr
->get_element_region
864 mgr
->get_or_create_constant_svalue (element_index_cst
));
865 subregion
->get_subregions_for_binding (mgr
, inner_bit_offset
,
866 size_in_bits
, type
, out
);
872 /* The bit offset might be *within* one of the fields (such as
873 with nested structs).
874 So we want to find the enclosing field, adjust the offset,
876 if (tree field
= get_field_at_bit_offset (get_type (),
877 relative_bit_offset
))
879 int field_bit_offset
= int_bit_position (field
);
880 const region
*subregion
= mgr
->get_field_region (this, field
);
881 subregion
->get_subregions_for_binding
882 (mgr
, relative_bit_offset
- field_bit_offset
,
883 size_in_bits
, type
, out
);
889 for (tree field
= TYPE_FIELDS (get_type ()); field
!= NULL_TREE
;
890 field
= DECL_CHAIN (field
))
892 if (TREE_CODE (field
) != FIELD_DECL
)
894 const region
*subregion
= mgr
->get_field_region (this, field
);
895 subregion
->get_subregions_for_binding (mgr
,
909 /* Walk from this region up to the base region within its cluster, calculating
910 the offset relative to the base region, either as an offset in bits,
911 or a symbolic offset. */
914 region::calc_offset (region_model_manager
*mgr
) const
916 const region
*iter_region
= this;
917 bit_offset_t accum_bit_offset
= 0;
918 const svalue
*accum_byte_sval
= NULL
;
922 switch (iter_region
->get_kind ())
931 = iter_region
->get_relative_symbolic_offset (mgr
);
933 = mgr
->get_or_create_binop (ptrdiff_type_node
, PLUS_EXPR
,
934 accum_byte_sval
, sval
);
935 iter_region
= iter_region
->get_parent_region ();
939 bit_offset_t rel_bit_offset
;
940 if (iter_region
->get_relative_concrete_offset (&rel_bit_offset
))
942 accum_bit_offset
+= rel_bit_offset
;
943 iter_region
= iter_region
->get_parent_region ();
947 /* If the iter_region is not concrete anymore, convert the
948 accumulated bits to a svalue in bytes and revisit the
949 iter_region collecting the symbolic value. */
950 byte_offset_t byte_offset
= accum_bit_offset
/ BITS_PER_UNIT
;
951 tree offset_tree
= wide_int_to_tree (ptrdiff_type_node
,
954 = mgr
->get_or_create_constant_svalue (offset_tree
);
959 iter_region
= iter_region
->get_parent_region ();
964 const cast_region
*cast_reg
965 = as_a
<const cast_region
*> (iter_region
);
966 iter_region
= cast_reg
->get_original_region ();
971 return accum_byte_sval
972 ? region_offset::make_symbolic (iter_region
,
974 : region_offset::make_concrete (iter_region
,
979 return accum_byte_sval
? region_offset::make_symbolic (iter_region
,
981 : region_offset::make_concrete (iter_region
,
985 /* Base implementation of region::get_relative_concrete_offset vfunc. */
988 region::get_relative_concrete_offset (bit_offset_t
*) const
993 /* Base implementation of region::get_relative_symbolic_offset vfunc. */
996 region::get_relative_symbolic_offset (region_model_manager
*mgr
) const
998 return mgr
->get_or_create_unknown_svalue (ptrdiff_type_node
);
1001 /* Attempt to get the position and size of this region expressed as a
1002 concrete range of bytes relative to its parent.
1003 If successful, return true and write to *OUT.
1004 Otherwise return false. */
1007 region::get_relative_concrete_byte_range (byte_range
*out
) const
1009 /* We must have a concrete offset relative to the parent. */
1010 bit_offset_t rel_bit_offset
;
1011 if (!get_relative_concrete_offset (&rel_bit_offset
))
1013 /* ...which must be a whole number of bytes. */
1014 if (rel_bit_offset
% BITS_PER_UNIT
!= 0)
1016 byte_offset_t start_byte_offset
= rel_bit_offset
/ BITS_PER_UNIT
;
1018 /* We must have a concrete size, which must be a whole number
1020 byte_size_t num_bytes
;
1021 if (!get_byte_size (&num_bytes
))
1025 *out
= byte_range (start_byte_offset
, num_bytes
);
1029 /* Dump a description of this region to stderr. */
1032 region::dump (bool simple
) const
1035 pp_format_decoder (&pp
) = default_tree_printer
;
1036 pp_show_color (&pp
) = pp_show_color (global_dc
->printer
);
1037 pp
.buffer
->stream
= stderr
;
1038 dump_to_pp (&pp
, simple
);
1043 /* Return a new json::string describing the region. */
1046 region::to_json () const
1048 label_text desc
= get_desc (true);
1049 json::value
*reg_js
= new json::string (desc
.get ());
1054 region::maybe_print_for_user (pretty_printer
*pp
,
1055 const region_model
&) const
1057 switch (get_kind ())
1063 const decl_region
*reg
= (const decl_region
*)this;
1064 tree decl
= reg
->get_decl ();
1065 if (TREE_CODE (decl
) == SSA_NAME
)
1066 decl
= SSA_NAME_VAR (decl
);
1067 print_expr_for_user (pp
, decl
);
1075 /* Generate a description of this region. */
1077 DEBUG_FUNCTION label_text
1078 region::get_desc (bool simple
) const
1081 pp_format_decoder (&pp
) = default_tree_printer
;
1082 dump_to_pp (&pp
, simple
);
1083 return label_text::take (xstrdup (pp_formatted_text (&pp
)));
1086 /* Base implementation of region::accept vfunc.
1087 Subclass implementations should chain up to this. */
1090 region::accept (visitor
*v
) const
1092 v
->visit_region (this);
1094 m_parent
->accept (v
);
1097 /* Return true if this is a symbolic region for deferencing an
1099 We shouldn't attempt to bind values for this region (but
1100 can unbind values for other regions). */
1103 region::symbolic_for_unknown_ptr_p () const
1105 if (const symbolic_region
*sym_reg
= dyn_cast_symbolic_region ())
1106 if (sym_reg
->get_pointer ()->get_kind () == SK_UNKNOWN
)
1111 /* Return true if this is a symbolic region. */
1114 region::symbolic_p () const
1116 return get_kind () == RK_SYMBOLIC
;
1119 /* Return true if this region is known to be zero bits in size. */
1122 region::empty_p () const
1124 bit_size_t num_bits
;
1125 if (get_bit_size (&num_bits
))
1131 /* Return true if this is a region for a decl with name DECL_NAME.
1132 Intended for use when debugging (for assertions and conditional
1136 region::is_named_decl_p (const char *decl_name
) const
1138 if (tree decl
= maybe_get_decl ())
1139 if (DECL_NAME (decl
)
1140 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (decl
)), decl_name
))
1145 /* region's ctor. */
1147 region::region (complexity c
, symbol::id_t id
, const region
*parent
, tree type
)
1149 m_parent (parent
), m_type (type
),
1150 m_cached_offset (NULL
), m_cached_init_sval_at_main (NULL
)
1152 gcc_assert (type
== NULL_TREE
|| TYPE_P (type
));
1155 /* Comparator for use by vec<const region *>::qsort,
1156 using their IDs to order them. */
1159 region::cmp_ptr_ptr (const void *p1
, const void *p2
)
1161 const region
* const *reg1
= (const region
* const *)p1
;
1162 const region
* const *reg2
= (const region
* const *)p2
;
1164 return cmp_ids (*reg1
, *reg2
);
1167 /* Determine if a pointer to this region must be non-NULL.
1169 Generally, pointers to regions must be non-NULL, but pointers
1170 to symbolic_regions might, in fact, be NULL.
1172 This allows us to simulate functions like malloc and calloc with:
1173 - only one "outcome" from each statement,
1174 - the idea that the pointer is on the heap if non-NULL
1175 - the possibility that the pointer could be NULL
1176 - the idea that successive values returned from malloc are non-equal
1177 - to be able to zero-fill for calloc. */
1180 region::non_null_p () const
1182 switch (get_kind ())
1187 /* Are we within a symbolic_region? If so, it could be NULL, and we
1188 have to fall back on the constraints. */
1190 case RK_HEAP_ALLOCATED
:
1195 /* Return true iff this region is defined in terms of SVAL. */
1198 region::involves_p (const svalue
*sval
) const
1200 if (const symbolic_region
*symbolic_reg
= dyn_cast_symbolic_region ())
1202 if (symbolic_reg
->get_pointer ()->involves_p (sval
))
1209 /* Comparator for trees to impose a deterministic ordering on
1213 tree_cmp (const_tree t1
, const_tree t2
)
1218 /* Test tree codes first. */
1219 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1220 return TREE_CODE (t1
) - TREE_CODE (t2
);
1222 /* From this point on, we know T1 and T2 have the same tree code. */
1226 if (DECL_NAME (t1
) && DECL_NAME (t2
))
1227 return strcmp (IDENTIFIER_POINTER (DECL_NAME (t1
)),
1228 IDENTIFIER_POINTER (DECL_NAME (t2
)));
1233 else if (DECL_NAME (t2
))
1236 return DECL_UID (t1
) - DECL_UID (t2
);
1240 switch (TREE_CODE (t1
))
1244 if (SSA_NAME_VAR (t1
) && SSA_NAME_VAR (t2
))
1246 int var_cmp
= tree_cmp (SSA_NAME_VAR (t1
), SSA_NAME_VAR (t2
));
1249 return SSA_NAME_VERSION (t1
) - SSA_NAME_VERSION (t2
);
1253 if (SSA_NAME_VAR (t1
))
1255 else if (SSA_NAME_VAR (t2
))
1258 return SSA_NAME_VERSION (t1
) - SSA_NAME_VERSION (t2
);
1264 return tree_int_cst_compare (t1
, t2
);
1268 const real_value
*rv1
= TREE_REAL_CST_PTR (t1
);
1269 const real_value
*rv2
= TREE_REAL_CST_PTR (t2
);
1270 if (real_compare (UNORDERED_EXPR
, rv1
, rv2
))
1272 /* Impose an arbitrary order on NaNs relative to other NaNs
1274 if (int cmp_isnan
= real_isnan (rv1
) - real_isnan (rv2
))
1276 if (int cmp_issignaling_nan
1277 = real_issignaling_nan (rv1
) - real_issignaling_nan (rv2
))
1278 return cmp_issignaling_nan
;
1279 return real_isneg (rv1
) - real_isneg (rv2
);
1281 if (real_compare (LT_EXPR
, rv1
, rv2
))
1283 if (real_compare (GT_EXPR
, rv1
, rv2
))
1289 return strcmp (TREE_STRING_POINTER (t1
),
1290 TREE_STRING_POINTER (t2
));
1302 /* qsort comparator for trees to impose a deterministic ordering on
1306 tree_cmp (const void *p1
, const void *p2
)
1308 const_tree t1
= *(const_tree
const *)p1
;
1309 const_tree t2
= *(const_tree
const *)p2
;
1311 return tree_cmp (t1
, t2
);
1314 /* class frame_region : public space_region. */
1316 frame_region::~frame_region ()
1318 for (map_t::iterator iter
= m_locals
.begin ();
1319 iter
!= m_locals
.end ();
1321 delete (*iter
).second
;
1325 frame_region::accept (visitor
*v
) const
1328 if (m_calling_frame
)
1329 m_calling_frame
->accept (v
);
1332 /* Implementation of region::dump_to_pp vfunc for frame_region. */
1335 frame_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1338 pp_printf (pp
, "frame: %qs@%i", function_name (&m_fun
), get_stack_depth ());
1340 pp_printf (pp
, "frame_region(%qs, index: %i, depth: %i)",
1341 function_name (&m_fun
), m_index
, get_stack_depth ());
1345 frame_region::get_region_for_local (region_model_manager
*mgr
,
1347 const region_model_context
*ctxt
) const
1351 /* Verify that EXPR is a local or SSA name, and that it's for the
1352 correct function for this stack frame. */
1353 gcc_assert (TREE_CODE (expr
) == PARM_DECL
1354 || TREE_CODE (expr
) == VAR_DECL
1355 || TREE_CODE (expr
) == SSA_NAME
1356 || TREE_CODE (expr
) == RESULT_DECL
);
1357 switch (TREE_CODE (expr
))
1362 gcc_assert (!is_global_var (expr
));
1366 gcc_assert (DECL_CONTEXT (expr
) == m_fun
.decl
);
1370 if (tree var
= SSA_NAME_VAR (expr
))
1373 gcc_assert (DECL_CONTEXT (var
) == m_fun
.decl
);
1376 if (const extrinsic_state
*ext_state
= ctxt
->get_ext_state ())
1377 if (const supergraph
*sg
1378 = ext_state
->get_engine ()->get_supergraph ())
1380 const gimple
*def_stmt
= SSA_NAME_DEF_STMT (expr
);
1381 const supernode
*snode
1382 = sg
->get_supernode_for_stmt (def_stmt
);
1383 gcc_assert (snode
->get_function () == &m_fun
);
1390 /* Ideally we'd use mutable here. */
1391 map_t
&mutable_locals
= const_cast <map_t
&> (m_locals
);
1393 if (decl_region
**slot
= mutable_locals
.get (expr
))
1396 = new decl_region (mgr
->alloc_symbol_id (), this, expr
);
1397 mutable_locals
.put (expr
, reg
);
1401 /* class globals_region : public space_region. */
1403 /* Implementation of region::dump_to_pp vfunc for globals_region. */
1406 globals_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1409 pp_string (pp
, "::");
1411 pp_string (pp
, "globals");
1414 /* class code_region : public map_region. */
1416 /* Implementation of region::dump_to_pp vfunc for code_region. */
1419 code_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1422 pp_string (pp
, "code region");
1424 pp_string (pp
, "code_region()");
1427 /* class function_region : public region. */
1429 /* Implementation of region::dump_to_pp vfunc for function_region. */
1432 function_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1436 dump_quoted_tree (pp
, m_fndecl
);
1440 pp_string (pp
, "function_region(");
1441 dump_quoted_tree (pp
, m_fndecl
);
1442 pp_string (pp
, ")");
1446 /* class label_region : public region. */
1448 /* Implementation of region::dump_to_pp vfunc for label_region. */
1451 label_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1455 dump_quoted_tree (pp
, m_label
);
1459 pp_string (pp
, "label_region(");
1460 dump_quoted_tree (pp
, m_label
);
1461 pp_string (pp
, ")");
1465 /* class stack_region : public region. */
1467 /* Implementation of region::dump_to_pp vfunc for stack_region. */
1470 stack_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1473 pp_string (pp
, "stack region");
1475 pp_string (pp
, "stack_region()");
1478 /* class heap_region : public region. */
1480 /* Implementation of region::dump_to_pp vfunc for heap_region. */
1483 heap_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1486 pp_string (pp
, "heap region");
1488 pp_string (pp
, "heap_region()");
1491 /* class root_region : public region. */
1493 /* root_region's ctor. */
1495 root_region::root_region (symbol::id_t id
)
1496 : region (complexity (1, 1), id
, NULL
, NULL_TREE
)
1500 /* Implementation of region::dump_to_pp vfunc for root_region. */
1503 root_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1506 pp_string (pp
, "root region");
1508 pp_string (pp
, "root_region()");
1511 /* class thread_local_region : public space_region. */
1514 thread_local_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1517 pp_string (pp
, "thread_local_region");
1519 pp_string (pp
, "thread_local_region()");
1522 /* class symbolic_region : public map_region. */
1524 /* symbolic_region's ctor. */
1526 symbolic_region::symbolic_region (symbol::id_t id
, region
*parent
,
1527 const svalue
*sval_ptr
)
1528 : region (complexity::from_pair (parent
, sval_ptr
), id
, parent
,
1529 (sval_ptr
->get_type ()
1530 ? TREE_TYPE (sval_ptr
->get_type ())
1532 m_sval_ptr (sval_ptr
)
1536 /* Implementation of region::accept vfunc for symbolic_region. */
1539 symbolic_region::accept (visitor
*v
) const
1542 m_sval_ptr
->accept (v
);
1545 /* Implementation of region::dump_to_pp vfunc for symbolic_region. */
1548 symbolic_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1552 pp_string (pp
, "(*");
1553 m_sval_ptr
->dump_to_pp (pp
, simple
);
1554 pp_string (pp
, ")");
1558 pp_string (pp
, "symbolic_region(");
1559 get_parent_region ()->dump_to_pp (pp
, simple
);
1562 pp_string (pp
, ", ");
1563 print_quoted_type (pp
, get_type ());
1565 pp_string (pp
, ", ");
1566 m_sval_ptr
->dump_to_pp (pp
, simple
);
1567 pp_string (pp
, ")");
1571 /* class decl_region : public region. */
1573 /* Implementation of region::dump_to_pp vfunc for decl_region. */
1576 decl_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1579 pp_printf (pp
, "%E", m_decl
);
1582 pp_string (pp
, "decl_region(");
1583 get_parent_region ()->dump_to_pp (pp
, simple
);
1584 pp_string (pp
, ", ");
1585 print_quoted_type (pp
, get_type ());
1586 pp_printf (pp
, ", %qE)", m_decl
);
1590 /* Get the stack depth for the frame containing this decl, or 0
1594 decl_region::get_stack_depth () const
1596 if (get_parent_region () == NULL
)
1598 if (const frame_region
*frame_reg
1599 = get_parent_region ()->dyn_cast_frame_region ())
1600 return frame_reg
->get_stack_depth ();
1604 /* If the underlying decl is in the global constant pool,
1605 return an svalue representing the constant value.
1606 Otherwise return NULL. */
1609 decl_region::maybe_get_constant_value (region_model_manager
*mgr
) const
1612 && DECL_IN_CONSTANT_POOL (m_decl
)
1613 && DECL_INITIAL (m_decl
)
1614 && TREE_CODE (DECL_INITIAL (m_decl
)) == CONSTRUCTOR
)
1615 return get_svalue_for_constructor (DECL_INITIAL (m_decl
), mgr
);
1619 /* Implementation of decl_region::get_svalue_for_constructor
1620 for when the cached value hasn't yet been calculated. */
1623 decl_region::calc_svalue_for_constructor (tree ctor
,
1624 region_model_manager
*mgr
) const
1626 /* Create a binding map, applying ctor to it, using this
1627 decl_region as the base region when building child regions
1628 for offset calculations. */
1630 if (!map
.apply_ctor_to_region (this, ctor
, mgr
))
1631 return mgr
->get_or_create_unknown_svalue (get_type ());
1633 /* Return a compound svalue for the map we built. */
1634 return mgr
->get_or_create_compound_svalue (get_type (), map
);
1637 /* Get an svalue for CTOR, a CONSTRUCTOR for this region's decl. */
1640 decl_region::get_svalue_for_constructor (tree ctor
,
1641 region_model_manager
*mgr
) const
1643 gcc_assert (!TREE_CLOBBER_P (ctor
));
1644 gcc_assert (ctor
== DECL_INITIAL (m_decl
));
1647 m_ctor_svalue
= calc_svalue_for_constructor (ctor
, mgr
);
1649 return m_ctor_svalue
;
1652 /* For use on decl_regions for global variables.
1654 Get an svalue for the initial value of this region at entry to
1655 "main" (either based on DECL_INITIAL, or implicit initialization to
1658 Return NULL if there is a problem. */
1661 decl_region::get_svalue_for_initializer (region_model_manager
*mgr
) const
1663 tree init
= DECL_INITIAL (m_decl
);
1666 /* If we have an "extern" decl then there may be an initializer in
1668 if (DECL_EXTERNAL (m_decl
))
1674 /* Implicit initialization to zero; use a compound_svalue for it.
1675 Doing so requires that we have a concrete binding for this region,
1676 which can fail if we have a region with unknown size
1677 (e.g. "extern const char arr[];"). */
1678 const binding_key
*binding
1679 = binding_key::make (mgr
->get_store_manager (), this);
1680 if (binding
->symbolic_p ())
1683 /* If we don't care about tracking the content of this region, then
1684 it's unused, and the value doesn't matter. */
1688 binding_cluster
c (this);
1689 c
.zero_fill_region (mgr
->get_store_manager (), this);
1690 return mgr
->get_or_create_compound_svalue (TREE_TYPE (m_decl
),
1694 /* LTO can write out error_mark_node as the DECL_INITIAL for simple scalar
1695 values (to avoid writing out an extra section). */
1696 if (init
== error_mark_node
)
1699 if (TREE_CODE (init
) == CONSTRUCTOR
)
1700 return get_svalue_for_constructor (init
, mgr
);
1702 /* Reuse the get_rvalue logic from region_model. */
1703 region_model
m (mgr
);
1704 return m
.get_rvalue (path_var (init
, 0), NULL
);
1707 /* Subroutine of symnode_requires_tracking_p; return true if REF
1708 might imply that we should be tracking the value of its decl. */
1711 ipa_ref_requires_tracking (ipa_ref
*ref
)
1713 /* If we have a load/store/alias of the symbol, then we'll track
1714 the decl's value. */
1715 if (ref
->use
!= IPA_REF_ADDR
)
1718 if (ref
->stmt
== NULL
)
1721 switch (ref
->stmt
->code
)
1727 cgraph_node
*caller_cnode
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1728 if (caller_cnode
== NULL
)
1730 cgraph_edge
*edge
= caller_cnode
->get_edge (ref
->stmt
);
1733 if (edge
->callee
== NULL
)
1734 return true; /* e.g. call through function ptr. */
1735 if (edge
->callee
->definition
)
1737 /* If we get here, then this ref is a pointer passed to
1738 a function we don't have the definition for. */
1744 const gasm
*asm_stmt
= as_a
<const gasm
*> (ref
->stmt
);
1745 if (gimple_asm_noutputs (asm_stmt
) > 0)
1747 if (gimple_asm_nclobbers (asm_stmt
) > 0)
1749 /* If we get here, then this ref is the decl being passed
1750 by pointer to asm with no outputs. */
1757 /* Determine if the decl for SYMNODE should have binding_clusters
1758 in our state objects; return false to optimize away tracking
1759 certain decls in our state objects, as an optimization. */
1762 symnode_requires_tracking_p (symtab_node
*symnode
)
1764 gcc_assert (symnode
);
1765 if (symnode
->externally_visible
)
1767 tree context_fndecl
= DECL_CONTEXT (symnode
->decl
);
1768 if (context_fndecl
== NULL
)
1770 if (TREE_CODE (context_fndecl
) != FUNCTION_DECL
)
1772 for (auto ref
: symnode
->ref_list
.referring
)
1773 if (ipa_ref_requires_tracking (ref
))
1776 /* If we get here, then we don't have uses of this decl that require
1777 tracking; we never read from it or write to it explicitly. */
1781 /* Subroutine of decl_region ctor: determine whether this decl_region
1782 can have binding_clusters; return false to optimize away tracking
1783 of certain decls in our state objects, as an optimization. */
1786 decl_region::calc_tracked_p (tree decl
)
1788 /* Precondition of symtab_node::get. */
1789 if (TREE_CODE (decl
) == VAR_DECL
1790 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
) || in_lto_p
))
1791 if (symtab_node
*symnode
= symtab_node::get (decl
))
1792 return symnode_requires_tracking_p (symnode
);
1796 /* class field_region : public region. */
1798 /* Implementation of region::dump_to_pp vfunc for field_region. */
1801 field_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1805 get_parent_region ()->dump_to_pp (pp
, simple
);
1806 pp_string (pp
, ".");
1807 pp_printf (pp
, "%E", m_field
);
1811 pp_string (pp
, "field_region(");
1812 get_parent_region ()->dump_to_pp (pp
, simple
);
1813 pp_string (pp
, ", ");
1814 print_quoted_type (pp
, get_type ());
1815 pp_printf (pp
, ", %qE)", m_field
);
1819 /* Implementation of region::get_relative_concrete_offset vfunc
1820 for field_region. */
1823 field_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1825 /* Compare with e.g. gimple-fold.cc's
1826 fold_nonarray_ctor_reference. */
1827 tree byte_offset
= DECL_FIELD_OFFSET (m_field
);
1828 if (TREE_CODE (byte_offset
) != INTEGER_CST
)
1830 tree field_offset
= DECL_FIELD_BIT_OFFSET (m_field
);
1831 /* Compute bit offset of the field. */
1832 offset_int bitoffset
1833 = (wi::to_offset (field_offset
)
1834 + (wi::to_offset (byte_offset
) << LOG2_BITS_PER_UNIT
));
1840 /* Implementation of region::get_relative_symbolic_offset vfunc
1842 If known, the returned svalue is equal to the offset converted to bytes and
1846 field_region::get_relative_symbolic_offset (region_model_manager
*mgr
) const
1849 if (get_relative_concrete_offset (&out
))
1852 = wide_int_to_tree (ptrdiff_type_node
, out
/ BITS_PER_UNIT
);
1853 return mgr
->get_or_create_constant_svalue (cst_tree
);
1855 return mgr
->get_or_create_unknown_svalue (ptrdiff_type_node
);
1858 /* class element_region : public region. */
1860 /* Implementation of region::accept vfunc for element_region. */
1863 element_region::accept (visitor
*v
) const
1866 m_index
->accept (v
);
1869 /* Implementation of region::dump_to_pp vfunc for element_region. */
1872 element_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1876 //pp_string (pp, "(");
1877 get_parent_region ()->dump_to_pp (pp
, simple
);
1878 pp_string (pp
, "[");
1879 m_index
->dump_to_pp (pp
, simple
);
1880 pp_string (pp
, "]");
1881 //pp_string (pp, ")");
1885 pp_string (pp
, "element_region(");
1886 get_parent_region ()->dump_to_pp (pp
, simple
);
1887 pp_string (pp
, ", ");
1888 print_quoted_type (pp
, get_type ());
1889 pp_string (pp
, ", ");
1890 m_index
->dump_to_pp (pp
, simple
);
1891 pp_printf (pp
, ")");
1895 /* Implementation of region::get_relative_concrete_offset vfunc
1896 for element_region. */
1899 element_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1901 if (tree idx_cst
= m_index
->maybe_get_constant ())
1903 gcc_assert (TREE_CODE (idx_cst
) == INTEGER_CST
);
1905 tree elem_type
= get_type ();
1906 offset_int element_idx
= wi::to_offset (idx_cst
);
1908 /* First, use int_size_in_bytes, to reject the case where we
1909 have an incomplete type, or a non-constant value. */
1910 HOST_WIDE_INT hwi_byte_size
= int_size_in_bytes (elem_type
);
1911 if (hwi_byte_size
> 0)
1913 offset_int element_bit_size
1914 = hwi_byte_size
<< LOG2_BITS_PER_UNIT
;
1915 offset_int element_bit_offset
1916 = element_idx
* element_bit_size
;
1917 *out
= element_bit_offset
;
1924 /* Implementation of region::get_relative_symbolic_offset vfunc
1925 for element_region. */
1928 element_region::get_relative_symbolic_offset (region_model_manager
*mgr
) const
1930 tree elem_type
= get_type ();
1932 /* First, use int_size_in_bytes, to reject the case where we
1933 have an incomplete type, or a non-constant value. */
1934 HOST_WIDE_INT hwi_byte_size
= int_size_in_bytes (elem_type
);
1935 if (hwi_byte_size
> 0)
1937 tree byte_size_tree
= wide_int_to_tree (ptrdiff_type_node
,
1939 const svalue
*byte_size_sval
1940 = mgr
->get_or_create_constant_svalue (byte_size_tree
);
1941 return mgr
->get_or_create_binop (NULL_TREE
, MULT_EXPR
,
1942 m_index
, byte_size_sval
);
1944 return mgr
->get_or_create_unknown_svalue (ptrdiff_type_node
);
1947 /* class offset_region : public region. */
1949 /* Implementation of region::accept vfunc for offset_region. */
1952 offset_region::accept (visitor
*v
) const
1955 m_byte_offset
->accept (v
);
1958 /* Implementation of region::dump_to_pp vfunc for offset_region. */
1961 offset_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
1965 //pp_string (pp, "(");
1966 get_parent_region ()->dump_to_pp (pp
, simple
);
1967 pp_string (pp
, "+");
1968 m_byte_offset
->dump_to_pp (pp
, simple
);
1969 //pp_string (pp, ")");
1973 pp_string (pp
, "offset_region(");
1974 get_parent_region ()->dump_to_pp (pp
, simple
);
1975 pp_string (pp
, ", ");
1976 print_quoted_type (pp
, get_type ());
1977 pp_string (pp
, ", ");
1978 m_byte_offset
->dump_to_pp (pp
, simple
);
1979 pp_printf (pp
, ")");
1984 offset_region::get_bit_offset (region_model_manager
*mgr
) const
1986 const svalue
*bits_per_byte_sval
1987 = mgr
->get_or_create_int_cst (NULL_TREE
, BITS_PER_UNIT
);
1988 return mgr
->get_or_create_binop (NULL_TREE
, MULT_EXPR
,
1989 m_byte_offset
, bits_per_byte_sval
);
1992 /* Implementation of region::get_relative_concrete_offset vfunc
1993 for offset_region. */
1996 offset_region::get_relative_concrete_offset (bit_offset_t
*out
) const
1998 if (tree byte_offset_cst
= m_byte_offset
->maybe_get_constant ())
2000 gcc_assert (TREE_CODE (byte_offset_cst
) == INTEGER_CST
);
2001 /* Use a signed value for the byte offset, to handle
2002 negative offsets. */
2003 HOST_WIDE_INT byte_offset
2004 = wi::to_offset (byte_offset_cst
).to_shwi ();
2005 HOST_WIDE_INT bit_offset
= byte_offset
* BITS_PER_UNIT
;
2012 /* Implementation of region::get_relative_symbolic_offset vfunc
2013 for offset_region. */
2016 offset_region::get_relative_symbolic_offset (region_model_manager
*mgr
2017 ATTRIBUTE_UNUSED
) const
2019 return get_byte_offset ();
2022 /* class sized_region : public region. */
2024 /* Implementation of region::accept vfunc for sized_region. */
2027 sized_region::accept (visitor
*v
) const
2030 m_byte_size_sval
->accept (v
);
2033 /* Implementation of region::dump_to_pp vfunc for sized_region. */
2036 sized_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
2040 pp_string (pp
, "SIZED_REG(");
2041 get_parent_region ()->dump_to_pp (pp
, simple
);
2042 pp_string (pp
, ", ");
2043 m_byte_size_sval
->dump_to_pp (pp
, simple
);
2044 pp_string (pp
, ")");
2048 pp_string (pp
, "sized_region(");
2049 get_parent_region ()->dump_to_pp (pp
, simple
);
2050 pp_string (pp
, ", ");
2051 m_byte_size_sval
->dump_to_pp (pp
, simple
);
2052 pp_printf (pp
, ")");
2056 /* Implementation of region::get_byte_size vfunc for sized_region. */
2059 sized_region::get_byte_size (byte_size_t
*out
) const
2061 if (tree cst
= m_byte_size_sval
->maybe_get_constant ())
2063 gcc_assert (TREE_CODE (cst
) == INTEGER_CST
);
2064 *out
= tree_to_uhwi (cst
);
2070 /* Implementation of region::get_bit_size vfunc for sized_region. */
2073 sized_region::get_bit_size (bit_size_t
*out
) const
2075 byte_size_t byte_size
;
2076 if (!get_byte_size (&byte_size
))
2078 *out
= byte_size
* BITS_PER_UNIT
;
2082 /* Implementation of region::get_bit_size_sval vfunc for sized_region. */
2085 sized_region::get_bit_size_sval (region_model_manager
*mgr
) const
2087 const svalue
*bits_per_byte_sval
2088 = mgr
->get_or_create_int_cst (NULL_TREE
, BITS_PER_UNIT
);
2089 return mgr
->get_or_create_binop (NULL_TREE
, MULT_EXPR
,
2090 m_byte_size_sval
, bits_per_byte_sval
);
2093 /* class cast_region : public region. */
2095 /* Implementation of region::accept vfunc for cast_region. */
2098 cast_region::accept (visitor
*v
) const
2101 m_original_region
->accept (v
);
2104 /* Implementation of region::dump_to_pp vfunc for cast_region. */
2107 cast_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
2111 pp_string (pp
, "CAST_REG(");
2112 print_quoted_type (pp
, get_type ());
2113 pp_string (pp
, ", ");
2114 m_original_region
->dump_to_pp (pp
, simple
);
2115 pp_string (pp
, ")");
2119 pp_string (pp
, "cast_region(");
2120 m_original_region
->dump_to_pp (pp
, simple
);
2121 pp_string (pp
, ", ");
2122 print_quoted_type (pp
, get_type ());
2123 pp_printf (pp
, ")");
2127 /* Implementation of region::get_relative_concrete_offset vfunc
2131 cast_region::get_relative_concrete_offset (bit_offset_t
*out
) const
2137 /* class heap_allocated_region : public region. */
2139 /* Implementation of region::dump_to_pp vfunc for heap_allocated_region. */
2142 heap_allocated_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
2145 pp_printf (pp
, "HEAP_ALLOCATED_REGION(%i)", get_id ());
2147 pp_printf (pp
, "heap_allocated_region(%i)", get_id ());
2150 /* class alloca_region : public region. */
2152 /* Implementation of region::dump_to_pp vfunc for alloca_region. */
2155 alloca_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
2158 pp_printf (pp
, "ALLOCA_REGION(%i)", get_id ());
2160 pp_printf (pp
, "alloca_region(%i)", get_id ());
2163 /* class string_region : public region. */
2165 /* Implementation of region::dump_to_pp vfunc for string_region. */
2168 string_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
2171 dump_tree (pp
, m_string_cst
);
2174 pp_string (pp
, "string_region(");
2175 dump_tree (pp
, m_string_cst
);
2176 if (!flag_dump_noaddr
)
2178 pp_string (pp
, " (");
2179 pp_pointer (pp
, m_string_cst
);
2180 pp_string (pp
, "))");
2185 /* class bit_range_region : public region. */
2187 /* Implementation of region::dump_to_pp vfunc for bit_range_region. */
2190 bit_range_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
2194 pp_string (pp
, "BIT_RANGE_REG(");
2195 get_parent_region ()->dump_to_pp (pp
, simple
);
2196 pp_string (pp
, ", ");
2197 m_bits
.dump_to_pp (pp
);
2198 pp_string (pp
, ")");
2202 pp_string (pp
, "bit_range_region(");
2203 get_parent_region ()->dump_to_pp (pp
, simple
);
2204 pp_string (pp
, ", ");
2205 m_bits
.dump_to_pp (pp
);
2206 pp_printf (pp
, ")");
2210 /* Implementation of region::get_byte_size vfunc for bit_range_region. */
2213 bit_range_region::get_byte_size (byte_size_t
*out
) const
2215 if (m_bits
.m_size_in_bits
% BITS_PER_UNIT
== 0)
2217 *out
= m_bits
.m_size_in_bits
/ BITS_PER_UNIT
;
2223 /* Implementation of region::get_bit_size vfunc for bit_range_region. */
2226 bit_range_region::get_bit_size (bit_size_t
*out
) const
2228 *out
= m_bits
.m_size_in_bits
;
2232 /* Implementation of region::get_byte_size_sval vfunc for bit_range_region. */
2235 bit_range_region::get_byte_size_sval (region_model_manager
*mgr
) const
2237 if (m_bits
.m_size_in_bits
% BITS_PER_UNIT
!= 0)
2238 return mgr
->get_or_create_unknown_svalue (size_type_node
);
2240 HOST_WIDE_INT num_bytes
= m_bits
.m_size_in_bits
.to_shwi () / BITS_PER_UNIT
;
2241 return mgr
->get_or_create_int_cst (size_type_node
, num_bytes
);
2244 /* Implementation of region::get_bit_size_sval vfunc for bit_range_region. */
2247 bit_range_region::get_bit_size_sval (region_model_manager
*mgr
) const
2249 return mgr
->get_or_create_int_cst (size_type_node
,
2250 m_bits
.m_size_in_bits
);
2253 /* Implementation of region::get_relative_concrete_offset vfunc for
2254 bit_range_region. */
2257 bit_range_region::get_relative_concrete_offset (bit_offset_t
*out
) const
2259 *out
= m_bits
.get_start_bit_offset ();
2263 /* Implementation of region::get_relative_symbolic_offset vfunc for
2265 The returned svalue is equal to the offset converted to bytes and
2269 bit_range_region::get_relative_symbolic_offset (region_model_manager
*mgr
)
2272 byte_offset_t start_byte
= m_bits
.get_start_bit_offset () / BITS_PER_UNIT
;
2273 tree start_bit_tree
= wide_int_to_tree (ptrdiff_type_node
, start_byte
);
2274 return mgr
->get_or_create_constant_svalue (start_bit_tree
);
2277 /* class var_arg_region : public region. */
2280 var_arg_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
2284 pp_string (pp
, "VAR_ARG_REG(");
2285 get_parent_region ()->dump_to_pp (pp
, simple
);
2286 pp_printf (pp
, ", arg_idx: %d)", m_idx
);
2290 pp_string (pp
, "var_arg_region(");
2291 get_parent_region ()->dump_to_pp (pp
, simple
);
2292 pp_printf (pp
, ", arg_idx: %d)", m_idx
);
2296 /* Get the frame_region for this var_arg_region. */
2298 const frame_region
*
2299 var_arg_region::get_frame_region () const
2301 gcc_assert (get_parent_region ());
2302 return as_a
<const frame_region
*> (get_parent_region ());
2305 /* class errno_region : public region. */
2308 errno_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
2311 pp_string (pp
, "errno_region");
2313 pp_string (pp
, "errno_region()");
2316 /* class private_region : public region. */
2319 private_region::dump_to_pp (pretty_printer
*pp
, bool simple
) const
2322 pp_printf (pp
, "PRIVATE_REG(%qs)", m_desc
);
2324 pp_printf (pp
, "private_region(%qs)", m_desc
);
2327 /* class unknown_region : public region. */
2329 /* Implementation of region::dump_to_pp vfunc for unknown_region. */
2332 unknown_region::dump_to_pp (pretty_printer
*pp
, bool /*simple*/) const
2334 pp_string (pp
, "UNKNOWN_REGION");
2339 #endif /* #if ENABLE_ANALYZER */