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