LCOV - code coverage report
Current view: top level - gcc/analyzer - symbolic-scalars.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 89.0 % 1180 1050
Test Date: 2026-12-05 16:10:02 Functions: 83.3 % 258 215
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Unit-safe symbolic scalar values (e.g. "n * 32 bytes")
       2              :    Copyright (C) 2019-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 "selftest.h"
      24              : 
      25              : #include "diagnostics/markup.h"
      26              : 
      27              : #include "analyzer/region-model-manager.h"
      28              : #include "analyzer/symbolic-scalars.h"
      29              : #include "analyzer/region-model.h"
      30              : 
      31              : #if ENABLE_ANALYZER
      32              : 
      33              : namespace ana {
      34              : namespace symbolic {
      35              : 
      36              : // template<typename ConcreteValueType> class scalar : public symbolic::node
      37              : 
      38              : template<typename ConcreteValueType>
      39              : bool
      40     18628258 : scalar<ConcreteValueType>::is_zero_p () const
      41              : {
      42     18628258 :   if (auto concrete = dyn_cast_concrete_scalar ())
      43     18362558 :     return concrete->get_concrete ().is_zero_p ();
      44              :   return false;
      45              : }
      46              : 
      47              : template
      48              : bool
      49              : scalar<integer_with_unit<units::bit>>::is_zero_p () const;
      50              : 
      51              : template
      52              : bool
      53              : scalar<integer_with_unit<units::byte>>::is_zero_p () const;
      54              : 
      55              : template
      56              : bool
      57              : scalar<integer>::is_zero_p () const;
      58              : 
      59              : template<typename ConcreteValueType>
      60              : bool
      61        11528 : scalar<ConcreteValueType>::is_one_p () const
      62              : {
      63        11528 :   if (auto concrete = dyn_cast_concrete_scalar ())
      64         9020 :     return concrete->get_concrete ().is_one_p ();
      65              :   return false;
      66              : }
      67              : 
      68              : template
      69              : bool
      70              : scalar<integer_with_unit<units::bit>>::is_one_p () const;
      71              : 
      72              : template
      73              : bool
      74              : scalar<integer_with_unit<units::byte>>::is_one_p () const;
      75              : 
      76              : template
      77              : bool
      78              : scalar<integer>::is_one_p () const;
      79              : 
      80              : template<typename ConcreteValueType>
      81              : bool
      82        58572 : scalar<ConcreteValueType>::is_unknown_p () const
      83              : {
      84        58572 :   if (auto *from_sval = dyn_cast_scalar_from_svalue ())
      85        14509 :     return from_sval->get_svalue ().get_kind () == SK_UNKNOWN;
      86              :   return false;
      87              : }
      88              : 
      89              : template
      90              : bool
      91              : scalar<integer_with_unit<units::bit>>::is_unknown_p () const;
      92              : 
      93              : template<typename ConcreteValueType>
      94              : void
      95         3660 : scalar<ConcreteValueType>::print_dump_widget_label (pretty_printer *pp) const
      96              : {
      97         3660 :   start_any_dump_color (pp);
      98         3660 :   print_dump_widget_label_for_scalar (pp);
      99         3660 :   end_any_dump_color (pp);
     100         3660 : }
     101              : 
     102              : template<>
     103              : std::unique_ptr<json::object>
     104           20 : scalar<integer>::make_json_obj (const char *type_name) const
     105              : {
     106           20 :   return value_node::make_json_obj (type_name);
     107              : }
     108              : 
     109              : template<>
     110              : std::unique_ptr<json::object>
     111          232 : scalar<integer_with_unit<units::bit>>::make_json_obj (const char *type_name) const
     112              : {
     113          232 :   auto obj = value_node::make_json_obj (type_name);
     114          232 :   obj->set_string ("unit", "bit");
     115          232 :   return obj;
     116              : }
     117              : 
     118              : template<>
     119              : std::unique_ptr<json::object>
     120          129 : scalar<integer_with_unit<units::byte>>::make_json_obj (const char *type_name) const
     121              : {
     122          129 :   auto obj = value_node::make_json_obj (type_name);
     123          129 :   obj->set_string ("unit", "byte");
     124          129 :   return obj;
     125              : }
     126              : 
     127              : /* A total ordering on scalars, which attempts to do the right
     128              :    thing where the scalars are directly comparable (using
     129              :    get_partial_ordering), with fallbacks for other cases.  */
     130              : 
     131              : template<typename ConcreteValueType>
     132              : bool
     133         2844 : scalar<ConcreteValueType>::
     134              : get_total_ordering (const scalar<ConcreteValueType> &a,
     135              :                     enum tree_code op,
     136              :                     const scalar<ConcreteValueType> &b,
     137              :                     const region_model *model)
     138              : {
     139         2844 :   tristate partial = get_partial_ordering (a, op, b, model);
     140         2844 :   if (partial.is_known ())
     141         2360 :     return partial.is_true ();
     142              : 
     143          484 :   ConcreteValueType conc_a;
     144          484 :   if (concrete_from_symbolic (a, &conc_a))
     145              :     {
     146          113 :       ConcreteValueType conc_b;
     147          113 :       if (concrete_from_symbolic (b, &conc_b))
     148              :         {
     149              :           /* Concrete vs concrete: should have already been handled
     150              :              by get_partial_ordering.  */
     151            0 :           gcc_unreachable ();
     152              :         }
     153              :       else
     154              :         {
     155              :           /* Concrete vs symbolic: put all concrete before all symbolic.  */
     156          113 :           switch (op)
     157              :             {
     158            0 :             default:
     159            0 :               gcc_unreachable ();
     160              :             case NE_EXPR:
     161              :             case LT_EXPR:
     162              :             case LE_EXPR:
     163              :               return true;
     164           36 :             case EQ_EXPR:
     165              :             case GT_EXPR:
     166              :             case GE_EXPR:
     167           36 :               return false;
     168              :             }
     169              :         }
     170          113 :     }
     171              :   else
     172              :     {
     173          371 :       ConcreteValueType conc_b;
     174          371 :       if (concrete_from_symbolic (b, &conc_b))
     175              :         {
     176              :           /* Symbolic vs concrete: put all symbolic after all concrete.  */
     177          161 :           switch (op)
     178              :             {
     179            0 :             default:
     180            0 :               gcc_unreachable ();
     181              :             case EQ_EXPR:
     182              :             case LT_EXPR:
     183              :             case LE_EXPR:
     184              :               return false;
     185           74 :             case NE_EXPR:
     186              :             case GT_EXPR:
     187              :             case GE_EXPR:
     188           74 :               return true;
     189              :             }
     190              :         }
     191              :       else
     192              :         {
     193              :           /* Symbolic vs symbolic with unknown ordering.
     194              :              Fall back to ID order.  */
     195          210 :           int cmp_ids = symbolic::node::cmp_ids (&a, &b);
     196          210 :           switch (op)
     197              :             {
     198            0 :             default:
     199            0 :               gcc_unreachable ();
     200           12 :             case EQ_EXPR:
     201           12 :               return cmp_ids == 0;
     202           12 :             case NE_EXPR:
     203           12 :               return cmp_ids != 0;
     204           90 :             case LT_EXPR:
     205           90 :               return cmp_ids < 0;
     206           48 :             case LE_EXPR:
     207           48 :               return cmp_ids <= 0;
     208           24 :             case GT_EXPR:
     209           24 :               return cmp_ids > 0;
     210           24 :             case GE_EXPR:
     211           24 :               return cmp_ids >= 0;
     212              :             }
     213              :         }
     214          371 :     }
     215          484 : }
     216              : 
     217              : template<typename ConcreteValueType>
     218              : tristate
     219         4148 : scalar<ConcreteValueType>::
     220              : get_partial_ordering (const scalar<ConcreteValueType> &a,
     221              :                       enum tree_code op,
     222              :                       const scalar<ConcreteValueType> &b,
     223              :                       const region_model *model)
     224              : {
     225         4148 :   ConcreteValueType conc_a;
     226         4148 :   if (concrete_from_symbolic (a, &conc_a))
     227              :     {
     228         2768 :       ConcreteValueType conc_b;
     229         2768 :       if (concrete_from_symbolic (b, &conc_b))
     230              :         {
     231              :           /* Concrete vs concrete.  */
     232         2627 :           switch (op)
     233              :             {
     234            0 :             default:
     235            0 :               gcc_unreachable ();
     236          860 :             case EQ_EXPR:
     237         1116 :               return conc_a == conc_b;
     238           24 :             case NE_EXPR:
     239           36 :               return conc_a != conc_b;
     240          145 :             case LT_EXPR:
     241          201 :               return conc_a < conc_b;
     242         1354 :             case LE_EXPR:
     243         1699 :               return conc_a <= conc_b;
     244           86 :             case GT_EXPR:
     245          144 :               return conc_a > conc_b;
     246          158 :             case GE_EXPR:
     247          220 :               return conc_a >= conc_b;
     248              :             }
     249              :         }
     250              :       else
     251              :         {
     252              :           /* Concrete vs symbolic: can't compare.  */
     253          141 :           return tristate::unknown ();
     254              :         }
     255         2768 :     }
     256              :   else
     257              :     {
     258         1380 :       ConcreteValueType conc_b;
     259         1380 :       if (concrete_from_symbolic (b, &conc_b))
     260              :         {
     261              :           /* Concrete vs symbolic: can't compare.  */
     262          206 :           return tristate::unknown ();
     263              :         }
     264              :       else
     265              :         {
     266              :           /* Symbolic vs symbolic.  */
     267              : 
     268              :           /* Check for identity.  */
     269         1174 :           if (&a == &b)
     270          148 :             switch (op)
     271              :               {
     272            0 :               default:
     273            0 :                 gcc_unreachable ();
     274           54 :               case NE_EXPR:
     275              :               case LT_EXPR:
     276              :               case GT_EXPR:
     277           54 :                 return false;
     278              : 
     279           94 :               case EQ_EXPR:
     280              :               case LE_EXPR:
     281              :               case GE_EXPR:
     282           94 :                 return true;
     283              :               }
     284              : 
     285              :           /* Try taking their difference to see if it simplifies
     286              :              to a concrete value.  */;
     287         1026 :           auto &diff = a - b;
     288         1026 :           ConcreteValueType conc_diff;
     289         1026 :           if (concrete_from_symbolic (diff, &conc_diff))
     290              :             {
     291          514 :               switch (op)
     292              :                 {
     293            0 :                 default:
     294            0 :                   gcc_unreachable ();
     295            8 :                 case EQ_EXPR:
     296           12 :                   return conc_diff.get_integer () == 0;
     297            8 :                 case NE_EXPR:
     298           24 :                   return conc_diff.get_integer () != 0;
     299          347 :                 case LT_EXPR:
     300          355 :                   return conc_diff.get_integer () < 0;
     301           87 :                 case LE_EXPR:
     302          154 :                   return conc_diff.get_integer () <= 0;
     303           24 :                 case GT_EXPR:
     304           36 :                   return conc_diff.get_integer () > 0;
     305           24 :                 case GE_EXPR:
     306           36 :                   return conc_diff.get_integer () >= 0;
     307              :                 }
     308              :             }
     309              : 
     310          512 :           if (auto *bits_from_bytes_a = a.dyn_cast_bits_from_bytes_scalar ())
     311           90 :             if (auto *bits_from_bytes_b = b.dyn_cast_bits_from_bytes_scalar ())
     312              :               return symbolic_byte_size::get_partial_ordering
     313           90 :                 (bits_from_bytes_a->get_bytes (),
     314              :                  op,
     315           90 :                  bits_from_bytes_b->get_bytes (),
     316           90 :                  model);
     317              : 
     318          422 :           if (auto *scalar_from_sval_a = a.dyn_cast_scalar_from_svalue ())
     319          218 :             if (auto *scalar_from_sval_b = b.dyn_cast_scalar_from_svalue ())
     320              :               {
     321              :                 // TODO: would be nice not to need a region_model here
     322          212 :                 auto &node_mgr = a.get_mgr ().get_node_manager ();
     323          212 :                 const region_model placeholder_model (node_mgr);
     324          212 :                 const region_model *tmp_model = model;
     325          212 :                 if (!tmp_model)
     326            0 :                   tmp_model = &placeholder_model;
     327          212 :                 auto ts_sval = tmp_model->eval_condition
     328          212 :                   (&scalar_from_sval_a->get_svalue (),
     329              :                    op,
     330          212 :                    &scalar_from_sval_b->get_svalue ());
     331          212 :                 if (ts_sval.is_known ())
     332           80 :                   return ts_sval;
     333          212 :               }
     334              : 
     335          342 :           return tristate::unknown ();
     336         1026 :         }
     337         1380 :     }
     338         4148 : }
     339              : 
     340              : template
     341              : bool
     342              : symbolic_bit_size::get_total_ordering (const symbolic_bit_size &a,
     343              :                                        enum tree_code op,
     344              :                                        const symbolic_bit_size &b,
     345              :                                        const region_model *model);
     346              : 
     347              : template
     348              : bool
     349              : symbolic_byte_size::get_total_ordering (const symbolic_byte_size &a,
     350              :                                         enum tree_code op,
     351              :                                         const symbolic_byte_size &b,
     352              :                                         const region_model *model);
     353              : 
     354              : // template<typename ConcreteValueType> class concrete_scalar : public scalar<ConcreteValueType>
     355              : 
     356              : template<typename ConcreteValueType>
     357        92501 : concrete_scalar<ConcreteValueType>::
     358              : concrete_scalar (unsigned id,
     359              :                  scalar_manager<ConcreteValueType> &mgr,
     360              :                  const ConcreteValueType &value)
     361        92501 : : scalar<ConcreteValueType> (complexity (1, 1), id, mgr,
     362              :                              scalar<ConcreteValueType>::kind::concrete),
     363        92501 :   m_value (value)
     364              : {
     365              : }
     366              : 
     367              : template<>
     368              : const integer &
     369         2429 : concrete_scalar<concrete_bit_size>::get_integer () const
     370              : {
     371         2429 :   return m_value.get_integer ();
     372              : }
     373              : 
     374              : template<>
     375              : const integer &
     376       118200 : concrete_scalar<concrete_byte_size>::get_integer () const
     377              : {
     378       118200 :   return m_value.get_integer ();
     379              : }
     380              : 
     381              : template<>
     382              : const integer &
     383         2427 : concrete_scalar<integer>::get_integer () const
     384              : {
     385         2427 :   return m_value;
     386              : }
     387              : 
     388              : template<typename ConcreteValueType>
     389              : void
     390         1906 : concrete_scalar<ConcreteValueType>::
     391              : print_dump_widget_label_for_scalar (pretty_printer *pp) const
     392              : {
     393         1906 :   pp_printf (pp, "concrete_scalar<%s>: ", ConcreteValueType::dump_type_name);
     394         1906 :   m_value.dump_to_pp (pp);
     395         1906 : }
     396              : 
     397              : template<typename ConcreteValueType>
     398              : void
     399         1620 : concrete_scalar<ConcreteValueType>::
     400              : add_dump_widget_children (text_art::tree_widget &,
     401              :                           node_dump_state &) const
     402              : {
     403              :     // no-op
     404         1620 : }
     405              : 
     406              : template<>
     407              : void
     408            0 : concrete_scalar<integer>::accept_node_visitor (symbolic::node_visitor &v) const
     409              : {
     410            0 :   v.visit_scalar_unitless (*this);
     411            0 : }
     412              : 
     413              : template<>
     414              : void
     415     27918052 : concrete_scalar<integer_with_unit<units::bit>>::accept_node_visitor (symbolic::node_visitor &v) const
     416              : {
     417     27918052 :   v.visit_scalar_bit (*this);
     418     27918052 : }
     419              : 
     420              : template<>
     421              : void
     422      3881724 : concrete_scalar<integer_with_unit<units::byte>>::
     423              : accept_node_visitor (symbolic::node_visitor &v) const
     424              : {
     425      3881724 :   v.visit_scalar_byte (*this);
     426      3881724 : }
     427              : 
     428              : template<typename ConcreteValueType>
     429              : std::unique_ptr<json::object>
     430          325 : concrete_scalar<ConcreteValueType>::to_json () const
     431              : {
     432          325 :   auto obj = scalar<ConcreteValueType>::make_json_obj ("concrete_scalar");
     433          325 :   obj->set ("value", m_value.to_json ());
     434          325 :   return obj;
     435              : }
     436              : 
     437              : template<typename ConcreteValueType>
     438              : void
     439         3062 : concrete_scalar<ConcreteValueType>::
     440              : dump_inline_to_pp (pretty_printer *pp, enum units::role role) const
     441              : {
     442         3062 :   m_value.dump_to_pp (pp, role);
     443         3054 : }
     444              : 
     445              : template<>
     446              : void
     447            0 : concrete_scalar<integer>::
     448              : dump_inline_to_pp (pretty_printer *pp, enum units::role) const
     449              : {
     450            0 :   m_value.dump_to_pp (pp);
     451            0 : }
     452              : 
     453              : template<typename ConcreteValueType>
     454              : std::unique_ptr<diagnostics::markup::node>
     455           82 : concrete_scalar<ConcreteValueType>::make_markup_node (markup_context &) const
     456              : {
     457           82 :   pretty_printer pp;
     458           82 :   m_value.dump_to_pp (&pp);
     459           82 :   return diagnostics::markup::make_text_node (pp_formatted_text (&pp));
     460           82 : }
     461              : 
     462              : // template<typename ConcreteValueType> class sum : public scalar<ConcreteValueType>
     463              : 
     464              : template<typename ConcreteValueType>
     465              : bool
     466      1320353 : sum<ConcreteValueType>::memoization_key::
     467              : operator< (const memoization_key &other) const
     468              : {
     469      1320353 :   if (m_addends.size () != other.m_addends.size ())
     470         3534 :     return m_addends.size () < other.m_addends.size ();
     471      2434225 :   for (size_t i = 0 ; i < m_addends.size (); ++i)
     472      2047625 :     if (int c = symbolic::node::cmp (*m_addends[i],
     473      2047625 :                                      *other.m_addends[i]))
     474       930219 :       return c < 0;
     475              :   return false;
     476              : }
     477              : 
     478              : template<typename ConcreteValueType>
     479              : void
     480        58787 : sum<ConcreteValueType>::folding_helper::
     481              : add_addend (const scalar<ConcreteValueType> &addend,
     482              :             scalar_manager<integer> &unitless_mgr)
     483              : {
     484              :   /* Accumulate the sum of any concrete terms.  */
     485        58787 :   if (auto concrete = addend.dyn_cast_concrete_scalar ())
     486        46184 :     m_sum_of_concrete_values += concrete->get_concrete ();
     487        12603 :   else if (auto sum = addend.dyn_cast_sum ())
     488              :     {
     489              :       /* Flatten sums of sums.  */
     490         2010 :       for (auto symbolic : sum->m_symbolic_addends)
     491         1089 :         add_addend (*symbolic, unitless_mgr);
     492          921 :       if (auto concrete = sum->m_concrete_addend)
     493          815 :         m_sum_of_concrete_values += concrete->get_concrete ();
     494              :     }
     495        11682 :   else if (auto product = addend.dyn_cast_product ())
     496         1832 :     add_to_coefficient_for_addend (&product->get_lhs (),
     497         1832 :                                    &product->get_rhs ());
     498              :   else
     499         9850 :     add_to_coefficient_for_addend (&addend,
     500        19700 :                                    &unitless_mgr.get_concrete (1));
     501        58787 : }
     502              : 
     503              : /* Update MAP by adding COEFFICIENT_CHANGE to the coefficient for TERM
     504              :    (if any).  */
     505              : 
     506              : template <typename Term, typename Coefficient>
     507              : static void
     508        11682 : add_coefficient_to_term (std::map<const Term *, const Coefficient *> &map,
     509              :                          const Term *term,
     510              :                          const Coefficient *coefficient_change)
     511              : {
     512        11682 :   auto iter = map.find (term);
     513        11682 :   if (iter == map.end ())
     514        11306 :     map.insert ({term, coefficient_change});
     515              :   else
     516              :     {
     517          376 :       auto &coefficient = map[term];
     518          376 :       coefficient = &(*coefficient + *coefficient_change);
     519              :     }
     520        11682 : }
     521              : 
     522              : /* Add (VAL_WITH_UNIT * UNITLESS_VAL) to this in-construction sum.
     523              : 
     524              :    Try to use concrete values as coefficients, and purely symbolic terms
     525              :    as "terms".  */
     526              : 
     527              : template<typename ConcreteValueType>
     528              : void
     529        11682 : sum<ConcreteValueType>::folding_helper::
     530              : add_to_coefficient_for_addend (const scalar<ConcreteValueType> *val_with_unit,
     531              :                                const scalar<integer> *unitless_val)
     532              : {
     533        11682 :   if (val_with_unit->dyn_cast_concrete_scalar ())
     534              :     {
     535         1218 :       gcc_assert (!unitless_val->dyn_cast_concrete_scalar ());
     536              : 
     537         1218 :       auto *term = unitless_val;
     538         1218 :       auto *coefficient_change = val_with_unit;
     539         1218 :       add_coefficient_to_term (m_map_unitless_term_to_coefficient_with_unit,
     540              :                                term,
     541              :                                coefficient_change);
     542              :     }
     543              :   else
     544              :     {
     545        10464 :       auto *term = val_with_unit;
     546        10464 :       auto *coefficient_change = unitless_val;
     547        10464 :       add_coefficient_to_term (m_map_term_with_unit_to_unitless_coefficient,
     548              :                                term,
     549              :                                coefficient_change);
     550              :     }
     551        11682 : }
     552              : 
     553              : template<typename ConcreteValueType>
     554              : DEBUG_FUNCTION void
     555            0 : sum<ConcreteValueType>::folding_helper::dump () const
     556              : {
     557            0 :   symbolic::dump_node_tree_to_stderr<sum<ConcreteValueType>::folding_helper> (*this);
     558            0 : }
     559              : 
     560              : template<typename ConcreteValueType>
     561              : std::unique_ptr<text_art::tree_widget>
     562            0 : sum<ConcreteValueType>::folding_helper::
     563              : make_dump_widget (symbolic::node_dump_state &nds) const
     564              : {
     565              :   using text_art::tree_widget;
     566            0 :   std::unique_ptr<tree_widget> fh_widget
     567              :     (tree_widget::from_fmt (nds, nullptr, "folding_helper"));
     568              : 
     569              :   {
     570            0 :     std::unique_ptr<tree_widget> map_widget
     571              :       (tree_widget::from_fmt (nds, nullptr, "terms with units"));
     572            0 :     for (auto iter : m_map_term_with_unit_to_unitless_coefficient)
     573              :       {
     574            0 :         auto &addend = iter.first;
     575            0 :         auto &factor = iter.second;
     576            0 :         std::unique_ptr<tree_widget> iter_widget
     577              :           (tree_widget::from_fmt (nds, nullptr, "symbolic addend"));
     578            0 :         iter_widget->add_child (addend->make_dump_widget (nds));
     579            0 :         iter_widget->add_child (factor->make_dump_widget (nds));
     580            0 :         map_widget->add_child (std::move (iter_widget));
     581              :       }
     582            0 :     fh_widget->add_child (std::move (map_widget));
     583            0 :   }
     584              :   {
     585            0 :     std::unique_ptr<tree_widget> map_widget
     586              :       (tree_widget::from_fmt (nds, nullptr, "unitless terms"));
     587            0 :     for (auto iter : m_map_unitless_term_to_coefficient_with_unit)
     588              :       {
     589            0 :         auto &addend = iter.first;
     590            0 :         auto &factor = iter.second;
     591            0 :         std::unique_ptr<tree_widget> iter_widget
     592              :           (tree_widget::from_fmt (nds, nullptr, "symbolic addend"));
     593            0 :         iter_widget->add_child (addend->make_dump_widget (nds));
     594            0 :         iter_widget->add_child (factor->make_dump_widget (nds));
     595            0 :         map_widget->add_child (std::move (iter_widget));
     596              :       }
     597            0 :     fh_widget->add_child (std::move (map_widget));
     598            0 :   }
     599              :   {
     600            0 :     pretty_printer the_pp;
     601            0 :     pretty_printer * const pp = &the_pp;
     602            0 :     pp_format_decoder (pp) = default_tree_printer;
     603            0 :     m_sum_of_concrete_values.dump_to_pp (pp);
     604            0 :     fh_widget->add_child (tree_widget::make (nds, pp));
     605            0 :   }
     606              : 
     607            0 :   return fh_widget;
     608              : }
     609              : 
     610              : template<typename ConcreteValueType>
     611              : bool
     612         9797 : sum<ConcreteValueType>::creation_key::term::
     613              : operator< (const sum<ConcreteValueType>::creation_key::term &other) const
     614              : {
     615         9797 :   if (int c = symbolic::node::cmp (*m_with_unit,  *other.m_with_unit))
     616         7742 :     return c < 0;
     617         2055 :   return symbolic::node::cmp (*m_unitless, *other.m_unitless) < 0;
     618              : }
     619              : 
     620              : template<typename ConcreteValueType>
     621              : bool
     622         9645 : sum<ConcreteValueType>::creation_key::term::
     623              : operator== (const sum<ConcreteValueType>::creation_key::term &other) const
     624              : {
     625         9645 :   return (m_with_unit == other.m_with_unit
     626         9645 :           && m_unitless == other.m_unitless);
     627              : }
     628              : 
     629              : template<typename ConcreteValueType>
     630              : complexity
     631         3084 : sum<ConcreteValueType>::creation_key::term::get_complexity () const
     632              : {
     633         3084 :   gcc_assert (!m_unitless->is_zero_p ());
     634         3084 :   gcc_assert (!m_with_unit->is_zero_p ());
     635              : 
     636         3084 :   if (m_unitless->is_one_p ())
     637         1974 :     return m_with_unit->get_complexity ();
     638         1110 :   else if (m_with_unit->is_one_p ())
     639            0 :     return m_unitless->get_complexity ();
     640              :   else
     641         1110 :     return complexity::from_pair (m_with_unit->get_complexity (),
     642         1110 :                                   m_unitless->get_complexity ());
     643              : }
     644              : 
     645              : template<typename ConcreteValueType>
     646              : std::unique_ptr<text_art::tree_widget>
     647            0 : sum<ConcreteValueType>::creation_key::term::
     648              : make_dump_widget (symbolic::node_dump_state &nds) const
     649              : {
     650              :   using text_art::tree_widget;
     651            0 :   std::unique_ptr<tree_widget> term_widget
     652              :     (tree_widget::from_fmt (nds, nullptr, "term"));
     653            0 :   term_widget->add_child (m_with_unit->make_dump_widget (nds));
     654            0 :   term_widget->add_child (m_unitless->make_dump_widget (nds));
     655            0 :   return term_widget;
     656              : }
     657              : 
     658              : template<typename ConcreteValueType>
     659              : complexity
     660         2673 : sum<ConcreteValueType>::creation_key::get_complexity () const
     661              : {
     662              :   /* If we just have a constant we should have been folded away already.  */
     663         2673 :   gcc_assert (!m_symbolic_terms.empty ());
     664              : 
     665         2673 :   unsigned num_nodes = 0;
     666         2673 :   unsigned max_depth = 0;
     667         5757 :   for (auto term : m_symbolic_terms)
     668              :     {
     669         3084 :       const complexity iter_c = term.get_complexity ();
     670         3084 :       num_nodes += iter_c.m_num_nodes;
     671         3084 :       max_depth = std::max (max_depth, iter_c.m_max_depth);
     672              :     }
     673         2673 :   if (!m_concrete_term.is_zero_p ())
     674              :     {
     675         2394 :       num_nodes += 1;
     676         2394 :       max_depth = std::max (max_depth, (unsigned)1);
     677              :     }
     678         2673 :   return complexity (num_nodes + 1, max_depth + 1);
     679              : }
     680              : 
     681              : template<typename ConcreteValueType>
     682        28719 : sum<ConcreteValueType>::creation_key::
     683              : creation_key (sum<ConcreteValueType>::folding_helper &&fh)
     684        28719 : : m_concrete_term (std::move (fh.m_sum_of_concrete_values))
     685              : {
     686        38902 :   for (auto iter : fh.m_map_term_with_unit_to_unitless_coefficient)
     687              :     {
     688        10183 :       gcc_assert (!iter.first->is_zero_p ());
     689        10183 :       if (!iter.second->is_zero_p ())
     690        10093 :         m_symbolic_terms.push_back ({iter.first, iter.second});
     691              :     }
     692        29842 :   for (auto iter : fh.m_map_unitless_term_to_coefficient_with_unit)
     693              :     {
     694         1123 :       gcc_assert (!iter.first->is_zero_p ());
     695         1123 :       if (!iter.second->is_zero_p ())
     696         1065 :         m_symbolic_terms.push_back ({iter.second, iter.first});
     697              :     }
     698              : 
     699              :   /* Make the ordering of the terms deterministic based on var node id.  */
     700        28719 :   std::sort (m_symbolic_terms.begin (), m_symbolic_terms.end ());
     701        28719 : }
     702              : 
     703              : template<typename ConcreteValueType>
     704              : bool
     705        18402 : sum<ConcreteValueType>::creation_key::
     706              : operator< (const sum<ConcreteValueType>::creation_key &other) const
     707              : {
     708        18402 :   if (int c = ConcreteValueType::cmp (m_concrete_term,
     709        17666 :                                       other.m_concrete_term))
     710         8506 :     return c < 0;
     711              : 
     712         9896 :   if (m_symbolic_terms.size ()
     713         9896 :       != other.m_symbolic_terms.size ())
     714          621 :     return (m_symbolic_terms.size () <  other.m_symbolic_terms.size ());
     715              : 
     716        10297 :   for (size_t i = 0; i < m_symbolic_terms.size (); ++i)
     717        10667 :     if (!(m_symbolic_terms[i] == other.m_symbolic_terms[i]))
     718         8623 :       return m_symbolic_terms[i] < other.m_symbolic_terms[i];
     719              : 
     720              :   return false;
     721              : }
     722              : 
     723              : template<typename ConcreteValueType>
     724              : const scalar<ConcreteValueType> *
     725        28719 : sum<ConcreteValueType>::creation_key::
     726              : maybe_fold (scalar_manager<ConcreteValueType> &mgr) const
     727              : {
     728        28719 :   if (m_symbolic_terms.empty ())
     729              :     {
     730              :       // We have a purely concrete value:
     731        18292 :       return &mgr.get_concrete (m_concrete_term);
     732              :     }
     733              : 
     734        10427 :   if (m_symbolic_terms.size () == 1
     735        10427 :       && m_concrete_term.is_zero_p ())
     736              :     {
     737              :       /* ((X * F) + 0) --> (X * F).  */
     738         4507 :       auto &term = m_symbolic_terms[0];
     739         4507 :       return &mgr.get_product (*term.m_with_unit, *term.m_unitless);
     740              :     }
     741              : 
     742              :   /* If any part of any of the symbolic terms is unknown, the
     743              :      whole sum is unknown.  */
     744        12208 :   for (auto iter : m_symbolic_terms)
     745              :     {
     746         6637 :       if (iter.m_with_unit->is_unknown_p ())
     747          349 :         return &mgr.get_unknown ();
     748         6288 :       if (iter.m_unitless->is_unknown_p ())
     749            0 :         return &mgr.get_unknown ();
     750              :     }
     751              : 
     752         2726 :   if (auto *folded = maybe_fold_for_cvt (mgr))
     753              :     return folded;
     754              : 
     755              :   return nullptr;
     756              : }
     757              : 
     758              : template<typename ConcreteValueType>
     759              : const scalar<ConcreteValueType> *
     760              : sum<ConcreteValueType>::creation_key::
     761              : maybe_fold_for_cvt (scalar_manager<ConcreteValueType> &) const
     762              : {
     763              :   return nullptr;
     764              : }
     765              : 
     766              : /* If all the symbolic terms are bits_from_bytes_scalar
     767              :    and any concrete term is a multiple of 8, then we can move the
     768              :    bits_from_bytes outside the sum.  This is a simplification if the
     769              :    number of terms (symbolic or concrete) > 1, which should be the case
     770              :    for a creation_key that has survived this far.  */
     771              : 
     772              : template<>
     773              : const symbolic_bit_size *
     774         2726 : sum<concrete_bit_size>::creation_key::
     775              : maybe_fold_for_cvt (scalar_manager<concrete_bit_size> &mgr) const
     776              : {
     777         2726 :   concrete_byte_size conc_bytes;
     778         2726 :   if (!maybe_bytes_from_bits (m_concrete_term, &conc_bytes))
     779              :     return nullptr;
     780              : 
     781         2656 :   auto &byte_mgr = mgr.get_node_manager ().get_scalars ().get_bytes ();
     782         2656 :   std::vector<const symbolic_byte_size *> addends_as_bytes;
     783         2656 :   addends_as_bytes.push_back (&symbolic_from_concrete (conc_bytes, byte_mgr));
     784              : 
     785         5388 :   for (auto sym_term : m_symbolic_terms)
     786              :     {
     787         2816 :       auto *bits_from_bytes
     788         2816 :         = sym_term.m_with_unit->dyn_cast_bits_from_bytes_scalar ();
     789         2816 :       if (!bits_from_bytes)
     790         2656 :         return nullptr;
     791         2732 :       auto &addend_as_bytes
     792         2732 :         = bits_from_bytes->get_bytes () * (*sym_term.m_unitless);
     793         2732 :       addends_as_bytes.push_back (&addend_as_bytes);
     794              :     }
     795              : 
     796         2572 :   auto &byte_sum = byte_mgr.get_sum (addends_as_bytes);
     797         2572 :   return &mgr.get_bits_from_bytes (byte_sum);
     798         2656 : }
     799              : 
     800              : template<typename ConcreteValueType>
     801              : DEBUG_FUNCTION void
     802            0 : sum<ConcreteValueType>::creation_key::dump () const
     803              : {
     804            0 :   symbolic::dump_node_tree_to_stderr<sum<ConcreteValueType>::creation_key> (*this);
     805            0 : }
     806              : 
     807              : template<typename ConcreteValueType>
     808              : std::unique_ptr<text_art::tree_widget>
     809            0 : sum<ConcreteValueType>::creation_key::
     810              : make_dump_widget (symbolic::node_dump_state &nds) const
     811              : {
     812              :   using text_art::tree_widget;
     813            0 :   std::unique_ptr<tree_widget> ck_widget
     814              :     (tree_widget::from_fmt (nds, nullptr, "creation_key"));
     815              : 
     816            0 :   for (auto sym_term : m_symbolic_terms)
     817            0 :     ck_widget->add_child (sym_term.make_dump_widget (nds));
     818              :   {
     819            0 :     pretty_printer the_pp;
     820            0 :     pretty_printer * const pp = &the_pp;
     821            0 :     pp_format_decoder (pp) = default_tree_printer;
     822            0 :     m_concrete_term.dump_to_pp (pp);
     823            0 :     ck_widget->add_child (tree_widget::make (nds, pp));
     824            0 :   }
     825              : 
     826            0 :   return ck_widget;
     827              : }
     828              : 
     829              : template<typename ConcreteValueType>
     830         2673 : sum<ConcreteValueType>::sum (unsigned id,
     831              :                              scalar_manager<ConcreteValueType> &mgr,
     832              :                              const creation_key &ck)
     833              : : scalar<ConcreteValueType> (ck.get_complexity (), id, mgr,
     834              :                              scalar<ConcreteValueType>::kind::sum),
     835         2673 :   m_concrete_addend (nullptr)
     836              : {
     837         5757 :   for (auto term : ck.m_symbolic_terms)
     838              :     {
     839         3084 :       gcc_assert (!term.m_with_unit->is_zero_p ());
     840         3084 :       gcc_assert (!term.m_unitless->is_zero_p ());
     841         3084 :       auto &symbolic_addend = (*term.m_with_unit) * (*term.m_unitless);
     842         3084 :       gcc_assert (!symbolic_addend.dyn_cast_concrete_scalar ());
     843         3084 :       gcc_assert (!symbolic_addend.dyn_cast_sum ());
     844         3084 :       m_symbolic_addends.push_back (&symbolic_addend);
     845              :     }
     846              : 
     847         2673 :   if (ck.m_concrete_term.is_zero_p ())
     848              :     {
     849          279 :       gcc_assert (m_symbolic_addends.size () >= 2);
     850              :     }
     851              :   else
     852              :     {
     853         2394 :       gcc_assert (m_symbolic_addends.size () >= 1);
     854         2394 :       m_concrete_addend = &mgr.get_concrete (ck.m_concrete_term);
     855              :     }
     856         2673 : }
     857              : 
     858              : template<typename ConcreteValueType>
     859              : void
     860           82 : sum<ConcreteValueType>::
     861              : print_dump_widget_label_for_scalar (pretty_printer *pp) const
     862              : {
     863           82 :   pp_printf (pp, "sum<%s>", ConcreteValueType::dump_type_name);
     864           82 : }
     865              : 
     866              : template<typename ConcreteValueType>
     867              : void
     868           82 : sum<ConcreteValueType>::
     869              : add_dump_widget_children (text_art::tree_widget &w,
     870              :                           node_dump_state &nds) const
     871              : {
     872          200 :   for (auto iter : m_symbolic_addends)
     873          236 :     w.add_child (iter->make_dump_widget (nds));
     874           82 :   if (m_concrete_addend)
     875          124 :     w.add_child (m_concrete_addend->make_dump_widget (nds));
     876           82 : }
     877              : 
     878              : template<>
     879              : void
     880            0 : sum<integer>::accept_node_visitor (symbolic::node_visitor &v) const
     881              : {
     882            0 :   if (v.visit_scalar_unitless (*this))
     883              :     {
     884            0 :       for (auto iter : m_symbolic_addends)
     885            0 :         iter->accept_node_visitor (v);
     886            0 :       if (m_concrete_addend)
     887            0 :         m_concrete_addend->accept_node_visitor (v);
     888              :     }
     889            0 : }
     890              : 
     891              : template<>
     892              : void
     893            0 : sum<integer_with_unit<units::bit>>::
     894              : accept_node_visitor (symbolic::node_visitor &v) const
     895              : {
     896            0 :   if (v.visit_scalar_bit (*this))
     897              :     {
     898            0 :       for (auto iter : m_symbolic_addends)
     899            0 :         iter->accept_node_visitor (v);
     900            0 :       if (m_concrete_addend)
     901            0 :         m_concrete_addend->accept_node_visitor (v);
     902              :     }
     903            0 : }
     904              : 
     905              : template<>
     906              : void
     907        45936 : sum<integer_with_unit<units::byte>>::
     908              : accept_node_visitor (symbolic::node_visitor &v) const
     909              : {
     910        45936 :   if (v.visit_scalar_byte (*this))
     911              :     {
     912        98429 :       for (auto iter : m_symbolic_addends)
     913        52493 :         iter->accept_node_visitor (v);
     914        45936 :       if (m_concrete_addend)
     915        42051 :         m_concrete_addend->accept_node_visitor (v);
     916              :     }
     917        45936 : }
     918              : 
     919              : template<typename ConcreteValueType>
     920              : std::unique_ptr<json::object>
     921            4 : sum<ConcreteValueType>::to_json () const
     922              : {
     923            4 :   auto obj = scalar<ConcreteValueType>::make_json_obj ("sum");
     924            4 :   auto arr = std::make_unique<json::array> ();
     925           16 :   for (auto iter : m_symbolic_addends)
     926           12 :     arr->append (iter->to_json ());
     927            4 :   obj->set ("symbolic_addends", std::move (arr));
     928            4 :   if (m_concrete_addend)
     929            0 :     obj->set ("concrete_addend", m_concrete_addend->to_json ());
     930            4 :   return obj;
     931            4 : }
     932              : 
     933              : template<typename ConcreteValueType>
     934              : void
     935           20 : sum<ConcreteValueType>::
     936              : dump_inline_to_pp (pretty_printer *pp, enum units::role role) const
     937              : {
     938           20 :   bool first = true;
     939           68 :   for (auto iter : m_symbolic_addends)
     940              :     {
     941           48 :       if (first)
     942              :         first = false;
     943              :       else
     944           28 :         pp_string (pp, " + ");
     945           48 :       iter->dump_inline_to_pp (pp, role);
     946              :     }
     947           20 :   if (m_concrete_addend)
     948              :     {
     949            8 :       if (first)
     950            8 :         first = false;
     951              :       else
     952            8 :         pp_string (pp, " + ");
     953            8 :       m_concrete_addend->dump_inline_to_pp (pp, role);
     954              :     }
     955           20 : }
     956              : 
     957              : template<typename ConcreteValueType>
     958              : std::unique_ptr<diagnostics::markup::node>
     959          110 : sum<ConcreteValueType>::make_markup_node (markup_context &markup_ctxt) const
     960              : {
     961          110 :   std::vector<std::unique_ptr<diagnostics::markup::node>> children;
     962          314 :   for (auto iter : m_symbolic_addends)
     963          204 :     children.push_back (iter->make_markup_node (markup_ctxt));
     964          110 :   if (m_concrete_addend)
     965           36 :     children.push_back (m_concrete_addend->make_markup_node (markup_ctxt));
     966              : 
     967          110 :   gcc_assert (children.size () >= 2);
     968              : 
     969          110 :   if (markup_ctxt.m_user_facing)
     970          110 :     if (children.size () == 2)
     971              :       {
     972              :         /* Lower "(-A) + B" to "B - A".  */
     973           90 :         if (auto *a_as_negation = children[0]->dyn_cast_math_negation ())
     974              :           return std::make_unique<diagnostics::markup::math::subtraction>
     975            0 :             (std::move (children[1]), // "B"
     976            0 :              a_as_negation->take_operand ()); // "A"
     977              : 
     978              :         /* Lower "A + (-B)" to "A - B".  */
     979           90 :         if (auto *b_as_negation = children[1]->dyn_cast_math_negation ())
     980              :           return std::make_unique<diagnostics::markup::math::subtraction>
     981           36 :             (std::move (children[0]), // "A"
     982           36 :              b_as_negation->take_operand ()); // "B"
     983              :       }
     984              : 
     985              :   return std::make_unique<diagnostics::markup::math::sum>
     986           74 :     (std::move (children));
     987          110 : }
     988              : 
     989              : // template<typename ConcreteValueType> class product : public scalar<ConcreteValueType>
     990              : 
     991              : template<typename ConcreteValueType>
     992              : bool
     993        15677 : product<ConcreteValueType>::key::operator< (const key &other) const
     994              : {
     995        15677 :   if (int c = symbolic::node::cmp (*m_lhs, *other.m_lhs))
     996         4105 :     return c < 0;
     997        11572 :   return symbolic::node::cmp (*m_rhs, *other.m_rhs) < 0;
     998              : }
     999              : 
    1000              : template<typename ConcreteValueType>
    1001              : complexity
    1002         1178 : product<ConcreteValueType>::key::get_complexity () const
    1003              : {
    1004         1178 :   return complexity::from_pair (m_lhs->get_complexity (),
    1005         1178 :                                 m_rhs->get_complexity ());
    1006              : }
    1007              : 
    1008              : template<typename ConcreteValueType>
    1009              : void
    1010              : product<ConcreteValueType>::key::canonicalize ()
    1011              : {
    1012              :   // No-op in general...
    1013              : }
    1014              : 
    1015              : template<>
    1016              : void
    1017          574 : product<integer>::key::canonicalize ()
    1018              : {
    1019              :   /* For integer * integer, make sure any
    1020              :      concrete value is on the right.  */
    1021          574 :   if (m_lhs->dyn_cast_concrete_scalar ())
    1022           40 :     std::swap (m_lhs, m_rhs);
    1023          574 : }
    1024              : 
    1025              : template<typename ConcreteValueType>
    1026         1178 : product<ConcreteValueType>::product (unsigned id,
    1027              :                                      scalar_manager<ConcreteValueType> &mgr,
    1028              :                                      const key &k)
    1029              : : scalar<ConcreteValueType> (k.get_complexity (), id, mgr,
    1030              :                              scalar<ConcreteValueType>::kind::product),
    1031         1178 :   m_lhs (*k.m_lhs),
    1032         1178 :   m_rhs (*k.m_rhs)
    1033              : {
    1034              :   if (CHECKING_P)
    1035              :     {
    1036              :       /* Multiplication by 0 or 1 should have been folded away by now.  */
    1037         1178 :       gcc_assert (!m_lhs.is_zero_p ());
    1038         1178 :       gcc_assert (!m_rhs.is_zero_p ());
    1039         1178 :       gcc_assert (!m_lhs.is_one_p ());
    1040         1178 :       gcc_assert (!m_rhs.is_one_p ());
    1041              : 
    1042              :       /* Concrete * concrete should have been folded by now.  */
    1043         1178 :       gcc_assert (!(m_lhs.dyn_cast_concrete_scalar ()
    1044              :                     && m_rhs.dyn_cast_concrete_scalar ()));
    1045              :     }
    1046         1178 : }
    1047              : 
    1048              : template<typename ConcreteValueType>
    1049              : void
    1050           24 : product<ConcreteValueType>::
    1051              : print_dump_widget_label_for_scalar (pretty_printer *pp) const
    1052              : {
    1053           24 :   pp_printf (pp, "product<%s>", ConcreteValueType::dump_type_name);
    1054           24 : }
    1055              : 
    1056              : template<typename ConcreteValueType>
    1057              : void
    1058           24 : product<ConcreteValueType>::
    1059              : add_dump_widget_children (text_art::tree_widget &w,
    1060              :                           node_dump_state &nds) const
    1061              : {
    1062           48 :   w.add_child (m_lhs.make_dump_widget (nds));
    1063           48 :   w.add_child (m_rhs.make_dump_widget (nds));
    1064           24 : }
    1065              : 
    1066              : template<>
    1067              : void
    1068            0 : product<integer>::accept_node_visitor (symbolic::node_visitor &v) const
    1069              : {
    1070            0 :   if (v.visit_scalar_unitless (*this))
    1071              :     {
    1072            0 :       m_lhs.accept_node_visitor (v);
    1073            0 :       m_rhs.accept_node_visitor (v);
    1074              :     }
    1075            0 : }
    1076              : 
    1077              : template<>
    1078              : void
    1079            0 : product<integer_with_unit<units::bit>>::
    1080              : accept_node_visitor (symbolic::node_visitor &v) const
    1081              : {
    1082            0 :   if (v.visit_scalar_bit (*this))
    1083              :     {
    1084            0 :       m_lhs.accept_node_visitor (v);
    1085            0 :       m_rhs.accept_node_visitor (v);
    1086              :     }
    1087            0 : }
    1088              : 
    1089              : template<>
    1090              : void
    1091        38710 : product<integer_with_unit<units::byte>>::
    1092              : accept_node_visitor (symbolic::node_visitor &v) const
    1093              : {
    1094        38710 :   if (v.visit_scalar_byte (*this))
    1095              :     {
    1096        38710 :       m_lhs.accept_node_visitor (v);
    1097        38710 :       m_rhs.accept_node_visitor (v);
    1098              :     }
    1099        38710 : }
    1100              : 
    1101              : template<typename ConcreteValueType>
    1102              : std::unique_ptr<json::object>
    1103           16 : product<ConcreteValueType>::to_json () const
    1104              : {
    1105           16 :   auto obj = scalar<ConcreteValueType>::make_json_obj ("product");
    1106           16 :   obj->set ("lhs", m_lhs.to_json ());
    1107           16 :   obj->set ("rhs", m_rhs.to_json ());
    1108           16 :   return obj;
    1109              : }
    1110              : 
    1111              : template<typename ConcreteValueType>
    1112              : void
    1113           16 : product<ConcreteValueType>::
    1114              : dump_inline_to_pp (pretty_printer *pp, enum units::role role) const
    1115              : {
    1116           16 :   m_lhs.dump_inline_to_pp (pp, role);
    1117           16 :   pp_string (pp, " * ");
    1118           16 :   m_rhs.dump_inline_to_pp (pp, role);
    1119           16 : }
    1120              : 
    1121              : template<typename ConcreteValueType>
    1122              : std::unique_ptr<diagnostics::markup::node>
    1123           98 : product<ConcreteValueType>::make_markup_node (markup_context &markup_ctxt) const
    1124              : {
    1125              :   /* "A * -1" -> "-A".  */
    1126           98 :   if (markup_ctxt.m_user_facing)
    1127           98 :     if (auto *conc = m_rhs.dyn_cast_concrete_scalar ())
    1128           60 :       if (conc->get_concrete ().get_integer () == -1)
    1129              :         return std::make_unique<diagnostics::markup::math::negation>
    1130           60 :           (m_lhs.make_markup_node (markup_ctxt));
    1131              : 
    1132           38 :   auto markup
    1133              :     = std::make_unique<diagnostics::markup::math::product> ();
    1134              :   /* Put any concrete value on the RHS.  */
    1135           38 :   if (m_lhs.dyn_cast_concrete_scalar ())
    1136              :     {
    1137           22 :       markup->add_child (m_rhs.make_markup_node (markup_ctxt));
    1138           22 :       markup->add_child (m_lhs.make_markup_node (markup_ctxt));
    1139              :     }
    1140              :   else
    1141              :     {
    1142           16 :       markup->add_child (m_lhs.make_markup_node (markup_ctxt));
    1143           16 :       markup->add_child (m_rhs.make_markup_node (markup_ctxt));
    1144              :     }
    1145           38 :   return markup;
    1146           38 : }
    1147              : 
    1148              : // class scalar_from_svalue : public scalar<ConcreteValueType>
    1149              : 
    1150              : template<typename ConcreteValueType>
    1151              : bool
    1152       253018 : scalar_from_svalue<ConcreteValueType>::key::operator< (const key &other) const
    1153              : {
    1154       253018 :   if (int c = symbolic::node::cmp (*m_sval, *other.m_sval))
    1155            0 :     return c < 0;
    1156              :   return false;
    1157              : }
    1158              : 
    1159              : template<typename ConcreteValueType>
    1160              : complexity
    1161         8132 : scalar_from_svalue<ConcreteValueType>::key::get_complexity () const
    1162              : {
    1163              :   // For now, return the complexity of m_sval directly
    1164         8132 :   return m_sval->get_complexity ();
    1165              : }
    1166              : 
    1167              : template<typename ConcreteValueType>
    1168         8132 : scalar_from_svalue<ConcreteValueType>::
    1169              : scalar_from_svalue (unsigned id,
    1170              :                     scalar_manager<ConcreteValueType> &mgr,
    1171              :                     const key &k)
    1172              : : scalar<ConcreteValueType> (k.get_complexity (), id, mgr,
    1173              :                              scalar<ConcreteValueType>::kind::from_svalue),
    1174         8132 :   m_sval (*k.m_sval)
    1175              : {
    1176              :   if (CHECKING_P)
    1177              :     {
    1178         8132 :       if (auto cst = m_sval.maybe_get_constant ())
    1179            0 :         gcc_assert (TREE_CODE (cst) != INTEGER_CST);
    1180              :     }
    1181         8132 : }
    1182              : 
    1183              : template<typename ConcreteValueType>
    1184              : void
    1185         1151 : scalar_from_svalue<ConcreteValueType>::
    1186              : print_dump_widget_label_for_scalar (pretty_printer *pp) const
    1187              : {
    1188         1151 :   pp_printf (pp, "scalar_from_svalue<%s>", ConcreteValueType::dump_type_name);
    1189         1151 : }
    1190              : 
    1191              : template<typename ConcreteValueType>
    1192              : void
    1193         1010 : scalar_from_svalue<ConcreteValueType>::
    1194              : add_dump_widget_children (text_art::tree_widget &w,
    1195              :                           node_dump_state &nds) const
    1196              : {
    1197         2020 :   w.add_child (m_sval.make_dump_widget (nds));
    1198         1010 : }
    1199              : 
    1200              : template<>
    1201              : void
    1202        38710 : scalar_from_svalue<integer>::
    1203              : accept_node_visitor (symbolic::node_visitor &v) const
    1204              : {
    1205        38710 :   if (v.visit_scalar_unitless (*this))
    1206        38710 :     m_sval.accept_node_visitor (v);
    1207        38710 : }
    1208              : 
    1209              : template<>
    1210              : void
    1211         3689 : scalar_from_svalue<integer_with_unit<units::bit>>::
    1212              : accept_node_visitor (symbolic::node_visitor &v) const
    1213              : {
    1214         3689 :   if (v.visit_scalar_bit (*this))
    1215         3689 :     m_sval.accept_node_visitor (v);
    1216         3689 : }
    1217              : 
    1218              : template<>
    1219              : void
    1220      2011353 : scalar_from_svalue<integer_with_unit<units::byte>>::
    1221              : accept_node_visitor (symbolic::node_visitor &v) const
    1222              : {
    1223      2011353 :   if (v.visit_scalar_byte (*this))
    1224      2011353 :     m_sval.accept_node_visitor (v);
    1225      2011353 : }
    1226              : 
    1227              : template<typename ConcreteValueType>
    1228              : std::unique_ptr<json::object>
    1229            0 : scalar_from_svalue<ConcreteValueType>::to_json () const
    1230              : {
    1231            0 :   auto obj = scalar<ConcreteValueType>::make_json_obj ("scalar_from_svalue");
    1232            0 :   obj->set ("svalue", m_sval.to_json ());
    1233            0 :   return obj;
    1234              : }
    1235              : 
    1236              : template<typename ConcreteValueType>
    1237              : void
    1238            0 : scalar_from_svalue<ConcreteValueType>::
    1239              : dump_inline_to_pp (pretty_printer *pp, enum units::role) const
    1240              : {
    1241            0 :   m_sval.dump_to_pp (pp, true);
    1242            0 : }
    1243              : 
    1244              : template<typename ConcreteValueType>
    1245              : std::unique_ptr<diagnostics::markup::node>
    1246           43 : scalar_from_svalue<ConcreteValueType>::
    1247              : make_markup_node (markup_context &markup_ctxt) const
    1248              : {
    1249           43 :   return m_sval.make_markup_node (markup_ctxt);
    1250              : }
    1251              : 
    1252              : #if CHECKING_P
    1253              : 
    1254              : // template<typename ConcreteValueType> class named_test_scalar : public scalar<ConcreteValueType>
    1255              : 
    1256              : template<typename ConcreteValueType>
    1257              : void
    1258          100 : named_test_scalar<ConcreteValueType>::
    1259              : print_dump_widget_label_for_scalar (pretty_printer *pp) const
    1260              : {
    1261          100 :   pp_printf (pp, "named_test_scalar<%s>: %qs",
    1262              :              ConcreteValueType::dump_type_name, m_name.c_str ());
    1263          100 : }
    1264              : 
    1265              : template<typename ConcreteValueType>
    1266              : void
    1267          100 : named_test_scalar<ConcreteValueType>::
    1268              : add_dump_widget_children (text_art::tree_widget &,
    1269              :                           node_dump_state &) const
    1270              : {
    1271              :   // no-op
    1272          100 : }
    1273              : 
    1274              : template<>
    1275              : void
    1276            0 : named_test_scalar<integer>::accept_node_visitor (symbolic::node_visitor &v) const
    1277              : {
    1278            0 :   v.visit_scalar_unitless (*this);
    1279            0 : }
    1280              : 
    1281              : template<>
    1282              : void
    1283            0 : named_test_scalar<integer_with_unit<units::bit>>::
    1284              : accept_node_visitor (symbolic::node_visitor &v) const
    1285              : {
    1286            0 :   v.visit_scalar_bit (*this);
    1287            0 : }
    1288              : 
    1289              : template<>
    1290              : void
    1291            0 : named_test_scalar<integer_with_unit<units::byte>>::
    1292              : accept_node_visitor (symbolic::node_visitor &v) const
    1293              : {
    1294            0 :   v.visit_scalar_byte (*this);
    1295            0 : }
    1296              : 
    1297              : template<typename ConcreteValueType>
    1298              : std::unique_ptr<json::object>
    1299           32 : named_test_scalar<ConcreteValueType>::to_json () const
    1300              : {
    1301           32 :   auto obj = scalar<ConcreteValueType>::make_json_obj ("named_test_scalar");
    1302           64 :   obj->set_string ("name", m_name);
    1303           32 :   return obj;
    1304              : }
    1305              : 
    1306              : template<>
    1307              : void
    1308           28 : named_test_scalar<integer_with_unit<units::bit>>::
    1309              : dump_inline_to_pp (pretty_printer *pp, enum units::role role) const
    1310              : {
    1311           28 :   switch (role)
    1312              :     {
    1313            0 :     default:
    1314            0 :       gcc_unreachable ();
    1315           16 :     case units::role::generic:
    1316           16 :     case units::role::size:
    1317           16 :       pp_printf (pp, "%s bits", m_name.c_str ());
    1318           16 :       break;
    1319           12 :     case units::role::offset:
    1320           12 :       pp_printf (pp, "bit %s", m_name.c_str ());
    1321           12 :       break;
    1322              :     }
    1323           28 : }
    1324              : 
    1325              : template<>
    1326              : void
    1327           24 : named_test_scalar<integer_with_unit<units::byte>>::
    1328              : dump_inline_to_pp (pretty_printer *pp, enum units::role role) const
    1329              : {
    1330           24 :   switch (role)
    1331              :     {
    1332            0 :     default:
    1333            0 :       gcc_unreachable ();
    1334           16 :     case units::role::generic:
    1335           16 :     case units::role::size:
    1336           16 :       pp_printf (pp, "%s bytes", m_name.c_str ());
    1337           16 :       break;
    1338            8 :     case units::role::offset:
    1339            8 :       pp_printf (pp, "byte %s", m_name.c_str ());
    1340            8 :       break;
    1341              :     }
    1342           24 : }
    1343              : 
    1344              : template<>
    1345              : void
    1346           16 : named_test_scalar<integer>::
    1347              : dump_inline_to_pp (pretty_printer *pp, enum units::role) const
    1348              : {
    1349           16 :   pp_string (pp, m_name.c_str ());
    1350           16 : }
    1351              : 
    1352              : template<typename ConcreteValueType>
    1353              : std::unique_ptr<diagnostics::markup::node>
    1354          232 : named_test_scalar<ConcreteValueType>::make_markup_node (markup_context &) const
    1355              : {
    1356          464 :   return diagnostics::markup::make_text_node (m_name);
    1357              : }
    1358              : 
    1359              : #endif /* #if CHECKING_P */
    1360              : 
    1361              : // class bits_from_bytes_scalar : public symbolic_bit_size
    1362              : 
    1363              : bool
    1364        81702 : bits_from_bytes_scalar::key::operator< (const key &other) const
    1365              : {
    1366        81702 :   return symbolic::node::cmp (*m_bytes, *other.m_bytes) < 0;
    1367              : }
    1368              : 
    1369              : complexity
    1370         5200 : bits_from_bytes_scalar::key::get_complexity () const
    1371              : {
    1372         5200 :   return complexity (*m_bytes);
    1373              : }
    1374              : 
    1375         5200 : bits_from_bytes_scalar::
    1376              : bits_from_bytes_scalar (unsigned id,
    1377              :                         scalar_manager<concrete_bit_size> &mgr,
    1378              :                         const key &k)
    1379              : : symbolic_bit_size (k.get_complexity (), id, mgr,
    1380              :                      symbolic_bit_size::kind::bits_from_bytes),
    1381         5200 :   m_bytes (*k.m_bytes)
    1382              : {
    1383         5200 :   if (CHECKING_P)
    1384              :     {
    1385         5200 :       gcc_assert (!m_bytes.dyn_cast_concrete_scalar ());
    1386         5200 :       gcc_assert (!m_bytes.is_unknown_p ());
    1387              :     }
    1388         5200 : }
    1389              : 
    1390              : void
    1391          397 : bits_from_bytes_scalar::print_dump_widget_label_for_scalar (pretty_printer *pp) const
    1392              : {
    1393          397 :   pp_string (pp, "bits_from_bytes_scalar");
    1394          397 : }
    1395              : 
    1396              : void
    1397          397 : bits_from_bytes_scalar::add_dump_widget_children (text_art::tree_widget &w,
    1398              :                                                   node_dump_state &nds) const
    1399              : {
    1400          794 :   w.add_child (m_bytes.make_dump_widget (nds));
    1401          397 : }
    1402              : 
    1403              : void
    1404      1729101 : bits_from_bytes_scalar::accept_node_visitor (symbolic::node_visitor &v) const
    1405              : {
    1406      1729101 :   if (v.visit_scalar_bit (*this))
    1407      1729101 :     m_bytes.accept_node_visitor (v);
    1408      1729101 : }
    1409              : 
    1410              : std::unique_ptr<json::object>
    1411            4 : bits_from_bytes_scalar::to_json () const
    1412              : {
    1413            4 :   auto obj = scalar<integer_with_unit<units::bit>>::make_json_obj
    1414            4 :     ("bits_from_bytes_scalar");
    1415            4 :   obj->set ("bytes", m_bytes.to_json ());
    1416            4 :   return obj;
    1417              : }
    1418              : 
    1419              : void
    1420            4 : bits_from_bytes_scalar::
    1421              : dump_inline_to_pp (pretty_printer *pp, enum units::role role) const
    1422              : {
    1423            4 :   pp_string (pp, "bits_from_bytes_scalar(");
    1424            4 :   m_bytes.dump_inline_to_pp (pp, role);
    1425            4 :   pp_string (pp, ")");
    1426            4 : }
    1427              : 
    1428              : std::unique_ptr<diagnostics::markup::node>
    1429            0 : bits_from_bytes_scalar::make_markup_node (markup_context &) const
    1430              : {
    1431            0 :   return diagnostics::markup::make_text_node ("bits_from_bytes_scalar");
    1432              : }
    1433              : 
    1434              : // template<typename ConcreteValueType> class scalar_manager;
    1435              : 
    1436              : template<typename ConcreteValueType>
    1437              : const concrete_scalar<ConcreteValueType> &
    1438     15957512 : scalar_manager<ConcreteValueType>::get_concrete (const ConcreteValueType &value)
    1439              : {
    1440     15957512 :   auto iter = m_concrete_map.find (value);
    1441     15957512 :   if (iter != m_concrete_map.end ())
    1442     15865011 :     return *(iter->second);
    1443              : 
    1444        92501 :   auto s = std::make_unique<concrete_scalar<ConcreteValueType>>
    1445        92501 :     (alloc_node_id (),
    1446              :      *this,
    1447              :      value);
    1448        92501 :   auto &result = *s.get ();
    1449        92501 :   m_concrete_map.emplace (value, std::move (s));
    1450              :   return result;
    1451        92501 : }
    1452              : 
    1453              : /* Helper for implementing (concrete unit) * scalar.
    1454              :    See if we can unpack scalar to move terms it to the
    1455              :    per-unit scalar.   */
    1456              : 
    1457              : template<typename ConcreteValueType>
    1458              : const scalar<ConcreteValueType> *
    1459        27026 : scalar_manager<ConcreteValueType>::
    1460              : maybe_from_unitless (const ConcreteValueType &concrete_lhs,
    1461              :                      const scalar<integer> &unitless_rhs)
    1462              : {
    1463        27026 :   if (concrete_lhs.is_one_p ())
    1464              :     {
    1465              :       // (1 unit) * CST --> (CST units)
    1466         7076 :       if (auto s = unitless_rhs.dyn_cast_concrete_scalar ())
    1467         5292 :         return &get_concrete (ConcreteValueType (s->get_concrete ()));
    1468              : 
    1469              :       /* (1 unit) * (A * B) --> to_unit(A) * B
    1470              :          if A can be converted; otherwise try --> to_unit(B) * A).  */
    1471         1784 :       if (auto s = unitless_rhs.dyn_cast_product ())
    1472              :         {
    1473           16 :           if (auto *lhs_with_units
    1474           16 :                 = maybe_from_unitless (concrete_lhs, s->get_lhs ()))
    1475            0 :             return &((*lhs_with_units) * s->get_rhs ());
    1476           16 :           if (auto *rhs_with_units
    1477           16 :                 = maybe_from_unitless (concrete_lhs, s->get_rhs ()))
    1478           16 :             return &((*rhs_with_units) * s->get_lhs ());
    1479              :         }
    1480              : 
    1481              :       /* (1 unit) * unitless_from_sval(SVAL) --> with_unit_from_sval(SVAL).  */
    1482         1768 :       if (auto *from_sval = unitless_rhs.dyn_cast_scalar_from_svalue ())
    1483         1752 :         return &get_scalar_from_svalue (from_sval->get_svalue ());
    1484              :     }
    1485              : 
    1486        19966 :   if (auto *bits = maybe_from_unitless_for_unit (concrete_lhs, unitless_rhs))
    1487              :     return bits;
    1488              : 
    1489              :   return nullptr;
    1490              : }
    1491              : 
    1492              : template<>
    1493              : const symbolic_bit_offset *
    1494        12437 : scalar_manager<concrete_bit_offset>::
    1495              : maybe_from_unitless_for_unit (const concrete_bit_offset &concrete_lhs,
    1496              :                               const scalar<integer> &unitless_rhs)
    1497              : {
    1498        12437 :   auto &byte_mgr = get_node_manager ().get_scalars ().get_bytes ();
    1499              : 
    1500              :   /* ((8 * C) bits * unitless_rhs)
    1501              :      --> bits_from_bytes (C_bytes * unitless_rhs).  */
    1502        24874 :   if (concrete_lhs.get_integer ().floor_mod (BITS_PER_UNIT) == 0)
    1503        12341 :     return &(get_bits_from_bytes
    1504        12341 :              (byte_mgr.get_concrete
    1505        24682 :               (concrete_byte_offset (concrete_lhs.get_integer ()
    1506        24682 :                                      .floor_div (integer (BITS_PER_UNIT))))
    1507        12341 :               * unitless_rhs));
    1508              : 
    1509              :   return nullptr;
    1510              : }
    1511              : 
    1512              : template<>
    1513              : const symbolic_byte_offset *
    1514         7529 : scalar_manager<concrete_byte_offset>::
    1515              : maybe_from_unitless_for_unit (const concrete_byte_offset &,
    1516              :                               const scalar<integer> &)
    1517              : {
    1518              :   // No-op
    1519         7529 :   return nullptr;
    1520              : }
    1521              : 
    1522              : /* No-op specializing for "converting" plain integer to integer.  */
    1523              : 
    1524              : template<>
    1525              : const scalar<integer> *
    1526          163 : scalar_manager<integer>::
    1527              : maybe_from_unitless (const integer &concrete_lhs,
    1528              :                      const scalar<integer> &unitless_rhs)
    1529              : {
    1530          163 :   if (concrete_lhs.is_one_p ())
    1531           20 :     return &unitless_rhs;
    1532              :   return nullptr;
    1533              : }
    1534              : 
    1535              : template<typename ConcreteValueType>
    1536              : const scalar<ConcreteValueType> &
    1537        28719 : scalar_manager<ConcreteValueType>::
    1538              : get_sum_unmemoized (const typename sum<ConcreteValueType>::memoization_key &memo_key)
    1539              : {
    1540              :   /* Gather and unwrap the addends.  */
    1541              :   using folding_helper = typename sum<ConcreteValueType>::folding_helper;
    1542        28719 :   folding_helper fh;
    1543        86417 :   for (auto iter : memo_key.m_addends)
    1544        57698 :     fh.add_addend (*iter, get_unitless_manager ());
    1545              : 
    1546              :   /* Convert from map to a vector in a canonical order.  */
    1547              :   using creation_key = typename sum<ConcreteValueType>::creation_key;
    1548        28719 :   creation_key ck (std::move (fh));
    1549              : 
    1550        28719 :   if (auto *folded = ck.maybe_fold (*this))
    1551              :     return *folded;
    1552              : 
    1553              :   /* Cache based on creation_key.  */
    1554         2999 :   auto iter = m_sum_map.find (ck);
    1555         2999 :   if (iter != m_sum_map.end ())
    1556          326 :     return *(iter->second.get ());
    1557              : 
    1558         2673 :   auto s = std::make_unique<sum<ConcreteValueType>> (alloc_node_id (),
    1559              :                                                      *this,
    1560              :                                                      ck);
    1561         2673 :   auto &result = *s.get ();
    1562         2673 :   m_sum_map.emplace (std::move (ck), std::move (s));
    1563         2673 :   return result;
    1564        31392 : }
    1565              : 
    1566              : 
    1567              : template<typename ConcreteValueType>
    1568              : const scalar<ConcreteValueType> &
    1569       222019 : scalar_manager<ConcreteValueType>::
    1570              : get_sum (std::vector<const scalar<ConcreteValueType> *> addends)
    1571              : {
    1572              :   typename sum<ConcreteValueType>::memoization_key
    1573       222019 :     memo_key {std::move (addends) };
    1574              : 
    1575       222019 :   auto iter = m_memoized_sums.find (memo_key);
    1576       222019 :   if (iter != m_memoized_sums.end ())
    1577       193300 :     return *(iter->second);
    1578              : 
    1579        28719 :   auto &s = get_sum_unmemoized (memo_key);
    1580        28719 :   m_memoized_sums.insert ({std::move (memo_key), &s});
    1581        28719 :   return s;
    1582       222019 : }
    1583              : 
    1584              : template<typename ConcreteValueType>
    1585              : const scalar<ConcreteValueType> &
    1586        22585 : scalar_manager<ConcreteValueType>::
    1587              : get_difference (const scalar<ConcreteValueType> &lhs,
    1588              :                 const scalar<ConcreteValueType> &rhs)
    1589              : {
    1590        22585 :   return lhs + get_negation (rhs);
    1591              : }
    1592              : 
    1593              : template
    1594              : const scalar<concrete_bit_offset> &
    1595              : scalar_manager<concrete_bit_offset>::
    1596              : get_difference (const scalar<concrete_bit_offset> &lhs,
    1597              :                 const scalar<concrete_bit_offset> &rhs);
    1598              : 
    1599              : template
    1600              : const scalar<concrete_byte_offset> &
    1601              : scalar_manager<concrete_byte_offset>::
    1602              : get_difference (const scalar<concrete_byte_offset> &lhs,
    1603              :                 const scalar<concrete_byte_offset> &rhs);
    1604              : 
    1605              : template<typename ConcreteValueType>
    1606              : const scalar<ConcreteValueType> &
    1607        23014 : scalar_manager<ConcreteValueType>::
    1608              : get_negation (const scalar<ConcreteValueType> &s)
    1609              : {
    1610        23014 :   if (auto concrete = s.dyn_cast_concrete_scalar ())
    1611              :     {
    1612        21686 :       auto &i = concrete->get_concrete ();
    1613              : 
    1614              :       // -0 --> 0
    1615        21686 :       if (i.is_zero_p ())
    1616              :         return s;
    1617        20234 :       return get_concrete (-i);
    1618              :     }
    1619              : 
    1620         1328 :   auto &unitless_mgr = get_unitless_manager ();
    1621         1328 :   auto &minus_one = unitless_mgr.get_concrete (integer (-1));
    1622         1328 :   return get_product (s, minus_one);
    1623              : }
    1624              : 
    1625              : template<typename ConcreteValueType>
    1626              : const integer &
    1627              : as_integer (const integer &i) { return i; }
    1628              : 
    1629              : template<typename Unit>
    1630              : const integer &
    1631              : as_integer (const integer_with_unit<Unit> &i) { return i.get_integer (); }
    1632              : 
    1633              : template<typename ConcreteValueType>
    1634              : const scalar<ConcreteValueType> *
    1635        38640 : scalar_manager<ConcreteValueType>::
    1636              : maybe_fold_product (const typename product<ConcreteValueType>::key &k)
    1637              : {
    1638        38640 :   if (auto concrete_lhs = k.m_lhs->dyn_cast_concrete_scalar ())
    1639              :     {
    1640        27181 :       auto &i = concrete_lhs->get_concrete ();
    1641              : 
    1642              :       // 0 units * A --> 0 units
    1643        27181 :       if (i.is_zero_p ())
    1644           24 :         return &get_concrete (ConcreteValueType (0));
    1645              : 
    1646              :       // concrete unit * A --> A units
    1647        27157 :       if (auto s = maybe_from_unitless (concrete_lhs->get_concrete (),
    1648        27157 :                                         *k.m_rhs))
    1649              :         return s;
    1650              : 
    1651              :       // concrete units * concrete rhs --> concrete units
    1652         7752 :       if (auto concrete_rhs = k.m_rhs->dyn_cast_concrete_scalar ())
    1653         5068 :         return &get_concrete (i * concrete_rhs->get_concrete ());
    1654              : 
    1655         2787 :       if (auto rhs_prod = k.m_rhs->dyn_cast_product ())
    1656              :         {
    1657              :           /* (C1 units) * (F * C2) where C1 and C2 are constant
    1658              :              --> (C1 * C2) units * F units.  */
    1659          154 :           if (auto conc = rhs_prod->get_rhs ().dyn_cast_concrete_scalar ())
    1660          154 :             return &(get_concrete (concrete_lhs->get_concrete ()
    1661          308 :                                    * conc->get_concrete ())
    1662          308 :                      * rhs_prod->get_lhs ());
    1663              :         }
    1664              :     }
    1665        14092 :   if (auto concrete_rhs = k.m_rhs->dyn_cast_concrete_scalar ())
    1666              :     {
    1667        11315 :       auto &i = concrete_rhs->get_concrete ();
    1668              : 
    1669              :       // X units * 0 --> 0 units
    1670        11315 :       if (i.is_zero_p ())
    1671           24 :         return &get_concrete (ConcreteValueType (0));
    1672              : 
    1673              :       // X units * 1 --> X units
    1674        11291 :       if (i.is_one_p ())
    1675         8807 :         return k.m_lhs;
    1676              : 
    1677              :       /* (X units * F1) units * concrete factor F2
    1678              :          --> (X * (F1 * F2)) units.  */
    1679         2484 :       if (auto lhs_prod = k.m_lhs->dyn_cast_product ())
    1680              :         return &(lhs_prod->get_lhs ()
    1681          210 :                  * (lhs_prod->get_rhs () * *concrete_rhs));
    1682              :     }
    1683              : 
    1684              :   /* Distributive property.  */
    1685              :   {
    1686         5051 :     if (auto sum_lhs = k.m_lhs->dyn_cast_sum ())
    1687              :       {
    1688              :         /* product(sum(s1 + ... sN), RHS)
    1689              :            --> sum((s1 * RHS), ... (sN * RHS)).  */
    1690          168 :         std::vector<const scalar<ConcreteValueType> *> products;
    1691          363 :         for (size_t i = 0; i < sum_lhs->get_num_symbolic_addends (); ++i)
    1692          195 :           products.push_back (&(sum_lhs->get_symbolic_addend (i)
    1693          195 :                                 * (*k.m_rhs)));
    1694          168 :         if (auto conc = sum_lhs->get_any_concrete_addend ())
    1695          157 :           products.push_back (&((*conc) * (*k.m_rhs)));
    1696          168 :         return &get_sum (products);
    1697          168 :     }
    1698              :   }
    1699              : 
    1700              :   /* Product of product.  */
    1701         4883 :   if (auto lhs_prod = k.m_lhs->dyn_cast_product ())
    1702              :     {
    1703              :       /* Product of product: (LHS units * A) * B --> (LHS units) * (A * B) */
    1704           12 :       auto &unitless_mgr = get_unitless_manager ();
    1705           12 :       return &get_product (lhs_prod->get_lhs (),
    1706              :                            unitless_mgr.get_product (lhs_prod->get_rhs (),
    1707           24 :                                                      *k.m_rhs));
    1708              :     }
    1709              : 
    1710         4871 :   if (k.m_lhs->is_unknown_p ())
    1711          103 :     return &get_unknown ();
    1712         4768 :   if (k.m_rhs->is_unknown_p ())
    1713          199 :     return &get_unknown ();
    1714              : 
    1715              :   return nullptr;
    1716              : }
    1717              : 
    1718              : template<typename ConcreteValueType>
    1719              : const scalar<ConcreteValueType> &
    1720        38640 : scalar_manager<ConcreteValueType>::
    1721              : get_product (const scalar<ConcreteValueType> &lhs,
    1722              :              const scalar<integer> &rhs)
    1723              : {
    1724        38640 :   typename product<ConcreteValueType>::key k {&lhs, &rhs};
    1725              : 
    1726        38640 :   if (auto *s = maybe_fold_product (k))
    1727              :     return *s;
    1728              : 
    1729              :   // Canonicalize order when lhs is also integer
    1730          574 :   k.canonicalize ();
    1731              : 
    1732         4569 :   auto iter = m_product_map.find (k);
    1733         4569 :   if (iter != m_product_map.end ())
    1734         3391 :     return *(iter->second);
    1735              : 
    1736         1178 :   auto s
    1737         1178 :     = std::make_unique<product<ConcreteValueType>> (alloc_node_id (),
    1738              :                                                     *this,
    1739              :                                                     k);
    1740         1178 :   auto &result = *s.get ();
    1741         1178 :   m_product_map.emplace (std::move (k), std::move (s));
    1742         1178 :   return result;
    1743         1178 : }
    1744              : 
    1745              : template<typename ConcreteValueType>
    1746              : const scalar<ConcreteValueType> *
    1747       741307 : scalar_manager<ConcreteValueType>::
    1748              : maybe_fold_scalar_from_svalue (const typename scalar_from_svalue<ConcreteValueType>::key &k)
    1749              : {
    1750       741307 :   switch (k.m_sval->get_kind ())
    1751              :     {
    1752              :     default:
    1753              :       break;
    1754              : 
    1755       672356 :     case SK_CONSTANT:
    1756              :       {
    1757       672356 :         tree cst = as_a<const constant_svalue *> (k.m_sval)->get_constant ();
    1758       672356 :         switch (TREE_CODE (cst))
    1759              :           {
    1760              :           default:
    1761              :             break;
    1762              : 
    1763       672356 :           case INTEGER_CST:
    1764              :             return &get_concrete
    1765       672356 :               (ConcreteValueType (convert_from_integer_cst (cst)));
    1766              :           }
    1767              :       }
    1768              :       break;
    1769              :     }
    1770              : 
    1771              :   return nullptr;
    1772              : }
    1773              : 
    1774              : template<typename ConcreteValueType>
    1775              : const scalar<ConcreteValueType> &
    1776       741307 : scalar_manager<ConcreteValueType>::get_scalar_from_svalue (const svalue &sval)
    1777              : {
    1778       741307 :   typename scalar_from_svalue<ConcreteValueType>::key k {&sval};
    1779              : 
    1780       741307 :   if (auto *s = maybe_fold_scalar_from_svalue (k))
    1781              :     return *s;
    1782              : 
    1783        68951 :   auto iter = m_scalar_from_svalue_map.find (k);
    1784        68951 :   if (iter != m_scalar_from_svalue_map.end ())
    1785        60819 :     return *(iter->second);
    1786              : 
    1787         8132 :   auto s = std::make_unique<scalar_from_svalue<ConcreteValueType>>
    1788         8132 :     (alloc_node_id (),
    1789              :      *this,
    1790              :      k);
    1791         8132 :   auto &result = *s.get ();
    1792         8132 :   m_scalar_from_svalue_map.emplace (std::move (k), std::move (s));
    1793         8132 :   return result;
    1794         8132 : }
    1795              : 
    1796              : template
    1797              : const scalar<integer> &
    1798              : scalar_manager<integer>::get_scalar_from_svalue (const svalue &sval);
    1799              : 
    1800              : template
    1801              : const scalar<integer_with_unit<units::byte>> &
    1802              : scalar_manager<integer_with_unit<units::byte>>::get_scalar_from_svalue (const svalue &sval);
    1803              : 
    1804              : // class scalar_managers
    1805              : 
    1806              : template <>
    1807              : scalar_manager<integer_with_unit<units::bit>> &
    1808           24 : scalar_managers::get_manager<integer_with_unit<units::bit>> ()
    1809              : {
    1810           24 :   return m_bit_mgr;
    1811              : }
    1812              : 
    1813              : template <>
    1814              : scalar_manager<integer_with_unit<units::byte>> &
    1815           24 : scalar_managers::get_manager<integer_with_unit<units::byte>> ()
    1816              : {
    1817           24 :   return m_byte_mgr;
    1818              : }
    1819              : 
    1820              : template <>
    1821              : scalar_manager<integer> &
    1822           24 : scalar_managers::get_manager<integer> ()
    1823              : {
    1824           24 :   return m_unitless_mgr;
    1825              : }
    1826              : 
    1827              : template<typename ConcreteValueType>
    1828              : scalar_manager<integer> &
    1829        67663 : scalar_manager<ConcreteValueType>::get_unitless_manager ()
    1830              : {
    1831        67663 :   return m_node_mgr.get_scalars ().get_unitless ();
    1832              : }
    1833              : 
    1834              : template
    1835              : scalar_manager<integer> &
    1836              : scalar_manager<concrete_bit_offset>::get_unitless_manager ();
    1837              : 
    1838              : template
    1839              : scalar_manager<integer> &
    1840              : scalar_manager<concrete_byte_offset>::get_unitless_manager ();
    1841              : 
    1842              : template<>
    1843              : scalar_manager<integer> &
    1844         1370 : scalar_manager<integer>::get_unitless_manager ()
    1845              : {
    1846         1370 :   return *this;
    1847              : }
    1848              : 
    1849              : template <>
    1850              : const scalar<concrete_bit_size> &
    1851        11042 : scalar_manager<concrete_bit_size>::get_unknown ()
    1852              : {
    1853        11042 :   auto &mgr = m_node_mgr.get_svalue_manager ();
    1854        11042 :   auto *sval = mgr.get_or_create_unknown_svalue (NULL_TREE);
    1855        11042 :   return get_scalar_from_svalue (*sval);
    1856              : }
    1857              : 
    1858              : template <>
    1859              : const scalar<concrete_byte_size> &
    1860          401 : scalar_manager<concrete_byte_size>::get_unknown ()
    1861              : {
    1862          401 :   auto &mgr = m_node_mgr.get_svalue_manager ();
    1863          401 :   auto *sval = mgr.get_or_create_unknown_svalue (size_type_node);
    1864          401 :   return get_scalar_from_svalue (*sval);
    1865              : }
    1866              : 
    1867              : template <>
    1868              : const scalar<integer> &
    1869          136 : scalar_manager<integer>::get_unknown ()
    1870              : {
    1871          136 :   auto &mgr = m_node_mgr.get_svalue_manager ();
    1872          136 :   auto *sval = mgr.get_or_create_unknown_svalue (size_type_node);
    1873          136 :   return get_scalar_from_svalue (*sval);
    1874              : }
    1875              : 
    1876              : #if CHECKING_P
    1877              : 
    1878              : template <typename ConcreteValueType>
    1879              : const scalar<ConcreteValueType> &
    1880          256 : scalar_manager<ConcreteValueType>::get_named_test_scalar (std::string name)
    1881              : {
    1882          256 :   auto obj
    1883              :     = std::make_unique<named_test_scalar<ConcreteValueType>> (*this,
    1884              :                                                               std::move (name));
    1885          256 :   auto &result = *obj.get ();
    1886          256 :   m_named_test_scalars.push_back (std::move (obj));
    1887          512 :   return result;
    1888          256 : }
    1889              : 
    1890              : template
    1891              : const scalar<integer> &
    1892              : scalar_manager<integer>::get_named_test_scalar (std::string name);
    1893              : 
    1894              : template
    1895              : const scalar<concrete_bit_offset> &
    1896              : scalar_manager<concrete_bit_offset>::get_named_test_scalar (std::string name);
    1897              : 
    1898              : template
    1899              : const scalar<concrete_byte_offset> &
    1900              : scalar_manager<concrete_byte_offset>::get_named_test_scalar (std::string name);
    1901              : 
    1902              : #endif /* #if CHECKING_P */
    1903              : 
    1904              : template<>
    1905              : const symbolic_bit_size *
    1906       999872 : scalar_manager<concrete_bit_size>::
    1907              : maybe_fold_bits_from_bytes (const symbolic_byte_size &bytes)
    1908              : {
    1909       999872 :   if (auto *conc = bytes.dyn_cast_concrete_scalar ())
    1910       990147 :     return &get_concrete
    1911       990147 :       (concrete_bit_size (conc->get_concrete ().get_integer ()
    1912      2970441 :                           * BITS_PER_UNIT));
    1913              : 
    1914         9725 :   if (bytes.is_unknown_p ())
    1915          810 :     return &get_unknown ();
    1916              : 
    1917              :   return nullptr;
    1918              : }
    1919              : 
    1920              : template<>
    1921              : const symbolic_bit_size &
    1922       999872 : scalar_manager<concrete_bit_size>::
    1923              : get_bits_from_bytes (const symbolic_byte_size &bytes)
    1924              : {
    1925       999872 :   if (auto *folded = maybe_fold_bits_from_bytes (bytes))
    1926              :     return *folded;
    1927              : 
    1928         8915 :   bits_from_bytes_scalar::key k {&bytes};
    1929         8915 :   auto iter = m_bits_from_bytes_map.find (k);
    1930         8915 :   if (iter != m_bits_from_bytes_map.end ())
    1931         3715 :     return *(iter->second);
    1932              : 
    1933         5200 :   auto s = std::make_unique<bits_from_bytes_scalar>
    1934        10400 :     (alloc_node_id (),
    1935              :      *this,
    1936         5200 :      k);
    1937         5200 :   auto &result = *s.get ();
    1938         5200 :   m_bits_from_bytes_map.emplace (std::move (k), std::move (s));
    1939         5200 :   return result;
    1940         5200 : }
    1941              : 
    1942              : } // namespace ana::symbolic
    1943              : 
    1944              : /* Fold
    1945              :      bytes_from_bits (A + ... + Z) to
    1946              :    to:
    1947              :      bytes_from_bits (A) + ... bytes_from_bits (Z)
    1948              :    if all of the terms themselves fold.  */
    1949              : 
    1950              : static const symbolic_byte_size *
    1951            5 : maybe_fold_bytes_from_bits_sum (const symbolic::sum<concrete_bit_size> &bit_sum)
    1952              : {
    1953            5 :   std::vector<const symbolic_byte_size *> byte_addends;
    1954              : 
    1955           10 :   for (size_t i = 0; i < bit_sum.get_num_symbolic_addends (); ++i)
    1956              :     {
    1957            5 :       auto &sym_addend_as_bits = bit_sum.get_symbolic_addend (i);
    1958            5 :       auto *sym_addend_as_bytes = maybe_bytes_from_bits (sym_addend_as_bits);
    1959            5 :       if (!sym_addend_as_bytes)
    1960            0 :         return nullptr;
    1961            5 :       byte_addends.push_back (sym_addend_as_bytes);
    1962              :     }
    1963              : 
    1964            5 :   if (auto *conc_addend_as_bits = bit_sum.get_any_concrete_addend ())
    1965              :     {
    1966            5 :       auto *conc_addend_as_bytes = maybe_bytes_from_bits (*conc_addend_as_bits);
    1967            5 :       if (!conc_addend_as_bytes)
    1968            5 :         return nullptr;
    1969            0 :       byte_addends.push_back (conc_addend_as_bytes);
    1970              :     }
    1971              : 
    1972            0 :   auto &bytes_mgr
    1973            0 :     = bit_sum.get_mgr ().get_node_manager ().get_scalars ().get_bytes ();
    1974            0 :   return &bytes_mgr.get_sum (std::move (byte_addends));
    1975            5 : }
    1976              : 
    1977              : const symbolic_byte_size *
    1978       132896 : maybe_bytes_from_bits (const symbolic_bit_size &bits)
    1979              : {
    1980       132896 :   if (auto *concrete_bits = bits.dyn_cast_concrete_scalar ())
    1981              :     {
    1982       104382 :       auto val = concrete_bits->get_concrete ();
    1983       104382 :       integer_with_unit<units::byte> concrete_bytes;
    1984       104382 :       if (maybe_bytes_from_bits (val, &concrete_bytes))
    1985              :         {
    1986       104190 :           auto &bytes_mgr
    1987       104190 :             = bits.get_mgr ().get_node_manager ().get_scalars ().get_bytes ();
    1988       104190 :           return &bytes_mgr.get_concrete (concrete_bytes);
    1989              :         }
    1990              :       else
    1991              :         return nullptr;
    1992       104382 :     }
    1993        28514 :   if (auto *b_from_b = bits.dyn_cast_bits_from_bytes_scalar ())
    1994        21607 :     return &b_from_b->get_bytes ();
    1995              : 
    1996         6907 :   if (auto *sum = bits.dyn_cast_sum ())
    1997            5 :     if (auto *folded = maybe_fold_bytes_from_bits_sum (*sum))
    1998              :       return folded;
    1999              : 
    2000         6907 :   if (auto *product = bits.dyn_cast_product ())
    2001            8 :     if (auto *lhs_as_bytes = maybe_bytes_from_bits (product->get_lhs ()))
    2002            8 :       return &((*lhs_as_bytes) * product->get_rhs ());
    2003              : 
    2004              :   return nullptr;
    2005              : }
    2006              : 
    2007              : #if CHECKING_P
    2008              : 
    2009              : namespace selftest {
    2010              : 
    2011              : using symbolic_bit_manager
    2012              :   = symbolic::scalar_manager<integer_with_unit<units::bit>>;
    2013              : using symbolic_byte_manager
    2014              :   = symbolic::scalar_manager<integer_with_unit<units::byte>>;
    2015              : 
    2016              : template <typename ConcreteValueType>
    2017              : void
    2018           28 : assert_scalar_dump_inline_eq (const ::selftest::location &loc,
    2019              :                               const symbolic::scalar<ConcreteValueType> &s,
    2020              :                               enum units::role role,
    2021              :                               const char *expected_str)
    2022              : {
    2023           28 :   pretty_printer pp;
    2024           28 :   pp_format_decoder (&pp) = default_tree_printer;
    2025           28 :   s.dump_inline_to_pp (&pp, role);
    2026           28 :   ASSERT_STREQ_AT (loc, pp_formatted_text (&pp), expected_str);
    2027           28 : }
    2028              : 
    2029              : #define ASSERT_SCALAR_DUMP_INLINE_EQ(SCALAR, ROLE, EXPECTED_STR)        \
    2030              :   SELFTEST_BEGIN_STMT                                                   \
    2031              :   assert_scalar_dump_inline_eq (SELFTEST_LOCATION, (SCALAR), (ROLE),    \
    2032              :                                 (EXPECTED_STR));                        \
    2033              :   SELFTEST_END_STMT
    2034              : 
    2035              : template<typename ConcreteValueType>
    2036              : static void
    2037           12 : test_basics ()
    2038              : {
    2039           12 :   symbolic::node_manager node_mgr;
    2040           12 :   auto &mgr = node_mgr.get_scalars ().get_manager<ConcreteValueType> ();
    2041              : 
    2042           12 :   auto &zero = mgr.get_concrete (ConcreteValueType (0));
    2043           12 :   ASSERT_EQ (zero.get_complexity ().m_num_nodes, 1);
    2044           12 :   ASSERT_EQ (zero.get_complexity ().m_max_depth, 1);
    2045              : 
    2046           12 :   ASSERT_TRUE (zero.is_zero_p ());
    2047           12 :   ASSERT_FALSE (zero.is_one_p ());
    2048           12 :   ASSERT_FALSE (zero.is_unknown_p ());
    2049              : 
    2050           12 :   auto &one = mgr.get_concrete (ConcreteValueType (1));
    2051           12 :   ASSERT_EQ (zero.get_complexity ().m_num_nodes, 1);
    2052           12 :   ASSERT_EQ (zero.get_complexity ().m_max_depth, 1);
    2053              : 
    2054           12 :   ASSERT_FALSE (one.is_zero_p ());
    2055           12 :   ASSERT_TRUE (one.is_one_p ());
    2056           12 :   ASSERT_FALSE (one.is_unknown_p ());
    2057              : 
    2058           12 :   auto &x = mgr.get_named_test_scalar ("x");
    2059           12 :   ASSERT_EQ (x.get_complexity ().m_num_nodes, 1);
    2060           12 :   ASSERT_EQ (x.get_complexity ().m_max_depth, 1);
    2061           12 :   ASSERT_FALSE (x.is_zero_p ());
    2062           12 :   ASSERT_FALSE (x.is_one_p ());
    2063           12 :   ASSERT_FALSE (x.is_unknown_p ());
    2064              : 
    2065           12 :   auto &unknown = mgr.get_unknown ();
    2066           12 :   ASSERT_FALSE (unknown.is_zero_p ());
    2067           12 :   ASSERT_FALSE (unknown.is_one_p ());
    2068           12 :   ASSERT_TRUE (unknown.is_unknown_p ());
    2069              : 
    2070           12 :   diagnostics::markup::lowering_options opts;
    2071           12 :   ASSERT_SYMBOLIC_NODE_MARKUP_TEXT_EQ (zero, opts, "0");
    2072           12 :   ASSERT_SYMBOLIC_NODE_MARKUP_TEXT_EQ (one, opts, "1");
    2073           12 :   ASSERT_SYMBOLIC_NODE_MARKUP_TEXT_EQ (x, opts, "x");
    2074              : 
    2075              :   // Verify that consolidation works
    2076           12 :   ASSERT_SAME_SYMBOLIC_NODE (mgr.get_concrete (ConcreteValueType (0)),
    2077              :                              zero);
    2078           12 :   ASSERT_SAME_SYMBOLIC_NODE (mgr.get_concrete (ConcreteValueType (1)),
    2079              :                              one);
    2080           12 :   ASSERT_DIFFERENT_SYMBOLIC_NODE (one, zero);
    2081           12 : }
    2082              : 
    2083              : /* Test addition, for scalars of the given unit.  */
    2084              : 
    2085              : template<typename ConcreteValueType>
    2086              : static void
    2087           12 : test_sums ()
    2088              : {
    2089           12 :   symbolic::node_manager node_mgr;
    2090           12 :   auto &mgr = node_mgr.get_scalars ().get_manager<ConcreteValueType> ();
    2091           12 :   auto &unitless_mgr = node_mgr.get_scalars ().get_unitless ();
    2092              : 
    2093           12 :   auto &x = mgr.get_named_test_scalar ("x");
    2094           12 :   auto &y = mgr.get_named_test_scalar ("y");
    2095           12 :   auto &z = mgr.get_named_test_scalar ("z");
    2096              : 
    2097           12 :   auto &zero = mgr.get_concrete (ConcreteValueType (0));
    2098              : 
    2099              :   /* concrete + concrete.  */
    2100              :   {
    2101           24 :     auto &sum = (mgr.get_concrete (ConcreteValueType (42))
    2102           24 :                  + mgr.get_concrete (ConcreteValueType (1)));
    2103           12 :     ASSERT_EQ (sum.dyn_cast_concrete_scalar ()->get_concrete (),
    2104              :                ConcreteValueType (43));
    2105           12 :     ASSERT_EQ (sum.get_complexity ().m_num_nodes, 1);
    2106           12 :     ASSERT_EQ (sum.get_complexity ().m_max_depth, 1);
    2107              :   }
    2108              : 
    2109              :   {
    2110              :     /* symbolic + concrete.  */
    2111           12 :     auto three = ConcreteValueType (3);
    2112           12 :     auto &x_plus_3 = (x + three);
    2113           12 :     ASSERT_EQ (x_plus_3.dyn_cast_sum ()->get_num_symbolic_addends (), 1);
    2114           12 :     ASSERT_SAME_SYMBOLIC_NODE
    2115              :       (x_plus_3.dyn_cast_sum ()->get_symbolic_addend (0),
    2116              :        x);
    2117           24 :     ASSERT_EQ (*x_plus_3.dyn_cast_sum ()->get_any_concrete_addend_value (),
    2118              :                three);
    2119           12 :     ASSERT_EQ (x_plus_3.get_complexity ().m_num_nodes, 3);
    2120           12 :     ASSERT_EQ (x_plus_3.get_complexity ().m_max_depth, 2);
    2121              : 
    2122           12 :     diagnostics::markup::lowering_options opts;
    2123           12 :     ASSERT_SYMBOLIC_NODE_MARKUP_TEXT_EQ (x_plus_3, opts, "x + 3");
    2124              : 
    2125              :     /* concrete + symbolic == symbolic + concrete.  */
    2126           12 :     auto &three_plus_x = (mgr.get_concrete (three) + x);
    2127           12 :     ASSERT_SAME_SYMBOLIC_NODE (three_plus_x, x_plus_3);
    2128           12 :     ASSERT_SYMBOLIC_NODE_MARKUP_TEXT_EQ (three_plus_x, opts, "x + 3");
    2129           12 :   }
    2130              : 
    2131              :   /* Additive identity.  */
    2132              :   {
    2133              :     /* X + 0 -> X.  */
    2134           12 :     ASSERT_SAME_SYMBOLIC_NODE ((x + zero), x);
    2135              : 
    2136              :     /* 0 + X -> X.  */
    2137           12 :     ASSERT_SAME_SYMBOLIC_NODE ((zero + x), x);
    2138              :   }
    2139              : 
    2140              :   /* symbolic + symbolic.  */
    2141              :   {
    2142           12 :     auto &x_plus_y = x + y;
    2143           12 :     ASSERT_EQ (x_plus_y.dyn_cast_sum ()->get_num_symbolic_addends (), 2);
    2144           12 :     ASSERT_SAME_SYMBOLIC_NODE
    2145              :       (x_plus_y.dyn_cast_sum ()->get_symbolic_addend (0),
    2146              :        x);
    2147           12 :     ASSERT_SAME_SYMBOLIC_NODE
    2148              :       (x_plus_y.dyn_cast_sum ()->get_symbolic_addend (1),
    2149              :        y);
    2150           12 :     ASSERT_EQ (x_plus_y.dyn_cast_sum ()->get_any_concrete_addend (), nullptr);
    2151           12 :     ASSERT_EQ (x_plus_y.get_complexity ().m_num_nodes, 3);
    2152           12 :     ASSERT_EQ (x_plus_y.get_complexity ().m_max_depth, 2);
    2153              : 
    2154              :     // Canonicalization of addition:
    2155           12 :     ASSERT_SAME_SYMBOLIC_NODE ((x + y), x_plus_y);
    2156              : 
    2157              :     /* X + Y == Y + X.  */
    2158           12 :     ASSERT_SAME_SYMBOLIC_NODE ((y + x), x_plus_y);
    2159              : 
    2160           12 :     diagnostics::markup::lowering_options opts;
    2161           12 :     ASSERT_SYMBOLIC_NODE_MARKUP_TEXT_EQ (x_plus_y, opts, "x + y");
    2162              :   }
    2163              : 
    2164              :   /* X + Y + Z.  */
    2165              :   {
    2166           12 :     auto &s = mgr.get_sum ({&x, &y, &z});
    2167           12 :     ASSERT_EQ (s.dyn_cast_sum ()->get_num_symbolic_addends (), 3);
    2168           12 :     ASSERT_SAME_SYMBOLIC_NODE (s.dyn_cast_sum ()->get_symbolic_addend (0),
    2169              :                                x);
    2170           12 :     ASSERT_SAME_SYMBOLIC_NODE (s.dyn_cast_sum ()->get_symbolic_addend (1),
    2171              :                                y);
    2172           12 :     ASSERT_SAME_SYMBOLIC_NODE (s.dyn_cast_sum ()->get_symbolic_addend (2),
    2173              :                                z);
    2174           12 :     ASSERT_EQ (s.dyn_cast_sum ()->get_any_concrete_addend (), nullptr);
    2175              : 
    2176           12 :     diagnostics::markup::lowering_options opts;
    2177           12 :     ASSERT_SYMBOLIC_NODE_MARKUP_TEXT_EQ (s, opts, "x + y + z");
    2178              :   }
    2179              : 
    2180              :   /* X + Y + Z == Z + 0 + Y + 0 + X.  */
    2181           12 :   ASSERT_SAME_SYMBOLIC_NODE (mgr.get_sum ({&x, &y, &z}),
    2182              :                              mgr.get_sum ({&z, &zero, &y, &zero, &x}));
    2183              : 
    2184              :   /* (X + 3) + 2 --> (X + 5).  */
    2185           12 :   ASSERT_SAME_SYMBOLIC_NODE (((x + ConcreteValueType (3))
    2186              :                               + ConcreteValueType (2)),
    2187              :                              (x + ConcreteValueType (5)));
    2188              : 
    2189              :   /* No addends: sum() --> 0.  */
    2190           12 :   ASSERT_SAME_SYMBOLIC_NODE (mgr.get_sum ({}),
    2191              :                              (mgr.get_concrete (ConcreteValueType(0))));
    2192              : 
    2193              :   /* One addend: sum(X) --> X.  */
    2194           12 :   ASSERT_SAME_SYMBOLIC_NODE (mgr.get_sum ({&x}),
    2195              :                              x);
    2196              : 
    2197              :   /* Unknown + anything --> unknown.  */
    2198           12 :   ASSERT_SAME_SYMBOLIC_NODE (mgr.get_sum ({&mgr.get_unknown (), &x}),
    2199              :                              mgr.get_unknown ());
    2200           12 :   ASSERT_SAME_SYMBOLIC_NODE (mgr.get_sum ({&x, &mgr.get_unknown ()}),
    2201              :                              mgr.get_unknown ());
    2202           12 :   ASSERT_SAME_SYMBOLIC_NODE
    2203              :     (mgr.get_sum ({&mgr.get_unknown (), &mgr.get_unknown ()}),
    2204              :      mgr.get_unknown ());
    2205              : 
    2206              :   /* X + X --> (X * 2).  */
    2207              :   {
    2208           12 :     auto &s = mgr.get_sum ({&x, &x});
    2209           12 :     ASSERT_SAME_SYMBOLIC_NODE (s,
    2210              :                                (x * unitless_mgr.get_concrete (2)));
    2211           12 :     ASSERT_SAME_SYMBOLIC_NODE (s.dyn_cast_product ()->get_lhs (),
    2212              :                                x);
    2213           12 :     ASSERT_SAME_SYMBOLIC_NODE (s.dyn_cast_product ()->get_rhs (),
    2214              :                                unitless_mgr.get_concrete (2));
    2215              :   }
    2216              : 
    2217              :   /* X + X + X --> (X * 3).  */
    2218              :   {
    2219           12 :     auto &s = mgr.get_sum ({&x, &x, &x});
    2220           12 :     ASSERT_SAME_SYMBOLIC_NODE (s,
    2221              :                                (x * unitless_mgr.get_concrete (3)));
    2222           12 :     ASSERT_SAME_SYMBOLIC_NODE (s.dyn_cast_product ()->get_lhs (),
    2223              :                                x);
    2224           12 :     ASSERT_SAME_SYMBOLIC_NODE (s.dyn_cast_product ()->get_rhs (),
    2225              :                                unitless_mgr.get_concrete (3));
    2226              :   }
    2227              : 
    2228              :   /* X + Y + X + Y --> (X * 2) + (Y * 2).  */
    2229           12 :   ASSERT_SAME_SYMBOLIC_NODE (mgr.get_sum ({&x, &y, &x, &y}),
    2230              :                              (x * unitless_mgr.get_concrete (2)
    2231              :                               + y * unitless_mgr.get_concrete (2)));
    2232              : 
    2233              :   /* X + Y + X + X + Y --> (X * 3) + (Y * 2).  */
    2234           12 :   ASSERT_SAME_SYMBOLIC_NODE (mgr.get_sum ({&x, &y, &x, &x, &y}),
    2235              :                              (x * unitless_mgr.get_concrete (3)
    2236              :                               + y * unitless_mgr.get_concrete (2)));
    2237              : 
    2238              :   /* The example from the comment in the header:
    2239              :      3x + 4y + 5 - (2x + 5y + 6).  */
    2240           12 :   ASSERT_SAME_SYMBOLIC_NODE
    2241              :     ((((x * unitless_mgr.get_concrete (3))
    2242              :        + (y * unitless_mgr.get_concrete (4))
    2243              :        + mgr.get_concrete (ConcreteValueType(5)))
    2244              :       - ((x * unitless_mgr.get_concrete (2))
    2245              :          + (y * unitless_mgr.get_concrete (5))
    2246              :          + mgr.get_concrete (ConcreteValueType(6)))),
    2247              :      x - (y + mgr.get_concrete (ConcreteValueType(1))));
    2248           12 : }
    2249              : 
    2250              : /* Test multiplication, for scalars of the given type.  */
    2251              : 
    2252              : template<typename ConcreteValueType>
    2253              : static void
    2254           12 : test_products ()
    2255              : {
    2256           12 :   symbolic::node_manager node_mgr;
    2257           12 :   auto &unitless_mgr = node_mgr.get_scalars ().get_unitless ();
    2258           12 :   auto &unit_mgr = node_mgr.get_scalars ().get_manager<ConcreteValueType> ();
    2259              : 
    2260           12 :   auto &x = unit_mgr.get_named_test_scalar ("x");
    2261              : 
    2262           12 :   auto &a = unitless_mgr.get_named_test_scalar ("a");
    2263           12 :   auto &b = unitless_mgr.get_named_test_scalar ("b");
    2264              : 
    2265              :   /* Multiplication by zero: X * 0 --> 0.  */
    2266              :   {
    2267              :     // e.g. "x bytes * 0 --> 0 bytes":
    2268           12 :     ASSERT_SAME_SYMBOLIC_NODE ((x * unitless_mgr.get_concrete (0)),
    2269              :                                unit_mgr.get_concrete (ConcreteValueType (0)));
    2270              : 
    2271              :     // e.g. "0 bytes * a --> 0 bytes":
    2272           12 :     ASSERT_SAME_SYMBOLIC_NODE
    2273              :       ((unit_mgr.get_concrete (ConcreteValueType (0)) * a),
    2274              :        unit_mgr.get_concrete (ConcreteValueType (0)));
    2275              : 
    2276              :     // 0 * unknown --> 0:
    2277           12 :     ASSERT_SAME_SYMBOLIC_NODE
    2278              :       (unit_mgr.get_concrete (ConcreteValueType (0))
    2279              :        * unitless_mgr.get_unknown (),
    2280              :        unit_mgr.get_concrete (ConcreteValueType (0)));
    2281           12 :     ASSERT_SAME_SYMBOLIC_NODE
    2282              :       (unit_mgr.get_unknown () * unitless_mgr.get_concrete (0),
    2283              :        unit_mgr.get_concrete (ConcreteValueType (0)));
    2284              :   }
    2285              : 
    2286              :   /* Multiplicative identity.  */
    2287              :   {
    2288              :     /* X * 1 --> X.  */
    2289              :     // e.g. "x bytes * 1 --> x bytes":
    2290           12 :     ASSERT_SAME_SYMBOLIC_NODE ((x * unitless_mgr.get_concrete (1)),
    2291              :                                x);
    2292              : 
    2293              :     /* 1 unit * X for various X.  */
    2294              :     {
    2295              :       // The constant case e.g. 1 byte * 5 --> 5 bytes
    2296           12 :       ASSERT_SAME_SYMBOLIC_NODE
    2297              :         ((unit_mgr.get_concrete (ConcreteValueType (1))
    2298              :           * unitless_mgr.get_concrete (5)),
    2299              :          unit_mgr.get_concrete (ConcreteValueType (5)));
    2300              : 
    2301              :       // The product cases e.g. 1 byte * (a * 3) --> 3 bytes * a
    2302           12 :       ASSERT_SAME_SYMBOLIC_NODE
    2303              :         ((unit_mgr.get_concrete (ConcreteValueType (1))
    2304              :           * (a * unitless_mgr.get_concrete (3))),
    2305              :          (unit_mgr.get_concrete (ConcreteValueType (3))
    2306              :           * a));
    2307           12 :       ASSERT_SAME_SYMBOLIC_NODE
    2308              :         ((unit_mgr.get_concrete (ConcreteValueType (1))
    2309              :           * (unitless_mgr.get_concrete (3) * a)),
    2310              :          (unit_mgr.get_concrete (ConcreteValueType (3))
    2311              :           * a));
    2312              :     }
    2313              :   }
    2314              : 
    2315              :   /* concrete * concrete --> concrete */
    2316              :   {
    2317              :     // e.g. "4 bytes * 3 --> 12 bytes":
    2318           24 :     auto &product = (unit_mgr.get_concrete (ConcreteValueType (4))
    2319           24 :                      * unitless_mgr.get_concrete (3));
    2320           12 :     ASSERT_EQ (product.dyn_cast_concrete_scalar ()->get_concrete (),
    2321              :                ConcreteValueType (12));
    2322              :   }
    2323              : 
    2324              :   /* Distributive property: (X + 8) units * 2 --> (X * 2) units + 16 units.  */
    2325           12 :   ASSERT_SAME_SYMBOLIC_NODE
    2326              :     (((x + unit_mgr.get_concrete (ConcreteValueType (8)))
    2327              :       * unitless_mgr.get_concrete (2)),
    2328              :      ((x * unitless_mgr.get_concrete (2))
    2329              :       + unit_mgr.get_concrete (ConcreteValueType (16))));
    2330              : 
    2331              :   /* Chained multiplication: (X * 3) units * 2 --> (X * 6) units.  */
    2332           12 :   ASSERT_SAME_SYMBOLIC_NODE ((x * unitless_mgr.get_concrete (3))
    2333              :                              * unitless_mgr.get_concrete (2),
    2334              :                              (x * unitless_mgr.get_concrete (6)));
    2335              : 
    2336              :   /* Chained multiplication: (X units * A) * B --> (X units) * (A * B).  */
    2337           12 :   ASSERT_SAME_SYMBOLIC_NODE ((x * a) * b,
    2338              :                              x * (a * b));
    2339              : 
    2340              :   // unknown * a --> unknown:
    2341           12 :   ASSERT_SAME_SYMBOLIC_NODE (unit_mgr.get_unknown () * a,
    2342              :                              unit_mgr.get_unknown ());
    2343           12 :   ASSERT_SAME_SYMBOLIC_NODE (x * unitless_mgr.get_unknown (),
    2344              :                              unit_mgr.get_unknown ());
    2345              : 
    2346              :   /* (1 unit) * unitless_from_sval(SVAL) --> with_unit_from_sval(SVAL).  */
    2347           12 :   placeholder_svalue sval (node_mgr.alloc_node_id (), size_type_node, "sval");
    2348           12 :   ASSERT_SAME_SYMBOLIC_NODE
    2349              :     (unit_mgr.get_concrete (ConcreteValueType (1))
    2350              :      * unitless_mgr.get_scalar_from_svalue (sval),
    2351              :      unit_mgr.get_scalar_from_svalue (sval));
    2352           12 : }
    2353              : 
    2354              : /* Test negation, for scalars of the given type.  */
    2355              : 
    2356              : template<typename ConcreteValueType>
    2357              : static void
    2358           12 : test_negation ()
    2359              : {
    2360           12 :   symbolic::node_manager node_mgr;
    2361           12 :   auto &unit_mgr = node_mgr.get_scalars ().get_manager<ConcreteValueType> ();
    2362           12 :   auto &unitless_mgr = node_mgr.get_scalars ().get_unitless ();
    2363              : 
    2364           12 :   auto &concrete = unit_mgr.get_concrete (ConcreteValueType (42));
    2365           12 :   ASSERT_SAME_SYMBOLIC_NODE (-concrete,
    2366              :                              unit_mgr.get_concrete (ConcreteValueType (-42)));
    2367              : 
    2368           12 :   auto &x = unit_mgr.get_named_test_scalar ("x");
    2369           12 :   ASSERT_SAME_SYMBOLIC_NODE (-x, x * unitless_mgr.get_concrete (-1));
    2370              : 
    2371           12 :   ASSERT_SAME_SYMBOLIC_NODE (-unit_mgr.get_unknown (), unit_mgr.get_unknown ());
    2372              : 
    2373           12 :   auto &a = unitless_mgr.get_named_test_scalar ("a");
    2374           12 :   ASSERT_SAME_SYMBOLIC_NODE
    2375              :     (-(unit_mgr.get_concrete (ConcreteValueType (3)) * a),
    2376              :      unit_mgr.get_concrete (ConcreteValueType (-3)) * a);
    2377              : 
    2378           12 :   ASSERT_SYMBOLIC_NODE_MARKUP_TEXT_EQ
    2379              :     (-x,
    2380              :      diagnostics::selftest::get_ascii_markup_opts (),
    2381              :      "-x");
    2382           12 :   ASSERT_SYMBOLIC_NODE_MARKUP_TEXT_EQ
    2383              :     (-x,
    2384              :      diagnostics::selftest::get_unicode_markup_opts (),
    2385              :      MARKUP_UNICODE_SYMBOL_UNARY_NEGATION "x");
    2386           12 : }
    2387              : 
    2388              : /* Test subtraction, for scalars of the given type.  */
    2389              : 
    2390              : template<typename ConcreteValueType>
    2391              : static void
    2392           12 : test_differences ()
    2393              : {
    2394           12 :   symbolic::node_manager node_mgr;
    2395           12 :   auto &unitless_mgr = node_mgr.get_scalars ().get_unitless ();
    2396           12 :   auto &unit_mgr = node_mgr.get_scalars ().get_manager<ConcreteValueType> ();
    2397              : 
    2398           12 :   auto &x = unit_mgr.get_named_test_scalar ("x");
    2399           12 :   auto &y = unit_mgr.get_named_test_scalar ("y");
    2400              : 
    2401           12 :   auto &zero = unit_mgr.get_concrete (ConcreteValueType (0));
    2402              : 
    2403           12 :   auto &a = unitless_mgr.get_named_test_scalar ("a");
    2404              : 
    2405              :   // concrete - concrete
    2406           12 :   ASSERT_SAME_SYMBOLIC_NODE
    2407              :     ((unit_mgr.get_concrete (ConcreteValueType (5))
    2408              :       - unit_mgr.get_concrete (ConcreteValueType (3))),
    2409              :      unit_mgr.get_concrete (ConcreteValueType (2)));
    2410              : 
    2411              :   // concrete - symbolic
    2412           12 :   ASSERT_SAME_SYMBOLIC_NODE
    2413              :     (unit_mgr.get_concrete (ConcreteValueType (5)) - x,
    2414              :      (-x) + unit_mgr.get_concrete (ConcreteValueType (5)));
    2415              : 
    2416              :   // symbolic - concrete
    2417           12 :   ASSERT_SAME_SYMBOLIC_NODE
    2418              :     (x - unit_mgr.get_concrete (ConcreteValueType (3)),
    2419              :      x + unit_mgr.get_concrete (ConcreteValueType (-3)));
    2420              : 
    2421              :   // symbolic - symbolic
    2422           12 :   ASSERT_SAME_SYMBOLIC_NODE (x - y, x + (-y));
    2423           12 :   ASSERT_SYMBOLIC_NODE_MARKUP_TEXT_EQ
    2424              :     (x - y,
    2425              :      diagnostics::selftest::get_ascii_markup_opts (),
    2426              :      "x - y");
    2427           12 :   ASSERT_SYMBOLIC_NODE_MARKUP_TEXT_EQ
    2428              :     (x - y,
    2429              :      diagnostics::selftest::get_unicode_markup_opts (),
    2430              :      "x " MARKUP_UNICODE_SYMBOL_SUBTRACTION " y");
    2431              : 
    2432              :   {
    2433           12 :     pretty_printer pp;
    2434           12 :     symbolic::pp_element_for_node e (x - y, true);
    2435           12 :     pp_printf (&pp, "before{%e}after", &e);
    2436           12 :     ASSERT_STREQ (pp_formatted_text (&pp), "before{x - y}after");
    2437           12 :   }
    2438              : 
    2439              :   // (X - X) --> 0
    2440           12 :   ASSERT_SAME_SYMBOLIC_NODE ((x - x),
    2441              :                              zero);
    2442              : 
    2443              :   // (unknown - 1) --> unknown
    2444           12 :   ASSERT_SAME_SYMBOLIC_NODE
    2445              :     (unit_mgr.get_unknown () - unit_mgr.get_concrete (ConcreteValueType (1)),
    2446              :      unit_mgr.get_unknown ());
    2447              :   // (unknown - unknown) --> unknown
    2448           12 :   ASSERT_SAME_SYMBOLIC_NODE
    2449              :     ((unit_mgr.get_unknown () - unit_mgr.get_unknown ()),
    2450              :      unit_mgr.get_unknown ());
    2451              : 
    2452              :   // (X - Y) + Y --> X
    2453           12 :   ASSERT_SAME_SYMBOLIC_NODE ((x - y) + y, x);
    2454              : 
    2455              :   // (X units * 5) - (X units * 3) --> (X units * 2)
    2456           12 :   ASSERT_SAME_SYMBOLIC_NODE
    2457              :     ((x * unitless_mgr.get_concrete (5)
    2458              :       - (x * unitless_mgr.get_concrete (3))),
    2459              :      x * unitless_mgr.get_concrete (2));
    2460              : 
    2461              :   // (5 units * a) - (3 units * a) --> (2 * a) units
    2462           12 :   ASSERT_SAME_SYMBOLIC_NODE
    2463              :     ((unit_mgr.get_concrete (ConcreteValueType (5)) * a)
    2464              :      - (unit_mgr.get_concrete (ConcreteValueType (3)) * a),
    2465              :      unit_mgr.get_concrete (ConcreteValueType (2)) * a);
    2466           12 : }
    2467              : 
    2468              : template<typename ConcreteValueType>
    2469              : static void
    2470           12 : test_comparisons ()
    2471              : {
    2472           12 :   symbolic::node_manager node_mgr;
    2473           12 :   auto &scalar_mgr = node_mgr.get_scalars ().get_manager<ConcreteValueType> ();
    2474              : 
    2475              :   /* Tests of get_total_ordering.  */
    2476              : 
    2477           12 :   auto &three = scalar_mgr.get_concrete (ConcreteValueType (3));
    2478           12 :   auto &four = scalar_mgr.get_concrete (ConcreteValueType (4));
    2479           12 :   auto &x = scalar_mgr.get_named_test_scalar ("x");
    2480           12 :   auto &y = scalar_mgr.get_named_test_scalar ("y");
    2481              : 
    2482              :   /* Concrete vs concrete.  */
    2483              :   {
    2484           12 :     ASSERT_TRUE (three == three);
    2485           12 :     ASSERT_FALSE (three == four);
    2486              : 
    2487           12 :     ASSERT_TRUE (three != four);
    2488           12 :     ASSERT_FALSE (three != three);
    2489              : 
    2490           12 :     ASSERT_TRUE (three < four);
    2491           12 :     ASSERT_FALSE (three < three);
    2492           12 :     ASSERT_FALSE (four < three);
    2493              : 
    2494           12 :     ASSERT_TRUE (three <= four);
    2495           12 :     ASSERT_TRUE (three <= three);
    2496           12 :     ASSERT_FALSE (four <= three);
    2497              : 
    2498           12 :     ASSERT_FALSE (three > four);
    2499           12 :     ASSERT_FALSE (three > three);
    2500           12 :     ASSERT_TRUE (four > three);
    2501              : 
    2502           12 :     ASSERT_FALSE (three >= four);
    2503           12 :     ASSERT_TRUE (three >= three);
    2504           12 :     ASSERT_TRUE (four >= three);
    2505              :   }
    2506              : 
    2507              :   /* Symbolic vs concrete and vice versa:
    2508              :      all concrete should be before all symbolic.  */
    2509              :   {
    2510           12 :     ASSERT_FALSE (three == x);
    2511              : 
    2512           12 :     ASSERT_TRUE (three != x);
    2513              : 
    2514           12 :     ASSERT_TRUE (three < x);
    2515           12 :     ASSERT_FALSE (x < three);
    2516              : 
    2517           12 :     ASSERT_TRUE (three <= x);
    2518           12 :     ASSERT_FALSE (x <= three);
    2519              : 
    2520           12 :     ASSERT_FALSE (three > x);
    2521           12 :     ASSERT_TRUE (x > three);
    2522              : 
    2523           12 :     ASSERT_FALSE (three >= x);
    2524           12 :     ASSERT_TRUE (x >= three);
    2525              :   }
    2526              : 
    2527              :   /* Symbolic vs symbolic.  */
    2528              :   {
    2529              :     /* Unfoldable: order by creation order.  */
    2530              :     {
    2531           12 :       ASSERT_TRUE (x == x);
    2532           12 :       ASSERT_FALSE (x == y);
    2533              : 
    2534           12 :       ASSERT_TRUE (x != y);
    2535           12 :       ASSERT_FALSE (x != x);
    2536              : 
    2537           12 :       ASSERT_TRUE (x < y);
    2538           12 :       ASSERT_FALSE (x < x);
    2539           12 :       ASSERT_FALSE (y < x);
    2540              : 
    2541           12 :       ASSERT_TRUE (x <= y);
    2542           12 :       ASSERT_TRUE (x <= x);
    2543           12 :       ASSERT_FALSE (y <= x);
    2544              : 
    2545           12 :       ASSERT_FALSE (x > y);
    2546           12 :       ASSERT_FALSE (x > x);
    2547           12 :       ASSERT_TRUE (y > x);
    2548              : 
    2549           12 :       ASSERT_FALSE (x >= y);
    2550           12 :       ASSERT_TRUE (x >= x);
    2551           12 :       ASSERT_TRUE (y >= x);
    2552              :     }
    2553              : 
    2554              :     /* Cases that have a constant difference and thus can be compared.  */
    2555              :     {
    2556           12 :       ASSERT_FALSE (x == x + three);
    2557              : 
    2558           12 :       ASSERT_TRUE (x != x + three);
    2559              : 
    2560           12 :       ASSERT_TRUE (x < x + three);
    2561           12 :       ASSERT_FALSE (x + three < x);
    2562              : 
    2563           12 :       ASSERT_TRUE (x <= x + three);
    2564           12 :       ASSERT_FALSE (x + three <= x);
    2565              : 
    2566           12 :       ASSERT_FALSE (x > x + three);
    2567           12 :       ASSERT_TRUE (x + three > x);
    2568              : 
    2569           12 :       ASSERT_FALSE (x >= x + three);
    2570           12 :       ASSERT_TRUE (x + three >= x);
    2571              :     }
    2572              :   }
    2573           12 : }
    2574              : 
    2575              : template<typename ConcreteValueType>
    2576              : static void
    2577           12 : per_concrete_value_type_scalar_tests ()
    2578              : {
    2579           12 :   test_basics<ConcreteValueType> ();
    2580           12 :   test_sums<ConcreteValueType> ();
    2581           12 :   test_products<ConcreteValueType> ();
    2582           12 :   test_negation<ConcreteValueType> ();
    2583           12 :   test_differences<ConcreteValueType> ();
    2584           12 :   test_comparisons<ConcreteValueType> ();
    2585           12 : }
    2586              : 
    2587              : /* Verify that symbolic::scalar subclasses are dumped correctly.
    2588              :    These can't be type-independent, as the strings mention the unit.  */
    2589              : 
    2590              : static void
    2591            4 : test_dumping_scalars ()
    2592              : {
    2593            4 :   symbolic::node_manager node_mgr;
    2594            4 :   auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
    2595            4 :   auto &byte_mgr = node_mgr.get_scalars ().get_bytes ();
    2596            4 :   auto &unitless_mgr  = node_mgr.get_scalars ().get_unitless ();
    2597              : 
    2598            4 :   auto &x = bit_mgr.get_named_test_scalar ("x");
    2599            4 :   auto &y = bit_mgr.get_named_test_scalar ("y");
    2600            4 :   auto &z = bit_mgr.get_named_test_scalar ("z");
    2601              : 
    2602            4 :   ASSERT_SYMBOLIC_NODE_DUMP_EQ (x, "named_test_scalar<bits>: 'x'\n");
    2603            4 :   ASSERT_SYMBOLIC_NODE_DUMP_EQ (bit_mgr.get_concrete (8_bits),
    2604              :                                 "concrete_scalar<bits>: 8\n");
    2605              : 
    2606            4 :   ASSERT_SYMBOLIC_NODE_DUMP_EQ
    2607              :     (x + y + z,
    2608              :      ("sum<bits>\n"
    2609              :       "+- named_test_scalar<bits>: 'x'\n"
    2610              :       "+- named_test_scalar<bits>: 'y'\n"
    2611              :       "`- named_test_scalar<bits>: 'z'\n"));
    2612            4 :   ASSERT_SYMBOLIC_NODE_DUMP_EQ
    2613              :     (x + y + z + 42_bits,
    2614              :      ("sum<bits>\n"
    2615              :       "+- named_test_scalar<bits>: 'x'\n"
    2616              :       "+- named_test_scalar<bits>: 'y'\n"
    2617              :       "+- named_test_scalar<bits>: 'z'\n"
    2618              :       "`- concrete_scalar<bits>: 42\n"));
    2619            4 :   ASSERT_SCALAR_DUMP_INLINE_EQ (x + y + z + 42_bits, units::role::offset,
    2620              :                                 "bit x + bit y + bit z + bit 42");
    2621            4 :   ASSERT_SCALAR_DUMP_INLINE_EQ (x + y + z + 42_bits, units::role::size,
    2622              :                                 "x bits + y bits + z bits + 42 bits");
    2623              : 
    2624            4 :   auto &m = byte_mgr.get_named_test_scalar ("m");
    2625            4 :   auto &n = byte_mgr.get_named_test_scalar ("n");
    2626            4 :   ASSERT_SYMBOLIC_NODE_DUMP_EQ (m, "named_test_scalar<bytes>: 'm'\n");
    2627            4 :   ASSERT_SYMBOLIC_NODE_DUMP_EQ (byte_mgr.get_concrete (10_bytes),
    2628              :                                 "concrete_scalar<bytes>: 10\n");
    2629            4 :   ASSERT_SYMBOLIC_NODE_DUMP_EQ
    2630              :     (m + n,
    2631              :      ("sum<bytes>\n"
    2632              :       "+- named_test_scalar<bytes>: 'm'\n"
    2633              :       "`- named_test_scalar<bytes>: 'n'\n"));
    2634            4 :   ASSERT_SCALAR_DUMP_INLINE_EQ (m + n, units::role::offset,
    2635              :                                 "byte m + byte n");
    2636            4 :   ASSERT_SCALAR_DUMP_INLINE_EQ (m + n, units::role::size,
    2637              :                                 "m bytes + n bytes");
    2638              : 
    2639            4 :   auto &a = unitless_mgr.get_named_test_scalar ("a");
    2640            4 :   auto &b = unitless_mgr.get_named_test_scalar ("b");
    2641            4 :   ASSERT_SYMBOLIC_NODE_DUMP_EQ (a, "named_test_scalar<integer>: 'a'\n");
    2642            4 :   ASSERT_SYMBOLIC_NODE_DUMP_EQ (b, "named_test_scalar<integer>: 'b'\n");
    2643            4 :   ASSERT_SYMBOLIC_NODE_DUMP_EQ
    2644              :     (a + b,
    2645              :      ("sum<integer>\n"
    2646              :       "+- named_test_scalar<integer>: 'a'\n"
    2647              :       "`- named_test_scalar<integer>: 'b'\n"));
    2648              : 
    2649              :   // Dump a complex example, showing a product
    2650            4 :   ASSERT_SYMBOLIC_NODE_DUMP_EQ
    2651              :     ((m * a) + (n * b),
    2652              :      ("sum<bytes>\n"
    2653              :       "+- product<bytes>\n"
    2654              :       "|  +- named_test_scalar<bytes>: 'm'\n"
    2655              :       "|  `- named_test_scalar<integer>: 'a'\n"
    2656              :       "`- product<bytes>\n"
    2657              :       "   +- named_test_scalar<bytes>: 'n'\n"
    2658              :       "   `- named_test_scalar<integer>: 'b'\n"));
    2659              : 
    2660            4 :   ASSERT_SCALAR_DUMP_INLINE_EQ
    2661              :     ((m * a) + (n * b),
    2662              :      units::role::size,
    2663              :      "m bytes * a + n bytes * b");
    2664            4 : }
    2665              : 
    2666              : /* Test an example showing multiple levels of nesting,
    2667              :    combining addition and multiplication.  */
    2668              : 
    2669              : static void
    2670            4 : test_example_1 ()
    2671              : {
    2672            4 :   symbolic::node_manager node_mgr;
    2673            4 :   auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
    2674            4 :   auto &unitless_mgr = node_mgr.get_scalars ().get_unitless ();
    2675              : 
    2676            4 :   auto &x = bit_mgr.get_named_test_scalar ("x");
    2677            4 :   auto &y = bit_mgr.get_named_test_scalar ("y");
    2678            4 :   auto &z = bit_mgr.get_named_test_scalar ("z");
    2679              : 
    2680            4 :   auto &a = unitless_mgr.get_named_test_scalar ("a");
    2681            4 :   auto &b = unitless_mgr.get_named_test_scalar ("b");
    2682              : 
    2683            4 :   auto &s = (x * a) + (y * b) + z;
    2684            4 :   ASSERT_SYMBOLIC_NODE_DUMP_EQ
    2685              :     (s,
    2686              :      ("sum<bits>\n"
    2687              :       "+- product<bits>\n"
    2688              :       "|  +- named_test_scalar<bits>: 'x'\n"
    2689              :       "|  `- named_test_scalar<integer>: 'a'\n"
    2690              :       "+- product<bits>\n"
    2691              :       "|  +- named_test_scalar<bits>: 'y'\n"
    2692              :       "|  `- named_test_scalar<integer>: 'b'\n"
    2693              :       "`- named_test_scalar<bits>: 'z'\n"));
    2694            4 :   s.to_json ();
    2695              : 
    2696            4 :   ASSERT_SYMBOLIC_NODE_MARKUP_TEXT_EQ
    2697              :     (s,
    2698              :      diagnostics::selftest::get_ascii_markup_opts (),
    2699              :      "(x * a) + (y * b) + z");
    2700              : 
    2701            4 :   ASSERT_SYMBOLIC_NODE_MARKUP_TEXT_EQ
    2702              :     (s,
    2703              :      diagnostics::selftest::get_unicode_markup_opts (),
    2704              :      ("(x " MARKUP_UNICODE_SYMBOL_MULTIPLICATION " a)"
    2705              :       " + (y " MARKUP_UNICODE_SYMBOL_MULTIPLICATION " b)"
    2706              :       " + z"));
    2707              : 
    2708            4 :   ASSERT_EQ (s.get_complexity ().m_num_nodes, 8);
    2709            4 :   ASSERT_EQ (s.get_complexity ().m_max_depth, 3);
    2710            4 : }
    2711              : 
    2712              : static void
    2713            4 : test_example_2 ()
    2714              : {
    2715              :   /* Consider:
    2716              :      |
    2717              :      |  struct coord { uint32_t x; uint32_t y; };
    2718              :      |  struct triangle { coord m_vertices[3]; };
    2719              :      |
    2720              :      |  ptr = malloc (sizeof (triangle) * n);
    2721              :      |
    2722              :      and tracking the size of the allocated buffer.  */
    2723              : 
    2724            4 :   symbolic::node_manager node_mgr;
    2725            4 :   auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
    2726            4 :   auto &byte_mgr = node_mgr.get_scalars ().get_bytes ();
    2727            4 :   auto &unitless_mgr  = node_mgr.get_scalars ().get_unitless ();
    2728              : 
    2729            4 :   auto &n = unitless_mgr.get_named_test_scalar ("n");
    2730              : 
    2731              :   /* In bytes.  */
    2732            4 :   {
    2733            4 :     auto &sizeof_x = byte_mgr.get_concrete (4_bytes);
    2734            4 :     auto &sizeof_y = byte_mgr.get_concrete (4_bytes);
    2735              : 
    2736            4 :     auto &sizeof_coord = sizeof_x + sizeof_y;
    2737            4 :     ASSERT_EQ (sizeof_coord.dyn_cast_concrete_scalar ()->get_concrete (),
    2738              :                8_bytes);
    2739              : 
    2740            4 :     auto &sizeof_triangle = sizeof_coord * unitless_mgr.get_concrete (3);
    2741            4 :     ASSERT_EQ (sizeof_triangle.dyn_cast_concrete_scalar ()->get_concrete (),
    2742              :                24_bytes);
    2743              : 
    2744            4 :     auto &sizeof_buffer = sizeof_triangle * n;
    2745            4 :     ASSERT_SYMBOLIC_NODE_DUMP_EQ
    2746              :       (sizeof_buffer,
    2747              :        ("product<bytes>\n"
    2748              :         "+- concrete_scalar<bytes>: 24\n"
    2749              :         "`- named_test_scalar<integer>: 'n'\n"));
    2750              : 
    2751            4 :     ASSERT_SCALAR_DUMP_INLINE_EQ
    2752              :       (sizeof_buffer,
    2753              :        units::role::size,
    2754              :        "24 bytes * n");
    2755              : 
    2756            4 :     sizeof_buffer.to_json ();
    2757              :   }
    2758              : 
    2759              :   /* In bits.  */
    2760            4 :   {
    2761            4 :     auto &sizeof_x = bit_mgr.get_concrete (32_bits);
    2762            4 :     auto &sizeof_y = bit_mgr.get_concrete (32_bits);
    2763              : 
    2764            4 :     auto &sizeof_coord = sizeof_x + sizeof_y;
    2765            4 :     ASSERT_EQ (sizeof_coord.dyn_cast_concrete_scalar ()->get_concrete (),
    2766              :                64_bits);
    2767              : 
    2768            4 :     auto &sizeof_triangle = sizeof_coord * unitless_mgr.get_concrete (3);
    2769            4 :     ASSERT_EQ (sizeof_triangle.dyn_cast_concrete_scalar ()->get_concrete (),
    2770              :                192_bits);
    2771              : 
    2772            4 :     auto &sizeof_buffer = sizeof_triangle * n;
    2773            4 :     ASSERT_SYMBOLIC_NODE_DUMP_EQ
    2774              :       (sizeof_buffer,
    2775              :        ("bits_from_bytes_scalar\n"
    2776              :         "`- product<bytes>\n"
    2777              :         "   +- concrete_scalar<bytes>: 24\n"
    2778              :         "   `- named_test_scalar<integer>: 'n'\n"));
    2779              : 
    2780            4 :     ASSERT_SCALAR_DUMP_INLINE_EQ
    2781              :       (sizeof_buffer,
    2782              :        units::role::size,
    2783              :        "bits_from_bytes_scalar(24 bytes * n)");
    2784              : 
    2785            4 :     sizeof_buffer.to_json ();
    2786              :   }
    2787            4 : }
    2788              : 
    2789              : static void
    2790            4 : test_bits_from_bytes ()
    2791              : {
    2792            4 :   symbolic::node_manager node_mgr;
    2793            4 :   auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
    2794            4 :   auto &byte_mgr = node_mgr.get_scalars ().get_bytes ();
    2795            4 :   auto &unitless_mgr = node_mgr.get_scalars ().get_unitless ();
    2796              : 
    2797            4 :   auto &num_bytes_a = byte_mgr.get_named_test_scalar ("num_bytes_a");
    2798            4 :   auto &num_bytes_b = byte_mgr.get_named_test_scalar ("num_bytes_b");
    2799              : 
    2800            4 :   ASSERT_SAME_SYMBOLIC_NODE
    2801              :     (bits_from_bytes (num_bytes_a) + bits_from_bytes (num_bytes_b),
    2802              :      bits_from_bytes (num_bytes_a + num_bytes_b));
    2803              : 
    2804            4 :   ASSERT_SAME_SYMBOLIC_NODE
    2805              :     (bits_from_bytes (num_bytes_a)
    2806              :      + bits_from_bytes (num_bytes_b)
    2807              :      + bits_from_bytes (1_byte),
    2808              :      bits_from_bytes (num_bytes_a + num_bytes_b + 1_byte));
    2809              : 
    2810            4 :   ASSERT_SAME_SYMBOLIC_NODE
    2811              :     (bits_from_bytes (byte_mgr.get_unknown ()),
    2812              :      bit_mgr.get_unknown ());
    2813              : 
    2814            4 :   placeholder_svalue sval (node_mgr.alloc_node_id (), size_type_node, "sval");
    2815            4 :   auto &bytes_from_sval = byte_mgr.get_scalar_from_svalue (sval);
    2816            4 :   auto &unitless_from_sval = unitless_mgr.get_scalar_from_svalue (sval);
    2817            4 :   ASSERT_EQ (bits_from_bytes (bytes_from_sval).get_kind (),
    2818              :              symbolic::scalar<concrete_bit_offset>::kind::bits_from_bytes);
    2819            4 :   ASSERT_SAME_SYMBOLIC_NODE (bit_mgr.get_concrete (8_bits) * unitless_from_sval,
    2820              :                              bits_from_bytes (bytes_from_sval));
    2821            4 : }
    2822              : 
    2823              : static void
    2824            4 : test_maybe_bytes_from_bits ()
    2825              : {
    2826            4 :   symbolic::node_manager node_mgr;
    2827            4 :   auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
    2828            4 :   auto &byte_mgr = node_mgr.get_scalars ().get_bytes ();
    2829            4 :   auto &unitless_mgr = node_mgr.get_scalars ().get_unitless ();
    2830              : 
    2831            4 :   ASSERT_SAME_SYMBOLIC_NODE
    2832              :     (*maybe_bytes_from_bits (bit_mgr.get_concrete (8_bits)),
    2833              :      byte_mgr.get_concrete (1_byte));
    2834            4 :   ASSERT_EQ (maybe_bytes_from_bits (bit_mgr.get_concrete (3_bits)),
    2835              :              nullptr);
    2836              : 
    2837            4 :   placeholder_svalue sval (node_mgr.alloc_node_id (), size_type_node, "sval");
    2838            4 :   ASSERT_SAME_SYMBOLIC_NODE
    2839              :     (maybe_bytes_from_bits (bit_mgr.get_concrete (8_bits)
    2840              :                             * unitless_mgr.get_scalar_from_svalue (sval)),
    2841              :      &byte_mgr.get_scalar_from_svalue (sval));
    2842            4 : }
    2843              : 
    2844              : /* Run all of the selftests within this file.  */
    2845              : 
    2846              : void
    2847            4 : analyzer_symbolic_scalars_cc_tests ()
    2848              : {
    2849            4 :   per_concrete_value_type_scalar_tests<integer> ();
    2850            4 :   per_concrete_value_type_scalar_tests<integer_with_unit<units::bit>> ();
    2851            4 :   per_concrete_value_type_scalar_tests<integer_with_unit<units::byte>> ();
    2852              : 
    2853            4 :   test_dumping_scalars ();
    2854            4 :   test_example_1 ();
    2855            4 :   test_example_2 ();
    2856              : 
    2857            4 :   test_bits_from_bytes ();
    2858            4 :   test_maybe_bytes_from_bits ();
    2859            4 : }
    2860              : 
    2861              : } // namespace selftest
    2862              : 
    2863              : #endif /* CHECKING_P */
    2864              : 
    2865              : } // namespace ana
    2866              : 
    2867              : #endif /* #if ENABLE_ANALYZER */
        

Generated by: LCOV version 2.6-beta

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