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

Generated by: LCOV version 2.5.0-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.