1 /* Data references and dependences detectors.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This pass walks a given loop structure searching for array
23 references. The information about the array accesses is recorded
24 in DATA_REFERENCE structures.
26 The basic test for determining the dependences is:
27 given two access functions chrec1 and chrec2 to a same array, and
28 x and y two vectors from the iteration domain, the same element of
29 the array is accessed twice at iterations x and y if and only if:
30 | chrec1 (x) == chrec2 (y).
32 The goals of this analysis are:
34 - to determine the independence: the relation between two
35 independent accesses is qualified with the chrec_known (this
36 information allows a loop parallelization),
38 - when two data references access the same data, to qualify the
39 dependence relation with classic dependence representations:
43 - loop carried level dependence
44 - polyhedron dependence
45 or with the chains of recurrences based representation,
47 - to define a knowledge base for storing the data dependence
50 - to define an interface to access this data.
55 - subscript: given two array accesses a subscript is the tuple
56 composed of the access functions for a given dimension. Example:
57 Given A[f1][f2][f3] and B[g1][g2][g3], there are three subscripts:
58 (f1, g1), (f2, g2), (f3, g3).
60 - Diophantine equation: an equation whose coefficients and
61 solutions are integer constants, for example the equation
63 has an integer solution x = 1 and y = -1.
67 - "Advanced Compilation for High Performance Computing" by Randy
68 Allen and Ken Kennedy.
69 http://citeseer.ist.psu.edu/goff91practical.html
71 - "Loop Transformations for Restructuring Compilers - The Foundations"
79 #include "coretypes.h"
80 #include "gimple-pretty-print.h"
81 #include "tree-flow.h"
83 #include "tree-data-ref.h"
84 #include "tree-scalar-evolution.h"
85 #include "tree-pass.h"
86 #include "langhooks.h"
88 static struct datadep_stats
90 int num_dependence_tests;
91 int num_dependence_dependent;
92 int num_dependence_independent;
93 int num_dependence_undetermined;
95 int num_subscript_tests;
96 int num_subscript_undetermined;
97 int num_same_subscript_function;
100 int num_ziv_independent;
101 int num_ziv_dependent;
102 int num_ziv_unimplemented;
105 int num_siv_independent;
106 int num_siv_dependent;
107 int num_siv_unimplemented;
110 int num_miv_independent;
111 int num_miv_dependent;
112 int num_miv_unimplemented;
115 static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
116 struct data_reference *,
117 struct data_reference *,
119 /* Returns true iff A divides B. */
122 tree_fold_divides_p (const_tree a, const_tree b)
124 gcc_assert (TREE_CODE (a) == INTEGER_CST);
125 gcc_assert (TREE_CODE (b) == INTEGER_CST);
126 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a, 0));
129 /* Returns true iff A divides B. */
132 int_divides_p (int a, int b)
134 return ((b % a) == 0);
139 /* Dump into FILE all the data references from DATAREFS. */
142 dump_data_references (FILE *file, VEC (data_reference_p, heap) *datarefs)
145 struct data_reference *dr;
147 FOR_EACH_VEC_ELT (data_reference_p, datarefs, i, dr)
148 dump_data_reference (file, dr);
151 /* Dump into STDERR all the data references from DATAREFS. */
154 debug_data_references (VEC (data_reference_p, heap) *datarefs)
156 dump_data_references (stderr, datarefs);
159 /* Dump to STDERR all the dependence relations from DDRS. */
162 debug_data_dependence_relations (VEC (ddr_p, heap) *ddrs)
164 dump_data_dependence_relations (stderr, ddrs);
167 /* Dump into FILE all the dependence relations from DDRS. */
170 dump_data_dependence_relations (FILE *file,
171 VEC (ddr_p, heap) *ddrs)
174 struct data_dependence_relation *ddr;
176 FOR_EACH_VEC_ELT (ddr_p, ddrs, i, ddr)
177 dump_data_dependence_relation (file, ddr);
180 /* Print to STDERR the data_reference DR. */
183 debug_data_reference (struct data_reference *dr)
185 dump_data_reference (stderr, dr);
188 /* Dump function for a DATA_REFERENCE structure. */
191 dump_data_reference (FILE *outf,
192 struct data_reference *dr)
196 fprintf (outf, "#(Data Ref: \n");
197 fprintf (outf, "# bb: %d \n", gimple_bb (DR_STMT (dr))->index);
198 fprintf (outf, "# stmt: ");
199 print_gimple_stmt (outf, DR_STMT (dr), 0, 0);
200 fprintf (outf, "# ref: ");
201 print_generic_stmt (outf, DR_REF (dr), 0);
202 fprintf (outf, "# base_object: ");
203 print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
205 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
207 fprintf (outf, "# Access function %d: ", i);
208 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
210 fprintf (outf, "#)\n");
213 /* Dumps the affine function described by FN to the file OUTF. */
216 dump_affine_function (FILE *outf, affine_fn fn)
221 print_generic_expr (outf, VEC_index (tree, fn, 0), TDF_SLIM);
222 for (i = 1; VEC_iterate (tree, fn, i, coef); i++)
224 fprintf (outf, " + ");
225 print_generic_expr (outf, coef, TDF_SLIM);
226 fprintf (outf, " * x_%u", i);
230 /* Dumps the conflict function CF to the file OUTF. */
233 dump_conflict_function (FILE *outf, conflict_function *cf)
237 if (cf->n == NO_DEPENDENCE)
238 fprintf (outf, "no dependence\n");
239 else if (cf->n == NOT_KNOWN)
240 fprintf (outf, "not known\n");
243 for (i = 0; i < cf->n; i++)
246 dump_affine_function (outf, cf->fns[i]);
247 fprintf (outf, "]\n");
252 /* Dump function for a SUBSCRIPT structure. */
255 dump_subscript (FILE *outf, struct subscript *subscript)
257 conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
259 fprintf (outf, "\n (subscript \n");
260 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
261 dump_conflict_function (outf, cf);
262 if (CF_NONTRIVIAL_P (cf))
264 tree last_iteration = SUB_LAST_CONFLICT (subscript);
265 fprintf (outf, " last_conflict: ");
266 print_generic_stmt (outf, last_iteration, 0);
269 cf = SUB_CONFLICTS_IN_B (subscript);
270 fprintf (outf, " iterations_that_access_an_element_twice_in_B: ");
271 dump_conflict_function (outf, cf);
272 if (CF_NONTRIVIAL_P (cf))
274 tree last_iteration = SUB_LAST_CONFLICT (subscript);
275 fprintf (outf, " last_conflict: ");
276 print_generic_stmt (outf, last_iteration, 0);
279 fprintf (outf, " (Subscript distance: ");
280 print_generic_stmt (outf, SUB_DISTANCE (subscript), 0);
281 fprintf (outf, " )\n");
282 fprintf (outf, " )\n");
285 /* Print the classic direction vector DIRV to OUTF. */
288 print_direction_vector (FILE *outf,
294 for (eq = 0; eq < length; eq++)
296 enum data_dependence_direction dir = ((enum data_dependence_direction)
302 fprintf (outf, " +");
305 fprintf (outf, " -");
308 fprintf (outf, " =");
310 case dir_positive_or_equal:
311 fprintf (outf, " +=");
313 case dir_positive_or_negative:
314 fprintf (outf, " +-");
316 case dir_negative_or_equal:
317 fprintf (outf, " -=");
320 fprintf (outf, " *");
323 fprintf (outf, "indep");
327 fprintf (outf, "\n");
330 /* Print a vector of direction vectors. */
333 print_dir_vectors (FILE *outf, VEC (lambda_vector, heap) *dir_vects,
339 FOR_EACH_VEC_ELT (lambda_vector, dir_vects, j, v)
340 print_direction_vector (outf, v, length);
343 /* Print a vector of distance vectors. */
346 print_dist_vectors (FILE *outf, VEC (lambda_vector, heap) *dist_vects,
352 FOR_EACH_VEC_ELT (lambda_vector, dist_vects, j, v)
353 print_lambda_vector (outf, v, length);
359 debug_data_dependence_relation (struct data_dependence_relation *ddr)
361 dump_data_dependence_relation (stderr, ddr);
364 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
367 dump_data_dependence_relation (FILE *outf,
368 struct data_dependence_relation *ddr)
370 struct data_reference *dra, *drb;
372 fprintf (outf, "(Data Dep: \n");
374 if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
381 dump_data_reference (outf, dra);
383 fprintf (outf, " (nil)\n");
385 dump_data_reference (outf, drb);
387 fprintf (outf, " (nil)\n");
389 fprintf (outf, " (don't know)\n)\n");
395 dump_data_reference (outf, dra);
396 dump_data_reference (outf, drb);
398 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
399 fprintf (outf, " (no dependence)\n");
401 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
406 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
408 fprintf (outf, " access_fn_A: ");
409 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
410 fprintf (outf, " access_fn_B: ");
411 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
412 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
415 fprintf (outf, " inner loop index: %d\n", DDR_INNER_LOOP (ddr));
416 fprintf (outf, " loop nest: (");
417 FOR_EACH_VEC_ELT (loop_p, DDR_LOOP_NEST (ddr), i, loopi)
418 fprintf (outf, "%d ", loopi->num);
419 fprintf (outf, ")\n");
421 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
423 fprintf (outf, " distance_vector: ");
424 print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
428 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
430 fprintf (outf, " direction_vector: ");
431 print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
436 fprintf (outf, ")\n");
439 /* Dump function for a DATA_DEPENDENCE_DIRECTION structure. */
442 dump_data_dependence_direction (FILE *file,
443 enum data_dependence_direction dir)
459 case dir_positive_or_negative:
460 fprintf (file, "+-");
463 case dir_positive_or_equal:
464 fprintf (file, "+=");
467 case dir_negative_or_equal:
468 fprintf (file, "-=");
480 /* Dumps the distance and direction vectors in FILE. DDRS contains
481 the dependence relations, and VECT_SIZE is the size of the
482 dependence vectors, or in other words the number of loops in the
486 dump_dist_dir_vectors (FILE *file, VEC (ddr_p, heap) *ddrs)
489 struct data_dependence_relation *ddr;
492 FOR_EACH_VEC_ELT (ddr_p, ddrs, i, ddr)
493 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
495 FOR_EACH_VEC_ELT (lambda_vector, DDR_DIST_VECTS (ddr), j, v)
497 fprintf (file, "DISTANCE_V (");
498 print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
499 fprintf (file, ")\n");
502 FOR_EACH_VEC_ELT (lambda_vector, DDR_DIR_VECTS (ddr), j, v)
504 fprintf (file, "DIRECTION_V (");
505 print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
506 fprintf (file, ")\n");
510 fprintf (file, "\n\n");
513 /* Dumps the data dependence relations DDRS in FILE. */
516 dump_ddrs (FILE *file, VEC (ddr_p, heap) *ddrs)
519 struct data_dependence_relation *ddr;
521 FOR_EACH_VEC_ELT (ddr_p, ddrs, i, ddr)
522 dump_data_dependence_relation (file, ddr);
524 fprintf (file, "\n\n");
527 /* Helper function for split_constant_offset. Expresses OP0 CODE OP1
528 (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
529 constant of type ssizetype, and returns true. If we cannot do this
530 with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
534 split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
535 tree *var, tree *off)
539 enum tree_code ocode = code;
547 *var = build_int_cst (type, 0);
548 *off = fold_convert (ssizetype, op0);
551 case POINTER_PLUS_EXPR:
556 split_constant_offset (op0, &var0, &off0);
557 split_constant_offset (op1, &var1, &off1);
558 *var = fold_build2 (code, type, var0, var1);
559 *off = size_binop (ocode, off0, off1);
563 if (TREE_CODE (op1) != INTEGER_CST)
566 split_constant_offset (op0, &var0, &off0);
567 *var = fold_build2 (MULT_EXPR, type, var0, op1);
568 *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
574 HOST_WIDE_INT pbitsize, pbitpos;
575 enum machine_mode pmode;
576 int punsignedp, pvolatilep;
578 op0 = TREE_OPERAND (op0, 0);
579 if (!handled_component_p (op0))
582 base = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset,
583 &pmode, &punsignedp, &pvolatilep, false);
585 if (pbitpos % BITS_PER_UNIT != 0)
587 base = build_fold_addr_expr (base);
588 off0 = ssize_int (pbitpos / BITS_PER_UNIT);
592 split_constant_offset (poffset, &poffset, &off1);
593 off0 = size_binop (PLUS_EXPR, off0, off1);
594 if (POINTER_TYPE_P (TREE_TYPE (base)))
595 base = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (base),
596 base, fold_convert (sizetype, poffset));
598 base = fold_build2 (PLUS_EXPR, TREE_TYPE (base), base,
599 fold_convert (TREE_TYPE (base), poffset));
602 var0 = fold_convert (type, base);
604 /* If variable length types are involved, punt, otherwise casts
605 might be converted into ARRAY_REFs in gimplify_conversion.
606 To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
607 possibly no longer appears in current GIMPLE, might resurface.
608 This perhaps could run
609 if (CONVERT_EXPR_P (var0))
611 gimplify_conversion (&var0);
612 // Attempt to fill in any within var0 found ARRAY_REF's
613 // element size from corresponding op embedded ARRAY_REF,
614 // if unsuccessful, just punt.
616 while (POINTER_TYPE_P (type))
617 type = TREE_TYPE (type);
618 if (int_size_in_bytes (type) < 0)
628 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
629 enum tree_code subcode;
631 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
634 var0 = gimple_assign_rhs1 (def_stmt);
635 subcode = gimple_assign_rhs_code (def_stmt);
636 var1 = gimple_assign_rhs2 (def_stmt);
638 return split_constant_offset_1 (type, var0, subcode, var1, var, off);
642 /* We must not introduce undefined overflow, and we must not change the value.
643 Hence we're okay if the inner type doesn't overflow to start with
644 (pointer or signed), the outer type also is an integer or pointer
645 and the outer precision is at least as large as the inner. */
646 tree itype = TREE_TYPE (op0);
647 if ((POINTER_TYPE_P (itype)
648 || (INTEGRAL_TYPE_P (itype) && TYPE_OVERFLOW_UNDEFINED (itype)))
649 && TYPE_PRECISION (type) >= TYPE_PRECISION (itype)
650 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)))
652 split_constant_offset (op0, &var0, off);
653 *var = fold_convert (type, var0);
664 /* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
665 will be ssizetype. */
668 split_constant_offset (tree exp, tree *var, tree *off)
670 tree type = TREE_TYPE (exp), otype, op0, op1, e, o;
674 *off = ssize_int (0);
677 if (automatically_generated_chrec_p (exp))
680 otype = TREE_TYPE (exp);
681 code = TREE_CODE (exp);
682 extract_ops_from_tree (exp, &code, &op0, &op1);
683 if (split_constant_offset_1 (otype, op0, code, op1, &e, &o))
685 *var = fold_convert (type, e);
690 /* Returns the address ADDR of an object in a canonical shape (without nop
691 casts, and with type of pointer to the object). */
694 canonicalize_base_object_address (tree addr)
700 /* The base address may be obtained by casting from integer, in that case
702 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
705 if (TREE_CODE (addr) != ADDR_EXPR)
708 return build_fold_addr_expr (TREE_OPERAND (addr, 0));
711 /* Analyzes the behavior of the memory reference DR in the innermost loop or
712 basic block that contains it. Returns true if analysis succeed or false
716 dr_analyze_innermost (struct data_reference *dr)
718 gimple stmt = DR_STMT (dr);
719 struct loop *loop = loop_containing_stmt (stmt);
720 tree ref = DR_REF (dr);
721 HOST_WIDE_INT pbitsize, pbitpos;
723 enum machine_mode pmode;
724 int punsignedp, pvolatilep;
725 affine_iv base_iv, offset_iv;
726 tree init, dinit, step;
727 bool in_loop = (loop && loop->num);
729 if (dump_file && (dump_flags & TDF_DETAILS))
730 fprintf (dump_file, "analyze_innermost: ");
732 base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset,
733 &pmode, &punsignedp, &pvolatilep, false);
734 gcc_assert (base != NULL_TREE);
736 if (pbitpos % BITS_PER_UNIT != 0)
738 if (dump_file && (dump_flags & TDF_DETAILS))
739 fprintf (dump_file, "failed: bit offset alignment.\n");
743 if (TREE_CODE (base) == MEM_REF)
745 if (!integer_zerop (TREE_OPERAND (base, 1)))
749 double_int moff = mem_ref_offset (base);
750 poffset = double_int_to_tree (sizetype, moff);
753 poffset = size_binop (PLUS_EXPR, poffset, TREE_OPERAND (base, 1));
755 base = TREE_OPERAND (base, 0);
758 base = build_fold_addr_expr (base);
761 if (!simple_iv (loop, loop_containing_stmt (stmt), base, &base_iv,
764 if (dump_file && (dump_flags & TDF_DETAILS))
765 fprintf (dump_file, "failed: evolution of base is not affine.\n");
772 base_iv.step = ssize_int (0);
773 base_iv.no_overflow = true;
778 offset_iv.base = ssize_int (0);
779 offset_iv.step = ssize_int (0);
785 offset_iv.base = poffset;
786 offset_iv.step = ssize_int (0);
788 else if (!simple_iv (loop, loop_containing_stmt (stmt),
789 poffset, &offset_iv, false))
791 if (dump_file && (dump_flags & TDF_DETAILS))
792 fprintf (dump_file, "failed: evolution of offset is not"
798 init = ssize_int (pbitpos / BITS_PER_UNIT);
799 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
800 init = size_binop (PLUS_EXPR, init, dinit);
801 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
802 init = size_binop (PLUS_EXPR, init, dinit);
804 step = size_binop (PLUS_EXPR,
805 fold_convert (ssizetype, base_iv.step),
806 fold_convert (ssizetype, offset_iv.step));
808 DR_BASE_ADDRESS (dr) = canonicalize_base_object_address (base_iv.base);
810 DR_OFFSET (dr) = fold_convert (ssizetype, offset_iv.base);
814 DR_ALIGNED_TO (dr) = size_int (highest_pow2_factor (offset_iv.base));
816 if (dump_file && (dump_flags & TDF_DETAILS))
817 fprintf (dump_file, "success.\n");
822 /* Determines the base object and the list of indices of memory reference
823 DR, analyzed in LOOP and instantiated in loop nest NEST. */
826 dr_analyze_indices (struct data_reference *dr, loop_p nest, loop_p loop)
828 VEC (tree, heap) *access_fns = NULL;
829 tree ref = unshare_expr (DR_REF (dr)), aref = ref, op;
830 tree base, off, access_fn = NULL_TREE;
831 basic_block before_loop = NULL;
834 before_loop = block_before_loop (nest);
836 while (handled_component_p (aref))
838 if (TREE_CODE (aref) == ARRAY_REF)
840 op = TREE_OPERAND (aref, 1);
843 access_fn = analyze_scalar_evolution (loop, op);
844 access_fn = instantiate_scev (before_loop, loop, access_fn);
845 VEC_safe_push (tree, heap, access_fns, access_fn);
848 TREE_OPERAND (aref, 1) = build_int_cst (TREE_TYPE (op), 0);
851 aref = TREE_OPERAND (aref, 0);
855 && (INDIRECT_REF_P (aref)
856 || TREE_CODE (aref) == MEM_REF))
858 op = TREE_OPERAND (aref, 0);
859 access_fn = analyze_scalar_evolution (loop, op);
860 access_fn = instantiate_scev (before_loop, loop, access_fn);
861 base = initial_condition (access_fn);
862 split_constant_offset (base, &base, &off);
863 if (TREE_CODE (aref) == MEM_REF)
864 off = size_binop (PLUS_EXPR, off,
865 fold_convert (ssizetype, TREE_OPERAND (aref, 1)));
866 access_fn = chrec_replace_initial_condition (access_fn,
867 fold_convert (TREE_TYPE (base), off));
869 TREE_OPERAND (aref, 0) = base;
870 VEC_safe_push (tree, heap, access_fns, access_fn);
873 if (TREE_CODE (aref) == MEM_REF)
874 TREE_OPERAND (aref, 1)
875 = build_int_cst (TREE_TYPE (TREE_OPERAND (aref, 1)), 0);
877 if (TREE_CODE (ref) == MEM_REF
878 && TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR
879 && integer_zerop (TREE_OPERAND (ref, 1)))
880 ref = TREE_OPERAND (TREE_OPERAND (ref, 0), 0);
882 /* For canonicalization purposes we'd like to strip all outermost
883 zero-offset component-refs.
884 ??? For now simply handle zero-index array-refs. */
885 while (TREE_CODE (ref) == ARRAY_REF
886 && integer_zerop (TREE_OPERAND (ref, 1)))
887 ref = TREE_OPERAND (ref, 0);
889 DR_BASE_OBJECT (dr) = ref;
890 DR_ACCESS_FNS (dr) = access_fns;
893 /* Extracts the alias analysis information from the memory reference DR. */
896 dr_analyze_alias (struct data_reference *dr)
898 tree ref = DR_REF (dr);
899 tree base = get_base_address (ref), addr;
901 if (INDIRECT_REF_P (base)
902 || TREE_CODE (base) == MEM_REF)
904 addr = TREE_OPERAND (base, 0);
905 if (TREE_CODE (addr) == SSA_NAME)
906 DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
910 /* Returns true if the address of DR is invariant. */
913 dr_address_invariant_p (struct data_reference *dr)
918 FOR_EACH_VEC_ELT (tree, DR_ACCESS_FNS (dr), i, idx)
919 if (tree_contains_chrecs (idx, NULL))
925 /* Frees data reference DR. */
928 free_data_ref (data_reference_p dr)
930 VEC_free (tree, heap, DR_ACCESS_FNS (dr));
934 /* Analyzes memory reference MEMREF accessed in STMT. The reference
935 is read if IS_READ is true, write otherwise. Returns the
936 data_reference description of MEMREF. NEST is the outermost loop
937 in which the reference should be instantiated, LOOP is the loop in
938 which the data reference should be analyzed. */
940 struct data_reference *
941 create_data_ref (loop_p nest, loop_p loop, tree memref, gimple stmt,
944 struct data_reference *dr;
946 if (dump_file && (dump_flags & TDF_DETAILS))
948 fprintf (dump_file, "Creating dr for ");
949 print_generic_expr (dump_file, memref, TDF_SLIM);
950 fprintf (dump_file, "\n");
953 dr = XCNEW (struct data_reference);
955 DR_REF (dr) = memref;
956 DR_IS_READ (dr) = is_read;
958 dr_analyze_innermost (dr);
959 dr_analyze_indices (dr, nest, loop);
960 dr_analyze_alias (dr);
962 if (dump_file && (dump_flags & TDF_DETAILS))
964 fprintf (dump_file, "\tbase_address: ");
965 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
966 fprintf (dump_file, "\n\toffset from base address: ");
967 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
968 fprintf (dump_file, "\n\tconstant offset from base address: ");
969 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
970 fprintf (dump_file, "\n\tstep: ");
971 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
972 fprintf (dump_file, "\n\taligned to: ");
973 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
974 fprintf (dump_file, "\n\tbase_object: ");
975 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
976 fprintf (dump_file, "\n");
982 /* Returns true if FNA == FNB. */
985 affine_function_equal_p (affine_fn fna, affine_fn fnb)
987 unsigned i, n = VEC_length (tree, fna);
989 if (n != VEC_length (tree, fnb))
992 for (i = 0; i < n; i++)
993 if (!operand_equal_p (VEC_index (tree, fna, i),
994 VEC_index (tree, fnb, i), 0))
1000 /* If all the functions in CF are the same, returns one of them,
1001 otherwise returns NULL. */
1004 common_affine_function (conflict_function *cf)
1009 if (!CF_NONTRIVIAL_P (cf))
1014 for (i = 1; i < cf->n; i++)
1015 if (!affine_function_equal_p (comm, cf->fns[i]))
1021 /* Returns the base of the affine function FN. */
1024 affine_function_base (affine_fn fn)
1026 return VEC_index (tree, fn, 0);
1029 /* Returns true if FN is a constant. */
1032 affine_function_constant_p (affine_fn fn)
1037 for (i = 1; VEC_iterate (tree, fn, i, coef); i++)
1038 if (!integer_zerop (coef))
1044 /* Returns true if FN is the zero constant function. */
1047 affine_function_zero_p (affine_fn fn)
1049 return (integer_zerop (affine_function_base (fn))
1050 && affine_function_constant_p (fn));
1053 /* Returns a signed integer type with the largest precision from TA
1057 signed_type_for_types (tree ta, tree tb)
1059 if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
1060 return signed_type_for (ta);
1062 return signed_type_for (tb);
1065 /* Applies operation OP on affine functions FNA and FNB, and returns the
1069 affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
1075 if (VEC_length (tree, fnb) > VEC_length (tree, fna))
1077 n = VEC_length (tree, fna);
1078 m = VEC_length (tree, fnb);
1082 n = VEC_length (tree, fnb);
1083 m = VEC_length (tree, fna);
1086 ret = VEC_alloc (tree, heap, m);
1087 for (i = 0; i < n; i++)
1089 tree type = signed_type_for_types (TREE_TYPE (VEC_index (tree, fna, i)),
1090 TREE_TYPE (VEC_index (tree, fnb, i)));
1092 VEC_quick_push (tree, ret,
1093 fold_build2 (op, type,
1094 VEC_index (tree, fna, i),
1095 VEC_index (tree, fnb, i)));
1098 for (; VEC_iterate (tree, fna, i, coef); i++)
1099 VEC_quick_push (tree, ret,
1100 fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1101 coef, integer_zero_node));
1102 for (; VEC_iterate (tree, fnb, i, coef); i++)
1103 VEC_quick_push (tree, ret,
1104 fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1105 integer_zero_node, coef));
1110 /* Returns the sum of affine functions FNA and FNB. */
1113 affine_fn_plus (affine_fn fna, affine_fn fnb)
1115 return affine_fn_op (PLUS_EXPR, fna, fnb);
1118 /* Returns the difference of affine functions FNA and FNB. */
1121 affine_fn_minus (affine_fn fna, affine_fn fnb)
1123 return affine_fn_op (MINUS_EXPR, fna, fnb);
1126 /* Frees affine function FN. */
1129 affine_fn_free (affine_fn fn)
1131 VEC_free (tree, heap, fn);
1134 /* Determine for each subscript in the data dependence relation DDR
1138 compute_subscript_distance (struct data_dependence_relation *ddr)
1140 conflict_function *cf_a, *cf_b;
1141 affine_fn fn_a, fn_b, diff;
1143 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1147 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1149 struct subscript *subscript;
1151 subscript = DDR_SUBSCRIPT (ddr, i);
1152 cf_a = SUB_CONFLICTS_IN_A (subscript);
1153 cf_b = SUB_CONFLICTS_IN_B (subscript);
1155 fn_a = common_affine_function (cf_a);
1156 fn_b = common_affine_function (cf_b);
1159 SUB_DISTANCE (subscript) = chrec_dont_know;
1162 diff = affine_fn_minus (fn_a, fn_b);
1164 if (affine_function_constant_p (diff))
1165 SUB_DISTANCE (subscript) = affine_function_base (diff);
1167 SUB_DISTANCE (subscript) = chrec_dont_know;
1169 affine_fn_free (diff);
1174 /* Returns the conflict function for "unknown". */
1176 static conflict_function *
1177 conflict_fn_not_known (void)
1179 conflict_function *fn = XCNEW (conflict_function);
1185 /* Returns the conflict function for "independent". */
1187 static conflict_function *
1188 conflict_fn_no_dependence (void)
1190 conflict_function *fn = XCNEW (conflict_function);
1191 fn->n = NO_DEPENDENCE;
1196 /* Returns true if the address of OBJ is invariant in LOOP. */
1199 object_address_invariant_in_loop_p (const struct loop *loop, const_tree obj)
1201 while (handled_component_p (obj))
1203 if (TREE_CODE (obj) == ARRAY_REF)
1205 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1206 need to check the stride and the lower bound of the reference. */
1207 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1209 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 3),
1213 else if (TREE_CODE (obj) == COMPONENT_REF)
1215 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1219 obj = TREE_OPERAND (obj, 0);
1222 if (!INDIRECT_REF_P (obj)
1223 && TREE_CODE (obj) != MEM_REF)
1226 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
1230 /* Returns false if we can prove that data references A and B do not alias,
1234 dr_may_alias_p (const struct data_reference *a, const struct data_reference *b)
1236 tree addr_a = DR_BASE_OBJECT (a);
1237 tree addr_b = DR_BASE_OBJECT (b);
1239 if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
1240 return refs_output_dependent_p (addr_a, addr_b);
1241 else if (DR_IS_READ (a) && DR_IS_WRITE (b))
1242 return refs_anti_dependent_p (addr_a, addr_b);
1243 return refs_may_alias_p (addr_a, addr_b);
1246 static void compute_self_dependence (struct data_dependence_relation *);
1248 /* Initialize a data dependence relation between data accesses A and
1249 B. NB_LOOPS is the number of loops surrounding the references: the
1250 size of the classic distance/direction vectors. */
1252 static struct data_dependence_relation *
1253 initialize_data_dependence_relation (struct data_reference *a,
1254 struct data_reference *b,
1255 VEC (loop_p, heap) *loop_nest)
1257 struct data_dependence_relation *res;
1260 res = XNEW (struct data_dependence_relation);
1263 DDR_LOOP_NEST (res) = NULL;
1264 DDR_REVERSED_P (res) = false;
1265 DDR_SUBSCRIPTS (res) = NULL;
1266 DDR_DIR_VECTS (res) = NULL;
1267 DDR_DIST_VECTS (res) = NULL;
1269 if (a == NULL || b == NULL)
1271 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1275 /* If the data references do not alias, then they are independent. */
1276 if (!dr_may_alias_p (a, b))
1278 DDR_ARE_DEPENDENT (res) = chrec_known;
1282 /* When the references are exactly the same, don't spend time doing
1283 the data dependence tests, just initialize the ddr and return. */
1284 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
1286 DDR_AFFINE_P (res) = true;
1287 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1288 DDR_SUBSCRIPTS (res) = VEC_alloc (subscript_p, heap, DR_NUM_DIMENSIONS (a));
1289 DDR_LOOP_NEST (res) = loop_nest;
1290 DDR_INNER_LOOP (res) = 0;
1291 DDR_SELF_REFERENCE (res) = true;
1292 compute_self_dependence (res);
1296 /* If the references do not access the same object, we do not know
1297 whether they alias or not. */
1298 if (!operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0))
1300 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1304 /* If the base of the object is not invariant in the loop nest, we cannot
1305 analyze it. TODO -- in fact, it would suffice to record that there may
1306 be arbitrary dependences in the loops where the base object varies. */
1308 && !object_address_invariant_in_loop_p (VEC_index (loop_p, loop_nest, 0),
1309 DR_BASE_OBJECT (a)))
1311 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1315 /* If the number of dimensions of the access to not agree we can have
1316 a pointer access to a component of the array element type and an
1317 array access while the base-objects are still the same. Punt. */
1318 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1320 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1324 DDR_AFFINE_P (res) = true;
1325 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1326 DDR_SUBSCRIPTS (res) = VEC_alloc (subscript_p, heap, DR_NUM_DIMENSIONS (a));
1327 DDR_LOOP_NEST (res) = loop_nest;
1328 DDR_INNER_LOOP (res) = 0;
1329 DDR_SELF_REFERENCE (res) = false;
1331 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1333 struct subscript *subscript;
1335 subscript = XNEW (struct subscript);
1336 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1337 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1338 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1339 SUB_DISTANCE (subscript) = chrec_dont_know;
1340 VEC_safe_push (subscript_p, heap, DDR_SUBSCRIPTS (res), subscript);
1346 /* Frees memory used by the conflict function F. */
1349 free_conflict_function (conflict_function *f)
1353 if (CF_NONTRIVIAL_P (f))
1355 for (i = 0; i < f->n; i++)
1356 affine_fn_free (f->fns[i]);
1361 /* Frees memory used by SUBSCRIPTS. */
1364 free_subscripts (VEC (subscript_p, heap) *subscripts)
1369 FOR_EACH_VEC_ELT (subscript_p, subscripts, i, s)
1371 free_conflict_function (s->conflicting_iterations_in_a);
1372 free_conflict_function (s->conflicting_iterations_in_b);
1375 VEC_free (subscript_p, heap, subscripts);
1378 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1382 finalize_ddr_dependent (struct data_dependence_relation *ddr,
1385 if (dump_file && (dump_flags & TDF_DETAILS))
1387 fprintf (dump_file, "(dependence classified: ");
1388 print_generic_expr (dump_file, chrec, 0);
1389 fprintf (dump_file, ")\n");
1392 DDR_ARE_DEPENDENT (ddr) = chrec;
1393 free_subscripts (DDR_SUBSCRIPTS (ddr));
1394 DDR_SUBSCRIPTS (ddr) = NULL;
1397 /* The dependence relation DDR cannot be represented by a distance
1401 non_affine_dependence_relation (struct data_dependence_relation *ddr)
1403 if (dump_file && (dump_flags & TDF_DETAILS))
1404 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
1406 DDR_AFFINE_P (ddr) = false;
1411 /* This section contains the classic Banerjee tests. */
1413 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1414 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1417 ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1419 return (evolution_function_is_constant_p (chrec_a)
1420 && evolution_function_is_constant_p (chrec_b));
1423 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
1424 variable, i.e., if the SIV (Single Index Variable) test is true. */
1427 siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1429 if ((evolution_function_is_constant_p (chrec_a)
1430 && evolution_function_is_univariate_p (chrec_b))
1431 || (evolution_function_is_constant_p (chrec_b)
1432 && evolution_function_is_univariate_p (chrec_a)))
1435 if (evolution_function_is_univariate_p (chrec_a)
1436 && evolution_function_is_univariate_p (chrec_b))
1438 switch (TREE_CODE (chrec_a))
1440 case POLYNOMIAL_CHREC:
1441 switch (TREE_CODE (chrec_b))
1443 case POLYNOMIAL_CHREC:
1444 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
1459 /* Creates a conflict function with N dimensions. The affine functions
1460 in each dimension follow. */
1462 static conflict_function *
1463 conflict_fn (unsigned n, ...)
1466 conflict_function *ret = XCNEW (conflict_function);
1469 gcc_assert (0 < n && n <= MAX_DIM);
1473 for (i = 0; i < n; i++)
1474 ret->fns[i] = va_arg (ap, affine_fn);
1480 /* Returns constant affine function with value CST. */
1483 affine_fn_cst (tree cst)
1485 affine_fn fn = VEC_alloc (tree, heap, 1);
1486 VEC_quick_push (tree, fn, cst);
1490 /* Returns affine function with single variable, CST + COEF * x_DIM. */
1493 affine_fn_univar (tree cst, unsigned dim, tree coef)
1495 affine_fn fn = VEC_alloc (tree, heap, dim + 1);
1498 gcc_assert (dim > 0);
1499 VEC_quick_push (tree, fn, cst);
1500 for (i = 1; i < dim; i++)
1501 VEC_quick_push (tree, fn, integer_zero_node);
1502 VEC_quick_push (tree, fn, coef);
1506 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1507 *OVERLAPS_B are initialized to the functions that describe the
1508 relation between the elements accessed twice by CHREC_A and
1509 CHREC_B. For k >= 0, the following property is verified:
1511 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1514 analyze_ziv_subscript (tree chrec_a,
1516 conflict_function **overlaps_a,
1517 conflict_function **overlaps_b,
1518 tree *last_conflicts)
1520 tree type, difference;
1521 dependence_stats.num_ziv++;
1523 if (dump_file && (dump_flags & TDF_DETAILS))
1524 fprintf (dump_file, "(analyze_ziv_subscript \n");
1526 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1527 chrec_a = chrec_convert (type, chrec_a, NULL);
1528 chrec_b = chrec_convert (type, chrec_b, NULL);
1529 difference = chrec_fold_minus (type, chrec_a, chrec_b);
1531 switch (TREE_CODE (difference))
1534 if (integer_zerop (difference))
1536 /* The difference is equal to zero: the accessed index
1537 overlaps for each iteration in the loop. */
1538 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1539 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1540 *last_conflicts = chrec_dont_know;
1541 dependence_stats.num_ziv_dependent++;
1545 /* The accesses do not overlap. */
1546 *overlaps_a = conflict_fn_no_dependence ();
1547 *overlaps_b = conflict_fn_no_dependence ();
1548 *last_conflicts = integer_zero_node;
1549 dependence_stats.num_ziv_independent++;
1554 /* We're not sure whether the indexes overlap. For the moment,
1555 conservatively answer "don't know". */
1556 if (dump_file && (dump_flags & TDF_DETAILS))
1557 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
1559 *overlaps_a = conflict_fn_not_known ();
1560 *overlaps_b = conflict_fn_not_known ();
1561 *last_conflicts = chrec_dont_know;
1562 dependence_stats.num_ziv_unimplemented++;
1566 if (dump_file && (dump_flags & TDF_DETAILS))
1567 fprintf (dump_file, ")\n");
1570 /* Sets NIT to the estimated number of executions of the statements in
1571 LOOP. If CONSERVATIVE is true, we must be sure that NIT is at least as
1572 large as the number of iterations. If we have no reliable estimate,
1573 the function returns false, otherwise returns true. */
1576 estimated_loop_iterations (struct loop *loop, bool conservative,
1579 estimate_numbers_of_iterations_loop (loop, true);
1582 if (!loop->any_upper_bound)
1585 *nit = loop->nb_iterations_upper_bound;
1589 if (!loop->any_estimate)
1592 *nit = loop->nb_iterations_estimate;
1598 /* Similar to estimated_loop_iterations, but returns the estimate only
1599 if it fits to HOST_WIDE_INT. If this is not the case, or the estimate
1600 on the number of iterations of LOOP could not be derived, returns -1. */
1603 estimated_loop_iterations_int (struct loop *loop, bool conservative)
1606 HOST_WIDE_INT hwi_nit;
1608 if (!estimated_loop_iterations (loop, conservative, &nit))
1611 if (!double_int_fits_in_shwi_p (nit))
1613 hwi_nit = double_int_to_shwi (nit);
1615 return hwi_nit < 0 ? -1 : hwi_nit;
1618 /* Similar to estimated_loop_iterations, but returns the estimate as a tree,
1619 and only if it fits to the int type. If this is not the case, or the
1620 estimate on the number of iterations of LOOP could not be derived, returns
1624 estimated_loop_iterations_tree (struct loop *loop, bool conservative)
1629 if (!estimated_loop_iterations (loop, conservative, &nit))
1630 return chrec_dont_know;
1632 type = lang_hooks.types.type_for_size (INT_TYPE_SIZE, true);
1633 if (!double_int_fits_to_tree_p (type, nit))
1634 return chrec_dont_know;
1636 return double_int_to_tree (type, nit);
1639 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1640 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1641 *OVERLAPS_B are initialized to the functions that describe the
1642 relation between the elements accessed twice by CHREC_A and
1643 CHREC_B. For k >= 0, the following property is verified:
1645 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1648 analyze_siv_subscript_cst_affine (tree chrec_a,
1650 conflict_function **overlaps_a,
1651 conflict_function **overlaps_b,
1652 tree *last_conflicts)
1654 bool value0, value1, value2;
1655 tree type, difference, tmp;
1657 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1658 chrec_a = chrec_convert (type, chrec_a, NULL);
1659 chrec_b = chrec_convert (type, chrec_b, NULL);
1660 difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
1662 if (!chrec_is_positive (initial_condition (difference), &value0))
1664 if (dump_file && (dump_flags & TDF_DETAILS))
1665 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
1667 dependence_stats.num_siv_unimplemented++;
1668 *overlaps_a = conflict_fn_not_known ();
1669 *overlaps_b = conflict_fn_not_known ();
1670 *last_conflicts = chrec_dont_know;
1675 if (value0 == false)
1677 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
1679 if (dump_file && (dump_flags & TDF_DETAILS))
1680 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1682 *overlaps_a = conflict_fn_not_known ();
1683 *overlaps_b = conflict_fn_not_known ();
1684 *last_conflicts = chrec_dont_know;
1685 dependence_stats.num_siv_unimplemented++;
1694 chrec_b = {10, +, 1}
1697 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1699 HOST_WIDE_INT numiter;
1700 struct loop *loop = get_chrec_loop (chrec_b);
1702 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1703 tmp = fold_build2 (EXACT_DIV_EXPR, type,
1704 fold_build1 (ABS_EXPR, type, difference),
1705 CHREC_RIGHT (chrec_b));
1706 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1707 *last_conflicts = integer_one_node;
1710 /* Perform weak-zero siv test to see if overlap is
1711 outside the loop bounds. */
1712 numiter = estimated_loop_iterations_int (loop, false);
1715 && compare_tree_int (tmp, numiter) > 0)
1717 free_conflict_function (*overlaps_a);
1718 free_conflict_function (*overlaps_b);
1719 *overlaps_a = conflict_fn_no_dependence ();
1720 *overlaps_b = conflict_fn_no_dependence ();
1721 *last_conflicts = integer_zero_node;
1722 dependence_stats.num_siv_independent++;
1725 dependence_stats.num_siv_dependent++;
1729 /* When the step does not divide the difference, there are
1733 *overlaps_a = conflict_fn_no_dependence ();
1734 *overlaps_b = conflict_fn_no_dependence ();
1735 *last_conflicts = integer_zero_node;
1736 dependence_stats.num_siv_independent++;
1745 chrec_b = {10, +, -1}
1747 In this case, chrec_a will not overlap with chrec_b. */
1748 *overlaps_a = conflict_fn_no_dependence ();
1749 *overlaps_b = conflict_fn_no_dependence ();
1750 *last_conflicts = integer_zero_node;
1751 dependence_stats.num_siv_independent++;
1758 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
1760 if (dump_file && (dump_flags & TDF_DETAILS))
1761 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1763 *overlaps_a = conflict_fn_not_known ();
1764 *overlaps_b = conflict_fn_not_known ();
1765 *last_conflicts = chrec_dont_know;
1766 dependence_stats.num_siv_unimplemented++;
1771 if (value2 == false)
1775 chrec_b = {10, +, -1}
1777 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1779 HOST_WIDE_INT numiter;
1780 struct loop *loop = get_chrec_loop (chrec_b);
1782 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1783 tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
1784 CHREC_RIGHT (chrec_b));
1785 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1786 *last_conflicts = integer_one_node;
1788 /* Perform weak-zero siv test to see if overlap is
1789 outside the loop bounds. */
1790 numiter = estimated_loop_iterations_int (loop, false);
1793 && compare_tree_int (tmp, numiter) > 0)
1795 free_conflict_function (*overlaps_a);
1796 free_conflict_function (*overlaps_b);
1797 *overlaps_a = conflict_fn_no_dependence ();
1798 *overlaps_b = conflict_fn_no_dependence ();
1799 *last_conflicts = integer_zero_node;
1800 dependence_stats.num_siv_independent++;
1803 dependence_stats.num_siv_dependent++;
1807 /* When the step does not divide the difference, there
1811 *overlaps_a = conflict_fn_no_dependence ();
1812 *overlaps_b = conflict_fn_no_dependence ();
1813 *last_conflicts = integer_zero_node;
1814 dependence_stats.num_siv_independent++;
1824 In this case, chrec_a will not overlap with chrec_b. */
1825 *overlaps_a = conflict_fn_no_dependence ();
1826 *overlaps_b = conflict_fn_no_dependence ();
1827 *last_conflicts = integer_zero_node;
1828 dependence_stats.num_siv_independent++;
1836 /* Helper recursive function for initializing the matrix A. Returns
1837 the initial value of CHREC. */
1840 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
1844 switch (TREE_CODE (chrec))
1846 case POLYNOMIAL_CHREC:
1847 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST);
1849 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
1850 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
1856 tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
1857 tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
1859 return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
1864 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
1865 return chrec_convert (chrec_type (chrec), op, NULL);
1870 /* Handle ~X as -1 - X. */
1871 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
1872 return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
1873 build_int_cst (TREE_TYPE (chrec), -1), op);
1885 #define FLOOR_DIV(x,y) ((x) / (y))
1887 /* Solves the special case of the Diophantine equation:
1888 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
1890 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
1891 number of iterations that loops X and Y run. The overlaps will be
1892 constructed as evolutions in dimension DIM. */
1895 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
1896 affine_fn *overlaps_a,
1897 affine_fn *overlaps_b,
1898 tree *last_conflicts, int dim)
1900 if (((step_a > 0 && step_b > 0)
1901 || (step_a < 0 && step_b < 0)))
1903 int step_overlaps_a, step_overlaps_b;
1904 int gcd_steps_a_b, last_conflict, tau2;
1906 gcd_steps_a_b = gcd (step_a, step_b);
1907 step_overlaps_a = step_b / gcd_steps_a_b;
1908 step_overlaps_b = step_a / gcd_steps_a_b;
1912 tau2 = FLOOR_DIV (niter, step_overlaps_a);
1913 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
1914 last_conflict = tau2;
1915 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
1918 *last_conflicts = chrec_dont_know;
1920 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
1921 build_int_cst (NULL_TREE,
1923 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
1924 build_int_cst (NULL_TREE,
1930 *overlaps_a = affine_fn_cst (integer_zero_node);
1931 *overlaps_b = affine_fn_cst (integer_zero_node);
1932 *last_conflicts = integer_zero_node;
1936 /* Solves the special case of a Diophantine equation where CHREC_A is
1937 an affine bivariate function, and CHREC_B is an affine univariate
1938 function. For example,
1940 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
1942 has the following overlapping functions:
1944 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
1945 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
1946 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
1948 FORNOW: This is a specialized implementation for a case occurring in
1949 a common benchmark. Implement the general algorithm. */
1952 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
1953 conflict_function **overlaps_a,
1954 conflict_function **overlaps_b,
1955 tree *last_conflicts)
1957 bool xz_p, yz_p, xyz_p;
1958 int step_x, step_y, step_z;
1959 HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
1960 affine_fn overlaps_a_xz, overlaps_b_xz;
1961 affine_fn overlaps_a_yz, overlaps_b_yz;
1962 affine_fn overlaps_a_xyz, overlaps_b_xyz;
1963 affine_fn ova1, ova2, ovb;
1964 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
1966 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
1967 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
1968 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
1971 estimated_loop_iterations_int (get_chrec_loop (CHREC_LEFT (chrec_a)),
1973 niter_y = estimated_loop_iterations_int (get_chrec_loop (chrec_a), false);
1974 niter_z = estimated_loop_iterations_int (get_chrec_loop (chrec_b), false);
1976 if (niter_x < 0 || niter_y < 0 || niter_z < 0)
1978 if (dump_file && (dump_flags & TDF_DETAILS))
1979 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
1981 *overlaps_a = conflict_fn_not_known ();
1982 *overlaps_b = conflict_fn_not_known ();
1983 *last_conflicts = chrec_dont_know;
1987 niter = MIN (niter_x, niter_z);
1988 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
1991 &last_conflicts_xz, 1);
1992 niter = MIN (niter_y, niter_z);
1993 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
1996 &last_conflicts_yz, 2);
1997 niter = MIN (niter_x, niter_z);
1998 niter = MIN (niter_y, niter);
1999 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2002 &last_conflicts_xyz, 3);
2004 xz_p = !integer_zerop (last_conflicts_xz);
2005 yz_p = !integer_zerop (last_conflicts_yz);
2006 xyz_p = !integer_zerop (last_conflicts_xyz);
2008 if (xz_p || yz_p || xyz_p)
2010 ova1 = affine_fn_cst (integer_zero_node);
2011 ova2 = affine_fn_cst (integer_zero_node);
2012 ovb = affine_fn_cst (integer_zero_node);
2015 affine_fn t0 = ova1;
2018 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
2019 ovb = affine_fn_plus (ovb, overlaps_b_xz);
2020 affine_fn_free (t0);
2021 affine_fn_free (t2);
2022 *last_conflicts = last_conflicts_xz;
2026 affine_fn t0 = ova2;
2029 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
2030 ovb = affine_fn_plus (ovb, overlaps_b_yz);
2031 affine_fn_free (t0);
2032 affine_fn_free (t2);
2033 *last_conflicts = last_conflicts_yz;
2037 affine_fn t0 = ova1;
2038 affine_fn t2 = ova2;
2041 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
2042 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
2043 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
2044 affine_fn_free (t0);
2045 affine_fn_free (t2);
2046 affine_fn_free (t4);
2047 *last_conflicts = last_conflicts_xyz;
2049 *overlaps_a = conflict_fn (2, ova1, ova2);
2050 *overlaps_b = conflict_fn (1, ovb);
2054 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2055 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2056 *last_conflicts = integer_zero_node;
2059 affine_fn_free (overlaps_a_xz);
2060 affine_fn_free (overlaps_b_xz);
2061 affine_fn_free (overlaps_a_yz);
2062 affine_fn_free (overlaps_b_yz);
2063 affine_fn_free (overlaps_a_xyz);
2064 affine_fn_free (overlaps_b_xyz);
2067 /* Determines the overlapping elements due to accesses CHREC_A and
2068 CHREC_B, that are affine functions. This function cannot handle
2069 symbolic evolution functions, ie. when initial conditions are
2070 parameters, because it uses lambda matrices of integers. */
2073 analyze_subscript_affine_affine (tree chrec_a,
2075 conflict_function **overlaps_a,
2076 conflict_function **overlaps_b,
2077 tree *last_conflicts)
2079 unsigned nb_vars_a, nb_vars_b, dim;
2080 HOST_WIDE_INT init_a, init_b, gamma, gcd_alpha_beta;
2081 lambda_matrix A, U, S;
2082 struct obstack scratch_obstack;
2084 if (eq_evolutions_p (chrec_a, chrec_b))
2086 /* The accessed index overlaps for each iteration in the
2088 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2089 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2090 *last_conflicts = chrec_dont_know;
2093 if (dump_file && (dump_flags & TDF_DETAILS))
2094 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2096 /* For determining the initial intersection, we have to solve a
2097 Diophantine equation. This is the most time consuming part.
2099 For answering to the question: "Is there a dependence?" we have
2100 to prove that there exists a solution to the Diophantine
2101 equation, and that the solution is in the iteration domain,
2102 i.e. the solution is positive or zero, and that the solution
2103 happens before the upper bound loop.nb_iterations. Otherwise
2104 there is no dependence. This function outputs a description of
2105 the iterations that hold the intersections. */
2107 nb_vars_a = nb_vars_in_chrec (chrec_a);
2108 nb_vars_b = nb_vars_in_chrec (chrec_b);
2110 gcc_obstack_init (&scratch_obstack);
2112 dim = nb_vars_a + nb_vars_b;
2113 U = lambda_matrix_new (dim, dim, &scratch_obstack);
2114 A = lambda_matrix_new (dim, 1, &scratch_obstack);
2115 S = lambda_matrix_new (dim, 1, &scratch_obstack);
2117 init_a = int_cst_value (initialize_matrix_A (A, chrec_a, 0, 1));
2118 init_b = int_cst_value (initialize_matrix_A (A, chrec_b, nb_vars_a, -1));
2119 gamma = init_b - init_a;
2121 /* Don't do all the hard work of solving the Diophantine equation
2122 when we already know the solution: for example,
2125 | gamma = 3 - 3 = 0.
2126 Then the first overlap occurs during the first iterations:
2127 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2131 if (nb_vars_a == 1 && nb_vars_b == 1)
2133 HOST_WIDE_INT step_a, step_b;
2134 HOST_WIDE_INT niter, niter_a, niter_b;
2137 niter_a = estimated_loop_iterations_int (get_chrec_loop (chrec_a),
2139 niter_b = estimated_loop_iterations_int (get_chrec_loop (chrec_b),
2141 niter = MIN (niter_a, niter_b);
2142 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2143 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2145 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2148 *overlaps_a = conflict_fn (1, ova);
2149 *overlaps_b = conflict_fn (1, ovb);
2152 else if (nb_vars_a == 2 && nb_vars_b == 1)
2153 compute_overlap_steps_for_affine_1_2
2154 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2156 else if (nb_vars_a == 1 && nb_vars_b == 2)
2157 compute_overlap_steps_for_affine_1_2
2158 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2162 if (dump_file && (dump_flags & TDF_DETAILS))
2163 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
2164 *overlaps_a = conflict_fn_not_known ();
2165 *overlaps_b = conflict_fn_not_known ();
2166 *last_conflicts = chrec_dont_know;
2168 goto end_analyze_subs_aa;
2172 lambda_matrix_right_hermite (A, dim, 1, S, U);
2177 lambda_matrix_row_negate (U, dim, 0);
2179 gcd_alpha_beta = S[0][0];
2181 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2182 but that is a quite strange case. Instead of ICEing, answer
2184 if (gcd_alpha_beta == 0)
2186 *overlaps_a = conflict_fn_not_known ();
2187 *overlaps_b = conflict_fn_not_known ();
2188 *last_conflicts = chrec_dont_know;
2189 goto end_analyze_subs_aa;
2192 /* The classic "gcd-test". */
2193 if (!int_divides_p (gcd_alpha_beta, gamma))
2195 /* The "gcd-test" has determined that there is no integer
2196 solution, i.e. there is no dependence. */
2197 *overlaps_a = conflict_fn_no_dependence ();
2198 *overlaps_b = conflict_fn_no_dependence ();
2199 *last_conflicts = integer_zero_node;
2202 /* Both access functions are univariate. This includes SIV and MIV cases. */
2203 else if (nb_vars_a == 1 && nb_vars_b == 1)
2205 /* Both functions should have the same evolution sign. */
2206 if (((A[0][0] > 0 && -A[1][0] > 0)
2207 || (A[0][0] < 0 && -A[1][0] < 0)))
2209 /* The solutions are given by:
2211 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2214 For a given integer t. Using the following variables,
2216 | i0 = u11 * gamma / gcd_alpha_beta
2217 | j0 = u12 * gamma / gcd_alpha_beta
2224 | y0 = j0 + j1 * t. */
2225 HOST_WIDE_INT i0, j0, i1, j1;
2227 i0 = U[0][0] * gamma / gcd_alpha_beta;
2228 j0 = U[0][1] * gamma / gcd_alpha_beta;
2232 if ((i1 == 0 && i0 < 0)
2233 || (j1 == 0 && j0 < 0))
2235 /* There is no solution.
2236 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2237 falls in here, but for the moment we don't look at the
2238 upper bound of the iteration domain. */
2239 *overlaps_a = conflict_fn_no_dependence ();
2240 *overlaps_b = conflict_fn_no_dependence ();
2241 *last_conflicts = integer_zero_node;
2242 goto end_analyze_subs_aa;
2245 if (i1 > 0 && j1 > 0)
2247 HOST_WIDE_INT niter_a = estimated_loop_iterations_int
2248 (get_chrec_loop (chrec_a), false);
2249 HOST_WIDE_INT niter_b = estimated_loop_iterations_int
2250 (get_chrec_loop (chrec_b), false);
2251 HOST_WIDE_INT niter = MIN (niter_a, niter_b);
2253 /* (X0, Y0) is a solution of the Diophantine equation:
2254 "chrec_a (X0) = chrec_b (Y0)". */
2255 HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
2257 HOST_WIDE_INT x0 = i1 * tau1 + i0;
2258 HOST_WIDE_INT y0 = j1 * tau1 + j0;
2260 /* (X1, Y1) is the smallest positive solution of the eq
2261 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2262 first conflict occurs. */
2263 HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
2264 HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
2265 HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
2269 HOST_WIDE_INT tau2 = MIN (FLOOR_DIV (niter - i0, i1),
2270 FLOOR_DIV (niter - j0, j1));
2271 HOST_WIDE_INT last_conflict = tau2 - (x1 - i0)/i1;
2273 /* If the overlap occurs outside of the bounds of the
2274 loop, there is no dependence. */
2275 if (x1 >= niter || y1 >= niter)
2277 *overlaps_a = conflict_fn_no_dependence ();
2278 *overlaps_b = conflict_fn_no_dependence ();
2279 *last_conflicts = integer_zero_node;
2280 goto end_analyze_subs_aa;
2283 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2286 *last_conflicts = chrec_dont_know;
2290 affine_fn_univar (build_int_cst (NULL_TREE, x1),
2292 build_int_cst (NULL_TREE, i1)));
2295 affine_fn_univar (build_int_cst (NULL_TREE, y1),
2297 build_int_cst (NULL_TREE, j1)));
2301 /* FIXME: For the moment, the upper bound of the
2302 iteration domain for i and j is not checked. */
2303 if (dump_file && (dump_flags & TDF_DETAILS))
2304 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2305 *overlaps_a = conflict_fn_not_known ();
2306 *overlaps_b = conflict_fn_not_known ();
2307 *last_conflicts = chrec_dont_know;
2312 if (dump_file && (dump_flags & TDF_DETAILS))
2313 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2314 *overlaps_a = conflict_fn_not_known ();
2315 *overlaps_b = conflict_fn_not_known ();
2316 *last_conflicts = chrec_dont_know;
2321 if (dump_file && (dump_flags & TDF_DETAILS))
2322 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2323 *overlaps_a = conflict_fn_not_known ();
2324 *overlaps_b = conflict_fn_not_known ();
2325 *last_conflicts = chrec_dont_know;
2328 end_analyze_subs_aa:
2329 obstack_free (&scratch_obstack, NULL);
2330 if (dump_file && (dump_flags & TDF_DETAILS))
2332 fprintf (dump_file, " (overlaps_a = ");
2333 dump_conflict_function (dump_file, *overlaps_a);
2334 fprintf (dump_file, ")\n (overlaps_b = ");
2335 dump_conflict_function (dump_file, *overlaps_b);
2336 fprintf (dump_file, ")\n");
2337 fprintf (dump_file, ")\n");
2341 /* Returns true when analyze_subscript_affine_affine can be used for
2342 determining the dependence relation between chrec_a and chrec_b,
2343 that contain symbols. This function modifies chrec_a and chrec_b
2344 such that the analysis result is the same, and such that they don't
2345 contain symbols, and then can safely be passed to the analyzer.
2347 Example: The analysis of the following tuples of evolutions produce
2348 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2351 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2352 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2356 can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
2358 tree diff, type, left_a, left_b, right_b;
2360 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
2361 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
2362 /* FIXME: For the moment not handled. Might be refined later. */
2365 type = chrec_type (*chrec_a);
2366 left_a = CHREC_LEFT (*chrec_a);
2367 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
2368 diff = chrec_fold_minus (type, left_a, left_b);
2370 if (!evolution_function_is_constant_p (diff))
2373 if (dump_file && (dump_flags & TDF_DETAILS))
2374 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
2376 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
2377 diff, CHREC_RIGHT (*chrec_a));
2378 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
2379 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
2380 build_int_cst (type, 0),
2385 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2386 *OVERLAPS_B are initialized to the functions that describe the
2387 relation between the elements accessed twice by CHREC_A and
2388 CHREC_B. For k >= 0, the following property is verified:
2390 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2393 analyze_siv_subscript (tree chrec_a,
2395 conflict_function **overlaps_a,
2396 conflict_function **overlaps_b,
2397 tree *last_conflicts,
2400 dependence_stats.num_siv++;
2402 if (dump_file && (dump_flags & TDF_DETAILS))
2403 fprintf (dump_file, "(analyze_siv_subscript \n");
2405 if (evolution_function_is_constant_p (chrec_a)
2406 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2407 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
2408 overlaps_a, overlaps_b, last_conflicts);
2410 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2411 && evolution_function_is_constant_p (chrec_b))
2412 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
2413 overlaps_b, overlaps_a, last_conflicts);
2415 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2416 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2418 if (!chrec_contains_symbols (chrec_a)
2419 && !chrec_contains_symbols (chrec_b))
2421 analyze_subscript_affine_affine (chrec_a, chrec_b,
2422 overlaps_a, overlaps_b,
2425 if (CF_NOT_KNOWN_P (*overlaps_a)
2426 || CF_NOT_KNOWN_P (*overlaps_b))
2427 dependence_stats.num_siv_unimplemented++;
2428 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2429 || CF_NO_DEPENDENCE_P (*overlaps_b))
2430 dependence_stats.num_siv_independent++;
2432 dependence_stats.num_siv_dependent++;
2434 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
2437 analyze_subscript_affine_affine (chrec_a, chrec_b,
2438 overlaps_a, overlaps_b,
2441 if (CF_NOT_KNOWN_P (*overlaps_a)
2442 || CF_NOT_KNOWN_P (*overlaps_b))
2443 dependence_stats.num_siv_unimplemented++;
2444 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2445 || CF_NO_DEPENDENCE_P (*overlaps_b))
2446 dependence_stats.num_siv_independent++;
2448 dependence_stats.num_siv_dependent++;
2451 goto siv_subscript_dontknow;
2456 siv_subscript_dontknow:;
2457 if (dump_file && (dump_flags & TDF_DETAILS))
2458 fprintf (dump_file, "siv test failed: unimplemented.\n");
2459 *overlaps_a = conflict_fn_not_known ();
2460 *overlaps_b = conflict_fn_not_known ();
2461 *last_conflicts = chrec_dont_know;
2462 dependence_stats.num_siv_unimplemented++;
2465 if (dump_file && (dump_flags & TDF_DETAILS))
2466 fprintf (dump_file, ")\n");
2469 /* Returns false if we can prove that the greatest common divisor of the steps
2470 of CHREC does not divide CST, false otherwise. */
2473 gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
2475 HOST_WIDE_INT cd = 0, val;
2478 if (!host_integerp (cst, 0))
2480 val = tree_low_cst (cst, 0);
2482 while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
2484 step = CHREC_RIGHT (chrec);
2485 if (!host_integerp (step, 0))
2487 cd = gcd (cd, tree_low_cst (step, 0));
2488 chrec = CHREC_LEFT (chrec);
2491 return val % cd == 0;
2494 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
2495 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2496 functions that describe the relation between the elements accessed
2497 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2500 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2503 analyze_miv_subscript (tree chrec_a,
2505 conflict_function **overlaps_a,
2506 conflict_function **overlaps_b,
2507 tree *last_conflicts,
2508 struct loop *loop_nest)
2510 /* FIXME: This is a MIV subscript, not yet handled.
2511 Example: (A[{1, +, 1}_1] vs. A[{1, +, 1}_2]) that comes from
2514 In the SIV test we had to solve a Diophantine equation with two
2515 variables. In the MIV case we have to solve a Diophantine
2516 equation with 2*n variables (if the subscript uses n IVs).
2518 tree type, difference;
2520 dependence_stats.num_miv++;
2521 if (dump_file && (dump_flags & TDF_DETAILS))
2522 fprintf (dump_file, "(analyze_miv_subscript \n");
2524 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
2525 chrec_a = chrec_convert (type, chrec_a, NULL);
2526 chrec_b = chrec_convert (type, chrec_b, NULL);
2527 difference = chrec_fold_minus (type, chrec_a, chrec_b);
2529 if (eq_evolutions_p (chrec_a, chrec_b))
2531 /* Access functions are the same: all the elements are accessed
2532 in the same order. */
2533 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2534 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2535 *last_conflicts = estimated_loop_iterations_tree
2536 (get_chrec_loop (chrec_a), true);
2537 dependence_stats.num_miv_dependent++;
2540 else if (evolution_function_is_constant_p (difference)
2541 /* For the moment, the following is verified:
2542 evolution_function_is_affine_multivariate_p (chrec_a,
2544 && !gcd_of_steps_may_divide_p (chrec_a, difference))
2546 /* testsuite/.../ssa-chrec-33.c
2547 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2549 The difference is 1, and all the evolution steps are multiples
2550 of 2, consequently there are no overlapping elements. */
2551 *overlaps_a = conflict_fn_no_dependence ();
2552 *overlaps_b = conflict_fn_no_dependence ();
2553 *last_conflicts = integer_zero_node;
2554 dependence_stats.num_miv_independent++;
2557 else if (evolution_function_is_affine_multivariate_p (chrec_a, loop_nest->num)
2558 && !chrec_contains_symbols (chrec_a)
2559 && evolution_function_is_affine_multivariate_p (chrec_b, loop_nest->num)
2560 && !chrec_contains_symbols (chrec_b))
2562 /* testsuite/.../ssa-chrec-35.c
2563 {0, +, 1}_2 vs. {0, +, 1}_3
2564 the overlapping elements are respectively located at iterations:
2565 {0, +, 1}_x and {0, +, 1}_x,
2566 in other words, we have the equality:
2567 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2570 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2571 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2573 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2574 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2576 analyze_subscript_affine_affine (chrec_a, chrec_b,
2577 overlaps_a, overlaps_b, last_conflicts);
2579 if (CF_NOT_KNOWN_P (*overlaps_a)
2580 || CF_NOT_KNOWN_P (*overlaps_b))
2581 dependence_stats.num_miv_unimplemented++;
2582 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2583 || CF_NO_DEPENDENCE_P (*overlaps_b))
2584 dependence_stats.num_miv_independent++;
2586 dependence_stats.num_miv_dependent++;
2591 /* When the analysis is too difficult, answer "don't know". */
2592 if (dump_file && (dump_flags & TDF_DETAILS))
2593 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
2595 *overlaps_a = conflict_fn_not_known ();
2596 *overlaps_b = conflict_fn_not_known ();
2597 *last_conflicts = chrec_dont_know;
2598 dependence_stats.num_miv_unimplemented++;
2601 if (dump_file && (dump_flags & TDF_DETAILS))
2602 fprintf (dump_file, ")\n");
2605 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
2606 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
2607 OVERLAP_ITERATIONS_B are initialized with two functions that
2608 describe the iterations that contain conflicting elements.
2610 Remark: For an integer k >= 0, the following equality is true:
2612 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
2616 analyze_overlapping_iterations (tree chrec_a,
2618 conflict_function **overlap_iterations_a,
2619 conflict_function **overlap_iterations_b,
2620 tree *last_conflicts, struct loop *loop_nest)
2622 unsigned int lnn = loop_nest->num;
2624 dependence_stats.num_subscript_tests++;
2626 if (dump_file && (dump_flags & TDF_DETAILS))
2628 fprintf (dump_file, "(analyze_overlapping_iterations \n");
2629 fprintf (dump_file, " (chrec_a = ");
2630 print_generic_expr (dump_file, chrec_a, 0);
2631 fprintf (dump_file, ")\n (chrec_b = ");
2632 print_generic_expr (dump_file, chrec_b, 0);
2633 fprintf (dump_file, ")\n");
2636 if (chrec_a == NULL_TREE
2637 || chrec_b == NULL_TREE
2638 || chrec_contains_undetermined (chrec_a)
2639 || chrec_contains_undetermined (chrec_b))
2641 dependence_stats.num_subscript_undetermined++;
2643 *overlap_iterations_a = conflict_fn_not_known ();
2644 *overlap_iterations_b = conflict_fn_not_known ();
2647 /* If they are the same chrec, and are affine, they overlap
2648 on every iteration. */
2649 else if (eq_evolutions_p (chrec_a, chrec_b)
2650 && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
2651 || operand_equal_p (chrec_a, chrec_b, 0)))
2653 dependence_stats.num_same_subscript_function++;
2654 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2655 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2656 *last_conflicts = chrec_dont_know;
2659 /* If they aren't the same, and aren't affine, we can't do anything
2661 else if ((chrec_contains_symbols (chrec_a)
2662 || chrec_contains_symbols (chrec_b))
2663 && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
2664 || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
2666 dependence_stats.num_subscript_undetermined++;
2667 *overlap_iterations_a = conflict_fn_not_known ();
2668 *overlap_iterations_b = conflict_fn_not_known ();
2671 else if (ziv_subscript_p (chrec_a, chrec_b))
2672 analyze_ziv_subscript (chrec_a, chrec_b,
2673 overlap_iterations_a, overlap_iterations_b,
2676 else if (siv_subscript_p (chrec_a, chrec_b))
2677 analyze_siv_subscript (chrec_a, chrec_b,
2678 overlap_iterations_a, overlap_iterations_b,
2679 last_conflicts, lnn);
2682 analyze_miv_subscript (chrec_a, chrec_b,
2683 overlap_iterations_a, overlap_iterations_b,
2684 last_conflicts, loop_nest);
2686 if (dump_file && (dump_flags & TDF_DETAILS))
2688 fprintf (dump_file, " (overlap_iterations_a = ");
2689 dump_conflict_function (dump_file, *overlap_iterations_a);
2690 fprintf (dump_file, ")\n (overlap_iterations_b = ");
2691 dump_conflict_function (dump_file, *overlap_iterations_b);
2692 fprintf (dump_file, ")\n");
2693 fprintf (dump_file, ")\n");
2697 /* Helper function for uniquely inserting distance vectors. */
2700 save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
2705 FOR_EACH_VEC_ELT (lambda_vector, DDR_DIST_VECTS (ddr), i, v)
2706 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
2709 VEC_safe_push (lambda_vector, heap, DDR_DIST_VECTS (ddr), dist_v);
2712 /* Helper function for uniquely inserting direction vectors. */
2715 save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
2720 FOR_EACH_VEC_ELT (lambda_vector, DDR_DIR_VECTS (ddr), i, v)
2721 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
2724 VEC_safe_push (lambda_vector, heap, DDR_DIR_VECTS (ddr), dir_v);
2727 /* Add a distance of 1 on all the loops outer than INDEX. If we
2728 haven't yet determined a distance for this outer loop, push a new
2729 distance vector composed of the previous distance, and a distance
2730 of 1 for this outer loop. Example:
2738 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
2739 save (0, 1), then we have to save (1, 0). */
2742 add_outer_distances (struct data_dependence_relation *ddr,
2743 lambda_vector dist_v, int index)
2745 /* For each outer loop where init_v is not set, the accesses are
2746 in dependence of distance 1 in the loop. */
2747 while (--index >= 0)
2749 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
2750 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
2752 save_dist_v (ddr, save_v);
2756 /* Return false when fail to represent the data dependence as a
2757 distance vector. INIT_B is set to true when a component has been
2758 added to the distance vector DIST_V. INDEX_CARRY is then set to
2759 the index in DIST_V that carries the dependence. */
2762 build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
2763 struct data_reference *ddr_a,
2764 struct data_reference *ddr_b,
2765 lambda_vector dist_v, bool *init_b,
2769 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
2771 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
2773 tree access_fn_a, access_fn_b;
2774 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
2776 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
2778 non_affine_dependence_relation (ddr);
2782 access_fn_a = DR_ACCESS_FN (ddr_a, i);
2783 access_fn_b = DR_ACCESS_FN (ddr_b, i);
2785 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
2786 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
2789 int index_a = index_in_loop_nest (CHREC_VARIABLE (access_fn_a),
2790 DDR_LOOP_NEST (ddr));
2791 int index_b = index_in_loop_nest (CHREC_VARIABLE (access_fn_b),
2792 DDR_LOOP_NEST (ddr));
2794 /* The dependence is carried by the outermost loop. Example:
2801 In this case, the dependence is carried by loop_1. */
2802 index = index_a < index_b ? index_a : index_b;
2803 *index_carry = MIN (index, *index_carry);
2805 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
2807 non_affine_dependence_relation (ddr);
2811 dist = int_cst_value (SUB_DISTANCE (subscript));
2813 /* This is the subscript coupling test. If we have already
2814 recorded a distance for this loop (a distance coming from
2815 another subscript), it should be the same. For example,
2816 in the following code, there is no dependence:
2823 if (init_v[index] != 0 && dist_v[index] != dist)
2825 finalize_ddr_dependent (ddr, chrec_known);
2829 dist_v[index] = dist;
2833 else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
2835 /* This can be for example an affine vs. constant dependence
2836 (T[i] vs. T[3]) that is not an affine dependence and is
2837 not representable as a distance vector. */
2838 non_affine_dependence_relation (ddr);
2846 /* Return true when the DDR contains only constant access functions. */
2849 constant_access_functions (const struct data_dependence_relation *ddr)
2853 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
2854 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr), i))
2855 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr), i)))
2861 /* Helper function for the case where DDR_A and DDR_B are the same
2862 multivariate access function with a constant step. For an example
2866 add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
2869 tree c_1 = CHREC_LEFT (c_2);
2870 tree c_0 = CHREC_LEFT (c_1);
2871 lambda_vector dist_v;
2874 /* Polynomials with more than 2 variables are not handled yet. When
2875 the evolution steps are parameters, it is not possible to
2876 represent the dependence using classical distance vectors. */
2877 if (TREE_CODE (c_0) != INTEGER_CST
2878 || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
2879 || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
2881 DDR_AFFINE_P (ddr) = false;
2885 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
2886 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
2888 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
2889 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
2890 v1 = int_cst_value (CHREC_RIGHT (c_1));
2891 v2 = int_cst_value (CHREC_RIGHT (c_2));
2904 save_dist_v (ddr, dist_v);
2906 add_outer_distances (ddr, dist_v, x_1);
2909 /* Helper function for the case where DDR_A and DDR_B are the same
2910 access functions. */
2913 add_other_self_distances (struct data_dependence_relation *ddr)
2915 lambda_vector dist_v;
2917 int index_carry = DDR_NB_LOOPS (ddr);
2919 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
2921 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
2923 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
2925 if (!evolution_function_is_univariate_p (access_fun))
2927 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
2929 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
2933 access_fun = DR_ACCESS_FN (DDR_A (ddr), 0);
2935 if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
2936 add_multivariate_self_dist (ddr, access_fun);
2938 /* The evolution step is not constant: it varies in
2939 the outer loop, so this cannot be represented by a
2940 distance vector. For example in pr34635.c the
2941 evolution is {0, +, {0, +, 4}_1}_2. */
2942 DDR_AFFINE_P (ddr) = false;
2947 index_carry = MIN (index_carry,
2948 index_in_loop_nest (CHREC_VARIABLE (access_fun),
2949 DDR_LOOP_NEST (ddr)));
2953 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
2954 add_outer_distances (ddr, dist_v, index_carry);
2958 insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
2960 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
2962 dist_v[DDR_INNER_LOOP (ddr)] = 1;
2963 save_dist_v (ddr, dist_v);
2966 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
2967 is the case for example when access functions are the same and
2968 equal to a constant, as in:
2975 in which case the distance vectors are (0) and (1). */
2978 add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
2982 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
2984 subscript_p sub = DDR_SUBSCRIPT (ddr, i);
2985 conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
2986 conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
2988 for (j = 0; j < ca->n; j++)
2989 if (affine_function_zero_p (ca->fns[j]))
2991 insert_innermost_unit_dist_vector (ddr);
2995 for (j = 0; j < cb->n; j++)
2996 if (affine_function_zero_p (cb->fns[j]))
2998 insert_innermost_unit_dist_vector (ddr);
3004 /* Compute the classic per loop distance vector. DDR is the data
3005 dependence relation to build a vector from. Return false when fail
3006 to represent the data dependence as a distance vector. */
3009 build_classic_dist_vector (struct data_dependence_relation *ddr,
3010 struct loop *loop_nest)
3012 bool init_b = false;
3013 int index_carry = DDR_NB_LOOPS (ddr);
3014 lambda_vector dist_v;
3016 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3019 if (same_access_functions (ddr))
3021 /* Save the 0 vector. */
3022 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3023 save_dist_v (ddr, dist_v);
3025 if (constant_access_functions (ddr))
3026 add_distance_for_zero_overlaps (ddr);
3028 if (DDR_NB_LOOPS (ddr) > 1)
3029 add_other_self_distances (ddr);
3034 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3035 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3036 dist_v, &init_b, &index_carry))
3039 /* Save the distance vector if we initialized one. */
3042 /* Verify a basic constraint: classic distance vectors should
3043 always be lexicographically positive.
3045 Data references are collected in the order of execution of
3046 the program, thus for the following loop
3048 | for (i = 1; i < 100; i++)
3049 | for (j = 1; j < 100; j++)
3051 | t = T[j+1][i-1]; // A
3052 | T[j][i] = t + 2; // B
3055 references are collected following the direction of the wind:
3056 A then B. The data dependence tests are performed also
3057 following this order, such that we're looking at the distance
3058 separating the elements accessed by A from the elements later
3059 accessed by B. But in this example, the distance returned by
3060 test_dep (A, B) is lexicographically negative (-1, 1), that
3061 means that the access A occurs later than B with respect to
3062 the outer loop, ie. we're actually looking upwind. In this
3063 case we solve test_dep (B, A) looking downwind to the
3064 lexicographically positive solution, that returns the
3065 distance vector (1, -1). */
3066 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3068 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3069 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3072 compute_subscript_distance (ddr);
3073 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3074 save_v, &init_b, &index_carry))
3076 save_dist_v (ddr, save_v);
3077 DDR_REVERSED_P (ddr) = true;
3079 /* In this case there is a dependence forward for all the
3082 | for (k = 1; k < 100; k++)
3083 | for (i = 1; i < 100; i++)
3084 | for (j = 1; j < 100; j++)
3086 | t = T[j+1][i-1]; // A
3087 | T[j][i] = t + 2; // B
3095 if (DDR_NB_LOOPS (ddr) > 1)
3097 add_outer_distances (ddr, save_v, index_carry);
3098 add_outer_distances (ddr, dist_v, index_carry);
3103 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3104 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3106 if (DDR_NB_LOOPS (ddr) > 1)
3108 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3110 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr),
3111 DDR_A (ddr), loop_nest))
3113 compute_subscript_distance (ddr);
3114 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3115 opposite_v, &init_b,
3119 save_dist_v (ddr, save_v);
3120 add_outer_distances (ddr, dist_v, index_carry);
3121 add_outer_distances (ddr, opposite_v, index_carry);
3124 save_dist_v (ddr, save_v);
3129 /* There is a distance of 1 on all the outer loops: Example:
3130 there is a dependence of distance 1 on loop_1 for the array A.
3136 add_outer_distances (ddr, dist_v,
3137 lambda_vector_first_nz (dist_v,
3138 DDR_NB_LOOPS (ddr), 0));
3141 if (dump_file && (dump_flags & TDF_DETAILS))
3145 fprintf (dump_file, "(build_classic_dist_vector\n");
3146 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3148 fprintf (dump_file, " dist_vector = (");
3149 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3150 DDR_NB_LOOPS (ddr));
3151 fprintf (dump_file, " )\n");
3153 fprintf (dump_file, ")\n");
3159 /* Return the direction for a given distance.
3160 FIXME: Computing dir this way is suboptimal, since dir can catch
3161 cases that dist is unable to represent. */
3163 static inline enum data_dependence_direction
3164 dir_from_dist (int dist)
3167 return dir_positive;
3169 return dir_negative;
3174 /* Compute the classic per loop direction vector. DDR is the data
3175 dependence relation to build a vector from. */
3178 build_classic_dir_vector (struct data_dependence_relation *ddr)
3181 lambda_vector dist_v;
3183 FOR_EACH_VEC_ELT (lambda_vector, DDR_DIST_VECTS (ddr), i, dist_v)
3185 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3187 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3188 dir_v[j] = dir_from_dist (dist_v[j]);
3190 save_dir_v (ddr, dir_v);
3194 /* Helper function. Returns true when there is a dependence between
3195 data references DRA and DRB. */
3198 subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
3199 struct data_reference *dra,
3200 struct data_reference *drb,
3201 struct loop *loop_nest)
3204 tree last_conflicts;
3205 struct subscript *subscript;
3207 for (i = 0; VEC_iterate (subscript_p, DDR_SUBSCRIPTS (ddr), i, subscript);
3210 conflict_function *overlaps_a, *overlaps_b;
3212 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3213 DR_ACCESS_FN (drb, i),
3214 &overlaps_a, &overlaps_b,
3215 &last_conflicts, loop_nest);
3217 if (CF_NOT_KNOWN_P (overlaps_a)
3218 || CF_NOT_KNOWN_P (overlaps_b))
3220 finalize_ddr_dependent (ddr, chrec_dont_know);
3221 dependence_stats.num_dependence_undetermined++;
3222 free_conflict_function (overlaps_a);
3223 free_conflict_function (overlaps_b);
3227 else if (CF_NO_DEPENDENCE_P (overlaps_a)
3228 || CF_NO_DEPENDENCE_P (overlaps_b))
3230 finalize_ddr_dependent (ddr, chrec_known);
3231 dependence_stats.num_dependence_independent++;
3232 free_conflict_function (overlaps_a);
3233 free_conflict_function (overlaps_b);
3239 if (SUB_CONFLICTS_IN_A (subscript))
3240 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
3241 if (SUB_CONFLICTS_IN_B (subscript))
3242 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
3244 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3245 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3246 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3253 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
3256 subscript_dependence_tester (struct data_dependence_relation *ddr,
3257 struct loop *loop_nest)
3260 if (dump_file && (dump_flags & TDF_DETAILS))
3261 fprintf (dump_file, "(subscript_dependence_tester \n");
3263 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr), loop_nest))
3264 dependence_stats.num_dependence_dependent++;
3266 compute_subscript_distance (ddr);
3267 if (build_classic_dist_vector (ddr, loop_nest))
3268 build_classic_dir_vector (ddr);
3270 if (dump_file && (dump_flags & TDF_DETAILS))
3271 fprintf (dump_file, ")\n");
3274 /* Returns true when all the access functions of A are affine or
3275 constant with respect to LOOP_NEST. */
3278 access_functions_are_affine_or_constant_p (const struct data_reference *a,
3279 const struct loop *loop_nest)
3282 VEC(tree,heap) *fns = DR_ACCESS_FNS (a);
3285 FOR_EACH_VEC_ELT (tree, fns, i, t)
3286 if (!evolution_function_is_invariant_p (t, loop_nest->num)
3287 && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
3293 /* Initializes an equation for an OMEGA problem using the information
3294 contained in the ACCESS_FUN. Returns true when the operation
3297 PB is the omega constraint system.
3298 EQ is the number of the equation to be initialized.
3299 OFFSET is used for shifting the variables names in the constraints:
3300 a constrain is composed of 2 * the number of variables surrounding
3301 dependence accesses. OFFSET is set either to 0 for the first n variables,
3302 then it is set to n.
3303 ACCESS_FUN is expected to be an affine chrec. */
3306 init_omega_eq_with_af (omega_pb pb, unsigned eq,
3307 unsigned int offset, tree access_fun,
3308 struct data_dependence_relation *ddr)
3310 switch (TREE_CODE (access_fun))
3312 case POLYNOMIAL_CHREC:
3314 tree left = CHREC_LEFT (access_fun);
3315 tree right = CHREC_RIGHT (access_fun);
3316 int var = CHREC_VARIABLE (access_fun);
3319 if (TREE_CODE (right) != INTEGER_CST)
3322 var_idx = index_in_loop_nest (var, DDR_LOOP_NEST (ddr));
3323 pb->eqs[eq].coef[offset + var_idx + 1] = int_cst_value (right);
3325 /* Compute the innermost loop index. */
3326 DDR_INNER_LOOP (ddr) = MAX (DDR_INNER_LOOP (ddr), var_idx);
3329 pb->eqs[eq].coef[var_idx + DDR_NB_LOOPS (ddr) + 1]
3330 += int_cst_value (right);
3332 switch (TREE_CODE (left))
3334 case POLYNOMIAL_CHREC:
3335 return init_omega_eq_with_af (pb, eq, offset, left, ddr);
3338 pb->eqs[eq].coef[0] += int_cst_value (left);
3347 pb->eqs[eq].coef[0] += int_cst_value (access_fun);
3355 /* As explained in the comments preceding init_omega_for_ddr, we have
3356 to set up a system for each loop level, setting outer loops
3357 variation to zero, and current loop variation to positive or zero.
3358 Save each lexico positive distance vector. */
3361 omega_extract_distance_vectors (omega_pb pb,
3362 struct data_dependence_relation *ddr)
3366 struct loop *loopi, *loopj;
3367 enum omega_result res;
3369 /* Set a new problem for each loop in the nest. The basis is the
3370 problem that we have initialized until now. On top of this we
3371 add new constraints. */
3372 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3373 && VEC_iterate (loop_p, DDR_LOOP_NEST (ddr), i, loopi); i++)
3376 omega_pb copy = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr),
3377 DDR_NB_LOOPS (ddr));
3379 omega_copy_problem (copy, pb);
3381 /* For all the outer loops "loop_j", add "dj = 0". */
3383 j < i && VEC_iterate (loop_p, DDR_LOOP_NEST (ddr), j, loopj); j++)
3385 eq = omega_add_zero_eq (copy, omega_black);
3386 copy->eqs[eq].coef[j + 1] = 1;
3389 /* For "loop_i", add "0 <= di". */
3390 geq = omega_add_zero_geq (copy, omega_black);
3391 copy->geqs[geq].coef[i + 1] = 1;
3393 /* Reduce the constraint system, and test that the current
3394 problem is feasible. */
3395 res = omega_simplify_problem (copy);
3396 if (res == omega_false
3397 || res == omega_unknown
3398 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3401 for (eq = 0; eq < copy->num_subs; eq++)
3402 if (copy->subs[eq].key == (int) i + 1)
3404 dist = copy->subs[eq].coef[0];
3410 /* Reinitialize problem... */
3411 omega_copy_problem (copy, pb);
3413 j < i && VEC_iterate (loop_p, DDR_LOOP_NEST (ddr), j, loopj); j++)
3415 eq = omega_add_zero_eq (copy, omega_black);
3416 copy->eqs[eq].coef[j + 1] = 1;
3419 /* ..., but this time "di = 1". */
3420 eq = omega_add_zero_eq (copy, omega_black);
3421 copy->eqs[eq].coef[i + 1] = 1;
3422 copy->eqs[eq].coef[0] = -1;
3424 res = omega_simplify_problem (copy);
3425 if (res == omega_false
3426 || res == omega_unknown
3427 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3430 for (eq = 0; eq < copy->num_subs; eq++)
3431 if (copy->subs[eq].key == (int) i + 1)
3433 dist = copy->subs[eq].coef[0];
3439 /* Save the lexicographically positive distance vector. */
3442 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3443 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3447 for (eq = 0; eq < copy->num_subs; eq++)
3448 if (copy->subs[eq].key > 0)
3450 dist = copy->subs[eq].coef[0];
3451 dist_v[copy->subs[eq].key - 1] = dist;
3454 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3455 dir_v[j] = dir_from_dist (dist_v[j]);
3457 save_dist_v (ddr, dist_v);
3458 save_dir_v (ddr, dir_v);
3462 omega_free_problem (copy);
3466 /* This is called for each subscript of a tuple of data references:
3467 insert an equality for representing the conflicts. */
3470 omega_setup_subscript (tree access_fun_a, tree access_fun_b,
3471 struct data_dependence_relation *ddr,
3472 omega_pb pb, bool *maybe_dependent)
3475 tree type = signed_type_for_types (TREE_TYPE (access_fun_a),
3476 TREE_TYPE (access_fun_b));
3477 tree fun_a = chrec_convert (type, access_fun_a, NULL);
3478 tree fun_b = chrec_convert (type, access_fun_b, NULL);
3479 tree difference = chrec_fold_minus (type, fun_a, fun_b);
3482 /* When the fun_a - fun_b is not constant, the dependence is not
3483 captured by the classic distance vector representation. */
3484 if (TREE_CODE (difference) != INTEGER_CST)
3488 if (ziv_subscript_p (fun_a, fun_b) && !integer_zerop (difference))
3490 /* There is no dependence. */
3491 *maybe_dependent = false;
3495 minus_one = build_int_cst (type, -1);
3496 fun_b = chrec_fold_multiply (type, fun_b, minus_one);
3498 eq = omega_add_zero_eq (pb, omega_black);
3499 if (!init_omega_eq_with_af (pb, eq, DDR_NB_LOOPS (ddr), fun_a, ddr)
3500 || !init_omega_eq_with_af (pb, eq, 0, fun_b, ddr))
3501 /* There is probably a dependence, but the system of
3502 constraints cannot be built: answer "don't know". */
3506 if (DDR_NB_LOOPS (ddr) != 0 && pb->eqs[eq].coef[0]
3507 && !int_divides_p (lambda_vector_gcd
3508 ((lambda_vector) &(pb->eqs[eq].coef[1]),
3509 2 * DDR_NB_LOOPS (ddr)),
3510 pb->eqs[eq].coef[0]))
3512 /* There is no dependence. */
3513 *maybe_dependent = false;
3520 /* Helper function, same as init_omega_for_ddr but specialized for
3521 data references A and B. */
3524 init_omega_for_ddr_1 (struct data_reference *dra, struct data_reference *drb,
3525 struct data_dependence_relation *ddr,
3526 omega_pb pb, bool *maybe_dependent)
3531 unsigned nb_loops = DDR_NB_LOOPS (ddr);
3533 /* Insert an equality per subscript. */
3534 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3536 if (!omega_setup_subscript (DR_ACCESS_FN (dra, i), DR_ACCESS_FN (drb, i),
3537 ddr, pb, maybe_dependent))
3539 else if (*maybe_dependent == false)
3541 /* There is no dependence. */
3542 DDR_ARE_DEPENDENT (ddr) = chrec_known;
3547 /* Insert inequalities: constraints corresponding to the iteration
3548 domain, i.e. the loops surrounding the references "loop_x" and
3549 the distance variables "dx". The layout of the OMEGA
3550 representation is as follows:
3551 - coef[0] is the constant
3552 - coef[1..nb_loops] are the protected variables that will not be
3553 removed by the solver: the "dx"
3554 - coef[nb_loops + 1, 2*nb_loops] are the loop variables: "loop_x".
3556 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3557 && VEC_iterate (loop_p, DDR_LOOP_NEST (ddr), i, loopi); i++)
3559 HOST_WIDE_INT nbi = estimated_loop_iterations_int (loopi, false);
3562 ineq = omega_add_zero_geq (pb, omega_black);
3563 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3565 /* 0 <= loop_x + dx */
3566 ineq = omega_add_zero_geq (pb, omega_black);
3567 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3568 pb->geqs[ineq].coef[i + 1] = 1;
3572 /* loop_x <= nb_iters */
3573 ineq = omega_add_zero_geq (pb, omega_black);
3574 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3575 pb->geqs[ineq].coef[0] = nbi;
3577 /* loop_x + dx <= nb_iters */
3578 ineq = omega_add_zero_geq (pb, omega_black);
3579 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3580 pb->geqs[ineq].coef[i + 1] = -1;
3581 pb->geqs[ineq].coef[0] = nbi;
3583 /* A step "dx" bigger than nb_iters is not feasible, so
3584 add "0 <= nb_iters + dx", */
3585 ineq = omega_add_zero_geq (pb, omega_black);
3586 pb->geqs[ineq].coef[i + 1] = 1;
3587 pb->geqs[ineq].coef[0] = nbi;
3588 /* and "dx <= nb_iters". */
3589 ineq = omega_add_zero_geq (pb, omega_black);
3590 pb->geqs[ineq].coef[i + 1] = -1;
3591 pb->geqs[ineq].coef[0] = nbi;
3595 omega_extract_distance_vectors (pb, ddr);
3600 /* Sets up the Omega dependence problem for the data dependence
3601 relation DDR. Returns false when the constraint system cannot be
3602 built, ie. when the test answers "don't know". Returns true
3603 otherwise, and when independence has been proved (using one of the
3604 trivial dependence test), set MAYBE_DEPENDENT to false, otherwise
3605 set MAYBE_DEPENDENT to true.
3607 Example: for setting up the dependence system corresponding to the
3608 conflicting accesses
3613 | ... A[2*j, 2*(i + j)]
3617 the following constraints come from the iteration domain:
3624 where di, dj are the distance variables. The constraints
3625 representing the conflicting elements are:
3628 i + 1 = 2 * (i + di + j + dj)
3630 For asking that the resulting distance vector (di, dj) be
3631 lexicographically positive, we insert the constraint "di >= 0". If
3632 "di = 0" in the solution, we fix that component to zero, and we
3633 look at the inner loops: we set a new problem where all the outer
3634 loop distances are zero, and fix this inner component to be
3635 positive. When one of the components is positive, we save that
3636 distance, and set a new problem where the distance on this loop is
3637 zero, searching for other distances in the inner loops. Here is
3638 the classic example that illustrates that we have to set for each
3639 inner loop a new problem:
3647 we have to save two distances (1, 0) and (0, 1).
3649 Given two array references, refA and refB, we have to set the
3650 dependence problem twice, refA vs. refB and refB vs. refA, and we
3651 cannot do a single test, as refB might occur before refA in the
3652 inner loops, and the contrary when considering outer loops: ex.
3657 | T[{1,+,1}_2][{1,+,1}_1] // refA
3658 | T[{2,+,1}_2][{0,+,1}_1] // refB
3663 refB touches the elements in T before refA, and thus for the same
3664 loop_0 refB precedes refA: ie. the distance vector (0, 1, -1)
3665 but for successive loop_0 iterations, we have (1, -1, 1)
3667 The Omega solver expects the distance variables ("di" in the
3668 previous example) to come first in the constraint system (as
3669 variables to be protected, or "safe" variables), the constraint
3670 system is built using the following layout:
3672 "cst | distance vars | index vars".
3676 init_omega_for_ddr (struct data_dependence_relation *ddr,
3677 bool *maybe_dependent)
3682 *maybe_dependent = true;
3684 if (same_access_functions (ddr))
3687 lambda_vector dir_v;
3689 /* Save the 0 vector. */
3690 save_dist_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
3691 dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3692 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3693 dir_v[j] = dir_equal;
3694 save_dir_v (ddr, dir_v);
3696 /* Save the dependences carried by outer loops. */
3697 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
3698 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
3700 omega_free_problem (pb);
3704 /* Omega expects the protected variables (those that have to be kept
3705 after elimination) to appear first in the constraint system.
3706 These variables are the distance variables. In the following
3707 initialization we declare NB_LOOPS safe variables, and the total
3708 number of variables for the constraint system is 2*NB_LOOPS. */
3709 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
3710 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
3712 omega_free_problem (pb);
3714 /* Stop computation if not decidable, or no dependence. */
3715 if (res == false || *maybe_dependent == false)
3718 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
3719 res = init_omega_for_ddr_1 (DDR_B (ddr), DDR_A (ddr), ddr, pb,
3721 omega_free_problem (pb);
3726 /* Return true when DDR contains the same information as that stored
3727 in DIR_VECTS and in DIST_VECTS, return false otherwise. */
3730 ddr_consistent_p (FILE *file,
3731 struct data_dependence_relation *ddr,
3732 VEC (lambda_vector, heap) *dist_vects,
3733 VEC (lambda_vector, heap) *dir_vects)
3737 /* If dump_file is set, output there. */
3738 if (dump_file && (dump_flags & TDF_DETAILS))
3741 if (VEC_length (lambda_vector, dist_vects) != DDR_NUM_DIST_VECTS (ddr))
3743 lambda_vector b_dist_v;
3744 fprintf (file, "\n(Number of distance vectors differ: Banerjee has %d, Omega has %d.\n",
3745 VEC_length (lambda_vector, dist_vects),
3746 DDR_NUM_DIST_VECTS (ddr));
3748 fprintf (file, "Banerjee dist vectors:\n");
3749 FOR_EACH_VEC_ELT (lambda_vector, dist_vects, i, b_dist_v)
3750 print_lambda_vector (file, b_dist_v, DDR_NB_LOOPS (ddr));
3752 fprintf (file, "Omega dist vectors:\n");
3753 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3754 print_lambda_vector (file, DDR_DIST_VECT (ddr, i), DDR_NB_LOOPS (ddr));
3756 fprintf (file, "data dependence relation:\n");
3757 dump_data_dependence_relation (file, ddr);
3759 fprintf (file, ")\n");
3763 if (VEC_length (lambda_vector, dir_vects) != DDR_NUM_DIR_VECTS (ddr))
3765 fprintf (file, "\n(Number of direction vectors differ: Banerjee has %d, Omega has %d.)\n",
3766 VEC_length (lambda_vector, dir_vects),
3767 DDR_NUM_DIR_VECTS (ddr));
3771 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3773 lambda_vector a_dist_v;
3774 lambda_vector b_dist_v = DDR_DIST_VECT (ddr, i);
3776 /* Distance vectors are not ordered in the same way in the DDR
3777 and in the DIST_VECTS: search for a matching vector. */
3778 FOR_EACH_VEC_ELT (lambda_vector, dist_vects, j, a_dist_v)
3779 if (lambda_vector_equal (a_dist_v, b_dist_v, DDR_NB_LOOPS (ddr)))
3782 if (j == VEC_length (lambda_vector, dist_vects))
3784 fprintf (file, "\n(Dist vectors from the first dependence analyzer:\n");
3785 print_dist_vectors (file, dist_vects, DDR_NB_LOOPS (ddr));
3786 fprintf (file, "not found in Omega dist vectors:\n");
3787 print_dist_vectors (file, DDR_DIST_VECTS (ddr), DDR_NB_LOOPS (ddr));
3788 fprintf (file, "data dependence relation:\n");
3789 dump_data_dependence_relation (file, ddr);
3790 fprintf (file, ")\n");
3794 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
3796 lambda_vector a_dir_v;
3797 lambda_vector b_dir_v = DDR_DIR_VECT (ddr, i);
3799 /* Direction vectors are not ordered in the same way in the DDR
3800 and in the DIR_VECTS: search for a matching vector. */
3801 FOR_EACH_VEC_ELT (lambda_vector, dir_vects, j, a_dir_v)
3802 if (lambda_vector_equal (a_dir_v, b_dir_v, DDR_NB_LOOPS (ddr)))
3805 if (j == VEC_length (lambda_vector, dist_vects))
3807 fprintf (file, "\n(Dir vectors from the first dependence analyzer:\n");
3808 print_dir_vectors (file, dir_vects, DDR_NB_LOOPS (ddr));
3809 fprintf (file, "not found in Omega dir vectors:\n");
3810 print_dir_vectors (file, DDR_DIR_VECTS (ddr), DDR_NB_LOOPS (ddr));
3811 fprintf (file, "data dependence relation:\n");
3812 dump_data_dependence_relation (file, ddr);
3813 fprintf (file, ")\n");
3820 /* This computes the affine dependence relation between A and B with
3821 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
3822 independence between two accesses, while CHREC_DONT_KNOW is used
3823 for representing the unknown relation.
3825 Note that it is possible to stop the computation of the dependence
3826 relation the first time we detect a CHREC_KNOWN element for a given
3830 compute_affine_dependence (struct data_dependence_relation *ddr,
3831 struct loop *loop_nest)
3833 struct data_reference *dra = DDR_A (ddr);
3834 struct data_reference *drb = DDR_B (ddr);
3836 if (dump_file && (dump_flags & TDF_DETAILS))
3838 fprintf (dump_file, "(compute_affine_dependence\n");
3839 fprintf (dump_file, " (stmt_a = \n");
3840 print_gimple_stmt (dump_file, DR_STMT (dra), 0, 0);
3841 fprintf (dump_file, ")\n (stmt_b = \n");
3842 print_gimple_stmt (dump_file, DR_STMT (drb), 0, 0);
3843 fprintf (dump_file, ")\n");
3846 /* Analyze only when the dependence relation is not yet known. */
3847 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE
3848 && !DDR_SELF_REFERENCE (ddr))
3850 dependence_stats.num_dependence_tests++;
3852 if (access_functions_are_affine_or_constant_p (dra, loop_nest)
3853 && access_functions_are_affine_or_constant_p (drb, loop_nest))
3855 if (flag_check_data_deps)
3857 /* Compute the dependences using the first algorithm. */
3858 subscript_dependence_tester (ddr, loop_nest);
3860 if (dump_file && (dump_flags & TDF_DETAILS))
3862 fprintf (dump_file, "\n\nBanerjee Analyzer\n");
3863 dump_data_dependence_relation (dump_file, ddr);
3866 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
3868 bool maybe_dependent;
3869 VEC (lambda_vector, heap) *dir_vects, *dist_vects;
3871 /* Save the result of the first DD analyzer. */
3872 dist_vects = DDR_DIST_VECTS (ddr);
3873 dir_vects = DDR_DIR_VECTS (ddr);
3875 /* Reset the information. */
3876 DDR_DIST_VECTS (ddr) = NULL;
3877 DDR_DIR_VECTS (ddr) = NULL;
3879 /* Compute the same information using Omega. */
3880 if (!init_omega_for_ddr (ddr, &maybe_dependent))
3881 goto csys_dont_know;
3883 if (dump_file && (dump_flags & TDF_DETAILS))
3885 fprintf (dump_file, "Omega Analyzer\n");
3886 dump_data_dependence_relation (dump_file, ddr);
3889 /* Check that we get the same information. */
3890 if (maybe_dependent)
3891 gcc_assert (ddr_consistent_p (stderr, ddr, dist_vects,
3896 subscript_dependence_tester (ddr, loop_nest);
3899 /* As a last case, if the dependence cannot be determined, or if
3900 the dependence is considered too difficult to determine, answer
3905 dependence_stats.num_dependence_undetermined++;
3907 if (dump_file && (dump_flags & TDF_DETAILS))
3909 fprintf (dump_file, "Data ref a:\n");
3910 dump_data_reference (dump_file, dra);
3911 fprintf (dump_file, "Data ref b:\n");
3912 dump_data_reference (dump_file, drb);
3913 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
3915 finalize_ddr_dependent (ddr, chrec_dont_know);
3919 if (dump_file && (dump_flags & TDF_DETAILS))
3920 fprintf (dump_file, ")\n");
3923 /* This computes the dependence relation for the same data
3924 reference into DDR. */
3927 compute_self_dependence (struct data_dependence_relation *ddr)
3930 struct subscript *subscript;
3932 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3935 for (i = 0; VEC_iterate (subscript_p, DDR_SUBSCRIPTS (ddr), i, subscript);
3938 if (SUB_CONFLICTS_IN_A (subscript))
3939 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
3940 if (SUB_CONFLICTS_IN_B (subscript))
3941 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
3943 /* The accessed index overlaps for each iteration. */
3944 SUB_CONFLICTS_IN_A (subscript)
3945 = conflict_fn (1, affine_fn_cst (integer_zero_node));
3946 SUB_CONFLICTS_IN_B (subscript)
3947 = conflict_fn (1, affine_fn_cst (integer_zero_node));
3948 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
3951 /* The distance vector is the zero vector. */
3952 save_dist_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
3953 save_dir_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
3956 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
3957 the data references in DATAREFS, in the LOOP_NEST. When
3958 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
3962 compute_all_dependences (VEC (data_reference_p, heap) *datarefs,
3963 VEC (ddr_p, heap) **dependence_relations,
3964 VEC (loop_p, heap) *loop_nest,
3965 bool compute_self_and_rr)
3967 struct data_dependence_relation *ddr;
3968 struct data_reference *a, *b;
3971 FOR_EACH_VEC_ELT (data_reference_p, datarefs, i, a)
3972 for (j = i + 1; VEC_iterate (data_reference_p, datarefs, j, b); j++)
3973 if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
3975 ddr = initialize_data_dependence_relation (a, b, loop_nest);
3976 VEC_safe_push (ddr_p, heap, *dependence_relations, ddr);
3978 compute_affine_dependence (ddr, VEC_index (loop_p, loop_nest, 0));
3981 if (compute_self_and_rr)
3982 FOR_EACH_VEC_ELT (data_reference_p, datarefs, i, a)
3984 ddr = initialize_data_dependence_relation (a, a, loop_nest);
3985 VEC_safe_push (ddr_p, heap, *dependence_relations, ddr);
3986 compute_self_dependence (ddr);
3990 /* Stores the locations of memory references in STMT to REFERENCES. Returns
3991 true if STMT clobbers memory, false otherwise. */
3994 get_references_in_stmt (gimple stmt, VEC (data_ref_loc, heap) **references)
3996 bool clobbers_memory = false;
3999 enum gimple_code stmt_code = gimple_code (stmt);
4003 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4004 Calls have side-effects, except those to const or pure
4006 if ((stmt_code == GIMPLE_CALL
4007 && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
4008 || (stmt_code == GIMPLE_ASM
4009 && gimple_asm_volatile_p (stmt)))
4010 clobbers_memory = true;
4012 if (!gimple_vuse (stmt))
4013 return clobbers_memory;
4015 if (stmt_code == GIMPLE_ASSIGN)
4018 op0 = gimple_assign_lhs_ptr (stmt);
4019 op1 = gimple_assign_rhs1_ptr (stmt);
4022 || (REFERENCE_CLASS_P (*op1)
4023 && (base = get_base_address (*op1))
4024 && TREE_CODE (base) != SSA_NAME))
4026 ref = VEC_safe_push (data_ref_loc, heap, *references, NULL);
4028 ref->is_read = true;
4032 || (REFERENCE_CLASS_P (*op0) && get_base_address (*op0)))
4034 ref = VEC_safe_push (data_ref_loc, heap, *references, NULL);
4036 ref->is_read = false;
4039 else if (stmt_code == GIMPLE_CALL)
4041 unsigned i, n = gimple_call_num_args (stmt);
4043 for (i = 0; i < n; i++)
4045 op0 = gimple_call_arg_ptr (stmt, i);
4048 || (REFERENCE_CLASS_P (*op0) && get_base_address (*op0)))
4050 ref = VEC_safe_push (data_ref_loc, heap, *references, NULL);
4052 ref->is_read = true;
4057 return clobbers_memory;
4060 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
4061 reference, returns false, otherwise returns true. NEST is the outermost
4062 loop of the loop nest in which the references should be analyzed. */
4065 find_data_references_in_stmt (struct loop *nest, gimple stmt,
4066 VEC (data_reference_p, heap) **datarefs)
4069 VEC (data_ref_loc, heap) *references;
4072 data_reference_p dr;
4074 if (get_references_in_stmt (stmt, &references))
4076 VEC_free (data_ref_loc, heap, references);
4080 FOR_EACH_VEC_ELT (data_ref_loc, references, i, ref)
4082 dr = create_data_ref (nest, loop_containing_stmt (stmt),
4083 *ref->pos, stmt, ref->is_read);
4084 gcc_assert (dr != NULL);
4086 /* FIXME -- data dependence analysis does not work correctly for objects
4087 with invariant addresses in loop nests. Let us fail here until the
4088 problem is fixed. */
4089 if (dr_address_invariant_p (dr) && nest)
4092 if (dump_file && (dump_flags & TDF_DETAILS))
4093 fprintf (dump_file, "\tFAILED as dr address is invariant\n");
4098 VEC_safe_push (data_reference_p, heap, *datarefs, dr);
4100 VEC_free (data_ref_loc, heap, references);
4104 /* Stores the data references in STMT to DATAREFS. If there is an
4105 unanalyzable reference, returns false, otherwise returns true.
4106 NEST is the outermost loop of the loop nest in which the references
4107 should be instantiated, LOOP is the loop in which the references
4108 should be analyzed. */
4111 graphite_find_data_references_in_stmt (loop_p nest, loop_p loop, gimple stmt,
4112 VEC (data_reference_p, heap) **datarefs)
4115 VEC (data_ref_loc, heap) *references;
4118 data_reference_p dr;
4120 if (get_references_in_stmt (stmt, &references))
4122 VEC_free (data_ref_loc, heap, references);
4126 FOR_EACH_VEC_ELT (data_ref_loc, references, i, ref)
4128 dr = create_data_ref (nest, loop, *ref->pos, stmt, ref->is_read);
4129 gcc_assert (dr != NULL);
4130 VEC_safe_push (data_reference_p, heap, *datarefs, dr);
4133 VEC_free (data_ref_loc, heap, references);
4137 /* Search the data references in LOOP, and record the information into
4138 DATAREFS. Returns chrec_dont_know when failing to analyze a
4139 difficult case, returns NULL_TREE otherwise. */
4142 find_data_references_in_bb (struct loop *loop, basic_block bb,
4143 VEC (data_reference_p, heap) **datarefs)
4145 gimple_stmt_iterator bsi;
4147 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4149 gimple stmt = gsi_stmt (bsi);
4151 if (!find_data_references_in_stmt (loop, stmt, datarefs))
4153 struct data_reference *res;
4154 res = XCNEW (struct data_reference);
4155 VEC_safe_push (data_reference_p, heap, *datarefs, res);
4157 return chrec_dont_know;
4164 /* Search the data references in LOOP, and record the information into
4165 DATAREFS. Returns chrec_dont_know when failing to analyze a
4166 difficult case, returns NULL_TREE otherwise.
4168 TODO: This function should be made smarter so that it can handle address
4169 arithmetic as if they were array accesses, etc. */
4172 find_data_references_in_loop (struct loop *loop,
4173 VEC (data_reference_p, heap) **datarefs)
4175 basic_block bb, *bbs;
4178 bbs = get_loop_body_in_dom_order (loop);
4180 for (i = 0; i < loop->num_nodes; i++)
4184 if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
4187 return chrec_dont_know;
4195 /* Recursive helper function. */
4198 find_loop_nest_1 (struct loop *loop, VEC (loop_p, heap) **loop_nest)
4200 /* Inner loops of the nest should not contain siblings. Example:
4201 when there are two consecutive loops,
4212 the dependence relation cannot be captured by the distance
4217 VEC_safe_push (loop_p, heap, *loop_nest, loop);
4219 return find_loop_nest_1 (loop->inner, loop_nest);
4223 /* Return false when the LOOP is not well nested. Otherwise return
4224 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4225 contain the loops from the outermost to the innermost, as they will
4226 appear in the classic distance vector. */
4229 find_loop_nest (struct loop *loop, VEC (loop_p, heap) **loop_nest)
4231 VEC_safe_push (loop_p, heap, *loop_nest, loop);
4233 return find_loop_nest_1 (loop->inner, loop_nest);
4237 /* Returns true when the data dependences have been computed, false otherwise.
4238 Given a loop nest LOOP, the following vectors are returned:
4239 DATAREFS is initialized to all the array elements contained in this loop,
4240 DEPENDENCE_RELATIONS contains the relations between the data references.
4241 Compute read-read and self relations if
4242 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4245 compute_data_dependences_for_loop (struct loop *loop,
4246 bool compute_self_and_read_read_dependences,
4247 VEC (loop_p, heap) **loop_nest,
4248 VEC (data_reference_p, heap) **datarefs,
4249 VEC (ddr_p, heap) **dependence_relations)
4253 memset (&dependence_stats, 0, sizeof (dependence_stats));
4255 /* If the loop nest is not well formed, or one of the data references
4256 is not computable, give up without spending time to compute other
4259 || !find_loop_nest (loop, loop_nest)
4260 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know)
4262 struct data_dependence_relation *ddr;
4264 /* Insert a single relation into dependence_relations:
4266 ddr = initialize_data_dependence_relation (NULL, NULL, *loop_nest);
4267 VEC_safe_push (ddr_p, heap, *dependence_relations, ddr);
4271 compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
4272 compute_self_and_read_read_dependences);
4274 if (dump_file && (dump_flags & TDF_STATS))
4276 fprintf (dump_file, "Dependence tester statistics:\n");
4278 fprintf (dump_file, "Number of dependence tests: %d\n",
4279 dependence_stats.num_dependence_tests);
4280 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
4281 dependence_stats.num_dependence_dependent);
4282 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
4283 dependence_stats.num_dependence_independent);
4284 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
4285 dependence_stats.num_dependence_undetermined);
4287 fprintf (dump_file, "Number of subscript tests: %d\n",
4288 dependence_stats.num_subscript_tests);
4289 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
4290 dependence_stats.num_subscript_undetermined);
4291 fprintf (dump_file, "Number of same subscript function: %d\n",
4292 dependence_stats.num_same_subscript_function);
4294 fprintf (dump_file, "Number of ziv tests: %d\n",
4295 dependence_stats.num_ziv);
4296 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4297 dependence_stats.num_ziv_dependent);
4298 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4299 dependence_stats.num_ziv_independent);
4300 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
4301 dependence_stats.num_ziv_unimplemented);
4303 fprintf (dump_file, "Number of siv tests: %d\n",
4304 dependence_stats.num_siv);
4305 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4306 dependence_stats.num_siv_dependent);
4307 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4308 dependence_stats.num_siv_independent);
4309 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4310 dependence_stats.num_siv_unimplemented);
4312 fprintf (dump_file, "Number of miv tests: %d\n",
4313 dependence_stats.num_miv);
4314 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4315 dependence_stats.num_miv_dependent);
4316 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4317 dependence_stats.num_miv_independent);
4318 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4319 dependence_stats.num_miv_unimplemented);
4325 /* Returns true when the data dependences for the basic block BB have been
4326 computed, false otherwise.
4327 DATAREFS is initialized to all the array elements contained in this basic
4328 block, DEPENDENCE_RELATIONS contains the relations between the data
4329 references. Compute read-read and self relations if
4330 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4332 compute_data_dependences_for_bb (basic_block bb,
4333 bool compute_self_and_read_read_dependences,
4334 VEC (data_reference_p, heap) **datarefs,
4335 VEC (ddr_p, heap) **dependence_relations)
4337 if (find_data_references_in_bb (NULL, bb, datarefs) == chrec_dont_know)
4340 compute_all_dependences (*datarefs, dependence_relations, NULL,
4341 compute_self_and_read_read_dependences);
4345 /* Entry point (for testing only). Analyze all the data references
4346 and the dependence relations in LOOP.
4348 The data references are computed first.
4350 A relation on these nodes is represented by a complete graph. Some
4351 of the relations could be of no interest, thus the relations can be
4354 In the following function we compute all the relations. This is
4355 just a first implementation that is here for:
4356 - for showing how to ask for the dependence relations,
4357 - for the debugging the whole dependence graph,
4358 - for the dejagnu testcases and maintenance.
4360 It is possible to ask only for a part of the graph, avoiding to
4361 compute the whole dependence graph. The computed dependences are
4362 stored in a knowledge base (KB) such that later queries don't
4363 recompute the same information. The implementation of this KB is
4364 transparent to the optimizer, and thus the KB can be changed with a
4365 more efficient implementation, or the KB could be disabled. */
4367 analyze_all_data_dependences (struct loop *loop)
4370 int nb_data_refs = 10;
4371 VEC (data_reference_p, heap) *datarefs =
4372 VEC_alloc (data_reference_p, heap, nb_data_refs);
4373 VEC (ddr_p, heap) *dependence_relations =
4374 VEC_alloc (ddr_p, heap, nb_data_refs * nb_data_refs);
4375 VEC (loop_p, heap) *loop_nest = VEC_alloc (loop_p, heap, 3);
4377 /* Compute DDs on the whole function. */
4378 compute_data_dependences_for_loop (loop, false, &loop_nest, &datarefs,
4379 &dependence_relations);
4383 dump_data_dependence_relations (dump_file, dependence_relations);
4384 fprintf (dump_file, "\n\n");
4386 if (dump_flags & TDF_DETAILS)
4387 dump_dist_dir_vectors (dump_file, dependence_relations);
4389 if (dump_flags & TDF_STATS)
4391 unsigned nb_top_relations = 0;
4392 unsigned nb_bot_relations = 0;
4393 unsigned nb_chrec_relations = 0;
4394 struct data_dependence_relation *ddr;
4396 FOR_EACH_VEC_ELT (ddr_p, dependence_relations, i, ddr)
4398 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
4401 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4405 nb_chrec_relations++;
4408 gather_stats_on_scev_database ();
4412 VEC_free (loop_p, heap, loop_nest);
4413 free_dependence_relations (dependence_relations);
4414 free_data_refs (datarefs);
4417 /* Computes all the data dependences and check that the results of
4418 several analyzers are the same. */
4421 tree_check_data_deps (void)
4424 struct loop *loop_nest;
4426 FOR_EACH_LOOP (li, loop_nest, 0)
4427 analyze_all_data_dependences (loop_nest);
4430 /* Free the memory used by a data dependence relation DDR. */
4433 free_dependence_relation (struct data_dependence_relation *ddr)
4438 if (DDR_SUBSCRIPTS (ddr))
4439 free_subscripts (DDR_SUBSCRIPTS (ddr));
4440 if (DDR_DIST_VECTS (ddr))
4441 VEC_free (lambda_vector, heap, DDR_DIST_VECTS (ddr));
4442 if (DDR_DIR_VECTS (ddr))
4443 VEC_free (lambda_vector, heap, DDR_DIR_VECTS (ddr));
4448 /* Free the memory used by the data dependence relations from
4449 DEPENDENCE_RELATIONS. */
4452 free_dependence_relations (VEC (ddr_p, heap) *dependence_relations)
4455 struct data_dependence_relation *ddr;
4457 FOR_EACH_VEC_ELT (ddr_p, dependence_relations, i, ddr)
4459 free_dependence_relation (ddr);
4461 VEC_free (ddr_p, heap, dependence_relations);
4464 /* Free the memory used by the data references from DATAREFS. */
4467 free_data_refs (VEC (data_reference_p, heap) *datarefs)
4470 struct data_reference *dr;
4472 FOR_EACH_VEC_ELT (data_reference_p, datarefs, i, dr)
4474 VEC_free (data_reference_p, heap, datarefs);
4479 /* Dump vertex I in RDG to FILE. */
4482 dump_rdg_vertex (FILE *file, struct graph *rdg, int i)
4484 struct vertex *v = &(rdg->vertices[i]);
4485 struct graph_edge *e;
4487 fprintf (file, "(vertex %d: (%s%s) (in:", i,
4488 RDG_MEM_WRITE_STMT (rdg, i) ? "w" : "",
4489 RDG_MEM_READS_STMT (rdg, i) ? "r" : "");
4492 for (e = v->pred; e; e = e->pred_next)
4493 fprintf (file, " %d", e->src);
4495 fprintf (file, ") (out:");
4498 for (e = v->succ; e; e = e->succ_next)
4499 fprintf (file, " %d", e->dest);
4501 fprintf (file, ")\n");
4502 print_gimple_stmt (file, RDGV_STMT (v), 0, TDF_VOPS|TDF_MEMSYMS);
4503 fprintf (file, ")\n");
4506 /* Call dump_rdg_vertex on stderr. */
4509 debug_rdg_vertex (struct graph *rdg, int i)
4511 dump_rdg_vertex (stderr, rdg, i);
4514 /* Dump component C of RDG to FILE. If DUMPED is non-null, set the
4515 dumped vertices to that bitmap. */
4517 void dump_rdg_component (FILE *file, struct graph *rdg, int c, bitmap dumped)
4521 fprintf (file, "(%d\n", c);
4523 for (i = 0; i < rdg->n_vertices; i++)
4524 if (rdg->vertices[i].component == c)
4527 bitmap_set_bit (dumped, i);
4529 dump_rdg_vertex (file, rdg, i);
4532 fprintf (file, ")\n");
4535 /* Call dump_rdg_vertex on stderr. */
4538 debug_rdg_component (struct graph *rdg, int c)
4540 dump_rdg_component (stderr, rdg, c, NULL);
4543 /* Dump the reduced dependence graph RDG to FILE. */
4546 dump_rdg (FILE *file, struct graph *rdg)
4549 bitmap dumped = BITMAP_ALLOC (NULL);
4551 fprintf (file, "(rdg\n");
4553 for (i = 0; i < rdg->n_vertices; i++)
4554 if (!bitmap_bit_p (dumped, i))
4555 dump_rdg_component (file, rdg, rdg->vertices[i].component, dumped);
4557 fprintf (file, ")\n");
4558 BITMAP_FREE (dumped);
4561 /* Call dump_rdg on stderr. */
4564 debug_rdg (struct graph *rdg)
4566 dump_rdg (stderr, rdg);
4570 dot_rdg_1 (FILE *file, struct graph *rdg)
4574 fprintf (file, "digraph RDG {\n");
4576 for (i = 0; i < rdg->n_vertices; i++)
4578 struct vertex *v = &(rdg->vertices[i]);
4579 struct graph_edge *e;
4581 /* Highlight reads from memory. */
4582 if (RDG_MEM_READS_STMT (rdg, i))
4583 fprintf (file, "%d [style=filled, fillcolor=green]\n", i);
4585 /* Highlight stores to memory. */
4586 if (RDG_MEM_WRITE_STMT (rdg, i))
4587 fprintf (file, "%d [style=filled, fillcolor=red]\n", i);
4590 for (e = v->succ; e; e = e->succ_next)
4591 switch (RDGE_TYPE (e))
4594 fprintf (file, "%d -> %d [label=input] \n", i, e->dest);
4598 fprintf (file, "%d -> %d [label=output] \n", i, e->dest);
4602 /* These are the most common dependences: don't print these. */
4603 fprintf (file, "%d -> %d \n", i, e->dest);
4607 fprintf (file, "%d -> %d [label=anti] \n", i, e->dest);
4615 fprintf (file, "}\n\n");
4618 /* Display the Reduced Dependence Graph using dotty. */
4619 extern void dot_rdg (struct graph *);
4622 dot_rdg (struct graph *rdg)
4624 /* When debugging, enable the following code. This cannot be used
4625 in production compilers because it calls "system". */
4627 FILE *file = fopen ("/tmp/rdg.dot", "w");
4628 gcc_assert (file != NULL);
4630 dot_rdg_1 (file, rdg);
4633 system ("dotty /tmp/rdg.dot &");
4635 dot_rdg_1 (stderr, rdg);
4639 /* This structure is used for recording the mapping statement index in
4642 struct GTY(()) rdg_vertex_info
4648 /* Returns the index of STMT in RDG. */
4651 rdg_vertex_for_stmt (struct graph *rdg, gimple stmt)
4653 struct rdg_vertex_info rvi, *slot;
4656 slot = (struct rdg_vertex_info *) htab_find (rdg->indices, &rvi);
4664 /* Creates an edge in RDG for each distance vector from DDR. The
4665 order that we keep track of in the RDG is the order in which
4666 statements have to be executed. */
4669 create_rdg_edge_for_ddr (struct graph *rdg, ddr_p ddr)
4671 struct graph_edge *e;
4673 data_reference_p dra = DDR_A (ddr);
4674 data_reference_p drb = DDR_B (ddr);
4675 unsigned level = ddr_dependence_level (ddr);
4677 /* For non scalar dependences, when the dependence is REVERSED,
4678 statement B has to be executed before statement A. */
4680 && !DDR_REVERSED_P (ddr))
4682 data_reference_p tmp = dra;
4687 va = rdg_vertex_for_stmt (rdg, DR_STMT (dra));
4688 vb = rdg_vertex_for_stmt (rdg, DR_STMT (drb));
4690 if (va < 0 || vb < 0)
4693 e = add_edge (rdg, va, vb);
4694 e->data = XNEW (struct rdg_edge);
4696 RDGE_LEVEL (e) = level;
4697 RDGE_RELATION (e) = ddr;
4699 /* Determines the type of the data dependence. */
4700 if (DR_IS_READ (dra) && DR_IS_READ (drb))
4701 RDGE_TYPE (e) = input_dd;
4702 else if (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))
4703 RDGE_TYPE (e) = output_dd;
4704 else if (DR_IS_WRITE (dra) && DR_IS_READ (drb))
4705 RDGE_TYPE (e) = flow_dd;
4706 else if (DR_IS_READ (dra) && DR_IS_WRITE (drb))
4707 RDGE_TYPE (e) = anti_dd;
4710 /* Creates dependence edges in RDG for all the uses of DEF. IDEF is
4711 the index of DEF in RDG. */
4714 create_rdg_edges_for_scalar (struct graph *rdg, tree def, int idef)
4716 use_operand_p imm_use_p;
4717 imm_use_iterator iterator;
4719 FOR_EACH_IMM_USE_FAST (imm_use_p, iterator, def)
4721 struct graph_edge *e;
4722 int use = rdg_vertex_for_stmt (rdg, USE_STMT (imm_use_p));
4727 e = add_edge (rdg, idef, use);
4728 e->data = XNEW (struct rdg_edge);
4729 RDGE_TYPE (e) = flow_dd;
4730 RDGE_RELATION (e) = NULL;
4734 /* Creates the edges of the reduced dependence graph RDG. */
4737 create_rdg_edges (struct graph *rdg, VEC (ddr_p, heap) *ddrs)
4740 struct data_dependence_relation *ddr;
4741 def_operand_p def_p;
4744 FOR_EACH_VEC_ELT (ddr_p, ddrs, i, ddr)
4745 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4746 create_rdg_edge_for_ddr (rdg, ddr);
4748 for (i = 0; i < rdg->n_vertices; i++)
4749 FOR_EACH_PHI_OR_STMT_DEF (def_p, RDG_STMT (rdg, i),
4751 create_rdg_edges_for_scalar (rdg, DEF_FROM_PTR (def_p), i);
4754 /* Build the vertices of the reduced dependence graph RDG. */
4757 create_rdg_vertices (struct graph *rdg, VEC (gimple, heap) *stmts)
4762 FOR_EACH_VEC_ELT (gimple, stmts, i, stmt)
4764 VEC (data_ref_loc, heap) *references;
4766 struct vertex *v = &(rdg->vertices[i]);
4767 struct rdg_vertex_info *rvi = XNEW (struct rdg_vertex_info);
4768 struct rdg_vertex_info **slot;
4772 slot = (struct rdg_vertex_info **) htab_find_slot (rdg->indices, rvi, INSERT);
4779 v->data = XNEW (struct rdg_vertex);
4780 RDG_STMT (rdg, i) = stmt;
4782 RDG_MEM_WRITE_STMT (rdg, i) = false;
4783 RDG_MEM_READS_STMT (rdg, i) = false;
4784 if (gimple_code (stmt) == GIMPLE_PHI)
4787 get_references_in_stmt (stmt, &references);
4788 FOR_EACH_VEC_ELT (data_ref_loc, references, j, ref)
4790 RDG_MEM_WRITE_STMT (rdg, i) = true;
4792 RDG_MEM_READS_STMT (rdg, i) = true;
4794 VEC_free (data_ref_loc, heap, references);
4798 /* Initialize STMTS with all the statements of LOOP. When
4799 INCLUDE_PHIS is true, include also the PHI nodes. The order in
4800 which we discover statements is important as
4801 generate_loops_for_partition is using the same traversal for
4802 identifying statements. */
4805 stmts_from_loop (struct loop *loop, VEC (gimple, heap) **stmts)
4808 basic_block *bbs = get_loop_body_in_dom_order (loop);
4810 for (i = 0; i < loop->num_nodes; i++)
4812 basic_block bb = bbs[i];
4813 gimple_stmt_iterator bsi;
4816 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4817 VEC_safe_push (gimple, heap, *stmts, gsi_stmt (bsi));
4819 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4821 stmt = gsi_stmt (bsi);
4822 if (gimple_code (stmt) != GIMPLE_LABEL)
4823 VEC_safe_push (gimple, heap, *stmts, stmt);
4830 /* Returns true when all the dependences are computable. */
4833 known_dependences_p (VEC (ddr_p, heap) *dependence_relations)
4838 FOR_EACH_VEC_ELT (ddr_p, dependence_relations, i, ddr)
4839 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
4845 /* Computes a hash function for element ELT. */
4848 hash_stmt_vertex_info (const void *elt)
4850 const struct rdg_vertex_info *const rvi =
4851 (const struct rdg_vertex_info *) elt;
4852 gimple stmt = rvi->stmt;
4854 return htab_hash_pointer (stmt);
4857 /* Compares database elements E1 and E2. */
4860 eq_stmt_vertex_info (const void *e1, const void *e2)
4862 const struct rdg_vertex_info *elt1 = (const struct rdg_vertex_info *) e1;
4863 const struct rdg_vertex_info *elt2 = (const struct rdg_vertex_info *) e2;
4865 return elt1->stmt == elt2->stmt;
4868 /* Free the element E. */
4871 hash_stmt_vertex_del (void *e)
4876 /* Build the Reduced Dependence Graph (RDG) with one vertex per
4877 statement of the loop nest, and one edge per data dependence or
4878 scalar dependence. */
4881 build_empty_rdg (int n_stmts)
4883 int nb_data_refs = 10;
4884 struct graph *rdg = new_graph (n_stmts);
4886 rdg->indices = htab_create (nb_data_refs, hash_stmt_vertex_info,
4887 eq_stmt_vertex_info, hash_stmt_vertex_del);
4891 /* Build the Reduced Dependence Graph (RDG) with one vertex per
4892 statement of the loop nest, and one edge per data dependence or
4893 scalar dependence. */
4896 build_rdg (struct loop *loop,
4897 VEC (loop_p, heap) **loop_nest,
4898 VEC (ddr_p, heap) **dependence_relations,
4899 VEC (data_reference_p, heap) **datarefs)
4901 struct graph *rdg = NULL;
4902 VEC (gimple, heap) *stmts = VEC_alloc (gimple, heap, 10);
4904 compute_data_dependences_for_loop (loop, false, loop_nest, datarefs,
4905 dependence_relations);
4907 if (known_dependences_p (*dependence_relations))
4909 stmts_from_loop (loop, &stmts);
4910 rdg = build_empty_rdg (VEC_length (gimple, stmts));
4911 create_rdg_vertices (rdg, stmts);
4912 create_rdg_edges (rdg, *dependence_relations);
4915 VEC_free (gimple, heap, stmts);
4919 /* Free the reduced dependence graph RDG. */
4922 free_rdg (struct graph *rdg)
4926 for (i = 0; i < rdg->n_vertices; i++)
4928 struct vertex *v = &(rdg->vertices[i]);
4929 struct graph_edge *e;
4931 for (e = v->succ; e; e = e->succ_next)
4939 htab_delete (rdg->indices);
4943 /* Initialize STMTS with all the statements of LOOP that contain a
4947 stores_from_loop (struct loop *loop, VEC (gimple, heap) **stmts)
4950 basic_block *bbs = get_loop_body_in_dom_order (loop);
4952 for (i = 0; i < loop->num_nodes; i++)
4954 basic_block bb = bbs[i];
4955 gimple_stmt_iterator bsi;
4957 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4958 if (gimple_vdef (gsi_stmt (bsi)))
4959 VEC_safe_push (gimple, heap, *stmts, gsi_stmt (bsi));
4965 /* Returns true when the statement at STMT is of the form "A[i] = 0"
4966 that contains a data reference on its LHS with a stride of the same
4967 size as its unit type. */
4970 stmt_with_adjacent_zero_store_dr_p (gimple stmt)
4974 struct data_reference *dr;
4977 || !gimple_vdef (stmt)
4978 || !is_gimple_assign (stmt)
4979 || !gimple_assign_single_p (stmt)
4980 || !(op1 = gimple_assign_rhs1 (stmt))
4981 || !(integer_zerop (op1) || real_zerop (op1)))
4984 dr = XCNEW (struct data_reference);
4985 op0 = gimple_assign_lhs (stmt);
4987 DR_STMT (dr) = stmt;
4990 res = dr_analyze_innermost (dr)
4991 && stride_of_unit_type_p (DR_STEP (dr), TREE_TYPE (op0));
4997 /* Initialize STMTS with all the statements of LOOP that contain a
4998 store to memory of the form "A[i] = 0". */
5001 stores_zero_from_loop (struct loop *loop, VEC (gimple, heap) **stmts)
5005 gimple_stmt_iterator si;
5007 basic_block *bbs = get_loop_body_in_dom_order (loop);
5009 for (i = 0; i < loop->num_nodes; i++)
5010 for (bb = bbs[i], si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5011 if ((stmt = gsi_stmt (si))
5012 && stmt_with_adjacent_zero_store_dr_p (stmt))
5013 VEC_safe_push (gimple, heap, *stmts, gsi_stmt (si));
5018 /* For a data reference REF, return the declaration of its base
5019 address or NULL_TREE if the base is not determined. */
5022 ref_base_address (gimple stmt, data_ref_loc *ref)
5024 tree base = NULL_TREE;
5026 struct data_reference *dr = XCNEW (struct data_reference);
5028 DR_STMT (dr) = stmt;
5029 DR_REF (dr) = *ref->pos;
5030 dr_analyze_innermost (dr);
5031 base_address = DR_BASE_ADDRESS (dr);
5036 switch (TREE_CODE (base_address))
5039 base = TREE_OPERAND (base_address, 0);
5043 base = base_address;
5052 /* Determines whether the statement from vertex V of the RDG has a
5053 definition used outside the loop that contains this statement. */
5056 rdg_defs_used_in_other_loops_p (struct graph *rdg, int v)
5058 gimple stmt = RDG_STMT (rdg, v);
5059 struct loop *loop = loop_containing_stmt (stmt);
5060 use_operand_p imm_use_p;
5061 imm_use_iterator iterator;
5063 def_operand_p def_p;
5068 FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, it, SSA_OP_DEF)
5070 FOR_EACH_IMM_USE_FAST (imm_use_p, iterator, DEF_FROM_PTR (def_p))
5072 if (loop_containing_stmt (USE_STMT (imm_use_p)) != loop)
5080 /* Determines whether statements S1 and S2 access to similar memory
5081 locations. Two memory accesses are considered similar when they
5082 have the same base address declaration, i.e. when their
5083 ref_base_address is the same. */
5086 have_similar_memory_accesses (gimple s1, gimple s2)
5090 VEC (data_ref_loc, heap) *refs1, *refs2;
5091 data_ref_loc *ref1, *ref2;
5093 get_references_in_stmt (s1, &refs1);
5094 get_references_in_stmt (s2, &refs2);
5096 FOR_EACH_VEC_ELT (data_ref_loc, refs1, i, ref1)
5098 tree base1 = ref_base_address (s1, ref1);
5101 FOR_EACH_VEC_ELT (data_ref_loc, refs2, j, ref2)
5102 if (base1 == ref_base_address (s2, ref2))
5110 VEC_free (data_ref_loc, heap, refs1);
5111 VEC_free (data_ref_loc, heap, refs2);
5115 /* Helper function for the hashtab. */
5118 have_similar_memory_accesses_1 (const void *s1, const void *s2)
5120 return have_similar_memory_accesses (CONST_CAST_GIMPLE ((const_gimple) s1),
5121 CONST_CAST_GIMPLE ((const_gimple) s2));
5124 /* Helper function for the hashtab. */
5127 ref_base_address_1 (const void *s)
5129 gimple stmt = CONST_CAST_GIMPLE ((const_gimple) s);
5131 VEC (data_ref_loc, heap) *refs;
5135 get_references_in_stmt (stmt, &refs);
5137 FOR_EACH_VEC_ELT (data_ref_loc, refs, i, ref)
5140 res = htab_hash_pointer (ref_base_address (stmt, ref));
5144 VEC_free (data_ref_loc, heap, refs);
5148 /* Try to remove duplicated write data references from STMTS. */
5151 remove_similar_memory_refs (VEC (gimple, heap) **stmts)
5155 htab_t seen = htab_create (VEC_length (gimple, *stmts), ref_base_address_1,
5156 have_similar_memory_accesses_1, NULL);
5158 for (i = 0; VEC_iterate (gimple, *stmts, i, stmt); )
5162 slot = htab_find_slot (seen, stmt, INSERT);
5165 VEC_ordered_remove (gimple, *stmts, i);
5168 *slot = (void *) stmt;
5176 /* Returns the index of PARAMETER in the parameters vector of the
5177 ACCESS_MATRIX. If PARAMETER does not exist return -1. */
5180 access_matrix_get_index_for_parameter (tree parameter,
5181 struct access_matrix *access_matrix)
5184 VEC (tree,heap) *lambda_parameters = AM_PARAMETERS (access_matrix);
5185 tree lambda_parameter;
5187 FOR_EACH_VEC_ELT (tree, lambda_parameters, i, lambda_parameter)
5188 if (lambda_parameter == parameter)
5189 return i + AM_NB_INDUCTION_VARS (access_matrix);