LCOV - code coverage report
Current view: top level - gcc - loop-iv.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 90.6 % 1393 1262
Test Date: 2025-06-21 16:26:05 Functions: 98.0 % 51 50
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Rtl-level induction variable analysis.
       2                 :             :    Copyright (C) 2004-2025 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                 :             : /* This is a simple analysis of induction variables of the loop.  The major use
      21                 :             :    is for determining the number of iterations of a loop for loop unrolling,
      22                 :             :    doloop optimization and branch prediction.  The iv information is computed
      23                 :             :    on demand.
      24                 :             : 
      25                 :             :    Induction variables are analyzed by walking the use-def chains.  When
      26                 :             :    a basic induction variable (biv) is found, it is cached in the bivs
      27                 :             :    hash table.  When register is proved to be a biv, its description
      28                 :             :    is stored to DF_REF_DATA of the def reference.
      29                 :             : 
      30                 :             :    The analysis works always with one loop -- you must call
      31                 :             :    iv_analysis_loop_init (loop) for it.  All the other functions then work with
      32                 :             :    this loop.   When you need to work with another loop, just call
      33                 :             :    iv_analysis_loop_init for it.  When you no longer need iv analysis, call
      34                 :             :    iv_analysis_done () to clean up the memory.
      35                 :             : 
      36                 :             :    The available functions are:
      37                 :             : 
      38                 :             :    iv_analyze (insn, mode, reg, iv): Stores the description of the induction
      39                 :             :      variable corresponding to the use of register REG in INSN to IV, given
      40                 :             :      that REG has mode MODE.  Returns true if REG is an induction variable
      41                 :             :      in INSN. false otherwise.  If a use of REG is not found in INSN,
      42                 :             :      the following insns are scanned (so that we may call this function
      43                 :             :      on insns returned by get_condition).
      44                 :             :    iv_analyze_result (insn, def, iv):  Stores to IV the description of the iv
      45                 :             :      corresponding to DEF, which is a register defined in INSN.
      46                 :             :    iv_analyze_expr (insn, mode, expr, iv):  Stores to IV the description of iv
      47                 :             :      corresponding to expression EXPR evaluated at INSN.  All registers used by
      48                 :             :      EXPR must also be used in INSN.  MODE is the mode of EXPR.
      49                 :             : */
      50                 :             : 
      51                 :             : #include "config.h"
      52                 :             : #include "system.h"
      53                 :             : #include "coretypes.h"
      54                 :             : #include "backend.h"
      55                 :             : #include "rtl.h"
      56                 :             : #include "df.h"
      57                 :             : #include "memmodel.h"
      58                 :             : #include "emit-rtl.h"
      59                 :             : #include "diagnostic-core.h"
      60                 :             : #include "cfgloop.h"
      61                 :             : #include "intl.h"
      62                 :             : #include "dumpfile.h"
      63                 :             : #include "rtl-iter.h"
      64                 :             : #include "tree-ssa-loop-niter.h"
      65                 :             : #include "regs.h"
      66                 :             : #include "function-abi.h"
      67                 :             : 
      68                 :             : /* Possible return values of iv_get_reaching_def.  */
      69                 :             : 
      70                 :             : enum iv_grd_result
      71                 :             : {
      72                 :             :   /* More than one reaching def, or reaching def that does not
      73                 :             :      dominate the use.  */
      74                 :             :   GRD_INVALID,
      75                 :             : 
      76                 :             :   /* The use is trivial invariant of the loop, i.e. is not changed
      77                 :             :      inside the loop.  */
      78                 :             :   GRD_INVARIANT,
      79                 :             : 
      80                 :             :   /* The use is reached by initial value and a value from the
      81                 :             :      previous iteration.  */
      82                 :             :   GRD_MAYBE_BIV,
      83                 :             : 
      84                 :             :   /* The use has single dominating def.  */
      85                 :             :   GRD_SINGLE_DOM
      86                 :             : };
      87                 :             : 
      88                 :             : /* Information about a biv.  */
      89                 :             : 
      90                 :             : class biv_entry
      91                 :             : {
      92                 :             : public:
      93                 :             :   unsigned regno;       /* The register of the biv.  */
      94                 :             :   class rtx_iv iv;      /* Value of the biv.  */
      95                 :             : };
      96                 :             : 
      97                 :             : static bool clean_slate = true;
      98                 :             : 
      99                 :             : static unsigned int iv_ref_table_size = 0;
     100                 :             : 
     101                 :             : /* Table of rtx_ivs indexed by the df_ref uid field.  */
     102                 :             : static class rtx_iv ** iv_ref_table;
     103                 :             : 
     104                 :             : /* Induction variable stored at the reference.  */
     105                 :             : #define DF_REF_IV(REF) iv_ref_table[DF_REF_ID (REF)]
     106                 :             : #define DF_REF_IV_SET(REF, IV) iv_ref_table[DF_REF_ID (REF)] = (IV)
     107                 :             : 
     108                 :             : /* The current loop.  */
     109                 :             : 
     110                 :             : static class loop *current_loop;
     111                 :             : 
     112                 :             : /* Hashtable helper.  */
     113                 :             : 
     114                 :             : struct biv_entry_hasher : free_ptr_hash <biv_entry>
     115                 :             : {
     116                 :             :   typedef rtx_def *compare_type;
     117                 :             :   static inline hashval_t hash (const biv_entry *);
     118                 :             :   static inline bool equal (const biv_entry *, const rtx_def *);
     119                 :             : };
     120                 :             : 
     121                 :             : /* Returns hash value for biv B.  */
     122                 :             : 
     123                 :             : inline hashval_t
     124                 :      117598 : biv_entry_hasher::hash (const biv_entry *b)
     125                 :             : {
     126                 :      117598 :   return b->regno;
     127                 :             : }
     128                 :             : 
     129                 :             : /* Compares biv B and register R.  */
     130                 :             : 
     131                 :             : inline bool
     132                 :      137377 : biv_entry_hasher::equal (const biv_entry *b, const rtx_def *r)
     133                 :             : {
     134                 :      137377 :   return b->regno == REGNO (r);
     135                 :             : }
     136                 :             : 
     137                 :             : /* Bivs of the current loop.  */
     138                 :             : 
     139                 :             : static hash_table<biv_entry_hasher> *bivs;
     140                 :             : 
     141                 :             : static bool iv_analyze_op (rtx_insn *, scalar_int_mode, rtx, class rtx_iv *);
     142                 :             : 
     143                 :             : /* Return the RTX code corresponding to the IV extend code EXTEND.  */
     144                 :             : static inline enum rtx_code
     145                 :           2 : iv_extend_to_rtx_code (enum iv_extend_code extend)
     146                 :             : {
     147                 :           2 :   switch (extend)
     148                 :             :     {
     149                 :             :     case IV_SIGN_EXTEND:
     150                 :             :       return SIGN_EXTEND;
     151                 :             :     case IV_ZERO_EXTEND:
     152                 :             :       return ZERO_EXTEND;
     153                 :             :     case IV_UNKNOWN_EXTEND:
     154                 :             :       return UNKNOWN;
     155                 :             :     }
     156                 :           0 :   gcc_unreachable ();
     157                 :             : }
     158                 :             : 
     159                 :             : /* Dumps information about IV to FILE.  */
     160                 :             : 
     161                 :             : extern void dump_iv_info (FILE *, class rtx_iv *);
     162                 :             : void
     163                 :         570 : dump_iv_info (FILE *file, class rtx_iv *iv)
     164                 :             : {
     165                 :         570 :   if (!iv->base)
     166                 :             :     {
     167                 :          57 :       fprintf (file, "not simple");
     168                 :          57 :       return;
     169                 :             :     }
     170                 :             : 
     171                 :         513 :   if (iv->step == const0_rtx
     172                 :         221 :       && !iv->first_special)
     173                 :         221 :     fprintf (file, "invariant ");
     174                 :             : 
     175                 :         513 :   print_rtl (file, iv->base);
     176                 :         513 :   if (iv->step != const0_rtx)
     177                 :             :     {
     178                 :         292 :       fprintf (file, " + ");
     179                 :         292 :       print_rtl (file, iv->step);
     180                 :         292 :       fprintf (file, " * iteration");
     181                 :             :     }
     182                 :         513 :   fprintf (file, " (in %s)", GET_MODE_NAME (iv->mode));
     183                 :             : 
     184                 :         513 :   if (iv->mode != iv->extend_mode)
     185                 :           0 :     fprintf (file, " %s to %s",
     186                 :           0 :              rtx_name[iv_extend_to_rtx_code (iv->extend)],
     187                 :           0 :              GET_MODE_NAME (iv->extend_mode));
     188                 :             : 
     189                 :         513 :   if (iv->mult != const1_rtx)
     190                 :             :     {
     191                 :           0 :       fprintf (file, " * ");
     192                 :           0 :       print_rtl (file, iv->mult);
     193                 :             :     }
     194                 :         513 :   if (iv->delta != const0_rtx)
     195                 :             :     {
     196                 :           0 :       fprintf (file, " + ");
     197                 :           0 :       print_rtl (file, iv->delta);
     198                 :             :     }
     199                 :         513 :   if (iv->first_special)
     200                 :           0 :     fprintf (file, " (first special)");
     201                 :             : }
     202                 :             : 
     203                 :             : static void
     204                 :     2596978 : check_iv_ref_table_size (void)
     205                 :             : {
     206                 :     2596978 :   if (iv_ref_table_size < DF_DEFS_TABLE_SIZE ())
     207                 :             :     {
     208                 :      261999 :       unsigned int new_size = DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
     209                 :      261999 :       iv_ref_table = XRESIZEVEC (class rtx_iv *, iv_ref_table, new_size);
     210                 :      261999 :       memset (&iv_ref_table[iv_ref_table_size], 0,
     211                 :      261999 :               (new_size - iv_ref_table_size) * sizeof (class rtx_iv *));
     212                 :      261999 :       iv_ref_table_size = new_size;
     213                 :             :     }
     214                 :     2596978 : }
     215                 :             : 
     216                 :             : 
     217                 :             : /* Checks whether REG is a well-behaved register.  */
     218                 :             : 
     219                 :             : static bool
     220                 :     3770980 : simple_reg_p (rtx reg)
     221                 :             : {
     222                 :     3770980 :   unsigned r;
     223                 :             : 
     224                 :     3770980 :   if (GET_CODE (reg) == SUBREG)
     225                 :             :     {
     226                 :       12053 :       if (!subreg_lowpart_p (reg))
     227                 :             :         return false;
     228                 :       12028 :       reg = SUBREG_REG (reg);
     229                 :             :     }
     230                 :             : 
     231                 :     3770955 :   if (!REG_P (reg))
     232                 :             :     return false;
     233                 :             : 
     234                 :     3476870 :   r = REGNO (reg);
     235                 :     3476870 :   if (HARD_REGISTER_NUM_P (r))
     236                 :             :     return false;
     237                 :             : 
     238                 :     3441946 :   if (GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
     239                 :             :     return false;
     240                 :             : 
     241                 :             :   return true;
     242                 :             : }
     243                 :             : 
     244                 :             : /* Clears the information about ivs stored in df.  */
     245                 :             : 
     246                 :             : static void
     247                 :      630690 : clear_iv_info (void)
     248                 :             : {
     249                 :      630690 :   unsigned i, n_defs = DF_DEFS_TABLE_SIZE ();
     250                 :      630690 :   class rtx_iv *iv;
     251                 :             : 
     252                 :      630690 :   check_iv_ref_table_size ();
     253                 :    78011764 :   for (i = 0; i < n_defs; i++)
     254                 :             :     {
     255                 :    76750384 :       iv = iv_ref_table[i];
     256                 :    76750384 :       if (iv)
     257                 :             :         {
     258                 :      663765 :           free (iv);
     259                 :      663765 :           iv_ref_table[i] = NULL;
     260                 :             :         }
     261                 :             :     }
     262                 :             : 
     263                 :      630690 :   bivs->empty ();
     264                 :      630690 : }
     265                 :             : 
     266                 :             : 
     267                 :             : /* Prepare the data for an induction variable analysis of a LOOP.  */
     268                 :             : 
     269                 :             : void
     270                 :      630690 : iv_analysis_loop_init (class loop *loop)
     271                 :             : {
     272                 :      630690 :   current_loop = loop;
     273                 :             : 
     274                 :             :   /* Clear the information from the analysis of the previous loop.  */
     275                 :      630690 :   if (clean_slate)
     276                 :             :     {
     277                 :      183702 :       df_set_flags (DF_EQ_NOTES + DF_DEFER_INSN_RESCAN);
     278                 :      183702 :       bivs = new hash_table<biv_entry_hasher> (10);
     279                 :      183702 :       clean_slate = false;
     280                 :             :     }
     281                 :             :   else
     282                 :      446988 :     clear_iv_info ();
     283                 :             : 
     284                 :             :   /* Get rid of the ud chains before processing the rescans.  Then add
     285                 :             :      the problem back.  */
     286                 :      630690 :   df_remove_problem (df_chain);
     287                 :      630690 :   df_process_deferred_rescans ();
     288                 :      630690 :   df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
     289                 :      630690 :   df_chain_add_problem (DF_UD_CHAIN);
     290                 :      630690 :   df_note_add_problem ();
     291                 :      630690 :   df_analyze_loop (loop);
     292                 :      630690 :   if (dump_file)
     293                 :         174 :     df_dump_region (dump_file);
     294                 :             : 
     295                 :      630690 :   check_iv_ref_table_size ();
     296                 :      630690 : }
     297                 :             : 
     298                 :             : /* Finds the definition of REG that dominates loop latch and stores
     299                 :             :    it to DEF.  Returns false if there is not a single definition
     300                 :             :    dominating the latch.  If REG has no definition in loop, DEF
     301                 :             :    is set to NULL and true is returned.  */
     302                 :             : 
     303                 :             : static bool
     304                 :      668309 : latch_dominating_def (rtx reg, df_ref *def)
     305                 :             : {
     306                 :      668309 :   df_ref single_rd = NULL, adef;
     307                 :      668309 :   unsigned regno = REGNO (reg);
     308                 :      668309 :   class df_rd_bb_info *bb_info = DF_RD_BB_INFO (current_loop->latch);
     309                 :             : 
     310                 :     2152663 :   for (adef = DF_REG_DEF_CHAIN (regno); adef; adef = DF_REF_NEXT_REG (adef))
     311                 :             :     {
     312                 :     1484377 :       if (!bitmap_bit_p (df->blocks_to_analyze, DF_REF_BBNO (adef))
     313                 :     1484377 :           || !bitmap_bit_p (&bb_info->out, DF_REF_ID (adef)))
     314                 :      836736 :         continue;
     315                 :             : 
     316                 :             :       /* More than one reaching definition.  */
     317                 :      647641 :       if (single_rd)
     318                 :             :         return false;
     319                 :             : 
     320                 :      647641 :       if (!just_once_each_iteration_p (current_loop, DF_REF_BB (adef)))
     321                 :             :         return false;
     322                 :             : 
     323                 :             :       single_rd = adef;
     324                 :             :     }
     325                 :             : 
     326                 :      668286 :   *def = single_rd;
     327                 :      668286 :   return true;
     328                 :             : }
     329                 :             : 
     330                 :             : /* Gets definition of REG reaching its use in INSN and stores it to DEF.  */
     331                 :             : 
     332                 :             : static enum iv_grd_result
     333                 :     2020065 : iv_get_reaching_def (rtx_insn *insn, rtx reg, df_ref *def)
     334                 :             : {
     335                 :     2020065 :   df_ref use, adef;
     336                 :     2020065 :   basic_block def_bb, use_bb;
     337                 :     2020065 :   rtx_insn *def_insn;
     338                 :     2020065 :   bool dom_p;
     339                 :             : 
     340                 :     2020065 :   *def = NULL;
     341                 :     2020065 :   if (!simple_reg_p (reg))
     342                 :             :     return GRD_INVALID;
     343                 :     1930757 :   if (GET_CODE (reg) == SUBREG)
     344                 :           0 :     reg = SUBREG_REG (reg);
     345                 :     1930757 :   gcc_assert (REG_P (reg));
     346                 :             : 
     347                 :     1930757 :   use = df_find_use (insn, reg);
     348                 :     1930757 :   gcc_assert (use != NULL);
     349                 :             : 
     350                 :     1930757 :   if (!DF_REF_CHAIN (use))
     351                 :             :     return GRD_INVARIANT;
     352                 :             : 
     353                 :             :   /* More than one reaching def.  */
     354                 :     1647257 :   if (DF_REF_CHAIN (use)->next)
     355                 :             :     return GRD_INVALID;
     356                 :             : 
     357                 :     1621440 :   adef = DF_REF_CHAIN (use)->ref;
     358                 :             : 
     359                 :             :   /* We do not handle setting only part of the register.  */
     360                 :     1621440 :   if (DF_REF_FLAGS (adef) & DF_REF_READ_WRITE)
     361                 :             :     return GRD_INVALID;
     362                 :             : 
     363                 :     1621436 :   def_insn = DF_REF_INSN (adef);
     364                 :     1621436 :   def_bb = DF_REF_BB (adef);
     365                 :     1621436 :   use_bb = BLOCK_FOR_INSN (insn);
     366                 :             : 
     367                 :     1621436 :   if (use_bb == def_bb)
     368                 :     1491851 :     dom_p = (DF_INSN_LUID (def_insn) < DF_INSN_LUID (insn));
     369                 :             :   else
     370                 :      129585 :     dom_p = dominated_by_p (CDI_DOMINATORS, use_bb, def_bb);
     371                 :             : 
     372                 :     1621436 :   if (dom_p)
     373                 :             :     {
     374                 :      638342 :       *def = adef;
     375                 :      638342 :       return GRD_SINGLE_DOM;
     376                 :             :     }
     377                 :             : 
     378                 :             :   /* The definition does not dominate the use.  This is still OK if
     379                 :             :      this may be a use of a biv, i.e. if the def_bb dominates loop
     380                 :             :      latch.  */
     381                 :      983094 :   if (just_once_each_iteration_p (current_loop, def_bb))
     382                 :             :     return GRD_MAYBE_BIV;
     383                 :             : 
     384                 :             :   return GRD_INVALID;
     385                 :             : }
     386                 :             : 
     387                 :             : /* Sets IV to invariant CST in MODE.  Always returns true (just for
     388                 :             :    consistency with other iv manipulation functions that may fail).  */
     389                 :             : 
     390                 :             : static bool
     391                 :      901804 : iv_constant (class rtx_iv *iv, scalar_int_mode mode, rtx cst)
     392                 :             : {
     393                 :      901804 :   iv->mode = mode;
     394                 :      901804 :   iv->base = cst;
     395                 :      901804 :   iv->step = const0_rtx;
     396                 :      901804 :   iv->first_special = false;
     397                 :      901804 :   iv->extend = IV_UNKNOWN_EXTEND;
     398                 :      901804 :   iv->extend_mode = iv->mode;
     399                 :      901804 :   iv->delta = const0_rtx;
     400                 :      901804 :   iv->mult = const1_rtx;
     401                 :             : 
     402                 :      901804 :   return true;
     403                 :             : }
     404                 :             : 
     405                 :             : /* Evaluates application of subreg to MODE on IV.  */
     406                 :             : 
     407                 :             : static bool
     408                 :        5447 : iv_subreg (class rtx_iv *iv, scalar_int_mode mode)
     409                 :             : {
     410                 :             :   /* If iv is invariant, just calculate the new value.  */
     411                 :        5447 :   if (iv->step == const0_rtx
     412                 :          56 :       && !iv->first_special)
     413                 :             :     {
     414                 :          54 :       rtx val = get_iv_value (iv, const0_rtx);
     415                 :          54 :       val = lowpart_subreg (mode, val,
     416                 :          54 :                             iv->extend == IV_UNKNOWN_EXTEND
     417                 :             :                             ? iv->mode : iv->extend_mode);
     418                 :             : 
     419                 :          54 :       iv->base = val;
     420                 :          54 :       iv->extend = IV_UNKNOWN_EXTEND;
     421                 :          54 :       iv->mode = iv->extend_mode = mode;
     422                 :          54 :       iv->delta = const0_rtx;
     423                 :          54 :       iv->mult = const1_rtx;
     424                 :          54 :       return true;
     425                 :             :     }
     426                 :             : 
     427                 :        5393 :   if (iv->extend_mode == mode)
     428                 :             :     return true;
     429                 :             : 
     430                 :       16179 :   if (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (iv->mode))
     431                 :             :     return false;
     432                 :             : 
     433                 :        5393 :   iv->extend = IV_UNKNOWN_EXTEND;
     434                 :        5393 :   iv->mode = mode;
     435                 :             : 
     436                 :        5393 :   iv->base = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
     437                 :             :                                   simplify_gen_binary (MULT, iv->extend_mode,
     438                 :             :                                                        iv->base, iv->mult));
     439                 :        5393 :   iv->step = simplify_gen_binary (MULT, iv->extend_mode, iv->step, iv->mult);
     440                 :        5393 :   iv->mult = const1_rtx;
     441                 :        5393 :   iv->delta = const0_rtx;
     442                 :        5393 :   iv->first_special = false;
     443                 :             : 
     444                 :        5393 :   return true;
     445                 :             : }
     446                 :             : 
     447                 :             : /* Evaluates application of EXTEND to MODE on IV.  */
     448                 :             : 
     449                 :             : static bool
     450                 :         579 : iv_extend (class rtx_iv *iv, enum iv_extend_code extend, scalar_int_mode mode)
     451                 :             : {
     452                 :             :   /* If iv is invariant, just calculate the new value.  */
     453                 :         579 :   if (iv->step == const0_rtx
     454                 :           2 :       && !iv->first_special)
     455                 :             :     {
     456                 :           2 :       rtx val = get_iv_value (iv, const0_rtx);
     457                 :           2 :       if (iv->extend_mode != iv->mode
     458                 :           0 :           && iv->extend != IV_UNKNOWN_EXTEND
     459                 :           2 :           && iv->extend != extend)
     460                 :           0 :         val = lowpart_subreg (iv->mode, val, iv->extend_mode);
     461                 :           2 :       val = simplify_gen_unary (iv_extend_to_rtx_code (extend), mode,
     462                 :             :                                 val,
     463                 :           2 :                                 iv->extend == extend
     464                 :             :                                 ? iv->extend_mode : iv->mode);
     465                 :           2 :       iv->base = val;
     466                 :           2 :       iv->extend = IV_UNKNOWN_EXTEND;
     467                 :           2 :       iv->mode = iv->extend_mode = mode;
     468                 :           2 :       iv->delta = const0_rtx;
     469                 :           2 :       iv->mult = const1_rtx;
     470                 :           2 :       return true;
     471                 :             :     }
     472                 :             : 
     473                 :         577 :   if (mode != iv->extend_mode)
     474                 :             :     return false;
     475                 :             : 
     476                 :           6 :   if (iv->extend != IV_UNKNOWN_EXTEND
     477                 :           0 :       && iv->extend != extend)
     478                 :             :     return false;
     479                 :             : 
     480                 :           6 :   iv->extend = extend;
     481                 :             : 
     482                 :           6 :   return true;
     483                 :             : }
     484                 :             : 
     485                 :             : /* Evaluates negation of IV.  */
     486                 :             : 
     487                 :             : static bool
     488                 :          82 : iv_neg (class rtx_iv *iv)
     489                 :             : {
     490                 :          82 :   if (iv->extend == IV_UNKNOWN_EXTEND)
     491                 :             :     {
     492                 :          82 :       iv->base = simplify_gen_unary (NEG, iv->extend_mode,
     493                 :             :                                      iv->base, iv->extend_mode);
     494                 :          82 :       iv->step = simplify_gen_unary (NEG, iv->extend_mode,
     495                 :             :                                      iv->step, iv->extend_mode);
     496                 :             :     }
     497                 :             :   else
     498                 :             :     {
     499                 :           0 :       iv->delta = simplify_gen_unary (NEG, iv->extend_mode,
     500                 :             :                                       iv->delta, iv->extend_mode);
     501                 :           0 :       iv->mult = simplify_gen_unary (NEG, iv->extend_mode,
     502                 :             :                                      iv->mult, iv->extend_mode);
     503                 :             :     }
     504                 :             : 
     505                 :          82 :   return true;
     506                 :             : }
     507                 :             : 
     508                 :             : /* Evaluates addition or subtraction (according to OP) of IV1 to IV0.  */
     509                 :             : 
     510                 :             : static bool
     511                 :      446048 : iv_add (class rtx_iv *iv0, class rtx_iv *iv1, enum rtx_code op)
     512                 :             : {
     513                 :      446048 :   scalar_int_mode mode;
     514                 :      446048 :   rtx arg;
     515                 :             : 
     516                 :             :   /* Extend the constant to extend_mode of the other operand if necessary.  */
     517                 :      446048 :   if (iv0->extend == IV_UNKNOWN_EXTEND
     518                 :      446047 :       && iv0->mode == iv0->extend_mode
     519                 :      445366 :       && iv0->step == const0_rtx
     520                 :      449639 :       && GET_MODE_SIZE (iv0->extend_mode) < GET_MODE_SIZE (iv1->extend_mode))
     521                 :             :     {
     522                 :         297 :       iv0->extend_mode = iv1->extend_mode;
     523                 :         297 :       iv0->base = simplify_gen_unary (ZERO_EXTEND, iv0->extend_mode,
     524                 :             :                                       iv0->base, iv0->mode);
     525                 :             :     }
     526                 :      446048 :   if (iv1->extend == IV_UNKNOWN_EXTEND
     527                 :      446048 :       && iv1->mode == iv1->extend_mode
     528                 :      445751 :       && iv1->step == const0_rtx
     529                 :     1781282 :       && GET_MODE_SIZE (iv1->extend_mode) < GET_MODE_SIZE (iv0->extend_mode))
     530                 :             :     {
     531                 :         681 :       iv1->extend_mode = iv0->extend_mode;
     532                 :         681 :       iv1->base = simplify_gen_unary (ZERO_EXTEND, iv1->extend_mode,
     533                 :             :                                       iv1->base, iv1->mode);
     534                 :             :     }
     535                 :             : 
     536                 :      446048 :   mode = iv0->extend_mode;
     537                 :      446048 :   if (mode != iv1->extend_mode)
     538                 :             :     return false;
     539                 :             : 
     540                 :      446048 :   if (iv0->extend == IV_UNKNOWN_EXTEND
     541                 :      446047 :       && iv1->extend == IV_UNKNOWN_EXTEND)
     542                 :             :     {
     543                 :      446047 :       if (iv0->mode != iv1->mode)
     544                 :             :         return false;
     545                 :             : 
     546                 :      446047 :       iv0->base = simplify_gen_binary (op, mode, iv0->base, iv1->base);
     547                 :      446047 :       iv0->step = simplify_gen_binary (op, mode, iv0->step, iv1->step);
     548                 :             : 
     549                 :      446047 :       return true;
     550                 :             :     }
     551                 :             : 
     552                 :             :   /* Handle addition of constant.  */
     553                 :           1 :   if (iv1->extend == IV_UNKNOWN_EXTEND
     554                 :           1 :       && iv1->mode == mode
     555                 :           2 :       && iv1->step == const0_rtx)
     556                 :             :     {
     557                 :           1 :       iv0->delta = simplify_gen_binary (op, mode, iv0->delta, iv1->base);
     558                 :           1 :       return true;
     559                 :             :     }
     560                 :             : 
     561                 :           0 :   if (iv0->extend == IV_UNKNOWN_EXTEND
     562                 :           0 :       && iv0->mode == mode
     563                 :           0 :       && iv0->step == const0_rtx)
     564                 :             :     {
     565                 :           0 :       arg = iv0->base;
     566                 :           0 :       *iv0 = *iv1;
     567                 :           0 :       if (op == MINUS
     568                 :           0 :           && !iv_neg (iv0))
     569                 :             :         return false;
     570                 :             : 
     571                 :           0 :       iv0->delta = simplify_gen_binary (PLUS, mode, iv0->delta, arg);
     572                 :           0 :       return true;
     573                 :             :     }
     574                 :             : 
     575                 :             :   return false;
     576                 :             : }
     577                 :             : 
     578                 :             : /* Evaluates multiplication of IV by constant CST.  */
     579                 :             : 
     580                 :             : static bool
     581                 :          32 : iv_mult (class rtx_iv *iv, rtx mby)
     582                 :             : {
     583                 :          32 :   scalar_int_mode mode = iv->extend_mode;
     584                 :             : 
     585                 :          32 :   if (GET_MODE (mby) != VOIDmode
     586                 :          32 :       && GET_MODE (mby) != mode)
     587                 :             :     return false;
     588                 :             : 
     589                 :          32 :   if (iv->extend == IV_UNKNOWN_EXTEND)
     590                 :             :     {
     591                 :          32 :       iv->base = simplify_gen_binary (MULT, mode, iv->base, mby);
     592                 :          32 :       iv->step = simplify_gen_binary (MULT, mode, iv->step, mby);
     593                 :             :     }
     594                 :             :   else
     595                 :             :     {
     596                 :           0 :       iv->delta = simplify_gen_binary (MULT, mode, iv->delta, mby);
     597                 :           0 :       iv->mult = simplify_gen_binary (MULT, mode, iv->mult, mby);
     598                 :             :     }
     599                 :             : 
     600                 :             :   return true;
     601                 :             : }
     602                 :             : 
     603                 :             : /* Evaluates shift of IV by constant CST.  */
     604                 :             : 
     605                 :             : static bool
     606                 :         188 : iv_shift (class rtx_iv *iv, rtx mby)
     607                 :             : {
     608                 :         188 :   scalar_int_mode mode = iv->extend_mode;
     609                 :             : 
     610                 :         188 :   if (GET_MODE (mby) != VOIDmode
     611                 :         188 :       && GET_MODE (mby) != mode)
     612                 :             :     return false;
     613                 :             : 
     614                 :         188 :   if (iv->extend == IV_UNKNOWN_EXTEND)
     615                 :             :     {
     616                 :         188 :       iv->base = simplify_gen_binary (ASHIFT, mode, iv->base, mby);
     617                 :         188 :       iv->step = simplify_gen_binary (ASHIFT, mode, iv->step, mby);
     618                 :             :     }
     619                 :             :   else
     620                 :             :     {
     621                 :           0 :       iv->delta = simplify_gen_binary (ASHIFT, mode, iv->delta, mby);
     622                 :           0 :       iv->mult = simplify_gen_binary (ASHIFT, mode, iv->mult, mby);
     623                 :             :     }
     624                 :             : 
     625                 :             :   return true;
     626                 :             : }
     627                 :             : 
     628                 :             : /* The recursive part of get_biv_step.  Gets the value of the single value
     629                 :             :    defined by DEF wrto initial value of REG inside loop, in shape described
     630                 :             :    at get_biv_step.  */
     631                 :             : 
     632                 :             : static bool
     633                 :      523197 : get_biv_step_1 (df_ref def, scalar_int_mode outer_mode, rtx reg,
     634                 :             :                 rtx *inner_step, scalar_int_mode *inner_mode,
     635                 :             :                 enum iv_extend_code *extend,
     636                 :             :                 rtx *outer_step)
     637                 :             : {
     638                 :      523197 :   rtx set, rhs, op0 = NULL_RTX, op1 = NULL_RTX;
     639                 :      523197 :   rtx next, nextr;
     640                 :      523197 :   enum rtx_code code, prev_code = UNKNOWN;
     641                 :      523197 :   rtx_insn *insn = DF_REF_INSN (def);
     642                 :      523197 :   df_ref next_def;
     643                 :      523197 :   enum iv_grd_result res;
     644                 :             : 
     645                 :      523197 :   set = single_set (insn);
     646                 :      523197 :   if (!set)
     647                 :             :     return false;
     648                 :             : 
     649                 :      521818 :   rhs = find_reg_equal_equiv_note (insn);
     650                 :      521818 :   if (rhs)
     651                 :        2292 :     rhs = XEXP (rhs, 0);
     652                 :             :   else
     653                 :      519526 :     rhs = SET_SRC (set);
     654                 :             : 
     655                 :      521818 :   code = GET_CODE (rhs);
     656                 :      521818 :   switch (code)
     657                 :             :     {
     658                 :             :     case SUBREG:
     659                 :             :     case REG:
     660                 :             :       next = rhs;
     661                 :             :       break;
     662                 :             : 
     663                 :      477651 :     case PLUS:
     664                 :      477651 :     case MINUS:
     665                 :      477651 :       op0 = XEXP (rhs, 0);
     666                 :      477651 :       op1 = XEXP (rhs, 1);
     667                 :             : 
     668                 :      477651 :       if (code == PLUS && CONSTANT_P (op0))
     669                 :             :         std::swap (op0, op1);
     670                 :             : 
     671                 :      477651 :       if (!simple_reg_p (op0)
     672                 :      477651 :           || !CONSTANT_P (op1))
     673                 :             :         return false;
     674                 :             : 
     675                 :      458969 :       if (GET_MODE (rhs) != outer_mode)
     676                 :             :         {
     677                 :             :           /* ppc64 uses expressions like
     678                 :             : 
     679                 :             :              (set x:SI (plus:SI (subreg:SI y:DI) 1)).
     680                 :             : 
     681                 :             :              this is equivalent to
     682                 :             : 
     683                 :             :              (set x':DI (plus:DI y:DI 1))
     684                 :             :              (set x:SI (subreg:SI (x':DI)).  */
     685                 :         222 :           if (GET_CODE (op0) != SUBREG)
     686                 :             :             return false;
     687                 :           9 :           if (GET_MODE (SUBREG_REG (op0)) != outer_mode)
     688                 :             :             return false;
     689                 :             :         }
     690                 :             : 
     691                 :             :       next = op0;
     692                 :             :       break;
     693                 :             : 
     694                 :         394 :     case SIGN_EXTEND:
     695                 :         394 :     case ZERO_EXTEND:
     696                 :         394 :       if (GET_MODE (rhs) != outer_mode)
     697                 :             :         return false;
     698                 :             : 
     699                 :         390 :       op0 = XEXP (rhs, 0);
     700                 :             : 
     701                 :             :       /* rv64 wraps SImode arithmetic inside an extension to DImode.
     702                 :             :          This matches the actual hardware semantics.  So peek inside
     703                 :             :          the extension and see if we have simple arithmetic that we
     704                 :             :          can analyze.  */
     705                 :         390 :       if (GET_CODE (op0) == PLUS)
     706                 :             :         {
     707                 :           0 :           rhs = op0;
     708                 :           0 :           op0 = XEXP (rhs, 0);
     709                 :           0 :           op1 = XEXP (rhs, 1);
     710                 :             : 
     711                 :           0 :           if (CONSTANT_P (op0))
     712                 :           0 :             std::swap (op0, op1);
     713                 :             : 
     714                 :           0 :           if (!simple_reg_p (op0) || !CONSTANT_P (op1))
     715                 :             :             return false;
     716                 :             : 
     717                 :           0 :           op1 = simplify_gen_unary (code, outer_mode, op1, GET_MODE (rhs));
     718                 :           0 :           prev_code = code;
     719                 :           0 :           code = PLUS;
     720                 :             :         }
     721                 :             : 
     722                 :         390 :       if (!simple_reg_p (op0))
     723                 :             :         return false;
     724                 :             : 
     725                 :             :       next = op0;
     726                 :             :       break;
     727                 :             : 
     728                 :             :     default:
     729                 :             :       return false;
     730                 :             :     }
     731                 :             : 
     732                 :      482292 :   if (GET_CODE (next) == SUBREG)
     733                 :             :     {
     734                 :         955 :       if (!subreg_lowpart_p (next))
     735                 :             :         return false;
     736                 :             : 
     737                 :         910 :       nextr = SUBREG_REG (next);
     738                 :         910 :       if (GET_MODE (nextr) != outer_mode)
     739                 :             :         return false;
     740                 :             :     }
     741                 :             :   else
     742                 :             :     nextr = next;
     743                 :             : 
     744                 :      481356 :   res = iv_get_reaching_def (insn, nextr, &next_def);
     745                 :             : 
     746                 :      481356 :   if (res == GRD_INVALID || res == GRD_INVARIANT)
     747                 :             :     return false;
     748                 :             : 
     749                 :      477218 :   if (res == GRD_MAYBE_BIV)
     750                 :             :     {
     751                 :      458501 :       if (!rtx_equal_p (nextr, reg))
     752                 :             :         return false;
     753                 :             : 
     754                 :      457978 :       *inner_step = const0_rtx;
     755                 :      457978 :       *extend = IV_UNKNOWN_EXTEND;
     756                 :      457978 :       *inner_mode = outer_mode;
     757                 :      457978 :       *outer_step = const0_rtx;
     758                 :             :     }
     759                 :       18717 :   else if (!get_biv_step_1 (next_def, outer_mode, reg,
     760                 :             :                             inner_step, inner_mode, extend,
     761                 :             :                             outer_step))
     762                 :             :     return false;
     763                 :             : 
     764                 :      465458 :   if (GET_CODE (next) == SUBREG)
     765                 :             :     {
     766                 :          14 :       scalar_int_mode amode;
     767                 :          14 :       if (!is_a <scalar_int_mode> (GET_MODE (next), &amode)
     768                 :          28 :           || GET_MODE_SIZE (amode) > GET_MODE_SIZE (*inner_mode))
     769                 :       57739 :         return false;
     770                 :             : 
     771                 :          14 :       *inner_mode = amode;
     772                 :          14 :       *inner_step = simplify_gen_binary (PLUS, outer_mode,
     773                 :             :                                          *inner_step, *outer_step);
     774                 :          14 :       *outer_step = const0_rtx;
     775                 :          14 :       *extend = IV_UNKNOWN_EXTEND;
     776                 :             :     }
     777                 :             : 
     778                 :      465458 :   switch (code)
     779                 :             :     {
     780                 :             :     case REG:
     781                 :             :     case SUBREG:
     782                 :             :       break;
     783                 :             : 
     784                 :      457975 :     case PLUS:
     785                 :      457975 :     case MINUS:
     786                 :      457975 :       if (*inner_mode == outer_mode
     787                 :             :           /* See comment in previous switch.  */
     788                 :      457975 :           || GET_MODE (rhs) != outer_mode)
     789                 :      457974 :         *inner_step = simplify_gen_binary (code, outer_mode,
     790                 :             :                                            *inner_step, op1);
     791                 :             :       else
     792                 :           1 :         *outer_step = simplify_gen_binary (code, outer_mode,
     793                 :             :                                            *outer_step, op1);
     794                 :             : 
     795                 :      457975 :       if (prev_code == SIGN_EXTEND)
     796                 :           0 :         *extend = IV_SIGN_EXTEND;
     797                 :      457975 :       else if (prev_code == ZERO_EXTEND)
     798                 :           0 :         *extend = IV_ZERO_EXTEND;
     799                 :             :       break;
     800                 :             : 
     801                 :          14 :     case SIGN_EXTEND:
     802                 :          14 :     case ZERO_EXTEND:
     803                 :          14 :       gcc_assert (GET_MODE (op0) == *inner_mode
     804                 :             :                   && *extend == IV_UNKNOWN_EXTEND
     805                 :             :                   && *outer_step == const0_rtx);
     806                 :             : 
     807                 :          14 :       *extend = (code == SIGN_EXTEND) ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
     808                 :          14 :       break;
     809                 :             : 
     810                 :             :     default:
     811                 :             :       return false;
     812                 :             :     }
     813                 :             : 
     814                 :             :   return true;
     815                 :             : }
     816                 :             : 
     817                 :             : /* Gets the operation on register REG inside loop, in shape
     818                 :             : 
     819                 :             :    OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
     820                 :             : 
     821                 :             :    If the operation cannot be described in this shape, return false.
     822                 :             :    LAST_DEF is the definition of REG that dominates loop latch.  */
     823                 :             : 
     824                 :             : static bool
     825                 :      504480 : get_biv_step (df_ref last_def, scalar_int_mode outer_mode, rtx reg,
     826                 :             :               rtx *inner_step, scalar_int_mode *inner_mode,
     827                 :             :               enum iv_extend_code *extend, rtx *outer_step)
     828                 :             : {
     829                 :      504480 :   if (!get_biv_step_1 (last_def, outer_mode, reg,
     830                 :             :                        inner_step, inner_mode, extend,
     831                 :             :                        outer_step))
     832                 :             :     return false;
     833                 :             : 
     834                 :      457978 :   gcc_assert ((*inner_mode == outer_mode) != (*extend != IV_UNKNOWN_EXTEND));
     835                 :      457978 :   gcc_assert (*inner_mode != outer_mode || *outer_step == const0_rtx);
     836                 :             : 
     837                 :             :   return true;
     838                 :             : }
     839                 :             : 
     840                 :             : /* Records information that DEF is induction variable IV.  */
     841                 :             : 
     842                 :             : static void
     843                 :      663765 : record_iv (df_ref def, class rtx_iv *iv)
     844                 :             : {
     845                 :      663765 :   class rtx_iv *recorded_iv = XNEW (class rtx_iv);
     846                 :             : 
     847                 :      663765 :   *recorded_iv = *iv;
     848                 :      663765 :   check_iv_ref_table_size ();
     849                 :      663765 :   DF_REF_IV_SET (def, recorded_iv);
     850                 :      663765 : }
     851                 :             : 
     852                 :             : /* If DEF was already analyzed for bivness, store the description of the biv to
     853                 :             :    IV and return true.  Otherwise return false.  */
     854                 :             : 
     855                 :             : static bool
     856                 :      584550 : analyzed_for_bivness_p (rtx def, class rtx_iv *iv)
     857                 :             : {
     858                 :      584550 :   class biv_entry *biv = bivs->find_with_hash (def, REGNO (def));
     859                 :             : 
     860                 :      584550 :   if (!biv)
     861                 :             :     return false;
     862                 :             : 
     863                 :       80070 :   *iv = biv->iv;
     864                 :       80070 :   return true;
     865                 :             : }
     866                 :             : 
     867                 :             : static void
     868                 :      504480 : record_biv (rtx def, class rtx_iv *iv)
     869                 :             : {
     870                 :      504480 :   class biv_entry *biv = XNEW (class biv_entry);
     871                 :      504480 :   biv_entry **slot = bivs->find_slot_with_hash (def, REGNO (def), INSERT);
     872                 :             : 
     873                 :      504480 :   biv->regno = REGNO (def);
     874                 :      504480 :   biv->iv = *iv;
     875                 :      504480 :   gcc_assert (!*slot);
     876                 :      504480 :   *slot = biv;
     877                 :      504480 : }
     878                 :             : 
     879                 :             : /* Determines whether DEF is a biv and if so, stores its description
     880                 :             :    to *IV.  OUTER_MODE is the mode of DEF.  */
     881                 :             : 
     882                 :             : static bool
     883                 :      584550 : iv_analyze_biv (scalar_int_mode outer_mode, rtx def, class rtx_iv *iv)
     884                 :             : {
     885                 :      584550 :   rtx inner_step, outer_step;
     886                 :      584550 :   scalar_int_mode inner_mode;
     887                 :      584550 :   enum iv_extend_code extend;
     888                 :      584550 :   df_ref last_def;
     889                 :             : 
     890                 :      584550 :   if (dump_file)
     891                 :             :     {
     892                 :         217 :       fprintf (dump_file, "Analyzing ");
     893                 :         217 :       print_rtl (dump_file, def);
     894                 :         217 :       fprintf (dump_file, " for bivness.\n");
     895                 :             :     }
     896                 :             : 
     897                 :      584550 :   if (!REG_P (def))
     898                 :             :     {
     899                 :           0 :       if (!CONSTANT_P (def))
     900                 :             :         return false;
     901                 :             : 
     902                 :           0 :       return iv_constant (iv, outer_mode, def);
     903                 :             :     }
     904                 :             : 
     905                 :      584550 :   if (!latch_dominating_def (def, &last_def))
     906                 :             :     {
     907                 :           0 :       if (dump_file)
     908                 :           0 :         fprintf (dump_file, "  not simple.\n");
     909                 :           0 :       return false;
     910                 :             :     }
     911                 :             : 
     912                 :      584550 :   if (!last_def)
     913                 :           0 :     return iv_constant (iv, outer_mode, def);
     914                 :             : 
     915                 :      584550 :   if (analyzed_for_bivness_p (def, iv))
     916                 :             :     {
     917                 :       80070 :       if (dump_file)
     918                 :          71 :         fprintf (dump_file, "  already analysed.\n");
     919                 :       80070 :       return iv->base != NULL_RTX;
     920                 :             :     }
     921                 :             : 
     922                 :      504480 :   if (!get_biv_step (last_def, outer_mode, def, &inner_step, &inner_mode,
     923                 :             :                      &extend, &outer_step))
     924                 :             :     {
     925                 :       46502 :       iv->base = NULL_RTX;
     926                 :       46502 :       goto end;
     927                 :             :     }
     928                 :             : 
     929                 :             :   /* Loop transforms base to es (base + inner_step) + outer_step,
     930                 :             :      where es means extend of subreg between inner_mode and outer_mode.
     931                 :             :      The corresponding induction variable is
     932                 :             : 
     933                 :             :      es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step  */
     934                 :             : 
     935                 :      457978 :   iv->base = simplify_gen_binary (MINUS, outer_mode, def, outer_step);
     936                 :      457978 :   iv->step = simplify_gen_binary (PLUS, outer_mode, inner_step, outer_step);
     937                 :      457978 :   iv->mode = inner_mode;
     938                 :      457978 :   iv->extend_mode = outer_mode;
     939                 :      457978 :   iv->extend = extend;
     940                 :      457978 :   iv->mult = const1_rtx;
     941                 :      457978 :   iv->delta = outer_step;
     942                 :      457978 :   iv->first_special = inner_mode != outer_mode;
     943                 :             : 
     944                 :      504480 :  end:
     945                 :      504480 :   if (dump_file)
     946                 :             :     {
     947                 :         146 :       fprintf (dump_file, "  ");
     948                 :         146 :       dump_iv_info (dump_file, iv);
     949                 :         146 :       fprintf (dump_file, "\n");
     950                 :             :     }
     951                 :             : 
     952                 :      504480 :   record_biv (def, iv);
     953                 :      504480 :   return iv->base != NULL_RTX;
     954                 :             : }
     955                 :             : 
     956                 :             : /* Analyzes expression RHS used at INSN and stores the result to *IV.
     957                 :             :    The mode of the induction variable is MODE.  */
     958                 :             : 
     959                 :             : bool
     960                 :     1610918 : iv_analyze_expr (rtx_insn *insn, scalar_int_mode mode, rtx rhs,
     961                 :             :                  class rtx_iv *iv)
     962                 :             : {
     963                 :     1610918 :   rtx mby = NULL_RTX;
     964                 :     1610918 :   rtx op0 = NULL_RTX, op1 = NULL_RTX;
     965                 :     1610918 :   class rtx_iv iv0, iv1;
     966                 :     1610918 :   enum rtx_code code = GET_CODE (rhs);
     967                 :     1610918 :   scalar_int_mode omode = mode;
     968                 :             : 
     969                 :     1610918 :   iv->base = NULL_RTX;
     970                 :     1610918 :   iv->step = NULL_RTX;
     971                 :             : 
     972                 :     1610918 :   gcc_assert (GET_MODE (rhs) == mode || GET_MODE (rhs) == VOIDmode);
     973                 :             : 
     974                 :     1610918 :   if (CONSTANT_P (rhs)
     975                 :     1165958 :       || REG_P (rhs)
     976                 :      636968 :       || code == SUBREG)
     977                 :      975735 :     return iv_analyze_op (insn, mode, rhs, iv);
     978                 :             : 
     979                 :      635183 :   switch (code)
     980                 :             :     {
     981                 :             :     case REG:
     982                 :             :       op0 = rhs;
     983                 :             :       break;
     984                 :             : 
     985                 :       10309 :     case SIGN_EXTEND:
     986                 :       10309 :     case ZERO_EXTEND:
     987                 :       10309 :     case NEG:
     988                 :       10309 :       op0 = XEXP (rhs, 0);
     989                 :             :       /* We don't know how many bits there are in a sign-extended constant.  */
     990                 :      199134 :       if (!is_a <scalar_int_mode> (GET_MODE (op0), &omode))
     991                 :             :         return false;
     992                 :             :       break;
     993                 :             : 
     994                 :      486751 :     case PLUS:
     995                 :      486751 :     case MINUS:
     996                 :      486751 :       op0 = XEXP (rhs, 0);
     997                 :      486751 :       op1 = XEXP (rhs, 1);
     998                 :      486751 :       break;
     999                 :             : 
    1000                 :        1708 :     case MULT:
    1001                 :        1708 :       op0 = XEXP (rhs, 0);
    1002                 :        1708 :       mby = XEXP (rhs, 1);
    1003                 :        1708 :       if (!CONSTANT_P (mby))
    1004                 :        1314 :         std::swap (op0, mby);
    1005                 :        1708 :       if (!CONSTANT_P (mby))
    1006                 :             :         return false;
    1007                 :             :       break;
    1008                 :             : 
    1009                 :        2265 :     case ASHIFT:
    1010                 :        2265 :       op0 = XEXP (rhs, 0);
    1011                 :        2265 :       mby = XEXP (rhs, 1);
    1012                 :        2265 :       if (!CONSTANT_P (mby))
    1013                 :             :         return false;
    1014                 :             :       break;
    1015                 :             : 
    1016                 :             :     default:
    1017                 :             :       return false;
    1018                 :             :     }
    1019                 :             : 
    1020                 :      499672 :   if (op0
    1021                 :      499672 :       && !iv_analyze_expr (insn, omode, op0, &iv0))
    1022                 :             :     return false;
    1023                 :             : 
    1024                 :      448362 :   if (op1
    1025                 :      448362 :       && !iv_analyze_expr (insn, omode, op1, &iv1))
    1026                 :             :     return false;
    1027                 :             : 
    1028                 :      446929 :   switch (code)
    1029                 :             :     {
    1030                 :          39 :     case SIGN_EXTEND:
    1031                 :          39 :       if (!iv_extend (&iv0, IV_SIGN_EXTEND, mode))
    1032                 :             :         return false;
    1033                 :             :       break;
    1034                 :             : 
    1035                 :         540 :     case ZERO_EXTEND:
    1036                 :         540 :       if (!iv_extend (&iv0, IV_ZERO_EXTEND, mode))
    1037                 :             :         return false;
    1038                 :             :       break;
    1039                 :             : 
    1040                 :          82 :     case NEG:
    1041                 :          82 :       if (!iv_neg (&iv0))
    1042                 :             :         return false;
    1043                 :             :       break;
    1044                 :             : 
    1045                 :      446048 :     case PLUS:
    1046                 :      446048 :     case MINUS:
    1047                 :      446048 :       if (!iv_add (&iv0, &iv1, code))
    1048                 :             :         return false;
    1049                 :             :       break;
    1050                 :             : 
    1051                 :          32 :     case MULT:
    1052                 :          32 :       if (!iv_mult (&iv0, mby))
    1053                 :             :         return false;
    1054                 :             :       break;
    1055                 :             : 
    1056                 :         188 :     case ASHIFT:
    1057                 :         188 :       if (!iv_shift (&iv0, mby))
    1058                 :             :         return false;
    1059                 :             :       break;
    1060                 :             : 
    1061                 :             :     default:
    1062                 :             :       break;
    1063                 :             :     }
    1064                 :             : 
    1065                 :      446358 :   *iv = iv0;
    1066                 :      446358 :   return iv->base != NULL_RTX;
    1067                 :             : }
    1068                 :             : 
    1069                 :             : /* Analyzes iv DEF and stores the result to *IV.  */
    1070                 :             : 
    1071                 :             : static bool
    1072                 :      671833 : iv_analyze_def (df_ref def, class rtx_iv *iv)
    1073                 :             : {
    1074                 :      671833 :   rtx_insn *insn = DF_REF_INSN (def);
    1075                 :      671833 :   rtx reg = DF_REF_REG (def);
    1076                 :      671833 :   rtx set, rhs;
    1077                 :             : 
    1078                 :      671833 :   if (dump_file)
    1079                 :             :     {
    1080                 :         203 :       fprintf (dump_file, "Analyzing def of ");
    1081                 :         203 :       print_rtl (dump_file, reg);
    1082                 :         203 :       fprintf (dump_file, " in insn ");
    1083                 :         203 :       print_rtl_single (dump_file, insn);
    1084                 :             :     }
    1085                 :             : 
    1086                 :      671833 :   check_iv_ref_table_size ();
    1087                 :      671833 :   if (DF_REF_IV (def))
    1088                 :             :     {
    1089                 :        7552 :       if (dump_file)
    1090                 :           0 :         fprintf (dump_file, "  already analysed.\n");
    1091                 :        7552 :       *iv = *DF_REF_IV (def);
    1092                 :        7552 :       return iv->base != NULL_RTX;
    1093                 :             :     }
    1094                 :             : 
    1095                 :      664281 :   iv->base = NULL_RTX;
    1096                 :      664281 :   iv->step = NULL_RTX;
    1097                 :             : 
    1098                 :      664281 :   scalar_int_mode mode;
    1099                 :      664642 :   if (!REG_P (reg) || !is_a <scalar_int_mode> (GET_MODE (reg), &mode))
    1100                 :             :     return false;
    1101                 :             : 
    1102                 :      664126 :   set = single_set (insn);
    1103                 :      664126 :   if (!set)
    1104                 :             :     return false;
    1105                 :             : 
    1106                 :      663765 :   if (!REG_P (SET_DEST (set)))
    1107                 :             :     return false;
    1108                 :             : 
    1109                 :      663765 :   gcc_assert (SET_DEST (set) == reg);
    1110                 :      663765 :   rhs = find_reg_equal_equiv_note (insn);
    1111                 :      663765 :   if (rhs)
    1112                 :        7249 :     rhs = XEXP (rhs, 0);
    1113                 :             :   else
    1114                 :      656516 :     rhs = SET_SRC (set);
    1115                 :             : 
    1116                 :      663765 :   iv_analyze_expr (insn, mode, rhs, iv);
    1117                 :      663765 :   record_iv (def, iv);
    1118                 :             : 
    1119                 :      663765 :   if (dump_file)
    1120                 :             :     {
    1121                 :         203 :       print_rtl (dump_file, reg);
    1122                 :         203 :       fprintf (dump_file, " in insn ");
    1123                 :         203 :       print_rtl_single (dump_file, insn);
    1124                 :         203 :       fprintf (dump_file, "  is ");
    1125                 :         203 :       dump_iv_info (dump_file, iv);
    1126                 :         203 :       fprintf (dump_file, "\n");
    1127                 :             :     }
    1128                 :             : 
    1129                 :      663765 :   return iv->base != NULL_RTX;
    1130                 :             : }
    1131                 :             : 
    1132                 :             : /* Analyzes operand OP of INSN and stores the result to *IV.  MODE is the
    1133                 :             :    mode of OP.  */
    1134                 :             : 
    1135                 :             : static bool
    1136                 :     2174082 : iv_analyze_op (rtx_insn *insn, scalar_int_mode mode, rtx op, class rtx_iv *iv)
    1137                 :             : {
    1138                 :     2174082 :   df_ref def = NULL;
    1139                 :     2174082 :   enum iv_grd_result res;
    1140                 :             : 
    1141                 :     2174082 :   if (dump_file)
    1142                 :             :     {
    1143                 :         513 :       fprintf (dump_file, "Analyzing operand ");
    1144                 :         513 :       print_rtl (dump_file, op);
    1145                 :         513 :       fprintf (dump_file, " of insn ");
    1146                 :         513 :       print_rtl_single (dump_file, insn);
    1147                 :             :     }
    1148                 :             : 
    1149                 :     2174082 :   if (function_invariant_p (op))
    1150                 :             :     res = GRD_INVARIANT;
    1151                 :     1552527 :   else if (GET_CODE (op) == SUBREG)
    1152                 :             :     {
    1153                 :       13818 :       scalar_int_mode inner_mode;
    1154                 :       13818 :       if (!subreg_lowpart_p (op)
    1155                 :       21700 :           || !is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op)), &inner_mode))
    1156                 :             :         return false;
    1157                 :             : 
    1158                 :       13329 :       if (!iv_analyze_op (insn, inner_mode, SUBREG_REG (op), iv))
    1159                 :             :         return false;
    1160                 :             : 
    1161                 :        5447 :       return iv_subreg (iv, mode);
    1162                 :             :     }
    1163                 :             :   else
    1164                 :             :     {
    1165                 :     1538709 :       res = iv_get_reaching_def (insn, op, &def);
    1166                 :     1538709 :       if (res == GRD_INVALID)
    1167                 :             :         {
    1168                 :      117345 :           if (dump_file)
    1169                 :           6 :             fprintf (dump_file, "  not simple.\n");
    1170                 :      117345 :           return false;
    1171                 :             :         }
    1172                 :             :     }
    1173                 :             : 
    1174                 :     1421364 :   if (res == GRD_INVARIANT)
    1175                 :             :     {
    1176                 :      901804 :       iv_constant (iv, mode, op);
    1177                 :             : 
    1178                 :      901804 :       if (dump_file)
    1179                 :             :         {
    1180                 :         221 :           fprintf (dump_file, "  ");
    1181                 :         221 :           dump_iv_info (dump_file, iv);
    1182                 :         221 :           fprintf (dump_file, "\n");
    1183                 :             :         }
    1184                 :      901804 :       return true;
    1185                 :             :     }
    1186                 :             : 
    1187                 :     1141115 :   if (res == GRD_MAYBE_BIV)
    1188                 :      521490 :     return iv_analyze_biv (mode, op, iv);
    1189                 :             : 
    1190                 :      619625 :   return iv_analyze_def (def, iv);
    1191                 :             : }
    1192                 :             : 
    1193                 :             : /* Analyzes value VAL at INSN and stores the result to *IV.  MODE is the
    1194                 :             :    mode of VAL.  */
    1195                 :             : 
    1196                 :             : bool
    1197                 :     1185018 : iv_analyze (rtx_insn *insn, scalar_int_mode mode, rtx val, class rtx_iv *iv)
    1198                 :             : {
    1199                 :     1185018 :   rtx reg;
    1200                 :             : 
    1201                 :             :   /* We must find the insn in that val is used, so that we get to UD chains.
    1202                 :             :      Since the function is sometimes called on result of get_condition,
    1203                 :             :      this does not necessarily have to be directly INSN; scan also the
    1204                 :             :      following insns.  */
    1205                 :     1185018 :   if (simple_reg_p (val))
    1206                 :             :     {
    1207                 :      949431 :       if (GET_CODE (val) == SUBREG)
    1208                 :       12007 :         reg = SUBREG_REG (val);
    1209                 :             :       else
    1210                 :      949431 :         reg = val;
    1211                 :             : 
    1212                 :      949431 :       while (!df_find_use (insn, reg))
    1213                 :           0 :         insn = NEXT_INSN (insn);
    1214                 :             :     }
    1215                 :             : 
    1216                 :     1185018 :   return iv_analyze_op (insn, mode, val, iv);
    1217                 :             : }
    1218                 :             : 
    1219                 :             : /* Analyzes definition of DEF in INSN and stores the result to IV.  */
    1220                 :             : 
    1221                 :             : bool
    1222                 :       52208 : iv_analyze_result (rtx_insn *insn, rtx def, class rtx_iv *iv)
    1223                 :             : {
    1224                 :       52208 :   df_ref adef;
    1225                 :             : 
    1226                 :       52208 :   adef = df_find_def (insn, def);
    1227                 :       52208 :   if (!adef)
    1228                 :             :     return false;
    1229                 :             : 
    1230                 :       52208 :   return iv_analyze_def (adef, iv);
    1231                 :             : }
    1232                 :             : 
    1233                 :             : /* Checks whether definition of register REG in INSN is a basic induction
    1234                 :             :    variable.  MODE is the mode of REG.
    1235                 :             : 
    1236                 :             :    IV analysis must have been initialized (via a call to
    1237                 :             :    iv_analysis_loop_init) for this function to produce a result.  */
    1238                 :             : 
    1239                 :             : bool
    1240                 :       87856 : biv_p (rtx_insn *insn, scalar_int_mode mode, rtx reg)
    1241                 :             : {
    1242                 :       87856 :   class rtx_iv iv;
    1243                 :       87856 :   df_ref def, last_def;
    1244                 :             : 
    1245                 :       87856 :   if (!simple_reg_p (reg))
    1246                 :             :     return false;
    1247                 :             : 
    1248                 :       83759 :   def = df_find_def (insn, reg);
    1249                 :       83759 :   gcc_assert (def != NULL);
    1250                 :       83759 :   if (!latch_dominating_def (reg, &last_def))
    1251                 :             :     return false;
    1252                 :       83736 :   if (last_def != def)
    1253                 :             :     return false;
    1254                 :             : 
    1255                 :       63060 :   if (!iv_analyze_biv (mode, reg, &iv))
    1256                 :             :     return false;
    1257                 :             : 
    1258                 :       52209 :   return iv.step != const0_rtx;
    1259                 :             : }
    1260                 :             : 
    1261                 :             : /* Calculates value of IV at ITERATION-th iteration.  */
    1262                 :             : 
    1263                 :             : rtx
    1264                 :          56 : get_iv_value (class rtx_iv *iv, rtx iteration)
    1265                 :             : {
    1266                 :          56 :   rtx val;
    1267                 :             : 
    1268                 :             :   /* We would need to generate some if_then_else patterns, and so far
    1269                 :             :      it is not needed anywhere.  */
    1270                 :          56 :   gcc_assert (!iv->first_special);
    1271                 :             : 
    1272                 :          56 :   if (iv->step != const0_rtx && iteration != const0_rtx)
    1273                 :           0 :     val = simplify_gen_binary (PLUS, iv->extend_mode, iv->base,
    1274                 :             :                                simplify_gen_binary (MULT, iv->extend_mode,
    1275                 :             :                                                     iv->step, iteration));
    1276                 :             :   else
    1277                 :          56 :     val = iv->base;
    1278                 :             : 
    1279                 :          56 :   if (iv->extend_mode == iv->mode)
    1280                 :             :     return val;
    1281                 :             : 
    1282                 :           0 :   val = lowpart_subreg (iv->mode, val, iv->extend_mode);
    1283                 :             : 
    1284                 :           0 :   if (iv->extend == IV_UNKNOWN_EXTEND)
    1285                 :             :     return val;
    1286                 :             : 
    1287                 :           0 :   val = simplify_gen_unary (iv_extend_to_rtx_code (iv->extend),
    1288                 :             :                             iv->extend_mode, val, iv->mode);
    1289                 :           0 :   val = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
    1290                 :             :                              simplify_gen_binary (MULT, iv->extend_mode,
    1291                 :             :                                                   iv->mult, val));
    1292                 :             : 
    1293                 :           0 :   return val;
    1294                 :             : }
    1295                 :             : 
    1296                 :             : /* Free the data for an induction variable analysis.  */
    1297                 :             : 
    1298                 :             : void
    1299                 :      216107 : iv_analysis_done (void)
    1300                 :             : {
    1301                 :      216107 :   if (!clean_slate)
    1302                 :             :     {
    1303                 :      183702 :       clear_iv_info ();
    1304                 :      183702 :       clean_slate = true;
    1305                 :      183702 :       df_finish_pass (true);
    1306                 :      183702 :       delete bivs;
    1307                 :      183702 :       bivs = NULL;
    1308                 :      183702 :       free (iv_ref_table);
    1309                 :      183702 :       iv_ref_table = NULL;
    1310                 :      183702 :       iv_ref_table_size = 0;
    1311                 :             :     }
    1312                 :      216107 : }
    1313                 :             : 
    1314                 :             : /* Computes inverse to X modulo (1 << MOD).  */
    1315                 :             : 
    1316                 :             : static uint64_t
    1317                 :      389834 : inverse (uint64_t x, int mod)
    1318                 :             : {
    1319                 :      389834 :   uint64_t mask =
    1320                 :      389834 :           ((uint64_t) 1 << (mod - 1) << 1) - 1;
    1321                 :      389834 :   uint64_t rslt = 1;
    1322                 :      389834 :   int i;
    1323                 :             : 
    1324                 :    21733044 :   for (i = 0; i < mod - 1; i++)
    1325                 :             :     {
    1326                 :    21343210 :       rslt = (rslt * x) & mask;
    1327                 :    21343210 :       x = (x * x) & mask;
    1328                 :             :     }
    1329                 :             : 
    1330                 :      389834 :   return rslt;
    1331                 :             : }
    1332                 :             : 
    1333                 :             : /* Checks whether any register in X is in set ALT.  */
    1334                 :             : 
    1335                 :             : static bool
    1336                 :    40886662 : altered_reg_used (const_rtx x, bitmap alt)
    1337                 :             : {
    1338                 :    40886662 :   subrtx_iterator::array_type array;
    1339                 :   254977004 :   FOR_EACH_SUBRTX (iter, array, x, NONCONST)
    1340                 :             :     {
    1341                 :   214836855 :       const_rtx x = *iter;
    1342                 :   214836855 :       if (REG_P (x) && REGNO_REG_SET_P (alt, REGNO (x)))
    1343                 :      746513 :         return true;
    1344                 :             :     }
    1345                 :    40140149 :   return false;
    1346                 :    40886662 : }
    1347                 :             : 
    1348                 :             : /* Marks registers altered by EXPR in set ALT.  */
    1349                 :             : 
    1350                 :             : static void
    1351                 :    10196174 : mark_altered (rtx expr, const_rtx by ATTRIBUTE_UNUSED, void *alt)
    1352                 :             : {
    1353                 :    10196174 :   if (GET_CODE (expr) == SUBREG)
    1354                 :           0 :     expr = SUBREG_REG (expr);
    1355                 :    10196174 :   if (!REG_P (expr))
    1356                 :             :     return;
    1357                 :             : 
    1358                 :     8882289 :   SET_REGNO_REG_SET ((bitmap) alt, REGNO (expr));
    1359                 :             : }
    1360                 :             : 
    1361                 :             : /* Checks whether RHS is simple enough to process.  */
    1362                 :             : 
    1363                 :             : static bool
    1364                 :     6723462 : simple_rhs_p (rtx rhs)
    1365                 :             : {
    1366                 :     6723462 :   rtx op0, op1;
    1367                 :             : 
    1368                 :     6723462 :   if (function_invariant_p (rhs)
    1369                 :     6723462 :       || (REG_P (rhs) && !HARD_REGISTER_P (rhs)))
    1370                 :             :     return true;
    1371                 :             : 
    1372                 :     4411813 :   switch (GET_CODE (rhs))
    1373                 :             :     {
    1374                 :     1367843 :     case PLUS:
    1375                 :     1367843 :     case MINUS:
    1376                 :     1367843 :     case AND:
    1377                 :     1367843 :       op0 = XEXP (rhs, 0);
    1378                 :     1367843 :       op1 = XEXP (rhs, 1);
    1379                 :             :       /* Allow reg OP const and reg OP reg.  */
    1380                 :     1264494 :       if (!(REG_P (op0) && !HARD_REGISTER_P (op0))
    1381                 :     1439575 :           && !function_invariant_p (op0))
    1382                 :             :         return false;
    1383                 :      596434 :       if (!(REG_P (op1) && !HARD_REGISTER_P (op1))
    1384                 :     1201487 :           && !function_invariant_p (op1))
    1385                 :             :         return false;
    1386                 :             : 
    1387                 :             :       return true;
    1388                 :             : 
    1389                 :      610351 :     case ASHIFT:
    1390                 :      610351 :     case ASHIFTRT:
    1391                 :      610351 :     case LSHIFTRT:
    1392                 :      610351 :     case MULT:
    1393                 :      610351 :       op0 = XEXP (rhs, 0);
    1394                 :      610351 :       op1 = XEXP (rhs, 1);
    1395                 :             :       /* Allow reg OP const.  */
    1396                 :      610351 :       if (!(REG_P (op0) && !HARD_REGISTER_P (op0)))
    1397                 :             :         return false;
    1398                 :      593678 :       if (!function_invariant_p (op1))
    1399                 :             :         return false;
    1400                 :             : 
    1401                 :             :       return true;
    1402                 :             : 
    1403                 :             :     default:
    1404                 :             :       return false;
    1405                 :             :     }
    1406                 :             : }
    1407                 :             : 
    1408                 :             : /* If any registers in *EXPR that have a single definition, try to replace
    1409                 :             :    them with the known-equivalent values.  */
    1410                 :             : 
    1411                 :             : static void
    1412                 :     2423211 : replace_single_def_regs (rtx *expr)
    1413                 :             : {
    1414                 :     2423211 :   subrtx_var_iterator::array_type array;
    1415                 :     2494752 :  repeat:
    1416                 :    15509394 :   FOR_EACH_SUBRTX_VAR (iter, array, *expr, NONCONST)
    1417                 :             :     {
    1418                 :    13086183 :       rtx x = *iter;
    1419                 :    13086183 :       if (REG_P (x))
    1420                 :     3684161 :         if (rtx new_x = df_find_single_def_src (x))
    1421                 :             :           {
    1422                 :       71541 :             *expr = simplify_replace_rtx (*expr, x, new_x);
    1423                 :       71541 :             goto repeat;
    1424                 :             :           }
    1425                 :             :     }
    1426                 :     2423211 : }
    1427                 :             : 
    1428                 :             : /* A subroutine of simplify_using_initial_values, this function examines INSN
    1429                 :             :    to see if it contains a suitable set that we can use to make a replacement.
    1430                 :             :    If it is suitable, return true and set DEST and SRC to the lhs and rhs of
    1431                 :             :    the set; return false otherwise.  */
    1432                 :             : 
    1433                 :             : static bool
    1434                 :    15676102 : suitable_set_for_replacement (rtx_insn *insn, rtx *dest, rtx *src)
    1435                 :             : {
    1436                 :    15676102 :   rtx set = single_set (insn);
    1437                 :    15676102 :   rtx lhs = NULL_RTX, rhs;
    1438                 :             : 
    1439                 :    15676102 :   if (!set)
    1440                 :             :     return false;
    1441                 :             : 
    1442                 :     8035866 :   lhs = SET_DEST (set);
    1443                 :     8035866 :   if (!REG_P (lhs))
    1444                 :             :     return false;
    1445                 :             : 
    1446                 :     6723462 :   rhs = find_reg_equal_equiv_note (insn);
    1447                 :     6723462 :   if (rhs)
    1448                 :      419022 :     rhs = XEXP (rhs, 0);
    1449                 :             :   else
    1450                 :     6304440 :     rhs = SET_SRC (set);
    1451                 :             : 
    1452                 :     6723462 :   if (!simple_rhs_p (rhs))
    1453                 :             :     return false;
    1454                 :             : 
    1455                 :     4014273 :   *dest = lhs;
    1456                 :     4014273 :   *src = rhs;
    1457                 :     4014273 :   return true;
    1458                 :             : }
    1459                 :             : 
    1460                 :             : /* Using the data returned by suitable_set_for_replacement, replace DEST
    1461                 :             :    with SRC in *EXPR and return the new expression.  Also call
    1462                 :             :    replace_single_def_regs if the replacement changed something.  */
    1463                 :             : static void
    1464                 :     5303098 : replace_in_expr (rtx *expr, rtx dest, rtx src)
    1465                 :             : {
    1466                 :     5303098 :   rtx old = *expr;
    1467                 :     5303098 :   *expr = simplify_replace_rtx (*expr, dest, src);
    1468                 :     5303098 :   if (old == *expr)
    1469                 :             :     return;
    1470                 :     1238783 :   replace_single_def_regs (expr);
    1471                 :             : }
    1472                 :             : 
    1473                 :             : /* Checks whether A implies B.  */
    1474                 :             : 
    1475                 :             : static bool
    1476                 :     2173003 : implies_p (rtx a, rtx b)
    1477                 :             : {
    1478                 :     2173003 :   rtx op0, op1, opb0, opb1;
    1479                 :     2173003 :   machine_mode mode;
    1480                 :             : 
    1481                 :     2173003 :   if (rtx_equal_p (a, b))
    1482                 :             :     return true;
    1483                 :             : 
    1484                 :     2169882 :   if (GET_CODE (a) == EQ)
    1485                 :             :     {
    1486                 :      405647 :       op0 = XEXP (a, 0);
    1487                 :      405647 :       op1 = XEXP (a, 1);
    1488                 :             : 
    1489                 :      405647 :       if (REG_P (op0)
    1490                 :      221281 :           || (GET_CODE (op0) == SUBREG
    1491                 :        4352 :               && REG_P (SUBREG_REG (op0))))
    1492                 :             :         {
    1493                 :      187596 :           rtx r = simplify_replace_rtx (b, op0, op1);
    1494                 :      187596 :           if (r == const_true_rtx)
    1495                 :             :             return true;
    1496                 :             :         }
    1497                 :             : 
    1498                 :      388615 :       if (REG_P (op1)
    1499                 :      303702 :           || (GET_CODE (op1) == SUBREG
    1500                 :        1390 :               && REG_P (SUBREG_REG (op1))))
    1501                 :             :         {
    1502                 :       86303 :           rtx r = simplify_replace_rtx (b, op1, op0);
    1503                 :       86303 :           if (r == const_true_rtx)
    1504                 :             :             return true;
    1505                 :             :         }
    1506                 :             :     }
    1507                 :             : 
    1508                 :     2152826 :   if (b == const_true_rtx)
    1509                 :             :     return true;
    1510                 :             : 
    1511                 :     2152826 :   if ((GET_RTX_CLASS (GET_CODE (a)) != RTX_COMM_COMPARE
    1512                 :             :        && GET_RTX_CLASS (GET_CODE (a)) != RTX_COMPARE)
    1513                 :     2145194 :       || (GET_RTX_CLASS (GET_CODE (b)) != RTX_COMM_COMPARE
    1514                 :             :           && GET_RTX_CLASS (GET_CODE (b)) != RTX_COMPARE))
    1515                 :             :     return false;
    1516                 :             : 
    1517                 :     2124600 :   op0 = XEXP (a, 0);
    1518                 :     2124600 :   op1 = XEXP (a, 1);
    1519                 :     2124600 :   opb0 = XEXP (b, 0);
    1520                 :     2124600 :   opb1 = XEXP (b, 1);
    1521                 :             : 
    1522                 :     2124600 :   mode = GET_MODE (op0);
    1523                 :     2124600 :   if (mode != GET_MODE (opb0))
    1524                 :             :     mode = VOIDmode;
    1525                 :     1842566 :   else if (mode == VOIDmode)
    1526                 :             :     {
    1527                 :           0 :       mode = GET_MODE (op1);
    1528                 :           0 :       if (mode != GET_MODE (opb1))
    1529                 :      282034 :         mode = VOIDmode;
    1530                 :             :     }
    1531                 :             : 
    1532                 :             :   /* A < B implies A + 1 <= B.  */
    1533                 :     2124600 :   if ((GET_CODE (a) == GT || GET_CODE (a) == LT)
    1534                 :      466461 :       && (GET_CODE (b) == GE || GET_CODE (b) == LE))
    1535                 :             :     {
    1536                 :             : 
    1537                 :       32742 :       if (GET_CODE (a) == GT)
    1538                 :       19754 :         std::swap (op0, op1);
    1539                 :             : 
    1540                 :       32742 :       if (GET_CODE (b) == GE)
    1541                 :       12898 :         std::swap (opb0, opb1);
    1542                 :             : 
    1543                 :       32742 :       if (SCALAR_INT_MODE_P (mode)
    1544                 :       32091 :           && rtx_equal_p (op1, opb1)
    1545                 :       35306 :           && simplify_gen_binary (MINUS, mode, opb0, op0) == const1_rtx)
    1546                 :             :         return true;
    1547                 :       32501 :       return false;
    1548                 :             :     }
    1549                 :             : 
    1550                 :             :   /* A < B or A > B imply A != B.  TODO: Likewise
    1551                 :             :      A + n < B implies A != B + n if neither wraps.  */
    1552                 :     2091858 :   if (GET_CODE (b) == NE
    1553                 :             :       && (GET_CODE (a) == GT || GET_CODE (a) == GTU
    1554                 :             :           || GET_CODE (a) == LT || GET_CODE (a) == LTU))
    1555                 :             :     {
    1556                 :      163560 :       if (rtx_equal_p (op0, opb0)
    1557                 :      163560 :           && rtx_equal_p (op1, opb1))
    1558                 :             :         return true;
    1559                 :             :     }
    1560                 :             : 
    1561                 :             :   /* For unsigned comparisons, A != 0 implies A > 0 and A >= 1.  */
    1562                 :     2065774 :   if (GET_CODE (a) == NE
    1563                 :      521810 :       && op1 == const0_rtx)
    1564                 :             :     {
    1565                 :      301725 :       if ((GET_CODE (b) == GTU
    1566                 :       22906 :            && opb1 == const0_rtx)
    1567                 :      301725 :           || (GET_CODE (b) == GEU
    1568                 :        6633 :               && opb1 == const1_rtx))
    1569                 :           0 :         return rtx_equal_p (op0, opb0);
    1570                 :             :     }
    1571                 :             : 
    1572                 :             :   /* A != N is equivalent to A - (N + 1) <u -1.  */
    1573                 :     2065774 :   if (GET_CODE (a) == NE
    1574                 :      521810 :       && CONST_INT_P (op1)
    1575                 :      383786 :       && GET_CODE (b) == LTU
    1576                 :       66373 :       && opb1 == constm1_rtx
    1577                 :       23504 :       && GET_CODE (opb0) == PLUS
    1578                 :        7011 :       && CONST_INT_P (XEXP (opb0, 1))
    1579                 :             :       /* Avoid overflows.  */
    1580                 :         384 :       && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
    1581                 :             :           != (HOST_WIDE_INT_1U
    1582                 :             :               << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
    1583                 :         384 :       && INTVAL (XEXP (opb0, 1)) + 1 == -INTVAL (op1))
    1584                 :         191 :     return rtx_equal_p (op0, XEXP (opb0, 0));
    1585                 :             : 
    1586                 :             :   /* Likewise, A != N implies A - N > 0.  */
    1587                 :     2065583 :   if (GET_CODE (a) == NE
    1588                 :      521619 :       && CONST_INT_P (op1))
    1589                 :             :     {
    1590                 :      383595 :       if (GET_CODE (b) == GTU
    1591                 :       36883 :           && GET_CODE (opb0) == PLUS
    1592                 :       17330 :           && opb1 == const0_rtx
    1593                 :           0 :           && CONST_INT_P (XEXP (opb0, 1))
    1594                 :             :           /* Avoid overflows.  */
    1595                 :           0 :           && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
    1596                 :             :               != (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1)))
    1597                 :      383595 :           && rtx_equal_p (XEXP (opb0, 0), op0))
    1598                 :           0 :         return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
    1599                 :      383595 :       if (GET_CODE (b) == GEU
    1600                 :        9001 :           && GET_CODE (opb0) == PLUS
    1601                 :        2883 :           && opb1 == const1_rtx
    1602                 :           0 :           && CONST_INT_P (XEXP (opb0, 1))
    1603                 :             :           /* Avoid overflows.  */
    1604                 :           0 :           && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
    1605                 :             :               != (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1)))
    1606                 :      383595 :           && rtx_equal_p (XEXP (opb0, 0), op0))
    1607                 :           0 :         return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
    1608                 :             :     }
    1609                 :             : 
    1610                 :             :   /* A >s X, where X is positive, implies A <u Y, if Y is negative.  */
    1611                 :     2065583 :   if ((GET_CODE (a) == GT || GET_CODE (a) == GE)
    1612                 :      290784 :       && CONST_INT_P (op1)
    1613                 :      208338 :       && ((GET_CODE (a) == GT && op1 == constm1_rtx)
    1614                 :      181108 :           || INTVAL (op1) >= 0)
    1615                 :      197714 :       && GET_CODE (b) == LTU
    1616                 :       12013 :       && CONST_INT_P (opb1)
    1617                 :     2077240 :       && rtx_equal_p (op0, opb0))
    1618                 :        1006 :     return INTVAL (opb1) < 0;
    1619                 :             : 
    1620                 :             :   return false;
    1621                 :             : }
    1622                 :             : 
    1623                 :             : /* Canonicalizes COND so that
    1624                 :             : 
    1625                 :             :    (1) Ensure that operands are ordered according to
    1626                 :             :        swap_commutative_operands_p.
    1627                 :             :    (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
    1628                 :             :        for GE, GEU, and LEU.  */
    1629                 :             : 
    1630                 :             : rtx
    1631                 :     2125611 : canon_condition (rtx cond)
    1632                 :             : {
    1633                 :     2125611 :   rtx op0, op1;
    1634                 :     2125611 :   enum rtx_code code;
    1635                 :     2125611 :   machine_mode mode;
    1636                 :             : 
    1637                 :     2125611 :   code = GET_CODE (cond);
    1638                 :     2125611 :   op0 = XEXP (cond, 0);
    1639                 :     2125611 :   op1 = XEXP (cond, 1);
    1640                 :             : 
    1641                 :     2125611 :   if (swap_commutative_operands_p (op0, op1))
    1642                 :             :     {
    1643                 :      208478 :       code = swap_condition (code);
    1644                 :      208478 :       std::swap (op0, op1);
    1645                 :             :     }
    1646                 :             : 
    1647                 :     2125611 :   mode = GET_MODE (op0);
    1648                 :     2125611 :   if (mode == VOIDmode)
    1649                 :           0 :     mode = GET_MODE (op1);
    1650                 :     2125611 :   gcc_assert (mode != VOIDmode);
    1651                 :             : 
    1652                 :     2125611 :   if (CONST_SCALAR_INT_P (op1) && GET_MODE_CLASS (mode) != MODE_CC)
    1653                 :             :     {
    1654                 :     1530332 :       rtx_mode_t const_val (op1, mode);
    1655                 :             : 
    1656                 :     1530332 :       switch (code)
    1657                 :             :         {
    1658                 :       84881 :         case LE:
    1659                 :       84881 :           if (wi::ne_p (const_val, wi::max_value (mode, SIGNED)))
    1660                 :             :             {
    1661                 :       84881 :               code = LT;
    1662                 :       84881 :               op1 = immed_wide_int_const (wi::add (const_val, 1),  mode);
    1663                 :             :             }
    1664                 :             :           break;
    1665                 :             : 
    1666                 :      115324 :         case GE:
    1667                 :      115324 :           if (wi::ne_p (const_val, wi::min_value (mode, SIGNED)))
    1668                 :             :             {
    1669                 :      115324 :               code = GT;
    1670                 :      115324 :               op1 = immed_wide_int_const (wi::sub (const_val, 1), mode);
    1671                 :             :             }
    1672                 :             :           break;
    1673                 :             : 
    1674                 :       40211 :         case LEU:
    1675                 :       40211 :           if (wi::ne_p (const_val, -1))
    1676                 :             :             {
    1677                 :       40211 :               code = LTU;
    1678                 :       40211 :               op1 = immed_wide_int_const (wi::add (const_val, 1), mode);
    1679                 :             :             }
    1680                 :             :           break;
    1681                 :             : 
    1682                 :      170916 :         case GEU:
    1683                 :      170916 :           if (wi::ne_p (const_val, 0))
    1684                 :             :             {
    1685                 :      170916 :               code = GTU;
    1686                 :      170916 :               op1 = immed_wide_int_const (wi::sub (const_val, 1), mode);
    1687                 :             :             }
    1688                 :             :           break;
    1689                 :             : 
    1690                 :             :         default:
    1691                 :             :           break;
    1692                 :             :         }
    1693                 :             :     }
    1694                 :             : 
    1695                 :     2125611 :   if (op0 != XEXP (cond, 0)
    1696                 :     1917133 :       || op1 != XEXP (cond, 1)
    1697                 :     1565541 :       || code != GET_CODE (cond)
    1698                 :     1565541 :       || GET_MODE (cond) != SImode)
    1699                 :     1583133 :     cond = gen_rtx_fmt_ee (code, SImode, op0, op1);
    1700                 :             : 
    1701                 :     2125611 :   return cond;
    1702                 :             : }
    1703                 :             : 
    1704                 :             : /* Reverses CONDition; returns NULL if we cannot.  */
    1705                 :             : 
    1706                 :             : static rtx
    1707                 :     1845495 : reversed_condition (rtx cond)
    1708                 :             : {
    1709                 :     1845495 :   enum rtx_code reversed;
    1710                 :     1845495 :   reversed = reversed_comparison_code (cond, NULL);
    1711                 :     1845495 :   if (reversed == UNKNOWN)
    1712                 :             :     return NULL_RTX;
    1713                 :             :   else
    1714                 :     1837518 :     return gen_rtx_fmt_ee (reversed,
    1715                 :             :                            GET_MODE (cond), XEXP (cond, 0),
    1716                 :             :                            XEXP (cond, 1));
    1717                 :             : }
    1718                 :             : 
    1719                 :             : /* Tries to use the fact that COND holds to simplify EXPR.  ALTERED is the
    1720                 :             :    set of altered regs.  */
    1721                 :             : 
    1722                 :             : void
    1723                 :     1288043 : simplify_using_condition (rtx cond, rtx *expr, regset altered)
    1724                 :             : {
    1725                 :     1288043 :   rtx rev, reve, exp = *expr;
    1726                 :             : 
    1727                 :             :   /* If some register gets altered later, we do not really speak about its
    1728                 :             :      value at the time of comparison.  */
    1729                 :     1288043 :   if (altered && altered_reg_used (cond, altered))
    1730                 :             :     return;
    1731                 :             : 
    1732                 :     1270762 :   if (GET_CODE (cond) == EQ
    1733                 :      221372 :       && REG_P (XEXP (cond, 0)) && CONSTANT_P (XEXP (cond, 1)))
    1734                 :             :     {
    1735                 :       79734 :       *expr = simplify_replace_rtx (*expr, XEXP (cond, 0), XEXP (cond, 1));
    1736                 :       79734 :       return;
    1737                 :             :     }
    1738                 :             : 
    1739                 :     1191028 :   if (!COMPARISON_P (exp))
    1740                 :             :     return;
    1741                 :             : 
    1742                 :      531738 :   rev = reversed_condition (cond);
    1743                 :      531738 :   reve = reversed_condition (exp);
    1744                 :             : 
    1745                 :      531738 :   cond = canon_condition (cond);
    1746                 :      531738 :   exp = canon_condition (exp);
    1747                 :      531738 :   if (rev)
    1748                 :      530397 :     rev = canon_condition (rev);
    1749                 :      531738 :   if (reve)
    1750                 :      531738 :     reve = canon_condition (reve);
    1751                 :             : 
    1752                 :      531738 :   if (rtx_equal_p (exp, cond))
    1753                 :             :     {
    1754                 :        5724 :       *expr = const_true_rtx;
    1755                 :        5724 :       return;
    1756                 :             :     }
    1757                 :             : 
    1758                 :      526014 :   if (rev && rtx_equal_p (exp, rev))
    1759                 :             :     {
    1760                 :        1418 :       *expr = const0_rtx;
    1761                 :        1418 :       return;
    1762                 :             :     }
    1763                 :             : 
    1764                 :      524596 :   if (implies_p (cond, exp))
    1765                 :             :     {
    1766                 :       27227 :       *expr = const_true_rtx;
    1767                 :       27227 :       return;
    1768                 :             :     }
    1769                 :             : 
    1770                 :      497369 :   if (reve && implies_p (cond, reve))
    1771                 :             :     {
    1772                 :         288 :       *expr = const0_rtx;
    1773                 :         288 :       return;
    1774                 :             :     }
    1775                 :             : 
    1776                 :             :   /* A proof by contradiction.  If *EXPR implies (not cond), *EXPR must
    1777                 :             :      be false.  */
    1778                 :      497081 :   if (rev && implies_p (exp, rev))
    1779                 :             :     {
    1780                 :        5748 :       *expr = const0_rtx;
    1781                 :        5748 :       return;
    1782                 :             :     }
    1783                 :             : 
    1784                 :             :   /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true.  */
    1785                 :      491333 :   if (rev && reve && implies_p (reve, rev))
    1786                 :             :     {
    1787                 :        1468 :       *expr = const_true_rtx;
    1788                 :        1468 :       return;
    1789                 :             :     }
    1790                 :             : 
    1791                 :             :   /* We would like to have some other tests here.  TODO.  */
    1792                 :             : 
    1793                 :             :   return;
    1794                 :             : }
    1795                 :             : 
    1796                 :             : /* Use relationship between A and *B to eventually eliminate *B.
    1797                 :             :    OP is the operation we consider.  */
    1798                 :             : 
    1799                 :             : static void
    1800                 :      165306 : eliminate_implied_condition (enum rtx_code op, rtx a, rtx *b)
    1801                 :             : {
    1802                 :      165306 :   switch (op)
    1803                 :             :     {
    1804                 :           0 :     case AND:
    1805                 :             :       /* If A implies *B, we may replace *B by true.  */
    1806                 :           0 :       if (implies_p (a, *b))
    1807                 :           0 :         *b = const_true_rtx;
    1808                 :             :       break;
    1809                 :             : 
    1810                 :      165306 :     case IOR:
    1811                 :             :       /* If *B implies A, we may replace *B by false.  */
    1812                 :      165306 :       if (implies_p (*b, a))
    1813                 :       12962 :         *b = const0_rtx;
    1814                 :             :       break;
    1815                 :             : 
    1816                 :           0 :     default:
    1817                 :           0 :       gcc_unreachable ();
    1818                 :             :     }
    1819                 :      165306 : }
    1820                 :             : 
    1821                 :             : /* Eliminates the conditions in TAIL that are implied by HEAD.  OP is the
    1822                 :             :    operation we consider.  */
    1823                 :             : 
    1824                 :             : static void
    1825                 :      681633 : eliminate_implied_conditions (enum rtx_code op, rtx *head, rtx tail)
    1826                 :             : {
    1827                 :      681633 :   rtx elt;
    1828                 :             : 
    1829                 :      764286 :   for (elt = tail; elt; elt = XEXP (elt, 1))
    1830                 :       82653 :     eliminate_implied_condition (op, *head, &XEXP (elt, 0));
    1831                 :      764286 :   for (elt = tail; elt; elt = XEXP (elt, 1))
    1832                 :       82653 :     eliminate_implied_condition (op, XEXP (elt, 0), head);
    1833                 :      681633 : }
    1834                 :             : 
    1835                 :             : /* Simplifies *EXPR using initial values at the start of the LOOP.  If *EXPR
    1836                 :             :    is a list, its elements are assumed to be combined using OP.  */
    1837                 :             : 
    1838                 :             : static void
    1839                 :     4884497 : simplify_using_initial_values (class loop *loop, enum rtx_code op, rtx *expr)
    1840                 :             : {
    1841                 :     4884497 :   bool expression_valid;
    1842                 :     4884497 :   rtx head, tail, last_valid_expr;
    1843                 :     4884497 :   rtx_expr_list *cond_list;
    1844                 :     4884497 :   rtx_insn *insn;
    1845                 :     4884497 :   rtx neutral, aggr;
    1846                 :     4884497 :   regset altered, this_altered;
    1847                 :     4884497 :   edge e;
    1848                 :             : 
    1849                 :     4884497 :   if (!*expr)
    1850                 :     3700069 :     return;
    1851                 :             : 
    1852                 :     2403823 :   if (CONSTANT_P (*expr))
    1853                 :             :     return;
    1854                 :             : 
    1855                 :     1866061 :   if (GET_CODE (*expr) == EXPR_LIST)
    1856                 :             :     {
    1857                 :      681633 :       head = XEXP (*expr, 0);
    1858                 :      681633 :       tail = XEXP (*expr, 1);
    1859                 :             : 
    1860                 :      681633 :       eliminate_implied_conditions (op, &head, tail);
    1861                 :             : 
    1862                 :      681633 :       switch (op)
    1863                 :             :         {
    1864                 :        4877 :         case AND:
    1865                 :        4877 :           neutral = const_true_rtx;
    1866                 :        4877 :           aggr = const0_rtx;
    1867                 :        4877 :           break;
    1868                 :             : 
    1869                 :      676756 :         case IOR:
    1870                 :      676756 :           neutral = const0_rtx;
    1871                 :      676756 :           aggr = const_true_rtx;
    1872                 :      676756 :           break;
    1873                 :             : 
    1874                 :           0 :         default:
    1875                 :           0 :           gcc_unreachable ();
    1876                 :             :         }
    1877                 :             : 
    1878                 :      681633 :       simplify_using_initial_values (loop, UNKNOWN, &head);
    1879                 :      681633 :       if (head == aggr)
    1880                 :             :         {
    1881                 :         102 :           XEXP (*expr, 0) = aggr;
    1882                 :         102 :           XEXP (*expr, 1) = NULL_RTX;
    1883                 :         102 :           return;
    1884                 :             :         }
    1885                 :      681531 :       else if (head == neutral)
    1886                 :             :         {
    1887                 :      366003 :           *expr = tail;
    1888                 :      366003 :           simplify_using_initial_values (loop, op, expr);
    1889                 :      366003 :           return;
    1890                 :             :         }
    1891                 :      315528 :       simplify_using_initial_values (loop, op, &tail);
    1892                 :             : 
    1893                 :      315528 :       if (tail && XEXP (tail, 0) == aggr)
    1894                 :             :         {
    1895                 :           0 :           *expr = tail;
    1896                 :           0 :           return;
    1897                 :             :         }
    1898                 :             : 
    1899                 :      315528 :       XEXP (*expr, 0) = head;
    1900                 :      315528 :       XEXP (*expr, 1) = tail;
    1901                 :      315528 :       return;
    1902                 :             :     }
    1903                 :             : 
    1904                 :     1184428 :   gcc_assert (op == UNKNOWN);
    1905                 :             : 
    1906                 :     1184428 :   replace_single_def_regs (expr);
    1907                 :     1184428 :   if (CONSTANT_P (*expr))
    1908                 :             :     return;
    1909                 :             : 
    1910                 :     1184428 :   e = loop_preheader_edge (loop);
    1911                 :     1184428 :   if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
    1912                 :             :     return;
    1913                 :             : 
    1914                 :     1184428 :   altered = ALLOC_REG_SET (&reg_obstack);
    1915                 :     1184428 :   this_altered = ALLOC_REG_SET (&reg_obstack);
    1916                 :             : 
    1917                 :     1184428 :   expression_valid = true;
    1918                 :     1184428 :   last_valid_expr = *expr;
    1919                 :     1184428 :   cond_list = NULL;
    1920                 :     2162401 :   while (1)
    1921                 :             :     {
    1922                 :     2162401 :       insn = BB_END (e->src);
    1923                 :     2162401 :       if (any_condjump_p (insn) && onlyjump_p (insn))
    1924                 :             :         {
    1925                 :      939773 :           rtx cond = get_condition (BB_END (e->src), NULL, false, true);
    1926                 :             : 
    1927                 :      939773 :           if (cond && (e->flags & EDGE_FALLTHRU))
    1928                 :      597575 :             cond = reversed_condition (cond);
    1929                 :      930123 :           if (cond)
    1930                 :             :             {
    1931                 :      927083 :               rtx old = *expr;
    1932                 :      927083 :               simplify_using_condition (cond, expr, altered);
    1933                 :      927083 :               if (old != *expr)
    1934                 :             :                 {
    1935                 :       36036 :                   rtx note;
    1936                 :       36036 :                   if (CONSTANT_P (*expr))
    1937                 :       35363 :                     goto out;
    1938                 :        1047 :                   for (note = cond_list; note; note = XEXP (note, 1))
    1939                 :             :                     {
    1940                 :         511 :                       simplify_using_condition (XEXP (note, 0), expr, altered);
    1941                 :         511 :                       if (CONSTANT_P (*expr))
    1942                 :         137 :                         goto out;
    1943                 :             :                     }
    1944                 :             :                 }
    1945                 :      891583 :               cond_list = alloc_EXPR_LIST (0, cond, cond_list);
    1946                 :             :             }
    1947                 :             :         }
    1948                 :             : 
    1949                 :    19318684 :       FOR_BB_INSNS_REVERSE (e->src, insn)
    1950                 :             :         {
    1951                 :    17904137 :           rtx src, dest;
    1952                 :    17904137 :           rtx old = *expr;
    1953                 :             : 
    1954                 :    17904137 :           if (!INSN_P (insn))
    1955                 :     2228035 :             continue;
    1956                 :             : 
    1957                 :    15676102 :           CLEAR_REG_SET (this_altered);
    1958                 :    15676102 :           note_stores (insn, mark_altered, this_altered);
    1959                 :    15676102 :           if (CALL_P (insn))
    1960                 :             :             {
    1961                 :             :               /* Kill all registers that might be clobbered by the call.
    1962                 :             :                  We don't track modes of hard registers, so we need to be
    1963                 :             :                  conservative and assume that partial kills are full kills.  */
    1964                 :      129951 :               function_abi callee_abi = insn_callee_abi (insn);
    1965                 :      129951 :               IOR_REG_SET_HRS (this_altered,
    1966                 :             :                                callee_abi.full_and_partial_reg_clobbers ());
    1967                 :             :             }
    1968                 :             : 
    1969                 :    15676102 :           if (suitable_set_for_replacement (insn, &dest, &src))
    1970                 :             :             {
    1971                 :     4014273 :               rtx_expr_list **pnote, **pnote_next;
    1972                 :             : 
    1973                 :     4014273 :               replace_in_expr (expr, dest, src);
    1974                 :     4014273 :               if (CONSTANT_P (*expr))
    1975                 :      712354 :                 goto out;
    1976                 :             : 
    1977                 :     4995984 :               for (pnote = &cond_list; *pnote; pnote = pnote_next)
    1978                 :             :                 {
    1979                 :     1288825 :                   rtx_expr_list *note = *pnote;
    1980                 :     1288825 :                   rtx old_cond = XEXP (note, 0);
    1981                 :             : 
    1982                 :     1288825 :                   pnote_next = (rtx_expr_list **)&XEXP (note, 1);
    1983                 :     1288825 :                   replace_in_expr (&XEXP (note, 0), dest, src);
    1984                 :             : 
    1985                 :             :                   /* We can no longer use a condition that has been simplified
    1986                 :             :                      to a constant, and simplify_using_condition will abort if
    1987                 :             :                      we try.  */
    1988                 :     1288825 :                   if (CONSTANT_P (XEXP (note, 0)))
    1989                 :             :                     {
    1990                 :          12 :                       *pnote = *pnote_next;
    1991                 :          12 :                       pnote_next = pnote;
    1992                 :          12 :                       free_EXPR_LIST_node (note);
    1993                 :             :                     }
    1994                 :             :                   /* Retry simplifications with this condition if either the
    1995                 :             :                      expression or the condition changed.  */
    1996                 :     1288813 :                   else if (old_cond != XEXP (note, 0) || old != *expr)
    1997                 :      360449 :                     simplify_using_condition (XEXP (note, 0), expr, altered);
    1998                 :             :                 }
    1999                 :             :             }
    2000                 :             :           else
    2001                 :             :             {
    2002                 :    11661829 :               rtx_expr_list **pnote, **pnote_next;
    2003                 :             : 
    2004                 :             :               /* If we did not use this insn to make a replacement, any overlap
    2005                 :             :                  between stores in this insn and our expression will cause the
    2006                 :             :                  expression to become invalid.  */
    2007                 :    11661829 :               if (altered_reg_used (*expr, this_altered))
    2008                 :      398142 :                 goto out;
    2009                 :             : 
    2010                 :             :               /* Likewise for the conditions.  */
    2011                 :    24236729 :               for (pnote = &cond_list; *pnote; pnote = pnote_next)
    2012                 :             :                 {
    2013                 :    12973042 :                   rtx_expr_list *note = *pnote;
    2014                 :    12973042 :                   rtx old_cond = XEXP (note, 0);
    2015                 :             : 
    2016                 :    12973042 :                   pnote_next = (rtx_expr_list **)&XEXP (note, 1);
    2017                 :    12973042 :                   if (altered_reg_used (old_cond, this_altered))
    2018                 :             :                     {
    2019                 :      195492 :                       *pnote = *pnote_next;
    2020                 :      195492 :                       pnote_next = pnote;
    2021                 :      195492 :                       free_EXPR_LIST_node (note);
    2022                 :             :                     }
    2023                 :             :                 }
    2024                 :             :             }
    2025                 :             : 
    2026                 :    14970846 :           if (CONSTANT_P (*expr))
    2027                 :        7098 :             goto out;
    2028                 :             : 
    2029                 :    14963748 :           IOR_REG_SET (altered, this_altered);
    2030                 :             : 
    2031                 :             :           /* If the expression now contains regs that have been altered, we
    2032                 :             :              can't return it to the caller.  However, it is still valid for
    2033                 :             :              further simplification, so keep searching to see if we can
    2034                 :             :              eventually turn it into a constant.  */
    2035                 :    14963748 :           if (altered_reg_used (*expr, altered))
    2036                 :             :             expression_valid = false;
    2037                 :    14828150 :           if (expression_valid)
    2038                 :    14804813 :             last_valid_expr = *expr;
    2039                 :             :         }
    2040                 :             : 
    2041                 :     1414547 :       if (!single_pred_p (e->src)
    2042                 :     1414547 :           || single_pred (e->src) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
    2043                 :             :         break;
    2044                 :             :       e = single_pred_edge (e->src);
    2045                 :             :     }
    2046                 :             : 
    2047                 :     1184428 :  out:
    2048                 :     1184428 :   free_EXPR_LIST_list (&cond_list);
    2049                 :     1184428 :   if (!CONSTANT_P (*expr))
    2050                 :      834716 :     *expr = last_valid_expr;
    2051                 :     1184428 :   FREE_REG_SET (altered);
    2052                 :     1184428 :   FREE_REG_SET (this_altered);
    2053                 :             : }
    2054                 :             : 
    2055                 :             : /* Transforms invariant IV into MODE.  Adds assumptions based on the fact
    2056                 :             :    that IV occurs as left operands of comparison COND and its signedness
    2057                 :             :    is SIGNED_P to DESC.  */
    2058                 :             : 
    2059                 :             : static void
    2060                 :           1 : shorten_into_mode (class rtx_iv *iv, scalar_int_mode mode,
    2061                 :             :                    enum rtx_code cond, bool signed_p, class niter_desc *desc)
    2062                 :             : {
    2063                 :           1 :   rtx mmin, mmax, cond_over, cond_under;
    2064                 :             : 
    2065                 :           1 :   get_mode_bounds (mode, signed_p, iv->extend_mode, &mmin, &mmax);
    2066                 :           1 :   cond_under = simplify_gen_relational (LT, SImode, iv->extend_mode,
    2067                 :             :                                         iv->base, mmin);
    2068                 :           1 :   cond_over = simplify_gen_relational (GT, SImode, iv->extend_mode,
    2069                 :             :                                        iv->base, mmax);
    2070                 :             : 
    2071                 :           1 :   switch (cond)
    2072                 :             :     {
    2073                 :           0 :       case LE:
    2074                 :           0 :       case LT:
    2075                 :           0 :       case LEU:
    2076                 :           0 :       case LTU:
    2077                 :           0 :         if (cond_under != const0_rtx)
    2078                 :           0 :           desc->infinite =
    2079                 :           0 :                   alloc_EXPR_LIST (0, cond_under, desc->infinite);
    2080                 :           0 :         if (cond_over != const0_rtx)
    2081                 :           0 :           desc->noloop_assumptions =
    2082                 :           0 :                   alloc_EXPR_LIST (0, cond_over, desc->noloop_assumptions);
    2083                 :             :         break;
    2084                 :             : 
    2085                 :           1 :       case GE:
    2086                 :           1 :       case GT:
    2087                 :           1 :       case GEU:
    2088                 :           1 :       case GTU:
    2089                 :           1 :         if (cond_over != const0_rtx)
    2090                 :           1 :           desc->infinite =
    2091                 :           1 :                   alloc_EXPR_LIST (0, cond_over, desc->infinite);
    2092                 :           1 :         if (cond_under != const0_rtx)
    2093                 :           1 :           desc->noloop_assumptions =
    2094                 :           1 :                   alloc_EXPR_LIST (0, cond_under, desc->noloop_assumptions);
    2095                 :             :         break;
    2096                 :             : 
    2097                 :           0 :       case NE:
    2098                 :           0 :         if (cond_over != const0_rtx)
    2099                 :           0 :           desc->infinite =
    2100                 :           0 :                   alloc_EXPR_LIST (0, cond_over, desc->infinite);
    2101                 :           0 :         if (cond_under != const0_rtx)
    2102                 :           0 :           desc->infinite =
    2103                 :           0 :                   alloc_EXPR_LIST (0, cond_under, desc->infinite);
    2104                 :             :         break;
    2105                 :             : 
    2106                 :           0 :       default:
    2107                 :           0 :         gcc_unreachable ();
    2108                 :             :     }
    2109                 :             : 
    2110                 :           1 :   iv->mode = mode;
    2111                 :           1 :   iv->extend = signed_p ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
    2112                 :           1 : }
    2113                 :             : 
    2114                 :             : /* Transforms IV0 and IV1 compared by COND so that they are both compared as
    2115                 :             :    subregs of the same mode if possible (sometimes it is necessary to add
    2116                 :             :    some assumptions to DESC).  */
    2117                 :             : 
    2118                 :             : static bool
    2119                 :      420331 : canonicalize_iv_subregs (class rtx_iv *iv0, class rtx_iv *iv1,
    2120                 :             :                          enum rtx_code cond, class niter_desc *desc)
    2121                 :             : {
    2122                 :      420331 :   scalar_int_mode comp_mode;
    2123                 :      420331 :   bool signed_p;
    2124                 :             : 
    2125                 :             :   /* If the ivs behave specially in the first iteration, or are
    2126                 :             :      added/multiplied after extending, we ignore them.  */
    2127                 :      420331 :   if (iv0->first_special || iv0->mult != const1_rtx || iv0->delta != const0_rtx)
    2128                 :             :     return false;
    2129                 :      420330 :   if (iv1->first_special || iv1->mult != const1_rtx || iv1->delta != const0_rtx)
    2130                 :             :     return false;
    2131                 :             : 
    2132                 :             :   /* If there is some extend, it must match signedness of the comparison.  */
    2133                 :      420330 :   switch (cond)
    2134                 :             :     {
    2135                 :       31063 :       case LE:
    2136                 :       31063 :       case LT:
    2137                 :       31063 :         if (iv0->extend == IV_ZERO_EXTEND
    2138                 :       31063 :             || iv1->extend == IV_ZERO_EXTEND)
    2139                 :             :           return false;
    2140                 :             :         signed_p = true;
    2141                 :             :         break;
    2142                 :             : 
    2143                 :       30036 :       case LEU:
    2144                 :       30036 :       case LTU:
    2145                 :       30036 :         if (iv0->extend == IV_SIGN_EXTEND
    2146                 :       30036 :             || iv1->extend == IV_SIGN_EXTEND)
    2147                 :             :           return false;
    2148                 :             :         signed_p = false;
    2149                 :             :         break;
    2150                 :             : 
    2151                 :      359231 :       case NE:
    2152                 :      359231 :         if (iv0->extend != IV_UNKNOWN_EXTEND
    2153                 :           0 :             && iv1->extend != IV_UNKNOWN_EXTEND
    2154                 :           0 :             && iv0->extend != iv1->extend)
    2155                 :             :           return false;
    2156                 :             : 
    2157                 :      359231 :         signed_p = false;
    2158                 :      359231 :         if (iv0->extend != IV_UNKNOWN_EXTEND)
    2159                 :           0 :           signed_p = iv0->extend == IV_SIGN_EXTEND;
    2160                 :      359231 :         if (iv1->extend != IV_UNKNOWN_EXTEND)
    2161                 :           0 :           signed_p = iv1->extend == IV_SIGN_EXTEND;
    2162                 :             :         break;
    2163                 :             : 
    2164                 :           0 :       default:
    2165                 :           0 :         gcc_unreachable ();
    2166                 :             :     }
    2167                 :             : 
    2168                 :             :   /* Values of both variables should be computed in the same mode.  These
    2169                 :             :      might indeed be different, if we have comparison like
    2170                 :             : 
    2171                 :             :      (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
    2172                 :             : 
    2173                 :             :      and iv0 and iv1 are both ivs iterating in SI mode, but calculated
    2174                 :             :      in different modes.  This does not seem impossible to handle, but
    2175                 :             :      it hardly ever occurs in practice.
    2176                 :             : 
    2177                 :             :      The only exception is the case when one of operands is invariant.
    2178                 :             :      For example pentium 3 generates comparisons like
    2179                 :             :      (lt (subreg:HI (reg:SI)) 100).  Here we assign HImode to 100, but we
    2180                 :             :      definitely do not want this prevent the optimization.  */
    2181                 :      420330 :   comp_mode = iv0->extend_mode;
    2182                 :     1260990 :   if (GET_MODE_BITSIZE (comp_mode) < GET_MODE_BITSIZE (iv1->extend_mode))
    2183                 :      420330 :     comp_mode = iv1->extend_mode;
    2184                 :             : 
    2185                 :      420330 :   if (iv0->extend_mode != comp_mode)
    2186                 :             :     {
    2187                 :         266 :       if (iv0->mode != iv0->extend_mode
    2188                 :         266 :           || iv0->step != const0_rtx)
    2189                 :             :         return false;
    2190                 :             : 
    2191                 :         266 :       iv0->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
    2192                 :             :                                       comp_mode, iv0->base, iv0->mode);
    2193                 :         266 :       iv0->extend_mode = comp_mode;
    2194                 :             :     }
    2195                 :             : 
    2196                 :      420330 :   if (iv1->extend_mode != comp_mode)
    2197                 :             :     {
    2198                 :        5093 :       if (iv1->mode != iv1->extend_mode
    2199                 :        5093 :           || iv1->step != const0_rtx)
    2200                 :             :         return false;
    2201                 :             : 
    2202                 :        5087 :       iv1->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
    2203                 :             :                                       comp_mode, iv1->base, iv1->mode);
    2204                 :        5087 :       iv1->extend_mode = comp_mode;
    2205                 :             :     }
    2206                 :             : 
    2207                 :             :   /* Check that both ivs belong to a range of a single mode.  If one of the
    2208                 :             :      operands is an invariant, we may need to shorten it into the common
    2209                 :             :      mode.  */
    2210                 :      420324 :   if (iv0->mode == iv0->extend_mode
    2211                 :      414970 :       && iv0->step == const0_rtx
    2212                 :      542352 :       && iv0->mode != iv1->mode)
    2213                 :           0 :     shorten_into_mode (iv0, iv1->mode, cond, signed_p, desc);
    2214                 :             : 
    2215                 :      420324 :   if (iv1->mode == iv1->extend_mode
    2216                 :      414971 :       && iv1->step == const0_rtx
    2217                 :      716979 :       && iv0->mode != iv1->mode)
    2218                 :           1 :     shorten_into_mode (iv1, iv0->mode, swap_condition (cond), signed_p, desc);
    2219                 :             : 
    2220                 :      420324 :   if (iv0->mode != iv1->mode)
    2221                 :             :     return false;
    2222                 :             : 
    2223                 :      420324 :   desc->mode = iv0->mode;
    2224                 :      420324 :   desc->signed_p = signed_p;
    2225                 :             : 
    2226                 :      420324 :   return true;
    2227                 :             : }
    2228                 :             : 
    2229                 :             : /* Tries to estimate the maximum number of iterations in LOOP, and return the
    2230                 :             :    result.  This function is called from iv_number_of_iterations with
    2231                 :             :    a number of fields in DESC already filled in.  OLD_NITER is the original
    2232                 :             :    expression for the number of iterations, before we tried to simplify it.  */
    2233                 :             : 
    2234                 :             : static uint64_t
    2235                 :      213637 : determine_max_iter (class loop *loop, class niter_desc *desc, rtx old_niter)
    2236                 :             : {
    2237                 :      213637 :   rtx niter = desc->niter_expr;
    2238                 :      213637 :   rtx mmin, mmax, cmp;
    2239                 :      213637 :   uint64_t nmax, inc;
    2240                 :      213637 :   uint64_t andmax = 0;
    2241                 :             : 
    2242                 :             :   /* We used to look for constant operand 0 of AND,
    2243                 :             :      but canonicalization should always make this impossible.  */
    2244                 :      213637 :   gcc_checking_assert (GET_CODE (niter) != AND
    2245                 :             :                        || !CONST_INT_P (XEXP (niter, 0)));
    2246                 :             : 
    2247                 :      213637 :   if (GET_CODE (niter) == AND
    2248                 :        9253 :       && CONST_INT_P (XEXP (niter, 1)))
    2249                 :             :     {
    2250                 :        9253 :       andmax = UINTVAL (XEXP (niter, 1));
    2251                 :        9253 :       niter = XEXP (niter, 0);
    2252                 :             :     }
    2253                 :             : 
    2254                 :      213637 :   get_mode_bounds (desc->mode, desc->signed_p, desc->mode, &mmin, &mmax);
    2255                 :      213637 :   nmax = UINTVAL (mmax) - UINTVAL (mmin);
    2256                 :             : 
    2257                 :      213637 :   if (GET_CODE (niter) == UDIV)
    2258                 :             :     {
    2259                 :        2742 :       if (!CONST_INT_P (XEXP (niter, 1)))
    2260                 :             :         return nmax;
    2261                 :        2742 :       inc = INTVAL (XEXP (niter, 1));
    2262                 :        2742 :       niter = XEXP (niter, 0);
    2263                 :             :     }
    2264                 :             :   else
    2265                 :             :     inc = 1;
    2266                 :             : 
    2267                 :             :   /* We could use a binary search here, but for now improving the upper
    2268                 :             :      bound by just one eliminates one important corner case.  */
    2269                 :      213637 :   cmp = simplify_gen_relational (desc->signed_p ? LT : LTU, VOIDmode,
    2270                 :             :                                  desc->mode, old_niter, mmax);
    2271                 :      213637 :   simplify_using_initial_values (loop, UNKNOWN, &cmp);
    2272                 :      213637 :   if (cmp == const_true_rtx)
    2273                 :             :     {
    2274                 :      121723 :       nmax--;
    2275                 :             : 
    2276                 :      121723 :       if (dump_file)
    2277                 :          15 :         fprintf (dump_file, ";; improved upper bound by one.\n");
    2278                 :             :     }
    2279                 :      213637 :   nmax /= inc;
    2280                 :      213637 :   if (andmax)
    2281                 :        9253 :     nmax = MIN (nmax, andmax);
    2282                 :      213637 :   if (dump_file)
    2283                 :          24 :     fprintf (dump_file, ";; Determined upper bound %" PRId64".\n",
    2284                 :             :              nmax);
    2285                 :             :   return nmax;
    2286                 :             : }
    2287                 :             : 
    2288                 :             : /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
    2289                 :             :    the result into DESC.  Very similar to determine_number_of_iterations
    2290                 :             :    (basically its rtl version), complicated by things like subregs.  */
    2291                 :             : 
    2292                 :             : static void
    2293                 :      724851 : iv_number_of_iterations (class loop *loop, rtx_insn *insn, rtx condition,
    2294                 :             :                          class niter_desc *desc)
    2295                 :             : {
    2296                 :      724851 :   rtx op0, op1, delta, step, bound, may_xform, tmp, tmp0, tmp1;
    2297                 :      724851 :   class rtx_iv iv0, iv1;
    2298                 :      724851 :   rtx assumption, may_not_xform;
    2299                 :      724851 :   enum rtx_code cond;
    2300                 :      724851 :   machine_mode nonvoid_mode;
    2301                 :      724851 :   scalar_int_mode comp_mode;
    2302                 :      724851 :   rtx mmin, mmax, mode_mmin, mode_mmax;
    2303                 :      724851 :   uint64_t s, size, d, inv, max, up, down;
    2304                 :      724851 :   int64_t inc, step_val;
    2305                 :      724851 :   int was_sharp = false;
    2306                 :      724851 :   rtx old_niter;
    2307                 :      724851 :   bool step_is_pow2;
    2308                 :             : 
    2309                 :             :   /* The meaning of these assumptions is this:
    2310                 :             :      if !assumptions
    2311                 :             :        then the rest of information does not have to be valid
    2312                 :             :      if noloop_assumptions then the loop does not roll
    2313                 :             :      if infinite then this exit is never used */
    2314                 :             : 
    2315                 :      724851 :   desc->assumptions = NULL_RTX;
    2316                 :      724851 :   desc->noloop_assumptions = NULL_RTX;
    2317                 :      724851 :   desc->infinite = NULL_RTX;
    2318                 :      724851 :   desc->simple_p = true;
    2319                 :             : 
    2320                 :      724851 :   desc->const_iter = false;
    2321                 :      724851 :   desc->niter_expr = NULL_RTX;
    2322                 :             : 
    2323                 :      724851 :   cond = GET_CODE (condition);
    2324                 :      724851 :   gcc_assert (COMPARISON_P (condition));
    2325                 :             : 
    2326                 :      724851 :   nonvoid_mode = GET_MODE (XEXP (condition, 0));
    2327                 :      724851 :   if (nonvoid_mode == VOIDmode)
    2328                 :           0 :     nonvoid_mode = GET_MODE (XEXP (condition, 1));
    2329                 :             :   /* The constant comparisons should be folded.  */
    2330                 :      724851 :   gcc_assert (nonvoid_mode != VOIDmode);
    2331                 :             : 
    2332                 :             :   /* We only handle integers or pointers.  */
    2333                 :      724851 :   scalar_int_mode mode;
    2334                 :      724851 :   if (!is_a <scalar_int_mode> (nonvoid_mode, &mode))
    2335                 :         491 :     goto fail;
    2336                 :             : 
    2337                 :      724360 :   op0 = XEXP (condition, 0);
    2338                 :      724360 :   if (!iv_analyze (insn, mode, op0, &iv0))
    2339                 :      263702 :     goto fail;
    2340                 :             : 
    2341                 :      460658 :   op1 = XEXP (condition, 1);
    2342                 :      460658 :   if (!iv_analyze (insn, mode, op1, &iv1))
    2343                 :       38129 :     goto fail;
    2344                 :             : 
    2345                 :      844815 :   if (GET_MODE_BITSIZE (iv0.extend_mode) > HOST_BITS_PER_WIDE_INT
    2346                 :      844815 :       || GET_MODE_BITSIZE (iv1.extend_mode) > HOST_BITS_PER_WIDE_INT)
    2347                 :         245 :     goto fail;
    2348                 :             : 
    2349                 :             :   /* Check condition and normalize it.  */
    2350                 :             : 
    2351                 :      422284 :   switch (cond)
    2352                 :             :     {
    2353                 :       25652 :       case GE:
    2354                 :       25652 :       case GT:
    2355                 :       25652 :       case GEU:
    2356                 :       25652 :       case GTU:
    2357                 :       25652 :         std::swap (iv0, iv1);
    2358                 :       25652 :         cond = swap_condition (cond);
    2359                 :       25652 :         break;
    2360                 :             :       case NE:
    2361                 :             :       case LE:
    2362                 :             :       case LEU:
    2363                 :             :       case LT:
    2364                 :             :       case LTU:
    2365                 :             :         break;
    2366                 :        1953 :       default:
    2367                 :        1953 :         goto fail;
    2368                 :             :     }
    2369                 :             : 
    2370                 :             :   /* Handle extends.  This is relatively nontrivial, so we only try in some
    2371                 :             :      easy cases, when we can canonicalize the ivs (possibly by adding some
    2372                 :             :      assumptions) to shape subreg (base + i * step).  This function also fills
    2373                 :             :      in desc->mode and desc->signed_p.  */
    2374                 :             : 
    2375                 :      420331 :   if (!canonicalize_iv_subregs (&iv0, &iv1, cond, desc))
    2376                 :           7 :     goto fail;
    2377                 :             : 
    2378                 :      420324 :   comp_mode = iv0.extend_mode;
    2379                 :      420324 :   mode = iv0.mode;
    2380                 :      420324 :   size = GET_MODE_PRECISION (mode);
    2381                 :      420324 :   get_mode_bounds (mode, (cond == LE || cond == LT), comp_mode, &mmin, &mmax);
    2382                 :      420324 :   mode_mmin = lowpart_subreg (mode, mmin, comp_mode);
    2383                 :      420324 :   mode_mmax = lowpart_subreg (mode, mmax, comp_mode);
    2384                 :             : 
    2385                 :      420324 :   if (!CONST_INT_P (iv0.step) || !CONST_INT_P (iv1.step))
    2386                 :           0 :     goto fail;
    2387                 :             : 
    2388                 :             :   /* We can take care of the case of two induction variables chasing each other
    2389                 :             :      if the test is NE. I have never seen a loop using it, but still it is
    2390                 :             :      cool.  */
    2391                 :      420324 :   if (iv0.step != const0_rtx && iv1.step != const0_rtx)
    2392                 :             :     {
    2393                 :         322 :       if (cond != NE)
    2394                 :         165 :         goto fail;
    2395                 :             : 
    2396                 :         157 :       iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
    2397                 :         157 :       iv1.step = const0_rtx;
    2398                 :             :     }
    2399                 :             : 
    2400                 :      420159 :   iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
    2401                 :      420159 :   iv1.step = lowpart_subreg (mode, iv1.step, comp_mode);
    2402                 :             : 
    2403                 :             :   /* This is either infinite loop or the one that ends immediately, depending
    2404                 :             :      on initial values.  Unswitching should remove this kind of conditions.  */
    2405                 :      420159 :   if (iv0.step == const0_rtx && iv1.step == const0_rtx)
    2406                 :        4153 :     goto fail;
    2407                 :             : 
    2408                 :      416006 :   if (cond != NE)
    2409                 :             :     {
    2410                 :       58147 :       if (iv0.step == const0_rtx)
    2411                 :        5402 :         step_val = -INTVAL (iv1.step);
    2412                 :             :       else
    2413                 :       52745 :         step_val = INTVAL (iv0.step);
    2414                 :             : 
    2415                 :             :       /* Ignore loops of while (i-- < 10) type.  */
    2416                 :       58147 :       if (step_val < 0)
    2417                 :        2530 :         goto fail;
    2418                 :             : 
    2419                 :       55617 :       step_is_pow2 = !(step_val & (step_val - 1));
    2420                 :             :     }
    2421                 :             :   else
    2422                 :             :     {
    2423                 :             :       /* We do not care about whether the step is power of two in this
    2424                 :             :          case.  */
    2425                 :             :       step_is_pow2 = false;
    2426                 :      425876 :       step_val = 0;
    2427                 :             :     }
    2428                 :             : 
    2429                 :             :   /* Some more condition normalization.  We must record some assumptions
    2430                 :             :      due to overflows.  */
    2431                 :       55617 :   switch (cond)
    2432                 :             :     {
    2433                 :       43217 :       case LT:
    2434                 :       43217 :       case LTU:
    2435                 :             :         /* We want to take care only of non-sharp relationals; this is easy,
    2436                 :             :            as in cases the overflow would make the transformation unsafe
    2437                 :             :            the loop does not roll.  Seemingly it would make more sense to want
    2438                 :             :            to take care of sharp relationals instead, as NE is more similar to
    2439                 :             :            them, but the problem is that here the transformation would be more
    2440                 :             :            difficult due to possibly infinite loops.  */
    2441                 :       43217 :         if (iv0.step == const0_rtx)
    2442                 :             :           {
    2443                 :        3582 :             tmp = lowpart_subreg (mode, iv0.base, comp_mode);
    2444                 :        3582 :             assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
    2445                 :             :                                                   mode_mmax);
    2446                 :        3582 :             if (assumption == const_true_rtx)
    2447                 :           0 :               goto zero_iter_simplify;
    2448                 :        3582 :             iv0.base = simplify_gen_binary (PLUS, comp_mode,
    2449                 :             :                                             iv0.base, const1_rtx);
    2450                 :             :           }
    2451                 :             :         else
    2452                 :             :           {
    2453                 :       39635 :             tmp = lowpart_subreg (mode, iv1.base, comp_mode);
    2454                 :       39635 :             assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
    2455                 :             :                                                   mode_mmin);
    2456                 :       39635 :             if (assumption == const_true_rtx)
    2457                 :           0 :               goto zero_iter_simplify;
    2458                 :       39635 :             iv1.base = simplify_gen_binary (PLUS, comp_mode,
    2459                 :             :                                             iv1.base, constm1_rtx);
    2460                 :             :           }
    2461                 :             : 
    2462                 :       43217 :         if (assumption != const0_rtx)
    2463                 :       40791 :           desc->noloop_assumptions =
    2464                 :       40791 :                   alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
    2465                 :       43217 :         cond = (cond == LT) ? LE : LEU;
    2466                 :             : 
    2467                 :             :         /* It will be useful to be able to tell the difference once more in
    2468                 :             :            LE -> NE reduction.  */
    2469                 :             :         was_sharp = true;
    2470                 :             :         break;
    2471                 :      370259 :       default: ;
    2472                 :             :     }
    2473                 :             : 
    2474                 :             :   /* Take care of trivially infinite loops.  */
    2475                 :      370259 :   if (cond != NE)
    2476                 :             :     {
    2477                 :       55617 :       if (iv0.step == const0_rtx)
    2478                 :             :         {
    2479                 :        4633 :           tmp = lowpart_subreg (mode, iv0.base, comp_mode);
    2480                 :        4633 :           if (rtx_equal_p (tmp, mode_mmin))
    2481                 :             :             {
    2482                 :           0 :               desc->infinite =
    2483                 :           0 :                       alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
    2484                 :             :               /* Fill in the remaining fields somehow.  */
    2485                 :           0 :               goto zero_iter_simplify;
    2486                 :             :             }
    2487                 :             :         }
    2488                 :             :       else
    2489                 :             :         {
    2490                 :       50984 :           tmp = lowpart_subreg (mode, iv1.base, comp_mode);
    2491                 :       50984 :           if (rtx_equal_p (tmp, mode_mmax))
    2492                 :             :             {
    2493                 :           0 :               desc->infinite =
    2494                 :           0 :                       alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
    2495                 :             :               /* Fill in the remaining fields somehow.  */
    2496                 :           0 :               goto zero_iter_simplify;
    2497                 :             :             }
    2498                 :             :         }
    2499                 :             :     }
    2500                 :             : 
    2501                 :             :   /* If we can we want to take care of NE conditions instead of size
    2502                 :             :      comparisons, as they are much more friendly (most importantly
    2503                 :             :      this takes care of special handling of loops with step 1).  We can
    2504                 :             :      do it if we first check that upper bound is greater or equal to
    2505                 :             :      lower bound, their difference is constant c modulo step and that
    2506                 :             :      there is not an overflow.  */
    2507                 :       55617 :   if (cond != NE)
    2508                 :             :     {
    2509                 :       55617 :       if (iv0.step == const0_rtx)
    2510                 :        4633 :         step = simplify_gen_unary (NEG, comp_mode, iv1.step, comp_mode);
    2511                 :             :       else
    2512                 :             :         step = iv0.step;
    2513                 :       55617 :       step = lowpart_subreg (mode, step, comp_mode);
    2514                 :       55617 :       delta = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
    2515                 :       55617 :       delta = lowpart_subreg (mode, delta, comp_mode);
    2516                 :       55617 :       delta = simplify_gen_binary (UMOD, mode, delta, step);
    2517                 :       55617 :       may_xform = const0_rtx;
    2518                 :       55617 :       may_not_xform = const_true_rtx;
    2519                 :             : 
    2520                 :       55617 :       if (CONST_INT_P (delta))
    2521                 :             :         {
    2522                 :       31975 :           if (was_sharp && INTVAL (delta) == INTVAL (step) - 1)
    2523                 :             :             {
    2524                 :             :               /* A special case.  We have transformed condition of type
    2525                 :             :                  for (i = 0; i < 4; i += 4)
    2526                 :             :                  into
    2527                 :             :                  for (i = 0; i <= 3; i += 4)
    2528                 :             :                  obviously if the test for overflow during that transformation
    2529                 :             :                  passed, we cannot overflow here.  Most importantly any
    2530                 :             :                  loop with sharp end condition and step 1 falls into this
    2531                 :             :                  category, so handling this case specially is definitely
    2532                 :             :                  worth the troubles.  */
    2533                 :             :               may_xform = const_true_rtx;
    2534                 :             :             }
    2535                 :        7776 :           else if (iv0.step == const0_rtx)
    2536                 :             :             {
    2537                 :         654 :               bound = simplify_gen_binary (PLUS, comp_mode, mmin, step);
    2538                 :         654 :               bound = simplify_gen_binary (MINUS, comp_mode, bound, delta);
    2539                 :         654 :               bound = lowpart_subreg (mode, bound, comp_mode);
    2540                 :         654 :               tmp = lowpart_subreg (mode, iv0.base, comp_mode);
    2541                 :         654 :               may_xform = simplify_gen_relational (cond, SImode, mode,
    2542                 :             :                                                    bound, tmp);
    2543                 :         654 :               may_not_xform = simplify_gen_relational (reverse_condition (cond),
    2544                 :             :                                                        SImode, mode,
    2545                 :             :                                                        bound, tmp);
    2546                 :             :             }
    2547                 :             :           else
    2548                 :             :             {
    2549                 :        7122 :               bound = simplify_gen_binary (MINUS, comp_mode, mmax, step);
    2550                 :        7122 :               bound = simplify_gen_binary (PLUS, comp_mode, bound, delta);
    2551                 :        7122 :               bound = lowpart_subreg (mode, bound, comp_mode);
    2552                 :        7122 :               tmp = lowpart_subreg (mode, iv1.base, comp_mode);
    2553                 :        7122 :               may_xform = simplify_gen_relational (cond, SImode, mode,
    2554                 :             :                                                    tmp, bound);
    2555                 :        7122 :               may_not_xform = simplify_gen_relational (reverse_condition (cond),
    2556                 :             :                                                        SImode, mode,
    2557                 :             :                                                        tmp, bound);
    2558                 :             :             }
    2559                 :             :         }
    2560                 :             : 
    2561                 :       55617 :       if (may_xform != const0_rtx)
    2562                 :             :         {
    2563                 :             :           /* We perform the transformation always provided that it is not
    2564                 :             :              completely senseless.  This is OK, as we would need this assumption
    2565                 :             :              to determine the number of iterations anyway.  */
    2566                 :       31975 :           if (may_xform != const_true_rtx)
    2567                 :             :             {
    2568                 :             :               /* If the step is a power of two and the final value we have
    2569                 :             :                  computed overflows, the cycle is infinite.  Otherwise it
    2570                 :             :                  is nontrivial to compute the number of iterations.  */
    2571                 :        7630 :               if (step_is_pow2)
    2572                 :        7630 :                 desc->infinite = alloc_EXPR_LIST (0, may_not_xform,
    2573                 :             :                                                   desc->infinite);
    2574                 :             :               else
    2575                 :           0 :                 desc->assumptions = alloc_EXPR_LIST (0, may_xform,
    2576                 :             :                                                      desc->assumptions);
    2577                 :             :             }
    2578                 :             : 
    2579                 :             :           /* We are going to lose some information about upper bound on
    2580                 :             :              number of iterations in this step, so record the information
    2581                 :             :              here.  */
    2582                 :       31975 :           inc = INTVAL (iv0.step) - INTVAL (iv1.step);
    2583                 :       31975 :           if (CONST_INT_P (iv1.base))
    2584                 :         450 :             up = INTVAL (iv1.base);
    2585                 :             :           else
    2586                 :       31525 :             up = INTVAL (mode_mmax) - inc;
    2587                 :       31975 :           down = INTVAL (CONST_INT_P (iv0.base)
    2588                 :             :                          ? iv0.base
    2589                 :             :                          : mode_mmin);
    2590                 :       31975 :           max = (up - down) / inc + 1;
    2591                 :       31975 :           if (!desc->infinite
    2592                 :       24345 :               && !desc->assumptions)
    2593                 :       24345 :             record_niter_bound (loop, max, false, true);
    2594                 :             : 
    2595                 :       31975 :           if (iv0.step == const0_rtx)
    2596                 :             :             {
    2597                 :        1706 :               iv0.base = simplify_gen_binary (PLUS, comp_mode, iv0.base, delta);
    2598                 :        1706 :               iv0.base = simplify_gen_binary (MINUS, comp_mode, iv0.base, step);
    2599                 :             :             }
    2600                 :             :           else
    2601                 :             :             {
    2602                 :       30269 :               iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, delta);
    2603                 :       30269 :               iv1.base = simplify_gen_binary (PLUS, comp_mode, iv1.base, step);
    2604                 :             :             }
    2605                 :             : 
    2606                 :       31975 :           tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
    2607                 :       31975 :           tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
    2608                 :       31975 :           assumption = simplify_gen_relational (reverse_condition (cond),
    2609                 :             :                                                 SImode, mode, tmp0, tmp1);
    2610                 :       31975 :           if (assumption == const_true_rtx)
    2611                 :           0 :             goto zero_iter_simplify;
    2612                 :       31975 :           else if (assumption != const0_rtx)
    2613                 :       31975 :             desc->noloop_assumptions =
    2614                 :       31975 :                     alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
    2615                 :             :           cond = NE;
    2616                 :             :         }
    2617                 :             :     }
    2618                 :             : 
    2619                 :             :   /* Count the number of iterations.  */
    2620                 :       23642 :   if (cond == NE)
    2621                 :             :     {
    2622                 :             :       /* Everything we do here is just arithmetics modulo size of mode.  This
    2623                 :             :          makes us able to do more involved computations of number of iterations
    2624                 :             :          than in other cases.  First transform the condition into shape
    2625                 :             :          s * i <> c, with s positive.  */
    2626                 :      389834 :       iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
    2627                 :      389834 :       iv0.base = const0_rtx;
    2628                 :      389834 :       iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
    2629                 :      389834 :       iv1.step = const0_rtx;
    2630                 :      389834 :       if (INTVAL (iv0.step) < 0)
    2631                 :             :         {
    2632                 :      131342 :           iv0.step = simplify_gen_unary (NEG, comp_mode, iv0.step, comp_mode);
    2633                 :      131342 :           iv1.base = simplify_gen_unary (NEG, comp_mode, iv1.base, comp_mode);
    2634                 :             :         }
    2635                 :      389834 :       iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
    2636                 :             : 
    2637                 :             :       /* Let nsd (s, size of mode) = d.  If d does not divide c, the loop
    2638                 :             :          is infinite.  Otherwise, the number of iterations is
    2639                 :             :          (inverse(s/d) * (c/d)) mod (size of mode/d).  */
    2640                 :      389834 :       s = INTVAL (iv0.step); d = 1;
    2641                 :      900334 :       while (s % 2 != 1)
    2642                 :             :         {
    2643                 :      510500 :           s /= 2;
    2644                 :      510500 :           d *= 2;
    2645                 :      510500 :           size--;
    2646                 :             :         }
    2647                 :      389834 :       bound = gen_int_mode (((uint64_t) 1 << (size - 1) << 1) - 1, mode);
    2648                 :             : 
    2649                 :      389834 :       tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
    2650                 :      389834 :       tmp = simplify_gen_binary (UMOD, mode, tmp1, gen_int_mode (d, mode));
    2651                 :      389834 :       assumption = simplify_gen_relational (NE, SImode, mode, tmp, const0_rtx);
    2652                 :      389834 :       desc->infinite = alloc_EXPR_LIST (0, assumption, desc->infinite);
    2653                 :             : 
    2654                 :      389834 :       tmp = simplify_gen_binary (UDIV, mode, tmp1, gen_int_mode (d, mode));
    2655                 :      389834 :       inv = inverse (s, size);
    2656                 :      389834 :       tmp = simplify_gen_binary (MULT, mode, tmp, gen_int_mode (inv, mode));
    2657                 :      389834 :       desc->niter_expr = simplify_gen_binary (AND, mode, tmp, bound);
    2658                 :             :     }
    2659                 :             :   else
    2660                 :             :     {
    2661                 :       23642 :       if (iv1.step == const0_rtx)
    2662                 :             :         /* Condition in shape a + s * i <= b
    2663                 :             :            We must know that b + s does not overflow and a <= b + s and then we
    2664                 :             :            can compute number of iterations as (b + s - a) / s.  (It might
    2665                 :             :            seem that we in fact could be more clever about testing the b + s
    2666                 :             :            overflow condition using some information about b - a mod s,
    2667                 :             :            but it was already taken into account during LE -> NE transform).  */
    2668                 :             :         {
    2669                 :       20715 :           step = iv0.step;
    2670                 :       20715 :           tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
    2671                 :       20715 :           tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
    2672                 :             : 
    2673                 :       20715 :           bound = simplify_gen_binary (MINUS, mode, mode_mmax,
    2674                 :             :                                        lowpart_subreg (mode, step,
    2675                 :             :                                                        comp_mode));
    2676                 :       20715 :           if (step_is_pow2)
    2677                 :             :             {
    2678                 :       18972 :               rtx t0, t1;
    2679                 :             : 
    2680                 :             :               /* If s is power of 2, we know that the loop is infinite if
    2681                 :             :                  a % s <= b % s and b + s overflows.  */
    2682                 :       18972 :               assumption = simplify_gen_relational (reverse_condition (cond),
    2683                 :             :                                                     SImode, mode,
    2684                 :             :                                                     tmp1, bound);
    2685                 :             : 
    2686                 :       18972 :               t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
    2687                 :       18972 :               t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
    2688                 :       18972 :               tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
    2689                 :       18972 :               assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
    2690                 :       18972 :               desc->infinite =
    2691                 :       18972 :                       alloc_EXPR_LIST (0, assumption, desc->infinite);
    2692                 :             :             }
    2693                 :             :           else
    2694                 :             :             {
    2695                 :        1743 :               assumption = simplify_gen_relational (cond, SImode, mode,
    2696                 :             :                                                     tmp1, bound);
    2697                 :        1743 :               desc->assumptions =
    2698                 :        1743 :                       alloc_EXPR_LIST (0, assumption, desc->assumptions);
    2699                 :             :             }
    2700                 :             : 
    2701                 :       20715 :           tmp = simplify_gen_binary (PLUS, comp_mode, iv1.base, iv0.step);
    2702                 :       20715 :           tmp = lowpart_subreg (mode, tmp, comp_mode);
    2703                 :       20715 :           assumption = simplify_gen_relational (reverse_condition (cond),
    2704                 :             :                                                 SImode, mode, tmp0, tmp);
    2705                 :             : 
    2706                 :       20715 :           delta = simplify_gen_binary (PLUS, mode, tmp1, step);
    2707                 :       20715 :           delta = simplify_gen_binary (MINUS, mode, delta, tmp0);
    2708                 :             :         }
    2709                 :             :       else
    2710                 :             :         {
    2711                 :             :           /* Condition in shape a <= b - s * i
    2712                 :             :              We must know that a - s does not overflow and a - s <= b and then
    2713                 :             :              we can again compute number of iterations as (b - (a - s)) / s.  */
    2714                 :        2927 :           step = simplify_gen_unary (NEG, mode, iv1.step, mode);
    2715                 :        2927 :           tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
    2716                 :        2927 :           tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
    2717                 :             : 
    2718                 :        2927 :           bound = simplify_gen_binary (PLUS, mode, mode_mmin,
    2719                 :             :                                        lowpart_subreg (mode, step, comp_mode));
    2720                 :        2927 :           if (step_is_pow2)
    2721                 :             :             {
    2722                 :        1912 :               rtx t0, t1;
    2723                 :             : 
    2724                 :             :               /* If s is power of 2, we know that the loop is infinite if
    2725                 :             :                  a % s <= b % s and a - s overflows.  */
    2726                 :        1912 :               assumption = simplify_gen_relational (reverse_condition (cond),
    2727                 :             :                                                     SImode, mode,
    2728                 :             :                                                     bound, tmp0);
    2729                 :             : 
    2730                 :        1912 :               t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
    2731                 :        1912 :               t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
    2732                 :        1912 :               tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
    2733                 :        1912 :               assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
    2734                 :        1912 :               desc->infinite =
    2735                 :        1912 :                       alloc_EXPR_LIST (0, assumption, desc->infinite);
    2736                 :             :             }
    2737                 :             :           else
    2738                 :             :             {
    2739                 :        1015 :               assumption = simplify_gen_relational (cond, SImode, mode,
    2740                 :             :                                                     bound, tmp0);
    2741                 :        1015 :               desc->assumptions =
    2742                 :        1015 :                       alloc_EXPR_LIST (0, assumption, desc->assumptions);
    2743                 :             :             }
    2744                 :             : 
    2745                 :        2927 :           tmp = simplify_gen_binary (PLUS, comp_mode, iv0.base, iv1.step);
    2746                 :        2927 :           tmp = lowpart_subreg (mode, tmp, comp_mode);
    2747                 :        2927 :           assumption = simplify_gen_relational (reverse_condition (cond),
    2748                 :             :                                                 SImode, mode,
    2749                 :             :                                                 tmp, tmp1);
    2750                 :        2927 :           delta = simplify_gen_binary (MINUS, mode, tmp0, step);
    2751                 :        2927 :           delta = simplify_gen_binary (MINUS, mode, tmp1, delta);
    2752                 :             :         }
    2753                 :       23642 :       if (assumption == const_true_rtx)
    2754                 :           0 :         goto zero_iter_simplify;
    2755                 :       23642 :       else if (assumption != const0_rtx)
    2756                 :       23487 :         desc->noloop_assumptions =
    2757                 :       23487 :                 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
    2758                 :       23642 :       delta = simplify_gen_binary (UDIV, mode, delta, step);
    2759                 :       23642 :       desc->niter_expr = delta;
    2760                 :             :     }
    2761                 :             : 
    2762                 :      413476 :   old_niter = desc->niter_expr;
    2763                 :             : 
    2764                 :      413476 :   simplify_using_initial_values (loop, AND, &desc->assumptions);
    2765                 :      413476 :   if (desc->assumptions
    2766                 :        2135 :       && XEXP (desc->assumptions, 0) == const0_rtx)
    2767                 :          16 :     goto fail;
    2768                 :      413460 :   simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
    2769                 :      413460 :   simplify_using_initial_values (loop, IOR, &desc->infinite);
    2770                 :      413460 :   simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
    2771                 :             : 
    2772                 :             :   /* Rerun the simplification.  Consider code (created by copying loop headers)
    2773                 :             : 
    2774                 :             :      i = 0;
    2775                 :             : 
    2776                 :             :      if (0 < n)
    2777                 :             :        {
    2778                 :             :          do
    2779                 :             :            {
    2780                 :             :              i++;
    2781                 :             :            } while (i < n);
    2782                 :             :        }
    2783                 :             : 
    2784                 :             :     The first pass determines that i = 0, the second pass uses it to eliminate
    2785                 :             :     noloop assumption.  */
    2786                 :             : 
    2787                 :      413460 :   simplify_using_initial_values (loop, AND, &desc->assumptions);
    2788                 :      413460 :   if (desc->assumptions
    2789                 :        2119 :       && XEXP (desc->assumptions, 0) == const0_rtx)
    2790                 :           0 :     goto fail;
    2791                 :      413460 :   simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
    2792                 :      413460 :   simplify_using_initial_values (loop, IOR, &desc->infinite);
    2793                 :      413460 :   simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
    2794                 :             : 
    2795                 :      413460 :   if (desc->noloop_assumptions
    2796                 :       53911 :       && XEXP (desc->noloop_assumptions, 0) == const_true_rtx)
    2797                 :           0 :     goto zero_iter;
    2798                 :             : 
    2799                 :      413460 :   if (CONST_INT_P (desc->niter_expr))
    2800                 :             :     {
    2801                 :      199823 :       uint64_t val = INTVAL (desc->niter_expr);
    2802                 :             : 
    2803                 :      199823 :       desc->const_iter = true;
    2804                 :      199823 :       desc->niter = val & GET_MODE_MASK (desc->mode);
    2805                 :      199823 :       if (!desc->infinite
    2806                 :      199752 :           && !desc->assumptions)
    2807                 :      199752 :         record_niter_bound (loop, desc->niter, false, true);
    2808                 :             :     }
    2809                 :             :   else
    2810                 :             :     {
    2811                 :      213637 :       max = determine_max_iter (loop, desc, old_niter);
    2812                 :      213637 :       if (!max)
    2813                 :           0 :         goto zero_iter_simplify;
    2814                 :      213637 :       if (!desc->infinite
    2815                 :      139678 :           && !desc->assumptions)
    2816                 :      137559 :         record_niter_bound (loop, max, false, true);
    2817                 :             : 
    2818                 :             :       /* simplify_using_initial_values does a copy propagation on the registers
    2819                 :             :          in the expression for the number of iterations.  This prolongs life
    2820                 :             :          ranges of registers and increases register pressure, and usually
    2821                 :             :          brings no gain (and if it happens to do, the cse pass will take care
    2822                 :             :          of it anyway).  So prevent this behavior, unless it enabled us to
    2823                 :             :          derive that the number of iterations is a constant.  */
    2824                 :      213637 :       desc->niter_expr = old_niter;
    2825                 :             :     }
    2826                 :             : 
    2827                 :             :   return;
    2828                 :             : 
    2829                 :           0 : zero_iter_simplify:
    2830                 :             :   /* Simplify the assumptions.  */
    2831                 :           0 :   simplify_using_initial_values (loop, AND, &desc->assumptions);
    2832                 :           0 :   if (desc->assumptions
    2833                 :           0 :       && XEXP (desc->assumptions, 0) == const0_rtx)
    2834                 :           0 :     goto fail;
    2835                 :           0 :   simplify_using_initial_values (loop, IOR, &desc->infinite);
    2836                 :             : 
    2837                 :             :   /* Fallthru.  */
    2838                 :           0 : zero_iter:
    2839                 :           0 :   desc->const_iter = true;
    2840                 :           0 :   desc->niter = 0;
    2841                 :           0 :   record_niter_bound (loop, 0, true, true);
    2842                 :           0 :   desc->noloop_assumptions = NULL_RTX;
    2843                 :           0 :   desc->niter_expr = const0_rtx;
    2844                 :           0 :   return;
    2845                 :             : 
    2846                 :      311391 : fail:
    2847                 :      311391 :   desc->simple_p = false;
    2848                 :      311391 :   return;
    2849                 :             : }
    2850                 :             : 
    2851                 :             : /* Checks whether E is a simple exit from LOOP and stores its description
    2852                 :             :    into DESC.  */
    2853                 :             : 
    2854                 :             : static void
    2855                 :     1128673 : check_simple_exit (class loop *loop, edge e, class niter_desc *desc)
    2856                 :             : {
    2857                 :     1128673 :   basic_block exit_bb;
    2858                 :     1128673 :   rtx condition;
    2859                 :     1128673 :   rtx_insn *at;
    2860                 :     1128673 :   edge ein;
    2861                 :             : 
    2862                 :     1128673 :   exit_bb = e->src;
    2863                 :     1128673 :   desc->simple_p = false;
    2864                 :             : 
    2865                 :             :   /* It must belong directly to the loop.  */
    2866                 :     1128673 :   if (exit_bb->loop_father != loop)
    2867                 :      403822 :     return;
    2868                 :             : 
    2869                 :             :   /* It must be tested (at least) once during any iteration.  */
    2870                 :     1035957 :   if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit_bb))
    2871                 :             :     return;
    2872                 :             : 
    2873                 :             :   /* It must end in a simple conditional jump.  */
    2874                 :      823159 :   if (!any_condjump_p (BB_END (exit_bb)) || !onlyjump_p (BB_END (exit_bb)))
    2875                 :       64021 :     return;
    2876                 :             : 
    2877                 :      759138 :   ein = EDGE_SUCC (exit_bb, 0);
    2878                 :      759138 :   if (ein == e)
    2879                 :      247012 :     ein = EDGE_SUCC (exit_bb, 1);
    2880                 :             : 
    2881                 :      759138 :   desc->out_edge = e;
    2882                 :      759138 :   desc->in_edge = ein;
    2883                 :             : 
    2884                 :             :   /* Test whether the condition is suitable.  */
    2885                 :      759138 :   if (!(condition = get_condition (BB_END (ein->src), &at, false, false)))
    2886                 :             :     return;
    2887                 :             : 
    2888                 :      728447 :   if (ein->flags & EDGE_FALLTHRU)
    2889                 :             :     {
    2890                 :      184444 :       condition = reversed_condition (condition);
    2891                 :      184444 :       if (!condition)
    2892                 :             :         return;
    2893                 :             :     }
    2894                 :             : 
    2895                 :             :   /* Check that we are able to determine number of iterations and fill
    2896                 :             :      in information about it.  */
    2897                 :      724851 :   iv_number_of_iterations (loop, at, condition, desc);
    2898                 :             : }
    2899                 :             : 
    2900                 :             : /* Finds a simple exit of LOOP and stores its description into DESC.  */
    2901                 :             : 
    2902                 :             : static void
    2903                 :      580682 : find_simple_exit (class loop *loop, class niter_desc *desc)
    2904                 :             : {
    2905                 :      580682 :   unsigned i;
    2906                 :      580682 :   basic_block *body;
    2907                 :      580682 :   edge e;
    2908                 :      580682 :   class niter_desc act;
    2909                 :      580682 :   bool any = false;
    2910                 :      580682 :   edge_iterator ei;
    2911                 :             : 
    2912                 :      580682 :   desc->simple_p = false;
    2913                 :      580682 :   body = get_loop_body (loop);
    2914                 :             : 
    2915                 :     4888323 :   for (i = 0; i < loop->num_nodes; i++)
    2916                 :             :     {
    2917                 :     9555265 :       FOR_EACH_EDGE (e, ei, body[i]->succs)
    2918                 :             :         {
    2919                 :     5828306 :           if (flow_bb_inside_loop_p (loop, e->dest))
    2920                 :     4699633 :             continue;
    2921                 :             : 
    2922                 :     1128673 :           check_simple_exit (loop, e, &act);
    2923                 :     1128673 :           if (!act.simple_p)
    2924                 :      715213 :             continue;
    2925                 :             : 
    2926                 :      413460 :           if (!any)
    2927                 :             :             any = true;
    2928                 :             :           else
    2929                 :             :             {
    2930                 :             :               /* Prefer constant iterations; the less the better.  */
    2931                 :       15758 :               if (!act.const_iter
    2932                 :        1048 :                   || (desc->const_iter && act.niter >= desc->niter))
    2933                 :       15004 :                 continue;
    2934                 :             : 
    2935                 :             :               /* Also if the actual exit may be infinite, while the old one
    2936                 :             :                  not, prefer the old one.  */
    2937                 :         754 :               if (act.infinite && !desc->infinite)
    2938                 :          12 :                 continue;
    2939                 :             :             }
    2940                 :             : 
    2941                 :      398444 :           *desc = act;
    2942                 :             :         }
    2943                 :             :     }
    2944                 :             : 
    2945                 :      580682 :   if (dump_file)
    2946                 :             :     {
    2947                 :         103 :       if (desc->simple_p)
    2948                 :             :         {
    2949                 :          75 :           fprintf (dump_file, "Loop %d is simple:\n", loop->num);
    2950                 :          75 :           fprintf (dump_file, "  simple exit %d -> %d\n",
    2951                 :          75 :                    desc->out_edge->src->index,
    2952                 :          75 :                    desc->out_edge->dest->index);
    2953                 :          75 :           if (desc->assumptions)
    2954                 :             :             {
    2955                 :           0 :               fprintf (dump_file, "  assumptions: ");
    2956                 :           0 :               print_rtl (dump_file, desc->assumptions);
    2957                 :           0 :               fprintf (dump_file, "\n");
    2958                 :             :             }
    2959                 :          75 :           if (desc->noloop_assumptions)
    2960                 :             :             {
    2961                 :           8 :               fprintf (dump_file, "  does not roll if: ");
    2962                 :           8 :               print_rtl (dump_file, desc->noloop_assumptions);
    2963                 :           8 :               fprintf (dump_file, "\n");
    2964                 :             :             }
    2965                 :          75 :           if (desc->infinite)
    2966                 :             :             {
    2967                 :          19 :               fprintf (dump_file, "  infinite if: ");
    2968                 :          19 :               print_rtl (dump_file, desc->infinite);
    2969                 :          19 :               fprintf (dump_file, "\n");
    2970                 :             :             }
    2971                 :             : 
    2972                 :          75 :           fprintf (dump_file, "  number of iterations: ");
    2973                 :          75 :           print_rtl (dump_file, desc->niter_expr);
    2974                 :          75 :           fprintf (dump_file, "\n");
    2975                 :             : 
    2976                 :          75 :           fprintf (dump_file, "  upper bound: %li\n",
    2977                 :             :                    (long)get_max_loop_iterations_int (loop));
    2978                 :          75 :           fprintf (dump_file, "  likely upper bound: %li\n",
    2979                 :             :                    (long)get_likely_max_loop_iterations_int (loop));
    2980                 :          75 :           fprintf (dump_file, "  realistic bound: %li\n",
    2981                 :             :                    (long)get_estimated_loop_iterations_int (loop));
    2982                 :             :         }
    2983                 :             :       else
    2984                 :          28 :         fprintf (dump_file, "Loop %d is not simple.\n", loop->num);
    2985                 :             :     }
    2986                 :             : 
    2987                 :             :   /* Fix up the finiteness if possible.  We can only do it for single exit,
    2988                 :             :      since the loop is finite, but it's possible that we predicate one loop
    2989                 :             :      exit to be finite which can not be determined as finite in middle-end as
    2990                 :             :      well.  It results in incorrect predicate information on the exit condition
    2991                 :             :      expression.  For example, if says [(int) _1 + -8, + , -8] != 0 finite,
    2992                 :             :      it means _1 can exactly divide -8.  */
    2993                 :      580682 :   if (desc->infinite && single_exit (loop) && finite_loop_p (loop))
    2994                 :             :     {
    2995                 :       32553 :       desc->infinite = NULL_RTX;
    2996                 :       32553 :       if (dump_file)
    2997                 :          19 :         fprintf (dump_file, "  infinite updated to finite.\n");
    2998                 :             :     }
    2999                 :             : 
    3000                 :      580682 :   free (body);
    3001                 :      580682 : }
    3002                 :             : 
    3003                 :             : /* Creates a simple loop description of LOOP if it was not computed
    3004                 :             :    already.  */
    3005                 :             : 
    3006                 :             : class niter_desc *
    3007                 :      984073 : get_simple_loop_desc (class loop *loop)
    3008                 :             : {
    3009                 :      984073 :   class niter_desc *desc = simple_loop_desc (loop);
    3010                 :             : 
    3011                 :      984073 :   if (desc)
    3012                 :             :     return desc;
    3013                 :             : 
    3014                 :             :   /* At least desc->infinite is not always initialized by
    3015                 :             :      find_simple_loop_exit.  */
    3016                 :      580682 :   desc = ggc_cleared_alloc<niter_desc> ();
    3017                 :      580682 :   iv_analysis_loop_init (loop);
    3018                 :      580682 :   find_simple_exit (loop, desc);
    3019                 :      580682 :   loop->simple_loop_desc = desc;
    3020                 :      580682 :   return desc;
    3021                 :             : }
    3022                 :             : 
    3023                 :             : /* Releases simple loop description for LOOP.  */
    3024                 :             : 
    3025                 :             : void
    3026                 :     5894353 : free_simple_loop_desc (class loop *loop)
    3027                 :             : {
    3028                 :     5894353 :   class niter_desc *desc = simple_loop_desc (loop);
    3029                 :             : 
    3030                 :     5894353 :   if (!desc)
    3031                 :             :     return;
    3032                 :             : 
    3033                 :      580682 :   ggc_free (desc);
    3034                 :      580682 :   loop->simple_loop_desc = NULL;
    3035                 :             : }
        

Generated by: LCOV version 2.1-beta

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