LCOV - code coverage report
Current view: top level - gcc - tree-ssa-loop-niter.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 97.8 % 2485 2430
Test Date: 2026-06-20 15:32:29 Functions: 98.9 % 87 86
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Functions to determine/estimate number of iterations of a loop.
       2              :    Copyright (C) 2004-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 it
       7              : under the terms of the GNU General Public License as published by the
       8              : Free Software Foundation; either version 3, or (at your option) any
       9              : later version.
      10              : 
      11              : GCC is distributed in the hope that it will be useful, but WITHOUT
      12              : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14              : 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 "rtl.h"
      25              : #include "tree.h"
      26              : #include "gimple.h"
      27              : #include "tree-pass.h"
      28              : #include "ssa.h"
      29              : #include "gimple-pretty-print.h"
      30              : #include "diagnostic-core.h"
      31              : #include "stor-layout.h"
      32              : #include "fold-const.h"
      33              : #include "calls.h"
      34              : #include "intl.h"
      35              : #include "gimple-iterator.h"
      36              : #include "tree-cfg.h"
      37              : #include "tree-ssa-loop-ivopts.h"
      38              : #include "tree-ssa-loop-niter.h"
      39              : #include "tree-ssa-loop.h"
      40              : #include "cfgloop.h"
      41              : #include "tree-chrec.h"
      42              : #include "tree-scalar-evolution.h"
      43              : #include "tree-data-ref.h"
      44              : #include "tree-dfa.h"
      45              : #include "internal-fn.h"
      46              : #include "gimple-range.h"
      47              : #include "sreal.h"
      48              : 
      49              : 
      50              : /*
      51              : 
      52              :    Analysis of number of iterations of an affine exit test.
      53              : 
      54              : */
      55              : 
      56              : /* Bounds on some value, BELOW <= X <= UP.  */
      57              : 
      58              : struct bounds
      59              : {
      60              :   mpz_t below, up;
      61              : };
      62              : 
      63              : /* Splits expression EXPR to a variable part VAR and constant OFFSET.  */
      64              : 
      65              : static void
      66     57325432 : split_to_var_and_offset (tree expr, tree *var, mpz_t offset)
      67              : {
      68     57325432 :   tree type = TREE_TYPE (expr);
      69     57325432 :   tree op0, op1;
      70     57325432 :   bool negate = false;
      71              : 
      72     57325432 :   *var = expr;
      73     57325432 :   mpz_set_ui (offset, 0);
      74              : 
      75     57325432 :   switch (TREE_CODE (expr))
      76              :     {
      77         8088 :     case MINUS_EXPR:
      78         8088 :       negate = true;
      79              :       /* Fallthru.  */
      80              : 
      81      5832362 :     case PLUS_EXPR:
      82      5832362 :     case POINTER_PLUS_EXPR:
      83      5832362 :       op0 = TREE_OPERAND (expr, 0);
      84      5832362 :       op1 = TREE_OPERAND (expr, 1);
      85              : 
      86      5832362 :       if (TREE_CODE (op1) != INTEGER_CST)
      87              :         break;
      88              : 
      89      5820953 :       *var = op0;
      90              :       /* Always sign extend the offset.  */
      91      5820953 :       wi::to_mpz (wi::to_wide (op1), offset, SIGNED);
      92      5820953 :       if (negate)
      93           34 :         mpz_neg (offset, offset);
      94              :       break;
      95              : 
      96     25424845 :     case INTEGER_CST:
      97     25424845 :       *var = build_int_cst_type (type, 0);
      98     25424845 :       wi::to_mpz (wi::to_wide (expr), offset, TYPE_SIGN (type));
      99     25424845 :       break;
     100              : 
     101              :     default:
     102              :       break;
     103              :     }
     104     57325432 : }
     105              : 
     106              : /* From condition C0 CMP C1 derives information regarding the value range
     107              :    of VAR, which is of TYPE.  Results are stored in to BELOW and UP.  */
     108              : 
     109              : static void
     110     20668213 : refine_value_range_using_guard (tree type, tree var,
     111              :                                 tree c0, enum tree_code cmp, tree c1,
     112              :                                 mpz_t below, mpz_t up)
     113              : {
     114     20668213 :   tree varc0, varc1, ctype;
     115     20668213 :   mpz_t offc0, offc1;
     116     20668213 :   mpz_t mint, maxt, minc1, maxc1;
     117     20668213 :   bool no_wrap = nowrap_type_p (type);
     118     20668213 :   bool c0_ok, c1_ok;
     119     20668213 :   signop sgn = TYPE_SIGN (type);
     120              : 
     121     20668213 :   switch (cmp)
     122              :     {
     123      9718743 :     case LT_EXPR:
     124      9718743 :     case LE_EXPR:
     125      9718743 :     case GT_EXPR:
     126      9718743 :     case GE_EXPR:
     127      9718743 :       STRIP_SIGN_NOPS (c0);
     128      9718743 :       STRIP_SIGN_NOPS (c1);
     129      9718743 :       ctype = TREE_TYPE (c0);
     130      9718743 :       if (!useless_type_conversion_p (ctype, type))
     131     17690774 :         return;
     132              : 
     133              :       break;
     134              : 
     135              :     case EQ_EXPR:
     136              :       /* We could derive quite precise information from EQ_EXPR, however,
     137              :          such a guard is unlikely to appear, so we do not bother with
     138              :          handling it.  */
     139              :       return;
     140              : 
     141      5045437 :     case NE_EXPR:
     142              :       /* NE_EXPR comparisons do not contain much of useful information,
     143              :          except for cases of comparing with bounds.  */
     144      5045437 :       if (TREE_CODE (c1) != INTEGER_CST
     145      4726725 :           || !INTEGRAL_TYPE_P (type))
     146              :         return;
     147              : 
     148              :       /* Ensure that the condition speaks about an expression in the same
     149              :          type as X and Y.  */
     150      4726725 :       ctype = TREE_TYPE (c0);
     151      4726725 :       if (TYPE_PRECISION (ctype) != TYPE_PRECISION (type))
     152              :         return;
     153      2878511 :       c0 = fold_convert (type, c0);
     154      2878511 :       c1 = fold_convert (type, c1);
     155              : 
     156      2878511 :       if (operand_equal_p (var, c0, 0))
     157              :         {
     158              :           /* Case of comparing VAR with its below/up bounds.  */
     159       303420 :           auto_mpz valc1;
     160       303420 :           wi::to_mpz (wi::to_wide (c1), valc1, TYPE_SIGN (type));
     161       303420 :           if (mpz_cmp (valc1, below) == 0)
     162       100525 :             cmp = GT_EXPR;
     163       303420 :           if (mpz_cmp (valc1, up) == 0)
     164        46868 :             cmp = LT_EXPR;
     165       303420 :         }
     166              :       else
     167              :         {
     168              :           /* Case of comparing with the bounds of the type.  */
     169      2575091 :           wide_int min = wi::min_value (type);
     170      2575091 :           wide_int max = wi::max_value (type);
     171              : 
     172      2575091 :           if (wi::to_wide (c1) == min)
     173       715058 :             cmp = GT_EXPR;
     174      2575091 :           if (wi::to_wide (c1) == max)
     175        28323 :             cmp = LT_EXPR;
     176      2575091 :         }
     177              : 
     178              :       /* Quick return if no useful information.  */
     179      2878511 :       if (cmp == NE_EXPR)
     180              :         return;
     181              : 
     182              :       break;
     183              : 
     184              :     default:
     185              :       return;
     186              :     }
     187              : 
     188      8990097 :   mpz_init (offc0);
     189      8990097 :   mpz_init (offc1);
     190      8990097 :   split_to_var_and_offset (expand_simple_operations (c0), &varc0, offc0);
     191      8990097 :   split_to_var_and_offset (expand_simple_operations (c1), &varc1, offc1);
     192              : 
     193              :   /* We are only interested in comparisons of expressions based on VAR.  */
     194      8990097 :   if (operand_equal_p (var, varc1, 0))
     195              :     {
     196       864139 :       std::swap (varc0, varc1);
     197       864139 :       mpz_swap (offc0, offc1);
     198       864139 :       cmp = swap_tree_comparison (cmp);
     199              :     }
     200      8125958 :   else if (!operand_equal_p (var, varc0, 0))
     201              :     {
     202      6012658 :       mpz_clear (offc0);
     203      6012658 :       mpz_clear (offc1);
     204      6012658 :       return;
     205              :     }
     206              : 
     207      2977439 :   mpz_init (mint);
     208      2977439 :   mpz_init (maxt);
     209      2977439 :   get_type_static_bounds (type, mint, maxt);
     210      2977439 :   mpz_init (minc1);
     211      2977439 :   mpz_init (maxc1);
     212      5954878 :   int_range_max r (TREE_TYPE (varc1));
     213              :   /* Setup range information for varc1.  */
     214      2977439 :   if (integer_zerop (varc1))
     215              :     {
     216      1474513 :       wi::to_mpz (0, minc1, TYPE_SIGN (type));
     217      1474513 :       wi::to_mpz (0, maxc1, TYPE_SIGN (type));
     218              :     }
     219      1502926 :   else if (TREE_CODE (varc1) == SSA_NAME
     220      1407971 :            && INTEGRAL_TYPE_P (type)
     221      2815942 :            && get_range_query (cfun)->range_of_expr (r, varc1)
     222      1407971 :            && !r.undefined_p ()
     223      2908409 :            && !r.varying_p ())
     224              :     {
     225      1491844 :       gcc_assert (wi::le_p (r.lower_bound (), r.upper_bound (), sgn));
     226       745922 :       wi::to_mpz (r.lower_bound (), minc1, sgn);
     227       745922 :       wi::to_mpz (r.upper_bound (), maxc1, sgn);
     228              :     }
     229              :   else
     230              :     {
     231       757004 :       mpz_set (minc1, mint);
     232       757004 :       mpz_set (maxc1, maxt);
     233              :     }
     234              : 
     235              :   /* Compute valid range information for varc1 + offc1.  Note nothing
     236              :      useful can be derived if it overflows or underflows.  Overflow or
     237              :      underflow could happen when:
     238              : 
     239              :        offc1 > 0 && varc1 + offc1 > MAX_VAL (type)
     240              :        offc1 < 0 && varc1 + offc1 < MIN_VAL (type).  */
     241      2977439 :   mpz_add (minc1, minc1, offc1);
     242      2977439 :   mpz_add (maxc1, maxc1, offc1);
     243      5954878 :   c1_ok = (no_wrap
     244       858039 :            || mpz_sgn (offc1) == 0
     245       165213 :            || (mpz_sgn (offc1) < 0 && mpz_cmp (minc1, mint) >= 0)
     246      3141194 :            || (mpz_sgn (offc1) > 0 && mpz_cmp (maxc1, maxt) <= 0));
     247        27593 :   if (!c1_ok)
     248        27593 :     goto end;
     249              : 
     250      2949846 :   if (mpz_cmp (minc1, mint) < 0)
     251        28550 :     mpz_set (minc1, mint);
     252      2949846 :   if (mpz_cmp (maxc1, maxt) > 0)
     253        16484 :     mpz_set (maxc1, maxt);
     254              : 
     255      2949846 :   if (cmp == LT_EXPR)
     256              :     {
     257       404648 :       cmp = LE_EXPR;
     258       404648 :       mpz_sub_ui (maxc1, maxc1, 1);
     259              :     }
     260      2949846 :   if (cmp == GT_EXPR)
     261              :     {
     262      1533078 :       cmp = GE_EXPR;
     263      1533078 :       mpz_add_ui (minc1, minc1, 1);
     264              :     }
     265              : 
     266              :   /* Compute range information for varc0.  If there is no overflow,
     267              :      the condition implied that
     268              : 
     269              :        (varc0) cmp (varc1 + offc1 - offc0)
     270              : 
     271              :      We can possibly improve the upper bound of varc0 if cmp is LE_EXPR,
     272              :      or the below bound if cmp is GE_EXPR.
     273              : 
     274              :      To prove there is no overflow/underflow, we need to check below
     275              :      four cases:
     276              :        1) cmp == LE_EXPR && offc0 > 0
     277              : 
     278              :             (varc0 + offc0) doesn't overflow
     279              :             && (varc1 + offc1 - offc0) doesn't underflow
     280              : 
     281              :        2) cmp == LE_EXPR && offc0 < 0
     282              : 
     283              :             (varc0 + offc0) doesn't underflow
     284              :             && (varc1 + offc1 - offc0) doesn't overfloe
     285              : 
     286              :           In this case, (varc0 + offc0) will never underflow if we can
     287              :           prove (varc1 + offc1 - offc0) doesn't overflow.
     288              : 
     289              :        3) cmp == GE_EXPR && offc0 < 0
     290              : 
     291              :             (varc0 + offc0) doesn't underflow
     292              :             && (varc1 + offc1 - offc0) doesn't overflow
     293              : 
     294              :        4) cmp == GE_EXPR && offc0 > 0
     295              : 
     296              :             (varc0 + offc0) doesn't overflow
     297              :             && (varc1 + offc1 - offc0) doesn't underflow
     298              : 
     299              :           In this case, (varc0 + offc0) will never overflow if we can
     300              :           prove (varc1 + offc1 - offc0) doesn't underflow.
     301              : 
     302              :      Note we only handle case 2 and 4 in below code.  */
     303              : 
     304      2949846 :   mpz_sub (minc1, minc1, offc0);
     305      2949846 :   mpz_sub (maxc1, maxc1, offc0);
     306      5899692 :   c0_ok = (no_wrap
     307       830446 :            || mpz_sgn (offc0) == 0
     308        49160 :            || (cmp == LE_EXPR
     309        31417 :                && mpz_sgn (offc0) < 0 && mpz_cmp (maxc1, maxt) <= 0)
     310      2996489 :            || (cmp == GE_EXPR
     311        17743 :                && mpz_sgn (offc0) > 0 && mpz_cmp (minc1, mint) >= 0));
     312        44403 :   if (!c0_ok)
     313        44403 :     goto end;
     314              : 
     315      2905443 :   if (cmp == LE_EXPR)
     316              :     {
     317      1091813 :       if (mpz_cmp (up, maxc1) > 0)
     318       339003 :         mpz_set (up, maxc1);
     319              :     }
     320              :   else
     321              :     {
     322      1813630 :       if (mpz_cmp (below, minc1) < 0)
     323      1181779 :         mpz_set (below, minc1);
     324              :     }
     325              : 
     326       631851 : end:
     327      2977439 :   mpz_clear (mint);
     328      2977439 :   mpz_clear (maxt);
     329      2977439 :   mpz_clear (minc1);
     330      2977439 :   mpz_clear (maxc1);
     331      2977439 :   mpz_clear (offc0);
     332      2977439 :   mpz_clear (offc1);
     333              : }
     334              : 
     335              : /* Stores estimates of the minimum and maximum values of the expression
     336              :    VAR + OFF in TYPE to MIN and MAX.  The estimates are valid on entry
     337              :    to LOOP, i.e. on the loop preheader edge.  */
     338              : 
     339              : static void
     340     24973906 : determine_value_range (class loop *loop, tree type, tree var, mpz_t off,
     341              :                        mpz_t min, mpz_t max)
     342              : {
     343     24973906 :   int cnt = 0;
     344     24973906 :   mpz_t minm, maxm;
     345     24973906 :   basic_block bb;
     346     24973906 :   wide_int minv, maxv;
     347     24973906 :   enum value_range_kind rtype = VR_VARYING;
     348              : 
     349              :   /* If the expression is a constant, we know its value exactly.  */
     350     24973906 :   if (integer_zerop (var))
     351              :     {
     352     17661781 :       mpz_set (min, off);
     353     17661781 :       mpz_set (max, off);
     354     17661781 :       return;
     355              :     }
     356              : 
     357      7312125 :   get_type_static_bounds (type, min, max);
     358              : 
     359              :   /* See if we have some range info from VRP.  */
     360      7312125 :   if (INTEGRAL_TYPE_P (type))
     361              :     {
     362      5891342 :       edge e = loop_preheader_edge (loop);
     363      5891342 :       signop sgn = TYPE_SIGN (type);
     364      5891342 :       gphi_iterator gsi;
     365              : 
     366              :       /* Either for VAR itself...  */
     367      5891342 :       int_range_max var_range (TREE_TYPE (var));
     368     11782684 :       get_range_query (cfun)->range_on_edge (var_range, e, var);
     369      5891342 :       if (var_range.varying_p () || var_range.undefined_p ())
     370              :         rtype = VR_VARYING;
     371              :       else
     372              :         rtype = VR_RANGE;
     373      5891342 :       if (!var_range.undefined_p ())
     374              :         {
     375      5876264 :           minv = var_range.lower_bound ();
     376      5876332 :           maxv = var_range.upper_bound ();
     377              :         }
     378              : 
     379              :       /* Or for PHI results in loop->header where VAR is used as
     380              :          PHI argument from the loop preheader edge.  */
     381      5891342 :       int_range_max phi_range (TREE_TYPE (var));
     382     21709713 :       for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi))
     383              :         {
     384     15818372 :           gphi *phi = gsi.phi ();
     385     15818372 :           if (PHI_ARG_DEF_FROM_EDGE (phi, e) == var
     386      2593636 :               && get_range_query (cfun)->range_on_edge (phi_range,
     387              :                                                     e, gimple_phi_result (phi))
     388      1296818 :               && !phi_range.varying_p ()
     389     16559468 :               && !phi_range.undefined_p ())
     390              :             {
     391       741092 :               if (rtype != VR_RANGE)
     392              :                 {
     393       253547 :                   rtype = VR_RANGE;
     394       253547 :                   minv = phi_range.lower_bound ();
     395       253562 :                   maxv = phi_range.upper_bound ();
     396              :                 }
     397              :               else
     398              :                 {
     399       487547 :                   minv = wi::max (minv, phi_range.lower_bound (), sgn);
     400       487547 :                   maxv = wi::min (maxv, phi_range.upper_bound (), sgn);
     401              :                   /* If the PHI result range are inconsistent with
     402              :                      the VAR range, give up on looking at the PHI
     403              :                      results.  This can happen if VR_UNDEFINED is
     404              :                      involved.  */
     405       487545 :                   if (wi::gt_p (minv, maxv, sgn))
     406              :                     {
     407            1 :                       int_range_max vr (TREE_TYPE (var));
     408            2 :                       get_range_query (cfun)->range_on_edge (vr, e, var);
     409            1 :                       if (vr.varying_p () || vr.undefined_p ())
     410              :                         rtype = VR_VARYING;
     411              :                       else
     412              :                         rtype = VR_RANGE;
     413            1 :                       if (!vr.undefined_p ())
     414              :                         {
     415            1 :                           minv = vr.lower_bound ();
     416            1 :                           maxv = vr.upper_bound ();
     417              :                         }
     418            1 :                       break;
     419            1 :                     }
     420              :                 }
     421              :             }
     422              :         }
     423      5891342 :       mpz_init (minm);
     424      5891342 :       mpz_init (maxm);
     425      5891342 :       if (rtype != VR_RANGE)
     426              :         {
     427      2983305 :           mpz_set (minm, min);
     428      2983305 :           mpz_set (maxm, max);
     429              :         }
     430              :       else
     431              :         {
     432      2908037 :           gcc_assert (wi::le_p (minv, maxv, sgn));
     433      2908037 :           wi::to_mpz (minv, minm, sgn);
     434      2908037 :           wi::to_mpz (maxv, maxm, sgn);
     435              :         }
     436              :       /* Now walk the dominators of the loop header and use the entry
     437              :          guards to refine the estimates.  */
     438      5891342 :       for (bb = loop->header;
     439     59928046 :            bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
     440     59928046 :              && cnt < param_max_niter_dominators_walk;
     441     54036704 :            bb = get_immediate_dominator (CDI_DOMINATORS, bb))
     442              :         {
     443     54036704 :           edge e;
     444     54036704 :           tree c0, c1;
     445     54036704 :           enum tree_code cmp;
     446              : 
     447     54036704 :           if (!single_pred_p (bb))
     448     26989928 :             continue;
     449     27046776 :           e = single_pred_edge (bb);
     450              : 
     451     27046776 :           if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
     452      6378563 :             continue;
     453              : 
     454     41336426 :           gcond *cond = as_a <gcond *> (*gsi_last_bb (e->src));
     455     20668213 :           c0 = gimple_cond_lhs (cond);
     456     20668213 :           cmp = gimple_cond_code (cond);
     457     20668213 :           c1 = gimple_cond_rhs (cond);
     458              : 
     459     20668213 :           if (e->flags & EDGE_FALSE_VALUE)
     460     11146833 :             cmp = invert_tree_comparison (cmp, false);
     461              : 
     462     20668213 :           refine_value_range_using_guard (type, var, c0, cmp, c1, minm, maxm);
     463     20668213 :           ++cnt;
     464              :         }
     465              : 
     466      5891342 :       mpz_add (minm, minm, off);
     467      5891342 :       mpz_add (maxm, maxm, off);
     468              :       /* If the computation may not wrap or off is zero, then this
     469              :          is always fine.  If off is negative and minv + off isn't
     470              :          smaller than type's minimum, or off is positive and
     471              :          maxv + off isn't bigger than type's maximum, use the more
     472              :          precise range too.  */
     473      5891342 :       if (nowrap_type_p (type)
     474      2292294 :           || mpz_sgn (off) == 0
     475       576784 :           || (mpz_sgn (off) < 0 && mpz_cmp (minm, min) >= 0)
     476      6383182 :           || (mpz_sgn (off) > 0 && mpz_cmp (maxm, max) <= 0))
     477              :         {
     478      5690175 :           mpz_set (min, minm);
     479      5690175 :           mpz_set (max, maxm);
     480      5690175 :           mpz_clear (minm);
     481      5690175 :           mpz_clear (maxm);
     482      5690175 :           return;
     483              :         }
     484       201167 :       mpz_clear (minm);
     485       201167 :       mpz_clear (maxm);
     486      5891342 :     }
     487              : 
     488              :   /* If the computation may wrap, we know nothing about the value, except for
     489              :      the range of the type.  */
     490      1621950 :   if (!nowrap_type_p (type))
     491              :     return;
     492              : 
     493              :   /* Since the addition of OFF does not wrap, if OFF is positive, then we may
     494              :      add it to MIN, otherwise to MAX.  */
     495      1420783 :   if (mpz_sgn (off) < 0)
     496        30093 :     mpz_add (max, max, off);
     497              :   else
     498      1390690 :     mpz_add (min, min, off);
     499     24973974 : }
     500              : 
     501              : /* Stores the bounds on the difference of the values of the expressions
     502              :    (var + X) and (var + Y), computed in TYPE, to BNDS.  */
     503              : 
     504              : static void
     505       163310 : bound_difference_of_offsetted_base (tree type, mpz_t x, mpz_t y,
     506              :                                     bounds *bnds)
     507              : {
     508       163310 :   int rel = mpz_cmp (x, y);
     509       163310 :   bool may_wrap = !nowrap_type_p (type);
     510              : 
     511              :   /* If X == Y, then the expressions are always equal.
     512              :      If X > Y, there are the following possibilities:
     513              :        a) neither of var + X and var + Y overflow or underflow, or both of
     514              :           them do.  Then their difference is X - Y.
     515              :        b) var + X overflows, and var + Y does not.  Then the values of the
     516              :           expressions are var + X - M and var + Y, where M is the range of
     517              :           the type, and their difference is X - Y - M.
     518              :        c) var + Y underflows and var + X does not.  Their difference again
     519              :           is M - X + Y.
     520              :        Therefore, if the arithmetics in type does not overflow, then the
     521              :        bounds are (X - Y, X - Y), otherwise they are (X - Y - M, X - Y)
     522              :      Similarly, if X < Y, the bounds are either (X - Y, X - Y) or
     523              :      (X - Y, X - Y + M).  */
     524              : 
     525       163310 :   if (rel == 0)
     526              :     {
     527          746 :       mpz_set_ui (bnds->below, 0);
     528          746 :       mpz_set_ui (bnds->up, 0);
     529          746 :       return;
     530              :     }
     531              : 
     532       162564 :   auto_mpz m;
     533       162564 :   wi::to_mpz (wi::minus_one (TYPE_PRECISION (type)), m, UNSIGNED);
     534       162564 :   mpz_add_ui (m, m, 1);
     535       162564 :   mpz_sub (bnds->up, x, y);
     536       162564 :   mpz_set (bnds->below, bnds->up);
     537              : 
     538       162564 :   if (may_wrap)
     539              :     {
     540       142030 :       if (rel > 0)
     541       140776 :         mpz_sub (bnds->below, bnds->below, m);
     542              :       else
     543         1254 :         mpz_add (bnds->up, bnds->up, m);
     544              :     }
     545       162564 : }
     546              : 
     547              : /* From condition C0 CMP C1 derives information regarding the
     548              :    difference of values of VARX + OFFX and VARY + OFFY, computed in TYPE,
     549              :    and stores it to BNDS.  */
     550              : 
     551              : static void
     552     19477660 : refine_bounds_using_guard (tree type, tree varx, mpz_t offx,
     553              :                            tree vary, mpz_t offy,
     554              :                            tree c0, enum tree_code cmp, tree c1,
     555              :                            bounds *bnds)
     556              : {
     557     19477660 :   tree varc0, varc1, ctype;
     558     19477660 :   mpz_t offc0, offc1, loffx, loffy, bnd;
     559     19477660 :   bool lbound = false;
     560     19477660 :   bool no_wrap = nowrap_type_p (type);
     561     19477660 :   bool x_ok, y_ok;
     562              : 
     563     19477660 :   switch (cmp)
     564              :     {
     565      8354193 :     case LT_EXPR:
     566      8354193 :     case LE_EXPR:
     567      8354193 :     case GT_EXPR:
     568      8354193 :     case GE_EXPR:
     569      8354193 :       STRIP_SIGN_NOPS (c0);
     570      8354193 :       STRIP_SIGN_NOPS (c1);
     571      8354193 :       ctype = TREE_TYPE (c0);
     572      8354193 :       if (!useless_type_conversion_p (ctype, type))
     573     12455304 :         return;
     574              : 
     575              :       break;
     576              : 
     577              :     case EQ_EXPR:
     578              :       /* We could derive quite precise information from EQ_EXPR, however, such
     579              :          a guard is unlikely to appear, so we do not bother with handling
     580              :          it.  */
     581              :       return;
     582              : 
     583      5355712 :     case NE_EXPR:
     584              :       /* NE_EXPR comparisons do not contain much of useful information, except for
     585              :          special case of comparing with the bounds of the type.  */
     586      5355712 :       if (TREE_CODE (c1) != INTEGER_CST
     587      4593470 :           || !INTEGRAL_TYPE_P (type))
     588              :         return;
     589              : 
     590              :       /* Ensure that the condition speaks about an expression in the same type
     591              :          as X and Y.  */
     592      4106005 :       ctype = TREE_TYPE (c0);
     593      4106005 :       if (TYPE_PRECISION (ctype) != TYPE_PRECISION (type))
     594              :         return;
     595      2356612 :       c0 = fold_convert (type, c0);
     596      2356612 :       c1 = fold_convert (type, c1);
     597              : 
     598      2356612 :       if (TYPE_MIN_VALUE (type)
     599      2356612 :           && operand_equal_p (c1, TYPE_MIN_VALUE (type), 0))
     600              :         {
     601              :           cmp = GT_EXPR;
     602              :           break;
     603              :         }
     604      1694913 :       if (TYPE_MAX_VALUE (type)
     605      1694913 :           && operand_equal_p (c1, TYPE_MAX_VALUE (type), 0))
     606              :         {
     607              :           cmp = LT_EXPR;
     608              :           break;
     609              :         }
     610              : 
     611              :       return;
     612              :     default:
     613              :       return;
     614              :     }
     615              : 
     616      7022356 :   mpz_init (offc0);
     617      7022356 :   mpz_init (offc1);
     618      7022356 :   split_to_var_and_offset (expand_simple_operations (c0), &varc0, offc0);
     619      7022356 :   split_to_var_and_offset (expand_simple_operations (c1), &varc1, offc1);
     620              : 
     621              :   /* We are only interested in comparisons of expressions based on VARX and
     622              :      VARY.  TODO -- we might also be able to derive some bounds from
     623              :      expressions containing just one of the variables.  */
     624              : 
     625      7022356 :   if (operand_equal_p (varx, varc1, 0))
     626              :     {
     627       955029 :       std::swap (varc0, varc1);
     628       955029 :       mpz_swap (offc0, offc1);
     629       955029 :       cmp = swap_tree_comparison (cmp);
     630              :     }
     631              : 
     632      7022356 :   if (!operand_equal_p (varx, varc0, 0)
     633      7022356 :       || !operand_equal_p (vary, varc1, 0))
     634      5180683 :     goto end;
     635              : 
     636      1841673 :   mpz_init_set (loffx, offx);
     637      1841673 :   mpz_init_set (loffy, offy);
     638              : 
     639      1841673 :   if (cmp == GT_EXPR || cmp == GE_EXPR)
     640              :     {
     641      1760855 :       std::swap (varx, vary);
     642      1760855 :       mpz_swap (offc0, offc1);
     643      1760855 :       mpz_swap (loffx, loffy);
     644      1760855 :       cmp = swap_tree_comparison (cmp);
     645      1760855 :       lbound = true;
     646              :     }
     647              : 
     648              :   /* If there is no overflow, the condition implies that
     649              : 
     650              :      (VARX + OFFX) cmp (VARY + OFFY) + (OFFX - OFFY + OFFC1 - OFFC0).
     651              : 
     652              :      The overflows and underflows may complicate things a bit; each
     653              :      overflow decreases the appropriate offset by M, and underflow
     654              :      increases it by M.  The above inequality would not necessarily be
     655              :      true if
     656              : 
     657              :      -- VARX + OFFX underflows and VARX + OFFC0 does not, or
     658              :         VARX + OFFC0 overflows, but VARX + OFFX does not.
     659              :         This may only happen if OFFX < OFFC0.
     660              :      -- VARY + OFFY overflows and VARY + OFFC1 does not, or
     661              :         VARY + OFFC1 underflows and VARY + OFFY does not.
     662              :         This may only happen if OFFY > OFFC1.  */
     663              : 
     664      1841673 :   if (no_wrap)
     665              :     {
     666              :       x_ok = true;
     667              :       y_ok = true;
     668              :     }
     669              :   else
     670              :     {
     671       639205 :       x_ok = (integer_zerop (varx)
     672       639205 :               || mpz_cmp (loffx, offc0) >= 0);
     673       639205 :       y_ok = (integer_zerop (vary)
     674       639205 :               || mpz_cmp (loffy, offc1) <= 0);
     675              :     }
     676              : 
     677       636322 :   if (x_ok && y_ok)
     678              :     {
     679      1835035 :       mpz_init (bnd);
     680      1835035 :       mpz_sub (bnd, loffx, loffy);
     681      1835035 :       mpz_add (bnd, bnd, offc1);
     682      1835035 :       mpz_sub (bnd, bnd, offc0);
     683              : 
     684      1835035 :       if (cmp == LT_EXPR)
     685      1490509 :         mpz_sub_ui (bnd, bnd, 1);
     686              : 
     687      1835035 :       if (lbound)
     688              :         {
     689      1755441 :           mpz_neg (bnd, bnd);
     690      1755441 :           if (mpz_cmp (bnds->below, bnd) < 0)
     691       460820 :             mpz_set (bnds->below, bnd);
     692              :         }
     693              :       else
     694              :         {
     695        79594 :           if (mpz_cmp (bnd, bnds->up) < 0)
     696         2904 :             mpz_set (bnds->up, bnd);
     697              :         }
     698      1835035 :       mpz_clear (bnd);
     699              :     }
     700              : 
     701      1841673 :   mpz_clear (loffx);
     702      1841673 :   mpz_clear (loffy);
     703      7022356 : end:
     704      7022356 :   mpz_clear (offc0);
     705      7022356 :   mpz_clear (offc1);
     706              : }
     707              : 
     708              : /* Stores the bounds on the value of the expression X - Y in LOOP to BNDS.
     709              :    The subtraction is considered to be performed in arbitrary precision,
     710              :    without overflows.
     711              : 
     712              :    We do not attempt to be too clever regarding the value ranges of X and
     713              :    Y; most of the time, they are just integers or ssa names offsetted by
     714              :    integer.  However, we try to use the information contained in the
     715              :    comparisons before the loop (usually created by loop header copying).  */
     716              : 
     717              : static void
     718     12650263 : bound_difference (class loop *loop, tree x, tree y, bounds *bnds)
     719              : {
     720     12650263 :   tree type = TREE_TYPE (x);
     721     12650263 :   tree varx, vary;
     722     12650263 :   mpz_t offx, offy;
     723     12650263 :   int cnt = 0;
     724     12650263 :   edge e;
     725     12650263 :   basic_block bb;
     726     12650263 :   tree c0, c1;
     727     12650263 :   enum tree_code cmp;
     728              : 
     729              :   /* Get rid of unnecessary casts, but preserve the value of
     730              :      the expressions.  */
     731     12650263 :   STRIP_SIGN_NOPS (x);
     732     12650263 :   STRIP_SIGN_NOPS (y);
     733              : 
     734     12650263 :   mpz_init (bnds->below);
     735     12650263 :   mpz_init (bnds->up);
     736     12650263 :   mpz_init (offx);
     737     12650263 :   mpz_init (offy);
     738     12650263 :   split_to_var_and_offset (x, &varx, offx);
     739     12650263 :   split_to_var_and_offset (y, &vary, offy);
     740              : 
     741     12650263 :   if (!integer_zerop (varx)
     742     12650263 :       && operand_equal_p (varx, vary, 0))
     743              :     {
     744              :       /* Special case VARX == VARY -- we just need to compare the
     745              :          offsets.  The matters are a bit more complicated in the
     746              :          case addition of offsets may wrap.  */
     747       163310 :       bound_difference_of_offsetted_base (type, offx, offy, bnds);
     748              :     }
     749              :   else
     750              :     {
     751              :       /* Otherwise, use the value ranges to determine the initial
     752              :          estimates on below and up.  */
     753     12486953 :       auto_mpz minx, maxx, miny, maxy;
     754     12486953 :       determine_value_range (loop, type, varx, offx, minx, maxx);
     755     12486953 :       determine_value_range (loop, type, vary, offy, miny, maxy);
     756              : 
     757     12486953 :       mpz_sub (bnds->below, minx, maxy);
     758     12486953 :       mpz_sub (bnds->up, maxx, miny);
     759     12486953 :     }
     760              : 
     761              :   /* If both X and Y are constants, we cannot get any more precise.  */
     762     12650263 :   if (integer_zerop (varx) && integer_zerop (vary))
     763      7004330 :     goto end;
     764              : 
     765              :   /* Now walk the dominators of the loop header and use the entry
     766              :      guards to refine the estimates.  */
     767      5645933 :   for (bb = loop->header;
     768     56216775 :        bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
     769     56216775 :          && cnt < param_max_niter_dominators_walk;
     770     50570842 :        bb = get_immediate_dominator (CDI_DOMINATORS, bb))
     771              :     {
     772     50570842 :       if (!single_pred_p (bb))
     773     24338710 :         continue;
     774     26232132 :       e = single_pred_edge (bb);
     775              : 
     776     26232132 :       if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
     777      6754472 :         continue;
     778              : 
     779     38955320 :       gcond *cond = as_a <gcond *> (*gsi_last_bb (e->src));
     780     19477660 :       c0 = gimple_cond_lhs (cond);
     781     19477660 :       cmp = gimple_cond_code (cond);
     782     19477660 :       c1 = gimple_cond_rhs (cond);
     783              : 
     784     19477660 :       if (e->flags & EDGE_FALSE_VALUE)
     785     10826136 :         cmp = invert_tree_comparison (cmp, false);
     786              : 
     787     19477660 :       refine_bounds_using_guard (type, varx, offx, vary, offy,
     788              :                                  c0, cmp, c1, bnds);
     789     19477660 :       ++cnt;
     790              :     }
     791              : 
     792      5645933 : end:
     793     12650263 :   mpz_clear (offx);
     794     12650263 :   mpz_clear (offy);
     795     12650263 : }
     796              : 
     797              : /* Update the bounds in BNDS that restrict the value of X to the bounds
     798              :    that restrict the value of X + DELTA.  X can be obtained as a
     799              :    difference of two values in TYPE.  */
     800              : 
     801              : static void
     802      2038405 : bounds_add (bounds *bnds, const widest_int &delta, tree type)
     803              : {
     804      2038405 :   mpz_t mdelta, max;
     805              : 
     806      2038405 :   mpz_init (mdelta);
     807      2038405 :   wi::to_mpz (delta, mdelta, SIGNED);
     808              : 
     809      2038405 :   mpz_init (max);
     810      2038405 :   wi::to_mpz (wi::minus_one (TYPE_PRECISION (type)), max, UNSIGNED);
     811              : 
     812      2038405 :   mpz_add (bnds->up, bnds->up, mdelta);
     813      2038405 :   mpz_add (bnds->below, bnds->below, mdelta);
     814              : 
     815      2038405 :   if (mpz_cmp (bnds->up, max) > 0)
     816       119055 :     mpz_set (bnds->up, max);
     817              : 
     818      2038405 :   mpz_neg (max, max);
     819      2038405 :   if (mpz_cmp (bnds->below, max) < 0)
     820         9810 :     mpz_set (bnds->below, max);
     821              : 
     822      2038405 :   mpz_clear (mdelta);
     823      2038405 :   mpz_clear (max);
     824      2038405 : }
     825              : 
     826              : /* Update the bounds in BNDS that restrict the value of X to the bounds
     827              :    that restrict the value of -X.  */
     828              : 
     829              : static void
     830      2496789 : bounds_negate (bounds *bnds)
     831              : {
     832      2496789 :   mpz_t tmp;
     833              : 
     834      2496789 :   mpz_init_set (tmp, bnds->up);
     835      2496789 :   mpz_neg (bnds->up, bnds->below);
     836      2496789 :   mpz_neg (bnds->below, tmp);
     837      2496789 :   mpz_clear (tmp);
     838      2496789 : }
     839              : 
     840              : /* Returns inverse of X modulo 2^s, where MASK = 2^s-1.  */
     841              : 
     842              : static tree
     843       173858 : inverse (tree x, tree mask)
     844              : {
     845       173858 :   tree type = TREE_TYPE (x);
     846       173858 :   tree rslt;
     847       173858 :   unsigned ctr = tree_floor_log2 (mask);
     848              : 
     849       173858 :   if (TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT)
     850              :     {
     851       173794 :       unsigned HOST_WIDE_INT ix;
     852       173794 :       unsigned HOST_WIDE_INT imask;
     853       173794 :       unsigned HOST_WIDE_INT irslt = 1;
     854              : 
     855       173794 :       gcc_assert (cst_and_fits_in_hwi (x));
     856       173794 :       gcc_assert (cst_and_fits_in_hwi (mask));
     857              : 
     858       173794 :       ix = int_cst_value (x);
     859       173794 :       imask = int_cst_value (mask);
     860              : 
     861      9902160 :       for (; ctr; ctr--)
     862              :         {
     863      9554572 :           irslt *= ix;
     864      9554572 :           ix *= ix;
     865              :         }
     866       173794 :       irslt &= imask;
     867              : 
     868       173794 :       rslt = build_int_cst_type (type, irslt);
     869              :     }
     870              :   else
     871              :     {
     872           64 :       rslt = build_int_cst (type, 1);
     873         8192 :       for (; ctr; ctr--)
     874              :         {
     875         8128 :           rslt = int_const_binop (MULT_EXPR, rslt, x);
     876         8128 :           x = int_const_binop (MULT_EXPR, x, x);
     877              :         }
     878           64 :       rslt = int_const_binop (BIT_AND_EXPR, rslt, mask);
     879              :     }
     880              : 
     881       173858 :   return rslt;
     882              : }
     883              : 
     884              : /* Derives the upper bound BND on the number of executions of loop with exit
     885              :    condition S * i <> C.  If NO_OVERFLOW is true, then the control variable of
     886              :    the loop does not overflow.  EXIT_MUST_BE_TAKEN is true if we are guaranteed
     887              :    that the loop ends through this exit, i.e., the induction variable ever
     888              :    reaches the value of C.
     889              : 
     890              :    The value C is equal to final - base, where final and base are the final and
     891              :    initial value of the actual induction variable in the analysed loop.  BNDS
     892              :    bounds the value of this difference when computed in signed type with
     893              :    unbounded range, while the computation of C is performed in an unsigned
     894              :    type with the range matching the range of the type of the induction variable.
     895              :    In particular, BNDS.up contains an upper bound on C in the following cases:
     896              :    -- if the iv must reach its final value without overflow, i.e., if
     897              :       NO_OVERFLOW && EXIT_MUST_BE_TAKEN is true, or
     898              :    -- if final >= base, which we know to hold when BNDS.below >= 0.  */
     899              : 
     900              : static void
     901      7346834 : number_of_iterations_ne_max (mpz_t bnd, bool no_overflow, tree c, tree s,
     902              :                              bounds *bnds, bool exit_must_be_taken)
     903              : {
     904      7346834 :   widest_int max;
     905      7346834 :   mpz_t d;
     906      7346834 :   tree type = TREE_TYPE (c);
     907     14693668 :   bool bnds_u_valid = ((no_overflow && exit_must_be_taken)
     908      7346834 :                        || mpz_sgn (bnds->below) >= 0);
     909              : 
     910      7346834 :   if (integer_onep (s)
     911       836271 :       || (TREE_CODE (c) == INTEGER_CST
     912       328892 :           && TREE_CODE (s) == INTEGER_CST
     913      1165806 :           && wi::mod_trunc (wi::to_wide (c), wi::to_wide (s),
     914      8004618 :                             TYPE_SIGN (type)) == 0)
     915      7854856 :       || (TYPE_OVERFLOW_UNDEFINED (type)
     916            0 :           && multiple_of_p (type, c, s)))
     917              :     {
     918              :       /* If C is an exact multiple of S, then its value will be reached before
     919              :          the induction variable overflows (unless the loop is exited in some
     920              :          other way before).  Note that the actual induction variable in the
     921              :          loop (which ranges from base to final instead of from 0 to C) may
     922              :          overflow, in which case BNDS.up will not be giving a correct upper
     923              :          bound on C; thus, BNDS_U_VALID had to be computed in advance.  */
     924              :       no_overflow = true;
     925              :       exit_must_be_taken = true;
     926              :     }
     927              : 
     928              :   /* If the induction variable can overflow, the number of iterations is at
     929              :      most the period of the control variable (or infinite, but in that case
     930              :      the whole # of iterations analysis will fail).  */
     931       508022 :   if (!no_overflow)
     932              :     {
     933       121358 :       max = wi::mask <widest_int> (TYPE_PRECISION (type)
     934       121358 :                                    - wi::ctz (wi::to_wide (s)), false);
     935        60679 :       wi::to_mpz (max, bnd, UNSIGNED);
     936        60679 :       return;
     937              :     }
     938              : 
     939              :   /* Now we know that the induction variable does not overflow, so the loop
     940              :      iterates at most (range of type / S) times.  */
     941      7286155 :   wi::to_mpz (wi::minus_one (TYPE_PRECISION (type)), bnd, UNSIGNED);
     942              : 
     943              :   /* If the induction variable is guaranteed to reach the value of C before
     944              :      overflow, ... */
     945      7286155 :   if (exit_must_be_taken)
     946              :     {
     947              :       /* ... then we can strengthen this to C / S, and possibly we can use
     948              :          the upper bound on C given by BNDS.  */
     949      7045737 :       if (TREE_CODE (c) == INTEGER_CST)
     950      6197486 :         wi::to_mpz (wi::to_wide (c), bnd, UNSIGNED);
     951       848251 :       else if (bnds_u_valid)
     952       719279 :         mpz_set (bnd, bnds->up);
     953              :     }
     954              : 
     955      7286155 :   mpz_init (d);
     956      7286155 :   wi::to_mpz (wi::to_wide (s), d, UNSIGNED);
     957      7286155 :   mpz_fdiv_q (bnd, bnd, d);
     958      7286155 :   mpz_clear (d);
     959      7346834 : }
     960              : 
     961              : /* Determines number of iterations of loop whose ending condition
     962              :    is IV <> FINAL.  TYPE is the type of the iv.  The number of
     963              :    iterations is stored to NITER.  EXIT_MUST_BE_TAKEN is true if
     964              :    we know that the exit must be taken eventually, i.e., that the IV
     965              :    ever reaches the value FINAL (we derived this earlier, and possibly set
     966              :    NITER->assumptions to make sure this is the case).  BNDS contains the
     967              :    bounds on the difference FINAL - IV->base.  */
     968              : 
     969              : static bool
     970      7346834 : number_of_iterations_ne (class loop *loop, tree type, affine_iv *iv,
     971              :                          tree final, class tree_niter_desc *niter,
     972              :                          bool exit_must_be_taken, bounds *bnds)
     973              : {
     974      7346834 :   tree niter_type = unsigned_type_for (type);
     975      7346834 :   tree s, c, d, bits, assumption, tmp, bound;
     976              : 
     977      7346834 :   niter->control = *iv;
     978      7346834 :   niter->bound = final;
     979      7346834 :   niter->cmp = NE_EXPR;
     980              : 
     981              :   /* Rearrange the terms so that we get inequality S * i <> C, with S
     982              :      positive.  Also cast everything to the unsigned type.  If IV does
     983              :      not overflow, BNDS bounds the value of C.  Also, this is the
     984              :      case if the computation |FINAL - IV->base| does not overflow, i.e.,
     985              :      if BNDS->below in the result is nonnegative.  */
     986      7346834 :   if (tree_int_cst_sign_bit (iv->step))
     987              :     {
     988      2496789 :       s = fold_build1 (NEGATE_EXPR, niter_type,
     989              :                        fold_convert (niter_type, iv->step));
     990      2496789 :       c = fold_build2 (MINUS_EXPR, niter_type,
     991              :                        fold_convert (niter_type, iv->base),
     992              :                        fold_convert (niter_type, final));
     993      2496789 :       bounds_negate (bnds);
     994              :     }
     995              :   else
     996              :     {
     997      4850045 :       s = fold_convert (niter_type, iv->step);
     998      4850045 :       c = fold_build2 (MINUS_EXPR, niter_type,
     999              :                        fold_convert (niter_type, final),
    1000              :                        fold_convert (niter_type, iv->base));
    1001              :     }
    1002              : 
    1003      7346834 :   auto_mpz max;
    1004      7346834 :   number_of_iterations_ne_max (max, iv->no_overflow, c, s, bnds,
    1005              :                                exit_must_be_taken);
    1006     14693668 :   niter->max = widest_int::from (wi::from_mpz (niter_type, max, false),
    1007     14693668 :                                  TYPE_SIGN (niter_type));
    1008              : 
    1009              :   /* Compute no-overflow information for the control iv.  This can be
    1010              :      proven when below two conditions are satisfied:
    1011              : 
    1012              :        1) IV evaluates toward FINAL at beginning, i.e:
    1013              :             base <= FINAL ; step > 0
    1014              :             base >= FINAL ; step < 0
    1015              : 
    1016              :        2) |FINAL - base| is an exact multiple of step.
    1017              : 
    1018              :      Unfortunately, it's hard to prove above conditions after pass loop-ch
    1019              :      because loop with exit condition (IV != FINAL) usually will be guarded
    1020              :      by initial-condition (IV.base - IV.step != FINAL).  In this case, we
    1021              :      can alternatively try to prove below conditions:
    1022              : 
    1023              :        1') IV evaluates toward FINAL at beginning, i.e:
    1024              :             new_base = base - step < FINAL ; step > 0
    1025              :                                              && base - step doesn't underflow
    1026              :             new_base = base - step > FINAL ; step < 0
    1027              :                                              && base - step doesn't overflow
    1028              : 
    1029              :      Please refer to PR34114 as an example of loop-ch's impact.
    1030              : 
    1031              :      Note, for NE_EXPR, base equals to FINAL is a special case, in
    1032              :      which the loop exits immediately, and the iv does not overflow.
    1033              : 
    1034              :      Also note, we prove condition 2) by checking base and final separately
    1035              :      along with condition 1) or 1').  Since we ensure the difference
    1036              :      computation of c does not wrap with cond below and the adjusted s
    1037              :      will fit a signed type as well as an unsigned we can safely do
    1038              :      this using the type of the IV if it is not pointer typed.  */
    1039      7346834 :   tree mtype = type;
    1040      7346834 :   if (POINTER_TYPE_P (type))
    1041       576591 :     mtype = niter_type;
    1042      7346834 :   if (!niter->control.no_overflow
    1043      7346834 :       && (integer_onep (s)
    1044       198408 :           || (multiple_of_p (mtype, fold_convert (mtype, iv->base),
    1045       198408 :                              fold_convert (mtype, s), false)
    1046        30170 :               && multiple_of_p (mtype, fold_convert (mtype, final),
    1047        30170 :                                 fold_convert (mtype, s), false))))
    1048              :     {
    1049      2336630 :       tree t, cond, relaxed_cond = boolean_false_node;
    1050              : 
    1051      2336630 :       if (tree_int_cst_sign_bit (iv->step))
    1052              :         {
    1053      2256550 :           cond = fold_build2 (GE_EXPR, boolean_type_node, iv->base, final);
    1054      2256550 :           if (INTEGRAL_NB_TYPE_P (type))
    1055              :             {
    1056              :               /* Only when base - step doesn't overflow.  */
    1057      2256550 :               t = TYPE_MAX_VALUE (type);
    1058      2256550 :               t = fold_build2 (PLUS_EXPR, type, t, iv->step);
    1059      2256550 :               t = fold_build2 (GE_EXPR, boolean_type_node, t, iv->base);
    1060      2256550 :               if (integer_nonzerop (t))
    1061              :                 {
    1062      2099676 :                   t = fold_build2 (MINUS_EXPR, type, iv->base, iv->step);
    1063      2099676 :                   relaxed_cond = fold_build2 (GT_EXPR, boolean_type_node, t,
    1064              :                                               final);
    1065              :                 }
    1066              :             }
    1067              :         }
    1068              :       else
    1069              :         {
    1070        80080 :           cond = fold_build2 (LE_EXPR, boolean_type_node, iv->base, final);
    1071        80080 :           if (INTEGRAL_NB_TYPE_P (type))
    1072              :             {
    1073              :               /* Only when base - step doesn't underflow.  */
    1074        80080 :               t = TYPE_MIN_VALUE (type);
    1075        80080 :               t = fold_build2 (PLUS_EXPR, type, t, iv->step);
    1076        80080 :               t = fold_build2 (LE_EXPR, boolean_type_node, t, iv->base);
    1077        80080 :               if (integer_nonzerop (t))
    1078              :                 {
    1079        45268 :                   t = fold_build2 (MINUS_EXPR, type, iv->base, iv->step);
    1080        45268 :                   relaxed_cond = fold_build2 (LT_EXPR, boolean_type_node, t,
    1081              :                                               final);
    1082              :                 }
    1083              :             }
    1084              :         }
    1085              : 
    1086      2336630 :       t = simplify_using_initial_conditions (loop, cond);
    1087      2336630 :       if (!t || !integer_onep (t))
    1088        57933 :         t = simplify_using_initial_conditions (loop, relaxed_cond);
    1089              : 
    1090      2336630 :       if (t && integer_onep (t))
    1091              :         {
    1092      2278976 :           niter->control.no_overflow = true;
    1093      2278976 :           niter->niter = fold_build2 (EXACT_DIV_EXPR, niter_type, c, s);
    1094      2278976 :           return true;
    1095              :         }
    1096              :     }
    1097              : 
    1098              :   /* Let nsd (step, size of mode) = d.  If d does not divide c, the loop
    1099              :      is infinite.  Otherwise, the number of iterations is
    1100              :      (inverse(s/d) * (c/d)) mod (size of mode/d).  */
    1101      5067858 :   bits = num_ending_zeros (s);
    1102     15203574 :   bound = build_low_bits_mask (niter_type,
    1103      5067858 :                                (TYPE_PRECISION (niter_type)
    1104      5067858 :                                 - tree_to_uhwi (bits)));
    1105              : 
    1106      5067858 :   d = fold_binary_to_constant (LSHIFT_EXPR, niter_type,
    1107              :                                build_int_cst (niter_type, 1), bits);
    1108      5067858 :   s = fold_binary_to_constant (RSHIFT_EXPR, niter_type, s, bits);
    1109              : 
    1110      5067858 :   if (!exit_must_be_taken)
    1111              :     {
    1112              :       /* If we cannot assume that the exit is taken eventually, record the
    1113              :          assumptions for divisibility of c.  */
    1114      1812139 :       assumption = fold_build2 (FLOOR_MOD_EXPR, niter_type, c, d);
    1115      1812139 :       assumption = fold_build2 (EQ_EXPR, boolean_type_node,
    1116              :                                 assumption, build_int_cst (niter_type, 0));
    1117      1812139 :       if (!integer_nonzerop (assumption))
    1118       285589 :         niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
    1119              :                                           niter->assumptions, assumption);
    1120              :     }
    1121              : 
    1122      5067858 :   c = fold_build2 (EXACT_DIV_EXPR, niter_type, c, d);
    1123      5067858 :   if (integer_onep (s))
    1124              :     {
    1125      4894000 :       niter->niter = c;
    1126              :     }
    1127              :   else
    1128              :     {
    1129       173858 :       tmp = fold_build2 (MULT_EXPR, niter_type, c, inverse (s, bound));
    1130       173858 :       niter->niter = fold_build2 (BIT_AND_EXPR, niter_type, tmp, bound);
    1131              :     }
    1132              :   return true;
    1133      7346834 : }
    1134              : 
    1135              : /* Checks whether we can determine the final value of the control variable
    1136              :    of the loop with ending condition IV0 < IV1 (computed in TYPE).
    1137              :    DELTA is the difference IV1->base - IV0->base, STEP is the absolute value
    1138              :    of the step.  The assumptions necessary to ensure that the computation
    1139              :    of the final value does not overflow are recorded in NITER.  If we
    1140              :    find the final value, we adjust DELTA and return TRUE.  Otherwise
    1141              :    we return false.  BNDS bounds the value of IV1->base - IV0->base,
    1142              :    and will be updated by the same amount as DELTA.  EXIT_MUST_BE_TAKEN is
    1143              :    true if we know that the exit must be taken eventually.  */
    1144              : 
    1145              : static bool
    1146       384053 : number_of_iterations_lt_to_ne (tree type, affine_iv *iv0, affine_iv *iv1,
    1147              :                                class tree_niter_desc *niter,
    1148              :                                tree *delta, tree step,
    1149              :                                bool exit_must_be_taken, bounds *bnds)
    1150              : {
    1151       384053 :   tree niter_type = TREE_TYPE (step);
    1152       384053 :   tree mod = fold_build2 (FLOOR_MOD_EXPR, niter_type, *delta, step);
    1153       384053 :   tree tmod;
    1154       384053 :   tree assumption = boolean_true_node, bound, noloop;
    1155       384053 :   bool fv_comp_no_overflow;
    1156       384053 :   tree type1 = type;
    1157       384053 :   if (POINTER_TYPE_P (type))
    1158       124600 :     type1 = sizetype;
    1159              : 
    1160       384053 :   if (TREE_CODE (mod) != INTEGER_CST)
    1161              :     return false;
    1162        35004 :   if (integer_nonzerop (mod))
    1163        16271 :     mod = fold_build2 (MINUS_EXPR, niter_type, step, mod);
    1164        35004 :   tmod = fold_convert (type1, mod);
    1165              : 
    1166        35004 :   auto_mpz mmod;
    1167        35004 :   wi::to_mpz (wi::to_wide (mod), mmod, UNSIGNED);
    1168        35004 :   mpz_neg (mmod, mmod);
    1169              : 
    1170              :   /* If the induction variable does not overflow and the exit is taken,
    1171              :      then the computation of the final value does not overflow.  This is
    1172              :      also obviously the case if the new final value is equal to the
    1173              :      current one.  Finally, we postulate this for pointer type variables,
    1174              :      as the code cannot rely on the object to that the pointer points being
    1175              :      placed at the end of the address space (and more pragmatically,
    1176              :      TYPE_{MIN,MAX}_VALUE is not defined for pointers).  */
    1177        35004 :   if (integer_zerop (mod) || POINTER_TYPE_P (type))
    1178              :     fv_comp_no_overflow = true;
    1179        15590 :   else if (!exit_must_be_taken)
    1180              :     fv_comp_no_overflow = false;
    1181              :   else
    1182         7921 :     fv_comp_no_overflow =
    1183         7921 :             (iv0->no_overflow && integer_nonzerop (iv0->step))
    1184         8849 :             || (iv1->no_overflow && integer_nonzerop (iv1->step));
    1185              : 
    1186        35004 :   if (integer_nonzerop (iv0->step))
    1187              :     {
    1188              :       /* The final value of the iv is iv1->base + MOD, assuming that this
    1189              :          computation does not overflow, and that
    1190              :          iv0->base <= iv1->base + MOD.  */
    1191        30453 :       if (!fv_comp_no_overflow)
    1192              :         {
    1193         5884 :           bound = fold_build2 (MINUS_EXPR, type1,
    1194              :                                TYPE_MAX_VALUE (type1), tmod);
    1195         5884 :           assumption = fold_build2 (LE_EXPR, boolean_type_node,
    1196              :                                     iv1->base, bound);
    1197         5884 :           if (integer_zerop (assumption))
    1198              :             return false;
    1199              :         }
    1200              :     }
    1201              :   else
    1202              :     {
    1203              :       /* The final value of the iv is iv0->base - MOD, assuming that this
    1204              :          computation does not overflow, and that
    1205              :          iv0->base - MOD <= iv1->base. */
    1206         4551 :       if (!fv_comp_no_overflow)
    1207              :         {
    1208         1785 :           bound = fold_build2 (PLUS_EXPR, type1,
    1209              :                                TYPE_MIN_VALUE (type1), tmod);
    1210         1785 :           assumption = fold_build2 (GE_EXPR, boolean_type_node,
    1211              :                                     iv0->base, bound);
    1212         1785 :           if (integer_zerop (assumption))
    1213              :             return false;
    1214              :         }
    1215              :     }
    1216              : 
    1217              :   /* IV0 < IV1 does not loop if IV0->base >= IV1->base.  */
    1218        34948 :   if (fv_comp_no_overflow && mpz_cmp (mmod, bnds->below) < 0)
    1219        16423 :     noloop = boolean_false_node;
    1220              :   else
    1221        18525 :     noloop = fold_build2 (GE_EXPR, boolean_type_node,
    1222              :                           iv0->base, iv1->base);
    1223              : 
    1224        34948 :   if (!integer_nonzerop (assumption))
    1225          401 :     niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
    1226              :                                       niter->assumptions,
    1227              :                                       assumption);
    1228        34948 :   if (!integer_zerop (noloop))
    1229         3660 :     niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
    1230              :                                       niter->may_be_zero,
    1231              :                                       noloop);
    1232        34948 :   bounds_add (bnds, wi::to_widest (mod), type);
    1233        34948 :   *delta = fold_build2 (PLUS_EXPR, niter_type, *delta, mod);
    1234              : 
    1235        34948 :   return true;
    1236        35004 : }
    1237              : 
    1238              : /* Add assertions to NITER that ensure that the control variable of the loop
    1239              :    with ending condition IV0 < IV1 does not overflow.  Types of IV0 and IV1
    1240              :    are TYPE.  Returns false if we can prove that there is an overflow, true
    1241              :    otherwise.  STEP is the absolute value of the step.  */
    1242              : 
    1243              : static bool
    1244       349105 : assert_no_overflow_lt (tree type, affine_iv *iv0, affine_iv *iv1,
    1245              :                        class tree_niter_desc *niter, tree step)
    1246              : {
    1247       349105 :   tree bound, d, assumption, diff;
    1248       349105 :   tree niter_type = TREE_TYPE (step);
    1249              : 
    1250       349105 :   if (integer_nonzerop (iv0->step))
    1251              :     {
    1252              :       /* for (i = iv0->base; i < iv1->base; i += iv0->step) */
    1253       287023 :       if (iv0->no_overflow)
    1254              :         return true;
    1255              : 
    1256              :       /* If iv0->base is a constant, we can determine the last value before
    1257              :          overflow precisely; otherwise we conservatively assume
    1258              :          MAX - STEP + 1.  */
    1259              : 
    1260        47181 :       if (TREE_CODE (iv0->base) == INTEGER_CST)
    1261              :         {
    1262        16087 :           d = fold_build2 (MINUS_EXPR, niter_type,
    1263              :                            fold_convert (niter_type, TYPE_MAX_VALUE (type)),
    1264              :                            fold_convert (niter_type, iv0->base));
    1265        16087 :           diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step);
    1266              :         }
    1267              :       else
    1268        31094 :         diff = fold_build2 (MINUS_EXPR, niter_type, step,
    1269              :                             build_int_cst (niter_type, 1));
    1270        47181 :       bound = fold_build2 (MINUS_EXPR, type,
    1271              :                            TYPE_MAX_VALUE (type), fold_convert (type, diff));
    1272        47181 :       assumption = fold_build2 (LE_EXPR, boolean_type_node,
    1273              :                                 iv1->base, bound);
    1274              :     }
    1275              :   else
    1276              :     {
    1277              :       /* for (i = iv1->base; i > iv0->base; i += iv1->step) */
    1278        62082 :       if (iv1->no_overflow)
    1279              :         return true;
    1280              : 
    1281        31577 :       if (TREE_CODE (iv1->base) == INTEGER_CST)
    1282              :         {
    1283           56 :           d = fold_build2 (MINUS_EXPR, niter_type,
    1284              :                            fold_convert (niter_type, iv1->base),
    1285              :                            fold_convert (niter_type, TYPE_MIN_VALUE (type)));
    1286           56 :           diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step);
    1287              :         }
    1288              :       else
    1289        31521 :         diff = fold_build2 (MINUS_EXPR, niter_type, step,
    1290              :                             build_int_cst (niter_type, 1));
    1291        31577 :       bound = fold_build2 (PLUS_EXPR, type,
    1292              :                            TYPE_MIN_VALUE (type), fold_convert (type, diff));
    1293        31577 :       assumption = fold_build2 (GE_EXPR, boolean_type_node,
    1294              :                                 iv0->base, bound);
    1295              :     }
    1296              : 
    1297        78758 :   if (integer_zerop (assumption))
    1298              :     return false;
    1299        78694 :   if (!integer_nonzerop (assumption))
    1300        56457 :     niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
    1301              :                                       niter->assumptions, assumption);
    1302              : 
    1303        78694 :   iv0->no_overflow = true;
    1304        78694 :   iv1->no_overflow = true;
    1305        78694 :   return true;
    1306              : }
    1307              : 
    1308              : /* Add an assumption to NITER that a loop whose ending condition
    1309              :    is IV0 < IV1 rolls.  TYPE is the type of the control iv.  BNDS
    1310              :    bounds the value of IV1->base - IV0->base.  */
    1311              : 
    1312              : static void
    1313       349041 : assert_loop_rolls_lt (tree type, affine_iv *iv0, affine_iv *iv1,
    1314              :                       class tree_niter_desc *niter, bounds *bnds)
    1315              : {
    1316       349041 :   tree assumption = boolean_true_node, bound, diff;
    1317       349041 :   tree mbz, mbzl, mbzr, type1;
    1318       349041 :   bool rolls_p, no_overflow_p;
    1319       349041 :   widest_int dstep;
    1320       349041 :   mpz_t mstep, max;
    1321              : 
    1322              :   /* We are going to compute the number of iterations as
    1323              :      (iv1->base - iv0->base + step - 1) / step, computed in the unsigned
    1324              :      variant of TYPE.  This formula only works if
    1325              : 
    1326              :      -step + 1 <= (iv1->base - iv0->base) <= MAX - step + 1
    1327              : 
    1328              :      (where MAX is the maximum value of the unsigned variant of TYPE, and
    1329              :      the computations in this formula are performed in full precision,
    1330              :      i.e., without overflows).
    1331              : 
    1332              :      Usually, for loops with exit condition iv0->base + step * i < iv1->base,
    1333              :      we have a condition of the form iv0->base - step < iv1->base before the loop,
    1334              :      and for loops iv0->base < iv1->base - step * i the condition
    1335              :      iv0->base < iv1->base + step, due to loop header copying, which enable us
    1336              :      to prove the lower bound.
    1337              : 
    1338              :      The upper bound is more complicated.  Unless the expressions for initial
    1339              :      and final value themselves contain enough information, we usually cannot
    1340              :      derive it from the context.  */
    1341              : 
    1342              :   /* First check whether the answer does not follow from the bounds we gathered
    1343              :      before.  */
    1344       349041 :   if (integer_nonzerop (iv0->step))
    1345       287023 :     dstep = wi::to_widest (iv0->step);
    1346              :   else
    1347              :     {
    1348        62018 :       dstep = wi::sext (wi::to_widest (iv1->step), TYPE_PRECISION (type));
    1349        62018 :       dstep = -dstep;
    1350              :     }
    1351              : 
    1352       349041 :   mpz_init (mstep);
    1353       349041 :   wi::to_mpz (dstep, mstep, UNSIGNED);
    1354       349041 :   mpz_neg (mstep, mstep);
    1355       349041 :   mpz_add_ui (mstep, mstep, 1);
    1356              : 
    1357       349041 :   rolls_p = mpz_cmp (mstep, bnds->below) <= 0;
    1358              : 
    1359       349041 :   mpz_init (max);
    1360       349041 :   wi::to_mpz (wi::minus_one (TYPE_PRECISION (type)), max, UNSIGNED);
    1361       349041 :   mpz_add (max, max, mstep);
    1362       698082 :   no_overflow_p = (mpz_cmp (bnds->up, max) <= 0
    1363              :                    /* For pointers, only values lying inside a single object
    1364              :                       can be compared or manipulated by pointer arithmetics.
    1365              :                       Gcc in general does not allow or handle objects larger
    1366              :                       than half of the address space, hence the upper bound
    1367              :                       is satisfied for pointers.  */
    1368       349041 :                    || POINTER_TYPE_P (type));
    1369       349041 :   mpz_clear (mstep);
    1370       349041 :   mpz_clear (max);
    1371              : 
    1372       349041 :   if (rolls_p && no_overflow_p)
    1373       131197 :     return;
    1374              : 
    1375       217844 :   type1 = type;
    1376       217844 :   if (POINTER_TYPE_P (type))
    1377        63851 :     type1 = sizetype;
    1378              : 
    1379              :   /* Now the hard part; we must formulate the assumption(s) as expressions, and
    1380              :      we must be careful not to introduce overflow.  */
    1381              : 
    1382       217844 :   if (integer_nonzerop (iv0->step))
    1383              :     {
    1384       177047 :       diff = fold_build2 (MINUS_EXPR, type1,
    1385              :                           iv0->step, build_int_cst (type1, 1));
    1386              : 
    1387              :       /* We need to know that iv0->base >= MIN + iv0->step - 1.  Since
    1388              :          0 address never belongs to any object, we can assume this for
    1389              :          pointers.  */
    1390       177047 :       if (!POINTER_TYPE_P (type))
    1391              :         {
    1392       117034 :           bound = fold_build2 (PLUS_EXPR, type1,
    1393              :                                TYPE_MIN_VALUE (type), diff);
    1394       117034 :           assumption = fold_build2 (GE_EXPR, boolean_type_node,
    1395              :                                     iv0->base, bound);
    1396              :         }
    1397              : 
    1398              :       /* And then we can compute iv0->base - diff, and compare it with
    1399              :          iv1->base.  */
    1400       177047 :       mbzl = fold_build2 (MINUS_EXPR, type1,
    1401              :                           fold_convert (type1, iv0->base), diff);
    1402       177047 :       mbzr = fold_convert (type1, iv1->base);
    1403              :     }
    1404              :   else
    1405              :     {
    1406        40797 :       diff = fold_build2 (PLUS_EXPR, type1,
    1407              :                           iv1->step, build_int_cst (type1, 1));
    1408              : 
    1409        40797 :       if (!POINTER_TYPE_P (type))
    1410              :         {
    1411        36959 :           bound = fold_build2 (PLUS_EXPR, type1,
    1412              :                                TYPE_MAX_VALUE (type), diff);
    1413        36959 :           assumption = fold_build2 (LE_EXPR, boolean_type_node,
    1414              :                                     iv1->base, bound);
    1415              :         }
    1416              : 
    1417        40797 :       mbzl = fold_convert (type1, iv0->base);
    1418        40797 :       mbzr = fold_build2 (MINUS_EXPR, type1,
    1419              :                           fold_convert (type1, iv1->base), diff);
    1420              :     }
    1421              : 
    1422       217844 :   if (!integer_nonzerop (assumption))
    1423        93217 :     niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
    1424              :                                       niter->assumptions, assumption);
    1425       217844 :   if (!rolls_p)
    1426              :     {
    1427       201325 :       mbz = fold_build2 (GT_EXPR, boolean_type_node, mbzl, mbzr);
    1428       201325 :       niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
    1429              :                                         niter->may_be_zero, mbz);
    1430              :     }
    1431       349041 : }
    1432              : 
    1433              : /* Determines number of iterations of loop whose ending condition
    1434              :    is IV0 < IV1 which likes:  {base, -C} < n,  or n < {base, C}.
    1435              :    The number of iterations is stored to NITER.  */
    1436              : 
    1437              : static bool
    1438        67253 : number_of_iterations_until_wrap (class loop *loop, tree type, affine_iv *iv0,
    1439              :                                  affine_iv *iv1, class tree_niter_desc *niter)
    1440              : {
    1441        67253 :   tree niter_type = unsigned_type_for (type);
    1442        67253 :   tree step, num, assumptions, may_be_zero, span;
    1443        67253 :   wide_int high, low, max, min;
    1444              : 
    1445        67253 :   may_be_zero = fold_build2 (LE_EXPR, boolean_type_node, iv1->base, iv0->base);
    1446        67253 :   if (integer_onep (may_be_zero))
    1447              :     return false;
    1448              : 
    1449        67043 :   int prec = TYPE_PRECISION (type);
    1450        67043 :   signop sgn = TYPE_SIGN (type);
    1451        67043 :   min = wi::min_value (prec, sgn);
    1452        67043 :   max = wi::max_value (prec, sgn);
    1453              : 
    1454              :   /* n < {base, C}. */
    1455        67043 :   if (integer_zerop (iv0->step) && !tree_int_cst_sign_bit (iv1->step))
    1456              :     {
    1457              :       /* MIN + C - 1 <= n.  */
    1458        42330 :       tree last = wide_int_to_tree (type, min + wi::to_wide (iv1->step) - 1);
    1459        42330 :       assumptions = fold_build2 (LE_EXPR, boolean_type_node, last, iv0->base);
    1460        42330 :       if (integer_zerop (assumptions))
    1461              :         return false;
    1462              : 
    1463        42330 :       step = fold_convert (niter_type, iv1->step);
    1464        42330 :       num = fold_build2 (MINUS_EXPR, niter_type,
    1465              :                          wide_int_to_tree (niter_type, max),
    1466              :                          fold_convert (niter_type, iv1->base));
    1467              : 
    1468              :       /* When base has the form iv + 1, if we know iv >= n, then iv + 1 < n
    1469              :          only when iv + 1 overflows, i.e. when iv == TYPE_VALUE_MAX.  */
    1470        42330 :       if (sgn == UNSIGNED
    1471         5909 :           && integer_onep (step)
    1472          914 :           && TREE_CODE (iv1->base) == PLUS_EXPR
    1473        42915 :           && integer_onep (TREE_OPERAND (iv1->base, 1)))
    1474              :         {
    1475          434 :           tree cond = fold_build2 (GE_EXPR, boolean_type_node,
    1476              :                                    TREE_OPERAND (iv1->base, 0), iv0->base);
    1477          434 :           cond = simplify_using_initial_conditions (loop, cond);
    1478          434 :           if (integer_onep (cond))
    1479           16 :             may_be_zero = fold_build2 (EQ_EXPR, boolean_type_node,
    1480              :                                        TREE_OPERAND (iv1->base, 0),
    1481              :                                        TYPE_MAX_VALUE (type));
    1482              :         }
    1483              : 
    1484        42330 :       high = max;
    1485        42330 :       if (TREE_CODE (iv1->base) == INTEGER_CST)
    1486        29890 :         low = wi::to_wide (iv1->base) - 1;
    1487        12440 :       else if (TREE_CODE (iv0->base) == INTEGER_CST)
    1488         5726 :         low = wi::to_wide (iv0->base);
    1489              :       else
    1490         6714 :         low = min;
    1491              :     }
    1492              :   /* {base, -C} < n.  */
    1493        24713 :   else if (tree_int_cst_sign_bit (iv0->step) && integer_zerop (iv1->step))
    1494              :     {
    1495              :       /* MAX + (-C) + 1 >= n.  */
    1496        24713 :       tree last = wide_int_to_tree (type, max + wi::to_wide (iv0->step) + 1);
    1497        24713 :       assumptions = fold_build2 (GE_EXPR, boolean_type_node, last, iv1->base);
    1498        24713 :       if (integer_zerop (assumptions))
    1499              :         return false;
    1500              : 
    1501        24711 :       step = fold_build1 (NEGATE_EXPR, niter_type,
    1502              :                           fold_convert (niter_type, iv0->step));
    1503        24711 :       num = fold_build2 (MINUS_EXPR, niter_type,
    1504              :                          fold_convert (niter_type, iv0->base),
    1505              :                          wide_int_to_tree (niter_type, min));
    1506        24711 :       low = min;
    1507        24711 :       if (TREE_CODE (iv0->base) == INTEGER_CST)
    1508         1321 :         high = wi::to_wide (iv0->base) + 1;
    1509        23390 :       else if (TREE_CODE (iv1->base) == INTEGER_CST)
    1510         2294 :         high = wi::to_wide (iv1->base);
    1511              :       else
    1512        21096 :         high = max;
    1513              :     }
    1514              :   else
    1515            0 :     return false;
    1516              : 
    1517              :   /* (delta + step - 1) / step */
    1518        67041 :   num = fold_build2 (PLUS_EXPR, niter_type, num, step);
    1519        67041 :   niter->niter = fold_build2 (FLOOR_DIV_EXPR, niter_type, num, step);
    1520              : 
    1521        67041 :   widest_int delta, s;
    1522        67041 :   delta = widest_int::from (high, sgn) - widest_int::from (low, sgn);
    1523        67041 :   s = wi::to_widest (step);
    1524        67041 :   delta = delta + s - 1;
    1525        67041 :   niter->max = wi::udiv_floor (delta, s);
    1526              : 
    1527        67041 :   niter->may_be_zero = may_be_zero;
    1528              : 
    1529        67041 :   if (!integer_nonzerop (assumptions))
    1530         9245 :     niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
    1531              :                                       niter->assumptions, assumptions);
    1532              : 
    1533        67041 :   niter->control.no_overflow = false;
    1534              : 
    1535              :   /* Update bound and exit condition as:
    1536              :      bound = niter * STEP + (IVbase - STEP).
    1537              :      { IVbase - STEP, +, STEP } != bound
    1538              :      Here, biasing IVbase by 1 step makes 'bound' be the value before wrap.
    1539              :      */
    1540        67041 :   tree base_type = TREE_TYPE (niter->control.base);
    1541        67041 :   if (POINTER_TYPE_P (base_type))
    1542              :     {
    1543         6527 :       tree utype = unsigned_type_for (base_type);
    1544         6527 :       niter->control.base
    1545         6527 :         = fold_build2 (MINUS_EXPR, utype,
    1546              :                        fold_convert (utype, niter->control.base),
    1547              :                        fold_convert (utype, niter->control.step));
    1548         6527 :       niter->control.base = fold_convert (base_type, niter->control.base);
    1549              :     }
    1550              :   else
    1551        60514 :     niter->control.base
    1552        60514 :       = fold_build2 (MINUS_EXPR, base_type, niter->control.base,
    1553              :                      niter->control.step);
    1554              : 
    1555        67041 :   span = fold_build2 (MULT_EXPR, niter_type, niter->niter,
    1556              :                       fold_convert (niter_type, niter->control.step));
    1557        67041 :   niter->bound = fold_build2 (PLUS_EXPR, niter_type, span,
    1558              :                               fold_convert (niter_type, niter->control.base));
    1559        67041 :   niter->bound = fold_convert (type, niter->bound);
    1560        67041 :   niter->cmp = NE_EXPR;
    1561              : 
    1562        67041 :   return true;
    1563       134294 : }
    1564              : 
    1565              : /* Determines number of iterations of loop whose ending condition
    1566              :    is IV0 < IV1.  TYPE is the type of the iv.  The number of
    1567              :    iterations is stored to NITER.  BNDS bounds the difference
    1568              :    IV1->base - IV0->base.  EXIT_MUST_BE_TAKEN is true if we know
    1569              :    that the exit must be taken eventually.  */
    1570              : 
    1571              : static bool
    1572      5338377 : number_of_iterations_lt (class loop *loop, tree type, affine_iv *iv0,
    1573              :                          affine_iv *iv1, class tree_niter_desc *niter,
    1574              :                          bool exit_must_be_taken, bounds *bnds)
    1575              : {
    1576      5338377 :   tree niter_type = unsigned_type_for (type);
    1577      5338377 :   tree delta, step, s;
    1578      5338377 :   mpz_t mstep, tmp;
    1579              : 
    1580      5338377 :   if (integer_nonzerop (iv0->step))
    1581              :     {
    1582      5010385 :       niter->control = *iv0;
    1583      5010385 :       niter->cmp = LT_EXPR;
    1584      5010385 :       niter->bound = iv1->base;
    1585              :     }
    1586              :   else
    1587              :     {
    1588       327992 :       niter->control = *iv1;
    1589       327992 :       niter->cmp = GT_EXPR;
    1590       327992 :       niter->bound = iv0->base;
    1591              :     }
    1592              : 
    1593              :   /* {base, -C} < n,  or n < {base, C} */
    1594      5338377 :   if (tree_int_cst_sign_bit (iv0->step)
    1595      5338377 :       || (!integer_zerop (iv1->step) && !tree_int_cst_sign_bit (iv1->step)))
    1596        67253 :     return number_of_iterations_until_wrap (loop, type, iv0, iv1, niter);
    1597              : 
    1598      5271124 :   delta = fold_build2 (MINUS_EXPR, niter_type,
    1599              :                        fold_convert (niter_type, iv1->base),
    1600              :                        fold_convert (niter_type, iv0->base));
    1601              : 
    1602              :   /* First handle the special case that the step is +-1.  */
    1603      9939320 :   if ((integer_onep (iv0->step) && integer_zerop (iv1->step))
    1604      5271124 :       || (integer_all_onesp (iv1->step) && integer_zerop (iv0->step)))
    1605              :     {
    1606              :       /* for (i = iv0->base; i < iv1->base; i++)
    1607              : 
    1608              :          or
    1609              : 
    1610              :          for (i = iv1->base; i > iv0->base; i--).
    1611              : 
    1612              :          In both cases # of iterations is iv1->base - iv0->base, assuming that
    1613              :          iv1->base >= iv0->base.
    1614              : 
    1615              :          First try to derive a lower bound on the value of
    1616              :          iv1->base - iv0->base, computed in full precision.  If the difference
    1617              :          is nonnegative, we are done, otherwise we must record the
    1618              :          condition.  */
    1619              : 
    1620      4887071 :       if (mpz_sgn (bnds->below) < 0)
    1621      1830531 :         niter->may_be_zero = fold_build2 (LT_EXPR, boolean_type_node,
    1622              :                                           iv1->base, iv0->base);
    1623      4887071 :       niter->niter = delta;
    1624      9774142 :       niter->max = widest_int::from (wi::from_mpz (niter_type, bnds->up, false),
    1625      9774142 :                                      TYPE_SIGN (niter_type));
    1626      4887071 :       niter->control.no_overflow = true;
    1627      4887071 :       return true;
    1628              :     }
    1629              : 
    1630       384053 :   if (integer_nonzerop (iv0->step))
    1631       317476 :     step = fold_convert (niter_type, iv0->step);
    1632              :   else
    1633        66577 :     step = fold_build1 (NEGATE_EXPR, niter_type,
    1634              :                         fold_convert (niter_type, iv1->step));
    1635              : 
    1636              :   /* If we can determine the final value of the control iv exactly, we can
    1637              :      transform the condition to != comparison.  In particular, this will be
    1638              :      the case if DELTA is constant.  */
    1639       384053 :   if (number_of_iterations_lt_to_ne (type, iv0, iv1, niter, &delta, step,
    1640              :                                      exit_must_be_taken, bnds))
    1641              :     {
    1642        34948 :       affine_iv zps;
    1643              : 
    1644        34948 :       zps.base = build_int_cst (niter_type, 0);
    1645        34948 :       zps.step = step;
    1646              :       /* number_of_iterations_lt_to_ne will add assumptions that ensure that
    1647              :          zps does not overflow.  */
    1648        34948 :       zps.no_overflow = true;
    1649              : 
    1650        34948 :       return number_of_iterations_ne (loop, type, &zps,
    1651              :                                       delta, niter, true, bnds);
    1652              :     }
    1653              : 
    1654              :   /* Make sure that the control iv does not overflow.  */
    1655       349105 :   if (!assert_no_overflow_lt (type, iv0, iv1, niter, step))
    1656              :     return false;
    1657              : 
    1658              :   /* We determine the number of iterations as (delta + step - 1) / step.  For
    1659              :      this to work, we must know that iv1->base >= iv0->base - step + 1,
    1660              :      otherwise the loop does not roll.  */
    1661       349041 :   assert_loop_rolls_lt (type, iv0, iv1, niter, bnds);
    1662              : 
    1663       349041 :   s = fold_build2 (MINUS_EXPR, niter_type,
    1664              :                    step, build_int_cst (niter_type, 1));
    1665       349041 :   delta = fold_build2 (PLUS_EXPR, niter_type, delta, s);
    1666       349041 :   niter->niter = fold_build2 (FLOOR_DIV_EXPR, niter_type, delta, step);
    1667              : 
    1668       349041 :   mpz_init (mstep);
    1669       349041 :   mpz_init (tmp);
    1670       349041 :   wi::to_mpz (wi::to_wide (step), mstep, UNSIGNED);
    1671       349041 :   mpz_add (tmp, bnds->up, mstep);
    1672       349041 :   mpz_sub_ui (tmp, tmp, 1);
    1673       349041 :   mpz_fdiv_q (tmp, tmp, mstep);
    1674       698082 :   niter->max = widest_int::from (wi::from_mpz (niter_type, tmp, false),
    1675       698082 :                                  TYPE_SIGN (niter_type));
    1676       349041 :   mpz_clear (mstep);
    1677       349041 :   mpz_clear (tmp);
    1678              : 
    1679       349041 :   return true;
    1680              : }
    1681              : 
    1682              : /* Determines number of iterations of loop whose ending condition
    1683              :    is IV0 <= IV1.  TYPE is the type of the iv.  The number of
    1684              :    iterations is stored to NITER.  EXIT_MUST_BE_TAKEN is true if
    1685              :    we know that this condition must eventually become false (we derived this
    1686              :    earlier, and possibly set NITER->assumptions to make sure this
    1687              :    is the case).  BNDS bounds the difference IV1->base - IV0->base.  */
    1688              : 
    1689              : static bool
    1690      2003457 : number_of_iterations_le (class loop *loop, tree type, affine_iv *iv0,
    1691              :                          affine_iv *iv1, class tree_niter_desc *niter,
    1692              :                          bool exit_must_be_taken, bounds *bnds)
    1693              : {
    1694      2003457 :   tree assumption;
    1695      2003457 :   tree type1 = type;
    1696      2003457 :   if (POINTER_TYPE_P (type))
    1697         6250 :     type1 = sizetype;
    1698              : 
    1699              :   /* Say that IV0 is the control variable.  Then IV0 <= IV1 iff
    1700              :      IV0 < IV1 + 1, assuming that IV1 is not equal to the greatest
    1701              :      value of the type.  This we must know anyway, since if it is
    1702              :      equal to this value, the loop rolls forever.  We do not check
    1703              :      this condition for pointer type ivs, as the code cannot rely on
    1704              :      the object to that the pointer points being placed at the end of
    1705              :      the address space (and more pragmatically, TYPE_{MIN,MAX}_VALUE is
    1706              :      not defined for pointers).  */
    1707              : 
    1708      2003457 :   if (!exit_must_be_taken && !POINTER_TYPE_P (type))
    1709              :     {
    1710       625953 :       if (integer_nonzerop (iv0->step))
    1711       561634 :         assumption = fold_build2 (NE_EXPR, boolean_type_node,
    1712              :                                   iv1->base, TYPE_MAX_VALUE (type));
    1713              :       else
    1714        64319 :         assumption = fold_build2 (NE_EXPR, boolean_type_node,
    1715              :                                   iv0->base, TYPE_MIN_VALUE (type));
    1716              : 
    1717       625953 :       if (integer_zerop (assumption))
    1718              :         return false;
    1719       625953 :       if (!integer_nonzerop (assumption))
    1720       247224 :         niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
    1721              :                                           niter->assumptions, assumption);
    1722              :     }
    1723              : 
    1724      2003457 :   if (integer_nonzerop (iv0->step))
    1725              :     {
    1726      1906564 :       if (POINTER_TYPE_P (type))
    1727         1215 :         iv1->base = fold_build_pointer_plus_hwi (iv1->base, 1);
    1728              :       else
    1729      1905349 :         iv1->base = fold_build2 (PLUS_EXPR, type1, iv1->base,
    1730              :                                  build_int_cst (type1, 1));
    1731              :     }
    1732        96893 :   else if (POINTER_TYPE_P (type))
    1733         5035 :     iv0->base = fold_build_pointer_plus_hwi (iv0->base, -1);
    1734              :   else
    1735        91858 :     iv0->base = fold_build2 (MINUS_EXPR, type1,
    1736              :                              iv0->base, build_int_cst (type1, 1));
    1737              : 
    1738      2003457 :   bounds_add (bnds, 1, type1);
    1739              : 
    1740      2003457 :   return number_of_iterations_lt (loop, type, iv0, iv1, niter, exit_must_be_taken,
    1741      2003457 :                                   bnds);
    1742              : }
    1743              : 
    1744              : /* Dumps description of affine induction variable IV to FILE.  */
    1745              : 
    1746              : static void
    1747        86236 : dump_affine_iv (FILE *file, affine_iv *iv)
    1748              : {
    1749        86236 :   if (!integer_zerop (iv->step))
    1750        43118 :     fprintf (file, "[");
    1751              : 
    1752        86236 :   print_generic_expr (file, iv->base, TDF_SLIM);
    1753              : 
    1754        86236 :   if (!integer_zerop (iv->step))
    1755              :     {
    1756        43118 :       fprintf (file, ", + , ");
    1757        43118 :       print_generic_expr (file, iv->step, TDF_SLIM);
    1758        77201 :       fprintf (file, "]%s", iv->no_overflow ? "(no_overflow)" : "");
    1759              :     }
    1760        86236 : }
    1761              : 
    1762              : DEBUG_FUNCTION void
    1763            0 : debug (affine_iv *iv)
    1764              : {
    1765            0 :   dump_affine_iv (stderr, iv);
    1766            0 :   fputc ('\n', stderr);
    1767            0 : }
    1768              : 
    1769              : /* Determine the number of iterations according to condition (for staying
    1770              :    inside loop) which compares two induction variables using comparison
    1771              :    operator CODE.  The induction variable on left side of the comparison
    1772              :    is IV0, the right-hand side is IV1.  Both induction variables must have
    1773              :    type TYPE, which must be an integer or pointer type.  The steps of the
    1774              :    ivs must be constants (or NULL_TREE, which is interpreted as constant zero).
    1775              : 
    1776              :    LOOP is the loop whose number of iterations we are determining.
    1777              : 
    1778              :    ONLY_EXIT is true if we are sure this is the only way the loop could be
    1779              :    exited (including possibly non-returning function calls, exceptions, etc.)
    1780              :    -- in this case we can use the information whether the control induction
    1781              :    variables can overflow or not in a more efficient way.
    1782              : 
    1783              :    if EVERY_ITERATION is true, we know the test is executed on every iteration.
    1784              : 
    1785              :    The results (number of iterations and assumptions as described in
    1786              :    comments at class tree_niter_desc in tree-ssa-loop.h) are stored to NITER.
    1787              :    Returns false if it fails to determine number of iterations, true if it
    1788              :    was determined (possibly with some assumptions).  */
    1789              : 
    1790              : static bool
    1791     12872681 : number_of_iterations_cond (class loop *loop,
    1792              :                            tree type, affine_iv *iv0, enum tree_code code,
    1793              :                            affine_iv *iv1, class tree_niter_desc *niter,
    1794              :                            bool only_exit, bool every_iteration)
    1795              : {
    1796     12872681 :   bool exit_must_be_taken = false, ret;
    1797     12872681 :   bounds bnds;
    1798              : 
    1799              :   /* If the test is not executed every iteration, wrapping may make the test
    1800              :      to pass again.
    1801              :      TODO: the overflow case can be still used as unreliable estimate of upper
    1802              :      bound.  But we have no API to pass it down to number of iterations code
    1803              :      and, at present, it will not use it anyway.  */
    1804     12872681 :   if (!every_iteration
    1805        70285 :       && (!iv0->no_overflow || !iv1->no_overflow
    1806        48865 :           || code == NE_EXPR || code == EQ_EXPR))
    1807              :     return false;
    1808              : 
    1809              :   /* The meaning of these assumptions is this:
    1810              :      if !assumptions
    1811              :        then the rest of information does not have to be valid
    1812              :      if may_be_zero then the loop does not roll, even if
    1813              :        niter != 0.  */
    1814     12823963 :   niter->assumptions = boolean_true_node;
    1815     12823963 :   niter->may_be_zero = boolean_false_node;
    1816     12823963 :   niter->niter = NULL_TREE;
    1817     12823963 :   niter->max = 0;
    1818     12823963 :   niter->bound = NULL_TREE;
    1819     12823963 :   niter->cmp = ERROR_MARK;
    1820              : 
    1821              :   /* Make < comparison from > ones, and for NE_EXPR comparisons, ensure that
    1822              :      the control variable is on lhs.  */
    1823     12823963 :   if (code == GE_EXPR || code == GT_EXPR
    1824     12823963 :       || (code == NE_EXPR && integer_zerop (iv0->step)))
    1825              :     {
    1826      2974434 :       std::swap (iv0, iv1);
    1827      2974434 :       code = swap_tree_comparison (code);
    1828              :     }
    1829              : 
    1830     12823963 :   if (POINTER_TYPE_P (type))
    1831              :     {
    1832              :       /* Comparison of pointers is undefined unless both iv0 and iv1 point
    1833              :          to the same object.  If they do, the control variable cannot wrap
    1834              :          (as wrap around the bounds of memory will never return a pointer
    1835              :          that would be guaranteed to point to the same object, even if we
    1836              :          avoid undefined behavior by casting to size_t and back).  */
    1837       757224 :       iv0->no_overflow = true;
    1838       757224 :       iv1->no_overflow = true;
    1839              :     }
    1840              : 
    1841              :   /* If the control induction variable does not overflow and the only exit
    1842              :      from the loop is the one that we analyze, we know it must be taken
    1843              :      eventually.  */
    1844     12823963 :   if (only_exit)
    1845              :     {
    1846      8395988 :       if (!integer_zerop (iv0->step) && iv0->no_overflow)
    1847              :         exit_must_be_taken = true;
    1848      2105284 :       else if (!integer_zerop (iv1->step) && iv1->no_overflow)
    1849      6445713 :         exit_must_be_taken = true;
    1850              :     }
    1851              : 
    1852              :   /* We can handle cases which neither of the sides of the comparison is
    1853              :      invariant:
    1854              : 
    1855              :        {iv0.base, iv0.step} cmp_code {iv1.base, iv1.step}
    1856              :      as if:
    1857              :        {iv0.base, iv0.step - iv1.step} cmp_code {iv1.base, 0}
    1858              : 
    1859              :      provided that either below condition is satisfied:
    1860              : 
    1861              :        a) the test is NE_EXPR;
    1862              :        b) iv0 and iv1 do not overflow and iv0.step - iv1.step is of
    1863              :           the same sign and of less or equal magnitude than iv0.step
    1864              : 
    1865              :      This rarely occurs in practice, but it is simple enough to manage.  */
    1866     12823963 :   if (!integer_zerop (iv0->step) && !integer_zerop (iv1->step))
    1867              :     {
    1868         5255 :       tree step_type = POINTER_TYPE_P (type) ? sizetype : type;
    1869         5255 :       tree step = fold_binary_to_constant (MINUS_EXPR, step_type,
    1870              :                                            iv0->step, iv1->step);
    1871              : 
    1872              :       /* For code other than NE_EXPR we have to ensure moving the evolution
    1873              :          of IV1 to that of IV0 does not introduce overflow.  */
    1874         5255 :       if (TREE_CODE (step) != INTEGER_CST
    1875         5255 :           || !iv0->no_overflow || !iv1->no_overflow)
    1876              :         {
    1877         1000 :           if (code != NE_EXPR)
    1878              :             return false;
    1879            0 :           iv0->no_overflow = false;
    1880              :         }
    1881              :       /* If the new step of IV0 has changed sign or is of greater
    1882              :          magnitude then we do not know whether IV0 does overflow
    1883              :          and thus the transform is not valid for code other than NE_EXPR.  */
    1884         4255 :       else if (tree_int_cst_sign_bit (step) != tree_int_cst_sign_bit (iv0->step)
    1885         7831 :                || wi::gtu_p (wi::abs (wi::to_widest (step)),
    1886        11407 :                              wi::abs (wi::to_widest (iv0->step))))
    1887              :         {
    1888         2931 :           if (POINTER_TYPE_P (type) && code != NE_EXPR)
    1889              :             /* For relational pointer compares we have further guarantees
    1890              :                that the pointers always point to the same object (or one
    1891              :                after it) and that objects do not cross the zero page.  So
    1892              :                not only is the transform always valid for relational
    1893              :                pointer compares, we also know the resulting IV does not
    1894              :                overflow.  */
    1895              :             ;
    1896          786 :           else if (code != NE_EXPR)
    1897              :             return false;
    1898              :           else
    1899          499 :             iv0->no_overflow = false;
    1900              :         }
    1901              : 
    1902         3469 :       iv0->step = step;
    1903         3469 :       iv1->step = build_int_cst (step_type, 0);
    1904         3469 :       iv1->no_overflow = true;
    1905              :     }
    1906              : 
    1907              :   /* If the result of the comparison is a constant,  the loop is weird.  More
    1908              :      precise handling would be possible, but the situation is not common enough
    1909              :      to waste time on it.  */
    1910     12822177 :   if (integer_zerop (iv0->step) && integer_zerop (iv1->step))
    1911              :     return false;
    1912              : 
    1913              :   /* If the loop exits immediately, there is nothing to do.  */
    1914     12734239 :   tree tem = fold_binary (code, boolean_type_node, iv0->base, iv1->base);
    1915     12734239 :   if (tem && integer_zerop (tem))
    1916              :     {
    1917        83976 :       if (!every_iteration)
    1918              :         return false;
    1919        83913 :       niter->niter = build_int_cst (unsigned_type_for (type), 0);
    1920        83913 :       niter->max = 0;
    1921        83913 :       return true;
    1922              :     }
    1923              : 
    1924              :   /* OK, now we know we have a senseful loop.  Handle several cases, depending
    1925              :      on what comparison operator is used.  */
    1926     12650263 :   bound_difference (loop, iv1->base, iv0->base, &bnds);
    1927              : 
    1928     12650263 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1929              :     {
    1930        43118 :       fprintf (dump_file,
    1931              :                "Analyzing # of iterations of loop %d\n", loop->num);
    1932              : 
    1933        43118 :       fprintf (dump_file, "  exit condition ");
    1934        43118 :       dump_affine_iv (dump_file, iv0);
    1935        51406 :       fprintf (dump_file, " %s ",
    1936              :                code == NE_EXPR ? "!="
    1937         8288 :                : code == LT_EXPR ? "<"
    1938              :                : "<=");
    1939        43118 :       dump_affine_iv (dump_file, iv1);
    1940        43118 :       fprintf (dump_file, "\n");
    1941              : 
    1942        43118 :       fprintf (dump_file, "  bounds on difference of bases: ");
    1943        43118 :       mpz_out_str (dump_file, 10, bnds.below);
    1944        43118 :       fprintf (dump_file, " ... ");
    1945        43118 :       mpz_out_str (dump_file, 10, bnds.up);
    1946        43118 :       fprintf (dump_file, "\n");
    1947              :     }
    1948              : 
    1949     12650263 :   switch (code)
    1950              :     {
    1951      7311886 :     case NE_EXPR:
    1952      7311886 :       gcc_assert (integer_zerop (iv1->step));
    1953      7311886 :       ret = number_of_iterations_ne (loop, type, iv0, iv1->base, niter,
    1954              :                                      exit_must_be_taken, &bnds);
    1955      7311886 :       break;
    1956              : 
    1957      3334920 :     case LT_EXPR:
    1958      3334920 :       ret = number_of_iterations_lt (loop, type, iv0, iv1, niter,
    1959              :                                      exit_must_be_taken, &bnds);
    1960      3334920 :       break;
    1961              : 
    1962      2003457 :     case LE_EXPR:
    1963      2003457 :       ret = number_of_iterations_le (loop, type, iv0, iv1, niter,
    1964              :                                      exit_must_be_taken, &bnds);
    1965      2003457 :       break;
    1966              : 
    1967            0 :     default:
    1968            0 :       gcc_unreachable ();
    1969              :     }
    1970              : 
    1971     12650263 :   mpz_clear (bnds.up);
    1972     12650263 :   mpz_clear (bnds.below);
    1973              : 
    1974     12650263 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1975              :     {
    1976        43118 :       if (ret)
    1977              :         {
    1978        43118 :           fprintf (dump_file, "  result:\n");
    1979        43118 :           if (!integer_nonzerop (niter->assumptions))
    1980              :             {
    1981          222 :               fprintf (dump_file, "    under assumptions ");
    1982          222 :               print_generic_expr (dump_file, niter->assumptions, TDF_SLIM);
    1983          222 :               fprintf (dump_file, "\n");
    1984              :             }
    1985              : 
    1986        43118 :           if (!integer_zerop (niter->may_be_zero))
    1987              :             {
    1988          799 :               fprintf (dump_file, "    zero if ");
    1989          799 :               print_generic_expr (dump_file, niter->may_be_zero, TDF_SLIM);
    1990          799 :               fprintf (dump_file, "\n");
    1991              :             }
    1992              : 
    1993        43118 :           fprintf (dump_file, "    # of iterations ");
    1994        43118 :           print_generic_expr (dump_file, niter->niter, TDF_SLIM);
    1995        43118 :           fprintf (dump_file, ", bounded by ");
    1996        43118 :           print_decu (niter->max, dump_file);
    1997        43118 :           fprintf (dump_file, "\n");
    1998              :         }
    1999              :       else
    2000            0 :         fprintf (dump_file, "  failed\n\n");
    2001              :     }
    2002              :   return ret;
    2003              : }
    2004              : 
    2005              : /* Return an expression that computes the popcount of src.  */
    2006              : 
    2007              : static tree
    2008         2087 : build_popcount_expr (tree src)
    2009              : {
    2010         2087 :   tree fn;
    2011         2087 :   bool use_ifn = false;
    2012         2087 :   int prec = TYPE_PRECISION (TREE_TYPE (src));
    2013         2087 :   int i_prec = TYPE_PRECISION (integer_type_node);
    2014         2087 :   int li_prec = TYPE_PRECISION (long_integer_type_node);
    2015         2087 :   int lli_prec = TYPE_PRECISION (long_long_integer_type_node);
    2016              : 
    2017         2087 :   tree utype = unsigned_type_for (TREE_TYPE (src));
    2018         2087 :   src = fold_convert (utype, src);
    2019              : 
    2020         2087 :   if (direct_internal_fn_supported_p (IFN_POPCOUNT, utype, OPTIMIZE_FOR_BOTH))
    2021              :     use_ifn = true;
    2022         2036 :   else if (prec <= i_prec)
    2023         1960 :     fn = builtin_decl_implicit (BUILT_IN_POPCOUNT);
    2024           76 :   else if (prec == li_prec)
    2025           59 :     fn = builtin_decl_implicit (BUILT_IN_POPCOUNTL);
    2026           17 :   else if (prec == lli_prec || prec == 2 * lli_prec)
    2027           17 :     fn = builtin_decl_implicit (BUILT_IN_POPCOUNTLL);
    2028              :   else
    2029              :     return NULL_TREE;
    2030              : 
    2031           51 :   tree call;
    2032           51 :   if (use_ifn)
    2033           51 :       call = build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_POPCOUNT,
    2034              :                                            integer_type_node, 1, src);
    2035         2036 :   else if (prec == 2 * lli_prec)
    2036              :     {
    2037           17 :       tree src1 = fold_convert (long_long_unsigned_type_node,
    2038              :                                 fold_build2 (RSHIFT_EXPR, TREE_TYPE (src),
    2039              :                                              unshare_expr (src),
    2040              :                                              build_int_cst (integer_type_node,
    2041              :                                                             lli_prec)));
    2042           17 :       tree src2 = fold_convert (long_long_unsigned_type_node, src);
    2043           17 :       tree call1 = build_call_expr (fn, 1, src1);
    2044           17 :       tree call2 = build_call_expr (fn, 1, src2);
    2045           17 :       call = fold_build2 (PLUS_EXPR, integer_type_node, call1, call2);
    2046              :     }
    2047              :   else
    2048              :     {
    2049         2019 :       if (prec < i_prec)
    2050          291 :         src = fold_convert (unsigned_type_node, src);
    2051              : 
    2052         2019 :       call = build_call_expr (fn, 1, src);
    2053              :     }
    2054              : 
    2055              :   return call;
    2056              : }
    2057              : 
    2058              : /* Utility function to check if OP is defined by a stmt
    2059              :    that is a val - 1.  */
    2060              : 
    2061              : static bool
    2062       132097 : ssa_defined_by_minus_one_stmt_p (tree op, tree val)
    2063              : {
    2064       132097 :   gimple *stmt;
    2065       132097 :   return (TREE_CODE (op) == SSA_NAME
    2066        84585 :           && (stmt = SSA_NAME_DEF_STMT (op))
    2067        84585 :           && is_gimple_assign (stmt)
    2068        72700 :           && (gimple_assign_rhs_code (stmt) == PLUS_EXPR)
    2069         9152 :           && val == gimple_assign_rhs1 (stmt)
    2070       134434 :           && integer_minus_onep (gimple_assign_rhs2 (stmt)));
    2071              : }
    2072              : 
    2073              : /* See comment below for number_of_iterations_bitcount.
    2074              :    For popcount, we have:
    2075              : 
    2076              :    modify:
    2077              :    _1 = iv_1 + -1
    2078              :    iv_2 = iv_1 & _1
    2079              : 
    2080              :    test:
    2081              :    if (iv != 0)
    2082              : 
    2083              :    modification count:
    2084              :    popcount (src)
    2085              : 
    2086              :  */
    2087              : 
    2088              : static bool
    2089      3721031 : number_of_iterations_popcount (loop_p loop, edge exit,
    2090              :                                enum tree_code code,
    2091              :                                class tree_niter_desc *niter)
    2092              : {
    2093      3721031 :   bool modify_before_test = true;
    2094      3721031 :   HOST_WIDE_INT max;
    2095              : 
    2096              :   /* Check that condition for staying inside the loop is like
    2097              :      if (iv != 0).  */
    2098      7442062 :   gcond *cond_stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (exit->src));
    2099      3721031 :   if (!cond_stmt
    2100      3721031 :       || code != NE_EXPR
    2101      1850932 :       || !integer_zerop (gimple_cond_rhs (cond_stmt))
    2102      5064217 :       || TREE_CODE (gimple_cond_lhs (cond_stmt)) != SSA_NAME)
    2103      2377845 :     return false;
    2104              : 
    2105      1343186 :   tree iv_2 = gimple_cond_lhs (cond_stmt);
    2106      1343186 :   gimple *iv_2_stmt = SSA_NAME_DEF_STMT (iv_2);
    2107              : 
    2108              :   /* If the test comes before the iv modification, then these will actually be
    2109              :      iv_1 and a phi node.  */
    2110      1343186 :   if (gimple_code (iv_2_stmt) == GIMPLE_PHI
    2111       388819 :       && gimple_bb (iv_2_stmt) == loop->header
    2112       282101 :       && gimple_phi_num_args (iv_2_stmt) == 2
    2113      1625287 :       && (TREE_CODE (gimple_phi_arg_def (iv_2_stmt,
    2114              :                                          loop_latch_edge (loop)->dest_idx))
    2115              :           == SSA_NAME))
    2116              :     {
    2117              :       /* iv_2 is actually one of the inputs to the phi.  */
    2118       263513 :       iv_2 = gimple_phi_arg_def (iv_2_stmt, loop_latch_edge (loop)->dest_idx);
    2119       263513 :       iv_2_stmt = SSA_NAME_DEF_STMT (iv_2);
    2120       263513 :       modify_before_test = false;
    2121              :     }
    2122              : 
    2123              :   /* Make sure iv_2_stmt is an and stmt (iv_2 = _1 & iv_1).  */
    2124      1343186 :   if (!is_gimple_assign (iv_2_stmt)
    2125      1343186 :       || gimple_assign_rhs_code (iv_2_stmt) != BIT_AND_EXPR)
    2126              :     return false;
    2127              : 
    2128        66736 :   tree iv_1 = gimple_assign_rhs1 (iv_2_stmt);
    2129        66736 :   tree _1 = gimple_assign_rhs2 (iv_2_stmt);
    2130              : 
    2131              :   /* Check that _1 is defined by (_1 = iv_1 + -1).
    2132              :      Also make sure that _1 is the same in and_stmt and _1 defining stmt.
    2133              :      Also canonicalize if _1 and _b11 are reversed.  */
    2134        66736 :   if (ssa_defined_by_minus_one_stmt_p (iv_1, _1))
    2135              :     std::swap (iv_1, _1);
    2136        65361 :   else if (ssa_defined_by_minus_one_stmt_p (_1, iv_1))
    2137              :     ;
    2138              :   else
    2139              :     return false;
    2140              : 
    2141              :   /* Check the recurrence.  */
    2142         2106 :   gimple *phi = SSA_NAME_DEF_STMT (iv_1);
    2143         2106 :   if (gimple_code (phi) != GIMPLE_PHI
    2144         2106 :       || (gimple_bb (phi) != loop_latch_edge (loop)->dest)
    2145         4193 :       || (iv_2 != gimple_phi_arg_def (phi, loop_latch_edge (loop)->dest_idx)))
    2146           19 :     return false;
    2147              : 
    2148              :   /* We found a match.  */
    2149         2087 :   tree src = gimple_phi_arg_def (phi, loop_preheader_edge (loop)->dest_idx);
    2150         2087 :   int src_precision = TYPE_PRECISION (TREE_TYPE (src));
    2151              : 
    2152              :   /* Get the corresponding popcount builtin.  */
    2153         2087 :   tree expr = build_popcount_expr (src);
    2154              : 
    2155         2087 :   if (!expr)
    2156              :     return false;
    2157              : 
    2158         2087 :   max = src_precision;
    2159              : 
    2160         2087 :   tree may_be_zero = boolean_false_node;
    2161              : 
    2162         2087 :   if (modify_before_test)
    2163              :     {
    2164          984 :       expr = fold_build2 (MINUS_EXPR, integer_type_node, expr,
    2165              :                           integer_one_node);
    2166          984 :       max = max - 1;
    2167          984 :       may_be_zero = fold_build2 (EQ_EXPR, boolean_type_node, src,
    2168              :                                       build_zero_cst (TREE_TYPE (src)));
    2169              :     }
    2170              : 
    2171         2087 :   expr = fold_convert (unsigned_type_node, expr);
    2172              : 
    2173         2087 :   niter->assumptions = boolean_true_node;
    2174         2087 :   niter->may_be_zero = simplify_using_initial_conditions (loop, may_be_zero);
    2175         2087 :   niter->niter = simplify_using_initial_conditions(loop, expr);
    2176              : 
    2177         2087 :   if (TREE_CODE (niter->niter) == INTEGER_CST)
    2178          195 :     niter->max = tree_to_uhwi (niter->niter);
    2179              :   else
    2180         1892 :     niter->max = max;
    2181              : 
    2182         2087 :   niter->bound = NULL_TREE;
    2183         2087 :   niter->cmp = ERROR_MARK;
    2184         2087 :   return true;
    2185              : }
    2186              : 
    2187              : /* Return an expression that counts the leading/trailing zeroes of src.
    2188              : 
    2189              :    If define_at_zero is true, then the built expression will be defined to
    2190              :    return the precision of src when src == 0 (using either a conditional
    2191              :    expression or a suitable internal function).
    2192              :    Otherwise, we can elide the conditional expression and let src = 0 invoke
    2193              :    undefined behaviour.  */
    2194              : 
    2195              : static tree
    2196         8744 : build_cltz_expr (tree src, bool leading, bool define_at_zero)
    2197              : {
    2198         8744 :   tree fn;
    2199         8744 :   internal_fn ifn = leading ? IFN_CLZ : IFN_CTZ;
    2200         8744 :   bool use_ifn = false;
    2201         8744 :   int prec = TYPE_PRECISION (TREE_TYPE (src));
    2202         8744 :   int i_prec = TYPE_PRECISION (integer_type_node);
    2203         8744 :   int li_prec = TYPE_PRECISION (long_integer_type_node);
    2204         8744 :   int lli_prec = TYPE_PRECISION (long_long_integer_type_node);
    2205              : 
    2206         8744 :   tree utype = unsigned_type_for (TREE_TYPE (src));
    2207         8744 :   src = fold_convert (utype, src);
    2208              : 
    2209         8744 :   if (direct_internal_fn_supported_p (ifn, utype, OPTIMIZE_FOR_BOTH))
    2210              :     use_ifn = true;
    2211         3439 :   else if (prec <= i_prec)
    2212         1650 :     fn = leading ? builtin_decl_implicit (BUILT_IN_CLZ)
    2213         3439 :                  : builtin_decl_implicit (BUILT_IN_CTZ);
    2214         1789 :   else if (prec == li_prec)
    2215            0 :     fn = leading ? builtin_decl_implicit (BUILT_IN_CLZL)
    2216         3439 :                  : builtin_decl_implicit (BUILT_IN_CTZL);
    2217         1789 :   else if (prec == lli_prec || prec == 2 * lli_prec)
    2218         1789 :     fn = leading ? builtin_decl_implicit (BUILT_IN_CLZLL)
    2219         3439 :                  : builtin_decl_implicit (BUILT_IN_CTZLL);
    2220              :   else
    2221              :     return NULL_TREE;
    2222              : 
    2223         5305 :   tree call;
    2224         5305 :   if (use_ifn)
    2225              :     {
    2226         5305 :       int val;
    2227         5305 :       int optab_defined_at_zero
    2228              :         = (leading
    2229         5305 :            ? CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (utype), val)
    2230         6176 :            : CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (utype), val));
    2231         5305 :       tree arg2 = NULL_TREE;
    2232         5305 :       if (define_at_zero && optab_defined_at_zero == 2 && val == prec)
    2233            0 :         arg2 = build_int_cst (integer_type_node, val);
    2234         5305 :       call = build_call_expr_internal_loc (UNKNOWN_LOCATION, ifn,
    2235              :                                            integer_type_node, arg2 ? 2 : 1,
    2236              :                                            src, arg2);
    2237         5305 :       if (define_at_zero && arg2 == NULL_TREE)
    2238              :         {
    2239         4459 :           tree is_zero = fold_build2 (NE_EXPR, boolean_type_node, src,
    2240              :                                       build_zero_cst (TREE_TYPE (src)));
    2241         4459 :           call = fold_build3 (COND_EXPR, integer_type_node, is_zero, call,
    2242              :                               build_int_cst (integer_type_node, prec));
    2243              :         }
    2244              :     }
    2245         3439 :   else if (fn == NULL_TREE)
    2246              :     return NULL_TREE;
    2247         3439 :   else if (prec == 2 * lli_prec)
    2248              :     {
    2249          854 :       tree src1 = fold_convert (long_long_unsigned_type_node,
    2250              :                                 fold_build2 (RSHIFT_EXPR, TREE_TYPE (src),
    2251              :                                              unshare_expr (src),
    2252              :                                              build_int_cst (integer_type_node,
    2253              :                                                             lli_prec)));
    2254          854 :       tree src2 = fold_convert (long_long_unsigned_type_node, src);
    2255              :       /* We count the zeroes in src1, and add the number in src2 when src1
    2256              :          is 0.  */
    2257          854 :       if (!leading)
    2258           10 :         std::swap (src1, src2);
    2259          854 :       tree call1 = build_call_expr (fn, 1, src1);
    2260          854 :       tree call2 = build_call_expr (fn, 1, src2);
    2261          854 :       if (define_at_zero)
    2262              :         {
    2263          728 :           tree is_zero2 = fold_build2 (NE_EXPR, boolean_type_node, src2,
    2264              :                                        build_zero_cst (TREE_TYPE (src2)));
    2265          728 :           call2 = fold_build3 (COND_EXPR, integer_type_node, is_zero2, call2,
    2266              :                                build_int_cst (integer_type_node, lli_prec));
    2267              :         }
    2268          854 :       tree is_zero1 = fold_build2 (NE_EXPR, boolean_type_node, src1,
    2269              :                                    build_zero_cst (TREE_TYPE (src1)));
    2270          854 :       call = fold_build3 (COND_EXPR, integer_type_node, is_zero1, call1,
    2271              :                           fold_build2 (PLUS_EXPR, integer_type_node, call2,
    2272              :                                        build_int_cst (integer_type_node,
    2273              :                                                       lli_prec)));
    2274              :     }
    2275              :   else
    2276              :     {
    2277         2585 :       if (prec < i_prec)
    2278         1650 :         src = fold_convert (unsigned_type_node, src);
    2279              : 
    2280         2585 :       call = build_call_expr (fn, 1, src);
    2281         2585 :       if (leading && prec < i_prec)
    2282         1547 :         call = fold_build2 (MINUS_EXPR, integer_type_node, call,
    2283              :                             build_int_cst (integer_type_node, i_prec - prec));
    2284         2585 :       if (define_at_zero)
    2285              :         {
    2286         2388 :           tree is_zero = fold_build2 (NE_EXPR, boolean_type_node, src,
    2287              :                                       build_zero_cst (TREE_TYPE (src)));
    2288         2388 :           call = fold_build3 (COND_EXPR, integer_type_node, is_zero, call,
    2289              :                               build_int_cst (integer_type_node, prec));
    2290              :         }
    2291              :     }
    2292              : 
    2293              :   return call;
    2294              : }
    2295              : 
    2296              : /* Returns true if STMT is equivalent to x << 1.  */
    2297              : 
    2298              : static bool
    2299      1114308 : is_lshift_by_1 (gassign *stmt)
    2300              : {
    2301      1114308 :   if (gimple_assign_rhs_code (stmt) == LSHIFT_EXPR
    2302      1115594 :       && integer_onep (gimple_assign_rhs2 (stmt)))
    2303              :     return true;
    2304      1113569 :   if (gimple_assign_rhs_code (stmt) == MULT_EXPR
    2305         1300 :       && tree_fits_shwi_p (gimple_assign_rhs2 (stmt))
    2306      1114550 :       && tree_to_shwi (gimple_assign_rhs2 (stmt)) == 2)
    2307              :     return true;
    2308              :   return false;
    2309              : }
    2310              : 
    2311              : /* Returns true if STMT is equivalent to x >> 1.  */
    2312              : 
    2313              : static bool
    2314      1113336 : is_rshift_by_1 (gassign *stmt)
    2315              : {
    2316      1113336 :   if (!TYPE_UNSIGNED (TREE_TYPE (gimple_assign_lhs (stmt))))
    2317              :     return false;
    2318       808447 :   if (gimple_assign_rhs_code (stmt) == RSHIFT_EXPR
    2319       827929 :       && integer_onep (gimple_assign_rhs2 (stmt)))
    2320              :     return true;
    2321      1506721 :   if (trunc_or_exact_div_p (gimple_assign_rhs_code (stmt))
    2322          575 :       && tree_fits_shwi_p (gimple_assign_rhs2 (stmt))
    2323       799107 :       && tree_to_shwi (gimple_assign_rhs2 (stmt)) == 2)
    2324              :     return true;
    2325              :   return false;
    2326              : }
    2327              : 
    2328              : /* Helper for number_of_iterations_cltz that uses ranger to determine
    2329              :    if SRC's range, shifted left (when LEFT_SHIFT is true) or right
    2330              :    by NUM_IGNORED_BITS, is guaranteed to be != 0 on LOOP's preheader
    2331              :    edge.
    2332              :    Return true if so or false otherwise.  */
    2333              : 
    2334              : static bool
    2335         1169 : shifted_range_nonzero_p (loop_p loop, tree src,
    2336              :                          bool left_shift, int num_ignored_bits)
    2337              : {
    2338         1169 :   int_range_max r (TREE_TYPE (src));
    2339         1169 :   gcc_assert (num_ignored_bits >= 0);
    2340              : 
    2341         1169 :   if (get_range_query (cfun)->range_on_edge
    2342         1169 :       (r, loop_preheader_edge (loop), src)
    2343         1169 :       && !r.varying_p ()
    2344         1718 :       && !r.undefined_p ())
    2345              :     {
    2346          548 :       if (num_ignored_bits)
    2347              :         {
    2348          572 :           range_op_handler op (left_shift ? LSHIFT_EXPR : RSHIFT_EXPR);
    2349          384 :           int_range_max shifted_range (TREE_TYPE (src));
    2350          384 :           wide_int shift_count = wi::shwi (num_ignored_bits,
    2351          384 :                                            TYPE_PRECISION (TREE_TYPE
    2352          384 :                                                            (src)));
    2353          384 :           int_range_max shift_amount
    2354          384 :             (TREE_TYPE (src), shift_count, shift_count);
    2355              : 
    2356          384 :           if (op.fold_range (shifted_range, TREE_TYPE (src), r,
    2357              :                              shift_amount))
    2358          384 :             r = shifted_range;
    2359          384 :         }
    2360              : 
    2361              :       /* If the range does not contain zero we are good.  */
    2362          548 :       if (!range_includes_zero_p (r))
    2363              :         return true;
    2364              :     }
    2365              : 
    2366              :   return false;
    2367         1169 : }
    2368              : 
    2369              : 
    2370              : /* See comment below for number_of_iterations_bitcount.
    2371              :    For c[lt]z, we have:
    2372              : 
    2373              :    modify:
    2374              :    iv_2 = iv_1 << 1 OR iv_1 >> 1
    2375              : 
    2376              :    test:
    2377              :    if (iv & 1 << (prec-1)) OR (iv & 1)
    2378              : 
    2379              :    modification count:
    2380              :    src precision - c[lt]z (src)
    2381              : 
    2382              :  */
    2383              : 
    2384              : static bool
    2385      6827499 : number_of_iterations_cltz (loop_p loop, edge exit,
    2386              :                                enum tree_code code,
    2387              :                                class tree_niter_desc *niter)
    2388              : {
    2389      6827499 :   bool modify_before_test = true;
    2390      6827499 :   HOST_WIDE_INT max;
    2391      6827499 :   int checked_bit;
    2392      6827499 :   tree iv_2;
    2393              : 
    2394              :   /* Check that condition for staying inside the loop is like
    2395              :      if (iv == 0).  */
    2396     13654998 :   gcond *cond_stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (exit->src));
    2397      6827499 :   if (!cond_stmt
    2398      6827499 :       || (code != EQ_EXPR && code != GE_EXPR)
    2399      3404557 :       || !integer_zerop (gimple_cond_rhs (cond_stmt))
    2400      1314710 :       || TREE_CODE (gimple_cond_lhs (cond_stmt)) != SSA_NAME)
    2401      5512978 :     return false;
    2402              : 
    2403      1314521 :   if (code == EQ_EXPR)
    2404              :     {
    2405              :       /* Make sure we check a bitwise and with a suitable constant */
    2406      1191061 :       gimple *and_stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (cond_stmt));
    2407      1191061 :       if (!is_gimple_assign (and_stmt)
    2408       716055 :           || gimple_assign_rhs_code (and_stmt) != BIT_AND_EXPR
    2409       131953 :           || !integer_pow2p (gimple_assign_rhs2 (and_stmt))
    2410      1209162 :           || TREE_CODE (gimple_assign_rhs1 (and_stmt)) != SSA_NAME)
    2411      1172960 :         return false;
    2412              : 
    2413        18101 :       checked_bit = tree_log2 (gimple_assign_rhs2 (and_stmt));
    2414              : 
    2415        18101 :       iv_2 = gimple_assign_rhs1 (and_stmt);
    2416              :     }
    2417              :   else
    2418              :     {
    2419              :       /* We have a GE_EXPR - a signed comparison with zero is equivalent to
    2420              :          testing the leading bit, so check for this pattern too.  */
    2421              : 
    2422       123460 :       iv_2 = gimple_cond_lhs (cond_stmt);
    2423       123460 :       tree test_value_type = TREE_TYPE (iv_2);
    2424              : 
    2425       123460 :       if (TYPE_UNSIGNED (test_value_type))
    2426              :         return false;
    2427              : 
    2428       123460 :       gimple *test_value_stmt = SSA_NAME_DEF_STMT (iv_2);
    2429              : 
    2430       123460 :       if (is_gimple_assign (test_value_stmt)
    2431       123460 :           && gimple_assign_rhs_code (test_value_stmt) == NOP_EXPR)
    2432              :         {
    2433              :           /* If the test value comes from a NOP_EXPR, then we need to unwrap
    2434              :              this.  We conservatively require that both types have the same
    2435              :              precision.  */
    2436         5194 :           iv_2 = gimple_assign_rhs1 (test_value_stmt);
    2437         5194 :           tree rhs_type = TREE_TYPE (iv_2);
    2438         5194 :           if (TREE_CODE (iv_2) != SSA_NAME
    2439         5194 :               || !INTEGRAL_NB_TYPE_P (rhs_type)
    2440        10377 :               || (TYPE_PRECISION (rhs_type)
    2441         5183 :                   != TYPE_PRECISION (test_value_type)))
    2442              :             return false;
    2443              :         }
    2444              : 
    2445       123060 :       checked_bit = TYPE_PRECISION (test_value_type) - 1;
    2446              :     }
    2447              : 
    2448       141161 :   gimple *iv_2_stmt = SSA_NAME_DEF_STMT (iv_2);
    2449              : 
    2450              :   /* If the test comes before the iv modification, then these will actually be
    2451              :      iv_1 and a phi node.  */
    2452       141161 :   if (gimple_code (iv_2_stmt) == GIMPLE_PHI
    2453        26274 :       && gimple_bb (iv_2_stmt) == loop->header
    2454        15173 :       && gimple_phi_num_args (iv_2_stmt) == 2
    2455       156334 :       && (TREE_CODE (gimple_phi_arg_def (iv_2_stmt,
    2456              :                                          loop_latch_edge (loop)->dest_idx))
    2457              :           == SSA_NAME))
    2458              :     {
    2459              :       /* iv_2 is actually one of the inputs to the phi.  */
    2460        14979 :       iv_2 = gimple_phi_arg_def (iv_2_stmt, loop_latch_edge (loop)->dest_idx);
    2461        14979 :       iv_2_stmt = SSA_NAME_DEF_STMT (iv_2);
    2462        14979 :       modify_before_test = false;
    2463              :     }
    2464              : 
    2465              :   /* Make sure iv_2_stmt is a logical shift by one stmt:
    2466              :      iv_2 = iv_1 {<<|>>} 1  */
    2467       141161 :   if (!is_gimple_assign (iv_2_stmt))
    2468              :     return false;
    2469       111123 :   bool left_shift = false;
    2470       221738 :   if (!((left_shift = is_lshift_by_1 (as_a <gassign *> (iv_2_stmt)))
    2471       110615 :         || is_rshift_by_1 (as_a <gassign *> (iv_2_stmt))))
    2472              :     return false;
    2473              : 
    2474         1212 :   tree iv_1 = gimple_assign_rhs1 (iv_2_stmt);
    2475              : 
    2476              :   /* Check the recurrence.  */
    2477         1212 :   gimple *phi = SSA_NAME_DEF_STMT (iv_1);
    2478         1212 :   if (gimple_code (phi) != GIMPLE_PHI
    2479         1175 :       || (gimple_bb (phi) != loop_latch_edge (loop)->dest)
    2480         2381 :       || (iv_2 != gimple_phi_arg_def (phi, loop_latch_edge (loop)->dest_idx)))
    2481           43 :     return false;
    2482              : 
    2483              :   /* We found a match.  */
    2484         1169 :   tree src = gimple_phi_arg_def (phi, loop_preheader_edge (loop)->dest_idx);
    2485         1169 :   int src_precision = TYPE_PRECISION (TREE_TYPE (src));
    2486              : 
    2487              :   /* Save the original SSA name before preprocessing for ranger queries.  */
    2488         1169 :   tree unshifted_src = src;
    2489              : 
    2490              :   /* Apply any needed preprocessing to src.  */
    2491         1169 :   int num_ignored_bits;
    2492         1169 :   if (left_shift)
    2493          465 :     num_ignored_bits = src_precision - checked_bit - 1;
    2494              :   else
    2495              :     num_ignored_bits = checked_bit;
    2496              : 
    2497         1169 :   if (modify_before_test)
    2498          506 :     num_ignored_bits++;
    2499              : 
    2500         1169 :   if (num_ignored_bits != 0)
    2501         1102 :     src = fold_build2 (left_shift ? LSHIFT_EXPR : RSHIFT_EXPR,
    2502              :                        TREE_TYPE (src), src,
    2503              :                        build_int_cst (integer_type_node, num_ignored_bits));
    2504              : 
    2505              :   /* Get the corresponding c[lt]z builtin.  */
    2506         1169 :   tree expr = build_cltz_expr (src, left_shift, false);
    2507              : 
    2508         1169 :   if (!expr)
    2509              :     return false;
    2510              : 
    2511         1169 :   max = src_precision - num_ignored_bits - 1;
    2512              : 
    2513         1169 :   expr = fold_convert (unsigned_type_node, expr);
    2514              : 
    2515              :   /* If the copy-header (ch) pass peeled one iteration we're shifting
    2516              :      SRC by preprocessing it above.
    2517              : 
    2518              :      A loop like
    2519              :       if (bits)
    2520              :         {
    2521              :           while (!(bits & 1))
    2522              :             {
    2523              :               bits >>= 1;
    2524              :               cnt += 1;
    2525              :             }
    2526              :           return cnt;
    2527              :         }
    2528              :      ch (roughly) transforms into:
    2529              :       if (bits)
    2530              :         {
    2531              :           if (!(bits & 1)
    2532              :             {
    2533              :               do
    2534              :                 {
    2535              :                   bits >>= 1;
    2536              :                   cnt += 1;
    2537              :                 } while (!(bits & 1));
    2538              :             }
    2539              :            else
    2540              :              cnt = 1;
    2541              :           return cnt;
    2542              :         }
    2543              : 
    2544              :      Then, our preprocessed SRC (that is used for c[tl]z computation)
    2545              :      will be bits >> 1, and the assumption is bits >> 1 != 0.  */
    2546              : 
    2547         1169 :   tree assumptions;
    2548         1169 :   if (shifted_range_nonzero_p (loop, unshifted_src,
    2549              :                                left_shift, num_ignored_bits))
    2550          257 :     assumptions = boolean_true_node;
    2551              :   else
    2552              :     {
    2553              :       /* If ranger couldn't prove the assumption, try
    2554              :          simplify_using_initial_conditions.  */
    2555          912 :       assumptions = fold_build2 (NE_EXPR, boolean_type_node, src,
    2556              :                                  build_zero_cst (TREE_TYPE (src)));
    2557          912 :       assumptions = simplify_using_initial_conditions (loop, assumptions);
    2558              :     }
    2559              : 
    2560         1169 :   niter->assumptions = assumptions;
    2561         1169 :   niter->may_be_zero = boolean_false_node;
    2562         1169 :   niter->niter = simplify_using_initial_conditions (loop, expr);
    2563              : 
    2564         1169 :   if (TREE_CODE (niter->niter) == INTEGER_CST)
    2565            0 :     niter->max = tree_to_uhwi (niter->niter);
    2566              :   else
    2567         1169 :     niter->max = max;
    2568              : 
    2569         1169 :   niter->bound = NULL_TREE;
    2570         1169 :   niter->cmp = ERROR_MARK;
    2571              : 
    2572         1169 :   return true;
    2573              : }
    2574              : 
    2575              : /* See comment below for number_of_iterations_bitcount.
    2576              :    For c[lt]z complement, we have:
    2577              : 
    2578              :    modify:
    2579              :    iv_2 = iv_1 >> 1 OR iv_1 << 1
    2580              : 
    2581              :    test:
    2582              :    if (iv != 0)
    2583              : 
    2584              :    modification count:
    2585              :    src precision - c[lt]z (src)
    2586              : 
    2587              :  */
    2588              : 
    2589              : static bool
    2590      3718775 : number_of_iterations_cltz_complement (loop_p loop, edge exit,
    2591              :                                enum tree_code code,
    2592              :                                class tree_niter_desc *niter)
    2593              : {
    2594      3718775 :   bool modify_before_test = true;
    2595      3718775 :   HOST_WIDE_INT max;
    2596              : 
    2597              :   /* Check that condition for staying inside the loop is like
    2598              :      if (iv != 0).  */
    2599      7437550 :   gcond *cond_stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (exit->src));
    2600      3718775 :   if (!cond_stmt
    2601      3718775 :       || code != NE_EXPR
    2602      1848845 :       || !integer_zerop (gimple_cond_rhs (cond_stmt))
    2603      5059874 :       || TREE_CODE (gimple_cond_lhs (cond_stmt)) != SSA_NAME)
    2604      2377676 :     return false;
    2605              : 
    2606      1341099 :   tree iv_2 = gimple_cond_lhs (cond_stmt);
    2607      1341099 :   gimple *iv_2_stmt = SSA_NAME_DEF_STMT (iv_2);
    2608              : 
    2609              :   /* If the test comes before the iv modification, then these will actually be
    2610              :      iv_1 and a phi node.  */
    2611      1341099 :   if (gimple_code (iv_2_stmt) == GIMPLE_PHI
    2612       387716 :       && gimple_bb (iv_2_stmt) == loop->header
    2613       280998 :       && gimple_phi_num_args (iv_2_stmt) == 2
    2614      1622097 :       && (TREE_CODE (gimple_phi_arg_def (iv_2_stmt,
    2615              :                                          loop_latch_edge (loop)->dest_idx))
    2616              :           == SSA_NAME))
    2617              :     {
    2618              :       /* iv_2 is actually one of the inputs to the phi.  */
    2619       262410 :       iv_2 = gimple_phi_arg_def (iv_2_stmt, loop_latch_edge (loop)->dest_idx);
    2620       262410 :       iv_2_stmt = SSA_NAME_DEF_STMT (iv_2);
    2621       262410 :       modify_before_test = false;
    2622              :     }
    2623              : 
    2624              :   /* Make sure iv_2_stmt is a logical shift by one stmt:
    2625              :      iv_2 = iv_1 {>>|<<} 1  */
    2626      1341099 :   if (!is_gimple_assign (iv_2_stmt))
    2627              :     return false;
    2628      1003185 :   bool left_shift = false;
    2629      2005906 :   if (!((left_shift = is_lshift_by_1 (as_a <gassign *> (iv_2_stmt)))
    2630      1002721 :         || is_rshift_by_1 (as_a <gassign *> (iv_2_stmt))))
    2631              :     return false;
    2632              : 
    2633         9616 :   tree iv_1 = gimple_assign_rhs1 (iv_2_stmt);
    2634              : 
    2635              :   /* Check the recurrence.  */
    2636         9616 :   gimple *phi = SSA_NAME_DEF_STMT (iv_1);
    2637         9616 :   if (gimple_code (phi) != GIMPLE_PHI
    2638         8961 :       || (gimple_bb (phi) != loop_latch_edge (loop)->dest)
    2639        17409 :       || (iv_2 != gimple_phi_arg_def (phi, loop_latch_edge (loop)->dest_idx)))
    2640         2041 :     return false;
    2641              : 
    2642              :   /* We found a match.  */
    2643         7575 :   tree src = gimple_phi_arg_def (phi, loop_preheader_edge (loop)->dest_idx);
    2644         7575 :   int src_precision = TYPE_PRECISION (TREE_TYPE (src));
    2645              : 
    2646              :   /* Get the corresponding c[lt]z builtin.  */
    2647         7575 :   tree expr = build_cltz_expr (src, !left_shift, true);
    2648              : 
    2649         7575 :   if (!expr)
    2650              :     return false;
    2651              : 
    2652         7575 :   expr = fold_build2 (MINUS_EXPR, integer_type_node,
    2653              :                       build_int_cst (integer_type_node, src_precision),
    2654              :                       expr);
    2655              : 
    2656         7575 :   max = src_precision;
    2657              : 
    2658         7575 :   tree may_be_zero = boolean_false_node;
    2659              : 
    2660         7575 :   if (modify_before_test)
    2661              :     {
    2662         6315 :       expr = fold_build2 (MINUS_EXPR, integer_type_node, expr,
    2663              :                           integer_one_node);
    2664         6315 :       max = max - 1;
    2665         6315 :       may_be_zero = fold_build2 (EQ_EXPR, boolean_type_node, src,
    2666              :                                       build_zero_cst (TREE_TYPE (src)));
    2667              :     }
    2668              : 
    2669         7575 :   expr = fold_convert (unsigned_type_node, expr);
    2670              : 
    2671         7575 :   niter->assumptions = boolean_true_node;
    2672         7575 :   niter->may_be_zero = simplify_using_initial_conditions (loop, may_be_zero);
    2673         7575 :   niter->niter = simplify_using_initial_conditions (loop, expr);
    2674              : 
    2675         7575 :   if (TREE_CODE (niter->niter) == INTEGER_CST)
    2676           69 :     niter->max = tree_to_uhwi (niter->niter);
    2677              :   else
    2678         7506 :     niter->max = max;
    2679              : 
    2680         7575 :   niter->bound = NULL_TREE;
    2681         7575 :   niter->cmp = ERROR_MARK;
    2682         7575 :   return true;
    2683              : }
    2684              : 
    2685              : /* See if LOOP contains a bit counting idiom. The idiom consists of two parts:
    2686              :    1. A modification to the induction variabler;.
    2687              :    2. A test to determine whether or not to exit the loop.
    2688              : 
    2689              :    These can come in either order - i.e.:
    2690              : 
    2691              :    <bb 3>
    2692              :    iv_1 = PHI <src(2), iv_2(4)>
    2693              :    if (test (iv_1))
    2694              :      goto <bb 4>
    2695              :    else
    2696              :      goto <bb 5>
    2697              : 
    2698              :    <bb 4>
    2699              :    iv_2 = modify (iv_1)
    2700              :    goto <bb 3>
    2701              : 
    2702              :    OR
    2703              : 
    2704              :    <bb 3>
    2705              :    iv_1 = PHI <src(2), iv_2(4)>
    2706              :    iv_2 = modify (iv_1)
    2707              : 
    2708              :    <bb 4>
    2709              :    if (test (iv_2))
    2710              :      goto <bb 3>
    2711              :    else
    2712              :      goto <bb 5>
    2713              : 
    2714              :    The second form can be generated by copying the loop header out of the loop.
    2715              : 
    2716              :    In the first case, the number of latch executions will be equal to the
    2717              :    number of induction variable modifications required before the test fails.
    2718              : 
    2719              :    In the second case (modify_before_test), if we assume that the number of
    2720              :    modifications required before the test fails is nonzero, then the number of
    2721              :    latch executions will be one less than this number.
    2722              : 
    2723              :    If we recognise the pattern, then we update niter accordingly, and return
    2724              :    true.  */
    2725              : 
    2726              : static bool
    2727      3721031 : number_of_iterations_bitcount (loop_p loop, edge exit,
    2728              :                                enum tree_code code,
    2729              :                                class tree_niter_desc *niter)
    2730              : {
    2731      3721031 :   return (number_of_iterations_popcount (loop, exit, code, niter)
    2732      3718944 :           || number_of_iterations_cltz (loop, exit, code, niter)
    2733      7439806 :           || number_of_iterations_cltz_complement (loop, exit, code, niter));
    2734              : }
    2735              : 
    2736              : /* Substitute NEW_TREE for OLD in EXPR and fold the result.
    2737              :    If VALUEIZE is non-NULL then OLD and NEW_TREE are ignored and instead
    2738              :    all SSA names are replaced with the result of calling the VALUEIZE
    2739              :    function with the SSA name as argument.  */
    2740              : 
    2741              : tree
    2742    154179009 : simplify_replace_tree (tree expr, tree old, tree new_tree,
    2743              :                        tree (*valueize) (tree, void*), void *context,
    2744              :                        bool do_fold)
    2745              : {
    2746    154179009 :   unsigned i, n;
    2747    154179009 :   tree ret = NULL_TREE, e, se;
    2748              : 
    2749    154179009 :   if (!expr)
    2750              :     return NULL_TREE;
    2751              : 
    2752              :   /* Do not bother to replace constants.  */
    2753     31101854 :   if (CONSTANT_CLASS_P (expr))
    2754              :     return expr;
    2755              : 
    2756     23532871 :   if (valueize)
    2757              :     {
    2758       273004 :       if (TREE_CODE (expr) == SSA_NAME)
    2759              :         {
    2760        84493 :           new_tree = valueize (expr, context);
    2761        84493 :           if (new_tree != expr)
    2762              :             return new_tree;
    2763              :         }
    2764              :     }
    2765     23259867 :   else if (expr == old
    2766     23259867 :            || operand_equal_p (expr, old, 0))
    2767       218160 :     return unshare_expr (new_tree);
    2768              : 
    2769     23314181 :   if (!EXPR_P (expr))
    2770              :     return expr;
    2771              : 
    2772     12474207 :   n = TREE_OPERAND_LENGTH (expr);
    2773     34974092 :   for (i = 0; i < n; i++)
    2774              :     {
    2775     22499885 :       e = TREE_OPERAND (expr, i);
    2776     22499885 :       se = simplify_replace_tree (e, old, new_tree, valueize, context, do_fold);
    2777     22499885 :       if (e == se)
    2778     22221199 :         continue;
    2779              : 
    2780       278686 :       if (!ret)
    2781       278123 :         ret = copy_node (expr);
    2782              : 
    2783       278686 :       TREE_OPERAND (ret, i) = se;
    2784              :     }
    2785              : 
    2786     12474207 :   return (ret ? (do_fold ? fold (ret) : ret) : expr);
    2787              : }
    2788              : 
    2789              : /* Expand definitions of ssa names in EXPR as long as they are simple
    2790              :    enough, and return the new expression.  If STOP is specified, stop
    2791              :    expanding if EXPR equals to it.  */
    2792              : 
    2793              : static tree
    2794    122527610 : expand_simple_operations (tree expr, tree stop, hash_map<tree, tree> &cache)
    2795              : {
    2796    122969481 :   unsigned i, n;
    2797    122969481 :   tree ret = NULL_TREE, e, ee, e1;
    2798    122969481 :   enum tree_code code;
    2799    122969481 :   gimple *stmt;
    2800              : 
    2801    122969481 :   if (expr == NULL_TREE)
    2802              :     return expr;
    2803              : 
    2804    122969481 :   if (is_gimple_min_invariant (expr))
    2805              :     return expr;
    2806              : 
    2807     84318681 :   code = TREE_CODE (expr);
    2808     84318681 :   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
    2809              :     {
    2810     20197141 :       n = TREE_OPERAND_LENGTH (expr);
    2811     55840152 :       for (i = 0; i < n; i++)
    2812              :         {
    2813     35643011 :           e = TREE_OPERAND (expr, i);
    2814     35643011 :           if (!e)
    2815     32305391 :             continue;
    2816              :           /* SCEV analysis feeds us with a proper expression
    2817              :              graph matching the SSA graph.  Avoid turning it
    2818              :              into a tree here, thus handle tree sharing
    2819              :              properly.
    2820              :              ???  The SSA walk below still turns the SSA graph
    2821              :              into a tree but until we find a testcase do not
    2822              :              introduce additional tree sharing here.  */
    2823     35626217 :           bool existed_p;
    2824     35626217 :           tree &cee = cache.get_or_insert (e, &existed_p);
    2825     35626217 :           if (existed_p)
    2826       150677 :             ee = cee;
    2827              :           else
    2828              :             {
    2829     35475540 :               cee = e;
    2830     35475540 :               ee = expand_simple_operations (e, stop, cache);
    2831     35475540 :               if (ee != e)
    2832      3335782 :                 *cache.get (e) = ee;
    2833              :             }
    2834     35626217 :           if (e == ee)
    2835     32288597 :             continue;
    2836              : 
    2837      3337620 :           if (!ret)
    2838      3170615 :             ret = copy_node (expr);
    2839              : 
    2840      3337620 :           TREE_OPERAND (ret, i) = ee;
    2841              :         }
    2842              : 
    2843     20197141 :       if (!ret)
    2844              :         return expr;
    2845              : 
    2846      3170615 :       ret = fold (ret);
    2847      3170615 :       return ret;
    2848              :     }
    2849              : 
    2850              :   /* Stop if it's not ssa name or the one we don't want to expand.  */
    2851     64121540 :   if (TREE_CODE (expr) != SSA_NAME || expr == stop)
    2852              :     return expr;
    2853              : 
    2854     63923351 :   stmt = SSA_NAME_DEF_STMT (expr);
    2855     63923351 :   if (gimple_code (stmt) == GIMPLE_PHI)
    2856              :     {
    2857     17241221 :       basic_block src, dest;
    2858              : 
    2859     17241221 :       if (gimple_phi_num_args (stmt) != 1)
    2860              :         return expr;
    2861       591358 :       e = PHI_ARG_DEF (stmt, 0);
    2862              : 
    2863              :       /* Avoid propagating through loop exit phi nodes, which
    2864              :          could break loop-closed SSA form restrictions.  */
    2865       591358 :       dest = gimple_bb (stmt);
    2866       591358 :       src = single_pred (dest);
    2867       591358 :       if (TREE_CODE (e) == SSA_NAME
    2868       589538 :           && src->loop_father != dest->loop_father)
    2869              :         return expr;
    2870              : 
    2871              :       return expand_simple_operations (e, stop, cache);
    2872              :     }
    2873     46682130 :   if (gimple_code (stmt) != GIMPLE_ASSIGN)
    2874              :     return expr;
    2875              : 
    2876              :   /* Avoid expanding to expressions that contain SSA names that need
    2877              :      to take part in abnormal coalescing.  */
    2878     41202177 :   ssa_op_iter iter;
    2879     85650586 :   FOR_EACH_SSA_TREE_OPERAND (e, stmt, iter, SSA_OP_USE)
    2880     44449005 :     if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (e))
    2881              :       return expr;
    2882              : 
    2883     41201581 :   e = gimple_assign_rhs1 (stmt);
    2884     41201581 :   code = gimple_assign_rhs_code (stmt);
    2885     41201581 :   if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
    2886              :     {
    2887     16055479 :       if (is_gimple_min_invariant (e))
    2888              :         return e;
    2889              : 
    2890     16013339 :       if (code == SSA_NAME)
    2891              :         return expand_simple_operations (e, stop, cache);
    2892     15798121 :       else if (code == ADDR_EXPR)
    2893              :         {
    2894        14640 :           poly_int64 offset;
    2895        14640 :           tree base = get_addr_base_and_unit_offset (TREE_OPERAND (e, 0),
    2896              :                                                      &offset);
    2897        14640 :           if (base
    2898        14366 :               && TREE_CODE (base) == MEM_REF)
    2899              :             {
    2900        14366 :               ee = expand_simple_operations (TREE_OPERAND (base, 0), stop,
    2901              :                                              cache);
    2902        14366 :               return fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (expr), ee,
    2903              :                                   wide_int_to_tree (sizetype,
    2904              :                                                     mem_ref_offset (base)
    2905              :                                                     + offset));
    2906              :             }
    2907              :         }
    2908              : 
    2909     15783755 :       return expr;
    2910              :     }
    2911              : 
    2912     25146102 :   switch (code)
    2913              :     {
    2914      6292723 :     CASE_CONVERT:
    2915              :       /* Casts are simple.  */
    2916      6292723 :       ee = expand_simple_operations (e, stop, cache);
    2917      6292723 :       return fold_build1 (code, TREE_TYPE (expr), ee);
    2918              : 
    2919     13762974 :     case PLUS_EXPR:
    2920     13762974 :     case MINUS_EXPR:
    2921     13762974 :     case MULT_EXPR:
    2922     27525948 :       if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (expr))
    2923     27523367 :           && TYPE_OVERFLOW_TRAPS (TREE_TYPE (expr)))
    2924              :         return expr;
    2925              :       /* Fallthru.  */
    2926     14463170 :     case POINTER_PLUS_EXPR:
    2927              :       /* And increments and decrements by a constant are simple.  */
    2928     14463170 :       e1 = gimple_assign_rhs2 (stmt);
    2929     14463170 :       if (!is_gimple_min_invariant (e1))
    2930              :         return expr;
    2931              : 
    2932      7530178 :       ee = expand_simple_operations (e, stop, cache);
    2933      7530178 :       return fold_build2 (code, TREE_TYPE (expr), ee, e1);
    2934              : 
    2935              :     default:
    2936              :       return expr;
    2937              :     }
    2938              : }
    2939              : 
    2940              : tree
    2941     73214803 : expand_simple_operations (tree expr, tree stop)
    2942              : {
    2943     73214803 :   hash_map<tree, tree> cache;
    2944     73214803 :   return expand_simple_operations (expr, stop, cache);
    2945     73214803 : }
    2946              : 
    2947              : /* Tries to simplify EXPR using the condition COND.  Returns the simplified
    2948              :    expression (or EXPR unchanged, if no simplification was possible).  */
    2949              : 
    2950              : static tree
    2951      8912091 : tree_simplify_using_condition_1 (tree cond, tree expr)
    2952              : {
    2953      8912091 :   bool changed;
    2954      8912091 :   tree e, e0, e1, e2, notcond;
    2955      8912091 :   enum tree_code code = TREE_CODE (expr);
    2956              : 
    2957      8912091 :   if (code == INTEGER_CST)
    2958              :     return expr;
    2959              : 
    2960      8901891 :   if (code == TRUTH_OR_EXPR
    2961      8901891 :       || code == TRUTH_AND_EXPR
    2962      8901891 :       || code == COND_EXPR)
    2963              :     {
    2964       111805 :       changed = false;
    2965              : 
    2966       111805 :       e0 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 0));
    2967       111805 :       if (TREE_OPERAND (expr, 0) != e0)
    2968         2085 :         changed = true;
    2969              : 
    2970       111805 :       e1 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 1));
    2971       111805 :       if (TREE_OPERAND (expr, 1) != e1)
    2972            0 :         changed = true;
    2973              : 
    2974       111805 :       if (code == COND_EXPR)
    2975              :         {
    2976        12124 :           e2 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 2));
    2977        12124 :           if (TREE_OPERAND (expr, 2) != e2)
    2978              :             changed = true;
    2979              :         }
    2980              :       else
    2981              :         e2 = NULL_TREE;
    2982              : 
    2983       111805 :       if (changed)
    2984              :         {
    2985         2085 :           if (code == COND_EXPR)
    2986         1948 :             expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
    2987              :           else
    2988          137 :             expr = fold_build2 (code, boolean_type_node, e0, e1);
    2989              :         }
    2990              : 
    2991       111805 :       return expr;
    2992              :     }
    2993              : 
    2994              :   /* In case COND is equality, we may be able to simplify EXPR by copy/constant
    2995              :      propagation, and vice versa.  Fold does not handle this, since it is
    2996              :      considered too expensive.  */
    2997      8790086 :   if (TREE_CODE (cond) == EQ_EXPR)
    2998              :     {
    2999      2083115 :       e0 = TREE_OPERAND (cond, 0);
    3000      2083115 :       e1 = TREE_OPERAND (cond, 1);
    3001              : 
    3002              :       /* We know that e0 == e1.  Check whether we cannot simplify expr
    3003              :          using this fact.  */
    3004      2083115 :       e = simplify_replace_tree (expr, e0, e1);
    3005      2083115 :       if (integer_zerop (e) || integer_nonzerop (e))
    3006         2184 :         return e;
    3007              : 
    3008      2080931 :       e = simplify_replace_tree (expr, e1, e0);
    3009      2080931 :       if (integer_zerop (e) || integer_nonzerop (e))
    3010          411 :         return e;
    3011              :     }
    3012      8787491 :   if (TREE_CODE (expr) == EQ_EXPR)
    3013              :     {
    3014      1048476 :       e0 = TREE_OPERAND (expr, 0);
    3015      1048476 :       e1 = TREE_OPERAND (expr, 1);
    3016              : 
    3017              :       /* If e0 == e1 (EXPR) implies !COND, then EXPR cannot be true.  */
    3018      1048476 :       e = simplify_replace_tree (cond, e0, e1);
    3019      1048476 :       if (integer_zerop (e))
    3020              :         return e;
    3021      1034367 :       e = simplify_replace_tree (cond, e1, e0);
    3022      1034367 :       if (integer_zerop (e))
    3023              :         return e;
    3024              :     }
    3025      8773382 :   if (TREE_CODE (expr) == NE_EXPR)
    3026              :     {
    3027      1060639 :       e0 = TREE_OPERAND (expr, 0);
    3028      1060639 :       e1 = TREE_OPERAND (expr, 1);
    3029              : 
    3030              :       /* If e0 == e1 (!EXPR) implies !COND, then EXPR must be true.  */
    3031      1060639 :       e = simplify_replace_tree (cond, e0, e1);
    3032      1060639 :       if (integer_zerop (e))
    3033        12702 :         return boolean_true_node;
    3034      1047937 :       e = simplify_replace_tree (cond, e1, e0);
    3035      1047937 :       if (integer_zerop (e))
    3036            0 :         return boolean_true_node;
    3037              :     }
    3038              : 
    3039              :   /* Check whether COND ==> EXPR.  */
    3040      8760680 :   notcond = invert_truthvalue (cond);
    3041      8760680 :   e = fold_binary (TRUTH_OR_EXPR, boolean_type_node, notcond, expr);
    3042      8760680 :   if (e && integer_nonzerop (e))
    3043              :     return e;
    3044              : 
    3045              :   /* Check whether COND ==> not EXPR.  */
    3046      8758438 :   e = fold_binary (TRUTH_AND_EXPR, boolean_type_node, cond, expr);
    3047      8758438 :   if (e && integer_zerop (e))
    3048              :     return e;
    3049              : 
    3050              :   return expr;
    3051              : }
    3052              : 
    3053              : /* Tries to simplify EXPR using the condition COND.  Returns the simplified
    3054              :    expression (or EXPR unchanged, if no simplification was possible).
    3055              :    Wrapper around tree_simplify_using_condition_1 that ensures that chains
    3056              :    of simple operations in definitions of ssa names in COND are expanded,
    3057              :    so that things like casts or incrementing the value of the bound before
    3058              :    the loop do not cause us to fail.  */
    3059              : 
    3060              : static tree
    3061      8676357 : tree_simplify_using_condition (tree cond, tree expr)
    3062              : {
    3063      8676357 :   cond = expand_simple_operations (cond);
    3064              : 
    3065      8676357 :   return tree_simplify_using_condition_1 (cond, expr);
    3066              : }
    3067              : 
    3068              : /* Tries to simplify EXPR using the conditions on entry to LOOP.
    3069              :    Returns the simplified expression (or EXPR unchanged, if no
    3070              :    simplification was possible).  */
    3071              : 
    3072              : tree
    3073     27966513 : simplify_using_initial_conditions (class loop *loop, tree expr)
    3074              : {
    3075     27966513 :   edge e;
    3076     27966513 :   basic_block bb;
    3077     27966513 :   tree cond, expanded, backup;
    3078     27966513 :   int cnt = 0;
    3079              : 
    3080     27966513 :   if (TREE_CODE (expr) == INTEGER_CST)
    3081              :     return expr;
    3082              : 
    3083      2877160 :   value_range expr_range (TREE_TYPE (expr));
    3084      2877160 :   tree val;
    3085      2877160 :   if (TREE_TYPE (expr) == boolean_type_node
    3086      5733186 :       && get_range_query (cfun)->range_on_edge (expr_range,
    3087              :                                                 loop_preheader_edge (loop),
    3088              :                                                 expr)
    3089      5743753 :       && expr_range.singleton_p (&val))
    3090        84064 :     return val;
    3091              : 
    3092      2793096 :   backup = expanded = expand_simple_operations (expr);
    3093              : 
    3094              :   /* Limit walking the dominators to avoid quadraticness in
    3095              :      the number of BBs times the number of loops in degenerate
    3096              :      cases.  */
    3097      2793096 :   for (bb = loop->header;
    3098     27219935 :        bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
    3099     27219935 :          && cnt < param_max_niter_dominators_walk;
    3100     24426839 :        bb = get_immediate_dominator (CDI_DOMINATORS, bb))
    3101              :     {
    3102     24500190 :       if (!single_pred_p (bb))
    3103     12344885 :         continue;
    3104     12155305 :       e = single_pred_edge (bb);
    3105              : 
    3106     12155305 :       if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
    3107      3478948 :         continue;
    3108              : 
    3109     17352714 :       gcond *stmt = as_a <gcond *> (*gsi_last_bb (e->src));
    3110      8676357 :       cond = fold_build2 (gimple_cond_code (stmt),
    3111              :                           boolean_type_node,
    3112              :                           gimple_cond_lhs (stmt),
    3113              :                           gimple_cond_rhs (stmt));
    3114      8676357 :       if (e->flags & EDGE_FALSE_VALUE)
    3115      4886484 :         cond = invert_truthvalue (cond);
    3116      8676357 :       expanded = tree_simplify_using_condition (cond, expanded);
    3117              :       /* Break if EXPR is simplified to const values.  */
    3118      8676357 :       if (expanded
    3119      8676357 :           && (integer_zerop (expanded) || integer_nonzerop (expanded)))
    3120        73351 :         return expanded;
    3121              : 
    3122      8603006 :       ++cnt;
    3123              :     }
    3124              : 
    3125              :   /* Return the original expression if no simplification is done.  */
    3126      2719745 :   return operand_equal_p (backup, expanded, 0) ? expr : expanded;
    3127      2877160 : }
    3128              : 
    3129              : /* Tries to simplify EXPR using the evolutions of the loop invariants
    3130              :    in the superloops of LOOP.  Returns the simplified expression
    3131              :    (or EXPR unchanged, if no simplification was possible).  */
    3132              : 
    3133              : static tree
    3134      6529898 : simplify_using_outer_evolutions (class loop *loop, tree expr)
    3135              : {
    3136      6529898 :   enum tree_code code = TREE_CODE (expr);
    3137      6529898 :   bool changed;
    3138      6529898 :   tree e, e0, e1, e2;
    3139              : 
    3140      6529898 :   if (is_gimple_min_invariant (expr))
    3141              :     return expr;
    3142              : 
    3143      1491629 :   if (code == TRUTH_OR_EXPR
    3144      1491629 :       || code == TRUTH_AND_EXPR
    3145      1491629 :       || code == COND_EXPR)
    3146              :     {
    3147         9049 :       changed = false;
    3148              : 
    3149         9049 :       e0 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 0));
    3150         9049 :       if (TREE_OPERAND (expr, 0) != e0)
    3151            0 :         changed = true;
    3152              : 
    3153         9049 :       e1 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 1));
    3154         9049 :       if (TREE_OPERAND (expr, 1) != e1)
    3155            0 :         changed = true;
    3156              : 
    3157         9049 :       if (code == COND_EXPR)
    3158              :         {
    3159            0 :           e2 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 2));
    3160            0 :           if (TREE_OPERAND (expr, 2) != e2)
    3161              :             changed = true;
    3162              :         }
    3163              :       else
    3164              :         e2 = NULL_TREE;
    3165              : 
    3166         9049 :       if (changed)
    3167              :         {
    3168            0 :           if (code == COND_EXPR)
    3169            0 :             expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
    3170              :           else
    3171            0 :             expr = fold_build2 (code, boolean_type_node, e0, e1);
    3172              :         }
    3173              : 
    3174         9049 :       return expr;
    3175              :     }
    3176              : 
    3177      1482580 :   e = instantiate_parameters (loop, expr);
    3178      1482580 :   if (is_gimple_min_invariant (e))
    3179              :     return e;
    3180              : 
    3181              :   return expr;
    3182              : }
    3183              : 
    3184              : /* Returns true if EXIT is the only possible exit from LOOP.  */
    3185              : 
    3186              : bool
    3187     13282112 : loop_only_exit_p (const class loop *loop, basic_block *body, const_edge exit)
    3188              : {
    3189     13282112 :   gimple_stmt_iterator bsi;
    3190     13282112 :   unsigned i;
    3191              : 
    3192     13282112 :   if (exit != single_exit (loop))
    3193              :     return false;
    3194              : 
    3195     44690354 :   for (i = 0; i < loop->num_nodes; i++)
    3196    299894350 :     for (bsi = gsi_start_bb (body[i]); !gsi_end_p (bsi); gsi_next (&bsi))
    3197    229416342 :       if (stmt_can_terminate_bb_p (gsi_stmt (bsi)))
    3198              :         return false;
    3199              : 
    3200              :   return true;
    3201              : }
    3202              : 
    3203              : /* Stores description of number of iterations of LOOP derived from
    3204              :    EXIT (an exit edge of the LOOP) in NITER.  Returns true if some useful
    3205              :    information could be derived (and fields of NITER have meaning described
    3206              :    in comments at class tree_niter_desc declaration), false otherwise.
    3207              :    When EVERY_ITERATION is true, only tests that are known to be executed
    3208              :    every iteration are considered (i.e. only test that alone bounds the loop).
    3209              :    If AT_STMT is not NULL, this function stores LOOP's condition statement in
    3210              :    it when returning true.  */
    3211              : 
    3212              : bool
    3213     24144236 : number_of_iterations_exit_assumptions (class loop *loop, edge exit,
    3214              :                                        class tree_niter_desc *niter,
    3215              :                                        gcond **at_stmt, bool every_iteration,
    3216              :                                        basic_block *body)
    3217              : {
    3218     24144236 :   tree type;
    3219     24144236 :   tree op0, op1;
    3220     24144236 :   enum tree_code code;
    3221     24144236 :   affine_iv iv0, iv1;
    3222     24144236 :   bool safe;
    3223              : 
    3224              :   /* The condition at a fake exit (if it exists) does not control its
    3225              :      execution.  */
    3226     24144236 :   if (exit->flags & EDGE_FAKE)
    3227              :     return false;
    3228              : 
    3229              :   /* Nothing to analyze if the loop is known to be infinite.  */
    3230     24143805 :   if (loop_constraint_set_p (loop, LOOP_C_INFINITE))
    3231              :     return false;
    3232              : 
    3233     24143805 :   safe = dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src);
    3234              : 
    3235     24143805 :   if (every_iteration && !safe)
    3236              :     return false;
    3237              : 
    3238     23084354 :   niter->assumptions = boolean_false_node;
    3239     23084354 :   niter->control.base = NULL_TREE;
    3240     23084354 :   niter->control.step = NULL_TREE;
    3241     23084354 :   niter->control.no_overflow = false;
    3242     50749458 :   gcond *stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (exit->src));
    3243     21123545 :   if (!stmt)
    3244              :     return false;
    3245              : 
    3246     21123545 :   if (at_stmt)
    3247     20492419 :     *at_stmt = stmt;
    3248              : 
    3249              :   /* We want the condition for staying inside loop.  */
    3250     21123545 :   code = gimple_cond_code (stmt);
    3251     21123545 :   if (exit->flags & EDGE_TRUE_VALUE)
    3252      7864573 :     code = invert_tree_comparison (code, false);
    3253              : 
    3254     21123545 :   switch (code)
    3255              :     {
    3256     18014707 :     case GT_EXPR:
    3257     18014707 :     case GE_EXPR:
    3258     18014707 :     case LT_EXPR:
    3259     18014707 :     case LE_EXPR:
    3260     18014707 :     case NE_EXPR:
    3261     18014707 :       break;
    3262              : 
    3263      3108555 :     case EQ_EXPR:
    3264      3108555 :       return number_of_iterations_cltz (loop, exit, code, niter);
    3265              : 
    3266              :     default:
    3267              :       return false;
    3268              :     }
    3269              : 
    3270     18014707 :   op0 = gimple_cond_lhs (stmt);
    3271     18014707 :   op1 = gimple_cond_rhs (stmt);
    3272     18014707 :   type = TREE_TYPE (op0);
    3273              : 
    3274     18014707 :   if (!INTEGRAL_NB_TYPE_P (type)
    3275              :       && !POINTER_TYPE_P (type))
    3276              :     return false;
    3277              : 
    3278     17250662 :   tree iv0_niters = NULL_TREE;
    3279     34501324 :   if (!simple_iv_with_niters (loop, loop_containing_stmt (stmt),
    3280              :                               op0, &iv0, safe ? &iv0_niters : NULL, false))
    3281      3721031 :     return number_of_iterations_bitcount (loop, exit, code, niter);
    3282     13529631 :   tree iv1_niters = NULL_TREE;
    3283     27059262 :   if (!simple_iv_with_niters (loop, loop_containing_stmt (stmt),
    3284              :                               op1, &iv1, safe ? &iv1_niters : NULL, false))
    3285              :     return false;
    3286              :   /* Give up on complicated case.  */
    3287     12872681 :   if (iv0_niters && iv1_niters)
    3288              :     return false;
    3289              : 
    3290     12872681 :   iv0.base = expand_simple_operations (iv0.base);
    3291     12872681 :   iv1.base = expand_simple_operations (iv1.base);
    3292     12872681 :   bool body_from_caller = true;
    3293     12872681 :   if (!body)
    3294              :     {
    3295      7749159 :       body = get_loop_body (loop);
    3296      7749159 :       body_from_caller = false;
    3297              :     }
    3298     12872681 :   bool only_exit_p = loop_only_exit_p (loop, body, exit);
    3299     12872681 :   if (!body_from_caller)
    3300      7749159 :     free (body);
    3301     12872681 :   if (!number_of_iterations_cond (loop, type, &iv0, code, &iv1, niter,
    3302              :                                   only_exit_p, safe))
    3303              :     {
    3304              :       return false;
    3305              :     }
    3306              : 
    3307              :   /* Incorporate additional assumption implied by control iv.  */
    3308     12733900 :   tree iv_niters = iv0_niters ? iv0_niters : iv1_niters;
    3309     12733900 :   if (iv_niters)
    3310              :     {
    3311        25668 :       tree assumption = fold_build2 (LE_EXPR, boolean_type_node, niter->niter,
    3312              :                                      fold_convert (TREE_TYPE (niter->niter),
    3313              :                                                    iv_niters));
    3314              : 
    3315        25668 :       if (!integer_nonzerop (assumption))
    3316        25477 :         niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
    3317              :                                           niter->assumptions, assumption);
    3318              : 
    3319              :       /* Refine upper bound if possible.  */
    3320        25668 :       if (TREE_CODE (iv_niters) == INTEGER_CST
    3321        51336 :           && niter->max > wi::to_widest (iv_niters))
    3322        22388 :         niter->max = wi::to_widest (iv_niters);
    3323              :     }
    3324              : 
    3325              :   /* There is no assumptions if the loop is known to be finite.  */
    3326     12733900 :   if (!integer_zerop (niter->assumptions)
    3327     12733900 :       && loop_constraint_set_p (loop, LOOP_C_FINITE))
    3328        12155 :     niter->assumptions = boolean_true_node;
    3329              : 
    3330     12733900 :   if (optimize >= 3)
    3331              :     {
    3332      2170600 :       niter->assumptions = simplify_using_outer_evolutions (loop,
    3333              :                                                             niter->assumptions);
    3334      2170600 :       niter->may_be_zero = simplify_using_outer_evolutions (loop,
    3335              :                                                             niter->may_be_zero);
    3336      2170600 :       niter->niter = simplify_using_outer_evolutions (loop, niter->niter);
    3337              :     }
    3338              : 
    3339     12733900 :   niter->assumptions
    3340     12733900 :           = simplify_using_initial_conditions (loop,
    3341              :                                                niter->assumptions);
    3342     12733900 :   niter->may_be_zero
    3343     12733900 :           = simplify_using_initial_conditions (loop,
    3344              :                                                niter->may_be_zero);
    3345              : 
    3346              :   /* If NITER has simplified into a constant, update MAX.  */
    3347     12733900 :   if (TREE_CODE (niter->niter) == INTEGER_CST)
    3348      7302621 :     niter->max = wi::to_widest (niter->niter);
    3349              : 
    3350     12733900 :   return (!integer_zerop (niter->assumptions));
    3351              : }
    3352              : 
    3353              : /* Like number_of_iterations_exit_assumptions, but return TRUE only if
    3354              :    the niter information holds unconditionally.  */
    3355              : 
    3356              : bool
    3357     23420270 : number_of_iterations_exit (class loop *loop, edge exit,
    3358              :                            class tree_niter_desc *niter,
    3359              :                            bool warn, bool every_iteration,
    3360              :                            basic_block *body)
    3361              : {
    3362     23420270 :   gcond *stmt;
    3363     23420270 :   if (!number_of_iterations_exit_assumptions (loop, exit, niter,
    3364              :                                               &stmt, every_iteration, body))
    3365              :     return false;
    3366              : 
    3367     12385515 :   if (integer_nonzerop (niter->assumptions))
    3368              :     return true;
    3369              : 
    3370       574878 :   if (warn && dump_enabled_p ())
    3371            5 :     dump_printf_loc (MSG_MISSED_OPTIMIZATION, stmt,
    3372              :                      "missed loop optimization: niters analysis ends up "
    3373              :                      "with assumptions.\n");
    3374              : 
    3375              :   return false;
    3376              : }
    3377              : 
    3378              : /* Try to determine the number of iterations of LOOP.  If we succeed,
    3379              :    expression giving number of iterations is returned and *EXIT is
    3380              :    set to the edge from that the information is obtained.  Otherwise
    3381              :    chrec_dont_know is returned.  */
    3382              : 
    3383              : tree
    3384       750885 : find_loop_niter (class loop *loop, edge *exit)
    3385              : {
    3386       750885 :   unsigned i;
    3387       750885 :   auto_vec<edge> exits = get_loop_exit_edges (loop);
    3388       750885 :   edge ex;
    3389       750885 :   tree niter = NULL_TREE, aniter;
    3390       750885 :   class tree_niter_desc desc;
    3391              : 
    3392       750885 :   *exit = NULL;
    3393      3253820 :   FOR_EACH_VEC_ELT (exits, i, ex)
    3394              :     {
    3395      2502935 :       if (!number_of_iterations_exit (loop, ex, &desc, false))
    3396      1996946 :         continue;
    3397              : 
    3398       505989 :       if (integer_nonzerop (desc.may_be_zero))
    3399              :         {
    3400              :           /* We exit in the first iteration through this exit.
    3401              :              We won't find anything better.  */
    3402            0 :           niter = build_int_cst (unsigned_type_node, 0);
    3403            0 :           *exit = ex;
    3404            0 :           break;
    3405              :         }
    3406              : 
    3407       505989 :       if (!integer_zerop (desc.may_be_zero))
    3408        56450 :         continue;
    3409              : 
    3410       449539 :       aniter = desc.niter;
    3411              : 
    3412       449539 :       if (!niter)
    3413              :         {
    3414              :           /* Nothing recorded yet.  */
    3415       440222 :           niter = aniter;
    3416       440222 :           *exit = ex;
    3417       440222 :           continue;
    3418              :         }
    3419              : 
    3420              :       /* Prefer constants, the lower the better.  */
    3421         9317 :       if (TREE_CODE (aniter) != INTEGER_CST)
    3422         5323 :         continue;
    3423              : 
    3424         3994 :       if (TREE_CODE (niter) != INTEGER_CST)
    3425              :         {
    3426          960 :           niter = aniter;
    3427          960 :           *exit = ex;
    3428          960 :           continue;
    3429              :         }
    3430              : 
    3431         3034 :       if (tree_int_cst_lt (aniter, niter))
    3432              :         {
    3433          216 :           niter = aniter;
    3434          216 :           *exit = ex;
    3435          216 :           continue;
    3436              :         }
    3437              :     }
    3438              : 
    3439       750885 :   return niter ? niter : chrec_dont_know;
    3440       750885 : }
    3441              : 
    3442              : /* Return true if loop is known to have bounded number of iterations.  */
    3443              : 
    3444              : bool
    3445      2939931 : finite_loop_p (class loop *loop)
    3446              : {
    3447      2939931 :   widest_int nit;
    3448      2939931 :   int flags;
    3449              : 
    3450      2939931 :   if (loop->finite_p)
    3451              :     {
    3452      2179354 :       unsigned i;
    3453      2179354 :       auto_vec<edge> exits = get_loop_exit_edges (loop);
    3454      2179354 :       edge ex;
    3455              : 
    3456              :       /* If the loop has a normal exit, we can assume it will terminate.  */
    3457      4371363 :       FOR_EACH_VEC_ELT (exits, i, ex)
    3458      2187225 :         if (!(ex->flags & (EDGE_EH | EDGE_ABNORMAL | EDGE_FAKE)))
    3459              :           {
    3460      2174902 :             if (dump_file)
    3461          172 :               fprintf (dump_file, "Assume loop %i to be finite: it has an exit "
    3462              :                        "and -ffinite-loops is on or loop was "
    3463              :                        "previously finite.\n",
    3464              :                        loop->num);
    3465      2174902 :             return true;
    3466              :           }
    3467      2179354 :     }
    3468              : 
    3469       765029 :   flags = flags_from_decl_or_type (current_function_decl);
    3470       765029 :   if ((flags & (ECF_CONST|ECF_PURE)) && !(flags & ECF_LOOPING_CONST_OR_PURE))
    3471              :     {
    3472         1960 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3473            0 :         fprintf (dump_file,
    3474              :                  "Found loop %i to be finite: it is within "
    3475              :                  "pure or const function.\n",
    3476              :                  loop->num);
    3477         1960 :       loop->finite_p = true;
    3478         1960 :       return true;
    3479              :     }
    3480              : 
    3481       763069 :   if (loop->any_upper_bound
    3482              :       /* Loop with no normal exit will not pass max_loop_iterations.  */
    3483       763069 :       || (!loop->finite_p && max_loop_iterations (loop, &nit)))
    3484              :     {
    3485       401714 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3486           31 :         fprintf (dump_file, "Found loop %i to be finite: upper bound found.\n",
    3487              :                  loop->num);
    3488       401714 :       loop->finite_p = true;
    3489       401714 :       return true;
    3490              :     }
    3491              : 
    3492              :   return false;
    3493      2939931 : }
    3494              : 
    3495              : /*
    3496              : 
    3497              :    Analysis of a number of iterations of a loop by a brute-force evaluation.
    3498              : 
    3499              : */
    3500              : 
    3501              : /* Bound on the number of iterations we try to evaluate.  */
    3502              : 
    3503              : #define MAX_ITERATIONS_TO_TRACK \
    3504              :   ((unsigned) param_max_iterations_to_track)
    3505              : 
    3506              : /* Returns the loop phi node of LOOP such that ssa name X is derived from its
    3507              :    result by a chain of operations such that all but exactly one of their
    3508              :    operands are constants.  */
    3509              : 
    3510              : static gphi *
    3511      1919404 : chain_of_csts_start (class loop *loop, tree x)
    3512              : {
    3513      2297032 :   gimple *stmt = SSA_NAME_DEF_STMT (x);
    3514      2297032 :   tree use;
    3515      2297032 :   basic_block bb = gimple_bb (stmt);
    3516      2297032 :   enum tree_code code;
    3517              : 
    3518      2297032 :   if (!bb
    3519      2297032 :       || !flow_bb_inside_loop_p (loop, bb))
    3520       527449 :     return NULL;
    3521              : 
    3522      1769583 :   if (gimple_code (stmt) == GIMPLE_PHI)
    3523              :     {
    3524       709828 :       if (bb == loop->header)
    3525       646805 :         return as_a <gphi *> (stmt);
    3526              : 
    3527              :       return NULL;
    3528              :     }
    3529              : 
    3530      1059755 :   if (gimple_code (stmt) != GIMPLE_ASSIGN
    3531      2000781 :       || gimple_assign_rhs_class (stmt) == GIMPLE_TERNARY_RHS)
    3532              :     return NULL;
    3533              : 
    3534       940769 :   code = gimple_assign_rhs_code (stmt);
    3535       940769 :   if (gimple_references_memory_p (stmt)
    3536       508455 :       || TREE_CODE_CLASS (code) == tcc_reference
    3537       483430 :       || (code == ADDR_EXPR
    3538          513 :           && !is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
    3539       457852 :     return NULL;
    3540              : 
    3541       482917 :   use = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
    3542       482917 :   if (use == NULL_TREE)
    3543              :     return NULL;
    3544              : 
    3545              :   return chain_of_csts_start (loop, use);
    3546              : }
    3547              : 
    3548              : /* Determines whether the expression X is derived from a result of a phi node
    3549              :    in header of LOOP such that
    3550              : 
    3551              :    * the derivation of X consists only from operations with constants
    3552              :    * the initial value of the phi node is constant
    3553              :    * the value of the phi node in the next iteration can be derived from the
    3554              :      value in the current iteration by a chain of operations with constants,
    3555              :      or is also a constant
    3556              : 
    3557              :    If such phi node exists, it is returned, otherwise NULL is returned.  */
    3558              : 
    3559              : static gphi *
    3560      1733605 : get_base_for (class loop *loop, tree x)
    3561              : {
    3562      1733605 :   gphi *phi;
    3563      1733605 :   tree init, next;
    3564              : 
    3565      1733605 :   if (is_gimple_min_invariant (x))
    3566              :     return NULL;
    3567              : 
    3568      1733605 :   phi = chain_of_csts_start (loop, x);
    3569      1733605 :   if (!phi)
    3570              :     return NULL;
    3571              : 
    3572       476017 :   init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
    3573       476017 :   next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
    3574              : 
    3575       476017 :   if (!is_gimple_min_invariant (init))
    3576              :     return NULL;
    3577              : 
    3578       210925 :   if (TREE_CODE (next) == SSA_NAME
    3579       210925 :       && chain_of_csts_start (loop, next) != phi)
    3580              :     return NULL;
    3581              : 
    3582              :   return phi;
    3583              : }
    3584              : 
    3585              : /* Given an expression X, then
    3586              : 
    3587              :    * if X is NULL_TREE, we return the constant BASE.
    3588              :    * if X is a constant, we return the constant X.
    3589              :    * otherwise X is a SSA name, whose value in the considered loop is derived
    3590              :      by a chain of operations with constant from a result of a phi node in
    3591              :      the header of the loop.  Then we return value of X when the value of the
    3592              :      result of this phi node is given by the constant BASE.  */
    3593              : 
    3594              : static tree
    3595      7402967 : get_val_for (tree x, tree base)
    3596              : {
    3597      7410322 :   gimple *stmt;
    3598              : 
    3599      7410322 :   gcc_checking_assert (is_gimple_min_invariant (base));
    3600              : 
    3601      7410322 :   if (!x)
    3602              :     return base;
    3603      5188630 :   else if (is_gimple_min_invariant (x))
    3604              :     return x;
    3605              : 
    3606      5163898 :   stmt = SSA_NAME_DEF_STMT (x);
    3607      5163898 :   if (gimple_code (stmt) == GIMPLE_PHI)
    3608              :     return base;
    3609              : 
    3610      2742469 :   gcc_checking_assert (is_gimple_assign (stmt));
    3611              : 
    3612              :   /* STMT must be either an assignment of a single SSA name or an
    3613              :      expression involving an SSA name and a constant.  Try to fold that
    3614              :      expression using the value for the SSA name.  */
    3615      2742469 :   if (gimple_assign_ssa_name_copy_p (stmt))
    3616         7355 :     return get_val_for (gimple_assign_rhs1 (stmt), base);
    3617      2735114 :   else if (gimple_assign_rhs_class (stmt) == GIMPLE_UNARY_RHS
    3618      2735114 :            && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
    3619       285720 :     return fold_build1 (gimple_assign_rhs_code (stmt),
    3620              :                         TREE_TYPE (gimple_assign_lhs (stmt)),
    3621              :                         get_val_for (gimple_assign_rhs1 (stmt), base));
    3622      2449394 :   else if (gimple_assign_rhs_class (stmt) == GIMPLE_BINARY_RHS)
    3623              :     {
    3624      2449394 :       tree rhs1 = gimple_assign_rhs1 (stmt);
    3625      2449394 :       tree rhs2 = gimple_assign_rhs2 (stmt);
    3626      2449394 :       if (TREE_CODE (rhs1) == SSA_NAME)
    3627      2387784 :         rhs1 = get_val_for (rhs1, base);
    3628        61610 :       else if (TREE_CODE (rhs2) == SSA_NAME)
    3629        61610 :         rhs2 = get_val_for (rhs2, base);
    3630              :       else
    3631            0 :         gcc_unreachable ();
    3632      2449394 :       return fold_build2 (gimple_assign_rhs_code (stmt),
    3633              :                           TREE_TYPE (gimple_assign_lhs (stmt)), rhs1, rhs2);
    3634              :     }
    3635              :   else
    3636            0 :     gcc_unreachable ();
    3637              : }
    3638              : 
    3639              : 
    3640              : /* Tries to count the number of iterations of LOOP till it exits by EXIT
    3641              :    by brute force -- i.e. by determining the value of the operands of the
    3642              :    condition at EXIT in first few iterations of the loop (assuming that
    3643              :    these values are constant) and determining the first one in that the
    3644              :    condition is not satisfied.  Returns the constant giving the number
    3645              :    of the iterations of LOOP if successful, chrec_dont_know otherwise.  */
    3646              : 
    3647              : tree
    3648      1688421 : loop_niter_by_eval (class loop *loop, edge exit)
    3649              : {
    3650      1688421 :   tree acnd;
    3651      1688421 :   tree op[2], val[2], next[2], aval[2];
    3652      1688421 :   gphi *phi;
    3653      1688421 :   unsigned i, j;
    3654      1688421 :   enum tree_code cmp;
    3655              : 
    3656      3376842 :   gcond *cond = safe_dyn_cast <gcond *> (*gsi_last_bb (exit->src));
    3657      1688421 :   if (!cond)
    3658       121906 :     return chrec_dont_know;
    3659              : 
    3660      1566515 :   cmp = gimple_cond_code (cond);
    3661      1566515 :   if (exit->flags & EDGE_TRUE_VALUE)
    3662       565638 :     cmp = invert_tree_comparison (cmp, false);
    3663              : 
    3664      1566515 :   switch (cmp)
    3665              :     {
    3666      1566490 :     case EQ_EXPR:
    3667      1566490 :     case NE_EXPR:
    3668      1566490 :     case GT_EXPR:
    3669      1566490 :     case GE_EXPR:
    3670      1566490 :     case LT_EXPR:
    3671      1566490 :     case LE_EXPR:
    3672      1566490 :       op[0] = gimple_cond_lhs (cond);
    3673      1566490 :       op[1] = gimple_cond_rhs (cond);
    3674      1566490 :       break;
    3675              : 
    3676           25 :     default:
    3677           25 :       return chrec_dont_know;
    3678              :     }
    3679              : 
    3680      1790895 :   for (j = 0; j < 2; j++)
    3681              :     {
    3682      1762130 :       if (is_gimple_min_invariant (op[j]))
    3683              :         {
    3684        28525 :           val[j] = op[j];
    3685        28525 :           next[j] = NULL_TREE;
    3686        28525 :           op[j] = NULL_TREE;
    3687              :         }
    3688              :       else
    3689              :         {
    3690      1733605 :           phi = get_base_for (loop, op[j]);
    3691      1733605 :           if (!phi)
    3692              :             {
    3693      1537725 :               gassign *def;
    3694      1537725 :               if (j == 0
    3695      1370850 :                   && (cmp == NE_EXPR || cmp == EQ_EXPR)
    3696       789289 :                   && TREE_CODE (op[0]) == SSA_NAME
    3697       789289 :                   && TREE_CODE (op[1]) == INTEGER_CST
    3698       508046 :                   && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op[0])))
    3699      1810229 :                   && gimple_assign_rhs_code (def) == MEM_REF)
    3700              :                 {
    3701        58831 :                   tree mem = gimple_assign_rhs1 (def);
    3702        58831 :                   affine_iv iv;
    3703        58831 :                   if (TYPE_MODE (TREE_TYPE (mem)) == TYPE_MODE (char_type_node)
    3704        22845 :                       && simple_iv (loop, loop,
    3705        22845 :                                     TREE_OPERAND (mem, 0), &iv, false)
    3706        16498 :                       && tree_fits_uhwi_p (TREE_OPERAND (mem, 1))
    3707        75329 :                       && tree_fits_uhwi_p (iv.step))
    3708              :                     {
    3709        16498 :                       tree str, off;
    3710              :                       /* iv.base can be &"Foo" but also (char *)&"Foo" + 1.  */
    3711        16498 :                       split_constant_offset (iv.base, &str, &off);
    3712        16498 :                       STRIP_NOPS (str);
    3713        16498 :                       if (TREE_CODE (str) == ADDR_EXPR
    3714         3202 :                           && TREE_CODE (TREE_OPERAND (str, 0)) == STRING_CST
    3715        18224 :                           && tree_fits_uhwi_p (off))
    3716              :                         {
    3717         1726 :                           str = TREE_OPERAND (str, 0);
    3718         1726 :                           unsigned i = 0;
    3719         3452 :                           for (unsigned HOST_WIDE_INT idx
    3720         1726 :                                = (tree_to_uhwi (TREE_OPERAND (mem, 1))
    3721         1726 :                                   + tree_to_uhwi (off));
    3722        25650 :                                idx < (unsigned)TREE_STRING_LENGTH (str)
    3723        12825 :                                && i < MAX_ITERATIONS_TO_TRACK;
    3724        11099 :                                idx += tree_to_uhwi (iv.step), ++i)
    3725              :                             {
    3726        12031 :                               int res = compare_tree_int
    3727        12031 :                                 (op[1], TREE_STRING_POINTER (str)[idx]);
    3728        12031 :                               if ((cmp == NE_EXPR && res == 0)
    3729        11118 :                                   || (cmp == EQ_EXPR && res != 0))
    3730          932 :                                 return build_int_cst (unsigned_type_node, i);
    3731              :                             }
    3732              :                         }
    3733              :                     }
    3734              :                 }
    3735      1536793 :               return chrec_dont_know;
    3736              :             }
    3737       195880 :           val[j] = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
    3738       195880 :           next[j] = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
    3739              :         }
    3740              :     }
    3741              : 
    3742      1181612 :   for (i = 0; i < MAX_ITERATIONS_TO_TRACK; i++)
    3743              :     {
    3744      3542109 :       for (j = 0; j < 2; j++)
    3745      2361406 :         aval[j] = get_val_for (op[j], val[j]);
    3746              : 
    3747      1180703 :       acnd = fold_binary (cmp, boolean_type_node, aval[0], aval[1]);
    3748      1180703 :       if (acnd && integer_zerop (acnd))
    3749              :         {
    3750        27128 :           if (dump_file && (dump_flags & TDF_DETAILS))
    3751            3 :             fprintf (dump_file,
    3752              :                      "Proved that loop %d iterates %d times using brute force.\n",
    3753              :                      loop->num, i);
    3754        27128 :           return build_int_cst (unsigned_type_node, i);
    3755              :         }
    3756              : 
    3757      3459319 :       for (j = 0; j < 2; j++)
    3758              :         {
    3759      2306447 :           aval[j] = val[j];
    3760      2306447 :           val[j] = get_val_for (next[j], val[j]);
    3761      2306447 :           if (!is_gimple_min_invariant (val[j]))
    3762          703 :             return chrec_dont_know;
    3763              :         }
    3764              : 
    3765              :       /* If the next iteration would use the same base values
    3766              :          as the current one, there is no point looping further,
    3767              :          all following iterations will be the same as this one.  */
    3768      1152872 :       if (val[0] == aval[0] && val[1] == aval[1])
    3769              :         break;
    3770              :     }
    3771              : 
    3772          934 :   return chrec_dont_know;
    3773              : }
    3774              : 
    3775              : /* Finds the exit of the LOOP by that the loop exits after a constant
    3776              :    number of iterations and stores the exit edge to *EXIT.  The constant
    3777              :    giving the number of iterations of LOOP is returned.  The number of
    3778              :    iterations is determined using loop_niter_by_eval (i.e. by brute force
    3779              :    evaluation).  If we are unable to find the exit for that loop_niter_by_eval
    3780              :    determines the number of iterations, chrec_dont_know is returned.  */
    3781              : 
    3782              : tree
    3783       775745 : find_loop_niter_by_eval (class loop *loop, edge *exit)
    3784              : {
    3785       775745 :   unsigned i;
    3786       775745 :   auto_vec<edge> exits = get_loop_exit_edges (loop);
    3787       775745 :   edge ex;
    3788       775745 :   tree niter = NULL_TREE, aniter;
    3789              : 
    3790       775745 :   *exit = NULL;
    3791              : 
    3792              :   /* Loops with multiple exits are expensive to handle and less important.  */
    3793       775745 :   if (!flag_expensive_optimizations
    3794       796159 :       && exits.length () > 1)
    3795         4305 :     return chrec_dont_know;
    3796              : 
    3797      2316543 :   FOR_EACH_VEC_ELT (exits, i, ex)
    3798              :     {
    3799      1545103 :       if (!just_once_each_iteration_p (loop, ex->src))
    3800       412394 :         continue;
    3801              : 
    3802      1132709 :       aniter = loop_niter_by_eval (loop, ex);
    3803      1132709 :       if (chrec_contains_undetermined (aniter))
    3804      1117509 :         continue;
    3805              : 
    3806        15243 :       if (niter
    3807        15200 :           && !tree_int_cst_lt (aniter, niter))
    3808           43 :         continue;
    3809              : 
    3810        15157 :       niter = aniter;
    3811        15157 :       *exit = ex;
    3812              :     }
    3813              : 
    3814       771440 :   return niter ? niter : chrec_dont_know;
    3815       775745 : }
    3816              : 
    3817              : /*
    3818              : 
    3819              :    Analysis of upper bounds on number of iterations of a loop.
    3820              : 
    3821              : */
    3822              : 
    3823              : static widest_int derive_constant_upper_bound_ops (tree, tree,
    3824              :                                                    enum tree_code, tree);
    3825              : 
    3826              : /* Returns a constant upper bound on the value of the right-hand side of
    3827              :    an assignment statement STMT.  */
    3828              : 
    3829              : static widest_int
    3830          279 : derive_constant_upper_bound_assign (gimple *stmt)
    3831              : {
    3832          279 :   enum tree_code code = gimple_assign_rhs_code (stmt);
    3833          279 :   tree op0 = gimple_assign_rhs1 (stmt);
    3834          279 :   tree op1 = gimple_assign_rhs2 (stmt);
    3835              : 
    3836          279 :   return derive_constant_upper_bound_ops (TREE_TYPE (gimple_assign_lhs (stmt)),
    3837          279 :                                           op0, code, op1);
    3838              : }
    3839              : 
    3840              : /* Returns a constant upper bound on the value of expression VAL.  VAL
    3841              :    is considered to be unsigned.  If its type is signed, its value must
    3842              :    be nonnegative.  */
    3843              : 
    3844              : static widest_int
    3845     11177249 : derive_constant_upper_bound (tree val)
    3846              : {
    3847     11177249 :   enum tree_code code;
    3848     11177249 :   tree op0, op1, op2;
    3849              : 
    3850     11177249 :   extract_ops_from_tree (val, &code, &op0, &op1, &op2);
    3851     11177249 :   return derive_constant_upper_bound_ops (TREE_TYPE (val), op0, code, op1);
    3852              : }
    3853              : 
    3854              : /* Returns a constant upper bound on the value of expression OP0 CODE OP1,
    3855              :    whose type is TYPE.  The expression is considered to be unsigned.  If
    3856              :    its type is signed, its value must be nonnegative.  */
    3857              : 
    3858              : static widest_int
    3859     11177528 : derive_constant_upper_bound_ops (tree type, tree op0,
    3860              :                                  enum tree_code code, tree op1)
    3861              : {
    3862     11177528 :   tree subtype, maxt;
    3863     11177528 :   widest_int bnd, max, cst;
    3864     11177528 :   gimple *stmt;
    3865              : 
    3866     11177528 :   if (INTEGRAL_TYPE_P (type))
    3867     11177528 :     maxt = TYPE_MAX_VALUE (type);
    3868              :   else
    3869            0 :     maxt = upper_bound_in_type (type, type);
    3870              : 
    3871     11177528 :   max = wi::to_widest (maxt);
    3872              : 
    3873     11177528 :   switch (code)
    3874              :     {
    3875     11103921 :     case INTEGER_CST:
    3876     11103921 :       return wi::to_widest (op0);
    3877              : 
    3878         1053 :     CASE_CONVERT:
    3879         1053 :       subtype = TREE_TYPE (op0);
    3880         1053 :       if (!TYPE_UNSIGNED (subtype)
    3881              :           /* If TYPE is also signed, the fact that VAL is nonnegative implies
    3882              :              that OP0 is nonnegative.  */
    3883          774 :           && TYPE_UNSIGNED (type)
    3884         1827 :           && !tree_expr_nonnegative_p (op0))
    3885              :         {
    3886              :           /* If we cannot prove that the casted expression is nonnegative,
    3887              :              we cannot establish more useful upper bound than the precision
    3888              :              of the type gives us.  */
    3889          751 :           return max;
    3890              :         }
    3891              : 
    3892              :       /* We now know that op0 is an nonnegative value.  Try deriving an upper
    3893              :          bound for it.  */
    3894          302 :       bnd = derive_constant_upper_bound (op0);
    3895              : 
    3896              :       /* If the bound does not fit in TYPE, max. value of TYPE could be
    3897              :          attained.  */
    3898          302 :       if (wi::ltu_p (max, bnd))
    3899            0 :         return max;
    3900              : 
    3901          302 :       return bnd;
    3902              : 
    3903        42251 :     case PLUS_EXPR:
    3904        42251 :     case POINTER_PLUS_EXPR:
    3905        42251 :     case MINUS_EXPR:
    3906        42251 :       if (TREE_CODE (op1) != INTEGER_CST
    3907        42251 :           || !tree_expr_nonnegative_p (op0))
    3908        41820 :         return max;
    3909              : 
    3910              :       /* Canonicalize to OP0 - CST.  Consider CST to be signed, in order to
    3911              :          choose the most logical way how to treat this constant regardless
    3912              :          of the signedness of the type.  */
    3913          431 :       cst = wi::sext (wi::to_widest (op1), TYPE_PRECISION (type));
    3914          431 :       if (code != MINUS_EXPR)
    3915          431 :         cst = -cst;
    3916              : 
    3917          431 :       bnd = derive_constant_upper_bound (op0);
    3918              : 
    3919          431 :       if (wi::neg_p (cst))
    3920              :         {
    3921           29 :           cst = -cst;
    3922              :           /* Avoid CST == 0x80000...  */
    3923           29 :           if (wi::neg_p (cst))
    3924            0 :             return max;
    3925              : 
    3926              :           /* OP0 + CST.  We need to check that
    3927              :              BND <= MAX (type) - CST.  */
    3928              : 
    3929           29 :           widest_int mmax = max - cst;
    3930           29 :           if (wi::leu_p (bnd, mmax))
    3931            0 :             return max;
    3932              : 
    3933           29 :           return bnd + cst;
    3934           29 :         }
    3935              :       else
    3936              :         {
    3937              :           /* OP0 - CST, where CST >= 0.
    3938              : 
    3939              :              If TYPE is signed, we have already verified that OP0 >= 0, and we
    3940              :              know that the result is nonnegative.  This implies that
    3941              :              VAL <= BND - CST.
    3942              : 
    3943              :              If TYPE is unsigned, we must additionally know that OP0 >= CST,
    3944              :              otherwise the operation underflows.
    3945              :            */
    3946              : 
    3947              :           /* This should only happen if the type is unsigned; however, for
    3948              :              buggy programs that use overflowing signed arithmetics even with
    3949              :              -fno-wrapv, this condition may also be true for signed values.  */
    3950          402 :           if (wi::ltu_p (bnd, cst))
    3951            0 :             return max;
    3952              : 
    3953          402 :           if (TYPE_UNSIGNED (type))
    3954              :             {
    3955          402 :               tree tem = fold_binary (GE_EXPR, boolean_type_node, op0,
    3956              :                                       wide_int_to_tree (type, cst));
    3957          402 :               if (!tem || integer_nonzerop (tem))
    3958           50 :                 return max;
    3959              :             }
    3960              : 
    3961          352 :           bnd -= cst;
    3962              :         }
    3963              : 
    3964          352 :       return bnd;
    3965              : 
    3966            0 :     case FLOOR_DIV_EXPR:
    3967            0 :     case EXACT_DIV_EXPR:
    3968            0 :       if (TREE_CODE (op1) != INTEGER_CST
    3969            0 :           || tree_int_cst_sign_bit (op1))
    3970            0 :         return max;
    3971              : 
    3972            0 :       bnd = derive_constant_upper_bound (op0);
    3973            0 :       return wi::udiv_floor (bnd, wi::to_widest (op1));
    3974              : 
    3975            7 :     case BIT_AND_EXPR:
    3976            7 :       if (TREE_CODE (op1) != INTEGER_CST
    3977            7 :           || tree_int_cst_sign_bit (op1))
    3978            0 :         return max;
    3979            7 :       return wi::to_widest (op1);
    3980              : 
    3981          473 :     case SSA_NAME:
    3982          473 :       stmt = SSA_NAME_DEF_STMT (op0);
    3983          473 :       if (gimple_code (stmt) != GIMPLE_ASSIGN
    3984          473 :           || gimple_assign_lhs (stmt) != op0)
    3985          194 :         return max;
    3986          279 :       return derive_constant_upper_bound_assign (stmt);
    3987              : 
    3988        29823 :     default:
    3989        29823 :       return max;
    3990              :     }
    3991     11177541 : }
    3992              : 
    3993              : /* Emit a -Waggressive-loop-optimizations warning if needed.  */
    3994              : 
    3995              : static void
    3996     10226750 : do_warn_aggressive_loop_optimizations (class loop *loop,
    3997              :                                        widest_int i_bound, gimple *stmt)
    3998              : {
    3999              :   /* Don't warn if the loop doesn't have known constant bound.  */
    4000     10226750 :   if (!loop->nb_iterations
    4001     10226750 :       || TREE_CODE (loop->nb_iterations) != INTEGER_CST
    4002      4784154 :       || !warn_aggressive_loop_optimizations
    4003              :       /* To avoid warning multiple times for the same loop,
    4004              :          only start warning when we preserve loops.  */
    4005      4783987 :       || (cfun->curr_properties & PROP_loops) == 0
    4006              :       /* Only warn once per loop.  */
    4007      4783987 :       || loop->warned_aggressive_loop_optimizations
    4008              :       /* Only warn if undefined behavior gives us lower estimate than the
    4009              :          known constant bound.  */
    4010      4783219 :       || wi::cmpu (i_bound, wi::to_widest (loop->nb_iterations)) >= 0
    4011              :       /* And undefined behavior happens unconditionally.  */
    4012     10226805 :       || !dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (stmt)))
    4013     10226695 :     return;
    4014              : 
    4015           55 :   edge e = single_exit (loop);
    4016           55 :   if (e == NULL)
    4017              :     return;
    4018              : 
    4019           55 :   gimple *estmt = last_nondebug_stmt (e->src);
    4020           55 :   char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p;
    4021           55 :   unsigned len;
    4022           55 :   if (print_dec_buf_size (i_bound, TYPE_SIGN (TREE_TYPE (loop->nb_iterations)),
    4023              :                           &len))
    4024            0 :     p = XALLOCAVEC (char, len);
    4025              :   else
    4026              :     p = buf;
    4027           55 :   print_dec (i_bound, p, TYPE_SIGN (TREE_TYPE (loop->nb_iterations)));
    4028           55 :   auto_diagnostic_group d;
    4029           55 :   if (warning_at (gimple_location (stmt), OPT_Waggressive_loop_optimizations,
    4030              :                   "iteration %s invokes undefined behavior", p))
    4031            9 :     inform (gimple_location (estmt), "within this loop");
    4032           55 :   loop->warned_aggressive_loop_optimizations = true;
    4033           55 : }
    4034              : 
    4035              : /* Records that AT_STMT is executed at most BOUND + 1 times in LOOP.  IS_EXIT
    4036              :    is true if the loop is exited immediately after STMT, and this exit
    4037              :    is taken at last when the STMT is executed BOUND + 1 times.
    4038              :    REALISTIC is true if BOUND is expected to be close to the real number
    4039              :    of iterations.  UPPER is true if we are sure the loop iterates at most
    4040              :    BOUND times.  I_BOUND is a widest_int upper estimate on BOUND.  */
    4041              : 
    4042              : static void
    4043     15908971 : record_estimate (class loop *loop, tree bound, const widest_int &i_bound,
    4044              :                  gimple *at_stmt, bool is_exit, bool realistic, bool upper)
    4045              : {
    4046     15908971 :   widest_int delta;
    4047              : 
    4048     15908971 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4049              :     {
    4050       125027 :       fprintf (dump_file, "Statement %s", is_exit ? "(exit)" : "");
    4051        69351 :       print_gimple_stmt (dump_file, at_stmt, 0, TDF_SLIM);
    4052        70040 :       fprintf (dump_file, " is %sexecuted at most ",
    4053              :                upper ? "" : "probably ");
    4054        69351 :       print_generic_expr (dump_file, bound, TDF_SLIM);
    4055        69351 :       fprintf (dump_file, " (bounded by ");
    4056        69351 :       print_decu (i_bound, dump_file);
    4057        69351 :       fprintf (dump_file, ") + 1 times in loop %d.\n", loop->num);
    4058              :     }
    4059              : 
    4060              :   /* If the I_BOUND is just an estimate of BOUND, it rarely is close to the
    4061              :      real number of iterations.  */
    4062     15908971 :   if (TREE_CODE (bound) != INTEGER_CST)
    4063              :     realistic = false;
    4064              :   else
    4065     14045002 :     gcc_checking_assert (i_bound == wi::to_widest (bound));
    4066              : 
    4067     15908971 :   if (wi::min_precision (i_bound, SIGNED) > bound_wide_int ().get_precision ())
    4068              :     return;
    4069              : 
    4070              :   /* If we have a guaranteed upper bound, record it in the appropriate
    4071              :      list, unless this is an !is_exit bound (i.e. undefined behavior in
    4072              :      at_stmt) in a loop with known constant number of iterations.  */
    4073     15908944 :   if (upper
    4074     15377576 :       && (is_exit
    4075     10645135 :           || loop->nb_iterations == NULL_TREE
    4076     10645135 :           || TREE_CODE (loop->nb_iterations) != INTEGER_CST))
    4077              :     {
    4078     10395242 :       class nb_iter_bound *elt = ggc_alloc<nb_iter_bound> ();
    4079              : 
    4080     10395242 :       elt->bound = bound_wide_int::from (i_bound, SIGNED);
    4081     10395242 :       elt->stmt = at_stmt;
    4082     10395242 :       elt->is_exit = is_exit;
    4083     10395242 :       elt->next = loop->bounds;
    4084     10395242 :       loop->bounds = elt;
    4085              :     }
    4086              : 
    4087              :   /* If statement is executed on every path to the loop latch, we can directly
    4088              :      infer the upper bound on the # of iterations of the loop.  */
    4089     15908944 :   if (!dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (at_stmt)))
    4090       487566 :     upper = false;
    4091              : 
    4092              :   /* Update the number of iteration estimates according to the bound.
    4093              :      If at_stmt is an exit then the loop latch is executed at most BOUND times,
    4094              :      otherwise it can be executed BOUND + 1 times.  We will lower the estimate
    4095              :      later if such statement must be executed on last iteration  */
    4096     15908944 :   if (is_exit)
    4097      4732443 :     delta = 0;
    4098              :   else
    4099     11176501 :     delta = 1;
    4100     15908944 :   widest_int new_i_bound = i_bound + delta;
    4101              : 
    4102              :   /* If an overflow occurred, ignore the result.  */
    4103     15908944 :   if (wi::ltu_p (new_i_bound, delta))
    4104            0 :     return;
    4105              : 
    4106     15908944 :   if (upper && !is_exit)
    4107     10226750 :     do_warn_aggressive_loop_optimizations (loop, new_i_bound, at_stmt);
    4108     15908944 :   record_niter_bound (loop, new_i_bound, realistic, upper);
    4109     15908971 : }
    4110              : 
    4111              : /* Records the control iv analyzed in NITER for LOOP if the iv is valid
    4112              :    and doesn't overflow.  */
    4113              : 
    4114              : static void
    4115      4732455 : record_control_iv (class loop *loop, class tree_niter_desc *niter)
    4116              : {
    4117      4732455 :   struct control_iv *iv;
    4118              : 
    4119      4732455 :   if (!niter->control.base || !niter->control.step)
    4120              :     return;
    4121              : 
    4122      4694382 :   if (!integer_onep (niter->assumptions) || !niter->control.no_overflow)
    4123              :     return;
    4124              : 
    4125      4552456 :   iv = ggc_alloc<control_iv> ();
    4126      4552456 :   iv->base = niter->control.base;
    4127      4552456 :   iv->step = niter->control.step;
    4128      4552456 :   iv->next = loop->control_ivs;
    4129      4552456 :   loop->control_ivs = iv;
    4130              : 
    4131      4552456 :   return;
    4132              : }
    4133              : 
    4134              : /* This function returns TRUE if below conditions are satisfied:
    4135              :      1) VAR is SSA variable.
    4136              :      2) VAR is an IV:{base, step} in its defining loop.
    4137              :      3) IV doesn't overflow.
    4138              :      4) Both base and step are integer constants.
    4139              :      5) Base is the MIN/MAX value depends on IS_MIN.
    4140              :    Store value of base to INIT correspondingly.  */
    4141              : 
    4142              : static bool
    4143       122726 : get_cst_init_from_scev (tree var, wide_int *init, bool is_min)
    4144              : {
    4145       122726 :   if (TREE_CODE (var) != SSA_NAME)
    4146              :     return false;
    4147              : 
    4148       122726 :   gimple *def_stmt = SSA_NAME_DEF_STMT (var);
    4149       228448 :   class loop *loop = loop_containing_stmt (def_stmt);
    4150              : 
    4151       110444 :   if (loop == NULL)
    4152              :     return false;
    4153              : 
    4154       110444 :   affine_iv iv;
    4155       110444 :   if (!simple_iv (loop, loop, var, &iv, false))
    4156              :     return false;
    4157              : 
    4158         5945 :   if (!iv.no_overflow)
    4159              :     return false;
    4160              : 
    4161         5899 :   if (TREE_CODE (iv.base) != INTEGER_CST || TREE_CODE (iv.step) != INTEGER_CST)
    4162              :     return false;
    4163              : 
    4164         4947 :   if (is_min == tree_int_cst_sign_bit (iv.step))
    4165              :     return false;
    4166              : 
    4167         4722 :   *init = wi::to_wide (iv.base);
    4168         4722 :   return true;
    4169              : }
    4170              : 
    4171              : /* Record the estimate on number of iterations of LOOP based on the fact that
    4172              :    the induction variable BASE + STEP * i evaluated in STMT does not wrap and
    4173              :    its values belong to the range <LOW, HIGH>.  REALISTIC is true if the
    4174              :    estimated number of iterations is expected to be close to the real one.
    4175              :    UPPER is true if we are sure the induction variable does not wrap.  */
    4176              : 
    4177              : static void
    4178     11176514 : record_nonwrapping_iv (class loop *loop, tree base, tree step, gimple *stmt,
    4179              :                        tree low, tree high, bool realistic, bool upper)
    4180              : {
    4181     11176514 :   tree niter_bound, extreme, delta;
    4182     11176514 :   tree type = TREE_TYPE (base), unsigned_type;
    4183     11176514 :   tree orig_base = base;
    4184              : 
    4185     11176514 :   if (TREE_CODE (step) != INTEGER_CST || integer_zerop (step))
    4186            0 :     return;
    4187              : 
    4188     11176514 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4189              :     {
    4190        55676 :       fprintf (dump_file, "Induction variable (");
    4191        55676 :       print_generic_expr (dump_file, TREE_TYPE (base), TDF_SLIM);
    4192        55676 :       fprintf (dump_file, ") ");
    4193        55676 :       print_generic_expr (dump_file, base, TDF_SLIM);
    4194        55676 :       fprintf (dump_file, " + ");
    4195        55676 :       print_generic_expr (dump_file, step, TDF_SLIM);
    4196        55676 :       fprintf (dump_file, " * iteration does not wrap in statement ");
    4197        55676 :       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    4198        55676 :       fprintf (dump_file, " in loop %d.\n", loop->num);
    4199              :     }
    4200              : 
    4201     11176514 :   unsigned_type = unsigned_type_for (type);
    4202     11176514 :   base = fold_convert (unsigned_type, base);
    4203     11176514 :   step = fold_convert (unsigned_type, step);
    4204              : 
    4205     11176514 :   if (tree_int_cst_sign_bit (step))
    4206              :     {
    4207       344406 :       wide_int max;
    4208       344406 :       value_range base_range (TREE_TYPE (orig_base));
    4209       688812 :       if (get_range_query (cfun)->range_of_expr (base_range, orig_base)
    4210       344406 :           && !base_range.undefined_p ())
    4211       344327 :         max = wi::to_wide (base_range.ubound ());
    4212       344406 :       extreme = fold_convert (unsigned_type, low);
    4213       344406 :       if (TREE_CODE (orig_base) == SSA_NAME
    4214        20066 :           && TREE_CODE (high) == INTEGER_CST
    4215        20066 :           && INTEGRAL_TYPE_P (TREE_TYPE (orig_base))
    4216        19992 :           && ((!base_range.varying_p ()
    4217         6793 :                && !base_range.undefined_p ())
    4218        13278 :               || get_cst_init_from_scev (orig_base, &max, false))
    4219       351120 :           && wi::gts_p (wi::to_wide (high), max))
    4220         1936 :         base = wide_int_to_tree (unsigned_type, max);
    4221       342470 :       else if (TREE_CODE (base) != INTEGER_CST
    4222       550918 :                && dominated_by_p (CDI_DOMINATORS,
    4223       208448 :                                   loop->latch, gimple_bb (stmt)))
    4224       206792 :         base = fold_convert (unsigned_type, high);
    4225       344406 :       delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme);
    4226       344406 :       step = fold_build1 (NEGATE_EXPR, unsigned_type, step);
    4227       344407 :     }
    4228              :   else
    4229              :     {
    4230     10832108 :       wide_int min;
    4231     10832108 :       value_range base_range (TREE_TYPE (orig_base));
    4232     21664216 :       if (get_range_query (cfun)->range_of_expr (base_range, orig_base)
    4233     10832108 :           && !base_range.undefined_p ())
    4234     10829830 :         min = wi::to_wide (base_range.lbound ());
    4235     10832108 :       extreme = fold_convert (unsigned_type, high);
    4236     10832108 :       if (TREE_CODE (orig_base) == SSA_NAME
    4237       868071 :           && TREE_CODE (low) == INTEGER_CST
    4238       868071 :           && INTEGRAL_TYPE_P (TREE_TYPE (orig_base))
    4239       268762 :           && ((!base_range.varying_p ()
    4240       161309 :                && !base_range.undefined_p ())
    4241       109448 :               || get_cst_init_from_scev (orig_base, &min, true))
    4242     10996144 :           && wi::gts_p (min, wi::to_wide (low)))
    4243        12870 :         base = wide_int_to_tree (unsigned_type, min);
    4244     10819238 :       else if (TREE_CODE (base) != INTEGER_CST
    4245     14225882 :                && dominated_by_p (CDI_DOMINATORS,
    4246      3406644 :                                   loop->latch, gimple_bb (stmt)))
    4247      3335705 :         base = fold_convert (unsigned_type, low);
    4248     10832108 :       delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base);
    4249     10832120 :     }
    4250              : 
    4251              :   /* STMT is executed at most NITER_BOUND + 1 times, since otherwise the value
    4252              :      would get out of the range.  */
    4253     11176514 :   niter_bound = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step);
    4254     11176514 :   widest_int max = derive_constant_upper_bound (niter_bound);
    4255     11176514 :   record_estimate (loop, niter_bound, max, stmt, false, realistic, upper);
    4256     11176514 : }
    4257              : 
    4258              : /* Determine information about number of iterations a LOOP from the index
    4259              :    IDX of a data reference accessed in STMT.  RELIABLE is true if STMT is
    4260              :    guaranteed to be executed in every iteration of LOOP.  Callback for
    4261              :    for_each_index.  */
    4262              : 
    4263              : struct ilb_data
    4264              : {
    4265              :   class loop *loop;
    4266              :   gimple *stmt;
    4267              : };
    4268              : 
    4269              : static bool
    4270     35206212 : idx_infer_loop_bounds (tree base, tree *idx, void *dta)
    4271              : {
    4272     35206212 :   struct ilb_data *data = (struct ilb_data *) dta;
    4273     35206212 :   tree ev, init, step;
    4274     35206212 :   tree low, high, type, next;
    4275     35206212 :   bool sign, upper = true, has_flexible_size = false;
    4276     35206212 :   class loop *loop = data->loop;
    4277              : 
    4278     35206212 :   if (TREE_CODE (base) != ARRAY_REF)
    4279              :     return true;
    4280              : 
    4281              :   /* For arrays that might have flexible sizes, it is not guaranteed that they
    4282              :      do not really extend over their declared size.  */
    4283      9470582 :   if (array_ref_flexible_size_p (base))
    4284              :     {
    4285      2278341 :       has_flexible_size = true;
    4286      2278341 :       upper = false;
    4287              :     }
    4288              : 
    4289      9470582 :   class loop *dloop = loop_containing_stmt (data->stmt);
    4290      9470582 :   if (!dloop)
    4291              :     return true;
    4292              : 
    4293      9470582 :   ev = analyze_scalar_evolution (dloop, *idx);
    4294      9470582 :   ev = instantiate_parameters (loop, ev);
    4295      9470582 :   init = initial_condition (ev);
    4296      9470582 :   step = evolution_part_in_loop_num (ev, loop->num);
    4297              : 
    4298      9470582 :   if (!init
    4299      9470582 :       || !step
    4300      5622218 :       || TREE_CODE (step) != INTEGER_CST
    4301      4310080 :       || integer_zerop (step)
    4302      4310080 :       || tree_contains_chrecs (init, NULL)
    4303     13780662 :       || chrec_contains_symbols_defined_in_loop (init, loop->num))
    4304      5160502 :     return true;
    4305              : 
    4306      4310080 :   low = array_ref_low_bound (base);
    4307      4310080 :   high = array_ref_up_bound (base);
    4308              : 
    4309              :   /* The case of nonconstant bounds could be handled, but it would be
    4310              :      complicated.  */
    4311      4310080 :   if (TREE_CODE (low) != INTEGER_CST
    4312      4310080 :       || !high
    4313      3584083 :       || TREE_CODE (high) != INTEGER_CST)
    4314              :     return true;
    4315      3380818 :   sign = tree_int_cst_sign_bit (step);
    4316      3380818 :   type = TREE_TYPE (step);
    4317              : 
    4318              :   /* The array that might have flexible size most likely extends
    4319              :      beyond its bounds.  */
    4320      3380818 :   if (has_flexible_size
    4321      3380818 :       && operand_equal_p (low, high, 0))
    4322              :     return true;
    4323              : 
    4324              :   /* In case the relevant bound of the array does not fit in type, or
    4325              :      it does, but bound + step (in type) still belongs into the range of the
    4326              :      array, the index may wrap and still stay within the range of the array
    4327              :      (consider e.g. if the array is indexed by the full range of
    4328              :      unsigned char).
    4329              : 
    4330              :      To make things simpler, we require both bounds to fit into type, although
    4331              :      there are cases where this would not be strictly necessary.  */
    4332      3361808 :   if (!int_fits_type_p (high, type)
    4333      3361776 :       || !int_fits_type_p (low, type))
    4334              :     return true;
    4335      3361776 :   low = fold_convert (type, low);
    4336      3361776 :   high = fold_convert (type, high);
    4337              : 
    4338      3361776 :   if (sign)
    4339        59527 :     next = fold_binary (PLUS_EXPR, type, low, step);
    4340              :   else
    4341      3302249 :     next = fold_binary (PLUS_EXPR, type, high, step);
    4342              : 
    4343      3361776 :   if (tree_int_cst_compare (low, next) <= 0
    4344      3361776 :       && tree_int_cst_compare (next, high) <= 0)
    4345              :     return true;
    4346              : 
    4347              :   /* If access is not executed on every iteration, we must ensure that overflow
    4348              :      may not make the access valid later.  */
    4349      3361776 :   if (!dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (data->stmt)))
    4350              :     {
    4351       478833 :       if (scev_probably_wraps_p (NULL_TREE,
    4352       478833 :                                  initial_condition_in_loop_num (ev, loop->num),
    4353              :                                  step, data->stmt, loop, true))
    4354      3361776 :         upper = false;
    4355              :     }
    4356              :   else
    4357      2882943 :     record_nonwrapping_chrec (ev);
    4358              : 
    4359      3361776 :   record_nonwrapping_iv (loop, init, step, data->stmt, low, high, false, upper);
    4360      3361776 :   return true;
    4361              : }
    4362              : 
    4363              : /* Determine information about number of iterations a LOOP from the bounds
    4364              :    of arrays in the data reference REF accessed in STMT.  RELIABLE is true if
    4365              :    STMT is guaranteed to be executed in every iteration of LOOP.*/
    4366              : 
    4367              : static void
    4368     35923900 : infer_loop_bounds_from_ref (class loop *loop, gimple *stmt, tree ref)
    4369              : {
    4370     35923900 :   struct ilb_data data;
    4371              : 
    4372     35923900 :   data.loop = loop;
    4373     35923900 :   data.stmt = stmt;
    4374            0 :   for_each_index (&ref, idx_infer_loop_bounds, &data);
    4375            0 : }
    4376              : 
    4377              : /* Determine information about number of iterations of a LOOP from the way
    4378              :    arrays are used in STMT.  RELIABLE is true if STMT is guaranteed to be
    4379              :    executed in every iteration of LOOP.  */
    4380              : 
    4381              : static void
    4382    257020215 : infer_loop_bounds_from_array (class loop *loop, gimple *stmt)
    4383              : {
    4384    257020215 :   if (is_gimple_assign (stmt))
    4385              :     {
    4386    103476338 :       tree op0 = gimple_assign_lhs (stmt);
    4387    103476338 :       tree op1 = gimple_assign_rhs1 (stmt);
    4388              : 
    4389              :       /* For each memory access, analyze its access function
    4390              :          and record a bound on the loop iteration domain.  */
    4391    103476338 :       if (REFERENCE_CLASS_P (op0))
    4392     14034204 :         infer_loop_bounds_from_ref (loop, stmt, op0);
    4393              : 
    4394    103476338 :       if (REFERENCE_CLASS_P (op1))
    4395     21717052 :         infer_loop_bounds_from_ref (loop, stmt, op1);
    4396              :     }
    4397    153543877 :   else if (is_gimple_call (stmt))
    4398              :     {
    4399      7245983 :       tree arg, lhs;
    4400      7245983 :       unsigned i, n = gimple_call_num_args (stmt);
    4401              : 
    4402      7245983 :       lhs = gimple_call_lhs (stmt);
    4403      7245983 :       if (lhs && REFERENCE_CLASS_P (lhs))
    4404         5048 :         infer_loop_bounds_from_ref (loop, stmt, lhs);
    4405              : 
    4406     23473555 :       for (i = 0; i < n; i++)
    4407              :         {
    4408     16227572 :           arg = gimple_call_arg (stmt, i);
    4409     16227572 :           if (REFERENCE_CLASS_P (arg))
    4410       167596 :             infer_loop_bounds_from_ref (loop, stmt, arg);
    4411              :         }
    4412              :     }
    4413    257020215 : }
    4414              : 
    4415              : /* Determine information about number of iterations of a LOOP from the fact
    4416              :    that pointer arithmetics in STMT does not overflow.  */
    4417              : 
    4418              : static void
    4419    126636479 : infer_loop_bounds_from_pointer_arith (class loop *loop, gimple *stmt)
    4420              : {
    4421    126636479 :   tree def, base, step, scev, type, low, high;
    4422    126636479 :   tree var, ptr;
    4423              : 
    4424    126636479 :   if (!is_gimple_assign (stmt)
    4425    126636479 :       || gimple_assign_rhs_code (stmt) != POINTER_PLUS_EXPR)
    4426              :     return;
    4427              : 
    4428      4556470 :   def = gimple_assign_lhs (stmt);
    4429      4556470 :   if (TREE_CODE (def) != SSA_NAME)
    4430              :     return;
    4431              : 
    4432      4556470 :   type = TREE_TYPE (def);
    4433      4556470 :   if (!nowrap_type_p (type))
    4434              :     return;
    4435              : 
    4436      4556470 :   ptr = gimple_assign_rhs1 (stmt);
    4437      4556470 :   if (!expr_invariant_in_loop_p (loop, ptr))
    4438              :     return;
    4439              : 
    4440      2527849 :   var = gimple_assign_rhs2 (stmt);
    4441      2527849 :   if (TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (var)))
    4442              :     return;
    4443              : 
    4444      2527849 :   class loop *uloop = loop_containing_stmt (stmt);
    4445      2527849 :   scev = instantiate_parameters (loop, analyze_scalar_evolution (uloop, def));
    4446      2527849 :   if (chrec_contains_undetermined (scev))
    4447              :     return;
    4448              : 
    4449      2123510 :   base = initial_condition_in_loop_num (scev, loop->num);
    4450      2123510 :   step = evolution_part_in_loop_num (scev, loop->num);
    4451              : 
    4452      2123510 :   if (!base || !step
    4453      2075078 :       || TREE_CODE (step) != INTEGER_CST
    4454      1875343 :       || tree_contains_chrecs (base, NULL)
    4455      3998853 :       || chrec_contains_symbols_defined_in_loop (base, loop->num))
    4456       248167 :     return;
    4457              : 
    4458      1875343 :   low = lower_bound_in_type (type, type);
    4459      1875343 :   high = upper_bound_in_type (type, type);
    4460              : 
    4461              :   /* In C, pointer arithmetic p + 1 cannot use a NULL pointer, and p - 1 cannot
    4462              :      produce a NULL pointer.  The contrary would mean NULL points to an object,
    4463              :      while NULL is supposed to compare unequal with the address of all objects.
    4464              :      Furthermore, p + 1 cannot produce a NULL pointer and p - 1 cannot use a
    4465              :      NULL pointer since that would mean wrapping, which we assume here not to
    4466              :      happen.  So, we can exclude NULL from the valid range of pointer
    4467              :      arithmetic.  */
    4468      1875343 :   if (flag_delete_null_pointer_checks && int_cst_value (low) == 0)
    4469      1875343 :     low = build_int_cstu (TREE_TYPE (low), TYPE_ALIGN_UNIT (TREE_TYPE (type)));
    4470              : 
    4471      1875343 :   record_nonwrapping_chrec (scev);
    4472      1875343 :   record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true);
    4473              : }
    4474              : 
    4475              : /* Determine information about number of iterations of a LOOP from the fact
    4476              :    that signed arithmetics in STMT does not overflow.  */
    4477              : 
    4478              : static void
    4479    126636479 : infer_loop_bounds_from_signedness (class loop *loop, gimple *stmt)
    4480              : {
    4481    126636479 :   tree def, base, step, scev, type, low, high;
    4482              : 
    4483    126636479 :   if (gimple_code (stmt) != GIMPLE_ASSIGN)
    4484    120697084 :     return;
    4485              : 
    4486     52042555 :   def = gimple_assign_lhs (stmt);
    4487              : 
    4488     52042555 :   if (TREE_CODE (def) != SSA_NAME)
    4489              :     return;
    4490              : 
    4491     44494724 :   type = TREE_TYPE (def);
    4492     44494724 :   if (!INTEGRAL_TYPE_P (type)
    4493     44494724 :       || !TYPE_OVERFLOW_UNDEFINED (type))
    4494              :     return;
    4495              : 
    4496     14980153 :   scev = instantiate_parameters (loop, analyze_scalar_evolution (loop, def));
    4497     14980153 :   if (chrec_contains_undetermined (scev))
    4498              :     return;
    4499              : 
    4500      7403836 :   base = initial_condition_in_loop_num (scev, loop->num);
    4501      7403836 :   step = evolution_part_in_loop_num (scev, loop->num);
    4502              : 
    4503      7403836 :   if (!base || !step
    4504      6609203 :       || TREE_CODE (step) != INTEGER_CST
    4505      5939395 :       || tree_contains_chrecs (base, NULL)
    4506     13343231 :       || chrec_contains_symbols_defined_in_loop (base, loop->num))
    4507      1464441 :     return;
    4508              : 
    4509      5939395 :   low = lower_bound_in_type (type, type);
    4510      5939395 :   high = upper_bound_in_type (type, type);
    4511      5939395 :   int_range_max r (TREE_TYPE (def));
    4512     11878790 :   get_range_query (cfun)->range_of_expr (r, def);
    4513      5939395 :   if (!r.varying_p () && !r.undefined_p ())
    4514              :     {
    4515      5431220 :       low = wide_int_to_tree (type, r.lower_bound ());
    4516      5431232 :       high = wide_int_to_tree (type, r.upper_bound ());
    4517              :     }
    4518              : 
    4519      5939395 :   record_nonwrapping_chrec (scev);
    4520      5939395 :   record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true);
    4521      5939395 : }
    4522              : 
    4523              : /* The following analyzers are extracting information on the bounds
    4524              :    of LOOP from the following undefined behaviors:
    4525              : 
    4526              :    - data references should not access elements over the statically
    4527              :      allocated size,
    4528              : 
    4529              :    - signed variables should not overflow when flag_wrapv is not set.
    4530              : */
    4531              : 
    4532              : static void
    4533      6465790 : infer_loop_bounds_from_undefined (class loop *loop, basic_block *bbs)
    4534              : {
    4535      6465790 :   unsigned i;
    4536      6465790 :   gimple_stmt_iterator bsi;
    4537      6465790 :   basic_block bb;
    4538      6465790 :   bool reliable;
    4539              : 
    4540     44526629 :   for (i = 0; i < loop->num_nodes; i++)
    4541              :     {
    4542     38060839 :       bb = bbs[i];
    4543              : 
    4544              :       /* If BB is not executed in each iteration of the loop, we cannot
    4545              :          use the operations in it to infer reliable upper bound on the
    4546              :          # of iterations of the loop.  However, we can use it as a guess.
    4547              :          Reliable guesses come only from array bounds.  */
    4548     38060839 :       reliable = dominated_by_p (CDI_DOMINATORS, loop->latch, bb);
    4549              : 
    4550    333141893 :       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
    4551              :         {
    4552    257020215 :           gimple *stmt = gsi_stmt (bsi);
    4553              : 
    4554    257020215 :           infer_loop_bounds_from_array (loop, stmt);
    4555              : 
    4556    257020215 :           if (reliable)
    4557              :             {
    4558    126636479 :               infer_loop_bounds_from_signedness (loop, stmt);
    4559    126636479 :               infer_loop_bounds_from_pointer_arith (loop, stmt);
    4560              :             }
    4561              :         }
    4562              : 
    4563              :     }
    4564      6465790 : }
    4565              : 
    4566              : /* Compare wide ints, callback for qsort.  */
    4567              : 
    4568              : static int
    4569        28670 : wide_int_cmp (const void *p1, const void *p2)
    4570              : {
    4571        28670 :   const bound_wide_int *d1 = (const bound_wide_int *) p1;
    4572        28670 :   const bound_wide_int *d2 = (const bound_wide_int *) p2;
    4573        28670 :   return wi::cmpu (*d1, *d2);
    4574              : }
    4575              : 
    4576              : /* Return index of BOUND in BOUNDS array sorted in increasing order.
    4577              :    Lookup by binary search.  */
    4578              : 
    4579              : static int
    4580         6674 : bound_index (const vec<bound_wide_int> &bounds, const bound_wide_int &bound)
    4581              : {
    4582         6674 :   unsigned int end = bounds.length ();
    4583              :   unsigned int begin = 0;
    4584              : 
    4585              :   /* Find a matching index by means of a binary search.  */
    4586         7626 :   while (begin != end)
    4587              :     {
    4588         7626 :       unsigned int middle = (begin + end) / 2;
    4589         7626 :       bound_wide_int index = bounds[middle];
    4590              : 
    4591         7626 :       if (index == bound)
    4592         6674 :         return middle;
    4593          952 :       else if (wi::ltu_p (index, bound))
    4594          233 :         begin = middle + 1;
    4595              :       else
    4596              :         end = middle;
    4597              :     }
    4598            0 :   gcc_unreachable ();
    4599              : }
    4600              : 
    4601              : /* We recorded loop bounds only for statements dominating loop latch (and thus
    4602              :    executed each loop iteration).  If there are any bounds on statements not
    4603              :    dominating the loop latch we can improve the estimate by walking the loop
    4604              :    body and seeing if every path from loop header to loop latch contains
    4605              :    some bounded statement.  */
    4606              : 
    4607              : static void
    4608      6505237 : discover_iteration_bound_by_body_walk (class loop *loop)
    4609              : {
    4610      6505237 :   class nb_iter_bound *elt;
    4611      6505237 :   auto_vec<bound_wide_int> bounds;
    4612      6505237 :   vec<vec<basic_block> > queues = vNULL;
    4613      6505237 :   vec<basic_block> queue = vNULL;
    4614      6505237 :   ptrdiff_t queue_index;
    4615      6505237 :   ptrdiff_t latch_index = 0;
    4616              : 
    4617              :   /* Discover what bounds may interest us.  */
    4618     16900479 :   for (elt = loop->bounds; elt; elt = elt->next)
    4619              :     {
    4620     10395242 :       bound_wide_int bound = elt->bound;
    4621              : 
    4622              :       /* Exit terminates loop at given iteration, while non-exits produce undefined
    4623              :          effect on the next iteration.  */
    4624     10395242 :       if (!elt->is_exit)
    4625              :         {
    4626      5662801 :           bound += 1;
    4627              :           /* If an overflow occurred, ignore the result.  */
    4628      5662801 :           if (bound == 0)
    4629            0 :             continue;
    4630              :         }
    4631              : 
    4632     10395242 :       if (!loop->any_upper_bound
    4633     10395242 :           || wi::ltu_p (bound, loop->nb_iterations_upper_bound))
    4634         6674 :         bounds.safe_push (bound);
    4635              :     }
    4636              : 
    4637              :   /* Exit early if there is nothing to do.  */
    4638      6505237 :   if (!bounds.exists ())
    4639      6501563 :     return;
    4640              : 
    4641         3674 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4642           16 :     fprintf (dump_file, " Trying to walk loop body to reduce the bound.\n");
    4643              : 
    4644              :   /* Sort the bounds in decreasing order.  */
    4645         3674 :   bounds.qsort (wide_int_cmp);
    4646              : 
    4647              :   /* For every basic block record the lowest bound that is guaranteed to
    4648              :      terminate the loop.  */
    4649              : 
    4650         3674 :   hash_map<basic_block, ptrdiff_t> bb_bounds;
    4651        18298 :   for (elt = loop->bounds; elt; elt = elt->next)
    4652              :     {
    4653        14624 :       bound_wide_int bound = elt->bound;
    4654        14624 :       if (!elt->is_exit)
    4655              :         {
    4656        10269 :           bound += 1;
    4657              :           /* If an overflow occurred, ignore the result.  */
    4658        10269 :           if (bound == 0)
    4659            0 :             continue;
    4660              :         }
    4661              : 
    4662        14624 :       if (!loop->any_upper_bound
    4663        14624 :           || wi::ltu_p (bound, loop->nb_iterations_upper_bound))
    4664              :         {
    4665         6674 :           ptrdiff_t index = bound_index (bounds, bound);
    4666         6674 :           ptrdiff_t *entry = bb_bounds.get (gimple_bb (elt->stmt));
    4667         6674 :           if (!entry)
    4668         4816 :             bb_bounds.put (gimple_bb (elt->stmt), index);
    4669         1858 :           else if ((ptrdiff_t)*entry > index)
    4670          119 :             *entry = index;
    4671              :         }
    4672              :     }
    4673              : 
    4674         3674 :   hash_map<basic_block, ptrdiff_t> block_priority;
    4675              : 
    4676              :   /* Perform shortest path discovery loop->header ... loop->latch.
    4677              : 
    4678              :      The "distance" is given by the smallest loop bound of basic block
    4679              :      present in the path and we look for path with largest smallest bound
    4680              :      on it.
    4681              : 
    4682              :      To avoid the need for fibonacci heap on double ints we simply compress
    4683              :      double ints into indexes to BOUNDS array and then represent the queue
    4684              :      as arrays of queues for every index.
    4685              :      Index of BOUNDS.length() means that the execution of given BB has
    4686              :      no bounds determined.
    4687              : 
    4688              :      VISITED is a pointer map translating basic block into smallest index
    4689              :      it was inserted into the priority queue with.  */
    4690         3674 :   latch_index = -1;
    4691              : 
    4692              :   /* Start walk in loop header with index set to infinite bound.  */
    4693         3674 :   queue_index = bounds.length ();
    4694         3674 :   queues.safe_grow_cleared (queue_index + 1, true);
    4695         3674 :   queue.safe_push (loop->header);
    4696         3674 :   queues[queue_index] = queue;
    4697         3674 :   block_priority.put (loop->header, queue_index);
    4698              : 
    4699        14022 :   for (; queue_index >= 0; queue_index--)
    4700              :     {
    4701        10348 :       if (latch_index < queue_index)
    4702              :         {
    4703        42112 :           while (queues[queue_index].length ())
    4704              :             {
    4705        38121 :               basic_block bb;
    4706        38121 :               ptrdiff_t bound_index = queue_index;
    4707        38121 :               edge e;
    4708        38121 :               edge_iterator ei;
    4709              : 
    4710        38121 :               queue = queues[queue_index];
    4711        38121 :               bb = queue.pop ();
    4712              : 
    4713              :               /* OK, we later inserted the BB with lower priority, skip it.  */
    4714        38121 :               if (*block_priority.get (bb) > queue_index)
    4715            0 :                 continue;
    4716              : 
    4717              :               /* See if we can improve the bound.  */
    4718        38121 :               ptrdiff_t *entry = bb_bounds.get (bb);
    4719        38121 :               if (entry && *entry < bound_index)
    4720         4315 :                 bound_index = *entry;
    4721              : 
    4722              :               /* Insert successors into the queue, watch for latch edge
    4723              :                  and record greatest index we saw.  */
    4724        97367 :               FOR_EACH_EDGE (e, ei, bb->succs)
    4725              :                 {
    4726        59246 :                   bool insert = false;
    4727              : 
    4728        59246 :                   if (loop_exit_edge_p (loop, e))
    4729        10409 :                     continue;
    4730              : 
    4731        48837 :                   if (e == loop_latch_edge (loop)
    4732        48837 :                       && latch_index < bound_index)
    4733              :                     latch_index = bound_index;
    4734        45163 :                   else if (!(entry = block_priority.get (e->dest)))
    4735              :                     {
    4736        35913 :                       insert = true;
    4737        35913 :                       block_priority.put (e->dest, bound_index);
    4738              :                     }
    4739         9250 :                   else if (*entry < bound_index)
    4740              :                     {
    4741          160 :                       insert = true;
    4742          160 :                       *entry = bound_index;
    4743              :                     }
    4744              : 
    4745        36073 :                   if (insert)
    4746        36073 :                     queues[bound_index].safe_push (e->dest);
    4747              :                 }
    4748              :             }
    4749              :         }
    4750        10348 :       queues[queue_index].release ();
    4751              :     }
    4752              : 
    4753         3674 :   gcc_assert (latch_index >= 0);
    4754         3674 :   if ((unsigned)latch_index < bounds.length ())
    4755              :     {
    4756          241 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4757              :         {
    4758            0 :           fprintf (dump_file, "Found better loop bound ");
    4759            0 :           print_decu (bounds[latch_index], dump_file);
    4760            0 :           fprintf (dump_file, "\n");
    4761              :         }
    4762          241 :       record_niter_bound (loop, widest_int::from (bounds[latch_index],
    4763              :                                                   SIGNED), false, true);
    4764              :     }
    4765              : 
    4766         3674 :   queues.release ();
    4767      6505237 : }
    4768              : 
    4769              : /* See if every path cross the loop goes through a statement that is known
    4770              :    to not execute at the last iteration. In that case we can decrease iteration
    4771              :    count by 1.  */
    4772              : 
    4773              : static void
    4774      6505237 : maybe_lower_iteration_bound (class loop *loop)
    4775              : {
    4776      6505237 :   hash_set<gimple *> *not_executed_last_iteration = NULL;
    4777      6505237 :   class nb_iter_bound *elt;
    4778      6505237 :   bool found_exit = false;
    4779      6505237 :   auto_vec<basic_block> queue;
    4780      6505237 :   bitmap visited;
    4781              : 
    4782              :   /* Collect all statements with interesting (i.e. lower than
    4783              :      nb_iterations_upper_bound) bound on them.
    4784              : 
    4785              :      TODO: Due to the way record_estimate choose estimates to store, the bounds
    4786              :      will be always nb_iterations_upper_bound-1.  We can change this to record
    4787              :      also statements not dominating the loop latch and update the walk below
    4788              :      to the shortest path algorithm.  */
    4789     16900479 :   for (elt = loop->bounds; elt; elt = elt->next)
    4790              :     {
    4791     10395242 :       if (!elt->is_exit
    4792     10395242 :           && wi::ltu_p (elt->bound, loop->nb_iterations_upper_bound))
    4793              :         {
    4794      2186989 :           if (!not_executed_last_iteration)
    4795      1185043 :             not_executed_last_iteration = new hash_set<gimple *>;
    4796      2186989 :           not_executed_last_iteration->add (elt->stmt);
    4797              :         }
    4798              :     }
    4799      6505237 :   if (!not_executed_last_iteration)
    4800      5320194 :     return;
    4801              : 
    4802              :   /* Start DFS walk in the loop header and see if we can reach the
    4803              :      loop latch or any of the exits (including statements with side
    4804              :      effects that may terminate the loop otherwise) without visiting
    4805              :      any of the statements known to have undefined effect on the last
    4806              :      iteration.  */
    4807      1185043 :   queue.safe_push (loop->header);
    4808      1185043 :   visited = BITMAP_ALLOC (NULL);
    4809      1185043 :   bitmap_set_bit (visited, loop->header->index);
    4810      1185043 :   found_exit = false;
    4811              : 
    4812      1264639 :   do
    4813              :     {
    4814      1264639 :       basic_block bb = queue.pop ();
    4815      1264639 :       gimple_stmt_iterator gsi;
    4816      1264639 :       bool stmt_found = false;
    4817              : 
    4818              :       /* Loop for possible exits and statements bounding the execution.  */
    4819      6403540 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    4820              :         {
    4821      3935533 :           gimple *stmt = gsi_stmt (gsi);
    4822      3935533 :           if (not_executed_last_iteration->contains (stmt))
    4823              :             {
    4824              :               stmt_found = true;
    4825        61271 :               break;
    4826              :             }
    4827      3895894 :           if (gimple_has_side_effects (stmt))
    4828              :             {
    4829              :               found_exit = true;
    4830              :               break;
    4831              :             }
    4832              :         }
    4833      1264639 :       if (found_exit)
    4834              :         break;
    4835              : 
    4836              :       /* If no bounding statement is found, continue the walk.  */
    4837      1243007 :       if (!stmt_found)
    4838              :         {
    4839      1203368 :           edge e;
    4840      1203368 :           edge_iterator ei;
    4841              : 
    4842      2019500 :           FOR_EACH_EDGE (e, ei, bb->succs)
    4843              :             {
    4844      1945291 :               if (loop_exit_edge_p (loop, e)
    4845       816769 :                   || e == loop_latch_edge (loop)
    4846              :                   /* When exiting an inner loop, verify it is finite.  */
    4847       816733 :                   || (!flow_bb_inside_loop_p (bb->loop_father, e->dest)
    4848         7553 :                       && !finite_loop_p (bb->loop_father))
    4849              :                   /* When we enter an irreducible region and the entry
    4850              :                      does not contain a bounding stmt assume it might be
    4851              :                      infinite.  */
    4852      2761423 :                   || (bb->flags & BB_IRREDUCIBLE_LOOP))
    4853              :                 {
    4854              :                   found_exit = true;
    4855              :                   break;
    4856              :                 }
    4857       816132 :               if (bitmap_set_bit (visited, e->dest->index))
    4858       799210 :                 queue.safe_push (e->dest);
    4859              :             }
    4860              :         }
    4861              :     }
    4862      1243007 :   while (queue.length () && !found_exit);
    4863              : 
    4864              :   /* If every path through the loop reach bounding statement before exit,
    4865              :      then we know the last iteration of the loop will have undefined effect
    4866              :      and we can decrease number of iterations.  */
    4867              : 
    4868      1185043 :   if (!found_exit)
    4869              :     {
    4870        34252 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4871           68 :         fprintf (dump_file, "Reducing loop iteration estimate by 1; "
    4872              :                  "undefined statement must be executed at the last iteration.\n");
    4873        68504 :       record_niter_bound (loop, widest_int::from (loop->nb_iterations_upper_bound,
    4874       102756 :                                                   SIGNED) - 1,
    4875              :                           false, true);
    4876              :     }
    4877              : 
    4878      1185043 :   BITMAP_FREE (visited);
    4879      1185043 :   delete not_executed_last_iteration;
    4880      6505237 : }
    4881              : 
    4882              : /* Get expected upper bound for number of loop iterations for
    4883              :    BUILT_IN_EXPECT_WITH_PROBABILITY for a condition COND.  */
    4884              : 
    4885              : static tree
    4886      5054676 : get_upper_bound_based_on_builtin_expr_with_prob (gcond *cond)
    4887              : {
    4888      5054676 :   if (cond == NULL)
    4889              :     return NULL_TREE;
    4890              : 
    4891      4897987 :   tree lhs = gimple_cond_lhs (cond);
    4892      4897987 :   if (TREE_CODE (lhs) != SSA_NAME)
    4893              :     return NULL_TREE;
    4894              : 
    4895      4897913 :   gimple *stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (cond));
    4896      5240913 :   gcall *def = dyn_cast<gcall *> (stmt);
    4897       186239 :   if (def == NULL)
    4898              :     return NULL_TREE;
    4899              : 
    4900       186239 :   tree decl = gimple_call_fndecl (def);
    4901       186239 :   if (!decl
    4902       158009 :       || !fndecl_built_in_p (decl, BUILT_IN_EXPECT_WITH_PROBABILITY)
    4903       186243 :       || gimple_call_num_args (stmt) != 3)
    4904              :     return NULL_TREE;
    4905              : 
    4906            4 :   tree c = gimple_call_arg (def, 1);
    4907            4 :   tree condt = TREE_TYPE (lhs);
    4908            4 :   tree res = fold_build2 (gimple_cond_code (cond),
    4909              :                           condt, c,
    4910              :                           gimple_cond_rhs (cond));
    4911            4 :   if (TREE_CODE (res) != INTEGER_CST)
    4912              :     return NULL_TREE;
    4913              : 
    4914              : 
    4915            4 :   tree prob = gimple_call_arg (def, 2);
    4916            4 :   tree t = TREE_TYPE (prob);
    4917            4 :   tree one
    4918            8 :     = build_real_from_int_cst (t,
    4919            4 :                                integer_one_node);
    4920            4 :   if (integer_zerop (res))
    4921            0 :     prob = fold_build2 (MINUS_EXPR, t, one, prob);
    4922            4 :   tree r = fold_build2 (RDIV_EXPR, t, one, prob);
    4923            4 :   if (TREE_CODE (r) != REAL_CST)
    4924              :     return NULL_TREE;
    4925              : 
    4926            2 :   HOST_WIDE_INT probi
    4927            2 :     = real_to_integer (TREE_REAL_CST_PTR (r));
    4928            2 :   return build_int_cst (condt, probi);
    4929              : }
    4930              : 
    4931              : /* Records estimates on numbers of iterations of LOOP.  If USE_UNDEFINED_P
    4932              :    is true also use estimates derived from undefined behavior.  */
    4933              : 
    4934              : void
    4935     26913431 : estimate_numbers_of_iterations (class loop *loop)
    4936              : {
    4937     26913431 :   tree niter, type;
    4938     26913431 :   unsigned i;
    4939     26913431 :   class tree_niter_desc niter_desc;
    4940     26913431 :   edge ex;
    4941     26913431 :   widest_int bound;
    4942     26913431 :   edge likely_exit;
    4943              : 
    4944              :   /* Give up if we already have tried to compute an estimation.  */
    4945     26913431 :   if (loop->estimate_state != EST_NOT_COMPUTED)
    4946     20408194 :     return;
    4947              : 
    4948      6505237 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4949        13796 :     fprintf (dump_file, "Estimating # of iterations of loop %d\n", loop->num);
    4950              : 
    4951      6505237 :   loop->estimate_state = EST_AVAILABLE;
    4952              : 
    4953      6505237 :   sreal nit;
    4954      6505237 :   bool reliable;
    4955              : 
    4956              :   /* If we have a measured profile, use it to estimate the number of
    4957              :      iterations.  Normally this is recorded by branch_prob right after
    4958              :      reading the profile.  In case we however found a new loop, record the
    4959              :      information here.
    4960              : 
    4961              :      Explicitly check for profile status so we do not report
    4962              :      wrong prediction hitrates for guessed loop iterations heuristics.
    4963              :      Do not recompute already recorded bounds - we ought to be better on
    4964              :      updating iteration bounds than updating profile in general and thus
    4965              :      recomputing iteration bounds later in the compilation process will just
    4966              :      introduce random roundoff errors.  */
    4967      6505237 :   if (!loop->any_estimate
    4968      4440937 :       && expected_loop_iterations_by_profile (loop, &nit, &reliable)
    4969     10062370 :       && reliable)
    4970              :     {
    4971           11 :       bound = nit.to_nearest_int ();
    4972           11 :       record_niter_bound (loop, bound, true, false);
    4973              :     }
    4974              : 
    4975              :   /* Ensure that loop->nb_iterations is computed if possible.  If it turns out
    4976              :      to be constant, we avoid undefined behavior implied bounds and instead
    4977              :      diagnose those loops with -Waggressive-loop-optimizations.  */
    4978      6505237 :   number_of_latch_executions (loop);
    4979              : 
    4980      6505237 :   basic_block *body = get_loop_body (loop);
    4981      6505237 :   auto_vec<edge> exits = get_loop_exit_edges (loop, body);
    4982      6505237 :   likely_exit = single_likely_exit (loop, exits);
    4983     18143831 :   FOR_EACH_VEC_ELT (exits, i, ex)
    4984              :     {
    4985     11638594 :       if (ex == likely_exit)
    4986              :         {
    4987      5054676 :           gimple *stmt = *gsi_last_bb (ex->src);
    4988      5054676 :           if (stmt != NULL)
    4989              :             {
    4990      5054676 :               gcond *cond = dyn_cast<gcond *> (stmt);
    4991      5054676 :               tree niter_bound
    4992      5054676 :                 = get_upper_bound_based_on_builtin_expr_with_prob (cond);
    4993      5054676 :               if (niter_bound != NULL_TREE)
    4994              :                 {
    4995            2 :                   widest_int max = derive_constant_upper_bound (niter_bound);
    4996            2 :                   record_estimate (loop, niter_bound, max, cond,
    4997              :                                    true, true, false);
    4998            2 :                 }
    4999              :             }
    5000              :         }
    5001              : 
    5002     11638594 :       if (!number_of_iterations_exit (loop, ex, &niter_desc,
    5003              :                                       false, false, body))
    5004      6906139 :         continue;
    5005              : 
    5006      4732455 :       niter = niter_desc.niter;
    5007      4732455 :       type = TREE_TYPE (niter);
    5008      4732455 :       if (TREE_CODE (niter_desc.may_be_zero) != INTEGER_CST)
    5009       735238 :         niter = build3 (COND_EXPR, type, niter_desc.may_be_zero,
    5010              :                         build_int_cst (type, 0),
    5011              :                         niter);
    5012      4732455 :       record_estimate (loop, niter, niter_desc.max,
    5013              :                        last_nondebug_stmt (ex->src),
    5014              :                        true, ex == likely_exit, true);
    5015      4732455 :       record_control_iv (loop, &niter_desc);
    5016              :     }
    5017              : 
    5018      6505237 :   if (flag_aggressive_loop_optimizations)
    5019      6465790 :     infer_loop_bounds_from_undefined (loop, body);
    5020      6505237 :   free (body);
    5021              : 
    5022      6505237 :   discover_iteration_bound_by_body_walk (loop);
    5023              : 
    5024      6505237 :   maybe_lower_iteration_bound (loop);
    5025              : 
    5026              :   /* If we know the exact number of iterations of this loop, try to
    5027              :      not break code with undefined behavior by not recording smaller
    5028              :      maximum number of iterations.  */
    5029      6505237 :   if (loop->nb_iterations
    5030      6505237 :       && TREE_CODE (loop->nb_iterations) == INTEGER_CST
    5031     13010474 :       && (wi::min_precision (wi::to_widest (loop->nb_iterations), SIGNED)
    5032      2000839 :           <= bound_wide_int ().get_precision ()))
    5033              :     {
    5034      2000839 :       loop->any_upper_bound = true;
    5035      2000839 :       loop->nb_iterations_upper_bound
    5036      2000839 :         = bound_wide_int::from (wi::to_widest (loop->nb_iterations), SIGNED);
    5037              :     }
    5038     26913445 : }
    5039              : 
    5040              : /* Sets NIT to the estimated number of executions of the latch of the
    5041              :    LOOP.  If CONSERVATIVE is true, we must be sure that NIT is at least as
    5042              :    large as the number of iterations.  If we have no reliable estimate,
    5043              :    the function returns false, otherwise returns true.  */
    5044              : 
    5045              : bool
    5046      9883585 : estimated_loop_iterations (class loop *loop, widest_int *nit)
    5047              : {
    5048              :   /* When SCEV information is available, try to update loop iterations
    5049              :      estimate.  Otherwise just return whatever we recorded earlier.  */
    5050      9883585 :   if (scev_initialized_p ())
    5051      9883585 :     estimate_numbers_of_iterations (loop);
    5052              : 
    5053      9883585 :   return (get_estimated_loop_iterations (loop, nit));
    5054              : }
    5055              : 
    5056              : /* Similar to estimated_loop_iterations, but returns the estimate only
    5057              :    if it fits to HOST_WIDE_INT.  If this is not the case, or the estimate
    5058              :    on the number of iterations of LOOP could not be derived, returns -1.  */
    5059              : 
    5060              : HOST_WIDE_INT
    5061      9621978 : estimated_loop_iterations_int (class loop *loop)
    5062              : {
    5063      9621978 :   widest_int nit;
    5064      9621978 :   HOST_WIDE_INT hwi_nit;
    5065              : 
    5066      9621978 :   if (!estimated_loop_iterations (loop, &nit))
    5067              :     return -1;
    5068              : 
    5069      3989545 :   if (!wi::fits_shwi_p (nit))
    5070              :     return -1;
    5071      3989539 :   hwi_nit = nit.to_shwi ();
    5072              : 
    5073      3989539 :   return hwi_nit < 0 ? -1 : hwi_nit;
    5074      9621978 : }
    5075              : 
    5076              : 
    5077              : /* Sets NIT to an upper bound for the maximum number of executions of the
    5078              :    latch of the LOOP.  If we have no reliable estimate, the function returns
    5079              :    false, otherwise returns true.  */
    5080              : 
    5081              : bool
    5082      8839331 : max_loop_iterations (class loop *loop, widest_int *nit)
    5083              : {
    5084              :   /* When SCEV information is available, try to update loop iterations
    5085              :      estimate.  Otherwise just return whatever we recorded earlier.  */
    5086      8839331 :   if (scev_initialized_p ())
    5087      8826704 :     estimate_numbers_of_iterations (loop);
    5088              : 
    5089      8839331 :   return get_max_loop_iterations (loop, nit);
    5090              : }
    5091              : 
    5092              : /* Similar to max_loop_iterations, but returns the estimate only
    5093              :    if it fits to HOST_WIDE_INT.  If this is not the case, or the estimate
    5094              :    on the number of iterations of LOOP could not be derived, returns -1.  */
    5095              : 
    5096              : HOST_WIDE_INT
    5097      2244808 : max_loop_iterations_int (class loop *loop)
    5098              : {
    5099      2244808 :   widest_int nit;
    5100      2244808 :   HOST_WIDE_INT hwi_nit;
    5101              : 
    5102      2244808 :   if (!max_loop_iterations (loop, &nit))
    5103              :     return -1;
    5104              : 
    5105      1702009 :   if (!wi::fits_shwi_p (nit))
    5106              :     return -1;
    5107      1632422 :   hwi_nit = nit.to_shwi ();
    5108              : 
    5109      1632422 :   return hwi_nit < 0 ? -1 : hwi_nit;
    5110      2244808 : }
    5111              : 
    5112              : /* Sets NIT to an likely upper bound for the maximum number of executions of the
    5113              :    latch of the LOOP.  If we have no reliable estimate, the function returns
    5114              :    false, otherwise returns true.  */
    5115              : 
    5116              : bool
    5117       362699 : likely_max_loop_iterations (class loop *loop, widest_int *nit)
    5118              : {
    5119              :   /* When SCEV information is available, try to update loop iterations
    5120              :      estimate.  Otherwise just return whatever we recorded earlier.  */
    5121       362699 :   if (scev_initialized_p ())
    5122       362699 :     estimate_numbers_of_iterations (loop);
    5123              : 
    5124       362699 :   return get_likely_max_loop_iterations (loop, nit);
    5125              : }
    5126              : 
    5127              : /* Similar to max_loop_iterations, but returns the estimate only
    5128              :    if it fits to HOST_WIDE_INT.  If this is not the case, or the estimate
    5129              :    on the number of iterations of LOOP could not be derived, returns -1.  */
    5130              : 
    5131              : HOST_WIDE_INT
    5132       110143 : likely_max_loop_iterations_int (class loop *loop)
    5133              : {
    5134       110143 :   widest_int nit;
    5135       110143 :   HOST_WIDE_INT hwi_nit;
    5136              : 
    5137       110143 :   if (!likely_max_loop_iterations (loop, &nit))
    5138              :     return -1;
    5139              : 
    5140        89905 :   if (!wi::fits_shwi_p (nit))
    5141              :     return -1;
    5142        82232 :   hwi_nit = nit.to_shwi ();
    5143              : 
    5144        82232 :   return hwi_nit < 0 ? -1 : hwi_nit;
    5145       110143 : }
    5146              : 
    5147              : /* Returns an estimate for the number of executions of statements
    5148              :    in the LOOP.  For statements before the loop exit, this exceeds
    5149              :    the number of execution of the latch by one.  */
    5150              : 
    5151              : HOST_WIDE_INT
    5152      9448243 : estimated_stmt_executions_int (class loop *loop)
    5153              : {
    5154      9448243 :   HOST_WIDE_INT nit = estimated_loop_iterations_int (loop);
    5155      9448243 :   HOST_WIDE_INT snit;
    5156              : 
    5157      9448243 :   if (nit == -1)
    5158              :     return -1;
    5159              : 
    5160      3926113 :   snit = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) nit + 1);
    5161              : 
    5162              :   /* If the computation overflows, return -1.  */
    5163      3926113 :   return snit < 0 ? -1 : snit;
    5164              : }
    5165              : 
    5166              : /* Sets NIT to the maximum number of executions of the latch of the
    5167              :    LOOP, plus one.  If we have no reliable estimate, the function returns
    5168              :    false, otherwise returns true.  */
    5169              : 
    5170              : bool
    5171          154 : max_stmt_executions (class loop *loop, widest_int *nit)
    5172              : {
    5173          154 :   widest_int nit_minus_one;
    5174              : 
    5175          154 :   if (!max_loop_iterations (loop, nit))
    5176              :     return false;
    5177              : 
    5178          154 :   nit_minus_one = *nit;
    5179              : 
    5180          154 :   *nit += 1;
    5181              : 
    5182          154 :   return wi::gtu_p (*nit, nit_minus_one);
    5183          154 : }
    5184              : 
    5185              : /* Sets NIT to the estimated maximum number of executions of the latch of the
    5186              :    LOOP, plus one.  If we have no likely estimate, the function returns
    5187              :    false, otherwise returns true.  */
    5188              : 
    5189              : bool
    5190       252556 : likely_max_stmt_executions (class loop *loop, widest_int *nit)
    5191              : {
    5192       252556 :   widest_int nit_minus_one;
    5193              : 
    5194       252556 :   if (!likely_max_loop_iterations (loop, nit))
    5195              :     return false;
    5196              : 
    5197       143295 :   nit_minus_one = *nit;
    5198              : 
    5199       143295 :   *nit += 1;
    5200              : 
    5201       143295 :   return wi::gtu_p (*nit, nit_minus_one);
    5202       252556 : }
    5203              : 
    5204              : /* Sets NIT to the estimated number of executions of the latch of the
    5205              :    LOOP, plus one.  If we have no reliable estimate, the function returns
    5206              :    false, otherwise returns true.  */
    5207              : 
    5208              : bool
    5209       261607 : estimated_stmt_executions (class loop *loop, widest_int *nit)
    5210              : {
    5211       261607 :   widest_int nit_minus_one;
    5212              : 
    5213       261607 :   if (!estimated_loop_iterations (loop, nit))
    5214              :     return false;
    5215              : 
    5216         7744 :   nit_minus_one = *nit;
    5217              : 
    5218         7744 :   *nit += 1;
    5219              : 
    5220         7744 :   return wi::gtu_p (*nit, nit_minus_one);
    5221       261607 : }
    5222              : 
    5223              : /* Records estimates on numbers of iterations of loops.  */
    5224              : 
    5225              : void
    5226      1220486 : estimate_numbers_of_iterations (function *fn)
    5227              : {
    5228      7345759 :   for (auto loop : loops_list (fn, 0))
    5229      3684301 :     estimate_numbers_of_iterations (loop);
    5230      1220486 : }
    5231              : 
    5232              : /* Returns true if statement S1 dominates statement S2.  */
    5233              : 
    5234              : bool
    5235      2298056 : stmt_dominates_stmt_p (gimple *s1, gimple *s2)
    5236              : {
    5237      2298056 :   basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2);
    5238              : 
    5239      2298056 :   if (!bb1
    5240      2298056 :       || s1 == s2)
    5241              :     return true;
    5242              : 
    5243      2290020 :   if (bb1 == bb2)
    5244              :     {
    5245       839972 :       gimple_stmt_iterator bsi;
    5246              : 
    5247       839972 :       if (gimple_code (s2) == GIMPLE_PHI)
    5248              :         return false;
    5249              : 
    5250       537513 :       if (gimple_code (s1) == GIMPLE_PHI)
    5251              :         return true;
    5252              : 
    5253     19524307 :       for (bsi = gsi_start_bb (bb1); gsi_stmt (bsi) != s2; gsi_next (&bsi))
    5254     18711723 :         if (gsi_stmt (bsi) == s1)
    5255              :           return true;
    5256              : 
    5257              :       return false;
    5258              :     }
    5259              : 
    5260      1450048 :   return dominated_by_p (CDI_DOMINATORS, bb2, bb1);
    5261              : }
    5262              : 
    5263              : /* Returns true when we can prove that the number of executions of
    5264              :    STMT in the loop is at most NITER, according to the bound on
    5265              :    the number of executions of the statement NITER_BOUND->stmt recorded in
    5266              :    NITER_BOUND and fact that NITER_BOUND->stmt dominate STMT.
    5267              : 
    5268              :    ??? This code can become quite a CPU hog - we can have many bounds,
    5269              :    and large basic block forcing stmt_dominates_stmt_p to be queried
    5270              :    many times on a large basic blocks, so the whole thing is O(n^2)
    5271              :    for scev_probably_wraps_p invocation (that can be done n times).
    5272              : 
    5273              :    It would make more sense (and give better answers) to remember BB
    5274              :    bounds computed by discover_iteration_bound_by_body_walk.  */
    5275              : 
    5276              : static bool
    5277      2454207 : n_of_executions_at_most (gimple *stmt,
    5278              :                          class nb_iter_bound *niter_bound,
    5279              :                          tree niter)
    5280              : {
    5281      2454207 :   widest_int bound = widest_int::from (niter_bound->bound, SIGNED);
    5282      2454207 :   tree nit_type = TREE_TYPE (niter), e;
    5283      2454207 :   enum tree_code cmp;
    5284              : 
    5285      2454207 :   gcc_assert (TYPE_UNSIGNED (nit_type));
    5286              : 
    5287              :   /* If the bound does not even fit into NIT_TYPE, it cannot tell us that
    5288              :      the number of iterations is small.  */
    5289      2454207 :   if (!wi::fits_to_tree_p (bound, nit_type))
    5290              :     return false;
    5291              : 
    5292              :   /* We know that NITER_BOUND->stmt is executed at most NITER_BOUND->bound + 1
    5293              :      times.  This means that:
    5294              : 
    5295              :      -- if NITER_BOUND->is_exit is true, then everything after
    5296              :         it at most NITER_BOUND->bound times.
    5297              : 
    5298              :      -- If NITER_BOUND->is_exit is false, then if we can prove that when STMT
    5299              :         is executed, then NITER_BOUND->stmt is executed as well in the same
    5300              :         iteration then STMT is executed at most NITER_BOUND->bound + 1 times.
    5301              : 
    5302              :         If we can determine that NITER_BOUND->stmt is always executed
    5303              :         after STMT, then STMT is executed at most NITER_BOUND->bound + 2 times.
    5304              :         We conclude that if both statements belong to the same
    5305              :         basic block and STMT is before NITER_BOUND->stmt and there are no
    5306              :         statements with side effects in between.  */
    5307              : 
    5308      2298056 :   if (niter_bound->is_exit)
    5309              :     {
    5310       861054 :       if (stmt == niter_bound->stmt
    5311       861054 :           || !stmt_dominates_stmt_p (niter_bound->stmt, stmt))
    5312       746518 :         return false;
    5313              :       cmp = GE_EXPR;
    5314              :     }
    5315              :   else
    5316              :     {
    5317      1437002 :       if (!stmt_dominates_stmt_p (niter_bound->stmt, stmt))
    5318              :         {
    5319       586701 :           gimple_stmt_iterator bsi;
    5320       586701 :           if (gimple_bb (stmt) != gimple_bb (niter_bound->stmt)
    5321       248277 :               || gimple_code (stmt) == GIMPLE_PHI
    5322       770540 :               || gimple_code (niter_bound->stmt) == GIMPLE_PHI)
    5323       408243 :             return false;
    5324              : 
    5325              :           /* By stmt_dominates_stmt_p we already know that STMT appears
    5326              :              before NITER_BOUND->STMT.  Still need to test that the loop
    5327              :              cannot be terinated by a side effect in between.  */
    5328      7985706 :           for (bsi = gsi_for_stmt (stmt); gsi_stmt (bsi) != niter_bound->stmt;
    5329      7801867 :                gsi_next (&bsi))
    5330      7802778 :             if (gimple_has_side_effects (gsi_stmt (bsi)))
    5331              :                return false;
    5332       182928 :           bound += 1;
    5333       361386 :           if (bound == 0
    5334       182928 :               || !wi::fits_to_tree_p (bound, nit_type))
    5335         4470 :             return false;
    5336              :         }
    5337              :       cmp = GT_EXPR;
    5338              :     }
    5339              : 
    5340      1143295 :   e = fold_binary (cmp, boolean_type_node,
    5341              :                    niter, wide_int_to_tree (nit_type, bound));
    5342      1143295 :   return e && integer_nonzerop (e);
    5343      2454207 : }
    5344              : 
    5345              : /* Returns true if the arithmetics in TYPE can be assumed not to wrap.  */
    5346              : 
    5347              : bool
    5348     86391227 : nowrap_type_p (tree type)
    5349              : {
    5350     11695704 :   if (ANY_INTEGRAL_TYPE_P (type)
    5351     86391227 :       && TYPE_OVERFLOW_UNDEFINED (type))
    5352              :     return true;
    5353              : 
    5354     42995502 :   if (POINTER_TYPE_P (type))
    5355     11695704 :     return true;
    5356              : 
    5357              :   return false;
    5358              : }
    5359              : 
    5360              : /* Return true if we can prove LOOP is exited before evolution of induction
    5361              :    variable {BASE, STEP} overflows with respect to its type bound.  */
    5362              : 
    5363              : static bool
    5364      3437115 : loop_exits_before_overflow (tree base, tree step,
    5365              :                             gimple *at_stmt, class loop *loop)
    5366              : {
    5367      3437115 :   widest_int niter;
    5368      3437115 :   struct control_iv *civ;
    5369      3437115 :   class nb_iter_bound *bound;
    5370      3437115 :   tree e, delta, step_abs, unsigned_base;
    5371      3437115 :   tree type = TREE_TYPE (step);
    5372      3437115 :   tree unsigned_type, valid_niter;
    5373              : 
    5374              :   /* Compute the number of iterations before we reach the bound of the
    5375              :      type, and verify that the loop is exited before this occurs.  */
    5376      3437115 :   unsigned_type = unsigned_type_for (type);
    5377      3437115 :   unsigned_base = fold_convert (unsigned_type, base);
    5378              : 
    5379      3437115 :   if (tree_int_cst_sign_bit (step))
    5380              :     {
    5381       490509 :       tree extreme = fold_convert (unsigned_type,
    5382              :                                    lower_bound_in_type (type, type));
    5383       490509 :       delta = fold_build2 (MINUS_EXPR, unsigned_type, unsigned_base, extreme);
    5384       490509 :       step_abs = fold_build1 (NEGATE_EXPR, unsigned_type,
    5385              :                               fold_convert (unsigned_type, step));
    5386              :     }
    5387              :   else
    5388              :     {
    5389      2946606 :       tree extreme = fold_convert (unsigned_type,
    5390              :                                    upper_bound_in_type (type, type));
    5391      2946606 :       delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, unsigned_base);
    5392      2946606 :       step_abs = fold_convert (unsigned_type, step);
    5393              :     }
    5394              : 
    5395      3437115 :   valid_niter = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step_abs);
    5396              : 
    5397      3437115 :   estimate_numbers_of_iterations (loop);
    5398              : 
    5399      3437115 :   if (max_loop_iterations (loop, &niter)
    5400      2926222 :       && wi::fits_to_tree_p (niter, TREE_TYPE (valid_niter))
    5401      2744145 :       && (e = fold_binary (GT_EXPR, boolean_type_node, valid_niter,
    5402              :                            wide_int_to_tree (TREE_TYPE (valid_niter),
    5403              :                                              niter))) != NULL
    5404      6098133 :       && integer_nonzerop (e))
    5405      1135776 :     return true;
    5406      2301339 :   if (at_stmt)
    5407      3774161 :     for (bound = loop->bounds; bound; bound = bound->next)
    5408              :       {
    5409      2454207 :         if (n_of_executions_at_most (at_stmt, bound, valid_niter))
    5410              :           return true;
    5411              :       }
    5412              : 
    5413              :   /* Try to prove loop is exited before {base, step} overflows with the
    5414              :      help of analyzed loop control IV.  This is done only for IVs with
    5415              :      constant step because otherwise we don't have the information.  */
    5416      2283001 :   if (TREE_CODE (step) == INTEGER_CST)
    5417              :     {
    5418      3420607 :       for (civ = loop->control_ivs; civ; civ = civ->next)
    5419              :         {
    5420      1322399 :           enum tree_code code;
    5421      1322399 :           tree civ_type = TREE_TYPE (civ->step);
    5422              : 
    5423              :           /* Have to consider type difference because operand_equal_p ignores
    5424              :              that for constants.  */
    5425      1322399 :           if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (civ_type)
    5426      1322399 :               || element_precision (type) != element_precision (civ_type))
    5427       770146 :             continue;
    5428              : 
    5429              :           /* Only consider control IV with same step.  */
    5430       552253 :           if (!operand_equal_p (step, civ->step, 0))
    5431       202767 :             continue;
    5432              : 
    5433              :           /* Done proving if this is a no-overflow control IV.  */
    5434       349486 :           if (operand_equal_p (base, civ->base, 0))
    5435              :             return true;
    5436              : 
    5437              :           /* Control IV is recorded after expanding simple operations,
    5438              :              Here we expand base and compare it too.  */
    5439       241504 :           tree expanded_base = expand_simple_operations (base);
    5440       241504 :           if (operand_equal_p (expanded_base, civ->base, 0))
    5441              :             return true;
    5442              : 
    5443              :           /* If this is a before stepping control IV, in other words, we have
    5444              : 
    5445              :                {civ_base, step} = {base + step, step}
    5446              : 
    5447              :              Because civ {base + step, step} doesn't overflow during loop
    5448              :              iterations, {base, step} will not overflow if we can prove the
    5449              :              operation "base + step" does not overflow.  Specifically, we try
    5450              :              to prove below conditions are satisfied:
    5451              : 
    5452              :                base <= UPPER_BOUND (type) - step  ;;step > 0
    5453              :                base >= LOWER_BOUND (type) - step  ;;step < 0
    5454              : 
    5455              :              by proving the reverse conditions are false using loop's initial
    5456              :              condition.  */
    5457       209149 :           if (POINTER_TYPE_P (TREE_TYPE (base)))
    5458              :             code = POINTER_PLUS_EXPR;
    5459              :           else
    5460              :             code = PLUS_EXPR;
    5461              : 
    5462       209149 :           tree stepped = fold_build2 (code, TREE_TYPE (base), base, step);
    5463       209149 :           tree expanded_stepped = fold_build2 (code, TREE_TYPE (base),
    5464              :                                                expanded_base, step);
    5465       209149 :           if (operand_equal_p (stepped, civ->base, 0)
    5466       209149 :               || operand_equal_p (expanded_stepped, civ->base, 0))
    5467              :             {
    5468        55073 :               tree extreme;
    5469              : 
    5470        55073 :               if (tree_int_cst_sign_bit (step))
    5471              :                 {
    5472        13821 :                   code = LT_EXPR;
    5473        13821 :                   extreme = lower_bound_in_type (type, type);
    5474              :                 }
    5475              :               else
    5476              :                 {
    5477        41252 :                   code = GT_EXPR;
    5478        41252 :                   extreme = upper_bound_in_type (type, type);
    5479              :                 }
    5480        55073 :               extreme = fold_build2 (MINUS_EXPR, type, extreme, step);
    5481        55073 :               e = fold_build2 (code, boolean_type_node, base, extreme);
    5482        55073 :               e = simplify_using_initial_conditions (loop, e);
    5483        55073 :               if (integer_zerop (e))
    5484              :                 return true;
    5485              :             }
    5486              :         }
    5487              :     }
    5488              : 
    5489              :   return false;
    5490      3437115 : }
    5491              : 
    5492              : /* VAR is scev variable whose evolution part is constant STEP, this function
    5493              :    proves that VAR can't overflow by using value range info.  If VAR's value
    5494              :    range is [MIN, MAX], it can be proven by:
    5495              :      MAX + step doesn't overflow    ; if step > 0
    5496              :    or
    5497              :      MIN + step doesn't underflow   ; if step < 0.
    5498              : 
    5499              :    We can only do this if var is computed in every loop iteration, i.e, var's
    5500              :    definition has to dominate loop latch.  Consider below example:
    5501              : 
    5502              :      {
    5503              :        unsigned int i;
    5504              : 
    5505              :        <bb 3>:
    5506              : 
    5507              :        <bb 4>:
    5508              :        # RANGE [0, 4294967294] NONZERO 65535
    5509              :        # i_21 = PHI <0(3), i_18(9)>
    5510              :        if (i_21 != 0)
    5511              :          goto <bb 6>;
    5512              :        else
    5513              :          goto <bb 8>;
    5514              : 
    5515              :        <bb 6>:
    5516              :        # RANGE [0, 65533] NONZERO 65535
    5517              :        _6 = i_21 + 4294967295;
    5518              :        # RANGE [0, 65533] NONZERO 65535
    5519              :        _7 = (long unsigned int) _6;
    5520              :        # RANGE [0, 524264] NONZERO 524280
    5521              :        _8 = _7 * 8;
    5522              :        # PT = nonlocal escaped
    5523              :        _9 = a_14 + _8;
    5524              :        *_9 = 0;
    5525              : 
    5526              :        <bb 8>:
    5527              :        # RANGE [1, 65535] NONZERO 65535
    5528              :        i_18 = i_21 + 1;
    5529              :        if (i_18 >= 65535)
    5530              :          goto <bb 10>;
    5531              :        else
    5532              :          goto <bb 9>;
    5533              : 
    5534              :        <bb 9>:
    5535              :        goto <bb 4>;
    5536              : 
    5537              :        <bb 10>:
    5538              :        return;
    5539              :      }
    5540              : 
    5541              :    VAR _6 doesn't overflow only with pre-condition (i_21 != 0), here we
    5542              :    can't use _6 to prove no-overflow for _7.  In fact, var _7 takes value
    5543              :    sequence (4294967295, 0, 1, ..., 65533) in loop life time, rather than
    5544              :    (4294967295, 4294967296, ...).  */
    5545              : 
    5546              : static bool
    5547       308902 : scev_var_range_cant_overflow (tree var, tree step, class loop *loop)
    5548              : {
    5549       308902 :   tree type;
    5550       308902 :   wide_int minv, maxv, diff, step_wi;
    5551              : 
    5552       308902 :   if (TREE_CODE (step) != INTEGER_CST || !INTEGRAL_TYPE_P (TREE_TYPE (var)))
    5553              :     return false;
    5554              : 
    5555              :   /* Check if VAR evaluates in every loop iteration.  It's not the case
    5556              :      if VAR is default definition or does not dominate loop's latch.  */
    5557       308902 :   basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (var));
    5558       308902 :   if (!def_bb || !dominated_by_p (CDI_DOMINATORS, loop->latch, def_bb))
    5559         2642 :     return false;
    5560              : 
    5561       306260 :   int_range_max r (TREE_TYPE (var));
    5562       612520 :   get_range_query (cfun)->range_of_expr (r, var);
    5563       306260 :   if (r.varying_p () || r.undefined_p ())
    5564              :     return false;
    5565              : 
    5566              :   /* VAR is a scev whose evolution part is STEP and value range info
    5567              :      is [MIN, MAX], we can prove its no-overflowness by conditions:
    5568              : 
    5569              :        type_MAX - MAX >= step   ; if step > 0
    5570              :        MIN - type_MIN >= |step| ; if step < 0.
    5571              : 
    5572              :      Or VAR must take value outside of value range, which is not true.  */
    5573       114489 :   step_wi = wi::to_wide (step);
    5574       114489 :   type = TREE_TYPE (var);
    5575       114489 :   if (tree_int_cst_sign_bit (step))
    5576              :     {
    5577         6841 :       diff = r.lower_bound () - wi::to_wide (lower_bound_in_type (type, type));
    5578         6841 :       step_wi = - step_wi;
    5579              :     }
    5580              :   else
    5581       107648 :     diff = wi::to_wide (upper_bound_in_type (type, type)) - r.upper_bound ();
    5582              : 
    5583       114489 :   return (wi::geu_p (diff, step_wi));
    5584       615162 : }
    5585              : 
    5586              : /* Return false only when the induction variable BASE + STEP * I is
    5587              :    known to not overflow: i.e. when the number of iterations is small
    5588              :    enough with respect to the step and initial condition in order to
    5589              :    keep the evolution confined in TYPEs bounds.  Return true when the
    5590              :    iv is known to overflow or when the property is not computable.
    5591              : 
    5592              :    USE_OVERFLOW_SEMANTICS is true if this function should assume that
    5593              :    the rules for overflow of the given language apply (e.g., that signed
    5594              :    arithmetics in C does not overflow).
    5595              : 
    5596              :    If VAR is a ssa variable, this function also returns false if VAR can
    5597              :    be proven not overflow with value range info.  */
    5598              : 
    5599              : bool
    5600      7802614 : scev_probably_wraps_p (tree var, tree base, tree step,
    5601              :                        gimple *at_stmt, class loop *loop,
    5602              :                        bool use_overflow_semantics)
    5603              : {
    5604              :   /* FIXME: We really need something like
    5605              :      http://gcc.gnu.org/ml/gcc-patches/2005-06/msg02025.html.
    5606              : 
    5607              :      We used to test for the following situation that frequently appears
    5608              :      during address arithmetics:
    5609              : 
    5610              :        D.1621_13 = (long unsigned intD.4) D.1620_12;
    5611              :        D.1622_14 = D.1621_13 * 8;
    5612              :        D.1623_15 = (doubleD.29 *) D.1622_14;
    5613              : 
    5614              :      And derived that the sequence corresponding to D_14
    5615              :      can be proved to not wrap because it is used for computing a
    5616              :      memory access; however, this is not really the case -- for example,
    5617              :      if D_12 = (unsigned char) [254,+,1], then D_14 has values
    5618              :      2032, 2040, 0, 8, ..., but the code is still legal.  */
    5619              : 
    5620      7802614 :   if (chrec_contains_undetermined (base)
    5621      7802614 :       || chrec_contains_undetermined (step))
    5622            0 :     return true;
    5623              : 
    5624      7802614 :   if (integer_zerop (step))
    5625              :     return false;
    5626              : 
    5627              :   /* If we can use the fact that signed and pointer arithmetics does not
    5628              :      wrap, we are done.  */
    5629      7802614 :   if (use_overflow_semantics && nowrap_type_p (TREE_TYPE (base)))
    5630              :     return false;
    5631              : 
    5632              :   /* To be able to use estimates on number of iterations of the loop,
    5633              :      we must have an upper bound on the absolute value of the step.  */
    5634      3986701 :   if (TREE_CODE (step) != INTEGER_CST)
    5635              :     return true;
    5636              : 
    5637              :   /* Check if var can be proven not overflow with value range info.  */
    5638       310582 :   if (var && TREE_CODE (var) == SSA_NAME
    5639      3848573 :       && scev_var_range_cant_overflow (var, step, loop))
    5640              :     return false;
    5641              : 
    5642      3437115 :   if (loop_exits_before_overflow (base, step, at_stmt, loop))
    5643              :     return false;
    5644              : 
    5645              :   /* Check the nonwrapping flag, which may be set by niter analysis (e.g., the
    5646              :      above loop exits before overflow).  */
    5647      2098208 :   if (var && nonwrapping_chrec_p (analyze_scalar_evolution (loop, var)))
    5648              :     return false;
    5649              : 
    5650              :   /* At this point we still don't have a proof that the iv does not
    5651              :      overflow: give up.  */
    5652              :   return true;
    5653              : }
    5654              : 
    5655              : /* Frees the information on upper bounds on numbers of iterations of LOOP.  */
    5656              : 
    5657              : void
    5658     55191644 : free_numbers_of_iterations_estimates (class loop *loop)
    5659              : {
    5660     55191644 :   struct control_iv *civ;
    5661     55191644 :   class nb_iter_bound *bound;
    5662              : 
    5663     55191644 :   loop->nb_iterations = NULL;
    5664     55191644 :   loop->estimate_state = EST_NOT_COMPUTED;
    5665     65300967 :   for (bound = loop->bounds; bound;)
    5666              :     {
    5667     10109323 :       class nb_iter_bound *next = bound->next;
    5668     10109323 :       ggc_free (bound);
    5669     10109323 :       bound = next;
    5670              :     }
    5671     55191644 :   loop->bounds = NULL;
    5672              : 
    5673     59630602 :   for (civ = loop->control_ivs; civ;)
    5674              :     {
    5675      4438958 :       struct control_iv *next = civ->next;
    5676      4438958 :       ggc_free (civ);
    5677      4438958 :       civ = next;
    5678              :     }
    5679     55191644 :   loop->control_ivs = NULL;
    5680     55191644 : }
    5681              : 
    5682              : /* Frees the information on upper bounds on numbers of iterations of loops.  */
    5683              : 
    5684              : void
    5685     71322753 : free_numbers_of_iterations_estimates (function *fn)
    5686              : {
    5687    268499176 :   for (auto loop : loops_list (fn, 0))
    5688     54530917 :     free_numbers_of_iterations_estimates (loop);
    5689     71322753 : }
    5690              : 
    5691              : /* Substitute value VAL for ssa name NAME inside expressions held
    5692              :    at LOOP.  */
    5693              : 
    5694              : void
    5695    123166694 : substitute_in_loop_info (class loop *loop, tree name, tree val)
    5696              : {
    5697    123166694 :   loop->nb_iterations = simplify_replace_tree (loop->nb_iterations, name, val);
    5698    123166694 : }
        

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.