LCOV - code coverage report
Current view: top level - gcc - vr-values.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 97.0 % 965 936
Test Date: 2026-06-20 15:32:29 Functions: 100.0 % 32 32
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Support routines for Value Range Propagation (VRP).
       2              :    Copyright (C) 2005-2026 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       473789 : simplify_using_ranges::op_with_boolean_value_range_p (tree op, gimple *s)
      59              : {
      60       473789 :   if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
      61              :     return true;
      62              : 
      63       461637 :   if (integer_zerop (op)
      64       461637 :       || integer_onep (op))
      65         8461 :     return true;
      66              : 
      67       453176 :   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       453176 :   int_range_max vr;
      73       453176 :   return (query->range_of_expr (vr, op, s)
      74       453176 :           && vr == range_true_and_false (TREE_TYPE (op)));
      75       453176 : }
      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       143486 : 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       143486 :   relation_kind rel = VREL_VARYING;
      89              :   /* For subtraction see if relations could simplify it.  */
      90       143486 :   if (s
      91       143486 :       && subcode == MINUS_EXPR
      92       143486 :       && types_compatible_p (TREE_TYPE (op0), TREE_TYPE (op1)))
      93              :     {
      94        28982 :       rel = query->relation().query (s, op0, op1);
      95              :       /* The result of the infinite precision subtraction of
      96              :          the same values will be always 0.  That will fit into any result
      97              :          type.  */
      98        28982 :       if (rel == VREL_EQ)
      99              :         return true;
     100              :     }
     101              : 
     102       143485 :   int_range_max vr0, vr1;
     103       143485 :   if (!query->range_of_expr (vr0, op0, s) || vr0.undefined_p ())
     104           12 :     vr0.set_varying (TREE_TYPE (op0));
     105       143485 :   if (!query->range_of_expr (vr1, op1, s) || vr1.undefined_p ())
     106            0 :     vr1.set_varying (TREE_TYPE (op1));
     107              : 
     108       143485 :   tree vr0min = wide_int_to_tree (TREE_TYPE (op0), vr0.lower_bound ());
     109       143485 :   tree vr0max = wide_int_to_tree (TREE_TYPE (op0), vr0.upper_bound ());
     110       143485 :   tree vr1min = wide_int_to_tree (TREE_TYPE (op1), vr1.lower_bound ());
     111       143485 :   tree vr1max = wide_int_to_tree (TREE_TYPE (op1), vr1.upper_bound ());
     112              : 
     113              :   /* If op1 is not negative, op0 - op1 in infinite precision for op0 >= op1
     114              :      will be always in [0, op0] and so if vr0max - vr1min fits into type,
     115              :      there won't be any overflow.  */
     116       143485 :   if ((rel == VREL_GT || rel == VREL_GE)
     117           23 :       && tree_int_cst_sgn (vr1min) >= 0
     118       143502 :       && !arith_overflowed_p (MINUS_EXPR, type, vr0max, vr1min))
     119              :     return true;
     120              : 
     121              :   /* If op1 is not negative, op0 - op1 in infinite precision for op0 < op1
     122              :      will be always in [-inf, -1] and so will always overflow if type is
     123              :      unsigned.  */
     124       143468 :   if (rel == VREL_LT
     125            1 :       && tree_int_cst_sgn (vr1min) >= 0
     126       143469 :       && TYPE_UNSIGNED (type))
     127              :     {
     128            1 :       *ovf = true;
     129            1 :       return true;
     130              :     }
     131              : 
     132       235156 :   *ovf = arith_overflowed_p (subcode, type, vr0min,
     133              :                              subcode == MINUS_EXPR ? vr1max : vr1min);
     134       235156 :   if (arith_overflowed_p (subcode, type, vr0max,
     135       143467 :                           subcode == MINUS_EXPR ? vr1min : vr1max) != *ovf)
     136              :     return false;
     137        39432 :   if (subcode == MULT_EXPR)
     138              :     {
     139        23882 :       if (arith_overflowed_p (subcode, type, vr0min, vr1max) != *ovf
     140        23882 :           || arith_overflowed_p (subcode, type, vr0max, vr1min) != *ovf)
     141          376 :         return false;
     142              :     }
     143        39056 :   if (*ovf)
     144              :     {
     145              :       /* So far we found that there is an overflow on the boundaries.
     146              :          That doesn't prove that there is an overflow even for all values
     147              :          in between the boundaries.  For that compute widest2_int range
     148              :          of the result and see if it doesn't overlap the range of
     149              :          type.  */
     150        37061 :       widest2_int wmin, wmax;
     151       333731 :       widest2_int w[4];
     152        37061 :       int i;
     153        37061 :       signop sign0 = TYPE_SIGN (TREE_TYPE (op0));
     154        37061 :       signop sign1 = TYPE_SIGN (TREE_TYPE (op1));
     155        37107 :       w[0] = widest2_int::from (vr0.lower_bound (), sign0);
     156        37115 :       w[1] = widest2_int::from (vr0.upper_bound (), sign0);
     157        37098 :       w[2] = widest2_int::from (vr1.lower_bound (), sign1);
     158        37106 :       w[3] = widest2_int::from (vr1.upper_bound (), sign1);
     159       185305 :       for (i = 0; i < 4; i++)
     160              :         {
     161       148244 :           widest2_int wt;
     162       148244 :           switch (subcode)
     163              :             {
     164        26996 :             case PLUS_EXPR:
     165        26996 :               wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
     166        26996 :               break;
     167        27856 :             case MINUS_EXPR:
     168        27856 :               wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
     169        27856 :               break;
     170        93392 :             case MULT_EXPR:
     171        93392 :               wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
     172        93392 :               break;
     173            0 :             default:
     174            0 :               gcc_unreachable ();
     175              :             }
     176       148244 :           if (i == 0)
     177              :             {
     178        37061 :               wmin = wt;
     179        37061 :               wmax = wt;
     180              :             }
     181              :           else
     182              :             {
     183       111183 :               wmin = wi::smin (wmin, wt);
     184       111991 :               wmax = wi::smax (wmax, wt);
     185              :             }
     186       148244 :         }
     187              :       /* The result of op0 CODE op1 is known to be in range
     188              :          [wmin, wmax].  */
     189        37061 :       widest2_int wtmin
     190        74122 :         = widest2_int::from (irange_val_min (type), TYPE_SIGN (type));
     191        37061 :       widest2_int wtmax
     192        74122 :         = widest2_int::from (irange_val_max (type), TYPE_SIGN (type));
     193              :       /* If all values in [wmin, wmax] are smaller than
     194              :          [wtmin, wtmax] or all are larger than [wtmin, wtmax],
     195              :          the arithmetic operation will always overflow.  */
     196        73112 :       if (wmax < wtmin || wmin > wtmax)
     197         1661 :         return true;
     198              :       return false;
     199       222632 :     }
     200              :   return true;
     201       143485 : }
     202              : 
     203              : /* Set INIT, STEP, and DIRECTION to the corresponding values of NAME
     204              :    within LOOP, and return TRUE.  Otherwise return FALSE, and set R to
     205              :    the conservative range of NAME within the loop.  */
     206              : 
     207              : static bool
     208      6709377 : get_scev_info (vrange &r, tree name, gimple *stmt, class loop *l,
     209              :                tree &init, tree &step, enum ev_direction &dir)
     210              : {
     211      6709377 :   tree ev = analyze_scalar_evolution (l, name);
     212      6709377 :   tree chrec = instantiate_parameters (l, ev);
     213      6709377 :   tree type = TREE_TYPE (name);
     214      6709377 :   if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
     215              :     {
     216      3229025 :       r.set_varying (type);
     217      3229025 :       return false;
     218              :     }
     219      3480352 :   if (is_gimple_min_invariant (chrec))
     220              :     {
     221            0 :       if (is_gimple_constant (chrec))
     222            0 :         r.set (chrec, chrec);
     223              :       else
     224            0 :         r.set_varying (type);
     225            0 :       return false;
     226              :     }
     227              : 
     228      3480352 :   init = initial_condition_in_loop_num (chrec, l->num);
     229      3480352 :   step = evolution_part_in_loop_num (chrec, l->num);
     230      3480352 :   if (!init || !step)
     231              :     {
     232            0 :       r.set_varying (type);
     233            0 :       return false;
     234              :     }
     235      3480352 :   dir = scev_direction (chrec);
     236      3480352 :   if (dir == EV_DIR_UNKNOWN
     237      3480352 :       || scev_probably_wraps_p (NULL, init, step, stmt,
     238              :                                 get_chrec_loop (chrec), true))
     239              :     {
     240       749943 :       r.set_varying (type);
     241       749943 :       return false;
     242              :     }
     243              :   return true;
     244              : }
     245              : 
     246              : /* Return TRUE if STEP * NIT may overflow when calculated in TYPE.  */
     247              : 
     248              : static bool
     249      2290108 : induction_variable_may_overflow_p (tree type,
     250              :                                    const wide_int &step, const widest_int &nit)
     251              : {
     252      2290108 :   wi::overflow_type ovf;
     253      2290108 :   signop sign = TYPE_SIGN (type);
     254      2290108 :   widest_int max_step = wi::mul (widest_int::from (step, sign),
     255      2290108 :                                  nit, sign, &ovf);
     256              : 
     257      2290108 :   if (ovf || !wi::fits_to_tree_p (max_step, type))
     258       119104 :     return true;
     259              : 
     260              :   /* For a signed type we have to check whether the result has the
     261              :      expected signedness which is that of the step as number of
     262              :      iterations is unsigned.  */
     263      2171004 :   return (sign == SIGNED
     264      4341218 :           && wi::gts_p (max_step, 0) != wi::gts_p (step, 0));
     265      2290108 : }
     266              : 
     267              : /* Set R to the range from BEGIN to END, assuming the direction of the
     268              :    loop is DIR.  */
     269              : 
     270              : static void
     271      2730116 : range_from_loop_direction (irange &r, tree type,
     272              :                            const irange &begin, const irange &end,
     273              :                            ev_direction dir)
     274              : {
     275      2730116 :   signop sign = TYPE_SIGN (type);
     276              : 
     277      2730116 :   if (begin.undefined_p () || end.undefined_p ())
     278         2664 :     r.set_varying (type);
     279      2727452 :   else if (dir == EV_DIR_GROWS)
     280              :     {
     281      2445811 :       if (wi::ge_p (begin.lower_bound (), end.upper_bound (), sign))
     282          198 :         r.set_varying (type);
     283              :       else
     284      2445613 :         r = int_range<1> (type, begin.lower_bound (), end.upper_bound ());
     285              :     }
     286              :   else
     287              :     {
     288       281661 :       if (wi::ge_p (end.lower_bound (), begin.upper_bound (), sign))
     289          226 :         r.set_varying (type);
     290              :       else
     291       281435 :         r = int_range<1> (type, end.lower_bound (), begin.upper_bound ());
     292              :     }
     293      2730116 : }
     294              : 
     295              : /* Set V to the range of NAME in STMT within LOOP.  Return TRUE if a
     296              :    range was found.  */
     297              : 
     298              : bool
     299      6709377 : range_of_var_in_loop (vrange &v, tree name, class loop *l, gimple *stmt,
     300              :                       range_query *query)
     301              : {
     302      6709377 :   tree init, step;
     303      6709377 :   enum ev_direction dir;
     304      6709377 :   if (!get_scev_info (v, name, stmt, l, init, step, dir))
     305              :     return true;
     306              : 
     307              :   // Calculate ranges for the values from SCEV.
     308      2730409 :   irange &r = as_a <irange> (v);
     309      2730409 :   tree type = TREE_TYPE (init);
     310      2730409 :   int_range<2> rinit (type), rstep (type), max_init (type);
     311      2730409 :   if (!query->range_of_expr (rinit, init, stmt)
     312      2730409 :       || !query->range_of_expr (rstep, step, stmt))
     313            0 :     return false;
     314              : 
     315              :   // Calculate the final range of NAME if possible.
     316      2730409 :   if (rinit.singleton_p () && rstep.singleton_p ())
     317              :     {
     318      2290401 :       widest_int nit;
     319      2290401 :       if (!max_loop_iterations (l, &nit))
     320              :         return false;
     321              : 
     322      2290108 :       if (!induction_variable_may_overflow_p (type, rstep.lower_bound (), nit))
     323              :         {
     324              :           // Calculate the max bounds for init (init + niter * step).
     325      2170214 :           wide_int w = wide_int::from (nit, TYPE_PRECISION (type), TYPE_SIGN (type));
     326      2170214 :           int_range<1> niter (type, w, w);
     327      2170214 :           int_range_max max_step;
     328      2170214 :           range_op_handler mult_handler (MULT_EXPR);
     329      2170214 :           range_op_handler plus_handler (PLUS_EXPR);
     330      2170214 :           if (!mult_handler.fold_range (max_step, type, niter, rstep)
     331      2170214 :               || !plus_handler.fold_range (max_init, type, rinit, max_step))
     332            0 :             return false;
     333      2170214 :         }
     334      2290401 :     }
     335      2730116 :   range_from_loop_direction (r, type, rinit, max_init, dir);
     336      2730116 :   return true;
     337      2730409 : }
     338              : 
     339              : /* Helper function for vrp_evaluate_conditional_warnv & other
     340              :    optimizers.  */
     341              : 
     342              : tree
     343     20264277 : simplify_using_ranges::fold_cond_with_ops (enum tree_code code,
     344              :                                            tree op0, tree op1, gimple *s)
     345              : {
     346     20264277 :   value_range r0 (TREE_TYPE (op0));
     347     20264277 :   value_range r1 (TREE_TYPE (op1));
     348     20264277 :   if (!query->range_of_expr (r0, op0, s)
     349     20264277 :       || !query->range_of_expr (r1, op1, s))
     350         6684 :     return NULL_TREE;
     351              : 
     352     20257593 :   int_range<1> res;
     353     20257593 :   range_op_handler handler (code);
     354              : 
     355              :   // Find any relation between op0 and op1 and pass it to fold_range.
     356     20257593 :   relation_kind rel = VREL_VARYING;
     357     20257593 :   if (gimple_range_ssa_p (op0) && gimple_range_ssa_p (op1))
     358      4544011 :     rel = query->relation ().query (s, op0, op1);
     359              : 
     360     20257593 :   if (handler && handler.fold_range (res, boolean_type_node, r0, r1,
     361              :                                      relation_trio::op1_op2 (rel)))
     362              :     {
     363     20257593 :       if (res == range_true ())
     364         8753 :         return boolean_true_node;
     365     20248840 :       if (res == range_false ())
     366         5031 :         return boolean_false_node;
     367              :     }
     368              :   return NULL;
     369     20264277 : }
     370              : 
     371              : /* Helper function for legacy_fold_cond.  */
     372              : 
     373              : tree
     374     20614659 : simplify_using_ranges::legacy_fold_cond_overflow (gimple *stmt)
     375              : {
     376     20614659 :   tree ret;
     377     20614659 :   tree_code code = gimple_cond_code (stmt);
     378     20614659 :   tree op0 = gimple_cond_lhs (stmt);
     379     20614659 :   tree op1 = gimple_cond_rhs (stmt);
     380              : 
     381              :   /* We only deal with integral and pointer types.  */
     382     40877170 :   if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
     383     25170148 :       && !POINTER_TYPE_P (TREE_TYPE (op0)))
     384              :     return NULL_TREE;
     385              : 
     386              :   /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
     387              :      as a simple equality test, then prefer that over its current form
     388              :      for evaluation.
     389              : 
     390              :      An overflow test which collapses to an equality test can always be
     391              :      expressed as a comparison of one argument against zero.  Overflow
     392              :      occurs when the chosen argument is zero and does not occur if the
     393              :      chosen argument is not zero.  */
     394     19774751 :   tree x;
     395     19774751 :   if (overflow_comparison_p (code, op0, op1, &x))
     396              :     {
     397         1204 :       wide_int max = wi::max_value (TYPE_PRECISION (TREE_TYPE (op0)), UNSIGNED);
     398              :       /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
     399              :          B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
     400              :          B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
     401              :          B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
     402         1204 :       if (integer_zerop (x))
     403              :         {
     404          219 :           op1 = x;
     405          219 :           code = (code == LT_EXPR || code == LE_EXPR) ? EQ_EXPR : NE_EXPR;
     406              :         }
     407              :       /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
     408              :          B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
     409              :          B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
     410              :          B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
     411          985 :       else if (wi::to_wide (x) == max - 1)
     412              :         {
     413          645 :           op0 = op1;
     414          645 :           op1 = wide_int_to_tree (TREE_TYPE (op0), 0);
     415          645 :           code = (code == GT_EXPR || code == GE_EXPR) ? EQ_EXPR : NE_EXPR;
     416              :         }
     417              :       else
     418              :         {
     419          340 :           int_range_max vro, vri;
     420          340 :           tree type = TREE_TYPE (op0);
     421          340 :           if (code == GT_EXPR || code == GE_EXPR)
     422              :             {
     423          118 :               vro.set (type,
     424          236 :                        wi::to_wide (TYPE_MIN_VALUE (type)),
     425          118 :                        wi::to_wide (x), VR_ANTI_RANGE);
     426          118 :               vri.set (type,
     427          236 :                        wi::to_wide (TYPE_MIN_VALUE (type)),
     428          236 :                        wi::to_wide (x));
     429              :             }
     430          222 :           else if (code == LT_EXPR || code == LE_EXPR)
     431              :             {
     432          222 :               vro.set (type,
     433          444 :                        wi::to_wide (TYPE_MIN_VALUE (type)),
     434          222 :                        wi::to_wide (x));
     435          222 :               vri.set (type,
     436          444 :                        wi::to_wide (TYPE_MIN_VALUE (type)),
     437          444 :                        wi::to_wide (x),
     438              :                        VR_ANTI_RANGE);
     439              :             }
     440              :           else
     441            0 :             gcc_unreachable ();
     442          340 :           int_range_max vr0;
     443          340 :           if (!query->range_of_expr (vr0, op0, stmt))
     444            0 :             vr0.set_varying (TREE_TYPE (op0));
     445              :           /* If vro, the range for OP0 to pass the overflow test, has
     446              :              no intersection with *vr0, OP0's known range, then the
     447              :              overflow test can't pass, so return the node for false.
     448              :              If it is the inverted range, vri, that has no
     449              :              intersection, then the overflow test must pass, so return
     450              :              the node for true.  In other cases, we could proceed with
     451              :              a simplified condition comparing OP0 and X, with LE_EXPR
     452              :              for previously LE_ or LT_EXPR and GT_EXPR otherwise, but
     453              :              the comments next to the enclosing if suggest it's not
     454              :              generally profitable to do so.  */
     455          340 :           vro.intersect (vr0);
     456          340 :           if (vro.undefined_p ())
     457            8 :             return boolean_false_node;
     458          332 :           vri.intersect (vr0);
     459          332 :           if (vri.undefined_p ())
     460            0 :             return boolean_true_node;
     461          340 :         }
     462         1204 :     }
     463              : 
     464     19774743 :   if ((ret = fold_cond_with_ops (code, op0, op1, stmt)))
     465              :     return ret;
     466              :   return NULL_TREE;
     467              : }
     468              : 
     469              : /* Visit conditional statement STMT.  If we can determine which edge
     470              :    will be taken out of STMT's basic block, record it in
     471              :    *TAKEN_EDGE_P.  Otherwise, set *TAKEN_EDGE_P to NULL.  */
     472              : 
     473              : void
     474     20614659 : simplify_using_ranges::legacy_fold_cond (gcond *stmt, edge *taken_edge_p)
     475              : {
     476     20614659 :   tree val;
     477              : 
     478     20614659 :   *taken_edge_p = NULL;
     479              : 
     480     20614659 :   if (dump_file && (dump_flags & TDF_DETAILS))
     481              :     {
     482          384 :       tree use;
     483          384 :       ssa_op_iter i;
     484              : 
     485          384 :       fprintf (dump_file, "\nVisiting conditional with predicate: ");
     486          384 :       print_gimple_stmt (dump_file, stmt, 0);
     487          384 :       fprintf (dump_file, "\nWith known ranges\n");
     488              : 
     489          859 :       FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
     490              :         {
     491          475 :           fprintf (dump_file, "\t");
     492          475 :           print_generic_expr (dump_file, use);
     493          475 :           fprintf (dump_file, ": ");
     494          475 :           value_range r (TREE_TYPE (use));
     495          475 :           query->range_of_expr (r, use, stmt);
     496          475 :           r.dump (dump_file);
     497          475 :         }
     498              : 
     499          384 :       fprintf (dump_file, "\n");
     500              :     }
     501              : 
     502     20614659 :   val = legacy_fold_cond_overflow (stmt);
     503     20614659 :   if (val)
     504            8 :     *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
     505              : 
     506     20614659 :   if (dump_file && (dump_flags & TDF_DETAILS))
     507              :     {
     508          384 :       fprintf (dump_file, "\nPredicate evaluates to: ");
     509          384 :       if (val == NULL_TREE)
     510          384 :         fprintf (dump_file, "DON'T KNOW\n");
     511              :       else
     512            0 :         print_generic_stmt (dump_file, val);
     513              :     }
     514     20614659 : }
     515              : 
     516              : /* Simplify boolean operations if the source is known
     517              :    to be already a boolean.  */
     518              : bool
     519       458744 : simplify_using_ranges::simplify_truth_ops_using_ranges
     520              :                                         (gimple_stmt_iterator *gsi,
     521              :                                          gimple *stmt)
     522              : {
     523       458744 :   enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
     524       458744 :   tree lhs, op0, op1;
     525       458744 :   bool need_conversion;
     526              : 
     527              :   /* We handle only !=/== case here.  */
     528       458744 :   gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
     529              : 
     530       458744 :   op0 = gimple_assign_rhs1 (stmt);
     531       458744 :   if (!op_with_boolean_value_range_p (op0, stmt))
     532              :     return false;
     533              : 
     534        15045 :   op1 = gimple_assign_rhs2 (stmt);
     535        15045 :   if (!op_with_boolean_value_range_p (op1, stmt))
     536              :     return false;
     537              : 
     538              :   /* Reduce number of cases to handle to NE_EXPR.  As there is no
     539              :      BIT_XNOR_EXPR we cannot replace A == B with a single statement.  */
     540        14606 :   if (rhs_code == EQ_EXPR)
     541              :     {
     542         6728 :       if (TREE_CODE (op1) == INTEGER_CST)
     543         2414 :         op1 = int_const_binop (BIT_XOR_EXPR, op1,
     544         4828 :                                build_int_cst (TREE_TYPE (op1), 1));
     545              :       else
     546              :         return false;
     547              :     }
     548              : 
     549        10292 :   lhs = gimple_assign_lhs (stmt);
     550        10292 :   need_conversion
     551        10292 :     = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
     552              : 
     553              :   /* Make sure to not sign-extend a 1-bit 1 when converting the result.  */
     554        10292 :   if (need_conversion
     555         8944 :       && !TYPE_UNSIGNED (TREE_TYPE (op0))
     556         3703 :       && TYPE_PRECISION (TREE_TYPE (op0)) == 1
     557        10315 :       && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
     558              :     return false;
     559              : 
     560              :   /* For A != 0 we can substitute A itself.  */
     561        10292 :   if (integer_zerop (op1))
     562         6824 :     gimple_assign_set_rhs_with_ops (gsi,
     563              :                                     need_conversion
     564            0 :                                     ? NOP_EXPR : TREE_CODE (op0), op0);
     565              :   /* For A != B we substitute A ^ B.  Either with conversion.  */
     566         3468 :   else if (need_conversion)
     567              :     {
     568         2120 :       tree tem = make_ssa_name (TREE_TYPE (op0));
     569         2120 :       gassign *newop
     570         2120 :         = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
     571         2120 :       gsi_insert_before (gsi, newop, GSI_SAME_STMT);
     572         4212 :       if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
     573         4212 :           && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
     574              :         {
     575         4056 :           int_range<1> vr (TREE_TYPE (tem),
     576         4056 :                            wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
     577         4056 :                            wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
     578         2028 :           set_range_info (tem, vr);
     579         2028 :         }
     580         2120 :       gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
     581              :     }
     582              :   /* Or without.  */
     583              :   else
     584         1348 :     gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
     585        10292 :   update_stmt (gsi_stmt (*gsi));
     586        10292 :   fold_stmt (gsi, follow_single_use_edges);
     587              : 
     588        10292 :   return true;
     589              : }
     590              : 
     591              : /* Simplify a division or modulo operator to a right shift or bitwise and
     592              :    if the first operand is unsigned or is greater than zero and the second
     593              :    operand is an exact power of two.  For TRUNC_MOD_EXPR op0 % op1 with
     594              :    constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
     595              :    optimize it into just op0 if op0's range is known to be a subset of
     596              :    [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
     597              :    modulo.  */
     598              : 
     599              : bool
     600       313574 : simplify_using_ranges::simplify_div_or_mod_using_ranges
     601              :                                         (gimple_stmt_iterator *gsi,
     602              :                                          gimple *stmt)
     603              : {
     604       313574 :   enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
     605       313574 :   tree val = NULL;
     606       313574 :   tree op0 = gimple_assign_rhs1 (stmt);
     607       313574 :   tree op1 = gimple_assign_rhs2 (stmt);
     608       313574 :   tree op0min = NULL_TREE, op0max = NULL_TREE;
     609       313574 :   tree op1min = op1;
     610       313574 :   int_range_max vr;
     611              : 
     612       313574 :   if (TREE_CODE (op0) == INTEGER_CST)
     613              :     {
     614              :       op0min = op0;
     615              :       op0max = op0;
     616              :     }
     617              :   else
     618              :     {
     619       288588 :       if (!query->range_of_expr (vr, op0, stmt))
     620            0 :         vr.set_varying (TREE_TYPE (op0));
     621       288588 :       if (!vr.varying_p () && !vr.undefined_p ())
     622              :         {
     623       136795 :           tree type = vr.type ();
     624       136795 :           op0min = wide_int_to_tree (type, vr.lower_bound ());
     625       136804 :           op0max = wide_int_to_tree (type, vr.upper_bound ());
     626              :         }
     627              :     }
     628              : 
     629       313574 :   if (rhs_code == TRUNC_MOD_EXPR
     630       146055 :       && TREE_CODE (op1) == SSA_NAME)
     631              :     {
     632        90417 :       int_range_max vr1;
     633        90417 :       if (!query->range_of_expr (vr1, op1, stmt))
     634            0 :         vr1.set_varying (TREE_TYPE (op1));
     635        90417 :       if (!vr1.varying_p () && !vr1.undefined_p ())
     636        25930 :         op1min = wide_int_to_tree (vr1.type (), vr1.lower_bound ());
     637        90417 :     }
     638       146055 :   if (rhs_code == TRUNC_MOD_EXPR
     639       146055 :       && TREE_CODE (op1min) == INTEGER_CST
     640        81564 :       && tree_int_cst_sgn (op1min) == 1
     641        60820 :       && op0max
     642        34999 :       && tree_int_cst_lt (op0max, op1min))
     643              :     {
     644         1208 :       if (TYPE_UNSIGNED (TREE_TYPE (op0))
     645          535 :           || tree_int_cst_sgn (op0min) >= 0
     646         1434 :           || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1min), op1min),
     647              :                               op0min))
     648              :         {
     649              :           /* If op0 already has the range op0 % op1 has,
     650              :              then TRUNC_MOD_EXPR won't change anything.  */
     651         1175 :           gimple_assign_set_rhs_from_tree (gsi, op0);
     652         1175 :           return true;
     653              :         }
     654              :     }
     655              : 
     656       312399 :   if (TREE_CODE (op0) != SSA_NAME)
     657              :     return false;
     658              : 
     659       287414 :   if (!integer_pow2p (op1))
     660              :     {
     661              :       /* X % -Y can be only optimized into X % Y either if
     662              :          X is not INT_MIN, or Y is not -1.  Fold it now, as after
     663              :          remove_range_assertions the range info might be not available
     664              :          anymore.  */
     665       247589 :       if (rhs_code == TRUNC_MOD_EXPR
     666       247589 :           && fold_stmt (gsi, follow_single_use_edges))
     667              :         return true;
     668       247577 :       return false;
     669              :     }
     670              : 
     671        39825 :   if (TYPE_UNSIGNED (TREE_TYPE (op0)))
     672            0 :     val = integer_one_node;
     673              :   else
     674              :     {
     675        39825 :       tree zero = build_zero_cst (TREE_TYPE (op0));
     676        39825 :       val = fold_cond_with_ops (GE_EXPR, op0, zero, stmt);
     677              :     }
     678              : 
     679        39825 :   if (val && integer_onep (val))
     680              :     {
     681         6846 :       tree t;
     682              : 
     683         6846 :       if (rhs_code == TRUNC_DIV_EXPR)
     684              :         {
     685         4471 :           t = build_int_cst (integer_type_node, tree_log2 (op1));
     686         4471 :           gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
     687         4471 :           gimple_assign_set_rhs1 (stmt, op0);
     688         4471 :           gimple_assign_set_rhs2 (stmt, t);
     689              :         }
     690              :       else
     691              :         {
     692         2375 :           t = build_int_cst (TREE_TYPE (op1), 1);
     693         2375 :           t = int_const_binop (MINUS_EXPR, op1, t);
     694         2375 :           t = fold_convert (TREE_TYPE (op0), t);
     695              : 
     696         2375 :           gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
     697         2375 :           gimple_assign_set_rhs1 (stmt, op0);
     698         2375 :           gimple_assign_set_rhs2 (stmt, t);
     699              :         }
     700              : 
     701         6846 :       update_stmt (stmt);
     702         6846 :       fold_stmt (gsi, follow_single_use_edges);
     703         6846 :       return true;
     704              :     }
     705              : 
     706              :   return false;
     707       313574 : }
     708              : 
     709              : /* Simplify a min or max if the ranges of the two operands are
     710              :    disjoint.   Return true if we do simplify.  */
     711              : 
     712              : bool
     713       218211 : simplify_using_ranges::simplify_min_or_max_using_ranges
     714              :                                 (gimple_stmt_iterator *gsi,
     715              :                                  gimple *stmt)
     716              : {
     717       218211 :   tree op0 = gimple_assign_rhs1 (stmt);
     718       218211 :   tree op1 = gimple_assign_rhs2 (stmt);
     719       218211 :   tree val;
     720              : 
     721       218211 :   val = fold_cond_with_ops (LE_EXPR, op0, op1, stmt);
     722       218211 :   if (!val)
     723       212744 :     val = fold_cond_with_ops (LT_EXPR, op0, op1, stmt);
     724              : 
     725       212744 :   if (val)
     726              :     {
     727              :       /* VAL == TRUE -> OP0 < or <= op1
     728              :          VAL == FALSE -> OP0 > or >= op1.  */
     729         6468 :       tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
     730         6468 :                   == integer_zerop (val)) ? op0 : op1;
     731         6468 :       gimple_assign_set_rhs_from_tree (gsi, res);
     732         6468 :       return true;
     733              :     }
     734              : 
     735              :   return false;
     736              : }
     737              : 
     738              : /* If the operand to an ABS_EXPR is >= 0, then eliminate the
     739              :    ABS_EXPR.  If the operand is <= 0, then simplify the
     740              :    ABS_EXPR into a NEGATE_EXPR.  */
     741              : 
     742              : bool
     743         9512 : simplify_using_ranges::simplify_abs_using_ranges (gimple_stmt_iterator *gsi,
     744              :                                                   gimple *stmt)
     745              : {
     746         9512 :   tree op = gimple_assign_rhs1 (stmt);
     747         9512 :   tree zero = build_zero_cst (TREE_TYPE (op));
     748         9512 :   tree val = fold_cond_with_ops (LE_EXPR, op, zero, stmt);
     749              : 
     750         9512 :   if (!val)
     751              :     {
     752              :       /* The range is neither <= 0 nor > 0.  Now see if it is
     753              :          either < 0 or >= 0.  */
     754         9242 :       val = fold_cond_with_ops (LT_EXPR, op, zero, stmt);
     755              :     }
     756         9242 :   if (val)
     757              :     {
     758          469 :       gimple_assign_set_rhs1 (stmt, op);
     759          469 :       if (integer_zerop (val))
     760          297 :         gimple_assign_set_rhs_code (stmt, SSA_NAME);
     761              :       else
     762          172 :         gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
     763          469 :       update_stmt (stmt);
     764          469 :       fold_stmt (gsi, follow_single_use_edges);
     765          469 :       return true;
     766              :     }
     767              :   return false;
     768              : }
     769              : 
     770              : /* irange wrapper for wi_set_zero_nonzero_bits.
     771              : 
     772              :    Return TRUE if VR was a constant range and we were able to compute
     773              :    the bit masks.  */
     774              : 
     775              : static bool
     776      1592325 : vr_set_zero_nonzero_bits (const tree expr_type,
     777              :                           const irange *vr,
     778              :                           wide_int *may_be_nonzero,
     779              :                           wide_int *must_be_nonzero)
     780              : {
     781      1592325 :   if (vr->varying_p () || vr->undefined_p ())
     782              :     {
     783       926922 :       *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
     784       926922 :       *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
     785       926922 :       return false;
     786              :     }
     787       665405 :   wi_set_zero_nonzero_bits (expr_type, vr->lower_bound (), vr->upper_bound (),
     788              :                             *may_be_nonzero, *must_be_nonzero);
     789       665403 :   return true;
     790              : }
     791              : 
     792              : /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
     793              :    If all the bits that are being cleared by & are already
     794              :    known to be zero from VR, or all the bits that are being
     795              :    set by | are already known to be one from VR, the bit
     796              :    operation is redundant.  */
     797              : 
     798              : bool
     799      1238716 : simplify_using_ranges::simplify_bit_ops_using_ranges
     800              :                                 (gimple_stmt_iterator *gsi,
     801              :                                  gimple *stmt)
     802              : {
     803      1238716 :   tree op0 = gimple_assign_rhs1 (stmt);
     804      1238716 :   tree op1 = gimple_assign_rhs2 (stmt);
     805      1238716 :   tree op = NULL_TREE;
     806      1238716 :   int_range_max vr0, vr1;
     807      1238716 :   wide_int may_be_nonzero0, may_be_nonzero1;
     808      1238716 :   wide_int must_be_nonzero0, must_be_nonzero1;
     809      1238716 :   wide_int mask;
     810              : 
     811      1238716 :   if (!query->range_of_expr (vr0, op0, stmt)
     812      1238716 :       || vr0.undefined_p ())
     813              :     return false;
     814      1238041 :   if (!query->range_of_expr (vr1, op1, stmt)
     815      1238041 :       || vr1.undefined_p ())
     816              :     return false;
     817              : 
     818      1237694 :   if (!vr_set_zero_nonzero_bits (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
     819              :                                  &must_be_nonzero0))
     820              :     return false;
     821       354631 :   if (!vr_set_zero_nonzero_bits (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
     822              :                                  &must_be_nonzero1))
     823              :     return false;
     824              : 
     825       310772 :   switch (gimple_assign_rhs_code (stmt))
     826              :     {
     827       221150 :     case BIT_AND_EXPR:
     828       221150 :       mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
     829       221150 :       if (mask == 0)
     830              :         {
     831              :           op = op0;
     832              :           break;
     833              :         }
     834       216657 :       mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
     835       216657 :       if (mask == 0)
     836              :         {
     837              :           op = op1;
     838              :           break;
     839              :         }
     840              :       break;
     841        89622 :     case BIT_IOR_EXPR:
     842        89622 :       mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
     843        89622 :       if (mask == 0)
     844              :         {
     845              :           op = op1;
     846              :           break;
     847              :         }
     848        89622 :       mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
     849        89622 :       if (mask == 0)
     850              :         {
     851              :           op = op0;
     852              :           break;
     853              :         }
     854              :       break;
     855            0 :     default:
     856            0 :       gcc_unreachable ();
     857              :     }
     858              : 
     859         4695 :   if (op == NULL_TREE)
     860       306077 :     return false;
     861              : 
     862         4695 :   gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
     863         4695 :   update_stmt (gsi_stmt (*gsi));
     864         4695 :   return true;
     865      1238728 : }
     866              : 
     867              : /* We are comparing trees OP0 and OP1 using COND_CODE.  OP0 has
     868              :    a known value range VR.
     869              : 
     870              :    If there is one and only one value which will satisfy the
     871              :    conditional, then return that value.  Else return NULL.  */
     872              : 
     873              : static tree
     874      1565906 : test_for_singularity (enum tree_code cond_code, tree op0,
     875              :                       tree op1, const irange *vr)
     876              : {
     877      1565906 :   tree min = NULL;
     878      1565906 :   tree max = NULL;
     879              : 
     880              :   /* Extract minimum/maximum values which satisfy the conditional as it was
     881              :      written.  */
     882      1565906 :   if (cond_code == LE_EXPR || cond_code == LT_EXPR)
     883              :     {
     884       681588 :       min = TYPE_MIN_VALUE (TREE_TYPE (op0));
     885              : 
     886       681588 :       max = op1;
     887       681588 :       if (cond_code == LT_EXPR)
     888              :         {
     889        92705 :           tree one = build_int_cst (TREE_TYPE (op0), 1);
     890        92705 :           max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
     891              :         }
     892              :     }
     893       884318 :   else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
     894              :     {
     895       761709 :       max = TYPE_MAX_VALUE (TREE_TYPE (op0));
     896              : 
     897       761709 :       min = op1;
     898       761709 :       if (cond_code == GT_EXPR)
     899              :         {
     900       670575 :           tree one = build_int_cst (TREE_TYPE (op0), 1);
     901       670575 :           min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
     902              :         }
     903              :     }
     904              : 
     905              :   /* Now refine the minimum and maximum values using any
     906              :      value range information we have for op0.  */
     907      1565906 :   if (min && max)
     908              :     {
     909      1443297 :       tree type = TREE_TYPE (op0);
     910      1443297 :       tree tmin = wide_int_to_tree (type, vr->lower_bound ());
     911      1443297 :       tree tmax = wide_int_to_tree (type, vr->upper_bound ());
     912      1443297 :       if (compare_values (tmin, min) == 1)
     913       438041 :         min = tmin;
     914      1443297 :       if (compare_values (tmax, max) == -1)
     915       550496 :         max = tmax;
     916              : 
     917              :       /* If the new min/max values have converged to a single value,
     918              :          then there is only one value which can satisfy the condition,
     919              :          return that value.  */
     920      1443297 :       if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
     921              :         return min;
     922              :     }
     923              :   return NULL;
     924              : }
     925              : 
     926              : /* Return whether the value range *VR fits in an integer type specified
     927              :    by PRECISION and UNSIGNED_P.  */
     928              : 
     929              : bool
     930       250537 : range_fits_type_p (const irange *vr,
     931              :                    unsigned dest_precision, signop dest_sgn)
     932              : {
     933       250537 :   tree src_type;
     934       250537 :   unsigned src_precision;
     935       250537 :   widest_int tem;
     936       250537 :   signop src_sgn;
     937              : 
     938              :   /* Now we can only handle ranges with constant bounds.  */
     939       250537 :   if (vr->undefined_p () || vr->varying_p ())
     940              :     return false;
     941              : 
     942              :   /* We can only handle integral and pointer types.  */
     943       194755 :   src_type = vr->type ();
     944       194755 :   if (!INTEGRAL_TYPE_P (src_type)
     945            0 :       && !POINTER_TYPE_P (src_type))
     946              :     return false;
     947              : 
     948              :   /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
     949              :      and so is an identity transform.  */
     950       194755 :   src_precision = TYPE_PRECISION (src_type);
     951       194755 :   src_sgn = TYPE_SIGN (src_type);
     952       194755 :   if ((src_precision < dest_precision
     953        10765 :        && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
     954       185341 :       || (src_precision == dest_precision && src_sgn == dest_sgn))
     955              :     return true;
     956              : 
     957       185189 :   wide_int vrmin = vr->lower_bound ();
     958       185189 :   wide_int vrmax = vr->upper_bound ();
     959              : 
     960              :   /* For sign changes, the MSB of the wide_int has to be clear.
     961              :      An unsigned value with its MSB set cannot be represented by
     962              :      a signed wide_int, while a negative value cannot be represented
     963              :      by an unsigned wide_int.  */
     964       185189 :   if (src_sgn != dest_sgn
     965       185189 :       && (wi::lts_p (vrmin, 0) || wi::lts_p (vrmax, 0)))
     966        68493 :     return false;
     967              : 
     968              :   /* Then we can perform the conversion on both ends and compare
     969              :      the result for equality.  */
     970       116696 :   signop sign = TYPE_SIGN (vr->type ());
     971       116696 :   tem = wi::ext (widest_int::from (vrmin, sign), dest_precision, dest_sgn);
     972       116696 :   if (tem != widest_int::from (vrmin, sign))
     973              :     return false;
     974       112440 :   tem = wi::ext (widest_int::from (vrmax, sign), dest_precision, dest_sgn);
     975       112440 :   if (tem != widest_int::from (vrmax, sign))
     976              :     return false;
     977              : 
     978              :   return true;
     979       185189 : }
     980              : 
     981              : // Clear edge E of EDGE_EXECUTABLE (it is unexecutable). If it wasn't
     982              : // previously clear, propagate to successor blocks if appropriate.
     983              : 
     984              : void
     985       302140 : simplify_using_ranges::set_and_propagate_unexecutable (edge e)
     986              : {
     987              :   // If not_executable is already set, we're done.
     988              :   // This works in the absence of a flag as well.
     989       302140 :   if ((e->flags & m_not_executable_flag) == m_not_executable_flag)
     990       209410 :     return;
     991              : 
     992       202960 :   e->flags |= m_not_executable_flag;
     993       202960 :   m_flag_set_edges.safe_push (e);
     994              : 
     995              :   // Check if the destination block needs to propagate the property.
     996       202960 :   basic_block bb = e->dest;
     997              : 
     998              :   // If any incoming edge is executable, we are done.
     999       202960 :   edge_iterator ei;
    1000       335128 :   FOR_EACH_EDGE (e, ei, bb->preds)
    1001       242398 :     if ((e->flags & m_not_executable_flag) == 0)
    1002              :       return;
    1003              : 
    1004              :   // This block is also unexecutable, propagate to all exit edges as well.
    1005       157622 :   FOR_EACH_EDGE (e, ei, bb->succs)
    1006        64892 :     set_and_propagate_unexecutable (e);
    1007              : }
    1008              : 
    1009              : /* If COND can be folded entirely as TRUE or FALSE, rewrite the
    1010              :    conditional as such, and return TRUE.  */
    1011              : 
    1012              : bool
    1013     20929079 : simplify_using_ranges::fold_cond (gcond *cond)
    1014              : {
    1015     20929079 :   int_range_max r;
    1016     20929079 :   if (query->range_of_stmt (r, cond) && r.singleton_p ())
    1017              :     {
    1018              :       // COND has already been folded if arguments are constant.
    1019       314420 :       if (TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME
    1020       314420 :           && TREE_CODE (gimple_cond_rhs (cond)) != SSA_NAME)
    1021              :         return false;
    1022       230262 :       if (dump_file)
    1023              :         {
    1024          183 :           fprintf (dump_file, "Folding predicate ");
    1025          183 :           print_gimple_expr (dump_file, cond, 0);
    1026          183 :           fprintf (dump_file, " to ");
    1027              :         }
    1028       230262 :       edge e0 = EDGE_SUCC (gimple_bb (cond), 0);
    1029       230262 :       edge e1 = EDGE_SUCC (gimple_bb (cond), 1);
    1030       230262 :       if (r.zero_p ())
    1031              :         {
    1032       139093 :           if (dump_file)
    1033          129 :             fprintf (dump_file, "0\n");
    1034       139093 :           gimple_cond_make_false (cond);
    1035       139093 :           if (e0->flags & EDGE_TRUE_VALUE)
    1036       137322 :             set_and_propagate_unexecutable (e0);
    1037              :           else
    1038         1771 :             set_and_propagate_unexecutable (e1);
    1039              :         }
    1040              :       else
    1041              :         {
    1042        91169 :           if (dump_file)
    1043           54 :             fprintf (dump_file, "1\n");
    1044        91169 :           gimple_cond_make_true (cond);
    1045        91169 :           if (e0->flags & EDGE_FALSE_VALUE)
    1046          700 :             set_and_propagate_unexecutable (e0);
    1047              :           else
    1048        90469 :             set_and_propagate_unexecutable (e1);
    1049              :         }
    1050       230262 :       update_stmt (cond);
    1051       230262 :       return true;
    1052              :     }
    1053              : 
    1054              :   // FIXME: Audit the code below and make sure it never finds anything.
    1055     20614659 :   edge taken_edge;
    1056     20614659 :   legacy_fold_cond (cond, &taken_edge);
    1057              : 
    1058     20614659 :   if (taken_edge)
    1059              :     {
    1060            8 :       if (taken_edge->flags & EDGE_TRUE_VALUE)
    1061              :         {
    1062            0 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1063            0 :             fprintf (dump_file, "\nVRP Predicate evaluates to: 1\n");
    1064            0 :           gimple_cond_make_true (cond);
    1065              :         }
    1066            8 :       else if (taken_edge->flags & EDGE_FALSE_VALUE)
    1067              :         {
    1068            8 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1069            0 :             fprintf (dump_file, "\nVRP Predicate evaluates to: 0\n");
    1070            8 :           gimple_cond_make_false (cond);
    1071              :         }
    1072              :       else
    1073            0 :        gcc_unreachable ();
    1074            8 :       update_stmt (cond);
    1075            8 :       return true;
    1076              :     }
    1077              :   return false;
    1078     20929079 : }
    1079              : 
    1080              : /* Simplify a conditional using a relational operator to an equality
    1081              :    test if the range information indicates only one value can satisfy
    1082              :    the original conditional.  */
    1083              : 
    1084              : bool
    1085     12065474 : simplify_using_ranges::simplify_cond_using_ranges_1 (gcond *stmt)
    1086              : {
    1087     12065474 :   tree op0 = gimple_cond_lhs (stmt);
    1088     12065474 :   tree op1 = gimple_cond_rhs (stmt);
    1089     12065474 :   enum tree_code cond_code = gimple_cond_code (stmt);
    1090              : 
    1091     12065474 :   if (fold_cond (stmt))
    1092              :     return true;
    1093              : 
    1094     11934094 :   if (simplify_compare_using_ranges_1 (cond_code, op0, op1, stmt))
    1095              :     {
    1096       358587 :       if (dump_file)
    1097              :         {
    1098           26 :           fprintf (dump_file, "Simplified relational ");
    1099           26 :           print_gimple_stmt (dump_file, stmt, 0);
    1100           26 :           fprintf (dump_file, " into ");
    1101              :         }
    1102              : 
    1103       358587 :       gimple_cond_set_code (stmt, cond_code);
    1104       358587 :       gimple_cond_set_lhs (stmt, op0);
    1105       358587 :       gimple_cond_set_rhs (stmt, op1);
    1106              : 
    1107       358587 :       update_stmt (stmt);
    1108              : 
    1109       358587 :        if (dump_file)
    1110              :         {
    1111           26 :           print_gimple_stmt (dump_file, stmt, 0);
    1112           26 :           fprintf (dump_file, "\n");
    1113              :         }
    1114       358587 :       return true;
    1115              :     }
    1116              :   return false;
    1117              : }
    1118              : 
    1119              : /* Like simplify_cond_using_ranges_1 but for assignments rather
    1120              :    than GIMPLE_COND. */
    1121              : 
    1122              : bool
    1123      1226227 : simplify_using_ranges::simplify_compare_assign_using_ranges_1
    1124              :                                         (gimple_stmt_iterator *gsi,
    1125              :                                          gimple *stmt)
    1126              : {
    1127      1226227 :   enum tree_code code = gimple_assign_rhs_code (stmt);
    1128      1226227 :   tree op0 = gimple_assign_rhs1 (stmt);
    1129      1226227 :   tree op1 = gimple_assign_rhs2 (stmt);
    1130      1226227 :   gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
    1131      1226227 :   bool happened = false;
    1132              : 
    1133      1226227 :   if (simplify_compare_using_ranges_1 (code, op0, op1, stmt))
    1134              :     {
    1135         6488 :       if (dump_file)
    1136              :         {
    1137            3 :           fprintf (dump_file, "Simplified relational ");
    1138            3 :           print_gimple_stmt (dump_file, stmt, 0);
    1139            3 :           fprintf (dump_file, " into ");
    1140              :         }
    1141              : 
    1142         6488 :       gimple_assign_set_rhs_code (stmt, code);
    1143         6488 :       gimple_assign_set_rhs1 (stmt, op0);
    1144         6488 :       gimple_assign_set_rhs2 (stmt, op1);
    1145              : 
    1146         6488 :       update_stmt (stmt);
    1147              : 
    1148         6488 :        if (dump_file)
    1149              :         {
    1150            3 :           print_gimple_stmt (dump_file, stmt, 0);
    1151            3 :           fprintf (dump_file, "\n");
    1152              :         }
    1153              :       happened = true;
    1154              :     }
    1155              : 
    1156              :   /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
    1157              :      if the RHS is zero or one, and the LHS are known to be boolean
    1158              :      values.  */
    1159      1226227 :   if ((code == EQ_EXPR || code == NE_EXPR)
    1160       662387 :       && INTEGRAL_TYPE_P (TREE_TYPE (op0))
    1161      1684971 :       && simplify_truth_ops_using_ranges (gsi, stmt))
    1162              :     happened = true;
    1163              : 
    1164      1226227 :   return happened;
    1165              : }
    1166              : 
    1167              : /* Try to simplify OP0 COND_CODE OP1 using a relational operator to an
    1168              :    equality test if the range information indicates only one value can
    1169              :    satisfy the original conditional.   */
    1170              : 
    1171              : bool
    1172     13160321 : simplify_using_ranges::simplify_compare_using_ranges_1 (tree_code &cond_code, tree &op0, tree &op1, gimple *stmt)
    1173              : {
    1174     13160321 :   bool happened = false;
    1175     13160321 :   if (cond_code != NE_EXPR
    1176     13160321 :       && cond_code != EQ_EXPR
    1177      3423049 :       && TREE_CODE (op0) == SSA_NAME
    1178      3422574 :       && INTEGRAL_TYPE_P (TREE_TYPE (op0))
    1179     16241955 :       && is_gimple_min_invariant (op1))
    1180              :     {
    1181      1837235 :       int_range_max vr;
    1182              : 
    1183      1837235 :       if (!query->range_of_expr (vr, op0, stmt))
    1184            0 :         vr.set_undefined ();
    1185              : 
    1186              :       /* If we have range information for OP0, then we might be
    1187              :          able to simplify this conditional. */
    1188      1837235 :       if (!vr.undefined_p () && !vr.varying_p ())
    1189              :         {
    1190       782953 :           tree new_tree = test_for_singularity (cond_code, op0, op1, &vr);
    1191       782953 :           if (new_tree)
    1192              :             {
    1193       122609 :               cond_code = EQ_EXPR;
    1194       122609 :               op1 = new_tree;
    1195       122609 :               happened = true;
    1196              :             }
    1197              : 
    1198              :           /* Try again after inverting the condition.  We only deal
    1199              :              with integral types here, so no need to worry about
    1200              :              issues with inverting FP comparisons.  */
    1201       782953 :           new_tree = test_for_singularity
    1202       782953 :                        (invert_tree_comparison (cond_code, false),
    1203              :                         op0, op1, &vr);
    1204       782953 :           if (new_tree)
    1205              :             {
    1206       173934 :               cond_code = NE_EXPR;
    1207       173934 :               op1 = new_tree;
    1208       173934 :               happened = true;
    1209              :             }
    1210              :         }
    1211      1837235 :     }
    1212              :   // Try to simplify casted conditions.
    1213     13160321 :   if (simplify_casted_compare (cond_code, op0, op1))
    1214        79974 :     happened = true;
    1215     13160321 :   return happened;
    1216              : }
    1217              : 
    1218              : /* Simplify OP0 code OP1 when OP1 is a constant and OP0 was a SSA_NAME
    1219              :    defined by a type conversion. Replacing OP0 with RHS of the type conversion.
    1220              :    Doing so makes the conversion dead which helps subsequent passes.  */
    1221              : 
    1222              : bool
    1223     13160321 : simplify_using_ranges::simplify_casted_compare (tree_code &, tree &op0, tree &op1)
    1224              : {
    1225              : 
    1226              :   /* If we have a comparison of an SSA_NAME (OP0) against a constant,
    1227              :      see if OP0 was set by a type conversion where the source of
    1228              :      the conversion is another SSA_NAME with a range that fits
    1229              :      into the range of OP0's type.
    1230              : 
    1231              :      If so, the conversion is redundant as the earlier SSA_NAME can be
    1232              :      used for the comparison directly if we just massage the constant in the
    1233              :      comparison.  */
    1234     13160321 :   if (TREE_CODE (op0) == SSA_NAME
    1235     13074604 :       && TREE_CODE (op1) == INTEGER_CST)
    1236              :     {
    1237      9179775 :       gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
    1238      9179775 :       tree innerop;
    1239              : 
    1240      9179775 :       if (!is_gimple_assign (def_stmt))
    1241              :         return false;
    1242              : 
    1243      5622952 :       switch (gimple_assign_rhs_code (def_stmt))
    1244              :         {
    1245       467254 :         CASE_CONVERT:
    1246       467254 :           innerop = gimple_assign_rhs1 (def_stmt);
    1247       467254 :           break;
    1248        50133 :         case VIEW_CONVERT_EXPR:
    1249        50133 :           innerop = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
    1250        50133 :           if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop)))
    1251              :             return false;
    1252              :           break;
    1253              :         default:
    1254              :           return false;
    1255              :         }
    1256              : 
    1257       514909 :       if (TREE_CODE (innerop) == SSA_NAME
    1258       514881 :           && !POINTER_TYPE_P (TREE_TYPE (innerop))
    1259       509390 :           && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
    1260      1024286 :           && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
    1261              :         {
    1262       480351 :           int_range_max vr;
    1263              : 
    1264       480351 :           if (query->range_of_expr (vr, innerop)
    1265       480348 :               && !vr.varying_p ()
    1266       163801 :               && !vr.undefined_p ()
    1267       163684 :               && range_fits_type_p (&vr,
    1268       163684 :                                     TYPE_PRECISION (TREE_TYPE (op0)),
    1269       163684 :                                     TYPE_SIGN (TREE_TYPE (op0)))
    1270       560325 :               && int_fits_type_p (op1, TREE_TYPE (innerop)))
    1271              :             {
    1272        79974 :               tree newconst = fold_convert (TREE_TYPE (innerop), op1);
    1273        79974 :               op0 = innerop;
    1274        79974 :               op1 = newconst;
    1275        79974 :               return true;
    1276              :             }
    1277       480351 :         }
    1278              :     }
    1279              :   return false;
    1280              : }
    1281              : 
    1282              : /* Simplify a switch statement using the value range of the switch
    1283              :    argument.  */
    1284              : 
    1285              : bool
    1286        60568 : simplify_using_ranges::simplify_switch_using_ranges (gswitch *stmt)
    1287              : {
    1288        60568 :   tree op = gimple_switch_index (stmt);
    1289        60568 :   tree type = TREE_TYPE (op);
    1290        60568 :   int_range_max op_range (type);
    1291        60568 :   int_range_max default_range (type);
    1292        60568 :   auto_vec<unsigned> cases;
    1293        60568 :   cases.truncate (0);
    1294        60568 :   edge e;
    1295        60568 :   switch_update su;
    1296              : 
    1297              :   // Abort if we don't have a useful range for the switch index.
    1298        60568 :   if (!query->range_of_expr (op_range, op, stmt)
    1299        60568 :       || op_range.varying_p () || op_range.undefined_p ())
    1300              :     return false;
    1301              : 
    1302              :   // Default range starts with full known range of op.
    1303        15789 :   default_range = op_range;
    1304        15789 :   edge default_edge = gimple_switch_default_edge (cfun, stmt);
    1305              : 
    1306        15789 :   unsigned x, lim = gimple_switch_num_labels (stmt);
    1307        94847 :   for (x = 1; x < lim; x++)
    1308              :     {
    1309        80041 :       e = gimple_switch_edge (cfun, stmt, x);
    1310        80041 :       tree label = gimple_switch_label (stmt, x);
    1311              : 
    1312              :       // If this edge is the same as the default edge, do nothing else.
    1313        80041 :       if (e == default_edge)
    1314         6152 :         continue;
    1315              :       // Ada sometimes has mismatched labels and index.  Just bail.
    1316        80035 :       if (TREE_TYPE (CASE_LOW (label)) != type)
    1317          983 :         return false;
    1318              : 
    1319        79052 :       wide_int low = wi::to_wide (CASE_LOW (label));
    1320        79052 :       wide_int high;
    1321              :       // Singleton cases have no CASE_HIGH.
    1322        79052 :       tree tree_high = CASE_HIGH (label);
    1323        79052 :       if (tree_high)
    1324         3315 :         high = wi::to_wide (tree_high);
    1325              :       else
    1326        75737 :         high = low;
    1327              : 
    1328              :       // If the case range is fully contained in op_range, leave the
    1329              :       // case as it is, otherwise adjust the labels.
    1330        79052 :       int_range_max case_range (type, low, high);
    1331        79052 :       if (case_range.intersect (op_range))
    1332              :         {
    1333              :           // If none of the label is in op_range, skip this label.
    1334        50803 :           if (case_range.undefined_p ())
    1335         6146 :             continue;
    1336              : 
    1337              :           // Part of the label is in op_range, but not all of it.  CASE_RANGE
    1338              :           // contains the part that is.   Adjust the case range to
    1339              :           // the new min/max.
    1340        44657 :           if (case_range.lower_bound () != low)
    1341           10 :             CASE_LOW (label) = wide_int_to_tree (type,
    1342           20 :                                                  case_range.lower_bound ());
    1343        44657 :           if (case_range.singleton_p ())
    1344        43189 :             CASE_HIGH (label) = NULL_TREE;
    1345              :           else
    1346         1468 :             if (case_range.upper_bound () != high)
    1347            7 :               CASE_HIGH (label) = wide_int_to_tree (type,
    1348           14 :                                                     case_range.upper_bound ());
    1349              :         }
    1350              :       // Add case label to the keep list.
    1351        72906 :       cases.safe_push (x);
    1352              :       // Remove case_range from needing to be handled by the default.
    1353        72906 :       case_range.invert ();
    1354        72906 :       default_range.intersect (case_range);
    1355        79052 :     }
    1356              : 
    1357              :   // An undefined DEFAULT range means the current default case is not needed.
    1358        14806 :   unsigned idx = default_range.undefined_p () ? 0 : 1;
    1359        14806 :   unsigned vec_size = cases.length () + idx;
    1360        14806 :   if (vec_size == lim)
    1361              :     return false;
    1362              : 
    1363         4052 :   tree vec2 = make_tree_vec (vec_size);
    1364              :   // Add default label if there is one.
    1365         4052 :   if (idx)
    1366              :     {
    1367         3079 :       TREE_VEC_ELT (vec2, 0) = gimple_switch_default_label (stmt);
    1368         3079 :       e = gimple_switch_edge (cfun, stmt, 0);
    1369         3079 :       e->aux =  (void *)-1;
    1370              :     }
    1371              : 
    1372        16624 :   for (x = 0; x < cases.length (); x++)
    1373              :     {
    1374        12572 :       unsigned swi = cases[x];
    1375        12572 :       TREE_VEC_ELT (vec2, idx++) = gimple_switch_label (stmt, swi);
    1376        12572 :       e = gimple_switch_edge (cfun, stmt, swi);
    1377        12572 :       e->aux =  (void *)-1;
    1378              :     }
    1379              : 
    1380              :   /* Queue not needed edges for later removal.  */
    1381         4052 :   edge_iterator ei;
    1382        25813 :   FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
    1383              :     {
    1384        21761 :       if (e->aux == (void *)-1)
    1385              :         {
    1386        14775 :           e->aux = NULL;
    1387        14775 :           continue;
    1388              :         }
    1389              : 
    1390         6986 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1391              :         {
    1392            0 :           fprintf (dump_file, "removing unreachable case label\n");
    1393              :         }
    1394         6986 :       to_remove_edges.safe_push (e);
    1395         6986 :       set_and_propagate_unexecutable (e);
    1396         6986 :       e->flags &= ~EDGE_EXECUTABLE;
    1397         6986 :       e->flags |= EDGE_IGNORE;
    1398              :     }
    1399              : 
    1400              :   /* And queue an update for the stmt.  */
    1401         4052 :   su.stmt = stmt;
    1402         4052 :   su.vec = vec2;
    1403         4052 :   to_update_switch_stmts.safe_push (su);
    1404         4052 :   return true;
    1405        60568 : }
    1406              : 
    1407              : void
    1408     13080947 : simplify_using_ranges::cleanup_edges_and_switches (void)
    1409              : {
    1410     13080947 :   int i;
    1411     13080947 :   edge e;
    1412     13080947 :   switch_update *su;
    1413              : 
    1414              :   /* Clear any edges marked as not executable.  */
    1415     13080947 :   if (m_not_executable_flag)
    1416              :     {
    1417      4420296 :       FOR_EACH_VEC_ELT (m_flag_set_edges, i, e)
    1418       202960 :         e->flags &= ~m_not_executable_flag;
    1419              :     }
    1420              :   /* Remove dead edges from SWITCH_EXPR optimization.  This leaves the
    1421              :      CFG in a broken state and requires a cfg_cleanup run.  */
    1422     13091705 :   FOR_EACH_VEC_ELT (to_remove_edges, i, e)
    1423         6986 :     remove_edge (e);
    1424              : 
    1425              :   /* Update SWITCH_EXPR case label vector.  */
    1426     13084999 :   FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
    1427              :     {
    1428         4052 :       size_t j;
    1429         4052 :       size_t n = TREE_VEC_LENGTH (su->vec);
    1430         4052 :       tree label;
    1431         4052 :       gimple_switch_set_num_labels (su->stmt, n);
    1432        23755 :       for (j = 0; j < n; j++)
    1433        15651 :         gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
    1434              :       /* As we may have replaced the default label with a regular one
    1435              :          make sure to make it a real default label again.  This ensures
    1436              :          optimal expansion.  */
    1437         4052 :       label = gimple_switch_label (su->stmt, 0);
    1438         4052 :       CASE_LOW (label) = NULL_TREE;
    1439         4052 :       CASE_HIGH (label) = NULL_TREE;
    1440              :     }
    1441              : 
    1442     13080947 :   if (!to_remove_edges.is_empty ())
    1443              :     {
    1444         3772 :       free_dominance_info (CDI_DOMINATORS);
    1445         3772 :       loops_state_set (LOOPS_NEED_FIXUP);
    1446              :     }
    1447              : 
    1448     13080947 :   to_remove_edges.release ();
    1449     13080947 :   to_update_switch_stmts.release ();
    1450     13080947 : }
    1451              : 
    1452              : /* Simplify an integral conversion from an SSA name in STMT.  */
    1453              : 
    1454              : static bool
    1455      4709998 : simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
    1456              : {
    1457      4709998 :   tree innerop, middleop, finaltype;
    1458      4709998 :   gimple *def_stmt;
    1459      4709998 :   signop inner_sgn, middle_sgn, final_sgn;
    1460      4709998 :   unsigned inner_prec, middle_prec, final_prec;
    1461      4709998 :   widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
    1462              : 
    1463      4709998 :   finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
    1464      4709998 :   if (!INTEGRAL_TYPE_P (finaltype))
    1465              :     return false;
    1466      4333875 :   middleop = gimple_assign_rhs1 (stmt);
    1467      4333875 :   def_stmt = SSA_NAME_DEF_STMT (middleop);
    1468      4333875 :   if (!is_gimple_assign (def_stmt)
    1469      4333875 :       || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
    1470              :     return false;
    1471       148572 :   innerop = gimple_assign_rhs1 (def_stmt);
    1472       148572 :   if (TREE_CODE (innerop) != SSA_NAME
    1473       148572 :       || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
    1474              :     return false;
    1475              : 
    1476              :   /* Get the value-range of the inner operand.  Use global ranges in
    1477              :      case innerop was created during substitute-and-fold.  */
    1478       145319 :   wide_int imin, imax;
    1479       145319 :   int_range_max vr;
    1480       145319 :   if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop)))
    1481              :     return false;
    1482       242530 :   get_range_query (cfun)->range_of_expr (vr, innerop, stmt);
    1483       121265 :   if (vr.undefined_p () || vr.varying_p ())
    1484              :     return false;
    1485        69037 :   innermin = widest_int::from (vr.lower_bound (), TYPE_SIGN (TREE_TYPE (innerop)));
    1486        69037 :   innermax = widest_int::from (vr.upper_bound (), TYPE_SIGN (TREE_TYPE (innerop)));
    1487              : 
    1488              :   /* Simulate the conversion chain to check if the result is equal if
    1489              :      the middle conversion is removed.  */
    1490        69037 :   inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
    1491        69037 :   middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
    1492        69037 :   final_prec = TYPE_PRECISION (finaltype);
    1493              : 
    1494              :   /* If the first conversion is not injective, the second must not
    1495              :      be widening.  */
    1496        69037 :   if (wi::gtu_p (innermax - innermin,
    1497       138074 :                  wi::mask <widest_int> (middle_prec, false))
    1498        69037 :       && middle_prec < final_prec)
    1499              :     return false;
    1500              :   /* We also want a medium value so that we can track the effect that
    1501              :      narrowing conversions with sign change have.  */
    1502        57683 :   inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
    1503        57683 :   if (inner_sgn == UNSIGNED)
    1504        43667 :     innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
    1505              :   else
    1506        14016 :     innermed = 0;
    1507        57683 :   if (wi::cmp (innermin, innermed, inner_sgn) >= 0
    1508        57683 :       || wi::cmp (innermed, innermax, inner_sgn) >= 0)
    1509        42390 :     innermed = innermin;
    1510              : 
    1511        57683 :   middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
    1512        57683 :   middlemin = wi::ext (innermin, middle_prec, middle_sgn);
    1513        57683 :   middlemed = wi::ext (innermed, middle_prec, middle_sgn);
    1514        57683 :   middlemax = wi::ext (innermax, middle_prec, middle_sgn);
    1515              : 
    1516              :   /* Require that the final conversion applied to both the original
    1517              :      and the intermediate range produces the same result.  */
    1518        57683 :   final_sgn = TYPE_SIGN (finaltype);
    1519       115366 :   if (wi::ext (middlemin, final_prec, final_sgn)
    1520       115366 :          != wi::ext (innermin, final_prec, final_sgn)
    1521       110474 :       || wi::ext (middlemed, final_prec, final_sgn)
    1522       223394 :          != wi::ext (innermed, final_prec, final_sgn)
    1523       151185 :       || wi::ext (middlemax, final_prec, final_sgn)
    1524       197936 :          != wi::ext (innermax, final_prec, final_sgn))
    1525              :     return false;
    1526              : 
    1527        45507 :   gimple_assign_set_rhs1 (stmt, innerop);
    1528        45507 :   fold_stmt (gsi, follow_single_use_edges);
    1529        45507 :   return true;
    1530      4855317 : }
    1531              : 
    1532              : /* Simplify a conversion from integral SSA name to float in STMT.  */
    1533              : 
    1534              : bool
    1535       255121 : simplify_using_ranges::simplify_float_conversion_using_ranges
    1536              :                                         (gimple_stmt_iterator *gsi,
    1537              :                                          gimple *stmt)
    1538              : {
    1539       255121 :   tree rhs1 = gimple_assign_rhs1 (stmt);
    1540       255121 :   int_range_max vr;
    1541       255121 :   scalar_float_mode fltmode
    1542       255121 :     = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
    1543       255121 :   scalar_int_mode mode;
    1544       255121 :   tree tem;
    1545       255121 :   gassign *conv;
    1546              : 
    1547              :   /* We can only handle constant ranges.  */
    1548       255121 :   if (!query->range_of_expr (vr, rhs1, stmt)
    1549       255121 :       || vr.varying_p ()
    1550       356584 :       || vr.undefined_p ())
    1551              :     return false;
    1552              : 
    1553              :   /* The code below doesn't work for large/huge _BitInt, nor is really
    1554              :      needed for those, bitint lowering does use ranges already.  */
    1555       201584 :   if (BITINT_TYPE_P (TREE_TYPE (rhs1))
    1556       100793 :       && TYPE_MODE (TREE_TYPE (rhs1)) == BLKmode)
    1557              :     return false;
    1558              :   /* First check if we can use a signed type in place of an unsigned.  */
    1559       100791 :   scalar_int_mode rhs_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1));
    1560       100791 :   if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
    1561         5340 :       && can_float_p (fltmode, rhs_mode, 0) != CODE_FOR_nothing
    1562       104361 :       && range_fits_type_p (&vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
    1563              :     mode = rhs_mode;
    1564              :   /* If we can do the conversion in the current input mode do nothing.  */
    1565        99245 :   else if (can_float_p (fltmode, rhs_mode,
    1566        99245 :                         TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
    1567              :     return false;
    1568              :   /* Otherwise search for a mode we can use, starting from the narrowest
    1569              :      integer mode available.  */
    1570              :   else
    1571              :     {
    1572         4400 :       mode = NARROWEST_INT_MODE;
    1573        15907 :       for (;;)
    1574              :         {
    1575              :           /* If we cannot do a signed conversion to float from mode
    1576              :              or if the value-range does not fit in the signed type
    1577              :              try with a wider mode.  */
    1578        15907 :           if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
    1579        15907 :               && range_fits_type_p (&vr, GET_MODE_PRECISION (mode), SIGNED))
    1580              :             break;
    1581              : 
    1582              :           /* But do not widen the input.  Instead leave that to the
    1583              :              optabs expansion code.  */
    1584        31548 :           if (!GET_MODE_WIDER_MODE (mode).exists (&mode)
    1585        15774 :               || GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
    1586              :             return false;
    1587              :         }
    1588              :     }
    1589              : 
    1590              :   /* It works, insert a truncation or sign-change before the
    1591              :      float conversion.  */
    1592         1679 :   tem = make_ssa_name (build_nonstandard_integer_type
    1593         1679 :                           (GET_MODE_PRECISION (mode), 0));
    1594         1679 :   conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
    1595         1679 :   gsi_insert_before (gsi, conv, GSI_SAME_STMT);
    1596         1679 :   gimple_assign_set_rhs1 (stmt, tem);
    1597         1679 :   fold_stmt (gsi, follow_single_use_edges);
    1598              : 
    1599         1679 :   return true;
    1600       255121 : }
    1601              : 
    1602              : /* Simplify an internal fn call using ranges if possible.  */
    1603              : 
    1604              : bool
    1605       545793 : simplify_using_ranges::simplify_internal_call_using_ranges
    1606              :                                         (gimple_stmt_iterator *gsi,
    1607              :                                          gimple *stmt)
    1608              : {
    1609       545793 :   enum tree_code subcode;
    1610       545793 :   bool is_ubsan = false;
    1611       545793 :   bool ovf = false;
    1612       545793 :   switch (gimple_call_internal_fn (stmt))
    1613              :     {
    1614              :     case IFN_UBSAN_CHECK_ADD:
    1615              :       subcode = PLUS_EXPR;
    1616              :       is_ubsan = true;
    1617              :       break;
    1618         2480 :     case IFN_UBSAN_CHECK_SUB:
    1619         2480 :       subcode = MINUS_EXPR;
    1620         2480 :       is_ubsan = true;
    1621         2480 :       break;
    1622         2123 :     case IFN_UBSAN_CHECK_MUL:
    1623         2123 :       subcode = MULT_EXPR;
    1624         2123 :       is_ubsan = true;
    1625         2123 :       break;
    1626        40640 :     case IFN_ADD_OVERFLOW:
    1627        40640 :       subcode = PLUS_EXPR;
    1628        40640 :       break;
    1629        49725 :     case IFN_SUB_OVERFLOW:
    1630        49725 :       subcode = MINUS_EXPR;
    1631        49725 :       break;
    1632        46809 :     case IFN_MUL_OVERFLOW:
    1633        46809 :       subcode = MULT_EXPR;
    1634        46809 :       break;
    1635              :     default:
    1636              :       return false;
    1637              :     }
    1638              : 
    1639       144356 :   tree op0 = gimple_call_arg (stmt, 0);
    1640       144356 :   tree op1 = gimple_call_arg (stmt, 1);
    1641       144356 :   tree type;
    1642       144356 :   if (is_ubsan)
    1643              :     {
    1644         7182 :       type = TREE_TYPE (op0);
    1645         7182 :       if (VECTOR_TYPE_P (type))
    1646              :         return false;
    1647              :     }
    1648       137174 :   else if (gimple_call_lhs (stmt) == NULL_TREE)
    1649              :     return false;
    1650              :   else
    1651       137174 :     type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
    1652       143486 :   if (!check_for_binary_op_overflow (query, subcode, type, op0, op1, &ovf, stmt)
    1653       143486 :       || (is_ubsan && ovf))
    1654              :     return false;
    1655              : 
    1656         3502 :   gimple *g;
    1657         3502 :   location_t loc = gimple_location (stmt);
    1658         3502 :   if (is_ubsan)
    1659          573 :     g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
    1660              :   else
    1661              :     {
    1662         2929 :       tree utype = type;
    1663         2929 :       if (ovf
    1664         1440 :           || !useless_type_conversion_p (type, TREE_TYPE (op0))
    1665         3645 :           || !useless_type_conversion_p (type, TREE_TYPE (op1)))
    1666         2502 :         utype = unsigned_type_for (type);
    1667         2929 :       if (TREE_CODE (op0) == INTEGER_CST)
    1668         1175 :         op0 = fold_convert (utype, op0);
    1669         1754 :       else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
    1670              :         {
    1671          734 :           g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
    1672          734 :           gimple_set_location (g, loc);
    1673          734 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    1674          734 :           op0 = gimple_assign_lhs (g);
    1675              :         }
    1676         2929 :       if (TREE_CODE (op1) == INTEGER_CST)
    1677         1699 :         op1 = fold_convert (utype, op1);
    1678         1230 :       else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
    1679              :         {
    1680           21 :           g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
    1681           21 :           gimple_set_location (g, loc);
    1682           21 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    1683           21 :           op1 = gimple_assign_lhs (g);
    1684              :         }
    1685         2929 :       g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
    1686         2929 :       gimple_set_location (g, loc);
    1687         2929 :       gsi_insert_before (gsi, g, GSI_SAME_STMT);
    1688         2929 :       if (utype != type)
    1689              :         {
    1690         1344 :           g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
    1691              :                                    gimple_assign_lhs (g));
    1692         1344 :           gimple_set_location (g, loc);
    1693         1344 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    1694              :         }
    1695         2929 :       g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
    1696              :                                gimple_assign_lhs (g),
    1697         2929 :                                build_int_cst (type, ovf));
    1698              :     }
    1699         3502 :   gimple_set_location (g, loc);
    1700         3502 :   gsi_replace (gsi, g, false);
    1701         3502 :   return true;
    1702              : }
    1703              : 
    1704              : /* Return true if VAR is a two-valued variable.  Set a and b with the
    1705              :    two-values when it is true.  Return false otherwise.  */
    1706              : 
    1707              : bool
    1708       477510 : simplify_using_ranges::two_valued_val_range_p (tree var, tree *a, tree *b,
    1709              :                                                gimple *s)
    1710              : {
    1711       477510 :   int_range_max vr;
    1712       477510 :   if (!query->range_of_expr (vr, var, s))
    1713              :     return false;
    1714       477510 :   if (vr.varying_p () || vr.undefined_p ())
    1715              :     return false;
    1716              : 
    1717       785399 :   if ((vr.num_pairs () == 1 && vr.upper_bound () - vr.lower_bound () == 1)
    1718       407594 :       || (vr.num_pairs () == 2
    1719       283509 :           && vr.lower_bound (0) == vr.upper_bound (0)
    1720       239995 :           && vr.lower_bound (1) == vr.upper_bound (1)))
    1721              :     {
    1722         7569 :       *a = wide_int_to_tree (TREE_TYPE (var), vr.lower_bound ());
    1723         7569 :       *b = wide_int_to_tree (TREE_TYPE (var), vr.upper_bound ());
    1724         7569 :       return true;
    1725              :     }
    1726              :   return false;
    1727       477510 : }
    1728              : 
    1729     13080947 : simplify_using_ranges::simplify_using_ranges (range_query *query,
    1730     13080947 :                                               int not_executable_flag)
    1731     13080947 :   : query (query)
    1732              : {
    1733     13080947 :   to_remove_edges = vNULL;
    1734     13080947 :   to_update_switch_stmts = vNULL;
    1735     13080947 :   m_not_executable_flag = not_executable_flag;
    1736     13080947 :   m_flag_set_edges = vNULL;
    1737     13080947 : }
    1738              : 
    1739     13080947 : simplify_using_ranges::~simplify_using_ranges ()
    1740              : {
    1741     13080947 :   cleanup_edges_and_switches ();
    1742     13080947 :   m_flag_set_edges.release ();
    1743     13080947 : }
    1744              : 
    1745              : /* Simplify STMT using ranges if possible.  */
    1746              : 
    1747              : bool
    1748    236907536 : simplify_using_ranges::simplify (gimple_stmt_iterator *gsi)
    1749              : {
    1750    236907536 :   gcc_checking_assert (query);
    1751              : 
    1752    236907536 :   gimple *stmt = gsi_stmt (*gsi);
    1753    236907536 :   if (is_gimple_assign (stmt))
    1754              :     {
    1755     66530702 :       enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
    1756     66530702 :       tree rhs1 = gimple_assign_rhs1 (stmt);
    1757     66530702 :       tree rhs2 = gimple_assign_rhs2 (stmt);
    1758     66530702 :       tree lhs = gimple_assign_lhs (stmt);
    1759     66530702 :       tree val1 = NULL_TREE, val2 = NULL_TREE;
    1760     66530702 :       use_operand_p use_p;
    1761     66530702 :       gimple *use_stmt;
    1762              : 
    1763              :       /* Convert:
    1764              :          LHS = CST BINOP VAR
    1765              :          Where VAR is two-valued and LHS is used in GIMPLE_COND only
    1766              :          To:
    1767              :          LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
    1768              : 
    1769              :          Also handles:
    1770              :          LHS = VAR BINOP CST
    1771              :          Where VAR is two-valued and LHS is used in GIMPLE_COND only
    1772              :          To:
    1773              :          LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
    1774              : 
    1775     66530702 :       if (TREE_CODE_CLASS (rhs_code) == tcc_binary
    1776     14255173 :           && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
    1777     10176184 :           && ((TREE_CODE (rhs1) == INTEGER_CST
    1778       182211 :                && TREE_CODE (rhs2) == SSA_NAME)
    1779      9994578 :               || (TREE_CODE (rhs2) == INTEGER_CST
    1780      6668681 :                   && TREE_CODE (rhs1) == SSA_NAME))
    1781      6849682 :           && single_imm_use (lhs, &use_p, &use_stmt)
    1782     71792373 :           && gimple_code (use_stmt) == GIMPLE_COND)
    1783              : 
    1784              :         {
    1785       477510 :           tree new_rhs1 = NULL_TREE;
    1786       477510 :           tree new_rhs2 = NULL_TREE;
    1787       477510 :           tree cmp_var = NULL_TREE;
    1788              : 
    1789       477510 :           if (TREE_CODE (rhs2) == SSA_NAME
    1790       477510 :               && two_valued_val_range_p (rhs2, &val1, &val2, stmt))
    1791              :             {
    1792              :               /* Optimize RHS1 OP [VAL1, VAL2].  */
    1793          237 :               new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
    1794          237 :               new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
    1795          237 :               cmp_var = rhs2;
    1796              :             }
    1797       477273 :           else if (TREE_CODE (rhs1) == SSA_NAME
    1798       477273 :                    && two_valued_val_range_p (rhs1, &val1, &val2, stmt))
    1799              :             {
    1800              :               /* Optimize [VAL1, VAL2] OP RHS2.  */
    1801         7332 :               new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
    1802         7332 :               new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
    1803         7332 :               cmp_var = rhs1;
    1804              :             }
    1805              : 
    1806              :           /* If we could not find two-vals or the optimization is invalid as
    1807              :              in divide by zero, new_rhs1 / new_rhs will be NULL_TREE.  */
    1808       477510 :           if (new_rhs1 && new_rhs2)
    1809              :             {
    1810         7569 :               tree cond = gimple_build (gsi, true, GSI_SAME_STMT,
    1811              :                                         UNKNOWN_LOCATION,
    1812              :                                         EQ_EXPR, boolean_type_node,
    1813              :                                         cmp_var, val1);
    1814         7569 :               gimple_assign_set_rhs_with_ops (gsi,
    1815              :                                               COND_EXPR, cond,
    1816              :                                               new_rhs1,
    1817              :                                               new_rhs2);
    1818         7569 :               update_stmt (gsi_stmt (*gsi));
    1819         7569 :               fold_stmt (gsi, follow_single_use_edges);
    1820      8077399 :               return true;
    1821              :             }
    1822              :         }
    1823              : 
    1824     66523133 :       if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
    1825      1226227 :         return simplify_compare_assign_using_ranges_1 (gsi, stmt);
    1826              : 
    1827     65296906 :       switch (rhs_code)
    1828              :         {
    1829              : 
    1830              :       /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
    1831              :          and BIT_AND_EXPR respectively if the first operand is greater
    1832              :          than zero and the second operand is an exact power of two.
    1833              :          Also optimize TRUNC_MOD_EXPR away if the second operand is
    1834              :          constant and the first operand already has the right value
    1835              :          range.  */
    1836       314872 :         case TRUNC_DIV_EXPR:
    1837       314872 :         case TRUNC_MOD_EXPR:
    1838       314872 :           if ((TREE_CODE (rhs1) == SSA_NAME
    1839       314872 :                || TREE_CODE (rhs1) == INTEGER_CST)
    1840       314872 :               && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
    1841       313574 :             return simplify_div_or_mod_using_ranges (gsi, stmt);
    1842              :           break;
    1843              : 
    1844              :       /* Transform ABS (X) into X or -X as appropriate.  */
    1845        56068 :         case ABS_EXPR:
    1846        56068 :           if (TREE_CODE (rhs1) == SSA_NAME
    1847        56068 :               && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
    1848         9512 :             return simplify_abs_using_ranges (gsi, stmt);
    1849              :           break;
    1850              : 
    1851      1273214 :         case BIT_AND_EXPR:
    1852      1273214 :         case BIT_IOR_EXPR:
    1853              :           /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR if all the bits
    1854              :              being cleared are already cleared or all the bits being set
    1855              :              are already set.  Beware that boolean types must be handled
    1856              :              logically (see range-op.cc) unless they have precision 1.  */
    1857      2463938 :           if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
    1858      2429440 :               && (TREE_CODE (TREE_TYPE (rhs1)) != BOOLEAN_TYPE
    1859       290885 :                   || TYPE_PRECISION (TREE_TYPE (rhs1)) == 1))
    1860      1238716 :             return simplify_bit_ops_using_ranges (gsi, stmt);
    1861              :           break;
    1862              : 
    1863      5802850 :         CASE_CONVERT:
    1864      5802850 :           if (TREE_CODE (rhs1) == SSA_NAME
    1865      5802850 :               && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
    1866      4709998 :             return simplify_conversion_using_ranges (gsi, stmt);
    1867              :           break;
    1868              : 
    1869       257449 :         case FLOAT_EXPR:
    1870       257449 :           if (TREE_CODE (rhs1) == SSA_NAME
    1871       257449 :               && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
    1872       255121 :             return simplify_float_conversion_using_ranges (gsi, stmt);
    1873              :           break;
    1874              : 
    1875       218211 :         case MIN_EXPR:
    1876       218211 :         case MAX_EXPR:
    1877       218211 :           return simplify_min_or_max_using_ranges (gsi, stmt);
    1878              : 
    1879       400447 :         case RSHIFT_EXPR:
    1880       400447 :           {
    1881       400447 :             tree op0 = gimple_assign_rhs1 (stmt);
    1882       400447 :             tree type = TREE_TYPE (op0);
    1883       400447 :             int_range_max range;
    1884       400447 :             if (TYPE_SIGN (type) == SIGNED
    1885       400447 :                 && query->range_of_expr (range, op0, stmt))
    1886              :               {
    1887        98471 :                 unsigned prec = TYPE_PRECISION (TREE_TYPE (op0));
    1888       196942 :                 int_range<2> nzm1 (type, wi::minus_one (prec), wi::zero (prec),
    1889        98471 :                                    VR_ANTI_RANGE);
    1890        98471 :                 range.intersect (nzm1);
    1891              :                 // If there are no ranges other than [-1, 0] remove the shift.
    1892        98471 :                 if (range.undefined_p ())
    1893              :                   {
    1894           80 :                     gimple_assign_set_rhs_from_tree (gsi, op0);
    1895           80 :                     return true;
    1896              :                   }
    1897              :                 return false;
    1898        98471 :               }
    1899       301976 :             break;
    1900       400447 :           }
    1901              :         default:
    1902              :           break;
    1903              :         }
    1904              :     }
    1905    170376834 :   else if (gimple_code (stmt) == GIMPLE_COND)
    1906     12065474 :     return simplify_cond_using_ranges_1 (as_a <gcond *> (stmt));
    1907    158311360 :   else if (gimple_code (stmt) == GIMPLE_SWITCH)
    1908        60568 :     return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
    1909    158250792 :   else if (is_gimple_call (stmt)
    1910    158250792 :            && gimple_call_internal_p (stmt))
    1911       545793 :     return simplify_internal_call_using_ranges (gsi, stmt);
    1912              : 
    1913              :   return false;
    1914              : }
        

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