Merge from trunk @215656.
[official-gcc.git] / gcc / tree-data-ref.c
blob78ff20ef634f834df9e1806c3ab6aa04c6fc8fd8
1 /* Data references and dependences detectors.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This pass walks a given loop structure searching for array
22 references. The information about the array accesses is recorded
23 in DATA_REFERENCE structures.
25 The basic test for determining the dependences is:
26 given two access functions chrec1 and chrec2 to a same array, and
27 x and y two vectors from the iteration domain, the same element of
28 the array is accessed twice at iterations x and y if and only if:
29 | chrec1 (x) == chrec2 (y).
31 The goals of this analysis are:
33 - to determine the independence: the relation between two
34 independent accesses is qualified with the chrec_known (this
35 information allows a loop parallelization),
37 - when two data references access the same data, to qualify the
38 dependence relation with classic dependence representations:
40 - distance vectors
41 - direction vectors
42 - loop carried level dependence
43 - polyhedron dependence
44 or with the chains of recurrences based representation,
46 - to define a knowledge base for storing the data dependence
47 information,
49 - to define an interface to access this data.
52 Definitions:
54 - subscript: given two array accesses a subscript is the tuple
55 composed of the access functions for a given dimension. Example:
56 Given A[f1][f2][f3] and B[g1][g2][g3], there are three subscripts:
57 (f1, g1), (f2, g2), (f3, g3).
59 - Diophantine equation: an equation whose coefficients and
60 solutions are integer constants, for example the equation
61 | 3*x + 2*y = 1
62 has an integer solution x = 1 and y = -1.
64 References:
66 - "Advanced Compilation for High Performance Computing" by Randy
67 Allen and Ken Kennedy.
68 http://citeseer.ist.psu.edu/goff91practical.html
70 - "Loop Transformations for Restructuring Compilers - The Foundations"
71 by Utpal Banerjee.
76 #include "config.h"
77 #include "system.h"
78 #include "coretypes.h"
79 #include "tree.h"
80 #include "expr.h"
81 #include "gimple-pretty-print.h"
82 #include "basic-block.h"
83 #include "tree-ssa-alias.h"
84 #include "internal-fn.h"
85 #include "gimple-expr.h"
86 #include "is-a.h"
87 #include "gimple.h"
88 #include "gimple-iterator.h"
89 #include "tree-ssa-loop-niter.h"
90 #include "tree-ssa-loop.h"
91 #include "tree-ssa.h"
92 #include "cfgloop.h"
93 #include "tree-data-ref.h"
94 #include "tree-scalar-evolution.h"
95 #include "dumpfile.h"
96 #include "langhooks.h"
97 #include "tree-affine.h"
98 #include "params.h"
100 static struct datadep_stats
102 int num_dependence_tests;
103 int num_dependence_dependent;
104 int num_dependence_independent;
105 int num_dependence_undetermined;
107 int num_subscript_tests;
108 int num_subscript_undetermined;
109 int num_same_subscript_function;
111 int num_ziv;
112 int num_ziv_independent;
113 int num_ziv_dependent;
114 int num_ziv_unimplemented;
116 int num_siv;
117 int num_siv_independent;
118 int num_siv_dependent;
119 int num_siv_unimplemented;
121 int num_miv;
122 int num_miv_independent;
123 int num_miv_dependent;
124 int num_miv_unimplemented;
125 } dependence_stats;
127 static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
128 struct data_reference *,
129 struct data_reference *,
130 struct loop *);
131 /* Returns true iff A divides B. */
133 static inline bool
134 tree_fold_divides_p (const_tree a, const_tree b)
136 gcc_assert (TREE_CODE (a) == INTEGER_CST);
137 gcc_assert (TREE_CODE (b) == INTEGER_CST);
138 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a));
141 /* Returns true iff A divides B. */
143 static inline bool
144 int_divides_p (int a, int b)
146 return ((b % a) == 0);
151 /* Dump into FILE all the data references from DATAREFS. */
153 static void
154 dump_data_references (FILE *file, vec<data_reference_p> datarefs)
156 unsigned int i;
157 struct data_reference *dr;
159 FOR_EACH_VEC_ELT (datarefs, i, dr)
160 dump_data_reference (file, dr);
163 /* Unified dump into FILE all the data references from DATAREFS. */
165 DEBUG_FUNCTION void
166 debug (vec<data_reference_p> &ref)
168 dump_data_references (stderr, ref);
171 DEBUG_FUNCTION void
172 debug (vec<data_reference_p> *ptr)
174 if (ptr)
175 debug (*ptr);
176 else
177 fprintf (stderr, "<nil>\n");
181 /* Dump into STDERR all the data references from DATAREFS. */
183 DEBUG_FUNCTION void
184 debug_data_references (vec<data_reference_p> datarefs)
186 dump_data_references (stderr, datarefs);
189 /* Print to STDERR the data_reference DR. */
191 DEBUG_FUNCTION void
192 debug_data_reference (struct data_reference *dr)
194 dump_data_reference (stderr, dr);
197 /* Dump function for a DATA_REFERENCE structure. */
199 void
200 dump_data_reference (FILE *outf,
201 struct data_reference *dr)
203 unsigned int i;
205 fprintf (outf, "#(Data Ref: \n");
206 fprintf (outf, "# bb: %d \n", gimple_bb (DR_STMT (dr))->index);
207 fprintf (outf, "# stmt: ");
208 print_gimple_stmt (outf, DR_STMT (dr), 0, 0);
209 fprintf (outf, "# ref: ");
210 print_generic_stmt (outf, DR_REF (dr), 0);
211 fprintf (outf, "# base_object: ");
212 print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
214 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
216 fprintf (outf, "# Access function %d: ", i);
217 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
219 fprintf (outf, "#)\n");
222 /* Unified dump function for a DATA_REFERENCE structure. */
224 DEBUG_FUNCTION void
225 debug (data_reference &ref)
227 dump_data_reference (stderr, &ref);
230 DEBUG_FUNCTION void
231 debug (data_reference *ptr)
233 if (ptr)
234 debug (*ptr);
235 else
236 fprintf (stderr, "<nil>\n");
240 /* Dumps the affine function described by FN to the file OUTF. */
242 static void
243 dump_affine_function (FILE *outf, affine_fn fn)
245 unsigned i;
246 tree coef;
248 print_generic_expr (outf, fn[0], TDF_SLIM);
249 for (i = 1; fn.iterate (i, &coef); i++)
251 fprintf (outf, " + ");
252 print_generic_expr (outf, coef, TDF_SLIM);
253 fprintf (outf, " * x_%u", i);
257 /* Dumps the conflict function CF to the file OUTF. */
259 static void
260 dump_conflict_function (FILE *outf, conflict_function *cf)
262 unsigned i;
264 if (cf->n == NO_DEPENDENCE)
265 fprintf (outf, "no dependence");
266 else if (cf->n == NOT_KNOWN)
267 fprintf (outf, "not known");
268 else
270 for (i = 0; i < cf->n; i++)
272 if (i != 0)
273 fprintf (outf, " ");
274 fprintf (outf, "[");
275 dump_affine_function (outf, cf->fns[i]);
276 fprintf (outf, "]");
281 /* Dump function for a SUBSCRIPT structure. */
283 static void
284 dump_subscript (FILE *outf, struct subscript *subscript)
286 conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
288 fprintf (outf, "\n (subscript \n");
289 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
290 dump_conflict_function (outf, cf);
291 if (CF_NONTRIVIAL_P (cf))
293 tree last_iteration = SUB_LAST_CONFLICT (subscript);
294 fprintf (outf, "\n last_conflict: ");
295 print_generic_expr (outf, last_iteration, 0);
298 cf = SUB_CONFLICTS_IN_B (subscript);
299 fprintf (outf, "\n iterations_that_access_an_element_twice_in_B: ");
300 dump_conflict_function (outf, cf);
301 if (CF_NONTRIVIAL_P (cf))
303 tree last_iteration = SUB_LAST_CONFLICT (subscript);
304 fprintf (outf, "\n last_conflict: ");
305 print_generic_expr (outf, last_iteration, 0);
308 fprintf (outf, "\n (Subscript distance: ");
309 print_generic_expr (outf, SUB_DISTANCE (subscript), 0);
310 fprintf (outf, " ))\n");
313 /* Print the classic direction vector DIRV to OUTF. */
315 static void
316 print_direction_vector (FILE *outf,
317 lambda_vector dirv,
318 int length)
320 int eq;
322 for (eq = 0; eq < length; eq++)
324 enum data_dependence_direction dir = ((enum data_dependence_direction)
325 dirv[eq]);
327 switch (dir)
329 case dir_positive:
330 fprintf (outf, " +");
331 break;
332 case dir_negative:
333 fprintf (outf, " -");
334 break;
335 case dir_equal:
336 fprintf (outf, " =");
337 break;
338 case dir_positive_or_equal:
339 fprintf (outf, " +=");
340 break;
341 case dir_positive_or_negative:
342 fprintf (outf, " +-");
343 break;
344 case dir_negative_or_equal:
345 fprintf (outf, " -=");
346 break;
347 case dir_star:
348 fprintf (outf, " *");
349 break;
350 default:
351 fprintf (outf, "indep");
352 break;
355 fprintf (outf, "\n");
358 /* Print a vector of direction vectors. */
360 static void
361 print_dir_vectors (FILE *outf, vec<lambda_vector> dir_vects,
362 int length)
364 unsigned j;
365 lambda_vector v;
367 FOR_EACH_VEC_ELT (dir_vects, j, v)
368 print_direction_vector (outf, v, length);
371 /* Print out a vector VEC of length N to OUTFILE. */
373 static inline void
374 print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
376 int i;
378 for (i = 0; i < n; i++)
379 fprintf (outfile, "%3d ", vector[i]);
380 fprintf (outfile, "\n");
383 /* Print a vector of distance vectors. */
385 static void
386 print_dist_vectors (FILE *outf, vec<lambda_vector> dist_vects,
387 int length)
389 unsigned j;
390 lambda_vector v;
392 FOR_EACH_VEC_ELT (dist_vects, j, v)
393 print_lambda_vector (outf, v, length);
396 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
398 static void
399 dump_data_dependence_relation (FILE *outf,
400 struct data_dependence_relation *ddr)
402 struct data_reference *dra, *drb;
404 fprintf (outf, "(Data Dep: \n");
406 if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
408 if (ddr)
410 dra = DDR_A (ddr);
411 drb = DDR_B (ddr);
412 if (dra)
413 dump_data_reference (outf, dra);
414 else
415 fprintf (outf, " (nil)\n");
416 if (drb)
417 dump_data_reference (outf, drb);
418 else
419 fprintf (outf, " (nil)\n");
421 fprintf (outf, " (don't know)\n)\n");
422 return;
425 dra = DDR_A (ddr);
426 drb = DDR_B (ddr);
427 dump_data_reference (outf, dra);
428 dump_data_reference (outf, drb);
430 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
431 fprintf (outf, " (no dependence)\n");
433 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
435 unsigned int i;
436 struct loop *loopi;
438 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
440 fprintf (outf, " access_fn_A: ");
441 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
442 fprintf (outf, " access_fn_B: ");
443 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
444 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
447 fprintf (outf, " inner loop index: %d\n", DDR_INNER_LOOP (ddr));
448 fprintf (outf, " loop nest: (");
449 FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
450 fprintf (outf, "%d ", loopi->num);
451 fprintf (outf, ")\n");
453 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
455 fprintf (outf, " distance_vector: ");
456 print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
457 DDR_NB_LOOPS (ddr));
460 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
462 fprintf (outf, " direction_vector: ");
463 print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
464 DDR_NB_LOOPS (ddr));
468 fprintf (outf, ")\n");
471 /* Debug version. */
473 DEBUG_FUNCTION void
474 debug_data_dependence_relation (struct data_dependence_relation *ddr)
476 dump_data_dependence_relation (stderr, ddr);
479 /* Dump into FILE all the dependence relations from DDRS. */
481 void
482 dump_data_dependence_relations (FILE *file,
483 vec<ddr_p> ddrs)
485 unsigned int i;
486 struct data_dependence_relation *ddr;
488 FOR_EACH_VEC_ELT (ddrs, i, ddr)
489 dump_data_dependence_relation (file, ddr);
492 DEBUG_FUNCTION void
493 debug (vec<ddr_p> &ref)
495 dump_data_dependence_relations (stderr, ref);
498 DEBUG_FUNCTION void
499 debug (vec<ddr_p> *ptr)
501 if (ptr)
502 debug (*ptr);
503 else
504 fprintf (stderr, "<nil>\n");
508 /* Dump to STDERR all the dependence relations from DDRS. */
510 DEBUG_FUNCTION void
511 debug_data_dependence_relations (vec<ddr_p> ddrs)
513 dump_data_dependence_relations (stderr, ddrs);
516 /* Dumps the distance and direction vectors in FILE. DDRS contains
517 the dependence relations, and VECT_SIZE is the size of the
518 dependence vectors, or in other words the number of loops in the
519 considered nest. */
521 static void
522 dump_dist_dir_vectors (FILE *file, vec<ddr_p> ddrs)
524 unsigned int i, j;
525 struct data_dependence_relation *ddr;
526 lambda_vector v;
528 FOR_EACH_VEC_ELT (ddrs, i, ddr)
529 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
531 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), j, v)
533 fprintf (file, "DISTANCE_V (");
534 print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
535 fprintf (file, ")\n");
538 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), j, v)
540 fprintf (file, "DIRECTION_V (");
541 print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
542 fprintf (file, ")\n");
546 fprintf (file, "\n\n");
549 /* Dumps the data dependence relations DDRS in FILE. */
551 static void
552 dump_ddrs (FILE *file, vec<ddr_p> ddrs)
554 unsigned int i;
555 struct data_dependence_relation *ddr;
557 FOR_EACH_VEC_ELT (ddrs, i, ddr)
558 dump_data_dependence_relation (file, ddr);
560 fprintf (file, "\n\n");
563 DEBUG_FUNCTION void
564 debug_ddrs (vec<ddr_p> ddrs)
566 dump_ddrs (stderr, ddrs);
569 /* Helper function for split_constant_offset. Expresses OP0 CODE OP1
570 (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
571 constant of type ssizetype, and returns true. If we cannot do this
572 with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
573 is returned. */
575 static bool
576 split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
577 tree *var, tree *off)
579 tree var0, var1;
580 tree off0, off1;
581 enum tree_code ocode = code;
583 *var = NULL_TREE;
584 *off = NULL_TREE;
586 switch (code)
588 case INTEGER_CST:
589 *var = build_int_cst (type, 0);
590 *off = fold_convert (ssizetype, op0);
591 return true;
593 case POINTER_PLUS_EXPR:
594 ocode = PLUS_EXPR;
595 /* FALLTHROUGH */
596 case PLUS_EXPR:
597 case MINUS_EXPR:
598 split_constant_offset (op0, &var0, &off0);
599 split_constant_offset (op1, &var1, &off1);
600 *var = fold_build2 (code, type, var0, var1);
601 *off = size_binop (ocode, off0, off1);
602 return true;
604 case MULT_EXPR:
605 if (TREE_CODE (op1) != INTEGER_CST)
606 return false;
608 split_constant_offset (op0, &var0, &off0);
609 *var = fold_build2 (MULT_EXPR, type, var0, op1);
610 *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
611 return true;
613 case ADDR_EXPR:
615 tree base, poffset;
616 HOST_WIDE_INT pbitsize, pbitpos;
617 enum machine_mode pmode;
618 int punsignedp, preversep, pvolatilep;
620 op0 = TREE_OPERAND (op0, 0);
621 base
622 = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset, &pmode,
623 &punsignedp, &preversep, &pvolatilep, false);
625 if (pbitpos % BITS_PER_UNIT != 0)
626 return false;
627 base = build_fold_addr_expr (base);
628 off0 = ssize_int (pbitpos / BITS_PER_UNIT);
630 if (poffset)
632 split_constant_offset (poffset, &poffset, &off1);
633 off0 = size_binop (PLUS_EXPR, off0, off1);
634 if (POINTER_TYPE_P (TREE_TYPE (base)))
635 base = fold_build_pointer_plus (base, poffset);
636 else
637 base = fold_build2 (PLUS_EXPR, TREE_TYPE (base), base,
638 fold_convert (TREE_TYPE (base), poffset));
641 var0 = fold_convert (type, base);
643 /* If variable length types are involved, punt, otherwise casts
644 might be converted into ARRAY_REFs in gimplify_conversion.
645 To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
646 possibly no longer appears in current GIMPLE, might resurface.
647 This perhaps could run
648 if (CONVERT_EXPR_P (var0))
650 gimplify_conversion (&var0);
651 // Attempt to fill in any within var0 found ARRAY_REF's
652 // element size from corresponding op embedded ARRAY_REF,
653 // if unsuccessful, just punt.
654 } */
655 while (POINTER_TYPE_P (type))
656 type = TREE_TYPE (type);
657 if (int_size_in_bytes (type) < 0)
658 return false;
660 *var = var0;
661 *off = off0;
662 return true;
665 case SSA_NAME:
667 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
668 enum tree_code subcode;
670 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
671 return false;
673 var0 = gimple_assign_rhs1 (def_stmt);
674 subcode = gimple_assign_rhs_code (def_stmt);
675 var1 = gimple_assign_rhs2 (def_stmt);
677 return split_constant_offset_1 (type, var0, subcode, var1, var, off);
679 CASE_CONVERT:
681 /* We must not introduce undefined overflow, and we must not change the value.
682 Hence we're okay if the inner type doesn't overflow to start with
683 (pointer or signed), the outer type also is an integer or pointer
684 and the outer precision is at least as large as the inner. */
685 tree itype = TREE_TYPE (op0);
686 if ((POINTER_TYPE_P (itype)
687 || (INTEGRAL_TYPE_P (itype) && TYPE_OVERFLOW_UNDEFINED (itype)))
688 && TYPE_PRECISION (type) >= TYPE_PRECISION (itype)
689 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)))
691 split_constant_offset (op0, &var0, off);
692 *var = fold_convert (type, var0);
693 return true;
695 return false;
698 default:
699 return false;
703 /* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
704 will be ssizetype. */
706 void
707 split_constant_offset (tree exp, tree *var, tree *off)
709 tree type = TREE_TYPE (exp), otype, op0, op1, e, o;
710 enum tree_code code;
712 *var = exp;
713 *off = ssize_int (0);
714 STRIP_NOPS (exp);
716 if (tree_is_chrec (exp)
717 || get_gimple_rhs_class (TREE_CODE (exp)) == GIMPLE_TERNARY_RHS)
718 return;
720 otype = TREE_TYPE (exp);
721 code = TREE_CODE (exp);
722 extract_ops_from_tree (exp, &code, &op0, &op1);
723 if (split_constant_offset_1 (otype, op0, code, op1, &e, &o))
725 *var = fold_convert (type, e);
726 *off = o;
730 /* Returns the address ADDR of an object in a canonical shape (without nop
731 casts, and with type of pointer to the object). */
733 static tree
734 canonicalize_base_object_address (tree addr)
736 tree orig = addr;
738 STRIP_NOPS (addr);
740 /* The base address may be obtained by casting from integer, in that case
741 keep the cast. */
742 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
743 return orig;
745 if (TREE_CODE (addr) != ADDR_EXPR)
746 return addr;
748 return build_fold_addr_expr (TREE_OPERAND (addr, 0));
751 /* Analyzes the behavior of the memory reference DR in the innermost loop or
752 basic block that contains it. Returns true if analysis succeed or false
753 otherwise. */
755 bool
756 dr_analyze_innermost (struct data_reference *dr, struct loop *nest)
758 gimple stmt = DR_STMT (dr);
759 struct loop *loop = loop_containing_stmt (stmt);
760 tree ref = DR_REF (dr);
761 HOST_WIDE_INT pbitsize, pbitpos;
762 tree base, poffset;
763 enum machine_mode pmode;
764 int punsignedp, preversep, pvolatilep;
765 affine_iv base_iv, offset_iv;
766 tree init, dinit, step;
767 bool in_loop = (loop && loop->num);
769 if (dump_file && (dump_flags & TDF_DETAILS))
770 fprintf (dump_file, "analyze_innermost: ");
772 base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset, &pmode,
773 &punsignedp, &preversep, &pvolatilep, false);
774 gcc_assert (base != NULL_TREE);
776 if (pbitpos % BITS_PER_UNIT != 0)
778 if (dump_file && (dump_flags & TDF_DETAILS))
779 fprintf (dump_file, "failed: bit offset alignment.\n");
780 return false;
783 if (preversep)
785 if (dump_file && (dump_flags & TDF_DETAILS))
786 fprintf (dump_file, "failed: reverse storage order.\n");
787 return false;
790 if (TREE_CODE (base) == MEM_REF)
792 if (!integer_zerop (TREE_OPERAND (base, 1)))
794 offset_int moff = mem_ref_offset (base);
795 tree mofft = wide_int_to_tree (sizetype, moff);
796 if (!poffset)
797 poffset = mofft;
798 else
799 poffset = size_binop (PLUS_EXPR, poffset, mofft);
801 base = TREE_OPERAND (base, 0);
803 else
804 base = build_fold_addr_expr (base);
806 if (in_loop)
808 if (!simple_iv (loop, loop_containing_stmt (stmt), base, &base_iv,
809 nest ? true : false))
811 if (nest)
813 if (dump_file && (dump_flags & TDF_DETAILS))
814 fprintf (dump_file, "failed: evolution of base is not"
815 " affine.\n");
816 return false;
818 else
820 base_iv.base = base;
821 base_iv.step = ssize_int (0);
822 base_iv.no_overflow = true;
826 else
828 base_iv.base = base;
829 base_iv.step = ssize_int (0);
830 base_iv.no_overflow = true;
833 if (!poffset)
835 offset_iv.base = ssize_int (0);
836 offset_iv.step = ssize_int (0);
838 else
840 if (!in_loop)
842 offset_iv.base = poffset;
843 offset_iv.step = ssize_int (0);
845 else if (!simple_iv (loop, loop_containing_stmt (stmt),
846 poffset, &offset_iv,
847 nest ? true : false))
849 if (nest)
851 if (dump_file && (dump_flags & TDF_DETAILS))
852 fprintf (dump_file, "failed: evolution of offset is not"
853 " affine.\n");
854 return false;
856 else
858 offset_iv.base = poffset;
859 offset_iv.step = ssize_int (0);
864 init = ssize_int (pbitpos / BITS_PER_UNIT);
865 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
866 init = size_binop (PLUS_EXPR, init, dinit);
867 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
868 init = size_binop (PLUS_EXPR, init, dinit);
870 step = size_binop (PLUS_EXPR,
871 fold_convert (ssizetype, base_iv.step),
872 fold_convert (ssizetype, offset_iv.step));
874 DR_BASE_ADDRESS (dr) = canonicalize_base_object_address (base_iv.base);
876 DR_OFFSET (dr) = fold_convert (ssizetype, offset_iv.base);
877 DR_INIT (dr) = init;
878 DR_STEP (dr) = step;
880 DR_ALIGNED_TO (dr) = size_int (highest_pow2_factor (offset_iv.base));
882 if (dump_file && (dump_flags & TDF_DETAILS))
883 fprintf (dump_file, "success.\n");
885 return true;
888 /* Determines the base object and the list of indices of memory reference
889 DR, analyzed in LOOP and instantiated in loop nest NEST. */
891 static void
892 dr_analyze_indices (struct data_reference *dr, loop_p nest, loop_p loop)
894 vec<tree> access_fns = vNULL;
895 tree ref, op;
896 tree base, off, access_fn;
897 basic_block before_loop;
899 /* If analyzing a basic-block there are no indices to analyze
900 and thus no access functions. */
901 if (!nest)
903 DR_BASE_OBJECT (dr) = DR_REF (dr);
904 DR_ACCESS_FNS (dr).create (0);
905 return;
908 ref = DR_REF (dr);
909 before_loop = block_before_loop (nest);
911 /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
912 into a two element array with a constant index. The base is
913 then just the immediate underlying object. */
914 if (TREE_CODE (ref) == REALPART_EXPR)
916 ref = TREE_OPERAND (ref, 0);
917 access_fns.safe_push (integer_zero_node);
919 else if (TREE_CODE (ref) == IMAGPART_EXPR)
921 ref = TREE_OPERAND (ref, 0);
922 access_fns.safe_push (integer_one_node);
925 /* Analyze access functions of dimensions we know to be independent. */
926 while (handled_component_p (ref))
928 if (TREE_CODE (ref) == ARRAY_REF)
930 op = TREE_OPERAND (ref, 1);
931 access_fn = analyze_scalar_evolution (loop, op);
932 access_fn = instantiate_scev (before_loop, loop, access_fn);
933 access_fns.safe_push (access_fn);
935 else if (TREE_CODE (ref) == COMPONENT_REF
936 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
938 /* For COMPONENT_REFs of records (but not unions!) use the
939 FIELD_DECL offset as constant access function so we can
940 disambiguate a[i].f1 and a[i].f2. */
941 tree off = component_ref_field_offset (ref);
942 off = size_binop (PLUS_EXPR,
943 size_binop (MULT_EXPR,
944 fold_convert (bitsizetype, off),
945 bitsize_int (BITS_PER_UNIT)),
946 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
947 access_fns.safe_push (off);
949 else
950 /* If we have an unhandled component we could not translate
951 to an access function stop analyzing. We have determined
952 our base object in this case. */
953 break;
955 ref = TREE_OPERAND (ref, 0);
958 /* If the address operand of a MEM_REF base has an evolution in the
959 analyzed nest, add it as an additional independent access-function. */
960 if (TREE_CODE (ref) == MEM_REF)
962 op = TREE_OPERAND (ref, 0);
963 access_fn = analyze_scalar_evolution (loop, op);
964 access_fn = instantiate_scev (before_loop, loop, access_fn);
965 if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
967 tree orig_type;
968 tree memoff = TREE_OPERAND (ref, 1);
969 base = initial_condition (access_fn);
970 orig_type = TREE_TYPE (base);
971 STRIP_USELESS_TYPE_CONVERSION (base);
972 split_constant_offset (base, &base, &off);
973 STRIP_USELESS_TYPE_CONVERSION (base);
974 /* Fold the MEM_REF offset into the evolutions initial
975 value to make more bases comparable. */
976 if (!integer_zerop (memoff))
978 off = size_binop (PLUS_EXPR, off,
979 fold_convert (ssizetype, memoff));
980 memoff = build_int_cst (TREE_TYPE (memoff), 0);
982 access_fn = chrec_replace_initial_condition
983 (access_fn, fold_convert (orig_type, off));
984 /* ??? This is still not a suitable base object for
985 dr_may_alias_p - the base object needs to be an
986 access that covers the object as whole. With
987 an evolution in the pointer this cannot be
988 guaranteed.
989 As a band-aid, mark the access so we can special-case
990 it in dr_may_alias_p. */
991 ref = fold_build2_loc (EXPR_LOCATION (ref),
992 MEM_REF, TREE_TYPE (ref),
993 base, memoff);
994 access_fns.safe_push (access_fn);
997 else if (DECL_P (ref))
999 /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
1000 ref = build2 (MEM_REF, TREE_TYPE (ref),
1001 build_fold_addr_expr (ref),
1002 build_int_cst (reference_alias_ptr_type (ref), 0));
1005 DR_BASE_OBJECT (dr) = ref;
1006 DR_ACCESS_FNS (dr) = access_fns;
1009 /* Extracts the alias analysis information from the memory reference DR. */
1011 static void
1012 dr_analyze_alias (struct data_reference *dr)
1014 tree ref = DR_REF (dr);
1015 tree base = get_base_address (ref), addr;
1017 if (INDIRECT_REF_P (base)
1018 || TREE_CODE (base) == MEM_REF)
1020 addr = TREE_OPERAND (base, 0);
1021 if (TREE_CODE (addr) == SSA_NAME)
1022 DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
1026 /* Frees data reference DR. */
1028 void
1029 free_data_ref (data_reference_p dr)
1031 DR_ACCESS_FNS (dr).release ();
1032 free (dr);
1035 /* Analyzes memory reference MEMREF accessed in STMT. The reference
1036 is read if IS_READ is true, write otherwise. Returns the
1037 data_reference description of MEMREF. NEST is the outermost loop
1038 in which the reference should be instantiated, LOOP is the loop in
1039 which the data reference should be analyzed. */
1041 struct data_reference *
1042 create_data_ref (loop_p nest, loop_p loop, tree memref, gimple stmt,
1043 bool is_read)
1045 struct data_reference *dr;
1047 if (dump_file && (dump_flags & TDF_DETAILS))
1049 fprintf (dump_file, "Creating dr for ");
1050 print_generic_expr (dump_file, memref, TDF_SLIM);
1051 fprintf (dump_file, "\n");
1054 dr = XCNEW (struct data_reference);
1055 DR_STMT (dr) = stmt;
1056 DR_REF (dr) = memref;
1057 DR_IS_READ (dr) = is_read;
1059 dr_analyze_innermost (dr, nest);
1060 dr_analyze_indices (dr, nest, loop);
1061 dr_analyze_alias (dr);
1063 if (dump_file && (dump_flags & TDF_DETAILS))
1065 unsigned i;
1066 fprintf (dump_file, "\tbase_address: ");
1067 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1068 fprintf (dump_file, "\n\toffset from base address: ");
1069 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1070 fprintf (dump_file, "\n\tconstant offset from base address: ");
1071 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1072 fprintf (dump_file, "\n\tstep: ");
1073 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1074 fprintf (dump_file, "\n\taligned to: ");
1075 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
1076 fprintf (dump_file, "\n\tbase_object: ");
1077 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1078 fprintf (dump_file, "\n");
1079 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
1081 fprintf (dump_file, "\tAccess function %d: ", i);
1082 print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
1086 return dr;
1089 /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
1090 expressions. */
1091 static bool
1092 dr_equal_offsets_p1 (tree offset1, tree offset2)
1094 bool res;
1096 STRIP_NOPS (offset1);
1097 STRIP_NOPS (offset2);
1099 if (offset1 == offset2)
1100 return true;
1102 if (TREE_CODE (offset1) != TREE_CODE (offset2)
1103 || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
1104 return false;
1106 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
1107 TREE_OPERAND (offset2, 0));
1109 if (!res || !BINARY_CLASS_P (offset1))
1110 return res;
1112 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
1113 TREE_OPERAND (offset2, 1));
1115 return res;
1118 /* Check if DRA and DRB have equal offsets. */
1119 bool
1120 dr_equal_offsets_p (struct data_reference *dra,
1121 struct data_reference *drb)
1123 tree offset1, offset2;
1125 offset1 = DR_OFFSET (dra);
1126 offset2 = DR_OFFSET (drb);
1128 return dr_equal_offsets_p1 (offset1, offset2);
1131 /* Returns true if FNA == FNB. */
1133 static bool
1134 affine_function_equal_p (affine_fn fna, affine_fn fnb)
1136 unsigned i, n = fna.length ();
1138 if (n != fnb.length ())
1139 return false;
1141 for (i = 0; i < n; i++)
1142 if (!operand_equal_p (fna[i], fnb[i], 0))
1143 return false;
1145 return true;
1148 /* If all the functions in CF are the same, returns one of them,
1149 otherwise returns NULL. */
1151 static affine_fn
1152 common_affine_function (conflict_function *cf)
1154 unsigned i;
1155 affine_fn comm;
1157 if (!CF_NONTRIVIAL_P (cf))
1158 return affine_fn ();
1160 comm = cf->fns[0];
1162 for (i = 1; i < cf->n; i++)
1163 if (!affine_function_equal_p (comm, cf->fns[i]))
1164 return affine_fn ();
1166 return comm;
1169 /* Returns the base of the affine function FN. */
1171 static tree
1172 affine_function_base (affine_fn fn)
1174 return fn[0];
1177 /* Returns true if FN is a constant. */
1179 static bool
1180 affine_function_constant_p (affine_fn fn)
1182 unsigned i;
1183 tree coef;
1185 for (i = 1; fn.iterate (i, &coef); i++)
1186 if (!integer_zerop (coef))
1187 return false;
1189 return true;
1192 /* Returns true if FN is the zero constant function. */
1194 static bool
1195 affine_function_zero_p (affine_fn fn)
1197 return (integer_zerop (affine_function_base (fn))
1198 && affine_function_constant_p (fn));
1201 /* Returns a signed integer type with the largest precision from TA
1202 and TB. */
1204 static tree
1205 signed_type_for_types (tree ta, tree tb)
1207 if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
1208 return signed_type_for (ta);
1209 else
1210 return signed_type_for (tb);
1213 /* Applies operation OP on affine functions FNA and FNB, and returns the
1214 result. */
1216 static affine_fn
1217 affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
1219 unsigned i, n, m;
1220 affine_fn ret;
1221 tree coef;
1223 if (fnb.length () > fna.length ())
1225 n = fna.length ();
1226 m = fnb.length ();
1228 else
1230 n = fnb.length ();
1231 m = fna.length ();
1234 ret.create (m);
1235 for (i = 0; i < n; i++)
1237 tree type = signed_type_for_types (TREE_TYPE (fna[i]),
1238 TREE_TYPE (fnb[i]));
1239 ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
1242 for (; fna.iterate (i, &coef); i++)
1243 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1244 coef, integer_zero_node));
1245 for (; fnb.iterate (i, &coef); i++)
1246 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1247 integer_zero_node, coef));
1249 return ret;
1252 /* Returns the sum of affine functions FNA and FNB. */
1254 static affine_fn
1255 affine_fn_plus (affine_fn fna, affine_fn fnb)
1257 return affine_fn_op (PLUS_EXPR, fna, fnb);
1260 /* Returns the difference of affine functions FNA and FNB. */
1262 static affine_fn
1263 affine_fn_minus (affine_fn fna, affine_fn fnb)
1265 return affine_fn_op (MINUS_EXPR, fna, fnb);
1268 /* Frees affine function FN. */
1270 static void
1271 affine_fn_free (affine_fn fn)
1273 fn.release ();
1276 /* Determine for each subscript in the data dependence relation DDR
1277 the distance. */
1279 static void
1280 compute_subscript_distance (struct data_dependence_relation *ddr)
1282 conflict_function *cf_a, *cf_b;
1283 affine_fn fn_a, fn_b, diff;
1285 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1287 unsigned int i;
1289 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1291 struct subscript *subscript;
1293 subscript = DDR_SUBSCRIPT (ddr, i);
1294 cf_a = SUB_CONFLICTS_IN_A (subscript);
1295 cf_b = SUB_CONFLICTS_IN_B (subscript);
1297 fn_a = common_affine_function (cf_a);
1298 fn_b = common_affine_function (cf_b);
1299 if (!fn_a.exists () || !fn_b.exists ())
1301 SUB_DISTANCE (subscript) = chrec_dont_know;
1302 return;
1304 diff = affine_fn_minus (fn_a, fn_b);
1306 if (affine_function_constant_p (diff))
1307 SUB_DISTANCE (subscript) = affine_function_base (diff);
1308 else
1309 SUB_DISTANCE (subscript) = chrec_dont_know;
1311 affine_fn_free (diff);
1316 /* Returns the conflict function for "unknown". */
1318 static conflict_function *
1319 conflict_fn_not_known (void)
1321 conflict_function *fn = XCNEW (conflict_function);
1322 fn->n = NOT_KNOWN;
1324 return fn;
1327 /* Returns the conflict function for "independent". */
1329 static conflict_function *
1330 conflict_fn_no_dependence (void)
1332 conflict_function *fn = XCNEW (conflict_function);
1333 fn->n = NO_DEPENDENCE;
1335 return fn;
1338 /* Returns true if the address of OBJ is invariant in LOOP. */
1340 static bool
1341 object_address_invariant_in_loop_p (const struct loop *loop, const_tree obj)
1343 while (handled_component_p (obj))
1345 if (TREE_CODE (obj) == ARRAY_REF)
1347 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1348 need to check the stride and the lower bound of the reference. */
1349 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1350 loop->num)
1351 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 3),
1352 loop->num))
1353 return false;
1355 else if (TREE_CODE (obj) == COMPONENT_REF)
1357 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1358 loop->num))
1359 return false;
1361 obj = TREE_OPERAND (obj, 0);
1364 if (!INDIRECT_REF_P (obj)
1365 && TREE_CODE (obj) != MEM_REF)
1366 return true;
1368 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
1369 loop->num);
1372 /* Returns false if we can prove that data references A and B do not alias,
1373 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
1374 considered. */
1376 bool
1377 dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
1378 bool loop_nest)
1380 tree addr_a = DR_BASE_OBJECT (a);
1381 tree addr_b = DR_BASE_OBJECT (b);
1383 /* If we are not processing a loop nest but scalar code we
1384 do not need to care about possible cross-iteration dependences
1385 and thus can process the full original reference. Do so,
1386 similar to how loop invariant motion applies extra offset-based
1387 disambiguation. */
1388 if (!loop_nest)
1390 aff_tree off1, off2;
1391 widest_int size1, size2;
1392 get_inner_reference_aff (DR_REF (a), &off1, &size1);
1393 get_inner_reference_aff (DR_REF (b), &off2, &size2);
1394 aff_combination_scale (&off1, -1);
1395 aff_combination_add (&off2, &off1);
1396 if (aff_comb_cannot_overlap_p (&off2, size1, size2))
1397 return false;
1400 /* If we had an evolution in a pointer-based MEM_REF BASE_OBJECT we
1401 do not know the size of the base-object. So we cannot do any
1402 offset/overlap based analysis but have to rely on points-to
1403 information only. */
1404 if (TREE_CODE (addr_a) == MEM_REF
1405 && TREE_CODE (TREE_OPERAND (addr_a, 0)) == SSA_NAME)
1407 /* For true dependences we can apply TBAA. */
1408 if (flag_strict_aliasing
1409 && DR_IS_WRITE (a) && DR_IS_READ (b)
1410 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1411 get_alias_set (DR_REF (b))))
1412 return false;
1413 if (TREE_CODE (addr_b) == MEM_REF)
1414 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1415 TREE_OPERAND (addr_b, 0));
1416 else
1417 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1418 build_fold_addr_expr (addr_b));
1420 else if (TREE_CODE (addr_b) == MEM_REF
1421 && TREE_CODE (TREE_OPERAND (addr_b, 0)) == SSA_NAME)
1423 /* For true dependences we can apply TBAA. */
1424 if (flag_strict_aliasing
1425 && DR_IS_WRITE (a) && DR_IS_READ (b)
1426 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1427 get_alias_set (DR_REF (b))))
1428 return false;
1429 if (TREE_CODE (addr_a) == MEM_REF)
1430 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1431 TREE_OPERAND (addr_b, 0));
1432 else
1433 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
1434 TREE_OPERAND (addr_b, 0));
1437 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1438 that is being subsetted in the loop nest. */
1439 if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
1440 return refs_output_dependent_p (addr_a, addr_b);
1441 else if (DR_IS_READ (a) && DR_IS_WRITE (b))
1442 return refs_anti_dependent_p (addr_a, addr_b);
1443 return refs_may_alias_p (addr_a, addr_b);
1446 /* Initialize a data dependence relation between data accesses A and
1447 B. NB_LOOPS is the number of loops surrounding the references: the
1448 size of the classic distance/direction vectors. */
1450 struct data_dependence_relation *
1451 initialize_data_dependence_relation (struct data_reference *a,
1452 struct data_reference *b,
1453 vec<loop_p> loop_nest)
1455 struct data_dependence_relation *res;
1456 unsigned int i;
1458 res = XNEW (struct data_dependence_relation);
1459 DDR_A (res) = a;
1460 DDR_B (res) = b;
1461 DDR_LOOP_NEST (res).create (0);
1462 DDR_REVERSED_P (res) = false;
1463 DDR_SUBSCRIPTS (res).create (0);
1464 DDR_DIR_VECTS (res).create (0);
1465 DDR_DIST_VECTS (res).create (0);
1467 if (a == NULL || b == NULL)
1469 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1470 return res;
1473 /* If the data references do not alias, then they are independent. */
1474 if (!dr_may_alias_p (a, b, loop_nest.exists ()))
1476 DDR_ARE_DEPENDENT (res) = chrec_known;
1477 return res;
1480 /* The case where the references are exactly the same. */
1481 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
1483 if (loop_nest.exists ()
1484 && !object_address_invariant_in_loop_p (loop_nest[0],
1485 DR_BASE_OBJECT (a)))
1487 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1488 return res;
1490 DDR_AFFINE_P (res) = true;
1491 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1492 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1493 DDR_LOOP_NEST (res) = loop_nest;
1494 DDR_INNER_LOOP (res) = 0;
1495 DDR_SELF_REFERENCE (res) = true;
1496 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1498 struct subscript *subscript;
1500 subscript = XNEW (struct subscript);
1501 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1502 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1503 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1504 SUB_DISTANCE (subscript) = chrec_dont_know;
1505 DDR_SUBSCRIPTS (res).safe_push (subscript);
1507 return res;
1510 /* If the references do not access the same object, we do not know
1511 whether they alias or not. */
1512 if (!operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0))
1514 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1515 return res;
1518 /* If the base of the object is not invariant in the loop nest, we cannot
1519 analyze it. TODO -- in fact, it would suffice to record that there may
1520 be arbitrary dependences in the loops where the base object varies. */
1521 if (loop_nest.exists ()
1522 && !object_address_invariant_in_loop_p (loop_nest[0],
1523 DR_BASE_OBJECT (a)))
1525 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1526 return res;
1529 /* If the number of dimensions of the access to not agree we can have
1530 a pointer access to a component of the array element type and an
1531 array access while the base-objects are still the same. Punt. */
1532 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1534 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1535 return res;
1538 DDR_AFFINE_P (res) = true;
1539 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1540 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1541 DDR_LOOP_NEST (res) = loop_nest;
1542 DDR_INNER_LOOP (res) = 0;
1543 DDR_SELF_REFERENCE (res) = false;
1545 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1547 struct subscript *subscript;
1549 subscript = XNEW (struct subscript);
1550 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1551 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1552 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1553 SUB_DISTANCE (subscript) = chrec_dont_know;
1554 DDR_SUBSCRIPTS (res).safe_push (subscript);
1557 return res;
1560 /* Frees memory used by the conflict function F. */
1562 static void
1563 free_conflict_function (conflict_function *f)
1565 unsigned i;
1567 if (CF_NONTRIVIAL_P (f))
1569 for (i = 0; i < f->n; i++)
1570 affine_fn_free (f->fns[i]);
1572 free (f);
1575 /* Frees memory used by SUBSCRIPTS. */
1577 static void
1578 free_subscripts (vec<subscript_p> subscripts)
1580 unsigned i;
1581 subscript_p s;
1583 FOR_EACH_VEC_ELT (subscripts, i, s)
1585 free_conflict_function (s->conflicting_iterations_in_a);
1586 free_conflict_function (s->conflicting_iterations_in_b);
1587 free (s);
1589 subscripts.release ();
1592 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1593 description. */
1595 static inline void
1596 finalize_ddr_dependent (struct data_dependence_relation *ddr,
1597 tree chrec)
1599 DDR_ARE_DEPENDENT (ddr) = chrec;
1600 free_subscripts (DDR_SUBSCRIPTS (ddr));
1601 DDR_SUBSCRIPTS (ddr).create (0);
1604 /* The dependence relation DDR cannot be represented by a distance
1605 vector. */
1607 static inline void
1608 non_affine_dependence_relation (struct data_dependence_relation *ddr)
1610 if (dump_file && (dump_flags & TDF_DETAILS))
1611 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
1613 DDR_AFFINE_P (ddr) = false;
1618 /* This section contains the classic Banerjee tests. */
1620 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1621 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1623 static inline bool
1624 ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1626 return (evolution_function_is_constant_p (chrec_a)
1627 && evolution_function_is_constant_p (chrec_b));
1630 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
1631 variable, i.e., if the SIV (Single Index Variable) test is true. */
1633 static bool
1634 siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1636 if ((evolution_function_is_constant_p (chrec_a)
1637 && evolution_function_is_univariate_p (chrec_b))
1638 || (evolution_function_is_constant_p (chrec_b)
1639 && evolution_function_is_univariate_p (chrec_a)))
1640 return true;
1642 if (evolution_function_is_univariate_p (chrec_a)
1643 && evolution_function_is_univariate_p (chrec_b))
1645 switch (TREE_CODE (chrec_a))
1647 case POLYNOMIAL_CHREC:
1648 switch (TREE_CODE (chrec_b))
1650 case POLYNOMIAL_CHREC:
1651 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
1652 return false;
1654 default:
1655 return true;
1658 default:
1659 return true;
1663 return false;
1666 /* Creates a conflict function with N dimensions. The affine functions
1667 in each dimension follow. */
1669 static conflict_function *
1670 conflict_fn (unsigned n, ...)
1672 unsigned i;
1673 conflict_function *ret = XCNEW (conflict_function);
1674 va_list ap;
1676 gcc_assert (0 < n && n <= MAX_DIM);
1677 va_start (ap, n);
1679 ret->n = n;
1680 for (i = 0; i < n; i++)
1681 ret->fns[i] = va_arg (ap, affine_fn);
1682 va_end (ap);
1684 return ret;
1687 /* Returns constant affine function with value CST. */
1689 static affine_fn
1690 affine_fn_cst (tree cst)
1692 affine_fn fn;
1693 fn.create (1);
1694 fn.quick_push (cst);
1695 return fn;
1698 /* Returns affine function with single variable, CST + COEF * x_DIM. */
1700 static affine_fn
1701 affine_fn_univar (tree cst, unsigned dim, tree coef)
1703 affine_fn fn;
1704 fn.create (dim + 1);
1705 unsigned i;
1707 gcc_assert (dim > 0);
1708 fn.quick_push (cst);
1709 for (i = 1; i < dim; i++)
1710 fn.quick_push (integer_zero_node);
1711 fn.quick_push (coef);
1712 return fn;
1715 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1716 *OVERLAPS_B are initialized to the functions that describe the
1717 relation between the elements accessed twice by CHREC_A and
1718 CHREC_B. For k >= 0, the following property is verified:
1720 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1722 static void
1723 analyze_ziv_subscript (tree chrec_a,
1724 tree chrec_b,
1725 conflict_function **overlaps_a,
1726 conflict_function **overlaps_b,
1727 tree *last_conflicts)
1729 tree type, difference;
1730 dependence_stats.num_ziv++;
1732 if (dump_file && (dump_flags & TDF_DETAILS))
1733 fprintf (dump_file, "(analyze_ziv_subscript \n");
1735 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1736 chrec_a = chrec_convert (type, chrec_a, NULL);
1737 chrec_b = chrec_convert (type, chrec_b, NULL);
1738 difference = chrec_fold_minus (type, chrec_a, chrec_b);
1740 switch (TREE_CODE (difference))
1742 case INTEGER_CST:
1743 if (integer_zerop (difference))
1745 /* The difference is equal to zero: the accessed index
1746 overlaps for each iteration in the loop. */
1747 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1748 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1749 *last_conflicts = chrec_dont_know;
1750 dependence_stats.num_ziv_dependent++;
1752 else
1754 /* The accesses do not overlap. */
1755 *overlaps_a = conflict_fn_no_dependence ();
1756 *overlaps_b = conflict_fn_no_dependence ();
1757 *last_conflicts = integer_zero_node;
1758 dependence_stats.num_ziv_independent++;
1760 break;
1762 default:
1763 /* We're not sure whether the indexes overlap. For the moment,
1764 conservatively answer "don't know". */
1765 if (dump_file && (dump_flags & TDF_DETAILS))
1766 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
1768 *overlaps_a = conflict_fn_not_known ();
1769 *overlaps_b = conflict_fn_not_known ();
1770 *last_conflicts = chrec_dont_know;
1771 dependence_stats.num_ziv_unimplemented++;
1772 break;
1775 if (dump_file && (dump_flags & TDF_DETAILS))
1776 fprintf (dump_file, ")\n");
1779 /* Similar to max_stmt_executions_int, but returns the bound as a tree,
1780 and only if it fits to the int type. If this is not the case, or the
1781 bound on the number of iterations of LOOP could not be derived, returns
1782 chrec_dont_know. */
1784 static tree
1785 max_stmt_executions_tree (struct loop *loop)
1787 widest_int nit;
1789 if (!max_stmt_executions (loop, &nit))
1790 return chrec_dont_know;
1792 if (!wi::fits_to_tree_p (nit, unsigned_type_node))
1793 return chrec_dont_know;
1795 return wide_int_to_tree (unsigned_type_node, nit);
1798 /* Determine whether the CHREC is always positive/negative. If the expression
1799 cannot be statically analyzed, return false, otherwise set the answer into
1800 VALUE. */
1802 static bool
1803 chrec_is_positive (tree chrec, bool *value)
1805 bool value0, value1, value2;
1806 tree end_value, nb_iter;
1808 switch (TREE_CODE (chrec))
1810 case POLYNOMIAL_CHREC:
1811 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
1812 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
1813 return false;
1815 /* FIXME -- overflows. */
1816 if (value0 == value1)
1818 *value = value0;
1819 return true;
1822 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1823 and the proof consists in showing that the sign never
1824 changes during the execution of the loop, from 0 to
1825 loop->nb_iterations. */
1826 if (!evolution_function_is_affine_p (chrec))
1827 return false;
1829 nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
1830 if (chrec_contains_undetermined (nb_iter))
1831 return false;
1833 #if 0
1834 /* TODO -- If the test is after the exit, we may decrease the number of
1835 iterations by one. */
1836 if (after_exit)
1837 nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
1838 #endif
1840 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
1842 if (!chrec_is_positive (end_value, &value2))
1843 return false;
1845 *value = value0;
1846 return value0 == value1;
1848 case INTEGER_CST:
1849 switch (tree_int_cst_sgn (chrec))
1851 case -1:
1852 *value = false;
1853 break;
1854 case 1:
1855 *value = true;
1856 break;
1857 default:
1858 return false;
1860 return true;
1862 default:
1863 return false;
1868 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1869 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1870 *OVERLAPS_B are initialized to the functions that describe the
1871 relation between the elements accessed twice by CHREC_A and
1872 CHREC_B. For k >= 0, the following property is verified:
1874 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1876 static void
1877 analyze_siv_subscript_cst_affine (tree chrec_a,
1878 tree chrec_b,
1879 conflict_function **overlaps_a,
1880 conflict_function **overlaps_b,
1881 tree *last_conflicts)
1883 bool value0, value1, value2;
1884 tree type, difference, tmp;
1886 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1887 chrec_a = chrec_convert (type, chrec_a, NULL);
1888 chrec_b = chrec_convert (type, chrec_b, NULL);
1889 difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
1891 /* Special case overlap in the first iteration. */
1892 if (integer_zerop (difference))
1894 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1895 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1896 *last_conflicts = integer_one_node;
1897 return;
1900 if (!chrec_is_positive (initial_condition (difference), &value0))
1902 if (dump_file && (dump_flags & TDF_DETAILS))
1903 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
1905 dependence_stats.num_siv_unimplemented++;
1906 *overlaps_a = conflict_fn_not_known ();
1907 *overlaps_b = conflict_fn_not_known ();
1908 *last_conflicts = chrec_dont_know;
1909 return;
1911 else
1913 if (value0 == false)
1915 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
1917 if (dump_file && (dump_flags & TDF_DETAILS))
1918 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1920 *overlaps_a = conflict_fn_not_known ();
1921 *overlaps_b = conflict_fn_not_known ();
1922 *last_conflicts = chrec_dont_know;
1923 dependence_stats.num_siv_unimplemented++;
1924 return;
1926 else
1928 if (value1 == true)
1930 /* Example:
1931 chrec_a = 12
1932 chrec_b = {10, +, 1}
1935 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1937 HOST_WIDE_INT numiter;
1938 struct loop *loop = get_chrec_loop (chrec_b);
1940 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1941 tmp = fold_build2 (EXACT_DIV_EXPR, type,
1942 fold_build1 (ABS_EXPR, type, difference),
1943 CHREC_RIGHT (chrec_b));
1944 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1945 *last_conflicts = integer_one_node;
1948 /* Perform weak-zero siv test to see if overlap is
1949 outside the loop bounds. */
1950 numiter = max_stmt_executions_int (loop);
1952 if (numiter >= 0
1953 && compare_tree_int (tmp, numiter) > 0)
1955 free_conflict_function (*overlaps_a);
1956 free_conflict_function (*overlaps_b);
1957 *overlaps_a = conflict_fn_no_dependence ();
1958 *overlaps_b = conflict_fn_no_dependence ();
1959 *last_conflicts = integer_zero_node;
1960 dependence_stats.num_siv_independent++;
1961 return;
1963 dependence_stats.num_siv_dependent++;
1964 return;
1967 /* When the step does not divide the difference, there are
1968 no overlaps. */
1969 else
1971 *overlaps_a = conflict_fn_no_dependence ();
1972 *overlaps_b = conflict_fn_no_dependence ();
1973 *last_conflicts = integer_zero_node;
1974 dependence_stats.num_siv_independent++;
1975 return;
1979 else
1981 /* Example:
1982 chrec_a = 12
1983 chrec_b = {10, +, -1}
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++;
1990 return;
1994 else
1996 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
1998 if (dump_file && (dump_flags & TDF_DETAILS))
1999 fprintf (dump_file, "siv test failed: chrec not positive.\n");
2001 *overlaps_a = conflict_fn_not_known ();
2002 *overlaps_b = conflict_fn_not_known ();
2003 *last_conflicts = chrec_dont_know;
2004 dependence_stats.num_siv_unimplemented++;
2005 return;
2007 else
2009 if (value2 == false)
2011 /* Example:
2012 chrec_a = 3
2013 chrec_b = {10, +, -1}
2015 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2017 HOST_WIDE_INT numiter;
2018 struct loop *loop = get_chrec_loop (chrec_b);
2020 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2021 tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
2022 CHREC_RIGHT (chrec_b));
2023 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
2024 *last_conflicts = integer_one_node;
2026 /* Perform weak-zero siv test to see if overlap is
2027 outside the loop bounds. */
2028 numiter = max_stmt_executions_int (loop);
2030 if (numiter >= 0
2031 && compare_tree_int (tmp, numiter) > 0)
2033 free_conflict_function (*overlaps_a);
2034 free_conflict_function (*overlaps_b);
2035 *overlaps_a = conflict_fn_no_dependence ();
2036 *overlaps_b = conflict_fn_no_dependence ();
2037 *last_conflicts = integer_zero_node;
2038 dependence_stats.num_siv_independent++;
2039 return;
2041 dependence_stats.num_siv_dependent++;
2042 return;
2045 /* When the step does not divide the difference, there
2046 are no overlaps. */
2047 else
2049 *overlaps_a = conflict_fn_no_dependence ();
2050 *overlaps_b = conflict_fn_no_dependence ();
2051 *last_conflicts = integer_zero_node;
2052 dependence_stats.num_siv_independent++;
2053 return;
2056 else
2058 /* Example:
2059 chrec_a = 3
2060 chrec_b = {4, +, 1}
2062 In this case, chrec_a will not overlap with chrec_b. */
2063 *overlaps_a = conflict_fn_no_dependence ();
2064 *overlaps_b = conflict_fn_no_dependence ();
2065 *last_conflicts = integer_zero_node;
2066 dependence_stats.num_siv_independent++;
2067 return;
2074 /* Helper recursive function for initializing the matrix A. Returns
2075 the initial value of CHREC. */
2077 static tree
2078 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2080 gcc_assert (chrec);
2082 switch (TREE_CODE (chrec))
2084 case POLYNOMIAL_CHREC:
2085 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST);
2087 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2088 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2090 case PLUS_EXPR:
2091 case MULT_EXPR:
2092 case MINUS_EXPR:
2094 tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2095 tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
2097 return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
2100 case NOP_EXPR:
2102 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2103 return chrec_convert (chrec_type (chrec), op, NULL);
2106 case BIT_NOT_EXPR:
2108 /* Handle ~X as -1 - X. */
2109 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2110 return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
2111 build_int_cst (TREE_TYPE (chrec), -1), op);
2114 case INTEGER_CST:
2115 return chrec;
2117 default:
2118 gcc_unreachable ();
2119 return NULL_TREE;
2123 #define FLOOR_DIV(x,y) ((x) / (y))
2125 /* Solves the special case of the Diophantine equation:
2126 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2128 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2129 number of iterations that loops X and Y run. The overlaps will be
2130 constructed as evolutions in dimension DIM. */
2132 static void
2133 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2134 affine_fn *overlaps_a,
2135 affine_fn *overlaps_b,
2136 tree *last_conflicts, int dim)
2138 if (((step_a > 0 && step_b > 0)
2139 || (step_a < 0 && step_b < 0)))
2141 int step_overlaps_a, step_overlaps_b;
2142 int gcd_steps_a_b, last_conflict, tau2;
2144 gcd_steps_a_b = gcd (step_a, step_b);
2145 step_overlaps_a = step_b / gcd_steps_a_b;
2146 step_overlaps_b = step_a / gcd_steps_a_b;
2148 if (niter > 0)
2150 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2151 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2152 last_conflict = tau2;
2153 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2155 else
2156 *last_conflicts = chrec_dont_know;
2158 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
2159 build_int_cst (NULL_TREE,
2160 step_overlaps_a));
2161 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
2162 build_int_cst (NULL_TREE,
2163 step_overlaps_b));
2166 else
2168 *overlaps_a = affine_fn_cst (integer_zero_node);
2169 *overlaps_b = affine_fn_cst (integer_zero_node);
2170 *last_conflicts = integer_zero_node;
2174 /* Solves the special case of a Diophantine equation where CHREC_A is
2175 an affine bivariate function, and CHREC_B is an affine univariate
2176 function. For example,
2178 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2180 has the following overlapping functions:
2182 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2183 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2184 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2186 FORNOW: This is a specialized implementation for a case occurring in
2187 a common benchmark. Implement the general algorithm. */
2189 static void
2190 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2191 conflict_function **overlaps_a,
2192 conflict_function **overlaps_b,
2193 tree *last_conflicts)
2195 bool xz_p, yz_p, xyz_p;
2196 int step_x, step_y, step_z;
2197 HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
2198 affine_fn overlaps_a_xz, overlaps_b_xz;
2199 affine_fn overlaps_a_yz, overlaps_b_yz;
2200 affine_fn overlaps_a_xyz, overlaps_b_xyz;
2201 affine_fn ova1, ova2, ovb;
2202 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
2204 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2205 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2206 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2208 niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
2209 niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
2210 niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
2212 if (niter_x < 0 || niter_y < 0 || niter_z < 0)
2214 if (dump_file && (dump_flags & TDF_DETAILS))
2215 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
2217 *overlaps_a = conflict_fn_not_known ();
2218 *overlaps_b = conflict_fn_not_known ();
2219 *last_conflicts = chrec_dont_know;
2220 return;
2223 niter = MIN (niter_x, niter_z);
2224 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2225 &overlaps_a_xz,
2226 &overlaps_b_xz,
2227 &last_conflicts_xz, 1);
2228 niter = MIN (niter_y, niter_z);
2229 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2230 &overlaps_a_yz,
2231 &overlaps_b_yz,
2232 &last_conflicts_yz, 2);
2233 niter = MIN (niter_x, niter_z);
2234 niter = MIN (niter_y, niter);
2235 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2236 &overlaps_a_xyz,
2237 &overlaps_b_xyz,
2238 &last_conflicts_xyz, 3);
2240 xz_p = !integer_zerop (last_conflicts_xz);
2241 yz_p = !integer_zerop (last_conflicts_yz);
2242 xyz_p = !integer_zerop (last_conflicts_xyz);
2244 if (xz_p || yz_p || xyz_p)
2246 ova1 = affine_fn_cst (integer_zero_node);
2247 ova2 = affine_fn_cst (integer_zero_node);
2248 ovb = affine_fn_cst (integer_zero_node);
2249 if (xz_p)
2251 affine_fn t0 = ova1;
2252 affine_fn t2 = ovb;
2254 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
2255 ovb = affine_fn_plus (ovb, overlaps_b_xz);
2256 affine_fn_free (t0);
2257 affine_fn_free (t2);
2258 *last_conflicts = last_conflicts_xz;
2260 if (yz_p)
2262 affine_fn t0 = ova2;
2263 affine_fn t2 = ovb;
2265 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
2266 ovb = affine_fn_plus (ovb, overlaps_b_yz);
2267 affine_fn_free (t0);
2268 affine_fn_free (t2);
2269 *last_conflicts = last_conflicts_yz;
2271 if (xyz_p)
2273 affine_fn t0 = ova1;
2274 affine_fn t2 = ova2;
2275 affine_fn t4 = ovb;
2277 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
2278 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
2279 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
2280 affine_fn_free (t0);
2281 affine_fn_free (t2);
2282 affine_fn_free (t4);
2283 *last_conflicts = last_conflicts_xyz;
2285 *overlaps_a = conflict_fn (2, ova1, ova2);
2286 *overlaps_b = conflict_fn (1, ovb);
2288 else
2290 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2291 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2292 *last_conflicts = integer_zero_node;
2295 affine_fn_free (overlaps_a_xz);
2296 affine_fn_free (overlaps_b_xz);
2297 affine_fn_free (overlaps_a_yz);
2298 affine_fn_free (overlaps_b_yz);
2299 affine_fn_free (overlaps_a_xyz);
2300 affine_fn_free (overlaps_b_xyz);
2303 /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2305 static void
2306 lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
2307 int size)
2309 memcpy (vec2, vec1, size * sizeof (*vec1));
2312 /* Copy the elements of M x N matrix MAT1 to MAT2. */
2314 static void
2315 lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
2316 int m, int n)
2318 int i;
2320 for (i = 0; i < m; i++)
2321 lambda_vector_copy (mat1[i], mat2[i], n);
2324 /* Store the N x N identity matrix in MAT. */
2326 static void
2327 lambda_matrix_id (lambda_matrix mat, int size)
2329 int i, j;
2331 for (i = 0; i < size; i++)
2332 for (j = 0; j < size; j++)
2333 mat[i][j] = (i == j) ? 1 : 0;
2336 /* Return the first nonzero element of vector VEC1 between START and N.
2337 We must have START <= N. Returns N if VEC1 is the zero vector. */
2339 static int
2340 lambda_vector_first_nz (lambda_vector vec1, int n, int start)
2342 int j = start;
2343 while (j < n && vec1[j] == 0)
2344 j++;
2345 return j;
2348 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2349 R2 = R2 + CONST1 * R1. */
2351 static void
2352 lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2, int const1)
2354 int i;
2356 if (const1 == 0)
2357 return;
2359 for (i = 0; i < n; i++)
2360 mat[r2][i] += const1 * mat[r1][i];
2363 /* Swap rows R1 and R2 in matrix MAT. */
2365 static void
2366 lambda_matrix_row_exchange (lambda_matrix mat, int r1, int r2)
2368 lambda_vector row;
2370 row = mat[r1];
2371 mat[r1] = mat[r2];
2372 mat[r2] = row;
2375 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
2376 and store the result in VEC2. */
2378 static void
2379 lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
2380 int size, int const1)
2382 int i;
2384 if (const1 == 0)
2385 lambda_vector_clear (vec2, size);
2386 else
2387 for (i = 0; i < size; i++)
2388 vec2[i] = const1 * vec1[i];
2391 /* Negate vector VEC1 with length SIZE and store it in VEC2. */
2393 static void
2394 lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
2395 int size)
2397 lambda_vector_mult_const (vec1, vec2, size, -1);
2400 /* Negate row R1 of matrix MAT which has N columns. */
2402 static void
2403 lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
2405 lambda_vector_negate (mat[r1], mat[r1], n);
2408 /* Return true if two vectors are equal. */
2410 static bool
2411 lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
2413 int i;
2414 for (i = 0; i < size; i++)
2415 if (vec1[i] != vec2[i])
2416 return false;
2417 return true;
2420 /* Given an M x N integer matrix A, this function determines an M x
2421 M unimodular matrix U, and an M x N echelon matrix S such that
2422 "U.A = S". This decomposition is also known as "right Hermite".
2424 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2425 Restructuring Compilers" Utpal Banerjee. */
2427 static void
2428 lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
2429 lambda_matrix S, lambda_matrix U)
2431 int i, j, i0 = 0;
2433 lambda_matrix_copy (A, S, m, n);
2434 lambda_matrix_id (U, m);
2436 for (j = 0; j < n; j++)
2438 if (lambda_vector_first_nz (S[j], m, i0) < m)
2440 ++i0;
2441 for (i = m - 1; i >= i0; i--)
2443 while (S[i][j] != 0)
2445 int sigma, factor, a, b;
2447 a = S[i-1][j];
2448 b = S[i][j];
2449 sigma = (a * b < 0) ? -1: 1;
2450 a = abs (a);
2451 b = abs (b);
2452 factor = sigma * (a / b);
2454 lambda_matrix_row_add (S, n, i, i-1, -factor);
2455 lambda_matrix_row_exchange (S, i, i-1);
2457 lambda_matrix_row_add (U, m, i, i-1, -factor);
2458 lambda_matrix_row_exchange (U, i, i-1);
2465 /* Determines the overlapping elements due to accesses CHREC_A and
2466 CHREC_B, that are affine functions. This function cannot handle
2467 symbolic evolution functions, ie. when initial conditions are
2468 parameters, because it uses lambda matrices of integers. */
2470 static void
2471 analyze_subscript_affine_affine (tree chrec_a,
2472 tree chrec_b,
2473 conflict_function **overlaps_a,
2474 conflict_function **overlaps_b,
2475 tree *last_conflicts)
2477 unsigned nb_vars_a, nb_vars_b, dim;
2478 HOST_WIDE_INT init_a, init_b, gamma, gcd_alpha_beta;
2479 lambda_matrix A, U, S;
2480 struct obstack scratch_obstack;
2482 if (eq_evolutions_p (chrec_a, chrec_b))
2484 /* The accessed index overlaps for each iteration in the
2485 loop. */
2486 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2487 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2488 *last_conflicts = chrec_dont_know;
2489 return;
2491 if (dump_file && (dump_flags & TDF_DETAILS))
2492 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2494 /* For determining the initial intersection, we have to solve a
2495 Diophantine equation. This is the most time consuming part.
2497 For answering to the question: "Is there a dependence?" we have
2498 to prove that there exists a solution to the Diophantine
2499 equation, and that the solution is in the iteration domain,
2500 i.e. the solution is positive or zero, and that the solution
2501 happens before the upper bound loop.nb_iterations. Otherwise
2502 there is no dependence. This function outputs a description of
2503 the iterations that hold the intersections. */
2505 nb_vars_a = nb_vars_in_chrec (chrec_a);
2506 nb_vars_b = nb_vars_in_chrec (chrec_b);
2508 gcc_obstack_init (&scratch_obstack);
2510 dim = nb_vars_a + nb_vars_b;
2511 U = lambda_matrix_new (dim, dim, &scratch_obstack);
2512 A = lambda_matrix_new (dim, 1, &scratch_obstack);
2513 S = lambda_matrix_new (dim, 1, &scratch_obstack);
2515 init_a = int_cst_value (initialize_matrix_A (A, chrec_a, 0, 1));
2516 init_b = int_cst_value (initialize_matrix_A (A, chrec_b, nb_vars_a, -1));
2517 gamma = init_b - init_a;
2519 /* Don't do all the hard work of solving the Diophantine equation
2520 when we already know the solution: for example,
2521 | {3, +, 1}_1
2522 | {3, +, 4}_2
2523 | gamma = 3 - 3 = 0.
2524 Then the first overlap occurs during the first iterations:
2525 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2527 if (gamma == 0)
2529 if (nb_vars_a == 1 && nb_vars_b == 1)
2531 HOST_WIDE_INT step_a, step_b;
2532 HOST_WIDE_INT niter, niter_a, niter_b;
2533 affine_fn ova, ovb;
2535 niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
2536 niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
2537 niter = MIN (niter_a, niter_b);
2538 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2539 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2541 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2542 &ova, &ovb,
2543 last_conflicts, 1);
2544 *overlaps_a = conflict_fn (1, ova);
2545 *overlaps_b = conflict_fn (1, ovb);
2548 else if (nb_vars_a == 2 && nb_vars_b == 1)
2549 compute_overlap_steps_for_affine_1_2
2550 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2552 else if (nb_vars_a == 1 && nb_vars_b == 2)
2553 compute_overlap_steps_for_affine_1_2
2554 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2556 else
2558 if (dump_file && (dump_flags & TDF_DETAILS))
2559 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
2560 *overlaps_a = conflict_fn_not_known ();
2561 *overlaps_b = conflict_fn_not_known ();
2562 *last_conflicts = chrec_dont_know;
2564 goto end_analyze_subs_aa;
2567 /* U.A = S */
2568 lambda_matrix_right_hermite (A, dim, 1, S, U);
2570 if (S[0][0] < 0)
2572 S[0][0] *= -1;
2573 lambda_matrix_row_negate (U, dim, 0);
2575 gcd_alpha_beta = S[0][0];
2577 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2578 but that is a quite strange case. Instead of ICEing, answer
2579 don't know. */
2580 if (gcd_alpha_beta == 0)
2582 *overlaps_a = conflict_fn_not_known ();
2583 *overlaps_b = conflict_fn_not_known ();
2584 *last_conflicts = chrec_dont_know;
2585 goto end_analyze_subs_aa;
2588 /* The classic "gcd-test". */
2589 if (!int_divides_p (gcd_alpha_beta, gamma))
2591 /* The "gcd-test" has determined that there is no integer
2592 solution, i.e. there is no dependence. */
2593 *overlaps_a = conflict_fn_no_dependence ();
2594 *overlaps_b = conflict_fn_no_dependence ();
2595 *last_conflicts = integer_zero_node;
2598 /* Both access functions are univariate. This includes SIV and MIV cases. */
2599 else if (nb_vars_a == 1 && nb_vars_b == 1)
2601 /* Both functions should have the same evolution sign. */
2602 if (((A[0][0] > 0 && -A[1][0] > 0)
2603 || (A[0][0] < 0 && -A[1][0] < 0)))
2605 /* The solutions are given by:
2607 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2608 | [u21 u22] [y0]
2610 For a given integer t. Using the following variables,
2612 | i0 = u11 * gamma / gcd_alpha_beta
2613 | j0 = u12 * gamma / gcd_alpha_beta
2614 | i1 = u21
2615 | j1 = u22
2617 the solutions are:
2619 | x0 = i0 + i1 * t,
2620 | y0 = j0 + j1 * t. */
2621 HOST_WIDE_INT i0, j0, i1, j1;
2623 i0 = U[0][0] * gamma / gcd_alpha_beta;
2624 j0 = U[0][1] * gamma / gcd_alpha_beta;
2625 i1 = U[1][0];
2626 j1 = U[1][1];
2628 if ((i1 == 0 && i0 < 0)
2629 || (j1 == 0 && j0 < 0))
2631 /* There is no solution.
2632 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2633 falls in here, but for the moment we don't look at the
2634 upper bound of the iteration domain. */
2635 *overlaps_a = conflict_fn_no_dependence ();
2636 *overlaps_b = conflict_fn_no_dependence ();
2637 *last_conflicts = integer_zero_node;
2638 goto end_analyze_subs_aa;
2641 if (i1 > 0 && j1 > 0)
2643 HOST_WIDE_INT niter_a
2644 = max_stmt_executions_int (get_chrec_loop (chrec_a));
2645 HOST_WIDE_INT niter_b
2646 = max_stmt_executions_int (get_chrec_loop (chrec_b));
2647 HOST_WIDE_INT niter = MIN (niter_a, niter_b);
2649 /* (X0, Y0) is a solution of the Diophantine equation:
2650 "chrec_a (X0) = chrec_b (Y0)". */
2651 HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
2652 CEIL (-j0, j1));
2653 HOST_WIDE_INT x0 = i1 * tau1 + i0;
2654 HOST_WIDE_INT y0 = j1 * tau1 + j0;
2656 /* (X1, Y1) is the smallest positive solution of the eq
2657 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2658 first conflict occurs. */
2659 HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
2660 HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
2661 HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
2663 if (niter > 0)
2665 HOST_WIDE_INT tau2 = MIN (FLOOR_DIV (niter - i0, i1),
2666 FLOOR_DIV (niter - j0, j1));
2667 HOST_WIDE_INT last_conflict = tau2 - (x1 - i0)/i1;
2669 /* If the overlap occurs outside of the bounds of the
2670 loop, there is no dependence. */
2671 if (x1 >= niter || y1 >= niter)
2673 *overlaps_a = conflict_fn_no_dependence ();
2674 *overlaps_b = conflict_fn_no_dependence ();
2675 *last_conflicts = integer_zero_node;
2676 goto end_analyze_subs_aa;
2678 else
2679 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2681 else
2682 *last_conflicts = chrec_dont_know;
2684 *overlaps_a
2685 = conflict_fn (1,
2686 affine_fn_univar (build_int_cst (NULL_TREE, x1),
2688 build_int_cst (NULL_TREE, i1)));
2689 *overlaps_b
2690 = conflict_fn (1,
2691 affine_fn_univar (build_int_cst (NULL_TREE, y1),
2693 build_int_cst (NULL_TREE, j1)));
2695 else
2697 /* FIXME: For the moment, the upper bound of the
2698 iteration domain for i and j is not checked. */
2699 if (dump_file && (dump_flags & TDF_DETAILS))
2700 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2701 *overlaps_a = conflict_fn_not_known ();
2702 *overlaps_b = conflict_fn_not_known ();
2703 *last_conflicts = chrec_dont_know;
2706 else
2708 if (dump_file && (dump_flags & TDF_DETAILS))
2709 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2710 *overlaps_a = conflict_fn_not_known ();
2711 *overlaps_b = conflict_fn_not_known ();
2712 *last_conflicts = chrec_dont_know;
2715 else
2717 if (dump_file && (dump_flags & TDF_DETAILS))
2718 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2719 *overlaps_a = conflict_fn_not_known ();
2720 *overlaps_b = conflict_fn_not_known ();
2721 *last_conflicts = chrec_dont_know;
2724 end_analyze_subs_aa:
2725 obstack_free (&scratch_obstack, NULL);
2726 if (dump_file && (dump_flags & TDF_DETAILS))
2728 fprintf (dump_file, " (overlaps_a = ");
2729 dump_conflict_function (dump_file, *overlaps_a);
2730 fprintf (dump_file, ")\n (overlaps_b = ");
2731 dump_conflict_function (dump_file, *overlaps_b);
2732 fprintf (dump_file, "))\n");
2736 /* Returns true when analyze_subscript_affine_affine can be used for
2737 determining the dependence relation between chrec_a and chrec_b,
2738 that contain symbols. This function modifies chrec_a and chrec_b
2739 such that the analysis result is the same, and such that they don't
2740 contain symbols, and then can safely be passed to the analyzer.
2742 Example: The analysis of the following tuples of evolutions produce
2743 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2744 vs. {0, +, 1}_1
2746 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2747 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2750 static bool
2751 can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
2753 tree diff, type, left_a, left_b, right_b;
2755 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
2756 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
2757 /* FIXME: For the moment not handled. Might be refined later. */
2758 return false;
2760 type = chrec_type (*chrec_a);
2761 left_a = CHREC_LEFT (*chrec_a);
2762 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
2763 diff = chrec_fold_minus (type, left_a, left_b);
2765 if (!evolution_function_is_constant_p (diff))
2766 return false;
2768 if (dump_file && (dump_flags & TDF_DETAILS))
2769 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
2771 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
2772 diff, CHREC_RIGHT (*chrec_a));
2773 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
2774 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
2775 build_int_cst (type, 0),
2776 right_b);
2777 return true;
2780 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2781 *OVERLAPS_B are initialized to the functions that describe the
2782 relation between the elements accessed twice by CHREC_A and
2783 CHREC_B. For k >= 0, the following property is verified:
2785 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2787 static void
2788 analyze_siv_subscript (tree chrec_a,
2789 tree chrec_b,
2790 conflict_function **overlaps_a,
2791 conflict_function **overlaps_b,
2792 tree *last_conflicts,
2793 int loop_nest_num)
2795 dependence_stats.num_siv++;
2797 if (dump_file && (dump_flags & TDF_DETAILS))
2798 fprintf (dump_file, "(analyze_siv_subscript \n");
2800 if (evolution_function_is_constant_p (chrec_a)
2801 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2802 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
2803 overlaps_a, overlaps_b, last_conflicts);
2805 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2806 && evolution_function_is_constant_p (chrec_b))
2807 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
2808 overlaps_b, overlaps_a, last_conflicts);
2810 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2811 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2813 if (!chrec_contains_symbols (chrec_a)
2814 && !chrec_contains_symbols (chrec_b))
2816 analyze_subscript_affine_affine (chrec_a, chrec_b,
2817 overlaps_a, overlaps_b,
2818 last_conflicts);
2820 if (CF_NOT_KNOWN_P (*overlaps_a)
2821 || CF_NOT_KNOWN_P (*overlaps_b))
2822 dependence_stats.num_siv_unimplemented++;
2823 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2824 || CF_NO_DEPENDENCE_P (*overlaps_b))
2825 dependence_stats.num_siv_independent++;
2826 else
2827 dependence_stats.num_siv_dependent++;
2829 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
2830 &chrec_b))
2832 analyze_subscript_affine_affine (chrec_a, chrec_b,
2833 overlaps_a, overlaps_b,
2834 last_conflicts);
2836 if (CF_NOT_KNOWN_P (*overlaps_a)
2837 || CF_NOT_KNOWN_P (*overlaps_b))
2838 dependence_stats.num_siv_unimplemented++;
2839 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2840 || CF_NO_DEPENDENCE_P (*overlaps_b))
2841 dependence_stats.num_siv_independent++;
2842 else
2843 dependence_stats.num_siv_dependent++;
2845 else
2846 goto siv_subscript_dontknow;
2849 else
2851 siv_subscript_dontknow:;
2852 if (dump_file && (dump_flags & TDF_DETAILS))
2853 fprintf (dump_file, " siv test failed: unimplemented");
2854 *overlaps_a = conflict_fn_not_known ();
2855 *overlaps_b = conflict_fn_not_known ();
2856 *last_conflicts = chrec_dont_know;
2857 dependence_stats.num_siv_unimplemented++;
2860 if (dump_file && (dump_flags & TDF_DETAILS))
2861 fprintf (dump_file, ")\n");
2864 /* Returns false if we can prove that the greatest common divisor of the steps
2865 of CHREC does not divide CST, false otherwise. */
2867 static bool
2868 gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
2870 HOST_WIDE_INT cd = 0, val;
2871 tree step;
2873 if (!tree_fits_shwi_p (cst))
2874 return true;
2875 val = tree_to_shwi (cst);
2877 while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
2879 step = CHREC_RIGHT (chrec);
2880 if (!tree_fits_shwi_p (step))
2881 return true;
2882 cd = gcd (cd, tree_to_shwi (step));
2883 chrec = CHREC_LEFT (chrec);
2886 return val % cd == 0;
2889 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
2890 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2891 functions that describe the relation between the elements accessed
2892 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2893 is verified:
2895 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2897 static void
2898 analyze_miv_subscript (tree chrec_a,
2899 tree chrec_b,
2900 conflict_function **overlaps_a,
2901 conflict_function **overlaps_b,
2902 tree *last_conflicts,
2903 struct loop *loop_nest)
2905 tree type, difference;
2907 dependence_stats.num_miv++;
2908 if (dump_file && (dump_flags & TDF_DETAILS))
2909 fprintf (dump_file, "(analyze_miv_subscript \n");
2911 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
2912 chrec_a = chrec_convert (type, chrec_a, NULL);
2913 chrec_b = chrec_convert (type, chrec_b, NULL);
2914 difference = chrec_fold_minus (type, chrec_a, chrec_b);
2916 if (eq_evolutions_p (chrec_a, chrec_b))
2918 /* Access functions are the same: all the elements are accessed
2919 in the same order. */
2920 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2921 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2922 *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
2923 dependence_stats.num_miv_dependent++;
2926 else if (evolution_function_is_constant_p (difference)
2927 /* For the moment, the following is verified:
2928 evolution_function_is_affine_multivariate_p (chrec_a,
2929 loop_nest->num) */
2930 && !gcd_of_steps_may_divide_p (chrec_a, difference))
2932 /* testsuite/.../ssa-chrec-33.c
2933 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2935 The difference is 1, and all the evolution steps are multiples
2936 of 2, consequently there are no overlapping elements. */
2937 *overlaps_a = conflict_fn_no_dependence ();
2938 *overlaps_b = conflict_fn_no_dependence ();
2939 *last_conflicts = integer_zero_node;
2940 dependence_stats.num_miv_independent++;
2943 else if (evolution_function_is_affine_multivariate_p (chrec_a, loop_nest->num)
2944 && !chrec_contains_symbols (chrec_a)
2945 && evolution_function_is_affine_multivariate_p (chrec_b, loop_nest->num)
2946 && !chrec_contains_symbols (chrec_b))
2948 /* testsuite/.../ssa-chrec-35.c
2949 {0, +, 1}_2 vs. {0, +, 1}_3
2950 the overlapping elements are respectively located at iterations:
2951 {0, +, 1}_x and {0, +, 1}_x,
2952 in other words, we have the equality:
2953 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2955 Other examples:
2956 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2957 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2959 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2960 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2962 analyze_subscript_affine_affine (chrec_a, chrec_b,
2963 overlaps_a, overlaps_b, last_conflicts);
2965 if (CF_NOT_KNOWN_P (*overlaps_a)
2966 || CF_NOT_KNOWN_P (*overlaps_b))
2967 dependence_stats.num_miv_unimplemented++;
2968 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2969 || CF_NO_DEPENDENCE_P (*overlaps_b))
2970 dependence_stats.num_miv_independent++;
2971 else
2972 dependence_stats.num_miv_dependent++;
2975 else
2977 /* When the analysis is too difficult, answer "don't know". */
2978 if (dump_file && (dump_flags & TDF_DETAILS))
2979 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
2981 *overlaps_a = conflict_fn_not_known ();
2982 *overlaps_b = conflict_fn_not_known ();
2983 *last_conflicts = chrec_dont_know;
2984 dependence_stats.num_miv_unimplemented++;
2987 if (dump_file && (dump_flags & TDF_DETAILS))
2988 fprintf (dump_file, ")\n");
2991 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
2992 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
2993 OVERLAP_ITERATIONS_B are initialized with two functions that
2994 describe the iterations that contain conflicting elements.
2996 Remark: For an integer k >= 0, the following equality is true:
2998 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
3001 static void
3002 analyze_overlapping_iterations (tree chrec_a,
3003 tree chrec_b,
3004 conflict_function **overlap_iterations_a,
3005 conflict_function **overlap_iterations_b,
3006 tree *last_conflicts, struct loop *loop_nest)
3008 unsigned int lnn = loop_nest->num;
3010 dependence_stats.num_subscript_tests++;
3012 if (dump_file && (dump_flags & TDF_DETAILS))
3014 fprintf (dump_file, "(analyze_overlapping_iterations \n");
3015 fprintf (dump_file, " (chrec_a = ");
3016 print_generic_expr (dump_file, chrec_a, 0);
3017 fprintf (dump_file, ")\n (chrec_b = ");
3018 print_generic_expr (dump_file, chrec_b, 0);
3019 fprintf (dump_file, ")\n");
3022 if (chrec_a == NULL_TREE
3023 || chrec_b == NULL_TREE
3024 || chrec_contains_undetermined (chrec_a)
3025 || chrec_contains_undetermined (chrec_b))
3027 dependence_stats.num_subscript_undetermined++;
3029 *overlap_iterations_a = conflict_fn_not_known ();
3030 *overlap_iterations_b = conflict_fn_not_known ();
3033 /* If they are the same chrec, and are affine, they overlap
3034 on every iteration. */
3035 else if (eq_evolutions_p (chrec_a, chrec_b)
3036 && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3037 || operand_equal_p (chrec_a, chrec_b, 0)))
3039 dependence_stats.num_same_subscript_function++;
3040 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3041 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3042 *last_conflicts = chrec_dont_know;
3045 /* If they aren't the same, and aren't affine, we can't do anything
3046 yet. */
3047 else if ((chrec_contains_symbols (chrec_a)
3048 || chrec_contains_symbols (chrec_b))
3049 && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3050 || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
3052 dependence_stats.num_subscript_undetermined++;
3053 *overlap_iterations_a = conflict_fn_not_known ();
3054 *overlap_iterations_b = conflict_fn_not_known ();
3057 else if (ziv_subscript_p (chrec_a, chrec_b))
3058 analyze_ziv_subscript (chrec_a, chrec_b,
3059 overlap_iterations_a, overlap_iterations_b,
3060 last_conflicts);
3062 else if (siv_subscript_p (chrec_a, chrec_b))
3063 analyze_siv_subscript (chrec_a, chrec_b,
3064 overlap_iterations_a, overlap_iterations_b,
3065 last_conflicts, lnn);
3067 else
3068 analyze_miv_subscript (chrec_a, chrec_b,
3069 overlap_iterations_a, overlap_iterations_b,
3070 last_conflicts, loop_nest);
3072 if (dump_file && (dump_flags & TDF_DETAILS))
3074 fprintf (dump_file, " (overlap_iterations_a = ");
3075 dump_conflict_function (dump_file, *overlap_iterations_a);
3076 fprintf (dump_file, ")\n (overlap_iterations_b = ");
3077 dump_conflict_function (dump_file, *overlap_iterations_b);
3078 fprintf (dump_file, "))\n");
3082 /* Helper function for uniquely inserting distance vectors. */
3084 static void
3085 save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
3087 unsigned i;
3088 lambda_vector v;
3090 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, v)
3091 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
3092 return;
3094 DDR_DIST_VECTS (ddr).safe_push (dist_v);
3097 /* Helper function for uniquely inserting direction vectors. */
3099 static void
3100 save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
3102 unsigned i;
3103 lambda_vector v;
3105 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), i, v)
3106 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
3107 return;
3109 DDR_DIR_VECTS (ddr).safe_push (dir_v);
3112 /* Add a distance of 1 on all the loops outer than INDEX. If we
3113 haven't yet determined a distance for this outer loop, push a new
3114 distance vector composed of the previous distance, and a distance
3115 of 1 for this outer loop. Example:
3117 | loop_1
3118 | loop_2
3119 | A[10]
3120 | endloop_2
3121 | endloop_1
3123 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3124 save (0, 1), then we have to save (1, 0). */
3126 static void
3127 add_outer_distances (struct data_dependence_relation *ddr,
3128 lambda_vector dist_v, int index)
3130 /* For each outer loop where init_v is not set, the accesses are
3131 in dependence of distance 1 in the loop. */
3132 while (--index >= 0)
3134 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3135 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3136 save_v[index] = 1;
3137 save_dist_v (ddr, save_v);
3141 /* Return false when fail to represent the data dependence as a
3142 distance vector. INIT_B is set to true when a component has been
3143 added to the distance vector DIST_V. INDEX_CARRY is then set to
3144 the index in DIST_V that carries the dependence. */
3146 static bool
3147 build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
3148 struct data_reference *ddr_a,
3149 struct data_reference *ddr_b,
3150 lambda_vector dist_v, bool *init_b,
3151 int *index_carry)
3153 unsigned i;
3154 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3156 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3158 tree access_fn_a, access_fn_b;
3159 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3161 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3163 non_affine_dependence_relation (ddr);
3164 return false;
3167 access_fn_a = DR_ACCESS_FN (ddr_a, i);
3168 access_fn_b = DR_ACCESS_FN (ddr_b, i);
3170 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3171 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3173 int dist, index;
3174 int var_a = CHREC_VARIABLE (access_fn_a);
3175 int var_b = CHREC_VARIABLE (access_fn_b);
3177 if (var_a != var_b
3178 || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3180 non_affine_dependence_relation (ddr);
3181 return false;
3184 dist = int_cst_value (SUB_DISTANCE (subscript));
3185 index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
3186 *index_carry = MIN (index, *index_carry);
3188 /* This is the subscript coupling test. If we have already
3189 recorded a distance for this loop (a distance coming from
3190 another subscript), it should be the same. For example,
3191 in the following code, there is no dependence:
3193 | loop i = 0, N, 1
3194 | T[i+1][i] = ...
3195 | ... = T[i][i]
3196 | endloop
3198 if (init_v[index] != 0 && dist_v[index] != dist)
3200 finalize_ddr_dependent (ddr, chrec_known);
3201 return false;
3204 dist_v[index] = dist;
3205 init_v[index] = 1;
3206 *init_b = true;
3208 else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
3210 /* This can be for example an affine vs. constant dependence
3211 (T[i] vs. T[3]) that is not an affine dependence and is
3212 not representable as a distance vector. */
3213 non_affine_dependence_relation (ddr);
3214 return false;
3218 return true;
3221 /* Return true when the DDR contains only constant access functions. */
3223 static bool
3224 constant_access_functions (const struct data_dependence_relation *ddr)
3226 unsigned i;
3228 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3229 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr), i))
3230 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr), i)))
3231 return false;
3233 return true;
3236 /* Helper function for the case where DDR_A and DDR_B are the same
3237 multivariate access function with a constant step. For an example
3238 see pr34635-1.c. */
3240 static void
3241 add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
3243 int x_1, x_2;
3244 tree c_1 = CHREC_LEFT (c_2);
3245 tree c_0 = CHREC_LEFT (c_1);
3246 lambda_vector dist_v;
3247 int v1, v2, cd;
3249 /* Polynomials with more than 2 variables are not handled yet. When
3250 the evolution steps are parameters, it is not possible to
3251 represent the dependence using classical distance vectors. */
3252 if (TREE_CODE (c_0) != INTEGER_CST
3253 || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
3254 || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
3256 DDR_AFFINE_P (ddr) = false;
3257 return;
3260 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
3261 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
3263 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3264 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3265 v1 = int_cst_value (CHREC_RIGHT (c_1));
3266 v2 = int_cst_value (CHREC_RIGHT (c_2));
3267 cd = gcd (v1, v2);
3268 v1 /= cd;
3269 v2 /= cd;
3271 if (v2 < 0)
3273 v2 = -v2;
3274 v1 = -v1;
3277 dist_v[x_1] = v2;
3278 dist_v[x_2] = -v1;
3279 save_dist_v (ddr, dist_v);
3281 add_outer_distances (ddr, dist_v, x_1);
3284 /* Helper function for the case where DDR_A and DDR_B are the same
3285 access functions. */
3287 static void
3288 add_other_self_distances (struct data_dependence_relation *ddr)
3290 lambda_vector dist_v;
3291 unsigned i;
3292 int index_carry = DDR_NB_LOOPS (ddr);
3294 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3296 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
3298 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
3300 if (!evolution_function_is_univariate_p (access_fun))
3302 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
3304 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3305 return;
3308 access_fun = DR_ACCESS_FN (DDR_A (ddr), 0);
3310 if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
3311 add_multivariate_self_dist (ddr, access_fun);
3312 else
3313 /* The evolution step is not constant: it varies in
3314 the outer loop, so this cannot be represented by a
3315 distance vector. For example in pr34635.c the
3316 evolution is {0, +, {0, +, 4}_1}_2. */
3317 DDR_AFFINE_P (ddr) = false;
3319 return;
3322 index_carry = MIN (index_carry,
3323 index_in_loop_nest (CHREC_VARIABLE (access_fun),
3324 DDR_LOOP_NEST (ddr)));
3328 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3329 add_outer_distances (ddr, dist_v, index_carry);
3332 static void
3333 insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
3335 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3337 dist_v[DDR_INNER_LOOP (ddr)] = 1;
3338 save_dist_v (ddr, dist_v);
3341 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
3342 is the case for example when access functions are the same and
3343 equal to a constant, as in:
3345 | loop_1
3346 | A[3] = ...
3347 | ... = A[3]
3348 | endloop_1
3350 in which case the distance vectors are (0) and (1). */
3352 static void
3353 add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
3355 unsigned i, j;
3357 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3359 subscript_p sub = DDR_SUBSCRIPT (ddr, i);
3360 conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
3361 conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
3363 for (j = 0; j < ca->n; j++)
3364 if (affine_function_zero_p (ca->fns[j]))
3366 insert_innermost_unit_dist_vector (ddr);
3367 return;
3370 for (j = 0; j < cb->n; j++)
3371 if (affine_function_zero_p (cb->fns[j]))
3373 insert_innermost_unit_dist_vector (ddr);
3374 return;
3379 /* Compute the classic per loop distance vector. DDR is the data
3380 dependence relation to build a vector from. Return false when fail
3381 to represent the data dependence as a distance vector. */
3383 static bool
3384 build_classic_dist_vector (struct data_dependence_relation *ddr,
3385 struct loop *loop_nest)
3387 bool init_b = false;
3388 int index_carry = DDR_NB_LOOPS (ddr);
3389 lambda_vector dist_v;
3391 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3392 return false;
3394 if (same_access_functions (ddr))
3396 /* Save the 0 vector. */
3397 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3398 save_dist_v (ddr, dist_v);
3400 if (constant_access_functions (ddr))
3401 add_distance_for_zero_overlaps (ddr);
3403 if (DDR_NB_LOOPS (ddr) > 1)
3404 add_other_self_distances (ddr);
3406 return true;
3409 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3410 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3411 dist_v, &init_b, &index_carry))
3412 return false;
3414 /* Save the distance vector if we initialized one. */
3415 if (init_b)
3417 /* Verify a basic constraint: classic distance vectors should
3418 always be lexicographically positive.
3420 Data references are collected in the order of execution of
3421 the program, thus for the following loop
3423 | for (i = 1; i < 100; i++)
3424 | for (j = 1; j < 100; j++)
3426 | t = T[j+1][i-1]; // A
3427 | T[j][i] = t + 2; // B
3430 references are collected following the direction of the wind:
3431 A then B. The data dependence tests are performed also
3432 following this order, such that we're looking at the distance
3433 separating the elements accessed by A from the elements later
3434 accessed by B. But in this example, the distance returned by
3435 test_dep (A, B) is lexicographically negative (-1, 1), that
3436 means that the access A occurs later than B with respect to
3437 the outer loop, ie. we're actually looking upwind. In this
3438 case we solve test_dep (B, A) looking downwind to the
3439 lexicographically positive solution, that returns the
3440 distance vector (1, -1). */
3441 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3443 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3444 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3445 loop_nest))
3446 return false;
3447 compute_subscript_distance (ddr);
3448 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3449 save_v, &init_b, &index_carry))
3450 return false;
3451 save_dist_v (ddr, save_v);
3452 DDR_REVERSED_P (ddr) = true;
3454 /* In this case there is a dependence forward for all the
3455 outer loops:
3457 | for (k = 1; k < 100; k++)
3458 | for (i = 1; i < 100; i++)
3459 | for (j = 1; j < 100; j++)
3461 | t = T[j+1][i-1]; // A
3462 | T[j][i] = t + 2; // B
3465 the vectors are:
3466 (0, 1, -1)
3467 (1, 1, -1)
3468 (1, -1, 1)
3470 if (DDR_NB_LOOPS (ddr) > 1)
3472 add_outer_distances (ddr, save_v, index_carry);
3473 add_outer_distances (ddr, dist_v, index_carry);
3476 else
3478 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3479 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3481 if (DDR_NB_LOOPS (ddr) > 1)
3483 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3485 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr),
3486 DDR_A (ddr), loop_nest))
3487 return false;
3488 compute_subscript_distance (ddr);
3489 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3490 opposite_v, &init_b,
3491 &index_carry))
3492 return false;
3494 save_dist_v (ddr, save_v);
3495 add_outer_distances (ddr, dist_v, index_carry);
3496 add_outer_distances (ddr, opposite_v, index_carry);
3498 else
3499 save_dist_v (ddr, save_v);
3502 else
3504 /* There is a distance of 1 on all the outer loops: Example:
3505 there is a dependence of distance 1 on loop_1 for the array A.
3507 | loop_1
3508 | A[5] = ...
3509 | endloop
3511 add_outer_distances (ddr, dist_v,
3512 lambda_vector_first_nz (dist_v,
3513 DDR_NB_LOOPS (ddr), 0));
3516 if (dump_file && (dump_flags & TDF_DETAILS))
3518 unsigned i;
3520 fprintf (dump_file, "(build_classic_dist_vector\n");
3521 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3523 fprintf (dump_file, " dist_vector = (");
3524 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3525 DDR_NB_LOOPS (ddr));
3526 fprintf (dump_file, " )\n");
3528 fprintf (dump_file, ")\n");
3531 return true;
3534 /* Return the direction for a given distance.
3535 FIXME: Computing dir this way is suboptimal, since dir can catch
3536 cases that dist is unable to represent. */
3538 static inline enum data_dependence_direction
3539 dir_from_dist (int dist)
3541 if (dist > 0)
3542 return dir_positive;
3543 else if (dist < 0)
3544 return dir_negative;
3545 else
3546 return dir_equal;
3549 /* Compute the classic per loop direction vector. DDR is the data
3550 dependence relation to build a vector from. */
3552 static void
3553 build_classic_dir_vector (struct data_dependence_relation *ddr)
3555 unsigned i, j;
3556 lambda_vector dist_v;
3558 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
3560 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3562 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3563 dir_v[j] = dir_from_dist (dist_v[j]);
3565 save_dir_v (ddr, dir_v);
3569 /* Helper function. Returns true when there is a dependence between
3570 data references DRA and DRB. */
3572 static bool
3573 subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
3574 struct data_reference *dra,
3575 struct data_reference *drb,
3576 struct loop *loop_nest)
3578 unsigned int i;
3579 tree last_conflicts;
3580 struct subscript *subscript;
3581 tree res = NULL_TREE;
3583 for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
3585 conflict_function *overlaps_a, *overlaps_b;
3587 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3588 DR_ACCESS_FN (drb, i),
3589 &overlaps_a, &overlaps_b,
3590 &last_conflicts, loop_nest);
3592 if (SUB_CONFLICTS_IN_A (subscript))
3593 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
3594 if (SUB_CONFLICTS_IN_B (subscript))
3595 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
3597 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3598 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3599 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3601 /* If there is any undetermined conflict function we have to
3602 give a conservative answer in case we cannot prove that
3603 no dependence exists when analyzing another subscript. */
3604 if (CF_NOT_KNOWN_P (overlaps_a)
3605 || CF_NOT_KNOWN_P (overlaps_b))
3607 res = chrec_dont_know;
3608 continue;
3611 /* When there is a subscript with no dependence we can stop. */
3612 else if (CF_NO_DEPENDENCE_P (overlaps_a)
3613 || CF_NO_DEPENDENCE_P (overlaps_b))
3615 res = chrec_known;
3616 break;
3620 if (res == NULL_TREE)
3621 return true;
3623 if (res == chrec_known)
3624 dependence_stats.num_dependence_independent++;
3625 else
3626 dependence_stats.num_dependence_undetermined++;
3627 finalize_ddr_dependent (ddr, res);
3628 return false;
3631 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
3633 static void
3634 subscript_dependence_tester (struct data_dependence_relation *ddr,
3635 struct loop *loop_nest)
3637 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr), loop_nest))
3638 dependence_stats.num_dependence_dependent++;
3640 compute_subscript_distance (ddr);
3641 if (build_classic_dist_vector (ddr, loop_nest))
3642 build_classic_dir_vector (ddr);
3645 /* Returns true when all the access functions of A are affine or
3646 constant with respect to LOOP_NEST. */
3648 static bool
3649 access_functions_are_affine_or_constant_p (const struct data_reference *a,
3650 const struct loop *loop_nest)
3652 unsigned int i;
3653 vec<tree> fns = DR_ACCESS_FNS (a);
3654 tree t;
3656 FOR_EACH_VEC_ELT (fns, i, t)
3657 if (!evolution_function_is_invariant_p (t, loop_nest->num)
3658 && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
3659 return false;
3661 return true;
3664 /* Initializes an equation for an OMEGA problem using the information
3665 contained in the ACCESS_FUN. Returns true when the operation
3666 succeeded.
3668 PB is the omega constraint system.
3669 EQ is the number of the equation to be initialized.
3670 OFFSET is used for shifting the variables names in the constraints:
3671 a constrain is composed of 2 * the number of variables surrounding
3672 dependence accesses. OFFSET is set either to 0 for the first n variables,
3673 then it is set to n.
3674 ACCESS_FUN is expected to be an affine chrec. */
3676 static bool
3677 init_omega_eq_with_af (omega_pb pb, unsigned eq,
3678 unsigned int offset, tree access_fun,
3679 struct data_dependence_relation *ddr)
3681 switch (TREE_CODE (access_fun))
3683 case POLYNOMIAL_CHREC:
3685 tree left = CHREC_LEFT (access_fun);
3686 tree right = CHREC_RIGHT (access_fun);
3687 int var = CHREC_VARIABLE (access_fun);
3688 unsigned var_idx;
3690 if (TREE_CODE (right) != INTEGER_CST)
3691 return false;
3693 var_idx = index_in_loop_nest (var, DDR_LOOP_NEST (ddr));
3694 pb->eqs[eq].coef[offset + var_idx + 1] = int_cst_value (right);
3696 /* Compute the innermost loop index. */
3697 DDR_INNER_LOOP (ddr) = MAX (DDR_INNER_LOOP (ddr), var_idx);
3699 if (offset == 0)
3700 pb->eqs[eq].coef[var_idx + DDR_NB_LOOPS (ddr) + 1]
3701 += int_cst_value (right);
3703 switch (TREE_CODE (left))
3705 case POLYNOMIAL_CHREC:
3706 return init_omega_eq_with_af (pb, eq, offset, left, ddr);
3708 case INTEGER_CST:
3709 pb->eqs[eq].coef[0] += int_cst_value (left);
3710 return true;
3712 default:
3713 return false;
3717 case INTEGER_CST:
3718 pb->eqs[eq].coef[0] += int_cst_value (access_fun);
3719 return true;
3721 default:
3722 return false;
3726 /* As explained in the comments preceding init_omega_for_ddr, we have
3727 to set up a system for each loop level, setting outer loops
3728 variation to zero, and current loop variation to positive or zero.
3729 Save each lexico positive distance vector. */
3731 static void
3732 omega_extract_distance_vectors (omega_pb pb,
3733 struct data_dependence_relation *ddr)
3735 int eq, geq;
3736 unsigned i, j;
3737 struct loop *loopi, *loopj;
3738 enum omega_result res;
3740 /* Set a new problem for each loop in the nest. The basis is the
3741 problem that we have initialized until now. On top of this we
3742 add new constraints. */
3743 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3744 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3746 int dist = 0;
3747 omega_pb copy = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr),
3748 DDR_NB_LOOPS (ddr));
3750 omega_copy_problem (copy, pb);
3752 /* For all the outer loops "loop_j", add "dj = 0". */
3753 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3755 eq = omega_add_zero_eq (copy, omega_black);
3756 copy->eqs[eq].coef[j + 1] = 1;
3759 /* For "loop_i", add "0 <= di". */
3760 geq = omega_add_zero_geq (copy, omega_black);
3761 copy->geqs[geq].coef[i + 1] = 1;
3763 /* Reduce the constraint system, and test that the current
3764 problem is feasible. */
3765 res = omega_simplify_problem (copy);
3766 if (res == omega_false
3767 || res == omega_unknown
3768 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3769 goto next_problem;
3771 for (eq = 0; eq < copy->num_subs; eq++)
3772 if (copy->subs[eq].key == (int) i + 1)
3774 dist = copy->subs[eq].coef[0];
3775 goto found_dist;
3778 if (dist == 0)
3780 /* Reinitialize problem... */
3781 omega_copy_problem (copy, pb);
3782 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3784 eq = omega_add_zero_eq (copy, omega_black);
3785 copy->eqs[eq].coef[j + 1] = 1;
3788 /* ..., but this time "di = 1". */
3789 eq = omega_add_zero_eq (copy, omega_black);
3790 copy->eqs[eq].coef[i + 1] = 1;
3791 copy->eqs[eq].coef[0] = -1;
3793 res = omega_simplify_problem (copy);
3794 if (res == omega_false
3795 || res == omega_unknown
3796 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3797 goto next_problem;
3799 for (eq = 0; eq < copy->num_subs; eq++)
3800 if (copy->subs[eq].key == (int) i + 1)
3802 dist = copy->subs[eq].coef[0];
3803 goto found_dist;
3807 found_dist:;
3808 /* Save the lexicographically positive distance vector. */
3809 if (dist >= 0)
3811 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3812 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3814 dist_v[i] = dist;
3816 for (eq = 0; eq < copy->num_subs; eq++)
3817 if (copy->subs[eq].key > 0)
3819 dist = copy->subs[eq].coef[0];
3820 dist_v[copy->subs[eq].key - 1] = dist;
3823 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3824 dir_v[j] = dir_from_dist (dist_v[j]);
3826 save_dist_v (ddr, dist_v);
3827 save_dir_v (ddr, dir_v);
3830 next_problem:;
3831 omega_free_problem (copy);
3835 /* This is called for each subscript of a tuple of data references:
3836 insert an equality for representing the conflicts. */
3838 static bool
3839 omega_setup_subscript (tree access_fun_a, tree access_fun_b,
3840 struct data_dependence_relation *ddr,
3841 omega_pb pb, bool *maybe_dependent)
3843 int eq;
3844 tree type = signed_type_for_types (TREE_TYPE (access_fun_a),
3845 TREE_TYPE (access_fun_b));
3846 tree fun_a = chrec_convert (type, access_fun_a, NULL);
3847 tree fun_b = chrec_convert (type, access_fun_b, NULL);
3848 tree difference = chrec_fold_minus (type, fun_a, fun_b);
3849 tree minus_one;
3851 /* When the fun_a - fun_b is not constant, the dependence is not
3852 captured by the classic distance vector representation. */
3853 if (TREE_CODE (difference) != INTEGER_CST)
3854 return false;
3856 /* ZIV test. */
3857 if (ziv_subscript_p (fun_a, fun_b) && !integer_zerop (difference))
3859 /* There is no dependence. */
3860 *maybe_dependent = false;
3861 return true;
3864 minus_one = build_int_cst (type, -1);
3865 fun_b = chrec_fold_multiply (type, fun_b, minus_one);
3867 eq = omega_add_zero_eq (pb, omega_black);
3868 if (!init_omega_eq_with_af (pb, eq, DDR_NB_LOOPS (ddr), fun_a, ddr)
3869 || !init_omega_eq_with_af (pb, eq, 0, fun_b, ddr))
3870 /* There is probably a dependence, but the system of
3871 constraints cannot be built: answer "don't know". */
3872 return false;
3874 /* GCD test. */
3875 if (DDR_NB_LOOPS (ddr) != 0 && pb->eqs[eq].coef[0]
3876 && !int_divides_p (lambda_vector_gcd
3877 ((lambda_vector) &(pb->eqs[eq].coef[1]),
3878 2 * DDR_NB_LOOPS (ddr)),
3879 pb->eqs[eq].coef[0]))
3881 /* There is no dependence. */
3882 *maybe_dependent = false;
3883 return true;
3886 return true;
3889 /* Helper function, same as init_omega_for_ddr but specialized for
3890 data references A and B. */
3892 static bool
3893 init_omega_for_ddr_1 (struct data_reference *dra, struct data_reference *drb,
3894 struct data_dependence_relation *ddr,
3895 omega_pb pb, bool *maybe_dependent)
3897 unsigned i;
3898 int ineq;
3899 struct loop *loopi;
3900 unsigned nb_loops = DDR_NB_LOOPS (ddr);
3902 /* Insert an equality per subscript. */
3903 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3905 if (!omega_setup_subscript (DR_ACCESS_FN (dra, i), DR_ACCESS_FN (drb, i),
3906 ddr, pb, maybe_dependent))
3907 return false;
3908 else if (*maybe_dependent == false)
3910 /* There is no dependence. */
3911 DDR_ARE_DEPENDENT (ddr) = chrec_known;
3912 return true;
3916 /* Insert inequalities: constraints corresponding to the iteration
3917 domain, i.e. the loops surrounding the references "loop_x" and
3918 the distance variables "dx". The layout of the OMEGA
3919 representation is as follows:
3920 - coef[0] is the constant
3921 - coef[1..nb_loops] are the protected variables that will not be
3922 removed by the solver: the "dx"
3923 - coef[nb_loops + 1, 2*nb_loops] are the loop variables: "loop_x".
3925 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3926 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3928 HOST_WIDE_INT nbi = max_stmt_executions_int (loopi);
3930 /* 0 <= loop_x */
3931 ineq = omega_add_zero_geq (pb, omega_black);
3932 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3934 /* 0 <= loop_x + dx */
3935 ineq = omega_add_zero_geq (pb, omega_black);
3936 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3937 pb->geqs[ineq].coef[i + 1] = 1;
3939 if (nbi != -1)
3941 /* loop_x <= nb_iters */
3942 ineq = omega_add_zero_geq (pb, omega_black);
3943 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3944 pb->geqs[ineq].coef[0] = nbi;
3946 /* loop_x + dx <= nb_iters */
3947 ineq = omega_add_zero_geq (pb, omega_black);
3948 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3949 pb->geqs[ineq].coef[i + 1] = -1;
3950 pb->geqs[ineq].coef[0] = nbi;
3952 /* A step "dx" bigger than nb_iters is not feasible, so
3953 add "0 <= nb_iters + dx", */
3954 ineq = omega_add_zero_geq (pb, omega_black);
3955 pb->geqs[ineq].coef[i + 1] = 1;
3956 pb->geqs[ineq].coef[0] = nbi;
3957 /* and "dx <= nb_iters". */
3958 ineq = omega_add_zero_geq (pb, omega_black);
3959 pb->geqs[ineq].coef[i + 1] = -1;
3960 pb->geqs[ineq].coef[0] = nbi;
3964 omega_extract_distance_vectors (pb, ddr);
3966 return true;
3969 /* Sets up the Omega dependence problem for the data dependence
3970 relation DDR. Returns false when the constraint system cannot be
3971 built, ie. when the test answers "don't know". Returns true
3972 otherwise, and when independence has been proved (using one of the
3973 trivial dependence test), set MAYBE_DEPENDENT to false, otherwise
3974 set MAYBE_DEPENDENT to true.
3976 Example: for setting up the dependence system corresponding to the
3977 conflicting accesses
3979 | loop_i
3980 | loop_j
3981 | A[i, i+1] = ...
3982 | ... A[2*j, 2*(i + j)]
3983 | endloop_j
3984 | endloop_i
3986 the following constraints come from the iteration domain:
3988 0 <= i <= Ni
3989 0 <= i + di <= Ni
3990 0 <= j <= Nj
3991 0 <= j + dj <= Nj
3993 where di, dj are the distance variables. The constraints
3994 representing the conflicting elements are:
3996 i = 2 * (j + dj)
3997 i + 1 = 2 * (i + di + j + dj)
3999 For asking that the resulting distance vector (di, dj) be
4000 lexicographically positive, we insert the constraint "di >= 0". If
4001 "di = 0" in the solution, we fix that component to zero, and we
4002 look at the inner loops: we set a new problem where all the outer
4003 loop distances are zero, and fix this inner component to be
4004 positive. When one of the components is positive, we save that
4005 distance, and set a new problem where the distance on this loop is
4006 zero, searching for other distances in the inner loops. Here is
4007 the classic example that illustrates that we have to set for each
4008 inner loop a new problem:
4010 | loop_1
4011 | loop_2
4012 | A[10]
4013 | endloop_2
4014 | endloop_1
4016 we have to save two distances (1, 0) and (0, 1).
4018 Given two array references, refA and refB, we have to set the
4019 dependence problem twice, refA vs. refB and refB vs. refA, and we
4020 cannot do a single test, as refB might occur before refA in the
4021 inner loops, and the contrary when considering outer loops: ex.
4023 | loop_0
4024 | loop_1
4025 | loop_2
4026 | T[{1,+,1}_2][{1,+,1}_1] // refA
4027 | T[{2,+,1}_2][{0,+,1}_1] // refB
4028 | endloop_2
4029 | endloop_1
4030 | endloop_0
4032 refB touches the elements in T before refA, and thus for the same
4033 loop_0 refB precedes refA: ie. the distance vector (0, 1, -1)
4034 but for successive loop_0 iterations, we have (1, -1, 1)
4036 The Omega solver expects the distance variables ("di" in the
4037 previous example) to come first in the constraint system (as
4038 variables to be protected, or "safe" variables), the constraint
4039 system is built using the following layout:
4041 "cst | distance vars | index vars".
4044 static bool
4045 init_omega_for_ddr (struct data_dependence_relation *ddr,
4046 bool *maybe_dependent)
4048 omega_pb pb;
4049 bool res = false;
4051 *maybe_dependent = true;
4053 if (same_access_functions (ddr))
4055 unsigned j;
4056 lambda_vector dir_v;
4058 /* Save the 0 vector. */
4059 save_dist_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
4060 dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4061 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
4062 dir_v[j] = dir_equal;
4063 save_dir_v (ddr, dir_v);
4065 /* Save the dependences carried by outer loops. */
4066 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4067 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4068 maybe_dependent);
4069 omega_free_problem (pb);
4070 return res;
4073 /* Omega expects the protected variables (those that have to be kept
4074 after elimination) to appear first in the constraint system.
4075 These variables are the distance variables. In the following
4076 initialization we declare NB_LOOPS safe variables, and the total
4077 number of variables for the constraint system is 2*NB_LOOPS. */
4078 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4079 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4080 maybe_dependent);
4081 omega_free_problem (pb);
4083 /* Stop computation if not decidable, or no dependence. */
4084 if (res == false || *maybe_dependent == false)
4085 return res;
4087 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4088 res = init_omega_for_ddr_1 (DDR_B (ddr), DDR_A (ddr), ddr, pb,
4089 maybe_dependent);
4090 omega_free_problem (pb);
4092 return res;
4095 /* Return true when DDR contains the same information as that stored
4096 in DIR_VECTS and in DIST_VECTS, return false otherwise. */
4098 static bool
4099 ddr_consistent_p (FILE *file,
4100 struct data_dependence_relation *ddr,
4101 vec<lambda_vector> dist_vects,
4102 vec<lambda_vector> dir_vects)
4104 unsigned int i, j;
4106 /* If dump_file is set, output there. */
4107 if (dump_file && (dump_flags & TDF_DETAILS))
4108 file = dump_file;
4110 if (dist_vects.length () != DDR_NUM_DIST_VECTS (ddr))
4112 lambda_vector b_dist_v;
4113 fprintf (file, "\n(Number of distance vectors differ: Banerjee has %d, Omega has %d.\n",
4114 dist_vects.length (),
4115 DDR_NUM_DIST_VECTS (ddr));
4117 fprintf (file, "Banerjee dist vectors:\n");
4118 FOR_EACH_VEC_ELT (dist_vects, i, b_dist_v)
4119 print_lambda_vector (file, b_dist_v, DDR_NB_LOOPS (ddr));
4121 fprintf (file, "Omega dist vectors:\n");
4122 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4123 print_lambda_vector (file, DDR_DIST_VECT (ddr, i), DDR_NB_LOOPS (ddr));
4125 fprintf (file, "data dependence relation:\n");
4126 dump_data_dependence_relation (file, ddr);
4128 fprintf (file, ")\n");
4129 return false;
4132 if (dir_vects.length () != DDR_NUM_DIR_VECTS (ddr))
4134 fprintf (file, "\n(Number of direction vectors differ: Banerjee has %d, Omega has %d.)\n",
4135 dir_vects.length (),
4136 DDR_NUM_DIR_VECTS (ddr));
4137 return false;
4140 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4142 lambda_vector a_dist_v;
4143 lambda_vector b_dist_v = DDR_DIST_VECT (ddr, i);
4145 /* Distance vectors are not ordered in the same way in the DDR
4146 and in the DIST_VECTS: search for a matching vector. */
4147 FOR_EACH_VEC_ELT (dist_vects, j, a_dist_v)
4148 if (lambda_vector_equal (a_dist_v, b_dist_v, DDR_NB_LOOPS (ddr)))
4149 break;
4151 if (j == dist_vects.length ())
4153 fprintf (file, "\n(Dist vectors from the first dependence analyzer:\n");
4154 print_dist_vectors (file, dist_vects, DDR_NB_LOOPS (ddr));
4155 fprintf (file, "not found in Omega dist vectors:\n");
4156 print_dist_vectors (file, DDR_DIST_VECTS (ddr), DDR_NB_LOOPS (ddr));
4157 fprintf (file, "data dependence relation:\n");
4158 dump_data_dependence_relation (file, ddr);
4159 fprintf (file, ")\n");
4163 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
4165 lambda_vector a_dir_v;
4166 lambda_vector b_dir_v = DDR_DIR_VECT (ddr, i);
4168 /* Direction vectors are not ordered in the same way in the DDR
4169 and in the DIR_VECTS: search for a matching vector. */
4170 FOR_EACH_VEC_ELT (dir_vects, j, a_dir_v)
4171 if (lambda_vector_equal (a_dir_v, b_dir_v, DDR_NB_LOOPS (ddr)))
4172 break;
4174 if (j == dist_vects.length ())
4176 fprintf (file, "\n(Dir vectors from the first dependence analyzer:\n");
4177 print_dir_vectors (file, dir_vects, DDR_NB_LOOPS (ddr));
4178 fprintf (file, "not found in Omega dir vectors:\n");
4179 print_dir_vectors (file, DDR_DIR_VECTS (ddr), DDR_NB_LOOPS (ddr));
4180 fprintf (file, "data dependence relation:\n");
4181 dump_data_dependence_relation (file, ddr);
4182 fprintf (file, ")\n");
4186 return true;
4189 /* This computes the affine dependence relation between A and B with
4190 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
4191 independence between two accesses, while CHREC_DONT_KNOW is used
4192 for representing the unknown relation.
4194 Note that it is possible to stop the computation of the dependence
4195 relation the first time we detect a CHREC_KNOWN element for a given
4196 subscript. */
4198 void
4199 compute_affine_dependence (struct data_dependence_relation *ddr,
4200 struct loop *loop_nest)
4202 struct data_reference *dra = DDR_A (ddr);
4203 struct data_reference *drb = DDR_B (ddr);
4205 if (dump_file && (dump_flags & TDF_DETAILS))
4207 fprintf (dump_file, "(compute_affine_dependence\n");
4208 fprintf (dump_file, " stmt_a: ");
4209 print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
4210 fprintf (dump_file, " stmt_b: ");
4211 print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
4214 /* Analyze only when the dependence relation is not yet known. */
4215 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4217 dependence_stats.num_dependence_tests++;
4219 if (access_functions_are_affine_or_constant_p (dra, loop_nest)
4220 && access_functions_are_affine_or_constant_p (drb, loop_nest))
4222 subscript_dependence_tester (ddr, loop_nest);
4224 if (flag_check_data_deps)
4226 /* Dump the dependences from the first algorithm. */
4227 if (dump_file && (dump_flags & TDF_DETAILS))
4229 fprintf (dump_file, "\n\nBanerjee Analyzer\n");
4230 dump_data_dependence_relation (dump_file, ddr);
4233 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4235 bool maybe_dependent;
4236 vec<lambda_vector> dir_vects, dist_vects;
4238 /* Save the result of the first DD analyzer. */
4239 dist_vects = DDR_DIST_VECTS (ddr);
4240 dir_vects = DDR_DIR_VECTS (ddr);
4242 /* Reset the information. */
4243 DDR_DIST_VECTS (ddr).create (0);
4244 DDR_DIR_VECTS (ddr).create (0);
4246 /* Compute the same information using Omega. */
4247 if (!init_omega_for_ddr (ddr, &maybe_dependent))
4248 goto csys_dont_know;
4250 if (dump_file && (dump_flags & TDF_DETAILS))
4252 fprintf (dump_file, "Omega Analyzer\n");
4253 dump_data_dependence_relation (dump_file, ddr);
4256 /* Check that we get the same information. */
4257 if (maybe_dependent)
4258 gcc_assert (ddr_consistent_p (stderr, ddr, dist_vects,
4259 dir_vects));
4264 /* As a last case, if the dependence cannot be determined, or if
4265 the dependence is considered too difficult to determine, answer
4266 "don't know". */
4267 else
4269 csys_dont_know:;
4270 dependence_stats.num_dependence_undetermined++;
4272 if (dump_file && (dump_flags & TDF_DETAILS))
4274 fprintf (dump_file, "Data ref a:\n");
4275 dump_data_reference (dump_file, dra);
4276 fprintf (dump_file, "Data ref b:\n");
4277 dump_data_reference (dump_file, drb);
4278 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
4280 finalize_ddr_dependent (ddr, chrec_dont_know);
4284 if (dump_file && (dump_flags & TDF_DETAILS))
4286 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4287 fprintf (dump_file, ") -> no dependence\n");
4288 else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
4289 fprintf (dump_file, ") -> dependence analysis failed\n");
4290 else
4291 fprintf (dump_file, ")\n");
4295 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
4296 the data references in DATAREFS, in the LOOP_NEST. When
4297 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
4298 relations. Return true when successful, i.e. data references number
4299 is small enough to be handled. */
4301 bool
4302 compute_all_dependences (vec<data_reference_p> datarefs,
4303 vec<ddr_p> *dependence_relations,
4304 vec<loop_p> loop_nest,
4305 bool compute_self_and_rr)
4307 struct data_dependence_relation *ddr;
4308 struct data_reference *a, *b;
4309 unsigned int i, j;
4311 if ((int) datarefs.length ()
4312 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
4314 struct data_dependence_relation *ddr;
4316 /* Insert a single relation into dependence_relations:
4317 chrec_dont_know. */
4318 ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
4319 dependence_relations->safe_push (ddr);
4320 return false;
4323 FOR_EACH_VEC_ELT (datarefs, i, a)
4324 for (j = i + 1; datarefs.iterate (j, &b); j++)
4325 if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
4327 ddr = initialize_data_dependence_relation (a, b, loop_nest);
4328 dependence_relations->safe_push (ddr);
4329 if (loop_nest.exists ())
4330 compute_affine_dependence (ddr, loop_nest[0]);
4333 if (compute_self_and_rr)
4334 FOR_EACH_VEC_ELT (datarefs, i, a)
4336 ddr = initialize_data_dependence_relation (a, a, loop_nest);
4337 dependence_relations->safe_push (ddr);
4338 if (loop_nest.exists ())
4339 compute_affine_dependence (ddr, loop_nest[0]);
4342 return true;
4345 /* Describes a location of a memory reference. */
4347 typedef struct data_ref_loc_d
4349 /* The memory reference. */
4350 tree ref;
4352 /* True if the memory reference is read. */
4353 bool is_read;
4354 } data_ref_loc;
4357 /* Stores the locations of memory references in STMT to REFERENCES. Returns
4358 true if STMT clobbers memory, false otherwise. */
4360 static bool
4361 get_references_in_stmt (gimple stmt, vec<data_ref_loc, va_heap> *references)
4363 bool clobbers_memory = false;
4364 data_ref_loc ref;
4365 tree op0, op1;
4366 enum gimple_code stmt_code = gimple_code (stmt);
4368 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4369 As we cannot model data-references to not spelled out
4370 accesses give up if they may occur. */
4371 if (stmt_code == GIMPLE_CALL
4372 && !(gimple_call_flags (stmt) & ECF_CONST))
4374 /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
4375 if (gimple_call_internal_p (stmt))
4376 switch (gimple_call_internal_fn (stmt))
4378 case IFN_GOMP_SIMD_LANE:
4380 struct loop *loop = gimple_bb (stmt)->loop_father;
4381 tree uid = gimple_call_arg (stmt, 0);
4382 gcc_assert (TREE_CODE (uid) == SSA_NAME);
4383 if (loop == NULL
4384 || loop->simduid != SSA_NAME_VAR (uid))
4385 clobbers_memory = true;
4386 break;
4388 case IFN_MASK_LOAD:
4389 case IFN_MASK_STORE:
4390 break;
4391 default:
4392 clobbers_memory = true;
4393 break;
4395 else
4396 clobbers_memory = true;
4398 else if (stmt_code == GIMPLE_ASM
4399 && (gimple_asm_volatile_p (stmt) || gimple_vuse (stmt)))
4400 clobbers_memory = true;
4402 if (!gimple_vuse (stmt))
4403 return clobbers_memory;
4405 if (stmt_code == GIMPLE_ASSIGN)
4407 tree base;
4408 op0 = gimple_assign_lhs (stmt);
4409 op1 = gimple_assign_rhs1 (stmt);
4411 if (DECL_P (op1)
4412 || (REFERENCE_CLASS_P (op1)
4413 && (base = get_base_address (op1))
4414 && TREE_CODE (base) != SSA_NAME))
4416 ref.ref = op1;
4417 ref.is_read = true;
4418 references->safe_push (ref);
4421 else if (stmt_code == GIMPLE_CALL)
4423 unsigned i, n;
4425 ref.is_read = false;
4426 if (gimple_call_internal_p (stmt))
4427 switch (gimple_call_internal_fn (stmt))
4429 case IFN_MASK_LOAD:
4430 if (gimple_call_lhs (stmt) == NULL_TREE)
4431 break;
4432 ref.is_read = true;
4433 case IFN_MASK_STORE:
4434 ref.ref = fold_build2 (MEM_REF,
4435 ref.is_read
4436 ? TREE_TYPE (gimple_call_lhs (stmt))
4437 : TREE_TYPE (gimple_call_arg (stmt, 3)),
4438 gimple_call_arg (stmt, 0),
4439 gimple_call_arg (stmt, 1));
4440 references->safe_push (ref);
4441 return false;
4442 default:
4443 break;
4446 op0 = gimple_call_lhs (stmt);
4447 n = gimple_call_num_args (stmt);
4448 for (i = 0; i < n; i++)
4450 op1 = gimple_call_arg (stmt, i);
4452 if (DECL_P (op1)
4453 || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
4455 ref.ref = op1;
4456 ref.is_read = true;
4457 references->safe_push (ref);
4461 else
4462 return clobbers_memory;
4464 if (op0
4465 && (DECL_P (op0)
4466 || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
4468 ref.ref = op0;
4469 ref.is_read = false;
4470 references->safe_push (ref);
4472 return clobbers_memory;
4475 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
4476 reference, returns false, otherwise returns true. NEST is the outermost
4477 loop of the loop nest in which the references should be analyzed. */
4479 bool
4480 find_data_references_in_stmt (struct loop *nest, gimple stmt,
4481 vec<data_reference_p> *datarefs)
4483 unsigned i;
4484 auto_vec<data_ref_loc, 2> references;
4485 data_ref_loc *ref;
4486 bool ret = true;
4487 data_reference_p dr;
4489 if (get_references_in_stmt (stmt, &references))
4490 return false;
4492 FOR_EACH_VEC_ELT (references, i, ref)
4494 dr = create_data_ref (nest, loop_containing_stmt (stmt),
4495 ref->ref, stmt, ref->is_read);
4496 gcc_assert (dr != NULL);
4497 datarefs->safe_push (dr);
4499 references.release ();
4500 return ret;
4503 /* Stores the data references in STMT to DATAREFS. If there is an
4504 unanalyzable reference, returns false, otherwise returns true.
4505 NEST is the outermost loop of the loop nest in which the references
4506 should be instantiated, LOOP is the loop in which the references
4507 should be analyzed. */
4509 bool
4510 graphite_find_data_references_in_stmt (loop_p nest, loop_p loop, gimple stmt,
4511 vec<data_reference_p> *datarefs)
4513 unsigned i;
4514 auto_vec<data_ref_loc, 2> references;
4515 data_ref_loc *ref;
4516 bool ret = true;
4517 data_reference_p dr;
4519 if (get_references_in_stmt (stmt, &references))
4520 return false;
4522 FOR_EACH_VEC_ELT (references, i, ref)
4524 dr = create_data_ref (nest, loop, ref->ref, stmt, ref->is_read);
4525 gcc_assert (dr != NULL);
4526 datarefs->safe_push (dr);
4529 references.release ();
4530 return ret;
4533 /* Search the data references in LOOP, and record the information into
4534 DATAREFS. Returns chrec_dont_know when failing to analyze a
4535 difficult case, returns NULL_TREE otherwise. */
4537 tree
4538 find_data_references_in_bb (struct loop *loop, basic_block bb,
4539 vec<data_reference_p> *datarefs)
4541 gimple_stmt_iterator bsi;
4543 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4545 gimple stmt = gsi_stmt (bsi);
4547 if (!find_data_references_in_stmt (loop, stmt, datarefs))
4549 struct data_reference *res;
4550 res = XCNEW (struct data_reference);
4551 datarefs->safe_push (res);
4553 return chrec_dont_know;
4557 return NULL_TREE;
4560 /* Search the data references in LOOP, and record the information into
4561 DATAREFS. Returns chrec_dont_know when failing to analyze a
4562 difficult case, returns NULL_TREE otherwise.
4564 TODO: This function should be made smarter so that it can handle address
4565 arithmetic as if they were array accesses, etc. */
4567 tree
4568 find_data_references_in_loop (struct loop *loop,
4569 vec<data_reference_p> *datarefs)
4571 basic_block bb, *bbs;
4572 unsigned int i;
4574 bbs = get_loop_body_in_dom_order (loop);
4576 for (i = 0; i < loop->num_nodes; i++)
4578 bb = bbs[i];
4580 if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
4582 free (bbs);
4583 return chrec_dont_know;
4586 free (bbs);
4588 return NULL_TREE;
4591 /* Recursive helper function. */
4593 static bool
4594 find_loop_nest_1 (struct loop *loop, vec<loop_p> *loop_nest)
4596 /* Inner loops of the nest should not contain siblings. Example:
4597 when there are two consecutive loops,
4599 | loop_0
4600 | loop_1
4601 | A[{0, +, 1}_1]
4602 | endloop_1
4603 | loop_2
4604 | A[{0, +, 1}_2]
4605 | endloop_2
4606 | endloop_0
4608 the dependence relation cannot be captured by the distance
4609 abstraction. */
4610 if (loop->next)
4611 return false;
4613 loop_nest->safe_push (loop);
4614 if (loop->inner)
4615 return find_loop_nest_1 (loop->inner, loop_nest);
4616 return true;
4619 /* Return false when the LOOP is not well nested. Otherwise return
4620 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4621 contain the loops from the outermost to the innermost, as they will
4622 appear in the classic distance vector. */
4624 bool
4625 find_loop_nest (struct loop *loop, vec<loop_p> *loop_nest)
4627 loop_nest->safe_push (loop);
4628 if (loop->inner)
4629 return find_loop_nest_1 (loop->inner, loop_nest);
4630 return true;
4633 /* Returns true when the data dependences have been computed, false otherwise.
4634 Given a loop nest LOOP, the following vectors are returned:
4635 DATAREFS is initialized to all the array elements contained in this loop,
4636 DEPENDENCE_RELATIONS contains the relations between the data references.
4637 Compute read-read and self relations if
4638 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4640 bool
4641 compute_data_dependences_for_loop (struct loop *loop,
4642 bool compute_self_and_read_read_dependences,
4643 vec<loop_p> *loop_nest,
4644 vec<data_reference_p> *datarefs,
4645 vec<ddr_p> *dependence_relations)
4647 bool res = true;
4649 memset (&dependence_stats, 0, sizeof (dependence_stats));
4651 /* If the loop nest is not well formed, or one of the data references
4652 is not computable, give up without spending time to compute other
4653 dependences. */
4654 if (!loop
4655 || !find_loop_nest (loop, loop_nest)
4656 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
4657 || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
4658 compute_self_and_read_read_dependences))
4659 res = false;
4661 if (dump_file && (dump_flags & TDF_STATS))
4663 fprintf (dump_file, "Dependence tester statistics:\n");
4665 fprintf (dump_file, "Number of dependence tests: %d\n",
4666 dependence_stats.num_dependence_tests);
4667 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
4668 dependence_stats.num_dependence_dependent);
4669 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
4670 dependence_stats.num_dependence_independent);
4671 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
4672 dependence_stats.num_dependence_undetermined);
4674 fprintf (dump_file, "Number of subscript tests: %d\n",
4675 dependence_stats.num_subscript_tests);
4676 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
4677 dependence_stats.num_subscript_undetermined);
4678 fprintf (dump_file, "Number of same subscript function: %d\n",
4679 dependence_stats.num_same_subscript_function);
4681 fprintf (dump_file, "Number of ziv tests: %d\n",
4682 dependence_stats.num_ziv);
4683 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4684 dependence_stats.num_ziv_dependent);
4685 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4686 dependence_stats.num_ziv_independent);
4687 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
4688 dependence_stats.num_ziv_unimplemented);
4690 fprintf (dump_file, "Number of siv tests: %d\n",
4691 dependence_stats.num_siv);
4692 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4693 dependence_stats.num_siv_dependent);
4694 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4695 dependence_stats.num_siv_independent);
4696 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4697 dependence_stats.num_siv_unimplemented);
4699 fprintf (dump_file, "Number of miv tests: %d\n",
4700 dependence_stats.num_miv);
4701 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4702 dependence_stats.num_miv_dependent);
4703 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4704 dependence_stats.num_miv_independent);
4705 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4706 dependence_stats.num_miv_unimplemented);
4709 return res;
4712 /* Returns true when the data dependences for the basic block BB have been
4713 computed, false otherwise.
4714 DATAREFS is initialized to all the array elements contained in this basic
4715 block, DEPENDENCE_RELATIONS contains the relations between the data
4716 references. Compute read-read and self relations if
4717 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4718 bool
4719 compute_data_dependences_for_bb (basic_block bb,
4720 bool compute_self_and_read_read_dependences,
4721 vec<data_reference_p> *datarefs,
4722 vec<ddr_p> *dependence_relations)
4724 if (find_data_references_in_bb (NULL, bb, datarefs) == chrec_dont_know)
4725 return false;
4727 return compute_all_dependences (*datarefs, dependence_relations, vNULL,
4728 compute_self_and_read_read_dependences);
4731 /* Entry point (for testing only). Analyze all the data references
4732 and the dependence relations in LOOP.
4734 The data references are computed first.
4736 A relation on these nodes is represented by a complete graph. Some
4737 of the relations could be of no interest, thus the relations can be
4738 computed on demand.
4740 In the following function we compute all the relations. This is
4741 just a first implementation that is here for:
4742 - for showing how to ask for the dependence relations,
4743 - for the debugging the whole dependence graph,
4744 - for the dejagnu testcases and maintenance.
4746 It is possible to ask only for a part of the graph, avoiding to
4747 compute the whole dependence graph. The computed dependences are
4748 stored in a knowledge base (KB) such that later queries don't
4749 recompute the same information. The implementation of this KB is
4750 transparent to the optimizer, and thus the KB can be changed with a
4751 more efficient implementation, or the KB could be disabled. */
4752 static void
4753 analyze_all_data_dependences (struct loop *loop)
4755 unsigned int i;
4756 int nb_data_refs = 10;
4757 vec<data_reference_p> datarefs;
4758 datarefs.create (nb_data_refs);
4759 vec<ddr_p> dependence_relations;
4760 dependence_relations.create (nb_data_refs * nb_data_refs);
4761 vec<loop_p> loop_nest;
4762 loop_nest.create (3);
4764 /* Compute DDs on the whole function. */
4765 compute_data_dependences_for_loop (loop, false, &loop_nest, &datarefs,
4766 &dependence_relations);
4768 if (dump_file)
4770 dump_data_dependence_relations (dump_file, dependence_relations);
4771 fprintf (dump_file, "\n\n");
4773 if (dump_flags & TDF_DETAILS)
4774 dump_dist_dir_vectors (dump_file, dependence_relations);
4776 if (dump_flags & TDF_STATS)
4778 unsigned nb_top_relations = 0;
4779 unsigned nb_bot_relations = 0;
4780 unsigned nb_chrec_relations = 0;
4781 struct data_dependence_relation *ddr;
4783 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4785 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
4786 nb_top_relations++;
4788 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4789 nb_bot_relations++;
4791 else
4792 nb_chrec_relations++;
4795 gather_stats_on_scev_database ();
4799 loop_nest.release ();
4800 free_dependence_relations (dependence_relations);
4801 free_data_refs (datarefs);
4804 /* Computes all the data dependences and check that the results of
4805 several analyzers are the same. */
4807 void
4808 tree_check_data_deps (void)
4810 struct loop *loop_nest;
4812 FOR_EACH_LOOP (loop_nest, 0)
4813 analyze_all_data_dependences (loop_nest);
4816 /* Free the memory used by a data dependence relation DDR. */
4818 void
4819 free_dependence_relation (struct data_dependence_relation *ddr)
4821 if (ddr == NULL)
4822 return;
4824 if (DDR_SUBSCRIPTS (ddr).exists ())
4825 free_subscripts (DDR_SUBSCRIPTS (ddr));
4826 DDR_DIST_VECTS (ddr).release ();
4827 DDR_DIR_VECTS (ddr).release ();
4829 free (ddr);
4832 /* Free the memory used by the data dependence relations from
4833 DEPENDENCE_RELATIONS. */
4835 void
4836 free_dependence_relations (vec<ddr_p> dependence_relations)
4838 unsigned int i;
4839 struct data_dependence_relation *ddr;
4841 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4842 if (ddr)
4843 free_dependence_relation (ddr);
4845 dependence_relations.release ();
4848 /* Free the memory used by the data references from DATAREFS. */
4850 void
4851 free_data_refs (vec<data_reference_p> datarefs)
4853 unsigned int i;
4854 struct data_reference *dr;
4856 FOR_EACH_VEC_ELT (datarefs, i, dr)
4857 free_data_ref (dr);
4858 datarefs.release ();