LCOV - code coverage report
Current view: top level - gcc - tree-affine.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 90.3 % 619 559
Test Date: 2026-12-05 16:10:02 Functions: 93.1 % 29 27
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Operations with affine combinations of trees.
       2              :    Copyright (C) 2005-2026 Free Software Foundation, Inc.
       3              : 
       4              : This file is part of GCC.
       5              : 
       6              : GCC is free software; you can redistribute it and/or modify it
       7              : under the terms of the GNU General Public License as published by the
       8              : Free Software Foundation; either version 3, or (at your option) any
       9              : later version.
      10              : 
      11              : GCC is distributed in the hope that it will be useful, but WITHOUT
      12              : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14              : for more details.
      15              : 
      16              : You should have received a copy of the GNU General Public License
      17              : along with GCC; see the file COPYING3.  If not see
      18              : <http://www.gnu.org/licenses/>.  */
      19              : 
      20              : #include "config.h"
      21              : #include "system.h"
      22              : #include "coretypes.h"
      23              : #include "backend.h"
      24              : #include "rtl.h"
      25              : #include "tree.h"
      26              : #include "gimple.h"
      27              : #include "ssa.h"
      28              : #include "tree-pretty-print.h"
      29              : #include "fold-const.h"
      30              : #include "tree-affine.h"
      31              : #include "gimplify.h"
      32              : #include "dumpfile.h"
      33              : #include "cfgexpand.h"
      34              : #include "value-query.h"
      35              : #include "vr-values.h"
      36              : #include "range-op.h"
      37              : 
      38              : /* Extends CST as appropriate for the affine combinations COMB.  */
      39              : 
      40              : static widest_int
      41    144507707 : wide_int_ext_for_comb (const widest_int &cst, tree type)
      42              : {
      43    144507707 :   return wi::sext (cst, TYPE_PRECISION (type));
      44              : }
      45              : 
      46              : /* Likewise for polynomial offsets.  */
      47              : 
      48              : static poly_widest_int
      49    210379006 : wide_int_ext_for_comb (const poly_widest_int &cst, tree type)
      50              : {
      51    210379006 :   return wi::sext (cst, TYPE_PRECISION (type));
      52              : }
      53              : 
      54              : /* Initializes affine combination COMB so that its value is zero in TYPE.  */
      55              : 
      56              : static void
      57    172865521 : aff_combination_zero (aff_tree *comb, tree type)
      58              : {
      59    172865521 :   int i;
      60    172865521 :   comb->type = type;
      61    172865521 :   comb->offset = 0;
      62    172865521 :   comb->n = 0;
      63   1555789689 :   for (i = 0; i < MAX_AFF_ELTS; i++)
      64   1382924168 :     comb->elts[i].coef = 0;
      65    172865521 :   comb->rest = NULL_TREE;
      66    172865521 : }
      67              : 
      68              : /* Sets COMB to CST.  */
      69              : 
      70              : void
      71     90115932 : aff_combination_const (aff_tree *comb, tree type, const poly_widest_int &cst)
      72              : {
      73     90115932 :   aff_combination_zero (comb, type);
      74     90115932 :   comb->offset = wide_int_ext_for_comb (cst, comb->type);;
      75     90115932 : }
      76              : 
      77              : /* Sets COMB to single element ELT.  */
      78              : 
      79              : void
      80     42509644 : aff_combination_elt (aff_tree *comb, tree type, tree elt)
      81              : {
      82     42509644 :   aff_combination_zero (comb, type);
      83              : 
      84     42509644 :   comb->n = 1;
      85     42509644 :   comb->elts[0].val = elt;
      86     42509644 :   comb->elts[0].coef = 1;
      87     42509644 : }
      88              : 
      89              : /* Scales COMB by SCALE.  */
      90              : 
      91              : void
      92     39165550 : aff_combination_scale (aff_tree *comb, const widest_int &scale_in)
      93              : {
      94     39165550 :   unsigned i, j;
      95              : 
      96     39165550 :   widest_int scale = wide_int_ext_for_comb (scale_in, comb->type);
      97     39165550 :   if (scale == 1)
      98              :     return;
      99              : 
     100     28476221 :   if (scale == 0)
     101              :     {
     102          234 :       aff_combination_zero (comb, comb->type);
     103          234 :       return;
     104              :     }
     105              : 
     106     28475987 :   comb->offset = wide_int_ext_for_comb (scale * comb->offset, comb->type);
     107     51598931 :   for (i = 0, j = 0; i < comb->n; i++)
     108              :     {
     109     23122944 :       widest_int new_coef
     110     23122944 :         = wide_int_ext_for_comb (scale * comb->elts[i].coef, comb->type);
     111              :       /* A coefficient may become zero due to overflow.  Remove the zero
     112              :          elements.  */
     113     23122944 :       if (new_coef == 0)
     114            4 :         continue;
     115     23122940 :       comb->elts[j].coef = new_coef;
     116     23122940 :       comb->elts[j].val = comb->elts[i].val;
     117     23122940 :       j++;
     118     23122944 :     }
     119     28475987 :   comb->n = j;
     120              : 
     121     28475987 :   if (comb->rest)
     122              :     {
     123          166 :       tree type = comb->type;
     124          166 :       if (POINTER_TYPE_P (type))
     125          112 :         type = sizetype;
     126          166 :       if (comb->n < MAX_AFF_ELTS)
     127              :         {
     128            0 :           comb->elts[comb->n].coef = scale;
     129            0 :           comb->elts[comb->n].val = comb->rest;
     130            0 :           comb->rest = NULL_TREE;
     131            0 :           comb->n++;
     132              :         }
     133              :       else
     134          166 :         comb->rest = fold_build2 (MULT_EXPR, type, comb->rest,
     135              :                                   wide_int_to_tree (type, scale));
     136              :     }
     137     39165550 : }
     138              : 
     139              : /* Adds ELT * SCALE to COMB.  */
     140              : 
     141              : void
     142     32242853 : aff_combination_add_elt (aff_tree *comb, tree elt, const widest_int &scale_in)
     143              : {
     144     32242853 :   unsigned i;
     145     32242853 :   tree type;
     146              : 
     147     32242853 :   widest_int scale = wide_int_ext_for_comb (scale_in, comb->type);
     148     32242853 :   if (scale == 0)
     149              :     return;
     150              : 
     151     45510778 :   for (i = 0; i < comb->n; i++)
     152     21779214 :     if (operand_equal_p (comb->elts[i].val, elt, 0))
     153              :       {
     154      8511289 :         widest_int new_coef
     155      8511289 :           = wide_int_ext_for_comb (comb->elts[i].coef + scale, comb->type);
     156      8511289 :         if (new_coef != 0)
     157              :           {
     158       125964 :             comb->elts[i].coef = new_coef;
     159       125964 :             return;
     160              :           }
     161              : 
     162      8385325 :         comb->n--;
     163      8385325 :         comb->elts[i] = comb->elts[comb->n];
     164              : 
     165      8385325 :         if (comb->rest)
     166              :           {
     167          194 :             gcc_assert (comb->n == MAX_AFF_ELTS - 1);
     168          194 :             comb->elts[comb->n].coef = 1;
     169          194 :             comb->elts[comb->n].val = comb->rest;
     170          194 :             comb->rest = NULL_TREE;
     171          194 :             comb->n++;
     172              :           }
     173              :         return;
     174      8511289 :       }
     175     23731564 :   if (comb->n < MAX_AFF_ELTS)
     176              :     {
     177     23730869 :       comb->elts[comb->n].coef = scale;
     178     23730869 :       comb->elts[comb->n].val = elt;
     179     23730869 :       comb->n++;
     180     23730869 :       return;
     181              :     }
     182              : 
     183          695 :   type = comb->type;
     184          695 :   if (POINTER_TYPE_P (type))
     185          314 :     type = sizetype;
     186              : 
     187          695 :   if (scale == 1)
     188          518 :     elt = fold_convert (type, elt);
     189              :   else
     190          177 :     elt = fold_build2 (MULT_EXPR, type,
     191              :                        fold_convert (type, elt),
     192              :                        wide_int_to_tree (type, scale));
     193              : 
     194          695 :   if (comb->rest)
     195           42 :     comb->rest = fold_build2 (PLUS_EXPR, type, comb->rest,
     196              :                               elt);
     197              :   else
     198              :     comb->rest = elt;
     199     32242853 : }
     200              : 
     201              : /* Adds CST to C.  */
     202              : 
     203              : static void
     204     91655696 : aff_combination_add_cst (aff_tree *c, const poly_widest_int &cst)
     205              : {
     206     91655696 :   c->offset = wide_int_ext_for_comb (c->offset + cst, c->type);
     207     91655696 : }
     208              : 
     209              : /* Adds COMB2 to COMB1.  */
     210              : 
     211              : void
     212     87414555 : aff_combination_add (aff_tree *comb1, aff_tree *comb2)
     213              : {
     214     87414555 :   unsigned i;
     215              : 
     216     87414555 :   aff_combination_add_cst (comb1, comb2->offset);
     217    195488237 :   for (i = 0; i < comb2->n; i++)
     218     20659127 :     aff_combination_add_elt (comb1, comb2->elts[i].val, comb2->elts[i].coef);
     219     87414555 :   if (comb2->rest)
     220          180 :     aff_combination_add_elt (comb1, comb2->rest, 1);
     221     87414555 : }
     222              : 
     223              : /* Converts affine combination COMB to TYPE.  AT is the range-query context.  */
     224              : 
     225              : void
     226     29224358 : aff_combination_convert (aff_tree *comb, tree type, gimple *at)
     227              : {
     228     29224358 :   unsigned i, j;
     229     29224358 :   tree comb_type = comb->type;
     230              : 
     231     29224358 :   if  (TYPE_PRECISION (type) > TYPE_PRECISION (comb_type))
     232              :     {
     233       911049 :       tree val = fold_convert (type, aff_combination_to_tree (comb));
     234       911049 :       tree_to_aff_combination (val, type, comb, at);
     235       911049 :       return;
     236              :     }
     237              : 
     238     28313309 :   comb->type = type;
     239     28313309 :   if (comb->rest && !POINTER_TYPE_P (type))
     240          208 :     comb->rest = fold_convert (type, comb->rest);
     241              : 
     242     28313309 :   if (TYPE_PRECISION (type) == TYPE_PRECISION (comb_type))
     243              :     return;
     244              : 
     245       131391 :   comb->offset = wide_int_ext_for_comb (comb->offset, comb->type);
     246       179903 :   for (i = j = 0; i < comb->n; i++)
     247              :     {
     248        48512 :       if (comb->elts[i].coef == 0)
     249            0 :         continue;
     250        48512 :       comb->elts[j].coef = comb->elts[i].coef;
     251        48512 :       comb->elts[j].val = fold_convert (type, comb->elts[i].val);
     252        48512 :       j++;
     253              :     }
     254              : 
     255       131391 :   comb->n = j;
     256       131391 :   if (comb->n < MAX_AFF_ELTS && comb->rest)
     257              :     {
     258            0 :       comb->elts[comb->n].coef = 1;
     259            0 :       comb->elts[comb->n].val = comb->rest;
     260            0 :       comb->rest = NULL_TREE;
     261            0 :       comb->n++;
     262              :     }
     263              : }
     264              : 
     265              : /* Tries to handle OP0 CODE OP1 as affine combination of parts.  AT is the
     266              :    range-query context.  Returns true when that was successful and returns the
     267              :    combination in COMB.  */
     268              : 
     269              : static bool
     270     24432071 : expr_to_aff_combination (aff_tree *comb, tree_code code, tree type,
     271              :                          tree op0, tree op1 = NULL_TREE, gimple *at = NULL)
     272              : {
     273     24432071 :   aff_tree tmp;
     274              : 
     275     24432071 :   switch (code)
     276              :     {
     277       367921 :     case POINTER_PLUS_EXPR:
     278       367921 :       tree_to_aff_combination (op0, type, comb, at);
     279       367921 :       tree_to_aff_combination (op1, sizetype, &tmp, at);
     280       367921 :       aff_combination_add (comb, &tmp);
     281       367921 :       return true;
     282              : 
     283     13988781 :     case PLUS_EXPR:
     284     13988781 :     case MINUS_EXPR:
     285     13988781 :       tree_to_aff_combination (op0, type, comb, at);
     286     13988781 :       tree_to_aff_combination (op1, type, &tmp, at);
     287     13988781 :       if (code == MINUS_EXPR)
     288       577937 :         aff_combination_scale (&tmp, -1);
     289     13988781 :       aff_combination_add (comb, &tmp);
     290     13988781 :       return true;
     291              : 
     292      6888195 :     case MULT_EXPR:
     293      6888195 :       if (TREE_CODE (op1) != INTEGER_CST)
     294              :         break;
     295      5945041 :       tree_to_aff_combination (op0, type, comb, at);
     296      5945041 :       aff_combination_scale (comb, wi::to_widest (op1));
     297      5945041 :       return true;
     298              : 
     299        97913 :     case NEGATE_EXPR:
     300        97913 :       tree_to_aff_combination (op0, type, comb, at);
     301        97913 :       aff_combination_scale (comb, -1);
     302        97913 :       return true;
     303              : 
     304         3783 :     case BIT_NOT_EXPR:
     305              :       /* ~x = -x - 1 */
     306         3783 :       tree_to_aff_combination (op0, type, comb, at);
     307         3783 :       aff_combination_scale (comb, -1);
     308         3783 :       aff_combination_add_cst (comb, -1);
     309         3783 :       return true;
     310              : 
     311      3085478 :     CASE_CONVERT:
     312      3085478 :       {
     313      3085478 :         tree otype = type;
     314      3085478 :         tree inner = op0;
     315      3085478 :         tree itype = TREE_TYPE (inner);
     316      3085478 :         enum tree_code icode = TREE_CODE (inner);
     317              : 
     318              :         /* STRIP_NOPS  */
     319      3085478 :         if (tree_nop_conversion_p (otype, itype))
     320              :           {
     321        13595 :             tree_to_aff_combination (op0, type, comb, at);
     322        13595 :             return true;
     323              :           }
     324              : 
     325              :         /* Look through (OTYPE) (ITYPE) IOP.  ITYPE and IOTYPE have the
     326              :            same precision and differ at most in signedness.  */
     327      3071606 :         if (INTEGRAL_NB_TYPE_P (otype)
     328      3070889 :             && INTEGRAL_NB_TYPE_P (itype)
     329      3046713 :             && TYPE_PRECISION (otype) > TYPE_PRECISION (itype)
     330      4968103 :             && CONVERT_EXPR_P (inner))
     331              :           {
     332        98974 :             tree iop = TREE_OPERAND (inner, 0);
     333        98974 :             tree iotype = TREE_TYPE (iop);
     334        98974 :             tree_code wcode = TREE_CODE (iop);
     335              : 
     336        98958 :             if (INTEGRAL_NB_TYPE_P (iotype)
     337       197920 :                 && tree_nop_conversion_p (itype, iotype))
     338              :               {
     339        79599 :                 int_range_max vr;
     340        79599 :                 range_query *query = get_range_query (cfun);
     341              : 
     342              :                 /* Distribute the conversions when the operation fits ITYPE:
     343              :                      (OTYPE) (ITYPE) ((IOTYPE) X +- CST)
     344              :                        ->
     345              :                      (OTYPE) (ITYPE) (IOTYPE) X +- (OTYPE) (ITYPE) CST.
     346              : 
     347              :                   (OTYPE)(ITYPE) (IOTYPE) X will be further simplified to
     348              :                   (OTYPE) X when X fits ITYPE.
     349              : 
     350              :                   So eventually the simplification is like
     351              :                   (OTYPE) (ITYPE) ((IOTYPE) X + CST)
     352              :                   ->
     353              :                   (OTYPE) X + (OTYPE) (ITYPE) CST.
     354              :                   This is used to handle the complicated convert chain which
     355              :                   is created by unsigned canonicalization in IVOPTS, the
     356              :                   canonicalization itself is used to detect more equality
     357              :                   for iv, so handle the redundancy here.  */
     358              : 
     359        79599 :                 if (!TYPE_UNSIGNED (itype)
     360        63582 :                     && TYPE_UNSIGNED (iotype)
     361        63582 :                     && (wcode == PLUS_EXPR || wcode == MINUS_EXPR)
     362       135007 :                     && TREE_CODE (TREE_OPERAND (iop, 1)) == INTEGER_CST)
     363              :                   {
     364        52281 :                     tree wop0 = fold_convert (itype, TREE_OPERAND (iop, 0));
     365        52281 :                     tree wop1 = fold_convert (itype, TREE_OPERAND (iop, 1));
     366              : 
     367        52281 :                     if (query->range_of_expr (vr, wop0, at)
     368        52281 :                         && !vr.varying_p ()
     369        88957 :                         && !vr.undefined_p ())
     370              :                       {
     371        36314 :                         wi::overflow_type ovf1 = wi::OVF_NONE;
     372        36314 :                         wi::overflow_type ovf2 = wi::OVF_NONE;
     373        36314 :                         wide_int cst = wi::to_wide (wop1);
     374              : 
     375              :                         /* IOP has wrapping type IOTYPE, so check signed
     376              :                            overflow explicitly.  overflow_free_p uses ITYPE's
     377              :                            undefined-overflow semantics and always returns true.  */
     378        36314 :                         if (wcode == PLUS_EXPR)
     379              :                           {
     380        36314 :                             wi::add (vr.lower_bound (), cst, SIGNED, &ovf1);
     381        36314 :                             wi::add (vr.upper_bound (), cst, SIGNED, &ovf2);
     382              :                           }
     383              :                         else
     384              :                           {
     385            0 :                             wi::sub (vr.lower_bound (), cst, SIGNED, &ovf1);
     386            0 :                             wi::sub (vr.upper_bound (), cst, SIGNED, &ovf2);
     387              :                           }
     388              : 
     389        36314 :                         if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
     390              :                           {
     391         6612 :                             wop0 = fold_convert (otype, wop0);
     392         6612 :                             wop1 = fold_convert (otype, wop1);
     393         6612 :                             return expr_to_aff_combination (comb, wcode, otype,
     394              :                                                             wop0, wop1, at);
     395              :                           }
     396        36314 :                       }
     397              :                   }
     398              : 
     399              :                 /* Strip the inner conversion when IOP fits ITYPE:
     400              :                      (OTYPE) (ITYPE) IOP -> (OTYPE) IOP.
     401              : 
     402              :                   (unsigned long) (int) (unsigned) k is simplified to
     403              :                   (unsigned long) k when we know k >= 0.  */
     404        72987 :                 if (TYPE_SIGN (itype) == TYPE_SIGN (iotype)
     405        72987 :                     || (TREE_CODE (iop) == INTEGER_CST
     406        72987 :                         ? int_fits_type_p (iop, itype)
     407        72987 :                         : (query->range_of_expr (vr, iop, at)
     408        72987 :                            && range_fits_type_p (&vr, TYPE_PRECISION (itype),
     409        72987 :                                                  TYPE_SIGN (itype)))))
     410              :                   {
     411         8699 :                     tree_to_aff_combination (fold_convert (otype, iop), type,
     412              :                                              comb, at);
     413         8699 :                     return true;
     414              :                   }
     415        79599 :               }
     416              :           }
     417              : 
     418              :         /* In principle this is a valid folding, but it isn't necessarily
     419              :            an optimization, so do it here and not in fold_unary.  */
     420      3056572 :         if ((icode == PLUS_EXPR || icode == MINUS_EXPR || icode == MULT_EXPR)
     421       249463 :             && INTEGRAL_NB_TYPE_P (itype)
     422       249463 :             && INTEGRAL_NB_TYPE_P (otype)
     423      3306035 :             && TYPE_PRECISION (otype) > TYPE_PRECISION (itype))
     424              :           {
     425       195567 :             tree op0 = TREE_OPERAND (inner, 0), op1 = TREE_OPERAND (inner, 1);
     426              : 
     427              :             /* If inner type has undefined overflow behavior, fold conversion
     428              :                for below two cases:
     429              :                  (T1)(X *+- CST) -> (T1)X *+- (T1)CST
     430              :                  (T1)(X + X)     -> (T1)X + (T1)X.  */
     431       195567 :             if (TYPE_OVERFLOW_UNDEFINED (itype)
     432        93303 :                 && (TREE_CODE (op1) == INTEGER_CST
     433         3995 :                     || (icode == PLUS_EXPR && operand_equal_p (op0, op1, 0))))
     434              :               {
     435        89308 :                 op0 = fold_convert (otype, op0);
     436        89308 :                 op1 = fold_convert (otype, op1);
     437        89308 :                 return expr_to_aff_combination (comb, icode, otype, op0, op1,
     438       103590 :                                                 at);
     439              :               }
     440       106259 :             wide_int minv, maxv;
     441              :             /* If inner type has wrapping overflow behavior, fold conversion
     442              :                for below case:
     443              :                  (T1)(X *+- CST) -> (T1)X *+- (T1)CST
     444              :                if X *+- CST doesn't overflow by range information.  */
     445       106259 :             int_range_max vr;
     446       106259 :             if (TYPE_UNSIGNED (itype)
     447       102016 :                 && TYPE_OVERFLOW_WRAPS (itype)
     448       102016 :                 && TREE_CODE (op1) == INTEGER_CST
     449       145530 :                 && get_range_query (cfun)->range_of_expr (vr, op0, at)
     450        72765 :                 && !vr.varying_p ()
     451       126380 :                 && !vr.undefined_p ())
     452              :               {
     453        20107 :                 wide_int minv = vr.lower_bound ();
     454        20107 :                 wide_int maxv = vr.upper_bound ();
     455        20107 :                 wi::overflow_type overflow = wi::OVF_NONE;
     456        20107 :                 signop sign = UNSIGNED;
     457        20107 :                 if (icode == PLUS_EXPR)
     458        15334 :                   wi::add (maxv, wi::to_wide (op1), sign, &overflow);
     459         4773 :                 else if (icode == MULT_EXPR)
     460         4773 :                   wi::mul (maxv, wi::to_wide (op1), sign, &overflow);
     461              :                 else
     462            0 :                   wi::sub (minv, wi::to_wide (op1), sign, &overflow);
     463              : 
     464        20107 :                 if (overflow == wi::OVF_NONE)
     465              :                   {
     466         6623 :                     op0 = fold_convert (otype, op0);
     467         6623 :                     op1 = fold_convert (otype, op1);
     468         6623 :                     return expr_to_aff_combination (comb, icode, otype, op0,
     469         6623 :                                                     op1, at);
     470              :                   }
     471              :                 /* A negative offset can be handled as an exact subtraction:
     472              :                      (T1) (X + CST) -> (T1) X - (T1) -CST.
     473              : 
     474              :                    This rewrites (int) ((unsigned int) k + UINT_MAX) to
     475              :                    (int) k - 1 when k is known >= 1.
     476              : 
     477              :                    This also works for the most-negative signed CST: unsigned
     478              :                    negation is modulo 2^precision, so -CST equals CST.  */
     479        13484 :                 else if (icode == PLUS_EXPR
     480        13484 :                          && wi::neg_p (wi::to_wide (op1), SIGNED))
     481              :                   {
     482        12336 :                     wide_int pos_cst = wi::neg (wi::to_wide (op1));
     483        12336 :                     int_range<1> cst_range (itype, pos_cst, pos_cst);
     484        12336 :                     range_op_handler minus (MINUS_EXPR);
     485              : 
     486        12336 :                     if (minus.overflow_free_p (vr, cst_range))
     487              :                       {
     488         7659 :                         op0 = fold_convert (otype, op0);
     489         7659 :                         op1 = fold_convert (otype,
     490              :                                             wide_int_to_tree (itype, pos_cst));
     491         7659 :                         return expr_to_aff_combination (comb, MINUS_EXPR,
     492              :                                                         otype, op0, op1, at);
     493              :                       }
     494        12336 :                   }
     495        20107 :               }
     496       106259 :           }
     497              :       }
     498              :       break;
     499              : 
     500       943154 :     default:;
     501              :     }
     502              : 
     503              :   return false;
     504     24432071 : }
     505              : 
     506              : /* Splits EXPR into an affine combination of parts.  AT is the range-query
     507              :    context.  */
     508              : 
     509              : void
     510    147716005 : tree_to_aff_combination (tree expr, tree type, aff_tree *comb, gimple *at)
     511              : {
     512    147716005 :   aff_tree tmp;
     513    147716005 :   enum tree_code code;
     514    147716005 :   tree core, toffset;
     515    147716005 :   poly_int64 bitpos, bitsize, bytepos;
     516    147716005 :   machine_mode mode;
     517    147716005 :   int unsignedp, reversep, volatilep;
     518              : 
     519    147716005 :   STRIP_NOPS (expr);
     520              : 
     521    147716005 :   code = TREE_CODE (expr);
     522    147716005 :   switch (code)
     523              :     {
     524     21028127 :     case POINTER_PLUS_EXPR:
     525     21028127 :     case PLUS_EXPR:
     526     21028127 :     case MINUS_EXPR:
     527     21028127 :     case MULT_EXPR:
     528     21028127 :       if (expr_to_aff_combination (comb, code, type, TREE_OPERAND (expr, 0),
     529     21028127 :                                    TREE_OPERAND (expr, 1), at))
     530              :         return;
     531              :       break;
     532              : 
     533       101105 :     case NEGATE_EXPR:
     534       101105 :     case BIT_NOT_EXPR:
     535       101105 :       if (expr_to_aff_combination (comb, code, type, TREE_OPERAND (expr, 0),
     536              :                                    NULL_TREE, at))
     537              :         return;
     538              :       break;
     539              : 
     540      3053975 :     CASE_CONVERT:
     541              :       /* ???  TREE_TYPE (expr) should be equal to type here, but IVOPTS
     542              :          calls this with not showing an outer widening cast.  */
     543      3053975 :       if (expr_to_aff_combination (comb, code,
     544      3053975 :                                    TREE_TYPE (expr), TREE_OPERAND (expr, 0),
     545              :                                    NULL_TREE, at))
     546              :         {
     547       118901 :           aff_combination_convert (comb, type, at);
     548       118901 :           return;
     549              :         }
     550              :       break;
     551              : 
     552     10232373 :     case ADDR_EXPR:
     553              :       /* Handle &MEM[ptr + CST] which is equivalent to POINTER_PLUS_EXPR.  */
     554     10232373 :       if (TREE_CODE (TREE_OPERAND (expr, 0)) == MEM_REF)
     555              :         {
     556       614469 :           expr = TREE_OPERAND (expr, 0);
     557       614469 :           tree_to_aff_combination (TREE_OPERAND (expr, 0), type, comb, at);
     558       614469 :           tree_to_aff_combination (TREE_OPERAND (expr, 1), sizetype, &tmp,
     559              :                                    at);
     560       614469 :           aff_combination_add (comb, &tmp);
     561       614469 :           return;
     562              :         }
     563      9617904 :       core = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize, &bitpos,
     564              :                                   &toffset, &mode, &unsignedp, &reversep,
     565              :                                   &volatilep);
     566     19235808 :       if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
     567              :         break;
     568      9617904 :       aff_combination_const (comb, type, bytepos);
     569      9617904 :       if (TREE_CODE (core) == MEM_REF)
     570              :         {
     571       226126 :           tree mem_offset = TREE_OPERAND (core, 1);
     572       226126 :           aff_combination_add_cst (comb, wi::to_poly_widest (mem_offset));
     573       226126 :           core = TREE_OPERAND (core, 0);
     574              :         }
     575              :       else
     576      9391778 :         core = build_fold_addr_expr (core);
     577              : 
     578      9617904 :       if (TREE_CODE (core) == ADDR_EXPR)
     579      9391778 :         aff_combination_add_elt (comb, core, 1);
     580              :       else
     581              :         {
     582       226126 :           tree_to_aff_combination (core, type, &tmp, at);
     583       226126 :           aff_combination_add (comb, &tmp);
     584              :         }
     585      9617904 :       if (toffset)
     586              :         {
     587        64234 :           tree_to_aff_combination (toffset, type, &tmp, at);
     588        64234 :           aff_combination_add (comb, &tmp);
     589              :         }
     590              :       return;
     591              : 
     592    113300425 :     default:
     593    113300425 :       {
     594    113300425 :         if (poly_int_tree_p (expr))
     595              :           {
     596     74620070 :             aff_combination_const (comb, type, wi::to_poly_widest (expr));
     597     74620070 :             return;
     598              :           }
     599              :         break;
     600              :       }
     601              :     }
     602              : 
     603     42491736 :   aff_combination_elt (comb, type, expr);
     604    147716005 : }
     605              : 
     606              : /* Creates EXPR + ELT * SCALE in TYPE.  EXPR is taken from affine
     607              :    combination COMB.  */
     608              : 
     609              : static tree
     610     41465071 : add_elt_to_tree (tree expr, tree type, tree elt, const widest_int &scale_in)
     611              : {
     612     41465071 :   enum tree_code code;
     613              : 
     614     41465071 :   widest_int scale = wide_int_ext_for_comb (scale_in, type);
     615              : 
     616     41465071 :   elt = fold_convert (type, elt);
     617     41465071 :   if (scale == 1)
     618              :     {
     619     35374992 :       if (!expr)
     620              :         return elt;
     621              : 
     622     13832242 :       return fold_build2 (PLUS_EXPR, type, expr, elt);
     623              :     }
     624              : 
     625      6090079 :   if (scale == -1)
     626              :     {
     627      2841300 :       if (!expr)
     628      1686681 :         return fold_build1 (NEGATE_EXPR, type, elt);
     629              : 
     630      1154619 :       return fold_build2 (MINUS_EXPR, type, expr, elt);
     631              :     }
     632              : 
     633      3248779 :   if (!expr)
     634      1920867 :     return fold_build2 (MULT_EXPR, type, elt, wide_int_to_tree (type, scale));
     635              : 
     636      1327912 :   if (wi::neg_p (scale))
     637              :     {
     638       346658 :       code = MINUS_EXPR;
     639       346658 :       scale = -scale;
     640              :     }
     641              :   else
     642              :     code = PLUS_EXPR;
     643              : 
     644      1327912 :   elt = fold_build2 (MULT_EXPR, type, elt, wide_int_to_tree (type, scale));
     645      1327912 :   return fold_build2 (code, type, expr, elt);
     646     41465071 : }
     647              : 
     648              : /* Makes tree from the affine combination COMB.  */
     649              : 
     650              : tree
     651     25150298 : aff_combination_to_tree (aff_tree *comb)
     652              : {
     653     25150298 :   tree type = comb->type, base = NULL_TREE, expr = NULL_TREE;
     654     25150298 :   unsigned i;
     655     25150298 :   poly_widest_int off;
     656     25150298 :   int sgn;
     657              : 
     658     25150298 :   gcc_assert (comb->n == MAX_AFF_ELTS || comb->rest == NULL_TREE);
     659              : 
     660     25150298 :   i = 0;
     661     25150298 :   if (POINTER_TYPE_P (type))
     662              :     {
     663       124849 :       type = sizetype;
     664       249540 :       if (comb->n > 0 && comb->elts[0].coef == 1
     665       249540 :           && POINTER_TYPE_P (TREE_TYPE (comb->elts[0].val)))
     666              :         {
     667     25150298 :           base = comb->elts[0].val;
     668     25150298 :           ++i;
     669              :         }
     670              :     }
     671              : 
     672     41464801 :   for (; i < comb->n; i++)
     673     16314503 :     expr = add_elt_to_tree (expr, type, comb->elts[i].val, comb->elts[i].coef);
     674              : 
     675     25150298 :   if (comb->rest)
     676          270 :     expr = add_elt_to_tree (expr, type, comb->rest, 1);
     677              : 
     678              :   /* Ensure that we get x - 1, not x + (-1) or x + 0xff..f if x is
     679              :      unsigned.  */
     680     25150298 :   if (known_lt (comb->offset, 0))
     681              :     {
     682      2243715 :       off = -comb->offset;
     683      2243715 :       sgn = -1;
     684              :     }
     685              :   else
     686              :     {
     687     22906583 :       off = comb->offset;
     688     22906583 :       sgn = 1;
     689              :     }
     690     25150298 :   expr = add_elt_to_tree (expr, type, wide_int_to_tree (type, off), sgn);
     691              : 
     692     25150298 :   if (base)
     693       120678 :     return fold_build_pointer_plus (base, expr);
     694              :   else
     695     25029620 :     return fold_convert (comb->type, expr);
     696     25150298 : }
     697              : 
     698              : /* Copies the tree elements of COMB to ensure that they are not shared.  */
     699              : 
     700              : void
     701      1837556 : unshare_aff_combination (aff_tree *comb)
     702              : {
     703      1837556 :   unsigned i;
     704              : 
     705      3900145 :   for (i = 0; i < comb->n; i++)
     706      2062589 :     comb->elts[i].val = unshare_expr (comb->elts[i].val);
     707      1837556 :   if (comb->rest)
     708            0 :     comb->rest = unshare_expr (comb->rest);
     709      1837556 : }
     710              : 
     711              : /* Remove M-th element from COMB.  */
     712              : 
     713              : void
     714      1802387 : aff_combination_remove_elt (aff_tree *comb, unsigned m)
     715              : {
     716      1802387 :   comb->n--;
     717      1802387 :   if (m <= comb->n)
     718      1802387 :     comb->elts[m] = comb->elts[comb->n];
     719      1802387 :   if (comb->rest)
     720              :     {
     721            0 :       comb->elts[comb->n].coef = 1;
     722            0 :       comb->elts[comb->n].val = comb->rest;
     723            0 :       comb->rest = NULL_TREE;
     724            0 :       comb->n++;
     725              :     }
     726      1802387 : }
     727              : 
     728              : /* Adds C * COEF * VAL to R.  VAL may be NULL, in that case only
     729              :    C * COEF is added to R.  */
     730              : 
     731              : 
     732              : static void
     733      4011232 : aff_combination_add_product (aff_tree *c, const widest_int &coef, tree val,
     734              :                              aff_tree *r)
     735              : {
     736      4011232 :   unsigned i;
     737      4011232 :   tree aval, type;
     738              : 
     739      5478681 :   for (i = 0; i < c->n; i++)
     740              :     {
     741      1467449 :       aval = c->elts[i].val;
     742      1467449 :       if (val)
     743              :         {
     744            0 :           type = TREE_TYPE (aval);
     745            0 :           aval = fold_build2 (MULT_EXPR, type, aval,
     746              :                               fold_convert (type, val));
     747              :         }
     748              : 
     749      1467449 :       aff_combination_add_elt (r, aval, coef * c->elts[i].coef);
     750              :     }
     751              : 
     752      4011232 :   if (c->rest)
     753              :     {
     754            0 :       aval = c->rest;
     755            0 :       if (val)
     756              :         {
     757            0 :           type = TREE_TYPE (aval);
     758            0 :           aval = fold_build2 (MULT_EXPR, type, aval,
     759              :                               fold_convert (type, val));
     760              :         }
     761              : 
     762            0 :       aff_combination_add_elt (r, aval, coef);
     763              :     }
     764              : 
     765      4011232 :   if (val)
     766              :     {
     767            0 :       if (c->offset.is_constant ())
     768              :         /* Access coeffs[0] directly, for efficiency.  */
     769            0 :         aff_combination_add_elt (r, val, coef * c->offset.coeffs[0]);
     770              :       else
     771              :         {
     772              :           /* c->offset is polynomial, so multiply VAL rather than COEF
     773              :              by it.  */
     774              :           tree offset = wide_int_to_tree (TREE_TYPE (val), c->offset);
     775              :           val = fold_build2 (MULT_EXPR, TREE_TYPE (val), val, offset);
     776              :           aff_combination_add_elt (r, val, coef);
     777              :         }
     778              :     }
     779              :   else
     780      4011232 :     aff_combination_add_cst (r, coef * c->offset);
     781      4011232 : }
     782              : 
     783              : /* Multiplies C1 by C2, storing the result to R  */
     784              : 
     785              : void
     786      4011232 : aff_combination_mult (aff_tree *c1, aff_tree *c2, aff_tree *r)
     787              : {
     788      4011232 :   unsigned i;
     789      4011232 :   gcc_assert (TYPE_PRECISION (c1->type) == TYPE_PRECISION (c2->type));
     790              : 
     791      4011232 :   aff_combination_zero (r, c1->type);
     792              : 
     793      8022464 :   for (i = 0; i < c2->n; i++)
     794            0 :     aff_combination_add_product (c1, c2->elts[i].coef, c2->elts[i].val, r);
     795      4011232 :   if (c2->rest)
     796            0 :     aff_combination_add_product (c1, 1, c2->rest, r);
     797      4011232 :   if (c2->offset.is_constant ())
     798              :     /* Access coeffs[0] directly, for efficiency.  */
     799      4011232 :     aff_combination_add_product (c1, c2->offset.coeffs[0], NULL, r);
     800              :   else
     801              :     {
     802              :       /* c2->offset is polynomial, so do the multiplication in tree form.  */
     803              :       tree offset = wide_int_to_tree (c2->type, c2->offset);
     804              :       aff_combination_add_product (c1, 1, offset, r);
     805              :     }
     806      4011232 : }
     807              : 
     808              : /* Returns the element of COMB whose value is VAL, or NULL if no such
     809              :    element exists.  If IDX is not NULL, it is set to the index of VAL in
     810              :    COMB.  */
     811              : 
     812              : static class aff_comb_elt *
     813      1198697 : aff_combination_find_elt (aff_tree *comb, tree val, unsigned *idx)
     814              : {
     815      1198697 :   unsigned i;
     816              : 
     817      1530191 :   for (i = 0; i < comb->n; i++)
     818      1203872 :     if (operand_equal_p (comb->elts[i].val, val, 0))
     819              :       {
     820       872378 :         if (idx)
     821            0 :           *idx = i;
     822              : 
     823       872378 :         return &comb->elts[i];
     824              :       }
     825              : 
     826              :   return NULL;
     827              : }
     828              : 
     829              : /* Element of the cache that maps ssa name NAME to its expanded form
     830              :    as an affine expression EXPANSION.  */
     831              : 
     832       288160 : class name_expansion
     833              : {
     834              : public:
     835              :   aff_tree expansion;
     836              : 
     837              :   /* True if the expansion for the name is just being generated.  */
     838              :   unsigned in_progress : 1;
     839              : };
     840              : 
     841              : /* Expands SSA names in COMB recursively.  CACHE is used to cache the
     842              :    results.  */
     843              : 
     844              : void
     845     35511518 : aff_combination_expand (aff_tree *comb ATTRIBUTE_UNUSED,
     846              :                         hash_map<tree, name_expansion *> **cache)
     847              : {
     848     35511518 :   unsigned i;
     849    106534554 :   aff_tree to_add, current, curre;
     850     35511518 :   tree e;
     851     35511518 :   widest_int scale;
     852     35511518 :   class name_expansion *exp;
     853              : 
     854     35511518 :   aff_combination_zero (&to_add, comb->type);
     855     74569024 :   for (i = 0; i < comb->n; i++)
     856              :     {
     857      3545988 :       tree type, name;
     858      3545988 :       enum tree_code code;
     859              : 
     860      3545988 :       e = comb->elts[i].val;
     861      3545988 :       type = TREE_TYPE (e);
     862      3545988 :       name = e;
     863              :       /* Look through some conversions.  */
     864      3407462 :       if (CONVERT_EXPR_P (e)
     865      3545988 :           && (TYPE_PRECISION (type)
     866       138526 :               >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (e, 0)))))
     867        89255 :         name = TREE_OPERAND (e, 0);
     868      3545988 :       if (TREE_CODE (name) != SSA_NAME)
     869      2829027 :         continue;
     870      3325677 :       auto def = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (name));
     871      1489060 :       if (!def || gimple_assign_lhs (def) != name)
     872      1836617 :         continue;
     873              : 
     874      1489060 :       code = gimple_assign_rhs_code (def);
     875      1515728 :       if (code != SSA_NAME
     876      1488443 :           && !IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
     877      1515732 :           && (get_gimple_rhs_class (code) != GIMPLE_SINGLE_RHS
     878        26672 :               || !is_gimple_min_invariant (gimple_assign_rhs1 (def))))
     879        26668 :         continue;
     880              : 
     881              :       /* We do not know whether the reference retains its value at the
     882              :          place where the expansion is used.  */
     883      1462392 :       if (TREE_CODE_CLASS (code) == tcc_reference)
     884       586192 :         continue;
     885              : 
     886       876200 :       name_expansion **slot = NULL;
     887       876200 :       if (*cache)
     888       750107 :         slot = (*cache)->get (name);
     889       750107 :       exp = slot ? *slot : NULL;
     890       876200 :       if (!exp)
     891              :         {
     892              :           /* Only bother to handle cases tree_to_aff_combination will.  */
     893       231279 :           switch (code)
     894              :             {
     895       106568 :             case POINTER_PLUS_EXPR:
     896       106568 :             case PLUS_EXPR:
     897       106568 :             case MINUS_EXPR:
     898       106568 :             case MULT_EXPR:
     899       213136 :               if (!expr_to_aff_combination (&current, code, TREE_TYPE (name),
     900              :                                             gimple_assign_rhs1 (def),
     901              :                                             gimple_assign_rhs2 (def)))
     902        66847 :                 continue;
     903              :               break;
     904          591 :             case NEGATE_EXPR:
     905          591 :             case BIT_NOT_EXPR:
     906          591 :               if (!expr_to_aff_combination (&current, code, TREE_TYPE (name),
     907              :                                             gimple_assign_rhs1 (def)))
     908            0 :                 continue;
     909              :               break;
     910        31503 :             CASE_CONVERT:
     911        31503 :               if (!expr_to_aff_combination (&current, code, TREE_TYPE (name),
     912              :                                             gimple_assign_rhs1 (def)))
     913              :                 /* This makes us always expand conversions which we did
     914              :                    in the past and makes gcc.dg/tree-ssa/ivopts-lt-2.c
     915              :                    PASS, eliminating one induction variable in IVOPTs.
     916              :                    ???  But it is really excessive and we should try
     917              :                    harder to do without it.  */
     918        17908 :                 aff_combination_elt (&current, TREE_TYPE (name),
     919        17908 :                                      fold_convert (TREE_TYPE (name),
     920              :                                                    gimple_assign_rhs1 (def)));
     921              :               break;
     922          225 :             case ADDR_EXPR:
     923          225 :             case INTEGER_CST:
     924          225 :             case POLY_INT_CST:
     925          225 :               tree_to_aff_combination (gimple_assign_rhs1 (def),
     926          225 :                                        TREE_TYPE (name), &current);
     927          225 :               break;
     928        92392 :             default:
     929        92392 :               continue;
     930              :             }
     931        72040 :           exp = XNEW (class name_expansion);
     932        72040 :           ::new (static_cast<void *> (exp)) name_expansion ();
     933        72040 :           exp->in_progress = 1;
     934        72040 :           if (!*cache)
     935        30843 :             *cache = new hash_map<tree, name_expansion *>;
     936        72040 :           (*cache)->put (name, exp);
     937        72040 :           aff_combination_expand (&current, cache);
     938        72040 :           exp->expansion = current;
     939        72040 :           exp->in_progress = 0;
     940              :         }
     941              :       else
     942              :         {
     943              :           /* Since we follow the definitions in the SSA form, we should not
     944              :              enter a cycle unless we pass through a phi node.  */
     945       644921 :           gcc_assert (!exp->in_progress);
     946       644921 :           current = exp->expansion;
     947              :         }
     948       716961 :       if (!useless_type_conversion_p (comb->type, current.type))
     949        81720 :         aff_combination_convert (&current, comb->type);
     950              : 
     951              :       /* Accumulate the new terms to TO_ADD, so that we do not modify
     952              :          COMB while traversing it; include the term -coef * E, to remove
     953              :          it from COMB.  */
     954       716961 :       scale = comb->elts[i].coef;
     955       716961 :       aff_combination_zero (&curre, comb->type);
     956       716961 :       aff_combination_add_elt (&curre, e, -scale);
     957       716961 :       aff_combination_scale (&current, scale);
     958       716961 :       aff_combination_add (&to_add, &current);
     959       716961 :       aff_combination_add (&to_add, &curre);
     960              :     }
     961     35511518 :   aff_combination_add (comb, &to_add);
     962     35511518 : }
     963              : 
     964              : /* Similar to tree_to_aff_combination, but follows SSA name definitions
     965              :    and expands them recursively.  CACHE is used to cache the expansions
     966              :    of the ssa names, to avoid exponential time complexity for cases
     967              :    like
     968              : 
     969              :    a1 = a0 + a0;
     970              :    a2 = a1 + a1;
     971              :    a3 = a2 + a2;
     972              :    ...  */
     973              : 
     974              : void
     975     35337380 : tree_to_aff_combination_expand (tree expr, tree type, aff_tree *comb,
     976              :                                 hash_map<tree, name_expansion *> **cache)
     977              : {
     978     35337380 :   tree_to_aff_combination (expr, type, comb);
     979     35337380 :   aff_combination_expand (comb, cache);
     980     35337380 : }
     981              : 
     982              : /* Frees memory occupied by struct name_expansion in *VALUE.  Callback for
     983              :    hash_map::traverse.  */
     984              : 
     985              : bool
     986        72040 : free_name_expansion (tree const &, name_expansion **value, void *)
     987              : {
     988        72040 :   (*value)->~name_expansion ();
     989        72040 :   free (*value);
     990        72040 :   return true;
     991              : }
     992              : 
     993              : /* Frees memory allocated for the CACHE used by
     994              :    tree_to_aff_combination_expand.  */
     995              : 
     996              : void
     997      1793258 : free_affine_expand_cache (hash_map<tree, name_expansion *> **cache)
     998              : {
     999      1793258 :   if (!*cache)
    1000              :     return;
    1001              : 
    1002       102883 :   (*cache)->traverse<void *, free_name_expansion> (NULL);
    1003        61686 :   delete (*cache);
    1004        30843 :   *cache = NULL;
    1005              : }
    1006              : 
    1007              : /* If VAL == CST * DIV for any constant CST, returns true.
    1008              :    and if *MULT_SET is true, additionally compares CST and MULT
    1009              :    and if they are different, returns false.  If true is returned, CST is
    1010              :    stored to MULT and MULT_SET is set to true unless VAL and DIV are both zero
    1011              :    in which case neither MULT nor MULT_SET are updated.  */
    1012              : 
    1013              : static bool
    1014     17587792 : wide_int_constant_multiple_p (const poly_widest_int &val,
    1015              :                               const poly_widest_int &div,
    1016              :                               bool *mult_set, poly_widest_int *mult)
    1017              : {
    1018     17587792 :   poly_widest_int cst;
    1019              : 
    1020     17587792 :   if (known_eq (val, 0))
    1021              :     {
    1022      1179625 :       if (known_eq (div, 0))
    1023              :         return true;
    1024              : 
    1025          321 :       if (*mult_set && maybe_ne (*mult, 0))
    1026            0 :         return false;
    1027          321 :       *mult_set = true;
    1028          321 :       *mult = 0;
    1029          321 :       return true;
    1030              :     }
    1031              : 
    1032     16408167 :   if (maybe_eq (div, 0))
    1033              :     return false;
    1034              : 
    1035     16406789 :   if (!multiple_p (val, div, &cst))
    1036              :     return false;
    1037              : 
    1038     14521853 :   if (*mult_set && maybe_ne (*mult, cst))
    1039              :     return false;
    1040              : 
    1041     14502653 :   *mult_set = true;
    1042     14502653 :   *mult = cst;
    1043     14502653 :   return true;
    1044     17587792 : }
    1045              : 
    1046              : /* Returns true if VAL = X * DIV for some constant X.  If this is the case,
    1047              :    X is stored to MULT.  */
    1048              : 
    1049              : bool
    1050     17582188 : aff_combination_constant_multiple_p (aff_tree *val, aff_tree *div,
    1051              :                                      poly_widest_int *mult)
    1052              : {
    1053     17582188 :   bool mult_set = false;
    1054     17582188 :   unsigned i;
    1055              : 
    1056     17582188 :   if (val->n == 0 && known_eq (val->offset, 0))
    1057              :     {
    1058        53790 :       *mult = 0;
    1059        53790 :       return true;
    1060              :     }
    1061     17528398 :   if (val->n != div->n)
    1062              :     return false;
    1063              : 
    1064     16715414 :   if (val->rest || div->rest)
    1065              :     return false;
    1066              : 
    1067     16715414 :   if (!wide_int_constant_multiple_p (val->offset, div->offset,
    1068              :                                      &mult_set, mult))
    1069              :     return false;
    1070              : 
    1071     15682278 :   for (i = 0; i < div->n; i++)
    1072              :     {
    1073      1198697 :       class aff_comb_elt *elt
    1074      1198697 :               = aff_combination_find_elt (val, div->elts[i].val, NULL);
    1075      1198697 :       if (!elt)
    1076              :         return false;
    1077       872378 :       if (!wide_int_constant_multiple_p (elt->coef, div->elts[i].coef,
    1078              :                                          &mult_set, mult))
    1079              :         return false;
    1080              :     }
    1081              : 
    1082     14483581 :   gcc_assert (mult_set);
    1083              :   return true;
    1084              : }
    1085              : 
    1086              : /* Prints the affine VAL to the FILE. */
    1087              : 
    1088              : static void
    1089            0 : print_aff (FILE *file, aff_tree *val)
    1090              : {
    1091            0 :   unsigned i;
    1092            0 :   signop sgn = TYPE_SIGN (val->type);
    1093            0 :   if (POINTER_TYPE_P (val->type))
    1094            0 :     sgn = SIGNED;
    1095            0 :   fprintf (file, "{\n  type = ");
    1096            0 :   print_generic_expr (file, val->type, TDF_VOPS|TDF_MEMSYMS);
    1097            0 :   fprintf (file, "\n  offset = ");
    1098            0 :   print_dec (val->offset, file, sgn);
    1099            0 :   if (val->n > 0)
    1100              :     {
    1101            0 :       fprintf (file, "\n  elements = {\n");
    1102            0 :       for (i = 0; i < val->n; i++)
    1103              :         {
    1104            0 :           fprintf (file, "    [%d] = ", i);
    1105            0 :           print_generic_expr (file, val->elts[i].val, TDF_VOPS|TDF_MEMSYMS);
    1106              : 
    1107            0 :           fprintf (file, " * ");
    1108            0 :           print_dec (val->elts[i].coef, file, sgn);
    1109            0 :           if (i != val->n - 1)
    1110            0 :             fprintf (file, ", \n");
    1111              :         }
    1112            0 :       fprintf (file, "\n  }");
    1113              :   }
    1114            0 :   if (val->rest)
    1115              :     {
    1116            0 :       fprintf (file, "\n  rest = ");
    1117            0 :       print_generic_expr (file, val->rest, TDF_VOPS|TDF_MEMSYMS);
    1118              :     }
    1119            0 :   fprintf (file, "\n}");
    1120            0 : }
    1121              : 
    1122              : /* Prints the affine VAL to the standard error, used for debugging.  */
    1123              : 
    1124              : DEBUG_FUNCTION void
    1125            0 : debug_aff (aff_tree *val)
    1126              : {
    1127            0 :   print_aff (stderr, val);
    1128            0 :   fprintf (stderr, "\n");
    1129            0 : }
    1130              : 
    1131              : /* Computes address of the reference REF in ADDR.  The size of the accessed
    1132              :    location is stored to SIZE.  Returns the ultimate containing object to
    1133              :    which REF refers.  */
    1134              : 
    1135              : tree
    1136      5649242 : get_inner_reference_aff (tree ref, aff_tree *addr, poly_widest_int *size)
    1137              : {
    1138      5649242 :   poly_int64 bitsize, bitpos;
    1139      5649242 :   tree toff;
    1140      5649242 :   machine_mode mode;
    1141      5649242 :   int uns, rev, vol;
    1142      5649242 :   aff_tree tmp;
    1143      5649242 :   tree base = get_inner_reference (ref, &bitsize, &bitpos, &toff, &mode,
    1144              :                                    &uns, &rev, &vol);
    1145      5649242 :   tree base_addr = build_fold_addr_expr (base);
    1146              : 
    1147              :   /* ADDR = &BASE + TOFF + BITPOS / BITS_PER_UNIT.  */
    1148              : 
    1149      5649242 :   tree_to_aff_combination (base_addr, sizetype, addr);
    1150              : 
    1151      5649242 :   if (toff)
    1152              :     {
    1153       170692 :       tree_to_aff_combination (toff, sizetype, &tmp);
    1154       170692 :       aff_combination_add (addr, &tmp);
    1155              :     }
    1156              : 
    1157      5649242 :   poly_int64 bytepos = bits_to_bytes_round_down (bitpos);
    1158      5649242 :   aff_combination_const (&tmp, sizetype, bytepos);
    1159      5649242 :   aff_combination_add (addr, &tmp);
    1160              : 
    1161      5649242 :   *size = bits_to_bytes_round_up (bitpos + bitsize) - bytepos;
    1162              : 
    1163     11298484 :   return base;
    1164      5649242 : }
    1165              : 
    1166              : /* Returns true if a region of size SIZE1 at position 0 and a region of
    1167              :    size SIZE2 at position DIFF cannot overlap.  If DIFF is not constant,
    1168              :    bound it using value ranges of its elements obtained from QUERY,
    1169              :    or from the range query of the current function if QUERY is NULL.  */
    1170              : 
    1171              : bool
    1172      2824702 : aff_comb_cannot_overlap_p (aff_tree *diff, const poly_widest_int &size1,
    1173              :                            const poly_widest_int &size2,
    1174              :                            range_query *query)
    1175              : {
    1176              :   /* If the difference is a constant, check directly.  */
    1177      2824702 :   if (diff->n == 0)
    1178              :     {
    1179      1851342 :       if (!ordered_p (diff->offset, 0))
    1180              :         return false;
    1181              : 
    1182      1851342 :       if (maybe_lt (diff->offset, 0))
    1183              :         {
    1184              :           /* The second object is before the first one, we succeed if the last
    1185              :              element of the second object is before the start of the first one.  */
    1186       239621 :           return known_le (diff->offset + size2, 0);
    1187              :         }
    1188              :       else
    1189              :         {
    1190              :           /* We succeed if the second object starts after the first one ends.  */
    1191      1611721 :           return known_le (size1, diff->offset);
    1192              :         }
    1193              :     }
    1194              : 
    1195       973360 :   if (diff->rest != NULL_TREE)
    1196              :     return false;
    1197              : 
    1198       973351 :   if (!query)
    1199       973351 :     query = get_range_query (cfun);
    1200              : 
    1201       973351 :   poly_widest_int min_pos = diff->offset;
    1202       973351 :   poly_widest_int max_pos = diff->offset;
    1203              : 
    1204      2071759 :   for (unsigned i = 0; i < diff->n; i++)
    1205              :     {
    1206       993100 :       tree val = diff->elts[i].val;
    1207       993100 :       const widest_int &coef = diff->elts[i].coef;
    1208       993100 :       if (coef == 0)
    1209            0 :         continue;
    1210              : 
    1211       993100 :       tree val_type = TREE_TYPE (val);
    1212       993100 :       if (!INTEGRAL_TYPE_P (val_type))
    1213       868043 :         return false;
    1214              : 
    1215       125081 :       int_range_max vr;
    1216       125081 :       if (!query->range_of_expr (vr, val) || vr.undefined_p ())
    1217           24 :         return false;
    1218              : 
    1219       125057 :       widest_int min_val = widest_int::from (vr.lower_bound (),
    1220       250114 :                                              TYPE_SIGN (val_type));
    1221       125057 :       widest_int max_val = widest_int::from (vr.upper_bound (),
    1222       250114 :                                              TYPE_SIGN (val_type));
    1223              : 
    1224       125057 :       if (coef > 0)
    1225              :         {
    1226       112436 :           min_pos += coef * min_val;
    1227       112436 :           max_pos += coef * max_val;
    1228              :         }
    1229              :       else
    1230              :         {
    1231        12621 :           min_pos += coef * max_val;
    1232        12621 :           max_pos += coef * min_val;
    1233              :         }
    1234       125081 :     }
    1235              : 
    1236       105308 :   unsigned prec = TYPE_PRECISION (diff->type);
    1237       210616 :   poly_widest_int min_limit = widest_int::from (wi::min_value (prec, SIGNED),
    1238       105308 :                                                 SIGNED);
    1239       210616 :   poly_widest_int max_limit = widest_int::from (wi::max_value (prec, SIGNED),
    1240       105308 :                                                 SIGNED);
    1241       105308 :   if (maybe_lt (min_pos, min_limit)
    1242       207892 :       || maybe_gt (max_pos, max_limit))
    1243              :     return false;
    1244              : 
    1245        90671 :   if (known_ge (min_pos, 0))
    1246        33646 :     return known_le (size1, min_pos);
    1247        57025 :   else if (known_le (max_pos, 0))
    1248        10559 :     return known_le (max_pos + size2, 0);
    1249              : 
    1250              :   return false;
    1251       973351 : }
        

Generated by: LCOV version 2.6-beta

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