1 /* Data references and dependences detectors.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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"
79 #include "gimple-pretty-print.h"
80 #include "tree-flow.h"
82 #include "tree-data-ref.h"
83 #include "tree-scalar-evolution.h"
85 #include "langhooks.h"
86 #include "tree-affine.h"
89 static struct datadep_stats
91 int num_dependence_tests
;
92 int num_dependence_dependent
;
93 int num_dependence_independent
;
94 int num_dependence_undetermined
;
96 int num_subscript_tests
;
97 int num_subscript_undetermined
;
98 int num_same_subscript_function
;
101 int num_ziv_independent
;
102 int num_ziv_dependent
;
103 int num_ziv_unimplemented
;
106 int num_siv_independent
;
107 int num_siv_dependent
;
108 int num_siv_unimplemented
;
111 int num_miv_independent
;
112 int num_miv_dependent
;
113 int num_miv_unimplemented
;
116 static bool subscript_dependence_tester_1 (struct data_dependence_relation
*,
117 struct data_reference
*,
118 struct data_reference
*,
120 /* Returns true iff A divides B. */
123 tree_fold_divides_p (const_tree a
, const_tree b
)
125 gcc_assert (TREE_CODE (a
) == INTEGER_CST
);
126 gcc_assert (TREE_CODE (b
) == INTEGER_CST
);
127 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR
, b
, a
));
130 /* Returns true iff A divides B. */
133 int_divides_p (int a
, int b
)
135 return ((b
% a
) == 0);
140 /* Dump into FILE all the data references from DATAREFS. */
143 dump_data_references (FILE *file
, vec
<data_reference_p
> datarefs
)
146 struct data_reference
*dr
;
148 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
149 dump_data_reference (file
, dr
);
152 /* Dump into STDERR all the data references from DATAREFS. */
155 debug_data_references (vec
<data_reference_p
> datarefs
)
157 dump_data_references (stderr
, datarefs
);
160 /* Print to STDERR the data_reference DR. */
163 debug_data_reference (struct data_reference
*dr
)
165 dump_data_reference (stderr
, dr
);
168 /* Dump function for a DATA_REFERENCE structure. */
171 dump_data_reference (FILE *outf
,
172 struct data_reference
*dr
)
176 fprintf (outf
, "#(Data Ref: \n");
177 fprintf (outf
, "# bb: %d \n", gimple_bb (DR_STMT (dr
))->index
);
178 fprintf (outf
, "# stmt: ");
179 print_gimple_stmt (outf
, DR_STMT (dr
), 0, 0);
180 fprintf (outf
, "# ref: ");
181 print_generic_stmt (outf
, DR_REF (dr
), 0);
182 fprintf (outf
, "# base_object: ");
183 print_generic_stmt (outf
, DR_BASE_OBJECT (dr
), 0);
185 for (i
= 0; i
< DR_NUM_DIMENSIONS (dr
); i
++)
187 fprintf (outf
, "# Access function %d: ", i
);
188 print_generic_stmt (outf
, DR_ACCESS_FN (dr
, i
), 0);
190 fprintf (outf
, "#)\n");
193 /* Dumps the affine function described by FN to the file OUTF. */
196 dump_affine_function (FILE *outf
, affine_fn fn
)
201 print_generic_expr (outf
, fn
[0], TDF_SLIM
);
202 for (i
= 1; fn
.iterate (i
, &coef
); i
++)
204 fprintf (outf
, " + ");
205 print_generic_expr (outf
, coef
, TDF_SLIM
);
206 fprintf (outf
, " * x_%u", i
);
210 /* Dumps the conflict function CF to the file OUTF. */
213 dump_conflict_function (FILE *outf
, conflict_function
*cf
)
217 if (cf
->n
== NO_DEPENDENCE
)
218 fprintf (outf
, "no dependence");
219 else if (cf
->n
== NOT_KNOWN
)
220 fprintf (outf
, "not known");
223 for (i
= 0; i
< cf
->n
; i
++)
228 dump_affine_function (outf
, cf
->fns
[i
]);
234 /* Dump function for a SUBSCRIPT structure. */
237 dump_subscript (FILE *outf
, struct subscript
*subscript
)
239 conflict_function
*cf
= SUB_CONFLICTS_IN_A (subscript
);
241 fprintf (outf
, "\n (subscript \n");
242 fprintf (outf
, " iterations_that_access_an_element_twice_in_A: ");
243 dump_conflict_function (outf
, cf
);
244 if (CF_NONTRIVIAL_P (cf
))
246 tree last_iteration
= SUB_LAST_CONFLICT (subscript
);
247 fprintf (outf
, "\n last_conflict: ");
248 print_generic_expr (outf
, last_iteration
, 0);
251 cf
= SUB_CONFLICTS_IN_B (subscript
);
252 fprintf (outf
, "\n iterations_that_access_an_element_twice_in_B: ");
253 dump_conflict_function (outf
, cf
);
254 if (CF_NONTRIVIAL_P (cf
))
256 tree last_iteration
= SUB_LAST_CONFLICT (subscript
);
257 fprintf (outf
, "\n last_conflict: ");
258 print_generic_expr (outf
, last_iteration
, 0);
261 fprintf (outf
, "\n (Subscript distance: ");
262 print_generic_expr (outf
, SUB_DISTANCE (subscript
), 0);
263 fprintf (outf
, " ))\n");
266 /* Print the classic direction vector DIRV to OUTF. */
269 print_direction_vector (FILE *outf
,
275 for (eq
= 0; eq
< length
; eq
++)
277 enum data_dependence_direction dir
= ((enum data_dependence_direction
)
283 fprintf (outf
, " +");
286 fprintf (outf
, " -");
289 fprintf (outf
, " =");
291 case dir_positive_or_equal
:
292 fprintf (outf
, " +=");
294 case dir_positive_or_negative
:
295 fprintf (outf
, " +-");
297 case dir_negative_or_equal
:
298 fprintf (outf
, " -=");
301 fprintf (outf
, " *");
304 fprintf (outf
, "indep");
308 fprintf (outf
, "\n");
311 /* Print a vector of direction vectors. */
314 print_dir_vectors (FILE *outf
, vec
<lambda_vector
> dir_vects
,
320 FOR_EACH_VEC_ELT (dir_vects
, j
, v
)
321 print_direction_vector (outf
, v
, length
);
324 /* Print out a vector VEC of length N to OUTFILE. */
327 print_lambda_vector (FILE * outfile
, lambda_vector vector
, int n
)
331 for (i
= 0; i
< n
; i
++)
332 fprintf (outfile
, "%3d ", vector
[i
]);
333 fprintf (outfile
, "\n");
336 /* Print a vector of distance vectors. */
339 print_dist_vectors (FILE *outf
, vec
<lambda_vector
> dist_vects
,
345 FOR_EACH_VEC_ELT (dist_vects
, j
, v
)
346 print_lambda_vector (outf
, v
, length
);
349 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
352 dump_data_dependence_relation (FILE *outf
,
353 struct data_dependence_relation
*ddr
)
355 struct data_reference
*dra
, *drb
;
357 fprintf (outf
, "(Data Dep: \n");
359 if (!ddr
|| DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
366 dump_data_reference (outf
, dra
);
368 fprintf (outf
, " (nil)\n");
370 dump_data_reference (outf
, drb
);
372 fprintf (outf
, " (nil)\n");
374 fprintf (outf
, " (don't know)\n)\n");
380 dump_data_reference (outf
, dra
);
381 dump_data_reference (outf
, drb
);
383 if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
384 fprintf (outf
, " (no dependence)\n");
386 else if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
391 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
393 fprintf (outf
, " access_fn_A: ");
394 print_generic_stmt (outf
, DR_ACCESS_FN (dra
, i
), 0);
395 fprintf (outf
, " access_fn_B: ");
396 print_generic_stmt (outf
, DR_ACCESS_FN (drb
, i
), 0);
397 dump_subscript (outf
, DDR_SUBSCRIPT (ddr
, i
));
400 fprintf (outf
, " inner loop index: %d\n", DDR_INNER_LOOP (ddr
));
401 fprintf (outf
, " loop nest: (");
402 FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr
), i
, loopi
)
403 fprintf (outf
, "%d ", loopi
->num
);
404 fprintf (outf
, ")\n");
406 for (i
= 0; i
< DDR_NUM_DIST_VECTS (ddr
); i
++)
408 fprintf (outf
, " distance_vector: ");
409 print_lambda_vector (outf
, DDR_DIST_VECT (ddr
, i
),
413 for (i
= 0; i
< DDR_NUM_DIR_VECTS (ddr
); i
++)
415 fprintf (outf
, " direction_vector: ");
416 print_direction_vector (outf
, DDR_DIR_VECT (ddr
, i
),
421 fprintf (outf
, ")\n");
427 debug_data_dependence_relation (struct data_dependence_relation
*ddr
)
429 dump_data_dependence_relation (stderr
, ddr
);
432 /* Dump into FILE all the dependence relations from DDRS. */
435 dump_data_dependence_relations (FILE *file
,
439 struct data_dependence_relation
*ddr
;
441 FOR_EACH_VEC_ELT (ddrs
, i
, ddr
)
442 dump_data_dependence_relation (file
, ddr
);
445 /* Dump to STDERR all the dependence relations from DDRS. */
448 debug_data_dependence_relations (vec
<ddr_p
> ddrs
)
450 dump_data_dependence_relations (stderr
, ddrs
);
453 /* Dumps the distance and direction vectors in FILE. DDRS contains
454 the dependence relations, and VECT_SIZE is the size of the
455 dependence vectors, or in other words the number of loops in the
459 dump_dist_dir_vectors (FILE *file
, vec
<ddr_p
> ddrs
)
462 struct data_dependence_relation
*ddr
;
465 FOR_EACH_VEC_ELT (ddrs
, i
, ddr
)
466 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
&& DDR_AFFINE_P (ddr
))
468 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr
), j
, v
)
470 fprintf (file
, "DISTANCE_V (");
471 print_lambda_vector (file
, v
, DDR_NB_LOOPS (ddr
));
472 fprintf (file
, ")\n");
475 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr
), j
, v
)
477 fprintf (file
, "DIRECTION_V (");
478 print_direction_vector (file
, v
, DDR_NB_LOOPS (ddr
));
479 fprintf (file
, ")\n");
483 fprintf (file
, "\n\n");
486 /* Dumps the data dependence relations DDRS in FILE. */
489 dump_ddrs (FILE *file
, vec
<ddr_p
> ddrs
)
492 struct data_dependence_relation
*ddr
;
494 FOR_EACH_VEC_ELT (ddrs
, i
, ddr
)
495 dump_data_dependence_relation (file
, ddr
);
497 fprintf (file
, "\n\n");
501 debug_ddrs (vec
<ddr_p
> ddrs
)
503 dump_ddrs (stderr
, ddrs
);
506 /* Helper function for split_constant_offset. Expresses OP0 CODE OP1
507 (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
508 constant of type ssizetype, and returns true. If we cannot do this
509 with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
513 split_constant_offset_1 (tree type
, tree op0
, enum tree_code code
, tree op1
,
514 tree
*var
, tree
*off
)
518 enum tree_code ocode
= code
;
526 *var
= build_int_cst (type
, 0);
527 *off
= fold_convert (ssizetype
, op0
);
530 case POINTER_PLUS_EXPR
:
535 split_constant_offset (op0
, &var0
, &off0
);
536 split_constant_offset (op1
, &var1
, &off1
);
537 *var
= fold_build2 (code
, type
, var0
, var1
);
538 *off
= size_binop (ocode
, off0
, off1
);
542 if (TREE_CODE (op1
) != INTEGER_CST
)
545 split_constant_offset (op0
, &var0
, &off0
);
546 *var
= fold_build2 (MULT_EXPR
, type
, var0
, op1
);
547 *off
= size_binop (MULT_EXPR
, off0
, fold_convert (ssizetype
, op1
));
553 HOST_WIDE_INT pbitsize
, pbitpos
;
554 enum machine_mode pmode
;
555 int punsignedp
, pvolatilep
;
557 op0
= TREE_OPERAND (op0
, 0);
558 base
= get_inner_reference (op0
, &pbitsize
, &pbitpos
, &poffset
,
559 &pmode
, &punsignedp
, &pvolatilep
, false);
561 if (pbitpos
% BITS_PER_UNIT
!= 0)
563 base
= build_fold_addr_expr (base
);
564 off0
= ssize_int (pbitpos
/ BITS_PER_UNIT
);
568 split_constant_offset (poffset
, &poffset
, &off1
);
569 off0
= size_binop (PLUS_EXPR
, off0
, off1
);
570 if (POINTER_TYPE_P (TREE_TYPE (base
)))
571 base
= fold_build_pointer_plus (base
, poffset
);
573 base
= fold_build2 (PLUS_EXPR
, TREE_TYPE (base
), base
,
574 fold_convert (TREE_TYPE (base
), poffset
));
577 var0
= fold_convert (type
, base
);
579 /* If variable length types are involved, punt, otherwise casts
580 might be converted into ARRAY_REFs in gimplify_conversion.
581 To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
582 possibly no longer appears in current GIMPLE, might resurface.
583 This perhaps could run
584 if (CONVERT_EXPR_P (var0))
586 gimplify_conversion (&var0);
587 // Attempt to fill in any within var0 found ARRAY_REF's
588 // element size from corresponding op embedded ARRAY_REF,
589 // if unsuccessful, just punt.
591 while (POINTER_TYPE_P (type
))
592 type
= TREE_TYPE (type
);
593 if (int_size_in_bytes (type
) < 0)
603 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
604 enum tree_code subcode
;
606 if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
609 var0
= gimple_assign_rhs1 (def_stmt
);
610 subcode
= gimple_assign_rhs_code (def_stmt
);
611 var1
= gimple_assign_rhs2 (def_stmt
);
613 return split_constant_offset_1 (type
, var0
, subcode
, var1
, var
, off
);
617 /* We must not introduce undefined overflow, and we must not change the value.
618 Hence we're okay if the inner type doesn't overflow to start with
619 (pointer or signed), the outer type also is an integer or pointer
620 and the outer precision is at least as large as the inner. */
621 tree itype
= TREE_TYPE (op0
);
622 if ((POINTER_TYPE_P (itype
)
623 || (INTEGRAL_TYPE_P (itype
) && TYPE_OVERFLOW_UNDEFINED (itype
)))
624 && TYPE_PRECISION (type
) >= TYPE_PRECISION (itype
)
625 && (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)))
627 split_constant_offset (op0
, &var0
, off
);
628 *var
= fold_convert (type
, var0
);
639 /* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
640 will be ssizetype. */
643 split_constant_offset (tree exp
, tree
*var
, tree
*off
)
645 tree type
= TREE_TYPE (exp
), otype
, op0
, op1
, e
, o
;
649 *off
= ssize_int (0);
652 if (tree_is_chrec (exp
)
653 || get_gimple_rhs_class (TREE_CODE (exp
)) == GIMPLE_TERNARY_RHS
)
656 otype
= TREE_TYPE (exp
);
657 code
= TREE_CODE (exp
);
658 extract_ops_from_tree (exp
, &code
, &op0
, &op1
);
659 if (split_constant_offset_1 (otype
, op0
, code
, op1
, &e
, &o
))
661 *var
= fold_convert (type
, e
);
666 /* Returns the address ADDR of an object in a canonical shape (without nop
667 casts, and with type of pointer to the object). */
670 canonicalize_base_object_address (tree addr
)
676 /* The base address may be obtained by casting from integer, in that case
678 if (!POINTER_TYPE_P (TREE_TYPE (addr
)))
681 if (TREE_CODE (addr
) != ADDR_EXPR
)
684 return build_fold_addr_expr (TREE_OPERAND (addr
, 0));
687 /* Analyzes the behavior of the memory reference DR in the innermost loop or
688 basic block that contains it. Returns true if analysis succeed or false
692 dr_analyze_innermost (struct data_reference
*dr
, struct loop
*nest
)
694 gimple stmt
= DR_STMT (dr
);
695 struct loop
*loop
= loop_containing_stmt (stmt
);
696 tree ref
= DR_REF (dr
);
697 HOST_WIDE_INT pbitsize
, pbitpos
;
699 enum machine_mode pmode
;
700 int punsignedp
, pvolatilep
;
701 affine_iv base_iv
, offset_iv
;
702 tree init
, dinit
, step
;
703 bool in_loop
= (loop
&& loop
->num
);
705 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
706 fprintf (dump_file
, "analyze_innermost: ");
708 base
= get_inner_reference (ref
, &pbitsize
, &pbitpos
, &poffset
,
709 &pmode
, &punsignedp
, &pvolatilep
, false);
710 gcc_assert (base
!= NULL_TREE
);
712 if (pbitpos
% BITS_PER_UNIT
!= 0)
714 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
715 fprintf (dump_file
, "failed: bit offset alignment.\n");
719 if (TREE_CODE (base
) == MEM_REF
)
721 if (!integer_zerop (TREE_OPERAND (base
, 1)))
723 double_int moff
= mem_ref_offset (base
);
724 tree mofft
= double_int_to_tree (sizetype
, moff
);
728 poffset
= size_binop (PLUS_EXPR
, poffset
, mofft
);
730 base
= TREE_OPERAND (base
, 0);
733 base
= build_fold_addr_expr (base
);
737 if (!simple_iv (loop
, loop_containing_stmt (stmt
), base
, &base_iv
,
738 nest
? true : false))
742 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
743 fprintf (dump_file
, "failed: evolution of base is not"
750 base_iv
.step
= ssize_int (0);
751 base_iv
.no_overflow
= true;
758 base_iv
.step
= ssize_int (0);
759 base_iv
.no_overflow
= true;
764 offset_iv
.base
= ssize_int (0);
765 offset_iv
.step
= ssize_int (0);
771 offset_iv
.base
= poffset
;
772 offset_iv
.step
= ssize_int (0);
774 else if (!simple_iv (loop
, loop_containing_stmt (stmt
),
776 nest
? true : false))
780 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
781 fprintf (dump_file
, "failed: evolution of offset is not"
787 offset_iv
.base
= poffset
;
788 offset_iv
.step
= ssize_int (0);
793 init
= ssize_int (pbitpos
/ BITS_PER_UNIT
);
794 split_constant_offset (base_iv
.base
, &base_iv
.base
, &dinit
);
795 init
= size_binop (PLUS_EXPR
, init
, dinit
);
796 split_constant_offset (offset_iv
.base
, &offset_iv
.base
, &dinit
);
797 init
= size_binop (PLUS_EXPR
, init
, dinit
);
799 step
= size_binop (PLUS_EXPR
,
800 fold_convert (ssizetype
, base_iv
.step
),
801 fold_convert (ssizetype
, offset_iv
.step
));
803 DR_BASE_ADDRESS (dr
) = canonicalize_base_object_address (base_iv
.base
);
805 DR_OFFSET (dr
) = fold_convert (ssizetype
, offset_iv
.base
);
809 DR_ALIGNED_TO (dr
) = size_int (highest_pow2_factor (offset_iv
.base
));
811 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
812 fprintf (dump_file
, "success.\n");
817 /* Determines the base object and the list of indices of memory reference
818 DR, analyzed in LOOP and instantiated in loop nest NEST. */
821 dr_analyze_indices (struct data_reference
*dr
, loop_p nest
, loop_p loop
)
823 vec
<tree
> access_fns
= vNULL
;
825 tree base
, off
, access_fn
;
826 basic_block before_loop
;
828 /* If analyzing a basic-block there are no indices to analyze
829 and thus no access functions. */
832 DR_BASE_OBJECT (dr
) = DR_REF (dr
);
833 DR_ACCESS_FNS (dr
).create (0);
838 before_loop
= block_before_loop (nest
);
840 /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
841 into a two element array with a constant index. The base is
842 then just the immediate underlying object. */
843 if (TREE_CODE (ref
) == REALPART_EXPR
)
845 ref
= TREE_OPERAND (ref
, 0);
846 access_fns
.safe_push (integer_zero_node
);
848 else if (TREE_CODE (ref
) == IMAGPART_EXPR
)
850 ref
= TREE_OPERAND (ref
, 0);
851 access_fns
.safe_push (integer_one_node
);
854 /* Analyze access functions of dimensions we know to be independent. */
855 while (handled_component_p (ref
))
857 if (TREE_CODE (ref
) == ARRAY_REF
)
859 op
= TREE_OPERAND (ref
, 1);
860 access_fn
= analyze_scalar_evolution (loop
, op
);
861 access_fn
= instantiate_scev (before_loop
, loop
, access_fn
);
862 access_fns
.safe_push (access_fn
);
864 else if (TREE_CODE (ref
) == COMPONENT_REF
865 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref
, 0))) == RECORD_TYPE
)
867 /* For COMPONENT_REFs of records (but not unions!) use the
868 FIELD_DECL offset as constant access function so we can
869 disambiguate a[i].f1 and a[i].f2. */
870 tree off
= component_ref_field_offset (ref
);
871 off
= size_binop (PLUS_EXPR
,
872 size_binop (MULT_EXPR
,
873 fold_convert (bitsizetype
, off
),
874 bitsize_int (BITS_PER_UNIT
)),
875 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref
, 1)));
876 access_fns
.safe_push (off
);
879 /* If we have an unhandled component we could not translate
880 to an access function stop analyzing. We have determined
881 our base object in this case. */
884 ref
= TREE_OPERAND (ref
, 0);
887 /* If the address operand of a MEM_REF base has an evolution in the
888 analyzed nest, add it as an additional independent access-function. */
889 if (TREE_CODE (ref
) == MEM_REF
)
891 op
= TREE_OPERAND (ref
, 0);
892 access_fn
= analyze_scalar_evolution (loop
, op
);
893 access_fn
= instantiate_scev (before_loop
, loop
, access_fn
);
894 if (TREE_CODE (access_fn
) == POLYNOMIAL_CHREC
)
897 tree memoff
= TREE_OPERAND (ref
, 1);
898 base
= initial_condition (access_fn
);
899 orig_type
= TREE_TYPE (base
);
900 STRIP_USELESS_TYPE_CONVERSION (base
);
901 split_constant_offset (base
, &base
, &off
);
902 /* Fold the MEM_REF offset into the evolutions initial
903 value to make more bases comparable. */
904 if (!integer_zerop (memoff
))
906 off
= size_binop (PLUS_EXPR
, off
,
907 fold_convert (ssizetype
, memoff
));
908 memoff
= build_int_cst (TREE_TYPE (memoff
), 0);
910 access_fn
= chrec_replace_initial_condition
911 (access_fn
, fold_convert (orig_type
, off
));
912 /* ??? This is still not a suitable base object for
913 dr_may_alias_p - the base object needs to be an
914 access that covers the object as whole. With
915 an evolution in the pointer this cannot be
917 As a band-aid, mark the access so we can special-case
918 it in dr_may_alias_p. */
919 ref
= fold_build2_loc (EXPR_LOCATION (ref
),
920 MEM_REF
, TREE_TYPE (ref
),
922 DR_UNCONSTRAINED_BASE (dr
) = true;
923 access_fns
.safe_push (access_fn
);
926 else if (DECL_P (ref
))
928 /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
929 ref
= build2 (MEM_REF
, TREE_TYPE (ref
),
930 build_fold_addr_expr (ref
),
931 build_int_cst (reference_alias_ptr_type (ref
), 0));
934 DR_BASE_OBJECT (dr
) = ref
;
935 DR_ACCESS_FNS (dr
) = access_fns
;
938 /* Extracts the alias analysis information from the memory reference DR. */
941 dr_analyze_alias (struct data_reference
*dr
)
943 tree ref
= DR_REF (dr
);
944 tree base
= get_base_address (ref
), addr
;
946 if (INDIRECT_REF_P (base
)
947 || TREE_CODE (base
) == MEM_REF
)
949 addr
= TREE_OPERAND (base
, 0);
950 if (TREE_CODE (addr
) == SSA_NAME
)
951 DR_PTR_INFO (dr
) = SSA_NAME_PTR_INFO (addr
);
955 /* Frees data reference DR. */
958 free_data_ref (data_reference_p dr
)
960 DR_ACCESS_FNS (dr
).release ();
964 /* Analyzes memory reference MEMREF accessed in STMT. The reference
965 is read if IS_READ is true, write otherwise. Returns the
966 data_reference description of MEMREF. NEST is the outermost loop
967 in which the reference should be instantiated, LOOP is the loop in
968 which the data reference should be analyzed. */
970 struct data_reference
*
971 create_data_ref (loop_p nest
, loop_p loop
, tree memref
, gimple stmt
,
974 struct data_reference
*dr
;
976 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
978 fprintf (dump_file
, "Creating dr for ");
979 print_generic_expr (dump_file
, memref
, TDF_SLIM
);
980 fprintf (dump_file
, "\n");
983 dr
= XCNEW (struct data_reference
);
985 DR_REF (dr
) = memref
;
986 DR_IS_READ (dr
) = is_read
;
988 dr_analyze_innermost (dr
, nest
);
989 dr_analyze_indices (dr
, nest
, loop
);
990 dr_analyze_alias (dr
);
992 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
995 fprintf (dump_file
, "\tbase_address: ");
996 print_generic_expr (dump_file
, DR_BASE_ADDRESS (dr
), TDF_SLIM
);
997 fprintf (dump_file
, "\n\toffset from base address: ");
998 print_generic_expr (dump_file
, DR_OFFSET (dr
), TDF_SLIM
);
999 fprintf (dump_file
, "\n\tconstant offset from base address: ");
1000 print_generic_expr (dump_file
, DR_INIT (dr
), TDF_SLIM
);
1001 fprintf (dump_file
, "\n\tstep: ");
1002 print_generic_expr (dump_file
, DR_STEP (dr
), TDF_SLIM
);
1003 fprintf (dump_file
, "\n\taligned to: ");
1004 print_generic_expr (dump_file
, DR_ALIGNED_TO (dr
), TDF_SLIM
);
1005 fprintf (dump_file
, "\n\tbase_object: ");
1006 print_generic_expr (dump_file
, DR_BASE_OBJECT (dr
), TDF_SLIM
);
1007 fprintf (dump_file
, "\n");
1008 for (i
= 0; i
< DR_NUM_DIMENSIONS (dr
); i
++)
1010 fprintf (dump_file
, "\tAccess function %d: ", i
);
1011 print_generic_stmt (dump_file
, DR_ACCESS_FN (dr
, i
), TDF_SLIM
);
1018 /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
1021 dr_equal_offsets_p1 (tree offset1
, tree offset2
)
1025 STRIP_NOPS (offset1
);
1026 STRIP_NOPS (offset2
);
1028 if (offset1
== offset2
)
1031 if (TREE_CODE (offset1
) != TREE_CODE (offset2
)
1032 || (!BINARY_CLASS_P (offset1
) && !UNARY_CLASS_P (offset1
)))
1035 res
= dr_equal_offsets_p1 (TREE_OPERAND (offset1
, 0),
1036 TREE_OPERAND (offset2
, 0));
1038 if (!res
|| !BINARY_CLASS_P (offset1
))
1041 res
= dr_equal_offsets_p1 (TREE_OPERAND (offset1
, 1),
1042 TREE_OPERAND (offset2
, 1));
1047 /* Check if DRA and DRB have equal offsets. */
1049 dr_equal_offsets_p (struct data_reference
*dra
,
1050 struct data_reference
*drb
)
1052 tree offset1
, offset2
;
1054 offset1
= DR_OFFSET (dra
);
1055 offset2
= DR_OFFSET (drb
);
1057 return dr_equal_offsets_p1 (offset1
, offset2
);
1060 /* Returns true if FNA == FNB. */
1063 affine_function_equal_p (affine_fn fna
, affine_fn fnb
)
1065 unsigned i
, n
= fna
.length ();
1067 if (n
!= fnb
.length ())
1070 for (i
= 0; i
< n
; i
++)
1071 if (!operand_equal_p (fna
[i
], fnb
[i
], 0))
1077 /* If all the functions in CF are the same, returns one of them,
1078 otherwise returns NULL. */
1081 common_affine_function (conflict_function
*cf
)
1086 if (!CF_NONTRIVIAL_P (cf
))
1091 for (i
= 1; i
< cf
->n
; i
++)
1092 if (!affine_function_equal_p (comm
, cf
->fns
[i
]))
1098 /* Returns the base of the affine function FN. */
1101 affine_function_base (affine_fn fn
)
1106 /* Returns true if FN is a constant. */
1109 affine_function_constant_p (affine_fn fn
)
1114 for (i
= 1; fn
.iterate (i
, &coef
); i
++)
1115 if (!integer_zerop (coef
))
1121 /* Returns true if FN is the zero constant function. */
1124 affine_function_zero_p (affine_fn fn
)
1126 return (integer_zerop (affine_function_base (fn
))
1127 && affine_function_constant_p (fn
));
1130 /* Returns a signed integer type with the largest precision from TA
1134 signed_type_for_types (tree ta
, tree tb
)
1136 if (TYPE_PRECISION (ta
) > TYPE_PRECISION (tb
))
1137 return signed_type_for (ta
);
1139 return signed_type_for (tb
);
1142 /* Applies operation OP on affine functions FNA and FNB, and returns the
1146 affine_fn_op (enum tree_code op
, affine_fn fna
, affine_fn fnb
)
1152 if (fnb
.length () > fna
.length ())
1164 for (i
= 0; i
< n
; i
++)
1166 tree type
= signed_type_for_types (TREE_TYPE (fna
[i
]),
1167 TREE_TYPE (fnb
[i
]));
1168 ret
.quick_push (fold_build2 (op
, type
, fna
[i
], fnb
[i
]));
1171 for (; fna
.iterate (i
, &coef
); i
++)
1172 ret
.quick_push (fold_build2 (op
, signed_type_for (TREE_TYPE (coef
)),
1173 coef
, integer_zero_node
));
1174 for (; fnb
.iterate (i
, &coef
); i
++)
1175 ret
.quick_push (fold_build2 (op
, signed_type_for (TREE_TYPE (coef
)),
1176 integer_zero_node
, coef
));
1181 /* Returns the sum of affine functions FNA and FNB. */
1184 affine_fn_plus (affine_fn fna
, affine_fn fnb
)
1186 return affine_fn_op (PLUS_EXPR
, fna
, fnb
);
1189 /* Returns the difference of affine functions FNA and FNB. */
1192 affine_fn_minus (affine_fn fna
, affine_fn fnb
)
1194 return affine_fn_op (MINUS_EXPR
, fna
, fnb
);
1197 /* Frees affine function FN. */
1200 affine_fn_free (affine_fn fn
)
1205 /* Determine for each subscript in the data dependence relation DDR
1209 compute_subscript_distance (struct data_dependence_relation
*ddr
)
1211 conflict_function
*cf_a
, *cf_b
;
1212 affine_fn fn_a
, fn_b
, diff
;
1214 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
1218 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
1220 struct subscript
*subscript
;
1222 subscript
= DDR_SUBSCRIPT (ddr
, i
);
1223 cf_a
= SUB_CONFLICTS_IN_A (subscript
);
1224 cf_b
= SUB_CONFLICTS_IN_B (subscript
);
1226 fn_a
= common_affine_function (cf_a
);
1227 fn_b
= common_affine_function (cf_b
);
1228 if (!fn_a
.exists () || !fn_b
.exists ())
1230 SUB_DISTANCE (subscript
) = chrec_dont_know
;
1233 diff
= affine_fn_minus (fn_a
, fn_b
);
1235 if (affine_function_constant_p (diff
))
1236 SUB_DISTANCE (subscript
) = affine_function_base (diff
);
1238 SUB_DISTANCE (subscript
) = chrec_dont_know
;
1240 affine_fn_free (diff
);
1245 /* Returns the conflict function for "unknown". */
1247 static conflict_function
*
1248 conflict_fn_not_known (void)
1250 conflict_function
*fn
= XCNEW (conflict_function
);
1256 /* Returns the conflict function for "independent". */
1258 static conflict_function
*
1259 conflict_fn_no_dependence (void)
1261 conflict_function
*fn
= XCNEW (conflict_function
);
1262 fn
->n
= NO_DEPENDENCE
;
1267 /* Returns true if the address of OBJ is invariant in LOOP. */
1270 object_address_invariant_in_loop_p (const struct loop
*loop
, const_tree obj
)
1272 while (handled_component_p (obj
))
1274 if (TREE_CODE (obj
) == ARRAY_REF
)
1276 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1277 need to check the stride and the lower bound of the reference. */
1278 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj
, 2),
1280 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj
, 3),
1284 else if (TREE_CODE (obj
) == COMPONENT_REF
)
1286 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj
, 2),
1290 obj
= TREE_OPERAND (obj
, 0);
1293 if (!INDIRECT_REF_P (obj
)
1294 && TREE_CODE (obj
) != MEM_REF
)
1297 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj
, 0),
1301 /* Returns false if we can prove that data references A and B do not alias,
1302 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
1306 dr_may_alias_p (const struct data_reference
*a
, const struct data_reference
*b
,
1309 tree addr_a
= DR_BASE_OBJECT (a
);
1310 tree addr_b
= DR_BASE_OBJECT (b
);
1312 /* If we are not processing a loop nest but scalar code we
1313 do not need to care about possible cross-iteration dependences
1314 and thus can process the full original reference. Do so,
1315 similar to how loop invariant motion applies extra offset-based
1319 aff_tree off1
, off2
;
1320 double_int size1
, size2
;
1321 get_inner_reference_aff (DR_REF (a
), &off1
, &size1
);
1322 get_inner_reference_aff (DR_REF (b
), &off2
, &size2
);
1323 aff_combination_scale (&off1
, double_int_minus_one
);
1324 aff_combination_add (&off2
, &off1
);
1325 if (aff_comb_cannot_overlap_p (&off2
, size1
, size2
))
1329 /* If we had an evolution in a MEM_REF BASE_OBJECT we do not know
1330 the size of the base-object. So we cannot do any offset/overlap
1331 based analysis but have to rely on points-to information only. */
1332 if (TREE_CODE (addr_a
) == MEM_REF
1333 && DR_UNCONSTRAINED_BASE (a
))
1335 if (TREE_CODE (addr_b
) == MEM_REF
1336 && DR_UNCONSTRAINED_BASE (b
))
1337 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a
, 0),
1338 TREE_OPERAND (addr_b
, 0));
1340 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a
, 0),
1341 build_fold_addr_expr (addr_b
));
1343 else if (TREE_CODE (addr_b
) == MEM_REF
1344 && DR_UNCONSTRAINED_BASE (b
))
1345 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a
),
1346 TREE_OPERAND (addr_b
, 0));
1348 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1349 that is being subsetted in the loop nest. */
1350 if (DR_IS_WRITE (a
) && DR_IS_WRITE (b
))
1351 return refs_output_dependent_p (addr_a
, addr_b
);
1352 else if (DR_IS_READ (a
) && DR_IS_WRITE (b
))
1353 return refs_anti_dependent_p (addr_a
, addr_b
);
1354 return refs_may_alias_p (addr_a
, addr_b
);
1357 /* Initialize a data dependence relation between data accesses A and
1358 B. NB_LOOPS is the number of loops surrounding the references: the
1359 size of the classic distance/direction vectors. */
1361 struct data_dependence_relation
*
1362 initialize_data_dependence_relation (struct data_reference
*a
,
1363 struct data_reference
*b
,
1364 vec
<loop_p
> loop_nest
)
1366 struct data_dependence_relation
*res
;
1369 res
= XNEW (struct data_dependence_relation
);
1372 DDR_LOOP_NEST (res
).create (0);
1373 DDR_REVERSED_P (res
) = false;
1374 DDR_SUBSCRIPTS (res
).create (0);
1375 DDR_DIR_VECTS (res
).create (0);
1376 DDR_DIST_VECTS (res
).create (0);
1378 if (a
== NULL
|| b
== NULL
)
1380 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
1384 /* If the data references do not alias, then they are independent. */
1385 if (!dr_may_alias_p (a
, b
, loop_nest
.exists ()))
1387 DDR_ARE_DEPENDENT (res
) = chrec_known
;
1391 /* The case where the references are exactly the same. */
1392 if (operand_equal_p (DR_REF (a
), DR_REF (b
), 0))
1394 if (loop_nest
.exists ()
1395 && !object_address_invariant_in_loop_p (loop_nest
[0],
1396 DR_BASE_OBJECT (a
)))
1398 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
1401 DDR_AFFINE_P (res
) = true;
1402 DDR_ARE_DEPENDENT (res
) = NULL_TREE
;
1403 DDR_SUBSCRIPTS (res
).create (DR_NUM_DIMENSIONS (a
));
1404 DDR_LOOP_NEST (res
) = loop_nest
;
1405 DDR_INNER_LOOP (res
) = 0;
1406 DDR_SELF_REFERENCE (res
) = true;
1407 for (i
= 0; i
< DR_NUM_DIMENSIONS (a
); i
++)
1409 struct subscript
*subscript
;
1411 subscript
= XNEW (struct subscript
);
1412 SUB_CONFLICTS_IN_A (subscript
) = conflict_fn_not_known ();
1413 SUB_CONFLICTS_IN_B (subscript
) = conflict_fn_not_known ();
1414 SUB_LAST_CONFLICT (subscript
) = chrec_dont_know
;
1415 SUB_DISTANCE (subscript
) = chrec_dont_know
;
1416 DDR_SUBSCRIPTS (res
).safe_push (subscript
);
1421 /* If the references do not access the same object, we do not know
1422 whether they alias or not. */
1423 if (!operand_equal_p (DR_BASE_OBJECT (a
), DR_BASE_OBJECT (b
), 0))
1425 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
1429 /* If the base of the object is not invariant in the loop nest, we cannot
1430 analyze it. TODO -- in fact, it would suffice to record that there may
1431 be arbitrary dependences in the loops where the base object varies. */
1432 if (loop_nest
.exists ()
1433 && !object_address_invariant_in_loop_p (loop_nest
[0],
1434 DR_BASE_OBJECT (a
)))
1436 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
1440 /* If the number of dimensions of the access to not agree we can have
1441 a pointer access to a component of the array element type and an
1442 array access while the base-objects are still the same. Punt. */
1443 if (DR_NUM_DIMENSIONS (a
) != DR_NUM_DIMENSIONS (b
))
1445 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
1449 DDR_AFFINE_P (res
) = true;
1450 DDR_ARE_DEPENDENT (res
) = NULL_TREE
;
1451 DDR_SUBSCRIPTS (res
).create (DR_NUM_DIMENSIONS (a
));
1452 DDR_LOOP_NEST (res
) = loop_nest
;
1453 DDR_INNER_LOOP (res
) = 0;
1454 DDR_SELF_REFERENCE (res
) = false;
1456 for (i
= 0; i
< DR_NUM_DIMENSIONS (a
); i
++)
1458 struct subscript
*subscript
;
1460 subscript
= XNEW (struct subscript
);
1461 SUB_CONFLICTS_IN_A (subscript
) = conflict_fn_not_known ();
1462 SUB_CONFLICTS_IN_B (subscript
) = conflict_fn_not_known ();
1463 SUB_LAST_CONFLICT (subscript
) = chrec_dont_know
;
1464 SUB_DISTANCE (subscript
) = chrec_dont_know
;
1465 DDR_SUBSCRIPTS (res
).safe_push (subscript
);
1471 /* Frees memory used by the conflict function F. */
1474 free_conflict_function (conflict_function
*f
)
1478 if (CF_NONTRIVIAL_P (f
))
1480 for (i
= 0; i
< f
->n
; i
++)
1481 affine_fn_free (f
->fns
[i
]);
1486 /* Frees memory used by SUBSCRIPTS. */
1489 free_subscripts (vec
<subscript_p
> subscripts
)
1494 FOR_EACH_VEC_ELT (subscripts
, i
, s
)
1496 free_conflict_function (s
->conflicting_iterations_in_a
);
1497 free_conflict_function (s
->conflicting_iterations_in_b
);
1500 subscripts
.release ();
1503 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1507 finalize_ddr_dependent (struct data_dependence_relation
*ddr
,
1510 DDR_ARE_DEPENDENT (ddr
) = chrec
;
1511 free_subscripts (DDR_SUBSCRIPTS (ddr
));
1512 DDR_SUBSCRIPTS (ddr
).create (0);
1515 /* The dependence relation DDR cannot be represented by a distance
1519 non_affine_dependence_relation (struct data_dependence_relation
*ddr
)
1521 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1522 fprintf (dump_file
, "(Dependence relation cannot be represented by distance vector.) \n");
1524 DDR_AFFINE_P (ddr
) = false;
1529 /* This section contains the classic Banerjee tests. */
1531 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1532 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1535 ziv_subscript_p (const_tree chrec_a
, const_tree chrec_b
)
1537 return (evolution_function_is_constant_p (chrec_a
)
1538 && evolution_function_is_constant_p (chrec_b
));
1541 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
1542 variable, i.e., if the SIV (Single Index Variable) test is true. */
1545 siv_subscript_p (const_tree chrec_a
, const_tree chrec_b
)
1547 if ((evolution_function_is_constant_p (chrec_a
)
1548 && evolution_function_is_univariate_p (chrec_b
))
1549 || (evolution_function_is_constant_p (chrec_b
)
1550 && evolution_function_is_univariate_p (chrec_a
)))
1553 if (evolution_function_is_univariate_p (chrec_a
)
1554 && evolution_function_is_univariate_p (chrec_b
))
1556 switch (TREE_CODE (chrec_a
))
1558 case POLYNOMIAL_CHREC
:
1559 switch (TREE_CODE (chrec_b
))
1561 case POLYNOMIAL_CHREC
:
1562 if (CHREC_VARIABLE (chrec_a
) != CHREC_VARIABLE (chrec_b
))
1577 /* Creates a conflict function with N dimensions. The affine functions
1578 in each dimension follow. */
1580 static conflict_function
*
1581 conflict_fn (unsigned n
, ...)
1584 conflict_function
*ret
= XCNEW (conflict_function
);
1587 gcc_assert (0 < n
&& n
<= MAX_DIM
);
1591 for (i
= 0; i
< n
; i
++)
1592 ret
->fns
[i
] = va_arg (ap
, affine_fn
);
1598 /* Returns constant affine function with value CST. */
1601 affine_fn_cst (tree cst
)
1605 fn
.quick_push (cst
);
1609 /* Returns affine function with single variable, CST + COEF * x_DIM. */
1612 affine_fn_univar (tree cst
, unsigned dim
, tree coef
)
1615 fn
.create (dim
+ 1);
1618 gcc_assert (dim
> 0);
1619 fn
.quick_push (cst
);
1620 for (i
= 1; i
< dim
; i
++)
1621 fn
.quick_push (integer_zero_node
);
1622 fn
.quick_push (coef
);
1626 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1627 *OVERLAPS_B are initialized to the functions that describe the
1628 relation between the elements accessed twice by CHREC_A and
1629 CHREC_B. For k >= 0, the following property is verified:
1631 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1634 analyze_ziv_subscript (tree chrec_a
,
1636 conflict_function
**overlaps_a
,
1637 conflict_function
**overlaps_b
,
1638 tree
*last_conflicts
)
1640 tree type
, difference
;
1641 dependence_stats
.num_ziv
++;
1643 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1644 fprintf (dump_file
, "(analyze_ziv_subscript \n");
1646 type
= signed_type_for_types (TREE_TYPE (chrec_a
), TREE_TYPE (chrec_b
));
1647 chrec_a
= chrec_convert (type
, chrec_a
, NULL
);
1648 chrec_b
= chrec_convert (type
, chrec_b
, NULL
);
1649 difference
= chrec_fold_minus (type
, chrec_a
, chrec_b
);
1651 switch (TREE_CODE (difference
))
1654 if (integer_zerop (difference
))
1656 /* The difference is equal to zero: the accessed index
1657 overlaps for each iteration in the loop. */
1658 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1659 *overlaps_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1660 *last_conflicts
= chrec_dont_know
;
1661 dependence_stats
.num_ziv_dependent
++;
1665 /* The accesses do not overlap. */
1666 *overlaps_a
= conflict_fn_no_dependence ();
1667 *overlaps_b
= conflict_fn_no_dependence ();
1668 *last_conflicts
= integer_zero_node
;
1669 dependence_stats
.num_ziv_independent
++;
1674 /* We're not sure whether the indexes overlap. For the moment,
1675 conservatively answer "don't know". */
1676 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1677 fprintf (dump_file
, "ziv test failed: difference is non-integer.\n");
1679 *overlaps_a
= conflict_fn_not_known ();
1680 *overlaps_b
= conflict_fn_not_known ();
1681 *last_conflicts
= chrec_dont_know
;
1682 dependence_stats
.num_ziv_unimplemented
++;
1686 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1687 fprintf (dump_file
, ")\n");
1690 /* Similar to max_stmt_executions_int, but returns the bound as a tree,
1691 and only if it fits to the int type. If this is not the case, or the
1692 bound on the number of iterations of LOOP could not be derived, returns
1696 max_stmt_executions_tree (struct loop
*loop
)
1700 if (!max_stmt_executions (loop
, &nit
))
1701 return chrec_dont_know
;
1703 if (!double_int_fits_to_tree_p (unsigned_type_node
, nit
))
1704 return chrec_dont_know
;
1706 return double_int_to_tree (unsigned_type_node
, nit
);
1709 /* Determine whether the CHREC is always positive/negative. If the expression
1710 cannot be statically analyzed, return false, otherwise set the answer into
1714 chrec_is_positive (tree chrec
, bool *value
)
1716 bool value0
, value1
, value2
;
1717 tree end_value
, nb_iter
;
1719 switch (TREE_CODE (chrec
))
1721 case POLYNOMIAL_CHREC
:
1722 if (!chrec_is_positive (CHREC_LEFT (chrec
), &value0
)
1723 || !chrec_is_positive (CHREC_RIGHT (chrec
), &value1
))
1726 /* FIXME -- overflows. */
1727 if (value0
== value1
)
1733 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1734 and the proof consists in showing that the sign never
1735 changes during the execution of the loop, from 0 to
1736 loop->nb_iterations. */
1737 if (!evolution_function_is_affine_p (chrec
))
1740 nb_iter
= number_of_latch_executions (get_chrec_loop (chrec
));
1741 if (chrec_contains_undetermined (nb_iter
))
1745 /* TODO -- If the test is after the exit, we may decrease the number of
1746 iterations by one. */
1748 nb_iter
= chrec_fold_minus (type
, nb_iter
, build_int_cst (type
, 1));
1751 end_value
= chrec_apply (CHREC_VARIABLE (chrec
), chrec
, nb_iter
);
1753 if (!chrec_is_positive (end_value
, &value2
))
1757 return value0
== value1
;
1760 switch (tree_int_cst_sgn (chrec
))
1779 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1780 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1781 *OVERLAPS_B are initialized to the functions that describe the
1782 relation between the elements accessed twice by CHREC_A and
1783 CHREC_B. For k >= 0, the following property is verified:
1785 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1788 analyze_siv_subscript_cst_affine (tree chrec_a
,
1790 conflict_function
**overlaps_a
,
1791 conflict_function
**overlaps_b
,
1792 tree
*last_conflicts
)
1794 bool value0
, value1
, value2
;
1795 tree type
, difference
, tmp
;
1797 type
= signed_type_for_types (TREE_TYPE (chrec_a
), TREE_TYPE (chrec_b
));
1798 chrec_a
= chrec_convert (type
, chrec_a
, NULL
);
1799 chrec_b
= chrec_convert (type
, chrec_b
, NULL
);
1800 difference
= chrec_fold_minus (type
, initial_condition (chrec_b
), chrec_a
);
1802 /* Special case overlap in the first iteration. */
1803 if (integer_zerop (difference
))
1805 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1806 *overlaps_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1807 *last_conflicts
= integer_one_node
;
1811 if (!chrec_is_positive (initial_condition (difference
), &value0
))
1813 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1814 fprintf (dump_file
, "siv test failed: chrec is not positive.\n");
1816 dependence_stats
.num_siv_unimplemented
++;
1817 *overlaps_a
= conflict_fn_not_known ();
1818 *overlaps_b
= conflict_fn_not_known ();
1819 *last_conflicts
= chrec_dont_know
;
1824 if (value0
== false)
1826 if (!chrec_is_positive (CHREC_RIGHT (chrec_b
), &value1
))
1828 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1829 fprintf (dump_file
, "siv test failed: chrec not positive.\n");
1831 *overlaps_a
= conflict_fn_not_known ();
1832 *overlaps_b
= conflict_fn_not_known ();
1833 *last_conflicts
= chrec_dont_know
;
1834 dependence_stats
.num_siv_unimplemented
++;
1843 chrec_b = {10, +, 1}
1846 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b
), difference
))
1848 HOST_WIDE_INT numiter
;
1849 struct loop
*loop
= get_chrec_loop (chrec_b
);
1851 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1852 tmp
= fold_build2 (EXACT_DIV_EXPR
, type
,
1853 fold_build1 (ABS_EXPR
, type
, difference
),
1854 CHREC_RIGHT (chrec_b
));
1855 *overlaps_b
= conflict_fn (1, affine_fn_cst (tmp
));
1856 *last_conflicts
= integer_one_node
;
1859 /* Perform weak-zero siv test to see if overlap is
1860 outside the loop bounds. */
1861 numiter
= max_stmt_executions_int (loop
);
1864 && compare_tree_int (tmp
, numiter
) > 0)
1866 free_conflict_function (*overlaps_a
);
1867 free_conflict_function (*overlaps_b
);
1868 *overlaps_a
= conflict_fn_no_dependence ();
1869 *overlaps_b
= conflict_fn_no_dependence ();
1870 *last_conflicts
= integer_zero_node
;
1871 dependence_stats
.num_siv_independent
++;
1874 dependence_stats
.num_siv_dependent
++;
1878 /* When the step does not divide the difference, there are
1882 *overlaps_a
= conflict_fn_no_dependence ();
1883 *overlaps_b
= conflict_fn_no_dependence ();
1884 *last_conflicts
= integer_zero_node
;
1885 dependence_stats
.num_siv_independent
++;
1894 chrec_b = {10, +, -1}
1896 In this case, chrec_a will not overlap with chrec_b. */
1897 *overlaps_a
= conflict_fn_no_dependence ();
1898 *overlaps_b
= conflict_fn_no_dependence ();
1899 *last_conflicts
= integer_zero_node
;
1900 dependence_stats
.num_siv_independent
++;
1907 if (!chrec_is_positive (CHREC_RIGHT (chrec_b
), &value2
))
1909 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1910 fprintf (dump_file
, "siv test failed: chrec not positive.\n");
1912 *overlaps_a
= conflict_fn_not_known ();
1913 *overlaps_b
= conflict_fn_not_known ();
1914 *last_conflicts
= chrec_dont_know
;
1915 dependence_stats
.num_siv_unimplemented
++;
1920 if (value2
== false)
1924 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
, difference
,
1933 CHREC_RIGHT (chrec_b
));
1934 *overlaps_b
= conflict_fn (1, affine_fn_cst (tmp
));
1935 *last_conflicts
= integer_one_node
;
1937 /* Perform weak-zero siv test to see if overlap is
1938 outside the loop bounds. */
1939 numiter
= max_stmt_executions_int (loop
);
1942 && compare_tree_int (tmp
, numiter
) > 0)
1944 free_conflict_function (*overlaps_a
);
1945 free_conflict_function (*overlaps_b
);
1946 *overlaps_a
= conflict_fn_no_dependence ();
1947 *overlaps_b
= conflict_fn_no_dependence ();
1948 *last_conflicts
= integer_zero_node
;
1949 dependence_stats
.num_siv_independent
++;
1952 dependence_stats
.num_siv_dependent
++;
1956 /* When the step does not divide the difference, there
1960 *overlaps_a
= conflict_fn_no_dependence ();
1961 *overlaps_b
= conflict_fn_no_dependence ();
1962 *last_conflicts
= integer_zero_node
;
1963 dependence_stats
.num_siv_independent
++;
1973 In this case, chrec_a will not overlap with chrec_b. */
1974 *overlaps_a
= conflict_fn_no_dependence ();
1975 *overlaps_b
= conflict_fn_no_dependence ();
1976 *last_conflicts
= integer_zero_node
;
1977 dependence_stats
.num_siv_independent
++;
1985 /* Helper recursive function for initializing the matrix A. Returns
1986 the initial value of CHREC. */
1989 initialize_matrix_A (lambda_matrix A
, tree chrec
, unsigned index
, int mult
)
1993 switch (TREE_CODE (chrec
))
1995 case POLYNOMIAL_CHREC
:
1996 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec
)) == INTEGER_CST
);
1998 A
[index
][0] = mult
* int_cst_value (CHREC_RIGHT (chrec
));
1999 return initialize_matrix_A (A
, CHREC_LEFT (chrec
), index
+ 1, mult
);
2005 tree op0
= initialize_matrix_A (A
, TREE_OPERAND (chrec
, 0), index
, mult
);
2006 tree op1
= initialize_matrix_A (A
, TREE_OPERAND (chrec
, 1), index
, mult
);
2008 return chrec_fold_op (TREE_CODE (chrec
), chrec_type (chrec
), op0
, op1
);
2013 tree op
= initialize_matrix_A (A
, TREE_OPERAND (chrec
, 0), index
, mult
);
2014 return chrec_convert (chrec_type (chrec
), op
, NULL
);
2019 /* Handle ~X as -1 - X. */
2020 tree op
= initialize_matrix_A (A
, TREE_OPERAND (chrec
, 0), index
, mult
);
2021 return chrec_fold_op (MINUS_EXPR
, chrec_type (chrec
),
2022 build_int_cst (TREE_TYPE (chrec
), -1), op
);
2034 #define FLOOR_DIV(x,y) ((x) / (y))
2036 /* Solves the special case of the Diophantine equation:
2037 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2039 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2040 number of iterations that loops X and Y run. The overlaps will be
2041 constructed as evolutions in dimension DIM. */
2044 compute_overlap_steps_for_affine_univar (int niter
, int step_a
, int step_b
,
2045 affine_fn
*overlaps_a
,
2046 affine_fn
*overlaps_b
,
2047 tree
*last_conflicts
, int dim
)
2049 if (((step_a
> 0 && step_b
> 0)
2050 || (step_a
< 0 && step_b
< 0)))
2052 int step_overlaps_a
, step_overlaps_b
;
2053 int gcd_steps_a_b
, last_conflict
, tau2
;
2055 gcd_steps_a_b
= gcd (step_a
, step_b
);
2056 step_overlaps_a
= step_b
/ gcd_steps_a_b
;
2057 step_overlaps_b
= step_a
/ gcd_steps_a_b
;
2061 tau2
= FLOOR_DIV (niter
, step_overlaps_a
);
2062 tau2
= MIN (tau2
, FLOOR_DIV (niter
, step_overlaps_b
));
2063 last_conflict
= tau2
;
2064 *last_conflicts
= build_int_cst (NULL_TREE
, last_conflict
);
2067 *last_conflicts
= chrec_dont_know
;
2069 *overlaps_a
= affine_fn_univar (integer_zero_node
, dim
,
2070 build_int_cst (NULL_TREE
,
2072 *overlaps_b
= affine_fn_univar (integer_zero_node
, dim
,
2073 build_int_cst (NULL_TREE
,
2079 *overlaps_a
= affine_fn_cst (integer_zero_node
);
2080 *overlaps_b
= affine_fn_cst (integer_zero_node
);
2081 *last_conflicts
= integer_zero_node
;
2085 /* Solves the special case of a Diophantine equation where CHREC_A is
2086 an affine bivariate function, and CHREC_B is an affine univariate
2087 function. For example,
2089 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2091 has the following overlapping functions:
2093 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2094 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2095 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2097 FORNOW: This is a specialized implementation for a case occurring in
2098 a common benchmark. Implement the general algorithm. */
2101 compute_overlap_steps_for_affine_1_2 (tree chrec_a
, tree chrec_b
,
2102 conflict_function
**overlaps_a
,
2103 conflict_function
**overlaps_b
,
2104 tree
*last_conflicts
)
2106 bool xz_p
, yz_p
, xyz_p
;
2107 int step_x
, step_y
, step_z
;
2108 HOST_WIDE_INT niter_x
, niter_y
, niter_z
, niter
;
2109 affine_fn overlaps_a_xz
, overlaps_b_xz
;
2110 affine_fn overlaps_a_yz
, overlaps_b_yz
;
2111 affine_fn overlaps_a_xyz
, overlaps_b_xyz
;
2112 affine_fn ova1
, ova2
, ovb
;
2113 tree last_conflicts_xz
, last_conflicts_yz
, last_conflicts_xyz
;
2115 step_x
= int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a
)));
2116 step_y
= int_cst_value (CHREC_RIGHT (chrec_a
));
2117 step_z
= int_cst_value (CHREC_RIGHT (chrec_b
));
2119 niter_x
= max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a
)));
2120 niter_y
= max_stmt_executions_int (get_chrec_loop (chrec_a
));
2121 niter_z
= max_stmt_executions_int (get_chrec_loop (chrec_b
));
2123 if (niter_x
< 0 || niter_y
< 0 || niter_z
< 0)
2125 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2126 fprintf (dump_file
, "overlap steps test failed: no iteration counts.\n");
2128 *overlaps_a
= conflict_fn_not_known ();
2129 *overlaps_b
= conflict_fn_not_known ();
2130 *last_conflicts
= chrec_dont_know
;
2134 niter
= MIN (niter_x
, niter_z
);
2135 compute_overlap_steps_for_affine_univar (niter
, step_x
, step_z
,
2138 &last_conflicts_xz
, 1);
2139 niter
= MIN (niter_y
, niter_z
);
2140 compute_overlap_steps_for_affine_univar (niter
, step_y
, step_z
,
2143 &last_conflicts_yz
, 2);
2144 niter
= MIN (niter_x
, niter_z
);
2145 niter
= MIN (niter_y
, niter
);
2146 compute_overlap_steps_for_affine_univar (niter
, step_x
+ step_y
, step_z
,
2149 &last_conflicts_xyz
, 3);
2151 xz_p
= !integer_zerop (last_conflicts_xz
);
2152 yz_p
= !integer_zerop (last_conflicts_yz
);
2153 xyz_p
= !integer_zerop (last_conflicts_xyz
);
2155 if (xz_p
|| yz_p
|| xyz_p
)
2157 ova1
= affine_fn_cst (integer_zero_node
);
2158 ova2
= affine_fn_cst (integer_zero_node
);
2159 ovb
= affine_fn_cst (integer_zero_node
);
2162 affine_fn t0
= ova1
;
2165 ova1
= affine_fn_plus (ova1
, overlaps_a_xz
);
2166 ovb
= affine_fn_plus (ovb
, overlaps_b_xz
);
2167 affine_fn_free (t0
);
2168 affine_fn_free (t2
);
2169 *last_conflicts
= last_conflicts_xz
;
2173 affine_fn t0
= ova2
;
2176 ova2
= affine_fn_plus (ova2
, overlaps_a_yz
);
2177 ovb
= affine_fn_plus (ovb
, overlaps_b_yz
);
2178 affine_fn_free (t0
);
2179 affine_fn_free (t2
);
2180 *last_conflicts
= last_conflicts_yz
;
2184 affine_fn t0
= ova1
;
2185 affine_fn t2
= ova2
;
2188 ova1
= affine_fn_plus (ova1
, overlaps_a_xyz
);
2189 ova2
= affine_fn_plus (ova2
, overlaps_a_xyz
);
2190 ovb
= affine_fn_plus (ovb
, overlaps_b_xyz
);
2191 affine_fn_free (t0
);
2192 affine_fn_free (t2
);
2193 affine_fn_free (t4
);
2194 *last_conflicts
= last_conflicts_xyz
;
2196 *overlaps_a
= conflict_fn (2, ova1
, ova2
);
2197 *overlaps_b
= conflict_fn (1, ovb
);
2201 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2202 *overlaps_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2203 *last_conflicts
= integer_zero_node
;
2206 affine_fn_free (overlaps_a_xz
);
2207 affine_fn_free (overlaps_b_xz
);
2208 affine_fn_free (overlaps_a_yz
);
2209 affine_fn_free (overlaps_b_yz
);
2210 affine_fn_free (overlaps_a_xyz
);
2211 affine_fn_free (overlaps_b_xyz
);
2214 /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2217 lambda_vector_copy (lambda_vector vec1
, lambda_vector vec2
,
2220 memcpy (vec2
, vec1
, size
* sizeof (*vec1
));
2223 /* Copy the elements of M x N matrix MAT1 to MAT2. */
2226 lambda_matrix_copy (lambda_matrix mat1
, lambda_matrix mat2
,
2231 for (i
= 0; i
< m
; i
++)
2232 lambda_vector_copy (mat1
[i
], mat2
[i
], n
);
2235 /* Store the N x N identity matrix in MAT. */
2238 lambda_matrix_id (lambda_matrix mat
, int size
)
2242 for (i
= 0; i
< size
; i
++)
2243 for (j
= 0; j
< size
; j
++)
2244 mat
[i
][j
] = (i
== j
) ? 1 : 0;
2247 /* Return the first nonzero element of vector VEC1 between START and N.
2248 We must have START <= N. Returns N if VEC1 is the zero vector. */
2251 lambda_vector_first_nz (lambda_vector vec1
, int n
, int start
)
2254 while (j
< n
&& vec1
[j
] == 0)
2259 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2260 R2 = R2 + CONST1 * R1. */
2263 lambda_matrix_row_add (lambda_matrix mat
, int n
, int r1
, int r2
, int const1
)
2270 for (i
= 0; i
< n
; i
++)
2271 mat
[r2
][i
] += const1
* mat
[r1
][i
];
2274 /* Swap rows R1 and R2 in matrix MAT. */
2277 lambda_matrix_row_exchange (lambda_matrix mat
, int r1
, int r2
)
2286 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
2287 and store the result in VEC2. */
2290 lambda_vector_mult_const (lambda_vector vec1
, lambda_vector vec2
,
2291 int size
, int const1
)
2296 lambda_vector_clear (vec2
, size
);
2298 for (i
= 0; i
< size
; i
++)
2299 vec2
[i
] = const1
* vec1
[i
];
2302 /* Negate vector VEC1 with length SIZE and store it in VEC2. */
2305 lambda_vector_negate (lambda_vector vec1
, lambda_vector vec2
,
2308 lambda_vector_mult_const (vec1
, vec2
, size
, -1);
2311 /* Negate row R1 of matrix MAT which has N columns. */
2314 lambda_matrix_row_negate (lambda_matrix mat
, int n
, int r1
)
2316 lambda_vector_negate (mat
[r1
], mat
[r1
], n
);
2319 /* Return true if two vectors are equal. */
2322 lambda_vector_equal (lambda_vector vec1
, lambda_vector vec2
, int size
)
2325 for (i
= 0; i
< size
; i
++)
2326 if (vec1
[i
] != vec2
[i
])
2331 /* Given an M x N integer matrix A, this function determines an M x
2332 M unimodular matrix U, and an M x N echelon matrix S such that
2333 "U.A = S". This decomposition is also known as "right Hermite".
2335 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2336 Restructuring Compilers" Utpal Banerjee. */
2339 lambda_matrix_right_hermite (lambda_matrix A
, int m
, int n
,
2340 lambda_matrix S
, lambda_matrix U
)
2344 lambda_matrix_copy (A
, S
, m
, n
);
2345 lambda_matrix_id (U
, m
);
2347 for (j
= 0; j
< n
; j
++)
2349 if (lambda_vector_first_nz (S
[j
], m
, i0
) < m
)
2352 for (i
= m
- 1; i
>= i0
; i
--)
2354 while (S
[i
][j
] != 0)
2356 int sigma
, factor
, a
, b
;
2360 sigma
= (a
* b
< 0) ? -1: 1;
2363 factor
= sigma
* (a
/ b
);
2365 lambda_matrix_row_add (S
, n
, i
, i
-1, -factor
);
2366 lambda_matrix_row_exchange (S
, i
, i
-1);
2368 lambda_matrix_row_add (U
, m
, i
, i
-1, -factor
);
2369 lambda_matrix_row_exchange (U
, i
, i
-1);
2376 /* Determines the overlapping elements due to accesses CHREC_A and
2377 CHREC_B, that are affine functions. This function cannot handle
2378 symbolic evolution functions, ie. when initial conditions are
2379 parameters, because it uses lambda matrices of integers. */
2382 analyze_subscript_affine_affine (tree chrec_a
,
2384 conflict_function
**overlaps_a
,
2385 conflict_function
**overlaps_b
,
2386 tree
*last_conflicts
)
2388 unsigned nb_vars_a
, nb_vars_b
, dim
;
2389 HOST_WIDE_INT init_a
, init_b
, gamma
, gcd_alpha_beta
;
2390 lambda_matrix A
, U
, S
;
2391 struct obstack scratch_obstack
;
2393 if (eq_evolutions_p (chrec_a
, chrec_b
))
2395 /* The accessed index overlaps for each iteration in the
2397 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2398 *overlaps_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2399 *last_conflicts
= chrec_dont_know
;
2402 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2403 fprintf (dump_file
, "(analyze_subscript_affine_affine \n");
2405 /* For determining the initial intersection, we have to solve a
2406 Diophantine equation. This is the most time consuming part.
2408 For answering to the question: "Is there a dependence?" we have
2409 to prove that there exists a solution to the Diophantine
2410 equation, and that the solution is in the iteration domain,
2411 i.e. the solution is positive or zero, and that the solution
2412 happens before the upper bound loop.nb_iterations. Otherwise
2413 there is no dependence. This function outputs a description of
2414 the iterations that hold the intersections. */
2416 nb_vars_a
= nb_vars_in_chrec (chrec_a
);
2417 nb_vars_b
= nb_vars_in_chrec (chrec_b
);
2419 gcc_obstack_init (&scratch_obstack
);
2421 dim
= nb_vars_a
+ nb_vars_b
;
2422 U
= lambda_matrix_new (dim
, dim
, &scratch_obstack
);
2423 A
= lambda_matrix_new (dim
, 1, &scratch_obstack
);
2424 S
= lambda_matrix_new (dim
, 1, &scratch_obstack
);
2426 init_a
= int_cst_value (initialize_matrix_A (A
, chrec_a
, 0, 1));
2427 init_b
= int_cst_value (initialize_matrix_A (A
, chrec_b
, nb_vars_a
, -1));
2428 gamma
= init_b
- init_a
;
2430 /* Don't do all the hard work of solving the Diophantine equation
2431 when we already know the solution: for example,
2434 | gamma = 3 - 3 = 0.
2435 Then the first overlap occurs during the first iterations:
2436 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2440 if (nb_vars_a
== 1 && nb_vars_b
== 1)
2442 HOST_WIDE_INT step_a
, step_b
;
2443 HOST_WIDE_INT niter
, niter_a
, niter_b
;
2446 niter_a
= max_stmt_executions_int (get_chrec_loop (chrec_a
));
2447 niter_b
= max_stmt_executions_int (get_chrec_loop (chrec_b
));
2448 niter
= MIN (niter_a
, niter_b
);
2449 step_a
= int_cst_value (CHREC_RIGHT (chrec_a
));
2450 step_b
= int_cst_value (CHREC_RIGHT (chrec_b
));
2452 compute_overlap_steps_for_affine_univar (niter
, step_a
, step_b
,
2455 *overlaps_a
= conflict_fn (1, ova
);
2456 *overlaps_b
= conflict_fn (1, ovb
);
2459 else if (nb_vars_a
== 2 && nb_vars_b
== 1)
2460 compute_overlap_steps_for_affine_1_2
2461 (chrec_a
, chrec_b
, overlaps_a
, overlaps_b
, last_conflicts
);
2463 else if (nb_vars_a
== 1 && nb_vars_b
== 2)
2464 compute_overlap_steps_for_affine_1_2
2465 (chrec_b
, chrec_a
, overlaps_b
, overlaps_a
, last_conflicts
);
2469 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2470 fprintf (dump_file
, "affine-affine test failed: too many variables.\n");
2471 *overlaps_a
= conflict_fn_not_known ();
2472 *overlaps_b
= conflict_fn_not_known ();
2473 *last_conflicts
= chrec_dont_know
;
2475 goto end_analyze_subs_aa
;
2479 lambda_matrix_right_hermite (A
, dim
, 1, S
, U
);
2484 lambda_matrix_row_negate (U
, dim
, 0);
2486 gcd_alpha_beta
= S
[0][0];
2488 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2489 but that is a quite strange case. Instead of ICEing, answer
2491 if (gcd_alpha_beta
== 0)
2493 *overlaps_a
= conflict_fn_not_known ();
2494 *overlaps_b
= conflict_fn_not_known ();
2495 *last_conflicts
= chrec_dont_know
;
2496 goto end_analyze_subs_aa
;
2499 /* The classic "gcd-test". */
2500 if (!int_divides_p (gcd_alpha_beta
, gamma
))
2502 /* The "gcd-test" has determined that there is no integer
2503 solution, i.e. there is no dependence. */
2504 *overlaps_a
= conflict_fn_no_dependence ();
2505 *overlaps_b
= conflict_fn_no_dependence ();
2506 *last_conflicts
= integer_zero_node
;
2509 /* Both access functions are univariate. This includes SIV and MIV cases. */
2510 else if (nb_vars_a
== 1 && nb_vars_b
== 1)
2512 /* Both functions should have the same evolution sign. */
2513 if (((A
[0][0] > 0 && -A
[1][0] > 0)
2514 || (A
[0][0] < 0 && -A
[1][0] < 0)))
2516 /* The solutions are given by:
2518 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2521 For a given integer t. Using the following variables,
2523 | i0 = u11 * gamma / gcd_alpha_beta
2524 | j0 = u12 * gamma / gcd_alpha_beta
2531 | y0 = j0 + j1 * t. */
2532 HOST_WIDE_INT i0
, j0
, i1
, j1
;
2534 i0
= U
[0][0] * gamma
/ gcd_alpha_beta
;
2535 j0
= U
[0][1] * gamma
/ gcd_alpha_beta
;
2539 if ((i1
== 0 && i0
< 0)
2540 || (j1
== 0 && j0
< 0))
2542 /* There is no solution.
2543 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2544 falls in here, but for the moment we don't look at the
2545 upper bound of the iteration domain. */
2546 *overlaps_a
= conflict_fn_no_dependence ();
2547 *overlaps_b
= conflict_fn_no_dependence ();
2548 *last_conflicts
= integer_zero_node
;
2549 goto end_analyze_subs_aa
;
2552 if (i1
> 0 && j1
> 0)
2554 HOST_WIDE_INT niter_a
2555 = max_stmt_executions_int (get_chrec_loop (chrec_a
));
2556 HOST_WIDE_INT niter_b
2557 = max_stmt_executions_int (get_chrec_loop (chrec_b
));
2558 HOST_WIDE_INT niter
= MIN (niter_a
, niter_b
);
2560 /* (X0, Y0) is a solution of the Diophantine equation:
2561 "chrec_a (X0) = chrec_b (Y0)". */
2562 HOST_WIDE_INT tau1
= MAX (CEIL (-i0
, i1
),
2564 HOST_WIDE_INT x0
= i1
* tau1
+ i0
;
2565 HOST_WIDE_INT y0
= j1
* tau1
+ j0
;
2567 /* (X1, Y1) is the smallest positive solution of the eq
2568 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2569 first conflict occurs. */
2570 HOST_WIDE_INT min_multiple
= MIN (x0
/ i1
, y0
/ j1
);
2571 HOST_WIDE_INT x1
= x0
- i1
* min_multiple
;
2572 HOST_WIDE_INT y1
= y0
- j1
* min_multiple
;
2576 HOST_WIDE_INT tau2
= MIN (FLOOR_DIV (niter
- i0
, i1
),
2577 FLOOR_DIV (niter
- j0
, j1
));
2578 HOST_WIDE_INT last_conflict
= tau2
- (x1
- i0
)/i1
;
2580 /* If the overlap occurs outside of the bounds of the
2581 loop, there is no dependence. */
2582 if (x1
>= niter
|| y1
>= niter
)
2584 *overlaps_a
= conflict_fn_no_dependence ();
2585 *overlaps_b
= conflict_fn_no_dependence ();
2586 *last_conflicts
= integer_zero_node
;
2587 goto end_analyze_subs_aa
;
2590 *last_conflicts
= build_int_cst (NULL_TREE
, last_conflict
);
2593 *last_conflicts
= chrec_dont_know
;
2597 affine_fn_univar (build_int_cst (NULL_TREE
, x1
),
2599 build_int_cst (NULL_TREE
, i1
)));
2602 affine_fn_univar (build_int_cst (NULL_TREE
, y1
),
2604 build_int_cst (NULL_TREE
, j1
)));
2608 /* FIXME: For the moment, the upper bound of the
2609 iteration domain for i and j is not checked. */
2610 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2611 fprintf (dump_file
, "affine-affine test failed: unimplemented.\n");
2612 *overlaps_a
= conflict_fn_not_known ();
2613 *overlaps_b
= conflict_fn_not_known ();
2614 *last_conflicts
= chrec_dont_know
;
2619 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2620 fprintf (dump_file
, "affine-affine test failed: unimplemented.\n");
2621 *overlaps_a
= conflict_fn_not_known ();
2622 *overlaps_b
= conflict_fn_not_known ();
2623 *last_conflicts
= chrec_dont_know
;
2628 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2629 fprintf (dump_file
, "affine-affine test failed: unimplemented.\n");
2630 *overlaps_a
= conflict_fn_not_known ();
2631 *overlaps_b
= conflict_fn_not_known ();
2632 *last_conflicts
= chrec_dont_know
;
2635 end_analyze_subs_aa
:
2636 obstack_free (&scratch_obstack
, NULL
);
2637 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2639 fprintf (dump_file
, " (overlaps_a = ");
2640 dump_conflict_function (dump_file
, *overlaps_a
);
2641 fprintf (dump_file
, ")\n (overlaps_b = ");
2642 dump_conflict_function (dump_file
, *overlaps_b
);
2643 fprintf (dump_file
, "))\n");
2647 /* Returns true when analyze_subscript_affine_affine can be used for
2648 determining the dependence relation between chrec_a and chrec_b,
2649 that contain symbols. This function modifies chrec_a and chrec_b
2650 such that the analysis result is the same, and such that they don't
2651 contain symbols, and then can safely be passed to the analyzer.
2653 Example: The analysis of the following tuples of evolutions produce
2654 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2657 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2658 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2662 can_use_analyze_subscript_affine_affine (tree
*chrec_a
, tree
*chrec_b
)
2664 tree diff
, type
, left_a
, left_b
, right_b
;
2666 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a
))
2667 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b
)))
2668 /* FIXME: For the moment not handled. Might be refined later. */
2671 type
= chrec_type (*chrec_a
);
2672 left_a
= CHREC_LEFT (*chrec_a
);
2673 left_b
= chrec_convert (type
, CHREC_LEFT (*chrec_b
), NULL
);
2674 diff
= chrec_fold_minus (type
, left_a
, left_b
);
2676 if (!evolution_function_is_constant_p (diff
))
2679 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2680 fprintf (dump_file
, "can_use_subscript_aff_aff_for_symbolic \n");
2682 *chrec_a
= build_polynomial_chrec (CHREC_VARIABLE (*chrec_a
),
2683 diff
, CHREC_RIGHT (*chrec_a
));
2684 right_b
= chrec_convert (type
, CHREC_RIGHT (*chrec_b
), NULL
);
2685 *chrec_b
= build_polynomial_chrec (CHREC_VARIABLE (*chrec_b
),
2686 build_int_cst (type
, 0),
2691 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2692 *OVERLAPS_B are initialized to the functions that describe the
2693 relation between the elements accessed twice by CHREC_A and
2694 CHREC_B. For k >= 0, the following property is verified:
2696 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2699 analyze_siv_subscript (tree chrec_a
,
2701 conflict_function
**overlaps_a
,
2702 conflict_function
**overlaps_b
,
2703 tree
*last_conflicts
,
2706 dependence_stats
.num_siv
++;
2708 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2709 fprintf (dump_file
, "(analyze_siv_subscript \n");
2711 if (evolution_function_is_constant_p (chrec_a
)
2712 && evolution_function_is_affine_in_loop (chrec_b
, loop_nest_num
))
2713 analyze_siv_subscript_cst_affine (chrec_a
, chrec_b
,
2714 overlaps_a
, overlaps_b
, last_conflicts
);
2716 else if (evolution_function_is_affine_in_loop (chrec_a
, loop_nest_num
)
2717 && evolution_function_is_constant_p (chrec_b
))
2718 analyze_siv_subscript_cst_affine (chrec_b
, chrec_a
,
2719 overlaps_b
, overlaps_a
, last_conflicts
);
2721 else if (evolution_function_is_affine_in_loop (chrec_a
, loop_nest_num
)
2722 && evolution_function_is_affine_in_loop (chrec_b
, loop_nest_num
))
2724 if (!chrec_contains_symbols (chrec_a
)
2725 && !chrec_contains_symbols (chrec_b
))
2727 analyze_subscript_affine_affine (chrec_a
, chrec_b
,
2728 overlaps_a
, overlaps_b
,
2731 if (CF_NOT_KNOWN_P (*overlaps_a
)
2732 || CF_NOT_KNOWN_P (*overlaps_b
))
2733 dependence_stats
.num_siv_unimplemented
++;
2734 else if (CF_NO_DEPENDENCE_P (*overlaps_a
)
2735 || CF_NO_DEPENDENCE_P (*overlaps_b
))
2736 dependence_stats
.num_siv_independent
++;
2738 dependence_stats
.num_siv_dependent
++;
2740 else if (can_use_analyze_subscript_affine_affine (&chrec_a
,
2743 analyze_subscript_affine_affine (chrec_a
, chrec_b
,
2744 overlaps_a
, overlaps_b
,
2747 if (CF_NOT_KNOWN_P (*overlaps_a
)
2748 || CF_NOT_KNOWN_P (*overlaps_b
))
2749 dependence_stats
.num_siv_unimplemented
++;
2750 else if (CF_NO_DEPENDENCE_P (*overlaps_a
)
2751 || CF_NO_DEPENDENCE_P (*overlaps_b
))
2752 dependence_stats
.num_siv_independent
++;
2754 dependence_stats
.num_siv_dependent
++;
2757 goto siv_subscript_dontknow
;
2762 siv_subscript_dontknow
:;
2763 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2764 fprintf (dump_file
, " siv test failed: unimplemented");
2765 *overlaps_a
= conflict_fn_not_known ();
2766 *overlaps_b
= conflict_fn_not_known ();
2767 *last_conflicts
= chrec_dont_know
;
2768 dependence_stats
.num_siv_unimplemented
++;
2771 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2772 fprintf (dump_file
, ")\n");
2775 /* Returns false if we can prove that the greatest common divisor of the steps
2776 of CHREC does not divide CST, false otherwise. */
2779 gcd_of_steps_may_divide_p (const_tree chrec
, const_tree cst
)
2781 HOST_WIDE_INT cd
= 0, val
;
2784 if (!host_integerp (cst
, 0))
2786 val
= tree_low_cst (cst
, 0);
2788 while (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
2790 step
= CHREC_RIGHT (chrec
);
2791 if (!host_integerp (step
, 0))
2793 cd
= gcd (cd
, tree_low_cst (step
, 0));
2794 chrec
= CHREC_LEFT (chrec
);
2797 return val
% cd
== 0;
2800 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
2801 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2802 functions that describe the relation between the elements accessed
2803 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2806 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2809 analyze_miv_subscript (tree chrec_a
,
2811 conflict_function
**overlaps_a
,
2812 conflict_function
**overlaps_b
,
2813 tree
*last_conflicts
,
2814 struct loop
*loop_nest
)
2816 tree type
, difference
;
2818 dependence_stats
.num_miv
++;
2819 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2820 fprintf (dump_file
, "(analyze_miv_subscript \n");
2822 type
= signed_type_for_types (TREE_TYPE (chrec_a
), TREE_TYPE (chrec_b
));
2823 chrec_a
= chrec_convert (type
, chrec_a
, NULL
);
2824 chrec_b
= chrec_convert (type
, chrec_b
, NULL
);
2825 difference
= chrec_fold_minus (type
, chrec_a
, chrec_b
);
2827 if (eq_evolutions_p (chrec_a
, chrec_b
))
2829 /* Access functions are the same: all the elements are accessed
2830 in the same order. */
2831 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2832 *overlaps_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2833 *last_conflicts
= max_stmt_executions_tree (get_chrec_loop (chrec_a
));
2834 dependence_stats
.num_miv_dependent
++;
2837 else if (evolution_function_is_constant_p (difference
)
2838 /* For the moment, the following is verified:
2839 evolution_function_is_affine_multivariate_p (chrec_a,
2841 && !gcd_of_steps_may_divide_p (chrec_a
, difference
))
2843 /* testsuite/.../ssa-chrec-33.c
2844 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2846 The difference is 1, and all the evolution steps are multiples
2847 of 2, consequently there are no overlapping elements. */
2848 *overlaps_a
= conflict_fn_no_dependence ();
2849 *overlaps_b
= conflict_fn_no_dependence ();
2850 *last_conflicts
= integer_zero_node
;
2851 dependence_stats
.num_miv_independent
++;
2854 else if (evolution_function_is_affine_multivariate_p (chrec_a
, loop_nest
->num
)
2855 && !chrec_contains_symbols (chrec_a
)
2856 && evolution_function_is_affine_multivariate_p (chrec_b
, loop_nest
->num
)
2857 && !chrec_contains_symbols (chrec_b
))
2859 /* testsuite/.../ssa-chrec-35.c
2860 {0, +, 1}_2 vs. {0, +, 1}_3
2861 the overlapping elements are respectively located at iterations:
2862 {0, +, 1}_x and {0, +, 1}_x,
2863 in other words, we have the equality:
2864 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2867 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2868 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2870 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2871 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2873 analyze_subscript_affine_affine (chrec_a
, chrec_b
,
2874 overlaps_a
, overlaps_b
, last_conflicts
);
2876 if (CF_NOT_KNOWN_P (*overlaps_a
)
2877 || CF_NOT_KNOWN_P (*overlaps_b
))
2878 dependence_stats
.num_miv_unimplemented
++;
2879 else if (CF_NO_DEPENDENCE_P (*overlaps_a
)
2880 || CF_NO_DEPENDENCE_P (*overlaps_b
))
2881 dependence_stats
.num_miv_independent
++;
2883 dependence_stats
.num_miv_dependent
++;
2888 /* When the analysis is too difficult, answer "don't know". */
2889 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2890 fprintf (dump_file
, "analyze_miv_subscript test failed: unimplemented.\n");
2892 *overlaps_a
= conflict_fn_not_known ();
2893 *overlaps_b
= conflict_fn_not_known ();
2894 *last_conflicts
= chrec_dont_know
;
2895 dependence_stats
.num_miv_unimplemented
++;
2898 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2899 fprintf (dump_file
, ")\n");
2902 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
2903 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
2904 OVERLAP_ITERATIONS_B are initialized with two functions that
2905 describe the iterations that contain conflicting elements.
2907 Remark: For an integer k >= 0, the following equality is true:
2909 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
2913 analyze_overlapping_iterations (tree chrec_a
,
2915 conflict_function
**overlap_iterations_a
,
2916 conflict_function
**overlap_iterations_b
,
2917 tree
*last_conflicts
, struct loop
*loop_nest
)
2919 unsigned int lnn
= loop_nest
->num
;
2921 dependence_stats
.num_subscript_tests
++;
2923 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2925 fprintf (dump_file
, "(analyze_overlapping_iterations \n");
2926 fprintf (dump_file
, " (chrec_a = ");
2927 print_generic_expr (dump_file
, chrec_a
, 0);
2928 fprintf (dump_file
, ")\n (chrec_b = ");
2929 print_generic_expr (dump_file
, chrec_b
, 0);
2930 fprintf (dump_file
, ")\n");
2933 if (chrec_a
== NULL_TREE
2934 || chrec_b
== NULL_TREE
2935 || chrec_contains_undetermined (chrec_a
)
2936 || chrec_contains_undetermined (chrec_b
))
2938 dependence_stats
.num_subscript_undetermined
++;
2940 *overlap_iterations_a
= conflict_fn_not_known ();
2941 *overlap_iterations_b
= conflict_fn_not_known ();
2944 /* If they are the same chrec, and are affine, they overlap
2945 on every iteration. */
2946 else if (eq_evolutions_p (chrec_a
, chrec_b
)
2947 && (evolution_function_is_affine_multivariate_p (chrec_a
, lnn
)
2948 || operand_equal_p (chrec_a
, chrec_b
, 0)))
2950 dependence_stats
.num_same_subscript_function
++;
2951 *overlap_iterations_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2952 *overlap_iterations_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2953 *last_conflicts
= chrec_dont_know
;
2956 /* If they aren't the same, and aren't affine, we can't do anything
2958 else if ((chrec_contains_symbols (chrec_a
)
2959 || chrec_contains_symbols (chrec_b
))
2960 && (!evolution_function_is_affine_multivariate_p (chrec_a
, lnn
)
2961 || !evolution_function_is_affine_multivariate_p (chrec_b
, lnn
)))
2963 dependence_stats
.num_subscript_undetermined
++;
2964 *overlap_iterations_a
= conflict_fn_not_known ();
2965 *overlap_iterations_b
= conflict_fn_not_known ();
2968 else if (ziv_subscript_p (chrec_a
, chrec_b
))
2969 analyze_ziv_subscript (chrec_a
, chrec_b
,
2970 overlap_iterations_a
, overlap_iterations_b
,
2973 else if (siv_subscript_p (chrec_a
, chrec_b
))
2974 analyze_siv_subscript (chrec_a
, chrec_b
,
2975 overlap_iterations_a
, overlap_iterations_b
,
2976 last_conflicts
, lnn
);
2979 analyze_miv_subscript (chrec_a
, chrec_b
,
2980 overlap_iterations_a
, overlap_iterations_b
,
2981 last_conflicts
, loop_nest
);
2983 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2985 fprintf (dump_file
, " (overlap_iterations_a = ");
2986 dump_conflict_function (dump_file
, *overlap_iterations_a
);
2987 fprintf (dump_file
, ")\n (overlap_iterations_b = ");
2988 dump_conflict_function (dump_file
, *overlap_iterations_b
);
2989 fprintf (dump_file
, "))\n");
2993 /* Helper function for uniquely inserting distance vectors. */
2996 save_dist_v (struct data_dependence_relation
*ddr
, lambda_vector dist_v
)
3001 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr
), i
, v
)
3002 if (lambda_vector_equal (v
, dist_v
, DDR_NB_LOOPS (ddr
)))
3005 DDR_DIST_VECTS (ddr
).safe_push (dist_v
);
3008 /* Helper function for uniquely inserting direction vectors. */
3011 save_dir_v (struct data_dependence_relation
*ddr
, lambda_vector dir_v
)
3016 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr
), i
, v
)
3017 if (lambda_vector_equal (v
, dir_v
, DDR_NB_LOOPS (ddr
)))
3020 DDR_DIR_VECTS (ddr
).safe_push (dir_v
);
3023 /* Add a distance of 1 on all the loops outer than INDEX. If we
3024 haven't yet determined a distance for this outer loop, push a new
3025 distance vector composed of the previous distance, and a distance
3026 of 1 for this outer loop. Example:
3034 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3035 save (0, 1), then we have to save (1, 0). */
3038 add_outer_distances (struct data_dependence_relation
*ddr
,
3039 lambda_vector dist_v
, int index
)
3041 /* For each outer loop where init_v is not set, the accesses are
3042 in dependence of distance 1 in the loop. */
3043 while (--index
>= 0)
3045 lambda_vector save_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3046 lambda_vector_copy (dist_v
, save_v
, DDR_NB_LOOPS (ddr
));
3048 save_dist_v (ddr
, save_v
);
3052 /* Return false when fail to represent the data dependence as a
3053 distance vector. INIT_B is set to true when a component has been
3054 added to the distance vector DIST_V. INDEX_CARRY is then set to
3055 the index in DIST_V that carries the dependence. */
3058 build_classic_dist_vector_1 (struct data_dependence_relation
*ddr
,
3059 struct data_reference
*ddr_a
,
3060 struct data_reference
*ddr_b
,
3061 lambda_vector dist_v
, bool *init_b
,
3065 lambda_vector init_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3067 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3069 tree access_fn_a
, access_fn_b
;
3070 struct subscript
*subscript
= DDR_SUBSCRIPT (ddr
, i
);
3072 if (chrec_contains_undetermined (SUB_DISTANCE (subscript
)))
3074 non_affine_dependence_relation (ddr
);
3078 access_fn_a
= DR_ACCESS_FN (ddr_a
, i
);
3079 access_fn_b
= DR_ACCESS_FN (ddr_b
, i
);
3081 if (TREE_CODE (access_fn_a
) == POLYNOMIAL_CHREC
3082 && TREE_CODE (access_fn_b
) == POLYNOMIAL_CHREC
)
3085 int var_a
= CHREC_VARIABLE (access_fn_a
);
3086 int var_b
= CHREC_VARIABLE (access_fn_b
);
3089 || chrec_contains_undetermined (SUB_DISTANCE (subscript
)))
3091 non_affine_dependence_relation (ddr
);
3095 dist
= int_cst_value (SUB_DISTANCE (subscript
));
3096 index
= index_in_loop_nest (var_a
, DDR_LOOP_NEST (ddr
));
3097 *index_carry
= MIN (index
, *index_carry
);
3099 /* This is the subscript coupling test. If we have already
3100 recorded a distance for this loop (a distance coming from
3101 another subscript), it should be the same. For example,
3102 in the following code, there is no dependence:
3109 if (init_v
[index
] != 0 && dist_v
[index
] != dist
)
3111 finalize_ddr_dependent (ddr
, chrec_known
);
3115 dist_v
[index
] = dist
;
3119 else if (!operand_equal_p (access_fn_a
, access_fn_b
, 0))
3121 /* This can be for example an affine vs. constant dependence
3122 (T[i] vs. T[3]) that is not an affine dependence and is
3123 not representable as a distance vector. */
3124 non_affine_dependence_relation (ddr
);
3132 /* Return true when the DDR contains only constant access functions. */
3135 constant_access_functions (const struct data_dependence_relation
*ddr
)
3139 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3140 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr
), i
))
3141 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr
), i
)))
3147 /* Helper function for the case where DDR_A and DDR_B are the same
3148 multivariate access function with a constant step. For an example
3152 add_multivariate_self_dist (struct data_dependence_relation
*ddr
, tree c_2
)
3155 tree c_1
= CHREC_LEFT (c_2
);
3156 tree c_0
= CHREC_LEFT (c_1
);
3157 lambda_vector dist_v
;
3160 /* Polynomials with more than 2 variables are not handled yet. When
3161 the evolution steps are parameters, it is not possible to
3162 represent the dependence using classical distance vectors. */
3163 if (TREE_CODE (c_0
) != INTEGER_CST
3164 || TREE_CODE (CHREC_RIGHT (c_1
)) != INTEGER_CST
3165 || TREE_CODE (CHREC_RIGHT (c_2
)) != INTEGER_CST
)
3167 DDR_AFFINE_P (ddr
) = false;
3171 x_2
= index_in_loop_nest (CHREC_VARIABLE (c_2
), DDR_LOOP_NEST (ddr
));
3172 x_1
= index_in_loop_nest (CHREC_VARIABLE (c_1
), DDR_LOOP_NEST (ddr
));
3174 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3175 dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3176 v1
= int_cst_value (CHREC_RIGHT (c_1
));
3177 v2
= int_cst_value (CHREC_RIGHT (c_2
));
3190 save_dist_v (ddr
, dist_v
);
3192 add_outer_distances (ddr
, dist_v
, x_1
);
3195 /* Helper function for the case where DDR_A and DDR_B are the same
3196 access functions. */
3199 add_other_self_distances (struct data_dependence_relation
*ddr
)
3201 lambda_vector dist_v
;
3203 int index_carry
= DDR_NB_LOOPS (ddr
);
3205 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3207 tree access_fun
= DR_ACCESS_FN (DDR_A (ddr
), i
);
3209 if (TREE_CODE (access_fun
) == POLYNOMIAL_CHREC
)
3211 if (!evolution_function_is_univariate_p (access_fun
))
3213 if (DDR_NUM_SUBSCRIPTS (ddr
) != 1)
3215 DDR_ARE_DEPENDENT (ddr
) = chrec_dont_know
;
3219 access_fun
= DR_ACCESS_FN (DDR_A (ddr
), 0);
3221 if (TREE_CODE (CHREC_LEFT (access_fun
)) == POLYNOMIAL_CHREC
)
3222 add_multivariate_self_dist (ddr
, access_fun
);
3224 /* The evolution step is not constant: it varies in
3225 the outer loop, so this cannot be represented by a
3226 distance vector. For example in pr34635.c the
3227 evolution is {0, +, {0, +, 4}_1}_2. */
3228 DDR_AFFINE_P (ddr
) = false;
3233 index_carry
= MIN (index_carry
,
3234 index_in_loop_nest (CHREC_VARIABLE (access_fun
),
3235 DDR_LOOP_NEST (ddr
)));
3239 dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3240 add_outer_distances (ddr
, dist_v
, index_carry
);
3244 insert_innermost_unit_dist_vector (struct data_dependence_relation
*ddr
)
3246 lambda_vector dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3248 dist_v
[DDR_INNER_LOOP (ddr
)] = 1;
3249 save_dist_v (ddr
, dist_v
);
3252 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
3253 is the case for example when access functions are the same and
3254 equal to a constant, as in:
3261 in which case the distance vectors are (0) and (1). */
3264 add_distance_for_zero_overlaps (struct data_dependence_relation
*ddr
)
3268 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3270 subscript_p sub
= DDR_SUBSCRIPT (ddr
, i
);
3271 conflict_function
*ca
= SUB_CONFLICTS_IN_A (sub
);
3272 conflict_function
*cb
= SUB_CONFLICTS_IN_B (sub
);
3274 for (j
= 0; j
< ca
->n
; j
++)
3275 if (affine_function_zero_p (ca
->fns
[j
]))
3277 insert_innermost_unit_dist_vector (ddr
);
3281 for (j
= 0; j
< cb
->n
; j
++)
3282 if (affine_function_zero_p (cb
->fns
[j
]))
3284 insert_innermost_unit_dist_vector (ddr
);
3290 /* Compute the classic per loop distance vector. DDR is the data
3291 dependence relation to build a vector from. Return false when fail
3292 to represent the data dependence as a distance vector. */
3295 build_classic_dist_vector (struct data_dependence_relation
*ddr
,
3296 struct loop
*loop_nest
)
3298 bool init_b
= false;
3299 int index_carry
= DDR_NB_LOOPS (ddr
);
3300 lambda_vector dist_v
;
3302 if (DDR_ARE_DEPENDENT (ddr
) != NULL_TREE
)
3305 if (same_access_functions (ddr
))
3307 /* Save the 0 vector. */
3308 dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3309 save_dist_v (ddr
, dist_v
);
3311 if (constant_access_functions (ddr
))
3312 add_distance_for_zero_overlaps (ddr
);
3314 if (DDR_NB_LOOPS (ddr
) > 1)
3315 add_other_self_distances (ddr
);
3320 dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3321 if (!build_classic_dist_vector_1 (ddr
, DDR_A (ddr
), DDR_B (ddr
),
3322 dist_v
, &init_b
, &index_carry
))
3325 /* Save the distance vector if we initialized one. */
3328 /* Verify a basic constraint: classic distance vectors should
3329 always be lexicographically positive.
3331 Data references are collected in the order of execution of
3332 the program, thus for the following loop
3334 | for (i = 1; i < 100; i++)
3335 | for (j = 1; j < 100; j++)
3337 | t = T[j+1][i-1]; // A
3338 | T[j][i] = t + 2; // B
3341 references are collected following the direction of the wind:
3342 A then B. The data dependence tests are performed also
3343 following this order, such that we're looking at the distance
3344 separating the elements accessed by A from the elements later
3345 accessed by B. But in this example, the distance returned by
3346 test_dep (A, B) is lexicographically negative (-1, 1), that
3347 means that the access A occurs later than B with respect to
3348 the outer loop, ie. we're actually looking upwind. In this
3349 case we solve test_dep (B, A) looking downwind to the
3350 lexicographically positive solution, that returns the
3351 distance vector (1, -1). */
3352 if (!lambda_vector_lexico_pos (dist_v
, DDR_NB_LOOPS (ddr
)))
3354 lambda_vector save_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3355 if (!subscript_dependence_tester_1 (ddr
, DDR_B (ddr
), DDR_A (ddr
),
3358 compute_subscript_distance (ddr
);
3359 if (!build_classic_dist_vector_1 (ddr
, DDR_B (ddr
), DDR_A (ddr
),
3360 save_v
, &init_b
, &index_carry
))
3362 save_dist_v (ddr
, save_v
);
3363 DDR_REVERSED_P (ddr
) = true;
3365 /* In this case there is a dependence forward for all the
3368 | for (k = 1; k < 100; k++)
3369 | for (i = 1; i < 100; i++)
3370 | for (j = 1; j < 100; j++)
3372 | t = T[j+1][i-1]; // A
3373 | T[j][i] = t + 2; // B
3381 if (DDR_NB_LOOPS (ddr
) > 1)
3383 add_outer_distances (ddr
, save_v
, index_carry
);
3384 add_outer_distances (ddr
, dist_v
, index_carry
);
3389 lambda_vector save_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3390 lambda_vector_copy (dist_v
, save_v
, DDR_NB_LOOPS (ddr
));
3392 if (DDR_NB_LOOPS (ddr
) > 1)
3394 lambda_vector opposite_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3396 if (!subscript_dependence_tester_1 (ddr
, DDR_B (ddr
),
3397 DDR_A (ddr
), loop_nest
))
3399 compute_subscript_distance (ddr
);
3400 if (!build_classic_dist_vector_1 (ddr
, DDR_B (ddr
), DDR_A (ddr
),
3401 opposite_v
, &init_b
,
3405 save_dist_v (ddr
, save_v
);
3406 add_outer_distances (ddr
, dist_v
, index_carry
);
3407 add_outer_distances (ddr
, opposite_v
, index_carry
);
3410 save_dist_v (ddr
, save_v
);
3415 /* There is a distance of 1 on all the outer loops: Example:
3416 there is a dependence of distance 1 on loop_1 for the array A.
3422 add_outer_distances (ddr
, dist_v
,
3423 lambda_vector_first_nz (dist_v
,
3424 DDR_NB_LOOPS (ddr
), 0));
3427 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3431 fprintf (dump_file
, "(build_classic_dist_vector\n");
3432 for (i
= 0; i
< DDR_NUM_DIST_VECTS (ddr
); i
++)
3434 fprintf (dump_file
, " dist_vector = (");
3435 print_lambda_vector (dump_file
, DDR_DIST_VECT (ddr
, i
),
3436 DDR_NB_LOOPS (ddr
));
3437 fprintf (dump_file
, " )\n");
3439 fprintf (dump_file
, ")\n");
3445 /* Return the direction for a given distance.
3446 FIXME: Computing dir this way is suboptimal, since dir can catch
3447 cases that dist is unable to represent. */
3449 static inline enum data_dependence_direction
3450 dir_from_dist (int dist
)
3453 return dir_positive
;
3455 return dir_negative
;
3460 /* Compute the classic per loop direction vector. DDR is the data
3461 dependence relation to build a vector from. */
3464 build_classic_dir_vector (struct data_dependence_relation
*ddr
)
3467 lambda_vector dist_v
;
3469 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr
), i
, dist_v
)
3471 lambda_vector dir_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3473 for (j
= 0; j
< DDR_NB_LOOPS (ddr
); j
++)
3474 dir_v
[j
] = dir_from_dist (dist_v
[j
]);
3476 save_dir_v (ddr
, dir_v
);
3480 /* Helper function. Returns true when there is a dependence between
3481 data references DRA and DRB. */
3484 subscript_dependence_tester_1 (struct data_dependence_relation
*ddr
,
3485 struct data_reference
*dra
,
3486 struct data_reference
*drb
,
3487 struct loop
*loop_nest
)
3490 tree last_conflicts
;
3491 struct subscript
*subscript
;
3492 tree res
= NULL_TREE
;
3494 for (i
= 0; DDR_SUBSCRIPTS (ddr
).iterate (i
, &subscript
); i
++)
3496 conflict_function
*overlaps_a
, *overlaps_b
;
3498 analyze_overlapping_iterations (DR_ACCESS_FN (dra
, i
),
3499 DR_ACCESS_FN (drb
, i
),
3500 &overlaps_a
, &overlaps_b
,
3501 &last_conflicts
, loop_nest
);
3503 if (SUB_CONFLICTS_IN_A (subscript
))
3504 free_conflict_function (SUB_CONFLICTS_IN_A (subscript
));
3505 if (SUB_CONFLICTS_IN_B (subscript
))
3506 free_conflict_function (SUB_CONFLICTS_IN_B (subscript
));
3508 SUB_CONFLICTS_IN_A (subscript
) = overlaps_a
;
3509 SUB_CONFLICTS_IN_B (subscript
) = overlaps_b
;
3510 SUB_LAST_CONFLICT (subscript
) = last_conflicts
;
3512 /* If there is any undetermined conflict function we have to
3513 give a conservative answer in case we cannot prove that
3514 no dependence exists when analyzing another subscript. */
3515 if (CF_NOT_KNOWN_P (overlaps_a
)
3516 || CF_NOT_KNOWN_P (overlaps_b
))
3518 res
= chrec_dont_know
;
3522 /* When there is a subscript with no dependence we can stop. */
3523 else if (CF_NO_DEPENDENCE_P (overlaps_a
)
3524 || CF_NO_DEPENDENCE_P (overlaps_b
))
3531 if (res
== NULL_TREE
)
3534 if (res
== chrec_known
)
3535 dependence_stats
.num_dependence_independent
++;
3537 dependence_stats
.num_dependence_undetermined
++;
3538 finalize_ddr_dependent (ddr
, res
);
3542 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
3545 subscript_dependence_tester (struct data_dependence_relation
*ddr
,
3546 struct loop
*loop_nest
)
3548 if (subscript_dependence_tester_1 (ddr
, DDR_A (ddr
), DDR_B (ddr
), loop_nest
))
3549 dependence_stats
.num_dependence_dependent
++;
3551 compute_subscript_distance (ddr
);
3552 if (build_classic_dist_vector (ddr
, loop_nest
))
3553 build_classic_dir_vector (ddr
);
3556 /* Returns true when all the access functions of A are affine or
3557 constant with respect to LOOP_NEST. */
3560 access_functions_are_affine_or_constant_p (const struct data_reference
*a
,
3561 const struct loop
*loop_nest
)
3564 vec
<tree
> fns
= DR_ACCESS_FNS (a
);
3567 FOR_EACH_VEC_ELT (fns
, i
, t
)
3568 if (!evolution_function_is_invariant_p (t
, loop_nest
->num
)
3569 && !evolution_function_is_affine_multivariate_p (t
, loop_nest
->num
))
3575 /* Initializes an equation for an OMEGA problem using the information
3576 contained in the ACCESS_FUN. Returns true when the operation
3579 PB is the omega constraint system.
3580 EQ is the number of the equation to be initialized.
3581 OFFSET is used for shifting the variables names in the constraints:
3582 a constrain is composed of 2 * the number of variables surrounding
3583 dependence accesses. OFFSET is set either to 0 for the first n variables,
3584 then it is set to n.
3585 ACCESS_FUN is expected to be an affine chrec. */
3588 init_omega_eq_with_af (omega_pb pb
, unsigned eq
,
3589 unsigned int offset
, tree access_fun
,
3590 struct data_dependence_relation
*ddr
)
3592 switch (TREE_CODE (access_fun
))
3594 case POLYNOMIAL_CHREC
:
3596 tree left
= CHREC_LEFT (access_fun
);
3597 tree right
= CHREC_RIGHT (access_fun
);
3598 int var
= CHREC_VARIABLE (access_fun
);
3601 if (TREE_CODE (right
) != INTEGER_CST
)
3604 var_idx
= index_in_loop_nest (var
, DDR_LOOP_NEST (ddr
));
3605 pb
->eqs
[eq
].coef
[offset
+ var_idx
+ 1] = int_cst_value (right
);
3607 /* Compute the innermost loop index. */
3608 DDR_INNER_LOOP (ddr
) = MAX (DDR_INNER_LOOP (ddr
), var_idx
);
3611 pb
->eqs
[eq
].coef
[var_idx
+ DDR_NB_LOOPS (ddr
) + 1]
3612 += int_cst_value (right
);
3614 switch (TREE_CODE (left
))
3616 case POLYNOMIAL_CHREC
:
3617 return init_omega_eq_with_af (pb
, eq
, offset
, left
, ddr
);
3620 pb
->eqs
[eq
].coef
[0] += int_cst_value (left
);
3629 pb
->eqs
[eq
].coef
[0] += int_cst_value (access_fun
);
3637 /* As explained in the comments preceding init_omega_for_ddr, we have
3638 to set up a system for each loop level, setting outer loops
3639 variation to zero, and current loop variation to positive or zero.
3640 Save each lexico positive distance vector. */
3643 omega_extract_distance_vectors (omega_pb pb
,
3644 struct data_dependence_relation
*ddr
)
3648 struct loop
*loopi
, *loopj
;
3649 enum omega_result res
;
3651 /* Set a new problem for each loop in the nest. The basis is the
3652 problem that we have initialized until now. On top of this we
3653 add new constraints. */
3654 for (i
= 0; i
<= DDR_INNER_LOOP (ddr
)
3655 && DDR_LOOP_NEST (ddr
).iterate (i
, &loopi
); i
++)
3658 omega_pb copy
= omega_alloc_problem (2 * DDR_NB_LOOPS (ddr
),
3659 DDR_NB_LOOPS (ddr
));
3661 omega_copy_problem (copy
, pb
);
3663 /* For all the outer loops "loop_j", add "dj = 0". */
3664 for (j
= 0; j
< i
&& DDR_LOOP_NEST (ddr
).iterate (j
, &loopj
); j
++)
3666 eq
= omega_add_zero_eq (copy
, omega_black
);
3667 copy
->eqs
[eq
].coef
[j
+ 1] = 1;
3670 /* For "loop_i", add "0 <= di". */
3671 geq
= omega_add_zero_geq (copy
, omega_black
);
3672 copy
->geqs
[geq
].coef
[i
+ 1] = 1;
3674 /* Reduce the constraint system, and test that the current
3675 problem is feasible. */
3676 res
= omega_simplify_problem (copy
);
3677 if (res
== omega_false
3678 || res
== omega_unknown
3679 || copy
->num_geqs
> (int) DDR_NB_LOOPS (ddr
))
3682 for (eq
= 0; eq
< copy
->num_subs
; eq
++)
3683 if (copy
->subs
[eq
].key
== (int) i
+ 1)
3685 dist
= copy
->subs
[eq
].coef
[0];
3691 /* Reinitialize problem... */
3692 omega_copy_problem (copy
, pb
);
3693 for (j
= 0; j
< i
&& DDR_LOOP_NEST (ddr
).iterate (j
, &loopj
); j
++)
3695 eq
= omega_add_zero_eq (copy
, omega_black
);
3696 copy
->eqs
[eq
].coef
[j
+ 1] = 1;
3699 /* ..., but this time "di = 1". */
3700 eq
= omega_add_zero_eq (copy
, omega_black
);
3701 copy
->eqs
[eq
].coef
[i
+ 1] = 1;
3702 copy
->eqs
[eq
].coef
[0] = -1;
3704 res
= omega_simplify_problem (copy
);
3705 if (res
== omega_false
3706 || res
== omega_unknown
3707 || copy
->num_geqs
> (int) DDR_NB_LOOPS (ddr
))
3710 for (eq
= 0; eq
< copy
->num_subs
; eq
++)
3711 if (copy
->subs
[eq
].key
== (int) i
+ 1)
3713 dist
= copy
->subs
[eq
].coef
[0];
3719 /* Save the lexicographically positive distance vector. */
3722 lambda_vector dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3723 lambda_vector dir_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3727 for (eq
= 0; eq
< copy
->num_subs
; eq
++)
3728 if (copy
->subs
[eq
].key
> 0)
3730 dist
= copy
->subs
[eq
].coef
[0];
3731 dist_v
[copy
->subs
[eq
].key
- 1] = dist
;
3734 for (j
= 0; j
< DDR_NB_LOOPS (ddr
); j
++)
3735 dir_v
[j
] = dir_from_dist (dist_v
[j
]);
3737 save_dist_v (ddr
, dist_v
);
3738 save_dir_v (ddr
, dir_v
);
3742 omega_free_problem (copy
);
3746 /* This is called for each subscript of a tuple of data references:
3747 insert an equality for representing the conflicts. */
3750 omega_setup_subscript (tree access_fun_a
, tree access_fun_b
,
3751 struct data_dependence_relation
*ddr
,
3752 omega_pb pb
, bool *maybe_dependent
)
3755 tree type
= signed_type_for_types (TREE_TYPE (access_fun_a
),
3756 TREE_TYPE (access_fun_b
));
3757 tree fun_a
= chrec_convert (type
, access_fun_a
, NULL
);
3758 tree fun_b
= chrec_convert (type
, access_fun_b
, NULL
);
3759 tree difference
= chrec_fold_minus (type
, fun_a
, fun_b
);
3762 /* When the fun_a - fun_b is not constant, the dependence is not
3763 captured by the classic distance vector representation. */
3764 if (TREE_CODE (difference
) != INTEGER_CST
)
3768 if (ziv_subscript_p (fun_a
, fun_b
) && !integer_zerop (difference
))
3770 /* There is no dependence. */
3771 *maybe_dependent
= false;
3775 minus_one
= build_int_cst (type
, -1);
3776 fun_b
= chrec_fold_multiply (type
, fun_b
, minus_one
);
3778 eq
= omega_add_zero_eq (pb
, omega_black
);
3779 if (!init_omega_eq_with_af (pb
, eq
, DDR_NB_LOOPS (ddr
), fun_a
, ddr
)
3780 || !init_omega_eq_with_af (pb
, eq
, 0, fun_b
, ddr
))
3781 /* There is probably a dependence, but the system of
3782 constraints cannot be built: answer "don't know". */
3786 if (DDR_NB_LOOPS (ddr
) != 0 && pb
->eqs
[eq
].coef
[0]
3787 && !int_divides_p (lambda_vector_gcd
3788 ((lambda_vector
) &(pb
->eqs
[eq
].coef
[1]),
3789 2 * DDR_NB_LOOPS (ddr
)),
3790 pb
->eqs
[eq
].coef
[0]))
3792 /* There is no dependence. */
3793 *maybe_dependent
= false;
3800 /* Helper function, same as init_omega_for_ddr but specialized for
3801 data references A and B. */
3804 init_omega_for_ddr_1 (struct data_reference
*dra
, struct data_reference
*drb
,
3805 struct data_dependence_relation
*ddr
,
3806 omega_pb pb
, bool *maybe_dependent
)
3811 unsigned nb_loops
= DDR_NB_LOOPS (ddr
);
3813 /* Insert an equality per subscript. */
3814 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3816 if (!omega_setup_subscript (DR_ACCESS_FN (dra
, i
), DR_ACCESS_FN (drb
, i
),
3817 ddr
, pb
, maybe_dependent
))
3819 else if (*maybe_dependent
== false)
3821 /* There is no dependence. */
3822 DDR_ARE_DEPENDENT (ddr
) = chrec_known
;
3827 /* Insert inequalities: constraints corresponding to the iteration
3828 domain, i.e. the loops surrounding the references "loop_x" and
3829 the distance variables "dx". The layout of the OMEGA
3830 representation is as follows:
3831 - coef[0] is the constant
3832 - coef[1..nb_loops] are the protected variables that will not be
3833 removed by the solver: the "dx"
3834 - coef[nb_loops + 1, 2*nb_loops] are the loop variables: "loop_x".
3836 for (i
= 0; i
<= DDR_INNER_LOOP (ddr
)
3837 && DDR_LOOP_NEST (ddr
).iterate (i
, &loopi
); i
++)
3839 HOST_WIDE_INT nbi
= max_stmt_executions_int (loopi
);
3842 ineq
= omega_add_zero_geq (pb
, omega_black
);
3843 pb
->geqs
[ineq
].coef
[i
+ nb_loops
+ 1] = 1;
3845 /* 0 <= loop_x + dx */
3846 ineq
= omega_add_zero_geq (pb
, omega_black
);
3847 pb
->geqs
[ineq
].coef
[i
+ nb_loops
+ 1] = 1;
3848 pb
->geqs
[ineq
].coef
[i
+ 1] = 1;
3852 /* loop_x <= nb_iters */
3853 ineq
= omega_add_zero_geq (pb
, omega_black
);
3854 pb
->geqs
[ineq
].coef
[i
+ nb_loops
+ 1] = -1;
3855 pb
->geqs
[ineq
].coef
[0] = nbi
;
3857 /* loop_x + dx <= nb_iters */
3858 ineq
= omega_add_zero_geq (pb
, omega_black
);
3859 pb
->geqs
[ineq
].coef
[i
+ nb_loops
+ 1] = -1;
3860 pb
->geqs
[ineq
].coef
[i
+ 1] = -1;
3861 pb
->geqs
[ineq
].coef
[0] = nbi
;
3863 /* A step "dx" bigger than nb_iters is not feasible, so
3864 add "0 <= nb_iters + dx", */
3865 ineq
= omega_add_zero_geq (pb
, omega_black
);
3866 pb
->geqs
[ineq
].coef
[i
+ 1] = 1;
3867 pb
->geqs
[ineq
].coef
[0] = nbi
;
3868 /* and "dx <= nb_iters". */
3869 ineq
= omega_add_zero_geq (pb
, omega_black
);
3870 pb
->geqs
[ineq
].coef
[i
+ 1] = -1;
3871 pb
->geqs
[ineq
].coef
[0] = nbi
;
3875 omega_extract_distance_vectors (pb
, ddr
);
3880 /* Sets up the Omega dependence problem for the data dependence
3881 relation DDR. Returns false when the constraint system cannot be
3882 built, ie. when the test answers "don't know". Returns true
3883 otherwise, and when independence has been proved (using one of the
3884 trivial dependence test), set MAYBE_DEPENDENT to false, otherwise
3885 set MAYBE_DEPENDENT to true.
3887 Example: for setting up the dependence system corresponding to the
3888 conflicting accesses
3893 | ... A[2*j, 2*(i + j)]
3897 the following constraints come from the iteration domain:
3904 where di, dj are the distance variables. The constraints
3905 representing the conflicting elements are:
3908 i + 1 = 2 * (i + di + j + dj)
3910 For asking that the resulting distance vector (di, dj) be
3911 lexicographically positive, we insert the constraint "di >= 0". If
3912 "di = 0" in the solution, we fix that component to zero, and we
3913 look at the inner loops: we set a new problem where all the outer
3914 loop distances are zero, and fix this inner component to be
3915 positive. When one of the components is positive, we save that
3916 distance, and set a new problem where the distance on this loop is
3917 zero, searching for other distances in the inner loops. Here is
3918 the classic example that illustrates that we have to set for each
3919 inner loop a new problem:
3927 we have to save two distances (1, 0) and (0, 1).
3929 Given two array references, refA and refB, we have to set the
3930 dependence problem twice, refA vs. refB and refB vs. refA, and we
3931 cannot do a single test, as refB might occur before refA in the
3932 inner loops, and the contrary when considering outer loops: ex.
3937 | T[{1,+,1}_2][{1,+,1}_1] // refA
3938 | T[{2,+,1}_2][{0,+,1}_1] // refB
3943 refB touches the elements in T before refA, and thus for the same
3944 loop_0 refB precedes refA: ie. the distance vector (0, 1, -1)
3945 but for successive loop_0 iterations, we have (1, -1, 1)
3947 The Omega solver expects the distance variables ("di" in the
3948 previous example) to come first in the constraint system (as
3949 variables to be protected, or "safe" variables), the constraint
3950 system is built using the following layout:
3952 "cst | distance vars | index vars".
3956 init_omega_for_ddr (struct data_dependence_relation
*ddr
,
3957 bool *maybe_dependent
)
3962 *maybe_dependent
= true;
3964 if (same_access_functions (ddr
))
3967 lambda_vector dir_v
;
3969 /* Save the 0 vector. */
3970 save_dist_v (ddr
, lambda_vector_new (DDR_NB_LOOPS (ddr
)));
3971 dir_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3972 for (j
= 0; j
< DDR_NB_LOOPS (ddr
); j
++)
3973 dir_v
[j
] = dir_equal
;
3974 save_dir_v (ddr
, dir_v
);
3976 /* Save the dependences carried by outer loops. */
3977 pb
= omega_alloc_problem (2 * DDR_NB_LOOPS (ddr
), DDR_NB_LOOPS (ddr
));
3978 res
= init_omega_for_ddr_1 (DDR_A (ddr
), DDR_B (ddr
), ddr
, pb
,
3980 omega_free_problem (pb
);
3984 /* Omega expects the protected variables (those that have to be kept
3985 after elimination) to appear first in the constraint system.
3986 These variables are the distance variables. In the following
3987 initialization we declare NB_LOOPS safe variables, and the total
3988 number of variables for the constraint system is 2*NB_LOOPS. */
3989 pb
= omega_alloc_problem (2 * DDR_NB_LOOPS (ddr
), DDR_NB_LOOPS (ddr
));
3990 res
= init_omega_for_ddr_1 (DDR_A (ddr
), DDR_B (ddr
), ddr
, pb
,
3992 omega_free_problem (pb
);
3994 /* Stop computation if not decidable, or no dependence. */
3995 if (res
== false || *maybe_dependent
== false)
3998 pb
= omega_alloc_problem (2 * DDR_NB_LOOPS (ddr
), DDR_NB_LOOPS (ddr
));
3999 res
= init_omega_for_ddr_1 (DDR_B (ddr
), DDR_A (ddr
), ddr
, pb
,
4001 omega_free_problem (pb
);
4006 /* Return true when DDR contains the same information as that stored
4007 in DIR_VECTS and in DIST_VECTS, return false otherwise. */
4010 ddr_consistent_p (FILE *file
,
4011 struct data_dependence_relation
*ddr
,
4012 vec
<lambda_vector
> dist_vects
,
4013 vec
<lambda_vector
> dir_vects
)
4017 /* If dump_file is set, output there. */
4018 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4021 if (dist_vects
.length () != DDR_NUM_DIST_VECTS (ddr
))
4023 lambda_vector b_dist_v
;
4024 fprintf (file
, "\n(Number of distance vectors differ: Banerjee has %d, Omega has %d.\n",
4025 dist_vects
.length (),
4026 DDR_NUM_DIST_VECTS (ddr
));
4028 fprintf (file
, "Banerjee dist vectors:\n");
4029 FOR_EACH_VEC_ELT (dist_vects
, i
, b_dist_v
)
4030 print_lambda_vector (file
, b_dist_v
, DDR_NB_LOOPS (ddr
));
4032 fprintf (file
, "Omega dist vectors:\n");
4033 for (i
= 0; i
< DDR_NUM_DIST_VECTS (ddr
); i
++)
4034 print_lambda_vector (file
, DDR_DIST_VECT (ddr
, i
), DDR_NB_LOOPS (ddr
));
4036 fprintf (file
, "data dependence relation:\n");
4037 dump_data_dependence_relation (file
, ddr
);
4039 fprintf (file
, ")\n");
4043 if (dir_vects
.length () != DDR_NUM_DIR_VECTS (ddr
))
4045 fprintf (file
, "\n(Number of direction vectors differ: Banerjee has %d, Omega has %d.)\n",
4046 dir_vects
.length (),
4047 DDR_NUM_DIR_VECTS (ddr
));
4051 for (i
= 0; i
< DDR_NUM_DIST_VECTS (ddr
); i
++)
4053 lambda_vector a_dist_v
;
4054 lambda_vector b_dist_v
= DDR_DIST_VECT (ddr
, i
);
4056 /* Distance vectors are not ordered in the same way in the DDR
4057 and in the DIST_VECTS: search for a matching vector. */
4058 FOR_EACH_VEC_ELT (dist_vects
, j
, a_dist_v
)
4059 if (lambda_vector_equal (a_dist_v
, b_dist_v
, DDR_NB_LOOPS (ddr
)))
4062 if (j
== dist_vects
.length ())
4064 fprintf (file
, "\n(Dist vectors from the first dependence analyzer:\n");
4065 print_dist_vectors (file
, dist_vects
, DDR_NB_LOOPS (ddr
));
4066 fprintf (file
, "not found in Omega dist vectors:\n");
4067 print_dist_vectors (file
, DDR_DIST_VECTS (ddr
), DDR_NB_LOOPS (ddr
));
4068 fprintf (file
, "data dependence relation:\n");
4069 dump_data_dependence_relation (file
, ddr
);
4070 fprintf (file
, ")\n");
4074 for (i
= 0; i
< DDR_NUM_DIR_VECTS (ddr
); i
++)
4076 lambda_vector a_dir_v
;
4077 lambda_vector b_dir_v
= DDR_DIR_VECT (ddr
, i
);
4079 /* Direction vectors are not ordered in the same way in the DDR
4080 and in the DIR_VECTS: search for a matching vector. */
4081 FOR_EACH_VEC_ELT (dir_vects
, j
, a_dir_v
)
4082 if (lambda_vector_equal (a_dir_v
, b_dir_v
, DDR_NB_LOOPS (ddr
)))
4085 if (j
== dist_vects
.length ())
4087 fprintf (file
, "\n(Dir vectors from the first dependence analyzer:\n");
4088 print_dir_vectors (file
, dir_vects
, DDR_NB_LOOPS (ddr
));
4089 fprintf (file
, "not found in Omega dir vectors:\n");
4090 print_dir_vectors (file
, DDR_DIR_VECTS (ddr
), DDR_NB_LOOPS (ddr
));
4091 fprintf (file
, "data dependence relation:\n");
4092 dump_data_dependence_relation (file
, ddr
);
4093 fprintf (file
, ")\n");
4100 /* This computes the affine dependence relation between A and B with
4101 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
4102 independence between two accesses, while CHREC_DONT_KNOW is used
4103 for representing the unknown relation.
4105 Note that it is possible to stop the computation of the dependence
4106 relation the first time we detect a CHREC_KNOWN element for a given
4110 compute_affine_dependence (struct data_dependence_relation
*ddr
,
4111 struct loop
*loop_nest
)
4113 struct data_reference
*dra
= DDR_A (ddr
);
4114 struct data_reference
*drb
= DDR_B (ddr
);
4116 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4118 fprintf (dump_file
, "(compute_affine_dependence\n");
4119 fprintf (dump_file
, " stmt_a: ");
4120 print_gimple_stmt (dump_file
, DR_STMT (dra
), 0, TDF_SLIM
);
4121 fprintf (dump_file
, " stmt_b: ");
4122 print_gimple_stmt (dump_file
, DR_STMT (drb
), 0, TDF_SLIM
);
4125 /* Analyze only when the dependence relation is not yet known. */
4126 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
4128 dependence_stats
.num_dependence_tests
++;
4130 if (access_functions_are_affine_or_constant_p (dra
, loop_nest
)
4131 && access_functions_are_affine_or_constant_p (drb
, loop_nest
))
4133 subscript_dependence_tester (ddr
, loop_nest
);
4135 if (flag_check_data_deps
)
4137 /* Dump the dependences from the first algorithm. */
4138 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4140 fprintf (dump_file
, "\n\nBanerjee Analyzer\n");
4141 dump_data_dependence_relation (dump_file
, ddr
);
4144 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
4146 bool maybe_dependent
;
4147 vec
<lambda_vector
> dir_vects
, dist_vects
;
4149 /* Save the result of the first DD analyzer. */
4150 dist_vects
= DDR_DIST_VECTS (ddr
);
4151 dir_vects
= DDR_DIR_VECTS (ddr
);
4153 /* Reset the information. */
4154 DDR_DIST_VECTS (ddr
).create (0);
4155 DDR_DIR_VECTS (ddr
).create (0);
4157 /* Compute the same information using Omega. */
4158 if (!init_omega_for_ddr (ddr
, &maybe_dependent
))
4159 goto csys_dont_know
;
4161 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4163 fprintf (dump_file
, "Omega Analyzer\n");
4164 dump_data_dependence_relation (dump_file
, ddr
);
4167 /* Check that we get the same information. */
4168 if (maybe_dependent
)
4169 gcc_assert (ddr_consistent_p (stderr
, ddr
, dist_vects
,
4175 /* As a last case, if the dependence cannot be determined, or if
4176 the dependence is considered too difficult to determine, answer
4181 dependence_stats
.num_dependence_undetermined
++;
4183 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4185 fprintf (dump_file
, "Data ref a:\n");
4186 dump_data_reference (dump_file
, dra
);
4187 fprintf (dump_file
, "Data ref b:\n");
4188 dump_data_reference (dump_file
, drb
);
4189 fprintf (dump_file
, "affine dependence test not usable: access function not affine or constant.\n");
4191 finalize_ddr_dependent (ddr
, chrec_dont_know
);
4195 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4197 if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
4198 fprintf (dump_file
, ") -> no dependence\n");
4199 else if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
4200 fprintf (dump_file
, ") -> dependence analysis failed\n");
4202 fprintf (dump_file
, ")\n");
4206 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
4207 the data references in DATAREFS, in the LOOP_NEST. When
4208 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
4209 relations. Return true when successful, i.e. data references number
4210 is small enough to be handled. */
4213 compute_all_dependences (vec
<data_reference_p
> datarefs
,
4214 vec
<ddr_p
> *dependence_relations
,
4215 vec
<loop_p
> loop_nest
,
4216 bool compute_self_and_rr
)
4218 struct data_dependence_relation
*ddr
;
4219 struct data_reference
*a
, *b
;
4222 if ((int) datarefs
.length ()
4223 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS
))
4225 struct data_dependence_relation
*ddr
;
4227 /* Insert a single relation into dependence_relations:
4229 ddr
= initialize_data_dependence_relation (NULL
, NULL
, loop_nest
);
4230 dependence_relations
->safe_push (ddr
);
4234 FOR_EACH_VEC_ELT (datarefs
, i
, a
)
4235 for (j
= i
+ 1; datarefs
.iterate (j
, &b
); j
++)
4236 if (DR_IS_WRITE (a
) || DR_IS_WRITE (b
) || compute_self_and_rr
)
4238 ddr
= initialize_data_dependence_relation (a
, b
, loop_nest
);
4239 dependence_relations
->safe_push (ddr
);
4240 if (loop_nest
.exists ())
4241 compute_affine_dependence (ddr
, loop_nest
[0]);
4244 if (compute_self_and_rr
)
4245 FOR_EACH_VEC_ELT (datarefs
, i
, a
)
4247 ddr
= initialize_data_dependence_relation (a
, a
, loop_nest
);
4248 dependence_relations
->safe_push (ddr
);
4249 if (loop_nest
.exists ())
4250 compute_affine_dependence (ddr
, loop_nest
[0]);
4256 /* Describes a location of a memory reference. */
4258 typedef struct data_ref_loc_d
4260 /* Position of the memory reference. */
4263 /* True if the memory reference is read. */
4268 /* Stores the locations of memory references in STMT to REFERENCES. Returns
4269 true if STMT clobbers memory, false otherwise. */
4272 get_references_in_stmt (gimple stmt
, vec
<data_ref_loc
> *references
)
4274 bool clobbers_memory
= false;
4277 enum gimple_code stmt_code
= gimple_code (stmt
);
4279 references
->create (0);
4281 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4282 As we cannot model data-references to not spelled out
4283 accesses give up if they may occur. */
4284 if ((stmt_code
== GIMPLE_CALL
4285 && !(gimple_call_flags (stmt
) & ECF_CONST
))
4286 || (stmt_code
== GIMPLE_ASM
4287 && (gimple_asm_volatile_p (stmt
) || gimple_vuse (stmt
))))
4288 clobbers_memory
= true;
4290 if (!gimple_vuse (stmt
))
4291 return clobbers_memory
;
4293 if (stmt_code
== GIMPLE_ASSIGN
)
4296 op0
= gimple_assign_lhs_ptr (stmt
);
4297 op1
= gimple_assign_rhs1_ptr (stmt
);
4300 || (REFERENCE_CLASS_P (*op1
)
4301 && (base
= get_base_address (*op1
))
4302 && TREE_CODE (base
) != SSA_NAME
))
4306 references
->safe_push (ref
);
4309 else if (stmt_code
== GIMPLE_CALL
)
4313 op0
= gimple_call_lhs_ptr (stmt
);
4314 n
= gimple_call_num_args (stmt
);
4315 for (i
= 0; i
< n
; i
++)
4317 op1
= gimple_call_arg_ptr (stmt
, i
);
4320 || (REFERENCE_CLASS_P (*op1
) && get_base_address (*op1
)))
4324 references
->safe_push (ref
);
4329 return clobbers_memory
;
4333 || (REFERENCE_CLASS_P (*op0
) && get_base_address (*op0
))))
4336 ref
.is_read
= false;
4337 references
->safe_push (ref
);
4339 return clobbers_memory
;
4342 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
4343 reference, returns false, otherwise returns true. NEST is the outermost
4344 loop of the loop nest in which the references should be analyzed. */
4347 find_data_references_in_stmt (struct loop
*nest
, gimple stmt
,
4348 vec
<data_reference_p
> *datarefs
)
4351 vec
<data_ref_loc
> references
;
4354 data_reference_p dr
;
4356 if (get_references_in_stmt (stmt
, &references
))
4358 references
.release ();
4362 FOR_EACH_VEC_ELT (references
, i
, ref
)
4364 dr
= create_data_ref (nest
, loop_containing_stmt (stmt
),
4365 *ref
->pos
, stmt
, ref
->is_read
);
4366 gcc_assert (dr
!= NULL
);
4367 datarefs
->safe_push (dr
);
4369 references
.release ();
4373 /* Stores the data references in STMT to DATAREFS. If there is an
4374 unanalyzable reference, returns false, otherwise returns true.
4375 NEST is the outermost loop of the loop nest in which the references
4376 should be instantiated, LOOP is the loop in which the references
4377 should be analyzed. */
4380 graphite_find_data_references_in_stmt (loop_p nest
, loop_p loop
, gimple stmt
,
4381 vec
<data_reference_p
> *datarefs
)
4384 vec
<data_ref_loc
> references
;
4387 data_reference_p dr
;
4389 if (get_references_in_stmt (stmt
, &references
))
4391 references
.release ();
4395 FOR_EACH_VEC_ELT (references
, i
, ref
)
4397 dr
= create_data_ref (nest
, loop
, *ref
->pos
, stmt
, ref
->is_read
);
4398 gcc_assert (dr
!= NULL
);
4399 datarefs
->safe_push (dr
);
4402 references
.release ();
4406 /* Search the data references in LOOP, and record the information into
4407 DATAREFS. Returns chrec_dont_know when failing to analyze a
4408 difficult case, returns NULL_TREE otherwise. */
4411 find_data_references_in_bb (struct loop
*loop
, basic_block bb
,
4412 vec
<data_reference_p
> *datarefs
)
4414 gimple_stmt_iterator bsi
;
4416 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
4418 gimple stmt
= gsi_stmt (bsi
);
4420 if (!find_data_references_in_stmt (loop
, stmt
, datarefs
))
4422 struct data_reference
*res
;
4423 res
= XCNEW (struct data_reference
);
4424 datarefs
->safe_push (res
);
4426 return chrec_dont_know
;
4433 /* Search the data references in LOOP, and record the information into
4434 DATAREFS. Returns chrec_dont_know when failing to analyze a
4435 difficult case, returns NULL_TREE otherwise.
4437 TODO: This function should be made smarter so that it can handle address
4438 arithmetic as if they were array accesses, etc. */
4441 find_data_references_in_loop (struct loop
*loop
,
4442 vec
<data_reference_p
> *datarefs
)
4444 basic_block bb
, *bbs
;
4447 bbs
= get_loop_body_in_dom_order (loop
);
4449 for (i
= 0; i
< loop
->num_nodes
; i
++)
4453 if (find_data_references_in_bb (loop
, bb
, datarefs
) == chrec_dont_know
)
4456 return chrec_dont_know
;
4464 /* Recursive helper function. */
4467 find_loop_nest_1 (struct loop
*loop
, vec
<loop_p
> *loop_nest
)
4469 /* Inner loops of the nest should not contain siblings. Example:
4470 when there are two consecutive loops,
4481 the dependence relation cannot be captured by the distance
4486 loop_nest
->safe_push (loop
);
4488 return find_loop_nest_1 (loop
->inner
, loop_nest
);
4492 /* Return false when the LOOP is not well nested. Otherwise return
4493 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4494 contain the loops from the outermost to the innermost, as they will
4495 appear in the classic distance vector. */
4498 find_loop_nest (struct loop
*loop
, vec
<loop_p
> *loop_nest
)
4500 loop_nest
->safe_push (loop
);
4502 return find_loop_nest_1 (loop
->inner
, loop_nest
);
4506 /* Returns true when the data dependences have been computed, false otherwise.
4507 Given a loop nest LOOP, the following vectors are returned:
4508 DATAREFS is initialized to all the array elements contained in this loop,
4509 DEPENDENCE_RELATIONS contains the relations between the data references.
4510 Compute read-read and self relations if
4511 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4514 compute_data_dependences_for_loop (struct loop
*loop
,
4515 bool compute_self_and_read_read_dependences
,
4516 vec
<loop_p
> *loop_nest
,
4517 vec
<data_reference_p
> *datarefs
,
4518 vec
<ddr_p
> *dependence_relations
)
4522 memset (&dependence_stats
, 0, sizeof (dependence_stats
));
4524 /* If the loop nest is not well formed, or one of the data references
4525 is not computable, give up without spending time to compute other
4528 || !find_loop_nest (loop
, loop_nest
)
4529 || find_data_references_in_loop (loop
, datarefs
) == chrec_dont_know
4530 || !compute_all_dependences (*datarefs
, dependence_relations
, *loop_nest
,
4531 compute_self_and_read_read_dependences
))
4534 if (dump_file
&& (dump_flags
& TDF_STATS
))
4536 fprintf (dump_file
, "Dependence tester statistics:\n");
4538 fprintf (dump_file
, "Number of dependence tests: %d\n",
4539 dependence_stats
.num_dependence_tests
);
4540 fprintf (dump_file
, "Number of dependence tests classified dependent: %d\n",
4541 dependence_stats
.num_dependence_dependent
);
4542 fprintf (dump_file
, "Number of dependence tests classified independent: %d\n",
4543 dependence_stats
.num_dependence_independent
);
4544 fprintf (dump_file
, "Number of undetermined dependence tests: %d\n",
4545 dependence_stats
.num_dependence_undetermined
);
4547 fprintf (dump_file
, "Number of subscript tests: %d\n",
4548 dependence_stats
.num_subscript_tests
);
4549 fprintf (dump_file
, "Number of undetermined subscript tests: %d\n",
4550 dependence_stats
.num_subscript_undetermined
);
4551 fprintf (dump_file
, "Number of same subscript function: %d\n",
4552 dependence_stats
.num_same_subscript_function
);
4554 fprintf (dump_file
, "Number of ziv tests: %d\n",
4555 dependence_stats
.num_ziv
);
4556 fprintf (dump_file
, "Number of ziv tests returning dependent: %d\n",
4557 dependence_stats
.num_ziv_dependent
);
4558 fprintf (dump_file
, "Number of ziv tests returning independent: %d\n",
4559 dependence_stats
.num_ziv_independent
);
4560 fprintf (dump_file
, "Number of ziv tests unimplemented: %d\n",
4561 dependence_stats
.num_ziv_unimplemented
);
4563 fprintf (dump_file
, "Number of siv tests: %d\n",
4564 dependence_stats
.num_siv
);
4565 fprintf (dump_file
, "Number of siv tests returning dependent: %d\n",
4566 dependence_stats
.num_siv_dependent
);
4567 fprintf (dump_file
, "Number of siv tests returning independent: %d\n",
4568 dependence_stats
.num_siv_independent
);
4569 fprintf (dump_file
, "Number of siv tests unimplemented: %d\n",
4570 dependence_stats
.num_siv_unimplemented
);
4572 fprintf (dump_file
, "Number of miv tests: %d\n",
4573 dependence_stats
.num_miv
);
4574 fprintf (dump_file
, "Number of miv tests returning dependent: %d\n",
4575 dependence_stats
.num_miv_dependent
);
4576 fprintf (dump_file
, "Number of miv tests returning independent: %d\n",
4577 dependence_stats
.num_miv_independent
);
4578 fprintf (dump_file
, "Number of miv tests unimplemented: %d\n",
4579 dependence_stats
.num_miv_unimplemented
);
4585 /* Returns true when the data dependences for the basic block BB have been
4586 computed, false otherwise.
4587 DATAREFS is initialized to all the array elements contained in this basic
4588 block, DEPENDENCE_RELATIONS contains the relations between the data
4589 references. Compute read-read and self relations if
4590 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4592 compute_data_dependences_for_bb (basic_block bb
,
4593 bool compute_self_and_read_read_dependences
,
4594 vec
<data_reference_p
> *datarefs
,
4595 vec
<ddr_p
> *dependence_relations
)
4597 if (find_data_references_in_bb (NULL
, bb
, datarefs
) == chrec_dont_know
)
4600 return compute_all_dependences (*datarefs
, dependence_relations
, vNULL
,
4601 compute_self_and_read_read_dependences
);
4604 /* Entry point (for testing only). Analyze all the data references
4605 and the dependence relations in LOOP.
4607 The data references are computed first.
4609 A relation on these nodes is represented by a complete graph. Some
4610 of the relations could be of no interest, thus the relations can be
4613 In the following function we compute all the relations. This is
4614 just a first implementation that is here for:
4615 - for showing how to ask for the dependence relations,
4616 - for the debugging the whole dependence graph,
4617 - for the dejagnu testcases and maintenance.
4619 It is possible to ask only for a part of the graph, avoiding to
4620 compute the whole dependence graph. The computed dependences are
4621 stored in a knowledge base (KB) such that later queries don't
4622 recompute the same information. The implementation of this KB is
4623 transparent to the optimizer, and thus the KB can be changed with a
4624 more efficient implementation, or the KB could be disabled. */
4626 analyze_all_data_dependences (struct loop
*loop
)
4629 int nb_data_refs
= 10;
4630 vec
<data_reference_p
> datarefs
;
4631 datarefs
.create (nb_data_refs
);
4632 vec
<ddr_p
> dependence_relations
;
4633 dependence_relations
.create (nb_data_refs
* nb_data_refs
);
4634 vec
<loop_p
> loop_nest
;
4635 loop_nest
.create (3);
4637 /* Compute DDs on the whole function. */
4638 compute_data_dependences_for_loop (loop
, false, &loop_nest
, &datarefs
,
4639 &dependence_relations
);
4643 dump_data_dependence_relations (dump_file
, dependence_relations
);
4644 fprintf (dump_file
, "\n\n");
4646 if (dump_flags
& TDF_DETAILS
)
4647 dump_dist_dir_vectors (dump_file
, dependence_relations
);
4649 if (dump_flags
& TDF_STATS
)
4651 unsigned nb_top_relations
= 0;
4652 unsigned nb_bot_relations
= 0;
4653 unsigned nb_chrec_relations
= 0;
4654 struct data_dependence_relation
*ddr
;
4656 FOR_EACH_VEC_ELT (dependence_relations
, i
, ddr
)
4658 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr
)))
4661 else if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
4665 nb_chrec_relations
++;
4668 gather_stats_on_scev_database ();
4672 loop_nest
.release ();
4673 free_dependence_relations (dependence_relations
);
4674 free_data_refs (datarefs
);
4677 /* Computes all the data dependences and check that the results of
4678 several analyzers are the same. */
4681 tree_check_data_deps (void)
4684 struct loop
*loop_nest
;
4686 FOR_EACH_LOOP (li
, loop_nest
, 0)
4687 analyze_all_data_dependences (loop_nest
);
4690 /* Free the memory used by a data dependence relation DDR. */
4693 free_dependence_relation (struct data_dependence_relation
*ddr
)
4698 if (DDR_SUBSCRIPTS (ddr
).exists ())
4699 free_subscripts (DDR_SUBSCRIPTS (ddr
));
4700 DDR_DIST_VECTS (ddr
).release ();
4701 DDR_DIR_VECTS (ddr
).release ();
4706 /* Free the memory used by the data dependence relations from
4707 DEPENDENCE_RELATIONS. */
4710 free_dependence_relations (vec
<ddr_p
> dependence_relations
)
4713 struct data_dependence_relation
*ddr
;
4715 FOR_EACH_VEC_ELT (dependence_relations
, i
, ddr
)
4717 free_dependence_relation (ddr
);
4719 dependence_relations
.release ();
4722 /* Free the memory used by the data references from DATAREFS. */
4725 free_data_refs (vec
<data_reference_p
> datarefs
)
4728 struct data_reference
*dr
;
4730 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
4732 datarefs
.release ();
4737 /* Dump vertex I in RDG to FILE. */
4740 dump_rdg_vertex (FILE *file
, struct graph
*rdg
, int i
)
4742 struct vertex
*v
= &(rdg
->vertices
[i
]);
4743 struct graph_edge
*e
;
4745 fprintf (file
, "(vertex %d: (%s%s) (in:", i
,
4746 RDG_MEM_WRITE_STMT (rdg
, i
) ? "w" : "",
4747 RDG_MEM_READS_STMT (rdg
, i
) ? "r" : "");
4750 for (e
= v
->pred
; e
; e
= e
->pred_next
)
4751 fprintf (file
, " %d", e
->src
);
4753 fprintf (file
, ") (out:");
4756 for (e
= v
->succ
; e
; e
= e
->succ_next
)
4757 fprintf (file
, " %d", e
->dest
);
4759 fprintf (file
, ")\n");
4760 print_gimple_stmt (file
, RDGV_STMT (v
), 0, TDF_VOPS
|TDF_MEMSYMS
);
4761 fprintf (file
, ")\n");
4764 /* Call dump_rdg_vertex on stderr. */
4767 debug_rdg_vertex (struct graph
*rdg
, int i
)
4769 dump_rdg_vertex (stderr
, rdg
, i
);
4772 /* Dump component C of RDG to FILE. If DUMPED is non-null, set the
4773 dumped vertices to that bitmap. */
4776 dump_rdg_component (FILE *file
, struct graph
*rdg
, int c
, bitmap dumped
)
4780 fprintf (file
, "(%d\n", c
);
4782 for (i
= 0; i
< rdg
->n_vertices
; i
++)
4783 if (rdg
->vertices
[i
].component
== c
)
4786 bitmap_set_bit (dumped
, i
);
4788 dump_rdg_vertex (file
, rdg
, i
);
4791 fprintf (file
, ")\n");
4794 /* Call dump_rdg_vertex on stderr. */
4797 debug_rdg_component (struct graph
*rdg
, int c
)
4799 dump_rdg_component (stderr
, rdg
, c
, NULL
);
4802 /* Dump the reduced dependence graph RDG to FILE. */
4805 dump_rdg (FILE *file
, struct graph
*rdg
)
4808 bitmap dumped
= BITMAP_ALLOC (NULL
);
4810 fprintf (file
, "(rdg\n");
4812 for (i
= 0; i
< rdg
->n_vertices
; i
++)
4813 if (!bitmap_bit_p (dumped
, i
))
4814 dump_rdg_component (file
, rdg
, rdg
->vertices
[i
].component
, dumped
);
4816 fprintf (file
, ")\n");
4817 BITMAP_FREE (dumped
);
4820 /* Call dump_rdg on stderr. */
4823 debug_rdg (struct graph
*rdg
)
4825 dump_rdg (stderr
, rdg
);
4829 dot_rdg_1 (FILE *file
, struct graph
*rdg
)
4833 fprintf (file
, "digraph RDG {\n");
4835 for (i
= 0; i
< rdg
->n_vertices
; i
++)
4837 struct vertex
*v
= &(rdg
->vertices
[i
]);
4838 struct graph_edge
*e
;
4840 /* Highlight reads from memory. */
4841 if (RDG_MEM_READS_STMT (rdg
, i
))
4842 fprintf (file
, "%d [style=filled, fillcolor=green]\n", i
);
4844 /* Highlight stores to memory. */
4845 if (RDG_MEM_WRITE_STMT (rdg
, i
))
4846 fprintf (file
, "%d [style=filled, fillcolor=red]\n", i
);
4849 for (e
= v
->succ
; e
; e
= e
->succ_next
)
4850 switch (RDGE_TYPE (e
))
4853 fprintf (file
, "%d -> %d [label=input] \n", i
, e
->dest
);
4857 fprintf (file
, "%d -> %d [label=output] \n", i
, e
->dest
);
4861 /* These are the most common dependences: don't print these. */
4862 fprintf (file
, "%d -> %d \n", i
, e
->dest
);
4866 fprintf (file
, "%d -> %d [label=anti] \n", i
, e
->dest
);
4874 fprintf (file
, "}\n\n");
4877 /* Display the Reduced Dependence Graph using dotty. */
4878 extern void dot_rdg (struct graph
*);
4881 dot_rdg (struct graph
*rdg
)
4883 /* When debugging, enable the following code. This cannot be used
4884 in production compilers because it calls "system". */
4886 FILE *file
= fopen ("/tmp/rdg.dot", "w");
4887 gcc_assert (file
!= NULL
);
4889 dot_rdg_1 (file
, rdg
);
4892 system ("dotty /tmp/rdg.dot &");
4894 dot_rdg_1 (stderr
, rdg
);
4898 /* Returns the index of STMT in RDG. */
4901 rdg_vertex_for_stmt (struct graph
*rdg ATTRIBUTE_UNUSED
, gimple stmt
)
4903 int index
= gimple_uid (stmt
);
4904 gcc_checking_assert (index
== -1 || RDG_STMT (rdg
, index
) == stmt
);
4908 /* Creates an edge in RDG for each distance vector from DDR. The
4909 order that we keep track of in the RDG is the order in which
4910 statements have to be executed. */
4913 create_rdg_edge_for_ddr (struct graph
*rdg
, ddr_p ddr
)
4915 struct graph_edge
*e
;
4917 data_reference_p dra
= DDR_A (ddr
);
4918 data_reference_p drb
= DDR_B (ddr
);
4919 unsigned level
= ddr_dependence_level (ddr
);
4921 /* For non scalar dependences, when the dependence is REVERSED,
4922 statement B has to be executed before statement A. */
4924 && !DDR_REVERSED_P (ddr
))
4926 data_reference_p tmp
= dra
;
4931 va
= rdg_vertex_for_stmt (rdg
, DR_STMT (dra
));
4932 vb
= rdg_vertex_for_stmt (rdg
, DR_STMT (drb
));
4934 if (va
< 0 || vb
< 0)
4937 e
= add_edge (rdg
, va
, vb
);
4938 e
->data
= XNEW (struct rdg_edge
);
4940 RDGE_LEVEL (e
) = level
;
4941 RDGE_RELATION (e
) = ddr
;
4943 /* Determines the type of the data dependence. */
4944 if (DR_IS_READ (dra
) && DR_IS_READ (drb
))
4945 RDGE_TYPE (e
) = input_dd
;
4946 else if (DR_IS_WRITE (dra
) && DR_IS_WRITE (drb
))
4947 RDGE_TYPE (e
) = output_dd
;
4948 else if (DR_IS_WRITE (dra
) && DR_IS_READ (drb
))
4949 RDGE_TYPE (e
) = flow_dd
;
4950 else if (DR_IS_READ (dra
) && DR_IS_WRITE (drb
))
4951 RDGE_TYPE (e
) = anti_dd
;
4954 /* Creates dependence edges in RDG for all the uses of DEF. IDEF is
4955 the index of DEF in RDG. */
4958 create_rdg_edges_for_scalar (struct graph
*rdg
, tree def
, int idef
)
4960 use_operand_p imm_use_p
;
4961 imm_use_iterator iterator
;
4963 FOR_EACH_IMM_USE_FAST (imm_use_p
, iterator
, def
)
4965 struct graph_edge
*e
;
4966 int use
= rdg_vertex_for_stmt (rdg
, USE_STMT (imm_use_p
));
4971 e
= add_edge (rdg
, idef
, use
);
4972 e
->data
= XNEW (struct rdg_edge
);
4973 RDGE_TYPE (e
) = flow_dd
;
4974 RDGE_RELATION (e
) = NULL
;
4978 /* Creates the edges of the reduced dependence graph RDG. */
4981 create_rdg_edges (struct graph
*rdg
, vec
<ddr_p
> ddrs
)
4984 struct data_dependence_relation
*ddr
;
4985 def_operand_p def_p
;
4988 FOR_EACH_VEC_ELT (ddrs
, i
, ddr
)
4989 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
4990 create_rdg_edge_for_ddr (rdg
, ddr
);
4992 for (i
= 0; i
< rdg
->n_vertices
; i
++)
4993 FOR_EACH_PHI_OR_STMT_DEF (def_p
, RDG_STMT (rdg
, i
),
4995 create_rdg_edges_for_scalar (rdg
, DEF_FROM_PTR (def_p
), i
);
4998 /* Build the vertices of the reduced dependence graph RDG. */
5001 create_rdg_vertices (struct graph
*rdg
, vec
<gimple
> stmts
, loop_p loop
)
5006 FOR_EACH_VEC_ELT (stmts
, i
, stmt
)
5008 vec
<data_ref_loc
> references
;
5010 struct vertex
*v
= &(rdg
->vertices
[i
]);
5012 /* Record statement to vertex mapping. */
5013 gimple_set_uid (stmt
, i
);
5015 v
->data
= XNEW (struct rdg_vertex
);
5016 RDGV_STMT (v
) = stmt
;
5017 RDGV_DATAREFS (v
).create (0);
5018 RDGV_HAS_MEM_WRITE (v
) = false;
5019 RDGV_HAS_MEM_READS (v
) = false;
5020 if (gimple_code (stmt
) == GIMPLE_PHI
)
5023 get_references_in_stmt (stmt
, &references
);
5024 FOR_EACH_VEC_ELT (references
, j
, ref
)
5026 data_reference_p dr
;
5028 RDGV_HAS_MEM_WRITE (v
) = true;
5030 RDGV_HAS_MEM_READS (v
) = true;
5031 dr
= create_data_ref (loop
, loop_containing_stmt (stmt
),
5032 *ref
->pos
, stmt
, ref
->is_read
);
5034 RDGV_DATAREFS (v
).safe_push (dr
);
5036 references
.release ();
5040 /* Initialize STMTS with all the statements of LOOP. When
5041 INCLUDE_PHIS is true, include also the PHI nodes. The order in
5042 which we discover statements is important as
5043 generate_loops_for_partition is using the same traversal for
5044 identifying statements. */
5047 stmts_from_loop (struct loop
*loop
, vec
<gimple
> *stmts
)
5050 basic_block
*bbs
= get_loop_body_in_dom_order (loop
);
5052 for (i
= 0; i
< loop
->num_nodes
; i
++)
5054 basic_block bb
= bbs
[i
];
5055 gimple_stmt_iterator bsi
;
5058 for (bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
5059 stmts
->safe_push (gsi_stmt (bsi
));
5061 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
5063 stmt
= gsi_stmt (bsi
);
5064 if (gimple_code (stmt
) != GIMPLE_LABEL
&& !is_gimple_debug (stmt
))
5065 stmts
->safe_push (stmt
);
5072 /* Returns true when all the dependences are computable. */
5075 known_dependences_p (vec
<ddr_p
> dependence_relations
)
5080 FOR_EACH_VEC_ELT (dependence_relations
, i
, ddr
)
5081 if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
5087 /* Build the Reduced Dependence Graph (RDG) with one vertex per
5088 statement of the loop nest, and one edge per data dependence or
5089 scalar dependence. */
5092 build_empty_rdg (int n_stmts
)
5094 struct graph
*rdg
= new_graph (n_stmts
);
5098 /* Build the Reduced Dependence Graph (RDG) with one vertex per
5099 statement of the loop nest, and one edge per data dependence or
5100 scalar dependence. */
5103 build_rdg (struct loop
*loop
,
5104 vec
<loop_p
> *loop_nest
,
5105 vec
<ddr_p
> *dependence_relations
,
5106 vec
<data_reference_p
> *datarefs
)
5108 struct graph
*rdg
= NULL
;
5110 if (compute_data_dependences_for_loop (loop
, false, loop_nest
, datarefs
,
5111 dependence_relations
)
5112 && known_dependences_p (*dependence_relations
))
5116 stmts_from_loop (loop
, &stmts
);
5117 rdg
= build_empty_rdg (stmts
.length ());
5118 create_rdg_vertices (rdg
, stmts
, loop
);
5119 create_rdg_edges (rdg
, *dependence_relations
);
5126 /* Free the reduced dependence graph RDG. */
5129 free_rdg (struct graph
*rdg
)
5133 for (i
= 0; i
< rdg
->n_vertices
; i
++)
5135 struct vertex
*v
= &(rdg
->vertices
[i
]);
5136 struct graph_edge
*e
;
5138 for (e
= v
->succ
; e
; e
= e
->succ_next
)
5141 gimple_set_uid (RDGV_STMT (v
), -1);
5142 free_data_refs (RDGV_DATAREFS (v
));
5149 /* Determines whether the statement from vertex V of the RDG has a
5150 definition used outside the loop that contains this statement. */
5153 rdg_defs_used_in_other_loops_p (struct graph
*rdg
, int v
)
5155 gimple stmt
= RDG_STMT (rdg
, v
);
5156 struct loop
*loop
= loop_containing_stmt (stmt
);
5157 use_operand_p imm_use_p
;
5158 imm_use_iterator iterator
;
5160 def_operand_p def_p
;
5165 FOR_EACH_PHI_OR_STMT_DEF (def_p
, stmt
, it
, SSA_OP_DEF
)
5167 FOR_EACH_IMM_USE_FAST (imm_use_p
, iterator
, DEF_FROM_PTR (def_p
))
5169 if (loop_containing_stmt (USE_STMT (imm_use_p
)) != loop
)