]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-data-ref.c
* Makefile.in (omp-low.o): Depend on $(TARGET_H).
[thirdparty/gcc.git] / gcc / tree-data-ref.c
CommitLineData
2146e26d 1/* Data references and dependences detectors.
711789cc 2 Copyright (C) 2003-2013 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"
ce084dfc 79#include "gimple-pretty-print.h"
2146e26d 80#include "tree-flow.h"
2146e26d 81#include "cfgloop.h"
2146e26d 82#include "tree-data-ref.h"
83#include "tree-scalar-evolution.h"
b9ed1410 84#include "dumpfile.h"
faa56cf9 85#include "langhooks.h"
5fc88ffd 86#include "tree-affine.h"
c7af8ae7 87#include "params.h"
2146e26d 88
6b421feb 89static struct datadep_stats
90{
91 int num_dependence_tests;
92 int num_dependence_dependent;
93 int num_dependence_independent;
94 int num_dependence_undetermined;
95
96 int num_subscript_tests;
97 int num_subscript_undetermined;
98 int num_same_subscript_function;
99
100 int num_ziv;
101 int num_ziv_independent;
102 int num_ziv_dependent;
103 int num_ziv_unimplemented;
104
105 int num_siv;
106 int num_siv_independent;
107 int num_siv_dependent;
108 int num_siv_unimplemented;
109
110 int num_miv;
111 int num_miv_independent;
112 int num_miv_dependent;
113 int num_miv_unimplemented;
114} dependence_stats;
115
b44d1046 116static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
117 struct data_reference *,
8c7b1d8e 118 struct data_reference *,
119 struct loop *);
2146e26d 120/* Returns true iff A divides B. */
121
48e1416a 122static inline bool
7ecb5bb2 123tree_fold_divides_p (const_tree a, const_tree b)
2146e26d 124{
426a138f 125 gcc_assert (TREE_CODE (a) == INTEGER_CST);
126 gcc_assert (TREE_CODE (b) == INTEGER_CST);
317e2a67 127 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a));
2146e26d 128}
129
bc3c8ad4 130/* Returns true iff A divides B. */
131
48e1416a 132static inline bool
bc3c8ad4 133int_divides_p (int a, int b)
134{
135 return ((b % a) == 0);
2146e26d 136}
137
138\f
139
48e1416a 140/* Dump into FILE all the data references from DATAREFS. */
2146e26d 141
d129ada0 142static void
f1f41a6c 143dump_data_references (FILE *file, vec<data_reference_p> datarefs)
2146e26d 144{
145 unsigned int i;
41c7a324 146 struct data_reference *dr;
147
f1f41a6c 148 FOR_EACH_VEC_ELT (datarefs, i, dr)
41c7a324 149 dump_data_reference (file, dr);
2146e26d 150}
151
c7d89805 152/* Unified dump into FILE all the data references from DATAREFS. */
153
154DEBUG_FUNCTION void
155debug (vec<data_reference_p> &ref)
156{
157 dump_data_references (stderr, ref);
158}
159
160DEBUG_FUNCTION void
161debug (vec<data_reference_p> *ptr)
162{
163 if (ptr)
164 debug (*ptr);
165 else
166 fprintf (stderr, "<nil>\n");
167}
168
169
48e1416a 170/* Dump into STDERR all the data references from DATAREFS. */
5df4cc8d 171
4b987fac 172DEBUG_FUNCTION void
f1f41a6c 173debug_data_references (vec<data_reference_p> datarefs)
5df4cc8d 174{
175 dump_data_references (stderr, datarefs);
176}
177
5df4cc8d 178/* Print to STDERR the data_reference DR. */
179
4b987fac 180DEBUG_FUNCTION void
5df4cc8d 181debug_data_reference (struct data_reference *dr)
182{
183 dump_data_reference (stderr, dr);
184}
185
2146e26d 186/* Dump function for a DATA_REFERENCE structure. */
187
48e1416a 188void
189dump_data_reference (FILE *outf,
2146e26d 190 struct data_reference *dr)
191{
192 unsigned int i;
48e1416a 193
b40e3432 194 fprintf (outf, "#(Data Ref: \n");
195 fprintf (outf, "# bb: %d \n", gimple_bb (DR_STMT (dr))->index);
196 fprintf (outf, "# stmt: ");
75a70cf9 197 print_gimple_stmt (outf, DR_STMT (dr), 0, 0);
5dc5fe13 198 fprintf (outf, "# ref: ");
2146e26d 199 print_generic_stmt (outf, DR_REF (dr), 0);
5dc5fe13 200 fprintf (outf, "# base_object: ");
516849c7 201 print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
48e1416a 202
2146e26d 203 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
204 {
5dc5fe13 205 fprintf (outf, "# Access function %d: ", i);
2146e26d 206 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
207 }
5dc5fe13 208 fprintf (outf, "#)\n");
2146e26d 209}
210
c7d89805 211/* Unified dump function for a DATA_REFERENCE structure. */
212
213DEBUG_FUNCTION void
214debug (data_reference &ref)
215{
216 dump_data_reference (stderr, &ref);
217}
218
219DEBUG_FUNCTION void
220debug (data_reference *ptr)
221{
222 if (ptr)
223 debug (*ptr);
224 else
225 fprintf (stderr, "<nil>\n");
226}
227
228
87da4f2e 229/* Dumps the affine function described by FN to the file OUTF. */
230
231static void
232dump_affine_function (FILE *outf, affine_fn fn)
233{
234 unsigned i;
235 tree coef;
236
f1f41a6c 237 print_generic_expr (outf, fn[0], TDF_SLIM);
238 for (i = 1; fn.iterate (i, &coef); i++)
87da4f2e 239 {
240 fprintf (outf, " + ");
241 print_generic_expr (outf, coef, TDF_SLIM);
242 fprintf (outf, " * x_%u", i);
243 }
244}
245
246/* Dumps the conflict function CF to the file OUTF. */
247
248static void
249dump_conflict_function (FILE *outf, conflict_function *cf)
250{
251 unsigned i;
252
253 if (cf->n == NO_DEPENDENCE)
984cebc1 254 fprintf (outf, "no dependence");
87da4f2e 255 else if (cf->n == NOT_KNOWN)
984cebc1 256 fprintf (outf, "not known");
87da4f2e 257 else
258 {
259 for (i = 0; i < cf->n; i++)
260 {
984cebc1 261 if (i != 0)
262 fprintf (outf, " ");
87da4f2e 263 fprintf (outf, "[");
264 dump_affine_function (outf, cf->fns[i]);
984cebc1 265 fprintf (outf, "]");
87da4f2e 266 }
267 }
268}
269
bc3c8ad4 270/* Dump function for a SUBSCRIPT structure. */
271
d129ada0 272static void
bc3c8ad4 273dump_subscript (FILE *outf, struct subscript *subscript)
274{
87da4f2e 275 conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
bc3c8ad4 276
277 fprintf (outf, "\n (subscript \n");
278 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
87da4f2e 279 dump_conflict_function (outf, cf);
280 if (CF_NONTRIVIAL_P (cf))
bc3c8ad4 281 {
282 tree last_iteration = SUB_LAST_CONFLICT (subscript);
984cebc1 283 fprintf (outf, "\n last_conflict: ");
284 print_generic_expr (outf, last_iteration, 0);
bc3c8ad4 285 }
48e1416a 286
87da4f2e 287 cf = SUB_CONFLICTS_IN_B (subscript);
984cebc1 288 fprintf (outf, "\n iterations_that_access_an_element_twice_in_B: ");
87da4f2e 289 dump_conflict_function (outf, cf);
290 if (CF_NONTRIVIAL_P (cf))
bc3c8ad4 291 {
292 tree last_iteration = SUB_LAST_CONFLICT (subscript);
984cebc1 293 fprintf (outf, "\n last_conflict: ");
294 print_generic_expr (outf, last_iteration, 0);
bc3c8ad4 295 }
296
984cebc1 297 fprintf (outf, "\n (Subscript distance: ");
298 print_generic_expr (outf, SUB_DISTANCE (subscript), 0);
299 fprintf (outf, " ))\n");
bc3c8ad4 300}
301
6b421feb 302/* Print the classic direction vector DIRV to OUTF. */
303
d129ada0 304static void
6b421feb 305print_direction_vector (FILE *outf,
306 lambda_vector dirv,
307 int length)
308{
309 int eq;
310
311 for (eq = 0; eq < length; eq++)
312 {
bc620c5c 313 enum data_dependence_direction dir = ((enum data_dependence_direction)
314 dirv[eq]);
6b421feb 315
316 switch (dir)
317 {
318 case dir_positive:
319 fprintf (outf, " +");
320 break;
321 case dir_negative:
322 fprintf (outf, " -");
323 break;
324 case dir_equal:
325 fprintf (outf, " =");
326 break;
327 case dir_positive_or_equal:
328 fprintf (outf, " +=");
329 break;
330 case dir_positive_or_negative:
331 fprintf (outf, " +-");
332 break;
333 case dir_negative_or_equal:
334 fprintf (outf, " -=");
335 break;
336 case dir_star:
337 fprintf (outf, " *");
338 break;
339 default:
340 fprintf (outf, "indep");
341 break;
342 }
343 }
344 fprintf (outf, "\n");
345}
346
b44d1046 347/* Print a vector of direction vectors. */
348
d129ada0 349static void
f1f41a6c 350print_dir_vectors (FILE *outf, vec<lambda_vector> dir_vects,
b44d1046 351 int length)
352{
353 unsigned j;
354 lambda_vector v;
355
f1f41a6c 356 FOR_EACH_VEC_ELT (dir_vects, j, v)
b44d1046 357 print_direction_vector (outf, v, length);
358}
359
e01f9f1f 360/* Print out a vector VEC of length N to OUTFILE. */
361
362static inline void
363print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
364{
365 int i;
366
367 for (i = 0; i < n; i++)
368 fprintf (outfile, "%3d ", vector[i]);
369 fprintf (outfile, "\n");
370}
371
b44d1046 372/* Print a vector of distance vectors. */
373
d129ada0 374static void
f1f41a6c 375print_dist_vectors (FILE *outf, vec<lambda_vector> dist_vects,
d129ada0 376 int length)
b44d1046 377{
378 unsigned j;
379 lambda_vector v;
380
f1f41a6c 381 FOR_EACH_VEC_ELT (dist_vects, j, v)
b44d1046 382 print_lambda_vector (outf, v, length);
383}
384
2146e26d 385/* Dump function for a DATA_DEPENDENCE_RELATION structure. */
386
d129ada0 387static void
48e1416a 388dump_data_dependence_relation (FILE *outf,
2146e26d 389 struct data_dependence_relation *ddr)
390{
2146e26d 391 struct data_reference *dra, *drb;
bc3c8ad4 392
bc3c8ad4 393 fprintf (outf, "(Data Dep: \n");
801c5610 394
4cc34f6b 395 if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
396 {
be2e5c02 397 if (ddr)
398 {
399 dra = DDR_A (ddr);
400 drb = DDR_B (ddr);
401 if (dra)
402 dump_data_reference (outf, dra);
403 else
404 fprintf (outf, " (nil)\n");
405 if (drb)
406 dump_data_reference (outf, drb);
407 else
408 fprintf (outf, " (nil)\n");
409 }
4cc34f6b 410 fprintf (outf, " (don't know)\n)\n");
411 return;
412 }
413
414 dra = DDR_A (ddr);
415 drb = DDR_B (ddr);
801c5610 416 dump_data_reference (outf, dra);
417 dump_data_reference (outf, drb);
418
4cc34f6b 419 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
2146e26d 420 fprintf (outf, " (no dependence)\n");
48e1416a 421
bc3c8ad4 422 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
2146e26d 423 {
bc3c8ad4 424 unsigned int i;
b44d1046 425 struct loop *loopi;
1532ec98 426
2146e26d 427 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
428 {
2146e26d 429 fprintf (outf, " access_fn_A: ");
430 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
431 fprintf (outf, " access_fn_B: ");
432 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
bc3c8ad4 433 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
2146e26d 434 }
1532ec98 435
355572cc 436 fprintf (outf, " inner loop index: %d\n", DDR_INNER_LOOP (ddr));
b44d1046 437 fprintf (outf, " loop nest: (");
f1f41a6c 438 FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
b44d1046 439 fprintf (outf, "%d ", loopi->num);
440 fprintf (outf, ")\n");
441
1532ec98 442 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
2146e26d 443 {
1532ec98 444 fprintf (outf, " distance_vector: ");
445 print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
b44d1046 446 DDR_NB_LOOPS (ddr));
bc3c8ad4 447 }
1532ec98 448
449 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
bc3c8ad4 450 {
1532ec98 451 fprintf (outf, " direction_vector: ");
6b421feb 452 print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
b44d1046 453 DDR_NB_LOOPS (ddr));
2146e26d 454 }
2146e26d 455 }
456
457 fprintf (outf, ")\n");
458}
459
d129ada0 460/* Debug version. */
2146e26d 461
d129ada0 462DEBUG_FUNCTION void
463debug_data_dependence_relation (struct data_dependence_relation *ddr)
2146e26d 464{
d129ada0 465 dump_data_dependence_relation (stderr, ddr);
466}
48e1416a 467
d129ada0 468/* Dump into FILE all the dependence relations from DDRS. */
48e1416a 469
d129ada0 470void
471dump_data_dependence_relations (FILE *file,
f1f41a6c 472 vec<ddr_p> ddrs)
d129ada0 473{
474 unsigned int i;
475 struct data_dependence_relation *ddr;
48e1416a 476
f1f41a6c 477 FOR_EACH_VEC_ELT (ddrs, i, ddr)
d129ada0 478 dump_data_dependence_relation (file, ddr);
479}
48e1416a 480
c7d89805 481DEBUG_FUNCTION void
482debug (vec<ddr_p> &ref)
483{
484 dump_data_dependence_relations (stderr, ref);
485}
486
487DEBUG_FUNCTION void
488debug (vec<ddr_p> *ptr)
489{
490 if (ptr)
491 debug (*ptr);
492 else
493 fprintf (stderr, "<nil>\n");
494}
495
496
d129ada0 497/* Dump to STDERR all the dependence relations from DDRS. */
48e1416a 498
d129ada0 499DEBUG_FUNCTION void
f1f41a6c 500debug_data_dependence_relations (vec<ddr_p> ddrs)
d129ada0 501{
502 dump_data_dependence_relations (stderr, ddrs);
2146e26d 503}
504
bc3c8ad4 505/* Dumps the distance and direction vectors in FILE. DDRS contains
506 the dependence relations, and VECT_SIZE is the size of the
507 dependence vectors, or in other words the number of loops in the
508 considered nest. */
509
d129ada0 510static void
f1f41a6c 511dump_dist_dir_vectors (FILE *file, vec<ddr_p> ddrs)
bc3c8ad4 512{
1532ec98 513 unsigned int i, j;
41c7a324 514 struct data_dependence_relation *ddr;
515 lambda_vector v;
bc3c8ad4 516
f1f41a6c 517 FOR_EACH_VEC_ELT (ddrs, i, ddr)
41c7a324 518 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
519 {
f1f41a6c 520 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), j, v)
41c7a324 521 {
522 fprintf (file, "DISTANCE_V (");
523 print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
524 fprintf (file, ")\n");
525 }
526
f1f41a6c 527 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), j, v)
41c7a324 528 {
529 fprintf (file, "DIRECTION_V (");
530 print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
531 fprintf (file, ")\n");
532 }
533 }
1532ec98 534
bc3c8ad4 535 fprintf (file, "\n\n");
536}
537
538/* Dumps the data dependence relations DDRS in FILE. */
539
d129ada0 540static void
f1f41a6c 541dump_ddrs (FILE *file, vec<ddr_p> ddrs)
bc3c8ad4 542{
543 unsigned int i;
41c7a324 544 struct data_dependence_relation *ddr;
545
f1f41a6c 546 FOR_EACH_VEC_ELT (ddrs, i, ddr)
41c7a324 547 dump_data_dependence_relation (file, ddr);
bc3c8ad4 548
bc3c8ad4 549 fprintf (file, "\n\n");
550}
551
d129ada0 552DEBUG_FUNCTION void
f1f41a6c 553debug_ddrs (vec<ddr_p> ddrs)
d129ada0 554{
555 dump_ddrs (stderr, ddrs);
556}
557
75a70cf9 558/* Helper function for split_constant_offset. Expresses OP0 CODE OP1
559 (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
560 constant of type ssizetype, and returns true. If we cannot do this
561 with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
562 is returned. */
516849c7 563
75a70cf9 564static bool
565split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
566 tree *var, tree *off)
516849c7 567{
80bb306a 568 tree var0, var1;
569 tree off0, off1;
75a70cf9 570 enum tree_code ocode = code;
516849c7 571
75a70cf9 572 *var = NULL_TREE;
573 *off = NULL_TREE;
516849c7 574
0de36bdb 575 switch (code)
516849c7 576 {
80bb306a 577 case INTEGER_CST:
578 *var = build_int_cst (type, 0);
75a70cf9 579 *off = fold_convert (ssizetype, op0);
580 return true;
516849c7 581
0de36bdb 582 case POINTER_PLUS_EXPR:
75a70cf9 583 ocode = PLUS_EXPR;
0de36bdb 584 /* FALLTHROUGH */
80bb306a 585 case PLUS_EXPR:
586 case MINUS_EXPR:
75a70cf9 587 split_constant_offset (op0, &var0, &off0);
588 split_constant_offset (op1, &var1, &off1);
589 *var = fold_build2 (code, type, var0, var1);
590 *off = size_binop (ocode, off0, off1);
591 return true;
516849c7 592
516849c7 593 case MULT_EXPR:
75a70cf9 594 if (TREE_CODE (op1) != INTEGER_CST)
595 return false;
80bb306a 596
75a70cf9 597 split_constant_offset (op0, &var0, &off0);
598 *var = fold_build2 (MULT_EXPR, type, var0, op1);
599 *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
600 return true;
516849c7 601
80bb306a 602 case ADDR_EXPR:
603 {
75a70cf9 604 tree base, poffset;
80bb306a 605 HOST_WIDE_INT pbitsize, pbitpos;
606 enum machine_mode pmode;
607 int punsignedp, pvolatilep;
516849c7 608
97d819e5 609 op0 = TREE_OPERAND (op0, 0);
75a70cf9 610 base = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset,
80bb306a 611 &pmode, &punsignedp, &pvolatilep, false);
516849c7 612
80bb306a 613 if (pbitpos % BITS_PER_UNIT != 0)
75a70cf9 614 return false;
80bb306a 615 base = build_fold_addr_expr (base);
616 off0 = ssize_int (pbitpos / BITS_PER_UNIT);
516849c7 617
80bb306a 618 if (poffset)
619 {
620 split_constant_offset (poffset, &poffset, &off1);
621 off0 = size_binop (PLUS_EXPR, off0, off1);
55ed4df6 622 if (POINTER_TYPE_P (TREE_TYPE (base)))
2cc66f2a 623 base = fold_build_pointer_plus (base, poffset);
55ed4df6 624 else
625 base = fold_build2 (PLUS_EXPR, TREE_TYPE (base), base,
626 fold_convert (TREE_TYPE (base), poffset));
80bb306a 627 }
628
69a83716 629 var0 = fold_convert (type, base);
630
631 /* If variable length types are involved, punt, otherwise casts
632 might be converted into ARRAY_REFs in gimplify_conversion.
633 To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
634 possibly no longer appears in current GIMPLE, might resurface.
635 This perhaps could run
d9659041 636 if (CONVERT_EXPR_P (var0))
69a83716 637 {
638 gimplify_conversion (&var0);
639 // Attempt to fill in any within var0 found ARRAY_REF's
640 // element size from corresponding op embedded ARRAY_REF,
641 // if unsuccessful, just punt.
642 } */
643 while (POINTER_TYPE_P (type))
644 type = TREE_TYPE (type);
645 if (int_size_in_bytes (type) < 0)
75a70cf9 646 return false;
69a83716 647
648 *var = var0;
80bb306a 649 *off = off0;
75a70cf9 650 return true;
80bb306a 651 }
516849c7 652
a091367a 653 case SSA_NAME:
654 {
75a70cf9 655 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
656 enum tree_code subcode;
a091367a 657
75a70cf9 658 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
659 return false;
660
661 var0 = gimple_assign_rhs1 (def_stmt);
662 subcode = gimple_assign_rhs_code (def_stmt);
663 var1 = gimple_assign_rhs2 (def_stmt);
664
665 return split_constant_offset_1 (type, var0, subcode, var1, var, off);
a091367a 666 }
be2e5c02 667 CASE_CONVERT:
668 {
669 /* We must not introduce undefined overflow, and we must not change the value.
670 Hence we're okay if the inner type doesn't overflow to start with
671 (pointer or signed), the outer type also is an integer or pointer
672 and the outer precision is at least as large as the inner. */
673 tree itype = TREE_TYPE (op0);
674 if ((POINTER_TYPE_P (itype)
675 || (INTEGRAL_TYPE_P (itype) && TYPE_OVERFLOW_UNDEFINED (itype)))
676 && TYPE_PRECISION (type) >= TYPE_PRECISION (itype)
677 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)))
678 {
679 split_constant_offset (op0, &var0, off);
680 *var = fold_convert (type, var0);
681 return true;
682 }
683 return false;
684 }
a091367a 685
516849c7 686 default:
75a70cf9 687 return false;
516849c7 688 }
75a70cf9 689}
690
691/* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
692 will be ssizetype. */
693
694void
695split_constant_offset (tree exp, tree *var, tree *off)
696{
697 tree type = TREE_TYPE (exp), otype, op0, op1, e, o;
698 enum tree_code code;
516849c7 699
75a70cf9 700 *var = exp;
80bb306a 701 *off = ssize_int (0);
75a70cf9 702 STRIP_NOPS (exp);
703
32246fd4 704 if (tree_is_chrec (exp)
705 || get_gimple_rhs_class (TREE_CODE (exp)) == GIMPLE_TERNARY_RHS)
75a70cf9 706 return;
707
708 otype = TREE_TYPE (exp);
709 code = TREE_CODE (exp);
710 extract_ops_from_tree (exp, &code, &op0, &op1);
711 if (split_constant_offset_1 (otype, op0, code, op1, &e, &o))
712 {
713 *var = fold_convert (type, e);
714 *off = o;
715 }
516849c7 716}
717
80bb306a 718/* Returns the address ADDR of an object in a canonical shape (without nop
719 casts, and with type of pointer to the object). */
516849c7 720
721static tree
80bb306a 722canonicalize_base_object_address (tree addr)
516849c7 723{
ad4a85ad 724 tree orig = addr;
725
80bb306a 726 STRIP_NOPS (addr);
516849c7 727
ad4a85ad 728 /* The base address may be obtained by casting from integer, in that case
729 keep the cast. */
730 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
731 return orig;
732
80bb306a 733 if (TREE_CODE (addr) != ADDR_EXPR)
734 return addr;
516849c7 735
80bb306a 736 return build_fold_addr_expr (TREE_OPERAND (addr, 0));
516849c7 737}
738
48e1416a 739/* Analyzes the behavior of the memory reference DR in the innermost loop or
0c257e4c 740 basic block that contains it. Returns true if analysis succeed or false
37545e54 741 otherwise. */
516849c7 742
880734c8 743bool
0c257e4c 744dr_analyze_innermost (struct data_reference *dr, struct loop *nest)
516849c7 745{
75a70cf9 746 gimple stmt = DR_STMT (dr);
80bb306a 747 struct loop *loop = loop_containing_stmt (stmt);
748 tree ref = DR_REF (dr);
516849c7 749 HOST_WIDE_INT pbitsize, pbitpos;
80bb306a 750 tree base, poffset;
516849c7 751 enum machine_mode pmode;
752 int punsignedp, pvolatilep;
80bb306a 753 affine_iv base_iv, offset_iv;
754 tree init, dinit, step;
37545e54 755 bool in_loop = (loop && loop->num);
80bb306a 756
757 if (dump_file && (dump_flags & TDF_DETAILS))
758 fprintf (dump_file, "analyze_innermost: ");
516849c7 759
80bb306a 760 base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset,
761 &pmode, &punsignedp, &pvolatilep, false);
762 gcc_assert (base != NULL_TREE);
516849c7 763
80bb306a 764 if (pbitpos % BITS_PER_UNIT != 0)
516849c7 765 {
80bb306a 766 if (dump_file && (dump_flags & TDF_DETAILS))
767 fprintf (dump_file, "failed: bit offset alignment.\n");
880734c8 768 return false;
80bb306a 769 }
516849c7 770
182cf5a9 771 if (TREE_CODE (base) == MEM_REF)
772 {
773 if (!integer_zerop (TREE_OPERAND (base, 1)))
774 {
7667733b 775 double_int moff = mem_ref_offset (base);
776 tree mofft = double_int_to_tree (sizetype, moff);
182cf5a9 777 if (!poffset)
7667733b 778 poffset = mofft;
182cf5a9 779 else
7667733b 780 poffset = size_binop (PLUS_EXPR, poffset, mofft);
182cf5a9 781 }
782 base = TREE_OPERAND (base, 0);
783 }
784 else
785 base = build_fold_addr_expr (base);
0c257e4c 786
37545e54 787 if (in_loop)
80bb306a 788 {
48e1416a 789 if (!simple_iv (loop, loop_containing_stmt (stmt), base, &base_iv,
f634c3e9 790 nest ? true : false))
37545e54 791 {
0c257e4c 792 if (nest)
793 {
794 if (dump_file && (dump_flags & TDF_DETAILS))
795 fprintf (dump_file, "failed: evolution of base is not"
796 " affine.\n");
797 return false;
798 }
799 else
800 {
801 base_iv.base = base;
802 base_iv.step = ssize_int (0);
803 base_iv.no_overflow = true;
804 }
37545e54 805 }
806 }
807 else
808 {
809 base_iv.base = base;
810 base_iv.step = ssize_int (0);
811 base_iv.no_overflow = true;
80bb306a 812 }
37545e54 813
dd2644cf 814 if (!poffset)
80bb306a 815 {
816 offset_iv.base = ssize_int (0);
817 offset_iv.step = ssize_int (0);
818 }
dd2644cf 819 else
80bb306a 820 {
dd2644cf 821 if (!in_loop)
822 {
823 offset_iv.base = poffset;
824 offset_iv.step = ssize_int (0);
825 }
826 else if (!simple_iv (loop, loop_containing_stmt (stmt),
f634c3e9 827 poffset, &offset_iv,
828 nest ? true : false))
dd2644cf 829 {
0c257e4c 830 if (nest)
831 {
832 if (dump_file && (dump_flags & TDF_DETAILS))
833 fprintf (dump_file, "failed: evolution of offset is not"
834 " affine.\n");
835 return false;
836 }
837 else
838 {
839 offset_iv.base = poffset;
840 offset_iv.step = ssize_int (0);
841 }
dd2644cf 842 }
80bb306a 843 }
516849c7 844
80bb306a 845 init = ssize_int (pbitpos / BITS_PER_UNIT);
846 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
847 init = size_binop (PLUS_EXPR, init, dinit);
848 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
849 init = size_binop (PLUS_EXPR, init, dinit);
516849c7 850
80bb306a 851 step = size_binop (PLUS_EXPR,
852 fold_convert (ssizetype, base_iv.step),
853 fold_convert (ssizetype, offset_iv.step));
516849c7 854
80bb306a 855 DR_BASE_ADDRESS (dr) = canonicalize_base_object_address (base_iv.base);
516849c7 856
80bb306a 857 DR_OFFSET (dr) = fold_convert (ssizetype, offset_iv.base);
858 DR_INIT (dr) = init;
859 DR_STEP (dr) = step;
516849c7 860
80bb306a 861 DR_ALIGNED_TO (dr) = size_int (highest_pow2_factor (offset_iv.base));
516849c7 862
80bb306a 863 if (dump_file && (dump_flags & TDF_DETAILS))
864 fprintf (dump_file, "success.\n");
880734c8 865
866 return true;
80bb306a 867}
516849c7 868
80bb306a 869/* Determines the base object and the list of indices of memory reference
221a697e 870 DR, analyzed in LOOP and instantiated in loop nest NEST. */
516849c7 871
80bb306a 872static void
221a697e 873dr_analyze_indices (struct data_reference *dr, loop_p nest, loop_p loop)
80bb306a 874{
1e094109 875 vec<tree> access_fns = vNULL;
95539e1d 876 tree ref, op;
93fc59e8 877 tree base, off, access_fn;
878 basic_block before_loop;
48e1416a 879
93fc59e8 880 /* If analyzing a basic-block there are no indices to analyze
881 and thus no access functions. */
5fc88ffd 882 if (!nest)
883 {
93fc59e8 884 DR_BASE_OBJECT (dr) = DR_REF (dr);
f1f41a6c 885 DR_ACCESS_FNS (dr).create (0);
5fc88ffd 886 return;
887 }
888
95539e1d 889 ref = DR_REF (dr);
5fc88ffd 890 before_loop = block_before_loop (nest);
48e1416a 891
93fc59e8 892 /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
893 into a two element array with a constant index. The base is
894 then just the immediate underlying object. */
895 if (TREE_CODE (ref) == REALPART_EXPR)
896 {
897 ref = TREE_OPERAND (ref, 0);
f1f41a6c 898 access_fns.safe_push (integer_zero_node);
93fc59e8 899 }
900 else if (TREE_CODE (ref) == IMAGPART_EXPR)
901 {
902 ref = TREE_OPERAND (ref, 0);
f1f41a6c 903 access_fns.safe_push (integer_one_node);
93fc59e8 904 }
905
bc4113a9 906 /* Analyze access functions of dimensions we know to be independent. */
95539e1d 907 while (handled_component_p (ref))
516849c7 908 {
95539e1d 909 if (TREE_CODE (ref) == ARRAY_REF)
516849c7 910 {
95539e1d 911 op = TREE_OPERAND (ref, 1);
5fc88ffd 912 access_fn = analyze_scalar_evolution (loop, op);
913 access_fn = instantiate_scev (before_loop, loop, access_fn);
f1f41a6c 914 access_fns.safe_push (access_fn);
bc4113a9 915 }
95539e1d 916 else if (TREE_CODE (ref) == COMPONENT_REF
917 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
918 {
919 /* For COMPONENT_REFs of records (but not unions!) use the
920 FIELD_DECL offset as constant access function so we can
921 disambiguate a[i].f1 and a[i].f2. */
922 tree off = component_ref_field_offset (ref);
923 off = size_binop (PLUS_EXPR,
924 size_binop (MULT_EXPR,
925 fold_convert (bitsizetype, off),
926 bitsize_int (BITS_PER_UNIT)),
927 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
f1f41a6c 928 access_fns.safe_push (off);
95539e1d 929 }
930 else
931 /* If we have an unhandled component we could not translate
932 to an access function stop analyzing. We have determined
933 our base object in this case. */
934 break;
48e1416a 935
95539e1d 936 ref = TREE_OPERAND (ref, 0);
516849c7 937 }
938
0f00ec21 939 /* If the address operand of a MEM_REF base has an evolution in the
940 analyzed nest, add it as an additional independent access-function. */
95539e1d 941 if (TREE_CODE (ref) == MEM_REF)
516849c7 942 {
95539e1d 943 op = TREE_OPERAND (ref, 0);
80bb306a 944 access_fn = analyze_scalar_evolution (loop, op);
089aa668 945 access_fn = instantiate_scev (before_loop, loop, access_fn);
0f00ec21 946 if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
9488020c 947 {
a927c5fa 948 tree orig_type;
95539e1d 949 tree memoff = TREE_OPERAND (ref, 1);
0f00ec21 950 base = initial_condition (access_fn);
a927c5fa 951 orig_type = TREE_TYPE (base);
952 STRIP_USELESS_TYPE_CONVERSION (base);
0f00ec21 953 split_constant_offset (base, &base, &off);
954 /* Fold the MEM_REF offset into the evolutions initial
955 value to make more bases comparable. */
95539e1d 956 if (!integer_zerop (memoff))
0f00ec21 957 {
958 off = size_binop (PLUS_EXPR, off,
95539e1d 959 fold_convert (ssizetype, memoff));
960 memoff = build_int_cst (TREE_TYPE (memoff), 0);
0f00ec21 961 }
962 access_fn = chrec_replace_initial_condition
a927c5fa 963 (access_fn, fold_convert (orig_type, off));
95539e1d 964 /* ??? This is still not a suitable base object for
965 dr_may_alias_p - the base object needs to be an
966 access that covers the object as whole. With
967 an evolution in the pointer this cannot be
968 guaranteed.
969 As a band-aid, mark the access so we can special-case
970 it in dr_may_alias_p. */
971 ref = fold_build2_loc (EXPR_LOCATION (ref),
972 MEM_REF, TREE_TYPE (ref),
973 base, memoff);
974 DR_UNCONSTRAINED_BASE (dr) = true;
f1f41a6c 975 access_fns.safe_push (access_fn);
9488020c 976 }
516849c7 977 }
95539e1d 978 else if (DECL_P (ref))
979 {
980 /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
981 ref = build2 (MEM_REF, TREE_TYPE (ref),
982 build_fold_addr_expr (ref),
983 build_int_cst (reference_alias_ptr_type (ref), 0));
984 }
516849c7 985
80bb306a 986 DR_BASE_OBJECT (dr) = ref;
987 DR_ACCESS_FNS (dr) = access_fns;
516849c7 988}
989
80bb306a 990/* Extracts the alias analysis information from the memory reference DR. */
516849c7 991
80bb306a 992static void
993dr_analyze_alias (struct data_reference *dr)
516849c7 994{
80bb306a 995 tree ref = DR_REF (dr);
dd277d48 996 tree base = get_base_address (ref), addr;
997
182cf5a9 998 if (INDIRECT_REF_P (base)
999 || TREE_CODE (base) == MEM_REF)
80bb306a 1000 {
1001 addr = TREE_OPERAND (base, 0);
1002 if (TREE_CODE (addr) == SSA_NAME)
dd277d48 1003 DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
80bb306a 1004 }
80bb306a 1005}
516849c7 1006
80bb306a 1007/* Frees data reference DR. */
31e389a2 1008
801c5610 1009void
31e389a2 1010free_data_ref (data_reference_p dr)
1011{
f1f41a6c 1012 DR_ACCESS_FNS (dr).release ();
31e389a2 1013 free (dr);
1014}
516849c7 1015
80bb306a 1016/* Analyzes memory reference MEMREF accessed in STMT. The reference
1017 is read if IS_READ is true, write otherwise. Returns the
221a697e 1018 data_reference description of MEMREF. NEST is the outermost loop
1019 in which the reference should be instantiated, LOOP is the loop in
1020 which the data reference should be analyzed. */
516849c7 1021
5c205353 1022struct data_reference *
221a697e 1023create_data_ref (loop_p nest, loop_p loop, tree memref, gimple stmt,
1024 bool is_read)
516849c7 1025{
80bb306a 1026 struct data_reference *dr;
6b421feb 1027
80bb306a 1028 if (dump_file && (dump_flags & TDF_DETAILS))
6b421feb 1029 {
80bb306a 1030 fprintf (dump_file, "Creating dr for ");
1031 print_generic_expr (dump_file, memref, TDF_SLIM);
1032 fprintf (dump_file, "\n");
6b421feb 1033 }
f84a688a 1034
80bb306a 1035 dr = XCNEW (struct data_reference);
1036 DR_STMT (dr) = stmt;
1037 DR_REF (dr) = memref;
1038 DR_IS_READ (dr) = is_read;
516849c7 1039
0c257e4c 1040 dr_analyze_innermost (dr, nest);
221a697e 1041 dr_analyze_indices (dr, nest, loop);
80bb306a 1042 dr_analyze_alias (dr);
516849c7 1043
1044 if (dump_file && (dump_flags & TDF_DETAILS))
1045 {
bc4113a9 1046 unsigned i;
80bb306a 1047 fprintf (dump_file, "\tbase_address: ");
516849c7 1048 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1049 fprintf (dump_file, "\n\toffset from base address: ");
1050 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1051 fprintf (dump_file, "\n\tconstant offset from base address: ");
1052 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
516849c7 1053 fprintf (dump_file, "\n\tstep: ");
1054 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
80bb306a 1055 fprintf (dump_file, "\n\taligned to: ");
1056 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
1057 fprintf (dump_file, "\n\tbase_object: ");
1058 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
516849c7 1059 fprintf (dump_file, "\n");
bc4113a9 1060 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
1061 {
1062 fprintf (dump_file, "\tAccess function %d: ", i);
1063 print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
1064 }
80bb306a 1065 }
1066
48e1416a 1067 return dr;
516849c7 1068}
1069
ec611e12 1070/* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
1071 expressions. */
1072static bool
1073dr_equal_offsets_p1 (tree offset1, tree offset2)
1074{
1075 bool res;
1076
1077 STRIP_NOPS (offset1);
1078 STRIP_NOPS (offset2);
1079
1080 if (offset1 == offset2)
1081 return true;
1082
1083 if (TREE_CODE (offset1) != TREE_CODE (offset2)
1084 || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
1085 return false;
1086
1087 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
1088 TREE_OPERAND (offset2, 0));
1089
1090 if (!res || !BINARY_CLASS_P (offset1))
1091 return res;
1092
1093 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
1094 TREE_OPERAND (offset2, 1));
1095
1096 return res;
1097}
1098
1099/* Check if DRA and DRB have equal offsets. */
1100bool
1101dr_equal_offsets_p (struct data_reference *dra,
1102 struct data_reference *drb)
1103{
1104 tree offset1, offset2;
1105
1106 offset1 = DR_OFFSET (dra);
1107 offset2 = DR_OFFSET (drb);
1108
1109 return dr_equal_offsets_p1 (offset1, offset2);
1110}
1111
87da4f2e 1112/* Returns true if FNA == FNB. */
1113
1114static bool
1115affine_function_equal_p (affine_fn fna, affine_fn fnb)
1116{
f1f41a6c 1117 unsigned i, n = fna.length ();
516849c7 1118
f1f41a6c 1119 if (n != fnb.length ())
de33470d 1120 return false;
bc3c8ad4 1121
87da4f2e 1122 for (i = 0; i < n; i++)
f1f41a6c 1123 if (!operand_equal_p (fna[i], fnb[i], 0))
87da4f2e 1124 return false;
1125
1126 return true;
1127}
1128
1129/* If all the functions in CF are the same, returns one of them,
1130 otherwise returns NULL. */
1131
1132static affine_fn
1133common_affine_function (conflict_function *cf)
bc3c8ad4 1134{
87da4f2e 1135 unsigned i;
1136 affine_fn comm;
1137
1138 if (!CF_NONTRIVIAL_P (cf))
f1f41a6c 1139 return affine_fn();
87da4f2e 1140
1141 comm = cf->fns[0];
1142
1143 for (i = 1; i < cf->n; i++)
1144 if (!affine_function_equal_p (comm, cf->fns[i]))
f1f41a6c 1145 return affine_fn();
87da4f2e 1146
1147 return comm;
1148}
bc3c8ad4 1149
87da4f2e 1150/* Returns the base of the affine function FN. */
1151
1152static tree
1153affine_function_base (affine_fn fn)
1154{
f1f41a6c 1155 return fn[0];
87da4f2e 1156}
1157
1158/* Returns true if FN is a constant. */
1159
1160static bool
1161affine_function_constant_p (affine_fn fn)
1162{
1163 unsigned i;
1164 tree coef;
1165
f1f41a6c 1166 for (i = 1; fn.iterate (i, &coef); i++)
87da4f2e 1167 if (!integer_zerop (coef))
f84a688a 1168 return false;
1169
bc3c8ad4 1170 return true;
1171}
1172
2a21a5df 1173/* Returns true if FN is the zero constant function. */
1174
1175static bool
1176affine_function_zero_p (affine_fn fn)
1177{
1178 return (integer_zerop (affine_function_base (fn))
1179 && affine_function_constant_p (fn));
1180}
1181
9a3dad1c 1182/* Returns a signed integer type with the largest precision from TA
1183 and TB. */
1184
1185static tree
1186signed_type_for_types (tree ta, tree tb)
1187{
1188 if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
1189 return signed_type_for (ta);
1190 else
1191 return signed_type_for (tb);
1192}
1193
87da4f2e 1194/* Applies operation OP on affine functions FNA and FNB, and returns the
1195 result. */
1196
1197static affine_fn
1198affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
1199{
1200 unsigned i, n, m;
1201 affine_fn ret;
1202 tree coef;
1203
f1f41a6c 1204 if (fnb.length () > fna.length ())
87da4f2e 1205 {
f1f41a6c 1206 n = fna.length ();
1207 m = fnb.length ();
87da4f2e 1208 }
1209 else
1210 {
f1f41a6c 1211 n = fnb.length ();
1212 m = fna.length ();
87da4f2e 1213 }
1214
f1f41a6c 1215 ret.create (m);
87da4f2e 1216 for (i = 0; i < n; i++)
9a3dad1c 1217 {
f1f41a6c 1218 tree type = signed_type_for_types (TREE_TYPE (fna[i]),
1219 TREE_TYPE (fnb[i]));
1220 ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
9a3dad1c 1221 }
87da4f2e 1222
f1f41a6c 1223 for (; fna.iterate (i, &coef); i++)
1224 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
87da4f2e 1225 coef, integer_zero_node));
f1f41a6c 1226 for (; fnb.iterate (i, &coef); i++)
1227 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
87da4f2e 1228 integer_zero_node, coef));
1229
1230 return ret;
1231}
1232
1233/* Returns the sum of affine functions FNA and FNB. */
1234
1235static affine_fn
1236affine_fn_plus (affine_fn fna, affine_fn fnb)
1237{
1238 return affine_fn_op (PLUS_EXPR, fna, fnb);
1239}
1240
1241/* Returns the difference of affine functions FNA and FNB. */
1242
1243static affine_fn
1244affine_fn_minus (affine_fn fna, affine_fn fnb)
1245{
1246 return affine_fn_op (MINUS_EXPR, fna, fnb);
1247}
1248
1249/* Frees affine function FN. */
1250
1251static void
1252affine_fn_free (affine_fn fn)
1253{
f1f41a6c 1254 fn.release ();
87da4f2e 1255}
1256
bc3c8ad4 1257/* Determine for each subscript in the data dependence relation DDR
1258 the distance. */
2146e26d 1259
6b421feb 1260static void
bc3c8ad4 1261compute_subscript_distance (struct data_dependence_relation *ddr)
2146e26d 1262{
87da4f2e 1263 conflict_function *cf_a, *cf_b;
1264 affine_fn fn_a, fn_b, diff;
1265
2146e26d 1266 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1267 {
1268 unsigned int i;
48e1416a 1269
2146e26d 1270 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1271 {
2146e26d 1272 struct subscript *subscript;
48e1416a 1273
2146e26d 1274 subscript = DDR_SUBSCRIPT (ddr, i);
87da4f2e 1275 cf_a = SUB_CONFLICTS_IN_A (subscript);
1276 cf_b = SUB_CONFLICTS_IN_B (subscript);
bc3c8ad4 1277
87da4f2e 1278 fn_a = common_affine_function (cf_a);
1279 fn_b = common_affine_function (cf_b);
f1f41a6c 1280 if (!fn_a.exists () || !fn_b.exists ())
bc3c8ad4 1281 {
87da4f2e 1282 SUB_DISTANCE (subscript) = chrec_dont_know;
1283 return;
bc3c8ad4 1284 }
87da4f2e 1285 diff = affine_fn_minus (fn_a, fn_b);
48e1416a 1286
87da4f2e 1287 if (affine_function_constant_p (diff))
1288 SUB_DISTANCE (subscript) = affine_function_base (diff);
2146e26d 1289 else
1290 SUB_DISTANCE (subscript) = chrec_dont_know;
87da4f2e 1291
1292 affine_fn_free (diff);
2146e26d 1293 }
1294 }
1295}
1296
87da4f2e 1297/* Returns the conflict function for "unknown". */
1298
1299static conflict_function *
1300conflict_fn_not_known (void)
1301{
1302 conflict_function *fn = XCNEW (conflict_function);
1303 fn->n = NOT_KNOWN;
1304
1305 return fn;
1306}
1307
1308/* Returns the conflict function for "independent". */
1309
1310static conflict_function *
1311conflict_fn_no_dependence (void)
1312{
1313 conflict_function *fn = XCNEW (conflict_function);
1314 fn->n = NO_DEPENDENCE;
1315
1316 return fn;
1317}
1318
80bb306a 1319/* Returns true if the address of OBJ is invariant in LOOP. */
1320
1321static bool
7ecb5bb2 1322object_address_invariant_in_loop_p (const struct loop *loop, const_tree obj)
80bb306a 1323{
1324 while (handled_component_p (obj))
1325 {
1326 if (TREE_CODE (obj) == ARRAY_REF)
1327 {
1328 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1329 need to check the stride and the lower bound of the reference. */
1330 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1331 loop->num)
1332 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 3),
1333 loop->num))
1334 return false;
1335 }
1336 else if (TREE_CODE (obj) == COMPONENT_REF)
1337 {
1338 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1339 loop->num))
1340 return false;
1341 }
1342 obj = TREE_OPERAND (obj, 0);
1343 }
1344
182cf5a9 1345 if (!INDIRECT_REF_P (obj)
1346 && TREE_CODE (obj) != MEM_REF)
80bb306a 1347 return true;
1348
1349 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
1350 loop->num);
1351}
1352
80bb306a 1353/* Returns false if we can prove that data references A and B do not alias,
5fc88ffd 1354 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
1355 considered. */
80bb306a 1356
255b6be7 1357bool
5fc88ffd 1358dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
1359 bool loop_nest)
80bb306a 1360{
2760c0e3 1361 tree addr_a = DR_BASE_OBJECT (a);
1362 tree addr_b = DR_BASE_OBJECT (b);
80bb306a 1363
5fc88ffd 1364 /* If we are not processing a loop nest but scalar code we
1365 do not need to care about possible cross-iteration dependences
1366 and thus can process the full original reference. Do so,
1367 similar to how loop invariant motion applies extra offset-based
1368 disambiguation. */
1369 if (!loop_nest)
1370 {
1371 aff_tree off1, off2;
1372 double_int size1, size2;
1373 get_inner_reference_aff (DR_REF (a), &off1, &size1);
1374 get_inner_reference_aff (DR_REF (b), &off2, &size2);
1375 aff_combination_scale (&off1, double_int_minus_one);
1376 aff_combination_add (&off2, &off1);
1377 if (aff_comb_cannot_overlap_p (&off2, size1, size2))
1378 return false;
1379 }
1380
95539e1d 1381 /* If we had an evolution in a MEM_REF BASE_OBJECT we do not know
1382 the size of the base-object. So we cannot do any offset/overlap
1383 based analysis but have to rely on points-to information only. */
1384 if (TREE_CODE (addr_a) == MEM_REF
1385 && DR_UNCONSTRAINED_BASE (a))
1386 {
1387 if (TREE_CODE (addr_b) == MEM_REF
1388 && DR_UNCONSTRAINED_BASE (b))
1389 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1390 TREE_OPERAND (addr_b, 0));
1391 else
1392 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1393 build_fold_addr_expr (addr_b));
1394 }
1395 else if (TREE_CODE (addr_b) == MEM_REF
1396 && DR_UNCONSTRAINED_BASE (b))
1397 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
1398 TREE_OPERAND (addr_b, 0));
1399
1400 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1401 that is being subsetted in the loop nest. */
9ff25603 1402 if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
2760c0e3 1403 return refs_output_dependent_p (addr_a, addr_b);
9ff25603 1404 else if (DR_IS_READ (a) && DR_IS_WRITE (b))
2760c0e3 1405 return refs_anti_dependent_p (addr_a, addr_b);
1406 return refs_may_alias_p (addr_a, addr_b);
80bb306a 1407}
1408
6b421feb 1409/* Initialize a data dependence relation between data accesses A and
1410 B. NB_LOOPS is the number of loops surrounding the references: the
1411 size of the classic distance/direction vectors. */
2146e26d 1412
16dfb112 1413struct data_dependence_relation *
48e1416a 1414initialize_data_dependence_relation (struct data_reference *a,
6b421feb 1415 struct data_reference *b,
f1f41a6c 1416 vec<loop_p> loop_nest)
2146e26d 1417{
1418 struct data_dependence_relation *res;
6b421feb 1419 unsigned int i;
48e1416a 1420
4c36ffe6 1421 res = XNEW (struct data_dependence_relation);
2146e26d 1422 DDR_A (res) = a;
1423 DDR_B (res) = b;
f1f41a6c 1424 DDR_LOOP_NEST (res).create (0);
0ecb94cf 1425 DDR_REVERSED_P (res) = false;
f1f41a6c 1426 DDR_SUBSCRIPTS (res).create (0);
1427 DDR_DIR_VECTS (res).create (0);
1428 DDR_DIST_VECTS (res).create (0);
2146e26d 1429
516849c7 1430 if (a == NULL || b == NULL)
1431 {
48e1416a 1432 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
516849c7 1433 return res;
48e1416a 1434 }
516849c7 1435
80bb306a 1436 /* If the data references do not alias, then they are independent. */
f1f41a6c 1437 if (!dr_may_alias_p (a, b, loop_nest.exists ()))
516849c7 1438 {
48e1416a 1439 DDR_ARE_DEPENDENT (res) = chrec_known;
516849c7 1440 return res;
1441 }
2146e26d 1442
cc76857b 1443 /* The case where the references are exactly the same. */
c127dd86 1444 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
1445 {
f1f41a6c 1446 if (loop_nest.exists ()
1447 && !object_address_invariant_in_loop_p (loop_nest[0],
c76b51be 1448 DR_BASE_OBJECT (a)))
1449 {
1450 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1451 return res;
1452 }
c127dd86 1453 DDR_AFFINE_P (res) = true;
1454 DDR_ARE_DEPENDENT (res) = NULL_TREE;
f1f41a6c 1455 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
c127dd86 1456 DDR_LOOP_NEST (res) = loop_nest;
1457 DDR_INNER_LOOP (res) = 0;
1458 DDR_SELF_REFERENCE (res) = true;
c76b51be 1459 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1460 {
1461 struct subscript *subscript;
1462
1463 subscript = XNEW (struct subscript);
1464 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1465 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1466 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1467 SUB_DISTANCE (subscript) = chrec_dont_know;
f1f41a6c 1468 DDR_SUBSCRIPTS (res).safe_push (subscript);
c76b51be 1469 }
c127dd86 1470 return res;
1471 }
1472
80bb306a 1473 /* If the references do not access the same object, we do not know
1474 whether they alias or not. */
1475 if (!operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0))
2146e26d 1476 {
48e1416a 1477 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
516849c7 1478 return res;
1479 }
6b421feb 1480
80bb306a 1481 /* If the base of the object is not invariant in the loop nest, we cannot
bef304b8 1482 analyze it. TODO -- in fact, it would suffice to record that there may
310d2511 1483 be arbitrary dependences in the loops where the base object varies. */
f1f41a6c 1484 if (loop_nest.exists ()
1485 && !object_address_invariant_in_loop_p (loop_nest[0],
37545e54 1486 DR_BASE_OBJECT (a)))
516849c7 1487 {
48e1416a 1488 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
516849c7 1489 return res;
1490 }
80bb306a 1491
fabd4538 1492 /* If the number of dimensions of the access to not agree we can have
1493 a pointer access to a component of the array element type and an
1494 array access while the base-objects are still the same. Punt. */
1495 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1496 {
1497 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1498 return res;
1499 }
80bb306a 1500
516849c7 1501 DDR_AFFINE_P (res) = true;
1502 DDR_ARE_DEPENDENT (res) = NULL_TREE;
f1f41a6c 1503 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
b44d1046 1504 DDR_LOOP_NEST (res) = loop_nest;
355572cc 1505 DDR_INNER_LOOP (res) = 0;
c127dd86 1506 DDR_SELF_REFERENCE (res) = false;
1532ec98 1507
516849c7 1508 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1509 {
1510 struct subscript *subscript;
48e1416a 1511
4c36ffe6 1512 subscript = XNEW (struct subscript);
87da4f2e 1513 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1514 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
516849c7 1515 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1516 SUB_DISTANCE (subscript) = chrec_dont_know;
f1f41a6c 1517 DDR_SUBSCRIPTS (res).safe_push (subscript);
2146e26d 1518 }
41c7a324 1519
2146e26d 1520 return res;
1521}
1522
87da4f2e 1523/* Frees memory used by the conflict function F. */
1524
1525static void
1526free_conflict_function (conflict_function *f)
1527{
1528 unsigned i;
1529
1530 if (CF_NONTRIVIAL_P (f))
1531 {
1532 for (i = 0; i < f->n; i++)
1533 affine_fn_free (f->fns[i]);
1534 }
1535 free (f);
1536}
1537
1538/* Frees memory used by SUBSCRIPTS. */
1539
1540static void
f1f41a6c 1541free_subscripts (vec<subscript_p> subscripts)
87da4f2e 1542{
1543 unsigned i;
1544 subscript_p s;
1545
f1f41a6c 1546 FOR_EACH_VEC_ELT (subscripts, i, s)
87da4f2e 1547 {
1548 free_conflict_function (s->conflicting_iterations_in_a);
1549 free_conflict_function (s->conflicting_iterations_in_b);
19af51e2 1550 free (s);
87da4f2e 1551 }
f1f41a6c 1552 subscripts.release ();
87da4f2e 1553}
1554
2146e26d 1555/* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1556 description. */
1557
1558static inline void
48e1416a 1559finalize_ddr_dependent (struct data_dependence_relation *ddr,
2146e26d 1560 tree chrec)
1561{
48e1416a 1562 DDR_ARE_DEPENDENT (ddr) = chrec;
87da4f2e 1563 free_subscripts (DDR_SUBSCRIPTS (ddr));
f1f41a6c 1564 DDR_SUBSCRIPTS (ddr).create (0);
2146e26d 1565}
1566
bc3c8ad4 1567/* The dependence relation DDR cannot be represented by a distance
1568 vector. */
1569
1570static inline void
1571non_affine_dependence_relation (struct data_dependence_relation *ddr)
1572{
1573 if (dump_file && (dump_flags & TDF_DETAILS))
1574 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
1575
1576 DDR_AFFINE_P (ddr) = false;
1577}
1578
2146e26d 1579\f
1580
1581/* This section contains the classic Banerjee tests. */
1582
1583/* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1584 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1585
1586static inline bool
7ecb5bb2 1587ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
2146e26d 1588{
1589 return (evolution_function_is_constant_p (chrec_a)
1590 && evolution_function_is_constant_p (chrec_b));
1591}
1592
1593/* Returns true iff CHREC_A and CHREC_B are dependent on an index
1594 variable, i.e., if the SIV (Single Index Variable) test is true. */
1595
1596static bool
7ecb5bb2 1597siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
2146e26d 1598{
1599 if ((evolution_function_is_constant_p (chrec_a)
1600 && evolution_function_is_univariate_p (chrec_b))
1601 || (evolution_function_is_constant_p (chrec_b)
1602 && evolution_function_is_univariate_p (chrec_a)))
1603 return true;
48e1416a 1604
2146e26d 1605 if (evolution_function_is_univariate_p (chrec_a)
1606 && evolution_function_is_univariate_p (chrec_b))
1607 {
1608 switch (TREE_CODE (chrec_a))
1609 {
1610 case POLYNOMIAL_CHREC:
1611 switch (TREE_CODE (chrec_b))
1612 {
1613 case POLYNOMIAL_CHREC:
1614 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
1615 return false;
48e1416a 1616
2146e26d 1617 default:
1618 return true;
1619 }
48e1416a 1620
2146e26d 1621 default:
1622 return true;
1623 }
1624 }
48e1416a 1625
2146e26d 1626 return false;
1627}
1628
87da4f2e 1629/* Creates a conflict function with N dimensions. The affine functions
1630 in each dimension follow. */
1631
1632static conflict_function *
1633conflict_fn (unsigned n, ...)
1634{
1635 unsigned i;
1636 conflict_function *ret = XCNEW (conflict_function);
1637 va_list ap;
1638
a86c75a6 1639 gcc_assert (0 < n && n <= MAX_DIM);
87da4f2e 1640 va_start(ap, n);
48e1416a 1641
87da4f2e 1642 ret->n = n;
1643 for (i = 0; i < n; i++)
1644 ret->fns[i] = va_arg (ap, affine_fn);
1645 va_end(ap);
1646
1647 return ret;
1648}
1649
1650/* Returns constant affine function with value CST. */
1651
1652static affine_fn
1653affine_fn_cst (tree cst)
1654{
f1f41a6c 1655 affine_fn fn;
1656 fn.create (1);
1657 fn.quick_push (cst);
87da4f2e 1658 return fn;
1659}
1660
1661/* Returns affine function with single variable, CST + COEF * x_DIM. */
1662
1663static affine_fn
1664affine_fn_univar (tree cst, unsigned dim, tree coef)
1665{
f1f41a6c 1666 affine_fn fn;
1667 fn.create (dim + 1);
87da4f2e 1668 unsigned i;
1669
1670 gcc_assert (dim > 0);
f1f41a6c 1671 fn.quick_push (cst);
87da4f2e 1672 for (i = 1; i < dim; i++)
f1f41a6c 1673 fn.quick_push (integer_zero_node);
1674 fn.quick_push (coef);
87da4f2e 1675 return fn;
1676}
1677
2146e26d 1678/* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1679 *OVERLAPS_B are initialized to the functions that describe the
1680 relation between the elements accessed twice by CHREC_A and
1681 CHREC_B. For k >= 0, the following property is verified:
1682
1683 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1684
48e1416a 1685static void
1686analyze_ziv_subscript (tree chrec_a,
1687 tree chrec_b,
87da4f2e 1688 conflict_function **overlaps_a,
48e1416a 1689 conflict_function **overlaps_b,
bc3c8ad4 1690 tree *last_conflicts)
2146e26d 1691{
9a3dad1c 1692 tree type, difference;
6b421feb 1693 dependence_stats.num_ziv++;
48e1416a 1694
2146e26d 1695 if (dump_file && (dump_flags & TDF_DETAILS))
1696 fprintf (dump_file, "(analyze_ziv_subscript \n");
9a3dad1c 1697
1698 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
75a70cf9 1699 chrec_a = chrec_convert (type, chrec_a, NULL);
1700 chrec_b = chrec_convert (type, chrec_b, NULL);
9a3dad1c 1701 difference = chrec_fold_minus (type, chrec_a, chrec_b);
48e1416a 1702
2146e26d 1703 switch (TREE_CODE (difference))
1704 {
1705 case INTEGER_CST:
1706 if (integer_zerop (difference))
1707 {
1708 /* The difference is equal to zero: the accessed index
1709 overlaps for each iteration in the loop. */
87da4f2e 1710 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1711 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
bc3c8ad4 1712 *last_conflicts = chrec_dont_know;
6b421feb 1713 dependence_stats.num_ziv_dependent++;
2146e26d 1714 }
1715 else
1716 {
1717 /* The accesses do not overlap. */
87da4f2e 1718 *overlaps_a = conflict_fn_no_dependence ();
1719 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 1720 *last_conflicts = integer_zero_node;
6b421feb 1721 dependence_stats.num_ziv_independent++;
2146e26d 1722 }
1723 break;
48e1416a 1724
2146e26d 1725 default:
48e1416a 1726 /* We're not sure whether the indexes overlap. For the moment,
2146e26d 1727 conservatively answer "don't know". */
6b421feb 1728 if (dump_file && (dump_flags & TDF_DETAILS))
1729 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
1730
87da4f2e 1731 *overlaps_a = conflict_fn_not_known ();
1732 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 1733 *last_conflicts = chrec_dont_know;
6b421feb 1734 dependence_stats.num_ziv_unimplemented++;
2146e26d 1735 break;
1736 }
48e1416a 1737
2146e26d 1738 if (dump_file && (dump_flags & TDF_DETAILS))
1739 fprintf (dump_file, ")\n");
1740}
1741
8fe79ba5 1742/* Similar to max_stmt_executions_int, but returns the bound as a tree,
d500fef3 1743 and only if it fits to the int type. If this is not the case, or the
8fe79ba5 1744 bound on the number of iterations of LOOP could not be derived, returns
d500fef3 1745 chrec_dont_know. */
1746
1747static tree
8fe79ba5 1748max_stmt_executions_tree (struct loop *loop)
d500fef3 1749{
1750 double_int nit;
d500fef3 1751
fee017b3 1752 if (!max_stmt_executions (loop, &nit))
d500fef3 1753 return chrec_dont_know;
1754
7d30f431 1755 if (!double_int_fits_to_tree_p (unsigned_type_node, nit))
d500fef3 1756 return chrec_dont_know;
1757
7d30f431 1758 return double_int_to_tree (unsigned_type_node, nit);
d500fef3 1759}
1760
06a9142d 1761/* Determine whether the CHREC is always positive/negative. If the expression
1762 cannot be statically analyzed, return false, otherwise set the answer into
1763 VALUE. */
1764
1765static bool
1766chrec_is_positive (tree chrec, bool *value)
1767{
1768 bool value0, value1, value2;
1769 tree end_value, nb_iter;
1770
1771 switch (TREE_CODE (chrec))
1772 {
1773 case POLYNOMIAL_CHREC:
1774 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
1775 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
1776 return false;
1777
1778 /* FIXME -- overflows. */
1779 if (value0 == value1)
1780 {
1781 *value = value0;
1782 return true;
1783 }
1784
1785 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1786 and the proof consists in showing that the sign never
1787 changes during the execution of the loop, from 0 to
1788 loop->nb_iterations. */
1789 if (!evolution_function_is_affine_p (chrec))
1790 return false;
1791
1792 nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
1793 if (chrec_contains_undetermined (nb_iter))
1794 return false;
1795
1796#if 0
1797 /* TODO -- If the test is after the exit, we may decrease the number of
1798 iterations by one. */
1799 if (after_exit)
1800 nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
1801#endif
1802
1803 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
1804
1805 if (!chrec_is_positive (end_value, &value2))
1806 return false;
1807
1808 *value = value0;
1809 return value0 == value1;
1810
1811 case INTEGER_CST:
1812 switch (tree_int_cst_sgn (chrec))
1813 {
1814 case -1:
1815 *value = false;
1816 break;
1817 case 1:
1818 *value = true;
1819 break;
1820 default:
1821 return false;
1822 }
1823 return true;
1824
1825 default:
1826 return false;
1827 }
1828}
1829
1830
2146e26d 1831/* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1832 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1833 *OVERLAPS_B are initialized to the functions that describe the
1834 relation between the elements accessed twice by CHREC_A and
1835 CHREC_B. For k >= 0, the following property is verified:
1836
1837 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1838
1839static void
48e1416a 1840analyze_siv_subscript_cst_affine (tree chrec_a,
2146e26d 1841 tree chrec_b,
48e1416a 1842 conflict_function **overlaps_a,
1843 conflict_function **overlaps_b,
bc3c8ad4 1844 tree *last_conflicts)
2146e26d 1845{
1846 bool value0, value1, value2;
9a3dad1c 1847 tree type, difference, tmp;
f84a688a 1848
9a3dad1c 1849 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
75a70cf9 1850 chrec_a = chrec_convert (type, chrec_a, NULL);
1851 chrec_b = chrec_convert (type, chrec_b, NULL);
9a3dad1c 1852 difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
48e1416a 1853
06a9142d 1854 /* Special case overlap in the first iteration. */
1855 if (integer_zerop (difference))
1856 {
1857 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1858 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1859 *last_conflicts = integer_one_node;
1860 return;
1861 }
1862
2146e26d 1863 if (!chrec_is_positive (initial_condition (difference), &value0))
1864 {
6b421feb 1865 if (dump_file && (dump_flags & TDF_DETAILS))
48e1416a 1866 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
6b421feb 1867
1868 dependence_stats.num_siv_unimplemented++;
87da4f2e 1869 *overlaps_a = conflict_fn_not_known ();
1870 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 1871 *last_conflicts = chrec_dont_know;
2146e26d 1872 return;
1873 }
1874 else
1875 {
1876 if (value0 == false)
1877 {
1878 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
1879 {
6b421feb 1880 if (dump_file && (dump_flags & TDF_DETAILS))
1881 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1882
87da4f2e 1883 *overlaps_a = conflict_fn_not_known ();
48e1416a 1884 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 1885 *last_conflicts = chrec_dont_know;
6b421feb 1886 dependence_stats.num_siv_unimplemented++;
2146e26d 1887 return;
1888 }
1889 else
1890 {
1891 if (value1 == true)
1892 {
48e1416a 1893 /* Example:
2146e26d 1894 chrec_a = 12
1895 chrec_b = {10, +, 1}
1896 */
48e1416a 1897
e7be49a3 1898 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2146e26d 1899 {
d500fef3 1900 HOST_WIDE_INT numiter;
1901 struct loop *loop = get_chrec_loop (chrec_b);
b6d41046 1902
87da4f2e 1903 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
9a3dad1c 1904 tmp = fold_build2 (EXACT_DIV_EXPR, type,
1905 fold_build1 (ABS_EXPR, type, difference),
87da4f2e 1906 CHREC_RIGHT (chrec_b));
1907 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
bc3c8ad4 1908 *last_conflicts = integer_one_node;
48e1416a 1909
b6d41046 1910
1911 /* Perform weak-zero siv test to see if overlap is
1912 outside the loop bounds. */
fee017b3 1913 numiter = max_stmt_executions_int (loop);
b6d41046 1914
d500fef3 1915 if (numiter >= 0
1916 && compare_tree_int (tmp, numiter) > 0)
b6d41046 1917 {
87da4f2e 1918 free_conflict_function (*overlaps_a);
1919 free_conflict_function (*overlaps_b);
1920 *overlaps_a = conflict_fn_no_dependence ();
1921 *overlaps_b = conflict_fn_no_dependence ();
b6d41046 1922 *last_conflicts = integer_zero_node;
6b421feb 1923 dependence_stats.num_siv_independent++;
b6d41046 1924 return;
48e1416a 1925 }
6b421feb 1926 dependence_stats.num_siv_dependent++;
2146e26d 1927 return;
1928 }
48e1416a 1929
e7be49a3 1930 /* When the step does not divide the difference, there are
2146e26d 1931 no overlaps. */
1932 else
1933 {
87da4f2e 1934 *overlaps_a = conflict_fn_no_dependence ();
48e1416a 1935 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 1936 *last_conflicts = integer_zero_node;
6b421feb 1937 dependence_stats.num_siv_independent++;
2146e26d 1938 return;
1939 }
1940 }
48e1416a 1941
2146e26d 1942 else
1943 {
48e1416a 1944 /* Example:
2146e26d 1945 chrec_a = 12
1946 chrec_b = {10, +, -1}
48e1416a 1947
2146e26d 1948 In this case, chrec_a will not overlap with chrec_b. */
87da4f2e 1949 *overlaps_a = conflict_fn_no_dependence ();
1950 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 1951 *last_conflicts = integer_zero_node;
6b421feb 1952 dependence_stats.num_siv_independent++;
2146e26d 1953 return;
1954 }
1955 }
1956 }
48e1416a 1957 else
2146e26d 1958 {
1959 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
1960 {
6b421feb 1961 if (dump_file && (dump_flags & TDF_DETAILS))
1962 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1963
87da4f2e 1964 *overlaps_a = conflict_fn_not_known ();
48e1416a 1965 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 1966 *last_conflicts = chrec_dont_know;
6b421feb 1967 dependence_stats.num_siv_unimplemented++;
2146e26d 1968 return;
1969 }
1970 else
1971 {
1972 if (value2 == false)
1973 {
48e1416a 1974 /* Example:
2146e26d 1975 chrec_a = 3
1976 chrec_b = {10, +, -1}
1977 */
e7be49a3 1978 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2146e26d 1979 {
d500fef3 1980 HOST_WIDE_INT numiter;
1981 struct loop *loop = get_chrec_loop (chrec_b);
b6d41046 1982
87da4f2e 1983 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
9a3dad1c 1984 tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
87da4f2e 1985 CHREC_RIGHT (chrec_b));
1986 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
bc3c8ad4 1987 *last_conflicts = integer_one_node;
b6d41046 1988
1989 /* Perform weak-zero siv test to see if overlap is
1990 outside the loop bounds. */
fee017b3 1991 numiter = max_stmt_executions_int (loop);
b6d41046 1992
d500fef3 1993 if (numiter >= 0
1994 && compare_tree_int (tmp, numiter) > 0)
b6d41046 1995 {
87da4f2e 1996 free_conflict_function (*overlaps_a);
1997 free_conflict_function (*overlaps_b);
1998 *overlaps_a = conflict_fn_no_dependence ();
1999 *overlaps_b = conflict_fn_no_dependence ();
b6d41046 2000 *last_conflicts = integer_zero_node;
6b421feb 2001 dependence_stats.num_siv_independent++;
b6d41046 2002 return;
48e1416a 2003 }
6b421feb 2004 dependence_stats.num_siv_dependent++;
2146e26d 2005 return;
2006 }
48e1416a 2007
c4243fe7 2008 /* When the step does not divide the difference, there
2146e26d 2009 are no overlaps. */
2010 else
2011 {
87da4f2e 2012 *overlaps_a = conflict_fn_no_dependence ();
48e1416a 2013 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 2014 *last_conflicts = integer_zero_node;
6b421feb 2015 dependence_stats.num_siv_independent++;
2146e26d 2016 return;
2017 }
2018 }
2019 else
2020 {
48e1416a 2021 /* Example:
2022 chrec_a = 3
2146e26d 2023 chrec_b = {4, +, 1}
48e1416a 2024
2146e26d 2025 In this case, chrec_a will not overlap with chrec_b. */
87da4f2e 2026 *overlaps_a = conflict_fn_no_dependence ();
2027 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 2028 *last_conflicts = integer_zero_node;
6b421feb 2029 dependence_stats.num_siv_independent++;
2146e26d 2030 return;
2031 }
2032 }
2033 }
2034 }
2035}
2036
fbd828d4 2037/* Helper recursive function for initializing the matrix A. Returns
bc3c8ad4 2038 the initial value of CHREC. */
2146e26d 2039
8ceddf57 2040static tree
bc3c8ad4 2041initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2042{
2043 gcc_assert (chrec);
2044
8ceddf57 2045 switch (TREE_CODE (chrec))
2046 {
2047 case POLYNOMIAL_CHREC:
2048 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST);
2049
2050 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2051 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2052
2053 case PLUS_EXPR:
2054 case MULT_EXPR:
2055 case MINUS_EXPR:
2056 {
2057 tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2058 tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
2059
2060 return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
2061 }
2062
2063 case NOP_EXPR:
2064 {
2065 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
75a70cf9 2066 return chrec_convert (chrec_type (chrec), op, NULL);
8ceddf57 2067 }
2068
b6eab06c 2069 case BIT_NOT_EXPR:
2070 {
2071 /* Handle ~X as -1 - X. */
2072 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2073 return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
2074 build_int_cst (TREE_TYPE (chrec), -1), op);
2075 }
2076
8ceddf57 2077 case INTEGER_CST:
2078 return chrec;
c782188f 2079
8ceddf57 2080 default:
2081 gcc_unreachable ();
2082 return NULL_TREE;
2083 }
bc3c8ad4 2084}
2085
2086#define FLOOR_DIV(x,y) ((x) / (y))
2087
48e1416a 2088/* Solves the special case of the Diophantine equation:
bc3c8ad4 2089 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2090
2091 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2092 number of iterations that loops X and Y run. The overlaps will be
2093 constructed as evolutions in dimension DIM. */
2146e26d 2094
2095static void
48e1416a 2096compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
87da4f2e 2097 affine_fn *overlaps_a,
48e1416a 2098 affine_fn *overlaps_b,
bc3c8ad4 2099 tree *last_conflicts, int dim)
2100{
2101 if (((step_a > 0 && step_b > 0)
2102 || (step_a < 0 && step_b < 0)))
2103 {
2104 int step_overlaps_a, step_overlaps_b;
2105 int gcd_steps_a_b, last_conflict, tau2;
2106
2107 gcd_steps_a_b = gcd (step_a, step_b);
2108 step_overlaps_a = step_b / gcd_steps_a_b;
2109 step_overlaps_b = step_a / gcd_steps_a_b;
2110
e781c550 2111 if (niter > 0)
2112 {
2113 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2114 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2115 last_conflict = tau2;
2116 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2117 }
2118 else
2119 *last_conflicts = chrec_dont_know;
bc3c8ad4 2120
48e1416a 2121 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
87da4f2e 2122 build_int_cst (NULL_TREE,
2123 step_overlaps_a));
48e1416a 2124 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
2125 build_int_cst (NULL_TREE,
87da4f2e 2126 step_overlaps_b));
bc3c8ad4 2127 }
2128
2129 else
2130 {
87da4f2e 2131 *overlaps_a = affine_fn_cst (integer_zero_node);
2132 *overlaps_b = affine_fn_cst (integer_zero_node);
bc3c8ad4 2133 *last_conflicts = integer_zero_node;
2134 }
2135}
2136
bc3c8ad4 2137/* Solves the special case of a Diophantine equation where CHREC_A is
2138 an affine bivariate function, and CHREC_B is an affine univariate
48e1416a 2139 function. For example,
bc3c8ad4 2140
2141 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
48e1416a 2142
2143 has the following overlapping functions:
bc3c8ad4 2144
2145 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2146 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2147 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2148
0975351b 2149 FORNOW: This is a specialized implementation for a case occurring in
bc3c8ad4 2150 a common benchmark. Implement the general algorithm. */
2151
2152static void
48e1416a 2153compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
87da4f2e 2154 conflict_function **overlaps_a,
48e1416a 2155 conflict_function **overlaps_b,
bc3c8ad4 2156 tree *last_conflicts)
2146e26d 2157{
bc3c8ad4 2158 bool xz_p, yz_p, xyz_p;
2159 int step_x, step_y, step_z;
d500fef3 2160 HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
87da4f2e 2161 affine_fn overlaps_a_xz, overlaps_b_xz;
2162 affine_fn overlaps_a_yz, overlaps_b_yz;
2163 affine_fn overlaps_a_xyz, overlaps_b_xyz;
2164 affine_fn ova1, ova2, ovb;
2165 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
bc3c8ad4 2166
58f77a87 2167 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2168 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2169 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
bc3c8ad4 2170
fee017b3 2171 niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
2172 niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
2173 niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
48e1416a 2174
d500fef3 2175 if (niter_x < 0 || niter_y < 0 || niter_z < 0)
bc3c8ad4 2176 {
6b421feb 2177 if (dump_file && (dump_flags & TDF_DETAILS))
2178 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
48e1416a 2179
87da4f2e 2180 *overlaps_a = conflict_fn_not_known ();
2181 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 2182 *last_conflicts = chrec_dont_know;
2183 return;
2184 }
2185
bc3c8ad4 2186 niter = MIN (niter_x, niter_z);
2187 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2188 &overlaps_a_xz,
2189 &overlaps_b_xz,
2190 &last_conflicts_xz, 1);
2191 niter = MIN (niter_y, niter_z);
2192 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2193 &overlaps_a_yz,
2194 &overlaps_b_yz,
2195 &last_conflicts_yz, 2);
2196 niter = MIN (niter_x, niter_z);
2197 niter = MIN (niter_y, niter);
2198 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2199 &overlaps_a_xyz,
2200 &overlaps_b_xyz,
2201 &last_conflicts_xyz, 3);
2202
2203 xz_p = !integer_zerop (last_conflicts_xz);
2204 yz_p = !integer_zerop (last_conflicts_yz);
2205 xyz_p = !integer_zerop (last_conflicts_xyz);
2206
2207 if (xz_p || yz_p || xyz_p)
2208 {
87da4f2e 2209 ova1 = affine_fn_cst (integer_zero_node);
2210 ova2 = affine_fn_cst (integer_zero_node);
2211 ovb = affine_fn_cst (integer_zero_node);
bc3c8ad4 2212 if (xz_p)
2213 {
87da4f2e 2214 affine_fn t0 = ova1;
2215 affine_fn t2 = ovb;
2216
2217 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
2218 ovb = affine_fn_plus (ovb, overlaps_b_xz);
2219 affine_fn_free (t0);
2220 affine_fn_free (t2);
bc3c8ad4 2221 *last_conflicts = last_conflicts_xz;
2222 }
2223 if (yz_p)
2224 {
87da4f2e 2225 affine_fn t0 = ova2;
2226 affine_fn t2 = ovb;
2227
2228 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
2229 ovb = affine_fn_plus (ovb, overlaps_b_yz);
2230 affine_fn_free (t0);
2231 affine_fn_free (t2);
bc3c8ad4 2232 *last_conflicts = last_conflicts_yz;
2233 }
2234 if (xyz_p)
2235 {
87da4f2e 2236 affine_fn t0 = ova1;
2237 affine_fn t2 = ova2;
2238 affine_fn t4 = ovb;
2239
2240 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
2241 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
2242 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
2243 affine_fn_free (t0);
2244 affine_fn_free (t2);
2245 affine_fn_free (t4);
bc3c8ad4 2246 *last_conflicts = last_conflicts_xyz;
2247 }
87da4f2e 2248 *overlaps_a = conflict_fn (2, ova1, ova2);
2249 *overlaps_b = conflict_fn (1, ovb);
bc3c8ad4 2250 }
2251 else
2252 {
87da4f2e 2253 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2254 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
bc3c8ad4 2255 *last_conflicts = integer_zero_node;
2256 }
87da4f2e 2257
2258 affine_fn_free (overlaps_a_xz);
2259 affine_fn_free (overlaps_b_xz);
2260 affine_fn_free (overlaps_a_yz);
2261 affine_fn_free (overlaps_b_yz);
2262 affine_fn_free (overlaps_a_xyz);
2263 affine_fn_free (overlaps_b_xyz);
2146e26d 2264}
2265
e01f9f1f 2266/* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2267
2268static void
2269lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
2270 int size)
2271{
2272 memcpy (vec2, vec1, size * sizeof (*vec1));
2273}
2274
2275/* Copy the elements of M x N matrix MAT1 to MAT2. */
2276
2277static void
2278lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
2279 int m, int n)
2280{
2281 int i;
2282
2283 for (i = 0; i < m; i++)
2284 lambda_vector_copy (mat1[i], mat2[i], n);
2285}
2286
2287/* Store the N x N identity matrix in MAT. */
2288
2289static void
2290lambda_matrix_id (lambda_matrix mat, int size)
2291{
2292 int i, j;
2293
2294 for (i = 0; i < size; i++)
2295 for (j = 0; j < size; j++)
2296 mat[i][j] = (i == j) ? 1 : 0;
2297}
2298
2299/* Return the first nonzero element of vector VEC1 between START and N.
2300 We must have START <= N. Returns N if VEC1 is the zero vector. */
2301
2302static int
2303lambda_vector_first_nz (lambda_vector vec1, int n, int start)
2304{
2305 int j = start;
2306 while (j < n && vec1[j] == 0)
2307 j++;
2308 return j;
2309}
2310
2311/* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2312 R2 = R2 + CONST1 * R1. */
2313
2314static void
2315lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2, int const1)
2316{
2317 int i;
2318
2319 if (const1 == 0)
2320 return;
2321
2322 for (i = 0; i < n; i++)
2323 mat[r2][i] += const1 * mat[r1][i];
2324}
2325
2326/* Swap rows R1 and R2 in matrix MAT. */
2327
2328static void
2329lambda_matrix_row_exchange (lambda_matrix mat, int r1, int r2)
2330{
2331 lambda_vector row;
2332
2333 row = mat[r1];
2334 mat[r1] = mat[r2];
2335 mat[r2] = row;
2336}
2337
2338/* Multiply vector VEC1 of length SIZE by a constant CONST1,
2339 and store the result in VEC2. */
2340
2341static void
2342lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
2343 int size, int const1)
2344{
2345 int i;
2346
2347 if (const1 == 0)
2348 lambda_vector_clear (vec2, size);
2349 else
2350 for (i = 0; i < size; i++)
2351 vec2[i] = const1 * vec1[i];
2352}
2353
2354/* Negate vector VEC1 with length SIZE and store it in VEC2. */
2355
2356static void
2357lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
2358 int size)
2359{
2360 lambda_vector_mult_const (vec1, vec2, size, -1);
2361}
2362
2363/* Negate row R1 of matrix MAT which has N columns. */
2364
2365static void
2366lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
2367{
2368 lambda_vector_negate (mat[r1], mat[r1], n);
2369}
2370
2371/* Return true if two vectors are equal. */
2372
2373static bool
2374lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
2375{
2376 int i;
2377 for (i = 0; i < size; i++)
2378 if (vec1[i] != vec2[i])
2379 return false;
2380 return true;
2381}
2382
2383/* Given an M x N integer matrix A, this function determines an M x
2384 M unimodular matrix U, and an M x N echelon matrix S such that
2385 "U.A = S". This decomposition is also known as "right Hermite".
2386
2387 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2388 Restructuring Compilers" Utpal Banerjee. */
2389
2390static void
2391lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
2392 lambda_matrix S, lambda_matrix U)
2393{
2394 int i, j, i0 = 0;
2395
2396 lambda_matrix_copy (A, S, m, n);
2397 lambda_matrix_id (U, m);
2398
2399 for (j = 0; j < n; j++)
2400 {
2401 if (lambda_vector_first_nz (S[j], m, i0) < m)
2402 {
2403 ++i0;
2404 for (i = m - 1; i >= i0; i--)
2405 {
2406 while (S[i][j] != 0)
2407 {
2408 int sigma, factor, a, b;
2409
2410 a = S[i-1][j];
2411 b = S[i][j];
2412 sigma = (a * b < 0) ? -1: 1;
2413 a = abs (a);
2414 b = abs (b);
2415 factor = sigma * (a / b);
2416
2417 lambda_matrix_row_add (S, n, i, i-1, -factor);
2418 lambda_matrix_row_exchange (S, i, i-1);
2419
2420 lambda_matrix_row_add (U, m, i, i-1, -factor);
2421 lambda_matrix_row_exchange (U, i, i-1);
2422 }
2423 }
2424 }
2425 }
2426}
2427
2146e26d 2428/* Determines the overlapping elements due to accesses CHREC_A and
6b421feb 2429 CHREC_B, that are affine functions. This function cannot handle
2430 symbolic evolution functions, ie. when initial conditions are
2431 parameters, because it uses lambda matrices of integers. */
2146e26d 2432
2433static void
48e1416a 2434analyze_subscript_affine_affine (tree chrec_a,
2146e26d 2435 tree chrec_b,
48e1416a 2436 conflict_function **overlaps_a,
2437 conflict_function **overlaps_b,
bc3c8ad4 2438 tree *last_conflicts)
2146e26d 2439{
bc3c8ad4 2440 unsigned nb_vars_a, nb_vars_b, dim;
4ee81b10 2441 HOST_WIDE_INT init_a, init_b, gamma, gcd_alpha_beta;
bc3c8ad4 2442 lambda_matrix A, U, S;
1e33ad50 2443 struct obstack scratch_obstack;
bc3c8ad4 2444
f84a688a 2445 if (eq_evolutions_p (chrec_a, chrec_b))
b6d41046 2446 {
f84a688a 2447 /* The accessed index overlaps for each iteration in the
2448 loop. */
87da4f2e 2449 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2450 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
b6d41046 2451 *last_conflicts = chrec_dont_know;
2452 return;
2453 }
2146e26d 2454 if (dump_file && (dump_flags & TDF_DETAILS))
2455 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
48e1416a 2456
2146e26d 2457 /* For determining the initial intersection, we have to solve a
2458 Diophantine equation. This is the most time consuming part.
48e1416a 2459
2146e26d 2460 For answering to the question: "Is there a dependence?" we have
2461 to prove that there exists a solution to the Diophantine
2462 equation, and that the solution is in the iteration domain,
5c9dae64 2463 i.e. the solution is positive or zero, and that the solution
2146e26d 2464 happens before the upper bound loop.nb_iterations. Otherwise
2465 there is no dependence. This function outputs a description of
2466 the iterations that hold the intersections. */
2467
bc3c8ad4 2468 nb_vars_a = nb_vars_in_chrec (chrec_a);
2469 nb_vars_b = nb_vars_in_chrec (chrec_b);
2470
1e33ad50 2471 gcc_obstack_init (&scratch_obstack);
2472
bc3c8ad4 2473 dim = nb_vars_a + nb_vars_b;
1e33ad50 2474 U = lambda_matrix_new (dim, dim, &scratch_obstack);
2475 A = lambda_matrix_new (dim, 1, &scratch_obstack);
2476 S = lambda_matrix_new (dim, 1, &scratch_obstack);
bc3c8ad4 2477
8ceddf57 2478 init_a = int_cst_value (initialize_matrix_A (A, chrec_a, 0, 1));
2479 init_b = int_cst_value (initialize_matrix_A (A, chrec_b, nb_vars_a, -1));
bc3c8ad4 2480 gamma = init_b - init_a;
2481
2482 /* Don't do all the hard work of solving the Diophantine equation
48e1416a 2483 when we already know the solution: for example,
bc3c8ad4 2484 | {3, +, 1}_1
2485 | {3, +, 4}_2
2486 | gamma = 3 - 3 = 0.
48e1416a 2487 Then the first overlap occurs during the first iterations:
bc3c8ad4 2488 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2489 */
2490 if (gamma == 0)
2146e26d 2491 {
bc3c8ad4 2492 if (nb_vars_a == 1 && nb_vars_b == 1)
2146e26d 2493 {
4ee81b10 2494 HOST_WIDE_INT step_a, step_b;
d500fef3 2495 HOST_WIDE_INT niter, niter_a, niter_b;
87da4f2e 2496 affine_fn ova, ovb;
bc3c8ad4 2497
fee017b3 2498 niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
2499 niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
bc3c8ad4 2500 niter = MIN (niter_a, niter_b);
58f77a87 2501 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2502 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
bc3c8ad4 2503
48e1416a 2504 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2505 &ova, &ovb,
bc3c8ad4 2506 last_conflicts, 1);
87da4f2e 2507 *overlaps_a = conflict_fn (1, ova);
2508 *overlaps_b = conflict_fn (1, ovb);
2146e26d 2509 }
bc3c8ad4 2510
2511 else if (nb_vars_a == 2 && nb_vars_b == 1)
2512 compute_overlap_steps_for_affine_1_2
2513 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2514
2515 else if (nb_vars_a == 1 && nb_vars_b == 2)
2516 compute_overlap_steps_for_affine_1_2
2517 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2518
2519 else
2146e26d 2520 {
6b421feb 2521 if (dump_file && (dump_flags & TDF_DETAILS))
2522 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
87da4f2e 2523 *overlaps_a = conflict_fn_not_known ();
2524 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 2525 *last_conflicts = chrec_dont_know;
2146e26d 2526 }
6b421feb 2527 goto end_analyze_subs_aa;
bc3c8ad4 2528 }
2529
2530 /* U.A = S */
2531 lambda_matrix_right_hermite (A, dim, 1, S, U);
2532
2533 if (S[0][0] < 0)
2534 {
2535 S[0][0] *= -1;
2536 lambda_matrix_row_negate (U, dim, 0);
2537 }
2538 gcd_alpha_beta = S[0][0];
2539
b44d1046 2540 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2541 but that is a quite strange case. Instead of ICEing, answer
2542 don't know. */
2543 if (gcd_alpha_beta == 0)
2544 {
87da4f2e 2545 *overlaps_a = conflict_fn_not_known ();
2546 *overlaps_b = conflict_fn_not_known ();
b44d1046 2547 *last_conflicts = chrec_dont_know;
2548 goto end_analyze_subs_aa;
2549 }
2550
bc3c8ad4 2551 /* The classic "gcd-test". */
2552 if (!int_divides_p (gcd_alpha_beta, gamma))
2553 {
2554 /* The "gcd-test" has determined that there is no integer
2555 solution, i.e. there is no dependence. */
87da4f2e 2556 *overlaps_a = conflict_fn_no_dependence ();
2557 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 2558 *last_conflicts = integer_zero_node;
2559 }
2560
2561 /* Both access functions are univariate. This includes SIV and MIV cases. */
2562 else if (nb_vars_a == 1 && nb_vars_b == 1)
2563 {
2564 /* Both functions should have the same evolution sign. */
2565 if (((A[0][0] > 0 && -A[1][0] > 0)
2566 || (A[0][0] < 0 && -A[1][0] < 0)))
2146e26d 2567 {
2568 /* The solutions are given by:
48e1416a 2569 |
bc3c8ad4 2570 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2571 | [u21 u22] [y0]
48e1416a 2572
2146e26d 2573 For a given integer t. Using the following variables,
48e1416a 2574
2146e26d 2575 | i0 = u11 * gamma / gcd_alpha_beta
2576 | j0 = u12 * gamma / gcd_alpha_beta
2577 | i1 = u21
2578 | j1 = u22
48e1416a 2579
2146e26d 2580 the solutions are:
48e1416a 2581
2582 | x0 = i0 + i1 * t,
bc3c8ad4 2583 | y0 = j0 + j1 * t. */
e781c550 2584 HOST_WIDE_INT i0, j0, i1, j1;
bc3c8ad4 2585
2586 i0 = U[0][0] * gamma / gcd_alpha_beta;
2587 j0 = U[0][1] * gamma / gcd_alpha_beta;
2588 i1 = U[1][0];
2589 j1 = U[1][1];
2590
2591 if ((i1 == 0 && i0 < 0)
2592 || (j1 == 0 && j0 < 0))
2146e26d 2593 {
48e1416a 2594 /* There is no solution.
2595 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2596 falls in here, but for the moment we don't look at the
2146e26d 2597 upper bound of the iteration domain. */
87da4f2e 2598 *overlaps_a = conflict_fn_no_dependence ();
2599 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 2600 *last_conflicts = integer_zero_node;
e781c550 2601 goto end_analyze_subs_aa;
bc3c8ad4 2602 }
2603
e781c550 2604 if (i1 > 0 && j1 > 0)
2146e26d 2605 {
fee017b3 2606 HOST_WIDE_INT niter_a
2607 = max_stmt_executions_int (get_chrec_loop (chrec_a));
2608 HOST_WIDE_INT niter_b
2609 = max_stmt_executions_int (get_chrec_loop (chrec_b));
e781c550 2610 HOST_WIDE_INT niter = MIN (niter_a, niter_b);
2611
2612 /* (X0, Y0) is a solution of the Diophantine equation:
2613 "chrec_a (X0) = chrec_b (Y0)". */
2614 HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
2615 CEIL (-j0, j1));
2616 HOST_WIDE_INT x0 = i1 * tau1 + i0;
2617 HOST_WIDE_INT y0 = j1 * tau1 + j0;
2618
2619 /* (X1, Y1) is the smallest positive solution of the eq
2620 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2621 first conflict occurs. */
2622 HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
2623 HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
2624 HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
2625
2626 if (niter > 0)
2146e26d 2627 {
e781c550 2628 HOST_WIDE_INT tau2 = MIN (FLOOR_DIV (niter - i0, i1),
2629 FLOOR_DIV (niter - j0, j1));
2630 HOST_WIDE_INT last_conflict = tau2 - (x1 - i0)/i1;
bc3c8ad4 2631
e781c550 2632 /* If the overlap occurs outside of the bounds of the
2633 loop, there is no dependence. */
63ca8934 2634 if (x1 >= niter || y1 >= niter)
2146e26d 2635 {
e781c550 2636 *overlaps_a = conflict_fn_no_dependence ();
2637 *overlaps_b = conflict_fn_no_dependence ();
2638 *last_conflicts = integer_zero_node;
2639 goto end_analyze_subs_aa;
2146e26d 2640 }
2641 else
e781c550 2642 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2146e26d 2643 }
2146e26d 2644 else
e781c550 2645 *last_conflicts = chrec_dont_know;
2646
2647 *overlaps_a
2648 = conflict_fn (1,
2649 affine_fn_univar (build_int_cst (NULL_TREE, x1),
2650 1,
2651 build_int_cst (NULL_TREE, i1)));
2652 *overlaps_b
2653 = conflict_fn (1,
2654 affine_fn_univar (build_int_cst (NULL_TREE, y1),
2655 1,
2656 build_int_cst (NULL_TREE, j1)));
2657 }
2658 else
2659 {
2660 /* FIXME: For the moment, the upper bound of the
2661 iteration domain for i and j is not checked. */
2662 if (dump_file && (dump_flags & TDF_DETAILS))
2663 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2664 *overlaps_a = conflict_fn_not_known ();
2665 *overlaps_b = conflict_fn_not_known ();
2666 *last_conflicts = chrec_dont_know;
2146e26d 2667 }
2668 }
bc3c8ad4 2669 else
2670 {
6b421feb 2671 if (dump_file && (dump_flags & TDF_DETAILS))
2672 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
87da4f2e 2673 *overlaps_a = conflict_fn_not_known ();
2674 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 2675 *last_conflicts = chrec_dont_know;
2676 }
2146e26d 2677 }
2146e26d 2678 else
2679 {
6b421feb 2680 if (dump_file && (dump_flags & TDF_DETAILS))
2681 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
87da4f2e 2682 *overlaps_a = conflict_fn_not_known ();
2683 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 2684 *last_conflicts = chrec_dont_know;
2146e26d 2685 }
bc3c8ad4 2686
48e1416a 2687end_analyze_subs_aa:
1e33ad50 2688 obstack_free (&scratch_obstack, NULL);
2146e26d 2689 if (dump_file && (dump_flags & TDF_DETAILS))
2690 {
2691 fprintf (dump_file, " (overlaps_a = ");
87da4f2e 2692 dump_conflict_function (dump_file, *overlaps_a);
2146e26d 2693 fprintf (dump_file, ")\n (overlaps_b = ");
87da4f2e 2694 dump_conflict_function (dump_file, *overlaps_b);
984cebc1 2695 fprintf (dump_file, "))\n");
2146e26d 2696 }
6b421feb 2697}
2698
2699/* Returns true when analyze_subscript_affine_affine can be used for
2700 determining the dependence relation between chrec_a and chrec_b,
2701 that contain symbols. This function modifies chrec_a and chrec_b
2702 such that the analysis result is the same, and such that they don't
48e1416a 2703 contain symbols, and then can safely be passed to the analyzer.
6b421feb 2704
2705 Example: The analysis of the following tuples of evolutions produce
2706 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2707 vs. {0, +, 1}_1
48e1416a 2708
6b421feb 2709 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2710 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2711*/
2712
2713static bool
2714can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
2715{
b92381b6 2716 tree diff, type, left_a, left_b, right_b;
6b421feb 2717
2718 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
2719 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
2720 /* FIXME: For the moment not handled. Might be refined later. */
2721 return false;
2722
b92381b6 2723 type = chrec_type (*chrec_a);
2724 left_a = CHREC_LEFT (*chrec_a);
75a70cf9 2725 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
b92381b6 2726 diff = chrec_fold_minus (type, left_a, left_b);
2727
6b421feb 2728 if (!evolution_function_is_constant_p (diff))
2729 return false;
2730
2146e26d 2731 if (dump_file && (dump_flags & TDF_DETAILS))
6b421feb 2732 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
2733
48e1416a 2734 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
6b421feb 2735 diff, CHREC_RIGHT (*chrec_a));
75a70cf9 2736 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
6b421feb 2737 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
7c010d6a 2738 build_int_cst (type, 0),
b92381b6 2739 right_b);
6b421feb 2740 return true;
2146e26d 2741}
2742
2743/* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2744 *OVERLAPS_B are initialized to the functions that describe the
2745 relation between the elements accessed twice by CHREC_A and
2746 CHREC_B. For k >= 0, the following property is verified:
2747
2748 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2749
2750static void
48e1416a 2751analyze_siv_subscript (tree chrec_a,
2146e26d 2752 tree chrec_b,
48e1416a 2753 conflict_function **overlaps_a,
2754 conflict_function **overlaps_b,
8ceddf57 2755 tree *last_conflicts,
2756 int loop_nest_num)
2146e26d 2757{
6b421feb 2758 dependence_stats.num_siv++;
48e1416a 2759
2146e26d 2760 if (dump_file && (dump_flags & TDF_DETAILS))
2761 fprintf (dump_file, "(analyze_siv_subscript \n");
48e1416a 2762
2146e26d 2763 if (evolution_function_is_constant_p (chrec_a)
8ceddf57 2764 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
48e1416a 2765 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
bc3c8ad4 2766 overlaps_a, overlaps_b, last_conflicts);
48e1416a 2767
8ceddf57 2768 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2146e26d 2769 && evolution_function_is_constant_p (chrec_b))
48e1416a 2770 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
bc3c8ad4 2771 overlaps_b, overlaps_a, last_conflicts);
48e1416a 2772
8ceddf57 2773 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2774 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
6b421feb 2775 {
2776 if (!chrec_contains_symbols (chrec_a)
2777 && !chrec_contains_symbols (chrec_b))
2778 {
48e1416a 2779 analyze_subscript_affine_affine (chrec_a, chrec_b,
2780 overlaps_a, overlaps_b,
6b421feb 2781 last_conflicts);
2782
87da4f2e 2783 if (CF_NOT_KNOWN_P (*overlaps_a)
2784 || CF_NOT_KNOWN_P (*overlaps_b))
6b421feb 2785 dependence_stats.num_siv_unimplemented++;
87da4f2e 2786 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2787 || CF_NO_DEPENDENCE_P (*overlaps_b))
6b421feb 2788 dependence_stats.num_siv_independent++;
2789 else
2790 dependence_stats.num_siv_dependent++;
2791 }
48e1416a 2792 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
6b421feb 2793 &chrec_b))
2794 {
48e1416a 2795 analyze_subscript_affine_affine (chrec_a, chrec_b,
2796 overlaps_a, overlaps_b,
6b421feb 2797 last_conflicts);
6b421feb 2798
87da4f2e 2799 if (CF_NOT_KNOWN_P (*overlaps_a)
2800 || CF_NOT_KNOWN_P (*overlaps_b))
6b421feb 2801 dependence_stats.num_siv_unimplemented++;
87da4f2e 2802 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2803 || CF_NO_DEPENDENCE_P (*overlaps_b))
6b421feb 2804 dependence_stats.num_siv_independent++;
2805 else
2806 dependence_stats.num_siv_dependent++;
2807 }
2808 else
2809 goto siv_subscript_dontknow;
2810 }
2811
2146e26d 2812 else
2813 {
6b421feb 2814 siv_subscript_dontknow:;
2815 if (dump_file && (dump_flags & TDF_DETAILS))
984cebc1 2816 fprintf (dump_file, " siv test failed: unimplemented");
87da4f2e 2817 *overlaps_a = conflict_fn_not_known ();
2818 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 2819 *last_conflicts = chrec_dont_know;
6b421feb 2820 dependence_stats.num_siv_unimplemented++;
2146e26d 2821 }
48e1416a 2822
2146e26d 2823 if (dump_file && (dump_flags & TDF_DETAILS))
2824 fprintf (dump_file, ")\n");
2825}
2826
cabedb72 2827/* Returns false if we can prove that the greatest common divisor of the steps
2828 of CHREC does not divide CST, false otherwise. */
2146e26d 2829
2830static bool
7ecb5bb2 2831gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
2146e26d 2832{
cabedb72 2833 HOST_WIDE_INT cd = 0, val;
2834 tree step;
6b421feb 2835
cabedb72 2836 if (!host_integerp (cst, 0))
2837 return true;
2838 val = tree_low_cst (cst, 0);
2839
2840 while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
2841 {
2842 step = CHREC_RIGHT (chrec);
2843 if (!host_integerp (step, 0))
2844 return true;
2845 cd = gcd (cd, tree_low_cst (step, 0));
2846 chrec = CHREC_LEFT (chrec);
2146e26d 2847 }
cabedb72 2848
2849 return val % cd == 0;
2146e26d 2850}
2851
8c7b1d8e 2852/* Analyze a MIV (Multiple Index Variable) subscript with respect to
2853 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2854 functions that describe the relation between the elements accessed
2855 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2856 is verified:
2146e26d 2857
2858 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2859
2860static void
48e1416a 2861analyze_miv_subscript (tree chrec_a,
2862 tree chrec_b,
2863 conflict_function **overlaps_a,
2864 conflict_function **overlaps_b,
8c7b1d8e 2865 tree *last_conflicts,
2866 struct loop *loop_nest)
2146e26d 2867{
9a3dad1c 2868 tree type, difference;
2869
6b421feb 2870 dependence_stats.num_miv++;
2146e26d 2871 if (dump_file && (dump_flags & TDF_DETAILS))
2872 fprintf (dump_file, "(analyze_miv_subscript \n");
f84a688a 2873
9a3dad1c 2874 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
75a70cf9 2875 chrec_a = chrec_convert (type, chrec_a, NULL);
2876 chrec_b = chrec_convert (type, chrec_b, NULL);
9a3dad1c 2877 difference = chrec_fold_minus (type, chrec_a, chrec_b);
48e1416a 2878
f84a688a 2879 if (eq_evolutions_p (chrec_a, chrec_b))
2146e26d 2880 {
2881 /* Access functions are the same: all the elements are accessed
2882 in the same order. */
87da4f2e 2883 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2884 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
8fe79ba5 2885 *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
6b421feb 2886 dependence_stats.num_miv_dependent++;
2146e26d 2887 }
48e1416a 2888
2146e26d 2889 else if (evolution_function_is_constant_p (difference)
2890 /* For the moment, the following is verified:
8c7b1d8e 2891 evolution_function_is_affine_multivariate_p (chrec_a,
2892 loop_nest->num) */
cabedb72 2893 && !gcd_of_steps_may_divide_p (chrec_a, difference))
2146e26d 2894 {
2895 /* testsuite/.../ssa-chrec-33.c
48e1416a 2896 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2897
cabedb72 2898 The difference is 1, and all the evolution steps are multiples
2899 of 2, consequently there are no overlapping elements. */
87da4f2e 2900 *overlaps_a = conflict_fn_no_dependence ();
2901 *overlaps_b = conflict_fn_no_dependence ();
bc3c8ad4 2902 *last_conflicts = integer_zero_node;
6b421feb 2903 dependence_stats.num_miv_independent++;
2146e26d 2904 }
48e1416a 2905
8c7b1d8e 2906 else if (evolution_function_is_affine_multivariate_p (chrec_a, loop_nest->num)
6b421feb 2907 && !chrec_contains_symbols (chrec_a)
8c7b1d8e 2908 && evolution_function_is_affine_multivariate_p (chrec_b, loop_nest->num)
6b421feb 2909 && !chrec_contains_symbols (chrec_b))
2146e26d 2910 {
2911 /* testsuite/.../ssa-chrec-35.c
2912 {0, +, 1}_2 vs. {0, +, 1}_3
2913 the overlapping elements are respectively located at iterations:
48e1416a 2914 {0, +, 1}_x and {0, +, 1}_x,
2915 in other words, we have the equality:
bc3c8ad4 2916 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
48e1416a 2917
2918 Other examples:
2919 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
bc3c8ad4 2920 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2921
48e1416a 2922 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
bc3c8ad4 2923 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2146e26d 2924 */
48e1416a 2925 analyze_subscript_affine_affine (chrec_a, chrec_b,
bc3c8ad4 2926 overlaps_a, overlaps_b, last_conflicts);
6b421feb 2927
87da4f2e 2928 if (CF_NOT_KNOWN_P (*overlaps_a)
2929 || CF_NOT_KNOWN_P (*overlaps_b))
6b421feb 2930 dependence_stats.num_miv_unimplemented++;
87da4f2e 2931 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2932 || CF_NO_DEPENDENCE_P (*overlaps_b))
6b421feb 2933 dependence_stats.num_miv_independent++;
2934 else
2935 dependence_stats.num_miv_dependent++;
2146e26d 2936 }
48e1416a 2937
2146e26d 2938 else
2939 {
2940 /* When the analysis is too difficult, answer "don't know". */
6b421feb 2941 if (dump_file && (dump_flags & TDF_DETAILS))
2942 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
2943
87da4f2e 2944 *overlaps_a = conflict_fn_not_known ();
2945 *overlaps_b = conflict_fn_not_known ();
bc3c8ad4 2946 *last_conflicts = chrec_dont_know;
6b421feb 2947 dependence_stats.num_miv_unimplemented++;
2146e26d 2948 }
48e1416a 2949
2146e26d 2950 if (dump_file && (dump_flags & TDF_DETAILS))
2951 fprintf (dump_file, ")\n");
2952}
2953
8c7b1d8e 2954/* Determines the iterations for which CHREC_A is equal to CHREC_B in
2955 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
2956 OVERLAP_ITERATIONS_B are initialized with two functions that
2957 describe the iterations that contain conflicting elements.
48e1416a 2958
2146e26d 2959 Remark: For an integer k >= 0, the following equality is true:
48e1416a 2960
2146e26d 2961 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
2962*/
2963
48e1416a 2964static void
2965analyze_overlapping_iterations (tree chrec_a,
2966 tree chrec_b,
2967 conflict_function **overlap_iterations_a,
2968 conflict_function **overlap_iterations_b,
8c7b1d8e 2969 tree *last_conflicts, struct loop *loop_nest)
2146e26d 2970{
8c7b1d8e 2971 unsigned int lnn = loop_nest->num;
2972
6b421feb 2973 dependence_stats.num_subscript_tests++;
48e1416a 2974
2146e26d 2975 if (dump_file && (dump_flags & TDF_DETAILS))
2976 {
2977 fprintf (dump_file, "(analyze_overlapping_iterations \n");
2978 fprintf (dump_file, " (chrec_a = ");
2979 print_generic_expr (dump_file, chrec_a, 0);
6b421feb 2980 fprintf (dump_file, ")\n (chrec_b = ");
2146e26d 2981 print_generic_expr (dump_file, chrec_b, 0);
2982 fprintf (dump_file, ")\n");
2983 }
6b421feb 2984
2146e26d 2985 if (chrec_a == NULL_TREE
2986 || chrec_b == NULL_TREE
2987 || chrec_contains_undetermined (chrec_a)
6b421feb 2988 || chrec_contains_undetermined (chrec_b))
2146e26d 2989 {
6b421feb 2990 dependence_stats.num_subscript_undetermined++;
48e1416a 2991
87da4f2e 2992 *overlap_iterations_a = conflict_fn_not_known ();
2993 *overlap_iterations_b = conflict_fn_not_known ();
2146e26d 2994 }
6b421feb 2995
48e1416a 2996 /* If they are the same chrec, and are affine, they overlap
6b421feb 2997 on every iteration. */
2998 else if (eq_evolutions_p (chrec_a, chrec_b)
328218c2 2999 && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3000 || operand_equal_p (chrec_a, chrec_b, 0)))
6b421feb 3001 {
3002 dependence_stats.num_same_subscript_function++;
87da4f2e 3003 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3004 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
6b421feb 3005 *last_conflicts = chrec_dont_know;
3006 }
3007
3008 /* If they aren't the same, and aren't affine, we can't do anything
328218c2 3009 yet. */
48e1416a 3010 else if ((chrec_contains_symbols (chrec_a)
6b421feb 3011 || chrec_contains_symbols (chrec_b))
8c7b1d8e 3012 && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3013 || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
6b421feb 3014 {
3015 dependence_stats.num_subscript_undetermined++;
87da4f2e 3016 *overlap_iterations_a = conflict_fn_not_known ();
3017 *overlap_iterations_b = conflict_fn_not_known ();
6b421feb 3018 }
3019
2146e26d 3020 else if (ziv_subscript_p (chrec_a, chrec_b))
48e1416a 3021 analyze_ziv_subscript (chrec_a, chrec_b,
bc3c8ad4 3022 overlap_iterations_a, overlap_iterations_b,
3023 last_conflicts);
48e1416a 3024
2146e26d 3025 else if (siv_subscript_p (chrec_a, chrec_b))
48e1416a 3026 analyze_siv_subscript (chrec_a, chrec_b,
3027 overlap_iterations_a, overlap_iterations_b,
8ceddf57 3028 last_conflicts, lnn);
48e1416a 3029
2146e26d 3030 else
48e1416a 3031 analyze_miv_subscript (chrec_a, chrec_b,
bc3c8ad4 3032 overlap_iterations_a, overlap_iterations_b,
8c7b1d8e 3033 last_conflicts, loop_nest);
48e1416a 3034
2146e26d 3035 if (dump_file && (dump_flags & TDF_DETAILS))
3036 {
3037 fprintf (dump_file, " (overlap_iterations_a = ");
87da4f2e 3038 dump_conflict_function (dump_file, *overlap_iterations_a);
2146e26d 3039 fprintf (dump_file, ")\n (overlap_iterations_b = ");
87da4f2e 3040 dump_conflict_function (dump_file, *overlap_iterations_b);
984cebc1 3041 fprintf (dump_file, "))\n");
2146e26d 3042 }
3043}
3044
b44d1046 3045/* Helper function for uniquely inserting distance vectors. */
2146e26d 3046
b44d1046 3047static void
3048save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
3049{
3050 unsigned i;
3051 lambda_vector v;
2146e26d 3052
f1f41a6c 3053 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, v)
b44d1046 3054 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
3055 return;
2146e26d 3056
f1f41a6c 3057 DDR_DIST_VECTS (ddr).safe_push (dist_v);
b44d1046 3058}
2146e26d 3059
b44d1046 3060/* Helper function for uniquely inserting direction vectors. */
3061
3062static void
3063save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
2146e26d 3064{
3065 unsigned i;
b44d1046 3066 lambda_vector v;
6b421feb 3067
f1f41a6c 3068 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), i, v)
b44d1046 3069 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
3070 return;
3071
f1f41a6c 3072 DDR_DIR_VECTS (ddr).safe_push (dir_v);
b44d1046 3073}
3074
3075/* Add a distance of 1 on all the loops outer than INDEX. If we
3076 haven't yet determined a distance for this outer loop, push a new
3077 distance vector composed of the previous distance, and a distance
3078 of 1 for this outer loop. Example:
3079
3080 | loop_1
3081 | loop_2
3082 | A[10]
3083 | endloop_2
3084 | endloop_1
3085
3086 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3087 save (0, 1), then we have to save (1, 0). */
3088
3089static void
3090add_outer_distances (struct data_dependence_relation *ddr,
3091 lambda_vector dist_v, int index)
3092{
3093 /* For each outer loop where init_v is not set, the accesses are
3094 in dependence of distance 1 in the loop. */
3095 while (--index >= 0)
3096 {
3097 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3098 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3099 save_v[index] = 1;
3100 save_dist_v (ddr, save_v);
3101 }
3102}
3103
3104/* Return false when fail to represent the data dependence as a
3105 distance vector. INIT_B is set to true when a component has been
3106 added to the distance vector DIST_V. INDEX_CARRY is then set to
3107 the index in DIST_V that carries the dependence. */
3108
3109static bool
3110build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
3111 struct data_reference *ddr_a,
3112 struct data_reference *ddr_b,
3113 lambda_vector dist_v, bool *init_b,
3114 int *index_carry)
3115{
3116 unsigned i;
3117 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
6b421feb 3118
6b6f234c 3119 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
2146e26d 3120 {
bc3c8ad4 3121 tree access_fn_a, access_fn_b;
6b6f234c 3122 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
2146e26d 3123
3124 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
bc3c8ad4 3125 {
3126 non_affine_dependence_relation (ddr);
b44d1046 3127 return false;
bc3c8ad4 3128 }
3129
b44d1046 3130 access_fn_a = DR_ACCESS_FN (ddr_a, i);
3131 access_fn_b = DR_ACCESS_FN (ddr_b, i);
2146e26d 3132
48e1416a 3133 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
bc3c8ad4 3134 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
2146e26d 3135 {
b44d1046 3136 int dist, index;
ba3fc4d0 3137 int var_a = CHREC_VARIABLE (access_fn_a);
3138 int var_b = CHREC_VARIABLE (access_fn_b);
b44d1046 3139
ba3fc4d0 3140 if (var_a != var_b
3141 || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
bc3c8ad4 3142 {
3143 non_affine_dependence_relation (ddr);
b44d1046 3144 return false;
bc3c8ad4 3145 }
48e1416a 3146
58f77a87 3147 dist = int_cst_value (SUB_DISTANCE (subscript));
ba3fc4d0 3148 index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
3149 *index_carry = MIN (index, *index_carry);
2146e26d 3150
b44d1046 3151 /* This is the subscript coupling test. If we have already
3152 recorded a distance for this loop (a distance coming from
3153 another subscript), it should be the same. For example,
3154 in the following code, there is no dependence:
3155
2146e26d 3156 | loop i = 0, N, 1
3157 | T[i+1][i] = ...
3158 | ... = T[i][i]
3159 | endloop
b44d1046 3160 */
3161 if (init_v[index] != 0 && dist_v[index] != dist)
2146e26d 3162 {
6b6f234c 3163 finalize_ddr_dependent (ddr, chrec_known);
b44d1046 3164 return false;
2146e26d 3165 }
3166
b44d1046 3167 dist_v[index] = dist;
3168 init_v[index] = 1;
3169 *init_b = true;
3170 }
9c77efff 3171 else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
b44d1046 3172 {
3173 /* This can be for example an affine vs. constant dependence
3174 (T[i] vs. T[3]) that is not an affine dependence and is
3175 not representable as a distance vector. */
3176 non_affine_dependence_relation (ddr);
3177 return false;
2146e26d 3178 }
3179 }
1532ec98 3180
b44d1046 3181 return true;
3182}
1532ec98 3183
2a21a5df 3184/* Return true when the DDR contains only constant access functions. */
3185
3186static bool
7ecb5bb2 3187constant_access_functions (const struct data_dependence_relation *ddr)
2a21a5df 3188{
3189 unsigned i;
3190
3191 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3192 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr), i))
3193 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr), i)))
3194 return false;
3195
3196 return true;
3197}
3198
b44d1046 3199/* Helper function for the case where DDR_A and DDR_B are the same
bfe20447 3200 multivariate access function with a constant step. For an example
3201 see pr34635-1.c. */
bc3c8ad4 3202
b44d1046 3203static void
3204add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
3205{
3206 int x_1, x_2;
3207 tree c_1 = CHREC_LEFT (c_2);
3208 tree c_0 = CHREC_LEFT (c_1);
3209 lambda_vector dist_v;
8fb896c4 3210 int v1, v2, cd;
bc3c8ad4 3211
bb11a0e8 3212 /* Polynomials with more than 2 variables are not handled yet. When
3213 the evolution steps are parameters, it is not possible to
3214 represent the dependence using classical distance vectors. */
3215 if (TREE_CODE (c_0) != INTEGER_CST
3216 || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
3217 || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
3218 {
3219 DDR_AFFINE_P (ddr) = false;
b44d1046 3220 return;
3221 }
1532ec98 3222
b44d1046 3223 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
3224 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
1532ec98 3225
b44d1046 3226 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3227 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
58f77a87 3228 v1 = int_cst_value (CHREC_RIGHT (c_1));
3229 v2 = int_cst_value (CHREC_RIGHT (c_2));
8fb896c4 3230 cd = gcd (v1, v2);
3231 v1 /= cd;
3232 v2 /= cd;
3233
3234 if (v2 < 0)
3235 {
3236 v2 = -v2;
3237 v1 = -v1;
3238 }
3239
3240 dist_v[x_1] = v2;
3241 dist_v[x_2] = -v1;
b44d1046 3242 save_dist_v (ddr, dist_v);
1532ec98 3243
b44d1046 3244 add_outer_distances (ddr, dist_v, x_1);
3245}
1532ec98 3246
b44d1046 3247/* Helper function for the case where DDR_A and DDR_B are the same
3248 access functions. */
ffe944fa 3249
b44d1046 3250static void
3251add_other_self_distances (struct data_dependence_relation *ddr)
3252{
3253 lambda_vector dist_v;
3254 unsigned i;
3255 int index_carry = DDR_NB_LOOPS (ddr);
1532ec98 3256
b44d1046 3257 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
ffe944fa 3258 {
b44d1046 3259 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
1532ec98 3260
b44d1046 3261 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
1532ec98 3262 {
b44d1046 3263 if (!evolution_function_is_univariate_p (access_fun))
3264 {
3265 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
3266 {
3267 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3268 return;
3269 }
3270
bfe20447 3271 access_fun = DR_ACCESS_FN (DDR_A (ddr), 0);
3272
3273 if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
3274 add_multivariate_self_dist (ddr, access_fun);
3275 else
3276 /* The evolution step is not constant: it varies in
3277 the outer loop, so this cannot be represented by a
3278 distance vector. For example in pr34635.c the
3279 evolution is {0, +, {0, +, 4}_1}_2. */
3280 DDR_AFFINE_P (ddr) = false;
3281
b44d1046 3282 return;
3283 }
3284
3285 index_carry = MIN (index_carry,
3286 index_in_loop_nest (CHREC_VARIABLE (access_fun),
3287 DDR_LOOP_NEST (ddr)));
1532ec98 3288 }
ffe944fa 3289 }
3290
b44d1046 3291 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3292 add_outer_distances (ddr, dist_v, index_carry);
2146e26d 3293}
3294
2a21a5df 3295static void
3296insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
3297{
3298 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3299
3300 dist_v[DDR_INNER_LOOP (ddr)] = 1;
3301 save_dist_v (ddr, dist_v);
3302}
3303
3304/* Adds a unit distance vector to DDR when there is a 0 overlap. This
3305 is the case for example when access functions are the same and
3306 equal to a constant, as in:
3307
3308 | loop_1
3309 | A[3] = ...
3310 | ... = A[3]
3311 | endloop_1
3312
3313 in which case the distance vectors are (0) and (1). */
3314
3315static void
3316add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
3317{
3318 unsigned i, j;
3319
3320 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3321 {
3322 subscript_p sub = DDR_SUBSCRIPT (ddr, i);
3323 conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
3324 conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
3325
3326 for (j = 0; j < ca->n; j++)
3327 if (affine_function_zero_p (ca->fns[j]))
3328 {
3329 insert_innermost_unit_dist_vector (ddr);
3330 return;
3331 }
3332
3333 for (j = 0; j < cb->n; j++)
3334 if (affine_function_zero_p (cb->fns[j]))
3335 {
3336 insert_innermost_unit_dist_vector (ddr);
3337 return;
3338 }
3339 }
3340}
3341
b44d1046 3342/* Compute the classic per loop distance vector. DDR is the data
3343 dependence relation to build a vector from. Return false when fail
3344 to represent the data dependence as a distance vector. */
2146e26d 3345
557ef5d8 3346static bool
8c7b1d8e 3347build_classic_dist_vector (struct data_dependence_relation *ddr,
3348 struct loop *loop_nest)
2146e26d 3349{
1532ec98 3350 bool init_b = false;
b44d1046 3351 int index_carry = DDR_NB_LOOPS (ddr);
3352 lambda_vector dist_v;
1532ec98 3353
6b6f234c 3354 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
d534eb36 3355 return false;
b44d1046 3356
3357 if (same_access_functions (ddr))
2146e26d 3358 {
b44d1046 3359 /* Save the 0 vector. */
3360 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3361 save_dist_v (ddr, dist_v);
2146e26d 3362
2a21a5df 3363 if (constant_access_functions (ddr))
3364 add_distance_for_zero_overlaps (ddr);
3365
b44d1046 3366 if (DDR_NB_LOOPS (ddr) > 1)
3367 add_other_self_distances (ddr);
bc3c8ad4 3368
b44d1046 3369 return true;
3370 }
bc3c8ad4 3371
b44d1046 3372 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3373 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3374 dist_v, &init_b, &index_carry))
3375 return false;
bc3c8ad4 3376
b44d1046 3377 /* Save the distance vector if we initialized one. */
3378 if (init_b)
3379 {
3380 /* Verify a basic constraint: classic distance vectors should
3381 always be lexicographically positive.
3382
3383 Data references are collected in the order of execution of
3384 the program, thus for the following loop
3385
3386 | for (i = 1; i < 100; i++)
3387 | for (j = 1; j < 100; j++)
3388 | {
3389 | t = T[j+1][i-1]; // A
3390 | T[j][i] = t + 2; // B
3391 | }
3392
3393 references are collected following the direction of the wind:
3394 A then B. The data dependence tests are performed also
3395 following this order, such that we're looking at the distance
3396 separating the elements accessed by A from the elements later
3397 accessed by B. But in this example, the distance returned by
3398 test_dep (A, B) is lexicographically negative (-1, 1), that
3399 means that the access A occurs later than B with respect to
3400 the outer loop, ie. we're actually looking upwind. In this
3401 case we solve test_dep (B, A) looking downwind to the
3402 lexicographically positive solution, that returns the
3403 distance vector (1, -1). */
3404 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3405 {
3406 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
d534eb36 3407 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3408 loop_nest))
3409 return false;
b44d1046 3410 compute_subscript_distance (ddr);
d534eb36 3411 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3412 save_v, &init_b, &index_carry))
3413 return false;
b44d1046 3414 save_dist_v (ddr, save_v);
0ecb94cf 3415 DDR_REVERSED_P (ddr) = true;
b44d1046 3416
3417 /* In this case there is a dependence forward for all the
3418 outer loops:
3419
3420 | for (k = 1; k < 100; k++)
3421 | for (i = 1; i < 100; i++)
3422 | for (j = 1; j < 100; j++)
3423 | {
3424 | t = T[j+1][i-1]; // A
3425 | T[j][i] = t + 2; // B
3426 | }
3427
48e1416a 3428 the vectors are:
b44d1046 3429 (0, 1, -1)
3430 (1, 1, -1)
3431 (1, -1, 1)
3432 */
3433 if (DDR_NB_LOOPS (ddr) > 1)
3434 {
3435 add_outer_distances (ddr, save_v, index_carry);
3436 add_outer_distances (ddr, dist_v, index_carry);
bc3c8ad4 3437 }
b44d1046 3438 }
3439 else
3440 {
3441 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3442 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
bc3c8ad4 3443
b44d1046 3444 if (DDR_NB_LOOPS (ddr) > 1)
2146e26d 3445 {
b44d1046 3446 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
bc3c8ad4 3447
d534eb36 3448 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr),
3449 DDR_A (ddr), loop_nest))
3450 return false;
b44d1046 3451 compute_subscript_distance (ddr);
d534eb36 3452 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3453 opposite_v, &init_b,
3454 &index_carry))
3455 return false;
bc3c8ad4 3456
d534eb36 3457 save_dist_v (ddr, save_v);
b44d1046 3458 add_outer_distances (ddr, dist_v, index_carry);
3459 add_outer_distances (ddr, opposite_v, index_carry);
2146e26d 3460 }
d534eb36 3461 else
3462 save_dist_v (ddr, save_v);
2146e26d 3463 }
3464 }
b44d1046 3465 else
3466 {
3467 /* There is a distance of 1 on all the outer loops: Example:
3468 there is a dependence of distance 1 on loop_1 for the array A.
1532ec98 3469
b44d1046 3470 | loop_1
3471 | A[5] = ...
3472 | endloop
3473 */
3474 add_outer_distances (ddr, dist_v,
3475 lambda_vector_first_nz (dist_v,
3476 DDR_NB_LOOPS (ddr), 0));
3477 }
3478
3479 if (dump_file && (dump_flags & TDF_DETAILS))
1532ec98 3480 {
b44d1046 3481 unsigned i;
1532ec98 3482
b44d1046 3483 fprintf (dump_file, "(build_classic_dist_vector\n");
3484 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3485 {
3486 fprintf (dump_file, " dist_vector = (");
3487 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3488 DDR_NB_LOOPS (ddr));
3489 fprintf (dump_file, " )\n");
3490 }
3491 fprintf (dump_file, ")\n");
1532ec98 3492 }
3493
b44d1046 3494 return true;
3495}
2146e26d 3496
b44d1046 3497/* Return the direction for a given distance.
3498 FIXME: Computing dir this way is suboptimal, since dir can catch
3499 cases that dist is unable to represent. */
bc3c8ad4 3500
b44d1046 3501static inline enum data_dependence_direction
3502dir_from_dist (int dist)
3503{
3504 if (dist > 0)
3505 return dir_positive;
3506 else if (dist < 0)
3507 return dir_negative;
3508 else
3509 return dir_equal;
3510}
1532ec98 3511
b44d1046 3512/* Compute the classic per loop direction vector. DDR is the data
3513 dependence relation to build a vector from. */
1532ec98 3514
b44d1046 3515static void
3516build_classic_dir_vector (struct data_dependence_relation *ddr)
3517{
3518 unsigned i, j;
3519 lambda_vector dist_v;
bc3c8ad4 3520
f1f41a6c 3521 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
b44d1046 3522 {
3523 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
bc3c8ad4 3524
b44d1046 3525 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3526 dir_v[j] = dir_from_dist (dist_v[j]);
1532ec98 3527
b44d1046 3528 save_dir_v (ddr, dir_v);
3529 }
2146e26d 3530}
3531
b44d1046 3532/* Helper function. Returns true when there is a dependence between
3533 data references DRA and DRB. */
6b421feb 3534
b44d1046 3535static bool
3536subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
3537 struct data_reference *dra,
8c7b1d8e 3538 struct data_reference *drb,
3539 struct loop *loop_nest)
6b421feb 3540{
3541 unsigned int i;
6b421feb 3542 tree last_conflicts;
41c7a324 3543 struct subscript *subscript;
2ecb06bc 3544 tree res = NULL_TREE;
b44d1046 3545
f1f41a6c 3546 for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
6b421feb 3547 {
87da4f2e 3548 conflict_function *overlaps_a, *overlaps_b;
41c7a324 3549
48e1416a 3550 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
6b421feb 3551 DR_ACCESS_FN (drb, i),
48e1416a 3552 &overlaps_a, &overlaps_b,
8c7b1d8e 3553 &last_conflicts, loop_nest);
41c7a324 3554
2ecb06bc 3555 if (SUB_CONFLICTS_IN_A (subscript))
3556 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
3557 if (SUB_CONFLICTS_IN_B (subscript))
3558 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
3559
3560 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3561 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3562 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3563
3564 /* If there is any undetermined conflict function we have to
3565 give a conservative answer in case we cannot prove that
3566 no dependence exists when analyzing another subscript. */
87da4f2e 3567 if (CF_NOT_KNOWN_P (overlaps_a)
3568 || CF_NOT_KNOWN_P (overlaps_b))
6b421feb 3569 {
2ecb06bc 3570 res = chrec_dont_know;
3571 continue;
6b421feb 3572 }
41c7a324 3573
2ecb06bc 3574 /* When there is a subscript with no dependence we can stop. */
87da4f2e 3575 else if (CF_NO_DEPENDENCE_P (overlaps_a)
3576 || CF_NO_DEPENDENCE_P (overlaps_b))
6b421feb 3577 {
2ecb06bc 3578 res = chrec_known;
3579 break;
6b421feb 3580 }
3581 }
3582
2ecb06bc 3583 if (res == NULL_TREE)
3584 return true;
3585
3586 if (res == chrec_known)
3587 dependence_stats.num_dependence_independent++;
3588 else
3589 dependence_stats.num_dependence_undetermined++;
3590 finalize_ddr_dependent (ddr, res);
3591 return false;
b44d1046 3592}
3593
8c7b1d8e 3594/* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
b44d1046 3595
3596static void
8c7b1d8e 3597subscript_dependence_tester (struct data_dependence_relation *ddr,
3598 struct loop *loop_nest)
b44d1046 3599{
8c7b1d8e 3600 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr), loop_nest))
b44d1046 3601 dependence_stats.num_dependence_dependent++;
6b421feb 3602
6b421feb 3603 compute_subscript_distance (ddr);
8c7b1d8e 3604 if (build_classic_dist_vector (ddr, loop_nest))
b44d1046 3605 build_classic_dir_vector (ddr);
6b421feb 3606}
3607
2146e26d 3608/* Returns true when all the access functions of A are affine or
8c7b1d8e 3609 constant with respect to LOOP_NEST. */
2146e26d 3610
48e1416a 3611static bool
7ecb5bb2 3612access_functions_are_affine_or_constant_p (const struct data_reference *a,
3613 const struct loop *loop_nest)
2146e26d 3614{
3615 unsigned int i;
f1f41a6c 3616 vec<tree> fns = DR_ACCESS_FNS (a);
e38f1ca4 3617 tree t;
355572cc 3618
f1f41a6c 3619 FOR_EACH_VEC_ELT (fns, i, t)
8c7b1d8e 3620 if (!evolution_function_is_invariant_p (t, loop_nest->num)
3621 && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
2146e26d 3622 return false;
48e1416a 3623
2146e26d 3624 return true;
3625}
3626
355572cc 3627/* Initializes an equation for an OMEGA problem using the information
3628 contained in the ACCESS_FUN. Returns true when the operation
3629 succeeded.
3630
3631 PB is the omega constraint system.
3632 EQ is the number of the equation to be initialized.
3633 OFFSET is used for shifting the variables names in the constraints:
3634 a constrain is composed of 2 * the number of variables surrounding
3635 dependence accesses. OFFSET is set either to 0 for the first n variables,
3636 then it is set to n.
3637 ACCESS_FUN is expected to be an affine chrec. */
3638
3639static bool
48e1416a 3640init_omega_eq_with_af (omega_pb pb, unsigned eq,
3641 unsigned int offset, tree access_fun,
355572cc 3642 struct data_dependence_relation *ddr)
3643{
3644 switch (TREE_CODE (access_fun))
3645 {
3646 case POLYNOMIAL_CHREC:
3647 {
3648 tree left = CHREC_LEFT (access_fun);
3649 tree right = CHREC_RIGHT (access_fun);
3650 int var = CHREC_VARIABLE (access_fun);
3651 unsigned var_idx;
3652
3653 if (TREE_CODE (right) != INTEGER_CST)
3654 return false;
3655
3656 var_idx = index_in_loop_nest (var, DDR_LOOP_NEST (ddr));
58f77a87 3657 pb->eqs[eq].coef[offset + var_idx + 1] = int_cst_value (right);
355572cc 3658
3659 /* Compute the innermost loop index. */
3660 DDR_INNER_LOOP (ddr) = MAX (DDR_INNER_LOOP (ddr), var_idx);
3661
3662 if (offset == 0)
48e1416a 3663 pb->eqs[eq].coef[var_idx + DDR_NB_LOOPS (ddr) + 1]
58f77a87 3664 += int_cst_value (right);
355572cc 3665
3666 switch (TREE_CODE (left))
3667 {
3668 case POLYNOMIAL_CHREC:
3669 return init_omega_eq_with_af (pb, eq, offset, left, ddr);
3670
3671 case INTEGER_CST:
58f77a87 3672 pb->eqs[eq].coef[0] += int_cst_value (left);
355572cc 3673 return true;
3674
3675 default:
3676 return false;
3677 }
3678 }
3679
3680 case INTEGER_CST:
58f77a87 3681 pb->eqs[eq].coef[0] += int_cst_value (access_fun);
355572cc 3682 return true;
3683
3684 default:
3685 return false;
3686 }
3687}
3688
3689/* As explained in the comments preceding init_omega_for_ddr, we have
3690 to set up a system for each loop level, setting outer loops
3691 variation to zero, and current loop variation to positive or zero.
3692 Save each lexico positive distance vector. */
3693
3694static void
3695omega_extract_distance_vectors (omega_pb pb,
3696 struct data_dependence_relation *ddr)
3697{
3698 int eq, geq;
3699 unsigned i, j;
3700 struct loop *loopi, *loopj;
3701 enum omega_result res;
3702
3703 /* Set a new problem for each loop in the nest. The basis is the
3704 problem that we have initialized until now. On top of this we
3705 add new constraints. */
48e1416a 3706 for (i = 0; i <= DDR_INNER_LOOP (ddr)
f1f41a6c 3707 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
355572cc 3708 {
3709 int dist = 0;
3710 omega_pb copy = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr),
3711 DDR_NB_LOOPS (ddr));
3712
3713 omega_copy_problem (copy, pb);
3714
3715 /* For all the outer loops "loop_j", add "dj = 0". */
f1f41a6c 3716 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
355572cc 3717 {
3718 eq = omega_add_zero_eq (copy, omega_black);
3719 copy->eqs[eq].coef[j + 1] = 1;
3720 }
3721
3722 /* For "loop_i", add "0 <= di". */
3723 geq = omega_add_zero_geq (copy, omega_black);
3724 copy->geqs[geq].coef[i + 1] = 1;
3725
3726 /* Reduce the constraint system, and test that the current
3727 problem is feasible. */
3728 res = omega_simplify_problem (copy);
48e1416a 3729 if (res == omega_false
355572cc 3730 || res == omega_unknown
3731 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3732 goto next_problem;
3733
3734 for (eq = 0; eq < copy->num_subs; eq++)
3735 if (copy->subs[eq].key == (int) i + 1)
3736 {
3737 dist = copy->subs[eq].coef[0];
3738 goto found_dist;
3739 }
3740
3741 if (dist == 0)
3742 {
3743 /* Reinitialize problem... */
3744 omega_copy_problem (copy, pb);
f1f41a6c 3745 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
355572cc 3746 {
3747 eq = omega_add_zero_eq (copy, omega_black);
3748 copy->eqs[eq].coef[j + 1] = 1;
3749 }
3750
3751 /* ..., but this time "di = 1". */
3752 eq = omega_add_zero_eq (copy, omega_black);
3753 copy->eqs[eq].coef[i + 1] = 1;
3754 copy->eqs[eq].coef[0] = -1;
3755
3756 res = omega_simplify_problem (copy);
48e1416a 3757 if (res == omega_false
355572cc 3758 || res == omega_unknown
3759 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3760 goto next_problem;
3761
3762 for (eq = 0; eq < copy->num_subs; eq++)
3763 if (copy->subs[eq].key == (int) i + 1)
3764 {
3765 dist = copy->subs[eq].coef[0];
3766 goto found_dist;
3767 }
3768 }
3769
3770 found_dist:;
3771 /* Save the lexicographically positive distance vector. */
3772 if (dist >= 0)
3773 {
3774 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3775 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3776
3777 dist_v[i] = dist;
3778
3779 for (eq = 0; eq < copy->num_subs; eq++)
3780 if (copy->subs[eq].key > 0)
3781 {
3782 dist = copy->subs[eq].coef[0];
3783 dist_v[copy->subs[eq].key - 1] = dist;
3784 }
3785
3786 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3787 dir_v[j] = dir_from_dist (dist_v[j]);
3788
3789 save_dist_v (ddr, dist_v);
3790 save_dir_v (ddr, dir_v);
3791 }
3792
3793 next_problem:;
3794 omega_free_problem (copy);
3795 }
3796}
3797
3798/* This is called for each subscript of a tuple of data references:
3799 insert an equality for representing the conflicts. */
3800
3801static bool
3802omega_setup_subscript (tree access_fun_a, tree access_fun_b,
3803 struct data_dependence_relation *ddr,
3804 omega_pb pb, bool *maybe_dependent)
3805{
3806 int eq;
9a3dad1c 3807 tree type = signed_type_for_types (TREE_TYPE (access_fun_a),
3808 TREE_TYPE (access_fun_b));
75a70cf9 3809 tree fun_a = chrec_convert (type, access_fun_a, NULL);
3810 tree fun_b = chrec_convert (type, access_fun_b, NULL);
9a3dad1c 3811 tree difference = chrec_fold_minus (type, fun_a, fun_b);
ff2cd6d1 3812 tree minus_one;
355572cc 3813
3814 /* When the fun_a - fun_b is not constant, the dependence is not
3815 captured by the classic distance vector representation. */
3816 if (TREE_CODE (difference) != INTEGER_CST)
3817 return false;
3818
3819 /* ZIV test. */
3820 if (ziv_subscript_p (fun_a, fun_b) && !integer_zerop (difference))
3821 {
3822 /* There is no dependence. */
3823 *maybe_dependent = false;
3824 return true;
3825 }
3826
ff2cd6d1 3827 minus_one = build_int_cst (type, -1);
3828 fun_b = chrec_fold_multiply (type, fun_b, minus_one);
355572cc 3829
3830 eq = omega_add_zero_eq (pb, omega_black);
3831 if (!init_omega_eq_with_af (pb, eq, DDR_NB_LOOPS (ddr), fun_a, ddr)
3832 || !init_omega_eq_with_af (pb, eq, 0, fun_b, ddr))
3833 /* There is probably a dependence, but the system of
3834 constraints cannot be built: answer "don't know". */
3835 return false;
3836
3837 /* GCD test. */
3838 if (DDR_NB_LOOPS (ddr) != 0 && pb->eqs[eq].coef[0]
48e1416a 3839 && !int_divides_p (lambda_vector_gcd
355572cc 3840 ((lambda_vector) &(pb->eqs[eq].coef[1]),
3841 2 * DDR_NB_LOOPS (ddr)),
3842 pb->eqs[eq].coef[0]))
3843 {
3844 /* There is no dependence. */
3845 *maybe_dependent = false;
3846 return true;
3847 }
3848
3849 return true;
3850}
3851
3852/* Helper function, same as init_omega_for_ddr but specialized for
3853 data references A and B. */
3854
3855static bool
3856init_omega_for_ddr_1 (struct data_reference *dra, struct data_reference *drb,
3857 struct data_dependence_relation *ddr,
3858 omega_pb pb, bool *maybe_dependent)
3859{
3860 unsigned i;
3861 int ineq;
3862 struct loop *loopi;
3863 unsigned nb_loops = DDR_NB_LOOPS (ddr);
3864
3865 /* Insert an equality per subscript. */
3866 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3867 {
3868 if (!omega_setup_subscript (DR_ACCESS_FN (dra, i), DR_ACCESS_FN (drb, i),
3869 ddr, pb, maybe_dependent))
3870 return false;
3871 else if (*maybe_dependent == false)
3872 {
3873 /* There is no dependence. */
3874 DDR_ARE_DEPENDENT (ddr) = chrec_known;
3875 return true;
3876 }
3877 }
3878
3879 /* Insert inequalities: constraints corresponding to the iteration
3880 domain, i.e. the loops surrounding the references "loop_x" and
3881 the distance variables "dx". The layout of the OMEGA
3882 representation is as follows:
3883 - coef[0] is the constant
3884 - coef[1..nb_loops] are the protected variables that will not be
3885 removed by the solver: the "dx"
3886 - coef[nb_loops + 1, 2*nb_loops] are the loop variables: "loop_x".
3887 */
48e1416a 3888 for (i = 0; i <= DDR_INNER_LOOP (ddr)
f1f41a6c 3889 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
355572cc 3890 {
fee017b3 3891 HOST_WIDE_INT nbi = max_stmt_executions_int (loopi);
355572cc 3892
3893 /* 0 <= loop_x */
3894 ineq = omega_add_zero_geq (pb, omega_black);
3895 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3896
3897 /* 0 <= loop_x + dx */
3898 ineq = omega_add_zero_geq (pb, omega_black);
3899 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3900 pb->geqs[ineq].coef[i + 1] = 1;
3901
3902 if (nbi != -1)
3903 {
3904 /* loop_x <= nb_iters */
3905 ineq = omega_add_zero_geq (pb, omega_black);
3906 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3907 pb->geqs[ineq].coef[0] = nbi;
3908
3909 /* loop_x + dx <= nb_iters */
3910 ineq = omega_add_zero_geq (pb, omega_black);
3911 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3912 pb->geqs[ineq].coef[i + 1] = -1;
3913 pb->geqs[ineq].coef[0] = nbi;
3914
3915 /* A step "dx" bigger than nb_iters is not feasible, so
3916 add "0 <= nb_iters + dx", */
3917 ineq = omega_add_zero_geq (pb, omega_black);
3918 pb->geqs[ineq].coef[i + 1] = 1;
3919 pb->geqs[ineq].coef[0] = nbi;
3920 /* and "dx <= nb_iters". */
3921 ineq = omega_add_zero_geq (pb, omega_black);
3922 pb->geqs[ineq].coef[i + 1] = -1;
3923 pb->geqs[ineq].coef[0] = nbi;
3924 }
3925 }
3926
3927 omega_extract_distance_vectors (pb, ddr);
3928
3929 return true;
3930}
3931
3932/* Sets up the Omega dependence problem for the data dependence
3933 relation DDR. Returns false when the constraint system cannot be
3934 built, ie. when the test answers "don't know". Returns true
3935 otherwise, and when independence has been proved (using one of the
3936 trivial dependence test), set MAYBE_DEPENDENT to false, otherwise
3937 set MAYBE_DEPENDENT to true.
3938
3939 Example: for setting up the dependence system corresponding to the
48e1416a 3940 conflicting accesses
355572cc 3941
3942 | loop_i
3943 | loop_j
3944 | A[i, i+1] = ...
3945 | ... A[2*j, 2*(i + j)]
3946 | endloop_j
3947 | endloop_i
48e1416a 3948
355572cc 3949 the following constraints come from the iteration domain:
3950
3951 0 <= i <= Ni
3952 0 <= i + di <= Ni
3953 0 <= j <= Nj
3954 0 <= j + dj <= Nj
3955
3956 where di, dj are the distance variables. The constraints
3957 representing the conflicting elements are:
3958
3959 i = 2 * (j + dj)
3960 i + 1 = 2 * (i + di + j + dj)
3961
3962 For asking that the resulting distance vector (di, dj) be
3963 lexicographically positive, we insert the constraint "di >= 0". If
3964 "di = 0" in the solution, we fix that component to zero, and we
3965 look at the inner loops: we set a new problem where all the outer
3966 loop distances are zero, and fix this inner component to be
3967 positive. When one of the components is positive, we save that
3968 distance, and set a new problem where the distance on this loop is
3969 zero, searching for other distances in the inner loops. Here is
3970 the classic example that illustrates that we have to set for each
3971 inner loop a new problem:
3972
3973 | loop_1
3974 | loop_2
3975 | A[10]
3976 | endloop_2
3977 | endloop_1
3978
3979 we have to save two distances (1, 0) and (0, 1).
3980
3981 Given two array references, refA and refB, we have to set the
3982 dependence problem twice, refA vs. refB and refB vs. refA, and we
3983 cannot do a single test, as refB might occur before refA in the
3984 inner loops, and the contrary when considering outer loops: ex.
3985
3986 | loop_0
3987 | loop_1
3988 | loop_2
3989 | T[{1,+,1}_2][{1,+,1}_1] // refA
3990 | T[{2,+,1}_2][{0,+,1}_1] // refB
3991 | endloop_2
3992 | endloop_1
3993 | endloop_0
3994
3995 refB touches the elements in T before refA, and thus for the same
3996 loop_0 refB precedes refA: ie. the distance vector (0, 1, -1)
3997 but for successive loop_0 iterations, we have (1, -1, 1)
3998
3999 The Omega solver expects the distance variables ("di" in the
4000 previous example) to come first in the constraint system (as
4001 variables to be protected, or "safe" variables), the constraint
4002 system is built using the following layout:
4003
4004 "cst | distance vars | index vars".
4005*/
4006
4007static bool
4008init_omega_for_ddr (struct data_dependence_relation *ddr,
4009 bool *maybe_dependent)
4010{
4011 omega_pb pb;
4012 bool res = false;
4013
4014 *maybe_dependent = true;
4015
4016 if (same_access_functions (ddr))
4017 {
4018 unsigned j;
4019 lambda_vector dir_v;
4020
4021 /* Save the 0 vector. */
4022 save_dist_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
4023 dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4024 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
4025 dir_v[j] = dir_equal;
4026 save_dir_v (ddr, dir_v);
4027
4028 /* Save the dependences carried by outer loops. */
4029 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4030 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4031 maybe_dependent);
4032 omega_free_problem (pb);
4033 return res;
4034 }
4035
4036 /* Omega expects the protected variables (those that have to be kept
4037 after elimination) to appear first in the constraint system.
4038 These variables are the distance variables. In the following
4039 initialization we declare NB_LOOPS safe variables, and the total
4040 number of variables for the constraint system is 2*NB_LOOPS. */
4041 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4042 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4043 maybe_dependent);
4044 omega_free_problem (pb);
4045
4046 /* Stop computation if not decidable, or no dependence. */
4047 if (res == false || *maybe_dependent == false)
4048 return res;
4049
4050 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4051 res = init_omega_for_ddr_1 (DDR_B (ddr), DDR_A (ddr), ddr, pb,
4052 maybe_dependent);
4053 omega_free_problem (pb);
4054
4055 return res;
4056}
4057
4058/* Return true when DDR contains the same information as that stored
4059 in DIR_VECTS and in DIST_VECTS, return false otherwise. */
4060
4061static bool
4062ddr_consistent_p (FILE *file,
4063 struct data_dependence_relation *ddr,
f1f41a6c 4064 vec<lambda_vector> dist_vects,
4065 vec<lambda_vector> dir_vects)
355572cc 4066{
4067 unsigned int i, j;
4068
4069 /* If dump_file is set, output there. */
4070 if (dump_file && (dump_flags & TDF_DETAILS))
4071 file = dump_file;
4072
f1f41a6c 4073 if (dist_vects.length () != DDR_NUM_DIST_VECTS (ddr))
355572cc 4074 {
4075 lambda_vector b_dist_v;
4076 fprintf (file, "\n(Number of distance vectors differ: Banerjee has %d, Omega has %d.\n",
f1f41a6c 4077 dist_vects.length (),
355572cc 4078 DDR_NUM_DIST_VECTS (ddr));
4079
4080 fprintf (file, "Banerjee dist vectors:\n");
f1f41a6c 4081 FOR_EACH_VEC_ELT (dist_vects, i, b_dist_v)
355572cc 4082 print_lambda_vector (file, b_dist_v, DDR_NB_LOOPS (ddr));
4083
4084 fprintf (file, "Omega dist vectors:\n");
4085 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4086 print_lambda_vector (file, DDR_DIST_VECT (ddr, i), DDR_NB_LOOPS (ddr));
4087
4088 fprintf (file, "data dependence relation:\n");
4089 dump_data_dependence_relation (file, ddr);
4090
4091 fprintf (file, ")\n");
4092 return false;
4093 }
4094
f1f41a6c 4095 if (dir_vects.length () != DDR_NUM_DIR_VECTS (ddr))
355572cc 4096 {
4097 fprintf (file, "\n(Number of direction vectors differ: Banerjee has %d, Omega has %d.)\n",
f1f41a6c 4098 dir_vects.length (),
355572cc 4099 DDR_NUM_DIR_VECTS (ddr));
4100 return false;
4101 }
4102
4103 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4104 {
4105 lambda_vector a_dist_v;
4106 lambda_vector b_dist_v = DDR_DIST_VECT (ddr, i);
4107
4108 /* Distance vectors are not ordered in the same way in the DDR
4109 and in the DIST_VECTS: search for a matching vector. */
f1f41a6c 4110 FOR_EACH_VEC_ELT (dist_vects, j, a_dist_v)
355572cc 4111 if (lambda_vector_equal (a_dist_v, b_dist_v, DDR_NB_LOOPS (ddr)))
4112 break;
4113
f1f41a6c 4114 if (j == dist_vects.length ())
355572cc 4115 {
4116 fprintf (file, "\n(Dist vectors from the first dependence analyzer:\n");
4117 print_dist_vectors (file, dist_vects, DDR_NB_LOOPS (ddr));
4118 fprintf (file, "not found in Omega dist vectors:\n");
4119 print_dist_vectors (file, DDR_DIST_VECTS (ddr), DDR_NB_LOOPS (ddr));
4120 fprintf (file, "data dependence relation:\n");
4121 dump_data_dependence_relation (file, ddr);
4122 fprintf (file, ")\n");
4123 }
4124 }
4125
4126 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
4127 {
4128 lambda_vector a_dir_v;
4129 lambda_vector b_dir_v = DDR_DIR_VECT (ddr, i);
4130
4131 /* Direction vectors are not ordered in the same way in the DDR
4132 and in the DIR_VECTS: search for a matching vector. */
f1f41a6c 4133 FOR_EACH_VEC_ELT (dir_vects, j, a_dir_v)
355572cc 4134 if (lambda_vector_equal (a_dir_v, b_dir_v, DDR_NB_LOOPS (ddr)))
4135 break;
4136
f1f41a6c 4137 if (j == dist_vects.length ())
355572cc 4138 {
4139 fprintf (file, "\n(Dir vectors from the first dependence analyzer:\n");
4140 print_dir_vectors (file, dir_vects, DDR_NB_LOOPS (ddr));
4141 fprintf (file, "not found in Omega dir vectors:\n");
4142 print_dir_vectors (file, DDR_DIR_VECTS (ddr), DDR_NB_LOOPS (ddr));
4143 fprintf (file, "data dependence relation:\n");
4144 dump_data_dependence_relation (file, ddr);
4145 fprintf (file, ")\n");
4146 }
4147 }
4148
48e1416a 4149 return true;
355572cc 4150}
4151
8c7b1d8e 4152/* This computes the affine dependence relation between A and B with
4153 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
4154 independence between two accesses, while CHREC_DONT_KNOW is used
4155 for representing the unknown relation.
48e1416a 4156
2146e26d 4157 Note that it is possible to stop the computation of the dependence
4158 relation the first time we detect a CHREC_KNOWN element for a given
4159 subscript. */
4160
7b6f8db4 4161void
8c7b1d8e 4162compute_affine_dependence (struct data_dependence_relation *ddr,
4163 struct loop *loop_nest)
2146e26d 4164{
4165 struct data_reference *dra = DDR_A (ddr);
4166 struct data_reference *drb = DDR_B (ddr);
48e1416a 4167
2146e26d 4168 if (dump_file && (dump_flags & TDF_DETAILS))
4169 {
6b6f234c 4170 fprintf (dump_file, "(compute_affine_dependence\n");
95539e1d 4171 fprintf (dump_file, " stmt_a: ");
4172 print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
4173 fprintf (dump_file, " stmt_b: ");
4174 print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
2146e26d 4175 }
6b421feb 4176
2146e26d 4177 /* Analyze only when the dependence relation is not yet known. */
c76b51be 4178 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
2146e26d 4179 {
6b421feb 4180 dependence_stats.num_dependence_tests++;
4181
8c7b1d8e 4182 if (access_functions_are_affine_or_constant_p (dra, loop_nest)
4183 && access_functions_are_affine_or_constant_p (drb, loop_nest))
355572cc 4184 {
984cebc1 4185 subscript_dependence_tester (ddr, loop_nest);
4186
355572cc 4187 if (flag_check_data_deps)
4188 {
984cebc1 4189 /* Dump the dependences from the first algorithm. */
355572cc 4190 if (dump_file && (dump_flags & TDF_DETAILS))
4191 {
4192 fprintf (dump_file, "\n\nBanerjee Analyzer\n");
4193 dump_data_dependence_relation (dump_file, ddr);
4194 }
4195
4196 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4197 {
4198 bool maybe_dependent;
f1f41a6c 4199 vec<lambda_vector> dir_vects, dist_vects;
355572cc 4200
4201 /* Save the result of the first DD analyzer. */
4202 dist_vects = DDR_DIST_VECTS (ddr);
4203 dir_vects = DDR_DIR_VECTS (ddr);
4204
4205 /* Reset the information. */
f1f41a6c 4206 DDR_DIST_VECTS (ddr).create (0);
4207 DDR_DIR_VECTS (ddr).create (0);
355572cc 4208
4209 /* Compute the same information using Omega. */
4210 if (!init_omega_for_ddr (ddr, &maybe_dependent))
4211 goto csys_dont_know;
4212
4213 if (dump_file && (dump_flags & TDF_DETAILS))
4214 {
4215 fprintf (dump_file, "Omega Analyzer\n");
4216 dump_data_dependence_relation (dump_file, ddr);
4217 }
4218
4219 /* Check that we get the same information. */
4220 if (maybe_dependent)
4221 gcc_assert (ddr_consistent_p (stderr, ddr, dist_vects,
4222 dir_vects));
4223 }
4224 }
355572cc 4225 }
48e1416a 4226
2146e26d 4227 /* As a last case, if the dependence cannot be determined, or if
4228 the dependence is considered too difficult to determine, answer
4229 "don't know". */
4230 else
6b421feb 4231 {
355572cc 4232 csys_dont_know:;
6b421feb 4233 dependence_stats.num_dependence_undetermined++;
4234
4235 if (dump_file && (dump_flags & TDF_DETAILS))
4236 {
4237 fprintf (dump_file, "Data ref a:\n");
4238 dump_data_reference (dump_file, dra);
4239 fprintf (dump_file, "Data ref b:\n");
4240 dump_data_reference (dump_file, drb);
4241 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
4242 }
4243 finalize_ddr_dependent (ddr, chrec_dont_know);
4244 }
2146e26d 4245 }
48e1416a 4246
2146e26d 4247 if (dump_file && (dump_flags & TDF_DETAILS))
95539e1d 4248 {
4249 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4250 fprintf (dump_file, ") -> no dependence\n");
4251 else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
4252 fprintf (dump_file, ") -> dependence analysis failed\n");
4253 else
4254 fprintf (dump_file, ")\n");
4255 }
2146e26d 4256}
4257
b44d1046 4258/* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
4259 the data references in DATAREFS, in the LOOP_NEST. When
41c7a324 4260 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
c7af8ae7 4261 relations. Return true when successful, i.e. data references number
4262 is small enough to be handled. */
2146e26d 4263
c7af8ae7 4264bool
f1f41a6c 4265compute_all_dependences (vec<data_reference_p> datarefs,
4266 vec<ddr_p> *dependence_relations,
4267 vec<loop_p> loop_nest,
41c7a324 4268 bool compute_self_and_rr)
2146e26d 4269{
41c7a324 4270 struct data_dependence_relation *ddr;
4271 struct data_reference *a, *b;
4272 unsigned int i, j;
2146e26d 4273
f1f41a6c 4274 if ((int) datarefs.length ()
c7af8ae7 4275 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
4276 {
4277 struct data_dependence_relation *ddr;
4278
4279 /* Insert a single relation into dependence_relations:
4280 chrec_dont_know. */
4281 ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
f1f41a6c 4282 dependence_relations->safe_push (ddr);
c7af8ae7 4283 return false;
4284 }
4285
f1f41a6c 4286 FOR_EACH_VEC_ELT (datarefs, i, a)
4287 for (j = i + 1; datarefs.iterate (j, &b); j++)
9ff25603 4288 if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
41c7a324 4289 {
4290 ddr = initialize_data_dependence_relation (a, b, loop_nest);
f1f41a6c 4291 dependence_relations->safe_push (ddr);
4292 if (loop_nest.exists ())
4293 compute_affine_dependence (ddr, loop_nest[0]);
41c7a324 4294 }
02d46b9c 4295
41c7a324 4296 if (compute_self_and_rr)
f1f41a6c 4297 FOR_EACH_VEC_ELT (datarefs, i, a)
2146e26d 4298 {
41c7a324 4299 ddr = initialize_data_dependence_relation (a, a, loop_nest);
f1f41a6c 4300 dependence_relations->safe_push (ddr);
4301 if (loop_nest.exists ())
4302 compute_affine_dependence (ddr, loop_nest[0]);
2146e26d 4303 }
c7af8ae7 4304
4305 return true;
2146e26d 4306}
4307
d129ada0 4308/* Describes a location of a memory reference. */
4309
4310typedef struct data_ref_loc_d
4311{
07e3bcbf 4312 /* Position of the memory reference. */
4313 tree *pos;
d129ada0 4314
07e3bcbf 4315 /* True if the memory reference is read. */
4316 bool is_read;
d129ada0 4317} data_ref_loc;
4318
d129ada0 4319
faa56cf9 4320/* Stores the locations of memory references in STMT to REFERENCES. Returns
4321 true if STMT clobbers memory, false otherwise. */
4322
d129ada0 4323static bool
07e3bcbf 4324get_references_in_stmt (gimple stmt, vec<data_ref_loc, va_stack> *references)
faa56cf9 4325{
4326 bool clobbers_memory = false;
e82e4eb5 4327 data_ref_loc ref;
75a70cf9 4328 tree *op0, *op1;
4329 enum gimple_code stmt_code = gimple_code (stmt);
faa56cf9 4330
faa56cf9 4331 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
0ef2a6d1 4332 As we cannot model data-references to not spelled out
4333 accesses give up if they may occur. */
3d483a94 4334 if (stmt_code == GIMPLE_CALL
4335 && !(gimple_call_flags (stmt) & ECF_CONST))
4336 {
4337 /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
4338 if (gimple_call_internal_p (stmt)
4339 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
4340 {
4341 struct loop *loop = gimple_bb (stmt)->loop_father;
4342 tree uid = gimple_call_arg (stmt, 0);
4343 gcc_assert (TREE_CODE (uid) == SSA_NAME);
4344 if (loop == NULL
4345 || loop->simduid != SSA_NAME_VAR (uid))
4346 clobbers_memory = true;
4347 }
4348 else
4349 clobbers_memory = true;
4350 }
4351 else if (stmt_code == GIMPLE_ASM
4352 && (gimple_asm_volatile_p (stmt) || gimple_vuse (stmt)))
faa56cf9 4353 clobbers_memory = true;
4354
dd277d48 4355 if (!gimple_vuse (stmt))
faa56cf9 4356 return clobbers_memory;
4357
75a70cf9 4358 if (stmt_code == GIMPLE_ASSIGN)
faa56cf9 4359 {
ffb04365 4360 tree base;
75a70cf9 4361 op0 = gimple_assign_lhs_ptr (stmt);
4362 op1 = gimple_assign_rhs1_ptr (stmt);
48e1416a 4363
faa56cf9 4364 if (DECL_P (*op1)
ffb04365 4365 || (REFERENCE_CLASS_P (*op1)
4366 && (base = get_base_address (*op1))
4367 && TREE_CODE (base) != SSA_NAME))
faa56cf9 4368 {
e82e4eb5 4369 ref.pos = op1;
4370 ref.is_read = true;
f1f41a6c 4371 references->safe_push (ref);
faa56cf9 4372 }
faa56cf9 4373 }
75a70cf9 4374 else if (stmt_code == GIMPLE_CALL)
faa56cf9 4375 {
9f2e27ae 4376 unsigned i, n;
d97e22fb 4377
9f2e27ae 4378 op0 = gimple_call_lhs_ptr (stmt);
4379 n = gimple_call_num_args (stmt);
d97e22fb 4380 for (i = 0; i < n; i++)
faa56cf9 4381 {
9f2e27ae 4382 op1 = gimple_call_arg_ptr (stmt, i);
d97e22fb 4383
9f2e27ae 4384 if (DECL_P (*op1)
4385 || (REFERENCE_CLASS_P (*op1) && get_base_address (*op1)))
faa56cf9 4386 {
e82e4eb5 4387 ref.pos = op1;
4388 ref.is_read = true;
f1f41a6c 4389 references->safe_push (ref);
faa56cf9 4390 }
4391 }
4392 }
9f2e27ae 4393 else
4394 return clobbers_memory;
faa56cf9 4395
9f2e27ae 4396 if (*op0
4397 && (DECL_P (*op0)
4398 || (REFERENCE_CLASS_P (*op0) && get_base_address (*op0))))
4399 {
e82e4eb5 4400 ref.pos = op0;
4401 ref.is_read = false;
f1f41a6c 4402 references->safe_push (ref);
9f2e27ae 4403 }
faa56cf9 4404 return clobbers_memory;
4405}
4406
4407/* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
80bb306a 4408 reference, returns false, otherwise returns true. NEST is the outermost
255b6be7 4409 loop of the loop nest in which the references should be analyzed. */
faa56cf9 4410
255b6be7 4411bool
75a70cf9 4412find_data_references_in_stmt (struct loop *nest, gimple stmt,
f1f41a6c 4413 vec<data_reference_p> *datarefs)
faa56cf9 4414{
4415 unsigned i;
07e3bcbf 4416 vec<data_ref_loc, va_stack> references;
faa56cf9 4417 data_ref_loc *ref;
4418 bool ret = true;
4419 data_reference_p dr;
4420
07e3bcbf 4421 vec_stack_alloc (data_ref_loc, references, 2);
faa56cf9 4422 if (get_references_in_stmt (stmt, &references))
4423 {
f1f41a6c 4424 references.release ();
faa56cf9 4425 return false;
4426 }
4427
f1f41a6c 4428 FOR_EACH_VEC_ELT (references, i, ref)
faa56cf9 4429 {
221a697e 4430 dr = create_data_ref (nest, loop_containing_stmt (stmt),
4431 *ref->pos, stmt, ref->is_read);
ad4a85ad 4432 gcc_assert (dr != NULL);
f1f41a6c 4433 datarefs->safe_push (dr);
faa56cf9 4434 }
f1f41a6c 4435 references.release ();
faa56cf9 4436 return ret;
4437}
4438
221a697e 4439/* Stores the data references in STMT to DATAREFS. If there is an
4440 unanalyzable reference, returns false, otherwise returns true.
4441 NEST is the outermost loop of the loop nest in which the references
4442 should be instantiated, LOOP is the loop in which the references
4443 should be analyzed. */
c794d738 4444
4445bool
221a697e 4446graphite_find_data_references_in_stmt (loop_p nest, loop_p loop, gimple stmt,
f1f41a6c 4447 vec<data_reference_p> *datarefs)
c794d738 4448{
4449 unsigned i;
07e3bcbf 4450 vec<data_ref_loc, va_stack> references;
c794d738 4451 data_ref_loc *ref;
4452 bool ret = true;
4453 data_reference_p dr;
4454
07e3bcbf 4455 vec_stack_alloc (data_ref_loc, references, 2);
c794d738 4456 if (get_references_in_stmt (stmt, &references))
4457 {
f1f41a6c 4458 references.release ();
c794d738 4459 return false;
4460 }
4461
f1f41a6c 4462 FOR_EACH_VEC_ELT (references, i, ref)
c794d738 4463 {
221a697e 4464 dr = create_data_ref (nest, loop, *ref->pos, stmt, ref->is_read);
c794d738 4465 gcc_assert (dr != NULL);
f1f41a6c 4466 datarefs->safe_push (dr);
c794d738 4467 }
4468
f1f41a6c 4469 references.release ();
c794d738 4470 return ret;
4471}
4472
37545e54 4473/* Search the data references in LOOP, and record the information into
4474 DATAREFS. Returns chrec_dont_know when failing to analyze a
4475 difficult case, returns NULL_TREE otherwise. */
4476
ec611e12 4477tree
37545e54 4478find_data_references_in_bb (struct loop *loop, basic_block bb,
f1f41a6c 4479 vec<data_reference_p> *datarefs)
37545e54 4480{
4481 gimple_stmt_iterator bsi;
4482
4483 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4484 {
4485 gimple stmt = gsi_stmt (bsi);
4486
4487 if (!find_data_references_in_stmt (loop, stmt, datarefs))
4488 {
4489 struct data_reference *res;
4490 res = XCNEW (struct data_reference);
f1f41a6c 4491 datarefs->safe_push (res);
37545e54 4492
4493 return chrec_dont_know;
4494 }
4495 }
4496
4497 return NULL_TREE;
4498}
4499
2146e26d 4500/* Search the data references in LOOP, and record the information into
4501 DATAREFS. Returns chrec_dont_know when failing to analyze a
4502 difficult case, returns NULL_TREE otherwise.
80bb306a 4503
557ef5d8 4504 TODO: This function should be made smarter so that it can handle address
4505 arithmetic as if they were array accesses, etc. */
2146e26d 4506
07e3bcbf 4507tree
41c7a324 4508find_data_references_in_loop (struct loop *loop,
f1f41a6c 4509 vec<data_reference_p> *datarefs)
2146e26d 4510{
0332ea54 4511 basic_block bb, *bbs;
4512 unsigned int i;
bc3c8ad4 4513
ad4a85ad 4514 bbs = get_loop_body_in_dom_order (loop);
0332ea54 4515
4516 for (i = 0; i < loop->num_nodes; i++)
2146e26d 4517 {
0332ea54 4518 bb = bbs[i];
4519
37545e54 4520 if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
4521 {
4522 free (bbs);
4523 return chrec_dont_know;
4524 }
2146e26d 4525 }
0332ea54 4526 free (bbs);
4527
4a012ac6 4528 return NULL_TREE;
2146e26d 4529}
4530
b44d1046 4531/* Recursive helper function. */
4532
4533static bool
f1f41a6c 4534find_loop_nest_1 (struct loop *loop, vec<loop_p> *loop_nest)
b44d1046 4535{
4536 /* Inner loops of the nest should not contain siblings. Example:
4537 when there are two consecutive loops,
4538
4539 | loop_0
4540 | loop_1
4541 | A[{0, +, 1}_1]
4542 | endloop_1
4543 | loop_2
4544 | A[{0, +, 1}_2]
4545 | endloop_2
4546 | endloop_0
4547
4548 the dependence relation cannot be captured by the distance
4549 abstraction. */
4550 if (loop->next)
4551 return false;
2146e26d 4552
f1f41a6c 4553 loop_nest->safe_push (loop);
b44d1046 4554 if (loop->inner)
4555 return find_loop_nest_1 (loop->inner, loop_nest);
4556 return true;
4557}
4558
4559/* Return false when the LOOP is not well nested. Otherwise return
4560 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4561 contain the loops from the outermost to the innermost, as they will
4562 appear in the classic distance vector. */
4563
5c205353 4564bool
f1f41a6c 4565find_loop_nest (struct loop *loop, vec<loop_p> *loop_nest)
b44d1046 4566{
f1f41a6c 4567 loop_nest->safe_push (loop);
b44d1046 4568 if (loop->inner)
4569 return find_loop_nest_1 (loop->inner, loop_nest);
4570 return true;
4571}
2146e26d 4572
b79b3386 4573/* Returns true when the data dependences have been computed, false otherwise.
4574 Given a loop nest LOOP, the following vectors are returned:
48e1416a 4575 DATAREFS is initialized to all the array elements contained in this loop,
4576 DEPENDENCE_RELATIONS contains the relations between the data references.
4577 Compute read-read and self relations if
516849c7 4578 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
2146e26d 4579
b79b3386 4580bool
48e1416a 4581compute_data_dependences_for_loop (struct loop *loop,
516849c7 4582 bool compute_self_and_read_read_dependences,
f1f41a6c 4583 vec<loop_p> *loop_nest,
4584 vec<data_reference_p> *datarefs,
4585 vec<ddr_p> *dependence_relations)
2146e26d 4586{
b79b3386 4587 bool res = true;
516849c7 4588
6b421feb 4589 memset (&dependence_stats, 0, sizeof (dependence_stats));
2146e26d 4590
48e1416a 4591 /* If the loop nest is not well formed, or one of the data references
b44d1046 4592 is not computable, give up without spending time to compute other
4593 dependences. */
80bb306a 4594 if (!loop
a8af2e86 4595 || !find_loop_nest (loop, loop_nest)
c7af8ae7 4596 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
4597 || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
4598 compute_self_and_read_read_dependences))
4599 res = false;
6b421feb 4600
4601 if (dump_file && (dump_flags & TDF_STATS))
2146e26d 4602 {
6b421feb 4603 fprintf (dump_file, "Dependence tester statistics:\n");
4604
48e1416a 4605 fprintf (dump_file, "Number of dependence tests: %d\n",
6b421feb 4606 dependence_stats.num_dependence_tests);
48e1416a 4607 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
6b421feb 4608 dependence_stats.num_dependence_dependent);
48e1416a 4609 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
6b421feb 4610 dependence_stats.num_dependence_independent);
48e1416a 4611 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
6b421feb 4612 dependence_stats.num_dependence_undetermined);
4613
48e1416a 4614 fprintf (dump_file, "Number of subscript tests: %d\n",
6b421feb 4615 dependence_stats.num_subscript_tests);
48e1416a 4616 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
6b421feb 4617 dependence_stats.num_subscript_undetermined);
48e1416a 4618 fprintf (dump_file, "Number of same subscript function: %d\n",
6b421feb 4619 dependence_stats.num_same_subscript_function);
4620
4621 fprintf (dump_file, "Number of ziv tests: %d\n",
4622 dependence_stats.num_ziv);
4623 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4624 dependence_stats.num_ziv_dependent);
4625 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4626 dependence_stats.num_ziv_independent);
4627 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
48e1416a 4628 dependence_stats.num_ziv_unimplemented);
6b421feb 4629
48e1416a 4630 fprintf (dump_file, "Number of siv tests: %d\n",
6b421feb 4631 dependence_stats.num_siv);
4632 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4633 dependence_stats.num_siv_dependent);
4634 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4635 dependence_stats.num_siv_independent);
4636 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4637 dependence_stats.num_siv_unimplemented);
4638
48e1416a 4639 fprintf (dump_file, "Number of miv tests: %d\n",
6b421feb 4640 dependence_stats.num_miv);
4641 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4642 dependence_stats.num_miv_dependent);
4643 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4644 dependence_stats.num_miv_independent);
4645 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4646 dependence_stats.num_miv_unimplemented);
b79b3386 4647 }
4648
4649 return res;
2146e26d 4650}
4651
48e1416a 4652/* Returns true when the data dependences for the basic block BB have been
37545e54 4653 computed, false otherwise.
48e1416a 4654 DATAREFS is initialized to all the array elements contained in this basic
37545e54 4655 block, DEPENDENCE_RELATIONS contains the relations between the data
4656 references. Compute read-read and self relations if
4657 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4658bool
4659compute_data_dependences_for_bb (basic_block bb,
4660 bool compute_self_and_read_read_dependences,
f1f41a6c 4661 vec<data_reference_p> *datarefs,
4662 vec<ddr_p> *dependence_relations)
37545e54 4663{
4664 if (find_data_references_in_bb (NULL, bb, datarefs) == chrec_dont_know)
4665 return false;
4666
1e094109 4667 return compute_all_dependences (*datarefs, dependence_relations, vNULL,
c7af8ae7 4668 compute_self_and_read_read_dependences);
37545e54 4669}
4670
2146e26d 4671/* Entry point (for testing only). Analyze all the data references
355572cc 4672 and the dependence relations in LOOP.
2146e26d 4673
48e1416a 4674 The data references are computed first.
4675
2146e26d 4676 A relation on these nodes is represented by a complete graph. Some
4677 of the relations could be of no interest, thus the relations can be
4678 computed on demand.
48e1416a 4679
2146e26d 4680 In the following function we compute all the relations. This is
4681 just a first implementation that is here for:
48e1416a 4682 - for showing how to ask for the dependence relations,
2146e26d 4683 - for the debugging the whole dependence graph,
4684 - for the dejagnu testcases and maintenance.
48e1416a 4685
2146e26d 4686 It is possible to ask only for a part of the graph, avoiding to
4687 compute the whole dependence graph. The computed dependences are
4688 stored in a knowledge base (KB) such that later queries don't
4689 recompute the same information. The implementation of this KB is
4690 transparent to the optimizer, and thus the KB can be changed with a
4691 more efficient implementation, or the KB could be disabled. */
48e1416a 4692static void
355572cc 4693analyze_all_data_dependences (struct loop *loop)
2146e26d 4694{
4695 unsigned int i;
2146e26d 4696 int nb_data_refs = 10;
f1f41a6c 4697 vec<data_reference_p> datarefs;
4698 datarefs.create (nb_data_refs);
4699 vec<ddr_p> dependence_relations;
4700 dependence_relations.create (nb_data_refs * nb_data_refs);
4701 vec<loop_p> loop_nest;
4702 loop_nest.create (3);
2146e26d 4703
4704 /* Compute DDs on the whole function. */
a8af2e86 4705 compute_data_dependences_for_loop (loop, false, &loop_nest, &datarefs,
355572cc 4706 &dependence_relations);
2146e26d 4707
4708 if (dump_file)
4709 {
4710 dump_data_dependence_relations (dump_file, dependence_relations);
4711 fprintf (dump_file, "\n\n");
2146e26d 4712
bc3c8ad4 4713 if (dump_flags & TDF_DETAILS)
4714 dump_dist_dir_vectors (dump_file, dependence_relations);
2146e26d 4715
bc3c8ad4 4716 if (dump_flags & TDF_STATS)
2146e26d 4717 {
bc3c8ad4 4718 unsigned nb_top_relations = 0;
4719 unsigned nb_bot_relations = 0;
bc3c8ad4 4720 unsigned nb_chrec_relations = 0;
41c7a324 4721 struct data_dependence_relation *ddr;
bc3c8ad4 4722
f1f41a6c 4723 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
bc3c8ad4 4724 {
bc3c8ad4 4725 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
4726 nb_top_relations++;
48e1416a 4727
bc3c8ad4 4728 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
dd277d48 4729 nb_bot_relations++;
48e1416a 4730
4731 else
bc3c8ad4 4732 nb_chrec_relations++;
4733 }
48e1416a 4734
bc3c8ad4 4735 gather_stats_on_scev_database ();
4736 }
2146e26d 4737 }
6b6f234c 4738
f1f41a6c 4739 loop_nest.release ();
6b6f234c 4740 free_dependence_relations (dependence_relations);
4741 free_data_refs (datarefs);
4742}
355572cc 4743
4744/* Computes all the data dependences and check that the results of
4745 several analyzers are the same. */
4746
4747void
4748tree_check_data_deps (void)
4749{
4750 loop_iterator li;
4751 struct loop *loop_nest;
4752
4753 FOR_EACH_LOOP (li, loop_nest, 0)
4754 analyze_all_data_dependences (loop_nest);
4755}
6b6f234c 4756
4757/* Free the memory used by a data dependence relation DDR. */
4758
4759void
4760free_dependence_relation (struct data_dependence_relation *ddr)
4761{
4762 if (ddr == NULL)
4763 return;
4764
f1f41a6c 4765 if (DDR_SUBSCRIPTS (ddr).exists ())
87da4f2e 4766 free_subscripts (DDR_SUBSCRIPTS (ddr));
f1f41a6c 4767 DDR_DIST_VECTS (ddr).release ();
4768 DDR_DIR_VECTS (ddr).release ();
41c7a324 4769
6b6f234c 4770 free (ddr);
4771}
4772
4773/* Free the memory used by the data dependence relations from
4774 DEPENDENCE_RELATIONS. */
4775
48e1416a 4776void
f1f41a6c 4777free_dependence_relations (vec<ddr_p> dependence_relations)
6b6f234c 4778{
4779 unsigned int i;
41c7a324 4780 struct data_dependence_relation *ddr;
6b6f234c 4781
f1f41a6c 4782 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
a8af2e86 4783 if (ddr)
62761183 4784 free_dependence_relation (ddr);
41c7a324 4785
f1f41a6c 4786 dependence_relations.release ();
2146e26d 4787}
4788
6b6f234c 4789/* Free the memory used by the data references from DATAREFS. */
4790
4791void
f1f41a6c 4792free_data_refs (vec<data_reference_p> datarefs)
6b6f234c 4793{
4794 unsigned int i;
41c7a324 4795 struct data_reference *dr;
2146e26d 4796
f1f41a6c 4797 FOR_EACH_VEC_ELT (datarefs, i, dr)
31e389a2 4798 free_data_ref (dr);
f1f41a6c 4799 datarefs.release ();
6b6f234c 4800}
bc3c8ad4 4801
74c8f69a 4802\f
4803
801c5610 4804/* Dump vertex I in RDG to FILE. */
74c8f69a 4805
d129ada0 4806static void
801c5610 4807dump_rdg_vertex (FILE *file, struct graph *rdg, int i)
4808{
4809 struct vertex *v = &(rdg->vertices[i]);
4810 struct graph_edge *e;
4811
48e1416a 4812 fprintf (file, "(vertex %d: (%s%s) (in:", i,
801c5610 4813 RDG_MEM_WRITE_STMT (rdg, i) ? "w" : "",
4814 RDG_MEM_READS_STMT (rdg, i) ? "r" : "");
4815
4816 if (v->pred)
4817 for (e = v->pred; e; e = e->pred_next)
4818 fprintf (file, " %d", e->src);
4819
4820 fprintf (file, ") (out:");
4821
4822 if (v->succ)
4823 for (e = v->succ; e; e = e->succ_next)
4824 fprintf (file, " %d", e->dest);
4825
a136cad6 4826 fprintf (file, ")\n");
75a70cf9 4827 print_gimple_stmt (file, RDGV_STMT (v), 0, TDF_VOPS|TDF_MEMSYMS);
801c5610 4828 fprintf (file, ")\n");
4829}
4830
4831/* Call dump_rdg_vertex on stderr. */
4832
4b987fac 4833DEBUG_FUNCTION void
801c5610 4834debug_rdg_vertex (struct graph *rdg, int i)
4835{
4836 dump_rdg_vertex (stderr, rdg, i);
4837}
4838
4839/* Dump component C of RDG to FILE. If DUMPED is non-null, set the
4840 dumped vertices to that bitmap. */
4841
d129ada0 4842static void
4843dump_rdg_component (FILE *file, struct graph *rdg, int c, bitmap dumped)
801c5610 4844{
4845 int i;
4846
4847 fprintf (file, "(%d\n", c);
4848
4849 for (i = 0; i < rdg->n_vertices; i++)
4850 if (rdg->vertices[i].component == c)
4851 {
4852 if (dumped)
4853 bitmap_set_bit (dumped, i);
4854
4855 dump_rdg_vertex (file, rdg, i);
4856 }
4857
4858 fprintf (file, ")\n");
4859}
4860
4861/* Call dump_rdg_vertex on stderr. */
4862
4b987fac 4863DEBUG_FUNCTION void
801c5610 4864debug_rdg_component (struct graph *rdg, int c)
4865{
4866 dump_rdg_component (stderr, rdg, c, NULL);
4867}
4868
4869/* Dump the reduced dependence graph RDG to FILE. */
4870
4871void
4872dump_rdg (FILE *file, struct graph *rdg)
74c8f69a 4873{
4874 int i;
801c5610 4875 bitmap dumped = BITMAP_ALLOC (NULL);
4876
4877 fprintf (file, "(rdg\n");
74c8f69a 4878
4879 for (i = 0; i < rdg->n_vertices; i++)
801c5610 4880 if (!bitmap_bit_p (dumped, i))
4881 dump_rdg_component (file, rdg, rdg->vertices[i].component, dumped);
74c8f69a 4882
801c5610 4883 fprintf (file, ")\n");
4884 BITMAP_FREE (dumped);
74c8f69a 4885}
4886
801c5610 4887/* Call dump_rdg on stderr. */
4888
4b987fac 4889DEBUG_FUNCTION void
801c5610 4890debug_rdg (struct graph *rdg)
4891{
4892 dump_rdg (stderr, rdg);
4893}
74c8f69a 4894
2df6dddb 4895static void
4896dot_rdg_1 (FILE *file, struct graph *rdg)
4897{
4898 int i;
4899
4900 fprintf (file, "digraph RDG {\n");
4901
4902 for (i = 0; i < rdg->n_vertices; i++)
4903 {
4904 struct vertex *v = &(rdg->vertices[i]);
4905 struct graph_edge *e;
4906
4907 /* Highlight reads from memory. */
4908 if (RDG_MEM_READS_STMT (rdg, i))
4909 fprintf (file, "%d [style=filled, fillcolor=green]\n", i);
4910
4911 /* Highlight stores to memory. */
4912 if (RDG_MEM_WRITE_STMT (rdg, i))
4913 fprintf (file, "%d [style=filled, fillcolor=red]\n", i);
4914
4915 if (v->succ)
4916 for (e = v->succ; e; e = e->succ_next)
4917 switch (RDGE_TYPE (e))
4918 {
4919 case input_dd:
4920 fprintf (file, "%d -> %d [label=input] \n", i, e->dest);
4921 break;
4922
4923 case output_dd:
4924 fprintf (file, "%d -> %d [label=output] \n", i, e->dest);
4925 break;
4926
4927 case flow_dd:
4928 /* These are the most common dependences: don't print these. */
4929 fprintf (file, "%d -> %d \n", i, e->dest);
4930 break;
4931
4932 case anti_dd:
4933 fprintf (file, "%d -> %d [label=anti] \n", i, e->dest);
4934 break;
4935
4936 default:
4937 gcc_unreachable ();
4938 }
4939 }
4940
4941 fprintf (file, "}\n\n");
4942}
4943
4944/* Display the Reduced Dependence Graph using dotty. */
4945extern void dot_rdg (struct graph *);
4946
4947DEBUG_FUNCTION void
4948dot_rdg (struct graph *rdg)
4949{
4950 /* When debugging, enable the following code. This cannot be used
4951 in production compilers because it calls "system". */
4952#if 0
4953 FILE *file = fopen ("/tmp/rdg.dot", "w");
4954 gcc_assert (file != NULL);
4955
4956 dot_rdg_1 (file, rdg);
4957 fclose (file);
4958
4959 system ("dotty /tmp/rdg.dot &");
4960#else
4961 dot_rdg_1 (stderr, rdg);
4962#endif
4963}
4964
801c5610 4965/* Returns the index of STMT in RDG. */
4966
4967int
f83623cc 4968rdg_vertex_for_stmt (struct graph *rdg ATTRIBUTE_UNUSED, gimple stmt)
801c5610 4969{
f83623cc 4970 int index = gimple_uid (stmt);
4971 gcc_checking_assert (index == -1 || RDG_STMT (rdg, index) == stmt);
4972 return index;
801c5610 4973}
4974
4975/* Creates an edge in RDG for each distance vector from DDR. The
4976 order that we keep track of in the RDG is the order in which
4977 statements have to be executed. */
4978
4979static void
4980create_rdg_edge_for_ddr (struct graph *rdg, ddr_p ddr)
4981{
4982 struct graph_edge *e;
4983 int va, vb;
4984 data_reference_p dra = DDR_A (ddr);
4985 data_reference_p drb = DDR_B (ddr);
4986 unsigned level = ddr_dependence_level (ddr);
4987
4988 /* For non scalar dependences, when the dependence is REVERSED,
4989 statement B has to be executed before statement A. */
4990 if (level > 0
4991 && !DDR_REVERSED_P (ddr))
74c8f69a 4992 {
801c5610 4993 data_reference_p tmp = dra;
4994 dra = drb;
4995 drb = tmp;
74c8f69a 4996 }
4997
801c5610 4998 va = rdg_vertex_for_stmt (rdg, DR_STMT (dra));
4999 vb = rdg_vertex_for_stmt (rdg, DR_STMT (drb));
5000
5001 if (va < 0 || vb < 0)
5002 return;
74c8f69a 5003
5004 e = add_edge (rdg, va, vb);
5005 e->data = XNEW (struct rdg_edge);
5006
801c5610 5007 RDGE_LEVEL (e) = level;
255b6be7 5008 RDGE_RELATION (e) = ddr;
801c5610 5009
74c8f69a 5010 /* Determines the type of the data dependence. */
5011 if (DR_IS_READ (dra) && DR_IS_READ (drb))
5012 RDGE_TYPE (e) = input_dd;
9ff25603 5013 else if (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))
74c8f69a 5014 RDGE_TYPE (e) = output_dd;
9ff25603 5015 else if (DR_IS_WRITE (dra) && DR_IS_READ (drb))
74c8f69a 5016 RDGE_TYPE (e) = flow_dd;
9ff25603 5017 else if (DR_IS_READ (dra) && DR_IS_WRITE (drb))
74c8f69a 5018 RDGE_TYPE (e) = anti_dd;
5019}
5020
5021/* Creates dependence edges in RDG for all the uses of DEF. IDEF is
5022 the index of DEF in RDG. */
5023
5024static void
5025create_rdg_edges_for_scalar (struct graph *rdg, tree def, int idef)
5026{
5027 use_operand_p imm_use_p;
5028 imm_use_iterator iterator;
48e1416a 5029
74c8f69a 5030 FOR_EACH_IMM_USE_FAST (imm_use_p, iterator, def)
5031 {
801c5610 5032 struct graph_edge *e;
5033 int use = rdg_vertex_for_stmt (rdg, USE_STMT (imm_use_p));
74c8f69a 5034
801c5610 5035 if (use < 0)
5036 continue;
5037
5038 e = add_edge (rdg, idef, use);
74c8f69a 5039 e->data = XNEW (struct rdg_edge);
5040 RDGE_TYPE (e) = flow_dd;
255b6be7 5041 RDGE_RELATION (e) = NULL;
74c8f69a 5042 }
5043}
5044
5045/* Creates the edges of the reduced dependence graph RDG. */
5046
5047static void
f1f41a6c 5048create_rdg_edges (struct graph *rdg, vec<ddr_p> ddrs)
74c8f69a 5049{
5050 int i;
5051 struct data_dependence_relation *ddr;
5052 def_operand_p def_p;
5053 ssa_op_iter iter;
5054
f1f41a6c 5055 FOR_EACH_VEC_ELT (ddrs, i, ddr)
74c8f69a 5056 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
5057 create_rdg_edge_for_ddr (rdg, ddr);
5058
5059 for (i = 0; i < rdg->n_vertices; i++)
801c5610 5060 FOR_EACH_PHI_OR_STMT_DEF (def_p, RDG_STMT (rdg, i),
5061 iter, SSA_OP_DEF)
74c8f69a 5062 create_rdg_edges_for_scalar (rdg, DEF_FROM_PTR (def_p), i);
5063}
5064
5065/* Build the vertices of the reduced dependence graph RDG. */
5066
f83623cc 5067static void
f1f41a6c 5068create_rdg_vertices (struct graph *rdg, vec<gimple> stmts, loop_p loop)
74c8f69a 5069{
801c5610 5070 int i, j;
75a70cf9 5071 gimple stmt;
74c8f69a 5072
f1f41a6c 5073 FOR_EACH_VEC_ELT (stmts, i, stmt)
74c8f69a 5074 {
07e3bcbf 5075 vec<data_ref_loc, va_stack> references;
801c5610 5076 data_ref_loc *ref;
74c8f69a 5077 struct vertex *v = &(rdg->vertices[i]);
801c5610 5078
f83623cc 5079 /* Record statement to vertex mapping. */
5080 gimple_set_uid (stmt, i);
74c8f69a 5081
5082 v->data = XNEW (struct rdg_vertex);
f83623cc 5083 RDGV_STMT (v) = stmt;
f1f41a6c 5084 RDGV_DATAREFS (v).create (0);
f83623cc 5085 RDGV_HAS_MEM_WRITE (v) = false;
5086 RDGV_HAS_MEM_READS (v) = false;
75a70cf9 5087 if (gimple_code (stmt) == GIMPLE_PHI)
801c5610 5088 continue;
5089
07e3bcbf 5090 vec_stack_alloc (data_ref_loc, references, 2);
801c5610 5091 get_references_in_stmt (stmt, &references);
f1f41a6c 5092 FOR_EACH_VEC_ELT (references, j, ref)
f83623cc 5093 {
5094 data_reference_p dr;
5095 if (!ref->is_read)
5096 RDGV_HAS_MEM_WRITE (v) = true;
5097 else
5098 RDGV_HAS_MEM_READS (v) = true;
5099 dr = create_data_ref (loop, loop_containing_stmt (stmt),
5100 *ref->pos, stmt, ref->is_read);
5101 if (dr)
f1f41a6c 5102 RDGV_DATAREFS (v).safe_push (dr);
f83623cc 5103 }
f1f41a6c 5104 references.release ();
74c8f69a 5105 }
5106}
5107
801c5610 5108/* Initialize STMTS with all the statements of LOOP. When
5109 INCLUDE_PHIS is true, include also the PHI nodes. The order in
5110 which we discover statements is important as
5111 generate_loops_for_partition is using the same traversal for
5112 identifying statements. */
74c8f69a 5113
5114static void
f1f41a6c 5115stmts_from_loop (struct loop *loop, vec<gimple> *stmts)
74c8f69a 5116{
5117 unsigned int i;
5118 basic_block *bbs = get_loop_body_in_dom_order (loop);
5119
5120 for (i = 0; i < loop->num_nodes; i++)
5121 {
74c8f69a 5122 basic_block bb = bbs[i];
75a70cf9 5123 gimple_stmt_iterator bsi;
5124 gimple stmt;
74c8f69a 5125
75a70cf9 5126 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
f1f41a6c 5127 stmts->safe_push (gsi_stmt (bsi));
74c8f69a 5128
75a70cf9 5129 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
5130 {
5131 stmt = gsi_stmt (bsi);
8ebd8f29 5132 if (gimple_code (stmt) != GIMPLE_LABEL && !is_gimple_debug (stmt))
f1f41a6c 5133 stmts->safe_push (stmt);
75a70cf9 5134 }
74c8f69a 5135 }
5136
5137 free (bbs);
5138}
5139
5140/* Returns true when all the dependences are computable. */
5141
5142static bool
f1f41a6c 5143known_dependences_p (vec<ddr_p> dependence_relations)
74c8f69a 5144{
5145 ddr_p ddr;
5146 unsigned int i;
5147
f1f41a6c 5148 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
74c8f69a 5149 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
5150 return false;
48e1416a 5151
74c8f69a 5152 return true;
5153}
5154
255b6be7 5155/* Build the Reduced Dependence Graph (RDG) with one vertex per
5156 statement of the loop nest, and one edge per data dependence or
5157 scalar dependence. */
5158
5159struct graph *
5160build_empty_rdg (int n_stmts)
5161{
255b6be7 5162 struct graph *rdg = new_graph (n_stmts);
255b6be7 5163 return rdg;
5164}
5165
801c5610 5166/* Build the Reduced Dependence Graph (RDG) with one vertex per
5167 statement of the loop nest, and one edge per data dependence or
5168 scalar dependence. */
74c8f69a 5169
5170struct graph *
a8af2e86 5171build_rdg (struct loop *loop,
f1f41a6c 5172 vec<loop_p> *loop_nest,
5173 vec<ddr_p> *dependence_relations,
5174 vec<data_reference_p> *datarefs)
74c8f69a 5175{
74c8f69a 5176 struct graph *rdg = NULL;
74c8f69a 5177
96758487 5178 if (compute_data_dependences_for_loop (loop, false, loop_nest, datarefs,
5179 dependence_relations)
5180 && known_dependences_p (*dependence_relations))
a8af2e86 5181 {
f1f41a6c 5182 vec<gimple> stmts;
5183 stmts.create (10);
a8af2e86 5184 stmts_from_loop (loop, &stmts);
f1f41a6c 5185 rdg = build_empty_rdg (stmts.length ());
f83623cc 5186 create_rdg_vertices (rdg, stmts, loop);
a8af2e86 5187 create_rdg_edges (rdg, *dependence_relations);
f1f41a6c 5188 stmts.release ();
a8af2e86 5189 }
74c8f69a 5190
74c8f69a 5191 return rdg;
5192}
801c5610 5193
5194/* Free the reduced dependence graph RDG. */
5195
5196void
5197free_rdg (struct graph *rdg)
5198{
5199 int i;
5200
5201 for (i = 0; i < rdg->n_vertices; i++)
a8af2e86 5202 {
5203 struct vertex *v = &(rdg->vertices[i]);
5204 struct graph_edge *e;
5205
5206 for (e = v->succ; e; e = e->succ_next)
dd045aee 5207 free (e->data);
a8af2e86 5208
f83623cc 5209 gimple_set_uid (RDGV_STMT (v), -1);
5210 free_data_refs (RDGV_DATAREFS (v));
dd045aee 5211 free (v->data);
a8af2e86 5212 }
801c5610 5213
801c5610 5214 free_graph (rdg);
5215}
5216
801c5610 5217/* Determines whether the statement from vertex V of the RDG has a
5218 definition used outside the loop that contains this statement. */
5219
5220bool
5221rdg_defs_used_in_other_loops_p (struct graph *rdg, int v)
5222{
75a70cf9 5223 gimple stmt = RDG_STMT (rdg, v);
801c5610 5224 struct loop *loop = loop_containing_stmt (stmt);
5225 use_operand_p imm_use_p;
5226 imm_use_iterator iterator;
5227 ssa_op_iter it;
5228 def_operand_p def_p;
5229
5230 if (!loop)
5231 return true;
5232
5233 FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, it, SSA_OP_DEF)
5234 {
5235 FOR_EACH_IMM_USE_FAST (imm_use_p, iterator, DEF_FROM_PTR (def_p))
5236 {
5237 if (loop_containing_stmt (USE_STMT (imm_use_p)) != loop)
5238 return true;
5239 }
5240 }
5241
5242 return false;
5243}