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