]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-data-ref.c
Update copyright years.
[thirdparty/gcc.git] / gcc / tree-data-ref.c
CommitLineData
2146e26d 1/* Data references and dependences detectors.
f1717362 2 Copyright (C) 2003-2016 Free Software Foundation, Inc.
6b421feb 3 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
2146e26d 4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
8c4c00c1 9Software Foundation; either version 3, or (at your option) any later
2146e26d 10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
8c4c00c1 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
2146e26d 20
21/* This pass walks a given loop structure searching for array
22 references. The information about the array accesses is recorded
48e1416a 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
2146e26d 28 the array is accessed twice at iterations x and y if and only if:
29 | chrec1 (x) == chrec2 (y).
48e1416a 30
2146e26d 31 The goals of this analysis are:
48e1416a 32
2146e26d 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),
48e1416a 36
2146e26d 37 - when two data references access the same data, to qualify the
38 dependence relation with classic dependence representations:
48e1416a 39
2146e26d 40 - distance vectors
41 - direction vectors
42 - loop carried level dependence
43 - polyhedron dependence
44 or with the chains of recurrences based representation,
48e1416a 45
46 - to define a knowledge base for storing the data dependence
2146e26d 47 information,
48e1416a 48
2146e26d 49 - to define an interface to access this data.
48e1416a 50
51
2146e26d 52 Definitions:
48e1416a 53
2146e26d 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
48e1416a 60 solutions are integer constants, for example the equation
2146e26d 61 | 3*x + 2*y = 1
62 has an integer solution x = 1 and y = -1.
48e1416a 63
2146e26d 64 References:
48e1416a 65
2146e26d 66 - "Advanced Compilation for High Performance Computing" by Randy
67 Allen and Ken Kennedy.
48e1416a 68 http://citeseer.ist.psu.edu/goff91practical.html
69
70 - "Loop Transformations for Restructuring Compilers - The Foundations"
2146e26d 71 by Utpal Banerjee.
72
48e1416a 73
2146e26d 74*/
75
76#include "config.h"
77#include "system.h"
78#include "coretypes.h"
9ef16211 79#include "backend.h"
7c29e30e 80#include "rtl.h"
0d9585ca 81#include "tree.h"
9ef16211 82#include "gimple.h"
7c29e30e 83#include "gimple-pretty-print.h"
84#include "alias.h"
9ef16211 85#include "fold-const.h"
d53441c8 86#include "expr.h"
dcf1a1ec 87#include "gimple-iterator.h"
05d9c18a 88#include "tree-ssa-loop-niter.h"
073c1fd5 89#include "tree-ssa-loop.h"
69ee5dbb 90#include "tree-ssa.h"
2146e26d 91#include "cfgloop.h"
2146e26d 92#include "tree-data-ref.h"
93#include "tree-scalar-evolution.h"
b9ed1410 94#include "dumpfile.h"
5fc88ffd 95#include "tree-affine.h"
c7af8ae7 96#include "params.h"
2146e26d 97
6b421feb 98static struct datadep_stats
99{
100 int num_dependence_tests;
101 int num_dependence_dependent;
102 int num_dependence_independent;
103 int num_dependence_undetermined;
104
105 int num_subscript_tests;
106 int num_subscript_undetermined;
107 int num_same_subscript_function;
108
109 int num_ziv;
110 int num_ziv_independent;
111 int num_ziv_dependent;
112 int num_ziv_unimplemented;
113
114 int num_siv;
115 int num_siv_independent;
116 int num_siv_dependent;
117 int num_siv_unimplemented;
118
119 int num_miv;
120 int num_miv_independent;
121 int num_miv_dependent;
122 int num_miv_unimplemented;
123} dependence_stats;
124
b44d1046 125static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
126 struct data_reference *,
8c7b1d8e 127 struct data_reference *,
128 struct loop *);
2146e26d 129/* Returns true iff A divides B. */
130
48e1416a 131static inline bool
7ecb5bb2 132tree_fold_divides_p (const_tree a, const_tree b)
2146e26d 133{
426a138f 134 gcc_assert (TREE_CODE (a) == INTEGER_CST);
135 gcc_assert (TREE_CODE (b) == INTEGER_CST);
317e2a67 136 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a));
2146e26d 137}
138
bc3c8ad4 139/* Returns true iff A divides B. */
140
48e1416a 141static inline bool
bc3c8ad4 142int_divides_p (int a, int b)
143{
144 return ((b % a) == 0);
2146e26d 145}
146
147\f
148
48e1416a 149/* Dump into FILE all the data references from DATAREFS. */
2146e26d 150
d129ada0 151static void
f1f41a6c 152dump_data_references (FILE *file, vec<data_reference_p> datarefs)
2146e26d 153{
154 unsigned int i;
41c7a324 155 struct data_reference *dr;
156
f1f41a6c 157 FOR_EACH_VEC_ELT (datarefs, i, dr)
41c7a324 158 dump_data_reference (file, dr);
2146e26d 159}
160
c7d89805 161/* Unified dump into FILE all the data references from DATAREFS. */
162
163DEBUG_FUNCTION void
164debug (vec<data_reference_p> &ref)
165{
166 dump_data_references (stderr, ref);
167}
168
169DEBUG_FUNCTION void
170debug (vec<data_reference_p> *ptr)
171{
172 if (ptr)
173 debug (*ptr);
174 else
175 fprintf (stderr, "<nil>\n");
176}
177
178
48e1416a 179/* Dump into STDERR all the data references from DATAREFS. */
5df4cc8d 180
4b987fac 181DEBUG_FUNCTION void
f1f41a6c 182debug_data_references (vec<data_reference_p> datarefs)
5df4cc8d 183{
184 dump_data_references (stderr, datarefs);
185}
186
5df4cc8d 187/* Print to STDERR the data_reference DR. */
188
4b987fac 189DEBUG_FUNCTION void
5df4cc8d 190debug_data_reference (struct data_reference *dr)
191{
192 dump_data_reference (stderr, dr);
193}
194
2146e26d 195/* Dump function for a DATA_REFERENCE structure. */
196
48e1416a 197void
198dump_data_reference (FILE *outf,
2146e26d 199 struct data_reference *dr)
200{
201 unsigned int i;
48e1416a 202
b40e3432 203 fprintf (outf, "#(Data Ref: \n");
204 fprintf (outf, "# bb: %d \n", gimple_bb (DR_STMT (dr))->index);
205 fprintf (outf, "# stmt: ");
75a70cf9 206 print_gimple_stmt (outf, DR_STMT (dr), 0, 0);
5dc5fe13 207 fprintf (outf, "# ref: ");
2146e26d 208 print_generic_stmt (outf, DR_REF (dr), 0);
5dc5fe13 209 fprintf (outf, "# base_object: ");
516849c7 210 print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
48e1416a 211
2146e26d 212 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
213 {
5dc5fe13 214 fprintf (outf, "# Access function %d: ", i);
2146e26d 215 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
216 }
5dc5fe13 217 fprintf (outf, "#)\n");
2146e26d 218}
219
c7d89805 220/* Unified dump function for a DATA_REFERENCE structure. */
221
222DEBUG_FUNCTION void
223debug (data_reference &ref)
224{
225 dump_data_reference (stderr, &ref);
226}
227
228DEBUG_FUNCTION void
229debug (data_reference *ptr)
230{
231 if (ptr)
232 debug (*ptr);
233 else
234 fprintf (stderr, "<nil>\n");
235}
236
237
87da4f2e 238/* Dumps the affine function described by FN to the file OUTF. */
239
0d8001a7 240DEBUG_FUNCTION void
87da4f2e 241dump_affine_function (FILE *outf, affine_fn fn)
242{
243 unsigned i;
244 tree coef;
245
f1f41a6c 246 print_generic_expr (outf, fn[0], TDF_SLIM);
247 for (i = 1; fn.iterate (i, &coef); i++)
87da4f2e 248 {
249 fprintf (outf, " + ");
250 print_generic_expr (outf, coef, TDF_SLIM);
251 fprintf (outf, " * x_%u", i);
252 }
253}
254
255/* Dumps the conflict function CF to the file OUTF. */
256
0d8001a7 257DEBUG_FUNCTION void
87da4f2e 258dump_conflict_function (FILE *outf, conflict_function *cf)
259{
260 unsigned i;
261
262 if (cf->n == NO_DEPENDENCE)
984cebc1 263 fprintf (outf, "no dependence");
87da4f2e 264 else if (cf->n == NOT_KNOWN)
984cebc1 265 fprintf (outf, "not known");
87da4f2e 266 else
267 {
268 for (i = 0; i < cf->n; i++)
269 {
984cebc1 270 if (i != 0)
271 fprintf (outf, " ");
87da4f2e 272 fprintf (outf, "[");
273 dump_affine_function (outf, cf->fns[i]);
984cebc1 274 fprintf (outf, "]");
87da4f2e 275 }
276 }
277}
278
bc3c8ad4 279/* Dump function for a SUBSCRIPT structure. */
280
0d8001a7 281DEBUG_FUNCTION void
bc3c8ad4 282dump_subscript (FILE *outf, struct subscript *subscript)
283{
87da4f2e 284 conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
bc3c8ad4 285
286 fprintf (outf, "\n (subscript \n");
287 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
87da4f2e 288 dump_conflict_function (outf, cf);
289 if (CF_NONTRIVIAL_P (cf))
bc3c8ad4 290 {
291 tree last_iteration = SUB_LAST_CONFLICT (subscript);
984cebc1 292 fprintf (outf, "\n last_conflict: ");
293 print_generic_expr (outf, last_iteration, 0);
bc3c8ad4 294 }
48e1416a 295
87da4f2e 296 cf = SUB_CONFLICTS_IN_B (subscript);
984cebc1 297 fprintf (outf, "\n iterations_that_access_an_element_twice_in_B: ");
87da4f2e 298 dump_conflict_function (outf, cf);
299 if (CF_NONTRIVIAL_P (cf))
bc3c8ad4 300 {
301 tree last_iteration = SUB_LAST_CONFLICT (subscript);
984cebc1 302 fprintf (outf, "\n last_conflict: ");
303 print_generic_expr (outf, last_iteration, 0);
bc3c8ad4 304 }
305
984cebc1 306 fprintf (outf, "\n (Subscript distance: ");
307 print_generic_expr (outf, SUB_DISTANCE (subscript), 0);
308 fprintf (outf, " ))\n");
bc3c8ad4 309}
310
6b421feb 311/* Print the classic direction vector DIRV to OUTF. */
312
0d8001a7 313DEBUG_FUNCTION void
6b421feb 314print_direction_vector (FILE *outf,
315 lambda_vector dirv,
316 int length)
317{
318 int eq;
319
320 for (eq = 0; eq < length; eq++)
321 {
bc620c5c 322 enum data_dependence_direction dir = ((enum data_dependence_direction)
323 dirv[eq]);
6b421feb 324
325 switch (dir)
326 {
327 case dir_positive:
328 fprintf (outf, " +");
329 break;
330 case dir_negative:
331 fprintf (outf, " -");
332 break;
333 case dir_equal:
334 fprintf (outf, " =");
335 break;
336 case dir_positive_or_equal:
337 fprintf (outf, " +=");
338 break;
339 case dir_positive_or_negative:
340 fprintf (outf, " +-");
341 break;
342 case dir_negative_or_equal:
343 fprintf (outf, " -=");
344 break;
345 case dir_star:
346 fprintf (outf, " *");
347 break;
348 default:
349 fprintf (outf, "indep");
350 break;
351 }
352 }
353 fprintf (outf, "\n");
354}
355
b44d1046 356/* Print a vector of direction vectors. */
357
0d8001a7 358DEBUG_FUNCTION void
f1f41a6c 359print_dir_vectors (FILE *outf, vec<lambda_vector> dir_vects,
b44d1046 360 int length)
361{
362 unsigned j;
363 lambda_vector v;
364
f1f41a6c 365 FOR_EACH_VEC_ELT (dir_vects, j, v)
b44d1046 366 print_direction_vector (outf, v, length);
367}
368
e01f9f1f 369/* Print out a vector VEC of length N to OUTFILE. */
370
0d8001a7 371DEBUG_FUNCTION void
e01f9f1f 372print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
373{
374 int i;
375
376 for (i = 0; i < n; i++)
377 fprintf (outfile, "%3d ", vector[i]);
378 fprintf (outfile, "\n");
379}
380
b44d1046 381/* Print a vector of distance vectors. */
382
0d8001a7 383DEBUG_FUNCTION void
f1f41a6c 384print_dist_vectors (FILE *outf, vec<lambda_vector> dist_vects,
d129ada0 385 int length)
b44d1046 386{
387 unsigned j;
388 lambda_vector v;
389
f1f41a6c 390 FOR_EACH_VEC_ELT (dist_vects, j, v)
b44d1046 391 print_lambda_vector (outf, v, length);
392}
393
2146e26d 394/* Dump function for a DATA_DEPENDENCE_RELATION structure. */
395
0d8001a7 396DEBUG_FUNCTION void
48e1416a 397dump_data_dependence_relation (FILE *outf,
2146e26d 398 struct data_dependence_relation *ddr)
399{
2146e26d 400 struct data_reference *dra, *drb;
bc3c8ad4 401
bc3c8ad4 402 fprintf (outf, "(Data Dep: \n");
801c5610 403
4cc34f6b 404 if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
405 {
be2e5c02 406 if (ddr)
407 {
408 dra = DDR_A (ddr);
409 drb = DDR_B (ddr);
410 if (dra)
411 dump_data_reference (outf, dra);
412 else
413 fprintf (outf, " (nil)\n");
414 if (drb)
415 dump_data_reference (outf, drb);
416 else
417 fprintf (outf, " (nil)\n");
418 }
4cc34f6b 419 fprintf (outf, " (don't know)\n)\n");
420 return;
421 }
422
423 dra = DDR_A (ddr);
424 drb = DDR_B (ddr);
801c5610 425 dump_data_reference (outf, dra);
426 dump_data_reference (outf, drb);
427
4cc34f6b 428 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
2146e26d 429 fprintf (outf, " (no dependence)\n");
48e1416a 430
bc3c8ad4 431 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
2146e26d 432 {
bc3c8ad4 433 unsigned int i;
b44d1046 434 struct loop *loopi;
1532ec98 435
2146e26d 436 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
437 {
2146e26d 438 fprintf (outf, " access_fn_A: ");
439 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
440 fprintf (outf, " access_fn_B: ");
441 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
bc3c8ad4 442 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
2146e26d 443 }
1532ec98 444
355572cc 445 fprintf (outf, " inner loop index: %d\n", DDR_INNER_LOOP (ddr));
b44d1046 446 fprintf (outf, " loop nest: (");
f1f41a6c 447 FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
b44d1046 448 fprintf (outf, "%d ", loopi->num);
449 fprintf (outf, ")\n");
450
1532ec98 451 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
2146e26d 452 {
1532ec98 453 fprintf (outf, " distance_vector: ");
454 print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
b44d1046 455 DDR_NB_LOOPS (ddr));
bc3c8ad4 456 }
1532ec98 457
458 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
bc3c8ad4 459 {
1532ec98 460 fprintf (outf, " direction_vector: ");
6b421feb 461 print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
b44d1046 462 DDR_NB_LOOPS (ddr));
2146e26d 463 }
2146e26d 464 }
465
466 fprintf (outf, ")\n");
467}
468
d129ada0 469/* Debug version. */
2146e26d 470
d129ada0 471DEBUG_FUNCTION void
472debug_data_dependence_relation (struct data_dependence_relation *ddr)
2146e26d 473{
d129ada0 474 dump_data_dependence_relation (stderr, ddr);
475}
48e1416a 476
d129ada0 477/* Dump into FILE all the dependence relations from DDRS. */
48e1416a 478
0d8001a7 479DEBUG_FUNCTION void
d129ada0 480dump_data_dependence_relations (FILE *file,
f1f41a6c 481 vec<ddr_p> ddrs)
d129ada0 482{
483 unsigned int i;
484 struct data_dependence_relation *ddr;
48e1416a 485
f1f41a6c 486 FOR_EACH_VEC_ELT (ddrs, i, ddr)
d129ada0 487 dump_data_dependence_relation (file, ddr);
488}
48e1416a 489
c7d89805 490DEBUG_FUNCTION void
491debug (vec<ddr_p> &ref)
492{
493 dump_data_dependence_relations (stderr, ref);
494}
495
496DEBUG_FUNCTION void
497debug (vec<ddr_p> *ptr)
498{
499 if (ptr)
500 debug (*ptr);
501 else
502 fprintf (stderr, "<nil>\n");
503}
504
505
d129ada0 506/* Dump to STDERR all the dependence relations from DDRS. */
48e1416a 507
d129ada0 508DEBUG_FUNCTION void
f1f41a6c 509debug_data_dependence_relations (vec<ddr_p> ddrs)
d129ada0 510{
511 dump_data_dependence_relations (stderr, ddrs);
2146e26d 512}
513
bc3c8ad4 514/* Dumps the distance and direction vectors in FILE. DDRS contains
515 the dependence relations, and VECT_SIZE is the size of the
516 dependence vectors, or in other words the number of loops in the
517 considered nest. */
518
0d8001a7 519DEBUG_FUNCTION void
f1f41a6c 520dump_dist_dir_vectors (FILE *file, vec<ddr_p> ddrs)
bc3c8ad4 521{
1532ec98 522 unsigned int i, j;
41c7a324 523 struct data_dependence_relation *ddr;
524 lambda_vector v;
bc3c8ad4 525
f1f41a6c 526 FOR_EACH_VEC_ELT (ddrs, i, ddr)
41c7a324 527 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
528 {
f1f41a6c 529 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), j, v)
41c7a324 530 {
531 fprintf (file, "DISTANCE_V (");
532 print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
533 fprintf (file, ")\n");
534 }
535
f1f41a6c 536 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), j, v)
41c7a324 537 {
538 fprintf (file, "DIRECTION_V (");
539 print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
540 fprintf (file, ")\n");
541 }
542 }
1532ec98 543
bc3c8ad4 544 fprintf (file, "\n\n");
545}
546
547/* Dumps the data dependence relations DDRS in FILE. */
548
0d8001a7 549DEBUG_FUNCTION void
f1f41a6c 550dump_ddrs (FILE *file, vec<ddr_p> ddrs)
bc3c8ad4 551{
552 unsigned int i;
41c7a324 553 struct data_dependence_relation *ddr;
554
f1f41a6c 555 FOR_EACH_VEC_ELT (ddrs, i, ddr)
41c7a324 556 dump_data_dependence_relation (file, ddr);
bc3c8ad4 557
bc3c8ad4 558 fprintf (file, "\n\n");
559}
560
d129ada0 561DEBUG_FUNCTION void
f1f41a6c 562debug_ddrs (vec<ddr_p> ddrs)
d129ada0 563{
564 dump_ddrs (stderr, ddrs);
565}
566
75a70cf9 567/* Helper function for split_constant_offset. Expresses OP0 CODE OP1
568 (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
569 constant of type ssizetype, and returns true. If we cannot do this
570 with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
571 is returned. */
516849c7 572
75a70cf9 573static bool
574split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
575 tree *var, tree *off)
516849c7 576{
80bb306a 577 tree var0, var1;
578 tree off0, off1;
75a70cf9 579 enum tree_code ocode = code;
516849c7 580
75a70cf9 581 *var = NULL_TREE;
582 *off = NULL_TREE;
516849c7 583
0de36bdb 584 switch (code)
516849c7 585 {
80bb306a 586 case INTEGER_CST:
587 *var = build_int_cst (type, 0);
75a70cf9 588 *off = fold_convert (ssizetype, op0);
589 return true;
516849c7 590
0de36bdb 591 case POINTER_PLUS_EXPR:
75a70cf9 592 ocode = PLUS_EXPR;
0de36bdb 593 /* FALLTHROUGH */
80bb306a 594 case PLUS_EXPR:
595 case MINUS_EXPR:
75a70cf9 596 split_constant_offset (op0, &var0, &off0);
597 split_constant_offset (op1, &var1, &off1);
598 *var = fold_build2 (code, type, var0, var1);
599 *off = size_binop (ocode, off0, off1);
600 return true;
516849c7 601
516849c7 602 case MULT_EXPR:
75a70cf9 603 if (TREE_CODE (op1) != INTEGER_CST)
604 return false;
80bb306a 605
75a70cf9 606 split_constant_offset (op0, &var0, &off0);
607 *var = fold_build2 (MULT_EXPR, type, var0, op1);
608 *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
609 return true;
516849c7 610
80bb306a 611 case ADDR_EXPR:
612 {
75a70cf9 613 tree base, poffset;
80bb306a 614 HOST_WIDE_INT pbitsize, pbitpos;
3754d046 615 machine_mode pmode;
292237f3 616 int punsignedp, preversep, pvolatilep;
516849c7 617
97d819e5 618 op0 = TREE_OPERAND (op0, 0);
292237f3 619 base
620 = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset, &pmode,
621 &punsignedp, &preversep, &pvolatilep, false);
516849c7 622
80bb306a 623 if (pbitpos % BITS_PER_UNIT != 0)
75a70cf9 624 return false;
80bb306a 625 base = build_fold_addr_expr (base);
626 off0 = ssize_int (pbitpos / BITS_PER_UNIT);
516849c7 627
80bb306a 628 if (poffset)
629 {
630 split_constant_offset (poffset, &poffset, &off1);
631 off0 = size_binop (PLUS_EXPR, off0, off1);
55ed4df6 632 if (POINTER_TYPE_P (TREE_TYPE (base)))
2cc66f2a 633 base = fold_build_pointer_plus (base, poffset);
55ed4df6 634 else
635 base = fold_build2 (PLUS_EXPR, TREE_TYPE (base), base,
636 fold_convert (TREE_TYPE (base), poffset));
80bb306a 637 }
638
69a83716 639 var0 = fold_convert (type, base);
640
641 /* If variable length types are involved, punt, otherwise casts
642 might be converted into ARRAY_REFs in gimplify_conversion.
643 To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
644 possibly no longer appears in current GIMPLE, might resurface.
645 This perhaps could run
d9659041 646 if (CONVERT_EXPR_P (var0))
69a83716 647 {
648 gimplify_conversion (&var0);
649 // Attempt to fill in any within var0 found ARRAY_REF's
650 // element size from corresponding op embedded ARRAY_REF,
651 // if unsuccessful, just punt.
652 } */
653 while (POINTER_TYPE_P (type))
654 type = TREE_TYPE (type);
655 if (int_size_in_bytes (type) < 0)
75a70cf9 656 return false;
69a83716 657
658 *var = var0;
80bb306a 659 *off = off0;
75a70cf9 660 return true;
80bb306a 661 }
516849c7 662
a091367a 663 case SSA_NAME:
664 {
520f63be 665 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
666 return false;
667
42acab1c 668 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
75a70cf9 669 enum tree_code subcode;
a091367a 670
75a70cf9 671 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
672 return false;
673
674 var0 = gimple_assign_rhs1 (def_stmt);
675 subcode = gimple_assign_rhs_code (def_stmt);
676 var1 = gimple_assign_rhs2 (def_stmt);
677
678 return split_constant_offset_1 (type, var0, subcode, var1, var, off);
a091367a 679 }
be2e5c02 680 CASE_CONVERT:
681 {
682 /* We must not introduce undefined overflow, and we must not change the value.
683 Hence we're okay if the inner type doesn't overflow to start with
684 (pointer or signed), the outer type also is an integer or pointer
685 and the outer precision is at least as large as the inner. */
686 tree itype = TREE_TYPE (op0);
687 if ((POINTER_TYPE_P (itype)
688 || (INTEGRAL_TYPE_P (itype) && TYPE_OVERFLOW_UNDEFINED (itype)))
689 && TYPE_PRECISION (type) >= TYPE_PRECISION (itype)
690 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)))
691 {
692 split_constant_offset (op0, &var0, off);
693 *var = fold_convert (type, var0);
694 return true;
695 }
696 return false;
697 }
a091367a 698
516849c7 699 default:
75a70cf9 700 return false;
516849c7 701 }
75a70cf9 702}
703
704/* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
705 will be ssizetype. */
706
707void
708split_constant_offset (tree exp, tree *var, tree *off)
709{
710 tree type = TREE_TYPE (exp), otype, op0, op1, e, o;
711 enum tree_code code;
516849c7 712
75a70cf9 713 *var = exp;
80bb306a 714 *off = ssize_int (0);
75a70cf9 715 STRIP_NOPS (exp);
716
32246fd4 717 if (tree_is_chrec (exp)
718 || get_gimple_rhs_class (TREE_CODE (exp)) == GIMPLE_TERNARY_RHS)
75a70cf9 719 return;
720
721 otype = TREE_TYPE (exp);
722 code = TREE_CODE (exp);
723 extract_ops_from_tree (exp, &code, &op0, &op1);
724 if (split_constant_offset_1 (otype, op0, code, op1, &e, &o))
725 {
726 *var = fold_convert (type, e);
727 *off = o;
728 }
516849c7 729}
730
80bb306a 731/* Returns the address ADDR of an object in a canonical shape (without nop
732 casts, and with type of pointer to the object). */
516849c7 733
734static tree
80bb306a 735canonicalize_base_object_address (tree addr)
516849c7 736{
ad4a85ad 737 tree orig = addr;
738
80bb306a 739 STRIP_NOPS (addr);
516849c7 740
ad4a85ad 741 /* The base address may be obtained by casting from integer, in that case
742 keep the cast. */
743 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
744 return orig;
745
80bb306a 746 if (TREE_CODE (addr) != ADDR_EXPR)
747 return addr;
516849c7 748
80bb306a 749 return build_fold_addr_expr (TREE_OPERAND (addr, 0));
516849c7 750}
751
48e1416a 752/* Analyzes the behavior of the memory reference DR in the innermost loop or
0c257e4c 753 basic block that contains it. Returns true if analysis succeed or false
37545e54 754 otherwise. */
516849c7 755
880734c8 756bool
0c257e4c 757dr_analyze_innermost (struct data_reference *dr, struct loop *nest)
516849c7 758{
42acab1c 759 gimple *stmt = DR_STMT (dr);
80bb306a 760 struct loop *loop = loop_containing_stmt (stmt);
761 tree ref = DR_REF (dr);
516849c7 762 HOST_WIDE_INT pbitsize, pbitpos;
80bb306a 763 tree base, poffset;
3754d046 764 machine_mode pmode;
292237f3 765 int punsignedp, preversep, pvolatilep;
80bb306a 766 affine_iv base_iv, offset_iv;
767 tree init, dinit, step;
37545e54 768 bool in_loop = (loop && loop->num);
80bb306a 769
770 if (dump_file && (dump_flags & TDF_DETAILS))
771 fprintf (dump_file, "analyze_innermost: ");
516849c7 772
292237f3 773 base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset, &pmode,
774 &punsignedp, &preversep, &pvolatilep, false);
80bb306a 775 gcc_assert (base != NULL_TREE);
516849c7 776
80bb306a 777 if (pbitpos % BITS_PER_UNIT != 0)
516849c7 778 {
80bb306a 779 if (dump_file && (dump_flags & TDF_DETAILS))
780 fprintf (dump_file, "failed: bit offset alignment.\n");
880734c8 781 return false;
80bb306a 782 }
516849c7 783
292237f3 784 if (preversep)
785 {
786 if (dump_file && (dump_flags & TDF_DETAILS))
787 fprintf (dump_file, "failed: reverse storage order.\n");
788 return false;
789 }
790
182cf5a9 791 if (TREE_CODE (base) == MEM_REF)
792 {
793 if (!integer_zerop (TREE_OPERAND (base, 1)))
794 {
5de9d3ed 795 offset_int moff = mem_ref_offset (base);
e913b5cd 796 tree mofft = wide_int_to_tree (sizetype, moff);
182cf5a9 797 if (!poffset)
7667733b 798 poffset = mofft;
182cf5a9 799 else
7667733b 800 poffset = size_binop (PLUS_EXPR, poffset, mofft);
182cf5a9 801 }
802 base = TREE_OPERAND (base, 0);
803 }
804 else
805 base = build_fold_addr_expr (base);
0c257e4c 806
37545e54 807 if (in_loop)
80bb306a 808 {
48e1416a 809 if (!simple_iv (loop, loop_containing_stmt (stmt), base, &base_iv,
f634c3e9 810 nest ? true : false))
37545e54 811 {
0c257e4c 812 if (nest)
813 {
814 if (dump_file && (dump_flags & TDF_DETAILS))
815 fprintf (dump_file, "failed: evolution of base is not"
816 " affine.\n");
817 return false;
818 }
819 else
820 {
821 base_iv.base = base;
822 base_iv.step = ssize_int (0);
823 base_iv.no_overflow = true;
824 }
37545e54 825 }
826 }
827 else
828 {
829 base_iv.base = base;
830 base_iv.step = ssize_int (0);
831 base_iv.no_overflow = true;
80bb306a 832 }
37545e54 833
dd2644cf 834 if (!poffset)
80bb306a 835 {
836 offset_iv.base = ssize_int (0);
837 offset_iv.step = ssize_int (0);
838 }
dd2644cf 839 else
80bb306a 840 {
dd2644cf 841 if (!in_loop)
842 {
843 offset_iv.base = poffset;
844 offset_iv.step = ssize_int (0);
845 }
846 else if (!simple_iv (loop, loop_containing_stmt (stmt),
f634c3e9 847 poffset, &offset_iv,
848 nest ? true : false))
dd2644cf 849 {
0c257e4c 850 if (nest)
851 {
852 if (dump_file && (dump_flags & TDF_DETAILS))
853 fprintf (dump_file, "failed: evolution of offset is not"
854 " affine.\n");
855 return false;
856 }
857 else
858 {
859 offset_iv.base = poffset;
860 offset_iv.step = ssize_int (0);
861 }
dd2644cf 862 }
80bb306a 863 }
516849c7 864
80bb306a 865 init = ssize_int (pbitpos / BITS_PER_UNIT);
866 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
867 init = size_binop (PLUS_EXPR, init, dinit);
868 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
869 init = size_binop (PLUS_EXPR, init, dinit);
516849c7 870
80bb306a 871 step = size_binop (PLUS_EXPR,
872 fold_convert (ssizetype, base_iv.step),
873 fold_convert (ssizetype, offset_iv.step));
516849c7 874
80bb306a 875 DR_BASE_ADDRESS (dr) = canonicalize_base_object_address (base_iv.base);
516849c7 876
80bb306a 877 DR_OFFSET (dr) = fold_convert (ssizetype, offset_iv.base);
878 DR_INIT (dr) = init;
879 DR_STEP (dr) = step;
516849c7 880
80bb306a 881 DR_ALIGNED_TO (dr) = size_int (highest_pow2_factor (offset_iv.base));
516849c7 882
80bb306a 883 if (dump_file && (dump_flags & TDF_DETAILS))
884 fprintf (dump_file, "success.\n");
880734c8 885
886 return true;
80bb306a 887}
516849c7 888
80bb306a 889/* Determines the base object and the list of indices of memory reference
221a697e 890 DR, analyzed in LOOP and instantiated in loop nest NEST. */
516849c7 891
80bb306a 892static void
221a697e 893dr_analyze_indices (struct data_reference *dr, loop_p nest, loop_p loop)
80bb306a 894{
1e094109 895 vec<tree> access_fns = vNULL;
95539e1d 896 tree ref, op;
93fc59e8 897 tree base, off, access_fn;
898 basic_block before_loop;
48e1416a 899
93fc59e8 900 /* If analyzing a basic-block there are no indices to analyze
901 and thus no access functions. */
5fc88ffd 902 if (!nest)
903 {
93fc59e8 904 DR_BASE_OBJECT (dr) = DR_REF (dr);
f1f41a6c 905 DR_ACCESS_FNS (dr).create (0);
5fc88ffd 906 return;
907 }
908
95539e1d 909 ref = DR_REF (dr);
5fc88ffd 910 before_loop = block_before_loop (nest);
48e1416a 911
93fc59e8 912 /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
913 into a two element array with a constant index. The base is
914 then just the immediate underlying object. */
915 if (TREE_CODE (ref) == REALPART_EXPR)
916 {
917 ref = TREE_OPERAND (ref, 0);
f1f41a6c 918 access_fns.safe_push (integer_zero_node);
93fc59e8 919 }
920 else if (TREE_CODE (ref) == IMAGPART_EXPR)
921 {
922 ref = TREE_OPERAND (ref, 0);
f1f41a6c 923 access_fns.safe_push (integer_one_node);
93fc59e8 924 }
925
bc4113a9 926 /* Analyze access functions of dimensions we know to be independent. */
95539e1d 927 while (handled_component_p (ref))
516849c7 928 {
95539e1d 929 if (TREE_CODE (ref) == ARRAY_REF)
516849c7 930 {
95539e1d 931 op = TREE_OPERAND (ref, 1);
5fc88ffd 932 access_fn = analyze_scalar_evolution (loop, op);
933 access_fn = instantiate_scev (before_loop, loop, access_fn);
f1f41a6c 934 access_fns.safe_push (access_fn);
bc4113a9 935 }
95539e1d 936 else if (TREE_CODE (ref) == COMPONENT_REF
937 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
938 {
939 /* For COMPONENT_REFs of records (but not unions!) use the
940 FIELD_DECL offset as constant access function so we can
941 disambiguate a[i].f1 and a[i].f2. */
942 tree off = component_ref_field_offset (ref);
943 off = size_binop (PLUS_EXPR,
944 size_binop (MULT_EXPR,
945 fold_convert (bitsizetype, off),
946 bitsize_int (BITS_PER_UNIT)),
947 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
f1f41a6c 948 access_fns.safe_push (off);
95539e1d 949 }
950 else
951 /* If we have an unhandled component we could not translate
952 to an access function stop analyzing. We have determined
953 our base object in this case. */
954 break;
48e1416a 955
95539e1d 956 ref = TREE_OPERAND (ref, 0);
516849c7 957 }
958
0f00ec21 959 /* If the address operand of a MEM_REF base has an evolution in the
960 analyzed nest, add it as an additional independent access-function. */
95539e1d 961 if (TREE_CODE (ref) == MEM_REF)
516849c7 962 {
95539e1d 963 op = TREE_OPERAND (ref, 0);
80bb306a 964 access_fn = analyze_scalar_evolution (loop, op);
089aa668 965 access_fn = instantiate_scev (before_loop, loop, access_fn);
0f00ec21 966 if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
9488020c 967 {
a927c5fa 968 tree orig_type;
95539e1d 969 tree memoff = TREE_OPERAND (ref, 1);
0f00ec21 970 base = initial_condition (access_fn);
a927c5fa 971 orig_type = TREE_TYPE (base);
972 STRIP_USELESS_TYPE_CONVERSION (base);
0f00ec21 973 split_constant_offset (base, &base, &off);
1e074e77 974 STRIP_USELESS_TYPE_CONVERSION (base);
0f00ec21 975 /* Fold the MEM_REF offset into the evolutions initial
976 value to make more bases comparable. */
95539e1d 977 if (!integer_zerop (memoff))
0f00ec21 978 {
979 off = size_binop (PLUS_EXPR, off,
95539e1d 980 fold_convert (ssizetype, memoff));
981 memoff = build_int_cst (TREE_TYPE (memoff), 0);
0f00ec21 982 }
43085be3 983 /* Adjust the offset so it is a multiple of the access type
984 size and thus we separate bases that can possibly be used
985 to produce partial overlaps (which the access_fn machinery
986 cannot handle). */
987 wide_int rem;
988 if (TYPE_SIZE_UNIT (TREE_TYPE (ref))
989 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref))) == INTEGER_CST
990 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (ref))))
991 rem = wi::mod_trunc (off, TYPE_SIZE_UNIT (TREE_TYPE (ref)), SIGNED);
992 else
993 /* If we can't compute the remainder simply force the initial
994 condition to zero. */
995 rem = off;
996 off = wide_int_to_tree (ssizetype, wi::sub (off, rem));
997 memoff = wide_int_to_tree (TREE_TYPE (memoff), rem);
998 /* And finally replace the initial condition. */
0f00ec21 999 access_fn = chrec_replace_initial_condition
a927c5fa 1000 (access_fn, fold_convert (orig_type, off));
95539e1d 1001 /* ??? This is still not a suitable base object for
1002 dr_may_alias_p - the base object needs to be an
1003 access that covers the object as whole. With
1004 an evolution in the pointer this cannot be
1005 guaranteed.
1006 As a band-aid, mark the access so we can special-case
1007 it in dr_may_alias_p. */
62b0a610 1008 tree old = ref;
95539e1d 1009 ref = fold_build2_loc (EXPR_LOCATION (ref),
1010 MEM_REF, TREE_TYPE (ref),
1011 base, memoff);
62b0a610 1012 MR_DEPENDENCE_CLIQUE (ref) = MR_DEPENDENCE_CLIQUE (old);
1013 MR_DEPENDENCE_BASE (ref) = MR_DEPENDENCE_BASE (old);
f146c442 1014 DR_UNCONSTRAINED_BASE (dr) = true;
f1f41a6c 1015 access_fns.safe_push (access_fn);
9488020c 1016 }
516849c7 1017 }
95539e1d 1018 else if (DECL_P (ref))
1019 {
1020 /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
1021 ref = build2 (MEM_REF, TREE_TYPE (ref),
1022 build_fold_addr_expr (ref),
1023 build_int_cst (reference_alias_ptr_type (ref), 0));
1024 }
516849c7 1025
80bb306a 1026 DR_BASE_OBJECT (dr) = ref;
1027 DR_ACCESS_FNS (dr) = access_fns;
516849c7 1028}
1029
80bb306a 1030/* Extracts the alias analysis information from the memory reference DR. */
516849c7 1031
80bb306a 1032static void
1033dr_analyze_alias (struct data_reference *dr)
516849c7 1034{
80bb306a 1035 tree ref = DR_REF (dr);
dd277d48 1036 tree base = get_base_address (ref), addr;
1037
182cf5a9 1038 if (INDIRECT_REF_P (base)
1039 || TREE_CODE (base) == MEM_REF)
80bb306a 1040 {
1041 addr = TREE_OPERAND (base, 0);
1042 if (TREE_CODE (addr) == SSA_NAME)
dd277d48 1043 DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
80bb306a 1044 }
80bb306a 1045}
516849c7 1046
80bb306a 1047/* Frees data reference DR. */
31e389a2 1048
801c5610 1049void
31e389a2 1050free_data_ref (data_reference_p dr)
1051{
f1f41a6c 1052 DR_ACCESS_FNS (dr).release ();
31e389a2 1053 free (dr);
1054}
516849c7 1055
80bb306a 1056/* Analyzes memory reference MEMREF accessed in STMT. The reference
1057 is read if IS_READ is true, write otherwise. Returns the
221a697e 1058 data_reference description of MEMREF. NEST is the outermost loop
1059 in which the reference should be instantiated, LOOP is the loop in
1060 which the data reference should be analyzed. */
516849c7 1061
5c205353 1062struct data_reference *
42acab1c 1063create_data_ref (loop_p nest, loop_p loop, tree memref, gimple *stmt,
221a697e 1064 bool is_read)
516849c7 1065{
80bb306a 1066 struct data_reference *dr;
6b421feb 1067
80bb306a 1068 if (dump_file && (dump_flags & TDF_DETAILS))
6b421feb 1069 {
80bb306a 1070 fprintf (dump_file, "Creating dr for ");
1071 print_generic_expr (dump_file, memref, TDF_SLIM);
1072 fprintf (dump_file, "\n");
6b421feb 1073 }
f84a688a 1074
80bb306a 1075 dr = XCNEW (struct data_reference);
1076 DR_STMT (dr) = stmt;
1077 DR_REF (dr) = memref;
1078 DR_IS_READ (dr) = is_read;
516849c7 1079
0c257e4c 1080 dr_analyze_innermost (dr, nest);
221a697e 1081 dr_analyze_indices (dr, nest, loop);
80bb306a 1082 dr_analyze_alias (dr);
516849c7 1083
1084 if (dump_file && (dump_flags & TDF_DETAILS))
1085 {
bc4113a9 1086 unsigned i;
80bb306a 1087 fprintf (dump_file, "\tbase_address: ");
516849c7 1088 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1089 fprintf (dump_file, "\n\toffset from base address: ");
1090 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1091 fprintf (dump_file, "\n\tconstant offset from base address: ");
1092 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
516849c7 1093 fprintf (dump_file, "\n\tstep: ");
1094 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
80bb306a 1095 fprintf (dump_file, "\n\taligned to: ");
1096 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
1097 fprintf (dump_file, "\n\tbase_object: ");
1098 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
516849c7 1099 fprintf (dump_file, "\n");
bc4113a9 1100 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
1101 {
1102 fprintf (dump_file, "\tAccess function %d: ", i);
1103 print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
1104 }
80bb306a 1105 }
1106
48e1416a 1107 return dr;
516849c7 1108}
1109
ec611e12 1110/* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
1111 expressions. */
1112static bool
1113dr_equal_offsets_p1 (tree offset1, tree offset2)
1114{
1115 bool res;
1116
1117 STRIP_NOPS (offset1);
1118 STRIP_NOPS (offset2);
1119
1120 if (offset1 == offset2)
1121 return true;
1122
1123 if (TREE_CODE (offset1) != TREE_CODE (offset2)
1124 || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
1125 return false;
1126
1127 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
1128 TREE_OPERAND (offset2, 0));
1129
1130 if (!res || !BINARY_CLASS_P (offset1))
1131 return res;
1132
1133 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
1134 TREE_OPERAND (offset2, 1));
1135
1136 return res;
1137}
1138
1139/* Check if DRA and DRB have equal offsets. */
1140bool
1141dr_equal_offsets_p (struct data_reference *dra,
1142 struct data_reference *drb)
1143{
1144 tree offset1, offset2;
1145
1146 offset1 = DR_OFFSET (dra);
1147 offset2 = DR_OFFSET (drb);
1148
1149 return dr_equal_offsets_p1 (offset1, offset2);
1150}
1151
87da4f2e 1152/* Returns true if FNA == FNB. */
1153
1154static bool
1155affine_function_equal_p (affine_fn fna, affine_fn fnb)
1156{
f1f41a6c 1157 unsigned i, n = fna.length ();
516849c7 1158
f1f41a6c 1159 if (n != fnb.length ())
de33470d 1160 return false;
bc3c8ad4 1161
87da4f2e 1162 for (i = 0; i < n; i++)
f1f41a6c 1163 if (!operand_equal_p (fna[i], fnb[i], 0))
87da4f2e 1164 return false;
1165
1166 return true;
1167}
1168
1169/* If all the functions in CF are the same, returns one of them,
1170 otherwise returns NULL. */
1171
1172static affine_fn
1173common_affine_function (conflict_function *cf)
bc3c8ad4 1174{
87da4f2e 1175 unsigned i;
1176 affine_fn comm;
1177
1178 if (!CF_NONTRIVIAL_P (cf))
9af5ce0c 1179 return affine_fn ();
87da4f2e 1180
1181 comm = cf->fns[0];
1182
1183 for (i = 1; i < cf->n; i++)
1184 if (!affine_function_equal_p (comm, cf->fns[i]))
9af5ce0c 1185 return affine_fn ();
87da4f2e 1186
1187 return comm;
1188}
bc3c8ad4 1189
87da4f2e 1190/* Returns the base of the affine function FN. */
1191
1192static tree
1193affine_function_base (affine_fn fn)
1194{
f1f41a6c 1195 return fn[0];
87da4f2e 1196}
1197
1198/* Returns true if FN is a constant. */
1199
1200static bool
1201affine_function_constant_p (affine_fn fn)
1202{
1203 unsigned i;
1204 tree coef;
1205
f1f41a6c 1206 for (i = 1; fn.iterate (i, &coef); i++)
87da4f2e 1207 if (!integer_zerop (coef))
f84a688a 1208 return false;
1209
bc3c8ad4 1210 return true;
1211}
1212
2a21a5df 1213/* Returns true if FN is the zero constant function. */
1214
1215static bool
1216affine_function_zero_p (affine_fn fn)
1217{
1218 return (integer_zerop (affine_function_base (fn))
1219 && affine_function_constant_p (fn));
1220}
1221
9a3dad1c 1222/* Returns a signed integer type with the largest precision from TA
1223 and TB. */
1224
1225static tree
1226signed_type_for_types (tree ta, tree tb)
1227{
1228 if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
1229 return signed_type_for (ta);
1230 else
1231 return signed_type_for (tb);
1232}
1233
87da4f2e 1234/* Applies operation OP on affine functions FNA and FNB, and returns the
1235 result. */
1236
1237static affine_fn
1238affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
1239{
1240 unsigned i, n, m;
1241 affine_fn ret;
1242 tree coef;
1243
f1f41a6c 1244 if (fnb.length () > fna.length ())
87da4f2e 1245 {
f1f41a6c 1246 n = fna.length ();
1247 m = fnb.length ();
87da4f2e 1248 }
1249 else
1250 {
f1f41a6c 1251 n = fnb.length ();
1252 m = fna.length ();
87da4f2e 1253 }
1254
f1f41a6c 1255 ret.create (m);
87da4f2e 1256 for (i = 0; i < n; i++)
9a3dad1c 1257 {
f1f41a6c 1258 tree type = signed_type_for_types (TREE_TYPE (fna[i]),
1259 TREE_TYPE (fnb[i]));
1260 ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
9a3dad1c 1261 }
87da4f2e 1262
f1f41a6c 1263 for (; fna.iterate (i, &coef); i++)
1264 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
87da4f2e 1265 coef, integer_zero_node));
f1f41a6c 1266 for (; fnb.iterate (i, &coef); i++)
1267 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
87da4f2e 1268 integer_zero_node, coef));
1269
1270 return ret;
1271}
1272
1273/* Returns the sum of affine functions FNA and FNB. */
1274
1275static affine_fn
1276affine_fn_plus (affine_fn fna, affine_fn fnb)
1277{
1278 return affine_fn_op (PLUS_EXPR, fna, fnb);
1279}
1280
1281/* Returns the difference of affine functions FNA and FNB. */
1282
1283static affine_fn
1284affine_fn_minus (affine_fn fna, affine_fn fnb)
1285{
1286 return affine_fn_op (MINUS_EXPR, fna, fnb);
1287}
1288
1289/* Frees affine function FN. */
1290
1291static void
1292affine_fn_free (affine_fn fn)
1293{
f1f41a6c 1294 fn.release ();
87da4f2e 1295}
1296
bc3c8ad4 1297/* Determine for each subscript in the data dependence relation DDR
1298 the distance. */
2146e26d 1299
6b421feb 1300static void
bc3c8ad4 1301compute_subscript_distance (struct data_dependence_relation *ddr)
2146e26d 1302{
87da4f2e 1303 conflict_function *cf_a, *cf_b;
1304 affine_fn fn_a, fn_b, diff;
1305
2146e26d 1306 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1307 {
1308 unsigned int i;
48e1416a 1309
2146e26d 1310 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1311 {
2146e26d 1312 struct subscript *subscript;
48e1416a 1313
2146e26d 1314 subscript = DDR_SUBSCRIPT (ddr, i);
87da4f2e 1315 cf_a = SUB_CONFLICTS_IN_A (subscript);
1316 cf_b = SUB_CONFLICTS_IN_B (subscript);
bc3c8ad4 1317
87da4f2e 1318 fn_a = common_affine_function (cf_a);
1319 fn_b = common_affine_function (cf_b);
f1f41a6c 1320 if (!fn_a.exists () || !fn_b.exists ())
bc3c8ad4 1321 {
87da4f2e 1322 SUB_DISTANCE (subscript) = chrec_dont_know;
1323 return;
bc3c8ad4 1324 }
87da4f2e 1325 diff = affine_fn_minus (fn_a, fn_b);
48e1416a 1326
87da4f2e 1327 if (affine_function_constant_p (diff))
1328 SUB_DISTANCE (subscript) = affine_function_base (diff);
2146e26d 1329 else
1330 SUB_DISTANCE (subscript) = chrec_dont_know;
87da4f2e 1331
1332 affine_fn_free (diff);
2146e26d 1333 }
1334 }
1335}
1336
87da4f2e 1337/* Returns the conflict function for "unknown". */
1338
1339static conflict_function *
1340conflict_fn_not_known (void)
1341{
1342 conflict_function *fn = XCNEW (conflict_function);
1343 fn->n = NOT_KNOWN;
1344
1345 return fn;
1346}
1347
1348/* Returns the conflict function for "independent". */
1349
1350static conflict_function *
1351conflict_fn_no_dependence (void)
1352{
1353 conflict_function *fn = XCNEW (conflict_function);
1354 fn->n = NO_DEPENDENCE;
1355
1356 return fn;
1357}
1358
80bb306a 1359/* Returns true if the address of OBJ is invariant in LOOP. */
1360
1361static bool
7ecb5bb2 1362object_address_invariant_in_loop_p (const struct loop *loop, const_tree obj)
80bb306a 1363{
1364 while (handled_component_p (obj))
1365 {
1366 if (TREE_CODE (obj) == ARRAY_REF)
1367 {
1368 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1369 need to check the stride and the lower bound of the reference. */
1370 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1371 loop->num)
1372 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 3),
1373 loop->num))
1374 return false;
1375 }
1376 else if (TREE_CODE (obj) == COMPONENT_REF)
1377 {
1378 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1379 loop->num))
1380 return false;
1381 }
1382 obj = TREE_OPERAND (obj, 0);
1383 }
1384
182cf5a9 1385 if (!INDIRECT_REF_P (obj)
1386 && TREE_CODE (obj) != MEM_REF)
80bb306a 1387 return true;
1388
1389 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
1390 loop->num);
1391}
1392
80bb306a 1393/* Returns false if we can prove that data references A and B do not alias,
5fc88ffd 1394 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
1395 considered. */
80bb306a 1396
255b6be7 1397bool
5fc88ffd 1398dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
1399 bool loop_nest)
80bb306a 1400{
2760c0e3 1401 tree addr_a = DR_BASE_OBJECT (a);
1402 tree addr_b = DR_BASE_OBJECT (b);
80bb306a 1403
5fc88ffd 1404 /* If we are not processing a loop nest but scalar code we
1405 do not need to care about possible cross-iteration dependences
1406 and thus can process the full original reference. Do so,
1407 similar to how loop invariant motion applies extra offset-based
1408 disambiguation. */
1409 if (!loop_nest)
1410 {
1411 aff_tree off1, off2;
5de9d3ed 1412 widest_int size1, size2;
5fc88ffd 1413 get_inner_reference_aff (DR_REF (a), &off1, &size1);
1414 get_inner_reference_aff (DR_REF (b), &off2, &size2);
e913b5cd 1415 aff_combination_scale (&off1, -1);
5fc88ffd 1416 aff_combination_add (&off2, &off1);
1417 if (aff_comb_cannot_overlap_p (&off2, size1, size2))
1418 return false;
1419 }
1420
62b0a610 1421 if ((TREE_CODE (addr_a) == MEM_REF || TREE_CODE (addr_a) == TARGET_MEM_REF)
1422 && (TREE_CODE (addr_b) == MEM_REF || TREE_CODE (addr_b) == TARGET_MEM_REF)
1423 && MR_DEPENDENCE_CLIQUE (addr_a) == MR_DEPENDENCE_CLIQUE (addr_b)
1424 && MR_DEPENDENCE_BASE (addr_a) != MR_DEPENDENCE_BASE (addr_b))
1425 return false;
1426
c8efcccb 1427 /* If we had an evolution in a pointer-based MEM_REF BASE_OBJECT we
1428 do not know the size of the base-object. So we cannot do any
1429 offset/overlap based analysis but have to rely on points-to
1430 information only. */
95539e1d 1431 if (TREE_CODE (addr_a) == MEM_REF
f146c442 1432 && (DR_UNCONSTRAINED_BASE (a)
1433 || TREE_CODE (TREE_OPERAND (addr_a, 0)) == SSA_NAME))
95539e1d 1434 {
c8efcccb 1435 /* For true dependences we can apply TBAA. */
1436 if (flag_strict_aliasing
1437 && DR_IS_WRITE (a) && DR_IS_READ (b)
1438 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1439 get_alias_set (DR_REF (b))))
1440 return false;
1441 if (TREE_CODE (addr_b) == MEM_REF)
95539e1d 1442 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1443 TREE_OPERAND (addr_b, 0));
1444 else
1445 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1446 build_fold_addr_expr (addr_b));
1447 }
1448 else if (TREE_CODE (addr_b) == MEM_REF
f146c442 1449 && (DR_UNCONSTRAINED_BASE (b)
1450 || TREE_CODE (TREE_OPERAND (addr_b, 0)) == SSA_NAME))
c8efcccb 1451 {
1452 /* For true dependences we can apply TBAA. */
1453 if (flag_strict_aliasing
1454 && DR_IS_WRITE (a) && DR_IS_READ (b)
1455 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1456 get_alias_set (DR_REF (b))))
1457 return false;
1458 if (TREE_CODE (addr_a) == MEM_REF)
1459 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1460 TREE_OPERAND (addr_b, 0));
1461 else
1462 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
1463 TREE_OPERAND (addr_b, 0));
1464 }
95539e1d 1465
1466 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1467 that is being subsetted in the loop nest. */
9ff25603 1468 if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
2760c0e3 1469 return refs_output_dependent_p (addr_a, addr_b);
9ff25603 1470 else if (DR_IS_READ (a) && DR_IS_WRITE (b))
2760c0e3 1471 return refs_anti_dependent_p (addr_a, addr_b);
1472 return refs_may_alias_p (addr_a, addr_b);
80bb306a 1473}
1474
6b421feb 1475/* Initialize a data dependence relation between data accesses A and
1476 B. NB_LOOPS is the number of loops surrounding the references: the
1477 size of the classic distance/direction vectors. */
2146e26d 1478
16dfb112 1479struct data_dependence_relation *
48e1416a 1480initialize_data_dependence_relation (struct data_reference *a,
6b421feb 1481 struct data_reference *b,
f1f41a6c 1482 vec<loop_p> loop_nest)
2146e26d 1483{
1484 struct data_dependence_relation *res;
6b421feb 1485 unsigned int i;
48e1416a 1486
4c36ffe6 1487 res = XNEW (struct data_dependence_relation);
2146e26d 1488 DDR_A (res) = a;
1489 DDR_B (res) = b;
f1f41a6c 1490 DDR_LOOP_NEST (res).create (0);
0ecb94cf 1491 DDR_REVERSED_P (res) = false;
f1f41a6c 1492 DDR_SUBSCRIPTS (res).create (0);
1493 DDR_DIR_VECTS (res).create (0);
1494 DDR_DIST_VECTS (res).create (0);
2146e26d 1495
516849c7 1496 if (a == NULL || b == NULL)
1497 {
48e1416a 1498 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
516849c7 1499 return res;
48e1416a 1500 }
516849c7 1501
80bb306a 1502 /* If the data references do not alias, then they are independent. */
f1f41a6c 1503 if (!dr_may_alias_p (a, b, loop_nest.exists ()))
516849c7 1504 {
48e1416a 1505 DDR_ARE_DEPENDENT (res) = chrec_known;
516849c7 1506 return res;
1507 }
2146e26d 1508
cc76857b 1509 /* The case where the references are exactly the same. */
c127dd86 1510 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
1511 {
f1f41a6c 1512 if (loop_nest.exists ()
1513 && !object_address_invariant_in_loop_p (loop_nest[0],
c76b51be 1514 DR_BASE_OBJECT (a)))
1515 {
1516 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1517 return res;
1518 }
c127dd86 1519 DDR_AFFINE_P (res) = true;
1520 DDR_ARE_DEPENDENT (res) = NULL_TREE;
f1f41a6c 1521 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
c127dd86 1522 DDR_LOOP_NEST (res) = loop_nest;
1523 DDR_INNER_LOOP (res) = 0;
1524 DDR_SELF_REFERENCE (res) = true;
c76b51be 1525 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1526 {
1527 struct subscript *subscript;
1528
1529 subscript = XNEW (struct subscript);
1530 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1531 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1532 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1533 SUB_DISTANCE (subscript) = chrec_dont_know;
f1f41a6c 1534 DDR_SUBSCRIPTS (res).safe_push (subscript);
c76b51be 1535 }
c127dd86 1536 return res;
1537 }
1538
80bb306a 1539 /* If the references do not access the same object, we do not know
1540 whether they alias or not. */
1541 if (!operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0))
2146e26d 1542 {
48e1416a 1543 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
516849c7 1544 return res;
1545 }
6b421feb 1546
80bb306a 1547 /* If the base of the object is not invariant in the loop nest, we cannot
bef304b8 1548 analyze it. TODO -- in fact, it would suffice to record that there may
310d2511 1549 be arbitrary dependences in the loops where the base object varies. */
f1f41a6c 1550 if (loop_nest.exists ()
1551 && !object_address_invariant_in_loop_p (loop_nest[0],
37545e54 1552 DR_BASE_OBJECT (a)))
516849c7 1553 {
48e1416a 1554 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
516849c7 1555 return res;
1556 }
80bb306a 1557
fabd4538 1558 /* If the number of dimensions of the access to not agree we can have
1559 a pointer access to a component of the array element type and an
1560 array access while the base-objects are still the same. Punt. */
1561 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1562 {
1563 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1564 return res;
1565 }
80bb306a 1566
516849c7 1567 DDR_AFFINE_P (res) = true;
1568 DDR_ARE_DEPENDENT (res) = NULL_TREE;
f1f41a6c 1569 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
b44d1046 1570 DDR_LOOP_NEST (res) = loop_nest;
355572cc 1571 DDR_INNER_LOOP (res) = 0;
c127dd86 1572 DDR_SELF_REFERENCE (res) = false;
1532ec98 1573
516849c7 1574 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1575 {
1576 struct subscript *subscript;
48e1416a 1577
4c36ffe6 1578 subscript = XNEW (struct subscript);
87da4f2e 1579 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1580 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
516849c7 1581 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1582 SUB_DISTANCE (subscript) = chrec_dont_know;
f1f41a6c 1583 DDR_SUBSCRIPTS (res).safe_push (subscript);
2146e26d 1584 }
41c7a324 1585
2146e26d 1586 return res;
1587}
1588
87da4f2e 1589/* Frees memory used by the conflict function F. */
1590
1591static void
1592free_conflict_function (conflict_function *f)
1593{
1594 unsigned i;
1595
1596 if (CF_NONTRIVIAL_P (f))
1597 {
1598 for (i = 0; i < f->n; i++)
1599 affine_fn_free (f->fns[i]);
1600 }
1601 free (f);
1602}
1603
1604/* Frees memory used by SUBSCRIPTS. */
1605
1606static void
f1f41a6c 1607free_subscripts (vec<subscript_p> subscripts)
87da4f2e 1608{
1609 unsigned i;
1610 subscript_p s;
1611
f1f41a6c 1612 FOR_EACH_VEC_ELT (subscripts, i, s)
87da4f2e 1613 {
1614 free_conflict_function (s->conflicting_iterations_in_a);
1615 free_conflict_function (s->conflicting_iterations_in_b);
19af51e2 1616 free (s);
87da4f2e 1617 }
f1f41a6c 1618 subscripts.release ();
87da4f2e 1619}
1620
2146e26d 1621/* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1622 description. */
1623
1624static inline void
48e1416a 1625finalize_ddr_dependent (struct data_dependence_relation *ddr,
2146e26d 1626 tree chrec)
1627{
48e1416a 1628 DDR_ARE_DEPENDENT (ddr) = chrec;
87da4f2e 1629 free_subscripts (DDR_SUBSCRIPTS (ddr));
f1f41a6c 1630 DDR_SUBSCRIPTS (ddr).create (0);
2146e26d 1631}
1632
bc3c8ad4 1633/* The dependence relation DDR cannot be represented by a distance
1634 vector. */
1635
1636static inline void
1637non_affine_dependence_relation (struct data_dependence_relation *ddr)
1638{
1639 if (dump_file && (dump_flags & TDF_DETAILS))
1640 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
1641
1642 DDR_AFFINE_P (ddr) = false;
1643}
1644
2146e26d 1645\f
1646
1647/* This section contains the classic Banerjee tests. */
1648
1649/* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1650 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1651
1652static inline bool
7ecb5bb2 1653ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
2146e26d 1654{
1655 return (evolution_function_is_constant_p (chrec_a)
1656 && evolution_function_is_constant_p (chrec_b));
1657}
1658
1659/* Returns true iff CHREC_A and CHREC_B are dependent on an index
1660 variable, i.e., if the SIV (Single Index Variable) test is true. */
1661
1662static bool
7ecb5bb2 1663siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
2146e26d 1664{
1665 if ((evolution_function_is_constant_p (chrec_a)
1666 && evolution_function_is_univariate_p (chrec_b))
1667 || (evolution_function_is_constant_p (chrec_b)
1668 && evolution_function_is_univariate_p (chrec_a)))
1669 return true;
48e1416a 1670
2146e26d 1671 if (evolution_function_is_univariate_p (chrec_a)
1672 && evolution_function_is_univariate_p (chrec_b))
1673 {
1674 switch (TREE_CODE (chrec_a))
1675 {
1676 case POLYNOMIAL_CHREC:
1677 switch (TREE_CODE (chrec_b))
1678 {
1679 case POLYNOMIAL_CHREC:
1680 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
1681 return false;
48e1416a 1682
2146e26d 1683 default:
1684 return true;
1685 }
48e1416a 1686
2146e26d 1687 default:
1688 return true;
1689 }
1690 }
48e1416a 1691
2146e26d 1692 return false;
1693}
1694
87da4f2e 1695/* Creates a conflict function with N dimensions. The affine functions
1696 in each dimension follow. */
1697
1698static conflict_function *
1699conflict_fn (unsigned n, ...)
1700{
1701 unsigned i;
1702 conflict_function *ret = XCNEW (conflict_function);
1703 va_list ap;
1704
a86c75a6 1705 gcc_assert (0 < n && n <= MAX_DIM);
9af5ce0c 1706 va_start (ap, n);
48e1416a 1707
87da4f2e 1708 ret->n = n;
1709 for (i = 0; i < n; i++)
1710 ret->fns[i] = va_arg (ap, affine_fn);
9af5ce0c 1711 va_end (ap);
87da4f2e 1712
1713 return ret;
1714}
1715
1716/* Returns constant affine function with value CST. */
1717
1718static affine_fn
1719affine_fn_cst (tree cst)
1720{
f1f41a6c 1721 affine_fn fn;
1722 fn.create (1);
1723 fn.quick_push (cst);
87da4f2e 1724 return fn;
1725}
1726
1727/* Returns affine function with single variable, CST + COEF * x_DIM. */
1728
1729static affine_fn
1730affine_fn_univar (tree cst, unsigned dim, tree coef)
1731{
f1f41a6c 1732 affine_fn fn;
1733 fn.create (dim + 1);
87da4f2e 1734 unsigned i;
1735
1736 gcc_assert (dim > 0);
f1f41a6c 1737 fn.quick_push (cst);
87da4f2e 1738 for (i = 1; i < dim; i++)
f1f41a6c 1739 fn.quick_push (integer_zero_node);
1740 fn.quick_push (coef);
87da4f2e 1741 return fn;
1742}
1743
2146e26d 1744/* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1745 *OVERLAPS_B are initialized to the functions that describe the
1746 relation between the elements accessed twice by CHREC_A and
1747 CHREC_B. For k >= 0, the following property is verified:
1748
1749 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1750
48e1416a 1751static void
1752analyze_ziv_subscript (tree chrec_a,
1753 tree chrec_b,
87da4f2e 1754 conflict_function **overlaps_a,
48e1416a 1755 conflict_function **overlaps_b,
bc3c8ad4 1756 tree *last_conflicts)
2146e26d 1757{
9a3dad1c 1758 tree type, difference;
6b421feb 1759 dependence_stats.num_ziv++;
48e1416a 1760
2146e26d 1761 if (dump_file && (dump_flags & TDF_DETAILS))
1762 fprintf (dump_file, "(analyze_ziv_subscript \n");
9a3dad1c 1763
1764 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
75a70cf9 1765 chrec_a = chrec_convert (type, chrec_a, NULL);
1766 chrec_b = chrec_convert (type, chrec_b, NULL);
9a3dad1c 1767 difference = chrec_fold_minus (type, chrec_a, chrec_b);
48e1416a 1768
2146e26d 1769 switch (TREE_CODE (difference))
1770 {
1771 case INTEGER_CST:
1772 if (integer_zerop (difference))
1773 {
1774 /* The difference is equal to zero: the accessed index
1775 overlaps for each iteration in the loop. */
87da4f2e 1776 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1777 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
bc3c8ad4 1778 *last_conflicts = chrec_dont_know;
6b421feb 1779 dependence_stats.num_ziv_dependent++;
2146e26d 1780 }
1781 else
1782 {
1783 /* The accesses do not overlap. */
87da4f2e 1784 *overlaps_a = conflict_fn_no_dependence ();
1785 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 1786 *last_conflicts = integer_zero_node;
6b421feb 1787 dependence_stats.num_ziv_independent++;
2146e26d 1788 }
1789 break;
48e1416a 1790
2146e26d 1791 default:
48e1416a 1792 /* We're not sure whether the indexes overlap. For the moment,
2146e26d 1793 conservatively answer "don't know". */
6b421feb 1794 if (dump_file && (dump_flags & TDF_DETAILS))
1795 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
1796
87da4f2e 1797 *overlaps_a = conflict_fn_not_known ();
1798 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 1799 *last_conflicts = chrec_dont_know;
6b421feb 1800 dependence_stats.num_ziv_unimplemented++;
2146e26d 1801 break;
1802 }
48e1416a 1803
2146e26d 1804 if (dump_file && (dump_flags & TDF_DETAILS))
1805 fprintf (dump_file, ")\n");
1806}
1807
8fe79ba5 1808/* Similar to max_stmt_executions_int, but returns the bound as a tree,
d500fef3 1809 and only if it fits to the int type. If this is not the case, or the
8fe79ba5 1810 bound on the number of iterations of LOOP could not be derived, returns
d500fef3 1811 chrec_dont_know. */
1812
1813static tree
8fe79ba5 1814max_stmt_executions_tree (struct loop *loop)
d500fef3 1815{
5de9d3ed 1816 widest_int nit;
d500fef3 1817
fee017b3 1818 if (!max_stmt_executions (loop, &nit))
d500fef3 1819 return chrec_dont_know;
1820
796b6678 1821 if (!wi::fits_to_tree_p (nit, unsigned_type_node))
d500fef3 1822 return chrec_dont_know;
1823
e913b5cd 1824 return wide_int_to_tree (unsigned_type_node, nit);
d500fef3 1825}
1826
06a9142d 1827/* Determine whether the CHREC is always positive/negative. If the expression
1828 cannot be statically analyzed, return false, otherwise set the answer into
1829 VALUE. */
1830
1831static bool
1832chrec_is_positive (tree chrec, bool *value)
1833{
1834 bool value0, value1, value2;
1835 tree end_value, nb_iter;
1836
1837 switch (TREE_CODE (chrec))
1838 {
1839 case POLYNOMIAL_CHREC:
1840 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
1841 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
1842 return false;
1843
1844 /* FIXME -- overflows. */
1845 if (value0 == value1)
1846 {
1847 *value = value0;
1848 return true;
1849 }
1850
1851 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1852 and the proof consists in showing that the sign never
1853 changes during the execution of the loop, from 0 to
1854 loop->nb_iterations. */
1855 if (!evolution_function_is_affine_p (chrec))
1856 return false;
1857
1858 nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
1859 if (chrec_contains_undetermined (nb_iter))
1860 return false;
1861
1862#if 0
1863 /* TODO -- If the test is after the exit, we may decrease the number of
1864 iterations by one. */
1865 if (after_exit)
1866 nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
1867#endif
1868
1869 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
1870
1871 if (!chrec_is_positive (end_value, &value2))
1872 return false;
1873
1874 *value = value0;
1875 return value0 == value1;
1876
1877 case INTEGER_CST:
1878 switch (tree_int_cst_sgn (chrec))
1879 {
1880 case -1:
1881 *value = false;
1882 break;
1883 case 1:
1884 *value = true;
1885 break;
1886 default:
1887 return false;
1888 }
1889 return true;
1890
1891 default:
1892 return false;
1893 }
1894}
1895
1896
2146e26d 1897/* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1898 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1899 *OVERLAPS_B are initialized to the functions that describe the
1900 relation between the elements accessed twice by CHREC_A and
1901 CHREC_B. For k >= 0, the following property is verified:
1902
1903 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1904
1905static void
48e1416a 1906analyze_siv_subscript_cst_affine (tree chrec_a,
2146e26d 1907 tree chrec_b,
48e1416a 1908 conflict_function **overlaps_a,
1909 conflict_function **overlaps_b,
bc3c8ad4 1910 tree *last_conflicts)
2146e26d 1911{
1912 bool value0, value1, value2;
9a3dad1c 1913 tree type, difference, tmp;
f84a688a 1914
9a3dad1c 1915 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
75a70cf9 1916 chrec_a = chrec_convert (type, chrec_a, NULL);
1917 chrec_b = chrec_convert (type, chrec_b, NULL);
9a3dad1c 1918 difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
48e1416a 1919
06a9142d 1920 /* Special case overlap in the first iteration. */
1921 if (integer_zerop (difference))
1922 {
1923 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1924 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1925 *last_conflicts = integer_one_node;
1926 return;
1927 }
1928
2146e26d 1929 if (!chrec_is_positive (initial_condition (difference), &value0))
1930 {
6b421feb 1931 if (dump_file && (dump_flags & TDF_DETAILS))
48e1416a 1932 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
6b421feb 1933
1934 dependence_stats.num_siv_unimplemented++;
87da4f2e 1935 *overlaps_a = conflict_fn_not_known ();
1936 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 1937 *last_conflicts = chrec_dont_know;
2146e26d 1938 return;
1939 }
1940 else
1941 {
1942 if (value0 == false)
1943 {
1944 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
1945 {
6b421feb 1946 if (dump_file && (dump_flags & TDF_DETAILS))
1947 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1948
87da4f2e 1949 *overlaps_a = conflict_fn_not_known ();
48e1416a 1950 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 1951 *last_conflicts = chrec_dont_know;
6b421feb 1952 dependence_stats.num_siv_unimplemented++;
2146e26d 1953 return;
1954 }
1955 else
1956 {
1957 if (value1 == true)
1958 {
48e1416a 1959 /* Example:
2146e26d 1960 chrec_a = 12
1961 chrec_b = {10, +, 1}
1962 */
48e1416a 1963
e7be49a3 1964 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2146e26d 1965 {
d500fef3 1966 HOST_WIDE_INT numiter;
1967 struct loop *loop = get_chrec_loop (chrec_b);
b6d41046 1968
87da4f2e 1969 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
9a3dad1c 1970 tmp = fold_build2 (EXACT_DIV_EXPR, type,
1971 fold_build1 (ABS_EXPR, type, difference),
87da4f2e 1972 CHREC_RIGHT (chrec_b));
1973 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
bc3c8ad4 1974 *last_conflicts = integer_one_node;
48e1416a 1975
b6d41046 1976
1977 /* Perform weak-zero siv test to see if overlap is
1978 outside the loop bounds. */
fee017b3 1979 numiter = max_stmt_executions_int (loop);
b6d41046 1980
d500fef3 1981 if (numiter >= 0
1982 && compare_tree_int (tmp, numiter) > 0)
b6d41046 1983 {
87da4f2e 1984 free_conflict_function (*overlaps_a);
1985 free_conflict_function (*overlaps_b);
1986 *overlaps_a = conflict_fn_no_dependence ();
1987 *overlaps_b = conflict_fn_no_dependence ();
b6d41046 1988 *last_conflicts = integer_zero_node;
6b421feb 1989 dependence_stats.num_siv_independent++;
b6d41046 1990 return;
48e1416a 1991 }
6b421feb 1992 dependence_stats.num_siv_dependent++;
2146e26d 1993 return;
1994 }
48e1416a 1995
e7be49a3 1996 /* When the step does not divide the difference, there are
2146e26d 1997 no overlaps. */
1998 else
1999 {
87da4f2e 2000 *overlaps_a = conflict_fn_no_dependence ();
48e1416a 2001 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 2002 *last_conflicts = integer_zero_node;
6b421feb 2003 dependence_stats.num_siv_independent++;
2146e26d 2004 return;
2005 }
2006 }
48e1416a 2007
2146e26d 2008 else
2009 {
48e1416a 2010 /* Example:
2146e26d 2011 chrec_a = 12
2012 chrec_b = {10, +, -1}
48e1416a 2013
2146e26d 2014 In this case, chrec_a will not overlap with chrec_b. */
87da4f2e 2015 *overlaps_a = conflict_fn_no_dependence ();
2016 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 2017 *last_conflicts = integer_zero_node;
6b421feb 2018 dependence_stats.num_siv_independent++;
2146e26d 2019 return;
2020 }
2021 }
2022 }
48e1416a 2023 else
2146e26d 2024 {
2025 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
2026 {
6b421feb 2027 if (dump_file && (dump_flags & TDF_DETAILS))
2028 fprintf (dump_file, "siv test failed: chrec not positive.\n");
2029
87da4f2e 2030 *overlaps_a = conflict_fn_not_known ();
48e1416a 2031 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 2032 *last_conflicts = chrec_dont_know;
6b421feb 2033 dependence_stats.num_siv_unimplemented++;
2146e26d 2034 return;
2035 }
2036 else
2037 {
2038 if (value2 == false)
2039 {
48e1416a 2040 /* Example:
2146e26d 2041 chrec_a = 3
2042 chrec_b = {10, +, -1}
2043 */
e7be49a3 2044 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2146e26d 2045 {
d500fef3 2046 HOST_WIDE_INT numiter;
2047 struct loop *loop = get_chrec_loop (chrec_b);
b6d41046 2048
87da4f2e 2049 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
9a3dad1c 2050 tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
87da4f2e 2051 CHREC_RIGHT (chrec_b));
2052 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
bc3c8ad4 2053 *last_conflicts = integer_one_node;
b6d41046 2054
2055 /* Perform weak-zero siv test to see if overlap is
2056 outside the loop bounds. */
fee017b3 2057 numiter = max_stmt_executions_int (loop);
b6d41046 2058
d500fef3 2059 if (numiter >= 0
2060 && compare_tree_int (tmp, numiter) > 0)
b6d41046 2061 {
87da4f2e 2062 free_conflict_function (*overlaps_a);
2063 free_conflict_function (*overlaps_b);
2064 *overlaps_a = conflict_fn_no_dependence ();
2065 *overlaps_b = conflict_fn_no_dependence ();
b6d41046 2066 *last_conflicts = integer_zero_node;
6b421feb 2067 dependence_stats.num_siv_independent++;
b6d41046 2068 return;
48e1416a 2069 }
6b421feb 2070 dependence_stats.num_siv_dependent++;
2146e26d 2071 return;
2072 }
48e1416a 2073
c4243fe7 2074 /* When the step does not divide the difference, there
2146e26d 2075 are no overlaps. */
2076 else
2077 {
87da4f2e 2078 *overlaps_a = conflict_fn_no_dependence ();
48e1416a 2079 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 2080 *last_conflicts = integer_zero_node;
6b421feb 2081 dependence_stats.num_siv_independent++;
2146e26d 2082 return;
2083 }
2084 }
2085 else
2086 {
48e1416a 2087 /* Example:
2088 chrec_a = 3
2146e26d 2089 chrec_b = {4, +, 1}
48e1416a 2090
2146e26d 2091 In this case, chrec_a will not overlap with chrec_b. */
87da4f2e 2092 *overlaps_a = conflict_fn_no_dependence ();
2093 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 2094 *last_conflicts = integer_zero_node;
6b421feb 2095 dependence_stats.num_siv_independent++;
2146e26d 2096 return;
2097 }
2098 }
2099 }
2100 }
2101}
2102
fbd828d4 2103/* Helper recursive function for initializing the matrix A. Returns
bc3c8ad4 2104 the initial value of CHREC. */
2146e26d 2105
8ceddf57 2106static tree
bc3c8ad4 2107initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2108{
2109 gcc_assert (chrec);
2110
8ceddf57 2111 switch (TREE_CODE (chrec))
2112 {
2113 case POLYNOMIAL_CHREC:
2114 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST);
2115
2116 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2117 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2118
2119 case PLUS_EXPR:
2120 case MULT_EXPR:
2121 case MINUS_EXPR:
2122 {
2123 tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2124 tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
2125
2126 return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
2127 }
2128
d09ef31a 2129 CASE_CONVERT:
8ceddf57 2130 {
2131 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
75a70cf9 2132 return chrec_convert (chrec_type (chrec), op, NULL);
8ceddf57 2133 }
2134
b6eab06c 2135 case BIT_NOT_EXPR:
2136 {
2137 /* Handle ~X as -1 - X. */
2138 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2139 return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
2140 build_int_cst (TREE_TYPE (chrec), -1), op);
2141 }
2142
8ceddf57 2143 case INTEGER_CST:
2144 return chrec;
c782188f 2145
8ceddf57 2146 default:
2147 gcc_unreachable ();
2148 return NULL_TREE;
2149 }
bc3c8ad4 2150}
2151
2152#define FLOOR_DIV(x,y) ((x) / (y))
2153
48e1416a 2154/* Solves the special case of the Diophantine equation:
bc3c8ad4 2155 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2156
2157 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2158 number of iterations that loops X and Y run. The overlaps will be
2159 constructed as evolutions in dimension DIM. */
2146e26d 2160
2161static void
48e1416a 2162compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
87da4f2e 2163 affine_fn *overlaps_a,
48e1416a 2164 affine_fn *overlaps_b,
bc3c8ad4 2165 tree *last_conflicts, int dim)
2166{
2167 if (((step_a > 0 && step_b > 0)
2168 || (step_a < 0 && step_b < 0)))
2169 {
2170 int step_overlaps_a, step_overlaps_b;
2171 int gcd_steps_a_b, last_conflict, tau2;
2172
2173 gcd_steps_a_b = gcd (step_a, step_b);
2174 step_overlaps_a = step_b / gcd_steps_a_b;
2175 step_overlaps_b = step_a / gcd_steps_a_b;
2176
e781c550 2177 if (niter > 0)
2178 {
2179 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2180 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2181 last_conflict = tau2;
2182 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2183 }
2184 else
2185 *last_conflicts = chrec_dont_know;
bc3c8ad4 2186
48e1416a 2187 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
87da4f2e 2188 build_int_cst (NULL_TREE,
2189 step_overlaps_a));
48e1416a 2190 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
2191 build_int_cst (NULL_TREE,
87da4f2e 2192 step_overlaps_b));
bc3c8ad4 2193 }
2194
2195 else
2196 {
87da4f2e 2197 *overlaps_a = affine_fn_cst (integer_zero_node);
2198 *overlaps_b = affine_fn_cst (integer_zero_node);
bc3c8ad4 2199 *last_conflicts = integer_zero_node;
2200 }
2201}
2202
bc3c8ad4 2203/* Solves the special case of a Diophantine equation where CHREC_A is
2204 an affine bivariate function, and CHREC_B is an affine univariate
48e1416a 2205 function. For example,
bc3c8ad4 2206
2207 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
48e1416a 2208
2209 has the following overlapping functions:
bc3c8ad4 2210
2211 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2212 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2213 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2214
0975351b 2215 FORNOW: This is a specialized implementation for a case occurring in
bc3c8ad4 2216 a common benchmark. Implement the general algorithm. */
2217
2218static void
48e1416a 2219compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
87da4f2e 2220 conflict_function **overlaps_a,
48e1416a 2221 conflict_function **overlaps_b,
bc3c8ad4 2222 tree *last_conflicts)
2146e26d 2223{
bc3c8ad4 2224 bool xz_p, yz_p, xyz_p;
2225 int step_x, step_y, step_z;
d500fef3 2226 HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
87da4f2e 2227 affine_fn overlaps_a_xz, overlaps_b_xz;
2228 affine_fn overlaps_a_yz, overlaps_b_yz;
2229 affine_fn overlaps_a_xyz, overlaps_b_xyz;
2230 affine_fn ova1, ova2, ovb;
2231 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
bc3c8ad4 2232
58f77a87 2233 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2234 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2235 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
bc3c8ad4 2236
fee017b3 2237 niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
2238 niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
2239 niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
48e1416a 2240
d500fef3 2241 if (niter_x < 0 || niter_y < 0 || niter_z < 0)
bc3c8ad4 2242 {
6b421feb 2243 if (dump_file && (dump_flags & TDF_DETAILS))
2244 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
48e1416a 2245
87da4f2e 2246 *overlaps_a = conflict_fn_not_known ();
2247 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 2248 *last_conflicts = chrec_dont_know;
2249 return;
2250 }
2251
bc3c8ad4 2252 niter = MIN (niter_x, niter_z);
2253 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2254 &overlaps_a_xz,
2255 &overlaps_b_xz,
2256 &last_conflicts_xz, 1);
2257 niter = MIN (niter_y, niter_z);
2258 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2259 &overlaps_a_yz,
2260 &overlaps_b_yz,
2261 &last_conflicts_yz, 2);
2262 niter = MIN (niter_x, niter_z);
2263 niter = MIN (niter_y, niter);
2264 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2265 &overlaps_a_xyz,
2266 &overlaps_b_xyz,
2267 &last_conflicts_xyz, 3);
2268
2269 xz_p = !integer_zerop (last_conflicts_xz);
2270 yz_p = !integer_zerop (last_conflicts_yz);
2271 xyz_p = !integer_zerop (last_conflicts_xyz);
2272
2273 if (xz_p || yz_p || xyz_p)
2274 {
87da4f2e 2275 ova1 = affine_fn_cst (integer_zero_node);
2276 ova2 = affine_fn_cst (integer_zero_node);
2277 ovb = affine_fn_cst (integer_zero_node);
bc3c8ad4 2278 if (xz_p)
2279 {
87da4f2e 2280 affine_fn t0 = ova1;
2281 affine_fn t2 = ovb;
2282
2283 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
2284 ovb = affine_fn_plus (ovb, overlaps_b_xz);
2285 affine_fn_free (t0);
2286 affine_fn_free (t2);
bc3c8ad4 2287 *last_conflicts = last_conflicts_xz;
2288 }
2289 if (yz_p)
2290 {
87da4f2e 2291 affine_fn t0 = ova2;
2292 affine_fn t2 = ovb;
2293
2294 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
2295 ovb = affine_fn_plus (ovb, overlaps_b_yz);
2296 affine_fn_free (t0);
2297 affine_fn_free (t2);
bc3c8ad4 2298 *last_conflicts = last_conflicts_yz;
2299 }
2300 if (xyz_p)
2301 {
87da4f2e 2302 affine_fn t0 = ova1;
2303 affine_fn t2 = ova2;
2304 affine_fn t4 = ovb;
2305
2306 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
2307 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
2308 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
2309 affine_fn_free (t0);
2310 affine_fn_free (t2);
2311 affine_fn_free (t4);
bc3c8ad4 2312 *last_conflicts = last_conflicts_xyz;
2313 }
87da4f2e 2314 *overlaps_a = conflict_fn (2, ova1, ova2);
2315 *overlaps_b = conflict_fn (1, ovb);
bc3c8ad4 2316 }
2317 else
2318 {
87da4f2e 2319 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2320 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
bc3c8ad4 2321 *last_conflicts = integer_zero_node;
2322 }
87da4f2e 2323
2324 affine_fn_free (overlaps_a_xz);
2325 affine_fn_free (overlaps_b_xz);
2326 affine_fn_free (overlaps_a_yz);
2327 affine_fn_free (overlaps_b_yz);
2328 affine_fn_free (overlaps_a_xyz);
2329 affine_fn_free (overlaps_b_xyz);
2146e26d 2330}
2331
e01f9f1f 2332/* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2333
2334static void
2335lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
2336 int size)
2337{
2338 memcpy (vec2, vec1, size * sizeof (*vec1));
2339}
2340
2341/* Copy the elements of M x N matrix MAT1 to MAT2. */
2342
2343static void
2344lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
2345 int m, int n)
2346{
2347 int i;
2348
2349 for (i = 0; i < m; i++)
2350 lambda_vector_copy (mat1[i], mat2[i], n);
2351}
2352
2353/* Store the N x N identity matrix in MAT. */
2354
2355static void
2356lambda_matrix_id (lambda_matrix mat, int size)
2357{
2358 int i, j;
2359
2360 for (i = 0; i < size; i++)
2361 for (j = 0; j < size; j++)
2362 mat[i][j] = (i == j) ? 1 : 0;
2363}
2364
2365/* Return the first nonzero element of vector VEC1 between START and N.
2366 We must have START <= N. Returns N if VEC1 is the zero vector. */
2367
2368static int
2369lambda_vector_first_nz (lambda_vector vec1, int n, int start)
2370{
2371 int j = start;
2372 while (j < n && vec1[j] == 0)
2373 j++;
2374 return j;
2375}
2376
2377/* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2378 R2 = R2 + CONST1 * R1. */
2379
2380static void
2381lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2, int const1)
2382{
2383 int i;
2384
2385 if (const1 == 0)
2386 return;
2387
2388 for (i = 0; i < n; i++)
2389 mat[r2][i] += const1 * mat[r1][i];
2390}
2391
e01f9f1f 2392/* Multiply vector VEC1 of length SIZE by a constant CONST1,
2393 and store the result in VEC2. */
2394
2395static void
2396lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
2397 int size, int const1)
2398{
2399 int i;
2400
2401 if (const1 == 0)
2402 lambda_vector_clear (vec2, size);
2403 else
2404 for (i = 0; i < size; i++)
2405 vec2[i] = const1 * vec1[i];
2406}
2407
2408/* Negate vector VEC1 with length SIZE and store it in VEC2. */
2409
2410static void
2411lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
2412 int size)
2413{
2414 lambda_vector_mult_const (vec1, vec2, size, -1);
2415}
2416
2417/* Negate row R1 of matrix MAT which has N columns. */
2418
2419static void
2420lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
2421{
2422 lambda_vector_negate (mat[r1], mat[r1], n);
2423}
2424
2425/* Return true if two vectors are equal. */
2426
2427static bool
2428lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
2429{
2430 int i;
2431 for (i = 0; i < size; i++)
2432 if (vec1[i] != vec2[i])
2433 return false;
2434 return true;
2435}
2436
2437/* Given an M x N integer matrix A, this function determines an M x
2438 M unimodular matrix U, and an M x N echelon matrix S such that
2439 "U.A = S". This decomposition is also known as "right Hermite".
2440
2441 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2442 Restructuring Compilers" Utpal Banerjee. */
2443
2444static void
2445lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
2446 lambda_matrix S, lambda_matrix U)
2447{
2448 int i, j, i0 = 0;
2449
2450 lambda_matrix_copy (A, S, m, n);
2451 lambda_matrix_id (U, m);
2452
2453 for (j = 0; j < n; j++)
2454 {
2455 if (lambda_vector_first_nz (S[j], m, i0) < m)
2456 {
2457 ++i0;
2458 for (i = m - 1; i >= i0; i--)
2459 {
2460 while (S[i][j] != 0)
2461 {
2462 int sigma, factor, a, b;
2463
2464 a = S[i-1][j];
2465 b = S[i][j];
2466 sigma = (a * b < 0) ? -1: 1;
2467 a = abs (a);
2468 b = abs (b);
2469 factor = sigma * (a / b);
2470
2471 lambda_matrix_row_add (S, n, i, i-1, -factor);
dfcf26a5 2472 std::swap (S[i], S[i-1]);
e01f9f1f 2473
2474 lambda_matrix_row_add (U, m, i, i-1, -factor);
dfcf26a5 2475 std::swap (U[i], U[i-1]);
e01f9f1f 2476 }
2477 }
2478 }
2479 }
2480}
2481
2146e26d 2482/* Determines the overlapping elements due to accesses CHREC_A and
6b421feb 2483 CHREC_B, that are affine functions. This function cannot handle
2484 symbolic evolution functions, ie. when initial conditions are
2485 parameters, because it uses lambda matrices of integers. */
2146e26d 2486
2487static void
48e1416a 2488analyze_subscript_affine_affine (tree chrec_a,
2146e26d 2489 tree chrec_b,
48e1416a 2490 conflict_function **overlaps_a,
2491 conflict_function **overlaps_b,
bc3c8ad4 2492 tree *last_conflicts)
2146e26d 2493{
bc3c8ad4 2494 unsigned nb_vars_a, nb_vars_b, dim;
4ee81b10 2495 HOST_WIDE_INT init_a, init_b, gamma, gcd_alpha_beta;
bc3c8ad4 2496 lambda_matrix A, U, S;
1e33ad50 2497 struct obstack scratch_obstack;
bc3c8ad4 2498
f84a688a 2499 if (eq_evolutions_p (chrec_a, chrec_b))
b6d41046 2500 {
f84a688a 2501 /* The accessed index overlaps for each iteration in the
2502 loop. */
87da4f2e 2503 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2504 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
b6d41046 2505 *last_conflicts = chrec_dont_know;
2506 return;
2507 }
2146e26d 2508 if (dump_file && (dump_flags & TDF_DETAILS))
2509 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
48e1416a 2510
2146e26d 2511 /* For determining the initial intersection, we have to solve a
2512 Diophantine equation. This is the most time consuming part.
48e1416a 2513
2146e26d 2514 For answering to the question: "Is there a dependence?" we have
2515 to prove that there exists a solution to the Diophantine
2516 equation, and that the solution is in the iteration domain,
5c9dae64 2517 i.e. the solution is positive or zero, and that the solution
2146e26d 2518 happens before the upper bound loop.nb_iterations. Otherwise
2519 there is no dependence. This function outputs a description of
2520 the iterations that hold the intersections. */
2521
bc3c8ad4 2522 nb_vars_a = nb_vars_in_chrec (chrec_a);
2523 nb_vars_b = nb_vars_in_chrec (chrec_b);
2524
1e33ad50 2525 gcc_obstack_init (&scratch_obstack);
2526
bc3c8ad4 2527 dim = nb_vars_a + nb_vars_b;
1e33ad50 2528 U = lambda_matrix_new (dim, dim, &scratch_obstack);
2529 A = lambda_matrix_new (dim, 1, &scratch_obstack);
2530 S = lambda_matrix_new (dim, 1, &scratch_obstack);
bc3c8ad4 2531
8ceddf57 2532 init_a = int_cst_value (initialize_matrix_A (A, chrec_a, 0, 1));
2533 init_b = int_cst_value (initialize_matrix_A (A, chrec_b, nb_vars_a, -1));
bc3c8ad4 2534 gamma = init_b - init_a;
2535
2536 /* Don't do all the hard work of solving the Diophantine equation
48e1416a 2537 when we already know the solution: for example,
bc3c8ad4 2538 | {3, +, 1}_1
2539 | {3, +, 4}_2
2540 | gamma = 3 - 3 = 0.
48e1416a 2541 Then the first overlap occurs during the first iterations:
bc3c8ad4 2542 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2543 */
2544 if (gamma == 0)
2146e26d 2545 {
bc3c8ad4 2546 if (nb_vars_a == 1 && nb_vars_b == 1)
2146e26d 2547 {
4ee81b10 2548 HOST_WIDE_INT step_a, step_b;
d500fef3 2549 HOST_WIDE_INT niter, niter_a, niter_b;
87da4f2e 2550 affine_fn ova, ovb;
bc3c8ad4 2551
fee017b3 2552 niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
2553 niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
bc3c8ad4 2554 niter = MIN (niter_a, niter_b);
58f77a87 2555 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2556 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
bc3c8ad4 2557
48e1416a 2558 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2559 &ova, &ovb,
bc3c8ad4 2560 last_conflicts, 1);
87da4f2e 2561 *overlaps_a = conflict_fn (1, ova);
2562 *overlaps_b = conflict_fn (1, ovb);
2146e26d 2563 }
bc3c8ad4 2564
2565 else if (nb_vars_a == 2 && nb_vars_b == 1)
2566 compute_overlap_steps_for_affine_1_2
2567 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2568
2569 else if (nb_vars_a == 1 && nb_vars_b == 2)
2570 compute_overlap_steps_for_affine_1_2
2571 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2572
2573 else
2146e26d 2574 {
6b421feb 2575 if (dump_file && (dump_flags & TDF_DETAILS))
2576 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
87da4f2e 2577 *overlaps_a = conflict_fn_not_known ();
2578 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 2579 *last_conflicts = chrec_dont_know;
2146e26d 2580 }
6b421feb 2581 goto end_analyze_subs_aa;
bc3c8ad4 2582 }
2583
2584 /* U.A = S */
2585 lambda_matrix_right_hermite (A, dim, 1, S, U);
2586
2587 if (S[0][0] < 0)
2588 {
2589 S[0][0] *= -1;
2590 lambda_matrix_row_negate (U, dim, 0);
2591 }
2592 gcd_alpha_beta = S[0][0];
2593
b44d1046 2594 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2595 but that is a quite strange case. Instead of ICEing, answer
2596 don't know. */
2597 if (gcd_alpha_beta == 0)
2598 {
87da4f2e 2599 *overlaps_a = conflict_fn_not_known ();
2600 *overlaps_b = conflict_fn_not_known ();
b44d1046 2601 *last_conflicts = chrec_dont_know;
2602 goto end_analyze_subs_aa;
2603 }
2604
bc3c8ad4 2605 /* The classic "gcd-test". */
2606 if (!int_divides_p (gcd_alpha_beta, gamma))
2607 {
2608 /* The "gcd-test" has determined that there is no integer
2609 solution, i.e. there is no dependence. */
87da4f2e 2610 *overlaps_a = conflict_fn_no_dependence ();
2611 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 2612 *last_conflicts = integer_zero_node;
2613 }
2614
2615 /* Both access functions are univariate. This includes SIV and MIV cases. */
2616 else if (nb_vars_a == 1 && nb_vars_b == 1)
2617 {
2618 /* Both functions should have the same evolution sign. */
2619 if (((A[0][0] > 0 && -A[1][0] > 0)
2620 || (A[0][0] < 0 && -A[1][0] < 0)))
2146e26d 2621 {
2622 /* The solutions are given by:
48e1416a 2623 |
bc3c8ad4 2624 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2625 | [u21 u22] [y0]
48e1416a 2626
2146e26d 2627 For a given integer t. Using the following variables,
48e1416a 2628
2146e26d 2629 | i0 = u11 * gamma / gcd_alpha_beta
2630 | j0 = u12 * gamma / gcd_alpha_beta
2631 | i1 = u21
2632 | j1 = u22
48e1416a 2633
2146e26d 2634 the solutions are:
48e1416a 2635
2636 | x0 = i0 + i1 * t,
bc3c8ad4 2637 | y0 = j0 + j1 * t. */
e781c550 2638 HOST_WIDE_INT i0, j0, i1, j1;
bc3c8ad4 2639
2640 i0 = U[0][0] * gamma / gcd_alpha_beta;
2641 j0 = U[0][1] * gamma / gcd_alpha_beta;
2642 i1 = U[1][0];
2643 j1 = U[1][1];
2644
2645 if ((i1 == 0 && i0 < 0)
2646 || (j1 == 0 && j0 < 0))
2146e26d 2647 {
48e1416a 2648 /* There is no solution.
2649 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2650 falls in here, but for the moment we don't look at the
2146e26d 2651 upper bound of the iteration domain. */
87da4f2e 2652 *overlaps_a = conflict_fn_no_dependence ();
2653 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 2654 *last_conflicts = integer_zero_node;
e781c550 2655 goto end_analyze_subs_aa;
bc3c8ad4 2656 }
2657
e781c550 2658 if (i1 > 0 && j1 > 0)
2146e26d 2659 {
fee017b3 2660 HOST_WIDE_INT niter_a
2661 = max_stmt_executions_int (get_chrec_loop (chrec_a));
2662 HOST_WIDE_INT niter_b
2663 = max_stmt_executions_int (get_chrec_loop (chrec_b));
e781c550 2664 HOST_WIDE_INT niter = MIN (niter_a, niter_b);
2665
2666 /* (X0, Y0) is a solution of the Diophantine equation:
2667 "chrec_a (X0) = chrec_b (Y0)". */
2668 HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
2669 CEIL (-j0, j1));
2670 HOST_WIDE_INT x0 = i1 * tau1 + i0;
2671 HOST_WIDE_INT y0 = j1 * tau1 + j0;
2672
2673 /* (X1, Y1) is the smallest positive solution of the eq
2674 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2675 first conflict occurs. */
2676 HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
2677 HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
2678 HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
2679
2680 if (niter > 0)
2146e26d 2681 {
e781c550 2682 HOST_WIDE_INT tau2 = MIN (FLOOR_DIV (niter - i0, i1),
2683 FLOOR_DIV (niter - j0, j1));
2684 HOST_WIDE_INT last_conflict = tau2 - (x1 - i0)/i1;
bc3c8ad4 2685
e781c550 2686 /* If the overlap occurs outside of the bounds of the
2687 loop, there is no dependence. */
63ca8934 2688 if (x1 >= niter || y1 >= niter)
2146e26d 2689 {
e781c550 2690 *overlaps_a = conflict_fn_no_dependence ();
2691 *overlaps_b = conflict_fn_no_dependence ();
2692 *last_conflicts = integer_zero_node;
2693 goto end_analyze_subs_aa;
2146e26d 2694 }
2695 else
e781c550 2696 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2146e26d 2697 }
2146e26d 2698 else
e781c550 2699 *last_conflicts = chrec_dont_know;
2700
2701 *overlaps_a
2702 = conflict_fn (1,
2703 affine_fn_univar (build_int_cst (NULL_TREE, x1),
2704 1,
2705 build_int_cst (NULL_TREE, i1)));
2706 *overlaps_b
2707 = conflict_fn (1,
2708 affine_fn_univar (build_int_cst (NULL_TREE, y1),
2709 1,
2710 build_int_cst (NULL_TREE, j1)));
2711 }
2712 else
2713 {
2714 /* FIXME: For the moment, the upper bound of the
2715 iteration domain for i and j is not checked. */
2716 if (dump_file && (dump_flags & TDF_DETAILS))
2717 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2718 *overlaps_a = conflict_fn_not_known ();
2719 *overlaps_b = conflict_fn_not_known ();
2720 *last_conflicts = chrec_dont_know;
2146e26d 2721 }
2722 }
bc3c8ad4 2723 else
2724 {
6b421feb 2725 if (dump_file && (dump_flags & TDF_DETAILS))
2726 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
87da4f2e 2727 *overlaps_a = conflict_fn_not_known ();
2728 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 2729 *last_conflicts = chrec_dont_know;
2730 }
2146e26d 2731 }
2146e26d 2732 else
2733 {
6b421feb 2734 if (dump_file && (dump_flags & TDF_DETAILS))
2735 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
87da4f2e 2736 *overlaps_a = conflict_fn_not_known ();
2737 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 2738 *last_conflicts = chrec_dont_know;
2146e26d 2739 }
bc3c8ad4 2740
48e1416a 2741end_analyze_subs_aa:
1e33ad50 2742 obstack_free (&scratch_obstack, NULL);
2146e26d 2743 if (dump_file && (dump_flags & TDF_DETAILS))
2744 {
2745 fprintf (dump_file, " (overlaps_a = ");
87da4f2e 2746 dump_conflict_function (dump_file, *overlaps_a);
2146e26d 2747 fprintf (dump_file, ")\n (overlaps_b = ");
87da4f2e 2748 dump_conflict_function (dump_file, *overlaps_b);
984cebc1 2749 fprintf (dump_file, "))\n");
2146e26d 2750 }
6b421feb 2751}
2752
2753/* Returns true when analyze_subscript_affine_affine can be used for
2754 determining the dependence relation between chrec_a and chrec_b,
2755 that contain symbols. This function modifies chrec_a and chrec_b
2756 such that the analysis result is the same, and such that they don't
48e1416a 2757 contain symbols, and then can safely be passed to the analyzer.
6b421feb 2758
2759 Example: The analysis of the following tuples of evolutions produce
2760 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2761 vs. {0, +, 1}_1
48e1416a 2762
6b421feb 2763 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2764 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2765*/
2766
2767static bool
2768can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
2769{
b92381b6 2770 tree diff, type, left_a, left_b, right_b;
6b421feb 2771
2772 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
2773 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
2774 /* FIXME: For the moment not handled. Might be refined later. */
2775 return false;
2776
b92381b6 2777 type = chrec_type (*chrec_a);
2778 left_a = CHREC_LEFT (*chrec_a);
75a70cf9 2779 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
b92381b6 2780 diff = chrec_fold_minus (type, left_a, left_b);
2781
6b421feb 2782 if (!evolution_function_is_constant_p (diff))
2783 return false;
2784
2146e26d 2785 if (dump_file && (dump_flags & TDF_DETAILS))
6b421feb 2786 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
2787
48e1416a 2788 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
6b421feb 2789 diff, CHREC_RIGHT (*chrec_a));
75a70cf9 2790 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
6b421feb 2791 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
7c010d6a 2792 build_int_cst (type, 0),
b92381b6 2793 right_b);
6b421feb 2794 return true;
2146e26d 2795}
2796
2797/* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2798 *OVERLAPS_B are initialized to the functions that describe the
2799 relation between the elements accessed twice by CHREC_A and
2800 CHREC_B. For k >= 0, the following property is verified:
2801
2802 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2803
2804static void
48e1416a 2805analyze_siv_subscript (tree chrec_a,
2146e26d 2806 tree chrec_b,
48e1416a 2807 conflict_function **overlaps_a,
2808 conflict_function **overlaps_b,
8ceddf57 2809 tree *last_conflicts,
2810 int loop_nest_num)
2146e26d 2811{
6b421feb 2812 dependence_stats.num_siv++;
48e1416a 2813
2146e26d 2814 if (dump_file && (dump_flags & TDF_DETAILS))
2815 fprintf (dump_file, "(analyze_siv_subscript \n");
48e1416a 2816
2146e26d 2817 if (evolution_function_is_constant_p (chrec_a)
8ceddf57 2818 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
48e1416a 2819 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
bc3c8ad4 2820 overlaps_a, overlaps_b, last_conflicts);
48e1416a 2821
8ceddf57 2822 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2146e26d 2823 && evolution_function_is_constant_p (chrec_b))
48e1416a 2824 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
bc3c8ad4 2825 overlaps_b, overlaps_a, last_conflicts);
48e1416a 2826
8ceddf57 2827 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2828 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
6b421feb 2829 {
2830 if (!chrec_contains_symbols (chrec_a)
2831 && !chrec_contains_symbols (chrec_b))
2832 {
48e1416a 2833 analyze_subscript_affine_affine (chrec_a, chrec_b,
2834 overlaps_a, overlaps_b,
6b421feb 2835 last_conflicts);
2836
87da4f2e 2837 if (CF_NOT_KNOWN_P (*overlaps_a)
2838 || CF_NOT_KNOWN_P (*overlaps_b))
6b421feb 2839 dependence_stats.num_siv_unimplemented++;
87da4f2e 2840 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2841 || CF_NO_DEPENDENCE_P (*overlaps_b))
6b421feb 2842 dependence_stats.num_siv_independent++;
2843 else
2844 dependence_stats.num_siv_dependent++;
2845 }
48e1416a 2846 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
6b421feb 2847 &chrec_b))
2848 {
48e1416a 2849 analyze_subscript_affine_affine (chrec_a, chrec_b,
2850 overlaps_a, overlaps_b,
6b421feb 2851 last_conflicts);
6b421feb 2852
87da4f2e 2853 if (CF_NOT_KNOWN_P (*overlaps_a)
2854 || CF_NOT_KNOWN_P (*overlaps_b))
6b421feb 2855 dependence_stats.num_siv_unimplemented++;
87da4f2e 2856 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2857 || CF_NO_DEPENDENCE_P (*overlaps_b))
6b421feb 2858 dependence_stats.num_siv_independent++;
2859 else
2860 dependence_stats.num_siv_dependent++;
2861 }
2862 else
2863 goto siv_subscript_dontknow;
2864 }
2865
2146e26d 2866 else
2867 {
6b421feb 2868 siv_subscript_dontknow:;
2869 if (dump_file && (dump_flags & TDF_DETAILS))
984cebc1 2870 fprintf (dump_file, " siv test failed: unimplemented");
87da4f2e 2871 *overlaps_a = conflict_fn_not_known ();
2872 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 2873 *last_conflicts = chrec_dont_know;
6b421feb 2874 dependence_stats.num_siv_unimplemented++;
2146e26d 2875 }
48e1416a 2876
2146e26d 2877 if (dump_file && (dump_flags & TDF_DETAILS))
2878 fprintf (dump_file, ")\n");
2879}
2880
cabedb72 2881/* Returns false if we can prove that the greatest common divisor of the steps
2882 of CHREC does not divide CST, false otherwise. */
2146e26d 2883
2884static bool
7ecb5bb2 2885gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
2146e26d 2886{
cabedb72 2887 HOST_WIDE_INT cd = 0, val;
2888 tree step;
6b421feb 2889
e913b5cd 2890 if (!tree_fits_shwi_p (cst))
cabedb72 2891 return true;
e913b5cd 2892 val = tree_to_shwi (cst);
cabedb72 2893
2894 while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
2895 {
2896 step = CHREC_RIGHT (chrec);
e913b5cd 2897 if (!tree_fits_shwi_p (step))
cabedb72 2898 return true;
e913b5cd 2899 cd = gcd (cd, tree_to_shwi (step));
cabedb72 2900 chrec = CHREC_LEFT (chrec);
2146e26d 2901 }
cabedb72 2902
2903 return val % cd == 0;
2146e26d 2904}
2905
8c7b1d8e 2906/* Analyze a MIV (Multiple Index Variable) subscript with respect to
2907 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2908 functions that describe the relation between the elements accessed
2909 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2910 is verified:
2146e26d 2911
2912 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2913
2914static void
48e1416a 2915analyze_miv_subscript (tree chrec_a,
2916 tree chrec_b,
2917 conflict_function **overlaps_a,
2918 conflict_function **overlaps_b,
8c7b1d8e 2919 tree *last_conflicts,
2920 struct loop *loop_nest)
2146e26d 2921{
9a3dad1c 2922 tree type, difference;
2923
6b421feb 2924 dependence_stats.num_miv++;
2146e26d 2925 if (dump_file && (dump_flags & TDF_DETAILS))
2926 fprintf (dump_file, "(analyze_miv_subscript \n");
f84a688a 2927
9a3dad1c 2928 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
75a70cf9 2929 chrec_a = chrec_convert (type, chrec_a, NULL);
2930 chrec_b = chrec_convert (type, chrec_b, NULL);
9a3dad1c 2931 difference = chrec_fold_minus (type, chrec_a, chrec_b);
48e1416a 2932
f84a688a 2933 if (eq_evolutions_p (chrec_a, chrec_b))
2146e26d 2934 {
2935 /* Access functions are the same: all the elements are accessed
2936 in the same order. */
87da4f2e 2937 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2938 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
8fe79ba5 2939 *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
6b421feb 2940 dependence_stats.num_miv_dependent++;
2146e26d 2941 }
48e1416a 2942
2146e26d 2943 else if (evolution_function_is_constant_p (difference)
2944 /* For the moment, the following is verified:
8c7b1d8e 2945 evolution_function_is_affine_multivariate_p (chrec_a,
2946 loop_nest->num) */
cabedb72 2947 && !gcd_of_steps_may_divide_p (chrec_a, difference))
2146e26d 2948 {
2949 /* testsuite/.../ssa-chrec-33.c
48e1416a 2950 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2951
cabedb72 2952 The difference is 1, and all the evolution steps are multiples
2953 of 2, consequently there are no overlapping elements. */
87da4f2e 2954 *overlaps_a = conflict_fn_no_dependence ();
2955 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 2956 *last_conflicts = integer_zero_node;
6b421feb 2957 dependence_stats.num_miv_independent++;
2146e26d 2958 }
48e1416a 2959
8c7b1d8e 2960 else if (evolution_function_is_affine_multivariate_p (chrec_a, loop_nest->num)
6b421feb 2961 && !chrec_contains_symbols (chrec_a)
8c7b1d8e 2962 && evolution_function_is_affine_multivariate_p (chrec_b, loop_nest->num)
6b421feb 2963 && !chrec_contains_symbols (chrec_b))
2146e26d 2964 {
2965 /* testsuite/.../ssa-chrec-35.c
2966 {0, +, 1}_2 vs. {0, +, 1}_3
2967 the overlapping elements are respectively located at iterations:
48e1416a 2968 {0, +, 1}_x and {0, +, 1}_x,
2969 in other words, we have the equality:
bc3c8ad4 2970 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
48e1416a 2971
2972 Other examples:
2973 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
bc3c8ad4 2974 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2975
48e1416a 2976 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
bc3c8ad4 2977 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2146e26d 2978 */
48e1416a 2979 analyze_subscript_affine_affine (chrec_a, chrec_b,
bc3c8ad4 2980 overlaps_a, overlaps_b, last_conflicts);
6b421feb 2981
87da4f2e 2982 if (CF_NOT_KNOWN_P (*overlaps_a)
2983 || CF_NOT_KNOWN_P (*overlaps_b))
6b421feb 2984 dependence_stats.num_miv_unimplemented++;
87da4f2e 2985 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2986 || CF_NO_DEPENDENCE_P (*overlaps_b))
6b421feb 2987 dependence_stats.num_miv_independent++;
2988 else
2989 dependence_stats.num_miv_dependent++;
2146e26d 2990 }
48e1416a 2991
2146e26d 2992 else
2993 {
2994 /* When the analysis is too difficult, answer "don't know". */
6b421feb 2995 if (dump_file && (dump_flags & TDF_DETAILS))
2996 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
2997
87da4f2e 2998 *overlaps_a = conflict_fn_not_known ();
2999 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 3000 *last_conflicts = chrec_dont_know;
6b421feb 3001 dependence_stats.num_miv_unimplemented++;
2146e26d 3002 }
48e1416a 3003
2146e26d 3004 if (dump_file && (dump_flags & TDF_DETAILS))
3005 fprintf (dump_file, ")\n");
3006}
3007
8c7b1d8e 3008/* Determines the iterations for which CHREC_A is equal to CHREC_B in
3009 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
3010 OVERLAP_ITERATIONS_B are initialized with two functions that
3011 describe the iterations that contain conflicting elements.
48e1416a 3012
2146e26d 3013 Remark: For an integer k >= 0, the following equality is true:
48e1416a 3014
2146e26d 3015 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
3016*/
3017
48e1416a 3018static void
3019analyze_overlapping_iterations (tree chrec_a,
3020 tree chrec_b,
3021 conflict_function **overlap_iterations_a,
3022 conflict_function **overlap_iterations_b,
8c7b1d8e 3023 tree *last_conflicts, struct loop *loop_nest)
2146e26d 3024{
8c7b1d8e 3025 unsigned int lnn = loop_nest->num;
3026
6b421feb 3027 dependence_stats.num_subscript_tests++;
48e1416a 3028
2146e26d 3029 if (dump_file && (dump_flags & TDF_DETAILS))
3030 {
3031 fprintf (dump_file, "(analyze_overlapping_iterations \n");
3032 fprintf (dump_file, " (chrec_a = ");
3033 print_generic_expr (dump_file, chrec_a, 0);
6b421feb 3034 fprintf (dump_file, ")\n (chrec_b = ");
2146e26d 3035 print_generic_expr (dump_file, chrec_b, 0);
3036 fprintf (dump_file, ")\n");
3037 }
6b421feb 3038
2146e26d 3039 if (chrec_a == NULL_TREE
3040 || chrec_b == NULL_TREE
3041 || chrec_contains_undetermined (chrec_a)
6b421feb 3042 || chrec_contains_undetermined (chrec_b))
2146e26d 3043 {
6b421feb 3044 dependence_stats.num_subscript_undetermined++;
48e1416a 3045
87da4f2e 3046 *overlap_iterations_a = conflict_fn_not_known ();
3047 *overlap_iterations_b = conflict_fn_not_known ();
2146e26d 3048 }
6b421feb 3049
48e1416a 3050 /* If they are the same chrec, and are affine, they overlap
6b421feb 3051 on every iteration. */
3052 else if (eq_evolutions_p (chrec_a, chrec_b)
328218c2 3053 && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3054 || operand_equal_p (chrec_a, chrec_b, 0)))
6b421feb 3055 {
3056 dependence_stats.num_same_subscript_function++;
87da4f2e 3057 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3058 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
6b421feb 3059 *last_conflicts = chrec_dont_know;
3060 }
3061
3062 /* If they aren't the same, and aren't affine, we can't do anything
328218c2 3063 yet. */
48e1416a 3064 else if ((chrec_contains_symbols (chrec_a)
6b421feb 3065 || chrec_contains_symbols (chrec_b))
8c7b1d8e 3066 && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3067 || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
6b421feb 3068 {
3069 dependence_stats.num_subscript_undetermined++;
87da4f2e 3070 *overlap_iterations_a = conflict_fn_not_known ();
3071 *overlap_iterations_b = conflict_fn_not_known ();
6b421feb 3072 }
3073
2146e26d 3074 else if (ziv_subscript_p (chrec_a, chrec_b))
48e1416a 3075 analyze_ziv_subscript (chrec_a, chrec_b,
bc3c8ad4 3076 overlap_iterations_a, overlap_iterations_b,
3077 last_conflicts);
48e1416a 3078
2146e26d 3079 else if (siv_subscript_p (chrec_a, chrec_b))
48e1416a 3080 analyze_siv_subscript (chrec_a, chrec_b,
3081 overlap_iterations_a, overlap_iterations_b,
8ceddf57 3082 last_conflicts, lnn);
48e1416a 3083
2146e26d 3084 else
48e1416a 3085 analyze_miv_subscript (chrec_a, chrec_b,
bc3c8ad4 3086 overlap_iterations_a, overlap_iterations_b,
8c7b1d8e 3087 last_conflicts, loop_nest);
48e1416a 3088
2146e26d 3089 if (dump_file && (dump_flags & TDF_DETAILS))
3090 {
3091 fprintf (dump_file, " (overlap_iterations_a = ");
87da4f2e 3092 dump_conflict_function (dump_file, *overlap_iterations_a);
2146e26d 3093 fprintf (dump_file, ")\n (overlap_iterations_b = ");
87da4f2e 3094 dump_conflict_function (dump_file, *overlap_iterations_b);
984cebc1 3095 fprintf (dump_file, "))\n");
2146e26d 3096 }
3097}
3098
b44d1046 3099/* Helper function for uniquely inserting distance vectors. */
2146e26d 3100
b44d1046 3101static void
3102save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
3103{
3104 unsigned i;
3105 lambda_vector v;
2146e26d 3106
f1f41a6c 3107 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, v)
b44d1046 3108 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
3109 return;
2146e26d 3110
f1f41a6c 3111 DDR_DIST_VECTS (ddr).safe_push (dist_v);
b44d1046 3112}
2146e26d 3113
b44d1046 3114/* Helper function for uniquely inserting direction vectors. */
3115
3116static void
3117save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
2146e26d 3118{
3119 unsigned i;
b44d1046 3120 lambda_vector v;
6b421feb 3121
f1f41a6c 3122 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), i, v)
b44d1046 3123 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
3124 return;
3125
f1f41a6c 3126 DDR_DIR_VECTS (ddr).safe_push (dir_v);
b44d1046 3127}
3128
3129/* Add a distance of 1 on all the loops outer than INDEX. If we
3130 haven't yet determined a distance for this outer loop, push a new
3131 distance vector composed of the previous distance, and a distance
3132 of 1 for this outer loop. Example:
3133
3134 | loop_1
3135 | loop_2
3136 | A[10]
3137 | endloop_2
3138 | endloop_1
3139
3140 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3141 save (0, 1), then we have to save (1, 0). */
3142
3143static void
3144add_outer_distances (struct data_dependence_relation *ddr,
3145 lambda_vector dist_v, int index)
3146{
3147 /* For each outer loop where init_v is not set, the accesses are
3148 in dependence of distance 1 in the loop. */
3149 while (--index >= 0)
3150 {
3151 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3152 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3153 save_v[index] = 1;
3154 save_dist_v (ddr, save_v);
3155 }
3156}
3157
3158/* Return false when fail to represent the data dependence as a
3159 distance vector. INIT_B is set to true when a component has been
3160 added to the distance vector DIST_V. INDEX_CARRY is then set to
3161 the index in DIST_V that carries the dependence. */
3162
3163static bool
3164build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
3165 struct data_reference *ddr_a,
3166 struct data_reference *ddr_b,
3167 lambda_vector dist_v, bool *init_b,
3168 int *index_carry)
3169{
3170 unsigned i;
3171 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
6b421feb 3172
6b6f234c 3173 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
2146e26d 3174 {
bc3c8ad4 3175 tree access_fn_a, access_fn_b;
6b6f234c 3176 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
2146e26d 3177
3178 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
bc3c8ad4 3179 {
3180 non_affine_dependence_relation (ddr);
b44d1046 3181 return false;
bc3c8ad4 3182 }
3183
b44d1046 3184 access_fn_a = DR_ACCESS_FN (ddr_a, i);
3185 access_fn_b = DR_ACCESS_FN (ddr_b, i);
2146e26d 3186
48e1416a 3187 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
bc3c8ad4 3188 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
2146e26d 3189 {
b44d1046 3190 int dist, index;
ba3fc4d0 3191 int var_a = CHREC_VARIABLE (access_fn_a);
3192 int var_b = CHREC_VARIABLE (access_fn_b);
b44d1046 3193
ba3fc4d0 3194 if (var_a != var_b
3195 || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
bc3c8ad4 3196 {
3197 non_affine_dependence_relation (ddr);
b44d1046 3198 return false;
bc3c8ad4 3199 }
48e1416a 3200
58f77a87 3201 dist = int_cst_value (SUB_DISTANCE (subscript));
ba3fc4d0 3202 index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
3203 *index_carry = MIN (index, *index_carry);
2146e26d 3204
b44d1046 3205 /* This is the subscript coupling test. If we have already
3206 recorded a distance for this loop (a distance coming from
3207 another subscript), it should be the same. For example,
3208 in the following code, there is no dependence:
3209
2146e26d 3210 | loop i = 0, N, 1
3211 | T[i+1][i] = ...
3212 | ... = T[i][i]
3213 | endloop
b44d1046 3214 */
3215 if (init_v[index] != 0 && dist_v[index] != dist)
2146e26d 3216 {
6b6f234c 3217 finalize_ddr_dependent (ddr, chrec_known);
b44d1046 3218 return false;
2146e26d 3219 }
3220
b44d1046 3221 dist_v[index] = dist;
3222 init_v[index] = 1;
3223 *init_b = true;
3224 }
9c77efff 3225 else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
b44d1046 3226 {
3227 /* This can be for example an affine vs. constant dependence
3228 (T[i] vs. T[3]) that is not an affine dependence and is
3229 not representable as a distance vector. */
3230 non_affine_dependence_relation (ddr);
3231 return false;
2146e26d 3232 }
3233 }
1532ec98 3234
b44d1046 3235 return true;
3236}
1532ec98 3237
2a21a5df 3238/* Return true when the DDR contains only constant access functions. */
3239
3240static bool
7ecb5bb2 3241constant_access_functions (const struct data_dependence_relation *ddr)
2a21a5df 3242{
3243 unsigned i;
3244
3245 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3246 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr), i))
3247 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr), i)))
3248 return false;
3249
3250 return true;
3251}
3252
b44d1046 3253/* Helper function for the case where DDR_A and DDR_B are the same
bfe20447 3254 multivariate access function with a constant step. For an example
3255 see pr34635-1.c. */
bc3c8ad4 3256
b44d1046 3257static void
3258add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
3259{
3260 int x_1, x_2;
3261 tree c_1 = CHREC_LEFT (c_2);
3262 tree c_0 = CHREC_LEFT (c_1);
3263 lambda_vector dist_v;
8fb896c4 3264 int v1, v2, cd;
bc3c8ad4 3265
bb11a0e8 3266 /* Polynomials with more than 2 variables are not handled yet. When
3267 the evolution steps are parameters, it is not possible to
3268 represent the dependence using classical distance vectors. */
3269 if (TREE_CODE (c_0) != INTEGER_CST
3270 || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
3271 || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
3272 {
3273 DDR_AFFINE_P (ddr) = false;
b44d1046 3274 return;
3275 }
1532ec98 3276
b44d1046 3277 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
3278 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
1532ec98 3279
b44d1046 3280 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3281 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
58f77a87 3282 v1 = int_cst_value (CHREC_RIGHT (c_1));
3283 v2 = int_cst_value (CHREC_RIGHT (c_2));
8fb896c4 3284 cd = gcd (v1, v2);
3285 v1 /= cd;
3286 v2 /= cd;
3287
3288 if (v2 < 0)
3289 {
3290 v2 = -v2;
3291 v1 = -v1;
3292 }
3293
3294 dist_v[x_1] = v2;
3295 dist_v[x_2] = -v1;
b44d1046 3296 save_dist_v (ddr, dist_v);
1532ec98 3297
b44d1046 3298 add_outer_distances (ddr, dist_v, x_1);
3299}
1532ec98 3300
b44d1046 3301/* Helper function for the case where DDR_A and DDR_B are the same
3302 access functions. */
ffe944fa 3303
b44d1046 3304static void
3305add_other_self_distances (struct data_dependence_relation *ddr)
3306{
3307 lambda_vector dist_v;
3308 unsigned i;
3309 int index_carry = DDR_NB_LOOPS (ddr);
1532ec98 3310
b44d1046 3311 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
ffe944fa 3312 {
b44d1046 3313 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
1532ec98 3314
b44d1046 3315 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
1532ec98 3316 {
b44d1046 3317 if (!evolution_function_is_univariate_p (access_fun))
3318 {
3319 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
3320 {
3321 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3322 return;
3323 }
3324
bfe20447 3325 access_fun = DR_ACCESS_FN (DDR_A (ddr), 0);
3326
3327 if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
3328 add_multivariate_self_dist (ddr, access_fun);
3329 else
3330 /* The evolution step is not constant: it varies in
3331 the outer loop, so this cannot be represented by a
3332 distance vector. For example in pr34635.c the
3333 evolution is {0, +, {0, +, 4}_1}_2. */
3334 DDR_AFFINE_P (ddr) = false;
3335
b44d1046 3336 return;
3337 }
3338
3339 index_carry = MIN (index_carry,
3340 index_in_loop_nest (CHREC_VARIABLE (access_fun),
3341 DDR_LOOP_NEST (ddr)));
1532ec98 3342 }
ffe944fa 3343 }
3344
b44d1046 3345 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3346 add_outer_distances (ddr, dist_v, index_carry);
2146e26d 3347}
3348
2a21a5df 3349static void
3350insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
3351{
3352 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3353
3354 dist_v[DDR_INNER_LOOP (ddr)] = 1;
3355 save_dist_v (ddr, dist_v);
3356}
3357
3358/* Adds a unit distance vector to DDR when there is a 0 overlap. This
3359 is the case for example when access functions are the same and
3360 equal to a constant, as in:
3361
3362 | loop_1
3363 | A[3] = ...
3364 | ... = A[3]
3365 | endloop_1
3366
3367 in which case the distance vectors are (0) and (1). */
3368
3369static void
3370add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
3371{
3372 unsigned i, j;
3373
3374 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3375 {
3376 subscript_p sub = DDR_SUBSCRIPT (ddr, i);
3377 conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
3378 conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
3379
3380 for (j = 0; j < ca->n; j++)
3381 if (affine_function_zero_p (ca->fns[j]))
3382 {
3383 insert_innermost_unit_dist_vector (ddr);
3384 return;
3385 }
3386
3387 for (j = 0; j < cb->n; j++)
3388 if (affine_function_zero_p (cb->fns[j]))
3389 {
3390 insert_innermost_unit_dist_vector (ddr);
3391 return;
3392 }
3393 }
3394}
3395
b44d1046 3396/* Compute the classic per loop distance vector. DDR is the data
3397 dependence relation to build a vector from. Return false when fail
3398 to represent the data dependence as a distance vector. */
2146e26d 3399
557ef5d8 3400static bool
8c7b1d8e 3401build_classic_dist_vector (struct data_dependence_relation *ddr,
3402 struct loop *loop_nest)
2146e26d 3403{
1532ec98 3404 bool init_b = false;
b44d1046 3405 int index_carry = DDR_NB_LOOPS (ddr);
3406 lambda_vector dist_v;
1532ec98 3407
6b6f234c 3408 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
d534eb36 3409 return false;
b44d1046 3410
3411 if (same_access_functions (ddr))
2146e26d 3412 {
b44d1046 3413 /* Save the 0 vector. */
3414 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3415 save_dist_v (ddr, dist_v);
2146e26d 3416
2a21a5df 3417 if (constant_access_functions (ddr))
3418 add_distance_for_zero_overlaps (ddr);
3419
b44d1046 3420 if (DDR_NB_LOOPS (ddr) > 1)
3421 add_other_self_distances (ddr);
bc3c8ad4 3422
b44d1046 3423 return true;
3424 }
bc3c8ad4 3425
b44d1046 3426 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3427 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3428 dist_v, &init_b, &index_carry))
3429 return false;
bc3c8ad4 3430
b44d1046 3431 /* Save the distance vector if we initialized one. */
3432 if (init_b)
3433 {
3434 /* Verify a basic constraint: classic distance vectors should
3435 always be lexicographically positive.
3436
3437 Data references are collected in the order of execution of
3438 the program, thus for the following loop
3439
3440 | for (i = 1; i < 100; i++)
3441 | for (j = 1; j < 100; j++)
3442 | {
3443 | t = T[j+1][i-1]; // A
3444 | T[j][i] = t + 2; // B
3445 | }
3446
3447 references are collected following the direction of the wind:
3448 A then B. The data dependence tests are performed also
3449 following this order, such that we're looking at the distance
3450 separating the elements accessed by A from the elements later
3451 accessed by B. But in this example, the distance returned by
3452 test_dep (A, B) is lexicographically negative (-1, 1), that
3453 means that the access A occurs later than B with respect to
3454 the outer loop, ie. we're actually looking upwind. In this
3455 case we solve test_dep (B, A) looking downwind to the
3456 lexicographically positive solution, that returns the
3457 distance vector (1, -1). */
3458 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3459 {
3460 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
d534eb36 3461 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3462 loop_nest))
3463 return false;
b44d1046 3464 compute_subscript_distance (ddr);
d534eb36 3465 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3466 save_v, &init_b, &index_carry))
3467 return false;
b44d1046 3468 save_dist_v (ddr, save_v);
0ecb94cf 3469 DDR_REVERSED_P (ddr) = true;
b44d1046 3470
3471 /* In this case there is a dependence forward for all the
3472 outer loops:
3473
3474 | for (k = 1; k < 100; k++)
3475 | for (i = 1; i < 100; i++)
3476 | for (j = 1; j < 100; j++)
3477 | {
3478 | t = T[j+1][i-1]; // A
3479 | T[j][i] = t + 2; // B
3480 | }
3481
48e1416a 3482 the vectors are:
b44d1046 3483 (0, 1, -1)
3484 (1, 1, -1)
3485 (1, -1, 1)
3486 */
3487 if (DDR_NB_LOOPS (ddr) > 1)
3488 {
3489 add_outer_distances (ddr, save_v, index_carry);
3490 add_outer_distances (ddr, dist_v, index_carry);
bc3c8ad4 3491 }
b44d1046 3492 }
3493 else
3494 {
3495 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3496 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
bc3c8ad4 3497
b44d1046 3498 if (DDR_NB_LOOPS (ddr) > 1)
2146e26d 3499 {
b44d1046 3500 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
bc3c8ad4 3501
d534eb36 3502 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr),
3503 DDR_A (ddr), loop_nest))
3504 return false;
b44d1046 3505 compute_subscript_distance (ddr);
d534eb36 3506 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3507 opposite_v, &init_b,
3508 &index_carry))
3509 return false;
bc3c8ad4 3510
d534eb36 3511 save_dist_v (ddr, save_v);
b44d1046 3512 add_outer_distances (ddr, dist_v, index_carry);
3513 add_outer_distances (ddr, opposite_v, index_carry);
2146e26d 3514 }
d534eb36 3515 else
3516 save_dist_v (ddr, save_v);
2146e26d 3517 }
3518 }
b44d1046 3519 else
3520 {
3521 /* There is a distance of 1 on all the outer loops: Example:
3522 there is a dependence of distance 1 on loop_1 for the array A.
1532ec98 3523
b44d1046 3524 | loop_1
3525 | A[5] = ...
3526 | endloop
3527 */
3528 add_outer_distances (ddr, dist_v,
3529 lambda_vector_first_nz (dist_v,
3530 DDR_NB_LOOPS (ddr), 0));
3531 }
3532
3533 if (dump_file && (dump_flags & TDF_DETAILS))
1532ec98 3534 {
b44d1046 3535 unsigned i;
1532ec98 3536
b44d1046 3537 fprintf (dump_file, "(build_classic_dist_vector\n");
3538 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3539 {
3540 fprintf (dump_file, " dist_vector = (");
3541 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3542 DDR_NB_LOOPS (ddr));
3543 fprintf (dump_file, " )\n");
3544 }
3545 fprintf (dump_file, ")\n");
1532ec98 3546 }
3547
b44d1046 3548 return true;
3549}
2146e26d 3550
b44d1046 3551/* Return the direction for a given distance.
3552 FIXME: Computing dir this way is suboptimal, since dir can catch
3553 cases that dist is unable to represent. */
bc3c8ad4 3554
b44d1046 3555static inline enum data_dependence_direction
3556dir_from_dist (int dist)
3557{
3558 if (dist > 0)
3559 return dir_positive;
3560 else if (dist < 0)
3561 return dir_negative;
3562 else
3563 return dir_equal;
3564}
1532ec98 3565
b44d1046 3566/* Compute the classic per loop direction vector. DDR is the data
3567 dependence relation to build a vector from. */
1532ec98 3568
b44d1046 3569static void
3570build_classic_dir_vector (struct data_dependence_relation *ddr)
3571{
3572 unsigned i, j;
3573 lambda_vector dist_v;
bc3c8ad4 3574
f1f41a6c 3575 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
b44d1046 3576 {
3577 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
bc3c8ad4 3578
b44d1046 3579 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3580 dir_v[j] = dir_from_dist (dist_v[j]);
1532ec98 3581
b44d1046 3582 save_dir_v (ddr, dir_v);
3583 }
2146e26d 3584}
3585
b44d1046 3586/* Helper function. Returns true when there is a dependence between
3587 data references DRA and DRB. */
6b421feb 3588
b44d1046 3589static bool
3590subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
3591 struct data_reference *dra,
8c7b1d8e 3592 struct data_reference *drb,
3593 struct loop *loop_nest)
6b421feb 3594{
3595 unsigned int i;
6b421feb 3596 tree last_conflicts;
41c7a324 3597 struct subscript *subscript;
2ecb06bc 3598 tree res = NULL_TREE;
b44d1046 3599
f1f41a6c 3600 for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
6b421feb 3601 {
87da4f2e 3602 conflict_function *overlaps_a, *overlaps_b;
41c7a324 3603
48e1416a 3604 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
6b421feb 3605 DR_ACCESS_FN (drb, i),
48e1416a 3606 &overlaps_a, &overlaps_b,
8c7b1d8e 3607 &last_conflicts, loop_nest);
41c7a324 3608
2ecb06bc 3609 if (SUB_CONFLICTS_IN_A (subscript))
3610 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
3611 if (SUB_CONFLICTS_IN_B (subscript))
3612 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
3613
3614 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3615 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3616 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3617
3618 /* If there is any undetermined conflict function we have to
3619 give a conservative answer in case we cannot prove that
3620 no dependence exists when analyzing another subscript. */
87da4f2e 3621 if (CF_NOT_KNOWN_P (overlaps_a)
3622 || CF_NOT_KNOWN_P (overlaps_b))
6b421feb 3623 {
2ecb06bc 3624 res = chrec_dont_know;
3625 continue;
6b421feb 3626 }
41c7a324 3627
2ecb06bc 3628 /* When there is a subscript with no dependence we can stop. */
87da4f2e 3629 else if (CF_NO_DEPENDENCE_P (overlaps_a)
3630 || CF_NO_DEPENDENCE_P (overlaps_b))
6b421feb 3631 {
2ecb06bc 3632 res = chrec_known;
3633 break;
6b421feb 3634 }
3635 }
3636
2ecb06bc 3637 if (res == NULL_TREE)
3638 return true;
3639
3640 if (res == chrec_known)
3641 dependence_stats.num_dependence_independent++;
3642 else
3643 dependence_stats.num_dependence_undetermined++;
3644 finalize_ddr_dependent (ddr, res);
3645 return false;
b44d1046 3646}
3647
8c7b1d8e 3648/* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
b44d1046 3649
3650static void
8c7b1d8e 3651subscript_dependence_tester (struct data_dependence_relation *ddr,
3652 struct loop *loop_nest)
b44d1046 3653{
8c7b1d8e 3654 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr), loop_nest))
b44d1046 3655 dependence_stats.num_dependence_dependent++;
6b421feb 3656
6b421feb 3657 compute_subscript_distance (ddr);
8c7b1d8e 3658 if (build_classic_dist_vector (ddr, loop_nest))
b44d1046 3659 build_classic_dir_vector (ddr);
6b421feb 3660}
3661
2146e26d 3662/* Returns true when all the access functions of A are affine or
8c7b1d8e 3663 constant with respect to LOOP_NEST. */
2146e26d 3664
48e1416a 3665static bool
7ecb5bb2 3666access_functions_are_affine_or_constant_p (const struct data_reference *a,
3667 const struct loop *loop_nest)
2146e26d 3668{
3669 unsigned int i;
f1f41a6c 3670 vec<tree> fns = DR_ACCESS_FNS (a);
e38f1ca4 3671 tree t;
355572cc 3672
f1f41a6c 3673 FOR_EACH_VEC_ELT (fns, i, t)
8c7b1d8e 3674 if (!evolution_function_is_invariant_p (t, loop_nest->num)
3675 && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
2146e26d 3676 return false;
48e1416a 3677
2146e26d 3678 return true;
3679}
3680
8c7b1d8e 3681/* This computes the affine dependence relation between A and B with
3682 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
3683 independence between two accesses, while CHREC_DONT_KNOW is used
3684 for representing the unknown relation.
48e1416a 3685
2146e26d 3686 Note that it is possible to stop the computation of the dependence
3687 relation the first time we detect a CHREC_KNOWN element for a given
3688 subscript. */
3689
7b6f8db4 3690void
8c7b1d8e 3691compute_affine_dependence (struct data_dependence_relation *ddr,
3692 struct loop *loop_nest)
2146e26d 3693{
3694 struct data_reference *dra = DDR_A (ddr);
3695 struct data_reference *drb = DDR_B (ddr);
48e1416a 3696
2146e26d 3697 if (dump_file && (dump_flags & TDF_DETAILS))
3698 {
6b6f234c 3699 fprintf (dump_file, "(compute_affine_dependence\n");
95539e1d 3700 fprintf (dump_file, " stmt_a: ");
3701 print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
3702 fprintf (dump_file, " stmt_b: ");
3703 print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
2146e26d 3704 }
6b421feb 3705
2146e26d 3706 /* Analyze only when the dependence relation is not yet known. */
c76b51be 3707 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
2146e26d 3708 {
6b421feb 3709 dependence_stats.num_dependence_tests++;
3710
8c7b1d8e 3711 if (access_functions_are_affine_or_constant_p (dra, loop_nest)
3712 && access_functions_are_affine_or_constant_p (drb, loop_nest))
0d8001a7 3713 subscript_dependence_tester (ddr, loop_nest);
48e1416a 3714
2146e26d 3715 /* As a last case, if the dependence cannot be determined, or if
3716 the dependence is considered too difficult to determine, answer
3717 "don't know". */
3718 else
6b421feb 3719 {
3720 dependence_stats.num_dependence_undetermined++;
3721
3722 if (dump_file && (dump_flags & TDF_DETAILS))
3723 {
3724 fprintf (dump_file, "Data ref a:\n");
3725 dump_data_reference (dump_file, dra);
3726 fprintf (dump_file, "Data ref b:\n");
3727 dump_data_reference (dump_file, drb);
3728 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
3729 }
3730 finalize_ddr_dependent (ddr, chrec_dont_know);
3731 }
2146e26d 3732 }
48e1416a 3733
2146e26d 3734 if (dump_file && (dump_flags & TDF_DETAILS))
95539e1d 3735 {
3736 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
3737 fprintf (dump_file, ") -> no dependence\n");
3738 else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
3739 fprintf (dump_file, ") -> dependence analysis failed\n");
3740 else
3741 fprintf (dump_file, ")\n");
3742 }
2146e26d 3743}
3744
b44d1046 3745/* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
3746 the data references in DATAREFS, in the LOOP_NEST. When
41c7a324 3747 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
c7af8ae7 3748 relations. Return true when successful, i.e. data references number
3749 is small enough to be handled. */
2146e26d 3750
c7af8ae7 3751bool
f1f41a6c 3752compute_all_dependences (vec<data_reference_p> datarefs,
3753 vec<ddr_p> *dependence_relations,
3754 vec<loop_p> loop_nest,
41c7a324 3755 bool compute_self_and_rr)
2146e26d 3756{
41c7a324 3757 struct data_dependence_relation *ddr;
3758 struct data_reference *a, *b;
3759 unsigned int i, j;
2146e26d 3760
f1f41a6c 3761 if ((int) datarefs.length ()
c7af8ae7 3762 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
3763 {
3764 struct data_dependence_relation *ddr;
3765
3766 /* Insert a single relation into dependence_relations:
3767 chrec_dont_know. */
3768 ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
f1f41a6c 3769 dependence_relations->safe_push (ddr);
c7af8ae7 3770 return false;
3771 }
3772
f1f41a6c 3773 FOR_EACH_VEC_ELT (datarefs, i, a)
3774 for (j = i + 1; datarefs.iterate (j, &b); j++)
9ff25603 3775 if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
41c7a324 3776 {
3777 ddr = initialize_data_dependence_relation (a, b, loop_nest);
f1f41a6c 3778 dependence_relations->safe_push (ddr);
3779 if (loop_nest.exists ())
3780 compute_affine_dependence (ddr, loop_nest[0]);
41c7a324 3781 }
02d46b9c 3782
41c7a324 3783 if (compute_self_and_rr)
f1f41a6c 3784 FOR_EACH_VEC_ELT (datarefs, i, a)
2146e26d 3785 {
41c7a324 3786 ddr = initialize_data_dependence_relation (a, a, loop_nest);
f1f41a6c 3787 dependence_relations->safe_push (ddr);
3788 if (loop_nest.exists ())
3789 compute_affine_dependence (ddr, loop_nest[0]);
2146e26d 3790 }
c7af8ae7 3791
3792 return true;
2146e26d 3793}
3794
d129ada0 3795/* Describes a location of a memory reference. */
3796
df8eb490 3797struct data_ref_loc
d129ada0 3798{
239dd34a 3799 /* The memory reference. */
3800 tree ref;
d129ada0 3801
07e3bcbf 3802 /* True if the memory reference is read. */
3803 bool is_read;
df8eb490 3804};
d129ada0 3805
d129ada0 3806
faa56cf9 3807/* Stores the locations of memory references in STMT to REFERENCES. Returns
3808 true if STMT clobbers memory, false otherwise. */
3809
d129ada0 3810static bool
42acab1c 3811get_references_in_stmt (gimple *stmt, vec<data_ref_loc, va_heap> *references)
faa56cf9 3812{
3813 bool clobbers_memory = false;
e82e4eb5 3814 data_ref_loc ref;
239dd34a 3815 tree op0, op1;
75a70cf9 3816 enum gimple_code stmt_code = gimple_code (stmt);
faa56cf9 3817
faa56cf9 3818 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
0ef2a6d1 3819 As we cannot model data-references to not spelled out
3820 accesses give up if they may occur. */
3d483a94 3821 if (stmt_code == GIMPLE_CALL
3822 && !(gimple_call_flags (stmt) & ECF_CONST))
3823 {
3824 /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
c71d3c24 3825 if (gimple_call_internal_p (stmt))
3826 switch (gimple_call_internal_fn (stmt))
3827 {
3828 case IFN_GOMP_SIMD_LANE:
3829 {
3830 struct loop *loop = gimple_bb (stmt)->loop_father;
3831 tree uid = gimple_call_arg (stmt, 0);
3832 gcc_assert (TREE_CODE (uid) == SSA_NAME);
3833 if (loop == NULL
3834 || loop->simduid != SSA_NAME_VAR (uid))
3835 clobbers_memory = true;
3836 break;
3837 }
3838 case IFN_MASK_LOAD:
3839 case IFN_MASK_STORE:
3840 break;
3841 default:
3d483a94 3842 clobbers_memory = true;
c71d3c24 3843 break;
3844 }
3d483a94 3845 else
3846 clobbers_memory = true;
3847 }
3848 else if (stmt_code == GIMPLE_ASM
1a91d914 3849 && (gimple_asm_volatile_p (as_a <gasm *> (stmt))
3850 || gimple_vuse (stmt)))
faa56cf9 3851 clobbers_memory = true;
3852
dd277d48 3853 if (!gimple_vuse (stmt))
faa56cf9 3854 return clobbers_memory;
3855
75a70cf9 3856 if (stmt_code == GIMPLE_ASSIGN)
faa56cf9 3857 {
ffb04365 3858 tree base;
239dd34a 3859 op0 = gimple_assign_lhs (stmt);
3860 op1 = gimple_assign_rhs1 (stmt);
48e1416a 3861
239dd34a 3862 if (DECL_P (op1)
3863 || (REFERENCE_CLASS_P (op1)
3864 && (base = get_base_address (op1))
ffb04365 3865 && TREE_CODE (base) != SSA_NAME))
faa56cf9 3866 {
239dd34a 3867 ref.ref = op1;
e82e4eb5 3868 ref.is_read = true;
f1f41a6c 3869 references->safe_push (ref);
faa56cf9 3870 }
faa56cf9 3871 }
75a70cf9 3872 else if (stmt_code == GIMPLE_CALL)
faa56cf9 3873 {
9f2e27ae 3874 unsigned i, n;
d73c1238 3875 tree ptr, type;
3876 unsigned int align;
d97e22fb 3877
c71d3c24 3878 ref.is_read = false;
3879 if (gimple_call_internal_p (stmt))
3880 switch (gimple_call_internal_fn (stmt))
3881 {
3882 case IFN_MASK_LOAD:
4e459157 3883 if (gimple_call_lhs (stmt) == NULL_TREE)
3884 break;
c71d3c24 3885 ref.is_read = true;
3886 case IFN_MASK_STORE:
d73c1238 3887 ptr = build_int_cst (TREE_TYPE (gimple_call_arg (stmt, 1)), 0);
3888 align = tree_to_shwi (gimple_call_arg (stmt, 1));
3889 if (ref.is_read)
3890 type = TREE_TYPE (gimple_call_lhs (stmt));
3891 else
3892 type = TREE_TYPE (gimple_call_arg (stmt, 3));
3893 if (TYPE_ALIGN (type) != align)
3894 type = build_aligned_type (type, align);
3895 ref.ref = fold_build2 (MEM_REF, type, gimple_call_arg (stmt, 0),
3896 ptr);
c71d3c24 3897 references->safe_push (ref);
3898 return false;
3899 default:
3900 break;
3901 }
3902
239dd34a 3903 op0 = gimple_call_lhs (stmt);
9f2e27ae 3904 n = gimple_call_num_args (stmt);
d97e22fb 3905 for (i = 0; i < n; i++)
faa56cf9 3906 {
239dd34a 3907 op1 = gimple_call_arg (stmt, i);
d97e22fb 3908
239dd34a 3909 if (DECL_P (op1)
3910 || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
faa56cf9 3911 {
239dd34a 3912 ref.ref = op1;
e82e4eb5 3913 ref.is_read = true;
f1f41a6c 3914 references->safe_push (ref);
faa56cf9 3915 }
3916 }
3917 }
9f2e27ae 3918 else
3919 return clobbers_memory;
faa56cf9 3920
239dd34a 3921 if (op0
3922 && (DECL_P (op0)
3923 || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
9f2e27ae 3924 {
239dd34a 3925 ref.ref = op0;
e82e4eb5 3926 ref.is_read = false;
f1f41a6c 3927 references->safe_push (ref);
9f2e27ae 3928 }
faa56cf9 3929 return clobbers_memory;
3930}
3931
fa4dba85 3932
3933/* Returns true if the loop-nest has any data reference. */
3934
3935bool
3936loop_nest_has_data_refs (loop_p loop)
3937{
3938 basic_block *bbs = get_loop_body (loop);
3939 vec<data_ref_loc> references;
3940 references.create (3);
3941
3942 for (unsigned i = 0; i < loop->num_nodes; i++)
3943 {
3944 basic_block bb = bbs[i];
3945 gimple_stmt_iterator bsi;
3946
3947 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
3948 {
42acab1c 3949 gimple *stmt = gsi_stmt (bsi);
fa4dba85 3950 get_references_in_stmt (stmt, &references);
3951 if (references.length ())
3952 {
3953 free (bbs);
3954 references.release ();
3955 return true;
3956 }
3957 }
3958 }
3959 free (bbs);
3960 references.release ();
3961
3962 if (loop->inner)
3963 {
3964 loop = loop->inner;
3965 while (loop)
3966 {
3967 if (loop_nest_has_data_refs (loop))
3968 return true;
3969 loop = loop->next;
3970 }
3971 }
3972 return false;
3973}
3974
faa56cf9 3975/* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
80bb306a 3976 reference, returns false, otherwise returns true. NEST is the outermost
255b6be7 3977 loop of the loop nest in which the references should be analyzed. */
faa56cf9 3978
255b6be7 3979bool
42acab1c 3980find_data_references_in_stmt (struct loop *nest, gimple *stmt,
f1f41a6c 3981 vec<data_reference_p> *datarefs)
faa56cf9 3982{
3983 unsigned i;
4997014d 3984 auto_vec<data_ref_loc, 2> references;
faa56cf9 3985 data_ref_loc *ref;
3986 bool ret = true;
3987 data_reference_p dr;
3988
3989 if (get_references_in_stmt (stmt, &references))
d70aebca 3990 return false;
faa56cf9 3991
f1f41a6c 3992 FOR_EACH_VEC_ELT (references, i, ref)
faa56cf9 3993 {
221a697e 3994 dr = create_data_ref (nest, loop_containing_stmt (stmt),
239dd34a 3995 ref->ref, stmt, ref->is_read);
ad4a85ad 3996 gcc_assert (dr != NULL);
f1f41a6c 3997 datarefs->safe_push (dr);
faa56cf9 3998 }
f1f41a6c 3999 references.release ();
faa56cf9 4000 return ret;
4001}
4002
221a697e 4003/* Stores the data references in STMT to DATAREFS. If there is an
4004 unanalyzable reference, returns false, otherwise returns true.
4005 NEST is the outermost loop of the loop nest in which the references
4006 should be instantiated, LOOP is the loop in which the references
4007 should be analyzed. */
c794d738 4008
4009bool
42acab1c 4010graphite_find_data_references_in_stmt (loop_p nest, loop_p loop, gimple *stmt,
f1f41a6c 4011 vec<data_reference_p> *datarefs)
c794d738 4012{
4013 unsigned i;
4997014d 4014 auto_vec<data_ref_loc, 2> references;
c794d738 4015 data_ref_loc *ref;
4016 bool ret = true;
4017 data_reference_p dr;
4018
4019 if (get_references_in_stmt (stmt, &references))
d70aebca 4020 return false;
c794d738 4021
f1f41a6c 4022 FOR_EACH_VEC_ELT (references, i, ref)
c794d738 4023 {
239dd34a 4024 dr = create_data_ref (nest, loop, ref->ref, stmt, ref->is_read);
c794d738 4025 gcc_assert (dr != NULL);
f1f41a6c 4026 datarefs->safe_push (dr);
c794d738 4027 }
4028
f1f41a6c 4029 references.release ();
c794d738 4030 return ret;
4031}
4032
37545e54 4033/* Search the data references in LOOP, and record the information into
4034 DATAREFS. Returns chrec_dont_know when failing to analyze a
4035 difficult case, returns NULL_TREE otherwise. */
4036
ec611e12 4037tree
37545e54 4038find_data_references_in_bb (struct loop *loop, basic_block bb,
f1f41a6c 4039 vec<data_reference_p> *datarefs)
37545e54 4040{
4041 gimple_stmt_iterator bsi;
4042
4043 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4044 {
42acab1c 4045 gimple *stmt = gsi_stmt (bsi);
37545e54 4046
4047 if (!find_data_references_in_stmt (loop, stmt, datarefs))
4048 {
4049 struct data_reference *res;
4050 res = XCNEW (struct data_reference);
f1f41a6c 4051 datarefs->safe_push (res);
37545e54 4052
4053 return chrec_dont_know;
4054 }
4055 }
4056
4057 return NULL_TREE;
4058}
4059
2146e26d 4060/* Search the data references in LOOP, and record the information into
4061 DATAREFS. Returns chrec_dont_know when failing to analyze a
4062 difficult case, returns NULL_TREE otherwise.
80bb306a 4063
557ef5d8 4064 TODO: This function should be made smarter so that it can handle address
4065 arithmetic as if they were array accesses, etc. */
2146e26d 4066
07e3bcbf 4067tree
41c7a324 4068find_data_references_in_loop (struct loop *loop,
f1f41a6c 4069 vec<data_reference_p> *datarefs)
2146e26d 4070{
0332ea54 4071 basic_block bb, *bbs;
4072 unsigned int i;
bc3c8ad4 4073
ad4a85ad 4074 bbs = get_loop_body_in_dom_order (loop);
0332ea54 4075
4076 for (i = 0; i < loop->num_nodes; i++)
2146e26d 4077 {
0332ea54 4078 bb = bbs[i];
4079
37545e54 4080 if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
4081 {
4082 free (bbs);
4083 return chrec_dont_know;
4084 }
2146e26d 4085 }
0332ea54 4086 free (bbs);
4087
4a012ac6 4088 return NULL_TREE;
2146e26d 4089}
4090
b44d1046 4091/* Recursive helper function. */
4092
4093static bool
f1f41a6c 4094find_loop_nest_1 (struct loop *loop, vec<loop_p> *loop_nest)
b44d1046 4095{
4096 /* Inner loops of the nest should not contain siblings. Example:
4097 when there are two consecutive loops,
4098
4099 | loop_0
4100 | loop_1
4101 | A[{0, +, 1}_1]
4102 | endloop_1
4103 | loop_2
4104 | A[{0, +, 1}_2]
4105 | endloop_2
4106 | endloop_0
4107
4108 the dependence relation cannot be captured by the distance
4109 abstraction. */
4110 if (loop->next)
4111 return false;
2146e26d 4112
f1f41a6c 4113 loop_nest->safe_push (loop);
b44d1046 4114 if (loop->inner)
4115 return find_loop_nest_1 (loop->inner, loop_nest);
4116 return true;
4117}
4118
4119/* Return false when the LOOP is not well nested. Otherwise return
4120 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4121 contain the loops from the outermost to the innermost, as they will
4122 appear in the classic distance vector. */
4123
5c205353 4124bool
f1f41a6c 4125find_loop_nest (struct loop *loop, vec<loop_p> *loop_nest)
b44d1046 4126{
f1f41a6c 4127 loop_nest->safe_push (loop);
b44d1046 4128 if (loop->inner)
4129 return find_loop_nest_1 (loop->inner, loop_nest);
4130 return true;
4131}
2146e26d 4132
b79b3386 4133/* Returns true when the data dependences have been computed, false otherwise.
4134 Given a loop nest LOOP, the following vectors are returned:
48e1416a 4135 DATAREFS is initialized to all the array elements contained in this loop,
4136 DEPENDENCE_RELATIONS contains the relations between the data references.
4137 Compute read-read and self relations if
516849c7 4138 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
2146e26d 4139
b79b3386 4140bool
48e1416a 4141compute_data_dependences_for_loop (struct loop *loop,
516849c7 4142 bool compute_self_and_read_read_dependences,
f1f41a6c 4143 vec<loop_p> *loop_nest,
4144 vec<data_reference_p> *datarefs,
4145 vec<ddr_p> *dependence_relations)
2146e26d 4146{
b79b3386 4147 bool res = true;
516849c7 4148
6b421feb 4149 memset (&dependence_stats, 0, sizeof (dependence_stats));
2146e26d 4150
48e1416a 4151 /* If the loop nest is not well formed, or one of the data references
b44d1046 4152 is not computable, give up without spending time to compute other
4153 dependences. */
80bb306a 4154 if (!loop
a8af2e86 4155 || !find_loop_nest (loop, loop_nest)
c7af8ae7 4156 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
4157 || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
4158 compute_self_and_read_read_dependences))
4159 res = false;
6b421feb 4160
4161 if (dump_file && (dump_flags & TDF_STATS))
2146e26d 4162 {
6b421feb 4163 fprintf (dump_file, "Dependence tester statistics:\n");
4164
48e1416a 4165 fprintf (dump_file, "Number of dependence tests: %d\n",
6b421feb 4166 dependence_stats.num_dependence_tests);
48e1416a 4167 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
6b421feb 4168 dependence_stats.num_dependence_dependent);
48e1416a 4169 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
6b421feb 4170 dependence_stats.num_dependence_independent);
48e1416a 4171 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
6b421feb 4172 dependence_stats.num_dependence_undetermined);
4173
48e1416a 4174 fprintf (dump_file, "Number of subscript tests: %d\n",
6b421feb 4175 dependence_stats.num_subscript_tests);
48e1416a 4176 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
6b421feb 4177 dependence_stats.num_subscript_undetermined);
48e1416a 4178 fprintf (dump_file, "Number of same subscript function: %d\n",
6b421feb 4179 dependence_stats.num_same_subscript_function);
4180
4181 fprintf (dump_file, "Number of ziv tests: %d\n",
4182 dependence_stats.num_ziv);
4183 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4184 dependence_stats.num_ziv_dependent);
4185 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4186 dependence_stats.num_ziv_independent);
4187 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
48e1416a 4188 dependence_stats.num_ziv_unimplemented);
6b421feb 4189
48e1416a 4190 fprintf (dump_file, "Number of siv tests: %d\n",
6b421feb 4191 dependence_stats.num_siv);
4192 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4193 dependence_stats.num_siv_dependent);
4194 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4195 dependence_stats.num_siv_independent);
4196 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4197 dependence_stats.num_siv_unimplemented);
4198
48e1416a 4199 fprintf (dump_file, "Number of miv tests: %d\n",
6b421feb 4200 dependence_stats.num_miv);
4201 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4202 dependence_stats.num_miv_dependent);
4203 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4204 dependence_stats.num_miv_independent);
4205 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4206 dependence_stats.num_miv_unimplemented);
b79b3386 4207 }
4208
4209 return res;
2146e26d 4210}
4211
6b6f234c 4212/* Free the memory used by a data dependence relation DDR. */
4213
4214void
4215free_dependence_relation (struct data_dependence_relation *ddr)
4216{
4217 if (ddr == NULL)
4218 return;
4219
f1f41a6c 4220 if (DDR_SUBSCRIPTS (ddr).exists ())
87da4f2e 4221 free_subscripts (DDR_SUBSCRIPTS (ddr));
f1f41a6c 4222 DDR_DIST_VECTS (ddr).release ();
4223 DDR_DIR_VECTS (ddr).release ();
41c7a324 4224
6b6f234c 4225 free (ddr);
4226}
4227
4228/* Free the memory used by the data dependence relations from
4229 DEPENDENCE_RELATIONS. */
4230
48e1416a 4231void
f1f41a6c 4232free_dependence_relations (vec<ddr_p> dependence_relations)
6b6f234c 4233{
4234 unsigned int i;
41c7a324 4235 struct data_dependence_relation *ddr;
6b6f234c 4236
f1f41a6c 4237 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
a8af2e86 4238 if (ddr)
62761183 4239 free_dependence_relation (ddr);
41c7a324 4240
f1f41a6c 4241 dependence_relations.release ();
2146e26d 4242}
4243
6b6f234c 4244/* Free the memory used by the data references from DATAREFS. */
4245
4246void
f1f41a6c 4247free_data_refs (vec<data_reference_p> datarefs)
6b6f234c 4248{
4249 unsigned int i;
41c7a324 4250 struct data_reference *dr;
2146e26d 4251
f1f41a6c 4252 FOR_EACH_VEC_ELT (datarefs, i, dr)
31e389a2 4253 free_data_ref (dr);
f1f41a6c 4254 datarefs.release ();
6b6f234c 4255}