LCOV - code coverage report
Current view: top level - gcc - range-op.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 92.4 % 2249 2079
Test Date: 2025-12-06 14:04:50 Functions: 86.2 % 196 169
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Code for range operators.
       2                 :             :    Copyright (C) 2017-2025 Free Software Foundation, Inc.
       3                 :             :    Contributed by Andrew MacLeod <amacleod@redhat.com>
       4                 :             :    and Aldy Hernandez <aldyh@redhat.com>.
       5                 :             : 
       6                 :             : This file is part of GCC.
       7                 :             : 
       8                 :             : GCC is free software; you can redistribute it and/or modify
       9                 :             : it under the terms of the GNU General Public License as published by
      10                 :             : the Free Software Foundation; either version 3, or (at your option)
      11                 :             : any later version.
      12                 :             : 
      13                 :             : GCC is distributed in the hope that it will be useful,
      14                 :             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      15                 :             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16                 :             : GNU General Public License for more details.
      17                 :             : 
      18                 :             : You should have received a copy of the GNU General Public License
      19                 :             : along with GCC; see the file COPYING3.  If not see
      20                 :             : <http://www.gnu.org/licenses/>.  */
      21                 :             : 
      22                 :             : #include "config.h"
      23                 :             : #include "system.h"
      24                 :             : #include "coretypes.h"
      25                 :             : #include "backend.h"
      26                 :             : #include "insn-codes.h"
      27                 :             : #include "rtl.h"
      28                 :             : #include "tree.h"
      29                 :             : #include "gimple.h"
      30                 :             : #include "cfghooks.h"
      31                 :             : #include "tree-pass.h"
      32                 :             : #include "ssa.h"
      33                 :             : #include "optabs-tree.h"
      34                 :             : #include "gimple-pretty-print.h"
      35                 :             : #include "diagnostic-core.h"
      36                 :             : #include "flags.h"
      37                 :             : #include "fold-const.h"
      38                 :             : #include "stor-layout.h"
      39                 :             : #include "calls.h"
      40                 :             : #include "cfganal.h"
      41                 :             : #include "gimple-iterator.h"
      42                 :             : #include "gimple-fold.h"
      43                 :             : #include "tree-eh.h"
      44                 :             : #include "gimple-walk.h"
      45                 :             : #include "tree-cfg.h"
      46                 :             : #include "wide-int.h"
      47                 :             : #include "value-relation.h"
      48                 :             : #include "range-op.h"
      49                 :             : #include "tree-ssa-ccp.h"
      50                 :             : #include "range-op-mixed.h"
      51                 :             : 
      52                 :             : // Instantiate the operators which apply to multiple types here.
      53                 :             : 
      54                 :             : operator_equal op_equal;
      55                 :             : operator_not_equal op_not_equal;
      56                 :             : operator_lt op_lt;
      57                 :             : operator_le op_le;
      58                 :             : operator_gt op_gt;
      59                 :             : operator_ge op_ge;
      60                 :             : operator_identity op_ident;
      61                 :             : operator_cst op_cst;
      62                 :             : operator_cast op_cast;
      63                 :             : operator_view op_view;
      64                 :             : operator_plus op_plus;
      65                 :             : operator_abs op_abs;
      66                 :             : operator_minus op_minus;
      67                 :             : operator_negate op_negate;
      68                 :             : operator_mult op_mult;
      69                 :             : operator_addr_expr op_addr;
      70                 :             : operator_bitwise_not op_bitwise_not;
      71                 :             : operator_bitwise_xor op_bitwise_xor;
      72                 :             : operator_bitwise_and op_bitwise_and;
      73                 :             : operator_bitwise_or op_bitwise_or;
      74                 :             : operator_min op_min;
      75                 :             : operator_max op_max;
      76                 :             : 
      77                 :             : // Instantaite a range operator table.
      78                 :             : range_op_table operator_table;
      79                 :             : 
      80                 :             : // Invoke the initialization routines for each class of range.
      81                 :             : 
      82                 :      288902 : range_op_table::range_op_table ()
      83                 :             : {
      84                 :      288902 :   initialize_integral_ops ();
      85                 :      288902 :   initialize_pointer_ops ();
      86                 :      288902 :   initialize_float_ops ();
      87                 :             : 
      88                 :      288902 :   set (EQ_EXPR, op_equal);
      89                 :      288902 :   set (NE_EXPR, op_not_equal);
      90                 :      288902 :   set (LT_EXPR, op_lt);
      91                 :      288902 :   set (LE_EXPR, op_le);
      92                 :      288902 :   set (GT_EXPR, op_gt);
      93                 :      288902 :   set (GE_EXPR, op_ge);
      94                 :      288902 :   set (SSA_NAME, op_ident);
      95                 :      288902 :   set (PAREN_EXPR, op_ident);
      96                 :      288902 :   set (OBJ_TYPE_REF, op_ident);
      97                 :      288902 :   set (REAL_CST, op_cst);
      98                 :      288902 :   set (INTEGER_CST, op_cst);
      99                 :      288902 :   set (NOP_EXPR, op_cast);
     100                 :      288902 :   set (CONVERT_EXPR, op_cast);
     101                 :      288902 :   set (VIEW_CONVERT_EXPR, op_view);
     102                 :      288902 :   set (FLOAT_EXPR, op_cast);
     103                 :      288902 :   set (FIX_TRUNC_EXPR, op_cast);
     104                 :      288902 :   set (PLUS_EXPR, op_plus);
     105                 :      288902 :   set (ABS_EXPR, op_abs);
     106                 :      288902 :   set (MINUS_EXPR, op_minus);
     107                 :      288902 :   set (NEGATE_EXPR, op_negate);
     108                 :      288902 :   set (MULT_EXPR, op_mult);
     109                 :      288902 :   set (ADDR_EXPR, op_addr);
     110                 :      288902 :   set (BIT_NOT_EXPR, op_bitwise_not);
     111                 :      288902 :   set (BIT_XOR_EXPR, op_bitwise_xor);
     112                 :      288902 :   set (BIT_AND_EXPR, op_bitwise_and);
     113                 :      288902 :   set (BIT_IOR_EXPR, op_bitwise_or);
     114                 :      288902 :   set (MIN_EXPR, op_min);
     115                 :      288902 :   set (MAX_EXPR, op_max);
     116                 :      288902 : }
     117                 :             : 
     118                 :             : // Instantiate a default range operator for opcodes with no entry.
     119                 :             : 
     120                 :             : range_operator default_operator;
     121                 :             : 
     122                 :             : // Create a default range_op_handler.
     123                 :             : 
     124                 :   907582493 : range_op_handler::range_op_handler ()
     125                 :             : {
     126                 :   907582493 :   m_operator = &default_operator;
     127                 :   907582493 : }
     128                 :             : 
     129                 :             : // Create a range_op_handler for CODE.  Use a default operatoer if CODE
     130                 :             : // does not have an entry.
     131                 :             : 
     132                 :  2011788977 : range_op_handler::range_op_handler (unsigned code)
     133                 :             : {
     134                 :  2011788977 :   m_operator = operator_table[code];
     135                 :  2011788977 :   if (!m_operator)
     136                 :   476086202 :     m_operator = &default_operator;
     137                 :  2011788977 : }
     138                 :             : 
     139                 :             : // Return TRUE if this handler has a non-default operator.
     140                 :             : 
     141                 :  3060740167 : range_op_handler::operator bool () const
     142                 :             : {
     143                 :  3060740167 :   return m_operator != &default_operator;
     144                 :             : }
     145                 :             : 
     146                 :             : // Return a pointer to the range operator assocaited with this handler.
     147                 :             : // If it is a default operator, return NULL.
     148                 :             : // This is the equivalent of indexing the range table.
     149                 :             : 
     150                 :             : range_operator *
     151                 :   542158959 : range_op_handler::range_op () const
     152                 :             : {
     153                 :   542158959 :   if (m_operator != &default_operator)
     154                 :   542158959 :     return m_operator;
     155                 :             :   return NULL;
     156                 :             : }
     157                 :             : 
     158                 :             : // Create a dispatch pattern for value range discriminators LHS, OP1, and OP2.
     159                 :             : // This is used to produce a unique value for each dispatch pattern.  Shift
     160                 :             : // values are based on the size of the m_discriminator field in value_range.h.
     161                 :             : 
     162                 :             : constexpr unsigned
     163                 :   663523430 : dispatch_trio (unsigned lhs, unsigned op1, unsigned op2)
     164                 :             : {
     165                 :   663523430 :   return ((lhs << 8) + (op1 << 4) + (op2));
     166                 :             : }
     167                 :             : 
     168                 :             : // These are the supported dispatch patterns. These map to the parameter list
     169                 :             : // of the routines in range_operator.  Note the last 3 characters are
     170                 :             : // shorthand for the LHS, OP1, and OP2 range discriminator class.
     171                 :             : // Reminder, single operand instructions use the LHS type for op2, even if
     172                 :             : // unused.  So FLOAT = INT would be RO_FIF.
     173                 :             : 
     174                 :             : const unsigned RO_III = dispatch_trio (VR_IRANGE, VR_IRANGE, VR_IRANGE);
     175                 :             : const unsigned RO_IFI = dispatch_trio (VR_IRANGE, VR_FRANGE, VR_IRANGE);
     176                 :             : const unsigned RO_IFF = dispatch_trio (VR_IRANGE, VR_FRANGE, VR_FRANGE);
     177                 :             : const unsigned RO_FFF = dispatch_trio (VR_FRANGE, VR_FRANGE, VR_FRANGE);
     178                 :             : const unsigned RO_FIF = dispatch_trio (VR_FRANGE, VR_IRANGE, VR_FRANGE);
     179                 :             : const unsigned RO_FII = dispatch_trio (VR_FRANGE, VR_IRANGE, VR_IRANGE);
     180                 :             : const unsigned RO_PPP = dispatch_trio (VR_PRANGE, VR_PRANGE, VR_PRANGE);
     181                 :             : const unsigned RO_PPI = dispatch_trio (VR_PRANGE, VR_PRANGE, VR_IRANGE);
     182                 :             : const unsigned RO_IPP = dispatch_trio (VR_IRANGE, VR_PRANGE, VR_PRANGE);
     183                 :             : const unsigned RO_IPI = dispatch_trio (VR_IRANGE, VR_PRANGE, VR_IRANGE);
     184                 :             : const unsigned RO_PIP = dispatch_trio (VR_PRANGE, VR_IRANGE, VR_PRANGE);
     185                 :             : const unsigned RO_PII = dispatch_trio (VR_PRANGE, VR_IRANGE, VR_IRANGE);
     186                 :             : 
     187                 :             : // Return a dispatch value for parameter types LHS, OP1 and OP2.
     188                 :             : 
     189                 :             : unsigned
     190                 :   663523430 : range_op_handler::dispatch_kind (const vrange &lhs, const vrange &op1,
     191                 :             :                                  const vrange& op2) const
     192                 :             : {
     193                 :   663523430 :   return dispatch_trio (lhs.m_discriminator, op1.m_discriminator,
     194                 :   663523430 :                         op2.m_discriminator);
     195                 :             : }
     196                 :             : 
     197                 :             : void
     198                 :           0 : range_op_handler::discriminator_fail (const vrange &r1,
     199                 :             :                                       const vrange &r2,
     200                 :             :                                       const vrange &r3) const
     201                 :             : {
     202                 :           0 :   const char name[] = "IPF";
     203                 :           0 :   gcc_checking_assert (r1.m_discriminator < sizeof (name) - 1);
     204                 :           0 :   gcc_checking_assert (r2.m_discriminator < sizeof (name) - 1);
     205                 :           0 :   gcc_checking_assert (r3.m_discriminator < sizeof (name) - 1);
     206                 :           0 :   fprintf (stderr,
     207                 :             :            "Unsupported operand combination in dispatch: RO_%c%c%c\n",
     208                 :           0 :            name[r1.m_discriminator],
     209                 :           0 :            name[r2.m_discriminator],
     210                 :           0 :            name[r3.m_discriminator]);
     211                 :           0 :   gcc_unreachable ();
     212                 :             : }
     213                 :             : 
     214                 :             : static inline bool
     215                 :             : has_pointer_operand_p (const vrange &r1, const vrange &r2, const vrange &r3)
     216                 :             : {
     217                 :             :   return is_a <prange> (r1) || is_a <prange> (r2) || is_a <prange> (r3);
     218                 :             : }
     219                 :             : 
     220                 :             : // Dispatch a call to fold_range based on the types of R, LH and RH.
     221                 :             : 
     222                 :             : bool
     223                 :   296897364 : range_op_handler::fold_range (vrange &r, tree type,
     224                 :             :                               const vrange &lh,
     225                 :             :                               const vrange &rh,
     226                 :             :                               relation_trio rel) const
     227                 :             : {
     228                 :   296897364 :   gcc_checking_assert (m_operator);
     229                 :             : #if CHECKING_P
     230                 :   296897364 :   if (!lh.undefined_p () && !rh.undefined_p ())
     231                 :   291202325 :     gcc_assert (m_operator->operand_check_p (type, lh.type (), rh.type ()));
     232                 :             : #endif
     233                 :   296897364 :   switch (dispatch_kind (r, lh, rh))
     234                 :             :     {
     235                 :   226045770 :       case RO_III:
     236                 :   226045770 :         return m_operator->fold_range (as_a <irange> (r), type,
     237                 :             :                                        as_a <irange> (lh),
     238                 :   226045770 :                                        as_a <irange> (rh), rel);
     239                 :      368727 :       case RO_IFI:
     240                 :      368727 :         return m_operator->fold_range (as_a <irange> (r), type,
     241                 :             :                                        as_a <frange> (lh),
     242                 :      368727 :                                        as_a <irange> (rh), rel);
     243                 :     2659029 :       case RO_IFF:
     244                 :     2659029 :         return m_operator->fold_range (as_a <irange> (r), type,
     245                 :             :                                        as_a <frange> (lh),
     246                 :     2659029 :                                        as_a <frange> (rh), rel);
     247                 :     7600437 :       case RO_FFF:
     248                 :     7600437 :         return m_operator->fold_range (as_a <frange> (r), type,
     249                 :             :                                        as_a <frange> (lh),
     250                 :     7600437 :                                        as_a <frange> (rh), rel);
     251                 :           0 :       case RO_FII:
     252                 :           0 :         return m_operator->fold_range (as_a <frange> (r), type,
     253                 :             :                                        as_a <irange> (lh),
     254                 :           0 :                                        as_a <irange> (rh), rel);
     255                 :      917576 :       case RO_FIF:
     256                 :      917576 :         return m_operator->fold_range (as_a <frange> (r), type,
     257                 :             :                                        as_a <irange> (lh),
     258                 :      917576 :                                        as_a <frange> (rh), rel);
     259                 :    18115361 :       case RO_PPP:
     260                 :    18115361 :         return m_operator->fold_range (as_a <prange> (r), type,
     261                 :             :                                        as_a <prange> (lh),
     262                 :    18115361 :                                        as_a <prange> (rh), rel);
     263                 :     9740488 :       case RO_PPI:
     264                 :     9740488 :         return m_operator->fold_range (as_a <prange> (r), type,
     265                 :             :                                        as_a <prange> (lh),
     266                 :     9740488 :                                        as_a <irange> (rh), rel);
     267                 :    18539385 :       case RO_IPP:
     268                 :    18539385 :         return m_operator->fold_range (as_a <irange> (r), type,
     269                 :             :                                        as_a <prange> (lh),
     270                 :    18539385 :                                        as_a <prange> (rh), rel);
     271                 :     1900607 :       case RO_PIP:
     272                 :     1900607 :         return m_operator->fold_range (as_a <prange> (r), type,
     273                 :             :                                        as_a <irange> (lh),
     274                 :     1900607 :                                        as_a <prange> (rh), rel);
     275                 :    11009952 :       case RO_IPI:
     276                 :    11009952 :         return m_operator->fold_range (as_a <irange> (r), type,
     277                 :             :                                        as_a <prange> (lh),
     278                 :    11009952 :                                        as_a <irange> (rh), rel);
     279                 :             :       default:
     280                 :             :         return false;
     281                 :             :     }
     282                 :             : }
     283                 :             : 
     284                 :             : // Dispatch a call to op1_range based on the types of R, LHS and OP2.
     285                 :             : 
     286                 :             : bool
     287                 :    92647855 : range_op_handler::op1_range (vrange &r, tree type,
     288                 :             :                              const vrange &lhs,
     289                 :             :                              const vrange &op2,
     290                 :             :                              relation_trio rel) const
     291                 :             : {
     292                 :    92647855 :   gcc_checking_assert (m_operator);
     293                 :    92647855 :   if (lhs.undefined_p ())
     294                 :             :     return false;
     295                 :             : #if CHECKING_P
     296                 :    92646639 :   if (!op2.undefined_p ())
     297                 :    92646527 :     gcc_assert (m_operator->operand_check_p (lhs.type (), type, op2.type ()));
     298                 :             : #endif
     299                 :    92646639 :   switch (dispatch_kind (r, lhs, op2))
     300                 :             :     {
     301                 :    80080255 :       case RO_III:
     302                 :    80080255 :         return m_operator->op1_range (as_a <irange> (r), type,
     303                 :             :                                       as_a <irange> (lhs),
     304                 :    80080255 :                                       as_a <irange> (op2), rel);
     305                 :      248081 :       case RO_IFI:
     306                 :      248081 :         return m_operator->op1_range (as_a <irange> (r), type,
     307                 :             :                                       as_a <frange> (lhs),
     308                 :      248081 :                                       as_a <irange> (op2), rel);
     309                 :      747003 :       case RO_PPP:
     310                 :      747003 :         return m_operator->op1_range (as_a <prange> (r), type,
     311                 :             :                                       as_a <prange> (lhs),
     312                 :      747003 :                                       as_a <prange> (op2), rel);
     313                 :     8416839 :       case RO_PIP:
     314                 :     8416839 :         return m_operator->op1_range (as_a <prange> (r), type,
     315                 :             :                                       as_a <irange> (lhs),
     316                 :     8416839 :                                       as_a <prange> (op2), rel);
     317                 :      472264 :       case RO_PPI:
     318                 :      472264 :         return m_operator->op1_range (as_a <prange> (r), type,
     319                 :             :                                       as_a <prange> (lhs),
     320                 :      472264 :                                       as_a <irange> (op2), rel);
     321                 :      263136 :       case RO_IPI:
     322                 :      263136 :         return m_operator->op1_range (as_a <irange> (r), type,
     323                 :             :                                       as_a <prange> (lhs),
     324                 :      263136 :                                       as_a <irange> (op2), rel);
     325                 :     1500725 :       case RO_FIF:
     326                 :     1500725 :         return m_operator->op1_range (as_a <frange> (r), type,
     327                 :             :                                       as_a <irange> (lhs),
     328                 :     1500725 :                                       as_a <frange> (op2), rel);
     329                 :      918336 :       case RO_FFF:
     330                 :      918336 :         return m_operator->op1_range (as_a <frange> (r), type,
     331                 :             :                                       as_a <frange> (lhs),
     332                 :      918336 :                                       as_a <frange> (op2), rel);
     333                 :             :       default:
     334                 :             :         return false;
     335                 :             :     }
     336                 :             : }
     337                 :             : 
     338                 :             : // Dispatch a call to op2_range based on the types of R, LHS and OP1.
     339                 :             : 
     340                 :             : bool
     341                 :    26619746 : range_op_handler::op2_range (vrange &r, tree type,
     342                 :             :                              const vrange &lhs,
     343                 :             :                              const vrange &op1,
     344                 :             :                              relation_trio rel) const
     345                 :             : {
     346                 :    26619746 :   gcc_checking_assert (m_operator);
     347                 :    26619746 :   if (lhs.undefined_p ())
     348                 :             :     return false;
     349                 :             : #if CHECKING_P
     350                 :    26619691 :   if (!op1.undefined_p ())
     351                 :    26619531 :     gcc_assert (m_operator->operand_check_p (lhs.type (), op1.type (), type));
     352                 :             : #endif
     353                 :    26619691 :   switch (dispatch_kind (r, lhs, op1))
     354                 :             :     {
     355                 :    20708374 :       case RO_III:
     356                 :    20708374 :         return m_operator->op2_range (as_a <irange> (r), type,
     357                 :             :                                       as_a <irange> (lhs),
     358                 :    20708374 :                                       as_a <irange> (op1), rel);
     359                 :     4762811 :       case RO_PIP:
     360                 :     4762811 :         return m_operator->op2_range (as_a <prange> (r), type,
     361                 :             :                                       as_a <irange> (lhs),
     362                 :     4762811 :                                       as_a <prange> (op1), rel);
     363                 :      207067 :       case RO_IPP:
     364                 :      207067 :         return m_operator->op2_range (as_a <irange> (r), type,
     365                 :             :                                       as_a <prange> (lhs),
     366                 :      207067 :                                       as_a <prange> (op1), rel);
     367                 :      565465 :       case RO_FIF:
     368                 :      565465 :         return m_operator->op2_range (as_a <frange> (r), type,
     369                 :             :                                       as_a <irange> (lhs),
     370                 :      565465 :                                       as_a <frange> (op1), rel);
     371                 :      375830 :       case RO_FFF:
     372                 :      375830 :         return m_operator->op2_range (as_a <frange> (r), type,
     373                 :             :                                       as_a <frange> (lhs),
     374                 :      375830 :                                       as_a <frange> (op1), rel);
     375                 :             :       default:
     376                 :             :         return false;
     377                 :             :     }
     378                 :             : }
     379                 :             : 
     380                 :             : // Dispatch a call to lhs_op1_relation based on the types of LHS, OP1 and OP2.
     381                 :             : 
     382                 :             : relation_kind
     383                 :   127354383 : range_op_handler::lhs_op1_relation (const vrange &lhs,
     384                 :             :                                     const vrange &op1,
     385                 :             :                                     const vrange &op2,
     386                 :             :                                     relation_kind rel) const
     387                 :             : {
     388                 :   127354383 :   gcc_checking_assert (m_operator);
     389                 :   127354383 :   switch (dispatch_kind (lhs, op1, op2))
     390                 :             :     {
     391                 :   102106064 :       case RO_III:
     392                 :   102106064 :         return m_operator->lhs_op1_relation (as_a <irange> (lhs),
     393                 :             :                                              as_a <irange> (op1),
     394                 :   102106064 :                                              as_a <irange> (op2), rel);
     395                 :     1438026 :       case RO_PPP:
     396                 :     1438026 :         return m_operator->lhs_op1_relation (as_a <prange> (lhs),
     397                 :             :                                              as_a <prange> (op1),
     398                 :     1438026 :                                              as_a <prange> (op2), rel);
     399                 :     6179005 :       case RO_IPP:
     400                 :     6179005 :         return m_operator->lhs_op1_relation (as_a <irange> (lhs),
     401                 :             :                                              as_a <prange> (op1),
     402                 :     6179005 :                                              as_a <prange> (op2), rel);
     403                 :     1441044 :       case RO_PII:
     404                 :     1441044 :         return m_operator->lhs_op1_relation (as_a <prange> (lhs),
     405                 :             :                                              as_a <irange> (op1),
     406                 :     1441044 :                                              as_a <irange> (op2), rel);
     407                 :     8412199 :       case RO_PPI:
     408                 :     8412199 :         return m_operator->lhs_op1_relation (as_a <prange> (lhs),
     409                 :             :                                              as_a <prange> (op1),
     410                 :     8412199 :                                              as_a <irange> (op2), rel);
     411                 :     1046154 :       case RO_IFF:
     412                 :     1046154 :         return m_operator->lhs_op1_relation (as_a <irange> (lhs),
     413                 :             :                                              as_a <frange> (op1),
     414                 :     1046154 :                                              as_a <frange> (op2), rel);
     415                 :     5814856 :       case RO_FFF:
     416                 :     5814856 :         return m_operator->lhs_op1_relation (as_a <frange> (lhs),
     417                 :             :                                              as_a <frange> (op1),
     418                 :     5814856 :                                              as_a <frange> (op2), rel);
     419                 :             :       default:
     420                 :             :         return VREL_VARYING;
     421                 :             :     }
     422                 :             : }
     423                 :             : 
     424                 :             : // Dispatch a call to lhs_op2_relation based on the types of LHS, OP1 and OP2.
     425                 :             : 
     426                 :             : relation_kind
     427                 :    37755650 : range_op_handler::lhs_op2_relation (const vrange &lhs,
     428                 :             :                                     const vrange &op1,
     429                 :             :                                     const vrange &op2,
     430                 :             :                                     relation_kind rel) const
     431                 :             : {
     432                 :    37755650 :   gcc_checking_assert (m_operator);
     433                 :    37755650 :   switch (dispatch_kind (lhs, op1, op2))
     434                 :             :     {
     435                 :    25883731 :       case RO_III:
     436                 :    25883731 :         return m_operator->lhs_op2_relation (as_a <irange> (lhs),
     437                 :             :                                              as_a <irange> (op1),
     438                 :    25883731 :                                              as_a <irange> (op2), rel);
     439                 :      169983 :       case RO_IFF:
     440                 :      169983 :         return m_operator->lhs_op2_relation (as_a <irange> (lhs),
     441                 :             :                                              as_a <frange> (op1),
     442                 :      169983 :                                              as_a <frange> (op2), rel);
     443                 :     3640040 :       case RO_FFF:
     444                 :     3640040 :         return m_operator->lhs_op2_relation (as_a <frange> (lhs),
     445                 :             :                                              as_a <frange> (op1),
     446                 :     3640040 :                                              as_a <frange> (op2), rel);
     447                 :             :       default:
     448                 :             :         return VREL_VARYING;
     449                 :             :     }
     450                 :             : }
     451                 :             : 
     452                 :             : // Dispatch a call to op1_op2_relation based on the type of LHS.
     453                 :             : 
     454                 :             : relation_kind
     455                 :    82205473 : range_op_handler::op1_op2_relation (const vrange &lhs,
     456                 :             :                                     const vrange &op1,
     457                 :             :                                     const vrange &op2) const
     458                 :             : {
     459                 :    82205473 :   gcc_checking_assert (m_operator);
     460                 :             : 
     461                 :    82205473 :   switch (dispatch_kind (lhs, op1, op2))
     462                 :             :     {
     463                 :    64144283 :       case RO_III:
     464                 :    64144283 :         return m_operator->op1_op2_relation (as_a <irange> (lhs),
     465                 :             :                                              as_a <irange> (op1),
     466                 :    64144283 :                                              as_a <irange> (op2));
     467                 :             : 
     468                 :    15185789 :       case RO_IPP:
     469                 :    15185789 :         return m_operator->op1_op2_relation (as_a <irange> (lhs),
     470                 :             :                                              as_a <prange> (op1),
     471                 :    15185789 :                                              as_a <prange> (op2));
     472                 :             : 
     473                 :     1893710 :       case RO_IFF:
     474                 :     1893710 :         return m_operator->op1_op2_relation (as_a <irange> (lhs),
     475                 :             :                                              as_a <frange> (op1),
     476                 :     1893710 :                                              as_a <frange> (op2));
     477                 :             : 
     478                 :      660990 :       case RO_FFF:
     479                 :      660990 :         return m_operator->op1_op2_relation (as_a <frange> (lhs),
     480                 :             :                                              as_a <frange> (op1),
     481                 :      660990 :                                              as_a <frange> (op2));
     482                 :             : 
     483                 :             :       default:
     484                 :             :         return VREL_VARYING;
     485                 :             :     }
     486                 :             : }
     487                 :             : 
     488                 :             : bool
     489                 :       44230 : range_op_handler::overflow_free_p (const vrange &lh,
     490                 :             :                                    const vrange &rh,
     491                 :             :                                    relation_trio rel) const
     492                 :             : {
     493                 :       44230 :   gcc_checking_assert (m_operator);
     494                 :       44230 :   switch (dispatch_kind (lh, lh, rh))
     495                 :             :     {
     496                 :       44230 :       case RO_III:
     497                 :       44230 :         return m_operator->overflow_free_p(as_a <irange> (lh),
     498                 :             :                                            as_a <irange> (rh),
     499                 :       44230 :                                            rel);
     500                 :             :       default:
     501                 :             :         return false;
     502                 :             :     }
     503                 :             : }
     504                 :             : 
     505                 :             : bool
     506                 :     9182830 : range_op_handler::operand_check_p (tree t1, tree t2, tree t3) const
     507                 :             : {
     508                 :     9182830 :   gcc_checking_assert (m_operator);
     509                 :     9182830 :   return m_operator->operand_check_p (t1, t2, t3);
     510                 :             : }
     511                 :             : 
     512                 :             : // Update the known bitmasks in R when applying the operation CODE to
     513                 :             : // LH and RH.
     514                 :             : 
     515                 :             : void
     516                 :   175039574 : update_known_bitmask (vrange &r, tree_code code,
     517                 :             :                       const vrange &lh, const vrange &rh)
     518                 :             : {
     519                 :   175039574 :   if (r.undefined_p () || lh.undefined_p () || rh.undefined_p ()
     520                 :   350074618 :       || r.singleton_p ())
     521                 :     9484029 :     return;
     522                 :             : 
     523                 :   165555545 :   widest_int widest_value, widest_mask;
     524                 :   165555545 :   tree type = r.type ();
     525                 :   165555545 :   signop sign = TYPE_SIGN (type);
     526                 :   165555545 :   int prec = TYPE_PRECISION (type);
     527                 :   165555545 :   irange_bitmask lh_bits = lh.get_bitmask ();
     528                 :   165555545 :   irange_bitmask rh_bits = rh.get_bitmask ();
     529                 :             : 
     530                 :   165555545 :   switch (get_gimple_rhs_class (code))
     531                 :             :     {
     532                 :    55488692 :     case GIMPLE_UNARY_RHS:
     533                 :    55488692 :       bit_value_unop (code, sign, prec, &widest_value, &widest_mask,
     534                 :    55488692 :                       TYPE_SIGN (lh.type ()),
     535                 :    55488692 :                       TYPE_PRECISION (lh.type ()),
     536                 :   110977844 :                       widest_int::from (lh_bits.value (),
     537                 :    55488692 :                                         TYPE_SIGN (lh.type ())),
     538                 :   110977384 :                       widest_int::from (lh_bits.mask (),
     539                 :    55488692 :                                         TYPE_SIGN (lh.type ())));
     540                 :    55488692 :       break;
     541                 :   110066853 :     case GIMPLE_BINARY_RHS:
     542                 :   220133706 :       bit_value_binop (code, sign, prec, &widest_value, &widest_mask,
     543                 :   110066853 :                        TYPE_SIGN (lh.type ()),
     544                 :   110066853 :                        TYPE_PRECISION (lh.type ()),
     545                 :   220134595 :                        widest_int::from (lh_bits.value (), sign),
     546                 :   220134595 :                        widest_int::from (lh_bits.mask (), sign),
     547                 :   110066853 :                        TYPE_SIGN (rh.type ()),
     548                 :   110066853 :                        TYPE_PRECISION (rh.type ()),
     549                 :   220134565 :                        widest_int::from (rh_bits.value (), sign),
     550                 :   220133706 :                        widest_int::from (rh_bits.mask (), sign));
     551                 :   110066853 :       break;
     552                 :           0 :     default:
     553                 :           0 :       gcc_unreachable ();
     554                 :             :     }
     555                 :             : 
     556                 :   165555545 :   wide_int mask = wide_int::from (widest_mask, prec, sign);
     557                 :   331111090 :   wide_int value = wide_int::from (widest_value, prec, sign);
     558                 :             :   // Bitmasks must have the unknown value bits cleared.
     559                 :   165555545 :   value &= ~mask;
     560                 :   331111090 :   irange_bitmask bm (value, mask);
     561                 :   165555545 :   r.update_bitmask (bm);
     562                 :   165556484 : }
     563                 :             : 
     564                 :             : // Return the upper limit for a type.
     565                 :             : 
     566                 :             : static inline wide_int
     567                 :    16708129 : max_limit (const_tree type)
     568                 :             : {
     569                 :    16708129 :   return irange_val_max (type);
     570                 :             : }
     571                 :             : 
     572                 :             : // Return the lower limit for a type.
     573                 :             : 
     574                 :             : static inline wide_int
     575                 :    19854998 : min_limit (const_tree type)
     576                 :             : {
     577                 :    19854998 :   return irange_val_min (type);
     578                 :             : }
     579                 :             : 
     580                 :             : // Return false if shifting by OP is undefined behavior.  Otherwise, return
     581                 :             : // true and the range it is to be shifted by.  This allows trimming out of
     582                 :             : // undefined ranges, leaving only valid ranges if there are any.
     583                 :             : 
     584                 :             : static inline bool
     585                 :     4983789 : get_shift_range (irange &r, tree type, const irange &op)
     586                 :             : {
     587                 :     4983789 :   if (op.undefined_p ())
     588                 :             :     return false;
     589                 :             : 
     590                 :             :   // Build valid range and intersect it with the shift range.
     591                 :     4983304 :   r.set (op.type (),
     592                 :     9966608 :          wi::shwi (0, TYPE_PRECISION (op.type ())),
     593                 :     4983304 :          wi::shwi (TYPE_PRECISION (type) - 1, TYPE_PRECISION (op.type ())));
     594                 :     4983304 :   r.intersect (op);
     595                 :             : 
     596                 :             :   // If there are no valid ranges in the shift range, returned false.
     597                 :     4983304 :   if (r.undefined_p ())
     598                 :             :     return false;
     599                 :             :   return true;
     600                 :             : }
     601                 :             : 
     602                 :             : // Default wide_int fold operation returns [MIN, MAX].
     603                 :             : 
     604                 :             : void
     605                 :           0 : range_operator::wi_fold (irange &r, tree type,
     606                 :             :                          const wide_int &lh_lb ATTRIBUTE_UNUSED,
     607                 :             :                          const wide_int &lh_ub ATTRIBUTE_UNUSED,
     608                 :             :                          const wide_int &rh_lb ATTRIBUTE_UNUSED,
     609                 :             :                          const wide_int &rh_ub ATTRIBUTE_UNUSED) const
     610                 :             : {
     611                 :           0 :   gcc_checking_assert (r.supports_type_p (type));
     612                 :           0 :   r.set_varying (type);
     613                 :           0 : }
     614                 :             : 
     615                 :             : // Call wi_fold when both op1 and op2 are equivalent. Further split small
     616                 :             : // subranges into constants.  This can provide better precision.
     617                 :             : // For x + y,  when x == y with a range of [0,4] instead of [0, 8] produce
     618                 :             : // [0,0][2, 2][4,4][6, 6][8, 8]
     619                 :             : // LIMIT is the maximum number of elements in range allowed before we
     620                 :             : // do not process them individually.
     621                 :             : 
     622                 :             : void
     623                 :       56892 : range_operator::wi_fold_in_parts_equiv (irange &r, tree type,
     624                 :             :                                         const wide_int &lh_lb,
     625                 :             :                                         const wide_int &lh_ub,
     626                 :             :                                         unsigned limit) const
     627                 :             : {
     628                 :       56892 :   int_range_max tmp;
     629                 :      113784 :   widest_int lh_range = wi::sub (widest_int::from (lh_ub, TYPE_SIGN (type)),
     630                 :      113784 :                                  widest_int::from (lh_lb, TYPE_SIGN (type)));
     631                 :             :   // if there are 1 to 8 values in the LH range, split them up.
     632                 :       56892 :   r.set_undefined ();
     633                 :      113784 :   if (lh_range >= 0 && lh_range < limit)
     634                 :             :     {
     635                 :       18507 :       for (unsigned x = 0; x <= lh_range; x++)
     636                 :             :         {
     637                 :       12730 :           wide_int val = lh_lb + x;
     638                 :       12730 :           wi_fold (tmp, type, val, val, val, val);
     639                 :       12730 :           r.union_ (tmp);
     640                 :       12730 :         }
     641                 :             :     }
     642                 :             :   // Otherwise just call wi_fold.
     643                 :             :   else
     644                 :       51115 :     wi_fold (r, type, lh_lb, lh_ub, lh_lb, lh_ub);
     645                 :       56892 : }
     646                 :             : 
     647                 :             : // Call wi_fold, except further split small subranges into constants.
     648                 :             : // This can provide better precision. For something   8 >> [0,1]
     649                 :             : // Instead of [8, 16], we will produce [8,8][16,16]
     650                 :             : 
     651                 :             : void
     652                 :   147385886 : range_operator::wi_fold_in_parts (irange &r, tree type,
     653                 :             :                                   const wide_int &lh_lb,
     654                 :             :                                   const wide_int &lh_ub,
     655                 :             :                                   const wide_int &rh_lb,
     656                 :             :                                   const wide_int &rh_ub) const
     657                 :             : {
     658                 :   147385886 :   int_range_max tmp;
     659                 :   294771772 :   widest_int rh_range = wi::sub (widest_int::from (rh_ub, TYPE_SIGN (type)),
     660                 :   294771772 :                                  widest_int::from (rh_lb, TYPE_SIGN (type)));
     661                 :   294771772 :   widest_int lh_range = wi::sub (widest_int::from (lh_ub, TYPE_SIGN (type)),
     662                 :   294771772 :                                  widest_int::from (lh_lb, TYPE_SIGN (type)));
     663                 :             :   // If there are 2, 3, or 4 values in the RH range, do them separately.
     664                 :             :   // Call wi_fold_in_parts to check the RH side.
     665                 :   147385886 :   if (rh_range > 0 && rh_range < 4)
     666                 :             :     {
     667                 :     5300979 :       wi_fold_in_parts (r, type, lh_lb, lh_ub, rh_lb, rh_lb);
     668                 :     5300979 :       if (rh_range > 1)
     669                 :             :         {
     670                 :      577990 :           wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 1, rh_lb + 1);
     671                 :      577990 :           r.union_ (tmp);
     672                 :      577990 :           if (rh_range == 3)
     673                 :             :             {
     674                 :      356992 :               wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 2, rh_lb + 2);
     675                 :      356992 :               r.union_ (tmp);
     676                 :             :             }
     677                 :             :         }
     678                 :     5300979 :       wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_ub, rh_ub);
     679                 :     5300979 :       r.union_ (tmp);
     680                 :             :     }
     681                 :             :   // Otherwise check for 2, 3, or 4 values in the LH range and split them up.
     682                 :             :   // The RH side has been checked, so no recursion needed.
     683                 :   142084907 :   else if (lh_range > 0 && lh_range < 4)
     684                 :             :     {
     685                 :    10415658 :       wi_fold (r, type, lh_lb, lh_lb, rh_lb, rh_ub);
     686                 :    10415658 :       if (lh_range > 1)
     687                 :             :         {
     688                 :     1792505 :           wi_fold (tmp, type, lh_lb + 1, lh_lb + 1, rh_lb, rh_ub);
     689                 :     1792497 :           r.union_ (tmp);
     690                 :     1792497 :           if (lh_range == 3)
     691                 :             :             {
     692                 :      758239 :               wi_fold (tmp, type, lh_lb + 2, lh_lb + 2, rh_lb, rh_ub);
     693                 :      758235 :               r.union_ (tmp);
     694                 :             :             }
     695                 :             :         }
     696                 :    10415658 :       wi_fold (tmp, type, lh_ub, lh_ub, rh_lb, rh_ub);
     697                 :    10415658 :       r.union_ (tmp);
     698                 :             :     }
     699                 :             :   // Otherwise just call wi_fold.
     700                 :             :   else
     701                 :   131669249 :     wi_fold (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
     702                 :   147386236 : }
     703                 :             : 
     704                 :             : // The default for fold is to break all ranges into sub-ranges and
     705                 :             : // invoke the wi_fold method on each sub-range pair.
     706                 :             : 
     707                 :             : bool
     708                 :   102979903 : range_operator::fold_range (irange &r, tree type,
     709                 :             :                             const irange &lh,
     710                 :             :                             const irange &rh,
     711                 :             :                             relation_trio trio) const
     712                 :             : {
     713                 :   102979903 :   gcc_checking_assert (r.supports_type_p (type));
     714                 :   102979903 :   if (empty_range_varying (r, type, lh, rh))
     715                 :       48802 :     return true;
     716                 :             : 
     717                 :   102931101 :   relation_kind rel = trio.op1_op2 ();
     718                 :   102931101 :   unsigned num_lh = lh.num_pairs ();
     719                 :   102931101 :   unsigned num_rh = rh.num_pairs ();
     720                 :             : 
     721                 :             :   // If op1 and op2 are equivalences, then we don't need a complete cross
     722                 :             :   // product, just pairs of matching elements.
     723                 :   102931482 :   if (relation_equiv_p (rel) && lh == rh)
     724                 :             :     {
     725                 :       53119 :       int_range_max tmp;
     726                 :       53119 :       r.set_undefined ();
     727                 :       94548 :       for (unsigned x = 0; x < num_lh; ++x)
     728                 :             :         {
     729                 :             :           // If the number of subranges is too high, limit subrange creation.
     730                 :       56892 :           unsigned limit = (r.num_pairs () > 32) ? 0 : 8;
     731                 :       56892 :           wide_int lh_lb = lh.lower_bound (x);
     732                 :       56892 :           wide_int lh_ub = lh.upper_bound (x);
     733                 :       56892 :           wi_fold_in_parts_equiv (tmp, type, lh_lb, lh_ub, limit);
     734                 :       56892 :           r.union_ (tmp);
     735                 :       56892 :           if (r.varying_p ())
     736                 :             :             break;
     737                 :       56892 :         }
     738                 :       53119 :       op1_op2_relation_effect (r, type, lh, rh, rel);
     739                 :       53119 :       update_bitmask (r, lh, rh);
     740                 :       53119 :       return true;
     741                 :       53119 :     }
     742                 :             : 
     743                 :             :   // If both ranges are single pairs, fold directly into the result range.
     744                 :             :   // If the number of subranges grows too high, produce a summary result as the
     745                 :             :   // loop becomes exponential with little benefit.  See PR 103821.
     746                 :   102877982 :   if ((num_lh == 1 && num_rh == 1) || num_lh * num_rh > 12)
     747                 :             :     {
     748                 :    85105059 :       wi_fold_in_parts (r, type, lh.lower_bound (), lh.upper_bound (),
     749                 :   170207834 :                         rh.lower_bound (), rh.upper_bound ());
     750                 :    85103917 :       op1_op2_relation_effect (r, type, lh, rh, rel);
     751                 :    85103917 :       update_bitmask (r, lh, rh);
     752                 :    85103917 :       return true;
     753                 :             :     }
     754                 :             : 
     755                 :    17774065 :   int_range_max tmp;
     756                 :    17774065 :   r.set_undefined ();
     757                 :    55991642 :   for (unsigned x = 0; x < num_lh; ++x)
     758                 :    88962606 :     for (unsigned y = 0; y < num_rh; ++y)
     759                 :             :       {
     760                 :    50745029 :         wide_int lh_lb = lh.lower_bound (x);
     761                 :    50745029 :         wide_int lh_ub = lh.upper_bound (x);
     762                 :    50745029 :         wide_int rh_lb = rh.lower_bound (y);
     763                 :    50745029 :         wide_int rh_ub = rh.upper_bound (y);
     764                 :    50745029 :         wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb, rh_ub);
     765                 :    50745029 :         r.union_ (tmp);
     766                 :    50745029 :         if (r.varying_p ())
     767                 :             :           {
     768                 :     4067645 :             op1_op2_relation_effect (r, type, lh, rh, rel);
     769                 :     4067645 :             update_bitmask (r, lh, rh);
     770                 :     4067645 :             return true;
     771                 :             :           }
     772                 :    50746118 :       }
     773                 :    13706420 :   op1_op2_relation_effect (r, type, lh, rh, rel);
     774                 :    13706420 :   update_bitmask (r, lh, rh);
     775                 :    13706420 :   return true;
     776                 :    17774065 : }
     777                 :             : 
     778                 :             : 
     779                 :             : bool
     780                 :       73272 : range_operator::fold_range (frange &, tree, const irange &,
     781                 :             :                             const frange &, relation_trio) const
     782                 :             : {
     783                 :       73272 :   return false;
     784                 :             : }
     785                 :             : 
     786                 :             : bool
     787                 :        1553 : range_operator::op1_range (irange &, tree, const frange &,
     788                 :             :                            const irange &, relation_trio) const
     789                 :             : {
     790                 :        1553 :   return false;
     791                 :             : }
     792                 :             : 
     793                 :             : 
     794                 :             : 
     795                 :             : // The default for op1_range is to return false.
     796                 :             : 
     797                 :             : bool
     798                 :      754468 : range_operator::op1_range (irange &r ATTRIBUTE_UNUSED,
     799                 :             :                            tree type ATTRIBUTE_UNUSED,
     800                 :             :                            const irange &lhs ATTRIBUTE_UNUSED,
     801                 :             :                            const irange &op2 ATTRIBUTE_UNUSED,
     802                 :             :                            relation_trio) const
     803                 :             : {
     804                 :      754468 :   return false;
     805                 :             : }
     806                 :             : 
     807                 :             : // The default for op2_range is to return false.
     808                 :             : 
     809                 :             : bool
     810                 :      571103 : range_operator::op2_range (irange &r ATTRIBUTE_UNUSED,
     811                 :             :                            tree type ATTRIBUTE_UNUSED,
     812                 :             :                            const irange &lhs ATTRIBUTE_UNUSED,
     813                 :             :                            const irange &op1 ATTRIBUTE_UNUSED,
     814                 :             :                            relation_trio) const
     815                 :             : {
     816                 :      571103 :   return false;
     817                 :             : }
     818                 :             : 
     819                 :             : // The default relation routines return VREL_VARYING.
     820                 :             : 
     821                 :             : relation_kind
     822                 :    23569702 : range_operator::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
     823                 :             :                                   const irange &op1 ATTRIBUTE_UNUSED,
     824                 :             :                                   const irange &op2 ATTRIBUTE_UNUSED,
     825                 :             :                                   relation_kind rel ATTRIBUTE_UNUSED) const
     826                 :             : {
     827                 :    23569702 :   return VREL_VARYING;
     828                 :             : }
     829                 :             : 
     830                 :             : relation_kind
     831                 :    17695165 : range_operator::lhs_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
     832                 :             :                                   const irange &op1 ATTRIBUTE_UNUSED,
     833                 :             :                                   const irange &op2 ATTRIBUTE_UNUSED,
     834                 :             :                                   relation_kind rel ATTRIBUTE_UNUSED) const
     835                 :             : {
     836                 :    17695165 :   return VREL_VARYING;
     837                 :             : }
     838                 :             : 
     839                 :             : relation_kind
     840                 :    14831210 : range_operator::op1_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
     841                 :             :                                   const irange &op1 ATTRIBUTE_UNUSED,
     842                 :             :                                   const irange &op2 ATTRIBUTE_UNUSED) const
     843                 :             : {
     844                 :    14831210 :   return VREL_VARYING;
     845                 :             : }
     846                 :             : 
     847                 :             : // Default is no relation affects the LHS.
     848                 :             : 
     849                 :             : bool
     850                 :    84261518 : range_operator::op1_op2_relation_effect (irange &lhs_range ATTRIBUTE_UNUSED,
     851                 :             :                                          tree type ATTRIBUTE_UNUSED,
     852                 :             :                                          const irange &op1_range
     853                 :             :                                          ATTRIBUTE_UNUSED,
     854                 :             :                                          const irange &op2_range
     855                 :             :                                          ATTRIBUTE_UNUSED,
     856                 :             :                                          relation_kind rel
     857                 :             :                                          ATTRIBUTE_UNUSED) const
     858                 :             : {
     859                 :    84261518 :   return false;
     860                 :             : }
     861                 :             : 
     862                 :             : bool
     863                 :           0 : range_operator::overflow_free_p (const irange &, const irange &,
     864                 :             :                                  relation_trio) const
     865                 :             : {
     866                 :           0 :   return false;
     867                 :             : }
     868                 :             : 
     869                 :             : // Apply any known bitmask updates based on this operator.
     870                 :             : 
     871                 :             : void
     872                 :        6243 : range_operator::update_bitmask (irange &, const irange &,
     873                 :             :                                 const irange &) const
     874                 :             : {
     875                 :        6243 : }
     876                 :             : 
     877                 :             : // Check that operand types are OK.  Default to always OK.
     878                 :             : 
     879                 :             : bool
     880                 :   124099619 : range_operator::operand_check_p (tree, tree, tree) const
     881                 :             : {
     882                 :   124099619 :   return true;
     883                 :             : }
     884                 :             : 
     885                 :             : // Create and return a range from a pair of wide-ints that are known
     886                 :             : // to have overflowed (or underflowed).
     887                 :             : 
     888                 :             : static void
     889                 :    42710468 : value_range_from_overflowed_bounds (irange &r, tree type,
     890                 :             :                                     const wide_int &wmin,
     891                 :             :                                     const wide_int &wmax)
     892                 :             : {
     893                 :    42710468 :   const signop sgn = TYPE_SIGN (type);
     894                 :    42710468 :   const unsigned int prec = TYPE_PRECISION (type);
     895                 :             : 
     896                 :    42710468 :   wide_int tmin = wide_int::from (wmin, prec, sgn);
     897                 :    42710468 :   wide_int tmax = wide_int::from (wmax, prec, sgn);
     898                 :             : 
     899                 :    42710468 :   bool covers = false;
     900                 :    42710468 :   wide_int tem = tmin;
     901                 :    42710468 :   tmin = tmax + 1;
     902                 :    42710468 :   if (wi::cmp (tmin, tmax, sgn) < 0)
     903                 :     2878249 :     covers = true;
     904                 :    42710468 :   tmax = tem - 1;
     905                 :    42710468 :   if (wi::cmp (tmax, tem, sgn) > 0)
     906                 :             :     covers = true;
     907                 :             : 
     908                 :             :   // If the anti-range would cover nothing, drop to varying.
     909                 :             :   // Likewise if the anti-range bounds are outside of the types
     910                 :             :   // values.
     911                 :    40059680 :   if (covers || wi::cmp (tmin, tmax, sgn) > 0)
     912                 :    29293487 :     r.set_varying (type);
     913                 :             :   else
     914                 :    13416981 :     r.set (type, tmin, tmax, VR_ANTI_RANGE);
     915                 :    42711063 : }
     916                 :             : 
     917                 :             : // Create and return a range from a pair of wide-ints.  MIN_OVF and
     918                 :             : // MAX_OVF describe any overflow that might have occurred while
     919                 :             : // calculating WMIN and WMAX respectively.
     920                 :             : 
     921                 :             : static void
     922                 :   144074332 : value_range_with_overflow (irange &r, tree type,
     923                 :             :                            const wide_int &wmin, const wide_int &wmax,
     924                 :             :                            wi::overflow_type min_ovf = wi::OVF_NONE,
     925                 :             :                            wi::overflow_type max_ovf = wi::OVF_NONE)
     926                 :             : {
     927                 :   144074332 :   const signop sgn = TYPE_SIGN (type);
     928                 :   144074332 :   const unsigned int prec = TYPE_PRECISION (type);
     929                 :   229026337 :   const bool overflow_wraps = TYPE_OVERFLOW_WRAPS (type);
     930                 :             : 
     931                 :             :   // For one bit precision if max != min, then the range covers all
     932                 :             :   // values.
     933                 :   159171616 :   if (prec == 1 && wi::ne_p (wmax, wmin))
     934                 :             :     {
     935                 :           0 :       r.set_varying (type);
     936                 :           0 :       return;
     937                 :             :     }
     938                 :             : 
     939                 :   144074332 :   if (overflow_wraps)
     940                 :             :     {
     941                 :             :       // If overflow wraps, truncate the values and adjust the range,
     942                 :             :       // kind, and bounds appropriately.
     943                 :    84952005 :       if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE))
     944                 :             :         {
     945                 :    59420273 :           wide_int tmin = wide_int::from (wmin, prec, sgn);
     946                 :    59420273 :           wide_int tmax = wide_int::from (wmax, prec, sgn);
     947                 :             :           // If the limits are swapped, we wrapped around and cover
     948                 :             :           // the entire range.
     949                 :    59420273 :           if (wi::gt_p (tmin, tmax, sgn))
     950                 :      542694 :             r.set_varying (type);
     951                 :             :           else
     952                 :             :             // No overflow or both overflow or underflow.  The range
     953                 :             :             // kind stays normal.
     954                 :    58877579 :             r.set (type, tmin, tmax);
     955                 :    59420273 :           return;
     956                 :    59420411 :         }
     957                 :             : 
     958                 :    25531732 :       if ((min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_NONE)
     959                 :    17876803 :           || (max_ovf == wi::OVF_OVERFLOW && min_ovf == wi::OVF_NONE))
     960                 :    25531732 :         value_range_from_overflowed_bounds (r, type, wmin, wmax);
     961                 :             :       else
     962                 :             :         // Other underflow and/or overflow, drop to VR_VARYING.
     963                 :           0 :         r.set_varying (type);
     964                 :             :     }
     965                 :             :   else
     966                 :             :     {
     967                 :             :       // If both bounds either underflowed or overflowed, then the result
     968                 :             :       // is undefined.
     969                 :    59122327 :       if ((min_ovf == wi::OVF_OVERFLOW && max_ovf == wi::OVF_OVERFLOW)
     970                 :    59119804 :           || (min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_UNDERFLOW))
     971                 :             :         {
     972                 :        4009 :           r.set_undefined ();
     973                 :        4009 :           return;
     974                 :             :         }
     975                 :             : 
     976                 :             :       // If overflow does not wrap, saturate to [MIN, MAX].
     977                 :    59118318 :       wide_int new_lb, new_ub;
     978                 :    59118318 :       if (min_ovf == wi::OVF_UNDERFLOW)
     979                 :     7183899 :         new_lb = wi::min_value (prec, sgn);
     980                 :    51934630 :       else if (min_ovf == wi::OVF_OVERFLOW)
     981                 :           0 :         new_lb = wi::max_value (prec, sgn);
     982                 :             :       else
     983                 :    51934630 :         new_lb = wmin;
     984                 :             : 
     985                 :    59118318 :       if (max_ovf == wi::OVF_UNDERFLOW)
     986                 :           0 :         new_ub = wi::min_value (prec, sgn);
     987                 :    59118318 :       else if (max_ovf == wi::OVF_OVERFLOW)
     988                 :    12034480 :         new_ub = wi::max_value (prec, sgn);
     989                 :             :       else
     990                 :    47084093 :         new_ub = wmax;
     991                 :             : 
     992                 :    59118318 :       r.set (type, new_lb, new_ub);
     993                 :    59118892 :     }
     994                 :             : }
     995                 :             : 
     996                 :             : // Create and return a range from a pair of wide-ints.  Canonicalize
     997                 :             : // the case where the bounds are swapped.  In which case, we transform
     998                 :             : // [10,5] into [MIN,5][10,MAX].
     999                 :             : 
    1000                 :             : static inline void
    1001                 :    85580068 : create_possibly_reversed_range (irange &r, tree type,
    1002                 :             :                                 const wide_int &new_lb, const wide_int &new_ub)
    1003                 :             : {
    1004                 :    85580068 :   signop s = TYPE_SIGN (type);
    1005                 :             :   // If the bounds are swapped, treat the result as if an overflow occurred.
    1006                 :    85580068 :   if (wi::gt_p (new_lb, new_ub, s))
    1007                 :    17178736 :     value_range_from_overflowed_bounds (r, type, new_lb, new_ub);
    1008                 :             :   else
    1009                 :             :     // Otherwise it's just a normal range.
    1010                 :    68401332 :     r.set (type, new_lb, new_ub);
    1011                 :    85580068 : }
    1012                 :             : 
    1013                 :             : // Return the summary information about boolean range LHS.  If EMPTY/FULL,
    1014                 :             : // return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing.
    1015                 :             : 
    1016                 :             : bool_range_state
    1017                 :    85176372 : get_bool_state (vrange &r, const vrange &lhs, tree val_type)
    1018                 :             : {
    1019                 :             :   // If there is no result, then this is unexecutable.
    1020                 :    85176372 :   if (lhs.undefined_p ())
    1021                 :             :     {
    1022                 :           0 :       r.set_undefined ();
    1023                 :           0 :       return BRS_EMPTY;
    1024                 :             :     }
    1025                 :             : 
    1026                 :    85176372 :   if (lhs.zero_p ())
    1027                 :             :     return BRS_FALSE;
    1028                 :             : 
    1029                 :             :   // For TRUE, we can't just test for [1,1] because Ada can have
    1030                 :             :   // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
    1031                 :    43675859 :   if (lhs.contains_p (build_zero_cst (lhs.type ())))
    1032                 :             :     {
    1033                 :      177342 :       r.set_varying (val_type);
    1034                 :      177342 :       return BRS_FULL;
    1035                 :             :     }
    1036                 :             : 
    1037                 :             :   return BRS_TRUE;
    1038                 :             : }
    1039                 :             : 
    1040                 :             : // ------------------------------------------------------------------------
    1041                 :             : 
    1042                 :             : void
    1043                 :           0 : operator_equal::update_bitmask (irange &r, const irange &lh,
    1044                 :             :                                 const irange &rh) const
    1045                 :             : {
    1046                 :           0 :   update_known_bitmask (r, EQ_EXPR, lh, rh);
    1047                 :           0 : }
    1048                 :             : 
    1049                 :             : // Check if the LHS range indicates a relation between OP1 and OP2.
    1050                 :             : 
    1051                 :             : relation_kind
    1052                 :     5961047 : operator_equal::op1_op2_relation (const irange &lhs, const irange &,
    1053                 :             :                                   const irange &) const
    1054                 :             : {
    1055                 :     5961047 :   if (lhs.undefined_p ())
    1056                 :             :     return VREL_UNDEFINED;
    1057                 :             : 
    1058                 :             :   // FALSE = op1 == op2 indicates NE_EXPR.
    1059                 :     5961047 :   if (lhs.zero_p ())
    1060                 :             :     return VREL_NE;
    1061                 :             : 
    1062                 :             :   // TRUE = op1 == op2 indicates EQ_EXPR.
    1063                 :     3118398 :   if (!contains_zero_p (lhs))
    1064                 :     3083016 :     return VREL_EQ;
    1065                 :             :   return VREL_VARYING;
    1066                 :             : }
    1067                 :             : 
    1068                 :             : bool
    1069                 :    18679698 : operator_equal::fold_range (irange &r, tree type,
    1070                 :             :                             const irange &op1,
    1071                 :             :                             const irange &op2,
    1072                 :             :                             relation_trio rel) const
    1073                 :             : {
    1074                 :    18679698 :   if (relop_early_resolve (r, type, op1, op2, rel, VREL_EQ))
    1075                 :             :     return true;
    1076                 :             : 
    1077                 :             :   // We can be sure the values are always equal or not if both ranges
    1078                 :             :   // consist of a single value, and then compare them.
    1079                 :    18646872 :   bool op1_const = wi::eq_p (op1.lower_bound (), op1.upper_bound ());
    1080                 :    18646872 :   bool op2_const = wi::eq_p (op2.lower_bound (), op2.upper_bound ());
    1081                 :    18646862 :   if (op1_const && op2_const)
    1082                 :             :     {
    1083                 :      302119 :       if (wi::eq_p (op1.lower_bound (), op2.upper_bound()))
    1084                 :      160540 :         r = range_true (type);
    1085                 :             :       else
    1086                 :      141579 :         r = range_false (type);
    1087                 :             :     }
    1088                 :             :   else
    1089                 :             :     {
    1090                 :             :       // If ranges do not intersect, we know the range is not equal,
    1091                 :             :       // otherwise we don't know anything for sure.
    1092                 :    18344743 :       int_range_max tmp = op1;
    1093                 :    18344743 :       tmp.intersect (op2);
    1094                 :    18344743 :       if (tmp.undefined_p ())
    1095                 :      249855 :         r = range_false (type);
    1096                 :             :       // Check if a constant cannot satisfy the bitmask requirements.
    1097                 :    33764039 :       else if (op2_const && !op1.get_bitmask ().member_p (op2.lower_bound ()))
    1098                 :           0 :          r = range_false (type);
    1099                 :    18154337 :       else if (op1_const && !op2.get_bitmask ().member_p (op1.lower_bound ()))
    1100                 :           0 :          r = range_false (type);
    1101                 :             :       else
    1102                 :    18094888 :         r = range_true_and_false (type);
    1103                 :    18344743 :     }
    1104                 :             :   return true;
    1105                 :             : }
    1106                 :             : 
    1107                 :             : bool
    1108                 :    11926207 : operator_equal::op1_range (irange &r, tree type,
    1109                 :             :                            const irange &lhs,
    1110                 :             :                            const irange &op2,
    1111                 :             :                            relation_trio) const
    1112                 :             : {
    1113                 :    11926207 :   switch (get_bool_state (r, lhs, type))
    1114                 :             :     {
    1115                 :     3454925 :     case BRS_TRUE:
    1116                 :             :       // If it's true, the result is the same as OP2.
    1117                 :     3454925 :       r = op2;
    1118                 :     3454925 :       break;
    1119                 :             : 
    1120                 :     8430238 :     case BRS_FALSE:
    1121                 :             :       // If the result is false, the only time we know anything is
    1122                 :             :       // if OP2 is a constant.
    1123                 :     8430238 :       if (!op2.undefined_p ()
    1124                 :    25290714 :           && wi::eq_p (op2.lower_bound(), op2.upper_bound()))
    1125                 :             :         {
    1126                 :     6727033 :           r = op2;
    1127                 :     6727033 :           r.invert ();
    1128                 :             :         }
    1129                 :             :       else
    1130                 :     1703205 :         r.set_varying (type);
    1131                 :             :       break;
    1132                 :             : 
    1133                 :             :     default:
    1134                 :             :       break;
    1135                 :             :     }
    1136                 :    11926207 :   return true;
    1137                 :             : }
    1138                 :             : 
    1139                 :             : bool
    1140                 :     1569828 : operator_equal::op2_range (irange &r, tree type,
    1141                 :             :                            const irange &lhs,
    1142                 :             :                            const irange &op1,
    1143                 :             :                            relation_trio rel) const
    1144                 :             : {
    1145                 :     1569828 :   return operator_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
    1146                 :             : }
    1147                 :             : 
    1148                 :             : // -------------------------------------------------------------------------
    1149                 :             : 
    1150                 :             : void
    1151                 :           0 : operator_not_equal::update_bitmask (irange &r, const irange &lh,
    1152                 :             :                                     const irange &rh) const
    1153                 :             : {
    1154                 :           0 :   update_known_bitmask (r, NE_EXPR, lh, rh);
    1155                 :           0 : }
    1156                 :             : 
    1157                 :             : // Check if the LHS range indicates a relation between OP1 and OP2.
    1158                 :             : 
    1159                 :             : relation_kind
    1160                 :    10899041 : operator_not_equal::op1_op2_relation (const irange &lhs, const irange &,
    1161                 :             :                                       const irange &) const
    1162                 :             : {
    1163                 :    10899041 :   if (lhs.undefined_p ())
    1164                 :             :     return VREL_UNDEFINED;
    1165                 :             : 
    1166                 :             :   // FALSE = op1 != op2  indicates EQ_EXPR.
    1167                 :    10899041 :   if (lhs.zero_p ())
    1168                 :             :     return VREL_EQ;
    1169                 :             : 
    1170                 :             :   // TRUE = op1 != op2  indicates NE_EXPR.
    1171                 :     5522730 :   if (!contains_zero_p (lhs))
    1172                 :     5483275 :     return VREL_NE;
    1173                 :             :   return VREL_VARYING;
    1174                 :             : }
    1175                 :             : 
    1176                 :             : bool
    1177                 :    27567884 : operator_not_equal::fold_range (irange &r, tree type,
    1178                 :             :                                 const irange &op1,
    1179                 :             :                                 const irange &op2,
    1180                 :             :                                 relation_trio rel) const
    1181                 :             : {
    1182                 :    27567884 :   if (relop_early_resolve (r, type, op1, op2, rel, VREL_NE))
    1183                 :             :     return true;
    1184                 :             : 
    1185                 :             :   // We can be sure the values are always equal or not if both ranges
    1186                 :             :   // consist of a single value, and then compare them.
    1187                 :    27546138 :   bool op1_const = wi::eq_p (op1.lower_bound (), op1.upper_bound ());
    1188                 :    27546138 :   bool op2_const = wi::eq_p (op2.lower_bound (), op2.upper_bound ());
    1189                 :    27545805 :   if (op1_const && op2_const)
    1190                 :             :     {
    1191                 :     1139888 :       if (wi::ne_p (op1.lower_bound (), op2.upper_bound()))
    1192                 :      641169 :         r = range_true (type);
    1193                 :             :       else
    1194                 :      498717 :         r = range_false (type);
    1195                 :             :     }
    1196                 :             :   else
    1197                 :             :     {
    1198                 :             :       // If ranges do not intersect, we know the range is not equal,
    1199                 :             :       // otherwise we don't know anything for sure.
    1200                 :    26405919 :       int_range_max tmp = op1;
    1201                 :    26405919 :       tmp.intersect (op2);
    1202                 :    26405919 :       if (tmp.undefined_p ())
    1203                 :      215339 :         r = range_true (type);
    1204                 :             :       // Check if a constant cannot satisfy the bitmask requirements.
    1205                 :    47844533 :       else if (op2_const && !op1.get_bitmask ().member_p (op2.lower_bound ()))
    1206                 :           0 :          r = range_true (type);
    1207                 :    26228189 :       else if (op1_const && !op2.get_bitmask ().member_p (op1.lower_bound ()))
    1208                 :           0 :          r = range_true (type);
    1209                 :             :       else
    1210                 :    26190580 :         r = range_true_and_false (type);
    1211                 :    26405919 :     }
    1212                 :             :   return true;
    1213                 :             : }
    1214                 :             : 
    1215                 :             : bool
    1216                 :    22309692 : operator_not_equal::op1_range (irange &r, tree type,
    1217                 :             :                                const irange &lhs,
    1218                 :             :                                const irange &op2,
    1219                 :             :                                relation_trio) const
    1220                 :             : {
    1221                 :    22309692 :   switch (get_bool_state (r, lhs, type))
    1222                 :             :     {
    1223                 :    14093210 :     case BRS_TRUE:
    1224                 :             :       // If the result is true, the only time we know anything is if
    1225                 :             :       // OP2 is a constant.
    1226                 :    14093210 :       if (!op2.undefined_p ()
    1227                 :    42279630 :           && wi::eq_p (op2.lower_bound(), op2.upper_bound()))
    1228                 :             :         {
    1229                 :    11072510 :           r = op2;
    1230                 :    11072510 :           r.invert ();
    1231                 :             :         }
    1232                 :             :       else
    1233                 :     3020700 :         r.set_varying (type);
    1234                 :             :       break;
    1235                 :             : 
    1236                 :     8190531 :     case BRS_FALSE:
    1237                 :             :       // If it's false, the result is the same as OP2.
    1238                 :     8190531 :       r = op2;
    1239                 :     8190531 :       break;
    1240                 :             : 
    1241                 :             :     default:
    1242                 :             :       break;
    1243                 :             :     }
    1244                 :    22309692 :   return true;
    1245                 :             : }
    1246                 :             : 
    1247                 :             : 
    1248                 :             : bool
    1249                 :     2771177 : operator_not_equal::op2_range (irange &r, tree type,
    1250                 :             :                                const irange &lhs,
    1251                 :             :                                const irange &op1,
    1252                 :             :                                relation_trio rel) const
    1253                 :             : {
    1254                 :     2771177 :   return operator_not_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
    1255                 :             : }
    1256                 :             : 
    1257                 :             : // (X < VAL) produces the range of [MIN, VAL - 1].
    1258                 :             : 
    1259                 :             : static void
    1260                 :     6564375 : build_lt (irange &r, tree type, const wide_int &val)
    1261                 :             : {
    1262                 :     6564375 :   wi::overflow_type ov;
    1263                 :     6564375 :   wide_int lim;
    1264                 :     6564375 :   signop sgn = TYPE_SIGN (type);
    1265                 :             : 
    1266                 :             :   // Signed 1 bit cannot represent 1 for subtraction.
    1267                 :     6564375 :   if (sgn == SIGNED)
    1268                 :     4459259 :     lim = wi::add (val, -1, sgn, &ov);
    1269                 :             :   else
    1270                 :     2105176 :     lim = wi::sub (val, 1, sgn, &ov);
    1271                 :             : 
    1272                 :             :   // If val - 1 underflows, check if X < MIN, which is an empty range.
    1273                 :     6564375 :   if (ov)
    1274                 :         489 :     r.set_undefined ();
    1275                 :             :   else
    1276                 :     6563946 :     r = int_range<1> (type, min_limit (type), lim);
    1277                 :     6564375 : }
    1278                 :             : 
    1279                 :             : // (X <= VAL) produces the range of [MIN, VAL].
    1280                 :             : 
    1281                 :             : static void
    1282                 :    13291084 : build_le (irange &r, tree type, const wide_int &val)
    1283                 :             : {
    1284                 :    13291084 :   r = int_range<1> (type, min_limit (type), val);
    1285                 :    13291084 : }
    1286                 :             : 
    1287                 :             : // (X > VAL) produces the range of [VAL + 1, MAX].
    1288                 :             : 
    1289                 :             : static void
    1290                 :     9928318 : build_gt (irange &r, tree type, const wide_int &val)
    1291                 :             : {
    1292                 :     9928318 :   wi::overflow_type ov;
    1293                 :     9928318 :   wide_int lim;
    1294                 :     9928318 :   signop sgn = TYPE_SIGN (type);
    1295                 :             : 
    1296                 :             :   // Signed 1 bit cannot represent 1 for addition.
    1297                 :     9928318 :   if (sgn == SIGNED)
    1298                 :     5286112 :     lim = wi::sub (val, -1, sgn, &ov);
    1299                 :             :   else
    1300                 :     4642292 :     lim = wi::add (val, 1, sgn, &ov);
    1301                 :             :   // If val + 1 overflows, check is for X > MAX, which is an empty range.
    1302                 :     9928318 :   if (ov)
    1303                 :           0 :     r.set_undefined ();
    1304                 :             :   else
    1305                 :     9928404 :     r = int_range<1> (type, lim, max_limit (type));
    1306                 :     9928318 : }
    1307                 :             : 
    1308                 :             : // (X >= val) produces the range of [VAL, MAX].
    1309                 :             : 
    1310                 :             : static void
    1311                 :     6779783 : build_ge (irange &r, tree type, const wide_int &val)
    1312                 :             : {
    1313                 :     6779783 :   r = int_range<1> (type, val, max_limit (type));
    1314                 :     6779783 : }
    1315                 :             : 
    1316                 :             : 
    1317                 :             : void
    1318                 :           0 : operator_lt::update_bitmask (irange &r, const irange &lh,
    1319                 :             :                              const irange &rh) const
    1320                 :             : {
    1321                 :           0 :   update_known_bitmask (r, LT_EXPR, lh, rh);
    1322                 :           0 : }
    1323                 :             : 
    1324                 :             : // Check if the LHS range indicates a relation between OP1 and OP2.
    1325                 :             : 
    1326                 :             : relation_kind
    1327                 :    11859228 : operator_lt::op1_op2_relation (const irange &lhs, const irange &,
    1328                 :             :                                const irange &) const
    1329                 :             : {
    1330                 :    11859228 :   if (lhs.undefined_p ())
    1331                 :             :     return VREL_UNDEFINED;
    1332                 :             : 
    1333                 :             :   // FALSE = op1 < op2 indicates GE_EXPR.
    1334                 :    11859228 :   if (lhs.zero_p ())
    1335                 :             :     return VREL_GE;
    1336                 :             : 
    1337                 :             :   // TRUE = op1 < op2 indicates LT_EXPR.
    1338                 :     6002249 :   if (!contains_zero_p (lhs))
    1339                 :     5994650 :     return VREL_LT;
    1340                 :             :   return VREL_VARYING;
    1341                 :             : }
    1342                 :             : 
    1343                 :             : bool
    1344                 :     6330850 : operator_lt::fold_range (irange &r, tree type,
    1345                 :             :                          const irange &op1,
    1346                 :             :                          const irange &op2,
    1347                 :             :                          relation_trio rel) const
    1348                 :             : {
    1349                 :     6330850 :   if (relop_early_resolve (r, type, op1, op2, rel, VREL_LT))
    1350                 :             :     return true;
    1351                 :             : 
    1352                 :     6307655 :   signop sign = TYPE_SIGN (op1.type ());
    1353                 :     6307655 :   gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
    1354                 :             : 
    1355                 :     6307691 :   if (wi::lt_p (op1.upper_bound (), op2.lower_bound (), sign))
    1356                 :       22110 :     r = range_true (type);
    1357                 :     6285581 :   else if (!wi::lt_p (op1.lower_bound (), op2.upper_bound (), sign))
    1358                 :       70000 :     r = range_false (type);
    1359                 :             :   // Use nonzero bits to determine if < 0 is false.
    1360                 :     8132392 :   else if (op2.zero_p () && !wi::neg_p (op1.get_nonzero_bits (), sign))
    1361                 :           0 :     r = range_false (type);
    1362                 :             :   else
    1363                 :     6215545 :     r = range_true_and_false (type);
    1364                 :             :   return true;
    1365                 :             : }
    1366                 :             : 
    1367                 :             : bool
    1368                 :     5145062 : operator_lt::op1_range (irange &r, tree type,
    1369                 :             :                         const irange &lhs,
    1370                 :             :                         const irange &op2,
    1371                 :             :                         relation_trio) const
    1372                 :             : {
    1373                 :     5145062 :   if (op2.undefined_p ())
    1374                 :             :     return false;
    1375                 :             : 
    1376                 :     5145062 :   switch (get_bool_state (r, lhs, type))
    1377                 :             :     {
    1378                 :     2402420 :     case BRS_TRUE:
    1379                 :     2402420 :       build_lt (r, type, op2.upper_bound ());
    1380                 :     2402420 :       break;
    1381                 :             : 
    1382                 :     2737889 :     case BRS_FALSE:
    1383                 :     2737889 :       build_ge (r, type, op2.lower_bound ());
    1384                 :     2737889 :       break;
    1385                 :             : 
    1386                 :             :     default:
    1387                 :             :       break;
    1388                 :             :     }
    1389                 :             :   return true;
    1390                 :             : }
    1391                 :             : 
    1392                 :             : bool
    1393                 :     3924996 : operator_lt::op2_range (irange &r, tree type,
    1394                 :             :                         const irange &lhs,
    1395                 :             :                         const irange &op1,
    1396                 :             :                         relation_trio) const
    1397                 :             : {
    1398                 :     3924996 :   if (op1.undefined_p ())
    1399                 :             :     return false;
    1400                 :             : 
    1401                 :     3924994 :   switch (get_bool_state (r, lhs, type))
    1402                 :             :     {
    1403                 :     1694432 :     case BRS_TRUE:
    1404                 :     1694432 :       build_gt (r, type, op1.lower_bound ());
    1405                 :     1694432 :       break;
    1406                 :             : 
    1407                 :     2227052 :     case BRS_FALSE:
    1408                 :     2227052 :       build_le (r, type, op1.upper_bound ());
    1409                 :     2227052 :       break;
    1410                 :             : 
    1411                 :             :     default:
    1412                 :             :       break;
    1413                 :             :     }
    1414                 :             :   return true;
    1415                 :             : }
    1416                 :             : 
    1417                 :             : 
    1418                 :             : void
    1419                 :           0 : operator_le::update_bitmask (irange &r, const irange &lh,
    1420                 :             :                              const irange &rh) const
    1421                 :             : {
    1422                 :           0 :   update_known_bitmask (r, LE_EXPR, lh, rh);
    1423                 :           0 : }
    1424                 :             : 
    1425                 :             : // Check if the LHS range indicates a relation between OP1 and OP2.
    1426                 :             : 
    1427                 :             : relation_kind
    1428                 :     4291368 : operator_le::op1_op2_relation (const irange &lhs, const irange &,
    1429                 :             :                                const irange &) const
    1430                 :             : {
    1431                 :     4291368 :   if (lhs.undefined_p ())
    1432                 :             :     return VREL_UNDEFINED;
    1433                 :             : 
    1434                 :             :   // FALSE = op1 <= op2 indicates GT_EXPR.
    1435                 :     4291368 :   if (lhs.zero_p ())
    1436                 :             :     return VREL_GT;
    1437                 :             : 
    1438                 :             :   // TRUE = op1 <= op2 indicates LE_EXPR.
    1439                 :     2503643 :   if (!contains_zero_p (lhs))
    1440                 :     2494359 :     return VREL_LE;
    1441                 :             :   return VREL_VARYING;
    1442                 :             : }
    1443                 :             : 
    1444                 :             : bool
    1445                 :     5283912 : operator_le::fold_range (irange &r, tree type,
    1446                 :             :                          const irange &op1,
    1447                 :             :                          const irange &op2,
    1448                 :             :                          relation_trio rel) const
    1449                 :             : {
    1450                 :     5283912 :   if (relop_early_resolve (r, type, op1, op2, rel, VREL_LE))
    1451                 :             :     return true;
    1452                 :             : 
    1453                 :     5269786 :   signop sign = TYPE_SIGN (op1.type ());
    1454                 :     5269786 :   gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
    1455                 :             : 
    1456                 :     5269790 :   if (wi::le_p (op1.upper_bound (), op2.lower_bound (), sign))
    1457                 :      112412 :     r = range_true (type);
    1458                 :     5157378 :   else if (!wi::le_p (op1.lower_bound (), op2.upper_bound (), sign))
    1459                 :       80293 :     r = range_false (type);
    1460                 :             :   else
    1461                 :     5077081 :     r = range_true_and_false (type);
    1462                 :             :   return true;
    1463                 :             : }
    1464                 :             : 
    1465                 :             : bool
    1466                 :     6001467 : operator_le::op1_range (irange &r, tree type,
    1467                 :             :                         const irange &lhs,
    1468                 :             :                         const irange &op2,
    1469                 :             :                         relation_trio) const
    1470                 :             : {
    1471                 :     6001467 :   if (op2.undefined_p ())
    1472                 :             :     return false;
    1473                 :             : 
    1474                 :     6001467 :   switch (get_bool_state (r, lhs, type))
    1475                 :             :     {
    1476                 :     3192093 :     case BRS_TRUE:
    1477                 :     3192093 :       build_le (r, type, op2.upper_bound ());
    1478                 :     3192093 :       break;
    1479                 :             : 
    1480                 :     2796454 :     case BRS_FALSE:
    1481                 :     2796454 :       build_gt (r, type, op2.lower_bound ());
    1482                 :     2796454 :       break;
    1483                 :             : 
    1484                 :             :     default:
    1485                 :             :       break;
    1486                 :             :     }
    1487                 :             :   return true;
    1488                 :             : }
    1489                 :             : 
    1490                 :             : bool
    1491                 :     1123430 : operator_le::op2_range (irange &r, tree type,
    1492                 :             :                         const irange &lhs,
    1493                 :             :                         const irange &op1,
    1494                 :             :                         relation_trio) const
    1495                 :             : {
    1496                 :     1123430 :   if (op1.undefined_p ())
    1497                 :             :     return false;
    1498                 :             : 
    1499                 :     1123430 :   switch (get_bool_state (r, lhs, type))
    1500                 :             :     {
    1501                 :      517470 :     case BRS_TRUE:
    1502                 :      517470 :       build_ge (r, type, op1.lower_bound ());
    1503                 :      517470 :       break;
    1504                 :             : 
    1505                 :      602132 :     case BRS_FALSE:
    1506                 :      602132 :       build_lt (r, type, op1.upper_bound ());
    1507                 :      602132 :       break;
    1508                 :             : 
    1509                 :             :     default:
    1510                 :             :       break;
    1511                 :             :     }
    1512                 :             :   return true;
    1513                 :             : }
    1514                 :             : 
    1515                 :             : 
    1516                 :             : void
    1517                 :           0 : operator_gt::update_bitmask (irange &r, const irange &lh,
    1518                 :             :                              const irange &rh) const
    1519                 :             : {
    1520                 :           0 :   update_known_bitmask (r, GT_EXPR, lh, rh);
    1521                 :           0 : }
    1522                 :             : 
    1523                 :             : // Check if the LHS range indicates a relation between OP1 and OP2.
    1524                 :             : 
    1525                 :             : relation_kind
    1526                 :    11999078 : operator_gt::op1_op2_relation (const irange &lhs, const irange &,
    1527                 :             :                                const irange &) const
    1528                 :             : {
    1529                 :    11999078 :   if (lhs.undefined_p ())
    1530                 :             :     return VREL_UNDEFINED;
    1531                 :             : 
    1532                 :             :   // FALSE = op1 > op2 indicates LE_EXPR.
    1533                 :    11999078 :   if (lhs.zero_p ())
    1534                 :             :     return VREL_LE;
    1535                 :             : 
    1536                 :             :   // TRUE = op1 > op2 indicates GT_EXPR.
    1537                 :     6506597 :   if (!contains_zero_p (lhs))
    1538                 :     6492607 :     return VREL_GT;
    1539                 :             :   return VREL_VARYING;
    1540                 :             : }
    1541                 :             : 
    1542                 :             : bool
    1543                 :    11535314 : operator_gt::fold_range (irange &r, tree type,
    1544                 :             :                          const irange &op1, const irange &op2,
    1545                 :             :                          relation_trio rel) const
    1546                 :             : {
    1547                 :    11535314 :   if (relop_early_resolve (r, type, op1, op2, rel, VREL_GT))
    1548                 :             :     return true;
    1549                 :             : 
    1550                 :    11482309 :   signop sign = TYPE_SIGN (op1.type ());
    1551                 :    11482309 :   gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
    1552                 :             : 
    1553                 :    11482335 :   if (wi::gt_p (op1.lower_bound (), op2.upper_bound (), sign))
    1554                 :       93519 :     r = range_true (type);
    1555                 :    11388816 :   else if (!wi::gt_p (op1.upper_bound (), op2.lower_bound (), sign))
    1556                 :      365525 :     r = range_false (type);
    1557                 :             :   else
    1558                 :    11023265 :     r = range_true_and_false (type);
    1559                 :             :   return true;
    1560                 :             : }
    1561                 :             : 
    1562                 :             : bool
    1563                 :    11879856 : operator_gt::op1_range (irange &r, tree type,
    1564                 :             :                         const irange &lhs, const irange &op2,
    1565                 :             :                         relation_trio) const
    1566                 :             : {
    1567                 :    11879856 :   if (op2.undefined_p ())
    1568                 :             :     return false;
    1569                 :             : 
    1570                 :    11879856 :   switch (get_bool_state (r, lhs, type))
    1571                 :             :     {
    1572                 :     4854941 :     case BRS_TRUE:
    1573                 :     4854941 :       build_gt (r, type, op2.lower_bound ());
    1574                 :     4854941 :       break;
    1575                 :             : 
    1576                 :     7015562 :     case BRS_FALSE:
    1577                 :     7015562 :       build_le (r, type, op2.upper_bound ());
    1578                 :     7015562 :       break;
    1579                 :             : 
    1580                 :             :     default:
    1581                 :             :       break;
    1582                 :             :     }
    1583                 :             :   return true;
    1584                 :             : }
    1585                 :             : 
    1586                 :             : bool
    1587                 :     3777319 : operator_gt::op2_range (irange &r, tree type,
    1588                 :             :                         const irange &lhs,
    1589                 :             :                         const irange &op1,
    1590                 :             :                         relation_trio) const
    1591                 :             : {
    1592                 :     3777319 :   if (op1.undefined_p ())
    1593                 :             :     return false;
    1594                 :             : 
    1595                 :     3777319 :   switch (get_bool_state (r, lhs, type))
    1596                 :             :     {
    1597                 :     2315705 :     case BRS_TRUE:
    1598                 :     2315705 :       build_lt (r, type, op1.upper_bound ());
    1599                 :     2315705 :       break;
    1600                 :             : 
    1601                 :     1452785 :     case BRS_FALSE:
    1602                 :     1452785 :       build_ge (r, type, op1.lower_bound ());
    1603                 :     1452785 :       break;
    1604                 :             : 
    1605                 :             :     default:
    1606                 :             :       break;
    1607                 :             :     }
    1608                 :             :   return true;
    1609                 :             : }
    1610                 :             : 
    1611                 :             : 
    1612                 :             : void
    1613                 :           0 : operator_ge::update_bitmask (irange &r, const irange &lh,
    1614                 :             :                              const irange &rh) const
    1615                 :             : {
    1616                 :           0 :   update_known_bitmask (r, GE_EXPR, lh, rh);
    1617                 :           0 : }
    1618                 :             : 
    1619                 :             : // Check if the LHS range indicates a relation between OP1 and OP2.
    1620                 :             : 
    1621                 :             : relation_kind
    1622                 :     4303311 : operator_ge::op1_op2_relation (const irange &lhs, const irange &,
    1623                 :             :                                const irange &) const
    1624                 :             : {
    1625                 :     4303311 :   if (lhs.undefined_p ())
    1626                 :             :     return VREL_UNDEFINED;
    1627                 :             : 
    1628                 :             :   // FALSE = op1 >= op2 indicates LT_EXPR.
    1629                 :     4303311 :   if (lhs.zero_p ())
    1630                 :             :     return VREL_LT;
    1631                 :             : 
    1632                 :             :   // TRUE = op1 >= op2 indicates GE_EXPR.
    1633                 :     2313206 :   if (!contains_zero_p (lhs))
    1634                 :     2307482 :     return VREL_GE;
    1635                 :             :   return VREL_VARYING;
    1636                 :             : }
    1637                 :             : 
    1638                 :             : bool
    1639                 :     2775165 : operator_ge::fold_range (irange &r, tree type,
    1640                 :             :                          const irange &op1,
    1641                 :             :                          const irange &op2,
    1642                 :             :                          relation_trio rel) const
    1643                 :             : {
    1644                 :     2775165 :   if (relop_early_resolve (r, type, op1, op2, rel, VREL_GE))
    1645                 :             :     return true;
    1646                 :             : 
    1647                 :     2763419 :   signop sign = TYPE_SIGN (op1.type ());
    1648                 :     2763419 :   gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
    1649                 :             : 
    1650                 :     2763451 :   if (wi::ge_p (op1.lower_bound (), op2.upper_bound (), sign))
    1651                 :      172416 :     r = range_true (type);
    1652                 :     2591035 :   else if (!wi::ge_p (op1.upper_bound (), op2.lower_bound (), sign))
    1653                 :        6362 :     r = range_false (type);
    1654                 :             :   else
    1655                 :     2584641 :     r = range_true_and_false (type);
    1656                 :             :   return true;
    1657                 :             : }
    1658                 :             : 
    1659                 :             : bool
    1660                 :     3322030 : operator_ge::op1_range (irange &r, tree type,
    1661                 :             :                         const irange &lhs,
    1662                 :             :                         const irange &op2,
    1663                 :             :                         relation_trio) const
    1664                 :             : {
    1665                 :     3322030 :   if (op2.undefined_p ())
    1666                 :             :     return false;
    1667                 :             : 
    1668                 :     3322030 :   switch (get_bool_state (r, lhs, type))
    1669                 :             :     {
    1670                 :     2071639 :     case BRS_TRUE:
    1671                 :     2071639 :       build_ge (r, type, op2.lower_bound ());
    1672                 :     2071639 :       break;
    1673                 :             : 
    1674                 :     1244118 :     case BRS_FALSE:
    1675                 :     1244118 :       build_lt (r, type, op2.upper_bound ());
    1676                 :     1244118 :       break;
    1677                 :             : 
    1678                 :             :     default:
    1679                 :             :       break;
    1680                 :             :     }
    1681                 :             :   return true;
    1682                 :             : }
    1683                 :             : 
    1684                 :             : bool
    1685                 :     1441662 : operator_ge::op2_range (irange &r, tree type,
    1686                 :             :                         const irange &lhs,
    1687                 :             :                         const irange &op1,
    1688                 :             :                         relation_trio) const
    1689                 :             : {
    1690                 :     1441662 :   if (op1.undefined_p ())
    1691                 :             :     return false;
    1692                 :             : 
    1693                 :     1441662 :   switch (get_bool_state (r, lhs, type))
    1694                 :             :     {
    1695                 :      856377 :     case BRS_TRUE:
    1696                 :      856377 :       build_le (r, type, op1.upper_bound ());
    1697                 :      856377 :       break;
    1698                 :             : 
    1699                 :      582491 :     case BRS_FALSE:
    1700                 :      582491 :       build_gt (r, type, op1.lower_bound ());
    1701                 :      582491 :       break;
    1702                 :             : 
    1703                 :             :     default:
    1704                 :             :       break;
    1705                 :             :     }
    1706                 :             :   return true;
    1707                 :             : }
    1708                 :             : 
    1709                 :             : 
    1710                 :             : void
    1711                 :    51058538 : operator_plus::update_bitmask (irange &r, const irange &lh,
    1712                 :             :                                const irange &rh) const
    1713                 :             : {
    1714                 :    51058538 :   update_known_bitmask (r, PLUS_EXPR, lh, rh);
    1715                 :    51058538 : }
    1716                 :             : 
    1717                 :             : // Check to see if the range of OP2 indicates anything about the relation
    1718                 :             : // between LHS and OP1.
    1719                 :             : 
    1720                 :             : relation_kind
    1721                 :    47337269 : operator_plus::lhs_op1_relation (const irange &lhs,
    1722                 :             :                                  const irange &op1,
    1723                 :             :                                  const irange &op2,
    1724                 :             :                                  relation_kind) const
    1725                 :             : {
    1726                 :    47337269 :   if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
    1727                 :             :     return VREL_VARYING;
    1728                 :             : 
    1729                 :    47305572 :   tree type = lhs.type ();
    1730                 :    47305572 :   unsigned prec = TYPE_PRECISION (type);
    1731                 :    47305572 :   wi::overflow_type ovf1, ovf2;
    1732                 :    47305572 :   signop sign = TYPE_SIGN (type);
    1733                 :             : 
    1734                 :             :   // LHS = OP1 + 0  indicates LHS == OP1.
    1735                 :    47305572 :   if (op2.zero_p ())
    1736                 :             :     return VREL_EQ;
    1737                 :             : 
    1738                 :    47134067 :   if (TYPE_OVERFLOW_WRAPS (type))
    1739                 :             :     {
    1740                 :    27087819 :       wi::add (op1.lower_bound (), op2.lower_bound (), sign, &ovf1);
    1741                 :    27088041 :       wi::add (op1.upper_bound (), op2.upper_bound (), sign, &ovf2);
    1742                 :             :     }
    1743                 :             :   else
    1744                 :    20046692 :     ovf1 = ovf2 = wi::OVF_NONE;
    1745                 :             : 
    1746                 :             :   // Never wrapping additions.
    1747                 :    47134067 :   if (!ovf1 && !ovf2)
    1748                 :             :     {
    1749                 :             :       // Positive op2 means lhs > op1.
    1750                 :    27205061 :       if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
    1751                 :             :         return VREL_GT;
    1752                 :    10207370 :       if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
    1753                 :             :         return VREL_GE;
    1754                 :             : 
    1755                 :             :       // Negative op2 means lhs < op1.
    1756                 :     8443452 :       if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
    1757                 :             :         return VREL_LT;
    1758                 :     5205724 :       if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
    1759                 :             :         return VREL_LE;
    1760                 :             :     }
    1761                 :             :   // Always wrapping additions.
    1762                 :    19929385 :   else if (ovf1 && ovf1 == ovf2)
    1763                 :             :     {
    1764                 :             :       // Positive op2 means lhs < op1.
    1765                 :      771868 :       if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
    1766                 :             :         return VREL_LT;
    1767                 :          20 :       if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
    1768                 :             :         return VREL_LE;
    1769                 :             : 
    1770                 :             :       // Negative op2 means lhs > op1.
    1771                 :          20 :       if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
    1772                 :             :         return VREL_GT;
    1773                 :           0 :       if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
    1774                 :             :         return VREL_GE;
    1775                 :             :     }
    1776                 :             : 
    1777                 :             :   // If op2 does not contain 0, then LHS and OP1 can never be equal.
    1778                 :    24350926 :   if (!range_includes_zero_p (op2))
    1779                 :             :     return VREL_NE;
    1780                 :             : 
    1781                 :             :   return VREL_VARYING;
    1782                 :             : }
    1783                 :             : 
    1784                 :             : // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
    1785                 :             : // operands.
    1786                 :             : 
    1787                 :             : relation_kind
    1788                 :     8188566 : operator_plus::lhs_op2_relation (const irange &lhs, const irange &op1,
    1789                 :             :                                  const irange &op2, relation_kind rel) const
    1790                 :             : {
    1791                 :     8188566 :   return lhs_op1_relation (lhs, op2, op1, rel);
    1792                 :             : }
    1793                 :             : 
    1794                 :             : void
    1795                 :    76248791 : operator_plus::wi_fold (irange &r, tree type,
    1796                 :             :                         const wide_int &lh_lb, const wide_int &lh_ub,
    1797                 :             :                         const wide_int &rh_lb, const wide_int &rh_ub) const
    1798                 :             : {
    1799                 :    76248791 :   wi::overflow_type ov_lb, ov_ub;
    1800                 :    76248791 :   signop s = TYPE_SIGN (type);
    1801                 :    76248791 :   wide_int new_lb = wi::add (lh_lb, rh_lb, s, &ov_lb);
    1802                 :    76248791 :   wide_int new_ub = wi::add (lh_ub, rh_ub, s, &ov_ub);
    1803                 :    76248791 :   value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
    1804                 :    76248791 : }
    1805                 :             : 
    1806                 :             : // Given addition or subtraction, determine the possible NORMAL ranges and
    1807                 :             : // OVERFLOW ranges given an OFFSET range.  ADD_P is true for addition.
    1808                 :             : // Return the relation that exists between the LHS and OP1 in order for the
    1809                 :             : // NORMAL range to apply.
    1810                 :             : // a return value of VREL_VARYING means no ranges were applicable.
    1811                 :             : 
    1812                 :             : static relation_kind
    1813                 :      749248 : plus_minus_ranges (irange &r_ov, irange &r_normal, const irange &offset,
    1814                 :             :                    bool add_p)
    1815                 :             : {
    1816                 :      749248 :   relation_kind kind = VREL_VARYING;
    1817                 :             :   // For now, only deal with constant adds.  This could be extended to ranges
    1818                 :             :   // when someone is so motivated.
    1819                 :      749248 :   if (!offset.singleton_p () || offset.zero_p ())
    1820                 :      731923 :     return kind;
    1821                 :             : 
    1822                 :             :   // Always work with a positive offset.  ie a+ -2 -> a-2  and a- -2 > a+2
    1823                 :       17325 :   wide_int off = offset.lower_bound ();
    1824                 :       17325 :   if (wi::neg_p (off, SIGNED))
    1825                 :             :     {
    1826                 :         831 :       add_p = !add_p;
    1827                 :         831 :       off = wi::neg (off);
    1828                 :             :     }
    1829                 :             : 
    1830                 :       17325 :   wi::overflow_type ov;
    1831                 :       17325 :   tree type = offset.type ();
    1832                 :       17325 :   unsigned prec = TYPE_PRECISION (type);
    1833                 :       17325 :   wide_int ub;
    1834                 :       17325 :   wide_int lb;
    1835                 :             :   // calculate the normal range and relation for the operation.
    1836                 :       17325 :   if (add_p)
    1837                 :             :     {
    1838                 :             :       //  [ 0 , INF - OFF]
    1839                 :       16494 :       lb = wi::zero (prec);
    1840                 :       16494 :       ub = wi::sub (irange_val_max (type), off, UNSIGNED, &ov);
    1841                 :       16494 :       kind = VREL_GT;
    1842                 :             :     }
    1843                 :             :   else
    1844                 :             :     {
    1845                 :             :       //  [ OFF, INF ]
    1846                 :         831 :       lb = off;
    1847                 :         831 :       ub = irange_val_max (type);
    1848                 :         831 :       kind = VREL_LT;
    1849                 :             :     }
    1850                 :       17325 :   int_range<2> normal_range (type, lb, ub);
    1851                 :       17325 :   int_range<2> ov_range (type, lb, ub, VR_ANTI_RANGE);
    1852                 :             : 
    1853                 :       17325 :   r_ov = ov_range;
    1854                 :       17325 :   r_normal = normal_range;
    1855                 :       17325 :   return kind;
    1856                 :       17325 : }
    1857                 :             : 
    1858                 :             : // Once op1 has been calculated by operator_plus or operator_minus, check
    1859                 :             : // to see if the relation passed causes any part of the calculation to
    1860                 :             : // be not possible.  ie
    1861                 :             : // a_2 = b_3 + 1  with a_2 < b_3 can refine the range of b_3 to [INF, INF]
    1862                 :             : // and that further refines a_2 to [0, 0].
    1863                 :             : // R is the value of op1, OP2 is the offset being added/subtracted, REL is the
    1864                 :             : // relation between LHS relation OP1  and ADD_P is true for PLUS, false for
    1865                 :             : // MINUS.    IF any adjustment can be made, R will reflect it.
    1866                 :             : 
    1867                 :             : static void
    1868                 :    10580647 : adjust_op1_for_overflow (irange &r, const irange &op2, relation_kind rel,
    1869                 :             :                          bool add_p)
    1870                 :             : {
    1871                 :    10580647 :   if (r.undefined_p ())
    1872                 :             :     return;
    1873                 :    10580645 :   tree type = r.type ();
    1874                 :             :   // Check for unsigned overflow and calculate the overflow part.
    1875                 :    10580645 :   signop s = TYPE_SIGN (type);
    1876                 :    10580645 :   if (!TYPE_OVERFLOW_WRAPS (type) || s == SIGNED)
    1877                 :             :     return;
    1878                 :             : 
    1879                 :             :   // Only work with <, <=, >, >= relations.
    1880                 :     5458717 :   if (!relation_lt_le_gt_ge_p (rel))
    1881                 :             :     return;
    1882                 :             : 
    1883                 :             :   // Get the ranges for this offset.
    1884                 :      749248 :   int_range_max normal, overflow;
    1885                 :      749248 :   relation_kind k = plus_minus_ranges (overflow, normal, op2, add_p);
    1886                 :             : 
    1887                 :             :   // VREL_VARYING means there are no adjustments.
    1888                 :      749248 :   if (k == VREL_VARYING)
    1889                 :             :     return;
    1890                 :             : 
    1891                 :             :   // If the relations match use the normal range, otherwise use overflow range.
    1892                 :       17325 :   if (relation_intersect (k, rel) == k)
    1893                 :       12231 :     r.intersect (normal);
    1894                 :             :   else
    1895                 :        5094 :     r.intersect (overflow);
    1896                 :             :   return;
    1897                 :      749248 : }
    1898                 :             : 
    1899                 :             : bool
    1900                 :     9529024 : operator_plus::op1_range (irange &r, tree type,
    1901                 :             :                           const irange &lhs,
    1902                 :             :                           const irange &op2,
    1903                 :             :                           relation_trio trio) const
    1904                 :             : {
    1905                 :     9529024 :   if (lhs.undefined_p ())
    1906                 :             :     return false;
    1907                 :             :   // Start with the default operation.
    1908                 :     9529024 :   range_op_handler minus (MINUS_EXPR);
    1909                 :     9529024 :   if (!minus)
    1910                 :             :     return false;
    1911                 :     9529024 :   bool res = minus.fold_range (r, type, lhs, op2);
    1912                 :     9529024 :   relation_kind rel = trio.lhs_op1 ();
    1913                 :             :   // Check for a relation refinement.
    1914                 :     9529024 :   if (res)
    1915                 :     9529024 :     adjust_op1_for_overflow (r, op2, rel, true /* PLUS_EXPR */);
    1916                 :             :   return res;
    1917                 :             : }
    1918                 :             : 
    1919                 :             : bool
    1920                 :     2039561 : operator_plus::op2_range (irange &r, tree type,
    1921                 :             :                           const irange &lhs,
    1922                 :             :                           const irange &op1,
    1923                 :             :                           relation_trio rel) const
    1924                 :             : {
    1925                 :     2039561 :   return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
    1926                 :             : }
    1927                 :             : 
    1928                 :             : class operator_widen_plus_signed : public range_operator
    1929                 :             : {
    1930                 :             : public:
    1931                 :             :   virtual void wi_fold (irange &r, tree type,
    1932                 :             :                         const wide_int &lh_lb,
    1933                 :             :                         const wide_int &lh_ub,
    1934                 :             :                         const wide_int &rh_lb,
    1935                 :             :                         const wide_int &rh_ub) const;
    1936                 :             : } op_widen_plus_signed;
    1937                 :             : 
    1938                 :             : void
    1939                 :           0 : operator_widen_plus_signed::wi_fold (irange &r, tree type,
    1940                 :             :                                      const wide_int &lh_lb,
    1941                 :             :                                      const wide_int &lh_ub,
    1942                 :             :                                      const wide_int &rh_lb,
    1943                 :             :                                      const wide_int &rh_ub) const
    1944                 :             : {
    1945                 :           0 :    wi::overflow_type ov_lb, ov_ub;
    1946                 :           0 :    signop s = TYPE_SIGN (type);
    1947                 :             : 
    1948                 :           0 :    wide_int lh_wlb
    1949                 :           0 :      = wide_int::from (lh_lb, wi::get_precision (lh_lb) * 2, SIGNED);
    1950                 :           0 :    wide_int lh_wub
    1951                 :           0 :      = wide_int::from (lh_ub, wi::get_precision (lh_ub) * 2, SIGNED);
    1952                 :           0 :    wide_int rh_wlb = wide_int::from (rh_lb, wi::get_precision (rh_lb) * 2, s);
    1953                 :           0 :    wide_int rh_wub = wide_int::from (rh_ub, wi::get_precision (rh_ub) * 2, s);
    1954                 :             : 
    1955                 :           0 :    wide_int new_lb = wi::add (lh_wlb, rh_wlb, s, &ov_lb);
    1956                 :           0 :    wide_int new_ub = wi::add (lh_wub, rh_wub, s, &ov_ub);
    1957                 :             : 
    1958                 :           0 :    r = int_range<2> (type, new_lb, new_ub);
    1959                 :           0 : }
    1960                 :             : 
    1961                 :             : class operator_widen_plus_unsigned : public range_operator
    1962                 :             : {
    1963                 :             : public:
    1964                 :             :   virtual void wi_fold (irange &r, tree type,
    1965                 :             :                         const wide_int &lh_lb,
    1966                 :             :                         const wide_int &lh_ub,
    1967                 :             :                         const wide_int &rh_lb,
    1968                 :             :                         const wide_int &rh_ub) const;
    1969                 :             : } op_widen_plus_unsigned;
    1970                 :             : 
    1971                 :             : void
    1972                 :           0 : operator_widen_plus_unsigned::wi_fold (irange &r, tree type,
    1973                 :             :                                        const wide_int &lh_lb,
    1974                 :             :                                        const wide_int &lh_ub,
    1975                 :             :                                        const wide_int &rh_lb,
    1976                 :             :                                        const wide_int &rh_ub) const
    1977                 :             : {
    1978                 :           0 :    wi::overflow_type ov_lb, ov_ub;
    1979                 :           0 :    signop s = TYPE_SIGN (type);
    1980                 :             : 
    1981                 :           0 :    wide_int lh_wlb
    1982                 :           0 :      = wide_int::from (lh_lb, wi::get_precision (lh_lb) * 2, UNSIGNED);
    1983                 :           0 :    wide_int lh_wub
    1984                 :           0 :      = wide_int::from (lh_ub, wi::get_precision (lh_ub) * 2, UNSIGNED);
    1985                 :           0 :    wide_int rh_wlb = wide_int::from (rh_lb, wi::get_precision (rh_lb) * 2, s);
    1986                 :           0 :    wide_int rh_wub = wide_int::from (rh_ub, wi::get_precision (rh_ub) * 2, s);
    1987                 :             : 
    1988                 :           0 :    wide_int new_lb = wi::add (lh_wlb, rh_wlb, s, &ov_lb);
    1989                 :           0 :    wide_int new_ub = wi::add (lh_wub, rh_wub, s, &ov_ub);
    1990                 :             : 
    1991                 :           0 :    r = int_range<2> (type, new_lb, new_ub);
    1992                 :           0 : }
    1993                 :             : 
    1994                 :             : void
    1995                 :    18582378 : operator_minus::update_bitmask (irange &r, const irange &lh,
    1996                 :             :                                 const irange &rh) const
    1997                 :             : {
    1998                 :    18582378 :   update_known_bitmask (r, MINUS_EXPR, lh, rh);
    1999                 :    18582378 : }
    2000                 :             : 
    2001                 :             : void
    2002                 :    26332935 : operator_minus::wi_fold (irange &r, tree type,
    2003                 :             :                          const wide_int &lh_lb, const wide_int &lh_ub,
    2004                 :             :                          const wide_int &rh_lb, const wide_int &rh_ub) const
    2005                 :             : {
    2006                 :    26332935 :   wi::overflow_type ov_lb, ov_ub;
    2007                 :    26332935 :   signop s = TYPE_SIGN (type);
    2008                 :    26332935 :   wide_int new_lb = wi::sub (lh_lb, rh_ub, s, &ov_lb);
    2009                 :    26332935 :   wide_int new_ub = wi::sub (lh_ub, rh_lb, s, &ov_ub);
    2010                 :    26332935 :   value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
    2011                 :    26332935 : }
    2012                 :             : 
    2013                 :             : 
    2014                 :             : // Return the relation between LHS and OP1 based on the relation between
    2015                 :             : // OP1 and OP2.
    2016                 :             : 
    2017                 :             : relation_kind
    2018                 :     4862183 : operator_minus::lhs_op1_relation (const irange &, const irange &op1,
    2019                 :             :                                   const irange &, relation_kind rel) const
    2020                 :             : {
    2021                 :     4862183 :   if (!op1.undefined_p () && TYPE_SIGN (op1.type ()) == UNSIGNED)
    2022                 :     2526661 :     switch (rel)
    2023                 :             :       {
    2024                 :             :       case VREL_GT:
    2025                 :             :       case VREL_GE:
    2026                 :             :         return VREL_LE;
    2027                 :             :       default:
    2028                 :             :         break;
    2029                 :             :       }
    2030                 :             :   return VREL_VARYING;
    2031                 :             : }
    2032                 :             : 
    2033                 :             : // Check to see if the relation REL between OP1 and OP2 has any effect on the
    2034                 :             : // LHS of the expression.  If so, apply it to LHS_RANGE.  This is a helper
    2035                 :             : // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
    2036                 :             : 
    2037                 :             : bool
    2038                 :    21327339 : minus_op1_op2_relation_effect (irange &lhs_range, tree type,
    2039                 :             :                                const irange &op1_range ATTRIBUTE_UNUSED,
    2040                 :             :                                const irange &op2_range ATTRIBUTE_UNUSED,
    2041                 :             :                                relation_kind rel)
    2042                 :             : {
    2043                 :    21327339 :   if (rel == VREL_VARYING)
    2044                 :             :     return false;
    2045                 :             : 
    2046                 :      263832 :   int_range<2> rel_range;
    2047                 :      263832 :   unsigned prec = TYPE_PRECISION (type);
    2048                 :      263832 :   signop sgn = TYPE_SIGN (type);
    2049                 :             : 
    2050                 :             :   // == and != produce [0,0] and ~[0,0] regardless of wrapping.
    2051                 :      263832 :   if (rel == VREL_EQ)
    2052                 :        8139 :     rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec));
    2053                 :      255693 :   else if (rel == VREL_NE)
    2054                 :      107102 :     rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
    2055                 :       53551 :                               VR_ANTI_RANGE);
    2056                 :      202142 :   else if (TYPE_OVERFLOW_WRAPS (type))
    2057                 :             :     {
    2058                 :      132741 :       switch (rel)
    2059                 :             :         {
    2060                 :             :           // For wrapping signed values and unsigned, if op1 > op2 or
    2061                 :             :           // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
    2062                 :       50592 :           case VREL_GT:
    2063                 :       50592 :           case VREL_LT:
    2064                 :      101184 :               rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
    2065                 :       50592 :                                         VR_ANTI_RANGE);
    2066                 :       50592 :             break;
    2067                 :             :           default:
    2068                 :             :             return false;
    2069                 :             :         }
    2070                 :             :     }
    2071                 :             :   else
    2072                 :             :     {
    2073                 :       69401 :       switch (rel)
    2074                 :             :         {
    2075                 :             :           // op1 > op2, op1 - op2 can be restricted to [1, +INF]
    2076                 :       22068 :           case VREL_GT:
    2077                 :       44136 :             rel_range = int_range<2> (type, wi::one (prec),
    2078                 :       44136 :                                       wi::max_value (prec, sgn));
    2079                 :       22068 :             break;
    2080                 :             :           // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
    2081                 :       46604 :           case VREL_GE:
    2082                 :       93208 :             rel_range = int_range<2> (type, wi::zero (prec),
    2083                 :       93208 :                                       wi::max_value (prec, sgn));
    2084                 :       46604 :             break;
    2085                 :             :           // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
    2086                 :         288 :           case VREL_LT:
    2087                 :         576 :             rel_range = int_range<2> (type, wi::min_value (prec, sgn),
    2088                 :         288 :                                       wi::minus_one (prec));
    2089                 :         288 :             break;
    2090                 :             :           // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
    2091                 :         275 :           case VREL_LE:
    2092                 :         550 :             rel_range = int_range<2> (type, wi::min_value (prec, sgn),
    2093                 :         275 :                                       wi::zero (prec));
    2094                 :         275 :             break;
    2095                 :             :           default:
    2096                 :             :             return false;
    2097                 :             :         }
    2098                 :             :     }
    2099                 :      181517 :   lhs_range.intersect (rel_range);
    2100                 :      181517 :   return true;
    2101                 :      263832 : }
    2102                 :             : 
    2103                 :             : bool
    2104                 :    18582378 : operator_minus::op1_op2_relation_effect (irange &lhs_range, tree type,
    2105                 :             :                                          const irange &op1_range,
    2106                 :             :                                          const irange &op2_range,
    2107                 :             :                                          relation_kind rel) const
    2108                 :             : {
    2109                 :    18582378 :   return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
    2110                 :    18582378 :                                         rel);
    2111                 :             : }
    2112                 :             : 
    2113                 :             : bool
    2114                 :     1051623 : operator_minus::op1_range (irange &r, tree type,
    2115                 :             :                            const irange &lhs,
    2116                 :             :                            const irange &op2,
    2117                 :             :                            relation_trio trio) const
    2118                 :             : {
    2119                 :     1051623 :   if (lhs.undefined_p ())
    2120                 :             :     return false;
    2121                 :             :   // Start with the default operation.
    2122                 :     1051623 :   range_op_handler minus (PLUS_EXPR);
    2123                 :     1051623 :   if (!minus)
    2124                 :             :     return false;
    2125                 :     1051623 :   bool res = minus.fold_range (r, type, lhs, op2);
    2126                 :     1051623 :   relation_kind rel = trio.lhs_op1 ();
    2127                 :     1051623 :   if (res)
    2128                 :     1051623 :     adjust_op1_for_overflow (r, op2, rel, false /* PLUS_EXPR */);
    2129                 :             :   return res;
    2130                 :             : 
    2131                 :             : }
    2132                 :             : 
    2133                 :             : bool
    2134                 :     1722383 : operator_minus::op2_range (irange &r, tree type,
    2135                 :             :                            const irange &lhs,
    2136                 :             :                            const irange &op1,
    2137                 :             :                            relation_trio) const
    2138                 :             : {
    2139                 :     1722383 :   if (lhs.undefined_p ())
    2140                 :             :     return false;
    2141                 :     1722383 :   return fold_range (r, type, op1, lhs);
    2142                 :             : }
    2143                 :             : 
    2144                 :             : void
    2145                 :      858142 : operator_min::update_bitmask (irange &r, const irange &lh,
    2146                 :             :                               const irange &rh) const
    2147                 :             : {
    2148                 :      858142 :   update_known_bitmask (r, MIN_EXPR, lh, rh);
    2149                 :      858142 : }
    2150                 :             : 
    2151                 :             : void
    2152                 :     1363209 : operator_min::wi_fold (irange &r, tree type,
    2153                 :             :                        const wide_int &lh_lb, const wide_int &lh_ub,
    2154                 :             :                        const wide_int &rh_lb, const wide_int &rh_ub) const
    2155                 :             : {
    2156                 :     1363209 :   signop s = TYPE_SIGN (type);
    2157                 :     1363209 :   wide_int new_lb = wi::min (lh_lb, rh_lb, s);
    2158                 :     1363209 :   wide_int new_ub = wi::min (lh_ub, rh_ub, s);
    2159                 :     1363209 :   value_range_with_overflow (r, type, new_lb, new_ub);
    2160                 :     1363209 : }
    2161                 :             : 
    2162                 :             : 
    2163                 :             : void
    2164                 :      661215 : operator_max::update_bitmask (irange &r, const irange &lh,
    2165                 :             :                               const irange &rh) const
    2166                 :             : {
    2167                 :      661215 :   update_known_bitmask (r, MAX_EXPR, lh, rh);
    2168                 :      661215 : }
    2169                 :             : 
    2170                 :             : void
    2171                 :      788507 : operator_max::wi_fold (irange &r, tree type,
    2172                 :             :                        const wide_int &lh_lb, const wide_int &lh_ub,
    2173                 :             :                        const wide_int &rh_lb, const wide_int &rh_ub) const
    2174                 :             : {
    2175                 :      788507 :   signop s = TYPE_SIGN (type);
    2176                 :      788507 :   wide_int new_lb = wi::max (lh_lb, rh_lb, s);
    2177                 :      788507 :   wide_int new_ub = wi::max (lh_ub, rh_ub, s);
    2178                 :      788507 :   value_range_with_overflow (r, type, new_lb, new_ub);
    2179                 :      788507 : }
    2180                 :             : 
    2181                 :             : 
    2182                 :             : // Calculate the cross product of two sets of ranges and return it.
    2183                 :             : //
    2184                 :             : // Multiplications, divisions and shifts are a bit tricky to handle,
    2185                 :             : // depending on the mix of signs we have in the two ranges, we need to
    2186                 :             : // operate on different values to get the minimum and maximum values
    2187                 :             : // for the new range.  One approach is to figure out all the
    2188                 :             : // variations of range combinations and do the operations.
    2189                 :             : //
    2190                 :             : // However, this involves several calls to compare_values and it is
    2191                 :             : // pretty convoluted.  It's simpler to do the 4 operations (MIN0 OP
    2192                 :             : // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
    2193                 :             : // figure the smallest and largest values to form the new range.
    2194                 :             : 
    2195                 :             : void
    2196                 :    14313820 : cross_product_operator::wi_cross_product (irange &r, tree type,
    2197                 :             :                                           const wide_int &lh_lb,
    2198                 :             :                                           const wide_int &lh_ub,
    2199                 :             :                                           const wide_int &rh_lb,
    2200                 :             :                                           const wide_int &rh_ub) const
    2201                 :             : {
    2202                 :    14313820 :   wide_int cp1, cp2, cp3, cp4;
    2203                 :             :   // Default to varying.
    2204                 :    14313820 :   r.set_varying (type);
    2205                 :             : 
    2206                 :             :   // Compute the 4 cross operations, bailing if we get an overflow we
    2207                 :             :   // can't handle.
    2208                 :    14313820 :   if (wi_op_overflows (cp1, type, lh_lb, rh_lb))
    2209                 :             :     return;
    2210                 :    14313816 :   if (wi::eq_p (lh_lb, lh_ub))
    2211                 :     4250272 :     cp3 = cp1;
    2212                 :    10063544 :   else if (wi_op_overflows (cp3, type, lh_ub, rh_lb))
    2213                 :             :     return;
    2214                 :    14313816 :   if (wi::eq_p (rh_lb, rh_ub))
    2215                 :    11060805 :     cp2 = cp1;
    2216                 :     3253011 :   else if (wi_op_overflows (cp2, type, lh_lb, rh_ub))
    2217                 :             :     return;
    2218                 :    14311546 :   if (wi::eq_p (lh_lb, lh_ub))
    2219                 :     4250252 :     cp4 = cp2;
    2220                 :    10061294 :   else if (wi_op_overflows (cp4, type, lh_ub, rh_ub))
    2221                 :             :     return;
    2222                 :             : 
    2223                 :             :   // Order pairs.
    2224                 :    14311546 :   signop sign = TYPE_SIGN (type);
    2225                 :    14311546 :   if (wi::gt_p (cp1, cp2, sign))
    2226                 :     1481164 :     std::swap (cp1, cp2);
    2227                 :    14311546 :   if (wi::gt_p (cp3, cp4, sign))
    2228                 :     1472937 :     std::swap (cp3, cp4);
    2229                 :             : 
    2230                 :             :   // Choose min and max from the ordered pairs.
    2231                 :    14311546 :   wide_int res_lb = wi::min (cp1, cp3, sign);
    2232                 :    14311546 :   wide_int res_ub = wi::max (cp2, cp4, sign);
    2233                 :    14311546 :   value_range_with_overflow (r, type, res_lb, res_ub);
    2234                 :    14315375 : }
    2235                 :             : 
    2236                 :             : 
    2237                 :             : void
    2238                 :    13577249 : operator_mult::update_bitmask (irange &r, const irange &lh,
    2239                 :             :                                const irange &rh) const
    2240                 :             : {
    2241                 :    13577249 :   update_known_bitmask (r, MULT_EXPR, lh, rh);
    2242                 :    13577249 : }
    2243                 :             : 
    2244                 :             : bool
    2245                 :     1188894 : operator_mult::op1_range (irange &r, tree type,
    2246                 :             :                           const irange &lhs, const irange &op2,
    2247                 :             :                           relation_trio) const
    2248                 :             : {
    2249                 :     1188894 :   if (lhs.undefined_p ())
    2250                 :             :     return false;
    2251                 :             : 
    2252                 :             :   // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
    2253                 :             :   // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
    2254                 :             :   // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
    2255                 :     1188894 :   if (TYPE_OVERFLOW_WRAPS (type))
    2256                 :             :     return false;
    2257                 :             : 
    2258                 :      331221 :   wide_int offset;
    2259                 :      331221 :   if (op2.singleton_p (offset) && offset != 0)
    2260                 :      233642 :     return range_op_handler (TRUNC_DIV_EXPR).fold_range (r, type, lhs, op2);
    2261                 :             : 
    2262                 :             :   //  ~[0, 0] = op1 * op2  defines op1 and op2 as non-zero.
    2263                 :       97579 :   if (!lhs.contains_p (wi::zero (TYPE_PRECISION (lhs.type ()))))
    2264                 :             :     {
    2265                 :       21083 :       r.set_nonzero (type);
    2266                 :       21083 :       return true;
    2267                 :             :     }
    2268                 :             :   return false;
    2269                 :      331221 : }
    2270                 :             : 
    2271                 :             : bool
    2272                 :      123210 : operator_mult::op2_range (irange &r, tree type,
    2273                 :             :                           const irange &lhs, const irange &op1,
    2274                 :             :                           relation_trio rel) const
    2275                 :             : {
    2276                 :      123210 :   return operator_mult::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
    2277                 :             : }
    2278                 :             : 
    2279                 :             : bool
    2280                 :    13813135 : operator_mult::wi_op_overflows (wide_int &res, tree type,
    2281                 :             :                                 const wide_int &w0, const wide_int &w1) const
    2282                 :             : {
    2283                 :    13813135 :   wi::overflow_type overflow = wi::OVF_NONE;
    2284                 :    13813135 :   signop sign = TYPE_SIGN (type);
    2285                 :    13813135 :   res = wi::mul (w0, w1, sign, &overflow);
    2286                 :    13813135 :    if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
    2287                 :             :      {
    2288                 :             :        // For multiplication, the sign of the overflow is given
    2289                 :             :        // by the comparison of the signs of the operands.
    2290                 :     6540486 :        if (sign == UNSIGNED || w0.sign_mask () == w1.sign_mask ())
    2291                 :     3605994 :          res = wi::max_value (w0.get_precision (), sign);
    2292                 :             :        else
    2293                 :     2934672 :          res = wi::min_value (w0.get_precision (), sign);
    2294                 :     6540486 :        return false;
    2295                 :             :      }
    2296                 :     7272649 :    return overflow;
    2297                 :             : }
    2298                 :             : 
    2299                 :             : void
    2300                 :    17222619 : operator_mult::wi_fold (irange &r, tree type,
    2301                 :             :                         const wide_int &lh_lb, const wide_int &lh_ub,
    2302                 :             :                         const wide_int &rh_lb, const wide_int &rh_ub) const
    2303                 :             : {
    2304                 :    17222619 :   if (TYPE_OVERFLOW_UNDEFINED (type))
    2305                 :             :     {
    2306                 :     5754390 :       wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
    2307                 :     5754390 :       return;
    2308                 :             :     }
    2309                 :             : 
    2310                 :             :   // Multiply the ranges when overflow wraps.  This is basically fancy
    2311                 :             :   // code so we don't drop to varying with an unsigned
    2312                 :             :   // [-3,-1]*[-3,-1].
    2313                 :             :   //
    2314                 :             :   // This test requires 2*prec bits if both operands are signed and
    2315                 :             :   // 2*prec + 2 bits if either is not.  Therefore, extend the values
    2316                 :             :   // using the sign of the result to PREC2.  From here on out,
    2317                 :             :   // everything is just signed math no matter what the input types
    2318                 :             :   // were.
    2319                 :             : 
    2320                 :    11468229 :   signop sign = TYPE_SIGN (type);
    2321                 :    11468229 :   unsigned prec = TYPE_PRECISION (type);
    2322                 :    11468229 :   widest2_int min0 = widest2_int::from (lh_lb, sign);
    2323                 :    11468229 :   widest2_int max0 = widest2_int::from (lh_ub, sign);
    2324                 :    11468229 :   widest2_int min1 = widest2_int::from (rh_lb, sign);
    2325                 :    11468229 :   widest2_int max1 = widest2_int::from (rh_ub, sign);
    2326                 :    11468229 :   widest2_int sizem1 = wi::mask <widest2_int> (prec, false);
    2327                 :    11468229 :   widest2_int size = sizem1 + 1;
    2328                 :             : 
    2329                 :             :   // Canonicalize the intervals.
    2330                 :    11468229 :   if (sign == UNSIGNED)
    2331                 :             :     {
    2332                 :    10786893 :       if (wi::ltu_p (size, min0 + max0))
    2333                 :             :         {
    2334                 :     1536024 :           min0 -= size;
    2335                 :     1536024 :           max0 -= size;
    2336                 :             :         }
    2337                 :    10786860 :       if (wi::ltu_p (size, min1 + max1))
    2338                 :             :         {
    2339                 :      154486 :           min1 -= size;
    2340                 :      154486 :           max1 -= size;
    2341                 :             :         }
    2342                 :             :     }
    2343                 :             : 
    2344                 :             :   // Sort the 4 products so that min is in prod0 and max is in
    2345                 :             :   // prod3.
    2346                 :    11468229 :   widest2_int prod0 = min0 * min1;
    2347                 :    11468229 :   widest2_int prod1 = min0 * max1;
    2348                 :    11468229 :   widest2_int prod2 = max0 * min1;
    2349                 :    11468229 :   widest2_int prod3 = max0 * max1;
    2350                 :             : 
    2351                 :             :   // min0min1 > max0max1
    2352                 :    11468229 :   if (prod0 > prod3)
    2353                 :      184776 :     std::swap (prod0, prod3);
    2354                 :             : 
    2355                 :             :   // min0max1 > max0min1
    2356                 :    11468229 :   if (prod1 > prod2)
    2357                 :      301180 :     std::swap (prod1, prod2);
    2358                 :             : 
    2359                 :    11468229 :   if (prod0 > prod1)
    2360                 :       78610 :     std::swap (prod0, prod1);
    2361                 :             : 
    2362                 :    11468229 :   if (prod2 > prod3)
    2363                 :        3984 :     std::swap (prod2, prod3);
    2364                 :             : 
    2365                 :             :   // diff = max - min
    2366                 :    11468229 :   prod2 = prod3 - prod0;
    2367                 :    11468229 :   if (wi::geu_p (prod2, sizem1))
    2368                 :             :     {
    2369                 :             :       // Multiplying by X, where X is a power of 2 is [0,0][X,+INF].
    2370                 :     7649193 :       if (TYPE_UNSIGNED (type) && rh_lb == rh_ub
    2371                 :     7056895 :           && wi::exact_log2 (rh_lb) != -1 && prec > 1)
    2372                 :             :         {
    2373                 :     2646260 :           r.set (type, rh_lb, wi::max_value (prec, sign));
    2374                 :     2646260 :           int_range<2> zero;
    2375                 :     2646260 :           zero.set_zero (type);
    2376                 :     2646260 :           r.union_ (zero);
    2377                 :     2646260 :         }
    2378                 :             :       else
    2379                 :             :         // The range covers all values.
    2380                 :     1248673 :         r.set_varying (type);
    2381                 :             :     }
    2382                 :             :   else
    2383                 :             :     {
    2384                 :     7573296 :       wide_int new_lb = wide_int::from (prod0, prec, sign);
    2385                 :     7573296 :       wide_int new_ub = wide_int::from (prod3, prec, sign);
    2386                 :     7573296 :       create_possibly_reversed_range (r, type, new_lb, new_ub);
    2387                 :     7573339 :     }
    2388                 :    11468825 : }
    2389                 :             : 
    2390                 :             : class operator_widen_mult_signed : public range_operator
    2391                 :             : {
    2392                 :             : public:
    2393                 :             :   virtual void wi_fold (irange &r, tree type,
    2394                 :             :                         const wide_int &lh_lb,
    2395                 :             :                         const wide_int &lh_ub,
    2396                 :             :                         const wide_int &rh_lb,
    2397                 :             :                         const wide_int &rh_ub)
    2398                 :             :     const;
    2399                 :             : } op_widen_mult_signed;
    2400                 :             : 
    2401                 :             : void
    2402                 :        1347 : operator_widen_mult_signed::wi_fold (irange &r, tree type,
    2403                 :             :                                      const wide_int &lh_lb,
    2404                 :             :                                      const wide_int &lh_ub,
    2405                 :             :                                      const wide_int &rh_lb,
    2406                 :             :                                      const wide_int &rh_ub) const
    2407                 :             : {
    2408                 :        1347 :   signop s = TYPE_SIGN (type);
    2409                 :             : 
    2410                 :        1347 :   wide_int lh_wlb = wide_int::from (lh_lb, wi::get_precision (lh_lb) * 2, SIGNED);
    2411                 :        1347 :   wide_int lh_wub = wide_int::from (lh_ub, wi::get_precision (lh_ub) * 2, SIGNED);
    2412                 :        1347 :   wide_int rh_wlb = wide_int::from (rh_lb, wi::get_precision (rh_lb) * 2, s);
    2413                 :        1347 :   wide_int rh_wub = wide_int::from (rh_ub, wi::get_precision (rh_ub) * 2, s);
    2414                 :             : 
    2415                 :             :   /* We don't expect a widening multiplication to be able to overflow but range
    2416                 :             :      calculations for multiplications are complicated.  After widening the
    2417                 :             :      operands lets call the base class.  */
    2418                 :        1347 :   return op_mult.wi_fold (r, type, lh_wlb, lh_wub, rh_wlb, rh_wub);
    2419                 :        1347 : }
    2420                 :             : 
    2421                 :             : 
    2422                 :             : class operator_widen_mult_unsigned : public range_operator
    2423                 :             : {
    2424                 :             : public:
    2425                 :             :   virtual void wi_fold (irange &r, tree type,
    2426                 :             :                         const wide_int &lh_lb,
    2427                 :             :                         const wide_int &lh_ub,
    2428                 :             :                         const wide_int &rh_lb,
    2429                 :             :                         const wide_int &rh_ub)
    2430                 :             :     const;
    2431                 :             : } op_widen_mult_unsigned;
    2432                 :             : 
    2433                 :             : void
    2434                 :        5502 : operator_widen_mult_unsigned::wi_fold (irange &r, tree type,
    2435                 :             :                                        const wide_int &lh_lb,
    2436                 :             :                                        const wide_int &lh_ub,
    2437                 :             :                                        const wide_int &rh_lb,
    2438                 :             :                                        const wide_int &rh_ub) const
    2439                 :             : {
    2440                 :        5502 :   signop s = TYPE_SIGN (type);
    2441                 :             : 
    2442                 :        5502 :   wide_int lh_wlb = wide_int::from (lh_lb, wi::get_precision (lh_lb) * 2, UNSIGNED);
    2443                 :        5502 :   wide_int lh_wub = wide_int::from (lh_ub, wi::get_precision (lh_ub) * 2, UNSIGNED);
    2444                 :        5502 :   wide_int rh_wlb = wide_int::from (rh_lb, wi::get_precision (rh_lb) * 2, s);
    2445                 :        5502 :   wide_int rh_wub = wide_int::from (rh_ub, wi::get_precision (rh_ub) * 2, s);
    2446                 :             : 
    2447                 :             :   /* We don't expect a widening multiplication to be able to overflow but range
    2448                 :             :      calculations for multiplications are complicated.  After widening the
    2449                 :             :      operands lets call the base class.  */
    2450                 :        5502 :   return op_mult.wi_fold (r, type, lh_wlb, lh_wub, rh_wlb, rh_wub);
    2451                 :        5502 : }
    2452                 :             : 
    2453                 :             : class operator_div : public cross_product_operator
    2454                 :             : {
    2455                 :             :   using range_operator::update_bitmask;
    2456                 :             :   using range_operator::op2_range;
    2457                 :             : public:
    2458                 :             :   operator_div (tree_code div_kind) { m_code = div_kind; }
    2459                 :             :   bool op2_range (irange &r, tree type, const irange &lhs, const irange &,
    2460                 :             :                   relation_trio) const final override;
    2461                 :             :   virtual void wi_fold (irange &r, tree type,
    2462                 :             :                         const wide_int &lh_lb,
    2463                 :             :                         const wide_int &lh_ub,
    2464                 :             :                         const wide_int &rh_lb,
    2465                 :             :                         const wide_int &rh_ub) const final override;
    2466                 :             :   virtual bool wi_op_overflows (wide_int &res, tree type,
    2467                 :             :                                 const wide_int &, const wide_int &)
    2468                 :             :     const final override;
    2469                 :     2807818 :   void update_bitmask (irange &r, const irange &lh, const irange &rh)
    2470                 :             :     const final override
    2471                 :     2807818 :     { update_known_bitmask (r, m_code, lh, rh); }
    2472                 :             : protected:
    2473                 :             :   tree_code m_code;
    2474                 :             : };
    2475                 :             : 
    2476                 :             : static operator_div op_trunc_div (TRUNC_DIV_EXPR);
    2477                 :             : static operator_div op_floor_div (FLOOR_DIV_EXPR);
    2478                 :             : static operator_div op_round_div (ROUND_DIV_EXPR);
    2479                 :             : static operator_div op_ceil_div (CEIL_DIV_EXPR);
    2480                 :             : 
    2481                 :             : // Set OP2 to non-zero if the LHS isn't UNDEFINED.
    2482                 :             : bool
    2483                 :       38685 : operator_div::op2_range (irange &r, tree type, const irange &lhs,
    2484                 :             :                          const irange &, relation_trio) const
    2485                 :             : {
    2486                 :       38685 :   if (!lhs.undefined_p ())
    2487                 :             :     {
    2488                 :       38685 :       r.set_nonzero (type);
    2489                 :       38685 :       return true;
    2490                 :             :     }
    2491                 :             :   return false;
    2492                 :             : }
    2493                 :             : 
    2494                 :             : bool
    2495                 :    12130347 : operator_div::wi_op_overflows (wide_int &res, tree type,
    2496                 :             :                                const wide_int &w0, const wide_int &w1) const
    2497                 :             : {
    2498                 :    12130347 :   if (w1 == 0)
    2499                 :             :     return true;
    2500                 :             : 
    2501                 :    12130347 :   wi::overflow_type overflow = wi::OVF_NONE;
    2502                 :    12130347 :   signop sign = TYPE_SIGN (type);
    2503                 :             : 
    2504                 :    12130347 :   switch (m_code)
    2505                 :             :     {
    2506                 :    12025627 :     case EXACT_DIV_EXPR:
    2507                 :    12025627 :     case TRUNC_DIV_EXPR:
    2508                 :    12025627 :       res = wi::div_trunc (w0, w1, sign, &overflow);
    2509                 :    12025627 :       break;
    2510                 :       92910 :     case FLOOR_DIV_EXPR:
    2511                 :       92910 :       res = wi::div_floor (w0, w1, sign, &overflow);
    2512                 :       92910 :       break;
    2513                 :         288 :     case ROUND_DIV_EXPR:
    2514                 :         288 :       res = wi::div_round (w0, w1, sign, &overflow);
    2515                 :         288 :       break;
    2516                 :       11522 :     case CEIL_DIV_EXPR:
    2517                 :       11522 :       res = wi::div_ceil (w0, w1, sign, &overflow);
    2518                 :       11522 :       break;
    2519                 :           0 :     default:
    2520                 :           0 :       gcc_unreachable ();
    2521                 :             :     }
    2522                 :             : 
    2523                 :    12130347 :   if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
    2524                 :             :     {
    2525                 :             :       // For division, the only case is -INF / -1 = +INF.
    2526                 :      208733 :       res = wi::max_value (w0.get_precision (), sign);
    2527                 :      208733 :       return false;
    2528                 :             :     }
    2529                 :    11921614 :   return overflow;
    2530                 :             : }
    2531                 :             : 
    2532                 :             : void
    2533                 :     3898077 : operator_div::wi_fold (irange &r, tree type,
    2534                 :             :                        const wide_int &lh_lb, const wide_int &lh_ub,
    2535                 :             :                        const wide_int &rh_lb, const wide_int &rh_ub) const
    2536                 :             : {
    2537                 :     3898077 :   const wide_int dividend_min = lh_lb;
    2538                 :     3898077 :   const wide_int dividend_max = lh_ub;
    2539                 :     3898077 :   const wide_int divisor_min = rh_lb;
    2540                 :     3898077 :   const wide_int divisor_max = rh_ub;
    2541                 :     3898077 :   signop sign = TYPE_SIGN (type);
    2542                 :     3898077 :   unsigned prec = TYPE_PRECISION (type);
    2543                 :     3898077 :   wide_int extra_min, extra_max;
    2544                 :             : 
    2545                 :             :   // If we know we won't divide by zero, just do the division.
    2546                 :     3898077 :   if (!wi_includes_zero_p (type, divisor_min, divisor_max))
    2547                 :             :     {
    2548                 :     3213020 :       wi_cross_product (r, type, dividend_min, dividend_max,
    2549                 :             :                        divisor_min, divisor_max);
    2550                 :     3213020 :       return;
    2551                 :             :     }
    2552                 :             : 
    2553                 :             :   // If we're definitely dividing by zero, there's nothing to do.
    2554                 :      685057 :   if (wi_zero_p (type, divisor_min, divisor_max))
    2555                 :             :     {
    2556                 :       16825 :       r.set_undefined ();
    2557                 :       16825 :       return;
    2558                 :             :     }
    2559                 :             : 
    2560                 :             :   // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
    2561                 :             :   // skip any division by zero.
    2562                 :             : 
    2563                 :             :   // First divide by the negative numbers, if any.
    2564                 :      668232 :   if (wi::neg_p (divisor_min, sign))
    2565                 :      857036 :     wi_cross_product (r, type, dividend_min, dividend_max,
    2566                 :      857036 :                       divisor_min, wi::minus_one (prec));
    2567                 :             :   else
    2568                 :      239714 :     r.set_undefined ();
    2569                 :             : 
    2570                 :             :   // Then divide by the non-zero positive numbers, if any.
    2571                 :      668232 :   if (wi::gt_p (divisor_max, wi::zero (prec), sign))
    2572                 :             :     {
    2573                 :      667476 :       int_range_max tmp;
    2574                 :     1334952 :       wi_cross_product (tmp, type, dividend_min, dividend_max,
    2575                 :      667476 :                         wi::one (prec), divisor_max);
    2576                 :      667476 :       r.union_ (tmp);
    2577                 :      667476 :     }
    2578                 :             :   // We shouldn't still have undefined here.
    2579                 :      668232 :   gcc_checking_assert (!r.undefined_p ());
    2580                 :     3898729 : }
    2581                 :             : 
    2582                 :             : 
    2583                 :             : class operator_exact_divide : public operator_div
    2584                 :             : {
    2585                 :             :   using range_operator::op1_range;
    2586                 :             : public:
    2587                 :             :   operator_exact_divide () : operator_div (EXACT_DIV_EXPR) { }
    2588                 :             :   virtual bool op1_range (irange &r, tree type,
    2589                 :             :                           const irange &lhs,
    2590                 :             :                           const irange &op2,
    2591                 :             :                           relation_trio) const;
    2592                 :             : 
    2593                 :             : } op_exact_div;
    2594                 :             : 
    2595                 :             : bool
    2596                 :      488988 : operator_exact_divide::op1_range (irange &r, tree type,
    2597                 :             :                                   const irange &lhs,
    2598                 :             :                                   const irange &op2,
    2599                 :             :                                   relation_trio) const
    2600                 :             : {
    2601                 :      488988 :   if (lhs.undefined_p ())
    2602                 :             :     return false;
    2603                 :      488988 :   wide_int offset;
    2604                 :             :   // [2, 4] = op1 / [3,3]   since its exact divide, no need to worry about
    2605                 :             :   // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
    2606                 :             :   // We wont bother trying to enumerate all the in between stuff :-P
    2607                 :             :   // TRUE accuracy is [6,6][9,9][12,12].  This is unlikely to matter most of
    2608                 :             :   // the time however.
    2609                 :             :   // If op2 is a multiple of 2, we would be able to set some non-zero bits.
    2610                 :      488988 :   if (op2.singleton_p (offset) && offset != 0)
    2611                 :      488988 :     return range_op_handler (MULT_EXPR).fold_range (r, type, lhs, op2);
    2612                 :             :   return false;
    2613                 :      488988 : }
    2614                 :             : 
    2615                 :             : 
    2616                 :             : class operator_lshift : public cross_product_operator
    2617                 :             : {
    2618                 :             :   using range_operator::fold_range;
    2619                 :             :   using range_operator::op1_range;
    2620                 :             :   using range_operator::update_bitmask;
    2621                 :             : public:
    2622                 :             :   virtual bool op1_range (irange &r, tree type, const irange &lhs,
    2623                 :             :                           const irange &op2, relation_trio rel = TRIO_VARYING)
    2624                 :             :     const final override;
    2625                 :             :   virtual bool fold_range (irange &r, tree type, const irange &op1,
    2626                 :             :                            const irange &op2, relation_trio rel = TRIO_VARYING)
    2627                 :             :     const final override;
    2628                 :             : 
    2629                 :             :   virtual void wi_fold (irange &r, tree type,
    2630                 :             :                         const wide_int &lh_lb, const wide_int &lh_ub,
    2631                 :             :                         const wide_int &rh_lb,
    2632                 :             :                         const wide_int &rh_ub) const final override;
    2633                 :             :   virtual bool wi_op_overflows (wide_int &res,
    2634                 :             :                                 tree type,
    2635                 :             :                                 const wide_int &,
    2636                 :             :                                 const wide_int &) const final override;
    2637                 :      456187 :   void update_bitmask (irange &r, const irange &lh,
    2638                 :             :                        const irange &rh) const final override
    2639                 :      456187 :     { update_known_bitmask (r, LSHIFT_EXPR, lh, rh); }
    2640                 :             :   // Check compatibility of LHS and op1.
    2641                 :     1057746 :   bool operand_check_p (tree t1, tree t2, tree) const final override
    2642                 :     1057746 :     { return range_compatible_p (t1, t2); }
    2643                 :             : } op_lshift;
    2644                 :             : 
    2645                 :             : class operator_rshift : public cross_product_operator
    2646                 :             : {
    2647                 :             :   using range_operator::fold_range;
    2648                 :             :   using range_operator::op1_range;
    2649                 :             :   using range_operator::lhs_op1_relation;
    2650                 :             :   using range_operator::update_bitmask;
    2651                 :             : public:
    2652                 :             :   virtual bool fold_range (irange &r, tree type, const irange &op1,
    2653                 :             :                            const irange &op2, relation_trio rel = TRIO_VARYING)
    2654                 :             :    const final override;
    2655                 :             :   virtual void wi_fold (irange &r, tree type,
    2656                 :             :                         const wide_int &lh_lb,
    2657                 :             :                         const wide_int &lh_ub,
    2658                 :             :                         const wide_int &rh_lb,
    2659                 :             :                         const wide_int &rh_ub) const final override;
    2660                 :             :   virtual bool wi_op_overflows (wide_int &res,
    2661                 :             :                                 tree type,
    2662                 :             :                                 const wide_int &w0,
    2663                 :             :                                 const wide_int &w1) const final override;
    2664                 :             :   virtual bool op1_range (irange &, tree type, const irange &lhs,
    2665                 :             :                           const irange &op2, relation_trio rel = TRIO_VARYING)
    2666                 :             :     const final override;
    2667                 :             :   virtual relation_kind lhs_op1_relation (const irange &lhs, const irange &op1,
    2668                 :             :                                           const irange &op2, relation_kind rel)
    2669                 :             :     const final override;
    2670                 :     3362611 :   void update_bitmask (irange &r, const irange &lh,
    2671                 :             :                        const irange &rh) const final override
    2672                 :     3362611 :     { update_known_bitmask (r, RSHIFT_EXPR, lh, rh); }
    2673                 :             :   // Check compatibility of LHS and op1.
    2674                 :     3461064 :   bool operand_check_p (tree t1, tree t2, tree) const final override
    2675                 :     3461064 :     { return range_compatible_p (t1, t2); }
    2676                 :             : } op_rshift;
    2677                 :             : 
    2678                 :             : 
    2679                 :             : relation_kind
    2680                 :     2445997 : operator_rshift::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
    2681                 :             :                                    const irange &op1,
    2682                 :             :                                    const irange &op2,
    2683                 :             :                                    relation_kind) const
    2684                 :             : {
    2685                 :             :   // If both operands range are >= 0, then the LHS <= op1.
    2686                 :     2445997 :   if (!op1.undefined_p () && !op2.undefined_p ()
    2687                 :     4891146 :       && wi::ge_p (op1.lower_bound (), 0, TYPE_SIGN (op1.type ()))
    2688                 :     7137747 :       && wi::ge_p (op2.lower_bound (), 0, TYPE_SIGN (op2.type ())))
    2689                 :     2219840 :     return VREL_LE;
    2690                 :             :   return VREL_VARYING;
    2691                 :             : }
    2692                 :             : 
    2693                 :             : bool
    2694                 :     1619895 : operator_lshift::fold_range (irange &r, tree type,
    2695                 :             :                              const irange &op1,
    2696                 :             :                              const irange &op2,
    2697                 :             :                              relation_trio rel) const
    2698                 :             : {
    2699                 :     1619895 :   int_range_max shift_range;
    2700                 :     1619895 :   if (!get_shift_range (shift_range, type, op2))
    2701                 :             :     {
    2702                 :         575 :       if (op2.undefined_p ())
    2703                 :         230 :         r.set_undefined ();
    2704                 :             :       else
    2705                 :         345 :         r.set_zero (type);
    2706                 :         575 :       return true;
    2707                 :             :     }
    2708                 :             : 
    2709                 :             :   // Transform left shifts by constants into multiplies.
    2710                 :     1619320 :   if (shift_range.singleton_p ())
    2711                 :             :     {
    2712                 :     1163106 :       unsigned shift = shift_range.lower_bound ().to_uhwi ();
    2713                 :     1163106 :       wide_int tmp = wi::set_bit_in_zero (shift, TYPE_PRECISION (type));
    2714                 :     1163106 :       int_range<1> mult (type, tmp, tmp);
    2715                 :             : 
    2716                 :             :       // Force wrapping multiplication.
    2717                 :     1163106 :       bool saved_flag_wrapv = flag_wrapv;
    2718                 :     1163106 :       bool saved_flag_wrapv_pointer = flag_wrapv_pointer;
    2719                 :     1163106 :       flag_wrapv = 1;
    2720                 :     1163106 :       flag_wrapv_pointer = 1;
    2721                 :     1163106 :       bool b = op_mult.fold_range (r, type, op1, mult);
    2722                 :     1163106 :       flag_wrapv = saved_flag_wrapv;
    2723                 :     1163106 :       flag_wrapv_pointer = saved_flag_wrapv_pointer;
    2724                 :     1163106 :       return b;
    2725                 :     1163115 :     }
    2726                 :             :   else
    2727                 :             :     // Otherwise, invoke the generic fold routine.
    2728                 :      456214 :     return range_operator::fold_range (r, type, op1, shift_range, rel);
    2729                 :     1619895 : }
    2730                 :             : 
    2731                 :             : void
    2732                 :      547352 : operator_lshift::wi_fold (irange &r, tree type,
    2733                 :             :                           const wide_int &lh_lb, const wide_int &lh_ub,
    2734                 :             :                           const wide_int &rh_lb, const wide_int &rh_ub) const
    2735                 :             : {
    2736                 :      547352 :   signop sign = TYPE_SIGN (type);
    2737                 :      547352 :   unsigned prec = TYPE_PRECISION (type);
    2738                 :      547352 :   int overflow_pos = sign == SIGNED ? prec - 1 : prec;
    2739                 :      547352 :   int bound_shift = overflow_pos - rh_ub.to_shwi ();
    2740                 :             :   // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
    2741                 :             :   // overflow.  However, for that to happen, rh.max needs to be zero,
    2742                 :             :   // which means rh is a singleton range of zero, which means we simply return
    2743                 :             :   // [lh_lb, lh_ub] as the range.
    2744                 :      547352 :   if (wi::eq_p (rh_ub, rh_lb) && wi::eq_p (rh_ub, 0))
    2745                 :             :     {
    2746                 :       24451 :       r = int_range<2> (type, lh_lb, lh_ub);
    2747                 :       24451 :       return;
    2748                 :             :     }
    2749                 :             : 
    2750                 :      522901 :   wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
    2751                 :      522901 :   wide_int complement = ~(bound - 1);
    2752                 :      522901 :   wide_int low_bound, high_bound;
    2753                 :      522901 :   bool in_bounds = false;
    2754                 :             : 
    2755                 :      522901 :   if (sign == UNSIGNED)
    2756                 :             :     {
    2757                 :      269427 :       low_bound = bound;
    2758                 :      269427 :       high_bound = complement;
    2759                 :      269427 :       if (wi::ltu_p (lh_ub, low_bound))
    2760                 :             :         {
    2761                 :             :           // [5, 6] << [1, 2] == [10, 24].
    2762                 :             :           // We're shifting out only zeroes, the value increases
    2763                 :             :           // monotonically.
    2764                 :             :           in_bounds = true;
    2765                 :             :         }
    2766                 :       84120 :       else if (wi::ltu_p (high_bound, lh_lb))
    2767                 :             :         {
    2768                 :             :           // [0xffffff00, 0xffffffff] << [1, 2]
    2769                 :             :           // == [0xfffffc00, 0xfffffffe].
    2770                 :             :           // We're shifting out only ones, the value decreases
    2771                 :             :           // monotonically.
    2772                 :             :           in_bounds = true;
    2773                 :             :         }
    2774                 :             :     }
    2775                 :             :   else
    2776                 :             :     {
    2777                 :             :       // [-1, 1] << [1, 2] == [-4, 4]
    2778                 :      253474 :       low_bound = complement;
    2779                 :      253474 :       high_bound = bound;
    2780                 :      253474 :       if (wi::lts_p (lh_ub, high_bound)
    2781                 :      253474 :           && wi::lts_p (low_bound, lh_lb))
    2782                 :             :         {
    2783                 :             :           // For non-negative numbers, we're shifting out only zeroes,
    2784                 :             :           // the value increases monotonically.  For negative numbers,
    2785                 :             :           // we're shifting out only ones, the value decreases
    2786                 :             :           // monotonically.
    2787                 :             :           in_bounds = true;
    2788                 :             :         }
    2789                 :             :     }
    2790                 :             : 
    2791                 :             :   if (in_bounds)
    2792                 :      311421 :     wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
    2793                 :             :   else
    2794                 :      211480 :    r.set_varying (type);
    2795                 :      522910 : }
    2796                 :             : 
    2797                 :             : bool
    2798                 :      597728 : operator_lshift::wi_op_overflows (wide_int &res, tree type,
    2799                 :             :                                   const wide_int &w0, const wide_int &w1) const
    2800                 :             : {
    2801                 :      597728 :   signop sign = TYPE_SIGN (type);
    2802                 :      597728 :   if (wi::neg_p (w1))
    2803                 :             :     {
    2804                 :             :       // It's unclear from the C standard whether shifts can overflow.
    2805                 :             :       // The following code ignores overflow; perhaps a C standard
    2806                 :             :       // interpretation ruling is needed.
    2807                 :           0 :       res = wi::rshift (w0, -w1, sign);
    2808                 :             :     }
    2809                 :             :   else
    2810                 :      597728 :     res = wi::lshift (w0, w1);
    2811                 :      597728 :   return false;
    2812                 :             : }
    2813                 :             : 
    2814                 :             : bool
    2815                 :       44625 : operator_lshift::op1_range (irange &r,
    2816                 :             :                             tree type,
    2817                 :             :                             const irange &lhs,
    2818                 :             :                             const irange &op2,
    2819                 :             :                             relation_trio) const
    2820                 :             : {
    2821                 :       44625 :   if (lhs.undefined_p ())
    2822                 :             :     return false;
    2823                 :             : 
    2824                 :       44625 :   if (!contains_zero_p (lhs))
    2825                 :       21074 :     r.set_nonzero (type);
    2826                 :             :   else
    2827                 :       23551 :     r.set_varying (type);
    2828                 :             : 
    2829                 :       44625 :   wide_int shift;
    2830                 :       44625 :   if (op2.singleton_p (shift))
    2831                 :             :     {
    2832                 :       38992 :       if (wi::lt_p (shift, 0, SIGNED))
    2833                 :             :         return false;
    2834                 :       38992 :       if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type),
    2835                 :       38992 :                                      TYPE_PRECISION (op2.type ())),
    2836                 :             :                     UNSIGNED))
    2837                 :             :         return false;
    2838                 :       38992 :       if (shift == 0)
    2839                 :             :         {
    2840                 :           6 :           r.intersect (lhs);
    2841                 :           6 :           return true;
    2842                 :             :         }
    2843                 :             : 
    2844                 :             :       // Work completely in unsigned mode to start.
    2845                 :       38986 :       tree utype = type;
    2846                 :       38986 :       int_range_max tmp_range;
    2847                 :       38986 :       if (TYPE_SIGN (type) == SIGNED)
    2848                 :             :         {
    2849                 :        7240 :           int_range_max tmp = lhs;
    2850                 :        7240 :           utype = unsigned_type_for (type);
    2851                 :        7240 :           range_cast (tmp, utype);
    2852                 :        7240 :           op_rshift.fold_range (tmp_range, utype, tmp, op2);
    2853                 :        7240 :         }
    2854                 :             :       else
    2855                 :       31746 :         op_rshift.fold_range (tmp_range, utype, lhs, op2);
    2856                 :             : 
    2857                 :             :       // Start with ranges which can produce the LHS by right shifting the
    2858                 :             :       // result by the shift amount.
    2859                 :             :       // ie   [0x08, 0xF0] = op1 << 2 will start with
    2860                 :             :       //      [00001000, 11110000] = op1 << 2
    2861                 :             :       //  [0x02, 0x4C] aka [00000010, 00111100]
    2862                 :             : 
    2863                 :             :       // Then create a range from the LB with the least significant upper bit
    2864                 :             :       // set, to the upper bound with all the bits set.
    2865                 :             :       // This would be [0x42, 0xFC] aka [01000010, 11111100].
    2866                 :             : 
    2867                 :             :       // Ideally we do this for each subrange, but just lump them all for now.
    2868                 :       38986 :       unsigned low_bits = TYPE_PRECISION (utype) - shift.to_uhwi ();
    2869                 :       38986 :       wide_int up_mask = wi::mask (low_bits, true, TYPE_PRECISION (utype));
    2870                 :       38986 :       wide_int new_ub = wi::bit_or (up_mask, tmp_range.upper_bound ());
    2871                 :       38986 :       wide_int new_lb = wi::set_bit (tmp_range.lower_bound (), low_bits);
    2872                 :       38986 :       int_range<2> fill_range (utype, new_lb, new_ub);
    2873                 :       38986 :       tmp_range.union_ (fill_range);
    2874                 :             : 
    2875                 :       38986 :       if (utype != type)
    2876                 :        7240 :         range_cast (tmp_range, type);
    2877                 :             : 
    2878                 :       38986 :       r.intersect (tmp_range);
    2879                 :       38986 :       return true;
    2880                 :       38986 :     }
    2881                 :             : 
    2882                 :        5633 :   return !r.varying_p ();
    2883                 :       44625 : }
    2884                 :             : 
    2885                 :             : bool
    2886                 :      827183 : operator_rshift::op1_range (irange &r,
    2887                 :             :                             tree type,
    2888                 :             :                             const irange &lhs,
    2889                 :             :                             const irange &op2,
    2890                 :             :                             relation_trio) const
    2891                 :             : {
    2892                 :      827183 :   if (lhs.undefined_p ())
    2893                 :             :     return false;
    2894                 :      827183 :   wide_int shift;
    2895                 :      827183 :   if (op2.singleton_p (shift))
    2896                 :             :     {
    2897                 :             :       // Ignore nonsensical shifts.
    2898                 :      802208 :       unsigned prec = TYPE_PRECISION (type);
    2899                 :     1604416 :       if (wi::ge_p (shift,
    2900                 :      802208 :                     wi::uhwi (prec, TYPE_PRECISION (op2.type ())),
    2901                 :             :                     UNSIGNED))
    2902                 :             :         return false;
    2903                 :      802208 :       if (shift == 0)
    2904                 :             :         {
    2905                 :          75 :           r = lhs;
    2906                 :          75 :           return true;
    2907                 :             :         }
    2908                 :             : 
    2909                 :             :       // Folding the original operation may discard some impossible
    2910                 :             :       // ranges from the LHS.
    2911                 :      802133 :       int_range_max lhs_refined;
    2912                 :      802133 :       op_rshift.fold_range (lhs_refined, type, int_range<1> (type), op2);
    2913                 :      802133 :       lhs_refined.intersect (lhs);
    2914                 :      802133 :       if (lhs_refined.undefined_p ())
    2915                 :             :         {
    2916                 :           4 :           r.set_undefined ();
    2917                 :           4 :           return true;
    2918                 :             :         }
    2919                 :      802129 :       int_range_max shift_range (op2.type (), shift, shift);
    2920                 :      802129 :       int_range_max lb, ub;
    2921                 :      802129 :       op_lshift.fold_range (lb, type, lhs_refined, shift_range);
    2922                 :             :       //    LHS
    2923                 :             :       // 0000 0111 = OP1 >> 3
    2924                 :             :       //
    2925                 :             :       // OP1 is anything from 0011 1000 to 0011 1111.  That is, a
    2926                 :             :       // range from LHS<<3 plus a mask of the 3 bits we shifted on the
    2927                 :             :       // right hand side (0x07).
    2928                 :      802129 :       wide_int mask = wi::mask (shift.to_uhwi (), false, prec);
    2929                 :      802129 :       int_range_max mask_range (type,
    2930                 :      802129 :                                 wi::zero (TYPE_PRECISION (type)),
    2931                 :      802129 :                                 mask);
    2932                 :      802129 :       op_plus.fold_range (ub, type, lb, mask_range);
    2933                 :      802129 :       r = lb;
    2934                 :      802129 :       r.union_ (ub);
    2935                 :      802129 :       if (!contains_zero_p (lhs_refined))
    2936                 :             :         {
    2937                 :      459193 :           mask_range.invert ();
    2938                 :      459193 :           r.intersect (mask_range);
    2939                 :             :         }
    2940                 :      802129 :       return true;
    2941                 :      802133 :     }
    2942                 :             :   return false;
    2943                 :      827183 : }
    2944                 :             : 
    2945                 :             : bool
    2946                 :    11150459 : operator_rshift::wi_op_overflows (wide_int &res,
    2947                 :             :                                   tree type,
    2948                 :             :                                   const wide_int &w0,
    2949                 :             :                                   const wide_int &w1) const
    2950                 :             : {
    2951                 :    11150459 :   signop sign = TYPE_SIGN (type);
    2952                 :    11150459 :   if (wi::neg_p (w1))
    2953                 :           0 :     res = wi::lshift (w0, -w1);
    2954                 :             :   else
    2955                 :             :     {
    2956                 :             :       // It's unclear from the C standard whether shifts can overflow.
    2957                 :             :       // The following code ignores overflow; perhaps a C standard
    2958                 :             :       // interpretation ruling is needed.
    2959                 :    11150550 :       res = wi::rshift (w0, w1, sign);
    2960                 :             :     }
    2961                 :    11150459 :   return false;
    2962                 :             : }
    2963                 :             : 
    2964                 :             : bool
    2965                 :     3363894 : operator_rshift::fold_range (irange &r, tree type,
    2966                 :             :                              const irange &op1,
    2967                 :             :                              const irange &op2,
    2968                 :             :                              relation_trio rel) const
    2969                 :             : {
    2970                 :     3363894 :   int_range_max shift;
    2971                 :     3363894 :   if (!get_shift_range (shift, type, op2))
    2972                 :             :     {
    2973                 :         659 :       if (op2.undefined_p ())
    2974                 :         255 :         r.set_undefined ();
    2975                 :             :       else
    2976                 :         404 :         r.set_zero (type);
    2977                 :         659 :       return true;
    2978                 :             :     }
    2979                 :             : 
    2980                 :     3363235 :   return range_operator::fold_range (r, type, op1, shift, rel);
    2981                 :     3363894 : }
    2982                 :             : 
    2983                 :             : void
    2984                 :     3938995 : operator_rshift::wi_fold (irange &r, tree type,
    2985                 :             :                           const wide_int &lh_lb, const wide_int &lh_ub,
    2986                 :             :                           const wide_int &rh_lb, const wide_int &rh_ub) const
    2987                 :             : {
    2988                 :     3938995 :   wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
    2989                 :     3938995 : }
    2990                 :             : 
    2991                 :             : 
    2992                 :             : // Add a partial equivalence between the LHS and op1 for casts.
    2993                 :             : 
    2994                 :             : relation_kind
    2995                 :    24023731 : operator_cast::lhs_op1_relation (const irange &lhs,
    2996                 :             :                                  const irange &op1,
    2997                 :             :                                  const irange &op2 ATTRIBUTE_UNUSED,
    2998                 :             :                                  relation_kind) const
    2999                 :             : {
    3000                 :    24023731 :   if (lhs.undefined_p () || op1.undefined_p ())
    3001                 :             :     return VREL_VARYING;
    3002                 :    24002192 :   unsigned lhs_prec = TYPE_PRECISION (lhs.type ());
    3003                 :    24002192 :   unsigned op1_prec = TYPE_PRECISION (op1.type ());
    3004                 :             :   // If the result gets sign extended into a larger type check first if this
    3005                 :             :   // qualifies as a partial equivalence.
    3006                 :    24002192 :   if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec)
    3007                 :             :     {
    3008                 :             :       // If the result is sign extended, and the LHS is larger than op1,
    3009                 :             :       // check if op1's range can be negative as the sign extension will
    3010                 :             :       // cause the upper bits to be 1 instead of 0, invalidating the PE.
    3011                 :     3700887 :       int_range<3> negs = range_negatives (op1.type ());
    3012                 :     3700887 :       negs.intersect (op1);
    3013                 :     3700887 :       if (!negs.undefined_p ())
    3014                 :     2589116 :         return VREL_VARYING;
    3015                 :     3700887 :     }
    3016                 :             : 
    3017                 :    21413076 :   unsigned prec = MIN (lhs_prec, op1_prec);
    3018                 :    21413076 :   return bits_to_pe (prec);
    3019                 :             : }
    3020                 :             : 
    3021                 :             : // Return TRUE if casting from INNER to OUTER is a truncating cast.
    3022                 :             : 
    3023                 :             : inline bool
    3024                 :    82039889 : operator_cast::truncating_cast_p (const irange &inner,
    3025                 :             :                                   const irange &outer) const
    3026                 :             : {
    3027                 :    82039889 :   return TYPE_PRECISION (outer.type ()) < TYPE_PRECISION (inner.type ());
    3028                 :             : }
    3029                 :             : 
    3030                 :             : // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
    3031                 :             : 
    3032                 :             : bool
    3033                 :    71032354 : operator_cast::inside_domain_p (const wide_int &min,
    3034                 :             :                                 const wide_int &max,
    3035                 :             :                                 const irange &range) const
    3036                 :             : {
    3037                 :    71032354 :   wide_int domain_min = irange_val_min (range.type ());
    3038                 :    71032354 :   wide_int domain_max = irange_val_max (range.type ());
    3039                 :    71032354 :   signop domain_sign = TYPE_SIGN (range.type ());
    3040                 :    71032354 :   return (wi::le_p (min, domain_max, domain_sign)
    3041                 :    71032354 :           && wi::le_p (max, domain_max, domain_sign)
    3042                 :    71032354 :           && wi::ge_p (min, domain_min, domain_sign)
    3043                 :   142064708 :           && wi::ge_p (max, domain_min, domain_sign));
    3044                 :    71032354 : }
    3045                 :             : 
    3046                 :             : 
    3047                 :             : // Helper for fold_range which work on a pair at a time.
    3048                 :             : 
    3049                 :             : void
    3050                 :    74165589 : operator_cast::fold_pair (irange &r, unsigned index,
    3051                 :             :                            const irange &inner,
    3052                 :             :                            const irange &outer) const
    3053                 :             : {
    3054                 :    74165589 :   tree inner_type = inner.type ();
    3055                 :    74165589 :   tree outer_type = outer.type ();
    3056                 :    74165589 :   signop inner_sign = TYPE_SIGN (inner_type);
    3057                 :    74165589 :   unsigned outer_prec = TYPE_PRECISION (outer_type);
    3058                 :             : 
    3059                 :             :   // check to see if casting from INNER to OUTER is a conversion that
    3060                 :             :   // fits in the resulting OUTER type.
    3061                 :    74165589 :   wide_int inner_lb = inner.lower_bound (index);
    3062                 :    74165589 :   wide_int inner_ub = inner.upper_bound (index);
    3063                 :    74165589 :   if (truncating_cast_p (inner, outer))
    3064                 :             :     {
    3065                 :             :       // We may be able to accommodate a truncating cast if the
    3066                 :             :       // resulting range can be represented in the target type...
    3067                 :    14485232 :       if (wi::rshift (wi::sub (inner_ub, inner_lb),
    3068                 :     7242616 :                       wi::uhwi (outer_prec, TYPE_PRECISION (inner.type ())),
    3069                 :    21727848 :                                 inner_sign) != 0)
    3070                 :             :         {
    3071                 :     3133235 :           r.set_varying (outer_type);
    3072                 :     3133235 :           return;
    3073                 :             :         }
    3074                 :             :     }
    3075                 :             :   // ...but we must still verify that the final range fits in the
    3076                 :             :   // domain.  This catches -fstrict-enum restrictions where the domain
    3077                 :             :   // range is smaller than what fits in the underlying type.
    3078                 :    71032354 :   wide_int min = wide_int::from (inner_lb, outer_prec, inner_sign);
    3079                 :    71032354 :   wide_int max = wide_int::from (inner_ub, outer_prec, inner_sign);
    3080                 :    71032354 :   if (inside_domain_p (min, max, outer))
    3081                 :    71032354 :     create_possibly_reversed_range (r, outer_type, min, max);
    3082                 :             :   else
    3083                 :           0 :     r.set_varying (outer_type);
    3084                 :    74168011 : }
    3085                 :             : 
    3086                 :             : 
    3087                 :             : bool
    3088                 :    59662918 : operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
    3089                 :             :                            const irange &inner,
    3090                 :             :                            const irange &outer,
    3091                 :             :                            relation_trio) const
    3092                 :             : {
    3093                 :    59662918 :   if (empty_range_varying (r, type, inner, outer))
    3094                 :       31593 :     return true;
    3095                 :             : 
    3096                 :    59631325 :   gcc_checking_assert (outer.varying_p ());
    3097                 :    59631325 :   gcc_checking_assert (inner.num_pairs () > 0);
    3098                 :             : 
    3099                 :             :   // Avoid a temporary by folding the first pair directly into the result.
    3100                 :    59631325 :   fold_pair (r, 0, inner, outer);
    3101                 :             : 
    3102                 :             :   // Then process any additional pairs by unioning with their results.
    3103                 :    73464059 :   for (unsigned x = 1; x < inner.num_pairs (); ++x)
    3104                 :             :     {
    3105                 :    14534264 :       int_range_max tmp;
    3106                 :    14534264 :       fold_pair (tmp, x, inner, outer);
    3107                 :    14534264 :       r.union_ (tmp);
    3108                 :             :       // If we hit varying, go update the bitmask.
    3109                 :    14534264 :       if (r.varying_p ())
    3110                 :             :         break;
    3111                 :    14534264 :     }
    3112                 :             : 
    3113                 :    59631325 :   update_bitmask (r, inner, outer);
    3114                 :    59631325 :   return true;
    3115                 :             : }
    3116                 :             : 
    3117                 :             : void
    3118                 :    59631325 : operator_cast::update_bitmask (irange &r, const irange &lh,
    3119                 :             :                                const irange &rh) const
    3120                 :             : {
    3121                 :    59631325 :   update_known_bitmask (r, CONVERT_EXPR, lh, rh);
    3122                 :    59631325 : }
    3123                 :             : 
    3124                 :             : bool
    3125                 :     7874300 : operator_cast::op1_range (irange &r, tree type,
    3126                 :             :                           const irange &lhs,
    3127                 :             :                           const irange &op2,
    3128                 :             :                           relation_trio) const
    3129                 :             : {
    3130                 :     7874300 :   if (lhs.undefined_p ())
    3131                 :             :     return false;
    3132                 :     7874300 :   tree lhs_type = lhs.type ();
    3133                 :     7874300 :   gcc_checking_assert (types_compatible_p (op2.type(), type));
    3134                 :             : 
    3135                 :             :   // If we are calculating a pointer, shortcut to what we really care about.
    3136                 :     7874300 :   if (POINTER_TYPE_P (type))
    3137                 :             :     {
    3138                 :             :       // Conversion from other pointers or a constant (including 0/NULL)
    3139                 :             :       // are straightforward.
    3140                 :           0 :       if (POINTER_TYPE_P (lhs.type ())
    3141                 :           0 :           || (lhs.singleton_p ()
    3142                 :           0 :               && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
    3143                 :             :         {
    3144                 :           0 :           r = lhs;
    3145                 :           0 :           range_cast (r, type);
    3146                 :             :         }
    3147                 :             :       else
    3148                 :             :         {
    3149                 :             :           // If the LHS is not a pointer nor a singleton, then it is
    3150                 :             :           // either VARYING or non-zero.
    3151                 :           0 :           if (!lhs.undefined_p () && !contains_zero_p (lhs))
    3152                 :           0 :             r.set_nonzero (type);
    3153                 :             :           else
    3154                 :           0 :             r.set_varying (type);
    3155                 :             :         }
    3156                 :           0 :       r.intersect (op2);
    3157                 :           0 :       return true;
    3158                 :             :     }
    3159                 :             : 
    3160                 :     7874300 :   if (truncating_cast_p (op2, lhs))
    3161                 :             :     {
    3162                 :     1357349 :       if (lhs.varying_p ())
    3163                 :      164616 :         r.set_varying (type);
    3164                 :             :       else
    3165                 :             :         {
    3166                 :             :           // We want to insert the LHS as an unsigned value since it
    3167                 :             :           // would not trigger the signed bit of the larger type.
    3168                 :     1192733 :           int_range_max converted_lhs = lhs;
    3169                 :     1192733 :           range_cast (converted_lhs, unsigned_type_for (lhs_type));
    3170                 :     1192733 :           range_cast (converted_lhs, type);
    3171                 :             :           // Start by building the positive signed outer range for the type.
    3172                 :     1192733 :           wide_int lim = wi::set_bit_in_zero (TYPE_PRECISION (lhs_type),
    3173                 :     2385466 :                                               TYPE_PRECISION (type));
    3174                 :     1192733 :           create_possibly_reversed_range (r, type, lim,
    3175                 :     1192733 :                                           wi::max_value (TYPE_PRECISION (type),
    3176                 :             :                                                          SIGNED));
    3177                 :             :           // For the signed part, we need to simply union the 2 ranges now.
    3178                 :     1192733 :           r.union_ (converted_lhs);
    3179                 :             : 
    3180                 :             :           // Create maximal negative number outside of LHS bits.
    3181                 :     1192733 :           lim = wi::mask (TYPE_PRECISION (lhs_type), true,
    3182                 :     2385466 :                           TYPE_PRECISION (type));
    3183                 :             :           // Add this to the unsigned LHS range(s).
    3184                 :     1192733 :           int_range_max lim_range (type, lim, lim);
    3185                 :     1192733 :           int_range_max lhs_neg;
    3186                 :     1192733 :           range_op_handler (PLUS_EXPR).fold_range (lhs_neg, type,
    3187                 :             :                                                    converted_lhs, lim_range);
    3188                 :             :           // lhs_neg now has all the negative versions of the LHS.
    3189                 :             :           // Now union in all the values from SIGNED MIN (0x80000) to
    3190                 :             :           // lim-1 in order to fill in all the ranges with the upper
    3191                 :             :           // bits set.
    3192                 :             : 
    3193                 :             :           // PR 97317.  If the lhs has only 1 bit less precision than the rhs,
    3194                 :             :           // we don't need to create a range from min to lim-1
    3195                 :             :           // calculate neg range traps trying to create [lim, lim - 1].
    3196                 :     1192733 :           wide_int min_val = wi::min_value (TYPE_PRECISION (type), SIGNED);
    3197                 :     1192733 :           if (lim != min_val)
    3198                 :             :             {
    3199                 :     1191026 :               int_range_max neg (type,
    3200                 :     2382052 :                                  wi::min_value (TYPE_PRECISION (type),
    3201                 :             :                                                 SIGNED),
    3202                 :     2382052 :                                  lim - 1);
    3203                 :     1191026 :               lhs_neg.union_ (neg);
    3204                 :     1191026 :             }
    3205                 :             :           // And finally, munge the signed and unsigned portions.
    3206                 :     1192733 :           r.union_ (lhs_neg);
    3207                 :     1192733 :         }
    3208                 :             :       // And intersect with any known value passed in the extra operand.
    3209                 :     1357349 :       r.intersect (op2);
    3210                 :     1357349 :       if (r.undefined_p ())
    3211                 :             :         return true;
    3212                 :             : 
    3213                 :             :       // Now create a bitmask indicating that the lower bit must match the
    3214                 :             :       // bits in the LHS.   Zero-extend LHS bitmask to precision of op1.
    3215                 :     1357292 :       irange_bitmask bm = lhs.get_bitmask ();
    3216                 :     2714584 :       wide_int mask = wide_int::from (bm.mask (), TYPE_PRECISION (type),
    3217                 :     2714584 :                                       UNSIGNED);
    3218                 :     2714584 :       wide_int value = wide_int::from (bm.value (), TYPE_PRECISION (type),
    3219                 :     2714584 :                                        UNSIGNED);
    3220                 :             : 
    3221                 :             :       // Set then additonal unknown bits in mask.
    3222                 :     1357292 :       wide_int lim = wi::mask (TYPE_PRECISION (lhs_type), true,
    3223                 :     2714584 :                                TYPE_PRECISION (type));
    3224                 :     1357292 :       mask = mask | lim;
    3225                 :             : 
    3226                 :             :       // Now set the new bitmask for the range.
    3227                 :     1357292 :       irange_bitmask new_bm (value, mask);
    3228                 :     1357292 :       r.update_bitmask (new_bm);
    3229                 :     1357292 :       return true;
    3230                 :     1357292 :     }
    3231                 :             : 
    3232                 :     6516951 :   int_range_max tmp;
    3233                 :     6516951 :   if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
    3234                 :     4855321 :     tmp = lhs;
    3235                 :             :   else
    3236                 :             :     {
    3237                 :             :       // The cast is not truncating, and the range is restricted to
    3238                 :             :       // the range of the RHS by this assignment.
    3239                 :             :       //
    3240                 :             :       // Cast the range of the RHS to the type of the LHS.
    3241                 :     1661630 :       fold_range (tmp, lhs_type, int_range<1> (type), int_range<1> (lhs_type));
    3242                 :             :       // Intersect this with the LHS range will produce the range,
    3243                 :             :       // which will be cast to the RHS type before returning.
    3244                 :     1661630 :       tmp.intersect (lhs);
    3245                 :             :     }
    3246                 :             : 
    3247                 :             :   // Cast the calculated range to the type of the RHS.
    3248                 :     6516951 :   fold_range (r, type, tmp, int_range<1> (type));
    3249                 :     6516951 :   return true;
    3250                 :     6516951 : }
    3251                 :             : 
    3252                 :             : // VIEW_CONVERT_EXPR works like a cast between integral values.
    3253                 :             : // If the number of bits are not the same, behaviour is undefined,
    3254                 :             : // so cast behaviour still works.
    3255                 :             : 
    3256                 :             : bool
    3257                 :      277578 : operator_view::fold_range (irange &r, tree type,
    3258                 :             :                            const irange &op1, const irange &op2,
    3259                 :             :                            relation_trio rel) const
    3260                 :             : {
    3261                 :      277578 :   return m_cast.fold_range (r, type, op1, op2, rel);
    3262                 :             : }
    3263                 :             : 
    3264                 :             : bool
    3265                 :           0 : operator_view::fold_range (prange &r, tree type,
    3266                 :             :                            const prange &op1, const prange &op2,
    3267                 :             :                            relation_trio rel) const
    3268                 :             : {
    3269                 :           0 :   return m_cast.fold_range (r, type, op1, op2, rel);
    3270                 :             : }
    3271                 :             : bool
    3272                 :      263567 : operator_view::fold_range (irange &r, tree type,
    3273                 :             :                            const prange &op1, const irange &op2,
    3274                 :             :                            relation_trio rel) const
    3275                 :             : {
    3276                 :      263567 :   return m_cast.fold_range (r, type, op1, op2, rel);
    3277                 :             : }
    3278                 :             : 
    3279                 :             : bool
    3280                 :           0 : operator_view::fold_range (prange &r, tree type,
    3281                 :             :                            const irange &op1, const prange &op2,
    3282                 :             :                            relation_trio rel) const
    3283                 :             : {
    3284                 :           0 :   return m_cast.fold_range (r, type, op1, op2, rel);
    3285                 :             : }
    3286                 :             : 
    3287                 :             : bool
    3288                 :       13635 : operator_view::op1_range (irange &r, tree type,
    3289                 :             :                           const irange &lhs, const irange &op2,
    3290                 :             :                           relation_trio rel) const
    3291                 :             : {
    3292                 :       13635 :   return m_cast.op1_range (r, type, lhs, op2, rel);
    3293                 :             : }
    3294                 :             : 
    3295                 :             : bool
    3296                 :           0 : operator_view::op1_range (prange &r, tree type,
    3297                 :             :                           const prange &lhs, const prange &op2,
    3298                 :             :                           relation_trio rel) const
    3299                 :             : {
    3300                 :           0 :   return m_cast.op1_range (r, type, lhs, op2, rel);
    3301                 :             : }
    3302                 :             : 
    3303                 :             : bool
    3304                 :           0 : operator_view::op1_range (irange &r, tree type,
    3305                 :             :                           const prange &lhs, const irange &op2,
    3306                 :             :                           relation_trio rel) const
    3307                 :             : {
    3308                 :           0 :   return m_cast.op1_range (r, type, lhs, op2, rel);
    3309                 :             : }
    3310                 :             : 
    3311                 :             : bool
    3312                 :           0 : operator_view::op1_range (prange &r, tree type,
    3313                 :             :                           const irange &lhs, const prange &op2,
    3314                 :             :                           relation_trio rel) const
    3315                 :             : {
    3316                 :           0 :   return m_cast.op1_range (r, type, lhs, op2, rel);
    3317                 :             : }
    3318                 :             : 
    3319                 :             : void
    3320                 :           0 : operator_view::update_bitmask (irange &r, const irange &lh,
    3321                 :             :                                const irange &rh) const
    3322                 :             : {
    3323                 :           0 :   m_cast.update_bitmask (r, lh, rh);
    3324                 :           0 : }
    3325                 :             : 
    3326                 :             : 
    3327                 :             : class operator_logical_and : public range_operator
    3328                 :             : {
    3329                 :             :   using range_operator::fold_range;
    3330                 :             :   using range_operator::op1_range;
    3331                 :             :   using range_operator::op2_range;
    3332                 :             : public:
    3333                 :             :   bool fold_range (irange &r, tree type,
    3334                 :             :                    const irange &lh,
    3335                 :             :                    const irange &rh,
    3336                 :             :                    relation_trio rel = TRIO_VARYING) const final override;
    3337                 :             :   bool op1_range (irange &r, tree type,
    3338                 :             :                   const irange &lhs,
    3339                 :             :                   const irange &op2,
    3340                 :             :                   relation_trio rel = TRIO_VARYING) const final override;
    3341                 :             :   bool op2_range (irange &r, tree type,
    3342                 :             :                   const irange &lhs,
    3343                 :             :                   const irange &op1,
    3344                 :             :                   relation_trio rel = TRIO_VARYING) const final override;
    3345                 :             :   // Check compatibility of all operands.
    3346                 :           0 :   bool operand_check_p (tree t1, tree t2, tree t3) const final override
    3347                 :           0 :     { return range_compatible_p (t1, t2) && range_compatible_p (t1, t3); }
    3348                 :             : } op_logical_and;
    3349                 :             : 
    3350                 :             : bool
    3351                 :           0 : operator_logical_and::fold_range (irange &r, tree type,
    3352                 :             :                                   const irange &lh,
    3353                 :             :                                   const irange &rh,
    3354                 :             :                                   relation_trio) const
    3355                 :             : {
    3356                 :           0 :   if (empty_range_varying (r, type, lh, rh))
    3357                 :           0 :     return true;
    3358                 :             : 
    3359                 :             :   // Precision of LHS and both operands must match.
    3360                 :           0 :   if (TYPE_PRECISION (lh.type ()) != TYPE_PRECISION (type)
    3361                 :           0 :       || TYPE_PRECISION (type) != TYPE_PRECISION (rh.type ()))
    3362                 :           0 :     return false;
    3363                 :             : 
    3364                 :             :   // 0 && anything is 0.
    3365                 :           0 :   if ((wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (lh.upper_bound (), 0))
    3366                 :           0 :       || (wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (rh.upper_bound (), 0)))
    3367                 :           0 :     r = range_false (type);
    3368                 :           0 :   else if (contains_zero_p (lh) || contains_zero_p (rh))
    3369                 :             :     // To reach this point, there must be a logical 1 on each side, and
    3370                 :             :     // the only remaining question is whether there is a zero or not.
    3371                 :           0 :     r = range_true_and_false (type);
    3372                 :             :   else
    3373                 :           0 :     r = range_true (type);
    3374                 :             :   return true;
    3375                 :             : }
    3376                 :             : 
    3377                 :             : bool
    3378                 :      908608 : operator_logical_and::op1_range (irange &r, tree type,
    3379                 :             :                                  const irange &lhs,
    3380                 :             :                                  const irange &op2 ATTRIBUTE_UNUSED,
    3381                 :             :                                  relation_trio) const
    3382                 :             : {
    3383                 :      908608 :    switch (get_bool_state (r, lhs, type))
    3384                 :             :      {
    3385                 :      482176 :      case BRS_TRUE:
    3386                 :             :        // A true result means both sides of the AND must be true.
    3387                 :      482176 :        r = range_true (type);
    3388                 :      482176 :        break;
    3389                 :      426432 :      default:
    3390                 :             :        // Any other result means only one side has to be false, the
    3391                 :             :        // other side can be anything.  So we cannot be sure of any
    3392                 :             :        // result here.
    3393                 :      426432 :        r = range_true_and_false (type);
    3394                 :      426432 :        break;
    3395                 :             :      }
    3396                 :      908608 :   return true;
    3397                 :             : }
    3398                 :             : 
    3399                 :             : bool
    3400                 :           0 : operator_logical_and::op2_range (irange &r, tree type,
    3401                 :             :                                  const irange &lhs,
    3402                 :             :                                  const irange &op1,
    3403                 :             :                                  relation_trio) const
    3404                 :             : {
    3405                 :           0 :   return operator_logical_and::op1_range (r, type, lhs, op1);
    3406                 :             : }
    3407                 :             : 
    3408                 :             : 
    3409                 :             : void
    3410                 :     7851794 : operator_bitwise_and::update_bitmask (irange &r, const irange &lh,
    3411                 :             :                                       const irange &rh) const
    3412                 :             : {
    3413                 :     7851794 :   update_known_bitmask (r, BIT_AND_EXPR, lh, rh);
    3414                 :     7851794 : }
    3415                 :             : 
    3416                 :             : // Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
    3417                 :             : // by considering the number of leading redundant sign bit copies.
    3418                 :             : // clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
    3419                 :             : // [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
    3420                 :             : static bool
    3421                 :      184332 : wi_optimize_signed_bitwise_op (irange &r, tree type,
    3422                 :             :                                const wide_int &lh_lb, const wide_int &lh_ub,
    3423                 :             :                                const wide_int &rh_lb, const wide_int &rh_ub)
    3424                 :             : {
    3425                 :      368664 :   int lh_clrsb = MIN (wi::clrsb (lh_lb), wi::clrsb (lh_ub));
    3426                 :      368664 :   int rh_clrsb = MIN (wi::clrsb (rh_lb), wi::clrsb (rh_ub));
    3427                 :      184332 :   int new_clrsb = MIN (lh_clrsb, rh_clrsb);
    3428                 :      184332 :   if (new_clrsb == 0)
    3429                 :             :     return false;
    3430                 :       13926 :   int type_prec = TYPE_PRECISION (type);
    3431                 :       13926 :   int rprec = (type_prec - new_clrsb) - 1;
    3432                 :       13926 :   value_range_with_overflow (r, type,
    3433                 :       27852 :                              wi::mask (rprec, true, type_prec),
    3434                 :       13926 :                              wi::mask (rprec, false, type_prec));
    3435                 :       13926 :   return true;
    3436                 :             : }
    3437                 :             : 
    3438                 :             : // An AND of 8,16, 32 or 64 bits can produce a partial equivalence between
    3439                 :             : // the LHS and op1.
    3440                 :             : 
    3441                 :             : relation_kind
    3442                 :     7118515 : operator_bitwise_and::lhs_op1_relation (const irange &lhs,
    3443                 :             :                                         const irange &op1,
    3444                 :             :                                         const irange &op2,
    3445                 :             :                                         relation_kind) const
    3446                 :             : {
    3447                 :     7118515 :   if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
    3448                 :             :     return VREL_VARYING;
    3449                 :     7114315 :   if (!op2.singleton_p ())
    3450                 :             :     return VREL_VARYING;
    3451                 :             :   // if val == 0xff or 0xFFFF OR 0Xffffffff OR 0Xffffffffffffffff, return TRUE
    3452                 :     4072345 :   int prec1 = TYPE_PRECISION (op1.type ());
    3453                 :     4072345 :   int prec2 = TYPE_PRECISION (op2.type ());
    3454                 :     4072345 :   int mask_prec = 0;
    3455                 :     4072345 :   wide_int mask = op2.lower_bound ();
    3456                 :     4072345 :   if (wi::eq_p (mask, wi::mask (8, false, prec2)))
    3457                 :             :     mask_prec = 8;
    3458                 :     3989863 :   else if (wi::eq_p (mask, wi::mask (16, false, prec2)))
    3459                 :             :     mask_prec = 16;
    3460                 :     3972640 :   else if (wi::eq_p (mask, wi::mask (32, false, prec2)))
    3461                 :             :     mask_prec = 32;
    3462                 :     3788895 :   else if (wi::eq_p (mask, wi::mask (64, false, prec2)))
    3463                 :         826 :     mask_prec = 64;
    3464                 :     4072345 :   return bits_to_pe (MIN (prec1, mask_prec));
    3465                 :     4072345 : }
    3466                 :             : 
    3467                 :             : // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
    3468                 :             : // possible.  Basically, see if we can optimize:
    3469                 :             : //
    3470                 :             : //      [LB, UB] op Z
    3471                 :             : //   into:
    3472                 :             : //      [LB op Z, UB op Z]
    3473                 :             : //
    3474                 :             : // If the optimization was successful, accumulate the range in R and
    3475                 :             : // return TRUE.
    3476                 :             : 
    3477                 :             : static bool
    3478                 :    23818826 : wi_optimize_and_or (irange &r,
    3479                 :             :                     enum tree_code code,
    3480                 :             :                     tree type,
    3481                 :             :                     const wide_int &lh_lb, const wide_int &lh_ub,
    3482                 :             :                     const wide_int &rh_lb, const wide_int &rh_ub)
    3483                 :             : {
    3484                 :             :   // Calculate the singleton mask among the ranges, if any.
    3485                 :    23818826 :   wide_int lower_bound, upper_bound, mask;
    3486                 :    23818826 :   if (wi::eq_p (rh_lb, rh_ub))
    3487                 :             :     {
    3488                 :    22149216 :       mask = rh_lb;
    3489                 :    22149216 :       lower_bound = lh_lb;
    3490                 :    22149216 :       upper_bound = lh_ub;
    3491                 :             :     }
    3492                 :     1669610 :   else if (wi::eq_p (lh_lb, lh_ub))
    3493                 :             :     {
    3494                 :      367378 :       mask = lh_lb;
    3495                 :      367378 :       lower_bound = rh_lb;
    3496                 :      367378 :       upper_bound = rh_ub;
    3497                 :             :     }
    3498                 :             :   else
    3499                 :             :     return false;
    3500                 :             : 
    3501                 :             :   // If Z is a constant which (for op | its bitwise not) has n
    3502                 :             :   // consecutive least significant bits cleared followed by m 1
    3503                 :             :   // consecutive bits set immediately above it and either
    3504                 :             :   // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
    3505                 :             :   //
    3506                 :             :   // The least significant n bits of all the values in the range are
    3507                 :             :   // cleared or set, the m bits above it are preserved and any bits
    3508                 :             :   // above these are required to be the same for all values in the
    3509                 :             :   // range.
    3510                 :    22516594 :   wide_int w = mask;
    3511                 :    22516594 :   int m = 0, n = 0;
    3512                 :    22516594 :   if (code == BIT_IOR_EXPR)
    3513                 :     6544410 :     w = ~w;
    3514                 :    22516594 :   if (wi::eq_p (w, 0))
    3515                 :     7629793 :     n = w.get_precision ();
    3516                 :             :   else
    3517                 :             :     {
    3518                 :    14886801 :       n = wi::ctz (w);
    3519                 :    14886807 :       w = ~(w | wi::mask (n, false, w.get_precision ()));
    3520                 :    14886801 :       if (wi::eq_p (w, 0))
    3521                 :     8811087 :         m = w.get_precision () - n;
    3522                 :             :       else
    3523                 :     6075714 :         m = wi::ctz (w) - n;
    3524                 :             :     }
    3525                 :    22516594 :   wide_int new_mask = wi::mask (m + n, true, w.get_precision ());
    3526                 :    22516600 :   if ((new_mask & lower_bound) != (new_mask & upper_bound))
    3527                 :             :     return false;
    3528                 :             : 
    3529                 :    17295285 :   wide_int res_lb, res_ub;
    3530                 :    17295285 :   if (code == BIT_AND_EXPR)
    3531                 :             :     {
    3532                 :    11009055 :       res_lb = wi::bit_and (lower_bound, mask);
    3533                 :    11009055 :       res_ub = wi::bit_and (upper_bound, mask);
    3534                 :             :     }
    3535                 :     6286230 :   else if (code == BIT_IOR_EXPR)
    3536                 :             :     {
    3537                 :     6286230 :       res_lb = wi::bit_or (lower_bound, mask);
    3538                 :     6286230 :       res_ub = wi::bit_or (upper_bound, mask);
    3539                 :             :     }
    3540                 :             :   else
    3541                 :           0 :     gcc_unreachable ();
    3542                 :    17295285 :   value_range_with_overflow (r, type, res_lb, res_ub);
    3543                 :             : 
    3544                 :             :   // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
    3545                 :    17295285 :   if (code == BIT_IOR_EXPR && wi::ne_p (mask, 0))
    3546                 :             :     {
    3547                 :     3193097 :       int_range<2> tmp;
    3548                 :     3193097 :       tmp.set_nonzero (type);
    3549                 :     3193097 :       r.intersect (tmp);
    3550                 :     3193097 :     }
    3551                 :    17295285 :   return true;
    3552                 :    63630711 : }
    3553                 :             : 
    3554                 :             : // For range [LB, UB] compute two wide_int bit masks.
    3555                 :             : //
    3556                 :             : // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
    3557                 :             : // for all numbers in the range the bit is 0, otherwise it might be 0
    3558                 :             : // or 1.
    3559                 :             : //
    3560                 :             : // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
    3561                 :             : // for all numbers in the range the bit is 1, otherwise it might be 0
    3562                 :             : // or 1.
    3563                 :             : 
    3564                 :             : void
    3565                 :    14120473 : wi_set_zero_nonzero_bits (tree type,
    3566                 :             :                           const wide_int &lb, const wide_int &ub,
    3567                 :             :                           wide_int &maybe_nonzero,
    3568                 :             :                           wide_int &mustbe_nonzero)
    3569                 :             : {
    3570                 :    14120473 :   signop sign = TYPE_SIGN (type);
    3571                 :             : 
    3572                 :    14120473 :   if (wi::eq_p (lb, ub))
    3573                 :     5720804 :     maybe_nonzero = mustbe_nonzero = lb;
    3574                 :     8399669 :   else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
    3575                 :             :     {
    3576                 :     7956569 :       wide_int xor_mask = lb ^ ub;
    3577                 :     7956569 :       maybe_nonzero = lb | ub;
    3578                 :     7956569 :       mustbe_nonzero = lb & ub;
    3579                 :     7956569 :       if (xor_mask != 0)
    3580                 :             :         {
    3581                 :     7956569 :           wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
    3582                 :    15913138 :                                     maybe_nonzero.get_precision ());
    3583                 :     7956569 :           maybe_nonzero = maybe_nonzero | mask;
    3584                 :     7956574 :           mustbe_nonzero = wi::bit_and_not (mustbe_nonzero, mask);
    3585                 :     7956569 :         }
    3586                 :     7956569 :     }
    3587                 :             :   else
    3588                 :             :     {
    3589                 :      443100 :       maybe_nonzero = wi::minus_one (lb.get_precision ());
    3590                 :      443100 :       mustbe_nonzero = wi::zero (lb.get_precision ());
    3591                 :             :     }
    3592                 :    14120473 : }
    3593                 :             : 
    3594                 :             : void
    3595                 :    17273460 : operator_bitwise_and::wi_fold (irange &r, tree type,
    3596                 :             :                                const wide_int &lh_lb,
    3597                 :             :                                const wide_int &lh_ub,
    3598                 :             :                                const wide_int &rh_lb,
    3599                 :             :                                const wide_int &rh_ub) const
    3600                 :             : {
    3601                 :             :   // The AND algorithm does not handle complex signed operations well.
    3602                 :             :   // If a signed range crosses the boundry between signed and unsigned
    3603                 :             :   // proces sit as 2 ranges and union the results.
    3604                 :    17273460 :   if (TYPE_SIGN (type) == SIGNED
    3605                 :    17273460 :       && wi::neg_p (lh_lb, SIGNED) != wi::neg_p (lh_ub, SIGNED))
    3606                 :             :     {
    3607                 :      628992 :       int prec = TYPE_PRECISION (type);
    3608                 :      628992 :       int_range_max tmp;
    3609                 :             :       // Process [lh_lb, -1]
    3610                 :      628992 :       wi_fold (tmp, type, lh_lb, wi::minus_one (prec), rh_lb, rh_ub);
    3611                 :             :       // Now Process [0, rh_ub]
    3612                 :      628992 :       wi_fold (r, type, wi::zero (prec), lh_ub, rh_lb, rh_ub);
    3613                 :      628992 :       r.union_ (tmp);
    3614                 :      628992 :       return;
    3615                 :      628992 :     }
    3616                 :             : 
    3617                 :    16644468 :   if (wi_optimize_and_or (r, BIT_AND_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
    3618                 :             :     return;
    3619                 :             : 
    3620                 :     5635413 :   wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
    3621                 :     5635413 :   wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
    3622                 :     5635413 :   wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
    3623                 :             :                             maybe_nonzero_lh, mustbe_nonzero_lh);
    3624                 :     5635413 :   wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
    3625                 :             :                             maybe_nonzero_rh, mustbe_nonzero_rh);
    3626                 :             : 
    3627                 :     5635413 :   wide_int new_lb = mustbe_nonzero_lh & mustbe_nonzero_rh;
    3628                 :     5635413 :   wide_int new_ub = maybe_nonzero_lh & maybe_nonzero_rh;
    3629                 :     5635413 :   signop sign = TYPE_SIGN (type);
    3630                 :     5635413 :   unsigned prec = TYPE_PRECISION (type);
    3631                 :             :   // If both input ranges contain only negative values, we can
    3632                 :             :   // truncate the result range maximum to the minimum of the
    3633                 :             :   // input range maxima.
    3634                 :     5635413 :   if (wi::lt_p (lh_ub, 0, sign) && wi::lt_p (rh_ub, 0, sign))
    3635                 :             :     {
    3636                 :       49612 :       new_ub = wi::min (new_ub, lh_ub, sign);
    3637                 :       49612 :       new_ub = wi::min (new_ub, rh_ub, sign);
    3638                 :             :     }
    3639                 :             :   // If either input range contains only non-negative values
    3640                 :             :   // we can truncate the result range maximum to the respective
    3641                 :             :   // maximum of the input range.
    3642                 :     5635413 :   if (wi::ge_p (lh_lb, 0, sign))
    3643                 :     4871059 :     new_ub = wi::min (new_ub, lh_ub, sign);
    3644                 :     5635413 :   if (wi::ge_p (rh_lb, 0, sign))
    3645                 :     5407025 :     new_ub = wi::min (new_ub, rh_ub, sign);
    3646                 :             :   // PR68217: In case of signed & sign-bit-CST should
    3647                 :             :   // result in [-INF, 0] instead of [-INF, INF].
    3648                 :     5635413 :   if (wi::gt_p (new_lb, new_ub, sign))
    3649                 :             :     {
    3650                 :       57978 :       wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec);
    3651                 :       57978 :       if (sign == SIGNED
    3652                 :       57978 :           && ((wi::eq_p (lh_lb, lh_ub)
    3653                 :          66 :                && !wi::cmps (lh_lb, sign_bit))
    3654                 :       57978 :               || (wi::eq_p (rh_lb, rh_ub)
    3655                 :           0 :                   && !wi::cmps (rh_lb, sign_bit))))
    3656                 :             :         {
    3657                 :           0 :           new_lb = wi::min_value (prec, sign);
    3658                 :           0 :           new_ub = wi::zero (prec);
    3659                 :             :         }
    3660                 :       57978 :     }
    3661                 :             :   // If the limits got swapped around, return varying.
    3662                 :     5635413 :   if (wi::gt_p (new_lb, new_ub,sign))
    3663                 :             :     {
    3664                 :       57978 :       if (sign == SIGNED
    3665                 :       57978 :           && wi_optimize_signed_bitwise_op (r, type,
    3666                 :             :                                             lh_lb, lh_ub,
    3667                 :             :                                             rh_lb, rh_ub))
    3668                 :        4883 :         return;
    3669                 :       53095 :       r.set_varying (type);
    3670                 :             :     }
    3671                 :             :   else
    3672                 :     5577435 :     value_range_with_overflow (r, type, new_lb, new_ub);
    3673                 :     5635413 : }
    3674                 :             : 
    3675                 :             : static void
    3676                 :      523525 : set_nonzero_range_from_mask (irange &r, tree type, const irange &lhs)
    3677                 :             : {
    3678                 :      523525 :   if (lhs.undefined_p () || contains_zero_p (lhs))
    3679                 :      276103 :     r.set_varying (type);
    3680                 :             :   else
    3681                 :      247422 :     r.set_nonzero (type);
    3682                 :      523525 : }
    3683                 :             : 
    3684                 :             : /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
    3685                 :             :    (otherwise return VAL).  VAL and MASK must be zero-extended for
    3686                 :             :    precision PREC.  If SGNBIT is non-zero, first xor VAL with SGNBIT
    3687                 :             :    (to transform signed values into unsigned) and at the end xor
    3688                 :             :    SGNBIT back.  */
    3689                 :             : 
    3690                 :             : wide_int
    3691                 :       36680 : masked_increment (const wide_int &val_in, const wide_int &mask,
    3692                 :             :                   const wide_int &sgnbit, unsigned int prec)
    3693                 :             : {
    3694                 :       36680 :   wide_int bit = wi::one (prec), res;
    3695                 :       36680 :   unsigned int i;
    3696                 :             : 
    3697                 :       36680 :   wide_int val = val_in ^ sgnbit;
    3698                 :      564392 :   for (i = 0; i < prec; i++, bit += bit)
    3699                 :             :     {
    3700                 :      518369 :       res = mask;
    3701                 :      518369 :       if ((res & bit) == 0)
    3702                 :      431059 :         continue;
    3703                 :       87310 :       res = bit - 1;
    3704                 :       87310 :       res = wi::bit_and_not (val + bit, res);
    3705                 :       87310 :       res &= mask;
    3706                 :       87310 :       if (wi::gtu_p (res, val))
    3707                 :       27337 :         return res ^ sgnbit;
    3708                 :             :     }
    3709                 :        9343 :   return val ^ sgnbit;
    3710                 :       36680 : }
    3711                 :             : 
    3712                 :             : // This was shamelessly stolen from register_edge_assert_for_2 and
    3713                 :             : // adjusted to work with iranges.
    3714                 :             : 
    3715                 :             : void
    3716                 :     3418116 : operator_bitwise_and::simple_op1_range_solver (irange &r, tree type,
    3717                 :             :                                                const irange &lhs,
    3718                 :             :                                                const irange &op2) const
    3719                 :             : {
    3720                 :     3418116 :   if (!op2.singleton_p ())
    3721                 :             :     {
    3722                 :      522602 :       set_nonzero_range_from_mask (r, type, lhs);
    3723                 :     1050948 :       return;
    3724                 :             :     }
    3725                 :     2895514 :   unsigned int nprec = TYPE_PRECISION (type);
    3726                 :     2895514 :   wide_int cst2v = op2.lower_bound ();
    3727                 :     2895514 :   bool cst2n = wi::neg_p (cst2v, TYPE_SIGN (type));
    3728                 :     2895514 :   wide_int sgnbit;
    3729                 :     2895514 :   if (cst2n)
    3730                 :      572605 :     sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
    3731                 :             :   else
    3732                 :     2322909 :     sgnbit = wi::zero (nprec);
    3733                 :             : 
    3734                 :             :   // Solve [lhs.lower_bound (), +INF] = x & MASK.
    3735                 :             :   //
    3736                 :             :   // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
    3737                 :             :   // maximum unsigned value is ~0.  For signed comparison, if CST2
    3738                 :             :   // doesn't have the most significant bit set, handle it similarly.  If
    3739                 :             :   // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
    3740                 :     2895514 :   wide_int valv = lhs.lower_bound ();
    3741                 :     2895514 :   wide_int minv = valv & cst2v, maxv;
    3742                 :     2895514 :   bool we_know_nothing = false;
    3743                 :     2895514 :   if (minv != valv)
    3744                 :             :     {
    3745                 :             :       // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
    3746                 :        7867 :       minv = masked_increment (valv, cst2v, sgnbit, nprec);
    3747                 :        7867 :       if (minv == valv)
    3748                 :             :         {
    3749                 :             :           // If we can't determine anything on this bound, fall
    3750                 :             :           // through and conservatively solve for the other end point.
    3751                 :     2895514 :           we_know_nothing = true;
    3752                 :             :         }
    3753                 :             :     }
    3754                 :     5218423 :   maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
    3755                 :     2895514 :   if (we_know_nothing)
    3756                 :        3599 :     r.set_varying (type);
    3757                 :             :   else
    3758                 :     2891915 :     create_possibly_reversed_range (r, type, minv, maxv);
    3759                 :             : 
    3760                 :             :   // Solve [-INF, lhs.upper_bound ()] = x & MASK.
    3761                 :             :   //
    3762                 :             :   // Minimum unsigned value for <= is 0 and maximum unsigned value is
    3763                 :             :   // VAL | ~CST2 if (VAL & CST2) == VAL.  Otherwise, find smallest
    3764                 :             :   // VAL2 where
    3765                 :             :   // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
    3766                 :             :   // as maximum.
    3767                 :             :   // For signed comparison, if CST2 doesn't have most significant bit
    3768                 :             :   // set, handle it similarly.  If CST2 has MSB set, the maximum is
    3769                 :             :   // the same and minimum is INT_MIN.
    3770                 :     2895514 :   valv = lhs.upper_bound ();
    3771                 :     2895514 :   minv = valv & cst2v;
    3772                 :     2895514 :   if (minv == valv)
    3773                 :     2866701 :     maxv = valv;
    3774                 :             :   else
    3775                 :             :     {
    3776                 :       28813 :       maxv = masked_increment (valv, cst2v, sgnbit, nprec);
    3777                 :       28813 :       if (maxv == valv)
    3778                 :             :         {
    3779                 :             :           // If we couldn't determine anything on either bound, return
    3780                 :             :           // undefined.
    3781                 :        5744 :           if (we_know_nothing)
    3782                 :        2634 :             r.set_undefined ();
    3783                 :        5744 :           return;
    3784                 :             :         }
    3785                 :       23069 :       maxv -= 1;
    3786                 :             :     }
    3787                 :     2889770 :   maxv |= ~cst2v;
    3788                 :     2889770 :   minv = sgnbit;
    3789                 :     2889770 :   int_range<2> upper_bits;
    3790                 :     2889770 :   create_possibly_reversed_range (upper_bits, type, minv, maxv);
    3791                 :     2889770 :   r.intersect (upper_bits);
    3792                 :     2895514 : }
    3793                 :             : 
    3794                 :             : bool
    3795                 :     3553591 : operator_bitwise_and::op1_range (irange &r, tree type,
    3796                 :             :                                  const irange &lhs,
    3797                 :             :                                  const irange &op2,
    3798                 :             :                                  relation_trio) const
    3799                 :             : {
    3800                 :     3553591 :   if (lhs.undefined_p ())
    3801                 :             :     return false;
    3802                 :     3553591 :   if (types_compatible_p (type, boolean_type_node))
    3803                 :      908608 :     return op_logical_and.op1_range (r, type, lhs, op2);
    3804                 :             : 
    3805                 :     2644983 :   r.set_undefined ();
    3806                 :     6063099 :   for (unsigned i = 0; i < lhs.num_pairs (); ++i)
    3807                 :             :     {
    3808                 :     6836232 :       int_range_max chunk (lhs.type (),
    3809                 :     6836232 :                            lhs.lower_bound (i),
    3810                 :     6836232 :                            lhs.upper_bound (i));
    3811                 :     3418116 :       int_range_max res;
    3812                 :     3418116 :       simple_op1_range_solver (res, type, chunk, op2);
    3813                 :     3418116 :       r.union_ (res);
    3814                 :     3418116 :     }
    3815                 :     2644983 :   if (r.undefined_p ())
    3816                 :         923 :     set_nonzero_range_from_mask (r, type, lhs);
    3817                 :             : 
    3818                 :             :   // For MASK == op1 & MASK, all the bits in MASK must be set in op1.
    3819                 :     2644983 :   wide_int mask;
    3820                 :     2644983 :   if (lhs == op2 && lhs.singleton_p (mask))
    3821                 :             :     {
    3822                 :      363946 :       r.update_bitmask (irange_bitmask (mask, ~mask));
    3823                 :      363946 :       return true;
    3824                 :             :     }
    3825                 :             : 
    3826                 :     2281037 :   if (!op2.singleton_p (mask))
    3827                 :             :     return true;
    3828                 :             : 
    3829                 :             :   // For 0 = op1 & MASK, op1 is ~MASK.
    3830                 :     1811048 :   if (lhs.zero_p ())
    3831                 :             :     {
    3832                 :      669038 :       wide_int nz = wi::bit_not (op2.get_nonzero_bits ());
    3833                 :      669038 :       int_range<2> tmp (type);
    3834                 :      669038 :       tmp.set_nonzero_bits (nz);
    3835                 :      669038 :       r.intersect (tmp);
    3836                 :      669038 :     }
    3837                 :             : 
    3838                 :     1811048 :   irange_bitmask lhs_bm = lhs.get_bitmask ();
    3839                 :             :   // given   [5,7]  mask 0x3 value 0x4 =  N &  [7, 7] mask 0x0 value 0x7
    3840                 :             :   // Nothing is known about the bits not specified in the mask value (op2),
    3841                 :             :   //  Start with the mask, 1's will occur where values were masked.
    3842                 :     1811048 :   wide_int op1_mask = ~mask;
    3843                 :             :   // Any bits that are unknown on the LHS are also unknown in op1,
    3844                 :             :   // so union the current mask with the LHS mask.
    3845                 :     1811048 :   op1_mask |= lhs_bm.mask ();
    3846                 :             :   // The resulting zeros correspond to known bits in the LHS mask, and
    3847                 :             :   // the LHS value should tell us what they are.  Mask off any
    3848                 :             :   // extraneous values thats are not convered by the mask.
    3849                 :     1811048 :   wide_int op1_value = lhs_bm.value () & ~op1_mask;
    3850                 :     1811048 :   irange_bitmask op1_bm (op1_value, op1_mask);
    3851                 :             :   // Intersect this mask with anything already known about the value.
    3852                 :             :   // A return valueof false indicated the bitmask is an UNDEFINED range.
    3853                 :     1811048 :   if (op1_bm.intersect (r.get_bitmask ()))
    3854                 :     1811048 :     r.update_bitmask (op1_bm);
    3855                 :             :   else
    3856                 :           0 :     r.set_undefined ();
    3857                 :     1811048 :   return true;
    3858                 :     4456031 : }
    3859                 :             : 
    3860                 :             : bool
    3861                 :      923367 : operator_bitwise_and::op2_range (irange &r, tree type,
    3862                 :             :                                  const irange &lhs,
    3863                 :             :                                  const irange &op1,
    3864                 :             :                                  relation_trio) const
    3865                 :             : {
    3866                 :      923367 :   return operator_bitwise_and::op1_range (r, type, lhs, op1);
    3867                 :             : }
    3868                 :             : 
    3869                 :             : 
    3870                 :             : class operator_logical_or : public range_operator
    3871                 :             : {
    3872                 :             :   using range_operator::fold_range;
    3873                 :             :   using range_operator::op1_range;
    3874                 :             :   using range_operator::op2_range;
    3875                 :             : public:
    3876                 :             :   bool fold_range (irange &r, tree type,
    3877                 :             :                    const irange &lh,
    3878                 :             :                    const irange &rh,
    3879                 :             :                    relation_trio rel = TRIO_VARYING) const final override;
    3880                 :             :   bool op1_range (irange &r, tree type,
    3881                 :             :                   const irange &lhs,
    3882                 :             :                   const irange &op2,
    3883                 :             :                   relation_trio rel = TRIO_VARYING) const final override;
    3884                 :             :   bool op2_range (irange &r, tree type,
    3885                 :             :                   const irange &lhs,
    3886                 :             :                   const irange &op1,
    3887                 :             :                   relation_trio rel = TRIO_VARYING) const final override;
    3888                 :             :   // Check compatibility of all operands.
    3889                 :           0 :   bool operand_check_p (tree t1, tree t2, tree t3) const final override
    3890                 :           0 :     { return range_compatible_p (t1, t2) && range_compatible_p (t1, t3); }
    3891                 :             : } op_logical_or;
    3892                 :             : 
    3893                 :             : bool
    3894                 :           0 : operator_logical_or::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
    3895                 :             :                                  const irange &lh,
    3896                 :             :                                  const irange &rh,
    3897                 :             :                                  relation_trio) const
    3898                 :             : {
    3899                 :           0 :   if (empty_range_varying (r, type, lh, rh))
    3900                 :           0 :     return true;
    3901                 :             : 
    3902                 :           0 :   r = lh;
    3903                 :           0 :   r.union_ (rh);
    3904                 :           0 :   return true;
    3905                 :             : }
    3906                 :             : 
    3907                 :             : bool
    3908                 :      337403 : operator_logical_or::op1_range (irange &r, tree type,
    3909                 :             :                                 const irange &lhs,
    3910                 :             :                                 const irange &op2 ATTRIBUTE_UNUSED,
    3911                 :             :                                 relation_trio) const
    3912                 :             : {
    3913                 :      337403 :    switch (get_bool_state (r, lhs, type))
    3914                 :             :      {
    3915                 :      203997 :      case BRS_FALSE:
    3916                 :             :        // A false result means both sides of the OR must be false.
    3917                 :      203997 :        r = range_false (type);
    3918                 :      203997 :        break;
    3919                 :      133406 :      default:
    3920                 :             :        // Any other result means only one side has to be true, the
    3921                 :             :        // other side can be anything. so we can't be sure of any result
    3922                 :             :        // here.
    3923                 :      133406 :        r = range_true_and_false (type);
    3924                 :      133406 :        break;
    3925                 :             :     }
    3926                 :      337403 :   return true;
    3927                 :             : }
    3928                 :             : 
    3929                 :             : bool
    3930                 :           0 : operator_logical_or::op2_range (irange &r, tree type,
    3931                 :             :                                 const irange &lhs,
    3932                 :             :                                 const irange &op1,
    3933                 :             :                                 relation_trio) const
    3934                 :             : {
    3935                 :           0 :   return operator_logical_or::op1_range (r, type, lhs, op1);
    3936                 :             : }
    3937                 :             : 
    3938                 :             : 
    3939                 :             : void
    3940                 :     2471905 : operator_bitwise_or::update_bitmask (irange &r, const irange &lh,
    3941                 :             :                                      const irange &rh) const
    3942                 :             : {
    3943                 :     2471905 :   update_known_bitmask (r, BIT_IOR_EXPR, lh, rh);
    3944                 :     2471905 : }
    3945                 :             : 
    3946                 :             : void
    3947                 :     7174358 : operator_bitwise_or::wi_fold (irange &r, tree type,
    3948                 :             :                               const wide_int &lh_lb,
    3949                 :             :                               const wide_int &lh_ub,
    3950                 :             :                               const wide_int &rh_lb,
    3951                 :             :                               const wide_int &rh_ub) const
    3952                 :             : {
    3953                 :     7174358 :   if (wi_optimize_and_or (r, BIT_IOR_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
    3954                 :     6477822 :     return;
    3955                 :             : 
    3956                 :      888128 :   wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
    3957                 :      888128 :   wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
    3958                 :      888128 :   wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
    3959                 :             :                             maybe_nonzero_lh, mustbe_nonzero_lh);
    3960                 :      888128 :   wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
    3961                 :             :                             maybe_nonzero_rh, mustbe_nonzero_rh);
    3962                 :      888128 :   wide_int new_lb = mustbe_nonzero_lh | mustbe_nonzero_rh;
    3963                 :      888128 :   wide_int new_ub = maybe_nonzero_lh | maybe_nonzero_rh;
    3964                 :      888128 :   signop sign = TYPE_SIGN (type);
    3965                 :             :   // If the input ranges contain only positive values we can
    3966                 :             :   // truncate the minimum of the result range to the maximum
    3967                 :             :   // of the input range minima.
    3968                 :      888128 :   if (wi::ge_p (lh_lb, 0, sign)
    3969                 :      888128 :       && wi::ge_p (rh_lb, 0, sign))
    3970                 :             :     {
    3971                 :      667139 :       new_lb = wi::max (new_lb, lh_lb, sign);
    3972                 :      667139 :       new_lb = wi::max (new_lb, rh_lb, sign);
    3973                 :             :     }
    3974                 :             :   // If either input range contains only negative values
    3975                 :             :   // we can truncate the minimum of the result range to the
    3976                 :             :   // respective minimum range.
    3977                 :      888128 :   if (wi::lt_p (lh_ub, 0, sign))
    3978                 :       20952 :     new_lb = wi::max (new_lb, lh_lb, sign);
    3979                 :      888128 :   if (wi::lt_p (rh_ub, 0, sign))
    3980                 :        9440 :     new_lb = wi::max (new_lb, rh_lb, sign);
    3981                 :             :   // If the limits got swapped around, return a conservative range.
    3982                 :      888128 :   if (wi::gt_p (new_lb, new_ub, sign))
    3983                 :             :     {
    3984                 :             :       // Make sure that nonzero|X is nonzero.
    3985                 :      191592 :       if (wi::gt_p (lh_lb, 0, sign)
    3986                 :      186884 :           || wi::gt_p (rh_lb, 0, sign)
    3987                 :      119329 :           || wi::lt_p (lh_ub, 0, sign)
    3988                 :      310921 :           || wi::lt_p (rh_ub, 0, sign))
    3989                 :       72263 :         r.set_nonzero (type);
    3990                 :      119329 :       else if (sign == SIGNED
    3991                 :      119329 :                && wi_optimize_signed_bitwise_op (r, type,
    3992                 :             :                                                  lh_lb, lh_ub,
    3993                 :             :                                                  rh_lb, rh_ub))
    3994                 :             :         return;
    3995                 :             :       else
    3996                 :      116493 :         r.set_varying (type);
    3997                 :      188756 :       return;
    3998                 :             :     }
    3999                 :      696536 :   value_range_with_overflow (r, type, new_lb, new_ub);
    4000                 :      888158 : }
    4001                 :             : 
    4002                 :             : bool
    4003                 :      581161 : operator_bitwise_or::op1_range (irange &r, tree type,
    4004                 :             :                                 const irange &lhs,
    4005                 :             :                                 const irange &op2,
    4006                 :             :                                 relation_trio) const
    4007                 :             : {
    4008                 :      581161 :   if (lhs.undefined_p ())
    4009                 :             :     return false;
    4010                 :             :   // If this is really a logical wi_fold, call that.
    4011                 :      581161 :   if (types_compatible_p (type, boolean_type_node))
    4012                 :      337403 :     return op_logical_or.op1_range (r, type, lhs, op2);
    4013                 :             : 
    4014                 :      243758 :   if (lhs.zero_p ())
    4015                 :             :     {
    4016                 :       76608 :       r.set_zero (type);
    4017                 :       76608 :       return true;
    4018                 :             :     }
    4019                 :             : 
    4020                 :             :   //   if (A < 0 && B < 0)
    4021                 :             :   // Sometimes gets translated to
    4022                 :             :   //   _1 = A | B
    4023                 :             :   //   if (_1 < 0))
    4024                 :             :   // It is useful for ranger to recognize a positive LHS means the RHS
    4025                 :             :   // operands are also positive when dealing with the ELSE range..
    4026                 :      236185 :   if (TYPE_SIGN (type) == SIGNED && wi::ge_p (lhs.lower_bound (), 0, SIGNED))
    4027                 :             :     {
    4028                 :       27021 :       unsigned prec = TYPE_PRECISION (type);
    4029                 :       27021 :       r.set (type, wi::zero (prec), wi::max_value (prec, SIGNED));
    4030                 :       27021 :       return true;
    4031                 :             :     }
    4032                 :      140129 :   r.set_varying (type);
    4033                 :      140129 :   return true;
    4034                 :             : }
    4035                 :             : 
    4036                 :             : bool
    4037                 :      292577 : operator_bitwise_or::op2_range (irange &r, tree type,
    4038                 :             :                                 const irange &lhs,
    4039                 :             :                                 const irange &op1,
    4040                 :             :                                 relation_trio) const
    4041                 :             : {
    4042                 :      292577 :   return operator_bitwise_or::op1_range (r, type, lhs, op1);
    4043                 :             : }
    4044                 :             : 
    4045                 :             : void
    4046                 :       87205 : operator_bitwise_xor::update_bitmask (irange &r, const irange &lh,
    4047                 :             :                                       const irange &rh) const
    4048                 :             : {
    4049                 :       87205 :   update_known_bitmask (r, BIT_XOR_EXPR, lh, rh);
    4050                 :       87205 : }
    4051                 :             : 
    4052                 :             : bool
    4053                 :      295716 : operator_bitwise_xor::fold_range (irange &r, tree type,
    4054                 :             :                                   const irange &lh, const irange &rh,
    4055                 :             :                                   relation_trio rel) const
    4056                 :             : {
    4057                 :             :   // Handle X ^ UNDEFINED = UNDEFINED.
    4058                 :      295716 :   if (lh.undefined_p () || rh.undefined_p ())
    4059                 :             :     {
    4060                 :         394 :       r.set_undefined ();
    4061                 :         394 :       return true;
    4062                 :             :     }
    4063                 :             : 
    4064                 :             :   // Next, handle X ^ X == [0, 0].
    4065                 :      295322 :   if (rel.op1_op2 () == VREL_EQ)
    4066                 :             :    {
    4067                 :          40 :      r.set_zero (type);
    4068                 :          40 :      return true;
    4069                 :             :    }
    4070                 :             : 
    4071                 :             :   // If either operand is VARYING, the result is VARYING.
    4072                 :      295282 :   if (lh.varying_p () || rh.varying_p ())
    4073                 :             :     {
    4074                 :             :       // If the operands are not equal, zero is not possible.
    4075                 :      205938 :       if (rel.op1_op2 () != VREL_NE)
    4076                 :      204969 :         r.set_varying (type);
    4077                 :             :       else
    4078                 :         969 :         r.set_nonzero (type);
    4079                 :      205938 :       return true;
    4080                 :             :     }
    4081                 :             : 
    4082                 :             :   // Now deal with X ^ 0 == X.
    4083                 :       89344 :   if (lh.zero_p ())
    4084                 :             :     {
    4085                 :        1620 :       r = rh;
    4086                 :        1620 :       return true;
    4087                 :             :     }
    4088                 :       87724 :   if (rh.zero_p ())
    4089                 :             :     {
    4090                 :         519 :       r = lh;
    4091                 :         519 :       return true;
    4092                 :             :     }
    4093                 :             : 
    4094                 :             :   // Start with the legacy range.  This can sometimes pick up values
    4095                 :             :   // when there are a lot of subranges and fold_range aggregates them.
    4096                 :       87205 :   bool res = range_operator::fold_range (r, type, lh, rh, rel);
    4097                 :             : 
    4098                 :             :   // Calculate the XOR identity :   x ^ y = (x | y) & ~(x & y)
    4099                 :             :   // AND and OR are already much better optimized.
    4100                 :       87205 :   int_range_max tmp1, tmp2, tmp3, new_result;
    4101                 :       87205 :   int_range<2> varying;
    4102                 :       87205 :   varying.set_varying (type);
    4103                 :             : 
    4104                 :       87205 :   if (m_or.fold_range  (tmp1, type, lh, rh, rel)
    4105                 :       87205 :       && m_and.fold_range (tmp2, type, lh, rh, rel)
    4106                 :       87205 :       && m_not.fold_range (tmp3, type, tmp2, varying, rel)
    4107                 :      174410 :       && m_and.fold_range (new_result, type, tmp1, tmp3, rel))
    4108                 :             :     {
    4109                 :             :       // If the operands are not equal, or the LH does not contain any
    4110                 :             :       // element of the RH, zero is not possible.
    4111                 :       87205 :       tmp1 = lh;
    4112                 :       87205 :       if (rel.op1_op2 () == VREL_NE
    4113                 :       87205 :           || (tmp1.intersect (rh) && tmp1.undefined_p ()))
    4114                 :             :         {
    4115                 :       31413 :           tmp1.set_nonzero (type);
    4116                 :       31413 :           new_result.intersect (tmp1);
    4117                 :             :         }
    4118                 :             : 
    4119                 :             :       // Combine with the legacy range if there was one.
    4120                 :       87205 :       if (res)
    4121                 :       87205 :         r.intersect (new_result);
    4122                 :             :       else
    4123                 :           0 :         r = new_result;
    4124                 :       87205 :       return true;
    4125                 :             :     }
    4126                 :             :   return res;
    4127                 :       87205 : }
    4128                 :             : 
    4129                 :             : void
    4130                 :      177903 : operator_bitwise_xor::wi_fold (irange &r, tree type,
    4131                 :             :                                const wide_int &lh_lb,
    4132                 :             :                                const wide_int &lh_ub,
    4133                 :             :                                const wide_int &rh_lb,
    4134                 :             :                                const wide_int &rh_ub) const
    4135                 :             : {
    4136                 :      177903 :   signop sign = TYPE_SIGN (type);
    4137                 :      177903 :   wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
    4138                 :      177903 :   wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
    4139                 :      177903 :   wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
    4140                 :             :                             maybe_nonzero_lh, mustbe_nonzero_lh);
    4141                 :      177903 :   wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
    4142                 :             :                             maybe_nonzero_rh, mustbe_nonzero_rh);
    4143                 :             : 
    4144                 :      533709 :   wide_int result_zero_bits = ((mustbe_nonzero_lh & mustbe_nonzero_rh)
    4145                 :      533709 :                                | ~(maybe_nonzero_lh | maybe_nonzero_rh));
    4146                 :      177903 :   wide_int result_one_bits
    4147                 :      355806 :     = (wi::bit_and_not (mustbe_nonzero_lh, maybe_nonzero_rh)
    4148                 :      355806 :        | wi::bit_and_not (mustbe_nonzero_rh, maybe_nonzero_lh));
    4149                 :      177903 :   wide_int new_ub = ~result_zero_bits;
    4150                 :      177903 :   wide_int new_lb = result_one_bits;
    4151                 :             : 
    4152                 :             :   // If the range has all positive or all negative values, the result
    4153                 :             :   // is better than VARYING.
    4154                 :      177903 :   if (wi::lt_p (new_lb, 0, sign) || wi::ge_p (new_ub, 0, sign))
    4155                 :      170878 :     value_range_with_overflow (r, type, new_lb, new_ub);
    4156                 :        7025 :   else if (sign == SIGNED
    4157                 :        7025 :            && wi_optimize_signed_bitwise_op (r, type,
    4158                 :             :                                              lh_lb, lh_ub,
    4159                 :             :                                              rh_lb, rh_ub))
    4160                 :             :     ;  /* Do nothing.  */
    4161                 :             :   else
    4162                 :         818 :     r.set_varying (type);
    4163                 :             : 
    4164                 :             :   /* Furthermore, XOR is non-zero if its arguments can't be equal.  */
    4165                 :      177903 :   if (wi::lt_p (lh_ub, rh_lb, sign)
    4166                 :      120830 :       || wi::lt_p (rh_ub, lh_lb, sign)
    4167                 :      272838 :       || wi::ne_p (result_one_bits, 0))
    4168                 :             :     {
    4169                 :       82968 :       int_range<2> tmp;
    4170                 :       82968 :       tmp.set_nonzero (type);
    4171                 :       82968 :       r.intersect (tmp);
    4172                 :       82968 :     }
    4173                 :      177903 : }
    4174                 :             : 
    4175                 :             : bool
    4176                 :       87205 : operator_bitwise_xor::op1_op2_relation_effect (irange &lhs_range,
    4177                 :             :                                                tree type,
    4178                 :             :                                                const irange &,
    4179                 :             :                                                const irange &,
    4180                 :             :                                                relation_kind rel) const
    4181                 :             : {
    4182                 :       87205 :   if (rel == VREL_VARYING)
    4183                 :             :     return false;
    4184                 :             : 
    4185                 :        6220 :   int_range<2> rel_range;
    4186                 :             : 
    4187                 :        6220 :   switch (rel)
    4188                 :             :     {
    4189                 :           0 :     case VREL_EQ:
    4190                 :           0 :       rel_range.set_zero (type);
    4191                 :           0 :       break;
    4192                 :         407 :     case VREL_NE:
    4193                 :         407 :       rel_range.set_nonzero (type);
    4194                 :         407 :       break;
    4195                 :             :     default:
    4196                 :             :       return false;
    4197                 :             :     }
    4198                 :             : 
    4199                 :         407 :   lhs_range.intersect (rel_range);
    4200                 :         407 :   return true;
    4201                 :        6220 : }
    4202                 :             : 
    4203                 :             : bool
    4204                 :       78781 : operator_bitwise_xor::op1_range (irange &r, tree type,
    4205                 :             :                                  const irange &lhs,
    4206                 :             :                                  const irange &op2,
    4207                 :             :                                  relation_trio) const
    4208                 :             : {
    4209                 :       78781 :   if (lhs.undefined_p () || lhs.varying_p ())
    4210                 :             :     {
    4211                 :        8047 :       r = lhs;
    4212                 :        8047 :       return true;
    4213                 :             :     }
    4214                 :       70734 :   if (types_compatible_p (type, boolean_type_node))
    4215                 :             :     {
    4216                 :        2762 :       switch (get_bool_state (r, lhs, type))
    4217                 :             :         {
    4218                 :        1283 :         case BRS_TRUE:
    4219                 :        1283 :           if (op2.varying_p ())
    4220                 :        1219 :             r.set_varying (type);
    4221                 :          64 :           else if (op2.zero_p ())
    4222                 :          48 :             r = range_true (type);
    4223                 :             :           // See get_bool_state for the rationale
    4224                 :          16 :           else if (op2.undefined_p () || contains_zero_p (op2))
    4225                 :           0 :             r = range_true_and_false (type);
    4226                 :             :           else
    4227                 :          16 :             r = range_false (type);
    4228                 :             :           break;
    4229                 :        1479 :         case BRS_FALSE:
    4230                 :        1479 :           r = op2;
    4231                 :        1479 :           break;
    4232                 :             :         default:
    4233                 :             :           break;
    4234                 :             :         }
    4235                 :        2762 :       return true;
    4236                 :             :     }
    4237                 :       67972 :   r.set_varying (type);
    4238                 :       67972 :   return true;
    4239                 :             : }
    4240                 :             : 
    4241                 :             : bool
    4242                 :       34580 : operator_bitwise_xor::op2_range (irange &r, tree type,
    4243                 :             :                                  const irange &lhs,
    4244                 :             :                                  const irange &op1,
    4245                 :             :                                  relation_trio) const
    4246                 :             : {
    4247                 :       34580 :   return operator_bitwise_xor::op1_range (r, type, lhs, op1);
    4248                 :             : }
    4249                 :             : 
    4250                 :             : class operator_trunc_mod : public range_operator
    4251                 :             : {
    4252                 :             :   using range_operator::op1_range;
    4253                 :             :   using range_operator::op2_range;
    4254                 :             :   using range_operator::update_bitmask;
    4255                 :             : public:
    4256                 :             :   virtual void wi_fold (irange &r, tree type,
    4257                 :             :                         const wide_int &lh_lb,
    4258                 :             :                         const wide_int &lh_ub,
    4259                 :             :                         const wide_int &rh_lb,
    4260                 :             :                         const wide_int &rh_ub) const;
    4261                 :             :   virtual bool op1_range (irange &r, tree type,
    4262                 :             :                           const irange &lhs,
    4263                 :             :                           const irange &op2,
    4264                 :             :                           relation_trio) const;
    4265                 :             :   virtual bool op2_range (irange &r, tree type,
    4266                 :             :                           const irange &lhs,
    4267                 :             :                           const irange &op1,
    4268                 :             :                           relation_trio) const;
    4269                 :     1058226 :   void update_bitmask (irange &r, const irange &lh, const irange &rh) const
    4270                 :     1058226 :     { update_known_bitmask (r, TRUNC_MOD_EXPR, lh, rh); }
    4271                 :             : } op_trunc_mod;
    4272                 :             : 
    4273                 :             : void
    4274                 :     1306614 : operator_trunc_mod::wi_fold (irange &r, tree type,
    4275                 :             :                              const wide_int &lh_lb,
    4276                 :             :                              const wide_int &lh_ub,
    4277                 :             :                              const wide_int &rh_lb,
    4278                 :             :                              const wide_int &rh_ub) const
    4279                 :             : {
    4280                 :     1306614 :   wide_int new_lb, new_ub, tmp;
    4281                 :     1306614 :   signop sign = TYPE_SIGN (type);
    4282                 :     1306614 :   unsigned prec = TYPE_PRECISION (type);
    4283                 :             : 
    4284                 :             :   // Mod 0 is undefined.
    4285                 :     1306614 :   if (wi_zero_p (type, rh_lb, rh_ub))
    4286                 :             :     {
    4287                 :        8361 :       r.set_undefined ();
    4288                 :        8361 :       return;
    4289                 :             :     }
    4290                 :             : 
    4291                 :             :   // Check for constant and try to fold.
    4292                 :     1510853 :   if (lh_lb == lh_ub && rh_lb == rh_ub)
    4293                 :             :     {
    4294                 :       22969 :       wi::overflow_type ov = wi::OVF_NONE;
    4295                 :       22969 :       tmp = wi::mod_trunc (lh_lb, rh_lb, sign, &ov);
    4296                 :       22969 :       if (ov == wi::OVF_NONE)
    4297                 :             :         {
    4298                 :       22969 :           r = int_range<2> (type, tmp, tmp);
    4299                 :       22969 :           return;
    4300                 :             :         }
    4301                 :             :     }
    4302                 :             : 
    4303                 :             :   // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
    4304                 :     1275284 :   new_ub = rh_ub - 1;
    4305                 :     1275284 :   if (sign == SIGNED)
    4306                 :             :     {
    4307                 :      543066 :       tmp = -1 - rh_lb;
    4308                 :      543066 :       new_ub = wi::smax (new_ub, tmp);
    4309                 :             :     }
    4310                 :             : 
    4311                 :     1275284 :   if (sign == UNSIGNED)
    4312                 :      732218 :     new_lb = wi::zero (prec);
    4313                 :             :   else
    4314                 :             :     {
    4315                 :      543066 :       new_lb = -new_ub;
    4316                 :      543066 :       tmp = lh_lb;
    4317                 :      543066 :       if (wi::gts_p (tmp, 0))
    4318                 :      133305 :         tmp = wi::zero (prec);
    4319                 :      543090 :       new_lb = wi::smax (new_lb, tmp);
    4320                 :             :     }
    4321                 :     1275284 :   tmp = lh_ub;
    4322                 :     1275284 :   if (sign == SIGNED && wi::neg_p (tmp))
    4323                 :       25873 :     tmp = wi::zero (prec);
    4324                 :     1275284 :   new_ub = wi::min (new_ub, tmp, sign);
    4325                 :             : 
    4326                 :     1275284 :   value_range_with_overflow (r, type, new_lb, new_ub);
    4327                 :     1306738 : }
    4328                 :             : 
    4329                 :             : bool
    4330                 :      549558 : operator_trunc_mod::op1_range (irange &r, tree type,
    4331                 :             :                                const irange &lhs,
    4332                 :             :                                const irange &,
    4333                 :             :                                relation_trio) const
    4334                 :             : {
    4335                 :      549558 :   if (lhs.undefined_p ())
    4336                 :             :     return false;
    4337                 :             :   // PR 91029.
    4338                 :      549558 :   signop sign = TYPE_SIGN (type);
    4339                 :      549558 :   unsigned prec = TYPE_PRECISION (type);
    4340                 :             :   // (a % b) >= x && x > 0 , then a >= x.
    4341                 :      549650 :   if (wi::gt_p (lhs.lower_bound (), 0, sign))
    4342                 :             :     {
    4343                 :      133830 :       r.set (type, lhs.lower_bound (), wi::max_value (prec, sign));
    4344                 :      133797 :       return true;
    4345                 :             :     }
    4346                 :             :   // (a % b) <= x && x < 0 , then a <= x.
    4347                 :      415820 :   if (wi::lt_p (lhs.upper_bound (), 0, sign))
    4348                 :             :     {
    4349                 :        5638 :       r.set (type, wi::min_value (prec, sign), lhs.upper_bound ());
    4350                 :        5638 :       return true;
    4351                 :             :     }
    4352                 :             :   return false;
    4353                 :             : }
    4354                 :             : 
    4355                 :             : bool
    4356                 :      354496 : operator_trunc_mod::op2_range (irange &r, tree type,
    4357                 :             :                                const irange &lhs,
    4358                 :             :                                const irange &,
    4359                 :             :                                relation_trio) const
    4360                 :             : {
    4361                 :      354496 :   if (lhs.undefined_p ())
    4362                 :             :     return false;
    4363                 :             :   // PR 91029.
    4364                 :      354496 :   signop sign = TYPE_SIGN (type);
    4365                 :      354496 :   unsigned prec = TYPE_PRECISION (type);
    4366                 :             :   // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
    4367                 :             :   //                           or b > x for unsigned.
    4368                 :      354529 :   if (wi::gt_p (lhs.lower_bound (), 0, sign))
    4369                 :             :     {
    4370                 :       56147 :       if (sign == SIGNED)
    4371                 :        2165 :         r.set (type, wi::neg (lhs.lower_bound ()),
    4372                 :        4330 :                lhs.lower_bound (), VR_ANTI_RANGE);
    4373                 :       53994 :       else if (wi::lt_p (lhs.lower_bound (), wi::max_value (prec, sign),
    4374                 :             :                          sign))
    4375                 :       54000 :         r.set (type, lhs.lower_bound () + 1, wi::max_value (prec, sign));
    4376                 :             :       else
    4377                 :             :         return false;
    4378                 :       56147 :       return true;
    4379                 :             :     }
    4380                 :             :   // (a % b) <= x && x < 0 , then b is in ~[x, -x].
    4381                 :      298376 :   if (wi::lt_p (lhs.upper_bound (), 0, sign))
    4382                 :             :     {
    4383                 :        5282 :       if (wi::gt_p (lhs.upper_bound (), wi::min_value (prec, sign), sign))
    4384                 :        5282 :         r.set (type, lhs.upper_bound (),
    4385                 :       10564 :                wi::neg (lhs.upper_bound ()), VR_ANTI_RANGE);
    4386                 :             :       else
    4387                 :             :         return false;
    4388                 :        5282 :       return true;
    4389                 :             :     }
    4390                 :             :   return false;
    4391                 :             : }
    4392                 :             : 
    4393                 :             : 
    4394                 :             : class operator_logical_not : public range_operator
    4395                 :             : {
    4396                 :             :   using range_operator::fold_range;
    4397                 :             :   using range_operator::op1_range;
    4398                 :             : public:
    4399                 :             :   bool fold_range (irange &r, tree type,
    4400                 :             :                    const irange &lh,
    4401                 :             :                    const irange &rh,
    4402                 :             :                    relation_trio rel = TRIO_VARYING) const final override;
    4403                 :             :   bool op1_range (irange &r, tree type,
    4404                 :             :                   const irange &lhs,
    4405                 :             :                   const irange &op2,
    4406                 :             :                   relation_trio rel = TRIO_VARYING) const final override;
    4407                 :             :   // Check compatibility of LHS and op1.
    4408                 :           0 :   bool operand_check_p (tree t1, tree t2, tree) const final override
    4409                 :           0 :     { return range_compatible_p (t1, t2); }
    4410                 :             : } op_logical_not;
    4411                 :             : 
    4412                 :             : // Folding a logical NOT, oddly enough, involves doing nothing on the
    4413                 :             : // forward pass through.  During the initial walk backwards, the
    4414                 :             : // logical NOT reversed the desired outcome on the way back, so on the
    4415                 :             : // way forward all we do is pass the range forward.
    4416                 :             : //
    4417                 :             : //      b_2 = x_1 < 20
    4418                 :             : //      b_3 = !b_2
    4419                 :             : //      if (b_3)
    4420                 :             : //  to determine the TRUE branch, walking  backward
    4421                 :             : //       if (b_3)               if ([1,1])
    4422                 :             : //       b_3 = !b_2             [1,1] = ![0,0]
    4423                 :             : //       b_2 = x_1 < 20              [0,0] = x_1 < 20,   false, so x_1 == [20, 255]
    4424                 :             : //   which is the result we are looking for.. so.. pass it through.
    4425                 :             : 
    4426                 :             : bool
    4427                 :      244489 : operator_logical_not::fold_range (irange &r, tree type,
    4428                 :             :                                   const irange &lh,
    4429                 :             :                                   const irange &rh ATTRIBUTE_UNUSED,
    4430                 :             :                                   relation_trio) const
    4431                 :             : {
    4432                 :      244489 :   if (empty_range_varying (r, type, lh, rh))
    4433                 :           0 :     return true;
    4434                 :             : 
    4435                 :      244489 :   r = lh;
    4436                 :      244489 :   if (!lh.varying_p () && !lh.undefined_p ())
    4437                 :       50872 :     r.invert ();
    4438                 :             : 
    4439                 :             :   return true;
    4440                 :             : }
    4441                 :             : 
    4442                 :             : bool
    4443                 :       26349 : operator_logical_not::op1_range (irange &r,
    4444                 :             :                                  tree type,
    4445                 :             :                                  const irange &lhs,
    4446                 :             :                                  const irange &op2,
    4447                 :             :                                  relation_trio) const
    4448                 :             : {
    4449                 :             :   // Logical NOT is involutary...do it again.
    4450                 :       26349 :   return fold_range (r, type, lhs, op2);
    4451                 :             : }
    4452                 :             : 
    4453                 :             : bool
    4454                 :      612644 : operator_bitwise_not::fold_range (irange &r, tree type,
    4455                 :             :                                   const irange &lh,
    4456                 :             :                                   const irange &rh,
    4457                 :             :                                   relation_trio) const
    4458                 :             : {
    4459                 :      612644 :   if (empty_range_varying (r, type, lh, rh))
    4460                 :          97 :     return true;
    4461                 :             : 
    4462                 :      612547 :   if (types_compatible_p (type, boolean_type_node))
    4463                 :      218140 :     return op_logical_not.fold_range (r, type, lh, rh);
    4464                 :             : 
    4465                 :             :   // ~X is simply -1 - X.
    4466                 :      788814 :   int_range<1> minusone (type, wi::minus_one (TYPE_PRECISION (type)),
    4467                 :      788814 :                          wi::minus_one (TYPE_PRECISION (type)));
    4468                 :      394407 :   return range_op_handler (MINUS_EXPR).fold_range (r, type, minusone, lh);
    4469                 :      394407 : }
    4470                 :             : 
    4471                 :             : bool
    4472                 :       51844 : operator_bitwise_not::op1_range (irange &r, tree type,
    4473                 :             :                                  const irange &lhs,
    4474                 :             :                                  const irange &op2,
    4475                 :             :                                  relation_trio) const
    4476                 :             : {
    4477                 :       51844 :   if (lhs.undefined_p ())
    4478                 :             :     return false;
    4479                 :       51844 :   if (types_compatible_p (type, boolean_type_node))
    4480                 :       26349 :     return op_logical_not.op1_range (r, type, lhs, op2);
    4481                 :             : 
    4482                 :             :   // ~X is -1 - X and since bitwise NOT is involutary...do it again.
    4483                 :       25495 :   return fold_range (r, type, lhs, op2);
    4484                 :             : }
    4485                 :             : 
    4486                 :             : void
    4487                 :           0 : operator_bitwise_not::update_bitmask (irange &r, const irange &lh,
    4488                 :             :                                       const irange &rh) const
    4489                 :             : {
    4490                 :           0 :   update_known_bitmask (r, BIT_NOT_EXPR, lh, rh);
    4491                 :           0 : }
    4492                 :             : 
    4493                 :             : 
    4494                 :             : bool
    4495                 :      306688 : operator_cst::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
    4496                 :             :                           const irange &lh,
    4497                 :             :                           const irange &rh ATTRIBUTE_UNUSED,
    4498                 :             :                           relation_trio) const
    4499                 :             : {
    4500                 :      306688 :   r = lh;
    4501                 :      306688 :   return true;
    4502                 :             : }
    4503                 :             : 
    4504                 :             : 
    4505                 :             : // Determine if there is a relationship between LHS and OP1.
    4506                 :             : 
    4507                 :             : relation_kind
    4508                 :      937245 : operator_identity::lhs_op1_relation (const irange &lhs,
    4509                 :             :                                      const irange &op1 ATTRIBUTE_UNUSED,
    4510                 :             :                                      const irange &op2 ATTRIBUTE_UNUSED,
    4511                 :             :                                      relation_kind) const
    4512                 :             : {
    4513                 :      937245 :   if (lhs.undefined_p ())
    4514                 :         483 :     return VREL_VARYING;
    4515                 :             :   // Simply a copy, so they are equivalent.
    4516                 :             :   return VREL_EQ;
    4517                 :             : }
    4518                 :             : 
    4519                 :             : bool
    4520                 :      937812 : operator_identity::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
    4521                 :             :                                const irange &lh,
    4522                 :             :                                const irange &rh ATTRIBUTE_UNUSED,
    4523                 :             :                                relation_trio) const
    4524                 :             : {
    4525                 :      937812 :   r = lh;
    4526                 :      937812 :   return true;
    4527                 :             : }
    4528                 :             : 
    4529                 :             : bool
    4530                 :      286365 : operator_identity::op1_range (irange &r, tree type ATTRIBUTE_UNUSED,
    4531                 :             :                               const irange &lhs,
    4532                 :             :                               const irange &op2 ATTRIBUTE_UNUSED,
    4533                 :             :                               relation_trio) const
    4534                 :             : {
    4535                 :      286365 :   r = lhs;
    4536                 :      286365 :   return true;
    4537                 :             : }
    4538                 :             : 
    4539                 :             : 
    4540                 :             : class operator_unknown : public range_operator
    4541                 :             : {
    4542                 :             :   using range_operator::fold_range;
    4543                 :             : public:
    4544                 :             :   virtual bool fold_range (irange &r, tree type,
    4545                 :             :                            const irange &op1,
    4546                 :             :                            const irange &op2,
    4547                 :             :                            relation_trio rel = TRIO_VARYING) const;
    4548                 :             : } op_unknown;
    4549                 :             : 
    4550                 :             : bool
    4551                 :      825424 : operator_unknown::fold_range (irange &r, tree type,
    4552                 :             :                               const irange &lh ATTRIBUTE_UNUSED,
    4553                 :             :                               const irange &rh ATTRIBUTE_UNUSED,
    4554                 :             :                               relation_trio) const
    4555                 :             : {
    4556                 :      825424 :   r.set_varying (type);
    4557                 :      825424 :   return true;
    4558                 :             : }
    4559                 :             : 
    4560                 :             : 
    4561                 :             : void
    4562                 :       82977 : operator_abs::wi_fold (irange &r, tree type,
    4563                 :             :                        const wide_int &lh_lb, const wide_int &lh_ub,
    4564                 :             :                        const wide_int &rh_lb ATTRIBUTE_UNUSED,
    4565                 :             :                        const wide_int &rh_ub ATTRIBUTE_UNUSED) const
    4566                 :             : {
    4567                 :       82977 :   wide_int min, max;
    4568                 :       82977 :   signop sign = TYPE_SIGN (type);
    4569                 :       82977 :   unsigned prec = TYPE_PRECISION (type);
    4570                 :             : 
    4571                 :             :   // Pass through LH for the easy cases.
    4572                 :       82977 :   if (sign == UNSIGNED || wi::ge_p (lh_lb, 0, sign))
    4573                 :             :     {
    4574                 :       12412 :       r = int_range<1> (type, lh_lb, lh_ub);
    4575                 :       12412 :       return;
    4576                 :             :     }
    4577                 :             : 
    4578                 :             :   // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
    4579                 :             :   // a useful range.
    4580                 :       70565 :   wide_int min_value = wi::min_value (prec, sign);
    4581                 :       70565 :   wide_int max_value = wi::max_value (prec, sign);
    4582                 :       70565 :   if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lh_lb, min_value))
    4583                 :             :     {
    4584                 :         640 :       r.set_varying (type);
    4585                 :         640 :       return;
    4586                 :             :     }
    4587                 :             : 
    4588                 :             :   // ABS_EXPR may flip the range around, if the original range
    4589                 :             :   // included negative values.
    4590                 :       69925 :   if (wi::eq_p (lh_lb, min_value))
    4591                 :             :     {
    4592                 :             :       // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
    4593                 :             :       // returned [-MIN,-MIN] so this preserves that behavior.  PR37078
    4594                 :       37234 :       if (wi::eq_p (lh_ub, min_value))
    4595                 :             :         {
    4596                 :         113 :           r = int_range<1> (type, min_value, min_value);
    4597                 :         113 :           return;
    4598                 :             :         }
    4599                 :       37121 :       min = max_value;
    4600                 :             :     }
    4601                 :             :   else
    4602                 :       32691 :     min = wi::abs (lh_lb);
    4603                 :             : 
    4604                 :       69812 :   if (wi::eq_p (lh_ub, min_value))
    4605                 :           0 :     max = max_value;
    4606                 :             :   else
    4607                 :       69812 :     max = wi::abs (lh_ub);
    4608                 :             : 
    4609                 :             :   // If the range contains zero then we know that the minimum value in the
    4610                 :             :   // range will be zero.
    4611                 :       69812 :   if (wi::le_p (lh_lb, 0, sign) && wi::ge_p (lh_ub, 0, sign))
    4612                 :             :     {
    4613                 :       59792 :       if (wi::gt_p (min, max, sign))
    4614                 :       21531 :         max = min;
    4615                 :       59792 :       min = wi::zero (prec);
    4616                 :             :     }
    4617                 :             :   else
    4618                 :             :     {
    4619                 :             :       // If the range was reversed, swap MIN and MAX.
    4620                 :       10020 :       if (wi::gt_p (min, max, sign))
    4621                 :        9422 :         std::swap (min, max);
    4622                 :             :     }
    4623                 :             : 
    4624                 :             :   // If the new range has its limits swapped around (MIN > MAX), then
    4625                 :             :   // the operation caused one of them to wrap around.  The only thing
    4626                 :             :   // we know is that the result is positive.
    4627                 :       69812 :   if (wi::gt_p (min, max, sign))
    4628                 :             :     {
    4629                 :           0 :       min = wi::zero (prec);
    4630                 :           0 :       max = max_value;
    4631                 :             :     }
    4632                 :       69812 :   r = int_range<1> (type, min, max);
    4633                 :       83730 : }
    4634                 :             : 
    4635                 :             : bool
    4636                 :       59197 : operator_abs::op1_range (irange &r, tree type,
    4637                 :             :                          const irange &lhs,
    4638                 :             :                          const irange &op2,
    4639                 :             :                          relation_trio) const
    4640                 :             : {
    4641                 :       59197 :   if (empty_range_varying (r, type, lhs, op2))
    4642                 :           0 :     return true;
    4643                 :       59197 :   if (TYPE_UNSIGNED (type))
    4644                 :             :     {
    4645                 :           0 :       r = lhs;
    4646                 :           0 :       return true;
    4647                 :             :     }
    4648                 :             :   // Start with the positives because negatives are an impossible result.
    4649                 :       59197 :   int_range_max positives = range_positives (type);
    4650                 :       59197 :   positives.intersect (lhs);
    4651                 :       59197 :   r = positives;
    4652                 :             :   // Then add the negative of each pair:
    4653                 :             :   // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
    4654                 :      119181 :   for (unsigned i = 0; i < positives.num_pairs (); ++i)
    4655                 :       59984 :     r.union_ (int_range<1> (type,
    4656                 :      119968 :                             -positives.upper_bound (i),
    4657                 :      179952 :                             -positives.lower_bound (i)));
    4658                 :             :   // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
    4659                 :             :   // unrepresentable.  Add -TYPE_MIN_VALUE in this case.
    4660                 :       59197 :   wide_int min_value = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
    4661                 :       59197 :   wide_int lb = lhs.lower_bound ();
    4662                 :       59197 :   if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lb, min_value))
    4663                 :         169 :     r.union_ (int_range<2> (type, lb, lb));
    4664                 :       59197 :   return true;
    4665                 :       59197 : }
    4666                 :             : 
    4667                 :             : void
    4668                 :       75625 : operator_abs::update_bitmask (irange &r, const irange &lh,
    4669                 :             :                               const irange &rh) const
    4670                 :             : {
    4671                 :       75625 :   update_known_bitmask (r, ABS_EXPR, lh, rh);
    4672                 :       75625 : }
    4673                 :             : 
    4674                 :             : class operator_absu : public range_operator
    4675                 :             : {
    4676                 :             :   using range_operator::update_bitmask;
    4677                 :             :  public:
    4678                 :             :   virtual void wi_fold (irange &r, tree type,
    4679                 :             :                         const wide_int &lh_lb, const wide_int &lh_ub,
    4680                 :             :                         const wide_int &rh_lb, const wide_int &rh_ub)
    4681                 :             :     const final override;
    4682                 :             :   virtual void update_bitmask (irange &r, const irange &lh,
    4683                 :             :                                const irange &rh) const final override;
    4684                 :             : } op_absu;
    4685                 :             : 
    4686                 :             : void
    4687                 :       17329 : operator_absu::wi_fold (irange &r, tree type,
    4688                 :             :                         const wide_int &lh_lb, const wide_int &lh_ub,
    4689                 :             :                         const wide_int &rh_lb ATTRIBUTE_UNUSED,
    4690                 :             :                         const wide_int &rh_ub ATTRIBUTE_UNUSED) const
    4691                 :             : {
    4692                 :       17329 :   wide_int new_lb, new_ub;
    4693                 :             : 
    4694                 :             :   // Pass through VR0 the easy cases.
    4695                 :       17329 :   if (wi::ges_p (lh_lb, 0))
    4696                 :             :     {
    4697                 :        2479 :       new_lb = lh_lb;
    4698                 :        2479 :       new_ub = lh_ub;
    4699                 :             :     }
    4700                 :             :   else
    4701                 :             :     {
    4702                 :       14850 :       new_lb = wi::abs (lh_lb);
    4703                 :       14850 :       new_ub = wi::abs (lh_ub);
    4704                 :             : 
    4705                 :             :       // If the range contains zero then we know that the minimum
    4706                 :             :       // value in the range will be zero.
    4707                 :       14850 :       if (wi::ges_p (lh_ub, 0))
    4708                 :             :         {
    4709                 :       11628 :           if (wi::gtu_p (new_lb, new_ub))
    4710                 :       10334 :             new_ub = new_lb;
    4711                 :       11628 :           new_lb = wi::zero (TYPE_PRECISION (type));
    4712                 :             :         }
    4713                 :             :       else
    4714                 :        3222 :         std::swap (new_lb, new_ub);
    4715                 :             :     }
    4716                 :             : 
    4717                 :       17329 :   gcc_checking_assert (TYPE_UNSIGNED (type));
    4718                 :       17329 :   r = int_range<1> (type, new_lb, new_ub);
    4719                 :       17329 : }
    4720                 :             : 
    4721                 :             : void
    4722                 :       15965 : operator_absu::update_bitmask (irange &r, const irange &lh,
    4723                 :             :                               const irange &rh) const
    4724                 :             : {
    4725                 :       15965 :   update_known_bitmask (r, ABSU_EXPR, lh, rh);
    4726                 :       15965 : }
    4727                 :             : 
    4728                 :             : 
    4729                 :             : bool
    4730                 :      601101 : operator_negate::fold_range (irange &r, tree type,
    4731                 :             :                              const irange &lh,
    4732                 :             :                              const irange &rh,
    4733                 :             :                              relation_trio) const
    4734                 :             : {
    4735                 :      601101 :   if (empty_range_varying (r, type, lh, rh))
    4736                 :         967 :     return true;
    4737                 :             : 
    4738                 :             : // -X is simply 0 - X.
    4739                 :      600134 :   int_range<1> zero;
    4740                 :      600134 :   zero.set_zero (type);
    4741                 :      600134 :   return range_op_handler (MINUS_EXPR).fold_range (r, type, zero, lh);
    4742                 :      600134 : }
    4743                 :             : 
    4744                 :             : bool
    4745                 :       74051 : operator_negate::op1_range (irange &r, tree type,
    4746                 :             :                             const irange &lhs,
    4747                 :             :                             const irange &op2,
    4748                 :             :                             relation_trio) const
    4749                 :             : {
    4750                 :             :   // NEGATE is involutory.
    4751                 :       74051 :   return fold_range (r, type, lhs, op2);
    4752                 :             : }
    4753                 :             : 
    4754                 :             : 
    4755                 :             : bool
    4756                 :           0 : operator_addr_expr::fold_range (irange &r, tree type,
    4757                 :             :                                 const irange &lh,
    4758                 :             :                                 const irange &rh,
    4759                 :             :                                 relation_trio) const
    4760                 :             : {
    4761                 :           0 :   if (empty_range_varying (r, type, lh, rh))
    4762                 :           0 :     return true;
    4763                 :             : 
    4764                 :             :   // Return a non-null pointer of the LHS type (passed in op2).
    4765                 :           0 :   if (lh.zero_p ())
    4766                 :           0 :     r.set_zero (type);
    4767                 :           0 :   else if (lh.undefined_p () || contains_zero_p (lh))
    4768                 :           0 :     r.set_varying (type);
    4769                 :             :   else
    4770                 :           0 :     r.set_nonzero (type);
    4771                 :             :   return true;
    4772                 :             : }
    4773                 :             : 
    4774                 :             : bool
    4775                 :           0 : operator_addr_expr::op1_range (irange &r, tree type,
    4776                 :             :                                const irange &lhs,
    4777                 :             :                                const irange &op2,
    4778                 :             :                                relation_trio) const
    4779                 :             : {
    4780                 :           0 :   if (empty_range_varying (r, type, lhs, op2))
    4781                 :           0 :     return true;
    4782                 :             : 
    4783                 :             :   // Return a non-null pointer of the LHS type (passed in op2), but only
    4784                 :             :   // if we cant overflow, eitherwise a no-zero offset could wrap to zero.
    4785                 :             :   // See PR 111009.
    4786                 :           0 :   if (!lhs.undefined_p () && !contains_zero_p (lhs) && TYPE_OVERFLOW_UNDEFINED (type))
    4787                 :           0 :     r.set_nonzero (type);
    4788                 :             :   else
    4789                 :           0 :     r.set_varying (type);
    4790                 :             :   return true;
    4791                 :             : }
    4792                 :             : 
    4793                 :             : // Initialize any integral operators to the primary table
    4794                 :             : 
    4795                 :             : void
    4796                 :      288902 : range_op_table::initialize_integral_ops ()
    4797                 :             : {
    4798                 :      288902 :   set (TRUNC_DIV_EXPR, op_trunc_div);
    4799                 :      288902 :   set (FLOOR_DIV_EXPR, op_floor_div);
    4800                 :      288902 :   set (ROUND_DIV_EXPR, op_round_div);
    4801                 :      288902 :   set (CEIL_DIV_EXPR, op_ceil_div);
    4802                 :      288902 :   set (EXACT_DIV_EXPR, op_exact_div);
    4803                 :      288902 :   set (LSHIFT_EXPR, op_lshift);
    4804                 :      288902 :   set (RSHIFT_EXPR, op_rshift);
    4805                 :      288902 :   set (TRUTH_AND_EXPR, op_logical_and);
    4806                 :      288902 :   set (TRUTH_OR_EXPR, op_logical_or);
    4807                 :      288902 :   set (TRUNC_MOD_EXPR, op_trunc_mod);
    4808                 :      288902 :   set (TRUTH_NOT_EXPR, op_logical_not);
    4809                 :      288902 :   set (IMAGPART_EXPR, op_unknown);
    4810                 :      288902 :   set (REALPART_EXPR, op_unknown);
    4811                 :      288902 :   set (ABSU_EXPR, op_absu);
    4812                 :      288902 :   set (OP_WIDEN_MULT_SIGNED, op_widen_mult_signed);
    4813                 :      288902 :   set (OP_WIDEN_MULT_UNSIGNED, op_widen_mult_unsigned);
    4814                 :      288902 :   set (OP_WIDEN_PLUS_SIGNED, op_widen_plus_signed);
    4815                 :      288902 :   set (OP_WIDEN_PLUS_UNSIGNED, op_widen_plus_unsigned);
    4816                 :             : 
    4817                 :      288902 : }
    4818                 :             : 
    4819                 :             : bool
    4820                 :       40877 : operator_plus::overflow_free_p (const irange &lh, const irange &rh,
    4821                 :             :                                 relation_trio) const
    4822                 :             : {
    4823                 :       40877 :   if (lh.undefined_p () || rh.undefined_p ())
    4824                 :             :     return false;
    4825                 :             : 
    4826                 :       40877 :   tree type = lh.type ();
    4827                 :       40877 :   if (TYPE_OVERFLOW_UNDEFINED (type))
    4828                 :             :     return true;
    4829                 :             : 
    4830                 :       10723 :   wi::overflow_type ovf;
    4831                 :       10723 :   signop sgn = TYPE_SIGN (type);
    4832                 :       10723 :   wide_int wmax0 = lh.upper_bound ();
    4833                 :       10723 :   wide_int wmax1 = rh.upper_bound ();
    4834                 :       10723 :   wi::add (wmax0, wmax1, sgn, &ovf);
    4835                 :       10723 :   if (ovf != wi::OVF_NONE)
    4836                 :             :     return false;
    4837                 :             : 
    4838                 :        1327 :   if (TYPE_UNSIGNED (type))
    4839                 :             :     return true;
    4840                 :             : 
    4841                 :         372 :   wide_int wmin0 = lh.lower_bound ();
    4842                 :         372 :   wide_int wmin1 = rh.lower_bound ();
    4843                 :         372 :   wi::add (wmin0, wmin1, sgn, &ovf);
    4844                 :         372 :   if (ovf != wi::OVF_NONE)
    4845                 :         261 :     return false;
    4846                 :             : 
    4847                 :             :   return true;
    4848                 :       11095 : }
    4849                 :             : 
    4850                 :             : bool
    4851                 :          31 : operator_minus::overflow_free_p (const irange &lh, const irange &rh,
    4852                 :             :                                  relation_trio) const
    4853                 :             : {
    4854                 :          31 :   if (lh.undefined_p () || rh.undefined_p ())
    4855                 :             :     return false;
    4856                 :             : 
    4857                 :          31 :   tree type = lh.type ();
    4858                 :          31 :   if (TYPE_OVERFLOW_UNDEFINED (type))
    4859                 :             :     return true;
    4860                 :             : 
    4861                 :          10 :   wi::overflow_type ovf;
    4862                 :          10 :   signop sgn = TYPE_SIGN (type);
    4863                 :          10 :   wide_int wmin0 = lh.lower_bound ();
    4864                 :          10 :   wide_int wmax1 = rh.upper_bound ();
    4865                 :          10 :   wi::sub (wmin0, wmax1, sgn, &ovf);
    4866                 :          10 :   if (ovf != wi::OVF_NONE)
    4867                 :             :     return false;
    4868                 :             : 
    4869                 :           7 :   if (TYPE_UNSIGNED (type))
    4870                 :             :     return true;
    4871                 :             : 
    4872                 :           6 :   wide_int wmax0 = lh.upper_bound ();
    4873                 :           6 :   wide_int wmin1 = rh.lower_bound ();
    4874                 :           6 :   wi::sub (wmax0, wmin1, sgn, &ovf);
    4875                 :           6 :   if (ovf != wi::OVF_NONE)
    4876                 :           0 :     return false;
    4877                 :             : 
    4878                 :             :   return true;
    4879                 :          16 : }
    4880                 :             : 
    4881                 :             : bool
    4882                 :        3322 : operator_mult::overflow_free_p (const irange &lh, const irange &rh,
    4883                 :             :                                 relation_trio) const
    4884                 :             : {
    4885                 :        3322 :   if (lh.undefined_p () || rh.undefined_p ())
    4886                 :             :     return false;
    4887                 :             : 
    4888                 :        3322 :   tree type = lh.type ();
    4889                 :        3322 :   if (TYPE_OVERFLOW_UNDEFINED (type))
    4890                 :             :     return true;
    4891                 :             : 
    4892                 :        2778 :   wi::overflow_type ovf;
    4893                 :        2778 :   signop sgn = TYPE_SIGN (type);
    4894                 :        2778 :   wide_int wmax0 = lh.upper_bound ();
    4895                 :        2778 :   wide_int wmax1 = rh.upper_bound ();
    4896                 :        2778 :   wi::mul (wmax0, wmax1, sgn, &ovf);
    4897                 :        2778 :   if (ovf != wi::OVF_NONE)
    4898                 :             :     return false;
    4899                 :             : 
    4900                 :         105 :   if (TYPE_UNSIGNED (type))
    4901                 :             :     return true;
    4902                 :             : 
    4903                 :          22 :   wide_int wmin0 = lh.lower_bound ();
    4904                 :          22 :   wide_int wmin1 = rh.lower_bound ();
    4905                 :          22 :   wi::mul (wmin0, wmin1, sgn, &ovf);
    4906                 :          22 :   if (ovf != wi::OVF_NONE)
    4907                 :             :     return false;
    4908                 :             : 
    4909                 :          22 :   wi::mul (wmin0, wmax1, sgn, &ovf);
    4910                 :          22 :   if (ovf != wi::OVF_NONE)
    4911                 :             :     return false;
    4912                 :             : 
    4913                 :          22 :   wi::mul (wmax0, wmin1, sgn, &ovf);
    4914                 :          22 :   if (ovf != wi::OVF_NONE)
    4915                 :             :     return false;
    4916                 :             : 
    4917                 :             :   return true;
    4918                 :        2800 : }
    4919                 :             : 
    4920                 :             : #if CHECKING_P
    4921                 :             : #include "selftest.h"
    4922                 :             : 
    4923                 :             : namespace selftest
    4924                 :             : {
    4925                 :             : #define INT(x) wi::shwi ((x), TYPE_PRECISION (integer_type_node))
    4926                 :             : #define UINT(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_type_node))
    4927                 :             : #define INT16(x) wi::shwi ((x), TYPE_PRECISION (short_integer_type_node))
    4928                 :             : #define UINT16(x) wi::uhwi ((x), TYPE_PRECISION (short_unsigned_type_node))
    4929                 :             : #define SCHAR(x) wi::shwi ((x), TYPE_PRECISION (signed_char_type_node))
    4930                 :             : #define UCHAR(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_char_type_node))
    4931                 :             : 
    4932                 :             : static void
    4933                 :           4 : range_op_cast_tests ()
    4934                 :             : {
    4935                 :           4 :   int_range<2> r0, r1, r2, rold;
    4936                 :           4 :   r0.set_varying (integer_type_node);
    4937                 :           4 :   wide_int maxint = r0.upper_bound ();
    4938                 :             : 
    4939                 :             :   // If a range is in any way outside of the range for the converted
    4940                 :             :   // to range, default to the range for the new type.
    4941                 :           4 :   r0.set_varying (short_integer_type_node);
    4942                 :           4 :   wide_int minshort = r0.lower_bound ();
    4943                 :           4 :   wide_int maxshort = r0.upper_bound ();
    4944                 :           4 :   if (TYPE_PRECISION (integer_type_node)
    4945                 :           4 :       > TYPE_PRECISION (short_integer_type_node))
    4946                 :             :     {
    4947                 :           8 :       r1 = int_range<1> (integer_type_node,
    4948                 :           4 :                          wi::zero (TYPE_PRECISION (integer_type_node)),
    4949                 :           8 :                          maxint);
    4950                 :           4 :       range_cast (r1, short_integer_type_node);
    4951                 :           4 :       ASSERT_TRUE (r1.lower_bound () == minshort
    4952                 :             :                    && r1.upper_bound() == maxshort);
    4953                 :             :     }
    4954                 :             : 
    4955                 :             :   // (unsigned char)[-5,-1] => [251,255].
    4956                 :           4 :   r0 = rold = int_range<1> (signed_char_type_node, SCHAR (-5), SCHAR (-1));
    4957                 :           4 :   range_cast (r0, unsigned_char_type_node);
    4958                 :           4 :   ASSERT_TRUE (r0 == int_range<1> (unsigned_char_type_node,
    4959                 :             :                                    UCHAR (251), UCHAR (255)));
    4960                 :           4 :   range_cast (r0, signed_char_type_node);
    4961                 :           4 :   ASSERT_TRUE (r0 == rold);
    4962                 :             : 
    4963                 :             :   // (signed char)[15, 150] => [-128,-106][15,127].
    4964                 :           4 :   r0 = rold = int_range<1> (unsigned_char_type_node, UCHAR (15), UCHAR (150));
    4965                 :           4 :   range_cast (r0, signed_char_type_node);
    4966                 :           4 :   r1 = int_range<1> (signed_char_type_node, SCHAR (15), SCHAR (127));
    4967                 :           4 :   r2 = int_range<1> (signed_char_type_node, SCHAR (-128), SCHAR (-106));
    4968                 :           4 :   r1.union_ (r2);
    4969                 :           4 :   ASSERT_TRUE (r1 == r0);
    4970                 :           4 :   range_cast (r0, unsigned_char_type_node);
    4971                 :           4 :   ASSERT_TRUE (r0 == rold);
    4972                 :             : 
    4973                 :             :   // (unsigned char)[-5, 5] => [0,5][251,255].
    4974                 :           4 :   r0 = rold = int_range<1> (signed_char_type_node, SCHAR (-5), SCHAR (5));
    4975                 :           4 :   range_cast (r0, unsigned_char_type_node);
    4976                 :           4 :   r1 = int_range<1> (unsigned_char_type_node, UCHAR (251), UCHAR (255));
    4977                 :           4 :   r2 = int_range<1> (unsigned_char_type_node, UCHAR (0), UCHAR (5));
    4978                 :           4 :   r1.union_ (r2);
    4979                 :           4 :   ASSERT_TRUE (r0 == r1);
    4980                 :           4 :   range_cast (r0, signed_char_type_node);
    4981                 :           4 :   ASSERT_TRUE (r0 == rold);
    4982                 :             : 
    4983                 :             :   // (unsigned char)[-5,5] => [0,5][251,255].
    4984                 :           4 :   r0 = int_range<1> (integer_type_node, INT (-5), INT (5));
    4985                 :           4 :   range_cast (r0, unsigned_char_type_node);
    4986                 :           4 :   r1 = int_range<1> (unsigned_char_type_node, UCHAR (0), UCHAR (5));
    4987                 :           4 :   r1.union_ (int_range<1> (unsigned_char_type_node, UCHAR (251), UCHAR (255)));
    4988                 :           4 :   ASSERT_TRUE (r0 == r1);
    4989                 :             : 
    4990                 :             :   // (unsigned char)[5U,1974U] => [0,255].
    4991                 :           4 :   r0 = int_range<1> (unsigned_type_node, UINT (5), UINT (1974));
    4992                 :           4 :   range_cast (r0, unsigned_char_type_node);
    4993                 :           4 :   ASSERT_TRUE (r0 == int_range<1> (unsigned_char_type_node, UCHAR (0), UCHAR (255)));
    4994                 :           4 :   range_cast (r0, integer_type_node);
    4995                 :             :   // Going to a wider range should not sign extend.
    4996                 :           4 :   ASSERT_TRUE (r0 == int_range<1> (integer_type_node, INT (0), INT (255)));
    4997                 :             : 
    4998                 :             :   // (unsigned char)[-350,15] => [0,255].
    4999                 :           4 :   r0 = int_range<1> (integer_type_node, INT (-350), INT (15));
    5000                 :           4 :   range_cast (r0, unsigned_char_type_node);
    5001                 :           4 :   ASSERT_TRUE (r0 == (int_range<1>
    5002                 :             :                       (unsigned_char_type_node,
    5003                 :             :                        min_limit (unsigned_char_type_node),
    5004                 :             :                        max_limit (unsigned_char_type_node))));
    5005                 :             : 
    5006                 :             :   // Casting [-120,20] from signed char to unsigned short.
    5007                 :             :   // => [0, 20][0xff88, 0xffff].
    5008                 :           4 :   r0 = int_range<1> (signed_char_type_node, SCHAR (-120), SCHAR (20));
    5009                 :           4 :   range_cast (r0, short_unsigned_type_node);
    5010                 :           4 :   r1 = int_range<1> (short_unsigned_type_node, UINT16 (0), UINT16 (20));
    5011                 :           8 :   r2 = int_range<1> (short_unsigned_type_node,
    5012                 :          12 :                      UINT16 (0xff88), UINT16 (0xffff));
    5013                 :           4 :   r1.union_ (r2);
    5014                 :           4 :   ASSERT_TRUE (r0 == r1);
    5015                 :             :   // A truncating cast back to signed char will work because [-120, 20]
    5016                 :             :   // is representable in signed char.
    5017                 :           4 :   range_cast (r0, signed_char_type_node);
    5018                 :           4 :   ASSERT_TRUE (r0 == int_range<1> (signed_char_type_node,
    5019                 :             :                                    SCHAR (-120), SCHAR (20)));
    5020                 :             : 
    5021                 :             :   // unsigned char -> signed short
    5022                 :             :   //    (signed short)[(unsigned char)25, (unsigned char)250]
    5023                 :             :   // => [(signed short)25, (signed short)250]
    5024                 :           4 :   r0 = rold = int_range<1> (unsigned_char_type_node, UCHAR (25), UCHAR (250));
    5025                 :           4 :   range_cast (r0, short_integer_type_node);
    5026                 :           4 :   r1 = int_range<1> (short_integer_type_node, INT16 (25), INT16 (250));
    5027                 :           4 :   ASSERT_TRUE (r0 == r1);
    5028                 :           4 :   range_cast (r0, unsigned_char_type_node);
    5029                 :           4 :   ASSERT_TRUE (r0 == rold);
    5030                 :             : 
    5031                 :             :   // Test casting a wider signed [-MIN,MAX] to a narrower unsigned.
    5032                 :           8 :   r0 = int_range<1> (long_long_integer_type_node,
    5033                 :           4 :                      min_limit (long_long_integer_type_node),
    5034                 :           8 :                      max_limit (long_long_integer_type_node));
    5035                 :           4 :   range_cast (r0, short_unsigned_type_node);
    5036                 :           8 :   r1 = int_range<1> (short_unsigned_type_node,
    5037                 :           4 :                      min_limit (short_unsigned_type_node),
    5038                 :           8 :                      max_limit (short_unsigned_type_node));
    5039                 :           4 :   ASSERT_TRUE (r0 == r1);
    5040                 :             : 
    5041                 :             :   // Casting NONZERO to a narrower type will wrap/overflow so
    5042                 :             :   // it's just the entire range for the narrower type.
    5043                 :             :   //
    5044                 :             :   // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32].  This is
    5045                 :             :   // is outside of the range of a smaller range, return the full
    5046                 :             :   // smaller range.
    5047                 :           4 :   if (TYPE_PRECISION (integer_type_node)
    5048                 :           4 :       > TYPE_PRECISION (short_integer_type_node))
    5049                 :             :     {
    5050                 :           4 :       r0.set_nonzero (integer_type_node);
    5051                 :           4 :       range_cast (r0, short_integer_type_node);
    5052                 :           8 :       r1 = int_range<1> (short_integer_type_node,
    5053                 :           4 :                          min_limit (short_integer_type_node),
    5054                 :           8 :                          max_limit (short_integer_type_node));
    5055                 :           4 :       ASSERT_TRUE (r0 == r1);
    5056                 :             :     }
    5057                 :             : 
    5058                 :             :   // Casting NONZERO from a narrower signed to a wider signed.
    5059                 :             :   //
    5060                 :             :   // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
    5061                 :             :   // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
    5062                 :           4 :   r0.set_nonzero (short_integer_type_node);
    5063                 :           4 :   range_cast (r0, integer_type_node);
    5064                 :           4 :   r1 = int_range<1> (integer_type_node, INT (-32768), INT (-1));
    5065                 :           4 :   r2 = int_range<1> (integer_type_node, INT (1), INT (32767));
    5066                 :           4 :   r1.union_ (r2);
    5067                 :           4 :   ASSERT_TRUE (r0 == r1);
    5068                 :           4 : }
    5069                 :             : 
    5070                 :             : static void
    5071                 :           4 : range_op_lshift_tests ()
    5072                 :             : {
    5073                 :             :   // Test that 0x808.... & 0x8.... still contains 0x8....
    5074                 :             :   // for a large set of numbers.
    5075                 :           4 :   {
    5076                 :           4 :     int_range_max res;
    5077                 :           4 :     tree big_type = long_long_unsigned_type_node;
    5078                 :           4 :     unsigned big_prec = TYPE_PRECISION (big_type);
    5079                 :             :     // big_num = 0x808,0000,0000,0000
    5080                 :           4 :     wide_int big_num = wi::lshift (wi::uhwi (0x808, big_prec),
    5081                 :           8 :                                    wi::uhwi (48, big_prec));
    5082                 :           8 :     op_bitwise_and.fold_range (res, big_type,
    5083                 :           8 :                                int_range <1> (big_type),
    5084                 :           8 :                                int_range <1> (big_type, big_num, big_num));
    5085                 :             :     // val = 0x8,0000,0000,0000
    5086                 :           4 :     wide_int val = wi::lshift (wi::uhwi (8, big_prec),
    5087                 :           8 :                                wi::uhwi (48, big_prec));
    5088                 :           4 :     ASSERT_TRUE (res.contains_p (val));
    5089                 :           4 :   }
    5090                 :             : 
    5091                 :           4 :   if (TYPE_PRECISION (unsigned_type_node) > 31)
    5092                 :             :     {
    5093                 :             :       // unsigned VARYING = op1 << 1 should be VARYING.
    5094                 :           4 :       int_range<2> lhs (unsigned_type_node);
    5095                 :           4 :       int_range<2> shift (unsigned_type_node, INT (1), INT (1));
    5096                 :           4 :       int_range_max op1;
    5097                 :           4 :       op_lshift.op1_range (op1, unsigned_type_node, lhs, shift);
    5098                 :           4 :       ASSERT_TRUE (op1.varying_p ());
    5099                 :             : 
    5100                 :             :       // 0 = op1 << 1  should be [0,0], [0x8000000, 0x8000000].
    5101                 :           4 :       int_range<2> zero (unsigned_type_node, UINT (0), UINT (0));
    5102                 :           4 :       op_lshift.op1_range (op1, unsigned_type_node, zero, shift);
    5103                 :           4 :       ASSERT_TRUE (op1.num_pairs () == 2);
    5104                 :             :       // Remove the [0,0] range.
    5105                 :           4 :       op1.intersect (zero);
    5106                 :           4 :       ASSERT_TRUE (op1.num_pairs () == 1);
    5107                 :             :       //  op1 << 1   should be [0x8000,0x8000] << 1,
    5108                 :             :       //  which should result in [0,0].
    5109                 :           4 :       int_range_max result;
    5110                 :           4 :       op_lshift.fold_range (result, unsigned_type_node, op1, shift);
    5111                 :           4 :       ASSERT_TRUE (result == zero);
    5112                 :           4 :     }
    5113                 :             :   // signed VARYING = op1 << 1 should be VARYING.
    5114                 :           4 :   if (TYPE_PRECISION (integer_type_node) > 31)
    5115                 :             :     {
    5116                 :             :       // unsigned VARYING = op1 << 1 should be VARYING.
    5117                 :           4 :       int_range<2> lhs (integer_type_node);
    5118                 :           4 :       int_range<2> shift (integer_type_node, INT (1), INT (1));
    5119                 :           4 :       int_range_max op1;
    5120                 :           4 :       op_lshift.op1_range (op1, integer_type_node, lhs, shift);
    5121                 :           4 :       ASSERT_TRUE (op1.varying_p ());
    5122                 :             : 
    5123                 :             :       //  0 = op1 << 1  should be [0,0], [0x8000000, 0x8000000].
    5124                 :           4 :       int_range<2> zero (integer_type_node, INT (0), INT (0));
    5125                 :           4 :       op_lshift.op1_range (op1, integer_type_node, zero, shift);
    5126                 :           4 :       ASSERT_TRUE (op1.num_pairs () == 2);
    5127                 :             :       // Remove the [0,0] range.
    5128                 :           4 :       op1.intersect (zero);
    5129                 :           4 :       ASSERT_TRUE (op1.num_pairs () == 1);
    5130                 :             :       //  op1 << 1   should be [0x8000,0x8000] << 1,
    5131                 :             :       //  which should result in [0,0].
    5132                 :           4 :       int_range_max result;
    5133                 :           4 :       op_lshift.fold_range (result, unsigned_type_node, op1, shift);
    5134                 :           4 :       ASSERT_TRUE (result == zero);
    5135                 :           4 :     }
    5136                 :           4 : }
    5137                 :             : 
    5138                 :             : static void
    5139                 :           4 : range_op_rshift_tests ()
    5140                 :             : {
    5141                 :             :   // unsigned: [3, MAX] = OP1 >> 1
    5142                 :           4 :   {
    5143                 :           4 :     int_range_max lhs (unsigned_type_node,
    5144                 :           4 :                        UINT (3), max_limit (unsigned_type_node));
    5145                 :           4 :     int_range_max one (unsigned_type_node,
    5146                 :           8 :                        wi::one (TYPE_PRECISION (unsigned_type_node)),
    5147                 :           8 :                        wi::one (TYPE_PRECISION (unsigned_type_node)));
    5148                 :           4 :     int_range_max op1;
    5149                 :           4 :     op_rshift.op1_range (op1, unsigned_type_node, lhs, one);
    5150                 :           4 :     ASSERT_FALSE (op1.contains_p (UINT (3)));
    5151                 :           4 :   }
    5152                 :             : 
    5153                 :             :   // signed: [3, MAX] = OP1 >> 1
    5154                 :           4 :   {
    5155                 :           4 :     int_range_max lhs (integer_type_node,
    5156                 :           4 :                        INT (3), max_limit (integer_type_node));
    5157                 :           4 :     int_range_max one (integer_type_node, INT (1), INT (1));
    5158                 :           4 :     int_range_max op1;
    5159                 :           4 :     op_rshift.op1_range (op1, integer_type_node, lhs, one);
    5160                 :           4 :     ASSERT_FALSE (op1.contains_p (INT (-2)));
    5161                 :           4 :   }
    5162                 :             : 
    5163                 :             :   // This is impossible, so OP1 should be [].
    5164                 :             :   // signed: [MIN, MIN] = OP1 >> 1
    5165                 :           4 :   {
    5166                 :           4 :     int_range_max lhs (integer_type_node,
    5167                 :           4 :                        min_limit (integer_type_node),
    5168                 :           4 :                        min_limit (integer_type_node));
    5169                 :           4 :     int_range_max one (integer_type_node, INT (1), INT (1));
    5170                 :           4 :     int_range_max op1;
    5171                 :           4 :     op_rshift.op1_range (op1, integer_type_node, lhs, one);
    5172                 :           4 :     ASSERT_TRUE (op1.undefined_p ());
    5173                 :           4 :   }
    5174                 :             : 
    5175                 :             :   // signed: ~[-1] = OP1 >> 31
    5176                 :           4 :   if (TYPE_PRECISION (integer_type_node) > 31)
    5177                 :             :     {
    5178                 :           4 :       int_range_max lhs (integer_type_node, INT (-1), INT (-1), VR_ANTI_RANGE);
    5179                 :           4 :       int_range_max shift (integer_type_node, INT (31), INT (31));
    5180                 :           4 :       int_range_max op1;
    5181                 :           4 :       op_rshift.op1_range (op1, integer_type_node, lhs, shift);
    5182                 :           4 :       int_range_max negatives = range_negatives (integer_type_node);
    5183                 :           4 :       negatives.intersect (op1);
    5184                 :           4 :       ASSERT_TRUE (negatives.undefined_p ());
    5185                 :           4 :     }
    5186                 :           4 : }
    5187                 :             : 
    5188                 :             : static void
    5189                 :           4 : range_op_bitwise_and_tests ()
    5190                 :             : {
    5191                 :           4 :   int_range_max res;
    5192                 :           4 :   wide_int min = min_limit (integer_type_node);
    5193                 :           4 :   wide_int max = max_limit (integer_type_node);
    5194                 :           4 :   wide_int tiny = wi::add (min, wi::one (TYPE_PRECISION (integer_type_node)));
    5195                 :           4 :   int_range_max i1 (integer_type_node, tiny, max);
    5196                 :           4 :   int_range_max i2 (integer_type_node, INT (255), INT (255));
    5197                 :             : 
    5198                 :             :   // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
    5199                 :           4 :   op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
    5200                 :           4 :   ASSERT_TRUE (res == int_range<1> (integer_type_node));
    5201                 :             : 
    5202                 :             :   // VARYING = OP1 & 255: OP1 is VARYING
    5203                 :           4 :   i1 = int_range<1> (integer_type_node);
    5204                 :           4 :   op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
    5205                 :           4 :   ASSERT_TRUE (res == int_range<1> (integer_type_node));
    5206                 :             : 
    5207                 :             :   // For 0 = x & MASK, x is ~MASK.
    5208                 :           4 :   {
    5209                 :           4 :     int_range<2> zero (integer_type_node, INT (0), INT (0));
    5210                 :           4 :     int_range<2> mask = int_range<2> (integer_type_node, INT (7), INT (7));
    5211                 :           4 :     op_bitwise_and.op1_range (res, integer_type_node, zero, mask);
    5212                 :           4 :     wide_int inv = wi::shwi (~7U, TYPE_PRECISION (integer_type_node));
    5213                 :           4 :     ASSERT_TRUE (res.get_nonzero_bits () == inv);
    5214                 :           4 :   }
    5215                 :             : 
    5216                 :             :   // (NONZERO | X) is nonzero.
    5217                 :           4 :   i1.set_nonzero (integer_type_node);
    5218                 :           4 :   i2.set_varying (integer_type_node);
    5219                 :           4 :   op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
    5220                 :           4 :   ASSERT_TRUE (res.nonzero_p ());
    5221                 :             : 
    5222                 :             :   // (NEGATIVE | X) is nonzero.
    5223                 :           4 :   i1 = int_range<1> (integer_type_node, INT (-5), INT (-3));
    5224                 :           4 :   i2.set_varying (integer_type_node);
    5225                 :           4 :   op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
    5226                 :           4 :   ASSERT_FALSE (res.contains_p (INT (0)));
    5227                 :           4 : }
    5228                 :             : 
    5229                 :             : static void
    5230                 :           4 : range_relational_tests ()
    5231                 :             : {
    5232                 :           4 :   int_range<2> lhs (unsigned_char_type_node);
    5233                 :           4 :   int_range<2> op1 (unsigned_char_type_node, UCHAR (8), UCHAR (10));
    5234                 :           4 :   int_range<2> op2 (unsigned_char_type_node, UCHAR (20), UCHAR (20));
    5235                 :             : 
    5236                 :             :   // Never wrapping additions mean LHS > OP1.
    5237                 :           4 :   relation_kind code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
    5238                 :           4 :   ASSERT_TRUE (code == VREL_GT);
    5239                 :             : 
    5240                 :             :   // Most wrapping additions mean nothing...
    5241                 :           4 :   op1 = int_range<2> (unsigned_char_type_node, UCHAR (8), UCHAR (10));
    5242                 :           4 :   op2 = int_range<2> (unsigned_char_type_node, UCHAR (0), UCHAR (255));
    5243                 :           4 :   code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
    5244                 :           4 :   ASSERT_TRUE (code == VREL_VARYING);
    5245                 :             : 
    5246                 :             :   // However, always wrapping additions mean LHS < OP1.
    5247                 :           4 :   op1 = int_range<2> (unsigned_char_type_node, UCHAR (1), UCHAR (255));
    5248                 :           4 :   op2 = int_range<2> (unsigned_char_type_node, UCHAR (255), UCHAR (255));
    5249                 :           4 :   code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
    5250                 :           4 :   ASSERT_TRUE (code == VREL_LT);
    5251                 :           4 : }
    5252                 :             : 
    5253                 :             : void
    5254                 :           4 : range_op_tests ()
    5255                 :             : {
    5256                 :           4 :   range_op_rshift_tests ();
    5257                 :           4 :   range_op_lshift_tests ();
    5258                 :           4 :   range_op_bitwise_and_tests ();
    5259                 :           4 :   range_op_cast_tests ();
    5260                 :           4 :   range_relational_tests ();
    5261                 :             : 
    5262                 :           4 :   extern void range_op_float_tests ();
    5263                 :           4 :   range_op_float_tests ();
    5264                 :           4 : }
    5265                 :             : 
    5266                 :             : } // namespace selftest
    5267                 :             : 
    5268                 :             : #endif // CHECKING_P
        

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