1 /* Data references and dependences detectors.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This pass walks a given loop structure searching for array
22 references. The information about the array accesses is recorded
23 in DATA_REFERENCE structures.
25 The basic test for determining the dependences is:
26 given two access functions chrec1 and chrec2 to a same array, and
27 x and y two vectors from the iteration domain, the same element of
28 the array is accessed twice at iterations x and y if and only if:
29 | chrec1 (x) == chrec2 (y).
31 The goals of this analysis are:
33 - to determine the independence: the relation between two
34 independent accesses is qualified with the chrec_known (this
35 information allows a loop parallelization),
37 - when two data references access the same data, to qualify the
38 dependence relation with classic dependence representations:
42 - loop carried level dependence
43 - polyhedron dependence
44 or with the chains of recurrences based representation,
46 - to define a knowledge base for storing the data dependence
49 - to define an interface to access this data.
54 - subscript: given two array accesses a subscript is the tuple
55 composed of the access functions for a given dimension. Example:
56 Given A[f1][f2][f3] and B[g1][g2][g3], there are three subscripts:
57 (f1, g1), (f2, g2), (f3, g3).
59 - Diophantine equation: an equation whose coefficients and
60 solutions are integer constants, for example the equation
62 has an integer solution x = 1 and y = -1.
66 - "Advanced Compilation for High Performance Computing" by Randy
67 Allen and Ken Kennedy.
68 http://citeseer.ist.psu.edu/goff91practical.html
70 - "Loop Transformations for Restructuring Compilers - The Foundations"
78 #include "coretypes.h"
81 #include "gimple-pretty-print.h"
82 #include "basic-block.h"
83 #include "tree-ssa-alias.h"
84 #include "internal-fn.h"
85 #include "gimple-expr.h"
88 #include "gimple-iterator.h"
89 #include "tree-ssa-loop-niter.h"
90 #include "tree-ssa-loop.h"
93 #include "tree-data-ref.h"
94 #include "tree-scalar-evolution.h"
96 #include "langhooks.h"
97 #include "tree-affine.h"
100 static struct datadep_stats
102 int num_dependence_tests
;
103 int num_dependence_dependent
;
104 int num_dependence_independent
;
105 int num_dependence_undetermined
;
107 int num_subscript_tests
;
108 int num_subscript_undetermined
;
109 int num_same_subscript_function
;
112 int num_ziv_independent
;
113 int num_ziv_dependent
;
114 int num_ziv_unimplemented
;
117 int num_siv_independent
;
118 int num_siv_dependent
;
119 int num_siv_unimplemented
;
122 int num_miv_independent
;
123 int num_miv_dependent
;
124 int num_miv_unimplemented
;
127 static bool subscript_dependence_tester_1 (struct data_dependence_relation
*,
128 struct data_reference
*,
129 struct data_reference
*,
131 /* Returns true iff A divides B. */
134 tree_fold_divides_p (const_tree a
, const_tree b
)
136 gcc_assert (TREE_CODE (a
) == INTEGER_CST
);
137 gcc_assert (TREE_CODE (b
) == INTEGER_CST
);
138 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR
, b
, a
));
141 /* Returns true iff A divides B. */
144 int_divides_p (int a
, int b
)
146 return ((b
% a
) == 0);
151 /* Dump into FILE all the data references from DATAREFS. */
154 dump_data_references (FILE *file
, vec
<data_reference_p
> datarefs
)
157 struct data_reference
*dr
;
159 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
160 dump_data_reference (file
, dr
);
163 /* Unified dump into FILE all the data references from DATAREFS. */
166 debug (vec
<data_reference_p
> &ref
)
168 dump_data_references (stderr
, ref
);
172 debug (vec
<data_reference_p
> *ptr
)
177 fprintf (stderr
, "<nil>\n");
181 /* Dump into STDERR all the data references from DATAREFS. */
184 debug_data_references (vec
<data_reference_p
> datarefs
)
186 dump_data_references (stderr
, datarefs
);
189 /* Print to STDERR the data_reference DR. */
192 debug_data_reference (struct data_reference
*dr
)
194 dump_data_reference (stderr
, dr
);
197 /* Dump function for a DATA_REFERENCE structure. */
200 dump_data_reference (FILE *outf
,
201 struct data_reference
*dr
)
205 fprintf (outf
, "#(Data Ref: \n");
206 fprintf (outf
, "# bb: %d \n", gimple_bb (DR_STMT (dr
))->index
);
207 fprintf (outf
, "# stmt: ");
208 print_gimple_stmt (outf
, DR_STMT (dr
), 0, 0);
209 fprintf (outf
, "# ref: ");
210 print_generic_stmt (outf
, DR_REF (dr
), 0);
211 fprintf (outf
, "# base_object: ");
212 print_generic_stmt (outf
, DR_BASE_OBJECT (dr
), 0);
214 for (i
= 0; i
< DR_NUM_DIMENSIONS (dr
); i
++)
216 fprintf (outf
, "# Access function %d: ", i
);
217 print_generic_stmt (outf
, DR_ACCESS_FN (dr
, i
), 0);
219 fprintf (outf
, "#)\n");
222 /* Unified dump function for a DATA_REFERENCE structure. */
225 debug (data_reference
&ref
)
227 dump_data_reference (stderr
, &ref
);
231 debug (data_reference
*ptr
)
236 fprintf (stderr
, "<nil>\n");
240 /* Dumps the affine function described by FN to the file OUTF. */
243 dump_affine_function (FILE *outf
, affine_fn fn
)
248 print_generic_expr (outf
, fn
[0], TDF_SLIM
);
249 for (i
= 1; fn
.iterate (i
, &coef
); i
++)
251 fprintf (outf
, " + ");
252 print_generic_expr (outf
, coef
, TDF_SLIM
);
253 fprintf (outf
, " * x_%u", i
);
257 /* Dumps the conflict function CF to the file OUTF. */
260 dump_conflict_function (FILE *outf
, conflict_function
*cf
)
264 if (cf
->n
== NO_DEPENDENCE
)
265 fprintf (outf
, "no dependence");
266 else if (cf
->n
== NOT_KNOWN
)
267 fprintf (outf
, "not known");
270 for (i
= 0; i
< cf
->n
; i
++)
275 dump_affine_function (outf
, cf
->fns
[i
]);
281 /* Dump function for a SUBSCRIPT structure. */
284 dump_subscript (FILE *outf
, struct subscript
*subscript
)
286 conflict_function
*cf
= SUB_CONFLICTS_IN_A (subscript
);
288 fprintf (outf
, "\n (subscript \n");
289 fprintf (outf
, " iterations_that_access_an_element_twice_in_A: ");
290 dump_conflict_function (outf
, cf
);
291 if (CF_NONTRIVIAL_P (cf
))
293 tree last_iteration
= SUB_LAST_CONFLICT (subscript
);
294 fprintf (outf
, "\n last_conflict: ");
295 print_generic_expr (outf
, last_iteration
, 0);
298 cf
= SUB_CONFLICTS_IN_B (subscript
);
299 fprintf (outf
, "\n iterations_that_access_an_element_twice_in_B: ");
300 dump_conflict_function (outf
, cf
);
301 if (CF_NONTRIVIAL_P (cf
))
303 tree last_iteration
= SUB_LAST_CONFLICT (subscript
);
304 fprintf (outf
, "\n last_conflict: ");
305 print_generic_expr (outf
, last_iteration
, 0);
308 fprintf (outf
, "\n (Subscript distance: ");
309 print_generic_expr (outf
, SUB_DISTANCE (subscript
), 0);
310 fprintf (outf
, " ))\n");
313 /* Print the classic direction vector DIRV to OUTF. */
316 print_direction_vector (FILE *outf
,
322 for (eq
= 0; eq
< length
; eq
++)
324 enum data_dependence_direction dir
= ((enum data_dependence_direction
)
330 fprintf (outf
, " +");
333 fprintf (outf
, " -");
336 fprintf (outf
, " =");
338 case dir_positive_or_equal
:
339 fprintf (outf
, " +=");
341 case dir_positive_or_negative
:
342 fprintf (outf
, " +-");
344 case dir_negative_or_equal
:
345 fprintf (outf
, " -=");
348 fprintf (outf
, " *");
351 fprintf (outf
, "indep");
355 fprintf (outf
, "\n");
358 /* Print a vector of direction vectors. */
361 print_dir_vectors (FILE *outf
, vec
<lambda_vector
> dir_vects
,
367 FOR_EACH_VEC_ELT (dir_vects
, j
, v
)
368 print_direction_vector (outf
, v
, length
);
371 /* Print out a vector VEC of length N to OUTFILE. */
374 print_lambda_vector (FILE * outfile
, lambda_vector vector
, int n
)
378 for (i
= 0; i
< n
; i
++)
379 fprintf (outfile
, "%3d ", vector
[i
]);
380 fprintf (outfile
, "\n");
383 /* Print a vector of distance vectors. */
386 print_dist_vectors (FILE *outf
, vec
<lambda_vector
> dist_vects
,
392 FOR_EACH_VEC_ELT (dist_vects
, j
, v
)
393 print_lambda_vector (outf
, v
, length
);
396 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
399 dump_data_dependence_relation (FILE *outf
,
400 struct data_dependence_relation
*ddr
)
402 struct data_reference
*dra
, *drb
;
404 fprintf (outf
, "(Data Dep: \n");
406 if (!ddr
|| DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
413 dump_data_reference (outf
, dra
);
415 fprintf (outf
, " (nil)\n");
417 dump_data_reference (outf
, drb
);
419 fprintf (outf
, " (nil)\n");
421 fprintf (outf
, " (don't know)\n)\n");
427 dump_data_reference (outf
, dra
);
428 dump_data_reference (outf
, drb
);
430 if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
431 fprintf (outf
, " (no dependence)\n");
433 else if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
438 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
440 fprintf (outf
, " access_fn_A: ");
441 print_generic_stmt (outf
, DR_ACCESS_FN (dra
, i
), 0);
442 fprintf (outf
, " access_fn_B: ");
443 print_generic_stmt (outf
, DR_ACCESS_FN (drb
, i
), 0);
444 dump_subscript (outf
, DDR_SUBSCRIPT (ddr
, i
));
447 fprintf (outf
, " inner loop index: %d\n", DDR_INNER_LOOP (ddr
));
448 fprintf (outf
, " loop nest: (");
449 FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr
), i
, loopi
)
450 fprintf (outf
, "%d ", loopi
->num
);
451 fprintf (outf
, ")\n");
453 for (i
= 0; i
< DDR_NUM_DIST_VECTS (ddr
); i
++)
455 fprintf (outf
, " distance_vector: ");
456 print_lambda_vector (outf
, DDR_DIST_VECT (ddr
, i
),
460 for (i
= 0; i
< DDR_NUM_DIR_VECTS (ddr
); i
++)
462 fprintf (outf
, " direction_vector: ");
463 print_direction_vector (outf
, DDR_DIR_VECT (ddr
, i
),
468 fprintf (outf
, ")\n");
474 debug_data_dependence_relation (struct data_dependence_relation
*ddr
)
476 dump_data_dependence_relation (stderr
, ddr
);
479 /* Dump into FILE all the dependence relations from DDRS. */
482 dump_data_dependence_relations (FILE *file
,
486 struct data_dependence_relation
*ddr
;
488 FOR_EACH_VEC_ELT (ddrs
, i
, ddr
)
489 dump_data_dependence_relation (file
, ddr
);
493 debug (vec
<ddr_p
> &ref
)
495 dump_data_dependence_relations (stderr
, ref
);
499 debug (vec
<ddr_p
> *ptr
)
504 fprintf (stderr
, "<nil>\n");
508 /* Dump to STDERR all the dependence relations from DDRS. */
511 debug_data_dependence_relations (vec
<ddr_p
> ddrs
)
513 dump_data_dependence_relations (stderr
, ddrs
);
516 /* Dumps the distance and direction vectors in FILE. DDRS contains
517 the dependence relations, and VECT_SIZE is the size of the
518 dependence vectors, or in other words the number of loops in the
522 dump_dist_dir_vectors (FILE *file
, vec
<ddr_p
> ddrs
)
525 struct data_dependence_relation
*ddr
;
528 FOR_EACH_VEC_ELT (ddrs
, i
, ddr
)
529 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
&& DDR_AFFINE_P (ddr
))
531 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr
), j
, v
)
533 fprintf (file
, "DISTANCE_V (");
534 print_lambda_vector (file
, v
, DDR_NB_LOOPS (ddr
));
535 fprintf (file
, ")\n");
538 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr
), j
, v
)
540 fprintf (file
, "DIRECTION_V (");
541 print_direction_vector (file
, v
, DDR_NB_LOOPS (ddr
));
542 fprintf (file
, ")\n");
546 fprintf (file
, "\n\n");
549 /* Dumps the data dependence relations DDRS in FILE. */
552 dump_ddrs (FILE *file
, vec
<ddr_p
> ddrs
)
555 struct data_dependence_relation
*ddr
;
557 FOR_EACH_VEC_ELT (ddrs
, i
, ddr
)
558 dump_data_dependence_relation (file
, ddr
);
560 fprintf (file
, "\n\n");
564 debug_ddrs (vec
<ddr_p
> ddrs
)
566 dump_ddrs (stderr
, ddrs
);
569 /* Helper function for split_constant_offset. Expresses OP0 CODE OP1
570 (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
571 constant of type ssizetype, and returns true. If we cannot do this
572 with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
576 split_constant_offset_1 (tree type
, tree op0
, enum tree_code code
, tree op1
,
577 tree
*var
, tree
*off
)
581 enum tree_code ocode
= code
;
589 *var
= build_int_cst (type
, 0);
590 *off
= fold_convert (ssizetype
, op0
);
593 case POINTER_PLUS_EXPR
:
598 split_constant_offset (op0
, &var0
, &off0
);
599 split_constant_offset (op1
, &var1
, &off1
);
600 *var
= fold_build2 (code
, type
, var0
, var1
);
601 *off
= size_binop (ocode
, off0
, off1
);
605 if (TREE_CODE (op1
) != INTEGER_CST
)
608 split_constant_offset (op0
, &var0
, &off0
);
609 *var
= fold_build2 (MULT_EXPR
, type
, var0
, op1
);
610 *off
= size_binop (MULT_EXPR
, off0
, fold_convert (ssizetype
, op1
));
616 HOST_WIDE_INT pbitsize
, pbitpos
;
617 enum machine_mode pmode
;
618 int punsignedp
, pvolatilep
;
620 op0
= TREE_OPERAND (op0
, 0);
621 base
= get_inner_reference (op0
, &pbitsize
, &pbitpos
, &poffset
,
622 &pmode
, &punsignedp
, &pvolatilep
, false);
624 if (pbitpos
% BITS_PER_UNIT
!= 0)
626 base
= build_fold_addr_expr (base
);
627 off0
= ssize_int (pbitpos
/ BITS_PER_UNIT
);
631 split_constant_offset (poffset
, &poffset
, &off1
);
632 off0
= size_binop (PLUS_EXPR
, off0
, off1
);
633 if (POINTER_TYPE_P (TREE_TYPE (base
)))
634 base
= fold_build_pointer_plus (base
, poffset
);
636 base
= fold_build2 (PLUS_EXPR
, TREE_TYPE (base
), base
,
637 fold_convert (TREE_TYPE (base
), poffset
));
640 var0
= fold_convert (type
, base
);
642 /* If variable length types are involved, punt, otherwise casts
643 might be converted into ARRAY_REFs in gimplify_conversion.
644 To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
645 possibly no longer appears in current GIMPLE, might resurface.
646 This perhaps could run
647 if (CONVERT_EXPR_P (var0))
649 gimplify_conversion (&var0);
650 // Attempt to fill in any within var0 found ARRAY_REF's
651 // element size from corresponding op embedded ARRAY_REF,
652 // if unsuccessful, just punt.
654 while (POINTER_TYPE_P (type
))
655 type
= TREE_TYPE (type
);
656 if (int_size_in_bytes (type
) < 0)
666 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
667 enum tree_code subcode
;
669 if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
672 var0
= gimple_assign_rhs1 (def_stmt
);
673 subcode
= gimple_assign_rhs_code (def_stmt
);
674 var1
= gimple_assign_rhs2 (def_stmt
);
676 return split_constant_offset_1 (type
, var0
, subcode
, var1
, var
, off
);
680 /* We must not introduce undefined overflow, and we must not change the value.
681 Hence we're okay if the inner type doesn't overflow to start with
682 (pointer or signed), the outer type also is an integer or pointer
683 and the outer precision is at least as large as the inner. */
684 tree itype
= TREE_TYPE (op0
);
685 if ((POINTER_TYPE_P (itype
)
686 || (INTEGRAL_TYPE_P (itype
) && TYPE_OVERFLOW_UNDEFINED (itype
)))
687 && TYPE_PRECISION (type
) >= TYPE_PRECISION (itype
)
688 && (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)))
690 split_constant_offset (op0
, &var0
, off
);
691 *var
= fold_convert (type
, var0
);
702 /* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
703 will be ssizetype. */
706 split_constant_offset (tree exp
, tree
*var
, tree
*off
)
708 tree type
= TREE_TYPE (exp
), otype
, op0
, op1
, e
, o
;
712 *off
= ssize_int (0);
715 if (tree_is_chrec (exp
)
716 || get_gimple_rhs_class (TREE_CODE (exp
)) == GIMPLE_TERNARY_RHS
)
719 otype
= TREE_TYPE (exp
);
720 code
= TREE_CODE (exp
);
721 extract_ops_from_tree (exp
, &code
, &op0
, &op1
);
722 if (split_constant_offset_1 (otype
, op0
, code
, op1
, &e
, &o
))
724 *var
= fold_convert (type
, e
);
729 /* Returns the address ADDR of an object in a canonical shape (without nop
730 casts, and with type of pointer to the object). */
733 canonicalize_base_object_address (tree addr
)
739 /* The base address may be obtained by casting from integer, in that case
741 if (!POINTER_TYPE_P (TREE_TYPE (addr
)))
744 if (TREE_CODE (addr
) != ADDR_EXPR
)
747 return build_fold_addr_expr (TREE_OPERAND (addr
, 0));
750 /* Analyzes the behavior of the memory reference DR in the innermost loop or
751 basic block that contains it. Returns true if analysis succeed or false
755 dr_analyze_innermost (struct data_reference
*dr
, struct loop
*nest
)
757 gimple stmt
= DR_STMT (dr
);
758 struct loop
*loop
= loop_containing_stmt (stmt
);
759 tree ref
= DR_REF (dr
);
760 HOST_WIDE_INT pbitsize
, pbitpos
;
762 enum machine_mode pmode
;
763 int punsignedp
, pvolatilep
;
764 affine_iv base_iv
, offset_iv
;
765 tree init
, dinit
, step
;
766 bool in_loop
= (loop
&& loop
->num
);
768 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
769 fprintf (dump_file
, "analyze_innermost: ");
771 base
= get_inner_reference (ref
, &pbitsize
, &pbitpos
, &poffset
,
772 &pmode
, &punsignedp
, &pvolatilep
, false);
773 gcc_assert (base
!= NULL_TREE
);
775 if (pbitpos
% BITS_PER_UNIT
!= 0)
777 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
778 fprintf (dump_file
, "failed: bit offset alignment.\n");
782 if (TREE_CODE (base
) == MEM_REF
)
784 if (!integer_zerop (TREE_OPERAND (base
, 1)))
786 offset_int moff
= mem_ref_offset (base
);
787 tree mofft
= wide_int_to_tree (sizetype
, moff
);
791 poffset
= size_binop (PLUS_EXPR
, poffset
, mofft
);
793 base
= TREE_OPERAND (base
, 0);
796 base
= build_fold_addr_expr (base
);
800 if (!simple_iv (loop
, loop_containing_stmt (stmt
), base
, &base_iv
,
801 nest
? true : false))
805 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
806 fprintf (dump_file
, "failed: evolution of base is not"
813 base_iv
.step
= ssize_int (0);
814 base_iv
.no_overflow
= true;
821 base_iv
.step
= ssize_int (0);
822 base_iv
.no_overflow
= true;
827 offset_iv
.base
= ssize_int (0);
828 offset_iv
.step
= ssize_int (0);
834 offset_iv
.base
= poffset
;
835 offset_iv
.step
= ssize_int (0);
837 else if (!simple_iv (loop
, loop_containing_stmt (stmt
),
839 nest
? true : false))
843 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
844 fprintf (dump_file
, "failed: evolution of offset is not"
850 offset_iv
.base
= poffset
;
851 offset_iv
.step
= ssize_int (0);
856 init
= ssize_int (pbitpos
/ BITS_PER_UNIT
);
857 split_constant_offset (base_iv
.base
, &base_iv
.base
, &dinit
);
858 init
= size_binop (PLUS_EXPR
, init
, dinit
);
859 split_constant_offset (offset_iv
.base
, &offset_iv
.base
, &dinit
);
860 init
= size_binop (PLUS_EXPR
, init
, dinit
);
862 step
= size_binop (PLUS_EXPR
,
863 fold_convert (ssizetype
, base_iv
.step
),
864 fold_convert (ssizetype
, offset_iv
.step
));
866 DR_BASE_ADDRESS (dr
) = canonicalize_base_object_address (base_iv
.base
);
868 DR_OFFSET (dr
) = fold_convert (ssizetype
, offset_iv
.base
);
872 DR_ALIGNED_TO (dr
) = size_int (highest_pow2_factor (offset_iv
.base
));
874 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
875 fprintf (dump_file
, "success.\n");
880 /* Determines the base object and the list of indices of memory reference
881 DR, analyzed in LOOP and instantiated in loop nest NEST. */
884 dr_analyze_indices (struct data_reference
*dr
, loop_p nest
, loop_p loop
)
886 vec
<tree
> access_fns
= vNULL
;
888 tree base
, off
, access_fn
;
889 basic_block before_loop
;
891 /* If analyzing a basic-block there are no indices to analyze
892 and thus no access functions. */
895 DR_BASE_OBJECT (dr
) = DR_REF (dr
);
896 DR_ACCESS_FNS (dr
).create (0);
901 before_loop
= block_before_loop (nest
);
903 /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
904 into a two element array with a constant index. The base is
905 then just the immediate underlying object. */
906 if (TREE_CODE (ref
) == REALPART_EXPR
)
908 ref
= TREE_OPERAND (ref
, 0);
909 access_fns
.safe_push (integer_zero_node
);
911 else if (TREE_CODE (ref
) == IMAGPART_EXPR
)
913 ref
= TREE_OPERAND (ref
, 0);
914 access_fns
.safe_push (integer_one_node
);
917 /* Analyze access functions of dimensions we know to be independent. */
918 while (handled_component_p (ref
))
920 if (TREE_CODE (ref
) == ARRAY_REF
)
922 op
= TREE_OPERAND (ref
, 1);
923 access_fn
= analyze_scalar_evolution (loop
, op
);
924 access_fn
= instantiate_scev (before_loop
, loop
, access_fn
);
925 access_fns
.safe_push (access_fn
);
927 else if (TREE_CODE (ref
) == COMPONENT_REF
928 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref
, 0))) == RECORD_TYPE
)
930 /* For COMPONENT_REFs of records (but not unions!) use the
931 FIELD_DECL offset as constant access function so we can
932 disambiguate a[i].f1 and a[i].f2. */
933 tree off
= component_ref_field_offset (ref
);
934 off
= size_binop (PLUS_EXPR
,
935 size_binop (MULT_EXPR
,
936 fold_convert (bitsizetype
, off
),
937 bitsize_int (BITS_PER_UNIT
)),
938 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref
, 1)));
939 access_fns
.safe_push (off
);
942 /* If we have an unhandled component we could not translate
943 to an access function stop analyzing. We have determined
944 our base object in this case. */
947 ref
= TREE_OPERAND (ref
, 0);
950 /* If the address operand of a MEM_REF base has an evolution in the
951 analyzed nest, add it as an additional independent access-function. */
952 if (TREE_CODE (ref
) == MEM_REF
)
954 op
= TREE_OPERAND (ref
, 0);
955 access_fn
= analyze_scalar_evolution (loop
, op
);
956 access_fn
= instantiate_scev (before_loop
, loop
, access_fn
);
957 if (TREE_CODE (access_fn
) == POLYNOMIAL_CHREC
)
960 tree memoff
= TREE_OPERAND (ref
, 1);
961 base
= initial_condition (access_fn
);
962 orig_type
= TREE_TYPE (base
);
963 STRIP_USELESS_TYPE_CONVERSION (base
);
964 split_constant_offset (base
, &base
, &off
);
965 /* Fold the MEM_REF offset into the evolutions initial
966 value to make more bases comparable. */
967 if (!integer_zerop (memoff
))
969 off
= size_binop (PLUS_EXPR
, off
,
970 fold_convert (ssizetype
, memoff
));
971 memoff
= build_int_cst (TREE_TYPE (memoff
), 0);
973 access_fn
= chrec_replace_initial_condition
974 (access_fn
, fold_convert (orig_type
, off
));
975 /* ??? This is still not a suitable base object for
976 dr_may_alias_p - the base object needs to be an
977 access that covers the object as whole. With
978 an evolution in the pointer this cannot be
980 As a band-aid, mark the access so we can special-case
981 it in dr_may_alias_p. */
982 ref
= fold_build2_loc (EXPR_LOCATION (ref
),
983 MEM_REF
, TREE_TYPE (ref
),
985 access_fns
.safe_push (access_fn
);
988 else if (DECL_P (ref
))
990 /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
991 ref
= build2 (MEM_REF
, TREE_TYPE (ref
),
992 build_fold_addr_expr (ref
),
993 build_int_cst (reference_alias_ptr_type (ref
), 0));
996 DR_BASE_OBJECT (dr
) = ref
;
997 DR_ACCESS_FNS (dr
) = access_fns
;
1000 /* Extracts the alias analysis information from the memory reference DR. */
1003 dr_analyze_alias (struct data_reference
*dr
)
1005 tree ref
= DR_REF (dr
);
1006 tree base
= get_base_address (ref
), addr
;
1008 if (INDIRECT_REF_P (base
)
1009 || TREE_CODE (base
) == MEM_REF
)
1011 addr
= TREE_OPERAND (base
, 0);
1012 if (TREE_CODE (addr
) == SSA_NAME
)
1013 DR_PTR_INFO (dr
) = SSA_NAME_PTR_INFO (addr
);
1017 /* Frees data reference DR. */
1020 free_data_ref (data_reference_p dr
)
1022 DR_ACCESS_FNS (dr
).release ();
1026 /* Analyzes memory reference MEMREF accessed in STMT. The reference
1027 is read if IS_READ is true, write otherwise. Returns the
1028 data_reference description of MEMREF. NEST is the outermost loop
1029 in which the reference should be instantiated, LOOP is the loop in
1030 which the data reference should be analyzed. */
1032 struct data_reference
*
1033 create_data_ref (loop_p nest
, loop_p loop
, tree memref
, gimple stmt
,
1036 struct data_reference
*dr
;
1038 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1040 fprintf (dump_file
, "Creating dr for ");
1041 print_generic_expr (dump_file
, memref
, TDF_SLIM
);
1042 fprintf (dump_file
, "\n");
1045 dr
= XCNEW (struct data_reference
);
1046 DR_STMT (dr
) = stmt
;
1047 DR_REF (dr
) = memref
;
1048 DR_IS_READ (dr
) = is_read
;
1050 dr_analyze_innermost (dr
, nest
);
1051 dr_analyze_indices (dr
, nest
, loop
);
1052 dr_analyze_alias (dr
);
1054 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1057 fprintf (dump_file
, "\tbase_address: ");
1058 print_generic_expr (dump_file
, DR_BASE_ADDRESS (dr
), TDF_SLIM
);
1059 fprintf (dump_file
, "\n\toffset from base address: ");
1060 print_generic_expr (dump_file
, DR_OFFSET (dr
), TDF_SLIM
);
1061 fprintf (dump_file
, "\n\tconstant offset from base address: ");
1062 print_generic_expr (dump_file
, DR_INIT (dr
), TDF_SLIM
);
1063 fprintf (dump_file
, "\n\tstep: ");
1064 print_generic_expr (dump_file
, DR_STEP (dr
), TDF_SLIM
);
1065 fprintf (dump_file
, "\n\taligned to: ");
1066 print_generic_expr (dump_file
, DR_ALIGNED_TO (dr
), TDF_SLIM
);
1067 fprintf (dump_file
, "\n\tbase_object: ");
1068 print_generic_expr (dump_file
, DR_BASE_OBJECT (dr
), TDF_SLIM
);
1069 fprintf (dump_file
, "\n");
1070 for (i
= 0; i
< DR_NUM_DIMENSIONS (dr
); i
++)
1072 fprintf (dump_file
, "\tAccess function %d: ", i
);
1073 print_generic_stmt (dump_file
, DR_ACCESS_FN (dr
, i
), TDF_SLIM
);
1080 /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
1083 dr_equal_offsets_p1 (tree offset1
, tree offset2
)
1087 STRIP_NOPS (offset1
);
1088 STRIP_NOPS (offset2
);
1090 if (offset1
== offset2
)
1093 if (TREE_CODE (offset1
) != TREE_CODE (offset2
)
1094 || (!BINARY_CLASS_P (offset1
) && !UNARY_CLASS_P (offset1
)))
1097 res
= dr_equal_offsets_p1 (TREE_OPERAND (offset1
, 0),
1098 TREE_OPERAND (offset2
, 0));
1100 if (!res
|| !BINARY_CLASS_P (offset1
))
1103 res
= dr_equal_offsets_p1 (TREE_OPERAND (offset1
, 1),
1104 TREE_OPERAND (offset2
, 1));
1109 /* Check if DRA and DRB have equal offsets. */
1111 dr_equal_offsets_p (struct data_reference
*dra
,
1112 struct data_reference
*drb
)
1114 tree offset1
, offset2
;
1116 offset1
= DR_OFFSET (dra
);
1117 offset2
= DR_OFFSET (drb
);
1119 return dr_equal_offsets_p1 (offset1
, offset2
);
1122 /* Returns true if FNA == FNB. */
1125 affine_function_equal_p (affine_fn fna
, affine_fn fnb
)
1127 unsigned i
, n
= fna
.length ();
1129 if (n
!= fnb
.length ())
1132 for (i
= 0; i
< n
; i
++)
1133 if (!operand_equal_p (fna
[i
], fnb
[i
], 0))
1139 /* If all the functions in CF are the same, returns one of them,
1140 otherwise returns NULL. */
1143 common_affine_function (conflict_function
*cf
)
1148 if (!CF_NONTRIVIAL_P (cf
))
1149 return affine_fn ();
1153 for (i
= 1; i
< cf
->n
; i
++)
1154 if (!affine_function_equal_p (comm
, cf
->fns
[i
]))
1155 return affine_fn ();
1160 /* Returns the base of the affine function FN. */
1163 affine_function_base (affine_fn fn
)
1168 /* Returns true if FN is a constant. */
1171 affine_function_constant_p (affine_fn fn
)
1176 for (i
= 1; fn
.iterate (i
, &coef
); i
++)
1177 if (!integer_zerop (coef
))
1183 /* Returns true if FN is the zero constant function. */
1186 affine_function_zero_p (affine_fn fn
)
1188 return (integer_zerop (affine_function_base (fn
))
1189 && affine_function_constant_p (fn
));
1192 /* Returns a signed integer type with the largest precision from TA
1196 signed_type_for_types (tree ta
, tree tb
)
1198 if (TYPE_PRECISION (ta
) > TYPE_PRECISION (tb
))
1199 return signed_type_for (ta
);
1201 return signed_type_for (tb
);
1204 /* Applies operation OP on affine functions FNA and FNB, and returns the
1208 affine_fn_op (enum tree_code op
, affine_fn fna
, affine_fn fnb
)
1214 if (fnb
.length () > fna
.length ())
1226 for (i
= 0; i
< n
; i
++)
1228 tree type
= signed_type_for_types (TREE_TYPE (fna
[i
]),
1229 TREE_TYPE (fnb
[i
]));
1230 ret
.quick_push (fold_build2 (op
, type
, fna
[i
], fnb
[i
]));
1233 for (; fna
.iterate (i
, &coef
); i
++)
1234 ret
.quick_push (fold_build2 (op
, signed_type_for (TREE_TYPE (coef
)),
1235 coef
, integer_zero_node
));
1236 for (; fnb
.iterate (i
, &coef
); i
++)
1237 ret
.quick_push (fold_build2 (op
, signed_type_for (TREE_TYPE (coef
)),
1238 integer_zero_node
, coef
));
1243 /* Returns the sum of affine functions FNA and FNB. */
1246 affine_fn_plus (affine_fn fna
, affine_fn fnb
)
1248 return affine_fn_op (PLUS_EXPR
, fna
, fnb
);
1251 /* Returns the difference of affine functions FNA and FNB. */
1254 affine_fn_minus (affine_fn fna
, affine_fn fnb
)
1256 return affine_fn_op (MINUS_EXPR
, fna
, fnb
);
1259 /* Frees affine function FN. */
1262 affine_fn_free (affine_fn fn
)
1267 /* Determine for each subscript in the data dependence relation DDR
1271 compute_subscript_distance (struct data_dependence_relation
*ddr
)
1273 conflict_function
*cf_a
, *cf_b
;
1274 affine_fn fn_a
, fn_b
, diff
;
1276 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
1280 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
1282 struct subscript
*subscript
;
1284 subscript
= DDR_SUBSCRIPT (ddr
, i
);
1285 cf_a
= SUB_CONFLICTS_IN_A (subscript
);
1286 cf_b
= SUB_CONFLICTS_IN_B (subscript
);
1288 fn_a
= common_affine_function (cf_a
);
1289 fn_b
= common_affine_function (cf_b
);
1290 if (!fn_a
.exists () || !fn_b
.exists ())
1292 SUB_DISTANCE (subscript
) = chrec_dont_know
;
1295 diff
= affine_fn_minus (fn_a
, fn_b
);
1297 if (affine_function_constant_p (diff
))
1298 SUB_DISTANCE (subscript
) = affine_function_base (diff
);
1300 SUB_DISTANCE (subscript
) = chrec_dont_know
;
1302 affine_fn_free (diff
);
1307 /* Returns the conflict function for "unknown". */
1309 static conflict_function
*
1310 conflict_fn_not_known (void)
1312 conflict_function
*fn
= XCNEW (conflict_function
);
1318 /* Returns the conflict function for "independent". */
1320 static conflict_function
*
1321 conflict_fn_no_dependence (void)
1323 conflict_function
*fn
= XCNEW (conflict_function
);
1324 fn
->n
= NO_DEPENDENCE
;
1329 /* Returns true if the address of OBJ is invariant in LOOP. */
1332 object_address_invariant_in_loop_p (const struct loop
*loop
, const_tree obj
)
1334 while (handled_component_p (obj
))
1336 if (TREE_CODE (obj
) == ARRAY_REF
)
1338 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1339 need to check the stride and the lower bound of the reference. */
1340 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj
, 2),
1342 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj
, 3),
1346 else if (TREE_CODE (obj
) == COMPONENT_REF
)
1348 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj
, 2),
1352 obj
= TREE_OPERAND (obj
, 0);
1355 if (!INDIRECT_REF_P (obj
)
1356 && TREE_CODE (obj
) != MEM_REF
)
1359 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj
, 0),
1363 /* Returns false if we can prove that data references A and B do not alias,
1364 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
1368 dr_may_alias_p (const struct data_reference
*a
, const struct data_reference
*b
,
1371 tree addr_a
= DR_BASE_OBJECT (a
);
1372 tree addr_b
= DR_BASE_OBJECT (b
);
1374 /* If we are not processing a loop nest but scalar code we
1375 do not need to care about possible cross-iteration dependences
1376 and thus can process the full original reference. Do so,
1377 similar to how loop invariant motion applies extra offset-based
1381 aff_tree off1
, off2
;
1382 widest_int size1
, size2
;
1383 get_inner_reference_aff (DR_REF (a
), &off1
, &size1
);
1384 get_inner_reference_aff (DR_REF (b
), &off2
, &size2
);
1385 aff_combination_scale (&off1
, -1);
1386 aff_combination_add (&off2
, &off1
);
1387 if (aff_comb_cannot_overlap_p (&off2
, size1
, size2
))
1391 /* If we had an evolution in a pointer-based MEM_REF BASE_OBJECT we
1392 do not know the size of the base-object. So we cannot do any
1393 offset/overlap based analysis but have to rely on points-to
1394 information only. */
1395 if (TREE_CODE (addr_a
) == MEM_REF
1396 && TREE_CODE (TREE_OPERAND (addr_a
, 0)) == SSA_NAME
)
1398 /* For true dependences we can apply TBAA. */
1399 if (flag_strict_aliasing
1400 && DR_IS_WRITE (a
) && DR_IS_READ (b
)
1401 && !alias_sets_conflict_p (get_alias_set (DR_REF (a
)),
1402 get_alias_set (DR_REF (b
))))
1404 if (TREE_CODE (addr_b
) == MEM_REF
)
1405 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a
, 0),
1406 TREE_OPERAND (addr_b
, 0));
1408 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a
, 0),
1409 build_fold_addr_expr (addr_b
));
1411 else if (TREE_CODE (addr_b
) == MEM_REF
1412 && TREE_CODE (TREE_OPERAND (addr_b
, 0)) == SSA_NAME
)
1414 /* For true dependences we can apply TBAA. */
1415 if (flag_strict_aliasing
1416 && DR_IS_WRITE (a
) && DR_IS_READ (b
)
1417 && !alias_sets_conflict_p (get_alias_set (DR_REF (a
)),
1418 get_alias_set (DR_REF (b
))))
1420 if (TREE_CODE (addr_a
) == MEM_REF
)
1421 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a
, 0),
1422 TREE_OPERAND (addr_b
, 0));
1424 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a
),
1425 TREE_OPERAND (addr_b
, 0));
1428 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1429 that is being subsetted in the loop nest. */
1430 if (DR_IS_WRITE (a
) && DR_IS_WRITE (b
))
1431 return refs_output_dependent_p (addr_a
, addr_b
);
1432 else if (DR_IS_READ (a
) && DR_IS_WRITE (b
))
1433 return refs_anti_dependent_p (addr_a
, addr_b
);
1434 return refs_may_alias_p (addr_a
, addr_b
);
1437 /* Initialize a data dependence relation between data accesses A and
1438 B. NB_LOOPS is the number of loops surrounding the references: the
1439 size of the classic distance/direction vectors. */
1441 struct data_dependence_relation
*
1442 initialize_data_dependence_relation (struct data_reference
*a
,
1443 struct data_reference
*b
,
1444 vec
<loop_p
> loop_nest
)
1446 struct data_dependence_relation
*res
;
1449 res
= XNEW (struct data_dependence_relation
);
1452 DDR_LOOP_NEST (res
).create (0);
1453 DDR_REVERSED_P (res
) = false;
1454 DDR_SUBSCRIPTS (res
).create (0);
1455 DDR_DIR_VECTS (res
).create (0);
1456 DDR_DIST_VECTS (res
).create (0);
1458 if (a
== NULL
|| b
== NULL
)
1460 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
1464 /* If the data references do not alias, then they are independent. */
1465 if (!dr_may_alias_p (a
, b
, loop_nest
.exists ()))
1467 DDR_ARE_DEPENDENT (res
) = chrec_known
;
1471 /* The case where the references are exactly the same. */
1472 if (operand_equal_p (DR_REF (a
), DR_REF (b
), 0))
1474 if (loop_nest
.exists ()
1475 && !object_address_invariant_in_loop_p (loop_nest
[0],
1476 DR_BASE_OBJECT (a
)))
1478 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
1481 DDR_AFFINE_P (res
) = true;
1482 DDR_ARE_DEPENDENT (res
) = NULL_TREE
;
1483 DDR_SUBSCRIPTS (res
).create (DR_NUM_DIMENSIONS (a
));
1484 DDR_LOOP_NEST (res
) = loop_nest
;
1485 DDR_INNER_LOOP (res
) = 0;
1486 DDR_SELF_REFERENCE (res
) = true;
1487 for (i
= 0; i
< DR_NUM_DIMENSIONS (a
); i
++)
1489 struct subscript
*subscript
;
1491 subscript
= XNEW (struct subscript
);
1492 SUB_CONFLICTS_IN_A (subscript
) = conflict_fn_not_known ();
1493 SUB_CONFLICTS_IN_B (subscript
) = conflict_fn_not_known ();
1494 SUB_LAST_CONFLICT (subscript
) = chrec_dont_know
;
1495 SUB_DISTANCE (subscript
) = chrec_dont_know
;
1496 DDR_SUBSCRIPTS (res
).safe_push (subscript
);
1501 /* If the references do not access the same object, we do not know
1502 whether they alias or not. */
1503 if (!operand_equal_p (DR_BASE_OBJECT (a
), DR_BASE_OBJECT (b
), 0))
1505 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
1509 /* If the base of the object is not invariant in the loop nest, we cannot
1510 analyze it. TODO -- in fact, it would suffice to record that there may
1511 be arbitrary dependences in the loops where the base object varies. */
1512 if (loop_nest
.exists ()
1513 && !object_address_invariant_in_loop_p (loop_nest
[0],
1514 DR_BASE_OBJECT (a
)))
1516 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
1520 /* If the number of dimensions of the access to not agree we can have
1521 a pointer access to a component of the array element type and an
1522 array access while the base-objects are still the same. Punt. */
1523 if (DR_NUM_DIMENSIONS (a
) != DR_NUM_DIMENSIONS (b
))
1525 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
1529 DDR_AFFINE_P (res
) = true;
1530 DDR_ARE_DEPENDENT (res
) = NULL_TREE
;
1531 DDR_SUBSCRIPTS (res
).create (DR_NUM_DIMENSIONS (a
));
1532 DDR_LOOP_NEST (res
) = loop_nest
;
1533 DDR_INNER_LOOP (res
) = 0;
1534 DDR_SELF_REFERENCE (res
) = false;
1536 for (i
= 0; i
< DR_NUM_DIMENSIONS (a
); i
++)
1538 struct subscript
*subscript
;
1540 subscript
= XNEW (struct subscript
);
1541 SUB_CONFLICTS_IN_A (subscript
) = conflict_fn_not_known ();
1542 SUB_CONFLICTS_IN_B (subscript
) = conflict_fn_not_known ();
1543 SUB_LAST_CONFLICT (subscript
) = chrec_dont_know
;
1544 SUB_DISTANCE (subscript
) = chrec_dont_know
;
1545 DDR_SUBSCRIPTS (res
).safe_push (subscript
);
1551 /* Frees memory used by the conflict function F. */
1554 free_conflict_function (conflict_function
*f
)
1558 if (CF_NONTRIVIAL_P (f
))
1560 for (i
= 0; i
< f
->n
; i
++)
1561 affine_fn_free (f
->fns
[i
]);
1566 /* Frees memory used by SUBSCRIPTS. */
1569 free_subscripts (vec
<subscript_p
> subscripts
)
1574 FOR_EACH_VEC_ELT (subscripts
, i
, s
)
1576 free_conflict_function (s
->conflicting_iterations_in_a
);
1577 free_conflict_function (s
->conflicting_iterations_in_b
);
1580 subscripts
.release ();
1583 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1587 finalize_ddr_dependent (struct data_dependence_relation
*ddr
,
1590 DDR_ARE_DEPENDENT (ddr
) = chrec
;
1591 free_subscripts (DDR_SUBSCRIPTS (ddr
));
1592 DDR_SUBSCRIPTS (ddr
).create (0);
1595 /* The dependence relation DDR cannot be represented by a distance
1599 non_affine_dependence_relation (struct data_dependence_relation
*ddr
)
1601 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1602 fprintf (dump_file
, "(Dependence relation cannot be represented by distance vector.) \n");
1604 DDR_AFFINE_P (ddr
) = false;
1609 /* This section contains the classic Banerjee tests. */
1611 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1612 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1615 ziv_subscript_p (const_tree chrec_a
, const_tree chrec_b
)
1617 return (evolution_function_is_constant_p (chrec_a
)
1618 && evolution_function_is_constant_p (chrec_b
));
1621 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
1622 variable, i.e., if the SIV (Single Index Variable) test is true. */
1625 siv_subscript_p (const_tree chrec_a
, const_tree chrec_b
)
1627 if ((evolution_function_is_constant_p (chrec_a
)
1628 && evolution_function_is_univariate_p (chrec_b
))
1629 || (evolution_function_is_constant_p (chrec_b
)
1630 && evolution_function_is_univariate_p (chrec_a
)))
1633 if (evolution_function_is_univariate_p (chrec_a
)
1634 && evolution_function_is_univariate_p (chrec_b
))
1636 switch (TREE_CODE (chrec_a
))
1638 case POLYNOMIAL_CHREC
:
1639 switch (TREE_CODE (chrec_b
))
1641 case POLYNOMIAL_CHREC
:
1642 if (CHREC_VARIABLE (chrec_a
) != CHREC_VARIABLE (chrec_b
))
1657 /* Creates a conflict function with N dimensions. The affine functions
1658 in each dimension follow. */
1660 static conflict_function
*
1661 conflict_fn (unsigned n
, ...)
1664 conflict_function
*ret
= XCNEW (conflict_function
);
1667 gcc_assert (0 < n
&& n
<= MAX_DIM
);
1671 for (i
= 0; i
< n
; i
++)
1672 ret
->fns
[i
] = va_arg (ap
, affine_fn
);
1678 /* Returns constant affine function with value CST. */
1681 affine_fn_cst (tree cst
)
1685 fn
.quick_push (cst
);
1689 /* Returns affine function with single variable, CST + COEF * x_DIM. */
1692 affine_fn_univar (tree cst
, unsigned dim
, tree coef
)
1695 fn
.create (dim
+ 1);
1698 gcc_assert (dim
> 0);
1699 fn
.quick_push (cst
);
1700 for (i
= 1; i
< dim
; i
++)
1701 fn
.quick_push (integer_zero_node
);
1702 fn
.quick_push (coef
);
1706 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1707 *OVERLAPS_B are initialized to the functions that describe the
1708 relation between the elements accessed twice by CHREC_A and
1709 CHREC_B. For k >= 0, the following property is verified:
1711 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1714 analyze_ziv_subscript (tree chrec_a
,
1716 conflict_function
**overlaps_a
,
1717 conflict_function
**overlaps_b
,
1718 tree
*last_conflicts
)
1720 tree type
, difference
;
1721 dependence_stats
.num_ziv
++;
1723 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1724 fprintf (dump_file
, "(analyze_ziv_subscript \n");
1726 type
= signed_type_for_types (TREE_TYPE (chrec_a
), TREE_TYPE (chrec_b
));
1727 chrec_a
= chrec_convert (type
, chrec_a
, NULL
);
1728 chrec_b
= chrec_convert (type
, chrec_b
, NULL
);
1729 difference
= chrec_fold_minus (type
, chrec_a
, chrec_b
);
1731 switch (TREE_CODE (difference
))
1734 if (integer_zerop (difference
))
1736 /* The difference is equal to zero: the accessed index
1737 overlaps for each iteration in the loop. */
1738 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1739 *overlaps_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1740 *last_conflicts
= chrec_dont_know
;
1741 dependence_stats
.num_ziv_dependent
++;
1745 /* The accesses do not overlap. */
1746 *overlaps_a
= conflict_fn_no_dependence ();
1747 *overlaps_b
= conflict_fn_no_dependence ();
1748 *last_conflicts
= integer_zero_node
;
1749 dependence_stats
.num_ziv_independent
++;
1754 /* We're not sure whether the indexes overlap. For the moment,
1755 conservatively answer "don't know". */
1756 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1757 fprintf (dump_file
, "ziv test failed: difference is non-integer.\n");
1759 *overlaps_a
= conflict_fn_not_known ();
1760 *overlaps_b
= conflict_fn_not_known ();
1761 *last_conflicts
= chrec_dont_know
;
1762 dependence_stats
.num_ziv_unimplemented
++;
1766 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1767 fprintf (dump_file
, ")\n");
1770 /* Similar to max_stmt_executions_int, but returns the bound as a tree,
1771 and only if it fits to the int type. If this is not the case, or the
1772 bound on the number of iterations of LOOP could not be derived, returns
1776 max_stmt_executions_tree (struct loop
*loop
)
1780 if (!max_stmt_executions (loop
, &nit
))
1781 return chrec_dont_know
;
1783 if (!wi::fits_to_tree_p (nit
, unsigned_type_node
))
1784 return chrec_dont_know
;
1786 return wide_int_to_tree (unsigned_type_node
, nit
);
1789 /* Determine whether the CHREC is always positive/negative. If the expression
1790 cannot be statically analyzed, return false, otherwise set the answer into
1794 chrec_is_positive (tree chrec
, bool *value
)
1796 bool value0
, value1
, value2
;
1797 tree end_value
, nb_iter
;
1799 switch (TREE_CODE (chrec
))
1801 case POLYNOMIAL_CHREC
:
1802 if (!chrec_is_positive (CHREC_LEFT (chrec
), &value0
)
1803 || !chrec_is_positive (CHREC_RIGHT (chrec
), &value1
))
1806 /* FIXME -- overflows. */
1807 if (value0
== value1
)
1813 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1814 and the proof consists in showing that the sign never
1815 changes during the execution of the loop, from 0 to
1816 loop->nb_iterations. */
1817 if (!evolution_function_is_affine_p (chrec
))
1820 nb_iter
= number_of_latch_executions (get_chrec_loop (chrec
));
1821 if (chrec_contains_undetermined (nb_iter
))
1825 /* TODO -- If the test is after the exit, we may decrease the number of
1826 iterations by one. */
1828 nb_iter
= chrec_fold_minus (type
, nb_iter
, build_int_cst (type
, 1));
1831 end_value
= chrec_apply (CHREC_VARIABLE (chrec
), chrec
, nb_iter
);
1833 if (!chrec_is_positive (end_value
, &value2
))
1837 return value0
== value1
;
1840 switch (tree_int_cst_sgn (chrec
))
1859 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1860 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1861 *OVERLAPS_B are initialized to the functions that describe the
1862 relation between the elements accessed twice by CHREC_A and
1863 CHREC_B. For k >= 0, the following property is verified:
1865 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1868 analyze_siv_subscript_cst_affine (tree chrec_a
,
1870 conflict_function
**overlaps_a
,
1871 conflict_function
**overlaps_b
,
1872 tree
*last_conflicts
)
1874 bool value0
, value1
, value2
;
1875 tree type
, difference
, tmp
;
1877 type
= signed_type_for_types (TREE_TYPE (chrec_a
), TREE_TYPE (chrec_b
));
1878 chrec_a
= chrec_convert (type
, chrec_a
, NULL
);
1879 chrec_b
= chrec_convert (type
, chrec_b
, NULL
);
1880 difference
= chrec_fold_minus (type
, initial_condition (chrec_b
), chrec_a
);
1882 /* Special case overlap in the first iteration. */
1883 if (integer_zerop (difference
))
1885 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1886 *overlaps_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1887 *last_conflicts
= integer_one_node
;
1891 if (!chrec_is_positive (initial_condition (difference
), &value0
))
1893 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1894 fprintf (dump_file
, "siv test failed: chrec is not positive.\n");
1896 dependence_stats
.num_siv_unimplemented
++;
1897 *overlaps_a
= conflict_fn_not_known ();
1898 *overlaps_b
= conflict_fn_not_known ();
1899 *last_conflicts
= chrec_dont_know
;
1904 if (value0
== false)
1906 if (!chrec_is_positive (CHREC_RIGHT (chrec_b
), &value1
))
1908 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1909 fprintf (dump_file
, "siv test failed: chrec not positive.\n");
1911 *overlaps_a
= conflict_fn_not_known ();
1912 *overlaps_b
= conflict_fn_not_known ();
1913 *last_conflicts
= chrec_dont_know
;
1914 dependence_stats
.num_siv_unimplemented
++;
1923 chrec_b = {10, +, 1}
1926 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b
), difference
))
1928 HOST_WIDE_INT numiter
;
1929 struct loop
*loop
= get_chrec_loop (chrec_b
);
1931 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1932 tmp
= fold_build2 (EXACT_DIV_EXPR
, type
,
1933 fold_build1 (ABS_EXPR
, type
, difference
),
1934 CHREC_RIGHT (chrec_b
));
1935 *overlaps_b
= conflict_fn (1, affine_fn_cst (tmp
));
1936 *last_conflicts
= integer_one_node
;
1939 /* Perform weak-zero siv test to see if overlap is
1940 outside the loop bounds. */
1941 numiter
= max_stmt_executions_int (loop
);
1944 && compare_tree_int (tmp
, numiter
) > 0)
1946 free_conflict_function (*overlaps_a
);
1947 free_conflict_function (*overlaps_b
);
1948 *overlaps_a
= conflict_fn_no_dependence ();
1949 *overlaps_b
= conflict_fn_no_dependence ();
1950 *last_conflicts
= integer_zero_node
;
1951 dependence_stats
.num_siv_independent
++;
1954 dependence_stats
.num_siv_dependent
++;
1958 /* When the step does not divide the difference, there are
1962 *overlaps_a
= conflict_fn_no_dependence ();
1963 *overlaps_b
= conflict_fn_no_dependence ();
1964 *last_conflicts
= integer_zero_node
;
1965 dependence_stats
.num_siv_independent
++;
1974 chrec_b = {10, +, -1}
1976 In this case, chrec_a will not overlap with chrec_b. */
1977 *overlaps_a
= conflict_fn_no_dependence ();
1978 *overlaps_b
= conflict_fn_no_dependence ();
1979 *last_conflicts
= integer_zero_node
;
1980 dependence_stats
.num_siv_independent
++;
1987 if (!chrec_is_positive (CHREC_RIGHT (chrec_b
), &value2
))
1989 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1990 fprintf (dump_file
, "siv test failed: chrec not positive.\n");
1992 *overlaps_a
= conflict_fn_not_known ();
1993 *overlaps_b
= conflict_fn_not_known ();
1994 *last_conflicts
= chrec_dont_know
;
1995 dependence_stats
.num_siv_unimplemented
++;
2000 if (value2
== false)
2004 chrec_b = {10, +, -1}
2006 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b
), difference
))
2008 HOST_WIDE_INT numiter
;
2009 struct loop
*loop
= get_chrec_loop (chrec_b
);
2011 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2012 tmp
= fold_build2 (EXACT_DIV_EXPR
, type
, difference
,
2013 CHREC_RIGHT (chrec_b
));
2014 *overlaps_b
= conflict_fn (1, affine_fn_cst (tmp
));
2015 *last_conflicts
= integer_one_node
;
2017 /* Perform weak-zero siv test to see if overlap is
2018 outside the loop bounds. */
2019 numiter
= max_stmt_executions_int (loop
);
2022 && compare_tree_int (tmp
, numiter
) > 0)
2024 free_conflict_function (*overlaps_a
);
2025 free_conflict_function (*overlaps_b
);
2026 *overlaps_a
= conflict_fn_no_dependence ();
2027 *overlaps_b
= conflict_fn_no_dependence ();
2028 *last_conflicts
= integer_zero_node
;
2029 dependence_stats
.num_siv_independent
++;
2032 dependence_stats
.num_siv_dependent
++;
2036 /* When the step does not divide the difference, there
2040 *overlaps_a
= conflict_fn_no_dependence ();
2041 *overlaps_b
= conflict_fn_no_dependence ();
2042 *last_conflicts
= integer_zero_node
;
2043 dependence_stats
.num_siv_independent
++;
2053 In this case, chrec_a will not overlap with chrec_b. */
2054 *overlaps_a
= conflict_fn_no_dependence ();
2055 *overlaps_b
= conflict_fn_no_dependence ();
2056 *last_conflicts
= integer_zero_node
;
2057 dependence_stats
.num_siv_independent
++;
2065 /* Helper recursive function for initializing the matrix A. Returns
2066 the initial value of CHREC. */
2069 initialize_matrix_A (lambda_matrix A
, tree chrec
, unsigned index
, int mult
)
2073 switch (TREE_CODE (chrec
))
2075 case POLYNOMIAL_CHREC
:
2076 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec
)) == INTEGER_CST
);
2078 A
[index
][0] = mult
* int_cst_value (CHREC_RIGHT (chrec
));
2079 return initialize_matrix_A (A
, CHREC_LEFT (chrec
), index
+ 1, mult
);
2085 tree op0
= initialize_matrix_A (A
, TREE_OPERAND (chrec
, 0), index
, mult
);
2086 tree op1
= initialize_matrix_A (A
, TREE_OPERAND (chrec
, 1), index
, mult
);
2088 return chrec_fold_op (TREE_CODE (chrec
), chrec_type (chrec
), op0
, op1
);
2093 tree op
= initialize_matrix_A (A
, TREE_OPERAND (chrec
, 0), index
, mult
);
2094 return chrec_convert (chrec_type (chrec
), op
, NULL
);
2099 /* Handle ~X as -1 - X. */
2100 tree op
= initialize_matrix_A (A
, TREE_OPERAND (chrec
, 0), index
, mult
);
2101 return chrec_fold_op (MINUS_EXPR
, chrec_type (chrec
),
2102 build_int_cst (TREE_TYPE (chrec
), -1), op
);
2114 #define FLOOR_DIV(x,y) ((x) / (y))
2116 /* Solves the special case of the Diophantine equation:
2117 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2119 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2120 number of iterations that loops X and Y run. The overlaps will be
2121 constructed as evolutions in dimension DIM. */
2124 compute_overlap_steps_for_affine_univar (int niter
, int step_a
, int step_b
,
2125 affine_fn
*overlaps_a
,
2126 affine_fn
*overlaps_b
,
2127 tree
*last_conflicts
, int dim
)
2129 if (((step_a
> 0 && step_b
> 0)
2130 || (step_a
< 0 && step_b
< 0)))
2132 int step_overlaps_a
, step_overlaps_b
;
2133 int gcd_steps_a_b
, last_conflict
, tau2
;
2135 gcd_steps_a_b
= gcd (step_a
, step_b
);
2136 step_overlaps_a
= step_b
/ gcd_steps_a_b
;
2137 step_overlaps_b
= step_a
/ gcd_steps_a_b
;
2141 tau2
= FLOOR_DIV (niter
, step_overlaps_a
);
2142 tau2
= MIN (tau2
, FLOOR_DIV (niter
, step_overlaps_b
));
2143 last_conflict
= tau2
;
2144 *last_conflicts
= build_int_cst (NULL_TREE
, last_conflict
);
2147 *last_conflicts
= chrec_dont_know
;
2149 *overlaps_a
= affine_fn_univar (integer_zero_node
, dim
,
2150 build_int_cst (NULL_TREE
,
2152 *overlaps_b
= affine_fn_univar (integer_zero_node
, dim
,
2153 build_int_cst (NULL_TREE
,
2159 *overlaps_a
= affine_fn_cst (integer_zero_node
);
2160 *overlaps_b
= affine_fn_cst (integer_zero_node
);
2161 *last_conflicts
= integer_zero_node
;
2165 /* Solves the special case of a Diophantine equation where CHREC_A is
2166 an affine bivariate function, and CHREC_B is an affine univariate
2167 function. For example,
2169 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2171 has the following overlapping functions:
2173 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2174 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2175 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2177 FORNOW: This is a specialized implementation for a case occurring in
2178 a common benchmark. Implement the general algorithm. */
2181 compute_overlap_steps_for_affine_1_2 (tree chrec_a
, tree chrec_b
,
2182 conflict_function
**overlaps_a
,
2183 conflict_function
**overlaps_b
,
2184 tree
*last_conflicts
)
2186 bool xz_p
, yz_p
, xyz_p
;
2187 int step_x
, step_y
, step_z
;
2188 HOST_WIDE_INT niter_x
, niter_y
, niter_z
, niter
;
2189 affine_fn overlaps_a_xz
, overlaps_b_xz
;
2190 affine_fn overlaps_a_yz
, overlaps_b_yz
;
2191 affine_fn overlaps_a_xyz
, overlaps_b_xyz
;
2192 affine_fn ova1
, ova2
, ovb
;
2193 tree last_conflicts_xz
, last_conflicts_yz
, last_conflicts_xyz
;
2195 step_x
= int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a
)));
2196 step_y
= int_cst_value (CHREC_RIGHT (chrec_a
));
2197 step_z
= int_cst_value (CHREC_RIGHT (chrec_b
));
2199 niter_x
= max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a
)));
2200 niter_y
= max_stmt_executions_int (get_chrec_loop (chrec_a
));
2201 niter_z
= max_stmt_executions_int (get_chrec_loop (chrec_b
));
2203 if (niter_x
< 0 || niter_y
< 0 || niter_z
< 0)
2205 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2206 fprintf (dump_file
, "overlap steps test failed: no iteration counts.\n");
2208 *overlaps_a
= conflict_fn_not_known ();
2209 *overlaps_b
= conflict_fn_not_known ();
2210 *last_conflicts
= chrec_dont_know
;
2214 niter
= MIN (niter_x
, niter_z
);
2215 compute_overlap_steps_for_affine_univar (niter
, step_x
, step_z
,
2218 &last_conflicts_xz
, 1);
2219 niter
= MIN (niter_y
, niter_z
);
2220 compute_overlap_steps_for_affine_univar (niter
, step_y
, step_z
,
2223 &last_conflicts_yz
, 2);
2224 niter
= MIN (niter_x
, niter_z
);
2225 niter
= MIN (niter_y
, niter
);
2226 compute_overlap_steps_for_affine_univar (niter
, step_x
+ step_y
, step_z
,
2229 &last_conflicts_xyz
, 3);
2231 xz_p
= !integer_zerop (last_conflicts_xz
);
2232 yz_p
= !integer_zerop (last_conflicts_yz
);
2233 xyz_p
= !integer_zerop (last_conflicts_xyz
);
2235 if (xz_p
|| yz_p
|| xyz_p
)
2237 ova1
= affine_fn_cst (integer_zero_node
);
2238 ova2
= affine_fn_cst (integer_zero_node
);
2239 ovb
= affine_fn_cst (integer_zero_node
);
2242 affine_fn t0
= ova1
;
2245 ova1
= affine_fn_plus (ova1
, overlaps_a_xz
);
2246 ovb
= affine_fn_plus (ovb
, overlaps_b_xz
);
2247 affine_fn_free (t0
);
2248 affine_fn_free (t2
);
2249 *last_conflicts
= last_conflicts_xz
;
2253 affine_fn t0
= ova2
;
2256 ova2
= affine_fn_plus (ova2
, overlaps_a_yz
);
2257 ovb
= affine_fn_plus (ovb
, overlaps_b_yz
);
2258 affine_fn_free (t0
);
2259 affine_fn_free (t2
);
2260 *last_conflicts
= last_conflicts_yz
;
2264 affine_fn t0
= ova1
;
2265 affine_fn t2
= ova2
;
2268 ova1
= affine_fn_plus (ova1
, overlaps_a_xyz
);
2269 ova2
= affine_fn_plus (ova2
, overlaps_a_xyz
);
2270 ovb
= affine_fn_plus (ovb
, overlaps_b_xyz
);
2271 affine_fn_free (t0
);
2272 affine_fn_free (t2
);
2273 affine_fn_free (t4
);
2274 *last_conflicts
= last_conflicts_xyz
;
2276 *overlaps_a
= conflict_fn (2, ova1
, ova2
);
2277 *overlaps_b
= conflict_fn (1, ovb
);
2281 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2282 *overlaps_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2283 *last_conflicts
= integer_zero_node
;
2286 affine_fn_free (overlaps_a_xz
);
2287 affine_fn_free (overlaps_b_xz
);
2288 affine_fn_free (overlaps_a_yz
);
2289 affine_fn_free (overlaps_b_yz
);
2290 affine_fn_free (overlaps_a_xyz
);
2291 affine_fn_free (overlaps_b_xyz
);
2294 /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2297 lambda_vector_copy (lambda_vector vec1
, lambda_vector vec2
,
2300 memcpy (vec2
, vec1
, size
* sizeof (*vec1
));
2303 /* Copy the elements of M x N matrix MAT1 to MAT2. */
2306 lambda_matrix_copy (lambda_matrix mat1
, lambda_matrix mat2
,
2311 for (i
= 0; i
< m
; i
++)
2312 lambda_vector_copy (mat1
[i
], mat2
[i
], n
);
2315 /* Store the N x N identity matrix in MAT. */
2318 lambda_matrix_id (lambda_matrix mat
, int size
)
2322 for (i
= 0; i
< size
; i
++)
2323 for (j
= 0; j
< size
; j
++)
2324 mat
[i
][j
] = (i
== j
) ? 1 : 0;
2327 /* Return the first nonzero element of vector VEC1 between START and N.
2328 We must have START <= N. Returns N if VEC1 is the zero vector. */
2331 lambda_vector_first_nz (lambda_vector vec1
, int n
, int start
)
2334 while (j
< n
&& vec1
[j
] == 0)
2339 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2340 R2 = R2 + CONST1 * R1. */
2343 lambda_matrix_row_add (lambda_matrix mat
, int n
, int r1
, int r2
, int const1
)
2350 for (i
= 0; i
< n
; i
++)
2351 mat
[r2
][i
] += const1
* mat
[r1
][i
];
2354 /* Swap rows R1 and R2 in matrix MAT. */
2357 lambda_matrix_row_exchange (lambda_matrix mat
, int r1
, int r2
)
2366 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
2367 and store the result in VEC2. */
2370 lambda_vector_mult_const (lambda_vector vec1
, lambda_vector vec2
,
2371 int size
, int const1
)
2376 lambda_vector_clear (vec2
, size
);
2378 for (i
= 0; i
< size
; i
++)
2379 vec2
[i
] = const1
* vec1
[i
];
2382 /* Negate vector VEC1 with length SIZE and store it in VEC2. */
2385 lambda_vector_negate (lambda_vector vec1
, lambda_vector vec2
,
2388 lambda_vector_mult_const (vec1
, vec2
, size
, -1);
2391 /* Negate row R1 of matrix MAT which has N columns. */
2394 lambda_matrix_row_negate (lambda_matrix mat
, int n
, int r1
)
2396 lambda_vector_negate (mat
[r1
], mat
[r1
], n
);
2399 /* Return true if two vectors are equal. */
2402 lambda_vector_equal (lambda_vector vec1
, lambda_vector vec2
, int size
)
2405 for (i
= 0; i
< size
; i
++)
2406 if (vec1
[i
] != vec2
[i
])
2411 /* Given an M x N integer matrix A, this function determines an M x
2412 M unimodular matrix U, and an M x N echelon matrix S such that
2413 "U.A = S". This decomposition is also known as "right Hermite".
2415 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2416 Restructuring Compilers" Utpal Banerjee. */
2419 lambda_matrix_right_hermite (lambda_matrix A
, int m
, int n
,
2420 lambda_matrix S
, lambda_matrix U
)
2424 lambda_matrix_copy (A
, S
, m
, n
);
2425 lambda_matrix_id (U
, m
);
2427 for (j
= 0; j
< n
; j
++)
2429 if (lambda_vector_first_nz (S
[j
], m
, i0
) < m
)
2432 for (i
= m
- 1; i
>= i0
; i
--)
2434 while (S
[i
][j
] != 0)
2436 int sigma
, factor
, a
, b
;
2440 sigma
= (a
* b
< 0) ? -1: 1;
2443 factor
= sigma
* (a
/ b
);
2445 lambda_matrix_row_add (S
, n
, i
, i
-1, -factor
);
2446 lambda_matrix_row_exchange (S
, i
, i
-1);
2448 lambda_matrix_row_add (U
, m
, i
, i
-1, -factor
);
2449 lambda_matrix_row_exchange (U
, i
, i
-1);
2456 /* Determines the overlapping elements due to accesses CHREC_A and
2457 CHREC_B, that are affine functions. This function cannot handle
2458 symbolic evolution functions, ie. when initial conditions are
2459 parameters, because it uses lambda matrices of integers. */
2462 analyze_subscript_affine_affine (tree chrec_a
,
2464 conflict_function
**overlaps_a
,
2465 conflict_function
**overlaps_b
,
2466 tree
*last_conflicts
)
2468 unsigned nb_vars_a
, nb_vars_b
, dim
;
2469 HOST_WIDE_INT init_a
, init_b
, gamma
, gcd_alpha_beta
;
2470 lambda_matrix A
, U
, S
;
2471 struct obstack scratch_obstack
;
2473 if (eq_evolutions_p (chrec_a
, chrec_b
))
2475 /* The accessed index overlaps for each iteration in the
2477 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2478 *overlaps_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2479 *last_conflicts
= chrec_dont_know
;
2482 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2483 fprintf (dump_file
, "(analyze_subscript_affine_affine \n");
2485 /* For determining the initial intersection, we have to solve a
2486 Diophantine equation. This is the most time consuming part.
2488 For answering to the question: "Is there a dependence?" we have
2489 to prove that there exists a solution to the Diophantine
2490 equation, and that the solution is in the iteration domain,
2491 i.e. the solution is positive or zero, and that the solution
2492 happens before the upper bound loop.nb_iterations. Otherwise
2493 there is no dependence. This function outputs a description of
2494 the iterations that hold the intersections. */
2496 nb_vars_a
= nb_vars_in_chrec (chrec_a
);
2497 nb_vars_b
= nb_vars_in_chrec (chrec_b
);
2499 gcc_obstack_init (&scratch_obstack
);
2501 dim
= nb_vars_a
+ nb_vars_b
;
2502 U
= lambda_matrix_new (dim
, dim
, &scratch_obstack
);
2503 A
= lambda_matrix_new (dim
, 1, &scratch_obstack
);
2504 S
= lambda_matrix_new (dim
, 1, &scratch_obstack
);
2506 init_a
= int_cst_value (initialize_matrix_A (A
, chrec_a
, 0, 1));
2507 init_b
= int_cst_value (initialize_matrix_A (A
, chrec_b
, nb_vars_a
, -1));
2508 gamma
= init_b
- init_a
;
2510 /* Don't do all the hard work of solving the Diophantine equation
2511 when we already know the solution: for example,
2514 | gamma = 3 - 3 = 0.
2515 Then the first overlap occurs during the first iterations:
2516 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2520 if (nb_vars_a
== 1 && nb_vars_b
== 1)
2522 HOST_WIDE_INT step_a
, step_b
;
2523 HOST_WIDE_INT niter
, niter_a
, niter_b
;
2526 niter_a
= max_stmt_executions_int (get_chrec_loop (chrec_a
));
2527 niter_b
= max_stmt_executions_int (get_chrec_loop (chrec_b
));
2528 niter
= MIN (niter_a
, niter_b
);
2529 step_a
= int_cst_value (CHREC_RIGHT (chrec_a
));
2530 step_b
= int_cst_value (CHREC_RIGHT (chrec_b
));
2532 compute_overlap_steps_for_affine_univar (niter
, step_a
, step_b
,
2535 *overlaps_a
= conflict_fn (1, ova
);
2536 *overlaps_b
= conflict_fn (1, ovb
);
2539 else if (nb_vars_a
== 2 && nb_vars_b
== 1)
2540 compute_overlap_steps_for_affine_1_2
2541 (chrec_a
, chrec_b
, overlaps_a
, overlaps_b
, last_conflicts
);
2543 else if (nb_vars_a
== 1 && nb_vars_b
== 2)
2544 compute_overlap_steps_for_affine_1_2
2545 (chrec_b
, chrec_a
, overlaps_b
, overlaps_a
, last_conflicts
);
2549 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2550 fprintf (dump_file
, "affine-affine test failed: too many variables.\n");
2551 *overlaps_a
= conflict_fn_not_known ();
2552 *overlaps_b
= conflict_fn_not_known ();
2553 *last_conflicts
= chrec_dont_know
;
2555 goto end_analyze_subs_aa
;
2559 lambda_matrix_right_hermite (A
, dim
, 1, S
, U
);
2564 lambda_matrix_row_negate (U
, dim
, 0);
2566 gcd_alpha_beta
= S
[0][0];
2568 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2569 but that is a quite strange case. Instead of ICEing, answer
2571 if (gcd_alpha_beta
== 0)
2573 *overlaps_a
= conflict_fn_not_known ();
2574 *overlaps_b
= conflict_fn_not_known ();
2575 *last_conflicts
= chrec_dont_know
;
2576 goto end_analyze_subs_aa
;
2579 /* The classic "gcd-test". */
2580 if (!int_divides_p (gcd_alpha_beta
, gamma
))
2582 /* The "gcd-test" has determined that there is no integer
2583 solution, i.e. there is no dependence. */
2584 *overlaps_a
= conflict_fn_no_dependence ();
2585 *overlaps_b
= conflict_fn_no_dependence ();
2586 *last_conflicts
= integer_zero_node
;
2589 /* Both access functions are univariate. This includes SIV and MIV cases. */
2590 else if (nb_vars_a
== 1 && nb_vars_b
== 1)
2592 /* Both functions should have the same evolution sign. */
2593 if (((A
[0][0] > 0 && -A
[1][0] > 0)
2594 || (A
[0][0] < 0 && -A
[1][0] < 0)))
2596 /* The solutions are given by:
2598 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2601 For a given integer t. Using the following variables,
2603 | i0 = u11 * gamma / gcd_alpha_beta
2604 | j0 = u12 * gamma / gcd_alpha_beta
2611 | y0 = j0 + j1 * t. */
2612 HOST_WIDE_INT i0
, j0
, i1
, j1
;
2614 i0
= U
[0][0] * gamma
/ gcd_alpha_beta
;
2615 j0
= U
[0][1] * gamma
/ gcd_alpha_beta
;
2619 if ((i1
== 0 && i0
< 0)
2620 || (j1
== 0 && j0
< 0))
2622 /* There is no solution.
2623 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2624 falls in here, but for the moment we don't look at the
2625 upper bound of the iteration domain. */
2626 *overlaps_a
= conflict_fn_no_dependence ();
2627 *overlaps_b
= conflict_fn_no_dependence ();
2628 *last_conflicts
= integer_zero_node
;
2629 goto end_analyze_subs_aa
;
2632 if (i1
> 0 && j1
> 0)
2634 HOST_WIDE_INT niter_a
2635 = max_stmt_executions_int (get_chrec_loop (chrec_a
));
2636 HOST_WIDE_INT niter_b
2637 = max_stmt_executions_int (get_chrec_loop (chrec_b
));
2638 HOST_WIDE_INT niter
= MIN (niter_a
, niter_b
);
2640 /* (X0, Y0) is a solution of the Diophantine equation:
2641 "chrec_a (X0) = chrec_b (Y0)". */
2642 HOST_WIDE_INT tau1
= MAX (CEIL (-i0
, i1
),
2644 HOST_WIDE_INT x0
= i1
* tau1
+ i0
;
2645 HOST_WIDE_INT y0
= j1
* tau1
+ j0
;
2647 /* (X1, Y1) is the smallest positive solution of the eq
2648 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2649 first conflict occurs. */
2650 HOST_WIDE_INT min_multiple
= MIN (x0
/ i1
, y0
/ j1
);
2651 HOST_WIDE_INT x1
= x0
- i1
* min_multiple
;
2652 HOST_WIDE_INT y1
= y0
- j1
* min_multiple
;
2656 HOST_WIDE_INT tau2
= MIN (FLOOR_DIV (niter
- i0
, i1
),
2657 FLOOR_DIV (niter
- j0
, j1
));
2658 HOST_WIDE_INT last_conflict
= tau2
- (x1
- i0
)/i1
;
2660 /* If the overlap occurs outside of the bounds of the
2661 loop, there is no dependence. */
2662 if (x1
>= niter
|| y1
>= niter
)
2664 *overlaps_a
= conflict_fn_no_dependence ();
2665 *overlaps_b
= conflict_fn_no_dependence ();
2666 *last_conflicts
= integer_zero_node
;
2667 goto end_analyze_subs_aa
;
2670 *last_conflicts
= build_int_cst (NULL_TREE
, last_conflict
);
2673 *last_conflicts
= chrec_dont_know
;
2677 affine_fn_univar (build_int_cst (NULL_TREE
, x1
),
2679 build_int_cst (NULL_TREE
, i1
)));
2682 affine_fn_univar (build_int_cst (NULL_TREE
, y1
),
2684 build_int_cst (NULL_TREE
, j1
)));
2688 /* FIXME: For the moment, the upper bound of the
2689 iteration domain for i and j is not checked. */
2690 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2691 fprintf (dump_file
, "affine-affine test failed: unimplemented.\n");
2692 *overlaps_a
= conflict_fn_not_known ();
2693 *overlaps_b
= conflict_fn_not_known ();
2694 *last_conflicts
= chrec_dont_know
;
2699 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2700 fprintf (dump_file
, "affine-affine test failed: unimplemented.\n");
2701 *overlaps_a
= conflict_fn_not_known ();
2702 *overlaps_b
= conflict_fn_not_known ();
2703 *last_conflicts
= chrec_dont_know
;
2708 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2709 fprintf (dump_file
, "affine-affine test failed: unimplemented.\n");
2710 *overlaps_a
= conflict_fn_not_known ();
2711 *overlaps_b
= conflict_fn_not_known ();
2712 *last_conflicts
= chrec_dont_know
;
2715 end_analyze_subs_aa
:
2716 obstack_free (&scratch_obstack
, NULL
);
2717 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2719 fprintf (dump_file
, " (overlaps_a = ");
2720 dump_conflict_function (dump_file
, *overlaps_a
);
2721 fprintf (dump_file
, ")\n (overlaps_b = ");
2722 dump_conflict_function (dump_file
, *overlaps_b
);
2723 fprintf (dump_file
, "))\n");
2727 /* Returns true when analyze_subscript_affine_affine can be used for
2728 determining the dependence relation between chrec_a and chrec_b,
2729 that contain symbols. This function modifies chrec_a and chrec_b
2730 such that the analysis result is the same, and such that they don't
2731 contain symbols, and then can safely be passed to the analyzer.
2733 Example: The analysis of the following tuples of evolutions produce
2734 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2737 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2738 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2742 can_use_analyze_subscript_affine_affine (tree
*chrec_a
, tree
*chrec_b
)
2744 tree diff
, type
, left_a
, left_b
, right_b
;
2746 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a
))
2747 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b
)))
2748 /* FIXME: For the moment not handled. Might be refined later. */
2751 type
= chrec_type (*chrec_a
);
2752 left_a
= CHREC_LEFT (*chrec_a
);
2753 left_b
= chrec_convert (type
, CHREC_LEFT (*chrec_b
), NULL
);
2754 diff
= chrec_fold_minus (type
, left_a
, left_b
);
2756 if (!evolution_function_is_constant_p (diff
))
2759 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2760 fprintf (dump_file
, "can_use_subscript_aff_aff_for_symbolic \n");
2762 *chrec_a
= build_polynomial_chrec (CHREC_VARIABLE (*chrec_a
),
2763 diff
, CHREC_RIGHT (*chrec_a
));
2764 right_b
= chrec_convert (type
, CHREC_RIGHT (*chrec_b
), NULL
);
2765 *chrec_b
= build_polynomial_chrec (CHREC_VARIABLE (*chrec_b
),
2766 build_int_cst (type
, 0),
2771 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2772 *OVERLAPS_B are initialized to the functions that describe the
2773 relation between the elements accessed twice by CHREC_A and
2774 CHREC_B. For k >= 0, the following property is verified:
2776 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2779 analyze_siv_subscript (tree chrec_a
,
2781 conflict_function
**overlaps_a
,
2782 conflict_function
**overlaps_b
,
2783 tree
*last_conflicts
,
2786 dependence_stats
.num_siv
++;
2788 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2789 fprintf (dump_file
, "(analyze_siv_subscript \n");
2791 if (evolution_function_is_constant_p (chrec_a
)
2792 && evolution_function_is_affine_in_loop (chrec_b
, loop_nest_num
))
2793 analyze_siv_subscript_cst_affine (chrec_a
, chrec_b
,
2794 overlaps_a
, overlaps_b
, last_conflicts
);
2796 else if (evolution_function_is_affine_in_loop (chrec_a
, loop_nest_num
)
2797 && evolution_function_is_constant_p (chrec_b
))
2798 analyze_siv_subscript_cst_affine (chrec_b
, chrec_a
,
2799 overlaps_b
, overlaps_a
, last_conflicts
);
2801 else if (evolution_function_is_affine_in_loop (chrec_a
, loop_nest_num
)
2802 && evolution_function_is_affine_in_loop (chrec_b
, loop_nest_num
))
2804 if (!chrec_contains_symbols (chrec_a
)
2805 && !chrec_contains_symbols (chrec_b
))
2807 analyze_subscript_affine_affine (chrec_a
, chrec_b
,
2808 overlaps_a
, overlaps_b
,
2811 if (CF_NOT_KNOWN_P (*overlaps_a
)
2812 || CF_NOT_KNOWN_P (*overlaps_b
))
2813 dependence_stats
.num_siv_unimplemented
++;
2814 else if (CF_NO_DEPENDENCE_P (*overlaps_a
)
2815 || CF_NO_DEPENDENCE_P (*overlaps_b
))
2816 dependence_stats
.num_siv_independent
++;
2818 dependence_stats
.num_siv_dependent
++;
2820 else if (can_use_analyze_subscript_affine_affine (&chrec_a
,
2823 analyze_subscript_affine_affine (chrec_a
, chrec_b
,
2824 overlaps_a
, overlaps_b
,
2827 if (CF_NOT_KNOWN_P (*overlaps_a
)
2828 || CF_NOT_KNOWN_P (*overlaps_b
))
2829 dependence_stats
.num_siv_unimplemented
++;
2830 else if (CF_NO_DEPENDENCE_P (*overlaps_a
)
2831 || CF_NO_DEPENDENCE_P (*overlaps_b
))
2832 dependence_stats
.num_siv_independent
++;
2834 dependence_stats
.num_siv_dependent
++;
2837 goto siv_subscript_dontknow
;
2842 siv_subscript_dontknow
:;
2843 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2844 fprintf (dump_file
, " siv test failed: unimplemented");
2845 *overlaps_a
= conflict_fn_not_known ();
2846 *overlaps_b
= conflict_fn_not_known ();
2847 *last_conflicts
= chrec_dont_know
;
2848 dependence_stats
.num_siv_unimplemented
++;
2851 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2852 fprintf (dump_file
, ")\n");
2855 /* Returns false if we can prove that the greatest common divisor of the steps
2856 of CHREC does not divide CST, false otherwise. */
2859 gcd_of_steps_may_divide_p (const_tree chrec
, const_tree cst
)
2861 HOST_WIDE_INT cd
= 0, val
;
2864 if (!tree_fits_shwi_p (cst
))
2866 val
= tree_to_shwi (cst
);
2868 while (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
2870 step
= CHREC_RIGHT (chrec
);
2871 if (!tree_fits_shwi_p (step
))
2873 cd
= gcd (cd
, tree_to_shwi (step
));
2874 chrec
= CHREC_LEFT (chrec
);
2877 return val
% cd
== 0;
2880 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
2881 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2882 functions that describe the relation between the elements accessed
2883 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2886 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2889 analyze_miv_subscript (tree chrec_a
,
2891 conflict_function
**overlaps_a
,
2892 conflict_function
**overlaps_b
,
2893 tree
*last_conflicts
,
2894 struct loop
*loop_nest
)
2896 tree type
, difference
;
2898 dependence_stats
.num_miv
++;
2899 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2900 fprintf (dump_file
, "(analyze_miv_subscript \n");
2902 type
= signed_type_for_types (TREE_TYPE (chrec_a
), TREE_TYPE (chrec_b
));
2903 chrec_a
= chrec_convert (type
, chrec_a
, NULL
);
2904 chrec_b
= chrec_convert (type
, chrec_b
, NULL
);
2905 difference
= chrec_fold_minus (type
, chrec_a
, chrec_b
);
2907 if (eq_evolutions_p (chrec_a
, chrec_b
))
2909 /* Access functions are the same: all the elements are accessed
2910 in the same order. */
2911 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2912 *overlaps_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2913 *last_conflicts
= max_stmt_executions_tree (get_chrec_loop (chrec_a
));
2914 dependence_stats
.num_miv_dependent
++;
2917 else if (evolution_function_is_constant_p (difference
)
2918 /* For the moment, the following is verified:
2919 evolution_function_is_affine_multivariate_p (chrec_a,
2921 && !gcd_of_steps_may_divide_p (chrec_a
, difference
))
2923 /* testsuite/.../ssa-chrec-33.c
2924 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2926 The difference is 1, and all the evolution steps are multiples
2927 of 2, consequently there are no overlapping elements. */
2928 *overlaps_a
= conflict_fn_no_dependence ();
2929 *overlaps_b
= conflict_fn_no_dependence ();
2930 *last_conflicts
= integer_zero_node
;
2931 dependence_stats
.num_miv_independent
++;
2934 else if (evolution_function_is_affine_multivariate_p (chrec_a
, loop_nest
->num
)
2935 && !chrec_contains_symbols (chrec_a
)
2936 && evolution_function_is_affine_multivariate_p (chrec_b
, loop_nest
->num
)
2937 && !chrec_contains_symbols (chrec_b
))
2939 /* testsuite/.../ssa-chrec-35.c
2940 {0, +, 1}_2 vs. {0, +, 1}_3
2941 the overlapping elements are respectively located at iterations:
2942 {0, +, 1}_x and {0, +, 1}_x,
2943 in other words, we have the equality:
2944 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2947 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2948 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2950 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2951 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2953 analyze_subscript_affine_affine (chrec_a
, chrec_b
,
2954 overlaps_a
, overlaps_b
, last_conflicts
);
2956 if (CF_NOT_KNOWN_P (*overlaps_a
)
2957 || CF_NOT_KNOWN_P (*overlaps_b
))
2958 dependence_stats
.num_miv_unimplemented
++;
2959 else if (CF_NO_DEPENDENCE_P (*overlaps_a
)
2960 || CF_NO_DEPENDENCE_P (*overlaps_b
))
2961 dependence_stats
.num_miv_independent
++;
2963 dependence_stats
.num_miv_dependent
++;
2968 /* When the analysis is too difficult, answer "don't know". */
2969 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2970 fprintf (dump_file
, "analyze_miv_subscript test failed: unimplemented.\n");
2972 *overlaps_a
= conflict_fn_not_known ();
2973 *overlaps_b
= conflict_fn_not_known ();
2974 *last_conflicts
= chrec_dont_know
;
2975 dependence_stats
.num_miv_unimplemented
++;
2978 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2979 fprintf (dump_file
, ")\n");
2982 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
2983 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
2984 OVERLAP_ITERATIONS_B are initialized with two functions that
2985 describe the iterations that contain conflicting elements.
2987 Remark: For an integer k >= 0, the following equality is true:
2989 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
2993 analyze_overlapping_iterations (tree chrec_a
,
2995 conflict_function
**overlap_iterations_a
,
2996 conflict_function
**overlap_iterations_b
,
2997 tree
*last_conflicts
, struct loop
*loop_nest
)
2999 unsigned int lnn
= loop_nest
->num
;
3001 dependence_stats
.num_subscript_tests
++;
3003 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3005 fprintf (dump_file
, "(analyze_overlapping_iterations \n");
3006 fprintf (dump_file
, " (chrec_a = ");
3007 print_generic_expr (dump_file
, chrec_a
, 0);
3008 fprintf (dump_file
, ")\n (chrec_b = ");
3009 print_generic_expr (dump_file
, chrec_b
, 0);
3010 fprintf (dump_file
, ")\n");
3013 if (chrec_a
== NULL_TREE
3014 || chrec_b
== NULL_TREE
3015 || chrec_contains_undetermined (chrec_a
)
3016 || chrec_contains_undetermined (chrec_b
))
3018 dependence_stats
.num_subscript_undetermined
++;
3020 *overlap_iterations_a
= conflict_fn_not_known ();
3021 *overlap_iterations_b
= conflict_fn_not_known ();
3024 /* If they are the same chrec, and are affine, they overlap
3025 on every iteration. */
3026 else if (eq_evolutions_p (chrec_a
, chrec_b
)
3027 && (evolution_function_is_affine_multivariate_p (chrec_a
, lnn
)
3028 || operand_equal_p (chrec_a
, chrec_b
, 0)))
3030 dependence_stats
.num_same_subscript_function
++;
3031 *overlap_iterations_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
3032 *overlap_iterations_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
3033 *last_conflicts
= chrec_dont_know
;
3036 /* If they aren't the same, and aren't affine, we can't do anything
3038 else if ((chrec_contains_symbols (chrec_a
)
3039 || chrec_contains_symbols (chrec_b
))
3040 && (!evolution_function_is_affine_multivariate_p (chrec_a
, lnn
)
3041 || !evolution_function_is_affine_multivariate_p (chrec_b
, lnn
)))
3043 dependence_stats
.num_subscript_undetermined
++;
3044 *overlap_iterations_a
= conflict_fn_not_known ();
3045 *overlap_iterations_b
= conflict_fn_not_known ();
3048 else if (ziv_subscript_p (chrec_a
, chrec_b
))
3049 analyze_ziv_subscript (chrec_a
, chrec_b
,
3050 overlap_iterations_a
, overlap_iterations_b
,
3053 else if (siv_subscript_p (chrec_a
, chrec_b
))
3054 analyze_siv_subscript (chrec_a
, chrec_b
,
3055 overlap_iterations_a
, overlap_iterations_b
,
3056 last_conflicts
, lnn
);
3059 analyze_miv_subscript (chrec_a
, chrec_b
,
3060 overlap_iterations_a
, overlap_iterations_b
,
3061 last_conflicts
, loop_nest
);
3063 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3065 fprintf (dump_file
, " (overlap_iterations_a = ");
3066 dump_conflict_function (dump_file
, *overlap_iterations_a
);
3067 fprintf (dump_file
, ")\n (overlap_iterations_b = ");
3068 dump_conflict_function (dump_file
, *overlap_iterations_b
);
3069 fprintf (dump_file
, "))\n");
3073 /* Helper function for uniquely inserting distance vectors. */
3076 save_dist_v (struct data_dependence_relation
*ddr
, lambda_vector dist_v
)
3081 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr
), i
, v
)
3082 if (lambda_vector_equal (v
, dist_v
, DDR_NB_LOOPS (ddr
)))
3085 DDR_DIST_VECTS (ddr
).safe_push (dist_v
);
3088 /* Helper function for uniquely inserting direction vectors. */
3091 save_dir_v (struct data_dependence_relation
*ddr
, lambda_vector dir_v
)
3096 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr
), i
, v
)
3097 if (lambda_vector_equal (v
, dir_v
, DDR_NB_LOOPS (ddr
)))
3100 DDR_DIR_VECTS (ddr
).safe_push (dir_v
);
3103 /* Add a distance of 1 on all the loops outer than INDEX. If we
3104 haven't yet determined a distance for this outer loop, push a new
3105 distance vector composed of the previous distance, and a distance
3106 of 1 for this outer loop. Example:
3114 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3115 save (0, 1), then we have to save (1, 0). */
3118 add_outer_distances (struct data_dependence_relation
*ddr
,
3119 lambda_vector dist_v
, int index
)
3121 /* For each outer loop where init_v is not set, the accesses are
3122 in dependence of distance 1 in the loop. */
3123 while (--index
>= 0)
3125 lambda_vector save_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3126 lambda_vector_copy (dist_v
, save_v
, DDR_NB_LOOPS (ddr
));
3128 save_dist_v (ddr
, save_v
);
3132 /* Return false when fail to represent the data dependence as a
3133 distance vector. INIT_B is set to true when a component has been
3134 added to the distance vector DIST_V. INDEX_CARRY is then set to
3135 the index in DIST_V that carries the dependence. */
3138 build_classic_dist_vector_1 (struct data_dependence_relation
*ddr
,
3139 struct data_reference
*ddr_a
,
3140 struct data_reference
*ddr_b
,
3141 lambda_vector dist_v
, bool *init_b
,
3145 lambda_vector init_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3147 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3149 tree access_fn_a
, access_fn_b
;
3150 struct subscript
*subscript
= DDR_SUBSCRIPT (ddr
, i
);
3152 if (chrec_contains_undetermined (SUB_DISTANCE (subscript
)))
3154 non_affine_dependence_relation (ddr
);
3158 access_fn_a
= DR_ACCESS_FN (ddr_a
, i
);
3159 access_fn_b
= DR_ACCESS_FN (ddr_b
, i
);
3161 if (TREE_CODE (access_fn_a
) == POLYNOMIAL_CHREC
3162 && TREE_CODE (access_fn_b
) == POLYNOMIAL_CHREC
)
3165 int var_a
= CHREC_VARIABLE (access_fn_a
);
3166 int var_b
= CHREC_VARIABLE (access_fn_b
);
3169 || chrec_contains_undetermined (SUB_DISTANCE (subscript
)))
3171 non_affine_dependence_relation (ddr
);
3175 dist
= int_cst_value (SUB_DISTANCE (subscript
));
3176 index
= index_in_loop_nest (var_a
, DDR_LOOP_NEST (ddr
));
3177 *index_carry
= MIN (index
, *index_carry
);
3179 /* This is the subscript coupling test. If we have already
3180 recorded a distance for this loop (a distance coming from
3181 another subscript), it should be the same. For example,
3182 in the following code, there is no dependence:
3189 if (init_v
[index
] != 0 && dist_v
[index
] != dist
)
3191 finalize_ddr_dependent (ddr
, chrec_known
);
3195 dist_v
[index
] = dist
;
3199 else if (!operand_equal_p (access_fn_a
, access_fn_b
, 0))
3201 /* This can be for example an affine vs. constant dependence
3202 (T[i] vs. T[3]) that is not an affine dependence and is
3203 not representable as a distance vector. */
3204 non_affine_dependence_relation (ddr
);
3212 /* Return true when the DDR contains only constant access functions. */
3215 constant_access_functions (const struct data_dependence_relation
*ddr
)
3219 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3220 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr
), i
))
3221 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr
), i
)))
3227 /* Helper function for the case where DDR_A and DDR_B are the same
3228 multivariate access function with a constant step. For an example
3232 add_multivariate_self_dist (struct data_dependence_relation
*ddr
, tree c_2
)
3235 tree c_1
= CHREC_LEFT (c_2
);
3236 tree c_0
= CHREC_LEFT (c_1
);
3237 lambda_vector dist_v
;
3240 /* Polynomials with more than 2 variables are not handled yet. When
3241 the evolution steps are parameters, it is not possible to
3242 represent the dependence using classical distance vectors. */
3243 if (TREE_CODE (c_0
) != INTEGER_CST
3244 || TREE_CODE (CHREC_RIGHT (c_1
)) != INTEGER_CST
3245 || TREE_CODE (CHREC_RIGHT (c_2
)) != INTEGER_CST
)
3247 DDR_AFFINE_P (ddr
) = false;
3251 x_2
= index_in_loop_nest (CHREC_VARIABLE (c_2
), DDR_LOOP_NEST (ddr
));
3252 x_1
= index_in_loop_nest (CHREC_VARIABLE (c_1
), DDR_LOOP_NEST (ddr
));
3254 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3255 dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3256 v1
= int_cst_value (CHREC_RIGHT (c_1
));
3257 v2
= int_cst_value (CHREC_RIGHT (c_2
));
3270 save_dist_v (ddr
, dist_v
);
3272 add_outer_distances (ddr
, dist_v
, x_1
);
3275 /* Helper function for the case where DDR_A and DDR_B are the same
3276 access functions. */
3279 add_other_self_distances (struct data_dependence_relation
*ddr
)
3281 lambda_vector dist_v
;
3283 int index_carry
= DDR_NB_LOOPS (ddr
);
3285 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3287 tree access_fun
= DR_ACCESS_FN (DDR_A (ddr
), i
);
3289 if (TREE_CODE (access_fun
) == POLYNOMIAL_CHREC
)
3291 if (!evolution_function_is_univariate_p (access_fun
))
3293 if (DDR_NUM_SUBSCRIPTS (ddr
) != 1)
3295 DDR_ARE_DEPENDENT (ddr
) = chrec_dont_know
;
3299 access_fun
= DR_ACCESS_FN (DDR_A (ddr
), 0);
3301 if (TREE_CODE (CHREC_LEFT (access_fun
)) == POLYNOMIAL_CHREC
)
3302 add_multivariate_self_dist (ddr
, access_fun
);
3304 /* The evolution step is not constant: it varies in
3305 the outer loop, so this cannot be represented by a
3306 distance vector. For example in pr34635.c the
3307 evolution is {0, +, {0, +, 4}_1}_2. */
3308 DDR_AFFINE_P (ddr
) = false;
3313 index_carry
= MIN (index_carry
,
3314 index_in_loop_nest (CHREC_VARIABLE (access_fun
),
3315 DDR_LOOP_NEST (ddr
)));
3319 dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3320 add_outer_distances (ddr
, dist_v
, index_carry
);
3324 insert_innermost_unit_dist_vector (struct data_dependence_relation
*ddr
)
3326 lambda_vector dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3328 dist_v
[DDR_INNER_LOOP (ddr
)] = 1;
3329 save_dist_v (ddr
, dist_v
);
3332 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
3333 is the case for example when access functions are the same and
3334 equal to a constant, as in:
3341 in which case the distance vectors are (0) and (1). */
3344 add_distance_for_zero_overlaps (struct data_dependence_relation
*ddr
)
3348 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3350 subscript_p sub
= DDR_SUBSCRIPT (ddr
, i
);
3351 conflict_function
*ca
= SUB_CONFLICTS_IN_A (sub
);
3352 conflict_function
*cb
= SUB_CONFLICTS_IN_B (sub
);
3354 for (j
= 0; j
< ca
->n
; j
++)
3355 if (affine_function_zero_p (ca
->fns
[j
]))
3357 insert_innermost_unit_dist_vector (ddr
);
3361 for (j
= 0; j
< cb
->n
; j
++)
3362 if (affine_function_zero_p (cb
->fns
[j
]))
3364 insert_innermost_unit_dist_vector (ddr
);
3370 /* Compute the classic per loop distance vector. DDR is the data
3371 dependence relation to build a vector from. Return false when fail
3372 to represent the data dependence as a distance vector. */
3375 build_classic_dist_vector (struct data_dependence_relation
*ddr
,
3376 struct loop
*loop_nest
)
3378 bool init_b
= false;
3379 int index_carry
= DDR_NB_LOOPS (ddr
);
3380 lambda_vector dist_v
;
3382 if (DDR_ARE_DEPENDENT (ddr
) != NULL_TREE
)
3385 if (same_access_functions (ddr
))
3387 /* Save the 0 vector. */
3388 dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3389 save_dist_v (ddr
, dist_v
);
3391 if (constant_access_functions (ddr
))
3392 add_distance_for_zero_overlaps (ddr
);
3394 if (DDR_NB_LOOPS (ddr
) > 1)
3395 add_other_self_distances (ddr
);
3400 dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3401 if (!build_classic_dist_vector_1 (ddr
, DDR_A (ddr
), DDR_B (ddr
),
3402 dist_v
, &init_b
, &index_carry
))
3405 /* Save the distance vector if we initialized one. */
3408 /* Verify a basic constraint: classic distance vectors should
3409 always be lexicographically positive.
3411 Data references are collected in the order of execution of
3412 the program, thus for the following loop
3414 | for (i = 1; i < 100; i++)
3415 | for (j = 1; j < 100; j++)
3417 | t = T[j+1][i-1]; // A
3418 | T[j][i] = t + 2; // B
3421 references are collected following the direction of the wind:
3422 A then B. The data dependence tests are performed also
3423 following this order, such that we're looking at the distance
3424 separating the elements accessed by A from the elements later
3425 accessed by B. But in this example, the distance returned by
3426 test_dep (A, B) is lexicographically negative (-1, 1), that
3427 means that the access A occurs later than B with respect to
3428 the outer loop, ie. we're actually looking upwind. In this
3429 case we solve test_dep (B, A) looking downwind to the
3430 lexicographically positive solution, that returns the
3431 distance vector (1, -1). */
3432 if (!lambda_vector_lexico_pos (dist_v
, DDR_NB_LOOPS (ddr
)))
3434 lambda_vector save_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3435 if (!subscript_dependence_tester_1 (ddr
, DDR_B (ddr
), DDR_A (ddr
),
3438 compute_subscript_distance (ddr
);
3439 if (!build_classic_dist_vector_1 (ddr
, DDR_B (ddr
), DDR_A (ddr
),
3440 save_v
, &init_b
, &index_carry
))
3442 save_dist_v (ddr
, save_v
);
3443 DDR_REVERSED_P (ddr
) = true;
3445 /* In this case there is a dependence forward for all the
3448 | for (k = 1; k < 100; k++)
3449 | for (i = 1; i < 100; i++)
3450 | for (j = 1; j < 100; j++)
3452 | t = T[j+1][i-1]; // A
3453 | T[j][i] = t + 2; // B
3461 if (DDR_NB_LOOPS (ddr
) > 1)
3463 add_outer_distances (ddr
, save_v
, index_carry
);
3464 add_outer_distances (ddr
, dist_v
, index_carry
);
3469 lambda_vector save_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3470 lambda_vector_copy (dist_v
, save_v
, DDR_NB_LOOPS (ddr
));
3472 if (DDR_NB_LOOPS (ddr
) > 1)
3474 lambda_vector opposite_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3476 if (!subscript_dependence_tester_1 (ddr
, DDR_B (ddr
),
3477 DDR_A (ddr
), loop_nest
))
3479 compute_subscript_distance (ddr
);
3480 if (!build_classic_dist_vector_1 (ddr
, DDR_B (ddr
), DDR_A (ddr
),
3481 opposite_v
, &init_b
,
3485 save_dist_v (ddr
, save_v
);
3486 add_outer_distances (ddr
, dist_v
, index_carry
);
3487 add_outer_distances (ddr
, opposite_v
, index_carry
);
3490 save_dist_v (ddr
, save_v
);
3495 /* There is a distance of 1 on all the outer loops: Example:
3496 there is a dependence of distance 1 on loop_1 for the array A.
3502 add_outer_distances (ddr
, dist_v
,
3503 lambda_vector_first_nz (dist_v
,
3504 DDR_NB_LOOPS (ddr
), 0));
3507 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3511 fprintf (dump_file
, "(build_classic_dist_vector\n");
3512 for (i
= 0; i
< DDR_NUM_DIST_VECTS (ddr
); i
++)
3514 fprintf (dump_file
, " dist_vector = (");
3515 print_lambda_vector (dump_file
, DDR_DIST_VECT (ddr
, i
),
3516 DDR_NB_LOOPS (ddr
));
3517 fprintf (dump_file
, " )\n");
3519 fprintf (dump_file
, ")\n");
3525 /* Return the direction for a given distance.
3526 FIXME: Computing dir this way is suboptimal, since dir can catch
3527 cases that dist is unable to represent. */
3529 static inline enum data_dependence_direction
3530 dir_from_dist (int dist
)
3533 return dir_positive
;
3535 return dir_negative
;
3540 /* Compute the classic per loop direction vector. DDR is the data
3541 dependence relation to build a vector from. */
3544 build_classic_dir_vector (struct data_dependence_relation
*ddr
)
3547 lambda_vector dist_v
;
3549 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr
), i
, dist_v
)
3551 lambda_vector dir_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3553 for (j
= 0; j
< DDR_NB_LOOPS (ddr
); j
++)
3554 dir_v
[j
] = dir_from_dist (dist_v
[j
]);
3556 save_dir_v (ddr
, dir_v
);
3560 /* Helper function. Returns true when there is a dependence between
3561 data references DRA and DRB. */
3564 subscript_dependence_tester_1 (struct data_dependence_relation
*ddr
,
3565 struct data_reference
*dra
,
3566 struct data_reference
*drb
,
3567 struct loop
*loop_nest
)
3570 tree last_conflicts
;
3571 struct subscript
*subscript
;
3572 tree res
= NULL_TREE
;
3574 for (i
= 0; DDR_SUBSCRIPTS (ddr
).iterate (i
, &subscript
); i
++)
3576 conflict_function
*overlaps_a
, *overlaps_b
;
3578 analyze_overlapping_iterations (DR_ACCESS_FN (dra
, i
),
3579 DR_ACCESS_FN (drb
, i
),
3580 &overlaps_a
, &overlaps_b
,
3581 &last_conflicts
, loop_nest
);
3583 if (SUB_CONFLICTS_IN_A (subscript
))
3584 free_conflict_function (SUB_CONFLICTS_IN_A (subscript
));
3585 if (SUB_CONFLICTS_IN_B (subscript
))
3586 free_conflict_function (SUB_CONFLICTS_IN_B (subscript
));
3588 SUB_CONFLICTS_IN_A (subscript
) = overlaps_a
;
3589 SUB_CONFLICTS_IN_B (subscript
) = overlaps_b
;
3590 SUB_LAST_CONFLICT (subscript
) = last_conflicts
;
3592 /* If there is any undetermined conflict function we have to
3593 give a conservative answer in case we cannot prove that
3594 no dependence exists when analyzing another subscript. */
3595 if (CF_NOT_KNOWN_P (overlaps_a
)
3596 || CF_NOT_KNOWN_P (overlaps_b
))
3598 res
= chrec_dont_know
;
3602 /* When there is a subscript with no dependence we can stop. */
3603 else if (CF_NO_DEPENDENCE_P (overlaps_a
)
3604 || CF_NO_DEPENDENCE_P (overlaps_b
))
3611 if (res
== NULL_TREE
)
3614 if (res
== chrec_known
)
3615 dependence_stats
.num_dependence_independent
++;
3617 dependence_stats
.num_dependence_undetermined
++;
3618 finalize_ddr_dependent (ddr
, res
);
3622 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
3625 subscript_dependence_tester (struct data_dependence_relation
*ddr
,
3626 struct loop
*loop_nest
)
3628 if (subscript_dependence_tester_1 (ddr
, DDR_A (ddr
), DDR_B (ddr
), loop_nest
))
3629 dependence_stats
.num_dependence_dependent
++;
3631 compute_subscript_distance (ddr
);
3632 if (build_classic_dist_vector (ddr
, loop_nest
))
3633 build_classic_dir_vector (ddr
);
3636 /* Returns true when all the access functions of A are affine or
3637 constant with respect to LOOP_NEST. */
3640 access_functions_are_affine_or_constant_p (const struct data_reference
*a
,
3641 const struct loop
*loop_nest
)
3644 vec
<tree
> fns
= DR_ACCESS_FNS (a
);
3647 FOR_EACH_VEC_ELT (fns
, i
, t
)
3648 if (!evolution_function_is_invariant_p (t
, loop_nest
->num
)
3649 && !evolution_function_is_affine_multivariate_p (t
, loop_nest
->num
))
3655 /* Initializes an equation for an OMEGA problem using the information
3656 contained in the ACCESS_FUN. Returns true when the operation
3659 PB is the omega constraint system.
3660 EQ is the number of the equation to be initialized.
3661 OFFSET is used for shifting the variables names in the constraints:
3662 a constrain is composed of 2 * the number of variables surrounding
3663 dependence accesses. OFFSET is set either to 0 for the first n variables,
3664 then it is set to n.
3665 ACCESS_FUN is expected to be an affine chrec. */
3668 init_omega_eq_with_af (omega_pb pb
, unsigned eq
,
3669 unsigned int offset
, tree access_fun
,
3670 struct data_dependence_relation
*ddr
)
3672 switch (TREE_CODE (access_fun
))
3674 case POLYNOMIAL_CHREC
:
3676 tree left
= CHREC_LEFT (access_fun
);
3677 tree right
= CHREC_RIGHT (access_fun
);
3678 int var
= CHREC_VARIABLE (access_fun
);
3681 if (TREE_CODE (right
) != INTEGER_CST
)
3684 var_idx
= index_in_loop_nest (var
, DDR_LOOP_NEST (ddr
));
3685 pb
->eqs
[eq
].coef
[offset
+ var_idx
+ 1] = int_cst_value (right
);
3687 /* Compute the innermost loop index. */
3688 DDR_INNER_LOOP (ddr
) = MAX (DDR_INNER_LOOP (ddr
), var_idx
);
3691 pb
->eqs
[eq
].coef
[var_idx
+ DDR_NB_LOOPS (ddr
) + 1]
3692 += int_cst_value (right
);
3694 switch (TREE_CODE (left
))
3696 case POLYNOMIAL_CHREC
:
3697 return init_omega_eq_with_af (pb
, eq
, offset
, left
, ddr
);
3700 pb
->eqs
[eq
].coef
[0] += int_cst_value (left
);
3709 pb
->eqs
[eq
].coef
[0] += int_cst_value (access_fun
);
3717 /* As explained in the comments preceding init_omega_for_ddr, we have
3718 to set up a system for each loop level, setting outer loops
3719 variation to zero, and current loop variation to positive or zero.
3720 Save each lexico positive distance vector. */
3723 omega_extract_distance_vectors (omega_pb pb
,
3724 struct data_dependence_relation
*ddr
)
3728 struct loop
*loopi
, *loopj
;
3729 enum omega_result res
;
3731 /* Set a new problem for each loop in the nest. The basis is the
3732 problem that we have initialized until now. On top of this we
3733 add new constraints. */
3734 for (i
= 0; i
<= DDR_INNER_LOOP (ddr
)
3735 && DDR_LOOP_NEST (ddr
).iterate (i
, &loopi
); i
++)
3738 omega_pb copy
= omega_alloc_problem (2 * DDR_NB_LOOPS (ddr
),
3739 DDR_NB_LOOPS (ddr
));
3741 omega_copy_problem (copy
, pb
);
3743 /* For all the outer loops "loop_j", add "dj = 0". */
3744 for (j
= 0; j
< i
&& DDR_LOOP_NEST (ddr
).iterate (j
, &loopj
); j
++)
3746 eq
= omega_add_zero_eq (copy
, omega_black
);
3747 copy
->eqs
[eq
].coef
[j
+ 1] = 1;
3750 /* For "loop_i", add "0 <= di". */
3751 geq
= omega_add_zero_geq (copy
, omega_black
);
3752 copy
->geqs
[geq
].coef
[i
+ 1] = 1;
3754 /* Reduce the constraint system, and test that the current
3755 problem is feasible. */
3756 res
= omega_simplify_problem (copy
);
3757 if (res
== omega_false
3758 || res
== omega_unknown
3759 || copy
->num_geqs
> (int) DDR_NB_LOOPS (ddr
))
3762 for (eq
= 0; eq
< copy
->num_subs
; eq
++)
3763 if (copy
->subs
[eq
].key
== (int) i
+ 1)
3765 dist
= copy
->subs
[eq
].coef
[0];
3771 /* Reinitialize problem... */
3772 omega_copy_problem (copy
, pb
);
3773 for (j
= 0; j
< i
&& DDR_LOOP_NEST (ddr
).iterate (j
, &loopj
); j
++)
3775 eq
= omega_add_zero_eq (copy
, omega_black
);
3776 copy
->eqs
[eq
].coef
[j
+ 1] = 1;
3779 /* ..., but this time "di = 1". */
3780 eq
= omega_add_zero_eq (copy
, omega_black
);
3781 copy
->eqs
[eq
].coef
[i
+ 1] = 1;
3782 copy
->eqs
[eq
].coef
[0] = -1;
3784 res
= omega_simplify_problem (copy
);
3785 if (res
== omega_false
3786 || res
== omega_unknown
3787 || copy
->num_geqs
> (int) DDR_NB_LOOPS (ddr
))
3790 for (eq
= 0; eq
< copy
->num_subs
; eq
++)
3791 if (copy
->subs
[eq
].key
== (int) i
+ 1)
3793 dist
= copy
->subs
[eq
].coef
[0];
3799 /* Save the lexicographically positive distance vector. */
3802 lambda_vector dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3803 lambda_vector dir_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3807 for (eq
= 0; eq
< copy
->num_subs
; eq
++)
3808 if (copy
->subs
[eq
].key
> 0)
3810 dist
= copy
->subs
[eq
].coef
[0];
3811 dist_v
[copy
->subs
[eq
].key
- 1] = dist
;
3814 for (j
= 0; j
< DDR_NB_LOOPS (ddr
); j
++)
3815 dir_v
[j
] = dir_from_dist (dist_v
[j
]);
3817 save_dist_v (ddr
, dist_v
);
3818 save_dir_v (ddr
, dir_v
);
3822 omega_free_problem (copy
);
3826 /* This is called for each subscript of a tuple of data references:
3827 insert an equality for representing the conflicts. */
3830 omega_setup_subscript (tree access_fun_a
, tree access_fun_b
,
3831 struct data_dependence_relation
*ddr
,
3832 omega_pb pb
, bool *maybe_dependent
)
3835 tree type
= signed_type_for_types (TREE_TYPE (access_fun_a
),
3836 TREE_TYPE (access_fun_b
));
3837 tree fun_a
= chrec_convert (type
, access_fun_a
, NULL
);
3838 tree fun_b
= chrec_convert (type
, access_fun_b
, NULL
);
3839 tree difference
= chrec_fold_minus (type
, fun_a
, fun_b
);
3842 /* When the fun_a - fun_b is not constant, the dependence is not
3843 captured by the classic distance vector representation. */
3844 if (TREE_CODE (difference
) != INTEGER_CST
)
3848 if (ziv_subscript_p (fun_a
, fun_b
) && !integer_zerop (difference
))
3850 /* There is no dependence. */
3851 *maybe_dependent
= false;
3855 minus_one
= build_int_cst (type
, -1);
3856 fun_b
= chrec_fold_multiply (type
, fun_b
, minus_one
);
3858 eq
= omega_add_zero_eq (pb
, omega_black
);
3859 if (!init_omega_eq_with_af (pb
, eq
, DDR_NB_LOOPS (ddr
), fun_a
, ddr
)
3860 || !init_omega_eq_with_af (pb
, eq
, 0, fun_b
, ddr
))
3861 /* There is probably a dependence, but the system of
3862 constraints cannot be built: answer "don't know". */
3866 if (DDR_NB_LOOPS (ddr
) != 0 && pb
->eqs
[eq
].coef
[0]
3867 && !int_divides_p (lambda_vector_gcd
3868 ((lambda_vector
) &(pb
->eqs
[eq
].coef
[1]),
3869 2 * DDR_NB_LOOPS (ddr
)),
3870 pb
->eqs
[eq
].coef
[0]))
3872 /* There is no dependence. */
3873 *maybe_dependent
= false;
3880 /* Helper function, same as init_omega_for_ddr but specialized for
3881 data references A and B. */
3884 init_omega_for_ddr_1 (struct data_reference
*dra
, struct data_reference
*drb
,
3885 struct data_dependence_relation
*ddr
,
3886 omega_pb pb
, bool *maybe_dependent
)
3891 unsigned nb_loops
= DDR_NB_LOOPS (ddr
);
3893 /* Insert an equality per subscript. */
3894 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3896 if (!omega_setup_subscript (DR_ACCESS_FN (dra
, i
), DR_ACCESS_FN (drb
, i
),
3897 ddr
, pb
, maybe_dependent
))
3899 else if (*maybe_dependent
== false)
3901 /* There is no dependence. */
3902 DDR_ARE_DEPENDENT (ddr
) = chrec_known
;
3907 /* Insert inequalities: constraints corresponding to the iteration
3908 domain, i.e. the loops surrounding the references "loop_x" and
3909 the distance variables "dx". The layout of the OMEGA
3910 representation is as follows:
3911 - coef[0] is the constant
3912 - coef[1..nb_loops] are the protected variables that will not be
3913 removed by the solver: the "dx"
3914 - coef[nb_loops + 1, 2*nb_loops] are the loop variables: "loop_x".
3916 for (i
= 0; i
<= DDR_INNER_LOOP (ddr
)
3917 && DDR_LOOP_NEST (ddr
).iterate (i
, &loopi
); i
++)
3919 HOST_WIDE_INT nbi
= max_stmt_executions_int (loopi
);
3922 ineq
= omega_add_zero_geq (pb
, omega_black
);
3923 pb
->geqs
[ineq
].coef
[i
+ nb_loops
+ 1] = 1;
3925 /* 0 <= loop_x + dx */
3926 ineq
= omega_add_zero_geq (pb
, omega_black
);
3927 pb
->geqs
[ineq
].coef
[i
+ nb_loops
+ 1] = 1;
3928 pb
->geqs
[ineq
].coef
[i
+ 1] = 1;
3932 /* loop_x <= nb_iters */
3933 ineq
= omega_add_zero_geq (pb
, omega_black
);
3934 pb
->geqs
[ineq
].coef
[i
+ nb_loops
+ 1] = -1;
3935 pb
->geqs
[ineq
].coef
[0] = nbi
;
3937 /* loop_x + dx <= nb_iters */
3938 ineq
= omega_add_zero_geq (pb
, omega_black
);
3939 pb
->geqs
[ineq
].coef
[i
+ nb_loops
+ 1] = -1;
3940 pb
->geqs
[ineq
].coef
[i
+ 1] = -1;
3941 pb
->geqs
[ineq
].coef
[0] = nbi
;
3943 /* A step "dx" bigger than nb_iters is not feasible, so
3944 add "0 <= nb_iters + dx", */
3945 ineq
= omega_add_zero_geq (pb
, omega_black
);
3946 pb
->geqs
[ineq
].coef
[i
+ 1] = 1;
3947 pb
->geqs
[ineq
].coef
[0] = nbi
;
3948 /* and "dx <= nb_iters". */
3949 ineq
= omega_add_zero_geq (pb
, omega_black
);
3950 pb
->geqs
[ineq
].coef
[i
+ 1] = -1;
3951 pb
->geqs
[ineq
].coef
[0] = nbi
;
3955 omega_extract_distance_vectors (pb
, ddr
);
3960 /* Sets up the Omega dependence problem for the data dependence
3961 relation DDR. Returns false when the constraint system cannot be
3962 built, ie. when the test answers "don't know". Returns true
3963 otherwise, and when independence has been proved (using one of the
3964 trivial dependence test), set MAYBE_DEPENDENT to false, otherwise
3965 set MAYBE_DEPENDENT to true.
3967 Example: for setting up the dependence system corresponding to the
3968 conflicting accesses
3973 | ... A[2*j, 2*(i + j)]
3977 the following constraints come from the iteration domain:
3984 where di, dj are the distance variables. The constraints
3985 representing the conflicting elements are:
3988 i + 1 = 2 * (i + di + j + dj)
3990 For asking that the resulting distance vector (di, dj) be
3991 lexicographically positive, we insert the constraint "di >= 0". If
3992 "di = 0" in the solution, we fix that component to zero, and we
3993 look at the inner loops: we set a new problem where all the outer
3994 loop distances are zero, and fix this inner component to be
3995 positive. When one of the components is positive, we save that
3996 distance, and set a new problem where the distance on this loop is
3997 zero, searching for other distances in the inner loops. Here is
3998 the classic example that illustrates that we have to set for each
3999 inner loop a new problem:
4007 we have to save two distances (1, 0) and (0, 1).
4009 Given two array references, refA and refB, we have to set the
4010 dependence problem twice, refA vs. refB and refB vs. refA, and we
4011 cannot do a single test, as refB might occur before refA in the
4012 inner loops, and the contrary when considering outer loops: ex.
4017 | T[{1,+,1}_2][{1,+,1}_1] // refA
4018 | T[{2,+,1}_2][{0,+,1}_1] // refB
4023 refB touches the elements in T before refA, and thus for the same
4024 loop_0 refB precedes refA: ie. the distance vector (0, 1, -1)
4025 but for successive loop_0 iterations, we have (1, -1, 1)
4027 The Omega solver expects the distance variables ("di" in the
4028 previous example) to come first in the constraint system (as
4029 variables to be protected, or "safe" variables), the constraint
4030 system is built using the following layout:
4032 "cst | distance vars | index vars".
4036 init_omega_for_ddr (struct data_dependence_relation
*ddr
,
4037 bool *maybe_dependent
)
4042 *maybe_dependent
= true;
4044 if (same_access_functions (ddr
))
4047 lambda_vector dir_v
;
4049 /* Save the 0 vector. */
4050 save_dist_v (ddr
, lambda_vector_new (DDR_NB_LOOPS (ddr
)));
4051 dir_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
4052 for (j
= 0; j
< DDR_NB_LOOPS (ddr
); j
++)
4053 dir_v
[j
] = dir_equal
;
4054 save_dir_v (ddr
, dir_v
);
4056 /* Save the dependences carried by outer loops. */
4057 pb
= omega_alloc_problem (2 * DDR_NB_LOOPS (ddr
), DDR_NB_LOOPS (ddr
));
4058 res
= init_omega_for_ddr_1 (DDR_A (ddr
), DDR_B (ddr
), ddr
, pb
,
4060 omega_free_problem (pb
);
4064 /* Omega expects the protected variables (those that have to be kept
4065 after elimination) to appear first in the constraint system.
4066 These variables are the distance variables. In the following
4067 initialization we declare NB_LOOPS safe variables, and the total
4068 number of variables for the constraint system is 2*NB_LOOPS. */
4069 pb
= omega_alloc_problem (2 * DDR_NB_LOOPS (ddr
), DDR_NB_LOOPS (ddr
));
4070 res
= init_omega_for_ddr_1 (DDR_A (ddr
), DDR_B (ddr
), ddr
, pb
,
4072 omega_free_problem (pb
);
4074 /* Stop computation if not decidable, or no dependence. */
4075 if (res
== false || *maybe_dependent
== false)
4078 pb
= omega_alloc_problem (2 * DDR_NB_LOOPS (ddr
), DDR_NB_LOOPS (ddr
));
4079 res
= init_omega_for_ddr_1 (DDR_B (ddr
), DDR_A (ddr
), ddr
, pb
,
4081 omega_free_problem (pb
);
4086 /* Return true when DDR contains the same information as that stored
4087 in DIR_VECTS and in DIST_VECTS, return false otherwise. */
4090 ddr_consistent_p (FILE *file
,
4091 struct data_dependence_relation
*ddr
,
4092 vec
<lambda_vector
> dist_vects
,
4093 vec
<lambda_vector
> dir_vects
)
4097 /* If dump_file is set, output there. */
4098 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4101 if (dist_vects
.length () != DDR_NUM_DIST_VECTS (ddr
))
4103 lambda_vector b_dist_v
;
4104 fprintf (file
, "\n(Number of distance vectors differ: Banerjee has %d, Omega has %d.\n",
4105 dist_vects
.length (),
4106 DDR_NUM_DIST_VECTS (ddr
));
4108 fprintf (file
, "Banerjee dist vectors:\n");
4109 FOR_EACH_VEC_ELT (dist_vects
, i
, b_dist_v
)
4110 print_lambda_vector (file
, b_dist_v
, DDR_NB_LOOPS (ddr
));
4112 fprintf (file
, "Omega dist vectors:\n");
4113 for (i
= 0; i
< DDR_NUM_DIST_VECTS (ddr
); i
++)
4114 print_lambda_vector (file
, DDR_DIST_VECT (ddr
, i
), DDR_NB_LOOPS (ddr
));
4116 fprintf (file
, "data dependence relation:\n");
4117 dump_data_dependence_relation (file
, ddr
);
4119 fprintf (file
, ")\n");
4123 if (dir_vects
.length () != DDR_NUM_DIR_VECTS (ddr
))
4125 fprintf (file
, "\n(Number of direction vectors differ: Banerjee has %d, Omega has %d.)\n",
4126 dir_vects
.length (),
4127 DDR_NUM_DIR_VECTS (ddr
));
4131 for (i
= 0; i
< DDR_NUM_DIST_VECTS (ddr
); i
++)
4133 lambda_vector a_dist_v
;
4134 lambda_vector b_dist_v
= DDR_DIST_VECT (ddr
, i
);
4136 /* Distance vectors are not ordered in the same way in the DDR
4137 and in the DIST_VECTS: search for a matching vector. */
4138 FOR_EACH_VEC_ELT (dist_vects
, j
, a_dist_v
)
4139 if (lambda_vector_equal (a_dist_v
, b_dist_v
, DDR_NB_LOOPS (ddr
)))
4142 if (j
== dist_vects
.length ())
4144 fprintf (file
, "\n(Dist vectors from the first dependence analyzer:\n");
4145 print_dist_vectors (file
, dist_vects
, DDR_NB_LOOPS (ddr
));
4146 fprintf (file
, "not found in Omega dist vectors:\n");
4147 print_dist_vectors (file
, DDR_DIST_VECTS (ddr
), DDR_NB_LOOPS (ddr
));
4148 fprintf (file
, "data dependence relation:\n");
4149 dump_data_dependence_relation (file
, ddr
);
4150 fprintf (file
, ")\n");
4154 for (i
= 0; i
< DDR_NUM_DIR_VECTS (ddr
); i
++)
4156 lambda_vector a_dir_v
;
4157 lambda_vector b_dir_v
= DDR_DIR_VECT (ddr
, i
);
4159 /* Direction vectors are not ordered in the same way in the DDR
4160 and in the DIR_VECTS: search for a matching vector. */
4161 FOR_EACH_VEC_ELT (dir_vects
, j
, a_dir_v
)
4162 if (lambda_vector_equal (a_dir_v
, b_dir_v
, DDR_NB_LOOPS (ddr
)))
4165 if (j
== dist_vects
.length ())
4167 fprintf (file
, "\n(Dir vectors from the first dependence analyzer:\n");
4168 print_dir_vectors (file
, dir_vects
, DDR_NB_LOOPS (ddr
));
4169 fprintf (file
, "not found in Omega dir vectors:\n");
4170 print_dir_vectors (file
, DDR_DIR_VECTS (ddr
), DDR_NB_LOOPS (ddr
));
4171 fprintf (file
, "data dependence relation:\n");
4172 dump_data_dependence_relation (file
, ddr
);
4173 fprintf (file
, ")\n");
4180 /* This computes the affine dependence relation between A and B with
4181 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
4182 independence between two accesses, while CHREC_DONT_KNOW is used
4183 for representing the unknown relation.
4185 Note that it is possible to stop the computation of the dependence
4186 relation the first time we detect a CHREC_KNOWN element for a given
4190 compute_affine_dependence (struct data_dependence_relation
*ddr
,
4191 struct loop
*loop_nest
)
4193 struct data_reference
*dra
= DDR_A (ddr
);
4194 struct data_reference
*drb
= DDR_B (ddr
);
4196 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4198 fprintf (dump_file
, "(compute_affine_dependence\n");
4199 fprintf (dump_file
, " stmt_a: ");
4200 print_gimple_stmt (dump_file
, DR_STMT (dra
), 0, TDF_SLIM
);
4201 fprintf (dump_file
, " stmt_b: ");
4202 print_gimple_stmt (dump_file
, DR_STMT (drb
), 0, TDF_SLIM
);
4205 /* Analyze only when the dependence relation is not yet known. */
4206 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
4208 dependence_stats
.num_dependence_tests
++;
4210 if (access_functions_are_affine_or_constant_p (dra
, loop_nest
)
4211 && access_functions_are_affine_or_constant_p (drb
, loop_nest
))
4213 subscript_dependence_tester (ddr
, loop_nest
);
4215 if (flag_check_data_deps
)
4217 /* Dump the dependences from the first algorithm. */
4218 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4220 fprintf (dump_file
, "\n\nBanerjee Analyzer\n");
4221 dump_data_dependence_relation (dump_file
, ddr
);
4224 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
4226 bool maybe_dependent
;
4227 vec
<lambda_vector
> dir_vects
, dist_vects
;
4229 /* Save the result of the first DD analyzer. */
4230 dist_vects
= DDR_DIST_VECTS (ddr
);
4231 dir_vects
= DDR_DIR_VECTS (ddr
);
4233 /* Reset the information. */
4234 DDR_DIST_VECTS (ddr
).create (0);
4235 DDR_DIR_VECTS (ddr
).create (0);
4237 /* Compute the same information using Omega. */
4238 if (!init_omega_for_ddr (ddr
, &maybe_dependent
))
4239 goto csys_dont_know
;
4241 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4243 fprintf (dump_file
, "Omega Analyzer\n");
4244 dump_data_dependence_relation (dump_file
, ddr
);
4247 /* Check that we get the same information. */
4248 if (maybe_dependent
)
4249 gcc_assert (ddr_consistent_p (stderr
, ddr
, dist_vects
,
4255 /* As a last case, if the dependence cannot be determined, or if
4256 the dependence is considered too difficult to determine, answer
4261 dependence_stats
.num_dependence_undetermined
++;
4263 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4265 fprintf (dump_file
, "Data ref a:\n");
4266 dump_data_reference (dump_file
, dra
);
4267 fprintf (dump_file
, "Data ref b:\n");
4268 dump_data_reference (dump_file
, drb
);
4269 fprintf (dump_file
, "affine dependence test not usable: access function not affine or constant.\n");
4271 finalize_ddr_dependent (ddr
, chrec_dont_know
);
4275 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4277 if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
4278 fprintf (dump_file
, ") -> no dependence\n");
4279 else if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
4280 fprintf (dump_file
, ") -> dependence analysis failed\n");
4282 fprintf (dump_file
, ")\n");
4286 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
4287 the data references in DATAREFS, in the LOOP_NEST. When
4288 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
4289 relations. Return true when successful, i.e. data references number
4290 is small enough to be handled. */
4293 compute_all_dependences (vec
<data_reference_p
> datarefs
,
4294 vec
<ddr_p
> *dependence_relations
,
4295 vec
<loop_p
> loop_nest
,
4296 bool compute_self_and_rr
)
4298 struct data_dependence_relation
*ddr
;
4299 struct data_reference
*a
, *b
;
4302 if ((int) datarefs
.length ()
4303 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS
))
4305 struct data_dependence_relation
*ddr
;
4307 /* Insert a single relation into dependence_relations:
4309 ddr
= initialize_data_dependence_relation (NULL
, NULL
, loop_nest
);
4310 dependence_relations
->safe_push (ddr
);
4314 FOR_EACH_VEC_ELT (datarefs
, i
, a
)
4315 for (j
= i
+ 1; datarefs
.iterate (j
, &b
); j
++)
4316 if (DR_IS_WRITE (a
) || DR_IS_WRITE (b
) || compute_self_and_rr
)
4318 ddr
= initialize_data_dependence_relation (a
, b
, loop_nest
);
4319 dependence_relations
->safe_push (ddr
);
4320 if (loop_nest
.exists ())
4321 compute_affine_dependence (ddr
, loop_nest
[0]);
4324 if (compute_self_and_rr
)
4325 FOR_EACH_VEC_ELT (datarefs
, i
, a
)
4327 ddr
= initialize_data_dependence_relation (a
, a
, loop_nest
);
4328 dependence_relations
->safe_push (ddr
);
4329 if (loop_nest
.exists ())
4330 compute_affine_dependence (ddr
, loop_nest
[0]);
4336 /* Describes a location of a memory reference. */
4338 typedef struct data_ref_loc_d
4340 /* The memory reference. */
4343 /* True if the memory reference is read. */
4348 /* Stores the locations of memory references in STMT to REFERENCES. Returns
4349 true if STMT clobbers memory, false otherwise. */
4352 get_references_in_stmt (gimple stmt
, vec
<data_ref_loc
, va_heap
> *references
)
4354 bool clobbers_memory
= false;
4357 enum gimple_code stmt_code
= gimple_code (stmt
);
4359 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4360 As we cannot model data-references to not spelled out
4361 accesses give up if they may occur. */
4362 if (stmt_code
== GIMPLE_CALL
4363 && !(gimple_call_flags (stmt
) & ECF_CONST
))
4365 /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
4366 if (gimple_call_internal_p (stmt
))
4367 switch (gimple_call_internal_fn (stmt
))
4369 case IFN_GOMP_SIMD_LANE
:
4371 struct loop
*loop
= gimple_bb (stmt
)->loop_father
;
4372 tree uid
= gimple_call_arg (stmt
, 0);
4373 gcc_assert (TREE_CODE (uid
) == SSA_NAME
);
4375 || loop
->simduid
!= SSA_NAME_VAR (uid
))
4376 clobbers_memory
= true;
4380 case IFN_MASK_STORE
:
4383 clobbers_memory
= true;
4387 clobbers_memory
= true;
4389 else if (stmt_code
== GIMPLE_ASM
4390 && (gimple_asm_volatile_p (stmt
) || gimple_vuse (stmt
)))
4391 clobbers_memory
= true;
4393 if (!gimple_vuse (stmt
))
4394 return clobbers_memory
;
4396 if (stmt_code
== GIMPLE_ASSIGN
)
4399 op0
= gimple_assign_lhs (stmt
);
4400 op1
= gimple_assign_rhs1 (stmt
);
4403 || (REFERENCE_CLASS_P (op1
)
4404 && (base
= get_base_address (op1
))
4405 && TREE_CODE (base
) != SSA_NAME
))
4409 references
->safe_push (ref
);
4412 else if (stmt_code
== GIMPLE_CALL
)
4416 ref
.is_read
= false;
4417 if (gimple_call_internal_p (stmt
))
4418 switch (gimple_call_internal_fn (stmt
))
4421 if (gimple_call_lhs (stmt
) == NULL_TREE
)
4424 case IFN_MASK_STORE
:
4425 ref
.ref
= fold_build2 (MEM_REF
,
4427 ? TREE_TYPE (gimple_call_lhs (stmt
))
4428 : TREE_TYPE (gimple_call_arg (stmt
, 3)),
4429 gimple_call_arg (stmt
, 0),
4430 gimple_call_arg (stmt
, 1));
4431 references
->safe_push (ref
);
4437 op0
= gimple_call_lhs (stmt
);
4438 n
= gimple_call_num_args (stmt
);
4439 for (i
= 0; i
< n
; i
++)
4441 op1
= gimple_call_arg (stmt
, i
);
4444 || (REFERENCE_CLASS_P (op1
) && get_base_address (op1
)))
4448 references
->safe_push (ref
);
4453 return clobbers_memory
;
4457 || (REFERENCE_CLASS_P (op0
) && get_base_address (op0
))))
4460 ref
.is_read
= false;
4461 references
->safe_push (ref
);
4463 return clobbers_memory
;
4466 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
4467 reference, returns false, otherwise returns true. NEST is the outermost
4468 loop of the loop nest in which the references should be analyzed. */
4471 find_data_references_in_stmt (struct loop
*nest
, gimple stmt
,
4472 vec
<data_reference_p
> *datarefs
)
4475 auto_vec
<data_ref_loc
, 2> references
;
4478 data_reference_p dr
;
4480 if (get_references_in_stmt (stmt
, &references
))
4483 FOR_EACH_VEC_ELT (references
, i
, ref
)
4485 dr
= create_data_ref (nest
, loop_containing_stmt (stmt
),
4486 ref
->ref
, stmt
, ref
->is_read
);
4487 gcc_assert (dr
!= NULL
);
4488 datarefs
->safe_push (dr
);
4490 references
.release ();
4494 /* Stores the data references in STMT to DATAREFS. If there is an
4495 unanalyzable reference, returns false, otherwise returns true.
4496 NEST is the outermost loop of the loop nest in which the references
4497 should be instantiated, LOOP is the loop in which the references
4498 should be analyzed. */
4501 graphite_find_data_references_in_stmt (loop_p nest
, loop_p loop
, gimple stmt
,
4502 vec
<data_reference_p
> *datarefs
)
4505 auto_vec
<data_ref_loc
, 2> references
;
4508 data_reference_p dr
;
4510 if (get_references_in_stmt (stmt
, &references
))
4513 FOR_EACH_VEC_ELT (references
, i
, ref
)
4515 dr
= create_data_ref (nest
, loop
, ref
->ref
, stmt
, ref
->is_read
);
4516 gcc_assert (dr
!= NULL
);
4517 datarefs
->safe_push (dr
);
4520 references
.release ();
4524 /* Search the data references in LOOP, and record the information into
4525 DATAREFS. Returns chrec_dont_know when failing to analyze a
4526 difficult case, returns NULL_TREE otherwise. */
4529 find_data_references_in_bb (struct loop
*loop
, basic_block bb
,
4530 vec
<data_reference_p
> *datarefs
)
4532 gimple_stmt_iterator bsi
;
4534 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
4536 gimple stmt
= gsi_stmt (bsi
);
4538 if (!find_data_references_in_stmt (loop
, stmt
, datarefs
))
4540 struct data_reference
*res
;
4541 res
= XCNEW (struct data_reference
);
4542 datarefs
->safe_push (res
);
4544 return chrec_dont_know
;
4551 /* Search the data references in LOOP, and record the information into
4552 DATAREFS. Returns chrec_dont_know when failing to analyze a
4553 difficult case, returns NULL_TREE otherwise.
4555 TODO: This function should be made smarter so that it can handle address
4556 arithmetic as if they were array accesses, etc. */
4559 find_data_references_in_loop (struct loop
*loop
,
4560 vec
<data_reference_p
> *datarefs
)
4562 basic_block bb
, *bbs
;
4565 bbs
= get_loop_body_in_dom_order (loop
);
4567 for (i
= 0; i
< loop
->num_nodes
; i
++)
4571 if (find_data_references_in_bb (loop
, bb
, datarefs
) == chrec_dont_know
)
4574 return chrec_dont_know
;
4582 /* Recursive helper function. */
4585 find_loop_nest_1 (struct loop
*loop
, vec
<loop_p
> *loop_nest
)
4587 /* Inner loops of the nest should not contain siblings. Example:
4588 when there are two consecutive loops,
4599 the dependence relation cannot be captured by the distance
4604 loop_nest
->safe_push (loop
);
4606 return find_loop_nest_1 (loop
->inner
, loop_nest
);
4610 /* Return false when the LOOP is not well nested. Otherwise return
4611 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4612 contain the loops from the outermost to the innermost, as they will
4613 appear in the classic distance vector. */
4616 find_loop_nest (struct loop
*loop
, vec
<loop_p
> *loop_nest
)
4618 loop_nest
->safe_push (loop
);
4620 return find_loop_nest_1 (loop
->inner
, loop_nest
);
4624 /* Returns true when the data dependences have been computed, false otherwise.
4625 Given a loop nest LOOP, the following vectors are returned:
4626 DATAREFS is initialized to all the array elements contained in this loop,
4627 DEPENDENCE_RELATIONS contains the relations between the data references.
4628 Compute read-read and self relations if
4629 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4632 compute_data_dependences_for_loop (struct loop
*loop
,
4633 bool compute_self_and_read_read_dependences
,
4634 vec
<loop_p
> *loop_nest
,
4635 vec
<data_reference_p
> *datarefs
,
4636 vec
<ddr_p
> *dependence_relations
)
4640 memset (&dependence_stats
, 0, sizeof (dependence_stats
));
4642 /* If the loop nest is not well formed, or one of the data references
4643 is not computable, give up without spending time to compute other
4646 || !find_loop_nest (loop
, loop_nest
)
4647 || find_data_references_in_loop (loop
, datarefs
) == chrec_dont_know
4648 || !compute_all_dependences (*datarefs
, dependence_relations
, *loop_nest
,
4649 compute_self_and_read_read_dependences
))
4652 if (dump_file
&& (dump_flags
& TDF_STATS
))
4654 fprintf (dump_file
, "Dependence tester statistics:\n");
4656 fprintf (dump_file
, "Number of dependence tests: %d\n",
4657 dependence_stats
.num_dependence_tests
);
4658 fprintf (dump_file
, "Number of dependence tests classified dependent: %d\n",
4659 dependence_stats
.num_dependence_dependent
);
4660 fprintf (dump_file
, "Number of dependence tests classified independent: %d\n",
4661 dependence_stats
.num_dependence_independent
);
4662 fprintf (dump_file
, "Number of undetermined dependence tests: %d\n",
4663 dependence_stats
.num_dependence_undetermined
);
4665 fprintf (dump_file
, "Number of subscript tests: %d\n",
4666 dependence_stats
.num_subscript_tests
);
4667 fprintf (dump_file
, "Number of undetermined subscript tests: %d\n",
4668 dependence_stats
.num_subscript_undetermined
);
4669 fprintf (dump_file
, "Number of same subscript function: %d\n",
4670 dependence_stats
.num_same_subscript_function
);
4672 fprintf (dump_file
, "Number of ziv tests: %d\n",
4673 dependence_stats
.num_ziv
);
4674 fprintf (dump_file
, "Number of ziv tests returning dependent: %d\n",
4675 dependence_stats
.num_ziv_dependent
);
4676 fprintf (dump_file
, "Number of ziv tests returning independent: %d\n",
4677 dependence_stats
.num_ziv_independent
);
4678 fprintf (dump_file
, "Number of ziv tests unimplemented: %d\n",
4679 dependence_stats
.num_ziv_unimplemented
);
4681 fprintf (dump_file
, "Number of siv tests: %d\n",
4682 dependence_stats
.num_siv
);
4683 fprintf (dump_file
, "Number of siv tests returning dependent: %d\n",
4684 dependence_stats
.num_siv_dependent
);
4685 fprintf (dump_file
, "Number of siv tests returning independent: %d\n",
4686 dependence_stats
.num_siv_independent
);
4687 fprintf (dump_file
, "Number of siv tests unimplemented: %d\n",
4688 dependence_stats
.num_siv_unimplemented
);
4690 fprintf (dump_file
, "Number of miv tests: %d\n",
4691 dependence_stats
.num_miv
);
4692 fprintf (dump_file
, "Number of miv tests returning dependent: %d\n",
4693 dependence_stats
.num_miv_dependent
);
4694 fprintf (dump_file
, "Number of miv tests returning independent: %d\n",
4695 dependence_stats
.num_miv_independent
);
4696 fprintf (dump_file
, "Number of miv tests unimplemented: %d\n",
4697 dependence_stats
.num_miv_unimplemented
);
4703 /* Returns true when the data dependences for the basic block BB have been
4704 computed, false otherwise.
4705 DATAREFS is initialized to all the array elements contained in this basic
4706 block, DEPENDENCE_RELATIONS contains the relations between the data
4707 references. Compute read-read and self relations if
4708 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4710 compute_data_dependences_for_bb (basic_block bb
,
4711 bool compute_self_and_read_read_dependences
,
4712 vec
<data_reference_p
> *datarefs
,
4713 vec
<ddr_p
> *dependence_relations
)
4715 if (find_data_references_in_bb (NULL
, bb
, datarefs
) == chrec_dont_know
)
4718 return compute_all_dependences (*datarefs
, dependence_relations
, vNULL
,
4719 compute_self_and_read_read_dependences
);
4722 /* Entry point (for testing only). Analyze all the data references
4723 and the dependence relations in LOOP.
4725 The data references are computed first.
4727 A relation on these nodes is represented by a complete graph. Some
4728 of the relations could be of no interest, thus the relations can be
4731 In the following function we compute all the relations. This is
4732 just a first implementation that is here for:
4733 - for showing how to ask for the dependence relations,
4734 - for the debugging the whole dependence graph,
4735 - for the dejagnu testcases and maintenance.
4737 It is possible to ask only for a part of the graph, avoiding to
4738 compute the whole dependence graph. The computed dependences are
4739 stored in a knowledge base (KB) such that later queries don't
4740 recompute the same information. The implementation of this KB is
4741 transparent to the optimizer, and thus the KB can be changed with a
4742 more efficient implementation, or the KB could be disabled. */
4744 analyze_all_data_dependences (struct loop
*loop
)
4747 int nb_data_refs
= 10;
4748 vec
<data_reference_p
> datarefs
;
4749 datarefs
.create (nb_data_refs
);
4750 vec
<ddr_p
> dependence_relations
;
4751 dependence_relations
.create (nb_data_refs
* nb_data_refs
);
4752 vec
<loop_p
> loop_nest
;
4753 loop_nest
.create (3);
4755 /* Compute DDs on the whole function. */
4756 compute_data_dependences_for_loop (loop
, false, &loop_nest
, &datarefs
,
4757 &dependence_relations
);
4761 dump_data_dependence_relations (dump_file
, dependence_relations
);
4762 fprintf (dump_file
, "\n\n");
4764 if (dump_flags
& TDF_DETAILS
)
4765 dump_dist_dir_vectors (dump_file
, dependence_relations
);
4767 if (dump_flags
& TDF_STATS
)
4769 unsigned nb_top_relations
= 0;
4770 unsigned nb_bot_relations
= 0;
4771 unsigned nb_chrec_relations
= 0;
4772 struct data_dependence_relation
*ddr
;
4774 FOR_EACH_VEC_ELT (dependence_relations
, i
, ddr
)
4776 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr
)))
4779 else if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
4783 nb_chrec_relations
++;
4786 gather_stats_on_scev_database ();
4790 loop_nest
.release ();
4791 free_dependence_relations (dependence_relations
);
4792 free_data_refs (datarefs
);
4795 /* Computes all the data dependences and check that the results of
4796 several analyzers are the same. */
4799 tree_check_data_deps (void)
4801 struct loop
*loop_nest
;
4803 FOR_EACH_LOOP (loop_nest
, 0)
4804 analyze_all_data_dependences (loop_nest
);
4807 /* Free the memory used by a data dependence relation DDR. */
4810 free_dependence_relation (struct data_dependence_relation
*ddr
)
4815 if (DDR_SUBSCRIPTS (ddr
).exists ())
4816 free_subscripts (DDR_SUBSCRIPTS (ddr
));
4817 DDR_DIST_VECTS (ddr
).release ();
4818 DDR_DIR_VECTS (ddr
).release ();
4823 /* Free the memory used by the data dependence relations from
4824 DEPENDENCE_RELATIONS. */
4827 free_dependence_relations (vec
<ddr_p
> dependence_relations
)
4830 struct data_dependence_relation
*ddr
;
4832 FOR_EACH_VEC_ELT (dependence_relations
, i
, ddr
)
4834 free_dependence_relation (ddr
);
4836 dependence_relations
.release ();
4839 /* Free the memory used by the data references from DATAREFS. */
4842 free_data_refs (vec
<data_reference_p
> datarefs
)
4845 struct data_reference
*dr
;
4847 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
4849 datarefs
.release ();