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