LCOV - code coverage report
Current view: top level - gcc - tree-data-ref.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 85.2 % 2708 2306
Test Date: 2026-07-11 15:47:05 Functions: 84.3 % 140 118
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Data references and dependences detectors.
       2              :    Copyright (C) 2003-2026 Free Software Foundation, Inc.
       3              :    Contributed by Sebastian Pop <pop@cri.ensmp.fr>
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              : for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : /* This pass walks a given loop structure searching for array
      22              :    references.  The information about the array accesses is recorded
      23              :    in DATA_REFERENCE structures.
      24              : 
      25              :    The basic test for determining the dependences is:
      26              :    given two access functions chrec1 and chrec2 to a same array, and
      27              :    x and y two vectors from the iteration domain, the same element of
      28              :    the array is accessed twice at iterations x and y if and only if:
      29              :    |             chrec1 (x) == chrec2 (y).
      30              : 
      31              :    The goals of this analysis are:
      32              : 
      33              :    - to determine the independence: the relation between two
      34              :      independent accesses is qualified with the chrec_known (this
      35              :      information allows a loop parallelization),
      36              : 
      37              :    - when two data references access the same data, to qualify the
      38              :      dependence relation with classic dependence representations:
      39              : 
      40              :        - distance vectors
      41              :        - direction vectors
      42              :        - loop carried level dependence
      43              :        - polyhedron dependence
      44              :      or with the chains of recurrences based representation,
      45              : 
      46              :    - to define a knowledge base for storing the data dependence
      47              :      information,
      48              : 
      49              :    - to define an interface to access this data.
      50              : 
      51              : 
      52              :    Definitions:
      53              : 
      54              :    - subscript: given two array accesses a subscript is the tuple
      55              :    composed of the access functions for a given dimension.  Example:
      56              :    Given A[f1][f2][f3] and B[g1][g2][g3], there are three subscripts:
      57              :    (f1, g1), (f2, g2), (f3, g3).
      58              : 
      59              :    - Diophantine equation: an equation whose coefficients and
      60              :    solutions are integer constants, for example the equation
      61              :    |   3*x + 2*y = 1
      62              :    has an integer solution x = 1 and y = -1.
      63              : 
      64              :    References:
      65              : 
      66              :    - "Advanced Compilation for High Performance Computing" by Randy
      67              :    Allen and Ken Kennedy.
      68              :    http://citeseer.ist.psu.edu/goff91practical.html
      69              : 
      70              :    - "Loop Transformations for Restructuring Compilers - The Foundations"
      71              :    by Utpal Banerjee.
      72              : 
      73              : 
      74              : */
      75              : 
      76              : #define INCLUDE_ALGORITHM
      77              : #include "config.h"
      78              : #include "system.h"
      79              : #include "coretypes.h"
      80              : #include "backend.h"
      81              : #include "rtl.h"
      82              : #include "tree.h"
      83              : #include "gimple.h"
      84              : #include "gimple-pretty-print.h"
      85              : #include "alias.h"
      86              : #include "fold-const.h"
      87              : #include "expr.h"
      88              : #include "gimple-iterator.h"
      89              : #include "tree-ssa-loop-niter.h"
      90              : #include "tree-ssa-loop.h"
      91              : #include "tree-ssa.h"
      92              : #include "cfgloop.h"
      93              : #include "tree-data-ref.h"
      94              : #include "tree-scalar-evolution.h"
      95              : #include "dumpfile.h"
      96              : #include "tree-affine.h"
      97              : #include "builtins.h"
      98              : #include "tree-eh.h"
      99              : #include "ssa.h"
     100              : #include "internal-fn.h"
     101              : #include "vr-values.h"
     102              : #include "range-op.h"
     103              : #include "tree-ssa-loop-ivopts.h"
     104              : #include "calls.h"
     105              : 
     106              : static struct datadep_stats
     107              : {
     108              :   int num_dependence_tests;
     109              :   int num_dependence_dependent;
     110              :   int num_dependence_independent;
     111              :   int num_dependence_undetermined;
     112              : 
     113              :   int num_subscript_tests;
     114              :   int num_subscript_undetermined;
     115              :   int num_same_subscript_function;
     116              : 
     117              :   int num_ziv;
     118              :   int num_ziv_independent;
     119              :   int num_ziv_dependent;
     120              :   int num_ziv_unimplemented;
     121              : 
     122              :   int num_siv;
     123              :   int num_siv_independent;
     124              :   int num_siv_dependent;
     125              :   int num_siv_unimplemented;
     126              : 
     127              :   int num_miv;
     128              :   int num_miv_independent;
     129              :   int num_miv_dependent;
     130              :   int num_miv_unimplemented;
     131              : } dependence_stats;
     132              : 
     133              : static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
     134              :                                            unsigned int, unsigned int,
     135              :                                            class loop *);
     136              : /* Returns true iff A divides B.  */
     137              : 
     138              : static inline bool
     139         2074 : tree_fold_divides_p (const_tree a, const_tree b)
     140              : {
     141         2074 :   gcc_assert (TREE_CODE (a) == INTEGER_CST);
     142         2074 :   gcc_assert (TREE_CODE (b) == INTEGER_CST);
     143         2074 :   return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a));
     144              : }
     145              : 
     146              : /* Returns true iff A divides B.  */
     147              : 
     148              : static inline bool
     149      1684989 : int_divides_p (lambda_int a, lambda_int b)
     150              : {
     151      1684989 :   return ((b % a) == 0);
     152              : }
     153              : 
     154              : /* Return true if reference REF contains a union access.  */
     155              : 
     156              : static bool
     157       464848 : ref_contains_union_access_p (tree ref)
     158              : {
     159       513018 :   while (handled_component_p (ref))
     160              :     {
     161       106248 :       ref = TREE_OPERAND (ref, 0);
     162       212496 :       if (TREE_CODE (TREE_TYPE (ref)) == UNION_TYPE
     163       106248 :           || TREE_CODE (TREE_TYPE (ref)) == QUAL_UNION_TYPE)
     164              :         return true;
     165              :     }
     166              :   return false;
     167              : }
     168              : 
     169              : 
     170              : 
     171              : /* Dump into FILE all the data references from DATAREFS.  */
     172              : 
     173              : static void
     174            0 : dump_data_references (FILE *file, vec<data_reference_p> datarefs)
     175              : {
     176            0 :   for (data_reference *dr : datarefs)
     177            0 :     dump_data_reference (file, dr);
     178            0 : }
     179              : 
     180              : /* Unified dump into FILE all the data references from DATAREFS.  */
     181              : 
     182              : DEBUG_FUNCTION void
     183            0 : debug (vec<data_reference_p> &ref)
     184              : {
     185            0 :   dump_data_references (stderr, ref);
     186            0 : }
     187              : 
     188              : DEBUG_FUNCTION void
     189            0 : debug (vec<data_reference_p> *ptr)
     190              : {
     191            0 :   if (ptr)
     192            0 :     debug (*ptr);
     193              :   else
     194            0 :     fprintf (stderr, "<nil>\n");
     195            0 : }
     196              : 
     197              : 
     198              : /* Dump into STDERR all the data references from DATAREFS.  */
     199              : 
     200              : DEBUG_FUNCTION void
     201            0 : debug_data_references (vec<data_reference_p> datarefs)
     202              : {
     203            0 :   dump_data_references (stderr, datarefs);
     204            0 : }
     205              : 
     206              : /* Print to STDERR the data_reference DR.  */
     207              : 
     208              : DEBUG_FUNCTION void
     209            0 : debug_data_reference (struct data_reference *dr)
     210              : {
     211            0 :   dump_data_reference (stderr, dr);
     212            0 : }
     213              : 
     214              : /* Dump function for a DATA_REFERENCE structure.  */
     215              : 
     216              : void
     217         3480 : dump_data_reference (FILE *outf,
     218              :                      struct data_reference *dr)
     219              : {
     220         3480 :   unsigned int i;
     221              : 
     222         3480 :   fprintf (outf, "#(Data Ref: \n");
     223         3480 :   fprintf (outf, "#  bb: %d \n", gimple_bb (DR_STMT (dr))->index);
     224         3480 :   fprintf (outf, "#  stmt: ");
     225         3480 :   print_gimple_stmt (outf, DR_STMT (dr), 0);
     226         3480 :   fprintf (outf, "#  ref: ");
     227         3480 :   print_generic_stmt (outf, DR_REF (dr));
     228         3480 :   fprintf (outf, "#  base_object: ");
     229         3480 :   print_generic_stmt (outf, DR_BASE_OBJECT (dr));
     230              : 
     231        10786 :   for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
     232              :     {
     233         3826 :       fprintf (outf, "#  Access function %d: ", i);
     234         3826 :       print_generic_stmt (outf, DR_ACCESS_FN (dr, i));
     235              :     }
     236         3480 :   fprintf (outf, "#)\n");
     237         3480 : }
     238              : 
     239              : /* Unified dump function for a DATA_REFERENCE structure.  */
     240              : 
     241              : DEBUG_FUNCTION void
     242            0 : debug (data_reference &ref)
     243              : {
     244            0 :   dump_data_reference (stderr, &ref);
     245            0 : }
     246              : 
     247              : DEBUG_FUNCTION void
     248            0 : debug (data_reference *ptr)
     249              : {
     250            0 :   if (ptr)
     251            0 :     debug (*ptr);
     252              :   else
     253            0 :     fprintf (stderr, "<nil>\n");
     254            0 : }
     255              : 
     256              : 
     257              : /* Dumps the affine function described by FN to the file OUTF.  */
     258              : 
     259              : DEBUG_FUNCTION void
     260        32720 : dump_affine_function (FILE *outf, affine_fn fn)
     261              : {
     262        32720 :   unsigned i;
     263        32720 :   tree coef;
     264              : 
     265        32720 :   print_generic_expr (outf, fn[0], TDF_SLIM);
     266        69134 :   for (i = 1; fn.iterate (i, &coef); i++)
     267              :     {
     268         3694 :       fprintf (outf, " + ");
     269         3694 :       print_generic_expr (outf, coef, TDF_SLIM);
     270         3694 :       fprintf (outf, " * x_%u", i);
     271              :     }
     272        32720 : }
     273              : 
     274              : /* Dumps the conflict function CF to the file OUTF.  */
     275              : 
     276              : DEBUG_FUNCTION void
     277       160224 : dump_conflict_function (FILE *outf, conflict_function *cf)
     278              : {
     279       160224 :   unsigned i;
     280              : 
     281       160224 :   if (cf->n == NO_DEPENDENCE)
     282       121366 :     fprintf (outf, "no dependence");
     283        38858 :   else if (cf->n == NOT_KNOWN)
     284         6138 :     fprintf (outf, "not known");
     285              :   else
     286              :     {
     287        65440 :       for (i = 0; i < cf->n; i++)
     288              :         {
     289        32720 :           if (i != 0)
     290            0 :             fprintf (outf, " ");
     291        32720 :           fprintf (outf, "[");
     292        32720 :           dump_affine_function (outf, cf->fns[i]);
     293        32720 :           fprintf (outf, "]");
     294              :         }
     295              :     }
     296       160224 : }
     297              : 
     298              : /* Dump function for a SUBSCRIPT structure.  */
     299              : 
     300              : DEBUG_FUNCTION void
     301          838 : dump_subscript (FILE *outf, struct subscript *subscript)
     302              : {
     303          838 :   conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
     304              : 
     305          838 :   fprintf (outf, "\n (subscript \n");
     306          838 :   fprintf (outf, "  iterations_that_access_an_element_twice_in_A: ");
     307          838 :   dump_conflict_function (outf, cf);
     308          838 :   if (CF_NONTRIVIAL_P (cf))
     309              :     {
     310          838 :       tree last_iteration = SUB_LAST_CONFLICT (subscript);
     311          838 :       fprintf (outf, "\n  last_conflict: ");
     312          838 :       print_generic_expr (outf, last_iteration);
     313              :     }
     314              : 
     315          838 :   cf = SUB_CONFLICTS_IN_B (subscript);
     316          838 :   fprintf (outf, "\n  iterations_that_access_an_element_twice_in_B: ");
     317          838 :   dump_conflict_function (outf, cf);
     318          838 :   if (CF_NONTRIVIAL_P (cf))
     319              :     {
     320          838 :       tree last_iteration = SUB_LAST_CONFLICT (subscript);
     321          838 :       fprintf (outf, "\n  last_conflict: ");
     322          838 :       print_generic_expr (outf, last_iteration);
     323              :     }
     324              : 
     325          838 :   fprintf (outf, "\n  (Subscript distance: ");
     326          838 :   print_generic_expr (outf, SUB_DISTANCE (subscript));
     327          838 :   fprintf (outf, " ))\n");
     328          838 : }
     329              : 
     330              : /* Print the classic direction vector DIRV to OUTF.  */
     331              : 
     332              : DEBUG_FUNCTION void
     333          777 : print_direction_vector (FILE *outf,
     334              :                         lambda_vector dirv,
     335              :                         int length)
     336              : {
     337          777 :   int eq;
     338              : 
     339         1683 :   for (eq = 0; eq < length; eq++)
     340              :     {
     341          906 :       enum data_dependence_direction dir = ((enum data_dependence_direction)
     342          906 :                                             dirv[eq]);
     343              : 
     344          906 :       switch (dir)
     345              :         {
     346          139 :         case dir_positive:
     347          139 :           fprintf (outf, "    +");
     348          139 :           break;
     349            6 :         case dir_negative:
     350            6 :           fprintf (outf, "    -");
     351            6 :           break;
     352          761 :         case dir_equal:
     353          761 :           fprintf (outf, "    =");
     354          761 :           break;
     355            0 :         case dir_positive_or_equal:
     356            0 :           fprintf (outf, "   +=");
     357            0 :           break;
     358            0 :         case dir_positive_or_negative:
     359            0 :           fprintf (outf, "   +-");
     360            0 :           break;
     361            0 :         case dir_negative_or_equal:
     362            0 :           fprintf (outf, "   -=");
     363            0 :           break;
     364            0 :         case dir_star:
     365            0 :           fprintf (outf, "    *");
     366            0 :           break;
     367            0 :         default:
     368            0 :           fprintf (outf, "indep");
     369            0 :           break;
     370              :         }
     371              :     }
     372          777 :   fprintf (outf, "\n");
     373          777 : }
     374              : 
     375              : /* Print a vector of direction vectors.  */
     376              : 
     377              : DEBUG_FUNCTION void
     378            0 : print_dir_vectors (FILE *outf, vec<lambda_vector> dir_vects,
     379              :                    int length)
     380              : {
     381            0 :   for (lambda_vector v : dir_vects)
     382            0 :     print_direction_vector (outf, v, length);
     383            0 : }
     384              : 
     385              : /* Print out a vector VEC of length N to OUTFILE.  */
     386              : 
     387              : DEBUG_FUNCTION void
     388         4855 : print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
     389              : {
     390         4855 :   int i;
     391              : 
     392        10122 :   for (i = 0; i < n; i++)
     393         5267 :     fprintf (outfile, HOST_WIDE_INT_PRINT_DEC " ", vector[i]);
     394         4855 :   fprintf (outfile, "\n");
     395         4855 : }
     396              : 
     397              : /* Print a vector of distance vectors.  */
     398              : 
     399              : DEBUG_FUNCTION void
     400            0 : print_dist_vectors (FILE *outf, vec<lambda_vector> dist_vects,
     401              :                     int length)
     402              : {
     403            0 :   for (lambda_vector v : dist_vects)
     404            0 :     print_lambda_vector (outf, v, length);
     405            0 : }
     406              : 
     407              : /* Dump function for a DATA_DEPENDENCE_RELATION structure.  */
     408              : 
     409              : DEBUG_FUNCTION void
     410         1582 : dump_data_dependence_relation (FILE *outf, const data_dependence_relation *ddr)
     411              : {
     412         1582 :   struct data_reference *dra, *drb;
     413              : 
     414         1582 :   fprintf (outf, "(Data Dep: \n");
     415              : 
     416         1582 :   if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
     417              :     {
     418          399 :       if (ddr)
     419              :         {
     420          399 :           dra = DDR_A (ddr);
     421          399 :           drb = DDR_B (ddr);
     422          399 :           if (dra)
     423          399 :             dump_data_reference (outf, dra);
     424              :           else
     425            0 :             fprintf (outf, "    (nil)\n");
     426          399 :           if (drb)
     427          399 :             dump_data_reference (outf, drb);
     428              :           else
     429            0 :             fprintf (outf, "    (nil)\n");
     430              :         }
     431          399 :       fprintf (outf, "    (don't know)\n)\n");
     432          399 :       return;
     433              :     }
     434              : 
     435         1183 :   dra = DDR_A (ddr);
     436         1183 :   drb = DDR_B (ddr);
     437         1183 :   dump_data_reference (outf, dra);
     438         1183 :   dump_data_reference (outf, drb);
     439              : 
     440         1183 :   if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
     441          426 :     fprintf (outf, "    (no dependence)\n");
     442              : 
     443          757 :   else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
     444              :     {
     445              :       unsigned int i;
     446              :       class loop *loopi;
     447              : 
     448              :       subscript *sub;
     449         1595 :       FOR_EACH_VEC_ELT (DDR_SUBSCRIPTS (ddr), i, sub)
     450              :         {
     451          838 :           fprintf (outf, "  access_fn_A: ");
     452          838 :           print_generic_stmt (outf, SUB_ACCESS_FN (sub, 0));
     453          838 :           fprintf (outf, "  access_fn_B: ");
     454          838 :           print_generic_stmt (outf, SUB_ACCESS_FN (sub, 1));
     455          838 :           dump_subscript (outf, sub);
     456              :         }
     457              : 
     458          757 :       fprintf (outf, "  loop nest: (");
     459         2374 :       FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
     460          860 :         fprintf (outf, "%d ", loopi->num);
     461          757 :       fprintf (outf, ")\n");
     462              : 
     463         3820 :       for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
     464              :         {
     465          777 :           fprintf (outf, "  distance_vector: ");
     466          777 :           print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
     467         1554 :                                DDR_NB_LOOPS (ddr));
     468              :         }
     469              : 
     470         1534 :       for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
     471              :         {
     472          777 :           fprintf (outf, "  direction_vector: ");
     473          777 :           print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
     474         1554 :                                   DDR_NB_LOOPS (ddr));
     475              :         }
     476              :     }
     477              : 
     478         1183 :   fprintf (outf, ")\n");
     479              : }
     480              : 
     481              : /* Debug version.  */
     482              : 
     483              : DEBUG_FUNCTION void
     484            0 : debug_data_dependence_relation (const struct data_dependence_relation *ddr)
     485              : {
     486            0 :   dump_data_dependence_relation (stderr, ddr);
     487            0 : }
     488              : 
     489              : /* Dump into FILE all the dependence relations from DDRS.  */
     490              : 
     491              : DEBUG_FUNCTION void
     492          307 : dump_data_dependence_relations (FILE *file, const vec<ddr_p> &ddrs)
     493              : {
     494         2473 :   for (auto ddr : ddrs)
     495         1582 :     dump_data_dependence_relation (file, ddr);
     496          307 : }
     497              : 
     498              : DEBUG_FUNCTION void
     499            0 : debug (vec<ddr_p> &ref)
     500              : {
     501            0 :   dump_data_dependence_relations (stderr, ref);
     502            0 : }
     503              : 
     504              : DEBUG_FUNCTION void
     505            0 : debug (vec<ddr_p> *ptr)
     506              : {
     507            0 :   if (ptr)
     508            0 :     debug (*ptr);
     509              :   else
     510            0 :     fprintf (stderr, "<nil>\n");
     511            0 : }
     512              : 
     513              : 
     514              : /* Dump to STDERR all the dependence relations from DDRS.  */
     515              : 
     516              : DEBUG_FUNCTION void
     517            0 : debug_data_dependence_relations (vec<ddr_p> ddrs)
     518              : {
     519            0 :   dump_data_dependence_relations (stderr, ddrs);
     520            0 : }
     521              : 
     522              : /* Dumps the distance and direction vectors in FILE.  DDRS contains
     523              :    the dependence relations, and VECT_SIZE is the size of the
     524              :    dependence vectors, or in other words the number of loops in the
     525              :    considered nest.  */
     526              : 
     527              : DEBUG_FUNCTION void
     528            0 : dump_dist_dir_vectors (FILE *file, vec<ddr_p> ddrs)
     529              : {
     530            0 :   for (data_dependence_relation *ddr : ddrs)
     531            0 :     if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
     532              :       {
     533            0 :         for (lambda_vector v : DDR_DIST_VECTS (ddr))
     534              :           {
     535            0 :             fprintf (file, "DISTANCE_V (");
     536            0 :             print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
     537            0 :             fprintf (file, ")\n");
     538              :           }
     539              : 
     540            0 :         for (lambda_vector v : DDR_DIR_VECTS (ddr))
     541              :           {
     542            0 :             fprintf (file, "DIRECTION_V (");
     543            0 :             print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
     544            0 :             fprintf (file, ")\n");
     545              :           }
     546              :       }
     547              : 
     548            0 :   fprintf (file, "\n\n");
     549            0 : }
     550              : 
     551              : /* Dumps the data dependence relations DDRS in FILE.  */
     552              : 
     553              : DEBUG_FUNCTION void
     554            0 : dump_ddrs (FILE *file, vec<ddr_p> ddrs)
     555              : {
     556            0 :   for (data_dependence_relation *ddr : ddrs)
     557            0 :     dump_data_dependence_relation (file, ddr);
     558              : 
     559            0 :   fprintf (file, "\n\n");
     560            0 : }
     561              : 
     562              : DEBUG_FUNCTION void
     563            0 : debug_ddrs (vec<ddr_p> ddrs)
     564              : {
     565            0 :   dump_ddrs (stderr, ddrs);
     566            0 : }
     567              : 
     568              : /* If RESULT_RANGE is nonnull, set *RESULT_RANGE to the range of
     569              :    OP0 CODE OP1, where:
     570              : 
     571              :    - OP0 CODE OP1 has integral type TYPE
     572              :    - the range of OP0 is given by OP0_RANGE and
     573              :    - the range of OP1 is given by OP1_RANGE.
     574              : 
     575              :    Independently of RESULT_RANGE, try to compute:
     576              : 
     577              :      DELTA = ((sizetype) OP0 CODE (sizetype) OP1)
     578              :              - (sizetype) (OP0 CODE OP1)
     579              : 
     580              :    as a constant and subtract DELTA from the ssizetype constant in *OFF.
     581              :    Return true on success, or false if DELTA is not known at compile time.
     582              : 
     583              :    Truncation and sign changes are known to distribute over CODE, i.e.
     584              : 
     585              :      (itype) (A CODE B) == (itype) A CODE (itype) B
     586              : 
     587              :    for any integral type ITYPE whose precision is no greater than the
     588              :    precision of A and B.  */
     589              : 
     590              : static bool
     591      4598261 : compute_distributive_range (tree type, irange &op0_range,
     592              :                             tree_code code, irange &op1_range,
     593              :                             tree *off, irange *result_range)
     594              : {
     595      4598261 :   gcc_assert (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_TRAPS (type));
     596      4598261 :   if (result_range)
     597              :     {
     598      1065648 :       range_op_handler op (code);
     599      1065648 :       if (!op.fold_range (*result_range, type, op0_range, op1_range))
     600            0 :         result_range->set_varying (type);
     601              :     }
     602              : 
     603              :   /* The distributive property guarantees that if TYPE is no narrower
     604              :      than SIZETYPE,
     605              : 
     606              :        (sizetype) (OP0 CODE OP1) == (sizetype) OP0 CODE (sizetype) OP1
     607              : 
     608              :      and so we can treat DELTA as zero.  */
     609      4598261 :   if (TYPE_PRECISION (type) >= TYPE_PRECISION (sizetype))
     610              :     return true;
     611              : 
     612              :   /* If overflow is undefined, we can assume that:
     613              : 
     614              :        X == (ssizetype) OP0 CODE (ssizetype) OP1
     615              : 
     616              :      is within the range of TYPE, i.e.:
     617              : 
     618              :        X == (ssizetype) (TYPE) X
     619              : 
     620              :      Distributing the (TYPE) truncation over X gives:
     621              : 
     622              :        X == (ssizetype) (OP0 CODE OP1)
     623              : 
     624              :      Casting both sides to sizetype and distributing the sizetype cast
     625              :      over X gives:
     626              : 
     627              :        (sizetype) OP0 CODE (sizetype) OP1 == (sizetype) (OP0 CODE OP1)
     628              : 
     629              :      and so we can treat DELTA as zero.  */
     630       266776 :   if (TYPE_OVERFLOW_UNDEFINED (type))
     631              :     return true;
     632              : 
     633              :   /* Compute the range of:
     634              : 
     635              :        (ssizetype) OP0 CODE (ssizetype) OP1
     636              : 
     637              :      The distributive property guarantees that this has the same bitpattern as:
     638              : 
     639              :        (sizetype) OP0 CODE (sizetype) OP1
     640              : 
     641              :      but its range is more conducive to analysis.  */
     642       102608 :   range_cast (op0_range, ssizetype);
     643       102608 :   range_cast (op1_range, ssizetype);
     644       102608 :   int_range_max wide_range;
     645       102608 :   range_op_handler op (code);
     646       102608 :   bool saved_flag_wrapv = flag_wrapv;
     647       102608 :   flag_wrapv = 1;
     648       102608 :   if (!op.fold_range (wide_range, ssizetype, op0_range, op1_range))
     649            0 :     wide_range.set_varying (ssizetype);;
     650       102608 :   flag_wrapv = saved_flag_wrapv;
     651       102608 :   if (wide_range.num_pairs () != 1
     652       102608 :       || wide_range.varying_p () || wide_range.undefined_p ())
     653              :     return false;
     654              : 
     655        82186 :   wide_int lb = wide_range.lower_bound ();
     656        82186 :   wide_int ub = wide_range.upper_bound ();
     657              : 
     658              :   /* Calculate the number of times that each end of the range overflows or
     659              :      underflows TYPE.  We can only calculate DELTA if the numbers match.  */
     660        82186 :   unsigned int precision = TYPE_PRECISION (type);
     661        82186 :   if (!TYPE_UNSIGNED (type))
     662              :     {
     663          206 :       wide_int type_min = wi::mask (precision - 1, true, lb.get_precision ());
     664          206 :       lb -= type_min;
     665          206 :       ub -= type_min;
     666          206 :     }
     667        82186 :   wide_int upper_bits = wi::mask (precision, true, lb.get_precision ());
     668        82186 :   lb &= upper_bits;
     669        82186 :   ub &= upper_bits;
     670        82186 :   if (lb != ub)
     671              :     return false;
     672              : 
     673              :   /* OP0 CODE OP1 overflows exactly arshift (LB, PRECISION) times, with
     674              :      negative values indicating underflow.  The low PRECISION bits of LB
     675              :      are clear, so DELTA is therefore LB (== UB).  */
     676        24641 :   *off = wide_int_to_tree (ssizetype, wi::to_wide (*off) - lb);
     677        24641 :   return true;
     678       102608 : }
     679              : 
     680              : /* Return true if (sizetype) OP == (sizetype) (TO_TYPE) OP,
     681              :    given that OP has type FROM_TYPE and range RANGE.  Both TO_TYPE and
     682              :    FROM_TYPE are integral types.  */
     683              : 
     684              : static bool
     685      2631196 : nop_conversion_for_offset_p (tree to_type, tree from_type, irange &range)
     686              : {
     687      2631196 :   gcc_assert (INTEGRAL_TYPE_P (to_type)
     688              :               && INTEGRAL_TYPE_P (from_type)
     689              :               && !TYPE_OVERFLOW_TRAPS (to_type)
     690              :               && !TYPE_OVERFLOW_TRAPS (from_type));
     691              : 
     692              :   /* Converting to something no narrower than sizetype and then to sizetype
     693              :      is equivalent to converting directly to sizetype.  */
     694      2631196 :   if (TYPE_PRECISION (to_type) >= TYPE_PRECISION (sizetype))
     695              :     return true;
     696              : 
     697              :   /* Check whether TO_TYPE can represent all values that FROM_TYPE can.  */
     698        88100 :   if (TYPE_PRECISION (from_type) < TYPE_PRECISION (to_type)
     699        88100 :       && (TYPE_UNSIGNED (from_type) || !TYPE_UNSIGNED (to_type)))
     700              :     return true;
     701              : 
     702              :   /* For narrowing conversions, we could in principle test whether
     703              :      the bits in FROM_TYPE but not in TO_TYPE have a fixed value
     704              :      and apply a constant adjustment.
     705              : 
     706              :      For other conversions (which involve a sign change) we could
     707              :      check that the signs are always equal, and apply a constant
     708              :      adjustment if the signs are negative.
     709              : 
     710              :      However, both cases should be rare.  */
     711        73264 :   return range_fits_type_p (&range, TYPE_PRECISION (to_type),
     712       146528 :                             TYPE_SIGN (to_type));
     713              : }
     714              : 
     715              : static void
     716              : split_constant_offset (tree type, tree *var, tree *off,
     717              :                        irange *result_range,
     718              :                        hash_map<tree, std::pair<tree, tree> > &cache,
     719              :                        unsigned *limit);
     720              : 
     721              : /* Helper function for split_constant_offset.  If TYPE is a pointer type,
     722              :    try to express OP0 CODE OP1 as:
     723              : 
     724              :      POINTER_PLUS <*VAR, (sizetype) *OFF>
     725              : 
     726              :    where:
     727              : 
     728              :    - *VAR has type TYPE
     729              :    - *OFF is a constant of type ssizetype.
     730              : 
     731              :    If TYPE is an integral type, try to express (sizetype) (OP0 CODE OP1) as:
     732              : 
     733              :      *VAR + (sizetype) *OFF
     734              : 
     735              :    where:
     736              : 
     737              :    - *VAR has type sizetype
     738              :    - *OFF is a constant of type ssizetype.
     739              : 
     740              :    In both cases, OP0 CODE OP1 has type TYPE.
     741              : 
     742              :    Return true on success.  A false return value indicates that we can't
     743              :    do better than set *OFF to zero.
     744              : 
     745              :    When returning true, set RESULT_RANGE to the range of OP0 CODE OP1,
     746              :    if RESULT_RANGE is nonnull and if we can do better than assume VR_VARYING.
     747              : 
     748              :    CACHE caches {*VAR, *OFF} pairs for SSA names that we've previously
     749              :    visited.  LIMIT counts down the number of SSA names that we are
     750              :    allowed to process before giving up.  */
     751              : 
     752              : static bool
     753     59224149 : split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
     754              :                          tree *var, tree *off, irange *result_range,
     755              :                          hash_map<tree, std::pair<tree, tree> > &cache,
     756              :                          unsigned *limit)
     757              : {
     758     59224149 :   tree var0, var1;
     759     59224149 :   tree off0, off1;
     760     59224149 :   int_range_max op0_range, op1_range;
     761              : 
     762     59224149 :   *var = NULL_TREE;
     763     59224149 :   *off = NULL_TREE;
     764              : 
     765     59224149 :   if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
     766              :     return false;
     767              : 
     768     59223527 :   if (TREE_CODE (op0) == SSA_NAME
     769     59223527 :       && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
     770              :     return false;
     771     59223132 :   if (op1
     772      7553268 :       && TREE_CODE (op1) == SSA_NAME
     773     61585341 :       && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))
     774              :     return false;
     775              : 
     776     59223132 :   switch (code)
     777              :     {
     778     17713568 :     case INTEGER_CST:
     779     17713568 :       *var = size_int (0);
     780     17713568 :       *off = fold_convert (ssizetype, op0);
     781     17713568 :       if (result_range)
     782              :         {
     783      1430785 :           wide_int w = wi::to_wide (op0);
     784      1430785 :           result_range->set (TREE_TYPE (op0), w, w);
     785      1430785 :         }
     786              :       return true;
     787              : 
     788      2162081 :     case POINTER_PLUS_EXPR:
     789      2162081 :       split_constant_offset (op0, &var0, &off0, nullptr, cache, limit);
     790      2162081 :       split_constant_offset (op1, &var1, &off1, nullptr, cache, limit);
     791      2162081 :       *var = fold_build2 (POINTER_PLUS_EXPR, type, var0, var1);
     792      2162081 :       *off = size_binop (PLUS_EXPR, off0, off1);
     793      2162081 :       return true;
     794              : 
     795      2442699 :     case PLUS_EXPR:
     796      2442699 :     case MINUS_EXPR:
     797      2442699 :       split_constant_offset (op0, &var0, &off0, &op0_range, cache, limit);
     798      2442699 :       split_constant_offset (op1, &var1, &off1, &op1_range, cache, limit);
     799      2442699 :       *off = size_binop (code, off0, off1);
     800      2442699 :       if (!compute_distributive_range (type, op0_range, code, op1_range,
     801              :                                        off, result_range))
     802              :         return false;
     803      2384986 :       *var = fold_build2 (code, sizetype, var0, var1);
     804      2384986 :       return true;
     805              : 
     806      2608416 :     case MULT_EXPR:
     807      2608416 :       if (TREE_CODE (op1) != INTEGER_CST)
     808              :         return false;
     809              : 
     810      2155562 :       split_constant_offset (op0, &var0, &off0, &op0_range, cache, limit);
     811      2155562 :       op1_range.set (TREE_TYPE (op1), wi::to_wide (op1), wi::to_wide (op1));
     812      2155562 :       *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
     813      2155562 :       if (!compute_distributive_range (type, op0_range, code, op1_range,
     814              :                                        off, result_range))
     815              :         return false;
     816      2135308 :       *var = fold_build2 (MULT_EXPR, sizetype, var0,
     817              :                           fold_convert (sizetype, op1));
     818      2135308 :       return true;
     819              : 
     820     10439870 :     case ADDR_EXPR:
     821     10439870 :       {
     822     10439870 :         tree base, poffset;
     823     10439870 :         poly_int64 pbitsize, pbitpos, pbytepos;
     824     10439870 :         machine_mode pmode;
     825     10439870 :         int punsignedp, preversep, pvolatilep;
     826              : 
     827     10439870 :         op0 = TREE_OPERAND (op0, 0);
     828     10439870 :         base
     829     10439870 :           = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset, &pmode,
     830              :                                  &punsignedp, &preversep, &pvolatilep);
     831              : 
     832     10465947 :         if (!multiple_p (pbitpos, BITS_PER_UNIT, &pbytepos))
     833              :           return false;
     834     10439870 :         base = build_fold_addr_expr (base);
     835     10439870 :         off0 = ssize_int (pbytepos);
     836              : 
     837     10439870 :         if (poffset)
     838              :           {
     839         1532 :             split_constant_offset (poffset, &poffset, &off1, nullptr,
     840              :                                    cache, limit);
     841         1532 :             off0 = size_binop (PLUS_EXPR, off0, off1);
     842         1532 :             base = fold_build_pointer_plus (base, poffset);
     843              :           }
     844              : 
     845     10439870 :         var0 = fold_convert (type, base);
     846              : 
     847              :         /* If variable length types are involved, punt, otherwise casts
     848              :            might be converted into ARRAY_REFs in gimplify_conversion.
     849              :            To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
     850              :            possibly no longer appears in current GIMPLE, might resurface.
     851              :            This perhaps could run
     852              :            if (CONVERT_EXPR_P (var0))
     853              :              {
     854              :                gimplify_conversion (&var0);
     855              :                // Attempt to fill in any within var0 found ARRAY_REF's
     856              :                // element size from corresponding op embedded ARRAY_REF,
     857              :                // if unsuccessful, just punt.
     858              :              }  */
     859     21291939 :         while (POINTER_TYPE_P (type))
     860     10852069 :           type = TREE_TYPE (type);
     861     10439870 :         if (int_size_in_bytes (type) < 0)
     862              :           return false;
     863              : 
     864     10413793 :         *var = var0;
     865     10413793 :         *off = off0;
     866     10413793 :         return true;
     867              :       }
     868              : 
     869     16026443 :     case SSA_NAME:
     870     16026443 :       {
     871     16026443 :         gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
     872     16026443 :         enum tree_code subcode;
     873              : 
     874     16026443 :         if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
     875              :           return false;
     876              : 
     877      8723658 :         subcode = gimple_assign_rhs_code (def_stmt);
     878              : 
     879              :         /* We are using a cache to avoid un-CSEing large amounts of code.  */
     880      8723658 :         bool use_cache = false;
     881      8723658 :         if (!has_single_use (op0)
     882      8723658 :             && (subcode == POINTER_PLUS_EXPR
     883      4490551 :                 || subcode == PLUS_EXPR
     884              :                 || subcode == MINUS_EXPR
     885              :                 || subcode == MULT_EXPR
     886              :                 || subcode == ADDR_EXPR
     887              :                 || CONVERT_EXPR_CODE_P (subcode)))
     888              :           {
     889      2164230 :             use_cache = true;
     890      2164230 :             bool existed;
     891      2164230 :             std::pair<tree, tree> &e = cache.get_or_insert (op0, &existed);
     892      2164230 :             if (existed)
     893              :               {
     894        31711 :                 if (integer_zerop (e.second))
     895        31711 :                   return false;
     896         1194 :                 *var = e.first;
     897         1194 :                 *off = e.second;
     898              :                 /* The caller sets the range in this case.  */
     899         1194 :                 return true;
     900              :               }
     901      2132519 :             e = std::make_pair (op0, ssize_int (0));
     902              :           }
     903              : 
     904      8691947 :         if (*limit == 0)
     905              :           return false;
     906      8690911 :         --*limit;
     907              : 
     908      8690911 :         var0 = gimple_assign_rhs1 (def_stmt);
     909      8690911 :         var1 = gimple_assign_rhs2 (def_stmt);
     910              : 
     911      8690911 :         bool res = split_constant_offset_1 (type, var0, subcode, var1,
     912              :                                             var, off, nullptr, cache, limit);
     913      8690911 :         if (res && use_cache)
     914      1911299 :           *cache.get (op0) = std::make_pair (*var, *off);
     915              :         /* The caller sets the range in this case.  */
     916              :         return res;
     917              :       }
     918      4378163 :     CASE_CONVERT:
     919      4378163 :       {
     920              :         /* We can only handle the following conversions:
     921              : 
     922              :            - Conversions from one pointer type to another pointer type.
     923              : 
     924              :            - Conversions from one non-trapping integral type to another
     925              :              non-trapping integral type.  In this case, the recursive
     926              :              call makes sure that:
     927              : 
     928              :                (sizetype) OP0
     929              : 
     930              :              can be expressed as a sizetype operation involving VAR and OFF,
     931              :              and all we need to do is check whether:
     932              : 
     933              :                (sizetype) OP0 == (sizetype) (TYPE) OP0
     934              : 
     935              :            - Conversions from a non-trapping sizetype-size integral type to
     936              :              a like-sized pointer type.  In this case, the recursive call
     937              :              makes sure that:
     938              : 
     939              :                (sizetype) OP0 == *VAR + (sizetype) *OFF
     940              : 
     941              :              and we can convert that to:
     942              : 
     943              :                POINTER_PLUS <(TYPE) *VAR, (sizetype) *OFF>
     944              : 
     945              :            - Conversions from a sizetype-sized pointer type to a like-sized
     946              :              non-trapping integral type.  In this case, the recursive call
     947              :              makes sure that:
     948              : 
     949              :                OP0 == POINTER_PLUS <*VAR, (sizetype) *OFF>
     950              : 
     951              :              where the POINTER_PLUS and *VAR have the same precision as
     952              :              TYPE (and the same precision as sizetype).  Then:
     953              : 
     954              :                (sizetype) (TYPE) OP0 == (sizetype) *VAR + (sizetype) *OFF.  */
     955      4378163 :         tree itype = TREE_TYPE (op0);
     956      4378163 :         if ((POINTER_TYPE_P (itype)
     957      3202145 :              || (INTEGRAL_TYPE_P (itype) && !TYPE_OVERFLOW_TRAPS (itype)))
     958      4377746 :             && (POINTER_TYPE_P (type)
     959      3147736 :                 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_TRAPS (type)))
     960      8755909 :             && (POINTER_TYPE_P (type) == POINTER_TYPE_P (itype)
     961      1087072 :                 || (TYPE_PRECISION (type) == TYPE_PRECISION (sizetype)
     962      1087072 :                     && TYPE_PRECISION (itype) == TYPE_PRECISION (sizetype))))
     963              :           {
     964      4377739 :             if (POINTER_TYPE_P (type))
     965              :               {
     966      1230003 :                 split_constant_offset (op0, var, off, nullptr, cache, limit);
     967      1230003 :                 *var = fold_convert (type, *var);
     968              :               }
     969      3147736 :             else if (POINTER_TYPE_P (itype))
     970              :               {
     971       516540 :                 split_constant_offset (op0, var, off, nullptr, cache, limit);
     972       516540 :                 *var = fold_convert (sizetype, *var);
     973              :               }
     974              :             else
     975              :               {
     976      2631196 :                 split_constant_offset (op0, var, off, &op0_range,
     977              :                                        cache, limit);
     978      2631196 :                 if (!nop_conversion_for_offset_p (type, itype, op0_range))
     979              :                   return false;
     980      2578964 :                 if (result_range)
     981              :                   {
     982      1333239 :                     *result_range = op0_range;
     983      1333239 :                     range_cast (*result_range, type);
     984              :                   }
     985              :               }
     986      4325507 :             return true;
     987              :           }
     988              :         return false;
     989              :       }
     990              : 
     991              :     default:
     992              :       return false;
     993              :     }
     994     59224149 : }
     995              : 
     996              : /* If EXP has pointer type, try to express it as:
     997              : 
     998              :      POINTER_PLUS <*VAR, (sizetype) *OFF>
     999              : 
    1000              :    where:
    1001              : 
    1002              :    - *VAR has the same type as EXP
    1003              :    - *OFF is a constant of type ssizetype.
    1004              : 
    1005              :    If EXP has an integral type, try to express (sizetype) EXP as:
    1006              : 
    1007              :      *VAR + (sizetype) *OFF
    1008              : 
    1009              :    where:
    1010              : 
    1011              :    - *VAR has type sizetype
    1012              :    - *OFF is a constant of type ssizetype.
    1013              : 
    1014              :    If EXP_RANGE is nonnull, set it to the range of EXP.
    1015              : 
    1016              :    CACHE caches {*VAR, *OFF} pairs for SSA names that we've previously
    1017              :    visited.  LIMIT counts down the number of SSA names that we are
    1018              :    allowed to process before giving up.  */
    1019              : 
    1020              : static void
    1021     50533259 : split_constant_offset (tree exp, tree *var, tree *off, irange *exp_range,
    1022              :                        hash_map<tree, std::pair<tree, tree> > &cache,
    1023              :                        unsigned *limit)
    1024              : {
    1025     50533259 :   tree type = TREE_TYPE (exp), op0, op1;
    1026     50533259 :   enum tree_code code;
    1027              : 
    1028     50533259 :   code = TREE_CODE (exp);
    1029     50533259 :   if (exp_range)
    1030              :     {
    1031      9672156 :       exp_range->set_varying (type);
    1032      9672156 :       if (code == SSA_NAME)
    1033              :         {
    1034      5325607 :           int_range_max vr;
    1035     10651214 :           get_range_query (cfun)->range_of_expr (vr, exp);
    1036      5325607 :           if (vr.undefined_p ())
    1037         4880 :             vr.set_varying (TREE_TYPE (exp));
    1038      5325607 :           tree vr_min, vr_max;
    1039      5325607 :           value_range_kind vr_kind = get_legacy_range (vr, vr_min, vr_max);
    1040      5325607 :           wide_int var_min = wi::to_wide (vr_min);
    1041      5325607 :           wide_int var_max = wi::to_wide (vr_max);
    1042      5325607 :           wide_int var_nonzero = get_nonzero_bits (exp);
    1043     15976821 :           vr_kind = intersect_range_with_nonzero_bits (vr_kind,
    1044              :                                                        &var_min, &var_max,
    1045              :                                                        var_nonzero,
    1046      5325607 :                                                        TYPE_SIGN (type));
    1047              :           /* This check for VR_VARYING is here because the old code
    1048              :              using get_range_info would return VR_RANGE for the entire
    1049              :              domain, instead of VR_VARYING.  The new code normalizes
    1050              :              full-domain ranges to VR_VARYING.  */
    1051      5325607 :           if (vr_kind == VR_RANGE || vr_kind == VR_VARYING)
    1052      5206693 :             exp_range->set (type, var_min, var_max);
    1053      5325607 :         }
    1054              :     }
    1055              : 
    1056     50533259 :   if (!tree_is_chrec (exp)
    1057     50533253 :       && get_gimple_rhs_class (TREE_CODE (exp)) != GIMPLE_TERNARY_RHS)
    1058              :     {
    1059     50533238 :       extract_ops_from_tree (exp, &code, &op0, &op1);
    1060     50533238 :       if (split_constant_offset_1 (type, op0, code, op1, var, off,
    1061              :                                    exp_range, cache, limit))
    1062     39136437 :         return;
    1063              :     }
    1064              : 
    1065     11396822 :   *var = exp;
    1066     11396822 :   if (INTEGRAL_TYPE_P (type))
    1067      3453627 :     *var = fold_convert (sizetype, *var);
    1068     11396822 :   *off = ssize_int (0);
    1069              : 
    1070     11396822 :   int_range_max r;
    1071      3139415 :   if (exp_range && code != SSA_NAME
    1072       108120 :       && get_range_query (cfun)->range_of_expr (r, exp)
    1073     11450882 :       && !r.undefined_p ())
    1074        54060 :     *exp_range = r;
    1075     11396822 : }
    1076              : 
    1077              : /* Expresses EXP as VAR + OFF, where OFF is a constant.  VAR has the same
    1078              :    type as EXP while OFF has type ssizetype.  */
    1079              : 
    1080              : void
    1081     34788866 : split_constant_offset (tree exp, tree *var, tree *off)
    1082              : {
    1083     34788866 :   unsigned limit = param_ssa_name_def_chain_limit;
    1084     34788866 :   static hash_map<tree, std::pair<tree, tree> > *cache;
    1085     34788866 :   if (!cache)
    1086        80705 :     cache = new hash_map<tree, std::pair<tree, tree> > (37);
    1087     34788866 :   split_constant_offset (exp, var, off, nullptr, *cache, &limit);
    1088     34788866 :   *var = fold_convert (TREE_TYPE (exp), *var);
    1089     34788866 :   cache->empty ();
    1090     34788866 : }
    1091              : 
    1092              : /* Returns the address ADDR of an object in a canonical shape (without nop
    1093              :    casts, and with type of pointer to the object).  */
    1094              : 
    1095              : static tree
    1096     16366852 : canonicalize_base_object_address (tree addr)
    1097              : {
    1098     16366852 :   tree orig = addr;
    1099              : 
    1100     16366852 :   STRIP_NOPS (addr);
    1101              : 
    1102              :   /* The base address may be obtained by casting from integer, in that case
    1103              :      keep the cast.  */
    1104     16366852 :   if (!POINTER_TYPE_P (TREE_TYPE (addr)))
    1105              :     return orig;
    1106              : 
    1107     16294727 :   if (TREE_CODE (addr) != ADDR_EXPR)
    1108              :     return addr;
    1109              : 
    1110      9715904 :   return build_fold_addr_expr (TREE_OPERAND (addr, 0));
    1111              : }
    1112              : 
    1113              : /* Analyze the behavior of memory reference REF within STMT.
    1114              :    There are two modes:
    1115              : 
    1116              :    - BB analysis.  In this case we simply split the address into base,
    1117              :      init and offset components, without reference to any containing loop.
    1118              :      The resulting base and offset are general expressions and they can
    1119              :      vary arbitrarily from one iteration of the containing loop to the next.
    1120              :      The step is always zero.
    1121              : 
    1122              :    - loop analysis.  In this case we analyze the reference both wrt LOOP
    1123              :      and on the basis that the reference occurs (is "used") in LOOP;
    1124              :      see the comment above analyze_scalar_evolution_in_loop for more
    1125              :      information about this distinction.  The base, init, offset and
    1126              :      step fields are all invariant in LOOP.
    1127              : 
    1128              :    Perform BB analysis if LOOP is null, or if LOOP is the function's
    1129              :    dummy outermost loop.  In other cases perform loop analysis.
    1130              : 
    1131              :    Return true if the analysis succeeded and store the results in DRB if so.
    1132              :    BB analysis can only fail for bitfield or reversed-storage accesses.  */
    1133              : 
    1134              : opt_result
    1135     16924530 : dr_analyze_innermost (innermost_loop_behavior *drb, tree ref,
    1136              :                       class loop *loop, const gimple *stmt)
    1137              : {
    1138     16924530 :   poly_int64 pbitsize, pbitpos;
    1139     16924530 :   tree base, poffset;
    1140     16924530 :   machine_mode pmode;
    1141     16924530 :   int punsignedp, preversep, pvolatilep;
    1142     16924530 :   affine_iv base_iv, offset_iv;
    1143     16924530 :   tree init, dinit, step;
    1144     16924530 :   bool in_loop = (loop && loop->num);
    1145              : 
    1146     16924530 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1147        68221 :     fprintf (dump_file, "analyze_innermost: ");
    1148              : 
    1149     16924530 :   base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset, &pmode,
    1150              :                               &punsignedp, &preversep, &pvolatilep);
    1151     16924530 :   gcc_assert (base != NULL_TREE);
    1152              : 
    1153     16924530 :   poly_int64 pbytepos;
    1154     16924530 :   if (!multiple_p (pbitpos, BITS_PER_UNIT, &pbytepos))
    1155        38194 :     return opt_result::failure_at (stmt,
    1156              :                                    "failed: bit offset alignment.\n");
    1157              : 
    1158     16886336 :   if (preversep)
    1159          653 :     return opt_result::failure_at (stmt,
    1160              :                                    "failed: reverse storage order.\n");
    1161              : 
    1162              :   /* Calculate the alignment and misalignment for the inner reference.  */
    1163     16885683 :   unsigned int HOST_WIDE_INT bit_base_misalignment;
    1164     16885683 :   unsigned int bit_base_alignment;
    1165     16885683 :   get_object_alignment_1 (base, &bit_base_alignment, &bit_base_misalignment);
    1166              : 
    1167              :   /* There are no bitfield references remaining in BASE, so the values
    1168              :      we got back must be whole bytes.  */
    1169     16885683 :   gcc_assert (bit_base_alignment % BITS_PER_UNIT == 0
    1170              :               && bit_base_misalignment % BITS_PER_UNIT == 0);
    1171     16885683 :   unsigned int base_alignment = bit_base_alignment / BITS_PER_UNIT;
    1172     16885683 :   poly_int64 base_misalignment = bit_base_misalignment / BITS_PER_UNIT;
    1173              : 
    1174     16885683 :   if (TREE_CODE (base) == MEM_REF)
    1175              :     {
    1176      7296454 :       if (!integer_zerop (TREE_OPERAND (base, 1)))
    1177              :         {
    1178              :           /* Subtract MOFF from the base and add it to POFFSET instead.
    1179              :              Adjust the misalignment to reflect the amount we subtracted.  */
    1180      1311089 :           poly_offset_int moff = mem_ref_offset (base);
    1181      1311089 :           base_misalignment -= moff.force_shwi ();
    1182      1311089 :           tree mofft = wide_int_to_tree (sizetype, moff);
    1183      1311089 :           if (!poffset)
    1184      1301092 :             poffset = mofft;
    1185              :           else
    1186         9997 :             poffset = size_binop (PLUS_EXPR, poffset, mofft);
    1187              :         }
    1188      7296454 :       base = TREE_OPERAND (base, 0);
    1189              :     }
    1190              :   else
    1191              :     {
    1192      9589229 :       if (may_be_nonaddressable_p (base))
    1193         2072 :         return opt_result::failure_at (stmt,
    1194              :                                        "failed: base not addressable.\n");
    1195      9587157 :       base = build_fold_addr_expr (base);
    1196              :     }
    1197              : 
    1198     16883611 :   if (in_loop)
    1199              :     {
    1200      3221269 :       if (!simple_iv (loop, loop, base, &base_iv, true))
    1201       434618 :         return opt_result::failure_at
    1202       434618 :           (stmt, "failed: evolution of base is not affine.\n");
    1203              :     }
    1204              :   else
    1205              :     {
    1206     13662342 :       base_iv.base = base;
    1207     13662342 :       base_iv.step = ssize_int (0);
    1208     13662342 :       base_iv.no_overflow = true;
    1209              :     }
    1210              : 
    1211     16448993 :   if (!poffset)
    1212              :     {
    1213     13610143 :       offset_iv.base = ssize_int (0);
    1214     13610143 :       offset_iv.step = ssize_int (0);
    1215              :     }
    1216              :   else
    1217              :     {
    1218      2838850 :       if (!in_loop)
    1219              :         {
    1220      1533459 :           offset_iv.base = poffset;
    1221      1533459 :           offset_iv.step = ssize_int (0);
    1222              :         }
    1223      1305391 :       else if (!simple_iv (loop, loop, poffset, &offset_iv, true))
    1224        82141 :         return opt_result::failure_at
    1225        82141 :           (stmt, "failed: evolution of offset is not affine.\n");
    1226              :     }
    1227              : 
    1228     16366852 :   init = ssize_int (pbytepos);
    1229              : 
    1230              :   /* Subtract any constant component from the base and add it to INIT instead.
    1231              :      Adjust the misalignment to reflect the amount we subtracted.  */
    1232     16366852 :   split_constant_offset (base_iv.base, &base_iv.base, &dinit);
    1233     16366852 :   init = size_binop (PLUS_EXPR, init, dinit);
    1234     16366852 :   base_misalignment -= TREE_INT_CST_LOW (dinit);
    1235              : 
    1236     16366852 :   split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
    1237     16366852 :   init = size_binop (PLUS_EXPR, init, dinit);
    1238              : 
    1239     16366852 :   step = size_binop (PLUS_EXPR,
    1240              :                      fold_convert (ssizetype, base_iv.step),
    1241              :                      fold_convert (ssizetype, offset_iv.step));
    1242              : 
    1243     16366852 :   base = canonicalize_base_object_address (base_iv.base);
    1244              : 
    1245              :   /* See if get_pointer_alignment can guarantee a higher alignment than
    1246              :      the one we calculated above.  */
    1247     16366852 :   unsigned int HOST_WIDE_INT alt_misalignment;
    1248     16366852 :   unsigned int alt_alignment;
    1249     16366852 :   get_pointer_alignment_1 (base, &alt_alignment, &alt_misalignment);
    1250              : 
    1251              :   /* As above, these values must be whole bytes.  */
    1252     16366852 :   gcc_assert (alt_alignment % BITS_PER_UNIT == 0
    1253              :               && alt_misalignment % BITS_PER_UNIT == 0);
    1254     16366852 :   alt_alignment /= BITS_PER_UNIT;
    1255     16366852 :   alt_misalignment /= BITS_PER_UNIT;
    1256              : 
    1257     16366852 :   if (base_alignment < alt_alignment)
    1258              :     {
    1259       148960 :       base_alignment = alt_alignment;
    1260       148960 :       base_misalignment = alt_misalignment;
    1261              :     }
    1262              : 
    1263     16366852 :   drb->base_address = base;
    1264     16366852 :   drb->offset = fold_convert (ssizetype, offset_iv.base);
    1265     16366852 :   drb->init = init;
    1266     16366852 :   drb->step = step;
    1267     16366852 :   if (known_misalignment (base_misalignment, base_alignment,
    1268              :                           &drb->base_misalignment))
    1269     16366852 :     drb->base_alignment = base_alignment;
    1270              :   else
    1271              :     {
    1272              :       drb->base_alignment = known_alignment (base_misalignment);
    1273              :       drb->base_misalignment = 0;
    1274              :     }
    1275     16366852 :   drb->offset_alignment = highest_pow2_factor (offset_iv.base);
    1276     16366852 :   drb->step_alignment = highest_pow2_factor (step);
    1277              : 
    1278     16366852 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1279        64753 :     fprintf (dump_file, "success.\n");
    1280              : 
    1281     16366852 :   return opt_result::success ();
    1282              : }
    1283              : 
    1284              : /* Return true if OP is a valid component reference for a DR access
    1285              :    function.  This accepts a subset of what handled_component_p accepts.  */
    1286              : 
    1287              : static bool
    1288      5762514 : access_fn_component_p (tree op)
    1289              : {
    1290      5762514 :   switch (TREE_CODE (op))
    1291              :     {
    1292              :     case REALPART_EXPR:
    1293              :     case IMAGPART_EXPR:
    1294              :     case ARRAY_REF:
    1295              :       return true;
    1296              : 
    1297      1982153 :     case COMPONENT_REF:
    1298      1982153 :       return (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == RECORD_TYPE
    1299      1982153 :               || (!AGGREGATE_TYPE_P (TREE_TYPE (op))
    1300         1559 :                   && TREE_CODE (TREE_TYPE (op)) != COMPLEX_TYPE));
    1301              : 
    1302              :     default:
    1303              :       return false;
    1304              :     }
    1305              : }
    1306              : 
    1307              : /* Returns whether BASE can have a access_fn_component_p with BASE
    1308              :    as base.  */
    1309              : 
    1310              : static bool
    1311      1720614 : base_supports_access_fn_components_p (tree base)
    1312              : {
    1313      1720614 :   switch (TREE_CODE (TREE_TYPE (base)))
    1314              :     {
    1315              :     case COMPLEX_TYPE:
    1316              :     case ARRAY_TYPE:
    1317              :     case RECORD_TYPE:
    1318              :       return true;
    1319      1713623 :     default:
    1320      1713623 :       return false;
    1321              :     }
    1322              : }
    1323              : 
    1324              : /* Determines the base object and the list of indices of memory reference
    1325              :    DR, analyzed in LOOP and instantiated before NEST.  */
    1326              : 
    1327              : static void
    1328     17027832 : dr_analyze_indices (struct indices *dri, tree ref, edge nest, loop_p loop)
    1329              : {
    1330              :   /* If analyzing a basic-block there are no indices to analyze
    1331              :      and thus no access functions.  */
    1332     17027832 :   if (!nest)
    1333              :     {
    1334     13702569 :       dri->base_object = ref;
    1335     13702569 :       dri->access_fns.create (0);
    1336     13702569 :       return;
    1337              :     }
    1338              : 
    1339      3325263 :   vec<tree> access_fns = vNULL;
    1340              : 
    1341              :   /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
    1342              :      into a two element array with a constant index.  The base is
    1343              :      then just the immediate underlying object.  */
    1344      3325263 :   if (TREE_CODE (ref) == REALPART_EXPR)
    1345              :     {
    1346        41791 :       ref = TREE_OPERAND (ref, 0);
    1347        41791 :       access_fns.safe_push (integer_zero_node);
    1348              :     }
    1349      3283472 :   else if (TREE_CODE (ref) == IMAGPART_EXPR)
    1350              :     {
    1351        39926 :       ref = TREE_OPERAND (ref, 0);
    1352        39926 :       access_fns.safe_push (integer_one_node);
    1353              :     }
    1354              : 
    1355              :   /* Analyze access functions of dimensions we know to be independent.
    1356              :      The list of component references handled here should be kept in
    1357              :      sync with access_fn_component_p.  */
    1358      5898983 :   while (handled_component_p (ref))
    1359              :     {
    1360      2715273 :       if (TREE_CODE (ref) == ARRAY_REF)
    1361              :         {
    1362      1311318 :           tree op = TREE_OPERAND (ref, 1);
    1363      1311318 :           tree access_fn = analyze_scalar_evolution (loop, op);
    1364      1311318 :           access_fn = instantiate_scev (nest, loop, access_fn);
    1365      1311318 :           access_fns.safe_push (access_fn);
    1366              :         }
    1367      1403955 :       else if (TREE_CODE (ref) == COMPONENT_REF
    1368      1403955 :                && (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE
    1369        99961 :                    || (!AGGREGATE_TYPE_P (TREE_TYPE (ref))
    1370        16024 :                        && TREE_CODE (TREE_TYPE (ref)) != COMPLEX_TYPE)))
    1371              :         {
    1372              :           /* For COMPONENT_REFs of records (but not unions!) use the
    1373              :              FIELD_DECL offset as constant access function so we can
    1374              :              disambiguate a[i].f1 and a[i].f2.  For unions and accesses
    1375              :              we do not create further access functions for just use
    1376              :              zero.  */
    1377      1262402 :           tree off;
    1378      1262402 :           if (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
    1379              :             {
    1380      1246378 :               off = component_ref_field_offset (ref);
    1381      1246378 :               off = size_binop (PLUS_EXPR,
    1382              :                                 size_binop (MULT_EXPR,
    1383              :                                             fold_convert (bitsizetype, off),
    1384              :                                             bitsize_int (BITS_PER_UNIT)),
    1385              :                                 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
    1386              :             }
    1387              :           else
    1388        16024 :             off = bitsize_zero_node;
    1389      1262402 :           access_fns.safe_push (off);
    1390              :         }
    1391              :       else
    1392              :         /* If we have an unhandled component we could not translate
    1393              :            to an access function stop analyzing.  We have determined
    1394              :            our base object in this case.  */
    1395              :         break;
    1396              : 
    1397      2573720 :       ref = TREE_OPERAND (ref, 0);
    1398              :     }
    1399              : 
    1400              :   /* If the address operand of a MEM_REF base has an evolution in the
    1401              :      analyzed nest, add it as an additional independent access-function.  */
    1402      3325263 :   if (TREE_CODE (ref) == MEM_REF)
    1403              :     {
    1404      2352526 :       tree op = TREE_OPERAND (ref, 0);
    1405      2352526 :       tree access_fn = analyze_scalar_evolution (loop, op);
    1406      2352526 :       access_fn = instantiate_scev (nest, loop, access_fn);
    1407      2352526 :       STRIP_NOPS (access_fn);
    1408      2352526 :       if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
    1409              :         {
    1410      1164743 :           tree memoff = TREE_OPERAND (ref, 1);
    1411      1164743 :           tree base = initial_condition (access_fn);
    1412      1164743 :           tree orig_type = TREE_TYPE (base);
    1413      1164743 :           STRIP_USELESS_TYPE_CONVERSION (base);
    1414      1164743 :           tree off;
    1415      1164743 :           split_constant_offset (base, &base, &off);
    1416      1164743 :           STRIP_USELESS_TYPE_CONVERSION (base);
    1417              :           /* Fold the MEM_REF offset into the evolutions initial
    1418              :              value to make more bases comparable.  */
    1419      1164743 :           if (!integer_zerop (memoff))
    1420              :             {
    1421       126438 :               off = size_binop (PLUS_EXPR, off,
    1422              :                                 fold_convert (ssizetype, memoff));
    1423       126438 :               memoff = build_int_cst (TREE_TYPE (memoff), 0);
    1424              :             }
    1425              :           /* Adjust the offset so it is a multiple of the access type
    1426              :              size and thus we separate bases that can possibly be used
    1427              :              to produce partial overlaps (which the access_fn machinery
    1428              :              cannot handle).  */
    1429      1164743 :           wide_int rem;
    1430      1164743 :           if (TYPE_SIZE_UNIT (TREE_TYPE (ref))
    1431      1164607 :               && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref))) == INTEGER_CST
    1432      2329033 :               && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (ref))))
    1433      1164290 :             rem = wi::mod_trunc
    1434      1164290 :               (wi::to_wide (off),
    1435      2328580 :                wi::to_wide (TYPE_SIZE_UNIT (TREE_TYPE (ref))),
    1436      1164290 :                SIGNED);
    1437              :           else
    1438              :             /* If we can't compute the remainder simply force the initial
    1439              :                condition to zero.  */
    1440          453 :             rem = wi::to_wide (off);
    1441      1164743 :           off = wide_int_to_tree (ssizetype, wi::to_wide (off) - rem);
    1442      1164743 :           memoff = wide_int_to_tree (TREE_TYPE (memoff), rem);
    1443              :           /* And finally replace the initial condition.  */
    1444      2329486 :           access_fn = chrec_replace_initial_condition
    1445      1164743 :               (access_fn, fold_convert (orig_type, off));
    1446              :           /* ???  This is still not a suitable base object for
    1447              :              dr_may_alias_p - the base object needs to be an
    1448              :              access that covers the object as whole.  With
    1449              :              an evolution in the pointer this cannot be
    1450              :              guaranteed.
    1451              :              As a band-aid, mark the access so we can special-case
    1452              :              it in dr_may_alias_p.  */
    1453      1164743 :           tree old = ref;
    1454      1164743 :           ref = fold_build2_loc (EXPR_LOCATION (ref),
    1455      1164743 :                                  MEM_REF, TREE_TYPE (ref),
    1456              :                                  base, memoff);
    1457      1164743 :           MR_DEPENDENCE_CLIQUE (ref) = MR_DEPENDENCE_CLIQUE (old);
    1458      1164743 :           MR_DEPENDENCE_BASE (ref) = MR_DEPENDENCE_BASE (old);
    1459      1164743 :           dri->unconstrained_base = true;
    1460      1164743 :           access_fns.safe_push (access_fn);
    1461      1164743 :         }
    1462              :     }
    1463       972737 :   else if (DECL_P (ref))
    1464              :     {
    1465              :       /* Canonicalize DR_BASE_OBJECT to MEM_REF form.  */
    1466       831184 :       ref = build2 (MEM_REF, TREE_TYPE (ref),
    1467              :                     build_fold_addr_expr (ref),
    1468              :                     build_int_cst (reference_alias_ptr_type (ref), 0));
    1469              :     }
    1470              : 
    1471      3325263 :   dri->base_object = ref;
    1472      3325263 :   dri->access_fns = access_fns;
    1473              : }
    1474              : 
    1475              : /* Extracts the alias analysis information from the memory reference DR.  */
    1476              : 
    1477              : static void
    1478     16912573 : dr_analyze_alias (struct data_reference *dr)
    1479              : {
    1480     16912573 :   tree ref = DR_REF (dr);
    1481     16912573 :   tree base = get_base_address (ref), addr;
    1482              : 
    1483     16912573 :   if (INDIRECT_REF_P (base)
    1484     16912573 :       || TREE_CODE (base) == MEM_REF)
    1485              :     {
    1486      7303197 :       addr = TREE_OPERAND (base, 0);
    1487      7303197 :       if (TREE_CODE (addr) == SSA_NAME)
    1488      7301819 :         DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
    1489              :     }
    1490     16912573 : }
    1491              : 
    1492              : /* Frees data reference DR.  */
    1493              : 
    1494              : void
    1495     17406166 : free_data_ref (data_reference_p dr)
    1496              : {
    1497     17406166 :   DR_ACCESS_FNS (dr).release ();
    1498     17406166 :   if (dr->alt_indices.base_object)
    1499       115259 :     dr->alt_indices.access_fns.release ();
    1500     17406166 :   free (dr);
    1501     17406166 : }
    1502              : 
    1503              : /* Analyze memory reference MEMREF, which is accessed in STMT.
    1504              :    The reference is a read if IS_READ is true, otherwise it is a write.
    1505              :    IS_CONDITIONAL_IN_STMT indicates that the reference is conditional
    1506              :    within STMT, i.e. that it might not occur even if STMT is executed
    1507              :    and runs to completion.
    1508              : 
    1509              :    Return the data_reference description of MEMREF.  NEST is the outermost
    1510              :    loop in which the reference should be instantiated, LOOP is the loop
    1511              :    in which the data reference should be analyzed.  */
    1512              : 
    1513              : struct data_reference *
    1514     16912573 : create_data_ref (edge nest, loop_p loop, tree memref, gimple *stmt,
    1515              :                  bool is_read, bool is_conditional_in_stmt)
    1516              : {
    1517     16912573 :   struct data_reference *dr;
    1518              : 
    1519     16912573 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1520              :     {
    1521        66996 :       fprintf (dump_file, "Creating dr for ");
    1522        66996 :       print_generic_expr (dump_file, memref, TDF_SLIM);
    1523        66996 :       fprintf (dump_file, "\n");
    1524              :     }
    1525              : 
    1526     16912573 :   dr = XCNEW (struct data_reference);
    1527     16912573 :   DR_STMT (dr) = stmt;
    1528     16912573 :   DR_REF (dr) = memref;
    1529     16912573 :   DR_IS_READ (dr) = is_read;
    1530     16912573 :   DR_IS_CONDITIONAL_IN_STMT (dr) = is_conditional_in_stmt;
    1531              : 
    1532     30615142 :   dr_analyze_innermost (&DR_INNERMOST (dr), memref,
    1533              :                         nest != NULL ? loop : NULL, stmt);
    1534     16912573 :   dr_analyze_indices (&dr->indices, DR_REF (dr), nest, loop);
    1535     16912573 :   dr_analyze_alias (dr);
    1536              : 
    1537     16912573 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1538              :     {
    1539        66996 :       unsigned i;
    1540        66996 :       fprintf (dump_file, "\tbase_address: ");
    1541        66996 :       print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
    1542        66996 :       fprintf (dump_file, "\n\toffset from base address: ");
    1543        66996 :       print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
    1544        66996 :       fprintf (dump_file, "\n\tconstant offset from base address: ");
    1545        66996 :       print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
    1546        66996 :       fprintf (dump_file, "\n\tstep: ");
    1547        66996 :       print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
    1548        66996 :       fprintf (dump_file, "\n\tbase alignment: %d", DR_BASE_ALIGNMENT (dr));
    1549        66996 :       fprintf (dump_file, "\n\tbase misalignment: %d",
    1550              :                DR_BASE_MISALIGNMENT (dr));
    1551        66996 :       fprintf (dump_file, "\n\toffset alignment: %d",
    1552              :                DR_OFFSET_ALIGNMENT (dr));
    1553        66996 :       fprintf (dump_file, "\n\tstep alignment: %d", DR_STEP_ALIGNMENT (dr));
    1554        66996 :       fprintf (dump_file, "\n\tbase_object: ");
    1555        66996 :       print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
    1556        66996 :       fprintf (dump_file, "\n");
    1557       192456 :       for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
    1558              :         {
    1559        58464 :           fprintf (dump_file, "\tAccess function %d: ", i);
    1560        58464 :           print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
    1561              :         }
    1562              :     }
    1563              : 
    1564     16912573 :   return dr;
    1565              : }
    1566              : 
    1567              : /*  A helper function computes order between two tree expressions T1 and T2.
    1568              :     This is used in comparator functions sorting objects based on the order
    1569              :     of tree expressions.  The function returns -1, 0, or 1.  */
    1570              : 
    1571              : int
    1572    426415950 : data_ref_compare_tree (tree t1, tree t2)
    1573              : {
    1574    426415950 :   int i, cmp;
    1575    426415950 :   enum tree_code code;
    1576    426415950 :   char tclass;
    1577              : 
    1578    426415950 :   if (t1 == t2)
    1579              :     return 0;
    1580    194542378 :   if (t1 == NULL)
    1581              :     return -1;
    1582    194414094 :   if (t2 == NULL)
    1583              :     return 1;
    1584              : 
    1585    194336304 :   STRIP_USELESS_TYPE_CONVERSION (t1);
    1586    194336304 :   STRIP_USELESS_TYPE_CONVERSION (t2);
    1587    194336304 :   if (t1 == t2)
    1588              :     return 0;
    1589              : 
    1590    193785744 :   if (TREE_CODE (t1) != TREE_CODE (t2)
    1591     13948492 :       && ! (CONVERT_EXPR_P (t1) && CONVERT_EXPR_P (t2)))
    1592     19755187 :     return TREE_CODE (t1) < TREE_CODE (t2) ? -1 : 1;
    1593              : 
    1594    179837252 :   code = TREE_CODE (t1);
    1595    179837252 :   switch (code)
    1596              :     {
    1597     52664262 :     case INTEGER_CST:
    1598     52664262 :       return tree_int_cst_compare (t1, t2);
    1599              : 
    1600           16 :     case STRING_CST:
    1601           16 :       if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2))
    1602           16 :         return TREE_STRING_LENGTH (t1) < TREE_STRING_LENGTH (t2) ? -1 : 1;
    1603            0 :       return memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
    1604            0 :                      TREE_STRING_LENGTH (t1));
    1605              : 
    1606     15181965 :     case SSA_NAME:
    1607     15181965 :       if (SSA_NAME_VERSION (t1) != SSA_NAME_VERSION (t2))
    1608     15181965 :         return SSA_NAME_VERSION (t1) < SSA_NAME_VERSION (t2) ? -1 : 1;
    1609              :       break;
    1610              : 
    1611    111991009 :     default:
    1612    111991009 :       if (POLY_INT_CST_P (t1))
    1613              :         return compare_sizes_for_sort (wi::to_poly_widest (t1),
    1614              :                                        wi::to_poly_widest (t2));
    1615              : 
    1616    111991009 :       tclass = TREE_CODE_CLASS (code);
    1617              : 
    1618              :       /* For decls, compare their UIDs.  */
    1619    111991009 :       if (tclass == tcc_declaration)
    1620              :         {
    1621     21255800 :           if (DECL_UID (t1) != DECL_UID (t2))
    1622     21255273 :             return DECL_UID (t1) < DECL_UID (t2) ? -1 : 1;
    1623              :           break;
    1624              :         }
    1625              :       /* For expressions, compare their operands recursively.  */
    1626     90735209 :       else if (IS_EXPR_CODE_CLASS (tclass))
    1627              :         {
    1628    161803542 :           for (i = TREE_OPERAND_LENGTH (t1) - 1; i >= 0; --i)
    1629              :             {
    1630    104952570 :               cmp = data_ref_compare_tree (TREE_OPERAND (t1, i),
    1631    104952570 :                                            TREE_OPERAND (t2, i));
    1632    104952570 :               if (cmp != 0)
    1633              :                 return cmp;
    1634              :             }
    1635              :         }
    1636              :       else
    1637            0 :         gcc_unreachable ();
    1638              :     }
    1639              : 
    1640              :   return 0;
    1641              : }
    1642              : 
    1643              : /* Return TRUE it's possible to resolve data dependence DDR by runtime alias
    1644              :    check.  */
    1645              : 
    1646              : opt_result
    1647       229424 : runtime_alias_check_p (ddr_p ddr, class loop *loop, bool speed_p)
    1648              : {
    1649       229424 :   if (dump_enabled_p ())
    1650         7831 :     dump_printf (MSG_NOTE,
    1651              :                  "consider run-time aliasing test between %T and %T\n",
    1652         7831 :                  DR_REF (DDR_A (ddr)), DR_REF (DDR_B (ddr)));
    1653              : 
    1654       229424 :   if (!speed_p)
    1655            0 :     return opt_result::failure_at (DR_STMT (DDR_A (ddr)),
    1656              :                                    "runtime alias check not supported when"
    1657              :                                    " optimizing for size.\n");
    1658              : 
    1659              :   /* FORNOW: We don't support versioning with outer-loop in either
    1660              :      vectorization or loop distribution.  */
    1661       229424 :   if (loop != NULL && loop->inner != NULL)
    1662          143 :     return opt_result::failure_at (DR_STMT (DDR_A (ddr)),
    1663              :                                    "runtime alias check not supported for"
    1664              :                                    " outer loop.\n");
    1665              : 
    1666              :   /* FORNOW: We don't support handling different address spaces.  */
    1667       229281 :   if (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (DR_BASE_ADDRESS (DDR_A (ddr)))))
    1668       229281 :       != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (DR_BASE_ADDRESS (DDR_B (ddr))))))
    1669            1 :     return opt_result::failure_at (DR_STMT (DDR_A (ddr)),
    1670              :                                    "runtime alias check between different "
    1671              :                                    "address spaces not supported.\n");
    1672              : 
    1673       229280 :   return opt_result::success ();
    1674              : }
    1675              : 
    1676              : /* Operator == between two dr_with_seg_len objects.
    1677              : 
    1678              :    This equality operator is used to make sure two data refs
    1679              :    are the same one so that we will consider to combine the
    1680              :    aliasing checks of those two pairs of data dependent data
    1681              :    refs.  */
    1682              : 
    1683              : static bool
    1684       142187 : operator == (const dr_with_seg_len& d1,
    1685              :              const dr_with_seg_len& d2)
    1686              : {
    1687       142187 :   return (operand_equal_p (DR_BASE_ADDRESS (d1.dr),
    1688       142187 :                            DR_BASE_ADDRESS (d2.dr), 0)
    1689       108138 :           && data_ref_compare_tree (DR_OFFSET (d1.dr), DR_OFFSET (d2.dr)) == 0
    1690       107238 :           && data_ref_compare_tree (DR_INIT (d1.dr), DR_INIT (d2.dr)) == 0
    1691        98242 :           && data_ref_compare_tree (d1.seg_len, d2.seg_len) == 0
    1692        97470 :           && known_eq (d1.access_size, d2.access_size)
    1693       236452 :           && d1.align == d2.align);
    1694              : }
    1695              : 
    1696              : /* Comparison function for sorting objects of dr_with_seg_len_pair_t
    1697              :    so that we can combine aliasing checks in one scan.  */
    1698              : 
    1699              : static int
    1700      1161052 : comp_dr_with_seg_len_pair (const void *pa_, const void *pb_)
    1701              : {
    1702      1161052 :   const dr_with_seg_len_pair_t* pa = (const dr_with_seg_len_pair_t *) pa_;
    1703      1161052 :   const dr_with_seg_len_pair_t* pb = (const dr_with_seg_len_pair_t *) pb_;
    1704      1161052 :   const dr_with_seg_len &a1 = pa->first, &a2 = pa->second;
    1705      1161052 :   const dr_with_seg_len &b1 = pb->first, &b2 = pb->second;
    1706              : 
    1707              :   /* For DR pairs (a, b) and (c, d), we only consider to merge the alias checks
    1708              :      if a and c have the same basic address snd step, and b and d have the same
    1709              :      address and step.  Therefore, if any a&c or b&d don't have the same address
    1710              :      and step, we don't care the order of those two pairs after sorting.  */
    1711      1161052 :   int comp_res;
    1712              : 
    1713      1161052 :   if ((comp_res = data_ref_compare_tree (DR_BASE_ADDRESS (a1.dr),
    1714      1161052 :                                          DR_BASE_ADDRESS (b1.dr))) != 0)
    1715              :     return comp_res;
    1716       602240 :   if ((comp_res = data_ref_compare_tree (DR_BASE_ADDRESS (a2.dr),
    1717       602240 :                                          DR_BASE_ADDRESS (b2.dr))) != 0)
    1718              :     return comp_res;
    1719       408488 :   if ((comp_res = data_ref_compare_tree (DR_STEP (a1.dr),
    1720       408488 :                                          DR_STEP (b1.dr))) != 0)
    1721              :     return comp_res;
    1722       407868 :   if ((comp_res = data_ref_compare_tree (DR_STEP (a2.dr),
    1723       407868 :                                          DR_STEP (b2.dr))) != 0)
    1724              :     return comp_res;
    1725       400305 :   if ((comp_res = data_ref_compare_tree (DR_OFFSET (a1.dr),
    1726       400305 :                                          DR_OFFSET (b1.dr))) != 0)
    1727              :     return comp_res;
    1728       384226 :   if ((comp_res = data_ref_compare_tree (DR_INIT (a1.dr),
    1729       384226 :                                          DR_INIT (b1.dr))) != 0)
    1730              :     return comp_res;
    1731       283862 :   if ((comp_res = data_ref_compare_tree (DR_OFFSET (a2.dr),
    1732       283862 :                                          DR_OFFSET (b2.dr))) != 0)
    1733              :     return comp_res;
    1734       268559 :   if ((comp_res = data_ref_compare_tree (DR_INIT (a2.dr),
    1735       268559 :                                          DR_INIT (b2.dr))) != 0)
    1736              :     return comp_res;
    1737              : 
    1738              :   return 0;
    1739              : }
    1740              : 
    1741              : /* Dump information about ALIAS_PAIR, indenting each line by INDENT.  */
    1742              : 
    1743              : static void
    1744         1008 : dump_alias_pair (dr_with_seg_len_pair_t *alias_pair, const char *indent)
    1745              : {
    1746         2016 :   dump_printf (MSG_NOTE, "%sreference:      %T vs. %T\n", indent,
    1747         1008 :                DR_REF (alias_pair->first.dr),
    1748         1008 :                DR_REF (alias_pair->second.dr));
    1749              : 
    1750         1008 :   dump_printf (MSG_NOTE, "%ssegment length: %T", indent,
    1751              :                alias_pair->first.seg_len);
    1752         1008 :   if (!operand_equal_p (alias_pair->first.seg_len,
    1753         1008 :                         alias_pair->second.seg_len, 0))
    1754          251 :     dump_printf (MSG_NOTE, " vs. %T", alias_pair->second.seg_len);
    1755              : 
    1756         1008 :   dump_printf (MSG_NOTE, "\n%saccess size:    ", indent);
    1757         1008 :   dump_dec (MSG_NOTE, alias_pair->first.access_size);
    1758         1008 :   if (maybe_ne (alias_pair->first.access_size, alias_pair->second.access_size))
    1759              :     {
    1760          231 :       dump_printf (MSG_NOTE, " vs. ");
    1761          231 :       dump_dec (MSG_NOTE, alias_pair->second.access_size);
    1762              :     }
    1763              : 
    1764         1008 :   dump_printf (MSG_NOTE, "\n%salignment:      %d", indent,
    1765              :                alias_pair->first.align);
    1766         1008 :   if (alias_pair->first.align != alias_pair->second.align)
    1767           73 :     dump_printf (MSG_NOTE, " vs. %d", alias_pair->second.align);
    1768              : 
    1769         1008 :   dump_printf (MSG_NOTE, "\n%sflags:         ", indent);
    1770         1008 :   if (alias_pair->flags & DR_ALIAS_RAW)
    1771          153 :     dump_printf (MSG_NOTE, " RAW");
    1772         1008 :   if (alias_pair->flags & DR_ALIAS_WAR)
    1773          799 :     dump_printf (MSG_NOTE, " WAR");
    1774         1008 :   if (alias_pair->flags & DR_ALIAS_WAW)
    1775          174 :     dump_printf (MSG_NOTE, " WAW");
    1776         1008 :   if (alias_pair->flags & DR_ALIAS_ARBITRARY)
    1777          213 :     dump_printf (MSG_NOTE, " ARBITRARY");
    1778         1008 :   if (alias_pair->flags & DR_ALIAS_SWAPPED)
    1779            0 :     dump_printf (MSG_NOTE, " SWAPPED");
    1780         1008 :   if (alias_pair->flags & DR_ALIAS_UNSWAPPED)
    1781            0 :     dump_printf (MSG_NOTE, " UNSWAPPED");
    1782         1008 :   if (alias_pair->flags & DR_ALIAS_MIXED_STEPS)
    1783            0 :     dump_printf (MSG_NOTE, " MIXED_STEPS");
    1784         1008 :   if (alias_pair->flags == 0)
    1785            0 :     dump_printf (MSG_NOTE, " <none>");
    1786         1008 :   dump_printf (MSG_NOTE, "\n");
    1787         1008 : }
    1788              : 
    1789              : /* Merge alias checks recorded in ALIAS_PAIRS and remove redundant ones.
    1790              :    FACTOR is number of iterations that each data reference is accessed.
    1791              : 
    1792              :    Basically, for each pair of dependent data refs store_ptr_0 & load_ptr_0,
    1793              :    we create an expression:
    1794              : 
    1795              :    ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
    1796              :    || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
    1797              : 
    1798              :    for aliasing checks.  However, in some cases we can decrease the number
    1799              :    of checks by combining two checks into one.  For example, suppose we have
    1800              :    another pair of data refs store_ptr_0 & load_ptr_1, and if the following
    1801              :    condition is satisfied:
    1802              : 
    1803              :    load_ptr_0 < load_ptr_1  &&
    1804              :    load_ptr_1 - load_ptr_0 - load_segment_length_0 < store_segment_length_0
    1805              : 
    1806              :    (this condition means, in each iteration of vectorized loop, the accessed
    1807              :    memory of store_ptr_0 cannot be between the memory of load_ptr_0 and
    1808              :    load_ptr_1.)
    1809              : 
    1810              :    we then can use only the following expression to finish the aliasing checks
    1811              :    between store_ptr_0 & load_ptr_0 and store_ptr_0 & load_ptr_1:
    1812              : 
    1813              :    ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
    1814              :    || (load_ptr_1 + load_segment_length_1 <= store_ptr_0))
    1815              : 
    1816              :    Note that we only consider that load_ptr_0 and load_ptr_1 have the same
    1817              :    basic address.  */
    1818              : 
    1819              : void
    1820        23391 : prune_runtime_alias_test_list (vec<dr_with_seg_len_pair_t> *alias_pairs,
    1821              :                                poly_uint64)
    1822              : {
    1823        23391 :   if (alias_pairs->is_empty ())
    1824        23391 :     return;
    1825              : 
    1826              :   /* Canonicalize each pair so that the base components are ordered wrt
    1827              :      data_ref_compare_tree.  This allows the loop below to merge more
    1828              :      cases.  */
    1829              :   unsigned int i;
    1830              :   dr_with_seg_len_pair_t *alias_pair;
    1831        92587 :   FOR_EACH_VEC_ELT (*alias_pairs, i, alias_pair)
    1832              :     {
    1833        70069 :       data_reference_p dr_a = alias_pair->first.dr;
    1834        70069 :       data_reference_p dr_b = alias_pair->second.dr;
    1835        70069 :       int comp_res = data_ref_compare_tree (DR_BASE_ADDRESS (dr_a),
    1836              :                                             DR_BASE_ADDRESS (dr_b));
    1837        70069 :       if (comp_res == 0)
    1838         1828 :         comp_res = data_ref_compare_tree (DR_OFFSET (dr_a), DR_OFFSET (dr_b));
    1839         1828 :       if (comp_res == 0)
    1840          136 :         comp_res = data_ref_compare_tree (DR_INIT (dr_a), DR_INIT (dr_b));
    1841        70069 :       if (comp_res > 0)
    1842              :         {
    1843        24751 :           std::swap (alias_pair->first, alias_pair->second);
    1844        24751 :           alias_pair->flags |= DR_ALIAS_SWAPPED;
    1845              :         }
    1846              :       else
    1847        45318 :         alias_pair->flags |= DR_ALIAS_UNSWAPPED;
    1848              :     }
    1849              : 
    1850              :   /* Sort the collected data ref pairs so that we can scan them once to
    1851              :      combine all possible aliasing checks.  */
    1852        22518 :   alias_pairs->qsort (comp_dr_with_seg_len_pair);
    1853              : 
    1854              :   /* Scan the sorted dr pairs and check if we can combine alias checks
    1855              :      of two neighboring dr pairs.  */
    1856              :   unsigned int last = 0;
    1857        70069 :   for (i = 1; i < alias_pairs->length (); ++i)
    1858              :     {
    1859              :       /* Deal with two ddrs (dr_a1, dr_b1) and (dr_a2, dr_b2).  */
    1860        47551 :       dr_with_seg_len_pair_t *alias_pair1 = &(*alias_pairs)[last];
    1861        47551 :       dr_with_seg_len_pair_t *alias_pair2 = &(*alias_pairs)[i];
    1862              : 
    1863        47551 :       dr_with_seg_len *dr_a1 = &alias_pair1->first;
    1864        47551 :       dr_with_seg_len *dr_b1 = &alias_pair1->second;
    1865        47551 :       dr_with_seg_len *dr_a2 = &alias_pair2->first;
    1866        47551 :       dr_with_seg_len *dr_b2 = &alias_pair2->second;
    1867              : 
    1868              :       /* Remove duplicate data ref pairs.  */
    1869        47551 :       if (*dr_a1 == *dr_a2 && *dr_b1 == *dr_b2)
    1870              :         {
    1871        22015 :           if (dump_enabled_p ())
    1872         1667 :             dump_printf (MSG_NOTE, "found equal ranges %T, %T and %T, %T\n",
    1873         1667 :                          DR_REF (dr_a1->dr), DR_REF (dr_b1->dr),
    1874         1667 :                          DR_REF (dr_a2->dr), DR_REF (dr_b2->dr));
    1875        22015 :           alias_pair1->flags |= alias_pair2->flags;
    1876        69566 :           continue;
    1877              :         }
    1878              : 
    1879              :       /* Assume that we won't be able to merge the pairs, then correct
    1880              :          if we do.  */
    1881        25536 :       last += 1;
    1882        25536 :       if (last != i)
    1883         6718 :         (*alias_pairs)[last] = (*alias_pairs)[i];
    1884              : 
    1885        25536 :       if (*dr_a1 == *dr_a2 || *dr_b1 == *dr_b2)
    1886              :         {
    1887              :           /* We consider the case that DR_B1 and DR_B2 are same memrefs,
    1888              :              and DR_A1 and DR_A2 are two consecutive memrefs.  */
    1889        21549 :           if (*dr_a1 == *dr_a2)
    1890              :             {
    1891        14343 :               std::swap (dr_a1, dr_b1);
    1892        14343 :               std::swap (dr_a2, dr_b2);
    1893              :             }
    1894              : 
    1895        21549 :           poly_int64 init_a1, init_a2;
    1896              :           /* Only consider cases in which the distance between the initial
    1897              :              DR_A1 and the initial DR_A2 is known at compile time.  */
    1898        39184 :           if (!operand_equal_p (DR_BASE_ADDRESS (dr_a1->dr),
    1899        21549 :                                 DR_BASE_ADDRESS (dr_a2->dr), 0)
    1900         4411 :               || !operand_equal_p (DR_OFFSET (dr_a1->dr),
    1901         4411 :                                    DR_OFFSET (dr_a2->dr), 0)
    1902         3914 :               || !poly_int_tree_p (DR_INIT (dr_a1->dr), &init_a1)
    1903        25463 :               || !poly_int_tree_p (DR_INIT (dr_a2->dr), &init_a2))
    1904        17654 :             continue;
    1905              : 
    1906              :           /* Don't combine if we can't tell which one comes first.  */
    1907         3914 :           if (!ordered_p (init_a1, init_a2))
    1908              :             continue;
    1909              : 
    1910              :           /* Work out what the segment length would be if we did combine
    1911              :              DR_A1 and DR_A2:
    1912              : 
    1913              :              - If DR_A1 and DR_A2 have equal lengths, that length is
    1914              :                also the combined length.
    1915              : 
    1916              :              - If DR_A1 and DR_A2 both have negative "lengths", the combined
    1917              :                length is the lower bound on those lengths.
    1918              : 
    1919              :              - If DR_A1 and DR_A2 both have positive lengths, the combined
    1920              :                length is the upper bound on those lengths.
    1921              : 
    1922              :              Other cases are unlikely to give a useful combination.
    1923              : 
    1924              :              The lengths both have sizetype, so the sign is taken from
    1925              :              the step instead.  */
    1926         3914 :           poly_uint64 new_seg_len = 0;
    1927         3914 :           bool new_seg_len_p = !operand_equal_p (dr_a1->seg_len,
    1928         3914 :                                                  dr_a2->seg_len, 0);
    1929         3914 :           if (new_seg_len_p)
    1930              :             {
    1931           19 :               poly_uint64 seg_len_a1, seg_len_a2;
    1932           19 :               if (!poly_int_tree_p (dr_a1->seg_len, &seg_len_a1)
    1933           19 :                   || !poly_int_tree_p (dr_a2->seg_len, &seg_len_a2))
    1934           19 :                 continue;
    1935              : 
    1936            0 :               tree indicator_a = dr_direction_indicator (dr_a1->dr);
    1937            0 :               if (TREE_CODE (indicator_a) != INTEGER_CST)
    1938            0 :                 continue;
    1939              : 
    1940            0 :               tree indicator_b = dr_direction_indicator (dr_a2->dr);
    1941            0 :               if (TREE_CODE (indicator_b) != INTEGER_CST)
    1942            0 :                 continue;
    1943              : 
    1944            0 :               int sign_a = tree_int_cst_sgn (indicator_a);
    1945            0 :               int sign_b = tree_int_cst_sgn (indicator_b);
    1946              : 
    1947            0 :               if (sign_a <= 0 && sign_b <= 0)
    1948            0 :                 new_seg_len = lower_bound (seg_len_a1, seg_len_a2);
    1949            0 :               else if (sign_a >= 0 && sign_b >= 0)
    1950            0 :                 new_seg_len = upper_bound (seg_len_a1, seg_len_a2);
    1951              :               else
    1952            0 :                 continue;
    1953              :             }
    1954              :           /* At this point we're committed to merging the refs.  */
    1955              : 
    1956              :           /* Make sure dr_a1 starts left of dr_a2.  */
    1957         3895 :           if (maybe_gt (init_a1, init_a2))
    1958              :             {
    1959            0 :               std::swap (*dr_a1, *dr_a2);
    1960            0 :               std::swap (init_a1, init_a2);
    1961              :             }
    1962              : 
    1963              :           /* The DR_Bs are equal, so only the DR_As can introduce
    1964              :              mixed steps.  */
    1965         3895 :           if (!operand_equal_p (DR_STEP (dr_a1->dr), DR_STEP (dr_a2->dr), 0))
    1966            0 :             alias_pair1->flags |= DR_ALIAS_MIXED_STEPS;
    1967              : 
    1968         3895 :           if (new_seg_len_p)
    1969              :             {
    1970            0 :               dr_a1->seg_len = build_int_cst (TREE_TYPE (dr_a1->seg_len),
    1971            0 :                                               new_seg_len);
    1972            0 :               dr_a1->align = MIN (dr_a1->align, known_alignment (new_seg_len));
    1973              :             }
    1974              : 
    1975              :           /* This is always positive due to the swap above.  */
    1976         3895 :           poly_uint64 diff = init_a2 - init_a1;
    1977              : 
    1978              :           /* The new check will start at DR_A1.  Make sure that its access
    1979              :              size encompasses the initial DR_A2.  */
    1980         3895 :           if (maybe_lt (dr_a1->access_size, diff + dr_a2->access_size))
    1981              :             {
    1982         1385 :               dr_a1->access_size = upper_bound (dr_a1->access_size,
    1983              :                                                 diff + dr_a2->access_size);
    1984         1385 :               unsigned int new_align = known_alignment (dr_a1->access_size);
    1985         1385 :               dr_a1->align = MIN (dr_a1->align, new_align);
    1986              :             }
    1987         3895 :           if (dump_enabled_p ())
    1988         1020 :             dump_printf (MSG_NOTE, "merging ranges for %T, %T and %T, %T\n",
    1989         1020 :                          DR_REF (dr_a1->dr), DR_REF (dr_b1->dr),
    1990         1020 :                          DR_REF (dr_a2->dr), DR_REF (dr_b2->dr));
    1991         3895 :           alias_pair1->flags |= alias_pair2->flags;
    1992         3895 :           last -= 1;
    1993              :         }
    1994              :     }
    1995        22518 :   alias_pairs->truncate (last + 1);
    1996              : 
    1997              :   /* Try to restore the original dr_with_seg_len order within each
    1998              :      dr_with_seg_len_pair_t.  If we ended up combining swapped and
    1999              :      unswapped pairs into the same check, we have to invalidate any
    2000              :      RAW, WAR and WAW information for it.  */
    2001        22518 :   if (dump_enabled_p ())
    2002          805 :     dump_printf (MSG_NOTE, "merged alias checks:\n");
    2003        66677 :   FOR_EACH_VEC_ELT (*alias_pairs, i, alias_pair)
    2004              :     {
    2005        44159 :       unsigned int swap_mask = (DR_ALIAS_SWAPPED | DR_ALIAS_UNSWAPPED);
    2006        44159 :       unsigned int swapped = (alias_pair->flags & swap_mask);
    2007        44159 :       if (swapped == DR_ALIAS_SWAPPED)
    2008        13313 :         std::swap (alias_pair->first, alias_pair->second);
    2009        30846 :       else if (swapped != DR_ALIAS_UNSWAPPED)
    2010         3211 :         alias_pair->flags |= DR_ALIAS_ARBITRARY;
    2011        44159 :       alias_pair->flags &= ~swap_mask;
    2012        44159 :       if (dump_enabled_p ())
    2013         1008 :         dump_alias_pair (alias_pair, "  ");
    2014              :     }
    2015              : }
    2016              : 
    2017              : /* A subroutine of create_intersect_range_checks, with a subset of the
    2018              :    same arguments.  Try to use IFN_CHECK_RAW_PTRS and IFN_CHECK_WAR_PTRS
    2019              :    to optimize cases in which the references form a simple RAW, WAR or
    2020              :    WAR dependence.  */
    2021              : 
    2022              : static bool
    2023         4751 : create_ifn_alias_checks (tree *cond_expr,
    2024              :                          const dr_with_seg_len_pair_t &alias_pair)
    2025              : {
    2026         4751 :   const dr_with_seg_len& dr_a = alias_pair.first;
    2027         4751 :   const dr_with_seg_len& dr_b = alias_pair.second;
    2028              : 
    2029              :   /* Check for cases in which:
    2030              : 
    2031              :      (a) we have a known RAW, WAR or WAR dependence
    2032              :      (b) the accesses are well-ordered in both the original and new code
    2033              :          (see the comment above the DR_ALIAS_* flags for details); and
    2034              :      (c) the DR_STEPs describe all access pairs covered by ALIAS_PAIR.  */
    2035         4751 :   if (alias_pair.flags & ~(DR_ALIAS_RAW | DR_ALIAS_WAR | DR_ALIAS_WAW))
    2036              :     return false;
    2037              : 
    2038              :   /* Make sure that both DRs access the same pattern of bytes,
    2039              :      with a constant length and step.  */
    2040         3083 :   poly_uint64 seg_len;
    2041         3083 :   if (!operand_equal_p (dr_a.seg_len, dr_b.seg_len, 0)
    2042         2676 :       || !poly_int_tree_p (dr_a.seg_len, &seg_len)
    2043         2669 :       || maybe_ne (dr_a.access_size, dr_b.access_size)
    2044         2628 :       || !operand_equal_p (DR_STEP (dr_a.dr), DR_STEP (dr_b.dr), 0)
    2045         5711 :       || !tree_fits_uhwi_p (DR_STEP (dr_a.dr)))
    2046          470 :     return false;
    2047              : 
    2048         2613 :   unsigned HOST_WIDE_INT bytes = tree_to_uhwi (DR_STEP (dr_a.dr));
    2049         2613 :   tree addr_a = DR_BASE_ADDRESS (dr_a.dr);
    2050         2613 :   tree addr_b = DR_BASE_ADDRESS (dr_b.dr);
    2051              : 
    2052              :   /* See whether the target supports what we want to do.  WAW checks are
    2053              :      equivalent to WAR checks here.  */
    2054         2577 :   internal_fn ifn = (alias_pair.flags & DR_ALIAS_RAW
    2055         2613 :                      ? IFN_CHECK_RAW_PTRS
    2056              :                      : IFN_CHECK_WAR_PTRS);
    2057         2613 :   unsigned int align = MIN (dr_a.align, dr_b.align);
    2058         2613 :   poly_uint64 full_length = seg_len + bytes;
    2059         2613 :   if (!internal_check_ptrs_fn_supported_p (ifn, TREE_TYPE (addr_a),
    2060              :                                            full_length, align))
    2061              :     {
    2062         2613 :       full_length = seg_len + dr_a.access_size;
    2063         2613 :       if (!internal_check_ptrs_fn_supported_p (ifn, TREE_TYPE (addr_a),
    2064              :                                                full_length, align))
    2065              :         return false;
    2066              :     }
    2067              : 
    2068              :   /* Commit to using this form of test.  */
    2069            0 :   addr_a = fold_build_pointer_plus (addr_a, DR_OFFSET (dr_a.dr));
    2070            0 :   addr_a = fold_build_pointer_plus (addr_a, DR_INIT (dr_a.dr));
    2071              : 
    2072            0 :   addr_b = fold_build_pointer_plus (addr_b, DR_OFFSET (dr_b.dr));
    2073            0 :   addr_b = fold_build_pointer_plus (addr_b, DR_INIT (dr_b.dr));
    2074              : 
    2075            0 :   *cond_expr = build_call_expr_internal_loc (UNKNOWN_LOCATION,
    2076              :                                              ifn, boolean_type_node,
    2077              :                                              4, addr_a, addr_b,
    2078            0 :                                              size_int (full_length),
    2079            0 :                                              size_int (align));
    2080              : 
    2081            0 :   if (dump_enabled_p ())
    2082              :     {
    2083            0 :       if (ifn == IFN_CHECK_RAW_PTRS)
    2084            0 :         dump_printf (MSG_NOTE, "using an IFN_CHECK_RAW_PTRS test\n");
    2085              :       else
    2086            0 :         dump_printf (MSG_NOTE, "using an IFN_CHECK_WAR_PTRS test\n");
    2087              :     }
    2088              :   return true;
    2089              : }
    2090              : 
    2091              : /* Try to generate a runtime condition that is true if ALIAS_PAIR is
    2092              :    free of aliases, using a condition based on index values instead
    2093              :    of a condition based on addresses.  Return true on success,
    2094              :    storing the condition in *COND_EXPR.
    2095              : 
    2096              :    This can only be done if the two data references in ALIAS_PAIR access
    2097              :    the same array object and the index is the only difference.  For example,
    2098              :    if the two data references are DR_A and DR_B:
    2099              : 
    2100              :                        DR_A                           DR_B
    2101              :       data-ref         arr[i]                         arr[j]
    2102              :       base_object      arr                            arr
    2103              :       index            {i_0, +, 1}_loop               {j_0, +, 1}_loop
    2104              : 
    2105              :    The addresses and their index are like:
    2106              : 
    2107              :         |<- ADDR_A    ->|          |<- ADDR_B    ->|
    2108              :      ------------------------------------------------------->
    2109              :         |   |   |   |   |          |   |   |   |   |
    2110              :      ------------------------------------------------------->
    2111              :         i_0 ...         i_0+4      j_0 ...         j_0+4
    2112              : 
    2113              :    We can create expression based on index rather than address:
    2114              : 
    2115              :      (unsigned) (i_0 - j_0 + 3) <= 6
    2116              : 
    2117              :    i.e. the indices are less than 4 apart.
    2118              : 
    2119              :    Note evolution step of index needs to be considered in comparison.  */
    2120              : 
    2121              : static bool
    2122         4902 : create_intersect_range_checks_index (class loop *loop, tree *cond_expr,
    2123              :                                      const dr_with_seg_len_pair_t &alias_pair)
    2124              : {
    2125         4902 :   const dr_with_seg_len &dr_a = alias_pair.first;
    2126         4902 :   const dr_with_seg_len &dr_b = alias_pair.second;
    2127         4902 :   if ((alias_pair.flags & DR_ALIAS_MIXED_STEPS)
    2128         4902 :       || integer_zerop (DR_STEP (dr_a.dr))
    2129         4646 :       || integer_zerop (DR_STEP (dr_b.dr))
    2130        18714 :       || DR_NUM_DIMENSIONS (dr_a.dr) != DR_NUM_DIMENSIONS (dr_b.dr))
    2131          366 :     return false;
    2132              : 
    2133         4536 :   poly_uint64 seg_len1, seg_len2;
    2134         4536 :   if (!poly_int_tree_p (dr_a.seg_len, &seg_len1)
    2135         4536 :       || !poly_int_tree_p (dr_b.seg_len, &seg_len2))
    2136          275 :     return false;
    2137              : 
    2138         4261 :   if (!tree_fits_shwi_p (DR_STEP (dr_a.dr)))
    2139              :     return false;
    2140              : 
    2141         4261 :   if (!operand_equal_p (DR_BASE_OBJECT (dr_a.dr), DR_BASE_OBJECT (dr_b.dr), 0))
    2142              :     return false;
    2143              : 
    2144          154 :   if (!operand_equal_p (DR_STEP (dr_a.dr), DR_STEP (dr_b.dr), 0))
    2145              :     return false;
    2146              : 
    2147          152 :   gcc_assert (TREE_CODE (DR_STEP (dr_a.dr)) == INTEGER_CST);
    2148              : 
    2149          152 :   bool neg_step = tree_int_cst_compare (DR_STEP (dr_a.dr), size_zero_node) < 0;
    2150          152 :   unsigned HOST_WIDE_INT abs_step = tree_to_shwi (DR_STEP (dr_a.dr));
    2151          152 :   if (neg_step)
    2152              :     {
    2153           30 :       abs_step = -abs_step;
    2154           30 :       seg_len1 = (-wi::to_poly_wide (dr_a.seg_len)).force_uhwi ();
    2155           30 :       seg_len2 = (-wi::to_poly_wide (dr_b.seg_len)).force_uhwi ();
    2156              :     }
    2157              : 
    2158              :   /* Infer the number of iterations with which the memory segment is accessed
    2159              :      by DR.  In other words, alias is checked if memory segment accessed by
    2160              :      DR_A in some iterations intersect with memory segment accessed by DR_B
    2161              :      in the same amount iterations.
    2162              :      Note segnment length is a linear function of number of iterations with
    2163              :      DR_STEP as the coefficient.  */
    2164          152 :   poly_uint64 niter_len1, niter_len2;
    2165          152 :   if (!can_div_trunc_p (seg_len1 + abs_step - 1, abs_step, &niter_len1)
    2166          152 :       || !can_div_trunc_p (seg_len2 + abs_step - 1, abs_step, &niter_len2))
    2167              :     return false;
    2168              : 
    2169              :   /* Divide each access size by the byte step, rounding up.  */
    2170          152 :   poly_uint64 niter_access1, niter_access2;
    2171          152 :   if (!can_div_trunc_p (dr_a.access_size + abs_step - 1,
    2172              :                         abs_step, &niter_access1)
    2173          152 :       || !can_div_trunc_p (dr_b.access_size + abs_step - 1,
    2174              :                            abs_step, &niter_access2))
    2175              :     return false;
    2176              : 
    2177          152 :   bool waw_or_war_p = (alias_pair.flags & ~(DR_ALIAS_WAR | DR_ALIAS_WAW)) == 0;
    2178              : 
    2179          152 :   int found = -1;
    2180          311 :   for (unsigned int i = 0; i < DR_NUM_DIMENSIONS (dr_a.dr); i++)
    2181              :     {
    2182          160 :       tree access1 = DR_ACCESS_FN (dr_a.dr, i);
    2183          160 :       tree access2 = DR_ACCESS_FN (dr_b.dr, i);
    2184              :       /* Two indices must be the same if they are not scev, or not scev wrto
    2185              :          current loop being vecorized.  */
    2186          160 :       if (TREE_CODE (access1) != POLYNOMIAL_CHREC
    2187          152 :           || TREE_CODE (access2) != POLYNOMIAL_CHREC
    2188          152 :           || CHREC_VARIABLE (access1) != (unsigned)loop->num
    2189          312 :           || CHREC_VARIABLE (access2) != (unsigned)loop->num)
    2190              :         {
    2191            8 :           if (operand_equal_p (access1, access2, 0))
    2192            7 :             continue;
    2193              : 
    2194              :           return false;
    2195              :         }
    2196          152 :       if (found >= 0)
    2197              :         return false;
    2198          152 :       found = i;
    2199              :     }
    2200              : 
    2201              :   /* Ought not to happen in practice, since if all accesses are equal then the
    2202              :      alias should be decidable at compile time.  */
    2203          151 :   if (found < 0)
    2204              :     return false;
    2205              : 
    2206              :   /* The two indices must have the same step.  */
    2207          151 :   tree access1 = DR_ACCESS_FN (dr_a.dr, found);
    2208          151 :   tree access2 = DR_ACCESS_FN (dr_b.dr, found);
    2209          151 :   if (!operand_equal_p (CHREC_RIGHT (access1), CHREC_RIGHT (access2), 0))
    2210              :     return false;
    2211              : 
    2212          151 :   tree idx_step = CHREC_RIGHT (access1);
    2213              :   /* Index must have const step, otherwise DR_STEP won't be constant.  */
    2214          151 :   gcc_assert (TREE_CODE (idx_step) == INTEGER_CST);
    2215              :   /* Index must evaluate in the same direction as DR.  */
    2216          151 :   gcc_assert (!neg_step || tree_int_cst_sign_bit (idx_step) == 1);
    2217              : 
    2218          151 :   tree min1 = CHREC_LEFT (access1);
    2219          151 :   tree min2 = CHREC_LEFT (access2);
    2220          151 :   if (!types_compatible_p (TREE_TYPE (min1), TREE_TYPE (min2)))
    2221              :     return false;
    2222              : 
    2223              :   /* Ideally, alias can be checked against loop's control IV, but we
    2224              :      need to prove linear mapping between control IV and reference
    2225              :      index.  Although that should be true, we check against (array)
    2226              :      index of data reference.  Like segment length, index length is
    2227              :      linear function of the number of iterations with index_step as
    2228              :      the coefficient, i.e, niter_len * idx_step.  */
    2229          151 :   offset_int abs_idx_step = offset_int::from (wi::to_wide (idx_step),
    2230              :                                               SIGNED);
    2231          151 :   if (neg_step)
    2232           30 :     abs_idx_step = -abs_idx_step;
    2233          302 :   poly_offset_int idx_len1 = abs_idx_step * niter_len1;
    2234          302 :   poly_offset_int idx_len2 = abs_idx_step * niter_len2;
    2235          151 :   poly_offset_int idx_access1 = abs_idx_step * niter_access1;
    2236          151 :   poly_offset_int idx_access2 = abs_idx_step * niter_access2;
    2237              : 
    2238          151 :   gcc_assert (known_ge (idx_len1, 0)
    2239              :               && known_ge (idx_len2, 0)
    2240              :               && known_ge (idx_access1, 0)
    2241              :               && known_ge (idx_access2, 0));
    2242              : 
    2243              :   /* Each access has the following pattern, with lengths measured
    2244              :      in units of INDEX:
    2245              : 
    2246              :           <-- idx_len -->
    2247              :           <--- A: -ve step --->
    2248              :           +-----+-------+-----+-------+-----+
    2249              :           | n-1 | ..... |  0  | ..... | n-1 |
    2250              :           +-----+-------+-----+-------+-----+
    2251              :                         <--- B: +ve step --->
    2252              :                         <-- idx_len -->
    2253              :                         |
    2254              :                        min
    2255              : 
    2256              :      where "n" is the number of scalar iterations covered by the segment
    2257              :      and where each access spans idx_access units.
    2258              : 
    2259              :      A is the range of bytes accessed when the step is negative,
    2260              :      B is the range when the step is positive.
    2261              : 
    2262              :      When checking for general overlap, we need to test whether
    2263              :      the range:
    2264              : 
    2265              :        [min1 + low_offset1, min1 + high_offset1 + idx_access1 - 1]
    2266              : 
    2267              :      overlaps:
    2268              : 
    2269              :        [min2 + low_offset2, min2 + high_offset2 + idx_access2 - 1]
    2270              : 
    2271              :      where:
    2272              : 
    2273              :         low_offsetN = +ve step ? 0 : -idx_lenN;
    2274              :        high_offsetN = +ve step ? idx_lenN : 0;
    2275              : 
    2276              :      This is equivalent to testing whether:
    2277              : 
    2278              :        min1 + low_offset1 <= min2 + high_offset2 + idx_access2 - 1
    2279              :        && min2 + low_offset2 <= min1 + high_offset1 + idx_access1 - 1
    2280              : 
    2281              :      Converting this into a single test, there is an overlap if:
    2282              : 
    2283              :        0 <= min2 - min1 + bias <= limit
    2284              : 
    2285              :      where  bias = high_offset2 + idx_access2 - 1 - low_offset1
    2286              :            limit = (high_offset1 - low_offset1 + idx_access1 - 1)
    2287              :                  + (high_offset2 - low_offset2 + idx_access2 - 1)
    2288              :       i.e. limit = idx_len1 + idx_access1 - 1 + idx_len2 + idx_access2 - 1
    2289              : 
    2290              :      Combining the tests requires limit to be computable in an unsigned
    2291              :      form of the index type; if it isn't, we fall back to the usual
    2292              :      pointer-based checks.
    2293              : 
    2294              :      We can do better if DR_B is a write and if DR_A and DR_B are
    2295              :      well-ordered in both the original and the new code (see the
    2296              :      comment above the DR_ALIAS_* flags for details).  In this case
    2297              :      we know that for each i in [0, n-1], the write performed by
    2298              :      access i of DR_B occurs after access numbers j<=i of DR_A in
    2299              :      both the original and the new code.  Any write or anti
    2300              :      dependencies wrt those DR_A accesses are therefore maintained.
    2301              : 
    2302              :      We just need to make sure that each individual write in DR_B does not
    2303              :      overlap any higher-indexed access in DR_A; such DR_A accesses happen
    2304              :      after the DR_B access in the original code but happen before it in
    2305              :      the new code.
    2306              : 
    2307              :      We know the steps for both accesses are equal, so by induction, we
    2308              :      just need to test whether the first write of DR_B overlaps a later
    2309              :      access of DR_A.  In other words, we need to move min1 along by
    2310              :      one iteration:
    2311              : 
    2312              :        min1' = min1 + idx_step
    2313              : 
    2314              :      and use the ranges:
    2315              : 
    2316              :        [min1' + low_offset1', min1' + high_offset1' + idx_access1 - 1]
    2317              : 
    2318              :      and:
    2319              : 
    2320              :        [min2, min2 + idx_access2 - 1]
    2321              : 
    2322              :      where:
    2323              : 
    2324              :         low_offset1' = +ve step ? 0 : -(idx_len1 - |idx_step|)
    2325              :        high_offset1' = +ve_step ? idx_len1 - |idx_step| : 0.  */
    2326          151 :   if (waw_or_war_p)
    2327          120 :     idx_len1 -= abs_idx_step;
    2328              : 
    2329          151 :   poly_offset_int limit = idx_len1 + idx_access1 - 1 + idx_access2 - 1;
    2330          151 :   if (!waw_or_war_p)
    2331          151 :     limit += idx_len2;
    2332              : 
    2333          151 :   tree utype = unsigned_type_for (TREE_TYPE (min1));
    2334          151 :   if (!wi::fits_to_tree_p (limit, utype))
    2335              :     return false;
    2336              : 
    2337          151 :   poly_offset_int low_offset1 = neg_step ? -idx_len1 : 0;
    2338          151 :   poly_offset_int high_offset2 = neg_step || waw_or_war_p ? 0 : idx_len2;
    2339          151 :   poly_offset_int bias = high_offset2 + idx_access2 - 1 - low_offset1;
    2340              :   /* Equivalent to adding IDX_STEP to MIN1.  */
    2341          151 :   if (waw_or_war_p)
    2342          120 :     bias -= wi::to_offset (idx_step);
    2343              : 
    2344          151 :   tree subject = fold_build2 (MINUS_EXPR, utype,
    2345              :                               fold_convert (utype, min2),
    2346              :                               fold_convert (utype, min1));
    2347          151 :   subject = fold_build2 (PLUS_EXPR, utype, subject,
    2348              :                          wide_int_to_tree (utype, bias));
    2349          151 :   tree part_cond_expr = fold_build2 (GT_EXPR, boolean_type_node, subject,
    2350              :                                      wide_int_to_tree (utype, limit));
    2351          151 :   if (*cond_expr)
    2352            0 :     *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
    2353              :                               *cond_expr, part_cond_expr);
    2354              :   else
    2355          151 :     *cond_expr = part_cond_expr;
    2356          151 :   if (dump_enabled_p ())
    2357              :     {
    2358          133 :       if (waw_or_war_p)
    2359          103 :         dump_printf (MSG_NOTE, "using an index-based WAR/WAW test\n");
    2360              :       else
    2361           30 :         dump_printf (MSG_NOTE, "using an index-based overlap test\n");
    2362              :     }
    2363              :   return true;
    2364              : }
    2365              : 
    2366              : /* A subroutine of create_intersect_range_checks, with a subset of the
    2367              :    same arguments.  Try to optimize cases in which the second access
    2368              :    is a write and in which some overlap is valid.  */
    2369              : 
    2370              : static bool
    2371         4751 : create_waw_or_war_checks (tree *cond_expr,
    2372              :                           const dr_with_seg_len_pair_t &alias_pair)
    2373              : {
    2374         4751 :   const dr_with_seg_len& dr_a = alias_pair.first;
    2375         4751 :   const dr_with_seg_len& dr_b = alias_pair.second;
    2376              : 
    2377              :   /* Check for cases in which:
    2378              : 
    2379              :      (a) DR_B is always a write;
    2380              :      (b) the accesses are well-ordered in both the original and new code
    2381              :          (see the comment above the DR_ALIAS_* flags for details); and
    2382              :      (c) the DR_STEPs describe all access pairs covered by ALIAS_PAIR.  */
    2383         4751 :   if (alias_pair.flags & ~(DR_ALIAS_WAR | DR_ALIAS_WAW))
    2384              :     return false;
    2385              : 
    2386              :   /* Check for equal (but possibly variable) steps.  */
    2387         3040 :   tree step = DR_STEP (dr_a.dr);
    2388         3040 :   if (!operand_equal_p (step, DR_STEP (dr_b.dr)))
    2389              :     return false;
    2390              : 
    2391              :   /* Make sure that we can operate on sizetype without loss of precision.  */
    2392         2640 :   tree addr_type = TREE_TYPE (DR_BASE_ADDRESS (dr_a.dr));
    2393         2640 :   if (TYPE_PRECISION (addr_type) != TYPE_PRECISION (sizetype))
    2394              :     return false;
    2395              : 
    2396              :   /* All addresses involved are known to have a common alignment ALIGN.
    2397              :      We can therefore subtract ALIGN from an exclusive endpoint to get
    2398              :      an inclusive endpoint.  In the best (and common) case, ALIGN is the
    2399              :      same as the access sizes of both DRs, and so subtracting ALIGN
    2400              :      cancels out the addition of an access size.  */
    2401         2640 :   unsigned int align = MIN (dr_a.align, dr_b.align);
    2402         2640 :   poly_uint64 last_chunk_a = dr_a.access_size - align;
    2403         2640 :   poly_uint64 last_chunk_b = dr_b.access_size - align;
    2404              : 
    2405              :   /* Get a boolean expression that is true when the step is negative.  */
    2406         2640 :   tree indicator = dr_direction_indicator (dr_a.dr);
    2407         2640 :   tree neg_step = fold_build2 (LT_EXPR, boolean_type_node,
    2408              :                                fold_convert (ssizetype, indicator),
    2409              :                                ssize_int (0));
    2410              : 
    2411              :   /* Get lengths in sizetype.  */
    2412         2640 :   tree seg_len_a
    2413         2640 :     = fold_convert (sizetype, rewrite_to_non_trapping_overflow (dr_a.seg_len));
    2414         2640 :   step = fold_convert (sizetype, rewrite_to_non_trapping_overflow (step));
    2415              : 
    2416              :   /* Each access has the following pattern:
    2417              : 
    2418              :           <- |seg_len| ->
    2419              :           <--- A: -ve step --->
    2420              :           +-----+-------+-----+-------+-----+
    2421              :           | n-1 | ..... |  0  | ..... | n-1 |
    2422              :           +-----+-------+-----+-------+-----+
    2423              :                         <--- B: +ve step --->
    2424              :                         <- |seg_len| ->
    2425              :                         |
    2426              :                    base address
    2427              : 
    2428              :      where "n" is the number of scalar iterations covered by the segment.
    2429              : 
    2430              :      A is the range of bytes accessed when the step is negative,
    2431              :      B is the range when the step is positive.
    2432              : 
    2433              :      We know that DR_B is a write.  We also know (from checking that
    2434              :      DR_A and DR_B are well-ordered) that for each i in [0, n-1],
    2435              :      the write performed by access i of DR_B occurs after access numbers
    2436              :      j<=i of DR_A in both the original and the new code.  Any write or
    2437              :      anti dependencies wrt those DR_A accesses are therefore maintained.
    2438              : 
    2439              :      We just need to make sure that each individual write in DR_B does not
    2440              :      overlap any higher-indexed access in DR_A; such DR_A accesses happen
    2441              :      after the DR_B access in the original code but happen before it in
    2442              :      the new code.
    2443              : 
    2444              :      We know the steps for both accesses are equal, so by induction, we
    2445              :      just need to test whether the first write of DR_B overlaps a later
    2446              :      access of DR_A.  In other words, we need to move addr_a along by
    2447              :      one iteration:
    2448              : 
    2449              :        addr_a' = addr_a + step
    2450              : 
    2451              :      and check whether:
    2452              : 
    2453              :        [addr_b, addr_b + last_chunk_b]
    2454              : 
    2455              :      overlaps:
    2456              : 
    2457              :        [addr_a' + low_offset_a, addr_a' + high_offset_a + last_chunk_a]
    2458              : 
    2459              :      where [low_offset_a, high_offset_a] spans accesses [1, n-1].  I.e.:
    2460              : 
    2461              :         low_offset_a = +ve step ? 0 : seg_len_a - step
    2462              :        high_offset_a = +ve step ? seg_len_a - step : 0
    2463              : 
    2464              :      This is equivalent to testing whether:
    2465              : 
    2466              :        addr_a' + low_offset_a <= addr_b + last_chunk_b
    2467              :        && addr_b <= addr_a' + high_offset_a + last_chunk_a
    2468              : 
    2469              :      Converting this into a single test, there is an overlap if:
    2470              : 
    2471              :        0 <= addr_b + last_chunk_b - addr_a' - low_offset_a <= limit
    2472              : 
    2473              :      where limit = high_offset_a - low_offset_a + last_chunk_a + last_chunk_b
    2474              : 
    2475              :      If DR_A is performed, limit + |step| - last_chunk_b is known to be
    2476              :      less than the size of the object underlying DR_A.  We also know
    2477              :      that last_chunk_b <= |step|; this is checked elsewhere if it isn't
    2478              :      guaranteed at compile time.  There can therefore be no overflow if
    2479              :      "limit" is calculated in an unsigned type with pointer precision.  */
    2480         2640 :   tree addr_a = fold_build_pointer_plus (DR_BASE_ADDRESS (dr_a.dr),
    2481              :                                          DR_OFFSET (dr_a.dr));
    2482         2640 :   addr_a = fold_build_pointer_plus (addr_a, DR_INIT (dr_a.dr));
    2483              : 
    2484         2640 :   tree addr_b = fold_build_pointer_plus (DR_BASE_ADDRESS (dr_b.dr),
    2485              :                                          DR_OFFSET (dr_b.dr));
    2486         2640 :   addr_b = fold_build_pointer_plus (addr_b, DR_INIT (dr_b.dr));
    2487              : 
    2488              :   /* Advance ADDR_A by one iteration and adjust the length to compensate.  */
    2489         2640 :   addr_a = fold_build_pointer_plus (addr_a, step);
    2490         2640 :   tree seg_len_a_minus_step = fold_build2 (MINUS_EXPR, sizetype,
    2491              :                                            seg_len_a, step);
    2492         2640 :   if (!CONSTANT_CLASS_P (seg_len_a_minus_step))
    2493            3 :     seg_len_a_minus_step = build1 (SAVE_EXPR, sizetype, seg_len_a_minus_step);
    2494              : 
    2495         2640 :   tree low_offset_a = fold_build3 (COND_EXPR, sizetype, neg_step,
    2496              :                                    seg_len_a_minus_step, size_zero_node);
    2497         2640 :   if (!CONSTANT_CLASS_P (low_offset_a))
    2498            3 :     low_offset_a = build1 (SAVE_EXPR, sizetype, low_offset_a);
    2499              : 
    2500              :   /* We could use COND_EXPR <neg_step, size_zero_node, seg_len_a_minus_step>,
    2501              :      but it's usually more efficient to reuse the LOW_OFFSET_A result.  */
    2502         2640 :   tree high_offset_a = fold_build2 (MINUS_EXPR, sizetype, seg_len_a_minus_step,
    2503              :                                     low_offset_a);
    2504              : 
    2505              :   /* The amount added to addr_b - addr_a'.  */
    2506         2640 :   tree bias = fold_build2 (MINUS_EXPR, sizetype,
    2507              :                            size_int (last_chunk_b), low_offset_a);
    2508              : 
    2509         2640 :   tree limit = fold_build2 (MINUS_EXPR, sizetype, high_offset_a, low_offset_a);
    2510         2640 :   limit = fold_build2 (PLUS_EXPR, sizetype, limit,
    2511              :                        size_int (last_chunk_a + last_chunk_b));
    2512              : 
    2513         2640 :   tree subject = fold_build2 (MINUS_EXPR, sizetype,
    2514              :                               fold_convert (sizetype, addr_b),
    2515              :                               fold_convert (sizetype, addr_a));
    2516         2640 :   subject = fold_build2 (PLUS_EXPR, sizetype, subject, bias);
    2517              : 
    2518         2640 :   *cond_expr = fold_build2 (GT_EXPR, boolean_type_node, subject, limit);
    2519         2640 :   if (dump_enabled_p ())
    2520          322 :     dump_printf (MSG_NOTE, "using an address-based WAR/WAW test\n");
    2521              :   return true;
    2522              : }
    2523              : 
    2524              : /* If ALIGN is nonzero, set up *SEQ_MIN_OUT and *SEQ_MAX_OUT so that for
    2525              :    every address ADDR accessed by D:
    2526              : 
    2527              :      *SEQ_MIN_OUT <= ADDR (== ADDR & -ALIGN) <= *SEQ_MAX_OUT
    2528              : 
    2529              :    In this case, every element accessed by D is aligned to at least
    2530              :    ALIGN bytes.
    2531              : 
    2532              :    If ALIGN is zero then instead set *SEG_MAX_OUT so that:
    2533              : 
    2534              :      *SEQ_MIN_OUT <= ADDR < *SEQ_MAX_OUT.  */
    2535              : 
    2536              : static void
    2537         4222 : get_segment_min_max (const dr_with_seg_len &d, tree *seg_min_out,
    2538              :                      tree *seg_max_out, HOST_WIDE_INT align)
    2539              : {
    2540              :   /* Each access has the following pattern:
    2541              : 
    2542              :           <- |seg_len| ->
    2543              :           <--- A: -ve step --->
    2544              :           +-----+-------+-----+-------+-----+
    2545              :           | n-1 | ,.... |  0  | ..... | n-1 |
    2546              :           +-----+-------+-----+-------+-----+
    2547              :                         <--- B: +ve step --->
    2548              :                         <- |seg_len| ->
    2549              :                         |
    2550              :                    base address
    2551              : 
    2552              :      where "n" is the number of scalar iterations covered by the segment.
    2553              :      (This should be VF for a particular pair if we know that both steps
    2554              :      are the same, otherwise it will be the full number of scalar loop
    2555              :      iterations.)
    2556              : 
    2557              :      A is the range of bytes accessed when the step is negative,
    2558              :      B is the range when the step is positive.
    2559              : 
    2560              :      If the access size is "access_size" bytes, the lowest addressed byte is:
    2561              : 
    2562              :          base + (step < 0 ? seg_len : 0)   [LB]
    2563              : 
    2564              :      and the highest addressed byte is always below:
    2565              : 
    2566              :          base + (step < 0 ? 0 : seg_len) + access_size   [UB]
    2567              : 
    2568              :      Thus:
    2569              : 
    2570              :          LB <= ADDR < UB
    2571              : 
    2572              :      If ALIGN is nonzero, all three values are aligned to at least ALIGN
    2573              :      bytes, so:
    2574              : 
    2575              :          LB <= ADDR <= UB - ALIGN
    2576              : 
    2577              :      where "- ALIGN" folds naturally with the "+ access_size" and often
    2578              :      cancels it out.
    2579              : 
    2580              :      We don't try to simplify LB and UB beyond this (e.g. by using
    2581              :      MIN and MAX based on whether seg_len rather than the stride is
    2582              :      negative) because it is possible for the absolute size of the
    2583              :      segment to overflow the range of a ssize_t.
    2584              : 
    2585              :      Keeping the pointer_plus outside of the cond_expr should allow
    2586              :      the cond_exprs to be shared with other alias checks.  */
    2587         4222 :   tree indicator = dr_direction_indicator (d.dr);
    2588         4222 :   tree neg_step = fold_build2 (LT_EXPR, boolean_type_node,
    2589              :                                fold_convert (ssizetype, indicator),
    2590              :                                ssize_int (0));
    2591         4222 :   tree addr_base = fold_build_pointer_plus (DR_BASE_ADDRESS (d.dr),
    2592              :                                             DR_OFFSET (d.dr));
    2593         4222 :   addr_base = fold_build_pointer_plus (addr_base, DR_INIT (d.dr));
    2594         4222 :   tree seg_len
    2595         4222 :     = fold_convert (sizetype, rewrite_to_non_trapping_overflow (d.seg_len));
    2596              : 
    2597         4222 :   tree min_reach = fold_build3 (COND_EXPR, sizetype, neg_step,
    2598              :                                 seg_len, size_zero_node);
    2599         4222 :   tree max_reach = fold_build3 (COND_EXPR, sizetype, neg_step,
    2600              :                                 size_zero_node, seg_len);
    2601         4222 :   max_reach = fold_build2 (PLUS_EXPR, sizetype, max_reach,
    2602              :                            size_int (d.access_size - align));
    2603              : 
    2604         4222 :   *seg_min_out = fold_build_pointer_plus (addr_base, min_reach);
    2605         4222 :   *seg_max_out = fold_build_pointer_plus (addr_base, max_reach);
    2606         4222 : }
    2607              : 
    2608              : /* Generate a runtime condition that is true if ALIAS_PAIR is free of aliases,
    2609              :    storing the condition in *COND_EXPR.  The fallback is to generate a
    2610              :    a test that the two accesses do not overlap:
    2611              : 
    2612              :      end_a <= start_b || end_b <= start_a.  */
    2613              : 
    2614              : static void
    2615         4902 : create_intersect_range_checks (class loop *loop, tree *cond_expr,
    2616              :                                const dr_with_seg_len_pair_t &alias_pair)
    2617              : {
    2618         4902 :   const dr_with_seg_len& dr_a = alias_pair.first;
    2619         4902 :   const dr_with_seg_len& dr_b = alias_pair.second;
    2620         4902 :   *cond_expr = NULL_TREE;
    2621         4902 :   if (create_intersect_range_checks_index (loop, cond_expr, alias_pair))
    2622         2791 :     return;
    2623              : 
    2624         4751 :   if (create_ifn_alias_checks (cond_expr, alias_pair))
    2625              :     return;
    2626              : 
    2627         4751 :   if (create_waw_or_war_checks (cond_expr, alias_pair))
    2628              :     return;
    2629              : 
    2630         2111 :   unsigned HOST_WIDE_INT min_align;
    2631         2111 :   tree_code cmp_code;
    2632              :   /* We don't have to check DR_ALIAS_MIXED_STEPS here, since both versions
    2633              :      are equivalent.  This is just an optimization heuristic.  */
    2634         2111 :   if (TREE_CODE (DR_STEP (dr_a.dr)) == INTEGER_CST
    2635         2018 :       && TREE_CODE (DR_STEP (dr_b.dr)) == INTEGER_CST)
    2636              :     {
    2637              :       /* In this case adding access_size to seg_len is likely to give
    2638              :          a simple X * step, where X is either the number of scalar
    2639              :          iterations or the vectorization factor.  We're better off
    2640              :          keeping that, rather than subtracting an alignment from it.
    2641              : 
    2642              :          In this case the maximum values are exclusive and so there is
    2643              :          no alias if the maximum of one segment equals the minimum
    2644              :          of another.  */
    2645              :       min_align = 0;
    2646              :       cmp_code = LE_EXPR;
    2647              :     }
    2648              :   else
    2649              :     {
    2650              :       /* Calculate the minimum alignment shared by all four pointers,
    2651              :          then arrange for this alignment to be subtracted from the
    2652              :          exclusive maximum values to get inclusive maximum values.
    2653              :          This "- min_align" is cumulative with a "+ access_size"
    2654              :          in the calculation of the maximum values.  In the best
    2655              :          (and common) case, the two cancel each other out, leaving
    2656              :          us with an inclusive bound based only on seg_len.  In the
    2657              :          worst case we're simply adding a smaller number than before.
    2658              : 
    2659              :          Because the maximum values are inclusive, there is an alias
    2660              :          if the maximum value of one segment is equal to the minimum
    2661              :          value of the other.  */
    2662          200 :       min_align = std::min (dr_a.align, dr_b.align);
    2663          200 :       cmp_code = LT_EXPR;
    2664              :     }
    2665              : 
    2666         2111 :   tree seg_a_min, seg_a_max, seg_b_min, seg_b_max;
    2667         2111 :   get_segment_min_max (dr_a, &seg_a_min, &seg_a_max, min_align);
    2668         2111 :   get_segment_min_max (dr_b, &seg_b_min, &seg_b_max, min_align);
    2669              : 
    2670         2111 :   *cond_expr
    2671         2111 :     = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
    2672              :         fold_build2 (cmp_code, boolean_type_node, seg_a_max, seg_b_min),
    2673              :         fold_build2 (cmp_code, boolean_type_node, seg_b_max, seg_a_min));
    2674         2111 :   if (dump_enabled_p ())
    2675          286 :     dump_printf (MSG_NOTE, "using an address-based overlap test\n");
    2676              : }
    2677              : 
    2678              : /* Create a conditional expression that represents the run-time checks for
    2679              :    overlapping of address ranges represented by a list of data references
    2680              :    pairs passed in ALIAS_PAIRS.  Data references are in LOOP.  The returned
    2681              :    COND_EXPR is the conditional expression to be used in the if statement
    2682              :    that controls which version of the loop gets executed at runtime.  */
    2683              : 
    2684              : void
    2685         3277 : create_runtime_alias_checks (class loop *loop,
    2686              :                              const vec<dr_with_seg_len_pair_t> *alias_pairs,
    2687              :                              tree * cond_expr)
    2688              : {
    2689         3277 :   tree part_cond_expr;
    2690              : 
    2691        14733 :   for (const dr_with_seg_len_pair_t &alias_pair : alias_pairs)
    2692              :     {
    2693         4902 :       gcc_assert (alias_pair.flags);
    2694         4902 :       if (dump_enabled_p ())
    2695          741 :         dump_printf (MSG_NOTE,
    2696              :                      "create runtime check for data references %T and %T\n",
    2697          741 :                      DR_REF (alias_pair.first.dr),
    2698          741 :                      DR_REF (alias_pair.second.dr));
    2699              : 
    2700              :       /* Create condition expression for each pair data references.  */
    2701         4902 :       create_intersect_range_checks (loop, &part_cond_expr, alias_pair);
    2702         4902 :       if (*cond_expr)
    2703         4816 :         *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
    2704              :                                   *cond_expr, part_cond_expr);
    2705              :       else
    2706           86 :         *cond_expr = part_cond_expr;
    2707              :     }
    2708         3277 : }
    2709              : 
    2710              : /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
    2711              :    expressions.  */
    2712              : static bool
    2713            0 : dr_equal_offsets_p1 (tree offset1, tree offset2)
    2714              : {
    2715            0 :   bool res;
    2716              : 
    2717            0 :   STRIP_NOPS (offset1);
    2718            0 :   STRIP_NOPS (offset2);
    2719              : 
    2720            0 :   if (offset1 == offset2)
    2721              :     return true;
    2722              : 
    2723            0 :   if (TREE_CODE (offset1) != TREE_CODE (offset2)
    2724            0 :       || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
    2725              :     return false;
    2726              : 
    2727            0 :   res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
    2728            0 :                              TREE_OPERAND (offset2, 0));
    2729              : 
    2730            0 :   if (!res || !BINARY_CLASS_P (offset1))
    2731              :     return res;
    2732              : 
    2733            0 :   res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
    2734            0 :                              TREE_OPERAND (offset2, 1));
    2735              : 
    2736            0 :   return res;
    2737              : }
    2738              : 
    2739              : /* Check if DRA and DRB have equal offsets.  */
    2740              : bool
    2741            0 : dr_equal_offsets_p (struct data_reference *dra,
    2742              :                     struct data_reference *drb)
    2743              : {
    2744            0 :   tree offset1, offset2;
    2745              : 
    2746            0 :   offset1 = DR_OFFSET (dra);
    2747            0 :   offset2 = DR_OFFSET (drb);
    2748              : 
    2749            0 :   return dr_equal_offsets_p1 (offset1, offset2);
    2750              : }
    2751              : 
    2752              : /* Returns true if FNA == FNB.  */
    2753              : 
    2754              : static bool
    2755            0 : affine_function_equal_p (affine_fn fna, affine_fn fnb)
    2756              : {
    2757            0 :   unsigned i, n = fna.length ();
    2758              : 
    2759            0 :   if (n != fnb.length ())
    2760              :     return false;
    2761              : 
    2762            0 :   for (i = 0; i < n; i++)
    2763            0 :     if (!operand_equal_p (fna[i], fnb[i], 0))
    2764              :       return false;
    2765              : 
    2766              :   return true;
    2767              : }
    2768              : 
    2769              : /* If all the functions in CF are the same, returns one of them,
    2770              :    otherwise returns NULL.  */
    2771              : 
    2772              : static affine_fn
    2773      2304236 : common_affine_function (conflict_function *cf)
    2774              : {
    2775      2304236 :   unsigned i;
    2776      2304236 :   affine_fn comm;
    2777              : 
    2778      2304236 :   if (!CF_NONTRIVIAL_P (cf))
    2779            0 :     return affine_fn ();
    2780              : 
    2781      2304236 :   comm = cf->fns[0];
    2782              : 
    2783      2304236 :   for (i = 1; i < cf->n; i++)
    2784            0 :     if (!affine_function_equal_p (comm, cf->fns[i]))
    2785            0 :       return affine_fn ();
    2786              : 
    2787      2304236 :   return comm;
    2788              : }
    2789              : 
    2790              : /* Returns the base of the affine function FN.  */
    2791              : 
    2792              : static tree
    2793      1326280 : affine_function_base (affine_fn fn)
    2794              : {
    2795            0 :   return fn[0];
    2796              : }
    2797              : 
    2798              : /* Returns true if FN is a constant.  */
    2799              : 
    2800              : static bool
    2801      1326589 : affine_function_constant_p (affine_fn fn)
    2802              : {
    2803      1326589 :   unsigned i;
    2804      1326589 :   tree coef;
    2805              : 
    2806      1386541 :   for (i = 1; fn.iterate (i, &coef); i++)
    2807        60261 :     if (!integer_zerop (coef))
    2808              :       return false;
    2809              : 
    2810              :   return true;
    2811              : }
    2812              : 
    2813              : /* Returns true if FN is the zero constant function.  */
    2814              : 
    2815              : static bool
    2816       174471 : affine_function_zero_p (affine_fn fn)
    2817              : {
    2818       174471 :   return (integer_zerop (affine_function_base (fn))
    2819       174471 :           && affine_function_constant_p (fn));
    2820              : }
    2821              : 
    2822              : /* Returns a signed integer type with the largest precision from TA
    2823              :    and TB.  */
    2824              : 
    2825              : static tree
    2826      1740547 : signed_type_for_types (tree ta, tree tb)
    2827              : {
    2828      1740547 :   if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
    2829          565 :     return signed_type_for (ta);
    2830              :   else
    2831      1739982 :     return signed_type_for (tb);
    2832              : }
    2833              : 
    2834              : /* Applies operation OP on affine functions FNA and FNB, and returns the
    2835              :    result.  */
    2836              : 
    2837              : static affine_fn
    2838      1152118 : affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
    2839              : {
    2840      1152118 :   unsigned i, n, m;
    2841      1152118 :   affine_fn ret;
    2842      1152118 :   tree coef;
    2843              : 
    2844      3456354 :   if (fnb.length () > fna.length ())
    2845              :     {
    2846            0 :       n = fna.length ();
    2847            0 :       m = fnb.length ();
    2848              :     }
    2849              :   else
    2850              :     {
    2851      1152118 :       n = fnb.length ();
    2852              :       m = fna.length ();
    2853              :     }
    2854              : 
    2855      1152118 :   ret.create (m);
    2856      2364497 :   for (i = 0; i < n; i++)
    2857              :     {
    2858      2424758 :       tree type = signed_type_for_types (TREE_TYPE (fna[i]),
    2859      1212379 :                                          TREE_TYPE (fnb[i]));
    2860      1212379 :       ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
    2861              :     }
    2862              : 
    2863      1152118 :   for (; fna.iterate (i, &coef); i++)
    2864            0 :     ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
    2865              :                                  coef, integer_zero_node));
    2866      1152118 :   for (; fnb.iterate (i, &coef); i++)
    2867            0 :     ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
    2868              :                                  integer_zero_node, coef));
    2869              : 
    2870      1152118 :   return ret;
    2871              : }
    2872              : 
    2873              : /* Returns the sum of affine functions FNA and FNB.  */
    2874              : 
    2875              : static affine_fn
    2876            0 : affine_fn_plus (affine_fn fna, affine_fn fnb)
    2877              : {
    2878            0 :   return affine_fn_op (PLUS_EXPR, fna, fnb);
    2879              : }
    2880              : 
    2881              : /* Returns the difference of affine functions FNA and FNB.  */
    2882              : 
    2883              : static affine_fn
    2884      1152118 : affine_fn_minus (affine_fn fna, affine_fn fnb)
    2885              : {
    2886            0 :   return affine_fn_op (MINUS_EXPR, fna, fnb);
    2887              : }
    2888              : 
    2889              : /* Frees affine function FN.  */
    2890              : 
    2891              : static void
    2892      3652974 : affine_fn_free (affine_fn fn)
    2893              : {
    2894            0 :   fn.release ();
    2895            0 : }
    2896              : 
    2897              : /* Determine for each subscript in the data dependence relation DDR
    2898              :    the distance.  */
    2899              : 
    2900              : static void
    2901      3089372 : compute_subscript_distance (struct data_dependence_relation *ddr)
    2902              : {
    2903      3089372 :   conflict_function *cf_a, *cf_b;
    2904      3089372 :   affine_fn fn_a, fn_b, diff;
    2905              : 
    2906      3089372 :   if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
    2907              :     {
    2908              :       unsigned int i;
    2909              : 
    2910      4241490 :       for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
    2911              :         {
    2912      1152118 :           struct subscript *subscript;
    2913              : 
    2914      1152118 :           subscript = DDR_SUBSCRIPT (ddr, i);
    2915      1152118 :           cf_a = SUB_CONFLICTS_IN_A (subscript);
    2916      1152118 :           cf_b = SUB_CONFLICTS_IN_B (subscript);
    2917              : 
    2918      1152118 :           fn_a = common_affine_function (cf_a);
    2919      1152118 :           fn_b = common_affine_function (cf_b);
    2920      1152118 :           if (!fn_a.exists () || !fn_b.exists ())
    2921              :             {
    2922            0 :               SUB_DISTANCE (subscript) = chrec_dont_know;
    2923            0 :               return;
    2924              :             }
    2925      1152118 :           diff = affine_fn_minus (fn_a, fn_b);
    2926              : 
    2927      1152118 :           if (affine_function_constant_p (diff))
    2928      1151809 :             SUB_DISTANCE (subscript) = affine_function_base (diff);
    2929              :           else
    2930          309 :             SUB_DISTANCE (subscript) = chrec_dont_know;
    2931              : 
    2932      1152118 :           affine_fn_free (diff);
    2933              :         }
    2934              :     }
    2935              : }
    2936              : 
    2937              : /* Returns the conflict function for "unknown".  */
    2938              : 
    2939              : static conflict_function *
    2940      8002900 : conflict_fn_not_known (void)
    2941              : {
    2942            0 :   conflict_function *fn = XCNEW (conflict_function);
    2943      8002900 :   fn->n = NOT_KNOWN;
    2944              : 
    2945      8002900 :   return fn;
    2946              : }
    2947              : 
    2948              : /* Returns the conflict function for "independent".  */
    2949              : 
    2950              : static conflict_function *
    2951      4286324 : conflict_fn_no_dependence (void)
    2952              : {
    2953            0 :   conflict_function *fn = XCNEW (conflict_function);
    2954      4286324 :   fn->n = NO_DEPENDENCE;
    2955              : 
    2956      4286324 :   return fn;
    2957              : }
    2958              : 
    2959              : /* Returns true if the address of OBJ is invariant in LOOP.  */
    2960              : 
    2961              : static bool
    2962      3283763 : object_address_invariant_in_loop_p (const class loop *loop, const_tree obj)
    2963              : {
    2964      3449385 :   while (handled_component_p (obj))
    2965              :     {
    2966       170991 :       if (TREE_CODE (obj) == ARRAY_REF)
    2967              :         {
    2968         9733 :           for (int i = 1; i < 4; ++i)
    2969         8642 :             if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, i),
    2970         8642 :                                                         loop->num))
    2971              :               return false;
    2972              :         }
    2973       164531 :       else if (TREE_CODE (obj) == COMPONENT_REF)
    2974              :         {
    2975       143435 :           if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
    2976       143435 :                                                       loop->num))
    2977              :             return false;
    2978              :         }
    2979       165622 :       obj = TREE_OPERAND (obj, 0);
    2980              :     }
    2981              : 
    2982      3278394 :   if (!INDIRECT_REF_P (obj)
    2983      3278394 :       && TREE_CODE (obj) != MEM_REF)
    2984              :     return true;
    2985              : 
    2986      3253881 :   return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
    2987      6507762 :                                                   loop->num);
    2988              : }
    2989              : 
    2990              : /* Helper for contains_ssa_ref_p.  */
    2991              : 
    2992              : static bool
    2993       100690 : contains_ssa_ref_p_1 (tree, tree *idx, void *data)
    2994              : {
    2995       100690 :   if (TREE_CODE (*idx) == SSA_NAME)
    2996              :     {
    2997        93837 :       *(bool *)data = true;
    2998        93837 :       return false;
    2999              :     }
    3000              :   return true;
    3001              : }
    3002              : 
    3003              : /* Returns true if the reference REF contains a SSA index. */
    3004              : 
    3005              : static bool
    3006       256380 : contains_ssa_ref_p (tree ref)
    3007              : {
    3008       256380 :   bool res = false;
    3009            0 :   for_each_index (&ref, contains_ssa_ref_p_1, &res);
    3010       256380 :   return res;
    3011              : }
    3012              : 
    3013              : /* Returns false if we can prove that data references A and B do not alias,
    3014              :    true otherwise.  If LOOP_NEST is false no cross-iteration aliases are
    3015              :    considered.  */
    3016              : 
    3017              : bool
    3018     14627023 : dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
    3019              :                 class loop *loop_nest)
    3020              : {
    3021     14627023 :   tree addr_a = DR_BASE_OBJECT (a);
    3022     14627023 :   tree addr_b = DR_BASE_OBJECT (b);
    3023              : 
    3024              :   /* If we are not processing a loop nest but scalar code we
    3025              :      do not need to care about possible cross-iteration dependences
    3026              :      and thus can process the full original reference.  Do so,
    3027              :      similar to how loop invariant motion applies extra offset-based
    3028              :      disambiguation.  */
    3029     14627023 :   if (!loop_nest)
    3030              :     {
    3031      8163456 :       tree tree_size_a = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (a)));
    3032      8163456 :       tree tree_size_b = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (b)));
    3033              : 
    3034      8163456 :       if (DR_BASE_ADDRESS (a)
    3035      8155041 :           && DR_BASE_ADDRESS (b)
    3036      8154710 :           && operand_equal_p (DR_BASE_ADDRESS (a), DR_BASE_ADDRESS (b))
    3037      7306989 :           && operand_equal_p (DR_OFFSET (a), DR_OFFSET (b))
    3038      7220949 :           && tree_size_a
    3039      7220949 :           && tree_size_b
    3040      7220940 :           && poly_int_tree_p (tree_size_a)
    3041      7220914 :           && poly_int_tree_p (tree_size_b)
    3042     15384370 :           && !ranges_maybe_overlap_p (wi::to_poly_widest (DR_INIT (a)),
    3043      7220914 :                                       wi::to_poly_widest (tree_size_a),
    3044      7220914 :                                       wi::to_poly_widest (DR_INIT (b)),
    3045      7220914 :                                       wi::to_poly_widest (tree_size_b)))
    3046              :         {
    3047      5392018 :           gcc_assert (integer_zerop (DR_STEP (a))
    3048              :                       && integer_zerop (DR_STEP (b)));
    3049      5392050 :           return false;
    3050              :         }
    3051              : 
    3052     11085752 :       aff_tree off1, off2;
    3053              :       poly_widest_int size1, size2;
    3054      2771438 :       get_inner_reference_aff (DR_REF (a), &off1, &size1);
    3055      2771438 :       get_inner_reference_aff (DR_REF (b), &off2, &size2);
    3056      2771438 :       aff_combination_scale (&off1, -1);
    3057      2771438 :       aff_combination_add (&off2, &off1);
    3058      2771438 :       if (aff_comb_cannot_overlap_p (&off2, size1, size2))
    3059           32 :         return false;
    3060      2771438 :     }
    3061              : 
    3062      9234973 :   if ((TREE_CODE (addr_a) == MEM_REF || TREE_CODE (addr_a) == TARGET_MEM_REF)
    3063      6845461 :       && (TREE_CODE (addr_b) == MEM_REF || TREE_CODE (addr_b) == TARGET_MEM_REF)
    3064              :       /* For cross-iteration dependences the cliques must be valid for the
    3065              :          whole loop, not just individual iterations.  */
    3066      6591682 :       && (!loop_nest
    3067      6250814 :           || MR_DEPENDENCE_CLIQUE (addr_a) == 1
    3068      5355880 :           || MR_DEPENDENCE_CLIQUE (addr_a) == loop_nest->owned_clique)
    3069      6365730 :       && MR_DEPENDENCE_CLIQUE (addr_a) == MR_DEPENDENCE_CLIQUE (addr_b)
    3070     15405421 :       && MR_DEPENDENCE_BASE (addr_a) != MR_DEPENDENCE_BASE (addr_b))
    3071              :     return false;
    3072              : 
    3073              :   /* If we had an evolution in a pointer-based MEM_REF BASE_OBJECT we
    3074              :      do not know the size of the base-object.  So we cannot do any
    3075              :      offset/overlap based analysis but have to rely on points-to
    3076              :      information only.  */
    3077      9008101 :   if (TREE_CODE (addr_a) == MEM_REF
    3078      9008101 :       && (DR_UNCONSTRAINED_BASE (a)
    3079      4135984 :           || TREE_CODE (TREE_OPERAND (addr_a, 0)) == SSA_NAME))
    3080              :     {
    3081              :       /* For true dependences we can apply TBAA.  */
    3082      4154555 :       if (flag_strict_aliasing
    3083      3975666 :           && DR_IS_WRITE (a) && DR_IS_READ (b)
    3084      4328232 :           && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
    3085       173677 :                                      get_alias_set (DR_REF (b))))
    3086              :         return false;
    3087      4123848 :       if (TREE_CODE (addr_b) == MEM_REF)
    3088      4018247 :         return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
    3089      8036494 :                                        TREE_OPERAND (addr_b, 0));
    3090              :       else
    3091       105601 :         return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
    3092       105601 :                                        build_fold_addr_expr (addr_b));
    3093              :     }
    3094      4853546 :   else if (TREE_CODE (addr_b) == MEM_REF
    3095      4853546 :            && (DR_UNCONSTRAINED_BASE (b)
    3096      2527747 :                || TREE_CODE (TREE_OPERAND (addr_b, 0)) == SSA_NAME))
    3097              :     {
    3098              :       /* For true dependences we can apply TBAA.  */
    3099       328212 :       if (flag_strict_aliasing
    3100       270204 :           && DR_IS_WRITE (a) && DR_IS_READ (b)
    3101       405515 :           && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
    3102        77303 :                                      get_alias_set (DR_REF (b))))
    3103              :         return false;
    3104       312986 :       if (TREE_CODE (addr_a) == MEM_REF)
    3105       183211 :         return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
    3106       366422 :                                        TREE_OPERAND (addr_b, 0));
    3107              :       else
    3108       129775 :         return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
    3109       259550 :                                        TREE_OPERAND (addr_b, 0));
    3110              :     }
    3111              :   /* If dr_analyze_innermost failed to handle a component we are
    3112              :      possibly left with a non-base in which case we didn't analyze
    3113              :      a possible evolution of the base when analyzing a loop.  */
    3114      4525334 :   else if (loop_nest
    3115      6667777 :            && ((handled_component_p (addr_a) && contains_ssa_ref_p (addr_a))
    3116        83719 :                || (handled_component_p (addr_b) && contains_ssa_ref_p (addr_b))))
    3117              :     {
    3118              :       /* For true dependences we can apply TBAA.  */
    3119        93837 :       if (flag_strict_aliasing
    3120        93195 :           && DR_IS_WRITE (a) && DR_IS_READ (b)
    3121       103279 :           && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
    3122         9442 :                                      get_alias_set (DR_REF (b))))
    3123              :         return false;
    3124        89714 :       if (TREE_CODE (addr_a) == MEM_REF)
    3125         3845 :         return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
    3126         3845 :                                        build_fold_addr_expr (addr_b));
    3127        85869 :       else if (TREE_CODE (addr_b) == MEM_REF)
    3128         6368 :         return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
    3129        12736 :                                        TREE_OPERAND (addr_b, 0));
    3130              :       else
    3131        79501 :         return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
    3132        79501 :                                        build_fold_addr_expr (addr_b));
    3133              :     }
    3134              : 
    3135              :   /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
    3136              :      that is being subsetted in the loop nest.  */
    3137      4431497 :   if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
    3138      2965473 :     return refs_output_dependent_p (addr_a, addr_b);
    3139      1466024 :   else if (DR_IS_READ (a) && DR_IS_WRITE (b))
    3140       403216 :     return refs_anti_dependent_p (addr_a, addr_b);
    3141      1062808 :   return refs_may_alias_p (addr_a, addr_b);
    3142              : }
    3143              : 
    3144              : /* REF_A and REF_B both satisfy access_fn_component_p.  Return true
    3145              :    if it is meaningful to compare their associated access functions
    3146              :    when checking for dependencies.  */
    3147              : 
    3148              : static bool
    3149      2881257 : access_fn_components_comparable_p (tree ref_a, tree ref_b)
    3150              : {
    3151              :   /* Allow pairs of component refs from the following sets:
    3152              : 
    3153              :        { REALPART_EXPR, IMAGPART_EXPR }
    3154              :        { COMPONENT_REF }
    3155              :        { ARRAY_REF }.  */
    3156      2881257 :   tree_code code_a = TREE_CODE (ref_a);
    3157      2881257 :   tree_code code_b = TREE_CODE (ref_b);
    3158      2881257 :   if (code_a == IMAGPART_EXPR)
    3159        34706 :     code_a = REALPART_EXPR;
    3160      2881257 :   if (code_b == IMAGPART_EXPR)
    3161        40919 :     code_b = REALPART_EXPR;
    3162      2881257 :   if (code_a != code_b)
    3163              :     return false;
    3164              : 
    3165      2857912 :   if (TREE_CODE (ref_a) == COMPONENT_REF)
    3166              :     /* ??? We cannot simply use the type of operand #0 of the refs here as
    3167              :        the Fortran compiler smuggles type punning into COMPONENT_REFs.
    3168              :        Use the DECL_CONTEXT of the FIELD_DECLs instead.  */
    3169       979446 :     return (DECL_CONTEXT (TREE_OPERAND (ref_a, 1))
    3170       979446 :             == DECL_CONTEXT (TREE_OPERAND (ref_b, 1)));
    3171              : 
    3172      1878466 :   return types_compatible_p (TREE_TYPE (TREE_OPERAND (ref_a, 0)),
    3173      3756932 :                              TREE_TYPE (TREE_OPERAND (ref_b, 0)));
    3174              : }
    3175              : 
    3176              : /* Initialize a data dependence relation RES in LOOP_NEST.  USE_ALT_INDICES
    3177              :    is true when the main indices of A and B were not comparable so we try again
    3178              :    with alternate indices computed on an indirect reference.  */
    3179              : 
    3180              : struct data_dependence_relation *
    3181      6596702 : initialize_data_dependence_relation (struct data_dependence_relation *res,
    3182              :                                      vec<loop_p> loop_nest,
    3183              :                                      bool use_alt_indices)
    3184              : {
    3185      6596702 :   struct data_reference *a = DDR_A (res);
    3186      6596702 :   struct data_reference *b = DDR_B (res);
    3187      6596702 :   unsigned int i;
    3188              : 
    3189      6596702 :   struct indices *indices_a = &a->indices;
    3190      6596702 :   struct indices *indices_b = &b->indices;
    3191      6596702 :   if (use_alt_indices)
    3192              :     {
    3193       370851 :       if (TREE_CODE (DR_REF (a)) != MEM_REF)
    3194       231973 :         indices_a = &a->alt_indices;
    3195       370851 :       if (TREE_CODE (DR_REF (b)) != MEM_REF)
    3196       263064 :         indices_b = &b->alt_indices;
    3197              :     }
    3198      6596702 :   unsigned int num_dimensions_a = indices_a->access_fns.length ();
    3199      6596702 :   unsigned int num_dimensions_b = indices_b->access_fns.length ();
    3200      6596702 :   if (num_dimensions_a == 0 || num_dimensions_b == 0)
    3201              :     {
    3202      2245042 :       DDR_ARE_DEPENDENT (res) = chrec_dont_know;
    3203      2245042 :       return res;
    3204              :     }
    3205              : 
    3206              :   /* For unconstrained bases, the root (highest-indexed) subscript
    3207              :      describes a variation in the base of the original DR_REF rather
    3208              :      than a component access.  We have no type that accurately describes
    3209              :      the new DR_BASE_OBJECT (whose TREE_TYPE describes the type *after*
    3210              :      applying this subscript) so limit the search to the last real
    3211              :      component access.
    3212              : 
    3213              :      E.g. for:
    3214              : 
    3215              :         void
    3216              :         f (int a[][8], int b[][8])
    3217              :         {
    3218              :           for (int i = 0; i < 8; ++i)
    3219              :             a[i * 2][0] = b[i][0];
    3220              :         }
    3221              : 
    3222              :      the a and b accesses have a single ARRAY_REF component reference [0]
    3223              :      but have two subscripts.  */
    3224      4351660 :   if (indices_a->unconstrained_base)
    3225      2483077 :     num_dimensions_a -= 1;
    3226      4351660 :   if (indices_b->unconstrained_base)
    3227      2437213 :     num_dimensions_b -= 1;
    3228              : 
    3229              :   /* These structures describe sequences of component references in
    3230              :      DR_REF (A) and DR_REF (B).  Each component reference is tied to a
    3231              :      specific access function.  */
    3232      4351660 :   struct {
    3233              :     /* The sequence starts at DR_ACCESS_FN (A, START_A) of A and
    3234              :        DR_ACCESS_FN (B, START_B) of B (inclusive) and extends to higher
    3235              :        indices.  In C notation, these are the indices of the rightmost
    3236              :        component references; e.g. for a sequence .b.c.d, the start
    3237              :        index is for .d.  */
    3238              :     unsigned int start_a;
    3239              :     unsigned int start_b;
    3240              : 
    3241              :     /* The sequence contains LENGTH consecutive access functions from
    3242              :        each DR.  */
    3243              :     unsigned int length;
    3244              : 
    3245              :     /* The enclosing objects for the A and B sequences respectively,
    3246              :        i.e. the objects to which DR_ACCESS_FN (A, START_A + LENGTH - 1)
    3247              :        and DR_ACCESS_FN (B, START_B + LENGTH - 1) are applied.  */
    3248              :     tree object_a;
    3249              :     tree object_b;
    3250      4351660 :   } full_seq = {}, struct_seq = {};
    3251              : 
    3252              :   /* Before each iteration of the loop:
    3253              : 
    3254              :      - REF_A is what you get after applying DR_ACCESS_FN (A, INDEX_A) and
    3255              :      - REF_B is what you get after applying DR_ACCESS_FN (B, INDEX_B).  */
    3256      4351660 :   unsigned int index_a = 0;
    3257      4351660 :   unsigned int index_b = 0;
    3258      4351660 :   tree ref_a = DR_REF (a);
    3259      4351660 :   tree ref_b = DR_REF (b);
    3260              : 
    3261              :   /* Now walk the component references from the final DR_REFs back up to
    3262              :      the enclosing base objects.  Each component reference corresponds
    3263              :      to one access function in the DR, with access function 0 being for
    3264              :      the final DR_REF and the highest-indexed access function being the
    3265              :      one that is applied to the base of the DR.
    3266              : 
    3267              :      Look for a sequence of component references whose access functions
    3268              :      are comparable (see access_fn_components_comparable_p).  If more
    3269              :      than one such sequence exists, pick the one nearest the base
    3270              :      (which is the leftmost sequence in C notation).  Store this sequence
    3271              :      in FULL_SEQ.
    3272              : 
    3273              :      For example, if we have:
    3274              : 
    3275              :         struct foo { struct bar s; ... } (*a)[10], (*b)[10];
    3276              : 
    3277              :         A: a[0][i].s.c.d
    3278              :         B: __real b[0][i].s.e[i].f
    3279              : 
    3280              :      (where d is the same type as the real component of f) then the access
    3281              :      functions would be:
    3282              : 
    3283              :                          0   1   2   3
    3284              :         A:              .d  .c  .s [i]
    3285              : 
    3286              :                  0   1   2   3   4   5
    3287              :         B:  __real  .f [i]  .e  .s [i]
    3288              : 
    3289              :      The A0/B2 column isn't comparable, since .d is a COMPONENT_REF
    3290              :      and [i] is an ARRAY_REF.  However, the A1/B3 column contains two
    3291              :      COMPONENT_REF accesses for struct bar, so is comparable.  Likewise
    3292              :      the A2/B4 column contains two COMPONENT_REF accesses for struct foo,
    3293              :      so is comparable.  The A3/B5 column contains two ARRAY_REFs that
    3294              :      index foo[10] arrays, so is again comparable.  The sequence is
    3295              :      therefore:
    3296              : 
    3297              :         A: [1, 3]  (i.e. [i].s.c)
    3298              :         B: [3, 5]  (i.e. [i].s.e)
    3299              : 
    3300              :      Also look for sequences of component references whose access
    3301              :      functions are comparable and whose enclosing objects have the same
    3302              :      RECORD_TYPE.  Store this sequence in STRUCT_SEQ.  In the above
    3303              :      example, STRUCT_SEQ would be:
    3304              : 
    3305              :         A: [1, 2]  (i.e. s.c)
    3306              :         B: [3, 4]  (i.e. s.e)  */
    3307      7219950 :   while (index_a < num_dimensions_a && index_b < num_dimensions_b)
    3308              :     {
    3309              :       /* The alternate indices form always has a single dimension
    3310              :          with unconstrained base.  */
    3311      2881257 :       gcc_assert (!use_alt_indices);
    3312              : 
    3313              :       /* REF_A and REF_B must be one of the component access types
    3314              :          allowed by dr_analyze_indices.  */
    3315      2881257 :       gcc_checking_assert (access_fn_component_p (ref_a));
    3316      2881257 :       gcc_checking_assert (access_fn_component_p (ref_b));
    3317              : 
    3318              :       /* Get the immediately-enclosing objects for REF_A and REF_B,
    3319              :          i.e. the references *before* applying DR_ACCESS_FN (A, INDEX_A)
    3320              :          and DR_ACCESS_FN (B, INDEX_B).  */
    3321      2881257 :       tree object_a = TREE_OPERAND (ref_a, 0);
    3322      2881257 :       tree object_b = TREE_OPERAND (ref_b, 0);
    3323              : 
    3324      2881257 :       tree type_a = TREE_TYPE (object_a);
    3325      2881257 :       tree type_b = TREE_TYPE (object_b);
    3326      2881257 :       if (access_fn_components_comparable_p (ref_a, ref_b))
    3327              :         {
    3328              :           /* This pair of component accesses is comparable for dependence
    3329              :              analysis, so we can include DR_ACCESS_FN (A, INDEX_A) and
    3330              :              DR_ACCESS_FN (B, INDEX_B) in the sequence.  */
    3331      2630025 :           if (full_seq.start_a + full_seq.length != index_a
    3332      2574005 :               || full_seq.start_b + full_seq.length != index_b)
    3333              :             {
    3334              :               /* The accesses don't extend the current sequence,
    3335              :                  so start a new one here.  */
    3336        63193 :               full_seq.start_a = index_a;
    3337        63193 :               full_seq.start_b = index_b;
    3338        63193 :               full_seq.length = 0;
    3339              :             }
    3340              : 
    3341              :           /* Add this pair of references to the sequence.  */
    3342      2630025 :           full_seq.length += 1;
    3343      2630025 :           full_seq.object_a = object_a;
    3344      2630025 :           full_seq.object_b = object_b;
    3345              : 
    3346              :           /* If the enclosing objects are structures (and thus have the
    3347              :              same RECORD_TYPE), record the new sequence in STRUCT_SEQ.  */
    3348      2630025 :           if (TREE_CODE (type_a) == RECORD_TYPE)
    3349       767605 :             struct_seq = full_seq;
    3350              : 
    3351              :           /* Move to the next containing reference for both A and B.  */
    3352      2630025 :           ref_a = object_a;
    3353      2630025 :           ref_b = object_b;
    3354      2630025 :           index_a += 1;
    3355      2630025 :           index_b += 1;
    3356      2630025 :           continue;
    3357              :         }
    3358              : 
    3359              :       /* Try to approach equal type sizes.  */
    3360       251232 :       if (!COMPLETE_TYPE_P (type_a)
    3361       248215 :           || !COMPLETE_TYPE_P (type_b)
    3362       240140 :           || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type_a))
    3363       489776 :           || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type_b)))
    3364              :         break;
    3365              : 
    3366       238265 :       unsigned HOST_WIDE_INT size_a = tree_to_uhwi (TYPE_SIZE_UNIT (type_a));
    3367       238265 :       unsigned HOST_WIDE_INT size_b = tree_to_uhwi (TYPE_SIZE_UNIT (type_b));
    3368       238265 :       if (size_a <= size_b)
    3369              :         {
    3370       144031 :           index_a += 1;
    3371       144031 :           ref_a = object_a;
    3372              :         }
    3373       238265 :       if (size_b <= size_a)
    3374              :         {
    3375       109365 :           index_b += 1;
    3376       109365 :           ref_b = object_b;
    3377              :         }
    3378              :     }
    3379              : 
    3380              :   /* See whether FULL_SEQ ends at the base and whether the two bases
    3381              :      are equal.  We do not care about TBAA or alignment info so we can
    3382              :      use OEP_ADDRESS_OF to avoid false negatives.  */
    3383      4351660 :   tree base_a = indices_a->base_object;
    3384      4351660 :   tree base_b = indices_b->base_object;
    3385      4351660 :   bool same_base_p = (full_seq.start_a + full_seq.length == num_dimensions_a
    3386      4146324 :                       && full_seq.start_b + full_seq.length == num_dimensions_b
    3387      3993900 :                       && (indices_a->unconstrained_base
    3388      3993900 :                           == indices_b->unconstrained_base)
    3389      3989154 :                       && operand_equal_p (base_a, base_b, OEP_ADDRESS_OF)
    3390      3515624 :                       && (types_compatible_p (TREE_TYPE (base_a),
    3391      3515624 :                                               TREE_TYPE (base_b))
    3392       862991 :                           || (!base_supports_access_fn_components_p (base_a)
    3393       857623 :                               && !base_supports_access_fn_components_p (base_b)
    3394       856000 :                               && operand_equal_p
    3395       856000 :                                    (TYPE_SIZE (TREE_TYPE (base_a)),
    3396       856000 :                                     TYPE_SIZE (TREE_TYPE (base_b)), 0)))
    3397      7413016 :                       && (!loop_nest.exists ()
    3398      3061356 :                           || (object_address_invariant_in_loop_p
    3399      3061356 :                               (loop_nest[0], base_a))));
    3400              : 
    3401              :   /* If the bases are the same, we can include the base variation too.
    3402              :      E.g. the b accesses in:
    3403              : 
    3404              :        for (int i = 0; i < n; ++i)
    3405              :          b[i + 4][0] = b[i][0];
    3406              : 
    3407              :      have a definite dependence distance of 4, while for:
    3408              : 
    3409              :        for (int i = 0; i < n; ++i)
    3410              :          a[i + 4][0] = b[i][0];
    3411              : 
    3412              :      the dependence distance depends on the gap between a and b.
    3413              : 
    3414              :      If the bases are different then we can only rely on the sequence
    3415              :      rooted at a structure access, since arrays are allowed to overlap
    3416              :      arbitrarily and change shape arbitrarily.  E.g. we treat this as
    3417              :      valid code:
    3418              : 
    3419              :        int a[256];
    3420              :        ...
    3421              :        ((int (*)[4][3]) &a[1])[i][0] += ((int (*)[4][3]) &a[2])[i][0];
    3422              : 
    3423              :      where two lvalues with the same int[4][3] type overlap, and where
    3424              :      both lvalues are distinct from the object's declared type.  */
    3425      2938565 :   if (same_base_p)
    3426              :     {
    3427      2938565 :       if (indices_a->unconstrained_base)
    3428      1488151 :         full_seq.length += 1;
    3429              :     }
    3430              :   else
    3431              :     full_seq = struct_seq;
    3432              : 
    3433              :   /* Punt if we didn't find a suitable sequence.  */
    3434      4351660 :   if (full_seq.length == 0)
    3435              :     {
    3436      1140008 :       if (use_alt_indices
    3437      1018502 :           || (TREE_CODE (DR_REF (a)) == MEM_REF
    3438       785733 :               && TREE_CODE (DR_REF (b)) == MEM_REF)
    3439       373027 :           || may_be_nonaddressable_p (DR_REF (a))
    3440      1512626 :           || may_be_nonaddressable_p (DR_REF (b)))
    3441              :         {
    3442              :           /* Fully exhausted possibilities.  */
    3443       769157 :           DDR_ARE_DEPENDENT (res) = chrec_dont_know;
    3444       769157 :           return res;
    3445              :         }
    3446              : 
    3447              :       /* Try evaluating both DRs as dereferences of pointers.  */
    3448       370851 :       if (!a->alt_indices.base_object
    3449       173744 :           && TREE_CODE (DR_REF (a)) != MEM_REF)
    3450              :         {
    3451        34866 :           tree alt_ref = build2 (MEM_REF, TREE_TYPE (DR_REF (a)),
    3452              :                                  build1 (ADDR_EXPR, ptr_type_node, DR_REF (a)),
    3453              :                                  build_int_cst
    3454              :                                    (reference_alias_ptr_type (DR_REF (a)), 0));
    3455       104598 :           dr_analyze_indices (&a->alt_indices, alt_ref,
    3456        34866 :                               loop_preheader_edge (loop_nest[0]),
    3457              :                               loop_containing_stmt (DR_STMT (a)));
    3458              :         }
    3459       370851 :       if (!b->alt_indices.base_object
    3460       188180 :           && TREE_CODE (DR_REF (b)) != MEM_REF)
    3461              :         {
    3462        80393 :           tree alt_ref = build2 (MEM_REF, TREE_TYPE (DR_REF (b)),
    3463              :                                  build1 (ADDR_EXPR, ptr_type_node, DR_REF (b)),
    3464              :                                  build_int_cst
    3465              :                                    (reference_alias_ptr_type (DR_REF (b)), 0));
    3466       241179 :           dr_analyze_indices (&b->alt_indices, alt_ref,
    3467        80393 :                               loop_preheader_edge (loop_nest[0]),
    3468              :                               loop_containing_stmt (DR_STMT (b)));
    3469              :         }
    3470       370851 :       return initialize_data_dependence_relation (res, loop_nest, true);
    3471              :     }
    3472              : 
    3473      3211652 :   if (!same_base_p)
    3474              :     {
    3475              :       /* Partial overlap is possible for different bases when strict aliasing
    3476              :          is not in effect.  It's also possible if either base involves a union
    3477              :          access; e.g. for:
    3478              : 
    3479              :            struct s1 { int a[2]; };
    3480              :            struct s2 { struct s1 b; int c; };
    3481              :            struct s3 { int d; struct s1 e; };
    3482              :            union u { struct s2 f; struct s3 g; } *p, *q;
    3483              : 
    3484              :          the s1 at "p->f.b" (base "p->f") partially overlaps the s1 at
    3485              :          "p->g.e" (base "p->g") and might partially overlap the s1 at
    3486              :          "q->g.e" (base "q->g").  */
    3487       273087 :       if (!flag_strict_aliasing
    3488       261444 :           || ref_contains_union_access_p (full_seq.object_a)
    3489       476491 :           || ref_contains_union_access_p (full_seq.object_b))
    3490              :         {
    3491        69721 :           DDR_ARE_DEPENDENT (res) = chrec_dont_know;
    3492        69721 :           return res;
    3493              :         }
    3494              : 
    3495       203366 :       DDR_COULD_BE_INDEPENDENT_P (res) = true;
    3496       203366 :       if (!loop_nest.exists ()
    3497       406732 :           || (object_address_invariant_in_loop_p (loop_nest[0],
    3498       203366 :                                                   full_seq.object_a)
    3499        19041 :               && object_address_invariant_in_loop_p (loop_nest[0],
    3500        19041 :                                                      full_seq.object_b)))
    3501              :         {
    3502         9378 :           DDR_OBJECT_A (res) = full_seq.object_a;
    3503         9378 :           DDR_OBJECT_B (res) = full_seq.object_b;
    3504              :         }
    3505              :     }
    3506              : 
    3507      3141931 :   DDR_AFFINE_P (res) = true;
    3508      3141931 :   DDR_ARE_DEPENDENT (res) = NULL_TREE;
    3509      3141931 :   DDR_SUBSCRIPTS (res).create (full_seq.length);
    3510      3141931 :   DDR_LOOP_NEST (res) = loop_nest;
    3511      3141931 :   DDR_SELF_REFERENCE (res) = false;
    3512              : 
    3513      7113420 :   for (i = 0; i < full_seq.length; ++i)
    3514              :     {
    3515      3971489 :       struct subscript *subscript;
    3516              : 
    3517      3971489 :       subscript = XNEW (struct subscript);
    3518      3971489 :       SUB_ACCESS_FN (subscript, 0) = indices_a->access_fns[full_seq.start_a + i];
    3519      3971489 :       SUB_ACCESS_FN (subscript, 1) = indices_b->access_fns[full_seq.start_b + i];
    3520      3971489 :       SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
    3521      3971489 :       SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
    3522      3971489 :       SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
    3523      3971489 :       SUB_DISTANCE (subscript) = chrec_dont_know;
    3524      3971489 :       DDR_SUBSCRIPTS (res).safe_push (subscript);
    3525              :     }
    3526              : 
    3527              :   return res;
    3528              : }
    3529              : 
    3530              : /* Initialize a data dependence relation between data accesses A and
    3531              :    B.  NB_LOOPS is the number of loops surrounding the references: the
    3532              :    size of the classic distance/direction vectors.  */
    3533              : 
    3534              : struct data_dependence_relation *
    3535     13394350 : initialize_data_dependence_relation (struct data_reference *a,
    3536              :                                      struct data_reference *b,
    3537              :                                      vec<loop_p> loop_nest)
    3538              : {
    3539     13394350 :   data_dependence_relation *res = XCNEW (struct data_dependence_relation);
    3540     13394350 :   DDR_A (res) = a;
    3541     13394350 :   DDR_B (res) = b;
    3542     13394350 :   DDR_LOOP_NEST (res).create (0);
    3543     13394350 :   DDR_SUBSCRIPTS (res).create (0);
    3544     13394350 :   DDR_DIR_VECTS (res).create (0);
    3545     13394350 :   DDR_DIST_VECTS (res).create (0);
    3546              : 
    3547     13394350 :   if (a == NULL || b == NULL)
    3548              :     {
    3549            0 :       DDR_ARE_DEPENDENT (res) = chrec_dont_know;
    3550            0 :       return res;
    3551              :     }
    3552              : 
    3553              :   /* If the data references do not alias, then they are independent.  */
    3554     19839883 :   if (!dr_may_alias_p (a, b, loop_nest.exists () ? loop_nest[0] : NULL))
    3555              :     {
    3556      7168499 :       DDR_ARE_DEPENDENT (res) = chrec_known;
    3557      7168499 :       return res;
    3558              :     }
    3559              : 
    3560      6225851 :   return initialize_data_dependence_relation (res, loop_nest, false);
    3561              : }
    3562              : 
    3563              : 
    3564              : /* Frees memory used by the conflict function F.  */
    3565              : 
    3566              : static void
    3567     14790080 : free_conflict_function (conflict_function *f)
    3568              : {
    3569     14790080 :   unsigned i;
    3570              : 
    3571     14790080 :   if (CF_NONTRIVIAL_P (f))
    3572              :     {
    3573      5001712 :       for (i = 0; i < f->n; i++)
    3574      2500856 :         affine_fn_free (f->fns[i]);
    3575              :     }
    3576     14790080 :   free (f);
    3577     14790080 : }
    3578              : 
    3579              : /* Frees memory used by SUBSCRIPTS.  */
    3580              : 
    3581              : static void
    3582      3141931 : free_subscripts (vec<subscript_p> subscripts)
    3583              : {
    3584     13397282 :   for (subscript_p s : subscripts)
    3585              :     {
    3586      3971489 :       free_conflict_function (s->conflicting_iterations_in_a);
    3587      3971489 :       free_conflict_function (s->conflicting_iterations_in_b);
    3588      3971489 :       free (s);
    3589              :     }
    3590      3141931 :   subscripts.release ();
    3591      3141931 : }
    3592              : 
    3593              : /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
    3594              :    description.  */
    3595              : 
    3596              : static inline void
    3597      2233103 : finalize_ddr_dependent (struct data_dependence_relation *ddr,
    3598              :                         tree chrec)
    3599              : {
    3600      2233103 :   DDR_ARE_DEPENDENT (ddr) = chrec;
    3601      2233103 :   free_subscripts (DDR_SUBSCRIPTS (ddr));
    3602      2233103 :   DDR_SUBSCRIPTS (ddr).create (0);
    3603        61065 : }
    3604              : 
    3605              : /* The dependence relation DDR cannot be represented by a distance
    3606              :    vector.  */
    3607              : 
    3608              : static inline void
    3609         2184 : non_affine_dependence_relation (struct data_dependence_relation *ddr)
    3610              : {
    3611         2184 :   if (dump_file && (dump_flags & TDF_DETAILS))
    3612           92 :     fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
    3613              : 
    3614         2184 :   DDR_AFFINE_P (ddr) = false;
    3615         2184 : }
    3616              : 
    3617              : 
    3618              : 
    3619              : /* This section contains the classic Banerjee tests.  */
    3620              : 
    3621              : /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
    3622              :    variables, i.e., if the ZIV (Zero Index Variable) test is true.  */
    3623              : 
    3624              : static inline bool
    3625      2233492 : ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
    3626              : {
    3627      2233492 :   return (evolution_function_is_constant_p (chrec_a)
    3628      2733958 :           && evolution_function_is_constant_p (chrec_b));
    3629              : }
    3630              : 
    3631              : /* Returns true iff CHREC_A and CHREC_B are dependent on an index
    3632              :    variable, i.e., if the SIV (Single Index Variable) test is true.  */
    3633              : 
    3634              : static bool
    3635      1734813 : siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
    3636              : {
    3637      3467842 :   if ((evolution_function_is_constant_p (chrec_a)
    3638         1787 :        && evolution_function_is_univariate_p (chrec_b))
    3639      3467842 :       || (evolution_function_is_constant_p (chrec_b)
    3640         1268 :           && evolution_function_is_univariate_p (chrec_a)))
    3641         3049 :     return true;
    3642              : 
    3643      1731764 :   if (evolution_function_is_univariate_p (chrec_a)
    3644      1731764 :       && evolution_function_is_univariate_p (chrec_b))
    3645              :     {
    3646      1705406 :       switch (TREE_CODE (chrec_a))
    3647              :         {
    3648      1705406 :         case POLYNOMIAL_CHREC:
    3649      1705406 :           switch (TREE_CODE (chrec_b))
    3650              :             {
    3651      1705406 :             case POLYNOMIAL_CHREC:
    3652      1705406 :               if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
    3653              :                 return false;
    3654              :               /* FALLTHRU */
    3655              : 
    3656              :             default:
    3657              :               return true;
    3658              :             }
    3659              : 
    3660              :         default:
    3661              :           return true;
    3662              :         }
    3663              :     }
    3664              : 
    3665              :   return false;
    3666              : }
    3667              : 
    3668              : /* Creates a conflict function with N dimensions.  The affine functions
    3669              :    in each dimension follow.  */
    3670              : 
    3671              : static conflict_function *
    3672      2500856 : conflict_fn (unsigned n, ...)
    3673              : {
    3674      2500856 :   unsigned i;
    3675      2500856 :   conflict_function *ret = XCNEW (conflict_function);
    3676      2500856 :   va_list ap;
    3677              : 
    3678      2500856 :   gcc_assert (n > 0 && n <= MAX_DIM);
    3679      2500856 :   va_start (ap, n);
    3680              : 
    3681      2500856 :   ret->n = n;
    3682      5001712 :   for (i = 0; i < n; i++)
    3683      2500856 :     ret->fns[i] = va_arg (ap, affine_fn);
    3684      2500856 :   va_end (ap);
    3685              : 
    3686      2500856 :   return ret;
    3687              : }
    3688              : 
    3689              : /* Returns constant affine function with value CST.  */
    3690              : 
    3691              : static affine_fn
    3692      2379882 : affine_fn_cst (tree cst)
    3693              : {
    3694      2379882 :   affine_fn fn;
    3695      2379882 :   fn.create (1);
    3696      2379882 :   fn.quick_push (cst);
    3697      2379882 :   return fn;
    3698              : }
    3699              : 
    3700              : /* Returns affine function with single variable, CST + COEF * x_DIM.  */
    3701              : 
    3702              : static affine_fn
    3703       120974 : affine_fn_univar (tree cst, unsigned dim, tree coef)
    3704              : {
    3705       120974 :   affine_fn fn;
    3706       120974 :   fn.create (dim + 1);
    3707       120974 :   unsigned i;
    3708              : 
    3709       120974 :   gcc_assert (dim > 0);
    3710       120974 :   fn.quick_push (cst);
    3711       241948 :   for (i = 1; i < dim; i++)
    3712            0 :     fn.quick_push (integer_zero_node);
    3713       120974 :   fn.quick_push (coef);
    3714       120974 :   return fn;
    3715              : }
    3716              : 
    3717              : /* Analyze a ZIV (Zero Index Variable) subscript.  *OVERLAPS_A and
    3718              :    *OVERLAPS_B are initialized to the functions that describe the
    3719              :    relation between the elements accessed twice by CHREC_A and
    3720              :    CHREC_B.  For k >= 0, the following property is verified:
    3721              : 
    3722              :    CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */
    3723              : 
    3724              : static void
    3725       498679 : analyze_ziv_subscript (tree chrec_a,
    3726              :                        tree chrec_b,
    3727              :                        conflict_function **overlaps_a,
    3728              :                        conflict_function **overlaps_b,
    3729              :                        tree *last_conflicts)
    3730              : {
    3731       498679 :   tree type, difference;
    3732       498679 :   dependence_stats.num_ziv++;
    3733              : 
    3734       498679 :   if (dump_file && (dump_flags & TDF_DETAILS))
    3735        22423 :     fprintf (dump_file, "(analyze_ziv_subscript \n");
    3736              : 
    3737       498679 :   type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
    3738       498679 :   chrec_a = chrec_convert (type, chrec_a, NULL);
    3739       498679 :   chrec_b = chrec_convert (type, chrec_b, NULL);
    3740       498679 :   difference = chrec_fold_minus (type, chrec_a, chrec_b);
    3741              : 
    3742       498679 :   switch (TREE_CODE (difference))
    3743              :     {
    3744       498679 :     case INTEGER_CST:
    3745       498679 :       if (integer_zerop (difference))
    3746              :         {
    3747              :           /* The difference is equal to zero: the accessed index
    3748              :              overlaps for each iteration in the loop.  */
    3749            0 :           *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
    3750            0 :           *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
    3751            0 :           *last_conflicts = chrec_dont_know;
    3752            0 :           dependence_stats.num_ziv_dependent++;
    3753              :         }
    3754              :       else
    3755              :         {
    3756              :           /* The accesses do not overlap.  */
    3757       498679 :           *overlaps_a = conflict_fn_no_dependence ();
    3758       498679 :           *overlaps_b = conflict_fn_no_dependence ();
    3759       498679 :           *last_conflicts = integer_zero_node;
    3760       498679 :           dependence_stats.num_ziv_independent++;
    3761              :         }
    3762              :       break;
    3763              : 
    3764            0 :     default:
    3765              :       /* We're not sure whether the indexes overlap.  For the moment,
    3766              :          conservatively answer "don't know".  */
    3767            0 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3768            0 :         fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
    3769              : 
    3770            0 :       *overlaps_a = conflict_fn_not_known ();
    3771            0 :       *overlaps_b = conflict_fn_not_known ();
    3772            0 :       *last_conflicts = chrec_dont_know;
    3773            0 :       dependence_stats.num_ziv_unimplemented++;
    3774            0 :       break;
    3775              :     }
    3776              : 
    3777       498679 :   if (dump_file && (dump_flags & TDF_DETAILS))
    3778        22423 :     fprintf (dump_file, ")\n");
    3779       498679 : }
    3780              : 
    3781              : /* Similar to max_stmt_executions_int, but returns the bound as a tree,
    3782              :    and only if it fits to the int type.  If this is not the case, or the
    3783              :    bound  on the number of iterations of LOOP could not be derived, returns
    3784              :    chrec_dont_know.  */
    3785              : 
    3786              : static tree
    3787            0 : max_stmt_executions_tree (class loop *loop)
    3788              : {
    3789            0 :   widest_int nit;
    3790              : 
    3791            0 :   if (!max_stmt_executions (loop, &nit))
    3792            0 :     return chrec_dont_know;
    3793              : 
    3794            0 :   if (!wi::fits_to_tree_p (nit, unsigned_type_node))
    3795            0 :     return chrec_dont_know;
    3796              : 
    3797            0 :   return wide_int_to_tree (unsigned_type_node, nit);
    3798            0 : }
    3799              : 
    3800              : /* Determine whether the CHREC is always positive/negative.  If the expression
    3801              :    cannot be statically analyzed, return false, otherwise set the answer into
    3802              :    VALUE.  */
    3803              : 
    3804              : static bool
    3805         4638 : chrec_is_positive (tree chrec, bool *value)
    3806              : {
    3807         4638 :   bool value0, value1, value2;
    3808         4638 :   tree end_value, nb_iter;
    3809              : 
    3810         4638 :   switch (TREE_CODE (chrec))
    3811              :     {
    3812            0 :     case POLYNOMIAL_CHREC:
    3813            0 :       if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
    3814            0 :           || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
    3815            0 :         return false;
    3816              : 
    3817              :       /* FIXME -- overflows.  */
    3818            0 :       if (value0 == value1)
    3819              :         {
    3820            0 :           *value = value0;
    3821            0 :           return true;
    3822              :         }
    3823              : 
    3824              :       /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
    3825              :          and the proof consists in showing that the sign never
    3826              :          changes during the execution of the loop, from 0 to
    3827              :          loop->nb_iterations.  */
    3828            0 :       if (!evolution_function_is_affine_p (chrec))
    3829              :         return false;
    3830              : 
    3831            0 :       nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
    3832            0 :       if (chrec_contains_undetermined (nb_iter))
    3833              :         return false;
    3834              : 
    3835              : #if 0
    3836              :       /* TODO -- If the test is after the exit, we may decrease the number of
    3837              :          iterations by one.  */
    3838              :       if (after_exit)
    3839              :         nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
    3840              : #endif
    3841              : 
    3842            0 :       end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
    3843              : 
    3844            0 :       if (!chrec_is_positive (end_value, &value2))
    3845              :         return false;
    3846              : 
    3847            0 :       *value = value0;
    3848            0 :       return value0 == value1;
    3849              : 
    3850         4638 :     case INTEGER_CST:
    3851         4638 :       switch (tree_int_cst_sgn (chrec))
    3852              :         {
    3853         2078 :         case -1:
    3854         2078 :           *value = false;
    3855         2078 :           break;
    3856         2560 :         case 1:
    3857         2560 :           *value = true;
    3858         2560 :           break;
    3859              :         default:
    3860              :           return false;
    3861              :         }
    3862              :       return true;
    3863              : 
    3864              :     default:
    3865              :       return false;
    3866              :     }
    3867              : }
    3868              : 
    3869              : 
    3870              : /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
    3871              :    constant, and CHREC_B is an affine function.  *OVERLAPS_A and
    3872              :    *OVERLAPS_B are initialized to the functions that describe the
    3873              :    relation between the elements accessed twice by CHREC_A and
    3874              :    CHREC_B.  For k >= 0, the following property is verified:
    3875              : 
    3876              :    CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */
    3877              : 
    3878              : static void
    3879         3049 : analyze_siv_subscript_cst_affine (tree chrec_a,
    3880              :                                   tree chrec_b,
    3881              :                                   conflict_function **overlaps_a,
    3882              :                                   conflict_function **overlaps_b,
    3883              :                                   tree *last_conflicts)
    3884              : {
    3885         3049 :   bool value0, value1, value2;
    3886         3049 :   tree type, difference, tmp;
    3887              : 
    3888         3049 :   type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
    3889         3049 :   chrec_a = chrec_convert (type, chrec_a, NULL);
    3890         3049 :   chrec_b = chrec_convert (type, chrec_b, NULL);
    3891         3049 :   difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
    3892              : 
    3893              :   /* Special case overlap in the first iteration.  */
    3894         3049 :   if (integer_zerop (difference))
    3895              :     {
    3896          728 :       *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
    3897          728 :       *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
    3898          728 :       *last_conflicts = integer_one_node;
    3899          728 :       return;
    3900              :     }
    3901              : 
    3902         2321 :   if (!chrec_is_positive (initial_condition (difference), &value0))
    3903              :     {
    3904            0 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3905            0 :         fprintf (dump_file, "siv test failed: chrec is not positive.\n");
    3906              : 
    3907            0 :       dependence_stats.num_siv_unimplemented++;
    3908            0 :       *overlaps_a = conflict_fn_not_known ();
    3909            0 :       *overlaps_b = conflict_fn_not_known ();
    3910            0 :       *last_conflicts = chrec_dont_know;
    3911            0 :       return;
    3912              :     }
    3913              :   else
    3914              :     {
    3915         2321 :       if (value0 == false)
    3916              :         {
    3917         1864 :           if (TREE_CODE (chrec_b) != POLYNOMIAL_CHREC
    3918         1864 :               || !chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
    3919              :             {
    3920            4 :               if (dump_file && (dump_flags & TDF_DETAILS))
    3921            0 :                 fprintf (dump_file, "siv test failed: chrec not positive.\n");
    3922              : 
    3923            4 :               *overlaps_a = conflict_fn_not_known ();
    3924            4 :               *overlaps_b = conflict_fn_not_known ();
    3925            4 :               *last_conflicts = chrec_dont_know;
    3926            4 :               dependence_stats.num_siv_unimplemented++;
    3927            4 :               return;
    3928              :             }
    3929              :           else
    3930              :             {
    3931         1860 :               if (value1 == true)
    3932              :                 {
    3933              :                   /* Example:
    3934              :                      chrec_a = 12
    3935              :                      chrec_b = {10, +, 1}
    3936              :                   */
    3937              : 
    3938         1860 :                   if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
    3939              :                     {
    3940         1563 :                       HOST_WIDE_INT numiter;
    3941         1563 :                       class loop *loop = get_chrec_loop (chrec_b);
    3942              : 
    3943         1563 :                       *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
    3944         1563 :                       tmp = fold_build2 (EXACT_DIV_EXPR, type,
    3945              :                                          fold_build1 (ABS_EXPR, type, difference),
    3946              :                                          CHREC_RIGHT (chrec_b));
    3947         1563 :                       *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
    3948         1563 :                       *last_conflicts = integer_one_node;
    3949              : 
    3950              : 
    3951              :                       /* Perform weak-zero siv test to see if overlap is
    3952              :                          outside the loop bounds.  */
    3953         1563 :                       numiter = max_stmt_executions_int (loop);
    3954              : 
    3955         1563 :                       if (numiter >= 0
    3956         1563 :                           && compare_tree_int (tmp, numiter) > 0)
    3957              :                         {
    3958            0 :                           free_conflict_function (*overlaps_a);
    3959            0 :                           free_conflict_function (*overlaps_b);
    3960            0 :                           *overlaps_a = conflict_fn_no_dependence ();
    3961            0 :                           *overlaps_b = conflict_fn_no_dependence ();
    3962            0 :                           *last_conflicts = integer_zero_node;
    3963            0 :                           dependence_stats.num_siv_independent++;
    3964            0 :                           return;
    3965              :                         }
    3966         1563 :                       dependence_stats.num_siv_dependent++;
    3967         1563 :                       return;
    3968              :                     }
    3969              : 
    3970              :                   /* When the step does not divide the difference, there are
    3971              :                      no overlaps.  */
    3972              :                   else
    3973              :                     {
    3974          297 :                       *overlaps_a = conflict_fn_no_dependence ();
    3975          297 :                       *overlaps_b = conflict_fn_no_dependence ();
    3976          297 :                       *last_conflicts = integer_zero_node;
    3977          297 :                       dependence_stats.num_siv_independent++;
    3978          297 :                       return;
    3979              :                     }
    3980              :                 }
    3981              : 
    3982              :               else
    3983              :                 {
    3984              :                   /* Example:
    3985              :                      chrec_a = 12
    3986              :                      chrec_b = {10, +, -1}
    3987              : 
    3988              :                      In this case, chrec_a will not overlap with chrec_b.  */
    3989            0 :                   *overlaps_a = conflict_fn_no_dependence ();
    3990            0 :                   *overlaps_b = conflict_fn_no_dependence ();
    3991            0 :                   *last_conflicts = integer_zero_node;
    3992            0 :                   dependence_stats.num_siv_independent++;
    3993            0 :                   return;
    3994              :                 }
    3995              :             }
    3996              :         }
    3997              :       else
    3998              :         {
    3999          457 :           if (TREE_CODE (chrec_b) != POLYNOMIAL_CHREC
    4000          457 :               || !chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
    4001              :             {
    4002            0 :               if (dump_file && (dump_flags & TDF_DETAILS))
    4003            0 :                 fprintf (dump_file, "siv test failed: chrec not positive.\n");
    4004              : 
    4005            0 :               *overlaps_a = conflict_fn_not_known ();
    4006            0 :               *overlaps_b = conflict_fn_not_known ();
    4007            0 :               *last_conflicts = chrec_dont_know;
    4008            0 :               dependence_stats.num_siv_unimplemented++;
    4009            0 :               return;
    4010              :             }
    4011              :           else
    4012              :             {
    4013          457 :               if (value2 == false)
    4014              :                 {
    4015              :                   /* Example:
    4016              :                      chrec_a = 3
    4017              :                      chrec_b = {10, +, -1}
    4018              :                   */
    4019          214 :                   if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
    4020              :                     {
    4021          109 :                       HOST_WIDE_INT numiter;
    4022          109 :                       class loop *loop = get_chrec_loop (chrec_b);
    4023              : 
    4024          109 :                       *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
    4025          109 :                       tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
    4026              :                                          CHREC_RIGHT (chrec_b));
    4027          109 :                       *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
    4028          109 :                       *last_conflicts = integer_one_node;
    4029              : 
    4030              :                       /* Perform weak-zero siv test to see if overlap is
    4031              :                          outside the loop bounds.  */
    4032          109 :                       numiter = max_stmt_executions_int (loop);
    4033              : 
    4034          109 :                       if (numiter >= 0
    4035          109 :                           && compare_tree_int (tmp, numiter) > 0)
    4036              :                         {
    4037            0 :                           free_conflict_function (*overlaps_a);
    4038            0 :                           free_conflict_function (*overlaps_b);
    4039            0 :                           *overlaps_a = conflict_fn_no_dependence ();
    4040            0 :                           *overlaps_b = conflict_fn_no_dependence ();
    4041            0 :                           *last_conflicts = integer_zero_node;
    4042            0 :                           dependence_stats.num_siv_independent++;
    4043            0 :                           return;
    4044              :                         }
    4045          109 :                       dependence_stats.num_siv_dependent++;
    4046          109 :                       return;
    4047              :                     }
    4048              : 
    4049              :                   /* When the step does not divide the difference, there
    4050              :                      are no overlaps.  */
    4051              :                   else
    4052              :                     {
    4053          105 :                       *overlaps_a = conflict_fn_no_dependence ();
    4054          105 :                       *overlaps_b = conflict_fn_no_dependence ();
    4055          105 :                       *last_conflicts = integer_zero_node;
    4056          105 :                       dependence_stats.num_siv_independent++;
    4057          105 :                       return;
    4058              :                     }
    4059              :                 }
    4060              :               else
    4061              :                 {
    4062              :                   /* Example:
    4063              :                      chrec_a = 3
    4064              :                      chrec_b = {4, +, 1}
    4065              : 
    4066              :                      In this case, chrec_a will not overlap with chrec_b.  */
    4067          243 :                   *overlaps_a = conflict_fn_no_dependence ();
    4068          243 :                   *overlaps_b = conflict_fn_no_dependence ();
    4069          243 :                   *last_conflicts = integer_zero_node;
    4070          243 :                   dependence_stats.num_siv_independent++;
    4071          243 :                   return;
    4072              :                 }
    4073              :             }
    4074              :         }
    4075              :     }
    4076              : }
    4077              : 
    4078              : /* Helper recursive function for initializing the matrix A.  Returns
    4079              :    the initial value of CHREC.  */
    4080              : 
    4081              : static tree
    4082      3370130 : initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
    4083              : {
    4084      6740252 :   gcc_assert (chrec);
    4085              : 
    4086      6740252 :   switch (TREE_CODE (chrec))
    4087              :     {
    4088      3370130 :     case POLYNOMIAL_CHREC:
    4089      3370130 :       HOST_WIDE_INT chrec_right;
    4090      3370130 :       if (!cst_and_fits_in_hwi (CHREC_RIGHT (chrec)))
    4091            8 :         return chrec_dont_know;
    4092      3370122 :       chrec_right = int_cst_value (CHREC_RIGHT (chrec));
    4093              :       /* We want to be able to negate without overflow.  */
    4094      3370122 :       if (chrec_right == HOST_WIDE_INT_MIN)
    4095            0 :         return chrec_dont_know;
    4096      3370122 :       A[index][0] = mult * chrec_right;
    4097      3370122 :       return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
    4098              : 
    4099            0 :     case PLUS_EXPR:
    4100            0 :     case MULT_EXPR:
    4101            0 :     case MINUS_EXPR:
    4102            0 :       {
    4103            0 :         tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
    4104            0 :         tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
    4105              : 
    4106            0 :         return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
    4107              :       }
    4108              : 
    4109            0 :     CASE_CONVERT:
    4110            0 :       {
    4111            0 :         tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
    4112            0 :         return chrec_convert (chrec_type (chrec), op, NULL);
    4113              :       }
    4114              : 
    4115            0 :     case BIT_NOT_EXPR:
    4116            0 :       {
    4117              :         /* Handle ~X as -1 - X.  */
    4118            0 :         tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
    4119            0 :         return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
    4120            0 :                               build_int_cst (TREE_TYPE (chrec), -1), op);
    4121              :       }
    4122              : 
    4123      3370122 :     case INTEGER_CST:
    4124      3370122 :       return cst_and_fits_in_hwi (chrec) ? chrec : chrec_dont_know;
    4125              : 
    4126            0 :     default:
    4127            0 :       gcc_unreachable ();
    4128              :       return NULL_TREE;
    4129              :     }
    4130              : }
    4131              : 
    4132              : #define FLOOR_DIV(x,y) ((x) / (y))
    4133              : 
    4134              : /* Solves the special case of the Diophantine equation:
    4135              :    | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
    4136              : 
    4137              :    Computes the descriptions OVERLAPS_A and OVERLAPS_B.  NITER is the
    4138              :    number of iterations that loops X and Y run.  The overlaps will be
    4139              :    constructed as evolutions in dimension DIM.  */
    4140              : 
    4141              : static void
    4142           64 : compute_overlap_steps_for_affine_univar (HOST_WIDE_INT niter,
    4143              :                                          HOST_WIDE_INT step_a,
    4144              :                                          HOST_WIDE_INT step_b,
    4145              :                                          affine_fn *overlaps_a,
    4146              :                                          affine_fn *overlaps_b,
    4147              :                                          tree *last_conflicts, int dim)
    4148              : {
    4149           64 :   if (((step_a > 0 && step_b > 0)
    4150            8 :        || (step_a < 0 && step_b < 0)))
    4151              :     {
    4152           60 :       HOST_WIDE_INT step_overlaps_a, step_overlaps_b;
    4153           60 :       HOST_WIDE_INT gcd_steps_a_b, last_conflict, tau2;
    4154              : 
    4155           60 :       gcd_steps_a_b = gcd (step_a, step_b);
    4156           60 :       step_overlaps_a = step_b / gcd_steps_a_b;
    4157           60 :       step_overlaps_b = step_a / gcd_steps_a_b;
    4158              : 
    4159           60 :       if (niter > 0)
    4160              :         {
    4161           60 :           tau2 = FLOOR_DIV (niter, step_overlaps_a);
    4162           60 :           tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
    4163           60 :           last_conflict = tau2;
    4164           60 :           *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
    4165              :         }
    4166              :       else
    4167            0 :         *last_conflicts = chrec_dont_know;
    4168              : 
    4169           60 :       *overlaps_a = affine_fn_univar (integer_zero_node, dim,
    4170              :                                       build_int_cst (NULL_TREE,
    4171           60 :                                                      step_overlaps_a));
    4172           60 :       *overlaps_b = affine_fn_univar (integer_zero_node, dim,
    4173              :                                       build_int_cst (NULL_TREE,
    4174           60 :                                                      step_overlaps_b));
    4175           60 :     }
    4176              : 
    4177              :   else
    4178              :     {
    4179            4 :       *overlaps_a = affine_fn_cst (integer_zero_node);
    4180            4 :       *overlaps_b = affine_fn_cst (integer_zero_node);
    4181            4 :       *last_conflicts = integer_zero_node;
    4182              :     }
    4183           64 : }
    4184              : 
    4185              : /* Solves the special case of a Diophantine equation where CHREC_A is
    4186              :    an affine bivariate function, and CHREC_B is an affine univariate
    4187              :    function.  For example,
    4188              : 
    4189              :    | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
    4190              : 
    4191              :    has the following overlapping functions:
    4192              : 
    4193              :    | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
    4194              :    | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
    4195              :    | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
    4196              : 
    4197              :    FORNOW: This is a specialized implementation for a case occurring in
    4198              :    a common benchmark.  Implement the general algorithm.  */
    4199              : 
    4200              : static void
    4201            0 : compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
    4202              :                                       conflict_function **overlaps_a,
    4203              :                                       conflict_function **overlaps_b,
    4204              :                                       tree *last_conflicts)
    4205              : {
    4206            0 :   bool xz_p, yz_p, xyz_p;
    4207            0 :   HOST_WIDE_INT step_x, step_y, step_z;
    4208            0 :   HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
    4209            0 :   affine_fn overlaps_a_xz, overlaps_b_xz;
    4210            0 :   affine_fn overlaps_a_yz, overlaps_b_yz;
    4211            0 :   affine_fn overlaps_a_xyz, overlaps_b_xyz;
    4212            0 :   affine_fn ova1, ova2, ovb;
    4213            0 :   tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
    4214              : 
    4215            0 :   step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
    4216            0 :   step_y = int_cst_value (CHREC_RIGHT (chrec_a));
    4217            0 :   step_z = int_cst_value (CHREC_RIGHT (chrec_b));
    4218              : 
    4219            0 :   niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
    4220            0 :   niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
    4221            0 :   niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
    4222              : 
    4223            0 :   if (niter_x < 0 || niter_y < 0 || niter_z < 0)
    4224              :     {
    4225            0 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4226            0 :         fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
    4227              : 
    4228            0 :       *overlaps_a = conflict_fn_not_known ();
    4229            0 :       *overlaps_b = conflict_fn_not_known ();
    4230            0 :       *last_conflicts = chrec_dont_know;
    4231            0 :       return;
    4232              :     }
    4233              : 
    4234            0 :   niter = MIN (niter_x, niter_z);
    4235            0 :   compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
    4236              :                                            &overlaps_a_xz,
    4237              :                                            &overlaps_b_xz,
    4238              :                                            &last_conflicts_xz, 1);
    4239            0 :   niter = MIN (niter_y, niter_z);
    4240            0 :   compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
    4241              :                                            &overlaps_a_yz,
    4242              :                                            &overlaps_b_yz,
    4243              :                                            &last_conflicts_yz, 2);
    4244            0 :   niter = MIN (niter_x, niter_z);
    4245            0 :   niter = MIN (niter_y, niter);
    4246            0 :   compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
    4247              :                                            &overlaps_a_xyz,
    4248              :                                            &overlaps_b_xyz,
    4249              :                                            &last_conflicts_xyz, 3);
    4250              : 
    4251            0 :   xz_p = !integer_zerop (last_conflicts_xz);
    4252            0 :   yz_p = !integer_zerop (last_conflicts_yz);
    4253            0 :   xyz_p = !integer_zerop (last_conflicts_xyz);
    4254              : 
    4255            0 :   if (xz_p || yz_p || xyz_p)
    4256              :     {
    4257            0 :       ova1 = affine_fn_cst (integer_zero_node);
    4258            0 :       ova2 = affine_fn_cst (integer_zero_node);
    4259            0 :       ovb = affine_fn_cst (integer_zero_node);
    4260            0 :       if (xz_p)
    4261              :         {
    4262            0 :           affine_fn t0 = ova1;
    4263            0 :           affine_fn t2 = ovb;
    4264              : 
    4265            0 :           ova1 = affine_fn_plus (ova1, overlaps_a_xz);
    4266            0 :           ovb = affine_fn_plus (ovb, overlaps_b_xz);
    4267            0 :           affine_fn_free (t0);
    4268            0 :           affine_fn_free (t2);
    4269            0 :           *last_conflicts = last_conflicts_xz;
    4270              :         }
    4271            0 :       if (yz_p)
    4272              :         {
    4273            0 :           affine_fn t0 = ova2;
    4274            0 :           affine_fn t2 = ovb;
    4275              : 
    4276            0 :           ova2 = affine_fn_plus (ova2, overlaps_a_yz);
    4277            0 :           ovb = affine_fn_plus (ovb, overlaps_b_yz);
    4278            0 :           affine_fn_free (t0);
    4279            0 :           affine_fn_free (t2);
    4280            0 :           *last_conflicts = last_conflicts_yz;
    4281              :         }
    4282            0 :       if (xyz_p)
    4283              :         {
    4284            0 :           affine_fn t0 = ova1;
    4285            0 :           affine_fn t2 = ova2;
    4286            0 :           affine_fn t4 = ovb;
    4287              : 
    4288            0 :           ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
    4289            0 :           ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
    4290            0 :           ovb = affine_fn_plus (ovb, overlaps_b_xyz);
    4291            0 :           affine_fn_free (t0);
    4292            0 :           affine_fn_free (t2);
    4293            0 :           affine_fn_free (t4);
    4294            0 :           *last_conflicts = last_conflicts_xyz;
    4295              :         }
    4296            0 :       *overlaps_a = conflict_fn (2, ova1, ova2);
    4297            0 :       *overlaps_b = conflict_fn (1, ovb);
    4298            0 :     }
    4299              :   else
    4300              :     {
    4301            0 :       *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
    4302            0 :       *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
    4303            0 :       *last_conflicts = integer_zero_node;
    4304              :     }
    4305              : 
    4306            0 :   affine_fn_free (overlaps_a_xz);
    4307            0 :   affine_fn_free (overlaps_b_xz);
    4308            0 :   affine_fn_free (overlaps_a_yz);
    4309            0 :   affine_fn_free (overlaps_b_yz);
    4310            0 :   affine_fn_free (overlaps_a_xyz);
    4311            0 :   affine_fn_free (overlaps_b_xyz);
    4312              : }
    4313              : 
    4314              : /* Copy the elements of vector VEC1 with length SIZE to VEC2.  */
    4315              : 
    4316              : static void
    4317      3416105 : lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
    4318              :                     int size)
    4319              : {
    4320      3416105 :   memcpy (vec2, vec1, size * sizeof (*vec1));
    4321            0 : }
    4322              : 
    4323              : /* Copy the elements of M x N matrix MAT1 to MAT2.  */
    4324              : 
    4325              : static void
    4326      1684989 : lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
    4327              :                     int m, int n)
    4328              : {
    4329      1684989 :   int i;
    4330              : 
    4331      5054967 :   for (i = 0; i < m; i++)
    4332      3369978 :     lambda_vector_copy (mat1[i], mat2[i], n);
    4333      1684989 : }
    4334              : 
    4335              : /* Store the N x N identity matrix in MAT.  */
    4336              : 
    4337              : static void
    4338      1684989 : lambda_matrix_id (lambda_matrix mat, int size)
    4339              : {
    4340      1684989 :   int i, j;
    4341              : 
    4342      5054967 :   for (i = 0; i < size; i++)
    4343     10109934 :     for (j = 0; j < size; j++)
    4344     10109934 :       mat[i][j] = (i == j) ? 1 : 0;
    4345      1684989 : }
    4346              : 
    4347              : /* Return the index of the first nonzero element of vector VEC1 between
    4348              :    START and N.  We must have START <= N.
    4349              :    Returns N if VEC1 is the zero vector.  */
    4350              : 
    4351              : static int
    4352      1684989 : lambda_vector_first_nz (lambda_vector vec1, int n, int start)
    4353              : {
    4354      1684989 :   int j = start;
    4355      1684989 :   while (j < n && vec1[j] == 0)
    4356            0 :     j++;
    4357      1684989 :   return j;
    4358              : }
    4359              : 
    4360              : /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
    4361              :    R2 = R2 + CONST1 * R1.  */
    4362              : 
    4363              : static bool
    4364      3370248 : lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2,
    4365              :                        lambda_int const1)
    4366              : {
    4367      3370248 :   int i;
    4368              : 
    4369      3370248 :   if (const1 == 0)
    4370              :     return true;
    4371              : 
    4372      8425035 :   for (i = 0; i < n; i++)
    4373              :     {
    4374      5055021 :       bool ovf;
    4375      5055021 :       lambda_int tem = mul_hwi (mat[r1][i], const1, &ovf);
    4376      5055021 :       if (ovf)
    4377      3370248 :         return false;
    4378      5055021 :       lambda_int tem2 = add_hwi (mat[r2][i], tem, &ovf);
    4379      5055021 :       if (ovf || tem2 == HOST_WIDE_INT_MIN)
    4380              :         return false;
    4381      5055021 :       mat[r2][i] = tem2;
    4382              :     }
    4383              : 
    4384              :   return true;
    4385              : }
    4386              : 
    4387              : /* Multiply vector VEC1 of length SIZE by a constant CONST1,
    4388              :    and store the result in VEC2.  */
    4389              : 
    4390              : static void
    4391      1672096 : lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
    4392              :                           int size, lambda_int const1)
    4393              : {
    4394      1672096 :   int i;
    4395              : 
    4396      1672096 :   if (const1 == 0)
    4397            0 :     lambda_vector_clear (vec2, size);
    4398              :   else
    4399      5016288 :     for (i = 0; i < size; i++)
    4400      3344192 :       vec2[i] = const1 * vec1[i];
    4401      1672096 : }
    4402              : 
    4403              : /* Negate vector VEC1 with length SIZE and store it in VEC2.  */
    4404              : 
    4405              : static void
    4406      1672096 : lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
    4407              :                       int size)
    4408              : {
    4409            0 :   lambda_vector_mult_const (vec1, vec2, size, -1);
    4410            0 : }
    4411              : 
    4412              : /* Negate row R1 of matrix MAT which has N columns.  */
    4413              : 
    4414              : static void
    4415      1672096 : lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
    4416              : {
    4417            0 :   lambda_vector_negate (mat[r1], mat[r1], n);
    4418      1672096 : }
    4419              : 
    4420              : /* Return true if two vectors are equal.  */
    4421              : 
    4422              : static bool
    4423       360664 : lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
    4424              : {
    4425       360664 :   int i;
    4426       361787 :   for (i = 0; i < size; i++)
    4427       361520 :     if (vec1[i] != vec2[i])
    4428              :       return false;
    4429              :   return true;
    4430              : }
    4431              : 
    4432              : /* Given an M x N integer matrix A, this function determines an M x
    4433              :    M unimodular matrix U, and an M x N echelon matrix S such that
    4434              :    "U.A = S".  This decomposition is also known as "right Hermite".
    4435              : 
    4436              :    Ref: Algorithm 2.1 page 33 in "Loop Transformations for
    4437              :    Restructuring Compilers" Utpal Banerjee.  */
    4438              : 
    4439              : static bool
    4440      1684989 : lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
    4441              :                              lambda_matrix S, lambda_matrix U)
    4442              : {
    4443      1684989 :   int i, j, i0 = 0;
    4444              : 
    4445      1684989 :   lambda_matrix_copy (A, S, m, n);
    4446      1684989 :   lambda_matrix_id (U, m);
    4447              : 
    4448      3369978 :   for (j = 0; j < n; j++)
    4449              :     {
    4450      3369978 :       if (lambda_vector_first_nz (S[j], m, i0) < m)
    4451              :         {
    4452      1684989 :           ++i0;
    4453      3369978 :           for (i = m - 1; i >= i0; i--)
    4454              :             {
    4455      3370113 :               while (S[i][j] != 0)
    4456              :                 {
    4457      1685124 :                   lambda_int factor, a, b;
    4458              : 
    4459      1685124 :                   a = S[i-1][j];
    4460      1685124 :                   b = S[i][j];
    4461      1685124 :                   gcc_assert (a != HOST_WIDE_INT_MIN);
    4462      1685124 :                   factor = a / b;
    4463              : 
    4464      1685124 :                   if (!lambda_matrix_row_add (S, n, i, i-1, -factor))
    4465              :                     return false;
    4466      1685124 :                   std::swap (S[i], S[i-1]);
    4467              : 
    4468      1685124 :                   if (!lambda_matrix_row_add (U, m, i, i-1, -factor))
    4469              :                     return false;
    4470      1685124 :                   std::swap (U[i], U[i-1]);
    4471              :                 }
    4472              :             }
    4473              :         }
    4474              :     }
    4475              : 
    4476              :   return true;
    4477              : }
    4478              : 
    4479              : /* Determines the overlapping elements due to accesses CHREC_A and
    4480              :    CHREC_B, that are affine functions.  This function cannot handle
    4481              :    symbolic evolution functions, ie. when initial conditions are
    4482              :    parameters, because it uses lambda matrices of integers.  */
    4483              : 
    4484              : static void
    4485      1685065 : analyze_subscript_affine_affine (tree chrec_a,
    4486              :                                  tree chrec_b,
    4487              :                                  conflict_function **overlaps_a,
    4488              :                                  conflict_function **overlaps_b,
    4489              :                                  tree *last_conflicts)
    4490              : {
    4491      1685065 :   unsigned nb_vars_a, nb_vars_b, dim;
    4492      1685065 :   lambda_int gamma, gcd_alpha_beta;
    4493      1685065 :   lambda_matrix A, U, S;
    4494      1685065 :   struct obstack scratch_obstack;
    4495              : 
    4496      1685065 :   if (eq_evolutions_p (chrec_a, chrec_b))
    4497              :     {
    4498              :       /* The accessed index overlaps for each iteration in the
    4499              :          loop.  */
    4500            0 :       *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
    4501            0 :       *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
    4502            0 :       *last_conflicts = chrec_dont_know;
    4503            0 :       return;
    4504              :     }
    4505      1685065 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4506        20014 :     fprintf (dump_file, "(analyze_subscript_affine_affine \n");
    4507              : 
    4508              :   /* For determining the initial intersection, we have to solve a
    4509              :      Diophantine equation.  This is the most time consuming part.
    4510              : 
    4511              :      For answering to the question: "Is there a dependence?" we have
    4512              :      to prove that there exists a solution to the Diophantine
    4513              :      equation, and that the solution is in the iteration domain,
    4514              :      i.e. the solution is positive or zero, and that the solution
    4515              :      happens before the upper bound loop.nb_iterations.  Otherwise
    4516              :      there is no dependence.  This function outputs a description of
    4517              :      the iterations that hold the intersections.  */
    4518              : 
    4519      1685065 :   nb_vars_a = nb_vars_in_chrec (chrec_a);
    4520      1685065 :   nb_vars_b = nb_vars_in_chrec (chrec_b);
    4521              : 
    4522      1685065 :   gcc_obstack_init (&scratch_obstack);
    4523              : 
    4524      1685065 :   dim = nb_vars_a + nb_vars_b;
    4525      1685065 :   U = lambda_matrix_new (dim, dim, &scratch_obstack);
    4526      1685065 :   A = lambda_matrix_new (dim, 1, &scratch_obstack);
    4527      1685065 :   S = lambda_matrix_new (dim, 1, &scratch_obstack);
    4528              : 
    4529      1685065 :   tree init_a = initialize_matrix_A (A, chrec_a, 0, 1);
    4530      1685065 :   tree init_b = initialize_matrix_A (A, chrec_b, nb_vars_a, -1);
    4531      1685065 :   if (init_a == chrec_dont_know
    4532      1685053 :       || init_b == chrec_dont_know)
    4533              :     {
    4534           12 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4535            0 :         fprintf (dump_file, "affine-affine test failed: "
    4536              :                  "representation issue.\n");
    4537           12 :       *overlaps_a = conflict_fn_not_known ();
    4538           12 :       *overlaps_b = conflict_fn_not_known ();
    4539           12 :       *last_conflicts = chrec_dont_know;
    4540           12 :       goto end_analyze_subs_aa;
    4541              :     }
    4542      1685053 :   gamma = int_cst_value (init_b) - int_cst_value (init_a);
    4543              : 
    4544              :   /* Don't do all the hard work of solving the Diophantine equation
    4545              :      when we already know the solution: for example,
    4546              :      | {3, +, 1}_1
    4547              :      | {3, +, 4}_2
    4548              :      | gamma = 3 - 3 = 0.
    4549              :      Then the first overlap occurs during the first iterations:
    4550              :      | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
    4551              :   */
    4552      1685053 :   if (gamma == 0)
    4553              :     {
    4554           64 :       if (nb_vars_a == 1 && nb_vars_b == 1)
    4555              :         {
    4556           64 :           HOST_WIDE_INT step_a, step_b;
    4557           64 :           HOST_WIDE_INT niter, niter_a, niter_b;
    4558           64 :           affine_fn ova, ovb;
    4559              : 
    4560           64 :           niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
    4561           64 :           niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
    4562           64 :           niter = MIN (niter_a, niter_b);
    4563           64 :           step_a = int_cst_value (CHREC_RIGHT (chrec_a));
    4564           64 :           step_b = int_cst_value (CHREC_RIGHT (chrec_b));
    4565              : 
    4566           64 :           compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
    4567              :                                                    &ova, &ovb,
    4568              :                                                    last_conflicts, 1);
    4569           64 :           *overlaps_a = conflict_fn (1, ova);
    4570           64 :           *overlaps_b = conflict_fn (1, ovb);
    4571              :         }
    4572              : 
    4573            0 :       else if (nb_vars_a == 2 && nb_vars_b == 1)
    4574            0 :         compute_overlap_steps_for_affine_1_2
    4575            0 :           (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
    4576              : 
    4577            0 :       else if (nb_vars_a == 1 && nb_vars_b == 2)
    4578            0 :         compute_overlap_steps_for_affine_1_2
    4579            0 :           (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
    4580              : 
    4581              :       else
    4582              :         {
    4583            0 :           if (dump_file && (dump_flags & TDF_DETAILS))
    4584            0 :             fprintf (dump_file, "affine-affine test failed: too many variables.\n");
    4585            0 :           *overlaps_a = conflict_fn_not_known ();
    4586            0 :           *overlaps_b = conflict_fn_not_known ();
    4587            0 :           *last_conflicts = chrec_dont_know;
    4588              :         }
    4589           64 :       goto end_analyze_subs_aa;
    4590              :     }
    4591              : 
    4592              :   /* U.A = S */
    4593      1684989 :   if (!lambda_matrix_right_hermite (A, dim, 1, S, U))
    4594              :     {
    4595            0 :       *overlaps_a = conflict_fn_not_known ();
    4596            0 :       *overlaps_b = conflict_fn_not_known ();
    4597            0 :       *last_conflicts = chrec_dont_know;
    4598            0 :       goto end_analyze_subs_aa;
    4599              :     }
    4600              : 
    4601      1684989 :   if (S[0][0] < 0)
    4602              :     {
    4603      1672096 :       S[0][0] *= -1;
    4604      1672096 :       lambda_matrix_row_negate (U, dim, 0);
    4605              :     }
    4606      1684989 :   gcd_alpha_beta = S[0][0];
    4607              : 
    4608              :   /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
    4609              :      but that is a quite strange case.  Instead of ICEing, answer
    4610              :      don't know.  */
    4611      1684989 :   if (gcd_alpha_beta == 0)
    4612              :     {
    4613            0 :       *overlaps_a = conflict_fn_not_known ();
    4614            0 :       *overlaps_b = conflict_fn_not_known ();
    4615            0 :       *last_conflicts = chrec_dont_know;
    4616            0 :       goto end_analyze_subs_aa;
    4617              :     }
    4618              : 
    4619              :   /* The classic "gcd-test".  */
    4620      1684989 :   if (!int_divides_p (gcd_alpha_beta, gamma))
    4621              :     {
    4622              :       /* The "gcd-test" has determined that there is no integer
    4623              :          solution, i.e. there is no dependence.  */
    4624      1568809 :       *overlaps_a = conflict_fn_no_dependence ();
    4625      1568809 :       *overlaps_b = conflict_fn_no_dependence ();
    4626      1568809 :       *last_conflicts = integer_zero_node;
    4627              :     }
    4628              : 
    4629              :   /* Both access functions are univariate.  This includes SIV and MIV cases.  */
    4630       116180 :   else if (nb_vars_a == 1 && nb_vars_b == 1)
    4631              :     {
    4632              :       /* Both functions should have the same evolution sign.  */
    4633       116180 :       if (((A[0][0] > 0 && -A[1][0] > 0)
    4634         8754 :            || (A[0][0] < 0 && -A[1][0] < 0)))
    4635              :         {
    4636              :           /* The solutions are given by:
    4637              :              |
    4638              :              | [GAMMA/GCD_ALPHA_BETA  t].[u11 u12]  = [x0]
    4639              :              |                           [u21 u22]    [y0]
    4640              : 
    4641              :              For a given integer t.  Using the following variables,
    4642              : 
    4643              :              | i0 = u11 * gamma / gcd_alpha_beta
    4644              :              | j0 = u12 * gamma / gcd_alpha_beta
    4645              :              | i1 = u21
    4646              :              | j1 = u22
    4647              : 
    4648              :              the solutions are:
    4649              : 
    4650              :              | x0 = i0 + i1 * t,
    4651              :              | y0 = j0 + j1 * t.  */
    4652       115786 :           HOST_WIDE_INT i0, j0, i1, j1;
    4653              : 
    4654       115786 :           i0 = U[0][0] * gamma / gcd_alpha_beta;
    4655       115786 :           j0 = U[0][1] * gamma / gcd_alpha_beta;
    4656       115786 :           i1 = U[1][0];
    4657       115786 :           j1 = U[1][1];
    4658              : 
    4659       115786 :           if ((i1 == 0 && i0 < 0)
    4660       115786 :               || (j1 == 0 && j0 < 0))
    4661              :             {
    4662              :               /* There is no solution.
    4663              :                  FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
    4664              :                  falls in here, but for the moment we don't look at the
    4665              :                  upper bound of the iteration domain.  */
    4666            0 :               *overlaps_a = conflict_fn_no_dependence ();
    4667            0 :               *overlaps_b = conflict_fn_no_dependence ();
    4668            0 :               *last_conflicts = integer_zero_node;
    4669        55359 :               goto end_analyze_subs_aa;
    4670              :             }
    4671              : 
    4672       115786 :           if (i1 > 0 && j1 > 0)
    4673              :             {
    4674       115786 :               HOST_WIDE_INT niter_a
    4675       115786 :                 = max_stmt_executions_int (get_chrec_loop (chrec_a));
    4676       115786 :               HOST_WIDE_INT niter_b
    4677       115786 :                 = max_stmt_executions_int (get_chrec_loop (chrec_b));
    4678       115786 :               HOST_WIDE_INT niter = MIN (niter_a, niter_b);
    4679              : 
    4680              :               /* (X0, Y0) is a solution of the Diophantine equation:
    4681              :                  "chrec_a (X0) = chrec_b (Y0)".  */
    4682       115786 :               HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
    4683              :                                         CEIL (-j0, j1));
    4684       115786 :               HOST_WIDE_INT x0 = i1 * tau1 + i0;
    4685       115786 :               HOST_WIDE_INT y0 = j1 * tau1 + j0;
    4686              : 
    4687              :               /* (X1, Y1) is the smallest positive solution of the eq
    4688              :                  "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
    4689              :                  first conflict occurs.  */
    4690       115786 :               HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
    4691       115786 :               HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
    4692       115786 :               HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
    4693              : 
    4694       115786 :               if (niter > 0)
    4695              :                 {
    4696              :                   /* If the overlap occurs outside of the bounds of the
    4697              :                      loop, there is no dependence.  */
    4698       106349 :                   if (x1 >= niter_a || y1 >= niter_b)
    4699              :                     {
    4700        55359 :                       *overlaps_a = conflict_fn_no_dependence ();
    4701        55359 :                       *overlaps_b = conflict_fn_no_dependence ();
    4702        55359 :                       *last_conflicts = integer_zero_node;
    4703        55359 :                       goto end_analyze_subs_aa;
    4704              :                     }
    4705              : 
    4706              :                   /* max stmt executions can get quite large, avoid
    4707              :                      overflows by using wide ints here.  */
    4708        50990 :                   widest_int tau2
    4709       101980 :                     = wi::smin (wi::sdiv_floor (wi::sub (niter_a, i0), i1),
    4710       152970 :                                 wi::sdiv_floor (wi::sub (niter_b, j0), j1));
    4711        50990 :                   widest_int last_conflict = wi::sub (tau2, (x1 - i0)/i1);
    4712        50990 :                   if (wi::min_precision (last_conflict, SIGNED)
    4713        50990 :                       <= TYPE_PRECISION (integer_type_node))
    4714        45975 :                     *last_conflicts
    4715        45975 :                        = build_int_cst (integer_type_node,
    4716        45975 :                                         last_conflict.to_shwi ());
    4717              :                   else
    4718         5015 :                     *last_conflicts = chrec_dont_know;
    4719        50990 :                 }
    4720              :               else
    4721         9437 :                 *last_conflicts = chrec_dont_know;
    4722              : 
    4723        60427 :               *overlaps_a
    4724        60427 :                 = conflict_fn (1,
    4725        60427 :                                affine_fn_univar (build_int_cst (NULL_TREE, x1),
    4726              :                                                  1,
    4727        60427 :                                                  build_int_cst (NULL_TREE, i1)));
    4728        60427 :               *overlaps_b
    4729        60427 :                 = conflict_fn (1,
    4730        60427 :                                affine_fn_univar (build_int_cst (NULL_TREE, y1),
    4731              :                                                  1,
    4732        60427 :                                                  build_int_cst (NULL_TREE, j1)));
    4733        60427 :             }
    4734              :           else
    4735              :             {
    4736              :               /* FIXME: For the moment, the upper bound of the
    4737              :                  iteration domain for i and j is not checked.  */
    4738            0 :               if (dump_file && (dump_flags & TDF_DETAILS))
    4739            0 :                 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
    4740            0 :               *overlaps_a = conflict_fn_not_known ();
    4741            0 :               *overlaps_b = conflict_fn_not_known ();
    4742            0 :               *last_conflicts = chrec_dont_know;
    4743              :             }
    4744        60427 :         }
    4745              :       else
    4746              :         {
    4747          394 :           if (dump_file && (dump_flags & TDF_DETAILS))
    4748           19 :             fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
    4749          394 :           *overlaps_a = conflict_fn_not_known ();
    4750          394 :           *overlaps_b = conflict_fn_not_known ();
    4751          394 :           *last_conflicts = chrec_dont_know;
    4752              :         }
    4753              :     }
    4754              :   else
    4755              :     {
    4756            0 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4757            0 :         fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
    4758            0 :       *overlaps_a = conflict_fn_not_known ();
    4759            0 :       *overlaps_b = conflict_fn_not_known ();
    4760            0 :       *last_conflicts = chrec_dont_know;
    4761              :     }
    4762              : 
    4763      1685065 : end_analyze_subs_aa:
    4764      1685065 :   obstack_free (&scratch_obstack, NULL);
    4765      1685065 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4766              :     {
    4767        20014 :       fprintf (dump_file, "  (overlaps_a = ");
    4768        20014 :       dump_conflict_function (dump_file, *overlaps_a);
    4769        20014 :       fprintf (dump_file, ")\n  (overlaps_b = ");
    4770        20014 :       dump_conflict_function (dump_file, *overlaps_b);
    4771        20014 :       fprintf (dump_file, "))\n");
    4772              :     }
    4773              : }
    4774              : 
    4775              : /* Returns true when analyze_subscript_affine_affine can be used for
    4776              :    determining the dependence relation between chrec_a and chrec_b,
    4777              :    that contain symbols.  This function modifies chrec_a and chrec_b
    4778              :    such that the analysis result is the same, and such that they don't
    4779              :    contain symbols, and then can safely be passed to the analyzer.
    4780              : 
    4781              :    Example: The analysis of the following tuples of evolutions produce
    4782              :    the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
    4783              :    vs. {0, +, 1}_1
    4784              : 
    4785              :    {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
    4786              :    {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
    4787              : */
    4788              : 
    4789              : static bool
    4790        44001 : can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
    4791              : {
    4792        44001 :   tree diff, type, left_a, left_b, right_b;
    4793              : 
    4794        44001 :   if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
    4795        44001 :       || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
    4796              :     /* FIXME: For the moment not handled.  Might be refined later.  */
    4797        14963 :     return false;
    4798              : 
    4799        29038 :   type = chrec_type (*chrec_a);
    4800        29038 :   left_a = CHREC_LEFT (*chrec_a);
    4801        29038 :   left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
    4802        29038 :   diff = chrec_fold_minus (type, left_a, left_b);
    4803              : 
    4804        58076 :   if (!evolution_function_is_constant_p (diff))
    4805         5376 :     return false;
    4806              : 
    4807        23662 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4808          105 :     fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
    4809              : 
    4810        23662 :   *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
    4811        23662 :                                      diff, CHREC_RIGHT (*chrec_a));
    4812        23662 :   right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
    4813        23662 :   *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
    4814              :                                      build_int_cst (type, 0),
    4815              :                                      right_b);
    4816        23662 :   return true;
    4817              : }
    4818              : 
    4819              : /* Analyze a SIV (Single Index Variable) subscript.  *OVERLAPS_A and
    4820              :    *OVERLAPS_B are initialized to the functions that describe the
    4821              :    relation between the elements accessed twice by CHREC_A and
    4822              :    CHREC_B.  For k >= 0, the following property is verified:
    4823              : 
    4824              :    CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */
    4825              : 
    4826              : static void
    4827      1708373 : analyze_siv_subscript (tree chrec_a,
    4828              :                        tree chrec_b,
    4829              :                        conflict_function **overlaps_a,
    4830              :                        conflict_function **overlaps_b,
    4831              :                        tree *last_conflicts,
    4832              :                        int loop_nest_num)
    4833              : {
    4834      1708373 :   dependence_stats.num_siv++;
    4835              : 
    4836      1708373 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4837        23145 :     fprintf (dump_file, "(analyze_siv_subscript \n");
    4838              : 
    4839      1708373 :   if (evolution_function_is_constant_p (chrec_a)
    4840      1708373 :       && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
    4841         1784 :     analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
    4842              :                                       overlaps_a, overlaps_b, last_conflicts);
    4843              : 
    4844      1706589 :   else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
    4845      3413178 :            && evolution_function_is_constant_p (chrec_b))
    4846         1265 :     analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
    4847              :                                       overlaps_b, overlaps_a, last_conflicts);
    4848              : 
    4849      1705324 :   else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
    4850      1705324 :            && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
    4851              :     {
    4852      1705324 :       if (!chrec_contains_symbols (chrec_a)
    4853      1705324 :           && !chrec_contains_symbols (chrec_b))
    4854              :         {
    4855      1661323 :           analyze_subscript_affine_affine (chrec_a, chrec_b,
    4856              :                                            overlaps_a, overlaps_b,
    4857              :                                            last_conflicts);
    4858              : 
    4859      1661323 :           if (CF_NOT_KNOWN_P (*overlaps_a)
    4860      1660937 :               || CF_NOT_KNOWN_P (*overlaps_b))
    4861          386 :             dependence_stats.num_siv_unimplemented++;
    4862      1660937 :           else if (CF_NO_DEPENDENCE_P (*overlaps_a)
    4863        59595 :                    || CF_NO_DEPENDENCE_P (*overlaps_b))
    4864      1601342 :             dependence_stats.num_siv_independent++;
    4865              :           else
    4866        59595 :             dependence_stats.num_siv_dependent++;
    4867              :         }
    4868        44001 :       else if (can_use_analyze_subscript_affine_affine (&chrec_a,
    4869              :                                                         &chrec_b))
    4870              :         {
    4871        23662 :           analyze_subscript_affine_affine (chrec_a, chrec_b,
    4872              :                                            overlaps_a, overlaps_b,
    4873              :                                            last_conflicts);
    4874              : 
    4875        23662 :           if (CF_NOT_KNOWN_P (*overlaps_a)
    4876        23646 :               || CF_NOT_KNOWN_P (*overlaps_b))
    4877           16 :             dependence_stats.num_siv_unimplemented++;
    4878        23646 :           else if (CF_NO_DEPENDENCE_P (*overlaps_a)
    4879          834 :                    || CF_NO_DEPENDENCE_P (*overlaps_b))
    4880        22812 :             dependence_stats.num_siv_independent++;
    4881              :           else
    4882          834 :             dependence_stats.num_siv_dependent++;
    4883              :         }
    4884              :       else
    4885        20339 :         goto siv_subscript_dontknow;
    4886              :     }
    4887              : 
    4888              :   else
    4889              :     {
    4890        20339 :     siv_subscript_dontknow:;
    4891        20339 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4892         2946 :         fprintf (dump_file, "  siv test failed: unimplemented");
    4893        20339 :       *overlaps_a = conflict_fn_not_known ();
    4894        20339 :       *overlaps_b = conflict_fn_not_known ();
    4895        20339 :       *last_conflicts = chrec_dont_know;
    4896        20339 :       dependence_stats.num_siv_unimplemented++;
    4897              :     }
    4898              : 
    4899      1708373 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4900        23145 :     fprintf (dump_file, ")\n");
    4901      1708373 : }
    4902              : 
    4903              : /* Returns false if we can prove that the greatest common divisor of the steps
    4904              :    of CHREC does not divide CST, false otherwise.  */
    4905              : 
    4906              : static bool
    4907        20662 : gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
    4908              : {
    4909        20662 :   HOST_WIDE_INT cd = 0, val;
    4910        20662 :   tree step;
    4911              : 
    4912        20662 :   if (!tree_fits_shwi_p (cst))
    4913              :     return true;
    4914        20662 :   val = tree_to_shwi (cst);
    4915              : 
    4916        61838 :   while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
    4917              :     {
    4918        41322 :       step = CHREC_RIGHT (chrec);
    4919        41322 :       if (!tree_fits_shwi_p (step))
    4920              :         return true;
    4921        41176 :       cd = gcd (cd, tree_to_shwi (step));
    4922        41176 :       chrec = CHREC_LEFT (chrec);
    4923              :     }
    4924              : 
    4925        20516 :   return val % cd == 0;
    4926              : }
    4927              : 
    4928              : /* Analyze a MIV (Multiple Index Variable) subscript with respect to
    4929              :    LOOP_NEST.  *OVERLAPS_A and *OVERLAPS_B are initialized to the
    4930              :    functions that describe the relation between the elements accessed
    4931              :    twice by CHREC_A and CHREC_B.  For k >= 0, the following property
    4932              :    is verified:
    4933              : 
    4934              :    CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */
    4935              : 
    4936              : static void
    4937        26440 : analyze_miv_subscript (tree chrec_a,
    4938              :                        tree chrec_b,
    4939              :                        conflict_function **overlaps_a,
    4940              :                        conflict_function **overlaps_b,
    4941              :                        tree *last_conflicts,
    4942              :                        class loop *loop_nest)
    4943              : {
    4944        26440 :   tree type, difference;
    4945              : 
    4946        26440 :   dependence_stats.num_miv++;
    4947        26440 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4948           27 :     fprintf (dump_file, "(analyze_miv_subscript \n");
    4949              : 
    4950        26440 :   type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
    4951        26440 :   chrec_a = chrec_convert (type, chrec_a, NULL);
    4952        26440 :   chrec_b = chrec_convert (type, chrec_b, NULL);
    4953        26440 :   difference = chrec_fold_minus (type, chrec_a, chrec_b);
    4954              : 
    4955        26440 :   if (eq_evolutions_p (chrec_a, chrec_b))
    4956              :     {
    4957              :       /* Access functions are the same: all the elements are accessed
    4958              :          in the same order.  */
    4959            0 :       *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
    4960            0 :       *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
    4961            0 :       *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
    4962            0 :       dependence_stats.num_miv_dependent++;
    4963              :     }
    4964              : 
    4965        26440 :   else if (evolution_function_is_constant_p (difference)
    4966        20692 :            && evolution_function_is_affine_multivariate_p (chrec_a,
    4967              :                                                            loop_nest->num)
    4968        47102 :            && !gcd_of_steps_may_divide_p (chrec_a, difference))
    4969              :     {
    4970              :       /* testsuite/.../ssa-chrec-33.c
    4971              :          {{21, +, 2}_1, +, -2}_2  vs.  {{20, +, 2}_1, +, -2}_2
    4972              : 
    4973              :          The difference is 1, and all the evolution steps are multiples
    4974              :          of 2, consequently there are no overlapping elements.  */
    4975        19670 :       *overlaps_a = conflict_fn_no_dependence ();
    4976        19670 :       *overlaps_b = conflict_fn_no_dependence ();
    4977        19670 :       *last_conflicts = integer_zero_node;
    4978        19670 :       dependence_stats.num_miv_independent++;
    4979              :     }
    4980              : 
    4981         6770 :   else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest->num)
    4982          122 :            && !chrec_contains_symbols (chrec_a, loop_nest)
    4983          110 :            && evolution_function_is_affine_in_loop (chrec_b, loop_nest->num)
    4984         6850 :            && !chrec_contains_symbols (chrec_b, loop_nest))
    4985              :     {
    4986              :       /* testsuite/.../ssa-chrec-35.c
    4987              :          {0, +, 1}_2  vs.  {0, +, 1}_3
    4988              :          the overlapping elements are respectively located at iterations:
    4989              :          {0, +, 1}_x and {0, +, 1}_x,
    4990              :          in other words, we have the equality:
    4991              :          {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
    4992              : 
    4993              :          Other examples:
    4994              :          {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
    4995              :          {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
    4996              : 
    4997              :          {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
    4998              :          {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
    4999              :       */
    5000           80 :       analyze_subscript_affine_affine (chrec_a, chrec_b,
    5001              :                                        overlaps_a, overlaps_b, last_conflicts);
    5002              : 
    5003           80 :       if (CF_NOT_KNOWN_P (*overlaps_a)
    5004           76 :           || CF_NOT_KNOWN_P (*overlaps_b))
    5005            4 :         dependence_stats.num_miv_unimplemented++;
    5006           76 :       else if (CF_NO_DEPENDENCE_P (*overlaps_a)
    5007           62 :                || CF_NO_DEPENDENCE_P (*overlaps_b))
    5008           14 :         dependence_stats.num_miv_independent++;
    5009              :       else
    5010           62 :         dependence_stats.num_miv_dependent++;
    5011              :     }
    5012              : 
    5013              :   else
    5014              :     {
    5015              :       /* When the analysis is too difficult, answer "don't know".  */
    5016         6690 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5017           23 :         fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
    5018              : 
    5019         6690 :       *overlaps_a = conflict_fn_not_known ();
    5020         6690 :       *overlaps_b = conflict_fn_not_known ();
    5021         6690 :       *last_conflicts = chrec_dont_know;
    5022         6690 :       dependence_stats.num_miv_unimplemented++;
    5023              :     }
    5024              : 
    5025        26440 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5026           27 :     fprintf (dump_file, ")\n");
    5027        26440 : }
    5028              : 
    5029              : /* Determines the iterations for which CHREC_A is equal to CHREC_B in
    5030              :    with respect to LOOP_NEST.  OVERLAP_ITERATIONS_A and
    5031              :    OVERLAP_ITERATIONS_B are initialized with two functions that
    5032              :    describe the iterations that contain conflicting elements.
    5033              : 
    5034              :    Remark: For an integer k >= 0, the following equality is true:
    5035              : 
    5036              :    CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
    5037              : */
    5038              : 
    5039              : static void
    5040      3423551 : analyze_overlapping_iterations (tree chrec_a,
    5041              :                                 tree chrec_b,
    5042              :                                 conflict_function **overlap_iterations_a,
    5043              :                                 conflict_function **overlap_iterations_b,
    5044              :                                 tree *last_conflicts, class loop *loop_nest)
    5045              : {
    5046      3423551 :   unsigned int lnn = loop_nest->num;
    5047              : 
    5048      3423551 :   dependence_stats.num_subscript_tests++;
    5049              : 
    5050      3423551 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5051              :     {
    5052        59260 :       fprintf (dump_file, "(analyze_overlapping_iterations \n");
    5053        59260 :       fprintf (dump_file, "  (chrec_a = ");
    5054        59260 :       print_generic_expr (dump_file, chrec_a);
    5055        59260 :       fprintf (dump_file, ")\n  (chrec_b = ");
    5056        59260 :       print_generic_expr (dump_file, chrec_b);
    5057        59260 :       fprintf (dump_file, ")\n");
    5058              :     }
    5059              : 
    5060      3423551 :   if (chrec_a == NULL_TREE
    5061      3423551 :       || chrec_b == NULL_TREE
    5062      3423551 :       || chrec_contains_undetermined (chrec_a)
    5063      6847102 :       || chrec_contains_undetermined (chrec_b))
    5064              :     {
    5065            0 :       dependence_stats.num_subscript_undetermined++;
    5066              : 
    5067            0 :       *overlap_iterations_a = conflict_fn_not_known ();
    5068            0 :       *overlap_iterations_b = conflict_fn_not_known ();
    5069              :     }
    5070              : 
    5071              :   /* If they are the same chrec, and are affine, they overlap
    5072              :      on every iteration.  */
    5073      3423551 :   else if (eq_evolutions_p (chrec_a, chrec_b)
    5074      3423551 :            && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
    5075       487973 :                || operand_equal_p (chrec_a, chrec_b, 0)))
    5076              :     {
    5077      1187537 :       dependence_stats.num_same_subscript_function++;
    5078      1187537 :       *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
    5079      1187537 :       *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
    5080      1187537 :       *last_conflicts = chrec_dont_know;
    5081              :     }
    5082              : 
    5083              :   /* If they aren't the same, and aren't affine, we can't do anything
    5084              :      yet.  */
    5085      2236014 :   else if ((chrec_contains_symbols (chrec_a)
    5086      2184302 :             || chrec_contains_symbols (chrec_b))
    5087      2236879 :            && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
    5088        50355 :                || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
    5089              :     {
    5090         2522 :       dependence_stats.num_subscript_undetermined++;
    5091         2522 :       *overlap_iterations_a = conflict_fn_not_known ();
    5092         2522 :       *overlap_iterations_b = conflict_fn_not_known ();
    5093              :     }
    5094              : 
    5095      2233492 :   else if (ziv_subscript_p (chrec_a, chrec_b))
    5096       498679 :     analyze_ziv_subscript (chrec_a, chrec_b,
    5097              :                            overlap_iterations_a, overlap_iterations_b,
    5098              :                            last_conflicts);
    5099              : 
    5100      1734813 :   else if (siv_subscript_p (chrec_a, chrec_b))
    5101      1708373 :     analyze_siv_subscript (chrec_a, chrec_b,
    5102              :                            overlap_iterations_a, overlap_iterations_b,
    5103              :                            last_conflicts, lnn);
    5104              : 
    5105              :   else
    5106        26440 :     analyze_miv_subscript (chrec_a, chrec_b,
    5107              :                            overlap_iterations_a, overlap_iterations_b,
    5108              :                            last_conflicts, loop_nest);
    5109              : 
    5110      3423551 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5111              :     {
    5112        59260 :       fprintf (dump_file, "  (overlap_iterations_a = ");
    5113        59260 :       dump_conflict_function (dump_file, *overlap_iterations_a);
    5114        59260 :       fprintf (dump_file, ")\n  (overlap_iterations_b = ");
    5115        59260 :       dump_conflict_function (dump_file, *overlap_iterations_b);
    5116        59260 :       fprintf (dump_file, "))\n");
    5117              :     }
    5118      3423551 : }
    5119              : 
    5120              : /* Helper function for uniquely inserting distance vectors.  */
    5121              : 
    5122              : static void
    5123      1086683 : save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
    5124              : {
    5125      1627093 :   for (lambda_vector v : DDR_DIST_VECTS (ddr))
    5126       541797 :     if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
    5127              :       return;
    5128              : 
    5129      1086416 :   DDR_DIST_VECTS (ddr).safe_push (dist_v);
    5130              : }
    5131              : 
    5132              : /* Helper function for uniquely inserting direction vectors.  */
    5133              : 
    5134              : static void
    5135      1086416 : save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
    5136              : {
    5137      1626025 :   for (lambda_vector v : DDR_DIR_VECTS (ddr))
    5138       540195 :     if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
    5139              :       return;
    5140              : 
    5141      1086416 :   DDR_DIR_VECTS (ddr).safe_push (dir_v);
    5142              : }
    5143              : 
    5144              : /* Add a distance of 1 on all the loops outer than INDEX.  If we
    5145              :    haven't yet determined a distance for this outer loop, push a new
    5146              :    distance vector composed of the previous distance, and a distance
    5147              :    of 1 for this outer loop.  Example:
    5148              : 
    5149              :    | loop_1
    5150              :    |   loop_2
    5151              :    |     A[10]
    5152              :    |   endloop_2
    5153              :    | endloop_1
    5154              : 
    5155              :    Saved vectors are of the form (dist_in_1, dist_in_2).  First, we
    5156              :    save (0, 1), then we have to save (1, 0).  */
    5157              : 
    5158              : static void
    5159        16671 : add_outer_distances (struct data_dependence_relation *ddr,
    5160              :                      lambda_vector dist_v, int index)
    5161              : {
    5162              :   /* For each outer loop where init_v is not set, the accesses are
    5163              :      in dependence of distance 1 in the loop.  */
    5164        19862 :   while (--index >= 0)
    5165              :     {
    5166         6382 :       lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
    5167         3191 :       lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
    5168         3191 :       save_v[index] = 1;
    5169         3191 :       save_dist_v (ddr, save_v);
    5170              :     }
    5171        16671 : }
    5172              : 
    5173              : /* Return false when fail to represent the data dependence as a
    5174              :    distance vector.  A_INDEX is the index of the first reference
    5175              :    (0 for DDR_A, 1 for DDR_B) and B_INDEX is the index of the
    5176              :    second reference.  INIT_B is set to true when a component has been
    5177              :    added to the distance vector DIST_V.  INDEX_CARRY is then set to
    5178              :    the index in DIST_V that carries the dependence.  */
    5179              : 
    5180              : static bool
    5181        62027 : build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
    5182              :                              unsigned int a_index, unsigned int b_index,
    5183              :                              lambda_vector dist_v, bool *init_b,
    5184              :                              int *index_carry)
    5185              : {
    5186        62027 :   unsigned i;
    5187       124054 :   lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
    5188        62027 :   class loop *loop = DDR_LOOP_NEST (ddr)[0];
    5189              : 
    5190       140031 :   for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
    5191              :     {
    5192        80188 :       tree access_fn_a, access_fn_b;
    5193        80188 :       struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
    5194              : 
    5195        80188 :       if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
    5196              :         {
    5197          309 :           non_affine_dependence_relation (ddr);
    5198          309 :           return false;
    5199              :         }
    5200              : 
    5201        79879 :       access_fn_a = SUB_ACCESS_FN (subscript, a_index);
    5202        79879 :       access_fn_b = SUB_ACCESS_FN (subscript, b_index);
    5203              : 
    5204        79879 :       if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
    5205        60647 :           && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
    5206              :         {
    5207        60021 :           HOST_WIDE_INT dist;
    5208        60021 :           int index;
    5209        60021 :           int var_a = CHREC_VARIABLE (access_fn_a);
    5210        60021 :           int var_b = CHREC_VARIABLE (access_fn_b);
    5211              : 
    5212        60021 :           if (var_a != var_b
    5213        60021 :               || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
    5214              :             {
    5215           34 :               non_affine_dependence_relation (ddr);
    5216           34 :               return false;
    5217              :             }
    5218              : 
    5219              :           /* When data references are collected in a loop while data
    5220              :              dependences are analyzed in loop nest nested in the loop, we
    5221              :              would have more number of access functions than number of
    5222              :              loops.  Skip access functions of loops not in the loop nest.
    5223              : 
    5224              :              See PR89725 for more information.  */
    5225        59987 :           if (flow_loop_nested_p (get_loop (cfun, var_a), loop))
    5226            2 :             continue;
    5227              : 
    5228        59985 :           dist = int_cst_value (SUB_DISTANCE (subscript));
    5229        59985 :           index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
    5230        59985 :           *index_carry = MIN (index, *index_carry);
    5231              : 
    5232              :           /* This is the subscript coupling test.  If we have already
    5233              :              recorded a distance for this loop (a distance coming from
    5234              :              another subscript), it should be the same.  For example,
    5235              :              in the following code, there is no dependence:
    5236              : 
    5237              :              | loop i = 0, N, 1
    5238              :              |   T[i+1][i] = ...
    5239              :              |   ... = T[i][i]
    5240              :              | endloop
    5241              :           */
    5242        59985 :           if (init_v[index] != 0 && dist_v[index] != dist)
    5243              :             {
    5244            0 :               finalize_ddr_dependent (ddr, chrec_known);
    5245            0 :               return false;
    5246              :             }
    5247              : 
    5248        59985 :           dist_v[index] = dist;
    5249        59985 :           init_v[index] = 1;
    5250        59985 :           *init_b = true;
    5251        59985 :         }
    5252        19858 :       else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
    5253              :         {
    5254              :           /* This can be for example an affine vs. constant dependence
    5255              :              (T[i] vs. T[3]) that is not an affine dependence and is
    5256              :              not representable as a distance vector.  */
    5257         1841 :           non_affine_dependence_relation (ddr);
    5258         1841 :           return false;
    5259              :         }
    5260              :     }
    5261              : 
    5262              :   return true;
    5263              : }
    5264              : 
    5265              : /* Return true when the DDR contains only invariant access functions wrto. loop
    5266              :    number LNUM.  */
    5267              : 
    5268              : static bool
    5269       855311 : invariant_access_functions (const struct data_dependence_relation *ddr,
    5270              :                             int lnum)
    5271              : {
    5272      2887854 :   for (subscript *sub : DDR_SUBSCRIPTS (ddr))
    5273      1002761 :     if (!evolution_function_is_invariant_p (SUB_ACCESS_FN (sub, 0), lnum)
    5274      1002761 :         || !evolution_function_is_invariant_p (SUB_ACCESS_FN (sub, 1), lnum))
    5275       680840 :       return false;
    5276              : 
    5277              :   return true;
    5278              : }
    5279              : 
    5280              : /* Helper function for the case where DDR_A and DDR_B are the same
    5281              :    multivariate access function with a constant step.  For an example
    5282              :    see pr34635-1.c.  */
    5283              : 
    5284              : static void
    5285         4540 : add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
    5286              : {
    5287         4540 :   int x_1, x_2;
    5288         4540 :   tree c_1 = CHREC_LEFT (c_2);
    5289         4540 :   tree c_0 = CHREC_LEFT (c_1);
    5290         4540 :   lambda_vector dist_v;
    5291         4540 :   HOST_WIDE_INT v1, v2, cd;
    5292              : 
    5293              :   /* Polynomials with more than 2 variables are not handled yet.  When
    5294              :      the evolution steps are parameters, it is not possible to
    5295              :      represent the dependence using classical distance vectors.  */
    5296         4540 :   if (TREE_CODE (c_0) != INTEGER_CST
    5297         3024 :       || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
    5298         6925 :       || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
    5299              :     {
    5300         2163 :       DDR_AFFINE_P (ddr) = false;
    5301         2163 :       return;
    5302              :     }
    5303              : 
    5304         2377 :   x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
    5305         2377 :   x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
    5306              : 
    5307              :   /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2).  */
    5308         4754 :   dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
    5309         2377 :   v1 = int_cst_value (CHREC_RIGHT (c_1));
    5310         2377 :   v2 = int_cst_value (CHREC_RIGHT (c_2));
    5311         2377 :   cd = gcd (v1, v2);
    5312         2377 :   v1 /= cd;
    5313         2377 :   v2 /= cd;
    5314              : 
    5315         2377 :   if (v2 < 0)
    5316              :     {
    5317            2 :       v2 = -v2;
    5318            2 :       v1 = -v1;
    5319              :     }
    5320              : 
    5321         2377 :   dist_v[x_1] = v2;
    5322         2377 :   dist_v[x_2] = -v1;
    5323         2377 :   save_dist_v (ddr, dist_v);
    5324              : 
    5325         2377 :   add_outer_distances (ddr, dist_v, x_1);
    5326              : }
    5327              : 
    5328              : /* Helper function for the case where DDR_A and DDR_B are the same
    5329              :    access functions.  */
    5330              : 
    5331              : static void
    5332        18973 : add_other_self_distances (struct data_dependence_relation *ddr)
    5333              : {
    5334        18973 :   lambda_vector dist_v;
    5335        18973 :   unsigned i;
    5336        18973 :   int index_carry = DDR_NB_LOOPS (ddr);
    5337        18973 :   subscript *sub;
    5338        18973 :   class loop *loop = DDR_LOOP_NEST (ddr)[0];
    5339              : 
    5340        40352 :   FOR_EACH_VEC_ELT (DDR_SUBSCRIPTS (ddr), i, sub)
    5341              :     {
    5342        26420 :       tree access_fun = SUB_ACCESS_FN (sub, 0);
    5343              : 
    5344        26420 :       if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
    5345              :         {
    5346        19094 :           if (!evolution_function_is_univariate_p (access_fun, loop->num))
    5347              :             {
    5348         5041 :               if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
    5349              :                 {
    5350          501 :                   DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
    5351          501 :                   return;
    5352              :                 }
    5353              : 
    5354         4540 :               access_fun = SUB_ACCESS_FN (DDR_SUBSCRIPT (ddr, 0), 0);
    5355              : 
    5356         4540 :               if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
    5357         4540 :                 add_multivariate_self_dist (ddr, access_fun);
    5358              :               else
    5359              :                 /* The evolution step is not constant: it varies in
    5360              :                    the outer loop, so this cannot be represented by a
    5361              :                    distance vector.  For example in pr34635.c the
    5362              :                    evolution is {0, +, {0, +, 4}_1}_2.  */
    5363            0 :                 DDR_AFFINE_P (ddr) = false;
    5364              : 
    5365         4540 :               return;
    5366              :             }
    5367              : 
    5368              :           /* When data references are collected in a loop while data
    5369              :              dependences are analyzed in loop nest nested in the loop, we
    5370              :              would have more number of access functions than number of
    5371              :              loops.  Skip access functions of loops not in the loop nest.
    5372              : 
    5373              :              See PR89725 for more information.  */
    5374        14053 :           if (flow_loop_nested_p (get_loop (cfun, CHREC_VARIABLE (access_fun)),
    5375              :                                   loop))
    5376            0 :             continue;
    5377              : 
    5378        21536 :           index_carry = MIN (index_carry,
    5379              :                              index_in_loop_nest (CHREC_VARIABLE (access_fun),
    5380              :                                                  DDR_LOOP_NEST (ddr)));
    5381              :         }
    5382              :     }
    5383              : 
    5384        27864 :   dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
    5385        13932 :   add_outer_distances (ddr, dist_v, index_carry);
    5386              : }
    5387              : 
    5388              : static void
    5389       174471 : insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
    5390              : {
    5391       348942 :   lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
    5392              : 
    5393       174471 :   dist_v[0] = 1;
    5394       174471 :   save_dist_v (ddr, dist_v);
    5395       174471 : }
    5396              : 
    5397              : /* Adds a unit distance vector to DDR when there is a 0 overlap.  This
    5398              :    is the case for example when access functions are the same and
    5399              :    equal to a constant, as in:
    5400              : 
    5401              :    | loop_1
    5402              :    |   A[3] = ...
    5403              :    |   ... = A[3]
    5404              :    | endloop_1
    5405              : 
    5406              :    in which case the distance vectors are (0) and (1).  */
    5407              : 
    5408              : static void
    5409       174471 : add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
    5410              : {
    5411       174471 :   unsigned i, j;
    5412              : 
    5413       174471 :   for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
    5414              :     {
    5415       174471 :       subscript_p sub = DDR_SUBSCRIPT (ddr, i);
    5416       174471 :       conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
    5417       174471 :       conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
    5418              : 
    5419       174471 :       for (j = 0; j < ca->n; j++)
    5420       174471 :         if (affine_function_zero_p (ca->fns[j]))
    5421              :           {
    5422       174471 :             insert_innermost_unit_dist_vector (ddr);
    5423       174471 :             return;
    5424              :           }
    5425              : 
    5426            0 :       for (j = 0; j < cb->n; j++)
    5427            0 :         if (affine_function_zero_p (cb->fns[j]))
    5428              :           {
    5429            0 :             insert_innermost_unit_dist_vector (ddr);
    5430            0 :             return;
    5431              :           }
    5432              :     }
    5433              : }
    5434              : 
    5435              : /* Return true when the DDR contains two data references that have the
    5436              :    same access functions.  */
    5437              : 
    5438              : static inline bool
    5439       908832 : same_access_functions (const struct data_dependence_relation *ddr)
    5440              : {
    5441      3805345 :   for (subscript *sub : DDR_SUBSCRIPTS (ddr))
    5442      1132370 :     if (!eq_evolutions_p (SUB_ACCESS_FN (sub, 0),
    5443      1132370 :                           SUB_ACCESS_FN (sub, 1)))
    5444              :       return false;
    5445              : 
    5446              :   return true;
    5447              : }
    5448              : 
    5449              : /* Compute the classic per loop distance vector.  DDR is the data
    5450              :    dependence relation to build a vector from.  Return false when fail
    5451              :    to represent the data dependence as a distance vector.  */
    5452              : 
    5453              : static bool
    5454      3080866 : build_classic_dist_vector (struct data_dependence_relation *ddr,
    5455              :                            class loop *loop_nest)
    5456              : {
    5457      3080866 :   bool init_b = false;
    5458      3080866 :   int index_carry = DDR_NB_LOOPS (ddr);
    5459      3080866 :   lambda_vector dist_v;
    5460              : 
    5461      3080866 :   if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
    5462              :     return false;
    5463              : 
    5464       908832 :   if (same_access_functions (ddr))
    5465              :     {
    5466              :       /* Save the 0 vector.  */
    5467      1710622 :       dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
    5468       855311 :       save_dist_v (ddr, dist_v);
    5469              : 
    5470       855311 :       if (invariant_access_functions (ddr, loop_nest->num))
    5471       174471 :         add_distance_for_zero_overlaps (ddr);
    5472              : 
    5473       855311 :       if (DDR_NB_LOOPS (ddr) > 1)
    5474        18973 :         add_other_self_distances (ddr);
    5475              : 
    5476       855311 :       return true;
    5477              :     }
    5478              : 
    5479       107042 :   dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
    5480        53521 :   if (!build_classic_dist_vector_1 (ddr, 0, 1, dist_v, &init_b, &index_carry))
    5481              :     return false;
    5482              : 
    5483              :   /* Save the distance vector if we initialized one.  */
    5484        51337 :   if (init_b)
    5485              :     {
    5486              :       /* Verify a basic constraint: classic distance vectors should
    5487              :          always be lexicographically positive.
    5488              : 
    5489              :          Data references are collected in the order of execution of
    5490              :          the program, thus for the following loop
    5491              : 
    5492              :          | for (i = 1; i < 100; i++)
    5493              :          |   for (j = 1; j < 100; j++)
    5494              :          |     {
    5495              :          |       t = T[j+1][i-1];  // A
    5496              :          |       T[j][i] = t + 2;  // B
    5497              :          |     }
    5498              : 
    5499              :          references are collected following the direction of the wind:
    5500              :          A then B.  The data dependence tests are performed also
    5501              :          following this order, such that we're looking at the distance
    5502              :          separating the elements accessed by A from the elements later
    5503              :          accessed by B.  But in this example, the distance returned by
    5504              :          test_dep (A, B) is lexicographically negative (-1, 1), that
    5505              :          means that the access A occurs later than B with respect to
    5506              :          the outer loop, ie. we're actually looking upwind.  In this
    5507              :          case we solve test_dep (B, A) looking downwind to the
    5508              :          lexicographically positive solution, that returns the
    5509              :          distance vector (1, -1).  */
    5510       102674 :       if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
    5511              :         {
    5512         8401 :           lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
    5513         8401 :           if (!subscript_dependence_tester_1 (ddr, 1, 0, loop_nest))
    5514              :             return false;
    5515         8397 :           compute_subscript_distance (ddr);
    5516         8397 :           if (!build_classic_dist_vector_1 (ddr, 1, 0, save_v, &init_b,
    5517              :                                             &index_carry))
    5518              :             return false;
    5519         8397 :           save_dist_v (ddr, save_v);
    5520         8397 :           DDR_REVERSED_P (ddr) = true;
    5521              : 
    5522              :           /* In this case there is a dependence forward for all the
    5523              :              outer loops:
    5524              : 
    5525              :              | for (k = 1; k < 100; k++)
    5526              :              |  for (i = 1; i < 100; i++)
    5527              :              |   for (j = 1; j < 100; j++)
    5528              :              |     {
    5529              :              |       t = T[j+1][i-1];  // A
    5530              :              |       T[j][i] = t + 2;  // B
    5531              :              |     }
    5532              : 
    5533              :              the vectors are:
    5534              :              (0,  1, -1)
    5535              :              (1,  1, -1)
    5536              :              (1, -1,  1)
    5537              :           */
    5538         8397 :           if (DDR_NB_LOOPS (ddr) > 1)
    5539              :             {
    5540           72 :               add_outer_distances (ddr, save_v, index_carry);
    5541           72 :               add_outer_distances (ddr, dist_v, index_carry);
    5542              :             }
    5543              :         }
    5544              :       else
    5545              :         {
    5546        42936 :           lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
    5547        42936 :           lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
    5548              : 
    5549        42936 :           if (DDR_NB_LOOPS (ddr) > 1)
    5550              :             {
    5551          109 :               lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
    5552              : 
    5553          109 :               if (!subscript_dependence_tester_1 (ddr, 1, 0, loop_nest))
    5554              :                 return false;
    5555          109 :               compute_subscript_distance (ddr);
    5556          109 :               if (!build_classic_dist_vector_1 (ddr, 1, 0, opposite_v, &init_b,
    5557              :                                                 &index_carry))
    5558              :                 return false;
    5559              : 
    5560          109 :               save_dist_v (ddr, save_v);
    5561          109 :               add_outer_distances (ddr, dist_v, index_carry);
    5562          109 :               add_outer_distances (ddr, opposite_v, index_carry);
    5563              :             }
    5564              :           else
    5565        42827 :             save_dist_v (ddr, save_v);
    5566              :         }
    5567              :     }
    5568              :   else
    5569              :     {
    5570              :       /* There is a distance of 1 on all the outer loops: Example:
    5571              :          there is a dependence of distance 1 on loop_1 for the array A.
    5572              : 
    5573              :          | loop_1
    5574              :          |   A[5] = ...
    5575              :          | endloop
    5576              :       */
    5577            0 :       add_outer_distances (ddr, dist_v,
    5578              :                            lambda_vector_first_nz (dist_v,
    5579            0 :                                                    DDR_NB_LOOPS (ddr), 0));
    5580              :     }
    5581              : 
    5582              :   return true;
    5583              : }
    5584              : 
    5585              : /* Return the direction for a given distance.
    5586              :    FIXME: Computing dir this way is suboptimal, since dir can catch
    5587              :    cases that dist is unable to represent.  */
    5588              : 
    5589              : static inline enum data_dependence_direction
    5590      1111236 : dir_from_dist (int dist)
    5591              : {
    5592      1111236 :   if (dist > 0)
    5593              :     return dir_positive;
    5594       880098 :   else if (dist < 0)
    5595              :     return dir_negative;
    5596              :   else
    5597       877689 :     return dir_equal;
    5598              : }
    5599              : 
    5600              : /* Compute the classic per loop direction vector.  DDR is the data
    5601              :    dependence relation to build a vector from.  */
    5602              : 
    5603              : static void
    5604       906644 : build_classic_dir_vector (struct data_dependence_relation *ddr)
    5605              : {
    5606       906644 :   unsigned i, j;
    5607       906644 :   lambda_vector dist_v;
    5608              : 
    5609      1993060 :   FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
    5610              :     {
    5611      2172832 :       lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
    5612              : 
    5613      3284068 :       for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
    5614      1991334 :         dir_v[j] = dir_from_dist (dist_v[j]);
    5615              : 
    5616      1086416 :       save_dir_v (ddr, dir_v);
    5617              :     }
    5618       906644 : }
    5619              : 
    5620              : /* Helper function.  Returns true when there is a dependence between the
    5621              :    data references.  A_INDEX is the index of the first reference (0 for
    5622              :    DDR_A, 1 for DDR_B) and B_INDEX is the index of the second reference.  */
    5623              : 
    5624              : static bool
    5625      3089376 : subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
    5626              :                                unsigned int a_index, unsigned int b_index,
    5627              :                                class loop *loop_nest)
    5628              : {
    5629      3089376 :   unsigned int i;
    5630      3089376 :   tree last_conflicts;
    5631      3089376 :   struct subscript *subscript;
    5632      3089376 :   tree res = NULL_TREE;
    5633              : 
    5634      4369765 :   for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
    5635              :     {
    5636      3423551 :       conflict_function *overlaps_a, *overlaps_b;
    5637              : 
    5638      3423551 :       analyze_overlapping_iterations (SUB_ACCESS_FN (subscript, a_index),
    5639              :                                       SUB_ACCESS_FN (subscript, b_index),
    5640              :                                       &overlaps_a, &overlaps_b,
    5641              :                                       &last_conflicts, loop_nest);
    5642              : 
    5643      3423551 :       if (SUB_CONFLICTS_IN_A (subscript))
    5644      3423551 :         free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
    5645      3423551 :       if (SUB_CONFLICTS_IN_B (subscript))
    5646      3423551 :         free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
    5647              : 
    5648      3423551 :       SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
    5649      3423551 :       SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
    5650      3423551 :       SUB_LAST_CONFLICT (subscript) = last_conflicts;
    5651              : 
    5652              :       /* If there is any undetermined conflict function we have to
    5653              :          give a conservative answer in case we cannot prove that
    5654              :          no dependence exists when analyzing another subscript.  */
    5655      3423551 :       if (CF_NOT_KNOWN_P (overlaps_a)
    5656      3393590 :           || CF_NOT_KNOWN_P (overlaps_b))
    5657              :         {
    5658        29961 :           res = chrec_dont_know;
    5659        29961 :           continue;
    5660              :         }
    5661              : 
    5662              :       /* When there is a subscript with no dependence we can stop.  */
    5663      3393590 :       else if (CF_NO_DEPENDENCE_P (overlaps_a)
    5664      1250428 :                || CF_NO_DEPENDENCE_P (overlaps_b))
    5665              :         {
    5666      2143162 :           res = chrec_known;
    5667      2143162 :           break;
    5668              :         }
    5669              :     }
    5670              : 
    5671      3089376 :   if (res == NULL_TREE)
    5672              :     return true;
    5673              : 
    5674      2172038 :   if (res == chrec_known)
    5675      2143162 :     dependence_stats.num_dependence_independent++;
    5676              :   else
    5677        28876 :     dependence_stats.num_dependence_undetermined++;
    5678      2172038 :   finalize_ddr_dependent (ddr, res);
    5679      2172038 :   return false;
    5680              : }
    5681              : 
    5682              : /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR.  */
    5683              : 
    5684              : static void
    5685      3080866 : subscript_dependence_tester (struct data_dependence_relation *ddr,
    5686              :                              class loop *loop_nest)
    5687              : {
    5688      3080866 :   if (subscript_dependence_tester_1 (ddr, 0, 1, loop_nest))
    5689       908832 :     dependence_stats.num_dependence_dependent++;
    5690              : 
    5691      3080866 :   compute_subscript_distance (ddr);
    5692      3080866 :   if (build_classic_dist_vector (ddr, loop_nest))
    5693              :     {
    5694       906644 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5695              :         {
    5696         4009 :           unsigned i;
    5697              : 
    5698         4009 :           fprintf (dump_file, "(build_classic_dist_vector\n");
    5699        12096 :           for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
    5700              :             {
    5701         4078 :               fprintf (dump_file, "  dist_vector = (");
    5702         4078 :               print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
    5703         8156 :                                    DDR_NB_LOOPS (ddr));
    5704         4078 :               fprintf (dump_file, "  )\n");
    5705              :             }
    5706         4009 :           fprintf (dump_file, ")\n");
    5707              :         }
    5708              : 
    5709       906644 :       build_classic_dir_vector (ddr);
    5710              :     }
    5711      3080866 : }
    5712              : 
    5713              : /* Returns true when all the access functions of A are affine or
    5714              :    constant with respect to LOOP_NEST.  */
    5715              : 
    5716              : static bool
    5717      6225846 : access_functions_are_affine_or_constant_p (const struct data_reference *a,
    5718              :                                            const class loop *loop_nest)
    5719              : {
    5720      6225846 :   vec<tree> fns = DR_ACCESS_FNS (a);
    5721     27046391 :   for (tree t : fns)
    5722      8429918 :     if (!evolution_function_is_invariant_p (t, loop_nest->num)
    5723      8429918 :         && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
    5724              :       return false;
    5725              : 
    5726              :   return true;
    5727              : }
    5728              : 
    5729              : /* This computes the affine dependence relation between A and B with
    5730              :    respect to LOOP_NEST.  CHREC_KNOWN is used for representing the
    5731              :    independence between two accesses, while CHREC_DONT_KNOW is used
    5732              :    for representing the unknown relation.
    5733              : 
    5734              :    Note that it is possible to stop the computation of the dependence
    5735              :    relation the first time we detect a CHREC_KNOWN element for a given
    5736              :    subscript.  */
    5737              : 
    5738              : void
    5739      6445533 : compute_affine_dependence (struct data_dependence_relation *ddr,
    5740              :                            class loop *loop_nest)
    5741              : {
    5742      6445533 :   struct data_reference *dra = DDR_A (ddr);
    5743      6445533 :   struct data_reference *drb = DDR_B (ddr);
    5744              : 
    5745      6445533 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5746              :     {
    5747       134318 :       fprintf (dump_file, "(compute_affine_dependence\n");
    5748       134318 :       fprintf (dump_file, "  ref_a: ");
    5749       134318 :       print_generic_expr (dump_file, DR_REF (dra));
    5750       134318 :       fprintf (dump_file, ", stmt_a: ");
    5751       134318 :       print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
    5752       134318 :       fprintf (dump_file, "  ref_b: ");
    5753       134318 :       print_generic_expr (dump_file, DR_REF (drb));
    5754       134318 :       fprintf (dump_file, ", stmt_b: ");
    5755       134318 :       print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
    5756              :     }
    5757              : 
    5758              :   /* Analyze only when the dependence relation is not yet known.  */
    5759      6445533 :   if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
    5760              :     {
    5761      3141931 :       dependence_stats.num_dependence_tests++;
    5762              : 
    5763      3141931 :       if (access_functions_are_affine_or_constant_p (dra, loop_nest)
    5764      3141931 :           && access_functions_are_affine_or_constant_p (drb, loop_nest))
    5765      3080866 :         subscript_dependence_tester (ddr, loop_nest);
    5766              : 
    5767              :       /* As a last case, if the dependence cannot be determined, or if
    5768              :          the dependence is considered too difficult to determine, answer
    5769              :          "don't know".  */
    5770              :       else
    5771              :         {
    5772        61065 :           dependence_stats.num_dependence_undetermined++;
    5773              : 
    5774        61065 :           if (dump_file && (dump_flags & TDF_DETAILS))
    5775              :             {
    5776          158 :               fprintf (dump_file, "Data ref a:\n");
    5777          158 :               dump_data_reference (dump_file, dra);
    5778          158 :               fprintf (dump_file, "Data ref b:\n");
    5779          158 :               dump_data_reference (dump_file, drb);
    5780          158 :               fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
    5781              :             }
    5782        61065 :           finalize_ddr_dependent (ddr, chrec_dont_know);
    5783              :         }
    5784              :     }
    5785              : 
    5786      6445533 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5787              :     {
    5788       134318 :       if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
    5789       118983 :         fprintf (dump_file, ") -> no dependence\n");
    5790        15335 :       else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
    5791        11236 :         fprintf (dump_file, ") -> dependence analysis failed\n");
    5792              :       else
    5793         4099 :         fprintf (dump_file, ")\n");
    5794              :     }
    5795      6445533 : }
    5796              : 
    5797              : /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
    5798              :    the data references in DATAREFS, in the LOOP_NEST.  When
    5799              :    COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
    5800              :    relations.  Return true when successful, i.e. data references number
    5801              :    is small enough to be handled.  */
    5802              : 
    5803              : bool
    5804       431040 : compute_all_dependences (const vec<data_reference_p> &datarefs,
    5805              :                          vec<ddr_p> *dependence_relations,
    5806              :                          const vec<loop_p> &loop_nest,
    5807              :                          bool compute_self_and_rr)
    5808              : {
    5809       431040 :   struct data_dependence_relation *ddr;
    5810       431040 :   struct data_reference *a, *b;
    5811       431040 :   unsigned int i, j;
    5812              : 
    5813       431040 :   if ((int) datarefs.length ()
    5814       431040 :       > param_loop_max_datarefs_for_datadeps)
    5815              :     {
    5816            0 :       struct data_dependence_relation *ddr;
    5817              : 
    5818              :       /* Insert a single relation into dependence_relations:
    5819              :          chrec_dont_know.  */
    5820            0 :       ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
    5821            0 :       dependence_relations->safe_push (ddr);
    5822            0 :       return false;
    5823              :     }
    5824              : 
    5825      3195494 :   FOR_EACH_VEC_ELT (datarefs, i, a)
    5826      7656391 :     for (j = i + 1; datarefs.iterate (j, &b); j++)
    5827      4891937 :       if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
    5828              :         {
    5829      4522823 :           ddr = initialize_data_dependence_relation (a, b, loop_nest);
    5830      4522823 :           dependence_relations->safe_push (ddr);
    5831      4522823 :           if (loop_nest.exists ())
    5832      4500615 :             compute_affine_dependence (ddr, loop_nest[0]);
    5833              :         }
    5834              : 
    5835       431040 :   if (compute_self_and_rr)
    5836      1023224 :     FOR_EACH_VEC_ELT (datarefs, i, a)
    5837              :       {
    5838       760526 :         ddr = initialize_data_dependence_relation (a, a, loop_nest);
    5839       760526 :         dependence_relations->safe_push (ddr);
    5840       760526 :         if (loop_nest.exists ())
    5841       760526 :           compute_affine_dependence (ddr, loop_nest[0]);
    5842              :       }
    5843              : 
    5844              :   return true;
    5845              : }
    5846              : 
    5847              : /* Describes a location of a memory reference.  */
    5848              : 
    5849              : struct data_ref_loc
    5850              : {
    5851              :   /* The memory reference.  */
    5852              :   tree ref;
    5853              : 
    5854              :   /* True if the memory reference is read.  */
    5855              :   bool is_read;
    5856              : 
    5857              :   /* True if the data reference is conditional within the containing
    5858              :      statement, i.e. if it might not occur even when the statement
    5859              :      is executed and runs to completion.  */
    5860              :   bool is_conditional_in_stmt;
    5861              : };
    5862              : 
    5863              : 
    5864              : /* Stores the locations of memory references in STMT to REFERENCES.  Returns
    5865              :    true if STMT clobbers memory, false otherwise.  */
    5866              : 
    5867              : static bool
    5868     50640698 : get_references_in_stmt (gimple *stmt, vec<data_ref_loc, va_heap> *references)
    5869              : {
    5870     50640698 :   bool clobbers_memory = false;
    5871     50640698 :   data_ref_loc ref;
    5872     50640698 :   tree op0, op1;
    5873     50640698 :   enum gimple_code stmt_code = gimple_code (stmt);
    5874              : 
    5875              :   /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
    5876              :      As we cannot model data-references to not spelled out
    5877              :      accesses give up if they may occur.  */
    5878     50640698 :   if (stmt_code == GIMPLE_CALL
    5879     50640698 :       && !(gimple_call_flags (stmt) & ECF_CONST))
    5880              :     {
    5881              :       /* Allow IFN_GOMP_SIMD_LANE in their own loops.  */
    5882      4206960 :       if (gimple_call_internal_p (stmt))
    5883        59775 :         switch (gimple_call_internal_fn (stmt))
    5884              :           {
    5885         5603 :           case IFN_GOMP_SIMD_LANE:
    5886         5603 :             {
    5887         5603 :               class loop *loop = gimple_bb (stmt)->loop_father;
    5888         5603 :               tree uid = gimple_call_arg (stmt, 0);
    5889         5603 :               gcc_assert (TREE_CODE (uid) == SSA_NAME);
    5890         5603 :               if (loop == NULL
    5891         5603 :                   || loop->simduid != SSA_NAME_VAR (uid))
    5892              :                 clobbers_memory = true;
    5893              :               break;
    5894              :             }
    5895              :           case IFN_MASK_LOAD:
    5896              :           case IFN_MASK_STORE:
    5897              :           break;
    5898          999 :           case IFN_MASK_CALL:
    5899          999 :             {
    5900          999 :               tree orig_fndecl
    5901          999 :                 = gimple_call_addr_fndecl (gimple_call_arg (stmt, 0));
    5902          999 :               if (!orig_fndecl
    5903          999 :                   || (flags_from_decl_or_type (orig_fndecl) & ECF_CONST) == 0)
    5904              :                 clobbers_memory = true;
    5905              :             }
    5906              :             break;
    5907              :           default:
    5908      4250376 :             clobbers_memory = true;
    5909              :             break;
    5910              :           }
    5911      4147185 :       else if (gimple_call_builtin_p (stmt, BUILT_IN_PREFETCH))
    5912              :         clobbers_memory = false;
    5913              :       else
    5914      4250376 :         clobbers_memory = true;
    5915              :     }
    5916     46433738 :   else if (stmt_code == GIMPLE_ASM
    5917     46433738 :            && (gimple_asm_volatile_p (as_a <gasm *> (stmt))
    5918         8531 :                || gimple_vuse (stmt)))
    5919              :     clobbers_memory = true;
    5920              : 
    5921    105260761 :   if (!gimple_vuse (stmt))
    5922              :     return clobbers_memory;
    5923              : 
    5924     19477031 :   if (stmt_code == GIMPLE_ASSIGN)
    5925              :     {
    5926     14337535 :       tree base;
    5927     14337535 :       op0 = gimple_assign_lhs (stmt);
    5928     14337535 :       op1 = gimple_assign_rhs1 (stmt);
    5929              : 
    5930     14337535 :       if (DECL_P (op1)
    5931     14337535 :           || (REFERENCE_CLASS_P (op1)
    5932      6868105 :               && (base = get_base_address (op1))
    5933      6868105 :               && TREE_CODE (base) != SSA_NAME
    5934      6868037 :               && !is_gimple_min_invariant (base)))
    5935              :         {
    5936      7750875 :           ref.ref = op1;
    5937      7750875 :           ref.is_read = true;
    5938      7750875 :           ref.is_conditional_in_stmt = false;
    5939      7750875 :           references->safe_push (ref);
    5940              :         }
    5941              :     }
    5942      5139496 :   else if (stmt_code == GIMPLE_CALL)
    5943              :     {
    5944      4222678 :       unsigned i = 0, n;
    5945      4222678 :       tree ptr, type;
    5946      4222678 :       unsigned int align;
    5947              : 
    5948      4222678 :       ref.is_read = false;
    5949      4222678 :       if (gimple_call_internal_p (stmt))
    5950        74555 :         switch (gimple_call_internal_fn (stmt))
    5951              :           {
    5952         2164 :           case IFN_MASK_LOAD:
    5953         2164 :             if (gimple_call_lhs (stmt) == NULL_TREE)
    5954              :               break;
    5955         2164 :             ref.is_read = true;
    5956              :             /* FALLTHRU */
    5957         4006 :           case IFN_MASK_STORE:
    5958         4006 :             ptr = build_int_cst (TREE_TYPE (gimple_call_arg (stmt, 1)), 0);
    5959         4006 :             align = tree_to_shwi (gimple_call_arg (stmt, 1));
    5960         4006 :             if (ref.is_read)
    5961         2164 :               type = TREE_TYPE (gimple_call_lhs (stmt));
    5962              :             else
    5963         1842 :               type = TREE_TYPE (gimple_call_arg (stmt, 3));
    5964         4006 :             if (TYPE_ALIGN (type) != align)
    5965         1506 :               type = build_aligned_type (type, align);
    5966         4006 :             ref.is_conditional_in_stmt = true;
    5967         4006 :             ref.ref = fold_build2 (MEM_REF, type, gimple_call_arg (stmt, 0),
    5968              :                                    ptr);
    5969         4006 :             references->safe_push (ref);
    5970         4006 :             return false;
    5971              :           case IFN_MASK_CALL:
    5972      4218672 :             i = 1;
    5973              :             gcc_fallthrough ();
    5974              :           default:
    5975              :             break;
    5976              :           }
    5977              : 
    5978      4218672 :       op0 = gimple_call_lhs (stmt);
    5979      4218672 :       n = gimple_call_num_args (stmt);
    5980     17114639 :       for (; i < n; i++)
    5981              :         {
    5982      8677295 :           op1 = gimple_call_arg (stmt, i);
    5983              : 
    5984      8677295 :           if (DECL_P (op1)
    5985      8677295 :               || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
    5986              :             {
    5987       518136 :               ref.ref = op1;
    5988       518136 :               ref.is_read = true;
    5989       518136 :               ref.is_conditional_in_stmt = false;
    5990       518136 :               references->safe_push (ref);
    5991              :             }
    5992              :         }
    5993              :     }
    5994              :   else
    5995              :     return clobbers_memory;
    5996              : 
    5997     18556207 :   if (op0
    5998     18556207 :       && (DECL_P (op0)
    5999     15052198 :           || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
    6000              :     {
    6001      7428001 :       ref.ref = op0;
    6002      7428001 :       ref.is_read = false;
    6003      7428001 :       ref.is_conditional_in_stmt = false;
    6004      7428001 :       references->safe_push (ref);
    6005              :     }
    6006              :   return clobbers_memory;
    6007              : }
    6008              : 
    6009              : 
    6010              : /* Returns true if the loop-nest has any data reference.  */
    6011              : 
    6012              : bool
    6013          752 : loop_nest_has_data_refs (loop_p loop)
    6014              : {
    6015          752 :   basic_block *bbs = get_loop_body (loop);
    6016          752 :   auto_vec<data_ref_loc, 3> references;
    6017              : 
    6018         1001 :   for (unsigned i = 0; i < loop->num_nodes; i++)
    6019              :     {
    6020          931 :       basic_block bb = bbs[i];
    6021          931 :       gimple_stmt_iterator bsi;
    6022              : 
    6023         3224 :       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
    6024              :         {
    6025         2044 :           gimple *stmt = gsi_stmt (bsi);
    6026         2044 :           get_references_in_stmt (stmt, &references);
    6027         2044 :           if (references.length ())
    6028              :             {
    6029          682 :               free (bbs);
    6030          682 :               return true;
    6031              :             }
    6032              :         }
    6033              :     }
    6034           70 :   free (bbs);
    6035           70 :   return false;
    6036          752 : }
    6037              : 
    6038              : /* Stores the data references in STMT to DATAREFS.  If there is an unanalyzable
    6039              :    reference, returns false, otherwise returns true.  NEST is the outermost
    6040              :    loop of the loop nest in which the references should be analyzed.  */
    6041              : 
    6042              : opt_result
    6043     50624302 : find_data_references_in_stmt (class loop *nest, gimple *stmt,
    6044              :                               vec<data_reference_p> *datarefs)
    6045              : {
    6046     50624302 :   auto_vec<data_ref_loc, 2> references;
    6047     50624302 :   data_reference_p dr;
    6048              : 
    6049     50624302 :   if (get_references_in_stmt (stmt, &references))
    6050      4250372 :     return opt_result::failure_at (stmt, "statement clobbers memory: %G",
    6051              :                                    stmt);
    6052              : 
    6053    154038392 :   for (const data_ref_loc &ref : references)
    6054              :     {
    6055     14916602 :       dr = create_data_ref (nest ? loop_preheader_edge (nest) : NULL,
    6056     14916602 :                             loop_containing_stmt (stmt), ref.ref,
    6057     14916602 :                             stmt, ref.is_read, ref.is_conditional_in_stmt);
    6058     14916602 :       gcc_assert (dr != NULL);
    6059     14916602 :       datarefs->safe_push (dr);
    6060              :     }
    6061              : 
    6062     46373930 :   return opt_result::success ();
    6063     50624302 : }
    6064              : 
    6065              : /* Stores the data references in STMT to DATAREFS.  If there is an
    6066              :    unanalyzable reference, returns false, otherwise returns true.
    6067              :    NEST is the outermost loop of the loop nest in which the references
    6068              :    should be instantiated, LOOP is the loop in which the references
    6069              :    should be analyzed.  */
    6070              : 
    6071              : bool
    6072        14352 : graphite_find_data_references_in_stmt (edge nest, loop_p loop, gimple *stmt,
    6073              :                                        vec<data_reference_p> *datarefs)
    6074              : {
    6075        14352 :   auto_vec<data_ref_loc, 2> references;
    6076        14352 :   bool ret = true;
    6077        14352 :   data_reference_p dr;
    6078              : 
    6079        14352 :   if (get_references_in_stmt (stmt, &references))
    6080              :     return false;
    6081              : 
    6082        45964 :   for (const data_ref_loc &ref : references)
    6083              :     {
    6084         5840 :       dr = create_data_ref (nest, loop, ref.ref, stmt, ref.is_read,
    6085         2920 :                             ref.is_conditional_in_stmt);
    6086         2920 :       gcc_assert (dr != NULL);
    6087         2920 :       datarefs->safe_push (dr);
    6088              :     }
    6089              : 
    6090              :   return ret;
    6091        14352 : }
    6092              : 
    6093              : /* Search the data references in LOOP, and record the information into
    6094              :    DATAREFS.  Returns chrec_dont_know when failing to analyze a
    6095              :    difficult case, returns NULL_TREE otherwise.  */
    6096              : 
    6097              : tree
    6098      2714697 : find_data_references_in_bb (class loop *loop, basic_block bb,
    6099              :                             vec<data_reference_p> *datarefs)
    6100              : {
    6101      2714697 :   gimple_stmt_iterator bsi;
    6102              : 
    6103     22869112 :   for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
    6104              :     {
    6105     17935432 :       gimple *stmt = gsi_stmt (bsi);
    6106              : 
    6107     17935432 :       if (!find_data_references_in_stmt (loop, stmt, datarefs))
    6108              :         {
    6109       495714 :           struct data_reference *res;
    6110       495714 :           res = XCNEW (struct data_reference);
    6111       495714 :           datarefs->safe_push (res);
    6112              : 
    6113       495714 :           return chrec_dont_know;
    6114              :         }
    6115              :     }
    6116              : 
    6117              :   return NULL_TREE;
    6118              : }
    6119              : 
    6120              : /* Search the data references in LOOP, and record the information into
    6121              :    DATAREFS.  Returns chrec_dont_know when failing to analyze a
    6122              :    difficult case, returns NULL_TREE otherwise.
    6123              : 
    6124              :    TODO: This function should be made smarter so that it can handle address
    6125              :    arithmetic as if they were array accesses, etc.  */
    6126              : 
    6127              : tree
    6128       814248 : find_data_references_in_loop (class loop *loop,
    6129              :                               vec<data_reference_p> *datarefs)
    6130              : {
    6131       814248 :   basic_block bb, *bbs;
    6132       814248 :   unsigned int i;
    6133              : 
    6134       814248 :   bbs = get_loop_body_in_dom_order (loop);
    6135              : 
    6136      3632819 :   for (i = 0; i < loop->num_nodes; i++)
    6137              :     {
    6138      2302120 :       bb = bbs[i];
    6139              : 
    6140      2302120 :       if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
    6141              :         {
    6142       297797 :           free (bbs);
    6143       297797 :           return chrec_dont_know;
    6144              :         }
    6145              :     }
    6146       516451 :   free (bbs);
    6147              : 
    6148       516451 :   return NULL_TREE;
    6149              : }
    6150              : 
    6151              : /* Return the alignment in bytes that DRB is guaranteed to have at all
    6152              :    times.  */
    6153              : 
    6154              : unsigned int
    6155       480117 : dr_alignment (innermost_loop_behavior *drb)
    6156              : {
    6157              :   /* Get the alignment of BASE_ADDRESS + INIT.  */
    6158       480117 :   unsigned int alignment = drb->base_alignment;
    6159       480117 :   unsigned int misalignment = (drb->base_misalignment
    6160       480117 :                                + TREE_INT_CST_LOW (drb->init));
    6161       480117 :   if (misalignment != 0)
    6162       210784 :     alignment = MIN (alignment, misalignment & -misalignment);
    6163              : 
    6164              :   /* Cap it to the alignment of OFFSET.  */
    6165       480117 :   if (!integer_zerop (drb->offset))
    6166        35922 :     alignment = MIN (alignment, drb->offset_alignment);
    6167              : 
    6168              :   /* Cap it to the alignment of STEP.  */
    6169       480117 :   if (!integer_zerop (drb->step))
    6170       287231 :     alignment = MIN (alignment, drb->step_alignment);
    6171              : 
    6172       480117 :   return alignment;
    6173              : }
    6174              : 
    6175              : /* If BASE is a pointer-typed SSA name, try to find the object that it
    6176              :    is based on.  Return this object X on success and store the alignment
    6177              :    in bytes of BASE - &X in *ALIGNMENT_OUT.  */
    6178              : 
    6179              : static tree
    6180       743802 : get_base_for_alignment_1 (tree base, unsigned int *alignment_out)
    6181              : {
    6182       743802 :   if (TREE_CODE (base) != SSA_NAME || !POINTER_TYPE_P (TREE_TYPE (base)))
    6183              :     return NULL_TREE;
    6184              : 
    6185       367798 :   gimple *def = SSA_NAME_DEF_STMT (base);
    6186       367798 :   base = analyze_scalar_evolution (loop_containing_stmt (def), base);
    6187              : 
    6188              :   /* Peel chrecs and record the minimum alignment preserved by
    6189              :      all steps.  */
    6190       367798 :   unsigned int alignment = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
    6191       745697 :   while (TREE_CODE (base) == POLYNOMIAL_CHREC)
    6192              :     {
    6193        10101 :       unsigned int step_alignment = highest_pow2_factor (CHREC_RIGHT (base));
    6194        10101 :       alignment = MIN (alignment, step_alignment);
    6195        10101 :       base = CHREC_LEFT (base);
    6196              :     }
    6197              : 
    6198              :   /* Punt if the expression is too complicated to handle.  */
    6199       367798 :   if (tree_contains_chrecs (base, NULL) || !POINTER_TYPE_P (TREE_TYPE (base)))
    6200              :     return NULL_TREE;
    6201              : 
    6202              :   /* The only useful cases are those for which a dereference folds to something
    6203              :      other than an INDIRECT_REF.  */
    6204       367756 :   tree ref_type = TREE_TYPE (TREE_TYPE (base));
    6205       367756 :   tree ref = fold_indirect_ref_1 (UNKNOWN_LOCATION, ref_type, base);
    6206       367756 :   if (!ref)
    6207              :     return NULL_TREE;
    6208              : 
    6209              :   /* Analyze the base to which the steps we peeled were applied.  */
    6210         2627 :   poly_int64 bitsize, bitpos, bytepos;
    6211         2627 :   machine_mode mode;
    6212         2627 :   int unsignedp, reversep, volatilep;
    6213         2627 :   tree offset;
    6214         2627 :   base = get_inner_reference (ref, &bitsize, &bitpos, &offset, &mode,
    6215              :                               &unsignedp, &reversep, &volatilep);
    6216       743802 :   if (!base || !multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
    6217              :     return NULL_TREE;
    6218              : 
    6219              :   /* Restrict the alignment to that guaranteed by the offsets.  */
    6220         2627 :   unsigned int bytepos_alignment = known_alignment (bytepos);
    6221         2627 :   if (bytepos_alignment != 0)
    6222         2474 :     alignment = MIN (alignment, bytepos_alignment);
    6223         2627 :   if (offset)
    6224              :     {
    6225            0 :       unsigned int offset_alignment = highest_pow2_factor (offset);
    6226            0 :       alignment = MIN (alignment, offset_alignment);
    6227              :     }
    6228              : 
    6229         2627 :   *alignment_out = alignment;
    6230         2627 :   return base;
    6231              : }
    6232              : 
    6233              : /* Return the object whose alignment would need to be changed in order
    6234              :    to increase the alignment of ADDR.  Store the maximum achievable
    6235              :    alignment in *MAX_ALIGNMENT.  */
    6236              : 
    6237              : tree
    6238       743802 : get_base_for_alignment (tree addr, unsigned int *max_alignment)
    6239              : {
    6240       743802 :   tree base = get_base_for_alignment_1 (addr, max_alignment);
    6241       743802 :   if (base)
    6242              :     return base;
    6243              : 
    6244       741175 :   if (TREE_CODE (addr) == ADDR_EXPR)
    6245       276967 :     addr = TREE_OPERAND (addr, 0);
    6246       741175 :   *max_alignment = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
    6247       741175 :   return addr;
    6248              : }
    6249              : 
    6250              : /* Recursive helper function.  */
    6251              : 
    6252              : static bool
    6253       139987 : find_loop_nest_1 (class loop *loop, vec<loop_p> *loop_nest)
    6254              : {
    6255              :   /* Inner loops of the nest should not contain siblings.  Example:
    6256              :      when there are two consecutive loops,
    6257              : 
    6258              :      | loop_0
    6259              :      |   loop_1
    6260              :      |     A[{0, +, 1}_1]
    6261              :      |   endloop_1
    6262              :      |   loop_2
    6263              :      |     A[{0, +, 1}_2]
    6264              :      |   endloop_2
    6265              :      | endloop_0
    6266              : 
    6267              :      the dependence relation cannot be captured by the distance
    6268              :      abstraction.  */
    6269       139987 :   if (loop->next)
    6270              :     return false;
    6271              : 
    6272       117492 :   loop_nest->safe_push (loop);
    6273       117492 :   if (loop->inner)
    6274        41826 :     return find_loop_nest_1 (loop->inner, loop_nest);
    6275              :   return true;
    6276              : }
    6277              : 
    6278              : /* Return false when the LOOP is not well nested.  Otherwise return
    6279              :    true and insert in LOOP_NEST the loops of the nest.  LOOP_NEST will
    6280              :    contain the loops from the outermost to the innermost, as they will
    6281              :    appear in the classic distance vector.  */
    6282              : 
    6283              : bool
    6284      1023148 : find_loop_nest (class loop *loop, vec<loop_p> *loop_nest)
    6285              : {
    6286      1023148 :   loop_nest->safe_push (loop);
    6287      1023148 :   if (loop->inner)
    6288        98161 :     return find_loop_nest_1 (loop->inner, loop_nest);
    6289              :   return true;
    6290              : }
    6291              : 
    6292              : /* Returns true when the data dependences have been computed, false otherwise.
    6293              :    Given a loop nest LOOP, the following vectors are returned:
    6294              :    DATAREFS is initialized to all the array elements contained in this loop,
    6295              :    DEPENDENCE_RELATIONS contains the relations between the data references.
    6296              :    Compute read-read and self relations if
    6297              :    COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE.  */
    6298              : 
    6299              : bool
    6300       408979 : compute_data_dependences_for_loop (class loop *loop,
    6301              :                                    bool compute_self_and_read_read_dependences,
    6302              :                                    vec<loop_p> *loop_nest,
    6303              :                                    vec<data_reference_p> *datarefs,
    6304              :                                    vec<ddr_p> *dependence_relations)
    6305              : {
    6306       408979 :   bool res = true;
    6307              : 
    6308       408979 :   memset (&dependence_stats, 0, sizeof (dependence_stats));
    6309              : 
    6310              :   /* If the loop nest is not well formed, or one of the data references
    6311              :      is not computable, give up without spending time to compute other
    6312              :      dependences.  */
    6313       408979 :   if (!loop
    6314       408979 :       || !find_loop_nest (loop, loop_nest)
    6315       408977 :       || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
    6316       671611 :       || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
    6317              :                                    compute_self_and_read_read_dependences))
    6318              :     res = false;
    6319              : 
    6320       408979 :   if (dump_file && (dump_flags & TDF_STATS))
    6321              :     {
    6322          157 :       fprintf (dump_file, "Dependence tester statistics:\n");
    6323              : 
    6324          157 :       fprintf (dump_file, "Number of dependence tests: %d\n",
    6325              :                dependence_stats.num_dependence_tests);
    6326          157 :       fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
    6327              :                dependence_stats.num_dependence_dependent);
    6328          157 :       fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
    6329              :                dependence_stats.num_dependence_independent);
    6330          157 :       fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
    6331              :                dependence_stats.num_dependence_undetermined);
    6332              : 
    6333          157 :       fprintf (dump_file, "Number of subscript tests: %d\n",
    6334              :                dependence_stats.num_subscript_tests);
    6335          157 :       fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
    6336              :                dependence_stats.num_subscript_undetermined);
    6337          157 :       fprintf (dump_file, "Number of same subscript function: %d\n",
    6338              :                dependence_stats.num_same_subscript_function);
    6339              : 
    6340          157 :       fprintf (dump_file, "Number of ziv tests: %d\n",
    6341              :                dependence_stats.num_ziv);
    6342          157 :       fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
    6343              :                dependence_stats.num_ziv_dependent);
    6344          157 :       fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
    6345              :                dependence_stats.num_ziv_independent);
    6346          157 :       fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
    6347              :                dependence_stats.num_ziv_unimplemented);
    6348              : 
    6349          157 :       fprintf (dump_file, "Number of siv tests: %d\n",
    6350              :                dependence_stats.num_siv);
    6351          157 :       fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
    6352              :                dependence_stats.num_siv_dependent);
    6353          157 :       fprintf (dump_file, "Number of siv tests returning independent: %d\n",
    6354              :                dependence_stats.num_siv_independent);
    6355          157 :       fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
    6356              :                dependence_stats.num_siv_unimplemented);
    6357              : 
    6358          157 :       fprintf (dump_file, "Number of miv tests: %d\n",
    6359              :                dependence_stats.num_miv);
    6360          157 :       fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
    6361              :                dependence_stats.num_miv_dependent);
    6362          157 :       fprintf (dump_file, "Number of miv tests returning independent: %d\n",
    6363              :                dependence_stats.num_miv_independent);
    6364          157 :       fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
    6365              :                dependence_stats.num_miv_unimplemented);
    6366              :     }
    6367              : 
    6368       408979 :   return res;
    6369              : }
    6370              : 
    6371              : /* Free the memory used by a data dependence relation DDR.  */
    6372              : 
    6373              : void
    6374     13394350 : free_dependence_relation (struct data_dependence_relation *ddr)
    6375              : {
    6376     13394350 :   if (ddr == NULL)
    6377              :     return;
    6378              : 
    6379     13394350 :   if (DDR_SUBSCRIPTS (ddr).exists ())
    6380       908828 :     free_subscripts (DDR_SUBSCRIPTS (ddr));
    6381     13394350 :   DDR_DIST_VECTS (ddr).release ();
    6382     13394350 :   DDR_DIR_VECTS (ddr).release ();
    6383              : 
    6384     13394350 :   free (ddr);
    6385              : }
    6386              : 
    6387              : /* Free the memory used by the data dependence relations from
    6388              :    DEPENDENCE_RELATIONS.  */
    6389              : 
    6390              : void
    6391      2892045 : free_dependence_relations (vec<ddr_p>& dependence_relations)
    6392              : {
    6393      9350081 :   for (data_dependence_relation *ddr : dependence_relations)
    6394      5286608 :     if (ddr)
    6395      5286608 :       free_dependence_relation (ddr);
    6396              : 
    6397      2892045 :   dependence_relations.release ();
    6398      2892045 : }
    6399              : 
    6400              : /* Free the memory used by the data references from DATAREFS.  */
    6401              : 
    6402              : void
    6403      3552109 : free_data_refs (vec<data_reference_p>& datarefs)
    6404              : {
    6405     21311017 :   for (data_reference *dr : datarefs)
    6406     13338058 :     free_data_ref (dr);
    6407      3552109 :   datarefs.release ();
    6408      3552109 : }
    6409              : 
    6410              : /* Common routine implementing both dr_direction_indicator and
    6411              :    dr_zero_step_indicator.  Return USEFUL_MIN if the indicator is known
    6412              :    to be >= USEFUL_MIN and -1 if the indicator is known to be negative.
    6413              :    Return the step as the indicator otherwise.  */
    6414              : 
    6415              : static tree
    6416        66445 : dr_step_indicator (struct data_reference *dr, int useful_min)
    6417              : {
    6418        66445 :   tree step = DR_STEP (dr);
    6419        66445 :   if (!step)
    6420              :     return NULL_TREE;
    6421        66445 :   STRIP_NOPS (step);
    6422              :   /* Look for cases where the step is scaled by a positive constant
    6423              :      integer, which will often be the access size.  If the multiplication
    6424              :      doesn't change the sign (due to overflow effects) then we can
    6425              :      test the unscaled value instead.  */
    6426        66445 :   if (TREE_CODE (step) == MULT_EXPR
    6427         5496 :       && TREE_CODE (TREE_OPERAND (step, 1)) == INTEGER_CST
    6428        71885 :       && tree_int_cst_sgn (TREE_OPERAND (step, 1)) > 0)
    6429              :     {
    6430         5440 :       tree factor = TREE_OPERAND (step, 1);
    6431         5440 :       step = TREE_OPERAND (step, 0);
    6432              : 
    6433              :       /* Strip widening and truncating conversions as well as nops.  */
    6434         1214 :       if (CONVERT_EXPR_P (step)
    6435         5440 :           && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (step, 0))))
    6436         4226 :         step = TREE_OPERAND (step, 0);
    6437         5440 :       tree type = TREE_TYPE (step);
    6438              : 
    6439              :       /* Get the range of step values that would not cause overflow.  */
    6440        10880 :       widest_int minv = (wi::to_widest (TYPE_MIN_VALUE (ssizetype))
    6441         5440 :                          / wi::to_widest (factor));
    6442        10880 :       widest_int maxv = (wi::to_widest (TYPE_MAX_VALUE (ssizetype))
    6443         5440 :                          / wi::to_widest (factor));
    6444              : 
    6445              :       /* Get the range of values that the unconverted step actually has.  */
    6446         5440 :       wide_int step_min, step_max;
    6447         5440 :       int_range_max vr;
    6448         5440 :       if (TREE_CODE (step) != SSA_NAME
    6449        10772 :           || !get_range_query (cfun)->range_of_expr (vr, step)
    6450        10826 :           || vr.undefined_p ())
    6451              :         {
    6452           54 :           step_min = wi::to_wide (TYPE_MIN_VALUE (type));
    6453           54 :           step_max = wi::to_wide (TYPE_MAX_VALUE (type));
    6454              :         }
    6455              :       else
    6456              :         {
    6457         5386 :           step_min = vr.lower_bound ();
    6458         5386 :           step_max = vr.upper_bound ();
    6459              :         }
    6460              : 
    6461              :       /* Check whether the unconverted step has an acceptable range.  */
    6462         5440 :       signop sgn = TYPE_SIGN (type);
    6463        10880 :       if (wi::les_p (minv, widest_int::from (step_min, sgn))
    6464        14004 :           && wi::ges_p (maxv, widest_int::from (step_max, sgn)))
    6465              :         {
    6466         1553 :           if (wi::ge_p (step_min, useful_min, sgn))
    6467          436 :             return ssize_int (useful_min);
    6468         1117 :           else if (wi::lt_p (step_max, 0, sgn))
    6469            0 :             return ssize_int (-1);
    6470              :           else
    6471         1117 :             return fold_convert (ssizetype, step);
    6472              :         }
    6473         5440 :     }
    6474        64892 :   return DR_STEP (dr);
    6475              : }
    6476              : 
    6477              : /* Return a value that is negative iff DR has a negative step.  */
    6478              : 
    6479              : tree
    6480        11853 : dr_direction_indicator (struct data_reference *dr)
    6481              : {
    6482        11853 :   return dr_step_indicator (dr, 0);
    6483              : }
    6484              : 
    6485              : /* Return a value that is zero iff DR has a zero step.  */
    6486              : 
    6487              : tree
    6488        54592 : dr_zero_step_indicator (struct data_reference *dr)
    6489              : {
    6490        54592 :   return dr_step_indicator (dr, 1);
    6491              : }
    6492              : 
    6493              : /* Return true if DR is known to have a nonnegative (but possibly zero)
    6494              :    step.  */
    6495              : 
    6496              : bool
    6497         4991 : dr_known_forward_stride_p (struct data_reference *dr)
    6498              : {
    6499         4991 :   tree indicator = dr_direction_indicator (dr);
    6500         4991 :   tree neg_step_val = fold_binary (LT_EXPR, boolean_type_node,
    6501              :                                    fold_convert (ssizetype, indicator),
    6502              :                                    ssize_int (0));
    6503         4991 :   return neg_step_val && integer_zerop (neg_step_val);
    6504              : }
        

Generated by: LCOV version 2.4-beta

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