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