LCOV - code coverage report
Current view: top level - gcc - tree-ssa-loop-ivopts.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 90.7 % 3747 3397
Test Date: 2024-04-20 14:03:02 Functions: 91.8 % 184 169
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Induction variable optimizations.
       2                 :             :    Copyright (C) 2003-2024 Free Software Foundation, Inc.
       3                 :             : 
       4                 :             : This file is part of GCC.
       5                 :             : 
       6                 :             : GCC is free software; you can redistribute it and/or modify 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 pass tries to find the optimal set of induction variables for the loop.
      21                 :             :    It optimizes just the basic linear induction variables (although adding
      22                 :             :    support for other types should not be too hard).  It includes the
      23                 :             :    optimizations commonly known as strength reduction, induction variable
      24                 :             :    coalescing and induction variable elimination.  It does it in the
      25                 :             :    following steps:
      26                 :             : 
      27                 :             :    1) The interesting uses of induction variables are found.  This includes
      28                 :             : 
      29                 :             :       -- uses of induction variables in non-linear expressions
      30                 :             :       -- addresses of arrays
      31                 :             :       -- comparisons of induction variables
      32                 :             : 
      33                 :             :       Note the interesting uses are categorized and handled in group.
      34                 :             :       Generally, address type uses are grouped together if their iv bases
      35                 :             :       are different in constant offset.
      36                 :             : 
      37                 :             :    2) Candidates for the induction variables are found.  This includes
      38                 :             : 
      39                 :             :       -- old induction variables
      40                 :             :       -- the variables defined by expressions derived from the "interesting
      41                 :             :          groups/uses" above
      42                 :             : 
      43                 :             :    3) The optimal (w.r. to a cost function) set of variables is chosen.  The
      44                 :             :       cost function assigns a cost to sets of induction variables and consists
      45                 :             :       of three parts:
      46                 :             : 
      47                 :             :       -- The group/use costs.  Each of the interesting groups/uses chooses
      48                 :             :          the best induction variable in the set and adds its cost to the sum.
      49                 :             :          The cost reflects the time spent on modifying the induction variables
      50                 :             :          value to be usable for the given purpose (adding base and offset for
      51                 :             :          arrays, etc.).
      52                 :             :       -- The variable costs.  Each of the variables has a cost assigned that
      53                 :             :          reflects the costs associated with incrementing the value of the
      54                 :             :          variable.  The original variables are somewhat preferred.
      55                 :             :       -- The set cost.  Depending on the size of the set, extra cost may be
      56                 :             :          added to reflect register pressure.
      57                 :             : 
      58                 :             :       All the costs are defined in a machine-specific way, using the target
      59                 :             :       hooks and machine descriptions to determine them.
      60                 :             : 
      61                 :             :    4) The trees are transformed to use the new variables, the dead code is
      62                 :             :       removed.
      63                 :             : 
      64                 :             :    All of this is done loop by loop.  Doing it globally is theoretically
      65                 :             :    possible, it might give a better performance and it might enable us
      66                 :             :    to decide costs more precisely, but getting all the interactions right
      67                 :             :    would be complicated.
      68                 :             : 
      69                 :             :    For the targets supporting low-overhead loops, IVOPTs has to take care of
      70                 :             :    the loops which will probably be transformed in RTL doloop optimization,
      71                 :             :    to try to make selected IV candidate set optimal.  The process of doloop
      72                 :             :    support includes:
      73                 :             : 
      74                 :             :    1) Analyze the current loop will be transformed to doloop or not, find and
      75                 :             :       mark its compare type IV use as doloop use (iv_group field doloop_p), and
      76                 :             :       set flag doloop_use_p of ivopts_data to notify subsequent processings on
      77                 :             :       doloop.  See analyze_and_mark_doloop_use and its callees for the details.
      78                 :             :       The target hook predict_doloop_p can be used for target specific checks.
      79                 :             : 
      80                 :             :    2) Add one doloop dedicated IV cand {(may_be_zero ? 1 : (niter + 1)), +, -1},
      81                 :             :       set flag doloop_p of iv_cand, step cost is set as zero and no extra cost
      82                 :             :       like biv.  For cost determination between doloop IV cand and IV use, the
      83                 :             :       target hooks doloop_cost_for_generic and doloop_cost_for_address are
      84                 :             :       provided to add on extra costs for generic type and address type IV use.
      85                 :             :       Zero cost is assigned to the pair between doloop IV cand and doloop IV
      86                 :             :       use, and bound zero is set for IV elimination.
      87                 :             : 
      88                 :             :    3) With the cost setting in step 2), the current cost model based IV
      89                 :             :       selection algorithm will process as usual, pick up doloop dedicated IV if
      90                 :             :       profitable.  */
      91                 :             : 
      92                 :             : #include "config.h"
      93                 :             : #include "system.h"
      94                 :             : #include "coretypes.h"
      95                 :             : #include "backend.h"
      96                 :             : #include "rtl.h"
      97                 :             : #include "tree.h"
      98                 :             : #include "gimple.h"
      99                 :             : #include "cfghooks.h"
     100                 :             : #include "tree-pass.h"
     101                 :             : #include "memmodel.h"
     102                 :             : #include "tm_p.h"
     103                 :             : #include "ssa.h"
     104                 :             : #include "expmed.h"
     105                 :             : #include "insn-config.h"
     106                 :             : #include "emit-rtl.h"
     107                 :             : #include "recog.h"
     108                 :             : #include "cgraph.h"
     109                 :             : #include "gimple-pretty-print.h"
     110                 :             : #include "alias.h"
     111                 :             : #include "fold-const.h"
     112                 :             : #include "stor-layout.h"
     113                 :             : #include "tree-eh.h"
     114                 :             : #include "gimplify.h"
     115                 :             : #include "gimple-iterator.h"
     116                 :             : #include "gimplify-me.h"
     117                 :             : #include "tree-cfg.h"
     118                 :             : #include "tree-ssa-loop-ivopts.h"
     119                 :             : #include "tree-ssa-loop-manip.h"
     120                 :             : #include "tree-ssa-loop-niter.h"
     121                 :             : #include "tree-ssa-loop.h"
     122                 :             : #include "explow.h"
     123                 :             : #include "expr.h"
     124                 :             : #include "tree-dfa.h"
     125                 :             : #include "tree-ssa.h"
     126                 :             : #include "cfgloop.h"
     127                 :             : #include "tree-scalar-evolution.h"
     128                 :             : #include "tree-affine.h"
     129                 :             : #include "tree-ssa-propagate.h"
     130                 :             : #include "tree-ssa-address.h"
     131                 :             : #include "builtins.h"
     132                 :             : #include "tree-vectorizer.h"
     133                 :             : #include "dbgcnt.h"
     134                 :             : #include "cfganal.h"
     135                 :             : 
     136                 :             : /* For lang_hooks.types.type_for_mode.  */
     137                 :             : #include "langhooks.h"
     138                 :             : 
     139                 :             : /* FIXME: Expressions are expanded to RTL in this pass to determine the
     140                 :             :    cost of different addressing modes.  This should be moved to a TBD
     141                 :             :    interface between the GIMPLE and RTL worlds.  */
     142                 :             : 
     143                 :             : /* The infinite cost.  */
     144                 :             : #define INFTY 1000000000
     145                 :             : 
     146                 :             : /* Returns the expected number of loop iterations for LOOP.
     147                 :             :    The average trip count is computed from profile data if it
     148                 :             :    exists. */
     149                 :             : 
     150                 :             : static inline HOST_WIDE_INT
     151                 :     7357487 : avg_loop_niter (class loop *loop)
     152                 :             : {
     153                 :     7357487 :   HOST_WIDE_INT niter = estimated_stmt_executions_int (loop);
     154                 :     7357487 :   if (niter == -1)
     155                 :             :     {
     156                 :     4148198 :       niter = likely_max_stmt_executions_int (loop);
     157                 :             : 
     158                 :     4148198 :       if (niter == -1 || niter > param_avg_loop_niter)
     159                 :     3710908 :         return param_avg_loop_niter;
     160                 :             :     }
     161                 :             : 
     162                 :             :   return niter;
     163                 :             : }
     164                 :             : 
     165                 :             : struct iv_use;
     166                 :             : 
     167                 :             : /* Representation of the induction variable.  */
     168                 :             : struct iv
     169                 :             : {
     170                 :             :   tree base;            /* Initial value of the iv.  */
     171                 :             :   tree base_object;     /* A memory object to that the induction variable points.  */
     172                 :             :   tree step;            /* Step of the iv (constant only).  */
     173                 :             :   tree ssa_name;        /* The ssa name with the value.  */
     174                 :             :   struct iv_use *nonlin_use;    /* The identifier in the use if it is the case.  */
     175                 :             :   bool biv_p;           /* Is it a biv?  */
     176                 :             :   bool no_overflow;     /* True if the iv doesn't overflow.  */
     177                 :             :   bool have_address_use;/* For biv, indicate if it's used in any address
     178                 :             :                            type use.  */
     179                 :             : };
     180                 :             : 
     181                 :             : /* Per-ssa version information (induction variable descriptions, etc.).  */
     182                 :             : struct version_info
     183                 :             : {
     184                 :             :   tree name;            /* The ssa name.  */
     185                 :             :   struct iv *iv;        /* Induction variable description.  */
     186                 :             :   bool has_nonlin_use;  /* For a loop-level invariant, whether it is used in
     187                 :             :                            an expression that is not an induction variable.  */
     188                 :             :   bool preserve_biv;    /* For the original biv, whether to preserve it.  */
     189                 :             :   unsigned inv_id;      /* Id of an invariant.  */
     190                 :             : };
     191                 :             : 
     192                 :             : /* Types of uses.  */
     193                 :             : enum use_type
     194                 :             : {
     195                 :             :   USE_NONLINEAR_EXPR,   /* Use in a nonlinear expression.  */
     196                 :             :   USE_REF_ADDRESS,      /* Use is an address for an explicit memory
     197                 :             :                            reference.  */
     198                 :             :   USE_PTR_ADDRESS,      /* Use is a pointer argument to a function in
     199                 :             :                            cases where the expansion of the function
     200                 :             :                            will turn the argument into a normal address.  */
     201                 :             :   USE_COMPARE           /* Use is a compare.  */
     202                 :             : };
     203                 :             : 
     204                 :             : /* Cost of a computation.  */
     205                 :             : class comp_cost
     206                 :             : {
     207                 :             : public:
     208                 :   112983163 :   comp_cost (): cost (0), complexity (0), scratch (0)
     209                 :             :   {}
     210                 :             : 
     211                 :    21775358 :   comp_cost (int64_t cost, unsigned complexity, int64_t scratch = 0)
     212                 :    13097888 :     : cost (cost), complexity (complexity), scratch (scratch)
     213                 :    12384973 :   {}
     214                 :             : 
     215                 :             :   /* Returns true if COST is infinite.  */
     216                 :             :   bool infinite_cost_p ();
     217                 :             : 
     218                 :             :   /* Adds costs COST1 and COST2.  */
     219                 :             :   friend comp_cost operator+ (comp_cost cost1, comp_cost cost2);
     220                 :             : 
     221                 :             :   /* Adds COST to the comp_cost.  */
     222                 :             :   comp_cost operator+= (comp_cost cost);
     223                 :             : 
     224                 :             :   /* Adds constant C to this comp_cost.  */
     225                 :             :   comp_cost operator+= (HOST_WIDE_INT c);
     226                 :             : 
     227                 :             :   /* Subtracts constant C to this comp_cost.  */
     228                 :             :   comp_cost operator-= (HOST_WIDE_INT c);
     229                 :             : 
     230                 :             :   /* Divide the comp_cost by constant C.  */
     231                 :             :   comp_cost operator/= (HOST_WIDE_INT c);
     232                 :             : 
     233                 :             :   /* Multiply the comp_cost by constant C.  */
     234                 :             :   comp_cost operator*= (HOST_WIDE_INT c);
     235                 :             : 
     236                 :             :   /* Subtracts costs COST1 and COST2.  */
     237                 :             :   friend comp_cost operator- (comp_cost cost1, comp_cost cost2);
     238                 :             : 
     239                 :             :   /* Subtracts COST from this comp_cost.  */
     240                 :             :   comp_cost operator-= (comp_cost cost);
     241                 :             : 
     242                 :             :   /* Returns true if COST1 is smaller than COST2.  */
     243                 :             :   friend bool operator< (comp_cost cost1, comp_cost cost2);
     244                 :             : 
     245                 :             :   /* Returns true if COST1 and COST2 are equal.  */
     246                 :             :   friend bool operator== (comp_cost cost1, comp_cost cost2);
     247                 :             : 
     248                 :             :   /* Returns true if COST1 is smaller or equal than COST2.  */
     249                 :             :   friend bool operator<= (comp_cost cost1, comp_cost cost2);
     250                 :             : 
     251                 :             :   int64_t cost;         /* The runtime cost.  */
     252                 :             :   unsigned complexity;  /* The estimate of the complexity of the code for
     253                 :             :                            the computation (in no concrete units --
     254                 :             :                            complexity field should be larger for more
     255                 :             :                            complex expressions and addressing modes).  */
     256                 :             :   int64_t scratch;      /* Scratch used during cost computation.  */
     257                 :             : };
     258                 :             : 
     259                 :             : static const comp_cost no_cost;
     260                 :             : static const comp_cost infinite_cost (INFTY, 0, INFTY);
     261                 :             : 
     262                 :             : bool
     263                 :  1467887756 : comp_cost::infinite_cost_p ()
     264                 :             : {
     265                 :  1467887756 :   return cost == INFTY;
     266                 :             : }
     267                 :             : 
     268                 :             : comp_cost
     269                 :   196612384 : operator+ (comp_cost cost1, comp_cost cost2)
     270                 :             : {
     271                 :   196612384 :   if (cost1.infinite_cost_p () || cost2.infinite_cost_p ())
     272                 :     1615787 :     return infinite_cost;
     273                 :             : 
     274                 :   194996597 :   gcc_assert (cost1.cost + cost2.cost < infinite_cost.cost);
     275                 :   194996597 :   cost1.cost += cost2.cost;
     276                 :   194996597 :   cost1.complexity += cost2.complexity;
     277                 :             : 
     278                 :   194996597 :   return cost1;
     279                 :             : }
     280                 :             : 
     281                 :             : comp_cost
     282                 :   165786342 : operator- (comp_cost cost1, comp_cost cost2)
     283                 :             : {
     284                 :   165786342 :   if (cost1.infinite_cost_p ())
     285                 :           0 :     return infinite_cost;
     286                 :             : 
     287                 :   165786342 :   gcc_assert (!cost2.infinite_cost_p ());
     288                 :   165786342 :   gcc_assert (cost1.cost - cost2.cost < infinite_cost.cost);
     289                 :             : 
     290                 :   165786342 :   cost1.cost -= cost2.cost;
     291                 :   165786342 :   cost1.complexity -= cost2.complexity;
     292                 :             : 
     293                 :   165786342 :   return cost1;
     294                 :             : }
     295                 :             : 
     296                 :             : comp_cost
     297                 :   196612384 : comp_cost::operator+= (comp_cost cost)
     298                 :             : {
     299                 :   196612384 :   *this = *this + cost;
     300                 :   196612384 :   return *this;
     301                 :             : }
     302                 :             : 
     303                 :             : comp_cost
     304                 :   686995958 : comp_cost::operator+= (HOST_WIDE_INT c)
     305                 :             : {
     306                 :   686995958 :   if (c >= INFTY)
     307                 :           0 :     this->cost = INFTY;
     308                 :             : 
     309                 :   686995958 :   if (infinite_cost_p ())
     310                 :           0 :     return *this;
     311                 :             : 
     312                 :   686995958 :   gcc_assert (this->cost + c < infinite_cost.cost);
     313                 :   686995958 :   this->cost += c;
     314                 :             : 
     315                 :   686995958 :   return *this;
     316                 :             : }
     317                 :             : 
     318                 :             : comp_cost
     319                 :     5728126 : comp_cost::operator-= (HOST_WIDE_INT c)
     320                 :             : {
     321                 :     5728126 :   if (infinite_cost_p ())
     322                 :     2557031 :     return *this;
     323                 :             : 
     324                 :     3171095 :   gcc_assert (this->cost - c < infinite_cost.cost);
     325                 :     3171095 :   this->cost -= c;
     326                 :             : 
     327                 :     3171095 :   return *this;
     328                 :             : }
     329                 :             : 
     330                 :             : comp_cost
     331                 :           0 : comp_cost::operator/= (HOST_WIDE_INT c)
     332                 :             : {
     333                 :           0 :   gcc_assert (c != 0);
     334                 :           0 :   if (infinite_cost_p ())
     335                 :           0 :     return *this;
     336                 :             : 
     337                 :           0 :   this->cost /= c;
     338                 :             : 
     339                 :           0 :   return *this;
     340                 :             : }
     341                 :             : 
     342                 :             : comp_cost
     343                 :           0 : comp_cost::operator*= (HOST_WIDE_INT c)
     344                 :             : {
     345                 :           0 :   if (infinite_cost_p ())
     346                 :           0 :     return *this;
     347                 :             : 
     348                 :           0 :   gcc_assert (this->cost * c < infinite_cost.cost);
     349                 :           0 :   this->cost *= c;
     350                 :             : 
     351                 :           0 :   return *this;
     352                 :             : }
     353                 :             : 
     354                 :             : comp_cost
     355                 :   165786342 : comp_cost::operator-= (comp_cost cost)
     356                 :             : {
     357                 :   165786342 :   *this = *this - cost;
     358                 :   165786342 :   return *this;
     359                 :             : }
     360                 :             : 
     361                 :             : bool
     362                 :   152525079 : operator< (comp_cost cost1, comp_cost cost2)
     363                 :             : {
     364                 :   152525079 :   if (cost1.cost == cost2.cost)
     365                 :    67934729 :     return cost1.complexity < cost2.complexity;
     366                 :             : 
     367                 :    84590350 :   return cost1.cost < cost2.cost;
     368                 :             : }
     369                 :             : 
     370                 :             : bool
     371                 :     3359259 : operator== (comp_cost cost1, comp_cost cost2)
     372                 :             : {
     373                 :     3359259 :   return cost1.cost == cost2.cost
     374                 :     3359259 :     && cost1.complexity == cost2.complexity;
     375                 :             : }
     376                 :             : 
     377                 :             : bool
     378                 :     5530053 : operator<= (comp_cost cost1, comp_cost cost2)
     379                 :             : {
     380                 :     5530053 :   return cost1 < cost2 || cost1 == cost2;
     381                 :             : }
     382                 :             : 
     383                 :             : struct iv_inv_expr_ent;
     384                 :             : 
     385                 :             : /* The candidate - cost pair.  */
     386                 :             : class cost_pair
     387                 :             : {
     388                 :             : public:
     389                 :             :   struct iv_cand *cand; /* The candidate.  */
     390                 :             :   comp_cost cost;       /* The cost.  */
     391                 :             :   enum tree_code comp;  /* For iv elimination, the comparison.  */
     392                 :             :   bitmap inv_vars;      /* The list of invariant ssa_vars that have to be
     393                 :             :                            preserved when representing iv_use with iv_cand.  */
     394                 :             :   bitmap inv_exprs;     /* The list of newly created invariant expressions
     395                 :             :                            when representing iv_use with iv_cand.  */
     396                 :             :   tree value;           /* For final value elimination, the expression for
     397                 :             :                            the final value of the iv.  For iv elimination,
     398                 :             :                            the new bound to compare with.  */
     399                 :             : };
     400                 :             : 
     401                 :             : /* Use.  */
     402                 :             : struct iv_use
     403                 :             : {
     404                 :             :   unsigned id;          /* The id of the use.  */
     405                 :             :   unsigned group_id;    /* The group id the use belongs to.  */
     406                 :             :   enum use_type type;   /* Type of the use.  */
     407                 :             :   tree mem_type;        /* The memory type to use when testing whether an
     408                 :             :                            address is legitimate, and what the address's
     409                 :             :                            cost is.  */
     410                 :             :   struct iv *iv;        /* The induction variable it is based on.  */
     411                 :             :   gimple *stmt;         /* Statement in that it occurs.  */
     412                 :             :   tree *op_p;           /* The place where it occurs.  */
     413                 :             : 
     414                 :             :   tree addr_base;       /* Base address with const offset stripped.  */
     415                 :             :   poly_uint64 addr_offset;
     416                 :             :                         /* Const offset stripped from base address.  */
     417                 :             : };
     418                 :             : 
     419                 :             : /* Group of uses.  */
     420                 :             : struct iv_group
     421                 :             : {
     422                 :             :   /* The id of the group.  */
     423                 :             :   unsigned id;
     424                 :             :   /* Uses of the group are of the same type.  */
     425                 :             :   enum use_type type;
     426                 :             :   /* The set of "related" IV candidates, plus the important ones.  */
     427                 :             :   bitmap related_cands;
     428                 :             :   /* Number of IV candidates in the cost_map.  */
     429                 :             :   unsigned n_map_members;
     430                 :             :   /* The costs wrto the iv candidates.  */
     431                 :             :   class cost_pair *cost_map;
     432                 :             :   /* The selected candidate for the group.  */
     433                 :             :   struct iv_cand *selected;
     434                 :             :   /* To indicate this is a doloop use group.  */
     435                 :             :   bool doloop_p;
     436                 :             :   /* Uses in the group.  */
     437                 :             :   vec<struct iv_use *> vuses;
     438                 :             : };
     439                 :             : 
     440                 :             : /* The position where the iv is computed.  */
     441                 :             : enum iv_position
     442                 :             : {
     443                 :             :   IP_NORMAL,            /* At the end, just before the exit condition.  */
     444                 :             :   IP_END,               /* At the end of the latch block.  */
     445                 :             :   IP_BEFORE_USE,        /* Immediately before a specific use.  */
     446                 :             :   IP_AFTER_USE,         /* Immediately after a specific use.  */
     447                 :             :   IP_ORIGINAL           /* The original biv.  */
     448                 :             : };
     449                 :             : 
     450                 :             : /* The induction variable candidate.  */
     451                 :             : struct iv_cand
     452                 :             : {
     453                 :             :   unsigned id;          /* The number of the candidate.  */
     454                 :             :   bool important;       /* Whether this is an "important" candidate, i.e. such
     455                 :             :                            that it should be considered by all uses.  */
     456                 :             :   bool involves_undefs; /* Whether the IV involves undefined values.  */
     457                 :             :   ENUM_BITFIELD(iv_position) pos : 8;   /* Where it is computed.  */
     458                 :             :   gimple *incremented_at;/* For original biv, the statement where it is
     459                 :             :                            incremented.  */
     460                 :             :   tree var_before;      /* The variable used for it before increment.  */
     461                 :             :   tree var_after;       /* The variable used for it after increment.  */
     462                 :             :   struct iv *iv;        /* The value of the candidate.  NULL for
     463                 :             :                            "pseudocandidate" used to indicate the possibility
     464                 :             :                            to replace the final value of an iv by direct
     465                 :             :                            computation of the value.  */
     466                 :             :   unsigned cost;        /* Cost of the candidate.  */
     467                 :             :   unsigned cost_step;   /* Cost of the candidate's increment operation.  */
     468                 :             :   struct iv_use *ainc_use; /* For IP_{BEFORE,AFTER}_USE candidates, the place
     469                 :             :                               where it is incremented.  */
     470                 :             :   bitmap inv_vars;      /* The list of invariant ssa_vars used in step of the
     471                 :             :                            iv_cand.  */
     472                 :             :   bitmap inv_exprs;     /* If step is more complicated than a single ssa_var,
     473                 :             :                            handle it as a new invariant expression which will
     474                 :             :                            be hoisted out of loop.  */
     475                 :             :   struct iv *orig_iv;   /* The original iv if this cand is added from biv with
     476                 :             :                            smaller type.  */
     477                 :             :   bool doloop_p;        /* Whether this is a doloop candidate.  */
     478                 :             : };
     479                 :             : 
     480                 :             : /* Hashtable entry for common candidate derived from iv uses.  */
     481                 :     2479757 : class iv_common_cand
     482                 :             : {
     483                 :             : public:
     484                 :             :   tree base;
     485                 :             :   tree step;
     486                 :             :   /* IV uses from which this common candidate is derived.  */
     487                 :             :   auto_vec<struct iv_use *> uses;
     488                 :             :   hashval_t hash;
     489                 :             : };
     490                 :             : 
     491                 :             : /* Hashtable helpers.  */
     492                 :             : 
     493                 :             : struct iv_common_cand_hasher : delete_ptr_hash <iv_common_cand>
     494                 :             : {
     495                 :             :   static inline hashval_t hash (const iv_common_cand *);
     496                 :             :   static inline bool equal (const iv_common_cand *, const iv_common_cand *);
     497                 :             : };
     498                 :             : 
     499                 :             : /* Hash function for possible common candidates.  */
     500                 :             : 
     501                 :             : inline hashval_t
     502                 :    10047306 : iv_common_cand_hasher::hash (const iv_common_cand *ccand)
     503                 :             : {
     504                 :    10047306 :   return ccand->hash;
     505                 :             : }
     506                 :             : 
     507                 :             : /* Hash table equality function for common candidates.  */
     508                 :             : 
     509                 :             : inline bool
     510                 :    11389954 : iv_common_cand_hasher::equal (const iv_common_cand *ccand1,
     511                 :             :                               const iv_common_cand *ccand2)
     512                 :             : {
     513                 :    11389954 :   return (ccand1->hash == ccand2->hash
     514                 :     1604427 :           && operand_equal_p (ccand1->base, ccand2->base, 0)
     515                 :     1579429 :           && operand_equal_p (ccand1->step, ccand2->step, 0)
     516                 :    12963365 :           && (TYPE_PRECISION (TREE_TYPE (ccand1->base))
     517                 :     1573411 :               == TYPE_PRECISION (TREE_TYPE (ccand2->base))));
     518                 :             : }
     519                 :             : 
     520                 :             : /* Loop invariant expression hashtable entry.  */
     521                 :             : 
     522                 :             : struct iv_inv_expr_ent
     523                 :             : {
     524                 :             :   /* Tree expression of the entry.  */
     525                 :             :   tree expr;
     526                 :             :   /* Unique indentifier.  */
     527                 :             :   int id;
     528                 :             :   /* Hash value.  */
     529                 :             :   hashval_t hash;
     530                 :             : };
     531                 :             : 
     532                 :             : /* Sort iv_inv_expr_ent pair A and B by id field.  */
     533                 :             : 
     534                 :             : static int
     535                 :        8086 : sort_iv_inv_expr_ent (const void *a, const void *b)
     536                 :             : {
     537                 :        8086 :   const iv_inv_expr_ent * const *e1 = (const iv_inv_expr_ent * const *) (a);
     538                 :        8086 :   const iv_inv_expr_ent * const *e2 = (const iv_inv_expr_ent * const *) (b);
     539                 :             : 
     540                 :        8086 :   unsigned id1 = (*e1)->id;
     541                 :        8086 :   unsigned id2 = (*e2)->id;
     542                 :             : 
     543                 :        8086 :   if (id1 < id2)
     544                 :             :     return -1;
     545                 :        3789 :   else if (id1 > id2)
     546                 :             :     return 1;
     547                 :             :   else
     548                 :           0 :     return 0;
     549                 :             : }
     550                 :             : 
     551                 :             : /* Hashtable helpers.  */
     552                 :             : 
     553                 :             : struct iv_inv_expr_hasher : free_ptr_hash <iv_inv_expr_ent>
     554                 :             : {
     555                 :             :   static inline hashval_t hash (const iv_inv_expr_ent *);
     556                 :             :   static inline bool equal (const iv_inv_expr_ent *, const iv_inv_expr_ent *);
     557                 :             : };
     558                 :             : 
     559                 :             : /* Return true if uses of type TYPE represent some form of address.  */
     560                 :             : 
     561                 :             : inline bool
     562                 :     7666800 : address_p (use_type type)
     563                 :             : {
     564                 :     7666800 :   return type == USE_REF_ADDRESS || type == USE_PTR_ADDRESS;
     565                 :             : }
     566                 :             : 
     567                 :             : /* Hash function for loop invariant expressions.  */
     568                 :             : 
     569                 :             : inline hashval_t
     570                 :     5756129 : iv_inv_expr_hasher::hash (const iv_inv_expr_ent *expr)
     571                 :             : {
     572                 :     5756129 :   return expr->hash;
     573                 :             : }
     574                 :             : 
     575                 :             : /* Hash table equality function for expressions.  */
     576                 :             : 
     577                 :             : inline bool
     578                 :     6899809 : iv_inv_expr_hasher::equal (const iv_inv_expr_ent *expr1,
     579                 :             :                            const iv_inv_expr_ent *expr2)
     580                 :             : {
     581                 :     6899809 :   return expr1->hash == expr2->hash
     582                 :     6899809 :          && operand_equal_p (expr1->expr, expr2->expr, 0);
     583                 :             : }
     584                 :             : 
     585                 :             : struct ivopts_data
     586                 :             : {
     587                 :             :   /* The currently optimized loop.  */
     588                 :             :   class loop *current_loop;
     589                 :             :   location_t loop_loc;
     590                 :             : 
     591                 :             :   /* Numbers of iterations for all exits of the current loop.  */
     592                 :             :   hash_map<edge, tree_niter_desc *> *niters;
     593                 :             : 
     594                 :             :   /* Number of registers used in it.  */
     595                 :             :   unsigned regs_used;
     596                 :             : 
     597                 :             :   /* The size of version_info array allocated.  */
     598                 :             :   unsigned version_info_size;
     599                 :             : 
     600                 :             :   /* The array of information for the ssa names.  */
     601                 :             :   struct version_info *version_info;
     602                 :             : 
     603                 :             :   /* The hashtable of loop invariant expressions created
     604                 :             :      by ivopt.  */
     605                 :             :   hash_table<iv_inv_expr_hasher> *inv_expr_tab;
     606                 :             : 
     607                 :             :   /* The bitmap of indices in version_info whose value was changed.  */
     608                 :             :   bitmap relevant;
     609                 :             : 
     610                 :             :   /* The uses of induction variables.  */
     611                 :             :   vec<iv_group *> vgroups;
     612                 :             : 
     613                 :             :   /* The candidates.  */
     614                 :             :   vec<iv_cand *> vcands;
     615                 :             : 
     616                 :             :   /* A bitmap of important candidates.  */
     617                 :             :   bitmap important_candidates;
     618                 :             : 
     619                 :             :   /* Cache used by tree_to_aff_combination_expand.  */
     620                 :             :   hash_map<tree, name_expansion *> *name_expansion_cache;
     621                 :             : 
     622                 :             :   /* The hashtable of common candidates derived from iv uses.  */
     623                 :             :   hash_table<iv_common_cand_hasher> *iv_common_cand_tab;
     624                 :             : 
     625                 :             :   /* The common candidates.  */
     626                 :             :   vec<iv_common_cand *> iv_common_cands;
     627                 :             : 
     628                 :             :   /* Hash map recording base object information of tree exp.  */
     629                 :             :   hash_map<tree, tree> *base_object_map;
     630                 :             : 
     631                 :             :   /* The maximum invariant variable id.  */
     632                 :             :   unsigned max_inv_var_id;
     633                 :             : 
     634                 :             :   /* The maximum invariant expression id.  */
     635                 :             :   unsigned max_inv_expr_id;
     636                 :             : 
     637                 :             :   /* Number of no_overflow BIVs which are not used in memory address.  */
     638                 :             :   unsigned bivs_not_used_in_addr;
     639                 :             : 
     640                 :             :   /* Obstack for iv structure.  */
     641                 :             :   struct obstack iv_obstack;
     642                 :             : 
     643                 :             :   /* Whether to consider just related and important candidates when replacing a
     644                 :             :      use.  */
     645                 :             :   bool consider_all_candidates;
     646                 :             : 
     647                 :             :   /* Are we optimizing for speed?  */
     648                 :             :   bool speed;
     649                 :             : 
     650                 :             :   /* Whether the loop body includes any function calls.  */
     651                 :             :   bool body_includes_call;
     652                 :             : 
     653                 :             :   /* Whether the loop body can only be exited via single exit.  */
     654                 :             :   bool loop_single_exit_p;
     655                 :             : 
     656                 :             :   /* Whether the loop has doloop comparison use.  */
     657                 :             :   bool doloop_use_p;
     658                 :             : };
     659                 :             : 
     660                 :             : /* An assignment of iv candidates to uses.  */
     661                 :             : 
     662                 :             : class iv_ca
     663                 :             : {
     664                 :             : public:
     665                 :             :   /* The number of uses covered by the assignment.  */
     666                 :             :   unsigned upto;
     667                 :             : 
     668                 :             :   /* Number of uses that cannot be expressed by the candidates in the set.  */
     669                 :             :   unsigned bad_groups;
     670                 :             : 
     671                 :             :   /* Candidate assigned to a use, together with the related costs.  */
     672                 :             :   class cost_pair **cand_for_group;
     673                 :             : 
     674                 :             :   /* Number of times each candidate is used.  */
     675                 :             :   unsigned *n_cand_uses;
     676                 :             : 
     677                 :             :   /* The candidates used.  */
     678                 :             :   bitmap cands;
     679                 :             : 
     680                 :             :   /* The number of candidates in the set.  */
     681                 :             :   unsigned n_cands;
     682                 :             : 
     683                 :             :   /* The number of invariants needed, including both invariant variants and
     684                 :             :      invariant expressions.  */
     685                 :             :   unsigned n_invs;
     686                 :             : 
     687                 :             :   /* Total cost of expressing uses.  */
     688                 :             :   comp_cost cand_use_cost;
     689                 :             : 
     690                 :             :   /* Total cost of candidates.  */
     691                 :             :   int64_t cand_cost;
     692                 :             : 
     693                 :             :   /* Number of times each invariant variable is used.  */
     694                 :             :   unsigned *n_inv_var_uses;
     695                 :             : 
     696                 :             :   /* Number of times each invariant expression is used.  */
     697                 :             :   unsigned *n_inv_expr_uses;
     698                 :             : 
     699                 :             :   /* Total cost of the assignment.  */
     700                 :             :   comp_cost cost;
     701                 :             : };
     702                 :             : 
     703                 :             : /* Difference of two iv candidate assignments.  */
     704                 :             : 
     705                 :             : struct iv_ca_delta
     706                 :             : {
     707                 :             :   /* Changed group.  */
     708                 :             :   struct iv_group *group;
     709                 :             : 
     710                 :             :   /* An old assignment (for rollback purposes).  */
     711                 :             :   class cost_pair *old_cp;
     712                 :             : 
     713                 :             :   /* A new assignment.  */
     714                 :             :   class cost_pair *new_cp;
     715                 :             : 
     716                 :             :   /* Next change in the list.  */
     717                 :             :   struct iv_ca_delta *next;
     718                 :             : };
     719                 :             : 
     720                 :             : /* Bound on number of candidates below that all candidates are considered.  */
     721                 :             : 
     722                 :             : #define CONSIDER_ALL_CANDIDATES_BOUND \
     723                 :             :   ((unsigned) param_iv_consider_all_candidates_bound)
     724                 :             : 
     725                 :             : /* If there are more iv occurrences, we just give up (it is quite unlikely that
     726                 :             :    optimizing such a loop would help, and it would take ages).  */
     727                 :             : 
     728                 :             : #define MAX_CONSIDERED_GROUPS \
     729                 :             :   ((unsigned) param_iv_max_considered_uses)
     730                 :             : 
     731                 :             : /* If there are at most this number of ivs in the set, try removing unnecessary
     732                 :             :    ivs from the set always.  */
     733                 :             : 
     734                 :             : #define ALWAYS_PRUNE_CAND_SET_BOUND \
     735                 :             :   ((unsigned) param_iv_always_prune_cand_set_bound)
     736                 :             : 
     737                 :             : /* The list of trees for that the decl_rtl field must be reset is stored
     738                 :             :    here.  */
     739                 :             : 
     740                 :             : static vec<tree> decl_rtl_to_reset;
     741                 :             : 
     742                 :             : static comp_cost force_expr_to_var_cost (tree, bool);
     743                 :             : 
     744                 :             : /* The single loop exit if it dominates the latch, NULL otherwise.  */
     745                 :             : 
     746                 :             : edge
     747                 :      601329 : single_dom_exit (class loop *loop)
     748                 :             : {
     749                 :      601329 :   edge exit = single_exit (loop);
     750                 :             : 
     751                 :      601329 :   if (!exit)
     752                 :             :     return NULL;
     753                 :             : 
     754                 :      400593 :   if (!just_once_each_iteration_p (loop, exit->src))
     755                 :             :     return NULL;
     756                 :             : 
     757                 :             :   return exit;
     758                 :             : }
     759                 :             : 
     760                 :             : /* Dumps information about the induction variable IV to FILE.  Don't dump
     761                 :             :    variable's name if DUMP_NAME is FALSE.  The information is dumped with
     762                 :             :    preceding spaces indicated by INDENT_LEVEL.  */
     763                 :             : 
     764                 :             : void
     765                 :        2053 : dump_iv (FILE *file, struct iv *iv, bool dump_name, unsigned indent_level)
     766                 :             : {
     767                 :        2053 :   const char *p;
     768                 :        2053 :   const char spaces[9] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0'};
     769                 :             : 
     770                 :        2053 :   if (indent_level > 4)
     771                 :             :     indent_level = 4;
     772                 :        2053 :   p = spaces + 8 - (indent_level << 1);
     773                 :             : 
     774                 :        2053 :   fprintf (file, "%sIV struct:\n", p);
     775                 :        2053 :   if (iv->ssa_name && dump_name)
     776                 :             :     {
     777                 :         787 :       fprintf (file, "%s  SSA_NAME:\t", p);
     778                 :         787 :       print_generic_expr (file, iv->ssa_name, TDF_SLIM);
     779                 :         787 :       fprintf (file, "\n");
     780                 :             :     }
     781                 :             : 
     782                 :        2053 :   fprintf (file, "%s  Type:\t", p);
     783                 :        2053 :   print_generic_expr (file, TREE_TYPE (iv->base), TDF_SLIM);
     784                 :        2053 :   fprintf (file, "\n");
     785                 :             : 
     786                 :        2053 :   fprintf (file, "%s  Base:\t", p);
     787                 :        2053 :   print_generic_expr (file, iv->base, TDF_SLIM);
     788                 :        2053 :   fprintf (file, "\n");
     789                 :             : 
     790                 :        2053 :   fprintf (file, "%s  Step:\t", p);
     791                 :        2053 :   print_generic_expr (file, iv->step, TDF_SLIM);
     792                 :        2053 :   fprintf (file, "\n");
     793                 :             : 
     794                 :        2053 :   if (iv->base_object)
     795                 :             :     {
     796                 :         578 :       fprintf (file, "%s  Object:\t", p);
     797                 :         578 :       print_generic_expr (file, iv->base_object, TDF_SLIM);
     798                 :         578 :       fprintf (file, "\n");
     799                 :             :     }
     800                 :             : 
     801                 :        3755 :   fprintf (file, "%s  Biv:\t%c\n", p, iv->biv_p ? 'Y' : 'N');
     802                 :             : 
     803                 :        2053 :   fprintf (file, "%s  Overflowness wrto loop niter:\t%s\n",
     804                 :        2053 :            p, iv->no_overflow ? "No-overflow" : "Overflow");
     805                 :        2053 : }
     806                 :             : 
     807                 :             : /* Dumps information about the USE to FILE.  */
     808                 :             : 
     809                 :             : void
     810                 :         301 : dump_use (FILE *file, struct iv_use *use)
     811                 :             : {
     812                 :         301 :   fprintf (file, "  Use %d.%d:\n", use->group_id, use->id);
     813                 :         301 :   fprintf (file, "    At stmt:\t");
     814                 :         301 :   print_gimple_stmt (file, use->stmt, 0);
     815                 :         301 :   fprintf (file, "    At pos:\t");
     816                 :         301 :   if (use->op_p)
     817                 :         176 :     print_generic_expr (file, *use->op_p, TDF_SLIM);
     818                 :         301 :   fprintf (file, "\n");
     819                 :         301 :   dump_iv (file, use->iv, false, 2);
     820                 :         301 : }
     821                 :             : 
     822                 :             : /* Dumps information about the uses to FILE.  */
     823                 :             : 
     824                 :             : void
     825                 :          76 : dump_groups (FILE *file, struct ivopts_data *data)
     826                 :             : {
     827                 :          76 :   unsigned i, j;
     828                 :          76 :   struct iv_group *group;
     829                 :             : 
     830                 :         704 :   for (i = 0; i < data->vgroups.length (); i++)
     831                 :             :     {
     832                 :         276 :       group = data->vgroups[i];
     833                 :         276 :       fprintf (file, "Group %d:\n", group->id);
     834                 :         276 :       if (group->type == USE_NONLINEAR_EXPR)
     835                 :         125 :         fprintf (file, "  Type:\tGENERIC\n");
     836                 :         151 :       else if (group->type == USE_REF_ADDRESS)
     837                 :          67 :         fprintf (file, "  Type:\tREFERENCE ADDRESS\n");
     838                 :          84 :       else if (group->type == USE_PTR_ADDRESS)
     839                 :           0 :         fprintf (file, "  Type:\tPOINTER ARGUMENT ADDRESS\n");
     840                 :             :       else
     841                 :             :         {
     842                 :          84 :           gcc_assert (group->type == USE_COMPARE);
     843                 :          84 :           fprintf (file, "  Type:\tCOMPARE\n");
     844                 :             :         }
     845                 :        1154 :       for (j = 0; j < group->vuses.length (); j++)
     846                 :         301 :         dump_use (file, group->vuses[j]);
     847                 :             :     }
     848                 :          76 : }
     849                 :             : 
     850                 :             : /* Dumps information about induction variable candidate CAND to FILE.  */
     851                 :             : 
     852                 :             : void
     853                 :         965 : dump_cand (FILE *file, struct iv_cand *cand)
     854                 :             : {
     855                 :         965 :   struct iv *iv = cand->iv;
     856                 :             : 
     857                 :         965 :   fprintf (file, "Candidate %d:\n", cand->id);
     858                 :         965 :   if (cand->inv_vars)
     859                 :             :     {
     860                 :          31 :       fprintf (file, "  Depend on inv.vars: ");
     861                 :          31 :       dump_bitmap (file, cand->inv_vars);
     862                 :             :     }
     863                 :         965 :   if (cand->inv_exprs)
     864                 :             :     {
     865                 :           0 :       fprintf (file, "  Depend on inv.exprs: ");
     866                 :           0 :       dump_bitmap (file, cand->inv_exprs);
     867                 :             :     }
     868                 :             : 
     869                 :         965 :   if (cand->var_before)
     870                 :             :     {
     871                 :         840 :       fprintf (file, "  Var befor: ");
     872                 :         840 :       print_generic_expr (file, cand->var_before, TDF_SLIM);
     873                 :         840 :       fprintf (file, "\n");
     874                 :             :     }
     875                 :         965 :   if (cand->var_after)
     876                 :             :     {
     877                 :         840 :       fprintf (file, "  Var after: ");
     878                 :         840 :       print_generic_expr (file, cand->var_after, TDF_SLIM);
     879                 :         840 :       fprintf (file, "\n");
     880                 :             :     }
     881                 :             : 
     882                 :         965 :   switch (cand->pos)
     883                 :             :     {
     884                 :         805 :     case IP_NORMAL:
     885                 :         805 :       fprintf (file, "  Incr POS: before exit test\n");
     886                 :         805 :       break;
     887                 :             : 
     888                 :           0 :     case IP_BEFORE_USE:
     889                 :           0 :       fprintf (file, "  Incr POS: before use %d\n", cand->ainc_use->id);
     890                 :           0 :       break;
     891                 :             : 
     892                 :           0 :     case IP_AFTER_USE:
     893                 :           0 :       fprintf (file, "  Incr POS: after use %d\n", cand->ainc_use->id);
     894                 :           0 :       break;
     895                 :             : 
     896                 :           0 :     case IP_END:
     897                 :           0 :       fprintf (file, "  Incr POS: at end\n");
     898                 :           0 :       break;
     899                 :             : 
     900                 :         160 :     case IP_ORIGINAL:
     901                 :         160 :       fprintf (file, "  Incr POS: orig biv\n");
     902                 :         160 :       break;
     903                 :             :     }
     904                 :             : 
     905                 :         965 :   dump_iv (file, iv, false, 1);
     906                 :         965 : }
     907                 :             : 
     908                 :             : /* Returns the info for ssa version VER.  */
     909                 :             : 
     910                 :             : static inline struct version_info *
     911                 :   102597383 : ver_info (struct ivopts_data *data, unsigned ver)
     912                 :             : {
     913                 :   102597383 :   return data->version_info + ver;
     914                 :             : }
     915                 :             : 
     916                 :             : /* Returns the info for ssa name NAME.  */
     917                 :             : 
     918                 :             : static inline struct version_info *
     919                 :    83328653 : name_info (struct ivopts_data *data, tree name)
     920                 :             : {
     921                 :    83328653 :   return ver_info (data, SSA_NAME_VERSION (name));
     922                 :             : }
     923                 :             : 
     924                 :             : /* Returns true if STMT is after the place where the IP_NORMAL ivs will be
     925                 :             :    emitted in LOOP.  */
     926                 :             : 
     927                 :             : static bool
     928                 :    29619633 : stmt_after_ip_normal_pos (class loop *loop, gimple *stmt)
     929                 :             : {
     930                 :    29619633 :   basic_block bb = ip_normal_pos (loop), sbb = gimple_bb (stmt);
     931                 :             : 
     932                 :    29619633 :   gcc_assert (bb);
     933                 :             : 
     934                 :    29619633 :   if (sbb == loop->latch)
     935                 :             :     return true;
     936                 :             : 
     937                 :    29517683 :   if (sbb != bb)
     938                 :             :     return false;
     939                 :             : 
     940                 :    16842087 :   return stmt == last_nondebug_stmt (bb);
     941                 :             : }
     942                 :             : 
     943                 :             : /* Returns true if STMT if after the place where the original induction
     944                 :             :    variable CAND is incremented.  If TRUE_IF_EQUAL is set, we return true
     945                 :             :    if the positions are identical.  */
     946                 :             : 
     947                 :             : static bool
     948                 :     6732214 : stmt_after_inc_pos (struct iv_cand *cand, gimple *stmt, bool true_if_equal)
     949                 :             : {
     950                 :     6732214 :   basic_block cand_bb = gimple_bb (cand->incremented_at);
     951                 :     6732214 :   basic_block stmt_bb = gimple_bb (stmt);
     952                 :             : 
     953                 :     6732214 :   if (!dominated_by_p (CDI_DOMINATORS, stmt_bb, cand_bb))
     954                 :             :     return false;
     955                 :             : 
     956                 :     4538871 :   if (stmt_bb != cand_bb)
     957                 :             :     return true;
     958                 :             : 
     959                 :     4326093 :   if (true_if_equal
     960                 :     4326093 :       && gimple_uid (stmt) == gimple_uid (cand->incremented_at))
     961                 :             :     return true;
     962                 :     4320408 :   return gimple_uid (stmt) > gimple_uid (cand->incremented_at);
     963                 :             : }
     964                 :             : 
     965                 :             : /* Returns true if STMT if after the place where the induction variable
     966                 :             :    CAND is incremented in LOOP.  */
     967                 :             : 
     968                 :             : static bool
     969                 :    37349974 : stmt_after_increment (class loop *loop, struct iv_cand *cand, gimple *stmt)
     970                 :             : {
     971                 :    37349974 :   switch (cand->pos)
     972                 :             :     {
     973                 :             :     case IP_END:
     974                 :             :       return false;
     975                 :             : 
     976                 :    29619633 :     case IP_NORMAL:
     977                 :    29619633 :       return stmt_after_ip_normal_pos (loop, stmt);
     978                 :             : 
     979                 :     6723804 :     case IP_ORIGINAL:
     980                 :     6723804 :     case IP_AFTER_USE:
     981                 :     6723804 :       return stmt_after_inc_pos (cand, stmt, false);
     982                 :             : 
     983                 :        8410 :     case IP_BEFORE_USE:
     984                 :        8410 :       return stmt_after_inc_pos (cand, stmt, true);
     985                 :             : 
     986                 :           0 :     default:
     987                 :           0 :       gcc_unreachable ();
     988                 :             :     }
     989                 :             : }
     990                 :             : 
     991                 :             : /* walk_tree callback for contains_abnormal_ssa_name_p.  */
     992                 :             : 
     993                 :             : static tree
     994                 :    13264578 : contains_abnormal_ssa_name_p_1 (tree *tp, int *walk_subtrees, void *)
     995                 :             : {
     996                 :    13264578 :   if (TREE_CODE (*tp) == SSA_NAME
     997                 :    13264578 :       && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (*tp))
     998                 :             :     return *tp;
     999                 :             : 
    1000                 :    13264561 :   if (!EXPR_P (*tp))
    1001                 :     8938564 :     *walk_subtrees = 0;
    1002                 :             : 
    1003                 :             :   return NULL_TREE;
    1004                 :             : }
    1005                 :             : 
    1006                 :             : /* Returns true if EXPR contains a ssa name that occurs in an
    1007                 :             :    abnormal phi node.  */
    1008                 :             : 
    1009                 :             : bool
    1010                 :     6897348 : contains_abnormal_ssa_name_p (tree expr)
    1011                 :             : {
    1012                 :     6897348 :   return walk_tree_without_duplicates
    1013                 :     6897348 :            (&expr, contains_abnormal_ssa_name_p_1, NULL) != NULL_TREE;
    1014                 :             : }
    1015                 :             : 
    1016                 :             : /*  Returns the structure describing number of iterations determined from
    1017                 :             :     EXIT of DATA->current_loop, or NULL if something goes wrong.  */
    1018                 :             : 
    1019                 :             : static class tree_niter_desc *
    1020                 :     3729453 : niter_for_exit (struct ivopts_data *data, edge exit)
    1021                 :             : {
    1022                 :     3729453 :   class tree_niter_desc *desc;
    1023                 :     3729453 :   tree_niter_desc **slot;
    1024                 :             : 
    1025                 :     3729453 :   if (!data->niters)
    1026                 :             :     {
    1027                 :      401323 :       data->niters = new hash_map<edge, tree_niter_desc *>;
    1028                 :      401323 :       slot = NULL;
    1029                 :             :     }
    1030                 :             :   else
    1031                 :     3328130 :     slot = data->niters->get (exit);
    1032                 :             : 
    1033                 :     3729453 :   if (!slot)
    1034                 :             :     {
    1035                 :             :       /* Try to determine number of iterations.  We cannot safely work with ssa
    1036                 :             :          names that appear in phi nodes on abnormal edges, so that we do not
    1037                 :             :          create overlapping life ranges for them (PR 27283).  */
    1038                 :      410715 :       desc = XNEW (class tree_niter_desc);
    1039                 :      410715 :       ::new (static_cast<void*> (desc)) tree_niter_desc ();
    1040                 :      410715 :       if (!number_of_iterations_exit (data->current_loop,
    1041                 :             :                                       exit, desc, true)
    1042                 :      410715 :           || contains_abnormal_ssa_name_p (desc->niter))
    1043                 :             :         {
    1044                 :       32046 :           desc->~tree_niter_desc ();
    1045                 :       32046 :           XDELETE (desc);
    1046                 :       32046 :           desc = NULL;
    1047                 :             :         }
    1048                 :      410715 :       data->niters->put (exit, desc);
    1049                 :             :     }
    1050                 :             :   else
    1051                 :     3318738 :     desc = *slot;
    1052                 :             : 
    1053                 :     3729453 :   return desc;
    1054                 :             : }
    1055                 :             : 
    1056                 :             : /* Returns the structure describing number of iterations determined from
    1057                 :             :    single dominating exit of DATA->current_loop, or NULL if something
    1058                 :             :    goes wrong.  */
    1059                 :             : 
    1060                 :             : static class tree_niter_desc *
    1061                 :          76 : niter_for_single_dom_exit (struct ivopts_data *data)
    1062                 :             : {
    1063                 :          76 :   edge exit = single_dom_exit (data->current_loop);
    1064                 :             : 
    1065                 :          76 :   if (!exit)
    1066                 :             :     return NULL;
    1067                 :             : 
    1068                 :          66 :   return niter_for_exit (data, exit);
    1069                 :             : }
    1070                 :             : 
    1071                 :             : /* Initializes data structures used by the iv optimization pass, stored
    1072                 :             :    in DATA.  */
    1073                 :             : 
    1074                 :             : static void
    1075                 :      219508 : tree_ssa_iv_optimize_init (struct ivopts_data *data)
    1076                 :             : {
    1077                 :      219508 :   data->version_info_size = 2 * num_ssa_names;
    1078                 :      219508 :   data->version_info = XCNEWVEC (struct version_info, data->version_info_size);
    1079                 :      219508 :   data->relevant = BITMAP_ALLOC (NULL);
    1080                 :      219508 :   data->important_candidates = BITMAP_ALLOC (NULL);
    1081                 :      219508 :   data->max_inv_var_id = 0;
    1082                 :      219508 :   data->max_inv_expr_id = 0;
    1083                 :      219508 :   data->niters = NULL;
    1084                 :      219508 :   data->vgroups.create (20);
    1085                 :      219508 :   data->vcands.create (20);
    1086                 :      219508 :   data->inv_expr_tab = new hash_table<iv_inv_expr_hasher> (10);
    1087                 :      219508 :   data->name_expansion_cache = NULL;
    1088                 :      219508 :   data->base_object_map = NULL;
    1089                 :      219508 :   data->iv_common_cand_tab = new hash_table<iv_common_cand_hasher> (10);
    1090                 :      219508 :   data->iv_common_cands.create (20);
    1091                 :      219508 :   decl_rtl_to_reset.create (20);
    1092                 :      219508 :   gcc_obstack_init (&data->iv_obstack);
    1093                 :      219508 : }
    1094                 :             : 
    1095                 :             : /* walk_tree callback for determine_base_object.  */
    1096                 :             : 
    1097                 :             : static tree
    1098                 :    14902857 : determine_base_object_1 (tree *tp, int *walk_subtrees, void *wdata)
    1099                 :             : {
    1100                 :    14902857 :   tree_code code = TREE_CODE (*tp);
    1101                 :    14902857 :   tree obj = NULL_TREE;
    1102                 :    14902857 :   if (code == ADDR_EXPR)
    1103                 :             :     {
    1104                 :      779126 :       tree base = get_base_address (TREE_OPERAND (*tp, 0));
    1105                 :      779126 :       if (!base)
    1106                 :           0 :         obj = *tp;
    1107                 :      779126 :       else if (TREE_CODE (base) != MEM_REF)
    1108                 :      779126 :         obj = fold_convert (ptr_type_node, build_fold_addr_expr (base));
    1109                 :             :     }
    1110                 :    14123731 :   else if (code == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (*tp)))
    1111                 :     1451700 :         obj = fold_convert (ptr_type_node, *tp);
    1112                 :             : 
    1113                 :     2230826 :   if (!obj)
    1114                 :             :     {
    1115                 :    12672031 :       if (!EXPR_P (*tp))
    1116                 :     6083987 :         *walk_subtrees = 0;
    1117                 :             : 
    1118                 :    12672031 :       return NULL_TREE;
    1119                 :             :     }
    1120                 :             :   /* Record special node for multiple base objects and stop.  */
    1121                 :     2230826 :   if (*static_cast<tree *> (wdata))
    1122                 :             :     {
    1123                 :        1044 :       *static_cast<tree *> (wdata) = integer_zero_node;
    1124                 :        1044 :       return integer_zero_node;
    1125                 :             :     }
    1126                 :             :   /* Record the base object and continue looking.  */
    1127                 :     2229782 :   *static_cast<tree *> (wdata) = obj;
    1128                 :     2229782 :   return NULL_TREE;
    1129                 :             : }
    1130                 :             : 
    1131                 :             : /* Returns a memory object to that EXPR points with caching.  Return NULL if we
    1132                 :             :    are able to determine that it does not point to any such object; specially
    1133                 :             :    return integer_zero_node if EXPR contains multiple base objects.  */
    1134                 :             : 
    1135                 :             : static tree
    1136                 :     8982374 : determine_base_object (struct ivopts_data *data, tree expr)
    1137                 :             : {
    1138                 :     8982374 :   tree *slot, obj = NULL_TREE;
    1139                 :     8982374 :   if (data->base_object_map)
    1140                 :             :     {
    1141                 :     8834326 :       if ((slot = data->base_object_map->get(expr)) != NULL)
    1142                 :     4343234 :         return *slot;
    1143                 :             :     }
    1144                 :             :   else
    1145                 :      148048 :     data->base_object_map = new hash_map<tree, tree>;
    1146                 :             : 
    1147                 :     4639140 :   (void) walk_tree_without_duplicates (&expr, determine_base_object_1, &obj);
    1148                 :     4639140 :   data->base_object_map->put (expr, obj);
    1149                 :     4639140 :   return obj;
    1150                 :             : }
    1151                 :             : 
    1152                 :             : /* Return true if address expression with non-DECL_P operand appears
    1153                 :             :    in EXPR.  */
    1154                 :             : 
    1155                 :             : static bool
    1156                 :    11510621 : contain_complex_addr_expr (tree expr)
    1157                 :             : {
    1158                 :    11510621 :   bool res = false;
    1159                 :             : 
    1160                 :    11510621 :   STRIP_NOPS (expr);
    1161                 :    11510621 :   switch (TREE_CODE (expr))
    1162                 :             :     {
    1163                 :     1485943 :     case POINTER_PLUS_EXPR:
    1164                 :     1485943 :     case PLUS_EXPR:
    1165                 :     1485943 :     case MINUS_EXPR:
    1166                 :     1485943 :       res |= contain_complex_addr_expr (TREE_OPERAND (expr, 0));
    1167                 :     1485943 :       res |= contain_complex_addr_expr (TREE_OPERAND (expr, 1));
    1168                 :     1485943 :       break;
    1169                 :             : 
    1170                 :      686953 :     case ADDR_EXPR:
    1171                 :      686953 :       return (!DECL_P (TREE_OPERAND (expr, 0)));
    1172                 :             : 
    1173                 :             :     default:
    1174                 :             :       return false;
    1175                 :             :     }
    1176                 :             : 
    1177                 :     1485943 :   return res;
    1178                 :             : }
    1179                 :             : 
    1180                 :             : /* Allocates an induction variable with given initial value BASE and step STEP
    1181                 :             :    for loop LOOP.  NO_OVERFLOW implies the iv doesn't overflow.  */
    1182                 :             : 
    1183                 :             : static struct iv *
    1184                 :     8982374 : alloc_iv (struct ivopts_data *data, tree base, tree step,
    1185                 :             :           bool no_overflow = false)
    1186                 :             : {
    1187                 :     8982374 :   tree expr = base;
    1188                 :     8982374 :   struct iv *iv = (struct iv*) obstack_alloc (&data->iv_obstack,
    1189                 :             :                                               sizeof (struct iv));
    1190                 :     8982374 :   gcc_assert (step != NULL_TREE);
    1191                 :             : 
    1192                 :             :   /* Lower address expression in base except ones with DECL_P as operand.
    1193                 :             :      By doing this:
    1194                 :             :        1) More accurate cost can be computed for address expressions;
    1195                 :             :        2) Duplicate candidates won't be created for bases in different
    1196                 :             :           forms, like &a[0] and &a.  */
    1197                 :     8982374 :   STRIP_NOPS (expr);
    1198                 :      892708 :   if ((TREE_CODE (expr) == ADDR_EXPR && !DECL_P (TREE_OPERAND (expr, 0)))
    1199                 :     9431443 :       || contain_complex_addr_expr (expr))
    1200                 :             :     {
    1201                 :      457589 :       aff_tree comb;
    1202                 :      457589 :       tree_to_aff_combination (expr, TREE_TYPE (expr), &comb);
    1203                 :      457589 :       base = fold_convert (TREE_TYPE (base), aff_combination_to_tree (&comb));
    1204                 :      457589 :     }
    1205                 :             : 
    1206                 :     8982374 :   iv->base = base;
    1207                 :     8982374 :   iv->base_object = determine_base_object (data, base);
    1208                 :     8982374 :   iv->step = step;
    1209                 :     8982374 :   iv->biv_p = false;
    1210                 :     8982374 :   iv->nonlin_use = NULL;
    1211                 :     8982374 :   iv->ssa_name = NULL_TREE;
    1212                 :     8982374 :   if (!no_overflow
    1213                 :     8982374 :        && !iv_can_overflow_p (data->current_loop, TREE_TYPE (base),
    1214                 :             :                               base, step))
    1215                 :             :     no_overflow = true;
    1216                 :     8982374 :   iv->no_overflow = no_overflow;
    1217                 :     8982374 :   iv->have_address_use = false;
    1218                 :             : 
    1219                 :     8982374 :   return iv;
    1220                 :             : }
    1221                 :             : 
    1222                 :             : /* Sets STEP and BASE for induction variable IV.  NO_OVERFLOW implies the IV
    1223                 :             :    doesn't overflow.  */
    1224                 :             : 
    1225                 :             : static void
    1226                 :     4237080 : set_iv (struct ivopts_data *data, tree iv, tree base, tree step,
    1227                 :             :         bool no_overflow)
    1228                 :             : {
    1229                 :     4237080 :   struct version_info *info = name_info (data, iv);
    1230                 :             : 
    1231                 :     4237080 :   gcc_assert (!info->iv);
    1232                 :             : 
    1233                 :     4237080 :   bitmap_set_bit (data->relevant, SSA_NAME_VERSION (iv));
    1234                 :     4237080 :   info->iv = alloc_iv (data, base, step, no_overflow);
    1235                 :     4237080 :   info->iv->ssa_name = iv;
    1236                 :     4237080 : }
    1237                 :             : 
    1238                 :             : /* Finds induction variable declaration for VAR.  */
    1239                 :             : 
    1240                 :             : static struct iv *
    1241                 :    39675082 : get_iv (struct ivopts_data *data, tree var)
    1242                 :             : {
    1243                 :    39675082 :   basic_block bb;
    1244                 :    39675082 :   tree type = TREE_TYPE (var);
    1245                 :             : 
    1246                 :    39675082 :   if (!POINTER_TYPE_P (type)
    1247                 :    31604215 :       && !INTEGRAL_TYPE_P (type))
    1248                 :             :     return NULL;
    1249                 :             : 
    1250                 :    34262374 :   if (!name_info (data, var)->iv)
    1251                 :             :     {
    1252                 :    16293925 :       bb = gimple_bb (SSA_NAME_DEF_STMT (var));
    1253                 :             : 
    1254                 :    16293925 :       if (!bb
    1255                 :    16293925 :           || !flow_bb_inside_loop_p (data->current_loop, bb))
    1256                 :             :         {
    1257                 :      666364 :           if (POINTER_TYPE_P (type))
    1258                 :      252652 :             type = sizetype;
    1259                 :      666364 :           set_iv (data, var, var, build_int_cst (type, 0), true);
    1260                 :             :         }
    1261                 :             :     }
    1262                 :             : 
    1263                 :    34262374 :   return name_info (data, var)->iv;
    1264                 :             : }
    1265                 :             : 
    1266                 :             : /* Return the first non-invariant ssa var found in EXPR.  */
    1267                 :             : 
    1268                 :             : static tree
    1269                 :     3608212 : extract_single_var_from_expr (tree expr)
    1270                 :             : {
    1271                 :     3608212 :   int i, n;
    1272                 :     3608212 :   tree tmp;
    1273                 :     3608212 :   enum tree_code code;
    1274                 :             : 
    1275                 :     3608212 :   if (!expr || is_gimple_min_invariant (expr))
    1276                 :     2950253 :     return NULL;
    1277                 :             : 
    1278                 :      657959 :   code = TREE_CODE (expr);
    1279                 :      657959 :   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
    1280                 :             :     {
    1281                 :      364299 :       n = TREE_OPERAND_LENGTH (expr);
    1282                 :      728618 :       for (i = 0; i < n; i++)
    1283                 :             :         {
    1284                 :      364319 :           tmp = extract_single_var_from_expr (TREE_OPERAND (expr, i));
    1285                 :             : 
    1286                 :      364319 :           if (tmp)
    1287                 :      364299 :             return tmp;
    1288                 :             :         }
    1289                 :             :     }
    1290                 :      293660 :   return (TREE_CODE (expr) == SSA_NAME) ? expr : NULL;
    1291                 :             : }
    1292                 :             : 
    1293                 :             : /* Finds basic ivs.  */
    1294                 :             : 
    1295                 :             : static bool
    1296                 :      543029 : find_bivs (struct ivopts_data *data)
    1297                 :             : {
    1298                 :      543029 :   gphi *phi;
    1299                 :      543029 :   affine_iv iv;
    1300                 :      543029 :   tree step, type, base, stop;
    1301                 :      543029 :   bool found = false;
    1302                 :      543029 :   class loop *loop = data->current_loop;
    1303                 :      543029 :   gphi_iterator psi;
    1304                 :             : 
    1305                 :     2017880 :   for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
    1306                 :             :     {
    1307                 :     1474851 :       phi = psi.phi ();
    1308                 :             : 
    1309                 :     1474851 :       if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
    1310                 :         211 :         continue;
    1311                 :             : 
    1312                 :     1474640 :       if (virtual_operand_p (PHI_RESULT (phi)))
    1313                 :      363919 :         continue;
    1314                 :             : 
    1315                 :     1110721 :       if (!simple_iv (loop, loop, PHI_RESULT (phi), &iv, true))
    1316                 :      375270 :         continue;
    1317                 :             : 
    1318                 :      735451 :       if (integer_zerop (iv.step))
    1319                 :           0 :         continue;
    1320                 :             : 
    1321                 :      735451 :       step = iv.step;
    1322                 :      735451 :       base = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
    1323                 :             :       /* Stop expanding iv base at the first ssa var referred by iv step.
    1324                 :             :          Ideally we should stop at any ssa var, because that's expensive
    1325                 :             :          and unusual to happen, we just do it on the first one.
    1326                 :             : 
    1327                 :             :          See PR64705 for the rationale.  */
    1328                 :      735451 :       stop = extract_single_var_from_expr (step);
    1329                 :      735451 :       base = expand_simple_operations (base, stop);
    1330                 :      735451 :       if (contains_abnormal_ssa_name_p (base)
    1331                 :      735451 :           || contains_abnormal_ssa_name_p (step))
    1332                 :          10 :         continue;
    1333                 :             : 
    1334                 :      735441 :       type = TREE_TYPE (PHI_RESULT (phi));
    1335                 :      735441 :       base = fold_convert (type, base);
    1336                 :      735441 :       if (step)
    1337                 :             :         {
    1338                 :      735441 :           if (POINTER_TYPE_P (type))
    1339                 :      124492 :             step = convert_to_ptrofftype (step);
    1340                 :             :           else
    1341                 :      610949 :             step = fold_convert (type, step);
    1342                 :             :         }
    1343                 :             : 
    1344                 :      735441 :       set_iv (data, PHI_RESULT (phi), base, step, iv.no_overflow);
    1345                 :      735441 :       found = true;
    1346                 :             :     }
    1347                 :             : 
    1348                 :      543029 :   return found;
    1349                 :             : }
    1350                 :             : 
    1351                 :             : /* Marks basic ivs.  */
    1352                 :             : 
    1353                 :             : static void
    1354                 :      427981 : mark_bivs (struct ivopts_data *data)
    1355                 :             : {
    1356                 :      427981 :   gphi *phi;
    1357                 :      427981 :   gimple *def;
    1358                 :      427981 :   tree var;
    1359                 :      427981 :   struct iv *iv, *incr_iv;
    1360                 :      427981 :   class loop *loop = data->current_loop;
    1361                 :      427981 :   basic_block incr_bb;
    1362                 :      427981 :   gphi_iterator psi;
    1363                 :             : 
    1364                 :      427981 :   data->bivs_not_used_in_addr = 0;
    1365                 :     1666306 :   for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
    1366                 :             :     {
    1367                 :     1238325 :       phi = psi.phi ();
    1368                 :             : 
    1369                 :     1238325 :       iv = get_iv (data, PHI_RESULT (phi));
    1370                 :     1238325 :       if (!iv)
    1371                 :      502884 :         continue;
    1372                 :             : 
    1373                 :      735441 :       var = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
    1374                 :      735441 :       def = SSA_NAME_DEF_STMT (var);
    1375                 :             :       /* Don't mark iv peeled from other one as biv.  */
    1376                 :      736020 :       if (def
    1377                 :      735441 :           && gimple_code (def) == GIMPLE_PHI
    1378                 :      737109 :           && gimple_bb (def) == loop->header)
    1379                 :         579 :         continue;
    1380                 :             : 
    1381                 :      734862 :       incr_iv = get_iv (data, var);
    1382                 :      734862 :       if (!incr_iv)
    1383                 :        1101 :         continue;
    1384                 :             : 
    1385                 :             :       /* If the increment is in the subloop, ignore it.  */
    1386                 :      733761 :       incr_bb = gimple_bb (SSA_NAME_DEF_STMT (var));
    1387                 :      733761 :       if (incr_bb->loop_father != data->current_loop
    1388                 :      733761 :           || (incr_bb->flags & BB_IRREDUCIBLE_LOOP))
    1389                 :           0 :         continue;
    1390                 :             : 
    1391                 :      733761 :       iv->biv_p = true;
    1392                 :      733761 :       incr_iv->biv_p = true;
    1393                 :      733761 :       if (iv->no_overflow)
    1394                 :      485620 :         data->bivs_not_used_in_addr++;
    1395                 :      733761 :       if (incr_iv->no_overflow)
    1396                 :      477658 :         data->bivs_not_used_in_addr++;
    1397                 :             :     }
    1398                 :      427981 : }
    1399                 :             : 
    1400                 :             : /* Checks whether STMT defines a linear induction variable and stores its
    1401                 :             :    parameters to IV.  */
    1402                 :             : 
    1403                 :             : static bool
    1404                 :    19086230 : find_givs_in_stmt_scev (struct ivopts_data *data, gimple *stmt, affine_iv *iv)
    1405                 :             : {
    1406                 :    19086230 :   tree lhs, stop;
    1407                 :    19086230 :   class loop *loop = data->current_loop;
    1408                 :             : 
    1409                 :    19086230 :   iv->base = NULL_TREE;
    1410                 :    19086230 :   iv->step = NULL_TREE;
    1411                 :             : 
    1412                 :    19086230 :   if (gimple_code (stmt) != GIMPLE_ASSIGN)
    1413                 :             :     return false;
    1414                 :             : 
    1415                 :     9788961 :   lhs = gimple_assign_lhs (stmt);
    1416                 :     9788961 :   if (TREE_CODE (lhs) != SSA_NAME)
    1417                 :             :     return false;
    1418                 :             : 
    1419                 :    17373466 :   if (!simple_iv (loop, loop_containing_stmt (stmt), lhs, iv, true))
    1420                 :             :     return false;
    1421                 :             : 
    1422                 :             :   /* Stop expanding iv base at the first ssa var referred by iv step.
    1423                 :             :      Ideally we should stop at any ssa var, because that's expensive
    1424                 :             :      and unusual to happen, we just do it on the first one.
    1425                 :             : 
    1426                 :             :      See PR64705 for the rationale.  */
    1427                 :     2508442 :   stop = extract_single_var_from_expr (iv->step);
    1428                 :     2508442 :   iv->base = expand_simple_operations (iv->base, stop);
    1429                 :     2508442 :   if (contains_abnormal_ssa_name_p (iv->base)
    1430                 :     2508442 :       || contains_abnormal_ssa_name_p (iv->step))
    1431                 :           6 :     return false;
    1432                 :             : 
    1433                 :             :   /* If STMT could throw, then do not consider STMT as defining a GIV.
    1434                 :             :      While this will suppress optimizations, we cannot safely delete this
    1435                 :             :      GIV and associated statements, even if it appears it is not used.  */
    1436                 :     2508436 :   if (stmt_could_throw_p (cfun, stmt))
    1437                 :             :     return false;
    1438                 :             : 
    1439                 :             :   return true;
    1440                 :             : }
    1441                 :             : 
    1442                 :             : /* Finds general ivs in statement STMT.  */
    1443                 :             : 
    1444                 :             : static void
    1445                 :    19086230 : find_givs_in_stmt (struct ivopts_data *data, gimple *stmt)
    1446                 :             : {
    1447                 :    19086230 :   affine_iv iv;
    1448                 :             : 
    1449                 :    19086230 :   if (!find_givs_in_stmt_scev (data, stmt, &iv))
    1450                 :    16577805 :     return;
    1451                 :             : 
    1452                 :     2508425 :   set_iv (data, gimple_assign_lhs (stmt), iv.base, iv.step, iv.no_overflow);
    1453                 :             : }
    1454                 :             : 
    1455                 :             : /* Finds general ivs in basic block BB.  */
    1456                 :             : 
    1457                 :             : static void
    1458                 :     2509877 : find_givs_in_bb (struct ivopts_data *data, basic_block bb)
    1459                 :             : {
    1460                 :     2509877 :   gimple_stmt_iterator bsi;
    1461                 :             : 
    1462                 :    24105984 :   for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
    1463                 :    19086230 :     find_givs_in_stmt (data, gsi_stmt (bsi));
    1464                 :     2509877 : }
    1465                 :             : 
    1466                 :             : /* Finds general ivs.  */
    1467                 :             : 
    1468                 :             : static void
    1469                 :      427981 : find_givs (struct ivopts_data *data, basic_block *body)
    1470                 :             : {
    1471                 :      427981 :   class loop *loop = data->current_loop;
    1472                 :      427981 :   unsigned i;
    1473                 :             : 
    1474                 :     2937858 :   for (i = 0; i < loop->num_nodes; i++)
    1475                 :     2509877 :     find_givs_in_bb (data, body[i]);
    1476                 :      427981 : }
    1477                 :             : 
    1478                 :             : /* For each ssa name defined in LOOP determines whether it is an induction
    1479                 :             :    variable and if so, its initial value and step.  */
    1480                 :             : 
    1481                 :             : static bool
    1482                 :      543029 : find_induction_variables (struct ivopts_data *data, basic_block *body)
    1483                 :             : {
    1484                 :      543029 :   unsigned i;
    1485                 :      543029 :   bitmap_iterator bi;
    1486                 :             : 
    1487                 :      543029 :   if (!find_bivs (data))
    1488                 :             :     return false;
    1489                 :             : 
    1490                 :      427981 :   find_givs (data, body);
    1491                 :      427981 :   mark_bivs (data);
    1492                 :             : 
    1493                 :      427981 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1494                 :             :     {
    1495                 :          76 :       class tree_niter_desc *niter = niter_for_single_dom_exit (data);
    1496                 :             : 
    1497                 :          76 :       if (niter)
    1498                 :             :         {
    1499                 :          59 :           fprintf (dump_file, "  number of iterations ");
    1500                 :          59 :           print_generic_expr (dump_file, niter->niter, TDF_SLIM);
    1501                 :          59 :           if (!integer_zerop (niter->may_be_zero))
    1502                 :             :             {
    1503                 :           0 :               fprintf (dump_file, "; zero if ");
    1504                 :           0 :               print_generic_expr (dump_file, niter->may_be_zero, TDF_SLIM);
    1505                 :             :             }
    1506                 :          59 :           fprintf (dump_file, "\n");
    1507                 :          76 :         };
    1508                 :             : 
    1509                 :          76 :       fprintf (dump_file, "\n<Induction Vars>:\n");
    1510                 :        1144 :       EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
    1511                 :             :         {
    1512                 :        1068 :           struct version_info *info = ver_info (data, i);
    1513                 :        1068 :           if (info->iv && info->iv->step && !integer_zerop (info->iv->step))
    1514                 :         787 :             dump_iv (dump_file, ver_info (data, i)->iv, true, 0);
    1515                 :             :         }
    1516                 :             :     }
    1517                 :             : 
    1518                 :             :   return true;
    1519                 :             : }
    1520                 :             : 
    1521                 :             : /* Records a use of TYPE at *USE_P in STMT whose value is IV in GROUP.
    1522                 :             :    For address type use, ADDR_BASE is the stripped IV base, ADDR_OFFSET
    1523                 :             :    is the const offset stripped from IV base and MEM_TYPE is the type
    1524                 :             :    of the memory being addressed.  For uses of other types, ADDR_BASE
    1525                 :             :    and ADDR_OFFSET are zero by default and MEM_TYPE is NULL_TREE.  */
    1526                 :             : 
    1527                 :             : static struct iv_use *
    1528                 :     1791401 : record_use (struct iv_group *group, tree *use_p, struct iv *iv,
    1529                 :             :             gimple *stmt, enum use_type type, tree mem_type,
    1530                 :             :             tree addr_base, poly_uint64 addr_offset)
    1531                 :             : {
    1532                 :     1791401 :   struct iv_use *use = XCNEW (struct iv_use);
    1533                 :             : 
    1534                 :     1791401 :   use->id = group->vuses.length ();
    1535                 :     1791401 :   use->group_id = group->id;
    1536                 :     1791401 :   use->type = type;
    1537                 :     1791401 :   use->mem_type = mem_type;
    1538                 :     1791401 :   use->iv = iv;
    1539                 :     1791401 :   use->stmt = stmt;
    1540                 :     1791401 :   use->op_p = use_p;
    1541                 :     1791401 :   use->addr_base = addr_base;
    1542                 :     1791401 :   use->addr_offset = addr_offset;
    1543                 :             : 
    1544                 :     1791401 :   group->vuses.safe_push (use);
    1545                 :     1791401 :   return use;
    1546                 :             : }
    1547                 :             : 
    1548                 :             : /* Checks whether OP is a loop-level invariant and if so, records it.
    1549                 :             :    NONLINEAR_USE is true if the invariant is used in a way we do not
    1550                 :             :    handle specially.  */
    1551                 :             : 
    1552                 :             : static void
    1553                 :    20419087 : record_invariant (struct ivopts_data *data, tree op, bool nonlinear_use)
    1554                 :             : {
    1555                 :    20419087 :   basic_block bb;
    1556                 :    20419087 :   struct version_info *info;
    1557                 :             : 
    1558                 :    20419087 :   if (TREE_CODE (op) != SSA_NAME
    1559                 :    20419087 :       || virtual_operand_p (op))
    1560                 :             :     return;
    1561                 :             : 
    1562                 :    19406669 :   bb = gimple_bb (SSA_NAME_DEF_STMT (op));
    1563                 :    19406669 :   if (bb
    1564                 :    19406669 :       && flow_bb_inside_loop_p (data->current_loop, bb))
    1565                 :             :     return;
    1566                 :             : 
    1567                 :     3376161 :   info = name_info (data, op);
    1568                 :     3376161 :   info->name = op;
    1569                 :     3376161 :   info->has_nonlin_use |= nonlinear_use;
    1570                 :     3376161 :   if (!info->inv_id)
    1571                 :     1114469 :     info->inv_id = ++data->max_inv_var_id;
    1572                 :     3376161 :   bitmap_set_bit (data->relevant, SSA_NAME_VERSION (op));
    1573                 :             : }
    1574                 :             : 
    1575                 :             : /* Record a group of TYPE.  */
    1576                 :             : 
    1577                 :             : static struct iv_group *
    1578                 :     1529977 : record_group (struct ivopts_data *data, enum use_type type)
    1579                 :             : {
    1580                 :     1529977 :   struct iv_group *group = XCNEW (struct iv_group);
    1581                 :             : 
    1582                 :     1529977 :   group->id = data->vgroups.length ();
    1583                 :     1529977 :   group->type = type;
    1584                 :     1529977 :   group->related_cands = BITMAP_ALLOC (NULL);
    1585                 :     1529977 :   group->vuses.create (1);
    1586                 :     1529977 :   group->doloop_p = false;
    1587                 :             : 
    1588                 :     1529977 :   data->vgroups.safe_push (group);
    1589                 :     1529977 :   return group;
    1590                 :             : }
    1591                 :             : 
    1592                 :             : /* Record a use of TYPE at *USE_P in STMT whose value is IV in a group.
    1593                 :             :    New group will be created if there is no existing group for the use.
    1594                 :             :    MEM_TYPE is the type of memory being addressed, or NULL if this
    1595                 :             :    isn't an address reference.  */
    1596                 :             : 
    1597                 :             : static struct iv_use *
    1598                 :     1791401 : record_group_use (struct ivopts_data *data, tree *use_p,
    1599                 :             :                   struct iv *iv, gimple *stmt, enum use_type type,
    1600                 :             :                   tree mem_type)
    1601                 :             : {
    1602                 :     1791401 :   tree addr_base = NULL;
    1603                 :     1791401 :   struct iv_group *group = NULL;
    1604                 :     1791401 :   poly_uint64 addr_offset = 0;
    1605                 :             : 
    1606                 :             :   /* Record non address type use in a new group.  */
    1607                 :     1791401 :   if (address_p (type))
    1608                 :             :     {
    1609                 :      756304 :       unsigned int i;
    1610                 :             : 
    1611                 :      756304 :       gcc_assert (POINTER_TYPE_P (TREE_TYPE (iv->base)));
    1612                 :      756304 :       tree addr_toffset;
    1613                 :      756304 :       split_constant_offset (iv->base, &addr_base, &addr_toffset);
    1614                 :      756304 :       addr_offset = int_cst_value (addr_toffset);
    1615                 :     2818816 :       for (i = 0; i < data->vgroups.length (); i++)
    1616                 :             :         {
    1617                 :      958620 :           struct iv_use *use;
    1618                 :             : 
    1619                 :      958620 :           group = data->vgroups[i];
    1620                 :      958620 :           use = group->vuses[0];
    1621                 :      958620 :           if (!address_p (use->type))
    1622                 :      275670 :             continue;
    1623                 :             : 
    1624                 :             :           /* Check if it has the same stripped base and step.  */
    1625                 :      682950 :           if (operand_equal_p (iv->base_object, use->iv->base_object, 0)
    1626                 :      376851 :               && operand_equal_p (iv->step, use->iv->step, 0)
    1627                 :     1056757 :               && operand_equal_p (addr_base, use->addr_base, 0))
    1628                 :             :             break;
    1629                 :             :         }
    1630                 :     1512608 :       if (i == data->vgroups.length ())
    1631                 :      450788 :         group = NULL;
    1632                 :             :     }
    1633                 :             : 
    1634                 :      756304 :   if (!group)
    1635                 :     1485885 :     group = record_group (data, type);
    1636                 :             : 
    1637                 :     1791401 :   return record_use (group, use_p, iv, stmt, type, mem_type,
    1638                 :     1791401 :                      addr_base, addr_offset);
    1639                 :             : }
    1640                 :             : 
    1641                 :             : /* Checks whether the use OP is interesting and if so, records it.  */
    1642                 :             : 
    1643                 :             : static struct iv_use *
    1644                 :     6569717 : find_interesting_uses_op (struct ivopts_data *data, tree op)
    1645                 :             : {
    1646                 :     6569717 :   struct iv *iv;
    1647                 :     6569717 :   gimple *stmt;
    1648                 :     6569717 :   struct iv_use *use;
    1649                 :             : 
    1650                 :     6569717 :   if (TREE_CODE (op) != SSA_NAME)
    1651                 :             :     return NULL;
    1652                 :             : 
    1653                 :     5312688 :   iv = get_iv (data, op);
    1654                 :     5312688 :   if (!iv)
    1655                 :             :     return NULL;
    1656                 :             : 
    1657                 :     2278810 :   if (iv->nonlin_use)
    1658                 :             :     {
    1659                 :      180158 :       gcc_assert (iv->nonlin_use->type == USE_NONLINEAR_EXPR);
    1660                 :             :       return iv->nonlin_use;
    1661                 :             :     }
    1662                 :             : 
    1663                 :     2098652 :   if (integer_zerop (iv->step))
    1664                 :             :     {
    1665                 :     1575541 :       record_invariant (data, op, true);
    1666                 :     1575541 :       return NULL;
    1667                 :             :     }
    1668                 :             : 
    1669                 :      523111 :   stmt = SSA_NAME_DEF_STMT (op);
    1670                 :      523111 :   gcc_assert (gimple_code (stmt) == GIMPLE_PHI || is_gimple_assign (stmt));
    1671                 :             : 
    1672                 :      523111 :   use = record_group_use (data, NULL, iv, stmt, USE_NONLINEAR_EXPR, NULL_TREE);
    1673                 :      523111 :   iv->nonlin_use = use;
    1674                 :      523111 :   return use;
    1675                 :             : }
    1676                 :             : 
    1677                 :             : /* Indicate how compare type iv_use can be handled.  */
    1678                 :             : enum comp_iv_rewrite
    1679                 :             : {
    1680                 :             :   COMP_IV_NA,
    1681                 :             :   /* We may rewrite compare type iv_use by expressing value of the iv_use.  */
    1682                 :             :   COMP_IV_EXPR,
    1683                 :             :   /* We may rewrite compare type iv_uses on both sides of comparison by
    1684                 :             :      expressing value of each iv_use.  */
    1685                 :             :   COMP_IV_EXPR_2,
    1686                 :             :   /* We may rewrite compare type iv_use by expressing value of the iv_use
    1687                 :             :      or by eliminating it with other iv_cand.  */
    1688                 :             :   COMP_IV_ELIM
    1689                 :             : };
    1690                 :             : 
    1691                 :             : /* Given a condition in statement STMT, checks whether it is a compare
    1692                 :             :    of an induction variable and an invariant.  If this is the case,
    1693                 :             :    CONTROL_VAR is set to location of the iv, BOUND to the location of
    1694                 :             :    the invariant, IV_VAR and IV_BOUND are set to the corresponding
    1695                 :             :    induction variable descriptions, and true is returned.  If this is not
    1696                 :             :    the case, CONTROL_VAR and BOUND are set to the arguments of the
    1697                 :             :    condition and false is returned.  */
    1698                 :             : 
    1699                 :             : static enum comp_iv_rewrite
    1700                 :     6425613 : extract_cond_operands (struct ivopts_data *data, gimple *stmt,
    1701                 :             :                        tree **control_var, tree **bound,
    1702                 :             :                        struct iv **iv_var, struct iv **iv_bound)
    1703                 :             : {
    1704                 :             :   /* The objects returned when COND has constant operands.  */
    1705                 :     6425613 :   static struct iv const_iv;
    1706                 :     6425613 :   static tree zero;
    1707                 :     6425613 :   tree *op0 = &zero, *op1 = &zero;
    1708                 :     6425613 :   struct iv *iv0 = &const_iv, *iv1 = &const_iv;
    1709                 :     6425613 :   enum comp_iv_rewrite rewrite_type = COMP_IV_NA;
    1710                 :             : 
    1711                 :     6425613 :   if (gimple_code (stmt) == GIMPLE_COND)
    1712                 :             :     {
    1713                 :     6136445 :       gcond *cond_stmt = as_a <gcond *> (stmt);
    1714                 :     6136445 :       op0 = gimple_cond_lhs_ptr (cond_stmt);
    1715                 :     6136445 :       op1 = gimple_cond_rhs_ptr (cond_stmt);
    1716                 :             :     }
    1717                 :             :   else
    1718                 :             :     {
    1719                 :      289168 :       op0 = gimple_assign_rhs1_ptr (stmt);
    1720                 :      289168 :       op1 = gimple_assign_rhs2_ptr (stmt);
    1721                 :             :     }
    1722                 :             : 
    1723                 :     6425613 :   zero = integer_zero_node;
    1724                 :     6425613 :   const_iv.step = integer_zero_node;
    1725                 :             : 
    1726                 :     6425613 :   if (TREE_CODE (*op0) == SSA_NAME)
    1727                 :     6393464 :     iv0 = get_iv (data, *op0);
    1728                 :     6425613 :   if (TREE_CODE (*op1) == SSA_NAME)
    1729                 :     2887810 :     iv1 = get_iv (data, *op1);
    1730                 :             : 
    1731                 :             :   /* If both sides of comparison are IVs.  We can express ivs on both end.  */
    1732                 :     6425613 :   if (iv0 && iv1 && !integer_zerop (iv0->step) && !integer_zerop (iv1->step))
    1733                 :             :     {
    1734                 :       62060 :       rewrite_type = COMP_IV_EXPR_2;
    1735                 :       62060 :       goto end;
    1736                 :             :     }
    1737                 :             : 
    1738                 :             :   /* If none side of comparison is IV.  */
    1739                 :     4976748 :   if ((!iv0 || integer_zerop (iv0->step))
    1740                 :     7510651 :       && (!iv1 || integer_zerop (iv1->step)))
    1741                 :      812954 :     goto end;
    1742                 :             : 
    1743                 :             :   /* Control variable may be on the other side.  */
    1744                 :     5550599 :   if (!iv0 || integer_zerop (iv0->step))
    1745                 :             :     {
    1746                 :             :       std::swap (op0, op1);
    1747                 :             :       std::swap (iv0, iv1);
    1748                 :             :     }
    1749                 :             :   /* If one side is IV and the other side isn't loop invariant.  */
    1750                 :     5550599 :   if (!iv1)
    1751                 :             :     rewrite_type = COMP_IV_EXPR;
    1752                 :             :   /* If one side is IV and the other side is loop invariant.  */
    1753                 :     4689249 :   else if (!integer_zerop (iv0->step) && integer_zerop (iv1->step))
    1754                 :             :     rewrite_type = COMP_IV_ELIM;
    1755                 :             : 
    1756                 :     6425613 : end:
    1757                 :     6425613 :   if (control_var)
    1758                 :     6425613 :     *control_var = op0;
    1759                 :     6425613 :   if (iv_var)
    1760                 :     1323068 :     *iv_var = iv0;
    1761                 :     6425613 :   if (bound)
    1762                 :     6425613 :     *bound = op1;
    1763                 :     6425613 :   if (iv_bound)
    1764                 :     6425613 :     *iv_bound = iv1;
    1765                 :             : 
    1766                 :     6425613 :   return rewrite_type;
    1767                 :             : }
    1768                 :             : 
    1769                 :             : /* Checks whether the condition in STMT is interesting and if so,
    1770                 :             :    records it.  */
    1771                 :             : 
    1772                 :             : static void
    1773                 :     1323068 : find_interesting_uses_cond (struct ivopts_data *data, gimple *stmt)
    1774                 :             : {
    1775                 :     1323068 :   tree *var_p, *bound_p;
    1776                 :     1323068 :   struct iv *var_iv, *bound_iv;
    1777                 :     1323068 :   enum comp_iv_rewrite ret;
    1778                 :             : 
    1779                 :     1323068 :   ret = extract_cond_operands (data, stmt,
    1780                 :             :                                &var_p, &bound_p, &var_iv, &bound_iv);
    1781                 :     1323068 :   if (ret == COMP_IV_NA)
    1782                 :             :     {
    1783                 :      812954 :       find_interesting_uses_op (data, *var_p);
    1784                 :      812954 :       find_interesting_uses_op (data, *bound_p);
    1785                 :      812954 :       return;
    1786                 :             :     }
    1787                 :             : 
    1788                 :      510114 :   record_group_use (data, var_p, var_iv, stmt, USE_COMPARE, NULL_TREE);
    1789                 :             :   /* Record compare type iv_use for iv on the other side of comparison.  */
    1790                 :      510114 :   if (ret == COMP_IV_EXPR_2)
    1791                 :        1872 :     record_group_use (data, bound_p, bound_iv, stmt, USE_COMPARE, NULL_TREE);
    1792                 :             : }
    1793                 :             : 
    1794                 :             : /* Returns the outermost loop EXPR is obviously invariant in
    1795                 :             :    relative to the loop LOOP, i.e. if all its operands are defined
    1796                 :             :    outside of the returned loop.  Returns NULL if EXPR is not
    1797                 :             :    even obviously invariant in LOOP.  */
    1798                 :             : 
    1799                 :             : class loop *
    1800                 :      159681 : outermost_invariant_loop_for_expr (class loop *loop, tree expr)
    1801                 :             : {
    1802                 :      159681 :   basic_block def_bb;
    1803                 :      159681 :   unsigned i, len;
    1804                 :             : 
    1805                 :      159681 :   if (is_gimple_min_invariant (expr))
    1806                 :       25137 :     return current_loops->tree_root;
    1807                 :             : 
    1808                 :      134544 :   if (TREE_CODE (expr) == SSA_NAME)
    1809                 :             :     {
    1810                 :       77089 :       def_bb = gimple_bb (SSA_NAME_DEF_STMT (expr));
    1811                 :       77089 :       if (def_bb)
    1812                 :             :         {
    1813                 :       42647 :           if (flow_bb_inside_loop_p (loop, def_bb))
    1814                 :             :             return NULL;
    1815                 :       85284 :           return superloop_at_depth (loop,
    1816                 :       60471 :                                      loop_depth (def_bb->loop_father) + 1);
    1817                 :             :         }
    1818                 :             : 
    1819                 :       34442 :       return current_loops->tree_root;
    1820                 :             :     }
    1821                 :             : 
    1822                 :       57455 :   if (!EXPR_P (expr))
    1823                 :             :     return NULL;
    1824                 :             : 
    1825                 :       57455 :   unsigned maxdepth = 0;
    1826                 :       57455 :   len = TREE_OPERAND_LENGTH (expr);
    1827                 :      150107 :   for (i = 0; i < len; i++)
    1828                 :             :     {
    1829                 :       92667 :       class loop *ivloop;
    1830                 :       92667 :       if (!TREE_OPERAND (expr, i))
    1831                 :           0 :         continue;
    1832                 :             : 
    1833                 :       92667 :       ivloop = outermost_invariant_loop_for_expr (loop, TREE_OPERAND (expr, i));
    1834                 :       92667 :       if (!ivloop)
    1835                 :             :         return NULL;
    1836                 :      164971 :       maxdepth = MAX (maxdepth, loop_depth (ivloop));
    1837                 :             :     }
    1838                 :             : 
    1839                 :       57440 :   return superloop_at_depth (loop, maxdepth);
    1840                 :             : }
    1841                 :             : 
    1842                 :             : /* Returns true if expression EXPR is obviously invariant in LOOP,
    1843                 :             :    i.e. if all its operands are defined outside of the LOOP.  LOOP
    1844                 :             :    should not be the function body.  */
    1845                 :             : 
    1846                 :             : bool
    1847                 :     8162354 : expr_invariant_in_loop_p (class loop *loop, tree expr)
    1848                 :             : {
    1849                 :     8162354 :   basic_block def_bb;
    1850                 :     8162354 :   unsigned i, len;
    1851                 :             : 
    1852                 :     8162354 :   gcc_assert (loop_depth (loop) > 0);
    1853                 :             : 
    1854                 :     8162354 :   if (is_gimple_min_invariant (expr))
    1855                 :             :     return true;
    1856                 :             : 
    1857                 :     5832013 :   if (TREE_CODE (expr) == SSA_NAME)
    1858                 :             :     {
    1859                 :     5761890 :       def_bb = gimple_bb (SSA_NAME_DEF_STMT (expr));
    1860                 :     5761890 :       if (def_bb
    1861                 :     5761890 :           && flow_bb_inside_loop_p (loop, def_bb))
    1862                 :             :         return false;
    1863                 :             : 
    1864                 :     3512068 :       return true;
    1865                 :             :     }
    1866                 :             : 
    1867                 :       70123 :   if (!EXPR_P (expr))
    1868                 :             :     return false;
    1869                 :             : 
    1870                 :       70120 :   len = TREE_OPERAND_LENGTH (expr);
    1871                 :      143091 :   for (i = 0; i < len; i++)
    1872                 :       99672 :     if (TREE_OPERAND (expr, i)
    1873                 :       99672 :         && !expr_invariant_in_loop_p (loop, TREE_OPERAND (expr, i)))
    1874                 :             :       return false;
    1875                 :             : 
    1876                 :             :   return true;
    1877                 :             : }
    1878                 :             : 
    1879                 :             : /* Given expression EXPR which computes inductive values with respect
    1880                 :             :    to loop recorded in DATA, this function returns biv from which EXPR
    1881                 :             :    is derived by tracing definition chains of ssa variables in EXPR.  */
    1882                 :             : 
    1883                 :             : static struct iv*
    1884                 :      641604 : find_deriving_biv_for_expr (struct ivopts_data *data, tree expr)
    1885                 :             : {
    1886                 :     1062088 :   struct iv *iv;
    1887                 :     1062088 :   unsigned i, n;
    1888                 :     1062088 :   tree e2, e1;
    1889                 :     1062088 :   enum tree_code code;
    1890                 :     1062088 :   gimple *stmt;
    1891                 :             : 
    1892                 :     1062088 :   if (expr == NULL_TREE)
    1893                 :             :     return NULL;
    1894                 :             : 
    1895                 :     1061898 :   if (is_gimple_min_invariant (expr))
    1896                 :             :     return NULL;
    1897                 :             : 
    1898                 :      833616 :   code = TREE_CODE (expr);
    1899                 :      833616 :   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
    1900                 :             :     {
    1901                 :        7143 :       n = TREE_OPERAND_LENGTH (expr);
    1902                 :        8829 :       for (i = 0; i < n; i++)
    1903                 :             :         {
    1904                 :        8409 :           iv = find_deriving_biv_for_expr (data, TREE_OPERAND (expr, i));
    1905                 :        8409 :           if (iv)
    1906                 :        6723 :             return iv;
    1907                 :             :         }
    1908                 :             :     }
    1909                 :             : 
    1910                 :             :   /* Stop if it's not ssa name.  */
    1911                 :      826893 :   if (code != SSA_NAME)
    1912                 :             :     return NULL;
    1913                 :             : 
    1914                 :      825913 :   iv = get_iv (data, expr);
    1915                 :      825913 :   if (!iv || integer_zerop (iv->step))
    1916                 :       32845 :     return NULL;
    1917                 :      793068 :   else if (iv->biv_p)
    1918                 :             :     return iv;
    1919                 :             : 
    1920                 :      586714 :   stmt = SSA_NAME_DEF_STMT (expr);
    1921                 :      586714 :   if (gphi *phi = dyn_cast <gphi *> (stmt))
    1922                 :             :     {
    1923                 :         779 :       ssa_op_iter iter;
    1924                 :         779 :       use_operand_p use_p;
    1925                 :         779 :       basic_block phi_bb = gimple_bb (phi);
    1926                 :             : 
    1927                 :             :       /* Skip loop header PHI that doesn't define biv.  */
    1928                 :         779 :       if (phi_bb->loop_father == data->current_loop)
    1929                 :             :         return NULL;
    1930                 :             : 
    1931                 :           0 :       if (virtual_operand_p (gimple_phi_result (phi)))
    1932                 :             :         return NULL;
    1933                 :             : 
    1934                 :           0 :       FOR_EACH_PHI_ARG (use_p, phi, iter, SSA_OP_USE)
    1935                 :             :         {
    1936                 :           0 :           tree use = USE_FROM_PTR (use_p);
    1937                 :           0 :           iv = find_deriving_biv_for_expr (data, use);
    1938                 :           0 :           if (iv)
    1939                 :           0 :             return iv;
    1940                 :             :         }
    1941                 :             :       return NULL;
    1942                 :             :     }
    1943                 :      585935 :   if (gimple_code (stmt) != GIMPLE_ASSIGN)
    1944                 :             :     return NULL;
    1945                 :             : 
    1946                 :      585935 :   e1 = gimple_assign_rhs1 (stmt);
    1947                 :      585935 :   code = gimple_assign_rhs_code (stmt);
    1948                 :      585935 :   if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
    1949                 :             :     return find_deriving_biv_for_expr (data, e1);
    1950                 :             : 
    1951                 :      577898 :   switch (code)
    1952                 :             :     {
    1953                 :      425936 :     case MULT_EXPR:
    1954                 :      425936 :     case PLUS_EXPR:
    1955                 :      425936 :     case MINUS_EXPR:
    1956                 :      425936 :     case POINTER_PLUS_EXPR:
    1957                 :             :       /* Increments, decrements and multiplications by a constant
    1958                 :             :          are simple.  */
    1959                 :      425936 :       e2 = gimple_assign_rhs2 (stmt);
    1960                 :      425936 :       iv = find_deriving_biv_for_expr (data, e2);
    1961                 :      425936 :       if (iv)
    1962                 :             :         return iv;
    1963                 :      412447 :       gcc_fallthrough ();
    1964                 :             : 
    1965                 :      412447 :     CASE_CONVERT:
    1966                 :             :       /* Casts are simple.  */
    1967                 :      412447 :       return find_deriving_biv_for_expr (data, e1);
    1968                 :             : 
    1969                 :             :     default:
    1970                 :             :       break;
    1971                 :             :     }
    1972                 :             : 
    1973                 :             :   return NULL;
    1974                 :             : }
    1975                 :             : 
    1976                 :             : /* Record BIV, its predecessor and successor that they are used in
    1977                 :             :    address type uses.  */
    1978                 :             : 
    1979                 :             : static void
    1980                 :      495888 : record_biv_for_address_use (struct ivopts_data *data, struct iv *biv)
    1981                 :             : {
    1982                 :      495888 :   unsigned i;
    1983                 :      495888 :   tree type, base_1, base_2;
    1984                 :      495888 :   bitmap_iterator bi;
    1985                 :             : 
    1986                 :      494983 :   if (!biv || !biv->biv_p || integer_zerop (biv->step)
    1987                 :      990871 :       || biv->have_address_use || !biv->no_overflow)
    1988                 :      258295 :     return;
    1989                 :             : 
    1990                 :      465803 :   type = TREE_TYPE (biv->base);
    1991                 :      465803 :   if (!INTEGRAL_TYPE_P (type))
    1992                 :             :     return;
    1993                 :             : 
    1994                 :      237593 :   biv->have_address_use = true;
    1995                 :      237593 :   data->bivs_not_used_in_addr--;
    1996                 :      237593 :   base_1 = fold_build2 (PLUS_EXPR, type, biv->base, biv->step);
    1997                 :     2154007 :   EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
    1998                 :             :     {
    1999                 :     1916414 :       struct iv *iv = ver_info (data, i)->iv;
    2000                 :             : 
    2001                 :     1727434 :       if (!iv || !iv->biv_p || integer_zerop (iv->step)
    2002                 :     2714648 :           || iv->have_address_use || !iv->no_overflow)
    2003                 :     1660808 :         continue;
    2004                 :             : 
    2005                 :      255606 :       if (type != TREE_TYPE (iv->base)
    2006                 :      255606 :           || !INTEGRAL_TYPE_P (TREE_TYPE (iv->base)))
    2007                 :       22676 :         continue;
    2008                 :             : 
    2009                 :      232930 :       if (!operand_equal_p (biv->step, iv->step, 0))
    2010                 :        5247 :         continue;
    2011                 :             : 
    2012                 :      227683 :       base_2 = fold_build2 (PLUS_EXPR, type, iv->base, iv->step);
    2013                 :      227683 :       if (operand_equal_p (base_1, iv->base, 0)
    2014                 :      227683 :           || operand_equal_p (base_2, biv->base, 0))
    2015                 :             :         {
    2016                 :      223016 :           iv->have_address_use = true;
    2017                 :      223016 :           data->bivs_not_used_in_addr--;
    2018                 :             :         }
    2019                 :             :     }
    2020                 :             : }
    2021                 :             : 
    2022                 :             : /* Cumulates the steps of indices into DATA and replaces their values with the
    2023                 :             :    initial ones.  Returns false when the value of the index cannot be determined.
    2024                 :             :    Callback for for_each_index.  */
    2025                 :             : 
    2026                 :             : struct ifs_ivopts_data
    2027                 :             : {
    2028                 :             :   struct ivopts_data *ivopts_data;
    2029                 :             :   gimple *stmt;
    2030                 :             :   tree step;
    2031                 :             : };
    2032                 :             : 
    2033                 :             : static bool
    2034                 :     2044431 : idx_find_step (tree base, tree *idx, void *data)
    2035                 :             : {
    2036                 :     2044431 :   struct ifs_ivopts_data *dta = (struct ifs_ivopts_data *) data;
    2037                 :     2044431 :   struct iv *iv;
    2038                 :     2044431 :   bool use_overflow_semantics = false;
    2039                 :     2044431 :   tree step, iv_base, iv_step, lbound, off;
    2040                 :     2044431 :   class loop *loop = dta->ivopts_data->current_loop;
    2041                 :             : 
    2042                 :             :   /* If base is a component ref, require that the offset of the reference
    2043                 :             :      be invariant.  */
    2044                 :     2044431 :   if (TREE_CODE (base) == COMPONENT_REF)
    2045                 :             :     {
    2046                 :          75 :       off = component_ref_field_offset (base);
    2047                 :          75 :       return expr_invariant_in_loop_p (loop, off);
    2048                 :             :     }
    2049                 :             : 
    2050                 :             :   /* If base is array, first check whether we will be able to move the
    2051                 :             :      reference out of the loop (in order to take its address in strength
    2052                 :             :      reduction).  In order for this to work we need both lower bound
    2053                 :             :      and step to be loop invariants.  */
    2054                 :     2044356 :   if (TREE_CODE (base) == ARRAY_REF || TREE_CODE (base) == ARRAY_RANGE_REF)
    2055                 :             :     {
    2056                 :             :       /* Moreover, for a range, the size needs to be invariant as well.  */
    2057                 :      484593 :       if (TREE_CODE (base) == ARRAY_RANGE_REF
    2058                 :      484593 :           && !expr_invariant_in_loop_p (loop, TYPE_SIZE (TREE_TYPE (base))))
    2059                 :             :         return false;
    2060                 :             : 
    2061                 :      484593 :       step = array_ref_element_size (base);
    2062                 :      484593 :       lbound = array_ref_low_bound (base);
    2063                 :             : 
    2064                 :      484593 :       if (!expr_invariant_in_loop_p (loop, step)
    2065                 :      484593 :           || !expr_invariant_in_loop_p (loop, lbound))
    2066                 :           5 :         return false;
    2067                 :             :     }
    2068                 :             : 
    2069                 :     2044351 :   if (TREE_CODE (*idx) != SSA_NAME)
    2070                 :             :     return true;
    2071                 :             : 
    2072                 :     1643339 :   iv = get_iv (dta->ivopts_data, *idx);
    2073                 :     1643339 :   if (!iv)
    2074                 :             :     return false;
    2075                 :             : 
    2076                 :             :   /* XXX  We produce for a base of *D42 with iv->base being &x[0]
    2077                 :             :           *&x[0], which is not folded and does not trigger the
    2078                 :             :           ARRAY_REF path below.  */
    2079                 :     1039322 :   *idx = iv->base;
    2080                 :             : 
    2081                 :     1039322 :   if (integer_zerop (iv->step))
    2082                 :             :     return true;
    2083                 :             : 
    2084                 :      768946 :   if (TREE_CODE (base) == ARRAY_REF || TREE_CODE (base) == ARRAY_RANGE_REF)
    2085                 :             :     {
    2086                 :      277794 :       step = array_ref_element_size (base);
    2087                 :             : 
    2088                 :             :       /* We only handle addresses whose step is an integer constant.  */
    2089                 :      277794 :       if (TREE_CODE (step) != INTEGER_CST)
    2090                 :             :         return false;
    2091                 :             :     }
    2092                 :             :   else
    2093                 :             :     /* The step for pointer arithmetics already is 1 byte.  */
    2094                 :      491152 :     step = size_one_node;
    2095                 :             : 
    2096                 :      768629 :   iv_base = iv->base;
    2097                 :      768629 :   iv_step = iv->step;
    2098                 :      768629 :   if (iv->no_overflow && nowrap_type_p (TREE_TYPE (iv_step)))
    2099                 :             :     use_overflow_semantics = true;
    2100                 :             : 
    2101                 :      768629 :   if (!convert_affine_scev (dta->ivopts_data->current_loop,
    2102                 :             :                             sizetype, &iv_base, &iv_step, dta->stmt,
    2103                 :             :                             use_overflow_semantics))
    2104                 :             :     {
    2105                 :             :       /* The index might wrap.  */
    2106                 :             :       return false;
    2107                 :             :     }
    2108                 :             : 
    2109                 :      765728 :   step = fold_build2 (MULT_EXPR, sizetype, step, iv_step);
    2110                 :      765728 :   dta->step = fold_build2 (PLUS_EXPR, sizetype, dta->step, step);
    2111                 :             : 
    2112                 :      765728 :   if (dta->ivopts_data->bivs_not_used_in_addr)
    2113                 :             :     {
    2114                 :      495888 :       if (!iv->biv_p)
    2115                 :      207259 :         iv = find_deriving_biv_for_expr (dta->ivopts_data, iv->ssa_name);
    2116                 :             : 
    2117                 :      495888 :       record_biv_for_address_use (dta->ivopts_data, iv);
    2118                 :             :     }
    2119                 :             :   return true;
    2120                 :             : }
    2121                 :             : 
    2122                 :             : /* Records use in index IDX.  Callback for for_each_index.  Ivopts data
    2123                 :             :    object is passed to it in DATA.  */
    2124                 :             : 
    2125                 :             : static bool
    2126                 :     1711120 : idx_record_use (tree base, tree *idx,
    2127                 :             :                 void *vdata)
    2128                 :             : {
    2129                 :     1711120 :   struct ivopts_data *data = (struct ivopts_data *) vdata;
    2130                 :     1711120 :   find_interesting_uses_op (data, *idx);
    2131                 :     1711120 :   if (TREE_CODE (base) == ARRAY_REF || TREE_CODE (base) == ARRAY_RANGE_REF)
    2132                 :             :     {
    2133                 :      211902 :       if (TREE_OPERAND (base, 2))
    2134                 :        4639 :         find_interesting_uses_op (data, TREE_OPERAND (base, 2));
    2135                 :      211902 :       if (TREE_OPERAND (base, 3))
    2136                 :       14339 :         find_interesting_uses_op (data, TREE_OPERAND (base, 3));
    2137                 :             :     }
    2138                 :     1711120 :   return true;
    2139                 :             : }
    2140                 :             : 
    2141                 :             : /* If we can prove that TOP = cst * BOT for some constant cst,
    2142                 :             :    store cst to MUL and return true.  Otherwise return false.
    2143                 :             :    The returned value is always sign-extended, regardless of the
    2144                 :             :    signedness of TOP and BOT.  */
    2145                 :             : 
    2146                 :             : static bool
    2147                 :    16159529 : constant_multiple_of (tree top, tree bot, widest_int *mul)
    2148                 :             : {
    2149                 :    16159529 :   tree mby;
    2150                 :    16159529 :   enum tree_code code;
    2151                 :    16159529 :   unsigned precision = TYPE_PRECISION (TREE_TYPE (top));
    2152                 :    16159529 :   widest_int res, p0, p1;
    2153                 :             : 
    2154                 :    16159529 :   STRIP_NOPS (top);
    2155                 :    16159529 :   STRIP_NOPS (bot);
    2156                 :             : 
    2157                 :    16159529 :   if (operand_equal_p (top, bot, 0))
    2158                 :             :     {
    2159                 :     7608719 :       *mul = 1;
    2160                 :     7608719 :       return true;
    2161                 :             :     }
    2162                 :             : 
    2163                 :     8550810 :   code = TREE_CODE (top);
    2164                 :     8550810 :   switch (code)
    2165                 :             :     {
    2166                 :      853429 :     case MULT_EXPR:
    2167                 :      853429 :       mby = TREE_OPERAND (top, 1);
    2168                 :      853429 :       if (TREE_CODE (mby) != INTEGER_CST)
    2169                 :             :         return false;
    2170                 :             : 
    2171                 :      815827 :       if (!constant_multiple_of (TREE_OPERAND (top, 0), bot, &res))
    2172                 :             :         return false;
    2173                 :             : 
    2174                 :       49636 :       *mul = wi::sext (res * wi::to_widest (mby), precision);
    2175                 :       49636 :       return true;
    2176                 :             : 
    2177                 :        8582 :     case PLUS_EXPR:
    2178                 :        8582 :     case MINUS_EXPR:
    2179                 :        8582 :       if (!constant_multiple_of (TREE_OPERAND (top, 0), bot, &p0)
    2180                 :        8582 :           || !constant_multiple_of (TREE_OPERAND (top, 1), bot, &p1))
    2181                 :        8582 :         return false;
    2182                 :             : 
    2183                 :           0 :       if (code == MINUS_EXPR)
    2184                 :           0 :         p1 = -p1;
    2185                 :           0 :       *mul = wi::sext (p0 + p1, precision);
    2186                 :           0 :       return true;
    2187                 :             : 
    2188                 :     6684702 :     case INTEGER_CST:
    2189                 :     6684702 :       if (TREE_CODE (bot) != INTEGER_CST)
    2190                 :             :         return false;
    2191                 :             : 
    2192                 :     6420455 :       p0 = widest_int::from (wi::to_wide (top), SIGNED);
    2193                 :     6420455 :       p1 = widest_int::from (wi::to_wide (bot), SIGNED);
    2194                 :     6420455 :       if (p1 == 0)
    2195                 :             :         return false;
    2196                 :     6419601 :       *mul = wi::sext (wi::divmod_trunc (p0, p1, SIGNED, &res), precision);
    2197                 :     6419601 :       return res == 0;
    2198                 :             : 
    2199                 :             :     default:
    2200                 :             :       if (POLY_INT_CST_P (top)
    2201                 :             :           && POLY_INT_CST_P (bot)
    2202                 :             :           && constant_multiple_p (wi::to_poly_widest (top),
    2203                 :             :                                   wi::to_poly_widest (bot), mul))
    2204                 :             :         return true;
    2205                 :             : 
    2206                 :             :       return false;
    2207                 :             :     }
    2208                 :    16159529 : }
    2209                 :             : 
    2210                 :             : /* Return true if memory reference REF with step STEP may be unaligned.  */
    2211                 :             : 
    2212                 :             : static bool
    2213                 :           0 : may_be_unaligned_p (tree ref, tree step)
    2214                 :             : {
    2215                 :             :   /* TARGET_MEM_REFs are translated directly to valid MEMs on the target,
    2216                 :             :      thus they are not misaligned.  */
    2217                 :           0 :   if (TREE_CODE (ref) == TARGET_MEM_REF)
    2218                 :             :     return false;
    2219                 :             : 
    2220                 :           0 :   unsigned int align = TYPE_ALIGN (TREE_TYPE (ref));
    2221                 :           0 :   if (GET_MODE_ALIGNMENT (TYPE_MODE (TREE_TYPE (ref))) > align)
    2222                 :           0 :     align = GET_MODE_ALIGNMENT (TYPE_MODE (TREE_TYPE (ref)));
    2223                 :             : 
    2224                 :           0 :   unsigned HOST_WIDE_INT bitpos;
    2225                 :           0 :   unsigned int ref_align;
    2226                 :           0 :   get_object_alignment_1 (ref, &ref_align, &bitpos);
    2227                 :           0 :   if (ref_align < align
    2228                 :           0 :       || (bitpos % align) != 0
    2229                 :           0 :       || (bitpos % BITS_PER_UNIT) != 0)
    2230                 :             :     return true;
    2231                 :             : 
    2232                 :           0 :   unsigned int trailing_zeros = tree_ctz (step);
    2233                 :           0 :   if (trailing_zeros < HOST_BITS_PER_INT
    2234                 :           0 :       && (1U << trailing_zeros) * BITS_PER_UNIT < align)
    2235                 :             :     return true;
    2236                 :             : 
    2237                 :             :   return false;
    2238                 :             : }
    2239                 :             : 
    2240                 :             : /* Return true if EXPR may be non-addressable.   */
    2241                 :             : 
    2242                 :             : bool
    2243                 :    11095895 : may_be_nonaddressable_p (tree expr)
    2244                 :             : {
    2245                 :    11740671 :   switch (TREE_CODE (expr))
    2246                 :             :     {
    2247                 :     8717625 :     case VAR_DECL:
    2248                 :             :       /* Check if it's a register variable.  */
    2249                 :     8717625 :       return DECL_HARD_REGISTER (expr);
    2250                 :             : 
    2251                 :             :     case TARGET_MEM_REF:
    2252                 :             :       /* TARGET_MEM_REFs are translated directly to valid MEMs on the
    2253                 :             :          target, thus they are always addressable.  */
    2254                 :             :       return false;
    2255                 :             : 
    2256                 :     1165844 :     case MEM_REF:
    2257                 :             :       /* Likewise for MEM_REFs, modulo the storage order.  */
    2258                 :     1165844 :       return REF_REVERSE_STORAGE_ORDER (expr);
    2259                 :             : 
    2260                 :          49 :     case BIT_FIELD_REF:
    2261                 :          49 :       if (REF_REVERSE_STORAGE_ORDER (expr))
    2262                 :             :         return true;
    2263                 :          49 :       return may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
    2264                 :             : 
    2265                 :      651652 :     case COMPONENT_REF:
    2266                 :      651652 :       if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (expr, 0))))
    2267                 :             :         return true;
    2268                 :      651652 :       return DECL_NONADDRESSABLE_P (TREE_OPERAND (expr, 1))
    2269                 :      651652 :              || may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
    2270                 :             : 
    2271                 :      625768 :     case ARRAY_REF:
    2272                 :      625768 :     case ARRAY_RANGE_REF:
    2273                 :      625768 :       if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (expr, 0))))
    2274                 :             :         return true;
    2275                 :      625768 :       return may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
    2276                 :             : 
    2277                 :       19254 :     case VIEW_CONVERT_EXPR:
    2278                 :             :       /* This kind of view-conversions may wrap non-addressable objects
    2279                 :             :          and make them look addressable.  After some processing the
    2280                 :             :          non-addressability may be uncovered again, causing ADDR_EXPRs
    2281                 :             :          of inappropriate objects to be built.  */
    2282                 :       19254 :       if (is_gimple_reg (TREE_OPERAND (expr, 0))
    2283                 :       19254 :           || !is_gimple_addressable (TREE_OPERAND (expr, 0)))
    2284                 :             :         return true;
    2285                 :       18959 :       return may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
    2286                 :             : 
    2287                 :             :     CASE_CONVERT:
    2288                 :             :       return true;
    2289                 :             : 
    2290                 :             :     default:
    2291                 :             :       break;
    2292                 :             :     }
    2293                 :             : 
    2294                 :             :   return false;
    2295                 :             : }
    2296                 :             : 
    2297                 :             : /* Finds addresses in *OP_P inside STMT.  */
    2298                 :             : 
    2299                 :             : static void
    2300                 :     2561421 : find_interesting_uses_address (struct ivopts_data *data, gimple *stmt,
    2301                 :             :                                tree *op_p)
    2302                 :             : {
    2303                 :     2561421 :   tree base = *op_p, step = size_zero_node;
    2304                 :     2561421 :   struct iv *civ;
    2305                 :     2561421 :   struct ifs_ivopts_data ifs_ivopts_data;
    2306                 :             : 
    2307                 :             :   /* Do not play with volatile memory references.  A bit too conservative,
    2308                 :             :      perhaps, but safe.  */
    2309                 :     5122842 :   if (gimple_has_volatile_ops (stmt))
    2310                 :        8197 :     goto fail;
    2311                 :             : 
    2312                 :             :   /* Ignore bitfields for now.  Not really something terribly complicated
    2313                 :             :      to handle.  TODO.  */
    2314                 :     2553224 :   if (TREE_CODE (base) == BIT_FIELD_REF)
    2315                 :      115036 :     goto fail;
    2316                 :             : 
    2317                 :     2438188 :   base = unshare_expr (base);
    2318                 :             : 
    2319                 :     2438188 :   if (TREE_CODE (base) == TARGET_MEM_REF)
    2320                 :             :     {
    2321                 :      294667 :       tree type = build_pointer_type (TREE_TYPE (base));
    2322                 :      294667 :       tree astep;
    2323                 :             : 
    2324                 :      294667 :       if (TMR_BASE (base)
    2325                 :      294667 :           && TREE_CODE (TMR_BASE (base)) == SSA_NAME)
    2326                 :             :         {
    2327                 :      269998 :           civ = get_iv (data, TMR_BASE (base));
    2328                 :      269998 :           if (!civ)
    2329                 :      239912 :             goto fail;
    2330                 :             : 
    2331                 :       30086 :           TMR_BASE (base) = civ->base;
    2332                 :       30086 :           step = civ->step;
    2333                 :             :         }
    2334                 :       54755 :       if (TMR_INDEX2 (base)
    2335                 :       54755 :           && TREE_CODE (TMR_INDEX2 (base)) == SSA_NAME)
    2336                 :             :         {
    2337                 :       11665 :           civ = get_iv (data, TMR_INDEX2 (base));
    2338                 :       11665 :           if (!civ)
    2339                 :        4567 :             goto fail;
    2340                 :             : 
    2341                 :        7098 :           TMR_INDEX2 (base) = civ->base;
    2342                 :        7098 :           step = civ->step;
    2343                 :             :         }
    2344                 :       50188 :       if (TMR_INDEX (base)
    2345                 :       50188 :           && TREE_CODE (TMR_INDEX (base)) == SSA_NAME)
    2346                 :             :         {
    2347                 :       50188 :           civ = get_iv (data, TMR_INDEX (base));
    2348                 :       50188 :           if (!civ)
    2349                 :       50188 :             goto fail;
    2350                 :             : 
    2351                 :           0 :           TMR_INDEX (base) = civ->base;
    2352                 :           0 :           astep = civ->step;
    2353                 :             : 
    2354                 :           0 :           if (astep)
    2355                 :             :             {
    2356                 :           0 :               if (TMR_STEP (base))
    2357                 :           0 :                 astep = fold_build2 (MULT_EXPR, type, TMR_STEP (base), astep);
    2358                 :             : 
    2359                 :           0 :               step = fold_build2 (PLUS_EXPR, type, step, astep);
    2360                 :             :             }
    2361                 :             :         }
    2362                 :             : 
    2363                 :           0 :       if (integer_zerop (step))
    2364                 :           0 :         goto fail;
    2365                 :           0 :       base = tree_mem_ref_addr (type, base);
    2366                 :             :     }
    2367                 :             :   else
    2368                 :             :     {
    2369                 :     2143521 :       ifs_ivopts_data.ivopts_data = data;
    2370                 :     2143521 :       ifs_ivopts_data.stmt = stmt;
    2371                 :     2143521 :       ifs_ivopts_data.step = size_zero_node;
    2372                 :     2143521 :       if (!for_each_index (&base, idx_find_step, &ifs_ivopts_data)
    2373                 :     2143521 :           || integer_zerop (ifs_ivopts_data.step))
    2374                 :     1379385 :         goto fail;
    2375                 :      764136 :       step = ifs_ivopts_data.step;
    2376                 :             : 
    2377                 :             :       /* Check that the base expression is addressable.  This needs
    2378                 :             :          to be done after substituting bases of IVs into it.  */
    2379                 :      764136 :       if (may_be_nonaddressable_p (base))
    2380                 :         950 :         goto fail;
    2381                 :             : 
    2382                 :             :       /* Moreover, on strict alignment platforms, check that it is
    2383                 :             :          sufficiently aligned.  */
    2384                 :      763186 :       if (STRICT_ALIGNMENT && may_be_unaligned_p (base, step))
    2385                 :             :         goto fail;
    2386                 :             : 
    2387                 :      763186 :       base = build_fold_addr_expr (base);
    2388                 :             : 
    2389                 :             :       /* Substituting bases of IVs into the base expression might
    2390                 :             :          have caused folding opportunities.  */
    2391                 :      763186 :       if (TREE_CODE (base) == ADDR_EXPR)
    2392                 :             :         {
    2393                 :      425828 :           tree *ref = &TREE_OPERAND (base, 0);
    2394                 :     1487214 :           while (handled_component_p (*ref))
    2395                 :      635558 :             ref = &TREE_OPERAND (*ref, 0);
    2396                 :      425828 :           if (TREE_CODE (*ref) == MEM_REF)
    2397                 :             :             {
    2398                 :      287230 :               tree tem = fold_binary (MEM_REF, TREE_TYPE (*ref),
    2399                 :             :                                       TREE_OPERAND (*ref, 0),
    2400                 :             :                                       TREE_OPERAND (*ref, 1));
    2401                 :      287230 :               if (tem)
    2402                 :           0 :                 *ref = tem;
    2403                 :             :             }
    2404                 :             :         }
    2405                 :             :     }
    2406                 :             : 
    2407                 :      763186 :   civ = alloc_iv (data, base, step);
    2408                 :             :   /* Fail if base object of this memory reference is unknown.  */
    2409                 :      763186 :   if (civ->base_object == NULL_TREE)
    2410                 :        7276 :     goto fail;
    2411                 :             : 
    2412                 :      755910 :   record_group_use (data, op_p, civ, stmt, USE_REF_ADDRESS, TREE_TYPE (*op_p));
    2413                 :      755910 :   return;
    2414                 :             : 
    2415                 :     1805511 : fail:
    2416                 :     1805511 :   for_each_index (op_p, idx_record_use, data);
    2417                 :             : }
    2418                 :             : 
    2419                 :             : /* Finds and records invariants used in STMT.  */
    2420                 :             : 
    2421                 :             : static void
    2422                 :    14131303 : find_invariants_stmt (struct ivopts_data *data, gimple *stmt)
    2423                 :             : {
    2424                 :    14131303 :   ssa_op_iter iter;
    2425                 :    14131303 :   use_operand_p use_p;
    2426                 :    14131303 :   tree op;
    2427                 :             : 
    2428                 :    46779302 :   FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
    2429                 :             :     {
    2430                 :    18516696 :       op = USE_FROM_PTR (use_p);
    2431                 :    18516696 :       record_invariant (data, op, false);
    2432                 :             :     }
    2433                 :    14131303 : }
    2434                 :             : 
    2435                 :             : /* CALL calls an internal function.  If operand *OP_P will become an
    2436                 :             :    address when the call is expanded, return the type of the memory
    2437                 :             :    being addressed, otherwise return null.  */
    2438                 :             : 
    2439                 :             : static tree
    2440                 :        1194 : get_mem_type_for_internal_fn (gcall *call, tree *op_p)
    2441                 :             : {
    2442                 :        1194 :   switch (gimple_call_internal_fn (call))
    2443                 :             :     {
    2444                 :         163 :     case IFN_MASK_LOAD:
    2445                 :         163 :     case IFN_MASK_LOAD_LANES:
    2446                 :         163 :     case IFN_MASK_LEN_LOAD_LANES:
    2447                 :         163 :     case IFN_LEN_LOAD:
    2448                 :         163 :     case IFN_MASK_LEN_LOAD:
    2449                 :         163 :       if (op_p == gimple_call_arg_ptr (call, 0))
    2450                 :         163 :         return TREE_TYPE (gimple_call_lhs (call));
    2451                 :             :       return NULL_TREE;
    2452                 :             : 
    2453                 :         231 :     case IFN_MASK_STORE:
    2454                 :         231 :     case IFN_MASK_STORE_LANES:
    2455                 :         231 :     case IFN_MASK_LEN_STORE_LANES:
    2456                 :         231 :     case IFN_LEN_STORE:
    2457                 :         231 :     case IFN_MASK_LEN_STORE:
    2458                 :         231 :       {
    2459                 :         231 :         if (op_p == gimple_call_arg_ptr (call, 0))
    2460                 :             :           {
    2461                 :         231 :             internal_fn ifn = gimple_call_internal_fn (call);
    2462                 :         231 :             int index = internal_fn_stored_value_index (ifn);
    2463                 :         231 :             return TREE_TYPE (gimple_call_arg (call, index));
    2464                 :             :           }
    2465                 :             :         return NULL_TREE;
    2466                 :             :       }
    2467                 :             : 
    2468                 :             :     default:
    2469                 :             :       return NULL_TREE;
    2470                 :             :     }
    2471                 :             : }
    2472                 :             : 
    2473                 :             : /* IV is a (non-address) iv that describes operand *OP_P of STMT.
    2474                 :             :    Return true if the operand will become an address when STMT
    2475                 :             :    is expanded and record the associated address use if so.  */
    2476                 :             : 
    2477                 :             : static bool
    2478                 :     1589398 : find_address_like_use (struct ivopts_data *data, gimple *stmt, tree *op_p,
    2479                 :             :                        struct iv *iv)
    2480                 :             : {
    2481                 :             :   /* Fail if base object of this memory reference is unknown.  */
    2482                 :     1589398 :   if (iv->base_object == NULL_TREE)
    2483                 :             :     return false;
    2484                 :             : 
    2485                 :      549662 :   tree mem_type = NULL_TREE;
    2486                 :      549662 :   if (gcall *call = dyn_cast <gcall *> (stmt))
    2487                 :      112331 :     if (gimple_call_internal_p (call))
    2488                 :        1194 :       mem_type = get_mem_type_for_internal_fn (call, op_p);
    2489                 :        1194 :   if (mem_type)
    2490                 :             :     {
    2491                 :         394 :       iv = alloc_iv (data, iv->base, iv->step);
    2492                 :         394 :       record_group_use (data, op_p, iv, stmt, USE_PTR_ADDRESS, mem_type);
    2493                 :         394 :       return true;
    2494                 :             :     }
    2495                 :             :   return false;
    2496                 :             : }
    2497                 :             : 
    2498                 :             : /* Finds interesting uses of induction variables in the statement STMT.  */
    2499                 :             : 
    2500                 :             : static void
    2501                 :    14131303 : find_interesting_uses_stmt (struct ivopts_data *data, gimple *stmt)
    2502                 :             : {
    2503                 :    14131303 :   struct iv *iv;
    2504                 :    14131303 :   tree op, *lhs, *rhs;
    2505                 :    14131303 :   ssa_op_iter iter;
    2506                 :    14131303 :   use_operand_p use_p;
    2507                 :    14131303 :   enum tree_code code;
    2508                 :             : 
    2509                 :    14131303 :   find_invariants_stmt (data, stmt);
    2510                 :             : 
    2511                 :    14131303 :   if (gimple_code (stmt) == GIMPLE_COND)
    2512                 :             :     {
    2513                 :     1236567 :       find_interesting_uses_cond (data, stmt);
    2514                 :     8037485 :       return;
    2515                 :             :     }
    2516                 :             : 
    2517                 :    12894736 :   if (is_gimple_assign (stmt))
    2518                 :             :     {
    2519                 :     9788961 :       lhs = gimple_assign_lhs_ptr (stmt);
    2520                 :     9788961 :       rhs = gimple_assign_rhs1_ptr (stmt);
    2521                 :             : 
    2522                 :     9788961 :       if (TREE_CODE (*lhs) == SSA_NAME)
    2523                 :             :         {
    2524                 :             :           /* If the statement defines an induction variable, the uses are not
    2525                 :             :              interesting by themselves.  */
    2526                 :             : 
    2527                 :     8686733 :           iv = get_iv (data, *lhs);
    2528                 :             : 
    2529                 :     8686733 :           if (iv && !integer_zerop (iv->step))
    2530                 :             :             return;
    2531                 :             :         }
    2532                 :             : 
    2533                 :     7735806 :       code = gimple_assign_rhs_code (stmt);
    2534                 :     7735806 :       if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
    2535                 :     7735806 :           && (REFERENCE_CLASS_P (*rhs)
    2536                 :     1273219 :               || is_gimple_val (*rhs)))
    2537                 :             :         {
    2538                 :     2689254 :           if (REFERENCE_CLASS_P (*rhs))
    2539                 :     1670827 :             find_interesting_uses_address (data, stmt, rhs);
    2540                 :             :           else
    2541                 :     1018427 :             find_interesting_uses_op (data, *rhs);
    2542                 :             : 
    2543                 :     2689254 :           if (REFERENCE_CLASS_P (*lhs))
    2544                 :      890594 :             find_interesting_uses_address (data, stmt, lhs);
    2545                 :     2689254 :           return;
    2546                 :             :         }
    2547                 :     5046552 :       else if (TREE_CODE_CLASS (code) == tcc_comparison)
    2548                 :             :         {
    2549                 :       86501 :           find_interesting_uses_cond (data, stmt);
    2550                 :       86501 :           return;
    2551                 :             :         }
    2552                 :             : 
    2553                 :             :       /* TODO -- we should also handle address uses of type
    2554                 :             : 
    2555                 :             :          memory = call (whatever);
    2556                 :             : 
    2557                 :             :          and
    2558                 :             : 
    2559                 :             :          call (memory).  */
    2560                 :             :     }
    2561                 :             : 
    2562                 :     8065826 :   if (gimple_code (stmt) == GIMPLE_PHI
    2563                 :     8065826 :       && gimple_bb (stmt) == data->current_loop->header)
    2564                 :             :     {
    2565                 :     1238325 :       iv = get_iv (data, PHI_RESULT (stmt));
    2566                 :             : 
    2567                 :     1238325 :       if (iv && !integer_zerop (iv->step))
    2568                 :             :         return;
    2569                 :             :     }
    2570                 :             : 
    2571                 :    24513203 :   FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
    2572                 :             :     {
    2573                 :     9852433 :       op = USE_FROM_PTR (use_p);
    2574                 :             : 
    2575                 :     9852433 :       if (TREE_CODE (op) != SSA_NAME)
    2576                 :      442790 :         continue;
    2577                 :             : 
    2578                 :     9409643 :       iv = get_iv (data, op);
    2579                 :     9409643 :       if (!iv)
    2580                 :     7820245 :         continue;
    2581                 :             : 
    2582                 :     1589398 :       if (!find_address_like_use (data, stmt, use_p->use, iv))
    2583                 :     1589004 :         find_interesting_uses_op (data, op);
    2584                 :             :     }
    2585                 :             : }
    2586                 :             : 
    2587                 :             : /* Finds interesting uses of induction variables outside of loops
    2588                 :             :    on loop exit edge EXIT.  */
    2589                 :             : 
    2590                 :             : static void
    2591                 :      822558 : find_interesting_uses_outside (struct ivopts_data *data, edge exit)
    2592                 :             : {
    2593                 :      822558 :   gphi *phi;
    2594                 :      822558 :   gphi_iterator psi;
    2595                 :      822558 :   tree def;
    2596                 :             : 
    2597                 :     1778351 :   for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); gsi_next (&psi))
    2598                 :             :     {
    2599                 :      955793 :       phi = psi.phi ();
    2600                 :      955793 :       def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
    2601                 :     1876809 :       if (!virtual_operand_p (def))
    2602                 :      439068 :         find_interesting_uses_op (data, def);
    2603                 :             :     }
    2604                 :      822558 : }
    2605                 :             : 
    2606                 :             : /* Return TRUE if OFFSET is within the range of [base + offset] addressing
    2607                 :             :    mode for memory reference represented by USE.  */
    2608                 :             : 
    2609                 :             : static GTY (()) vec<rtx, va_gc> *addr_list;
    2610                 :             : 
    2611                 :             : static bool
    2612                 :      190707 : addr_offset_valid_p (struct iv_use *use, poly_int64 offset)
    2613                 :             : {
    2614                 :      190707 :   rtx reg, addr;
    2615                 :      190707 :   unsigned list_index;
    2616                 :      190707 :   addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (use->iv->base));
    2617                 :      190707 :   machine_mode addr_mode, mem_mode = TYPE_MODE (use->mem_type);
    2618                 :             : 
    2619                 :      190707 :   list_index = (unsigned) as * MAX_MACHINE_MODE + (unsigned) mem_mode;
    2620                 :      372247 :   if (list_index >= vec_safe_length (addr_list))
    2621                 :        9167 :     vec_safe_grow_cleared (addr_list, list_index + MAX_MACHINE_MODE, true);
    2622                 :             : 
    2623                 :      190707 :   addr = (*addr_list)[list_index];
    2624                 :      190707 :   if (!addr)
    2625                 :             :     {
    2626                 :       11470 :       addr_mode = targetm.addr_space.address_mode (as);
    2627                 :       11470 :       reg = gen_raw_REG (addr_mode, LAST_VIRTUAL_REGISTER + 1);
    2628                 :       11470 :       addr = gen_rtx_fmt_ee (PLUS, addr_mode, reg, NULL_RTX);
    2629                 :       11470 :       (*addr_list)[list_index] = addr;
    2630                 :             :     }
    2631                 :             :   else
    2632                 :      179237 :     addr_mode = GET_MODE (addr);
    2633                 :             : 
    2634                 :      190707 :   XEXP (addr, 1) = gen_int_mode (offset, addr_mode);
    2635                 :      190707 :   return (memory_address_addr_space_p (mem_mode, addr, as));
    2636                 :             : }
    2637                 :             : 
    2638                 :             : /* Comparison function to sort group in ascending order of addr_offset.  */
    2639                 :             : 
    2640                 :             : static int
    2641                 :     3104125 : group_compare_offset (const void *a, const void *b)
    2642                 :             : {
    2643                 :     3104125 :   const struct iv_use *const *u1 = (const struct iv_use *const *) a;
    2644                 :     3104125 :   const struct iv_use *const *u2 = (const struct iv_use *const *) b;
    2645                 :             : 
    2646                 :     3104125 :   return compare_sizes_for_sort ((*u1)->addr_offset, (*u2)->addr_offset);
    2647                 :             : }
    2648                 :             : 
    2649                 :             : /* Check if small groups should be split.  Return true if no group
    2650                 :             :    contains more than two uses with distinct addr_offsets.  Return
    2651                 :             :    false otherwise.  We want to split such groups because:
    2652                 :             : 
    2653                 :             :      1) Small groups don't have much benefit and may interfer with
    2654                 :             :         general candidate selection.
    2655                 :             :      2) Size for problem with only small groups is usually small and
    2656                 :             :         general algorithm can handle it well.
    2657                 :             : 
    2658                 :             :    TODO -- Above claim may not hold when we want to merge memory
    2659                 :             :    accesses with conseuctive addresses.  */
    2660                 :             : 
    2661                 :             : static bool
    2662                 :      427981 : split_small_address_groups_p (struct ivopts_data *data)
    2663                 :             : {
    2664                 :      427981 :   unsigned int i, j, distinct = 1;
    2665                 :      427981 :   struct iv_use *pre;
    2666                 :      427981 :   struct iv_group *group;
    2667                 :             : 
    2668                 :     3554444 :   for (i = 0; i < data->vgroups.length (); i++)
    2669                 :             :     {
    2670                 :     1349241 :       group = data->vgroups[i];
    2671                 :     1349241 :       if (group->vuses.length () == 1)
    2672                 :     1232166 :         continue;
    2673                 :             : 
    2674                 :      117075 :       gcc_assert (address_p (group->type));
    2675                 :      117075 :       if (group->vuses.length () == 2)
    2676                 :             :         {
    2677                 :       58553 :           if (compare_sizes_for_sort (group->vuses[0]->addr_offset,
    2678                 :       58553 :                                       group->vuses[1]->addr_offset) > 0)
    2679                 :        8670 :             std::swap (group->vuses[0], group->vuses[1]);
    2680                 :             :         }
    2681                 :             :       else
    2682                 :       58522 :         group->vuses.qsort (group_compare_offset);
    2683                 :             : 
    2684                 :      117075 :       if (distinct > 2)
    2685                 :       11577 :         continue;
    2686                 :             : 
    2687                 :      105498 :       distinct = 1;
    2688                 :      251646 :       for (pre = group->vuses[0], j = 1; j < group->vuses.length (); j++)
    2689                 :             :         {
    2690                 :      178467 :           if (maybe_ne (group->vuses[j]->addr_offset, pre->addr_offset))
    2691                 :             :             {
    2692                 :      109900 :               pre = group->vuses[j];
    2693                 :      109900 :               distinct++;
    2694                 :             :             }
    2695                 :             : 
    2696                 :      178467 :           if (distinct > 2)
    2697                 :             :             break;
    2698                 :             :         }
    2699                 :             :     }
    2700                 :             : 
    2701                 :      427981 :   return (distinct <= 2);
    2702                 :             : }
    2703                 :             : 
    2704                 :             : /* For each group of address type uses, this function further groups
    2705                 :             :    these uses according to the maximum offset supported by target's
    2706                 :             :    [base + offset] addressing mode.  */
    2707                 :             : 
    2708                 :             : static void
    2709                 :      427981 : split_address_groups (struct ivopts_data *data)
    2710                 :             : {
    2711                 :      427981 :   unsigned int i, j;
    2712                 :             :   /* Always split group.  */
    2713                 :      427981 :   bool split_p = split_small_address_groups_p (data);
    2714                 :             : 
    2715                 :     3642628 :   for (i = 0; i < data->vgroups.length (); i++)
    2716                 :             :     {
    2717                 :     1393333 :       struct iv_group *new_group = NULL;
    2718                 :     1393333 :       struct iv_group *group = data->vgroups[i];
    2719                 :     1393333 :       struct iv_use *use = group->vuses[0];
    2720                 :             : 
    2721                 :     1393333 :       use->id = 0;
    2722                 :     1393333 :       use->group_id = group->id;
    2723                 :     1393333 :       if (group->vuses.length () == 1)
    2724                 :     1271349 :         continue;
    2725                 :             : 
    2726                 :      121984 :       gcc_assert (address_p (use->type));
    2727                 :             : 
    2728                 :      865676 :       for (j = 1; j < group->vuses.length ();)
    2729                 :             :         {
    2730                 :      310854 :           struct iv_use *next = group->vuses[j];
    2731                 :      310854 :           poly_int64 offset = next->addr_offset - use->addr_offset;
    2732                 :             : 
    2733                 :             :           /* Split group if aksed to, or the offset against the first
    2734                 :             :              use can't fit in offset part of addressing mode.  IV uses
    2735                 :             :              having the same offset are still kept in one group.  */
    2736                 :      360284 :           if (maybe_ne (offset, 0)
    2737                 :      310854 :               && (split_p || !addr_offset_valid_p (use, offset)))
    2738                 :             :             {
    2739                 :       49430 :               if (!new_group)
    2740                 :       44092 :                 new_group = record_group (data, group->type);
    2741                 :       49430 :               group->vuses.ordered_remove (j);
    2742                 :       49430 :               new_group->vuses.safe_push (next);
    2743                 :       49430 :               continue;
    2744                 :             :             }
    2745                 :             : 
    2746                 :      261424 :           next->id = j;
    2747                 :      261424 :           next->group_id = group->id;
    2748                 :      261424 :           j++;
    2749                 :             :         }
    2750                 :             :     }
    2751                 :      427981 : }
    2752                 :             : 
    2753                 :             : /* Finds uses of the induction variables that are interesting.  */
    2754                 :             : 
    2755                 :             : static void
    2756                 :      427981 : find_interesting_uses (struct ivopts_data *data, basic_block *body)
    2757                 :             : {
    2758                 :      427981 :   basic_block bb;
    2759                 :      427981 :   gimple_stmt_iterator bsi;
    2760                 :      427981 :   unsigned i;
    2761                 :      427981 :   edge e;
    2762                 :             : 
    2763                 :     2937858 :   for (i = 0; i < data->current_loop->num_nodes; i++)
    2764                 :             :     {
    2765                 :     2509877 :       edge_iterator ei;
    2766                 :     2509877 :       bb = body[i];
    2767                 :             : 
    2768                 :     6397245 :       FOR_EACH_EDGE (e, ei, bb->succs)
    2769                 :     3887368 :         if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
    2770                 :     3887368 :             && !flow_bb_inside_loop_p (data->current_loop, e->dest))
    2771                 :      822558 :           find_interesting_uses_outside (data, e);
    2772                 :             : 
    2773                 :     5078198 :       for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
    2774                 :     2568321 :         find_interesting_uses_stmt (data, gsi_stmt (bsi));
    2775                 :    24105984 :       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
    2776                 :    19086230 :         if (!is_gimple_debug (gsi_stmt (bsi)))
    2777                 :    11562982 :           find_interesting_uses_stmt (data, gsi_stmt (bsi));
    2778                 :             :     }
    2779                 :             : 
    2780                 :      427981 :   split_address_groups (data);
    2781                 :             : 
    2782                 :      427981 :   if (dump_file && (dump_flags & TDF_DETAILS))
    2783                 :             :     {
    2784                 :          76 :       fprintf (dump_file, "\n<IV Groups>:\n");
    2785                 :          76 :       dump_groups (dump_file, data);
    2786                 :          76 :       fprintf (dump_file, "\n");
    2787                 :             :     }
    2788                 :      427981 : }
    2789                 :             : 
    2790                 :             : /* Strips constant offsets from EXPR and stores them to OFFSET.  If INSIDE_ADDR
    2791                 :             :    is true, assume we are inside an address.  If TOP_COMPREF is true, assume
    2792                 :             :    we are at the top-level of the processed address.  */
    2793                 :             : 
    2794                 :             : static tree
    2795                 :     2879906 : strip_offset_1 (tree expr, bool inside_addr, bool top_compref,
    2796                 :             :                 poly_int64 *offset)
    2797                 :             : {
    2798                 :     2879906 :   tree op0 = NULL_TREE, op1 = NULL_TREE, tmp, step;
    2799                 :     2879906 :   enum tree_code code;
    2800                 :     2879906 :   tree type, orig_type = TREE_TYPE (expr);
    2801                 :     2879906 :   poly_int64 off0, off1;
    2802                 :     2879906 :   HOST_WIDE_INT st;
    2803                 :     2879906 :   tree orig_expr = expr;
    2804                 :             : 
    2805                 :     2879906 :   STRIP_NOPS (expr);
    2806                 :             : 
    2807                 :     2879906 :   type = TREE_TYPE (expr);
    2808                 :     2879906 :   code = TREE_CODE (expr);
    2809                 :     2879906 :   *offset = 0;
    2810                 :             : 
    2811                 :     2879906 :   switch (code)
    2812                 :             :     {
    2813                 :      528482 :     case POINTER_PLUS_EXPR:
    2814                 :      528482 :     case PLUS_EXPR:
    2815                 :      528482 :     case MINUS_EXPR:
    2816                 :      528482 :       op0 = TREE_OPERAND (expr, 0);
    2817                 :      528482 :       op1 = TREE_OPERAND (expr, 1);
    2818                 :             : 
    2819                 :      528482 :       op0 = strip_offset_1 (op0, false, false, &off0);
    2820                 :      528482 :       op1 = strip_offset_1 (op1, false, false, &off1);
    2821                 :             : 
    2822                 :      528482 :       *offset = (code == MINUS_EXPR ? off0 - off1 : off0 + off1);
    2823                 :      528482 :       if (op0 == TREE_OPERAND (expr, 0)
    2824                 :      528482 :           && op1 == TREE_OPERAND (expr, 1))
    2825                 :             :         return orig_expr;
    2826                 :             : 
    2827                 :      367725 :       if (integer_zerop (op1))
    2828                 :             :         expr = op0;
    2829                 :       48771 :       else if (integer_zerop (op0))
    2830                 :             :         {
    2831                 :        1128 :           if (code == MINUS_EXPR)
    2832                 :             :             {
    2833                 :        1128 :               if (TYPE_OVERFLOW_UNDEFINED (type))
    2834                 :             :                 {
    2835                 :         226 :                   type = unsigned_type_for (type);
    2836                 :         226 :                   op1 = fold_convert (type, op1);
    2837                 :             :                 }
    2838                 :        1128 :               expr = fold_build1 (NEGATE_EXPR, type, op1);
    2839                 :             :             }
    2840                 :             :           else
    2841                 :             :             expr = op1;
    2842                 :             :         }
    2843                 :             :       else
    2844                 :             :         {
    2845                 :       47643 :           if (TYPE_OVERFLOW_UNDEFINED (type))
    2846                 :             :             {
    2847                 :       44938 :               type = unsigned_type_for (type);
    2848                 :       44938 :               if (code == POINTER_PLUS_EXPR)
    2849                 :       42541 :                 code = PLUS_EXPR;
    2850                 :       44938 :               op0 = fold_convert (type, op0);
    2851                 :       44938 :               op1 = fold_convert (type, op1);
    2852                 :             :             }
    2853                 :       47643 :           expr = fold_build2 (code, type, op0, op1);
    2854                 :             :         }
    2855                 :             : 
    2856                 :      367725 :       return fold_convert (orig_type, expr);
    2857                 :             : 
    2858                 :      190286 :     case MULT_EXPR:
    2859                 :      190286 :       op1 = TREE_OPERAND (expr, 1);
    2860                 :      190286 :       if (!cst_and_fits_in_hwi (op1))
    2861                 :             :         return orig_expr;
    2862                 :             : 
    2863                 :      154341 :       op0 = TREE_OPERAND (expr, 0);
    2864                 :      154341 :       op0 = strip_offset_1 (op0, false, false, &off0);
    2865                 :      154341 :       if (op0 == TREE_OPERAND (expr, 0))
    2866                 :             :         return orig_expr;
    2867                 :             : 
    2868                 :       28094 :       *offset = off0 * int_cst_value (op1);
    2869                 :       28094 :       if (integer_zerop (op0))
    2870                 :             :         expr = op0;
    2871                 :             :       else
    2872                 :             :         {
    2873                 :       28094 :           if (TYPE_OVERFLOW_UNDEFINED (type))
    2874                 :             :             {
    2875                 :        3759 :               type = unsigned_type_for (type);
    2876                 :        3759 :               op0 = fold_convert (type, op0);
    2877                 :        3759 :               op1 = fold_convert (type, op1);
    2878                 :             :             }
    2879                 :       28094 :           expr = fold_build2 (MULT_EXPR, type, op0, op1);
    2880                 :             :         }
    2881                 :             : 
    2882                 :       28094 :       return fold_convert (orig_type, expr);
    2883                 :             : 
    2884                 :           0 :     case ARRAY_REF:
    2885                 :           0 :     case ARRAY_RANGE_REF:
    2886                 :           0 :       if (!inside_addr)
    2887                 :             :         return orig_expr;
    2888                 :             : 
    2889                 :           0 :       step = array_ref_element_size (expr);
    2890                 :           0 :       if (!cst_and_fits_in_hwi (step))
    2891                 :             :         break;
    2892                 :             : 
    2893                 :           0 :       st = int_cst_value (step);
    2894                 :           0 :       op1 = TREE_OPERAND (expr, 1);
    2895                 :           0 :       op1 = strip_offset_1 (op1, false, false, &off1);
    2896                 :           0 :       *offset = off1 * st;
    2897                 :             : 
    2898                 :           0 :       if (top_compref
    2899                 :           0 :           && integer_zerop (op1))
    2900                 :             :         {
    2901                 :             :           /* Strip the component reference completely.  */
    2902                 :           0 :           op0 = TREE_OPERAND (expr, 0);
    2903                 :           0 :           op0 = strip_offset_1 (op0, inside_addr, top_compref, &off0);
    2904                 :           0 :           *offset += off0;
    2905                 :           0 :           return op0;
    2906                 :             :         }
    2907                 :             :       break;
    2908                 :             : 
    2909                 :           0 :     case COMPONENT_REF:
    2910                 :           0 :       {
    2911                 :           0 :         tree field;
    2912                 :             : 
    2913                 :           0 :         if (!inside_addr)
    2914                 :             :           return orig_expr;
    2915                 :             : 
    2916                 :           0 :         tmp = component_ref_field_offset (expr);
    2917                 :           0 :         field = TREE_OPERAND (expr, 1);
    2918                 :           0 :         if (top_compref
    2919                 :           0 :             && cst_and_fits_in_hwi (tmp)
    2920                 :           0 :             && cst_and_fits_in_hwi (DECL_FIELD_BIT_OFFSET (field)))
    2921                 :             :           {
    2922                 :           0 :             HOST_WIDE_INT boffset, abs_off;
    2923                 :             : 
    2924                 :             :             /* Strip the component reference completely.  */
    2925                 :           0 :             op0 = TREE_OPERAND (expr, 0);
    2926                 :           0 :             op0 = strip_offset_1 (op0, inside_addr, top_compref, &off0);
    2927                 :           0 :             boffset = int_cst_value (DECL_FIELD_BIT_OFFSET (field));
    2928                 :           0 :             abs_off = abs_hwi (boffset) / BITS_PER_UNIT;
    2929                 :           0 :             if (boffset < 0)
    2930                 :           0 :               abs_off = -abs_off;
    2931                 :             : 
    2932                 :           0 :             *offset = off0 + int_cst_value (tmp) + abs_off;
    2933                 :           0 :             return op0;
    2934                 :             :           }
    2935                 :             :       }
    2936                 :             :       break;
    2937                 :             : 
    2938                 :      275435 :     case ADDR_EXPR:
    2939                 :      275435 :       op0 = TREE_OPERAND (expr, 0);
    2940                 :      275435 :       op0 = strip_offset_1 (op0, true, true, &off0);
    2941                 :      275435 :       *offset += off0;
    2942                 :             : 
    2943                 :      275435 :       if (op0 == TREE_OPERAND (expr, 0))
    2944                 :             :         return orig_expr;
    2945                 :             : 
    2946                 :           0 :       expr = build_fold_addr_expr (op0);
    2947                 :           0 :       return fold_convert (orig_type, expr);
    2948                 :             : 
    2949                 :             :     case MEM_REF:
    2950                 :             :       /* ???  Offset operand?  */
    2951                 :             :       inside_addr = false;
    2952                 :             :       break;
    2953                 :             : 
    2954                 :     1885703 :     default:
    2955                 :     1885703 :       if (ptrdiff_tree_p (expr, offset) && maybe_ne (*offset, 0))
    2956                 :      737094 :         return build_int_cst (orig_type, 0);
    2957                 :             :       return orig_expr;
    2958                 :             :     }
    2959                 :             : 
    2960                 :             :   /* Default handling of expressions for that we want to recurse into
    2961                 :             :      the first operand.  */
    2962                 :           0 :   op0 = TREE_OPERAND (expr, 0);
    2963                 :           0 :   op0 = strip_offset_1 (op0, inside_addr, false, &off0);
    2964                 :           0 :   *offset += off0;
    2965                 :             : 
    2966                 :           0 :   if (op0 == TREE_OPERAND (expr, 0)
    2967                 :           0 :       && (!op1 || op1 == TREE_OPERAND (expr, 1)))
    2968                 :             :     return orig_expr;
    2969                 :             : 
    2970                 :           0 :   expr = copy_node (expr);
    2971                 :           0 :   TREE_OPERAND (expr, 0) = op0;
    2972                 :           0 :   if (op1)
    2973                 :           0 :     TREE_OPERAND (expr, 1) = op1;
    2974                 :             : 
    2975                 :             :   /* Inside address, we might strip the top level component references,
    2976                 :             :      thus changing type of the expression.  Handling of ADDR_EXPR
    2977                 :             :      will fix that.  */
    2978                 :           0 :   expr = fold_convert (orig_type, expr);
    2979                 :             : 
    2980                 :           0 :   return expr;
    2981                 :             : }
    2982                 :             : 
    2983                 :             : /* Strips constant offsets from EXPR and stores them to OFFSET.  */
    2984                 :             : 
    2985                 :             : static tree
    2986                 :     1393166 : strip_offset (tree expr, poly_uint64 *offset)
    2987                 :             : {
    2988                 :     1393166 :   poly_int64 off;
    2989                 :     1393166 :   tree core = strip_offset_1 (expr, false, false, &off);
    2990                 :     1393166 :   *offset = off;
    2991                 :     1393166 :   return core;
    2992                 :             : }
    2993                 :             : 
    2994                 :             : /* Returns variant of TYPE that can be used as base for different uses.
    2995                 :             :    We return unsigned type with the same precision, which avoids problems
    2996                 :             :    with overflows.  */
    2997                 :             : 
    2998                 :             : static tree
    2999                 :     6865899 : generic_type_for (tree type)
    3000                 :             : {
    3001                 :     6865899 :   if (POINTER_TYPE_P (type))
    3002                 :     1139081 :     return unsigned_type_for (type);
    3003                 :             : 
    3004                 :     5726818 :   if (TYPE_UNSIGNED (type))
    3005                 :             :     return type;
    3006                 :             : 
    3007                 :     2698123 :   return unsigned_type_for (type);
    3008                 :             : }
    3009                 :             : 
    3010                 :             : /* Private data for walk_tree.  */
    3011                 :             : 
    3012                 :             : struct walk_tree_data
    3013                 :             : {
    3014                 :             :   bitmap *inv_vars;
    3015                 :             :   struct ivopts_data *idata;
    3016                 :             : };
    3017                 :             : 
    3018                 :             : /* Callback function for walk_tree, it records invariants and symbol
    3019                 :             :    reference in *EXPR_P.  DATA is the structure storing result info.  */
    3020                 :             : 
    3021                 :             : static tree
    3022                 :    28747476 : find_inv_vars_cb (tree *expr_p, int *ws ATTRIBUTE_UNUSED, void *data)
    3023                 :             : {
    3024                 :    28747476 :   tree op = *expr_p;
    3025                 :    28747476 :   struct version_info *info;
    3026                 :    28747476 :   struct walk_tree_data *wdata = (struct walk_tree_data*) data;
    3027                 :             : 
    3028                 :    28747476 :   if (TREE_CODE (op) != SSA_NAME)
    3029                 :             :     return NULL_TREE;
    3030                 :             : 
    3031                 :     6754024 :   info = name_info (wdata->idata, op);
    3032                 :             :   /* Because we expand simple operations when finding IVs, loop invariant
    3033                 :             :      variable that isn't referred by the original loop could be used now.
    3034                 :             :      Record such invariant variables here.  */
    3035                 :     6754024 :   if (!info->iv)
    3036                 :             :     {
    3037                 :      326850 :       struct ivopts_data *idata = wdata->idata;
    3038                 :      326850 :       basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (op));
    3039                 :             : 
    3040                 :      326850 :       if (!bb || !flow_bb_inside_loop_p (idata->current_loop, bb))
    3041                 :             :         {
    3042                 :      326850 :           tree steptype = TREE_TYPE (op);
    3043                 :      326850 :           if (POINTER_TYPE_P (steptype))
    3044                 :      159116 :             steptype = sizetype;
    3045                 :      326850 :           set_iv (idata, op, op, build_int_cst (steptype, 0), true);
    3046                 :      326850 :           record_invariant (idata, op, false);
    3047                 :             :         }
    3048                 :             :     }
    3049                 :     6754024 :   if (!info->inv_id || info->has_nonlin_use)
    3050                 :             :     return NULL_TREE;
    3051                 :             : 
    3052                 :     5654836 :   if (!*wdata->inv_vars)
    3053                 :     4445790 :     *wdata->inv_vars = BITMAP_ALLOC (NULL);
    3054                 :     5654836 :   bitmap_set_bit (*wdata->inv_vars, info->inv_id);
    3055                 :             : 
    3056                 :     5654836 :   return NULL_TREE;
    3057                 :             : }
    3058                 :             : 
    3059                 :             : /* Records invariants in *EXPR_P.  INV_VARS is the bitmap to that we should
    3060                 :             :    store it.  */
    3061                 :             : 
    3062                 :             : static inline void
    3063                 :    24071309 : find_inv_vars (struct ivopts_data *data, tree *expr_p, bitmap *inv_vars)
    3064                 :             : {
    3065                 :    24071309 :   struct walk_tree_data wdata;
    3066                 :             : 
    3067                 :    24071309 :   if (!inv_vars)
    3068                 :    10334738 :     return;
    3069                 :             : 
    3070                 :    13736571 :   wdata.idata = data;
    3071                 :    13736571 :   wdata.inv_vars = inv_vars;
    3072                 :    13736571 :   walk_tree (expr_p, find_inv_vars_cb, &wdata, NULL);
    3073                 :             : }
    3074                 :             : 
    3075                 :             : /* Get entry from invariant expr hash table for INV_EXPR.  New entry
    3076                 :             :    will be recorded if it doesn't exist yet.  Given below two exprs:
    3077                 :             :      inv_expr + cst1, inv_expr + cst2
    3078                 :             :    It's hard to make decision whether constant part should be stripped
    3079                 :             :    or not.  We choose to not strip based on below facts:
    3080                 :             :      1) We need to count ADD cost for constant part if it's stripped,
    3081                 :             :         which isn't always trivial where this functions is called.
    3082                 :             :      2) Stripping constant away may be conflict with following loop
    3083                 :             :         invariant hoisting pass.
    3084                 :             :      3) Not stripping constant away results in more invariant exprs,
    3085                 :             :         which usually leads to decision preferring lower reg pressure.  */
    3086                 :             : 
    3087                 :             : static iv_inv_expr_ent *
    3088                 :     2272226 : get_loop_invariant_expr (struct ivopts_data *data, tree inv_expr)
    3089                 :             : {
    3090                 :     2272226 :   STRIP_NOPS (inv_expr);
    3091                 :             : 
    3092                 :     2272226 :   if (poly_int_tree_p (inv_expr)
    3093                 :     2272226 :       || TREE_CODE (inv_expr) == SSA_NAME)
    3094                 :             :     return NULL;
    3095                 :             : 
    3096                 :             :   /* Don't strip constant part away as we used to.  */
    3097                 :             : 
    3098                 :             :   /* Stores EXPR in DATA->inv_expr_tab, return pointer to iv_inv_expr_ent.  */
    3099                 :     2189780 :   struct iv_inv_expr_ent ent;
    3100                 :     2189780 :   ent.expr = inv_expr;
    3101                 :     2189780 :   ent.hash = iterative_hash_expr (inv_expr, 0);
    3102                 :     2189780 :   struct iv_inv_expr_ent **slot = data->inv_expr_tab->find_slot (&ent, INSERT);
    3103                 :             : 
    3104                 :     2189780 :   if (!*slot)
    3105                 :             :     {
    3106                 :      983366 :       *slot = XNEW (struct iv_inv_expr_ent);
    3107                 :      983366 :       (*slot)->expr = inv_expr;
    3108                 :      983366 :       (*slot)->hash = ent.hash;
    3109                 :      983366 :       (*slot)->id = ++data->max_inv_expr_id;
    3110                 :             :     }
    3111                 :             : 
    3112                 :     2189780 :   return *slot;
    3113                 :             : }
    3114                 :             : 
    3115                 :             : 
    3116                 :             : /* Return *TP if it is an SSA_NAME marked with TREE_VISITED, i.e., as
    3117                 :             :    unsuitable as ivopts candidates for potentially involving undefined
    3118                 :             :    behavior.  */
    3119                 :             : 
    3120                 :             : static tree
    3121                 :    12257926 : find_ssa_undef (tree *tp, int *walk_subtrees, void *bb_)
    3122                 :             : {
    3123                 :    12257926 :   basic_block bb = (basic_block) bb_;
    3124                 :    12257926 :   if (TREE_CODE (*tp) == SSA_NAME
    3125                 :     1859630 :       && ssa_name_maybe_undef_p (*tp)
    3126                 :    12264910 :       && !ssa_name_any_use_dominates_bb_p (*tp, bb))
    3127                 :        2363 :     return *tp;
    3128                 :    12255563 :   if (!EXPR_P (*tp))
    3129                 :     8886103 :     *walk_subtrees = 0;
    3130                 :             :   return NULL;
    3131                 :             : }
    3132                 :             : 
    3133                 :             : /* Adds a candidate BASE + STEP * i.  Important field is set to IMPORTANT and
    3134                 :             :    position to POS.  If USE is not NULL, the candidate is set as related to
    3135                 :             :    it.  If both BASE and STEP are NULL, we add a pseudocandidate for the
    3136                 :             :    replacement of the final value of the iv by a direct computation.  */
    3137                 :             : 
    3138                 :             : static struct iv_cand *
    3139                 :     7704194 : add_candidate_1 (struct ivopts_data *data, tree base, tree step, bool important,
    3140                 :             :                  enum iv_position pos, struct iv_use *use,
    3141                 :             :                  gimple *incremented_at, struct iv *orig_iv = NULL,
    3142                 :             :                  bool doloop = false)
    3143                 :             : {
    3144                 :     7704194 :   unsigned i;
    3145                 :     7704194 :   struct iv_cand *cand = NULL;
    3146                 :     7704194 :   tree type, orig_type;
    3147                 :             : 
    3148                 :     7704194 :   gcc_assert (base && step);
    3149                 :             : 
    3150                 :             :   /* -fkeep-gc-roots-live means that we have to keep a real pointer
    3151                 :             :      live, but the ivopts code may replace a real pointer with one
    3152                 :             :      pointing before or after the memory block that is then adjusted
    3153                 :             :      into the memory block during the loop.  FIXME: It would likely be
    3154                 :             :      better to actually force the pointer live and still use ivopts;
    3155                 :             :      for example, it would be enough to write the pointer into memory
    3156                 :             :      and keep it there until after the loop.  */
    3157                 :     7704194 :   if (flag_keep_gc_roots_live && POINTER_TYPE_P (TREE_TYPE (base)))
    3158                 :             :     return NULL;
    3159                 :             : 
    3160                 :             :   /* If BASE contains undefined SSA names make sure we only record
    3161                 :             :      the original IV.  */
    3162                 :     7599618 :   bool involves_undefs = false;
    3163                 :     7599618 :   if (walk_tree (&base, find_ssa_undef, data->current_loop->header, NULL))
    3164                 :             :     {
    3165                 :        2363 :       if (pos != IP_ORIGINAL)
    3166                 :             :         return NULL;
    3167                 :             :       important = false;
    3168                 :             :       involves_undefs = true;
    3169                 :             :     }
    3170                 :             : 
    3171                 :             :   /* For non-original variables, make sure their values are computed in a type
    3172                 :             :      that does not invoke undefined behavior on overflows (since in general,
    3173                 :             :      we cannot prove that these induction variables are non-wrapping).  */
    3174                 :     7597561 :   if (pos != IP_ORIGINAL)
    3175                 :             :     {
    3176                 :     6865899 :       orig_type = TREE_TYPE (base);
    3177                 :     6865899 :       type = generic_type_for (orig_type);
    3178                 :     6865899 :       if (type != orig_type)
    3179                 :             :         {
    3180                 :     3837204 :           base = fold_convert (type, base);
    3181                 :     3837204 :           step = fold_convert (type, step);
    3182                 :             :         }
    3183                 :             :     }
    3184                 :             : 
    3185                 :    76795826 :   for (i = 0; i < data->vcands.length (); i++)
    3186                 :             :     {
    3187                 :    34416199 :       cand = data->vcands[i];
    3188                 :             : 
    3189                 :    34416199 :       if (cand->pos != pos)
    3190                 :     8276455 :         continue;
    3191                 :             : 
    3192                 :    26139744 :       if (cand->incremented_at != incremented_at
    3193                 :    25746376 :           || ((pos == IP_AFTER_USE || pos == IP_BEFORE_USE)
    3194                 :           0 :               && cand->ainc_use != use))
    3195                 :      393368 :         continue;
    3196                 :             : 
    3197                 :    25746376 :       if (operand_equal_p (base, cand->iv->base, 0)
    3198                 :     7996761 :           && operand_equal_p (step, cand->iv->step, 0)
    3199                 :    30525771 :           && (TYPE_PRECISION (TREE_TYPE (base))
    3200                 :     4779395 :               == TYPE_PRECISION (TREE_TYPE (cand->iv->base))))
    3201                 :             :         break;
    3202                 :             :     }
    3203                 :             : 
    3204                 :    15195122 :   if (i == data->vcands.length ())
    3205                 :             :     {
    3206                 :     3981714 :       cand = XCNEW (struct iv_cand);
    3207                 :     3981714 :       cand->id = i;
    3208                 :     3981714 :       cand->iv = alloc_iv (data, base, step);
    3209                 :     3981714 :       cand->pos = pos;
    3210                 :     3981714 :       if (pos != IP_ORIGINAL)
    3211                 :             :         {
    3212                 :     3250217 :           if (doloop)
    3213                 :           0 :             cand->var_before = create_tmp_var_raw (TREE_TYPE (base), "doloop");
    3214                 :             :           else
    3215                 :     3250217 :             cand->var_before = create_tmp_var_raw (TREE_TYPE (base), "ivtmp");
    3216                 :     3250217 :           cand->var_after = cand->var_before;
    3217                 :             :         }
    3218                 :     3981714 :       cand->important = important;
    3219                 :     3981714 :       cand->involves_undefs = involves_undefs;
    3220                 :     3981714 :       cand->incremented_at = incremented_at;
    3221                 :     3981714 :       cand->doloop_p = doloop;
    3222                 :     3981714 :       data->vcands.safe_push (cand);
    3223                 :             : 
    3224                 :     3981714 :       if (!poly_int_tree_p (step))
    3225                 :             :         {
    3226                 :      176821 :           find_inv_vars (data, &step, &cand->inv_vars);
    3227                 :             : 
    3228                 :      176821 :           iv_inv_expr_ent *inv_expr = get_loop_invariant_expr (data, step);
    3229                 :             :           /* Share bitmap between inv_vars and inv_exprs for cand.  */
    3230                 :      176821 :           if (inv_expr != NULL)
    3231                 :             :             {
    3232                 :      105066 :               cand->inv_exprs = cand->inv_vars;
    3233                 :      105066 :               cand->inv_vars = NULL;
    3234                 :      105066 :               if (cand->inv_exprs)
    3235                 :       84593 :                 bitmap_clear (cand->inv_exprs);
    3236                 :             :               else
    3237                 :       20473 :                 cand->inv_exprs = BITMAP_ALLOC (NULL);
    3238                 :             : 
    3239                 :      105066 :               bitmap_set_bit (cand->inv_exprs, inv_expr->id);
    3240                 :             :             }
    3241                 :             :         }
    3242                 :             : 
    3243                 :     3981714 :       if (pos == IP_AFTER_USE || pos == IP_BEFORE_USE)
    3244                 :           0 :         cand->ainc_use = use;
    3245                 :             :       else
    3246                 :     3981714 :         cand->ainc_use = NULL;
    3247                 :             : 
    3248                 :     3981714 :       cand->orig_iv = orig_iv;
    3249                 :     3981714 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3250                 :         839 :         dump_cand (dump_file, cand);
    3251                 :             :     }
    3252                 :             : 
    3253                 :     7597561 :   cand->important |= important;
    3254                 :     7597561 :   cand->doloop_p |= doloop;
    3255                 :             : 
    3256                 :             :   /* Relate candidate to the group for which it is added.  */
    3257                 :     7597561 :   if (use)
    3258                 :     2102995 :     bitmap_set_bit (data->vgroups[use->group_id]->related_cands, i);
    3259                 :             : 
    3260                 :             :   return cand;
    3261                 :             : }
    3262                 :             : 
    3263                 :             : /* Returns true if incrementing the induction variable at the end of the LOOP
    3264                 :             :    is allowed.
    3265                 :             : 
    3266                 :             :    The purpose is to avoid splitting latch edge with a biv increment, thus
    3267                 :             :    creating a jump, possibly confusing other optimization passes and leaving
    3268                 :             :    less freedom to scheduler.  So we allow IP_END only if IP_NORMAL is not
    3269                 :             :    available (so we do not have a better alternative), or if the latch edge
    3270                 :             :    is already nonempty.  */
    3271                 :             : 
    3272                 :             : static bool
    3273                 :     6773356 : allow_ip_end_pos_p (class loop *loop)
    3274                 :             : {
    3275                 :     6773356 :   if (!ip_normal_pos (loop))
    3276                 :             :     return true;
    3277                 :             : 
    3278                 :     6689852 :   if (!empty_block_p (ip_end_pos (loop)))
    3279                 :             :     return true;
    3280                 :             : 
    3281                 :             :   return false;
    3282                 :             : }
    3283                 :             : 
    3284                 :             : /* If possible, adds autoincrement candidates BASE + STEP * i based on use USE.
    3285                 :             :    Important field is set to IMPORTANT.  */
    3286                 :             : 
    3287                 :             : static void
    3288                 :      494878 : add_autoinc_candidates (struct ivopts_data *data, tree base, tree step,
    3289                 :             :                         bool important, struct iv_use *use)
    3290                 :             : {
    3291                 :      494878 :   basic_block use_bb = gimple_bb (use->stmt);
    3292                 :      494878 :   machine_mode mem_mode;
    3293                 :      494878 :   unsigned HOST_WIDE_INT cstepi;
    3294                 :             : 
    3295                 :             :   /* If we insert the increment in any position other than the standard
    3296                 :             :      ones, we must ensure that it is incremented once per iteration.
    3297                 :             :      It must not be in an inner nested loop, or one side of an if
    3298                 :             :      statement.  */
    3299                 :      494878 :   if (use_bb->loop_father != data->current_loop
    3300                 :      493467 :       || !dominated_by_p (CDI_DOMINATORS, data->current_loop->latch, use_bb)
    3301                 :      471781 :       || stmt_can_throw_internal (cfun, use->stmt)
    3302                 :      960485 :       || !cst_and_fits_in_hwi (step))
    3303                 :       55166 :     return;
    3304                 :             : 
    3305                 :      439712 :   cstepi = int_cst_value (step);
    3306                 :             : 
    3307                 :      439712 :   mem_mode = TYPE_MODE (use->mem_type);
    3308                 :             :   if (((USE_LOAD_PRE_INCREMENT (mem_mode)
    3309                 :             :         || USE_STORE_PRE_INCREMENT (mem_mode))
    3310                 :             :        && known_eq (GET_MODE_SIZE (mem_mode), cstepi))
    3311                 :             :       || ((USE_LOAD_PRE_DECREMENT (mem_mode)
    3312                 :             :            || USE_STORE_PRE_DECREMENT (mem_mode))
    3313                 :             :           && known_eq (GET_MODE_SIZE (mem_mode), -cstepi)))
    3314                 :             :     {
    3315                 :             :       enum tree_code code = MINUS_EXPR;
    3316                 :             :       tree new_base;
    3317                 :             :       tree new_step = step;
    3318                 :             : 
    3319                 :             :       if (POINTER_TYPE_P (TREE_TYPE (base)))
    3320                 :             :         {
    3321                 :             :           new_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
    3322                 :             :           code = POINTER_PLUS_EXPR;
    3323                 :             :         }
    3324                 :             :       else
    3325                 :             :         new_step = fold_convert (TREE_TYPE (base), new_step);
    3326                 :             :       new_base = fold_build2 (code, TREE_TYPE (base), base, new_step);
    3327                 :             :       add_candidate_1 (data, new_base, step, important, IP_BEFORE_USE, use,
    3328                 :             :                        use->stmt);
    3329                 :             :     }
    3330                 :             :   if (((USE_LOAD_POST_INCREMENT (mem_mode)
    3331                 :             :         || USE_STORE_POST_INCREMENT (mem_mode))
    3332                 :             :        && known_eq (GET_MODE_SIZE (mem_mode), cstepi))
    3333                 :             :       || ((USE_LOAD_POST_DECREMENT (mem_mode)
    3334                 :             :            || USE_STORE_POST_DECREMENT (mem_mode))
    3335                 :             :           && known_eq (GET_MODE_SIZE (mem_mode), -cstepi)))
    3336                 :             :     {
    3337                 :             :       add_candidate_1 (data, base, step, important, IP_AFTER_USE, use,
    3338                 :             :                        use->stmt);
    3339                 :             :     }
    3340                 :             : }
    3341                 :             : 
    3342                 :             : /* Adds a candidate BASE + STEP * i.  Important field is set to IMPORTANT and
    3343                 :             :    position to POS.  If USE is not NULL, the candidate is set as related to
    3344                 :             :    it.  The candidate computation is scheduled before exit condition and at
    3345                 :             :    the end of loop.  */
    3346                 :             : 
    3347                 :             : static void
    3348                 :     5901686 : add_candidate (struct ivopts_data *data, tree base, tree step, bool important,
    3349                 :             :                struct iv_use *use, struct iv *orig_iv = NULL,
    3350                 :             :                bool doloop = false)
    3351                 :             : {
    3352                 :     5901686 :   if (ip_normal_pos (data->current_loop))
    3353                 :     5831265 :     add_candidate_1 (data, base, step, important, IP_NORMAL, use, NULL, orig_iv,
    3354                 :             :                      doloop);
    3355                 :             :   /* Exclude doloop candidate here since it requires decrement then comparison
    3356                 :             :      and jump, the IP_END position doesn't match.  */
    3357                 :     5901686 :   if (!doloop && ip_end_pos (data->current_loop)
    3358                 :    11803372 :       && allow_ip_end_pos_p (data->current_loop))
    3359                 :      238769 :     add_candidate_1 (data, base, step, important, IP_END, use, NULL, orig_iv);
    3360                 :     5901686 : }
    3361                 :             : 
    3362                 :             : /* Adds standard iv candidates.  */
    3363                 :             : 
    3364                 :             : static void
    3365                 :      427980 : add_standard_iv_candidates (struct ivopts_data *data)
    3366                 :             : {
    3367                 :      427980 :   add_candidate (data, integer_zero_node, integer_one_node, true, NULL);
    3368                 :             : 
    3369                 :             :   /* The same for a double-integer type if it is still fast enough.  */
    3370                 :      427980 :   if (TYPE_PRECISION
    3371                 :      427980 :         (long_integer_type_node) > TYPE_PRECISION (integer_type_node)
    3372                 :      427980 :       && TYPE_PRECISION (long_integer_type_node) <= BITS_PER_WORD)
    3373                 :      384935 :     add_candidate (data, build_int_cst (long_integer_type_node, 0),
    3374                 :      384935 :                    build_int_cst (long_integer_type_node, 1), true, NULL);
    3375                 :             : 
    3376                 :             :   /* The same for a double-integer type if it is still fast enough.  */
    3377                 :      427980 :   if (TYPE_PRECISION
    3378                 :      427980 :         (long_long_integer_type_node) > TYPE_PRECISION (long_integer_type_node)
    3379                 :      471014 :       && TYPE_PRECISION (long_long_integer_type_node) <= BITS_PER_WORD)
    3380                 :          11 :     add_candidate (data, build_int_cst (long_long_integer_type_node, 0),
    3381                 :          11 :                    build_int_cst (long_long_integer_type_node, 1), true, NULL);
    3382                 :      427980 : }
    3383                 :             : 
    3384                 :             : 
    3385                 :             : /* Adds candidates bases on the old induction variable IV.  */
    3386                 :             : 
    3387                 :             : static void
    3388                 :     1467355 : add_iv_candidate_for_biv (struct ivopts_data *data, struct iv *iv)
    3389                 :             : {
    3390                 :     1467355 :   gimple *phi;
    3391                 :     1467355 :   tree def;
    3392                 :     1467355 :   struct iv_cand *cand;
    3393                 :             : 
    3394                 :             :   /* Check if this biv is used in address type use.  */
    3395                 :      963111 :   if (iv->no_overflow  && iv->have_address_use
    3396                 :      460607 :       && INTEGRAL_TYPE_P (TREE_TYPE (iv->base))
    3397                 :     1927962 :       && TYPE_PRECISION (TREE_TYPE (iv->base)) < TYPE_PRECISION (sizetype))
    3398                 :             :     {
    3399                 :      265730 :       tree base = fold_convert (sizetype, iv->base);
    3400                 :      265730 :       tree step = fold_convert (sizetype, iv->step);
    3401                 :             : 
    3402                 :             :       /* Add iv cand of same precision as index part in TARGET_MEM_REF.  */
    3403                 :      265730 :       add_candidate (data, base, step, true, NULL, iv);
    3404                 :             :       /* Add iv cand of the original type only if it has nonlinear use.  */
    3405                 :      265730 :       if (iv->nonlin_use)
    3406                 :       25881 :         add_candidate (data, iv->base, iv->step, true, NULL);
    3407                 :             :     }
    3408                 :             :   else
    3409                 :     1201625 :     add_candidate (data, iv->base, iv->step, true, NULL);
    3410                 :             : 
    3411                 :             :   /* The same, but with initial value zero.  */
    3412                 :     1467355 :   if (POINTER_TYPE_P (TREE_TYPE (iv->base)))
    3413                 :      247734 :     add_candidate (data, size_int (0), iv->step, true, NULL);
    3414                 :             :   else
    3415                 :     1219621 :     add_candidate (data, build_int_cst (TREE_TYPE (iv->base), 0),
    3416                 :             :                    iv->step, true, NULL);
    3417                 :             : 
    3418                 :     1467355 :   phi = SSA_NAME_DEF_STMT (iv->ssa_name);
    3419                 :     1467355 :   if (gimple_code (phi) == GIMPLE_PHI)
    3420                 :             :     {
    3421                 :             :       /* Additionally record the possibility of leaving the original iv
    3422                 :             :          untouched.  */
    3423                 :      733760 :       def = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (data->current_loop));
    3424                 :             :       /* Don't add candidate if it's from another PHI node because
    3425                 :             :          it's an affine iv appearing in the form of PEELED_CHREC.  */
    3426                 :      733760 :       phi = SSA_NAME_DEF_STMT (def);
    3427                 :      733760 :       if (gimple_code (phi) != GIMPLE_PHI)
    3428                 :             :         {
    3429                 :     1467520 :           cand = add_candidate_1 (data,
    3430                 :             :                                   iv->base, iv->step, true, IP_ORIGINAL, NULL,
    3431                 :      733760 :                                   SSA_NAME_DEF_STMT (def));
    3432                 :      733760 :           if (cand)
    3433                 :             :             {
    3434                 :      731662 :               cand->var_before = iv->ssa_name;
    3435                 :      731662 :               cand->var_after = def;
    3436                 :             :             }
    3437                 :             :         }
    3438                 :             :       else
    3439                 :           0 :         gcc_assert (gimple_bb (phi) == data->current_loop->header);
    3440                 :             :     }
    3441                 :     1467355 : }
    3442                 :             : 
    3443                 :             : /* Adds candidates based on the old induction variables.  */
    3444                 :             : 
    3445                 :             : static void
    3446                 :      427980 : add_iv_candidate_for_bivs (struct ivopts_data *data)
    3447                 :             : {
    3448                 :      427980 :   unsigned i;
    3449                 :      427980 :   struct iv *iv;
    3450                 :      427980 :   bitmap_iterator bi;
    3451                 :             : 
    3452                 :     4713691 :   EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
    3453                 :             :     {
    3454                 :     4285711 :       iv = ver_info (data, i)->iv;
    3455                 :     4285711 :       if (iv && iv->biv_p && !integer_zerop (iv->step))
    3456                 :     1467355 :         add_iv_candidate_for_biv (data, iv);
    3457                 :             :     }
    3458                 :      427980 : }
    3459                 :             : 
    3460                 :             : /* Record common candidate {BASE, STEP} derived from USE in hashtable.  */
    3461                 :             : 
    3462                 :             : static void
    3463                 :     4008035 : record_common_cand (struct ivopts_data *data, tree base,
    3464                 :             :                     tree step, struct iv_use *use)
    3465                 :             : {
    3466                 :     4008035 :   class iv_common_cand ent;
    3467                 :     4008035 :   class iv_common_cand **slot;
    3468                 :             : 
    3469                 :     4008035 :   ent.base = base;
    3470                 :     4008035 :   ent.step = step;
    3471                 :     4008035 :   ent.hash = iterative_hash_expr (base, 0);
    3472                 :     4008035 :   ent.hash = iterative_hash_expr (step, ent.hash);
    3473                 :             : 
    3474                 :     4008035 :   slot = data->iv_common_cand_tab->find_slot (&ent, INSERT);
    3475                 :     4008035 :   if (*slot == NULL)
    3476                 :             :     {
    3477                 :     2479757 :       *slot = new iv_common_cand ();
    3478                 :     2479757 :       (*slot)->base = base;
    3479                 :     2479757 :       (*slot)->step = step;
    3480                 :     2479757 :       (*slot)->uses.create (8);
    3481                 :     2479757 :       (*slot)->hash = ent.hash;
    3482                 :     2479757 :       data->iv_common_cands.safe_push ((*slot));
    3483                 :             :     }
    3484                 :             : 
    3485                 :     4008035 :   gcc_assert (use != NULL);
    3486                 :     4008035 :   (*slot)->uses.safe_push (use);
    3487                 :     4008035 :   return;
    3488                 :     4008035 : }
    3489                 :             : 
    3490                 :             : /* Comparison function used to sort common candidates.  */
    3491                 :             : 
    3492                 :             : static int
    3493                 :    20908498 : common_cand_cmp (const void *p1, const void *p2)
    3494                 :             : {
    3495                 :    20908498 :   unsigned n1, n2;
    3496                 :    20908498 :   const class iv_common_cand *const *const ccand1
    3497                 :             :     = (const class iv_common_cand *const *)p1;
    3498                 :    20908498 :   const class iv_common_cand *const *const ccand2
    3499                 :             :     = (const class iv_common_cand *const *)p2;
    3500                 :             : 
    3501                 :    20908498 :   n1 = (*ccand1)->uses.length ();
    3502                 :    20908498 :   n2 = (*ccand2)->uses.length ();
    3503                 :    20908498 :   return n2 - n1;
    3504                 :             : }
    3505                 :             : 
    3506                 :             : /* Adds IV candidates based on common candidated recorded.  */
    3507                 :             : 
    3508                 :             : static void
    3509                 :      427980 : add_iv_candidate_derived_from_uses (struct ivopts_data *data)
    3510                 :             : {
    3511                 :      427980 :   unsigned i, j;
    3512                 :      427980 :   struct iv_cand *cand_1, *cand_2;
    3513                 :             : 
    3514                 :      427980 :   data->iv_common_cands.qsort (common_cand_cmp);
    3515                 :     2599300 :   for (i = 0; i < data->iv_common_cands.length (); i++)
    3516                 :             :     {
    3517                 :     1287636 :       class iv_common_cand *ptr = data->iv_common_cands[i];
    3518                 :             : 
    3519                 :             :       /* Only add IV candidate if it's derived from multiple uses.  */
    3520                 :     1287636 :       if (ptr->uses.length () <= 1)
    3521                 :             :         break;
    3522                 :             : 
    3523                 :      871670 :       cand_1 = NULL;
    3524                 :      871670 :       cand_2 = NULL;
    3525                 :      871670 :       if (ip_normal_pos (data->current_loop))
    3526                 :      858587 :         cand_1 = add_candidate_1 (data, ptr->base, ptr->step,
    3527                 :             :                                   false, IP_NORMAL, NULL, NULL);
    3528                 :             : 
    3529                 :      871670 :       if (ip_end_pos (data->current_loop)
    3530                 :      871670 :           && allow_ip_end_pos_p (data->current_loop))
    3531                 :       41813 :         cand_2 = add_candidate_1 (data, ptr->base, ptr->step,
    3532                 :             :                                   false, IP_END, NULL, NULL);
    3533                 :             : 
    3534                 :             :       /* Bind deriving uses and the new candidates.  */
    3535                 :     6543236 :       for (j = 0; j < ptr->uses.length (); j++)
    3536                 :             :         {
    3537                 :     2399948 :           struct iv_group *group = data->vgroups[ptr->uses[j]->group_id];
    3538                 :     2399948 :           if (cand_1)
    3539                 :     2327550 :             bitmap_set_bit (group->related_cands, cand_1->id);
    3540                 :     2399948 :           if (cand_2)
    3541                 :      127993 :             bitmap_set_bit (group->related_cands, cand_2->id);
    3542                 :             :         }
    3543                 :             :     }
    3544                 :             : 
    3545                 :             :   /* Release data since it is useless from this point.  */
    3546                 :      427980 :   data->iv_common_cand_tab->empty ();
    3547                 :      427980 :   data->iv_common_cands.truncate (0);
    3548                 :      427980 : }
    3549                 :             : 
    3550                 :             : /* Adds candidates based on the value of USE's iv.  */
    3551                 :             : 
    3552                 :             : static void
    3553                 :     1393329 : add_iv_candidate_for_use (struct ivopts_data *data, struct iv_use *use)
    3554                 :             : {
    3555                 :     1393329 :   poly_uint64 offset;
    3556                 :     1393329 :   tree base;
    3557                 :     1393329 :   struct iv *iv = use->iv;
    3558                 :     1393329 :   tree basetype = TREE_TYPE (iv->base);
    3559                 :             : 
    3560                 :             :   /* Don't add candidate for iv_use with non integer, pointer or non-mode
    3561                 :             :      precision types, instead, add candidate for the corresponding scev in
    3562                 :             :      unsigned type with the same precision.  See PR93674 for more info.  */
    3563                 :      643479 :   if ((TREE_CODE (basetype) != INTEGER_TYPE && !POINTER_TYPE_P (basetype))
    3564                 :     2036656 :       || !type_has_mode_precision_p (basetype))
    3565                 :             :     {
    3566                 :         163 :       basetype = lang_hooks.types.type_for_mode (TYPE_MODE (basetype),
    3567                 :         163 :                                                  TYPE_UNSIGNED (basetype));
    3568                 :         163 :       add_candidate (data, fold_convert (basetype, iv->base),
    3569                 :             :                      fold_convert (basetype, iv->step), false, NULL);
    3570                 :         163 :       return;
    3571                 :             :     }
    3572                 :             : 
    3573                 :     1393166 :   add_candidate (data, iv->base, iv->step, false, use);
    3574                 :             : 
    3575                 :             :   /* Record common candidate for use in case it can be shared by others.  */
    3576                 :     1393166 :   record_common_cand (data, iv->base, iv->step, use);
    3577                 :             : 
    3578                 :             :   /* Record common candidate with initial value zero.  */
    3579                 :     1393166 :   basetype = TREE_TYPE (iv->base);
    3580                 :     1393166 :   if (POINTER_TYPE_P (basetype))
    3581                 :      643327 :     basetype = sizetype;
    3582                 :     1393166 :   record_common_cand (data, build_int_cst (basetype, 0), iv->step, use);
    3583                 :             : 
    3584                 :             :   /* Compare the cost of an address with an unscaled index with the cost of
    3585                 :             :     an address with a scaled index and add candidate if useful.  */
    3586                 :     1393166 :   poly_int64 step;
    3587                 :     1393166 :   if (use != NULL
    3588                 :     1393166 :       && poly_int_tree_p (iv->step, &step)
    3589                 :     1196011 :       && address_p (use->type))
    3590                 :             :     {
    3591                 :      453903 :       poly_int64 new_step;
    3592                 :      453903 :       unsigned int fact = preferred_mem_scale_factor
    3593                 :      453903 :         (use->iv->base,
    3594                 :      453903 :          TYPE_MODE (use->mem_type),
    3595                 :      453903 :          optimize_loop_for_speed_p (data->current_loop));
    3596                 :             : 
    3597                 :      453903 :       if (fact != 1
    3598                 :      453903 :           && multiple_p (step, fact, &new_step))
    3599                 :           0 :         add_candidate (data, size_int (0),
    3600                 :             :                        wide_int_to_tree (sizetype, new_step),
    3601                 :             :                        true, NULL);
    3602                 :             :     }
    3603                 :             : 
    3604                 :             :   /* Record common candidate with constant offset stripped in base.
    3605                 :             :      Like the use itself, we also add candidate directly for it.  */
    3606                 :     1393166 :   base = strip_offset (iv->base, &offset);
    3607                 :     1393166 :   if (maybe_ne (offset, 0U) || base != iv->base)
    3608                 :             :     {
    3609                 :      734840 :       record_common_cand (data, base, iv->step, use);
    3610                 :      734840 :       add_candidate (data, base, iv->step, false, use);
    3611                 :             :     }
    3612                 :             : 
    3613                 :             :   /* Record common candidate with base_object removed in base.  */
    3614                 :     1393166 :   base = iv->base;
    3615                 :     1393166 :   STRIP_NOPS (base);
    3616                 :     1393166 :   if (iv->base_object != NULL && TREE_CODE (base) == POINTER_PLUS_EXPR)
    3617                 :             :     {
    3618                 :      278691 :       tree step = iv->step;
    3619                 :             : 
    3620                 :      278691 :       STRIP_NOPS (step);
    3621                 :      278691 :       base = TREE_OPERAND (base, 1);
    3622                 :      278691 :       step = fold_convert (sizetype, step);
    3623                 :      278691 :       record_common_cand (data, base, step, use);
    3624                 :             :       /* Also record common candidate with offset stripped.  */
    3625                 :      278691 :       tree alt_base, alt_offset;
    3626                 :      278691 :       split_constant_offset (base, &alt_base, &alt_offset);
    3627                 :      278691 :       if (!integer_zerop (alt_offset))
    3628                 :      208172 :         record_common_cand (data, alt_base, step, use);
    3629                 :             :     }
    3630                 :             : 
    3631                 :             :   /* At last, add auto-incremental candidates.  Make such variables
    3632                 :             :      important since other iv uses with same base object may be based
    3633                 :             :      on it.  */
    3634                 :     1393166 :   if (use != NULL && address_p (use->type))
    3635                 :      494878 :     add_autoinc_candidates (data, iv->base, iv->step, true, use);
    3636                 :             : }
    3637                 :             : 
    3638                 :             : /* Adds candidates based on the uses.  */
    3639                 :             : 
    3640                 :             : static void
    3641                 :      427980 : add_iv_candidate_for_groups (struct ivopts_data *data)
    3642                 :             : {
    3643                 :      427980 :   unsigned i;
    3644                 :             : 
    3645                 :             :   /* Only add candidate for the first use in group.  */
    3646                 :     3642618 :   for (i = 0; i < data->vgroups.length (); i++)
    3647                 :             :     {
    3648                 :     1393329 :       struct iv_group *group = data->vgroups[i];
    3649                 :             : 
    3650                 :     1393329 :       gcc_assert (group->vuses[0] != NULL);
    3651                 :     1393329 :       add_iv_candidate_for_use (data, group->vuses[0]);
    3652                 :             :     }
    3653                 :      427980 :   add_iv_candidate_derived_from_uses (data);
    3654                 :      427980 : }
    3655                 :             : 
    3656                 :             : /* Record important candidates and add them to related_cands bitmaps.  */
    3657                 :             : 
    3658                 :             : static void
    3659                 :      427980 : record_important_candidates (struct ivopts_data *data)
    3660                 :             : {
    3661                 :      427980 :   unsigned i;
    3662                 :      427980 :   struct iv_group *group;
    3663                 :             : 
    3664                 :     8819388 :   for (i = 0; i < data->vcands.length (); i++)
    3665                 :             :     {
    3666                 :     3981714 :       struct iv_cand *cand = data->vcands[i];
    3667                 :             : 
    3668                 :     3981714 :       if (cand->important)
    3669                 :     3122207 :         bitmap_set_bit (data->important_candidates, i);
    3670                 :             :     }
    3671                 :             : 
    3672                 :      427980 :   data->consider_all_candidates = (data->vcands.length ()
    3673                 :      427980 :                                    <= CONSIDER_ALL_CANDIDATES_BOUND);
    3674                 :             : 
    3675                 :             :   /* Add important candidates to groups' related_cands bitmaps.  */
    3676                 :     3642618 :   for (i = 0; i < data->vgroups.length (); i++)
    3677                 :             :     {
    3678                 :     1393329 :       group = data->vgroups[i];
    3679                 :     1393329 :       bitmap_ior_into (group->related_cands, data->important_candidates);
    3680                 :             :     }
    3681                 :      427980 : }
    3682                 :             : 
    3683                 :             : /* Allocates the data structure mapping the (use, candidate) pairs to costs.
    3684                 :             :    If consider_all_candidates is true, we use a two-dimensional array, otherwise
    3685                 :             :    we allocate a simple list to every use.  */
    3686                 :             : 
    3687                 :             : static void
    3688                 :      427980 : alloc_use_cost_map (struct ivopts_data *data)
    3689                 :             : {
    3690                 :      427980 :   unsigned i, size, s;
    3691                 :             : 
    3692                 :     3642618 :   for (i = 0; i < data->vgroups.length (); i++)
    3693                 :             :     {
    3694                 :     1393329 :       struct iv_group *group = data->vgroups[i];
    3695                 :             : 
    3696                 :     1393329 :       if (data->consider_all_candidates)
    3697                 :     1381867 :         size = data->vcands.length ();
    3698                 :             :       else
    3699                 :             :         {
    3700                 :       11462 :           s = bitmap_count_bits (group->related_cands);
    3701                 :             : 
    3702                 :             :           /* Round up to the power of two, so that moduling by it is fast.  */
    3703                 :       22924 :           size = s ? (1 << ceil_log2 (s)) : 1;
    3704                 :             :         }
    3705                 :             : 
    3706                 :     1393329 :       group->n_map_members = size;
    3707                 :     1393329 :       group->cost_map = XCNEWVEC (class cost_pair, size);
    3708                 :             :     }
    3709                 :      427980 : }
    3710                 :             : 
    3711                 :             : /* Sets cost of (GROUP, CAND) pair to COST and record that it depends
    3712                 :             :    on invariants INV_VARS and that the value used in expressing it is
    3713                 :             :    VALUE, and in case of iv elimination the comparison operator is COMP.  */
    3714                 :             : 
    3715                 :             : static void
    3716                 :    15435461 : set_group_iv_cost (struct ivopts_data *data,
    3717                 :             :                    struct iv_group *group, struct iv_cand *cand,
    3718                 :             :                    comp_cost cost, bitmap inv_vars, tree value,
    3719                 :             :                    enum tree_code comp, bitmap inv_exprs)
    3720                 :             : {
    3721                 :    15435461 :   unsigned i, s;
    3722                 :             : 
    3723                 :    15435461 :   if (cost.infinite_cost_p ())
    3724                 :             :     {
    3725                 :     5513533 :       BITMAP_FREE (inv_vars);
    3726                 :     5513533 :       BITMAP_FREE (inv_exprs);
    3727                 :     5513533 :       return;
    3728                 :             :     }
    3729                 :             : 
    3730                 :     9921928 :   if (data->consider_all_candidates)
    3731                 :             :     {
    3732                 :     9818923 :       group->cost_map[cand->id].cand = cand;
    3733                 :     9818923 :       group->cost_map[cand->id].cost = cost;
    3734                 :     9818923 :       group->cost_map[cand->id].inv_vars = inv_vars;
    3735                 :     9818923 :       group->cost_map[cand->id].inv_exprs = inv_exprs;
    3736                 :     9818923 :       group->cost_map[cand->id].value = value;
    3737                 :     9818923 :       group->cost_map[cand->id].comp = comp;
    3738                 :     9818923 :       return;
    3739                 :             :     }
    3740                 :             : 
    3741                 :             :   /* n_map_members is a power of two, so this computes modulo.  */
    3742                 :      103005 :   s = cand->id & (group->n_map_members - 1);
    3743                 :      118132 :   for (i = s; i < group->n_map_members; i++)
    3744                 :      118044 :     if (!group->cost_map[i].cand)
    3745                 :      102917 :       goto found;
    3746                 :         138 :   for (i = 0; i < s; i++)
    3747                 :         138 :     if (!group->cost_map[i].cand)
    3748                 :          88 :       goto found;
    3749                 :             : 
    3750                 :           0 :   gcc_unreachable ();
    3751                 :             : 
    3752                 :      103005 : found:
    3753                 :      103005 :   group->cost_map[i].cand = cand;
    3754                 :      103005 :   group->cost_map[i].cost = cost;
    3755                 :      103005 :   group->cost_map[i].inv_vars = inv_vars;
    3756                 :      103005 :   group->cost_map[i].inv_exprs = inv_exprs;
    3757                 :      103005 :   group->cost_map[i].value = value;
    3758                 :      103005 :   group->cost_map[i].comp = comp;
    3759                 :             : }
    3760                 :             : 
    3761                 :             : /* Gets cost of (GROUP, CAND) pair.  */
    3762                 :             : 
    3763                 :             : static class cost_pair *
    3764                 :   174974964 : get_group_iv_cost (struct ivopts_data *data, struct iv_group *group,
    3765                 :             :                    struct iv_cand *cand)
    3766                 :             : {
    3767                 :   174974964 :   unsigned i, s;
    3768                 :   174974964 :   class cost_pair *ret;
    3769                 :             : 
    3770                 :   174974964 :   if (!cand)
    3771                 :             :     return NULL;
    3772                 :             : 
    3773                 :   169932355 :   if (data->consider_all_candidates)
    3774                 :             :     {
    3775                 :   160342058 :       ret = group->cost_map + cand->id;
    3776                 :   160342058 :       if (!ret->cand)
    3777                 :             :         return NULL;
    3778                 :             : 
    3779                 :    89685625 :       return ret;
    3780                 :             :     }
    3781                 :             : 
    3782                 :             :   /* n_map_members is a power of two, so this computes modulo.  */
    3783                 :     9590297 :   s = cand->id & (group->n_map_members - 1);
    3784                 :    16570723 :   for (i = s; i < group->n_map_members; i++)
    3785                 :    16429177 :     if (group->cost_map[i].cand == cand)
    3786                 :     3276202 :       return group->cost_map + i;
    3787                 :    13152975 :     else if (group->cost_map[i].cand == NULL)
    3788                 :             :       return NULL;
    3789                 :      371917 :   for (i = 0; i < s; i++)
    3790                 :      348352 :     if (group->cost_map[i].cand == cand)
    3791                 :        1121 :       return group->cost_map + i;
    3792                 :      347231 :     else if (group->cost_map[i].cand == NULL)
    3793                 :             :       return NULL;
    3794                 :             : 
    3795                 :             :   return NULL;
    3796                 :             : }
    3797                 :             : 
    3798                 :             : /* Produce DECL_RTL for object obj so it looks like it is stored in memory.  */
    3799                 :             : static rtx
    3800                 :       38585 : produce_memory_decl_rtl (tree obj, int *regno)
    3801                 :             : {
    3802                 :       38585 :   addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (obj));
    3803                 :       38585 :   machine_mode address_mode = targetm.addr_space.address_mode (as);
    3804                 :       38585 :   rtx x;
    3805                 :             : 
    3806                 :       38585 :   gcc_assert (obj);
    3807                 :       38585 :   if (TREE_STATIC (obj) || DECL_EXTERNAL (obj))
    3808                 :             :     {
    3809                 :       38585 :       const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (obj));
    3810                 :       38585 :       x = gen_rtx_SYMBOL_REF (address_mode, name);
    3811                 :       38585 :       SET_SYMBOL_REF_DECL (x, obj);
    3812                 :       38585 :       x = gen_rtx_MEM (DECL_MODE (obj), x);
    3813                 :       38585 :       set_mem_addr_space (x, as);
    3814                 :       38585 :       targetm.encode_section_info (obj, x, true);
    3815                 :             :     }
    3816                 :             :   else
    3817                 :             :     {
    3818                 :           0 :       x = gen_raw_REG (address_mode, (*regno)++);
    3819                 :           0 :       x = gen_rtx_MEM (DECL_MODE (obj), x);
    3820                 :           0 :       set_mem_addr_space (x, as);
    3821                 :             :     }
    3822                 :             : 
    3823                 :       38585 :   return x;
    3824                 :             : }
    3825                 :             : 
    3826                 :             : /* Prepares decl_rtl for variables referred in *EXPR_P.  Callback for
    3827                 :             :    walk_tree.  DATA contains the actual fake register number.  */
    3828                 :             : 
    3829                 :             : static tree
    3830                 :      540190 : prepare_decl_rtl (tree *expr_p, int *ws, void *data)
    3831                 :             : {
    3832                 :      540190 :   tree obj = NULL_TREE;
    3833                 :      540190 :   rtx x = NULL_RTX;
    3834                 :      540190 :   int *regno = (int *) data;
    3835                 :             : 
    3836                 :      540190 :   switch (TREE_CODE (*expr_p))
    3837                 :             :     {
    3838                 :      154340 :     case ADDR_EXPR:
    3839                 :      154340 :       for (expr_p = &TREE_OPERAND (*expr_p, 0);
    3840                 :      154340 :            handled_component_p (*expr_p);
    3841                 :           0 :            expr_p = &TREE_OPERAND (*expr_p, 0))
    3842                 :           0 :         continue;
    3843                 :      154340 :       obj = *expr_p;
    3844                 :      154340 :       if (DECL_P (obj) && HAS_RTL_P (obj) && !DECL_RTL_SET_P (obj))
    3845                 :           0 :         x = produce_memory_decl_rtl (obj, regno);
    3846                 :             :       break;
    3847                 :             : 
    3848                 :           0 :     case SSA_NAME:
    3849                 :           0 :       *ws = 0;
    3850                 :           0 :       obj = SSA_NAME_VAR (*expr_p);
    3851                 :             :       /* Defer handling of anonymous SSA_NAMEs to the expander.  */
    3852                 :           0 :       if (!obj)
    3853                 :             :         return NULL_TREE;
    3854                 :           0 :       if (!DECL_RTL_SET_P (obj))
    3855                 :           0 :         x = gen_raw_REG (DECL_MODE (obj), (*regno)++);
    3856                 :             :       break;
    3857                 :             : 
    3858                 :      154340 :     case VAR_DECL:
    3859                 :      154340 :     case PARM_DECL:
    3860                 :      154340 :     case RESULT_DECL:
    3861                 :      154340 :       *ws = 0;
    3862                 :      154340 :       obj = *expr_p;
    3863                 :             : 
    3864                 :      154340 :       if (DECL_RTL_SET_P (obj))
    3865                 :             :         break;
    3866                 :             : 
    3867                 :           0 :       if (DECL_MODE (obj) == BLKmode)
    3868                 :           0 :         x = produce_memory_decl_rtl (obj, regno);
    3869                 :             :       else
    3870                 :           0 :         x = gen_raw_REG (DECL_MODE (obj), (*regno)++);
    3871                 :             : 
    3872                 :             :       break;
    3873                 :             : 
    3874                 :             :     default:
    3875                 :             :       break;
    3876                 :             :     }
    3877                 :             : 
    3878                 :           0 :   if (x)
    3879                 :             :     {
    3880                 :           0 :       decl_rtl_to_reset.safe_push (obj);
    3881                 :           0 :       SET_DECL_RTL (obj, x);
    3882                 :             :     }
    3883                 :             : 
    3884                 :             :   return NULL_TREE;
    3885                 :             : }
    3886                 :             : 
    3887                 :             : /* Predict whether the given loop will be transformed in the RTL
    3888                 :             :    doloop_optimize pass.  Attempt to duplicate some doloop_optimize checks.
    3889                 :             :    This is only for target independent checks, see targetm.predict_doloop_p
    3890                 :             :    for the target dependent ones.
    3891                 :             : 
    3892                 :             :    Note that according to some initial investigation, some checks like costly
    3893                 :             :    niter check and invalid stmt scanning don't have much gains among general
    3894                 :             :    cases, so keep this as simple as possible first.
    3895                 :             : 
    3896                 :             :    Some RTL specific checks seems unable to be checked in gimple, if any new
    3897                 :             :    checks or easy checks _are_ missing here, please add them.  */
    3898                 :             : 
    3899                 :             : static bool
    3900                 :      427980 : generic_predict_doloop_p (struct ivopts_data *data)
    3901                 :             : {
    3902                 :      427980 :   class loop *loop = data->current_loop;
    3903                 :             : 
    3904                 :             :   /* Call target hook for target dependent checks.  */
    3905                 :      427980 :   if (!targetm.predict_doloop_p (loop))
    3906                 :             :     {
    3907                 :      427980 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3908                 :          76 :         fprintf (dump_file, "Predict doloop failure due to"
    3909                 :             :                             " target specific checks.\n");
    3910                 :      427980 :       return false;
    3911                 :             :     }
    3912                 :             : 
    3913                 :             :   /* Similar to doloop_optimize, check iteration description to know it's
    3914                 :             :      suitable or not.  Keep it as simple as possible, feel free to extend it
    3915                 :             :      if you find any multiple exits cases matter.  */
    3916                 :           0 :   edge exit = single_dom_exit (loop);
    3917                 :           0 :   class tree_niter_desc *niter_desc;
    3918                 :           0 :   if (!exit || !(niter_desc = niter_for_exit (data, exit)))
    3919                 :             :     {
    3920                 :           0 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3921                 :           0 :         fprintf (dump_file, "Predict doloop failure due to"
    3922                 :             :                             " unexpected niters.\n");
    3923                 :           0 :       return false;
    3924                 :             :     }
    3925                 :             : 
    3926                 :             :   /* Similar to doloop_optimize, check whether iteration count too small
    3927                 :             :      and not profitable.  */
    3928                 :           0 :   HOST_WIDE_INT est_niter = get_estimated_loop_iterations_int (loop);
    3929                 :           0 :   if (est_niter == -1)
    3930                 :           0 :     est_niter = get_likely_max_loop_iterations_int (loop);
    3931                 :           0 :   if (est_niter >= 0 && est_niter < 3)
    3932                 :             :     {
    3933                 :           0 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3934                 :           0 :         fprintf (dump_file,
    3935                 :             :                  "Predict doloop failure due to"
    3936                 :             :                  " too few iterations (%u).\n",
    3937                 :             :                  (unsigned int) est_niter);
    3938                 :           0 :       return false;
    3939                 :             :     }
    3940                 :             : 
    3941                 :             :   return true;
    3942                 :             : }
    3943                 :             : 
    3944                 :             : /* Determines cost of the computation of EXPR.  */
    3945                 :             : 
    3946                 :             : static unsigned
    3947                 :      231510 : computation_cost (tree expr, bool speed)
    3948                 :             : {
    3949                 :      231510 :   rtx_insn *seq;
    3950                 :      231510 :   rtx rslt;
    3951                 :      231510 :   tree type = TREE_TYPE (expr);
    3952                 :      231510 :   unsigned cost;
    3953                 :             :   /* Avoid using hard regs in ways which may be unsupported.  */
    3954                 :      231510 :   int regno = LAST_VIRTUAL_REGISTER + 1;
    3955                 :      231510 :   struct cgraph_node *node = cgraph_node::get (current_function_decl);
    3956                 :      231510 :   enum node_frequency real_frequency = node->frequency;
    3957                 :             : 
    3958                 :      231510 :   node->frequency = NODE_FREQUENCY_NORMAL;
    3959                 :      231510 :   crtl->maybe_hot_insn_p = speed;
    3960                 :      231510 :   walk_tree (&expr, prepare_decl_rtl, &regno, NULL);
    3961                 :      231510 :   start_sequence ();
    3962                 :      231510 :   rslt = expand_expr (expr, NULL_RTX, TYPE_MODE (type), EXPAND_NORMAL);
    3963                 :      231510 :   seq = get_insns ();
    3964                 :      231510 :   end_sequence ();
    3965                 :      231510 :   default_rtl_profile ();
    3966                 :      231510 :   node->frequency = real_frequency;
    3967                 :             : 
    3968                 :      231510 :   cost = seq_cost (seq, speed);
    3969                 :      231510 :   if (MEM_P (rslt))
    3970                 :           0 :     cost += address_cost (XEXP (rslt, 0), TYPE_MODE (type),
    3971                 :           0 :                           TYPE_ADDR_SPACE (type), speed);
    3972                 :      231510 :   else if (!REG_P (rslt))
    3973                 :      463020 :     cost += set_src_cost (rslt, TYPE_MODE (type), speed);
    3974                 :             : 
    3975                 :      231510 :   return cost;
    3976                 :             : }
    3977                 :             : 
    3978                 :             : /* Returns variable containing the value of candidate CAND at statement AT.  */
    3979                 :             : 
    3980                 :             : static tree
    3981                 :    16431427 : var_at_stmt (class loop *loop, struct iv_cand *cand, gimple *stmt)
    3982                 :             : {
    3983                 :    16431427 :   if (stmt_after_increment (loop, cand, stmt))
    3984                 :     4101802 :     return cand->var_after;
    3985                 :             :   else
    3986                 :    12329625 :     return cand->var_before;
    3987                 :             : }
    3988                 :             : 
    3989                 :             : /* If A is (TYPE) BA and B is (TYPE) BB, and the types of BA and BB have the
    3990                 :             :    same precision that is at least as wide as the precision of TYPE, stores
    3991                 :             :    BA to A and BB to B, and returns the type of BA.  Otherwise, returns the
    3992                 :             :    type of A and B.  */
    3993                 :             : 
    3994                 :             : static tree
    3995                 :    12536117 : determine_common_wider_type (tree *a, tree *b)
    3996                 :             : {
    3997                 :    12536117 :   tree wider_type = NULL;
    3998                 :    12536117 :   tree suba, subb;
    3999                 :    12536117 :   tree atype = TREE_TYPE (*a);
    4000                 :             : 
    4001                 :    12536117 :   if (CONVERT_EXPR_P (*a))
    4002                 :             :     {
    4003                 :     5616597 :       suba = TREE_OPERAND (*a, 0);
    4004                 :     5616597 :       wider_type = TREE_TYPE (suba);
    4005                 :     5616597 :       if (TYPE_PRECISION (wider_type) < TYPE_PRECISION (atype))
    4006                 :             :         return atype;
    4007                 :             :     }
    4008                 :             :   else
    4009                 :             :     return atype;
    4010                 :             : 
    4011                 :     5603486 :   if (CONVERT_EXPR_P (*b))
    4012                 :             :     {
    4013                 :     1958512 :       subb = TREE_OPERAND (*b, 0);
    4014                 :     1958512 :       if (TYPE_PRECISION (wider_type) != TYPE_PRECISION (TREE_TYPE (subb)))
    4015                 :             :         return atype;
    4016                 :             :     }
    4017                 :             :   else
    4018                 :             :     return atype;
    4019                 :             : 
    4020                 :     1919151 :   *a = suba;
    4021                 :     1919151 :   *b = subb;
    4022                 :     1919151 :   return wider_type;
    4023                 :             : }
    4024                 :             : 
    4025                 :             : /* Determines the expression by that USE is expressed from induction variable
    4026                 :             :    CAND at statement AT in LOOP.  The expression is stored in two parts in a
    4027                 :             :    decomposed form.  The invariant part is stored in AFF_INV; while variant
    4028                 :             :    part in AFF_VAR.  Store ratio of CAND.step over USE.step in PRAT if it's
    4029                 :             :    non-null.  Returns false if USE cannot be expressed using CAND.  */
    4030                 :             : 
    4031                 :             : static bool
    4032                 :    15334277 : get_computation_aff_1 (class loop *loop, gimple *at, struct iv_use *use,
    4033                 :             :                        struct iv_cand *cand, class aff_tree *aff_inv,
    4034                 :             :                        class aff_tree *aff_var, widest_int *prat = NULL)
    4035                 :             : {
    4036                 :    15334277 :   tree ubase = use->iv->base, ustep = use->iv->step;
    4037                 :    15334277 :   tree cbase = cand->iv->base, cstep = cand->iv->step;
    4038                 :    15334277 :   tree common_type, uutype, var, cstep_common;
    4039                 :    15334277 :   tree utype = TREE_TYPE (ubase), ctype = TREE_TYPE (cbase);
    4040                 :    15334277 :   aff_tree aff_cbase;
    4041                 :    15334277 :   widest_int rat;
    4042                 :             : 
    4043                 :             :   /* We must have a precision to express the values of use.  */
    4044                 :    15334277 :   if (TYPE_PRECISION (utype) > TYPE_PRECISION (ctype))
    4045                 :             :     return false;
    4046                 :             : 
    4047                 :    15334028 :   var = var_at_stmt (loop, cand, at);
    4048                 :    15334028 :   uutype = unsigned_type_for (utype);
    4049                 :             : 
    4050                 :             :   /* If the conversion is not noop, perform it.  */
    4051                 :    15334028 :   if (TYPE_PRECISION (utype) < TYPE_PRECISION (ctype))
    4052                 :             :     {
    4053                 :      253234 :       if (cand->orig_iv != NULL && CONVERT_EXPR_P (cbase)
    4054                 :     1505776 :           && (CONVERT_EXPR_P (cstep) || poly_int_tree_p (cstep)))
    4055                 :             :         {
    4056                 :       40418 :           tree inner_base, inner_step, inner_type;
    4057                 :       40418 :           inner_base = TREE_OPERAND (cbase, 0);
    4058                 :       40418 :           if (CONVERT_EXPR_P (cstep))
    4059                 :        4732 :             inner_step = TREE_OPERAND (cstep, 0);
    4060                 :             :           else
    4061                 :             :             inner_step = cstep;
    4062                 :             : 
    4063                 :       40418 :           inner_type = TREE_TYPE (inner_base);
    4064                 :             :           /* If candidate is added from a biv whose type is smaller than
    4065                 :             :              ctype, we know both candidate and the biv won't overflow.
    4066                 :             :              In this case, it's safe to skip the convertion in candidate.
    4067                 :             :              As an example, (unsigned short)((unsigned long)A) equals to
    4068                 :             :              (unsigned short)A, if A has a type no larger than short.  */
    4069                 :       40418 :           if (TYPE_PRECISION (inner_type) <= TYPE_PRECISION (uutype))
    4070                 :             :             {
    4071                 :       39132 :               cbase = inner_base;
    4072                 :       39132 :               cstep = inner_step;
    4073                 :             :             }
    4074                 :             :         }
    4075                 :     1465358 :       cbase = fold_convert (uutype, cbase);
    4076                 :     1465358 :       cstep = fold_convert (uutype, cstep);
    4077                 :     1465358 :       var = fold_convert (uutype, var);
    4078                 :             :     }
    4079                 :             : 
    4080                 :             :   /* Ratio is 1 when computing the value of biv cand by itself.
    4081                 :             :      We can't rely on constant_multiple_of in this case because the
    4082                 :             :      use is created after the original biv is selected.  The call
    4083                 :             :      could fail because of inconsistent fold behavior.  See PR68021
    4084                 :             :      for more information.  */
    4085                 :    15334028 :   if (cand->pos == IP_ORIGINAL && cand->incremented_at == use->stmt)
    4086                 :             :     {
    4087                 :        4588 :       gcc_assert (is_gimple_assign (use->stmt));
    4088                 :        4588 :       gcc_assert (use->iv->ssa_name == cand->var_after);
    4089                 :        4588 :       gcc_assert (gimple_assign_lhs (use->stmt) == cand->var_after);
    4090                 :        4588 :       rat = 1;
    4091                 :             :     }
    4092                 :    15329440 :   else if (!constant_multiple_of (ustep, cstep, &rat))
    4093                 :             :     return false;
    4094                 :             : 
    4095                 :    12536117 :   if (prat)
    4096                 :    11260488 :     *prat = rat;
    4097                 :             : 
    4098                 :             :   /* In case both UBASE and CBASE are shortened to UUTYPE from some common
    4099                 :             :      type, we achieve better folding by computing their difference in this
    4100                 :             :      wider type, and cast the result to UUTYPE.  We do not need to worry about
    4101                 :             :      overflows, as all the arithmetics will in the end be performed in UUTYPE
    4102                 :             :      anyway.  */
    4103                 :    12536117 :   common_type = determine_common_wider_type (&ubase, &cbase);
    4104                 :             : 
    4105                 :             :   /* use = ubase - ratio * cbase + ratio * var.  */
    4106                 :    12536117 :   tree_to_aff_combination (ubase, common_type, aff_inv);
    4107                 :    12536117 :   tree_to_aff_combination (cbase, common_type, &aff_cbase);
    4108                 :    12536117 :   tree_to_aff_combination (var, uutype, aff_var);
    4109                 :             : 
    4110                 :             :   /* We need to shift the value if we are after the increment.  */
    4111                 :    12536117 :   if (stmt_after_increment (loop, cand, at))
    4112                 :             :     {
    4113                 :     2776653 :       aff_tree cstep_aff;
    4114                 :             : 
    4115                 :     2776653 :       if (common_type != uutype)
    4116                 :      866640 :         cstep_common = fold_convert (common_type, cstep);
    4117                 :             :       else
    4118                 :             :         cstep_common = cstep;
    4119                 :             : 
    4120                 :     2776653 :       tree_to_aff_combination (cstep_common, common_type, &cstep_aff);
    4121                 :     2776653 :       aff_combination_add (&aff_cbase, &cstep_aff);
    4122                 :     2776653 :     }
    4123                 :             : 
    4124                 :    12536117 :   aff_combination_scale (&aff_cbase, -rat);
    4125                 :    12536117 :   aff_combination_add (aff_inv, &aff_cbase);
    4126                 :    12536117 :   if (common_type != uutype)
    4127                 :     9458616 :     aff_combination_convert (aff_inv, uutype);
    4128                 :             : 
    4129                 :    12536117 :   aff_combination_scale (aff_var, rat);
    4130                 :    12536117 :   return true;
    4131                 :    15334277 : }
    4132                 :             : 
    4133                 :             : /* Determines the expression by that USE is expressed from induction variable
    4134                 :             :    CAND at statement AT in LOOP.  The expression is stored in a decomposed
    4135                 :             :    form into AFF.  Returns false if USE cannot be expressed using CAND.  */
    4136                 :             : 
    4137                 :             : static bool
    4138                 :     1070700 : get_computation_aff (class loop *loop, gimple *at, struct iv_use *use,
    4139                 :             :                      struct iv_cand *cand, class aff_tree *aff)
    4140                 :             : {
    4141                 :     1070700 :   aff_tree aff_var;
    4142                 :             : 
    4143                 :     1070700 :   if (!get_computation_aff_1 (loop, at, use, cand, aff, &aff_var))
    4144                 :             :     return false;
    4145                 :             : 
    4146                 :      980980 :   aff_combination_add (aff, &aff_var);
    4147                 :      980980 :   return true;
    4148                 :     1070700 : }
    4149                 :             : 
    4150                 :             : /* Return the type of USE.  */
    4151                 :             : 
    4152                 :             : static tree
    4153                 :      836797 : get_use_type (struct iv_use *use)
    4154                 :             : {
    4155                 :      836797 :   tree base_type = TREE_TYPE (use->iv->base);
    4156                 :      836797 :   tree type;
    4157                 :             : 
    4158                 :      836797 :   if (use->type == USE_REF_ADDRESS)
    4159                 :             :     {
    4160                 :             :       /* The base_type may be a void pointer.  Create a pointer type based on
    4161                 :             :          the mem_ref instead.  */
    4162                 :           0 :       type = build_pointer_type (TREE_TYPE (*use->op_p));
    4163                 :           0 :       gcc_assert (TYPE_ADDR_SPACE (TREE_TYPE (type))
    4164                 :             :                   == TYPE_ADDR_SPACE (TREE_TYPE (base_type)));
    4165                 :             :     }
    4166                 :             :   else
    4167                 :             :     type = base_type;
    4168                 :             : 
    4169                 :      836797 :   return type;
    4170                 :             : }
    4171                 :             : 
    4172                 :             : /* Determines the expression by that USE is expressed from induction variable
    4173                 :             :    CAND at statement AT in LOOP.  The computation is unshared.  */
    4174                 :             : 
    4175                 :             : static tree
    4176                 :      314472 : get_computation_at (class loop *loop, gimple *at,
    4177                 :             :                     struct iv_use *use, struct iv_cand *cand)
    4178                 :             : {
    4179                 :      314472 :   aff_tree aff;
    4180                 :      314472 :   tree type = get_use_type (use);
    4181                 :             : 
    4182                 :      314472 :   if (!get_computation_aff (loop, at, use, cand, &aff))
    4183                 :             :     return NULL_TREE;
    4184                 :      224752 :   unshare_aff_combination (&aff);
    4185                 :      224752 :   return fold_convert (type, aff_combination_to_tree (&aff));
    4186                 :      314472 : }
    4187                 :             : 
    4188                 :             : /* Like get_computation_at, but try harder, even if the computation
    4189                 :             :    is more expensive.  Intended for debug stmts.  */
    4190                 :             : 
    4191                 :             : static tree
    4192                 :      140408 : get_debug_computation_at (class loop *loop, gimple *at,
    4193                 :             :                           struct iv_use *use, struct iv_cand *cand)
    4194                 :             : {
    4195                 :      140408 :   if (tree ret = get_computation_at (loop, at, use, cand))
    4196                 :             :     return ret;
    4197                 :             : 
    4198                 :       89720 :   tree ubase = use->iv->base, ustep = use->iv->step;
    4199                 :       89720 :   tree cbase = cand->iv->base, cstep = cand->iv->step;
    4200                 :       89720 :   tree var;
    4201                 :       89720 :   tree utype = TREE_TYPE (ubase), ctype = TREE_TYPE (cbase);
    4202                 :       89720 :   widest_int rat;
    4203                 :             : 
    4204                 :             :   /* We must have a precision to express the values of use.  */
    4205                 :       89720 :   if (TYPE_PRECISION (utype) >= TYPE_PRECISION (ctype))
    4206                 :             :     return NULL_TREE;
    4207                 :             : 
    4208                 :             :   /* Try to handle the case that get_computation_at doesn't,
    4209                 :             :      try to express
    4210                 :             :      use = ubase + (var - cbase) / ratio.  */
    4211                 :        5421 :   if (!constant_multiple_of (cstep, fold_convert (TREE_TYPE (cstep), ustep),
    4212                 :             :                              &rat))
    4213                 :             :     return NULL_TREE;
    4214                 :             : 
    4215                 :        4764 :   bool neg_p = false;
    4216                 :        4764 :   if (wi::neg_p (rat))
    4217                 :             :     {
    4218                 :         862 :       if (TYPE_UNSIGNED (ctype))
    4219                 :             :         return NULL_TREE;
    4220                 :           0 :       neg_p = true;
    4221                 :           0 :       rat = wi::neg (rat);
    4222                 :             :     }
    4223                 :             : 
    4224                 :             :   /* If both IVs can wrap around and CAND doesn't have a power of two step,
    4225                 :             :      it is unsafe.  Consider uint16_t CAND with step 9, when wrapping around,
    4226                 :             :      the values will be ... 0xfff0, 0xfff9, 2, 11 ... and when use is say
    4227                 :             :      uint8_t with step 3, those values divided by 3 cast to uint8_t will be
    4228                 :             :      ... 0x50, 0x53, 0, 3 ... rather than expected 0x50, 0x53, 0x56, 0x59.  */
    4229                 :        3902 :   if (!use->iv->no_overflow
    4230                 :          57 :       && !cand->iv->no_overflow
    4231                 :        3947 :       && !integer_pow2p (cstep))
    4232                 :             :     return NULL_TREE;
    4233                 :             : 
    4234                 :        3888 :   int bits = wi::exact_log2 (rat);
    4235                 :        3888 :   if (bits == -1)
    4236                 :         603 :     bits = wi::floor_log2 (rat) + 1;
    4237                 :        3888 :   if (!cand->iv->no_overflow
    4238                 :        3888 :       && TYPE_PRECISION (utype) + bits > TYPE_PRECISION (ctype))
    4239                 :             :     return NULL_TREE;
    4240                 :             : 
    4241                 :        3888 :   var = var_at_stmt (loop, cand, at);
    4242                 :             : 
    4243                 :        3888 :   if (POINTER_TYPE_P (ctype))
    4244                 :             :     {
    4245                 :         108 :       ctype = unsigned_type_for (ctype);
    4246                 :         108 :       cbase = fold_convert (ctype, cbase);
    4247                 :         108 :       cstep = fold_convert (ctype, cstep);
    4248                 :         108 :       var = fold_convert (ctype, var);
    4249                 :             :     }
    4250                 :             : 
    4251                 :        3888 :   if (stmt_after_increment (loop, cand, at))
    4252                 :          58 :     var = fold_build2 (MINUS_EXPR, TREE_TYPE (var), var,
    4253                 :             :                        unshare_expr (cstep));
    4254                 :             : 
    4255                 :        3888 :   var = fold_build2 (MINUS_EXPR, TREE_TYPE (var), var, cbase);
    4256                 :        3888 :   var = fold_build2 (EXACT_DIV_EXPR, TREE_TYPE (var), var,
    4257                 :             :                      wide_int_to_tree (TREE_TYPE (var), rat));
    4258                 :        3888 :   if (POINTER_TYPE_P (utype))
    4259                 :             :     {
    4260                 :           0 :       var = fold_convert (sizetype, var);
    4261                 :           0 :       if (neg_p)
    4262                 :           0 :         var = fold_build1 (NEGATE_EXPR, sizetype, var);
    4263                 :           0 :       var = fold_build2 (POINTER_PLUS_EXPR, utype, ubase, var);
    4264                 :             :     }
    4265                 :             :   else
    4266                 :             :     {
    4267                 :        3888 :       var = fold_convert (utype, var);
    4268                 :        7776 :       var = fold_build2 (neg_p ? MINUS_EXPR : PLUS_EXPR, utype,
    4269                 :             :                          ubase, var);
    4270                 :             :     }
    4271                 :             :   return var;
    4272                 :       89720 : }
    4273                 :             : 
    4274                 :             : /* Adjust the cost COST for being in loop setup rather than loop body.
    4275                 :             :    If we're optimizing for space, the loop setup overhead is constant;
    4276                 :             :    if we're optimizing for speed, amortize it over the per-iteration cost.
    4277                 :             :    If ROUND_UP_P is true, the result is round up rather than to zero when
    4278                 :             :    optimizing for speed.  */
    4279                 :             : static int64_t
    4280                 :     8896036 : adjust_setup_cost (struct ivopts_data *data, int64_t cost,
    4281                 :             :                    bool round_up_p = false)
    4282                 :             : {
    4283                 :     8896036 :   if (cost == INFTY)
    4284                 :             :     return cost;
    4285                 :     8896036 :   else if (optimize_loop_for_speed_p (data->current_loop))
    4286                 :             :     {
    4287                 :     7357411 :       int64_t niters = (int64_t) avg_loop_niter (data->current_loop);
    4288                 :     7357411 :       return (cost + (round_up_p ? niters - 1 : 0)) / niters;
    4289                 :             :     }
    4290                 :             :   else
    4291                 :             :     return cost;
    4292                 :             : }
    4293                 :             : 
    4294                 :             : /* Calculate the SPEED or size cost of shiftadd EXPR in MODE.  MULT is the
    4295                 :             :    EXPR operand holding the shift.  COST0 and COST1 are the costs for
    4296                 :             :    calculating the operands of EXPR.  Returns true if successful, and returns
    4297                 :             :    the cost in COST.  */
    4298                 :             : 
    4299                 :             : static bool
    4300                 :     1320549 : get_shiftadd_cost (tree expr, scalar_int_mode mode, comp_cost cost0,
    4301                 :             :                    comp_cost cost1, tree mult, bool speed, comp_cost *cost)
    4302                 :             : {
    4303                 :     1320549 :   comp_cost res;
    4304                 :     1320549 :   tree op1 = TREE_OPERAND (expr, 1);
    4305                 :     1320549 :   tree cst = TREE_OPERAND (mult, 1);
    4306                 :     1320549 :   tree multop = TREE_OPERAND (mult, 0);
    4307                 :     1320549 :   int m = exact_log2 (int_cst_value (cst));
    4308                 :     3960758 :   int maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (mode));
    4309                 :     1320549 :   int as_cost, sa_cost;
    4310                 :     1320549 :   bool mult_in_op1;
    4311                 :             : 
    4312                 :     1320549 :   if (!(m >= 0 && m < maxm))
    4313                 :             :     return false;
    4314                 :             : 
    4315                 :      906584 :   STRIP_NOPS (op1);
    4316                 :      906584 :   mult_in_op1 = operand_equal_p (op1, mult, 0);
    4317                 :             : 
    4318                 :      906584 :   as_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
    4319                 :             : 
    4320                 :             :   /* If the target has a cheap shift-and-add or shift-and-sub instruction,
    4321                 :             :      use that in preference to a shift insn followed by an add insn.  */
    4322                 :     1813168 :   sa_cost = (TREE_CODE (expr) != MINUS_EXPR
    4323                 :      906584 :              ? shiftadd_cost (speed, mode, m)
    4324                 :             :              : (mult_in_op1
    4325                 :      123728 :                 ? shiftsub1_cost (speed, mode, m)
    4326                 :       13194 :                 : shiftsub0_cost (speed, mode, m)));
    4327                 :             : 
    4328                 :      906584 :   res = comp_cost (MIN (as_cost, sa_cost), 0);
    4329                 :     1473813 :   res += (mult_in_op1 ? cost0 : cost1);
    4330                 :             : 
    4331                 :      906584 :   STRIP_NOPS (multop);
    4332                 :      906584 :   if (!is_gimple_val (multop))
    4333                 :      428133 :     res += force_expr_to_var_cost (multop, speed);
    4334                 :             : 
    4335                 :      906584 :   *cost = res;
    4336                 :      906584 :   return true;
    4337                 :             : }
    4338                 :             : 
    4339                 :             : /* Estimates cost of forcing expression EXPR into a variable.  */
    4340                 :             : 
    4341                 :             : static comp_cost
    4342                 :    24956739 : force_expr_to_var_cost (tree expr, bool speed)
    4343                 :             : {
    4344                 :    24956739 :   static bool costs_initialized = false;
    4345                 :    24956739 :   static unsigned integer_cost [2];
    4346                 :    24956739 :   static unsigned symbol_cost [2];
    4347                 :    24956739 :   static unsigned address_cost [2];
    4348                 :    24956739 :   tree op0, op1;
    4349                 :    24956739 :   comp_cost cost0, cost1, cost;
    4350                 :    24956739 :   machine_mode mode;
    4351                 :    24956739 :   scalar_int_mode int_mode;
    4352                 :             : 
    4353                 :    24956739 :   if (!costs_initialized)
    4354                 :             :     {
    4355                 :       38585 :       tree type = build_pointer_type (integer_type_node);
    4356                 :       38585 :       tree var, addr;
    4357                 :       38585 :       rtx x;
    4358                 :       38585 :       int i;
    4359                 :             : 
    4360                 :       38585 :       var = create_tmp_var_raw (integer_type_node, "test_var");
    4361                 :       38585 :       TREE_STATIC (var) = 1;
    4362                 :       38585 :       x = produce_memory_decl_rtl (var, NULL);
    4363                 :       38585 :       SET_DECL_RTL (var, x);
    4364                 :             : 
    4365                 :       38585 :       addr = build1 (ADDR_EXPR, type, var);
    4366                 :             : 
    4367                 :             : 
    4368                 :      154340 :       for (i = 0; i < 2; i++)
    4369                 :             :         {
    4370                 :       77170 :           integer_cost[i] = computation_cost (build_int_cst (integer_type_node,
    4371                 :       77170 :                                                              2000), i);
    4372                 :             : 
    4373                 :       77170 :           symbol_cost[i] = computation_cost (addr, i) + 1;
    4374                 :             : 
    4375                 :       77170 :           address_cost[i]
    4376                 :       77170 :             = computation_cost (fold_build_pointer_plus_hwi (addr, 2000), i) + 1;
    4377                 :       77170 :           if (dump_file && (dump_flags & TDF_DETAILS))
    4378                 :             :             {
    4379                 :         111 :               fprintf (dump_file, "force_expr_to_var_cost %s costs:\n", i ? "speed" : "size");
    4380                 :          74 :               fprintf (dump_file, "  integer %d\n", (int) integer_cost[i]);
    4381                 :          74 :               fprintf (dump_file, "  symbol %d\n", (int) symbol_cost[i]);
    4382                 :          74 :               fprintf (dump_file, "  address %d\n", (int) address_cost[i]);
    4383                 :          74 :               fprintf (dump_file, "  other %d\n", (int) target_spill_cost[i]);
    4384                 :          74 :               fprintf (dump_file, "\n");
    4385                 :             :             }
    4386                 :             :         }
    4387                 :             : 
    4388                 :       38585 :       costs_initialized = true;
    4389                 :             :     }
    4390                 :             : 
    4391                 :    24956739 :   STRIP_NOPS (expr);
    4392                 :             : 
    4393                 :    24956739 :   if (SSA_VAR_P (expr))
    4394                 :     4800880 :     return no_cost;
    4395                 :             : 
    4396                 :    20155859 :   if (is_gimple_min_invariant (expr))
    4397                 :             :     {
    4398                 :    12009111 :       if (poly_int_tree_p (expr))
    4399                 :    10301811 :         return comp_cost (integer_cost [speed], 0);
    4400                 :             : 
    4401                 :     1707300 :       if (TREE_CODE (expr) == ADDR_EXPR)
    4402                 :             :         {
    4403                 :     1707300 :           tree obj = TREE_OPERAND (expr, 0);
    4404                 :             : 
    4405                 :     1707300 :           if (VAR_P (obj)
    4406                 :             :               || TREE_CODE (obj) == PARM_DECL
    4407                 :             :               || TREE_CODE (obj) == RESULT_DECL)
    4408                 :     1645837 :             return comp_cost (symbol_cost [speed], 0);
    4409                 :             :         }
    4410                 :             : 
    4411                 :       61463 :       return comp_cost (address_cost [speed], 0);
    4412                 :             :     }
    4413                 :             : 
    4414                 :     8146748 :   switch (TREE_CODE (expr))
    4415                 :             :     {
    4416                 :     6918949 :     case POINTER_PLUS_EXPR:
    4417                 :     6918949 :     case PLUS_EXPR:
    4418                 :     6918949 :     case MINUS_EXPR:
    4419                 :     6918949 :     case MULT_EXPR:
    4420                 :     6918949 :     case TRUNC_DIV_EXPR:
    4421                 :     6918949 :     case BIT_AND_EXPR:
    4422                 :     6918949 :     case BIT_IOR_EXPR:
    4423                 :     6918949 :     case LSHIFT_EXPR:
    4424                 :     6918949 :     case RSHIFT_EXPR:
    4425                 :     6918949 :       op0 = TREE_OPERAND (expr, 0);
    4426                 :     6918949 :       op1 = TREE_OPERAND (expr, 1);
    4427                 :     6918949 :       STRIP_NOPS (op0);
    4428                 :     6918949 :       STRIP_NOPS (op1);
    4429                 :     6918949 :       break;
    4430                 :             : 
    4431                 :     1215414 :     CASE_CONVERT:
    4432                 :     1215414 :     case NEGATE_EXPR:
    4433                 :     1215414 :     case BIT_NOT_EXPR:
    4434                 :     1215414 :       op0 = TREE_OPERAND (expr, 0);
    4435                 :     1215414 :       STRIP_NOPS (op0);
    4436                 :     1215414 :       op1 = NULL_TREE;
    4437                 :     1215414 :       break;
    4438                 :             :     /* See add_iv_candidate_for_doloop, for doloop may_be_zero case, we
    4439                 :             :        introduce COND_EXPR for IV base, need to support better cost estimation
    4440                 :             :        for this COND_EXPR and tcc_comparison.  */
    4441                 :           0 :     case COND_EXPR:
    4442                 :           0 :       op0 = TREE_OPERAND (expr, 1);
    4443                 :           0 :       STRIP_NOPS (op0);
    4444                 :           0 :       op1 = TREE_OPERAND (expr, 2);
    4445                 :           0 :       STRIP_NOPS (op1);
    4446                 :           0 :       break;
    4447                 :           0 :     case LT_EXPR:
    4448                 :           0 :     case LE_EXPR:
    4449                 :           0 :     case GT_EXPR:
    4450                 :           0 :     case GE_EXPR:
    4451                 :           0 :     case EQ_EXPR:
    4452                 :           0 :     case NE_EXPR:
    4453                 :           0 :     case UNORDERED_EXPR:
    4454                 :           0 :     case ORDERED_EXPR:
    4455                 :           0 :     case UNLT_EXPR:
    4456                 :           0 :     case UNLE_EXPR:
    4457                 :           0 :     case UNGT_EXPR:
    4458                 :           0 :     case UNGE_EXPR:
    4459                 :           0 :     case UNEQ_EXPR:
    4460                 :           0 :     case LTGT_EXPR:
    4461                 :           0 :     case MAX_EXPR:
    4462                 :           0 :     case MIN_EXPR:
    4463                 :           0 :       op0 = TREE_OPERAND (expr, 0);
    4464                 :           0 :       STRIP_NOPS (op0);
    4465                 :           0 :       op1 = TREE_OPERAND (expr, 1);
    4466                 :           0 :       STRIP_NOPS (op1);
    4467                 :           0 :       break;
    4468                 :             : 
    4469                 :       12385 :     default:
    4470                 :             :       /* Just an arbitrary value, FIXME.  */
    4471                 :       12385 :       return comp_cost (target_spill_cost[speed], 0);
    4472                 :             :     }
    4473                 :             : 
    4474                 :     8134363 :   if (op0 == NULL_TREE
    4475                 :     8134363 :       || TREE_CODE (op0) == SSA_NAME || CONSTANT_CLASS_P (op0))
    4476                 :     3974986 :     cost0 = no_cost;
    4477                 :             :   else
    4478                 :     4159377 :     cost0 = force_expr_to_var_cost (op0, speed);
    4479                 :             : 
    4480                 :     8134363 :   if (op1 == NULL_TREE
    4481                 :     6918949 :       || TREE_CODE (op1) == SSA_NAME || CONSTANT_CLASS_P (op1))
    4482                 :     7352578 :     cost1 = no_cost;
    4483                 :             :   else
    4484                 :      781785 :     cost1 = force_expr_to_var_cost (op1, speed);
    4485                 :             : 
    4486                 :     8134363 :   mode = TYPE_MODE (TREE_TYPE (expr));
    4487                 :     8134363 :   switch (TREE_CODE (expr))
    4488                 :             :     {
    4489                 :     4744233 :     case POINTER_PLUS_EXPR:
    4490                 :     4744233 :     case PLUS_EXPR:
    4491                 :     4744233 :     case MINUS_EXPR:
    4492                 :     4744233 :     case NEGATE_EXPR:
    4493                 :     4744233 :       cost = comp_cost (add_cost (speed, mode), 0);
    4494                 :     4744233 :       if (TREE_CODE (expr) != NEGATE_EXPR)
    4495                 :             :         {
    4496                 :     4642618 :           tree mult = NULL_TREE;
    4497                 :     4642618 :           comp_cost sa_cost;
    4498                 :     4642618 :           if (TREE_CODE (op1) == MULT_EXPR)
    4499                 :             :             mult = op1;
    4500                 :     4055436 :           else if (TREE_CODE (op0) == MULT_EXPR)
    4501                 :             :             mult = op0;
    4502                 :             : 
    4503                 :             :           if (mult != NULL_TREE
    4504                 :     3736034 :               && is_a <scalar_int_mode> (mode, &int_mode)
    4505                 :     1482309 :               && cst_and_fits_in_hwi (TREE_OPERAND (mult, 1))
    4506                 :     1320549 :               && get_shiftadd_cost (expr, int_mode, cost0, cost1, mult,
    4507                 :             :                                     speed, &sa_cost))
    4508                 :      906584 :             return sa_cost;
    4509                 :             :         }
    4510                 :             :       break;
    4511                 :             : 
    4512                 :     1105248 :     CASE_CONVERT:
    4513                 :     1105248 :       {
    4514                 :     1105248 :         tree inner_mode, outer_mode;
    4515                 :     1105248 :         outer_mode = TREE_TYPE (expr);
    4516                 :     1105248 :         inner_mode = TREE_TYPE (op0);
    4517                 :     1105248 :         cost = comp_cost (convert_cost (TYPE_MODE (outer_mode),
    4518                 :     1105248 :                                        TYPE_MODE (inner_mode), speed), 0);
    4519                 :             :       }
    4520                 :     1105248 :       break;
    4521                 :             : 
    4522                 :     2217268 :     case MULT_EXPR:
    4523                 :     2217268 :       if (cst_and_fits_in_hwi (op0))
    4524                 :           0 :         cost = comp_cost (mult_by_coeff_cost (int_cst_value (op0),
    4525                 :             :                                              mode, speed), 0);
    4526                 :     2217268 :       else if (cst_and_fits_in_hwi (op1))
    4527                 :     1853791 :         cost = comp_cost (mult_by_coeff_cost (int_cst_value (op1),
    4528                 :             :                                              mode, speed), 0);
    4529                 :             :       else
    4530                 :      363477 :         return comp_cost (target_spill_cost [speed], 0);
    4531                 :             :       break;
    4532                 :             : 
    4533                 :       25374 :     case TRUNC_DIV_EXPR:
    4534                 :             :       /* Division by power of two is usually cheap, so we allow it.  Forbid
    4535                 :             :          anything else.  */
    4536                 :       25374 :       if (integer_pow2p (TREE_OPERAND (expr, 1)))
    4537                 :       25374 :         cost = comp_cost (add_cost (speed, mode), 0);
    4538                 :             :       else
    4539                 :           0 :         cost = comp_cost (target_spill_cost[speed], 0);
    4540                 :             :       break;
    4541                 :             : 
    4542                 :       42240 :     case BIT_AND_EXPR:
    4543                 :       42240 :     case BIT_IOR_EXPR:
    4544                 :       42240 :     case BIT_NOT_EXPR:
    4545                 :       42240 :     case LSHIFT_EXPR:
    4546                 :       42240 :     case RSHIFT_EXPR:
    4547                 :       42240 :       cost = comp_cost (add_cost (speed, mode), 0);
    4548                 :       42240 :       break;
    4549                 :           0 :     case COND_EXPR:
    4550                 :           0 :       op0 = TREE_OPERAND (expr, 0);
    4551                 :           0 :       STRIP_NOPS (op0);
    4552                 :           0 :       if (op0 == NULL_TREE || TREE_CODE (op0) == SSA_NAME
    4553                 :           0 :           || CONSTANT_CLASS_P (op0))
    4554                 :           0 :         cost = no_cost;
    4555                 :             :       else
    4556                 :           0 :         cost = force_expr_to_var_cost (op0, speed);
    4557                 :             :       break;
    4558                 :           0 :     case LT_EXPR:
    4559                 :           0 :     case LE_EXPR:
    4560                 :           0 :     case GT_EXPR:
    4561                 :           0 :     case GE_EXPR:
    4562                 :           0 :     case EQ_EXPR:
    4563                 :           0 :     case NE_EXPR:
    4564                 :           0 :     case UNORDERED_EXPR:
    4565                 :           0 :     case ORDERED_EXPR:
    4566                 :           0 :     case UNLT_EXPR:
    4567                 :           0 :     case UNLE_EXPR:
    4568                 :           0 :     case UNGT_EXPR:
    4569                 :           0 :     case UNGE_EXPR:
    4570                 :           0 :     case UNEQ_EXPR:
    4571                 :           0 :     case LTGT_EXPR:
    4572                 :           0 :     case MAX_EXPR:
    4573                 :           0 :     case MIN_EXPR:
    4574                 :             :       /* Simply use add cost for now, FIXME if there is some more accurate cost
    4575                 :             :          evaluation way.  */
    4576                 :           0 :       cost = comp_cost (add_cost (speed, mode), 0);
    4577                 :           0 :       break;
    4578                 :             : 
    4579                 :           0 :     default:
    4580                 :           0 :       gcc_unreachable ();
    4581                 :             :     }
    4582                 :             : 
    4583                 :     6864302 :   cost += cost0;
    4584                 :     6864302 :   cost += cost1;
    4585                 :     6864302 :   return cost;
    4586                 :             : }
    4587                 :             : 
    4588                 :             : /* Estimates cost of forcing EXPR into a variable.  INV_VARS is a set of the
    4589                 :             :    invariants the computation depends on.  */
    4590                 :             : 
    4591                 :             : static comp_cost
    4592                 :    21306795 : force_var_cost (struct ivopts_data *data, tree expr, bitmap *inv_vars)
    4593                 :             : {
    4594                 :    21306795 :   if (!expr)
    4595                 :     1719351 :     return no_cost;
    4596                 :             : 
    4597                 :    19587444 :   find_inv_vars (data, &expr, inv_vars);
    4598                 :    19587444 :   return force_expr_to_var_cost (expr, data->speed);
    4599                 :             : }
    4600                 :             : 
    4601                 :             : /* Returns cost of auto-modifying address expression in shape base + offset.
    4602                 :             :    AINC_STEP is step size of the address IV.  AINC_OFFSET is offset of the
    4603                 :             :    address expression.  The address expression has ADDR_MODE in addr space
    4604                 :             :    AS.  The memory access has MEM_MODE.  SPEED means we are optimizing for
    4605                 :             :    speed or size.  */
    4606                 :             : 
    4607                 :             : enum ainc_type
    4608                 :             : {
    4609                 :             :   AINC_PRE_INC,         /* Pre increment.  */
    4610                 :             :   AINC_PRE_DEC,         /* Pre decrement.  */
    4611                 :             :   AINC_POST_INC,        /* Post increment.  */
    4612                 :             :   AINC_POST_DEC,        /* Post decrement.  */
    4613                 :             :   AINC_NONE             /* Also the number of auto increment types.  */
    4614                 :             : };
    4615                 :             : 
    4616                 :             : struct ainc_cost_data
    4617                 :             : {
    4618                 :             :   int64_t costs[AINC_NONE];
    4619                 :             : };
    4620                 :             : 
    4621                 :             : static comp_cost
    4622                 :     1652480 : get_address_cost_ainc (poly_int64 ainc_step, poly_int64 ainc_offset,
    4623                 :             :                        machine_mode addr_mode, machine_mode mem_mode,
    4624                 :             :                        addr_space_t as, bool speed)
    4625                 :             : {
    4626                 :     1652480 :   if (!USE_LOAD_PRE_DECREMENT (mem_mode)
    4627                 :             :       && !USE_STORE_PRE_DECREMENT (mem_mode)
    4628                 :             :       && !USE_LOAD_POST_DECREMENT (mem_mode)
    4629                 :             :       && !USE_STORE_POST_DECREMENT (mem_mode)
    4630                 :             :       && !USE_LOAD_PRE_INCREMENT (mem_mode)
    4631                 :             :       && !USE_STORE_PRE_INCREMENT (mem_mode)
    4632                 :             :       && !USE_LOAD_POST_INCREMENT (mem_mode)
    4633                 :             :       && !USE_STORE_POST_INCREMENT (mem_mode))
    4634                 :     1652480 :     return infinite_cost;
    4635                 :             : 
    4636                 :             :   static vec<ainc_cost_data *> ainc_cost_data_list;
    4637                 :             :   unsigned idx = (unsigned) as * MAX_MACHINE_MODE + (unsigned) mem_mode;
    4638                 :             :   if (idx >= ainc_cost_data_list.length ())
    4639                 :             :     {
    4640                 :             :       unsigned nsize = ((unsigned) as + 1) *MAX_MACHINE_MODE;
    4641                 :             : 
    4642                 :             :       gcc_assert (nsize > idx);
    4643                 :             :       ainc_cost_data_list.safe_grow_cleared (nsize, true);
    4644                 :             :     }
    4645                 :             : 
    4646                 :             :   ainc_cost_data *data = ainc_cost_data_list[idx];
    4647                 :             :   if (data == NULL)
    4648                 :             :     {
    4649                 :             :       rtx reg = gen_raw_REG (addr_mode, LAST_VIRTUAL_REGISTER + 1);
    4650                 :             : 
    4651                 :             :       data = (ainc_cost_data *) xcalloc (1, sizeof (*data));
    4652                 :             :       data->costs[AINC_PRE_DEC] = INFTY;
    4653                 :             :       data->costs[AINC_POST_DEC] = INFTY;
    4654                 :             :       data->costs[AINC_PRE_INC] = INFTY;
    4655                 :             :       data->costs[AINC_POST_INC] = INFTY;
    4656                 :             :       if (USE_LOAD_PRE_DECREMENT (mem_mode)
    4657                 :             :           || USE_STORE_PRE_DECREMENT (mem_mode))
    4658                 :             :         {
    4659                 :             :           rtx addr = gen_rtx_PRE_DEC (addr_mode, reg);
    4660                 :             : 
    4661                 :             :           if (memory_address_addr_space_p (mem_mode, addr, as))
    4662                 :             :             data->costs[AINC_PRE_DEC]
    4663                 :             :               = address_cost (addr, mem_mode, as, speed);
    4664                 :             :         }
    4665                 :             :       if (USE_LOAD_POST_DECREMENT (mem_mode)
    4666                 :             :           || USE_STORE_POST_DECREMENT (mem_mode))
    4667                 :             :         {
    4668                 :             :           rtx addr = gen_rtx_POST_DEC (addr_mode, reg);
    4669                 :             : 
    4670                 :             :           if (memory_address_addr_space_p (mem_mode, addr, as))
    4671                 :             :             data->costs[AINC_POST_DEC]
    4672                 :             :               = address_cost (addr, mem_mode, as, speed);
    4673                 :             :         }
    4674                 :             :       if (USE_LOAD_PRE_INCREMENT (mem_mode)
    4675                 :             :           || USE_STORE_PRE_INCREMENT (mem_mode))
    4676                 :             :         {
    4677                 :             :           rtx addr = gen_rtx_PRE_INC (addr_mode, reg);
    4678                 :             : 
    4679                 :             :           if (memory_address_addr_space_p (mem_mode, addr, as))
    4680                 :             :             data->costs[AINC_PRE_INC]
    4681                 :             :               = address_cost (addr, mem_mode, as, speed);
    4682                 :             :         }
    4683                 :             :       if (USE_LOAD_POST_INCREMENT (mem_mode)
    4684                 :             :           || USE_STORE_POST_INCREMENT (mem_mode))
    4685                 :             :         {
    4686                 :             :           rtx addr = gen_rtx_POST_INC (addr_mode, reg);
    4687                 :             : 
    4688                 :             :           if (memory_address_addr_space_p (mem_mode, addr, as))
    4689                 :             :             data->costs[AINC_POST_INC]
    4690                 :             :               = address_cost (addr, mem_mode, as, speed);
    4691                 :             :         }
    4692                 :             :       ainc_cost_data_list[idx] = data;
    4693                 :             :     }
    4694                 :             : 
    4695                 :             :   poly_int64 msize = GET_MODE_SIZE (mem_mode);
    4696                 :             :   if (known_eq (ainc_offset, 0) && known_eq (msize, ainc_step))
    4697                 :             :     return comp_cost (data->costs[AINC_POST_INC], 0);
    4698                 :             :   if (known_eq (ainc_offset, 0) && known_eq (msize, -ainc_step))
    4699                 :             :     return comp_cost (data->costs[AINC_POST_DEC], 0);
    4700                 :             :   if (known_eq (ainc_offset, msize) && known_eq (msize, ainc_step))
    4701                 :             :     return comp_cost (data->costs[AINC_PRE_INC], 0);
    4702                 :             :   if (known_eq (ainc_offset, -msize) && known_eq (msize, -ainc_step))
    4703                 :             :     return comp_cost (data->costs[AINC_PRE_DEC], 0);
    4704                 :             : 
    4705                 :             :   return infinite_cost;
    4706                 :             : }
    4707                 :             : 
    4708                 :             : /* Return cost of computing USE's address expression by using CAND.
    4709                 :             :    AFF_INV and AFF_VAR represent invariant and variant parts of the
    4710                 :             :    address expression, respectively.  If AFF_INV is simple, store
    4711                 :             :    the loop invariant variables which are depended by it in INV_VARS;
    4712                 :             :    if AFF_INV is complicated, handle it as a new invariant expression
    4713                 :             :    and record it in INV_EXPR.  RATIO indicates multiple times between
    4714                 :             :    steps of USE and CAND.  If CAN_AUTOINC is nonNULL, store boolean
    4715                 :             :    value to it indicating if this is an auto-increment address.  */
    4716                 :             : 
    4717                 :             : static comp_cost
    4718                 :     5114196 : get_address_cost (struct ivopts_data *data, struct iv_use *use,
    4719                 :             :                   struct iv_cand *cand, aff_tree *aff_inv,
    4720                 :             :                   aff_tree *aff_var, HOST_WIDE_INT ratio,
    4721                 :             :                   bitmap *inv_vars, iv_inv_expr_ent **inv_expr,
    4722                 :             :                   bool *can_autoinc, bool speed)
    4723                 :             : {
    4724                 :     5114196 :   rtx addr;
    4725                 :     5114196 :   bool simple_inv = true;
    4726                 :     5114196 :   tree comp_inv = NULL_TREE, type = aff_var->type;
    4727                 :     5114196 :   comp_cost var_cost = no_cost, cost = no_cost;
    4728                 :     5114196 :   struct mem_address parts = {NULL_TREE, integer_one_node,
    4729                 :     5114196 :                               NULL_TREE, NULL_TREE, NULL_TREE};
    4730                 :     5114196 :   machine_mode addr_mode = TYPE_MODE (type);
    4731                 :     5114196 :   machine_mode mem_mode = TYPE_MODE (use->mem_type);
    4732                 :     5114196 :   addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (use->iv->base));
    4733                 :             :   /* Only true if ratio != 1.  */
    4734                 :     5114196 :   bool ok_with_ratio_p = false;
    4735                 :     5114196 :   bool ok_without_ratio_p = false;
    4736                 :     5114196 :   code_helper code = ERROR_MARK;
    4737                 :             : 
    4738                 :     5114196 :   if (use->type == USE_PTR_ADDRESS)
    4739                 :             :     {
    4740                 :        2464 :       gcall *call = as_a<gcall *> (use->stmt);
    4741                 :        2464 :       gcc_assert (gimple_call_internal_p (call));
    4742                 :        2464 :       code = gimple_call_internal_fn (call);
    4743                 :             :     }
    4744                 :             : 
    4745                 :     5114196 :   if (!aff_combination_const_p (aff_inv))
    4746                 :             :     {
    4747                 :     3401024 :       parts.index = integer_one_node;
    4748                 :             :       /* Addressing mode "base + index".  */
    4749                 :     3401024 :       ok_without_ratio_p = valid_mem_ref_p (mem_mode, as, &parts, code);
    4750                 :     3401024 :       if (ratio != 1)
    4751                 :             :         {
    4752                 :     2386596 :           parts.step = wide_int_to_tree (type, ratio);
    4753                 :             :           /* Addressing mode "base + index << scale".  */
    4754                 :     2386596 :           ok_with_ratio_p = valid_mem_ref_p (mem_mode, as, &parts, code);
    4755                 :     2386596 :           if (!ok_with_ratio_p)
    4756                 :     1460195 :             parts.step = NULL_TREE;
    4757                 :             :         }
    4758                 :     3401024 :       if (ok_with_ratio_p || ok_without_ratio_p)
    4759                 :             :         {
    4760                 :     3401024 :           if (maybe_ne (aff_inv->offset, 0))
    4761                 :             :             {
    4762                 :     2165299 :               parts.offset = wide_int_to_tree (sizetype, aff_inv->offset);
    4763                 :             :               /* Addressing mode "base + index [<< scale] + offset".  */
    4764                 :     2165299 :               if (!valid_mem_ref_p (mem_mode, as, &parts, code))
    4765                 :         426 :                 parts.offset = NULL_TREE;
    4766                 :             :               else
    4767                 :     2164873 :                 aff_inv->offset = 0;
    4768                 :             :             }
    4769                 :             : 
    4770                 :     3401024 :           move_fixed_address_to_symbol (&parts, aff_inv);
    4771                 :             :           /* Base is fixed address and is moved to symbol part.  */
    4772                 :     3401024 :           if (parts.symbol != NULL_TREE && aff_combination_zero_p (aff_inv))
    4773                 :      391026 :             parts.base = NULL_TREE;
    4774                 :             : 
    4775                 :             :           /* Addressing mode "symbol + base + index [<< scale] [+ offset]".  */
    4776                 :     3401024 :           if (parts.symbol != NULL_TREE
    4777                 :     3401024 :               && !valid_mem_ref_p (mem_mode, as, &parts, code))
    4778                 :             :             {
    4779                 :        6704 :               aff_combination_add_elt (aff_inv, parts.symbol, 1);
    4780                 :        6704 :               parts.symbol = NULL_TREE;
    4781                 :             :               /* Reset SIMPLE_INV since symbol address needs to be computed
    4782                 :             :                  outside of address expression in this case.  */
    4783                 :        6704 :               simple_inv = false;
    4784                 :             :               /* Symbol part is moved back to base part, it can't be NULL.  */
    4785                 :        6704 :               parts.base = integer_one_node;
    4786                 :             :             }
    4787                 :             :         }
    4788                 :             :       else
    4789                 :           0 :         parts.index = NULL_TREE;
    4790                 :             :     }
    4791                 :             :   else
    4792                 :             :     {
    4793                 :     1713172 :       poly_int64 ainc_step;
    4794                 :     1713172 :       if (can_autoinc
    4795                 :     1713172 :           && ratio == 1
    4796                 :     3426336 :           && ptrdiff_tree_p (cand->iv->step, &ainc_step))
    4797                 :             :         {
    4798                 :     1652480 :           poly_int64 ainc_offset = (aff_inv->offset).force_shwi ();
    4799                 :             : 
    4800                 :     1652480 :           if (stmt_after_increment (data->current_loop, cand, use->stmt))
    4801                 :             :             ainc_offset += ainc_step;
    4802                 :     1652480 :           cost = get_address_cost_ainc (ainc_step, ainc_offset,
    4803                 :             :                                         addr_mode, mem_mode, as, speed);
    4804                 :     1652480 :           if (!cost.infinite_cost_p ())
    4805                 :             :             {
    4806                 :           0 :               *can_autoinc = true;
    4807                 :           0 :               return cost;
    4808                 :             :             }
    4809                 :     1652480 :           cost = no_cost;
    4810                 :             :         }
    4811                 :     1713172 :       if (!aff_combination_zero_p (aff_inv))
    4812                 :             :         {
    4813                 :     1027761 :           parts.offset = wide_int_to_tree (sizetype, aff_inv->offset);
    4814                 :             :           /* Addressing mode "base + offset".  */
    4815                 :     1027761 :           if (!valid_mem_ref_p (mem_mode, as, &parts, code))
    4816                 :          43 :             parts.offset = NULL_TREE;
    4817                 :             :           else
    4818                 :     1027718 :             aff_inv->offset = 0;
    4819                 :             :         }
    4820                 :             :     }
    4821                 :             : 
    4822                 :     1719876 :   if (simple_inv)
    4823                 :     5107492 :     simple_inv = (aff_inv == NULL
    4824                 :     8123310 :                   || aff_combination_const_p (aff_inv)
    4825                 :     8116606 :                   || aff_combination_singleton_var_p (aff_inv));
    4826                 :     5114196 :   if (!aff_combination_zero_p (aff_inv))
    4827                 :     3015910 :     comp_inv = aff_combination_to_tree (aff_inv);
    4828                 :     3015910 :   if (comp_inv != NULL_TREE)
    4829                 :     3015910 :     cost = force_var_cost (data, comp_inv, inv_vars);
    4830                 :     5114196 :   if (ratio != 1 && parts.step == NULL_TREE)
    4831                 :     1460203 :     var_cost += mult_by_coeff_cost (ratio, addr_mode, speed);
    4832                 :     5114196 :   if (comp_inv != NULL_TREE && parts.index == NULL_TREE)
    4833                 :          43 :     var_cost += add_cost (speed, addr_mode);
    4834                 :             : 
    4835                 :     5114196 :   if (comp_inv && inv_expr && !simple_inv)
    4836                 :             :     {
    4837                 :      717227 :       *inv_expr = get_loop_invariant_expr (data, comp_inv);
    4838                 :             :       /* Clear depends on.  */
    4839                 :      717227 :       if (*inv_expr != NULL && inv_vars && *inv_vars)
    4840                 :      391772 :         bitmap_clear (*inv_vars);
    4841                 :             : 
    4842                 :             :       /* Cost of small invariant expression adjusted against loop niters
    4843                 :             :          is usually zero, which makes it difficult to be differentiated
    4844                 :             :          from candidate based on loop invariant variables.  Secondly, the
    4845                 :             :          generated invariant expression may not be hoisted out of loop by
    4846                 :             :          following pass.  We penalize the cost by rounding up in order to
    4847                 :             :          neutralize such effects.  */
    4848                 :      717227 :       cost.cost = adjust_setup_cost (data, cost.cost, true);
    4849                 :      717227 :       cost.scratch = cost.cost;
    4850                 :             :     }
    4851                 :             : 
    4852                 :     5114196 :   cost += var_cost;
    4853                 :     5114196 :   addr = addr_for_mem_ref (&parts, as, false);
    4854                 :     5114196 :   gcc_assert (memory_address_addr_space_p (mem_mode, addr, as));
    4855                 :     5114196 :   cost += address_cost (addr, mem_mode, as, speed);
    4856                 :             : 
    4857                 :     5114196 :   if (parts.symbol != NULL_TREE)
    4858                 :      436613 :     cost.complexity += 1;
    4859                 :             :   /* Don't increase the complexity of adding a scaled index if it's
    4860                 :             :      the only kind of index that the target allows.  */
    4861                 :     5114196 :   if (parts.step != NULL_TREE && ok_without_ratio_p)
    4862                 :      926401 :     cost.complexity += 1;
    4863                 :     5114196 :   if (parts.base != NULL_TREE && parts.index != NULL_TREE)
    4864                 :     3015867 :     cost.complexity += 1;
    4865                 :     5114196 :   if (parts.offset != NULL_TREE && !integer_zerop (parts.offset))
    4866                 :     3192591 :     cost.complexity += 1;
    4867                 :             : 
    4868                 :             :   return cost;
    4869                 :             : }
    4870                 :             : 
    4871                 :             : /* Scale (multiply) the computed COST (except scratch part that should be
    4872                 :             :    hoisted out a loop) by header->frequency / AT->frequency, which makes
    4873                 :             :    expected cost more accurate.  */
    4874                 :             : 
    4875                 :             : static comp_cost
    4876                 :    11260488 : get_scaled_computation_cost_at (ivopts_data *data, gimple *at, comp_cost cost)
    4877                 :             : {
    4878                 :    11260488 :   if (data->speed
    4879                 :    11260488 :       && data->current_loop->header->count.to_frequency (cfun) > 0)
    4880                 :             :     {
    4881                 :     9780521 :       basic_block bb = gimple_bb (at);
    4882                 :     9780521 :       gcc_assert (cost.scratch <= cost.cost);
    4883                 :     9780521 :       int scale_factor = (int)(intptr_t) bb->aux;
    4884                 :     9780521 :       if (scale_factor == 1)
    4885                 :     9387859 :         return cost;
    4886                 :             : 
    4887                 :      392662 :       int64_t scaled_cost
    4888                 :      392662 :         = cost.scratch + (cost.cost - cost.scratch) * scale_factor;
    4889                 :             : 
    4890                 :      392662 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4891                 :         160 :         fprintf (dump_file, "Scaling cost based on bb prob by %2.2f: "
    4892                 :             :                  "%" PRId64 " (scratch: %" PRId64 ") -> %" PRId64 "\n",
    4893                 :             :                  1.0f * scale_factor, cost.cost, cost.scratch, scaled_cost);
    4894                 :             : 
    4895                 :             :       cost.cost = scaled_cost;
    4896                 :             :     }
    4897                 :             : 
    4898                 :     1872629 :   return cost;
    4899                 :             : }
    4900                 :             : 
    4901                 :             : /* Determines the cost of the computation by that USE is expressed
    4902                 :             :    from induction variable CAND.  If ADDRESS_P is true, we just need
    4903                 :             :    to create an address from it, otherwise we want to get it into
    4904                 :             :    register.  A set of invariants we depend on is stored in INV_VARS.
    4905                 :             :    If CAN_AUTOINC is nonnull, use it to record whether autoinc
    4906                 :             :    addressing is likely.  If INV_EXPR is nonnull, record invariant
    4907                 :             :    expr entry in it.  */
    4908                 :             : 
    4909                 :             : static comp_cost
    4910                 :    17798267 : get_computation_cost (struct ivopts_data *data, struct iv_use *use,
    4911                 :             :                       struct iv_cand *cand, bool address_p, bitmap *inv_vars,
    4912                 :             :                       bool *can_autoinc, iv_inv_expr_ent **inv_expr)
    4913                 :             : {
    4914                 :    17798267 :   gimple *at = use->stmt;
    4915                 :    17798267 :   tree ubase = use->iv->base, cbase = cand->iv->base;
    4916                 :    17798267 :   tree utype = TREE_TYPE (ubase), ctype = TREE_TYPE (cbase);
    4917                 :    17798267 :   tree comp_inv = NULL_TREE;
    4918                 :    17798267 :   HOST_WIDE_INT ratio, aratio;
    4919                 :    17798267 :   comp_cost cost;
    4920                 :    17798267 :   widest_int rat;
    4921                 :    35596534 :   aff_tree aff_inv, aff_var;
    4922                 :    17798267 :   bool speed = optimize_bb_for_speed_p (gimple_bb (at));
    4923                 :             : 
    4924                 :    17798267 :   if (inv_vars)
    4925                 :    15391344 :     *inv_vars = NULL;
    4926                 :    17798267 :   if (can_autoinc)
    4927                 :     7914672 :     *can_autoinc = false;
    4928                 :    17798267 :   if (inv_expr)
    4929                 :    17440863 :     *inv_expr = NULL;
    4930                 :             : 
    4931                 :             :   /* Check if we have enough precision to express the values of use.  */
    4932                 :    17798267 :   if (TYPE_PRECISION (utype) > TYPE_PRECISION (ctype))
    4933                 :     2640685 :     return infinite_cost;
    4934                 :             : 
    4935                 :    15157582 :   if (address_p
    4936                 :    15157582 :       || (use->iv->base_object
    4937                 :     1847730 :           && cand->iv->base_object
    4938                 :      878085 :           && POINTER_TYPE_P (TREE_TYPE (use->iv->base_object))
    4939                 :      877611 :           && POINTER_TYPE_P (TREE_TYPE (cand->iv->base_object))))
    4940                 :             :     {
    4941                 :             :       /* Do not try to express address of an object with computation based
    4942                 :             :          on address of a different object.  This may cause problems in rtl
    4943                 :             :          level alias analysis (that does not expect this to be happening,
    4944                 :             :          as this is illegal in C), and would be unlikely to be useful
    4945                 :             :          anyway.  */
    4946                 :     7162141 :       if (use->iv->base_object
    4947                 :     7162141 :           && cand->iv->base_object
    4948                 :    10915965 :           && !operand_equal_p (use->iv->base_object, cand->iv->base_object, 0))
    4949                 :     1188654 :         return infinite_cost;
    4950                 :             :     }
    4951                 :             : 
    4952                 :    13968928 :   if (!get_computation_aff_1 (data->current_loop, at, use,
    4953                 :             :                               cand, &aff_inv, &aff_var, &rat)
    4954                 :    13968928 :       || !wi::fits_shwi_p (rat))
    4955                 :     2708440 :     return infinite_cost;
    4956                 :             : 
    4957                 :    11260488 :   ratio = rat.to_shwi ();
    4958                 :    11260488 :   if (address_p)
    4959                 :             :     {
    4960                 :     5114196 :       cost = get_address_cost (data, use, cand, &aff_inv, &aff_var, ratio,
    4961                 :             :                                inv_vars, inv_expr, can_autoinc, speed);
    4962                 :     5114196 :       cost = get_scaled_computation_cost_at (data, at, cost);
    4963                 :             :       /* For doloop IV cand, add on the extra cost.  */
    4964                 :     5114196 :       cost += cand->doloop_p ? targetm.doloop_cost_for_address : 0;
    4965                 :     5114196 :       return cost;
    4966                 :             :     }
    4967                 :             : 
    4968                 :     6146292 :   bool simple_inv = (aff_combination_const_p (&aff_inv)
    4969                 :     1621272 :                      || aff_combination_singleton_var_p (&aff_inv));
    4970                 :     6146292 :   tree signed_type = signed_type_for (aff_combination_type (&aff_inv));
    4971                 :     6146292 :   aff_combination_convert (&aff_inv, signed_type);
    4972                 :     6146292 :   if (!aff_combination_zero_p (&aff_inv))
    4973                 :     4426941 :     comp_inv = aff_combination_to_tree (&aff_inv);
    4974                 :             : 
    4975                 :     6146292 :   cost = force_var_cost (data, comp_inv, inv_vars);
    4976                 :     6146292 :   if (comp_inv && inv_expr && !simple_inv)
    4977                 :             :     {
    4978                 :     1136761 :       *inv_expr = get_loop_invariant_expr (data, comp_inv);
    4979                 :             :       /* Clear depends on.  */
    4980                 :     1136761 :       if (*inv_expr != NULL && inv_vars && *inv_vars)
    4981                 :      699795 :         bitmap_clear (*inv_vars);
    4982                 :             : 
    4983                 :     1136761 :       cost.cost = adjust_setup_cost (data, cost.cost);
    4984                 :             :       /* Record setup cost in scratch field.  */
    4985                 :     1136761 :       cost.scratch = cost.cost;
    4986                 :             :     }
    4987                 :             :   /* Cost of constant integer can be covered when adding invariant part to
    4988                 :             :      variant part.  */
    4989                 :     5009531 :   else if (comp_inv && CONSTANT_CLASS_P (comp_inv))
    4990                 :     2805644 :     cost = no_cost;
    4991                 :             : 
    4992                 :             :   /* Need type narrowing to represent use with cand.  */
    4993                 :     6146292 :   if (TYPE_PRECISION (utype) < TYPE_PRECISION (ctype))
    4994                 :             :     {
    4995                 :      712915 :       machine_mode outer_mode = TYPE_MODE (utype);
    4996                 :      712915 :       machine_mode inner_mode = TYPE_MODE (ctype);
    4997                 :      712915 :       cost += comp_cost (convert_cost (outer_mode, inner_mode, speed), 0);
    4998                 :             :     }
    4999                 :             : 
    5000                 :             :   /* Turn a + i * (-c) into a - i * c.  */
    5001                 :     6146292 :   if (ratio < 0 && comp_inv && !integer_zerop (comp_inv))
    5002                 :     1540272 :     aratio = -ratio;
    5003                 :             :   else
    5004                 :             :     aratio = ratio;
    5005                 :             : 
    5006                 :     6146292 :   if (ratio != 1)
    5007                 :     2172109 :     cost += mult_by_coeff_cost (aratio, TYPE_MODE (utype), speed);
    5008                 :             : 
    5009                 :             :   /* TODO: We may also need to check if we can compute  a + i * 4 in one
    5010                 :             :      instruction.  */
    5011                 :             :   /* Need to add up the invariant and variant parts.  */
    5012                 :     6146292 :   if (comp_inv && !integer_zerop (comp_inv))
    5013                 :     8845502 :     cost += add_cost (speed, TYPE_MODE (utype));
    5014                 :             : 
    5015                 :     6146292 :   cost = get_scaled_computation_cost_at (data, at, cost);
    5016                 :             : 
    5017                 :             :   /* For doloop IV cand, add on the extra cost.  */
    5018                 :     6146292 :   if (cand->doloop_p && use->type == USE_NONLINEAR_EXPR)
    5019                 :           0 :     cost += targetm.doloop_cost_for_generic;
    5020                 :             : 
    5021                 :     6146292 :   return cost;
    5022                 :    17798267 : }
    5023                 :             : 
    5024                 :             : /* Determines cost of computing the use in GROUP with CAND in a generic
    5025                 :             :    expression.  */
    5026                 :             : 
    5027                 :             : static bool
    5028                 :     4825370 : determine_group_iv_cost_generic (struct ivopts_data *data,
    5029                 :             :                                  struct iv_group *group, struct iv_cand *cand)
    5030                 :             : {
    5031                 :     4825370 :   comp_cost cost;
    5032                 :     4825370 :   iv_inv_expr_ent *inv_expr = NULL;
    5033                 :     4825370 :   bitmap inv_vars = NULL, inv_exprs = NULL;
    5034                 :     4825370 :   struct iv_use *use = group->vuses[0];
    5035                 :             : 
    5036                 :             :   /* The simple case first -- if we need to express value of the preserved
    5037                 :             :      original biv, the cost is 0.  This also prevents us from counting the
    5038                 :             :      cost of increment twice -- once at this use and once in the cost of
    5039                 :             :      the candidate.  */
    5040                 :     4825370 :   if (cand->pos == IP_ORIGINAL && cand->incremented_at == use->stmt)
    5041                 :       44117 :     cost = no_cost;
    5042                 :             :   /* If the IV candidate involves undefined SSA values and is not the
    5043                 :             :      same IV as on the USE avoid using that candidate here.  */
    5044                 :     4781253 :   else if (cand->involves_undefs
    5045                 :     4781253 :            && (!use->iv || !operand_equal_p (cand->iv->base, use->iv->base, 0)))
    5046                 :         203 :     return false;
    5047                 :             :   else
    5048                 :     4781050 :     cost = get_computation_cost (data, use, cand, false,
    5049                 :             :                                  &inv_vars, NULL, &inv_expr);
    5050                 :             : 
    5051                 :     4825167 :   if (inv_expr)
    5052                 :             :     {
    5053                 :      777236 :       inv_exprs = BITMAP_ALLOC (NULL);
    5054                 :      777236 :       bitmap_set_bit (inv_exprs, inv_expr->id);
    5055                 :             :     }
    5056                 :     4825167 :   set_group_iv_cost (data, group, cand, cost, inv_vars,
    5057                 :             :                      NULL_TREE, ERROR_MARK, inv_exprs);
    5058                 :     4825167 :   return !cost.infinite_cost_p ();
    5059                 :             : }
    5060                 :             : 
    5061                 :             : /* Determines cost of computing uses in GROUP with CAND in addresses.  */
    5062                 :             : 
    5063                 :             : static bool
    5064                 :     5507749 : determine_group_iv_cost_address (struct ivopts_data *data,
    5065                 :             :                                  struct iv_group *group, struct iv_cand *cand)
    5066                 :             : {
    5067                 :     5507749 :   unsigned i;
    5068                 :     5507749 :   bitmap inv_vars = NULL, inv_exprs = NULL;
    5069                 :     5507749 :   bool can_autoinc;
    5070                 :     5507749 :   iv_inv_expr_ent *inv_expr = NULL;
    5071                 :     5507749 :   struct iv_use *use = group->vuses[0];
    5072                 :     5507749 :   comp_cost sum_cost = no_cost, cost;
    5073                 :             : 
    5074                 :     5507749 :   cost = get_computation_cost (data, use, cand, true,
    5075                 :             :                                &inv_vars, &can_autoinc, &inv_expr);
    5076                 :             : 
    5077                 :     5507749 :   if (inv_expr)
    5078                 :             :     {
    5079                 :      414796 :       inv_exprs = BITMAP_ALLOC (NULL);
    5080                 :      414796 :       bitmap_set_bit (inv_exprs, inv_expr->id);
    5081                 :             :     }
    5082                 :     5507749 :   sum_cost = cost;
    5083                 :     5507749 :   if (!sum_cost.infinite_cost_p () && cand->ainc_use == use)
    5084                 :             :     {
    5085                 :           0 :       if (can_autoinc)
    5086                 :           0 :         sum_cost -= cand->cost_step;
    5087                 :             :       /* If we generated the candidate solely for exploiting autoincrement
    5088                 :             :          opportunities, and it turns out it can't be used, set the cost to
    5089                 :             :          infinity to make sure we ignore it.  */
    5090                 :           0 :       else if (cand->pos == IP_AFTER_USE || cand->pos == IP_BEFORE_USE)
    5091                 :           0 :         sum_cost = infinite_cost;
    5092                 :             :     }
    5093                 :             : 
    5094                 :             :   /* Uses in a group can share setup code, so only add setup cost once.  */
    5095                 :     5507749 :   cost -= cost.scratch;
    5096                 :             :   /* Compute and add costs for rest uses of this group.  */
    5097                 :     7557268 :   for (i = 1; i < group->vuses.length () && !sum_cost.infinite_cost_p (); i++)
    5098                 :             :     {
    5099                 :     2049519 :       struct iv_use *next = group->vuses[i];
    5100                 :             : 
    5101                 :             :       /* TODO: We could skip computing cost for sub iv_use when it has the
    5102                 :             :          same cost as the first iv_use, but the cost really depends on the
    5103                 :             :          offset and where the iv_use is.  */
    5104                 :     2049519 :         cost = get_computation_cost (data, next, cand, true,
    5105                 :             :                                      NULL, &can_autoinc, &inv_expr);
    5106                 :     2049519 :         if (inv_expr)
    5107                 :             :           {
    5108                 :      301453 :             if (!inv_exprs)
    5109                 :         116 :               inv_exprs = BITMAP_ALLOC (NULL);
    5110                 :             : 
    5111                 :      301453 :             bitmap_set_bit (inv_exprs, inv_expr->id);
    5112                 :             :           }
    5113                 :     2049519 :       sum_cost += cost;
    5114                 :             :     }
    5115                 :     5507749 :   set_group_iv_cost (data, group, cand, sum_cost, inv_vars,
    5116                 :             :                      NULL_TREE, ERROR_MARK, inv_exprs);
    5117                 :             : 
    5118                 :     5507749 :   return !sum_cost.infinite_cost_p ();
    5119                 :             : }
    5120                 :             : 
    5121                 :             : /* Computes value of candidate CAND at position AT in iteration DESC->NITER,
    5122                 :             :    and stores it to VAL.  */
    5123                 :             : 
    5124                 :             : static void
    5125                 :     3240476 : cand_value_at (class loop *loop, struct iv_cand *cand, gimple *at,
    5126                 :             :                class tree_niter_desc *desc, aff_tree *val)
    5127                 :             : {
    5128                 :     9721428 :   aff_tree step, delta, nit;
    5129                 :     3240476 :   struct iv *iv = cand->iv;
    5130                 :     3240476 :   tree type = TREE_TYPE (iv->base);
    5131                 :     3240476 :   tree niter = desc->niter;
    5132                 :     3240476 :   bool after_adjust = stmt_after_increment (loop, cand, at);
    5133                 :     3240476 :   tree steptype;
    5134                 :             : 
    5135                 :     3240476 :   if (POINTER_TYPE_P (type))
    5136                 :       81153 :     steptype = sizetype;
    5137                 :             :   else
    5138                 :     3159323 :     steptype = unsigned_type_for (type);
    5139                 :             : 
    5140                 :             :   /* If AFTER_ADJUST is required, the code below generates the equivalent
    5141                 :             :      of BASE + NITER * STEP + STEP, when ideally we'd prefer the expression
    5142                 :             :      BASE + (NITER + 1) * STEP, especially when NITER is often of the form
    5143                 :             :      SSA_NAME - 1.  Unfortunately, guaranteeing that adding 1 to NITER
    5144                 :             :      doesn't overflow is tricky, so we peek inside the TREE_NITER_DESC
    5145                 :             :      class for common idioms that we know are safe.  */
    5146                 :     3240476 :   if (after_adjust
    5147                 :     3079659 :       && desc->control.no_overflow
    5148                 :     3072706 :       && integer_onep (desc->control.step)
    5149                 :      800250 :       && (desc->cmp == LT_EXPR
    5150                 :       28149 :           || desc->cmp == NE_EXPR)
    5151                 :     4040726 :       && TREE_CODE (desc->bound) == SSA_NAME)
    5152                 :             :     {
    5153                 :      400749 :       if (integer_onep (desc->control.base))
    5154                 :             :         {
    5155                 :      287732 :           niter = desc->bound;
    5156                 :      287732 :           after_adjust = false;
    5157                 :             :         }
    5158                 :      113017 :       else if (TREE_CODE (niter) == MINUS_EXPR
    5159                 :      113017 :                && integer_onep (TREE_OPERAND (niter, 1)))
    5160                 :             :         {
    5161                 :       64393 :           niter = TREE_OPERAND (niter, 0);
    5162                 :       64393 :           after_adjust = false;
    5163                 :             :         }
    5164                 :             :     }
    5165                 :             : 
    5166                 :     3240476 :   tree_to_aff_combination (iv->step, TREE_TYPE (iv->step), &step);
    5167                 :     3240476 :   aff_combination_convert (&step, steptype);
    5168                 :     3240476 :   tree_to_aff_combination (niter, TREE_TYPE (niter), &nit);
    5169                 :     3240476 :   aff_combination_convert (&nit, steptype);
    5170                 :     3240476 :   aff_combination_mult (&nit, &step, &delta);
    5171                 :     3240476 :   if (after_adjust)
    5172                 :     2727534 :     aff_combination_add (&delta, &step);
    5173                 :             : 
    5174                 :     3240476 :   tree_to_aff_combination (iv->base, type, val);
    5175                 :     3240476 :   if (!POINTER_TYPE_P (type))
    5176                 :     3159323 :     aff_combination_convert (val, steptype);
    5177                 :     3240476 :   aff_combination_add (val, &delta);
    5178                 :     3240476 : }
    5179                 :             : 
    5180                 :             : /* Returns period of induction variable iv.  */
    5181                 :             : 
    5182                 :             : static tree
    5183                 :     3485586 : iv_period (struct iv *iv)
    5184                 :             : {
    5185                 :     3485586 :   tree step = iv->step, period, type;
    5186                 :     3485586 :   tree pow2div;
    5187                 :             : 
    5188                 :     3485586 :   gcc_assert (step && TREE_CODE (step) == INTEGER_CST);
    5189                 :             : 
    5190                 :     3485586 :   type = unsigned_type_for (TREE_TYPE (step));
    5191                 :             :   /* Period of the iv is lcm (step, type_range)/step -1,
    5192                 :             :      i.e., N*type_range/step - 1. Since type range is power
    5193                 :             :      of two, N == (step >> num_of_ending_zeros_binary (step),
    5194                 :             :      so the final result is
    5195                 :             : 
    5196                 :             :        (type_range >> num_of_ending_zeros_binary (step)) - 1
    5197                 :             : 
    5198                 :             :   */
    5199                 :     3485586 :   pow2div = num_ending_zeros (step);
    5200                 :             : 
    5201                 :    10456758 :   period = build_low_bits_mask (type,
    5202                 :     3485586 :                                 (TYPE_PRECISION (type)
    5203                 :     3485586 :                                  - tree_to_uhwi (pow2div)));
    5204                 :             : 
    5205                 :     3485586 :   return period;
    5206                 :             : }
    5207                 :             : 
    5208                 :             : /* Returns the comparison operator used when eliminating the iv USE.  */
    5209                 :             : 
    5210                 :             : static enum tree_code
    5211                 :     3240476 : iv_elimination_compare (struct ivopts_data *data, struct iv_use *use)
    5212                 :             : {
    5213                 :     3240476 :   class loop *loop = data->current_loop;
    5214                 :     3240476 :   basic_block ex_bb;
    5215                 :     3240476 :   edge exit;
    5216                 :             : 
    5217                 :     3240476 :   ex_bb = gimple_bb (use->stmt);
    5218                 :     3240476 :   exit = EDGE_SUCC (ex_bb, 0);
    5219                 :     3240476 :   if (flow_bb_inside_loop_p (loop, exit->dest))
    5220                 :     2495383 :     exit = EDGE_SUCC (ex_bb, 1);
    5221                 :             : 
    5222                 :     3240476 :   return (exit->flags & EDGE_TRUE_VALUE ? EQ_EXPR : NE_EXPR);
    5223                 :             : }
    5224                 :             : 
    5225                 :             : /* Returns true if we can prove that BASE - OFFSET does not overflow.  For now,
    5226                 :             :    we only detect the situation that BASE = SOMETHING + OFFSET, where the
    5227                 :             :    calculation is performed in non-wrapping type.
    5228                 :             : 
    5229                 :             :    TODO: More generally, we could test for the situation that
    5230                 :             :          BASE = SOMETHING + OFFSET' and OFFSET is between OFFSET' and zero.
    5231                 :             :          This would require knowing the sign of OFFSET.  */
    5232                 :             : 
    5233                 :             : static bool
    5234                 :         357 : difference_cannot_overflow_p (struct ivopts_data *data, tree base, tree offset)
    5235                 :             : {
    5236                 :         357 :   enum tree_code code;
    5237                 :         357 :   tree e1, e2;
    5238                 :        1071 :   aff_tree aff_e1, aff_e2, aff_offset;
    5239                 :             : 
    5240                 :         357 :   if (!nowrap_type_p (TREE_TYPE (base)))
    5241                 :             :     return false;
    5242                 :             : 
    5243                 :         357 :   base = expand_simple_operations (base);
    5244                 :             : 
    5245                 :         357 :   if (TREE_CODE (base) == SSA_NAME)
    5246                 :             :     {
    5247                 :         357 :       gimple *stmt = SSA_NAME_DEF_STMT (base);
    5248                 :             : 
    5249                 :         357 :       if (gimple_code (stmt) != GIMPLE_ASSIGN)
    5250                 :             :         return false;
    5251                 :             : 
    5252                 :           6 :       code = gimple_assign_rhs_code (stmt);
    5253                 :           6 :       if (get_gimple_rhs_class (code) != GIMPLE_BINARY_RHS)
    5254                 :             :         return false;
    5255                 :             : 
    5256                 :           5 :       e1 = gimple_assign_rhs1 (stmt);
    5257                 :           5 :       e2 = gimple_assign_rhs2 (stmt);
    5258                 :             :     }
    5259                 :             :   else
    5260                 :             :     {
    5261                 :           0 :       code = TREE_CODE (base);
    5262                 :           0 :       if (get_gimple_rhs_class (code) != GIMPLE_BINARY_RHS)
    5263                 :             :         return false;
    5264                 :           0 :       e1 = TREE_OPERAND (base, 0);
    5265                 :           0 :       e2 = TREE_OPERAND (base, 1);
    5266                 :             :     }
    5267                 :             : 
    5268                 :             :   /* Use affine expansion as deeper inspection to prove the equality.  */
    5269                 :           5 :   tree_to_aff_combination_expand (e2, TREE_TYPE (e2),
    5270                 :             :                                   &aff_e2, &data->name_expansion_cache);
    5271                 :           5 :   tree_to_aff_combination_expand (offset, TREE_TYPE (offset),
    5272                 :             :                                   &aff_offset, &data->name_expansion_cache);
    5273                 :           5 :   aff_combination_scale (&aff_offset, -1);
    5274                 :           5 :   switch (code)
    5275                 :             :     {
    5276                 :           3 :     case PLUS_EXPR:
    5277                 :           3 :       aff_combination_add (&aff_e2, &aff_offset);
    5278                 :           3 :       if (aff_combination_zero_p (&aff_e2))
    5279                 :             :         return true;
    5280                 :             : 
    5281                 :           1 :       tree_to_aff_combination_expand (e1, TREE_TYPE (e1),
    5282                 :             :                                       &aff_e1, &data->name_expansion_cache);
    5283                 :           1 :       aff_combination_add (&aff_e1, &aff_offset);
    5284                 :           1 :       return aff_combination_zero_p (&aff_e1);
    5285                 :             : 
    5286                 :           2 :     case POINTER_PLUS_EXPR:
    5287                 :           2 :       aff_combination_add (&aff_e2, &aff_offset);
    5288                 :           2 :       return aff_combination_zero_p (&aff_e2);
    5289                 :             : 
    5290                 :             :     default:
    5291                 :             :       return false;
    5292                 :             :     }
    5293                 :         357 : }
    5294                 :             : 
    5295                 :             : /* Tries to replace loop exit by one formulated in terms of a LT_EXPR
    5296                 :             :    comparison with CAND.  NITER describes the number of iterations of
    5297                 :             :    the loops.  If successful, the comparison in COMP_P is altered accordingly.
    5298                 :             : 
    5299                 :             :    We aim to handle the following situation:
    5300                 :             : 
    5301                 :             :    sometype *base, *p;
    5302                 :             :    int a, b, i;
    5303                 :             : 
    5304                 :             :    i = a;
    5305                 :             :    p = p_0 = base + a;
    5306                 :             : 
    5307                 :             :    do
    5308                 :             :      {
    5309                 :             :        bla (*p);
    5310                 :             :        p++;
    5311                 :             :        i++;
    5312                 :             :      }
    5313                 :             :    while (i < b);
    5314                 :             : 
    5315                 :             :    Here, the number of iterations of the loop is (a + 1 > b) ? 0 : b - a - 1.
    5316                 :             :    We aim to optimize this to
    5317                 :             : 
    5318                 :             :    p = p_0 = base + a;
    5319                 :             :    do
    5320                 :             :      {
    5321                 :             :        bla (*p);
    5322                 :             :        p++;
    5323                 :             :      }
    5324                 :             :    while (p < p_0 - a + b);
    5325                 :             : 
    5326                 :             :    This preserves the correctness, since the pointer arithmetics does not
    5327                 :             :    overflow.  More precisely:
    5328                 :             : 
    5329                 :             :    1) if a + 1 <= b, then p_0 - a + b is the final value of p, hence there is no
    5330                 :             :       overflow in computing it or the values of p.
    5331                 :             :    2) if a + 1 > b, then we need to verify that the expression p_0 - a does not
    5332                 :             :       overflow.  To prove this, we use the fact that p_0 = base + a.  */
    5333                 :             : 
    5334                 :             : static bool
    5335                 :      172482 : iv_elimination_compare_lt (struct ivopts_data *data,
    5336                 :             :                            struct iv_cand *cand, enum tree_code *comp_p,
    5337                 :             :                            class tree_niter_desc *niter)
    5338                 :             : {
    5339                 :      172482 :   tree cand_type, a, b, mbz, nit_type = TREE_TYPE (niter->niter), offset;
    5340                 :      517446 :   class aff_tree nit, tmpa, tmpb;
    5341                 :      172482 :   enum tree_code comp;
    5342                 :      172482 :   HOST_WIDE_INT step;
    5343                 :             : 
    5344                 :             :   /* We need to know that the candidate induction variable does not overflow.
    5345                 :             :      While more complex analysis may be used to prove this, for now just
    5346                 :             :      check that the variable appears in the original program and that it
    5347                 :             :      is computed in a type that guarantees no overflows.  */
    5348                 :      172482 :   cand_type = TREE_TYPE (cand->iv->base);
    5349                 :      172482 :   if (cand->pos != IP_ORIGINAL || !nowrap_type_p (cand_type))
    5350                 :      154730 :     return false;
    5351                 :             : 
    5352                 :             :   /* Make sure that the loop iterates till the loop bound is hit, as otherwise
    5353                 :             :      the calculation of the BOUND could overflow, making the comparison
    5354                 :             :      invalid.  */
    5355                 :       17752 :   if (!data->loop_single_exit_p)
    5356                 :             :     return false;
    5357                 :             : 
    5358                 :             :   /* We need to be able to decide whether candidate is increasing or decreasing
    5359                 :             :      in order to choose the right comparison operator.  */
    5360                 :       11063 :   if (!cst_and_fits_in_hwi (cand->iv->step))
    5361                 :             :     return false;
    5362                 :       11063 :   step = int_cst_value (cand->iv->step);
    5363                 :             : 
    5364                 :             :   /* Check that the number of iterations matches the expected pattern:
    5365                 :             :      a + 1 > b ? 0 : b - a - 1.  */
    5366                 :       11063 :   mbz = niter->may_be_zero;
    5367                 :       11063 :   if (TREE_CODE (mbz) == GT_EXPR)
    5368                 :             :     {
    5369                 :             :       /* Handle a + 1 > b.  */
    5370                 :        1307 :       tree op0 = TREE_OPERAND (mbz, 0);
    5371                 :        1307 :       if (TREE_CODE (op0) == PLUS_EXPR && integer_onep (TREE_OPERAND (op0, 1)))
    5372                 :             :         {
    5373                 :         622 :           a = TREE_OPERAND (op0, 0);
    5374                 :         622 :           b = TREE_OPERAND (mbz, 1);
    5375                 :             :         }
    5376                 :             :       else
    5377                 :         685 :         return false;
    5378                 :             :     }
    5379                 :        9756 :   else if (TREE_CODE (mbz) == LT_EXPR)
    5380                 :             :     {
    5381                 :        3765 :       tree op1 = TREE_OPERAND (mbz, 1);
    5382                 :             : 
    5383                 :             :       /* Handle b < a + 1.  */
    5384                 :        3765 :       if (TREE_CODE (op1) == PLUS_EXPR && integer_onep (TREE_OPERAND (op1, 1)))
    5385                 :             :         {
    5386                 :           0 :           a = TREE_OPERAND (op1, 0);
    5387                 :           0 :           b = TREE_OPERAND (mbz, 0);
    5388                 :             :         }
    5389                 :             :       else
    5390                 :        3765 :         return false;
    5391                 :             :     }
    5392                 :             :   else
    5393                 :             :     return false;
    5394                 :             : 
    5395                 :             :   /* Expected number of iterations is B - A - 1.  Check that it matches
    5396                 :             :      the actual number, i.e., that B - A - NITER = 1.  */
    5397                 :         622 :   tree_to_aff_combination (niter->niter, nit_type, &nit);
    5398                 :         622 :   tree_to_aff_combination (fold_convert (nit_type, a), nit_type, &tmpa);
    5399                 :         622 :   tree_to_aff_combination (fold_convert (nit_type, b), nit_type, &tmpb);
    5400                 :         622 :   aff_combination_scale (&nit, -1);
    5401                 :         622 :   aff_combination_scale (&tmpa, -1);
    5402                 :         622 :   aff_combination_add (&tmpb, &tmpa);
    5403                 :         622 :   aff_combination_add (&tmpb, &nit);
    5404                 :         622 :   if (tmpb.n != 0 || maybe_ne (tmpb.offset, 1))
    5405                 :         265 :     return false;
    5406                 :             : 
    5407                 :             :   /* Finally, check that CAND->IV->BASE - CAND->IV->STEP * A does not
    5408                 :             :      overflow.  */
    5409                 :         357 :   offset = fold_build2 (MULT_EXPR, TREE_TYPE (cand->iv->step),
    5410                 :             :                         cand->iv->step,
    5411                 :             :                         fold_convert (TREE_TYPE (cand->iv->step), a));
    5412                 :         357 :   if (!difference_cannot_overflow_p (data, cand->iv->base, offset))
    5413                 :             :     return false;
    5414                 :             : 
    5415                 :             :   /* Determine the new comparison operator.  */
    5416                 :           4 :   comp = step < 0 ? GT_EXPR : LT_EXPR;
    5417                 :           4 :   if (*comp_p == NE_EXPR)
    5418                 :           4 :     *comp_p = comp;
    5419                 :           0 :   else if (*comp_p == EQ_EXPR)
    5420                 :           0 :     *comp_p = invert_tree_comparison (comp, false);
    5421                 :             :   else
    5422                 :           0 :     gcc_unreachable ();
    5423                 :             : 
    5424                 :             :   return true;
    5425                 :      172482 : }
    5426                 :             : 
    5427                 :             : /* Check whether it is possible to express the condition in USE by comparison
    5428                 :             :    of candidate CAND.  If so, store the value compared with to BOUND, and the
    5429                 :             :    comparison operator to COMP.  */
    5430                 :             : 
    5431                 :             : static bool
    5432                 :     4246856 : may_eliminate_iv (struct ivopts_data *data,
    5433                 :             :                   struct iv_use *use, struct iv_cand *cand, tree *bound,
    5434                 :             :                   enum tree_code *comp)
    5435                 :             : {
    5436                 :     4246856 :   basic_block ex_bb;
    5437                 :     4246856 :   edge exit;
    5438                 :     4246856 :   tree period;
    5439                 :     4246856 :   class loop *loop = data->current_loop;
    5440                 :     4246856 :   aff_tree bnd;
    5441                 :     4246856 :   class tree_niter_desc *desc = NULL;
    5442                 :             : 
    5443                 :     4246856 :   if (TREE_CODE (cand->iv->step) != INTEGER_CST)
    5444                 :             :     return false;
    5445                 :             : 
    5446                 :             :   /* For now works only for exits that dominate the loop latch.
    5447                 :             :      TODO: extend to other conditions inside loop body.  */
    5448                 :     4056757 :   ex_bb = gimple_bb (use->stmt);
    5449                 :     4056757 :   if (use->stmt != last_nondebug_stmt (ex_bb)
    5450                 :     3933054 :       || gimple_code (use->stmt) != GIMPLE_COND
    5451                 :     7987566 :       || !dominated_by_p (CDI_DOMINATORS, loop->latch, ex_bb))
    5452                 :      224865 :     return false;
    5453                 :             : 
    5454                 :     3831892 :   exit = EDGE_SUCC (ex_bb, 0);
    5455                 :     3831892 :   if (flow_bb_inside_loop_p (loop, exit->dest))
    5456                 :     2928135 :     exit = EDGE_SUCC (ex_bb, 1);
    5457                 :     3831892 :   if (flow_bb_inside_loop_p (loop, exit->dest))
    5458                 :             :     return false;
    5459                 :             : 
    5460                 :     3729387 :   desc = niter_for_exit (data, exit);
    5461                 :     3729387 :   if (!desc)
    5462                 :             :     return false;
    5463                 :             : 
    5464                 :             :   /* Determine whether we can use the variable to test the exit condition.
    5465                 :             :      This is the case iff the period of the induction variable is greater
    5466                 :             :      than the number of iterations for which the exit condition is true.  */
    5467                 :     3485586 :   period = iv_period (cand->iv);
    5468                 :             : 
    5469                 :             :   /* If the number of iterations is constant, compare against it directly.  */
    5470                 :     3485586 :   if (TREE_CODE (desc->niter) == INTEGER_CST)
    5471                 :             :     {
    5472                 :             :       /* See cand_value_at.  */
    5473                 :     2294525 :       if (stmt_after_increment (loop, cand, use->stmt))
    5474                 :             :         {
    5475                 :     2246590 :           if (!tree_int_cst_lt (desc->niter, period))
    5476                 :             :             return false;
    5477                 :             :         }
    5478                 :             :       else
    5479                 :             :         {
    5480                 :       47935 :           if (tree_int_cst_lt (period, desc->niter))
    5481                 :             :             return false;
    5482                 :             :         }
    5483                 :             :     }
    5484                 :             : 
    5485                 :             :   /* If not, and if this is the only possible exit of the loop, see whether
    5486                 :             :      we can get a conservative estimate on the number of iterations of the
    5487                 :             :      entire loop and compare against that instead.  */
    5488                 :             :   else
    5489                 :             :     {
    5490                 :     1191061 :       widest_int period_value, max_niter;
    5491                 :             : 
    5492                 :     1191061 :       max_niter = desc->max;
    5493                 :     1191061 :       if (stmt_after_increment (loop, cand, use->stmt))
    5494                 :     1018164 :         max_niter += 1;
    5495                 :     1191061 :       period_value = wi::to_widest (period);
    5496                 :     1191061 :       if (wi::gtu_p (max_niter, period_value))
    5497                 :             :         {
    5498                 :             :           /* See if we can take advantage of inferred loop bound
    5499                 :             :              information.  */
    5500                 :      414636 :           if (data->loop_single_exit_p)
    5501                 :             :             {
    5502                 :      239098 :               if (!max_loop_iterations (loop, &max_niter))
    5503                 :             :                 return false;
    5504                 :             :               /* The loop bound is already adjusted by adding 1.  */
    5505                 :      239098 :               if (wi::gtu_p (max_niter, period_value))
    5506                 :             :                 return false;
    5507                 :             :             }
    5508                 :             :           else
    5509                 :             :             return false;
    5510                 :             :         }
    5511                 :     1191061 :     }
    5512                 :             : 
    5513                 :             :   /* For doloop IV cand, the bound would be zero.  It's safe whether
    5514                 :             :      may_be_zero set or not.  */
    5515                 :     3240476 :   if (cand->doloop_p)
    5516                 :             :     {
    5517                 :           0 :       *bound = build_int_cst (TREE_TYPE (cand->iv->base), 0);
    5518                 :           0 :       *comp = iv_elimination_compare (data, use);
    5519                 :           0 :       return true;
    5520                 :             :     }
    5521                 :             : 
    5522                 :     3240476 :   cand_value_at (loop, cand, use->stmt, desc, &bnd);
    5523                 :             : 
    5524                 :     3240476 :   *bound = fold_convert (TREE_TYPE (cand->iv->base),
    5525                 :             :                          aff_combination_to_tree (&bnd));
    5526                 :     3240476 :   *comp = iv_elimination_compare (data, use);
    5527                 :             : 
    5528                 :             :   /* It is unlikely that computing the number of iterations using division
    5529                 :             :      would be more profitable than keeping the original induction variable.  */
    5530                 :     3240476 :   bool cond_overflow_p;
    5531                 :     3240476 :   if (expression_expensive_p (*bound, &cond_overflow_p))
    5532                 :             :     return false;
    5533                 :             : 
    5534                 :             :   /* Sometimes, it is possible to handle the situation that the number of
    5535                 :             :      iterations may be zero unless additional assumptions by using <
    5536                 :             :      instead of != in the exit condition.
    5537                 :             : 
    5538                 :             :      TODO: we could also calculate the value MAY_BE_ZERO ? 0 : NITER and
    5539                 :             :            base the exit condition on it.  However, that is often too
    5540                 :             :            expensive.  */
    5541                 :     3232812 :   if (!integer_zerop (desc->may_be_zero))
    5542                 :      172482 :     return iv_elimination_compare_lt (data, cand, comp, desc);
    5543                 :             : 
    5544                 :             :   return true;
    5545                 :     4246856 : }
    5546                 :             : 
    5547                 :             :  /* Calculates the cost of BOUND, if it is a PARM_DECL.  A PARM_DECL must
    5548                 :             :     be copied, if it is used in the loop body and DATA->body_includes_call.  */
    5549                 :             : 
    5550                 :             : static int
    5551                 :     7187785 : parm_decl_cost (struct ivopts_data *data, tree bound)
    5552                 :             : {
    5553                 :     7187785 :   tree sbound = bound;
    5554                 :     7187785 :   STRIP_NOPS (sbound);
    5555                 :             : 
    5556                 :     7187785 :   if (TREE_CODE (sbound) == SSA_NAME
    5557                 :     2461012 :       && SSA_NAME_IS_DEFAULT_DEF (sbound)
    5558                 :      146609 :       && TREE_CODE (SSA_NAME_VAR (sbound)) == PARM_DECL
    5559                 :     7332237 :       && data->body_includes_call)
    5560                 :       40860 :     return COSTS_N_INSNS (1);
    5561                 :             : 
    5562                 :             :   return 0;
    5563                 :             : }
    5564                 :             : 
    5565                 :             : /* Determines cost of computing the use in GROUP with CAND in a condition.  */
    5566                 :             : 
    5567                 :             : static bool
    5568                 :     5102545 : determine_group_iv_cost_cond (struct ivopts_data *data,
    5569                 :             :                               struct iv_group *group, struct iv_cand *cand)
    5570                 :             : {
    5571                 :     5102545 :   tree bound = NULL_TREE;
    5572                 :     5102545 :   struct iv *cmp_iv;
    5573                 :     5102545 :   bitmap inv_exprs = NULL;
    5574                 :     5102545 :   bitmap inv_vars_elim = NULL, inv_vars_express = NULL, inv_vars;
    5575                 :     5102545 :   comp_cost elim_cost = infinite_cost, express_cost, cost, bound_cost;
    5576                 :     5102545 :   enum comp_iv_rewrite rewrite_type;
    5577                 :     5102545 :   iv_inv_expr_ent *inv_expr_elim = NULL, *inv_expr_express = NULL, *inv_expr;
    5578                 :     5102545 :   tree *control_var, *bound_cst;
    5579                 :     5102545 :   enum tree_code comp = ERROR_MARK;
    5580                 :     5102545 :   struct iv_use *use = group->vuses[0];
    5581                 :             : 
    5582                 :             :   /* Extract condition operands.  */
    5583                 :     5102545 :   rewrite_type = extract_cond_operands (data, use->stmt, &control_var,
    5584                 :             :                                         &bound_cst, NULL, &cmp_iv);
    5585                 :     5102545 :   gcc_assert (rewrite_type != COMP_IV_NA);
    5586                 :             : 
    5587                 :             :   /* Try iv elimination.  */
    5588                 :     5102545 :   if (rewrite_type == COMP_IV_ELIM
    5589                 :     5102545 :       && may_eliminate_iv (data, use, cand, &bound, &comp))
    5590                 :             :     {
    5591                 :     3060334 :       elim_cost = force_var_cost (data, bound, &inv_vars_elim);
    5592                 :     3060334 :       if (elim_cost.cost == 0)
    5593                 :     2112358 :         elim_cost.cost = parm_decl_cost (data, bound);
    5594                 :      947976 :       else if (TREE_CODE (bound) == INTEGER_CST)
    5595                 :           0 :         elim_cost.cost = 0;
    5596                 :             :       /* If we replace a loop condition 'i < n' with 'p < base + n',
    5597                 :             :          inv_vars_elim will have 'base' and 'n' set, which implies that both
    5598                 :             :          'base' and 'n' will be live during the loop.    More likely,
    5599                 :             :          'base + n' will be loop invariant, resulting in only one live value
    5600                 :             :          during the loop.  So in that case we clear inv_vars_elim and set
    5601                 :             :          inv_expr_elim instead.  */
    5602                 :     3060334 :       if (inv_vars_elim && bitmap_count_bits (inv_vars_elim) > 1)
    5603                 :             :         {
    5604                 :      241417 :           inv_expr_elim = get_loop_invariant_expr (data, bound);
    5605                 :      241417 :           bitmap_clear (inv_vars_elim);
    5606                 :             :         }
    5607                 :             :       /* The bound is a loop invariant, so it will be only computed
    5608                 :             :          once.  */
    5609                 :     3060334 :       elim_cost.cost = adjust_setup_cost (data, elim_cost.cost);
    5610                 :             :     }
    5611                 :             : 
    5612                 :             :   /* When the condition is a comparison of the candidate IV against
    5613                 :             :      zero, prefer this IV.
    5614                 :             : 
    5615                 :             :      TODO: The constant that we're subtracting from the cost should
    5616                 :             :      be target-dependent.  This information should be added to the
    5617                 :             :      target costs for each backend.  */
    5618                 :     5102545 :   if (!elim_cost.infinite_cost_p () /* Do not try to decrease infinite! */
    5619                 :     3060334 :       && integer_zerop (*bound_cst)
    5620                 :     7335141 :       && (operand_equal_p (*control_var, cand->var_after, 0)
    5621                 :     2016323 :           || operand_equal_p (*control_var, cand->var_before, 0)))
    5622                 :      220377 :     elim_cost -= 1;
    5623                 :             : 
    5624                 :     5102545 :   express_cost = get_computation_cost (data, use, cand, false,
    5625                 :             :                                        &inv_vars_express, NULL,
    5626                 :             :                                        &inv_expr_express);
    5627                 :     5102545 :   if (cmp_iv != NULL)
    5628                 :     4307044 :     find_inv_vars (data, &cmp_iv->base, &inv_vars_express);
    5629                 :             : 
    5630                 :             :   /* Count the cost of the original bound as well.  */
    5631                 :     5102545 :   bound_cost = force_var_cost (data, *bound_cst, NULL);
    5632                 :     5102545 :   if (bound_cost.cost == 0)
    5633                 :     5075427 :     bound_cost.cost = parm_decl_cost (data, *bound_cst);
    5634                 :       27118 :   else if (TREE_CODE (*bound_cst) == INTEGER_CST)
    5635                 :           0 :     bound_cost.cost = 0;
    5636                 :     5102545 :   express_cost += bound_cost;
    5637                 :             : 
    5638                 :             :   /* Choose the better approach, preferring the eliminated IV. */
    5639                 :     5102545 :   if (elim_cost <= express_cost)
    5640                 :             :     {
    5641                 :     3877391 :       cost = elim_cost;
    5642                 :     3877391 :       inv_vars = inv_vars_elim;
    5643                 :     3877391 :       inv_vars_elim = NULL;
    5644                 :     3877391 :       inv_expr = inv_expr_elim;
    5645                 :             :       /* For doloop candidate/use pair, adjust to zero cost.  */
    5646                 :     3877391 :       if (group->doloop_p && cand->doloop_p && elim_cost.cost > no_cost.cost)
    5647                 :           0 :         cost = no_cost;
    5648                 :             :     }
    5649                 :             :   else
    5650                 :             :     {
    5651                 :     1225154 :       cost = express_cost;
    5652                 :     1225154 :       inv_vars = inv_vars_express;
    5653                 :     1225154 :       inv_vars_express = NULL;
    5654                 :     1225154 :       bound = NULL_TREE;
    5655                 :     1225154 :       comp = ERROR_MARK;
    5656                 :     1225154 :       inv_expr = inv_expr_express;
    5657                 :             :     }
    5658                 :             : 
    5659                 :     5102545 :   if (inv_expr)
    5660                 :             :     {
    5661                 :      495143 :       inv_exprs = BITMAP_ALLOC (NULL);
    5662                 :      495143 :       bitmap_set_bit (inv_exprs, inv_expr->id);
    5663                 :             :     }
    5664                 :     5102545 :   set_group_iv_cost (data, group, cand, cost,
    5665                 :             :                      inv_vars, bound, comp, inv_exprs);
    5666                 :             : 
    5667                 :     5102545 :   if (inv_vars_elim)
    5668                 :       17547 :     BITMAP_FREE (inv_vars_elim);
    5669                 :     5102545 :   if (inv_vars_express)
    5670                 :     1066662 :     BITMAP_FREE (inv_vars_express);
    5671                 :             : 
    5672                 :     5102545 :   return !cost.infinite_cost_p ();
    5673                 :             : }
    5674                 :             : 
    5675                 :             : /* Determines cost of computing uses in GROUP with CAND.  Returns false
    5676                 :             :    if USE cannot be represented with CAND.  */
    5677                 :             : 
    5678                 :             : static bool
    5679                 :    15435664 : determine_group_iv_cost (struct ivopts_data *data,
    5680                 :             :                          struct iv_group *group, struct iv_cand *cand)
    5681                 :             : {
    5682                 :    15435664 :   switch (group->type)
    5683                 :             :     {
    5684                 :     4825370 :     case USE_NONLINEAR_EXPR:
    5685                 :     4825370 :       return determine_group_iv_cost_generic (data, group, cand);
    5686                 :             : 
    5687                 :     5507749 :     case USE_REF_ADDRESS:
    5688                 :     5507749 :     case USE_PTR_ADDRESS:
    5689                 :     5507749 :       return determine_group_iv_cost_address (data, group, cand);
    5690                 :             : 
    5691                 :     5102545 :     case USE_COMPARE:
    5692                 :     5102545 :       return determine_group_iv_cost_cond (data, group, cand);
    5693                 :             : 
    5694                 :           0 :     default:
    5695                 :           0 :       gcc_unreachable ();
    5696                 :             :     }
    5697                 :             : }
    5698                 :             : 
    5699                 :             : /* Return true if get_computation_cost indicates that autoincrement is
    5700                 :             :    a possibility for the pair of USE and CAND, false otherwise.  */
    5701                 :             : 
    5702                 :             : static bool
    5703                 :     1082389 : autoinc_possible_for_pair (struct ivopts_data *data, struct iv_use *use,
    5704                 :             :                            struct iv_cand *cand)
    5705                 :             : {
    5706                 :     1082389 :   if (!address_p (use->type))
    5707                 :             :     return false;
    5708                 :             : 
    5709                 :      357404 :   bool can_autoinc = false;
    5710                 :      357404 :   get_computation_cost (data, use, cand, true, NULL, &can_autoinc, NULL);
    5711                 :      357404 :   return can_autoinc;
    5712                 :             : }
    5713                 :             : 
    5714                 :             : /* Examine IP_ORIGINAL candidates to see if they are incremented next to a
    5715                 :             :    use that allows autoincrement, and set their AINC_USE if possible.  */
    5716                 :             : 
    5717                 :             : static void
    5718                 :      427980 : set_autoinc_for_original_candidates (struct ivopts_data *data)
    5719                 :             : {
    5720                 :      427980 :   unsigned i, j;
    5721                 :             : 
    5722                 :     8819388 :   for (i = 0; i < data->vcands.length (); i++)
    5723                 :             :     {
    5724                 :     3981714 :       struct iv_cand *cand = data->vcands[i];
    5725                 :     3981714 :       struct iv_use *closest_before = NULL;
    5726                 :     3981714 :       struct iv_use *closest_after = NULL;
    5727                 :     3981714 :       if (cand->pos != IP_ORIGINAL)
    5728                 :     3250217 :         continue;
    5729                 :             : 
    5730                 :     6361922 :       for (j = 0; j < data->vgroups.length (); j++)
    5731                 :             :         {
    5732                 :     2449464 :           struct iv_group *group = data->vgroups[j];
    5733                 :     2449464 :           struct iv_use *use = group->vuses[0];
    5734                 :     2449464 :           unsigned uid = gimple_uid (use->stmt);
    5735                 :             : 
    5736                 :     2449464 :           if (gimple_bb (use->stmt) != gimple_bb (cand->incremented_at))
    5737                 :      975813 :             continue;
    5738                 :             : 
    5739                 :     1473651 :           if (uid < gimple_uid (cand->incremented_at)
    5740                 :     1473651 :               && (closest_before == NULL
    5741                 :      290229 :                   || uid > gimple_uid (closest_before->stmt)))
    5742                 :             :             closest_before = use;
    5743                 :             : 
    5744                 :     1473651 :           if (uid > gimple_uid (cand->incremented_at)
    5745                 :     1473651 :               && (closest_after == NULL
    5746                 :       56916 :                   || uid < gimple_uid (closest_after->stmt)))
    5747                 :             :             closest_after = use;
    5748                 :             :         }
    5749                 :             : 
    5750                 :      731497 :       if (closest_before != NULL
    5751                 :      731497 :           && autoinc_possible_for_pair (data, closest_before, cand))
    5752                 :           0 :         cand->ainc_use = closest_before;
    5753                 :      731497 :       else if (closest_after != NULL
    5754                 :      731497 :                && autoinc_possible_for_pair (data, closest_after, cand))
    5755                 :           0 :         cand->ainc_use = closest_after;
    5756                 :             :     }
    5757                 :      427980 : }
    5758                 :             : 
    5759                 :             : /* Relate compare use with all candidates.  */
    5760                 :             : 
    5761                 :             : static void
    5762                 :         345 : relate_compare_use_with_all_cands (struct ivopts_data *data)
    5763                 :             : {
    5764                 :         345 :   unsigned i, count = data->vcands.length ();
    5765                 :       23614 :   for (i = 0; i < data->vgroups.length (); i++)
    5766                 :             :     {
    5767                 :       11462 :       struct iv_group *group = data->vgroups[i];
    5768                 :             : 
    5769                 :       11462 :       if (group->type == USE_COMPARE)
    5770                 :        1511 :         bitmap_set_range (group->related_cands, 0, count);
    5771                 :             :     }
    5772                 :         345 : }
    5773                 :             : 
    5774                 :             : /* If PREFERRED_MODE is suitable and profitable, use the preferred
    5775                 :             :    PREFERRED_MODE to compute doloop iv base from niter: base = niter + 1.  */
    5776                 :             : 
    5777                 :             : static tree
    5778                 :           0 : compute_doloop_base_on_mode (machine_mode preferred_mode, tree niter,
    5779                 :             :                              const widest_int &iterations_max)
    5780                 :             : {
    5781                 :           0 :   tree ntype = TREE_TYPE (niter);
    5782                 :           0 :   tree pref_type = lang_hooks.types.type_for_mode (preferred_mode, 1);
    5783                 :           0 :   if (!pref_type)
    5784                 :           0 :     return fold_build2 (PLUS_EXPR, ntype, unshare_expr (niter),
    5785                 :             :                         build_int_cst (ntype, 1));
    5786                 :             : 
    5787                 :           0 :   gcc_assert (TREE_CODE (pref_type) == INTEGER_TYPE);
    5788                 :             : 
    5789                 :           0 :   int prec = TYPE_PRECISION (ntype);
    5790                 :           0 :   int pref_prec = TYPE_PRECISION (pref_type);
    5791                 :             : 
    5792                 :           0 :   tree base;
    5793                 :             : 
    5794                 :             :   /* Check if the PREFERRED_MODED is able to present niter.  */
    5795                 :           0 :   if (pref_prec > prec
    5796                 :           0 :       || wi::ltu_p (iterations_max,
    5797                 :           0 :                     widest_int::from (wi::max_value (pref_prec, UNSIGNED),
    5798                 :             :                                       UNSIGNED)))
    5799                 :             :     {
    5800                 :             :       /* No wrap, it is safe to use preferred type after niter + 1.  */
    5801                 :           0 :       if (wi::ltu_p (iterations_max,
    5802                 :           0 :                      widest_int::from (wi::max_value (prec, UNSIGNED),
    5803                 :             :                                        UNSIGNED)))
    5804                 :             :         {
    5805                 :             :           /* This could help to optimize "-1 +1" pair when niter looks
    5806                 :             :              like "n-1": n is in original mode.  "base = (n - 1) + 1"
    5807                 :             :              in PREFERRED_MODED: it could be base = (PREFERRED_TYPE)n.  */
    5808                 :           0 :           base = fold_build2 (PLUS_EXPR, ntype, unshare_expr (niter),
    5809                 :             :                               build_int_cst (ntype, 1));
    5810                 :           0 :           base = fold_convert (pref_type, base);
    5811                 :             :         }
    5812                 :             : 
    5813                 :             :       /* To avoid wrap, convert niter to preferred type before plus 1.  */
    5814                 :             :       else
    5815                 :             :         {
    5816                 :           0 :           niter = fold_convert (pref_type, niter);
    5817                 :           0 :           base = fold_build2 (PLUS_EXPR, pref_type, unshare_expr (niter),
    5818                 :             :                               build_int_cst (pref_type, 1));
    5819                 :             :         }
    5820                 :             :     }
    5821                 :             :   else
    5822                 :           0 :     base = fold_build2 (PLUS_EXPR, ntype, unshare_expr (niter),
    5823                 :             :                         build_int_cst (ntype, 1));
    5824                 :             :   return base;
    5825                 :             : }
    5826                 :             : 
    5827                 :             : /* Add one doloop dedicated IV candidate:
    5828                 :             :      - Base is (may_be_zero ? 1 : (niter + 1)).
    5829                 :             :      - Step is -1.  */
    5830                 :             : 
    5831                 :             : static void
    5832                 :           0 : add_iv_candidate_for_doloop (struct ivopts_data *data)
    5833                 :             : {
    5834                 :           0 :   tree_niter_desc *niter_desc = niter_for_single_dom_exit (data);
    5835                 :           0 :   gcc_assert (niter_desc && niter_desc->assumptions);
    5836                 :             : 
    5837                 :           0 :   tree niter = niter_desc->niter;
    5838                 :           0 :   tree ntype = TREE_TYPE (niter);
    5839                 :           0 :   gcc_assert (TREE_CODE (ntype) == INTEGER_TYPE);
    5840                 :             : 
    5841                 :           0 :   tree may_be_zero = niter_desc->may_be_zero;
    5842                 :           0 :   if (may_be_zero && integer_zerop (may_be_zero))
    5843                 :             :     may_be_zero = NULL_TREE;
    5844                 :           0 :   if (may_be_zero)
    5845                 :             :     {
    5846                 :           0 :       if (COMPARISON_CLASS_P (may_be_zero))
    5847                 :             :         {
    5848                 :           0 :           niter = fold_build3 (COND_EXPR, ntype, may_be_zero,
    5849                 :             :                                build_int_cst (ntype, 0),
    5850                 :             :                                rewrite_to_non_trapping_overflow (niter));
    5851                 :             :         }
    5852                 :             :       /* Don't try to obtain the iteration count expression when may_be_zero is
    5853                 :             :          integer_nonzerop (actually iteration count is one) or else.  */
    5854                 :             :       else
    5855                 :             :         return;
    5856                 :             :     }
    5857                 :             : 
    5858                 :           0 :   machine_mode mode = TYPE_MODE (ntype);
    5859                 :           0 :   machine_mode pref_mode = targetm.preferred_doloop_mode (mode);
    5860                 :             : 
    5861                 :           0 :   tree base;
    5862                 :           0 :   if (mode != pref_mode)
    5863                 :             :     {
    5864                 :           0 :       base = compute_doloop_base_on_mode (pref_mode, niter, niter_desc->max);
    5865                 :           0 :       ntype = TREE_TYPE (base);
    5866                 :             :     }
    5867                 :             :   else
    5868                 :           0 :     base = fold_build2 (PLUS_EXPR, ntype, unshare_expr (niter),
    5869                 :             :                         build_int_cst (ntype, 1));
    5870                 :             : 
    5871                 :             : 
    5872                 :           0 :   add_candidate (data, base, build_int_cst (ntype, -1), true, NULL, NULL, true);
    5873                 :             : }
    5874                 :             : 
    5875                 :             : /* Finds the candidates for the induction variables.  */
    5876                 :             : 
    5877                 :             : static void
    5878                 :      427980 : find_iv_candidates (struct ivopts_data *data)
    5879                 :             : {
    5880                 :             :   /* Add commonly used ivs.  */
    5881                 :      427980 :   add_standard_iv_candidates (data);
    5882                 :             : 
    5883                 :             :   /* Add doloop dedicated ivs.  */
    5884                 :      427980 :   if (data->doloop_use_p)
    5885                 :           0 :     add_iv_candidate_for_doloop (data);
    5886                 :             : 
    5887                 :             :   /* Add old induction variables.  */
    5888                 :      427980 :   add_iv_candidate_for_bivs (data);
    5889                 :             : 
    5890                 :             :   /* Add induction variables derived from uses.  */
    5891                 :      427980 :   add_iv_candidate_for_groups (data);
    5892                 :             : 
    5893                 :      427980 :   set_autoinc_for_original_candidates (data);
    5894                 :             : 
    5895                 :             :   /* Record the important candidates.  */
    5896                 :      427980 :   record_important_candidates (data);
    5897                 :             : 
    5898                 :             :   /* Relate compare iv_use with all candidates.  */
    5899                 :      427980 :   if (!data->consider_all_candidates)
    5900                 :         345 :     relate_compare_use_with_all_cands (data);
    5901                 :             : 
    5902                 :      427980 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5903                 :             :     {
    5904                 :          76 :       unsigned i;
    5905                 :             : 
    5906                 :          76 :       fprintf (dump_file, "\n<Important Candidates>:\t");
    5907                 :        1906 :       for (i = 0; i < data->vcands.length (); i++)
    5908                 :         839 :         if (data->vcands[i]->important)
    5909                 :         569 :           fprintf (dump_file, " %d,", data->vcands[i]->id);
    5910                 :          76 :       fprintf (dump_file, "\n");
    5911                 :             : 
    5912                 :          76 :       fprintf (dump_file, "\n<Group, Cand> Related:\n");
    5913                 :         704 :       for (i = 0; i < data->vgroups.length (); i++)
    5914                 :             :         {
    5915                 :         276 :           struct iv_group *group = data->vgroups[i];
    5916                 :             : 
    5917                 :         276 :           if (group->related_cands)
    5918                 :             :             {
    5919                 :         276 :               fprintf (dump_file, "  Group %d:\t", group->id);
    5920                 :         276 :               dump_bitmap (dump_file, group->related_cands);
    5921                 :             :             }
    5922                 :             :         }
    5923                 :          76 :       fprintf (dump_file, "\n");
    5924                 :             :     }
    5925                 :      427980 : }
    5926                 :             : 
    5927                 :             : /* Determines costs of computing use of iv with an iv candidate.  */
    5928                 :             : 
    5929                 :             : static void
    5930                 :      427980 : determine_group_iv_costs (struct ivopts_data *data)
    5931                 :             : {
    5932                 :      427980 :   unsigned i, j;
    5933                 :      427980 :   struct iv_cand *cand;
    5934                 :      427980 :   struct iv_group *group;
    5935                 :      427980 :   bitmap to_clear = BITMAP_ALLOC (NULL);
    5936                 :             : 
    5937                 :      427980 :   alloc_use_cost_map (data);
    5938                 :             : 
    5939                 :     3642618 :   for (i = 0; i < data->vgroups.length (); i++)
    5940                 :             :     {
    5941                 :     1393329 :       group = data->vgroups[i];
    5942                 :             : 
    5943                 :     1393329 :       if (data->consider_all_candidates)
    5944                 :             :         {
    5945                 :    33105434 :           for (j = 0; j < data->vcands.length (); j++)
    5946                 :             :             {
    5947                 :    15170850 :               cand = data->vcands[j];
    5948                 :    15170850 :               determine_group_iv_cost (data, group, cand);
    5949                 :             :             }
    5950                 :             :         }
    5951                 :             :       else
    5952                 :             :         {
    5953                 :       11462 :           bitmap_iterator bi;
    5954                 :             : 
    5955                 :      276276 :           EXECUTE_IF_SET_IN_BITMAP (group->related_cands, 0, j, bi)
    5956                 :             :             {
    5957                 :      264814 :               cand = data->vcands[j];
    5958                 :      264814 :               if (!determine_group_iv_cost (data, group, cand))
    5959                 :      161809 :                 bitmap_set_bit (to_clear, j);
    5960                 :             :             }
    5961                 :             : 
    5962                 :             :           /* Remove the candidates for that the cost is infinite from
    5963                 :             :              the list of related candidates.  */
    5964                 :       11462 :           bitmap_and_compl_into (group->related_cands, to_clear);
    5965                 :       11462 :           bitmap_clear (to_clear);
    5966                 :             :         }
    5967                 :             :     }
    5968                 :             : 
    5969                 :      427980 :   BITMAP_FREE (to_clear);
    5970                 :             : 
    5971                 :      427980 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5972                 :             :     {
    5973                 :          76 :       bitmap_iterator bi;
    5974                 :             : 
    5975                 :             :       /* Dump invariant variables.  */
    5976                 :          76 :       fprintf (dump_file, "\n<Invariant Vars>:\n");
    5977                 :        1413 :       EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
    5978                 :             :         {
    5979                 :        1337 :           struct version_info *info = ver_info (data, i);
    5980                 :        1337 :           if (info->inv_id)
    5981                 :             :             {
    5982                 :         269 :               fprintf (dump_file, "Inv %d:\t", info->inv_id);
    5983                 :         269 :               print_generic_expr (dump_file, info->name, TDF_SLIM);
    5984                 :         269 :               fprintf (dump_file, "%s\n",
    5985                 :         269 :                        info->has_nonlin_use ? "" : "\t(eliminable)");
    5986                 :             :             }
    5987                 :             :         }
    5988                 :             : 
    5989                 :             :       /* Dump invariant expressions.  */
    5990                 :          76 :       fprintf (dump_file, "\n<Invariant Expressions>:\n");
    5991                 :          76 :       auto_vec <iv_inv_expr_ent *> list (data->inv_expr_tab->elements ());
    5992                 :             : 
    5993                 :         543 :       for (hash_table<iv_inv_expr_hasher>::iterator it
    5994                 :         619 :            = data->inv_expr_tab->begin (); it != data->inv_expr_tab->end ();
    5995                 :         467 :            ++it)
    5996                 :         467 :         list.safe_push (*it);
    5997                 :             : 
    5998                 :          76 :       list.qsort (sort_iv_inv_expr_ent);
    5999                 :             : 
    6000                 :        1065 :       for (i = 0; i < list.length (); ++i)
    6001                 :             :         {
    6002                 :         467 :           fprintf (dump_file, "inv_expr %d: \t", list[i]->id);
    6003                 :         467 :           print_generic_expr (dump_file, list[i]->expr, TDF_SLIM);
    6004                 :         467 :           fprintf (dump_file, "\n");
    6005                 :             :         }
    6006                 :             : 
    6007                 :          76 :       fprintf (dump_file, "\n<Group-candidate Costs>:\n");
    6008                 :             : 
    6009                 :         704 :       for (i = 0; i < data->vgroups.length (); i++)
    6010                 :             :         {
    6011                 :         276 :           group = data->vgroups[i];
    6012                 :             : 
    6013                 :         276 :           fprintf (dump_file, "Group %d:\n", i);
    6014                 :         276 :           fprintf (dump_file, "  cand\tcost\tcompl.\tinv.expr.\tinv.vars\n");
    6015                 :        4094 :           for (j = 0; j < group->n_map_members; j++)
    6016                 :             :             {
    6017                 :        5357 :               if (!group->cost_map[j].cand
    6018                 :        3818 :                   || group->cost_map[j].cost.infinite_cost_p ())
    6019                 :        1539 :                 continue;
    6020                 :             : 
    6021                 :        2279 :               fprintf (dump_file, "  %d\t%" PRId64 "\t%d\t",
    6022                 :        2279 :                        group->cost_map[j].cand->id,
    6023                 :             :                        group->cost_map[j].cost.cost,
    6024                 :        2279 :                        group->cost_map[j].cost.complexity);
    6025                 :        2279 :               if (!group->cost_map[j].inv_exprs
    6026                 :        2279 :                   || bitmap_empty_p (group->cost_map[j].inv_exprs))
    6027                 :        1599 :                 fprintf (dump_file, "NIL;\t");
    6028                 :             :               else
    6029                 :         680 :                 bitmap_print (dump_file,
    6030                 :             :                               group->cost_map[j].inv_exprs, "", ";\t");
    6031                 :        2279 :               if (!group->cost_map[j].inv_vars
    6032                 :        2279 :                   || bitmap_empty_p (group->cost_map[j].inv_vars))
    6033                 :        1891 :                 fprintf (dump_file, "NIL;\n");
    6034                 :             :               else
    6035                 :         388 :                 bitmap_print (dump_file,
    6036                 :             :                               group->cost_map[j].inv_vars, "", "\n");
    6037                 :             :             }
    6038                 :             : 
    6039                 :         276 :           fprintf (dump_file, "\n");
    6040                 :             :         }
    6041                 :          76 :       fprintf (dump_file, "\n");
    6042                 :          76 :     }
    6043                 :      427980 : }
    6044                 :             : 
    6045                 :             : /* Determines cost of the candidate CAND.  */
    6046                 :             : 
    6047                 :             : static void
    6048                 :     3981714 : determine_iv_cost (struct ivopts_data *data, struct iv_cand *cand)
    6049                 :             : {
    6050                 :     3981714 :   comp_cost cost_base;
    6051                 :     3981714 :   int64_t cost, cost_step;
    6052                 :     3981714 :   tree base;
    6053                 :             : 
    6054                 :     3981714 :   gcc_assert (cand->iv != NULL);
    6055                 :             : 
    6056                 :             :   /* There are two costs associated with the candidate -- its increment
    6057                 :             :      and its initialization.  The second is almost negligible for any loop
    6058                 :             :      that rolls enough, so we take it just very little into account.  */
    6059                 :             : 
    6060                 :     3981714 :   base = cand->iv->base;
    6061                 :     3981714 :   cost_base = force_var_cost (data, base, NULL);
    6062                 :             :   /* It will be exceptional that the iv register happens to be initialized with
    6063                 :             :      the proper value at no cost.  In general, there will at least be a regcopy
    6064                 :             :      or a const set.  */
    6065                 :     3981714 :   if (cost_base.cost == 0)
    6066                 :     3143846 :     cost_base.cost = COSTS_N_INSNS (1);
    6067                 :             :   /* Doloop decrement should be considered as zero cost.  */
    6068                 :     3981714 :   if (cand->doloop_p)
    6069                 :             :     cost_step = 0;
    6070                 :             :   else
    6071                 :     3981714 :     cost_step = add_cost (data->speed, TYPE_MODE (TREE_TYPE (base)));
    6072                 :     3981714 :   cost = cost_step + adjust_setup_cost (data, cost_base.cost);
    6073                 :             : 
    6074                 :             :   /* Prefer the original ivs unless we may gain something by replacing it.
    6075                 :             :      The reason is to make debugging simpler; so this is not relevant for
    6076                 :             :      artificial ivs created by other optimization passes.  */
    6077                 :     3981714 :   if ((cand->pos != IP_ORIGINAL
    6078                 :      731497 :        || !SSA_NAME_VAR (cand->var_before)
    6079                 :      371227 :        || DECL_ARTIFICIAL (SSA_NAME_VAR (cand->var_before)))
    6080                 :             :       /* Prefer doloop as well.  */
    6081                 :     4406592 :       && !cand->doloop_p)
    6082                 :     3675095 :     cost++;
    6083                 :             : 
    6084                 :             :   /* Prefer not to insert statements into latch unless there are some
    6085                 :             :      already (so that we do not create unnecessary jumps).  */
    6086                 :     3981714 :   if (cand->pos == IP_END
    6087                 :     3981714 :       && empty_block_p (ip_end_pos (data->current_loop)))
    6088                 :        1549 :     cost++;
    6089                 :             : 
    6090                 :     3981714 :   cand->cost = cost;
    6091                 :     3981714 :   cand->cost_step = cost_step;
    6092                 :     3981714 : }
    6093                 :             : 
    6094                 :             : /* Determines costs of computation of the candidates.  */
    6095                 :             : 
    6096                 :             : static void
    6097                 :      427980 : determine_iv_costs (struct ivopts_data *data)
    6098                 :             : {
    6099                 :      427980 :   unsigned i;
    6100                 :             : 
    6101                 :      427980 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6102                 :             :     {
    6103                 :          76 :       fprintf (dump_file, "<Candidate Costs>:\n");
    6104                 :          76 :       fprintf (dump_file, "  cand\tcost\n");
    6105                 :             :     }
    6106                 :             : 
    6107                 :     8819388 :   for (i = 0; i < data->vcands.length (); i++)
    6108                 :             :     {
    6109                 :     3981714 :       struct iv_cand *cand = data->vcands[i];
    6110                 :             : 
    6111                 :     3981714 :       determine_iv_cost (data, cand);
    6112                 :             : 
    6113                 :     3981714 :       if (dump_file && (dump_flags & TDF_DETAILS))
    6114                 :         839 :         fprintf (dump_file, "  %d\t%d\n", i, cand->cost);
    6115                 :             :     }
    6116                 :             : 
    6117                 :      427980 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6118                 :          76 :     fprintf (dump_file, "\n");
    6119                 :      427980 : }
    6120                 :             : 
    6121                 :             : /* Estimate register pressure for loop having N_INVS invariants and N_CANDS
    6122                 :             :    induction variables.  Note N_INVS includes both invariant variables and
    6123                 :             :    invariant expressions.  */
    6124                 :             : 
    6125                 :             : static unsigned
    6126                 :   334358883 : ivopts_estimate_reg_pressure (struct ivopts_data *data, unsigned n_invs,
    6127                 :             :                               unsigned n_cands)
    6128                 :             : {
    6129                 :   334358883 :   unsigned cost;
    6130                 :   334358883 :   unsigned n_old = data->regs_used, n_new = n_invs + n_cands;
    6131                 :   334358883 :   unsigned regs_needed = n_new + n_old, available_regs = target_avail_regs;
    6132                 :   334358883 :   bool speed = data->speed;
    6133                 :             : 
    6134                 :             :   /* If there is a call in the loop body, the call-clobbered registers
    6135                 :             :      are not available for loop invariants.  */
    6136                 :   334358883 :   if (data->body_includes_call)
    6137                 :    84696125 :     available_regs = available_regs - target_clobbered_regs;
    6138                 :             : 
    6139                 :             :   /* If we have enough registers.  */
    6140                 :   334358883 :   if (regs_needed + target_res_regs < available_regs)
    6141                 :             :     cost = n_new;
    6142                 :             :   /* If close to running out of registers, try to preserve them.  */
    6143                 :   149888850 :   else if (regs_needed <= available_regs)
    6144                 :    46055342 :     cost = target_reg_cost [speed] * regs_needed;
    6145                 :             :   /* If we run out of available registers but the number of candidates
    6146                 :             :      does not, we penalize extra registers using target_spill_cost.  */
    6147                 :   103833508 :   else if (n_cands <= available_regs)
    6148                 :    89622135 :     cost = target_reg_cost [speed] * available_regs
    6149                 :    89622135 :            + target_spill_cost [speed] * (regs_needed - available_regs);
    6150                 :             :   /* If the number of candidates runs out available registers, we penalize
    6151                 :             :      extra candidate registers using target_spill_cost * 2.  Because it is
    6152                 :             :      more expensive to spill induction variable than invariant.  */
    6153                 :             :   else
    6154                 :    14211373 :     cost = target_reg_cost [speed] * available_regs
    6155                 :    14211373 :            + target_spill_cost [speed] * (n_cands - available_regs) * 2
    6156                 :    14211373 :            + target_spill_cost [speed] * (regs_needed - n_cands);
    6157                 :             : 
    6158                 :             :   /* Finally, add the number of candidates, so that we prefer eliminating
    6159                 :             :      induction variables if possible.  */
    6160                 :   334358883 :   return cost + n_cands;
    6161                 :             : }
    6162                 :             : 
    6163                 :             : /* For each size of the induction variable set determine the penalty.  */
    6164                 :             : 
    6165                 :             : static void
    6166                 :      427980 : determine_set_costs (struct ivopts_data *data)
    6167                 :             : {
    6168                 :      427980 :   unsigned j, n;
    6169                 :      427980 :   gphi *phi;
    6170                 :      427980 :   gphi_iterator psi;
    6171                 :      427980 :   tree op;
    6172                 :      427980 :   class loop *loop = data->current_loop;
    6173                 :      427980 :   bitmap_iterator bi;
    6174                 :             : 
    6175                 :      427980 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6176                 :             :     {
    6177                 :          76 :       fprintf (dump_file, "<Global Costs>:\n");
    6178                 :          76 :       fprintf (dump_file, "  target_avail_regs %d\n", target_avail_regs);
    6179                 :          76 :       fprintf (dump_file, "  target_clobbered_regs %d\n", target_clobbered_regs);
    6180                 :          76 :       fprintf (dump_file, "  target_reg_cost %d\n", target_reg_cost[data->speed]);
    6181                 :          76 :       fprintf (dump_file, "  target_spill_cost %d\n", target_spill_cost[data->speed]);
    6182                 :             :     }
    6183                 :             : 
    6184                 :      427980 :   n = 0;
    6185                 :     1666303 :   for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
    6186                 :             :     {
    6187                 :     1238323 :       phi = psi.phi ();
    6188                 :     1238323 :       op = PHI_RESULT (phi);
    6189                 :             : 
    6190                 :     2476646 :       if (virtual_operand_p (op))
    6191                 :      266368 :         continue;
    6192                 :             : 
    6193                 :      971955 :       if (get_iv (data, op))
    6194                 :      735440 :         continue;
    6195                 :             : 
    6196                 :      442811 :       if (!POINTER_TYPE_P (TREE_TYPE (op))
    6197                 :      442663 :           && !INTEGRAL_TYPE_P (TREE_TYPE (op)))
    6198                 :       95003 :         continue;
    6199                 :             : 
    6200                 :      141512 :       n++;
    6201                 :             :     }
    6202                 :             : 
    6203                 :     4786307 :   EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi)
    6204                 :             :     {
    6205                 :     4358327 :       struct version_info *info = ver_info (data, j);
    6206                 :             : 
    6207                 :     4358327 :       if (info->inv_id && info->has_nonlin_use)
    6208                 :      421468 :         n++;
    6209                 :             :     }
    6210                 :             : 
    6211                 :      427980 :   data->regs_used = n;
    6212                 :      427980 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6213                 :          76 :     fprintf (dump_file, "  regs_used %d\n", n);
    6214                 :             : 
    6215                 :      427980 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6216                 :             :     {
    6217                 :          76 :       fprintf (dump_file, "  cost for size:\n");
    6218                 :          76 :       fprintf (dump_file, "  ivs\tcost\n");
    6219                 :        2432 :       for (j = 0; j <= 2 * target_avail_regs; j++)
    6220                 :        2356 :         fprintf (dump_file, "  %d\t%d\n", j,
    6221                 :             :                  ivopts_estimate_reg_pressure (data, 0, j));
    6222                 :          76 :       fprintf (dump_file, "\n");
    6223                 :             :     }
    6224                 :      427980 : }
    6225                 :             : 
    6226                 :             : /* Returns true if A is a cheaper cost pair than B.  */
    6227                 :             : 
    6228                 :             : static bool
    6229                 :    69556599 : cheaper_cost_pair (class cost_pair *a, class cost_pair *b)
    6230                 :             : {
    6231                 :    69556599 :   if (!a)
    6232                 :             :     return false;
    6233                 :             : 
    6234                 :    64848582 :   if (!b)
    6235                 :             :     return true;
    6236                 :             : 
    6237                 :    62146445 :   if (a->cost < b->cost)
    6238                 :             :     return true;
    6239                 :             : 
    6240                 :    46105083 :   if (b->cost < a->cost)
    6241                 :             :     return false;
    6242                 :             : 
    6243                 :             :   /* In case the costs are the same, prefer the cheaper candidate.  */
    6244                 :    26551769 :   if (a->cand->cost < b->cand->cost)
    6245                 :             :     return true;
    6246                 :             : 
    6247                 :             :   return false;
    6248                 :             : }
    6249                 :             : 
    6250                 :             : /* Compare if A is a more expensive cost pair than B.  Return 1, 0 and -1
    6251                 :             :    for more expensive, equal and cheaper respectively.  */
    6252                 :             : 
    6253                 :             : static int
    6254                 :    24275797 : compare_cost_pair (class cost_pair *a, class cost_pair *b)
    6255                 :             : {
    6256                 :    24275797 :   if (cheaper_cost_pair (a, b))
    6257                 :             :     return -1;
    6258                 :    18890175 :   if (cheaper_cost_pair (b, a))
    6259                 :    12326511 :     return 1;
    6260                 :             : 
    6261                 :             :   return 0;
    6262                 :             : }
    6263                 :             : 
    6264                 :             : /* Returns candidate by that USE is expressed in IVS.  */
    6265                 :             : 
    6266                 :             : static class cost_pair *
    6267                 :   234095786 : iv_ca_cand_for_group (class iv_ca *ivs, struct iv_group *group)
    6268                 :             : {
    6269                 :   234095786 :   return ivs->cand_for_group[group->id];
    6270                 :             : }
    6271                 :             : 
    6272                 :             : /* Computes the cost field of IVS structure.  */
    6273                 :             : 
    6274                 :             : static void
    6275                 :   334356230 : iv_ca_recount_cost (struct ivopts_data *data, class iv_ca *ivs)
    6276                 :             : {
    6277                 :   334356230 :   comp_cost cost = ivs->cand_use_cost;
    6278                 :             : 
    6279                 :   334356230 :   cost += ivs->cand_cost;
    6280                 :   334356230 :   cost += ivopts_estimate_reg_pressure (data, ivs->n_invs, ivs->n_cands);
    6281                 :   334356230 :   ivs->cost = cost;
    6282                 :   334356230 : }
    6283                 :             : 
    6284                 :             : /* Remove use of invariants in set INVS by decreasing counter in N_INV_USES
    6285                 :             :    and IVS.  */
    6286                 :             : 
    6287                 :             : static void
    6288                 :   461737824 : iv_ca_set_remove_invs (class iv_ca *ivs, bitmap invs, unsigned *n_inv_uses)
    6289                 :             : {
    6290                 :   461737824 :   bitmap_iterator bi;
    6291                 :   461737824 :   unsigned iid;
    6292                 :             : 
    6293                 :   461737824 :   if (!invs)
    6294                 :   370262597 :     return;
    6295                 :             : 
    6296                 :    91475227 :   gcc_assert (n_inv_uses != NULL);
    6297                 :   159041525 :   EXECUTE_IF_SET_IN_BITMAP (invs, 0, iid, bi)
    6298                 :             :     {
    6299                 :    67566298 :       n_inv_uses[iid]--;
    6300                 :    67566298 :       if (n_inv_uses[iid] == 0)
    6301                 :    51986754 :         ivs->n_invs--;
    6302                 :             :     }
    6303                 :             : }
    6304                 :             : 
    6305                 :             : /* Set USE not to be expressed by any candidate in IVS.  */
    6306                 :             : 
    6307                 :             : static void
    6308                 :   165786342 : iv_ca_set_no_cp (struct ivopts_data *data, class iv_ca *ivs,
    6309                 :             :                  struct iv_group *group)
    6310                 :             : {
    6311                 :   165786342 :   unsigned gid = group->id, cid;
    6312                 :   165786342 :   class cost_pair *cp;
    6313                 :             : 
    6314                 :   165786342 :   cp = ivs->cand_for_group[gid];
    6315                 :   165786342 :   if (!cp)
    6316                 :             :     return;
    6317                 :   165786342 :   cid = cp->cand->id;
    6318                 :             : 
    6319                 :   165786342 :   ivs->bad_groups++;
    6320                 :   165786342 :   ivs->cand_for_group[gid] = NULL;
    6321                 :   165786342 :   ivs->n_cand_uses[cid]--;
    6322                 :             : 
    6323                 :   165786342 :   if (ivs->n_cand_uses[cid] == 0)
    6324                 :             :     {
    6325                 :    65082570 :       bitmap_clear_bit (ivs->cands, cid);
    6326                 :    65082570 :       if (!cp->cand->doloop_p || !targetm.have_count_reg_decr_p)
    6327                 :    65082570 :         ivs->n_cands--;
    6328                 :    65082570 :       ivs->cand_cost -= cp->cand->cost;
    6329                 :    65082570 :       iv_ca_set_remove_invs (ivs, cp->cand->inv_vars, ivs->n_inv_var_uses);
    6330                 :    65082570 :       iv_ca_set_remove_invs (ivs, cp->cand->inv_exprs, ivs->n_inv_expr_uses);
    6331                 :             :     }
    6332                 :             : 
    6333                 :   165786342 :   ivs->cand_use_cost -= cp->cost;
    6334                 :   165786342 :   iv_ca_set_remove_invs (ivs, cp->inv_vars, ivs->n_inv_var_uses);
    6335                 :   165786342 :   iv_ca_set_remove_invs (ivs, cp->inv_exprs, ivs->n_inv_expr_uses);
    6336                 :   165786342 :   iv_ca_recount_cost (data, ivs);
    6337                 :             : }
    6338                 :             : 
    6339                 :             : /* Add use of invariants in set INVS by increasing counter in N_INV_USES and
    6340                 :             :    IVS.  */
    6341                 :             : 
    6342                 :             : static void
    6343                 :   469639540 : iv_ca_set_add_invs (class iv_ca *ivs, bitmap invs, unsigned *n_inv_uses)
    6344                 :             : {
    6345                 :   469639540 :   bitmap_iterator bi;
    6346                 :   469639540 :   unsigned iid;
    6347                 :             : 
    6348                 :   469639540 :   if (!invs)
    6349                 :   377189511 :     return;
    6350                 :             : 
    6351                 :    92450029 :   gcc_assert (n_inv_uses != NULL);
    6352                 :   160860311 :   EXECUTE_IF_SET_IN_BITMAP (invs, 0, iid, bi)
    6353                 :             :     {
    6354                 :    68410282 :       n_inv_uses[iid]++;
    6355                 :    68410282 :       if (n_inv_uses[iid] == 1)
    6356                 :    52761179 :         ivs->n_invs++;
    6357                 :             :     }
    6358                 :             : }
    6359                 :             : 
    6360                 :             : /* Set cost pair for GROUP in set IVS to CP.  */
    6361                 :             : 
    6362                 :             : static void
    6363                 :   179948985 : iv_ca_set_cp (struct ivopts_data *data, class iv_ca *ivs,
    6364                 :             :               struct iv_group *group, class cost_pair *cp)
    6365                 :             : {
    6366                 :   179948985 :   unsigned gid = group->id, cid;
    6367                 :             : 
    6368                 :   179948985 :   if (ivs->cand_for_group[gid] == cp)
    6369                 :             :     return;
    6370                 :             : 
    6371                 :   168569888 :   if (ivs->cand_for_group[gid])
    6372                 :   155387009 :     iv_ca_set_no_cp (data, ivs, group);
    6373                 :             : 
    6374                 :   168569888 :   if (cp)
    6375                 :             :     {
    6376                 :   168569888 :       cid = cp->cand->id;
    6377                 :             : 
    6378                 :   168569888 :       ivs->bad_groups--;
    6379                 :   168569888 :       ivs->cand_for_group[gid] = cp;
    6380                 :   168569888 :       ivs->n_cand_uses[cid]++;
    6381                 :   168569888 :       if (ivs->n_cand_uses[cid] == 1)
    6382                 :             :         {
    6383                 :    66249882 :           bitmap_set_bit (ivs->cands, cid);
    6384                 :    66249882 :           if (!cp->cand->doloop_p || !targetm.have_count_reg_decr_p)
    6385                 :    66249882 :             ivs->n_cands++;
    6386                 :    66249882 :           ivs->cand_cost += cp->cand->cost;
    6387                 :    66249882 :           iv_ca_set_add_invs (ivs, cp->cand->inv_vars, ivs->n_inv_var_uses);
    6388                 :    66249882 :           iv_ca_set_add_invs (ivs, cp->cand->inv_exprs, ivs->n_inv_expr_uses);
    6389                 :             :         }
    6390                 :             : 
    6391                 :   168569888 :       ivs->cand_use_cost += cp->cost;
    6392                 :   168569888 :       iv_ca_set_add_invs (ivs, cp->inv_vars, ivs->n_inv_var_uses);
    6393                 :   168569888 :       iv_ca_set_add_invs (ivs, cp->inv_exprs, ivs->n_inv_expr_uses);
    6394                 :   168569888 :       iv_ca_recount_cost (data, ivs);
    6395                 :             :     }
    6396                 :             : }
    6397                 :             : 
    6398                 :             : /* Extend set IVS by expressing USE by some of the candidates in it
    6399                 :             :    if possible.  Consider all important candidates if candidates in
    6400                 :             :    set IVS don't give any result.  */
    6401                 :             : 
    6402                 :             : static void
    6403                 :     2784490 : iv_ca_add_group (struct ivopts_data *data, class iv_ca *ivs,
    6404                 :             :                struct iv_group *group)
    6405                 :             : {
    6406                 :     2784490 :   class cost_pair *best_cp = NULL, *cp;
    6407                 :     2784490 :   bitmap_iterator bi;
    6408                 :     2784490 :   unsigned i;
    6409                 :     2784490 :   struct iv_cand *cand;
    6410                 :             : 
    6411                 :     2784490 :   gcc_assert (ivs->upto >= group->id);
    6412                 :     2784490 :   ivs->upto++;
    6413                 :     2784490 :   ivs->bad_groups++;
    6414                 :             : 
    6415                 :     5359997 :   EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi)
    6416                 :             :     {
    6417                 :     2575507 :       cand = data->vcands[i];
    6418                 :     2575507 :       cp = get_group_iv_cost (data, group, cand);
    6419                 :     2575507 :       if (cheaper_cost_pair (cp, best_cp))
    6420                 :     1689626 :         best_cp = cp;
    6421                 :             :     }
    6422                 :             : 
    6423                 :     2784490 :   if (best_cp == NULL)
    6424                 :             :     {
    6425                 :     9997021 :       EXECUTE_IF_SET_IN_BITMAP (data->important_candidates, 0, i, bi)
    6426                 :             :         {
    6427                 :     8843652 :           cand = data->vcands[i];
    6428                 :     8843652 :           cp = get_group_iv_cost (data, group, cand);
    6429                 :     8843652 :           if (cheaper_cost_pair (cp, best_cp))
    6430                 :     2025029 :             best_cp = cp;
    6431                 :             :         }
    6432                 :             :     }
    6433                 :             : 
    6434                 :     2784490 :   iv_ca_set_cp (data, ivs, group, best_cp);
    6435                 :     2784490 : }
    6436                 :             : 
    6437                 :             : /* Get cost for assignment IVS.  */
    6438                 :             : 
    6439                 :             : static comp_cost
    6440                 :    68559153 : iv_ca_cost (class iv_ca *ivs)
    6441                 :             : {
    6442                 :             :   /* This was a conditional expression but it triggered a bug in
    6443                 :             :      Sun C 5.5.  */
    6444                 :           0 :   if (ivs->bad_groups)
    6445                 :       82353 :     return infinite_cost;
    6446                 :             :   else
    6447                 :    68476800 :     return ivs->cost;
    6448                 :             : }
    6449                 :             : 
    6450                 :             : /* Compare if applying NEW_CP to GROUP for IVS introduces more invariants
    6451                 :             :    than OLD_CP.  Return 1, 0 and -1 for more, equal and fewer invariants
    6452                 :             :    respectively.  */
    6453                 :             : 
    6454                 :             : static int
    6455                 :    31329834 : iv_ca_compare_deps (struct ivopts_data *data, class iv_ca *ivs,
    6456                 :             :                     struct iv_group *group, class cost_pair *old_cp,
    6457                 :             :                     class cost_pair *new_cp)
    6458                 :             : {
    6459                 :    31329834 :   gcc_assert (old_cp && new_cp && old_cp != new_cp);
    6460                 :    31329834 :   unsigned old_n_invs = ivs->n_invs;
    6461                 :    31329834 :   iv_ca_set_cp (data, ivs, group, new_cp);
    6462                 :    31329834 :   unsigned new_n_invs = ivs->n_invs;
    6463                 :    31329834 :   iv_ca_set_cp (data, ivs, group, old_cp);
    6464                 :             : 
    6465                 :    31329834 :   return new_n_invs > old_n_invs ? 1 : (new_n_invs < old_n_invs ? -1 : 0);
    6466                 :             : }
    6467                 :             : 
    6468                 :             : /* Creates change of expressing GROUP by NEW_CP instead of OLD_CP and chains
    6469                 :             :    it before NEXT.  */
    6470                 :             : 
    6471                 :             : static struct iv_ca_delta *
    6472                 :    37910926 : iv_ca_delta_add (struct iv_group *group, class cost_pair *old_cp,
    6473                 :             :                  class cost_pair *new_cp, struct iv_ca_delta *next)
    6474                 :             : {
    6475                 :           0 :   struct iv_ca_delta *change = XNEW (struct iv_ca_delta);
    6476                 :             : 
    6477                 :    37910926 :   change->group = group;
    6478                 :    37910926 :   change->old_cp = old_cp;
    6479                 :    37910926 :   change->new_cp = new_cp;
    6480                 :    37910926 :   change->next = next;
    6481                 :             : 
    6482                 :    37910926 :   return change;
    6483                 :             : }
    6484                 :             : 
    6485                 :             : /* Joins two lists of changes L1 and L2.  Destructive -- old lists
    6486                 :             :    are rewritten.  */
    6487                 :             : 
    6488                 :             : static struct iv_ca_delta *
    6489                 :     6936392 : iv_ca_delta_join (struct iv_ca_delta *l1, struct iv_ca_delta *l2)
    6490                 :             : {
    6491                 :     6936392 :   struct iv_ca_delta *last;
    6492                 :             : 
    6493                 :           0 :   if (!l2)
    6494                 :             :     return l1;
    6495                 :             : 
    6496                 :           0 :   if (!l1)
    6497                 :             :     return l2;
    6498                 :             : 
    6499                 :     3110520 :   for (last = l1; last->next; last = last->next)
    6500                 :     1075842 :     continue;
    6501                 :     2034678 :   last->next = l2;
    6502                 :             : 
    6503                 :     2034678 :   return l1;
    6504                 :     1075842 : }
    6505                 :             : 
    6506                 :             : /* Reverse the list of changes DELTA, forming the inverse to it.  */
    6507                 :             : 
    6508                 :             : static struct iv_ca_delta *
    6509                 :           0 : iv_ca_delta_reverse (struct iv_ca_delta *delta)
    6510                 :             : {
    6511                 :           0 :   struct iv_ca_delta *act, *next, *prev = NULL;
    6512                 :             : 
    6513                 :   130949540 :   for (act = delta; act; act = next)
    6514                 :             :     {
    6515                 :    71678662 :       next = act->next;
    6516                 :    71678662 :       act->next = prev;
    6517                 :    71678662 :       prev = act;
    6518                 :             : 
    6519                 :    71678662 :       std::swap (act->old_cp, act->new_cp);
    6520                 :             :     }
    6521                 :             : 
    6522                 :           0 :   return prev;
    6523                 :             : }
    6524                 :             : 
    6525                 :             : /* Commit changes in DELTA to IVS.  If FORWARD is false, the changes are
    6526                 :             :    reverted instead.  */
    6527                 :             : 
    6528                 :             : static void
    6529                 :    62441472 : iv_ca_delta_commit (struct ivopts_data *data, class iv_ca *ivs,
    6530                 :             :                     struct iv_ca_delta *delta, bool forward)
    6531                 :             : {
    6532                 :    62441472 :   class cost_pair *from, *to;
    6533                 :    62441472 :   struct iv_ca_delta *act;
    6534                 :             : 
    6535                 :    62441472 :   if (!forward)
    6536                 :    62441472 :     delta = iv_ca_delta_reverse (delta);
    6537                 :             : 
    6538                 :   138183648 :   for (act = delta; act; act = act->next)
    6539                 :             :     {
    6540                 :    75742176 :       from = act->old_cp;
    6541                 :    75742176 :       to = act->new_cp;
    6542                 :    75742176 :       gcc_assert (iv_ca_cand_for_group (ivs, act->group) == from);
    6543                 :    75742176 :       iv_ca_set_cp (data, ivs, act->group, to);
    6544                 :             :     }
    6545                 :             : 
    6546                 :    62441472 :   if (!forward)
    6547                 :    62441472 :     iv_ca_delta_reverse (delta);
    6548                 :    62441472 : }
    6549                 :             : 
    6550                 :             : /* Returns true if CAND is used in IVS.  */
    6551                 :             : 
    6552                 :             : static bool
    6553                 :    24731749 : iv_ca_cand_used_p (class iv_ca *ivs, struct iv_cand *cand)
    6554                 :             : {
    6555                 :    24731749 :   return ivs->n_cand_uses[cand->id] > 0;
    6556                 :             : }
    6557                 :             : 
    6558                 :             : /* Returns number of induction variable candidates in the set IVS.  */
    6559                 :             : 
    6560                 :             : static unsigned
    6561                 :    10760888 : iv_ca_n_cands (class iv_ca *ivs)
    6562                 :             : {
    6563                 :    10760888 :   return ivs->n_cands;
    6564                 :             : }
    6565                 :             : 
    6566                 :             : /* Free the list of changes DELTA.  */
    6567                 :             : 
    6568                 :             : static void
    6569                 :    38036574 : iv_ca_delta_free (struct iv_ca_delta **delta)
    6570                 :             : {
    6571                 :    38036574 :   struct iv_ca_delta *act, *next;
    6572                 :             : 
    6573                 :    75947500 :   for (act = *delta; act; act = next)
    6574                 :             :     {
    6575                 :    37910926 :       next = act->next;
    6576                 :    37910926 :       free (act);
    6577                 :             :     }
    6578                 :             : 
    6579                 :    38036574 :   *delta = NULL;
    6580                 :    38036574 : }
    6581                 :             : 
    6582                 :             : /* Allocates new iv candidates assignment.  */
    6583                 :             : 
    6584                 :             : static class iv_ca *
    6585                 :      855960 : iv_ca_new (struct ivopts_data *data)
    6586                 :             : {
    6587                 :      855960 :   class iv_ca *nw = XNEW (class iv_ca);
    6588                 :             : 
    6589                 :      855960 :   nw->upto = 0;
    6590                 :      855960 :   nw->bad_groups = 0;
    6591                 :     1711920 :   nw->cand_for_group = XCNEWVEC (class cost_pair *,
    6592                 :             :                                  data->vgroups.length ());
    6593                 :     1711920 :   nw->n_cand_uses = XCNEWVEC (unsigned, data->vcands.length ());
    6594                 :      855960 :   nw->cands = BITMAP_ALLOC (NULL);
    6595                 :      855960 :   nw->n_cands = 0;
    6596                 :      855960 :   nw->n_invs = 0;
    6597                 :      855960 :   nw->cand_use_cost = no_cost;
    6598                 :      855960 :   nw->cand_cost = 0;
    6599                 :      855960 :   nw->n_inv_var_uses = XCNEWVEC (unsigned, data->max_inv_var_id + 1);
    6600                 :      855960 :   nw->n_inv_expr_uses = XCNEWVEC (unsigned, data->max_inv_expr_id + 1);
    6601                 :      855960 :   nw->cost = no_cost;
    6602                 :             : 
    6603                 :      855960 :   return nw;
    6604                 :             : }
    6605                 :             : 
    6606                 :             : /* Free memory occupied by the set IVS.  */
    6607                 :             : 
    6608                 :             : static void
    6609                 :      855960 : iv_ca_free (class iv_ca **ivs)
    6610                 :             : {
    6611                 :      855960 :   free ((*ivs)->cand_for_group);
    6612                 :      855960 :   free ((*ivs)->n_cand_uses);
    6613                 :      855960 :   BITMAP_FREE ((*ivs)->cands);
    6614                 :      855960 :   free ((*ivs)->n_inv_var_uses);
    6615                 :      855960 :   free ((*ivs)->n_inv_expr_uses);
    6616                 :      855960 :   free (*ivs);
    6617                 :      855960 :   *ivs = NULL;
    6618                 :      855960 : }
    6619                 :             : 
    6620                 :             : /* Dumps IVS to FILE.  */
    6621                 :             : 
    6622                 :             : static void
    6623                 :         297 : iv_ca_dump (struct ivopts_data *data, FILE *file, class iv_ca *ivs)
    6624                 :             : {
    6625                 :         297 :   unsigned i;
    6626                 :         297 :   comp_cost cost = iv_ca_cost (ivs);
    6627                 :             : 
    6628                 :         297 :   fprintf (file, "  cost: %" PRId64 " (complexity %d)\n", cost.cost,
    6629                 :             :            cost.complexity);
    6630                 :         297 :   fprintf (file, "  reg_cost: %d\n",
    6631                 :             :            ivopts_estimate_reg_pressure (data, ivs->n_invs, ivs->n_cands));
    6632                 :         297 :   fprintf (file, "  cand_cost: %" PRId64 "\n  cand_group_cost: "
    6633                 :             :            "%" PRId64 " (complexity %d)\n", ivs->cand_cost,
    6634                 :             :            ivs->cand_use_cost.cost, ivs->cand_use_cost.complexity);
    6635                 :         297 :   bitmap_print (file, ivs->cands, "  candidates: ","\n");
    6636                 :             : 
    6637                 :        1643 :   for (i = 0; i < ivs->upto; i++)
    6638                 :             :     {
    6639                 :        1346 :       struct iv_group *group = data->vgroups[i];
    6640                 :        1346 :       class cost_pair *cp = iv_ca_cand_for_group (ivs, group);
    6641                 :        1346 :       if (cp)
    6642                 :        1346 :         fprintf (file, "   group:%d --> iv_cand:%d, cost=("
    6643                 :        1346 :                  "%" PRId64 ",%d)\n", group->id, cp->cand->id,
    6644                 :             :                  cp->cost.cost, cp->cost.complexity);
    6645                 :             :       else
    6646                 :           0 :         fprintf (file, "   group:%d --> ??\n", group->id);
    6647                 :             :     }
    6648                 :             : 
    6649                 :         297 :   const char *pref = "";
    6650                 :         297 :   fprintf (file, "  invariant variables: ");
    6651                 :        1860 :   for (i = 1; i <= data->max_inv_var_id; i++)
    6652                 :        1266 :     if (ivs->n_inv_var_uses[i])
    6653                 :             :       {
    6654                 :         158 :         fprintf (file, "%s%d", pref, i);
    6655                 :         158 :         pref = ", ";
    6656                 :             :       }
    6657                 :             : 
    6658                 :         297 :   pref = "";
    6659                 :         297 :   fprintf (file, "\n  invariant expressions: ");
    6660                 :        3225 :   for (i = 1; i <= data->max_inv_expr_id; i++)
    6661                 :        2631 :     if (ivs->n_inv_expr_uses[i])
    6662                 :             :       {
    6663                 :         371 :         fprintf (file, "%s%d", pref, i);
    6664                 :         371 :         pref = ", ";
    6665                 :             :       }
    6666                 :             : 
    6667                 :         297 :   fprintf (file, "\n\n");
    6668                 :         297 : }
    6669                 :             : 
    6670                 :             : /* Try changing candidate in IVS to CAND for each use.  Return cost of the
    6671                 :             :    new set, and store differences in DELTA.  Number of induction variables
    6672                 :             :    in the new set is stored to N_IVS. MIN_NCAND is a flag. When it is true
    6673                 :             :    the function will try to find a solution with mimimal iv candidates.  */
    6674                 :             : 
    6675                 :             : static comp_cost
    6676                 :    18458084 : iv_ca_extend (struct ivopts_data *data, class iv_ca *ivs,
    6677                 :             :               struct iv_cand *cand, struct iv_ca_delta **delta,
    6678                 :             :               unsigned *n_ivs, bool min_ncand)
    6679                 :             : {
    6680                 :    18458084 :   unsigned i;
    6681                 :    18458084 :   comp_cost cost;
    6682                 :    18458084 :   struct iv_group *group;
    6683                 :    18458084 :   class cost_pair *old_cp, *new_cp;
    6684                 :             : 
    6685                 :    18458084 :   *delta = NULL;
    6686                 :   101172555 :   for (i = 0; i < ivs->upto; i++)
    6687                 :             :     {
    6688                 :    82714471 :       group = data->vgroups[i];
    6689                 :    82714471 :       old_cp = iv_ca_cand_for_group (ivs, group);
    6690                 :             : 
    6691                 :    82714471 :       if (old_cp
    6692                 :    82714471 :           && old_cp->cand == cand)
    6693                 :     7697196 :         continue;
    6694                 :             : 
    6695                 :    75017275 :       new_cp = get_group_iv_cost (data, group, cand);
    6696                 :    75017275 :       if (!new_cp)
    6697                 :    32445324 :         continue;
    6698                 :             : 
    6699                 :    42571951 :       if (!min_ncand)
    6700                 :             :         {
    6701                 :    31329834 :           int cmp_invs = iv_ca_compare_deps (data, ivs, group, old_cp, new_cp);
    6702                 :             :           /* Skip if new_cp depends on more invariants.  */
    6703                 :    31329834 :           if (cmp_invs > 0)
    6704                 :     7054037 :             continue;
    6705                 :             : 
    6706                 :    24275797 :           int cmp_cost = compare_cost_pair (new_cp, old_cp);
    6707                 :             :           /* Skip if new_cp is not cheaper.  */
    6708                 :    24275797 :           if (cmp_cost > 0 || (cmp_cost == 0 && cmp_invs == 0))
    6709                 :    18553941 :             continue;
    6710                 :             :         }
    6711                 :             : 
    6712                 :    16963973 :       *delta = iv_ca_delta_add (group, old_cp, new_cp, *delta);
    6713                 :             :     }
    6714                 :             : 
    6715                 :    18458084 :   iv_ca_delta_commit (data, ivs, *delta, true);
    6716                 :    18458084 :   cost = iv_ca_cost (ivs);
    6717                 :    18458084 :   if (n_ivs)
    6718                 :    10760888 :     *n_ivs = iv_ca_n_cands (ivs);
    6719                 :    18458084 :   iv_ca_delta_commit (data, ivs, *delta, false);
    6720                 :             : 
    6721                 :    18458084 :   return cost;
    6722                 :             : }
    6723                 :             : 
    6724                 :             : /* Try narrowing set IVS by removing CAND.  Return the cost of
    6725                 :             :    the new set and store the differences in DELTA.  START is
    6726                 :             :    the candidate with which we start narrowing.  */
    6727                 :             : 
    6728                 :             : static comp_cost
    6729                 :    13263330 : iv_ca_narrow (struct ivopts_data *data, class iv_ca *ivs,
    6730                 :             :               struct iv_cand *cand, struct iv_cand *start,
    6731                 :             :               struct iv_ca_delta **delta)
    6732                 :             : {
    6733                 :    13263330 :   unsigned i, ci;
    6734                 :    13263330 :   struct iv_group *group;
    6735                 :    13263330 :   class cost_pair *old_cp, *new_cp, *cp;
    6736                 :    13263330 :   bitmap_iterator bi;
    6737                 :    13263330 :   struct iv_cand *cnd;
    6738                 :    13263330 :   comp_cost cost, best_cost, acost;
    6739                 :             : 
    6740                 :    13263330 :   *delta = NULL;
    6741                 :   143767748 :   for (i = 0; i < data->vgroups.length (); i++)
    6742                 :             :     {
    6743                 :    67642911 :       group = data->vgroups[i];
    6744                 :             : 
    6745                 :    67642911 :       old_cp = iv_ca_cand_for_group (ivs, group);
    6746                 :    67642911 :       if (old_cp->cand != cand)
    6747                 :    49394639 :         continue;
    6748                 :             : 
    6749                 :    18248272 :       best_cost = iv_ca_cost (ivs);
    6750                 :             :       /* Start narrowing with START.  */
    6751                 :    18248272 :       new_cp = get_group_iv_cost (data, group, start);
    6752                 :             : 
    6753                 :    18248272 :       if (data->consider_all_candidates)
    6754                 :             :         {
    6755                 :    82211870 :           EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, ci, bi)
    6756                 :             :             {
    6757                 :    64727793 :               if (ci == cand->id || (start && ci == start->id))
    6758                 :    29975454 :                 continue;
    6759                 :             : 
    6760                 :    34752339 :               cnd = data->vcands[ci];
    6761                 :             : 
    6762                 :    34752339 :               cp = get_group_iv_cost (data, group, cnd);
    6763                 :    34752339 :               if (!cp)
    6764                 :    22797311 :                 continue;
    6765                 :             : 
    6766                 :    11955028 :               iv_ca_set_cp (data, ivs, group, cp);
    6767                 :    11955028 :               acost = iv_ca_cost (ivs);
    6768                 :             : 
    6769                 :    11955028 :               if (acost < best_cost)
    6770                 :             :                 {
    6771                 :     1684656 :                   best_cost = acost;
    6772                 :     1684656 :                   new_cp = cp;
    6773                 :             :                 }
    6774                 :             :             }
    6775                 :             :         }
    6776                 :             :       else
    6777                 :             :         {
    6778                 :     2703197 :           EXECUTE_IF_AND_IN_BITMAP (group->related_cands, ivs->cands, 0, ci, bi)
    6779                 :             :             {
    6780                 :     1939002 :               if (ci == cand->id || (start && ci == start->id))
    6781                 :     1076847 :                 continue;
    6782                 :             : 
    6783                 :      862155 :               cnd = data->vcands[ci];
    6784                 :             : 
    6785                 :      862155 :               cp = get_group_iv_cost (data, group, cnd);
    6786                 :      862155 :               if (!cp)
    6787                 :           0 :                 continue;
    6788                 :             : 
    6789                 :      862155 :               iv_ca_set_cp (data, ivs, group, cp);
    6790                 :      862155 :               acost = iv_ca_cost (ivs);
    6791                 :             : 
    6792                 :      862155 :               if (acost < best_cost)
    6793                 :             :                 {
    6794                 :       28555 :                   best_cost = acost;
    6795                 :       28555 :                   new_cp = cp;
    6796                 :             :                 }
    6797                 :             :             }
    6798                 :             :         }
    6799                 :             :       /* Restore to old cp for use.  */
    6800                 :    18248272 :       iv_ca_set_cp (data, ivs, group, old_cp);
    6801                 :             : 
    6802                 :    18248272 :       if (!new_cp)
    6803                 :             :         {
    6804                 :     9022367 :           iv_ca_delta_free (delta);
    6805                 :     9022367 :           return infinite_cost;
    6806                 :             :         }
    6807                 :             : 
    6808                 :     9225905 :       *delta = iv_ca_delta_add (group, old_cp, new_cp, *delta);
    6809                 :             :     }
    6810                 :             : 
    6811                 :     4240963 :   iv_ca_delta_commit (data, ivs, *delta, true);
    6812                 :     4240963 :   cost = iv_ca_cost (ivs);
    6813                 :     4240963 :   iv_ca_delta_commit (data, ivs, *delta, false);
    6814                 :             : 
    6815                 :     4240963 :   return cost;
    6816                 :             : }
    6817                 :             : 
    6818                 :             : /* Try optimizing the set of candidates IVS by removing candidates different
    6819                 :             :    from to EXCEPT_CAND from it.  Return cost of the new set, and store
    6820                 :             :    differences in DELTA.  */
    6821                 :             : 
    6822                 :             : static comp_cost
    6823                 :     7817592 : iv_ca_prune (struct ivopts_data *data, class iv_ca *ivs,
    6824                 :             :              struct iv_cand *except_cand, struct iv_ca_delta **delta)
    6825                 :             : {
    6826                 :     7817592 :   bitmap_iterator bi;
    6827                 :     7817592 :   struct iv_ca_delta *act_delta, *best_delta;
    6828                 :     7817592 :   unsigned i;
    6829                 :     7817592 :   comp_cost best_cost, acost;
    6830                 :     7817592 :   struct iv_cand *cand;
    6831                 :             : 
    6832                 :     7817592 :   best_delta = NULL;
    6833                 :     7817592 :   best_cost = iv_ca_cost (ivs);
    6834                 :             : 
    6835                 :    26552704 :   EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi)
    6836                 :             :     {
    6837                 :    18735112 :       cand = data->vcands[i];
    6838                 :             : 
    6839                 :    18735112 :       if (cand == except_cand)
    6840                 :     5471782 :         continue;
    6841                 :             : 
    6842                 :    13263330 :       acost = iv_ca_narrow (data, ivs, cand, except_cand, &act_delta);
    6843                 :             : 
    6844                 :    13263330 :       if (acost < best_cost)
    6845                 :             :         {
    6846                 :     2188696 :           best_cost = acost;
    6847                 :     2188696 :           iv_ca_delta_free (&best_delta);
    6848                 :     2188696 :           best_delta = act_delta;
    6849                 :             :         }
    6850                 :             :       else
    6851                 :    11074634 :         iv_ca_delta_free (&act_delta);
    6852                 :             :     }
    6853                 :             : 
    6854                 :     7817592 :   if (!best_delta)
    6855                 :             :     {
    6856                 :     5781071 :       *delta = NULL;
    6857                 :     5781071 :       return best_cost;
    6858                 :             :     }
    6859                 :             : 
    6860                 :             :   /* Recurse to possibly remove other unnecessary ivs.  */
    6861                 :     2036521 :   iv_ca_delta_commit (data, ivs, best_delta, true);
    6862                 :     2036521 :   best_cost = iv_ca_prune (data, ivs, except_cand, delta);
    6863                 :     2036521 :   iv_ca_delta_commit (data, ivs, best_delta, false);
    6864                 :     2036521 :   *delta = iv_ca_delta_join (best_delta, *delta);
    6865                 :     2036521 :   return best_cost;
    6866                 :             : }
    6867                 :             : 
    6868                 :             : /* Check if CAND_IDX is a candidate other than OLD_CAND and has
    6869                 :             :    cheaper local cost for GROUP than BEST_CP.  Return pointer to
    6870                 :             :    the corresponding cost_pair, otherwise just return BEST_CP.  */
    6871                 :             : 
    6872                 :             : static class cost_pair*
    6873                 :    25838655 : cheaper_cost_with_cand (struct ivopts_data *data, struct iv_group *group,
    6874                 :             :                         unsigned int cand_idx, struct iv_cand *old_cand,
    6875                 :             :                         class cost_pair *best_cp)
    6876                 :             : {
    6877                 :    25838655 :   struct iv_cand *cand;
    6878                 :    25838655 :   class cost_pair *cp;
    6879                 :             : 
    6880                 :    25838655 :   gcc_assert (old_cand != NULL && best_cp != NULL);
    6881                 :    25838655 :   if (cand_idx == old_cand->id)
    6882                 :             :     return best_cp;
    6883                 :             : 
    6884                 :    23438484 :   cand = data->vcands[cand_idx];
    6885                 :    23438484 :   cp = get_group_iv_cost (data, group, cand);
    6886                 :    23438484 :   if (cp != NULL && cheaper_cost_pair (cp, best_cp))
    6887                 :             :     return cp;
    6888                 :             : 
    6889                 :             :   return best_cp;
    6890                 :             : }
    6891                 :             : 
    6892                 :             : /* Try breaking local optimal fixed-point for IVS by replacing candidates
    6893                 :             :    which are used by more than one iv uses.  For each of those candidates,
    6894                 :             :    this function tries to represent iv uses under that candidate using
    6895                 :             :    other ones with lower local cost, then tries to prune the new set.
    6896                 :             :    If the new set has lower cost, It returns the new cost after recording
    6897                 :             :    candidate replacement in list DELTA.  */
    6898                 :             : 
    6899                 :             : static comp_cost
    6900                 :      855016 : iv_ca_replace (struct ivopts_data *data, class iv_ca *ivs,
    6901                 :             :                struct iv_ca_delta **delta)
    6902                 :             : {
    6903                 :      855016 :   bitmap_iterator bi, bj;
    6904                 :      855016 :   unsigned int i, j, k;
    6905                 :      855016 :   struct iv_cand *cand;
    6906                 :      855016 :   comp_cost orig_cost, acost;
    6907                 :      855016 :   struct iv_ca_delta *act_delta, *tmp_delta;
    6908                 :      855016 :   class cost_pair *old_cp, *best_cp = NULL;
    6909                 :             : 
    6910                 :      855016 :   *delta = NULL;
    6911                 :      855016 :   orig_cost = iv_ca_cost (ivs);
    6912                 :             : 
    6913                 :     1970381 :   EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi)
    6914                 :             :     {
    6915                 :     1139706 :       if (ivs->n_cand_uses[i] == 1
    6916                 :      866568 :           || ivs->n_cand_uses[i] > ALWAYS_PRUNE_CAND_SET_BOUND)
    6917                 :      278783 :         continue;
    6918                 :             : 
    6919                 :      860923 :       cand = data->vcands[i];
    6920                 :             : 
    6921                 :      860923 :       act_delta = NULL;
    6922                 :             :       /*  Represent uses under current candidate using other ones with
    6923                 :             :           lower local cost.  */
    6924                 :     4570415 :       for (j = 0; j < ivs->upto; j++)
    6925                 :             :         {
    6926                 :     3709492 :           struct iv_group *group = data->vgroups[j];
    6927                 :     3709492 :           old_cp = iv_ca_cand_for_group (ivs, group);
    6928                 :             : 
    6929                 :     3709492 :           if (old_cp->cand != cand)
    6930                 :     1309321 :             continue;
    6931                 :             : 
    6932                 :     2400171 :           best_cp = old_cp;
    6933                 :     2400171 :           if (data->consider_all_candidates)
    6934                 :    56278012 :             for (k = 0; k < data->vcands.length (); k++)
    6935                 :    25748624 :               best_cp = cheaper_cost_with_cand (data, group, k,
    6936                 :             :                                                 old_cp->cand, best_cp);
    6937                 :             :           else
    6938                 :       99820 :             EXECUTE_IF_SET_IN_BITMAP (group->related_cands, 0, k, bj)
    6939                 :       90031 :               best_cp = cheaper_cost_with_cand (data, group, k,
    6940                 :             :                                                 old_cp->cand, best_cp);
    6941                 :             : 
    6942                 :     2400171 :           if (best_cp == old_cp)
    6943                 :     1078456 :             continue;
    6944                 :             : 
    6945                 :     1321715 :           act_delta = iv_ca_delta_add (group, old_cp, best_cp, act_delta);
    6946                 :             :         }
    6947                 :             :       /* No need for further prune.  */
    6948                 :      860923 :       if (!act_delta)
    6949                 :      192172 :         continue;
    6950                 :             : 
    6951                 :             :       /* Prune the new candidate set.  */
    6952                 :      668751 :       iv_ca_delta_commit (data, ivs, act_delta, true);
    6953                 :      668751 :       acost = iv_ca_prune (data, ivs, NULL, &tmp_delta);
    6954                 :      668751 :       iv_ca_delta_commit (data, ivs, act_delta, false);
    6955                 :      668751 :       act_delta = iv_ca_delta_join (act_delta, tmp_delta);
    6956                 :             : 
    6957                 :      668751 :       if (acost < orig_cost)
    6958                 :             :         {
    6959                 :       24341 :           *delta = act_delta;
    6960                 :       24341 :           return acost;
    6961                 :             :         }
    6962                 :             :       else
    6963                 :      644410 :         iv_ca_delta_free (&act_delta);
    6964                 :             :     }
    6965                 :             : 
    6966                 :      830675 :   return orig_cost;
    6967                 :             : }
    6968                 :             : 
    6969                 :             : /* Tries to extend the sets IVS in the best possible way in order to
    6970                 :             :    express the GROUP.  If ORIGINALP is true, prefer candidates from
    6971                 :             :    the original set of IVs, otherwise favor important candidates not
    6972                 :             :    based on any memory object.  */
    6973                 :             : 
    6974                 :             : static bool
    6975                 :     2784490 : try_add_cand_for (struct ivopts_data *data, class iv_ca *ivs,
    6976                 :             :                   struct iv_group *group, bool originalp)
    6977                 :             : {
    6978                 :     2784490 :   comp_cost best_cost, act_cost;
    6979                 :     2784490 :   unsigned i;
    6980                 :     2784490 :   bitmap_iterator bi;
    6981                 :     2784490 :   struct iv_cand *cand;
    6982                 :     2784490 :   struct iv_ca_delta *best_delta = NULL, *act_delta;
    6983                 :     2784490 :   class cost_pair *cp;
    6984                 :             : 
    6985                 :     2784490 :   iv_ca_add_group (data, ivs, group);
    6986                 :     2784490 :   best_cost = iv_ca_cost (ivs);
    6987                 :     2784490 :   cp = iv_ca_cand_for_group (ivs, group);
    6988                 :     2784490 :   if (cp)
    6989                 :             :     {
    6990                 :     2702137 :       best_delta = iv_ca_delta_add (group, NULL, cp, NULL);
    6991                 :     2702137 :       iv_ca_set_no_cp (data, ivs, group);
    6992                 :             :     }
    6993                 :             : 
    6994                 :             :   /* If ORIGINALP is true, try to find the original IV for the use.  Otherwise
    6995                 :             :      first try important candidates not based on any memory object.  Only if
    6996                 :             :      this fails, try the specific ones.  Rationale -- in loops with many
    6997                 :             :      variables the best choice often is to use just one generic biv.  If we
    6998                 :             :      added here many ivs specific to the uses, the optimization algorithm later
    6999                 :             :      would be likely to get stuck in a local minimum, thus causing us to create
    7000                 :             :      too many ivs.  The approach from few ivs to more seems more likely to be
    7001                 :             :      successful -- starting from few ivs, replacing an expensive use by a
    7002                 :             :      specific iv should always be a win.  */
    7003                 :    25942134 :   EXECUTE_IF_SET_IN_BITMAP (group->related_cands, 0, i, bi)
    7004                 :             :     {
    7005                 :    23157644 :       cand = data->vcands[i];
    7006                 :             : 
    7007                 :    23157644 :       if (originalp && cand->pos !=IP_ORIGINAL)
    7008                 :     9166443 :         continue;
    7009                 :             : 
    7010                 :    11578822 :       if (!originalp && cand->iv->base_object != NULL_TREE)
    7011                 :     1975132 :         continue;
    7012                 :             : 
    7013                 :    12016069 :       if (iv_ca_cand_used_p (ivs, cand))
    7014                 :     1290136 :         continue;
    7015                 :             : 
    7016                 :    10725933 :       cp = get_group_iv_cost (data, group, cand);
    7017                 :    10725933 :       if (!cp)
    7018                 :     3138196 :         continue;
    7019                 :             : 
    7020                 :     7587737 :       iv_ca_set_cp (data, ivs, group, cp);
    7021                 :     7587737 :       act_cost = iv_ca_extend (data, ivs, cand, &act_delta, NULL,
    7022                 :             :                                true);
    7023                 :     7587737 :       iv_ca_set_no_cp (data, ivs, group);
    7024                 :     7587737 :       act_delta = iv_ca_delta_add (group, NULL, cp, act_delta);
    7025                 :             : 
    7026                 :     7587737 :       if (act_cost < best_cost)
    7027                 :             :         {
    7028                 :      364843 :           best_cost = act_cost;
    7029                 :             : 
    7030                 :      364843 :           iv_ca_delta_free (&best_delta);
    7031                 :      364843 :           best_delta = act_delta;
    7032                 :             :         }
    7033                 :             :       else
    7034                 :     7222894 :         iv_ca_delta_free (&act_delta);
    7035                 :             :     }
    7036                 :             : 
    7037                 :     2784490 :   if (best_cost.infinite_cost_p ())
    7038                 :             :     {
    7039                 :      712301 :       for (i = 0; i < group->n_map_members; i++)
    7040                 :             :         {
    7041                 :      652996 :           cp = group->cost_map + i;
    7042                 :      652996 :           cand = cp->cand;
    7043                 :      652996 :           if (!cand)
    7044                 :      543537 :             continue;
    7045                 :             : 
    7046                 :             :           /* Already tried this.  */
    7047                 :      109459 :           if (cand->important)
    7048                 :             :             {
    7049                 :           0 :               if (originalp && cand->pos == IP_ORIGINAL)
    7050                 :           0 :                 continue;
    7051                 :           0 :               if (!originalp && cand->iv->base_object == NULL_TREE)
    7052                 :           0 :                 continue;
    7053                 :             :             }
    7054                 :             : 
    7055                 :      109459 :           if (iv_ca_cand_used_p (ivs, cand))
    7056                 :           0 :             continue;
    7057                 :             : 
    7058                 :      109459 :           act_delta = NULL;
    7059                 :      109459 :           iv_ca_set_cp (data, ivs, group, cp);
    7060                 :      109459 :           act_cost = iv_ca_extend (data, ivs, cand, &act_delta, NULL, true);
    7061                 :      109459 :           iv_ca_set_no_cp (data, ivs, group);
    7062                 :      109459 :           act_delta = iv_ca_delta_add (group,
    7063                 :             :                                        iv_ca_cand_for_group (ivs, group),
    7064                 :             :                                        cp, act_delta);
    7065                 :             : 
    7066                 :      109459 :           if (act_cost < best_cost)
    7067                 :             :             {
    7068                 :       62220 :               best_cost = act_cost;
    7069                 :             : 
    7070                 :       62220 :               if (best_delta)
    7071                 :        3859 :                 iv_ca_delta_free (&best_delta);
    7072                 :       62220 :               best_delta = act_delta;
    7073                 :             :             }
    7074                 :             :           else
    7075                 :       47239 :             iv_ca_delta_free (&act_delta);
    7076                 :             :         }
    7077                 :             :     }
    7078                 :             : 
    7079                 :     2784490 :   iv_ca_delta_commit (data, ivs, best_delta, true);
    7080                 :     2784490 :   iv_ca_delta_free (&best_delta);
    7081                 :             : 
    7082                 :     2784490 :   return !best_cost.infinite_cost_p ();
    7083                 :             : }
    7084                 :             : 
    7085                 :             : /* Finds an initial assignment of candidates to uses.  */
    7086                 :             : 
    7087                 :             : static class iv_ca *
    7088                 :      855960 : get_initial_solution (struct ivopts_data *data, bool originalp)
    7089                 :             : {
    7090                 :      855960 :   unsigned i;
    7091                 :      855960 :   class iv_ca *ivs = iv_ca_new (data);
    7092                 :             : 
    7093                 :     7279012 :   for (i = 0; i < data->vgroups.length (); i++)
    7094                 :     2784490 :     if (!try_add_cand_for (data, ivs, data->vgroups[i], originalp))
    7095                 :             :       {
    7096                 :         944 :         iv_ca_free (&ivs);
    7097                 :         944 :         return NULL;
    7098                 :             :       }
    7099                 :             : 
    7100                 :             :   return ivs;
    7101                 :             : }
    7102                 :             : 
    7103                 :             : /* Tries to improve set of induction variables IVS.  TRY_REPLACE_P
    7104                 :             :    points to a bool variable, this function tries to break local
    7105                 :             :    optimal fixed-point by replacing candidates in IVS if it's true.  */
    7106                 :             : 
    7107                 :             : static bool
    7108                 :     1241120 : try_improve_iv_set (struct ivopts_data *data,
    7109                 :             :                     class iv_ca *ivs, bool *try_replace_p)
    7110                 :             : {
    7111                 :     1241120 :   unsigned i, n_ivs;
    7112                 :     1241120 :   comp_cost acost, best_cost = iv_ca_cost (ivs);
    7113                 :     1241120 :   struct iv_ca_delta *best_delta = NULL, *act_delta, *tmp_delta;
    7114                 :     1241120 :   struct iv_cand *cand;
    7115                 :             : 
    7116                 :             :   /* Try extending the set of induction variables by one.  */
    7117                 :    27694682 :   for (i = 0; i < data->vcands.length (); i++)
    7118                 :             :     {
    7119                 :    12606221 :       cand = data->vcands[i];
    7120                 :             : 
    7121                 :    12606221 :       if (iv_ca_cand_used_p (ivs, cand))
    7122                 :     1845333 :         continue;
    7123                 :             : 
    7124                 :    10760888 :       acost = iv_ca_extend (data, ivs, cand, &act_delta, &n_ivs, false);
    7125                 :    10760888 :       if (!act_delta)
    7126                 :     6463850 :         continue;
    7127                 :             : 
    7128                 :             :       /* If we successfully added the candidate and the set is small enough,
    7129                 :             :          try optimizing it by removing other candidates.  */
    7130                 :     4297038 :       if (n_ivs <= ALWAYS_PRUNE_CAND_SET_BOUND)
    7131                 :             :         {
    7132                 :     4231120 :           iv_ca_delta_commit (data, ivs, act_delta, true);
    7133                 :     4231120 :           acost = iv_ca_prune (data, ivs, cand, &tmp_delta);
    7134                 :     4231120 :           iv_ca_delta_commit (data, ivs, act_delta, false);
    7135                 :     4231120 :           act_delta = iv_ca_delta_join (act_delta, tmp_delta);
    7136                 :             :         }
    7137                 :             : 
    7138                 :     4297038 :       if (acost < best_cost)
    7139                 :             :         {
    7140                 :      454258 :           best_cost = acost;
    7141                 :      454258 :           iv_ca_delta_free (&best_delta);
    7142                 :      454258 :           best_delta = act_delta;
    7143                 :             :         }
    7144                 :             :       else
    7145                 :     3842780 :         iv_ca_delta_free (&act_delta);
    7146                 :             :     }
    7147                 :             : 
    7148                 :     1241120 :   if (!best_delta)
    7149                 :             :     {
    7150                 :             :       /* Try removing the candidates from the set instead.  */
    7151                 :      881200 :       best_cost = iv_ca_prune (data, ivs, NULL, &best_delta);
    7152                 :             : 
    7153                 :      881200 :       if (!best_delta && *try_replace_p)
    7154                 :             :         {
    7155                 :      855016 :           *try_replace_p = false;
    7156                 :             :           /* So far candidate selecting algorithm tends to choose fewer IVs
    7157                 :             :              so that it can handle cases in which loops have many variables
    7158                 :             :              but the best choice is often to use only one general biv.  One
    7159                 :             :              weakness is it can't handle opposite cases, in which different
    7160                 :             :              candidates should be chosen with respect to each use.  To solve
    7161                 :             :              the problem, we replace candidates in a manner described by the
    7162                 :             :              comments of iv_ca_replace, thus give general algorithm a chance
    7163                 :             :              to break local optimal fixed-point in these cases.  */
    7164                 :      855016 :           best_cost = iv_ca_replace (data, ivs, &best_delta);
    7165                 :             :         }
    7166                 :             : 
    7167                 :      881200 :       if (!best_delta)
    7168                 :             :         return false;
    7169                 :             :     }
    7170                 :             : 
    7171                 :      386104 :   iv_ca_delta_commit (data, ivs, best_delta, true);
    7172                 :      386104 :   iv_ca_delta_free (&best_delta);
    7173                 :      772208 :   return best_cost == iv_ca_cost (ivs);
    7174                 :             : }
    7175                 :             : 
    7176                 :             : /* Attempts to find the optimal set of induction variables.  We do simple
    7177                 :             :    greedy heuristic -- we try to replace at most one candidate in the selected
    7178                 :             :    solution and remove the unused ivs while this improves the cost.  */
    7179                 :             : 
    7180                 :             : static class iv_ca *
    7181                 :      855960 : find_optimal_iv_set_1 (struct ivopts_data *data, bool originalp)
    7182                 :             : {
    7183                 :      855960 :   class iv_ca *set;
    7184                 :      855960 :   bool try_replace_p = true;
    7185                 :             : 
    7186                 :             :   /* Get the initial solution.  */
    7187                 :      855960 :   set = get_initial_solution (data, originalp);
    7188                 :      855960 :   if (!set)
    7189                 :             :     {
    7190                 :         944 :       if (dump_file && (dump_flags & TDF_DETAILS))
    7191                 :           0 :         fprintf (dump_file, "Unable to substitute for ivs, failed.\n");
    7192                 :         944 :       return NULL;
    7193                 :             :     }
    7194                 :             : 
    7195                 :      855016 :   if (dump_file && (dump_flags & TDF_DETAILS))
    7196                 :             :     {
    7197                 :         152 :       fprintf (dump_file, "Initial set of candidates:\n");
    7198                 :         152 :       iv_ca_dump (data, dump_file, set);
    7199                 :             :     }
    7200                 :             : 
    7201                 :     1241120 :   while (try_improve_iv_set (data, set, &try_replace_p))
    7202                 :             :     {
    7203                 :      386104 :       if (dump_file && (dump_flags & TDF_DETAILS))
    7204                 :             :         {
    7205                 :         145 :           fprintf (dump_file, "Improved to:\n");
    7206                 :         145 :           iv_ca_dump (data, dump_file, set);
    7207                 :             :         }
    7208                 :             :     }
    7209                 :             : 
    7210                 :             :   /* If the set has infinite_cost, it can't be optimal.  */
    7211                 :     1710032 :   if (iv_ca_cost (set).infinite_cost_p ())
    7212                 :             :     {
    7213                 :           0 :       if (dump_file && (dump_flags & TDF_DETAILS))
    7214                 :           0 :         fprintf (dump_file,
    7215                 :             :                  "Overflow to infinite cost in try_improve_iv_set.\n");
    7216                 :           0 :       iv_ca_free (&set);
    7217                 :             :     }
    7218                 :      855016 :   return set;
    7219                 :             : }
    7220                 :             : 
    7221                 :             : static class iv_ca *
    7222                 :      427980 : find_optimal_iv_set (struct ivopts_data *data)
    7223                 :             : {
    7224                 :      427980 :   unsigned i;
    7225                 :      427980 :   comp_cost cost, origcost;
    7226                 :      427980 :   class iv_ca *set, *origset;
    7227                 :             : 
    7228                 :             :   /* Determine the cost based on a strategy that starts with original IVs,
    7229                 :             :      and try again using a strategy that prefers candidates not based
    7230                 :             :      on any IVs.  */
    7231                 :      427980 :   origset = find_optimal_iv_set_1 (data, true);
    7232                 :      427980 :   set = find_optimal_iv_set_1 (data, false);
    7233                 :             : 
    7234                 :      427980 :   if (!origset && !set)
    7235                 :             :     return NULL;
    7236                 :             : 
    7237                 :      427508 :   origcost = origset ? iv_ca_cost (origset) : infinite_cost;
    7238                 :      427508 :   cost = set ? iv_ca_cost (set) : infinite_cost;
    7239                 :             : 
    7240                 :      427508 :   if (dump_file && (dump_flags & TDF_DETAILS))
    7241                 :             :     {
    7242                 :          76 :       fprintf (dump_file, "Original cost %" PRId64 " (complexity %d)\n\n",
    7243                 :             :                origcost.cost, origcost.complexity);
    7244                 :          76 :       fprintf (dump_file, "Final cost %" PRId64 " (complexity %d)\n\n",
    7245                 :             :                cost.cost, cost.complexity);
    7246                 :             :     }
    7247                 :             : 
    7248                 :             :   /* Choose the one with the best cost.  */
    7249                 :      427508 :   if (origcost <= cost)
    7250                 :             :     {
    7251                 :      394694 :       if (set)
    7252                 :      394694 :         iv_ca_free (&set);
    7253                 :      394694 :       set = origset;
    7254                 :             :     }
    7255                 :       32814 :   else if (origset)
    7256                 :       32814 :     iv_ca_free (&origset);
    7257                 :             : 
    7258                 :     3637898 :   for (i = 0; i < data->vgroups.length (); i++)
    7259                 :             :     {
    7260                 :     1391441 :       struct iv_group *group = data->vgroups[i];
    7261                 :     1391441 :       group->selected = iv_ca_cand_for_group (set, group)->cand;
    7262                 :             :     }
    7263                 :             : 
    7264                 :      427508 :   return set;
    7265                 :             : }
    7266                 :             : 
    7267                 :             : /* Creates a new induction variable corresponding to CAND.  */
    7268                 :             : 
    7269                 :             : static void
    7270                 :      565158 : create_new_iv (struct ivopts_data *data, struct iv_cand *cand)
    7271                 :             : {
    7272                 :      565158 :   gimple_stmt_iterator incr_pos;
    7273                 :      565158 :   tree base;
    7274                 :      565158 :   struct iv_use *use;
    7275                 :      565158 :   struct iv_group *group;
    7276                 :      565158 :   bool after = false;
    7277                 :             : 
    7278                 :      565158 :   gcc_assert (cand->iv != NULL);
    7279                 :             : 
    7280                 :      565158 :   switch (cand->pos)
    7281                 :             :     {
    7282                 :      388997 :     case IP_NORMAL:
    7283                 :      388997 :       incr_pos = gsi_last_bb (ip_normal_pos (data->current_loop));
    7284                 :      388997 :       break;
    7285                 :             : 
    7286                 :        8949 :     case IP_END:
    7287                 :        8949 :       incr_pos = gsi_last_bb (ip_end_pos (data->current_loop));
    7288                 :        8949 :       after = true;
    7289                 :        8949 :       if (!gsi_end_p (incr_pos) && stmt_ends_bb_p (gsi_stmt (incr_pos)))
    7290                 :             :         {
    7291                 :           1 :           edge e = find_edge (gsi_bb (incr_pos), data->current_loop->header);
    7292                 :           1 :           incr_pos = gsi_after_labels (split_edge (e));
    7293                 :           1 :           after = false;
    7294                 :             :         }
    7295                 :             :       break;
    7296                 :             : 
    7297                 :           0 :     case IP_AFTER_USE:
    7298                 :           0 :       after = true;
    7299                 :             :       /* fall through */
    7300                 :           0 :     case IP_BEFORE_USE:
    7301                 :           0 :       incr_pos = gsi_for_stmt (cand->incremented_at);
    7302                 :           0 :       break;
    7303                 :             : 
    7304                 :      167212 :     case IP_ORIGINAL:
    7305                 :             :       /* Mark that the iv is preserved.  */
    7306                 :      167212 :       name_info (data, cand->var_before)->preserve_biv = true;
    7307                 :      167212 :       name_info (data, cand->var_after)->preserve_biv = true;
    7308                 :             : 
    7309                 :             :       /* Rewrite the increment so that it uses var_before directly.  */
    7310                 :      167212 :       use = find_interesting_uses_op (data, cand->var_after);
    7311                 :      167212 :       group = data->vgroups[use->group_id];
    7312                 :      167212 :       group->selected = cand;
    7313                 :      167212 :       return;
    7314                 :             :     }
    7315                 :             : 
    7316                 :      397946 :   gimple_add_tmp_var (cand->var_before);
    7317                 :             : 
    7318                 :      397946 :   base = unshare_expr (cand->iv->base);
    7319                 :             : 
    7320                 :      397946 :   create_iv (base, PLUS_EXPR, unshare_expr (cand->iv->step),
    7321                 :             :              cand->var_before, data->current_loop,
    7322                 :             :              &incr_pos, after, &cand->var_before, &cand->var_after);
    7323                 :             : }
    7324                 :             : 
    7325                 :             : /* Creates new induction variables described in SET.  */
    7326                 :             : 
    7327                 :             : static void
    7328                 :      427508 : create_new_ivs (struct ivopts_data *data, class iv_ca *set)
    7329                 :             : {
    7330                 :      427508 :   unsigned i;
    7331                 :      427508 :   struct iv_cand *cand;
    7332                 :      427508 :   bitmap_iterator bi;
    7333                 :             : 
    7334                 :      992666 :   EXECUTE_IF_SET_IN_BITMAP (set->cands, 0, i, bi)
    7335                 :             :     {
    7336                 :      565158 :       cand = data->vcands[i];
    7337                 :      565158 :       create_new_iv (data, cand);
    7338                 :             :     }
    7339                 :             : 
    7340                 :      427508 :   if (dump_file && (dump_flags & TDF_DETAILS))
    7341                 :             :     {
    7342                 :          76 :       fprintf (dump_file, "Selected IV set for loop %d",
    7343                 :          76 :                data->current_loop->num);
    7344                 :          76 :       if (data->loop_loc != UNKNOWN_LOCATION)
    7345                 :          74 :         fprintf (dump_file, " at %s:%d", LOCATION_FILE (data->loop_loc),
    7346                 :         148 :                  LOCATION_LINE (data->loop_loc));
    7347                 :          76 :       fprintf (dump_file, ", " HOST_WIDE_INT_PRINT_DEC " avg niters",
    7348                 :             :                avg_loop_niter (data->current_loop));
    7349                 :          76 :       fprintf (dump_file, ", %lu IVs:\n", bitmap_count_bits (set->cands));
    7350                 :         202 :       EXECUTE_IF_SET_IN_BITMAP (set->cands, 0, i, bi)
    7351                 :             :         {
    7352                 :         126 :           cand = data->vcands[i];
    7353                 :         126 :           dump_cand (dump_file, cand);
    7354                 :             :         }
    7355                 :          76 :       fprintf (dump_file, "\n");
    7356                 :             :     }
    7357                 :      427508 : }
    7358                 :             : 
    7359                 :             : /* Rewrites USE (definition of iv used in a nonlinear expression)
    7360                 :             :    using candidate CAND.  */
    7361                 :             : 
    7362                 :             : static void
    7363                 :      521931 : rewrite_use_nonlinear_expr (struct ivopts_data *data,
    7364                 :             :                             struct iv_use *use, struct iv_cand *cand)
    7365                 :             : {
    7366                 :      521931 :   gassign *ass;
    7367                 :      521931 :   gimple_stmt_iterator bsi;
    7368                 :      521931 :   tree comp, type = get_use_type (use), tgt;
    7369                 :             : 
    7370                 :             :   /* An important special case -- if we are asked to express value of
    7371                 :             :      the original iv by itself, just exit; there is no need to
    7372                 :             :      introduce a new computation (that might also need casting the
    7373                 :             :      variable to unsigned and back).  */
    7374                 :      521931 :   if (cand->pos == IP_ORIGINAL
    7375                 :      278585 :       && cand->incremented_at == use->stmt)
    7376                 :             :     {
    7377                 :      167212 :       tree op = NULL_TREE;
    7378                 :      167212 :       enum tree_code stmt_code;
    7379                 :             : 
    7380                 :      167212 :       gcc_assert (is_gimple_assign (use->stmt));
    7381                 :      167212 :       gcc_assert (gimple_assign_lhs (use->stmt) == cand->var_after);
    7382                 :             : 
    7383                 :             :       /* Check whether we may leave the computation unchanged.
    7384                 :             :          This is the case only if it does not rely on other
    7385                 :             :          computations in the loop -- otherwise, the computation
    7386                 :             :          we rely upon may be removed in remove_unused_ivs,
    7387                 :             :          thus leading to ICE.  */
    7388                 :      167212 :       stmt_code = gimple_assign_rhs_code (use->stmt);
    7389                 :      167212 :       if (stmt_code == PLUS_EXPR
    7390                 :      167212 :           || stmt_code == MINUS_EXPR
    7391                 :      167212 :           || stmt_code == POINTER_PLUS_EXPR)
    7392                 :             :         {
    7393                 :      163922 :           if (gimple_assign_rhs1 (use->stmt) == cand->var_before)
    7394                 :      162093 :             op = gimple_assign_rhs2 (use->stmt);
    7395                 :        1829 :           else if (gimple_assign_rhs2 (use->stmt) == cand->var_before)
    7396                 :             :             op = gimple_assign_rhs1 (use->stmt);
    7397                 :             :         }
    7398                 :             : 
    7399                 :      162624 :       if (op != NULL_TREE)
    7400                 :             :         {
    7401                 :      162624 :           if (expr_invariant_in_loop_p (data->current_loop, op))
    7402                 :      227282 :             return;
    7403                 :         174 :           if (TREE_CODE (op) == SSA_NAME)
    7404                 :             :             {
    7405                 :         174 :               struct iv *iv = get_iv (data, op);
    7406                 :         174 :               if (iv != NULL && integer_zerop (iv->step))
    7407                 :             :                 return;
    7408                 :             :             }
    7409                 :             :         }
    7410                 :             :     }
    7411                 :             : 
    7412                 :      359307 :   switch (gimple_code (use->stmt))
    7413                 :             :     {
    7414                 :      102216 :     case GIMPLE_PHI:
    7415                 :      102216 :       tgt = PHI_RESULT (use->stmt);
    7416                 :             : 
    7417                 :             :       /* If we should keep the biv, do not replace it.  */
    7418                 :      102216 :       if (name_info (data, tgt)->preserve_biv)
    7419                 :             :         return;
    7420                 :             : 
    7421                 :       37558 :       bsi = gsi_after_labels (gimple_bb (use->stmt));
    7422                 :       37558 :       break;
    7423                 :             : 
    7424                 :      257091 :     case GIMPLE_ASSIGN:
    7425                 :      257091 :       tgt = gimple_assign_lhs (use->stmt);
    7426                 :      257091 :       bsi = gsi_for_stmt (use->stmt);
    7427                 :      257091 :       break;
    7428                 :             : 
    7429                 :           0 :     default:
    7430                 :           0 :       gcc_unreachable ();
    7431                 :             :     }
    7432                 :             : 
    7433                 :      883947 :   aff_tree aff_inv, aff_var;
    7434                 :      294649 :   if (!get_computation_aff_1 (data->current_loop, use->stmt,
    7435                 :             :                               use, cand, &aff_inv, &aff_var))
    7436                 :           0 :     gcc_unreachable ();
    7437                 :             : 
    7438                 :      294649 :   unshare_aff_combination (&aff_inv);
    7439                 :      294649 :   unshare_aff_combination (&aff_var);
    7440                 :             :   /* Prefer CSE opportunity than loop invariant by adding offset at last
    7441                 :             :      so that iv_uses have different offsets can be CSEed.  */
    7442                 :      589298 :   poly_widest_int offset = aff_inv.offset;
    7443                 :      294649 :   aff_inv.offset = 0;
    7444                 :             : 
    7445                 :      294649 :   gimple_seq stmt_list = NULL, seq = NULL;
    7446                 :      294649 :   tree comp_op1 = aff_combination_to_tree (&aff_inv);
    7447                 :      294649 :   tree comp_op2 = aff_combination_to_tree (&aff_var);
    7448                 :      294649 :   gcc_assert (comp_op1 && comp_op2);
    7449                 :             : 
    7450                 :      294649 :   comp_op1 = force_gimple_operand (comp_op1, &seq, true, NULL);
    7451                 :      294649 :   gimple_seq_add_seq (&stmt_list, seq);
    7452                 :      294649 :   comp_op2 = force_gimple_operand (comp_op2, &seq, true, NULL);
    7453                 :      294649 :   gimple_seq_add_seq (&stmt_list, seq);
    7454                 :             : 
    7455                 :      294649 :   if (POINTER_TYPE_P (TREE_TYPE (comp_op2)))
    7456                 :             :     std::swap (comp_op1, comp_op2);
    7457                 :             : 
    7458                 :      294649 :   if (POINTER_TYPE_P (TREE_TYPE (comp_op1)))
    7459                 :             :     {
    7460                 :           0 :       comp = fold_build_pointer_plus (comp_op1,
    7461                 :             :                                       fold_convert (sizetype, comp_op2));
    7462                 :           0 :       comp = fold_build_pointer_plus (comp,
    7463                 :             :                                       wide_int_to_tree (sizetype, offset));
    7464                 :             :     }
    7465                 :             :   else
    7466                 :             :     {
    7467                 :      294649 :       comp = fold_build2 (PLUS_EXPR, TREE_TYPE (comp_op1), comp_op1,
    7468                 :             :                           fold_convert (TREE_TYPE (comp_op1), comp_op2));
    7469                 :      294649 :       comp = fold_build2 (PLUS_EXPR, TREE_TYPE (comp_op1), comp,
    7470                 :             :                           wide_int_to_tree (TREE_TYPE (comp_op1), offset));
    7471                 :             :     }
    7472                 :             : 
    7473                 :      294649 :   comp = fold_convert (type, comp);
    7474                 :      294649 :   comp = force_gimple_operand (comp, &seq, false, NULL);
    7475                 :      294649 :   gimple_seq_add_seq (&stmt_list, seq);
    7476                 :      294649 :   if (gimple_code (use->stmt) != GIMPLE_PHI
    7477                 :             :       /* We can't allow re-allocating the stmt as it might be pointed
    7478                 :             :          to still.  */
    7479                 :      294649 :       && (get_gimple_rhs_num_ops (TREE_CODE (comp))
    7480                 :      257091 :           >= gimple_num_ops (gsi_stmt (bsi))))
    7481                 :             :     {
    7482                 :       20595 :       comp = force_gimple_operand (comp, &seq, true, NULL);
    7483                 :       20595 :       gimple_seq_add_seq (&stmt_list, seq);
    7484                 :       20595 :       if (POINTER_TYPE_P (TREE_TYPE (tgt)))
    7485                 :             :         {
    7486                 :           0 :           duplicate_ssa_name_ptr_info (comp, SSA_NAME_PTR_INFO (tgt));
    7487                 :             :           /* As this isn't a plain copy we have to reset alignment
    7488                 :             :              information.  */
    7489                 :           0 :           if (SSA_NAME_PTR_INFO (comp))
    7490                 :           0 :             mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (comp));
    7491                 :             :         }
    7492                 :             :     }
    7493                 :             : 
    7494                 :      294649 :   gsi_insert_seq_before (&bsi, stmt_list, GSI_SAME_STMT);
    7495                 :      294649 :   if (gimple_code (use->stmt) == GIMPLE_PHI)
    7496                 :             :     {
    7497                 :       37558 :       ass = gimple_build_assign (tgt, comp);
    7498                 :       37558 :       gsi_insert_before (&bsi, ass, GSI_SAME_STMT);
    7499                 :             : 
    7500                 :       37558 :       bsi = gsi_for_stmt (use->stmt);
    7501                 :       37558 :       remove_phi_node (&bsi, false);
    7502                 :             :     }
    7503                 :             :   else
    7504                 :             :     {
    7505                 :      257091 :       gimple_assign_set_rhs_from_tree (&bsi, comp);
    7506                 :      257091 :       use->stmt = gsi_stmt (bsi);
    7507                 :             :     }
    7508                 :             : }
    7509                 :             : 
    7510                 :             : /* Performs a peephole optimization to reorder the iv update statement with
    7511                 :             :    a mem ref to enable instruction combining in later phases. The mem ref uses
    7512                 :             :    the iv value before the update, so the reordering transformation requires
    7513                 :             :    adjustment of the offset. CAND is the selected IV_CAND.
    7514                 :             : 
    7515                 :             :    Example:
    7516                 :             : 
    7517                 :             :    t = MEM_REF (base, iv1, 8, 16);  // base, index, stride, offset
    7518                 :             :    iv2 = iv1 + 1;
    7519                 :             : 
    7520                 :             :    if (t < val)      (1)
    7521                 :             :      goto L;
    7522                 :             :    goto Head;
    7523                 :             : 
    7524                 :             : 
    7525                 :             :    directly propagating t over to (1) will introduce overlapping live range
    7526                 :             :    thus increase register pressure. This peephole transform it into:
    7527                 :             : 
    7528                 :             : 
    7529                 :             :    iv2 = iv1 + 1;
    7530                 :             :    t = MEM_REF (base, iv2, 8, 8);
    7531                 :             :    if (t < val)
    7532                 :             :      goto L;
    7533                 :             :    goto Head;
    7534                 :             : */
    7535                 :             : 
    7536                 :             : static void
    7537                 :      756228 : adjust_iv_update_pos (struct iv_cand *cand, struct iv_use *use)
    7538                 :             : {
    7539                 :      756228 :   tree var_after;
    7540                 :      756228 :   gimple *iv_update, *stmt;
    7541                 :      756228 :   basic_block bb;
    7542                 :      756228 :   gimple_stmt_iterator gsi, gsi_iv;
    7543                 :             : 
    7544                 :      756228 :   if (cand->pos != IP_NORMAL)
    7545                 :      754333 :     return;
    7546                 :             : 
    7547                 :      567015 :   var_after = cand->var_after;
    7548                 :      567015 :   iv_update = SSA_NAME_DEF_STMT (var_after);
    7549                 :             : 
    7550                 :      567015 :   bb = gimple_bb (iv_update);
    7551                 :      567015 :   gsi = gsi_last_nondebug_bb (bb);
    7552                 :      567015 :   stmt = gsi_stmt (gsi);
    7553                 :             : 
    7554                 :             :   /* Only handle conditional statement for now.  */
    7555                 :      567015 :   if (gimple_code (stmt) != GIMPLE_COND)
    7556                 :             :     return;
    7557                 :             : 
    7558                 :      567015 :   gsi_prev_nondebug (&gsi);
    7559                 :      567015 :   stmt = gsi_stmt (gsi);
    7560                 :      567015 :   if (stmt != iv_update)
    7561                 :             :     return;
    7562                 :             : 
    7563                 :      480707 :   gsi_prev_nondebug (&gsi);
    7564                 :      480707 :   if (gsi_end_p (gsi))
    7565                 :             :     return;
    7566                 :             : 
    7567                 :      477838 :   stmt = gsi_stmt (gsi);
    7568                 :      477838 :   if (gimple_code (stmt) != GIMPLE_ASSIGN)
    7569                 :             :     return;
    7570                 :             : 
    7571                 :      477702 :   if (stmt != use->stmt)
    7572                 :             :     return;
    7573                 :             : 
    7574                 :        4747 :   if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
    7575                 :             :     return;
    7576                 :             : 
    7577                 :        1895 :   if (dump_file && (dump_flags & TDF_DETAILS))
    7578                 :             :     {
    7579                 :           0 :       fprintf (dump_file, "Reordering \n");
    7580                 :           0 :       print_gimple_stmt (dump_file, iv_update, 0);
    7581                 :           0 :       print_gimple_stmt (dump_file, use->stmt, 0);
    7582                 :           0 :       fprintf (dump_file, "\n");
    7583                 :             :     }
    7584                 :             : 
    7585                 :        1895 :   gsi = gsi_for_stmt (use->stmt);
    7586                 :        1895 :   gsi_iv = gsi_for_stmt (iv_update);
    7587                 :        1895 :   gsi_move_before (&gsi_iv, &gsi);
    7588                 :             : 
    7589                 :        1895 :   cand->pos = IP_BEFORE_USE;
    7590                 :        1895 :   cand->incremented_at = use->stmt;
    7591                 :             : }
    7592                 :             : 
    7593                 :             : /* Return the alias pointer type that should be used for a MEM_REF
    7594                 :             :    associated with USE, which has type USE_PTR_ADDRESS.  */
    7595                 :             : 
    7596                 :             : static tree
    7597                 :         394 : get_alias_ptr_type_for_ptr_address (iv_use *use)
    7598                 :             : {
    7599                 :         394 :   gcall *call = as_a <gcall *> (use->stmt);
    7600                 :         394 :   switch (gimple_call_internal_fn (call))
    7601                 :             :     {
    7602                 :         394 :     case IFN_MASK_LOAD:
    7603                 :         394 :     case IFN_MASK_STORE:
    7604                 :         394 :     case IFN_MASK_LOAD_LANES:
    7605                 :         394 :     case IFN_MASK_STORE_LANES:
    7606                 :         394 :     case IFN_MASK_LEN_LOAD_LANES:
    7607                 :         394 :     case IFN_MASK_LEN_STORE_LANES:
    7608                 :         394 :     case IFN_LEN_LOAD:
    7609                 :         394 :     case IFN_LEN_STORE:
    7610                 :         394 :     case IFN_MASK_LEN_LOAD:
    7611                 :         394 :     case IFN_MASK_LEN_STORE:
    7612                 :             :       /* The second argument contains the correct alias type.  */
    7613                 :         394 :       gcc_assert (use->op_p = gimple_call_arg_ptr (call, 0));
    7614                 :         394 :       return TREE_TYPE (gimple_call_arg (call, 1));
    7615                 :             : 
    7616                 :           0 :     default:
    7617                 :           0 :       gcc_unreachable ();
    7618                 :             :     }
    7619                 :             : }
    7620                 :             : 
    7621                 :             : 
    7622                 :             : /* Rewrites USE (address that is an iv) using candidate CAND.  */
    7623                 :             : 
    7624                 :             : static void
    7625                 :      756228 : rewrite_use_address (struct ivopts_data *data,
    7626                 :             :                      struct iv_use *use, struct iv_cand *cand)
    7627                 :             : {
    7628                 :      756228 :   aff_tree aff;
    7629                 :      756228 :   bool ok;
    7630                 :             : 
    7631                 :      756228 :   adjust_iv_update_pos (cand, use);
    7632                 :      756228 :   ok = get_computation_aff (data->current_loop, use->stmt, use, cand, &aff);
    7633                 :      756228 :   gcc_assert (ok);
    7634                 :      756228 :   unshare_aff_combination (&aff);
    7635                 :             : 
    7636                 :             :   /* To avoid undefined overflow problems, all IV candidates use unsigned
    7637                 :             :      integer types.  The drawback is that this makes it impossible for
    7638                 :             :      create_mem_ref to distinguish an IV that is based on a memory object
    7639                 :             :      from one that represents simply an offset.
    7640                 :             : 
    7641                 :             :      To work around this problem, we pass a hint to create_mem_ref that
    7642                 :             :      indicates which variable (if any) in aff is an IV based on a memory
    7643                 :             :      object.  Note that we only consider the candidate.  If this is not
    7644                 :             :      based on an object, the base of the reference is in some subexpression
    7645                 :             :      of the use -- but these will use pointer types, so they are recognized
    7646                 :             :      by the create_mem_ref heuristics anyway.  */
    7647                 :      756228 :   tree iv = var_at_stmt (data->current_loop, cand, use->stmt);
    7648                 :      756228 :   tree base_hint = (cand->iv->base_object) ? iv : NULL_TREE;
    7649                 :      756228 :   gimple_stmt_iterator bsi = gsi_for_stmt (use->stmt);
    7650                 :      756228 :   tree type = use->mem_type;
    7651                 :      756228 :   tree alias_ptr_type;
    7652                 :      756228 :   if (use->type == USE_PTR_ADDRESS)
    7653                 :         394 :     alias_ptr_type = get_alias_ptr_type_for_ptr_address (use);
    7654                 :             :   else
    7655                 :             :     {
    7656                 :      755834 :       gcc_assert (type == TREE_TYPE (*use->op_p));
    7657                 :      755834 :       unsigned int align = get_object_alignment (*use->op_p);
    7658                 :      755834 :       if (align != TYPE_ALIGN (type))
    7659                 :       33109 :         type = build_aligned_type (type, align);
    7660                 :      755834 :       alias_ptr_type = reference_alias_ptr_type (*use->op_p);
    7661                 :             :     }
    7662                 :     1512456 :   tree ref = create_mem_ref (&bsi, type, &aff, alias_ptr_type,
    7663                 :      756228 :                              iv, base_hint, data->speed);
    7664                 :             : 
    7665                 :      756228 :   if (use->type == USE_PTR_ADDRESS)
    7666                 :             :     {
    7667                 :         394 :       ref = fold_build1 (ADDR_EXPR, build_pointer_type (use->mem_type), ref);
    7668                 :         394 :       ref = fold_convert (get_use_type (use), ref);
    7669                 :         394 :       ref = force_gimple_operand_gsi (&bsi, ref, true, NULL_TREE,
    7670                 :             :                                       true, GSI_SAME_STMT);
    7671                 :             :     }
    7672                 :             :   else
    7673                 :             :     {
    7674                 :             :       /* When we end up confused enough and have no suitable base but
    7675                 :             :          stuffed everything to index2 use a LEA for the address and
    7676                 :             :          create a plain MEM_REF to avoid basing a memory reference
    7677                 :             :          on address zero which create_mem_ref_raw does as fallback.  */
    7678                 :      755834 :       if (TREE_CODE (ref) == TARGET_MEM_REF
    7679                 :      755834 :           && TMR_INDEX2 (ref) != NULL_TREE
    7680                 :      763226 :           && integer_zerop (TREE_OPERAND (ref, 0)))
    7681                 :             :         {
    7682                 :          19 :           ref = fold_build1 (ADDR_EXPR, TREE_TYPE (TREE_OPERAND (ref, 0)), ref);
    7683                 :          19 :           ref = force_gimple_operand_gsi (&bsi, ref, true, NULL_TREE,
    7684                 :             :                                           true, GSI_SAME_STMT);
    7685                 :          19 :           ref = build2 (MEM_REF, type, ref, build_zero_cst (alias_ptr_type));
    7686                 :             :         }
    7687                 :      755834 :       copy_ref_info (ref, *use->op_p);
    7688                 :             :     }
    7689                 :             : 
    7690                 :      756228 :   *use->op_p = ref;
    7691                 :      756228 : }
    7692                 :             : 
    7693                 :             : /* Rewrites USE (the condition such that one of the arguments is an iv) using
    7694                 :             :    candidate CAND.  */
    7695                 :             : 
    7696                 :             : static void
    7697                 :      511347 : rewrite_use_compare (struct ivopts_data *data,
    7698                 :             :                      struct iv_use *use, struct iv_cand *cand)
    7699                 :             : {
    7700                 :      511347 :   tree comp, op, bound;
    7701                 :      511347 :   gimple_stmt_iterator bsi = gsi_for_stmt (use->stmt);
    7702                 :      511347 :   enum tree_code compare;
    7703                 :      511347 :   struct iv_group *group = data->vgroups[use->group_id];
    7704                 :      511347 :   class cost_pair *cp = get_group_iv_cost (data, group, cand);
    7705                 :             : 
    7706                 :      511347 :   bound = cp->value;
    7707                 :      511347 :   if (bound)
    7708                 :             :     {
    7709                 :      337283 :       tree var = var_at_stmt (data->current_loop, cand, use->stmt);
    7710                 :      337283 :       tree var_type = TREE_TYPE (var);
    7711                 :      337283 :       gimple_seq stmts;
    7712                 :             : 
    7713                 :      337283 :       if (dump_file && (dump_flags & TDF_DETAILS))
    7714                 :             :         {
    7715                 :          67 :           fprintf (dump_file, "Replacing exit test: ");
    7716                 :          67 :           print_gimple_stmt (dump_file, use->stmt, 0, TDF_SLIM);
    7717                 :             :         }
    7718                 :      337283 :       compare = cp->comp;
    7719                 :      337283 :       bound = unshare_expr (fold_convert (var_type, bound));
    7720                 :      337283 :       op = force_gimple_operand (bound, &stmts, true, NULL_TREE);
    7721                 :      337283 :       if (stmts)
    7722                 :      151343 :         gsi_insert_seq_on_edge_immediate (
    7723                 :      151343 :                 loop_preheader_edge (data->current_loop),
    7724                 :             :                 stmts);
    7725                 :             : 
    7726                 :      337283 :       gcond *cond_stmt = as_a <gcond *> (use->stmt);
    7727                 :      337283 :       gimple_cond_set_lhs (cond_stmt, var);
    7728                 :      337283 :       gimple_cond_set_code (cond_stmt, compare);
    7729                 :      337283 :       gimple_cond_set_rhs (cond_stmt, op);
    7730                 :      337283 :       return;
    7731                 :             :     }
    7732                 :             : 
    7733                 :             :   /* The induction variable elimination failed; just express the original
    7734                 :             :      giv.  */
    7735                 :      174064 :   comp = get_computation_at (data->current_loop, use->stmt, use, cand);
    7736                 :      174064 :   gcc_assert (comp != NULL_TREE);
    7737                 :      174064 :   gcc_assert (use->op_p != NULL);
    7738                 :      174064 :   *use->op_p = force_gimple_operand_gsi (&bsi, comp, true,
    7739                 :      174064 :                                          SSA_NAME_VAR (*use->op_p),
    7740                 :             :                                          true, GSI_SAME_STMT);
    7741                 :             : }
    7742                 :             : 
    7743                 :             : /* Rewrite the groups using the selected induction variables.  */
    7744                 :             : 
    7745                 :             : static void
    7746                 :      427508 : rewrite_groups (struct ivopts_data *data)
    7747                 :             : {
    7748                 :      427508 :   unsigned i, j;
    7749                 :             : 
    7750                 :     3911186 :   for (i = 0; i < data->vgroups.length (); i++)
    7751                 :             :     {
    7752                 :     1528085 :       struct iv_group *group = data->vgroups[i];
    7753                 :     1528085 :       struct iv_cand *cand = group->selected;
    7754                 :             : 
    7755                 :     1528085 :       gcc_assert (cand);
    7756                 :             : 
    7757                 :     1528085 :       if (group->type == USE_NONLINEAR_EXPR)
    7758                 :             :         {
    7759                 :     2087724 :           for (j = 0; j < group->vuses.length (); j++)
    7760                 :             :             {
    7761                 :      521931 :               rewrite_use_nonlinear_expr (data, group->vuses[j], cand);
    7762                 :      521931 :               update_stmt (group->vuses[j]->stmt);
    7763                 :             :             }
    7764                 :             :         }
    7765                 :     1006154 :       else if (address_p (group->type))
    7766                 :             :         {
    7767                 :     2502070 :           for (j = 0; j < group->vuses.length (); j++)
    7768                 :             :             {
    7769                 :      756228 :               rewrite_use_address (data, group->vuses[j], cand);
    7770                 :      756228 :               update_stmt (group->vuses[j]->stmt);
    7771                 :             :             }
    7772                 :             :         }
    7773                 :             :       else
    7774                 :             :         {
    7775                 :      511347 :           gcc_assert (group->type == USE_COMPARE);
    7776                 :             : 
    7777                 :     2045388 :           for (j = 0; j < group->vuses.length (); j++)
    7778                 :             :             {
    7779                 :      511347 :               rewrite_use_compare (data, group->vuses[j], cand);
    7780                 :      511347 :               update_stmt (group->vuses[j]->stmt);
    7781                 :             :             }
    7782                 :             :         }
    7783                 :             :     }
    7784                 :      427508 : }
    7785                 :             : 
    7786                 :             : /* Removes the ivs that are not used after rewriting.  */
    7787                 :             : 
    7788                 :             : static void
    7789                 :      427508 : remove_unused_ivs (struct ivopts_data *data, bitmap toremove)
    7790                 :             : {
    7791                 :      427508 :   unsigned j;
    7792                 :      427508 :   bitmap_iterator bi;
    7793                 :             : 
    7794                 :             :   /* Figure out an order in which to release SSA DEFs so that we don't
    7795                 :             :      release something that we'd have to propagate into a debug stmt
    7796                 :             :      afterwards.  */
    7797                 :     4774259 :   EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi)
    7798                 :             :     {
    7799                 :     4346751 :       struct version_info *info;
    7800                 :             : 
    7801                 :     4346751 :       info = ver_info (data, j);
    7802                 :     4346751 :       if (info->iv
    7803                 :     4226018 :           && !integer_zerop (info->iv->step)
    7804                 :     2783865 :           && !info->inv_id
    7805                 :     2783865 :           && !info->iv->nonlin_use
    7806                 :     6608685 :           && !info->preserve_biv)
    7807                 :             :         {
    7808                 :     2159380 :           bitmap_set_bit (toremove, SSA_NAME_VERSION (info->iv->ssa_name));
    7809                 :             : 
    7810                 :     2159380 :           tree def = info->iv->ssa_name;
    7811                 :             : 
    7812                 :     2803278 :           if (MAY_HAVE_DEBUG_BIND_STMTS && SSA_NAME_DEF_STMT (def))
    7813                 :             :             {
    7814                 :      643898 :               imm_use_iterator imm_iter;
    7815                 :      643898 :               use_operand_p use_p;
    7816                 :      643898 :               gimple *stmt;
    7817                 :      643898 :               int count = 0;
    7818                 :             : 
    7819                 :     1253336 :               FOR_EACH_IMM_USE_STMT (stmt, imm_iter, def)
    7820                 :             :                 {
    7821                 :      627882 :                   if (!gimple_debug_bind_p (stmt))
    7822                 :      542477 :                     continue;
    7823                 :             : 
    7824                 :             :                   /* We just want to determine whether to do nothing
    7825                 :             :                      (count == 0), to substitute the computed
    7826                 :             :                      expression into a single use of the SSA DEF by
    7827                 :             :                      itself (count == 1), or to use a debug temp
    7828                 :             :                      because the SSA DEF is used multiple times or as
    7829                 :             :                      part of a larger expression (count > 1). */
    7830                 :       85405 :                   count++;
    7831                 :       85405 :                   if (gimple_debug_bind_get_value (stmt) != def)
    7832                 :        2905 :                     count++;
    7833                 :             : 
    7834                 :       85405 :                   if (count > 1)
    7835                 :             :                     break;
    7836                 :      643898 :                 }
    7837                 :             : 
    7838                 :      643898 :               if (!count)
    7839                 :      590554 :                 continue;
    7840                 :             : 
    7841                 :       69760 :               struct iv_use dummy_use;
    7842                 :       69760 :               struct iv_cand *best_cand = NULL, *cand;
    7843                 :       69760 :               unsigned i, best_pref = 0, cand_pref;
    7844                 :       69760 :               tree comp = NULL_TREE;
    7845                 :             : 
    7846                 :       69760 :               memset (&dummy_use, 0, sizeof (dummy_use));
    7847                 :       69760 :               dummy_use.iv = info->iv;
    7848                 :      371581 :               for (i = 0; i < data->vgroups.length () && i < 64; i++)
    7849                 :             :                 {
    7850                 :      301821 :                   cand = data->vgroups[i]->selected;
    7851                 :      301821 :                   if (cand == best_cand)
    7852                 :      118538 :                     continue;
    7853                 :      366566 :                   cand_pref = operand_equal_p (cand->iv->step,
    7854                 :      183283 :                                                info->iv->step, 0)
    7855                 :      183283 :                     ? 4 : 0;
    7856                 :      183283 :                   cand_pref
    7857                 :      183283 :                     += TYPE_MODE (TREE_TYPE (cand->iv->base))
    7858                 :      183283 :                     == TYPE_MODE (TREE_TYPE (info->iv->base))
    7859                 :      183283 :                     ? 2 : 0;
    7860                 :      183283 :                   cand_pref
    7861                 :      366566 :                     += TREE_CODE (cand->iv->base) == INTEGER_CST
    7862                 :      183283 :                     ? 1 : 0;
    7863                 :      183283 :                   if (best_cand == NULL || best_pref < cand_pref)
    7864                 :             :                     {
    7865                 :      140408 :                       tree this_comp
    7866                 :      280816 :                         = get_debug_computation_at (data->current_loop,
    7867                 :      140408 :                                                     SSA_NAME_DEF_STMT (def),
    7868                 :             :                                                     &dummy_use, cand);
    7869                 :      140408 :                       if (this_comp)
    7870                 :             :                         {
    7871                 :      301821 :                           best_cand = cand;
    7872                 :      301821 :                           best_pref = cand_pref;
    7873                 :      301821 :                           comp = this_comp;
    7874                 :             :                         }
    7875                 :             :                     }
    7876                 :             :                 }
    7877                 :             : 
    7878                 :       69760 :               if (!best_cand)
    7879                 :       16416 :                 continue;
    7880                 :             : 
    7881                 :       53344 :               comp = unshare_expr (comp);
    7882                 :       53344 :               if (count > 1)
    7883                 :             :                 {
    7884                 :       17411 :                   tree vexpr = build_debug_expr_decl (TREE_TYPE (comp));
    7885                 :             :                   /* FIXME: Is setting the mode really necessary? */
    7886                 :       17411 :                   if (SSA_NAME_VAR (def))
    7887                 :       13082 :                     SET_DECL_MODE (vexpr, DECL_MODE (SSA_NAME_VAR (def)));
    7888                 :             :                   else
    7889                 :        4329 :                     SET_DECL_MODE (vexpr, TYPE_MODE (TREE_TYPE (vexpr)));
    7890                 :       17411 :                   gdebug *def_temp
    7891                 :       17411 :                     = gimple_build_debug_bind (vexpr, comp, NULL);
    7892                 :       17411 :                   gimple_stmt_iterator gsi;
    7893                 :             : 
    7894                 :       17411 :                   if (gimple_code (SSA_NAME_DEF_STMT (def)) == GIMPLE_PHI)
    7895                 :        8423 :                     gsi = gsi_after_labels (gimple_bb
    7896                 :        8423 :                                             (SSA_NAME_DEF_STMT (def)));
    7897                 :             :                   else
    7898                 :        8988 :                     gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (def));
    7899                 :             : 
    7900                 :       17411 :                   gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
    7901                 :       17411 :                   comp = vexpr;
    7902                 :             :                 }
    7903                 :             : 
    7904                 :      197927 :               FOR_EACH_IMM_USE_STMT (stmt, imm_iter, def)
    7905                 :             :                 {
    7906                 :      144583 :                   if (!gimple_debug_bind_p (stmt))
    7907                 :       56366 :                     continue;
    7908                 :             : 
    7909                 :      264723 :                   FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
    7910                 :       88253 :                     SET_USE (use_p, comp);
    7911                 :             : 
    7912                 :       88217 :                   update_stmt (stmt);
    7913                 :       53344 :                 }
    7914                 :             :             }
    7915                 :             :         }
    7916                 :             :     }
    7917                 :      427508 : }
    7918                 :             : 
    7919                 :             : /* Frees memory occupied by class tree_niter_desc in *VALUE. Callback
    7920                 :             :    for hash_map::traverse.  */
    7921                 :             : 
    7922                 :             : bool
    7923                 :      410715 : free_tree_niter_desc (edge const &, tree_niter_desc *const &value, void *)
    7924                 :             : {
    7925                 :      410715 :   if (value)
    7926                 :             :     {
    7927                 :      378669 :       value->~tree_niter_desc ();
    7928                 :      378669 :       free (value);
    7929                 :             :     }
    7930                 :      410715 :   return true;
    7931                 :             : }
    7932                 :             : 
    7933                 :             : /* Frees data allocated by the optimization of a single loop.  */
    7934                 :             : 
    7935                 :             : static void
    7936                 :      762537 : free_loop_data (struct ivopts_data *data)
    7937                 :             : {
    7938                 :      762537 :   unsigned i, j;
    7939                 :      762537 :   bitmap_iterator bi;
    7940                 :      762537 :   tree obj;
    7941                 :             : 
    7942                 :      762537 :   if (data->niters)
    7943                 :             :     {
    7944                 :      401323 :       data->niters->traverse<void *, free_tree_niter_desc> (NULL);
    7945                 :      802646 :       delete data->niters;
    7946                 :      401323 :       data->niters = NULL;
    7947                 :             :     }
    7948                 :             : 
    7949                 :     5120872 :   EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
    7950                 :             :     {
    7951                 :     4358335 :       struct version_info *info;
    7952                 :             : 
    7953                 :     4358335 :       info = ver_info (data, i);
    7954                 :     4358335 :       info->iv = NULL;
    7955                 :     4358335 :       info->has_nonlin_use = false;
    7956                 :     4358335 :       info->preserve_biv = false;
    7957                 :     4358335 :       info->inv_id = 0;
    7958                 :             :     }
    7959                 :      762537 :   bitmap_clear (data->relevant);
    7960                 :      762537 :   bitmap_clear (data->important_candidates);
    7961                 :             : 
    7962                 :     4585028 :   for (i = 0; i < data->vgroups.length (); i++)
    7963                 :             :     {
    7964                 :     1529977 :       struct iv_group *group = data->vgroups[i];
    7965                 :             : 
    7966                 :     6642756 :       for (j = 0; j < group->vuses.length (); j++)
    7967                 :     1791401 :         free (group->vuses[j]);
    7968                 :     1529977 :       group->vuses.release ();
    7969                 :             : 
    7970                 :     1529977 :       BITMAP_FREE (group->related_cands);
    7971                 :    17079907 :       for (j = 0; j < group->n_map_members; j++)
    7972                 :             :         {
    7973                 :    15549930 :           if (group->cost_map[j].inv_vars)
    7974                 :     3211613 :             BITMAP_FREE (group->cost_map[j].inv_vars);
    7975                 :    15549930 :           if (group->cost_map[j].inv_exprs)
    7976                 :     1687291 :             BITMAP_FREE (group->cost_map[j].inv_exprs);
    7977                 :             :         }
    7978                 :             : 
    7979                 :     1529977 :       free (group->cost_map);
    7980                 :     1529977 :       free (group);
    7981                 :             :     }
    7982                 :      762537 :   data->vgroups.truncate (0);
    7983                 :             : 
    7984                 :     9488502 :   for (i = 0; i < data->vcands.length (); i++)
    7985                 :             :     {
    7986                 :     3981714 :       struct iv_cand *cand = data->vcands[i];
    7987                 :             : 
    7988                 :     3981714 :       if (cand->inv_vars)
    7989                 :       65375 :         BITMAP_FREE (cand->inv_vars);
    7990                 :     3981714 :       if (cand->inv_exprs)
    7991                 :      105066 :         BITMAP_FREE (cand->inv_exprs);
    7992                 :     3981714 :       free (cand);
    7993                 :             :     }
    7994                 :      762537 :   data->vcands.truncate (0);
    7995                 :             : 
    7996                 :     1525074 :   if (data->version_info_size < num_ssa_names)
    7997                 :             :     {
    7998                 :          95 :       data->version_info_size = 2 * num_ssa_names;
    7999                 :          95 :       free (data->version_info);
    8000                 :          95 :       data->version_info = XCNEWVEC (struct version_info, data->version_info_size);
    8001                 :             :     }
    8002                 :             : 
    8003                 :      762537 :   data->max_inv_var_id = 0;
    8004                 :      762537 :   data->max_inv_expr_id = 0;
    8005                 :             : 
    8006                 :      762537 :   FOR_EACH_VEC_ELT (decl_rtl_to_reset, i, obj)
    8007                 :           0 :     SET_DECL_RTL (obj, NULL_RTX);
    8008                 :             : 
    8009                 :      762537 :   decl_rtl_to_reset.truncate (0);
    8010                 :             : 
    8011                 :      762537 :   data->inv_expr_tab->empty ();
    8012                 :             : 
    8013                 :      762537 :   data->iv_common_cand_tab->empty ();
    8014                 :      762537 :   data->iv_common_cands.truncate (0);
    8015                 :      762537 : }
    8016                 :             : 
    8017                 :             : /* Finalizes data structures used by the iv optimization pass.  LOOPS is the
    8018                 :             :    loop tree.  */
    8019                 :             : 
    8020                 :             : static void
    8021                 :      219508 : tree_ssa_iv_optimize_finalize (struct ivopts_data *data)
    8022                 :             : {
    8023                 :      219508 :   free_loop_data (data);
    8024                 :      219508 :   free (data->version_info);
    8025                 :      219508 :   BITMAP_FREE (data->relevant);
    8026                 :      219508 :   BITMAP_FREE (data->important_candidates);
    8027                 :             : 
    8028                 :      219508 :   decl_rtl_to_reset.release ();
    8029                 :      219508 :   data->vgroups.release ();
    8030                 :      219508 :   data->vcands.release ();
    8031                 :      219508 :   delete data->inv_expr_tab;
    8032                 :      219508 :   data->inv_expr_tab = NULL;
    8033                 :      219508 :   free_affine_expand_cache (&data->name_expansion_cache);
    8034                 :      219508 :   if (data->base_object_map)
    8035                 :      148048 :     delete data->base_object_map;
    8036                 :      219508 :   delete data->iv_common_cand_tab;
    8037                 :      219508 :   data->iv_common_cand_tab = NULL;
    8038                 :      219508 :   data->iv_common_cands.release ();
    8039                 :      219508 :   obstack_free (&data->iv_obstack, NULL);
    8040                 :      219508 : }
    8041                 :             : 
    8042                 :             : /* Returns true if the loop body BODY includes any function calls.  */
    8043                 :             : 
    8044                 :             : static bool
    8045                 :      543029 : loop_body_includes_call (basic_block *body, unsigned num_nodes)
    8046                 :             : {
    8047                 :      543029 :   gimple_stmt_iterator gsi;
    8048                 :      543029 :   unsigned i;
    8049                 :             : 
    8050                 :     2423596 :   for (i = 0; i < num_nodes; i++)
    8051                 :    19417996 :     for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
    8052                 :             :       {
    8053                 :    15466463 :         gimple *stmt = gsi_stmt (gsi);
    8054                 :    15466463 :         if (is_gimple_call (stmt)
    8055                 :      258449 :             && !gimple_call_internal_p (stmt)
    8056                 :    15662960 :             && !is_inexpensive_builtin (gimple_call_fndecl (stmt)))
    8057                 :             :           return true;
    8058                 :             :       }
    8059                 :             :   return false;
    8060                 :             : }
    8061                 :             : 
    8062                 :             : /* Determine cost scaling factor for basic blocks in loop.  */
    8063                 :             : #define COST_SCALING_FACTOR_BOUND (20)
    8064                 :             : 
    8065                 :             : static void
    8066                 :      427980 : determine_scaling_factor (struct ivopts_data *data, basic_block *body)
    8067                 :             : {
    8068                 :      427980 :   int lfreq = data->current_loop->header->count.to_frequency (cfun);
    8069                 :      427980 :   if (!data->speed || lfreq <= 0)
    8070                 :             :     return;
    8071                 :             : 
    8072                 :             :   int max_freq = lfreq;
    8073                 :     2516015 :   for (unsigned i = 0; i < data->current_loop->num_nodes; i++)
    8074                 :             :     {
    8075                 :     2169568 :       body[i]->aux = (void *)(intptr_t) 1;
    8076                 :     2169568 :       if (max_freq < body[i]->count.to_frequency (cfun))
    8077                 :       83114 :         max_freq = body[i]->count.to_frequency (cfun);
    8078                 :             :     }
    8079                 :      346447 :   if (max_freq > lfreq)
    8080                 :             :     {
    8081                 :       53689 :       int divisor, factor;
    8082                 :             :       /* Check if scaling factor itself needs to be scaled by the bound.  This
    8083                 :             :          is to avoid overflow when scaling cost according to profile info.  */
    8084                 :       53689 :       if (max_freq / lfreq > COST_SCALING_FACTOR_BOUND)
    8085                 :             :         {
    8086                 :             :           divisor = max_freq;
    8087                 :             :           factor = COST_SCALING_FACTOR_BOUND;
    8088                 :             :         }
    8089                 :             :       else
    8090                 :             :         {
    8091                 :       41640 :           divisor = lfreq;
    8092                 :       41640 :           factor = 1;
    8093                 :             :         }
    8094                 :      857317 :       for (unsigned i = 0; i < data->current_loop->num_nodes; i++)
    8095                 :             :         {
    8096                 :      803628 :           int bfreq = body[i]->count.to_frequency (cfun);
    8097                 :      803628 :           if (bfreq <= lfreq)
    8098                 :      440869 :             continue;
    8099                 :             : 
    8100                 :      362759 :           body[i]->aux = (void*)(intptr_t) (factor * bfreq / divisor);
    8101                 :             :         }
    8102                 :             :     }
    8103                 :             : }
    8104                 :             : 
    8105                 :             : /* Find doloop comparison use and set its doloop_p on if found.  */
    8106                 :             : 
    8107                 :             : static bool
    8108                 :           0 : find_doloop_use (struct ivopts_data *data)
    8109                 :             : {
    8110                 :           0 :   struct loop *loop = data->current_loop;
    8111                 :             : 
    8112                 :           0 :   for (unsigned i = 0; i < data->vgroups.length (); i++)
    8113                 :             :     {
    8114                 :           0 :       struct iv_group *group = data->vgroups[i];
    8115                 :           0 :       if (group->type == USE_COMPARE)
    8116                 :             :         {
    8117                 :           0 :           gcc_assert (group->vuses.length () == 1);
    8118                 :           0 :           struct iv_use *use = group->vuses[0];
    8119                 :           0 :           gimple *stmt = use->stmt;
    8120                 :           0 :           if (gimple_code (stmt) == GIMPLE_COND)
    8121                 :             :             {
    8122                 :           0 :               basic_block bb = gimple_bb (stmt);
    8123                 :           0 :               edge true_edge, false_edge;
    8124                 :           0 :               extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
    8125                 :             :               /* This comparison is used for loop latch.  Require latch is empty
    8126                 :             :                  for now.  */
    8127                 :           0 :               if ((loop->latch == true_edge->dest
    8128                 :           0 :                    || loop->latch == false_edge->dest)
    8129                 :           0 :                   && empty_block_p (loop->latch))
    8130                 :             :                 {
    8131                 :           0 :                   group->doloop_p = true;
    8132                 :           0 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    8133                 :             :                     {
    8134                 :           0 :                       fprintf (dump_file, "Doloop cmp iv use: ");
    8135                 :           0 :                       print_gimple_stmt (dump_file, stmt, TDF_DETAILS);
    8136                 :             :                     }
    8137                 :           0 :                   return true;
    8138                 :             :                 }
    8139                 :             :             }
    8140                 :             :         }
    8141                 :             :     }
    8142                 :             : 
    8143                 :             :   return false;
    8144                 :             : }
    8145                 :             : 
    8146                 :             : /* For the targets which support doloop, to predict whether later RTL doloop
    8147                 :             :    transformation will perform on this loop, further detect the doloop use and
    8148                 :             :    mark the flag doloop_use_p if predicted.  */
    8149                 :             : 
    8150                 :             : void
    8151                 :      427980 : analyze_and_mark_doloop_use (struct ivopts_data *data)
    8152                 :             : {
    8153                 :      427980 :   data->doloop_use_p = false;
    8154                 :             : 
    8155                 :      427980 :   if (!flag_branch_on_count_reg)
    8156                 :             :     return;
    8157                 :             : 
    8158                 :      427980 :   if (data->current_loop->unroll == USHRT_MAX)
    8159                 :             :     return;
    8160                 :             : 
    8161                 :      427980 :   if (!generic_predict_doloop_p (data))
    8162                 :             :     return;
    8163                 :             : 
    8164                 :           0 :   if (find_doloop_use (data))
    8165                 :             :     {
    8166                 :           0 :       data->doloop_use_p = true;
    8167                 :           0 :       if (dump_file && (dump_flags & TDF_DETAILS))
    8168                 :             :         {
    8169                 :           0 :           struct loop *loop = data->current_loop;
    8170                 :           0 :           fprintf (dump_file,
    8171                 :             :                    "Predict loop %d can perform"
    8172                 :             :                    " doloop optimization later.\n",
    8173                 :             :                    loop->num);
    8174                 :           0 :           flow_loop_dump (loop, dump_file, NULL, 1);
    8175                 :             :         }
    8176                 :             :     }
    8177                 :             : }
    8178                 :             : 
    8179                 :             : /* Optimizes the LOOP.  Returns true if anything changed.  */
    8180                 :             : 
    8181                 :             : static bool
    8182                 :      543029 : tree_ssa_iv_optimize_loop (struct ivopts_data *data, class loop *loop,
    8183                 :             :                            bitmap toremove)
    8184                 :             : {
    8185                 :      543029 :   bool changed = false;
    8186                 :      543029 :   class iv_ca *iv_ca;
    8187                 :      543029 :   edge exit = single_dom_exit (loop);
    8188                 :      543029 :   basic_block *body;
    8189                 :             : 
    8190                 :      543029 :   gcc_assert (!data->niters);
    8191                 :      543029 :   data->current_loop = loop;
    8192                 :      543029 :   data->loop_loc = find_loop_location (loop).get_location_t ();
    8193                 :      543029 :   data->speed = optimize_loop_for_speed_p (loop);
    8194                 :             : 
    8195                 :      543029 :   if (dump_file && (dump_flags & TDF_DETAILS))
    8196                 :             :     {
    8197                 :          76 :       fprintf (dump_file, "Processing loop %d", loop->num);
    8198                 :          76 :       if (data->loop_loc != UNKNOWN_LOCATION)
    8199                 :          74 :         fprintf (dump_file, " at %s:%d", LOCATION_FILE (data->loop_loc),
    8200                 :         148 :                  LOCATION_LINE (data->loop_loc));
    8201                 :          76 :       fprintf (dump_file, "\n");
    8202                 :             : 
    8203                 :          76 :       if (exit)
    8204                 :             :         {
    8205                 :          66 :           fprintf (dump_file, "  single exit %d -> %d, exit condition ",
    8206                 :          66 :                    exit->src->index, exit->dest->index);
    8207                 :         132 :           print_gimple_stmt (dump_file, *gsi_last_bb (exit->src),
    8208                 :             :                              0, TDF_SLIM);
    8209                 :          66 :           fprintf (dump_file, "\n");
    8210                 :             :         }
    8211                 :             : 
    8212                 :          76 :       fprintf (dump_file, "\n");
    8213                 :             :     }
    8214                 :             : 
    8215                 :      543029 :   body = get_loop_body (loop);
    8216                 :      543029 :   data->body_includes_call = loop_body_includes_call (body, loop->num_nodes);
    8217                 :      543029 :   renumber_gimple_stmt_uids_in_blocks (body, loop->num_nodes);
    8218                 :             : 
    8219                 :      543029 :   data->loop_single_exit_p
    8220                 :      543029 :     = exit != NULL && loop_only_exit_p (loop, body, exit);
    8221                 :             : 
    8222                 :             :   /* For each ssa name determines whether it behaves as an induction variable
    8223                 :             :      in some loop.  */
    8224                 :      543029 :   if (!find_induction_variables (data, body))
    8225                 :      115048 :     goto finish;
    8226                 :             : 
    8227                 :             :   /* Finds interesting uses (item 1).  */
    8228                 :      427981 :   find_interesting_uses (data, body);
    8229                 :      855962 :   if (data->vgroups.length () > MAX_CONSIDERED_GROUPS)
    8230                 :           1 :     goto finish;
    8231                 :             : 
    8232                 :             :   /* Determine cost scaling factor for basic blocks in loop.  */
    8233                 :      427980 :   determine_scaling_factor (data, body);
    8234                 :             : 
    8235                 :             :   /* Analyze doloop possibility and mark the doloop use if predicted.  */
    8236                 :      427980 :   analyze_and_mark_doloop_use (data);
    8237                 :             : 
    8238                 :             :   /* Finds candidates for the induction variables (item 2).  */
    8239                 :      427980 :   find_iv_candidates (data);
    8240                 :             : 
    8241                 :             :   /* Calculates the costs (item 3, part 1).  */
    8242                 :      427980 :   determine_iv_costs (data);
    8243                 :      427980 :   determine_group_iv_costs (data);
    8244                 :      427980 :   determine_set_costs (data);
    8245                 :             : 
    8246                 :             :   /* Find the optimal set of induction variables (item 3, part 2).  */
    8247                 :      427980 :   iv_ca = find_optimal_iv_set (data);
    8248                 :             :   /* Cleanup basic block aux field.  */
    8249                 :     2937855 :   for (unsigned i = 0; i < data->current_loop->num_nodes; i++)
    8250                 :     2509875 :     body[i]->aux = NULL;
    8251                 :      427980 :   if (!iv_ca)
    8252                 :         472 :     goto finish;
    8253                 :      427508 :   changed = true;
    8254                 :             : 
    8255                 :             :   /* Create the new induction variables (item 4, part 1).  */
    8256                 :      427508 :   create_new_ivs (data, iv_ca);
    8257                 :      427508 :   iv_ca_free (&iv_ca);
    8258                 :             : 
    8259                 :             :   /* Rewrite the uses (item 4, part 2).  */
    8260                 :      427508 :   rewrite_groups (data);
    8261                 :             : 
    8262                 :             :   /* Remove the ivs that are unused after rewriting.  */
    8263                 :      427508 :   remove_unused_ivs (data, toremove);
    8264                 :             : 
    8265                 :      543029 : finish:
    8266                 :      543029 :   free (body);
    8267                 :      543029 :   free_loop_data (data);
    8268                 :             : 
    8269                 :      543029 :   return changed;
    8270                 :             : }
    8271                 :             : 
    8272                 :             : /* Main entry point.  Optimizes induction variables in loops.  */
    8273                 :             : 
    8274                 :             : void
    8275                 :      219508 : tree_ssa_iv_optimize (void)
    8276                 :             : {
    8277                 :      219508 :   struct ivopts_data data;
    8278                 :      219508 :   auto_bitmap toremove;
    8279                 :             : 
    8280                 :      219508 :   tree_ssa_iv_optimize_init (&data);
    8281                 :      219508 :   mark_ssa_maybe_undefs ();
    8282                 :             : 
    8283                 :             :   /* Optimize the loops starting with the innermost ones.  */
    8284                 :     1201553 :   for (auto loop : loops_list (cfun, LI_FROM_INNERMOST))
    8285                 :             :     {
    8286                 :      543029 :       if (!dbg_cnt (ivopts_loop))
    8287                 :           0 :         continue;
    8288                 :             : 
    8289                 :      543029 :       if (dump_file && (dump_flags & TDF_DETAILS))
    8290                 :          76 :         flow_loop_dump (loop, dump_file, NULL, 1);
    8291                 :             : 
    8292                 :      543029 :       tree_ssa_iv_optimize_loop (&data, loop, toremove);
    8293                 :      219508 :     }
    8294                 :             : 
    8295                 :             :   /* Remove eliminated IV defs.  */
    8296                 :      219508 :   release_defs_bitset (toremove);
    8297                 :             : 
    8298                 :             :   /* We have changed the structure of induction variables; it might happen
    8299                 :             :      that definitions in the scev database refer to some of them that were
    8300                 :             :      eliminated.  */
    8301                 :      219508 :   scev_reset_htab ();
    8302                 :             :   /* Likewise niter and control-IV information.  */
    8303                 :      219508 :   free_numbers_of_iterations_estimates (cfun);
    8304                 :             : 
    8305                 :      219508 :   tree_ssa_iv_optimize_finalize (&data);
    8306                 :      219508 : }
    8307                 :             : 
    8308                 :             : #include "gt-tree-ssa-loop-ivopts.h"
        

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.