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