Daily bump.
[official-gcc.git] / gcc / tree-data-ref.c
blobffa4cbf14d7bca7cf92fa5f5942bc0329e29b91b
1 /* Data references and dependences detectors.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
5 This file is part of GCC.
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.
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.
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/>. */
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.
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).
31 The goals of this analysis are:
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),
37 - when two data references access the same data, to qualify the
38 dependence relation with classic dependence representations:
40 - distance vectors
41 - direction vectors
42 - loop carried level dependence
43 - polyhedron dependence
44 or with the chains of recurrences based representation,
46 - to define a knowledge base for storing the data dependence
47 information,
49 - to define an interface to access this data.
52 Definitions:
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).
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.
64 References:
66 - "Advanced Compilation for High Performance Computing" by Randy
67 Allen and Ken Kennedy.
68 http://citeseer.ist.psu.edu/goff91practical.html
70 - "Loop Transformations for Restructuring Compilers - The Foundations"
71 by Utpal Banerjee.
76 #include "config.h"
77 #include "system.h"
78 #include "coretypes.h"
79 #include "tree.h"
80 #include "gimple-pretty-print.h"
81 #include "gimple.h"
82 #include "tree-ssa-loop-niter.h"
83 #include "tree-ssa-loop.h"
84 #include "tree-ssa.h"
85 #include "cfgloop.h"
86 #include "tree-data-ref.h"
87 #include "tree-scalar-evolution.h"
88 #include "dumpfile.h"
89 #include "langhooks.h"
90 #include "tree-affine.h"
91 #include "params.h"
93 static struct datadep_stats
95 int num_dependence_tests;
96 int num_dependence_dependent;
97 int num_dependence_independent;
98 int num_dependence_undetermined;
100 int num_subscript_tests;
101 int num_subscript_undetermined;
102 int num_same_subscript_function;
104 int num_ziv;
105 int num_ziv_independent;
106 int num_ziv_dependent;
107 int num_ziv_unimplemented;
109 int num_siv;
110 int num_siv_independent;
111 int num_siv_dependent;
112 int num_siv_unimplemented;
114 int num_miv;
115 int num_miv_independent;
116 int num_miv_dependent;
117 int num_miv_unimplemented;
118 } dependence_stats;
120 static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
121 struct data_reference *,
122 struct data_reference *,
123 struct loop *);
124 /* Returns true iff A divides B. */
126 static inline bool
127 tree_fold_divides_p (const_tree a, const_tree b)
129 gcc_assert (TREE_CODE (a) == INTEGER_CST);
130 gcc_assert (TREE_CODE (b) == INTEGER_CST);
131 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a));
134 /* Returns true iff A divides B. */
136 static inline bool
137 int_divides_p (int a, int b)
139 return ((b % a) == 0);
144 /* Dump into FILE all the data references from DATAREFS. */
146 static void
147 dump_data_references (FILE *file, vec<data_reference_p> datarefs)
149 unsigned int i;
150 struct data_reference *dr;
152 FOR_EACH_VEC_ELT (datarefs, i, dr)
153 dump_data_reference (file, dr);
156 /* Unified dump into FILE all the data references from DATAREFS. */
158 DEBUG_FUNCTION void
159 debug (vec<data_reference_p> &ref)
161 dump_data_references (stderr, ref);
164 DEBUG_FUNCTION void
165 debug (vec<data_reference_p> *ptr)
167 if (ptr)
168 debug (*ptr);
169 else
170 fprintf (stderr, "<nil>\n");
174 /* Dump into STDERR all the data references from DATAREFS. */
176 DEBUG_FUNCTION void
177 debug_data_references (vec<data_reference_p> datarefs)
179 dump_data_references (stderr, datarefs);
182 /* Print to STDERR the data_reference DR. */
184 DEBUG_FUNCTION void
185 debug_data_reference (struct data_reference *dr)
187 dump_data_reference (stderr, dr);
190 /* Dump function for a DATA_REFERENCE structure. */
192 void
193 dump_data_reference (FILE *outf,
194 struct data_reference *dr)
196 unsigned int i;
198 fprintf (outf, "#(Data Ref: \n");
199 fprintf (outf, "# bb: %d \n", gimple_bb (DR_STMT (dr))->index);
200 fprintf (outf, "# stmt: ");
201 print_gimple_stmt (outf, DR_STMT (dr), 0, 0);
202 fprintf (outf, "# ref: ");
203 print_generic_stmt (outf, DR_REF (dr), 0);
204 fprintf (outf, "# base_object: ");
205 print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
207 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
209 fprintf (outf, "# Access function %d: ", i);
210 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
212 fprintf (outf, "#)\n");
215 /* Unified dump function for a DATA_REFERENCE structure. */
217 DEBUG_FUNCTION void
218 debug (data_reference &ref)
220 dump_data_reference (stderr, &ref);
223 DEBUG_FUNCTION void
224 debug (data_reference *ptr)
226 if (ptr)
227 debug (*ptr);
228 else
229 fprintf (stderr, "<nil>\n");
233 /* Dumps the affine function described by FN to the file OUTF. */
235 static void
236 dump_affine_function (FILE *outf, affine_fn fn)
238 unsigned i;
239 tree coef;
241 print_generic_expr (outf, fn[0], TDF_SLIM);
242 for (i = 1; fn.iterate (i, &coef); i++)
244 fprintf (outf, " + ");
245 print_generic_expr (outf, coef, TDF_SLIM);
246 fprintf (outf, " * x_%u", i);
250 /* Dumps the conflict function CF to the file OUTF. */
252 static void
253 dump_conflict_function (FILE *outf, conflict_function *cf)
255 unsigned i;
257 if (cf->n == NO_DEPENDENCE)
258 fprintf (outf, "no dependence");
259 else if (cf->n == NOT_KNOWN)
260 fprintf (outf, "not known");
261 else
263 for (i = 0; i < cf->n; i++)
265 if (i != 0)
266 fprintf (outf, " ");
267 fprintf (outf, "[");
268 dump_affine_function (outf, cf->fns[i]);
269 fprintf (outf, "]");
274 /* Dump function for a SUBSCRIPT structure. */
276 static void
277 dump_subscript (FILE *outf, struct subscript *subscript)
279 conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
281 fprintf (outf, "\n (subscript \n");
282 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
283 dump_conflict_function (outf, cf);
284 if (CF_NONTRIVIAL_P (cf))
286 tree last_iteration = SUB_LAST_CONFLICT (subscript);
287 fprintf (outf, "\n last_conflict: ");
288 print_generic_expr (outf, last_iteration, 0);
291 cf = SUB_CONFLICTS_IN_B (subscript);
292 fprintf (outf, "\n iterations_that_access_an_element_twice_in_B: ");
293 dump_conflict_function (outf, cf);
294 if (CF_NONTRIVIAL_P (cf))
296 tree last_iteration = SUB_LAST_CONFLICT (subscript);
297 fprintf (outf, "\n last_conflict: ");
298 print_generic_expr (outf, last_iteration, 0);
301 fprintf (outf, "\n (Subscript distance: ");
302 print_generic_expr (outf, SUB_DISTANCE (subscript), 0);
303 fprintf (outf, " ))\n");
306 /* Print the classic direction vector DIRV to OUTF. */
308 static void
309 print_direction_vector (FILE *outf,
310 lambda_vector dirv,
311 int length)
313 int eq;
315 for (eq = 0; eq < length; eq++)
317 enum data_dependence_direction dir = ((enum data_dependence_direction)
318 dirv[eq]);
320 switch (dir)
322 case dir_positive:
323 fprintf (outf, " +");
324 break;
325 case dir_negative:
326 fprintf (outf, " -");
327 break;
328 case dir_equal:
329 fprintf (outf, " =");
330 break;
331 case dir_positive_or_equal:
332 fprintf (outf, " +=");
333 break;
334 case dir_positive_or_negative:
335 fprintf (outf, " +-");
336 break;
337 case dir_negative_or_equal:
338 fprintf (outf, " -=");
339 break;
340 case dir_star:
341 fprintf (outf, " *");
342 break;
343 default:
344 fprintf (outf, "indep");
345 break;
348 fprintf (outf, "\n");
351 /* Print a vector of direction vectors. */
353 static void
354 print_dir_vectors (FILE *outf, vec<lambda_vector> dir_vects,
355 int length)
357 unsigned j;
358 lambda_vector v;
360 FOR_EACH_VEC_ELT (dir_vects, j, v)
361 print_direction_vector (outf, v, length);
364 /* Print out a vector VEC of length N to OUTFILE. */
366 static inline void
367 print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
369 int i;
371 for (i = 0; i < n; i++)
372 fprintf (outfile, "%3d ", vector[i]);
373 fprintf (outfile, "\n");
376 /* Print a vector of distance vectors. */
378 static void
379 print_dist_vectors (FILE *outf, vec<lambda_vector> dist_vects,
380 int length)
382 unsigned j;
383 lambda_vector v;
385 FOR_EACH_VEC_ELT (dist_vects, j, v)
386 print_lambda_vector (outf, v, length);
389 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
391 static void
392 dump_data_dependence_relation (FILE *outf,
393 struct data_dependence_relation *ddr)
395 struct data_reference *dra, *drb;
397 fprintf (outf, "(Data Dep: \n");
399 if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
401 if (ddr)
403 dra = DDR_A (ddr);
404 drb = DDR_B (ddr);
405 if (dra)
406 dump_data_reference (outf, dra);
407 else
408 fprintf (outf, " (nil)\n");
409 if (drb)
410 dump_data_reference (outf, drb);
411 else
412 fprintf (outf, " (nil)\n");
414 fprintf (outf, " (don't know)\n)\n");
415 return;
418 dra = DDR_A (ddr);
419 drb = DDR_B (ddr);
420 dump_data_reference (outf, dra);
421 dump_data_reference (outf, drb);
423 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
424 fprintf (outf, " (no dependence)\n");
426 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
428 unsigned int i;
429 struct loop *loopi;
431 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
433 fprintf (outf, " access_fn_A: ");
434 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
435 fprintf (outf, " access_fn_B: ");
436 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
437 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
440 fprintf (outf, " inner loop index: %d\n", DDR_INNER_LOOP (ddr));
441 fprintf (outf, " loop nest: (");
442 FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
443 fprintf (outf, "%d ", loopi->num);
444 fprintf (outf, ")\n");
446 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
448 fprintf (outf, " distance_vector: ");
449 print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
450 DDR_NB_LOOPS (ddr));
453 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
455 fprintf (outf, " direction_vector: ");
456 print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
457 DDR_NB_LOOPS (ddr));
461 fprintf (outf, ")\n");
464 /* Debug version. */
466 DEBUG_FUNCTION void
467 debug_data_dependence_relation (struct data_dependence_relation *ddr)
469 dump_data_dependence_relation (stderr, ddr);
472 /* Dump into FILE all the dependence relations from DDRS. */
474 void
475 dump_data_dependence_relations (FILE *file,
476 vec<ddr_p> ddrs)
478 unsigned int i;
479 struct data_dependence_relation *ddr;
481 FOR_EACH_VEC_ELT (ddrs, i, ddr)
482 dump_data_dependence_relation (file, ddr);
485 DEBUG_FUNCTION void
486 debug (vec<ddr_p> &ref)
488 dump_data_dependence_relations (stderr, ref);
491 DEBUG_FUNCTION void
492 debug (vec<ddr_p> *ptr)
494 if (ptr)
495 debug (*ptr);
496 else
497 fprintf (stderr, "<nil>\n");
501 /* Dump to STDERR all the dependence relations from DDRS. */
503 DEBUG_FUNCTION void
504 debug_data_dependence_relations (vec<ddr_p> ddrs)
506 dump_data_dependence_relations (stderr, ddrs);
509 /* Dumps the distance and direction vectors in FILE. DDRS contains
510 the dependence relations, and VECT_SIZE is the size of the
511 dependence vectors, or in other words the number of loops in the
512 considered nest. */
514 static void
515 dump_dist_dir_vectors (FILE *file, vec<ddr_p> ddrs)
517 unsigned int i, j;
518 struct data_dependence_relation *ddr;
519 lambda_vector v;
521 FOR_EACH_VEC_ELT (ddrs, i, ddr)
522 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
524 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), j, v)
526 fprintf (file, "DISTANCE_V (");
527 print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
528 fprintf (file, ")\n");
531 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), j, v)
533 fprintf (file, "DIRECTION_V (");
534 print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
535 fprintf (file, ")\n");
539 fprintf (file, "\n\n");
542 /* Dumps the data dependence relations DDRS in FILE. */
544 static void
545 dump_ddrs (FILE *file, vec<ddr_p> ddrs)
547 unsigned int i;
548 struct data_dependence_relation *ddr;
550 FOR_EACH_VEC_ELT (ddrs, i, ddr)
551 dump_data_dependence_relation (file, ddr);
553 fprintf (file, "\n\n");
556 DEBUG_FUNCTION void
557 debug_ddrs (vec<ddr_p> ddrs)
559 dump_ddrs (stderr, ddrs);
562 /* Helper function for split_constant_offset. Expresses OP0 CODE OP1
563 (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
564 constant of type ssizetype, and returns true. If we cannot do this
565 with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
566 is returned. */
568 static bool
569 split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
570 tree *var, tree *off)
572 tree var0, var1;
573 tree off0, off1;
574 enum tree_code ocode = code;
576 *var = NULL_TREE;
577 *off = NULL_TREE;
579 switch (code)
581 case INTEGER_CST:
582 *var = build_int_cst (type, 0);
583 *off = fold_convert (ssizetype, op0);
584 return true;
586 case POINTER_PLUS_EXPR:
587 ocode = PLUS_EXPR;
588 /* FALLTHROUGH */
589 case PLUS_EXPR:
590 case MINUS_EXPR:
591 split_constant_offset (op0, &var0, &off0);
592 split_constant_offset (op1, &var1, &off1);
593 *var = fold_build2 (code, type, var0, var1);
594 *off = size_binop (ocode, off0, off1);
595 return true;
597 case MULT_EXPR:
598 if (TREE_CODE (op1) != INTEGER_CST)
599 return false;
601 split_constant_offset (op0, &var0, &off0);
602 *var = fold_build2 (MULT_EXPR, type, var0, op1);
603 *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
604 return true;
606 case ADDR_EXPR:
608 tree base, poffset;
609 HOST_WIDE_INT pbitsize, pbitpos;
610 enum machine_mode pmode;
611 int punsignedp, pvolatilep;
613 op0 = TREE_OPERAND (op0, 0);
614 base = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset,
615 &pmode, &punsignedp, &pvolatilep, false);
617 if (pbitpos % BITS_PER_UNIT != 0)
618 return false;
619 base = build_fold_addr_expr (base);
620 off0 = ssize_int (pbitpos / BITS_PER_UNIT);
622 if (poffset)
624 split_constant_offset (poffset, &poffset, &off1);
625 off0 = size_binop (PLUS_EXPR, off0, off1);
626 if (POINTER_TYPE_P (TREE_TYPE (base)))
627 base = fold_build_pointer_plus (base, poffset);
628 else
629 base = fold_build2 (PLUS_EXPR, TREE_TYPE (base), base,
630 fold_convert (TREE_TYPE (base), poffset));
633 var0 = fold_convert (type, base);
635 /* If variable length types are involved, punt, otherwise casts
636 might be converted into ARRAY_REFs in gimplify_conversion.
637 To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
638 possibly no longer appears in current GIMPLE, might resurface.
639 This perhaps could run
640 if (CONVERT_EXPR_P (var0))
642 gimplify_conversion (&var0);
643 // Attempt to fill in any within var0 found ARRAY_REF's
644 // element size from corresponding op embedded ARRAY_REF,
645 // if unsuccessful, just punt.
646 } */
647 while (POINTER_TYPE_P (type))
648 type = TREE_TYPE (type);
649 if (int_size_in_bytes (type) < 0)
650 return false;
652 *var = var0;
653 *off = off0;
654 return true;
657 case SSA_NAME:
659 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
660 enum tree_code subcode;
662 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
663 return false;
665 var0 = gimple_assign_rhs1 (def_stmt);
666 subcode = gimple_assign_rhs_code (def_stmt);
667 var1 = gimple_assign_rhs2 (def_stmt);
669 return split_constant_offset_1 (type, var0, subcode, var1, var, off);
671 CASE_CONVERT:
673 /* We must not introduce undefined overflow, and we must not change the value.
674 Hence we're okay if the inner type doesn't overflow to start with
675 (pointer or signed), the outer type also is an integer or pointer
676 and the outer precision is at least as large as the inner. */
677 tree itype = TREE_TYPE (op0);
678 if ((POINTER_TYPE_P (itype)
679 || (INTEGRAL_TYPE_P (itype) && TYPE_OVERFLOW_UNDEFINED (itype)))
680 && TYPE_PRECISION (type) >= TYPE_PRECISION (itype)
681 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)))
683 split_constant_offset (op0, &var0, off);
684 *var = fold_convert (type, var0);
685 return true;
687 return false;
690 default:
691 return false;
695 /* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
696 will be ssizetype. */
698 void
699 split_constant_offset (tree exp, tree *var, tree *off)
701 tree type = TREE_TYPE (exp), otype, op0, op1, e, o;
702 enum tree_code code;
704 *var = exp;
705 *off = ssize_int (0);
706 STRIP_NOPS (exp);
708 if (tree_is_chrec (exp)
709 || get_gimple_rhs_class (TREE_CODE (exp)) == GIMPLE_TERNARY_RHS)
710 return;
712 otype = TREE_TYPE (exp);
713 code = TREE_CODE (exp);
714 extract_ops_from_tree (exp, &code, &op0, &op1);
715 if (split_constant_offset_1 (otype, op0, code, op1, &e, &o))
717 *var = fold_convert (type, e);
718 *off = o;
722 /* Returns the address ADDR of an object in a canonical shape (without nop
723 casts, and with type of pointer to the object). */
725 static tree
726 canonicalize_base_object_address (tree addr)
728 tree orig = addr;
730 STRIP_NOPS (addr);
732 /* The base address may be obtained by casting from integer, in that case
733 keep the cast. */
734 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
735 return orig;
737 if (TREE_CODE (addr) != ADDR_EXPR)
738 return addr;
740 return build_fold_addr_expr (TREE_OPERAND (addr, 0));
743 /* Analyzes the behavior of the memory reference DR in the innermost loop or
744 basic block that contains it. Returns true if analysis succeed or false
745 otherwise. */
747 bool
748 dr_analyze_innermost (struct data_reference *dr, struct loop *nest)
750 gimple stmt = DR_STMT (dr);
751 struct loop *loop = loop_containing_stmt (stmt);
752 tree ref = DR_REF (dr);
753 HOST_WIDE_INT pbitsize, pbitpos;
754 tree base, poffset;
755 enum machine_mode pmode;
756 int punsignedp, pvolatilep;
757 affine_iv base_iv, offset_iv;
758 tree init, dinit, step;
759 bool in_loop = (loop && loop->num);
761 if (dump_file && (dump_flags & TDF_DETAILS))
762 fprintf (dump_file, "analyze_innermost: ");
764 base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset,
765 &pmode, &punsignedp, &pvolatilep, false);
766 gcc_assert (base != NULL_TREE);
768 if (pbitpos % BITS_PER_UNIT != 0)
770 if (dump_file && (dump_flags & TDF_DETAILS))
771 fprintf (dump_file, "failed: bit offset alignment.\n");
772 return false;
775 if (TREE_CODE (base) == MEM_REF)
777 if (!integer_zerop (TREE_OPERAND (base, 1)))
779 double_int moff = mem_ref_offset (base);
780 tree mofft = double_int_to_tree (sizetype, moff);
781 if (!poffset)
782 poffset = mofft;
783 else
784 poffset = size_binop (PLUS_EXPR, poffset, mofft);
786 base = TREE_OPERAND (base, 0);
788 else
789 base = build_fold_addr_expr (base);
791 if (in_loop)
793 if (!simple_iv (loop, loop_containing_stmt (stmt), base, &base_iv,
794 nest ? true : false))
796 if (nest)
798 if (dump_file && (dump_flags & TDF_DETAILS))
799 fprintf (dump_file, "failed: evolution of base is not"
800 " affine.\n");
801 return false;
803 else
805 base_iv.base = base;
806 base_iv.step = ssize_int (0);
807 base_iv.no_overflow = true;
811 else
813 base_iv.base = base;
814 base_iv.step = ssize_int (0);
815 base_iv.no_overflow = true;
818 if (!poffset)
820 offset_iv.base = ssize_int (0);
821 offset_iv.step = ssize_int (0);
823 else
825 if (!in_loop)
827 offset_iv.base = poffset;
828 offset_iv.step = ssize_int (0);
830 else if (!simple_iv (loop, loop_containing_stmt (stmt),
831 poffset, &offset_iv,
832 nest ? true : false))
834 if (nest)
836 if (dump_file && (dump_flags & TDF_DETAILS))
837 fprintf (dump_file, "failed: evolution of offset is not"
838 " affine.\n");
839 return false;
841 else
843 offset_iv.base = poffset;
844 offset_iv.step = ssize_int (0);
849 init = ssize_int (pbitpos / BITS_PER_UNIT);
850 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
851 init = size_binop (PLUS_EXPR, init, dinit);
852 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
853 init = size_binop (PLUS_EXPR, init, dinit);
855 step = size_binop (PLUS_EXPR,
856 fold_convert (ssizetype, base_iv.step),
857 fold_convert (ssizetype, offset_iv.step));
859 DR_BASE_ADDRESS (dr) = canonicalize_base_object_address (base_iv.base);
861 DR_OFFSET (dr) = fold_convert (ssizetype, offset_iv.base);
862 DR_INIT (dr) = init;
863 DR_STEP (dr) = step;
865 DR_ALIGNED_TO (dr) = size_int (highest_pow2_factor (offset_iv.base));
867 if (dump_file && (dump_flags & TDF_DETAILS))
868 fprintf (dump_file, "success.\n");
870 return true;
873 /* Determines the base object and the list of indices of memory reference
874 DR, analyzed in LOOP and instantiated in loop nest NEST. */
876 static void
877 dr_analyze_indices (struct data_reference *dr, loop_p nest, loop_p loop)
879 vec<tree> access_fns = vNULL;
880 tree ref, op;
881 tree base, off, access_fn;
882 basic_block before_loop;
884 /* If analyzing a basic-block there are no indices to analyze
885 and thus no access functions. */
886 if (!nest)
888 DR_BASE_OBJECT (dr) = DR_REF (dr);
889 DR_ACCESS_FNS (dr).create (0);
890 return;
893 ref = DR_REF (dr);
894 before_loop = block_before_loop (nest);
896 /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
897 into a two element array with a constant index. The base is
898 then just the immediate underlying object. */
899 if (TREE_CODE (ref) == REALPART_EXPR)
901 ref = TREE_OPERAND (ref, 0);
902 access_fns.safe_push (integer_zero_node);
904 else if (TREE_CODE (ref) == IMAGPART_EXPR)
906 ref = TREE_OPERAND (ref, 0);
907 access_fns.safe_push (integer_one_node);
910 /* Analyze access functions of dimensions we know to be independent. */
911 while (handled_component_p (ref))
913 if (TREE_CODE (ref) == ARRAY_REF)
915 op = TREE_OPERAND (ref, 1);
916 access_fn = analyze_scalar_evolution (loop, op);
917 access_fn = instantiate_scev (before_loop, loop, access_fn);
918 access_fns.safe_push (access_fn);
920 else if (TREE_CODE (ref) == COMPONENT_REF
921 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
923 /* For COMPONENT_REFs of records (but not unions!) use the
924 FIELD_DECL offset as constant access function so we can
925 disambiguate a[i].f1 and a[i].f2. */
926 tree off = component_ref_field_offset (ref);
927 off = size_binop (PLUS_EXPR,
928 size_binop (MULT_EXPR,
929 fold_convert (bitsizetype, off),
930 bitsize_int (BITS_PER_UNIT)),
931 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
932 access_fns.safe_push (off);
934 else
935 /* If we have an unhandled component we could not translate
936 to an access function stop analyzing. We have determined
937 our base object in this case. */
938 break;
940 ref = TREE_OPERAND (ref, 0);
943 /* If the address operand of a MEM_REF base has an evolution in the
944 analyzed nest, add it as an additional independent access-function. */
945 if (TREE_CODE (ref) == MEM_REF)
947 op = TREE_OPERAND (ref, 0);
948 access_fn = analyze_scalar_evolution (loop, op);
949 access_fn = instantiate_scev (before_loop, loop, access_fn);
950 if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
952 tree orig_type;
953 tree memoff = TREE_OPERAND (ref, 1);
954 base = initial_condition (access_fn);
955 orig_type = TREE_TYPE (base);
956 STRIP_USELESS_TYPE_CONVERSION (base);
957 split_constant_offset (base, &base, &off);
958 /* Fold the MEM_REF offset into the evolutions initial
959 value to make more bases comparable. */
960 if (!integer_zerop (memoff))
962 off = size_binop (PLUS_EXPR, off,
963 fold_convert (ssizetype, memoff));
964 memoff = build_int_cst (TREE_TYPE (memoff), 0);
966 access_fn = chrec_replace_initial_condition
967 (access_fn, fold_convert (orig_type, off));
968 /* ??? This is still not a suitable base object for
969 dr_may_alias_p - the base object needs to be an
970 access that covers the object as whole. With
971 an evolution in the pointer this cannot be
972 guaranteed.
973 As a band-aid, mark the access so we can special-case
974 it in dr_may_alias_p. */
975 ref = fold_build2_loc (EXPR_LOCATION (ref),
976 MEM_REF, TREE_TYPE (ref),
977 base, memoff);
978 DR_UNCONSTRAINED_BASE (dr) = true;
979 access_fns.safe_push (access_fn);
982 else if (DECL_P (ref))
984 /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
985 ref = build2 (MEM_REF, TREE_TYPE (ref),
986 build_fold_addr_expr (ref),
987 build_int_cst (reference_alias_ptr_type (ref), 0));
990 DR_BASE_OBJECT (dr) = ref;
991 DR_ACCESS_FNS (dr) = access_fns;
994 /* Extracts the alias analysis information from the memory reference DR. */
996 static void
997 dr_analyze_alias (struct data_reference *dr)
999 tree ref = DR_REF (dr);
1000 tree base = get_base_address (ref), addr;
1002 if (INDIRECT_REF_P (base)
1003 || TREE_CODE (base) == MEM_REF)
1005 addr = TREE_OPERAND (base, 0);
1006 if (TREE_CODE (addr) == SSA_NAME)
1007 DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
1011 /* Frees data reference DR. */
1013 void
1014 free_data_ref (data_reference_p dr)
1016 DR_ACCESS_FNS (dr).release ();
1017 free (dr);
1020 /* Analyzes memory reference MEMREF accessed in STMT. The reference
1021 is read if IS_READ is true, write otherwise. Returns the
1022 data_reference description of MEMREF. NEST is the outermost loop
1023 in which the reference should be instantiated, LOOP is the loop in
1024 which the data reference should be analyzed. */
1026 struct data_reference *
1027 create_data_ref (loop_p nest, loop_p loop, tree memref, gimple stmt,
1028 bool is_read)
1030 struct data_reference *dr;
1032 if (dump_file && (dump_flags & TDF_DETAILS))
1034 fprintf (dump_file, "Creating dr for ");
1035 print_generic_expr (dump_file, memref, TDF_SLIM);
1036 fprintf (dump_file, "\n");
1039 dr = XCNEW (struct data_reference);
1040 DR_STMT (dr) = stmt;
1041 DR_REF (dr) = memref;
1042 DR_IS_READ (dr) = is_read;
1044 dr_analyze_innermost (dr, nest);
1045 dr_analyze_indices (dr, nest, loop);
1046 dr_analyze_alias (dr);
1048 if (dump_file && (dump_flags & TDF_DETAILS))
1050 unsigned i;
1051 fprintf (dump_file, "\tbase_address: ");
1052 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1053 fprintf (dump_file, "\n\toffset from base address: ");
1054 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1055 fprintf (dump_file, "\n\tconstant offset from base address: ");
1056 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1057 fprintf (dump_file, "\n\tstep: ");
1058 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1059 fprintf (dump_file, "\n\taligned to: ");
1060 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
1061 fprintf (dump_file, "\n\tbase_object: ");
1062 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1063 fprintf (dump_file, "\n");
1064 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
1066 fprintf (dump_file, "\tAccess function %d: ", i);
1067 print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
1071 return dr;
1074 /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
1075 expressions. */
1076 static bool
1077 dr_equal_offsets_p1 (tree offset1, tree offset2)
1079 bool res;
1081 STRIP_NOPS (offset1);
1082 STRIP_NOPS (offset2);
1084 if (offset1 == offset2)
1085 return true;
1087 if (TREE_CODE (offset1) != TREE_CODE (offset2)
1088 || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
1089 return false;
1091 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
1092 TREE_OPERAND (offset2, 0));
1094 if (!res || !BINARY_CLASS_P (offset1))
1095 return res;
1097 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
1098 TREE_OPERAND (offset2, 1));
1100 return res;
1103 /* Check if DRA and DRB have equal offsets. */
1104 bool
1105 dr_equal_offsets_p (struct data_reference *dra,
1106 struct data_reference *drb)
1108 tree offset1, offset2;
1110 offset1 = DR_OFFSET (dra);
1111 offset2 = DR_OFFSET (drb);
1113 return dr_equal_offsets_p1 (offset1, offset2);
1116 /* Returns true if FNA == FNB. */
1118 static bool
1119 affine_function_equal_p (affine_fn fna, affine_fn fnb)
1121 unsigned i, n = fna.length ();
1123 if (n != fnb.length ())
1124 return false;
1126 for (i = 0; i < n; i++)
1127 if (!operand_equal_p (fna[i], fnb[i], 0))
1128 return false;
1130 return true;
1133 /* If all the functions in CF are the same, returns one of them,
1134 otherwise returns NULL. */
1136 static affine_fn
1137 common_affine_function (conflict_function *cf)
1139 unsigned i;
1140 affine_fn comm;
1142 if (!CF_NONTRIVIAL_P (cf))
1143 return affine_fn ();
1145 comm = cf->fns[0];
1147 for (i = 1; i < cf->n; i++)
1148 if (!affine_function_equal_p (comm, cf->fns[i]))
1149 return affine_fn ();
1151 return comm;
1154 /* Returns the base of the affine function FN. */
1156 static tree
1157 affine_function_base (affine_fn fn)
1159 return fn[0];
1162 /* Returns true if FN is a constant. */
1164 static bool
1165 affine_function_constant_p (affine_fn fn)
1167 unsigned i;
1168 tree coef;
1170 for (i = 1; fn.iterate (i, &coef); i++)
1171 if (!integer_zerop (coef))
1172 return false;
1174 return true;
1177 /* Returns true if FN is the zero constant function. */
1179 static bool
1180 affine_function_zero_p (affine_fn fn)
1182 return (integer_zerop (affine_function_base (fn))
1183 && affine_function_constant_p (fn));
1186 /* Returns a signed integer type with the largest precision from TA
1187 and TB. */
1189 static tree
1190 signed_type_for_types (tree ta, tree tb)
1192 if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
1193 return signed_type_for (ta);
1194 else
1195 return signed_type_for (tb);
1198 /* Applies operation OP on affine functions FNA and FNB, and returns the
1199 result. */
1201 static affine_fn
1202 affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
1204 unsigned i, n, m;
1205 affine_fn ret;
1206 tree coef;
1208 if (fnb.length () > fna.length ())
1210 n = fna.length ();
1211 m = fnb.length ();
1213 else
1215 n = fnb.length ();
1216 m = fna.length ();
1219 ret.create (m);
1220 for (i = 0; i < n; i++)
1222 tree type = signed_type_for_types (TREE_TYPE (fna[i]),
1223 TREE_TYPE (fnb[i]));
1224 ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
1227 for (; fna.iterate (i, &coef); i++)
1228 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1229 coef, integer_zero_node));
1230 for (; fnb.iterate (i, &coef); i++)
1231 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1232 integer_zero_node, coef));
1234 return ret;
1237 /* Returns the sum of affine functions FNA and FNB. */
1239 static affine_fn
1240 affine_fn_plus (affine_fn fna, affine_fn fnb)
1242 return affine_fn_op (PLUS_EXPR, fna, fnb);
1245 /* Returns the difference of affine functions FNA and FNB. */
1247 static affine_fn
1248 affine_fn_minus (affine_fn fna, affine_fn fnb)
1250 return affine_fn_op (MINUS_EXPR, fna, fnb);
1253 /* Frees affine function FN. */
1255 static void
1256 affine_fn_free (affine_fn fn)
1258 fn.release ();
1261 /* Determine for each subscript in the data dependence relation DDR
1262 the distance. */
1264 static void
1265 compute_subscript_distance (struct data_dependence_relation *ddr)
1267 conflict_function *cf_a, *cf_b;
1268 affine_fn fn_a, fn_b, diff;
1270 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1272 unsigned int i;
1274 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1276 struct subscript *subscript;
1278 subscript = DDR_SUBSCRIPT (ddr, i);
1279 cf_a = SUB_CONFLICTS_IN_A (subscript);
1280 cf_b = SUB_CONFLICTS_IN_B (subscript);
1282 fn_a = common_affine_function (cf_a);
1283 fn_b = common_affine_function (cf_b);
1284 if (!fn_a.exists () || !fn_b.exists ())
1286 SUB_DISTANCE (subscript) = chrec_dont_know;
1287 return;
1289 diff = affine_fn_minus (fn_a, fn_b);
1291 if (affine_function_constant_p (diff))
1292 SUB_DISTANCE (subscript) = affine_function_base (diff);
1293 else
1294 SUB_DISTANCE (subscript) = chrec_dont_know;
1296 affine_fn_free (diff);
1301 /* Returns the conflict function for "unknown". */
1303 static conflict_function *
1304 conflict_fn_not_known (void)
1306 conflict_function *fn = XCNEW (conflict_function);
1307 fn->n = NOT_KNOWN;
1309 return fn;
1312 /* Returns the conflict function for "independent". */
1314 static conflict_function *
1315 conflict_fn_no_dependence (void)
1317 conflict_function *fn = XCNEW (conflict_function);
1318 fn->n = NO_DEPENDENCE;
1320 return fn;
1323 /* Returns true if the address of OBJ is invariant in LOOP. */
1325 static bool
1326 object_address_invariant_in_loop_p (const struct loop *loop, const_tree obj)
1328 while (handled_component_p (obj))
1330 if (TREE_CODE (obj) == ARRAY_REF)
1332 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1333 need to check the stride and the lower bound of the reference. */
1334 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1335 loop->num)
1336 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 3),
1337 loop->num))
1338 return false;
1340 else if (TREE_CODE (obj) == COMPONENT_REF)
1342 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1343 loop->num))
1344 return false;
1346 obj = TREE_OPERAND (obj, 0);
1349 if (!INDIRECT_REF_P (obj)
1350 && TREE_CODE (obj) != MEM_REF)
1351 return true;
1353 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
1354 loop->num);
1357 /* Returns false if we can prove that data references A and B do not alias,
1358 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
1359 considered. */
1361 bool
1362 dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
1363 bool loop_nest)
1365 tree addr_a = DR_BASE_OBJECT (a);
1366 tree addr_b = DR_BASE_OBJECT (b);
1368 /* If we are not processing a loop nest but scalar code we
1369 do not need to care about possible cross-iteration dependences
1370 and thus can process the full original reference. Do so,
1371 similar to how loop invariant motion applies extra offset-based
1372 disambiguation. */
1373 if (!loop_nest)
1375 aff_tree off1, off2;
1376 double_int size1, size2;
1377 get_inner_reference_aff (DR_REF (a), &off1, &size1);
1378 get_inner_reference_aff (DR_REF (b), &off2, &size2);
1379 aff_combination_scale (&off1, double_int_minus_one);
1380 aff_combination_add (&off2, &off1);
1381 if (aff_comb_cannot_overlap_p (&off2, size1, size2))
1382 return false;
1385 /* If we had an evolution in a MEM_REF BASE_OBJECT we do not know
1386 the size of the base-object. So we cannot do any offset/overlap
1387 based analysis but have to rely on points-to information only. */
1388 if (TREE_CODE (addr_a) == MEM_REF
1389 && DR_UNCONSTRAINED_BASE (a))
1391 if (TREE_CODE (addr_b) == MEM_REF
1392 && DR_UNCONSTRAINED_BASE (b))
1393 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1394 TREE_OPERAND (addr_b, 0));
1395 else
1396 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1397 build_fold_addr_expr (addr_b));
1399 else if (TREE_CODE (addr_b) == MEM_REF
1400 && DR_UNCONSTRAINED_BASE (b))
1401 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
1402 TREE_OPERAND (addr_b, 0));
1404 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1405 that is being subsetted in the loop nest. */
1406 if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
1407 return refs_output_dependent_p (addr_a, addr_b);
1408 else if (DR_IS_READ (a) && DR_IS_WRITE (b))
1409 return refs_anti_dependent_p (addr_a, addr_b);
1410 return refs_may_alias_p (addr_a, addr_b);
1413 /* Initialize a data dependence relation between data accesses A and
1414 B. NB_LOOPS is the number of loops surrounding the references: the
1415 size of the classic distance/direction vectors. */
1417 struct data_dependence_relation *
1418 initialize_data_dependence_relation (struct data_reference *a,
1419 struct data_reference *b,
1420 vec<loop_p> loop_nest)
1422 struct data_dependence_relation *res;
1423 unsigned int i;
1425 res = XNEW (struct data_dependence_relation);
1426 DDR_A (res) = a;
1427 DDR_B (res) = b;
1428 DDR_LOOP_NEST (res).create (0);
1429 DDR_REVERSED_P (res) = false;
1430 DDR_SUBSCRIPTS (res).create (0);
1431 DDR_DIR_VECTS (res).create (0);
1432 DDR_DIST_VECTS (res).create (0);
1434 if (a == NULL || b == NULL)
1436 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1437 return res;
1440 /* If the data references do not alias, then they are independent. */
1441 if (!dr_may_alias_p (a, b, loop_nest.exists ()))
1443 DDR_ARE_DEPENDENT (res) = chrec_known;
1444 return res;
1447 /* The case where the references are exactly the same. */
1448 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
1450 if (loop_nest.exists ()
1451 && !object_address_invariant_in_loop_p (loop_nest[0],
1452 DR_BASE_OBJECT (a)))
1454 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1455 return res;
1457 DDR_AFFINE_P (res) = true;
1458 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1459 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1460 DDR_LOOP_NEST (res) = loop_nest;
1461 DDR_INNER_LOOP (res) = 0;
1462 DDR_SELF_REFERENCE (res) = true;
1463 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1465 struct subscript *subscript;
1467 subscript = XNEW (struct subscript);
1468 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1469 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1470 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1471 SUB_DISTANCE (subscript) = chrec_dont_know;
1472 DDR_SUBSCRIPTS (res).safe_push (subscript);
1474 return res;
1477 /* If the references do not access the same object, we do not know
1478 whether they alias or not. */
1479 if (!operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0))
1481 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1482 return res;
1485 /* If the base of the object is not invariant in the loop nest, we cannot
1486 analyze it. TODO -- in fact, it would suffice to record that there may
1487 be arbitrary dependences in the loops where the base object varies. */
1488 if (loop_nest.exists ()
1489 && !object_address_invariant_in_loop_p (loop_nest[0],
1490 DR_BASE_OBJECT (a)))
1492 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1493 return res;
1496 /* If the number of dimensions of the access to not agree we can have
1497 a pointer access to a component of the array element type and an
1498 array access while the base-objects are still the same. Punt. */
1499 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1501 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1502 return res;
1505 DDR_AFFINE_P (res) = true;
1506 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1507 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1508 DDR_LOOP_NEST (res) = loop_nest;
1509 DDR_INNER_LOOP (res) = 0;
1510 DDR_SELF_REFERENCE (res) = false;
1512 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1514 struct subscript *subscript;
1516 subscript = XNEW (struct subscript);
1517 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1518 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1519 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1520 SUB_DISTANCE (subscript) = chrec_dont_know;
1521 DDR_SUBSCRIPTS (res).safe_push (subscript);
1524 return res;
1527 /* Frees memory used by the conflict function F. */
1529 static void
1530 free_conflict_function (conflict_function *f)
1532 unsigned i;
1534 if (CF_NONTRIVIAL_P (f))
1536 for (i = 0; i < f->n; i++)
1537 affine_fn_free (f->fns[i]);
1539 free (f);
1542 /* Frees memory used by SUBSCRIPTS. */
1544 static void
1545 free_subscripts (vec<subscript_p> subscripts)
1547 unsigned i;
1548 subscript_p s;
1550 FOR_EACH_VEC_ELT (subscripts, i, s)
1552 free_conflict_function (s->conflicting_iterations_in_a);
1553 free_conflict_function (s->conflicting_iterations_in_b);
1554 free (s);
1556 subscripts.release ();
1559 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1560 description. */
1562 static inline void
1563 finalize_ddr_dependent (struct data_dependence_relation *ddr,
1564 tree chrec)
1566 DDR_ARE_DEPENDENT (ddr) = chrec;
1567 free_subscripts (DDR_SUBSCRIPTS (ddr));
1568 DDR_SUBSCRIPTS (ddr).create (0);
1571 /* The dependence relation DDR cannot be represented by a distance
1572 vector. */
1574 static inline void
1575 non_affine_dependence_relation (struct data_dependence_relation *ddr)
1577 if (dump_file && (dump_flags & TDF_DETAILS))
1578 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
1580 DDR_AFFINE_P (ddr) = false;
1585 /* This section contains the classic Banerjee tests. */
1587 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1588 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1590 static inline bool
1591 ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1593 return (evolution_function_is_constant_p (chrec_a)
1594 && evolution_function_is_constant_p (chrec_b));
1597 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
1598 variable, i.e., if the SIV (Single Index Variable) test is true. */
1600 static bool
1601 siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1603 if ((evolution_function_is_constant_p (chrec_a)
1604 && evolution_function_is_univariate_p (chrec_b))
1605 || (evolution_function_is_constant_p (chrec_b)
1606 && evolution_function_is_univariate_p (chrec_a)))
1607 return true;
1609 if (evolution_function_is_univariate_p (chrec_a)
1610 && evolution_function_is_univariate_p (chrec_b))
1612 switch (TREE_CODE (chrec_a))
1614 case POLYNOMIAL_CHREC:
1615 switch (TREE_CODE (chrec_b))
1617 case POLYNOMIAL_CHREC:
1618 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
1619 return false;
1621 default:
1622 return true;
1625 default:
1626 return true;
1630 return false;
1633 /* Creates a conflict function with N dimensions. The affine functions
1634 in each dimension follow. */
1636 static conflict_function *
1637 conflict_fn (unsigned n, ...)
1639 unsigned i;
1640 conflict_function *ret = XCNEW (conflict_function);
1641 va_list ap;
1643 gcc_assert (0 < n && n <= MAX_DIM);
1644 va_start (ap, n);
1646 ret->n = n;
1647 for (i = 0; i < n; i++)
1648 ret->fns[i] = va_arg (ap, affine_fn);
1649 va_end (ap);
1651 return ret;
1654 /* Returns constant affine function with value CST. */
1656 static affine_fn
1657 affine_fn_cst (tree cst)
1659 affine_fn fn;
1660 fn.create (1);
1661 fn.quick_push (cst);
1662 return fn;
1665 /* Returns affine function with single variable, CST + COEF * x_DIM. */
1667 static affine_fn
1668 affine_fn_univar (tree cst, unsigned dim, tree coef)
1670 affine_fn fn;
1671 fn.create (dim + 1);
1672 unsigned i;
1674 gcc_assert (dim > 0);
1675 fn.quick_push (cst);
1676 for (i = 1; i < dim; i++)
1677 fn.quick_push (integer_zero_node);
1678 fn.quick_push (coef);
1679 return fn;
1682 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1683 *OVERLAPS_B are initialized to the functions that describe the
1684 relation between the elements accessed twice by CHREC_A and
1685 CHREC_B. For k >= 0, the following property is verified:
1687 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1689 static void
1690 analyze_ziv_subscript (tree chrec_a,
1691 tree chrec_b,
1692 conflict_function **overlaps_a,
1693 conflict_function **overlaps_b,
1694 tree *last_conflicts)
1696 tree type, difference;
1697 dependence_stats.num_ziv++;
1699 if (dump_file && (dump_flags & TDF_DETAILS))
1700 fprintf (dump_file, "(analyze_ziv_subscript \n");
1702 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1703 chrec_a = chrec_convert (type, chrec_a, NULL);
1704 chrec_b = chrec_convert (type, chrec_b, NULL);
1705 difference = chrec_fold_minus (type, chrec_a, chrec_b);
1707 switch (TREE_CODE (difference))
1709 case INTEGER_CST:
1710 if (integer_zerop (difference))
1712 /* The difference is equal to zero: the accessed index
1713 overlaps for each iteration in the loop. */
1714 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1715 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1716 *last_conflicts = chrec_dont_know;
1717 dependence_stats.num_ziv_dependent++;
1719 else
1721 /* The accesses do not overlap. */
1722 *overlaps_a = conflict_fn_no_dependence ();
1723 *overlaps_b = conflict_fn_no_dependence ();
1724 *last_conflicts = integer_zero_node;
1725 dependence_stats.num_ziv_independent++;
1727 break;
1729 default:
1730 /* We're not sure whether the indexes overlap. For the moment,
1731 conservatively answer "don't know". */
1732 if (dump_file && (dump_flags & TDF_DETAILS))
1733 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
1735 *overlaps_a = conflict_fn_not_known ();
1736 *overlaps_b = conflict_fn_not_known ();
1737 *last_conflicts = chrec_dont_know;
1738 dependence_stats.num_ziv_unimplemented++;
1739 break;
1742 if (dump_file && (dump_flags & TDF_DETAILS))
1743 fprintf (dump_file, ")\n");
1746 /* Similar to max_stmt_executions_int, but returns the bound as a tree,
1747 and only if it fits to the int type. If this is not the case, or the
1748 bound on the number of iterations of LOOP could not be derived, returns
1749 chrec_dont_know. */
1751 static tree
1752 max_stmt_executions_tree (struct loop *loop)
1754 double_int nit;
1756 if (!max_stmt_executions (loop, &nit))
1757 return chrec_dont_know;
1759 if (!double_int_fits_to_tree_p (unsigned_type_node, nit))
1760 return chrec_dont_know;
1762 return double_int_to_tree (unsigned_type_node, nit);
1765 /* Determine whether the CHREC is always positive/negative. If the expression
1766 cannot be statically analyzed, return false, otherwise set the answer into
1767 VALUE. */
1769 static bool
1770 chrec_is_positive (tree chrec, bool *value)
1772 bool value0, value1, value2;
1773 tree end_value, nb_iter;
1775 switch (TREE_CODE (chrec))
1777 case POLYNOMIAL_CHREC:
1778 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
1779 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
1780 return false;
1782 /* FIXME -- overflows. */
1783 if (value0 == value1)
1785 *value = value0;
1786 return true;
1789 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1790 and the proof consists in showing that the sign never
1791 changes during the execution of the loop, from 0 to
1792 loop->nb_iterations. */
1793 if (!evolution_function_is_affine_p (chrec))
1794 return false;
1796 nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
1797 if (chrec_contains_undetermined (nb_iter))
1798 return false;
1800 #if 0
1801 /* TODO -- If the test is after the exit, we may decrease the number of
1802 iterations by one. */
1803 if (after_exit)
1804 nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
1805 #endif
1807 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
1809 if (!chrec_is_positive (end_value, &value2))
1810 return false;
1812 *value = value0;
1813 return value0 == value1;
1815 case INTEGER_CST:
1816 switch (tree_int_cst_sgn (chrec))
1818 case -1:
1819 *value = false;
1820 break;
1821 case 1:
1822 *value = true;
1823 break;
1824 default:
1825 return false;
1827 return true;
1829 default:
1830 return false;
1835 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1836 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1837 *OVERLAPS_B are initialized to the functions that describe the
1838 relation between the elements accessed twice by CHREC_A and
1839 CHREC_B. For k >= 0, the following property is verified:
1841 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1843 static void
1844 analyze_siv_subscript_cst_affine (tree chrec_a,
1845 tree chrec_b,
1846 conflict_function **overlaps_a,
1847 conflict_function **overlaps_b,
1848 tree *last_conflicts)
1850 bool value0, value1, value2;
1851 tree type, difference, tmp;
1853 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1854 chrec_a = chrec_convert (type, chrec_a, NULL);
1855 chrec_b = chrec_convert (type, chrec_b, NULL);
1856 difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
1858 /* Special case overlap in the first iteration. */
1859 if (integer_zerop (difference))
1861 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1862 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1863 *last_conflicts = integer_one_node;
1864 return;
1867 if (!chrec_is_positive (initial_condition (difference), &value0))
1869 if (dump_file && (dump_flags & TDF_DETAILS))
1870 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
1872 dependence_stats.num_siv_unimplemented++;
1873 *overlaps_a = conflict_fn_not_known ();
1874 *overlaps_b = conflict_fn_not_known ();
1875 *last_conflicts = chrec_dont_know;
1876 return;
1878 else
1880 if (value0 == false)
1882 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
1884 if (dump_file && (dump_flags & TDF_DETAILS))
1885 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1887 *overlaps_a = conflict_fn_not_known ();
1888 *overlaps_b = conflict_fn_not_known ();
1889 *last_conflicts = chrec_dont_know;
1890 dependence_stats.num_siv_unimplemented++;
1891 return;
1893 else
1895 if (value1 == true)
1897 /* Example:
1898 chrec_a = 12
1899 chrec_b = {10, +, 1}
1902 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1904 HOST_WIDE_INT numiter;
1905 struct loop *loop = get_chrec_loop (chrec_b);
1907 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1908 tmp = fold_build2 (EXACT_DIV_EXPR, type,
1909 fold_build1 (ABS_EXPR, type, difference),
1910 CHREC_RIGHT (chrec_b));
1911 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1912 *last_conflicts = integer_one_node;
1915 /* Perform weak-zero siv test to see if overlap is
1916 outside the loop bounds. */
1917 numiter = max_stmt_executions_int (loop);
1919 if (numiter >= 0
1920 && compare_tree_int (tmp, numiter) > 0)
1922 free_conflict_function (*overlaps_a);
1923 free_conflict_function (*overlaps_b);
1924 *overlaps_a = conflict_fn_no_dependence ();
1925 *overlaps_b = conflict_fn_no_dependence ();
1926 *last_conflicts = integer_zero_node;
1927 dependence_stats.num_siv_independent++;
1928 return;
1930 dependence_stats.num_siv_dependent++;
1931 return;
1934 /* When the step does not divide the difference, there are
1935 no overlaps. */
1936 else
1938 *overlaps_a = conflict_fn_no_dependence ();
1939 *overlaps_b = conflict_fn_no_dependence ();
1940 *last_conflicts = integer_zero_node;
1941 dependence_stats.num_siv_independent++;
1942 return;
1946 else
1948 /* Example:
1949 chrec_a = 12
1950 chrec_b = {10, +, -1}
1952 In this case, chrec_a will not overlap with chrec_b. */
1953 *overlaps_a = conflict_fn_no_dependence ();
1954 *overlaps_b = conflict_fn_no_dependence ();
1955 *last_conflicts = integer_zero_node;
1956 dependence_stats.num_siv_independent++;
1957 return;
1961 else
1963 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
1965 if (dump_file && (dump_flags & TDF_DETAILS))
1966 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1968 *overlaps_a = conflict_fn_not_known ();
1969 *overlaps_b = conflict_fn_not_known ();
1970 *last_conflicts = chrec_dont_know;
1971 dependence_stats.num_siv_unimplemented++;
1972 return;
1974 else
1976 if (value2 == false)
1978 /* Example:
1979 chrec_a = 3
1980 chrec_b = {10, +, -1}
1982 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1984 HOST_WIDE_INT numiter;
1985 struct loop *loop = get_chrec_loop (chrec_b);
1987 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1988 tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
1989 CHREC_RIGHT (chrec_b));
1990 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1991 *last_conflicts = integer_one_node;
1993 /* Perform weak-zero siv test to see if overlap is
1994 outside the loop bounds. */
1995 numiter = max_stmt_executions_int (loop);
1997 if (numiter >= 0
1998 && compare_tree_int (tmp, numiter) > 0)
2000 free_conflict_function (*overlaps_a);
2001 free_conflict_function (*overlaps_b);
2002 *overlaps_a = conflict_fn_no_dependence ();
2003 *overlaps_b = conflict_fn_no_dependence ();
2004 *last_conflicts = integer_zero_node;
2005 dependence_stats.num_siv_independent++;
2006 return;
2008 dependence_stats.num_siv_dependent++;
2009 return;
2012 /* When the step does not divide the difference, there
2013 are no overlaps. */
2014 else
2016 *overlaps_a = conflict_fn_no_dependence ();
2017 *overlaps_b = conflict_fn_no_dependence ();
2018 *last_conflicts = integer_zero_node;
2019 dependence_stats.num_siv_independent++;
2020 return;
2023 else
2025 /* Example:
2026 chrec_a = 3
2027 chrec_b = {4, +, 1}
2029 In this case, chrec_a will not overlap with chrec_b. */
2030 *overlaps_a = conflict_fn_no_dependence ();
2031 *overlaps_b = conflict_fn_no_dependence ();
2032 *last_conflicts = integer_zero_node;
2033 dependence_stats.num_siv_independent++;
2034 return;
2041 /* Helper recursive function for initializing the matrix A. Returns
2042 the initial value of CHREC. */
2044 static tree
2045 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2047 gcc_assert (chrec);
2049 switch (TREE_CODE (chrec))
2051 case POLYNOMIAL_CHREC:
2052 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST);
2054 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2055 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2057 case PLUS_EXPR:
2058 case MULT_EXPR:
2059 case MINUS_EXPR:
2061 tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2062 tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
2064 return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
2067 case NOP_EXPR:
2069 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2070 return chrec_convert (chrec_type (chrec), op, NULL);
2073 case BIT_NOT_EXPR:
2075 /* Handle ~X as -1 - X. */
2076 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2077 return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
2078 build_int_cst (TREE_TYPE (chrec), -1), op);
2081 case INTEGER_CST:
2082 return chrec;
2084 default:
2085 gcc_unreachable ();
2086 return NULL_TREE;
2090 #define FLOOR_DIV(x,y) ((x) / (y))
2092 /* Solves the special case of the Diophantine equation:
2093 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2095 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2096 number of iterations that loops X and Y run. The overlaps will be
2097 constructed as evolutions in dimension DIM. */
2099 static void
2100 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2101 affine_fn *overlaps_a,
2102 affine_fn *overlaps_b,
2103 tree *last_conflicts, int dim)
2105 if (((step_a > 0 && step_b > 0)
2106 || (step_a < 0 && step_b < 0)))
2108 int step_overlaps_a, step_overlaps_b;
2109 int gcd_steps_a_b, last_conflict, tau2;
2111 gcd_steps_a_b = gcd (step_a, step_b);
2112 step_overlaps_a = step_b / gcd_steps_a_b;
2113 step_overlaps_b = step_a / gcd_steps_a_b;
2115 if (niter > 0)
2117 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2118 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2119 last_conflict = tau2;
2120 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2122 else
2123 *last_conflicts = chrec_dont_know;
2125 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
2126 build_int_cst (NULL_TREE,
2127 step_overlaps_a));
2128 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
2129 build_int_cst (NULL_TREE,
2130 step_overlaps_b));
2133 else
2135 *overlaps_a = affine_fn_cst (integer_zero_node);
2136 *overlaps_b = affine_fn_cst (integer_zero_node);
2137 *last_conflicts = integer_zero_node;
2141 /* Solves the special case of a Diophantine equation where CHREC_A is
2142 an affine bivariate function, and CHREC_B is an affine univariate
2143 function. For example,
2145 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2147 has the following overlapping functions:
2149 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2150 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2151 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2153 FORNOW: This is a specialized implementation for a case occurring in
2154 a common benchmark. Implement the general algorithm. */
2156 static void
2157 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2158 conflict_function **overlaps_a,
2159 conflict_function **overlaps_b,
2160 tree *last_conflicts)
2162 bool xz_p, yz_p, xyz_p;
2163 int step_x, step_y, step_z;
2164 HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
2165 affine_fn overlaps_a_xz, overlaps_b_xz;
2166 affine_fn overlaps_a_yz, overlaps_b_yz;
2167 affine_fn overlaps_a_xyz, overlaps_b_xyz;
2168 affine_fn ova1, ova2, ovb;
2169 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
2171 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2172 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2173 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2175 niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
2176 niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
2177 niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
2179 if (niter_x < 0 || niter_y < 0 || niter_z < 0)
2181 if (dump_file && (dump_flags & TDF_DETAILS))
2182 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
2184 *overlaps_a = conflict_fn_not_known ();
2185 *overlaps_b = conflict_fn_not_known ();
2186 *last_conflicts = chrec_dont_know;
2187 return;
2190 niter = MIN (niter_x, niter_z);
2191 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2192 &overlaps_a_xz,
2193 &overlaps_b_xz,
2194 &last_conflicts_xz, 1);
2195 niter = MIN (niter_y, niter_z);
2196 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2197 &overlaps_a_yz,
2198 &overlaps_b_yz,
2199 &last_conflicts_yz, 2);
2200 niter = MIN (niter_x, niter_z);
2201 niter = MIN (niter_y, niter);
2202 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2203 &overlaps_a_xyz,
2204 &overlaps_b_xyz,
2205 &last_conflicts_xyz, 3);
2207 xz_p = !integer_zerop (last_conflicts_xz);
2208 yz_p = !integer_zerop (last_conflicts_yz);
2209 xyz_p = !integer_zerop (last_conflicts_xyz);
2211 if (xz_p || yz_p || xyz_p)
2213 ova1 = affine_fn_cst (integer_zero_node);
2214 ova2 = affine_fn_cst (integer_zero_node);
2215 ovb = affine_fn_cst (integer_zero_node);
2216 if (xz_p)
2218 affine_fn t0 = ova1;
2219 affine_fn t2 = ovb;
2221 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
2222 ovb = affine_fn_plus (ovb, overlaps_b_xz);
2223 affine_fn_free (t0);
2224 affine_fn_free (t2);
2225 *last_conflicts = last_conflicts_xz;
2227 if (yz_p)
2229 affine_fn t0 = ova2;
2230 affine_fn t2 = ovb;
2232 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
2233 ovb = affine_fn_plus (ovb, overlaps_b_yz);
2234 affine_fn_free (t0);
2235 affine_fn_free (t2);
2236 *last_conflicts = last_conflicts_yz;
2238 if (xyz_p)
2240 affine_fn t0 = ova1;
2241 affine_fn t2 = ova2;
2242 affine_fn t4 = ovb;
2244 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
2245 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
2246 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
2247 affine_fn_free (t0);
2248 affine_fn_free (t2);
2249 affine_fn_free (t4);
2250 *last_conflicts = last_conflicts_xyz;
2252 *overlaps_a = conflict_fn (2, ova1, ova2);
2253 *overlaps_b = conflict_fn (1, ovb);
2255 else
2257 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2258 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2259 *last_conflicts = integer_zero_node;
2262 affine_fn_free (overlaps_a_xz);
2263 affine_fn_free (overlaps_b_xz);
2264 affine_fn_free (overlaps_a_yz);
2265 affine_fn_free (overlaps_b_yz);
2266 affine_fn_free (overlaps_a_xyz);
2267 affine_fn_free (overlaps_b_xyz);
2270 /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2272 static void
2273 lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
2274 int size)
2276 memcpy (vec2, vec1, size * sizeof (*vec1));
2279 /* Copy the elements of M x N matrix MAT1 to MAT2. */
2281 static void
2282 lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
2283 int m, int n)
2285 int i;
2287 for (i = 0; i < m; i++)
2288 lambda_vector_copy (mat1[i], mat2[i], n);
2291 /* Store the N x N identity matrix in MAT. */
2293 static void
2294 lambda_matrix_id (lambda_matrix mat, int size)
2296 int i, j;
2298 for (i = 0; i < size; i++)
2299 for (j = 0; j < size; j++)
2300 mat[i][j] = (i == j) ? 1 : 0;
2303 /* Return the first nonzero element of vector VEC1 between START and N.
2304 We must have START <= N. Returns N if VEC1 is the zero vector. */
2306 static int
2307 lambda_vector_first_nz (lambda_vector vec1, int n, int start)
2309 int j = start;
2310 while (j < n && vec1[j] == 0)
2311 j++;
2312 return j;
2315 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2316 R2 = R2 + CONST1 * R1. */
2318 static void
2319 lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2, int const1)
2321 int i;
2323 if (const1 == 0)
2324 return;
2326 for (i = 0; i < n; i++)
2327 mat[r2][i] += const1 * mat[r1][i];
2330 /* Swap rows R1 and R2 in matrix MAT. */
2332 static void
2333 lambda_matrix_row_exchange (lambda_matrix mat, int r1, int r2)
2335 lambda_vector row;
2337 row = mat[r1];
2338 mat[r1] = mat[r2];
2339 mat[r2] = row;
2342 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
2343 and store the result in VEC2. */
2345 static void
2346 lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
2347 int size, int const1)
2349 int i;
2351 if (const1 == 0)
2352 lambda_vector_clear (vec2, size);
2353 else
2354 for (i = 0; i < size; i++)
2355 vec2[i] = const1 * vec1[i];
2358 /* Negate vector VEC1 with length SIZE and store it in VEC2. */
2360 static void
2361 lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
2362 int size)
2364 lambda_vector_mult_const (vec1, vec2, size, -1);
2367 /* Negate row R1 of matrix MAT which has N columns. */
2369 static void
2370 lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
2372 lambda_vector_negate (mat[r1], mat[r1], n);
2375 /* Return true if two vectors are equal. */
2377 static bool
2378 lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
2380 int i;
2381 for (i = 0; i < size; i++)
2382 if (vec1[i] != vec2[i])
2383 return false;
2384 return true;
2387 /* Given an M x N integer matrix A, this function determines an M x
2388 M unimodular matrix U, and an M x N echelon matrix S such that
2389 "U.A = S". This decomposition is also known as "right Hermite".
2391 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2392 Restructuring Compilers" Utpal Banerjee. */
2394 static void
2395 lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
2396 lambda_matrix S, lambda_matrix U)
2398 int i, j, i0 = 0;
2400 lambda_matrix_copy (A, S, m, n);
2401 lambda_matrix_id (U, m);
2403 for (j = 0; j < n; j++)
2405 if (lambda_vector_first_nz (S[j], m, i0) < m)
2407 ++i0;
2408 for (i = m - 1; i >= i0; i--)
2410 while (S[i][j] != 0)
2412 int sigma, factor, a, b;
2414 a = S[i-1][j];
2415 b = S[i][j];
2416 sigma = (a * b < 0) ? -1: 1;
2417 a = abs (a);
2418 b = abs (b);
2419 factor = sigma * (a / b);
2421 lambda_matrix_row_add (S, n, i, i-1, -factor);
2422 lambda_matrix_row_exchange (S, i, i-1);
2424 lambda_matrix_row_add (U, m, i, i-1, -factor);
2425 lambda_matrix_row_exchange (U, i, i-1);
2432 /* Determines the overlapping elements due to accesses CHREC_A and
2433 CHREC_B, that are affine functions. This function cannot handle
2434 symbolic evolution functions, ie. when initial conditions are
2435 parameters, because it uses lambda matrices of integers. */
2437 static void
2438 analyze_subscript_affine_affine (tree chrec_a,
2439 tree chrec_b,
2440 conflict_function **overlaps_a,
2441 conflict_function **overlaps_b,
2442 tree *last_conflicts)
2444 unsigned nb_vars_a, nb_vars_b, dim;
2445 HOST_WIDE_INT init_a, init_b, gamma, gcd_alpha_beta;
2446 lambda_matrix A, U, S;
2447 struct obstack scratch_obstack;
2449 if (eq_evolutions_p (chrec_a, chrec_b))
2451 /* The accessed index overlaps for each iteration in the
2452 loop. */
2453 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2454 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2455 *last_conflicts = chrec_dont_know;
2456 return;
2458 if (dump_file && (dump_flags & TDF_DETAILS))
2459 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2461 /* For determining the initial intersection, we have to solve a
2462 Diophantine equation. This is the most time consuming part.
2464 For answering to the question: "Is there a dependence?" we have
2465 to prove that there exists a solution to the Diophantine
2466 equation, and that the solution is in the iteration domain,
2467 i.e. the solution is positive or zero, and that the solution
2468 happens before the upper bound loop.nb_iterations. Otherwise
2469 there is no dependence. This function outputs a description of
2470 the iterations that hold the intersections. */
2472 nb_vars_a = nb_vars_in_chrec (chrec_a);
2473 nb_vars_b = nb_vars_in_chrec (chrec_b);
2475 gcc_obstack_init (&scratch_obstack);
2477 dim = nb_vars_a + nb_vars_b;
2478 U = lambda_matrix_new (dim, dim, &scratch_obstack);
2479 A = lambda_matrix_new (dim, 1, &scratch_obstack);
2480 S = lambda_matrix_new (dim, 1, &scratch_obstack);
2482 init_a = int_cst_value (initialize_matrix_A (A, chrec_a, 0, 1));
2483 init_b = int_cst_value (initialize_matrix_A (A, chrec_b, nb_vars_a, -1));
2484 gamma = init_b - init_a;
2486 /* Don't do all the hard work of solving the Diophantine equation
2487 when we already know the solution: for example,
2488 | {3, +, 1}_1
2489 | {3, +, 4}_2
2490 | gamma = 3 - 3 = 0.
2491 Then the first overlap occurs during the first iterations:
2492 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2494 if (gamma == 0)
2496 if (nb_vars_a == 1 && nb_vars_b == 1)
2498 HOST_WIDE_INT step_a, step_b;
2499 HOST_WIDE_INT niter, niter_a, niter_b;
2500 affine_fn ova, ovb;
2502 niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
2503 niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
2504 niter = MIN (niter_a, niter_b);
2505 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2506 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2508 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2509 &ova, &ovb,
2510 last_conflicts, 1);
2511 *overlaps_a = conflict_fn (1, ova);
2512 *overlaps_b = conflict_fn (1, ovb);
2515 else if (nb_vars_a == 2 && nb_vars_b == 1)
2516 compute_overlap_steps_for_affine_1_2
2517 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2519 else if (nb_vars_a == 1 && nb_vars_b == 2)
2520 compute_overlap_steps_for_affine_1_2
2521 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2523 else
2525 if (dump_file && (dump_flags & TDF_DETAILS))
2526 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
2527 *overlaps_a = conflict_fn_not_known ();
2528 *overlaps_b = conflict_fn_not_known ();
2529 *last_conflicts = chrec_dont_know;
2531 goto end_analyze_subs_aa;
2534 /* U.A = S */
2535 lambda_matrix_right_hermite (A, dim, 1, S, U);
2537 if (S[0][0] < 0)
2539 S[0][0] *= -1;
2540 lambda_matrix_row_negate (U, dim, 0);
2542 gcd_alpha_beta = S[0][0];
2544 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2545 but that is a quite strange case. Instead of ICEing, answer
2546 don't know. */
2547 if (gcd_alpha_beta == 0)
2549 *overlaps_a = conflict_fn_not_known ();
2550 *overlaps_b = conflict_fn_not_known ();
2551 *last_conflicts = chrec_dont_know;
2552 goto end_analyze_subs_aa;
2555 /* The classic "gcd-test". */
2556 if (!int_divides_p (gcd_alpha_beta, gamma))
2558 /* The "gcd-test" has determined that there is no integer
2559 solution, i.e. there is no dependence. */
2560 *overlaps_a = conflict_fn_no_dependence ();
2561 *overlaps_b = conflict_fn_no_dependence ();
2562 *last_conflicts = integer_zero_node;
2565 /* Both access functions are univariate. This includes SIV and MIV cases. */
2566 else if (nb_vars_a == 1 && nb_vars_b == 1)
2568 /* Both functions should have the same evolution sign. */
2569 if (((A[0][0] > 0 && -A[1][0] > 0)
2570 || (A[0][0] < 0 && -A[1][0] < 0)))
2572 /* The solutions are given by:
2574 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2575 | [u21 u22] [y0]
2577 For a given integer t. Using the following variables,
2579 | i0 = u11 * gamma / gcd_alpha_beta
2580 | j0 = u12 * gamma / gcd_alpha_beta
2581 | i1 = u21
2582 | j1 = u22
2584 the solutions are:
2586 | x0 = i0 + i1 * t,
2587 | y0 = j0 + j1 * t. */
2588 HOST_WIDE_INT i0, j0, i1, j1;
2590 i0 = U[0][0] * gamma / gcd_alpha_beta;
2591 j0 = U[0][1] * gamma / gcd_alpha_beta;
2592 i1 = U[1][0];
2593 j1 = U[1][1];
2595 if ((i1 == 0 && i0 < 0)
2596 || (j1 == 0 && j0 < 0))
2598 /* There is no solution.
2599 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2600 falls in here, but for the moment we don't look at the
2601 upper bound of the iteration domain. */
2602 *overlaps_a = conflict_fn_no_dependence ();
2603 *overlaps_b = conflict_fn_no_dependence ();
2604 *last_conflicts = integer_zero_node;
2605 goto end_analyze_subs_aa;
2608 if (i1 > 0 && j1 > 0)
2610 HOST_WIDE_INT niter_a
2611 = max_stmt_executions_int (get_chrec_loop (chrec_a));
2612 HOST_WIDE_INT niter_b
2613 = max_stmt_executions_int (get_chrec_loop (chrec_b));
2614 HOST_WIDE_INT niter = MIN (niter_a, niter_b);
2616 /* (X0, Y0) is a solution of the Diophantine equation:
2617 "chrec_a (X0) = chrec_b (Y0)". */
2618 HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
2619 CEIL (-j0, j1));
2620 HOST_WIDE_INT x0 = i1 * tau1 + i0;
2621 HOST_WIDE_INT y0 = j1 * tau1 + j0;
2623 /* (X1, Y1) is the smallest positive solution of the eq
2624 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2625 first conflict occurs. */
2626 HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
2627 HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
2628 HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
2630 if (niter > 0)
2632 HOST_WIDE_INT tau2 = MIN (FLOOR_DIV (niter - i0, i1),
2633 FLOOR_DIV (niter - j0, j1));
2634 HOST_WIDE_INT last_conflict = tau2 - (x1 - i0)/i1;
2636 /* If the overlap occurs outside of the bounds of the
2637 loop, there is no dependence. */
2638 if (x1 >= niter || y1 >= niter)
2640 *overlaps_a = conflict_fn_no_dependence ();
2641 *overlaps_b = conflict_fn_no_dependence ();
2642 *last_conflicts = integer_zero_node;
2643 goto end_analyze_subs_aa;
2645 else
2646 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2648 else
2649 *last_conflicts = chrec_dont_know;
2651 *overlaps_a
2652 = conflict_fn (1,
2653 affine_fn_univar (build_int_cst (NULL_TREE, x1),
2655 build_int_cst (NULL_TREE, i1)));
2656 *overlaps_b
2657 = conflict_fn (1,
2658 affine_fn_univar (build_int_cst (NULL_TREE, y1),
2660 build_int_cst (NULL_TREE, j1)));
2662 else
2664 /* FIXME: For the moment, the upper bound of the
2665 iteration domain for i and j is not checked. */
2666 if (dump_file && (dump_flags & TDF_DETAILS))
2667 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2668 *overlaps_a = conflict_fn_not_known ();
2669 *overlaps_b = conflict_fn_not_known ();
2670 *last_conflicts = chrec_dont_know;
2673 else
2675 if (dump_file && (dump_flags & TDF_DETAILS))
2676 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2677 *overlaps_a = conflict_fn_not_known ();
2678 *overlaps_b = conflict_fn_not_known ();
2679 *last_conflicts = chrec_dont_know;
2682 else
2684 if (dump_file && (dump_flags & TDF_DETAILS))
2685 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2686 *overlaps_a = conflict_fn_not_known ();
2687 *overlaps_b = conflict_fn_not_known ();
2688 *last_conflicts = chrec_dont_know;
2691 end_analyze_subs_aa:
2692 obstack_free (&scratch_obstack, NULL);
2693 if (dump_file && (dump_flags & TDF_DETAILS))
2695 fprintf (dump_file, " (overlaps_a = ");
2696 dump_conflict_function (dump_file, *overlaps_a);
2697 fprintf (dump_file, ")\n (overlaps_b = ");
2698 dump_conflict_function (dump_file, *overlaps_b);
2699 fprintf (dump_file, "))\n");
2703 /* Returns true when analyze_subscript_affine_affine can be used for
2704 determining the dependence relation between chrec_a and chrec_b,
2705 that contain symbols. This function modifies chrec_a and chrec_b
2706 such that the analysis result is the same, and such that they don't
2707 contain symbols, and then can safely be passed to the analyzer.
2709 Example: The analysis of the following tuples of evolutions produce
2710 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2711 vs. {0, +, 1}_1
2713 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2714 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2717 static bool
2718 can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
2720 tree diff, type, left_a, left_b, right_b;
2722 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
2723 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
2724 /* FIXME: For the moment not handled. Might be refined later. */
2725 return false;
2727 type = chrec_type (*chrec_a);
2728 left_a = CHREC_LEFT (*chrec_a);
2729 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
2730 diff = chrec_fold_minus (type, left_a, left_b);
2732 if (!evolution_function_is_constant_p (diff))
2733 return false;
2735 if (dump_file && (dump_flags & TDF_DETAILS))
2736 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
2738 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
2739 diff, CHREC_RIGHT (*chrec_a));
2740 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
2741 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
2742 build_int_cst (type, 0),
2743 right_b);
2744 return true;
2747 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2748 *OVERLAPS_B are initialized to the functions that describe the
2749 relation between the elements accessed twice by CHREC_A and
2750 CHREC_B. For k >= 0, the following property is verified:
2752 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2754 static void
2755 analyze_siv_subscript (tree chrec_a,
2756 tree chrec_b,
2757 conflict_function **overlaps_a,
2758 conflict_function **overlaps_b,
2759 tree *last_conflicts,
2760 int loop_nest_num)
2762 dependence_stats.num_siv++;
2764 if (dump_file && (dump_flags & TDF_DETAILS))
2765 fprintf (dump_file, "(analyze_siv_subscript \n");
2767 if (evolution_function_is_constant_p (chrec_a)
2768 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2769 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
2770 overlaps_a, overlaps_b, last_conflicts);
2772 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2773 && evolution_function_is_constant_p (chrec_b))
2774 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
2775 overlaps_b, overlaps_a, last_conflicts);
2777 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2778 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2780 if (!chrec_contains_symbols (chrec_a)
2781 && !chrec_contains_symbols (chrec_b))
2783 analyze_subscript_affine_affine (chrec_a, chrec_b,
2784 overlaps_a, overlaps_b,
2785 last_conflicts);
2787 if (CF_NOT_KNOWN_P (*overlaps_a)
2788 || CF_NOT_KNOWN_P (*overlaps_b))
2789 dependence_stats.num_siv_unimplemented++;
2790 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2791 || CF_NO_DEPENDENCE_P (*overlaps_b))
2792 dependence_stats.num_siv_independent++;
2793 else
2794 dependence_stats.num_siv_dependent++;
2796 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
2797 &chrec_b))
2799 analyze_subscript_affine_affine (chrec_a, chrec_b,
2800 overlaps_a, overlaps_b,
2801 last_conflicts);
2803 if (CF_NOT_KNOWN_P (*overlaps_a)
2804 || CF_NOT_KNOWN_P (*overlaps_b))
2805 dependence_stats.num_siv_unimplemented++;
2806 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2807 || CF_NO_DEPENDENCE_P (*overlaps_b))
2808 dependence_stats.num_siv_independent++;
2809 else
2810 dependence_stats.num_siv_dependent++;
2812 else
2813 goto siv_subscript_dontknow;
2816 else
2818 siv_subscript_dontknow:;
2819 if (dump_file && (dump_flags & TDF_DETAILS))
2820 fprintf (dump_file, " siv test failed: unimplemented");
2821 *overlaps_a = conflict_fn_not_known ();
2822 *overlaps_b = conflict_fn_not_known ();
2823 *last_conflicts = chrec_dont_know;
2824 dependence_stats.num_siv_unimplemented++;
2827 if (dump_file && (dump_flags & TDF_DETAILS))
2828 fprintf (dump_file, ")\n");
2831 /* Returns false if we can prove that the greatest common divisor of the steps
2832 of CHREC does not divide CST, false otherwise. */
2834 static bool
2835 gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
2837 HOST_WIDE_INT cd = 0, val;
2838 tree step;
2840 if (!host_integerp (cst, 0))
2841 return true;
2842 val = tree_low_cst (cst, 0);
2844 while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
2846 step = CHREC_RIGHT (chrec);
2847 if (!host_integerp (step, 0))
2848 return true;
2849 cd = gcd (cd, tree_low_cst (step, 0));
2850 chrec = CHREC_LEFT (chrec);
2853 return val % cd == 0;
2856 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
2857 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2858 functions that describe the relation between the elements accessed
2859 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2860 is verified:
2862 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2864 static void
2865 analyze_miv_subscript (tree chrec_a,
2866 tree chrec_b,
2867 conflict_function **overlaps_a,
2868 conflict_function **overlaps_b,
2869 tree *last_conflicts,
2870 struct loop *loop_nest)
2872 tree type, difference;
2874 dependence_stats.num_miv++;
2875 if (dump_file && (dump_flags & TDF_DETAILS))
2876 fprintf (dump_file, "(analyze_miv_subscript \n");
2878 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
2879 chrec_a = chrec_convert (type, chrec_a, NULL);
2880 chrec_b = chrec_convert (type, chrec_b, NULL);
2881 difference = chrec_fold_minus (type, chrec_a, chrec_b);
2883 if (eq_evolutions_p (chrec_a, chrec_b))
2885 /* Access functions are the same: all the elements are accessed
2886 in the same order. */
2887 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2888 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2889 *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
2890 dependence_stats.num_miv_dependent++;
2893 else if (evolution_function_is_constant_p (difference)
2894 /* For the moment, the following is verified:
2895 evolution_function_is_affine_multivariate_p (chrec_a,
2896 loop_nest->num) */
2897 && !gcd_of_steps_may_divide_p (chrec_a, difference))
2899 /* testsuite/.../ssa-chrec-33.c
2900 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2902 The difference is 1, and all the evolution steps are multiples
2903 of 2, consequently there are no overlapping elements. */
2904 *overlaps_a = conflict_fn_no_dependence ();
2905 *overlaps_b = conflict_fn_no_dependence ();
2906 *last_conflicts = integer_zero_node;
2907 dependence_stats.num_miv_independent++;
2910 else if (evolution_function_is_affine_multivariate_p (chrec_a, loop_nest->num)
2911 && !chrec_contains_symbols (chrec_a)
2912 && evolution_function_is_affine_multivariate_p (chrec_b, loop_nest->num)
2913 && !chrec_contains_symbols (chrec_b))
2915 /* testsuite/.../ssa-chrec-35.c
2916 {0, +, 1}_2 vs. {0, +, 1}_3
2917 the overlapping elements are respectively located at iterations:
2918 {0, +, 1}_x and {0, +, 1}_x,
2919 in other words, we have the equality:
2920 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2922 Other examples:
2923 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2924 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2926 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2927 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2929 analyze_subscript_affine_affine (chrec_a, chrec_b,
2930 overlaps_a, overlaps_b, last_conflicts);
2932 if (CF_NOT_KNOWN_P (*overlaps_a)
2933 || CF_NOT_KNOWN_P (*overlaps_b))
2934 dependence_stats.num_miv_unimplemented++;
2935 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2936 || CF_NO_DEPENDENCE_P (*overlaps_b))
2937 dependence_stats.num_miv_independent++;
2938 else
2939 dependence_stats.num_miv_dependent++;
2942 else
2944 /* When the analysis is too difficult, answer "don't know". */
2945 if (dump_file && (dump_flags & TDF_DETAILS))
2946 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
2948 *overlaps_a = conflict_fn_not_known ();
2949 *overlaps_b = conflict_fn_not_known ();
2950 *last_conflicts = chrec_dont_know;
2951 dependence_stats.num_miv_unimplemented++;
2954 if (dump_file && (dump_flags & TDF_DETAILS))
2955 fprintf (dump_file, ")\n");
2958 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
2959 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
2960 OVERLAP_ITERATIONS_B are initialized with two functions that
2961 describe the iterations that contain conflicting elements.
2963 Remark: For an integer k >= 0, the following equality is true:
2965 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
2968 static void
2969 analyze_overlapping_iterations (tree chrec_a,
2970 tree chrec_b,
2971 conflict_function **overlap_iterations_a,
2972 conflict_function **overlap_iterations_b,
2973 tree *last_conflicts, struct loop *loop_nest)
2975 unsigned int lnn = loop_nest->num;
2977 dependence_stats.num_subscript_tests++;
2979 if (dump_file && (dump_flags & TDF_DETAILS))
2981 fprintf (dump_file, "(analyze_overlapping_iterations \n");
2982 fprintf (dump_file, " (chrec_a = ");
2983 print_generic_expr (dump_file, chrec_a, 0);
2984 fprintf (dump_file, ")\n (chrec_b = ");
2985 print_generic_expr (dump_file, chrec_b, 0);
2986 fprintf (dump_file, ")\n");
2989 if (chrec_a == NULL_TREE
2990 || chrec_b == NULL_TREE
2991 || chrec_contains_undetermined (chrec_a)
2992 || chrec_contains_undetermined (chrec_b))
2994 dependence_stats.num_subscript_undetermined++;
2996 *overlap_iterations_a = conflict_fn_not_known ();
2997 *overlap_iterations_b = conflict_fn_not_known ();
3000 /* If they are the same chrec, and are affine, they overlap
3001 on every iteration. */
3002 else if (eq_evolutions_p (chrec_a, chrec_b)
3003 && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3004 || operand_equal_p (chrec_a, chrec_b, 0)))
3006 dependence_stats.num_same_subscript_function++;
3007 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3008 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3009 *last_conflicts = chrec_dont_know;
3012 /* If they aren't the same, and aren't affine, we can't do anything
3013 yet. */
3014 else if ((chrec_contains_symbols (chrec_a)
3015 || chrec_contains_symbols (chrec_b))
3016 && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3017 || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
3019 dependence_stats.num_subscript_undetermined++;
3020 *overlap_iterations_a = conflict_fn_not_known ();
3021 *overlap_iterations_b = conflict_fn_not_known ();
3024 else if (ziv_subscript_p (chrec_a, chrec_b))
3025 analyze_ziv_subscript (chrec_a, chrec_b,
3026 overlap_iterations_a, overlap_iterations_b,
3027 last_conflicts);
3029 else if (siv_subscript_p (chrec_a, chrec_b))
3030 analyze_siv_subscript (chrec_a, chrec_b,
3031 overlap_iterations_a, overlap_iterations_b,
3032 last_conflicts, lnn);
3034 else
3035 analyze_miv_subscript (chrec_a, chrec_b,
3036 overlap_iterations_a, overlap_iterations_b,
3037 last_conflicts, loop_nest);
3039 if (dump_file && (dump_flags & TDF_DETAILS))
3041 fprintf (dump_file, " (overlap_iterations_a = ");
3042 dump_conflict_function (dump_file, *overlap_iterations_a);
3043 fprintf (dump_file, ")\n (overlap_iterations_b = ");
3044 dump_conflict_function (dump_file, *overlap_iterations_b);
3045 fprintf (dump_file, "))\n");
3049 /* Helper function for uniquely inserting distance vectors. */
3051 static void
3052 save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
3054 unsigned i;
3055 lambda_vector v;
3057 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, v)
3058 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
3059 return;
3061 DDR_DIST_VECTS (ddr).safe_push (dist_v);
3064 /* Helper function for uniquely inserting direction vectors. */
3066 static void
3067 save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
3069 unsigned i;
3070 lambda_vector v;
3072 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), i, v)
3073 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
3074 return;
3076 DDR_DIR_VECTS (ddr).safe_push (dir_v);
3079 /* Add a distance of 1 on all the loops outer than INDEX. If we
3080 haven't yet determined a distance for this outer loop, push a new
3081 distance vector composed of the previous distance, and a distance
3082 of 1 for this outer loop. Example:
3084 | loop_1
3085 | loop_2
3086 | A[10]
3087 | endloop_2
3088 | endloop_1
3090 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3091 save (0, 1), then we have to save (1, 0). */
3093 static void
3094 add_outer_distances (struct data_dependence_relation *ddr,
3095 lambda_vector dist_v, int index)
3097 /* For each outer loop where init_v is not set, the accesses are
3098 in dependence of distance 1 in the loop. */
3099 while (--index >= 0)
3101 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3102 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3103 save_v[index] = 1;
3104 save_dist_v (ddr, save_v);
3108 /* Return false when fail to represent the data dependence as a
3109 distance vector. INIT_B is set to true when a component has been
3110 added to the distance vector DIST_V. INDEX_CARRY is then set to
3111 the index in DIST_V that carries the dependence. */
3113 static bool
3114 build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
3115 struct data_reference *ddr_a,
3116 struct data_reference *ddr_b,
3117 lambda_vector dist_v, bool *init_b,
3118 int *index_carry)
3120 unsigned i;
3121 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3123 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3125 tree access_fn_a, access_fn_b;
3126 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3128 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3130 non_affine_dependence_relation (ddr);
3131 return false;
3134 access_fn_a = DR_ACCESS_FN (ddr_a, i);
3135 access_fn_b = DR_ACCESS_FN (ddr_b, i);
3137 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3138 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3140 int dist, index;
3141 int var_a = CHREC_VARIABLE (access_fn_a);
3142 int var_b = CHREC_VARIABLE (access_fn_b);
3144 if (var_a != var_b
3145 || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3147 non_affine_dependence_relation (ddr);
3148 return false;
3151 dist = int_cst_value (SUB_DISTANCE (subscript));
3152 index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
3153 *index_carry = MIN (index, *index_carry);
3155 /* This is the subscript coupling test. If we have already
3156 recorded a distance for this loop (a distance coming from
3157 another subscript), it should be the same. For example,
3158 in the following code, there is no dependence:
3160 | loop i = 0, N, 1
3161 | T[i+1][i] = ...
3162 | ... = T[i][i]
3163 | endloop
3165 if (init_v[index] != 0 && dist_v[index] != dist)
3167 finalize_ddr_dependent (ddr, chrec_known);
3168 return false;
3171 dist_v[index] = dist;
3172 init_v[index] = 1;
3173 *init_b = true;
3175 else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
3177 /* This can be for example an affine vs. constant dependence
3178 (T[i] vs. T[3]) that is not an affine dependence and is
3179 not representable as a distance vector. */
3180 non_affine_dependence_relation (ddr);
3181 return false;
3185 return true;
3188 /* Return true when the DDR contains only constant access functions. */
3190 static bool
3191 constant_access_functions (const struct data_dependence_relation *ddr)
3193 unsigned i;
3195 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3196 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr), i))
3197 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr), i)))
3198 return false;
3200 return true;
3203 /* Helper function for the case where DDR_A and DDR_B are the same
3204 multivariate access function with a constant step. For an example
3205 see pr34635-1.c. */
3207 static void
3208 add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
3210 int x_1, x_2;
3211 tree c_1 = CHREC_LEFT (c_2);
3212 tree c_0 = CHREC_LEFT (c_1);
3213 lambda_vector dist_v;
3214 int v1, v2, cd;
3216 /* Polynomials with more than 2 variables are not handled yet. When
3217 the evolution steps are parameters, it is not possible to
3218 represent the dependence using classical distance vectors. */
3219 if (TREE_CODE (c_0) != INTEGER_CST
3220 || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
3221 || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
3223 DDR_AFFINE_P (ddr) = false;
3224 return;
3227 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
3228 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
3230 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3231 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3232 v1 = int_cst_value (CHREC_RIGHT (c_1));
3233 v2 = int_cst_value (CHREC_RIGHT (c_2));
3234 cd = gcd (v1, v2);
3235 v1 /= cd;
3236 v2 /= cd;
3238 if (v2 < 0)
3240 v2 = -v2;
3241 v1 = -v1;
3244 dist_v[x_1] = v2;
3245 dist_v[x_2] = -v1;
3246 save_dist_v (ddr, dist_v);
3248 add_outer_distances (ddr, dist_v, x_1);
3251 /* Helper function for the case where DDR_A and DDR_B are the same
3252 access functions. */
3254 static void
3255 add_other_self_distances (struct data_dependence_relation *ddr)
3257 lambda_vector dist_v;
3258 unsigned i;
3259 int index_carry = DDR_NB_LOOPS (ddr);
3261 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3263 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
3265 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
3267 if (!evolution_function_is_univariate_p (access_fun))
3269 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
3271 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3272 return;
3275 access_fun = DR_ACCESS_FN (DDR_A (ddr), 0);
3277 if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
3278 add_multivariate_self_dist (ddr, access_fun);
3279 else
3280 /* The evolution step is not constant: it varies in
3281 the outer loop, so this cannot be represented by a
3282 distance vector. For example in pr34635.c the
3283 evolution is {0, +, {0, +, 4}_1}_2. */
3284 DDR_AFFINE_P (ddr) = false;
3286 return;
3289 index_carry = MIN (index_carry,
3290 index_in_loop_nest (CHREC_VARIABLE (access_fun),
3291 DDR_LOOP_NEST (ddr)));
3295 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3296 add_outer_distances (ddr, dist_v, index_carry);
3299 static void
3300 insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
3302 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3304 dist_v[DDR_INNER_LOOP (ddr)] = 1;
3305 save_dist_v (ddr, dist_v);
3308 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
3309 is the case for example when access functions are the same and
3310 equal to a constant, as in:
3312 | loop_1
3313 | A[3] = ...
3314 | ... = A[3]
3315 | endloop_1
3317 in which case the distance vectors are (0) and (1). */
3319 static void
3320 add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
3322 unsigned i, j;
3324 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3326 subscript_p sub = DDR_SUBSCRIPT (ddr, i);
3327 conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
3328 conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
3330 for (j = 0; j < ca->n; j++)
3331 if (affine_function_zero_p (ca->fns[j]))
3333 insert_innermost_unit_dist_vector (ddr);
3334 return;
3337 for (j = 0; j < cb->n; j++)
3338 if (affine_function_zero_p (cb->fns[j]))
3340 insert_innermost_unit_dist_vector (ddr);
3341 return;
3346 /* Compute the classic per loop distance vector. DDR is the data
3347 dependence relation to build a vector from. Return false when fail
3348 to represent the data dependence as a distance vector. */
3350 static bool
3351 build_classic_dist_vector (struct data_dependence_relation *ddr,
3352 struct loop *loop_nest)
3354 bool init_b = false;
3355 int index_carry = DDR_NB_LOOPS (ddr);
3356 lambda_vector dist_v;
3358 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3359 return false;
3361 if (same_access_functions (ddr))
3363 /* Save the 0 vector. */
3364 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3365 save_dist_v (ddr, dist_v);
3367 if (constant_access_functions (ddr))
3368 add_distance_for_zero_overlaps (ddr);
3370 if (DDR_NB_LOOPS (ddr) > 1)
3371 add_other_self_distances (ddr);
3373 return true;
3376 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3377 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3378 dist_v, &init_b, &index_carry))
3379 return false;
3381 /* Save the distance vector if we initialized one. */
3382 if (init_b)
3384 /* Verify a basic constraint: classic distance vectors should
3385 always be lexicographically positive.
3387 Data references are collected in the order of execution of
3388 the program, thus for the following loop
3390 | for (i = 1; i < 100; i++)
3391 | for (j = 1; j < 100; j++)
3393 | t = T[j+1][i-1]; // A
3394 | T[j][i] = t + 2; // B
3397 references are collected following the direction of the wind:
3398 A then B. The data dependence tests are performed also
3399 following this order, such that we're looking at the distance
3400 separating the elements accessed by A from the elements later
3401 accessed by B. But in this example, the distance returned by
3402 test_dep (A, B) is lexicographically negative (-1, 1), that
3403 means that the access A occurs later than B with respect to
3404 the outer loop, ie. we're actually looking upwind. In this
3405 case we solve test_dep (B, A) looking downwind to the
3406 lexicographically positive solution, that returns the
3407 distance vector (1, -1). */
3408 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3410 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3411 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3412 loop_nest))
3413 return false;
3414 compute_subscript_distance (ddr);
3415 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3416 save_v, &init_b, &index_carry))
3417 return false;
3418 save_dist_v (ddr, save_v);
3419 DDR_REVERSED_P (ddr) = true;
3421 /* In this case there is a dependence forward for all the
3422 outer loops:
3424 | for (k = 1; k < 100; k++)
3425 | for (i = 1; i < 100; i++)
3426 | for (j = 1; j < 100; j++)
3428 | t = T[j+1][i-1]; // A
3429 | T[j][i] = t + 2; // B
3432 the vectors are:
3433 (0, 1, -1)
3434 (1, 1, -1)
3435 (1, -1, 1)
3437 if (DDR_NB_LOOPS (ddr) > 1)
3439 add_outer_distances (ddr, save_v, index_carry);
3440 add_outer_distances (ddr, dist_v, index_carry);
3443 else
3445 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3446 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3448 if (DDR_NB_LOOPS (ddr) > 1)
3450 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3452 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr),
3453 DDR_A (ddr), loop_nest))
3454 return false;
3455 compute_subscript_distance (ddr);
3456 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3457 opposite_v, &init_b,
3458 &index_carry))
3459 return false;
3461 save_dist_v (ddr, save_v);
3462 add_outer_distances (ddr, dist_v, index_carry);
3463 add_outer_distances (ddr, opposite_v, index_carry);
3465 else
3466 save_dist_v (ddr, save_v);
3469 else
3471 /* There is a distance of 1 on all the outer loops: Example:
3472 there is a dependence of distance 1 on loop_1 for the array A.
3474 | loop_1
3475 | A[5] = ...
3476 | endloop
3478 add_outer_distances (ddr, dist_v,
3479 lambda_vector_first_nz (dist_v,
3480 DDR_NB_LOOPS (ddr), 0));
3483 if (dump_file && (dump_flags & TDF_DETAILS))
3485 unsigned i;
3487 fprintf (dump_file, "(build_classic_dist_vector\n");
3488 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3490 fprintf (dump_file, " dist_vector = (");
3491 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3492 DDR_NB_LOOPS (ddr));
3493 fprintf (dump_file, " )\n");
3495 fprintf (dump_file, ")\n");
3498 return true;
3501 /* Return the direction for a given distance.
3502 FIXME: Computing dir this way is suboptimal, since dir can catch
3503 cases that dist is unable to represent. */
3505 static inline enum data_dependence_direction
3506 dir_from_dist (int dist)
3508 if (dist > 0)
3509 return dir_positive;
3510 else if (dist < 0)
3511 return dir_negative;
3512 else
3513 return dir_equal;
3516 /* Compute the classic per loop direction vector. DDR is the data
3517 dependence relation to build a vector from. */
3519 static void
3520 build_classic_dir_vector (struct data_dependence_relation *ddr)
3522 unsigned i, j;
3523 lambda_vector dist_v;
3525 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
3527 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3529 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3530 dir_v[j] = dir_from_dist (dist_v[j]);
3532 save_dir_v (ddr, dir_v);
3536 /* Helper function. Returns true when there is a dependence between
3537 data references DRA and DRB. */
3539 static bool
3540 subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
3541 struct data_reference *dra,
3542 struct data_reference *drb,
3543 struct loop *loop_nest)
3545 unsigned int i;
3546 tree last_conflicts;
3547 struct subscript *subscript;
3548 tree res = NULL_TREE;
3550 for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
3552 conflict_function *overlaps_a, *overlaps_b;
3554 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3555 DR_ACCESS_FN (drb, i),
3556 &overlaps_a, &overlaps_b,
3557 &last_conflicts, loop_nest);
3559 if (SUB_CONFLICTS_IN_A (subscript))
3560 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
3561 if (SUB_CONFLICTS_IN_B (subscript))
3562 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
3564 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3565 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3566 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3568 /* If there is any undetermined conflict function we have to
3569 give a conservative answer in case we cannot prove that
3570 no dependence exists when analyzing another subscript. */
3571 if (CF_NOT_KNOWN_P (overlaps_a)
3572 || CF_NOT_KNOWN_P (overlaps_b))
3574 res = chrec_dont_know;
3575 continue;
3578 /* When there is a subscript with no dependence we can stop. */
3579 else if (CF_NO_DEPENDENCE_P (overlaps_a)
3580 || CF_NO_DEPENDENCE_P (overlaps_b))
3582 res = chrec_known;
3583 break;
3587 if (res == NULL_TREE)
3588 return true;
3590 if (res == chrec_known)
3591 dependence_stats.num_dependence_independent++;
3592 else
3593 dependence_stats.num_dependence_undetermined++;
3594 finalize_ddr_dependent (ddr, res);
3595 return false;
3598 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
3600 static void
3601 subscript_dependence_tester (struct data_dependence_relation *ddr,
3602 struct loop *loop_nest)
3604 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr), loop_nest))
3605 dependence_stats.num_dependence_dependent++;
3607 compute_subscript_distance (ddr);
3608 if (build_classic_dist_vector (ddr, loop_nest))
3609 build_classic_dir_vector (ddr);
3612 /* Returns true when all the access functions of A are affine or
3613 constant with respect to LOOP_NEST. */
3615 static bool
3616 access_functions_are_affine_or_constant_p (const struct data_reference *a,
3617 const struct loop *loop_nest)
3619 unsigned int i;
3620 vec<tree> fns = DR_ACCESS_FNS (a);
3621 tree t;
3623 FOR_EACH_VEC_ELT (fns, i, t)
3624 if (!evolution_function_is_invariant_p (t, loop_nest->num)
3625 && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
3626 return false;
3628 return true;
3631 /* Initializes an equation for an OMEGA problem using the information
3632 contained in the ACCESS_FUN. Returns true when the operation
3633 succeeded.
3635 PB is the omega constraint system.
3636 EQ is the number of the equation to be initialized.
3637 OFFSET is used for shifting the variables names in the constraints:
3638 a constrain is composed of 2 * the number of variables surrounding
3639 dependence accesses. OFFSET is set either to 0 for the first n variables,
3640 then it is set to n.
3641 ACCESS_FUN is expected to be an affine chrec. */
3643 static bool
3644 init_omega_eq_with_af (omega_pb pb, unsigned eq,
3645 unsigned int offset, tree access_fun,
3646 struct data_dependence_relation *ddr)
3648 switch (TREE_CODE (access_fun))
3650 case POLYNOMIAL_CHREC:
3652 tree left = CHREC_LEFT (access_fun);
3653 tree right = CHREC_RIGHT (access_fun);
3654 int var = CHREC_VARIABLE (access_fun);
3655 unsigned var_idx;
3657 if (TREE_CODE (right) != INTEGER_CST)
3658 return false;
3660 var_idx = index_in_loop_nest (var, DDR_LOOP_NEST (ddr));
3661 pb->eqs[eq].coef[offset + var_idx + 1] = int_cst_value (right);
3663 /* Compute the innermost loop index. */
3664 DDR_INNER_LOOP (ddr) = MAX (DDR_INNER_LOOP (ddr), var_idx);
3666 if (offset == 0)
3667 pb->eqs[eq].coef[var_idx + DDR_NB_LOOPS (ddr) + 1]
3668 += int_cst_value (right);
3670 switch (TREE_CODE (left))
3672 case POLYNOMIAL_CHREC:
3673 return init_omega_eq_with_af (pb, eq, offset, left, ddr);
3675 case INTEGER_CST:
3676 pb->eqs[eq].coef[0] += int_cst_value (left);
3677 return true;
3679 default:
3680 return false;
3684 case INTEGER_CST:
3685 pb->eqs[eq].coef[0] += int_cst_value (access_fun);
3686 return true;
3688 default:
3689 return false;
3693 /* As explained in the comments preceding init_omega_for_ddr, we have
3694 to set up a system for each loop level, setting outer loops
3695 variation to zero, and current loop variation to positive or zero.
3696 Save each lexico positive distance vector. */
3698 static void
3699 omega_extract_distance_vectors (omega_pb pb,
3700 struct data_dependence_relation *ddr)
3702 int eq, geq;
3703 unsigned i, j;
3704 struct loop *loopi, *loopj;
3705 enum omega_result res;
3707 /* Set a new problem for each loop in the nest. The basis is the
3708 problem that we have initialized until now. On top of this we
3709 add new constraints. */
3710 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3711 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3713 int dist = 0;
3714 omega_pb copy = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr),
3715 DDR_NB_LOOPS (ddr));
3717 omega_copy_problem (copy, pb);
3719 /* For all the outer loops "loop_j", add "dj = 0". */
3720 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3722 eq = omega_add_zero_eq (copy, omega_black);
3723 copy->eqs[eq].coef[j + 1] = 1;
3726 /* For "loop_i", add "0 <= di". */
3727 geq = omega_add_zero_geq (copy, omega_black);
3728 copy->geqs[geq].coef[i + 1] = 1;
3730 /* Reduce the constraint system, and test that the current
3731 problem is feasible. */
3732 res = omega_simplify_problem (copy);
3733 if (res == omega_false
3734 || res == omega_unknown
3735 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3736 goto next_problem;
3738 for (eq = 0; eq < copy->num_subs; eq++)
3739 if (copy->subs[eq].key == (int) i + 1)
3741 dist = copy->subs[eq].coef[0];
3742 goto found_dist;
3745 if (dist == 0)
3747 /* Reinitialize problem... */
3748 omega_copy_problem (copy, pb);
3749 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3751 eq = omega_add_zero_eq (copy, omega_black);
3752 copy->eqs[eq].coef[j + 1] = 1;
3755 /* ..., but this time "di = 1". */
3756 eq = omega_add_zero_eq (copy, omega_black);
3757 copy->eqs[eq].coef[i + 1] = 1;
3758 copy->eqs[eq].coef[0] = -1;
3760 res = omega_simplify_problem (copy);
3761 if (res == omega_false
3762 || res == omega_unknown
3763 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3764 goto next_problem;
3766 for (eq = 0; eq < copy->num_subs; eq++)
3767 if (copy->subs[eq].key == (int) i + 1)
3769 dist = copy->subs[eq].coef[0];
3770 goto found_dist;
3774 found_dist:;
3775 /* Save the lexicographically positive distance vector. */
3776 if (dist >= 0)
3778 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3779 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3781 dist_v[i] = dist;
3783 for (eq = 0; eq < copy->num_subs; eq++)
3784 if (copy->subs[eq].key > 0)
3786 dist = copy->subs[eq].coef[0];
3787 dist_v[copy->subs[eq].key - 1] = dist;
3790 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3791 dir_v[j] = dir_from_dist (dist_v[j]);
3793 save_dist_v (ddr, dist_v);
3794 save_dir_v (ddr, dir_v);
3797 next_problem:;
3798 omega_free_problem (copy);
3802 /* This is called for each subscript of a tuple of data references:
3803 insert an equality for representing the conflicts. */
3805 static bool
3806 omega_setup_subscript (tree access_fun_a, tree access_fun_b,
3807 struct data_dependence_relation *ddr,
3808 omega_pb pb, bool *maybe_dependent)
3810 int eq;
3811 tree type = signed_type_for_types (TREE_TYPE (access_fun_a),
3812 TREE_TYPE (access_fun_b));
3813 tree fun_a = chrec_convert (type, access_fun_a, NULL);
3814 tree fun_b = chrec_convert (type, access_fun_b, NULL);
3815 tree difference = chrec_fold_minus (type, fun_a, fun_b);
3816 tree minus_one;
3818 /* When the fun_a - fun_b is not constant, the dependence is not
3819 captured by the classic distance vector representation. */
3820 if (TREE_CODE (difference) != INTEGER_CST)
3821 return false;
3823 /* ZIV test. */
3824 if (ziv_subscript_p (fun_a, fun_b) && !integer_zerop (difference))
3826 /* There is no dependence. */
3827 *maybe_dependent = false;
3828 return true;
3831 minus_one = build_int_cst (type, -1);
3832 fun_b = chrec_fold_multiply (type, fun_b, minus_one);
3834 eq = omega_add_zero_eq (pb, omega_black);
3835 if (!init_omega_eq_with_af (pb, eq, DDR_NB_LOOPS (ddr), fun_a, ddr)
3836 || !init_omega_eq_with_af (pb, eq, 0, fun_b, ddr))
3837 /* There is probably a dependence, but the system of
3838 constraints cannot be built: answer "don't know". */
3839 return false;
3841 /* GCD test. */
3842 if (DDR_NB_LOOPS (ddr) != 0 && pb->eqs[eq].coef[0]
3843 && !int_divides_p (lambda_vector_gcd
3844 ((lambda_vector) &(pb->eqs[eq].coef[1]),
3845 2 * DDR_NB_LOOPS (ddr)),
3846 pb->eqs[eq].coef[0]))
3848 /* There is no dependence. */
3849 *maybe_dependent = false;
3850 return true;
3853 return true;
3856 /* Helper function, same as init_omega_for_ddr but specialized for
3857 data references A and B. */
3859 static bool
3860 init_omega_for_ddr_1 (struct data_reference *dra, struct data_reference *drb,
3861 struct data_dependence_relation *ddr,
3862 omega_pb pb, bool *maybe_dependent)
3864 unsigned i;
3865 int ineq;
3866 struct loop *loopi;
3867 unsigned nb_loops = DDR_NB_LOOPS (ddr);
3869 /* Insert an equality per subscript. */
3870 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3872 if (!omega_setup_subscript (DR_ACCESS_FN (dra, i), DR_ACCESS_FN (drb, i),
3873 ddr, pb, maybe_dependent))
3874 return false;
3875 else if (*maybe_dependent == false)
3877 /* There is no dependence. */
3878 DDR_ARE_DEPENDENT (ddr) = chrec_known;
3879 return true;
3883 /* Insert inequalities: constraints corresponding to the iteration
3884 domain, i.e. the loops surrounding the references "loop_x" and
3885 the distance variables "dx". The layout of the OMEGA
3886 representation is as follows:
3887 - coef[0] is the constant
3888 - coef[1..nb_loops] are the protected variables that will not be
3889 removed by the solver: the "dx"
3890 - coef[nb_loops + 1, 2*nb_loops] are the loop variables: "loop_x".
3892 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3893 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3895 HOST_WIDE_INT nbi = max_stmt_executions_int (loopi);
3897 /* 0 <= loop_x */
3898 ineq = omega_add_zero_geq (pb, omega_black);
3899 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3901 /* 0 <= loop_x + dx */
3902 ineq = omega_add_zero_geq (pb, omega_black);
3903 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3904 pb->geqs[ineq].coef[i + 1] = 1;
3906 if (nbi != -1)
3908 /* loop_x <= nb_iters */
3909 ineq = omega_add_zero_geq (pb, omega_black);
3910 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3911 pb->geqs[ineq].coef[0] = nbi;
3913 /* loop_x + dx <= nb_iters */
3914 ineq = omega_add_zero_geq (pb, omega_black);
3915 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3916 pb->geqs[ineq].coef[i + 1] = -1;
3917 pb->geqs[ineq].coef[0] = nbi;
3919 /* A step "dx" bigger than nb_iters is not feasible, so
3920 add "0 <= nb_iters + dx", */
3921 ineq = omega_add_zero_geq (pb, omega_black);
3922 pb->geqs[ineq].coef[i + 1] = 1;
3923 pb->geqs[ineq].coef[0] = nbi;
3924 /* and "dx <= nb_iters". */
3925 ineq = omega_add_zero_geq (pb, omega_black);
3926 pb->geqs[ineq].coef[i + 1] = -1;
3927 pb->geqs[ineq].coef[0] = nbi;
3931 omega_extract_distance_vectors (pb, ddr);
3933 return true;
3936 /* Sets up the Omega dependence problem for the data dependence
3937 relation DDR. Returns false when the constraint system cannot be
3938 built, ie. when the test answers "don't know". Returns true
3939 otherwise, and when independence has been proved (using one of the
3940 trivial dependence test), set MAYBE_DEPENDENT to false, otherwise
3941 set MAYBE_DEPENDENT to true.
3943 Example: for setting up the dependence system corresponding to the
3944 conflicting accesses
3946 | loop_i
3947 | loop_j
3948 | A[i, i+1] = ...
3949 | ... A[2*j, 2*(i + j)]
3950 | endloop_j
3951 | endloop_i
3953 the following constraints come from the iteration domain:
3955 0 <= i <= Ni
3956 0 <= i + di <= Ni
3957 0 <= j <= Nj
3958 0 <= j + dj <= Nj
3960 where di, dj are the distance variables. The constraints
3961 representing the conflicting elements are:
3963 i = 2 * (j + dj)
3964 i + 1 = 2 * (i + di + j + dj)
3966 For asking that the resulting distance vector (di, dj) be
3967 lexicographically positive, we insert the constraint "di >= 0". If
3968 "di = 0" in the solution, we fix that component to zero, and we
3969 look at the inner loops: we set a new problem where all the outer
3970 loop distances are zero, and fix this inner component to be
3971 positive. When one of the components is positive, we save that
3972 distance, and set a new problem where the distance on this loop is
3973 zero, searching for other distances in the inner loops. Here is
3974 the classic example that illustrates that we have to set for each
3975 inner loop a new problem:
3977 | loop_1
3978 | loop_2
3979 | A[10]
3980 | endloop_2
3981 | endloop_1
3983 we have to save two distances (1, 0) and (0, 1).
3985 Given two array references, refA and refB, we have to set the
3986 dependence problem twice, refA vs. refB and refB vs. refA, and we
3987 cannot do a single test, as refB might occur before refA in the
3988 inner loops, and the contrary when considering outer loops: ex.
3990 | loop_0
3991 | loop_1
3992 | loop_2
3993 | T[{1,+,1}_2][{1,+,1}_1] // refA
3994 | T[{2,+,1}_2][{0,+,1}_1] // refB
3995 | endloop_2
3996 | endloop_1
3997 | endloop_0
3999 refB touches the elements in T before refA, and thus for the same
4000 loop_0 refB precedes refA: ie. the distance vector (0, 1, -1)
4001 but for successive loop_0 iterations, we have (1, -1, 1)
4003 The Omega solver expects the distance variables ("di" in the
4004 previous example) to come first in the constraint system (as
4005 variables to be protected, or "safe" variables), the constraint
4006 system is built using the following layout:
4008 "cst | distance vars | index vars".
4011 static bool
4012 init_omega_for_ddr (struct data_dependence_relation *ddr,
4013 bool *maybe_dependent)
4015 omega_pb pb;
4016 bool res = false;
4018 *maybe_dependent = true;
4020 if (same_access_functions (ddr))
4022 unsigned j;
4023 lambda_vector dir_v;
4025 /* Save the 0 vector. */
4026 save_dist_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
4027 dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4028 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
4029 dir_v[j] = dir_equal;
4030 save_dir_v (ddr, dir_v);
4032 /* Save the dependences carried by outer loops. */
4033 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4034 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4035 maybe_dependent);
4036 omega_free_problem (pb);
4037 return res;
4040 /* Omega expects the protected variables (those that have to be kept
4041 after elimination) to appear first in the constraint system.
4042 These variables are the distance variables. In the following
4043 initialization we declare NB_LOOPS safe variables, and the total
4044 number of variables for the constraint system is 2*NB_LOOPS. */
4045 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4046 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4047 maybe_dependent);
4048 omega_free_problem (pb);
4050 /* Stop computation if not decidable, or no dependence. */
4051 if (res == false || *maybe_dependent == false)
4052 return res;
4054 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4055 res = init_omega_for_ddr_1 (DDR_B (ddr), DDR_A (ddr), ddr, pb,
4056 maybe_dependent);
4057 omega_free_problem (pb);
4059 return res;
4062 /* Return true when DDR contains the same information as that stored
4063 in DIR_VECTS and in DIST_VECTS, return false otherwise. */
4065 static bool
4066 ddr_consistent_p (FILE *file,
4067 struct data_dependence_relation *ddr,
4068 vec<lambda_vector> dist_vects,
4069 vec<lambda_vector> dir_vects)
4071 unsigned int i, j;
4073 /* If dump_file is set, output there. */
4074 if (dump_file && (dump_flags & TDF_DETAILS))
4075 file = dump_file;
4077 if (dist_vects.length () != DDR_NUM_DIST_VECTS (ddr))
4079 lambda_vector b_dist_v;
4080 fprintf (file, "\n(Number of distance vectors differ: Banerjee has %d, Omega has %d.\n",
4081 dist_vects.length (),
4082 DDR_NUM_DIST_VECTS (ddr));
4084 fprintf (file, "Banerjee dist vectors:\n");
4085 FOR_EACH_VEC_ELT (dist_vects, i, b_dist_v)
4086 print_lambda_vector (file, b_dist_v, DDR_NB_LOOPS (ddr));
4088 fprintf (file, "Omega dist vectors:\n");
4089 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4090 print_lambda_vector (file, DDR_DIST_VECT (ddr, i), DDR_NB_LOOPS (ddr));
4092 fprintf (file, "data dependence relation:\n");
4093 dump_data_dependence_relation (file, ddr);
4095 fprintf (file, ")\n");
4096 return false;
4099 if (dir_vects.length () != DDR_NUM_DIR_VECTS (ddr))
4101 fprintf (file, "\n(Number of direction vectors differ: Banerjee has %d, Omega has %d.)\n",
4102 dir_vects.length (),
4103 DDR_NUM_DIR_VECTS (ddr));
4104 return false;
4107 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4109 lambda_vector a_dist_v;
4110 lambda_vector b_dist_v = DDR_DIST_VECT (ddr, i);
4112 /* Distance vectors are not ordered in the same way in the DDR
4113 and in the DIST_VECTS: search for a matching vector. */
4114 FOR_EACH_VEC_ELT (dist_vects, j, a_dist_v)
4115 if (lambda_vector_equal (a_dist_v, b_dist_v, DDR_NB_LOOPS (ddr)))
4116 break;
4118 if (j == dist_vects.length ())
4120 fprintf (file, "\n(Dist vectors from the first dependence analyzer:\n");
4121 print_dist_vectors (file, dist_vects, DDR_NB_LOOPS (ddr));
4122 fprintf (file, "not found in Omega dist vectors:\n");
4123 print_dist_vectors (file, DDR_DIST_VECTS (ddr), DDR_NB_LOOPS (ddr));
4124 fprintf (file, "data dependence relation:\n");
4125 dump_data_dependence_relation (file, ddr);
4126 fprintf (file, ")\n");
4130 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
4132 lambda_vector a_dir_v;
4133 lambda_vector b_dir_v = DDR_DIR_VECT (ddr, i);
4135 /* Direction vectors are not ordered in the same way in the DDR
4136 and in the DIR_VECTS: search for a matching vector. */
4137 FOR_EACH_VEC_ELT (dir_vects, j, a_dir_v)
4138 if (lambda_vector_equal (a_dir_v, b_dir_v, DDR_NB_LOOPS (ddr)))
4139 break;
4141 if (j == dist_vects.length ())
4143 fprintf (file, "\n(Dir vectors from the first dependence analyzer:\n");
4144 print_dir_vectors (file, dir_vects, DDR_NB_LOOPS (ddr));
4145 fprintf (file, "not found in Omega dir vectors:\n");
4146 print_dir_vectors (file, DDR_DIR_VECTS (ddr), DDR_NB_LOOPS (ddr));
4147 fprintf (file, "data dependence relation:\n");
4148 dump_data_dependence_relation (file, ddr);
4149 fprintf (file, ")\n");
4153 return true;
4156 /* This computes the affine dependence relation between A and B with
4157 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
4158 independence between two accesses, while CHREC_DONT_KNOW is used
4159 for representing the unknown relation.
4161 Note that it is possible to stop the computation of the dependence
4162 relation the first time we detect a CHREC_KNOWN element for a given
4163 subscript. */
4165 void
4166 compute_affine_dependence (struct data_dependence_relation *ddr,
4167 struct loop *loop_nest)
4169 struct data_reference *dra = DDR_A (ddr);
4170 struct data_reference *drb = DDR_B (ddr);
4172 if (dump_file && (dump_flags & TDF_DETAILS))
4174 fprintf (dump_file, "(compute_affine_dependence\n");
4175 fprintf (dump_file, " stmt_a: ");
4176 print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
4177 fprintf (dump_file, " stmt_b: ");
4178 print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
4181 /* Analyze only when the dependence relation is not yet known. */
4182 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4184 dependence_stats.num_dependence_tests++;
4186 if (access_functions_are_affine_or_constant_p (dra, loop_nest)
4187 && access_functions_are_affine_or_constant_p (drb, loop_nest))
4189 subscript_dependence_tester (ddr, loop_nest);
4191 if (flag_check_data_deps)
4193 /* Dump the dependences from the first algorithm. */
4194 if (dump_file && (dump_flags & TDF_DETAILS))
4196 fprintf (dump_file, "\n\nBanerjee Analyzer\n");
4197 dump_data_dependence_relation (dump_file, ddr);
4200 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4202 bool maybe_dependent;
4203 vec<lambda_vector> dir_vects, dist_vects;
4205 /* Save the result of the first DD analyzer. */
4206 dist_vects = DDR_DIST_VECTS (ddr);
4207 dir_vects = DDR_DIR_VECTS (ddr);
4209 /* Reset the information. */
4210 DDR_DIST_VECTS (ddr).create (0);
4211 DDR_DIR_VECTS (ddr).create (0);
4213 /* Compute the same information using Omega. */
4214 if (!init_omega_for_ddr (ddr, &maybe_dependent))
4215 goto csys_dont_know;
4217 if (dump_file && (dump_flags & TDF_DETAILS))
4219 fprintf (dump_file, "Omega Analyzer\n");
4220 dump_data_dependence_relation (dump_file, ddr);
4223 /* Check that we get the same information. */
4224 if (maybe_dependent)
4225 gcc_assert (ddr_consistent_p (stderr, ddr, dist_vects,
4226 dir_vects));
4231 /* As a last case, if the dependence cannot be determined, or if
4232 the dependence is considered too difficult to determine, answer
4233 "don't know". */
4234 else
4236 csys_dont_know:;
4237 dependence_stats.num_dependence_undetermined++;
4239 if (dump_file && (dump_flags & TDF_DETAILS))
4241 fprintf (dump_file, "Data ref a:\n");
4242 dump_data_reference (dump_file, dra);
4243 fprintf (dump_file, "Data ref b:\n");
4244 dump_data_reference (dump_file, drb);
4245 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
4247 finalize_ddr_dependent (ddr, chrec_dont_know);
4251 if (dump_file && (dump_flags & TDF_DETAILS))
4253 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4254 fprintf (dump_file, ") -> no dependence\n");
4255 else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
4256 fprintf (dump_file, ") -> dependence analysis failed\n");
4257 else
4258 fprintf (dump_file, ")\n");
4262 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
4263 the data references in DATAREFS, in the LOOP_NEST. When
4264 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
4265 relations. Return true when successful, i.e. data references number
4266 is small enough to be handled. */
4268 bool
4269 compute_all_dependences (vec<data_reference_p> datarefs,
4270 vec<ddr_p> *dependence_relations,
4271 vec<loop_p> loop_nest,
4272 bool compute_self_and_rr)
4274 struct data_dependence_relation *ddr;
4275 struct data_reference *a, *b;
4276 unsigned int i, j;
4278 if ((int) datarefs.length ()
4279 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
4281 struct data_dependence_relation *ddr;
4283 /* Insert a single relation into dependence_relations:
4284 chrec_dont_know. */
4285 ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
4286 dependence_relations->safe_push (ddr);
4287 return false;
4290 FOR_EACH_VEC_ELT (datarefs, i, a)
4291 for (j = i + 1; datarefs.iterate (j, &b); j++)
4292 if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
4294 ddr = initialize_data_dependence_relation (a, b, loop_nest);
4295 dependence_relations->safe_push (ddr);
4296 if (loop_nest.exists ())
4297 compute_affine_dependence (ddr, loop_nest[0]);
4300 if (compute_self_and_rr)
4301 FOR_EACH_VEC_ELT (datarefs, i, a)
4303 ddr = initialize_data_dependence_relation (a, a, loop_nest);
4304 dependence_relations->safe_push (ddr);
4305 if (loop_nest.exists ())
4306 compute_affine_dependence (ddr, loop_nest[0]);
4309 return true;
4312 /* Describes a location of a memory reference. */
4314 typedef struct data_ref_loc_d
4316 /* Position of the memory reference. */
4317 tree *pos;
4319 /* True if the memory reference is read. */
4320 bool is_read;
4321 } data_ref_loc;
4324 /* Stores the locations of memory references in STMT to REFERENCES. Returns
4325 true if STMT clobbers memory, false otherwise. */
4327 static bool
4328 get_references_in_stmt (gimple stmt, vec<data_ref_loc, va_heap> *references)
4330 bool clobbers_memory = false;
4331 data_ref_loc ref;
4332 tree *op0, *op1;
4333 enum gimple_code stmt_code = gimple_code (stmt);
4335 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4336 As we cannot model data-references to not spelled out
4337 accesses give up if they may occur. */
4338 if (stmt_code == GIMPLE_CALL
4339 && !(gimple_call_flags (stmt) & ECF_CONST))
4341 /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
4342 if (gimple_call_internal_p (stmt)
4343 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
4345 struct loop *loop = gimple_bb (stmt)->loop_father;
4346 tree uid = gimple_call_arg (stmt, 0);
4347 gcc_assert (TREE_CODE (uid) == SSA_NAME);
4348 if (loop == NULL
4349 || loop->simduid != SSA_NAME_VAR (uid))
4350 clobbers_memory = true;
4352 else
4353 clobbers_memory = true;
4355 else if (stmt_code == GIMPLE_ASM
4356 && (gimple_asm_volatile_p (stmt) || gimple_vuse (stmt)))
4357 clobbers_memory = true;
4359 if (!gimple_vuse (stmt))
4360 return clobbers_memory;
4362 if (stmt_code == GIMPLE_ASSIGN)
4364 tree base;
4365 op0 = gimple_assign_lhs_ptr (stmt);
4366 op1 = gimple_assign_rhs1_ptr (stmt);
4368 if (DECL_P (*op1)
4369 || (REFERENCE_CLASS_P (*op1)
4370 && (base = get_base_address (*op1))
4371 && TREE_CODE (base) != SSA_NAME))
4373 ref.pos = op1;
4374 ref.is_read = true;
4375 references->safe_push (ref);
4378 else if (stmt_code == GIMPLE_CALL)
4380 unsigned i, n;
4382 op0 = gimple_call_lhs_ptr (stmt);
4383 n = gimple_call_num_args (stmt);
4384 for (i = 0; i < n; i++)
4386 op1 = gimple_call_arg_ptr (stmt, i);
4388 if (DECL_P (*op1)
4389 || (REFERENCE_CLASS_P (*op1) && get_base_address (*op1)))
4391 ref.pos = op1;
4392 ref.is_read = true;
4393 references->safe_push (ref);
4397 else
4398 return clobbers_memory;
4400 if (*op0
4401 && (DECL_P (*op0)
4402 || (REFERENCE_CLASS_P (*op0) && get_base_address (*op0))))
4404 ref.pos = op0;
4405 ref.is_read = false;
4406 references->safe_push (ref);
4408 return clobbers_memory;
4411 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
4412 reference, returns false, otherwise returns true. NEST is the outermost
4413 loop of the loop nest in which the references should be analyzed. */
4415 bool
4416 find_data_references_in_stmt (struct loop *nest, gimple stmt,
4417 vec<data_reference_p> *datarefs)
4419 unsigned i;
4420 stack_vec<data_ref_loc, 2> references;
4421 data_ref_loc *ref;
4422 bool ret = true;
4423 data_reference_p dr;
4425 if (get_references_in_stmt (stmt, &references))
4426 return false;
4428 FOR_EACH_VEC_ELT (references, i, ref)
4430 dr = create_data_ref (nest, loop_containing_stmt (stmt),
4431 *ref->pos, stmt, ref->is_read);
4432 gcc_assert (dr != NULL);
4433 datarefs->safe_push (dr);
4435 references.release ();
4436 return ret;
4439 /* Stores the data references in STMT to DATAREFS. If there is an
4440 unanalyzable reference, returns false, otherwise returns true.
4441 NEST is the outermost loop of the loop nest in which the references
4442 should be instantiated, LOOP is the loop in which the references
4443 should be analyzed. */
4445 bool
4446 graphite_find_data_references_in_stmt (loop_p nest, loop_p loop, gimple stmt,
4447 vec<data_reference_p> *datarefs)
4449 unsigned i;
4450 stack_vec<data_ref_loc, 2> references;
4451 data_ref_loc *ref;
4452 bool ret = true;
4453 data_reference_p dr;
4455 if (get_references_in_stmt (stmt, &references))
4456 return false;
4458 FOR_EACH_VEC_ELT (references, i, ref)
4460 dr = create_data_ref (nest, loop, *ref->pos, stmt, ref->is_read);
4461 gcc_assert (dr != NULL);
4462 datarefs->safe_push (dr);
4465 references.release ();
4466 return ret;
4469 /* Search the data references in LOOP, and record the information into
4470 DATAREFS. Returns chrec_dont_know when failing to analyze a
4471 difficult case, returns NULL_TREE otherwise. */
4473 tree
4474 find_data_references_in_bb (struct loop *loop, basic_block bb,
4475 vec<data_reference_p> *datarefs)
4477 gimple_stmt_iterator bsi;
4479 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4481 gimple stmt = gsi_stmt (bsi);
4483 if (!find_data_references_in_stmt (loop, stmt, datarefs))
4485 struct data_reference *res;
4486 res = XCNEW (struct data_reference);
4487 datarefs->safe_push (res);
4489 return chrec_dont_know;
4493 return NULL_TREE;
4496 /* Search the data references in LOOP, and record the information into
4497 DATAREFS. Returns chrec_dont_know when failing to analyze a
4498 difficult case, returns NULL_TREE otherwise.
4500 TODO: This function should be made smarter so that it can handle address
4501 arithmetic as if they were array accesses, etc. */
4503 tree
4504 find_data_references_in_loop (struct loop *loop,
4505 vec<data_reference_p> *datarefs)
4507 basic_block bb, *bbs;
4508 unsigned int i;
4510 bbs = get_loop_body_in_dom_order (loop);
4512 for (i = 0; i < loop->num_nodes; i++)
4514 bb = bbs[i];
4516 if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
4518 free (bbs);
4519 return chrec_dont_know;
4522 free (bbs);
4524 return NULL_TREE;
4527 /* Recursive helper function. */
4529 static bool
4530 find_loop_nest_1 (struct loop *loop, vec<loop_p> *loop_nest)
4532 /* Inner loops of the nest should not contain siblings. Example:
4533 when there are two consecutive loops,
4535 | loop_0
4536 | loop_1
4537 | A[{0, +, 1}_1]
4538 | endloop_1
4539 | loop_2
4540 | A[{0, +, 1}_2]
4541 | endloop_2
4542 | endloop_0
4544 the dependence relation cannot be captured by the distance
4545 abstraction. */
4546 if (loop->next)
4547 return false;
4549 loop_nest->safe_push (loop);
4550 if (loop->inner)
4551 return find_loop_nest_1 (loop->inner, loop_nest);
4552 return true;
4555 /* Return false when the LOOP is not well nested. Otherwise return
4556 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4557 contain the loops from the outermost to the innermost, as they will
4558 appear in the classic distance vector. */
4560 bool
4561 find_loop_nest (struct loop *loop, vec<loop_p> *loop_nest)
4563 loop_nest->safe_push (loop);
4564 if (loop->inner)
4565 return find_loop_nest_1 (loop->inner, loop_nest);
4566 return true;
4569 /* Returns true when the data dependences have been computed, false otherwise.
4570 Given a loop nest LOOP, the following vectors are returned:
4571 DATAREFS is initialized to all the array elements contained in this loop,
4572 DEPENDENCE_RELATIONS contains the relations between the data references.
4573 Compute read-read and self relations if
4574 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4576 bool
4577 compute_data_dependences_for_loop (struct loop *loop,
4578 bool compute_self_and_read_read_dependences,
4579 vec<loop_p> *loop_nest,
4580 vec<data_reference_p> *datarefs,
4581 vec<ddr_p> *dependence_relations)
4583 bool res = true;
4585 memset (&dependence_stats, 0, sizeof (dependence_stats));
4587 /* If the loop nest is not well formed, or one of the data references
4588 is not computable, give up without spending time to compute other
4589 dependences. */
4590 if (!loop
4591 || !find_loop_nest (loop, loop_nest)
4592 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
4593 || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
4594 compute_self_and_read_read_dependences))
4595 res = false;
4597 if (dump_file && (dump_flags & TDF_STATS))
4599 fprintf (dump_file, "Dependence tester statistics:\n");
4601 fprintf (dump_file, "Number of dependence tests: %d\n",
4602 dependence_stats.num_dependence_tests);
4603 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
4604 dependence_stats.num_dependence_dependent);
4605 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
4606 dependence_stats.num_dependence_independent);
4607 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
4608 dependence_stats.num_dependence_undetermined);
4610 fprintf (dump_file, "Number of subscript tests: %d\n",
4611 dependence_stats.num_subscript_tests);
4612 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
4613 dependence_stats.num_subscript_undetermined);
4614 fprintf (dump_file, "Number of same subscript function: %d\n",
4615 dependence_stats.num_same_subscript_function);
4617 fprintf (dump_file, "Number of ziv tests: %d\n",
4618 dependence_stats.num_ziv);
4619 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4620 dependence_stats.num_ziv_dependent);
4621 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4622 dependence_stats.num_ziv_independent);
4623 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
4624 dependence_stats.num_ziv_unimplemented);
4626 fprintf (dump_file, "Number of siv tests: %d\n",
4627 dependence_stats.num_siv);
4628 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4629 dependence_stats.num_siv_dependent);
4630 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4631 dependence_stats.num_siv_independent);
4632 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4633 dependence_stats.num_siv_unimplemented);
4635 fprintf (dump_file, "Number of miv tests: %d\n",
4636 dependence_stats.num_miv);
4637 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4638 dependence_stats.num_miv_dependent);
4639 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4640 dependence_stats.num_miv_independent);
4641 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4642 dependence_stats.num_miv_unimplemented);
4645 return res;
4648 /* Returns true when the data dependences for the basic block BB have been
4649 computed, false otherwise.
4650 DATAREFS is initialized to all the array elements contained in this basic
4651 block, DEPENDENCE_RELATIONS contains the relations between the data
4652 references. Compute read-read and self relations if
4653 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4654 bool
4655 compute_data_dependences_for_bb (basic_block bb,
4656 bool compute_self_and_read_read_dependences,
4657 vec<data_reference_p> *datarefs,
4658 vec<ddr_p> *dependence_relations)
4660 if (find_data_references_in_bb (NULL, bb, datarefs) == chrec_dont_know)
4661 return false;
4663 return compute_all_dependences (*datarefs, dependence_relations, vNULL,
4664 compute_self_and_read_read_dependences);
4667 /* Entry point (for testing only). Analyze all the data references
4668 and the dependence relations in LOOP.
4670 The data references are computed first.
4672 A relation on these nodes is represented by a complete graph. Some
4673 of the relations could be of no interest, thus the relations can be
4674 computed on demand.
4676 In the following function we compute all the relations. This is
4677 just a first implementation that is here for:
4678 - for showing how to ask for the dependence relations,
4679 - for the debugging the whole dependence graph,
4680 - for the dejagnu testcases and maintenance.
4682 It is possible to ask only for a part of the graph, avoiding to
4683 compute the whole dependence graph. The computed dependences are
4684 stored in a knowledge base (KB) such that later queries don't
4685 recompute the same information. The implementation of this KB is
4686 transparent to the optimizer, and thus the KB can be changed with a
4687 more efficient implementation, or the KB could be disabled. */
4688 static void
4689 analyze_all_data_dependences (struct loop *loop)
4691 unsigned int i;
4692 int nb_data_refs = 10;
4693 vec<data_reference_p> datarefs;
4694 datarefs.create (nb_data_refs);
4695 vec<ddr_p> dependence_relations;
4696 dependence_relations.create (nb_data_refs * nb_data_refs);
4697 vec<loop_p> loop_nest;
4698 loop_nest.create (3);
4700 /* Compute DDs on the whole function. */
4701 compute_data_dependences_for_loop (loop, false, &loop_nest, &datarefs,
4702 &dependence_relations);
4704 if (dump_file)
4706 dump_data_dependence_relations (dump_file, dependence_relations);
4707 fprintf (dump_file, "\n\n");
4709 if (dump_flags & TDF_DETAILS)
4710 dump_dist_dir_vectors (dump_file, dependence_relations);
4712 if (dump_flags & TDF_STATS)
4714 unsigned nb_top_relations = 0;
4715 unsigned nb_bot_relations = 0;
4716 unsigned nb_chrec_relations = 0;
4717 struct data_dependence_relation *ddr;
4719 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4721 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
4722 nb_top_relations++;
4724 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4725 nb_bot_relations++;
4727 else
4728 nb_chrec_relations++;
4731 gather_stats_on_scev_database ();
4735 loop_nest.release ();
4736 free_dependence_relations (dependence_relations);
4737 free_data_refs (datarefs);
4740 /* Computes all the data dependences and check that the results of
4741 several analyzers are the same. */
4743 void
4744 tree_check_data_deps (void)
4746 loop_iterator li;
4747 struct loop *loop_nest;
4749 FOR_EACH_LOOP (li, loop_nest, 0)
4750 analyze_all_data_dependences (loop_nest);
4753 /* Free the memory used by a data dependence relation DDR. */
4755 void
4756 free_dependence_relation (struct data_dependence_relation *ddr)
4758 if (ddr == NULL)
4759 return;
4761 if (DDR_SUBSCRIPTS (ddr).exists ())
4762 free_subscripts (DDR_SUBSCRIPTS (ddr));
4763 DDR_DIST_VECTS (ddr).release ();
4764 DDR_DIR_VECTS (ddr).release ();
4766 free (ddr);
4769 /* Free the memory used by the data dependence relations from
4770 DEPENDENCE_RELATIONS. */
4772 void
4773 free_dependence_relations (vec<ddr_p> dependence_relations)
4775 unsigned int i;
4776 struct data_dependence_relation *ddr;
4778 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4779 if (ddr)
4780 free_dependence_relation (ddr);
4782 dependence_relations.release ();
4785 /* Free the memory used by the data references from DATAREFS. */
4787 void
4788 free_data_refs (vec<data_reference_p> datarefs)
4790 unsigned int i;
4791 struct data_reference *dr;
4793 FOR_EACH_VEC_ELT (datarefs, i, dr)
4794 free_data_ref (dr);
4795 datarefs.release ();