Move PREFERRED_DEBUGGING_TYPE define in pa64-hpux.h to pa.h
[official-gcc.git] / gcc / analyzer / region.cc
blobfa187fde3313286f888302d10061c71d75153b73
1 /* Regions of memory.
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)
10 any later version.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "diagnostic-core.h"
26 #include "gimple-pretty-print.h"
27 #include "function.h"
28 #include "basic-block.h"
29 #include "gimple.h"
30 #include "gimple-iterator.h"
31 #include "diagnostic-core.h"
32 #include "graphviz.h"
33 #include "options.h"
34 #include "cgraph.h"
35 #include "tree-dfa.h"
36 #include "stringpool.h"
37 #include "convert.h"
38 #include "target.h"
39 #include "fold-const.h"
40 #include "tree-pretty-print.h"
41 #include "diagnostic-color.h"
42 #include "diagnostic-metadata.h"
43 #include "tristate.h"
44 #include "bitmap.h"
45 #include "selftest.h"
46 #include "function.h"
47 #include "json.h"
48 #include "analyzer/analyzer.h"
49 #include "analyzer/analyzer-logging.h"
50 #include "ordered-hash-map.h"
51 #include "options.h"
52 #include "cgraph.h"
53 #include "cfg.h"
54 #include "digraph.h"
55 #include "analyzer/supergraph.h"
56 #include "sbitmap.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"
63 #if ENABLE_ANALYZER
65 namespace ana {
67 /* class region and its various subclasses. */
69 /* class region. */
71 region::~region ()
73 delete m_cached_offset;
76 /* Compare REG1 and REG2 by id. */
78 int
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. */
90 const region *
91 region::get_base_region () const
93 const region *iter = this;
94 while (iter)
96 switch (iter->get_kind ())
98 case RK_FIELD:
99 case RK_ELEMENT:
100 case RK_OFFSET:
101 case RK_SIZED:
102 iter = iter->get_parent_region ();
103 continue;
104 case RK_CAST:
105 iter = iter->dyn_cast_cast_region ()->get_original_region ();
106 continue;
107 default:
108 return iter;
111 return iter;
114 /* Return true if get_base_region() == this for this region. */
116 bool
117 region::base_region_p () const
119 switch (get_kind ())
121 /* Region kinds representing a descendent of a base region. */
122 case RK_FIELD:
123 case RK_ELEMENT:
124 case RK_OFFSET:
125 case RK_SIZED:
126 case RK_CAST:
127 return false;
129 default:
130 return true;
134 /* Return true if this region is ELDER or one of its descendents. */
136 bool
137 region::descendent_of_p (const region *elder) const
139 const region *iter = this;
140 while (iter)
142 if (iter == elder)
143 return true;
144 if (iter->get_kind () == RK_CAST)
145 iter = iter->dyn_cast_cast_region ()->get_original_region ();
146 else
147 iter = iter->get_parent_region ();
149 return false;
152 /* If this region is a frame_region, or a descendent of one, return it.
153 Otherwise return NULL. */
155 const frame_region *
156 region::maybe_get_frame_region () const
158 const region *iter = this;
159 while (iter)
161 if (const frame_region *frame_reg = iter->dyn_cast_frame_region ())
162 return frame_reg;
163 if (iter->get_kind () == RK_CAST)
164 iter = iter->dyn_cast_cast_region ()->get_original_region ();
165 else
166 iter = iter->get_parent_region ();
168 return NULL;
171 /* Get the memory space of this region. */
173 enum memory_space
174 region::get_memory_space () const
176 const region *iter = this;
177 while (iter)
179 switch (iter->get_kind ())
181 default:
182 break;
183 case RK_GLOBALS:
184 return MEMSPACE_GLOBALS;
185 case RK_CODE:
186 case RK_FUNCTION:
187 case RK_LABEL:
188 return MEMSPACE_CODE;
189 case RK_FRAME:
190 case RK_STACK:
191 case RK_ALLOCA:
192 return MEMSPACE_STACK;
193 case RK_HEAP:
194 case RK_HEAP_ALLOCATED:
195 return MEMSPACE_HEAP;
196 case RK_STRING:
197 return MEMSPACE_READONLY_DATA;
199 if (iter->get_kind () == RK_CAST)
200 iter = iter->dyn_cast_cast_region ()->get_original_region ();
201 else
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. */
212 bool
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 ();
219 switch (mem_space)
221 default:
222 gcc_unreachable ();
223 case MEMSPACE_UNKNOWN:
224 case MEMSPACE_CODE:
225 case MEMSPACE_GLOBALS:
226 case MEMSPACE_READONLY_DATA:
227 /* Such regions have initial_svalues. */
228 return true;
230 case MEMSPACE_HEAP:
231 /* Heap allocations are uninitialized by default. */
232 return false;
234 case MEMSPACE_STACK:
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))
241 default:
242 gcc_unreachable ();
244 case PARM_DECL:
245 /* Parameters have initial values. */
246 return true;
248 case VAR_DECL:
249 case RESULT_DECL:
250 /* Function locals don't have initial values. */
251 return false;
253 case SSA_NAME:
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)
261 return true;
262 else
263 return false;
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. */
270 return false;
274 /* If this region is a decl_region, return the decl.
275 Otherwise return NULL. */
277 tree
278 region::maybe_get_decl () const
280 if (const decl_region *decl_reg = dyn_cast_decl_region ())
281 return decl_reg->get_decl ();
282 return NULL_TREE;
285 /* Get the region_offset for this region (calculating it on the
286 first call and caching it internally). */
288 region_offset
289 region::get_offset () const
291 if(!m_cached_offset)
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
298 and return true.
299 Otherwise return false. */
301 bool
302 region::get_byte_size (byte_size_t *out) const
304 tree type = get_type ();
306 /* Bail out e.g. for heap-allocated regions. */
307 if (!type)
308 return false;
310 HOST_WIDE_INT bytes = int_size_in_bytes (type);
311 if (bytes == -1)
312 return false;
313 *out = bytes;
314 return true;
317 /* Base implementation of region::get_byte_size_sval vfunc. */
319 const svalue *
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. */
325 if (!type)
326 return mgr->get_or_create_unknown_svalue (size_type_node);
328 HOST_WIDE_INT bytes = int_size_in_bytes (type);
329 if (bytes == -1)
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. */
342 bool
343 int_size_in_bits (const_tree type, bit_size_t *out)
345 if (INTEGRAL_TYPE_P (type))
347 *out = TYPE_PRECISION (type);
348 return true;
351 tree sz = TYPE_SIZE (type);
352 if (sz && tree_fits_uhwi_p (sz))
354 *out = TREE_INT_CST_LOW (sz);
355 return true;
357 else
358 return false;
361 /* If the size of this region (in bits) is known statically, write it to *OUT
362 and return true.
363 Otherwise return false. */
365 bool
366 region::get_bit_size (bit_size_t *out) const
368 tree type = get_type ();
370 /* Bail out e.g. for heap-allocated regions. */
371 if (!type)
372 return false;
374 return int_size_in_bits (type, out);
377 /* Get the field within RECORD_TYPE at BIT_OFFSET. */
379 tree
380 get_field_at_bit_offset (tree record_type, bit_offset_t bit_offset)
382 gcc_assert (TREE_CODE (record_type) == RECORD_TYPE);
383 if (bit_offset < 0)
384 return NULL;
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)
397 return last_field;
398 last_field = iter;
401 return last_field;
404 /* Populate *OUT with descendent regions of type TYPE that match
405 RELATIVE_BIT_OFFSET and SIZE_IN_BITS within this region. */
407 void
408 region::get_subregions_for_binding (region_model_manager *mgr,
409 bit_offset_t relative_bit_offset,
410 bit_size_t size_in_bits,
411 tree type,
412 auto_vec <const region *> *out) const
414 if (get_type () == NULL_TREE || type == NULL_TREE)
415 return;
416 if (relative_bit_offset == 0
417 && types_compatible_p (get_type (), type))
419 out->safe_push (this);
420 return;
422 switch (TREE_CODE (get_type ()))
424 case ARRAY_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
439 (this, element_type,
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);
445 break;
446 case RECORD_TYPE:
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,
451 and repeat. */
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);
462 break;
463 case UNION_TYPE:
465 for (tree field = TYPE_FIELDS (get_type ()); field != NULL_TREE;
466 field = DECL_CHAIN (field))
468 if (TREE_CODE (field) != FIELD_DECL)
469 continue;
470 const region *subregion = mgr->get_field_region (this, field);
471 subregion->get_subregions_for_binding (mgr,
472 relative_bit_offset,
473 size_in_bits,
474 type,
475 out);
478 break;
479 default:
480 /* Do nothing. */
481 break;
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. */
489 region_offset
490 region::calc_offset () const
492 const region *iter_region = this;
493 bit_offset_t accum_bit_offset = 0;
495 while (iter_region)
497 switch (iter_region->get_kind ())
499 case RK_FIELD:
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;
510 continue;
512 case RK_ELEMENT:
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;
523 continue;
525 case RK_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;
536 continue;
538 case RK_SIZED:
539 iter_region = iter_region->get_parent_region ();
540 continue;
542 case RK_CAST:
544 const cast_region *cast_reg
545 = as_a <const cast_region *> (iter_region);
546 iter_region = cast_reg->get_original_region ();
548 continue;
550 default:
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. */
559 bool
560 region::get_relative_concrete_offset (bit_offset_t *) const
562 return false;
565 /* Copy from SRC_REG to DST_REG, using CTXT for any issues that occur. */
567 void
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)
574 return;
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. */
582 DEBUG_FUNCTION void
583 region::dump (bool simple) const
585 pretty_printer pp;
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);
590 pp_newline (&pp);
591 pp_flush (&pp);
594 /* Return a new json::string describing the region. */
596 json::value *
597 region::to_json () const
599 label_text desc = get_desc (true);
600 json::value *reg_js = new json::string (desc.m_buffer);
601 desc.maybe_free ();
602 return reg_js;
605 /* Generate a description of this region. */
607 DEBUG_FUNCTION label_text
608 region::get_desc (bool simple) const
610 pretty_printer pp;
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. */
619 void
620 region::accept (visitor *v) const
622 v->visit_region (this);
623 if (m_parent)
624 m_parent->accept (v);
627 /* Return true if this is a symbolic region for deferencing an
628 unknown ptr.
629 We shouldn't attempt to bind values for this region (but
630 can unbind values for other regions). */
632 bool
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)
637 return true;
638 return false;
641 /* region's ctor. */
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. */
674 bool
675 region::non_null_p () const
677 switch (get_kind ())
679 default:
680 return true;
681 case RK_SYMBOLIC:
682 /* Are we within a symbolic_region? If so, it could be NULL, and we
683 have to fall back on the constraints. */
684 return false;
685 case RK_HEAP_ALLOCATED:
686 return false;
690 /* Return true iff this region is defined in terms of SVAL. */
692 bool
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))
698 return true;
701 return false;
704 /* Comparator for trees to impose a deterministic ordering on
705 T1 and T2. */
707 static int
708 tree_cmp (const_tree t1, const_tree t2)
710 gcc_assert (t1);
711 gcc_assert (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. */
719 if (DECL_P (t1))
721 if (DECL_NAME (t1) && DECL_NAME (t2))
722 return strcmp (IDENTIFIER_POINTER (DECL_NAME (t1)),
723 IDENTIFIER_POINTER (DECL_NAME (t2)));
724 else
726 if (DECL_NAME (t1))
727 return -1;
728 else if (DECL_NAME (t2))
729 return 1;
730 else
731 return DECL_UID (t1) - DECL_UID (t2);
735 switch (TREE_CODE (t1))
737 case SSA_NAME:
739 if (SSA_NAME_VAR (t1) && SSA_NAME_VAR (t2))
741 int var_cmp = tree_cmp (SSA_NAME_VAR (t1), SSA_NAME_VAR (t2));
742 if (var_cmp)
743 return var_cmp;
744 return SSA_NAME_VERSION (t1) - SSA_NAME_VERSION (t2);
746 else
748 if (SSA_NAME_VAR (t1))
749 return -1;
750 else if (SSA_NAME_VAR (t2))
751 return 1;
752 else
753 return SSA_NAME_VERSION (t1) - SSA_NAME_VERSION (t2);
756 break;
758 case INTEGER_CST:
759 return tree_int_cst_compare (t1, t2);
761 case REAL_CST:
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
768 and to non-NaNs. */
769 if (int cmp_isnan = real_isnan (rv1) - real_isnan (rv2))
770 return cmp_isnan;
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))
777 return -1;
778 if (real_compare (GT_EXPR, rv1, rv2))
779 return 1;
780 return 0;
783 case STRING_CST:
784 return strcmp (TREE_STRING_POINTER (t1),
785 TREE_STRING_POINTER (t2));
787 default:
788 gcc_unreachable ();
789 break;
792 gcc_unreachable ();
794 return 0;
797 /* qsort comparator for trees to impose a deterministic ordering on
798 P1 and P2. */
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 ();
815 ++iter)
816 delete (*iter).second;
819 void
820 frame_region::accept (visitor *v) const
822 region::accept (v);
823 if (m_calling_frame)
824 m_calling_frame->accept (v);
827 /* Implementation of region::dump_to_pp vfunc for frame_region. */
829 void
830 frame_region::dump_to_pp (pretty_printer *pp, bool simple) const
832 if (simple)
833 pp_printf (pp, "frame: %qs@%i", function_name (m_fun), get_stack_depth ());
834 else
835 pp_printf (pp, "frame_region(%qs, index: %i, depth: %i)",
836 function_name (m_fun), m_index, get_stack_depth ());
839 const decl_region *
840 frame_region::get_region_for_local (region_model_manager *mgr,
841 tree expr) const
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))
853 return *slot;
854 decl_region *reg
855 = new decl_region (mgr->alloc_region_id (), this, expr);
856 mutable_locals.put (expr, reg);
857 return reg;
860 /* class globals_region : public space_region. */
862 /* Implementation of region::dump_to_pp vfunc for globals_region. */
864 void
865 globals_region::dump_to_pp (pretty_printer *pp, bool simple) const
867 if (simple)
868 pp_string (pp, "::");
869 else
870 pp_string (pp, "globals");
873 /* class code_region : public map_region. */
875 /* Implementation of region::dump_to_pp vfunc for code_region. */
877 void
878 code_region::dump_to_pp (pretty_printer *pp, bool simple) const
880 if (simple)
881 pp_string (pp, "code region");
882 else
883 pp_string (pp, "code_region()");
886 /* class function_region : public region. */
888 /* Implementation of region::dump_to_pp vfunc for function_region. */
890 void
891 function_region::dump_to_pp (pretty_printer *pp, bool simple) const
893 if (simple)
895 dump_quoted_tree (pp, m_fndecl);
897 else
899 pp_string (pp, "function_region(");
900 dump_quoted_tree (pp, m_fndecl);
901 pp_string (pp, ")");
905 /* class label_region : public region. */
907 /* Implementation of region::dump_to_pp vfunc for label_region. */
909 void
910 label_region::dump_to_pp (pretty_printer *pp, bool simple) const
912 if (simple)
914 dump_quoted_tree (pp, m_label);
916 else
918 pp_string (pp, "label_region(");
919 dump_quoted_tree (pp, m_label);
920 pp_string (pp, ")");
924 /* class stack_region : public region. */
926 /* Implementation of region::dump_to_pp vfunc for stack_region. */
928 void
929 stack_region::dump_to_pp (pretty_printer *pp, bool simple) const
931 if (simple)
932 pp_string (pp, "stack region");
933 else
934 pp_string (pp, "stack_region()");
937 /* class heap_region : public region. */
939 /* Implementation of region::dump_to_pp vfunc for heap_region. */
941 void
942 heap_region::dump_to_pp (pretty_printer *pp, bool simple) const
944 if (simple)
945 pp_string (pp, "heap region");
946 else
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. */
961 void
962 root_region::dump_to_pp (pretty_printer *pp, bool simple) const
964 if (simple)
965 pp_string (pp, "root region");
966 else
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. */
984 void
985 symbolic_region::accept (visitor *v) const
987 region::accept (v);
988 m_sval_ptr->accept (v);
991 /* Implementation of region::dump_to_pp vfunc for symbolic_region. */
993 void
994 symbolic_region::dump_to_pp (pretty_printer *pp, bool simple) const
996 if (simple)
998 pp_string (pp, "(*");
999 m_sval_ptr->dump_to_pp (pp, simple);
1000 pp_string (pp, ")");
1002 else
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. */
1018 void
1019 decl_region::dump_to_pp (pretty_printer *pp, bool simple) const
1021 if (simple)
1022 pp_printf (pp, "%E", m_decl);
1023 else
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
1034 for a global. */
1037 decl_region::get_stack_depth () const
1039 if (get_parent_region () == NULL)
1040 return 0;
1041 if (const frame_region *frame_reg
1042 = get_parent_region ()->dyn_cast_frame_region ())
1043 return frame_reg->get_stack_depth ();
1044 return 0;
1047 /* If the underlying decl is in the global constant pool,
1048 return an svalue representing the constant value.
1049 Otherwise return NULL. */
1051 const svalue *
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);
1059 return NULL;
1062 /* Get an svalue for CTOR, a CONSTRUCTOR for this region's decl. */
1064 const svalue *
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. */
1073 binding_map map;
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
1085 zero.
1087 Return NULL if there is a problem. */
1089 const svalue *
1090 decl_region::get_svalue_for_initializer (region_model_manager *mgr) const
1092 tree init = DECL_INITIAL (m_decl);
1093 if (!init)
1095 /* If we have an "extern" decl then there may be an initializer in
1096 another TU. */
1097 if (DECL_EXTERNAL (m_decl))
1098 return NULL;
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 ())
1107 return NULL;
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),
1112 c.get_map ());
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)
1118 return NULL;
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. */
1132 void
1133 field_region::dump_to_pp (pretty_printer *pp, bool simple) const
1135 if (simple)
1137 get_parent_region ()->dump_to_pp (pp, simple);
1138 pp_string (pp, ".");
1139 pp_printf (pp, "%E", m_field);
1141 else
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. */
1154 bool
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)
1161 return false;
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));
1167 *out = bitoffset;
1168 return true;
1171 /* class element_region : public region. */
1173 /* Implementation of region::accept vfunc for element_region. */
1175 void
1176 element_region::accept (visitor *v) const
1178 region::accept (v);
1179 m_index->accept (v);
1182 /* Implementation of region::dump_to_pp vfunc for element_region. */
1184 void
1185 element_region::dump_to_pp (pretty_printer *pp, bool simple) const
1187 if (simple)
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, ")");
1196 else
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. */
1211 bool
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;
1231 return true;
1234 return false;
1237 /* class offset_region : public region. */
1239 /* Implementation of region::accept vfunc for offset_region. */
1241 void
1242 offset_region::accept (visitor *v) const
1244 region::accept (v);
1245 m_byte_offset->accept (v);
1248 /* Implementation of region::dump_to_pp vfunc for offset_region. */
1250 void
1251 offset_region::dump_to_pp (pretty_printer *pp, bool simple) const
1253 if (simple)
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, ")");
1261 else
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. */
1276 bool
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;
1287 *out = bit_offset;
1288 return true;
1290 return false;
1293 /* class sized_region : public region. */
1295 /* Implementation of region::accept vfunc for sized_region. */
1297 void
1298 sized_region::accept (visitor *v) const
1300 region::accept (v);
1301 m_byte_size_sval->accept (v);
1304 /* Implementation of region::dump_to_pp vfunc for sized_region. */
1306 void
1307 sized_region::dump_to_pp (pretty_printer *pp, bool simple) const
1309 if (simple)
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, ")");
1317 else
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. */
1329 bool
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);
1336 return true;
1338 return false;
1341 /* Implementation of region::get_bit_size vfunc for sized_region. */
1343 bool
1344 sized_region::get_bit_size (bit_size_t *out) const
1346 byte_size_t byte_size;
1347 if (!get_byte_size (&byte_size))
1348 return false;
1349 *out = byte_size * BITS_PER_UNIT;
1350 return true;
1353 /* class cast_region : public region. */
1355 /* Implementation of region::accept vfunc for cast_region. */
1357 void
1358 cast_region::accept (visitor *v) const
1360 region::accept (v);
1361 m_original_region->accept (v);
1364 /* Implementation of region::dump_to_pp vfunc for cast_region. */
1366 void
1367 cast_region::dump_to_pp (pretty_printer *pp, bool simple) const
1369 if (simple)
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, ")");
1377 else
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. */
1391 void
1392 heap_allocated_region::dump_to_pp (pretty_printer *pp, bool simple) const
1394 if (simple)
1395 pp_printf (pp, "HEAP_ALLOCATED_REGION(%i)", get_id ());
1396 else
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. */
1404 void
1405 alloca_region::dump_to_pp (pretty_printer *pp, bool simple) const
1407 if (simple)
1408 pp_string (pp, "ALLOCA_REGION");
1409 else
1410 pp_string (pp, "alloca_region()");
1413 /* class string_region : public region. */
1415 /* Implementation of region::dump_to_pp vfunc for string_region. */
1417 void
1418 string_region::dump_to_pp (pretty_printer *pp, bool simple) const
1420 if (simple)
1421 dump_tree (pp, m_string_cst);
1422 else
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. */
1439 void
1440 unknown_region::dump_to_pp (pretty_printer *pp, bool /*simple*/) const
1442 pp_string (pp, "UNKNOWN_REGION");
1445 } // namespace ana
1447 #endif /* #if ENABLE_ANALYZER */