LCOV - code coverage report
Current view: top level - gcc - vr-values.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 97.1 % 1019 989
Test Date: 2024-04-20 14:03:02 Functions: 100.0 % 33 33
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Support routines for Value Range Propagation (VRP).
       2                 :             :    Copyright (C) 2005-2024 Free Software Foundation, Inc.
       3                 :             : 
       4                 :             : This file is part of GCC.
       5                 :             : 
       6                 :             : GCC is free software; you can redistribute it and/or modify
       7                 :             : it under the terms of the GNU General Public License as published by
       8                 :             : the Free Software Foundation; either version 3, or (at your option)
       9                 :             : any later version.
      10                 :             : 
      11                 :             : GCC is distributed in the hope that it will be useful,
      12                 :             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :             : GNU General Public License for more details.
      15                 :             : 
      16                 :             : You should have received a copy of the GNU General Public License
      17                 :             : along with GCC; see the file COPYING3.  If not see
      18                 :             : <http://www.gnu.org/licenses/>.  */
      19                 :             : 
      20                 :             : #include "config.h"
      21                 :             : #include "system.h"
      22                 :             : #include "coretypes.h"
      23                 :             : #include "backend.h"
      24                 :             : #include "insn-codes.h"
      25                 :             : #include "tree.h"
      26                 :             : #include "gimple.h"
      27                 :             : #include "ssa.h"
      28                 :             : #include "optabs-tree.h"
      29                 :             : #include "gimple-pretty-print.h"
      30                 :             : #include "diagnostic-core.h"
      31                 :             : #include "flags.h"
      32                 :             : #include "fold-const.h"
      33                 :             : #include "calls.h"
      34                 :             : #include "cfganal.h"
      35                 :             : #include "gimple-iterator.h"
      36                 :             : #include "gimple-fold.h"
      37                 :             : #include "tree-cfg.h"
      38                 :             : #include "tree-ssa-loop-niter.h"
      39                 :             : #include "tree-ssa-loop.h"
      40                 :             : #include "intl.h"
      41                 :             : #include "cfgloop.h"
      42                 :             : #include "tree-scalar-evolution.h"
      43                 :             : #include "tree-ssa-propagate.h"
      44                 :             : #include "tree-chrec.h"
      45                 :             : #include "omp-general.h"
      46                 :             : #include "case-cfn-macros.h"
      47                 :             : #include "alloc-pool.h"
      48                 :             : #include "attribs.h"
      49                 :             : #include "range.h"
      50                 :             : #include "vr-values.h"
      51                 :             : #include "cfghooks.h"
      52                 :             : #include "range-op.h"
      53                 :             : #include "gimple-range.h"
      54                 :             : 
      55                 :             : /* Return true if op is in a boolean [0, 1] value-range.  */
      56                 :             : 
      57                 :             : bool
      58                 :      477994 : simplify_using_ranges::op_with_boolean_value_range_p (tree op, gimple *s)
      59                 :             : {
      60                 :      477994 :   if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
      61                 :             :     return true;
      62                 :             : 
      63                 :      465620 :   if (integer_zerop (op)
      64                 :      465620 :       || integer_onep (op))
      65                 :        9143 :     return true;
      66                 :             : 
      67                 :      456477 :   if (TREE_CODE (op) != SSA_NAME)
      68                 :             :     return false;
      69                 :             : 
      70                 :             :   /* ?? Errr, this should probably check for [0,0] and [1,1] as well
      71                 :             :      as [0,1].  */
      72                 :      456477 :   value_range vr;
      73                 :      456477 :   return (query->range_of_expr (vr, op, s)
      74                 :      456477 :           && vr == range_true_and_false (TREE_TYPE (op)));
      75                 :      456477 : }
      76                 :             : 
      77                 :             : /* Helper function for simplify_internal_call_using_ranges and
      78                 :             :    extract_range_basic.  Return true if OP0 SUBCODE OP1 for
      79                 :             :    SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
      80                 :             :    always overflow.  Set *OVF to true if it is known to always
      81                 :             :    overflow.  */
      82                 :             : 
      83                 :             : static bool
      84                 :      142776 : check_for_binary_op_overflow (range_query *query,
      85                 :             :                               enum tree_code subcode, tree type,
      86                 :             :                               tree op0, tree op1, bool *ovf, gimple *s = NULL)
      87                 :             : {
      88                 :      142776 :   value_range vr0, vr1;
      89                 :      142776 :   if (!query->range_of_expr (vr0, op0, s) || vr0.undefined_p ())
      90                 :          12 :     vr0.set_varying (TREE_TYPE (op0));
      91                 :      142776 :   if (!query->range_of_expr (vr1, op1, s) || vr1.undefined_p ())
      92                 :           4 :     vr1.set_varying (TREE_TYPE (op1));
      93                 :             : 
      94                 :      142776 :   tree vr0min = wide_int_to_tree (TREE_TYPE (op0), vr0.lower_bound ());
      95                 :      142776 :   tree vr0max = wide_int_to_tree (TREE_TYPE (op0), vr0.upper_bound ());
      96                 :      142776 :   tree vr1min = wide_int_to_tree (TREE_TYPE (op1), vr1.lower_bound ());
      97                 :      142776 :   tree vr1max = wide_int_to_tree (TREE_TYPE (op1), vr1.upper_bound ());
      98                 :             : 
      99                 :      234010 :   *ovf = arith_overflowed_p (subcode, type, vr0min,
     100                 :             :                              subcode == MINUS_EXPR ? vr1max : vr1min);
     101                 :      234010 :   if (arith_overflowed_p (subcode, type, vr0max,
     102                 :      142776 :                           subcode == MINUS_EXPR ? vr1min : vr1max) != *ovf)
     103                 :             :     return false;
     104                 :       39243 :   if (subcode == MULT_EXPR)
     105                 :             :     {
     106                 :       23877 :       if (arith_overflowed_p (subcode, type, vr0min, vr1max) != *ovf
     107                 :       23877 :           || arith_overflowed_p (subcode, type, vr0max, vr1min) != *ovf)
     108                 :         388 :         return false;
     109                 :             :     }
     110                 :       38855 :   if (*ovf)
     111                 :             :     {
     112                 :             :       /* So far we found that there is an overflow on the boundaries.
     113                 :             :          That doesn't prove that there is an overflow even for all values
     114                 :             :          in between the boundaries.  For that compute widest2_int range
     115                 :             :          of the result and see if it doesn't overlap the range of
     116                 :             :          type.  */
     117                 :       36929 :       widest2_int wmin, wmax;
     118                 :      332399 :       widest2_int w[4];
     119                 :       36929 :       int i;
     120                 :       36929 :       signop sign0 = TYPE_SIGN (TREE_TYPE (op0));
     121                 :       36929 :       signop sign1 = TYPE_SIGN (TREE_TYPE (op1));
     122                 :       36943 :       w[0] = widest2_int::from (vr0.lower_bound (), sign0);
     123                 :       36943 :       w[1] = widest2_int::from (vr0.upper_bound (), sign0);
     124                 :       36934 :       w[2] = widest2_int::from (vr1.lower_bound (), sign1);
     125                 :       36934 :       w[3] = widest2_int::from (vr1.upper_bound (), sign1);
     126                 :      184645 :       for (i = 0; i < 4; i++)
     127                 :             :         {
     128                 :      147716 :           widest2_int wt;
     129                 :      147716 :           switch (subcode)
     130                 :             :             {
     131                 :       26740 :             case PLUS_EXPR:
     132                 :       26740 :               wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
     133                 :       26740 :               break;
     134                 :       27600 :             case MINUS_EXPR:
     135                 :       27600 :               wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
     136                 :       27600 :               break;
     137                 :       93376 :             case MULT_EXPR:
     138                 :       93376 :               wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
     139                 :       93376 :               break;
     140                 :           0 :             default:
     141                 :           0 :               gcc_unreachable ();
     142                 :             :             }
     143                 :      147716 :           if (i == 0)
     144                 :             :             {
     145                 :       36929 :               wmin = wt;
     146                 :       36929 :               wmax = wt;
     147                 :             :             }
     148                 :             :           else
     149                 :             :             {
     150                 :      110787 :               wmin = wi::smin (wmin, wt);
     151                 :      111443 :               wmax = wi::smax (wmax, wt);
     152                 :             :             }
     153                 :      147716 :         }
     154                 :             :       /* The result of op0 CODE op1 is known to be in range
     155                 :             :          [wmin, wmax].  */
     156                 :       36929 :       widest2_int wtmin
     157                 :       73858 :         = widest2_int::from (irange_val_min (type), TYPE_SIGN (type));
     158                 :       36929 :       widest2_int wtmax
     159                 :       73858 :         = widest2_int::from (irange_val_max (type), TYPE_SIGN (type));
     160                 :             :       /* If all values in [wmin, wmax] are smaller than
     161                 :             :          [wtmin, wtmax] or all are larger than [wtmin, wtmax],
     162                 :             :          the arithmetic operation will always overflow.  */
     163                 :       72849 :       if (wmax < wtmin || wmin > wtmax)
     164                 :        1651 :         return true;
     165                 :             :       return false;
     166                 :      221784 :     }
     167                 :             :   return true;
     168                 :      142776 : }
     169                 :             : 
     170                 :             : /* Set INIT, STEP, and DIRECTION to the corresponding values of NAME
     171                 :             :    within LOOP, and return TRUE.  Otherwise return FALSE, and set R to
     172                 :             :    the conservative range of NAME within the loop.  */
     173                 :             : 
     174                 :             : static bool
     175                 :     7584757 : get_scev_info (vrange &r, tree name, gimple *stmt, class loop *l,
     176                 :             :                tree &init, tree &step, enum ev_direction &dir)
     177                 :             : {
     178                 :     7584757 :   tree ev = analyze_scalar_evolution (l, name);
     179                 :     7584757 :   tree chrec = instantiate_parameters (l, ev);
     180                 :     7584757 :   tree type = TREE_TYPE (name);
     181                 :     7584757 :   if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
     182                 :             :     {
     183                 :     3624142 :       r.set_varying (type);
     184                 :     3624142 :       return false;
     185                 :             :     }
     186                 :     3960615 :   if (is_gimple_min_invariant (chrec))
     187                 :             :     {
     188                 :           0 :       if (is_gimple_constant (chrec))
     189                 :           0 :         r.set (chrec, chrec);
     190                 :             :       else
     191                 :           0 :         r.set_varying (type);
     192                 :           0 :       return false;
     193                 :             :     }
     194                 :             : 
     195                 :     3960615 :   init = initial_condition_in_loop_num (chrec, l->num);
     196                 :     3960615 :   step = evolution_part_in_loop_num (chrec, l->num);
     197                 :     3960615 :   if (!init || !step)
     198                 :             :     {
     199                 :           0 :       r.set_varying (type);
     200                 :           0 :       return false;
     201                 :             :     }
     202                 :     3960615 :   dir = scev_direction (chrec);
     203                 :     3960615 :   if (dir == EV_DIR_UNKNOWN
     204                 :     3960615 :       || scev_probably_wraps_p (NULL, init, step, stmt,
     205                 :             :                                 get_chrec_loop (chrec), true))
     206                 :             :     {
     207                 :      702900 :       r.set_varying (type);
     208                 :      702900 :       return false;
     209                 :             :     }
     210                 :             :   return true;
     211                 :             : }
     212                 :             : 
     213                 :             : /* Return TRUE if STEP * NIT may overflow when calculated in TYPE.  */
     214                 :             : 
     215                 :             : static bool
     216                 :     2794521 : induction_variable_may_overflow_p (tree type,
     217                 :             :                                    const wide_int &step, const widest_int &nit)
     218                 :             : {
     219                 :     2794521 :   wi::overflow_type ovf;
     220                 :     2794521 :   signop sign = TYPE_SIGN (type);
     221                 :     2794521 :   widest_int max_step = wi::mul (widest_int::from (step, sign),
     222                 :     2794521 :                                  nit, sign, &ovf);
     223                 :             : 
     224                 :     2794521 :   if (ovf || !wi::fits_to_tree_p (max_step, type))
     225                 :       24258 :     return true;
     226                 :             : 
     227                 :             :   /* For a signed type we have to check whether the result has the
     228                 :             :      expected signedness which is that of the step as number of
     229                 :             :      iterations is unsigned.  */
     230                 :     2770263 :   return (sign == SIGNED
     231                 :     5539819 :           && wi::gts_p (max_step, 0) != wi::gts_p (step, 0));
     232                 :     2794521 : }
     233                 :             : 
     234                 :             : /* Set R to the range from BEGIN to END, assuming the direction of the
     235                 :             :    loop is DIR.  */
     236                 :             : 
     237                 :             : static void
     238                 :     3257577 : range_from_loop_direction (irange &r, tree type,
     239                 :             :                            const irange &begin, const irange &end,
     240                 :             :                            ev_direction dir)
     241                 :             : {
     242                 :     3257577 :   signop sign = TYPE_SIGN (type);
     243                 :             : 
     244                 :     3257577 :   if (begin.undefined_p () || end.undefined_p ())
     245                 :        3930 :     r.set_varying (type);
     246                 :     3253647 :   else if (dir == EV_DIR_GROWS)
     247                 :             :     {
     248                 :     3030150 :       if (wi::gt_p (begin.lower_bound (), end.upper_bound (), sign))
     249                 :         306 :         r.set_varying (type);
     250                 :             :       else
     251                 :     3029844 :         r = int_range<1> (type, begin.lower_bound (), end.upper_bound ());
     252                 :             :     }
     253                 :             :   else
     254                 :             :     {
     255                 :      223513 :       if (wi::gt_p (end.lower_bound (), begin.upper_bound (), sign))
     256                 :         142 :         r.set_varying (type);
     257                 :             :       else
     258                 :      223371 :         r = int_range<1> (type, end.lower_bound (), begin.upper_bound ());
     259                 :             :     }
     260                 :     3257577 : }
     261                 :             : 
     262                 :             : /* Set V to the range of NAME in STMT within LOOP.  Return TRUE if a
     263                 :             :    range was found.  */
     264                 :             : 
     265                 :             : bool
     266                 :     7584757 : range_of_var_in_loop (vrange &v, tree name, class loop *l, gimple *stmt,
     267                 :             :                       range_query *query)
     268                 :             : {
     269                 :     7584757 :   tree init, step;
     270                 :     7584757 :   enum ev_direction dir;
     271                 :     7584757 :   if (!get_scev_info (v, name, stmt, l, init, step, dir))
     272                 :             :     return true;
     273                 :             : 
     274                 :             :   // Calculate ranges for the values from SCEV.
     275                 :     3257715 :   irange &r = as_a <irange> (v);
     276                 :     3257715 :   tree type = TREE_TYPE (init);
     277                 :     3257715 :   int_range<2> rinit (type), rstep (type), max_init (type);
     278                 :     3257715 :   if (!query->range_of_expr (rinit, init, stmt)
     279                 :     3257715 :       || !query->range_of_expr (rstep, step, stmt))
     280                 :           0 :     return false;
     281                 :             : 
     282                 :             :   // Calculate the final range of NAME if possible.
     283                 :     3257715 :   if (rinit.singleton_p () && rstep.singleton_p ())
     284                 :             :     {
     285                 :     2794659 :       widest_int nit;
     286                 :     2794659 :       if (!max_loop_iterations (l, &nit))
     287                 :             :         return false;
     288                 :             : 
     289                 :     2794521 :       if (!induction_variable_may_overflow_p (type, rstep.lower_bound (), nit))
     290                 :             :         {
     291                 :             :           // Calculate the max bounds for init (init + niter * step).
     292                 :     2769556 :           wide_int w = wide_int::from (nit, TYPE_PRECISION (type), TYPE_SIGN (type));
     293                 :     2769556 :           int_range<1> niter (type, w, w);
     294                 :     2769556 :           int_range_max max_step;
     295                 :     2769556 :           range_op_handler mult_handler (MULT_EXPR);
     296                 :     2769556 :           range_op_handler plus_handler (PLUS_EXPR);
     297                 :     2769556 :           if (!mult_handler.fold_range (max_step, type, niter, rstep)
     298                 :     2769556 :               || !plus_handler.fold_range (max_init, type, rinit, max_step))
     299                 :           0 :             return false;
     300                 :     2769556 :         }
     301                 :     2794659 :     }
     302                 :     3257577 :   range_from_loop_direction (r, type, rinit, max_init, dir);
     303                 :     3257577 :   return true;
     304                 :     3257715 : }
     305                 :             : 
     306                 :             : /* Helper function for vrp_evaluate_conditional_warnv & other
     307                 :             :    optimizers.  */
     308                 :             : 
     309                 :             : tree
     310                 :    17732878 : simplify_using_ranges::fold_cond_with_ops (enum tree_code code,
     311                 :             :                                            tree op0, tree op1, gimple *s)
     312                 :             : {
     313                 :    17732878 :   int_range_max r0, r1;
     314                 :    17732878 :   if (!query->range_of_expr (r0, op0, s)
     315                 :    17732878 :       || !query->range_of_expr (r1, op1, s))
     316                 :           0 :     return NULL_TREE;
     317                 :             : 
     318                 :    17732878 :   tree type = TREE_TYPE (op0);
     319                 :    17732878 :   int_range<1> res;
     320                 :    17732878 :   range_op_handler handler (code);
     321                 :    17732878 :   if (handler && handler.fold_range (res, type, r0, r1))
     322                 :             :     {
     323                 :    17732878 :       if (res == range_true (type))
     324                 :        5644 :         return boolean_true_node;
     325                 :    17727234 :       if (res == range_false (type))
     326                 :        2652 :         return boolean_false_node;
     327                 :             :     }
     328                 :             :   return NULL;
     329                 :    17732878 : }
     330                 :             : 
     331                 :             : /* Helper function for legacy_fold_cond.  */
     332                 :             : 
     333                 :             : tree
     334                 :    18179890 : simplify_using_ranges::legacy_fold_cond_overflow (gimple *stmt)
     335                 :             : {
     336                 :    18179890 :   tree ret;
     337                 :    18179890 :   tree_code code = gimple_cond_code (stmt);
     338                 :    18179890 :   tree op0 = gimple_cond_lhs (stmt);
     339                 :    18179890 :   tree op1 = gimple_cond_rhs (stmt);
     340                 :             : 
     341                 :             :   /* We only deal with integral and pointer types.  */
     342                 :    36004552 :   if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
     343                 :    22062370 :       && !POINTER_TYPE_P (TREE_TYPE (op0)))
     344                 :             :     return NULL_TREE;
     345                 :             : 
     346                 :             :   /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
     347                 :             :      as a simple equality test, then prefer that over its current form
     348                 :             :      for evaluation.
     349                 :             : 
     350                 :             :      An overflow test which collapses to an equality test can always be
     351                 :             :      expressed as a comparison of one argument against zero.  Overflow
     352                 :             :      occurs when the chosen argument is zero and does not occur if the
     353                 :             :      chosen argument is not zero.  */
     354                 :    17392517 :   tree x;
     355                 :    17392517 :   if (overflow_comparison_p (code, op0, op1, &x))
     356                 :             :     {
     357                 :        1159 :       wide_int max = wi::max_value (TYPE_PRECISION (TREE_TYPE (op0)), UNSIGNED);
     358                 :             :       /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
     359                 :             :          B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
     360                 :             :          B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
     361                 :             :          B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
     362                 :        1159 :       if (integer_zerop (x))
     363                 :             :         {
     364                 :         186 :           op1 = x;
     365                 :         186 :           code = (code == LT_EXPR || code == LE_EXPR) ? EQ_EXPR : NE_EXPR;
     366                 :             :         }
     367                 :             :       /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
     368                 :             :          B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
     369                 :             :          B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
     370                 :             :          B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
     371                 :         973 :       else if (wi::to_wide (x) == max - 1)
     372                 :             :         {
     373                 :         628 :           op0 = op1;
     374                 :         628 :           op1 = wide_int_to_tree (TREE_TYPE (op0), 0);
     375                 :         628 :           code = (code == GT_EXPR || code == GE_EXPR) ? EQ_EXPR : NE_EXPR;
     376                 :             :         }
     377                 :             :       else
     378                 :             :         {
     379                 :         345 :           value_range vro, vri;
     380                 :         345 :           tree type = TREE_TYPE (op0);
     381                 :         345 :           if (code == GT_EXPR || code == GE_EXPR)
     382                 :             :             {
     383                 :         120 :               vro.set (type,
     384                 :         240 :                        wi::to_wide (TYPE_MIN_VALUE (type)),
     385                 :         120 :                        wi::to_wide (x), VR_ANTI_RANGE);
     386                 :         120 :               vri.set (type,
     387                 :         240 :                        wi::to_wide (TYPE_MIN_VALUE (type)),
     388                 :         240 :                        wi::to_wide (x));
     389                 :             :             }
     390                 :         225 :           else if (code == LT_EXPR || code == LE_EXPR)
     391                 :             :             {
     392                 :         225 :               vro.set (type,
     393                 :         450 :                        wi::to_wide (TYPE_MIN_VALUE (type)),
     394                 :         225 :                        wi::to_wide (x));
     395                 :         225 :               vri.set (type,
     396                 :         450 :                        wi::to_wide (TYPE_MIN_VALUE (type)),
     397                 :         450 :                        wi::to_wide (x),
     398                 :             :                        VR_ANTI_RANGE);
     399                 :             :             }
     400                 :             :           else
     401                 :           0 :             gcc_unreachable ();
     402                 :         345 :           value_range vr0;
     403                 :         345 :           if (!query->range_of_expr (vr0, op0, stmt))
     404                 :           0 :             vr0.set_varying (TREE_TYPE (op0));
     405                 :             :           /* If vro, the range for OP0 to pass the overflow test, has
     406                 :             :              no intersection with *vr0, OP0's known range, then the
     407                 :             :              overflow test can't pass, so return the node for false.
     408                 :             :              If it is the inverted range, vri, that has no
     409                 :             :              intersection, then the overflow test must pass, so return
     410                 :             :              the node for true.  In other cases, we could proceed with
     411                 :             :              a simplified condition comparing OP0 and X, with LE_EXPR
     412                 :             :              for previously LE_ or LT_EXPR and GT_EXPR otherwise, but
     413                 :             :              the comments next to the enclosing if suggest it's not
     414                 :             :              generally profitable to do so.  */
     415                 :         345 :           vro.intersect (vr0);
     416                 :         345 :           if (vro.undefined_p ())
     417                 :           8 :             return boolean_false_node;
     418                 :         337 :           vri.intersect (vr0);
     419                 :         337 :           if (vri.undefined_p ())
     420                 :           0 :             return boolean_true_node;
     421                 :         345 :         }
     422                 :        1159 :     }
     423                 :             : 
     424                 :    17392509 :   if ((ret = fold_cond_with_ops (code, op0, op1, stmt)))
     425                 :             :     return ret;
     426                 :             :   return NULL_TREE;
     427                 :             : }
     428                 :             : 
     429                 :             : /* Visit conditional statement STMT.  If we can determine which edge
     430                 :             :    will be taken out of STMT's basic block, record it in
     431                 :             :    *TAKEN_EDGE_P.  Otherwise, set *TAKEN_EDGE_P to NULL.  */
     432                 :             : 
     433                 :             : void
     434                 :    18179890 : simplify_using_ranges::legacy_fold_cond (gcond *stmt, edge *taken_edge_p)
     435                 :             : {
     436                 :    18179890 :   tree val;
     437                 :             : 
     438                 :    18179890 :   *taken_edge_p = NULL;
     439                 :             : 
     440                 :    18179890 :   if (dump_file && (dump_flags & TDF_DETAILS))
     441                 :             :     {
     442                 :         340 :       tree use;
     443                 :         340 :       ssa_op_iter i;
     444                 :             : 
     445                 :         340 :       fprintf (dump_file, "\nVisiting conditional with predicate: ");
     446                 :         340 :       print_gimple_stmt (dump_file, stmt, 0);
     447                 :         340 :       fprintf (dump_file, "\nWith known ranges\n");
     448                 :             : 
     449                 :         769 :       FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
     450                 :             :         {
     451                 :         429 :           fprintf (dump_file, "\t");
     452                 :         429 :           print_generic_expr (dump_file, use);
     453                 :         429 :           fprintf (dump_file, ": ");
     454                 :         429 :           Value_Range r (TREE_TYPE (use));
     455                 :         429 :           query->range_of_expr (r, use, stmt);
     456                 :         429 :           r.dump (dump_file);
     457                 :         429 :         }
     458                 :             : 
     459                 :         340 :       fprintf (dump_file, "\n");
     460                 :             :     }
     461                 :             : 
     462                 :    18179890 :   val = legacy_fold_cond_overflow (stmt);
     463                 :    18179890 :   if (val)
     464                 :           8 :     *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
     465                 :             : 
     466                 :    18179890 :   if (dump_file && (dump_flags & TDF_DETAILS))
     467                 :             :     {
     468                 :         340 :       fprintf (dump_file, "\nPredicate evaluates to: ");
     469                 :         340 :       if (val == NULL_TREE)
     470                 :         340 :         fprintf (dump_file, "DON'T KNOW\n");
     471                 :             :       else
     472                 :           0 :         print_generic_stmt (dump_file, val);
     473                 :             :     }
     474                 :    18179890 : }
     475                 :             : 
     476                 :             : /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
     477                 :             :    used in range VR.  The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
     478                 :             :    MAX_IDX2.  If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
     479                 :             :    Returns true if the default label is not needed.  */
     480                 :             : 
     481                 :             : static bool
     482                 :       15717 : find_case_label_ranges (gswitch *stmt, const value_range *vr,
     483                 :             :                         size_t *min_idx1, size_t *max_idx1,
     484                 :             :                         size_t *min_idx2, size_t *max_idx2)
     485                 :             : {
     486                 :       15717 :   size_t i, j, k, l;
     487                 :       15717 :   unsigned int n = gimple_switch_num_labels (stmt);
     488                 :       15717 :   bool take_default;
     489                 :       15717 :   tree case_low, case_high;
     490                 :       15717 :   tree min, max;
     491                 :       15717 :   value_range_kind kind = get_legacy_range (*vr, min, max);
     492                 :             : 
     493                 :       15717 :   gcc_checking_assert (!vr->varying_p () && !vr->undefined_p ());
     494                 :             : 
     495                 :       15717 :   take_default = !find_case_label_range (stmt, min, max, &i, &j);
     496                 :             : 
     497                 :             :   /* Set second range to empty.  */
     498                 :       15717 :   *min_idx2 = 1;
     499                 :       15717 :   *max_idx2 = 0;
     500                 :             : 
     501                 :       15717 :   if (kind == VR_RANGE)
     502                 :             :     {
     503                 :       13497 :       *min_idx1 = i;
     504                 :       13497 :       *max_idx1 = j;
     505                 :       13497 :       return !take_default;
     506                 :             :     }
     507                 :             : 
     508                 :             :   /* Set first range to all case labels.  */
     509                 :        2220 :   *min_idx1 = 1;
     510                 :        2220 :   *max_idx1 = n - 1;
     511                 :             : 
     512                 :        2220 :   if (i > j)
     513                 :             :     return false;
     514                 :             : 
     515                 :             :   /* Make sure all the values of case labels [i , j] are contained in
     516                 :             :      range [MIN, MAX].  */
     517                 :         109 :   case_low = CASE_LOW (gimple_switch_label (stmt, i));
     518                 :         109 :   case_high = CASE_HIGH (gimple_switch_label (stmt, j));
     519                 :         109 :   if (tree_int_cst_compare (case_low, min) < 0)
     520                 :           4 :     i += 1;
     521                 :         109 :   if (case_high != NULL_TREE
     522                 :         109 :       && tree_int_cst_compare (max, case_high) < 0)
     523                 :           6 :     j -= 1;
     524                 :             : 
     525                 :         109 :   if (i > j)
     526                 :             :     return false;
     527                 :             : 
     528                 :             :   /* If the range spans case labels [i, j], the corresponding anti-range spans
     529                 :             :      the labels [1, i - 1] and [j + 1, n -  1].  */
     530                 :         102 :   k = j + 1;
     531                 :         102 :   l = n - 1;
     532                 :         102 :   if (k > l)
     533                 :             :     {
     534                 :          19 :       k = 1;
     535                 :          19 :       l = 0;
     536                 :             :     }
     537                 :             : 
     538                 :         102 :   j = i - 1;
     539                 :         102 :   i = 1;
     540                 :         102 :   if (i > j)
     541                 :             :     {
     542                 :          60 :       i = k;
     543                 :          60 :       j = l;
     544                 :          60 :       k = 1;
     545                 :          60 :       l = 0;
     546                 :             :     }
     547                 :             : 
     548                 :         102 :   *min_idx1 = i;
     549                 :         102 :   *max_idx1 = j;
     550                 :         102 :   *min_idx2 = k;
     551                 :         102 :   *max_idx2 = l;
     552                 :         102 :   return false;
     553                 :             : }
     554                 :             : 
     555                 :             : /* Simplify boolean operations if the source is known
     556                 :             :    to be already a boolean.  */
     557                 :             : bool
     558                 :      462231 : simplify_using_ranges::simplify_truth_ops_using_ranges
     559                 :             :                                         (gimple_stmt_iterator *gsi,
     560                 :             :                                          gimple *stmt)
     561                 :             : {
     562                 :      462231 :   enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
     563                 :      462231 :   tree lhs, op0, op1;
     564                 :      462231 :   bool need_conversion;
     565                 :             : 
     566                 :             :   /* We handle only !=/== case here.  */
     567                 :      462231 :   gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
     568                 :             : 
     569                 :      462231 :   op0 = gimple_assign_rhs1 (stmt);
     570                 :      462231 :   if (!op_with_boolean_value_range_p (op0, stmt))
     571                 :             :     return false;
     572                 :             : 
     573                 :       15763 :   op1 = gimple_assign_rhs2 (stmt);
     574                 :       15763 :   if (!op_with_boolean_value_range_p (op1, stmt))
     575                 :             :     return false;
     576                 :             : 
     577                 :             :   /* Reduce number of cases to handle to NE_EXPR.  As there is no
     578                 :             :      BIT_XNOR_EXPR we cannot replace A == B with a single statement.  */
     579                 :       15367 :   if (rhs_code == EQ_EXPR)
     580                 :             :     {
     581                 :        7264 :       if (TREE_CODE (op1) == INTEGER_CST)
     582                 :        2947 :         op1 = int_const_binop (BIT_XOR_EXPR, op1,
     583                 :        5894 :                                build_int_cst (TREE_TYPE (op1), 1));
     584                 :             :       else
     585                 :             :         return false;
     586                 :             :     }
     587                 :             : 
     588                 :       11050 :   lhs = gimple_assign_lhs (stmt);
     589                 :       11050 :   need_conversion
     590                 :       11050 :     = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
     591                 :             : 
     592                 :             :   /* Make sure to not sign-extend a 1-bit 1 when converting the result.  */
     593                 :       11050 :   if (need_conversion
     594                 :        9565 :       && !TYPE_UNSIGNED (TREE_TYPE (op0))
     595                 :        4390 :       && TYPE_PRECISION (TREE_TYPE (op0)) == 1
     596                 :       11079 :       && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
     597                 :             :     return false;
     598                 :             : 
     599                 :             :   /* For A != 0 we can substitute A itself.  */
     600                 :       11050 :   if (integer_zerop (op1))
     601                 :        6829 :     gimple_assign_set_rhs_with_ops (gsi,
     602                 :             :                                     need_conversion
     603                 :           0 :                                     ? NOP_EXPR : TREE_CODE (op0), op0);
     604                 :             :   /* For A != B we substitute A ^ B.  Either with conversion.  */
     605                 :        4221 :   else if (need_conversion)
     606                 :             :     {
     607                 :        2736 :       tree tem = make_ssa_name (TREE_TYPE (op0));
     608                 :        2736 :       gassign *newop
     609                 :        2736 :         = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
     610                 :        2736 :       gsi_insert_before (gsi, newop, GSI_SAME_STMT);
     611                 :        5449 :       if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
     612                 :        5449 :           && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
     613                 :             :         {
     614                 :        5286 :           value_range vr (TREE_TYPE (tem),
     615                 :        2643 :                           wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
     616                 :        5286 :                           wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
     617                 :        2643 :           set_range_info (tem, vr);
     618                 :        2643 :         }
     619                 :        2736 :       gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
     620                 :             :     }
     621                 :             :   /* Or without.  */
     622                 :             :   else
     623                 :        1485 :     gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
     624                 :       11050 :   update_stmt (gsi_stmt (*gsi));
     625                 :       11050 :   fold_stmt (gsi, follow_single_use_edges);
     626                 :             : 
     627                 :       11050 :   return true;
     628                 :             : }
     629                 :             : 
     630                 :             : /* Simplify a division or modulo operator to a right shift or bitwise and
     631                 :             :    if the first operand is unsigned or is greater than zero and the second
     632                 :             :    operand is an exact power of two.  For TRUNC_MOD_EXPR op0 % op1 with
     633                 :             :    constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
     634                 :             :    optimize it into just op0 if op0's range is known to be a subset of
     635                 :             :    [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
     636                 :             :    modulo.  */
     637                 :             : 
     638                 :             : bool
     639                 :      305822 : simplify_using_ranges::simplify_div_or_mod_using_ranges
     640                 :             :                                         (gimple_stmt_iterator *gsi,
     641                 :             :                                          gimple *stmt)
     642                 :             : {
     643                 :      305822 :   enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
     644                 :      305822 :   tree val = NULL;
     645                 :      305822 :   tree op0 = gimple_assign_rhs1 (stmt);
     646                 :      305822 :   tree op1 = gimple_assign_rhs2 (stmt);
     647                 :      305822 :   tree op0min = NULL_TREE, op0max = NULL_TREE;
     648                 :      305822 :   tree op1min = op1;
     649                 :      305822 :   value_range vr;
     650                 :             : 
     651                 :      305822 :   if (TREE_CODE (op0) == INTEGER_CST)
     652                 :             :     {
     653                 :             :       op0min = op0;
     654                 :             :       op0max = op0;
     655                 :             :     }
     656                 :             :   else
     657                 :             :     {
     658                 :      283008 :       if (!query->range_of_expr (vr, op0, stmt))
     659                 :           0 :         vr.set_varying (TREE_TYPE (op0));
     660                 :      283008 :       if (!vr.varying_p () && !vr.undefined_p ())
     661                 :             :         {
     662                 :      132518 :           tree type = vr.type ();
     663                 :      132518 :           op0min = wide_int_to_tree (type, vr.lower_bound ());
     664                 :      132527 :           op0max = wide_int_to_tree (type, vr.upper_bound ());
     665                 :             :         }
     666                 :             :     }
     667                 :             : 
     668                 :      305822 :   if (rhs_code == TRUNC_MOD_EXPR
     669                 :      135297 :       && TREE_CODE (op1) == SSA_NAME)
     670                 :             :     {
     671                 :       78768 :       value_range vr1;
     672                 :       78768 :       if (!query->range_of_expr (vr1, op1, stmt))
     673                 :           0 :         vr1.set_varying (TREE_TYPE (op1));
     674                 :       78768 :       if (!vr1.varying_p () && !vr1.undefined_p ())
     675                 :       23797 :         op1min = wide_int_to_tree (vr1.type (), vr1.lower_bound ());
     676                 :       78768 :     }
     677                 :      135297 :   if (rhs_code == TRUNC_MOD_EXPR
     678                 :      135297 :       && TREE_CODE (op1min) == INTEGER_CST
     679                 :       80322 :       && tree_int_cst_sgn (op1min) == 1
     680                 :       61012 :       && op0max
     681                 :       34987 :       && tree_int_cst_lt (op0max, op1min))
     682                 :             :     {
     683                 :        1030 :       if (TYPE_UNSIGNED (TREE_TYPE (op0))
     684                 :         350 :           || tree_int_cst_sgn (op0min) >= 0
     685                 :        1096 :           || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1min), op1min),
     686                 :             :                               op0min))
     687                 :             :         {
     688                 :             :           /* If op0 already has the range op0 % op1 has,
     689                 :             :              then TRUNC_MOD_EXPR won't change anything.  */
     690                 :         996 :           gimple_assign_set_rhs_from_tree (gsi, op0);
     691                 :         996 :           return true;
     692                 :             :         }
     693                 :             :     }
     694                 :             : 
     695                 :      304826 :   if (TREE_CODE (op0) != SSA_NAME)
     696                 :             :     return false;
     697                 :             : 
     698                 :      282019 :   if (!integer_pow2p (op1))
     699                 :             :     {
     700                 :             :       /* X % -Y can be only optimized into X % Y either if
     701                 :             :          X is not INT_MIN, or Y is not -1.  Fold it now, as after
     702                 :             :          remove_range_assertions the range info might be not available
     703                 :             :          anymore.  */
     704                 :      241740 :       if (rhs_code == TRUNC_MOD_EXPR
     705                 :      241740 :           && fold_stmt (gsi, follow_single_use_edges))
     706                 :             :         return true;
     707                 :      241740 :       return false;
     708                 :             :     }
     709                 :             : 
     710                 :       40279 :   if (TYPE_UNSIGNED (TREE_TYPE (op0)))
     711                 :        7863 :     val = integer_one_node;
     712                 :             :   else
     713                 :             :     {
     714                 :       32416 :       tree zero = build_zero_cst (TREE_TYPE (op0));
     715                 :       32416 :       val = fold_cond_with_ops (GE_EXPR, op0, zero, stmt);
     716                 :             :     }
     717                 :             : 
     718                 :       40279 :   if (val && integer_onep (val))
     719                 :             :     {
     720                 :       12958 :       tree t;
     721                 :             : 
     722                 :       12958 :       if (rhs_code == TRUNC_DIV_EXPR)
     723                 :             :         {
     724                 :       10685 :           t = build_int_cst (integer_type_node, tree_log2 (op1));
     725                 :       10685 :           gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
     726                 :       10685 :           gimple_assign_set_rhs1 (stmt, op0);
     727                 :       10685 :           gimple_assign_set_rhs2 (stmt, t);
     728                 :             :         }
     729                 :             :       else
     730                 :             :         {
     731                 :        2273 :           t = build_int_cst (TREE_TYPE (op1), 1);
     732                 :        2273 :           t = int_const_binop (MINUS_EXPR, op1, t);
     733                 :        2273 :           t = fold_convert (TREE_TYPE (op0), t);
     734                 :             : 
     735                 :        2273 :           gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
     736                 :        2273 :           gimple_assign_set_rhs1 (stmt, op0);
     737                 :        2273 :           gimple_assign_set_rhs2 (stmt, t);
     738                 :             :         }
     739                 :             : 
     740                 :       12958 :       update_stmt (stmt);
     741                 :       12958 :       fold_stmt (gsi, follow_single_use_edges);
     742                 :       12958 :       return true;
     743                 :             :     }
     744                 :             : 
     745                 :             :   return false;
     746                 :      305822 : }
     747                 :             : 
     748                 :             : /* Simplify a min or max if the ranges of the two operands are
     749                 :             :    disjoint.   Return true if we do simplify.  */
     750                 :             : 
     751                 :             : bool
     752                 :      149740 : simplify_using_ranges::simplify_min_or_max_using_ranges
     753                 :             :                                 (gimple_stmt_iterator *gsi,
     754                 :             :                                  gimple *stmt)
     755                 :             : {
     756                 :      149740 :   tree op0 = gimple_assign_rhs1 (stmt);
     757                 :      149740 :   tree op1 = gimple_assign_rhs2 (stmt);
     758                 :      149740 :   tree val;
     759                 :             : 
     760                 :      149740 :   val = fold_cond_with_ops (LE_EXPR, op0, op1, stmt);
     761                 :      149740 :   if (!val)
     762                 :      147423 :     val = fold_cond_with_ops (LT_EXPR, op0, op1, stmt);
     763                 :             : 
     764                 :      147423 :   if (val)
     765                 :             :     {
     766                 :             :       /* VAL == TRUE -> OP0 < or <= op1
     767                 :             :          VAL == FALSE -> OP0 > or >= op1.  */
     768                 :        2889 :       tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
     769                 :        2889 :                   == integer_zerop (val)) ? op0 : op1;
     770                 :        2889 :       gimple_assign_set_rhs_from_tree (gsi, res);
     771                 :        2889 :       return true;
     772                 :             :     }
     773                 :             : 
     774                 :             :   return false;
     775                 :             : }
     776                 :             : 
     777                 :             : /* If the operand to an ABS_EXPR is >= 0, then eliminate the
     778                 :             :    ABS_EXPR.  If the operand is <= 0, then simplify the
     779                 :             :    ABS_EXPR into a NEGATE_EXPR.  */
     780                 :             : 
     781                 :             : bool
     782                 :        5509 : simplify_using_ranges::simplify_abs_using_ranges (gimple_stmt_iterator *gsi,
     783                 :             :                                                   gimple *stmt)
     784                 :             : {
     785                 :        5509 :   tree op = gimple_assign_rhs1 (stmt);
     786                 :        5509 :   tree zero = build_zero_cst (TREE_TYPE (op));
     787                 :        5509 :   tree val = fold_cond_with_ops (LE_EXPR, op, zero, stmt);
     788                 :             : 
     789                 :        5509 :   if (!val)
     790                 :             :     {
     791                 :             :       /* The range is neither <= 0 nor > 0.  Now see if it is
     792                 :             :          either < 0 or >= 0.  */
     793                 :        5281 :       val = fold_cond_with_ops (LT_EXPR, op, zero, stmt);
     794                 :             :     }
     795                 :        5281 :   if (val)
     796                 :             :     {
     797                 :         312 :       gimple_assign_set_rhs1 (stmt, op);
     798                 :         312 :       if (integer_zerop (val))
     799                 :         137 :         gimple_assign_set_rhs_code (stmt, SSA_NAME);
     800                 :             :       else
     801                 :         175 :         gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
     802                 :         312 :       update_stmt (stmt);
     803                 :         312 :       fold_stmt (gsi, follow_single_use_edges);
     804                 :         312 :       return true;
     805                 :             :     }
     806                 :             :   return false;
     807                 :             : }
     808                 :             : 
     809                 :             : /* value_range wrapper for wi_set_zero_nonzero_bits.
     810                 :             : 
     811                 :             :    Return TRUE if VR was a constant range and we were able to compute
     812                 :             :    the bit masks.  */
     813                 :             : 
     814                 :             : static bool
     815                 :     1543282 : vr_set_zero_nonzero_bits (const tree expr_type,
     816                 :             :                           const irange *vr,
     817                 :             :                           wide_int *may_be_nonzero,
     818                 :             :                           wide_int *must_be_nonzero)
     819                 :             : {
     820                 :     1543282 :   if (vr->varying_p () || vr->undefined_p ())
     821                 :             :     {
     822                 :      918413 :       *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
     823                 :      918413 :       *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
     824                 :      918413 :       return false;
     825                 :             :     }
     826                 :      624869 :   wi_set_zero_nonzero_bits (expr_type, vr->lower_bound (), vr->upper_bound (),
     827                 :             :                             *may_be_nonzero, *must_be_nonzero);
     828                 :      624869 :   return true;
     829                 :             : }
     830                 :             : 
     831                 :             : /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
     832                 :             :    If all the bits that are being cleared by & are already
     833                 :             :    known to be zero from VR, or all the bits that are being
     834                 :             :    set by | are already known to be one from VR, the bit
     835                 :             :    operation is redundant.  */
     836                 :             : 
     837                 :             : bool
     838                 :     1215176 : simplify_using_ranges::simplify_bit_ops_using_ranges
     839                 :             :                                 (gimple_stmt_iterator *gsi,
     840                 :             :                                  gimple *stmt)
     841                 :             : {
     842                 :     1215176 :   tree op0 = gimple_assign_rhs1 (stmt);
     843                 :     1215176 :   tree op1 = gimple_assign_rhs2 (stmt);
     844                 :     1215176 :   tree op = NULL_TREE;
     845                 :     1215176 :   value_range vr0, vr1;
     846                 :     1215176 :   wide_int may_be_nonzero0, may_be_nonzero1;
     847                 :     1215176 :   wide_int must_be_nonzero0, must_be_nonzero1;
     848                 :     1215176 :   wide_int mask;
     849                 :             : 
     850                 :     1215176 :   if (!query->range_of_expr (vr0, op0, stmt)
     851                 :     1215176 :       || vr0.undefined_p ())
     852                 :             :     return false;
     853                 :     1214490 :   if (!query->range_of_expr (vr1, op1, stmt)
     854                 :     1214490 :       || vr1.undefined_p ())
     855                 :             :     return false;
     856                 :             : 
     857                 :     1214166 :   if (!vr_set_zero_nonzero_bits (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
     858                 :             :                                  &must_be_nonzero0))
     859                 :             :     return false;
     860                 :      329116 :   if (!vr_set_zero_nonzero_bits (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
     861                 :             :                                  &must_be_nonzero1))
     862                 :             :     return false;
     863                 :             : 
     864                 :      295753 :   switch (gimple_assign_rhs_code (stmt))
     865                 :             :     {
     866                 :      205636 :     case BIT_AND_EXPR:
     867                 :      205636 :       mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
     868                 :      205636 :       if (mask == 0)
     869                 :             :         {
     870                 :             :           op = op0;
     871                 :             :           break;
     872                 :             :         }
     873                 :      202920 :       mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
     874                 :      202920 :       if (mask == 0)
     875                 :             :         {
     876                 :             :           op = op1;
     877                 :             :           break;
     878                 :             :         }
     879                 :             :       break;
     880                 :       90117 :     case BIT_IOR_EXPR:
     881                 :       90117 :       mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
     882                 :       90117 :       if (mask == 0)
     883                 :             :         {
     884                 :             :           op = op1;
     885                 :             :           break;
     886                 :             :         }
     887                 :       90116 :       mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
     888                 :       90116 :       if (mask == 0)
     889                 :             :         {
     890                 :             :           op = op0;
     891                 :             :           break;
     892                 :             :         }
     893                 :             :       break;
     894                 :           0 :     default:
     895                 :           0 :       gcc_unreachable ();
     896                 :             :     }
     897                 :             : 
     898                 :        2930 :   if (op == NULL_TREE)
     899                 :      292823 :     return false;
     900                 :             : 
     901                 :        2930 :   gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
     902                 :        2930 :   update_stmt (gsi_stmt (*gsi));
     903                 :        2930 :   return true;
     904                 :     1215180 : }
     905                 :             : 
     906                 :             : /* We are comparing trees OP0 and OP1 using COND_CODE.  OP0 has
     907                 :             :    a known value range VR.
     908                 :             : 
     909                 :             :    If there is one and only one value which will satisfy the
     910                 :             :    conditional, then return that value.  Else return NULL.
     911                 :             : 
     912                 :             :    If signed overflow must be undefined for the value to satisfy
     913                 :             :    the conditional, then set *STRICT_OVERFLOW_P to true.  */
     914                 :             : 
     915                 :             : static tree
     916                 :     1231838 : test_for_singularity (enum tree_code cond_code, tree op0,
     917                 :             :                       tree op1, const value_range *vr)
     918                 :             : {
     919                 :     1231838 :   tree min = NULL;
     920                 :     1231838 :   tree max = NULL;
     921                 :             : 
     922                 :             :   /* Extract minimum/maximum values which satisfy the conditional as it was
     923                 :             :      written.  */
     924                 :     1231838 :   if (cond_code == LE_EXPR || cond_code == LT_EXPR)
     925                 :             :     {
     926                 :      542443 :       min = TYPE_MIN_VALUE (TREE_TYPE (op0));
     927                 :             : 
     928                 :      542443 :       max = op1;
     929                 :      542443 :       if (cond_code == LT_EXPR)
     930                 :             :         {
     931                 :       91592 :           tree one = build_int_cst (TREE_TYPE (op0), 1);
     932                 :       91592 :           max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
     933                 :             :           /* Signal to compare_values_warnv this expr doesn't overflow.  */
     934                 :       91592 :           if (EXPR_P (max))
     935                 :           0 :             suppress_warning (max, OPT_Woverflow);
     936                 :             :         }
     937                 :             :     }
     938                 :      689395 :   else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
     939                 :             :     {
     940                 :      597119 :       max = TYPE_MAX_VALUE (TREE_TYPE (op0));
     941                 :             : 
     942                 :      597119 :       min = op1;
     943                 :      597119 :       if (cond_code == GT_EXPR)
     944                 :             :         {
     945                 :      506700 :           tree one = build_int_cst (TREE_TYPE (op0), 1);
     946                 :      506700 :           min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
     947                 :             :           /* Signal to compare_values_warnv this expr doesn't overflow.  */
     948                 :      506700 :           if (EXPR_P (min))
     949                 :           0 :             suppress_warning (min, OPT_Woverflow);
     950                 :             :         }
     951                 :             :     }
     952                 :             : 
     953                 :             :   /* Now refine the minimum and maximum values using any
     954                 :             :      value range information we have for op0.  */
     955                 :     1231838 :   if (min && max)
     956                 :             :     {
     957                 :     1139562 :       tree type = TREE_TYPE (op0);
     958                 :     1139562 :       tree tmin = wide_int_to_tree (type, vr->lower_bound ());
     959                 :     1139562 :       tree tmax = wide_int_to_tree (type, vr->upper_bound ());
     960                 :     1139562 :       if (compare_values (tmin, min) == 1)
     961                 :      351126 :         min = tmin;
     962                 :     1139562 :       if (compare_values (tmax, max) == -1)
     963                 :      387190 :         max = tmax;
     964                 :             : 
     965                 :             :       /* If the new min/max values have converged to a single value,
     966                 :             :          then there is only one value which can satisfy the condition,
     967                 :             :          return that value.  */
     968                 :     1139562 :       if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
     969                 :             :         return min;
     970                 :             :     }
     971                 :             :   return NULL;
     972                 :             : }
     973                 :             : 
     974                 :             : /* Return whether the value range *VR fits in an integer type specified
     975                 :             :    by PRECISION and UNSIGNED_P.  */
     976                 :             : 
     977                 :             : bool
     978                 :      204122 : range_fits_type_p (const irange *vr,
     979                 :             :                    unsigned dest_precision, signop dest_sgn)
     980                 :             : {
     981                 :      204122 :   tree src_type;
     982                 :      204122 :   unsigned src_precision;
     983                 :      204122 :   widest_int tem;
     984                 :      204122 :   signop src_sgn;
     985                 :             : 
     986                 :             :   /* We can only handle integral and pointer types.  */
     987                 :      204122 :   src_type = vr->type ();
     988                 :      204122 :   if (!INTEGRAL_TYPE_P (src_type)
     989                 :           0 :       && !POINTER_TYPE_P (src_type))
     990                 :             :     return false;
     991                 :             : 
     992                 :             :   /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
     993                 :             :      and so is an identity transform.  */
     994                 :      204122 :   src_precision = TYPE_PRECISION (vr->type ());
     995                 :      204122 :   src_sgn = TYPE_SIGN (src_type);
     996                 :      204122 :   if ((src_precision < dest_precision
     997                 :        6342 :        && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
     998                 :      199097 :       || (src_precision == dest_precision && src_sgn == dest_sgn))
     999                 :             :     return true;
    1000                 :             : 
    1001                 :             :   /* Now we can only handle ranges with constant bounds.  */
    1002                 :      196347 :   if (vr->undefined_p () || vr->varying_p ())
    1003                 :             :     return false;
    1004                 :             : 
    1005                 :      154056 :   wide_int vrmin = vr->lower_bound ();
    1006                 :      154056 :   wide_int vrmax = vr->upper_bound ();
    1007                 :             : 
    1008                 :             :   /* For sign changes, the MSB of the wide_int has to be clear.
    1009                 :             :      An unsigned value with its MSB set cannot be represented by
    1010                 :             :      a signed wide_int, while a negative value cannot be represented
    1011                 :             :      by an unsigned wide_int.  */
    1012                 :      154056 :   if (src_sgn != dest_sgn
    1013                 :      154056 :       && (wi::lts_p (vrmin, 0) || wi::lts_p (vrmax, 0)))
    1014                 :       53825 :     return false;
    1015                 :             : 
    1016                 :             :   /* Then we can perform the conversion on both ends and compare
    1017                 :             :      the result for equality.  */
    1018                 :      100231 :   signop sign = TYPE_SIGN (vr->type ());
    1019                 :      100231 :   tem = wi::ext (widest_int::from (vrmin, sign), dest_precision, dest_sgn);
    1020                 :      100231 :   if (tem != widest_int::from (vrmin, sign))
    1021                 :             :     return false;
    1022                 :       95539 :   tem = wi::ext (widest_int::from (vrmax, sign), dest_precision, dest_sgn);
    1023                 :       95539 :   if (tem != widest_int::from (vrmax, sign))
    1024                 :             :     return false;
    1025                 :             : 
    1026                 :             :   return true;
    1027                 :      154056 : }
    1028                 :             : 
    1029                 :             : // Clear edge E of EDGE_EXECUTABLE (it is unexecutable). If it wasn't
    1030                 :             : // previously clear, propagate to successor blocks if appropriate.
    1031                 :             : 
    1032                 :             : void
    1033                 :      218800 : simplify_using_ranges::set_and_propagate_unexecutable (edge e)
    1034                 :             : {
    1035                 :             :   // If not_executable is already set, we're done.
    1036                 :             :   // This works in the absence of a flag as well.
    1037                 :      218800 :   if ((e->flags & m_not_executable_flag) == m_not_executable_flag)
    1038                 :      133056 :     return;
    1039                 :             : 
    1040                 :      183476 :   e->flags |= m_not_executable_flag;
    1041                 :      183476 :   m_flag_set_edges.safe_push (e);
    1042                 :             : 
    1043                 :             :   // Check if the destination block needs to propagate the property.
    1044                 :      183476 :   basic_block bb = e->dest;
    1045                 :             : 
    1046                 :             :   // If any incoming edge is executable, we are done.
    1047                 :      183476 :   edge_iterator ei;
    1048                 :      303679 :   FOR_EACH_EDGE (e, ei, bb->preds)
    1049                 :      217935 :     if ((e->flags & m_not_executable_flag) == 0)
    1050                 :             :       return;
    1051                 :             : 
    1052                 :             :   // This block is also unexecutable, propagate to all exit edges as well.
    1053                 :      151717 :   FOR_EACH_EDGE (e, ei, bb->succs)
    1054                 :       65973 :     set_and_propagate_unexecutable (e);
    1055                 :             : }
    1056                 :             : 
    1057                 :             : /* If COND can be folded entirely as TRUE or FALSE, rewrite the
    1058                 :             :    conditional as such, and return TRUE.  */
    1059                 :             : 
    1060                 :             : bool
    1061                 :    18395061 : simplify_using_ranges::fold_cond (gcond *cond)
    1062                 :             : {
    1063                 :    18395061 :   int_range_max r;
    1064                 :    18395061 :   if (query->range_of_stmt (r, cond) && r.singleton_p ())
    1065                 :             :     {
    1066                 :             :       // COND has already been folded if arguments are constant.
    1067                 :      215171 :       if (TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME
    1068                 :      215171 :           && TREE_CODE (gimple_cond_rhs (cond)) != SSA_NAME)
    1069                 :             :         return false;
    1070                 :      146192 :       if (dump_file)
    1071                 :             :         {
    1072                 :         169 :           fprintf (dump_file, "Folding predicate ");
    1073                 :         169 :           print_gimple_expr (dump_file, cond, 0);
    1074                 :         169 :           fprintf (dump_file, " to ");
    1075                 :             :         }
    1076                 :      146192 :       edge e0 = EDGE_SUCC (gimple_bb (cond), 0);
    1077                 :      146192 :       edge e1 = EDGE_SUCC (gimple_bb (cond), 1);
    1078                 :      146192 :       if (r.zero_p ())
    1079                 :             :         {
    1080                 :       68388 :           if (dump_file)
    1081                 :         116 :             fprintf (dump_file, "0\n");
    1082                 :       68388 :           gimple_cond_make_false (cond);
    1083                 :       68388 :           if (e0->flags & EDGE_TRUE_VALUE)
    1084                 :       66460 :             set_and_propagate_unexecutable (e0);
    1085                 :             :           else
    1086                 :        1928 :             set_and_propagate_unexecutable (e1);
    1087                 :             :         }
    1088                 :             :       else
    1089                 :             :         {
    1090                 :       77804 :           if (dump_file)
    1091                 :          53 :             fprintf (dump_file, "1\n");
    1092                 :       77804 :           gimple_cond_make_true (cond);
    1093                 :       77804 :           if (e0->flags & EDGE_FALSE_VALUE)
    1094                 :         540 :             set_and_propagate_unexecutable (e0);
    1095                 :             :           else
    1096                 :       77264 :             set_and_propagate_unexecutable (e1);
    1097                 :             :         }
    1098                 :      146192 :       update_stmt (cond);
    1099                 :      146192 :       return true;
    1100                 :             :     }
    1101                 :             : 
    1102                 :             :   // FIXME: Audit the code below and make sure it never finds anything.
    1103                 :    18179890 :   edge taken_edge;
    1104                 :    18179890 :   legacy_fold_cond (cond, &taken_edge);
    1105                 :             : 
    1106                 :    18179890 :   if (taken_edge)
    1107                 :             :     {
    1108                 :           8 :       if (taken_edge->flags & EDGE_TRUE_VALUE)
    1109                 :             :         {
    1110                 :           0 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1111                 :           0 :             fprintf (dump_file, "\nVRP Predicate evaluates to: 1\n");
    1112                 :           0 :           gimple_cond_make_true (cond);
    1113                 :             :         }
    1114                 :           8 :       else if (taken_edge->flags & EDGE_FALSE_VALUE)
    1115                 :             :         {
    1116                 :           8 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1117                 :           0 :             fprintf (dump_file, "\nVRP Predicate evaluates to: 0\n");
    1118                 :           8 :           gimple_cond_make_false (cond);
    1119                 :             :         }
    1120                 :             :       else
    1121                 :           0 :        gcc_unreachable ();
    1122                 :           8 :       update_stmt (cond);
    1123                 :           8 :       return true;
    1124                 :             :     }
    1125                 :             :   return false;
    1126                 :    18395061 : }
    1127                 :             : 
    1128                 :             : /* Simplify a conditional using a relational operator to an equality
    1129                 :             :    test if the range information indicates only one value can satisfy
    1130                 :             :    the original conditional.  */
    1131                 :             : 
    1132                 :             : bool
    1133                 :    10707128 : simplify_using_ranges::simplify_cond_using_ranges_1 (gcond *stmt)
    1134                 :             : {
    1135                 :    10707128 :   tree op0 = gimple_cond_lhs (stmt);
    1136                 :    10707128 :   tree op1 = gimple_cond_rhs (stmt);
    1137                 :    10707128 :   enum tree_code cond_code = gimple_cond_code (stmt);
    1138                 :             : 
    1139                 :    10707128 :   if (fold_cond (stmt))
    1140                 :             :     return true;
    1141                 :             : 
    1142                 :    10595822 :   if (simplify_compare_using_ranges_1 (cond_code, op0, op1, stmt))
    1143                 :             :     {
    1144                 :      295337 :       if (dump_file)
    1145                 :             :         {
    1146                 :          21 :           fprintf (dump_file, "Simplified relational ");
    1147                 :          21 :           print_gimple_stmt (dump_file, stmt, 0);
    1148                 :          21 :           fprintf (dump_file, " into ");
    1149                 :             :         }
    1150                 :             : 
    1151                 :      295337 :       gimple_cond_set_code (stmt, cond_code);
    1152                 :      295337 :       gimple_cond_set_lhs (stmt, op0);
    1153                 :      295337 :       gimple_cond_set_rhs (stmt, op1);
    1154                 :             : 
    1155                 :      295337 :       update_stmt (stmt);
    1156                 :             : 
    1157                 :      295337 :        if (dump_file)
    1158                 :             :         {
    1159                 :          21 :           print_gimple_stmt (dump_file, stmt, 0);
    1160                 :          21 :           fprintf (dump_file, "\n");
    1161                 :             :         }
    1162                 :      295337 :       return true;
    1163                 :             :     }
    1164                 :             :   return false;
    1165                 :             : }
    1166                 :             : 
    1167                 :             : /* Like simplify_cond_using_ranges_1 but for assignments rather
    1168                 :             :    than GIMPLE_COND. */
    1169                 :             : 
    1170                 :             : bool
    1171                 :     1192131 : simplify_using_ranges::simplify_compare_assign_using_ranges_1
    1172                 :             :                                         (gimple_stmt_iterator *gsi,
    1173                 :             :                                          gimple *stmt)
    1174                 :             : {
    1175                 :     1192131 :   enum tree_code code = gimple_assign_rhs_code (stmt);
    1176                 :     1192131 :   tree op0 = gimple_assign_rhs1 (stmt);
    1177                 :     1192131 :   tree op1 = gimple_assign_rhs2 (stmt);
    1178                 :     1192131 :   gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
    1179                 :     1192131 :   bool happened = false;
    1180                 :             : 
    1181                 :     1192131 :   if (simplify_compare_using_ranges_1 (code, op0, op1, stmt))
    1182                 :             :     {
    1183                 :        6744 :       if (dump_file)
    1184                 :             :         {
    1185                 :           1 :           fprintf (dump_file, "Simplified relational ");
    1186                 :           1 :           print_gimple_stmt (dump_file, stmt, 0);
    1187                 :           1 :           fprintf (dump_file, " into ");
    1188                 :             :         }
    1189                 :             : 
    1190                 :        6744 :       gimple_assign_set_rhs_code (stmt, code);
    1191                 :        6744 :       gimple_assign_set_rhs1 (stmt, op0);
    1192                 :        6744 :       gimple_assign_set_rhs2 (stmt, op1);
    1193                 :             : 
    1194                 :        6744 :       update_stmt (stmt);
    1195                 :             : 
    1196                 :        6744 :        if (dump_file)
    1197                 :             :         {
    1198                 :           1 :           print_gimple_stmt (dump_file, stmt, 0);
    1199                 :           1 :           fprintf (dump_file, "\n");
    1200                 :             :         }
    1201                 :             :       happened = true;
    1202                 :             :     }
    1203                 :             : 
    1204                 :             :   /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
    1205                 :             :      if the RHS is zero or one, and the LHS are known to be boolean
    1206                 :             :      values.  */
    1207                 :     1192131 :   if ((code == EQ_EXPR || code == NE_EXPR)
    1208                 :      648987 :       && INTEGRAL_TYPE_P (TREE_TYPE (op0))
    1209                 :     1654362 :       && simplify_truth_ops_using_ranges (gsi, stmt))
    1210                 :             :     happened = true;
    1211                 :             : 
    1212                 :     1192131 :   return happened;
    1213                 :             : }
    1214                 :             : 
    1215                 :             : /* Try to simplify OP0 COND_CODE OP1 using a relational operator to an
    1216                 :             :    equality test if the range information indicates only one value can
    1217                 :             :    satisfy the original conditional.   */
    1218                 :             : 
    1219                 :             : bool
    1220                 :    11787953 : simplify_using_ranges::simplify_compare_using_ranges_1 (tree_code &cond_code, tree &op0, tree &op1, gimple *stmt)
    1221                 :             : {
    1222                 :    11787953 :   bool happened = false;
    1223                 :    11787953 :   if (cond_code != NE_EXPR
    1224                 :    11787953 :       && cond_code != EQ_EXPR
    1225                 :     2835192 :       && TREE_CODE (op0) == SSA_NAME
    1226                 :     2831414 :       && INTEGRAL_TYPE_P (TREE_TYPE (op0))
    1227                 :    14329573 :       && is_gimple_min_invariant (op1))
    1228                 :             :     {
    1229                 :     1453649 :       value_range vr;
    1230                 :             : 
    1231                 :     1453649 :       if (!query->range_of_expr (vr, op0, stmt))
    1232                 :           0 :         vr.set_undefined ();
    1233                 :             : 
    1234                 :             :       /* If we have range information for OP0, then we might be
    1235                 :             :          able to simplify this conditional. */
    1236                 :     1453649 :       if (!vr.undefined_p () && !vr.varying_p ())
    1237                 :             :         {
    1238                 :      615919 :           tree new_tree = test_for_singularity (cond_code, op0, op1, &vr);
    1239                 :      615919 :           if (new_tree)
    1240                 :             :             {
    1241                 :       92276 :               cond_code = EQ_EXPR;
    1242                 :       92276 :               op1 = new_tree;
    1243                 :       92276 :               happened = true;
    1244                 :             :             }
    1245                 :             : 
    1246                 :             :           /* Try again after inverting the condition.  We only deal
    1247                 :             :              with integral types here, so no need to worry about
    1248                 :             :              issues with inverting FP comparisons.  */
    1249                 :      615919 :           new_tree = test_for_singularity
    1250                 :      615919 :                        (invert_tree_comparison (cond_code, false),
    1251                 :             :                         op0, op1, &vr);
    1252                 :      615919 :           if (new_tree)
    1253                 :             :             {
    1254                 :      150740 :               cond_code = NE_EXPR;
    1255                 :      150740 :               op1 = new_tree;
    1256                 :      150740 :               happened = true;
    1257                 :             :             }
    1258                 :             :         }
    1259                 :     1453649 :     }
    1260                 :             :   // Try to simplify casted conditions.
    1261                 :    11787953 :   if (simplify_casted_compare (cond_code, op0, op1))
    1262                 :       65643 :     happened = true;
    1263                 :    11787953 :   return happened;
    1264                 :             : }
    1265                 :             : 
    1266                 :             : /* Simplify OP0 code OP1 when OP1 is a constant and OP0 was a SSA_NAME
    1267                 :             :    defined by a type conversion. Replacing OP0 with RHS of the type conversion.
    1268                 :             :    Doing so makes the conversion dead which helps subsequent passes.  */
    1269                 :             : 
    1270                 :             : bool
    1271                 :    11787953 : simplify_using_ranges::simplify_casted_compare (tree_code &, tree &op0, tree &op1)
    1272                 :             : {
    1273                 :             : 
    1274                 :             :   /* If we have a comparison of an SSA_NAME (OP0) against a constant,
    1275                 :             :      see if OP0 was set by a type conversion where the source of
    1276                 :             :      the conversion is another SSA_NAME with a range that fits
    1277                 :             :      into the range of OP0's type.
    1278                 :             : 
    1279                 :             :      If so, the conversion is redundant as the earlier SSA_NAME can be
    1280                 :             :      used for the comparison directly if we just massage the constant in the
    1281                 :             :      comparison.  */
    1282                 :    11787953 :   if (TREE_CODE (op0) == SSA_NAME
    1283                 :    11561058 :       && TREE_CODE (op1) == INTEGER_CST)
    1284                 :             :     {
    1285                 :     8282690 :       gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
    1286                 :     8282690 :       tree innerop;
    1287                 :             : 
    1288                 :     8282690 :       if (!is_gimple_assign (def_stmt))
    1289                 :             :         return false;
    1290                 :             : 
    1291                 :     5008860 :       switch (gimple_assign_rhs_code (def_stmt))
    1292                 :             :         {
    1293                 :      422311 :         CASE_CONVERT:
    1294                 :      422311 :           innerop = gimple_assign_rhs1 (def_stmt);
    1295                 :      422311 :           break;
    1296                 :       46630 :         case VIEW_CONVERT_EXPR:
    1297                 :       46630 :           innerop = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
    1298                 :       46630 :           if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop)))
    1299                 :             :             return false;
    1300                 :             :           break;
    1301                 :             :         default:
    1302                 :             :           return false;
    1303                 :             :         }
    1304                 :             : 
    1305                 :      467702 :       if (TREE_CODE (innerop) == SSA_NAME
    1306                 :      467640 :           && !POINTER_TYPE_P (TREE_TYPE (innerop))
    1307                 :      461111 :           && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
    1308                 :      928800 :           && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
    1309                 :             :         {
    1310                 :      431076 :           value_range vr;
    1311                 :             : 
    1312                 :      431076 :           if (query->range_of_expr (vr, innerop)
    1313                 :      431072 :               && !vr.varying_p ()
    1314                 :      136054 :               && !vr.undefined_p ()
    1315                 :      135933 :               && range_fits_type_p (&vr,
    1316                 :      135933 :                                     TYPE_PRECISION (TREE_TYPE (op0)),
    1317                 :      135933 :                                     TYPE_SIGN (TREE_TYPE (op0)))
    1318                 :      496719 :               && int_fits_type_p (op1, TREE_TYPE (innerop)))
    1319                 :             :             {
    1320                 :       65643 :               tree newconst = fold_convert (TREE_TYPE (innerop), op1);
    1321                 :       65643 :               op0 = innerop;
    1322                 :       65643 :               op1 = newconst;
    1323                 :       65643 :               return true;
    1324                 :             :             }
    1325                 :      431076 :         }
    1326                 :             :     }
    1327                 :             :   return false;
    1328                 :             : }
    1329                 :             : 
    1330                 :             : /* Simplify a switch statement using the value range of the switch
    1331                 :             :    argument.  */
    1332                 :             : 
    1333                 :             : bool
    1334                 :       58016 : simplify_using_ranges::simplify_switch_using_ranges (gswitch *stmt)
    1335                 :             : {
    1336                 :       58016 :   tree op = gimple_switch_index (stmt);
    1337                 :       58016 :   value_range vr;
    1338                 :       58016 :   bool take_default;
    1339                 :       58016 :   edge e;
    1340                 :       58016 :   edge_iterator ei;
    1341                 :       58016 :   size_t i = 0, j = 0, n, n2;
    1342                 :       58016 :   tree vec2;
    1343                 :       58016 :   switch_update su;
    1344                 :       58016 :   size_t k = 1, l = 0;
    1345                 :             : 
    1346                 :       58016 :   if (TREE_CODE (op) == SSA_NAME)
    1347                 :             :     {
    1348                 :       58000 :       if (!query->range_of_expr (vr, op, stmt)
    1349                 :       58000 :           || vr.varying_p () || vr.undefined_p ())
    1350                 :             :         return false;
    1351                 :             : 
    1352                 :             :       /* Find case label for min/max of the value range.  */
    1353                 :       15717 :       take_default = !find_case_label_ranges (stmt, &vr, &i, &j, &k, &l);
    1354                 :             :     }
    1355                 :          16 :   else if (TREE_CODE (op) == INTEGER_CST)
    1356                 :             :     {
    1357                 :          16 :       take_default = !find_case_label_index (stmt, 1, op, &i);
    1358                 :          16 :       if (take_default)
    1359                 :             :         {
    1360                 :           1 :           i = 1;
    1361                 :           1 :           j = 0;
    1362                 :             :         }
    1363                 :             :       else
    1364                 :             :         {
    1365                 :          15 :           j = i;
    1366                 :             :         }
    1367                 :             :     }
    1368                 :             :   else
    1369                 :             :     return false;
    1370                 :             : 
    1371                 :       15733 :   n = gimple_switch_num_labels (stmt);
    1372                 :             : 
    1373                 :             :   /* We can truncate the case label ranges that partially overlap with OP's
    1374                 :             :      value range.  */
    1375                 :       15733 :   size_t min_idx = 1, max_idx = 0;
    1376                 :       15733 :   tree min, max;
    1377                 :       15733 :   value_range_kind kind = get_legacy_range (vr, min, max);
    1378                 :       15733 :   if (!vr.undefined_p ())
    1379                 :       15717 :     find_case_label_range (stmt, min, max, &min_idx, &max_idx);
    1380                 :       15733 :   if (min_idx <= max_idx)
    1381                 :             :     {
    1382                 :       13604 :       tree min_label = gimple_switch_label (stmt, min_idx);
    1383                 :       13604 :       tree max_label = gimple_switch_label (stmt, max_idx);
    1384                 :             : 
    1385                 :             :       /* Avoid changing the type of the case labels when truncating.  */
    1386                 :       13604 :       tree case_label_type = TREE_TYPE (CASE_LOW (min_label));
    1387                 :       13604 :       tree vr_min = fold_convert (case_label_type, min);
    1388                 :       13604 :       tree vr_max = fold_convert (case_label_type, max);
    1389                 :             : 
    1390                 :       13604 :       if (kind == VR_RANGE)
    1391                 :             :         {
    1392                 :             :           /* If OP's value range is [2,8] and the low label range is
    1393                 :             :              0 ... 3, truncate the label's range to 2 .. 3.  */
    1394                 :       13495 :           if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
    1395                 :           6 :               && CASE_HIGH (min_label) != NULL_TREE
    1396                 :       13501 :               && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
    1397                 :           6 :             CASE_LOW (min_label) = vr_min;
    1398                 :             : 
    1399                 :             :           /* If OP's value range is [2,8] and the high label range is
    1400                 :             :              7 ... 10, truncate the label's range to 7 .. 8.  */
    1401                 :       13495 :           if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
    1402                 :       13495 :               && CASE_HIGH (max_label) != NULL_TREE
    1403                 :       13801 :               && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
    1404                 :           6 :             CASE_HIGH (max_label) = vr_max;
    1405                 :             :         }
    1406                 :         109 :       else if (kind == VR_ANTI_RANGE)
    1407                 :             :         {
    1408                 :         109 :           tree one_cst = build_one_cst (case_label_type);
    1409                 :             : 
    1410                 :         109 :           if (min_label == max_label)
    1411                 :             :             {
    1412                 :             :               /* If OP's value range is ~[7,8] and the label's range is
    1413                 :             :                  7 ... 10, truncate the label's range to 9 ... 10.  */
    1414                 :          99 :               if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) == 0
    1415                 :          96 :                   && CASE_HIGH (min_label) != NULL_TREE
    1416                 :         107 :                   && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) > 0)
    1417                 :           3 :                 CASE_LOW (min_label)
    1418                 :           6 :                   = int_const_binop (PLUS_EXPR, vr_max, one_cst);
    1419                 :             : 
    1420                 :             :               /* If OP's value range is ~[7,8] and the label's range is
    1421                 :             :                  5 ... 8, truncate the label's range to 5 ... 6.  */
    1422                 :          99 :               if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
    1423                 :           3 :                   && CASE_HIGH (min_label) != NULL_TREE
    1424                 :         102 :                   && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) == 0)
    1425                 :           1 :                 CASE_HIGH (min_label)
    1426                 :           2 :                   = int_const_binop (MINUS_EXPR, vr_min, one_cst);
    1427                 :             :             }
    1428                 :             :           else
    1429                 :             :             {
    1430                 :             :               /* If OP's value range is ~[2,8] and the low label range is
    1431                 :             :                  0 ... 3, truncate the label's range to 0 ... 1.  */
    1432                 :          10 :               if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
    1433                 :           1 :                   && CASE_HIGH (min_label) != NULL_TREE
    1434                 :          11 :                   && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
    1435                 :           1 :                 CASE_HIGH (min_label)
    1436                 :           2 :                   = int_const_binop (MINUS_EXPR, vr_min, one_cst);
    1437                 :             : 
    1438                 :             :               /* If OP's value range is ~[2,8] and the high label range is
    1439                 :             :                  7 ... 10, truncate the label's range to 9 ... 10.  */
    1440                 :          10 :               if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
    1441                 :          10 :                   && CASE_HIGH (max_label) != NULL_TREE
    1442                 :          11 :                   && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
    1443                 :           1 :                 CASE_LOW (max_label)
    1444                 :           2 :                   = int_const_binop (PLUS_EXPR, vr_max, one_cst);
    1445                 :             :             }
    1446                 :             :         }
    1447                 :             : 
    1448                 :             :       /* Canonicalize singleton case ranges.  */
    1449                 :       13604 :       if (tree_int_cst_equal (CASE_LOW (min_label), CASE_HIGH (min_label)))
    1450                 :           4 :         CASE_HIGH (min_label) = NULL_TREE;
    1451                 :       13604 :       if (tree_int_cst_equal (CASE_LOW (max_label), CASE_HIGH (max_label)))
    1452                 :           1 :         CASE_HIGH (max_label) = NULL_TREE;
    1453                 :             :     }
    1454                 :             : 
    1455                 :             :   /* We can also eliminate case labels that lie completely outside OP's value
    1456                 :             :      range.  */
    1457                 :             : 
    1458                 :             :   /* Bail out if this is just all edges taken.  */
    1459                 :       15733 :   if (i == 1
    1460                 :       12825 :       && j == n - 1
    1461                 :       12622 :       && take_default)
    1462                 :             :     return false;
    1463                 :             : 
    1464                 :             :   /* Build a new vector of taken case labels.  */
    1465                 :        3792 :   vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
    1466                 :        3792 :   n2 = 0;
    1467                 :             : 
    1468                 :             :   /* Add the default edge, if necessary.  */
    1469                 :        3792 :   if (take_default)
    1470                 :        3068 :     TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
    1471                 :             : 
    1472                 :       14428 :   for (; i <= j; ++i, ++n2)
    1473                 :       10636 :     TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
    1474                 :             : 
    1475                 :        3959 :   for (; k <= l; ++k, ++n2)
    1476                 :         167 :     TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
    1477                 :             : 
    1478                 :             :   /* Mark needed edges.  */
    1479                 :       17663 :   for (i = 0; i < n2; ++i)
    1480                 :             :     {
    1481                 :       13871 :       e = find_edge (gimple_bb (stmt),
    1482                 :             :                      label_to_block (cfun,
    1483                 :       13871 :                                      CASE_LABEL (TREE_VEC_ELT (vec2, i))));
    1484                 :       13871 :       e->aux = (void *)-1;
    1485                 :             :     }
    1486                 :             : 
    1487                 :             :   /* Queue not needed edges for later removal.  */
    1488                 :       23933 :   FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
    1489                 :             :     {
    1490                 :       20141 :       if (e->aux == (void *)-1)
    1491                 :             :         {
    1492                 :       13506 :           e->aux = NULL;
    1493                 :       13506 :           continue;
    1494                 :             :         }
    1495                 :             : 
    1496                 :        6635 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1497                 :             :         {
    1498                 :           0 :           fprintf (dump_file, "removing unreachable case label\n");
    1499                 :             :         }
    1500                 :        6635 :       to_remove_edges.safe_push (e);
    1501                 :        6635 :       set_and_propagate_unexecutable (e);
    1502                 :        6635 :       e->flags &= ~EDGE_EXECUTABLE;
    1503                 :        6635 :       e->flags |= EDGE_IGNORE;
    1504                 :             :     }
    1505                 :             : 
    1506                 :             :   /* And queue an update for the stmt.  */
    1507                 :        3792 :   su.stmt = stmt;
    1508                 :        3792 :   su.vec = vec2;
    1509                 :        3792 :   to_update_switch_stmts.safe_push (su);
    1510                 :        3792 :   return true;
    1511                 :       58016 : }
    1512                 :             : 
    1513                 :             : void
    1514                 :    11644160 : simplify_using_ranges::cleanup_edges_and_switches (void)
    1515                 :             : {
    1516                 :    11644160 :   int i;
    1517                 :    11644160 :   edge e;
    1518                 :    11644160 :   switch_update *su;
    1519                 :             : 
    1520                 :             :   /* Clear any edges marked as not executable.  */
    1521                 :    11644160 :   if (m_not_executable_flag)
    1522                 :             :     {
    1523                 :     4139703 :       FOR_EACH_VEC_ELT (m_flag_set_edges, i, e)
    1524                 :      183476 :         e->flags &= ~m_not_executable_flag;
    1525                 :             :     }
    1526                 :             :   /* Remove dead edges from SWITCH_EXPR optimization.  This leaves the
    1527                 :             :      CFG in a broken state and requires a cfg_cleanup run.  */
    1528                 :    11654349 :   FOR_EACH_VEC_ELT (to_remove_edges, i, e)
    1529                 :        6635 :     remove_edge (e);
    1530                 :             : 
    1531                 :             :   /* Update SWITCH_EXPR case label vector.  */
    1532                 :    11647952 :   FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
    1533                 :             :     {
    1534                 :        3792 :       size_t j;
    1535                 :        3792 :       size_t n = TREE_VEC_LENGTH (su->vec);
    1536                 :        3792 :       tree label;
    1537                 :        3792 :       gimple_switch_set_num_labels (su->stmt, n);
    1538                 :       21455 :       for (j = 0; j < n; j++)
    1539                 :       13871 :         gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
    1540                 :             :       /* As we may have replaced the default label with a regular one
    1541                 :             :          make sure to make it a real default label again.  This ensures
    1542                 :             :          optimal expansion.  */
    1543                 :        3792 :       label = gimple_switch_label (su->stmt, 0);
    1544                 :        3792 :       CASE_LOW (label) = NULL_TREE;
    1545                 :        3792 :       CASE_HIGH (label) = NULL_TREE;
    1546                 :             :     }
    1547                 :             : 
    1548                 :    11644160 :   if (!to_remove_edges.is_empty ())
    1549                 :             :     {
    1550                 :        3554 :       free_dominance_info (CDI_DOMINATORS);
    1551                 :        3554 :       loops_state_set (LOOPS_NEED_FIXUP);
    1552                 :             :     }
    1553                 :             : 
    1554                 :    11644160 :   to_remove_edges.release ();
    1555                 :    11644160 :   to_update_switch_stmts.release ();
    1556                 :    11644160 : }
    1557                 :             : 
    1558                 :             : /* Simplify an integral conversion from an SSA name in STMT.  */
    1559                 :             : 
    1560                 :             : static bool
    1561                 :     4565225 : simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
    1562                 :             : {
    1563                 :     4565225 :   tree innerop, middleop, finaltype;
    1564                 :     4565225 :   gimple *def_stmt;
    1565                 :     4565225 :   signop inner_sgn, middle_sgn, final_sgn;
    1566                 :     4565225 :   unsigned inner_prec, middle_prec, final_prec;
    1567                 :     4565225 :   widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
    1568                 :             : 
    1569                 :     4565225 :   finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
    1570                 :     4565225 :   if (!INTEGRAL_TYPE_P (finaltype))
    1571                 :             :     return false;
    1572                 :     4246542 :   middleop = gimple_assign_rhs1 (stmt);
    1573                 :     4246542 :   def_stmt = SSA_NAME_DEF_STMT (middleop);
    1574                 :     4246542 :   if (!is_gimple_assign (def_stmt)
    1575                 :     4246542 :       || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
    1576                 :             :     return false;
    1577                 :      169966 :   innerop = gimple_assign_rhs1 (def_stmt);
    1578                 :      169966 :   if (TREE_CODE (innerop) != SSA_NAME
    1579                 :      169966 :       || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
    1580                 :             :     return false;
    1581                 :             : 
    1582                 :             :   /* Get the value-range of the inner operand.  Use global ranges in
    1583                 :             :      case innerop was created during substitute-and-fold.  */
    1584                 :      166726 :   wide_int imin, imax;
    1585                 :      166726 :   value_range vr;
    1586                 :      166726 :   if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop)))
    1587                 :             :     return false;
    1588                 :      286994 :   get_range_query (cfun)->range_of_expr (vr, innerop, stmt);
    1589                 :      143497 :   if (vr.undefined_p () || vr.varying_p ())
    1590                 :             :     return false;
    1591                 :       71836 :   innermin = widest_int::from (vr.lower_bound (), TYPE_SIGN (TREE_TYPE (innerop)));
    1592                 :       71836 :   innermax = widest_int::from (vr.upper_bound (), TYPE_SIGN (TREE_TYPE (innerop)));
    1593                 :             : 
    1594                 :             :   /* Simulate the conversion chain to check if the result is equal if
    1595                 :             :      the middle conversion is removed.  */
    1596                 :       71836 :   inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
    1597                 :       71836 :   middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
    1598                 :       71836 :   final_prec = TYPE_PRECISION (finaltype);
    1599                 :             : 
    1600                 :             :   /* If the first conversion is not injective, the second must not
    1601                 :             :      be widening.  */
    1602                 :       71836 :   if (wi::gtu_p (innermax - innermin,
    1603                 :      143672 :                  wi::mask <widest_int> (middle_prec, false))
    1604                 :       71836 :       && middle_prec < final_prec)
    1605                 :             :     return false;
    1606                 :             :   /* We also want a medium value so that we can track the effect that
    1607                 :             :      narrowing conversions with sign change have.  */
    1608                 :       58140 :   inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
    1609                 :       58140 :   if (inner_sgn == UNSIGNED)
    1610                 :       43937 :     innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
    1611                 :             :   else
    1612                 :       14203 :     innermed = 0;
    1613                 :       58140 :   if (wi::cmp (innermin, innermed, inner_sgn) >= 0
    1614                 :       58140 :       || wi::cmp (innermed, innermax, inner_sgn) >= 0)
    1615                 :       44380 :     innermed = innermin;
    1616                 :             : 
    1617                 :       58140 :   middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
    1618                 :       58140 :   middlemin = wi::ext (innermin, middle_prec, middle_sgn);
    1619                 :       58140 :   middlemed = wi::ext (innermed, middle_prec, middle_sgn);
    1620                 :       58140 :   middlemax = wi::ext (innermax, middle_prec, middle_sgn);
    1621                 :             : 
    1622                 :             :   /* Require that the final conversion applied to both the original
    1623                 :             :      and the intermediate range produces the same result.  */
    1624                 :       58140 :   final_sgn = TYPE_SIGN (finaltype);
    1625                 :      116280 :   if (wi::ext (middlemin, final_prec, final_sgn)
    1626                 :      116280 :          != wi::ext (innermin, final_prec, final_sgn)
    1627                 :      111446 :       || wi::ext (middlemed, final_prec, final_sgn)
    1628                 :      225309 :          != wi::ext (innermed, final_prec, final_sgn)
    1629                 :      207369 :       || wi::ext (middlemax, final_prec, final_sgn)
    1630                 :      198399 :          != wi::ext (innermax, final_prec, final_sgn))
    1631                 :             :     return false;
    1632                 :             : 
    1633                 :       45974 :   gimple_assign_set_rhs1 (stmt, innerop);
    1634                 :       45974 :   fold_stmt (gsi, follow_single_use_edges);
    1635                 :       45974 :   return true;
    1636                 :     4731951 : }
    1637                 :             : 
    1638                 :             : /* Simplify a conversion from integral SSA name to float in STMT.  */
    1639                 :             : 
    1640                 :             : bool
    1641                 :      246378 : simplify_using_ranges::simplify_float_conversion_using_ranges
    1642                 :             :                                         (gimple_stmt_iterator *gsi,
    1643                 :             :                                          gimple *stmt)
    1644                 :             : {
    1645                 :      246378 :   tree rhs1 = gimple_assign_rhs1 (stmt);
    1646                 :      246378 :   value_range vr;
    1647                 :      246378 :   scalar_float_mode fltmode
    1648                 :      246378 :     = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
    1649                 :      246378 :   scalar_int_mode mode;
    1650                 :      246378 :   tree tem;
    1651                 :      246378 :   gassign *conv;
    1652                 :             : 
    1653                 :             :   /* We can only handle constant ranges.  */
    1654                 :      246378 :   if (!query->range_of_expr (vr, rhs1, stmt)
    1655                 :      246378 :       || vr.varying_p ()
    1656                 :      341850 :       || vr.undefined_p ())
    1657                 :             :     return false;
    1658                 :             : 
    1659                 :             :   /* The code below doesn't work for large/huge _BitInt, nor is really
    1660                 :             :      needed for those, bitint lowering does use ranges already.  */
    1661                 :       94846 :   if (TREE_CODE (TREE_TYPE (rhs1)) == BITINT_TYPE
    1662                 :       94846 :       && TYPE_MODE (TREE_TYPE (rhs1)) == BLKmode)
    1663                 :             :     return false;
    1664                 :             :   /* First check if we can use a signed type in place of an unsigned.  */
    1665                 :       94844 :   scalar_int_mode rhs_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1));
    1666                 :       94844 :   if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
    1667                 :        5236 :       && can_float_p (fltmode, rhs_mode, 0) != CODE_FOR_nothing
    1668                 :       98357 :       && range_fits_type_p (&vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
    1669                 :             :     mode = rhs_mode;
    1670                 :             :   /* If we can do the conversion in the current input mode do nothing.  */
    1671                 :       93646 :   else if (can_float_p (fltmode, rhs_mode,
    1672                 :       93646 :                         TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
    1673                 :             :     return false;
    1674                 :             :   /* Otherwise search for a mode we can use, starting from the narrowest
    1675                 :             :      integer mode available.  */
    1676                 :             :   else
    1677                 :             :     {
    1678                 :        4314 :       mode = NARROWEST_INT_MODE;
    1679                 :       15561 :       for (;;)
    1680                 :             :         {
    1681                 :             :           /* If we cannot do a signed conversion to float from mode
    1682                 :             :              or if the value-range does not fit in the signed type
    1683                 :             :              try with a wider mode.  */
    1684                 :       15561 :           if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
    1685                 :       15561 :               && range_fits_type_p (&vr, GET_MODE_PRECISION (mode), SIGNED))
    1686                 :             :             break;
    1687                 :             : 
    1688                 :             :           /* But do not widen the input.  Instead leave that to the
    1689                 :             :              optabs expansion code.  */
    1690                 :       30890 :           if (!GET_MODE_WIDER_MODE (mode).exists (&mode)
    1691                 :       15445 :               || GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
    1692                 :             :             return false;
    1693                 :             :         }
    1694                 :             :     }
    1695                 :             : 
    1696                 :             :   /* It works, insert a truncation or sign-change before the
    1697                 :             :      float conversion.  */
    1698                 :        1314 :   tem = make_ssa_name (build_nonstandard_integer_type
    1699                 :        1314 :                           (GET_MODE_PRECISION (mode), 0));
    1700                 :        1314 :   conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
    1701                 :        1314 :   gsi_insert_before (gsi, conv, GSI_SAME_STMT);
    1702                 :        1314 :   gimple_assign_set_rhs1 (stmt, tem);
    1703                 :        1314 :   fold_stmt (gsi, follow_single_use_edges);
    1704                 :             : 
    1705                 :        1314 :   return true;
    1706                 :      246378 : }
    1707                 :             : 
    1708                 :             : /* Simplify an internal fn call using ranges if possible.  */
    1709                 :             : 
    1710                 :             : bool
    1711                 :      344136 : simplify_using_ranges::simplify_internal_call_using_ranges
    1712                 :             :                                         (gimple_stmt_iterator *gsi,
    1713                 :             :                                          gimple *stmt)
    1714                 :             : {
    1715                 :      344136 :   enum tree_code subcode;
    1716                 :      344136 :   bool is_ubsan = false;
    1717                 :      344136 :   bool ovf = false;
    1718                 :      344136 :   switch (gimple_call_internal_fn (stmt))
    1719                 :             :     {
    1720                 :             :     case IFN_UBSAN_CHECK_ADD:
    1721                 :             :       subcode = PLUS_EXPR;
    1722                 :             :       is_ubsan = true;
    1723                 :             :       break;
    1724                 :        2459 :     case IFN_UBSAN_CHECK_SUB:
    1725                 :        2459 :       subcode = MINUS_EXPR;
    1726                 :        2459 :       is_ubsan = true;
    1727                 :        2459 :       break;
    1728                 :        2114 :     case IFN_UBSAN_CHECK_MUL:
    1729                 :        2114 :       subcode = MULT_EXPR;
    1730                 :        2114 :       is_ubsan = true;
    1731                 :        2114 :       break;
    1732                 :       40262 :     case IFN_ADD_OVERFLOW:
    1733                 :       40262 :       subcode = PLUS_EXPR;
    1734                 :       40262 :       break;
    1735                 :       49491 :     case IFN_SUB_OVERFLOW:
    1736                 :       49491 :       subcode = MINUS_EXPR;
    1737                 :       49491 :       break;
    1738                 :       46745 :     case IFN_MUL_OVERFLOW:
    1739                 :       46745 :       subcode = MULT_EXPR;
    1740                 :       46745 :       break;
    1741                 :             :     default:
    1742                 :             :       return false;
    1743                 :             :     }
    1744                 :             : 
    1745                 :      143649 :   tree op0 = gimple_call_arg (stmt, 0);
    1746                 :      143649 :   tree op1 = gimple_call_arg (stmt, 1);
    1747                 :      143649 :   tree type;
    1748                 :      143649 :   if (is_ubsan)
    1749                 :             :     {
    1750                 :        7151 :       type = TREE_TYPE (op0);
    1751                 :        7151 :       if (VECTOR_TYPE_P (type))
    1752                 :             :         return false;
    1753                 :             :     }
    1754                 :      136498 :   else if (gimple_call_lhs (stmt) == NULL_TREE)
    1755                 :             :     return false;
    1756                 :             :   else
    1757                 :      136498 :     type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
    1758                 :      142776 :   if (!check_for_binary_op_overflow (query, subcode, type, op0, op1, &ovf, stmt)
    1759                 :      142776 :       || (is_ubsan && ovf))
    1760                 :             :     return false;
    1761                 :             : 
    1762                 :        3408 :   gimple *g;
    1763                 :        3408 :   location_t loc = gimple_location (stmt);
    1764                 :        3408 :   if (is_ubsan)
    1765                 :         554 :     g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
    1766                 :             :   else
    1767                 :             :     {
    1768                 :        2854 :       tree utype = type;
    1769                 :        2854 :       if (ovf
    1770                 :        1372 :           || !useless_type_conversion_p (type, TREE_TYPE (op0))
    1771                 :        3503 :           || !useless_type_conversion_p (type, TREE_TYPE (op1)))
    1772                 :        2489 :         utype = unsigned_type_for (type);
    1773                 :        2854 :       if (TREE_CODE (op0) == INTEGER_CST)
    1774                 :        1174 :         op0 = fold_convert (utype, op0);
    1775                 :        1680 :       else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
    1776                 :             :         {
    1777                 :         722 :           g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
    1778                 :         722 :           gimple_set_location (g, loc);
    1779                 :         722 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    1780                 :         722 :           op0 = gimple_assign_lhs (g);
    1781                 :             :         }
    1782                 :        2854 :       if (TREE_CODE (op1) == INTEGER_CST)
    1783                 :        1645 :         op1 = fold_convert (utype, op1);
    1784                 :        1209 :       else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
    1785                 :             :         {
    1786                 :          20 :           g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
    1787                 :          20 :           gimple_set_location (g, loc);
    1788                 :          20 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    1789                 :          20 :           op1 = gimple_assign_lhs (g);
    1790                 :             :         }
    1791                 :        2854 :       g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
    1792                 :        2854 :       gimple_set_location (g, loc);
    1793                 :        2854 :       gsi_insert_before (gsi, g, GSI_SAME_STMT);
    1794                 :        2854 :       if (utype != type)
    1795                 :             :         {
    1796                 :        1337 :           g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
    1797                 :             :                                    gimple_assign_lhs (g));
    1798                 :        1337 :           gimple_set_location (g, loc);
    1799                 :        1337 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    1800                 :             :         }
    1801                 :        2854 :       g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
    1802                 :             :                                gimple_assign_lhs (g),
    1803                 :             :                                build_int_cst (type, ovf));
    1804                 :             :     }
    1805                 :        3408 :   gimple_set_location (g, loc);
    1806                 :        3408 :   gsi_replace (gsi, g, false);
    1807                 :        3408 :   return true;
    1808                 :             : }
    1809                 :             : 
    1810                 :             : /* Return true if VAR is a two-valued variable.  Set a and b with the
    1811                 :             :    two-values when it is true.  Return false otherwise.  */
    1812                 :             : 
    1813                 :             : bool
    1814                 :      430842 : simplify_using_ranges::two_valued_val_range_p (tree var, tree *a, tree *b,
    1815                 :             :                                                gimple *s)
    1816                 :             : {
    1817                 :      430842 :   value_range vr;
    1818                 :      430842 :   if (!query->range_of_expr (vr, var, s))
    1819                 :             :     return false;
    1820                 :      430842 :   if (vr.varying_p () || vr.undefined_p ())
    1821                 :             :     return false;
    1822                 :             : 
    1823                 :      630878 :   if ((vr.num_pairs () == 1 && vr.upper_bound () - vr.lower_bound () == 1)
    1824                 :      326665 :       || (vr.num_pairs () == 2
    1825                 :      245736 :           && vr.lower_bound (0) == vr.upper_bound (0)
    1826                 :      201546 :           && vr.lower_bound (1) == vr.upper_bound (1)))
    1827                 :             :     {
    1828                 :        5113 :       *a = wide_int_to_tree (TREE_TYPE (var), vr.lower_bound ());
    1829                 :        5113 :       *b = wide_int_to_tree (TREE_TYPE (var), vr.upper_bound ());
    1830                 :        5113 :       return true;
    1831                 :             :     }
    1832                 :             :   return false;
    1833                 :      430842 : }
    1834                 :             : 
    1835                 :    11644160 : simplify_using_ranges::simplify_using_ranges (range_query *query,
    1836                 :    11644160 :                                               int not_executable_flag)
    1837                 :    11644160 :   : query (query)
    1838                 :             : {
    1839                 :    11644160 :   to_remove_edges = vNULL;
    1840                 :    11644160 :   to_update_switch_stmts = vNULL;
    1841                 :    11644160 :   m_not_executable_flag = not_executable_flag;
    1842                 :    11644160 :   m_flag_set_edges = vNULL;
    1843                 :    11644160 : }
    1844                 :             : 
    1845                 :    11644160 : simplify_using_ranges::~simplify_using_ranges ()
    1846                 :             : {
    1847                 :    11644160 :   cleanup_edges_and_switches ();
    1848                 :    11644160 :   m_flag_set_edges.release ();
    1849                 :    11644160 : }
    1850                 :             : 
    1851                 :             : /* Simplify STMT using ranges if possible.  */
    1852                 :             : 
    1853                 :             : bool
    1854                 :   200666518 : simplify_using_ranges::simplify (gimple_stmt_iterator *gsi)
    1855                 :             : {
    1856                 :   200666518 :   gcc_checking_assert (query);
    1857                 :             : 
    1858                 :   200666518 :   gimple *stmt = gsi_stmt (*gsi);
    1859                 :   200666518 :   if (is_gimple_assign (stmt))
    1860                 :             :     {
    1861                 :    61066578 :       enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
    1862                 :    61066578 :       tree rhs1 = gimple_assign_rhs1 (stmt);
    1863                 :    61066578 :       tree rhs2 = gimple_assign_rhs2 (stmt);
    1864                 :    61066578 :       tree lhs = gimple_assign_lhs (stmt);
    1865                 :    61066578 :       tree val1 = NULL_TREE, val2 = NULL_TREE;
    1866                 :    61066578 :       use_operand_p use_p;
    1867                 :    61066578 :       gimple *use_stmt;
    1868                 :             : 
    1869                 :             :       /* Convert:
    1870                 :             :          LHS = CST BINOP VAR
    1871                 :             :          Where VAR is two-valued and LHS is used in GIMPLE_COND only
    1872                 :             :          To:
    1873                 :             :          LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
    1874                 :             : 
    1875                 :             :          Also handles:
    1876                 :             :          LHS = VAR BINOP CST
    1877                 :             :          Where VAR is two-valued and LHS is used in GIMPLE_COND only
    1878                 :             :          To:
    1879                 :             :          LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
    1880                 :             : 
    1881                 :    61066578 :       if (TREE_CODE_CLASS (rhs_code) == tcc_binary
    1882                 :    12652723 :           && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
    1883                 :     8942797 :           && ((TREE_CODE (rhs1) == INTEGER_CST
    1884                 :      158851 :                && TREE_CODE (rhs2) == SSA_NAME)
    1885                 :     8784548 :               || (TREE_CODE (rhs2) == INTEGER_CST
    1886                 :     5814589 :                   && TREE_CODE (rhs1) == SSA_NAME))
    1887                 :     5972236 :           && single_imm_use (lhs, &use_p, &use_stmt)
    1888                 :    65666917 :           && gimple_code (use_stmt) == GIMPLE_COND)
    1889                 :             : 
    1890                 :             :         {
    1891                 :      430842 :           tree new_rhs1 = NULL_TREE;
    1892                 :      430842 :           tree new_rhs2 = NULL_TREE;
    1893                 :      430842 :           tree cmp_var = NULL_TREE;
    1894                 :             : 
    1895                 :      430842 :           if (TREE_CODE (rhs2) == SSA_NAME
    1896                 :      430842 :               && two_valued_val_range_p (rhs2, &val1, &val2, stmt))
    1897                 :             :             {
    1898                 :             :               /* Optimize RHS1 OP [VAL1, VAL2].  */
    1899                 :         196 :               new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
    1900                 :         196 :               new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
    1901                 :         196 :               cmp_var = rhs2;
    1902                 :             :             }
    1903                 :      430646 :           else if (TREE_CODE (rhs1) == SSA_NAME
    1904                 :      430646 :                    && two_valued_val_range_p (rhs1, &val1, &val2, stmt))
    1905                 :             :             {
    1906                 :             :               /* Optimize [VAL1, VAL2] OP RHS2.  */
    1907                 :        4917 :               new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
    1908                 :        4917 :               new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
    1909                 :        4917 :               cmp_var = rhs1;
    1910                 :             :             }
    1911                 :             : 
    1912                 :             :           /* If we could not find two-vals or the optimzation is invalid as
    1913                 :             :              in divide by zero, new_rhs1 / new_rhs will be NULL_TREE.  */
    1914                 :      430842 :           if (new_rhs1 && new_rhs2)
    1915                 :             :             {
    1916                 :        5113 :               tree cond = gimple_build (gsi, true, GSI_SAME_STMT,
    1917                 :             :                                         UNKNOWN_LOCATION,
    1918                 :             :                                         EQ_EXPR, boolean_type_node,
    1919                 :             :                                         cmp_var, val1);
    1920                 :        5113 :               gimple_assign_set_rhs_with_ops (gsi,
    1921                 :             :                                               COND_EXPR, cond,
    1922                 :             :                                               new_rhs1,
    1923                 :             :                                               new_rhs2);
    1924                 :        5113 :               update_stmt (gsi_stmt (*gsi));
    1925                 :        5113 :               fold_stmt (gsi, follow_single_use_edges);
    1926                 :     7761475 :               return true;
    1927                 :             :             }
    1928                 :             :         }
    1929                 :             : 
    1930                 :    61061465 :       if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
    1931                 :     1192131 :         return simplify_compare_assign_using_ranges_1 (gsi, stmt);
    1932                 :             : 
    1933                 :    59869334 :       switch (rhs_code)
    1934                 :             :         {
    1935                 :             : 
    1936                 :             :       /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
    1937                 :             :          and BIT_AND_EXPR respectively if the first operand is greater
    1938                 :             :          than zero and the second operand is an exact power of two.
    1939                 :             :          Also optimize TRUNC_MOD_EXPR away if the second operand is
    1940                 :             :          constant and the first operand already has the right value
    1941                 :             :          range.  */
    1942                 :      307040 :         case TRUNC_DIV_EXPR:
    1943                 :      307040 :         case TRUNC_MOD_EXPR:
    1944                 :      307040 :           if ((TREE_CODE (rhs1) == SSA_NAME
    1945                 :      307040 :                || TREE_CODE (rhs1) == INTEGER_CST)
    1946                 :      307040 :               && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
    1947                 :      305822 :             return simplify_div_or_mod_using_ranges (gsi, stmt);
    1948                 :             :           break;
    1949                 :             : 
    1950                 :             :       /* Transform ABS (X) into X or -X as appropriate.  */
    1951                 :       47851 :         case ABS_EXPR:
    1952                 :       47851 :           if (TREE_CODE (rhs1) == SSA_NAME
    1953                 :       47851 :               && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
    1954                 :        5509 :             return simplify_abs_using_ranges (gsi, stmt);
    1955                 :             :           break;
    1956                 :             : 
    1957                 :     1230207 :         case BIT_AND_EXPR:
    1958                 :     1230207 :         case BIT_IOR_EXPR:
    1959                 :             :           /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
    1960                 :             :              if all the bits being cleared are already cleared or
    1961                 :             :              all the bits being set are already set.  */
    1962                 :     1230207 :           if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
    1963                 :     1215176 :             return simplify_bit_ops_using_ranges (gsi, stmt);
    1964                 :             :           break;
    1965                 :             : 
    1966                 :     5605430 :         CASE_CONVERT:
    1967                 :     5605430 :           if (TREE_CODE (rhs1) == SSA_NAME
    1968                 :     5605430 :               && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
    1969                 :     4565225 :             return simplify_conversion_using_ranges (gsi, stmt);
    1970                 :             :           break;
    1971                 :             : 
    1972                 :      247984 :         case FLOAT_EXPR:
    1973                 :      247984 :           if (TREE_CODE (rhs1) == SSA_NAME
    1974                 :      247984 :               && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
    1975                 :      246378 :             return simplify_float_conversion_using_ranges (gsi, stmt);
    1976                 :             :           break;
    1977                 :             : 
    1978                 :      153293 :         case MIN_EXPR:
    1979                 :      153293 :         case MAX_EXPR:
    1980                 :      153293 :           if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
    1981                 :      149740 :             return simplify_min_or_max_using_ranges (gsi, stmt);
    1982                 :             :           break;
    1983                 :             : 
    1984                 :      346070 :         case RSHIFT_EXPR:
    1985                 :      346070 :           {
    1986                 :      346070 :             tree op0 = gimple_assign_rhs1 (stmt);
    1987                 :      346070 :             tree type = TREE_TYPE (op0);
    1988                 :      346070 :             int_range_max range;
    1989                 :      346070 :             if (TYPE_SIGN (type) == SIGNED
    1990                 :      346070 :                 && query->range_of_expr (range, op0, stmt))
    1991                 :             :               {
    1992                 :       76381 :                 unsigned prec = TYPE_PRECISION (TREE_TYPE (op0));
    1993                 :      152762 :                 int_range<2> nzm1 (type, wi::minus_one (prec), wi::zero (prec),
    1994                 :      152770 :                                    VR_ANTI_RANGE);
    1995                 :       76381 :                 range.intersect (nzm1);
    1996                 :             :                 // If there are no ranges other than [-1, 0] remove the shift.
    1997                 :       76381 :                 if (range.undefined_p ())
    1998                 :             :                   {
    1999                 :          44 :                     gimple_assign_set_rhs_from_tree (gsi, op0);
    2000                 :          44 :                     return true;
    2001                 :             :                   }
    2002                 :             :                 return false;
    2003                 :       76381 :               }
    2004                 :      269689 :             break;
    2005                 :      346070 :           }
    2006                 :             :         default:
    2007                 :             :           break;
    2008                 :             :         }
    2009                 :             :     }
    2010                 :   139599940 :   else if (gimple_code (stmt) == GIMPLE_COND)
    2011                 :    10707128 :     return simplify_cond_using_ranges_1 (as_a <gcond *> (stmt));
    2012                 :   128892812 :   else if (gimple_code (stmt) == GIMPLE_SWITCH)
    2013                 :       58016 :     return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
    2014                 :   128834796 :   else if (is_gimple_call (stmt)
    2015                 :   128834796 :            && gimple_call_internal_p (stmt))
    2016                 :      344136 :     return simplify_internal_call_using_ranges (gsi, stmt);
    2017                 :             : 
    2018                 :             :   return false;
    2019                 :             : }
        

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.