1 /* Data references and dependences detectors.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This pass walks a given loop structure searching for array
23 references. The information about the array accesses is recorded
24 in DATA_REFERENCE structures.
26 The basic test for determining the dependences is:
27 given two access functions chrec1 and chrec2 to a same array, and
28 x and y two vectors from the iteration domain, the same element of
29 the array is accessed twice at iterations x and y if and only if:
30 | chrec1 (x) == chrec2 (y).
32 The goals of this analysis are:
34 - to determine the independence: the relation between two
35 independent accesses is qualified with the chrec_known (this
36 information allows a loop parallelization),
38 - when two data references access the same data, to qualify the
39 dependence relation with classic dependence representations:
43 - loop carried level dependence
44 - polyhedron dependence
45 or with the chains of recurrences based representation,
47 - to define a knowledge base for storing the data dependence
50 - to define an interface to access this data.
55 - subscript: given two array accesses a subscript is the tuple
56 composed of the access functions for a given dimension. Example:
57 Given A[f1][f2][f3] and B[g1][g2][g3], there are three subscripts:
58 (f1, g1), (f2, g2), (f3, g3).
60 - Diophantine equation: an equation whose coefficients and
61 solutions are integer constants, for example the equation
63 has an integer solution x = 1 and y = -1.
67 - "Advanced Compilation for High Performance Computing" by Randy
68 Allen and Ken Kennedy.
69 http://citeseer.ist.psu.edu/goff91practical.html
71 - "Loop Transformations for Restructuring Compilers - The Foundations"
79 #include "coretypes.h"
80 #include "gimple-pretty-print.h"
81 #include "tree-flow.h"
83 #include "tree-data-ref.h"
84 #include "tree-scalar-evolution.h"
86 #include "langhooks.h"
87 #include "tree-affine.h"
90 static struct datadep_stats
92 int num_dependence_tests
;
93 int num_dependence_dependent
;
94 int num_dependence_independent
;
95 int num_dependence_undetermined
;
97 int num_subscript_tests
;
98 int num_subscript_undetermined
;
99 int num_same_subscript_function
;
102 int num_ziv_independent
;
103 int num_ziv_dependent
;
104 int num_ziv_unimplemented
;
107 int num_siv_independent
;
108 int num_siv_dependent
;
109 int num_siv_unimplemented
;
112 int num_miv_independent
;
113 int num_miv_dependent
;
114 int num_miv_unimplemented
;
117 static bool subscript_dependence_tester_1 (struct data_dependence_relation
*,
118 struct data_reference
*,
119 struct data_reference
*,
121 /* Returns true iff A divides B. */
124 tree_fold_divides_p (const_tree a
, const_tree b
)
126 gcc_assert (TREE_CODE (a
) == INTEGER_CST
);
127 gcc_assert (TREE_CODE (b
) == INTEGER_CST
);
128 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR
, b
, a
));
131 /* Returns true iff A divides B. */
134 int_divides_p (int a
, int b
)
136 return ((b
% a
) == 0);
141 /* Dump into FILE all the data references from DATAREFS. */
144 dump_data_references (FILE *file
, VEC (data_reference_p
, heap
) *datarefs
)
147 struct data_reference
*dr
;
149 FOR_EACH_VEC_ELT (data_reference_p
, datarefs
, i
, dr
)
150 dump_data_reference (file
, dr
);
153 /* Dump into STDERR all the data references from DATAREFS. */
156 debug_data_references (VEC (data_reference_p
, heap
) *datarefs
)
158 dump_data_references (stderr
, datarefs
);
161 /* Print to STDERR the data_reference DR. */
164 debug_data_reference (struct data_reference
*dr
)
166 dump_data_reference (stderr
, dr
);
169 /* Dump function for a DATA_REFERENCE structure. */
172 dump_data_reference (FILE *outf
,
173 struct data_reference
*dr
)
177 fprintf (outf
, "#(Data Ref: \n");
178 fprintf (outf
, "# bb: %d \n", gimple_bb (DR_STMT (dr
))->index
);
179 fprintf (outf
, "# stmt: ");
180 print_gimple_stmt (outf
, DR_STMT (dr
), 0, 0);
181 fprintf (outf
, "# ref: ");
182 print_generic_stmt (outf
, DR_REF (dr
), 0);
183 fprintf (outf
, "# base_object: ");
184 print_generic_stmt (outf
, DR_BASE_OBJECT (dr
), 0);
186 for (i
= 0; i
< DR_NUM_DIMENSIONS (dr
); i
++)
188 fprintf (outf
, "# Access function %d: ", i
);
189 print_generic_stmt (outf
, DR_ACCESS_FN (dr
, i
), 0);
191 fprintf (outf
, "#)\n");
194 /* Dumps the affine function described by FN to the file OUTF. */
197 dump_affine_function (FILE *outf
, affine_fn fn
)
202 print_generic_expr (outf
, VEC_index (tree
, fn
, 0), TDF_SLIM
);
203 for (i
= 1; VEC_iterate (tree
, fn
, i
, coef
); i
++)
205 fprintf (outf
, " + ");
206 print_generic_expr (outf
, coef
, TDF_SLIM
);
207 fprintf (outf
, " * x_%u", i
);
211 /* Dumps the conflict function CF to the file OUTF. */
214 dump_conflict_function (FILE *outf
, conflict_function
*cf
)
218 if (cf
->n
== NO_DEPENDENCE
)
219 fprintf (outf
, "no dependence\n");
220 else if (cf
->n
== NOT_KNOWN
)
221 fprintf (outf
, "not known\n");
224 for (i
= 0; i
< cf
->n
; i
++)
227 dump_affine_function (outf
, cf
->fns
[i
]);
228 fprintf (outf
, "]\n");
233 /* Dump function for a SUBSCRIPT structure. */
236 dump_subscript (FILE *outf
, struct subscript
*subscript
)
238 conflict_function
*cf
= SUB_CONFLICTS_IN_A (subscript
);
240 fprintf (outf
, "\n (subscript \n");
241 fprintf (outf
, " iterations_that_access_an_element_twice_in_A: ");
242 dump_conflict_function (outf
, cf
);
243 if (CF_NONTRIVIAL_P (cf
))
245 tree last_iteration
= SUB_LAST_CONFLICT (subscript
);
246 fprintf (outf
, " last_conflict: ");
247 print_generic_stmt (outf
, last_iteration
, 0);
250 cf
= SUB_CONFLICTS_IN_B (subscript
);
251 fprintf (outf
, " iterations_that_access_an_element_twice_in_B: ");
252 dump_conflict_function (outf
, cf
);
253 if (CF_NONTRIVIAL_P (cf
))
255 tree last_iteration
= SUB_LAST_CONFLICT (subscript
);
256 fprintf (outf
, " last_conflict: ");
257 print_generic_stmt (outf
, last_iteration
, 0);
260 fprintf (outf
, " (Subscript distance: ");
261 print_generic_stmt (outf
, SUB_DISTANCE (subscript
), 0);
262 fprintf (outf
, " )\n");
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
, heap
) *dir_vects
,
320 FOR_EACH_VEC_ELT (lambda_vector
, 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
, heap
) *dist_vects
,
345 FOR_EACH_VEC_ELT (lambda_vector
, 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 (loop_p
, 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
,
436 VEC (ddr_p
, heap
) *ddrs
)
439 struct data_dependence_relation
*ddr
;
441 FOR_EACH_VEC_ELT (ddr_p
, 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
, heap
) *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
, heap
) *ddrs
)
462 struct data_dependence_relation
*ddr
;
465 FOR_EACH_VEC_ELT (ddr_p
, ddrs
, i
, ddr
)
466 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
&& DDR_AFFINE_P (ddr
))
468 FOR_EACH_VEC_ELT (lambda_vector
, 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 (lambda_vector
, 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
, heap
) *ddrs
)
492 struct data_dependence_relation
*ddr
;
494 FOR_EACH_VEC_ELT (ddr_p
, ddrs
, i
, ddr
)
495 dump_data_dependence_relation (file
, ddr
);
497 fprintf (file
, "\n\n");
501 debug_ddrs (VEC (ddr_p
, heap
) *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
, heap
) *access_fns
= NULL
;
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
) = NULL
;
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 VEC_safe_push (tree
, heap
, access_fns
, integer_zero_node
);
848 else if (TREE_CODE (ref
) == IMAGPART_EXPR
)
850 ref
= TREE_OPERAND (ref
, 0);
851 VEC_safe_push (tree
, heap
, access_fns
, 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 VEC_safe_push (tree
, heap
, access_fns
, 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 VEC_safe_push (tree
, heap
, access_fns
, 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 VEC_safe_push (tree
, heap
, access_fns
, 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 VEC_free (tree
, heap
, DR_ACCESS_FNS (dr
));
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
= VEC_length (tree
, fna
);
1067 if (n
!= VEC_length (tree
, fnb
))
1070 for (i
= 0; i
< n
; i
++)
1071 if (!operand_equal_p (VEC_index (tree
, fna
, i
),
1072 VEC_index (tree
, fnb
, i
), 0))
1078 /* If all the functions in CF are the same, returns one of them,
1079 otherwise returns NULL. */
1082 common_affine_function (conflict_function
*cf
)
1087 if (!CF_NONTRIVIAL_P (cf
))
1092 for (i
= 1; i
< cf
->n
; i
++)
1093 if (!affine_function_equal_p (comm
, cf
->fns
[i
]))
1099 /* Returns the base of the affine function FN. */
1102 affine_function_base (affine_fn fn
)
1104 return VEC_index (tree
, fn
, 0);
1107 /* Returns true if FN is a constant. */
1110 affine_function_constant_p (affine_fn fn
)
1115 for (i
= 1; VEC_iterate (tree
, fn
, i
, coef
); i
++)
1116 if (!integer_zerop (coef
))
1122 /* Returns true if FN is the zero constant function. */
1125 affine_function_zero_p (affine_fn fn
)
1127 return (integer_zerop (affine_function_base (fn
))
1128 && affine_function_constant_p (fn
));
1131 /* Returns a signed integer type with the largest precision from TA
1135 signed_type_for_types (tree ta
, tree tb
)
1137 if (TYPE_PRECISION (ta
) > TYPE_PRECISION (tb
))
1138 return signed_type_for (ta
);
1140 return signed_type_for (tb
);
1143 /* Applies operation OP on affine functions FNA and FNB, and returns the
1147 affine_fn_op (enum tree_code op
, affine_fn fna
, affine_fn fnb
)
1153 if (VEC_length (tree
, fnb
) > VEC_length (tree
, fna
))
1155 n
= VEC_length (tree
, fna
);
1156 m
= VEC_length (tree
, fnb
);
1160 n
= VEC_length (tree
, fnb
);
1161 m
= VEC_length (tree
, fna
);
1164 ret
= VEC_alloc (tree
, heap
, m
);
1165 for (i
= 0; i
< n
; i
++)
1167 tree type
= signed_type_for_types (TREE_TYPE (VEC_index (tree
, fna
, i
)),
1168 TREE_TYPE (VEC_index (tree
, fnb
, i
)));
1170 VEC_quick_push (tree
, ret
,
1171 fold_build2 (op
, type
,
1172 VEC_index (tree
, fna
, i
),
1173 VEC_index (tree
, fnb
, i
)));
1176 for (; VEC_iterate (tree
, fna
, i
, coef
); i
++)
1177 VEC_quick_push (tree
, ret
,
1178 fold_build2 (op
, signed_type_for (TREE_TYPE (coef
)),
1179 coef
, integer_zero_node
));
1180 for (; VEC_iterate (tree
, fnb
, i
, coef
); i
++)
1181 VEC_quick_push (tree
, ret
,
1182 fold_build2 (op
, signed_type_for (TREE_TYPE (coef
)),
1183 integer_zero_node
, coef
));
1188 /* Returns the sum of affine functions FNA and FNB. */
1191 affine_fn_plus (affine_fn fna
, affine_fn fnb
)
1193 return affine_fn_op (PLUS_EXPR
, fna
, fnb
);
1196 /* Returns the difference of affine functions FNA and FNB. */
1199 affine_fn_minus (affine_fn fna
, affine_fn fnb
)
1201 return affine_fn_op (MINUS_EXPR
, fna
, fnb
);
1204 /* Frees affine function FN. */
1207 affine_fn_free (affine_fn fn
)
1209 VEC_free (tree
, heap
, fn
);
1212 /* Determine for each subscript in the data dependence relation DDR
1216 compute_subscript_distance (struct data_dependence_relation
*ddr
)
1218 conflict_function
*cf_a
, *cf_b
;
1219 affine_fn fn_a
, fn_b
, diff
;
1221 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
1225 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
1227 struct subscript
*subscript
;
1229 subscript
= DDR_SUBSCRIPT (ddr
, i
);
1230 cf_a
= SUB_CONFLICTS_IN_A (subscript
);
1231 cf_b
= SUB_CONFLICTS_IN_B (subscript
);
1233 fn_a
= common_affine_function (cf_a
);
1234 fn_b
= common_affine_function (cf_b
);
1237 SUB_DISTANCE (subscript
) = chrec_dont_know
;
1240 diff
= affine_fn_minus (fn_a
, fn_b
);
1242 if (affine_function_constant_p (diff
))
1243 SUB_DISTANCE (subscript
) = affine_function_base (diff
);
1245 SUB_DISTANCE (subscript
) = chrec_dont_know
;
1247 affine_fn_free (diff
);
1252 /* Returns the conflict function for "unknown". */
1254 static conflict_function
*
1255 conflict_fn_not_known (void)
1257 conflict_function
*fn
= XCNEW (conflict_function
);
1263 /* Returns the conflict function for "independent". */
1265 static conflict_function
*
1266 conflict_fn_no_dependence (void)
1268 conflict_function
*fn
= XCNEW (conflict_function
);
1269 fn
->n
= NO_DEPENDENCE
;
1274 /* Returns true if the address of OBJ is invariant in LOOP. */
1277 object_address_invariant_in_loop_p (const struct loop
*loop
, const_tree obj
)
1279 while (handled_component_p (obj
))
1281 if (TREE_CODE (obj
) == ARRAY_REF
)
1283 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1284 need to check the stride and the lower bound of the reference. */
1285 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj
, 2),
1287 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj
, 3),
1291 else if (TREE_CODE (obj
) == COMPONENT_REF
)
1293 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj
, 2),
1297 obj
= TREE_OPERAND (obj
, 0);
1300 if (!INDIRECT_REF_P (obj
)
1301 && TREE_CODE (obj
) != MEM_REF
)
1304 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj
, 0),
1308 /* Returns false if we can prove that data references A and B do not alias,
1309 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
1313 dr_may_alias_p (const struct data_reference
*a
, const struct data_reference
*b
,
1316 tree addr_a
= DR_BASE_OBJECT (a
);
1317 tree addr_b
= DR_BASE_OBJECT (b
);
1319 /* If we are not processing a loop nest but scalar code we
1320 do not need to care about possible cross-iteration dependences
1321 and thus can process the full original reference. Do so,
1322 similar to how loop invariant motion applies extra offset-based
1326 aff_tree off1
, off2
;
1327 double_int size1
, size2
;
1328 get_inner_reference_aff (DR_REF (a
), &off1
, &size1
);
1329 get_inner_reference_aff (DR_REF (b
), &off2
, &size2
);
1330 aff_combination_scale (&off1
, double_int_minus_one
);
1331 aff_combination_add (&off2
, &off1
);
1332 if (aff_comb_cannot_overlap_p (&off2
, size1
, size2
))
1336 /* If we had an evolution in a MEM_REF BASE_OBJECT we do not know
1337 the size of the base-object. So we cannot do any offset/overlap
1338 based analysis but have to rely on points-to information only. */
1339 if (TREE_CODE (addr_a
) == MEM_REF
1340 && DR_UNCONSTRAINED_BASE (a
))
1342 if (TREE_CODE (addr_b
) == MEM_REF
1343 && DR_UNCONSTRAINED_BASE (b
))
1344 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a
, 0),
1345 TREE_OPERAND (addr_b
, 0));
1347 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a
, 0),
1348 build_fold_addr_expr (addr_b
));
1350 else if (TREE_CODE (addr_b
) == MEM_REF
1351 && DR_UNCONSTRAINED_BASE (b
))
1352 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a
),
1353 TREE_OPERAND (addr_b
, 0));
1355 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1356 that is being subsetted in the loop nest. */
1357 if (DR_IS_WRITE (a
) && DR_IS_WRITE (b
))
1358 return refs_output_dependent_p (addr_a
, addr_b
);
1359 else if (DR_IS_READ (a
) && DR_IS_WRITE (b
))
1360 return refs_anti_dependent_p (addr_a
, addr_b
);
1361 return refs_may_alias_p (addr_a
, addr_b
);
1364 /* Initialize a data dependence relation between data accesses A and
1365 B. NB_LOOPS is the number of loops surrounding the references: the
1366 size of the classic distance/direction vectors. */
1368 struct data_dependence_relation
*
1369 initialize_data_dependence_relation (struct data_reference
*a
,
1370 struct data_reference
*b
,
1371 VEC (loop_p
, heap
) *loop_nest
)
1373 struct data_dependence_relation
*res
;
1376 res
= XNEW (struct data_dependence_relation
);
1379 DDR_LOOP_NEST (res
) = NULL
;
1380 DDR_REVERSED_P (res
) = false;
1381 DDR_SUBSCRIPTS (res
) = NULL
;
1382 DDR_DIR_VECTS (res
) = NULL
;
1383 DDR_DIST_VECTS (res
) = NULL
;
1385 if (a
== NULL
|| b
== NULL
)
1387 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
1391 /* If the data references do not alias, then they are independent. */
1392 if (!dr_may_alias_p (a
, b
, loop_nest
!= NULL
))
1394 DDR_ARE_DEPENDENT (res
) = chrec_known
;
1398 /* The case where the references are exactly the same. */
1399 if (operand_equal_p (DR_REF (a
), DR_REF (b
), 0))
1402 && !object_address_invariant_in_loop_p (VEC_index (loop_p
, loop_nest
, 0),
1403 DR_BASE_OBJECT (a
)))
1405 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
1408 DDR_AFFINE_P (res
) = true;
1409 DDR_ARE_DEPENDENT (res
) = NULL_TREE
;
1410 DDR_SUBSCRIPTS (res
) = VEC_alloc (subscript_p
, heap
, DR_NUM_DIMENSIONS (a
));
1411 DDR_LOOP_NEST (res
) = loop_nest
;
1412 DDR_INNER_LOOP (res
) = 0;
1413 DDR_SELF_REFERENCE (res
) = true;
1414 for (i
= 0; i
< DR_NUM_DIMENSIONS (a
); i
++)
1416 struct subscript
*subscript
;
1418 subscript
= XNEW (struct subscript
);
1419 SUB_CONFLICTS_IN_A (subscript
) = conflict_fn_not_known ();
1420 SUB_CONFLICTS_IN_B (subscript
) = conflict_fn_not_known ();
1421 SUB_LAST_CONFLICT (subscript
) = chrec_dont_know
;
1422 SUB_DISTANCE (subscript
) = chrec_dont_know
;
1423 VEC_safe_push (subscript_p
, heap
, DDR_SUBSCRIPTS (res
), subscript
);
1428 /* If the references do not access the same object, we do not know
1429 whether they alias or not. */
1430 if (!operand_equal_p (DR_BASE_OBJECT (a
), DR_BASE_OBJECT (b
), 0))
1432 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
1436 /* If the base of the object is not invariant in the loop nest, we cannot
1437 analyze it. TODO -- in fact, it would suffice to record that there may
1438 be arbitrary dependences in the loops where the base object varies. */
1440 && !object_address_invariant_in_loop_p (VEC_index (loop_p
, loop_nest
, 0),
1441 DR_BASE_OBJECT (a
)))
1443 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
1447 /* If the number of dimensions of the access to not agree we can have
1448 a pointer access to a component of the array element type and an
1449 array access while the base-objects are still the same. Punt. */
1450 if (DR_NUM_DIMENSIONS (a
) != DR_NUM_DIMENSIONS (b
))
1452 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
1456 DDR_AFFINE_P (res
) = true;
1457 DDR_ARE_DEPENDENT (res
) = NULL_TREE
;
1458 DDR_SUBSCRIPTS (res
) = VEC_alloc (subscript_p
, heap
, DR_NUM_DIMENSIONS (a
));
1459 DDR_LOOP_NEST (res
) = loop_nest
;
1460 DDR_INNER_LOOP (res
) = 0;
1461 DDR_SELF_REFERENCE (res
) = false;
1463 for (i
= 0; i
< DR_NUM_DIMENSIONS (a
); i
++)
1465 struct subscript
*subscript
;
1467 subscript
= XNEW (struct subscript
);
1468 SUB_CONFLICTS_IN_A (subscript
) = conflict_fn_not_known ();
1469 SUB_CONFLICTS_IN_B (subscript
) = conflict_fn_not_known ();
1470 SUB_LAST_CONFLICT (subscript
) = chrec_dont_know
;
1471 SUB_DISTANCE (subscript
) = chrec_dont_know
;
1472 VEC_safe_push (subscript_p
, heap
, DDR_SUBSCRIPTS (res
), subscript
);
1478 /* Frees memory used by the conflict function F. */
1481 free_conflict_function (conflict_function
*f
)
1485 if (CF_NONTRIVIAL_P (f
))
1487 for (i
= 0; i
< f
->n
; i
++)
1488 affine_fn_free (f
->fns
[i
]);
1493 /* Frees memory used by SUBSCRIPTS. */
1496 free_subscripts (VEC (subscript_p
, heap
) *subscripts
)
1501 FOR_EACH_VEC_ELT (subscript_p
, subscripts
, i
, s
)
1503 free_conflict_function (s
->conflicting_iterations_in_a
);
1504 free_conflict_function (s
->conflicting_iterations_in_b
);
1507 VEC_free (subscript_p
, heap
, subscripts
);
1510 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1514 finalize_ddr_dependent (struct data_dependence_relation
*ddr
,
1517 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1519 fprintf (dump_file
, "(dependence classified: ");
1520 print_generic_expr (dump_file
, chrec
, 0);
1521 fprintf (dump_file
, ")\n");
1524 DDR_ARE_DEPENDENT (ddr
) = chrec
;
1525 free_subscripts (DDR_SUBSCRIPTS (ddr
));
1526 DDR_SUBSCRIPTS (ddr
) = NULL
;
1529 /* The dependence relation DDR cannot be represented by a distance
1533 non_affine_dependence_relation (struct data_dependence_relation
*ddr
)
1535 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1536 fprintf (dump_file
, "(Dependence relation cannot be represented by distance vector.) \n");
1538 DDR_AFFINE_P (ddr
) = false;
1543 /* This section contains the classic Banerjee tests. */
1545 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1546 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1549 ziv_subscript_p (const_tree chrec_a
, const_tree chrec_b
)
1551 return (evolution_function_is_constant_p (chrec_a
)
1552 && evolution_function_is_constant_p (chrec_b
));
1555 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
1556 variable, i.e., if the SIV (Single Index Variable) test is true. */
1559 siv_subscript_p (const_tree chrec_a
, const_tree chrec_b
)
1561 if ((evolution_function_is_constant_p (chrec_a
)
1562 && evolution_function_is_univariate_p (chrec_b
))
1563 || (evolution_function_is_constant_p (chrec_b
)
1564 && evolution_function_is_univariate_p (chrec_a
)))
1567 if (evolution_function_is_univariate_p (chrec_a
)
1568 && evolution_function_is_univariate_p (chrec_b
))
1570 switch (TREE_CODE (chrec_a
))
1572 case POLYNOMIAL_CHREC
:
1573 switch (TREE_CODE (chrec_b
))
1575 case POLYNOMIAL_CHREC
:
1576 if (CHREC_VARIABLE (chrec_a
) != CHREC_VARIABLE (chrec_b
))
1591 /* Creates a conflict function with N dimensions. The affine functions
1592 in each dimension follow. */
1594 static conflict_function
*
1595 conflict_fn (unsigned n
, ...)
1598 conflict_function
*ret
= XCNEW (conflict_function
);
1601 gcc_assert (0 < n
&& n
<= MAX_DIM
);
1605 for (i
= 0; i
< n
; i
++)
1606 ret
->fns
[i
] = va_arg (ap
, affine_fn
);
1612 /* Returns constant affine function with value CST. */
1615 affine_fn_cst (tree cst
)
1617 affine_fn fn
= VEC_alloc (tree
, heap
, 1);
1618 VEC_quick_push (tree
, fn
, cst
);
1622 /* Returns affine function with single variable, CST + COEF * x_DIM. */
1625 affine_fn_univar (tree cst
, unsigned dim
, tree coef
)
1627 affine_fn fn
= VEC_alloc (tree
, heap
, dim
+ 1);
1630 gcc_assert (dim
> 0);
1631 VEC_quick_push (tree
, fn
, cst
);
1632 for (i
= 1; i
< dim
; i
++)
1633 VEC_quick_push (tree
, fn
, integer_zero_node
);
1634 VEC_quick_push (tree
, fn
, coef
);
1638 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1639 *OVERLAPS_B are initialized to the functions that describe the
1640 relation between the elements accessed twice by CHREC_A and
1641 CHREC_B. For k >= 0, the following property is verified:
1643 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1646 analyze_ziv_subscript (tree chrec_a
,
1648 conflict_function
**overlaps_a
,
1649 conflict_function
**overlaps_b
,
1650 tree
*last_conflicts
)
1652 tree type
, difference
;
1653 dependence_stats
.num_ziv
++;
1655 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1656 fprintf (dump_file
, "(analyze_ziv_subscript \n");
1658 type
= signed_type_for_types (TREE_TYPE (chrec_a
), TREE_TYPE (chrec_b
));
1659 chrec_a
= chrec_convert (type
, chrec_a
, NULL
);
1660 chrec_b
= chrec_convert (type
, chrec_b
, NULL
);
1661 difference
= chrec_fold_minus (type
, chrec_a
, chrec_b
);
1663 switch (TREE_CODE (difference
))
1666 if (integer_zerop (difference
))
1668 /* The difference is equal to zero: the accessed index
1669 overlaps for each iteration in the loop. */
1670 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1671 *overlaps_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1672 *last_conflicts
= chrec_dont_know
;
1673 dependence_stats
.num_ziv_dependent
++;
1677 /* The accesses do not overlap. */
1678 *overlaps_a
= conflict_fn_no_dependence ();
1679 *overlaps_b
= conflict_fn_no_dependence ();
1680 *last_conflicts
= integer_zero_node
;
1681 dependence_stats
.num_ziv_independent
++;
1686 /* We're not sure whether the indexes overlap. For the moment,
1687 conservatively answer "don't know". */
1688 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1689 fprintf (dump_file
, "ziv test failed: difference is non-integer.\n");
1691 *overlaps_a
= conflict_fn_not_known ();
1692 *overlaps_b
= conflict_fn_not_known ();
1693 *last_conflicts
= chrec_dont_know
;
1694 dependence_stats
.num_ziv_unimplemented
++;
1698 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1699 fprintf (dump_file
, ")\n");
1702 /* Similar to max_stmt_executions_int, but returns the bound as a tree,
1703 and only if it fits to the int type. If this is not the case, or the
1704 bound on the number of iterations of LOOP could not be derived, returns
1708 max_stmt_executions_tree (struct loop
*loop
)
1712 if (!max_stmt_executions (loop
, &nit
))
1713 return chrec_dont_know
;
1715 if (!double_int_fits_to_tree_p (unsigned_type_node
, nit
))
1716 return chrec_dont_know
;
1718 return double_int_to_tree (unsigned_type_node
, nit
);
1721 /* Determine whether the CHREC is always positive/negative. If the expression
1722 cannot be statically analyzed, return false, otherwise set the answer into
1726 chrec_is_positive (tree chrec
, bool *value
)
1728 bool value0
, value1
, value2
;
1729 tree end_value
, nb_iter
;
1731 switch (TREE_CODE (chrec
))
1733 case POLYNOMIAL_CHREC
:
1734 if (!chrec_is_positive (CHREC_LEFT (chrec
), &value0
)
1735 || !chrec_is_positive (CHREC_RIGHT (chrec
), &value1
))
1738 /* FIXME -- overflows. */
1739 if (value0
== value1
)
1745 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1746 and the proof consists in showing that the sign never
1747 changes during the execution of the loop, from 0 to
1748 loop->nb_iterations. */
1749 if (!evolution_function_is_affine_p (chrec
))
1752 nb_iter
= number_of_latch_executions (get_chrec_loop (chrec
));
1753 if (chrec_contains_undetermined (nb_iter
))
1757 /* TODO -- If the test is after the exit, we may decrease the number of
1758 iterations by one. */
1760 nb_iter
= chrec_fold_minus (type
, nb_iter
, build_int_cst (type
, 1));
1763 end_value
= chrec_apply (CHREC_VARIABLE (chrec
), chrec
, nb_iter
);
1765 if (!chrec_is_positive (end_value
, &value2
))
1769 return value0
== value1
;
1772 switch (tree_int_cst_sgn (chrec
))
1791 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1792 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1793 *OVERLAPS_B are initialized to the functions that describe the
1794 relation between the elements accessed twice by CHREC_A and
1795 CHREC_B. For k >= 0, the following property is verified:
1797 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1800 analyze_siv_subscript_cst_affine (tree chrec_a
,
1802 conflict_function
**overlaps_a
,
1803 conflict_function
**overlaps_b
,
1804 tree
*last_conflicts
)
1806 bool value0
, value1
, value2
;
1807 tree type
, difference
, tmp
;
1809 type
= signed_type_for_types (TREE_TYPE (chrec_a
), TREE_TYPE (chrec_b
));
1810 chrec_a
= chrec_convert (type
, chrec_a
, NULL
);
1811 chrec_b
= chrec_convert (type
, chrec_b
, NULL
);
1812 difference
= chrec_fold_minus (type
, initial_condition (chrec_b
), chrec_a
);
1814 /* Special case overlap in the first iteration. */
1815 if (integer_zerop (difference
))
1817 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1818 *overlaps_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1819 *last_conflicts
= integer_one_node
;
1823 if (!chrec_is_positive (initial_condition (difference
), &value0
))
1825 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1826 fprintf (dump_file
, "siv test failed: chrec is not positive.\n");
1828 dependence_stats
.num_siv_unimplemented
++;
1829 *overlaps_a
= conflict_fn_not_known ();
1830 *overlaps_b
= conflict_fn_not_known ();
1831 *last_conflicts
= chrec_dont_know
;
1836 if (value0
== false)
1838 if (!chrec_is_positive (CHREC_RIGHT (chrec_b
), &value1
))
1840 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1841 fprintf (dump_file
, "siv test failed: chrec not positive.\n");
1843 *overlaps_a
= conflict_fn_not_known ();
1844 *overlaps_b
= conflict_fn_not_known ();
1845 *last_conflicts
= chrec_dont_know
;
1846 dependence_stats
.num_siv_unimplemented
++;
1855 chrec_b = {10, +, 1}
1858 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b
), difference
))
1860 HOST_WIDE_INT numiter
;
1861 struct loop
*loop
= get_chrec_loop (chrec_b
);
1863 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1864 tmp
= fold_build2 (EXACT_DIV_EXPR
, type
,
1865 fold_build1 (ABS_EXPR
, type
, difference
),
1866 CHREC_RIGHT (chrec_b
));
1867 *overlaps_b
= conflict_fn (1, affine_fn_cst (tmp
));
1868 *last_conflicts
= integer_one_node
;
1871 /* Perform weak-zero siv test to see if overlap is
1872 outside the loop bounds. */
1873 numiter
= max_stmt_executions_int (loop
);
1876 && compare_tree_int (tmp
, numiter
) > 0)
1878 free_conflict_function (*overlaps_a
);
1879 free_conflict_function (*overlaps_b
);
1880 *overlaps_a
= conflict_fn_no_dependence ();
1881 *overlaps_b
= conflict_fn_no_dependence ();
1882 *last_conflicts
= integer_zero_node
;
1883 dependence_stats
.num_siv_independent
++;
1886 dependence_stats
.num_siv_dependent
++;
1890 /* When the step does not divide the difference, there are
1894 *overlaps_a
= conflict_fn_no_dependence ();
1895 *overlaps_b
= conflict_fn_no_dependence ();
1896 *last_conflicts
= integer_zero_node
;
1897 dependence_stats
.num_siv_independent
++;
1906 chrec_b = {10, +, -1}
1908 In this case, chrec_a will not overlap with chrec_b. */
1909 *overlaps_a
= conflict_fn_no_dependence ();
1910 *overlaps_b
= conflict_fn_no_dependence ();
1911 *last_conflicts
= integer_zero_node
;
1912 dependence_stats
.num_siv_independent
++;
1919 if (!chrec_is_positive (CHREC_RIGHT (chrec_b
), &value2
))
1921 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1922 fprintf (dump_file
, "siv test failed: chrec not positive.\n");
1924 *overlaps_a
= conflict_fn_not_known ();
1925 *overlaps_b
= conflict_fn_not_known ();
1926 *last_conflicts
= chrec_dont_know
;
1927 dependence_stats
.num_siv_unimplemented
++;
1932 if (value2
== false)
1936 chrec_b = {10, +, -1}
1938 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b
), difference
))
1940 HOST_WIDE_INT numiter
;
1941 struct loop
*loop
= get_chrec_loop (chrec_b
);
1943 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
1944 tmp
= fold_build2 (EXACT_DIV_EXPR
, type
, difference
,
1945 CHREC_RIGHT (chrec_b
));
1946 *overlaps_b
= conflict_fn (1, affine_fn_cst (tmp
));
1947 *last_conflicts
= integer_one_node
;
1949 /* Perform weak-zero siv test to see if overlap is
1950 outside the loop bounds. */
1951 numiter
= max_stmt_executions_int (loop
);
1954 && compare_tree_int (tmp
, numiter
) > 0)
1956 free_conflict_function (*overlaps_a
);
1957 free_conflict_function (*overlaps_b
);
1958 *overlaps_a
= conflict_fn_no_dependence ();
1959 *overlaps_b
= conflict_fn_no_dependence ();
1960 *last_conflicts
= integer_zero_node
;
1961 dependence_stats
.num_siv_independent
++;
1964 dependence_stats
.num_siv_dependent
++;
1968 /* When the step does not divide the difference, there
1972 *overlaps_a
= conflict_fn_no_dependence ();
1973 *overlaps_b
= conflict_fn_no_dependence ();
1974 *last_conflicts
= integer_zero_node
;
1975 dependence_stats
.num_siv_independent
++;
1985 In this case, chrec_a will not overlap with chrec_b. */
1986 *overlaps_a
= conflict_fn_no_dependence ();
1987 *overlaps_b
= conflict_fn_no_dependence ();
1988 *last_conflicts
= integer_zero_node
;
1989 dependence_stats
.num_siv_independent
++;
1997 /* Helper recursive function for initializing the matrix A. Returns
1998 the initial value of CHREC. */
2001 initialize_matrix_A (lambda_matrix A
, tree chrec
, unsigned index
, int mult
)
2005 switch (TREE_CODE (chrec
))
2007 case POLYNOMIAL_CHREC
:
2008 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec
)) == INTEGER_CST
);
2010 A
[index
][0] = mult
* int_cst_value (CHREC_RIGHT (chrec
));
2011 return initialize_matrix_A (A
, CHREC_LEFT (chrec
), index
+ 1, mult
);
2017 tree op0
= initialize_matrix_A (A
, TREE_OPERAND (chrec
, 0), index
, mult
);
2018 tree op1
= initialize_matrix_A (A
, TREE_OPERAND (chrec
, 1), index
, mult
);
2020 return chrec_fold_op (TREE_CODE (chrec
), chrec_type (chrec
), op0
, op1
);
2025 tree op
= initialize_matrix_A (A
, TREE_OPERAND (chrec
, 0), index
, mult
);
2026 return chrec_convert (chrec_type (chrec
), op
, NULL
);
2031 /* Handle ~X as -1 - X. */
2032 tree op
= initialize_matrix_A (A
, TREE_OPERAND (chrec
, 0), index
, mult
);
2033 return chrec_fold_op (MINUS_EXPR
, chrec_type (chrec
),
2034 build_int_cst (TREE_TYPE (chrec
), -1), op
);
2046 #define FLOOR_DIV(x,y) ((x) / (y))
2048 /* Solves the special case of the Diophantine equation:
2049 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2051 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2052 number of iterations that loops X and Y run. The overlaps will be
2053 constructed as evolutions in dimension DIM. */
2056 compute_overlap_steps_for_affine_univar (int niter
, int step_a
, int step_b
,
2057 affine_fn
*overlaps_a
,
2058 affine_fn
*overlaps_b
,
2059 tree
*last_conflicts
, int dim
)
2061 if (((step_a
> 0 && step_b
> 0)
2062 || (step_a
< 0 && step_b
< 0)))
2064 int step_overlaps_a
, step_overlaps_b
;
2065 int gcd_steps_a_b
, last_conflict
, tau2
;
2067 gcd_steps_a_b
= gcd (step_a
, step_b
);
2068 step_overlaps_a
= step_b
/ gcd_steps_a_b
;
2069 step_overlaps_b
= step_a
/ gcd_steps_a_b
;
2073 tau2
= FLOOR_DIV (niter
, step_overlaps_a
);
2074 tau2
= MIN (tau2
, FLOOR_DIV (niter
, step_overlaps_b
));
2075 last_conflict
= tau2
;
2076 *last_conflicts
= build_int_cst (NULL_TREE
, last_conflict
);
2079 *last_conflicts
= chrec_dont_know
;
2081 *overlaps_a
= affine_fn_univar (integer_zero_node
, dim
,
2082 build_int_cst (NULL_TREE
,
2084 *overlaps_b
= affine_fn_univar (integer_zero_node
, dim
,
2085 build_int_cst (NULL_TREE
,
2091 *overlaps_a
= affine_fn_cst (integer_zero_node
);
2092 *overlaps_b
= affine_fn_cst (integer_zero_node
);
2093 *last_conflicts
= integer_zero_node
;
2097 /* Solves the special case of a Diophantine equation where CHREC_A is
2098 an affine bivariate function, and CHREC_B is an affine univariate
2099 function. For example,
2101 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2103 has the following overlapping functions:
2105 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2106 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2107 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2109 FORNOW: This is a specialized implementation for a case occurring in
2110 a common benchmark. Implement the general algorithm. */
2113 compute_overlap_steps_for_affine_1_2 (tree chrec_a
, tree chrec_b
,
2114 conflict_function
**overlaps_a
,
2115 conflict_function
**overlaps_b
,
2116 tree
*last_conflicts
)
2118 bool xz_p
, yz_p
, xyz_p
;
2119 int step_x
, step_y
, step_z
;
2120 HOST_WIDE_INT niter_x
, niter_y
, niter_z
, niter
;
2121 affine_fn overlaps_a_xz
, overlaps_b_xz
;
2122 affine_fn overlaps_a_yz
, overlaps_b_yz
;
2123 affine_fn overlaps_a_xyz
, overlaps_b_xyz
;
2124 affine_fn ova1
, ova2
, ovb
;
2125 tree last_conflicts_xz
, last_conflicts_yz
, last_conflicts_xyz
;
2127 step_x
= int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a
)));
2128 step_y
= int_cst_value (CHREC_RIGHT (chrec_a
));
2129 step_z
= int_cst_value (CHREC_RIGHT (chrec_b
));
2131 niter_x
= max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a
)));
2132 niter_y
= max_stmt_executions_int (get_chrec_loop (chrec_a
));
2133 niter_z
= max_stmt_executions_int (get_chrec_loop (chrec_b
));
2135 if (niter_x
< 0 || niter_y
< 0 || niter_z
< 0)
2137 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2138 fprintf (dump_file
, "overlap steps test failed: no iteration counts.\n");
2140 *overlaps_a
= conflict_fn_not_known ();
2141 *overlaps_b
= conflict_fn_not_known ();
2142 *last_conflicts
= chrec_dont_know
;
2146 niter
= MIN (niter_x
, niter_z
);
2147 compute_overlap_steps_for_affine_univar (niter
, step_x
, step_z
,
2150 &last_conflicts_xz
, 1);
2151 niter
= MIN (niter_y
, niter_z
);
2152 compute_overlap_steps_for_affine_univar (niter
, step_y
, step_z
,
2155 &last_conflicts_yz
, 2);
2156 niter
= MIN (niter_x
, niter_z
);
2157 niter
= MIN (niter_y
, niter
);
2158 compute_overlap_steps_for_affine_univar (niter
, step_x
+ step_y
, step_z
,
2161 &last_conflicts_xyz
, 3);
2163 xz_p
= !integer_zerop (last_conflicts_xz
);
2164 yz_p
= !integer_zerop (last_conflicts_yz
);
2165 xyz_p
= !integer_zerop (last_conflicts_xyz
);
2167 if (xz_p
|| yz_p
|| xyz_p
)
2169 ova1
= affine_fn_cst (integer_zero_node
);
2170 ova2
= affine_fn_cst (integer_zero_node
);
2171 ovb
= affine_fn_cst (integer_zero_node
);
2174 affine_fn t0
= ova1
;
2177 ova1
= affine_fn_plus (ova1
, overlaps_a_xz
);
2178 ovb
= affine_fn_plus (ovb
, overlaps_b_xz
);
2179 affine_fn_free (t0
);
2180 affine_fn_free (t2
);
2181 *last_conflicts
= last_conflicts_xz
;
2185 affine_fn t0
= ova2
;
2188 ova2
= affine_fn_plus (ova2
, overlaps_a_yz
);
2189 ovb
= affine_fn_plus (ovb
, overlaps_b_yz
);
2190 affine_fn_free (t0
);
2191 affine_fn_free (t2
);
2192 *last_conflicts
= last_conflicts_yz
;
2196 affine_fn t0
= ova1
;
2197 affine_fn t2
= ova2
;
2200 ova1
= affine_fn_plus (ova1
, overlaps_a_xyz
);
2201 ova2
= affine_fn_plus (ova2
, overlaps_a_xyz
);
2202 ovb
= affine_fn_plus (ovb
, overlaps_b_xyz
);
2203 affine_fn_free (t0
);
2204 affine_fn_free (t2
);
2205 affine_fn_free (t4
);
2206 *last_conflicts
= last_conflicts_xyz
;
2208 *overlaps_a
= conflict_fn (2, ova1
, ova2
);
2209 *overlaps_b
= conflict_fn (1, ovb
);
2213 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2214 *overlaps_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2215 *last_conflicts
= integer_zero_node
;
2218 affine_fn_free (overlaps_a_xz
);
2219 affine_fn_free (overlaps_b_xz
);
2220 affine_fn_free (overlaps_a_yz
);
2221 affine_fn_free (overlaps_b_yz
);
2222 affine_fn_free (overlaps_a_xyz
);
2223 affine_fn_free (overlaps_b_xyz
);
2226 /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2229 lambda_vector_copy (lambda_vector vec1
, lambda_vector vec2
,
2232 memcpy (vec2
, vec1
, size
* sizeof (*vec1
));
2235 /* Copy the elements of M x N matrix MAT1 to MAT2. */
2238 lambda_matrix_copy (lambda_matrix mat1
, lambda_matrix mat2
,
2243 for (i
= 0; i
< m
; i
++)
2244 lambda_vector_copy (mat1
[i
], mat2
[i
], n
);
2247 /* Store the N x N identity matrix in MAT. */
2250 lambda_matrix_id (lambda_matrix mat
, int size
)
2254 for (i
= 0; i
< size
; i
++)
2255 for (j
= 0; j
< size
; j
++)
2256 mat
[i
][j
] = (i
== j
) ? 1 : 0;
2259 /* Return the first nonzero element of vector VEC1 between START and N.
2260 We must have START <= N. Returns N if VEC1 is the zero vector. */
2263 lambda_vector_first_nz (lambda_vector vec1
, int n
, int start
)
2266 while (j
< n
&& vec1
[j
] == 0)
2271 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2272 R2 = R2 + CONST1 * R1. */
2275 lambda_matrix_row_add (lambda_matrix mat
, int n
, int r1
, int r2
, int const1
)
2282 for (i
= 0; i
< n
; i
++)
2283 mat
[r2
][i
] += const1
* mat
[r1
][i
];
2286 /* Swap rows R1 and R2 in matrix MAT. */
2289 lambda_matrix_row_exchange (lambda_matrix mat
, int r1
, int r2
)
2298 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
2299 and store the result in VEC2. */
2302 lambda_vector_mult_const (lambda_vector vec1
, lambda_vector vec2
,
2303 int size
, int const1
)
2308 lambda_vector_clear (vec2
, size
);
2310 for (i
= 0; i
< size
; i
++)
2311 vec2
[i
] = const1
* vec1
[i
];
2314 /* Negate vector VEC1 with length SIZE and store it in VEC2. */
2317 lambda_vector_negate (lambda_vector vec1
, lambda_vector vec2
,
2320 lambda_vector_mult_const (vec1
, vec2
, size
, -1);
2323 /* Negate row R1 of matrix MAT which has N columns. */
2326 lambda_matrix_row_negate (lambda_matrix mat
, int n
, int r1
)
2328 lambda_vector_negate (mat
[r1
], mat
[r1
], n
);
2331 /* Return true if two vectors are equal. */
2334 lambda_vector_equal (lambda_vector vec1
, lambda_vector vec2
, int size
)
2337 for (i
= 0; i
< size
; i
++)
2338 if (vec1
[i
] != vec2
[i
])
2343 /* Given an M x N integer matrix A, this function determines an M x
2344 M unimodular matrix U, and an M x N echelon matrix S such that
2345 "U.A = S". This decomposition is also known as "right Hermite".
2347 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2348 Restructuring Compilers" Utpal Banerjee. */
2351 lambda_matrix_right_hermite (lambda_matrix A
, int m
, int n
,
2352 lambda_matrix S
, lambda_matrix U
)
2356 lambda_matrix_copy (A
, S
, m
, n
);
2357 lambda_matrix_id (U
, m
);
2359 for (j
= 0; j
< n
; j
++)
2361 if (lambda_vector_first_nz (S
[j
], m
, i0
) < m
)
2364 for (i
= m
- 1; i
>= i0
; i
--)
2366 while (S
[i
][j
] != 0)
2368 int sigma
, factor
, a
, b
;
2372 sigma
= (a
* b
< 0) ? -1: 1;
2375 factor
= sigma
* (a
/ b
);
2377 lambda_matrix_row_add (S
, n
, i
, i
-1, -factor
);
2378 lambda_matrix_row_exchange (S
, i
, i
-1);
2380 lambda_matrix_row_add (U
, m
, i
, i
-1, -factor
);
2381 lambda_matrix_row_exchange (U
, i
, i
-1);
2388 /* Determines the overlapping elements due to accesses CHREC_A and
2389 CHREC_B, that are affine functions. This function cannot handle
2390 symbolic evolution functions, ie. when initial conditions are
2391 parameters, because it uses lambda matrices of integers. */
2394 analyze_subscript_affine_affine (tree chrec_a
,
2396 conflict_function
**overlaps_a
,
2397 conflict_function
**overlaps_b
,
2398 tree
*last_conflicts
)
2400 unsigned nb_vars_a
, nb_vars_b
, dim
;
2401 HOST_WIDE_INT init_a
, init_b
, gamma
, gcd_alpha_beta
;
2402 lambda_matrix A
, U
, S
;
2403 struct obstack scratch_obstack
;
2405 if (eq_evolutions_p (chrec_a
, chrec_b
))
2407 /* The accessed index overlaps for each iteration in the
2409 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2410 *overlaps_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2411 *last_conflicts
= chrec_dont_know
;
2414 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2415 fprintf (dump_file
, "(analyze_subscript_affine_affine \n");
2417 /* For determining the initial intersection, we have to solve a
2418 Diophantine equation. This is the most time consuming part.
2420 For answering to the question: "Is there a dependence?" we have
2421 to prove that there exists a solution to the Diophantine
2422 equation, and that the solution is in the iteration domain,
2423 i.e. the solution is positive or zero, and that the solution
2424 happens before the upper bound loop.nb_iterations. Otherwise
2425 there is no dependence. This function outputs a description of
2426 the iterations that hold the intersections. */
2428 nb_vars_a
= nb_vars_in_chrec (chrec_a
);
2429 nb_vars_b
= nb_vars_in_chrec (chrec_b
);
2431 gcc_obstack_init (&scratch_obstack
);
2433 dim
= nb_vars_a
+ nb_vars_b
;
2434 U
= lambda_matrix_new (dim
, dim
, &scratch_obstack
);
2435 A
= lambda_matrix_new (dim
, 1, &scratch_obstack
);
2436 S
= lambda_matrix_new (dim
, 1, &scratch_obstack
);
2438 init_a
= int_cst_value (initialize_matrix_A (A
, chrec_a
, 0, 1));
2439 init_b
= int_cst_value (initialize_matrix_A (A
, chrec_b
, nb_vars_a
, -1));
2440 gamma
= init_b
- init_a
;
2442 /* Don't do all the hard work of solving the Diophantine equation
2443 when we already know the solution: for example,
2446 | gamma = 3 - 3 = 0.
2447 Then the first overlap occurs during the first iterations:
2448 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2452 if (nb_vars_a
== 1 && nb_vars_b
== 1)
2454 HOST_WIDE_INT step_a
, step_b
;
2455 HOST_WIDE_INT niter
, niter_a
, niter_b
;
2458 niter_a
= max_stmt_executions_int (get_chrec_loop (chrec_a
));
2459 niter_b
= max_stmt_executions_int (get_chrec_loop (chrec_b
));
2460 niter
= MIN (niter_a
, niter_b
);
2461 step_a
= int_cst_value (CHREC_RIGHT (chrec_a
));
2462 step_b
= int_cst_value (CHREC_RIGHT (chrec_b
));
2464 compute_overlap_steps_for_affine_univar (niter
, step_a
, step_b
,
2467 *overlaps_a
= conflict_fn (1, ova
);
2468 *overlaps_b
= conflict_fn (1, ovb
);
2471 else if (nb_vars_a
== 2 && nb_vars_b
== 1)
2472 compute_overlap_steps_for_affine_1_2
2473 (chrec_a
, chrec_b
, overlaps_a
, overlaps_b
, last_conflicts
);
2475 else if (nb_vars_a
== 1 && nb_vars_b
== 2)
2476 compute_overlap_steps_for_affine_1_2
2477 (chrec_b
, chrec_a
, overlaps_b
, overlaps_a
, last_conflicts
);
2481 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2482 fprintf (dump_file
, "affine-affine test failed: too many variables.\n");
2483 *overlaps_a
= conflict_fn_not_known ();
2484 *overlaps_b
= conflict_fn_not_known ();
2485 *last_conflicts
= chrec_dont_know
;
2487 goto end_analyze_subs_aa
;
2491 lambda_matrix_right_hermite (A
, dim
, 1, S
, U
);
2496 lambda_matrix_row_negate (U
, dim
, 0);
2498 gcd_alpha_beta
= S
[0][0];
2500 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2501 but that is a quite strange case. Instead of ICEing, answer
2503 if (gcd_alpha_beta
== 0)
2505 *overlaps_a
= conflict_fn_not_known ();
2506 *overlaps_b
= conflict_fn_not_known ();
2507 *last_conflicts
= chrec_dont_know
;
2508 goto end_analyze_subs_aa
;
2511 /* The classic "gcd-test". */
2512 if (!int_divides_p (gcd_alpha_beta
, gamma
))
2514 /* The "gcd-test" has determined that there is no integer
2515 solution, i.e. there is no dependence. */
2516 *overlaps_a
= conflict_fn_no_dependence ();
2517 *overlaps_b
= conflict_fn_no_dependence ();
2518 *last_conflicts
= integer_zero_node
;
2521 /* Both access functions are univariate. This includes SIV and MIV cases. */
2522 else if (nb_vars_a
== 1 && nb_vars_b
== 1)
2524 /* Both functions should have the same evolution sign. */
2525 if (((A
[0][0] > 0 && -A
[1][0] > 0)
2526 || (A
[0][0] < 0 && -A
[1][0] < 0)))
2528 /* The solutions are given by:
2530 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2533 For a given integer t. Using the following variables,
2535 | i0 = u11 * gamma / gcd_alpha_beta
2536 | j0 = u12 * gamma / gcd_alpha_beta
2543 | y0 = j0 + j1 * t. */
2544 HOST_WIDE_INT i0
, j0
, i1
, j1
;
2546 i0
= U
[0][0] * gamma
/ gcd_alpha_beta
;
2547 j0
= U
[0][1] * gamma
/ gcd_alpha_beta
;
2551 if ((i1
== 0 && i0
< 0)
2552 || (j1
== 0 && j0
< 0))
2554 /* There is no solution.
2555 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2556 falls in here, but for the moment we don't look at the
2557 upper bound of the iteration domain. */
2558 *overlaps_a
= conflict_fn_no_dependence ();
2559 *overlaps_b
= conflict_fn_no_dependence ();
2560 *last_conflicts
= integer_zero_node
;
2561 goto end_analyze_subs_aa
;
2564 if (i1
> 0 && j1
> 0)
2566 HOST_WIDE_INT niter_a
2567 = max_stmt_executions_int (get_chrec_loop (chrec_a
));
2568 HOST_WIDE_INT niter_b
2569 = max_stmt_executions_int (get_chrec_loop (chrec_b
));
2570 HOST_WIDE_INT niter
= MIN (niter_a
, niter_b
);
2572 /* (X0, Y0) is a solution of the Diophantine equation:
2573 "chrec_a (X0) = chrec_b (Y0)". */
2574 HOST_WIDE_INT tau1
= MAX (CEIL (-i0
, i1
),
2576 HOST_WIDE_INT x0
= i1
* tau1
+ i0
;
2577 HOST_WIDE_INT y0
= j1
* tau1
+ j0
;
2579 /* (X1, Y1) is the smallest positive solution of the eq
2580 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2581 first conflict occurs. */
2582 HOST_WIDE_INT min_multiple
= MIN (x0
/ i1
, y0
/ j1
);
2583 HOST_WIDE_INT x1
= x0
- i1
* min_multiple
;
2584 HOST_WIDE_INT y1
= y0
- j1
* min_multiple
;
2588 HOST_WIDE_INT tau2
= MIN (FLOOR_DIV (niter
- i0
, i1
),
2589 FLOOR_DIV (niter
- j0
, j1
));
2590 HOST_WIDE_INT last_conflict
= tau2
- (x1
- i0
)/i1
;
2592 /* If the overlap occurs outside of the bounds of the
2593 loop, there is no dependence. */
2594 if (x1
>= niter
|| y1
>= niter
)
2596 *overlaps_a
= conflict_fn_no_dependence ();
2597 *overlaps_b
= conflict_fn_no_dependence ();
2598 *last_conflicts
= integer_zero_node
;
2599 goto end_analyze_subs_aa
;
2602 *last_conflicts
= build_int_cst (NULL_TREE
, last_conflict
);
2605 *last_conflicts
= chrec_dont_know
;
2609 affine_fn_univar (build_int_cst (NULL_TREE
, x1
),
2611 build_int_cst (NULL_TREE
, i1
)));
2614 affine_fn_univar (build_int_cst (NULL_TREE
, y1
),
2616 build_int_cst (NULL_TREE
, j1
)));
2620 /* FIXME: For the moment, the upper bound of the
2621 iteration domain for i and j is not checked. */
2622 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2623 fprintf (dump_file
, "affine-affine test failed: unimplemented.\n");
2624 *overlaps_a
= conflict_fn_not_known ();
2625 *overlaps_b
= conflict_fn_not_known ();
2626 *last_conflicts
= chrec_dont_know
;
2631 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2632 fprintf (dump_file
, "affine-affine test failed: unimplemented.\n");
2633 *overlaps_a
= conflict_fn_not_known ();
2634 *overlaps_b
= conflict_fn_not_known ();
2635 *last_conflicts
= chrec_dont_know
;
2640 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2641 fprintf (dump_file
, "affine-affine test failed: unimplemented.\n");
2642 *overlaps_a
= conflict_fn_not_known ();
2643 *overlaps_b
= conflict_fn_not_known ();
2644 *last_conflicts
= chrec_dont_know
;
2647 end_analyze_subs_aa
:
2648 obstack_free (&scratch_obstack
, NULL
);
2649 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2651 fprintf (dump_file
, " (overlaps_a = ");
2652 dump_conflict_function (dump_file
, *overlaps_a
);
2653 fprintf (dump_file
, ")\n (overlaps_b = ");
2654 dump_conflict_function (dump_file
, *overlaps_b
);
2655 fprintf (dump_file
, ")\n");
2656 fprintf (dump_file
, ")\n");
2660 /* Returns true when analyze_subscript_affine_affine can be used for
2661 determining the dependence relation between chrec_a and chrec_b,
2662 that contain symbols. This function modifies chrec_a and chrec_b
2663 such that the analysis result is the same, and such that they don't
2664 contain symbols, and then can safely be passed to the analyzer.
2666 Example: The analysis of the following tuples of evolutions produce
2667 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2670 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2671 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2675 can_use_analyze_subscript_affine_affine (tree
*chrec_a
, tree
*chrec_b
)
2677 tree diff
, type
, left_a
, left_b
, right_b
;
2679 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a
))
2680 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b
)))
2681 /* FIXME: For the moment not handled. Might be refined later. */
2684 type
= chrec_type (*chrec_a
);
2685 left_a
= CHREC_LEFT (*chrec_a
);
2686 left_b
= chrec_convert (type
, CHREC_LEFT (*chrec_b
), NULL
);
2687 diff
= chrec_fold_minus (type
, left_a
, left_b
);
2689 if (!evolution_function_is_constant_p (diff
))
2692 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2693 fprintf (dump_file
, "can_use_subscript_aff_aff_for_symbolic \n");
2695 *chrec_a
= build_polynomial_chrec (CHREC_VARIABLE (*chrec_a
),
2696 diff
, CHREC_RIGHT (*chrec_a
));
2697 right_b
= chrec_convert (type
, CHREC_RIGHT (*chrec_b
), NULL
);
2698 *chrec_b
= build_polynomial_chrec (CHREC_VARIABLE (*chrec_b
),
2699 build_int_cst (type
, 0),
2704 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2705 *OVERLAPS_B are initialized to the functions that describe the
2706 relation between the elements accessed twice by CHREC_A and
2707 CHREC_B. For k >= 0, the following property is verified:
2709 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2712 analyze_siv_subscript (tree chrec_a
,
2714 conflict_function
**overlaps_a
,
2715 conflict_function
**overlaps_b
,
2716 tree
*last_conflicts
,
2719 dependence_stats
.num_siv
++;
2721 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2722 fprintf (dump_file
, "(analyze_siv_subscript \n");
2724 if (evolution_function_is_constant_p (chrec_a
)
2725 && evolution_function_is_affine_in_loop (chrec_b
, loop_nest_num
))
2726 analyze_siv_subscript_cst_affine (chrec_a
, chrec_b
,
2727 overlaps_a
, overlaps_b
, last_conflicts
);
2729 else if (evolution_function_is_affine_in_loop (chrec_a
, loop_nest_num
)
2730 && evolution_function_is_constant_p (chrec_b
))
2731 analyze_siv_subscript_cst_affine (chrec_b
, chrec_a
,
2732 overlaps_b
, overlaps_a
, last_conflicts
);
2734 else if (evolution_function_is_affine_in_loop (chrec_a
, loop_nest_num
)
2735 && evolution_function_is_affine_in_loop (chrec_b
, loop_nest_num
))
2737 if (!chrec_contains_symbols (chrec_a
)
2738 && !chrec_contains_symbols (chrec_b
))
2740 analyze_subscript_affine_affine (chrec_a
, chrec_b
,
2741 overlaps_a
, overlaps_b
,
2744 if (CF_NOT_KNOWN_P (*overlaps_a
)
2745 || CF_NOT_KNOWN_P (*overlaps_b
))
2746 dependence_stats
.num_siv_unimplemented
++;
2747 else if (CF_NO_DEPENDENCE_P (*overlaps_a
)
2748 || CF_NO_DEPENDENCE_P (*overlaps_b
))
2749 dependence_stats
.num_siv_independent
++;
2751 dependence_stats
.num_siv_dependent
++;
2753 else if (can_use_analyze_subscript_affine_affine (&chrec_a
,
2756 analyze_subscript_affine_affine (chrec_a
, chrec_b
,
2757 overlaps_a
, overlaps_b
,
2760 if (CF_NOT_KNOWN_P (*overlaps_a
)
2761 || CF_NOT_KNOWN_P (*overlaps_b
))
2762 dependence_stats
.num_siv_unimplemented
++;
2763 else if (CF_NO_DEPENDENCE_P (*overlaps_a
)
2764 || CF_NO_DEPENDENCE_P (*overlaps_b
))
2765 dependence_stats
.num_siv_independent
++;
2767 dependence_stats
.num_siv_dependent
++;
2770 goto siv_subscript_dontknow
;
2775 siv_subscript_dontknow
:;
2776 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2777 fprintf (dump_file
, "siv test failed: unimplemented.\n");
2778 *overlaps_a
= conflict_fn_not_known ();
2779 *overlaps_b
= conflict_fn_not_known ();
2780 *last_conflicts
= chrec_dont_know
;
2781 dependence_stats
.num_siv_unimplemented
++;
2784 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2785 fprintf (dump_file
, ")\n");
2788 /* Returns false if we can prove that the greatest common divisor of the steps
2789 of CHREC does not divide CST, false otherwise. */
2792 gcd_of_steps_may_divide_p (const_tree chrec
, const_tree cst
)
2794 HOST_WIDE_INT cd
= 0, val
;
2797 if (!host_integerp (cst
, 0))
2799 val
= tree_low_cst (cst
, 0);
2801 while (TREE_CODE (chrec
) == POLYNOMIAL_CHREC
)
2803 step
= CHREC_RIGHT (chrec
);
2804 if (!host_integerp (step
, 0))
2806 cd
= gcd (cd
, tree_low_cst (step
, 0));
2807 chrec
= CHREC_LEFT (chrec
);
2810 return val
% cd
== 0;
2813 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
2814 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2815 functions that describe the relation between the elements accessed
2816 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2819 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2822 analyze_miv_subscript (tree chrec_a
,
2824 conflict_function
**overlaps_a
,
2825 conflict_function
**overlaps_b
,
2826 tree
*last_conflicts
,
2827 struct loop
*loop_nest
)
2829 tree type
, difference
;
2831 dependence_stats
.num_miv
++;
2832 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2833 fprintf (dump_file
, "(analyze_miv_subscript \n");
2835 type
= signed_type_for_types (TREE_TYPE (chrec_a
), TREE_TYPE (chrec_b
));
2836 chrec_a
= chrec_convert (type
, chrec_a
, NULL
);
2837 chrec_b
= chrec_convert (type
, chrec_b
, NULL
);
2838 difference
= chrec_fold_minus (type
, chrec_a
, chrec_b
);
2840 if (eq_evolutions_p (chrec_a
, chrec_b
))
2842 /* Access functions are the same: all the elements are accessed
2843 in the same order. */
2844 *overlaps_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2845 *overlaps_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2846 *last_conflicts
= max_stmt_executions_tree (get_chrec_loop (chrec_a
));
2847 dependence_stats
.num_miv_dependent
++;
2850 else if (evolution_function_is_constant_p (difference
)
2851 /* For the moment, the following is verified:
2852 evolution_function_is_affine_multivariate_p (chrec_a,
2854 && !gcd_of_steps_may_divide_p (chrec_a
, difference
))
2856 /* testsuite/.../ssa-chrec-33.c
2857 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2859 The difference is 1, and all the evolution steps are multiples
2860 of 2, consequently there are no overlapping elements. */
2861 *overlaps_a
= conflict_fn_no_dependence ();
2862 *overlaps_b
= conflict_fn_no_dependence ();
2863 *last_conflicts
= integer_zero_node
;
2864 dependence_stats
.num_miv_independent
++;
2867 else if (evolution_function_is_affine_multivariate_p (chrec_a
, loop_nest
->num
)
2868 && !chrec_contains_symbols (chrec_a
)
2869 && evolution_function_is_affine_multivariate_p (chrec_b
, loop_nest
->num
)
2870 && !chrec_contains_symbols (chrec_b
))
2872 /* testsuite/.../ssa-chrec-35.c
2873 {0, +, 1}_2 vs. {0, +, 1}_3
2874 the overlapping elements are respectively located at iterations:
2875 {0, +, 1}_x and {0, +, 1}_x,
2876 in other words, we have the equality:
2877 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2880 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2881 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2883 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2884 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2886 analyze_subscript_affine_affine (chrec_a
, chrec_b
,
2887 overlaps_a
, overlaps_b
, last_conflicts
);
2889 if (CF_NOT_KNOWN_P (*overlaps_a
)
2890 || CF_NOT_KNOWN_P (*overlaps_b
))
2891 dependence_stats
.num_miv_unimplemented
++;
2892 else if (CF_NO_DEPENDENCE_P (*overlaps_a
)
2893 || CF_NO_DEPENDENCE_P (*overlaps_b
))
2894 dependence_stats
.num_miv_independent
++;
2896 dependence_stats
.num_miv_dependent
++;
2901 /* When the analysis is too difficult, answer "don't know". */
2902 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2903 fprintf (dump_file
, "analyze_miv_subscript test failed: unimplemented.\n");
2905 *overlaps_a
= conflict_fn_not_known ();
2906 *overlaps_b
= conflict_fn_not_known ();
2907 *last_conflicts
= chrec_dont_know
;
2908 dependence_stats
.num_miv_unimplemented
++;
2911 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2912 fprintf (dump_file
, ")\n");
2915 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
2916 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
2917 OVERLAP_ITERATIONS_B are initialized with two functions that
2918 describe the iterations that contain conflicting elements.
2920 Remark: For an integer k >= 0, the following equality is true:
2922 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
2926 analyze_overlapping_iterations (tree chrec_a
,
2928 conflict_function
**overlap_iterations_a
,
2929 conflict_function
**overlap_iterations_b
,
2930 tree
*last_conflicts
, struct loop
*loop_nest
)
2932 unsigned int lnn
= loop_nest
->num
;
2934 dependence_stats
.num_subscript_tests
++;
2936 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2938 fprintf (dump_file
, "(analyze_overlapping_iterations \n");
2939 fprintf (dump_file
, " (chrec_a = ");
2940 print_generic_expr (dump_file
, chrec_a
, 0);
2941 fprintf (dump_file
, ")\n (chrec_b = ");
2942 print_generic_expr (dump_file
, chrec_b
, 0);
2943 fprintf (dump_file
, ")\n");
2946 if (chrec_a
== NULL_TREE
2947 || chrec_b
== NULL_TREE
2948 || chrec_contains_undetermined (chrec_a
)
2949 || chrec_contains_undetermined (chrec_b
))
2951 dependence_stats
.num_subscript_undetermined
++;
2953 *overlap_iterations_a
= conflict_fn_not_known ();
2954 *overlap_iterations_b
= conflict_fn_not_known ();
2957 /* If they are the same chrec, and are affine, they overlap
2958 on every iteration. */
2959 else if (eq_evolutions_p (chrec_a
, chrec_b
)
2960 && (evolution_function_is_affine_multivariate_p (chrec_a
, lnn
)
2961 || operand_equal_p (chrec_a
, chrec_b
, 0)))
2963 dependence_stats
.num_same_subscript_function
++;
2964 *overlap_iterations_a
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2965 *overlap_iterations_b
= conflict_fn (1, affine_fn_cst (integer_zero_node
));
2966 *last_conflicts
= chrec_dont_know
;
2969 /* If they aren't the same, and aren't affine, we can't do anything
2971 else if ((chrec_contains_symbols (chrec_a
)
2972 || chrec_contains_symbols (chrec_b
))
2973 && (!evolution_function_is_affine_multivariate_p (chrec_a
, lnn
)
2974 || !evolution_function_is_affine_multivariate_p (chrec_b
, lnn
)))
2976 dependence_stats
.num_subscript_undetermined
++;
2977 *overlap_iterations_a
= conflict_fn_not_known ();
2978 *overlap_iterations_b
= conflict_fn_not_known ();
2981 else if (ziv_subscript_p (chrec_a
, chrec_b
))
2982 analyze_ziv_subscript (chrec_a
, chrec_b
,
2983 overlap_iterations_a
, overlap_iterations_b
,
2986 else if (siv_subscript_p (chrec_a
, chrec_b
))
2987 analyze_siv_subscript (chrec_a
, chrec_b
,
2988 overlap_iterations_a
, overlap_iterations_b
,
2989 last_conflicts
, lnn
);
2992 analyze_miv_subscript (chrec_a
, chrec_b
,
2993 overlap_iterations_a
, overlap_iterations_b
,
2994 last_conflicts
, loop_nest
);
2996 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2998 fprintf (dump_file
, " (overlap_iterations_a = ");
2999 dump_conflict_function (dump_file
, *overlap_iterations_a
);
3000 fprintf (dump_file
, ")\n (overlap_iterations_b = ");
3001 dump_conflict_function (dump_file
, *overlap_iterations_b
);
3002 fprintf (dump_file
, ")\n");
3003 fprintf (dump_file
, ")\n");
3007 /* Helper function for uniquely inserting distance vectors. */
3010 save_dist_v (struct data_dependence_relation
*ddr
, lambda_vector dist_v
)
3015 FOR_EACH_VEC_ELT (lambda_vector
, DDR_DIST_VECTS (ddr
), i
, v
)
3016 if (lambda_vector_equal (v
, dist_v
, DDR_NB_LOOPS (ddr
)))
3019 VEC_safe_push (lambda_vector
, heap
, DDR_DIST_VECTS (ddr
), dist_v
);
3022 /* Helper function for uniquely inserting direction vectors. */
3025 save_dir_v (struct data_dependence_relation
*ddr
, lambda_vector dir_v
)
3030 FOR_EACH_VEC_ELT (lambda_vector
, DDR_DIR_VECTS (ddr
), i
, v
)
3031 if (lambda_vector_equal (v
, dir_v
, DDR_NB_LOOPS (ddr
)))
3034 VEC_safe_push (lambda_vector
, heap
, DDR_DIR_VECTS (ddr
), dir_v
);
3037 /* Add a distance of 1 on all the loops outer than INDEX. If we
3038 haven't yet determined a distance for this outer loop, push a new
3039 distance vector composed of the previous distance, and a distance
3040 of 1 for this outer loop. Example:
3048 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3049 save (0, 1), then we have to save (1, 0). */
3052 add_outer_distances (struct data_dependence_relation
*ddr
,
3053 lambda_vector dist_v
, int index
)
3055 /* For each outer loop where init_v is not set, the accesses are
3056 in dependence of distance 1 in the loop. */
3057 while (--index
>= 0)
3059 lambda_vector save_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3060 lambda_vector_copy (dist_v
, save_v
, DDR_NB_LOOPS (ddr
));
3062 save_dist_v (ddr
, save_v
);
3066 /* Return false when fail to represent the data dependence as a
3067 distance vector. INIT_B is set to true when a component has been
3068 added to the distance vector DIST_V. INDEX_CARRY is then set to
3069 the index in DIST_V that carries the dependence. */
3072 build_classic_dist_vector_1 (struct data_dependence_relation
*ddr
,
3073 struct data_reference
*ddr_a
,
3074 struct data_reference
*ddr_b
,
3075 lambda_vector dist_v
, bool *init_b
,
3079 lambda_vector init_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3081 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3083 tree access_fn_a
, access_fn_b
;
3084 struct subscript
*subscript
= DDR_SUBSCRIPT (ddr
, i
);
3086 if (chrec_contains_undetermined (SUB_DISTANCE (subscript
)))
3088 non_affine_dependence_relation (ddr
);
3092 access_fn_a
= DR_ACCESS_FN (ddr_a
, i
);
3093 access_fn_b
= DR_ACCESS_FN (ddr_b
, i
);
3095 if (TREE_CODE (access_fn_a
) == POLYNOMIAL_CHREC
3096 && TREE_CODE (access_fn_b
) == POLYNOMIAL_CHREC
)
3099 int var_a
= CHREC_VARIABLE (access_fn_a
);
3100 int var_b
= CHREC_VARIABLE (access_fn_b
);
3103 || chrec_contains_undetermined (SUB_DISTANCE (subscript
)))
3105 non_affine_dependence_relation (ddr
);
3109 dist
= int_cst_value (SUB_DISTANCE (subscript
));
3110 index
= index_in_loop_nest (var_a
, DDR_LOOP_NEST (ddr
));
3111 *index_carry
= MIN (index
, *index_carry
);
3113 /* This is the subscript coupling test. If we have already
3114 recorded a distance for this loop (a distance coming from
3115 another subscript), it should be the same. For example,
3116 in the following code, there is no dependence:
3123 if (init_v
[index
] != 0 && dist_v
[index
] != dist
)
3125 finalize_ddr_dependent (ddr
, chrec_known
);
3129 dist_v
[index
] = dist
;
3133 else if (!operand_equal_p (access_fn_a
, access_fn_b
, 0))
3135 /* This can be for example an affine vs. constant dependence
3136 (T[i] vs. T[3]) that is not an affine dependence and is
3137 not representable as a distance vector. */
3138 non_affine_dependence_relation (ddr
);
3146 /* Return true when the DDR contains only constant access functions. */
3149 constant_access_functions (const struct data_dependence_relation
*ddr
)
3153 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3154 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr
), i
))
3155 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr
), i
)))
3161 /* Helper function for the case where DDR_A and DDR_B are the same
3162 multivariate access function with a constant step. For an example
3166 add_multivariate_self_dist (struct data_dependence_relation
*ddr
, tree c_2
)
3169 tree c_1
= CHREC_LEFT (c_2
);
3170 tree c_0
= CHREC_LEFT (c_1
);
3171 lambda_vector dist_v
;
3174 /* Polynomials with more than 2 variables are not handled yet. When
3175 the evolution steps are parameters, it is not possible to
3176 represent the dependence using classical distance vectors. */
3177 if (TREE_CODE (c_0
) != INTEGER_CST
3178 || TREE_CODE (CHREC_RIGHT (c_1
)) != INTEGER_CST
3179 || TREE_CODE (CHREC_RIGHT (c_2
)) != INTEGER_CST
)
3181 DDR_AFFINE_P (ddr
) = false;
3185 x_2
= index_in_loop_nest (CHREC_VARIABLE (c_2
), DDR_LOOP_NEST (ddr
));
3186 x_1
= index_in_loop_nest (CHREC_VARIABLE (c_1
), DDR_LOOP_NEST (ddr
));
3188 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3189 dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3190 v1
= int_cst_value (CHREC_RIGHT (c_1
));
3191 v2
= int_cst_value (CHREC_RIGHT (c_2
));
3204 save_dist_v (ddr
, dist_v
);
3206 add_outer_distances (ddr
, dist_v
, x_1
);
3209 /* Helper function for the case where DDR_A and DDR_B are the same
3210 access functions. */
3213 add_other_self_distances (struct data_dependence_relation
*ddr
)
3215 lambda_vector dist_v
;
3217 int index_carry
= DDR_NB_LOOPS (ddr
);
3219 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3221 tree access_fun
= DR_ACCESS_FN (DDR_A (ddr
), i
);
3223 if (TREE_CODE (access_fun
) == POLYNOMIAL_CHREC
)
3225 if (!evolution_function_is_univariate_p (access_fun
))
3227 if (DDR_NUM_SUBSCRIPTS (ddr
) != 1)
3229 DDR_ARE_DEPENDENT (ddr
) = chrec_dont_know
;
3233 access_fun
= DR_ACCESS_FN (DDR_A (ddr
), 0);
3235 if (TREE_CODE (CHREC_LEFT (access_fun
)) == POLYNOMIAL_CHREC
)
3236 add_multivariate_self_dist (ddr
, access_fun
);
3238 /* The evolution step is not constant: it varies in
3239 the outer loop, so this cannot be represented by a
3240 distance vector. For example in pr34635.c the
3241 evolution is {0, +, {0, +, 4}_1}_2. */
3242 DDR_AFFINE_P (ddr
) = false;
3247 index_carry
= MIN (index_carry
,
3248 index_in_loop_nest (CHREC_VARIABLE (access_fun
),
3249 DDR_LOOP_NEST (ddr
)));
3253 dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3254 add_outer_distances (ddr
, dist_v
, index_carry
);
3258 insert_innermost_unit_dist_vector (struct data_dependence_relation
*ddr
)
3260 lambda_vector dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3262 dist_v
[DDR_INNER_LOOP (ddr
)] = 1;
3263 save_dist_v (ddr
, dist_v
);
3266 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
3267 is the case for example when access functions are the same and
3268 equal to a constant, as in:
3275 in which case the distance vectors are (0) and (1). */
3278 add_distance_for_zero_overlaps (struct data_dependence_relation
*ddr
)
3282 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3284 subscript_p sub
= DDR_SUBSCRIPT (ddr
, i
);
3285 conflict_function
*ca
= SUB_CONFLICTS_IN_A (sub
);
3286 conflict_function
*cb
= SUB_CONFLICTS_IN_B (sub
);
3288 for (j
= 0; j
< ca
->n
; j
++)
3289 if (affine_function_zero_p (ca
->fns
[j
]))
3291 insert_innermost_unit_dist_vector (ddr
);
3295 for (j
= 0; j
< cb
->n
; j
++)
3296 if (affine_function_zero_p (cb
->fns
[j
]))
3298 insert_innermost_unit_dist_vector (ddr
);
3304 /* Compute the classic per loop distance vector. DDR is the data
3305 dependence relation to build a vector from. Return false when fail
3306 to represent the data dependence as a distance vector. */
3309 build_classic_dist_vector (struct data_dependence_relation
*ddr
,
3310 struct loop
*loop_nest
)
3312 bool init_b
= false;
3313 int index_carry
= DDR_NB_LOOPS (ddr
);
3314 lambda_vector dist_v
;
3316 if (DDR_ARE_DEPENDENT (ddr
) != NULL_TREE
)
3319 if (same_access_functions (ddr
))
3321 /* Save the 0 vector. */
3322 dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3323 save_dist_v (ddr
, dist_v
);
3325 if (constant_access_functions (ddr
))
3326 add_distance_for_zero_overlaps (ddr
);
3328 if (DDR_NB_LOOPS (ddr
) > 1)
3329 add_other_self_distances (ddr
);
3334 dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3335 if (!build_classic_dist_vector_1 (ddr
, DDR_A (ddr
), DDR_B (ddr
),
3336 dist_v
, &init_b
, &index_carry
))
3339 /* Save the distance vector if we initialized one. */
3342 /* Verify a basic constraint: classic distance vectors should
3343 always be lexicographically positive.
3345 Data references are collected in the order of execution of
3346 the program, thus for the following loop
3348 | for (i = 1; i < 100; i++)
3349 | for (j = 1; j < 100; j++)
3351 | t = T[j+1][i-1]; // A
3352 | T[j][i] = t + 2; // B
3355 references are collected following the direction of the wind:
3356 A then B. The data dependence tests are performed also
3357 following this order, such that we're looking at the distance
3358 separating the elements accessed by A from the elements later
3359 accessed by B. But in this example, the distance returned by
3360 test_dep (A, B) is lexicographically negative (-1, 1), that
3361 means that the access A occurs later than B with respect to
3362 the outer loop, ie. we're actually looking upwind. In this
3363 case we solve test_dep (B, A) looking downwind to the
3364 lexicographically positive solution, that returns the
3365 distance vector (1, -1). */
3366 if (!lambda_vector_lexico_pos (dist_v
, DDR_NB_LOOPS (ddr
)))
3368 lambda_vector save_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3369 if (!subscript_dependence_tester_1 (ddr
, DDR_B (ddr
), DDR_A (ddr
),
3372 compute_subscript_distance (ddr
);
3373 if (!build_classic_dist_vector_1 (ddr
, DDR_B (ddr
), DDR_A (ddr
),
3374 save_v
, &init_b
, &index_carry
))
3376 save_dist_v (ddr
, save_v
);
3377 DDR_REVERSED_P (ddr
) = true;
3379 /* In this case there is a dependence forward for all the
3382 | for (k = 1; k < 100; k++)
3383 | for (i = 1; i < 100; i++)
3384 | for (j = 1; j < 100; j++)
3386 | t = T[j+1][i-1]; // A
3387 | T[j][i] = t + 2; // B
3395 if (DDR_NB_LOOPS (ddr
) > 1)
3397 add_outer_distances (ddr
, save_v
, index_carry
);
3398 add_outer_distances (ddr
, dist_v
, index_carry
);
3403 lambda_vector save_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3404 lambda_vector_copy (dist_v
, save_v
, DDR_NB_LOOPS (ddr
));
3406 if (DDR_NB_LOOPS (ddr
) > 1)
3408 lambda_vector opposite_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3410 if (!subscript_dependence_tester_1 (ddr
, DDR_B (ddr
),
3411 DDR_A (ddr
), loop_nest
))
3413 compute_subscript_distance (ddr
);
3414 if (!build_classic_dist_vector_1 (ddr
, DDR_B (ddr
), DDR_A (ddr
),
3415 opposite_v
, &init_b
,
3419 save_dist_v (ddr
, save_v
);
3420 add_outer_distances (ddr
, dist_v
, index_carry
);
3421 add_outer_distances (ddr
, opposite_v
, index_carry
);
3424 save_dist_v (ddr
, save_v
);
3429 /* There is a distance of 1 on all the outer loops: Example:
3430 there is a dependence of distance 1 on loop_1 for the array A.
3436 add_outer_distances (ddr
, dist_v
,
3437 lambda_vector_first_nz (dist_v
,
3438 DDR_NB_LOOPS (ddr
), 0));
3441 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3445 fprintf (dump_file
, "(build_classic_dist_vector\n");
3446 for (i
= 0; i
< DDR_NUM_DIST_VECTS (ddr
); i
++)
3448 fprintf (dump_file
, " dist_vector = (");
3449 print_lambda_vector (dump_file
, DDR_DIST_VECT (ddr
, i
),
3450 DDR_NB_LOOPS (ddr
));
3451 fprintf (dump_file
, " )\n");
3453 fprintf (dump_file
, ")\n");
3459 /* Return the direction for a given distance.
3460 FIXME: Computing dir this way is suboptimal, since dir can catch
3461 cases that dist is unable to represent. */
3463 static inline enum data_dependence_direction
3464 dir_from_dist (int dist
)
3467 return dir_positive
;
3469 return dir_negative
;
3474 /* Compute the classic per loop direction vector. DDR is the data
3475 dependence relation to build a vector from. */
3478 build_classic_dir_vector (struct data_dependence_relation
*ddr
)
3481 lambda_vector dist_v
;
3483 FOR_EACH_VEC_ELT (lambda_vector
, DDR_DIST_VECTS (ddr
), i
, dist_v
)
3485 lambda_vector dir_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3487 for (j
= 0; j
< DDR_NB_LOOPS (ddr
); j
++)
3488 dir_v
[j
] = dir_from_dist (dist_v
[j
]);
3490 save_dir_v (ddr
, dir_v
);
3494 /* Helper function. Returns true when there is a dependence between
3495 data references DRA and DRB. */
3498 subscript_dependence_tester_1 (struct data_dependence_relation
*ddr
,
3499 struct data_reference
*dra
,
3500 struct data_reference
*drb
,
3501 struct loop
*loop_nest
)
3504 tree last_conflicts
;
3505 struct subscript
*subscript
;
3506 tree res
= NULL_TREE
;
3508 for (i
= 0; VEC_iterate (subscript_p
, DDR_SUBSCRIPTS (ddr
), i
, subscript
);
3511 conflict_function
*overlaps_a
, *overlaps_b
;
3513 analyze_overlapping_iterations (DR_ACCESS_FN (dra
, i
),
3514 DR_ACCESS_FN (drb
, i
),
3515 &overlaps_a
, &overlaps_b
,
3516 &last_conflicts
, loop_nest
);
3518 if (SUB_CONFLICTS_IN_A (subscript
))
3519 free_conflict_function (SUB_CONFLICTS_IN_A (subscript
));
3520 if (SUB_CONFLICTS_IN_B (subscript
))
3521 free_conflict_function (SUB_CONFLICTS_IN_B (subscript
));
3523 SUB_CONFLICTS_IN_A (subscript
) = overlaps_a
;
3524 SUB_CONFLICTS_IN_B (subscript
) = overlaps_b
;
3525 SUB_LAST_CONFLICT (subscript
) = last_conflicts
;
3527 /* If there is any undetermined conflict function we have to
3528 give a conservative answer in case we cannot prove that
3529 no dependence exists when analyzing another subscript. */
3530 if (CF_NOT_KNOWN_P (overlaps_a
)
3531 || CF_NOT_KNOWN_P (overlaps_b
))
3533 res
= chrec_dont_know
;
3537 /* When there is a subscript with no dependence we can stop. */
3538 else if (CF_NO_DEPENDENCE_P (overlaps_a
)
3539 || CF_NO_DEPENDENCE_P (overlaps_b
))
3546 if (res
== NULL_TREE
)
3549 if (res
== chrec_known
)
3550 dependence_stats
.num_dependence_independent
++;
3552 dependence_stats
.num_dependence_undetermined
++;
3553 finalize_ddr_dependent (ddr
, res
);
3557 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
3560 subscript_dependence_tester (struct data_dependence_relation
*ddr
,
3561 struct loop
*loop_nest
)
3564 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3565 fprintf (dump_file
, "(subscript_dependence_tester \n");
3567 if (subscript_dependence_tester_1 (ddr
, DDR_A (ddr
), DDR_B (ddr
), loop_nest
))
3568 dependence_stats
.num_dependence_dependent
++;
3570 compute_subscript_distance (ddr
);
3571 if (build_classic_dist_vector (ddr
, loop_nest
))
3572 build_classic_dir_vector (ddr
);
3574 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3575 fprintf (dump_file
, ")\n");
3578 /* Returns true when all the access functions of A are affine or
3579 constant with respect to LOOP_NEST. */
3582 access_functions_are_affine_or_constant_p (const struct data_reference
*a
,
3583 const struct loop
*loop_nest
)
3586 VEC(tree
,heap
) *fns
= DR_ACCESS_FNS (a
);
3589 FOR_EACH_VEC_ELT (tree
, fns
, i
, t
)
3590 if (!evolution_function_is_invariant_p (t
, loop_nest
->num
)
3591 && !evolution_function_is_affine_multivariate_p (t
, loop_nest
->num
))
3597 /* Initializes an equation for an OMEGA problem using the information
3598 contained in the ACCESS_FUN. Returns true when the operation
3601 PB is the omega constraint system.
3602 EQ is the number of the equation to be initialized.
3603 OFFSET is used for shifting the variables names in the constraints:
3604 a constrain is composed of 2 * the number of variables surrounding
3605 dependence accesses. OFFSET is set either to 0 for the first n variables,
3606 then it is set to n.
3607 ACCESS_FUN is expected to be an affine chrec. */
3610 init_omega_eq_with_af (omega_pb pb
, unsigned eq
,
3611 unsigned int offset
, tree access_fun
,
3612 struct data_dependence_relation
*ddr
)
3614 switch (TREE_CODE (access_fun
))
3616 case POLYNOMIAL_CHREC
:
3618 tree left
= CHREC_LEFT (access_fun
);
3619 tree right
= CHREC_RIGHT (access_fun
);
3620 int var
= CHREC_VARIABLE (access_fun
);
3623 if (TREE_CODE (right
) != INTEGER_CST
)
3626 var_idx
= index_in_loop_nest (var
, DDR_LOOP_NEST (ddr
));
3627 pb
->eqs
[eq
].coef
[offset
+ var_idx
+ 1] = int_cst_value (right
);
3629 /* Compute the innermost loop index. */
3630 DDR_INNER_LOOP (ddr
) = MAX (DDR_INNER_LOOP (ddr
), var_idx
);
3633 pb
->eqs
[eq
].coef
[var_idx
+ DDR_NB_LOOPS (ddr
) + 1]
3634 += int_cst_value (right
);
3636 switch (TREE_CODE (left
))
3638 case POLYNOMIAL_CHREC
:
3639 return init_omega_eq_with_af (pb
, eq
, offset
, left
, ddr
);
3642 pb
->eqs
[eq
].coef
[0] += int_cst_value (left
);
3651 pb
->eqs
[eq
].coef
[0] += int_cst_value (access_fun
);
3659 /* As explained in the comments preceding init_omega_for_ddr, we have
3660 to set up a system for each loop level, setting outer loops
3661 variation to zero, and current loop variation to positive or zero.
3662 Save each lexico positive distance vector. */
3665 omega_extract_distance_vectors (omega_pb pb
,
3666 struct data_dependence_relation
*ddr
)
3670 struct loop
*loopi
, *loopj
;
3671 enum omega_result res
;
3673 /* Set a new problem for each loop in the nest. The basis is the
3674 problem that we have initialized until now. On top of this we
3675 add new constraints. */
3676 for (i
= 0; i
<= DDR_INNER_LOOP (ddr
)
3677 && VEC_iterate (loop_p
, DDR_LOOP_NEST (ddr
), i
, loopi
); i
++)
3680 omega_pb copy
= omega_alloc_problem (2 * DDR_NB_LOOPS (ddr
),
3681 DDR_NB_LOOPS (ddr
));
3683 omega_copy_problem (copy
, pb
);
3685 /* For all the outer loops "loop_j", add "dj = 0". */
3687 j
< i
&& VEC_iterate (loop_p
, DDR_LOOP_NEST (ddr
), j
, loopj
); j
++)
3689 eq
= omega_add_zero_eq (copy
, omega_black
);
3690 copy
->eqs
[eq
].coef
[j
+ 1] = 1;
3693 /* For "loop_i", add "0 <= di". */
3694 geq
= omega_add_zero_geq (copy
, omega_black
);
3695 copy
->geqs
[geq
].coef
[i
+ 1] = 1;
3697 /* Reduce the constraint system, and test that the current
3698 problem is feasible. */
3699 res
= omega_simplify_problem (copy
);
3700 if (res
== omega_false
3701 || res
== omega_unknown
3702 || copy
->num_geqs
> (int) DDR_NB_LOOPS (ddr
))
3705 for (eq
= 0; eq
< copy
->num_subs
; eq
++)
3706 if (copy
->subs
[eq
].key
== (int) i
+ 1)
3708 dist
= copy
->subs
[eq
].coef
[0];
3714 /* Reinitialize problem... */
3715 omega_copy_problem (copy
, pb
);
3717 j
< i
&& VEC_iterate (loop_p
, DDR_LOOP_NEST (ddr
), j
, loopj
); j
++)
3719 eq
= omega_add_zero_eq (copy
, omega_black
);
3720 copy
->eqs
[eq
].coef
[j
+ 1] = 1;
3723 /* ..., but this time "di = 1". */
3724 eq
= omega_add_zero_eq (copy
, omega_black
);
3725 copy
->eqs
[eq
].coef
[i
+ 1] = 1;
3726 copy
->eqs
[eq
].coef
[0] = -1;
3728 res
= omega_simplify_problem (copy
);
3729 if (res
== omega_false
3730 || res
== omega_unknown
3731 || copy
->num_geqs
> (int) DDR_NB_LOOPS (ddr
))
3734 for (eq
= 0; eq
< copy
->num_subs
; eq
++)
3735 if (copy
->subs
[eq
].key
== (int) i
+ 1)
3737 dist
= copy
->subs
[eq
].coef
[0];
3743 /* Save the lexicographically positive distance vector. */
3746 lambda_vector dist_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3747 lambda_vector dir_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3751 for (eq
= 0; eq
< copy
->num_subs
; eq
++)
3752 if (copy
->subs
[eq
].key
> 0)
3754 dist
= copy
->subs
[eq
].coef
[0];
3755 dist_v
[copy
->subs
[eq
].key
- 1] = dist
;
3758 for (j
= 0; j
< DDR_NB_LOOPS (ddr
); j
++)
3759 dir_v
[j
] = dir_from_dist (dist_v
[j
]);
3761 save_dist_v (ddr
, dist_v
);
3762 save_dir_v (ddr
, dir_v
);
3766 omega_free_problem (copy
);
3770 /* This is called for each subscript of a tuple of data references:
3771 insert an equality for representing the conflicts. */
3774 omega_setup_subscript (tree access_fun_a
, tree access_fun_b
,
3775 struct data_dependence_relation
*ddr
,
3776 omega_pb pb
, bool *maybe_dependent
)
3779 tree type
= signed_type_for_types (TREE_TYPE (access_fun_a
),
3780 TREE_TYPE (access_fun_b
));
3781 tree fun_a
= chrec_convert (type
, access_fun_a
, NULL
);
3782 tree fun_b
= chrec_convert (type
, access_fun_b
, NULL
);
3783 tree difference
= chrec_fold_minus (type
, fun_a
, fun_b
);
3786 /* When the fun_a - fun_b is not constant, the dependence is not
3787 captured by the classic distance vector representation. */
3788 if (TREE_CODE (difference
) != INTEGER_CST
)
3792 if (ziv_subscript_p (fun_a
, fun_b
) && !integer_zerop (difference
))
3794 /* There is no dependence. */
3795 *maybe_dependent
= false;
3799 minus_one
= build_int_cst (type
, -1);
3800 fun_b
= chrec_fold_multiply (type
, fun_b
, minus_one
);
3802 eq
= omega_add_zero_eq (pb
, omega_black
);
3803 if (!init_omega_eq_with_af (pb
, eq
, DDR_NB_LOOPS (ddr
), fun_a
, ddr
)
3804 || !init_omega_eq_with_af (pb
, eq
, 0, fun_b
, ddr
))
3805 /* There is probably a dependence, but the system of
3806 constraints cannot be built: answer "don't know". */
3810 if (DDR_NB_LOOPS (ddr
) != 0 && pb
->eqs
[eq
].coef
[0]
3811 && !int_divides_p (lambda_vector_gcd
3812 ((lambda_vector
) &(pb
->eqs
[eq
].coef
[1]),
3813 2 * DDR_NB_LOOPS (ddr
)),
3814 pb
->eqs
[eq
].coef
[0]))
3816 /* There is no dependence. */
3817 *maybe_dependent
= false;
3824 /* Helper function, same as init_omega_for_ddr but specialized for
3825 data references A and B. */
3828 init_omega_for_ddr_1 (struct data_reference
*dra
, struct data_reference
*drb
,
3829 struct data_dependence_relation
*ddr
,
3830 omega_pb pb
, bool *maybe_dependent
)
3835 unsigned nb_loops
= DDR_NB_LOOPS (ddr
);
3837 /* Insert an equality per subscript. */
3838 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3840 if (!omega_setup_subscript (DR_ACCESS_FN (dra
, i
), DR_ACCESS_FN (drb
, i
),
3841 ddr
, pb
, maybe_dependent
))
3843 else if (*maybe_dependent
== false)
3845 /* There is no dependence. */
3846 DDR_ARE_DEPENDENT (ddr
) = chrec_known
;
3851 /* Insert inequalities: constraints corresponding to the iteration
3852 domain, i.e. the loops surrounding the references "loop_x" and
3853 the distance variables "dx". The layout of the OMEGA
3854 representation is as follows:
3855 - coef[0] is the constant
3856 - coef[1..nb_loops] are the protected variables that will not be
3857 removed by the solver: the "dx"
3858 - coef[nb_loops + 1, 2*nb_loops] are the loop variables: "loop_x".
3860 for (i
= 0; i
<= DDR_INNER_LOOP (ddr
)
3861 && VEC_iterate (loop_p
, DDR_LOOP_NEST (ddr
), i
, loopi
); i
++)
3863 HOST_WIDE_INT nbi
= max_stmt_executions_int (loopi
);
3866 ineq
= omega_add_zero_geq (pb
, omega_black
);
3867 pb
->geqs
[ineq
].coef
[i
+ nb_loops
+ 1] = 1;
3869 /* 0 <= loop_x + dx */
3870 ineq
= omega_add_zero_geq (pb
, omega_black
);
3871 pb
->geqs
[ineq
].coef
[i
+ nb_loops
+ 1] = 1;
3872 pb
->geqs
[ineq
].coef
[i
+ 1] = 1;
3876 /* loop_x <= nb_iters */
3877 ineq
= omega_add_zero_geq (pb
, omega_black
);
3878 pb
->geqs
[ineq
].coef
[i
+ nb_loops
+ 1] = -1;
3879 pb
->geqs
[ineq
].coef
[0] = nbi
;
3881 /* loop_x + dx <= nb_iters */
3882 ineq
= omega_add_zero_geq (pb
, omega_black
);
3883 pb
->geqs
[ineq
].coef
[i
+ nb_loops
+ 1] = -1;
3884 pb
->geqs
[ineq
].coef
[i
+ 1] = -1;
3885 pb
->geqs
[ineq
].coef
[0] = nbi
;
3887 /* A step "dx" bigger than nb_iters is not feasible, so
3888 add "0 <= nb_iters + dx", */
3889 ineq
= omega_add_zero_geq (pb
, omega_black
);
3890 pb
->geqs
[ineq
].coef
[i
+ 1] = 1;
3891 pb
->geqs
[ineq
].coef
[0] = nbi
;
3892 /* and "dx <= nb_iters". */
3893 ineq
= omega_add_zero_geq (pb
, omega_black
);
3894 pb
->geqs
[ineq
].coef
[i
+ 1] = -1;
3895 pb
->geqs
[ineq
].coef
[0] = nbi
;
3899 omega_extract_distance_vectors (pb
, ddr
);
3904 /* Sets up the Omega dependence problem for the data dependence
3905 relation DDR. Returns false when the constraint system cannot be
3906 built, ie. when the test answers "don't know". Returns true
3907 otherwise, and when independence has been proved (using one of the
3908 trivial dependence test), set MAYBE_DEPENDENT to false, otherwise
3909 set MAYBE_DEPENDENT to true.
3911 Example: for setting up the dependence system corresponding to the
3912 conflicting accesses
3917 | ... A[2*j, 2*(i + j)]
3921 the following constraints come from the iteration domain:
3928 where di, dj are the distance variables. The constraints
3929 representing the conflicting elements are:
3932 i + 1 = 2 * (i + di + j + dj)
3934 For asking that the resulting distance vector (di, dj) be
3935 lexicographically positive, we insert the constraint "di >= 0". If
3936 "di = 0" in the solution, we fix that component to zero, and we
3937 look at the inner loops: we set a new problem where all the outer
3938 loop distances are zero, and fix this inner component to be
3939 positive. When one of the components is positive, we save that
3940 distance, and set a new problem where the distance on this loop is
3941 zero, searching for other distances in the inner loops. Here is
3942 the classic example that illustrates that we have to set for each
3943 inner loop a new problem:
3951 we have to save two distances (1, 0) and (0, 1).
3953 Given two array references, refA and refB, we have to set the
3954 dependence problem twice, refA vs. refB and refB vs. refA, and we
3955 cannot do a single test, as refB might occur before refA in the
3956 inner loops, and the contrary when considering outer loops: ex.
3961 | T[{1,+,1}_2][{1,+,1}_1] // refA
3962 | T[{2,+,1}_2][{0,+,1}_1] // refB
3967 refB touches the elements in T before refA, and thus for the same
3968 loop_0 refB precedes refA: ie. the distance vector (0, 1, -1)
3969 but for successive loop_0 iterations, we have (1, -1, 1)
3971 The Omega solver expects the distance variables ("di" in the
3972 previous example) to come first in the constraint system (as
3973 variables to be protected, or "safe" variables), the constraint
3974 system is built using the following layout:
3976 "cst | distance vars | index vars".
3980 init_omega_for_ddr (struct data_dependence_relation
*ddr
,
3981 bool *maybe_dependent
)
3986 *maybe_dependent
= true;
3988 if (same_access_functions (ddr
))
3991 lambda_vector dir_v
;
3993 /* Save the 0 vector. */
3994 save_dist_v (ddr
, lambda_vector_new (DDR_NB_LOOPS (ddr
)));
3995 dir_v
= lambda_vector_new (DDR_NB_LOOPS (ddr
));
3996 for (j
= 0; j
< DDR_NB_LOOPS (ddr
); j
++)
3997 dir_v
[j
] = dir_equal
;
3998 save_dir_v (ddr
, dir_v
);
4000 /* Save the dependences carried by outer loops. */
4001 pb
= omega_alloc_problem (2 * DDR_NB_LOOPS (ddr
), DDR_NB_LOOPS (ddr
));
4002 res
= init_omega_for_ddr_1 (DDR_A (ddr
), DDR_B (ddr
), ddr
, pb
,
4004 omega_free_problem (pb
);
4008 /* Omega expects the protected variables (those that have to be kept
4009 after elimination) to appear first in the constraint system.
4010 These variables are the distance variables. In the following
4011 initialization we declare NB_LOOPS safe variables, and the total
4012 number of variables for the constraint system is 2*NB_LOOPS. */
4013 pb
= omega_alloc_problem (2 * DDR_NB_LOOPS (ddr
), DDR_NB_LOOPS (ddr
));
4014 res
= init_omega_for_ddr_1 (DDR_A (ddr
), DDR_B (ddr
), ddr
, pb
,
4016 omega_free_problem (pb
);
4018 /* Stop computation if not decidable, or no dependence. */
4019 if (res
== false || *maybe_dependent
== false)
4022 pb
= omega_alloc_problem (2 * DDR_NB_LOOPS (ddr
), DDR_NB_LOOPS (ddr
));
4023 res
= init_omega_for_ddr_1 (DDR_B (ddr
), DDR_A (ddr
), ddr
, pb
,
4025 omega_free_problem (pb
);
4030 /* Return true when DDR contains the same information as that stored
4031 in DIR_VECTS and in DIST_VECTS, return false otherwise. */
4034 ddr_consistent_p (FILE *file
,
4035 struct data_dependence_relation
*ddr
,
4036 VEC (lambda_vector
, heap
) *dist_vects
,
4037 VEC (lambda_vector
, heap
) *dir_vects
)
4041 /* If dump_file is set, output there. */
4042 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4045 if (VEC_length (lambda_vector
, dist_vects
) != DDR_NUM_DIST_VECTS (ddr
))
4047 lambda_vector b_dist_v
;
4048 fprintf (file
, "\n(Number of distance vectors differ: Banerjee has %d, Omega has %d.\n",
4049 VEC_length (lambda_vector
, dist_vects
),
4050 DDR_NUM_DIST_VECTS (ddr
));
4052 fprintf (file
, "Banerjee dist vectors:\n");
4053 FOR_EACH_VEC_ELT (lambda_vector
, dist_vects
, i
, b_dist_v
)
4054 print_lambda_vector (file
, b_dist_v
, DDR_NB_LOOPS (ddr
));
4056 fprintf (file
, "Omega dist vectors:\n");
4057 for (i
= 0; i
< DDR_NUM_DIST_VECTS (ddr
); i
++)
4058 print_lambda_vector (file
, DDR_DIST_VECT (ddr
, i
), DDR_NB_LOOPS (ddr
));
4060 fprintf (file
, "data dependence relation:\n");
4061 dump_data_dependence_relation (file
, ddr
);
4063 fprintf (file
, ")\n");
4067 if (VEC_length (lambda_vector
, dir_vects
) != DDR_NUM_DIR_VECTS (ddr
))
4069 fprintf (file
, "\n(Number of direction vectors differ: Banerjee has %d, Omega has %d.)\n",
4070 VEC_length (lambda_vector
, dir_vects
),
4071 DDR_NUM_DIR_VECTS (ddr
));
4075 for (i
= 0; i
< DDR_NUM_DIST_VECTS (ddr
); i
++)
4077 lambda_vector a_dist_v
;
4078 lambda_vector b_dist_v
= DDR_DIST_VECT (ddr
, i
);
4080 /* Distance vectors are not ordered in the same way in the DDR
4081 and in the DIST_VECTS: search for a matching vector. */
4082 FOR_EACH_VEC_ELT (lambda_vector
, dist_vects
, j
, a_dist_v
)
4083 if (lambda_vector_equal (a_dist_v
, b_dist_v
, DDR_NB_LOOPS (ddr
)))
4086 if (j
== VEC_length (lambda_vector
, dist_vects
))
4088 fprintf (file
, "\n(Dist vectors from the first dependence analyzer:\n");
4089 print_dist_vectors (file
, dist_vects
, DDR_NB_LOOPS (ddr
));
4090 fprintf (file
, "not found in Omega dist vectors:\n");
4091 print_dist_vectors (file
, DDR_DIST_VECTS (ddr
), DDR_NB_LOOPS (ddr
));
4092 fprintf (file
, "data dependence relation:\n");
4093 dump_data_dependence_relation (file
, ddr
);
4094 fprintf (file
, ")\n");
4098 for (i
= 0; i
< DDR_NUM_DIR_VECTS (ddr
); i
++)
4100 lambda_vector a_dir_v
;
4101 lambda_vector b_dir_v
= DDR_DIR_VECT (ddr
, i
);
4103 /* Direction vectors are not ordered in the same way in the DDR
4104 and in the DIR_VECTS: search for a matching vector. */
4105 FOR_EACH_VEC_ELT (lambda_vector
, dir_vects
, j
, a_dir_v
)
4106 if (lambda_vector_equal (a_dir_v
, b_dir_v
, DDR_NB_LOOPS (ddr
)))
4109 if (j
== VEC_length (lambda_vector
, dist_vects
))
4111 fprintf (file
, "\n(Dir vectors from the first dependence analyzer:\n");
4112 print_dir_vectors (file
, dir_vects
, DDR_NB_LOOPS (ddr
));
4113 fprintf (file
, "not found in Omega dir vectors:\n");
4114 print_dir_vectors (file
, DDR_DIR_VECTS (ddr
), DDR_NB_LOOPS (ddr
));
4115 fprintf (file
, "data dependence relation:\n");
4116 dump_data_dependence_relation (file
, ddr
);
4117 fprintf (file
, ")\n");
4124 /* This computes the affine dependence relation between A and B with
4125 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
4126 independence between two accesses, while CHREC_DONT_KNOW is used
4127 for representing the unknown relation.
4129 Note that it is possible to stop the computation of the dependence
4130 relation the first time we detect a CHREC_KNOWN element for a given
4134 compute_affine_dependence (struct data_dependence_relation
*ddr
,
4135 struct loop
*loop_nest
)
4137 struct data_reference
*dra
= DDR_A (ddr
);
4138 struct data_reference
*drb
= DDR_B (ddr
);
4140 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4142 fprintf (dump_file
, "(compute_affine_dependence\n");
4143 fprintf (dump_file
, " stmt_a: ");
4144 print_gimple_stmt (dump_file
, DR_STMT (dra
), 0, TDF_SLIM
);
4145 fprintf (dump_file
, " stmt_b: ");
4146 print_gimple_stmt (dump_file
, DR_STMT (drb
), 0, TDF_SLIM
);
4149 /* Analyze only when the dependence relation is not yet known. */
4150 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
4152 dependence_stats
.num_dependence_tests
++;
4154 if (access_functions_are_affine_or_constant_p (dra
, loop_nest
)
4155 && access_functions_are_affine_or_constant_p (drb
, loop_nest
))
4157 if (flag_check_data_deps
)
4159 /* Compute the dependences using the first algorithm. */
4160 subscript_dependence_tester (ddr
, loop_nest
);
4162 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4164 fprintf (dump_file
, "\n\nBanerjee Analyzer\n");
4165 dump_data_dependence_relation (dump_file
, ddr
);
4168 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
4170 bool maybe_dependent
;
4171 VEC (lambda_vector
, heap
) *dir_vects
, *dist_vects
;
4173 /* Save the result of the first DD analyzer. */
4174 dist_vects
= DDR_DIST_VECTS (ddr
);
4175 dir_vects
= DDR_DIR_VECTS (ddr
);
4177 /* Reset the information. */
4178 DDR_DIST_VECTS (ddr
) = NULL
;
4179 DDR_DIR_VECTS (ddr
) = NULL
;
4181 /* Compute the same information using Omega. */
4182 if (!init_omega_for_ddr (ddr
, &maybe_dependent
))
4183 goto csys_dont_know
;
4185 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4187 fprintf (dump_file
, "Omega Analyzer\n");
4188 dump_data_dependence_relation (dump_file
, ddr
);
4191 /* Check that we get the same information. */
4192 if (maybe_dependent
)
4193 gcc_assert (ddr_consistent_p (stderr
, ddr
, dist_vects
,
4198 subscript_dependence_tester (ddr
, loop_nest
);
4201 /* As a last case, if the dependence cannot be determined, or if
4202 the dependence is considered too difficult to determine, answer
4207 dependence_stats
.num_dependence_undetermined
++;
4209 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4211 fprintf (dump_file
, "Data ref a:\n");
4212 dump_data_reference (dump_file
, dra
);
4213 fprintf (dump_file
, "Data ref b:\n");
4214 dump_data_reference (dump_file
, drb
);
4215 fprintf (dump_file
, "affine dependence test not usable: access function not affine or constant.\n");
4217 finalize_ddr_dependent (ddr
, chrec_dont_know
);
4221 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4223 if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
4224 fprintf (dump_file
, ") -> no dependence\n");
4225 else if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
4226 fprintf (dump_file
, ") -> dependence analysis failed\n");
4228 fprintf (dump_file
, ")\n");
4232 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
4233 the data references in DATAREFS, in the LOOP_NEST. When
4234 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
4235 relations. Return true when successful, i.e. data references number
4236 is small enough to be handled. */
4239 compute_all_dependences (VEC (data_reference_p
, heap
) *datarefs
,
4240 VEC (ddr_p
, heap
) **dependence_relations
,
4241 VEC (loop_p
, heap
) *loop_nest
,
4242 bool compute_self_and_rr
)
4244 struct data_dependence_relation
*ddr
;
4245 struct data_reference
*a
, *b
;
4248 if ((int) VEC_length (data_reference_p
, datarefs
)
4249 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS
))
4251 struct data_dependence_relation
*ddr
;
4253 /* Insert a single relation into dependence_relations:
4255 ddr
= initialize_data_dependence_relation (NULL
, NULL
, loop_nest
);
4256 VEC_safe_push (ddr_p
, heap
, *dependence_relations
, ddr
);
4260 FOR_EACH_VEC_ELT (data_reference_p
, datarefs
, i
, a
)
4261 for (j
= i
+ 1; VEC_iterate (data_reference_p
, datarefs
, j
, b
); j
++)
4262 if (DR_IS_WRITE (a
) || DR_IS_WRITE (b
) || compute_self_and_rr
)
4264 ddr
= initialize_data_dependence_relation (a
, b
, loop_nest
);
4265 VEC_safe_push (ddr_p
, heap
, *dependence_relations
, ddr
);
4267 compute_affine_dependence (ddr
, VEC_index (loop_p
, loop_nest
, 0));
4270 if (compute_self_and_rr
)
4271 FOR_EACH_VEC_ELT (data_reference_p
, datarefs
, i
, a
)
4273 ddr
= initialize_data_dependence_relation (a
, a
, loop_nest
);
4274 VEC_safe_push (ddr_p
, heap
, *dependence_relations
, ddr
);
4276 compute_affine_dependence (ddr
, VEC_index (loop_p
, loop_nest
, 0));
4282 /* Describes a location of a memory reference. */
4284 typedef struct data_ref_loc_d
4286 /* Position of the memory reference. */
4289 /* True if the memory reference is read. */
4293 DEF_VEC_O (data_ref_loc
);
4294 DEF_VEC_ALLOC_O (data_ref_loc
, heap
);
4296 /* Stores the locations of memory references in STMT to REFERENCES. Returns
4297 true if STMT clobbers memory, false otherwise. */
4300 get_references_in_stmt (gimple stmt
, VEC (data_ref_loc
, heap
) **references
)
4302 bool clobbers_memory
= false;
4305 enum gimple_code stmt_code
= gimple_code (stmt
);
4309 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4310 Calls have side-effects, except those to const or pure
4312 if ((stmt_code
== GIMPLE_CALL
4313 && !(gimple_call_flags (stmt
) & (ECF_CONST
| ECF_PURE
)))
4314 || (stmt_code
== GIMPLE_ASM
4315 && (gimple_asm_volatile_p (stmt
) || gimple_vuse (stmt
))))
4316 clobbers_memory
= true;
4318 if (!gimple_vuse (stmt
))
4319 return clobbers_memory
;
4321 if (stmt_code
== GIMPLE_ASSIGN
)
4324 op0
= gimple_assign_lhs_ptr (stmt
);
4325 op1
= gimple_assign_rhs1_ptr (stmt
);
4328 || (REFERENCE_CLASS_P (*op1
)
4329 && (base
= get_base_address (*op1
))
4330 && TREE_CODE (base
) != SSA_NAME
))
4334 VEC_safe_push (data_ref_loc
, heap
, *references
, ref
);
4337 else if (stmt_code
== GIMPLE_CALL
)
4341 op0
= gimple_call_lhs_ptr (stmt
);
4342 n
= gimple_call_num_args (stmt
);
4343 for (i
= 0; i
< n
; i
++)
4345 op1
= gimple_call_arg_ptr (stmt
, i
);
4348 || (REFERENCE_CLASS_P (*op1
) && get_base_address (*op1
)))
4352 VEC_safe_push (data_ref_loc
, heap
, *references
, ref
);
4357 return clobbers_memory
;
4361 || (REFERENCE_CLASS_P (*op0
) && get_base_address (*op0
))))
4364 ref
.is_read
= false;
4365 VEC_safe_push (data_ref_loc
, heap
, *references
, ref
);
4367 return clobbers_memory
;
4370 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
4371 reference, returns false, otherwise returns true. NEST is the outermost
4372 loop of the loop nest in which the references should be analyzed. */
4375 find_data_references_in_stmt (struct loop
*nest
, gimple stmt
,
4376 VEC (data_reference_p
, heap
) **datarefs
)
4379 VEC (data_ref_loc
, heap
) *references
;
4382 data_reference_p dr
;
4384 if (get_references_in_stmt (stmt
, &references
))
4386 VEC_free (data_ref_loc
, heap
, references
);
4390 FOR_EACH_VEC_ELT (data_ref_loc
, references
, i
, ref
)
4392 dr
= create_data_ref (nest
, loop_containing_stmt (stmt
),
4393 *ref
->pos
, stmt
, ref
->is_read
);
4394 gcc_assert (dr
!= NULL
);
4395 VEC_safe_push (data_reference_p
, heap
, *datarefs
, dr
);
4397 VEC_free (data_ref_loc
, heap
, references
);
4401 /* Stores the data references in STMT to DATAREFS. If there is an
4402 unanalyzable reference, returns false, otherwise returns true.
4403 NEST is the outermost loop of the loop nest in which the references
4404 should be instantiated, LOOP is the loop in which the references
4405 should be analyzed. */
4408 graphite_find_data_references_in_stmt (loop_p nest
, loop_p loop
, gimple stmt
,
4409 VEC (data_reference_p
, heap
) **datarefs
)
4412 VEC (data_ref_loc
, heap
) *references
;
4415 data_reference_p dr
;
4417 if (get_references_in_stmt (stmt
, &references
))
4419 VEC_free (data_ref_loc
, heap
, references
);
4423 FOR_EACH_VEC_ELT (data_ref_loc
, references
, i
, ref
)
4425 dr
= create_data_ref (nest
, loop
, *ref
->pos
, stmt
, ref
->is_read
);
4426 gcc_assert (dr
!= NULL
);
4427 VEC_safe_push (data_reference_p
, heap
, *datarefs
, dr
);
4430 VEC_free (data_ref_loc
, heap
, references
);
4434 /* Search the data references in LOOP, and record the information into
4435 DATAREFS. Returns chrec_dont_know when failing to analyze a
4436 difficult case, returns NULL_TREE otherwise. */
4439 find_data_references_in_bb (struct loop
*loop
, basic_block bb
,
4440 VEC (data_reference_p
, heap
) **datarefs
)
4442 gimple_stmt_iterator bsi
;
4444 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
4446 gimple stmt
= gsi_stmt (bsi
);
4448 if (!find_data_references_in_stmt (loop
, stmt
, datarefs
))
4450 struct data_reference
*res
;
4451 res
= XCNEW (struct data_reference
);
4452 VEC_safe_push (data_reference_p
, heap
, *datarefs
, res
);
4454 return chrec_dont_know
;
4461 /* Search the data references in LOOP, and record the information into
4462 DATAREFS. Returns chrec_dont_know when failing to analyze a
4463 difficult case, returns NULL_TREE otherwise.
4465 TODO: This function should be made smarter so that it can handle address
4466 arithmetic as if they were array accesses, etc. */
4469 find_data_references_in_loop (struct loop
*loop
,
4470 VEC (data_reference_p
, heap
) **datarefs
)
4472 basic_block bb
, *bbs
;
4475 bbs
= get_loop_body_in_dom_order (loop
);
4477 for (i
= 0; i
< loop
->num_nodes
; i
++)
4481 if (find_data_references_in_bb (loop
, bb
, datarefs
) == chrec_dont_know
)
4484 return chrec_dont_know
;
4492 /* Recursive helper function. */
4495 find_loop_nest_1 (struct loop
*loop
, VEC (loop_p
, heap
) **loop_nest
)
4497 /* Inner loops of the nest should not contain siblings. Example:
4498 when there are two consecutive loops,
4509 the dependence relation cannot be captured by the distance
4514 VEC_safe_push (loop_p
, heap
, *loop_nest
, loop
);
4516 return find_loop_nest_1 (loop
->inner
, loop_nest
);
4520 /* Return false when the LOOP is not well nested. Otherwise return
4521 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4522 contain the loops from the outermost to the innermost, as they will
4523 appear in the classic distance vector. */
4526 find_loop_nest (struct loop
*loop
, VEC (loop_p
, heap
) **loop_nest
)
4528 VEC_safe_push (loop_p
, heap
, *loop_nest
, loop
);
4530 return find_loop_nest_1 (loop
->inner
, loop_nest
);
4534 /* Returns true when the data dependences have been computed, false otherwise.
4535 Given a loop nest LOOP, the following vectors are returned:
4536 DATAREFS is initialized to all the array elements contained in this loop,
4537 DEPENDENCE_RELATIONS contains the relations between the data references.
4538 Compute read-read and self relations if
4539 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4542 compute_data_dependences_for_loop (struct loop
*loop
,
4543 bool compute_self_and_read_read_dependences
,
4544 VEC (loop_p
, heap
) **loop_nest
,
4545 VEC (data_reference_p
, heap
) **datarefs
,
4546 VEC (ddr_p
, heap
) **dependence_relations
)
4550 memset (&dependence_stats
, 0, sizeof (dependence_stats
));
4552 /* If the loop nest is not well formed, or one of the data references
4553 is not computable, give up without spending time to compute other
4556 || !find_loop_nest (loop
, loop_nest
)
4557 || find_data_references_in_loop (loop
, datarefs
) == chrec_dont_know
4558 || !compute_all_dependences (*datarefs
, dependence_relations
, *loop_nest
,
4559 compute_self_and_read_read_dependences
))
4562 if (dump_file
&& (dump_flags
& TDF_STATS
))
4564 fprintf (dump_file
, "Dependence tester statistics:\n");
4566 fprintf (dump_file
, "Number of dependence tests: %d\n",
4567 dependence_stats
.num_dependence_tests
);
4568 fprintf (dump_file
, "Number of dependence tests classified dependent: %d\n",
4569 dependence_stats
.num_dependence_dependent
);
4570 fprintf (dump_file
, "Number of dependence tests classified independent: %d\n",
4571 dependence_stats
.num_dependence_independent
);
4572 fprintf (dump_file
, "Number of undetermined dependence tests: %d\n",
4573 dependence_stats
.num_dependence_undetermined
);
4575 fprintf (dump_file
, "Number of subscript tests: %d\n",
4576 dependence_stats
.num_subscript_tests
);
4577 fprintf (dump_file
, "Number of undetermined subscript tests: %d\n",
4578 dependence_stats
.num_subscript_undetermined
);
4579 fprintf (dump_file
, "Number of same subscript function: %d\n",
4580 dependence_stats
.num_same_subscript_function
);
4582 fprintf (dump_file
, "Number of ziv tests: %d\n",
4583 dependence_stats
.num_ziv
);
4584 fprintf (dump_file
, "Number of ziv tests returning dependent: %d\n",
4585 dependence_stats
.num_ziv_dependent
);
4586 fprintf (dump_file
, "Number of ziv tests returning independent: %d\n",
4587 dependence_stats
.num_ziv_independent
);
4588 fprintf (dump_file
, "Number of ziv tests unimplemented: %d\n",
4589 dependence_stats
.num_ziv_unimplemented
);
4591 fprintf (dump_file
, "Number of siv tests: %d\n",
4592 dependence_stats
.num_siv
);
4593 fprintf (dump_file
, "Number of siv tests returning dependent: %d\n",
4594 dependence_stats
.num_siv_dependent
);
4595 fprintf (dump_file
, "Number of siv tests returning independent: %d\n",
4596 dependence_stats
.num_siv_independent
);
4597 fprintf (dump_file
, "Number of siv tests unimplemented: %d\n",
4598 dependence_stats
.num_siv_unimplemented
);
4600 fprintf (dump_file
, "Number of miv tests: %d\n",
4601 dependence_stats
.num_miv
);
4602 fprintf (dump_file
, "Number of miv tests returning dependent: %d\n",
4603 dependence_stats
.num_miv_dependent
);
4604 fprintf (dump_file
, "Number of miv tests returning independent: %d\n",
4605 dependence_stats
.num_miv_independent
);
4606 fprintf (dump_file
, "Number of miv tests unimplemented: %d\n",
4607 dependence_stats
.num_miv_unimplemented
);
4613 /* Returns true when the data dependences for the basic block BB have been
4614 computed, false otherwise.
4615 DATAREFS is initialized to all the array elements contained in this basic
4616 block, DEPENDENCE_RELATIONS contains the relations between the data
4617 references. Compute read-read and self relations if
4618 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4620 compute_data_dependences_for_bb (basic_block bb
,
4621 bool compute_self_and_read_read_dependences
,
4622 VEC (data_reference_p
, heap
) **datarefs
,
4623 VEC (ddr_p
, heap
) **dependence_relations
)
4625 if (find_data_references_in_bb (NULL
, bb
, datarefs
) == chrec_dont_know
)
4628 return compute_all_dependences (*datarefs
, dependence_relations
, NULL
,
4629 compute_self_and_read_read_dependences
);
4632 /* Entry point (for testing only). Analyze all the data references
4633 and the dependence relations in LOOP.
4635 The data references are computed first.
4637 A relation on these nodes is represented by a complete graph. Some
4638 of the relations could be of no interest, thus the relations can be
4641 In the following function we compute all the relations. This is
4642 just a first implementation that is here for:
4643 - for showing how to ask for the dependence relations,
4644 - for the debugging the whole dependence graph,
4645 - for the dejagnu testcases and maintenance.
4647 It is possible to ask only for a part of the graph, avoiding to
4648 compute the whole dependence graph. The computed dependences are
4649 stored in a knowledge base (KB) such that later queries don't
4650 recompute the same information. The implementation of this KB is
4651 transparent to the optimizer, and thus the KB can be changed with a
4652 more efficient implementation, or the KB could be disabled. */
4654 analyze_all_data_dependences (struct loop
*loop
)
4657 int nb_data_refs
= 10;
4658 VEC (data_reference_p
, heap
) *datarefs
=
4659 VEC_alloc (data_reference_p
, heap
, nb_data_refs
);
4660 VEC (ddr_p
, heap
) *dependence_relations
=
4661 VEC_alloc (ddr_p
, heap
, nb_data_refs
* nb_data_refs
);
4662 VEC (loop_p
, heap
) *loop_nest
= VEC_alloc (loop_p
, heap
, 3);
4664 /* Compute DDs on the whole function. */
4665 compute_data_dependences_for_loop (loop
, false, &loop_nest
, &datarefs
,
4666 &dependence_relations
);
4670 dump_data_dependence_relations (dump_file
, dependence_relations
);
4671 fprintf (dump_file
, "\n\n");
4673 if (dump_flags
& TDF_DETAILS
)
4674 dump_dist_dir_vectors (dump_file
, dependence_relations
);
4676 if (dump_flags
& TDF_STATS
)
4678 unsigned nb_top_relations
= 0;
4679 unsigned nb_bot_relations
= 0;
4680 unsigned nb_chrec_relations
= 0;
4681 struct data_dependence_relation
*ddr
;
4683 FOR_EACH_VEC_ELT (ddr_p
, dependence_relations
, i
, ddr
)
4685 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr
)))
4688 else if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
4692 nb_chrec_relations
++;
4695 gather_stats_on_scev_database ();
4699 VEC_free (loop_p
, heap
, loop_nest
);
4700 free_dependence_relations (dependence_relations
);
4701 free_data_refs (datarefs
);
4704 /* Computes all the data dependences and check that the results of
4705 several analyzers are the same. */
4708 tree_check_data_deps (void)
4711 struct loop
*loop_nest
;
4713 FOR_EACH_LOOP (li
, loop_nest
, 0)
4714 analyze_all_data_dependences (loop_nest
);
4717 /* Free the memory used by a data dependence relation DDR. */
4720 free_dependence_relation (struct data_dependence_relation
*ddr
)
4725 if (DDR_SUBSCRIPTS (ddr
))
4726 free_subscripts (DDR_SUBSCRIPTS (ddr
));
4727 if (DDR_DIST_VECTS (ddr
))
4728 VEC_free (lambda_vector
, heap
, DDR_DIST_VECTS (ddr
));
4729 if (DDR_DIR_VECTS (ddr
))
4730 VEC_free (lambda_vector
, heap
, DDR_DIR_VECTS (ddr
));
4735 /* Free the memory used by the data dependence relations from
4736 DEPENDENCE_RELATIONS. */
4739 free_dependence_relations (VEC (ddr_p
, heap
) *dependence_relations
)
4742 struct data_dependence_relation
*ddr
;
4744 FOR_EACH_VEC_ELT (ddr_p
, dependence_relations
, i
, ddr
)
4746 free_dependence_relation (ddr
);
4748 VEC_free (ddr_p
, heap
, dependence_relations
);
4751 /* Free the memory used by the data references from DATAREFS. */
4754 free_data_refs (VEC (data_reference_p
, heap
) *datarefs
)
4757 struct data_reference
*dr
;
4759 FOR_EACH_VEC_ELT (data_reference_p
, datarefs
, i
, dr
)
4761 VEC_free (data_reference_p
, heap
, datarefs
);
4766 /* Dump vertex I in RDG to FILE. */
4769 dump_rdg_vertex (FILE *file
, struct graph
*rdg
, int i
)
4771 struct vertex
*v
= &(rdg
->vertices
[i
]);
4772 struct graph_edge
*e
;
4774 fprintf (file
, "(vertex %d: (%s%s) (in:", i
,
4775 RDG_MEM_WRITE_STMT (rdg
, i
) ? "w" : "",
4776 RDG_MEM_READS_STMT (rdg
, i
) ? "r" : "");
4779 for (e
= v
->pred
; e
; e
= e
->pred_next
)
4780 fprintf (file
, " %d", e
->src
);
4782 fprintf (file
, ") (out:");
4785 for (e
= v
->succ
; e
; e
= e
->succ_next
)
4786 fprintf (file
, " %d", e
->dest
);
4788 fprintf (file
, ")\n");
4789 print_gimple_stmt (file
, RDGV_STMT (v
), 0, TDF_VOPS
|TDF_MEMSYMS
);
4790 fprintf (file
, ")\n");
4793 /* Call dump_rdg_vertex on stderr. */
4796 debug_rdg_vertex (struct graph
*rdg
, int i
)
4798 dump_rdg_vertex (stderr
, rdg
, i
);
4801 /* Dump component C of RDG to FILE. If DUMPED is non-null, set the
4802 dumped vertices to that bitmap. */
4805 dump_rdg_component (FILE *file
, struct graph
*rdg
, int c
, bitmap dumped
)
4809 fprintf (file
, "(%d\n", c
);
4811 for (i
= 0; i
< rdg
->n_vertices
; i
++)
4812 if (rdg
->vertices
[i
].component
== c
)
4815 bitmap_set_bit (dumped
, i
);
4817 dump_rdg_vertex (file
, rdg
, i
);
4820 fprintf (file
, ")\n");
4823 /* Call dump_rdg_vertex on stderr. */
4826 debug_rdg_component (struct graph
*rdg
, int c
)
4828 dump_rdg_component (stderr
, rdg
, c
, NULL
);
4831 /* Dump the reduced dependence graph RDG to FILE. */
4834 dump_rdg (FILE *file
, struct graph
*rdg
)
4837 bitmap dumped
= BITMAP_ALLOC (NULL
);
4839 fprintf (file
, "(rdg\n");
4841 for (i
= 0; i
< rdg
->n_vertices
; i
++)
4842 if (!bitmap_bit_p (dumped
, i
))
4843 dump_rdg_component (file
, rdg
, rdg
->vertices
[i
].component
, dumped
);
4845 fprintf (file
, ")\n");
4846 BITMAP_FREE (dumped
);
4849 /* Call dump_rdg on stderr. */
4852 debug_rdg (struct graph
*rdg
)
4854 dump_rdg (stderr
, rdg
);
4858 dot_rdg_1 (FILE *file
, struct graph
*rdg
)
4862 fprintf (file
, "digraph RDG {\n");
4864 for (i
= 0; i
< rdg
->n_vertices
; i
++)
4866 struct vertex
*v
= &(rdg
->vertices
[i
]);
4867 struct graph_edge
*e
;
4869 /* Highlight reads from memory. */
4870 if (RDG_MEM_READS_STMT (rdg
, i
))
4871 fprintf (file
, "%d [style=filled, fillcolor=green]\n", i
);
4873 /* Highlight stores to memory. */
4874 if (RDG_MEM_WRITE_STMT (rdg
, i
))
4875 fprintf (file
, "%d [style=filled, fillcolor=red]\n", i
);
4878 for (e
= v
->succ
; e
; e
= e
->succ_next
)
4879 switch (RDGE_TYPE (e
))
4882 fprintf (file
, "%d -> %d [label=input] \n", i
, e
->dest
);
4886 fprintf (file
, "%d -> %d [label=output] \n", i
, e
->dest
);
4890 /* These are the most common dependences: don't print these. */
4891 fprintf (file
, "%d -> %d \n", i
, e
->dest
);
4895 fprintf (file
, "%d -> %d [label=anti] \n", i
, e
->dest
);
4903 fprintf (file
, "}\n\n");
4906 /* Display the Reduced Dependence Graph using dotty. */
4907 extern void dot_rdg (struct graph
*);
4910 dot_rdg (struct graph
*rdg
)
4912 /* When debugging, enable the following code. This cannot be used
4913 in production compilers because it calls "system". */
4915 FILE *file
= fopen ("/tmp/rdg.dot", "w");
4916 gcc_assert (file
!= NULL
);
4918 dot_rdg_1 (file
, rdg
);
4921 system ("dotty /tmp/rdg.dot &");
4923 dot_rdg_1 (stderr
, rdg
);
4927 /* Returns the index of STMT in RDG. */
4930 rdg_vertex_for_stmt (struct graph
*rdg ATTRIBUTE_UNUSED
, gimple stmt
)
4932 int index
= gimple_uid (stmt
);
4933 gcc_checking_assert (index
== -1 || RDG_STMT (rdg
, index
) == stmt
);
4937 /* Creates an edge in RDG for each distance vector from DDR. The
4938 order that we keep track of in the RDG is the order in which
4939 statements have to be executed. */
4942 create_rdg_edge_for_ddr (struct graph
*rdg
, ddr_p ddr
)
4944 struct graph_edge
*e
;
4946 data_reference_p dra
= DDR_A (ddr
);
4947 data_reference_p drb
= DDR_B (ddr
);
4948 unsigned level
= ddr_dependence_level (ddr
);
4950 /* For non scalar dependences, when the dependence is REVERSED,
4951 statement B has to be executed before statement A. */
4953 && !DDR_REVERSED_P (ddr
))
4955 data_reference_p tmp
= dra
;
4960 va
= rdg_vertex_for_stmt (rdg
, DR_STMT (dra
));
4961 vb
= rdg_vertex_for_stmt (rdg
, DR_STMT (drb
));
4963 if (va
< 0 || vb
< 0)
4966 e
= add_edge (rdg
, va
, vb
);
4967 e
->data
= XNEW (struct rdg_edge
);
4969 RDGE_LEVEL (e
) = level
;
4970 RDGE_RELATION (e
) = ddr
;
4972 /* Determines the type of the data dependence. */
4973 if (DR_IS_READ (dra
) && DR_IS_READ (drb
))
4974 RDGE_TYPE (e
) = input_dd
;
4975 else if (DR_IS_WRITE (dra
) && DR_IS_WRITE (drb
))
4976 RDGE_TYPE (e
) = output_dd
;
4977 else if (DR_IS_WRITE (dra
) && DR_IS_READ (drb
))
4978 RDGE_TYPE (e
) = flow_dd
;
4979 else if (DR_IS_READ (dra
) && DR_IS_WRITE (drb
))
4980 RDGE_TYPE (e
) = anti_dd
;
4983 /* Creates dependence edges in RDG for all the uses of DEF. IDEF is
4984 the index of DEF in RDG. */
4987 create_rdg_edges_for_scalar (struct graph
*rdg
, tree def
, int idef
)
4989 use_operand_p imm_use_p
;
4990 imm_use_iterator iterator
;
4992 FOR_EACH_IMM_USE_FAST (imm_use_p
, iterator
, def
)
4994 struct graph_edge
*e
;
4995 int use
= rdg_vertex_for_stmt (rdg
, USE_STMT (imm_use_p
));
5000 e
= add_edge (rdg
, idef
, use
);
5001 e
->data
= XNEW (struct rdg_edge
);
5002 RDGE_TYPE (e
) = flow_dd
;
5003 RDGE_RELATION (e
) = NULL
;
5007 /* Creates the edges of the reduced dependence graph RDG. */
5010 create_rdg_edges (struct graph
*rdg
, VEC (ddr_p
, heap
) *ddrs
)
5013 struct data_dependence_relation
*ddr
;
5014 def_operand_p def_p
;
5017 FOR_EACH_VEC_ELT (ddr_p
, ddrs
, i
, ddr
)
5018 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
5019 create_rdg_edge_for_ddr (rdg
, ddr
);
5021 for (i
= 0; i
< rdg
->n_vertices
; i
++)
5022 FOR_EACH_PHI_OR_STMT_DEF (def_p
, RDG_STMT (rdg
, i
),
5024 create_rdg_edges_for_scalar (rdg
, DEF_FROM_PTR (def_p
), i
);
5027 /* Build the vertices of the reduced dependence graph RDG. */
5030 create_rdg_vertices (struct graph
*rdg
, VEC (gimple
, heap
) *stmts
, loop_p loop
)
5035 FOR_EACH_VEC_ELT (gimple
, stmts
, i
, stmt
)
5037 VEC (data_ref_loc
, heap
) *references
;
5039 struct vertex
*v
= &(rdg
->vertices
[i
]);
5041 /* Record statement to vertex mapping. */
5042 gimple_set_uid (stmt
, i
);
5044 v
->data
= XNEW (struct rdg_vertex
);
5045 RDGV_STMT (v
) = stmt
;
5046 RDGV_DATAREFS (v
) = NULL
;
5047 RDGV_HAS_MEM_WRITE (v
) = false;
5048 RDGV_HAS_MEM_READS (v
) = false;
5049 if (gimple_code (stmt
) == GIMPLE_PHI
)
5052 get_references_in_stmt (stmt
, &references
);
5053 FOR_EACH_VEC_ELT (data_ref_loc
, references
, j
, ref
)
5055 data_reference_p dr
;
5057 RDGV_HAS_MEM_WRITE (v
) = true;
5059 RDGV_HAS_MEM_READS (v
) = true;
5060 dr
= create_data_ref (loop
, loop_containing_stmt (stmt
),
5061 *ref
->pos
, stmt
, ref
->is_read
);
5063 VEC_safe_push (data_reference_p
, heap
, RDGV_DATAREFS (v
), dr
);
5065 VEC_free (data_ref_loc
, heap
, references
);
5069 /* Initialize STMTS with all the statements of LOOP. When
5070 INCLUDE_PHIS is true, include also the PHI nodes. The order in
5071 which we discover statements is important as
5072 generate_loops_for_partition is using the same traversal for
5073 identifying statements. */
5076 stmts_from_loop (struct loop
*loop
, VEC (gimple
, heap
) **stmts
)
5079 basic_block
*bbs
= get_loop_body_in_dom_order (loop
);
5081 for (i
= 0; i
< loop
->num_nodes
; i
++)
5083 basic_block bb
= bbs
[i
];
5084 gimple_stmt_iterator bsi
;
5087 for (bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
5088 VEC_safe_push (gimple
, heap
, *stmts
, gsi_stmt (bsi
));
5090 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
5092 stmt
= gsi_stmt (bsi
);
5093 if (gimple_code (stmt
) != GIMPLE_LABEL
&& !is_gimple_debug (stmt
))
5094 VEC_safe_push (gimple
, heap
, *stmts
, stmt
);
5101 /* Returns true when all the dependences are computable. */
5104 known_dependences_p (VEC (ddr_p
, heap
) *dependence_relations
)
5109 FOR_EACH_VEC_ELT (ddr_p
, dependence_relations
, i
, ddr
)
5110 if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
5116 /* Build the Reduced Dependence Graph (RDG) with one vertex per
5117 statement of the loop nest, and one edge per data dependence or
5118 scalar dependence. */
5121 build_empty_rdg (int n_stmts
)
5123 struct graph
*rdg
= new_graph (n_stmts
);
5127 /* Build the Reduced Dependence Graph (RDG) with one vertex per
5128 statement of the loop nest, and one edge per data dependence or
5129 scalar dependence. */
5132 build_rdg (struct loop
*loop
,
5133 VEC (loop_p
, heap
) **loop_nest
,
5134 VEC (ddr_p
, heap
) **dependence_relations
,
5135 VEC (data_reference_p
, heap
) **datarefs
)
5137 struct graph
*rdg
= NULL
;
5139 if (compute_data_dependences_for_loop (loop
, false, loop_nest
, datarefs
,
5140 dependence_relations
)
5141 && known_dependences_p (*dependence_relations
))
5143 VEC (gimple
, heap
) *stmts
= VEC_alloc (gimple
, heap
, 10);
5144 stmts_from_loop (loop
, &stmts
);
5145 rdg
= build_empty_rdg (VEC_length (gimple
, stmts
));
5146 create_rdg_vertices (rdg
, stmts
, loop
);
5147 create_rdg_edges (rdg
, *dependence_relations
);
5148 VEC_free (gimple
, heap
, stmts
);
5154 /* Free the reduced dependence graph RDG. */
5157 free_rdg (struct graph
*rdg
)
5161 for (i
= 0; i
< rdg
->n_vertices
; i
++)
5163 struct vertex
*v
= &(rdg
->vertices
[i
]);
5164 struct graph_edge
*e
;
5166 for (e
= v
->succ
; e
; e
= e
->succ_next
)
5169 gimple_set_uid (RDGV_STMT (v
), -1);
5170 free_data_refs (RDGV_DATAREFS (v
));
5177 /* Determines whether the statement from vertex V of the RDG has a
5178 definition used outside the loop that contains this statement. */
5181 rdg_defs_used_in_other_loops_p (struct graph
*rdg
, int v
)
5183 gimple stmt
= RDG_STMT (rdg
, v
);
5184 struct loop
*loop
= loop_containing_stmt (stmt
);
5185 use_operand_p imm_use_p
;
5186 imm_use_iterator iterator
;
5188 def_operand_p def_p
;
5193 FOR_EACH_PHI_OR_STMT_DEF (def_p
, stmt
, it
, SSA_OP_DEF
)
5195 FOR_EACH_IMM_USE_FAST (imm_use_p
, iterator
, DEF_FROM_PTR (def_p
))
5197 if (loop_containing_stmt (USE_STMT (imm_use_p
)) != loop
)