2014-04-14 Martin Jambor <mjambor@suse.cz>
[official-gcc.git] / gcc / tree-data-ref.c
blob01d0a7a79d832826f0e0109f20c5ed129f4bd388
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, pvolatilep;
620 op0 = TREE_OPERAND (op0, 0);
621 base = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset,
622 &pmode, &punsignedp, &pvolatilep, false);
624 if (pbitpos % BITS_PER_UNIT != 0)
625 return false;
626 base = build_fold_addr_expr (base);
627 off0 = ssize_int (pbitpos / BITS_PER_UNIT);
629 if (poffset)
631 split_constant_offset (poffset, &poffset, &off1);
632 off0 = size_binop (PLUS_EXPR, off0, off1);
633 if (POINTER_TYPE_P (TREE_TYPE (base)))
634 base = fold_build_pointer_plus (base, poffset);
635 else
636 base = fold_build2 (PLUS_EXPR, TREE_TYPE (base), base,
637 fold_convert (TREE_TYPE (base), poffset));
640 var0 = fold_convert (type, base);
642 /* If variable length types are involved, punt, otherwise casts
643 might be converted into ARRAY_REFs in gimplify_conversion.
644 To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
645 possibly no longer appears in current GIMPLE, might resurface.
646 This perhaps could run
647 if (CONVERT_EXPR_P (var0))
649 gimplify_conversion (&var0);
650 // Attempt to fill in any within var0 found ARRAY_REF's
651 // element size from corresponding op embedded ARRAY_REF,
652 // if unsuccessful, just punt.
653 } */
654 while (POINTER_TYPE_P (type))
655 type = TREE_TYPE (type);
656 if (int_size_in_bytes (type) < 0)
657 return false;
659 *var = var0;
660 *off = off0;
661 return true;
664 case SSA_NAME:
666 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
667 enum tree_code subcode;
669 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
670 return false;
672 var0 = gimple_assign_rhs1 (def_stmt);
673 subcode = gimple_assign_rhs_code (def_stmt);
674 var1 = gimple_assign_rhs2 (def_stmt);
676 return split_constant_offset_1 (type, var0, subcode, var1, var, off);
678 CASE_CONVERT:
680 /* We must not introduce undefined overflow, and we must not change the value.
681 Hence we're okay if the inner type doesn't overflow to start with
682 (pointer or signed), the outer type also is an integer or pointer
683 and the outer precision is at least as large as the inner. */
684 tree itype = TREE_TYPE (op0);
685 if ((POINTER_TYPE_P (itype)
686 || (INTEGRAL_TYPE_P (itype) && TYPE_OVERFLOW_UNDEFINED (itype)))
687 && TYPE_PRECISION (type) >= TYPE_PRECISION (itype)
688 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)))
690 split_constant_offset (op0, &var0, off);
691 *var = fold_convert (type, var0);
692 return true;
694 return false;
697 default:
698 return false;
702 /* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
703 will be ssizetype. */
705 void
706 split_constant_offset (tree exp, tree *var, tree *off)
708 tree type = TREE_TYPE (exp), otype, op0, op1, e, o;
709 enum tree_code code;
711 *var = exp;
712 *off = ssize_int (0);
713 STRIP_NOPS (exp);
715 if (tree_is_chrec (exp)
716 || get_gimple_rhs_class (TREE_CODE (exp)) == GIMPLE_TERNARY_RHS)
717 return;
719 otype = TREE_TYPE (exp);
720 code = TREE_CODE (exp);
721 extract_ops_from_tree (exp, &code, &op0, &op1);
722 if (split_constant_offset_1 (otype, op0, code, op1, &e, &o))
724 *var = fold_convert (type, e);
725 *off = o;
729 /* Returns the address ADDR of an object in a canonical shape (without nop
730 casts, and with type of pointer to the object). */
732 static tree
733 canonicalize_base_object_address (tree addr)
735 tree orig = addr;
737 STRIP_NOPS (addr);
739 /* The base address may be obtained by casting from integer, in that case
740 keep the cast. */
741 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
742 return orig;
744 if (TREE_CODE (addr) != ADDR_EXPR)
745 return addr;
747 return build_fold_addr_expr (TREE_OPERAND (addr, 0));
750 /* Analyzes the behavior of the memory reference DR in the innermost loop or
751 basic block that contains it. Returns true if analysis succeed or false
752 otherwise. */
754 bool
755 dr_analyze_innermost (struct data_reference *dr, struct loop *nest)
757 gimple stmt = DR_STMT (dr);
758 struct loop *loop = loop_containing_stmt (stmt);
759 tree ref = DR_REF (dr);
760 HOST_WIDE_INT pbitsize, pbitpos;
761 tree base, poffset;
762 enum machine_mode pmode;
763 int punsignedp, pvolatilep;
764 affine_iv base_iv, offset_iv;
765 tree init, dinit, step;
766 bool in_loop = (loop && loop->num);
768 if (dump_file && (dump_flags & TDF_DETAILS))
769 fprintf (dump_file, "analyze_innermost: ");
771 base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset,
772 &pmode, &punsignedp, &pvolatilep, false);
773 gcc_assert (base != NULL_TREE);
775 if (pbitpos % BITS_PER_UNIT != 0)
777 if (dump_file && (dump_flags & TDF_DETAILS))
778 fprintf (dump_file, "failed: bit offset alignment.\n");
779 return false;
782 if (TREE_CODE (base) == MEM_REF)
784 if (!integer_zerop (TREE_OPERAND (base, 1)))
786 double_int moff = mem_ref_offset (base);
787 tree mofft = double_int_to_tree (sizetype, moff);
788 if (!poffset)
789 poffset = mofft;
790 else
791 poffset = size_binop (PLUS_EXPR, poffset, mofft);
793 base = TREE_OPERAND (base, 0);
795 else
796 base = build_fold_addr_expr (base);
798 if (in_loop)
800 if (!simple_iv (loop, loop_containing_stmt (stmt), base, &base_iv,
801 nest ? true : false))
803 if (nest)
805 if (dump_file && (dump_flags & TDF_DETAILS))
806 fprintf (dump_file, "failed: evolution of base is not"
807 " affine.\n");
808 return false;
810 else
812 base_iv.base = base;
813 base_iv.step = ssize_int (0);
814 base_iv.no_overflow = true;
818 else
820 base_iv.base = base;
821 base_iv.step = ssize_int (0);
822 base_iv.no_overflow = true;
825 if (!poffset)
827 offset_iv.base = ssize_int (0);
828 offset_iv.step = ssize_int (0);
830 else
832 if (!in_loop)
834 offset_iv.base = poffset;
835 offset_iv.step = ssize_int (0);
837 else if (!simple_iv (loop, loop_containing_stmt (stmt),
838 poffset, &offset_iv,
839 nest ? true : false))
841 if (nest)
843 if (dump_file && (dump_flags & TDF_DETAILS))
844 fprintf (dump_file, "failed: evolution of offset is not"
845 " affine.\n");
846 return false;
848 else
850 offset_iv.base = poffset;
851 offset_iv.step = ssize_int (0);
856 init = ssize_int (pbitpos / BITS_PER_UNIT);
857 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
858 init = size_binop (PLUS_EXPR, init, dinit);
859 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
860 init = size_binop (PLUS_EXPR, init, dinit);
862 step = size_binop (PLUS_EXPR,
863 fold_convert (ssizetype, base_iv.step),
864 fold_convert (ssizetype, offset_iv.step));
866 DR_BASE_ADDRESS (dr) = canonicalize_base_object_address (base_iv.base);
868 DR_OFFSET (dr) = fold_convert (ssizetype, offset_iv.base);
869 DR_INIT (dr) = init;
870 DR_STEP (dr) = step;
872 DR_ALIGNED_TO (dr) = size_int (highest_pow2_factor (offset_iv.base));
874 if (dump_file && (dump_flags & TDF_DETAILS))
875 fprintf (dump_file, "success.\n");
877 return true;
880 /* Determines the base object and the list of indices of memory reference
881 DR, analyzed in LOOP and instantiated in loop nest NEST. */
883 static void
884 dr_analyze_indices (struct data_reference *dr, loop_p nest, loop_p loop)
886 vec<tree> access_fns = vNULL;
887 tree ref, op;
888 tree base, off, access_fn;
889 basic_block before_loop;
891 /* If analyzing a basic-block there are no indices to analyze
892 and thus no access functions. */
893 if (!nest)
895 DR_BASE_OBJECT (dr) = DR_REF (dr);
896 DR_ACCESS_FNS (dr).create (0);
897 return;
900 ref = DR_REF (dr);
901 before_loop = block_before_loop (nest);
903 /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
904 into a two element array with a constant index. The base is
905 then just the immediate underlying object. */
906 if (TREE_CODE (ref) == REALPART_EXPR)
908 ref = TREE_OPERAND (ref, 0);
909 access_fns.safe_push (integer_zero_node);
911 else if (TREE_CODE (ref) == IMAGPART_EXPR)
913 ref = TREE_OPERAND (ref, 0);
914 access_fns.safe_push (integer_one_node);
917 /* Analyze access functions of dimensions we know to be independent. */
918 while (handled_component_p (ref))
920 if (TREE_CODE (ref) == ARRAY_REF)
922 op = TREE_OPERAND (ref, 1);
923 access_fn = analyze_scalar_evolution (loop, op);
924 access_fn = instantiate_scev (before_loop, loop, access_fn);
925 access_fns.safe_push (access_fn);
927 else if (TREE_CODE (ref) == COMPONENT_REF
928 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
930 /* For COMPONENT_REFs of records (but not unions!) use the
931 FIELD_DECL offset as constant access function so we can
932 disambiguate a[i].f1 and a[i].f2. */
933 tree off = component_ref_field_offset (ref);
934 off = size_binop (PLUS_EXPR,
935 size_binop (MULT_EXPR,
936 fold_convert (bitsizetype, off),
937 bitsize_int (BITS_PER_UNIT)),
938 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
939 access_fns.safe_push (off);
941 else
942 /* If we have an unhandled component we could not translate
943 to an access function stop analyzing. We have determined
944 our base object in this case. */
945 break;
947 ref = TREE_OPERAND (ref, 0);
950 /* If the address operand of a MEM_REF base has an evolution in the
951 analyzed nest, add it as an additional independent access-function. */
952 if (TREE_CODE (ref) == MEM_REF)
954 op = TREE_OPERAND (ref, 0);
955 access_fn = analyze_scalar_evolution (loop, op);
956 access_fn = instantiate_scev (before_loop, loop, access_fn);
957 if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
959 tree orig_type;
960 tree memoff = TREE_OPERAND (ref, 1);
961 base = initial_condition (access_fn);
962 orig_type = TREE_TYPE (base);
963 STRIP_USELESS_TYPE_CONVERSION (base);
964 split_constant_offset (base, &base, &off);
965 /* Fold the MEM_REF offset into the evolutions initial
966 value to make more bases comparable. */
967 if (!integer_zerop (memoff))
969 off = size_binop (PLUS_EXPR, off,
970 fold_convert (ssizetype, memoff));
971 memoff = build_int_cst (TREE_TYPE (memoff), 0);
973 access_fn = chrec_replace_initial_condition
974 (access_fn, fold_convert (orig_type, off));
975 /* ??? This is still not a suitable base object for
976 dr_may_alias_p - the base object needs to be an
977 access that covers the object as whole. With
978 an evolution in the pointer this cannot be
979 guaranteed.
980 As a band-aid, mark the access so we can special-case
981 it in dr_may_alias_p. */
982 ref = fold_build2_loc (EXPR_LOCATION (ref),
983 MEM_REF, TREE_TYPE (ref),
984 base, memoff);
985 DR_UNCONSTRAINED_BASE (dr) = true;
986 access_fns.safe_push (access_fn);
989 else if (DECL_P (ref))
991 /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
992 ref = build2 (MEM_REF, TREE_TYPE (ref),
993 build_fold_addr_expr (ref),
994 build_int_cst (reference_alias_ptr_type (ref), 0));
997 DR_BASE_OBJECT (dr) = ref;
998 DR_ACCESS_FNS (dr) = access_fns;
1001 /* Extracts the alias analysis information from the memory reference DR. */
1003 static void
1004 dr_analyze_alias (struct data_reference *dr)
1006 tree ref = DR_REF (dr);
1007 tree base = get_base_address (ref), addr;
1009 if (INDIRECT_REF_P (base)
1010 || TREE_CODE (base) == MEM_REF)
1012 addr = TREE_OPERAND (base, 0);
1013 if (TREE_CODE (addr) == SSA_NAME)
1014 DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
1018 /* Frees data reference DR. */
1020 void
1021 free_data_ref (data_reference_p dr)
1023 DR_ACCESS_FNS (dr).release ();
1024 free (dr);
1027 /* Analyzes memory reference MEMREF accessed in STMT. The reference
1028 is read if IS_READ is true, write otherwise. Returns the
1029 data_reference description of MEMREF. NEST is the outermost loop
1030 in which the reference should be instantiated, LOOP is the loop in
1031 which the data reference should be analyzed. */
1033 struct data_reference *
1034 create_data_ref (loop_p nest, loop_p loop, tree memref, gimple stmt,
1035 bool is_read)
1037 struct data_reference *dr;
1039 if (dump_file && (dump_flags & TDF_DETAILS))
1041 fprintf (dump_file, "Creating dr for ");
1042 print_generic_expr (dump_file, memref, TDF_SLIM);
1043 fprintf (dump_file, "\n");
1046 dr = XCNEW (struct data_reference);
1047 DR_STMT (dr) = stmt;
1048 DR_REF (dr) = memref;
1049 DR_IS_READ (dr) = is_read;
1051 dr_analyze_innermost (dr, nest);
1052 dr_analyze_indices (dr, nest, loop);
1053 dr_analyze_alias (dr);
1055 if (dump_file && (dump_flags & TDF_DETAILS))
1057 unsigned i;
1058 fprintf (dump_file, "\tbase_address: ");
1059 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1060 fprintf (dump_file, "\n\toffset from base address: ");
1061 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1062 fprintf (dump_file, "\n\tconstant offset from base address: ");
1063 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1064 fprintf (dump_file, "\n\tstep: ");
1065 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1066 fprintf (dump_file, "\n\taligned to: ");
1067 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
1068 fprintf (dump_file, "\n\tbase_object: ");
1069 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1070 fprintf (dump_file, "\n");
1071 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
1073 fprintf (dump_file, "\tAccess function %d: ", i);
1074 print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
1078 return dr;
1081 /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
1082 expressions. */
1083 static bool
1084 dr_equal_offsets_p1 (tree offset1, tree offset2)
1086 bool res;
1088 STRIP_NOPS (offset1);
1089 STRIP_NOPS (offset2);
1091 if (offset1 == offset2)
1092 return true;
1094 if (TREE_CODE (offset1) != TREE_CODE (offset2)
1095 || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
1096 return false;
1098 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
1099 TREE_OPERAND (offset2, 0));
1101 if (!res || !BINARY_CLASS_P (offset1))
1102 return res;
1104 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
1105 TREE_OPERAND (offset2, 1));
1107 return res;
1110 /* Check if DRA and DRB have equal offsets. */
1111 bool
1112 dr_equal_offsets_p (struct data_reference *dra,
1113 struct data_reference *drb)
1115 tree offset1, offset2;
1117 offset1 = DR_OFFSET (dra);
1118 offset2 = DR_OFFSET (drb);
1120 return dr_equal_offsets_p1 (offset1, offset2);
1123 /* Returns true if FNA == FNB. */
1125 static bool
1126 affine_function_equal_p (affine_fn fna, affine_fn fnb)
1128 unsigned i, n = fna.length ();
1130 if (n != fnb.length ())
1131 return false;
1133 for (i = 0; i < n; i++)
1134 if (!operand_equal_p (fna[i], fnb[i], 0))
1135 return false;
1137 return true;
1140 /* If all the functions in CF are the same, returns one of them,
1141 otherwise returns NULL. */
1143 static affine_fn
1144 common_affine_function (conflict_function *cf)
1146 unsigned i;
1147 affine_fn comm;
1149 if (!CF_NONTRIVIAL_P (cf))
1150 return affine_fn ();
1152 comm = cf->fns[0];
1154 for (i = 1; i < cf->n; i++)
1155 if (!affine_function_equal_p (comm, cf->fns[i]))
1156 return affine_fn ();
1158 return comm;
1161 /* Returns the base of the affine function FN. */
1163 static tree
1164 affine_function_base (affine_fn fn)
1166 return fn[0];
1169 /* Returns true if FN is a constant. */
1171 static bool
1172 affine_function_constant_p (affine_fn fn)
1174 unsigned i;
1175 tree coef;
1177 for (i = 1; fn.iterate (i, &coef); i++)
1178 if (!integer_zerop (coef))
1179 return false;
1181 return true;
1184 /* Returns true if FN is the zero constant function. */
1186 static bool
1187 affine_function_zero_p (affine_fn fn)
1189 return (integer_zerop (affine_function_base (fn))
1190 && affine_function_constant_p (fn));
1193 /* Returns a signed integer type with the largest precision from TA
1194 and TB. */
1196 static tree
1197 signed_type_for_types (tree ta, tree tb)
1199 if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
1200 return signed_type_for (ta);
1201 else
1202 return signed_type_for (tb);
1205 /* Applies operation OP on affine functions FNA and FNB, and returns the
1206 result. */
1208 static affine_fn
1209 affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
1211 unsigned i, n, m;
1212 affine_fn ret;
1213 tree coef;
1215 if (fnb.length () > fna.length ())
1217 n = fna.length ();
1218 m = fnb.length ();
1220 else
1222 n = fnb.length ();
1223 m = fna.length ();
1226 ret.create (m);
1227 for (i = 0; i < n; i++)
1229 tree type = signed_type_for_types (TREE_TYPE (fna[i]),
1230 TREE_TYPE (fnb[i]));
1231 ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
1234 for (; fna.iterate (i, &coef); i++)
1235 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1236 coef, integer_zero_node));
1237 for (; fnb.iterate (i, &coef); i++)
1238 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1239 integer_zero_node, coef));
1241 return ret;
1244 /* Returns the sum of affine functions FNA and FNB. */
1246 static affine_fn
1247 affine_fn_plus (affine_fn fna, affine_fn fnb)
1249 return affine_fn_op (PLUS_EXPR, fna, fnb);
1252 /* Returns the difference of affine functions FNA and FNB. */
1254 static affine_fn
1255 affine_fn_minus (affine_fn fna, affine_fn fnb)
1257 return affine_fn_op (MINUS_EXPR, fna, fnb);
1260 /* Frees affine function FN. */
1262 static void
1263 affine_fn_free (affine_fn fn)
1265 fn.release ();
1268 /* Determine for each subscript in the data dependence relation DDR
1269 the distance. */
1271 static void
1272 compute_subscript_distance (struct data_dependence_relation *ddr)
1274 conflict_function *cf_a, *cf_b;
1275 affine_fn fn_a, fn_b, diff;
1277 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1279 unsigned int i;
1281 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1283 struct subscript *subscript;
1285 subscript = DDR_SUBSCRIPT (ddr, i);
1286 cf_a = SUB_CONFLICTS_IN_A (subscript);
1287 cf_b = SUB_CONFLICTS_IN_B (subscript);
1289 fn_a = common_affine_function (cf_a);
1290 fn_b = common_affine_function (cf_b);
1291 if (!fn_a.exists () || !fn_b.exists ())
1293 SUB_DISTANCE (subscript) = chrec_dont_know;
1294 return;
1296 diff = affine_fn_minus (fn_a, fn_b);
1298 if (affine_function_constant_p (diff))
1299 SUB_DISTANCE (subscript) = affine_function_base (diff);
1300 else
1301 SUB_DISTANCE (subscript) = chrec_dont_know;
1303 affine_fn_free (diff);
1308 /* Returns the conflict function for "unknown". */
1310 static conflict_function *
1311 conflict_fn_not_known (void)
1313 conflict_function *fn = XCNEW (conflict_function);
1314 fn->n = NOT_KNOWN;
1316 return fn;
1319 /* Returns the conflict function for "independent". */
1321 static conflict_function *
1322 conflict_fn_no_dependence (void)
1324 conflict_function *fn = XCNEW (conflict_function);
1325 fn->n = NO_DEPENDENCE;
1327 return fn;
1330 /* Returns true if the address of OBJ is invariant in LOOP. */
1332 static bool
1333 object_address_invariant_in_loop_p (const struct loop *loop, const_tree obj)
1335 while (handled_component_p (obj))
1337 if (TREE_CODE (obj) == ARRAY_REF)
1339 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1340 need to check the stride and the lower bound of the reference. */
1341 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1342 loop->num)
1343 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 3),
1344 loop->num))
1345 return false;
1347 else if (TREE_CODE (obj) == COMPONENT_REF)
1349 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1350 loop->num))
1351 return false;
1353 obj = TREE_OPERAND (obj, 0);
1356 if (!INDIRECT_REF_P (obj)
1357 && TREE_CODE (obj) != MEM_REF)
1358 return true;
1360 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
1361 loop->num);
1364 /* Returns false if we can prove that data references A and B do not alias,
1365 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
1366 considered. */
1368 bool
1369 dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
1370 bool loop_nest)
1372 tree addr_a = DR_BASE_OBJECT (a);
1373 tree addr_b = DR_BASE_OBJECT (b);
1375 /* If we are not processing a loop nest but scalar code we
1376 do not need to care about possible cross-iteration dependences
1377 and thus can process the full original reference. Do so,
1378 similar to how loop invariant motion applies extra offset-based
1379 disambiguation. */
1380 if (!loop_nest)
1382 aff_tree off1, off2;
1383 double_int size1, size2;
1384 get_inner_reference_aff (DR_REF (a), &off1, &size1);
1385 get_inner_reference_aff (DR_REF (b), &off2, &size2);
1386 aff_combination_scale (&off1, double_int_minus_one);
1387 aff_combination_add (&off2, &off1);
1388 if (aff_comb_cannot_overlap_p (&off2, size1, size2))
1389 return false;
1392 /* If we had an evolution in a MEM_REF BASE_OBJECT we do not know
1393 the size of the base-object. So we cannot do any offset/overlap
1394 based analysis but have to rely on points-to information only. */
1395 if (TREE_CODE (addr_a) == MEM_REF
1396 && DR_UNCONSTRAINED_BASE (a))
1398 if (TREE_CODE (addr_b) == MEM_REF
1399 && DR_UNCONSTRAINED_BASE (b))
1400 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1401 TREE_OPERAND (addr_b, 0));
1402 else
1403 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1404 build_fold_addr_expr (addr_b));
1406 else if (TREE_CODE (addr_b) == MEM_REF
1407 && DR_UNCONSTRAINED_BASE (b))
1408 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
1409 TREE_OPERAND (addr_b, 0));
1411 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1412 that is being subsetted in the loop nest. */
1413 if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
1414 return refs_output_dependent_p (addr_a, addr_b);
1415 else if (DR_IS_READ (a) && DR_IS_WRITE (b))
1416 return refs_anti_dependent_p (addr_a, addr_b);
1417 return refs_may_alias_p (addr_a, addr_b);
1420 /* Initialize a data dependence relation between data accesses A and
1421 B. NB_LOOPS is the number of loops surrounding the references: the
1422 size of the classic distance/direction vectors. */
1424 struct data_dependence_relation *
1425 initialize_data_dependence_relation (struct data_reference *a,
1426 struct data_reference *b,
1427 vec<loop_p> loop_nest)
1429 struct data_dependence_relation *res;
1430 unsigned int i;
1432 res = XNEW (struct data_dependence_relation);
1433 DDR_A (res) = a;
1434 DDR_B (res) = b;
1435 DDR_LOOP_NEST (res).create (0);
1436 DDR_REVERSED_P (res) = false;
1437 DDR_SUBSCRIPTS (res).create (0);
1438 DDR_DIR_VECTS (res).create (0);
1439 DDR_DIST_VECTS (res).create (0);
1441 if (a == NULL || b == NULL)
1443 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1444 return res;
1447 /* If the data references do not alias, then they are independent. */
1448 if (!dr_may_alias_p (a, b, loop_nest.exists ()))
1450 DDR_ARE_DEPENDENT (res) = chrec_known;
1451 return res;
1454 /* The case where the references are exactly the same. */
1455 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
1457 if (loop_nest.exists ()
1458 && !object_address_invariant_in_loop_p (loop_nest[0],
1459 DR_BASE_OBJECT (a)))
1461 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1462 return res;
1464 DDR_AFFINE_P (res) = true;
1465 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1466 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1467 DDR_LOOP_NEST (res) = loop_nest;
1468 DDR_INNER_LOOP (res) = 0;
1469 DDR_SELF_REFERENCE (res) = true;
1470 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1472 struct subscript *subscript;
1474 subscript = XNEW (struct subscript);
1475 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1476 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1477 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1478 SUB_DISTANCE (subscript) = chrec_dont_know;
1479 DDR_SUBSCRIPTS (res).safe_push (subscript);
1481 return res;
1484 /* If the references do not access the same object, we do not know
1485 whether they alias or not. */
1486 if (!operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0))
1488 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1489 return res;
1492 /* If the base of the object is not invariant in the loop nest, we cannot
1493 analyze it. TODO -- in fact, it would suffice to record that there may
1494 be arbitrary dependences in the loops where the base object varies. */
1495 if (loop_nest.exists ()
1496 && !object_address_invariant_in_loop_p (loop_nest[0],
1497 DR_BASE_OBJECT (a)))
1499 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1500 return res;
1503 /* If the number of dimensions of the access to not agree we can have
1504 a pointer access to a component of the array element type and an
1505 array access while the base-objects are still the same. Punt. */
1506 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1508 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1509 return res;
1512 DDR_AFFINE_P (res) = true;
1513 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1514 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1515 DDR_LOOP_NEST (res) = loop_nest;
1516 DDR_INNER_LOOP (res) = 0;
1517 DDR_SELF_REFERENCE (res) = false;
1519 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1521 struct subscript *subscript;
1523 subscript = XNEW (struct subscript);
1524 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1525 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1526 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1527 SUB_DISTANCE (subscript) = chrec_dont_know;
1528 DDR_SUBSCRIPTS (res).safe_push (subscript);
1531 return res;
1534 /* Frees memory used by the conflict function F. */
1536 static void
1537 free_conflict_function (conflict_function *f)
1539 unsigned i;
1541 if (CF_NONTRIVIAL_P (f))
1543 for (i = 0; i < f->n; i++)
1544 affine_fn_free (f->fns[i]);
1546 free (f);
1549 /* Frees memory used by SUBSCRIPTS. */
1551 static void
1552 free_subscripts (vec<subscript_p> subscripts)
1554 unsigned i;
1555 subscript_p s;
1557 FOR_EACH_VEC_ELT (subscripts, i, s)
1559 free_conflict_function (s->conflicting_iterations_in_a);
1560 free_conflict_function (s->conflicting_iterations_in_b);
1561 free (s);
1563 subscripts.release ();
1566 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1567 description. */
1569 static inline void
1570 finalize_ddr_dependent (struct data_dependence_relation *ddr,
1571 tree chrec)
1573 DDR_ARE_DEPENDENT (ddr) = chrec;
1574 free_subscripts (DDR_SUBSCRIPTS (ddr));
1575 DDR_SUBSCRIPTS (ddr).create (0);
1578 /* The dependence relation DDR cannot be represented by a distance
1579 vector. */
1581 static inline void
1582 non_affine_dependence_relation (struct data_dependence_relation *ddr)
1584 if (dump_file && (dump_flags & TDF_DETAILS))
1585 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
1587 DDR_AFFINE_P (ddr) = false;
1592 /* This section contains the classic Banerjee tests. */
1594 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1595 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1597 static inline bool
1598 ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1600 return (evolution_function_is_constant_p (chrec_a)
1601 && evolution_function_is_constant_p (chrec_b));
1604 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
1605 variable, i.e., if the SIV (Single Index Variable) test is true. */
1607 static bool
1608 siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1610 if ((evolution_function_is_constant_p (chrec_a)
1611 && evolution_function_is_univariate_p (chrec_b))
1612 || (evolution_function_is_constant_p (chrec_b)
1613 && evolution_function_is_univariate_p (chrec_a)))
1614 return true;
1616 if (evolution_function_is_univariate_p (chrec_a)
1617 && evolution_function_is_univariate_p (chrec_b))
1619 switch (TREE_CODE (chrec_a))
1621 case POLYNOMIAL_CHREC:
1622 switch (TREE_CODE (chrec_b))
1624 case POLYNOMIAL_CHREC:
1625 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
1626 return false;
1628 default:
1629 return true;
1632 default:
1633 return true;
1637 return false;
1640 /* Creates a conflict function with N dimensions. The affine functions
1641 in each dimension follow. */
1643 static conflict_function *
1644 conflict_fn (unsigned n, ...)
1646 unsigned i;
1647 conflict_function *ret = XCNEW (conflict_function);
1648 va_list ap;
1650 gcc_assert (0 < n && n <= MAX_DIM);
1651 va_start (ap, n);
1653 ret->n = n;
1654 for (i = 0; i < n; i++)
1655 ret->fns[i] = va_arg (ap, affine_fn);
1656 va_end (ap);
1658 return ret;
1661 /* Returns constant affine function with value CST. */
1663 static affine_fn
1664 affine_fn_cst (tree cst)
1666 affine_fn fn;
1667 fn.create (1);
1668 fn.quick_push (cst);
1669 return fn;
1672 /* Returns affine function with single variable, CST + COEF * x_DIM. */
1674 static affine_fn
1675 affine_fn_univar (tree cst, unsigned dim, tree coef)
1677 affine_fn fn;
1678 fn.create (dim + 1);
1679 unsigned i;
1681 gcc_assert (dim > 0);
1682 fn.quick_push (cst);
1683 for (i = 1; i < dim; i++)
1684 fn.quick_push (integer_zero_node);
1685 fn.quick_push (coef);
1686 return fn;
1689 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1690 *OVERLAPS_B are initialized to the functions that describe the
1691 relation between the elements accessed twice by CHREC_A and
1692 CHREC_B. For k >= 0, the following property is verified:
1694 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1696 static void
1697 analyze_ziv_subscript (tree chrec_a,
1698 tree chrec_b,
1699 conflict_function **overlaps_a,
1700 conflict_function **overlaps_b,
1701 tree *last_conflicts)
1703 tree type, difference;
1704 dependence_stats.num_ziv++;
1706 if (dump_file && (dump_flags & TDF_DETAILS))
1707 fprintf (dump_file, "(analyze_ziv_subscript \n");
1709 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1710 chrec_a = chrec_convert (type, chrec_a, NULL);
1711 chrec_b = chrec_convert (type, chrec_b, NULL);
1712 difference = chrec_fold_minus (type, chrec_a, chrec_b);
1714 switch (TREE_CODE (difference))
1716 case INTEGER_CST:
1717 if (integer_zerop (difference))
1719 /* The difference is equal to zero: the accessed index
1720 overlaps for each iteration in the loop. */
1721 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1722 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1723 *last_conflicts = chrec_dont_know;
1724 dependence_stats.num_ziv_dependent++;
1726 else
1728 /* The accesses do not overlap. */
1729 *overlaps_a = conflict_fn_no_dependence ();
1730 *overlaps_b = conflict_fn_no_dependence ();
1731 *last_conflicts = integer_zero_node;
1732 dependence_stats.num_ziv_independent++;
1734 break;
1736 default:
1737 /* We're not sure whether the indexes overlap. For the moment,
1738 conservatively answer "don't know". */
1739 if (dump_file && (dump_flags & TDF_DETAILS))
1740 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
1742 *overlaps_a = conflict_fn_not_known ();
1743 *overlaps_b = conflict_fn_not_known ();
1744 *last_conflicts = chrec_dont_know;
1745 dependence_stats.num_ziv_unimplemented++;
1746 break;
1749 if (dump_file && (dump_flags & TDF_DETAILS))
1750 fprintf (dump_file, ")\n");
1753 /* Similar to max_stmt_executions_int, but returns the bound as a tree,
1754 and only if it fits to the int type. If this is not the case, or the
1755 bound on the number of iterations of LOOP could not be derived, returns
1756 chrec_dont_know. */
1758 static tree
1759 max_stmt_executions_tree (struct loop *loop)
1761 double_int nit;
1763 if (!max_stmt_executions (loop, &nit))
1764 return chrec_dont_know;
1766 if (!double_int_fits_to_tree_p (unsigned_type_node, nit))
1767 return chrec_dont_know;
1769 return double_int_to_tree (unsigned_type_node, nit);
1772 /* Determine whether the CHREC is always positive/negative. If the expression
1773 cannot be statically analyzed, return false, otherwise set the answer into
1774 VALUE. */
1776 static bool
1777 chrec_is_positive (tree chrec, bool *value)
1779 bool value0, value1, value2;
1780 tree end_value, nb_iter;
1782 switch (TREE_CODE (chrec))
1784 case POLYNOMIAL_CHREC:
1785 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
1786 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
1787 return false;
1789 /* FIXME -- overflows. */
1790 if (value0 == value1)
1792 *value = value0;
1793 return true;
1796 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1797 and the proof consists in showing that the sign never
1798 changes during the execution of the loop, from 0 to
1799 loop->nb_iterations. */
1800 if (!evolution_function_is_affine_p (chrec))
1801 return false;
1803 nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
1804 if (chrec_contains_undetermined (nb_iter))
1805 return false;
1807 #if 0
1808 /* TODO -- If the test is after the exit, we may decrease the number of
1809 iterations by one. */
1810 if (after_exit)
1811 nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
1812 #endif
1814 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
1816 if (!chrec_is_positive (end_value, &value2))
1817 return false;
1819 *value = value0;
1820 return value0 == value1;
1822 case INTEGER_CST:
1823 switch (tree_int_cst_sgn (chrec))
1825 case -1:
1826 *value = false;
1827 break;
1828 case 1:
1829 *value = true;
1830 break;
1831 default:
1832 return false;
1834 return true;
1836 default:
1837 return false;
1842 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1843 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1844 *OVERLAPS_B are initialized to the functions that describe the
1845 relation between the elements accessed twice by CHREC_A and
1846 CHREC_B. For k >= 0, the following property is verified:
1848 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1850 static void
1851 analyze_siv_subscript_cst_affine (tree chrec_a,
1852 tree chrec_b,
1853 conflict_function **overlaps_a,
1854 conflict_function **overlaps_b,
1855 tree *last_conflicts)
1857 bool value0, value1, value2;
1858 tree type, difference, tmp;
1860 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1861 chrec_a = chrec_convert (type, chrec_a, NULL);
1862 chrec_b = chrec_convert (type, chrec_b, NULL);
1863 difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
1865 /* Special case overlap in the first iteration. */
1866 if (integer_zerop (difference))
1868 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1869 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1870 *last_conflicts = integer_one_node;
1871 return;
1874 if (!chrec_is_positive (initial_condition (difference), &value0))
1876 if (dump_file && (dump_flags & TDF_DETAILS))
1877 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
1879 dependence_stats.num_siv_unimplemented++;
1880 *overlaps_a = conflict_fn_not_known ();
1881 *overlaps_b = conflict_fn_not_known ();
1882 *last_conflicts = chrec_dont_know;
1883 return;
1885 else
1887 if (value0 == false)
1889 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
1891 if (dump_file && (dump_flags & TDF_DETAILS))
1892 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1894 *overlaps_a = conflict_fn_not_known ();
1895 *overlaps_b = conflict_fn_not_known ();
1896 *last_conflicts = chrec_dont_know;
1897 dependence_stats.num_siv_unimplemented++;
1898 return;
1900 else
1902 if (value1 == true)
1904 /* Example:
1905 chrec_a = 12
1906 chrec_b = {10, +, 1}
1909 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1911 HOST_WIDE_INT numiter;
1912 struct loop *loop = get_chrec_loop (chrec_b);
1914 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1915 tmp = fold_build2 (EXACT_DIV_EXPR, type,
1916 fold_build1 (ABS_EXPR, type, difference),
1917 CHREC_RIGHT (chrec_b));
1918 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1919 *last_conflicts = integer_one_node;
1922 /* Perform weak-zero siv test to see if overlap is
1923 outside the loop bounds. */
1924 numiter = max_stmt_executions_int (loop);
1926 if (numiter >= 0
1927 && compare_tree_int (tmp, numiter) > 0)
1929 free_conflict_function (*overlaps_a);
1930 free_conflict_function (*overlaps_b);
1931 *overlaps_a = conflict_fn_no_dependence ();
1932 *overlaps_b = conflict_fn_no_dependence ();
1933 *last_conflicts = integer_zero_node;
1934 dependence_stats.num_siv_independent++;
1935 return;
1937 dependence_stats.num_siv_dependent++;
1938 return;
1941 /* When the step does not divide the difference, there are
1942 no overlaps. */
1943 else
1945 *overlaps_a = conflict_fn_no_dependence ();
1946 *overlaps_b = conflict_fn_no_dependence ();
1947 *last_conflicts = integer_zero_node;
1948 dependence_stats.num_siv_independent++;
1949 return;
1953 else
1955 /* Example:
1956 chrec_a = 12
1957 chrec_b = {10, +, -1}
1959 In this case, chrec_a will not overlap with chrec_b. */
1960 *overlaps_a = conflict_fn_no_dependence ();
1961 *overlaps_b = conflict_fn_no_dependence ();
1962 *last_conflicts = integer_zero_node;
1963 dependence_stats.num_siv_independent++;
1964 return;
1968 else
1970 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
1972 if (dump_file && (dump_flags & TDF_DETAILS))
1973 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1975 *overlaps_a = conflict_fn_not_known ();
1976 *overlaps_b = conflict_fn_not_known ();
1977 *last_conflicts = chrec_dont_know;
1978 dependence_stats.num_siv_unimplemented++;
1979 return;
1981 else
1983 if (value2 == false)
1985 /* Example:
1986 chrec_a = 3
1987 chrec_b = {10, +, -1}
1989 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1991 HOST_WIDE_INT numiter;
1992 struct loop *loop = get_chrec_loop (chrec_b);
1994 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1995 tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
1996 CHREC_RIGHT (chrec_b));
1997 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1998 *last_conflicts = integer_one_node;
2000 /* Perform weak-zero siv test to see if overlap is
2001 outside the loop bounds. */
2002 numiter = max_stmt_executions_int (loop);
2004 if (numiter >= 0
2005 && compare_tree_int (tmp, numiter) > 0)
2007 free_conflict_function (*overlaps_a);
2008 free_conflict_function (*overlaps_b);
2009 *overlaps_a = conflict_fn_no_dependence ();
2010 *overlaps_b = conflict_fn_no_dependence ();
2011 *last_conflicts = integer_zero_node;
2012 dependence_stats.num_siv_independent++;
2013 return;
2015 dependence_stats.num_siv_dependent++;
2016 return;
2019 /* When the step does not divide the difference, there
2020 are no overlaps. */
2021 else
2023 *overlaps_a = conflict_fn_no_dependence ();
2024 *overlaps_b = conflict_fn_no_dependence ();
2025 *last_conflicts = integer_zero_node;
2026 dependence_stats.num_siv_independent++;
2027 return;
2030 else
2032 /* Example:
2033 chrec_a = 3
2034 chrec_b = {4, +, 1}
2036 In this case, chrec_a will not overlap with chrec_b. */
2037 *overlaps_a = conflict_fn_no_dependence ();
2038 *overlaps_b = conflict_fn_no_dependence ();
2039 *last_conflicts = integer_zero_node;
2040 dependence_stats.num_siv_independent++;
2041 return;
2048 /* Helper recursive function for initializing the matrix A. Returns
2049 the initial value of CHREC. */
2051 static tree
2052 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2054 gcc_assert (chrec);
2056 switch (TREE_CODE (chrec))
2058 case POLYNOMIAL_CHREC:
2059 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST);
2061 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2062 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2064 case PLUS_EXPR:
2065 case MULT_EXPR:
2066 case MINUS_EXPR:
2068 tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2069 tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
2071 return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
2074 case NOP_EXPR:
2076 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2077 return chrec_convert (chrec_type (chrec), op, NULL);
2080 case BIT_NOT_EXPR:
2082 /* Handle ~X as -1 - X. */
2083 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2084 return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
2085 build_int_cst (TREE_TYPE (chrec), -1), op);
2088 case INTEGER_CST:
2089 return chrec;
2091 default:
2092 gcc_unreachable ();
2093 return NULL_TREE;
2097 #define FLOOR_DIV(x,y) ((x) / (y))
2099 /* Solves the special case of the Diophantine equation:
2100 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2102 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2103 number of iterations that loops X and Y run. The overlaps will be
2104 constructed as evolutions in dimension DIM. */
2106 static void
2107 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2108 affine_fn *overlaps_a,
2109 affine_fn *overlaps_b,
2110 tree *last_conflicts, int dim)
2112 if (((step_a > 0 && step_b > 0)
2113 || (step_a < 0 && step_b < 0)))
2115 int step_overlaps_a, step_overlaps_b;
2116 int gcd_steps_a_b, last_conflict, tau2;
2118 gcd_steps_a_b = gcd (step_a, step_b);
2119 step_overlaps_a = step_b / gcd_steps_a_b;
2120 step_overlaps_b = step_a / gcd_steps_a_b;
2122 if (niter > 0)
2124 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2125 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2126 last_conflict = tau2;
2127 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2129 else
2130 *last_conflicts = chrec_dont_know;
2132 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
2133 build_int_cst (NULL_TREE,
2134 step_overlaps_a));
2135 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
2136 build_int_cst (NULL_TREE,
2137 step_overlaps_b));
2140 else
2142 *overlaps_a = affine_fn_cst (integer_zero_node);
2143 *overlaps_b = affine_fn_cst (integer_zero_node);
2144 *last_conflicts = integer_zero_node;
2148 /* Solves the special case of a Diophantine equation where CHREC_A is
2149 an affine bivariate function, and CHREC_B is an affine univariate
2150 function. For example,
2152 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2154 has the following overlapping functions:
2156 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2157 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2158 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2160 FORNOW: This is a specialized implementation for a case occurring in
2161 a common benchmark. Implement the general algorithm. */
2163 static void
2164 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2165 conflict_function **overlaps_a,
2166 conflict_function **overlaps_b,
2167 tree *last_conflicts)
2169 bool xz_p, yz_p, xyz_p;
2170 int step_x, step_y, step_z;
2171 HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
2172 affine_fn overlaps_a_xz, overlaps_b_xz;
2173 affine_fn overlaps_a_yz, overlaps_b_yz;
2174 affine_fn overlaps_a_xyz, overlaps_b_xyz;
2175 affine_fn ova1, ova2, ovb;
2176 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
2178 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2179 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2180 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2182 niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
2183 niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
2184 niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
2186 if (niter_x < 0 || niter_y < 0 || niter_z < 0)
2188 if (dump_file && (dump_flags & TDF_DETAILS))
2189 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
2191 *overlaps_a = conflict_fn_not_known ();
2192 *overlaps_b = conflict_fn_not_known ();
2193 *last_conflicts = chrec_dont_know;
2194 return;
2197 niter = MIN (niter_x, niter_z);
2198 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2199 &overlaps_a_xz,
2200 &overlaps_b_xz,
2201 &last_conflicts_xz, 1);
2202 niter = MIN (niter_y, niter_z);
2203 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2204 &overlaps_a_yz,
2205 &overlaps_b_yz,
2206 &last_conflicts_yz, 2);
2207 niter = MIN (niter_x, niter_z);
2208 niter = MIN (niter_y, niter);
2209 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2210 &overlaps_a_xyz,
2211 &overlaps_b_xyz,
2212 &last_conflicts_xyz, 3);
2214 xz_p = !integer_zerop (last_conflicts_xz);
2215 yz_p = !integer_zerop (last_conflicts_yz);
2216 xyz_p = !integer_zerop (last_conflicts_xyz);
2218 if (xz_p || yz_p || xyz_p)
2220 ova1 = affine_fn_cst (integer_zero_node);
2221 ova2 = affine_fn_cst (integer_zero_node);
2222 ovb = affine_fn_cst (integer_zero_node);
2223 if (xz_p)
2225 affine_fn t0 = ova1;
2226 affine_fn t2 = ovb;
2228 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
2229 ovb = affine_fn_plus (ovb, overlaps_b_xz);
2230 affine_fn_free (t0);
2231 affine_fn_free (t2);
2232 *last_conflicts = last_conflicts_xz;
2234 if (yz_p)
2236 affine_fn t0 = ova2;
2237 affine_fn t2 = ovb;
2239 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
2240 ovb = affine_fn_plus (ovb, overlaps_b_yz);
2241 affine_fn_free (t0);
2242 affine_fn_free (t2);
2243 *last_conflicts = last_conflicts_yz;
2245 if (xyz_p)
2247 affine_fn t0 = ova1;
2248 affine_fn t2 = ova2;
2249 affine_fn t4 = ovb;
2251 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
2252 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
2253 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
2254 affine_fn_free (t0);
2255 affine_fn_free (t2);
2256 affine_fn_free (t4);
2257 *last_conflicts = last_conflicts_xyz;
2259 *overlaps_a = conflict_fn (2, ova1, ova2);
2260 *overlaps_b = conflict_fn (1, ovb);
2262 else
2264 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2265 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2266 *last_conflicts = integer_zero_node;
2269 affine_fn_free (overlaps_a_xz);
2270 affine_fn_free (overlaps_b_xz);
2271 affine_fn_free (overlaps_a_yz);
2272 affine_fn_free (overlaps_b_yz);
2273 affine_fn_free (overlaps_a_xyz);
2274 affine_fn_free (overlaps_b_xyz);
2277 /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2279 static void
2280 lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
2281 int size)
2283 memcpy (vec2, vec1, size * sizeof (*vec1));
2286 /* Copy the elements of M x N matrix MAT1 to MAT2. */
2288 static void
2289 lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
2290 int m, int n)
2292 int i;
2294 for (i = 0; i < m; i++)
2295 lambda_vector_copy (mat1[i], mat2[i], n);
2298 /* Store the N x N identity matrix in MAT. */
2300 static void
2301 lambda_matrix_id (lambda_matrix mat, int size)
2303 int i, j;
2305 for (i = 0; i < size; i++)
2306 for (j = 0; j < size; j++)
2307 mat[i][j] = (i == j) ? 1 : 0;
2310 /* Return the first nonzero element of vector VEC1 between START and N.
2311 We must have START <= N. Returns N if VEC1 is the zero vector. */
2313 static int
2314 lambda_vector_first_nz (lambda_vector vec1, int n, int start)
2316 int j = start;
2317 while (j < n && vec1[j] == 0)
2318 j++;
2319 return j;
2322 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2323 R2 = R2 + CONST1 * R1. */
2325 static void
2326 lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2, int const1)
2328 int i;
2330 if (const1 == 0)
2331 return;
2333 for (i = 0; i < n; i++)
2334 mat[r2][i] += const1 * mat[r1][i];
2337 /* Swap rows R1 and R2 in matrix MAT. */
2339 static void
2340 lambda_matrix_row_exchange (lambda_matrix mat, int r1, int r2)
2342 lambda_vector row;
2344 row = mat[r1];
2345 mat[r1] = mat[r2];
2346 mat[r2] = row;
2349 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
2350 and store the result in VEC2. */
2352 static void
2353 lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
2354 int size, int const1)
2356 int i;
2358 if (const1 == 0)
2359 lambda_vector_clear (vec2, size);
2360 else
2361 for (i = 0; i < size; i++)
2362 vec2[i] = const1 * vec1[i];
2365 /* Negate vector VEC1 with length SIZE and store it in VEC2. */
2367 static void
2368 lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
2369 int size)
2371 lambda_vector_mult_const (vec1, vec2, size, -1);
2374 /* Negate row R1 of matrix MAT which has N columns. */
2376 static void
2377 lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
2379 lambda_vector_negate (mat[r1], mat[r1], n);
2382 /* Return true if two vectors are equal. */
2384 static bool
2385 lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
2387 int i;
2388 for (i = 0; i < size; i++)
2389 if (vec1[i] != vec2[i])
2390 return false;
2391 return true;
2394 /* Given an M x N integer matrix A, this function determines an M x
2395 M unimodular matrix U, and an M x N echelon matrix S such that
2396 "U.A = S". This decomposition is also known as "right Hermite".
2398 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2399 Restructuring Compilers" Utpal Banerjee. */
2401 static void
2402 lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
2403 lambda_matrix S, lambda_matrix U)
2405 int i, j, i0 = 0;
2407 lambda_matrix_copy (A, S, m, n);
2408 lambda_matrix_id (U, m);
2410 for (j = 0; j < n; j++)
2412 if (lambda_vector_first_nz (S[j], m, i0) < m)
2414 ++i0;
2415 for (i = m - 1; i >= i0; i--)
2417 while (S[i][j] != 0)
2419 int sigma, factor, a, b;
2421 a = S[i-1][j];
2422 b = S[i][j];
2423 sigma = (a * b < 0) ? -1: 1;
2424 a = abs (a);
2425 b = abs (b);
2426 factor = sigma * (a / b);
2428 lambda_matrix_row_add (S, n, i, i-1, -factor);
2429 lambda_matrix_row_exchange (S, i, i-1);
2431 lambda_matrix_row_add (U, m, i, i-1, -factor);
2432 lambda_matrix_row_exchange (U, i, i-1);
2439 /* Determines the overlapping elements due to accesses CHREC_A and
2440 CHREC_B, that are affine functions. This function cannot handle
2441 symbolic evolution functions, ie. when initial conditions are
2442 parameters, because it uses lambda matrices of integers. */
2444 static void
2445 analyze_subscript_affine_affine (tree chrec_a,
2446 tree chrec_b,
2447 conflict_function **overlaps_a,
2448 conflict_function **overlaps_b,
2449 tree *last_conflicts)
2451 unsigned nb_vars_a, nb_vars_b, dim;
2452 HOST_WIDE_INT init_a, init_b, gamma, gcd_alpha_beta;
2453 lambda_matrix A, U, S;
2454 struct obstack scratch_obstack;
2456 if (eq_evolutions_p (chrec_a, chrec_b))
2458 /* The accessed index overlaps for each iteration in the
2459 loop. */
2460 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2461 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2462 *last_conflicts = chrec_dont_know;
2463 return;
2465 if (dump_file && (dump_flags & TDF_DETAILS))
2466 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2468 /* For determining the initial intersection, we have to solve a
2469 Diophantine equation. This is the most time consuming part.
2471 For answering to the question: "Is there a dependence?" we have
2472 to prove that there exists a solution to the Diophantine
2473 equation, and that the solution is in the iteration domain,
2474 i.e. the solution is positive or zero, and that the solution
2475 happens before the upper bound loop.nb_iterations. Otherwise
2476 there is no dependence. This function outputs a description of
2477 the iterations that hold the intersections. */
2479 nb_vars_a = nb_vars_in_chrec (chrec_a);
2480 nb_vars_b = nb_vars_in_chrec (chrec_b);
2482 gcc_obstack_init (&scratch_obstack);
2484 dim = nb_vars_a + nb_vars_b;
2485 U = lambda_matrix_new (dim, dim, &scratch_obstack);
2486 A = lambda_matrix_new (dim, 1, &scratch_obstack);
2487 S = lambda_matrix_new (dim, 1, &scratch_obstack);
2489 init_a = int_cst_value (initialize_matrix_A (A, chrec_a, 0, 1));
2490 init_b = int_cst_value (initialize_matrix_A (A, chrec_b, nb_vars_a, -1));
2491 gamma = init_b - init_a;
2493 /* Don't do all the hard work of solving the Diophantine equation
2494 when we already know the solution: for example,
2495 | {3, +, 1}_1
2496 | {3, +, 4}_2
2497 | gamma = 3 - 3 = 0.
2498 Then the first overlap occurs during the first iterations:
2499 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2501 if (gamma == 0)
2503 if (nb_vars_a == 1 && nb_vars_b == 1)
2505 HOST_WIDE_INT step_a, step_b;
2506 HOST_WIDE_INT niter, niter_a, niter_b;
2507 affine_fn ova, ovb;
2509 niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
2510 niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
2511 niter = MIN (niter_a, niter_b);
2512 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2513 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2515 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2516 &ova, &ovb,
2517 last_conflicts, 1);
2518 *overlaps_a = conflict_fn (1, ova);
2519 *overlaps_b = conflict_fn (1, ovb);
2522 else if (nb_vars_a == 2 && nb_vars_b == 1)
2523 compute_overlap_steps_for_affine_1_2
2524 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2526 else if (nb_vars_a == 1 && nb_vars_b == 2)
2527 compute_overlap_steps_for_affine_1_2
2528 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2530 else
2532 if (dump_file && (dump_flags & TDF_DETAILS))
2533 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
2534 *overlaps_a = conflict_fn_not_known ();
2535 *overlaps_b = conflict_fn_not_known ();
2536 *last_conflicts = chrec_dont_know;
2538 goto end_analyze_subs_aa;
2541 /* U.A = S */
2542 lambda_matrix_right_hermite (A, dim, 1, S, U);
2544 if (S[0][0] < 0)
2546 S[0][0] *= -1;
2547 lambda_matrix_row_negate (U, dim, 0);
2549 gcd_alpha_beta = S[0][0];
2551 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2552 but that is a quite strange case. Instead of ICEing, answer
2553 don't know. */
2554 if (gcd_alpha_beta == 0)
2556 *overlaps_a = conflict_fn_not_known ();
2557 *overlaps_b = conflict_fn_not_known ();
2558 *last_conflicts = chrec_dont_know;
2559 goto end_analyze_subs_aa;
2562 /* The classic "gcd-test". */
2563 if (!int_divides_p (gcd_alpha_beta, gamma))
2565 /* The "gcd-test" has determined that there is no integer
2566 solution, i.e. there is no dependence. */
2567 *overlaps_a = conflict_fn_no_dependence ();
2568 *overlaps_b = conflict_fn_no_dependence ();
2569 *last_conflicts = integer_zero_node;
2572 /* Both access functions are univariate. This includes SIV and MIV cases. */
2573 else if (nb_vars_a == 1 && nb_vars_b == 1)
2575 /* Both functions should have the same evolution sign. */
2576 if (((A[0][0] > 0 && -A[1][0] > 0)
2577 || (A[0][0] < 0 && -A[1][0] < 0)))
2579 /* The solutions are given by:
2581 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2582 | [u21 u22] [y0]
2584 For a given integer t. Using the following variables,
2586 | i0 = u11 * gamma / gcd_alpha_beta
2587 | j0 = u12 * gamma / gcd_alpha_beta
2588 | i1 = u21
2589 | j1 = u22
2591 the solutions are:
2593 | x0 = i0 + i1 * t,
2594 | y0 = j0 + j1 * t. */
2595 HOST_WIDE_INT i0, j0, i1, j1;
2597 i0 = U[0][0] * gamma / gcd_alpha_beta;
2598 j0 = U[0][1] * gamma / gcd_alpha_beta;
2599 i1 = U[1][0];
2600 j1 = U[1][1];
2602 if ((i1 == 0 && i0 < 0)
2603 || (j1 == 0 && j0 < 0))
2605 /* There is no solution.
2606 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2607 falls in here, but for the moment we don't look at the
2608 upper bound of the iteration domain. */
2609 *overlaps_a = conflict_fn_no_dependence ();
2610 *overlaps_b = conflict_fn_no_dependence ();
2611 *last_conflicts = integer_zero_node;
2612 goto end_analyze_subs_aa;
2615 if (i1 > 0 && j1 > 0)
2617 HOST_WIDE_INT niter_a
2618 = max_stmt_executions_int (get_chrec_loop (chrec_a));
2619 HOST_WIDE_INT niter_b
2620 = max_stmt_executions_int (get_chrec_loop (chrec_b));
2621 HOST_WIDE_INT niter = MIN (niter_a, niter_b);
2623 /* (X0, Y0) is a solution of the Diophantine equation:
2624 "chrec_a (X0) = chrec_b (Y0)". */
2625 HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
2626 CEIL (-j0, j1));
2627 HOST_WIDE_INT x0 = i1 * tau1 + i0;
2628 HOST_WIDE_INT y0 = j1 * tau1 + j0;
2630 /* (X1, Y1) is the smallest positive solution of the eq
2631 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2632 first conflict occurs. */
2633 HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
2634 HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
2635 HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
2637 if (niter > 0)
2639 HOST_WIDE_INT tau2 = MIN (FLOOR_DIV (niter - i0, i1),
2640 FLOOR_DIV (niter - j0, j1));
2641 HOST_WIDE_INT last_conflict = tau2 - (x1 - i0)/i1;
2643 /* If the overlap occurs outside of the bounds of the
2644 loop, there is no dependence. */
2645 if (x1 >= niter || y1 >= niter)
2647 *overlaps_a = conflict_fn_no_dependence ();
2648 *overlaps_b = conflict_fn_no_dependence ();
2649 *last_conflicts = integer_zero_node;
2650 goto end_analyze_subs_aa;
2652 else
2653 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2655 else
2656 *last_conflicts = chrec_dont_know;
2658 *overlaps_a
2659 = conflict_fn (1,
2660 affine_fn_univar (build_int_cst (NULL_TREE, x1),
2662 build_int_cst (NULL_TREE, i1)));
2663 *overlaps_b
2664 = conflict_fn (1,
2665 affine_fn_univar (build_int_cst (NULL_TREE, y1),
2667 build_int_cst (NULL_TREE, j1)));
2669 else
2671 /* FIXME: For the moment, the upper bound of the
2672 iteration domain for i and j is not checked. */
2673 if (dump_file && (dump_flags & TDF_DETAILS))
2674 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2675 *overlaps_a = conflict_fn_not_known ();
2676 *overlaps_b = conflict_fn_not_known ();
2677 *last_conflicts = chrec_dont_know;
2680 else
2682 if (dump_file && (dump_flags & TDF_DETAILS))
2683 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2684 *overlaps_a = conflict_fn_not_known ();
2685 *overlaps_b = conflict_fn_not_known ();
2686 *last_conflicts = chrec_dont_know;
2689 else
2691 if (dump_file && (dump_flags & TDF_DETAILS))
2692 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2693 *overlaps_a = conflict_fn_not_known ();
2694 *overlaps_b = conflict_fn_not_known ();
2695 *last_conflicts = chrec_dont_know;
2698 end_analyze_subs_aa:
2699 obstack_free (&scratch_obstack, NULL);
2700 if (dump_file && (dump_flags & TDF_DETAILS))
2702 fprintf (dump_file, " (overlaps_a = ");
2703 dump_conflict_function (dump_file, *overlaps_a);
2704 fprintf (dump_file, ")\n (overlaps_b = ");
2705 dump_conflict_function (dump_file, *overlaps_b);
2706 fprintf (dump_file, "))\n");
2710 /* Returns true when analyze_subscript_affine_affine can be used for
2711 determining the dependence relation between chrec_a and chrec_b,
2712 that contain symbols. This function modifies chrec_a and chrec_b
2713 such that the analysis result is the same, and such that they don't
2714 contain symbols, and then can safely be passed to the analyzer.
2716 Example: The analysis of the following tuples of evolutions produce
2717 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2718 vs. {0, +, 1}_1
2720 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2721 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2724 static bool
2725 can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
2727 tree diff, type, left_a, left_b, right_b;
2729 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
2730 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
2731 /* FIXME: For the moment not handled. Might be refined later. */
2732 return false;
2734 type = chrec_type (*chrec_a);
2735 left_a = CHREC_LEFT (*chrec_a);
2736 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
2737 diff = chrec_fold_minus (type, left_a, left_b);
2739 if (!evolution_function_is_constant_p (diff))
2740 return false;
2742 if (dump_file && (dump_flags & TDF_DETAILS))
2743 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
2745 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
2746 diff, CHREC_RIGHT (*chrec_a));
2747 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
2748 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
2749 build_int_cst (type, 0),
2750 right_b);
2751 return true;
2754 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2755 *OVERLAPS_B are initialized to the functions that describe the
2756 relation between the elements accessed twice by CHREC_A and
2757 CHREC_B. For k >= 0, the following property is verified:
2759 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2761 static void
2762 analyze_siv_subscript (tree chrec_a,
2763 tree chrec_b,
2764 conflict_function **overlaps_a,
2765 conflict_function **overlaps_b,
2766 tree *last_conflicts,
2767 int loop_nest_num)
2769 dependence_stats.num_siv++;
2771 if (dump_file && (dump_flags & TDF_DETAILS))
2772 fprintf (dump_file, "(analyze_siv_subscript \n");
2774 if (evolution_function_is_constant_p (chrec_a)
2775 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2776 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
2777 overlaps_a, overlaps_b, last_conflicts);
2779 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2780 && evolution_function_is_constant_p (chrec_b))
2781 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
2782 overlaps_b, overlaps_a, last_conflicts);
2784 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2785 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2787 if (!chrec_contains_symbols (chrec_a)
2788 && !chrec_contains_symbols (chrec_b))
2790 analyze_subscript_affine_affine (chrec_a, chrec_b,
2791 overlaps_a, overlaps_b,
2792 last_conflicts);
2794 if (CF_NOT_KNOWN_P (*overlaps_a)
2795 || CF_NOT_KNOWN_P (*overlaps_b))
2796 dependence_stats.num_siv_unimplemented++;
2797 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2798 || CF_NO_DEPENDENCE_P (*overlaps_b))
2799 dependence_stats.num_siv_independent++;
2800 else
2801 dependence_stats.num_siv_dependent++;
2803 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
2804 &chrec_b))
2806 analyze_subscript_affine_affine (chrec_a, chrec_b,
2807 overlaps_a, overlaps_b,
2808 last_conflicts);
2810 if (CF_NOT_KNOWN_P (*overlaps_a)
2811 || CF_NOT_KNOWN_P (*overlaps_b))
2812 dependence_stats.num_siv_unimplemented++;
2813 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2814 || CF_NO_DEPENDENCE_P (*overlaps_b))
2815 dependence_stats.num_siv_independent++;
2816 else
2817 dependence_stats.num_siv_dependent++;
2819 else
2820 goto siv_subscript_dontknow;
2823 else
2825 siv_subscript_dontknow:;
2826 if (dump_file && (dump_flags & TDF_DETAILS))
2827 fprintf (dump_file, " siv test failed: unimplemented");
2828 *overlaps_a = conflict_fn_not_known ();
2829 *overlaps_b = conflict_fn_not_known ();
2830 *last_conflicts = chrec_dont_know;
2831 dependence_stats.num_siv_unimplemented++;
2834 if (dump_file && (dump_flags & TDF_DETAILS))
2835 fprintf (dump_file, ")\n");
2838 /* Returns false if we can prove that the greatest common divisor of the steps
2839 of CHREC does not divide CST, false otherwise. */
2841 static bool
2842 gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
2844 HOST_WIDE_INT cd = 0, val;
2845 tree step;
2847 if (!tree_fits_shwi_p (cst))
2848 return true;
2849 val = tree_to_shwi (cst);
2851 while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
2853 step = CHREC_RIGHT (chrec);
2854 if (!tree_fits_shwi_p (step))
2855 return true;
2856 cd = gcd (cd, tree_to_shwi (step));
2857 chrec = CHREC_LEFT (chrec);
2860 return val % cd == 0;
2863 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
2864 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2865 functions that describe the relation between the elements accessed
2866 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2867 is verified:
2869 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2871 static void
2872 analyze_miv_subscript (tree chrec_a,
2873 tree chrec_b,
2874 conflict_function **overlaps_a,
2875 conflict_function **overlaps_b,
2876 tree *last_conflicts,
2877 struct loop *loop_nest)
2879 tree type, difference;
2881 dependence_stats.num_miv++;
2882 if (dump_file && (dump_flags & TDF_DETAILS))
2883 fprintf (dump_file, "(analyze_miv_subscript \n");
2885 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
2886 chrec_a = chrec_convert (type, chrec_a, NULL);
2887 chrec_b = chrec_convert (type, chrec_b, NULL);
2888 difference = chrec_fold_minus (type, chrec_a, chrec_b);
2890 if (eq_evolutions_p (chrec_a, chrec_b))
2892 /* Access functions are the same: all the elements are accessed
2893 in the same order. */
2894 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2895 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2896 *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
2897 dependence_stats.num_miv_dependent++;
2900 else if (evolution_function_is_constant_p (difference)
2901 /* For the moment, the following is verified:
2902 evolution_function_is_affine_multivariate_p (chrec_a,
2903 loop_nest->num) */
2904 && !gcd_of_steps_may_divide_p (chrec_a, difference))
2906 /* testsuite/.../ssa-chrec-33.c
2907 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2909 The difference is 1, and all the evolution steps are multiples
2910 of 2, consequently there are no overlapping elements. */
2911 *overlaps_a = conflict_fn_no_dependence ();
2912 *overlaps_b = conflict_fn_no_dependence ();
2913 *last_conflicts = integer_zero_node;
2914 dependence_stats.num_miv_independent++;
2917 else if (evolution_function_is_affine_multivariate_p (chrec_a, loop_nest->num)
2918 && !chrec_contains_symbols (chrec_a)
2919 && evolution_function_is_affine_multivariate_p (chrec_b, loop_nest->num)
2920 && !chrec_contains_symbols (chrec_b))
2922 /* testsuite/.../ssa-chrec-35.c
2923 {0, +, 1}_2 vs. {0, +, 1}_3
2924 the overlapping elements are respectively located at iterations:
2925 {0, +, 1}_x and {0, +, 1}_x,
2926 in other words, we have the equality:
2927 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2929 Other examples:
2930 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2931 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2933 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2934 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2936 analyze_subscript_affine_affine (chrec_a, chrec_b,
2937 overlaps_a, overlaps_b, last_conflicts);
2939 if (CF_NOT_KNOWN_P (*overlaps_a)
2940 || CF_NOT_KNOWN_P (*overlaps_b))
2941 dependence_stats.num_miv_unimplemented++;
2942 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2943 || CF_NO_DEPENDENCE_P (*overlaps_b))
2944 dependence_stats.num_miv_independent++;
2945 else
2946 dependence_stats.num_miv_dependent++;
2949 else
2951 /* When the analysis is too difficult, answer "don't know". */
2952 if (dump_file && (dump_flags & TDF_DETAILS))
2953 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
2955 *overlaps_a = conflict_fn_not_known ();
2956 *overlaps_b = conflict_fn_not_known ();
2957 *last_conflicts = chrec_dont_know;
2958 dependence_stats.num_miv_unimplemented++;
2961 if (dump_file && (dump_flags & TDF_DETAILS))
2962 fprintf (dump_file, ")\n");
2965 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
2966 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
2967 OVERLAP_ITERATIONS_B are initialized with two functions that
2968 describe the iterations that contain conflicting elements.
2970 Remark: For an integer k >= 0, the following equality is true:
2972 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
2975 static void
2976 analyze_overlapping_iterations (tree chrec_a,
2977 tree chrec_b,
2978 conflict_function **overlap_iterations_a,
2979 conflict_function **overlap_iterations_b,
2980 tree *last_conflicts, struct loop *loop_nest)
2982 unsigned int lnn = loop_nest->num;
2984 dependence_stats.num_subscript_tests++;
2986 if (dump_file && (dump_flags & TDF_DETAILS))
2988 fprintf (dump_file, "(analyze_overlapping_iterations \n");
2989 fprintf (dump_file, " (chrec_a = ");
2990 print_generic_expr (dump_file, chrec_a, 0);
2991 fprintf (dump_file, ")\n (chrec_b = ");
2992 print_generic_expr (dump_file, chrec_b, 0);
2993 fprintf (dump_file, ")\n");
2996 if (chrec_a == NULL_TREE
2997 || chrec_b == NULL_TREE
2998 || chrec_contains_undetermined (chrec_a)
2999 || chrec_contains_undetermined (chrec_b))
3001 dependence_stats.num_subscript_undetermined++;
3003 *overlap_iterations_a = conflict_fn_not_known ();
3004 *overlap_iterations_b = conflict_fn_not_known ();
3007 /* If they are the same chrec, and are affine, they overlap
3008 on every iteration. */
3009 else if (eq_evolutions_p (chrec_a, chrec_b)
3010 && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3011 || operand_equal_p (chrec_a, chrec_b, 0)))
3013 dependence_stats.num_same_subscript_function++;
3014 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3015 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3016 *last_conflicts = chrec_dont_know;
3019 /* If they aren't the same, and aren't affine, we can't do anything
3020 yet. */
3021 else if ((chrec_contains_symbols (chrec_a)
3022 || chrec_contains_symbols (chrec_b))
3023 && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3024 || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
3026 dependence_stats.num_subscript_undetermined++;
3027 *overlap_iterations_a = conflict_fn_not_known ();
3028 *overlap_iterations_b = conflict_fn_not_known ();
3031 else if (ziv_subscript_p (chrec_a, chrec_b))
3032 analyze_ziv_subscript (chrec_a, chrec_b,
3033 overlap_iterations_a, overlap_iterations_b,
3034 last_conflicts);
3036 else if (siv_subscript_p (chrec_a, chrec_b))
3037 analyze_siv_subscript (chrec_a, chrec_b,
3038 overlap_iterations_a, overlap_iterations_b,
3039 last_conflicts, lnn);
3041 else
3042 analyze_miv_subscript (chrec_a, chrec_b,
3043 overlap_iterations_a, overlap_iterations_b,
3044 last_conflicts, loop_nest);
3046 if (dump_file && (dump_flags & TDF_DETAILS))
3048 fprintf (dump_file, " (overlap_iterations_a = ");
3049 dump_conflict_function (dump_file, *overlap_iterations_a);
3050 fprintf (dump_file, ")\n (overlap_iterations_b = ");
3051 dump_conflict_function (dump_file, *overlap_iterations_b);
3052 fprintf (dump_file, "))\n");
3056 /* Helper function for uniquely inserting distance vectors. */
3058 static void
3059 save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
3061 unsigned i;
3062 lambda_vector v;
3064 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, v)
3065 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
3066 return;
3068 DDR_DIST_VECTS (ddr).safe_push (dist_v);
3071 /* Helper function for uniquely inserting direction vectors. */
3073 static void
3074 save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
3076 unsigned i;
3077 lambda_vector v;
3079 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), i, v)
3080 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
3081 return;
3083 DDR_DIR_VECTS (ddr).safe_push (dir_v);
3086 /* Add a distance of 1 on all the loops outer than INDEX. If we
3087 haven't yet determined a distance for this outer loop, push a new
3088 distance vector composed of the previous distance, and a distance
3089 of 1 for this outer loop. Example:
3091 | loop_1
3092 | loop_2
3093 | A[10]
3094 | endloop_2
3095 | endloop_1
3097 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3098 save (0, 1), then we have to save (1, 0). */
3100 static void
3101 add_outer_distances (struct data_dependence_relation *ddr,
3102 lambda_vector dist_v, int index)
3104 /* For each outer loop where init_v is not set, the accesses are
3105 in dependence of distance 1 in the loop. */
3106 while (--index >= 0)
3108 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3109 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3110 save_v[index] = 1;
3111 save_dist_v (ddr, save_v);
3115 /* Return false when fail to represent the data dependence as a
3116 distance vector. INIT_B is set to true when a component has been
3117 added to the distance vector DIST_V. INDEX_CARRY is then set to
3118 the index in DIST_V that carries the dependence. */
3120 static bool
3121 build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
3122 struct data_reference *ddr_a,
3123 struct data_reference *ddr_b,
3124 lambda_vector dist_v, bool *init_b,
3125 int *index_carry)
3127 unsigned i;
3128 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3130 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3132 tree access_fn_a, access_fn_b;
3133 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3135 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3137 non_affine_dependence_relation (ddr);
3138 return false;
3141 access_fn_a = DR_ACCESS_FN (ddr_a, i);
3142 access_fn_b = DR_ACCESS_FN (ddr_b, i);
3144 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3145 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3147 int dist, index;
3148 int var_a = CHREC_VARIABLE (access_fn_a);
3149 int var_b = CHREC_VARIABLE (access_fn_b);
3151 if (var_a != var_b
3152 || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3154 non_affine_dependence_relation (ddr);
3155 return false;
3158 dist = int_cst_value (SUB_DISTANCE (subscript));
3159 index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
3160 *index_carry = MIN (index, *index_carry);
3162 /* This is the subscript coupling test. If we have already
3163 recorded a distance for this loop (a distance coming from
3164 another subscript), it should be the same. For example,
3165 in the following code, there is no dependence:
3167 | loop i = 0, N, 1
3168 | T[i+1][i] = ...
3169 | ... = T[i][i]
3170 | endloop
3172 if (init_v[index] != 0 && dist_v[index] != dist)
3174 finalize_ddr_dependent (ddr, chrec_known);
3175 return false;
3178 dist_v[index] = dist;
3179 init_v[index] = 1;
3180 *init_b = true;
3182 else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
3184 /* This can be for example an affine vs. constant dependence
3185 (T[i] vs. T[3]) that is not an affine dependence and is
3186 not representable as a distance vector. */
3187 non_affine_dependence_relation (ddr);
3188 return false;
3192 return true;
3195 /* Return true when the DDR contains only constant access functions. */
3197 static bool
3198 constant_access_functions (const struct data_dependence_relation *ddr)
3200 unsigned i;
3202 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3203 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr), i))
3204 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr), i)))
3205 return false;
3207 return true;
3210 /* Helper function for the case where DDR_A and DDR_B are the same
3211 multivariate access function with a constant step. For an example
3212 see pr34635-1.c. */
3214 static void
3215 add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
3217 int x_1, x_2;
3218 tree c_1 = CHREC_LEFT (c_2);
3219 tree c_0 = CHREC_LEFT (c_1);
3220 lambda_vector dist_v;
3221 int v1, v2, cd;
3223 /* Polynomials with more than 2 variables are not handled yet. When
3224 the evolution steps are parameters, it is not possible to
3225 represent the dependence using classical distance vectors. */
3226 if (TREE_CODE (c_0) != INTEGER_CST
3227 || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
3228 || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
3230 DDR_AFFINE_P (ddr) = false;
3231 return;
3234 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
3235 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
3237 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3238 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3239 v1 = int_cst_value (CHREC_RIGHT (c_1));
3240 v2 = int_cst_value (CHREC_RIGHT (c_2));
3241 cd = gcd (v1, v2);
3242 v1 /= cd;
3243 v2 /= cd;
3245 if (v2 < 0)
3247 v2 = -v2;
3248 v1 = -v1;
3251 dist_v[x_1] = v2;
3252 dist_v[x_2] = -v1;
3253 save_dist_v (ddr, dist_v);
3255 add_outer_distances (ddr, dist_v, x_1);
3258 /* Helper function for the case where DDR_A and DDR_B are the same
3259 access functions. */
3261 static void
3262 add_other_self_distances (struct data_dependence_relation *ddr)
3264 lambda_vector dist_v;
3265 unsigned i;
3266 int index_carry = DDR_NB_LOOPS (ddr);
3268 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3270 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
3272 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
3274 if (!evolution_function_is_univariate_p (access_fun))
3276 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
3278 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3279 return;
3282 access_fun = DR_ACCESS_FN (DDR_A (ddr), 0);
3284 if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
3285 add_multivariate_self_dist (ddr, access_fun);
3286 else
3287 /* The evolution step is not constant: it varies in
3288 the outer loop, so this cannot be represented by a
3289 distance vector. For example in pr34635.c the
3290 evolution is {0, +, {0, +, 4}_1}_2. */
3291 DDR_AFFINE_P (ddr) = false;
3293 return;
3296 index_carry = MIN (index_carry,
3297 index_in_loop_nest (CHREC_VARIABLE (access_fun),
3298 DDR_LOOP_NEST (ddr)));
3302 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3303 add_outer_distances (ddr, dist_v, index_carry);
3306 static void
3307 insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
3309 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3311 dist_v[DDR_INNER_LOOP (ddr)] = 1;
3312 save_dist_v (ddr, dist_v);
3315 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
3316 is the case for example when access functions are the same and
3317 equal to a constant, as in:
3319 | loop_1
3320 | A[3] = ...
3321 | ... = A[3]
3322 | endloop_1
3324 in which case the distance vectors are (0) and (1). */
3326 static void
3327 add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
3329 unsigned i, j;
3331 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3333 subscript_p sub = DDR_SUBSCRIPT (ddr, i);
3334 conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
3335 conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
3337 for (j = 0; j < ca->n; j++)
3338 if (affine_function_zero_p (ca->fns[j]))
3340 insert_innermost_unit_dist_vector (ddr);
3341 return;
3344 for (j = 0; j < cb->n; j++)
3345 if (affine_function_zero_p (cb->fns[j]))
3347 insert_innermost_unit_dist_vector (ddr);
3348 return;
3353 /* Compute the classic per loop distance vector. DDR is the data
3354 dependence relation to build a vector from. Return false when fail
3355 to represent the data dependence as a distance vector. */
3357 static bool
3358 build_classic_dist_vector (struct data_dependence_relation *ddr,
3359 struct loop *loop_nest)
3361 bool init_b = false;
3362 int index_carry = DDR_NB_LOOPS (ddr);
3363 lambda_vector dist_v;
3365 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3366 return false;
3368 if (same_access_functions (ddr))
3370 /* Save the 0 vector. */
3371 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3372 save_dist_v (ddr, dist_v);
3374 if (constant_access_functions (ddr))
3375 add_distance_for_zero_overlaps (ddr);
3377 if (DDR_NB_LOOPS (ddr) > 1)
3378 add_other_self_distances (ddr);
3380 return true;
3383 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3384 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3385 dist_v, &init_b, &index_carry))
3386 return false;
3388 /* Save the distance vector if we initialized one. */
3389 if (init_b)
3391 /* Verify a basic constraint: classic distance vectors should
3392 always be lexicographically positive.
3394 Data references are collected in the order of execution of
3395 the program, thus for the following loop
3397 | for (i = 1; i < 100; i++)
3398 | for (j = 1; j < 100; j++)
3400 | t = T[j+1][i-1]; // A
3401 | T[j][i] = t + 2; // B
3404 references are collected following the direction of the wind:
3405 A then B. The data dependence tests are performed also
3406 following this order, such that we're looking at the distance
3407 separating the elements accessed by A from the elements later
3408 accessed by B. But in this example, the distance returned by
3409 test_dep (A, B) is lexicographically negative (-1, 1), that
3410 means that the access A occurs later than B with respect to
3411 the outer loop, ie. we're actually looking upwind. In this
3412 case we solve test_dep (B, A) looking downwind to the
3413 lexicographically positive solution, that returns the
3414 distance vector (1, -1). */
3415 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3417 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3418 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3419 loop_nest))
3420 return false;
3421 compute_subscript_distance (ddr);
3422 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3423 save_v, &init_b, &index_carry))
3424 return false;
3425 save_dist_v (ddr, save_v);
3426 DDR_REVERSED_P (ddr) = true;
3428 /* In this case there is a dependence forward for all the
3429 outer loops:
3431 | for (k = 1; k < 100; k++)
3432 | for (i = 1; i < 100; i++)
3433 | for (j = 1; j < 100; j++)
3435 | t = T[j+1][i-1]; // A
3436 | T[j][i] = t + 2; // B
3439 the vectors are:
3440 (0, 1, -1)
3441 (1, 1, -1)
3442 (1, -1, 1)
3444 if (DDR_NB_LOOPS (ddr) > 1)
3446 add_outer_distances (ddr, save_v, index_carry);
3447 add_outer_distances (ddr, dist_v, index_carry);
3450 else
3452 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3453 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3455 if (DDR_NB_LOOPS (ddr) > 1)
3457 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3459 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr),
3460 DDR_A (ddr), loop_nest))
3461 return false;
3462 compute_subscript_distance (ddr);
3463 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3464 opposite_v, &init_b,
3465 &index_carry))
3466 return false;
3468 save_dist_v (ddr, save_v);
3469 add_outer_distances (ddr, dist_v, index_carry);
3470 add_outer_distances (ddr, opposite_v, index_carry);
3472 else
3473 save_dist_v (ddr, save_v);
3476 else
3478 /* There is a distance of 1 on all the outer loops: Example:
3479 there is a dependence of distance 1 on loop_1 for the array A.
3481 | loop_1
3482 | A[5] = ...
3483 | endloop
3485 add_outer_distances (ddr, dist_v,
3486 lambda_vector_first_nz (dist_v,
3487 DDR_NB_LOOPS (ddr), 0));
3490 if (dump_file && (dump_flags & TDF_DETAILS))
3492 unsigned i;
3494 fprintf (dump_file, "(build_classic_dist_vector\n");
3495 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3497 fprintf (dump_file, " dist_vector = (");
3498 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3499 DDR_NB_LOOPS (ddr));
3500 fprintf (dump_file, " )\n");
3502 fprintf (dump_file, ")\n");
3505 return true;
3508 /* Return the direction for a given distance.
3509 FIXME: Computing dir this way is suboptimal, since dir can catch
3510 cases that dist is unable to represent. */
3512 static inline enum data_dependence_direction
3513 dir_from_dist (int dist)
3515 if (dist > 0)
3516 return dir_positive;
3517 else if (dist < 0)
3518 return dir_negative;
3519 else
3520 return dir_equal;
3523 /* Compute the classic per loop direction vector. DDR is the data
3524 dependence relation to build a vector from. */
3526 static void
3527 build_classic_dir_vector (struct data_dependence_relation *ddr)
3529 unsigned i, j;
3530 lambda_vector dist_v;
3532 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
3534 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3536 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3537 dir_v[j] = dir_from_dist (dist_v[j]);
3539 save_dir_v (ddr, dir_v);
3543 /* Helper function. Returns true when there is a dependence between
3544 data references DRA and DRB. */
3546 static bool
3547 subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
3548 struct data_reference *dra,
3549 struct data_reference *drb,
3550 struct loop *loop_nest)
3552 unsigned int i;
3553 tree last_conflicts;
3554 struct subscript *subscript;
3555 tree res = NULL_TREE;
3557 for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
3559 conflict_function *overlaps_a, *overlaps_b;
3561 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3562 DR_ACCESS_FN (drb, i),
3563 &overlaps_a, &overlaps_b,
3564 &last_conflicts, loop_nest);
3566 if (SUB_CONFLICTS_IN_A (subscript))
3567 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
3568 if (SUB_CONFLICTS_IN_B (subscript))
3569 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
3571 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3572 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3573 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3575 /* If there is any undetermined conflict function we have to
3576 give a conservative answer in case we cannot prove that
3577 no dependence exists when analyzing another subscript. */
3578 if (CF_NOT_KNOWN_P (overlaps_a)
3579 || CF_NOT_KNOWN_P (overlaps_b))
3581 res = chrec_dont_know;
3582 continue;
3585 /* When there is a subscript with no dependence we can stop. */
3586 else if (CF_NO_DEPENDENCE_P (overlaps_a)
3587 || CF_NO_DEPENDENCE_P (overlaps_b))
3589 res = chrec_known;
3590 break;
3594 if (res == NULL_TREE)
3595 return true;
3597 if (res == chrec_known)
3598 dependence_stats.num_dependence_independent++;
3599 else
3600 dependence_stats.num_dependence_undetermined++;
3601 finalize_ddr_dependent (ddr, res);
3602 return false;
3605 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
3607 static void
3608 subscript_dependence_tester (struct data_dependence_relation *ddr,
3609 struct loop *loop_nest)
3611 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr), loop_nest))
3612 dependence_stats.num_dependence_dependent++;
3614 compute_subscript_distance (ddr);
3615 if (build_classic_dist_vector (ddr, loop_nest))
3616 build_classic_dir_vector (ddr);
3619 /* Returns true when all the access functions of A are affine or
3620 constant with respect to LOOP_NEST. */
3622 static bool
3623 access_functions_are_affine_or_constant_p (const struct data_reference *a,
3624 const struct loop *loop_nest)
3626 unsigned int i;
3627 vec<tree> fns = DR_ACCESS_FNS (a);
3628 tree t;
3630 FOR_EACH_VEC_ELT (fns, i, t)
3631 if (!evolution_function_is_invariant_p (t, loop_nest->num)
3632 && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
3633 return false;
3635 return true;
3638 /* Initializes an equation for an OMEGA problem using the information
3639 contained in the ACCESS_FUN. Returns true when the operation
3640 succeeded.
3642 PB is the omega constraint system.
3643 EQ is the number of the equation to be initialized.
3644 OFFSET is used for shifting the variables names in the constraints:
3645 a constrain is composed of 2 * the number of variables surrounding
3646 dependence accesses. OFFSET is set either to 0 for the first n variables,
3647 then it is set to n.
3648 ACCESS_FUN is expected to be an affine chrec. */
3650 static bool
3651 init_omega_eq_with_af (omega_pb pb, unsigned eq,
3652 unsigned int offset, tree access_fun,
3653 struct data_dependence_relation *ddr)
3655 switch (TREE_CODE (access_fun))
3657 case POLYNOMIAL_CHREC:
3659 tree left = CHREC_LEFT (access_fun);
3660 tree right = CHREC_RIGHT (access_fun);
3661 int var = CHREC_VARIABLE (access_fun);
3662 unsigned var_idx;
3664 if (TREE_CODE (right) != INTEGER_CST)
3665 return false;
3667 var_idx = index_in_loop_nest (var, DDR_LOOP_NEST (ddr));
3668 pb->eqs[eq].coef[offset + var_idx + 1] = int_cst_value (right);
3670 /* Compute the innermost loop index. */
3671 DDR_INNER_LOOP (ddr) = MAX (DDR_INNER_LOOP (ddr), var_idx);
3673 if (offset == 0)
3674 pb->eqs[eq].coef[var_idx + DDR_NB_LOOPS (ddr) + 1]
3675 += int_cst_value (right);
3677 switch (TREE_CODE (left))
3679 case POLYNOMIAL_CHREC:
3680 return init_omega_eq_with_af (pb, eq, offset, left, ddr);
3682 case INTEGER_CST:
3683 pb->eqs[eq].coef[0] += int_cst_value (left);
3684 return true;
3686 default:
3687 return false;
3691 case INTEGER_CST:
3692 pb->eqs[eq].coef[0] += int_cst_value (access_fun);
3693 return true;
3695 default:
3696 return false;
3700 /* As explained in the comments preceding init_omega_for_ddr, we have
3701 to set up a system for each loop level, setting outer loops
3702 variation to zero, and current loop variation to positive or zero.
3703 Save each lexico positive distance vector. */
3705 static void
3706 omega_extract_distance_vectors (omega_pb pb,
3707 struct data_dependence_relation *ddr)
3709 int eq, geq;
3710 unsigned i, j;
3711 struct loop *loopi, *loopj;
3712 enum omega_result res;
3714 /* Set a new problem for each loop in the nest. The basis is the
3715 problem that we have initialized until now. On top of this we
3716 add new constraints. */
3717 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3718 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3720 int dist = 0;
3721 omega_pb copy = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr),
3722 DDR_NB_LOOPS (ddr));
3724 omega_copy_problem (copy, pb);
3726 /* For all the outer loops "loop_j", add "dj = 0". */
3727 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3729 eq = omega_add_zero_eq (copy, omega_black);
3730 copy->eqs[eq].coef[j + 1] = 1;
3733 /* For "loop_i", add "0 <= di". */
3734 geq = omega_add_zero_geq (copy, omega_black);
3735 copy->geqs[geq].coef[i + 1] = 1;
3737 /* Reduce the constraint system, and test that the current
3738 problem is feasible. */
3739 res = omega_simplify_problem (copy);
3740 if (res == omega_false
3741 || res == omega_unknown
3742 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3743 goto next_problem;
3745 for (eq = 0; eq < copy->num_subs; eq++)
3746 if (copy->subs[eq].key == (int) i + 1)
3748 dist = copy->subs[eq].coef[0];
3749 goto found_dist;
3752 if (dist == 0)
3754 /* Reinitialize problem... */
3755 omega_copy_problem (copy, pb);
3756 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3758 eq = omega_add_zero_eq (copy, omega_black);
3759 copy->eqs[eq].coef[j + 1] = 1;
3762 /* ..., but this time "di = 1". */
3763 eq = omega_add_zero_eq (copy, omega_black);
3764 copy->eqs[eq].coef[i + 1] = 1;
3765 copy->eqs[eq].coef[0] = -1;
3767 res = omega_simplify_problem (copy);
3768 if (res == omega_false
3769 || res == omega_unknown
3770 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3771 goto next_problem;
3773 for (eq = 0; eq < copy->num_subs; eq++)
3774 if (copy->subs[eq].key == (int) i + 1)
3776 dist = copy->subs[eq].coef[0];
3777 goto found_dist;
3781 found_dist:;
3782 /* Save the lexicographically positive distance vector. */
3783 if (dist >= 0)
3785 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3786 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3788 dist_v[i] = dist;
3790 for (eq = 0; eq < copy->num_subs; eq++)
3791 if (copy->subs[eq].key > 0)
3793 dist = copy->subs[eq].coef[0];
3794 dist_v[copy->subs[eq].key - 1] = dist;
3797 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3798 dir_v[j] = dir_from_dist (dist_v[j]);
3800 save_dist_v (ddr, dist_v);
3801 save_dir_v (ddr, dir_v);
3804 next_problem:;
3805 omega_free_problem (copy);
3809 /* This is called for each subscript of a tuple of data references:
3810 insert an equality for representing the conflicts. */
3812 static bool
3813 omega_setup_subscript (tree access_fun_a, tree access_fun_b,
3814 struct data_dependence_relation *ddr,
3815 omega_pb pb, bool *maybe_dependent)
3817 int eq;
3818 tree type = signed_type_for_types (TREE_TYPE (access_fun_a),
3819 TREE_TYPE (access_fun_b));
3820 tree fun_a = chrec_convert (type, access_fun_a, NULL);
3821 tree fun_b = chrec_convert (type, access_fun_b, NULL);
3822 tree difference = chrec_fold_minus (type, fun_a, fun_b);
3823 tree minus_one;
3825 /* When the fun_a - fun_b is not constant, the dependence is not
3826 captured by the classic distance vector representation. */
3827 if (TREE_CODE (difference) != INTEGER_CST)
3828 return false;
3830 /* ZIV test. */
3831 if (ziv_subscript_p (fun_a, fun_b) && !integer_zerop (difference))
3833 /* There is no dependence. */
3834 *maybe_dependent = false;
3835 return true;
3838 minus_one = build_int_cst (type, -1);
3839 fun_b = chrec_fold_multiply (type, fun_b, minus_one);
3841 eq = omega_add_zero_eq (pb, omega_black);
3842 if (!init_omega_eq_with_af (pb, eq, DDR_NB_LOOPS (ddr), fun_a, ddr)
3843 || !init_omega_eq_with_af (pb, eq, 0, fun_b, ddr))
3844 /* There is probably a dependence, but the system of
3845 constraints cannot be built: answer "don't know". */
3846 return false;
3848 /* GCD test. */
3849 if (DDR_NB_LOOPS (ddr) != 0 && pb->eqs[eq].coef[0]
3850 && !int_divides_p (lambda_vector_gcd
3851 ((lambda_vector) &(pb->eqs[eq].coef[1]),
3852 2 * DDR_NB_LOOPS (ddr)),
3853 pb->eqs[eq].coef[0]))
3855 /* There is no dependence. */
3856 *maybe_dependent = false;
3857 return true;
3860 return true;
3863 /* Helper function, same as init_omega_for_ddr but specialized for
3864 data references A and B. */
3866 static bool
3867 init_omega_for_ddr_1 (struct data_reference *dra, struct data_reference *drb,
3868 struct data_dependence_relation *ddr,
3869 omega_pb pb, bool *maybe_dependent)
3871 unsigned i;
3872 int ineq;
3873 struct loop *loopi;
3874 unsigned nb_loops = DDR_NB_LOOPS (ddr);
3876 /* Insert an equality per subscript. */
3877 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3879 if (!omega_setup_subscript (DR_ACCESS_FN (dra, i), DR_ACCESS_FN (drb, i),
3880 ddr, pb, maybe_dependent))
3881 return false;
3882 else if (*maybe_dependent == false)
3884 /* There is no dependence. */
3885 DDR_ARE_DEPENDENT (ddr) = chrec_known;
3886 return true;
3890 /* Insert inequalities: constraints corresponding to the iteration
3891 domain, i.e. the loops surrounding the references "loop_x" and
3892 the distance variables "dx". The layout of the OMEGA
3893 representation is as follows:
3894 - coef[0] is the constant
3895 - coef[1..nb_loops] are the protected variables that will not be
3896 removed by the solver: the "dx"
3897 - coef[nb_loops + 1, 2*nb_loops] are the loop variables: "loop_x".
3899 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3900 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3902 HOST_WIDE_INT nbi = max_stmt_executions_int (loopi);
3904 /* 0 <= loop_x */
3905 ineq = omega_add_zero_geq (pb, omega_black);
3906 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3908 /* 0 <= loop_x + dx */
3909 ineq = omega_add_zero_geq (pb, omega_black);
3910 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3911 pb->geqs[ineq].coef[i + 1] = 1;
3913 if (nbi != -1)
3915 /* loop_x <= nb_iters */
3916 ineq = omega_add_zero_geq (pb, omega_black);
3917 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3918 pb->geqs[ineq].coef[0] = nbi;
3920 /* loop_x + dx <= nb_iters */
3921 ineq = omega_add_zero_geq (pb, omega_black);
3922 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3923 pb->geqs[ineq].coef[i + 1] = -1;
3924 pb->geqs[ineq].coef[0] = nbi;
3926 /* A step "dx" bigger than nb_iters is not feasible, so
3927 add "0 <= nb_iters + dx", */
3928 ineq = omega_add_zero_geq (pb, omega_black);
3929 pb->geqs[ineq].coef[i + 1] = 1;
3930 pb->geqs[ineq].coef[0] = nbi;
3931 /* and "dx <= nb_iters". */
3932 ineq = omega_add_zero_geq (pb, omega_black);
3933 pb->geqs[ineq].coef[i + 1] = -1;
3934 pb->geqs[ineq].coef[0] = nbi;
3938 omega_extract_distance_vectors (pb, ddr);
3940 return true;
3943 /* Sets up the Omega dependence problem for the data dependence
3944 relation DDR. Returns false when the constraint system cannot be
3945 built, ie. when the test answers "don't know". Returns true
3946 otherwise, and when independence has been proved (using one of the
3947 trivial dependence test), set MAYBE_DEPENDENT to false, otherwise
3948 set MAYBE_DEPENDENT to true.
3950 Example: for setting up the dependence system corresponding to the
3951 conflicting accesses
3953 | loop_i
3954 | loop_j
3955 | A[i, i+1] = ...
3956 | ... A[2*j, 2*(i + j)]
3957 | endloop_j
3958 | endloop_i
3960 the following constraints come from the iteration domain:
3962 0 <= i <= Ni
3963 0 <= i + di <= Ni
3964 0 <= j <= Nj
3965 0 <= j + dj <= Nj
3967 where di, dj are the distance variables. The constraints
3968 representing the conflicting elements are:
3970 i = 2 * (j + dj)
3971 i + 1 = 2 * (i + di + j + dj)
3973 For asking that the resulting distance vector (di, dj) be
3974 lexicographically positive, we insert the constraint "di >= 0". If
3975 "di = 0" in the solution, we fix that component to zero, and we
3976 look at the inner loops: we set a new problem where all the outer
3977 loop distances are zero, and fix this inner component to be
3978 positive. When one of the components is positive, we save that
3979 distance, and set a new problem where the distance on this loop is
3980 zero, searching for other distances in the inner loops. Here is
3981 the classic example that illustrates that we have to set for each
3982 inner loop a new problem:
3984 | loop_1
3985 | loop_2
3986 | A[10]
3987 | endloop_2
3988 | endloop_1
3990 we have to save two distances (1, 0) and (0, 1).
3992 Given two array references, refA and refB, we have to set the
3993 dependence problem twice, refA vs. refB and refB vs. refA, and we
3994 cannot do a single test, as refB might occur before refA in the
3995 inner loops, and the contrary when considering outer loops: ex.
3997 | loop_0
3998 | loop_1
3999 | loop_2
4000 | T[{1,+,1}_2][{1,+,1}_1] // refA
4001 | T[{2,+,1}_2][{0,+,1}_1] // refB
4002 | endloop_2
4003 | endloop_1
4004 | endloop_0
4006 refB touches the elements in T before refA, and thus for the same
4007 loop_0 refB precedes refA: ie. the distance vector (0, 1, -1)
4008 but for successive loop_0 iterations, we have (1, -1, 1)
4010 The Omega solver expects the distance variables ("di" in the
4011 previous example) to come first in the constraint system (as
4012 variables to be protected, or "safe" variables), the constraint
4013 system is built using the following layout:
4015 "cst | distance vars | index vars".
4018 static bool
4019 init_omega_for_ddr (struct data_dependence_relation *ddr,
4020 bool *maybe_dependent)
4022 omega_pb pb;
4023 bool res = false;
4025 *maybe_dependent = true;
4027 if (same_access_functions (ddr))
4029 unsigned j;
4030 lambda_vector dir_v;
4032 /* Save the 0 vector. */
4033 save_dist_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
4034 dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4035 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
4036 dir_v[j] = dir_equal;
4037 save_dir_v (ddr, dir_v);
4039 /* Save the dependences carried by outer loops. */
4040 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4041 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4042 maybe_dependent);
4043 omega_free_problem (pb);
4044 return res;
4047 /* Omega expects the protected variables (those that have to be kept
4048 after elimination) to appear first in the constraint system.
4049 These variables are the distance variables. In the following
4050 initialization we declare NB_LOOPS safe variables, and the total
4051 number of variables for the constraint system is 2*NB_LOOPS. */
4052 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4053 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4054 maybe_dependent);
4055 omega_free_problem (pb);
4057 /* Stop computation if not decidable, or no dependence. */
4058 if (res == false || *maybe_dependent == false)
4059 return res;
4061 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4062 res = init_omega_for_ddr_1 (DDR_B (ddr), DDR_A (ddr), ddr, pb,
4063 maybe_dependent);
4064 omega_free_problem (pb);
4066 return res;
4069 /* Return true when DDR contains the same information as that stored
4070 in DIR_VECTS and in DIST_VECTS, return false otherwise. */
4072 static bool
4073 ddr_consistent_p (FILE *file,
4074 struct data_dependence_relation *ddr,
4075 vec<lambda_vector> dist_vects,
4076 vec<lambda_vector> dir_vects)
4078 unsigned int i, j;
4080 /* If dump_file is set, output there. */
4081 if (dump_file && (dump_flags & TDF_DETAILS))
4082 file = dump_file;
4084 if (dist_vects.length () != DDR_NUM_DIST_VECTS (ddr))
4086 lambda_vector b_dist_v;
4087 fprintf (file, "\n(Number of distance vectors differ: Banerjee has %d, Omega has %d.\n",
4088 dist_vects.length (),
4089 DDR_NUM_DIST_VECTS (ddr));
4091 fprintf (file, "Banerjee dist vectors:\n");
4092 FOR_EACH_VEC_ELT (dist_vects, i, b_dist_v)
4093 print_lambda_vector (file, b_dist_v, DDR_NB_LOOPS (ddr));
4095 fprintf (file, "Omega dist vectors:\n");
4096 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4097 print_lambda_vector (file, DDR_DIST_VECT (ddr, i), DDR_NB_LOOPS (ddr));
4099 fprintf (file, "data dependence relation:\n");
4100 dump_data_dependence_relation (file, ddr);
4102 fprintf (file, ")\n");
4103 return false;
4106 if (dir_vects.length () != DDR_NUM_DIR_VECTS (ddr))
4108 fprintf (file, "\n(Number of direction vectors differ: Banerjee has %d, Omega has %d.)\n",
4109 dir_vects.length (),
4110 DDR_NUM_DIR_VECTS (ddr));
4111 return false;
4114 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4116 lambda_vector a_dist_v;
4117 lambda_vector b_dist_v = DDR_DIST_VECT (ddr, i);
4119 /* Distance vectors are not ordered in the same way in the DDR
4120 and in the DIST_VECTS: search for a matching vector. */
4121 FOR_EACH_VEC_ELT (dist_vects, j, a_dist_v)
4122 if (lambda_vector_equal (a_dist_v, b_dist_v, DDR_NB_LOOPS (ddr)))
4123 break;
4125 if (j == dist_vects.length ())
4127 fprintf (file, "\n(Dist vectors from the first dependence analyzer:\n");
4128 print_dist_vectors (file, dist_vects, DDR_NB_LOOPS (ddr));
4129 fprintf (file, "not found in Omega dist vectors:\n");
4130 print_dist_vectors (file, DDR_DIST_VECTS (ddr), DDR_NB_LOOPS (ddr));
4131 fprintf (file, "data dependence relation:\n");
4132 dump_data_dependence_relation (file, ddr);
4133 fprintf (file, ")\n");
4137 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
4139 lambda_vector a_dir_v;
4140 lambda_vector b_dir_v = DDR_DIR_VECT (ddr, i);
4142 /* Direction vectors are not ordered in the same way in the DDR
4143 and in the DIR_VECTS: search for a matching vector. */
4144 FOR_EACH_VEC_ELT (dir_vects, j, a_dir_v)
4145 if (lambda_vector_equal (a_dir_v, b_dir_v, DDR_NB_LOOPS (ddr)))
4146 break;
4148 if (j == dist_vects.length ())
4150 fprintf (file, "\n(Dir vectors from the first dependence analyzer:\n");
4151 print_dir_vectors (file, dir_vects, DDR_NB_LOOPS (ddr));
4152 fprintf (file, "not found in Omega dir vectors:\n");
4153 print_dir_vectors (file, DDR_DIR_VECTS (ddr), DDR_NB_LOOPS (ddr));
4154 fprintf (file, "data dependence relation:\n");
4155 dump_data_dependence_relation (file, ddr);
4156 fprintf (file, ")\n");
4160 return true;
4163 /* This computes the affine dependence relation between A and B with
4164 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
4165 independence between two accesses, while CHREC_DONT_KNOW is used
4166 for representing the unknown relation.
4168 Note that it is possible to stop the computation of the dependence
4169 relation the first time we detect a CHREC_KNOWN element for a given
4170 subscript. */
4172 void
4173 compute_affine_dependence (struct data_dependence_relation *ddr,
4174 struct loop *loop_nest)
4176 struct data_reference *dra = DDR_A (ddr);
4177 struct data_reference *drb = DDR_B (ddr);
4179 if (dump_file && (dump_flags & TDF_DETAILS))
4181 fprintf (dump_file, "(compute_affine_dependence\n");
4182 fprintf (dump_file, " stmt_a: ");
4183 print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
4184 fprintf (dump_file, " stmt_b: ");
4185 print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
4188 /* Analyze only when the dependence relation is not yet known. */
4189 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4191 dependence_stats.num_dependence_tests++;
4193 if (access_functions_are_affine_or_constant_p (dra, loop_nest)
4194 && access_functions_are_affine_or_constant_p (drb, loop_nest))
4196 subscript_dependence_tester (ddr, loop_nest);
4198 if (flag_check_data_deps)
4200 /* Dump the dependences from the first algorithm. */
4201 if (dump_file && (dump_flags & TDF_DETAILS))
4203 fprintf (dump_file, "\n\nBanerjee Analyzer\n");
4204 dump_data_dependence_relation (dump_file, ddr);
4207 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4209 bool maybe_dependent;
4210 vec<lambda_vector> dir_vects, dist_vects;
4212 /* Save the result of the first DD analyzer. */
4213 dist_vects = DDR_DIST_VECTS (ddr);
4214 dir_vects = DDR_DIR_VECTS (ddr);
4216 /* Reset the information. */
4217 DDR_DIST_VECTS (ddr).create (0);
4218 DDR_DIR_VECTS (ddr).create (0);
4220 /* Compute the same information using Omega. */
4221 if (!init_omega_for_ddr (ddr, &maybe_dependent))
4222 goto csys_dont_know;
4224 if (dump_file && (dump_flags & TDF_DETAILS))
4226 fprintf (dump_file, "Omega Analyzer\n");
4227 dump_data_dependence_relation (dump_file, ddr);
4230 /* Check that we get the same information. */
4231 if (maybe_dependent)
4232 gcc_assert (ddr_consistent_p (stderr, ddr, dist_vects,
4233 dir_vects));
4238 /* As a last case, if the dependence cannot be determined, or if
4239 the dependence is considered too difficult to determine, answer
4240 "don't know". */
4241 else
4243 csys_dont_know:;
4244 dependence_stats.num_dependence_undetermined++;
4246 if (dump_file && (dump_flags & TDF_DETAILS))
4248 fprintf (dump_file, "Data ref a:\n");
4249 dump_data_reference (dump_file, dra);
4250 fprintf (dump_file, "Data ref b:\n");
4251 dump_data_reference (dump_file, drb);
4252 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
4254 finalize_ddr_dependent (ddr, chrec_dont_know);
4258 if (dump_file && (dump_flags & TDF_DETAILS))
4260 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4261 fprintf (dump_file, ") -> no dependence\n");
4262 else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
4263 fprintf (dump_file, ") -> dependence analysis failed\n");
4264 else
4265 fprintf (dump_file, ")\n");
4269 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
4270 the data references in DATAREFS, in the LOOP_NEST. When
4271 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
4272 relations. Return true when successful, i.e. data references number
4273 is small enough to be handled. */
4275 bool
4276 compute_all_dependences (vec<data_reference_p> datarefs,
4277 vec<ddr_p> *dependence_relations,
4278 vec<loop_p> loop_nest,
4279 bool compute_self_and_rr)
4281 struct data_dependence_relation *ddr;
4282 struct data_reference *a, *b;
4283 unsigned int i, j;
4285 if ((int) datarefs.length ()
4286 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
4288 struct data_dependence_relation *ddr;
4290 /* Insert a single relation into dependence_relations:
4291 chrec_dont_know. */
4292 ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
4293 dependence_relations->safe_push (ddr);
4294 return false;
4297 FOR_EACH_VEC_ELT (datarefs, i, a)
4298 for (j = i + 1; datarefs.iterate (j, &b); j++)
4299 if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
4301 ddr = initialize_data_dependence_relation (a, b, loop_nest);
4302 dependence_relations->safe_push (ddr);
4303 if (loop_nest.exists ())
4304 compute_affine_dependence (ddr, loop_nest[0]);
4307 if (compute_self_and_rr)
4308 FOR_EACH_VEC_ELT (datarefs, i, a)
4310 ddr = initialize_data_dependence_relation (a, a, loop_nest);
4311 dependence_relations->safe_push (ddr);
4312 if (loop_nest.exists ())
4313 compute_affine_dependence (ddr, loop_nest[0]);
4316 return true;
4319 /* Describes a location of a memory reference. */
4321 typedef struct data_ref_loc_d
4323 /* The memory reference. */
4324 tree ref;
4326 /* True if the memory reference is read. */
4327 bool is_read;
4328 } data_ref_loc;
4331 /* Stores the locations of memory references in STMT to REFERENCES. Returns
4332 true if STMT clobbers memory, false otherwise. */
4334 static bool
4335 get_references_in_stmt (gimple stmt, vec<data_ref_loc, va_heap> *references)
4337 bool clobbers_memory = false;
4338 data_ref_loc ref;
4339 tree op0, op1;
4340 enum gimple_code stmt_code = gimple_code (stmt);
4342 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4343 As we cannot model data-references to not spelled out
4344 accesses give up if they may occur. */
4345 if (stmt_code == GIMPLE_CALL
4346 && !(gimple_call_flags (stmt) & ECF_CONST))
4348 /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
4349 if (gimple_call_internal_p (stmt))
4350 switch (gimple_call_internal_fn (stmt))
4352 case IFN_GOMP_SIMD_LANE:
4354 struct loop *loop = gimple_bb (stmt)->loop_father;
4355 tree uid = gimple_call_arg (stmt, 0);
4356 gcc_assert (TREE_CODE (uid) == SSA_NAME);
4357 if (loop == NULL
4358 || loop->simduid != SSA_NAME_VAR (uid))
4359 clobbers_memory = true;
4360 break;
4362 case IFN_MASK_LOAD:
4363 case IFN_MASK_STORE:
4364 break;
4365 default:
4366 clobbers_memory = true;
4367 break;
4369 else
4370 clobbers_memory = true;
4372 else if (stmt_code == GIMPLE_ASM
4373 && (gimple_asm_volatile_p (stmt) || gimple_vuse (stmt)))
4374 clobbers_memory = true;
4376 if (!gimple_vuse (stmt))
4377 return clobbers_memory;
4379 if (stmt_code == GIMPLE_ASSIGN)
4381 tree base;
4382 op0 = gimple_assign_lhs (stmt);
4383 op1 = gimple_assign_rhs1 (stmt);
4385 if (DECL_P (op1)
4386 || (REFERENCE_CLASS_P (op1)
4387 && (base = get_base_address (op1))
4388 && TREE_CODE (base) != SSA_NAME))
4390 ref.ref = op1;
4391 ref.is_read = true;
4392 references->safe_push (ref);
4395 else if (stmt_code == GIMPLE_CALL)
4397 unsigned i, n;
4399 ref.is_read = false;
4400 if (gimple_call_internal_p (stmt))
4401 switch (gimple_call_internal_fn (stmt))
4403 case IFN_MASK_LOAD:
4404 if (gimple_call_lhs (stmt) == NULL_TREE)
4405 break;
4406 ref.is_read = true;
4407 case IFN_MASK_STORE:
4408 ref.ref = fold_build2 (MEM_REF,
4409 ref.is_read
4410 ? TREE_TYPE (gimple_call_lhs (stmt))
4411 : TREE_TYPE (gimple_call_arg (stmt, 3)),
4412 gimple_call_arg (stmt, 0),
4413 gimple_call_arg (stmt, 1));
4414 references->safe_push (ref);
4415 return false;
4416 default:
4417 break;
4420 op0 = gimple_call_lhs (stmt);
4421 n = gimple_call_num_args (stmt);
4422 for (i = 0; i < n; i++)
4424 op1 = gimple_call_arg (stmt, i);
4426 if (DECL_P (op1)
4427 || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
4429 ref.ref = op1;
4430 ref.is_read = true;
4431 references->safe_push (ref);
4435 else
4436 return clobbers_memory;
4438 if (op0
4439 && (DECL_P (op0)
4440 || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
4442 ref.ref = op0;
4443 ref.is_read = false;
4444 references->safe_push (ref);
4446 return clobbers_memory;
4449 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
4450 reference, returns false, otherwise returns true. NEST is the outermost
4451 loop of the loop nest in which the references should be analyzed. */
4453 bool
4454 find_data_references_in_stmt (struct loop *nest, gimple stmt,
4455 vec<data_reference_p> *datarefs)
4457 unsigned i;
4458 auto_vec<data_ref_loc, 2> references;
4459 data_ref_loc *ref;
4460 bool ret = true;
4461 data_reference_p dr;
4463 if (get_references_in_stmt (stmt, &references))
4464 return false;
4466 FOR_EACH_VEC_ELT (references, i, ref)
4468 dr = create_data_ref (nest, loop_containing_stmt (stmt),
4469 ref->ref, stmt, ref->is_read);
4470 gcc_assert (dr != NULL);
4471 datarefs->safe_push (dr);
4473 references.release ();
4474 return ret;
4477 /* Stores the data references in STMT to DATAREFS. If there is an
4478 unanalyzable reference, returns false, otherwise returns true.
4479 NEST is the outermost loop of the loop nest in which the references
4480 should be instantiated, LOOP is the loop in which the references
4481 should be analyzed. */
4483 bool
4484 graphite_find_data_references_in_stmt (loop_p nest, loop_p loop, gimple stmt,
4485 vec<data_reference_p> *datarefs)
4487 unsigned i;
4488 auto_vec<data_ref_loc, 2> references;
4489 data_ref_loc *ref;
4490 bool ret = true;
4491 data_reference_p dr;
4493 if (get_references_in_stmt (stmt, &references))
4494 return false;
4496 FOR_EACH_VEC_ELT (references, i, ref)
4498 dr = create_data_ref (nest, loop, ref->ref, stmt, ref->is_read);
4499 gcc_assert (dr != NULL);
4500 datarefs->safe_push (dr);
4503 references.release ();
4504 return ret;
4507 /* Search the data references in LOOP, and record the information into
4508 DATAREFS. Returns chrec_dont_know when failing to analyze a
4509 difficult case, returns NULL_TREE otherwise. */
4511 tree
4512 find_data_references_in_bb (struct loop *loop, basic_block bb,
4513 vec<data_reference_p> *datarefs)
4515 gimple_stmt_iterator bsi;
4517 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4519 gimple stmt = gsi_stmt (bsi);
4521 if (!find_data_references_in_stmt (loop, stmt, datarefs))
4523 struct data_reference *res;
4524 res = XCNEW (struct data_reference);
4525 datarefs->safe_push (res);
4527 return chrec_dont_know;
4531 return NULL_TREE;
4534 /* Search the data references in LOOP, and record the information into
4535 DATAREFS. Returns chrec_dont_know when failing to analyze a
4536 difficult case, returns NULL_TREE otherwise.
4538 TODO: This function should be made smarter so that it can handle address
4539 arithmetic as if they were array accesses, etc. */
4541 tree
4542 find_data_references_in_loop (struct loop *loop,
4543 vec<data_reference_p> *datarefs)
4545 basic_block bb, *bbs;
4546 unsigned int i;
4548 bbs = get_loop_body_in_dom_order (loop);
4550 for (i = 0; i < loop->num_nodes; i++)
4552 bb = bbs[i];
4554 if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
4556 free (bbs);
4557 return chrec_dont_know;
4560 free (bbs);
4562 return NULL_TREE;
4565 /* Recursive helper function. */
4567 static bool
4568 find_loop_nest_1 (struct loop *loop, vec<loop_p> *loop_nest)
4570 /* Inner loops of the nest should not contain siblings. Example:
4571 when there are two consecutive loops,
4573 | loop_0
4574 | loop_1
4575 | A[{0, +, 1}_1]
4576 | endloop_1
4577 | loop_2
4578 | A[{0, +, 1}_2]
4579 | endloop_2
4580 | endloop_0
4582 the dependence relation cannot be captured by the distance
4583 abstraction. */
4584 if (loop->next)
4585 return false;
4587 loop_nest->safe_push (loop);
4588 if (loop->inner)
4589 return find_loop_nest_1 (loop->inner, loop_nest);
4590 return true;
4593 /* Return false when the LOOP is not well nested. Otherwise return
4594 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4595 contain the loops from the outermost to the innermost, as they will
4596 appear in the classic distance vector. */
4598 bool
4599 find_loop_nest (struct loop *loop, vec<loop_p> *loop_nest)
4601 loop_nest->safe_push (loop);
4602 if (loop->inner)
4603 return find_loop_nest_1 (loop->inner, loop_nest);
4604 return true;
4607 /* Returns true when the data dependences have been computed, false otherwise.
4608 Given a loop nest LOOP, the following vectors are returned:
4609 DATAREFS is initialized to all the array elements contained in this loop,
4610 DEPENDENCE_RELATIONS contains the relations between the data references.
4611 Compute read-read and self relations if
4612 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4614 bool
4615 compute_data_dependences_for_loop (struct loop *loop,
4616 bool compute_self_and_read_read_dependences,
4617 vec<loop_p> *loop_nest,
4618 vec<data_reference_p> *datarefs,
4619 vec<ddr_p> *dependence_relations)
4621 bool res = true;
4623 memset (&dependence_stats, 0, sizeof (dependence_stats));
4625 /* If the loop nest is not well formed, or one of the data references
4626 is not computable, give up without spending time to compute other
4627 dependences. */
4628 if (!loop
4629 || !find_loop_nest (loop, loop_nest)
4630 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
4631 || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
4632 compute_self_and_read_read_dependences))
4633 res = false;
4635 if (dump_file && (dump_flags & TDF_STATS))
4637 fprintf (dump_file, "Dependence tester statistics:\n");
4639 fprintf (dump_file, "Number of dependence tests: %d\n",
4640 dependence_stats.num_dependence_tests);
4641 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
4642 dependence_stats.num_dependence_dependent);
4643 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
4644 dependence_stats.num_dependence_independent);
4645 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
4646 dependence_stats.num_dependence_undetermined);
4648 fprintf (dump_file, "Number of subscript tests: %d\n",
4649 dependence_stats.num_subscript_tests);
4650 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
4651 dependence_stats.num_subscript_undetermined);
4652 fprintf (dump_file, "Number of same subscript function: %d\n",
4653 dependence_stats.num_same_subscript_function);
4655 fprintf (dump_file, "Number of ziv tests: %d\n",
4656 dependence_stats.num_ziv);
4657 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4658 dependence_stats.num_ziv_dependent);
4659 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4660 dependence_stats.num_ziv_independent);
4661 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
4662 dependence_stats.num_ziv_unimplemented);
4664 fprintf (dump_file, "Number of siv tests: %d\n",
4665 dependence_stats.num_siv);
4666 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4667 dependence_stats.num_siv_dependent);
4668 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4669 dependence_stats.num_siv_independent);
4670 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4671 dependence_stats.num_siv_unimplemented);
4673 fprintf (dump_file, "Number of miv tests: %d\n",
4674 dependence_stats.num_miv);
4675 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4676 dependence_stats.num_miv_dependent);
4677 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4678 dependence_stats.num_miv_independent);
4679 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4680 dependence_stats.num_miv_unimplemented);
4683 return res;
4686 /* Returns true when the data dependences for the basic block BB have been
4687 computed, false otherwise.
4688 DATAREFS is initialized to all the array elements contained in this basic
4689 block, DEPENDENCE_RELATIONS contains the relations between the data
4690 references. Compute read-read and self relations if
4691 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4692 bool
4693 compute_data_dependences_for_bb (basic_block bb,
4694 bool compute_self_and_read_read_dependences,
4695 vec<data_reference_p> *datarefs,
4696 vec<ddr_p> *dependence_relations)
4698 if (find_data_references_in_bb (NULL, bb, datarefs) == chrec_dont_know)
4699 return false;
4701 return compute_all_dependences (*datarefs, dependence_relations, vNULL,
4702 compute_self_and_read_read_dependences);
4705 /* Entry point (for testing only). Analyze all the data references
4706 and the dependence relations in LOOP.
4708 The data references are computed first.
4710 A relation on these nodes is represented by a complete graph. Some
4711 of the relations could be of no interest, thus the relations can be
4712 computed on demand.
4714 In the following function we compute all the relations. This is
4715 just a first implementation that is here for:
4716 - for showing how to ask for the dependence relations,
4717 - for the debugging the whole dependence graph,
4718 - for the dejagnu testcases and maintenance.
4720 It is possible to ask only for a part of the graph, avoiding to
4721 compute the whole dependence graph. The computed dependences are
4722 stored in a knowledge base (KB) such that later queries don't
4723 recompute the same information. The implementation of this KB is
4724 transparent to the optimizer, and thus the KB can be changed with a
4725 more efficient implementation, or the KB could be disabled. */
4726 static void
4727 analyze_all_data_dependences (struct loop *loop)
4729 unsigned int i;
4730 int nb_data_refs = 10;
4731 vec<data_reference_p> datarefs;
4732 datarefs.create (nb_data_refs);
4733 vec<ddr_p> dependence_relations;
4734 dependence_relations.create (nb_data_refs * nb_data_refs);
4735 vec<loop_p> loop_nest;
4736 loop_nest.create (3);
4738 /* Compute DDs on the whole function. */
4739 compute_data_dependences_for_loop (loop, false, &loop_nest, &datarefs,
4740 &dependence_relations);
4742 if (dump_file)
4744 dump_data_dependence_relations (dump_file, dependence_relations);
4745 fprintf (dump_file, "\n\n");
4747 if (dump_flags & TDF_DETAILS)
4748 dump_dist_dir_vectors (dump_file, dependence_relations);
4750 if (dump_flags & TDF_STATS)
4752 unsigned nb_top_relations = 0;
4753 unsigned nb_bot_relations = 0;
4754 unsigned nb_chrec_relations = 0;
4755 struct data_dependence_relation *ddr;
4757 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4759 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
4760 nb_top_relations++;
4762 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4763 nb_bot_relations++;
4765 else
4766 nb_chrec_relations++;
4769 gather_stats_on_scev_database ();
4773 loop_nest.release ();
4774 free_dependence_relations (dependence_relations);
4775 free_data_refs (datarefs);
4778 /* Computes all the data dependences and check that the results of
4779 several analyzers are the same. */
4781 void
4782 tree_check_data_deps (void)
4784 struct loop *loop_nest;
4786 FOR_EACH_LOOP (loop_nest, 0)
4787 analyze_all_data_dependences (loop_nest);
4790 /* Free the memory used by a data dependence relation DDR. */
4792 void
4793 free_dependence_relation (struct data_dependence_relation *ddr)
4795 if (ddr == NULL)
4796 return;
4798 if (DDR_SUBSCRIPTS (ddr).exists ())
4799 free_subscripts (DDR_SUBSCRIPTS (ddr));
4800 DDR_DIST_VECTS (ddr).release ();
4801 DDR_DIR_VECTS (ddr).release ();
4803 free (ddr);
4806 /* Free the memory used by the data dependence relations from
4807 DEPENDENCE_RELATIONS. */
4809 void
4810 free_dependence_relations (vec<ddr_p> dependence_relations)
4812 unsigned int i;
4813 struct data_dependence_relation *ddr;
4815 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4816 if (ddr)
4817 free_dependence_relation (ddr);
4819 dependence_relations.release ();
4822 /* Free the memory used by the data references from DATAREFS. */
4824 void
4825 free_data_refs (vec<data_reference_p> datarefs)
4827 unsigned int i;
4828 struct data_reference *dr;
4830 FOR_EACH_VEC_ELT (datarefs, i, dr)
4831 free_data_ref (dr);
4832 datarefs.release ();