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