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