LCOV - code coverage report
Current view: top level - gcc/analyzer - region-model-manager.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 91.5 % 1049 960
Test Date: 2026-12-05 16:10:02 Functions: 92.0 % 113 104
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Consolidation of svalues and regions.
       2              :    Copyright (C) 2020-2026 Free Software Foundation, Inc.
       3              :    Contributed by David Malcolm <dmalcolm@redhat.com>.
       4              : 
       5              : This file is part of GCC.
       6              : 
       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.
      11              : 
      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.
      16              : 
      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/>.  */
      20              : 
      21              : #include "analyzer/common.h"
      22              : 
      23              : #include "fold-const.h"
      24              : #include "ordered-hash-map.h"
      25              : #include "options.h"
      26              : #include "analyzer/supergraph.h"
      27              : #include "sbitmap.h"
      28              : #include "target.h"
      29              : 
      30              : #include "analyzer/analyzer-logging.h"
      31              : #include "analyzer/call-string.h"
      32              : #include "analyzer/program-point.h"
      33              : #include "analyzer/store.h"
      34              : #include "analyzer/region-model.h"
      35              : #include "analyzer/constraint-manager.h"
      36              : #include "analyzer/symbolic-scalars.h"
      37              : #include "analyzer/record-layout.h"
      38              : #include "analyzer/analyzer-selftests.h"
      39              : 
      40              : #if ENABLE_ANALYZER
      41              : 
      42              : namespace ana {
      43              : 
      44              : /* class region_model_manager.  */
      45              : 
      46              : /* region_model_manager's ctor.  */
      47              : 
      48         4495 : region_model_manager::region_model_manager (symbolic::node_manager &node_mgr,
      49              :                                             logger *logger)
      50         4495 : : m_node_mgr (node_mgr),
      51         4495 :   m_logger (logger),
      52         4495 :   m_empty_call_string (),
      53         4495 :   m_root_region (alloc_node_id ()),
      54         4495 :   m_stack_region (alloc_node_id (), &m_root_region, memory_space::stack),
      55         4495 :   m_heap_region (alloc_node_id (), &m_root_region, memory_space::heap),
      56         4495 :   m_read_only_region (alloc_node_id (), &m_root_region, memory_space::read_only_data),
      57         4495 :   m_private_region (alloc_node_id (), &m_root_region, memory_space::private_),
      58         4495 :   m_unknown_NULL (nullptr),
      59         4495 :   m_checking_feasibility (false),
      60         4495 :   m_max_complexity (0, 0),
      61         4495 :   m_code_region (alloc_node_id (), &m_root_region, memory_space::code),
      62         4495 :   m_fndecls_map (), m_labels_map (),
      63         4495 :   m_globals_region (alloc_node_id (), &m_root_region, memory_space::globals),
      64         4495 :   m_globals_map (),
      65         4495 :   m_thread_local_region (alloc_node_id (), &m_root_region,
      66              :                          memory_space::thread_local_),
      67         4495 :   m_errno_region (alloc_node_id (), m_thread_local_region),
      68         4495 :   m_range_mgr (new bounded_ranges_manager ()),
      69        13485 :   m_known_fn_mgr (logger)
      70              : {
      71         4495 : }
      72              : 
      73              : /* region_model_manager's dtor.  Delete all of the managed svalues
      74              :    and regions.  */
      75              : 
      76         8990 : region_model_manager::~region_model_manager ()
      77              : {
      78              :   /* Delete consolidated svalues.  */
      79      1190688 :   for (constants_map_t::iterator iter = m_constants_map.begin ();
      80      1190688 :        iter != m_constants_map.end (); ++iter)
      81      1186193 :     delete (*iter).second;
      82        11847 :   for (unknowns_map_t::iterator iter = m_unknowns_map.begin ();
      83        11847 :        iter != m_unknowns_map.end (); ++iter)
      84         7352 :     delete (*iter).second;
      85         4495 :   delete m_unknown_NULL;
      86         4496 :   for (poisoned_values_map_t::iterator iter = m_poisoned_values_map.begin ();
      87         4496 :        iter != m_poisoned_values_map.end (); ++iter)
      88            1 :     delete (*iter).second;
      89         4529 :   for (setjmp_values_map_t::iterator iter = m_setjmp_values_map.begin ();
      90         4529 :        iter != m_setjmp_values_map.end (); ++iter)
      91           34 :     delete (*iter).second;
      92        26895 :   for (initial_values_map_t::iterator iter = m_initial_values_map.begin ();
      93        26895 :        iter != m_initial_values_map.end (); ++iter)
      94        22400 :     delete (*iter).second;
      95        48648 :   for (pointer_values_map_t::iterator iter = m_pointer_values_map.begin ();
      96        48648 :        iter != m_pointer_values_map.end (); ++iter)
      97        44153 :     delete (*iter).second;
      98        13650 :   for (unaryop_values_map_t::iterator iter = m_unaryop_values_map.begin ();
      99        13650 :        iter != m_unaryop_values_map.end (); ++iter)
     100         9155 :     delete (*iter).second;
     101        18504 :   for (binop_values_map_t::iterator iter = m_binop_values_map.begin ();
     102        18504 :        iter != m_binop_values_map.end (); ++iter)
     103        14009 :     delete (*iter).second;
     104        43852 :   for (auto iter : m_bit_vector_to_svalue_map)
     105        39357 :     delete iter.second;
     106          305 :   for (unmergeable_values_map_t::iterator iter
     107         4495 :          = m_unmergeable_values_map.begin ();
     108         4800 :        iter != m_unmergeable_values_map.end (); ++iter)
     109          305 :     delete (*iter).second;
     110         6188 :   for (widening_values_map_t::iterator iter = m_widening_values_map.begin ();
     111         6188 :        iter != m_widening_values_map.end (); ++iter)
     112         1693 :     delete (*iter).second;
     113         4662 :   for (auto iter : m_asm_output_values_map)
     114          167 :     delete iter.second;
     115         4627 :   for (auto iter : m_const_fn_result_values_map)
     116          132 :     delete iter.second;
     117              : 
     118              :   /* Delete consolidated regions.  */
     119        15206 :   for (fndecls_map_t::iterator iter = m_fndecls_map.begin ();
     120        15206 :        iter != m_fndecls_map.end (); ++iter)
     121        10711 :     delete (*iter).second;
     122         4546 :   for (labels_map_t::iterator iter = m_labels_map.begin ();
     123         4546 :        iter != m_labels_map.end (); ++iter)
     124           51 :     delete (*iter).second;
     125        11217 :   for (globals_map_t::iterator iter = m_globals_map.begin ();
     126        11217 :        iter != m_globals_map.end (); ++iter)
     127         6722 :     delete (*iter).second;
     128         8425 :   for (string_map_t::iterator iter = m_string_map.begin ();
     129         8425 :        iter != m_string_map.end (); ++iter)
     130         3930 :     delete (*iter).second;
     131              : 
     132         4495 :   delete m_range_mgr;
     133         4495 : }
     134              : 
     135              : /* Return true if C exceeds the complexity limit for svalues.  */
     136              : 
     137              : bool
     138      1318874 : region_model_manager::too_complex_p (const complexity &c) const
     139              : {
     140      1318874 :   if (c.m_max_depth > (unsigned)param_analyzer_max_svalue_depth)
     141         5719 :     return true;
     142              :   return false;
     143              : }
     144              : 
     145              : /* If SVAL exceeds the complexity limit for svalues, delete it
     146              :    and return true.
     147              :    Otherwise update m_max_complexity and return false.  */
     148              : 
     149              : bool
     150      1323318 : region_model_manager::reject_if_too_complex (svalue *sval)
     151              : {
     152      1323318 :   if (m_checking_feasibility)
     153              :     return false;
     154              : 
     155      1318874 :   const complexity &c = sval->get_complexity ();
     156      1318874 :   if (!too_complex_p (c))
     157              :     {
     158      1313155 :       if (m_max_complexity.m_num_nodes < c.m_num_nodes)
     159        15593 :         m_max_complexity.m_num_nodes = c.m_num_nodes;
     160      1313155 :       if (m_max_complexity.m_max_depth < c.m_max_depth)
     161        13920 :         m_max_complexity.m_max_depth = c.m_max_depth;
     162              :       return false;
     163              :     }
     164              : 
     165         5719 :   pretty_printer pp;
     166         5719 :   pp_format_decoder (&pp) = default_tree_printer;
     167         5719 :   sval->dump_to_pp (&pp, true);
     168         5719 :   if (warning_at (input_location, OPT_Wanalyzer_symbol_too_complex,
     169              :                   "symbol too complicated: %qs",
     170              :                   pp_formatted_text (&pp)))
     171            2 :     inform (input_location,
     172              :             "max_depth %i exceeds --param=analyzer-max-svalue-depth=%i",
     173            2 :             c.m_max_depth, param_analyzer_max_svalue_depth);
     174              : 
     175         5719 :   delete sval;
     176         5719 :   return true;
     177         5719 : }
     178              : 
     179              : /* Macro for imposing a complexity limit on svalues, for use within
     180              :    region_model_manager member functions.
     181              : 
     182              :    If SVAL exceeds the complexity limit, delete it and return an UNKNOWN
     183              :    value of the same type.
     184              :    Otherwise update m_max_complexity and carry on.  */
     185              : 
     186              : #define RETURN_UNKNOWN_IF_TOO_COMPLEX(SVAL)                     \
     187              :   do {                                                          \
     188              :     svalue *sval_ = (SVAL);                                     \
     189              :     tree type_ = sval_->get_type ();                         \
     190              :     if (reject_if_too_complex (sval_))                          \
     191              :       return get_or_create_unknown_svalue (type_);              \
     192              :   } while (0)
     193              : 
     194              : /* svalue consolidation.  */
     195              : 
     196              : /* Return the svalue * for a constant_svalue for CST_EXPR,
     197              :    creating it if necessary.
     198              :    The constant_svalue instances are reused, based on pointer equality
     199              :    of trees  */
     200              : 
     201              : const svalue *
     202      2670306 : region_model_manager::get_or_create_constant_svalue (tree type, tree cst_expr)
     203              : {
     204      2670306 :   gcc_assert (cst_expr);
     205      2670306 :   gcc_assert (CONSTANT_CLASS_P (cst_expr));
     206      2670306 :   gcc_assert (type == TREE_TYPE (cst_expr) || type == NULL_TREE);
     207              : 
     208      2670306 :   const constant_svalue::key_t key (type, cst_expr);
     209      2670306 :   auto iter = m_constants_map.find (key);
     210      2670306 :   if (iter != m_constants_map.end ())
     211      1479196 :     return iter->second;
     212      1191110 :   constant_svalue *cst_sval
     213      1191110 :     = new constant_svalue (alloc_node_id (), type, cst_expr);
     214      1191110 :   RETURN_UNKNOWN_IF_TOO_COMPLEX (cst_sval);
     215      1186193 :   m_constants_map.insert ({key, cst_sval});
     216      1186193 :   return cst_sval;
     217              : }
     218              : 
     219              : const svalue *
     220      1431505 : region_model_manager::get_or_create_constant_svalue (tree cst_expr)
     221              : {
     222      1431505 :   tree type = TREE_TYPE (cst_expr);
     223      1431505 :   if (TREE_CODE (cst_expr) == RAW_DATA_CST)
     224              :     /* The type of a RAW_DATA_CST is the type of each element, rather than
     225              :        that of the constant as a whole, so use NULL_TREE for simplicity.  */
     226           22 :     type = NULL_TREE;
     227      1431505 :   return get_or_create_constant_svalue (type, cst_expr);
     228              : }
     229              : 
     230              : /* Return the svalue * for a constant_svalue for the INTEGER_CST
     231              :    for VAL of type TYPE, creating it if necessary.  */
     232              : 
     233              : const svalue *
     234      1238801 : region_model_manager::get_or_create_int_cst (tree type,
     235              :                                              const poly_wide_int_ref &cst)
     236              : {
     237      1238801 :   tree effective_type = type;
     238      1238801 :   if (!type)
     239            0 :     effective_type = ptrdiff_type_node;
     240      1238801 :   gcc_assert (INTEGRAL_TYPE_P (effective_type)
     241              :               || POINTER_TYPE_P (effective_type));
     242      1238801 :   tree tree_cst = wide_int_to_tree (effective_type, cst);
     243      1238801 :   return get_or_create_constant_svalue (type, tree_cst);
     244              : }
     245              : 
     246              : /* Return the svalue * for the constant_svalue for the NULL pointer
     247              :    of POINTER_TYPE, creating it if necessary.  */
     248              : 
     249              : const svalue *
     250         1955 : region_model_manager::get_or_create_null_ptr (tree pointer_type)
     251              : {
     252         1955 :   gcc_assert (pointer_type);
     253         1955 :   gcc_assert (POINTER_TYPE_P (pointer_type));
     254         1955 :   return get_or_create_int_cst (pointer_type, 0);
     255              : }
     256              : 
     257              : /* Return the svalue * for a unknown_svalue for TYPE (which can be NULL_TREE),
     258              :    creating it if necessary.
     259              :    The unknown_svalue instances are reused, based on pointer equality
     260              :    of the types  */
     261              : 
     262              : const svalue *
     263       393657 : region_model_manager::get_or_create_unknown_svalue (tree type)
     264              : {
     265              :   /* Don't create unknown values when doing feasibility testing;
     266              :      instead, create a unique svalue.  */
     267       393657 :   if (m_checking_feasibility)
     268         5595 :     return create_unique_svalue (type);
     269              : 
     270              :   /* Special-case NULL, so that the hash_map can use NULL as the
     271              :      "empty" value.  */
     272       388062 :   if (type == NULL_TREE)
     273              :     {
     274        11040 :       if (!m_unknown_NULL)
     275          974 :         m_unknown_NULL = new unknown_svalue (alloc_node_id (), type);
     276        11040 :       return m_unknown_NULL;
     277              :     }
     278              : 
     279       377022 :   auto iter = m_unknowns_map.find (type);
     280       377022 :   if (iter != m_unknowns_map.end ())
     281       369670 :     return iter->second;
     282         7352 :   unknown_svalue *sval = new unknown_svalue (alloc_node_id (), type);
     283         7352 :   m_unknowns_map.insert ({type, sval});
     284         7352 :   return sval;
     285              : }
     286              : 
     287              : /* Return a freshly-allocated svalue of TYPE, owned by this manager.  */
     288              : 
     289              : const svalue *
     290         5595 : region_model_manager::create_unique_svalue (tree type)
     291              : {
     292         5595 :   svalue *sval = new placeholder_svalue (alloc_node_id (), type, "unique");
     293         5595 :   m_managed_dynamic_svalues.safe_push (sval);
     294         5595 :   return sval;
     295              : }
     296              : 
     297              : /* Return the svalue * for the initial value of REG, creating it if
     298              :    necessary.  */
     299              : 
     300              : const svalue *
     301        34926 : region_model_manager::get_or_create_initial_value (const region *reg,
     302              :                                                    bool check_poisoned)
     303              : {
     304        34926 :   if (!reg->can_have_initial_svalue_p () && check_poisoned)
     305            6 :     return get_or_create_poisoned_svalue (poison_kind::uninit,
     306            6 :                                           reg->get_type ());
     307              : 
     308              :   /* The initial value of a cast is a cast of the initial value.  */
     309        34920 :   if (const cast_region *cast_reg = reg->dyn_cast_cast_region ())
     310              :     {
     311            0 :       const region *original_reg = cast_reg->get_parent_region ();
     312            0 :       return get_or_create_cast (cast_reg->get_type (),
     313            0 :                                  get_or_create_initial_value (original_reg));
     314              :     }
     315              : 
     316              :   /* Simplify:
     317              :        INIT_VAL(ELEMENT_REG(STRING_REG), CONSTANT_SVAL)
     318              :      to:
     319              :        CONSTANT_SVAL(STRING[N]).  */
     320        34920 :   if (const element_region *element_reg = reg->dyn_cast_element_region ())
     321            0 :     if (tree cst_idx = element_reg->get_index ()->maybe_get_constant ())
     322            0 :       if (const string_region *string_reg
     323            0 :           = element_reg->get_parent_region ()->dyn_cast_string_region ())
     324            0 :         if (tree_fits_shwi_p (cst_idx))
     325              :           {
     326            0 :             HOST_WIDE_INT idx = tree_to_shwi (cst_idx);
     327            0 :             tree string_cst = string_reg->get_string_cst ();
     328            0 :             if (idx >= 0 && idx <= TREE_STRING_LENGTH (string_cst))
     329              :               {
     330            0 :                 int ch = TREE_STRING_POINTER (string_cst)[idx];
     331            0 :                 return get_or_create_int_cst (reg->get_type (), ch);
     332              :               }
     333              :           }
     334              : 
     335              :   /* INIT_VAL (*UNKNOWN_PTR) -> UNKNOWN_VAL.  */
     336        34920 :   if (reg->symbolic_for_unknown_ptr_p ())
     337            0 :     return get_or_create_unknown_svalue (reg->get_type ());
     338              : 
     339        34920 :   auto mem_range = reg->get_memory_range (*this);
     340        34920 :   if (&mem_range.get_base_region () != reg)
     341              :     {
     342              :       // Express as a cropping:
     343            0 :       auto &bc_mgr = get_node_manager ().get_bit_contents_manager ();;
     344            0 :       auto &baseline_bc
     345            0 :         = bc_mgr.get_baseline_contents (mem_range.get_base_region (),
     346              :                                         baseline_state::after_main);
     347            0 :       auto &init_bv = baseline_bc.get_bits (mem_range.get_bit_range ());
     348            0 :       return get_svalue_from_bit_vector (reg->get_type (), init_bv);
     349              :     }
     350              : 
     351        34920 :   auto iter = m_initial_values_map.find (reg);
     352        34920 :   if (iter != m_initial_values_map.end ())
     353        12513 :     return iter->second;
     354        22407 :   initial_svalue *initial_sval
     355        22407 :     = new initial_svalue (alloc_node_id (), reg->get_type (), reg);
     356        22407 :   RETURN_UNKNOWN_IF_TOO_COMPLEX (initial_sval);
     357        22400 :   m_initial_values_map.insert ({reg, initial_sval});
     358        22400 :   return initial_sval;
     359              : }
     360              : 
     361              : /* Return the svalue * for R using type TYPE, creating it if
     362              :    necessary.  */
     363              : 
     364              : const svalue *
     365           34 : region_model_manager::get_or_create_setjmp_svalue (const setjmp_record &r,
     366              :                                                    tree type)
     367              : {
     368           34 :   const setjmp_svalue::key_t key (r, type);
     369           34 :   auto iter = m_setjmp_values_map.find (key);
     370           34 :   if (iter != m_setjmp_values_map.end ())
     371            0 :     return iter->second;
     372           34 :   setjmp_svalue *setjmp_sval = new setjmp_svalue (r, alloc_node_id (), type);
     373           34 :   RETURN_UNKNOWN_IF_TOO_COMPLEX (setjmp_sval);
     374           34 :   m_setjmp_values_map.insert ({key, setjmp_sval});
     375           34 :   return setjmp_sval;
     376              : }
     377              : 
     378              : /* Return the svalue * for a poisoned value of KIND and TYPE, creating it if
     379              :    necessary.  */
     380              : 
     381              : const svalue *
     382            6 : region_model_manager::get_or_create_poisoned_svalue (enum poison_kind kind,
     383              :                                                      tree type)
     384              : {
     385            6 :   const poisoned_svalue::key_t key (kind, type);
     386            6 :   auto iter = m_poisoned_values_map.find (key);
     387            6 :   if (iter != m_poisoned_values_map.end ())
     388            5 :     return iter->second;
     389            1 :   poisoned_svalue *poisoned_sval
     390            1 :     = new poisoned_svalue (kind, alloc_node_id (), type);
     391            1 :   RETURN_UNKNOWN_IF_TOO_COMPLEX (poisoned_sval);
     392            1 :   m_poisoned_values_map.insert ( {key, poisoned_sval} );
     393            1 :   return poisoned_sval;
     394              : }
     395              : 
     396              : /* Return the svalue * for a pointer to POINTEE of type PTR_TYPE,
     397              :    creating it if necessary.  */
     398              : 
     399              : const svalue *
     400      1094053 : region_model_manager::get_ptr_svalue (tree ptr_type, const region *pointee)
     401              : {
     402              :   /* If this is a symbolic region from dereferencing a pointer, and the types
     403              :      match, then return the original pointer.  */
     404      1094053 :   if (const symbolic_region *sym_reg = pointee->dyn_cast_symbolic_region ())
     405       114640 :     if (ptr_type == sym_reg->get_pointer ()->get_type ())
     406              :       return sym_reg->get_pointer ();
     407              : 
     408      1093884 :   auto pointer = pointee->get_pointer (*this);
     409      1093884 :   return get_ptr_svalue (ptr_type, pointer);
     410              : }
     411              : 
     412              : const svalue *
     413      2899679 : region_model_manager::get_ptr_svalue (tree ptr_type, symbolic::pointer ptr)
     414              : {
     415              :   /* If this is a symbolic region from dereferencing a pointer, then
     416              :      return the original pointer.  */
     417      2899679 :   if (ptr.get_byte_offset ().is_zero_p ())
     418      2589145 :     if (auto *sym_reg = ptr.get_base_region ().dyn_cast_symbolic_region ())
     419       117409 :       return get_or_create_cast (ptr_type, sym_reg->get_pointer ());
     420              : 
     421      2782270 :   const pointer_svalue::key_t key (ptr_type, ptr);
     422      2782270 :   auto iter = m_pointer_values_map.find (key);
     423      2782270 :   if (iter != m_pointer_values_map.end ())
     424      2737681 :     return iter->second;
     425        44589 :   pointer_svalue *sval = new pointer_svalue (alloc_node_id (), key);
     426        44589 :   RETURN_UNKNOWN_IF_TOO_COMPLEX (sval);
     427        44153 :   m_pointer_values_map.insert ({key, sval});
     428        44153 :   return sval;
     429              : }
     430              : 
     431              : /* Subroutine of region_model_manager::maybe_fold_unaryop
     432              :    when the arg is a binop_svalue.
     433              :    Invert comparisons e.g. "!(x == y)" => "x != y".
     434              :    Otherwise, return nullptr.  */
     435              : 
     436              : const svalue *
     437           16 : region_model_manager::
     438              : maybe_invert_comparison_in_unaryop (tree result_type,
     439              :                                     const binop_svalue *binop)
     440              : {
     441           16 :   if (TREE_CODE_CLASS (binop->get_op ()) == tcc_comparison)
     442              :     {
     443           16 :       enum tree_code inv_op
     444           16 :         = invert_tree_comparison (binop->get_op (),
     445           16 :                                   HONOR_NANS (binop->get_type ()));
     446           16 :       if (inv_op != ERROR_MARK)
     447           16 :         return get_or_create_cast
     448           16 :           (result_type,
     449              :            get_or_create_binop (binop->get_type (), inv_op,
     450              :                                 binop->get_arg0 (),
     451           16 :                                 binop->get_arg1 ()));
     452              :     }
     453              :   return nullptr;
     454              : }
     455              : 
     456              : /* Subroutine of region_model_manager::get_or_create_unaryop.
     457              :    Attempt to fold the inputs and return a simpler svalue *.
     458              :    Otherwise, return nullptr.  */
     459              : 
     460              : const svalue *
     461       338466 : region_model_manager::maybe_fold_unaryop (tree type, enum tree_code op,
     462              :                                           const svalue *arg)
     463              : {
     464              :   /* Ops on "unknown" are also unknown.  */
     465       341155 :   if (arg->get_kind () == SK_UNKNOWN)
     466        15504 :     return get_or_create_unknown_svalue (type);
     467              :   /* Likewise for "poisoned".  */
     468       651302 :   else if (const poisoned_svalue *poisoned_sval
     469       325651 :              = arg->dyn_cast_poisoned_svalue ())
     470            0 :     return get_or_create_poisoned_svalue (poisoned_sval->get_poison_kind (),
     471            0 :                                           type);
     472              : 
     473       325651 :   gcc_assert (arg->can_have_associated_state_p ());
     474              : 
     475       325651 :   switch (op)
     476              :     {
     477              :     default: break;
     478       324558 :     case VIEW_CONVERT_EXPR:
     479       324558 :     case NOP_EXPR:
     480       324558 :       {
     481              :         /* Avoid creating symbolic regions for pointer casts by
     482              :            simplifying (T*)(&REGION) to ((T*)&REGION).  */
     483       324558 :         if (auto *sym_ptr = arg->maybe_get_symbolic_pointer ())
     484        11941 :           if (type == NULL_TREE || POINTER_TYPE_P (type))
     485        10639 :             return get_ptr_svalue (type, *sym_ptr);
     486              : 
     487       312617 :         if (!type)
     488              :           return nullptr;
     489              : 
     490              :         /* Handle redundant casts.  */
     491       199456 :         if (arg->get_type ()
     492       199456 :             && useless_type_conversion_p (arg->get_type (), type))
     493              :           return arg;
     494              : 
     495        69624 :         if (auto *sval_from_bv = arg->dyn_cast_svalue_from_bit_vector ())
     496              :           {
     497         6704 :             concrete_bit_size type_size;
     498         6704 :             if (get_type_size (type, &type_size))
     499              :               {
     500         6704 :                 concrete_bit_size bv_size;
     501         6704 :                 auto &bv = sval_from_bv->get_bit_vector ();
     502         6704 :                 if (bv.maybe_get_concrete_size_in_bits (&bv_size))
     503         6704 :                   if (type_size == bv_size)
     504         2416 :                     return get_svalue_from_bit_vector
     505         2416 :                       (type, sval_from_bv->get_bit_vector ());
     506         6704 :               }
     507         6704 :           }
     508              : 
     509              :         /* Fold "cast<TYPE> (cast <INNER_TYPE> (innermost_arg))
     510              :              => "cast<TYPE> (innermost_arg)",
     511              :            unless INNER_TYPE is narrower than TYPE.  */
     512        67208 :         if (const svalue *innermost_arg = arg->maybe_undo_cast ())
     513              :           {
     514         3762 :             if (tree inner_type = arg->get_type ())
     515         3762 :               if (TYPE_SIZE (type)
     516         3762 :                   && TYPE_SIZE (inner_type)
     517         7524 :                   && (fold_binary (LE_EXPR, boolean_type_node,
     518              :                                    TYPE_SIZE (type), TYPE_SIZE (inner_type))
     519         3762 :                       == boolean_true_node))
     520              :                 return maybe_fold_unaryop (type, op, innermost_arg);
     521              :           }
     522              : 
     523              :         /* Casting all zeroes should give all zeroes.  */
     524        64519 :         if (type
     525        64519 :             && arg->all_zeroes_p ()
     526         5061 :             && (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)))
     527         5045 :           return get_or_create_int_cst (type, 0);
     528              :       }
     529              :       break;
     530            8 :     case TRUTH_NOT_EXPR:
     531            8 :       {
     532              :         /* Invert comparisons e.g. "!(x == y)" => "x != y".  */
     533            8 :         if (const binop_svalue *binop = arg->dyn_cast_binop_svalue ())
     534           16 :           if (const svalue *folded
     535            8 :                 = maybe_invert_comparison_in_unaryop (type, binop))
     536              :             return folded;
     537              :       }
     538              :       break;
     539          225 :     case NEGATE_EXPR:
     540          225 :       {
     541              :         /* -(-(VAL)) is VAL, for integer types.  */
     542          225 :         if (const unaryop_svalue *unaryop = arg->dyn_cast_unaryop_svalue ())
     543            2 :           if (unaryop->get_op () == NEGATE_EXPR
     544            1 :               && type == unaryop->get_type ()
     545            1 :               && type
     546            3 :               && INTEGRAL_TYPE_P (type))
     547            1 :             return unaryop->get_arg ();
     548              :       }
     549              :       break;
     550          361 :     case BIT_NOT_EXPR:
     551          361 :       {
     552              :         /* Invert comparisons for e.g. "~(x == y)" => "x != y".  */
     553          361 :         if (type
     554          361 :             && TREE_CODE (type) == BOOLEAN_TYPE
     555          292 :             && arg->get_type ()
     556          653 :             && TREE_CODE (arg->get_type ()) == BOOLEAN_TYPE)
     557          292 :           if (const binop_svalue *binop = arg->dyn_cast_binop_svalue ())
     558           16 :             if (const svalue *folded
     559            8 :                 = maybe_invert_comparison_in_unaryop (type, binop))
     560              :               return folded;
     561              :       }
     562              :       break;
     563              :     }
     564              : 
     565              :   /* Constants.  */
     566        60550 :   if (type)
     567        60550 :     if (tree cst = arg->maybe_get_constant ())
     568        22710 :       if (tree result = fold_unary (op, type, cst))
     569              :         {
     570        22521 :           if (CONSTANT_CLASS_P (result))
     571        22521 :             return get_or_create_constant_svalue (result);
     572              : 
     573              :           /* fold_unary can return casts of constants; try to handle them.  */
     574            0 :           if (op != NOP_EXPR
     575            0 :               && TREE_CODE (result) == NOP_EXPR
     576            0 :               && CONSTANT_CLASS_P (TREE_OPERAND (result, 0)))
     577              :             {
     578            0 :               const svalue *inner_cst
     579            0 :                 = get_or_create_constant_svalue (TREE_OPERAND (result, 0));
     580            0 :               return get_or_create_cast (type,
     581            0 :                                          get_or_create_cast (TREE_TYPE (result),
     582            0 :                                                              inner_cst));
     583              :             }
     584              :         }
     585              : 
     586              :   return nullptr;
     587              : }
     588              : 
     589              : /* Return the svalue * for an unary operation OP on ARG with a result of
     590              :    type TYPE, creating it if necessary.  */
     591              : 
     592              : const svalue *
     593       338466 : region_model_manager::get_or_create_unaryop (tree type, enum tree_code op,
     594              :                                              const svalue *arg)
     595              : {
     596       338466 :   if (const svalue *folded = maybe_fold_unaryop  (type, op, arg))
     597              :     return folded;
     598       152492 :   const unaryop_svalue::key_t key (type, op, arg);
     599       152492 :   auto iter = m_unaryop_values_map.find (key);
     600       152492 :   if (iter != m_unaryop_values_map.end ())
     601       143299 :     return iter->second;
     602         9193 :   unaryop_svalue *unaryop_sval
     603         9193 :     = new unaryop_svalue (alloc_node_id (), type, op, arg);
     604         9193 :   RETURN_UNKNOWN_IF_TOO_COMPLEX (unaryop_sval);
     605         9155 :   m_unaryop_values_map.insert ({key, unaryop_sval});
     606         9155 :   return unaryop_sval;
     607              : }
     608              : 
     609              : /* Get a tree code for a cast to DST_TYPE from SRC_TYPE.
     610              :    Use NOP_EXPR if possible (e.g. to help fold_unary convert casts
     611              :    of 0 to (T*) to simple pointer constants), but use FIX_TRUNC_EXPR
     612              :    and VIEW_CONVERT_EXPR for cases that fold_unary would otherwise crash
     613              :    on.  */
     614              : 
     615              : static enum tree_code
     616       185524 : get_code_for_cast (tree dst_type, tree src_type)
     617              : {
     618            0 :   if (!dst_type)
     619              :     return NOP_EXPR;
     620            0 :   if (!src_type)
     621              :     return NOP_EXPR;
     622              : 
     623       183264 :   if (SCALAR_FLOAT_TYPE_P (src_type))
     624              :     {
     625           31 :       if (TREE_CODE (dst_type) == INTEGER_TYPE)
     626              :         return FIX_TRUNC_EXPR;
     627              :       else
     628           28 :         return VIEW_CONVERT_EXPR;
     629              :     }
     630              : 
     631              :   return NOP_EXPR;
     632              : }
     633              : 
     634              : /* Return the svalue * for a cast of ARG to type TYPE, creating it
     635              :    if necessary.  */
     636              : 
     637              : const svalue *
     638       344581 : region_model_manager::get_or_create_cast (tree type, const svalue *arg)
     639              : {
     640              :   /* No-op if the types are the same.  */
     641       344581 :   if (type == arg->get_type ())
     642              :     return arg;
     643              : 
     644              :   /* Don't attempt to handle casts involving vector types for now.  */
     645       299987 :   if (type)
     646       185524 :     if (VECTOR_TYPE_P (type)
     647       185524 :         || (arg->get_type ()
     648       183264 :             && VECTOR_TYPE_P (arg->get_type ())))
     649            0 :       return get_or_create_unknown_svalue (type);
     650              : 
     651       185524 :   enum tree_code op = get_code_for_cast (type, arg->get_type ());
     652       299987 :   return get_or_create_unaryop (type, op, arg);
     653              : }
     654              : 
     655              : /* Subroutine of region_model_manager::get_or_create_binop.
     656              :    Attempt to fold the inputs and return a simpler svalue *.
     657              :    Otherwise, return nullptr.  */
     658              : 
     659              : const svalue *
     660       102911 : region_model_manager::maybe_fold_binop (tree type, enum tree_code op,
     661              :                                         const svalue *arg0,
     662              :                                         const svalue *arg1)
     663              : {
     664       102911 :   tree cst0 = arg0->maybe_get_constant ();
     665       102911 :   tree cst1 = arg1->maybe_get_constant ();
     666              :   /* (CST OP CST).  */
     667       102911 :   if (cst0 && cst1)
     668              :     {
     669        18620 :       if (type)
     670              :         {
     671        18620 :           if (tree result = fold_binary (op, type, cst0, cst1))
     672        18582 :             if (CONSTANT_CLASS_P (result))
     673        18580 :               return get_or_create_constant_svalue (result);
     674              :         }
     675              :       else
     676              :         {
     677            0 :           if (tree result = int_const_binop (op, cst0, cst1, -1))
     678            0 :             return get_or_create_constant_svalue (NULL_TREE, result);
     679              :         }
     680              :     }
     681              : 
     682        84331 :   if ((type && FLOAT_TYPE_P (type))
     683        83832 :       || (arg0->get_type () && FLOAT_TYPE_P (arg0->get_type ()))
     684       168111 :       || (arg1->get_type () && FLOAT_TYPE_P (arg1->get_type ())))
     685              :     return nullptr;
     686              : 
     687        83780 :   switch (op)
     688              :     {
     689              :     default:
     690              :       break;
     691        27773 :     case POINTER_PLUS_EXPR:
     692        27773 :       if (auto *ptr = arg0->maybe_get_symbolic_pointer ())
     693              :         {
     694        14832 :           auto &byte_mgr = get_node_manager ().get_scalars ().get_bytes ();
     695              :           /* Interpret arg1 as signed twos-complement, so that very
     696              :              large values become subtractions.  */
     697        14832 :           auto *signed_arg1
     698        14832 :             = get_or_create_cast (signed_type_for (arg1->get_type ()),
     699              :                                   arg1);
     700        14832 :           auto &offset = byte_mgr.get_scalar_from_svalue (*signed_arg1);
     701        14832 :           symbolic::pointer ptr_plus_arg1
     702              :             (ptr->get_base_region (),
     703        14832 :              ptr->get_byte_offset () + offset);
     704        14832 :           return get_ptr_svalue (type, ptr_plus_arg1);
     705              :         }
     706              :       /* Fall through.  */
     707        38761 :     case PLUS_EXPR:
     708              :       /* (VAL + 0) -> VAL.  */
     709        38761 :       if (cst1 && zerop (cst1))
     710         1827 :         return get_or_create_cast (type, arg0);
     711              :       /* X + (-X) -> 0.  */
     712        36934 :       if (const unaryop_svalue *unary_op = arg1->dyn_cast_unaryop_svalue ())
     713          724 :         if (unary_op->get_op () == NEGATE_EXPR
     714           74 :             && unary_op->get_arg () == arg0
     715          758 :             && type && (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)))
     716           32 :           return get_or_create_int_cst (type, 0);
     717              :       /* X + (Y - X) -> Y.  */
     718        36902 :       if (const binop_svalue *bin_op = arg1->dyn_cast_binop_svalue ())
     719         4977 :         if (bin_op->get_op () == MINUS_EXPR)
     720           44 :           if (bin_op->get_arg1 () == arg0)
     721           12 :             return get_or_create_cast (type, bin_op->get_arg0 ());
     722              :       break;
     723         3272 :     case MINUS_EXPR:
     724              :       /* (VAL - 0) -> VAL.  */
     725         3272 :       if (cst1 && zerop (cst1))
     726           12 :         return get_or_create_cast (type, arg0);
     727              :       /* (0 - VAL) -> -VAL.  */
     728         3260 :       if (cst0 && zerop (cst0))
     729           17 :         return get_or_create_unaryop (type, NEGATE_EXPR, arg1);
     730              :       /* (X + Y) - X -> Y.  */
     731         3243 :       if (const binop_svalue *binop = arg0->dyn_cast_binop_svalue ())
     732           60 :         if (binop->get_op () == PLUS_EXPR)
     733           56 :           if (binop->get_arg0 () == arg1)
     734            0 :             return get_or_create_cast (type, binop->get_arg1 ());
     735              :       break;
     736          696 :     case POINTER_DIFF_EXPR:
     737              :       /* (X POINTER_PLUS Y) POINTER_DIFF_EXPR X -> Y.  */
     738          696 :       if (const binop_svalue *binop = arg0->dyn_cast_binop_svalue ())
     739           74 :         if (binop->get_op () == POINTER_PLUS_EXPR)
     740           74 :           if (binop->get_arg0 () == arg1)
     741           56 :             return get_or_create_cast (type, binop->get_arg1 ());
     742              : 
     743              :       /* Fold symptr(base_reg + x) - symptr(base_reg + y) --> x -- y
     744              :          when they share the same base region.  */
     745          640 :       if (auto *sym_ptr_0 = arg0->maybe_get_symbolic_pointer ())
     746          262 :         if (auto *sym_ptr_1 = arg1->maybe_get_symbolic_pointer ())
     747          246 :           if (&sym_ptr_0->get_base_region () == &sym_ptr_1->get_base_region ())
     748              :             {
     749          118 :               auto &offset_byte_diff = (sym_ptr_0->get_byte_offset ()
     750          118 :                                         - sym_ptr_1->get_byte_offset ());
     751          118 :               return get_svalue_from_scalar<concrete_byte_offset>
     752          118 :                 (type,
     753          118 :                  offset_byte_diff);
     754              :             }
     755              :       break;
     756        12915 :     case MULT_EXPR:
     757              :       /* (VAL * 0).  */
     758        12915 :       if (cst1
     759        12394 :           && zerop (cst1)
     760        12981 :           && (type == NULL_TREE || INTEGRAL_TYPE_P (type)))
     761           56 :         return get_or_create_int_cst (type, 0);
     762              :       /* (VAL * 1) -> VAL.  */
     763        12859 :       if (cst1 && integer_onep (cst1))
     764          169 :         return get_or_create_cast (type, arg0);
     765              :       break;
     766         2212 :     case BIT_AND_EXPR:
     767         2212 :       if (cst1)
     768              :         {
     769         1821 :           if (zerop (cst1)
     770         1821 :               && (type == NULL_TREE || INTEGRAL_TYPE_P (type)))
     771              :             /* "(ARG0 & 0)" -> "0".  */
     772           58 :             return get_or_create_int_cst (type, 0);
     773              :         }
     774         2154 :       if (arg0->get_type () == boolean_type_node
     775         2154 :           && arg1->get_type () == boolean_type_node)
     776              :         {
     777              :           /* If the LHS are both _Bool, then... */
     778              :           /* ..."(1 & x) -> x".  */
     779          356 :           if (cst0 && !zerop (cst0))
     780            0 :             return get_or_create_cast (type, arg1);
     781              :           /* ..."(x & 1) -> x".  */
     782          356 :           if (cst1 && !zerop (cst1))
     783          160 :             return get_or_create_cast (type, arg0);
     784              :           /* ..."(0 & x) -> 0".  */
     785          196 :           if (cst0 && zerop (cst0))
     786            0 :             return get_or_create_int_cst (type, 0);
     787              :           /* ..."(x & 0) -> 0".  */
     788          196 :           if (cst1 && zerop (cst1))
     789            0 :             return get_or_create_int_cst (type, 0);
     790              :         }
     791              :       break;
     792          781 :     case BIT_IOR_EXPR:
     793          781 :       if (arg0->get_type () == boolean_type_node
     794          781 :           && arg1->get_type () == boolean_type_node)
     795              :         {
     796              :           /* If the LHS are both _Bool, then... */
     797              :           /* ..."(1 | x) -> 1".  */
     798          119 :           if (cst0 && !zerop (cst0))
     799            0 :             return get_or_create_int_cst (type, 1);
     800              :           /* ..."(x | 1) -> 1".  */
     801          119 :           if (cst1 && !zerop (cst1))
     802           38 :             return get_or_create_int_cst (type, 1);
     803              :           /* ..."(0 | x) -> x".  */
     804           81 :           if (cst0 && zerop (cst0))
     805            0 :             return get_or_create_cast (type, arg1);
     806              :           /* ..."(x | 0) -> x".  */
     807           81 :           if (cst1 && zerop (cst1))
     808           32 :             return get_or_create_cast (type, arg0);
     809              :         }
     810              :       break;
     811            8 :     case TRUTH_ANDIF_EXPR:
     812            8 :     case TRUTH_AND_EXPR:
     813            8 :       if (cst1)
     814              :         {
     815            8 :           if (zerop (cst1) && INTEGRAL_TYPE_P (type))
     816              :             /* "(ARG0 && 0)" -> "0".  */
     817            4 :             return get_or_create_constant_svalue (build_int_cst (type, 0));
     818              :           else
     819              :             /* "(ARG0 && nonzero-cst)" -> "ARG0".  */
     820            4 :             return get_or_create_cast (type, arg0);
     821              :         }
     822              :       break;
     823           12 :     case TRUTH_ORIF_EXPR:
     824           12 :     case TRUTH_OR_EXPR:
     825           12 :       if (cst1)
     826              :         {
     827           12 :           if (zerop (cst1))
     828              :             /* "(ARG0 || 0)" -> "ARG0".  */
     829            8 :             return get_or_create_cast (type, arg0);
     830              :           else
     831              :             /* "(ARG0 && nonzero-cst)" -> "nonzero-cst".  */
     832            4 :             return get_or_create_cast (type, arg1);
     833              :         }
     834              :       break;
     835              : 
     836          820 :     case LSHIFT_EXPR:
     837          820 :       if (cst1)
     838          564 :         if (zerop (cst1))
     839            0 :           return get_or_create_cast (type, arg0);
     840              :       break;
     841              : 
     842          928 :     case TRUNC_DIV_EXPR:
     843          928 :     case CEIL_DIV_EXPR:
     844          928 :     case FLOOR_DIV_EXPR:
     845          928 :     case ROUND_DIV_EXPR:
     846          928 :     case TRUNC_MOD_EXPR:
     847          928 :     case CEIL_MOD_EXPR:
     848          928 :     case FLOOR_MOD_EXPR:
     849          928 :     case ROUND_MOD_EXPR:
     850          928 :     case RDIV_EXPR:
     851          928 :     case EXACT_DIV_EXPR:
     852          928 :       {
     853          928 :         value_range arg1_vr;
     854          928 :         if (arg1->maybe_get_value_range (arg1_vr))
     855          872 :           if (arg1_vr.zero_p ())
     856            0 :             return get_or_create_unknown_svalue (type);
     857          928 :       }
     858          928 :       break;
     859              :     }
     860              : 
     861              :   /* For associative ops, fold "(X op CST_A) op CST_B)" to
     862              :      "X op (CST_A op CST_B)".  */
     863        66085 :   if (cst1 && associative_tree_code (op))
     864        35704 :     if (const binop_svalue *binop = arg0->dyn_cast_binop_svalue ())
     865         9944 :       if (binop->get_op () == op
     866         9944 :           && binop->get_arg1 ()->maybe_get_constant ())
     867         1321 :         return get_or_create_binop
     868         1321 :           (type, op, binop->get_arg0 (),
     869              :            get_or_create_binop (type, op,
     870         1321 :                                 binop->get_arg1 (), arg1));
     871              : 
     872              :   /* associative_tree_code is false for POINTER_PLUS_EXPR, but we
     873              :      can fold:
     874              :        "(PTR ptr+ CST_A) ptr+ CST_B)" to "PTR ptr+ (CST_A ptr+ CST_B)"
     875              :      e.g. in data-model-1.c: test_4c.  */
     876        65020 :   if (cst1 && op == POINTER_PLUS_EXPR)
     877         5189 :     if (const binop_svalue *binop = arg0->dyn_cast_binop_svalue ())
     878          679 :       if (binop->get_op () == POINTER_PLUS_EXPR)
     879          667 :         if (binop->get_arg1 ()->maybe_get_constant ())
     880          469 :           return get_or_create_binop
     881          469 :             (type, op, binop->get_arg0 (),
     882              :              get_or_create_binop (size_type_node, op,
     883          469 :                                   binop->get_arg1 (), arg1));
     884              : 
     885              :   /* Distribute multiplication by a constant through addition/subtraction:
     886              :      (X + Y) * CST => (X * CST) + (Y * CST).  */
     887        64551 :   if (cst1 && op == MULT_EXPR)
     888        12148 :     if (const binop_svalue *binop = arg0->dyn_cast_binop_svalue ())
     889          982 :       if (binop->get_op () == PLUS_EXPR
     890          982 :           || binop->get_op () == MINUS_EXPR)
     891              :         {
     892          768 :           return get_or_create_binop
     893          768 :             (type, binop->get_op (),
     894              :              get_or_create_binop (type, op,
     895              :                                   binop->get_arg0 (), arg1),
     896              :              get_or_create_binop (type, op,
     897          768 :                                   binop->get_arg1 (), arg1));
     898              :         }
     899              : 
     900              : 
     901              :   /* Typeless operations, assumed to be effectively arbitrary sized
     902              :      integers following normal arithmetic rules.  */
     903        63783 :   if (!type)
     904            0 :     switch (op)
     905              :       {
     906              :       default:
     907              :         break;
     908            0 :       case MINUS_EXPR:
     909            0 :         {
     910              :           /* (X - X) -> 0.  */
     911            0 :           if (arg0 == arg1)
     912            0 :             return get_or_create_int_cst (type, 0);
     913              : 
     914              :           /* (X + A) - (A + B) -> (A - B).  */
     915            0 :           if (const binop_svalue *binop0 = arg0->dyn_cast_binop_svalue ())
     916            0 :             if (const binop_svalue *binop1 = arg1->dyn_cast_binop_svalue ())
     917            0 :               if (binop0->get_op () == PLUS_EXPR
     918            0 :                   && binop1->get_op () == PLUS_EXPR
     919            0 :                   && binop0->get_arg0 () == binop1->get_arg0 ())
     920            0 :                 return get_or_create_binop (NULL_TREE, op,
     921              :                                             binop0->get_arg1 (),
     922            0 :                                             binop1->get_arg1 ());
     923              :         }
     924              :         break;
     925              : 
     926            0 :       case EXACT_DIV_EXPR:
     927            0 :         {
     928            0 :           if (const unaryop_svalue *unaryop0 = arg0->dyn_cast_unaryop_svalue ())
     929              :             {
     930            0 :               if (unaryop0->get_op () == NOP_EXPR)
     931            0 :                 if (const svalue *sval = maybe_fold_binop (NULL_TREE, op,
     932              :                                                            unaryop0->get_arg (),
     933              :                                                            arg1))
     934              :                   return sval;
     935              :             }
     936            0 :           if (const binop_svalue *binop0 = arg0->dyn_cast_binop_svalue ())
     937              :             {
     938            0 :               switch (binop0->get_op ())
     939              :                 {
     940              :                 default:
     941              :                   break;
     942              : 
     943            0 :                 case PLUS_EXPR:
     944            0 :                 case MINUS_EXPR:
     945              :                   /* (A op B) / C -> (A / C) op (B / C).  */
     946            0 :                   {
     947            0 :                     if (const svalue *op_on_a
     948            0 :                         = maybe_fold_binop (NULL_TREE, op,
     949              :                                             binop0->get_arg0 (), arg1))
     950            0 :                       if (const svalue *op_on_b
     951            0 :                           = maybe_fold_binop (NULL_TREE, op,
     952              :                                               binop0->get_arg1 (), arg1))
     953            0 :                         return get_or_create_binop (NULL_TREE,
     954              :                                                     binop0->get_op (),
     955            0 :                                                     op_on_a, op_on_b);
     956              :                   }
     957              :                   break;
     958              : 
     959            0 :                 case MULT_EXPR:
     960              :                   /* (A * B) / C -> A * (B / C) if C is a divisor of B.
     961              :                      In particular, this should also handle the case
     962              :                      (A * B) / B -> A.  */
     963            0 :                   if (const svalue *b_div_c
     964            0 :                       = maybe_fold_binop (NULL_TREE, op,
     965              :                                           binop0->get_arg1 (), arg1))
     966            0 :                     return get_or_create_binop (NULL_TREE, binop0->get_op (),
     967            0 :                                                 binop0->get_arg0 (), b_div_c);
     968              :                 }
     969              :             }
     970              :         }
     971              :         break;
     972              :       }
     973              : 
     974              :   /* etc.  */
     975              : 
     976              :   return nullptr;
     977              : }
     978              : 
     979              : /* Return the svalue * for an binary operation OP on ARG0 and ARG1
     980              :    with a result of type TYPE, creating it if necessary.  */
     981              : 
     982              : const svalue *
     983       102911 : region_model_manager::get_or_create_binop (tree type, enum tree_code op,
     984              :                                            const svalue *arg0,
     985              :                                            const svalue *arg1)
     986              : {
     987              :   /* For commutative ops, put any constant on the RHS.  */
     988       102911 :   if (arg0->maybe_get_constant () && commutative_tree_code (op))
     989              :     std::swap (arg0, arg1);
     990              : 
     991       102911 :   if (const svalue *folded = maybe_fold_binop (type, op, arg0, arg1))
     992              :     return folded;
     993              : 
     994              :   /* Ops on "unknown"/"poisoned" are unknown (unless we were able to fold
     995              :      it via an identity in maybe_fold_binop).  */
     996        64334 :   if (!arg0->can_have_associated_state_p ()
     997        64334 :       || !arg1->can_have_associated_state_p ())
     998        13832 :     return get_or_create_unknown_svalue (type);
     999              : 
    1000        50502 :   const binop_svalue::key_t key (type, op, arg0, arg1);
    1001        50502 :   auto iter = m_binop_values_map.find (key);
    1002        50502 :   if (iter != m_binop_values_map.end ())
    1003        36441 :     return iter->second;
    1004        14061 :   binop_svalue *binop_sval
    1005        14061 :     = new binop_svalue (alloc_node_id (), type, op, arg0, arg1);
    1006        14061 :   RETURN_UNKNOWN_IF_TOO_COMPLEX (binop_sval);
    1007        14009 :   m_binop_values_map.insert ({key, binop_sval});
    1008        14009 :   return binop_sval;
    1009              : }
    1010              : 
    1011              : /* Subroutine of get_svalue_from_bit_vector.  */
    1012              : 
    1013              : const svalue *
    1014      3007097 : region_model_manager::
    1015              : maybe_fold_svalue_from_bit_vector (tree type,
    1016              :                                    const symbolic::bit_vector &bv)
    1017              : {
    1018      3007097 :   if (auto *svalue_bv = bv.dyn_cast_bit_vector_from_svalue ())
    1019              :     {
    1020      1736218 :       auto &sval = svalue_bv->get_svalue ();
    1021              : 
    1022      1736218 :       if (!type)
    1023              :         return &sval;
    1024              : 
    1025      1732898 :       if (type && sval.get_type ()
    1026      1732886 :           && types_compatible_p (type, sval.get_type ()))
    1027              :         return &sval;
    1028              : 
    1029        35356 :       if (auto underlying_ptr
    1030        17678 :             = sval.dyn_cast_pointer_svalue ())
    1031         1159 :         if (type && POINTER_TYPE_P (type))
    1032           73 :           return get_ptr_svalue (type,
    1033           73 :                                  underlying_ptr->get_symbolic_pointer ());
    1034              : 
    1035        17605 :       if (tree cst = sval.maybe_get_constant ())
    1036         5056 :         if (type
    1037         5056 :             && ANY_INTEGRAL_TYPE_P (type)
    1038         4845 :             && ANY_INTEGRAL_TYPE_P (TREE_TYPE (cst)))
    1039         4644 :           if (tree cast_expr = fold_build1 (NOP_EXPR, type, cst))
    1040         4644 :             if (CONSTANT_CLASS_P (cast_expr))
    1041         4644 :               return get_or_create_constant_svalue (cast_expr);
    1042              : 
    1043              :       /* Otherwise, we have SVALUE(OUTER_TYPE, BIT_VECTOR (SVALUE (INNER_TYPE)))
    1044              :          Fold it to CAST_SVALUE(OUTER_TYPE (SVALUE (INNER_TYPE))).  */
    1045        12961 :       return get_or_create_cast (type, &sval);
    1046              :     }
    1047              : 
    1048              :   /* Fold
    1049              :        svalue_from_bit_vector (_Bool, crop_to_bit_0 (SVAL (_Bool)))
    1050              :      to:
    1051              :        SVAL (_Bool).  */
    1052      1270879 :   if (type == boolean_type_node)
    1053        23254 :     if (auto *cropped_bv = bv.dyn_cast_cropped_bit_vector ())
    1054              :       {
    1055            4 :         auto uncropped_bit_range = cropped_bv->get_uncropped_bits_to_use ();
    1056            4 :         if (uncropped_bit_range.get_start_offset ().is_zero_p ()
    1057            4 :             && uncropped_bit_range.get_size ().is_one_p ())
    1058              :           {
    1059            4 :             auto &uncropped_bv = cropped_bv->get_uncropped_bit_vector ();
    1060            8 :             if (auto *from_sval
    1061            4 :                   = uncropped_bv.dyn_cast_bit_vector_from_svalue ())
    1062              :               {
    1063            4 :                 const svalue &sval = from_sval->get_svalue ();
    1064            4 :                 if (sval.get_type () == boolean_type_node)
    1065      3007097 :                   return &sval;
    1066              :               }
    1067              :           }
    1068              :       }
    1069              : 
    1070      1270875 :   if (!type)
    1071              :     return nullptr;
    1072              : 
    1073       537173 :   if (auto *tristate_bv = bv.dyn_cast_tristate_bit_vector ())
    1074       292641 :     switch (tristate_bv->get_bit_value ().get_value ())
    1075              :       {
    1076            0 :       default:
    1077            0 :         gcc_unreachable ();
    1078       188018 :         break;
    1079              : 
    1080       188018 :       case tristate::TS_UNKNOWN:
    1081       188018 :         return get_or_create_unknown_svalue (type);
    1082              : 
    1083        86192 :       case tristate::TS_FALSE:
    1084        86192 :         if (INTEGRAL_TYPE_P (type))
    1085        55138 :           return get_or_create_int_cst (type, 0);
    1086              :         break;
    1087              : 
    1088        18431 :       case tristate::TS_TRUE:
    1089        18431 :         if (INTEGRAL_TYPE_P (type))
    1090        36838 :           return get_or_create_int_cst (type,
    1091        18419 :                                         wi::minus_one (TYPE_PRECISION (type)));
    1092              :         break;
    1093              :       }
    1094              : 
    1095              :   /* Attempt to get a constant_svalue from a concrete bit_vector.  */
    1096       275598 :   if (can_native_interpret_type_p (type))
    1097              :     {
    1098       272550 :       concrete_byte_size byte_size;
    1099       272550 :       if (bv.maybe_get_concrete_size_in_bytes (&byte_size))
    1100              :         {
    1101              :           // Attempt to extract a constant
    1102       258790 :           std::vector<unsigned char> buf;
    1103       509339 :           for (concrete_byte_offset byte_offset = 0_bytes;
    1104       509339 :                byte_offset < byte_size;
    1105       250549 :                byte_offset += 1_byte)
    1106              :             {
    1107       478261 :               auto &bv_byte = bv.get_byte_at_offset (byte_offset);
    1108              : 
    1109       478261 :               uint8_t byte_val;
    1110       478261 :               if (bv_byte.maybe_as_uint8 (&byte_val))
    1111       250549 :                 buf.push_back (byte_val);
    1112              :               else
    1113       227712 :                 return nullptr;
    1114       227712 :             }
    1115              : 
    1116        62156 :           if (tree interpreted = native_interpret_expr (type,
    1117        31078 :                                                         buf.data (),
    1118        31078 :                                                         buf.size ()))
    1119        31078 :             if (CONSTANT_CLASS_P (interpreted))
    1120        31078 :               return get_or_create_constant_svalue (interpreted);
    1121       258790 :         }
    1122       272550 :     }
    1123              : 
    1124              :   return nullptr;
    1125              : }
    1126              : 
    1127              : /* Strip off qualifiers from pointer types, apart from address spaces.  */
    1128              : 
    1129              : static tree
    1130      1600688 : canonicalize_ptr_type_for_bit_vector (tree type)
    1131              : {
    1132      1600688 :   gcc_assert (type);
    1133      1600688 :   gcc_assert (POINTER_TYPE_P (type));
    1134              : 
    1135      1600688 :   tree pointee = TREE_TYPE (type);
    1136      1600688 :   qualifier_set qs (TYPE_UNQUALIFIED, TYPE_ADDR_SPACE (pointee));
    1137      1600688 :   tree unqualified_pointee = build_qualified_type (pointee, qs);
    1138      1600688 :   tree result = build_pointer_type (unqualified_pointee);
    1139      1600688 :   if (CHECKING_P)
    1140      1600688 :     gcc_assert (types_compatible_p (type, result));
    1141      1600688 :   return result;
    1142              : }
    1143              : 
    1144              : /* Get or create an svalue for BV interpreted as TYPE.  */
    1145              : 
    1146              : const svalue *
    1147      3007097 : region_model_manager::
    1148              : get_svalue_from_bit_vector (tree type,
    1149              :                             const symbolic::bit_vector &bv)
    1150              : {
    1151              :   // TODO: assert that we have matching sizes
    1152              : 
    1153      3007097 :   if (type && POINTER_TYPE_P (type))
    1154      1600688 :     type = canonicalize_ptr_type_for_bit_vector (type);
    1155              : 
    1156      3007097 :   if (const svalue *folded = maybe_fold_svalue_from_bit_vector (type, bv))
    1157              :     return folded;
    1158              : 
    1159       978222 :   const svalue_from_bit_vector::key_t key {type, &bv};
    1160       978222 :   auto iter = m_bit_vector_to_svalue_map.find (key);
    1161       978222 :   if (iter != m_bit_vector_to_svalue_map.end ())
    1162       938622 :     return iter->second;
    1163        39600 :   svalue_from_bit_vector *sval
    1164        39600 :     = new svalue_from_bit_vector (alloc_node_id (), type, bv);
    1165        39600 :   RETURN_UNKNOWN_IF_TOO_COMPLEX (sval);
    1166        39357 :   m_bit_vector_to_svalue_map.insert ({key, sval});
    1167        39357 :   return sval;
    1168              : }
    1169              : 
    1170              : /* Get or create an svalue of TYPE from SCALAR.  */
    1171              : 
    1172              : template <typename ConcreteValueType>
    1173              : const svalue *
    1174       159035 : region_model_manager::
    1175              : get_svalue_from_scalar (tree type,
    1176              :                         const symbolic::scalar<ConcreteValueType> &scalar)
    1177              : {
    1178       159035 :   switch (scalar.get_kind ())
    1179              :     {
    1180            0 :     default:
    1181            0 :       gcc_unreachable ();
    1182              : 
    1183       123056 :     case symbolic::scalar<ConcreteValueType>::kind::concrete:
    1184              :       {
    1185       123056 :         auto &conc
    1186              :           = (const symbolic::concrete_scalar<ConcreteValueType> &)scalar;
    1187              :         tree cst
    1188       123056 :           = make_integer_cst_from_integer (type,
    1189              :                                            conc.get_integer (),
    1190              :                                            true);
    1191       123056 :         return get_or_create_constant_svalue (cst);
    1192              :       }
    1193              :       break;
    1194              : 
    1195         7369 :     case symbolic::scalar<ConcreteValueType>::kind::sum:
    1196              :       {
    1197         7369 :         auto &sum = (const symbolic::sum<ConcreteValueType> &)scalar;
    1198         7369 :         auto *concrete_addend = sum.get_any_concrete_addend ();
    1199              :         const svalue *result
    1200              :           = (concrete_addend
    1201         7369 :              ? get_svalue_from_scalar (type, *concrete_addend)
    1202          119 :              : get_or_create_int_cst (type, 0));
    1203        14883 :         for (size_t i = 0; i < sum.get_num_symbolic_addends (); ++i)
    1204              :           {
    1205         7514 :             auto &symbolic_addend = sum.get_symbolic_addend (i);
    1206              :             result = get_or_create_binop
    1207         7514 :               (type,
    1208              :                PLUS_EXPR,
    1209              :                result,
    1210              :                get_svalue_from_scalar (type, symbolic_addend));
    1211              :           }
    1212              :         return result;
    1213              :       }
    1214              :       break;
    1215              : 
    1216         1767 :     case symbolic::scalar<ConcreteValueType>::kind::product:
    1217              :       {
    1218         1767 :         auto &product = (const symbolic::product<ConcreteValueType> &)scalar;
    1219              :         return get_or_create_binop
    1220         1767 :               (type,
    1221              :                MULT_EXPR,
    1222              :                get_svalue_from_scalar (type, product.get_lhs ()),
    1223         1767 :                get_svalue_from_scalar (type, product.get_rhs ()));
    1224              :       }
    1225              :       break;
    1226              : 
    1227        26830 :     case symbolic::scalar<ConcreteValueType>::kind::from_svalue:
    1228              :       {
    1229        26830 :         auto &scalar_from_sval
    1230              :           = (const symbolic::scalar_from_svalue<ConcreteValueType> &)scalar;
    1231        26830 :         return get_or_create_cast (type, &scalar_from_sval.get_svalue ());
    1232              :       }
    1233              :       break;
    1234              : 
    1235            0 :     case symbolic::scalar<ConcreteValueType>::kind::named_test:
    1236            0 :       gcc_unreachable ();
    1237              :       break;
    1238              : 
    1239           13 :     case symbolic::scalar<ConcreteValueType>::kind::bits_from_bytes:
    1240              :       {
    1241           13 :         auto &from_bytes = (const symbolic::bits_from_bytes_scalar &)scalar;
    1242              :         return get_or_create_binop
    1243           13 :               (type,
    1244              :                MULT_EXPR,
    1245           13 :                get_svalue_from_scalar (type, from_bytes.get_bytes ()),
    1246           26 :                get_or_create_int_cst (type, BITS_PER_UNIT));
    1247              :       }
    1248              :       break;
    1249              :     }
    1250              : }
    1251              : 
    1252              : template
    1253              : const svalue *
    1254              : region_model_manager::
    1255              : get_svalue_from_scalar (tree type,
    1256              :                         const symbolic_bit_offset &scalar);
    1257              : 
    1258              : template
    1259              : const svalue *
    1260              : region_model_manager::
    1261              : get_svalue_from_scalar (tree type,
    1262              :                         const symbolic_byte_offset &scalar);
    1263              : 
    1264              : /* Return the svalue * that decorates ARG as being unmergeable,
    1265              :    creating it if necessary.  */
    1266              : 
    1267              : const svalue *
    1268         1024 : region_model_manager::get_or_create_unmergeable (const svalue *arg)
    1269              : {
    1270         1024 :   if (arg->get_kind () == SK_UNMERGEABLE)
    1271              :     return arg;
    1272              : 
    1273         1024 :   auto iter = m_unmergeable_values_map.find (arg);
    1274         1024 :   if (iter != m_unmergeable_values_map.end ())
    1275          719 :     return iter->second;
    1276          305 :   unmergeable_svalue *unmergeable_sval
    1277          305 :     = new unmergeable_svalue (alloc_node_id (), arg);
    1278          305 :   RETURN_UNKNOWN_IF_TOO_COMPLEX (unmergeable_sval);
    1279          305 :   m_unmergeable_values_map.insert ({arg, unmergeable_sval});
    1280          305 :   return unmergeable_sval;
    1281              : }
    1282              : 
    1283              : /* Return the svalue * of type TYPE for the merger of value BASE_SVAL
    1284              :    and ITER_SVAL at SNODE, creating it if necessary.  */
    1285              : 
    1286              : const svalue *
    1287         2849 : region_model_manager::
    1288              : get_or_create_widening_svalue (tree type,
    1289              :                                const supernode *snode,
    1290              :                                const svalue *base_sval,
    1291              :                                const svalue *iter_sval)
    1292              : {
    1293         2849 :   gcc_assert (base_sval->get_kind () != SK_WIDENING);
    1294         2849 :   gcc_assert (iter_sval->get_kind () != SK_WIDENING);
    1295         2849 :   const widening_svalue::key_t key (type, snode, base_sval, iter_sval);
    1296         2849 :   auto iter = m_widening_values_map.find (key);
    1297         2849 :   if (iter != m_widening_values_map.end ())
    1298         1156 :     return iter->second;
    1299         1693 :   widening_svalue *widening_sval
    1300              :     = new widening_svalue (alloc_node_id (), type, snode, base_sval,
    1301         1693 :                            iter_sval);
    1302         1693 :   RETURN_UNKNOWN_IF_TOO_COMPLEX (widening_sval);
    1303         1693 :   m_widening_values_map.insert ({key, widening_sval});
    1304         1693 :   return widening_sval;
    1305              : }
    1306              : 
    1307              : /* Return the svalue * of type TYPE for the value conjured for ID_REG
    1308              :    at STMT (using IDX for any further disambiguation),
    1309              :    creating it if necessary.
    1310              :    Use P to purge existing state from the svalue, for the case where a
    1311              :    conjured_svalue would be reused along an execution path.  */
    1312              : 
    1313              : const svalue *
    1314        26264 : region_model_manager::get_or_create_conjured_svalue (tree type,
    1315              :                                                      const gimple *stmt,
    1316              :                                                      const region *id_reg,
    1317              :                                                      const conjured_purge &p,
    1318              :                                                      unsigned idx)
    1319              : {
    1320        26264 :   auto &bv_mgr = m_node_mgr.get_bit_vector_manager ();
    1321        26264 :   auto &bv = bv_mgr.get_conjured_bits (stmt, id_reg, p, idx);
    1322        26264 :   return get_svalue_from_bit_vector (type, bv);
    1323              : }
    1324              : 
    1325              : /* Subroutine of region_model_manager::get_or_create_asm_output_svalue.
    1326              :    Return a folded svalue, or nullptr.  */
    1327              : 
    1328              : const svalue *
    1329          297 : region_model_manager::
    1330              : maybe_fold_asm_output_svalue (tree type,
    1331              :                               const vec<const svalue *> &inputs)
    1332              : {
    1333              :   /* Unknown inputs should lead to unknown results.  */
    1334         1304 :   for (const auto &iter : inputs)
    1335          469 :     if (iter->get_kind () == SK_UNKNOWN)
    1336            8 :       return get_or_create_unknown_svalue (type);
    1337              : 
    1338              :   return nullptr;
    1339              : }
    1340              : 
    1341              : /* Return the svalue * of type TYPE for OUTPUT_IDX of the deterministic
    1342              :    asm stmt ASM_STMT, given INPUTS as inputs.  */
    1343              : 
    1344              : const svalue *
    1345          289 : region_model_manager::
    1346              : get_or_create_asm_output_svalue (tree type,
    1347              :                                  const gasm *asm_stmt,
    1348              :                                  unsigned output_idx,
    1349              :                                  const vec<const svalue *> &inputs)
    1350              : {
    1351          289 :   gcc_assert (inputs.length () <= asm_output_svalue::MAX_INPUTS);
    1352              : 
    1353          578 :   if (const svalue *folded
    1354          289 :         = maybe_fold_asm_output_svalue (type, inputs))
    1355              :     return folded;
    1356              : 
    1357          281 :   const char *asm_string = gimple_asm_string (asm_stmt);
    1358          281 :   const unsigned noutputs = gimple_asm_noutputs (asm_stmt);
    1359              : 
    1360          281 :   const asm_output_svalue::key_t key (type, asm_string, output_idx, inputs);
    1361          281 :   auto iter = m_asm_output_values_map.find (key);
    1362          281 :   if (iter != m_asm_output_values_map.end ())
    1363          108 :     return iter->second;
    1364          173 :   asm_output_svalue *asm_output_sval
    1365              :     = new asm_output_svalue (alloc_node_id (), type, asm_string, output_idx,
    1366          173 :                              noutputs, inputs);
    1367          173 :   RETURN_UNKNOWN_IF_TOO_COMPLEX (asm_output_sval);
    1368          167 :   m_asm_output_values_map.insert ({key, asm_output_sval});
    1369          167 :   return asm_output_sval;
    1370              : }
    1371              : 
    1372              : /* Return the svalue * of type TYPE for OUTPUT_IDX of a deterministic
    1373              :    asm stmt with string ASM_STRING with NUM_OUTPUTS outputs, given
    1374              :    INPUTS as inputs.  */
    1375              : 
    1376              : const svalue *
    1377            8 : region_model_manager::
    1378              : get_or_create_asm_output_svalue (tree type,
    1379              :                                  const char *asm_string,
    1380              :                                  unsigned output_idx,
    1381              :                                  unsigned num_outputs,
    1382              :                                  const vec<const svalue *> &inputs)
    1383              : {
    1384            8 :   gcc_assert (inputs.length () <= asm_output_svalue::MAX_INPUTS);
    1385              : 
    1386           16 :   if (const svalue *folded
    1387            8 :         = maybe_fold_asm_output_svalue (type, inputs))
    1388              :     return folded;
    1389              : 
    1390            8 :   const asm_output_svalue::key_t key (type, asm_string, output_idx, inputs);
    1391            8 :   auto iter = m_asm_output_values_map.find (key);
    1392            8 :   if (iter != m_asm_output_values_map.end ())
    1393            8 :     return iter->second;
    1394            0 :   asm_output_svalue *asm_output_sval
    1395              :     = new asm_output_svalue (alloc_node_id (), type, asm_string, output_idx,
    1396            0 :                              num_outputs, inputs);
    1397            0 :   RETURN_UNKNOWN_IF_TOO_COMPLEX (asm_output_sval);
    1398            0 :   m_asm_output_values_map.insert ({key, asm_output_sval});
    1399            0 :   return asm_output_sval;
    1400              : }
    1401              : 
    1402              : /* Return the svalue * of type TYPE for the result of a call to FNDECL
    1403              :    with __attribute__((const)), given INPUTS as inputs.  */
    1404              : 
    1405              : const svalue *
    1406          639 : region_model_manager::
    1407              : get_or_create_const_fn_result_svalue (tree type,
    1408              :                                       tree fndecl,
    1409              :                                       const vec<const svalue *> &inputs)
    1410              : {
    1411          639 :   gcc_assert (fndecl);
    1412          639 :   gcc_assert (DECL_P (fndecl));
    1413          639 :   gcc_assert (TREE_READONLY (fndecl));
    1414          639 :   gcc_assert (inputs.length () <= const_fn_result_svalue::MAX_INPUTS);
    1415              : 
    1416          639 :   const const_fn_result_svalue::key_t key (type, fndecl, inputs);
    1417          639 :   auto iter = m_const_fn_result_values_map.find (key);
    1418          639 :   if (iter != m_const_fn_result_values_map.end ())
    1419          487 :     return iter->second;
    1420          152 :   const_fn_result_svalue *const_fn_result_sval
    1421          152 :     = new const_fn_result_svalue (alloc_node_id (), type, fndecl, inputs);
    1422          152 :   RETURN_UNKNOWN_IF_TOO_COMPLEX (const_fn_result_sval);
    1423          132 :   m_const_fn_result_values_map.insert ({key, const_fn_result_sval});
    1424          132 :   return const_fn_result_sval;
    1425              : }
    1426              : 
    1427              : /* Get a tree for the size of STRING_CST, or NULL_TREE.
    1428              :    Note that this may be larger than TREE_STRING_LENGTH (implying
    1429              :    a run of trailing zero bytes from TREE_STRING_LENGTH up to this
    1430              :    higher limit).  */
    1431              : 
    1432              : tree
    1433         3543 : get_string_cst_size (const_tree string_cst)
    1434              : {
    1435         3543 :   gcc_assert (TREE_CODE (string_cst) == STRING_CST);
    1436         3543 :   gcc_assert (TREE_CODE (TREE_TYPE (string_cst)) == ARRAY_TYPE);
    1437              : 
    1438         3543 :   return TYPE_SIZE_UNIT (TREE_TYPE (string_cst));
    1439              : }
    1440              : 
    1441              : /* region consolidation.  */
    1442              : 
    1443              : /* Return the region for FNDECL, creating it if necessary.  */
    1444              : 
    1445              : const function_region *
    1446       890833 : region_model_manager::get_region_for_fndecl (tree fndecl)
    1447              : {
    1448       890833 :   gcc_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
    1449              : 
    1450       890833 :   auto iter = m_fndecls_map.find (fndecl);
    1451       890833 :   if (iter != m_fndecls_map.end ())
    1452       880122 :     return iter->second;
    1453        10711 :   function_region *reg
    1454        10711 :     = new function_region (alloc_node_id (), m_code_region, fndecl);
    1455        10711 :   m_fndecls_map.insert ({fndecl, reg});
    1456        10711 :   return reg;
    1457              : }
    1458              : 
    1459              : /* Return the region for LABEL, creating it if necessary.  */
    1460              : 
    1461              : const label_region *
    1462          456 : region_model_manager::get_region_for_label (tree label)
    1463              : {
    1464          456 :   gcc_assert (TREE_CODE (label) == LABEL_DECL);
    1465              : 
    1466          456 :   auto iter = m_labels_map.find (label);
    1467          456 :   if (iter != m_labels_map.end ())
    1468          405 :     return iter->second;
    1469              : 
    1470           51 :   tree fndecl = DECL_CONTEXT (label);
    1471           51 :   gcc_assert (fndecl && TREE_CODE (fndecl) == FUNCTION_DECL);
    1472              : 
    1473           51 :   const function_region *func_reg = get_region_for_fndecl (fndecl);
    1474           51 :   gcc_assert (func_reg);
    1475           51 :   label_region *reg
    1476           51 :     = new label_region (alloc_node_id (), *func_reg, label);
    1477           51 :   m_labels_map.insert ({label, reg});
    1478           51 :   return reg;
    1479              : }
    1480              : 
    1481              : /* Return the region for EXPR, creating it if necessary.  */
    1482              : 
    1483              : const decl_region *
    1484        45202 : region_model_manager::get_region_for_global (tree expr)
    1485              : {
    1486        45202 :   gcc_assert (VAR_P (expr));
    1487              : 
    1488        45202 :   auto iter = m_globals_map.find (expr);
    1489        45202 :   if (iter != m_globals_map.end ())
    1490        38480 :     return iter->second;
    1491              : 
    1492         6722 :   decl_region *reg
    1493         6722 :     = new decl_region (alloc_node_id (), m_globals_region, expr);
    1494         6722 :   m_globals_map.insert ({expr, reg});
    1495         6722 :   return reg;
    1496              : }
    1497              : 
    1498              : /* Return the region for an unknown access of type REGION_TYPE,
    1499              :    creating it if necessary.
    1500              :    This is a symbolic_region, where the pointer is an unknown_svalue
    1501              :    of type &REGION_TYPE.  */
    1502              : 
    1503              : const region *
    1504         9081 : region_model_manager::get_unknown_symbolic_region (tree region_type)
    1505              : {
    1506         9081 :   tree ptr_type = region_type ? build_pointer_type (region_type) : NULL_TREE;
    1507         9081 :   const svalue *unknown_ptr = get_or_create_unknown_svalue (ptr_type);
    1508         9081 :   return get_symbolic_region (unknown_ptr);
    1509              : }
    1510              : 
    1511              : /* Return the region that describes accessing field FIELD of PARENT,
    1512              :    creating it if necessary.  */
    1513              : 
    1514              : const region *
    1515        91249 : region_model_manager::get_field_region (const region *parent, tree field)
    1516              : {
    1517        91249 :   gcc_assert (parent);
    1518        91249 :   gcc_assert (field);
    1519        91249 :   gcc_assert (TREE_CODE (field) == FIELD_DECL);
    1520              : 
    1521              :   /* (*UNKNOWN_PTR).field is (*UNKNOWN_PTR_OF_&FIELD_TYPE).  */
    1522        91249 :   if (parent->symbolic_for_unknown_ptr_p ())
    1523         3126 :     return get_unknown_symbolic_region (TREE_TYPE (field));
    1524              : 
    1525        88123 :   const field_region::key_t key (parent, field);
    1526       162834 :   if (field_region *reg = m_field_regions.get (key))
    1527              :     return reg;
    1528              : 
    1529        13412 :   field_region *field_reg
    1530        13412 :     = new field_region (alloc_node_id (), *parent, field);
    1531        13412 :   m_field_regions.put (key, field_reg);
    1532        13412 :   return field_reg;
    1533              : }
    1534              : 
    1535              : /* Return the region that describes accessing the element of type
    1536              :    ELEMENT_TYPE at index INDEX of PARENT, creating it if necessary.  */
    1537              : 
    1538              : const region *
    1539        29869 : region_model_manager::get_element_region (const region *parent,
    1540              :                                           tree element_type,
    1541              :                                           const svalue *index)
    1542              : {
    1543              :   /* (UNKNOWN_PTR[IDX]) is (UNKNOWN_PTR).  */
    1544        29869 :   if (parent->symbolic_for_unknown_ptr_p ())
    1545           36 :     return get_unknown_symbolic_region (element_type);
    1546              : 
    1547        29833 :   const element_region::key_t key (parent, element_type, index);
    1548        53255 :   if (element_region *reg = m_element_regions.get (key))
    1549              :     return reg;
    1550              : 
    1551         6411 :   element_region *element_reg
    1552         6411 :     = new element_region (alloc_node_id (), *parent, element_type, index);
    1553         6411 :   m_element_regions.put (key, element_reg);
    1554         6411 :   return element_reg;
    1555              : }
    1556              : 
    1557              : /* Return the region that describes accessing the subregion of type
    1558              :    ELEMENT_TYPE at offset BYTE_OFFSET within PARENT, creating it if
    1559              :    necessary.  */
    1560              : 
    1561              : const region *
    1562       111406 : region_model_manager::get_offset_region (const region *parent,
    1563              :                                          tree type,
    1564              :                                          const svalue *byte_offset)
    1565              : {
    1566              :   /* (UNKNOWN_PTR + OFFSET) is (UNKNOWN_PTR).  */
    1567       111621 :   if (parent->symbolic_for_unknown_ptr_p ())
    1568         5694 :     return get_unknown_symbolic_region (type);
    1569              : 
    1570              :   /* If BYTE_OFFSET is zero, return PARENT.  */
    1571       105927 :   if (tree cst_offset = byte_offset->maybe_get_constant ())
    1572        93568 :     if (zerop (cst_offset))
    1573        73591 :       return get_cast_region (parent, type);
    1574              : 
    1575              :   /* Fold OFFSET_REGION(OFFSET_REGION(REG, X), Y)
    1576              :      to   OFFSET_REGION(REG, (X + Y)).  */
    1577        64672 :   if (const offset_region *parent_offset_reg
    1578        32336 :         = parent->dyn_cast_offset_region ())
    1579              :     {
    1580          215 :       const svalue *sval_x = parent_offset_reg->get_byte_offset ();
    1581          215 :       const svalue *sval_sum
    1582          215 :         = get_or_create_binop (byte_offset->get_type (),
    1583              :                                POINTER_PLUS_EXPR, sval_x, byte_offset);
    1584          215 :       return get_offset_region (parent->get_parent_region (), type, sval_sum);
    1585              :     }
    1586              : 
    1587        32121 :   const offset_region::key_t key (parent, type, byte_offset);
    1588        57659 :   if (offset_region *reg = m_offset_regions.get (key))
    1589              :     return reg;
    1590              : 
    1591         6583 :   offset_region *offset_reg
    1592         6583 :     = new offset_region (alloc_node_id (), *parent, type, byte_offset);
    1593         6583 :   m_offset_regions.put (key, offset_reg);
    1594         6583 :   return offset_reg;
    1595              : }
    1596              : 
    1597              : /* Fold sized_region[TYPE T1]( region[TYPE T2])
    1598              :    --> field_region[TYPE T1](region[TYPE T2])
    1599              :    if T2 is a RECORD_TYPE, the size is that of the record's first field, and
    1600              :    T1 is the type of that first field.
    1601              :    Also fold if there's an intermediate cast to NULL_TYPE.  */
    1602              : 
    1603              : const region *
    1604        36174 : region_model_manager::
    1605              : maybe_fold_sized_region_to_initial_field (const region *parent,
    1606              :                                           tree type,
    1607              :                                           const svalue *byte_size_sval)
    1608              : {
    1609        36174 :   if (!type)
    1610              :     return nullptr;
    1611        12282 :   tree cst_byte_size = byte_size_sval->maybe_get_constant ();
    1612        12282 :   if (!cst_byte_size)
    1613              :     return nullptr;
    1614        12279 :   const region *inner = parent;
    1615        12279 :   if (auto *cast = inner->dyn_cast_cast_region ())
    1616           36 :     if (cast->get_type () == NULL_TREE)
    1617           36 :       inner = cast->get_parent_region ();
    1618        12279 :   if (!inner->get_type ())
    1619              :     return nullptr;
    1620           51 :   if (TREE_CODE (inner->get_type ()) != RECORD_TYPE)
    1621              :     return nullptr;
    1622            5 :   record_layout layout (inner->get_type ()); // TODO: amortize this?
    1623            5 :   auto *initial_item = layout.get_item_at (0_bits);
    1624            5 :   if (!initial_item)
    1625              :     return nullptr;
    1626            5 :   if (!initial_item->m_bit_range.get_start_offset ().is_zero_p ())
    1627              :     return nullptr;
    1628            5 :   concrete_byte_size conc_byte_size_item;
    1629            5 :   if (!maybe_bytes_from_bits (initial_item->m_bit_range.get_size (),
    1630              :                               &conc_byte_size_item))
    1631              :     return nullptr;
    1632            5 :   concrete_byte_size conc_byte_size_from_sval
    1633            5 :     (convert_from_integer_cst (cst_byte_size));
    1634            5 :   if (conc_byte_size_item != conc_byte_size_from_sval)
    1635              :     return nullptr;
    1636              : 
    1637            2 :   return get_cast_region
    1638            2 :     (get_field_region (inner,
    1639            2 :                        const_cast<tree> (initial_item->m_field)),
    1640            2 :      type);
    1641           15 : }
    1642              : 
    1643              : /* Return the region that describes accessing the subregion of type
    1644              :    TYPE of size BYTE_SIZE_SVAL within PARENT, creating it if necessary.  */
    1645              : 
    1646              : const region *
    1647        36689 : region_model_manager::get_sized_region (const region *parent,
    1648              :                                         tree type,
    1649              :                                         const svalue *byte_size_sval)
    1650              : {
    1651        36689 :   if (parent->symbolic_for_unknown_ptr_p ())
    1652           56 :     return get_unknown_symbolic_region (type);
    1653              : 
    1654        36633 :   if (byte_size_sval->get_type () != size_type_node)
    1655         1635 :     byte_size_sval = get_or_create_cast (size_type_node, byte_size_sval);
    1656              : 
    1657              :   /* If PARENT is already that size, return it.  */
    1658        36633 :   const svalue *parent_byte_size_sval = parent->get_byte_size_sval (this);
    1659        36633 :   if (tree parent_size_cst = parent_byte_size_sval->maybe_get_constant ())
    1660          808 :     if (tree size_cst = byte_size_sval->maybe_get_constant ())
    1661              :       {
    1662          728 :         tree comparison
    1663          728 :           = fold_binary (EQ_EXPR, boolean_type_node, parent_size_cst, size_cst);
    1664          728 :         if (comparison == boolean_true_node)
    1665              :           return parent;
    1666              :       }
    1667              : 
    1668        36174 :   if (auto *folded = maybe_fold_sized_region_to_initial_field (parent,
    1669              :                                                                type,
    1670              :                                                                byte_size_sval))
    1671              :     return folded;
    1672              : 
    1673        36172 :   const sized_region::key_t key (parent, type, byte_size_sval);
    1674        61881 :   if (sized_region *reg = m_sized_regions.get (key))
    1675              :     return reg;
    1676              : 
    1677        10463 :   sized_region *sized_reg
    1678        10463 :     = new sized_region (alloc_node_id (), *parent, type, byte_size_sval);
    1679        10463 :   m_sized_regions.put (key, sized_reg);
    1680        10463 :   return sized_reg;
    1681              : }
    1682              : 
    1683              : /* Return the region that describes accessing PARENT_REGION as if
    1684              :    it were of type TYPE, creating it if necessary.  */
    1685              : 
    1686              : const region *
    1687        78620 : region_model_manager::get_cast_region (const region *original_region,
    1688              :                                        tree type)
    1689              : {
    1690              :   /* If types match, return ORIGINAL_REGION.  */
    1691        78620 :   if (type == original_region->get_type ())
    1692              :     return original_region;
    1693              : 
    1694        29654 :   if (original_region->symbolic_for_unknown_ptr_p ())
    1695           53 :     return get_unknown_symbolic_region (type);
    1696              : 
    1697              :   /* Fold
    1698              :        cast_region<T1>(symbolic_region(sval<(T2 *)>()))
    1699              :        --> symbolic_region(cast_sval<T1 *> (sval<(T2 *)>)).  */
    1700        29601 :   if (auto *sym_reg = original_region->dyn_cast_symbolic_region ())
    1701              :     {
    1702         6903 :       auto *ptr_sval = sym_reg->get_pointer ();
    1703         6903 :       if (type
    1704         4467 :           && ptr_sval->get_type ()
    1705        11370 :           && POINTER_TYPE_P (ptr_sval->get_type ()))
    1706              :         {
    1707         4467 :           if (auto *sym_ptr = ptr_sval->maybe_get_symbolic_pointer ())
    1708              :             {
    1709           94 :               tree ptr_to_type = build_pointer_type (type);
    1710           94 :               auto *cast_ptr_sval = get_ptr_svalue (ptr_to_type, *sym_ptr);
    1711           94 :               return get_symbolic_region (cast_ptr_sval);
    1712              :             }
    1713              :         }
    1714              :     }
    1715              : 
    1716        29507 :   const cast_region::key_t key (original_region, type);
    1717        52980 :   if (cast_region *reg = m_cast_regions.get (key))
    1718              :     return reg;
    1719              : 
    1720         6034 :   cast_region *cast_reg
    1721         6034 :     = new cast_region (alloc_node_id (), *original_region, type);
    1722         6034 :   m_cast_regions.put (key, cast_reg);
    1723         6034 :   return cast_reg;
    1724              : }
    1725              : 
    1726              : /* Return the frame_region for call to FUN from CALLING_FRAME, creating it
    1727              :    if necessary.  CALLING_FRAME may be nullptr.  */
    1728              : 
    1729              : const frame_region *
    1730        42017 : region_model_manager::get_frame_region (const frame_region *calling_frame,
    1731              :                                         const function &fun)
    1732              : {
    1733        42017 :   int index = calling_frame ? calling_frame->get_index () + 1 : 0;
    1734              : 
    1735        42017 :   const frame_region::key_t key (calling_frame, fun);
    1736        65068 :   if (frame_region *reg = m_frame_regions.get (key))
    1737              :     return reg;
    1738              : 
    1739        18966 :   frame_region *frame_reg
    1740              :     = new frame_region (alloc_node_id (), m_stack_region, calling_frame,
    1741        18966 :                         fun, index);
    1742        18966 :   m_frame_regions.put (key, frame_reg);
    1743        18966 :   return frame_reg;
    1744              : }
    1745              : 
    1746              : const region *
    1747       122541 : region_model_manager::maybe_fold_symbolic_region (const svalue *sval)
    1748              : {
    1749       122541 :   auto *sym_ptr = sval->maybe_get_symbolic_pointer ();
    1750       122541 :   if (!sym_ptr)
    1751              :     return nullptr;
    1752        48598 :   auto &base_reg = sym_ptr->get_base_region ();
    1753        48598 :   if (sym_ptr->zero_offset_p ())
    1754        36126 :     return &base_reg;
    1755              :   else
    1756              :     {
    1757              :       /* The non-zero offset case.  */
    1758        12472 :       if (!sval->get_type ())
    1759              :         return nullptr;
    1760              : 
    1761        12472 :       tree pointed_to_type = TREE_TYPE (sval->get_type ());
    1762        12472 :       concrete_bit_size type_conc_bit_size;
    1763        12472 :       if (!get_type_size (pointed_to_type, &type_conc_bit_size))
    1764              :         return nullptr;
    1765        12152 :       concrete_byte_size type_conc_byte_size;
    1766        12152 :       if (!maybe_bytes_from_bits (type_conc_bit_size, &type_conc_byte_size))
    1767              :         return nullptr;
    1768        12152 :       auto *byte_offset_sval = get_svalue_from_scalar<concrete_byte_offset>
    1769        24304 :         (ptrdiff_type_node,
    1770        12152 :          sym_ptr->get_byte_offset ());
    1771        12152 :       auto *offset_reg = get_offset_region (&base_reg,
    1772              :                                             NULL_TREE,
    1773              :                                             byte_offset_sval);
    1774        12152 :       auto &byte_mgr = get_node_manager ().get_scalars ().get_bytes ();
    1775        12152 :       auto *byte_size_sval = get_svalue_from_scalar<concrete_byte_offset>
    1776        12152 :         (size_type_node,
    1777        12152 :          byte_mgr.get_concrete (type_conc_byte_size));
    1778        12152 :       return get_sized_region (offset_reg,
    1779              :                                pointed_to_type,
    1780        12152 :                                byte_size_sval);
    1781        24624 :     }
    1782              : }
    1783              : 
    1784              : /* Return the region that describes dereferencing SVAL, creating it
    1785              :    if necessary.  */
    1786              : 
    1787              : const region *
    1788       122541 : region_model_manager::get_symbolic_region (const svalue *sval)
    1789              : {
    1790       122541 :   if (auto *folded = maybe_fold_symbolic_region (sval))
    1791              :     return folded;
    1792              : 
    1793        74263 :   const symbolic_region::key_t key (&m_root_region, sval);
    1794       138625 :   if (symbolic_region *reg = m_symbolic_regions.get (key))
    1795              :     return reg;
    1796              : 
    1797         9901 :   symbolic_region *symbolic_reg
    1798         9901 :     = new symbolic_region (alloc_node_id (), m_root_region, sval);
    1799         9901 :   m_symbolic_regions.put (key, symbolic_reg);
    1800         9901 :   return symbolic_reg;
    1801              : }
    1802              : 
    1803              : /* Return the region that describes accessing STRING_CST, creating it
    1804              :    if necessary.  */
    1805              : 
    1806              : const string_region *
    1807        13873 : region_model_manager::get_region_for_string (tree string_cst)
    1808              : {
    1809        13873 :   gcc_assert (TREE_CODE (string_cst) == STRING_CST);
    1810              : 
    1811        13873 :   auto iter = m_string_map.find (string_cst);
    1812        13873 :   if (iter != m_string_map.end ())
    1813         9943 :     return iter->second;
    1814              : 
    1815         3930 :   string_region *reg
    1816         3930 :     = new string_region (alloc_node_id (), m_read_only_region, string_cst);
    1817         3930 :   m_string_map.insert ({string_cst, reg});
    1818         3930 :   return reg;
    1819              : }
    1820              : 
    1821              : /* Return the region that describes accessing BITS within PARENT as TYPE,
    1822              :    creating it if necessary.  */
    1823              : 
    1824              : const region *
    1825          920 : region_model_manager::get_bit_range (const region *parent, tree type,
    1826              :                                      const concrete_bit_range &bits)
    1827              : {
    1828          920 :   gcc_assert (parent);
    1829              : 
    1830          920 :   if (parent->symbolic_for_unknown_ptr_p ())
    1831            0 :     return get_unknown_symbolic_region (type);
    1832              : 
    1833          920 :   const bit_range_region::key_t key (parent, type, bits);
    1834         1389 :   if (bit_range_region *reg = m_bit_range_regions.get (key))
    1835              :     return reg;
    1836              : 
    1837          451 :   bit_range_region *bit_range_reg
    1838          451 :     = new bit_range_region (alloc_node_id (), *parent, type, bits);
    1839          451 :   m_bit_range_regions.put (key, bit_range_reg);
    1840          451 :   return bit_range_reg;
    1841          920 : }
    1842              : 
    1843              : /* Return the region that describes accessing the IDX-th variadic argument
    1844              :    within PARENT_FRAME, creating it if necessary.  */
    1845              : 
    1846              : const var_arg_region *
    1847         1310 : region_model_manager::get_var_arg_region (const frame_region *parent_frame,
    1848              :                                           unsigned idx)
    1849              : {
    1850         1310 :   gcc_assert (parent_frame);
    1851              : 
    1852         1310 :   const var_arg_region::key_t key (parent_frame, idx);
    1853         2124 :   if (var_arg_region *reg = m_var_arg_regions.get (key))
    1854              :     return reg;
    1855              : 
    1856          496 :   var_arg_region *var_arg_reg
    1857          496 :     = new var_arg_region (alloc_node_id (), *parent_frame, idx);
    1858          496 :   m_var_arg_regions.put (key, var_arg_reg);
    1859          496 :   return var_arg_reg;
    1860              : }
    1861              : 
    1862              : /* If we see a tree code we don't know how to handle, rather than
    1863              :    ICE or generate bogus results, create a dummy region, and notify
    1864              :    CTXT so that it can mark the new state as being not properly
    1865              :    modelled.  The exploded graph can then stop exploring that path,
    1866              :    since any diagnostics we might issue will have questionable
    1867              :    validity.  */
    1868              : 
    1869              : const region *
    1870          122 : region_model_manager::
    1871              : get_region_for_unexpected_tree_code (region_model_context *ctxt,
    1872              :                                      tree t,
    1873              :                                      const dump_location_t &loc)
    1874              : {
    1875          122 :   tree type = TYPE_P (t) ? t : TREE_TYPE (t);
    1876          122 :   region *new_reg
    1877          122 :     = new unknown_region (alloc_node_id (), m_root_region, type);
    1878          122 :   if (ctxt)
    1879           52 :     ctxt->on_unexpected_tree_code (t, loc);
    1880          122 :   return new_reg;
    1881              : }
    1882              : 
    1883              : /* Return a region describing a heap-allocated block of memory.
    1884              :    Reuse an existing heap_allocated_region is its id is not within
    1885              :    BASE_REGS_IN_USE.  */
    1886              : 
    1887              : const region *
    1888        19052 : region_model_manager::
    1889              : get_or_create_region_for_heap_alloc (const bitmap &base_regs_in_use)
    1890              : {
    1891              :   /* Try to reuse an existing region, if it's unreferenced in the
    1892              :      client state.  */
    1893        65032 :   for (auto existing_reg : m_managed_dynamic_regions)
    1894        27714 :     if (!bitmap_bit_p (base_regs_in_use, existing_reg->get_id ()))
    1895        17328 :       if (existing_reg->get_kind () == RK_HEAP_ALLOCATED)
    1896              :         return existing_reg;
    1897              : 
    1898              :   /* All existing ones (if any) are in use; create a new one.  */
    1899         1826 :   region *reg
    1900         1826 :     = new heap_allocated_region (alloc_node_id (), m_heap_region);
    1901         1826 :   m_managed_dynamic_regions.safe_push (reg);
    1902         1826 :   return reg;
    1903              : }
    1904              : 
    1905              : /* Return a new region describing a block of memory allocated within FRAME.  */
    1906              : 
    1907              : const region *
    1908         1151 : region_model_manager::create_region_for_alloca (const frame_region *frame)
    1909              : {
    1910         1151 :   gcc_assert (frame);
    1911         1151 :   region *reg = new alloca_region (alloc_node_id (), *frame);
    1912         1151 :   m_managed_dynamic_regions.safe_push (reg);
    1913         1151 :   return reg;
    1914              : }
    1915              : 
    1916              : /* Log OBJ to LOGGER.  */
    1917              : 
    1918              : template <typename T>
    1919              : static void
    1920         1458 : log_managed_object (logger *logger, const T *obj)
    1921              : {
    1922         1458 :   logger->start_log_line ();
    1923         1458 :   pretty_printer *pp = logger->get_printer ();
    1924         1458 :   pp_string (pp, "    ");
    1925         1458 :   obj->dump_to_pp (pp, true);
    1926         1458 :   logger->end_log_line ();
    1927         1458 : }
    1928              : 
    1929              : /* Specialization for frame_region, which also logs the count of locals
    1930              :    managed by the frame_region.  */
    1931              : 
    1932              : template <>
    1933              : void
    1934            6 : log_managed_object (logger *logger, const frame_region *obj)
    1935              : {
    1936            6 :   logger->start_log_line ();
    1937            6 :   pretty_printer *pp = logger->get_printer ();
    1938            6 :   pp_string (pp, "    ");
    1939            6 :   obj->dump_to_pp (pp, true);
    1940            6 :   pp_printf (pp, " [with %i region(s) for locals]", obj->get_num_locals ());
    1941            6 :   logger->end_log_line ();
    1942            6 : }
    1943              : 
    1944              : /* Dump the number of objects that were managed by UNIQ_MAP to LOGGER.
    1945              :    If SHOW_OBJS is true, also dump the objects themselves.  */
    1946              : 
    1947              : template <typename K, typename T>
    1948              : static void
    1949              : log_uniq_map (logger *logger, bool show_objs, const char *title,
    1950              :               const hash_map<K, T*> &uniq_map)
    1951              : {
    1952              :   logger->log ("  # %s: %li", title, (long)uniq_map.elements ());
    1953              :   if (!show_objs)
    1954              :     return;
    1955              :   auto_vec<const T *> vec_objs (uniq_map.elements ());
    1956              :   for (typename hash_map<K, T*>::iterator iter = uniq_map.begin ();
    1957              :        iter != uniq_map.end (); ++iter)
    1958              :     vec_objs.quick_push ((*iter).second);
    1959              : 
    1960              :   vec_objs.qsort (T::cmp_ptr_ptr);
    1961              : 
    1962              :   unsigned i;
    1963              :   const T *obj;
    1964              :   FOR_EACH_VEC_ELT (vec_objs, i, obj)
    1965              :     log_managed_object<T> (logger, obj);
    1966              : }
    1967              : 
    1968              : /* Dump the number of objects that were managed by MAP to LOGGER.
    1969              :    If SHOW_OBJS is true, also dump the objects themselves.  */
    1970              : 
    1971              : template <typename K, typename T>
    1972              : static void
    1973           85 : log_uniq_map (logger *logger, bool show_objs, const char *title,
    1974              :               const std::map<K, T*> &map)
    1975              : {
    1976           85 :   logger->log ("  # %s: %li", title, (long)map.size ());
    1977           85 :   if (!show_objs)
    1978              :     return;
    1979              : 
    1980         1504 :   for (auto &iter : map)
    1981         1419 :     log_managed_object<T> (logger, iter.second);
    1982              : }
    1983              : 
    1984              : template <typename T>
    1985              : static void
    1986           45 : log_uniq_map (logger *logger, bool show_objs, const char *title,
    1987              :               const consolidation_map<T> &map)
    1988              : {
    1989           45 :   logger->log ("  # %s: %li", title, (long)map.elements ());
    1990           45 :   if (!show_objs)
    1991            0 :     return;
    1992              : 
    1993           45 :   auto_vec<const T *> vec_objs (map.elements ());
    1994           45 :   for (typename consolidation_map<T>::iterator iter = map.begin ();
    1995          132 :        iter != map.end (); ++iter)
    1996           42 :     vec_objs.quick_push ((*iter).second);
    1997              : 
    1998           45 :   vec_objs.qsort (T::cmp_ptr_ptr);
    1999              : 
    2000              :   unsigned i;
    2001              :   const T *obj;
    2002          103 :   FOR_EACH_VEC_ELT (vec_objs, i, obj)
    2003           42 :     log_managed_object<T> (logger, obj);
    2004           45 : }
    2005              : 
    2006              : /* Dump the number of objects of each class that were managed by this
    2007              :    manager to LOGGER.
    2008              :    If SHOW_OBJS is true, also dump the objects themselves.  */
    2009              : 
    2010              : void
    2011            5 : region_model_manager::log_stats (logger *logger, bool show_objs) const
    2012              : {
    2013            5 :   LOG_SCOPE (logger);
    2014            5 :   logger->log ("call string consolidation");
    2015            5 :   m_empty_call_string.recursive_log (logger);
    2016            5 :   logger->log ("svalue consolidation");
    2017            5 :   log_uniq_map (logger, show_objs, "constant_svalue", m_constants_map);
    2018            5 :   log_uniq_map (logger, show_objs, "unknown_svalue", m_unknowns_map);
    2019            5 :   if (m_unknown_NULL)
    2020            3 :     log_managed_object (logger, m_unknown_NULL);
    2021            5 :   log_uniq_map (logger, show_objs, "poisoned_svalue", m_poisoned_values_map);
    2022            5 :   log_uniq_map (logger, show_objs, "setjmp_svalue", m_setjmp_values_map);
    2023            5 :   log_uniq_map (logger, show_objs, "initial_svalue", m_initial_values_map);
    2024            5 :   log_uniq_map (logger, show_objs, "pointer_svalue", m_pointer_values_map);
    2025            5 :   log_uniq_map (logger, show_objs, "unaryop_svalue", m_unaryop_values_map);
    2026            5 :   log_uniq_map (logger, show_objs, "binop_svalue", m_binop_values_map);
    2027            5 :   log_uniq_map (logger, show_objs, "svalue_from_bit_vector", m_bit_vector_to_svalue_map);
    2028            5 :   log_uniq_map (logger, show_objs, "unmergeable_svalue",
    2029            5 :                 m_unmergeable_values_map);
    2030            5 :   log_uniq_map (logger, show_objs, "widening_svalue", m_widening_values_map);
    2031            5 :   log_uniq_map (logger, show_objs, "asm_output_svalue",
    2032            5 :                 m_asm_output_values_map);
    2033            5 :   log_uniq_map (logger, show_objs, "const_fn_result_svalue",
    2034            5 :                 m_const_fn_result_values_map);
    2035              : 
    2036            5 :   logger->log ("max accepted svalue num_nodes: %i",
    2037            5 :                m_max_complexity.m_num_nodes);
    2038            5 :   logger->log ("max accepted svalue max_depth: %i",
    2039            5 :                m_max_complexity.m_max_depth);
    2040              : 
    2041            5 :   logger->log ("region consolidation");
    2042            5 :   log_uniq_map (logger, show_objs, "function_region", m_fndecls_map);
    2043            5 :   log_uniq_map (logger, show_objs, "label_region", m_labels_map);
    2044            5 :   log_uniq_map (logger, show_objs, "decl_region for globals", m_globals_map);
    2045            5 :   log_uniq_map (logger, show_objs, "field_region", m_field_regions);
    2046            5 :   log_uniq_map (logger, show_objs, "element_region", m_element_regions);
    2047            5 :   log_uniq_map (logger, show_objs, "offset_region", m_offset_regions);
    2048            5 :   log_uniq_map (logger, show_objs, "sized_region", m_sized_regions);
    2049            5 :   log_uniq_map (logger, show_objs, "cast_region", m_cast_regions);
    2050            5 :   log_uniq_map (logger, show_objs, "frame_region", m_frame_regions);
    2051            5 :   log_uniq_map (logger, show_objs, "symbolic_region", m_symbolic_regions);
    2052            5 :   log_uniq_map (logger, show_objs, "string_region", m_string_map);
    2053            5 :   log_uniq_map (logger, show_objs, "bit_range_region", m_bit_range_regions);
    2054            5 :   log_uniq_map (logger, show_objs, "var_arg_region", m_var_arg_regions);
    2055            5 :   logger->log ("  # managed dynamic regions: %i",
    2056              :                m_managed_dynamic_regions.length ());
    2057            5 :   m_range_mgr->log_stats (logger, show_objs);
    2058            5 : }
    2059              : 
    2060              : /* Emit a warning showing DECL_REG->tracked_p () for use in DejaGnu tests
    2061              :    (using -fdump-analyzer-untracked).  */
    2062              : 
    2063              : static void
    2064          162 : dump_untracked_region (const decl_region *decl_reg)
    2065              : {
    2066          162 :   tree decl = decl_reg->get_decl ();
    2067          162 :   if (TREE_CODE (decl) != VAR_DECL)
    2068              :     return;
    2069              :   /* For now, don't emit the status of decls in the constant pool, to avoid
    2070              :      differences in DejaGnu test results between targets that use these vs
    2071              :      those that don't.
    2072              :      (Eventually these decls should probably be untracked and we should test
    2073              :      for that, but that's not stage 4 material).  */
    2074           56 :   if (DECL_IN_CONSTANT_POOL (decl))
    2075              :     return;
    2076           85 :   warning_at (DECL_SOURCE_LOCATION (decl), 0,
    2077              :               "track %qD: %s",
    2078           56 :               decl, (decl_reg->tracked_p () ? "yes" : "no"));
    2079              : }
    2080              : 
    2081              : /* Implementation of -fdump-analyzer-untracked.  */
    2082              : 
    2083              : void
    2084           23 : region_model_manager::dump_untracked_regions () const
    2085              : {
    2086           74 :   for (auto iter : m_globals_map)
    2087              :     {
    2088           51 :       const decl_region *decl_reg = iter.second;
    2089           51 :       dump_untracked_region (decl_reg);
    2090              :     }
    2091           75 :   for (auto frame_iter : m_frame_regions)
    2092              :     {
    2093           52 :       const frame_region *frame_reg = frame_iter.second;
    2094           52 :       frame_reg->dump_untracked_regions ();
    2095              :     }
    2096           23 : }
    2097              : 
    2098              : void
    2099           52 : frame_region::dump_untracked_regions () const
    2100              : {
    2101          163 :   for (auto iter : m_locals)
    2102              :     {
    2103          111 :       const decl_region *decl_reg = iter.second;
    2104          111 :       dump_untracked_region (decl_reg);
    2105              :     }
    2106           52 : }
    2107              : 
    2108              : #if CHECKING_P
    2109              : 
    2110              : namespace selftest {
    2111              : 
    2112              : static void
    2113            4 : test_svalue_from_bit_vector ()
    2114              : {
    2115            4 :   symbolic::node_manager node_mgr;
    2116            4 :   auto &bv_mgr = node_mgr.get_bit_vector_manager ();
    2117            4 :   auto &rmm = node_mgr.get_region_model_manager ();
    2118              : 
    2119              :   /* Folding the bits of a constant.  */
    2120            4 :   ASSERT_SAME_SYMBOLIC_NODE
    2121              :     (*rmm.get_svalue_from_bit_vector (char_type_node,
    2122              :                                       bv_mgr.get_all_zeros (1_byte)),
    2123              :      *rmm.get_or_create_int_cst (char_type_node, 0));
    2124              : 
    2125              :   /* Round-tripping an svalue through a bit_vector.  */
    2126            4 :   {
    2127            4 :     placeholder_svalue x (node_mgr.alloc_node_id (),
    2128            4 :                           integer_type_node, "x");
    2129            4 :     auto &bv_from_x = bv_mgr.get_bit_vector_from_svalue (x);
    2130              :     /* With matching types: SVAL_FROM_BITS(typeof(X), BITS_FROM_SVAL(X))
    2131              :        should fold to just X.  */
    2132            4 :     ASSERT_SAME_SYMBOLIC_NODE
    2133              :       (*rmm.get_svalue_from_bit_vector (integer_type_node, bv_from_x),
    2134              :        x);
    2135              : 
    2136              :     /* With different types of the same size,
    2137              :        SVAL_FROM_BITS(OTHER_TYPE, BITS_FROM_SVAL(X))
    2138              :        should fold to CAST_SVAL (OTHER_TYPE, X).  */
    2139            4 :     ASSERT_SAME_SYMBOLIC_NODE
    2140              :       (*rmm.get_svalue_from_bit_vector (unsigned_type_node, bv_from_x),
    2141              :        *rmm.get_or_create_cast (unsigned_type_node, &x));
    2142            4 :   }
    2143              : 
    2144              :   /* Round-tripping a _Bool cropped to its initial bit
    2145              :      (as if read from a region with 1-bit precision).  */
    2146            4 :   {
    2147            4 :     placeholder_svalue b (node_mgr.alloc_node_id (), boolean_type_node, "b");
    2148            4 :     auto &bv = bv_mgr.get_bit_vector_from_svalue (b);
    2149            4 :     auto &cropped_bv
    2150           12 :       = bv.get_cropped_bits (concrete_bit_range (0_bits, 1_bit));
    2151            4 :     auto *sval
    2152            4 :       = rmm.get_svalue_from_bit_vector (boolean_type_node, cropped_bv);
    2153            4 :     ASSERT_SAME_SYMBOLIC_NODE (b, *sval);
    2154            4 :   }
    2155            4 : }
    2156              : 
    2157              : /* Run all of the selftests within this file.  */
    2158              : 
    2159              : void
    2160            4 : analyzer_region_model_manager_cc_tests ()
    2161              : {
    2162            4 :   test_svalue_from_bit_vector ();
    2163            4 : }
    2164              : 
    2165              : } // namespace selftest
    2166              : 
    2167              : #endif /* CHECKING_P */
    2168              : 
    2169              : } // namespace ana
    2170              : 
    2171              : #endif /* #if ENABLE_ANALYZER */
        

Generated by: LCOV version 2.6-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.