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