LCOV - code coverage report
Current view: top level - gcc - tree-scalar-evolution.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 92.8 % 1506 1397
Test Date: 2026-03-28 14:25:54 Functions: 95.4 % 65 62
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Scalar evolution detector.
       2              :    Copyright (C) 2003-2026 Free Software Foundation, Inc.
       3              :    Contributed by Sebastian Pop <s.pop@laposte.net>
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              : for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : /*
      22              :    Description:
      23              : 
      24              :    This pass analyzes the evolution of scalar variables in loop
      25              :    structures.  The algorithm is based on the SSA representation,
      26              :    and on the loop hierarchy tree.  This algorithm is not based on
      27              :    the notion of versions of a variable, as it was the case for the
      28              :    previous implementations of the scalar evolution algorithm, but
      29              :    it assumes that each defined name is unique.
      30              : 
      31              :    The notation used in this file is called "chains of recurrences",
      32              :    and has been proposed by Eugene Zima, Robert Van Engelen, and
      33              :    others for describing induction variables in programs.  For example
      34              :    "b -> {0, +, 2}_1" means that the scalar variable "b" is equal to 0
      35              :    when entering in the loop_1 and has a step 2 in this loop, in other
      36              :    words "for (b = 0; b < N; b+=2);".  Note that the coefficients of
      37              :    this chain of recurrence (or chrec [shrek]) can contain the name of
      38              :    other variables, in which case they are called parametric chrecs.
      39              :    For example, "b -> {a, +, 2}_1" means that the initial value of "b"
      40              :    is the value of "a".  In most of the cases these parametric chrecs
      41              :    are fully instantiated before their use because symbolic names can
      42              :    hide some difficult cases such as self-references described later
      43              :    (see the Fibonacci example).
      44              : 
      45              :    A short sketch of the algorithm is:
      46              : 
      47              :    Given a scalar variable to be analyzed, follow the SSA edge to
      48              :    its definition:
      49              : 
      50              :    - When the definition is a GIMPLE_ASSIGN: if the right hand side
      51              :    (RHS) of the definition cannot be statically analyzed, the answer
      52              :    of the analyzer is: "don't know".
      53              :    Otherwise, for all the variables that are not yet analyzed in the
      54              :    RHS, try to determine their evolution, and finally try to
      55              :    evaluate the operation of the RHS that gives the evolution
      56              :    function of the analyzed variable.
      57              : 
      58              :    - When the definition is a condition-phi-node: determine the
      59              :    evolution function for all the branches of the phi node, and
      60              :    finally merge these evolutions (see chrec_merge).
      61              : 
      62              :    - When the definition is a loop-phi-node: determine its initial
      63              :    condition, that is the SSA edge defined in an outer loop, and
      64              :    keep it symbolic.  Then determine the SSA edges that are defined
      65              :    in the body of the loop.  Follow the inner edges until ending on
      66              :    another loop-phi-node of the same analyzed loop.  If the reached
      67              :    loop-phi-node is not the starting loop-phi-node, then we keep
      68              :    this definition under a symbolic form.  If the reached
      69              :    loop-phi-node is the same as the starting one, then we compute a
      70              :    symbolic stride on the return path.  The result is then the
      71              :    symbolic chrec {initial_condition, +, symbolic_stride}_loop.
      72              : 
      73              :    Examples:
      74              : 
      75              :    Example 1: Illustration of the basic algorithm.
      76              : 
      77              :    | a = 3
      78              :    | loop_1
      79              :    |   b = phi (a, c)
      80              :    |   c = b + 1
      81              :    |   if (c > 10) exit_loop
      82              :    | endloop
      83              : 
      84              :    Suppose that we want to know the number of iterations of the
      85              :    loop_1.  The exit_loop is controlled by a COND_EXPR (c > 10).  We
      86              :    ask the scalar evolution analyzer two questions: what's the
      87              :    scalar evolution (scev) of "c", and what's the scev of "10".  For
      88              :    "10" the answer is "10" since it is a scalar constant.  For the
      89              :    scalar variable "c", it follows the SSA edge to its definition,
      90              :    "c = b + 1", and then asks again what's the scev of "b".
      91              :    Following the SSA edge, we end on a loop-phi-node "b = phi (a,
      92              :    c)", where the initial condition is "a", and the inner loop edge
      93              :    is "c".  The initial condition is kept under a symbolic form (it
      94              :    may be the case that the copy constant propagation has done its
      95              :    work and we end with the constant "3" as one of the edges of the
      96              :    loop-phi-node).  The update edge is followed to the end of the
      97              :    loop, and until reaching again the starting loop-phi-node: b -> c
      98              :    -> b.  At this point we have drawn a path from "b" to "b" from
      99              :    which we compute the stride in the loop: in this example it is
     100              :    "+1".  The resulting scev for "b" is "b -> {a, +, 1}_1".  Now
     101              :    that the scev for "b" is known, it is possible to compute the
     102              :    scev for "c", that is "c -> {a + 1, +, 1}_1".  In order to
     103              :    determine the number of iterations in the loop_1, we have to
     104              :    instantiate_parameters (loop_1, {a + 1, +, 1}_1), that gives after some
     105              :    more analysis the scev {4, +, 1}_1, or in other words, this is
     106              :    the function "f (x) = x + 4", where x is the iteration count of
     107              :    the loop_1.  Now we have to solve the inequality "x + 4 > 10",
     108              :    and take the smallest iteration number for which the loop is
     109              :    exited: x = 7.  This loop runs from x = 0 to x = 7, and in total
     110              :    there are 8 iterations.  In terms of loop normalization, we have
     111              :    created a variable that is implicitly defined, "x" or just "_1",
     112              :    and all the other analyzed scalars of the loop are defined in
     113              :    function of this variable:
     114              : 
     115              :    a -> 3
     116              :    b -> {3, +, 1}_1
     117              :    c -> {4, +, 1}_1
     118              : 
     119              :    or in terms of a C program:
     120              : 
     121              :    | a = 3
     122              :    | for (x = 0; x <= 7; x++)
     123              :    |   {
     124              :    |     b = x + 3
     125              :    |     c = x + 4
     126              :    |   }
     127              : 
     128              :    Example 2a: Illustration of the algorithm on nested loops.
     129              : 
     130              :    | loop_1
     131              :    |   a = phi (1, b)
     132              :    |   c = a + 2
     133              :    |   loop_2  10 times
     134              :    |     b = phi (c, d)
     135              :    |     d = b + 3
     136              :    |   endloop
     137              :    | endloop
     138              : 
     139              :    For analyzing the scalar evolution of "a", the algorithm follows
     140              :    the SSA edge into the loop's body: "a -> b".  "b" is an inner
     141              :    loop-phi-node, and its analysis as in Example 1, gives:
     142              : 
     143              :    b -> {c, +, 3}_2
     144              :    d -> {c + 3, +, 3}_2
     145              : 
     146              :    Following the SSA edge for the initial condition, we end on "c = a
     147              :    + 2", and then on the starting loop-phi-node "a".  From this point,
     148              :    the loop stride is computed: back on "c = a + 2" we get a "+2" in
     149              :    the loop_1, then on the loop-phi-node "b" we compute the overall
     150              :    effect of the inner loop that is "b = c + 30", and we get a "+30"
     151              :    in the loop_1.  That means that the overall stride in loop_1 is
     152              :    equal to "+32", and the result is:
     153              : 
     154              :    a -> {1, +, 32}_1
     155              :    c -> {3, +, 32}_1
     156              : 
     157              :    Example 2b: Multivariate chains of recurrences.
     158              : 
     159              :    | loop_1
     160              :    |   k = phi (0, k + 1)
     161              :    |   loop_2  4 times
     162              :    |     j = phi (0, j + 1)
     163              :    |     loop_3 4 times
     164              :    |       i = phi (0, i + 1)
     165              :    |       A[j + k] = ...
     166              :    |     endloop
     167              :    |   endloop
     168              :    | endloop
     169              : 
     170              :    Analyzing the access function of array A with
     171              :    instantiate_parameters (loop_1, "j + k"), we obtain the
     172              :    instantiation and the analysis of the scalar variables "j" and "k"
     173              :    in loop_1.  This leads to the scalar evolution {4, +, 1}_1: the end
     174              :    value of loop_2 for "j" is 4, and the evolution of "k" in loop_1 is
     175              :    {0, +, 1}_1.  To obtain the evolution function in loop_3 and
     176              :    instantiate the scalar variables up to loop_1, one has to use:
     177              :    instantiate_scev (block_before_loop (loop_1), loop_3, "j + k").
     178              :    The result of this call is {{0, +, 1}_1, +, 1}_2.
     179              : 
     180              :    Example 3: Higher degree polynomials.
     181              : 
     182              :    | loop_1
     183              :    |   a = phi (2, b)
     184              :    |   c = phi (5, d)
     185              :    |   b = a + 1
     186              :    |   d = c + a
     187              :    | endloop
     188              : 
     189              :    a -> {2, +, 1}_1
     190              :    b -> {3, +, 1}_1
     191              :    c -> {5, +, a}_1
     192              :    d -> {5 + a, +, a}_1
     193              : 
     194              :    instantiate_parameters (loop_1, {5, +, a}_1) -> {5, +, 2, +, 1}_1
     195              :    instantiate_parameters (loop_1, {5 + a, +, a}_1) -> {7, +, 3, +, 1}_1
     196              : 
     197              :    Example 4: Lucas, Fibonacci, or mixers in general.
     198              : 
     199              :    | loop_1
     200              :    |   a = phi (1, b)
     201              :    |   c = phi (3, d)
     202              :    |   b = c
     203              :    |   d = c + a
     204              :    | endloop
     205              : 
     206              :    a -> (1, c)_1
     207              :    c -> {3, +, a}_1
     208              : 
     209              :    The syntax "(1, c)_1" stands for a PEELED_CHREC that has the
     210              :    following semantics: during the first iteration of the loop_1, the
     211              :    variable contains the value 1, and then it contains the value "c".
     212              :    Note that this syntax is close to the syntax of the loop-phi-node:
     213              :    "a -> (1, c)_1" vs. "a = phi (1, c)".
     214              : 
     215              :    The symbolic chrec representation contains all the semantics of the
     216              :    original code.  What is more difficult is to use this information.
     217              : 
     218              :    Example 5: Flip-flops, or exchangers.
     219              : 
     220              :    | loop_1
     221              :    |   a = phi (1, b)
     222              :    |   c = phi (3, d)
     223              :    |   b = c
     224              :    |   d = a
     225              :    | endloop
     226              : 
     227              :    a -> (1, c)_1
     228              :    c -> (3, a)_1
     229              : 
     230              :    Based on these symbolic chrecs, it is possible to refine this
     231              :    information into the more precise PERIODIC_CHRECs:
     232              : 
     233              :    a -> |1, 3|_1
     234              :    c -> |3, 1|_1
     235              : 
     236              :    This transformation is not yet implemented.
     237              : 
     238              :    Further readings:
     239              : 
     240              :    You can find a more detailed description of the algorithm in:
     241              :    http://icps.u-strasbg.fr/~pop/DEA_03_Pop.pdf
     242              :    http://icps.u-strasbg.fr/~pop/DEA_03_Pop.ps.gz.  But note that
     243              :    this is a preliminary report and some of the details of the
     244              :    algorithm have changed.  I'm working on a research report that
     245              :    updates the description of the algorithms to reflect the design
     246              :    choices used in this implementation.
     247              : 
     248              :    A set of slides show a high level overview of the algorithm and run
     249              :    an example through the scalar evolution analyzer:
     250              :    http://cri.ensmp.fr/~pop/gcc/mar04/slides.pdf
     251              : 
     252              :    The slides that I have presented at the GCC Summit'04 are available
     253              :    at: http://cri.ensmp.fr/~pop/gcc/20040604/gccsummit-lno-spop.pdf
     254              : */
     255              : 
     256              : #include "config.h"
     257              : #include "system.h"
     258              : #include "coretypes.h"
     259              : #include "backend.h"
     260              : #include "target.h"
     261              : #include "rtl.h"
     262              : #include "optabs-query.h"
     263              : #include "tree.h"
     264              : #include "gimple.h"
     265              : #include "ssa.h"
     266              : #include "gimple-pretty-print.h"
     267              : #include "fold-const.h"
     268              : #include "gimplify.h"
     269              : #include "gimple-iterator.h"
     270              : #include "gimplify-me.h"
     271              : #include "tree-cfg.h"
     272              : #include "tree-ssa-loop-ivopts.h"
     273              : #include "tree-ssa-loop-manip.h"
     274              : #include "tree-ssa-loop-niter.h"
     275              : #include "tree-ssa-loop.h"
     276              : #include "tree-ssa.h"
     277              : #include "cfgloop.h"
     278              : #include "tree-chrec.h"
     279              : #include "tree-affine.h"
     280              : #include "tree-scalar-evolution.h"
     281              : #include "dumpfile.h"
     282              : #include "tree-ssa-propagate.h"
     283              : #include "gimple-fold.h"
     284              : #include "tree-into-ssa.h"
     285              : #include "builtins.h"
     286              : #include "case-cfn-macros.h"
     287              : #include "tree-eh.h"
     288              : 
     289              : static tree analyze_scalar_evolution_1 (class loop *, tree);
     290              : static tree analyze_scalar_evolution_for_address_of (class loop *loop,
     291              :                                                      tree var);
     292              : 
     293              : /* The cached information about an SSA name with version NAME_VERSION,
     294              :    claiming that below basic block with index INSTANTIATED_BELOW, the
     295              :    value of the SSA name can be expressed as CHREC.  */
     296              : 
     297              : struct GTY((for_user)) scev_info_str {
     298              :   unsigned int name_version;
     299              :   int instantiated_below;
     300              :   tree chrec;
     301              : };
     302              : 
     303              : /* Counters for the scev database.  */
     304              : static unsigned nb_set_scev = 0;
     305              : static unsigned nb_get_scev = 0;
     306              : 
     307              : struct scev_info_hasher : ggc_ptr_hash<scev_info_str>
     308              : {
     309              :   static hashval_t hash (scev_info_str *i);
     310              :   static bool equal (const scev_info_str *a, const scev_info_str *b);
     311              : };
     312              : 
     313              : static GTY (()) hash_table<scev_info_hasher> *scalar_evolution_info;
     314              : 
     315              : 
     316              : /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW.  */
     317              : 
     318              : static inline struct scev_info_str *
     319     52317415 : new_scev_info_str (basic_block instantiated_below, tree var)
     320              : {
     321     52317415 :   struct scev_info_str *res;
     322              : 
     323     52317415 :   res = ggc_alloc<scev_info_str> ();
     324     52317415 :   res->name_version = SSA_NAME_VERSION (var);
     325     52317415 :   res->chrec = chrec_not_analyzed_yet;
     326     52317415 :   res->instantiated_below = instantiated_below->index;
     327              : 
     328     52317415 :   return res;
     329              : }
     330              : 
     331              : /* Computes a hash function for database element ELT.  */
     332              : 
     333              : hashval_t
     334   1067912200 : scev_info_hasher::hash (scev_info_str *elt)
     335              : {
     336   1067912200 :   return elt->name_version ^ elt->instantiated_below;
     337              : }
     338              : 
     339              : /* Compares database elements E1 and E2.  */
     340              : 
     341              : bool
     342   1058463387 : scev_info_hasher::equal (const scev_info_str *elt1, const scev_info_str *elt2)
     343              : {
     344   1058463387 :   return (elt1->name_version == elt2->name_version
     345   1058463387 :           && elt1->instantiated_below == elt2->instantiated_below);
     346              : }
     347              : 
     348              : /* Get the scalar evolution of VAR for INSTANTIATED_BELOW basic block.
     349              :    A first query on VAR returns chrec_not_analyzed_yet.  */
     350              : 
     351              : static tree *
     352    205755772 : find_var_scev_info (basic_block instantiated_below, tree var)
     353              : {
     354    205755772 :   struct scev_info_str *res;
     355    205755772 :   struct scev_info_str tmp;
     356              : 
     357    205755772 :   tmp.name_version = SSA_NAME_VERSION (var);
     358    205755772 :   tmp.instantiated_below = instantiated_below->index;
     359    205755772 :   scev_info_str **slot = scalar_evolution_info->find_slot (&tmp, INSERT);
     360              : 
     361    205755772 :   if (!*slot)
     362     52317415 :     *slot = new_scev_info_str (instantiated_below, var);
     363    205755772 :   res = *slot;
     364              : 
     365    205755772 :   return &res->chrec;
     366              : }
     367              : 
     368              : 
     369              : /* Hashtable helpers for a temporary hash-table used when
     370              :    analyzing a scalar evolution, instantiating a CHREC or
     371              :    resolving mixers.  */
     372              : 
     373              : class instantiate_cache_type
     374              : {
     375              : public:
     376              :   htab_t map;
     377              :   vec<scev_info_str> entries;
     378              : 
     379    129768808 :   instantiate_cache_type () : map (NULL), entries (vNULL) {}
     380              :   ~instantiate_cache_type ();
     381    126578016 :   tree get (unsigned slot) { return entries[slot].chrec; }
     382     98078904 :   void set (unsigned slot, tree chrec) { entries[slot].chrec = chrec; }
     383              : };
     384              : 
     385    129768808 : instantiate_cache_type::~instantiate_cache_type ()
     386              : {
     387    129768808 :   if (map != NULL)
     388              :     {
     389     29076953 :       htab_delete (map);
     390     29076953 :       entries.release ();
     391              :     }
     392    129768808 : }
     393              : 
     394              : /* Cache to avoid infinite recursion when instantiating an SSA name.
     395              :    Live during the outermost analyze_scalar_evolution, instantiate_scev
     396              :    or resolve_mixers call.  */
     397              : static instantiate_cache_type *global_cache;
     398              : 
     399              : 
     400              : /* Return true when PHI is a loop-phi-node.  */
     401              : 
     402              : static bool
     403     25901913 : loop_phi_node_p (gimple *phi)
     404              : {
     405              :   /* The implementation of this function is based on the following
     406              :      property: "all the loop-phi-nodes of a loop are contained in the
     407              :      loop's header basic block".  */
     408              : 
     409            0 :   return loop_containing_stmt (phi)->header == gimple_bb (phi);
     410              : }
     411              : 
     412              : /* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP.
     413              :    In general, in the case of multivariate evolutions we want to get
     414              :    the evolution in different loops.  LOOP specifies the level for
     415              :    which to get the evolution.
     416              : 
     417              :    Example:
     418              : 
     419              :    | for (j = 0; j < 100; j++)
     420              :    |   {
     421              :    |     for (k = 0; k < 100; k++)
     422              :    |       {
     423              :    |         i = k + j;   - Here the value of i is a function of j, k.
     424              :    |       }
     425              :    |      ... = i         - Here the value of i is a function of j.
     426              :    |   }
     427              :    | ... = i              - Here the value of i is a scalar.
     428              : 
     429              :    Example:
     430              : 
     431              :    | i_0 = ...
     432              :    | loop_1 10 times
     433              :    |   i_1 = phi (i_0, i_2)
     434              :    |   i_2 = i_1 + 2
     435              :    | endloop
     436              : 
     437              :    This loop has the same effect as:
     438              :    LOOP_1 has the same effect as:
     439              : 
     440              :    | i_1 = i_0 + 20
     441              : 
     442              :    The overall effect of the loop, "i_0 + 20" in the previous example,
     443              :    is obtained by passing in the parameters: LOOP = 1,
     444              :    EVOLUTION_FN = {i_0, +, 2}_1.
     445              : */
     446              : 
     447              : tree
     448      5733563 : compute_overall_effect_of_inner_loop (class loop *loop, tree evolution_fn)
     449              : {
     450      6627041 :   bool val = false;
     451              : 
     452      6627041 :   if (evolution_fn == chrec_dont_know)
     453              :     return chrec_dont_know;
     454              : 
     455      6507748 :   else if (TREE_CODE (evolution_fn) == POLYNOMIAL_CHREC)
     456              :     {
     457      2475444 :       class loop *inner_loop = get_chrec_loop (evolution_fn);
     458              : 
     459      2475444 :       if (inner_loop == loop
     460      2475444 :           || flow_loop_nested_p (loop, inner_loop))
     461              :         {
     462      2475444 :           tree nb_iter = number_of_latch_executions (inner_loop);
     463              : 
     464      2475444 :           if (nb_iter == chrec_dont_know)
     465              :             return chrec_dont_know;
     466              :           else
     467              :             {
     468       893478 :               tree res;
     469              : 
     470              :               /* evolution_fn is the evolution function in LOOP.  Get
     471              :                  its value in the nb_iter-th iteration.  */
     472       893478 :               res = chrec_apply (inner_loop->num, evolution_fn, nb_iter);
     473              : 
     474       893478 :               if (chrec_contains_symbols_defined_in_loop (res, loop->num))
     475        61568 :                 res = instantiate_parameters (loop, res);
     476              : 
     477              :               /* Continue the computation until ending on a parent of LOOP.  */
     478       893478 :               return compute_overall_effect_of_inner_loop (loop, res);
     479              :             }
     480              :         }
     481              :       else
     482              :         return evolution_fn;
     483              :      }
     484              : 
     485              :   /* If the evolution function is an invariant, there is nothing to do.  */
     486      4032304 :   else if (no_evolution_in_loop_p (evolution_fn, loop->num, &val) && val)
     487              :     return evolution_fn;
     488              : 
     489              :   else
     490      3252007 :     return chrec_dont_know;
     491              : }
     492              : 
     493              : /* Associate CHREC to SCALAR.  */
     494              : 
     495              : static void
     496     49801109 : set_scalar_evolution (basic_block instantiated_below, tree scalar, tree chrec)
     497              : {
     498     49801109 :   tree *scalar_info;
     499              : 
     500     49801109 :   if (TREE_CODE (scalar) != SSA_NAME)
     501              :     return;
     502              : 
     503     49801109 :   scalar_info = find_var_scev_info (instantiated_below, scalar);
     504              : 
     505     49801109 :   if (dump_file)
     506              :     {
     507        84477 :       if (dump_flags & TDF_SCEV)
     508              :         {
     509           11 :           fprintf (dump_file, "(set_scalar_evolution \n");
     510           11 :           fprintf (dump_file, "  instantiated_below = %d \n",
     511              :                    instantiated_below->index);
     512           11 :           fprintf (dump_file, "  (scalar = ");
     513           11 :           print_generic_expr (dump_file, scalar);
     514           11 :           fprintf (dump_file, ")\n  (scalar_evolution = ");
     515           11 :           print_generic_expr (dump_file, chrec);
     516           11 :           fprintf (dump_file, "))\n");
     517              :         }
     518        84477 :       if (dump_flags & TDF_STATS)
     519         7216 :         nb_set_scev++;
     520              :     }
     521              : 
     522     49801109 :   *scalar_info = chrec;
     523              : }
     524              : 
     525              : /* Retrieve the chrec associated to SCALAR instantiated below
     526              :    INSTANTIATED_BELOW block.  */
     527              : 
     528              : static tree
     529    193733911 : get_scalar_evolution (basic_block instantiated_below, tree scalar)
     530              : {
     531    193733911 :   tree res;
     532              : 
     533    193733911 :   if (dump_file)
     534              :     {
     535       691875 :       if (dump_flags & TDF_SCEV)
     536              :         {
     537           36 :           fprintf (dump_file, "(get_scalar_evolution \n");
     538           36 :           fprintf (dump_file, "  (scalar = ");
     539           36 :           print_generic_expr (dump_file, scalar);
     540           36 :           fprintf (dump_file, ")\n");
     541              :         }
     542       691875 :       if (dump_flags & TDF_STATS)
     543        50926 :         nb_get_scev++;
     544              :     }
     545              : 
     546    193733911 :   if (VECTOR_TYPE_P (TREE_TYPE (scalar))
     547    193733911 :       || TREE_CODE (TREE_TYPE (scalar)) == COMPLEX_TYPE)
     548              :     /* For chrec_dont_know we keep the symbolic form.  */
     549              :     res = scalar;
     550              :   else
     551    193431488 :     switch (TREE_CODE (scalar))
     552              :       {
     553    158036420 :       case SSA_NAME:
     554    158036420 :         if (SSA_NAME_IS_DEFAULT_DEF (scalar))
     555              :           res = scalar;
     556              :         else
     557    155954663 :           res = *find_var_scev_info (instantiated_below, scalar);
     558              :         break;
     559              : 
     560              :       case REAL_CST:
     561              :       case FIXED_CST:
     562              :       case INTEGER_CST:
     563              :         res = scalar;
     564              :         break;
     565              : 
     566              :       default:
     567    193733911 :         res = chrec_not_analyzed_yet;
     568              :         break;
     569              :       }
     570              : 
     571    193733911 :   if (dump_file && (dump_flags & TDF_SCEV))
     572              :     {
     573           36 :       fprintf (dump_file, "  (scalar_evolution = ");
     574           36 :       print_generic_expr (dump_file, res);
     575           36 :       fprintf (dump_file, "))\n");
     576              :     }
     577              : 
     578    193733911 :   return res;
     579              : }
     580              : 
     581              : 
     582              : /* Depth first search algorithm.  */
     583              : 
     584              : enum t_bool {
     585              :   t_false,
     586              :   t_true,
     587              :   t_dont_know
     588              : };
     589              : 
     590              : class scev_dfs
     591              : {
     592              : public:
     593     10737717 :   scev_dfs (class loop *loop_, gphi *phi_, tree init_cond_)
     594     10737717 :       : loop (loop_), loop_phi_node (phi_), init_cond (init_cond_) {}
     595              :   t_bool get_ev (tree *, tree);
     596              : 
     597              : private:
     598              :   t_bool follow_ssa_edge_expr (gimple *, tree, tree *, int);
     599              :   t_bool follow_ssa_edge_binary (gimple *at_stmt,
     600              :                                  tree type, tree rhs0, enum tree_code code,
     601              :                                  tree rhs1, tree *evolution_of_loop, int limit);
     602              :   t_bool follow_ssa_edge_in_condition_phi_branch (int i,
     603              :                                                   gphi *condition_phi,
     604              :                                                   tree *evolution_of_branch,
     605              :                                                   tree init_cond, int limit);
     606              :   t_bool follow_ssa_edge_in_condition_phi (gphi *condition_phi,
     607              :                                            tree *evolution_of_loop, int limit);
     608              :   t_bool follow_ssa_edge_inner_loop_phi (gphi *loop_phi_node,
     609              :                                          tree *evolution_of_loop, int limit);
     610              :   tree add_to_evolution (tree chrec_before, enum tree_code code,
     611              :                          tree to_add, gimple *at_stmt);
     612              :   tree add_to_evolution_1 (tree chrec_before, tree to_add, gimple *at_stmt);
     613              : 
     614              :   class loop *loop;
     615              :   gphi *loop_phi_node;
     616              :   tree init_cond;
     617              : };
     618              : 
     619              : t_bool
     620     10737717 : scev_dfs::get_ev (tree *ev_fn, tree arg)
     621              : {
     622     10737717 :   *ev_fn = chrec_dont_know;
     623     10737717 :   return follow_ssa_edge_expr (loop_phi_node, arg, ev_fn, 0);
     624              : }
     625              : 
     626              : /* Helper function for add_to_evolution.  Returns the evolution
     627              :    function for an assignment of the form "a = b + c", where "a" and
     628              :    "b" are on the strongly connected component.  CHREC_BEFORE is the
     629              :    information that we already have collected up to this point.
     630              :    TO_ADD is the evolution of "c".
     631              : 
     632              :    When CHREC_BEFORE has an evolution part in LOOP_NB, add to this
     633              :    evolution the expression TO_ADD, otherwise construct an evolution
     634              :    part for this loop.  */
     635              : 
     636              : tree
     637      8902707 : scev_dfs::add_to_evolution_1 (tree chrec_before, tree to_add, gimple *at_stmt)
     638              : {
     639      8902707 :   tree type, left, right;
     640      8902707 :   unsigned loop_nb = loop->num;
     641      8902707 :   class loop *chloop;
     642              : 
     643      8902707 :   switch (TREE_CODE (chrec_before))
     644              :     {
     645       159321 :     case POLYNOMIAL_CHREC:
     646       159321 :       chloop = get_chrec_loop (chrec_before);
     647       159321 :       if (chloop == loop
     648       159321 :           || flow_loop_nested_p (chloop, loop))
     649              :         {
     650       159321 :           unsigned var;
     651              : 
     652       159321 :           type = chrec_type (chrec_before);
     653              : 
     654              :           /* When there is no evolution part in this loop, build it.  */
     655       159321 :           if (chloop != loop)
     656              :             {
     657            0 :               var = loop_nb;
     658            0 :               left = chrec_before;
     659            0 :               right = SCALAR_FLOAT_TYPE_P (type)
     660            0 :                 ? build_real (type, dconst0)
     661            0 :                 : build_int_cst (type, 0);
     662              :             }
     663              :           else
     664              :             {
     665       159321 :               var = CHREC_VARIABLE (chrec_before);
     666       159321 :               left = CHREC_LEFT (chrec_before);
     667       159321 :               right = CHREC_RIGHT (chrec_before);
     668              :             }
     669              : 
     670       159321 :           to_add = chrec_convert (type, to_add, at_stmt);
     671       159321 :           right = chrec_convert_rhs (type, right, at_stmt);
     672       159321 :           right = chrec_fold_plus (chrec_type (right), right, to_add);
     673              :           /* When we have an evolution in a non-wrapping type and
     674              :              in the process of accumulating CHREC_RIGHT there was
     675              :              overflow this indicates in the association that happened
     676              :              in building the CHREC clearly involved UB.  Avoid this.
     677              :              In building a CHREC we basically turn (a + INCR1) + INCR2
     678              :              into a + (INCR1 + INCR2) which is not always valid.
     679              :              Note this check only catches few invalid cases.  */
     680        80191 :           if ((INTEGRAL_TYPE_P (type) && ! TYPE_OVERFLOW_WRAPS (type))
     681        41615 :               && TREE_CODE (right) == INTEGER_CST
     682       168472 :               && TREE_OVERFLOW (right))
     683            5 :             return chrec_dont_know;
     684       159316 :           return build_polynomial_chrec (var, left, right);
     685              :         }
     686              :       else
     687              :         {
     688            0 :           gcc_assert (flow_loop_nested_p (loop, chloop));
     689              : 
     690              :           /* Search the evolution in LOOP_NB.  */
     691            0 :           left = add_to_evolution_1 (CHREC_LEFT (chrec_before),
     692              :                                      to_add, at_stmt);
     693            0 :           right = CHREC_RIGHT (chrec_before);
     694            0 :           right = chrec_convert_rhs (chrec_type (left), right, at_stmt);
     695            0 :           return build_polynomial_chrec (CHREC_VARIABLE (chrec_before),
     696            0 :                                          left, right);
     697              :         }
     698              : 
     699      8743386 :     default:
     700              :       /* These nodes do not depend on a loop.  */
     701      8743386 :       if (chrec_before == chrec_dont_know)
     702              :         return chrec_dont_know;
     703              : 
     704      8724404 :       left = chrec_before;
     705      8724404 :       right = chrec_convert_rhs (chrec_type (left), to_add, at_stmt);
     706              :       /* When we add the first evolution we need to replace the symbolic
     707              :          evolution we've put in when the DFS reached the loop PHI node
     708              :          with the initial value.  There's only a limited cases of
     709              :          extra operations ontop of that symbol allowed, namely
     710              :          sign-conversions we can look through.  For other cases we leave
     711              :          the symbolic initial condition which causes build_polynomial_chrec
     712              :          to return chrec_dont_know.  See PR42512, PR66375 and PR107176 for
     713              :          cases we mishandled before.  */
     714      8724404 :       STRIP_NOPS (chrec_before);
     715      8724404 :       if (chrec_before == gimple_phi_result (loop_phi_node))
     716      8723683 :         left = fold_convert (TREE_TYPE (left), init_cond);
     717      8724404 :       return build_polynomial_chrec (loop_nb, left, right);
     718              :     }
     719              : }
     720              : 
     721              : /* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension
     722              :    of LOOP_NB.
     723              : 
     724              :    Description (provided for completeness, for those who read code in
     725              :    a plane, and for my poor 62 bytes brain that would have forgotten
     726              :    all this in the next two or three months):
     727              : 
     728              :    The algorithm of translation of programs from the SSA representation
     729              :    into the chrecs syntax is based on a pattern matching.  After having
     730              :    reconstructed the overall tree expression for a loop, there are only
     731              :    two cases that can arise:
     732              : 
     733              :    1. a = loop-phi (init, a + expr)
     734              :    2. a = loop-phi (init, expr)
     735              : 
     736              :    where EXPR is either a scalar constant with respect to the analyzed
     737              :    loop (this is a degree 0 polynomial), or an expression containing
     738              :    other loop-phi definitions (these are higher degree polynomials).
     739              : 
     740              :    Examples:
     741              : 
     742              :    1.
     743              :    | init = ...
     744              :    | loop_1
     745              :    |   a = phi (init, a + 5)
     746              :    | endloop
     747              : 
     748              :    2.
     749              :    | inita = ...
     750              :    | initb = ...
     751              :    | loop_1
     752              :    |   a = phi (inita, 2 * b + 3)
     753              :    |   b = phi (initb, b + 1)
     754              :    | endloop
     755              : 
     756              :    For the first case, the semantics of the SSA representation is:
     757              : 
     758              :    | a (x) = init + \sum_{j = 0}^{x - 1} expr (j)
     759              : 
     760              :    that is, there is a loop index "x" that determines the scalar value
     761              :    of the variable during the loop execution.  During the first
     762              :    iteration, the value is that of the initial condition INIT, while
     763              :    during the subsequent iterations, it is the sum of the initial
     764              :    condition with the sum of all the values of EXPR from the initial
     765              :    iteration to the before last considered iteration.
     766              : 
     767              :    For the second case, the semantics of the SSA program is:
     768              : 
     769              :    | a (x) = init, if x = 0;
     770              :    |         expr (x - 1), otherwise.
     771              : 
     772              :    The second case corresponds to the PEELED_CHREC, whose syntax is
     773              :    close to the syntax of a loop-phi-node:
     774              : 
     775              :    | phi (init, expr)  vs.  (init, expr)_x
     776              : 
     777              :    The proof of the translation algorithm for the first case is a
     778              :    proof by structural induction based on the degree of EXPR.
     779              : 
     780              :    Degree 0:
     781              :    When EXPR is a constant with respect to the analyzed loop, or in
     782              :    other words when EXPR is a polynomial of degree 0, the evolution of
     783              :    the variable A in the loop is an affine function with an initial
     784              :    condition INIT, and a step EXPR.  In order to show this, we start
     785              :    from the semantics of the SSA representation:
     786              : 
     787              :    f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
     788              : 
     789              :    and since "expr (j)" is a constant with respect to "j",
     790              : 
     791              :    f (x) = init + x * expr
     792              : 
     793              :    Finally, based on the semantics of the pure sum chrecs, by
     794              :    identification we get the corresponding chrecs syntax:
     795              : 
     796              :    f (x) = init * \binom{x}{0} + expr * \binom{x}{1}
     797              :    f (x) -> {init, +, expr}_x
     798              : 
     799              :    Higher degree:
     800              :    Suppose that EXPR is a polynomial of degree N with respect to the
     801              :    analyzed loop_x for which we have already determined that it is
     802              :    written under the chrecs syntax:
     803              : 
     804              :    | expr (x)  ->  {b_0, +, b_1, +, ..., +, b_{n-1}} (x)
     805              : 
     806              :    We start from the semantics of the SSA program:
     807              : 
     808              :    | f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
     809              :    |
     810              :    | f (x) = init + \sum_{j = 0}^{x - 1}
     811              :    |                (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1})
     812              :    |
     813              :    | f (x) = init + \sum_{j = 0}^{x - 1}
     814              :    |                \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k})
     815              :    |
     816              :    | f (x) = init + \sum_{k = 0}^{n - 1}
     817              :    |                (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k})
     818              :    |
     819              :    | f (x) = init + \sum_{k = 0}^{n - 1}
     820              :    |                (b_k * \binom{x}{k + 1})
     821              :    |
     822              :    | f (x) = init + b_0 * \binom{x}{1} + ...
     823              :    |              + b_{n-1} * \binom{x}{n}
     824              :    |
     825              :    | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ...
     826              :    |                             + b_{n-1} * \binom{x}{n}
     827              :    |
     828              : 
     829              :    And finally from the definition of the chrecs syntax, we identify:
     830              :    | f (x)  ->  {init, +, b_0, +, ..., +, b_{n-1}}_x
     831              : 
     832              :    This shows the mechanism that stands behind the add_to_evolution
     833              :    function.  An important point is that the use of symbolic
     834              :    parameters avoids the need of an analysis schedule.
     835              : 
     836              :    Example:
     837              : 
     838              :    | inita = ...
     839              :    | initb = ...
     840              :    | loop_1
     841              :    |   a = phi (inita, a + 2 + b)
     842              :    |   b = phi (initb, b + 1)
     843              :    | endloop
     844              : 
     845              :    When analyzing "a", the algorithm keeps "b" symbolically:
     846              : 
     847              :    | a  ->  {inita, +, 2 + b}_1
     848              : 
     849              :    Then, after instantiation, the analyzer ends on the evolution:
     850              : 
     851              :    | a  ->  {inita, +, 2 + initb, +, 1}_1
     852              : 
     853              : */
     854              : 
     855              : tree
     856      8902707 : scev_dfs::add_to_evolution (tree chrec_before, enum tree_code code,
     857              :                             tree to_add, gimple *at_stmt)
     858              : {
     859      8902707 :   tree type = chrec_type (to_add);
     860      8902707 :   tree res = NULL_TREE;
     861              : 
     862      8902707 :   if (to_add == NULL_TREE)
     863              :     return chrec_before;
     864              : 
     865              :   /* TO_ADD is either a scalar, or a parameter.  TO_ADD is not
     866              :      instantiated at this point.  */
     867      8902707 :   if (TREE_CODE (to_add) == POLYNOMIAL_CHREC)
     868              :     /* This should not happen.  */
     869            0 :     return chrec_dont_know;
     870              : 
     871      8902707 :   if (dump_file && (dump_flags & TDF_SCEV))
     872              :     {
     873            1 :       fprintf (dump_file, "(add_to_evolution \n");
     874            1 :       fprintf (dump_file, "  (loop_nb = %d)\n", loop->num);
     875            1 :       fprintf (dump_file, "  (chrec_before = ");
     876            1 :       print_generic_expr (dump_file, chrec_before);
     877            1 :       fprintf (dump_file, ")\n  (to_add = ");
     878            1 :       print_generic_expr (dump_file, to_add);
     879            1 :       fprintf (dump_file, ")\n");
     880              :     }
     881              : 
     882      8902707 :   if (code == MINUS_EXPR)
     883              :     {
     884      1602454 :       if (INTEGRAL_TYPE_P (type)
     885       796594 :           && TYPE_OVERFLOW_UNDEFINED (type)
     886       868563 :           && !expr_not_equal_to (to_add,
     887       868563 :                                  wi::to_wide (TYPE_MIN_VALUE (type))))
     888              :         {
     889        38021 :           tree utype = unsigned_type_for (type);
     890        38021 :           to_add = chrec_convert_rhs (utype, to_add);
     891        38021 :           to_add = chrec_fold_multiply (utype, to_add,
     892              :                                         build_int_cst_type (utype, -1));
     893        38021 :           to_add = chrec_convert_rhs (type, to_add);
     894              :         }
     895              :       else
     896      1526412 :         to_add = chrec_fold_multiply (type, to_add, SCALAR_FLOAT_TYPE_P (type)
     897         4633 :                                       ? build_real (type, dconstm1)
     898      1521779 :                                       : build_int_cst_type (type, -1));
     899              :     }
     900              : 
     901      8902707 :   res = add_to_evolution_1 (chrec_before, to_add, at_stmt);
     902              : 
     903      8902707 :   if (dump_file && (dump_flags & TDF_SCEV))
     904              :     {
     905            1 :       fprintf (dump_file, "  (res = ");
     906            1 :       print_generic_expr (dump_file, res);
     907            1 :       fprintf (dump_file, "))\n");
     908              :     }
     909              : 
     910              :   return res;
     911              : }
     912              : 
     913              : 
     914              : /* Follow the ssa edge into the binary expression RHS0 CODE RHS1.
     915              :    Return true if the strongly connected component has been found.  */
     916              : 
     917              : t_bool
     918      1075240 : scev_dfs::follow_ssa_edge_binary (gimple *at_stmt, tree type, tree rhs0,
     919              :                                   enum tree_code code, tree rhs1,
     920              :                                   tree *evolution_of_loop, int limit)
     921              : {
     922      1075240 :   t_bool res = t_false;
     923      1075240 :   tree evol;
     924              : 
     925      1075240 :   switch (code)
     926              :     {
     927      1069121 :     case POINTER_PLUS_EXPR:
     928      1069121 :     case PLUS_EXPR:
     929      1069121 :       if (TREE_CODE (rhs0) == SSA_NAME)
     930              :         {
     931      1052320 :           if (TREE_CODE (rhs1) == SSA_NAME)
     932              :             {
     933              :               /* Match an assignment under the form:
     934              :                  "a = b + c".  */
     935              : 
     936              :               /* We want only assignments of form "name + name" contribute to
     937              :                  LIMIT, as the other cases do not necessarily contribute to
     938              :                  the complexity of the expression.  */
     939      1052320 :               limit++;
     940              : 
     941      1052320 :               evol = *evolution_of_loop;
     942      1052320 :               res = follow_ssa_edge_expr (at_stmt, rhs0, &evol, limit);
     943      1052320 :               if (res == t_true)
     944       427077 :                 *evolution_of_loop = add_to_evolution
     945       427077 :                     (chrec_convert (type, evol, at_stmt), code, rhs1, at_stmt);
     946       625243 :               else if (res == t_false)
     947              :                 {
     948       607382 :                   res = follow_ssa_edge_expr
     949       607382 :                     (at_stmt, rhs1, evolution_of_loop, limit);
     950       607382 :                   if (res == t_true)
     951       458245 :                     *evolution_of_loop = add_to_evolution
     952       458245 :                         (chrec_convert (type, *evolution_of_loop, at_stmt),
     953              :                          code, rhs0, at_stmt);
     954              :                 }
     955              :             }
     956              : 
     957              :           else
     958            0 :             gcc_unreachable ();  /* Handled in caller.  */
     959              :         }
     960              : 
     961        16801 :       else if (TREE_CODE (rhs1) == SSA_NAME)
     962              :         {
     963              :           /* Match an assignment under the form:
     964              :              "a = ... + c".  */
     965         5675 :           res = follow_ssa_edge_expr (at_stmt, rhs1, evolution_of_loop, limit);
     966         5675 :           if (res == t_true)
     967         5036 :             *evolution_of_loop = add_to_evolution
     968         5036 :                 (chrec_convert (type, *evolution_of_loop, at_stmt),
     969              :                  code, rhs0, at_stmt);
     970              :         }
     971              : 
     972              :       else
     973              :         /* Otherwise, match an assignment under the form:
     974              :            "a = ... + ...".  */
     975              :         /* And there is nothing to do.  */
     976              :         res = t_false;
     977              :       break;
     978              : 
     979         6119 :     case MINUS_EXPR:
     980              :       /* This case is under the form "opnd0 = rhs0 - rhs1".  */
     981         6119 :       if (TREE_CODE (rhs0) == SSA_NAME)
     982            0 :         gcc_unreachable (); /* Handled in caller.  */
     983              :       else
     984              :         /* Otherwise, match an assignment under the form:
     985              :            "a = ... - ...".  */
     986              :         /* And there is nothing to do.  */
     987              :         res = t_false;
     988              :       break;
     989              : 
     990              :     default:
     991              :       res = t_false;
     992              :     }
     993              : 
     994      1075240 :   return res;
     995              : }
     996              : 
     997              : /* Checks whether the I-th argument of a PHI comes from a backedge.  */
     998              : 
     999              : static bool
    1000      8372810 : backedge_phi_arg_p (gphi *phi, int i)
    1001              : {
    1002      8372810 :   const_edge e = gimple_phi_arg_edge (phi, i);
    1003              : 
    1004              :   /* We would in fact like to test EDGE_DFS_BACK here, but we do not care
    1005              :      about updating it anywhere, and this should work as well most of the
    1006              :      time.  */
    1007      8372810 :   if (e->flags & EDGE_IRREDUCIBLE_LOOP)
    1008        47229 :     return true;
    1009              : 
    1010              :   return false;
    1011              : }
    1012              : 
    1013              : /* Helper function for one branch of the condition-phi-node.  Return
    1014              :    true if the strongly connected component has been found following
    1015              :    this path.  */
    1016              : 
    1017              : t_bool
    1018      3416727 : scev_dfs::follow_ssa_edge_in_condition_phi_branch (int i,
    1019              :                                                    gphi *condition_phi,
    1020              :                                                    tree *evolution_of_branch,
    1021              :                                                    tree init_cond, int limit)
    1022              : {
    1023      3416727 :   tree branch = PHI_ARG_DEF (condition_phi, i);
    1024      3416727 :   *evolution_of_branch = chrec_dont_know;
    1025              : 
    1026              :   /* Do not follow back edges (they must belong to an irreducible loop, which
    1027              :      we really do not want to worry about).  */
    1028      3416727 :   if (backedge_phi_arg_p (condition_phi, i))
    1029              :     return t_false;
    1030              : 
    1031      3411110 :   if (TREE_CODE (branch) == SSA_NAME)
    1032              :     {
    1033      3177574 :       *evolution_of_branch = init_cond;
    1034      3177574 :       return follow_ssa_edge_expr (condition_phi, branch,
    1035      3177574 :                                    evolution_of_branch, limit);
    1036              :     }
    1037              : 
    1038              :   /* This case occurs when one of the condition branches sets
    1039              :      the variable to a constant: i.e. a phi-node like
    1040              :      "a_2 = PHI <a_7(5), 2(6)>;".
    1041              : 
    1042              :      FIXME:  This case have to be refined correctly:
    1043              :      in some cases it is possible to say something better than
    1044              :      chrec_dont_know, for example using a wrap-around notation.  */
    1045              :   return t_false;
    1046              : }
    1047              : 
    1048              : /* This function merges the branches of a condition-phi-node in a
    1049              :    loop.  */
    1050              : 
    1051              : t_bool
    1052      2195126 : scev_dfs::follow_ssa_edge_in_condition_phi (gphi *condition_phi,
    1053              :                                             tree *evolution_of_loop, int limit)
    1054              : {
    1055      2195126 :   int i, n;
    1056      2195126 :   tree init = *evolution_of_loop;
    1057      2195126 :   tree evolution_of_branch;
    1058      2195126 :   t_bool res = follow_ssa_edge_in_condition_phi_branch (0, condition_phi,
    1059              :                                                         &evolution_of_branch,
    1060              :                                                         init, limit);
    1061      2195126 :   if (res == t_false || res == t_dont_know)
    1062              :     return res;
    1063              : 
    1064      1260991 :   *evolution_of_loop = evolution_of_branch;
    1065              : 
    1066      1260991 :   n = gimple_phi_num_args (condition_phi);
    1067      1798190 :   for (i = 1; i < n; i++)
    1068              :     {
    1069              :       /* Quickly give up when the evolution of one of the branches is
    1070              :          not known.  */
    1071      1360143 :       if (*evolution_of_loop == chrec_dont_know)
    1072              :         return t_true;
    1073              : 
    1074              :       /* Increase the limit by the PHI argument number to avoid exponential
    1075              :          time and memory complexity.  */
    1076      1221601 :       res = follow_ssa_edge_in_condition_phi_branch (i, condition_phi,
    1077              :                                                      &evolution_of_branch,
    1078              :                                                      init, limit + i);
    1079      1221601 :       if (res == t_false || res == t_dont_know)
    1080              :         return res;
    1081              : 
    1082       537199 :       *evolution_of_loop = chrec_merge (*evolution_of_loop,
    1083              :                                         evolution_of_branch);
    1084              :     }
    1085              : 
    1086              :   return t_true;
    1087              : }
    1088              : 
    1089              : /* Follow an SSA edge in an inner loop.  It computes the overall
    1090              :    effect of the loop, and following the symbolic initial conditions,
    1091              :    it follows the edges in the parent loop.  The inner loop is
    1092              :    considered as a single statement.  */
    1093              : 
    1094              : t_bool
    1095       251444 : scev_dfs::follow_ssa_edge_inner_loop_phi (gphi *loop_phi_node,
    1096              :                                           tree *evolution_of_loop, int limit)
    1097              : {
    1098       251444 :   class loop *loop = loop_containing_stmt (loop_phi_node);
    1099       251444 :   tree ev = analyze_scalar_evolution (loop, PHI_RESULT (loop_phi_node));
    1100              : 
    1101              :   /* Sometimes, the inner loop is too difficult to analyze, and the
    1102              :      result of the analysis is a symbolic parameter.  */
    1103       251444 :   if (ev == PHI_RESULT (loop_phi_node))
    1104              :     {
    1105        97272 :       t_bool res = t_false;
    1106        97272 :       int i, n = gimple_phi_num_args (loop_phi_node);
    1107              : 
    1108       144598 :       for (i = 0; i < n; i++)
    1109              :         {
    1110       136595 :           tree arg = PHI_ARG_DEF (loop_phi_node, i);
    1111       136595 :           basic_block bb;
    1112              : 
    1113              :           /* Follow the edges that exit the inner loop.  */
    1114       136595 :           bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
    1115       136595 :           if (!flow_bb_inside_loop_p (loop, bb))
    1116        97272 :             res = follow_ssa_edge_expr (loop_phi_node,
    1117              :                                         arg, evolution_of_loop, limit);
    1118       136595 :           if (res == t_true)
    1119              :             break;
    1120              :         }
    1121              : 
    1122              :       /* If the path crosses this loop-phi, give up.  */
    1123        97272 :       if (res == t_true)
    1124        89269 :         *evolution_of_loop = chrec_dont_know;
    1125              : 
    1126        97272 :       return res;
    1127              :     }
    1128              : 
    1129              :   /* Otherwise, compute the overall effect of the inner loop.  */
    1130       154172 :   ev = compute_overall_effect_of_inner_loop (loop, ev);
    1131       154172 :   return follow_ssa_edge_expr (loop_phi_node, ev, evolution_of_loop, limit);
    1132              : }
    1133              : 
    1134              : /* Follow the ssa edge into the expression EXPR.
    1135              :    Return true if the strongly connected component has been found.  */
    1136              : 
    1137              : t_bool
    1138     24328252 : scev_dfs::follow_ssa_edge_expr (gimple *at_stmt, tree expr,
    1139              :                                 tree *evolution_of_loop, int limit)
    1140              : {
    1141     24328252 :   gphi *halting_phi = loop_phi_node;
    1142     24328252 :   enum tree_code code;
    1143     24328252 :   tree type, rhs0, rhs1 = NULL_TREE;
    1144              : 
    1145              :   /* The EXPR is one of the following cases:
    1146              :      - an SSA_NAME,
    1147              :      - an INTEGER_CST,
    1148              :      - a PLUS_EXPR,
    1149              :      - a POINTER_PLUS_EXPR,
    1150              :      - a MINUS_EXPR,
    1151              :      - other cases are not yet handled.  */
    1152              : 
    1153              :   /* For SSA_NAME look at the definition statement, handling
    1154              :      PHI nodes and otherwise expand appropriately for the expression
    1155              :      handling below.  */
    1156     24328252 :   if (TREE_CODE (expr) == SSA_NAME)
    1157              :     {
    1158     24161981 :       gimple *def = SSA_NAME_DEF_STMT (expr);
    1159              : 
    1160     24161981 :       if (gimple_nop_p (def))
    1161              :         return t_false;
    1162              : 
    1163              :       /* Give up if the path is longer than the MAX that we allow.  */
    1164     24145999 :       if (limit > param_scev_max_expr_complexity)
    1165              :         {
    1166         6160 :           *evolution_of_loop = chrec_dont_know;
    1167         6160 :           return t_dont_know;
    1168              :         }
    1169              : 
    1170     24139839 :       if (gphi *phi = dyn_cast <gphi *>(def))
    1171              :         {
    1172     24878184 :           if (!loop_phi_node_p (phi))
    1173              :             /* DEF is a condition-phi-node.  Follow the branches, and
    1174              :                record their evolutions.  Finally, merge the collected
    1175              :                information and set the approximation to the main
    1176              :                variable.  */
    1177      2195126 :             return follow_ssa_edge_in_condition_phi (phi, evolution_of_loop,
    1178      2195126 :                                                      limit);
    1179              : 
    1180              :           /* When the analyzed phi is the halting_phi, the
    1181              :              depth-first search is over: we have found a path from
    1182              :              the halting_phi to itself in the loop.  */
    1183     10243966 :           if (phi == halting_phi)
    1184              :             {
    1185      9781151 :               *evolution_of_loop = expr;
    1186      9781151 :               return t_true;
    1187              :             }
    1188              : 
    1189              :           /* Otherwise, the evolution of the HALTING_PHI depends
    1190              :              on the evolution of another loop-phi-node, i.e. the
    1191              :              evolution function is a higher degree polynomial.  */
    1192       462815 :           class loop *def_loop = loop_containing_stmt (def);
    1193       462815 :           if (def_loop == loop)
    1194              :             return t_false;
    1195              : 
    1196              :           /* Inner loop.  */
    1197       272838 :           if (flow_loop_nested_p (loop, def_loop))
    1198       251444 :             return follow_ssa_edge_inner_loop_phi (phi, evolution_of_loop,
    1199       251444 :                                                    limit + 1);
    1200              : 
    1201              :           /* Outer loop.  */
    1202              :           return t_false;
    1203              :         }
    1204              : 
    1205              :       /* At this level of abstraction, the program is just a set
    1206              :          of GIMPLE_ASSIGNs and PHI_NODEs.  In principle there is no
    1207              :          other def to be handled.  */
    1208     11700747 :       if (!is_gimple_assign (def))
    1209              :         return t_false;
    1210              : 
    1211     11591889 :       code = gimple_assign_rhs_code (def);
    1212     11591889 :       switch (get_gimple_rhs_class (code))
    1213              :         {
    1214     10074016 :         case GIMPLE_BINARY_RHS:
    1215     10074016 :           rhs0 = gimple_assign_rhs1 (def);
    1216     10074016 :           rhs1 = gimple_assign_rhs2 (def);
    1217     10074016 :           break;
    1218      1486708 :         case GIMPLE_UNARY_RHS:
    1219      1486708 :         case GIMPLE_SINGLE_RHS:
    1220      1486708 :           rhs0 = gimple_assign_rhs1 (def);
    1221      1486708 :           break;
    1222              :         default:
    1223              :           return t_false;
    1224              :         }
    1225     11560724 :       type = TREE_TYPE (gimple_assign_lhs (def));
    1226     11560724 :       at_stmt = def;
    1227              :     }
    1228              :   else
    1229              :     {
    1230       166271 :       code = TREE_CODE (expr);
    1231       166271 :       type = TREE_TYPE (expr);
    1232              :       /* Via follow_ssa_edge_inner_loop_phi we arrive here with the
    1233              :          GENERIC scalar evolution of the inner loop.  */
    1234       166271 :       switch (code)
    1235              :         {
    1236        10732 :         CASE_CONVERT:
    1237        10732 :           rhs0 = TREE_OPERAND (expr, 0);
    1238        10732 :           break;
    1239        22477 :         case POINTER_PLUS_EXPR:
    1240        22477 :         case PLUS_EXPR:
    1241        22477 :         case MINUS_EXPR:
    1242        22477 :           rhs0 = TREE_OPERAND (expr, 0);
    1243        22477 :           rhs1 = TREE_OPERAND (expr, 1);
    1244        22477 :           STRIP_USELESS_TYPE_CONVERSION (rhs0);
    1245        22477 :           STRIP_USELESS_TYPE_CONVERSION (rhs1);
    1246        22477 :           break;
    1247              :         default:
    1248              :           rhs0 = expr;
    1249              :         }
    1250              :     }
    1251              : 
    1252     11726995 :   switch (code)
    1253              :     {
    1254       359825 :     CASE_CONVERT:
    1255       359825 :       {
    1256              :         /* This assignment is under the form "a_1 = (cast) rhs.  We cannot
    1257              :            validate any precision altering conversion during the SCC
    1258              :            analysis, so don't even try.  */
    1259       359825 :         if (!tree_nop_conversion_p (type, TREE_TYPE (rhs0)))
    1260              :           return t_false;
    1261       245779 :         t_bool res = follow_ssa_edge_expr (at_stmt, rhs0,
    1262              :                                            evolution_of_loop, limit);
    1263       245779 :         if (res == t_true)
    1264        99787 :           *evolution_of_loop = chrec_convert (type, *evolution_of_loop,
    1265              :                                               at_stmt);
    1266              :         return res;
    1267              :       }
    1268              : 
    1269              :     case INTEGER_CST:
    1270              :       /* This assignment is under the form "a_1 = 7".  */
    1271              :       return t_false;
    1272              : 
    1273         2201 :     case ADDR_EXPR:
    1274         2201 :       {
    1275              :         /* Handle &MEM[ptr + CST] which is equivalent to POINTER_PLUS_EXPR.  */
    1276         2201 :         if (TREE_CODE (TREE_OPERAND (rhs0, 0)) != MEM_REF)
    1277              :           return t_false;
    1278            1 :         tree mem = TREE_OPERAND (rhs0, 0);
    1279            1 :         rhs0 = TREE_OPERAND (mem, 0);
    1280            1 :         rhs1 = TREE_OPERAND (mem, 1);
    1281            1 :         code = POINTER_PLUS_EXPR;
    1282              :       }
    1283              :       /* Fallthru.  */
    1284      9325601 :     case POINTER_PLUS_EXPR:
    1285      9325601 :     case PLUS_EXPR:
    1286      9325601 :     case MINUS_EXPR:
    1287              :       /* This case is under the form "rhs0 +- rhs1".  */
    1288      9325601 :       if (TREE_CODE (rhs0) == SSA_NAME
    1289      9302681 :           && (TREE_CODE (rhs1) != SSA_NAME || code == MINUS_EXPR))
    1290              :         {
    1291              :           /* Match an assignment under the form:
    1292              :              "a = b +- ...".  */
    1293      8250361 :           t_bool res = follow_ssa_edge_expr (at_stmt, rhs0,
    1294              :                                              evolution_of_loop, limit);
    1295      8250361 :           if (res == t_true)
    1296      8012349 :             *evolution_of_loop = add_to_evolution
    1297      8012349 :                 (chrec_convert (type, *evolution_of_loop, at_stmt),
    1298              :                  code, rhs1, at_stmt);
    1299      8250361 :           return res;
    1300              :         }
    1301              :       /* Else search for the SCC in both rhs0 and rhs1.  */
    1302      1075240 :       return follow_ssa_edge_binary (at_stmt, type, rhs0, code, rhs1,
    1303      1075240 :                                      evolution_of_loop, limit);
    1304              : 
    1305              :     default:
    1306              :       return t_false;
    1307              :     }
    1308              : }
    1309              : 
    1310              : 
    1311              : /* This section selects the loops that will be good candidates for the
    1312              :    scalar evolution analysis.  For the moment, greedily select all the
    1313              :    loop nests we could analyze.  */
    1314              : 
    1315              : /* For a loop with a single exit edge, return the COND_EXPR that
    1316              :    guards the exit edge.  If the expression is too difficult to
    1317              :    analyze, then give up.  */
    1318              : 
    1319              : gcond *
    1320          236 : get_loop_exit_condition (const class loop *loop)
    1321              : {
    1322          236 :   return get_loop_exit_condition (single_exit (loop));
    1323              : }
    1324              : 
    1325              : /* If the statement just before the EXIT_EDGE contains a condition then
    1326              :    return the condition, otherwise NULL. */
    1327              : 
    1328              : gcond *
    1329      5077974 : get_loop_exit_condition (const_edge exit_edge)
    1330              : {
    1331      5077974 :   gcond *res = NULL;
    1332              : 
    1333      5077974 :   if (dump_file && (dump_flags & TDF_SCEV))
    1334            2 :     fprintf (dump_file, "(get_loop_exit_condition \n  ");
    1335              : 
    1336      5077974 :   if (exit_edge)
    1337     15233922 :     res = safe_dyn_cast <gcond *> (*gsi_last_bb (exit_edge->src));
    1338              : 
    1339      5077974 :   if (dump_file && (dump_flags & TDF_SCEV))
    1340              :     {
    1341            2 :       print_gimple_stmt (dump_file, res, 0);
    1342            2 :       fprintf (dump_file, ")\n");
    1343              :     }
    1344              : 
    1345      5077974 :   return res;
    1346              : }
    1347              : 
    1348              : 
    1349              : /* Simplify PEELED_CHREC represented by (init_cond, arg) in LOOP.
    1350              :    Handle below case and return the corresponding POLYNOMIAL_CHREC:
    1351              : 
    1352              :    # i_17 = PHI <i_13(5), 0(3)>
    1353              :    # _20 = PHI <_5(5), start_4(D)(3)>
    1354              :    ...
    1355              :    i_13 = i_17 + 1;
    1356              :    _5 = start_4(D) + i_13;
    1357              : 
    1358              :    Though variable _20 appears as a PEELED_CHREC in the form of
    1359              :    (start_4, _5)_LOOP, it's a POLYNOMIAL_CHREC like {start_4, 1}_LOOP.
    1360              : 
    1361              :    See PR41488.  */
    1362              : 
    1363              : static tree
    1364      1570935 : simplify_peeled_chrec (class loop *loop, tree arg, tree init_cond)
    1365              : {
    1366      3141870 :   aff_tree aff1, aff2;
    1367      1570935 :   tree ev, left, right, type, step_val;
    1368      1570935 :   hash_map<tree, name_expansion *> *peeled_chrec_map = NULL;
    1369              : 
    1370      1570935 :   ev = instantiate_parameters (loop, analyze_scalar_evolution (loop, arg));
    1371      1570935 :   if (ev == NULL_TREE)
    1372            0 :     return chrec_dont_know;
    1373              : 
    1374              :   /* Support the case where we can derive the original CHREC from the
    1375              :      peeled one if that's a converted other IV.  This can be done
    1376              :      when the original unpeeled converted IV does not overflow and
    1377              :      has the same initial value.  */
    1378      1560837 :   if (CONVERT_EXPR_P (ev)
    1379        10098 :       && TREE_CODE (init_cond) == INTEGER_CST
    1380         3411 :       && TREE_CODE (TREE_OPERAND (ev, 0)) == POLYNOMIAL_CHREC
    1381         3142 :       && (TYPE_PRECISION (TREE_TYPE (ev))
    1382         3142 :           > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (ev, 0))))
    1383      1573989 :       && (!TYPE_UNSIGNED (TREE_TYPE (ev))
    1384         3054 :           || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (ev, 0)))))
    1385              :     {
    1386         3054 :       left = CHREC_LEFT (TREE_OPERAND (ev, 0));
    1387         3054 :       right = CHREC_RIGHT (TREE_OPERAND (ev, 0));
    1388         3054 :       tree left_before = chrec_fold_minus (TREE_TYPE (TREE_OPERAND (ev, 0)),
    1389              :                                            left, right);
    1390         3054 :       if (TREE_CODE (left_before) == INTEGER_CST
    1391         3054 :           && wi::to_widest (init_cond) == wi::to_widest (left_before)
    1392         6108 :           && !scev_probably_wraps_p (NULL_TREE, left_before, right, NULL,
    1393              :                                      loop, false))
    1394          157 :         return build_polynomial_chrec (loop->num, init_cond,
    1395          157 :                                        chrec_convert (TREE_TYPE (ev),
    1396              :                                                       right, NULL,
    1397          157 :                                                       false, NULL_TREE));
    1398         2897 :       return chrec_dont_know;
    1399              :     }
    1400              : 
    1401      1567881 :   if (TREE_CODE (ev) != POLYNOMIAL_CHREC)
    1402      1528117 :     return chrec_dont_know;
    1403              : 
    1404        39764 :   left = CHREC_LEFT (ev);
    1405        39764 :   right = CHREC_RIGHT (ev);
    1406        39764 :   type = TREE_TYPE (left);
    1407        39764 :   step_val = chrec_fold_plus (type, init_cond, right);
    1408              : 
    1409              :   /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
    1410              :      if "left" equals to "init + right".  */
    1411        39764 :   if (operand_equal_p (left, step_val, 0))
    1412              :     {
    1413        17773 :       if (dump_file && (dump_flags & TDF_SCEV))
    1414            1 :         fprintf (dump_file, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
    1415              : 
    1416        17773 :       return build_polynomial_chrec (loop->num, init_cond, right);
    1417              :     }
    1418              : 
    1419              :   /* The affine code only deals with pointer and integer types.  */
    1420        21991 :   if (!POINTER_TYPE_P (type)
    1421        16462 :       && !INTEGRAL_TYPE_P (type))
    1422           15 :     return chrec_dont_know;
    1423              : 
    1424              :   /* Try harder to check if they are equal.  */
    1425        21976 :   tree_to_aff_combination_expand (left, type, &aff1, &peeled_chrec_map);
    1426        21976 :   tree_to_aff_combination_expand (step_val, type, &aff2, &peeled_chrec_map);
    1427        21976 :   free_affine_expand_cache (&peeled_chrec_map);
    1428        21976 :   aff_combination_scale (&aff2, -1);
    1429        21976 :   aff_combination_add (&aff1, &aff2);
    1430              : 
    1431              :   /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
    1432              :      if "left" equals to "init + right".  */
    1433        21976 :   if (aff_combination_zero_p (&aff1))
    1434              :     {
    1435        14550 :       if (dump_file && (dump_flags & TDF_SCEV))
    1436            1 :         fprintf (dump_file, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
    1437              : 
    1438        14550 :       return build_polynomial_chrec (loop->num, init_cond, right);
    1439              :     }
    1440         7426 :   return chrec_dont_know;
    1441      1570935 : }
    1442              : 
    1443              : /* Given a LOOP_PHI_NODE, this function determines the evolution
    1444              :    function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop.  */
    1445              : 
    1446              : static tree
    1447     10849553 : analyze_evolution_in_loop (gphi *loop_phi_node,
    1448              :                            tree init_cond)
    1449              : {
    1450     10849553 :   int i, n = gimple_phi_num_args (loop_phi_node);
    1451     10849553 :   tree evolution_function = chrec_not_analyzed_yet;
    1452     10849553 :   class loop *loop = loop_containing_stmt (loop_phi_node);
    1453     10849553 :   basic_block bb;
    1454     10849553 :   static bool simplify_peeled_chrec_p = true;
    1455              : 
    1456     10849553 :   if (dump_file && (dump_flags & TDF_SCEV))
    1457              :     {
    1458            3 :       fprintf (dump_file, "(analyze_evolution_in_loop \n");
    1459            3 :       fprintf (dump_file, "  (loop_phi_node = ");
    1460            3 :       print_gimple_stmt (dump_file, loop_phi_node, 0);
    1461            3 :       fprintf (dump_file, ")\n");
    1462              :     }
    1463              : 
    1464     28659980 :   for (i = 0; i < n; i++)
    1465              :     {
    1466     20411104 :       tree arg = PHI_ARG_DEF (loop_phi_node, i);
    1467     20411104 :       tree ev_fn = chrec_dont_know;
    1468     20411104 :       t_bool res;
    1469              : 
    1470              :       /* Select the edges that enter the loop body.  */
    1471     20411104 :       bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
    1472     20411104 :       if (!flow_bb_inside_loop_p (loop, bb))
    1473      9561551 :         continue;
    1474              : 
    1475     10849553 :       if (TREE_CODE (arg) == SSA_NAME)
    1476              :         {
    1477     10737717 :           bool val = false;
    1478              : 
    1479              :           /* Pass in the initial condition to the follow edge function.  */
    1480     10737717 :           scev_dfs dfs (loop, loop_phi_node, init_cond);
    1481     10737717 :           res = dfs.get_ev (&ev_fn, arg);
    1482              : 
    1483              :           /* If ev_fn has no evolution in the inner loop, and the
    1484              :              init_cond is not equal to ev_fn, then we have an
    1485              :              ambiguity between two possible values, as we cannot know
    1486              :              the number of iterations at this point.  */
    1487     10737717 :           if (TREE_CODE (ev_fn) != POLYNOMIAL_CHREC
    1488      2473411 :               && no_evolution_in_loop_p (ev_fn, loop->num, &val) && val
    1489     10737723 :               && !operand_equal_p (init_cond, ev_fn, 0))
    1490            0 :             ev_fn = chrec_dont_know;
    1491              :         }
    1492              :       else
    1493              :         res = t_false;
    1494              : 
    1495              :       /* When it is impossible to go back on the same
    1496              :          loop_phi_node by following the ssa edges, the
    1497              :          evolution is represented by a peeled chrec, i.e. the
    1498              :          first iteration, EV_FN has the value INIT_COND, then
    1499              :          all the other iterations it has the value of ARG.
    1500              :          For the moment, PEELED_CHREC nodes are not built.  */
    1501     10737717 :       if (res != t_true)
    1502              :         {
    1503      2290003 :           ev_fn = chrec_dont_know;
    1504              :           /* Try to recognize POLYNOMIAL_CHREC which appears in
    1505              :              the form of PEELED_CHREC, but guard the process with
    1506              :              a bool variable to keep the analyzer from infinite
    1507              :              recurrence for real PEELED_RECs.  */
    1508      2290003 :           if (simplify_peeled_chrec_p && TREE_CODE (arg) == SSA_NAME)
    1509              :             {
    1510      1570935 :               simplify_peeled_chrec_p = false;
    1511      1570935 :               ev_fn = simplify_peeled_chrec (loop, arg, init_cond);
    1512      1570935 :               simplify_peeled_chrec_p = true;
    1513              :             }
    1514              :         }
    1515              : 
    1516              :       /* When there are multiple back edges of the loop (which in fact never
    1517              :          happens currently, but nevertheless), merge their evolutions.  */
    1518     10849553 :       evolution_function = chrec_merge (evolution_function, ev_fn);
    1519              : 
    1520     10849553 :       if (evolution_function == chrec_dont_know)
    1521              :         break;
    1522              :     }
    1523              : 
    1524     10849553 :   if (dump_file && (dump_flags & TDF_SCEV))
    1525              :     {
    1526            3 :       fprintf (dump_file, "  (evolution_function = ");
    1527            3 :       print_generic_expr (dump_file, evolution_function);
    1528            3 :       fprintf (dump_file, "))\n");
    1529              :     }
    1530              : 
    1531     10849553 :   return evolution_function;
    1532              : }
    1533              : 
    1534              : /* Looks to see if VAR is a copy of a constant (via straightforward assignments
    1535              :    or degenerate phi's).  If so, returns the constant; else, returns VAR.  */
    1536              : 
    1537              : static tree
    1538     22449906 : follow_copies_to_constant (tree var)
    1539              : {
    1540     22449906 :   tree res = var;
    1541     22449906 :   while (TREE_CODE (res) == SSA_NAME
    1542              :          /* We face not updated SSA form in multiple places and this walk
    1543              :             may end up in sibling loops so we have to guard it.  */
    1544     27159258 :          && !name_registered_for_update_p (res))
    1545              :     {
    1546     16023178 :       gimple *def = SSA_NAME_DEF_STMT (res);
    1547     16023178 :       if (gphi *phi = dyn_cast <gphi *> (def))
    1548              :         {
    1549      4149443 :           if (tree rhs = degenerate_phi_result (phi))
    1550              :             res = rhs;
    1551              :           else
    1552              :             break;
    1553              :         }
    1554     11873735 :       else if (gimple_assign_single_p (def))
    1555              :         /* Will exit loop if not an SSA_NAME.  */
    1556      4383647 :         res = gimple_assign_rhs1 (def);
    1557              :       else
    1558              :         break;
    1559              :     }
    1560     22449906 :   if (CONSTANT_CLASS_P (res))
    1561      6602811 :     return res;
    1562              :   return var;
    1563              : }
    1564              : 
    1565              : /* Given a loop-phi-node, return the initial conditions of the
    1566              :    variable on entry of the loop.  When the CCP has propagated
    1567              :    constants into the loop-phi-node, the initial condition is
    1568              :    instantiated, otherwise the initial condition is kept symbolic.
    1569              :    This analyzer does not analyze the evolution outside the current
    1570              :    loop, and leaves this task to the on-demand tree reconstructor.  */
    1571              : 
    1572              : static tree
    1573     10849553 : analyze_initial_condition (gphi *loop_phi_node)
    1574              : {
    1575     10849553 :   int i, n;
    1576     10849553 :   tree init_cond = chrec_not_analyzed_yet;
    1577     10849553 :   class loop *loop = loop_containing_stmt (loop_phi_node);
    1578              : 
    1579     10849553 :   if (dump_file && (dump_flags & TDF_SCEV))
    1580              :     {
    1581            3 :       fprintf (dump_file, "(analyze_initial_condition \n");
    1582            3 :       fprintf (dump_file, "  (loop_phi_node = \n");
    1583            3 :       print_gimple_stmt (dump_file, loop_phi_node, 0);
    1584            3 :       fprintf (dump_file, ")\n");
    1585              :     }
    1586              : 
    1587     10849553 :   n = gimple_phi_num_args (loop_phi_node);
    1588     32548659 :   for (i = 0; i < n; i++)
    1589              :     {
    1590     21699106 :       tree branch = PHI_ARG_DEF (loop_phi_node, i);
    1591     21699106 :       basic_block bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
    1592              : 
    1593              :       /* When the branch is oriented to the loop's body, it does
    1594              :          not contribute to the initial condition.  */
    1595     21699106 :       if (flow_bb_inside_loop_p (loop, bb))
    1596     10849553 :         continue;
    1597              : 
    1598     10849553 :       if (init_cond == chrec_not_analyzed_yet)
    1599              :         {
    1600     10849553 :           init_cond = branch;
    1601     10849553 :           continue;
    1602              :         }
    1603              : 
    1604            0 :       if (TREE_CODE (branch) == SSA_NAME)
    1605              :         {
    1606            0 :           init_cond = chrec_dont_know;
    1607            0 :           break;
    1608              :         }
    1609              : 
    1610            0 :       init_cond = chrec_merge (init_cond, branch);
    1611              :     }
    1612              : 
    1613              :   /* Ooops -- a loop without an entry???  */
    1614     10849553 :   if (init_cond == chrec_not_analyzed_yet)
    1615            0 :     init_cond = chrec_dont_know;
    1616              : 
    1617              :   /* We may not have fully constant propagated IL.  Handle degenerate PHIs here
    1618              :      to not miss important early loop unrollings.  */
    1619     10849553 :   init_cond = follow_copies_to_constant (init_cond);
    1620              : 
    1621     10849553 :   if (dump_file && (dump_flags & TDF_SCEV))
    1622              :     {
    1623            3 :       fprintf (dump_file, "  (init_cond = ");
    1624            3 :       print_generic_expr (dump_file, init_cond);
    1625            3 :       fprintf (dump_file, "))\n");
    1626              :     }
    1627              : 
    1628     10849553 :   return init_cond;
    1629              : }
    1630              : 
    1631              : /* Analyze the scalar evolution for LOOP_PHI_NODE.  */
    1632              : 
    1633              : static tree
    1634     10849553 : interpret_loop_phi (class loop *loop, gphi *loop_phi_node)
    1635              : {
    1636     10849553 :   class loop *phi_loop = loop_containing_stmt (loop_phi_node);
    1637     10849553 :   tree init_cond;
    1638              : 
    1639     10849553 :   gcc_assert (phi_loop == loop);
    1640              : 
    1641              :   /* Otherwise really interpret the loop phi.  */
    1642     10849553 :   init_cond = analyze_initial_condition (loop_phi_node);
    1643     10849553 :   return analyze_evolution_in_loop (loop_phi_node, init_cond);
    1644              : }
    1645              : 
    1646              : /* This function merges the branches of a condition-phi-node,
    1647              :    contained in the outermost loop, and whose arguments are already
    1648              :    analyzed.  */
    1649              : 
    1650              : static tree
    1651      2613268 : interpret_condition_phi (class loop *loop, gphi *condition_phi)
    1652              : {
    1653      2613268 :   int i, n = gimple_phi_num_args (condition_phi);
    1654      2613268 :   tree res = chrec_not_analyzed_yet;
    1655              : 
    1656      5493065 :   for (i = 0; i < n; i++)
    1657              :     {
    1658      4956083 :       tree branch_chrec;
    1659              : 
    1660      4956083 :       if (backedge_phi_arg_p (condition_phi, i))
    1661              :         {
    1662        41612 :           res = chrec_dont_know;
    1663        41612 :           break;
    1664              :         }
    1665              : 
    1666      4914471 :       branch_chrec = analyze_scalar_evolution
    1667      4914471 :         (loop, PHI_ARG_DEF (condition_phi, i));
    1668              : 
    1669      4914471 :       res = chrec_merge (res, branch_chrec);
    1670      4914471 :       if (res == chrec_dont_know)
    1671              :         break;
    1672              :     }
    1673              : 
    1674      2613268 :   return res;
    1675              : }
    1676              : 
    1677              : /* Interpret the operation RHS1 OP RHS2.  If we didn't
    1678              :    analyze this node before, follow the definitions until ending
    1679              :    either on an analyzed GIMPLE_ASSIGN, or on a loop-phi-node.  On the
    1680              :    return path, this function propagates evolutions (ala constant copy
    1681              :    propagation).  OPND1 is not a GIMPLE expression because we could
    1682              :    analyze the effect of an inner loop: see interpret_loop_phi.  */
    1683              : 
    1684              : static tree
    1685     44631861 : interpret_rhs_expr (class loop *loop, gimple *at_stmt,
    1686              :                     tree type, tree rhs1, enum tree_code code, tree rhs2)
    1687              : {
    1688     44631861 :   tree res, chrec1, chrec2, ctype;
    1689     44631861 :   gimple *def;
    1690              : 
    1691     44631861 :   if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
    1692              :     {
    1693     10790711 :       if (is_gimple_min_invariant (rhs1))
    1694      2454765 :         return chrec_convert (type, rhs1, at_stmt);
    1695              : 
    1696      8335946 :       if (code == SSA_NAME)
    1697       131539 :         return chrec_convert (type, analyze_scalar_evolution (loop, rhs1),
    1698       131539 :                               at_stmt);
    1699              :     }
    1700              : 
    1701     42045557 :   switch (code)
    1702              :     {
    1703       338192 :     case ADDR_EXPR:
    1704       338192 :       if (TREE_CODE (TREE_OPERAND (rhs1, 0)) == MEM_REF
    1705       338192 :           || handled_component_p (TREE_OPERAND (rhs1, 0)))
    1706              :         {
    1707       337935 :           machine_mode mode;
    1708       337935 :           poly_int64 bitsize, bitpos;
    1709       337935 :           int unsignedp, reversep;
    1710       337935 :           int volatilep = 0;
    1711       337935 :           tree base, offset;
    1712       337935 :           tree chrec3;
    1713       337935 :           tree unitpos;
    1714              : 
    1715       337935 :           base = get_inner_reference (TREE_OPERAND (rhs1, 0),
    1716              :                                       &bitsize, &bitpos, &offset, &mode,
    1717              :                                       &unsignedp, &reversep, &volatilep);
    1718              : 
    1719       337935 :           if (TREE_CODE (base) == MEM_REF)
    1720              :             {
    1721       258717 :               rhs2 = TREE_OPERAND (base, 1);
    1722       258717 :               rhs1 = TREE_OPERAND (base, 0);
    1723              : 
    1724       258717 :               chrec1 = analyze_scalar_evolution (loop, rhs1);
    1725       258717 :               chrec2 = analyze_scalar_evolution (loop, rhs2);
    1726       258717 :               chrec1 = chrec_convert (type, chrec1, at_stmt);
    1727       258717 :               chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt);
    1728       258717 :               chrec1 = instantiate_parameters (loop, chrec1);
    1729       258717 :               chrec2 = instantiate_parameters (loop, chrec2);
    1730       258717 :               res = chrec_fold_plus (type, chrec1, chrec2);
    1731              :             }
    1732              :           else
    1733              :             {
    1734        79218 :               chrec1 = analyze_scalar_evolution_for_address_of (loop, base);
    1735        79218 :               chrec1 = chrec_convert (type, chrec1, at_stmt);
    1736        79218 :               res = chrec1;
    1737              :             }
    1738              : 
    1739       337935 :           if (offset != NULL_TREE)
    1740              :             {
    1741       150086 :               chrec2 = analyze_scalar_evolution (loop, offset);
    1742       150086 :               chrec2 = chrec_convert (TREE_TYPE (offset), chrec2, at_stmt);
    1743       150086 :               chrec2 = instantiate_parameters (loop, chrec2);
    1744       150086 :               res = chrec_fold_plus (type, res, chrec2);
    1745              :             }
    1746              : 
    1747       337935 :           if (maybe_ne (bitpos, 0))
    1748              :             {
    1749       124561 :               unitpos = size_int (exact_div (bitpos, BITS_PER_UNIT));
    1750       124561 :               chrec3 = analyze_scalar_evolution (loop, unitpos);
    1751       124561 :               chrec3 = chrec_convert (TREE_TYPE (unitpos), chrec3, at_stmt);
    1752       124561 :               chrec3 = instantiate_parameters (loop, chrec3);
    1753       124561 :               res = chrec_fold_plus (type, res, chrec3);
    1754              :             }
    1755              :         }
    1756              :       else
    1757          257 :         res = chrec_dont_know;
    1758              :       break;
    1759              : 
    1760      3474222 :     case POINTER_PLUS_EXPR:
    1761      3474222 :       chrec1 = analyze_scalar_evolution (loop, rhs1);
    1762      3474222 :       chrec2 = analyze_scalar_evolution (loop, rhs2);
    1763      3474222 :       chrec1 = chrec_convert (type, chrec1, at_stmt);
    1764      3474222 :       chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt);
    1765      3474222 :       chrec1 = instantiate_parameters (loop, chrec1);
    1766      3474222 :       chrec2 = instantiate_parameters (loop, chrec2);
    1767      3474222 :       res = chrec_fold_plus (type, chrec1, chrec2);
    1768      3474222 :       break;
    1769              : 
    1770       117733 :     case POINTER_DIFF_EXPR:
    1771       117733 :       {
    1772       117733 :         tree utype = unsigned_type_for (type);
    1773       117733 :         chrec1 = analyze_scalar_evolution (loop, rhs1);
    1774       117733 :         chrec2 = analyze_scalar_evolution (loop, rhs2);
    1775       117733 :         chrec1 = chrec_convert (utype, chrec1, at_stmt);
    1776       117733 :         chrec2 = chrec_convert (utype, chrec2, at_stmt);
    1777       117733 :         chrec1 = instantiate_parameters (loop, chrec1);
    1778       117733 :         chrec2 = instantiate_parameters (loop, chrec2);
    1779       117733 :         res = chrec_fold_minus (utype, chrec1, chrec2);
    1780       117733 :         res = chrec_convert (type, res, at_stmt);
    1781       117733 :         break;
    1782              :       }
    1783              : 
    1784     11775874 :     case PLUS_EXPR:
    1785     11775874 :       chrec1 = analyze_scalar_evolution (loop, rhs1);
    1786     11775874 :       chrec2 = analyze_scalar_evolution (loop, rhs2);
    1787     11775874 :       ctype = type;
    1788              :       /* When the stmt is conditionally executed re-write the CHREC
    1789              :          into a form that has well-defined behavior on overflow.  */
    1790     11775874 :       if (at_stmt
    1791     10655599 :           && INTEGRAL_TYPE_P (type)
    1792     10561224 :           && ! TYPE_OVERFLOW_WRAPS (type)
    1793     19836024 :           && ! dominated_by_p (CDI_DOMINATORS, loop->latch,
    1794      8060150 :                                gimple_bb (at_stmt)))
    1795       707121 :         ctype = unsigned_type_for (type);
    1796     11775874 :       chrec1 = chrec_convert (ctype, chrec1, at_stmt);
    1797     11775874 :       chrec2 = chrec_convert (ctype, chrec2, at_stmt);
    1798     11775874 :       chrec1 = instantiate_parameters (loop, chrec1);
    1799     11775874 :       chrec2 = instantiate_parameters (loop, chrec2);
    1800     11775874 :       res = chrec_fold_plus (ctype, chrec1, chrec2);
    1801     11775874 :       if (type != ctype)
    1802       707121 :         res = chrec_convert (type, res, at_stmt);
    1803              :       break;
    1804              : 
    1805      1462646 :     case MINUS_EXPR:
    1806      1462646 :       chrec1 = analyze_scalar_evolution (loop, rhs1);
    1807      1462646 :       chrec2 = analyze_scalar_evolution (loop, rhs2);
    1808      1462646 :       ctype = type;
    1809              :       /* When the stmt is conditionally executed re-write the CHREC
    1810              :          into a form that has well-defined behavior on overflow.  */
    1811      1462646 :       if (at_stmt
    1812      1398781 :           && INTEGRAL_TYPE_P (type)
    1813      1362126 :           && ! TYPE_OVERFLOW_WRAPS (type)
    1814      1996177 :           && ! dominated_by_p (CDI_DOMINATORS,
    1815       533531 :                                loop->latch, gimple_bb (at_stmt)))
    1816       135591 :         ctype = unsigned_type_for (type);
    1817      1462646 :       chrec1 = chrec_convert (ctype, chrec1, at_stmt);
    1818      1462646 :       chrec2 = chrec_convert (ctype, chrec2, at_stmt);
    1819      1462646 :       chrec1 = instantiate_parameters (loop, chrec1);
    1820      1462646 :       chrec2 = instantiate_parameters (loop, chrec2);
    1821      1462646 :       res = chrec_fold_minus (ctype, chrec1, chrec2);
    1822      1462646 :       if (type != ctype)
    1823       135591 :         res = chrec_convert (type, res, at_stmt);
    1824              :       break;
    1825              : 
    1826        82913 :     case NEGATE_EXPR:
    1827        82913 :       chrec1 = analyze_scalar_evolution (loop, rhs1);
    1828        82913 :       ctype = type;
    1829              :       /* When the stmt is conditionally executed re-write the CHREC
    1830              :          into a form that has well-defined behavior on overflow.  */
    1831        82913 :       if (at_stmt
    1832        71506 :           && INTEGRAL_TYPE_P (type)
    1833        69726 :           && ! TYPE_OVERFLOW_WRAPS (type)
    1834       121078 :           && ! dominated_by_p (CDI_DOMINATORS,
    1835        38165 :                                loop->latch, gimple_bb (at_stmt)))
    1836         7229 :         ctype = unsigned_type_for (type);
    1837        82913 :       chrec1 = chrec_convert (ctype, chrec1, at_stmt);
    1838              :       /* TYPE may be integer, real or complex, so use fold_convert.  */
    1839        82913 :       chrec1 = instantiate_parameters (loop, chrec1);
    1840        82913 :       res = chrec_fold_multiply (ctype, chrec1,
    1841              :                                  fold_convert (ctype, integer_minus_one_node));
    1842        82913 :       if (type != ctype)
    1843         7229 :         res = chrec_convert (type, res, at_stmt);
    1844              :       break;
    1845              : 
    1846        35960 :     case BIT_NOT_EXPR:
    1847              :       /* Handle ~X as -1 - X.  */
    1848        35960 :       chrec1 = analyze_scalar_evolution (loop, rhs1);
    1849        35960 :       chrec1 = chrec_convert (type, chrec1, at_stmt);
    1850        35960 :       chrec1 = instantiate_parameters (loop, chrec1);
    1851        35960 :       res = chrec_fold_minus (type,
    1852              :                               fold_convert (type, integer_minus_one_node),
    1853              :                               chrec1);
    1854        35960 :       break;
    1855              : 
    1856      6062245 :     case MULT_EXPR:
    1857      6062245 :       chrec1 = analyze_scalar_evolution (loop, rhs1);
    1858      6062245 :       chrec2 = analyze_scalar_evolution (loop, rhs2);
    1859      6062245 :       ctype = type;
    1860              :       /* When the stmt is conditionally executed re-write the CHREC
    1861              :          into a form that has well-defined behavior on overflow.  */
    1862      6062245 :       if (at_stmt
    1863      4151434 :           && INTEGRAL_TYPE_P (type)
    1864      4040777 :           && ! TYPE_OVERFLOW_WRAPS (type)
    1865      7882153 :           && ! dominated_by_p (CDI_DOMINATORS,
    1866      1819908 :                                loop->latch, gimple_bb (at_stmt)))
    1867       161830 :         ctype = unsigned_type_for (type);
    1868      6062245 :       chrec1 = chrec_convert (ctype, chrec1, at_stmt);
    1869      6062245 :       chrec2 = chrec_convert (ctype, chrec2, at_stmt);
    1870      6062245 :       chrec1 = instantiate_parameters (loop, chrec1);
    1871      6062245 :       chrec2 = instantiate_parameters (loop, chrec2);
    1872      6062245 :       res = chrec_fold_multiply (ctype, chrec1, chrec2);
    1873      6062245 :       if (type != ctype)
    1874       161830 :         res = chrec_convert (type, res, at_stmt);
    1875              :       break;
    1876              : 
    1877       155244 :     case LSHIFT_EXPR:
    1878       155244 :       {
    1879              :         /* Handle A<<B as A * (1<<B).  */
    1880       155244 :         tree uns = unsigned_type_for (type);
    1881       155244 :         chrec1 = analyze_scalar_evolution (loop, rhs1);
    1882       155244 :         chrec2 = analyze_scalar_evolution (loop, rhs2);
    1883       155244 :         chrec1 = chrec_convert (uns, chrec1, at_stmt);
    1884       155244 :         chrec1 = instantiate_parameters (loop, chrec1);
    1885       155244 :         chrec2 = instantiate_parameters (loop, chrec2);
    1886              : 
    1887       155244 :         tree one = build_int_cst (uns, 1);
    1888       155244 :         chrec2 = fold_build2 (LSHIFT_EXPR, uns, one, chrec2);
    1889       155244 :         res = chrec_fold_multiply (uns, chrec1, chrec2);
    1890       155244 :         res = chrec_convert (type, res, at_stmt);
    1891              :       }
    1892       155244 :       break;
    1893              : 
    1894      8459230 :     CASE_CONVERT:
    1895              :       /* In case we have a truncation of a widened operation that in
    1896              :          the truncated type has undefined overflow behavior analyze
    1897              :          the operation done in an unsigned type of the same precision
    1898              :          as the final truncation.  We cannot derive a scalar evolution
    1899              :          for the widened operation but for the truncated result.  */
    1900      8459230 :       if (TREE_CODE (type) == INTEGER_TYPE
    1901      8167076 :           && TREE_CODE (TREE_TYPE (rhs1)) == INTEGER_TYPE
    1902      7639813 :           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (rhs1))
    1903       464519 :           && TYPE_OVERFLOW_UNDEFINED (type)
    1904       269766 :           && TREE_CODE (rhs1) == SSA_NAME
    1905       269598 :           && (def = SSA_NAME_DEF_STMT (rhs1))
    1906       269598 :           && is_gimple_assign (def)
    1907       174917 :           && TREE_CODE_CLASS (gimple_assign_rhs_code (def)) == tcc_binary
    1908      8581825 :           && TREE_CODE (gimple_assign_rhs2 (def)) == INTEGER_CST)
    1909              :         {
    1910        88698 :           tree utype = unsigned_type_for (type);
    1911        88698 :           chrec1 = interpret_rhs_expr (loop, at_stmt, utype,
    1912              :                                        gimple_assign_rhs1 (def),
    1913              :                                        gimple_assign_rhs_code (def),
    1914              :                                        gimple_assign_rhs2 (def));
    1915              :         }
    1916              :       else
    1917      8370532 :         chrec1 = analyze_scalar_evolution (loop, rhs1);
    1918      8459230 :       res = chrec_convert (type, chrec1, at_stmt, true, rhs1);
    1919      8459230 :       break;
    1920              : 
    1921       395575 :     case BIT_AND_EXPR:
    1922              :       /* Given int variable A, handle A&0xffff as (int)(unsigned short)A.
    1923              :          If A is SCEV and its value is in the range of representable set
    1924              :          of type unsigned short, the result expression is a (no-overflow)
    1925              :          SCEV.  */
    1926       395575 :       res = chrec_dont_know;
    1927       395575 :       if (tree_fits_uhwi_p (rhs2))
    1928              :         {
    1929       270437 :           int precision;
    1930       270437 :           unsigned HOST_WIDE_INT val = tree_to_uhwi (rhs2);
    1931              : 
    1932       270437 :           val ++;
    1933              :           /* Skip if value of rhs2 wraps in unsigned HOST_WIDE_INT or
    1934              :              it's not the maximum value of a smaller type than rhs1.  */
    1935       270437 :           if (val != 0
    1936       214209 :               && (precision = exact_log2 (val)) > 0
    1937       484646 :               && (unsigned) precision < TYPE_PRECISION (TREE_TYPE (rhs1)))
    1938              :             {
    1939       214209 :               tree utype = build_nonstandard_integer_type (precision, 1);
    1940              : 
    1941       214209 :               if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (rhs1)))
    1942              :                 {
    1943       214209 :                   chrec1 = analyze_scalar_evolution (loop, rhs1);
    1944       214209 :                   chrec1 = chrec_convert (utype, chrec1, at_stmt);
    1945       214209 :                   res = chrec_convert (TREE_TYPE (rhs1), chrec1, at_stmt);
    1946              :                 }
    1947              :             }
    1948              :         }
    1949              :       break;
    1950              : 
    1951      9685723 :     default:
    1952      9685723 :       res = chrec_dont_know;
    1953      9685723 :       break;
    1954              :     }
    1955              : 
    1956              :   return res;
    1957              : }
    1958              : 
    1959              : /* Interpret the expression EXPR.  */
    1960              : 
    1961              : static tree
    1962      8845538 : interpret_expr (class loop *loop, gimple *at_stmt, tree expr)
    1963              : {
    1964      8845538 :   enum tree_code code;
    1965      8845538 :   tree type = TREE_TYPE (expr), op0, op1;
    1966              : 
    1967      8845538 :   if (automatically_generated_chrec_p (expr))
    1968              :     return expr;
    1969              : 
    1970      8840719 :   if (TREE_CODE (expr) == POLYNOMIAL_CHREC
    1971      8840231 :       || TREE_CODE (expr) == CALL_EXPR
    1972     17680882 :       || get_gimple_rhs_class (TREE_CODE (expr)) == GIMPLE_TERNARY_RHS)
    1973              :     return chrec_dont_know;
    1974              : 
    1975      8770286 :   extract_ops_from_tree (expr, &code, &op0, &op1);
    1976              : 
    1977      8770286 :   return interpret_rhs_expr (loop, at_stmt, type,
    1978      8770286 :                              op0, code, op1);
    1979              : }
    1980              : 
    1981              : /* Interpret the rhs of the assignment STMT.  */
    1982              : 
    1983              : static tree
    1984     35772877 : interpret_gimple_assign (class loop *loop, gimple *stmt)
    1985              : {
    1986     35772877 :   tree type = TREE_TYPE (gimple_assign_lhs (stmt));
    1987     35772877 :   enum tree_code code = gimple_assign_rhs_code (stmt);
    1988              : 
    1989     35772877 :   return interpret_rhs_expr (loop, stmt, type,
    1990              :                              gimple_assign_rhs1 (stmt), code,
    1991     35772877 :                              gimple_assign_rhs2 (stmt));
    1992              : }
    1993              : 
    1994              : 
    1995              : 
    1996              : /* This section contains all the entry points:
    1997              :    - number_of_iterations_in_loop,
    1998              :    - analyze_scalar_evolution,
    1999              :    - instantiate_parameters.
    2000              : */
    2001              : 
    2002              : /* Helper recursive function.  */
    2003              : 
    2004              : static tree
    2005     74171899 : analyze_scalar_evolution_1 (class loop *loop, tree var)
    2006              : {
    2007     74171899 :   gimple *def;
    2008     74171899 :   basic_block bb;
    2009     74171899 :   class loop *def_loop;
    2010     74171899 :   tree res;
    2011              : 
    2012     74171899 :   if (TREE_CODE (var) != SSA_NAME)
    2013      8845538 :     return interpret_expr (loop, NULL, var);
    2014              : 
    2015     65326361 :   def = SSA_NAME_DEF_STMT (var);
    2016     65326361 :   bb = gimple_bb (def);
    2017     65326361 :   def_loop = bb->loop_father;
    2018              : 
    2019     65326361 :   if (!flow_bb_inside_loop_p (loop, bb))
    2020              :     {
    2021              :       /* Keep symbolic form, but look through obvious copies for constants.  */
    2022     11600353 :       res = follow_copies_to_constant (var);
    2023     11600353 :       goto set_and_end;
    2024              :     }
    2025              : 
    2026     53726008 :   if (loop != def_loop)
    2027              :     {
    2028      3924899 :       res = analyze_scalar_evolution_1 (def_loop, var);
    2029      3924899 :       class loop *loop_to_skip = superloop_at_depth (def_loop,
    2030      3924899 :                                                       loop_depth (loop) + 1);
    2031      3924899 :       res = compute_overall_effect_of_inner_loop (loop_to_skip, res);
    2032      3924899 :       if (chrec_contains_symbols_defined_in_loop (res, loop->num))
    2033       294611 :         res = analyze_scalar_evolution_1 (loop, res);
    2034      3924899 :       goto set_and_end;
    2035              :     }
    2036              : 
    2037     49801109 :   switch (gimple_code (def))
    2038              :     {
    2039     35772877 :     case GIMPLE_ASSIGN:
    2040     35772877 :       res = interpret_gimple_assign (loop, def);
    2041     35772877 :       break;
    2042              : 
    2043     13462821 :     case GIMPLE_PHI:
    2044     26925642 :       if (loop_phi_node_p (def))
    2045     10849553 :         res = interpret_loop_phi (loop, as_a <gphi *> (def));
    2046              :       else
    2047      2613268 :         res = interpret_condition_phi (loop, as_a <gphi *> (def));
    2048              :       break;
    2049              : 
    2050       565411 :     default:
    2051       565411 :       res = chrec_dont_know;
    2052       565411 :       break;
    2053              :     }
    2054              : 
    2055     65326361 :  set_and_end:
    2056              : 
    2057              :   /* Keep the symbolic form.  */
    2058     65326361 :   if (res == chrec_dont_know)
    2059     23939963 :     res = var;
    2060              : 
    2061     65326361 :   if (loop == def_loop)
    2062     49801109 :     set_scalar_evolution (block_before_loop (loop), var, res);
    2063              : 
    2064              :   return res;
    2065              : }
    2066              : 
    2067              : /* Analyzes and returns the scalar evolution of the ssa_name VAR in
    2068              :    LOOP.  LOOP is the loop in which the variable is used.
    2069              : 
    2070              :    Example of use: having a pointer VAR to a SSA_NAME node, STMT a
    2071              :    pointer to the statement that uses this variable, in order to
    2072              :    determine the evolution function of the variable, use the following
    2073              :    calls:
    2074              : 
    2075              :    loop_p loop = loop_containing_stmt (stmt);
    2076              :    tree chrec_with_symbols = analyze_scalar_evolution (loop, var);
    2077              :    tree chrec_instantiated = instantiate_parameters (loop, chrec_with_symbols);
    2078              : */
    2079              : 
    2080              : tree
    2081    193892793 : analyze_scalar_evolution (class loop *loop, tree var)
    2082              : {
    2083    193892793 :   tree res;
    2084              : 
    2085              :   /* ???  Fix callers.  */
    2086    193892793 :   if (! loop)
    2087              :     return var;
    2088              : 
    2089    193733911 :   if (dump_file && (dump_flags & TDF_SCEV))
    2090              :     {
    2091           36 :       fprintf (dump_file, "(analyze_scalar_evolution \n");
    2092           36 :       fprintf (dump_file, "  (loop_nb = %d)\n", loop->num);
    2093           36 :       fprintf (dump_file, "  (scalar = ");
    2094           36 :       print_generic_expr (dump_file, var);
    2095           36 :       fprintf (dump_file, ")\n");
    2096              :     }
    2097              : 
    2098    193733911 :   res = get_scalar_evolution (block_before_loop (loop), var);
    2099    193733911 :   if (res == chrec_not_analyzed_yet)
    2100              :     {
    2101              :       /* We'll recurse into instantiate_scev, avoid tearing down the
    2102              :          instantiate cache repeatedly and keep it live from here.  */
    2103     69952389 :       bool destr = false;
    2104     69952389 :       if (!global_cache)
    2105              :         {
    2106     41890729 :           global_cache = new instantiate_cache_type;
    2107     41890729 :           destr = true;
    2108              :         }
    2109     69952389 :       res = analyze_scalar_evolution_1 (loop, var);
    2110     69952389 :       if (destr)
    2111              :         {
    2112     41890729 :           delete global_cache;
    2113     41890729 :           global_cache = NULL;
    2114              :         }
    2115              :     }
    2116              : 
    2117    193733911 :   if (dump_file && (dump_flags & TDF_SCEV))
    2118           36 :     fprintf (dump_file, ")\n");
    2119              : 
    2120              :   return res;
    2121              : }
    2122              : 
    2123              : /* If CHREC doesn't overflow, set the nonwrapping flag.  */
    2124              : 
    2125     10646951 : void record_nonwrapping_chrec (tree chrec)
    2126              : {
    2127     10646951 :   CHREC_NOWRAP(chrec) = 1;
    2128              : 
    2129     10646951 :   if (dump_file && (dump_flags & TDF_SCEV))
    2130              :     {
    2131            6 :       fprintf (dump_file, "(record_nonwrapping_chrec: ");
    2132            6 :       print_generic_expr (dump_file, chrec);
    2133            6 :       fprintf (dump_file, ")\n");
    2134              :     }
    2135     10646951 : }
    2136              : 
    2137              : /* Return true if CHREC's nonwrapping flag is set.  */
    2138              : 
    2139       226296 : bool nonwrapping_chrec_p (tree chrec)
    2140              : {
    2141       226296 :   if (!chrec || TREE_CODE(chrec) != POLYNOMIAL_CHREC)
    2142              :     return false;
    2143              : 
    2144       226296 :   return CHREC_NOWRAP(chrec);
    2145              : }
    2146              : 
    2147              : /* Analyzes and returns the scalar evolution of VAR address in LOOP.  */
    2148              : 
    2149              : static tree
    2150        79218 : analyze_scalar_evolution_for_address_of (class loop *loop, tree var)
    2151              : {
    2152        79218 :   return analyze_scalar_evolution (loop, build_fold_addr_expr (var));
    2153              : }
    2154              : 
    2155              : /* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
    2156              :    WRTO_LOOP (which should be a superloop of USE_LOOP)
    2157              : 
    2158              :    FOLDED_CASTS is set to true if resolve_mixers used
    2159              :    chrec_convert_aggressive (TODO -- not really, we are way too conservative
    2160              :    at the moment in order to keep things simple).
    2161              : 
    2162              :    To illustrate the meaning of USE_LOOP and WRTO_LOOP, consider the following
    2163              :    example:
    2164              : 
    2165              :    for (i = 0; i < 100; i++)                 -- loop 1
    2166              :      {
    2167              :        for (j = 0; j < 100; j++)             -- loop 2
    2168              :          {
    2169              :            k1 = i;
    2170              :            k2 = j;
    2171              : 
    2172              :            use2 (k1, k2);
    2173              : 
    2174              :            for (t = 0; t < 100; t++)         -- loop 3
    2175              :              use3 (k1, k2);
    2176              : 
    2177              :          }
    2178              :        use1 (k1, k2);
    2179              :      }
    2180              : 
    2181              :    Both k1 and k2 are invariants in loop3, thus
    2182              :      analyze_scalar_evolution_in_loop (loop3, loop3, k1) = k1
    2183              :      analyze_scalar_evolution_in_loop (loop3, loop3, k2) = k2
    2184              : 
    2185              :    As they are invariant, it does not matter whether we consider their
    2186              :    usage in loop 3 or loop 2, hence
    2187              :      analyze_scalar_evolution_in_loop (loop2, loop3, k1) =
    2188              :        analyze_scalar_evolution_in_loop (loop2, loop2, k1) = i
    2189              :      analyze_scalar_evolution_in_loop (loop2, loop3, k2) =
    2190              :        analyze_scalar_evolution_in_loop (loop2, loop2, k2) = [0,+,1]_2
    2191              : 
    2192              :    Similarly for their evolutions with respect to loop 1.  The values of K2
    2193              :    in the use in loop 2 vary independently on loop 1, thus we cannot express
    2194              :    the evolution with respect to loop 1:
    2195              :      analyze_scalar_evolution_in_loop (loop1, loop3, k1) =
    2196              :        analyze_scalar_evolution_in_loop (loop1, loop2, k1) = [0,+,1]_1
    2197              :      analyze_scalar_evolution_in_loop (loop1, loop3, k2) =
    2198              :        analyze_scalar_evolution_in_loop (loop1, loop2, k2) = dont_know
    2199              : 
    2200              :    The value of k2 in the use in loop 1 is known, though:
    2201              :      analyze_scalar_evolution_in_loop (loop1, loop1, k1) = [0,+,1]_1
    2202              :      analyze_scalar_evolution_in_loop (loop1, loop1, k2) = 100
    2203              :    */
    2204              : 
    2205              : static tree
    2206     49437485 : analyze_scalar_evolution_in_loop (class loop *wrto_loop, class loop *use_loop,
    2207              :                                   tree version, bool *folded_casts)
    2208              : {
    2209     49437485 :   bool val = false;
    2210     49437485 :   tree ev = version, tmp;
    2211              : 
    2212              :   /* We cannot just do
    2213              : 
    2214              :      tmp = analyze_scalar_evolution (use_loop, version);
    2215              :      ev = resolve_mixers (wrto_loop, tmp, folded_casts);
    2216              : 
    2217              :      as resolve_mixers would query the scalar evolution with respect to
    2218              :      wrto_loop.  For example, in the situation described in the function
    2219              :      comment, suppose that wrto_loop = loop1, use_loop = loop3 and
    2220              :      version = k2.  Then
    2221              : 
    2222              :      analyze_scalar_evolution (use_loop, version) = k2
    2223              : 
    2224              :      and resolve_mixers (loop1, k2, folded_casts) finds that the value of
    2225              :      k2 in loop 1 is 100, which is a wrong result, since we are interested
    2226              :      in the value in loop 3.
    2227              : 
    2228              :      Instead, we need to proceed from use_loop to wrto_loop loop by loop,
    2229              :      each time checking that there is no evolution in the inner loop.  */
    2230              : 
    2231     49437485 :   if (folded_casts)
    2232     49437485 :     *folded_casts = false;
    2233     51739659 :   while (1)
    2234              :     {
    2235     50588572 :       tmp = analyze_scalar_evolution (use_loop, ev);
    2236     50588572 :       ev = resolve_mixers (use_loop, tmp, folded_casts);
    2237              : 
    2238     50588572 :       if (use_loop == wrto_loop)
    2239              :         return ev;
    2240              : 
    2241              :       /* If the value of the use changes in the inner loop, we cannot express
    2242              :          its value in the outer loop (we might try to return interval chrec,
    2243              :          but we do not have a user for it anyway)  */
    2244      4295923 :       if (!no_evolution_in_loop_p (ev, use_loop->num, &val)
    2245      4295923 :           || !val)
    2246      3144836 :         return chrec_dont_know;
    2247              : 
    2248      1151087 :       use_loop = loop_outer (use_loop);
    2249              :     }
    2250              : }
    2251              : 
    2252              : 
    2253              : /* Computes a hash function for database element ELT.  */
    2254              : 
    2255              : static inline hashval_t
    2256       269261 : hash_idx_scev_info (const void *elt_)
    2257              : {
    2258       269261 :   unsigned idx = ((size_t) elt_) - 2;
    2259       269261 :   return scev_info_hasher::hash (&global_cache->entries[idx]);
    2260              : }
    2261              : 
    2262              : /* Compares database elements E1 and E2.  */
    2263              : 
    2264              : static inline int
    2265     31670120 : eq_idx_scev_info (const void *e1, const void *e2)
    2266              : {
    2267     31670120 :   unsigned idx1 = ((size_t) e1) - 2;
    2268     31670120 :   return scev_info_hasher::equal (&global_cache->entries[idx1],
    2269     31670120 :                                   (const scev_info_str *) e2);
    2270              : }
    2271              : 
    2272              : /* Returns from CACHE the slot number of the cached chrec for NAME.  */
    2273              : 
    2274              : static unsigned
    2275     63289008 : get_instantiated_value_entry (instantiate_cache_type &cache,
    2276              :                               tree name, edge instantiate_below)
    2277              : {
    2278     63289008 :   if (!cache.map)
    2279              :     {
    2280     29076953 :       cache.map = htab_create (10, hash_idx_scev_info, eq_idx_scev_info, NULL);
    2281     29076953 :       cache.entries.create (10);
    2282              :     }
    2283              : 
    2284     63289008 :   scev_info_str e;
    2285     63289008 :   e.name_version = SSA_NAME_VERSION (name);
    2286     63289008 :   e.instantiated_below = instantiate_below->dest->index;
    2287     63289008 :   void **slot = htab_find_slot_with_hash (cache.map, &e,
    2288              :                                           scev_info_hasher::hash (&e), INSERT);
    2289     63289008 :   if (!*slot)
    2290              :     {
    2291     32730306 :       e.chrec = chrec_not_analyzed_yet;
    2292     32730306 :       *slot = (void *)(size_t)(cache.entries.length () + 2);
    2293     32730306 :       cache.entries.safe_push (e);
    2294              :     }
    2295              : 
    2296     63289008 :   return ((size_t)*slot) - 2;
    2297              : }
    2298              : 
    2299              : 
    2300              : /* Return the closed_loop_phi node for VAR.  If there is none, return
    2301              :    NULL_TREE.  */
    2302              : 
    2303              : static tree
    2304      1879943 : loop_closed_phi_def (tree var)
    2305              : {
    2306      1879943 :   class loop *loop;
    2307      1879943 :   edge exit;
    2308      1879943 :   gphi *phi;
    2309      1879943 :   gphi_iterator psi;
    2310              : 
    2311      1879943 :   if (var == NULL_TREE
    2312      1879943 :       || TREE_CODE (var) != SSA_NAME)
    2313              :     return NULL_TREE;
    2314              : 
    2315      1879943 :   loop = loop_containing_stmt (SSA_NAME_DEF_STMT (var));
    2316      1879943 :   exit = single_exit (loop);
    2317      1879943 :   if (!exit)
    2318              :     return NULL_TREE;
    2319              : 
    2320      1574094 :   for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); gsi_next (&psi))
    2321              :     {
    2322       964159 :       phi = psi.phi ();
    2323       964159 :       if (PHI_ARG_DEF_FROM_EDGE (phi, exit) == var)
    2324       261278 :         return PHI_RESULT (phi);
    2325              :     }
    2326              : 
    2327              :   return NULL_TREE;
    2328              : }
    2329              : 
    2330              : static tree instantiate_scev_r (edge, class loop *, class loop *,
    2331              :                                 tree, bool *, int);
    2332              : 
    2333              : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
    2334              :    and EVOLUTION_LOOP, that were left under a symbolic form.
    2335              : 
    2336              :    CHREC is an SSA_NAME to be instantiated.
    2337              : 
    2338              :    CACHE is the cache of already instantiated values.
    2339              : 
    2340              :    Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
    2341              :    conversions that may wrap in signed/pointer type are folded, as long
    2342              :    as the value of the chrec is preserved.  If FOLD_CONVERSIONS is NULL
    2343              :    then we don't do such fold.
    2344              : 
    2345              :    SIZE_EXPR is used for computing the size of the expression to be
    2346              :    instantiated, and to stop if it exceeds some limit.  */
    2347              : 
    2348              : static tree
    2349    110799703 : instantiate_scev_name (edge instantiate_below,
    2350              :                        class loop *evolution_loop, class loop *inner_loop,
    2351              :                        tree chrec,
    2352              :                        bool *fold_conversions,
    2353              :                        int size_expr)
    2354              : {
    2355    110799703 :   tree res;
    2356    110799703 :   class loop *def_loop;
    2357    110799703 :   basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (chrec));
    2358              : 
    2359              :   /* A parameter, nothing to do.  */
    2360    110799703 :   if (!def_bb
    2361    110799703 :       || !dominated_by_p (CDI_DOMINATORS, def_bb, instantiate_below->dest))
    2362     47510695 :     return chrec;
    2363              : 
    2364              :   /* We cache the value of instantiated variable to avoid exponential
    2365              :      time complexity due to reevaluations.  We also store the convenient
    2366              :      value in the cache in order to prevent infinite recursion -- we do
    2367              :      not want to instantiate the SSA_NAME if it is in a mixer
    2368              :      structure.  This is used for avoiding the instantiation of
    2369              :      recursively defined functions, such as:
    2370              : 
    2371              :      | a_2 -> {0, +, 1, +, a_2}_1  */
    2372              : 
    2373     63289008 :   unsigned si = get_instantiated_value_entry (*global_cache,
    2374              :                                               chrec, instantiate_below);
    2375     63289008 :   if (global_cache->get (si) != chrec_not_analyzed_yet)
    2376              :     return global_cache->get (si);
    2377              : 
    2378              :   /* On recursion return chrec_dont_know.  */
    2379     32730306 :   global_cache->set (si, chrec_dont_know);
    2380              : 
    2381     32730306 :   def_loop = find_common_loop (evolution_loop, def_bb->loop_father);
    2382              : 
    2383     32730306 :   if (! dominated_by_p (CDI_DOMINATORS,
    2384     32730306 :                         def_loop->header, instantiate_below->dest))
    2385              :     {
    2386       190855 :       gimple *def = SSA_NAME_DEF_STMT (chrec);
    2387       190855 :       if (gassign *ass = dyn_cast <gassign *> (def))
    2388              :         {
    2389       135129 :           switch (gimple_assign_rhs_class (ass))
    2390              :             {
    2391         5581 :             case GIMPLE_UNARY_RHS:
    2392         5581 :               {
    2393         5581 :                 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
    2394              :                                                inner_loop, gimple_assign_rhs1 (ass),
    2395              :                                                fold_conversions, size_expr);
    2396         5581 :                 if (op0 == chrec_dont_know)
    2397              :                   return chrec_dont_know;
    2398         1392 :                 res = fold_build1 (gimple_assign_rhs_code (ass),
    2399              :                                    TREE_TYPE (chrec), op0);
    2400         1392 :                 break;
    2401              :               }
    2402        54133 :             case GIMPLE_BINARY_RHS:
    2403        54133 :               {
    2404        54133 :                 tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
    2405              :                                                inner_loop, gimple_assign_rhs1 (ass),
    2406              :                                                fold_conversions, size_expr);
    2407        54133 :                 if (op0 == chrec_dont_know)
    2408              :                   return chrec_dont_know;
    2409        12160 :                 tree op1 = instantiate_scev_r (instantiate_below, evolution_loop,
    2410              :                                                inner_loop, gimple_assign_rhs2 (ass),
    2411              :                                                fold_conversions, size_expr);
    2412         6080 :                 if (op1 == chrec_dont_know)
    2413              :                   return chrec_dont_know;
    2414         2315 :                 res = fold_build2 (gimple_assign_rhs_code (ass),
    2415              :                                    TREE_TYPE (chrec), op0, op1);
    2416         2315 :                 break;
    2417              :               }
    2418        75415 :             default:
    2419        75415 :               res = chrec_dont_know;
    2420              :             }
    2421              :         }
    2422              :       else
    2423        55726 :         res = chrec_dont_know;
    2424       134848 :       global_cache->set (si, res);
    2425       134848 :       return res;
    2426              :     }
    2427              : 
    2428              :   /* If the analysis yields a parametric chrec, instantiate the
    2429              :      result again.  */
    2430     32539451 :   res = analyze_scalar_evolution (def_loop, chrec);
    2431              : 
    2432              :   /* Don't instantiate default definitions.  */
    2433     32539451 :   if (TREE_CODE (res) == SSA_NAME
    2434     32539451 :       && SSA_NAME_IS_DEFAULT_DEF (res))
    2435              :     ;
    2436              : 
    2437              :   /* Don't instantiate loop-closed-ssa phi nodes.  */
    2438     32518631 :   else if (TREE_CODE (res) == SSA_NAME
    2439     95800496 :            && loop_depth (loop_containing_stmt (SSA_NAME_DEF_STMT (res)))
    2440     31641307 :            > loop_depth (def_loop))
    2441              :     {
    2442      1902594 :       if (res == chrec)
    2443      1879943 :         res = loop_closed_phi_def (chrec);
    2444              :       else
    2445              :         res = chrec;
    2446              : 
    2447              :       /* When there is no loop_closed_phi_def, it means that the
    2448              :          variable is not used after the loop: try to still compute the
    2449              :          value of the variable when exiting the loop.  */
    2450      1902594 :       if (res == NULL_TREE)
    2451              :         {
    2452      1618665 :           loop_p loop = loop_containing_stmt (SSA_NAME_DEF_STMT (chrec));
    2453      1618665 :           res = analyze_scalar_evolution (loop, chrec);
    2454      1618665 :           res = compute_overall_effect_of_inner_loop (loop, res);
    2455      1618665 :           res = instantiate_scev_r (instantiate_below, evolution_loop,
    2456              :                                     inner_loop, res,
    2457              :                                     fold_conversions, size_expr);
    2458              :         }
    2459       283929 :       else if (dominated_by_p (CDI_DOMINATORS,
    2460       283929 :                                 gimple_bb (SSA_NAME_DEF_STMT (res)),
    2461       283929 :                                 instantiate_below->dest))
    2462       283929 :         res = chrec_dont_know;
    2463              :     }
    2464              : 
    2465     30616037 :   else if (res != chrec_dont_know)
    2466              :     {
    2467     30616037 :       if (inner_loop
    2468      1273635 :           && def_bb->loop_father != inner_loop
    2469     31231962 :           && !flow_loop_nested_p (def_bb->loop_father, inner_loop))
    2470              :         /* ???  We could try to compute the overall effect of the loop here.  */
    2471          334 :         res = chrec_dont_know;
    2472              :       else
    2473     30615703 :         res = instantiate_scev_r (instantiate_below, evolution_loop,
    2474              :                                   inner_loop, res,
    2475              :                                   fold_conversions, size_expr);
    2476              :     }
    2477              : 
    2478              :   /* Store the correct value to the cache.  */
    2479     32539451 :   global_cache->set (si, res);
    2480     32539451 :   return res;
    2481              : }
    2482              : 
    2483              : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
    2484              :    and EVOLUTION_LOOP, that were left under a symbolic form.
    2485              : 
    2486              :    CHREC is a polynomial chain of recurrence to be instantiated.
    2487              : 
    2488              :    CACHE is the cache of already instantiated values.
    2489              : 
    2490              :    Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
    2491              :    conversions that may wrap in signed/pointer type are folded, as long
    2492              :    as the value of the chrec is preserved.  If FOLD_CONVERSIONS is NULL
    2493              :    then we don't do such fold.
    2494              : 
    2495              :    SIZE_EXPR is used for computing the size of the expression to be
    2496              :    instantiated, and to stop if it exceeds some limit.  */
    2497              : 
    2498              : static tree
    2499     60733947 : instantiate_scev_poly (edge instantiate_below,
    2500              :                        class loop *evolution_loop, class loop *,
    2501              :                        tree chrec, bool *fold_conversions, int size_expr)
    2502              : {
    2503     60733947 :   tree op1;
    2504    121467894 :   tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
    2505              :                                  get_chrec_loop (chrec),
    2506     60733947 :                                  CHREC_LEFT (chrec), fold_conversions,
    2507              :                                  size_expr);
    2508     60733947 :   if (op0 == chrec_dont_know)
    2509              :     return chrec_dont_know;
    2510              : 
    2511    121033254 :   op1 = instantiate_scev_r (instantiate_below, evolution_loop,
    2512              :                             get_chrec_loop (chrec),
    2513     60516627 :                             CHREC_RIGHT (chrec), fold_conversions,
    2514              :                             size_expr);
    2515     60516627 :   if (op1 == chrec_dont_know)
    2516              :     return chrec_dont_know;
    2517              : 
    2518     59746069 :   if (CHREC_LEFT (chrec) != op0
    2519     59746069 :       || CHREC_RIGHT (chrec) != op1)
    2520              :     {
    2521      7926612 :       op1 = chrec_convert_rhs (chrec_type (op0), op1, NULL);
    2522      7926612 :       chrec = build_polynomial_chrec (CHREC_VARIABLE (chrec), op0, op1);
    2523              :     }
    2524              : 
    2525              :   return chrec;
    2526              : }
    2527              : 
    2528              : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
    2529              :    and EVOLUTION_LOOP, that were left under a symbolic form.
    2530              : 
    2531              :    "C0 CODE C1" is a binary expression of type TYPE to be instantiated.
    2532              : 
    2533              :    CACHE is the cache of already instantiated values.
    2534              : 
    2535              :    Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
    2536              :    conversions that may wrap in signed/pointer type are folded, as long
    2537              :    as the value of the chrec is preserved.  If FOLD_CONVERSIONS is NULL
    2538              :    then we don't do such fold.
    2539              : 
    2540              :    SIZE_EXPR is used for computing the size of the expression to be
    2541              :    instantiated, and to stop if it exceeds some limit.  */
    2542              : 
    2543              : static tree
    2544     24233643 : instantiate_scev_binary (edge instantiate_below,
    2545              :                          class loop *evolution_loop, class loop *inner_loop,
    2546              :                          tree chrec, enum tree_code code,
    2547              :                          tree type, tree c0, tree c1,
    2548              :                          bool *fold_conversions, int size_expr)
    2549              : {
    2550     24233643 :   tree op1;
    2551     24233643 :   tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop,
    2552              :                                  c0, fold_conversions, size_expr);
    2553     24233643 :   if (op0 == chrec_dont_know)
    2554              :     return chrec_dont_know;
    2555              : 
    2556              :   /* While we eventually compute the same op1 if c0 == c1 the process
    2557              :      of doing this is expensive so the following short-cut prevents
    2558              :      exponential compile-time behavior.  */
    2559     23876166 :   if (c0 != c1)
    2560              :     {
    2561     23854083 :       op1 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop,
    2562              :                                 c1, fold_conversions, size_expr);
    2563     23854083 :       if (op1 == chrec_dont_know)
    2564              :         return chrec_dont_know;
    2565              :     }
    2566              :   else
    2567              :     op1 = op0;
    2568              : 
    2569     23806083 :   if (c0 != op0
    2570     23806083 :       || c1 != op1)
    2571              :     {
    2572     13664802 :       op0 = chrec_convert (type, op0, NULL);
    2573     13664802 :       op1 = chrec_convert_rhs (type, op1, NULL);
    2574              : 
    2575     13664802 :       switch (code)
    2576              :         {
    2577      7957991 :         case POINTER_PLUS_EXPR:
    2578      7957991 :         case PLUS_EXPR:
    2579      7957991 :           return chrec_fold_plus (type, op0, op1);
    2580              : 
    2581       881116 :         case MINUS_EXPR:
    2582       881116 :           return chrec_fold_minus (type, op0, op1);
    2583              : 
    2584      4825695 :         case MULT_EXPR:
    2585      4825695 :           return chrec_fold_multiply (type, op0, op1);
    2586              : 
    2587            0 :         default:
    2588            0 :           gcc_unreachable ();
    2589              :         }
    2590              :     }
    2591              : 
    2592     10141281 :   return chrec ? chrec : fold_build2 (code, type, c0, c1);
    2593              : }
    2594              : 
    2595              : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
    2596              :    and EVOLUTION_LOOP, that were left under a symbolic form.
    2597              : 
    2598              :    "CHREC" that stands for a convert expression "(TYPE) OP" is to be
    2599              :    instantiated.
    2600              : 
    2601              :    CACHE is the cache of already instantiated values.
    2602              : 
    2603              :    Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
    2604              :    conversions that may wrap in signed/pointer type are folded, as long
    2605              :    as the value of the chrec is preserved.  If FOLD_CONVERSIONS is NULL
    2606              :    then we don't do such fold.
    2607              : 
    2608              :    SIZE_EXPR is used for computing the size of the expression to be
    2609              :    instantiated, and to stop if it exceeds some limit.  */
    2610              : 
    2611              : static tree
    2612     24536496 : instantiate_scev_convert (edge instantiate_below,
    2613              :                           class loop *evolution_loop, class loop *inner_loop,
    2614              :                           tree chrec, tree type, tree op,
    2615              :                           bool *fold_conversions, int size_expr)
    2616              : {
    2617     24536496 :   tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
    2618              :                                  inner_loop, op,
    2619              :                                  fold_conversions, size_expr);
    2620              : 
    2621     24536496 :   if (op0 == chrec_dont_know)
    2622              :     return chrec_dont_know;
    2623              : 
    2624     19500841 :   if (fold_conversions)
    2625              :     {
    2626      7585709 :       tree tmp = chrec_convert_aggressive (type, op0, fold_conversions);
    2627      7585709 :       if (tmp)
    2628              :         return tmp;
    2629              : 
    2630              :       /* If we used chrec_convert_aggressive, we can no longer assume that
    2631              :          signed chrecs do not overflow, as chrec_convert does, so avoid
    2632              :          calling it in that case.  */
    2633      7088073 :       if (*fold_conversions)
    2634              :         {
    2635         8263 :           if (chrec && op0 == op)
    2636              :             return chrec;
    2637              : 
    2638         8263 :           return fold_convert (type, op0);
    2639              :         }
    2640              :     }
    2641              : 
    2642     18994942 :   return chrec_convert (type, op0, NULL);
    2643              : }
    2644              : 
    2645              : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
    2646              :    and EVOLUTION_LOOP, that were left under a symbolic form.
    2647              : 
    2648              :    CHREC is a BIT_NOT_EXPR or a NEGATE_EXPR expression to be instantiated.
    2649              :    Handle ~X as -1 - X.
    2650              :    Handle -X as -1 * X.
    2651              : 
    2652              :    CACHE is the cache of already instantiated values.
    2653              : 
    2654              :    Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
    2655              :    conversions that may wrap in signed/pointer type are folded, as long
    2656              :    as the value of the chrec is preserved.  If FOLD_CONVERSIONS is NULL
    2657              :    then we don't do such fold.
    2658              : 
    2659              :    SIZE_EXPR is used for computing the size of the expression to be
    2660              :    instantiated, and to stop if it exceeds some limit.  */
    2661              : 
    2662              : static tree
    2663       337490 : instantiate_scev_not (edge instantiate_below,
    2664              :                       class loop *evolution_loop, class loop *inner_loop,
    2665              :                       tree chrec,
    2666              :                       enum tree_code code, tree type, tree op,
    2667              :                       bool *fold_conversions, int size_expr)
    2668              : {
    2669       337490 :   tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
    2670              :                                  inner_loop, op,
    2671              :                                  fold_conversions, size_expr);
    2672              : 
    2673       337490 :   if (op0 == chrec_dont_know)
    2674              :     return chrec_dont_know;
    2675              : 
    2676       278986 :   if (op != op0)
    2677              :     {
    2678       208288 :       op0 = chrec_convert (type, op0, NULL);
    2679              : 
    2680       208288 :       switch (code)
    2681              :         {
    2682         1903 :         case BIT_NOT_EXPR:
    2683         1903 :           return chrec_fold_minus
    2684         1903 :             (type, fold_convert (type, integer_minus_one_node), op0);
    2685              : 
    2686       206385 :         case NEGATE_EXPR:
    2687       206385 :           return chrec_fold_multiply
    2688       206385 :             (type, fold_convert (type, integer_minus_one_node), op0);
    2689              : 
    2690            0 :         default:
    2691            0 :           gcc_unreachable ();
    2692              :         }
    2693              :     }
    2694              : 
    2695        70698 :   return chrec ? chrec : fold_build1 (code, type, op0);
    2696              : }
    2697              : 
    2698              : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
    2699              :    and EVOLUTION_LOOP, that were left under a symbolic form.
    2700              : 
    2701              :    CHREC is the scalar evolution to instantiate.
    2702              : 
    2703              :    CACHE is the cache of already instantiated values.
    2704              : 
    2705              :    Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
    2706              :    conversions that may wrap in signed/pointer type are folded, as long
    2707              :    as the value of the chrec is preserved.  If FOLD_CONVERSIONS is NULL
    2708              :    then we don't do such fold.
    2709              : 
    2710              :    SIZE_EXPR is used for computing the size of the expression to be
    2711              :    instantiated, and to stop if it exceeds some limit.  */
    2712              : 
    2713              : static tree
    2714    364572000 : instantiate_scev_r (edge instantiate_below,
    2715              :                     class loop *evolution_loop, class loop *inner_loop,
    2716              :                     tree chrec,
    2717              :                     bool *fold_conversions, int size_expr)
    2718              : {
    2719              :   /* Give up if the expression is larger than the MAX that we allow.  */
    2720    364572000 :   if (size_expr++ > param_scev_max_expr_size)
    2721           11 :     return chrec_dont_know;
    2722              : 
    2723    364571989 :   if (chrec == NULL_TREE
    2724    505801436 :       || automatically_generated_chrec_p (chrec)
    2725    727151516 :       || is_gimple_min_invariant (chrec))
    2726    143221909 :     return chrec;
    2727              : 
    2728    221350080 :   switch (TREE_CODE (chrec))
    2729              :     {
    2730    110799703 :     case SSA_NAME:
    2731    110799703 :       return instantiate_scev_name (instantiate_below, evolution_loop,
    2732              :                                     inner_loop, chrec,
    2733    110799703 :                                     fold_conversions, size_expr);
    2734              : 
    2735     60733947 :     case POLYNOMIAL_CHREC:
    2736     60733947 :       return instantiate_scev_poly (instantiate_below, evolution_loop,
    2737              :                                     inner_loop, chrec,
    2738     60733947 :                                     fold_conversions, size_expr);
    2739              : 
    2740     24233643 :     case POINTER_PLUS_EXPR:
    2741     24233643 :     case PLUS_EXPR:
    2742     24233643 :     case MINUS_EXPR:
    2743     24233643 :     case MULT_EXPR:
    2744     24233643 :       return instantiate_scev_binary (instantiate_below, evolution_loop,
    2745              :                                       inner_loop, chrec,
    2746              :                                       TREE_CODE (chrec), chrec_type (chrec),
    2747     24233643 :                                       TREE_OPERAND (chrec, 0),
    2748     24233643 :                                       TREE_OPERAND (chrec, 1),
    2749     24233643 :                                       fold_conversions, size_expr);
    2750              : 
    2751     24536496 :     CASE_CONVERT:
    2752     24536496 :       return instantiate_scev_convert (instantiate_below, evolution_loop,
    2753              :                                        inner_loop, chrec,
    2754     24536496 :                                        TREE_TYPE (chrec), TREE_OPERAND (chrec, 0),
    2755     24536496 :                                        fold_conversions, size_expr);
    2756              : 
    2757       337490 :     case NEGATE_EXPR:
    2758       337490 :     case BIT_NOT_EXPR:
    2759       337490 :       return instantiate_scev_not (instantiate_below, evolution_loop,
    2760              :                                    inner_loop, chrec,
    2761       337490 :                                    TREE_CODE (chrec), TREE_TYPE (chrec),
    2762       337490 :                                    TREE_OPERAND (chrec, 0),
    2763       337490 :                                    fold_conversions, size_expr);
    2764              : 
    2765            0 :     case ADDR_EXPR:
    2766            0 :       if (is_gimple_min_invariant (chrec))
    2767              :         return chrec;
    2768              :       /* Fallthru.  */
    2769            0 :     case SCEV_NOT_KNOWN:
    2770            0 :       return chrec_dont_know;
    2771              : 
    2772            0 :     case SCEV_KNOWN:
    2773            0 :       return chrec_known;
    2774              : 
    2775       708801 :     default:
    2776       708801 :       if (CONSTANT_CLASS_P (chrec))
    2777              :         return chrec;
    2778       708801 :       return chrec_dont_know;
    2779              :     }
    2780              : }
    2781              : 
    2782              : /* Analyze all the parameters of the chrec that were left under a
    2783              :    symbolic form.  INSTANTIATE_BELOW is the basic block that stops the
    2784              :    recursive instantiation of parameters: a parameter is a variable
    2785              :    that is defined in a basic block that dominates INSTANTIATE_BELOW or
    2786              :    a function parameter.  */
    2787              : 
    2788              : tree
    2789     87470980 : instantiate_scev (edge instantiate_below, class loop *evolution_loop,
    2790              :                   tree chrec)
    2791              : {
    2792     87470980 :   tree res;
    2793              : 
    2794     87470980 :   if (dump_file && (dump_flags & TDF_SCEV))
    2795              :     {
    2796           20 :       fprintf (dump_file, "(instantiate_scev \n");
    2797           20 :       fprintf (dump_file, "  (instantiate_below = %d -> %d)\n",
    2798           20 :                instantiate_below->src->index, instantiate_below->dest->index);
    2799           20 :       if (evolution_loop)
    2800           20 :         fprintf (dump_file, "  (evolution_loop = %d)\n", evolution_loop->num);
    2801           20 :       fprintf (dump_file, "  (chrec = ");
    2802           20 :       print_generic_expr (dump_file, chrec);
    2803           20 :       fprintf (dump_file, ")\n");
    2804              :     }
    2805              : 
    2806     87470980 :   bool destr = false;
    2807     87470980 :   if (!global_cache)
    2808              :     {
    2809     37989116 :       global_cache = new instantiate_cache_type;
    2810     37989116 :       destr = true;
    2811              :     }
    2812              : 
    2813     87470980 :   res = instantiate_scev_r (instantiate_below, evolution_loop,
    2814              :                             NULL, chrec, NULL, 0);
    2815              : 
    2816     87470980 :   if (destr)
    2817              :     {
    2818     37989116 :       delete global_cache;
    2819     37989116 :       global_cache = NULL;
    2820              :     }
    2821              : 
    2822     87470980 :   if (dump_file && (dump_flags & TDF_SCEV))
    2823              :     {
    2824           20 :       fprintf (dump_file, "  (res = ");
    2825           20 :       print_generic_expr (dump_file, res);
    2826           20 :       fprintf (dump_file, "))\n");
    2827              :     }
    2828              : 
    2829     87470980 :   return res;
    2830              : }
    2831              : 
    2832              : /* Similar to instantiate_parameters, but does not introduce the
    2833              :    evolutions in outer loops for LOOP invariants in CHREC, and does not
    2834              :    care about causing overflows, as long as they do not affect value
    2835              :    of an expression.  */
    2836              : 
    2837              : tree
    2838     50588572 : resolve_mixers (class loop *loop, tree chrec, bool *folded_casts)
    2839              : {
    2840     50588572 :   bool destr = false;
    2841     50588572 :   bool fold_conversions = false;
    2842     50588572 :   if (!global_cache)
    2843              :     {
    2844     49888963 :       global_cache = new instantiate_cache_type;
    2845     49888963 :       destr = true;
    2846              :     }
    2847              : 
    2848     50588572 :   tree ret = instantiate_scev_r (loop_preheader_edge (loop), loop, NULL,
    2849              :                                  chrec, &fold_conversions, 0);
    2850              : 
    2851     50588572 :   if (folded_casts && !*folded_casts)
    2852     50588572 :     *folded_casts = fold_conversions;
    2853              : 
    2854     50588572 :   if (destr)
    2855              :     {
    2856     49888963 :       delete global_cache;
    2857     49888963 :       global_cache = NULL;
    2858              :     }
    2859              : 
    2860     50588572 :   return ret;
    2861              : }
    2862              : 
    2863              : /* Entry point for the analysis of the number of iterations pass.
    2864              :    This function tries to safely approximate the number of iterations
    2865              :    the loop will run.  When this property is not decidable at compile
    2866              :    time, the result is chrec_dont_know.  Otherwise the result is a
    2867              :    scalar or a symbolic parameter.  When the number of iterations may
    2868              :    be equal to zero and the property cannot be determined at compile
    2869              :    time, the result is a COND_EXPR that represents in a symbolic form
    2870              :    the conditions under which the number of iterations is not zero.
    2871              : 
    2872              :    Example of analysis: suppose that the loop has an exit condition:
    2873              : 
    2874              :    "if (b > 49) goto end_loop;"
    2875              : 
    2876              :    and that in a previous analysis we have determined that the
    2877              :    variable 'b' has an evolution function:
    2878              : 
    2879              :    "EF = {23, +, 5}_2".
    2880              : 
    2881              :    When we evaluate the function at the point 5, i.e. the value of the
    2882              :    variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
    2883              :    and EF (6) = 53.  In this case the value of 'b' on exit is '53' and
    2884              :    the loop body has been executed 6 times.  */
    2885              : 
    2886              : tree
    2887     10601442 : number_of_latch_executions (class loop *loop)
    2888              : {
    2889     10601442 :   edge exit;
    2890     10601442 :   class tree_niter_desc niter_desc;
    2891     10601442 :   tree may_be_zero;
    2892     10601442 :   tree res;
    2893              : 
    2894              :   /* Determine whether the number of iterations in loop has already
    2895              :      been computed.  */
    2896     10601442 :   res = loop->nb_iterations;
    2897     10601442 :   if (res)
    2898              :     return res;
    2899              : 
    2900      6977828 :   may_be_zero = NULL_TREE;
    2901              : 
    2902      6977828 :   if (dump_file && (dump_flags & TDF_SCEV))
    2903            1 :     fprintf (dump_file, "(number_of_iterations_in_loop = \n");
    2904              : 
    2905      6977828 :   res = chrec_dont_know;
    2906      6977828 :   exit = single_exit (loop);
    2907              : 
    2908      6977828 :   if (exit && number_of_iterations_exit (loop, exit, &niter_desc, false))
    2909              :     {
    2910      3520360 :       may_be_zero = niter_desc.may_be_zero;
    2911      3520360 :       res = niter_desc.niter;
    2912              :     }
    2913              : 
    2914      6977828 :   if (res == chrec_dont_know
    2915      3520360 :       || !may_be_zero
    2916     10498188 :       || integer_zerop (may_be_zero))
    2917              :     ;
    2918       540491 :   else if (integer_nonzerop (may_be_zero))
    2919           93 :     res = build_int_cst (TREE_TYPE (res), 0);
    2920              : 
    2921       540398 :   else if (COMPARISON_CLASS_P (may_be_zero))
    2922       540398 :     res = fold_build3 (COND_EXPR, TREE_TYPE (res), may_be_zero,
    2923              :                        build_int_cst (TREE_TYPE (res), 0), res);
    2924              :   else
    2925            0 :     res = chrec_dont_know;
    2926              : 
    2927      6977828 :   if (dump_file && (dump_flags & TDF_SCEV))
    2928              :     {
    2929            1 :       fprintf (dump_file, "  (set_nb_iterations_in_loop = ");
    2930            1 :       print_generic_expr (dump_file, res);
    2931            1 :       fprintf (dump_file, "))\n");
    2932              :     }
    2933              : 
    2934      6977828 :   loop->nb_iterations = res;
    2935      6977828 :   return res;
    2936     10601442 : }
    2937              : 
    2938              : 
    2939              : /* Counters for the stats.  */
    2940              : 
    2941              : struct chrec_stats
    2942              : {
    2943              :   unsigned nb_chrecs;
    2944              :   unsigned nb_affine;
    2945              :   unsigned nb_affine_multivar;
    2946              :   unsigned nb_higher_poly;
    2947              :   unsigned nb_chrec_dont_know;
    2948              :   unsigned nb_undetermined;
    2949              : };
    2950              : 
    2951              : /* Reset the counters.  */
    2952              : 
    2953              : static inline void
    2954            0 : reset_chrecs_counters (struct chrec_stats *stats)
    2955              : {
    2956            0 :   stats->nb_chrecs = 0;
    2957            0 :   stats->nb_affine = 0;
    2958            0 :   stats->nb_affine_multivar = 0;
    2959            0 :   stats->nb_higher_poly = 0;
    2960            0 :   stats->nb_chrec_dont_know = 0;
    2961            0 :   stats->nb_undetermined = 0;
    2962              : }
    2963              : 
    2964              : /* Dump the contents of a CHREC_STATS structure.  */
    2965              : 
    2966              : static void
    2967            0 : dump_chrecs_stats (FILE *file, struct chrec_stats *stats)
    2968              : {
    2969            0 :   fprintf (file, "\n(\n");
    2970            0 :   fprintf (file, "-----------------------------------------\n");
    2971            0 :   fprintf (file, "%d\taffine univariate chrecs\n", stats->nb_affine);
    2972            0 :   fprintf (file, "%d\taffine multivariate chrecs\n", stats->nb_affine_multivar);
    2973            0 :   fprintf (file, "%d\tdegree greater than 2 polynomials\n",
    2974              :            stats->nb_higher_poly);
    2975            0 :   fprintf (file, "%d\tchrec_dont_know chrecs\n", stats->nb_chrec_dont_know);
    2976            0 :   fprintf (file, "-----------------------------------------\n");
    2977            0 :   fprintf (file, "%d\ttotal chrecs\n", stats->nb_chrecs);
    2978            0 :   fprintf (file, "%d\twith undetermined coefficients\n",
    2979              :            stats->nb_undetermined);
    2980            0 :   fprintf (file, "-----------------------------------------\n");
    2981            0 :   fprintf (file, "%d\tchrecs in the scev database\n",
    2982            0 :            (int) scalar_evolution_info->elements ());
    2983            0 :   fprintf (file, "%d\tsets in the scev database\n", nb_set_scev);
    2984            0 :   fprintf (file, "%d\tgets in the scev database\n", nb_get_scev);
    2985            0 :   fprintf (file, "-----------------------------------------\n");
    2986            0 :   fprintf (file, ")\n\n");
    2987            0 : }
    2988              : 
    2989              : /* Gather statistics about CHREC.  */
    2990              : 
    2991              : static void
    2992            0 : gather_chrec_stats (tree chrec, struct chrec_stats *stats)
    2993              : {
    2994            0 :   if (dump_file && (dump_flags & TDF_STATS))
    2995              :     {
    2996            0 :       fprintf (dump_file, "(classify_chrec ");
    2997            0 :       print_generic_expr (dump_file, chrec);
    2998            0 :       fprintf (dump_file, "\n");
    2999              :     }
    3000              : 
    3001            0 :   stats->nb_chrecs++;
    3002              : 
    3003            0 :   if (chrec == NULL_TREE)
    3004              :     {
    3005            0 :       stats->nb_undetermined++;
    3006            0 :       return;
    3007              :     }
    3008              : 
    3009            0 :   switch (TREE_CODE (chrec))
    3010              :     {
    3011            0 :     case POLYNOMIAL_CHREC:
    3012            0 :       if (evolution_function_is_affine_p (chrec))
    3013              :         {
    3014            0 :           if (dump_file && (dump_flags & TDF_STATS))
    3015            0 :             fprintf (dump_file, "  affine_univariate\n");
    3016            0 :           stats->nb_affine++;
    3017              :         }
    3018            0 :       else if (evolution_function_is_affine_multivariate_p (chrec, 0))
    3019              :         {
    3020            0 :           if (dump_file && (dump_flags & TDF_STATS))
    3021            0 :             fprintf (dump_file, "  affine_multivariate\n");
    3022            0 :           stats->nb_affine_multivar++;
    3023              :         }
    3024              :       else
    3025              :         {
    3026            0 :           if (dump_file && (dump_flags & TDF_STATS))
    3027            0 :             fprintf (dump_file, "  higher_degree_polynomial\n");
    3028            0 :           stats->nb_higher_poly++;
    3029              :         }
    3030              : 
    3031              :       break;
    3032              : 
    3033              :     default:
    3034              :       break;
    3035              :     }
    3036              : 
    3037            0 :   if (chrec_contains_undetermined (chrec))
    3038              :     {
    3039            0 :       if (dump_file && (dump_flags & TDF_STATS))
    3040            0 :         fprintf (dump_file, "  undetermined\n");
    3041            0 :       stats->nb_undetermined++;
    3042              :     }
    3043              : 
    3044            0 :   if (dump_file && (dump_flags & TDF_STATS))
    3045            0 :     fprintf (dump_file, ")\n");
    3046              : }
    3047              : 
    3048              : /* Classify the chrecs of the whole database.  */
    3049              : 
    3050              : void
    3051            0 : gather_stats_on_scev_database (void)
    3052              : {
    3053            0 :   struct chrec_stats stats;
    3054              : 
    3055            0 :   if (!dump_file)
    3056            0 :     return;
    3057              : 
    3058            0 :   reset_chrecs_counters (&stats);
    3059              : 
    3060            0 :   hash_table<scev_info_hasher>::iterator iter;
    3061            0 :   scev_info_str *elt;
    3062            0 :   FOR_EACH_HASH_TABLE_ELEMENT (*scalar_evolution_info, elt, scev_info_str *,
    3063              :                                iter)
    3064            0 :     gather_chrec_stats (elt->chrec, &stats);
    3065              : 
    3066            0 :   dump_chrecs_stats (dump_file, &stats);
    3067              : }
    3068              : 
    3069              : 
    3070              : /* Initialize the analysis of scalar evolutions for LOOPS.  */
    3071              : 
    3072              : void
    3073     14842676 : scev_initialize (void)
    3074              : {
    3075     14842676 :   gcc_assert (! scev_initialized_p ()
    3076              :               && loops_state_satisfies_p (cfun, LOOPS_NORMAL));
    3077              : 
    3078     14842676 :   scalar_evolution_info = hash_table<scev_info_hasher>::create_ggc (100);
    3079              : 
    3080     53805292 :   for (auto loop : loops_list (cfun, 0))
    3081      9277264 :     loop->nb_iterations = NULL_TREE;
    3082     14842676 : }
    3083              : 
    3084              : /* Return true if SCEV is initialized.  */
    3085              : 
    3086              : bool
    3087    100354795 : scev_initialized_p (void)
    3088              : {
    3089    100354795 :   return scalar_evolution_info != NULL;
    3090              : }
    3091              : 
    3092              : /* Cleans up the information cached by the scalar evolutions analysis
    3093              :    in the hash table.  */
    3094              : 
    3095              : void
    3096     23596151 : scev_reset_htab (void)
    3097              : {
    3098     23596151 :   if (!scalar_evolution_info)
    3099              :     return;
    3100              : 
    3101      6098769 :   scalar_evolution_info->empty ();
    3102              : }
    3103              : 
    3104              : /* Cleans up the information cached by the scalar evolutions analysis
    3105              :    in the hash table and in the loop->nb_iterations.  */
    3106              : 
    3107              : void
    3108     12734898 : scev_reset (void)
    3109              : {
    3110     12734898 :   scev_reset_htab ();
    3111              : 
    3112     62832824 :   for (auto loop : loops_list (cfun, 0))
    3113     24628130 :     loop->nb_iterations = NULL_TREE;
    3114     12734898 : }
    3115              : 
    3116              : /* Return true if the IV calculation in TYPE can overflow based on the knowledge
    3117              :    of the upper bound on the number of iterations of LOOP, the BASE and STEP
    3118              :    of IV.
    3119              : 
    3120              :    We do not use information whether TYPE can overflow so it is safe to
    3121              :    use this test even for derived IVs not computed every iteration or
    3122              :    hypotetical IVs to be inserted into code.  */
    3123              : 
    3124              : bool
    3125     15096223 : iv_can_overflow_p (class loop *loop, tree type, tree base, tree step)
    3126              : {
    3127     15096223 :   widest_int nit;
    3128     15096223 :   wide_int base_min, base_max, step_min, step_max, type_min, type_max;
    3129     15096223 :   signop sgn = TYPE_SIGN (type);
    3130     15096223 :   int_range_max r;
    3131              : 
    3132     15096223 :   if (integer_zerop (step))
    3133              :     return false;
    3134              : 
    3135     30191846 :   if (!INTEGRAL_TYPE_P (TREE_TYPE (base))
    3136     28069632 :       || !get_range_query (cfun)->range_of_expr (r, base)
    3137     14034816 :       || r.varying_p ()
    3138     27507726 :       || r.undefined_p ())
    3139      2686964 :     return true;
    3140              : 
    3141     12409252 :   base_min = r.lower_bound ();
    3142     12409252 :   base_max = r.upper_bound ();
    3143              : 
    3144     24817945 :   if (!INTEGRAL_TYPE_P (TREE_TYPE (step))
    3145     24818504 :       || !get_range_query (cfun)->range_of_expr (r, step)
    3146     12409252 :       || r.varying_p ()
    3147     24739318 :       || r.undefined_p ())
    3148        79186 :     return true;
    3149              : 
    3150     12330066 :   step_min = r.lower_bound ();
    3151     12330066 :   step_max = r.upper_bound ();
    3152              : 
    3153     12330066 :   if (!get_max_loop_iterations (loop, &nit))
    3154              :     return true;
    3155              : 
    3156     11662873 :   type_min = wi::min_value (type);
    3157     11662873 :   type_max = wi::max_value (type);
    3158              : 
    3159              :   /* Just sanity check that we don't see values out of the range of the type.
    3160              :      In this case the arithmetics below would overflow.  */
    3161     11662873 :   gcc_checking_assert (wi::ge_p (base_min, type_min, sgn)
    3162              :                        && wi::le_p (base_max, type_max, sgn));
    3163              : 
    3164              :   /* Account the possible increment in the last ieration.  */
    3165     11662873 :   wi::overflow_type overflow = wi::OVF_NONE;
    3166     11662873 :   nit = wi::add (nit, 1, SIGNED, &overflow);
    3167     11662873 :   if (overflow)
    3168              :     return true;
    3169              : 
    3170              :   /* NIT is typeless and can exceed the precision of the type.  In this case
    3171              :      overflow is always possible, because we know STEP is non-zero.  */
    3172     11662873 :   if (wi::min_precision (nit, UNSIGNED) > TYPE_PRECISION (type))
    3173              :     return true;
    3174     11417909 :   wide_int nit2 = wide_int::from (nit, TYPE_PRECISION (type), UNSIGNED);
    3175              : 
    3176              :   /* If step can be positive, check that nit*step <= type_max-base.
    3177              :      This can be done by unsigned arithmetic and we only need to watch overflow
    3178              :      in the multiplication. The right hand side can always be represented in
    3179              :      the type.  */
    3180     11417909 :   if (sgn == UNSIGNED || !wi::neg_p (step_max))
    3181              :     {
    3182     11374342 :       wi::overflow_type overflow = wi::OVF_NONE;
    3183     11374342 :       if (wi::gtu_p (wi::mul (step_max, nit2, UNSIGNED, &overflow),
    3184     22748684 :                      type_max - base_max)
    3185     22748684 :           || overflow)
    3186      5749045 :         return true;
    3187              :     }
    3188              :   /* If step can be negative, check that nit*(-step) <= base_min-type_min.  */
    3189      5668864 :   if (sgn == SIGNED && wi::neg_p (step_min))
    3190              :     {
    3191        43916 :       wi::overflow_type overflow, overflow2;
    3192        43916 :       overflow = overflow2 = wi::OVF_NONE;
    3193        87832 :       if (wi::gtu_p (wi::mul (wi::neg (step_min, &overflow2),
    3194              :                      nit2, UNSIGNED, &overflow),
    3195        87832 :                      base_min - type_min)
    3196        87832 :           || overflow || overflow2)
    3197        16335 :         return true;
    3198              :     }
    3199              : 
    3200              :   return false;
    3201     15096223 : }
    3202              : 
    3203              : /* Given EV with form of "(type) {inner_base, inner_step}_loop", this
    3204              :    function tries to derive condition under which it can be simplified
    3205              :    into "{(type)inner_base, (type)inner_step}_loop".  The condition is
    3206              :    the maximum number that inner iv can iterate.  */
    3207              : 
    3208              : static tree
    3209        40875 : derive_simple_iv_with_niters (tree ev, tree *niters)
    3210              : {
    3211        40875 :   if (!CONVERT_EXPR_P (ev))
    3212              :     return ev;
    3213              : 
    3214        40875 :   tree inner_ev = TREE_OPERAND (ev, 0);
    3215        40875 :   if (TREE_CODE (inner_ev) != POLYNOMIAL_CHREC)
    3216              :     return ev;
    3217              : 
    3218        40875 :   tree init = CHREC_LEFT (inner_ev);
    3219        40875 :   tree step = CHREC_RIGHT (inner_ev);
    3220        40875 :   if (TREE_CODE (init) != INTEGER_CST
    3221        40875 :       || TREE_CODE (step) != INTEGER_CST || integer_zerop (step))
    3222         7783 :     return ev;
    3223              : 
    3224        33092 :   tree type = TREE_TYPE (ev);
    3225        33092 :   tree inner_type = TREE_TYPE (inner_ev);
    3226        33092 :   if (TYPE_PRECISION (inner_type) >= TYPE_PRECISION (type))
    3227              :     return ev;
    3228              : 
    3229              :   /* Type conversion in "(type) {inner_base, inner_step}_loop" can be
    3230              :      folded only if inner iv won't overflow.  We compute the maximum
    3231              :      number the inner iv can iterate before overflowing and return the
    3232              :      simplified affine iv.  */
    3233        33092 :   tree delta;
    3234        33092 :   init = fold_convert (type, init);
    3235        33092 :   step = fold_convert (type, step);
    3236        33092 :   ev = build_polynomial_chrec (CHREC_VARIABLE (inner_ev), init, step);
    3237        33092 :   if (tree_int_cst_sign_bit (step))
    3238              :     {
    3239            0 :       tree bound = lower_bound_in_type (inner_type, inner_type);
    3240            0 :       delta = fold_build2 (MINUS_EXPR, type, init, fold_convert (type, bound));
    3241            0 :       step = fold_build1 (NEGATE_EXPR, type, step);
    3242              :     }
    3243              :   else
    3244              :     {
    3245        33092 :       tree bound = upper_bound_in_type (inner_type, inner_type);
    3246        33092 :       delta = fold_build2 (MINUS_EXPR, type, fold_convert (type, bound), init);
    3247              :     }
    3248        33092 :   *niters = fold_build2 (FLOOR_DIV_EXPR, type, delta, step);
    3249        33092 :   return ev;
    3250              : }
    3251              : 
    3252              : /* Checks whether use of OP in USE_LOOP behaves as a simple affine iv with
    3253              :    respect to WRTO_LOOP and returns its base and step in IV if possible
    3254              :    (see analyze_scalar_evolution_in_loop for more details on USE_LOOP
    3255              :    and WRTO_LOOP).  If ALLOW_NONCONSTANT_STEP is true, we want step to be
    3256              :    invariant in LOOP.  Otherwise we require it to be an integer constant.
    3257              : 
    3258              :    IV->no_overflow is set to true if we are sure the iv cannot overflow (e.g.
    3259              :    because it is computed in signed arithmetics).  Consequently, adding an
    3260              :    induction variable
    3261              : 
    3262              :    for (i = IV->base; ; i += IV->step)
    3263              : 
    3264              :    is only safe if IV->no_overflow is false, or TYPE_OVERFLOW_UNDEFINED is
    3265              :    false for the type of the induction variable, or you can prove that i does
    3266              :    not wrap by some other argument.  Otherwise, this might introduce undefined
    3267              :    behavior, and
    3268              : 
    3269              :    i = iv->base;
    3270              :    for (; ; i = (type) ((unsigned type) i + (unsigned type) iv->step))
    3271              : 
    3272              :    must be used instead.
    3273              : 
    3274              :    When IV_NITERS is not NULL, this function also checks case in which OP
    3275              :    is a conversion of an inner simple iv of below form:
    3276              : 
    3277              :      (outer_type){inner_base, inner_step}_loop.
    3278              : 
    3279              :    If type of inner iv has smaller precision than outer_type, it can't be
    3280              :    folded into {(outer_type)inner_base, (outer_type)inner_step}_loop because
    3281              :    the inner iv could overflow/wrap.  In this case, we derive a condition
    3282              :    under which the inner iv won't overflow/wrap and do the simplification.
    3283              :    The derived condition normally is the maximum number the inner iv can
    3284              :    iterate, and will be stored in IV_NITERS.  This is useful in loop niter
    3285              :    analysis, to derive break conditions when a loop must terminate, when is
    3286              :    infinite.  */
    3287              : 
    3288              : bool
    3289     51403057 : simple_iv_with_niters (class loop *wrto_loop, class loop *use_loop,
    3290              :                        tree op, affine_iv *iv, tree *iv_niters,
    3291              :                        bool allow_nonconstant_step)
    3292              : {
    3293     51403057 :   enum tree_code code;
    3294     51403057 :   tree type, ev, base, e;
    3295     51403057 :   wide_int extreme;
    3296     51403057 :   bool folded_casts;
    3297              : 
    3298     51403057 :   iv->base = NULL_TREE;
    3299     51403057 :   iv->step = NULL_TREE;
    3300     51403057 :   iv->no_overflow = false;
    3301              : 
    3302     51403057 :   type = TREE_TYPE (op);
    3303     51403057 :   if (!POINTER_TYPE_P (type)
    3304     42273035 :       && !INTEGRAL_TYPE_P (type))
    3305              :     return false;
    3306              : 
    3307     49326733 :   ev = analyze_scalar_evolution_in_loop (wrto_loop, use_loop, op,
    3308              :                                          &folded_casts);
    3309     49326733 :   if (chrec_contains_undetermined (ev)
    3310     49326733 :       || chrec_contains_symbols_defined_in_loop (ev, wrto_loop->num))
    3311     12799715 :     return false;
    3312              : 
    3313     36527018 :   if (tree_does_not_contain_chrecs (ev))
    3314              :     {
    3315     16131618 :       iv->base = ev;
    3316     16131618 :       tree ev_type = TREE_TYPE (ev);
    3317     16131618 :       if (POINTER_TYPE_P (ev_type))
    3318      2670189 :         ev_type = sizetype;
    3319              : 
    3320     16131618 :       iv->step = build_int_cst (ev_type, 0);
    3321     16131618 :       iv->no_overflow = true;
    3322     16131618 :       return true;
    3323              :     }
    3324              : 
    3325              :   /* If we can derive valid scalar evolution with assumptions.  */
    3326     20395400 :   if (iv_niters && TREE_CODE (ev) != POLYNOMIAL_CHREC)
    3327        40875 :     ev = derive_simple_iv_with_niters (ev, iv_niters);
    3328              : 
    3329     20395400 :   if (TREE_CODE (ev) != POLYNOMIAL_CHREC)
    3330              :     return false;
    3331              : 
    3332     20347713 :   if (CHREC_VARIABLE (ev) != (unsigned) wrto_loop->num)
    3333              :     return false;
    3334              : 
    3335     20347699 :   iv->step = CHREC_RIGHT (ev);
    3336     12975282 :   if ((!allow_nonconstant_step && TREE_CODE (iv->step) != INTEGER_CST)
    3337     33066329 :       || tree_contains_chrecs (iv->step, NULL))
    3338       268023 :     return false;
    3339              : 
    3340     20079676 :   iv->base = CHREC_LEFT (ev);
    3341     20079676 :   if (tree_contains_chrecs (iv->base, NULL))
    3342              :     return false;
    3343              : 
    3344     20079676 :   iv->no_overflow = !folded_casts && nowrap_type_p (type);
    3345              : 
    3346     20079676 :   if (!iv->no_overflow
    3347     20079676 :       && !iv_can_overflow_p (wrto_loop, type, iv->base, iv->step))
    3348      3789927 :     iv->no_overflow = true;
    3349              : 
    3350              :   /* Try to simplify iv base:
    3351              : 
    3352              :        (signed T) ((unsigned T)base + step) ;; TREE_TYPE (base) == signed T
    3353              :          == (signed T)(unsigned T)base + step
    3354              :          == base + step
    3355              : 
    3356              :      If we can prove operation (base + step) doesn't overflow or underflow.
    3357              :      Specifically, we try to prove below conditions are satisfied:
    3358              : 
    3359              :              base <= UPPER_BOUND (type) - step  ;;step > 0
    3360              :              base >= LOWER_BOUND (type) - step  ;;step < 0
    3361              : 
    3362              :      This is done by proving the reverse conditions are false using loop's
    3363              :      initial conditions.
    3364              : 
    3365              :      The is necessary to make loop niter, or iv overflow analysis easier
    3366              :      for below example:
    3367              : 
    3368              :        int foo (int *a, signed char s, signed char l)
    3369              :          {
    3370              :            signed char i;
    3371              :            for (i = s; i < l; i++)
    3372              :              a[i] = 0;
    3373              :            return 0;
    3374              :           }
    3375              : 
    3376              :      Note variable I is firstly converted to type unsigned char, incremented,
    3377              :      then converted back to type signed char.  */
    3378              : 
    3379     20079676 :   if (wrto_loop->num != use_loop->num)
    3380              :     return true;
    3381              : 
    3382     19908665 :   if (!CONVERT_EXPR_P (iv->base) || TREE_CODE (iv->step) != INTEGER_CST)
    3383              :     return true;
    3384              : 
    3385       229855 :   type = TREE_TYPE (iv->base);
    3386       229855 :   e = TREE_OPERAND (iv->base, 0);
    3387       229855 :   if (!tree_nop_conversion_p (type, TREE_TYPE (e))
    3388       197872 :       || TREE_CODE (e) != PLUS_EXPR
    3389       109321 :       || TREE_CODE (TREE_OPERAND (e, 1)) != INTEGER_CST
    3390       312250 :       || !tree_int_cst_equal (iv->step,
    3391        82395 :                               fold_convert (type, TREE_OPERAND (e, 1))))
    3392       166426 :     return true;
    3393        63429 :   e = TREE_OPERAND (e, 0);
    3394        63429 :   if (!CONVERT_EXPR_P (e))
    3395              :     return true;
    3396        35227 :   base = TREE_OPERAND (e, 0);
    3397        35227 :   if (!useless_type_conversion_p (type, TREE_TYPE (base)))
    3398              :     return true;
    3399              : 
    3400        26865 :   if (tree_int_cst_sign_bit (iv->step))
    3401              :     {
    3402         9121 :       code = LT_EXPR;
    3403         9121 :       extreme = wi::min_value (type);
    3404              :     }
    3405              :   else
    3406              :     {
    3407        17744 :       code = GT_EXPR;
    3408        17744 :       extreme = wi::max_value (type);
    3409              :     }
    3410        26865 :   wi::overflow_type overflow = wi::OVF_NONE;
    3411        26865 :   extreme = wi::sub (extreme, wi::to_wide (iv->step),
    3412        53730 :                      TYPE_SIGN (type), &overflow);
    3413        26865 :   if (overflow)
    3414              :     return true;
    3415        26841 :   e = fold_build2 (code, boolean_type_node, base,
    3416              :                    wide_int_to_tree (type, extreme));
    3417        26841 :   e = simplify_using_initial_conditions (use_loop, e);
    3418        26841 :   if (!integer_zerop (e))
    3419              :     return true;
    3420              : 
    3421        12989 :   if (POINTER_TYPE_P (TREE_TYPE (base)))
    3422              :     code = POINTER_PLUS_EXPR;
    3423              :   else
    3424              :     code = PLUS_EXPR;
    3425              : 
    3426        12989 :   iv->base = fold_build2 (code, TREE_TYPE (base), base, iv->step);
    3427        12989 :   return true;
    3428     51403057 : }
    3429              : 
    3430              : /* Like simple_iv_with_niters, but return TRUE when OP behaves as a simple
    3431              :    affine iv unconditionally.  */
    3432              : 
    3433              : bool
    3434     21120398 : simple_iv (class loop *wrto_loop, class loop *use_loop, tree op,
    3435              :            affine_iv *iv, bool allow_nonconstant_step)
    3436              : {
    3437     21120398 :   return simple_iv_with_niters (wrto_loop, use_loop, op, iv,
    3438     21120398 :                                 NULL, allow_nonconstant_step);
    3439              : }
    3440              : 
    3441              : /* Finalize the scalar evolution analysis.  */
    3442              : 
    3443              : void
    3444     14842677 : scev_finalize (void)
    3445              : {
    3446     14842677 :   if (!scalar_evolution_info)
    3447              :     return;
    3448     14842676 :   scalar_evolution_info->empty ();
    3449     14842676 :   scalar_evolution_info = NULL;
    3450     14842676 :   free_numbers_of_iterations_estimates (cfun);
    3451              : }
    3452              : 
    3453              : /* Returns true if the expression EXPR is considered to be too expensive
    3454              :    for scev_const_prop.  Sets *COND_OVERFLOW_P to true when the
    3455              :    expression might contain a sub-expression that is subject to undefined
    3456              :    overflow behavior and conditionally evaluated.  */
    3457              : 
    3458              : static bool
    3459     10782927 : expression_expensive_p (tree expr, bool *cond_overflow_p,
    3460              :                         hash_map<tree, uint64_t> &cache, uint64_t &cost)
    3461              : {
    3462     10782927 :   enum tree_code code;
    3463              : 
    3464     10782927 :   if (is_gimple_val (expr))
    3465              :     return false;
    3466              : 
    3467      4572449 :   code = TREE_CODE (expr);
    3468      4572449 :   if (code == TRUNC_DIV_EXPR
    3469              :       || code == CEIL_DIV_EXPR
    3470              :       || code == FLOOR_DIV_EXPR
    3471              :       || code == ROUND_DIV_EXPR
    3472              :       || code == TRUNC_MOD_EXPR
    3473              :       || code == CEIL_MOD_EXPR
    3474              :       || code == FLOOR_MOD_EXPR
    3475      4572449 :       || code == ROUND_MOD_EXPR
    3476      4572449 :       || code == EXACT_DIV_EXPR)
    3477              :     {
    3478              :       /* Division by power of two is usually cheap, so we allow it.
    3479              :          Forbid anything else.  */
    3480        65776 :       if (!integer_pow2p (TREE_OPERAND (expr, 1)))
    3481              :         return true;
    3482              :     }
    3483              : 
    3484      4562723 :   bool visited_p;
    3485      4562723 :   uint64_t &local_cost = cache.get_or_insert (expr, &visited_p);
    3486      4562723 :   if (visited_p)
    3487              :     {
    3488          341 :       uint64_t tem = cost + local_cost;
    3489          341 :       if (tem < cost)
    3490              :         return true;
    3491          341 :       cost = tem;
    3492          341 :       return false;
    3493              :     }
    3494      4562382 :   local_cost = 1;
    3495              : 
    3496      4562382 :   uint64_t op_cost = 0;
    3497      4562382 :   if (code == CALL_EXPR)
    3498              :     {
    3499          140 :       tree arg;
    3500          140 :       call_expr_arg_iterator iter;
    3501              :       /* Even though is_inexpensive_builtin might say true, we will get a
    3502              :          library call for popcount when backend does not have an instruction
    3503              :          to do so.  We consider this to be expensive and generate
    3504              :          __builtin_popcount only when backend defines it.  */
    3505          140 :       optab optab;
    3506          140 :       combined_fn cfn = get_call_combined_fn (expr);
    3507          140 :       switch (cfn)
    3508              :         {
    3509           31 :         CASE_CFN_POPCOUNT:
    3510           31 :           optab = popcount_optab;
    3511           31 :           goto bitcount_call;
    3512           85 :         CASE_CFN_CLZ:
    3513           85 :           optab = clz_optab;
    3514           85 :           goto bitcount_call;
    3515              :         CASE_CFN_CTZ:
    3516              :           optab = ctz_optab;
    3517          140 : bitcount_call:
    3518              :           /* Check if opcode for popcount is available in the mode required.  */
    3519          140 :           if (optab_handler (optab,
    3520          140 :                              TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (expr, 0))))
    3521              :               == CODE_FOR_nothing)
    3522              :             {
    3523           27 :               machine_mode mode;
    3524           27 :               mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (expr, 0)));
    3525           27 :               scalar_int_mode int_mode;
    3526              : 
    3527              :               /* If the mode is of 2 * UNITS_PER_WORD size, we can handle
    3528              :                  double-word popcount by emitting two single-word popcount
    3529              :                  instructions.  */
    3530           27 :               if (is_a <scalar_int_mode> (mode, &int_mode)
    3531           29 :                   && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
    3532            2 :                   && (optab_handler (optab, word_mode)
    3533              :                       != CODE_FOR_nothing))
    3534              :                   break;
    3535              :               /* If popcount is available for a wider mode, we emulate the
    3536              :                  operation for a narrow mode by first zero-extending the value
    3537              :                  and then computing popcount in the wider mode.  Analogue for
    3538              :                  ctz.  For clz we do the same except that we additionally have
    3539              :                  to subtract the difference of the mode precisions from the
    3540              :                  result.  */
    3541           25 :               if (is_a <scalar_int_mode> (mode, &int_mode))
    3542              :                 {
    3543           25 :                   machine_mode wider_mode_iter;
    3544          124 :                   FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
    3545           99 :                     if (optab_handler (optab, wider_mode_iter)
    3546              :                         != CODE_FOR_nothing)
    3547            0 :                       goto check_call_args;
    3548              :                   /* Operation ctz may be emulated via clz in expand_ctz.  */
    3549           25 :                   if (optab == ctz_optab)
    3550              :                     {
    3551            0 :                       FOR_EACH_WIDER_MODE_FROM (wider_mode_iter, mode)
    3552            0 :                         if (optab_handler (clz_optab, wider_mode_iter)
    3553              :                             != CODE_FOR_nothing)
    3554            0 :                           goto check_call_args;
    3555              :                     }
    3556              :                 }
    3557           25 :               return true;
    3558              :             }
    3559              :           break;
    3560              : 
    3561            0 :         default:
    3562            0 :           if (cfn == CFN_LAST
    3563            0 :               || !is_inexpensive_builtin (get_callee_fndecl (expr)))
    3564            0 :             return true;
    3565              :           break;
    3566              :         }
    3567              : 
    3568          115 : check_call_args:
    3569          345 :       FOR_EACH_CALL_EXPR_ARG (arg, iter, expr)
    3570          115 :         if (expression_expensive_p (arg, cond_overflow_p, cache, op_cost))
    3571              :           return true;
    3572          115 :       *cache.get (expr) += op_cost;
    3573          115 :       cost += op_cost + 1;
    3574          115 :       return false;
    3575              :     }
    3576              : 
    3577      4562242 :   if (code == COND_EXPR)
    3578              :     {
    3579         1899 :       if (expression_expensive_p (TREE_OPERAND (expr, 0), cond_overflow_p,
    3580              :                                   cache, op_cost)
    3581         1899 :           || (EXPR_P (TREE_OPERAND (expr, 1))
    3582         1898 :               && EXPR_P (TREE_OPERAND (expr, 2)))
    3583              :           /* If either branch has side effects or could trap.  */
    3584         1891 :           || TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
    3585         1891 :           || generic_expr_could_trap_p (TREE_OPERAND (expr, 1))
    3586         1890 :           || TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0))
    3587         1890 :           || generic_expr_could_trap_p (TREE_OPERAND (expr, 0))
    3588         1890 :           || expression_expensive_p (TREE_OPERAND (expr, 1), cond_overflow_p,
    3589              :                                      cache, op_cost)
    3590         3697 :           || expression_expensive_p (TREE_OPERAND (expr, 2), cond_overflow_p,
    3591              :                                      cache, op_cost))
    3592          101 :         return true;
    3593              :       /* Conservatively assume there's overflow for now.  */
    3594         1798 :       *cond_overflow_p = true;
    3595         1798 :       *cache.get (expr) += op_cost;
    3596         1798 :       cost += op_cost + 1;
    3597         1798 :       return false;
    3598              :     }
    3599              : 
    3600      4560343 :   switch (TREE_CODE_CLASS (code))
    3601              :     {
    3602      2378051 :     case tcc_binary:
    3603      2378051 :     case tcc_comparison:
    3604      2378051 :       if (expression_expensive_p (TREE_OPERAND (expr, 1), cond_overflow_p,
    3605              :                                   cache, op_cost))
    3606              :         return true;
    3607              : 
    3608              :       /* Fallthru.  */
    3609      4557602 :     case tcc_unary:
    3610      4557602 :       if (expression_expensive_p (TREE_OPERAND (expr, 0), cond_overflow_p,
    3611              :                                   cache, op_cost))
    3612              :         return true;
    3613      4533787 :       *cache.get (expr) += op_cost;
    3614      4533787 :       cost += op_cost + 1;
    3615      4533787 :       return false;
    3616              : 
    3617              :     default:
    3618              :       return true;
    3619              :     }
    3620              : }
    3621              : 
    3622              : bool
    3623      3841572 : expression_expensive_p (tree expr, bool *cond_overflow_p)
    3624              : {
    3625      3841572 :   hash_map<tree, uint64_t> cache;
    3626      3841572 :   uint64_t expanded_size = 0;
    3627      3841572 :   *cond_overflow_p = false;
    3628      3841572 :   return (expression_expensive_p (expr, cond_overflow_p, cache, expanded_size)
    3629              :           /* ???  Both the explicit unsharing and gimplification of expr will
    3630              :              expand shared trees to multiple copies.
    3631              :              Guard against exponential growth by counting the visits and
    3632              :              comparing againt the number of original nodes.  Allow a tiny
    3633              :              bit of duplication to catch some additional optimizations.  */
    3634      3851468 :           || expanded_size > (cache.elements () + 1));
    3635      3841572 : }
    3636              : 
    3637              : /* Match.pd function to match bitwise inductive expression.
    3638              :    .i.e.
    3639              :    _2 = 1 << _1;
    3640              :    _3 = ~_2;
    3641              :    tmp_9 = _3 & tmp_12;  */
    3642              : extern bool gimple_bitwise_induction_p (tree, tree *, tree (*)(tree));
    3643              : 
    3644              : /* Return the inductive expression of bitwise operation if possible,
    3645              :    otherwise returns DEF.  */
    3646              : static tree
    3647        20529 : analyze_and_compute_bitwise_induction_effect (class loop* loop,
    3648              :                                               tree phidef,
    3649              :                                               unsigned HOST_WIDE_INT niter)
    3650              : {
    3651        20529 :   tree match_op[3],inv, bitwise_scev;
    3652        20529 :   tree type = TREE_TYPE (phidef);
    3653        20529 :   gphi* header_phi = NULL;
    3654              : 
    3655              :   /* Match things like op2(MATCH_OP[2]), op1(MATCH_OP[1]), phidef(PHIDEF)
    3656              : 
    3657              :      op2 = PHI <phidef, inv>
    3658              :      _1 = (int) bit_17;
    3659              :      _3 = 1 << _1;
    3660              :      op1 = ~_3;
    3661              :      phidef = op1 & op2;  */
    3662        20529 :   if (!gimple_bitwise_induction_p (phidef, &match_op[0], NULL)
    3663          102 :       || TREE_CODE (match_op[2]) != SSA_NAME
    3664        20529 :       || !(header_phi = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (match_op[2])))
    3665          102 :       || gimple_bb (header_phi) != loop->header
    3666        20629 :       || gimple_phi_num_args (header_phi) != 2)
    3667              :     return NULL_TREE;
    3668              : 
    3669          100 :   if (PHI_ARG_DEF_FROM_EDGE (header_phi, loop_latch_edge (loop)) != phidef)
    3670              :     return NULL_TREE;
    3671              : 
    3672          100 :   bitwise_scev = analyze_scalar_evolution (loop, match_op[1]);
    3673          100 :   bitwise_scev = instantiate_parameters (loop, bitwise_scev);
    3674              : 
    3675              :   /* Make sure bits is in range of type precision.  */
    3676          100 :   if (TREE_CODE (bitwise_scev) != POLYNOMIAL_CHREC
    3677          100 :       || !INTEGRAL_TYPE_P (TREE_TYPE (bitwise_scev))
    3678          100 :       || !tree_fits_uhwi_p (CHREC_LEFT (bitwise_scev))
    3679          100 :       || tree_to_uhwi (CHREC_LEFT (bitwise_scev)) >= TYPE_PRECISION (type)
    3680          200 :       || !tree_fits_shwi_p (CHREC_RIGHT (bitwise_scev)))
    3681              :     return NULL_TREE;
    3682              : 
    3683          100 : enum bit_op_kind
    3684              :   {
    3685              :    INDUCTION_BIT_CLEAR,
    3686              :    INDUCTION_BIT_IOR,
    3687              :    INDUCTION_BIT_XOR,
    3688              :    INDUCTION_BIT_RESET,
    3689              :    INDUCTION_ZERO,
    3690              :    INDUCTION_ALL
    3691              :   };
    3692              : 
    3693          100 :   enum bit_op_kind induction_kind;
    3694          100 :   enum tree_code code1
    3695          100 :     = gimple_assign_rhs_code (SSA_NAME_DEF_STMT (phidef));
    3696          100 :   enum tree_code code2
    3697          100 :     = gimple_assign_rhs_code (SSA_NAME_DEF_STMT (match_op[0]));
    3698              : 
    3699              :   /* BIT_CLEAR: A &= ~(1 << bit)
    3700              :      BIT_RESET: A ^= (1 << bit).
    3701              :      BIT_IOR: A |= (1 << bit)
    3702              :      BIT_ZERO: A &= (1 << bit)
    3703              :      BIT_ALL: A |= ~(1 << bit)
    3704              :      BIT_XOR: A ^= ~(1 << bit).
    3705              :      bit is induction variable.  */
    3706          100 :   switch (code1)
    3707              :     {
    3708           27 :     case BIT_AND_EXPR:
    3709           27 :       induction_kind = code2 == BIT_NOT_EXPR
    3710           27 :         ? INDUCTION_BIT_CLEAR
    3711              :         : INDUCTION_ZERO;
    3712              :       break;
    3713           49 :     case BIT_IOR_EXPR:
    3714           49 :       induction_kind = code2 == BIT_NOT_EXPR
    3715           49 :         ? INDUCTION_ALL
    3716              :         : INDUCTION_BIT_IOR;
    3717              :       break;
    3718           12 :     case BIT_XOR_EXPR:
    3719           12 :       induction_kind = code2 == BIT_NOT_EXPR
    3720           12 :         ? INDUCTION_BIT_XOR
    3721              :         : INDUCTION_BIT_RESET;
    3722              :       break;
    3723              :       /* A ^ ~(1 << bit) is equal to ~(A ^ (1 << bit)).  */
    3724           12 :     case BIT_NOT_EXPR:
    3725           12 :       gcc_assert (code2 == BIT_XOR_EXPR);
    3726              :       induction_kind = INDUCTION_BIT_XOR;
    3727              :       break;
    3728            0 :     default:
    3729            0 :       gcc_unreachable ();
    3730              :     }
    3731              : 
    3732           37 :   if (induction_kind == INDUCTION_ZERO)
    3733           12 :     return build_zero_cst (type);
    3734           88 :   if (induction_kind == INDUCTION_ALL)
    3735           12 :     return build_all_ones_cst (type);
    3736              : 
    3737          152 :   wide_int bits = wi::zero (TYPE_PRECISION (type));
    3738           76 :   HOST_WIDE_INT bit_start = tree_to_shwi (CHREC_LEFT (bitwise_scev));
    3739           76 :   HOST_WIDE_INT step = tree_to_shwi (CHREC_RIGHT (bitwise_scev));
    3740           76 :   HOST_WIDE_INT bit_final = bit_start + step * niter;
    3741              : 
    3742              :   /* bit_start, bit_final in range of [0,TYPE_PRECISION)
    3743              :      implies all bits are set in range.  */
    3744           76 :   if (bit_final >= TYPE_PRECISION (type)
    3745           76 :       || bit_final < 0)
    3746              :     return NULL_TREE;
    3747              : 
    3748              :   /* Loop tripcount should be niter + 1.  */
    3749         1296 :   for (unsigned i = 0; i != niter + 1; i++)
    3750              :     {
    3751         1220 :       bits = wi::set_bit (bits, bit_start);
    3752         1220 :       bit_start += step;
    3753              :     }
    3754              : 
    3755           76 :   bool inverted = false;
    3756           76 :   switch (induction_kind)
    3757              :     {
    3758              :     case INDUCTION_BIT_CLEAR:
    3759              :       code1 = BIT_AND_EXPR;
    3760              :       inverted = true;
    3761              :       break;
    3762              :     case INDUCTION_BIT_IOR:
    3763              :       code1 = BIT_IOR_EXPR;
    3764              :       break;
    3765              :     case INDUCTION_BIT_RESET:
    3766              :       code1 = BIT_XOR_EXPR;
    3767              :       break;
    3768              :     /* A ^= ~(1 << bit) is special, when loop tripcount is even,
    3769              :        it's equal to  A ^= bits, else A ^= ~bits.  */
    3770           12 :     case INDUCTION_BIT_XOR:
    3771           12 :       code1 = BIT_XOR_EXPR;
    3772           12 :       if (niter % 2 == 0)
    3773              :         inverted = true;
    3774              :       break;
    3775              :     default:
    3776              :       gcc_unreachable ();
    3777              :     }
    3778              : 
    3779              :   if (inverted)
    3780           19 :     bits = wi::bit_not (bits);
    3781              : 
    3782           76 :   inv = PHI_ARG_DEF_FROM_EDGE (header_phi, loop_preheader_edge (loop));
    3783           76 :   return fold_build2 (code1, type, inv, wide_int_to_tree (type, bits));
    3784              : }
    3785              : 
    3786              : /* Match.pd function to match bitop with invariant expression
    3787              :   .i.e.
    3788              :   tmp_7 = _0 & _1; */
    3789              : extern bool gimple_bitop_with_inv_p (tree, tree *, tree (*)(tree));
    3790              : 
    3791              : /* Return the inductive expression of bitop with invariant if possible,
    3792              :    otherwise returns DEF.  */
    3793              : static tree
    3794        74925 : analyze_and_compute_bitop_with_inv_effect (class loop* loop, tree phidef,
    3795              :                                            tree niter)
    3796              : {
    3797        74925 :   tree match_op[2],inv;
    3798        74925 :   tree type = TREE_TYPE (phidef);
    3799        74925 :   gphi* header_phi = NULL;
    3800        74925 :   enum tree_code code;
    3801              :   /* match thing like op0 (match[0]), op1 (match[1]), phidef (PHIDEF)
    3802              : 
    3803              :     op1 =  PHI <phidef, inv>
    3804              :     phidef = op0 & op1
    3805              :     if op0 is an invariant, it could change to
    3806              :     phidef = op0 & inv.  */
    3807        74925 :   gimple *def;
    3808        74925 :   def = SSA_NAME_DEF_STMT (phidef);
    3809        74925 :   if (!(is_gimple_assign (def)
    3810        27586 :       && ((code = gimple_assign_rhs_code (def)), true)
    3811        27586 :       && (code == BIT_AND_EXPR || code == BIT_IOR_EXPR
    3812        21893 :           || code == BIT_XOR_EXPR)))
    3813              :     return NULL_TREE;
    3814              : 
    3815         6397 :   match_op[0] = gimple_assign_rhs1 (def);
    3816         6397 :   match_op[1] = gimple_assign_rhs2 (def);
    3817              : 
    3818         6397 :   if (expr_invariant_in_loop_p (loop, match_op[1]))
    3819          221 :     std::swap (match_op[0], match_op[1]);
    3820              : 
    3821         6397 :   if (TREE_CODE (match_op[1]) != SSA_NAME
    3822         6397 :       || !expr_invariant_in_loop_p (loop, match_op[0])
    3823          315 :       || !(header_phi = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (match_op[1])))
    3824          212 :       || gimple_bb (header_phi) != loop->header
    3825         6599 :       || gimple_phi_num_args (header_phi) != 2)
    3826         6195 :     return NULL_TREE;
    3827              : 
    3828          202 :   if (PHI_ARG_DEF_FROM_EDGE (header_phi, loop_latch_edge (loop)) != phidef)
    3829              :     return NULL_TREE;
    3830              : 
    3831          197 :   enum tree_code code1
    3832          197 :     = gimple_assign_rhs_code (def);
    3833              : 
    3834          197 :   if (code1 == BIT_XOR_EXPR)
    3835              :     {
    3836           51 :        if (!tree_fits_uhwi_p (niter))
    3837              :         return NULL_TREE;
    3838           19 :        unsigned HOST_WIDE_INT niter_num;
    3839           19 :        niter_num = tree_to_uhwi (niter);
    3840           19 :        if (niter_num % 2 != 0)
    3841           10 :         match_op[0] =  build_zero_cst (type);
    3842              :     }
    3843              : 
    3844          165 :   inv = PHI_ARG_DEF_FROM_EDGE (header_phi, loop_preheader_edge (loop));
    3845          165 :   return fold_build2 (code1, type, inv, match_op[0]);
    3846              : }
    3847              : 
    3848              : /* Do final value replacement for LOOP, return true if we did anything.  */
    3849              : 
    3850              : bool
    3851       686020 : final_value_replacement_loop (class loop *loop)
    3852              : {
    3853              :   /* If we do not know exact number of iterations of the loop, we cannot
    3854              :      replace the final value.  */
    3855       686020 :   edge exit = single_exit (loop);
    3856       686020 :   if (!exit)
    3857              :     return false;
    3858              : 
    3859       458710 :   tree niter = number_of_latch_executions (loop);
    3860       458710 :   if (niter == chrec_dont_know)
    3861              :     return false;
    3862              : 
    3863              :   /* Ensure that it is possible to insert new statements somewhere.  */
    3864       344367 :   if (!single_pred_p (exit->dest))
    3865        39251 :     split_loop_exit_edge (exit);
    3866              : 
    3867              :   /* Set stmt insertion pointer.  All stmts are inserted before this point.  */
    3868              : 
    3869       344367 :   class loop *ex_loop
    3870       688734 :     = superloop_at_depth (loop,
    3871       421494 :                           loop_depth (exit->dest->loop_father) + 1);
    3872              : 
    3873       344367 :   bool any = false;
    3874       344367 :   gphi_iterator psi;
    3875       770893 :   for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); )
    3876              :     {
    3877       426526 :       gphi *phi = psi.phi ();
    3878       426526 :       tree rslt = PHI_RESULT (phi);
    3879       426526 :       tree phidef = PHI_ARG_DEF_FROM_EDGE (phi, exit);
    3880       426526 :       tree def = phidef;
    3881       852377 :       if (virtual_operand_p (def))
    3882              :         {
    3883       242036 :           gsi_next (&psi);
    3884       634073 :           continue;
    3885              :         }
    3886              : 
    3887       348278 :       if (!POINTER_TYPE_P (TREE_TYPE (def))
    3888       348167 :           && !INTEGRAL_TYPE_P (TREE_TYPE (def)))
    3889              :         {
    3890        73738 :           gsi_next (&psi);
    3891        73738 :           continue;
    3892              :         }
    3893              : 
    3894       110752 :       bool folded_casts;
    3895       110752 :       def = analyze_scalar_evolution_in_loop (ex_loop, loop, def,
    3896              :                                               &folded_casts);
    3897              : 
    3898       110752 :       tree bitinv_def, bit_def;
    3899       110752 :       unsigned HOST_WIDE_INT niter_num;
    3900              : 
    3901       110752 :       if (def != chrec_dont_know)
    3902        35827 :         def = compute_overall_effect_of_inner_loop (ex_loop, def);
    3903              : 
    3904              :       /* Handle bitop with invariant induction expression.
    3905              : 
    3906              :         .i.e
    3907              :         for (int i =0 ;i < 32; i++)
    3908              :           tmp &= bit2;
    3909              :         if bit2 is an invariant in loop which could simple to
    3910              :         tmp &= bit2.  */
    3911       149850 :       else if ((bitinv_def
    3912        74925 :                 = analyze_and_compute_bitop_with_inv_effect (loop,
    3913              :                                                              phidef, niter)))
    3914              :         def = bitinv_def;
    3915              : 
    3916              :       /* Handle bitwise induction expression.
    3917              : 
    3918              :          .i.e.
    3919              :          for (int i = 0; i != 64; i+=3)
    3920              :            res &= ~(1UL << i);
    3921              : 
    3922              :          RES can't be analyzed out by SCEV because it is not polynomially
    3923              :          expressible, but in fact final value of RES can be replaced by
    3924              :          RES & CONSTANT where CONSTANT all ones with bit {0,3,6,9,... ,63}
    3925              :          being cleared, similar for BIT_IOR_EXPR/BIT_XOR_EXPR.  */
    3926        74760 :       else if (tree_fits_uhwi_p (niter)
    3927        31058 :                && (niter_num = tree_to_uhwi (niter)) != 0
    3928        31039 :                && niter_num < TYPE_PRECISION (TREE_TYPE (phidef))
    3929        74760 :                && (bit_def
    3930        20529 :                    = analyze_and_compute_bitwise_induction_effect (loop,
    3931              :                                                                    phidef,
    3932              :                                                                    niter_num)))
    3933              :         def = bit_def;
    3934              : 
    3935       110752 :       bool cond_overflow_p;
    3936       110752 :       if (!tree_does_not_contain_chrecs (def)
    3937        35510 :           || chrec_contains_symbols_defined_in_loop (def, ex_loop->num)
    3938              :           /* Moving the computation from the loop may prolong life range
    3939              :              of some ssa names, which may cause problems if they appear
    3940              :              on abnormal edges.  */
    3941        35510 :           || contains_abnormal_ssa_name_p (def)
    3942              :           /* Do not emit expensive expressions.  The rationale is that
    3943              :              when someone writes a code like
    3944              : 
    3945              :              while (n > 45) n -= 45;
    3946              : 
    3947              :              he probably knows that n is not large, and does not want it
    3948              :              to be turned into n %= 45.  */
    3949       146262 :           || expression_expensive_p (def, &cond_overflow_p))
    3950              :         {
    3951        76263 :           if (dump_file && (dump_flags & TDF_DETAILS))
    3952              :             {
    3953           56 :               fprintf (dump_file, "not replacing:\n  ");
    3954           56 :               print_gimple_stmt (dump_file, phi, 0);
    3955           56 :               fprintf (dump_file, "\n");
    3956              :             }
    3957        76263 :           gsi_next (&psi);
    3958        76263 :           continue;
    3959              :         }
    3960              : 
    3961              :       /* Eliminate the PHI node and replace it by a computation outside
    3962              :          the loop.  */
    3963        34489 :       if (dump_file)
    3964              :         {
    3965          133 :           fprintf (dump_file, "\nfinal value replacement:\n  ");
    3966          133 :           print_gimple_stmt (dump_file, phi, 0);
    3967          133 :           fprintf (dump_file, " with expr: ");
    3968          133 :           print_generic_expr (dump_file, def);
    3969          133 :           fprintf (dump_file, "\n");
    3970              :         }
    3971        34489 :       any = true;
    3972              :       /* ???  Here we'd like to have a unshare_expr that would assign
    3973              :          shared sub-trees to new temporary variables either gimplified
    3974              :          to a GIMPLE sequence or to a statement list (keeping this a
    3975              :          GENERIC interface).  */
    3976        34489 :       def = unshare_expr (def);
    3977        34489 :       auto loc = gimple_phi_arg_location (phi, exit->dest_idx);
    3978              : 
    3979              :       /* Create the replacement statements.  */
    3980        34489 :       gimple_seq stmts;
    3981        34489 :       def = force_gimple_operand (def, &stmts, false, NULL_TREE);
    3982              : 
    3983              :       /* Propagate constants immediately, but leave an unused initialization
    3984              :          around to avoid invalidating the SCEV cache.  */
    3985        41246 :       if (CONSTANT_CLASS_P (def) && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rslt))
    3986         6756 :         replace_uses_by (rslt, def);
    3987              : 
    3988              :       /* Remove the old phi after the gimplification to make sure the
    3989              :          SSA name is defined by a statement so that fold_stmt during
    3990              :          the gimplification does not crash. */
    3991        34489 :       remove_phi_node (&psi, false);
    3992        34489 :       gassign *ass = gimple_build_assign (rslt, def);
    3993        34489 :       gimple_set_location (ass, loc);
    3994        34489 :       gimple_seq_add_stmt (&stmts, ass);
    3995              : 
    3996              :       /* If def's type has undefined overflow and there were folded
    3997              :          casts, rewrite all stmts added for def into arithmetics
    3998              :          with defined overflow behavior.  */
    3999        34489 :       if ((folded_casts
    4000          425 :            && ANY_INTEGRAL_TYPE_P (TREE_TYPE (def))
    4001          718 :            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (def)))
    4002        34914 :           || cond_overflow_p)
    4003              :         {
    4004         2058 :           gimple_stmt_iterator gsi2;
    4005         2058 :           gsi2 = gsi_start (stmts);
    4006        18804 :           while (!gsi_end_p (gsi2))
    4007              :             {
    4008        16746 :               if (gimple_needing_rewrite_undefined (gsi_stmt (gsi2)))
    4009          404 :                 rewrite_to_defined_unconditional (&gsi2);
    4010        16746 :               gsi_next (&gsi2);
    4011              :             }
    4012              :         }
    4013        34489 :       gimple_stmt_iterator gsi = gsi_after_labels (exit->dest);
    4014        34489 :       gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
    4015        34489 :       if (dump_file)
    4016              :         {
    4017          133 :           fprintf (dump_file, " final stmt:\n  ");
    4018          133 :           print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (rslt), 0);
    4019          133 :           fprintf (dump_file, "\n");
    4020              :         }
    4021              : 
    4022              :       /* Re-fold immediate uses of the replaced def, but avoid
    4023              :          CFG manipulations from this function.  For now only do
    4024              :          a single-level re-folding, not re-folding uses of
    4025              :          folded uses.  */
    4026        34489 :       if (! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rslt))
    4027              :         {
    4028        34488 :           gimple *use_stmt;
    4029        34488 :           imm_use_iterator imm_iter;
    4030        34488 :           auto_vec<gimple *, 4> to_fold;
    4031        68975 :           FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, rslt)
    4032        34487 :             if (!stmt_can_throw_internal (cfun, use_stmt))
    4033        34485 :               to_fold.safe_push (use_stmt);
    4034              :           /* Delay folding until after the immediate use walk is completed
    4035              :              as we have an active ranger and that might walk immediate
    4036              :              uses of rslt again.  See PR122502.  */
    4037       137949 :           for (gimple *use_stmt : to_fold)
    4038              :             {
    4039        34485 :               gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
    4040        34485 :               if (fold_stmt (&gsi, follow_all_ssa_edges))
    4041         1970 :                 update_stmt (gsi_stmt (gsi));
    4042              :             }
    4043        34488 :         }
    4044              :     }
    4045              : 
    4046              :   return any;
    4047              : }
    4048              : 
    4049              : #include "gt-tree-scalar-evolution.h"
        

Generated by: LCOV version 2.4-beta

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