Home | History | Annotate | Line # | Download | only in gcc
      1  1.1  mrg /* Chains of recurrences.
      2  1.1  mrg    Copyright (C) 2003-2022 Free Software Foundation, Inc.
      3  1.1  mrg    Contributed by Sebastian Pop <pop (at) cri.ensmp.fr>
      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 /* This file implements operations on chains of recurrences.  Chains
     22  1.1  mrg    of recurrences are used for modeling evolution functions of scalar
     23  1.1  mrg    variables.
     24  1.1  mrg */
     25  1.1  mrg 
     26  1.1  mrg #include "config.h"
     27  1.1  mrg #include "system.h"
     28  1.1  mrg #include "coretypes.h"
     29  1.1  mrg #include "backend.h"
     30  1.1  mrg #include "tree.h"
     31  1.1  mrg #include "gimple-expr.h"
     32  1.1  mrg #include "tree-pretty-print.h"
     33  1.1  mrg #include "fold-const.h"
     34  1.1  mrg #include "cfgloop.h"
     35  1.1  mrg #include "tree-ssa-loop-ivopts.h"
     36  1.1  mrg #include "tree-ssa-loop-niter.h"
     37  1.1  mrg #include "tree-chrec.h"
     38  1.1  mrg #include "gimple.h"
     39  1.1  mrg #include "tree-ssa-loop.h"
     40  1.1  mrg #include "dumpfile.h"
     41  1.1  mrg #include "tree-scalar-evolution.h"
     42  1.1  mrg 
     43  1.1  mrg /* Extended folder for chrecs.  */
     44  1.1  mrg 
     45  1.1  mrg /* Fold the addition of two polynomial functions.  */
     46  1.1  mrg 
     47  1.1  mrg static inline tree
     48  1.1  mrg chrec_fold_plus_poly_poly (enum tree_code code,
     49  1.1  mrg 			   tree type,
     50  1.1  mrg 			   tree poly0,
     51  1.1  mrg 			   tree poly1)
     52  1.1  mrg {
     53  1.1  mrg   tree left, right;
     54  1.1  mrg   class loop *loop0 = get_chrec_loop (poly0);
     55  1.1  mrg   class loop *loop1 = get_chrec_loop (poly1);
     56  1.1  mrg   tree rtype = code == POINTER_PLUS_EXPR ? chrec_type (poly1) : type;
     57  1.1  mrg 
     58  1.1  mrg   gcc_assert (poly0);
     59  1.1  mrg   gcc_assert (poly1);
     60  1.1  mrg   gcc_assert (TREE_CODE (poly0) == POLYNOMIAL_CHREC);
     61  1.1  mrg   gcc_assert (TREE_CODE (poly1) == POLYNOMIAL_CHREC);
     62  1.1  mrg   if (POINTER_TYPE_P (chrec_type (poly0)))
     63  1.1  mrg     gcc_checking_assert (ptrofftype_p (chrec_type (poly1))
     64  1.1  mrg 			 && useless_type_conversion_p (type, chrec_type (poly0)));
     65  1.1  mrg   else
     66  1.1  mrg     gcc_checking_assert (useless_type_conversion_p (type, chrec_type (poly0))
     67  1.1  mrg 			 && useless_type_conversion_p (type, chrec_type (poly1)));
     68  1.1  mrg 
     69  1.1  mrg   /*
     70  1.1  mrg     {a, +, b}_1 + {c, +, d}_2  ->  {{a, +, b}_1 + c, +, d}_2,
     71  1.1  mrg     {a, +, b}_2 + {c, +, d}_1  ->  {{c, +, d}_1 + a, +, b}_2,
     72  1.1  mrg     {a, +, b}_x + {c, +, d}_x  ->  {a+c, +, b+d}_x.  */
     73  1.1  mrg   if (flow_loop_nested_p (loop0, loop1))
     74  1.1  mrg     {
     75  1.1  mrg       if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
     76  1.1  mrg 	return build_polynomial_chrec
     77  1.1  mrg 	  (CHREC_VARIABLE (poly1),
     78  1.1  mrg 	   chrec_fold_plus (type, poly0, CHREC_LEFT (poly1)),
     79  1.1  mrg 	   CHREC_RIGHT (poly1));
     80  1.1  mrg       else
     81  1.1  mrg 	return build_polynomial_chrec
     82  1.1  mrg 	  (CHREC_VARIABLE (poly1),
     83  1.1  mrg 	   chrec_fold_minus (type, poly0, CHREC_LEFT (poly1)),
     84  1.1  mrg 	   chrec_fold_multiply (type, CHREC_RIGHT (poly1),
     85  1.1  mrg 				SCALAR_FLOAT_TYPE_P (type)
     86  1.1  mrg 				? build_real (type, dconstm1)
     87  1.1  mrg 				: build_int_cst_type (type, -1)));
     88  1.1  mrg     }
     89  1.1  mrg 
     90  1.1  mrg   if (flow_loop_nested_p (loop1, loop0))
     91  1.1  mrg     {
     92  1.1  mrg       if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
     93  1.1  mrg 	return build_polynomial_chrec
     94  1.1  mrg 	  (CHREC_VARIABLE (poly0),
     95  1.1  mrg 	   chrec_fold_plus (type, CHREC_LEFT (poly0), poly1),
     96  1.1  mrg 	   CHREC_RIGHT (poly0));
     97  1.1  mrg       else
     98  1.1  mrg 	return build_polynomial_chrec
     99  1.1  mrg 	  (CHREC_VARIABLE (poly0),
    100  1.1  mrg 	   chrec_fold_minus (type, CHREC_LEFT (poly0), poly1),
    101  1.1  mrg 	   CHREC_RIGHT (poly0));
    102  1.1  mrg     }
    103  1.1  mrg 
    104  1.1  mrg   /* This function should never be called for chrecs of loops that
    105  1.1  mrg      do not belong to the same loop nest.  */
    106  1.1  mrg   if (loop0 != loop1)
    107  1.1  mrg     {
    108  1.1  mrg       /* It still can happen if we are not in loop-closed SSA form.  */
    109  1.1  mrg       gcc_assert (! loops_state_satisfies_p (LOOP_CLOSED_SSA));
    110  1.1  mrg       return chrec_dont_know;
    111  1.1  mrg     }
    112  1.1  mrg 
    113  1.1  mrg   if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
    114  1.1  mrg     {
    115  1.1  mrg       left = chrec_fold_plus
    116  1.1  mrg 	(type, CHREC_LEFT (poly0), CHREC_LEFT (poly1));
    117  1.1  mrg       right = chrec_fold_plus
    118  1.1  mrg 	(rtype, CHREC_RIGHT (poly0), CHREC_RIGHT (poly1));
    119  1.1  mrg     }
    120  1.1  mrg   else
    121  1.1  mrg     {
    122  1.1  mrg       left = chrec_fold_minus
    123  1.1  mrg 	(type, CHREC_LEFT (poly0), CHREC_LEFT (poly1));
    124  1.1  mrg       right = chrec_fold_minus
    125  1.1  mrg 	(type, CHREC_RIGHT (poly0), CHREC_RIGHT (poly1));
    126  1.1  mrg     }
    127  1.1  mrg 
    128  1.1  mrg   if (chrec_zerop (right))
    129  1.1  mrg     return left;
    130  1.1  mrg   else
    131  1.1  mrg     return build_polynomial_chrec
    132  1.1  mrg       (CHREC_VARIABLE (poly0), left, right);
    133  1.1  mrg }
    134  1.1  mrg 
    135  1.1  mrg 
    136  1.1  mrg 
    138  1.1  mrg /* Fold the multiplication of two polynomial functions.  */
    139  1.1  mrg 
    140  1.1  mrg static inline tree
    141  1.1  mrg chrec_fold_multiply_poly_poly (tree type,
    142  1.1  mrg 			       tree poly0,
    143  1.1  mrg 			       tree poly1)
    144  1.1  mrg {
    145  1.1  mrg   tree t0, t1, t2;
    146  1.1  mrg   int var;
    147  1.1  mrg   class loop *loop0 = get_chrec_loop (poly0);
    148  1.1  mrg   class loop *loop1 = get_chrec_loop (poly1);
    149  1.1  mrg 
    150  1.1  mrg   gcc_assert (poly0);
    151  1.1  mrg   gcc_assert (poly1);
    152  1.1  mrg   gcc_assert (TREE_CODE (poly0) == POLYNOMIAL_CHREC);
    153  1.1  mrg   gcc_assert (TREE_CODE (poly1) == POLYNOMIAL_CHREC);
    154  1.1  mrg   gcc_checking_assert (useless_type_conversion_p (type, chrec_type (poly0))
    155  1.1  mrg 		       && useless_type_conversion_p (type, chrec_type (poly1)));
    156  1.1  mrg 
    157  1.1  mrg   /* {a, +, b}_1 * {c, +, d}_2  ->  {c*{a, +, b}_1, +, d}_2,
    158  1.1  mrg      {a, +, b}_2 * {c, +, d}_1  ->  {a*{c, +, d}_1, +, b}_2,
    159  1.1  mrg      {a, +, b}_x * {c, +, d}_x  ->  {a*c, +, a*d + b*c + b*d, +, 2*b*d}_x.  */
    160  1.1  mrg   if (flow_loop_nested_p (loop0, loop1))
    161  1.1  mrg     /* poly0 is a constant wrt. poly1.  */
    162  1.1  mrg     return build_polynomial_chrec
    163  1.1  mrg       (CHREC_VARIABLE (poly1),
    164  1.1  mrg        chrec_fold_multiply (type, CHREC_LEFT (poly1), poly0),
    165  1.1  mrg        CHREC_RIGHT (poly1));
    166  1.1  mrg 
    167  1.1  mrg   if (flow_loop_nested_p (loop1, loop0))
    168  1.1  mrg     /* poly1 is a constant wrt. poly0.  */
    169  1.1  mrg     return build_polynomial_chrec
    170  1.1  mrg       (CHREC_VARIABLE (poly0),
    171  1.1  mrg        chrec_fold_multiply (type, CHREC_LEFT (poly0), poly1),
    172  1.1  mrg        CHREC_RIGHT (poly0));
    173  1.1  mrg 
    174  1.1  mrg   if (loop0 != loop1)
    175  1.1  mrg     {
    176  1.1  mrg       /* It still can happen if we are not in loop-closed SSA form.  */
    177  1.1  mrg       gcc_assert (! loops_state_satisfies_p (LOOP_CLOSED_SSA));
    178  1.1  mrg       return chrec_dont_know;
    179  1.1  mrg     }
    180  1.1  mrg 
    181  1.1  mrg   /* poly0 and poly1 are two polynomials in the same variable,
    182  1.1  mrg      {a, +, b}_x * {c, +, d}_x  ->  {a*c, +, a*d + b*c + b*d, +, 2*b*d}_x.  */
    183  1.1  mrg 
    184  1.1  mrg   /* "a*c".  */
    185  1.1  mrg   t0 = chrec_fold_multiply (type, CHREC_LEFT (poly0), CHREC_LEFT (poly1));
    186  1.1  mrg 
    187  1.1  mrg   /* "a*d + b*c".  */
    188  1.1  mrg   t1 = chrec_fold_multiply (type, CHREC_LEFT (poly0), CHREC_RIGHT (poly1));
    189  1.1  mrg   t1 = chrec_fold_plus (type, t1, chrec_fold_multiply (type,
    190  1.1  mrg 						       CHREC_RIGHT (poly0),
    191  1.1  mrg 						       CHREC_LEFT (poly1)));
    192  1.1  mrg   /* "b*d".  */
    193  1.1  mrg   t2 = chrec_fold_multiply (type, CHREC_RIGHT (poly0), CHREC_RIGHT (poly1));
    194  1.1  mrg   /* "a*d + b*c + b*d".  */
    195  1.1  mrg   t1 = chrec_fold_plus (type, t1, t2);
    196  1.1  mrg   /* "2*b*d".  */
    197  1.1  mrg   t2 = chrec_fold_multiply (type, SCALAR_FLOAT_TYPE_P (type)
    198  1.1  mrg 			    ? build_real (type, dconst2)
    199  1.1  mrg 			    : build_int_cst (type, 2), t2);
    200  1.1  mrg 
    201  1.1  mrg   var = CHREC_VARIABLE (poly0);
    202  1.1  mrg   return build_polynomial_chrec (var, t0,
    203  1.1  mrg 				 build_polynomial_chrec (var, t1, t2));
    204  1.1  mrg }
    205  1.1  mrg 
    206  1.1  mrg /* When the operands are automatically_generated_chrec_p, the fold has
    207  1.1  mrg    to respect the semantics of the operands.  */
    208  1.1  mrg 
    209  1.1  mrg static inline tree
    210  1.1  mrg chrec_fold_automatically_generated_operands (tree op0,
    211  1.1  mrg 					     tree op1)
    212  1.1  mrg {
    213  1.1  mrg   if (op0 == chrec_dont_know
    214  1.1  mrg       || op1 == chrec_dont_know)
    215  1.1  mrg     return chrec_dont_know;
    216  1.1  mrg 
    217  1.1  mrg   if (op0 == chrec_known
    218  1.1  mrg       || op1 == chrec_known)
    219  1.1  mrg     return chrec_known;
    220  1.1  mrg 
    221  1.1  mrg   if (op0 == chrec_not_analyzed_yet
    222  1.1  mrg       || op1 == chrec_not_analyzed_yet)
    223  1.1  mrg     return chrec_not_analyzed_yet;
    224  1.1  mrg 
    225  1.1  mrg   /* The default case produces a safe result.  */
    226  1.1  mrg   return chrec_dont_know;
    227  1.1  mrg }
    228  1.1  mrg 
    229  1.1  mrg /* Fold the addition of two chrecs.  */
    230  1.1  mrg 
    231  1.1  mrg static tree
    232  1.1  mrg chrec_fold_plus_1 (enum tree_code code, tree type,
    233  1.1  mrg 		   tree op0, tree op1)
    234  1.1  mrg {
    235  1.1  mrg   if (automatically_generated_chrec_p (op0)
    236  1.1  mrg       || automatically_generated_chrec_p (op1))
    237  1.1  mrg     return chrec_fold_automatically_generated_operands (op0, op1);
    238  1.1  mrg 
    239  1.1  mrg   switch (TREE_CODE (op0))
    240  1.1  mrg     {
    241  1.1  mrg     case POLYNOMIAL_CHREC:
    242  1.1  mrg       gcc_checking_assert
    243  1.1  mrg 	(!chrec_contains_symbols_defined_in_loop (op0, CHREC_VARIABLE (op0)));
    244  1.1  mrg       switch (TREE_CODE (op1))
    245  1.1  mrg 	{
    246  1.1  mrg 	case POLYNOMIAL_CHREC:
    247  1.1  mrg 	  gcc_checking_assert
    248  1.1  mrg 	    (!chrec_contains_symbols_defined_in_loop (op1,
    249  1.1  mrg 						      CHREC_VARIABLE (op1)));
    250  1.1  mrg 	  return chrec_fold_plus_poly_poly (code, type, op0, op1);
    251  1.1  mrg 
    252  1.1  mrg 	CASE_CONVERT:
    253  1.1  mrg 	  {
    254  1.1  mrg 	    /* We can strip sign-conversions to signed by performing the
    255  1.1  mrg 	       operation in unsigned.  */
    256  1.1  mrg 	    tree optype = TREE_TYPE (TREE_OPERAND (op1, 0));
    257  1.1  mrg 	    if (INTEGRAL_TYPE_P (type)
    258  1.1  mrg 		&& INTEGRAL_TYPE_P (optype)
    259  1.1  mrg 		&& tree_nop_conversion_p (type, optype)
    260  1.1  mrg 		&& TYPE_UNSIGNED (optype))
    261  1.1  mrg 	      return chrec_convert (type,
    262  1.1  mrg 				    chrec_fold_plus_1 (code, optype,
    263  1.1  mrg 						       chrec_convert (optype,
    264  1.1  mrg 								      op0, NULL),
    265  1.1  mrg 						       TREE_OPERAND (op1, 0)),
    266  1.1  mrg 				    NULL);
    267  1.1  mrg 	    if (tree_contains_chrecs (op1, NULL))
    268  1.1  mrg 	      return chrec_dont_know;
    269  1.1  mrg 	  }
    270  1.1  mrg 	  /* FALLTHRU */
    271  1.1  mrg 
    272  1.1  mrg 	default:
    273  1.1  mrg 	  if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
    274  1.1  mrg 	    return build_polynomial_chrec
    275  1.1  mrg 	      (CHREC_VARIABLE (op0),
    276  1.1  mrg 	       chrec_fold_plus (type, CHREC_LEFT (op0), op1),
    277  1.1  mrg 	       CHREC_RIGHT (op0));
    278  1.1  mrg 	  else
    279  1.1  mrg 	    return build_polynomial_chrec
    280  1.1  mrg 	      (CHREC_VARIABLE (op0),
    281  1.1  mrg 	       chrec_fold_minus (type, CHREC_LEFT (op0), op1),
    282  1.1  mrg 	       CHREC_RIGHT (op0));
    283  1.1  mrg 	}
    284  1.1  mrg 
    285  1.1  mrg     CASE_CONVERT:
    286  1.1  mrg       {
    287  1.1  mrg 	/* We can strip sign-conversions to signed by performing the
    288  1.1  mrg 	   operation in unsigned.  */
    289  1.1  mrg 	tree optype = TREE_TYPE (TREE_OPERAND (op0, 0));
    290  1.1  mrg 	if (INTEGRAL_TYPE_P (type)
    291  1.1  mrg 	    && INTEGRAL_TYPE_P (optype)
    292  1.1  mrg 	    && tree_nop_conversion_p (type, optype)
    293  1.1  mrg 	    && TYPE_UNSIGNED (optype))
    294  1.1  mrg 	  return chrec_convert (type,
    295  1.1  mrg 				chrec_fold_plus_1 (code, optype,
    296  1.1  mrg 						   TREE_OPERAND (op0, 0),
    297  1.1  mrg 						   chrec_convert (optype,
    298  1.1  mrg 								  op1, NULL)),
    299  1.1  mrg 				NULL);
    300  1.1  mrg 	if (tree_contains_chrecs (op0, NULL))
    301  1.1  mrg 	  return chrec_dont_know;
    302  1.1  mrg       }
    303  1.1  mrg       /* FALLTHRU */
    304  1.1  mrg 
    305  1.1  mrg     default:
    306  1.1  mrg       switch (TREE_CODE (op1))
    307  1.1  mrg 	{
    308  1.1  mrg 	case POLYNOMIAL_CHREC:
    309  1.1  mrg 	  gcc_checking_assert
    310  1.1  mrg 	    (!chrec_contains_symbols_defined_in_loop (op1,
    311  1.1  mrg 						      CHREC_VARIABLE (op1)));
    312  1.1  mrg 	  if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
    313  1.1  mrg 	    return build_polynomial_chrec
    314  1.1  mrg 	      (CHREC_VARIABLE (op1),
    315  1.1  mrg 	       chrec_fold_plus (type, op0, CHREC_LEFT (op1)),
    316  1.1  mrg 	       CHREC_RIGHT (op1));
    317  1.1  mrg 	  else
    318  1.1  mrg 	    return build_polynomial_chrec
    319  1.1  mrg 	      (CHREC_VARIABLE (op1),
    320  1.1  mrg 	       chrec_fold_minus (type, op0, CHREC_LEFT (op1)),
    321  1.1  mrg 	       chrec_fold_multiply (type, CHREC_RIGHT (op1),
    322  1.1  mrg 				    SCALAR_FLOAT_TYPE_P (type)
    323  1.1  mrg 				    ? build_real (type, dconstm1)
    324  1.1  mrg 				    : build_int_cst_type (type, -1)));
    325  1.1  mrg 
    326  1.1  mrg 	CASE_CONVERT:
    327  1.1  mrg 	  if (tree_contains_chrecs (op1, NULL))
    328  1.1  mrg 	    return chrec_dont_know;
    329  1.1  mrg 	  /* FALLTHRU */
    330  1.1  mrg 
    331  1.1  mrg 	default:
    332  1.1  mrg 	  {
    333  1.1  mrg 	    int size = 0;
    334  1.1  mrg 	    if ((tree_contains_chrecs (op0, &size)
    335  1.1  mrg 		 || tree_contains_chrecs (op1, &size))
    336  1.1  mrg 		&& size < param_scev_max_expr_size)
    337  1.1  mrg 	      return build2 (code, type, op0, op1);
    338  1.1  mrg 	    else if (size < param_scev_max_expr_size)
    339  1.1  mrg 	      {
    340  1.1  mrg 		if (code == POINTER_PLUS_EXPR)
    341  1.1  mrg 		  return fold_build_pointer_plus (fold_convert (type, op0),
    342  1.1  mrg 						  op1);
    343  1.1  mrg 		else
    344  1.1  mrg 		  return fold_build2 (code, type,
    345  1.1  mrg 				      fold_convert (type, op0),
    346  1.1  mrg 				      fold_convert (type, op1));
    347  1.1  mrg 	      }
    348  1.1  mrg 	    else
    349  1.1  mrg 	      return chrec_dont_know;
    350  1.1  mrg 	  }
    351  1.1  mrg 	}
    352  1.1  mrg     }
    353  1.1  mrg }
    354  1.1  mrg 
    355  1.1  mrg /* Fold the addition of two chrecs.  */
    356  1.1  mrg 
    357  1.1  mrg tree
    358  1.1  mrg chrec_fold_plus (tree type,
    359  1.1  mrg 		 tree op0,
    360  1.1  mrg 		 tree op1)
    361  1.1  mrg {
    362  1.1  mrg   enum tree_code code;
    363  1.1  mrg   if (automatically_generated_chrec_p (op0)
    364  1.1  mrg       || automatically_generated_chrec_p (op1))
    365  1.1  mrg     return chrec_fold_automatically_generated_operands (op0, op1);
    366  1.1  mrg 
    367  1.1  mrg   if (integer_zerop (op0))
    368  1.1  mrg     return chrec_convert (type, op1, NULL);
    369  1.1  mrg   if (integer_zerop (op1))
    370  1.1  mrg     return chrec_convert (type, op0, NULL);
    371  1.1  mrg 
    372  1.1  mrg   if (POINTER_TYPE_P (type))
    373  1.1  mrg     code = POINTER_PLUS_EXPR;
    374  1.1  mrg   else
    375  1.1  mrg     code = PLUS_EXPR;
    376  1.1  mrg 
    377  1.1  mrg   return chrec_fold_plus_1 (code, type, op0, op1);
    378  1.1  mrg }
    379  1.1  mrg 
    380  1.1  mrg /* Fold the subtraction of two chrecs.  */
    381  1.1  mrg 
    382  1.1  mrg tree
    383  1.1  mrg chrec_fold_minus (tree type,
    384  1.1  mrg 		  tree op0,
    385  1.1  mrg 		  tree op1)
    386  1.1  mrg {
    387  1.1  mrg   if (automatically_generated_chrec_p (op0)
    388  1.1  mrg       || automatically_generated_chrec_p (op1))
    389  1.1  mrg     return chrec_fold_automatically_generated_operands (op0, op1);
    390  1.1  mrg 
    391  1.1  mrg   if (integer_zerop (op1))
    392  1.1  mrg     return op0;
    393  1.1  mrg 
    394  1.1  mrg   return chrec_fold_plus_1 (MINUS_EXPR, type, op0, op1);
    395  1.1  mrg }
    396  1.1  mrg 
    397  1.1  mrg /* Fold the multiplication of two chrecs.  */
    398  1.1  mrg 
    399  1.1  mrg tree
    400  1.1  mrg chrec_fold_multiply (tree type,
    401  1.1  mrg 		     tree op0,
    402  1.1  mrg 		     tree op1)
    403  1.1  mrg {
    404  1.1  mrg   if (automatically_generated_chrec_p (op0)
    405  1.1  mrg       || automatically_generated_chrec_p (op1))
    406  1.1  mrg     return chrec_fold_automatically_generated_operands (op0, op1);
    407  1.1  mrg 
    408  1.1  mrg   switch (TREE_CODE (op0))
    409  1.1  mrg     {
    410  1.1  mrg     case POLYNOMIAL_CHREC:
    411  1.1  mrg       gcc_checking_assert
    412  1.1  mrg 	(!chrec_contains_symbols_defined_in_loop (op0, CHREC_VARIABLE (op0)));
    413  1.1  mrg       switch (TREE_CODE (op1))
    414  1.1  mrg 	{
    415  1.1  mrg 	case POLYNOMIAL_CHREC:
    416  1.1  mrg 	  gcc_checking_assert
    417  1.1  mrg 	    (!chrec_contains_symbols_defined_in_loop (op1,
    418  1.1  mrg 						      CHREC_VARIABLE (op1)));
    419  1.1  mrg 	  return chrec_fold_multiply_poly_poly (type, op0, op1);
    420  1.1  mrg 
    421  1.1  mrg 	CASE_CONVERT:
    422  1.1  mrg 	  if (tree_contains_chrecs (op1, NULL))
    423  1.1  mrg 	    return chrec_dont_know;
    424  1.1  mrg 	  /* FALLTHRU */
    425  1.1  mrg 
    426  1.1  mrg 	default:
    427  1.1  mrg 	  if (integer_onep (op1))
    428  1.1  mrg 	    return op0;
    429  1.1  mrg 	  if (integer_zerop (op1))
    430  1.1  mrg 	    return build_int_cst (type, 0);
    431  1.1  mrg 
    432  1.1  mrg 	  return build_polynomial_chrec
    433  1.1  mrg 	    (CHREC_VARIABLE (op0),
    434  1.1  mrg 	     chrec_fold_multiply (type, CHREC_LEFT (op0), op1),
    435  1.1  mrg 	     chrec_fold_multiply (type, CHREC_RIGHT (op0), op1));
    436  1.1  mrg 	}
    437  1.1  mrg 
    438  1.1  mrg     CASE_CONVERT:
    439  1.1  mrg       if (tree_contains_chrecs (op0, NULL))
    440  1.1  mrg 	return chrec_dont_know;
    441  1.1  mrg       /* FALLTHRU */
    442  1.1  mrg 
    443  1.1  mrg     default:
    444  1.1  mrg       if (integer_onep (op0))
    445  1.1  mrg 	return op1;
    446  1.1  mrg 
    447  1.1  mrg       if (integer_zerop (op0))
    448  1.1  mrg     	return build_int_cst (type, 0);
    449  1.1  mrg 
    450  1.1  mrg       switch (TREE_CODE (op1))
    451  1.1  mrg 	{
    452  1.1  mrg 	case POLYNOMIAL_CHREC:
    453  1.1  mrg 	  gcc_checking_assert
    454  1.1  mrg 	    (!chrec_contains_symbols_defined_in_loop (op1,
    455  1.1  mrg 						      CHREC_VARIABLE (op1)));
    456  1.1  mrg 	  return build_polynomial_chrec
    457  1.1  mrg 	    (CHREC_VARIABLE (op1),
    458  1.1  mrg 	     chrec_fold_multiply (type, CHREC_LEFT (op1), op0),
    459  1.1  mrg 	     chrec_fold_multiply (type, CHREC_RIGHT (op1), op0));
    460  1.1  mrg 
    461  1.1  mrg 	CASE_CONVERT:
    462  1.1  mrg 	  if (tree_contains_chrecs (op1, NULL))
    463  1.1  mrg 	    return chrec_dont_know;
    464  1.1  mrg 	  /* FALLTHRU */
    465  1.1  mrg 
    466  1.1  mrg 	default:
    467  1.1  mrg 	  if (integer_onep (op1))
    468  1.1  mrg 	    return op0;
    469  1.1  mrg 	  if (integer_zerop (op1))
    470  1.1  mrg 	    return build_int_cst (type, 0);
    471  1.1  mrg 	  return fold_build2 (MULT_EXPR, type, op0, op1);
    472  1.1  mrg 	}
    473  1.1  mrg     }
    474  1.1  mrg }
    475  1.1  mrg 
    476  1.1  mrg 
    477  1.1  mrg 
    479  1.1  mrg /* Operations.  */
    480  1.1  mrg 
    481  1.1  mrg /* Evaluate the binomial coefficient.  Return NULL_TREE if the intermediate
    482  1.1  mrg    calculation overflows, otherwise return C(n,k) with type TYPE.  */
    483  1.1  mrg 
    484  1.1  mrg static tree
    485  1.1  mrg tree_fold_binomial (tree type, tree n, unsigned int k)
    486  1.1  mrg {
    487  1.1  mrg   wi::overflow_type overflow;
    488  1.1  mrg   unsigned int i;
    489  1.1  mrg 
    490  1.1  mrg   /* Handle the most frequent cases.  */
    491  1.1  mrg   if (k == 0)
    492  1.1  mrg     return build_int_cst (type, 1);
    493  1.1  mrg   if (k == 1)
    494  1.1  mrg     return fold_convert (type, n);
    495  1.1  mrg 
    496  1.1  mrg   widest_int num = wi::to_widest (n);
    497  1.1  mrg 
    498  1.1  mrg   /* Check that k <= n.  */
    499  1.1  mrg   if (wi::ltu_p (num, k))
    500  1.1  mrg     return NULL_TREE;
    501  1.1  mrg 
    502  1.1  mrg   /* Denominator = 2.  */
    503  1.1  mrg   widest_int denom = 2;
    504  1.1  mrg 
    505  1.1  mrg   /* Index = Numerator-1.  */
    506  1.1  mrg   widest_int idx = num - 1;
    507  1.1  mrg 
    508  1.1  mrg   /* Numerator = Numerator*Index = n*(n-1).  */
    509  1.1  mrg   num = wi::smul (num, idx, &overflow);
    510  1.1  mrg   if (overflow)
    511  1.1  mrg     return NULL_TREE;
    512  1.1  mrg 
    513  1.1  mrg   for (i = 3; i <= k; i++)
    514  1.1  mrg     {
    515  1.1  mrg       /* Index--.  */
    516  1.1  mrg       --idx;
    517  1.1  mrg 
    518  1.1  mrg       /* Numerator *= Index.  */
    519  1.1  mrg       num = wi::smul (num, idx, &overflow);
    520  1.1  mrg       if (overflow)
    521  1.1  mrg 	return NULL_TREE;
    522  1.1  mrg 
    523  1.1  mrg       /* Denominator *= i.  */
    524  1.1  mrg       denom *= i;
    525  1.1  mrg     }
    526  1.1  mrg 
    527  1.1  mrg   /* Result = Numerator / Denominator.  */
    528  1.1  mrg   num = wi::udiv_trunc (num, denom);
    529  1.1  mrg   if (! wi::fits_to_tree_p (num, type))
    530  1.1  mrg     return NULL_TREE;
    531  1.1  mrg   return wide_int_to_tree (type, num);
    532  1.1  mrg }
    533  1.1  mrg 
    534  1.1  mrg /* Helper function.  Use the Newton's interpolating formula for
    535  1.1  mrg    evaluating the value of the evolution function.
    536  1.1  mrg    The result may be in an unsigned type of CHREC.  */
    537  1.1  mrg 
    538  1.1  mrg static tree
    539  1.1  mrg chrec_evaluate (unsigned var, tree chrec, tree n, unsigned int k)
    540  1.1  mrg {
    541  1.1  mrg   tree arg0, arg1, binomial_n_k;
    542  1.1  mrg   tree type = TREE_TYPE (chrec);
    543  1.1  mrg   class loop *var_loop = get_loop (cfun, var);
    544  1.1  mrg 
    545  1.1  mrg   while (TREE_CODE (chrec) == POLYNOMIAL_CHREC
    546  1.1  mrg 	 && flow_loop_nested_p (var_loop, get_chrec_loop (chrec)))
    547  1.1  mrg     chrec = CHREC_LEFT (chrec);
    548  1.1  mrg 
    549  1.1  mrg   /* The formula associates the expression and thus we have to make
    550  1.1  mrg      sure to not introduce undefined overflow.  */
    551  1.1  mrg   tree ctype = type;
    552  1.1  mrg   if (INTEGRAL_TYPE_P (type)
    553  1.1  mrg       && ! TYPE_OVERFLOW_WRAPS (type))
    554  1.1  mrg     ctype = unsigned_type_for (type);
    555  1.1  mrg 
    556  1.1  mrg   if (TREE_CODE (chrec) == POLYNOMIAL_CHREC
    557  1.1  mrg       && CHREC_VARIABLE (chrec) == var)
    558  1.1  mrg     {
    559  1.1  mrg       arg1 = chrec_evaluate (var, CHREC_RIGHT (chrec), n, k + 1);
    560  1.1  mrg       if (arg1 == chrec_dont_know)
    561  1.1  mrg 	return chrec_dont_know;
    562  1.1  mrg       binomial_n_k = tree_fold_binomial (ctype, n, k);
    563  1.1  mrg       if (!binomial_n_k)
    564  1.1  mrg 	return chrec_dont_know;
    565  1.1  mrg       tree l = chrec_convert (ctype, CHREC_LEFT (chrec), NULL);
    566  1.1  mrg       arg0 = fold_build2 (MULT_EXPR, ctype, l, binomial_n_k);
    567  1.1  mrg       return chrec_fold_plus (ctype, arg0, arg1);
    568  1.1  mrg     }
    569  1.1  mrg 
    570  1.1  mrg   binomial_n_k = tree_fold_binomial (ctype, n, k);
    571  1.1  mrg   if (!binomial_n_k)
    572  1.1  mrg     return chrec_dont_know;
    573  1.1  mrg 
    574  1.1  mrg   return fold_build2 (MULT_EXPR, ctype,
    575  1.1  mrg 		      chrec_convert (ctype, chrec, NULL), binomial_n_k);
    576  1.1  mrg }
    577  1.1  mrg 
    578  1.1  mrg /* Evaluates "CHREC (X)" when the varying variable is VAR.
    579  1.1  mrg    Example:  Given the following parameters,
    580  1.1  mrg 
    581  1.1  mrg    var = 1
    582  1.1  mrg    chrec = {3, +, 4}_1
    583  1.1  mrg    x = 10
    584  1.1  mrg 
    585  1.1  mrg    The result is given by the Newton's interpolating formula:
    586  1.1  mrg    3 * \binom{10}{0} + 4 * \binom{10}{1}.
    587  1.1  mrg */
    588  1.1  mrg 
    589  1.1  mrg tree
    590  1.1  mrg chrec_apply (unsigned var,
    591  1.1  mrg 	     tree chrec,
    592  1.1  mrg 	     tree x)
    593  1.1  mrg {
    594  1.1  mrg   tree type = chrec_type (chrec);
    595  1.1  mrg   tree res = chrec_dont_know;
    596  1.1  mrg 
    597  1.1  mrg   if (automatically_generated_chrec_p (chrec)
    598  1.1  mrg       || automatically_generated_chrec_p (x)
    599  1.1  mrg 
    600  1.1  mrg       /* When the symbols are defined in an outer loop, it is possible
    601  1.1  mrg 	 to symbolically compute the apply, since the symbols are
    602  1.1  mrg 	 constants with respect to the varying loop.  */
    603  1.1  mrg       || chrec_contains_symbols_defined_in_loop (chrec, var))
    604  1.1  mrg     return chrec_dont_know;
    605  1.1  mrg 
    606  1.1  mrg   if (dump_file && (dump_flags & TDF_SCEV))
    607  1.1  mrg     fprintf (dump_file, "(chrec_apply \n");
    608  1.1  mrg 
    609  1.1  mrg   if (TREE_CODE (x) == INTEGER_CST && SCALAR_FLOAT_TYPE_P (type))
    610  1.1  mrg     x = build_real_from_int_cst (type, x);
    611  1.1  mrg 
    612  1.1  mrg   switch (TREE_CODE (chrec))
    613  1.1  mrg     {
    614  1.1  mrg     case POLYNOMIAL_CHREC:
    615  1.1  mrg       if (evolution_function_is_affine_p (chrec))
    616  1.1  mrg 	{
    617  1.1  mrg 	  if (CHREC_VARIABLE (chrec) != var)
    618  1.1  mrg 	    return build_polynomial_chrec
    619  1.1  mrg 	      (CHREC_VARIABLE (chrec),
    620  1.1  mrg 	       chrec_apply (var, CHREC_LEFT (chrec), x),
    621  1.1  mrg 	       chrec_apply (var, CHREC_RIGHT (chrec), x));
    622  1.1  mrg 
    623  1.1  mrg 	  /* "{a, +, b} (x)"  ->  "a + b*x".  */
    624  1.1  mrg 	  x = chrec_convert_rhs (type, x, NULL);
    625  1.1  mrg 	  res = chrec_fold_multiply (TREE_TYPE (x), CHREC_RIGHT (chrec), x);
    626  1.1  mrg 	  res = chrec_fold_plus (type, CHREC_LEFT (chrec), res);
    627  1.1  mrg 	}
    628  1.1  mrg       else if (TREE_CODE (x) == INTEGER_CST
    629  1.1  mrg 	       && tree_int_cst_sgn (x) == 1)
    630  1.1  mrg 	/* testsuite/.../ssa-chrec-38.c.  */
    631  1.1  mrg 	res = chrec_convert (type, chrec_evaluate (var, chrec, x, 0), NULL);
    632  1.1  mrg       else
    633  1.1  mrg 	res = chrec_dont_know;
    634  1.1  mrg       break;
    635  1.1  mrg 
    636  1.1  mrg     CASE_CONVERT:
    637  1.1  mrg       res = chrec_convert (TREE_TYPE (chrec),
    638  1.1  mrg 			   chrec_apply (var, TREE_OPERAND (chrec, 0), x),
    639  1.1  mrg 			   NULL);
    640  1.1  mrg       break;
    641  1.1  mrg 
    642  1.1  mrg     default:
    643  1.1  mrg       res = chrec;
    644  1.1  mrg       break;
    645  1.1  mrg     }
    646  1.1  mrg 
    647  1.1  mrg   if (dump_file && (dump_flags & TDF_SCEV))
    648  1.1  mrg     {
    649  1.1  mrg       fprintf (dump_file, "  (varying_loop = %d\n", var);
    650  1.1  mrg       fprintf (dump_file, ")\n  (chrec = ");
    651  1.1  mrg       print_generic_expr (dump_file, chrec);
    652  1.1  mrg       fprintf (dump_file, ")\n  (x = ");
    653  1.1  mrg       print_generic_expr (dump_file, x);
    654  1.1  mrg       fprintf (dump_file, ")\n  (res = ");
    655  1.1  mrg       print_generic_expr (dump_file, res);
    656  1.1  mrg       fprintf (dump_file, "))\n");
    657  1.1  mrg     }
    658  1.1  mrg 
    659  1.1  mrg   return res;
    660  1.1  mrg }
    661  1.1  mrg 
    662  1.1  mrg /* For a given CHREC and an induction variable map IV_MAP that maps
    663  1.1  mrg    (loop->num, expr) for every loop number of the current_loops an
    664  1.1  mrg    expression, calls chrec_apply when the expression is not NULL.  */
    665  1.1  mrg 
    666  1.1  mrg tree
    667  1.1  mrg chrec_apply_map (tree chrec, vec<tree> iv_map)
    668  1.1  mrg {
    669  1.1  mrg   int i;
    670  1.1  mrg   tree expr;
    671  1.1  mrg 
    672  1.1  mrg   FOR_EACH_VEC_ELT (iv_map, i, expr)
    673  1.1  mrg     if (expr)
    674  1.1  mrg       chrec = chrec_apply (i, chrec, expr);
    675  1.1  mrg 
    676  1.1  mrg   return chrec;
    677  1.1  mrg }
    678  1.1  mrg 
    679  1.1  mrg /* Replaces the initial condition in CHREC with INIT_COND.  */
    680  1.1  mrg 
    681  1.1  mrg tree
    682  1.1  mrg chrec_replace_initial_condition (tree chrec,
    683  1.1  mrg 				 tree init_cond)
    684  1.1  mrg {
    685  1.1  mrg   if (automatically_generated_chrec_p (chrec))
    686  1.1  mrg     return chrec;
    687  1.1  mrg 
    688  1.1  mrg   gcc_assert (chrec_type (chrec) == chrec_type (init_cond));
    689  1.1  mrg 
    690  1.1  mrg   switch (TREE_CODE (chrec))
    691  1.1  mrg     {
    692  1.1  mrg     case POLYNOMIAL_CHREC:
    693  1.1  mrg       return build_polynomial_chrec
    694  1.1  mrg 	(CHREC_VARIABLE (chrec),
    695  1.1  mrg 	 chrec_replace_initial_condition (CHREC_LEFT (chrec), init_cond),
    696  1.1  mrg 	 CHREC_RIGHT (chrec));
    697  1.1  mrg 
    698  1.1  mrg     default:
    699  1.1  mrg       return init_cond;
    700  1.1  mrg     }
    701  1.1  mrg }
    702  1.1  mrg 
    703  1.1  mrg /* Returns the initial condition of a given CHREC.  */
    704  1.1  mrg 
    705  1.1  mrg tree
    706  1.1  mrg initial_condition (tree chrec)
    707  1.1  mrg {
    708  1.1  mrg   if (automatically_generated_chrec_p (chrec))
    709  1.1  mrg     return chrec;
    710  1.1  mrg 
    711  1.1  mrg   if (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
    712  1.1  mrg     return initial_condition (CHREC_LEFT (chrec));
    713  1.1  mrg   else
    714  1.1  mrg     return chrec;
    715  1.1  mrg }
    716  1.1  mrg 
    717  1.1  mrg /* Returns a univariate function that represents the evolution in
    718  1.1  mrg    LOOP_NUM.  Mask the evolution of any other loop.  */
    719  1.1  mrg 
    720  1.1  mrg tree
    721  1.1  mrg hide_evolution_in_other_loops_than_loop (tree chrec,
    722  1.1  mrg 					 unsigned loop_num)
    723  1.1  mrg {
    724  1.1  mrg   class loop *loop = get_loop (cfun, loop_num), *chloop;
    725  1.1  mrg   if (automatically_generated_chrec_p (chrec))
    726  1.1  mrg     return chrec;
    727  1.1  mrg 
    728  1.1  mrg   switch (TREE_CODE (chrec))
    729  1.1  mrg     {
    730  1.1  mrg     case POLYNOMIAL_CHREC:
    731  1.1  mrg       chloop = get_chrec_loop (chrec);
    732  1.1  mrg 
    733  1.1  mrg       if (chloop == loop)
    734  1.1  mrg 	return build_polynomial_chrec
    735  1.1  mrg 	  (loop_num,
    736  1.1  mrg 	   hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec),
    737  1.1  mrg 						    loop_num),
    738  1.1  mrg 	   CHREC_RIGHT (chrec));
    739  1.1  mrg 
    740  1.1  mrg       else if (flow_loop_nested_p (chloop, loop))
    741  1.1  mrg 	/* There is no evolution in this loop.  */
    742  1.1  mrg 	return initial_condition (chrec);
    743  1.1  mrg 
    744  1.1  mrg       else if (flow_loop_nested_p (loop, chloop))
    745  1.1  mrg 	return hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec),
    746  1.1  mrg 							loop_num);
    747  1.1  mrg 
    748  1.1  mrg       else
    749  1.1  mrg 	return chrec_dont_know;
    750  1.1  mrg 
    751  1.1  mrg     default:
    752  1.1  mrg       return chrec;
    753  1.1  mrg     }
    754  1.1  mrg }
    755  1.1  mrg 
    756  1.1  mrg /* Returns the evolution part of CHREC in LOOP_NUM when RIGHT is
    757  1.1  mrg    true, otherwise returns the initial condition in LOOP_NUM.  */
    758  1.1  mrg 
    759  1.1  mrg static tree
    760  1.1  mrg chrec_component_in_loop_num (tree chrec,
    761  1.1  mrg 			     unsigned loop_num,
    762  1.1  mrg 			     bool right)
    763  1.1  mrg {
    764  1.1  mrg   tree component;
    765  1.1  mrg   class loop *loop = get_loop (cfun, loop_num), *chloop;
    766  1.1  mrg 
    767  1.1  mrg   if (automatically_generated_chrec_p (chrec))
    768  1.1  mrg     return chrec;
    769  1.1  mrg 
    770  1.1  mrg   switch (TREE_CODE (chrec))
    771  1.1  mrg     {
    772  1.1  mrg     case POLYNOMIAL_CHREC:
    773  1.1  mrg       chloop = get_chrec_loop (chrec);
    774  1.1  mrg 
    775  1.1  mrg       if (chloop == loop)
    776  1.1  mrg 	{
    777  1.1  mrg 	  if (right)
    778  1.1  mrg 	    component = CHREC_RIGHT (chrec);
    779  1.1  mrg 	  else
    780  1.1  mrg 	    component = CHREC_LEFT (chrec);
    781  1.1  mrg 
    782  1.1  mrg 	  if (TREE_CODE (CHREC_LEFT (chrec)) != POLYNOMIAL_CHREC
    783  1.1  mrg 	      || CHREC_VARIABLE (CHREC_LEFT (chrec)) != CHREC_VARIABLE (chrec))
    784  1.1  mrg 	    return component;
    785  1.1  mrg 
    786  1.1  mrg 	  else
    787  1.1  mrg 	    return build_polynomial_chrec
    788  1.1  mrg 	      (loop_num,
    789  1.1  mrg 	       chrec_component_in_loop_num (CHREC_LEFT (chrec),
    790  1.1  mrg 					    loop_num,
    791  1.1  mrg 					    right),
    792  1.1  mrg 	       component);
    793  1.1  mrg 	}
    794  1.1  mrg 
    795  1.1  mrg       else if (flow_loop_nested_p (chloop, loop))
    796  1.1  mrg 	/* There is no evolution part in this loop.  */
    797  1.1  mrg 	return NULL_TREE;
    798  1.1  mrg 
    799  1.1  mrg       else
    800  1.1  mrg 	{
    801  1.1  mrg 	  gcc_assert (flow_loop_nested_p (loop, chloop));
    802  1.1  mrg 	  return chrec_component_in_loop_num (CHREC_LEFT (chrec),
    803  1.1  mrg 					      loop_num,
    804  1.1  mrg 					      right);
    805  1.1  mrg 	}
    806  1.1  mrg 
    807  1.1  mrg      default:
    808  1.1  mrg       if (right)
    809  1.1  mrg 	return NULL_TREE;
    810  1.1  mrg       else
    811  1.1  mrg 	return chrec;
    812  1.1  mrg     }
    813  1.1  mrg }
    814  1.1  mrg 
    815  1.1  mrg /* Returns the evolution part in LOOP_NUM.  Example: the call
    816  1.1  mrg    evolution_part_in_loop_num ({{0, +, 1}_1, +, 2}_1, 1) returns
    817  1.1  mrg    {1, +, 2}_1  */
    818  1.1  mrg 
    819  1.1  mrg tree
    820  1.1  mrg evolution_part_in_loop_num (tree chrec,
    821  1.1  mrg 			    unsigned loop_num)
    822  1.1  mrg {
    823  1.1  mrg   return chrec_component_in_loop_num (chrec, loop_num, true);
    824  1.1  mrg }
    825  1.1  mrg 
    826  1.1  mrg /* Returns the initial condition in LOOP_NUM.  Example: the call
    827  1.1  mrg    initial_condition_in_loop_num ({{0, +, 1}_1, +, 2}_2, 2) returns
    828  1.1  mrg    {0, +, 1}_1  */
    829  1.1  mrg 
    830  1.1  mrg tree
    831  1.1  mrg initial_condition_in_loop_num (tree chrec,
    832  1.1  mrg 			       unsigned loop_num)
    833  1.1  mrg {
    834  1.1  mrg   return chrec_component_in_loop_num (chrec, loop_num, false);
    835  1.1  mrg }
    836  1.1  mrg 
    837  1.1  mrg /* Set or reset the evolution of CHREC to NEW_EVOL in loop LOOP_NUM.
    838  1.1  mrg    This function is essentially used for setting the evolution to
    839  1.1  mrg    chrec_dont_know, for example after having determined that it is
    840  1.1  mrg    impossible to say how many times a loop will execute.  */
    841  1.1  mrg 
    842  1.1  mrg tree
    843  1.1  mrg reset_evolution_in_loop (unsigned loop_num,
    844  1.1  mrg 			 tree chrec,
    845  1.1  mrg 			 tree new_evol)
    846  1.1  mrg {
    847  1.1  mrg   class loop *loop = get_loop (cfun, loop_num);
    848  1.1  mrg 
    849  1.1  mrg   if (POINTER_TYPE_P (chrec_type (chrec)))
    850  1.1  mrg     gcc_assert (ptrofftype_p (chrec_type (new_evol)));
    851  1.1  mrg   else
    852  1.1  mrg     gcc_assert (chrec_type (chrec) == chrec_type (new_evol));
    853  1.1  mrg 
    854  1.1  mrg   if (TREE_CODE (chrec) == POLYNOMIAL_CHREC
    855  1.1  mrg       && flow_loop_nested_p (loop, get_chrec_loop (chrec)))
    856  1.1  mrg     {
    857  1.1  mrg       tree left = reset_evolution_in_loop (loop_num, CHREC_LEFT (chrec),
    858  1.1  mrg 					   new_evol);
    859  1.1  mrg       tree right = reset_evolution_in_loop (loop_num, CHREC_RIGHT (chrec),
    860  1.1  mrg 					    new_evol);
    861  1.1  mrg       return build_polynomial_chrec (CHREC_VARIABLE (chrec), left, right);
    862  1.1  mrg     }
    863  1.1  mrg 
    864  1.1  mrg   while (TREE_CODE (chrec) == POLYNOMIAL_CHREC
    865  1.1  mrg 	 && CHREC_VARIABLE (chrec) == loop_num)
    866  1.1  mrg     chrec = CHREC_LEFT (chrec);
    867  1.1  mrg 
    868  1.1  mrg   return build_polynomial_chrec (loop_num, chrec, new_evol);
    869  1.1  mrg }
    870  1.1  mrg 
    871  1.1  mrg /* Merges two evolution functions that were found by following two
    872  1.1  mrg    alternate paths of a conditional expression.  */
    873  1.1  mrg 
    874  1.1  mrg tree
    875  1.1  mrg chrec_merge (tree chrec1,
    876  1.1  mrg 	     tree chrec2)
    877  1.1  mrg {
    878  1.1  mrg   if (chrec1 == chrec_dont_know
    879  1.1  mrg       || chrec2 == chrec_dont_know)
    880  1.1  mrg     return chrec_dont_know;
    881  1.1  mrg 
    882  1.1  mrg   if (chrec1 == chrec_known
    883  1.1  mrg       || chrec2 == chrec_known)
    884  1.1  mrg     return chrec_known;
    885  1.1  mrg 
    886  1.1  mrg   if (chrec1 == chrec_not_analyzed_yet)
    887  1.1  mrg     return chrec2;
    888  1.1  mrg   if (chrec2 == chrec_not_analyzed_yet)
    889  1.1  mrg     return chrec1;
    890  1.1  mrg 
    891  1.1  mrg   if (eq_evolutions_p (chrec1, chrec2))
    892  1.1  mrg     return chrec1;
    893  1.1  mrg 
    894  1.1  mrg   return chrec_dont_know;
    895  1.1  mrg }
    896  1.1  mrg 
    897  1.1  mrg 
    898  1.1  mrg 
    900  1.1  mrg /* Observers.  */
    901  1.1  mrg 
    902  1.1  mrg /* Helper function for is_multivariate_chrec.  */
    903  1.1  mrg 
    904  1.1  mrg static bool
    905  1.1  mrg is_multivariate_chrec_rec (const_tree chrec, unsigned int rec_var)
    906  1.1  mrg {
    907  1.1  mrg   if (chrec == NULL_TREE)
    908  1.1  mrg     return false;
    909  1.1  mrg 
    910  1.1  mrg   if (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
    911  1.1  mrg     {
    912  1.1  mrg       if (CHREC_VARIABLE (chrec) != rec_var)
    913  1.1  mrg 	return true;
    914  1.1  mrg       else
    915  1.1  mrg 	return (is_multivariate_chrec_rec (CHREC_LEFT (chrec), rec_var)
    916  1.1  mrg 		|| is_multivariate_chrec_rec (CHREC_RIGHT (chrec), rec_var));
    917  1.1  mrg     }
    918  1.1  mrg   else
    919  1.1  mrg     return false;
    920  1.1  mrg }
    921  1.1  mrg 
    922  1.1  mrg /* Determine whether the given chrec is multivariate or not.  */
    923  1.1  mrg 
    924  1.1  mrg bool
    925  1.1  mrg is_multivariate_chrec (const_tree chrec)
    926  1.1  mrg {
    927  1.1  mrg   if (chrec == NULL_TREE)
    928  1.1  mrg     return false;
    929  1.1  mrg 
    930  1.1  mrg   if (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
    931  1.1  mrg     return (is_multivariate_chrec_rec (CHREC_LEFT (chrec),
    932  1.1  mrg 				       CHREC_VARIABLE (chrec))
    933  1.1  mrg 	    || is_multivariate_chrec_rec (CHREC_RIGHT (chrec),
    934  1.1  mrg 					  CHREC_VARIABLE (chrec)));
    935  1.1  mrg   else
    936  1.1  mrg     return false;
    937  1.1  mrg }
    938  1.1  mrg 
    939  1.1  mrg /* Determines whether the chrec contains symbolic names or not.  If LOOP isn't
    940  1.1  mrg    NULL, we also consider chrec wrto outer loops of LOOP as symbol.  */
    941  1.1  mrg 
    942  1.1  mrg static bool
    943  1.1  mrg chrec_contains_symbols (const_tree chrec, hash_set<const_tree> &visited,
    944  1.1  mrg 			class loop *loop)
    945  1.1  mrg {
    946  1.1  mrg   int i, n;
    947  1.1  mrg 
    948  1.1  mrg   if (chrec == NULL_TREE)
    949  1.1  mrg     return false;
    950  1.1  mrg 
    951  1.1  mrg   if (TREE_CODE (chrec) == SSA_NAME
    952  1.1  mrg       || VAR_P (chrec)
    953  1.1  mrg       || TREE_CODE (chrec) == POLY_INT_CST
    954  1.1  mrg       || TREE_CODE (chrec) == PARM_DECL
    955  1.1  mrg       || TREE_CODE (chrec) == FUNCTION_DECL
    956  1.1  mrg       || TREE_CODE (chrec) == LABEL_DECL
    957  1.1  mrg       || TREE_CODE (chrec) == RESULT_DECL
    958  1.1  mrg       || TREE_CODE (chrec) == FIELD_DECL)
    959  1.1  mrg     return true;
    960  1.1  mrg 
    961  1.1  mrg   if (loop != NULL
    962  1.1  mrg       && TREE_CODE (chrec) == POLYNOMIAL_CHREC
    963  1.1  mrg       && flow_loop_nested_p (get_chrec_loop (chrec), loop))
    964  1.1  mrg     return true;
    965  1.1  mrg 
    966  1.1  mrg   if (visited.add (chrec))
    967  1.1  mrg     return false;
    968  1.1  mrg 
    969  1.1  mrg   n = TREE_OPERAND_LENGTH (chrec);
    970  1.1  mrg   for (i = 0; i < n; i++)
    971  1.1  mrg     if (chrec_contains_symbols (TREE_OPERAND (chrec, i), visited, loop))
    972  1.1  mrg       return true;
    973  1.1  mrg   return false;
    974  1.1  mrg }
    975  1.1  mrg 
    976  1.1  mrg /* Return true if CHREC contains any symbols.  If LOOP is not NULL, check if
    977  1.1  mrg    CHREC contains any chrec which is invariant wrto the loop (nest), in other
    978  1.1  mrg    words, chrec defined by outer loops of loop, so from LOOP's point of view,
    979  1.1  mrg    the chrec is considered as a SYMBOL.  */
    980  1.1  mrg 
    981  1.1  mrg bool
    982  1.1  mrg chrec_contains_symbols (const_tree chrec, class loop* loop)
    983  1.1  mrg {
    984  1.1  mrg   hash_set<const_tree> visited;
    985  1.1  mrg   return chrec_contains_symbols (chrec, visited, loop);
    986  1.1  mrg }
    987  1.1  mrg 
    988  1.1  mrg /* Return true when CHREC contains symbolic names defined in
    989  1.1  mrg    LOOP_NB.  */
    990  1.1  mrg 
    991  1.1  mrg static bool
    992  1.1  mrg chrec_contains_symbols_defined_in_loop (const_tree chrec, unsigned loop_nb,
    993  1.1  mrg 					hash_set<const_tree> &visited)
    994  1.1  mrg {
    995  1.1  mrg   int i, n;
    996  1.1  mrg 
    997  1.1  mrg   if (chrec == NULL_TREE)
    998  1.1  mrg     return false;
    999  1.1  mrg 
   1000  1.1  mrg   if (is_gimple_min_invariant (chrec))
   1001  1.1  mrg     return false;
   1002  1.1  mrg 
   1003  1.1  mrg   if (TREE_CODE (chrec) == SSA_NAME)
   1004  1.1  mrg     {
   1005  1.1  mrg       gimple *def;
   1006  1.1  mrg       loop_p def_loop, loop;
   1007  1.1  mrg 
   1008  1.1  mrg       if (SSA_NAME_IS_DEFAULT_DEF (chrec))
   1009  1.1  mrg 	return false;
   1010  1.1  mrg 
   1011  1.1  mrg       def = SSA_NAME_DEF_STMT (chrec);
   1012  1.1  mrg       def_loop = loop_containing_stmt (def);
   1013  1.1  mrg       loop = get_loop (cfun, loop_nb);
   1014  1.1  mrg 
   1015  1.1  mrg       if (def_loop == NULL)
   1016  1.1  mrg 	return false;
   1017  1.1  mrg 
   1018  1.1  mrg       if (loop == def_loop || flow_loop_nested_p (loop, def_loop))
   1019  1.1  mrg 	return true;
   1020  1.1  mrg 
   1021  1.1  mrg       return false;
   1022  1.1  mrg     }
   1023  1.1  mrg 
   1024  1.1  mrg   if (visited.add (chrec))
   1025  1.1  mrg     return false;
   1026  1.1  mrg 
   1027  1.1  mrg   n = TREE_OPERAND_LENGTH (chrec);
   1028  1.1  mrg   for (i = 0; i < n; i++)
   1029  1.1  mrg     if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec, i),
   1030  1.1  mrg 						loop_nb, visited))
   1031  1.1  mrg       return true;
   1032  1.1  mrg   return false;
   1033  1.1  mrg }
   1034  1.1  mrg 
   1035  1.1  mrg /* Return true when CHREC contains symbolic names defined in
   1036  1.1  mrg    LOOP_NB.  */
   1037  1.1  mrg 
   1038  1.1  mrg bool
   1039  1.1  mrg chrec_contains_symbols_defined_in_loop (const_tree chrec, unsigned loop_nb)
   1040  1.1  mrg {
   1041  1.1  mrg   hash_set<const_tree> visited;
   1042  1.1  mrg   return chrec_contains_symbols_defined_in_loop (chrec, loop_nb, visited);
   1043  1.1  mrg }
   1044  1.1  mrg 
   1045  1.1  mrg /* Determines whether the chrec contains undetermined coefficients.  */
   1046  1.1  mrg 
   1047  1.1  mrg static bool
   1048  1.1  mrg chrec_contains_undetermined (const_tree chrec, hash_set<const_tree> &visited)
   1049  1.1  mrg {
   1050  1.1  mrg   int i, n;
   1051  1.1  mrg 
   1052  1.1  mrg   if (chrec == chrec_dont_know)
   1053  1.1  mrg     return true;
   1054  1.1  mrg 
   1055  1.1  mrg   if (chrec == NULL_TREE)
   1056  1.1  mrg     return false;
   1057  1.1  mrg 
   1058  1.1  mrg   if (visited.add (chrec))
   1059  1.1  mrg     return false;
   1060  1.1  mrg 
   1061  1.1  mrg   n = TREE_OPERAND_LENGTH (chrec);
   1062  1.1  mrg   for (i = 0; i < n; i++)
   1063  1.1  mrg     if (chrec_contains_undetermined (TREE_OPERAND (chrec, i), visited))
   1064  1.1  mrg       return true;
   1065  1.1  mrg   return false;
   1066  1.1  mrg }
   1067  1.1  mrg 
   1068  1.1  mrg bool
   1069  1.1  mrg chrec_contains_undetermined (const_tree chrec)
   1070  1.1  mrg {
   1071  1.1  mrg   hash_set<const_tree> visited;
   1072  1.1  mrg   return chrec_contains_undetermined (chrec, visited);
   1073  1.1  mrg }
   1074  1.1  mrg 
   1075  1.1  mrg /* Determines whether the tree EXPR contains chrecs, and increment
   1076  1.1  mrg    SIZE if it is not a NULL pointer by an estimation of the depth of
   1077  1.1  mrg    the tree.  */
   1078  1.1  mrg 
   1079  1.1  mrg static bool
   1080  1.1  mrg tree_contains_chrecs (const_tree expr, int *size, hash_set<const_tree> &visited)
   1081  1.1  mrg {
   1082  1.1  mrg   int i, n;
   1083  1.1  mrg 
   1084  1.1  mrg   if (expr == NULL_TREE)
   1085  1.1  mrg     return false;
   1086  1.1  mrg 
   1087  1.1  mrg   if (size)
   1088  1.1  mrg     (*size)++;
   1089  1.1  mrg 
   1090  1.1  mrg   if (tree_is_chrec (expr))
   1091  1.1  mrg     return true;
   1092  1.1  mrg 
   1093  1.1  mrg   if (visited.add (expr))
   1094  1.1  mrg     return false;
   1095  1.1  mrg 
   1096  1.1  mrg   n = TREE_OPERAND_LENGTH (expr);
   1097  1.1  mrg   for (i = 0; i < n; i++)
   1098  1.1  mrg     if (tree_contains_chrecs (TREE_OPERAND (expr, i), size, visited))
   1099  1.1  mrg       return true;
   1100  1.1  mrg   return false;
   1101  1.1  mrg }
   1102  1.1  mrg 
   1103  1.1  mrg bool
   1104  1.1  mrg tree_contains_chrecs (const_tree expr, int *size)
   1105  1.1  mrg {
   1106  1.1  mrg   hash_set<const_tree> visited;
   1107  1.1  mrg   return tree_contains_chrecs (expr, size, visited);
   1108  1.1  mrg }
   1109  1.1  mrg 
   1110  1.1  mrg 
   1111  1.1  mrg /* Recursive helper function.  */
   1112  1.1  mrg 
   1113  1.1  mrg static bool
   1114  1.1  mrg evolution_function_is_invariant_rec_p (tree chrec, int loopnum)
   1115  1.1  mrg {
   1116  1.1  mrg   if (evolution_function_is_constant_p (chrec))
   1117  1.1  mrg     return true;
   1118  1.1  mrg 
   1119  1.1  mrg   if (TREE_CODE (chrec) == SSA_NAME
   1120  1.1  mrg       && (loopnum == 0
   1121  1.1  mrg 	  || expr_invariant_in_loop_p (get_loop (cfun, loopnum), chrec)))
   1122  1.1  mrg     return true;
   1123  1.1  mrg 
   1124  1.1  mrg   if (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
   1125  1.1  mrg     {
   1126  1.1  mrg       if (CHREC_VARIABLE (chrec) == (unsigned) loopnum
   1127  1.1  mrg 	  || flow_loop_nested_p (get_loop (cfun, loopnum),
   1128  1.1  mrg 				 get_chrec_loop (chrec))
   1129  1.1  mrg 	  || !evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec),
   1130  1.1  mrg 						     loopnum)
   1131  1.1  mrg 	  || !evolution_function_is_invariant_rec_p (CHREC_LEFT (chrec),
   1132  1.1  mrg 						     loopnum))
   1133  1.1  mrg 	return false;
   1134  1.1  mrg       return true;
   1135  1.1  mrg     }
   1136  1.1  mrg 
   1137  1.1  mrg   switch (TREE_OPERAND_LENGTH (chrec))
   1138  1.1  mrg     {
   1139  1.1  mrg     case 2:
   1140  1.1  mrg       if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec, 1),
   1141  1.1  mrg 						  loopnum))
   1142  1.1  mrg 	return false;
   1143  1.1  mrg       /* FALLTHRU */
   1144  1.1  mrg 
   1145  1.1  mrg     case 1:
   1146  1.1  mrg       if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec, 0),
   1147  1.1  mrg 						  loopnum))
   1148  1.1  mrg 	return false;
   1149  1.1  mrg       return true;
   1150  1.1  mrg 
   1151  1.1  mrg     default:
   1152  1.1  mrg       return false;
   1153  1.1  mrg     }
   1154  1.1  mrg }
   1155  1.1  mrg 
   1156  1.1  mrg /* Return true if CHREC is invariant in loop LOOPNUM, false otherwise. */
   1157  1.1  mrg 
   1158  1.1  mrg bool
   1159  1.1  mrg evolution_function_is_invariant_p (tree chrec, int loopnum)
   1160  1.1  mrg {
   1161  1.1  mrg   return evolution_function_is_invariant_rec_p (chrec, loopnum);
   1162  1.1  mrg }
   1163  1.1  mrg 
   1164  1.1  mrg /* Determine whether the given tree is an affine multivariate
   1165  1.1  mrg    evolution.  */
   1166  1.1  mrg 
   1167  1.1  mrg bool
   1168  1.1  mrg evolution_function_is_affine_multivariate_p (const_tree chrec, int loopnum)
   1169  1.1  mrg {
   1170  1.1  mrg   if (chrec == NULL_TREE)
   1171  1.1  mrg     return false;
   1172  1.1  mrg 
   1173  1.1  mrg   switch (TREE_CODE (chrec))
   1174  1.1  mrg     {
   1175  1.1  mrg     case POLYNOMIAL_CHREC:
   1176  1.1  mrg       if (evolution_function_is_invariant_rec_p (CHREC_LEFT (chrec), loopnum))
   1177  1.1  mrg 	{
   1178  1.1  mrg 	  if (evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec), loopnum))
   1179  1.1  mrg 	    return true;
   1180  1.1  mrg 	  else
   1181  1.1  mrg 	    {
   1182  1.1  mrg 	      if (TREE_CODE (CHREC_RIGHT (chrec)) == POLYNOMIAL_CHREC
   1183  1.1  mrg 		  && CHREC_VARIABLE (CHREC_RIGHT (chrec))
   1184  1.1  mrg 		     != CHREC_VARIABLE (chrec)
   1185  1.1  mrg 		  && evolution_function_is_affine_multivariate_p
   1186  1.1  mrg 		  (CHREC_RIGHT (chrec), loopnum))
   1187  1.1  mrg 		return true;
   1188  1.1  mrg 	      else
   1189  1.1  mrg 		return false;
   1190  1.1  mrg 	    }
   1191  1.1  mrg 	}
   1192  1.1  mrg       else
   1193  1.1  mrg 	{
   1194  1.1  mrg 	  if (evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec), loopnum)
   1195  1.1  mrg 	      && TREE_CODE (CHREC_LEFT (chrec)) == POLYNOMIAL_CHREC
   1196  1.1  mrg 	      && CHREC_VARIABLE (CHREC_LEFT (chrec)) != CHREC_VARIABLE (chrec)
   1197  1.1  mrg 	      && evolution_function_is_affine_multivariate_p
   1198  1.1  mrg 	      (CHREC_LEFT (chrec), loopnum))
   1199  1.1  mrg 	    return true;
   1200  1.1  mrg 	  else
   1201  1.1  mrg 	    return false;
   1202  1.1  mrg 	}
   1203  1.1  mrg 
   1204  1.1  mrg     default:
   1205  1.1  mrg       return false;
   1206  1.1  mrg     }
   1207  1.1  mrg }
   1208  1.1  mrg 
   1209  1.1  mrg /* Determine whether the given tree is a function in zero or one
   1210  1.1  mrg    variables with respect to loop specified by LOOPNUM.  Note only positive
   1211  1.1  mrg    LOOPNUM stands for a real loop.  */
   1212  1.1  mrg 
   1213  1.1  mrg bool
   1214  1.1  mrg evolution_function_is_univariate_p (const_tree chrec, int loopnum)
   1215  1.1  mrg {
   1216  1.1  mrg   if (chrec == NULL_TREE)
   1217  1.1  mrg     return true;
   1218  1.1  mrg 
   1219  1.1  mrg   tree sub_chrec;
   1220  1.1  mrg   switch (TREE_CODE (chrec))
   1221  1.1  mrg     {
   1222  1.1  mrg     case POLYNOMIAL_CHREC:
   1223  1.1  mrg       switch (TREE_CODE (CHREC_LEFT (chrec)))
   1224  1.1  mrg 	{
   1225  1.1  mrg 	case POLYNOMIAL_CHREC:
   1226  1.1  mrg 	  sub_chrec = CHREC_LEFT (chrec);
   1227  1.1  mrg 	  if (CHREC_VARIABLE (chrec) != CHREC_VARIABLE (sub_chrec)
   1228  1.1  mrg 	      && (loopnum <= 0
   1229  1.1  mrg 		  || CHREC_VARIABLE (sub_chrec) == (unsigned) loopnum
   1230  1.1  mrg 		  || flow_loop_nested_p (get_loop (cfun, loopnum),
   1231  1.1  mrg 					 get_chrec_loop (sub_chrec))))
   1232  1.1  mrg 	    return false;
   1233  1.1  mrg 	  if (!evolution_function_is_univariate_p (sub_chrec, loopnum))
   1234  1.1  mrg 	    return false;
   1235  1.1  mrg 	  break;
   1236  1.1  mrg 
   1237  1.1  mrg 	default:
   1238  1.1  mrg 	  if (tree_contains_chrecs (CHREC_LEFT (chrec), NULL))
   1239  1.1  mrg 	    return false;
   1240  1.1  mrg 	  break;
   1241  1.1  mrg 	}
   1242  1.1  mrg 
   1243  1.1  mrg       switch (TREE_CODE (CHREC_RIGHT (chrec)))
   1244  1.1  mrg 	{
   1245  1.1  mrg 	case POLYNOMIAL_CHREC:
   1246  1.1  mrg 	  sub_chrec = CHREC_RIGHT (chrec);
   1247  1.1  mrg 	  if (CHREC_VARIABLE (chrec) != CHREC_VARIABLE (sub_chrec)
   1248  1.1  mrg 	      && (loopnum <= 0
   1249  1.1  mrg 		  || CHREC_VARIABLE (sub_chrec) == (unsigned) loopnum
   1250  1.1  mrg 		  || flow_loop_nested_p (get_loop (cfun, loopnum),
   1251  1.1  mrg 					 get_chrec_loop (sub_chrec))))
   1252  1.1  mrg 	    return false;
   1253  1.1  mrg 	  if (!evolution_function_is_univariate_p (sub_chrec, loopnum))
   1254  1.1  mrg 	    return false;
   1255  1.1  mrg 	  break;
   1256  1.1  mrg 
   1257  1.1  mrg 	default:
   1258  1.1  mrg 	  if (tree_contains_chrecs (CHREC_RIGHT (chrec), NULL))
   1259  1.1  mrg 	    return false;
   1260  1.1  mrg 	  break;
   1261  1.1  mrg 	}
   1262  1.1  mrg       return true;
   1263  1.1  mrg 
   1264  1.1  mrg     default:
   1265  1.1  mrg       return true;
   1266  1.1  mrg     }
   1267  1.1  mrg }
   1268  1.1  mrg 
   1269  1.1  mrg /* Returns the number of variables of CHREC.  Example: the call
   1270  1.1  mrg    nb_vars_in_chrec ({{0, +, 1}_5, +, 2}_6) returns 2.  */
   1271  1.1  mrg 
   1272  1.1  mrg unsigned
   1273  1.1  mrg nb_vars_in_chrec (tree chrec)
   1274  1.1  mrg {
   1275  1.1  mrg   if (chrec == NULL_TREE)
   1276  1.1  mrg     return 0;
   1277  1.1  mrg 
   1278  1.1  mrg   switch (TREE_CODE (chrec))
   1279  1.1  mrg     {
   1280  1.1  mrg     case POLYNOMIAL_CHREC:
   1281  1.1  mrg       return 1 + nb_vars_in_chrec
   1282  1.1  mrg 	(initial_condition_in_loop_num (chrec, CHREC_VARIABLE (chrec)));
   1283  1.1  mrg 
   1284  1.1  mrg     default:
   1285  1.1  mrg       return 0;
   1286  1.1  mrg     }
   1287  1.1  mrg }
   1288  1.1  mrg 
   1289  1.1  mrg /* Converts BASE and STEP of affine scev to TYPE.  LOOP is the loop whose iv
   1290  1.1  mrg    the scev corresponds to.  AT_STMT is the statement at that the scev is
   1291  1.1  mrg    evaluated.  USE_OVERFLOW_SEMANTICS is true if this function should assume
   1292  1.1  mrg    that the rules for overflow of the given language apply (e.g., that signed
   1293  1.1  mrg    arithmetics in C does not overflow) -- i.e., to use them to avoid
   1294  1.1  mrg    unnecessary tests, but also to enforce that the result follows them.
   1295  1.1  mrg    FROM is the source variable converted if it's not NULL.  Returns true if
   1296  1.1  mrg    the conversion succeeded, false otherwise.  */
   1297  1.1  mrg 
   1298  1.1  mrg bool
   1299  1.1  mrg convert_affine_scev (class loop *loop, tree type,
   1300  1.1  mrg 		     tree *base, tree *step, gimple *at_stmt,
   1301  1.1  mrg 		     bool use_overflow_semantics, tree from)
   1302  1.1  mrg {
   1303  1.1  mrg   tree ct = TREE_TYPE (*step);
   1304  1.1  mrg   bool enforce_overflow_semantics;
   1305  1.1  mrg   bool must_check_src_overflow, must_check_rslt_overflow;
   1306  1.1  mrg   tree new_base, new_step;
   1307  1.1  mrg   tree step_type = POINTER_TYPE_P (type) ? sizetype : type;
   1308  1.1  mrg 
   1309  1.1  mrg   /* In general,
   1310  1.1  mrg      (TYPE) (BASE + STEP * i) = (TYPE) BASE + (TYPE -- sign extend) STEP * i,
   1311  1.1  mrg      but we must check some assumptions.
   1312  1.1  mrg 
   1313  1.1  mrg      1) If [BASE, +, STEP] wraps, the equation is not valid when precision
   1314  1.1  mrg         of CT is smaller than the precision of TYPE.  For example, when we
   1315  1.1  mrg 	cast unsigned char [254, +, 1] to unsigned, the values on left side
   1316  1.1  mrg 	are 254, 255, 0, 1, ..., but those on the right side are
   1317  1.1  mrg 	254, 255, 256, 257, ...
   1318  1.1  mrg      2) In case that we must also preserve the fact that signed ivs do not
   1319  1.1  mrg         overflow, we must additionally check that the new iv does not wrap.
   1320  1.1  mrg 	For example, unsigned char [125, +, 1] casted to signed char could
   1321  1.1  mrg 	become a wrapping variable with values 125, 126, 127, -128, -127, ...,
   1322  1.1  mrg 	which would confuse optimizers that assume that this does not
   1323  1.1  mrg 	happen.  */
   1324  1.1  mrg   must_check_src_overflow = TYPE_PRECISION (ct) < TYPE_PRECISION (type);
   1325  1.1  mrg 
   1326  1.1  mrg   enforce_overflow_semantics = (use_overflow_semantics
   1327  1.1  mrg 				&& nowrap_type_p (type));
   1328  1.1  mrg   if (enforce_overflow_semantics)
   1329  1.1  mrg     {
   1330  1.1  mrg       /* We can avoid checking whether the result overflows in the following
   1331  1.1  mrg 	 cases:
   1332  1.1  mrg 
   1333  1.1  mrg 	 -- must_check_src_overflow is true, and the range of TYPE is superset
   1334  1.1  mrg 	    of the range of CT -- i.e., in all cases except if CT signed and
   1335  1.1  mrg 	    TYPE unsigned.
   1336  1.1  mrg          -- both CT and TYPE have the same precision and signedness, and we
   1337  1.1  mrg 	    verify instead that the source does not overflow (this may be
   1338  1.1  mrg 	    easier than verifying it for the result, as we may use the
   1339  1.1  mrg 	    information about the semantics of overflow in CT).  */
   1340  1.1  mrg       if (must_check_src_overflow)
   1341  1.1  mrg 	{
   1342  1.1  mrg 	  if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (ct))
   1343  1.1  mrg 	    must_check_rslt_overflow = true;
   1344  1.1  mrg 	  else
   1345  1.1  mrg 	    must_check_rslt_overflow = false;
   1346  1.1  mrg 	}
   1347  1.1  mrg       else if (TYPE_UNSIGNED (ct) == TYPE_UNSIGNED (type)
   1348  1.1  mrg 	       && TYPE_PRECISION (ct) == TYPE_PRECISION (type))
   1349  1.1  mrg 	{
   1350  1.1  mrg 	  must_check_rslt_overflow = false;
   1351  1.1  mrg 	  must_check_src_overflow = true;
   1352  1.1  mrg 	}
   1353  1.1  mrg       else
   1354  1.1  mrg 	must_check_rslt_overflow = true;
   1355  1.1  mrg     }
   1356  1.1  mrg   else
   1357  1.1  mrg     must_check_rslt_overflow = false;
   1358  1.1  mrg 
   1359  1.1  mrg   if (must_check_src_overflow
   1360  1.1  mrg       && scev_probably_wraps_p (from, *base, *step, at_stmt, loop,
   1361  1.1  mrg 				use_overflow_semantics))
   1362  1.1  mrg     return false;
   1363  1.1  mrg 
   1364  1.1  mrg   new_base = chrec_convert (type, *base, at_stmt, use_overflow_semantics);
   1365  1.1  mrg   /* The step must be sign extended, regardless of the signedness
   1366  1.1  mrg      of CT and TYPE.  This only needs to be handled specially when
   1367  1.1  mrg      CT is unsigned -- to avoid e.g. unsigned char [100, +, 255]
   1368  1.1  mrg      (with values 100, 99, 98, ...) from becoming signed or unsigned
   1369  1.1  mrg      [100, +, 255] with values 100, 355, ...; the sign-extension is
   1370  1.1  mrg      performed by default when CT is signed.  */
   1371  1.1  mrg   new_step = *step;
   1372  1.1  mrg   if (TYPE_PRECISION (step_type) > TYPE_PRECISION (ct) && TYPE_UNSIGNED (ct))
   1373  1.1  mrg     {
   1374  1.1  mrg       tree signed_ct = build_nonstandard_integer_type (TYPE_PRECISION (ct), 0);
   1375  1.1  mrg       new_step = chrec_convert (signed_ct, new_step, at_stmt,
   1376  1.1  mrg                                 use_overflow_semantics);
   1377  1.1  mrg     }
   1378  1.1  mrg   new_step = chrec_convert (step_type, new_step, at_stmt,
   1379  1.1  mrg 			    use_overflow_semantics);
   1380  1.1  mrg 
   1381  1.1  mrg   if (automatically_generated_chrec_p (new_base)
   1382  1.1  mrg       || automatically_generated_chrec_p (new_step))
   1383  1.1  mrg     return false;
   1384  1.1  mrg 
   1385  1.1  mrg   if (must_check_rslt_overflow
   1386  1.1  mrg       /* Note that in this case we cannot use the fact that signed variables
   1387  1.1  mrg 	 do not overflow, as this is what we are verifying for the new iv.  */
   1388  1.1  mrg       && scev_probably_wraps_p (NULL_TREE, new_base, new_step,
   1389  1.1  mrg 				at_stmt, loop, false))
   1390  1.1  mrg     return false;
   1391  1.1  mrg 
   1392  1.1  mrg   *base = new_base;
   1393  1.1  mrg   *step = new_step;
   1394  1.1  mrg   return true;
   1395  1.1  mrg }
   1396  1.1  mrg 
   1397  1.1  mrg 
   1399  1.1  mrg /* Convert CHREC for the right hand side of a CHREC.
   1400  1.1  mrg    The increment for a pointer type is always sizetype.  */
   1401  1.1  mrg 
   1402  1.1  mrg tree
   1403  1.1  mrg chrec_convert_rhs (tree type, tree chrec, gimple *at_stmt)
   1404  1.1  mrg {
   1405  1.1  mrg   if (POINTER_TYPE_P (type))
   1406  1.1  mrg     type = sizetype;
   1407  1.1  mrg 
   1408  1.1  mrg   return chrec_convert (type, chrec, at_stmt);
   1409  1.1  mrg }
   1410  1.1  mrg 
   1411  1.1  mrg /* Convert CHREC to TYPE.  When the analyzer knows the context in
   1412  1.1  mrg    which the CHREC is built, it sets AT_STMT to the statement that
   1413  1.1  mrg    contains the definition of the analyzed variable, otherwise the
   1414  1.1  mrg    conversion is less accurate: the information is used for
   1415  1.1  mrg    determining a more accurate estimation of the number of iterations.
   1416  1.1  mrg    By default AT_STMT could be safely set to NULL_TREE.
   1417  1.1  mrg 
   1418  1.1  mrg    USE_OVERFLOW_SEMANTICS is true if this function should assume that
   1419  1.1  mrg    the rules for overflow of the given language apply (e.g., that signed
   1420  1.1  mrg    arithmetics in C does not overflow) -- i.e., to use them to avoid
   1421  1.1  mrg    unnecessary tests, but also to enforce that the result follows them.
   1422  1.1  mrg 
   1423  1.1  mrg    FROM is the source variable converted if it's not NULL.  */
   1424  1.1  mrg 
   1425  1.1  mrg static tree
   1426  1.1  mrg chrec_convert_1 (tree type, tree chrec, gimple *at_stmt,
   1427  1.1  mrg 		 bool use_overflow_semantics, tree from)
   1428  1.1  mrg {
   1429  1.1  mrg   tree ct, res;
   1430  1.1  mrg   tree base, step;
   1431  1.1  mrg   class loop *loop;
   1432  1.1  mrg 
   1433  1.1  mrg   if (automatically_generated_chrec_p (chrec))
   1434  1.1  mrg     return chrec;
   1435  1.1  mrg 
   1436  1.1  mrg   ct = chrec_type (chrec);
   1437  1.1  mrg   if (useless_type_conversion_p (type, ct))
   1438  1.1  mrg     return chrec;
   1439  1.1  mrg 
   1440  1.1  mrg   if (!evolution_function_is_affine_p (chrec))
   1441  1.1  mrg     goto keep_cast;
   1442  1.1  mrg 
   1443  1.1  mrg   loop = get_chrec_loop (chrec);
   1444  1.1  mrg   base = CHREC_LEFT (chrec);
   1445  1.1  mrg   step = CHREC_RIGHT (chrec);
   1446  1.1  mrg 
   1447  1.1  mrg   if (convert_affine_scev (loop, type, &base, &step, at_stmt,
   1448  1.1  mrg 			   use_overflow_semantics, from))
   1449  1.1  mrg     return build_polynomial_chrec (loop->num, base, step);
   1450  1.1  mrg 
   1451  1.1  mrg   /* If we cannot propagate the cast inside the chrec, just keep the cast.  */
   1452  1.1  mrg keep_cast:
   1453  1.1  mrg   /* Fold will not canonicalize (long)(i - 1) to (long)i - 1 because that
   1454  1.1  mrg      may be more expensive.  We do want to perform this optimization here
   1455  1.1  mrg      though for canonicalization reasons.  */
   1456  1.1  mrg   if (use_overflow_semantics
   1457  1.1  mrg       && (TREE_CODE (chrec) == PLUS_EXPR
   1458  1.1  mrg 	  || TREE_CODE (chrec) == MINUS_EXPR)
   1459  1.1  mrg       && TREE_CODE (type) == INTEGER_TYPE
   1460  1.1  mrg       && TREE_CODE (ct) == INTEGER_TYPE
   1461  1.1  mrg       && TYPE_PRECISION (type) > TYPE_PRECISION (ct)
   1462  1.1  mrg       && TYPE_OVERFLOW_UNDEFINED (ct))
   1463  1.1  mrg     res = fold_build2 (TREE_CODE (chrec), type,
   1464  1.1  mrg 		       fold_convert (type, TREE_OPERAND (chrec, 0)),
   1465  1.1  mrg 		       fold_convert (type, TREE_OPERAND (chrec, 1)));
   1466  1.1  mrg   /* Similar perform the trick that (signed char)((int)x + 2) can be
   1467  1.1  mrg      narrowed to (signed char)((unsigned char)x + 2).  */
   1468  1.1  mrg   else if (use_overflow_semantics
   1469  1.1  mrg 	   && TREE_CODE (chrec) == POLYNOMIAL_CHREC
   1470  1.1  mrg 	   && TREE_CODE (ct) == INTEGER_TYPE
   1471  1.1  mrg 	   && TREE_CODE (type) == INTEGER_TYPE
   1472  1.1  mrg 	   && TYPE_OVERFLOW_UNDEFINED (type)
   1473  1.1  mrg 	   && TYPE_PRECISION (type) < TYPE_PRECISION (ct))
   1474  1.1  mrg     {
   1475  1.1  mrg       tree utype = unsigned_type_for (type);
   1476  1.1  mrg       res = build_polynomial_chrec (CHREC_VARIABLE (chrec),
   1477  1.1  mrg 				    fold_convert (utype,
   1478  1.1  mrg 						  CHREC_LEFT (chrec)),
   1479  1.1  mrg 				    fold_convert (utype,
   1480  1.1  mrg 						  CHREC_RIGHT (chrec)));
   1481  1.1  mrg       res = chrec_convert_1 (type, res, at_stmt, use_overflow_semantics, from);
   1482  1.1  mrg     }
   1483  1.1  mrg   else
   1484  1.1  mrg     res = fold_convert (type, chrec);
   1485  1.1  mrg 
   1486  1.1  mrg   /* Don't propagate overflows.  */
   1487  1.1  mrg   if (CONSTANT_CLASS_P (res))
   1488  1.1  mrg     TREE_OVERFLOW (res) = 0;
   1489  1.1  mrg 
   1490  1.1  mrg   /* But reject constants that don't fit in their type after conversion.
   1491  1.1  mrg      This can happen if TYPE_MIN_VALUE or TYPE_MAX_VALUE are not the
   1492  1.1  mrg      natural values associated with TYPE_PRECISION and TYPE_UNSIGNED,
   1493  1.1  mrg      and can cause problems later when computing niters of loops.  Note
   1494  1.1  mrg      that we don't do the check before converting because we don't want
   1495  1.1  mrg      to reject conversions of negative chrecs to unsigned types.  */
   1496  1.1  mrg   if (TREE_CODE (res) == INTEGER_CST
   1497  1.1  mrg       && TREE_CODE (type) == INTEGER_TYPE
   1498  1.1  mrg       && !int_fits_type_p (res, type))
   1499  1.1  mrg     res = chrec_dont_know;
   1500  1.1  mrg 
   1501  1.1  mrg   return res;
   1502  1.1  mrg }
   1503  1.1  mrg 
   1504  1.1  mrg /* Convert CHREC to TYPE.  When the analyzer knows the context in
   1505  1.1  mrg    which the CHREC is built, it sets AT_STMT to the statement that
   1506  1.1  mrg    contains the definition of the analyzed variable, otherwise the
   1507  1.1  mrg    conversion is less accurate: the information is used for
   1508  1.1  mrg    determining a more accurate estimation of the number of iterations.
   1509  1.1  mrg    By default AT_STMT could be safely set to NULL_TREE.
   1510  1.1  mrg 
   1511  1.1  mrg    The following rule is always true: TREE_TYPE (chrec) ==
   1512  1.1  mrg    TREE_TYPE (CHREC_LEFT (chrec)) == TREE_TYPE (CHREC_RIGHT (chrec)).
   1513  1.1  mrg    An example of what could happen when adding two chrecs and the type
   1514  1.1  mrg    of the CHREC_RIGHT is different than CHREC_LEFT is:
   1515  1.1  mrg 
   1516  1.1  mrg    {(uint) 0, +, (uchar) 10} +
   1517  1.1  mrg    {(uint) 0, +, (uchar) 250}
   1518  1.1  mrg 
   1519  1.1  mrg    that would produce a wrong result if CHREC_RIGHT is not (uint):
   1520  1.1  mrg 
   1521  1.1  mrg    {(uint) 0, +, (uchar) 4}
   1522  1.1  mrg 
   1523  1.1  mrg    instead of
   1524  1.1  mrg 
   1525  1.1  mrg    {(uint) 0, +, (uint) 260}
   1526  1.1  mrg 
   1527  1.1  mrg    USE_OVERFLOW_SEMANTICS is true if this function should assume that
   1528  1.1  mrg    the rules for overflow of the given language apply (e.g., that signed
   1529  1.1  mrg    arithmetics in C does not overflow) -- i.e., to use them to avoid
   1530  1.1  mrg    unnecessary tests, but also to enforce that the result follows them.
   1531  1.1  mrg 
   1532  1.1  mrg    FROM is the source variable converted if it's not NULL.  */
   1533  1.1  mrg 
   1534  1.1  mrg tree
   1535  1.1  mrg chrec_convert (tree type, tree chrec, gimple *at_stmt,
   1536  1.1  mrg 	       bool use_overflow_semantics, tree from)
   1537  1.1  mrg {
   1538  1.1  mrg   return chrec_convert_1 (type, chrec, at_stmt, use_overflow_semantics, from);
   1539  1.1  mrg }
   1540  1.1  mrg 
   1541  1.1  mrg /* Convert CHREC to TYPE, without regard to signed overflows.  Returns the new
   1542  1.1  mrg    chrec if something else than what chrec_convert would do happens, NULL_TREE
   1543  1.1  mrg    otherwise.  This function set TRUE to variable pointed by FOLD_CONVERSIONS
   1544  1.1  mrg    if the result chrec may overflow.  */
   1545  1.1  mrg 
   1546  1.1  mrg tree
   1547  1.1  mrg chrec_convert_aggressive (tree type, tree chrec, bool *fold_conversions)
   1548  1.1  mrg {
   1549  1.1  mrg   tree inner_type, left, right, lc, rc, rtype;
   1550  1.1  mrg 
   1551  1.1  mrg   gcc_assert (fold_conversions != NULL);
   1552  1.1  mrg 
   1553  1.1  mrg   if (automatically_generated_chrec_p (chrec)
   1554  1.1  mrg       || TREE_CODE (chrec) != POLYNOMIAL_CHREC)
   1555  1.1  mrg     return NULL_TREE;
   1556  1.1  mrg 
   1557  1.1  mrg   inner_type = TREE_TYPE (chrec);
   1558  1.1  mrg   if (TYPE_PRECISION (type) > TYPE_PRECISION (inner_type))
   1559  1.1  mrg     return NULL_TREE;
   1560  1.1  mrg 
   1561  1.1  mrg   if (useless_type_conversion_p (type, inner_type))
   1562  1.1  mrg     return NULL_TREE;
   1563  1.1  mrg 
   1564  1.1  mrg   if (!*fold_conversions && evolution_function_is_affine_p (chrec))
   1565  1.1  mrg     {
   1566  1.1  mrg       tree base, step;
   1567  1.1  mrg       class loop *loop;
   1568  1.1  mrg 
   1569  1.1  mrg       loop = get_chrec_loop (chrec);
   1570  1.1  mrg       base = CHREC_LEFT (chrec);
   1571  1.1  mrg       step = CHREC_RIGHT (chrec);
   1572  1.1  mrg       if (convert_affine_scev (loop, type, &base, &step, NULL, true))
   1573  1.1  mrg 	return build_polynomial_chrec (loop->num, base, step);
   1574  1.1  mrg     }
   1575  1.1  mrg   rtype = POINTER_TYPE_P (type) ? sizetype : type;
   1576  1.1  mrg 
   1577  1.1  mrg   left = CHREC_LEFT (chrec);
   1578  1.1  mrg   right = CHREC_RIGHT (chrec);
   1579  1.1  mrg   lc = chrec_convert_aggressive (type, left, fold_conversions);
   1580  1.1  mrg   if (!lc)
   1581  1.1  mrg     lc = chrec_convert (type, left, NULL);
   1582  1.1  mrg   rc = chrec_convert_aggressive (rtype, right, fold_conversions);
   1583  1.1  mrg   if (!rc)
   1584  1.1  mrg     rc = chrec_convert (rtype, right, NULL);
   1585  1.1  mrg 
   1586  1.1  mrg   *fold_conversions = true;
   1587  1.1  mrg 
   1588  1.1  mrg   return build_polynomial_chrec (CHREC_VARIABLE (chrec), lc, rc);
   1589  1.1  mrg }
   1590  1.1  mrg 
   1591  1.1  mrg /* Returns true when CHREC0 == CHREC1.  */
   1592  1.1  mrg 
   1593  1.1  mrg bool
   1594  1.1  mrg eq_evolutions_p (const_tree chrec0, const_tree chrec1)
   1595  1.1  mrg {
   1596  1.1  mrg   if (chrec0 == NULL_TREE
   1597  1.1  mrg       || chrec1 == NULL_TREE
   1598  1.1  mrg       || TREE_CODE (chrec0) != TREE_CODE (chrec1))
   1599  1.1  mrg     return false;
   1600  1.1  mrg 
   1601  1.1  mrg   if (operand_equal_p (chrec0, chrec1, 0))
   1602  1.1  mrg     return true;
   1603  1.1  mrg 
   1604  1.1  mrg   if (! types_compatible_p (TREE_TYPE (chrec0), TREE_TYPE (chrec1)))
   1605  1.1  mrg     return false;
   1606  1.1  mrg 
   1607  1.1  mrg   switch (TREE_CODE (chrec0))
   1608  1.1  mrg     {
   1609  1.1  mrg     case POLYNOMIAL_CHREC:
   1610  1.1  mrg       return (CHREC_VARIABLE (chrec0) == CHREC_VARIABLE (chrec1)
   1611  1.1  mrg 	      && eq_evolutions_p (CHREC_LEFT (chrec0), CHREC_LEFT (chrec1))
   1612  1.1  mrg 	      && eq_evolutions_p (CHREC_RIGHT (chrec0), CHREC_RIGHT (chrec1)));
   1613  1.1  mrg 
   1614  1.1  mrg     case PLUS_EXPR:
   1615  1.1  mrg     case MULT_EXPR:
   1616  1.1  mrg     case MINUS_EXPR:
   1617  1.1  mrg     case POINTER_PLUS_EXPR:
   1618  1.1  mrg       return eq_evolutions_p (TREE_OPERAND (chrec0, 0),
   1619  1.1  mrg 			      TREE_OPERAND (chrec1, 0))
   1620  1.1  mrg 	  && eq_evolutions_p (TREE_OPERAND (chrec0, 1),
   1621  1.1  mrg 			      TREE_OPERAND (chrec1, 1));
   1622  1.1  mrg 
   1623  1.1  mrg     CASE_CONVERT:
   1624  1.1  mrg       return eq_evolutions_p (TREE_OPERAND (chrec0, 0),
   1625  1.1  mrg 			      TREE_OPERAND (chrec1, 0));
   1626  1.1  mrg 
   1627  1.1  mrg     default:
   1628  1.1  mrg       return false;
   1629  1.1  mrg     }
   1630  1.1  mrg }
   1631  1.1  mrg 
   1632  1.1  mrg /* Returns EV_GROWS if CHREC grows (assuming that it does not overflow),
   1633  1.1  mrg    EV_DECREASES if it decreases, and EV_UNKNOWN if we cannot determine
   1634  1.1  mrg    which of these cases happens.  */
   1635  1.1  mrg 
   1636  1.1  mrg enum ev_direction
   1637  1.1  mrg scev_direction (const_tree chrec)
   1638  1.1  mrg {
   1639  1.1  mrg   const_tree step;
   1640  1.1  mrg 
   1641  1.1  mrg   if (!evolution_function_is_affine_p (chrec))
   1642  1.1  mrg     return EV_DIR_UNKNOWN;
   1643  1.1  mrg 
   1644  1.1  mrg   step = CHREC_RIGHT (chrec);
   1645  1.1  mrg   if (TREE_CODE (step) != INTEGER_CST)
   1646  1.1  mrg     return EV_DIR_UNKNOWN;
   1647  1.1  mrg 
   1648  1.1  mrg   if (tree_int_cst_sign_bit (step))
   1649  1.1  mrg     return EV_DIR_DECREASES;
   1650  1.1  mrg   else
   1651  1.1  mrg     return EV_DIR_GROWS;
   1652  1.1  mrg }
   1653  1.1  mrg 
   1654  1.1  mrg /* Iterates over all the components of SCEV, and calls CBCK.  */
   1655  1.1  mrg 
   1656  1.1  mrg void
   1657  1.1  mrg for_each_scev_op (tree *scev, bool (*cbck) (tree *, void *), void *data)
   1658  1.1  mrg {
   1659  1.1  mrg   switch (TREE_CODE_LENGTH (TREE_CODE (*scev)))
   1660  1.1  mrg     {
   1661  1.1  mrg     case 3:
   1662  1.1  mrg       for_each_scev_op (&TREE_OPERAND (*scev, 2), cbck, data);
   1663  1.1  mrg       /* FALLTHRU */
   1664  1.1  mrg 
   1665  1.1  mrg     case 2:
   1666  1.1  mrg       for_each_scev_op (&TREE_OPERAND (*scev, 1), cbck, data);
   1667  1.1  mrg       /* FALLTHRU */
   1668  1.1  mrg 
   1669  1.1  mrg     case 1:
   1670  1.1  mrg       for_each_scev_op (&TREE_OPERAND (*scev, 0), cbck, data);
   1671  1.1  mrg       /* FALLTHRU */
   1672  1.1  mrg 
   1673  1.1  mrg     default:
   1674  1.1  mrg       cbck (scev, data);
   1675  1.1  mrg       break;
   1676  1.1  mrg     }
   1677  1.1  mrg }
   1678  1.1  mrg 
   1679  1.1  mrg /* Returns true when the operation can be part of a linear
   1680  1.1  mrg    expression.  */
   1681  1.1  mrg 
   1682  1.1  mrg static inline bool
   1683  1.1  mrg operator_is_linear (tree scev)
   1684  1.1  mrg {
   1685  1.1  mrg   switch (TREE_CODE (scev))
   1686  1.1  mrg     {
   1687  1.1  mrg     case INTEGER_CST:
   1688  1.1  mrg     case POLYNOMIAL_CHREC:
   1689  1.1  mrg     case PLUS_EXPR:
   1690  1.1  mrg     case POINTER_PLUS_EXPR:
   1691  1.1  mrg     case MULT_EXPR:
   1692  1.1  mrg     case MINUS_EXPR:
   1693  1.1  mrg     case NEGATE_EXPR:
   1694  1.1  mrg     case SSA_NAME:
   1695  1.1  mrg     case NON_LVALUE_EXPR:
   1696  1.1  mrg     case BIT_NOT_EXPR:
   1697  1.1  mrg     CASE_CONVERT:
   1698  1.1  mrg       return true;
   1699  1.1  mrg 
   1700  1.1  mrg     default:
   1701  1.1  mrg       return false;
   1702  1.1  mrg     }
   1703  1.1  mrg }
   1704  1.1  mrg 
   1705  1.1  mrg /* Return true when SCEV is a linear expression.  Linear expressions
   1706  1.1  mrg    can contain additions, substractions and multiplications.
   1707  1.1  mrg    Multiplications are restricted to constant scaling: "cst * x".  */
   1708  1.1  mrg 
   1709  1.1  mrg bool
   1710  1.1  mrg scev_is_linear_expression (tree scev)
   1711  1.1  mrg {
   1712  1.1  mrg   if (evolution_function_is_constant_p (scev))
   1713  1.1  mrg     return true;
   1714  1.1  mrg 
   1715  1.1  mrg   if (scev == NULL
   1716  1.1  mrg       || !operator_is_linear (scev))
   1717  1.1  mrg     return false;
   1718  1.1  mrg 
   1719  1.1  mrg   if (TREE_CODE (scev) == MULT_EXPR)
   1720  1.1  mrg     return !(tree_contains_chrecs (TREE_OPERAND (scev, 0), NULL)
   1721  1.1  mrg 	     && tree_contains_chrecs (TREE_OPERAND (scev, 1), NULL));
   1722  1.1  mrg 
   1723  1.1  mrg   if (TREE_CODE (scev) == POLYNOMIAL_CHREC
   1724  1.1  mrg       && !evolution_function_is_affine_multivariate_p (scev, CHREC_VARIABLE (scev)))
   1725  1.1  mrg     return false;
   1726  1.1  mrg 
   1727  1.1  mrg   switch (TREE_CODE_LENGTH (TREE_CODE (scev)))
   1728  1.1  mrg     {
   1729  1.1  mrg     case 3:
   1730  1.1  mrg       return scev_is_linear_expression (TREE_OPERAND (scev, 0))
   1731  1.1  mrg 	&& scev_is_linear_expression (TREE_OPERAND (scev, 1))
   1732  1.1  mrg 	&& scev_is_linear_expression (TREE_OPERAND (scev, 2));
   1733  1.1  mrg 
   1734  1.1  mrg     case 2:
   1735  1.1  mrg       return scev_is_linear_expression (TREE_OPERAND (scev, 0))
   1736  1.1  mrg 	&& scev_is_linear_expression (TREE_OPERAND (scev, 1));
   1737  1.1  mrg 
   1738  1.1  mrg     case 1:
   1739  1.1  mrg       return scev_is_linear_expression (TREE_OPERAND (scev, 0));
   1740  1.1  mrg 
   1741  1.1  mrg     case 0:
   1742  1.1  mrg       return true;
   1743  1.1  mrg 
   1744  1.1  mrg     default:
   1745  1.1  mrg       return false;
   1746  1.1  mrg     }
   1747  1.1  mrg }
   1748  1.1  mrg 
   1749  1.1  mrg /* Determines whether the expression CHREC contains only interger consts
   1750  1.1  mrg    in the right parts.  */
   1751  1.1  mrg 
   1752  1.1  mrg bool
   1753  1.1  mrg evolution_function_right_is_integer_cst (const_tree chrec)
   1754  1.1  mrg {
   1755  1.1  mrg   if (chrec == NULL_TREE)
   1756  1.1  mrg     return false;
   1757  1.1  mrg 
   1758  1.1  mrg   switch (TREE_CODE (chrec))
   1759  1.1  mrg     {
   1760  1.1  mrg     case INTEGER_CST:
   1761  1.1  mrg       return true;
   1762  1.1  mrg 
   1763  1.1  mrg     case POLYNOMIAL_CHREC:
   1764  1.1  mrg       return TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST
   1765  1.1  mrg 	&& (TREE_CODE (CHREC_LEFT (chrec)) != POLYNOMIAL_CHREC
   1766  1.1  mrg 	    || evolution_function_right_is_integer_cst (CHREC_LEFT (chrec)));
   1767  1.1  mrg 
   1768  1.1  mrg     CASE_CONVERT:
   1769  1.1  mrg       return evolution_function_right_is_integer_cst (TREE_OPERAND (chrec, 0));
   1770  1.1  mrg 
   1771               default:
   1772                 return false;
   1773               }
   1774           }
   1775