Use -fbuilding-libgcc for more target macros used in libgcc.
[official-gcc.git] / gcc / tree-data-ref.c
blob89aeb30c1e57ad265dc0b5ef421d9cd23f88841a
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 offset_int moff = mem_ref_offset (base);
787 tree mofft = wide_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 STRIP_USELESS_TYPE_CONVERSION (base);
966 /* Fold the MEM_REF offset into the evolutions initial
967 value to make more bases comparable. */
968 if (!integer_zerop (memoff))
970 off = size_binop (PLUS_EXPR, off,
971 fold_convert (ssizetype, memoff));
972 memoff = build_int_cst (TREE_TYPE (memoff), 0);
974 access_fn = chrec_replace_initial_condition
975 (access_fn, fold_convert (orig_type, off));
976 /* ??? This is still not a suitable base object for
977 dr_may_alias_p - the base object needs to be an
978 access that covers the object as whole. With
979 an evolution in the pointer this cannot be
980 guaranteed.
981 As a band-aid, mark the access so we can special-case
982 it in dr_may_alias_p. */
983 ref = fold_build2_loc (EXPR_LOCATION (ref),
984 MEM_REF, TREE_TYPE (ref),
985 base, memoff);
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 widest_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, -1);
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 pointer-based MEM_REF BASE_OBJECT we
1393 do not know the size of the base-object. So we cannot do any
1394 offset/overlap based analysis but have to rely on points-to
1395 information only. */
1396 if (TREE_CODE (addr_a) == MEM_REF
1397 && TREE_CODE (TREE_OPERAND (addr_a, 0)) == SSA_NAME)
1399 /* For true dependences we can apply TBAA. */
1400 if (flag_strict_aliasing
1401 && DR_IS_WRITE (a) && DR_IS_READ (b)
1402 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1403 get_alias_set (DR_REF (b))))
1404 return false;
1405 if (TREE_CODE (addr_b) == MEM_REF)
1406 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1407 TREE_OPERAND (addr_b, 0));
1408 else
1409 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1410 build_fold_addr_expr (addr_b));
1412 else if (TREE_CODE (addr_b) == MEM_REF
1413 && TREE_CODE (TREE_OPERAND (addr_b, 0)) == SSA_NAME)
1415 /* For true dependences we can apply TBAA. */
1416 if (flag_strict_aliasing
1417 && DR_IS_WRITE (a) && DR_IS_READ (b)
1418 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1419 get_alias_set (DR_REF (b))))
1420 return false;
1421 if (TREE_CODE (addr_a) == MEM_REF)
1422 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1423 TREE_OPERAND (addr_b, 0));
1424 else
1425 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
1426 TREE_OPERAND (addr_b, 0));
1429 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1430 that is being subsetted in the loop nest. */
1431 if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
1432 return refs_output_dependent_p (addr_a, addr_b);
1433 else if (DR_IS_READ (a) && DR_IS_WRITE (b))
1434 return refs_anti_dependent_p (addr_a, addr_b);
1435 return refs_may_alias_p (addr_a, addr_b);
1438 /* Initialize a data dependence relation between data accesses A and
1439 B. NB_LOOPS is the number of loops surrounding the references: the
1440 size of the classic distance/direction vectors. */
1442 struct data_dependence_relation *
1443 initialize_data_dependence_relation (struct data_reference *a,
1444 struct data_reference *b,
1445 vec<loop_p> loop_nest)
1447 struct data_dependence_relation *res;
1448 unsigned int i;
1450 res = XNEW (struct data_dependence_relation);
1451 DDR_A (res) = a;
1452 DDR_B (res) = b;
1453 DDR_LOOP_NEST (res).create (0);
1454 DDR_REVERSED_P (res) = false;
1455 DDR_SUBSCRIPTS (res).create (0);
1456 DDR_DIR_VECTS (res).create (0);
1457 DDR_DIST_VECTS (res).create (0);
1459 if (a == NULL || b == NULL)
1461 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1462 return res;
1465 /* If the data references do not alias, then they are independent. */
1466 if (!dr_may_alias_p (a, b, loop_nest.exists ()))
1468 DDR_ARE_DEPENDENT (res) = chrec_known;
1469 return res;
1472 /* The case where the references are exactly the same. */
1473 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
1475 if (loop_nest.exists ()
1476 && !object_address_invariant_in_loop_p (loop_nest[0],
1477 DR_BASE_OBJECT (a)))
1479 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1480 return res;
1482 DDR_AFFINE_P (res) = true;
1483 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1484 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1485 DDR_LOOP_NEST (res) = loop_nest;
1486 DDR_INNER_LOOP (res) = 0;
1487 DDR_SELF_REFERENCE (res) = true;
1488 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1490 struct subscript *subscript;
1492 subscript = XNEW (struct subscript);
1493 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1494 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1495 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1496 SUB_DISTANCE (subscript) = chrec_dont_know;
1497 DDR_SUBSCRIPTS (res).safe_push (subscript);
1499 return res;
1502 /* If the references do not access the same object, we do not know
1503 whether they alias or not. */
1504 if (!operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0))
1506 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1507 return res;
1510 /* If the base of the object is not invariant in the loop nest, we cannot
1511 analyze it. TODO -- in fact, it would suffice to record that there may
1512 be arbitrary dependences in the loops where the base object varies. */
1513 if (loop_nest.exists ()
1514 && !object_address_invariant_in_loop_p (loop_nest[0],
1515 DR_BASE_OBJECT (a)))
1517 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1518 return res;
1521 /* If the number of dimensions of the access to not agree we can have
1522 a pointer access to a component of the array element type and an
1523 array access while the base-objects are still the same. Punt. */
1524 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1526 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1527 return res;
1530 DDR_AFFINE_P (res) = true;
1531 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1532 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1533 DDR_LOOP_NEST (res) = loop_nest;
1534 DDR_INNER_LOOP (res) = 0;
1535 DDR_SELF_REFERENCE (res) = false;
1537 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1539 struct subscript *subscript;
1541 subscript = XNEW (struct subscript);
1542 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1543 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1544 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1545 SUB_DISTANCE (subscript) = chrec_dont_know;
1546 DDR_SUBSCRIPTS (res).safe_push (subscript);
1549 return res;
1552 /* Frees memory used by the conflict function F. */
1554 static void
1555 free_conflict_function (conflict_function *f)
1557 unsigned i;
1559 if (CF_NONTRIVIAL_P (f))
1561 for (i = 0; i < f->n; i++)
1562 affine_fn_free (f->fns[i]);
1564 free (f);
1567 /* Frees memory used by SUBSCRIPTS. */
1569 static void
1570 free_subscripts (vec<subscript_p> subscripts)
1572 unsigned i;
1573 subscript_p s;
1575 FOR_EACH_VEC_ELT (subscripts, i, s)
1577 free_conflict_function (s->conflicting_iterations_in_a);
1578 free_conflict_function (s->conflicting_iterations_in_b);
1579 free (s);
1581 subscripts.release ();
1584 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1585 description. */
1587 static inline void
1588 finalize_ddr_dependent (struct data_dependence_relation *ddr,
1589 tree chrec)
1591 DDR_ARE_DEPENDENT (ddr) = chrec;
1592 free_subscripts (DDR_SUBSCRIPTS (ddr));
1593 DDR_SUBSCRIPTS (ddr).create (0);
1596 /* The dependence relation DDR cannot be represented by a distance
1597 vector. */
1599 static inline void
1600 non_affine_dependence_relation (struct data_dependence_relation *ddr)
1602 if (dump_file && (dump_flags & TDF_DETAILS))
1603 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
1605 DDR_AFFINE_P (ddr) = false;
1610 /* This section contains the classic Banerjee tests. */
1612 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1613 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1615 static inline bool
1616 ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1618 return (evolution_function_is_constant_p (chrec_a)
1619 && evolution_function_is_constant_p (chrec_b));
1622 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
1623 variable, i.e., if the SIV (Single Index Variable) test is true. */
1625 static bool
1626 siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1628 if ((evolution_function_is_constant_p (chrec_a)
1629 && evolution_function_is_univariate_p (chrec_b))
1630 || (evolution_function_is_constant_p (chrec_b)
1631 && evolution_function_is_univariate_p (chrec_a)))
1632 return true;
1634 if (evolution_function_is_univariate_p (chrec_a)
1635 && evolution_function_is_univariate_p (chrec_b))
1637 switch (TREE_CODE (chrec_a))
1639 case POLYNOMIAL_CHREC:
1640 switch (TREE_CODE (chrec_b))
1642 case POLYNOMIAL_CHREC:
1643 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
1644 return false;
1646 default:
1647 return true;
1650 default:
1651 return true;
1655 return false;
1658 /* Creates a conflict function with N dimensions. The affine functions
1659 in each dimension follow. */
1661 static conflict_function *
1662 conflict_fn (unsigned n, ...)
1664 unsigned i;
1665 conflict_function *ret = XCNEW (conflict_function);
1666 va_list ap;
1668 gcc_assert (0 < n && n <= MAX_DIM);
1669 va_start (ap, n);
1671 ret->n = n;
1672 for (i = 0; i < n; i++)
1673 ret->fns[i] = va_arg (ap, affine_fn);
1674 va_end (ap);
1676 return ret;
1679 /* Returns constant affine function with value CST. */
1681 static affine_fn
1682 affine_fn_cst (tree cst)
1684 affine_fn fn;
1685 fn.create (1);
1686 fn.quick_push (cst);
1687 return fn;
1690 /* Returns affine function with single variable, CST + COEF * x_DIM. */
1692 static affine_fn
1693 affine_fn_univar (tree cst, unsigned dim, tree coef)
1695 affine_fn fn;
1696 fn.create (dim + 1);
1697 unsigned i;
1699 gcc_assert (dim > 0);
1700 fn.quick_push (cst);
1701 for (i = 1; i < dim; i++)
1702 fn.quick_push (integer_zero_node);
1703 fn.quick_push (coef);
1704 return fn;
1707 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1708 *OVERLAPS_B are initialized to the functions that describe the
1709 relation between the elements accessed twice by CHREC_A and
1710 CHREC_B. For k >= 0, the following property is verified:
1712 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1714 static void
1715 analyze_ziv_subscript (tree chrec_a,
1716 tree chrec_b,
1717 conflict_function **overlaps_a,
1718 conflict_function **overlaps_b,
1719 tree *last_conflicts)
1721 tree type, difference;
1722 dependence_stats.num_ziv++;
1724 if (dump_file && (dump_flags & TDF_DETAILS))
1725 fprintf (dump_file, "(analyze_ziv_subscript \n");
1727 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1728 chrec_a = chrec_convert (type, chrec_a, NULL);
1729 chrec_b = chrec_convert (type, chrec_b, NULL);
1730 difference = chrec_fold_minus (type, chrec_a, chrec_b);
1732 switch (TREE_CODE (difference))
1734 case INTEGER_CST:
1735 if (integer_zerop (difference))
1737 /* The difference is equal to zero: the accessed index
1738 overlaps for each iteration in the loop. */
1739 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1740 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1741 *last_conflicts = chrec_dont_know;
1742 dependence_stats.num_ziv_dependent++;
1744 else
1746 /* The accesses do not overlap. */
1747 *overlaps_a = conflict_fn_no_dependence ();
1748 *overlaps_b = conflict_fn_no_dependence ();
1749 *last_conflicts = integer_zero_node;
1750 dependence_stats.num_ziv_independent++;
1752 break;
1754 default:
1755 /* We're not sure whether the indexes overlap. For the moment,
1756 conservatively answer "don't know". */
1757 if (dump_file && (dump_flags & TDF_DETAILS))
1758 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
1760 *overlaps_a = conflict_fn_not_known ();
1761 *overlaps_b = conflict_fn_not_known ();
1762 *last_conflicts = chrec_dont_know;
1763 dependence_stats.num_ziv_unimplemented++;
1764 break;
1767 if (dump_file && (dump_flags & TDF_DETAILS))
1768 fprintf (dump_file, ")\n");
1771 /* Similar to max_stmt_executions_int, but returns the bound as a tree,
1772 and only if it fits to the int type. If this is not the case, or the
1773 bound on the number of iterations of LOOP could not be derived, returns
1774 chrec_dont_know. */
1776 static tree
1777 max_stmt_executions_tree (struct loop *loop)
1779 widest_int nit;
1781 if (!max_stmt_executions (loop, &nit))
1782 return chrec_dont_know;
1784 if (!wi::fits_to_tree_p (nit, unsigned_type_node))
1785 return chrec_dont_know;
1787 return wide_int_to_tree (unsigned_type_node, nit);
1790 /* Determine whether the CHREC is always positive/negative. If the expression
1791 cannot be statically analyzed, return false, otherwise set the answer into
1792 VALUE. */
1794 static bool
1795 chrec_is_positive (tree chrec, bool *value)
1797 bool value0, value1, value2;
1798 tree end_value, nb_iter;
1800 switch (TREE_CODE (chrec))
1802 case POLYNOMIAL_CHREC:
1803 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
1804 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
1805 return false;
1807 /* FIXME -- overflows. */
1808 if (value0 == value1)
1810 *value = value0;
1811 return true;
1814 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1815 and the proof consists in showing that the sign never
1816 changes during the execution of the loop, from 0 to
1817 loop->nb_iterations. */
1818 if (!evolution_function_is_affine_p (chrec))
1819 return false;
1821 nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
1822 if (chrec_contains_undetermined (nb_iter))
1823 return false;
1825 #if 0
1826 /* TODO -- If the test is after the exit, we may decrease the number of
1827 iterations by one. */
1828 if (after_exit)
1829 nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
1830 #endif
1832 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
1834 if (!chrec_is_positive (end_value, &value2))
1835 return false;
1837 *value = value0;
1838 return value0 == value1;
1840 case INTEGER_CST:
1841 switch (tree_int_cst_sgn (chrec))
1843 case -1:
1844 *value = false;
1845 break;
1846 case 1:
1847 *value = true;
1848 break;
1849 default:
1850 return false;
1852 return true;
1854 default:
1855 return false;
1860 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1861 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1862 *OVERLAPS_B are initialized to the functions that describe the
1863 relation between the elements accessed twice by CHREC_A and
1864 CHREC_B. For k >= 0, the following property is verified:
1866 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1868 static void
1869 analyze_siv_subscript_cst_affine (tree chrec_a,
1870 tree chrec_b,
1871 conflict_function **overlaps_a,
1872 conflict_function **overlaps_b,
1873 tree *last_conflicts)
1875 bool value0, value1, value2;
1876 tree type, difference, tmp;
1878 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1879 chrec_a = chrec_convert (type, chrec_a, NULL);
1880 chrec_b = chrec_convert (type, chrec_b, NULL);
1881 difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
1883 /* Special case overlap in the first iteration. */
1884 if (integer_zerop (difference))
1886 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1887 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1888 *last_conflicts = integer_one_node;
1889 return;
1892 if (!chrec_is_positive (initial_condition (difference), &value0))
1894 if (dump_file && (dump_flags & TDF_DETAILS))
1895 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
1897 dependence_stats.num_siv_unimplemented++;
1898 *overlaps_a = conflict_fn_not_known ();
1899 *overlaps_b = conflict_fn_not_known ();
1900 *last_conflicts = chrec_dont_know;
1901 return;
1903 else
1905 if (value0 == false)
1907 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
1909 if (dump_file && (dump_flags & TDF_DETAILS))
1910 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1912 *overlaps_a = conflict_fn_not_known ();
1913 *overlaps_b = conflict_fn_not_known ();
1914 *last_conflicts = chrec_dont_know;
1915 dependence_stats.num_siv_unimplemented++;
1916 return;
1918 else
1920 if (value1 == true)
1922 /* Example:
1923 chrec_a = 12
1924 chrec_b = {10, +, 1}
1927 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1929 HOST_WIDE_INT numiter;
1930 struct loop *loop = get_chrec_loop (chrec_b);
1932 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1933 tmp = fold_build2 (EXACT_DIV_EXPR, type,
1934 fold_build1 (ABS_EXPR, type, difference),
1935 CHREC_RIGHT (chrec_b));
1936 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1937 *last_conflicts = integer_one_node;
1940 /* Perform weak-zero siv test to see if overlap is
1941 outside the loop bounds. */
1942 numiter = max_stmt_executions_int (loop);
1944 if (numiter >= 0
1945 && compare_tree_int (tmp, numiter) > 0)
1947 free_conflict_function (*overlaps_a);
1948 free_conflict_function (*overlaps_b);
1949 *overlaps_a = conflict_fn_no_dependence ();
1950 *overlaps_b = conflict_fn_no_dependence ();
1951 *last_conflicts = integer_zero_node;
1952 dependence_stats.num_siv_independent++;
1953 return;
1955 dependence_stats.num_siv_dependent++;
1956 return;
1959 /* When the step does not divide the difference, there are
1960 no overlaps. */
1961 else
1963 *overlaps_a = conflict_fn_no_dependence ();
1964 *overlaps_b = conflict_fn_no_dependence ();
1965 *last_conflicts = integer_zero_node;
1966 dependence_stats.num_siv_independent++;
1967 return;
1971 else
1973 /* Example:
1974 chrec_a = 12
1975 chrec_b = {10, +, -1}
1977 In this case, chrec_a will not overlap with chrec_b. */
1978 *overlaps_a = conflict_fn_no_dependence ();
1979 *overlaps_b = conflict_fn_no_dependence ();
1980 *last_conflicts = integer_zero_node;
1981 dependence_stats.num_siv_independent++;
1982 return;
1986 else
1988 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
1990 if (dump_file && (dump_flags & TDF_DETAILS))
1991 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1993 *overlaps_a = conflict_fn_not_known ();
1994 *overlaps_b = conflict_fn_not_known ();
1995 *last_conflicts = chrec_dont_know;
1996 dependence_stats.num_siv_unimplemented++;
1997 return;
1999 else
2001 if (value2 == false)
2003 /* Example:
2004 chrec_a = 3
2005 chrec_b = {10, +, -1}
2007 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2009 HOST_WIDE_INT numiter;
2010 struct loop *loop = get_chrec_loop (chrec_b);
2012 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2013 tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
2014 CHREC_RIGHT (chrec_b));
2015 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
2016 *last_conflicts = integer_one_node;
2018 /* Perform weak-zero siv test to see if overlap is
2019 outside the loop bounds. */
2020 numiter = max_stmt_executions_int (loop);
2022 if (numiter >= 0
2023 && compare_tree_int (tmp, numiter) > 0)
2025 free_conflict_function (*overlaps_a);
2026 free_conflict_function (*overlaps_b);
2027 *overlaps_a = conflict_fn_no_dependence ();
2028 *overlaps_b = conflict_fn_no_dependence ();
2029 *last_conflicts = integer_zero_node;
2030 dependence_stats.num_siv_independent++;
2031 return;
2033 dependence_stats.num_siv_dependent++;
2034 return;
2037 /* When the step does not divide the difference, there
2038 are no overlaps. */
2039 else
2041 *overlaps_a = conflict_fn_no_dependence ();
2042 *overlaps_b = conflict_fn_no_dependence ();
2043 *last_conflicts = integer_zero_node;
2044 dependence_stats.num_siv_independent++;
2045 return;
2048 else
2050 /* Example:
2051 chrec_a = 3
2052 chrec_b = {4, +, 1}
2054 In this case, chrec_a will not overlap with chrec_b. */
2055 *overlaps_a = conflict_fn_no_dependence ();
2056 *overlaps_b = conflict_fn_no_dependence ();
2057 *last_conflicts = integer_zero_node;
2058 dependence_stats.num_siv_independent++;
2059 return;
2066 /* Helper recursive function for initializing the matrix A. Returns
2067 the initial value of CHREC. */
2069 static tree
2070 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2072 gcc_assert (chrec);
2074 switch (TREE_CODE (chrec))
2076 case POLYNOMIAL_CHREC:
2077 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST);
2079 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2080 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2082 case PLUS_EXPR:
2083 case MULT_EXPR:
2084 case MINUS_EXPR:
2086 tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2087 tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
2089 return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
2092 case NOP_EXPR:
2094 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2095 return chrec_convert (chrec_type (chrec), op, NULL);
2098 case BIT_NOT_EXPR:
2100 /* Handle ~X as -1 - X. */
2101 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2102 return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
2103 build_int_cst (TREE_TYPE (chrec), -1), op);
2106 case INTEGER_CST:
2107 return chrec;
2109 default:
2110 gcc_unreachable ();
2111 return NULL_TREE;
2115 #define FLOOR_DIV(x,y) ((x) / (y))
2117 /* Solves the special case of the Diophantine equation:
2118 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2120 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2121 number of iterations that loops X and Y run. The overlaps will be
2122 constructed as evolutions in dimension DIM. */
2124 static void
2125 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2126 affine_fn *overlaps_a,
2127 affine_fn *overlaps_b,
2128 tree *last_conflicts, int dim)
2130 if (((step_a > 0 && step_b > 0)
2131 || (step_a < 0 && step_b < 0)))
2133 int step_overlaps_a, step_overlaps_b;
2134 int gcd_steps_a_b, last_conflict, tau2;
2136 gcd_steps_a_b = gcd (step_a, step_b);
2137 step_overlaps_a = step_b / gcd_steps_a_b;
2138 step_overlaps_b = step_a / gcd_steps_a_b;
2140 if (niter > 0)
2142 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2143 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2144 last_conflict = tau2;
2145 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2147 else
2148 *last_conflicts = chrec_dont_know;
2150 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
2151 build_int_cst (NULL_TREE,
2152 step_overlaps_a));
2153 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
2154 build_int_cst (NULL_TREE,
2155 step_overlaps_b));
2158 else
2160 *overlaps_a = affine_fn_cst (integer_zero_node);
2161 *overlaps_b = affine_fn_cst (integer_zero_node);
2162 *last_conflicts = integer_zero_node;
2166 /* Solves the special case of a Diophantine equation where CHREC_A is
2167 an affine bivariate function, and CHREC_B is an affine univariate
2168 function. For example,
2170 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2172 has the following overlapping functions:
2174 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2175 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2176 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2178 FORNOW: This is a specialized implementation for a case occurring in
2179 a common benchmark. Implement the general algorithm. */
2181 static void
2182 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2183 conflict_function **overlaps_a,
2184 conflict_function **overlaps_b,
2185 tree *last_conflicts)
2187 bool xz_p, yz_p, xyz_p;
2188 int step_x, step_y, step_z;
2189 HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
2190 affine_fn overlaps_a_xz, overlaps_b_xz;
2191 affine_fn overlaps_a_yz, overlaps_b_yz;
2192 affine_fn overlaps_a_xyz, overlaps_b_xyz;
2193 affine_fn ova1, ova2, ovb;
2194 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
2196 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2197 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2198 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2200 niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
2201 niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
2202 niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
2204 if (niter_x < 0 || niter_y < 0 || niter_z < 0)
2206 if (dump_file && (dump_flags & TDF_DETAILS))
2207 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
2209 *overlaps_a = conflict_fn_not_known ();
2210 *overlaps_b = conflict_fn_not_known ();
2211 *last_conflicts = chrec_dont_know;
2212 return;
2215 niter = MIN (niter_x, niter_z);
2216 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2217 &overlaps_a_xz,
2218 &overlaps_b_xz,
2219 &last_conflicts_xz, 1);
2220 niter = MIN (niter_y, niter_z);
2221 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2222 &overlaps_a_yz,
2223 &overlaps_b_yz,
2224 &last_conflicts_yz, 2);
2225 niter = MIN (niter_x, niter_z);
2226 niter = MIN (niter_y, niter);
2227 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2228 &overlaps_a_xyz,
2229 &overlaps_b_xyz,
2230 &last_conflicts_xyz, 3);
2232 xz_p = !integer_zerop (last_conflicts_xz);
2233 yz_p = !integer_zerop (last_conflicts_yz);
2234 xyz_p = !integer_zerop (last_conflicts_xyz);
2236 if (xz_p || yz_p || xyz_p)
2238 ova1 = affine_fn_cst (integer_zero_node);
2239 ova2 = affine_fn_cst (integer_zero_node);
2240 ovb = affine_fn_cst (integer_zero_node);
2241 if (xz_p)
2243 affine_fn t0 = ova1;
2244 affine_fn t2 = ovb;
2246 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
2247 ovb = affine_fn_plus (ovb, overlaps_b_xz);
2248 affine_fn_free (t0);
2249 affine_fn_free (t2);
2250 *last_conflicts = last_conflicts_xz;
2252 if (yz_p)
2254 affine_fn t0 = ova2;
2255 affine_fn t2 = ovb;
2257 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
2258 ovb = affine_fn_plus (ovb, overlaps_b_yz);
2259 affine_fn_free (t0);
2260 affine_fn_free (t2);
2261 *last_conflicts = last_conflicts_yz;
2263 if (xyz_p)
2265 affine_fn t0 = ova1;
2266 affine_fn t2 = ova2;
2267 affine_fn t4 = ovb;
2269 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
2270 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
2271 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
2272 affine_fn_free (t0);
2273 affine_fn_free (t2);
2274 affine_fn_free (t4);
2275 *last_conflicts = last_conflicts_xyz;
2277 *overlaps_a = conflict_fn (2, ova1, ova2);
2278 *overlaps_b = conflict_fn (1, ovb);
2280 else
2282 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2283 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2284 *last_conflicts = integer_zero_node;
2287 affine_fn_free (overlaps_a_xz);
2288 affine_fn_free (overlaps_b_xz);
2289 affine_fn_free (overlaps_a_yz);
2290 affine_fn_free (overlaps_b_yz);
2291 affine_fn_free (overlaps_a_xyz);
2292 affine_fn_free (overlaps_b_xyz);
2295 /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2297 static void
2298 lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
2299 int size)
2301 memcpy (vec2, vec1, size * sizeof (*vec1));
2304 /* Copy the elements of M x N matrix MAT1 to MAT2. */
2306 static void
2307 lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
2308 int m, int n)
2310 int i;
2312 for (i = 0; i < m; i++)
2313 lambda_vector_copy (mat1[i], mat2[i], n);
2316 /* Store the N x N identity matrix in MAT. */
2318 static void
2319 lambda_matrix_id (lambda_matrix mat, int size)
2321 int i, j;
2323 for (i = 0; i < size; i++)
2324 for (j = 0; j < size; j++)
2325 mat[i][j] = (i == j) ? 1 : 0;
2328 /* Return the first nonzero element of vector VEC1 between START and N.
2329 We must have START <= N. Returns N if VEC1 is the zero vector. */
2331 static int
2332 lambda_vector_first_nz (lambda_vector vec1, int n, int start)
2334 int j = start;
2335 while (j < n && vec1[j] == 0)
2336 j++;
2337 return j;
2340 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2341 R2 = R2 + CONST1 * R1. */
2343 static void
2344 lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2, int const1)
2346 int i;
2348 if (const1 == 0)
2349 return;
2351 for (i = 0; i < n; i++)
2352 mat[r2][i] += const1 * mat[r1][i];
2355 /* Swap rows R1 and R2 in matrix MAT. */
2357 static void
2358 lambda_matrix_row_exchange (lambda_matrix mat, int r1, int r2)
2360 lambda_vector row;
2362 row = mat[r1];
2363 mat[r1] = mat[r2];
2364 mat[r2] = row;
2367 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
2368 and store the result in VEC2. */
2370 static void
2371 lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
2372 int size, int const1)
2374 int i;
2376 if (const1 == 0)
2377 lambda_vector_clear (vec2, size);
2378 else
2379 for (i = 0; i < size; i++)
2380 vec2[i] = const1 * vec1[i];
2383 /* Negate vector VEC1 with length SIZE and store it in VEC2. */
2385 static void
2386 lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
2387 int size)
2389 lambda_vector_mult_const (vec1, vec2, size, -1);
2392 /* Negate row R1 of matrix MAT which has N columns. */
2394 static void
2395 lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
2397 lambda_vector_negate (mat[r1], mat[r1], n);
2400 /* Return true if two vectors are equal. */
2402 static bool
2403 lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
2405 int i;
2406 for (i = 0; i < size; i++)
2407 if (vec1[i] != vec2[i])
2408 return false;
2409 return true;
2412 /* Given an M x N integer matrix A, this function determines an M x
2413 M unimodular matrix U, and an M x N echelon matrix S such that
2414 "U.A = S". This decomposition is also known as "right Hermite".
2416 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2417 Restructuring Compilers" Utpal Banerjee. */
2419 static void
2420 lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
2421 lambda_matrix S, lambda_matrix U)
2423 int i, j, i0 = 0;
2425 lambda_matrix_copy (A, S, m, n);
2426 lambda_matrix_id (U, m);
2428 for (j = 0; j < n; j++)
2430 if (lambda_vector_first_nz (S[j], m, i0) < m)
2432 ++i0;
2433 for (i = m - 1; i >= i0; i--)
2435 while (S[i][j] != 0)
2437 int sigma, factor, a, b;
2439 a = S[i-1][j];
2440 b = S[i][j];
2441 sigma = (a * b < 0) ? -1: 1;
2442 a = abs (a);
2443 b = abs (b);
2444 factor = sigma * (a / b);
2446 lambda_matrix_row_add (S, n, i, i-1, -factor);
2447 lambda_matrix_row_exchange (S, i, i-1);
2449 lambda_matrix_row_add (U, m, i, i-1, -factor);
2450 lambda_matrix_row_exchange (U, i, i-1);
2457 /* Determines the overlapping elements due to accesses CHREC_A and
2458 CHREC_B, that are affine functions. This function cannot handle
2459 symbolic evolution functions, ie. when initial conditions are
2460 parameters, because it uses lambda matrices of integers. */
2462 static void
2463 analyze_subscript_affine_affine (tree chrec_a,
2464 tree chrec_b,
2465 conflict_function **overlaps_a,
2466 conflict_function **overlaps_b,
2467 tree *last_conflicts)
2469 unsigned nb_vars_a, nb_vars_b, dim;
2470 HOST_WIDE_INT init_a, init_b, gamma, gcd_alpha_beta;
2471 lambda_matrix A, U, S;
2472 struct obstack scratch_obstack;
2474 if (eq_evolutions_p (chrec_a, chrec_b))
2476 /* The accessed index overlaps for each iteration in the
2477 loop. */
2478 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2479 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2480 *last_conflicts = chrec_dont_know;
2481 return;
2483 if (dump_file && (dump_flags & TDF_DETAILS))
2484 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2486 /* For determining the initial intersection, we have to solve a
2487 Diophantine equation. This is the most time consuming part.
2489 For answering to the question: "Is there a dependence?" we have
2490 to prove that there exists a solution to the Diophantine
2491 equation, and that the solution is in the iteration domain,
2492 i.e. the solution is positive or zero, and that the solution
2493 happens before the upper bound loop.nb_iterations. Otherwise
2494 there is no dependence. This function outputs a description of
2495 the iterations that hold the intersections. */
2497 nb_vars_a = nb_vars_in_chrec (chrec_a);
2498 nb_vars_b = nb_vars_in_chrec (chrec_b);
2500 gcc_obstack_init (&scratch_obstack);
2502 dim = nb_vars_a + nb_vars_b;
2503 U = lambda_matrix_new (dim, dim, &scratch_obstack);
2504 A = lambda_matrix_new (dim, 1, &scratch_obstack);
2505 S = lambda_matrix_new (dim, 1, &scratch_obstack);
2507 init_a = int_cst_value (initialize_matrix_A (A, chrec_a, 0, 1));
2508 init_b = int_cst_value (initialize_matrix_A (A, chrec_b, nb_vars_a, -1));
2509 gamma = init_b - init_a;
2511 /* Don't do all the hard work of solving the Diophantine equation
2512 when we already know the solution: for example,
2513 | {3, +, 1}_1
2514 | {3, +, 4}_2
2515 | gamma = 3 - 3 = 0.
2516 Then the first overlap occurs during the first iterations:
2517 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2519 if (gamma == 0)
2521 if (nb_vars_a == 1 && nb_vars_b == 1)
2523 HOST_WIDE_INT step_a, step_b;
2524 HOST_WIDE_INT niter, niter_a, niter_b;
2525 affine_fn ova, ovb;
2527 niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
2528 niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
2529 niter = MIN (niter_a, niter_b);
2530 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2531 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2533 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2534 &ova, &ovb,
2535 last_conflicts, 1);
2536 *overlaps_a = conflict_fn (1, ova);
2537 *overlaps_b = conflict_fn (1, ovb);
2540 else if (nb_vars_a == 2 && nb_vars_b == 1)
2541 compute_overlap_steps_for_affine_1_2
2542 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2544 else if (nb_vars_a == 1 && nb_vars_b == 2)
2545 compute_overlap_steps_for_affine_1_2
2546 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2548 else
2550 if (dump_file && (dump_flags & TDF_DETAILS))
2551 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
2552 *overlaps_a = conflict_fn_not_known ();
2553 *overlaps_b = conflict_fn_not_known ();
2554 *last_conflicts = chrec_dont_know;
2556 goto end_analyze_subs_aa;
2559 /* U.A = S */
2560 lambda_matrix_right_hermite (A, dim, 1, S, U);
2562 if (S[0][0] < 0)
2564 S[0][0] *= -1;
2565 lambda_matrix_row_negate (U, dim, 0);
2567 gcd_alpha_beta = S[0][0];
2569 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2570 but that is a quite strange case. Instead of ICEing, answer
2571 don't know. */
2572 if (gcd_alpha_beta == 0)
2574 *overlaps_a = conflict_fn_not_known ();
2575 *overlaps_b = conflict_fn_not_known ();
2576 *last_conflicts = chrec_dont_know;
2577 goto end_analyze_subs_aa;
2580 /* The classic "gcd-test". */
2581 if (!int_divides_p (gcd_alpha_beta, gamma))
2583 /* The "gcd-test" has determined that there is no integer
2584 solution, i.e. there is no dependence. */
2585 *overlaps_a = conflict_fn_no_dependence ();
2586 *overlaps_b = conflict_fn_no_dependence ();
2587 *last_conflicts = integer_zero_node;
2590 /* Both access functions are univariate. This includes SIV and MIV cases. */
2591 else if (nb_vars_a == 1 && nb_vars_b == 1)
2593 /* Both functions should have the same evolution sign. */
2594 if (((A[0][0] > 0 && -A[1][0] > 0)
2595 || (A[0][0] < 0 && -A[1][0] < 0)))
2597 /* The solutions are given by:
2599 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2600 | [u21 u22] [y0]
2602 For a given integer t. Using the following variables,
2604 | i0 = u11 * gamma / gcd_alpha_beta
2605 | j0 = u12 * gamma / gcd_alpha_beta
2606 | i1 = u21
2607 | j1 = u22
2609 the solutions are:
2611 | x0 = i0 + i1 * t,
2612 | y0 = j0 + j1 * t. */
2613 HOST_WIDE_INT i0, j0, i1, j1;
2615 i0 = U[0][0] * gamma / gcd_alpha_beta;
2616 j0 = U[0][1] * gamma / gcd_alpha_beta;
2617 i1 = U[1][0];
2618 j1 = U[1][1];
2620 if ((i1 == 0 && i0 < 0)
2621 || (j1 == 0 && j0 < 0))
2623 /* There is no solution.
2624 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2625 falls in here, but for the moment we don't look at the
2626 upper bound of the iteration domain. */
2627 *overlaps_a = conflict_fn_no_dependence ();
2628 *overlaps_b = conflict_fn_no_dependence ();
2629 *last_conflicts = integer_zero_node;
2630 goto end_analyze_subs_aa;
2633 if (i1 > 0 && j1 > 0)
2635 HOST_WIDE_INT niter_a
2636 = max_stmt_executions_int (get_chrec_loop (chrec_a));
2637 HOST_WIDE_INT niter_b
2638 = max_stmt_executions_int (get_chrec_loop (chrec_b));
2639 HOST_WIDE_INT niter = MIN (niter_a, niter_b);
2641 /* (X0, Y0) is a solution of the Diophantine equation:
2642 "chrec_a (X0) = chrec_b (Y0)". */
2643 HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
2644 CEIL (-j0, j1));
2645 HOST_WIDE_INT x0 = i1 * tau1 + i0;
2646 HOST_WIDE_INT y0 = j1 * tau1 + j0;
2648 /* (X1, Y1) is the smallest positive solution of the eq
2649 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2650 first conflict occurs. */
2651 HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
2652 HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
2653 HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
2655 if (niter > 0)
2657 HOST_WIDE_INT tau2 = MIN (FLOOR_DIV (niter - i0, i1),
2658 FLOOR_DIV (niter - j0, j1));
2659 HOST_WIDE_INT last_conflict = tau2 - (x1 - i0)/i1;
2661 /* If the overlap occurs outside of the bounds of the
2662 loop, there is no dependence. */
2663 if (x1 >= niter || y1 >= niter)
2665 *overlaps_a = conflict_fn_no_dependence ();
2666 *overlaps_b = conflict_fn_no_dependence ();
2667 *last_conflicts = integer_zero_node;
2668 goto end_analyze_subs_aa;
2670 else
2671 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2673 else
2674 *last_conflicts = chrec_dont_know;
2676 *overlaps_a
2677 = conflict_fn (1,
2678 affine_fn_univar (build_int_cst (NULL_TREE, x1),
2680 build_int_cst (NULL_TREE, i1)));
2681 *overlaps_b
2682 = conflict_fn (1,
2683 affine_fn_univar (build_int_cst (NULL_TREE, y1),
2685 build_int_cst (NULL_TREE, j1)));
2687 else
2689 /* FIXME: For the moment, the upper bound of the
2690 iteration domain for i and j is not checked. */
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 else
2700 if (dump_file && (dump_flags & TDF_DETAILS))
2701 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2702 *overlaps_a = conflict_fn_not_known ();
2703 *overlaps_b = conflict_fn_not_known ();
2704 *last_conflicts = chrec_dont_know;
2707 else
2709 if (dump_file && (dump_flags & TDF_DETAILS))
2710 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2711 *overlaps_a = conflict_fn_not_known ();
2712 *overlaps_b = conflict_fn_not_known ();
2713 *last_conflicts = chrec_dont_know;
2716 end_analyze_subs_aa:
2717 obstack_free (&scratch_obstack, NULL);
2718 if (dump_file && (dump_flags & TDF_DETAILS))
2720 fprintf (dump_file, " (overlaps_a = ");
2721 dump_conflict_function (dump_file, *overlaps_a);
2722 fprintf (dump_file, ")\n (overlaps_b = ");
2723 dump_conflict_function (dump_file, *overlaps_b);
2724 fprintf (dump_file, "))\n");
2728 /* Returns true when analyze_subscript_affine_affine can be used for
2729 determining the dependence relation between chrec_a and chrec_b,
2730 that contain symbols. This function modifies chrec_a and chrec_b
2731 such that the analysis result is the same, and such that they don't
2732 contain symbols, and then can safely be passed to the analyzer.
2734 Example: The analysis of the following tuples of evolutions produce
2735 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2736 vs. {0, +, 1}_1
2738 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2739 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2742 static bool
2743 can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
2745 tree diff, type, left_a, left_b, right_b;
2747 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
2748 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
2749 /* FIXME: For the moment not handled. Might be refined later. */
2750 return false;
2752 type = chrec_type (*chrec_a);
2753 left_a = CHREC_LEFT (*chrec_a);
2754 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
2755 diff = chrec_fold_minus (type, left_a, left_b);
2757 if (!evolution_function_is_constant_p (diff))
2758 return false;
2760 if (dump_file && (dump_flags & TDF_DETAILS))
2761 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
2763 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
2764 diff, CHREC_RIGHT (*chrec_a));
2765 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
2766 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
2767 build_int_cst (type, 0),
2768 right_b);
2769 return true;
2772 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2773 *OVERLAPS_B are initialized to the functions that describe the
2774 relation between the elements accessed twice by CHREC_A and
2775 CHREC_B. For k >= 0, the following property is verified:
2777 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2779 static void
2780 analyze_siv_subscript (tree chrec_a,
2781 tree chrec_b,
2782 conflict_function **overlaps_a,
2783 conflict_function **overlaps_b,
2784 tree *last_conflicts,
2785 int loop_nest_num)
2787 dependence_stats.num_siv++;
2789 if (dump_file && (dump_flags & TDF_DETAILS))
2790 fprintf (dump_file, "(analyze_siv_subscript \n");
2792 if (evolution_function_is_constant_p (chrec_a)
2793 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2794 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
2795 overlaps_a, overlaps_b, last_conflicts);
2797 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2798 && evolution_function_is_constant_p (chrec_b))
2799 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
2800 overlaps_b, overlaps_a, last_conflicts);
2802 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2803 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2805 if (!chrec_contains_symbols (chrec_a)
2806 && !chrec_contains_symbols (chrec_b))
2808 analyze_subscript_affine_affine (chrec_a, chrec_b,
2809 overlaps_a, overlaps_b,
2810 last_conflicts);
2812 if (CF_NOT_KNOWN_P (*overlaps_a)
2813 || CF_NOT_KNOWN_P (*overlaps_b))
2814 dependence_stats.num_siv_unimplemented++;
2815 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2816 || CF_NO_DEPENDENCE_P (*overlaps_b))
2817 dependence_stats.num_siv_independent++;
2818 else
2819 dependence_stats.num_siv_dependent++;
2821 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
2822 &chrec_b))
2824 analyze_subscript_affine_affine (chrec_a, chrec_b,
2825 overlaps_a, overlaps_b,
2826 last_conflicts);
2828 if (CF_NOT_KNOWN_P (*overlaps_a)
2829 || CF_NOT_KNOWN_P (*overlaps_b))
2830 dependence_stats.num_siv_unimplemented++;
2831 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2832 || CF_NO_DEPENDENCE_P (*overlaps_b))
2833 dependence_stats.num_siv_independent++;
2834 else
2835 dependence_stats.num_siv_dependent++;
2837 else
2838 goto siv_subscript_dontknow;
2841 else
2843 siv_subscript_dontknow:;
2844 if (dump_file && (dump_flags & TDF_DETAILS))
2845 fprintf (dump_file, " siv test failed: unimplemented");
2846 *overlaps_a = conflict_fn_not_known ();
2847 *overlaps_b = conflict_fn_not_known ();
2848 *last_conflicts = chrec_dont_know;
2849 dependence_stats.num_siv_unimplemented++;
2852 if (dump_file && (dump_flags & TDF_DETAILS))
2853 fprintf (dump_file, ")\n");
2856 /* Returns false if we can prove that the greatest common divisor of the steps
2857 of CHREC does not divide CST, false otherwise. */
2859 static bool
2860 gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
2862 HOST_WIDE_INT cd = 0, val;
2863 tree step;
2865 if (!tree_fits_shwi_p (cst))
2866 return true;
2867 val = tree_to_shwi (cst);
2869 while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
2871 step = CHREC_RIGHT (chrec);
2872 if (!tree_fits_shwi_p (step))
2873 return true;
2874 cd = gcd (cd, tree_to_shwi (step));
2875 chrec = CHREC_LEFT (chrec);
2878 return val % cd == 0;
2881 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
2882 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2883 functions that describe the relation between the elements accessed
2884 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2885 is verified:
2887 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2889 static void
2890 analyze_miv_subscript (tree chrec_a,
2891 tree chrec_b,
2892 conflict_function **overlaps_a,
2893 conflict_function **overlaps_b,
2894 tree *last_conflicts,
2895 struct loop *loop_nest)
2897 tree type, difference;
2899 dependence_stats.num_miv++;
2900 if (dump_file && (dump_flags & TDF_DETAILS))
2901 fprintf (dump_file, "(analyze_miv_subscript \n");
2903 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
2904 chrec_a = chrec_convert (type, chrec_a, NULL);
2905 chrec_b = chrec_convert (type, chrec_b, NULL);
2906 difference = chrec_fold_minus (type, chrec_a, chrec_b);
2908 if (eq_evolutions_p (chrec_a, chrec_b))
2910 /* Access functions are the same: all the elements are accessed
2911 in the same order. */
2912 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2913 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2914 *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
2915 dependence_stats.num_miv_dependent++;
2918 else if (evolution_function_is_constant_p (difference)
2919 /* For the moment, the following is verified:
2920 evolution_function_is_affine_multivariate_p (chrec_a,
2921 loop_nest->num) */
2922 && !gcd_of_steps_may_divide_p (chrec_a, difference))
2924 /* testsuite/.../ssa-chrec-33.c
2925 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2927 The difference is 1, and all the evolution steps are multiples
2928 of 2, consequently there are no overlapping elements. */
2929 *overlaps_a = conflict_fn_no_dependence ();
2930 *overlaps_b = conflict_fn_no_dependence ();
2931 *last_conflicts = integer_zero_node;
2932 dependence_stats.num_miv_independent++;
2935 else if (evolution_function_is_affine_multivariate_p (chrec_a, loop_nest->num)
2936 && !chrec_contains_symbols (chrec_a)
2937 && evolution_function_is_affine_multivariate_p (chrec_b, loop_nest->num)
2938 && !chrec_contains_symbols (chrec_b))
2940 /* testsuite/.../ssa-chrec-35.c
2941 {0, +, 1}_2 vs. {0, +, 1}_3
2942 the overlapping elements are respectively located at iterations:
2943 {0, +, 1}_x and {0, +, 1}_x,
2944 in other words, we have the equality:
2945 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2947 Other examples:
2948 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2949 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2951 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2952 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2954 analyze_subscript_affine_affine (chrec_a, chrec_b,
2955 overlaps_a, overlaps_b, last_conflicts);
2957 if (CF_NOT_KNOWN_P (*overlaps_a)
2958 || CF_NOT_KNOWN_P (*overlaps_b))
2959 dependence_stats.num_miv_unimplemented++;
2960 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2961 || CF_NO_DEPENDENCE_P (*overlaps_b))
2962 dependence_stats.num_miv_independent++;
2963 else
2964 dependence_stats.num_miv_dependent++;
2967 else
2969 /* When the analysis is too difficult, answer "don't know". */
2970 if (dump_file && (dump_flags & TDF_DETAILS))
2971 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
2973 *overlaps_a = conflict_fn_not_known ();
2974 *overlaps_b = conflict_fn_not_known ();
2975 *last_conflicts = chrec_dont_know;
2976 dependence_stats.num_miv_unimplemented++;
2979 if (dump_file && (dump_flags & TDF_DETAILS))
2980 fprintf (dump_file, ")\n");
2983 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
2984 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
2985 OVERLAP_ITERATIONS_B are initialized with two functions that
2986 describe the iterations that contain conflicting elements.
2988 Remark: For an integer k >= 0, the following equality is true:
2990 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
2993 static void
2994 analyze_overlapping_iterations (tree chrec_a,
2995 tree chrec_b,
2996 conflict_function **overlap_iterations_a,
2997 conflict_function **overlap_iterations_b,
2998 tree *last_conflicts, struct loop *loop_nest)
3000 unsigned int lnn = loop_nest->num;
3002 dependence_stats.num_subscript_tests++;
3004 if (dump_file && (dump_flags & TDF_DETAILS))
3006 fprintf (dump_file, "(analyze_overlapping_iterations \n");
3007 fprintf (dump_file, " (chrec_a = ");
3008 print_generic_expr (dump_file, chrec_a, 0);
3009 fprintf (dump_file, ")\n (chrec_b = ");
3010 print_generic_expr (dump_file, chrec_b, 0);
3011 fprintf (dump_file, ")\n");
3014 if (chrec_a == NULL_TREE
3015 || chrec_b == NULL_TREE
3016 || chrec_contains_undetermined (chrec_a)
3017 || chrec_contains_undetermined (chrec_b))
3019 dependence_stats.num_subscript_undetermined++;
3021 *overlap_iterations_a = conflict_fn_not_known ();
3022 *overlap_iterations_b = conflict_fn_not_known ();
3025 /* If they are the same chrec, and are affine, they overlap
3026 on every iteration. */
3027 else if (eq_evolutions_p (chrec_a, chrec_b)
3028 && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3029 || operand_equal_p (chrec_a, chrec_b, 0)))
3031 dependence_stats.num_same_subscript_function++;
3032 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3033 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3034 *last_conflicts = chrec_dont_know;
3037 /* If they aren't the same, and aren't affine, we can't do anything
3038 yet. */
3039 else if ((chrec_contains_symbols (chrec_a)
3040 || chrec_contains_symbols (chrec_b))
3041 && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3042 || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
3044 dependence_stats.num_subscript_undetermined++;
3045 *overlap_iterations_a = conflict_fn_not_known ();
3046 *overlap_iterations_b = conflict_fn_not_known ();
3049 else if (ziv_subscript_p (chrec_a, chrec_b))
3050 analyze_ziv_subscript (chrec_a, chrec_b,
3051 overlap_iterations_a, overlap_iterations_b,
3052 last_conflicts);
3054 else if (siv_subscript_p (chrec_a, chrec_b))
3055 analyze_siv_subscript (chrec_a, chrec_b,
3056 overlap_iterations_a, overlap_iterations_b,
3057 last_conflicts, lnn);
3059 else
3060 analyze_miv_subscript (chrec_a, chrec_b,
3061 overlap_iterations_a, overlap_iterations_b,
3062 last_conflicts, loop_nest);
3064 if (dump_file && (dump_flags & TDF_DETAILS))
3066 fprintf (dump_file, " (overlap_iterations_a = ");
3067 dump_conflict_function (dump_file, *overlap_iterations_a);
3068 fprintf (dump_file, ")\n (overlap_iterations_b = ");
3069 dump_conflict_function (dump_file, *overlap_iterations_b);
3070 fprintf (dump_file, "))\n");
3074 /* Helper function for uniquely inserting distance vectors. */
3076 static void
3077 save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
3079 unsigned i;
3080 lambda_vector v;
3082 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, v)
3083 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
3084 return;
3086 DDR_DIST_VECTS (ddr).safe_push (dist_v);
3089 /* Helper function for uniquely inserting direction vectors. */
3091 static void
3092 save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
3094 unsigned i;
3095 lambda_vector v;
3097 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), i, v)
3098 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
3099 return;
3101 DDR_DIR_VECTS (ddr).safe_push (dir_v);
3104 /* Add a distance of 1 on all the loops outer than INDEX. If we
3105 haven't yet determined a distance for this outer loop, push a new
3106 distance vector composed of the previous distance, and a distance
3107 of 1 for this outer loop. Example:
3109 | loop_1
3110 | loop_2
3111 | A[10]
3112 | endloop_2
3113 | endloop_1
3115 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3116 save (0, 1), then we have to save (1, 0). */
3118 static void
3119 add_outer_distances (struct data_dependence_relation *ddr,
3120 lambda_vector dist_v, int index)
3122 /* For each outer loop where init_v is not set, the accesses are
3123 in dependence of distance 1 in the loop. */
3124 while (--index >= 0)
3126 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3127 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3128 save_v[index] = 1;
3129 save_dist_v (ddr, save_v);
3133 /* Return false when fail to represent the data dependence as a
3134 distance vector. INIT_B is set to true when a component has been
3135 added to the distance vector DIST_V. INDEX_CARRY is then set to
3136 the index in DIST_V that carries the dependence. */
3138 static bool
3139 build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
3140 struct data_reference *ddr_a,
3141 struct data_reference *ddr_b,
3142 lambda_vector dist_v, bool *init_b,
3143 int *index_carry)
3145 unsigned i;
3146 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3148 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3150 tree access_fn_a, access_fn_b;
3151 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3153 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3155 non_affine_dependence_relation (ddr);
3156 return false;
3159 access_fn_a = DR_ACCESS_FN (ddr_a, i);
3160 access_fn_b = DR_ACCESS_FN (ddr_b, i);
3162 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3163 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3165 int dist, index;
3166 int var_a = CHREC_VARIABLE (access_fn_a);
3167 int var_b = CHREC_VARIABLE (access_fn_b);
3169 if (var_a != var_b
3170 || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3172 non_affine_dependence_relation (ddr);
3173 return false;
3176 dist = int_cst_value (SUB_DISTANCE (subscript));
3177 index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
3178 *index_carry = MIN (index, *index_carry);
3180 /* This is the subscript coupling test. If we have already
3181 recorded a distance for this loop (a distance coming from
3182 another subscript), it should be the same. For example,
3183 in the following code, there is no dependence:
3185 | loop i = 0, N, 1
3186 | T[i+1][i] = ...
3187 | ... = T[i][i]
3188 | endloop
3190 if (init_v[index] != 0 && dist_v[index] != dist)
3192 finalize_ddr_dependent (ddr, chrec_known);
3193 return false;
3196 dist_v[index] = dist;
3197 init_v[index] = 1;
3198 *init_b = true;
3200 else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
3202 /* This can be for example an affine vs. constant dependence
3203 (T[i] vs. T[3]) that is not an affine dependence and is
3204 not representable as a distance vector. */
3205 non_affine_dependence_relation (ddr);
3206 return false;
3210 return true;
3213 /* Return true when the DDR contains only constant access functions. */
3215 static bool
3216 constant_access_functions (const struct data_dependence_relation *ddr)
3218 unsigned i;
3220 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3221 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr), i))
3222 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr), i)))
3223 return false;
3225 return true;
3228 /* Helper function for the case where DDR_A and DDR_B are the same
3229 multivariate access function with a constant step. For an example
3230 see pr34635-1.c. */
3232 static void
3233 add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
3235 int x_1, x_2;
3236 tree c_1 = CHREC_LEFT (c_2);
3237 tree c_0 = CHREC_LEFT (c_1);
3238 lambda_vector dist_v;
3239 int v1, v2, cd;
3241 /* Polynomials with more than 2 variables are not handled yet. When
3242 the evolution steps are parameters, it is not possible to
3243 represent the dependence using classical distance vectors. */
3244 if (TREE_CODE (c_0) != INTEGER_CST
3245 || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
3246 || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
3248 DDR_AFFINE_P (ddr) = false;
3249 return;
3252 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
3253 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
3255 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3256 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3257 v1 = int_cst_value (CHREC_RIGHT (c_1));
3258 v2 = int_cst_value (CHREC_RIGHT (c_2));
3259 cd = gcd (v1, v2);
3260 v1 /= cd;
3261 v2 /= cd;
3263 if (v2 < 0)
3265 v2 = -v2;
3266 v1 = -v1;
3269 dist_v[x_1] = v2;
3270 dist_v[x_2] = -v1;
3271 save_dist_v (ddr, dist_v);
3273 add_outer_distances (ddr, dist_v, x_1);
3276 /* Helper function for the case where DDR_A and DDR_B are the same
3277 access functions. */
3279 static void
3280 add_other_self_distances (struct data_dependence_relation *ddr)
3282 lambda_vector dist_v;
3283 unsigned i;
3284 int index_carry = DDR_NB_LOOPS (ddr);
3286 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3288 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
3290 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
3292 if (!evolution_function_is_univariate_p (access_fun))
3294 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
3296 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3297 return;
3300 access_fun = DR_ACCESS_FN (DDR_A (ddr), 0);
3302 if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
3303 add_multivariate_self_dist (ddr, access_fun);
3304 else
3305 /* The evolution step is not constant: it varies in
3306 the outer loop, so this cannot be represented by a
3307 distance vector. For example in pr34635.c the
3308 evolution is {0, +, {0, +, 4}_1}_2. */
3309 DDR_AFFINE_P (ddr) = false;
3311 return;
3314 index_carry = MIN (index_carry,
3315 index_in_loop_nest (CHREC_VARIABLE (access_fun),
3316 DDR_LOOP_NEST (ddr)));
3320 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3321 add_outer_distances (ddr, dist_v, index_carry);
3324 static void
3325 insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
3327 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3329 dist_v[DDR_INNER_LOOP (ddr)] = 1;
3330 save_dist_v (ddr, dist_v);
3333 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
3334 is the case for example when access functions are the same and
3335 equal to a constant, as in:
3337 | loop_1
3338 | A[3] = ...
3339 | ... = A[3]
3340 | endloop_1
3342 in which case the distance vectors are (0) and (1). */
3344 static void
3345 add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
3347 unsigned i, j;
3349 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3351 subscript_p sub = DDR_SUBSCRIPT (ddr, i);
3352 conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
3353 conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
3355 for (j = 0; j < ca->n; j++)
3356 if (affine_function_zero_p (ca->fns[j]))
3358 insert_innermost_unit_dist_vector (ddr);
3359 return;
3362 for (j = 0; j < cb->n; j++)
3363 if (affine_function_zero_p (cb->fns[j]))
3365 insert_innermost_unit_dist_vector (ddr);
3366 return;
3371 /* Compute the classic per loop distance vector. DDR is the data
3372 dependence relation to build a vector from. Return false when fail
3373 to represent the data dependence as a distance vector. */
3375 static bool
3376 build_classic_dist_vector (struct data_dependence_relation *ddr,
3377 struct loop *loop_nest)
3379 bool init_b = false;
3380 int index_carry = DDR_NB_LOOPS (ddr);
3381 lambda_vector dist_v;
3383 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3384 return false;
3386 if (same_access_functions (ddr))
3388 /* Save the 0 vector. */
3389 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3390 save_dist_v (ddr, dist_v);
3392 if (constant_access_functions (ddr))
3393 add_distance_for_zero_overlaps (ddr);
3395 if (DDR_NB_LOOPS (ddr) > 1)
3396 add_other_self_distances (ddr);
3398 return true;
3401 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3402 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3403 dist_v, &init_b, &index_carry))
3404 return false;
3406 /* Save the distance vector if we initialized one. */
3407 if (init_b)
3409 /* Verify a basic constraint: classic distance vectors should
3410 always be lexicographically positive.
3412 Data references are collected in the order of execution of
3413 the program, thus for the following loop
3415 | for (i = 1; i < 100; i++)
3416 | for (j = 1; j < 100; j++)
3418 | t = T[j+1][i-1]; // A
3419 | T[j][i] = t + 2; // B
3422 references are collected following the direction of the wind:
3423 A then B. The data dependence tests are performed also
3424 following this order, such that we're looking at the distance
3425 separating the elements accessed by A from the elements later
3426 accessed by B. But in this example, the distance returned by
3427 test_dep (A, B) is lexicographically negative (-1, 1), that
3428 means that the access A occurs later than B with respect to
3429 the outer loop, ie. we're actually looking upwind. In this
3430 case we solve test_dep (B, A) looking downwind to the
3431 lexicographically positive solution, that returns the
3432 distance vector (1, -1). */
3433 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3435 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3436 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3437 loop_nest))
3438 return false;
3439 compute_subscript_distance (ddr);
3440 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3441 save_v, &init_b, &index_carry))
3442 return false;
3443 save_dist_v (ddr, save_v);
3444 DDR_REVERSED_P (ddr) = true;
3446 /* In this case there is a dependence forward for all the
3447 outer loops:
3449 | for (k = 1; k < 100; k++)
3450 | for (i = 1; i < 100; i++)
3451 | for (j = 1; j < 100; j++)
3453 | t = T[j+1][i-1]; // A
3454 | T[j][i] = t + 2; // B
3457 the vectors are:
3458 (0, 1, -1)
3459 (1, 1, -1)
3460 (1, -1, 1)
3462 if (DDR_NB_LOOPS (ddr) > 1)
3464 add_outer_distances (ddr, save_v, index_carry);
3465 add_outer_distances (ddr, dist_v, index_carry);
3468 else
3470 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3471 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3473 if (DDR_NB_LOOPS (ddr) > 1)
3475 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3477 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr),
3478 DDR_A (ddr), loop_nest))
3479 return false;
3480 compute_subscript_distance (ddr);
3481 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3482 opposite_v, &init_b,
3483 &index_carry))
3484 return false;
3486 save_dist_v (ddr, save_v);
3487 add_outer_distances (ddr, dist_v, index_carry);
3488 add_outer_distances (ddr, opposite_v, index_carry);
3490 else
3491 save_dist_v (ddr, save_v);
3494 else
3496 /* There is a distance of 1 on all the outer loops: Example:
3497 there is a dependence of distance 1 on loop_1 for the array A.
3499 | loop_1
3500 | A[5] = ...
3501 | endloop
3503 add_outer_distances (ddr, dist_v,
3504 lambda_vector_first_nz (dist_v,
3505 DDR_NB_LOOPS (ddr), 0));
3508 if (dump_file && (dump_flags & TDF_DETAILS))
3510 unsigned i;
3512 fprintf (dump_file, "(build_classic_dist_vector\n");
3513 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3515 fprintf (dump_file, " dist_vector = (");
3516 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3517 DDR_NB_LOOPS (ddr));
3518 fprintf (dump_file, " )\n");
3520 fprintf (dump_file, ")\n");
3523 return true;
3526 /* Return the direction for a given distance.
3527 FIXME: Computing dir this way is suboptimal, since dir can catch
3528 cases that dist is unable to represent. */
3530 static inline enum data_dependence_direction
3531 dir_from_dist (int dist)
3533 if (dist > 0)
3534 return dir_positive;
3535 else if (dist < 0)
3536 return dir_negative;
3537 else
3538 return dir_equal;
3541 /* Compute the classic per loop direction vector. DDR is the data
3542 dependence relation to build a vector from. */
3544 static void
3545 build_classic_dir_vector (struct data_dependence_relation *ddr)
3547 unsigned i, j;
3548 lambda_vector dist_v;
3550 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
3552 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3554 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3555 dir_v[j] = dir_from_dist (dist_v[j]);
3557 save_dir_v (ddr, dir_v);
3561 /* Helper function. Returns true when there is a dependence between
3562 data references DRA and DRB. */
3564 static bool
3565 subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
3566 struct data_reference *dra,
3567 struct data_reference *drb,
3568 struct loop *loop_nest)
3570 unsigned int i;
3571 tree last_conflicts;
3572 struct subscript *subscript;
3573 tree res = NULL_TREE;
3575 for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
3577 conflict_function *overlaps_a, *overlaps_b;
3579 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3580 DR_ACCESS_FN (drb, i),
3581 &overlaps_a, &overlaps_b,
3582 &last_conflicts, loop_nest);
3584 if (SUB_CONFLICTS_IN_A (subscript))
3585 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
3586 if (SUB_CONFLICTS_IN_B (subscript))
3587 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
3589 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3590 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3591 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3593 /* If there is any undetermined conflict function we have to
3594 give a conservative answer in case we cannot prove that
3595 no dependence exists when analyzing another subscript. */
3596 if (CF_NOT_KNOWN_P (overlaps_a)
3597 || CF_NOT_KNOWN_P (overlaps_b))
3599 res = chrec_dont_know;
3600 continue;
3603 /* When there is a subscript with no dependence we can stop. */
3604 else if (CF_NO_DEPENDENCE_P (overlaps_a)
3605 || CF_NO_DEPENDENCE_P (overlaps_b))
3607 res = chrec_known;
3608 break;
3612 if (res == NULL_TREE)
3613 return true;
3615 if (res == chrec_known)
3616 dependence_stats.num_dependence_independent++;
3617 else
3618 dependence_stats.num_dependence_undetermined++;
3619 finalize_ddr_dependent (ddr, res);
3620 return false;
3623 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
3625 static void
3626 subscript_dependence_tester (struct data_dependence_relation *ddr,
3627 struct loop *loop_nest)
3629 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr), loop_nest))
3630 dependence_stats.num_dependence_dependent++;
3632 compute_subscript_distance (ddr);
3633 if (build_classic_dist_vector (ddr, loop_nest))
3634 build_classic_dir_vector (ddr);
3637 /* Returns true when all the access functions of A are affine or
3638 constant with respect to LOOP_NEST. */
3640 static bool
3641 access_functions_are_affine_or_constant_p (const struct data_reference *a,
3642 const struct loop *loop_nest)
3644 unsigned int i;
3645 vec<tree> fns = DR_ACCESS_FNS (a);
3646 tree t;
3648 FOR_EACH_VEC_ELT (fns, i, t)
3649 if (!evolution_function_is_invariant_p (t, loop_nest->num)
3650 && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
3651 return false;
3653 return true;
3656 /* Initializes an equation for an OMEGA problem using the information
3657 contained in the ACCESS_FUN. Returns true when the operation
3658 succeeded.
3660 PB is the omega constraint system.
3661 EQ is the number of the equation to be initialized.
3662 OFFSET is used for shifting the variables names in the constraints:
3663 a constrain is composed of 2 * the number of variables surrounding
3664 dependence accesses. OFFSET is set either to 0 for the first n variables,
3665 then it is set to n.
3666 ACCESS_FUN is expected to be an affine chrec. */
3668 static bool
3669 init_omega_eq_with_af (omega_pb pb, unsigned eq,
3670 unsigned int offset, tree access_fun,
3671 struct data_dependence_relation *ddr)
3673 switch (TREE_CODE (access_fun))
3675 case POLYNOMIAL_CHREC:
3677 tree left = CHREC_LEFT (access_fun);
3678 tree right = CHREC_RIGHT (access_fun);
3679 int var = CHREC_VARIABLE (access_fun);
3680 unsigned var_idx;
3682 if (TREE_CODE (right) != INTEGER_CST)
3683 return false;
3685 var_idx = index_in_loop_nest (var, DDR_LOOP_NEST (ddr));
3686 pb->eqs[eq].coef[offset + var_idx + 1] = int_cst_value (right);
3688 /* Compute the innermost loop index. */
3689 DDR_INNER_LOOP (ddr) = MAX (DDR_INNER_LOOP (ddr), var_idx);
3691 if (offset == 0)
3692 pb->eqs[eq].coef[var_idx + DDR_NB_LOOPS (ddr) + 1]
3693 += int_cst_value (right);
3695 switch (TREE_CODE (left))
3697 case POLYNOMIAL_CHREC:
3698 return init_omega_eq_with_af (pb, eq, offset, left, ddr);
3700 case INTEGER_CST:
3701 pb->eqs[eq].coef[0] += int_cst_value (left);
3702 return true;
3704 default:
3705 return false;
3709 case INTEGER_CST:
3710 pb->eqs[eq].coef[0] += int_cst_value (access_fun);
3711 return true;
3713 default:
3714 return false;
3718 /* As explained in the comments preceding init_omega_for_ddr, we have
3719 to set up a system for each loop level, setting outer loops
3720 variation to zero, and current loop variation to positive or zero.
3721 Save each lexico positive distance vector. */
3723 static void
3724 omega_extract_distance_vectors (omega_pb pb,
3725 struct data_dependence_relation *ddr)
3727 int eq, geq;
3728 unsigned i, j;
3729 struct loop *loopi, *loopj;
3730 enum omega_result res;
3732 /* Set a new problem for each loop in the nest. The basis is the
3733 problem that we have initialized until now. On top of this we
3734 add new constraints. */
3735 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3736 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3738 int dist = 0;
3739 omega_pb copy = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr),
3740 DDR_NB_LOOPS (ddr));
3742 omega_copy_problem (copy, pb);
3744 /* For all the outer loops "loop_j", add "dj = 0". */
3745 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3747 eq = omega_add_zero_eq (copy, omega_black);
3748 copy->eqs[eq].coef[j + 1] = 1;
3751 /* For "loop_i", add "0 <= di". */
3752 geq = omega_add_zero_geq (copy, omega_black);
3753 copy->geqs[geq].coef[i + 1] = 1;
3755 /* Reduce the constraint system, and test that the current
3756 problem is feasible. */
3757 res = omega_simplify_problem (copy);
3758 if (res == omega_false
3759 || res == omega_unknown
3760 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3761 goto next_problem;
3763 for (eq = 0; eq < copy->num_subs; eq++)
3764 if (copy->subs[eq].key == (int) i + 1)
3766 dist = copy->subs[eq].coef[0];
3767 goto found_dist;
3770 if (dist == 0)
3772 /* Reinitialize problem... */
3773 omega_copy_problem (copy, pb);
3774 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3776 eq = omega_add_zero_eq (copy, omega_black);
3777 copy->eqs[eq].coef[j + 1] = 1;
3780 /* ..., but this time "di = 1". */
3781 eq = omega_add_zero_eq (copy, omega_black);
3782 copy->eqs[eq].coef[i + 1] = 1;
3783 copy->eqs[eq].coef[0] = -1;
3785 res = omega_simplify_problem (copy);
3786 if (res == omega_false
3787 || res == omega_unknown
3788 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3789 goto next_problem;
3791 for (eq = 0; eq < copy->num_subs; eq++)
3792 if (copy->subs[eq].key == (int) i + 1)
3794 dist = copy->subs[eq].coef[0];
3795 goto found_dist;
3799 found_dist:;
3800 /* Save the lexicographically positive distance vector. */
3801 if (dist >= 0)
3803 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3804 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3806 dist_v[i] = dist;
3808 for (eq = 0; eq < copy->num_subs; eq++)
3809 if (copy->subs[eq].key > 0)
3811 dist = copy->subs[eq].coef[0];
3812 dist_v[copy->subs[eq].key - 1] = dist;
3815 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3816 dir_v[j] = dir_from_dist (dist_v[j]);
3818 save_dist_v (ddr, dist_v);
3819 save_dir_v (ddr, dir_v);
3822 next_problem:;
3823 omega_free_problem (copy);
3827 /* This is called for each subscript of a tuple of data references:
3828 insert an equality for representing the conflicts. */
3830 static bool
3831 omega_setup_subscript (tree access_fun_a, tree access_fun_b,
3832 struct data_dependence_relation *ddr,
3833 omega_pb pb, bool *maybe_dependent)
3835 int eq;
3836 tree type = signed_type_for_types (TREE_TYPE (access_fun_a),
3837 TREE_TYPE (access_fun_b));
3838 tree fun_a = chrec_convert (type, access_fun_a, NULL);
3839 tree fun_b = chrec_convert (type, access_fun_b, NULL);
3840 tree difference = chrec_fold_minus (type, fun_a, fun_b);
3841 tree minus_one;
3843 /* When the fun_a - fun_b is not constant, the dependence is not
3844 captured by the classic distance vector representation. */
3845 if (TREE_CODE (difference) != INTEGER_CST)
3846 return false;
3848 /* ZIV test. */
3849 if (ziv_subscript_p (fun_a, fun_b) && !integer_zerop (difference))
3851 /* There is no dependence. */
3852 *maybe_dependent = false;
3853 return true;
3856 minus_one = build_int_cst (type, -1);
3857 fun_b = chrec_fold_multiply (type, fun_b, minus_one);
3859 eq = omega_add_zero_eq (pb, omega_black);
3860 if (!init_omega_eq_with_af (pb, eq, DDR_NB_LOOPS (ddr), fun_a, ddr)
3861 || !init_omega_eq_with_af (pb, eq, 0, fun_b, ddr))
3862 /* There is probably a dependence, but the system of
3863 constraints cannot be built: answer "don't know". */
3864 return false;
3866 /* GCD test. */
3867 if (DDR_NB_LOOPS (ddr) != 0 && pb->eqs[eq].coef[0]
3868 && !int_divides_p (lambda_vector_gcd
3869 ((lambda_vector) &(pb->eqs[eq].coef[1]),
3870 2 * DDR_NB_LOOPS (ddr)),
3871 pb->eqs[eq].coef[0]))
3873 /* There is no dependence. */
3874 *maybe_dependent = false;
3875 return true;
3878 return true;
3881 /* Helper function, same as init_omega_for_ddr but specialized for
3882 data references A and B. */
3884 static bool
3885 init_omega_for_ddr_1 (struct data_reference *dra, struct data_reference *drb,
3886 struct data_dependence_relation *ddr,
3887 omega_pb pb, bool *maybe_dependent)
3889 unsigned i;
3890 int ineq;
3891 struct loop *loopi;
3892 unsigned nb_loops = DDR_NB_LOOPS (ddr);
3894 /* Insert an equality per subscript. */
3895 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3897 if (!omega_setup_subscript (DR_ACCESS_FN (dra, i), DR_ACCESS_FN (drb, i),
3898 ddr, pb, maybe_dependent))
3899 return false;
3900 else if (*maybe_dependent == false)
3902 /* There is no dependence. */
3903 DDR_ARE_DEPENDENT (ddr) = chrec_known;
3904 return true;
3908 /* Insert inequalities: constraints corresponding to the iteration
3909 domain, i.e. the loops surrounding the references "loop_x" and
3910 the distance variables "dx". The layout of the OMEGA
3911 representation is as follows:
3912 - coef[0] is the constant
3913 - coef[1..nb_loops] are the protected variables that will not be
3914 removed by the solver: the "dx"
3915 - coef[nb_loops + 1, 2*nb_loops] are the loop variables: "loop_x".
3917 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3918 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3920 HOST_WIDE_INT nbi = max_stmt_executions_int (loopi);
3922 /* 0 <= loop_x */
3923 ineq = omega_add_zero_geq (pb, omega_black);
3924 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3926 /* 0 <= loop_x + dx */
3927 ineq = omega_add_zero_geq (pb, omega_black);
3928 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3929 pb->geqs[ineq].coef[i + 1] = 1;
3931 if (nbi != -1)
3933 /* loop_x <= nb_iters */
3934 ineq = omega_add_zero_geq (pb, omega_black);
3935 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3936 pb->geqs[ineq].coef[0] = nbi;
3938 /* loop_x + dx <= nb_iters */
3939 ineq = omega_add_zero_geq (pb, omega_black);
3940 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3941 pb->geqs[ineq].coef[i + 1] = -1;
3942 pb->geqs[ineq].coef[0] = nbi;
3944 /* A step "dx" bigger than nb_iters is not feasible, so
3945 add "0 <= nb_iters + dx", */
3946 ineq = omega_add_zero_geq (pb, omega_black);
3947 pb->geqs[ineq].coef[i + 1] = 1;
3948 pb->geqs[ineq].coef[0] = nbi;
3949 /* and "dx <= nb_iters". */
3950 ineq = omega_add_zero_geq (pb, omega_black);
3951 pb->geqs[ineq].coef[i + 1] = -1;
3952 pb->geqs[ineq].coef[0] = nbi;
3956 omega_extract_distance_vectors (pb, ddr);
3958 return true;
3961 /* Sets up the Omega dependence problem for the data dependence
3962 relation DDR. Returns false when the constraint system cannot be
3963 built, ie. when the test answers "don't know". Returns true
3964 otherwise, and when independence has been proved (using one of the
3965 trivial dependence test), set MAYBE_DEPENDENT to false, otherwise
3966 set MAYBE_DEPENDENT to true.
3968 Example: for setting up the dependence system corresponding to the
3969 conflicting accesses
3971 | loop_i
3972 | loop_j
3973 | A[i, i+1] = ...
3974 | ... A[2*j, 2*(i + j)]
3975 | endloop_j
3976 | endloop_i
3978 the following constraints come from the iteration domain:
3980 0 <= i <= Ni
3981 0 <= i + di <= Ni
3982 0 <= j <= Nj
3983 0 <= j + dj <= Nj
3985 where di, dj are the distance variables. The constraints
3986 representing the conflicting elements are:
3988 i = 2 * (j + dj)
3989 i + 1 = 2 * (i + di + j + dj)
3991 For asking that the resulting distance vector (di, dj) be
3992 lexicographically positive, we insert the constraint "di >= 0". If
3993 "di = 0" in the solution, we fix that component to zero, and we
3994 look at the inner loops: we set a new problem where all the outer
3995 loop distances are zero, and fix this inner component to be
3996 positive. When one of the components is positive, we save that
3997 distance, and set a new problem where the distance on this loop is
3998 zero, searching for other distances in the inner loops. Here is
3999 the classic example that illustrates that we have to set for each
4000 inner loop a new problem:
4002 | loop_1
4003 | loop_2
4004 | A[10]
4005 | endloop_2
4006 | endloop_1
4008 we have to save two distances (1, 0) and (0, 1).
4010 Given two array references, refA and refB, we have to set the
4011 dependence problem twice, refA vs. refB and refB vs. refA, and we
4012 cannot do a single test, as refB might occur before refA in the
4013 inner loops, and the contrary when considering outer loops: ex.
4015 | loop_0
4016 | loop_1
4017 | loop_2
4018 | T[{1,+,1}_2][{1,+,1}_1] // refA
4019 | T[{2,+,1}_2][{0,+,1}_1] // refB
4020 | endloop_2
4021 | endloop_1
4022 | endloop_0
4024 refB touches the elements in T before refA, and thus for the same
4025 loop_0 refB precedes refA: ie. the distance vector (0, 1, -1)
4026 but for successive loop_0 iterations, we have (1, -1, 1)
4028 The Omega solver expects the distance variables ("di" in the
4029 previous example) to come first in the constraint system (as
4030 variables to be protected, or "safe" variables), the constraint
4031 system is built using the following layout:
4033 "cst | distance vars | index vars".
4036 static bool
4037 init_omega_for_ddr (struct data_dependence_relation *ddr,
4038 bool *maybe_dependent)
4040 omega_pb pb;
4041 bool res = false;
4043 *maybe_dependent = true;
4045 if (same_access_functions (ddr))
4047 unsigned j;
4048 lambda_vector dir_v;
4050 /* Save the 0 vector. */
4051 save_dist_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
4052 dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4053 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
4054 dir_v[j] = dir_equal;
4055 save_dir_v (ddr, dir_v);
4057 /* Save the dependences carried by outer loops. */
4058 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4059 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4060 maybe_dependent);
4061 omega_free_problem (pb);
4062 return res;
4065 /* Omega expects the protected variables (those that have to be kept
4066 after elimination) to appear first in the constraint system.
4067 These variables are the distance variables. In the following
4068 initialization we declare NB_LOOPS safe variables, and the total
4069 number of variables for the constraint system is 2*NB_LOOPS. */
4070 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4071 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4072 maybe_dependent);
4073 omega_free_problem (pb);
4075 /* Stop computation if not decidable, or no dependence. */
4076 if (res == false || *maybe_dependent == false)
4077 return res;
4079 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4080 res = init_omega_for_ddr_1 (DDR_B (ddr), DDR_A (ddr), ddr, pb,
4081 maybe_dependent);
4082 omega_free_problem (pb);
4084 return res;
4087 /* Return true when DDR contains the same information as that stored
4088 in DIR_VECTS and in DIST_VECTS, return false otherwise. */
4090 static bool
4091 ddr_consistent_p (FILE *file,
4092 struct data_dependence_relation *ddr,
4093 vec<lambda_vector> dist_vects,
4094 vec<lambda_vector> dir_vects)
4096 unsigned int i, j;
4098 /* If dump_file is set, output there. */
4099 if (dump_file && (dump_flags & TDF_DETAILS))
4100 file = dump_file;
4102 if (dist_vects.length () != DDR_NUM_DIST_VECTS (ddr))
4104 lambda_vector b_dist_v;
4105 fprintf (file, "\n(Number of distance vectors differ: Banerjee has %d, Omega has %d.\n",
4106 dist_vects.length (),
4107 DDR_NUM_DIST_VECTS (ddr));
4109 fprintf (file, "Banerjee dist vectors:\n");
4110 FOR_EACH_VEC_ELT (dist_vects, i, b_dist_v)
4111 print_lambda_vector (file, b_dist_v, DDR_NB_LOOPS (ddr));
4113 fprintf (file, "Omega dist vectors:\n");
4114 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4115 print_lambda_vector (file, DDR_DIST_VECT (ddr, i), DDR_NB_LOOPS (ddr));
4117 fprintf (file, "data dependence relation:\n");
4118 dump_data_dependence_relation (file, ddr);
4120 fprintf (file, ")\n");
4121 return false;
4124 if (dir_vects.length () != DDR_NUM_DIR_VECTS (ddr))
4126 fprintf (file, "\n(Number of direction vectors differ: Banerjee has %d, Omega has %d.)\n",
4127 dir_vects.length (),
4128 DDR_NUM_DIR_VECTS (ddr));
4129 return false;
4132 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4134 lambda_vector a_dist_v;
4135 lambda_vector b_dist_v = DDR_DIST_VECT (ddr, i);
4137 /* Distance vectors are not ordered in the same way in the DDR
4138 and in the DIST_VECTS: search for a matching vector. */
4139 FOR_EACH_VEC_ELT (dist_vects, j, a_dist_v)
4140 if (lambda_vector_equal (a_dist_v, b_dist_v, DDR_NB_LOOPS (ddr)))
4141 break;
4143 if (j == dist_vects.length ())
4145 fprintf (file, "\n(Dist vectors from the first dependence analyzer:\n");
4146 print_dist_vectors (file, dist_vects, DDR_NB_LOOPS (ddr));
4147 fprintf (file, "not found in Omega dist vectors:\n");
4148 print_dist_vectors (file, DDR_DIST_VECTS (ddr), DDR_NB_LOOPS (ddr));
4149 fprintf (file, "data dependence relation:\n");
4150 dump_data_dependence_relation (file, ddr);
4151 fprintf (file, ")\n");
4155 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
4157 lambda_vector a_dir_v;
4158 lambda_vector b_dir_v = DDR_DIR_VECT (ddr, i);
4160 /* Direction vectors are not ordered in the same way in the DDR
4161 and in the DIR_VECTS: search for a matching vector. */
4162 FOR_EACH_VEC_ELT (dir_vects, j, a_dir_v)
4163 if (lambda_vector_equal (a_dir_v, b_dir_v, DDR_NB_LOOPS (ddr)))
4164 break;
4166 if (j == dist_vects.length ())
4168 fprintf (file, "\n(Dir vectors from the first dependence analyzer:\n");
4169 print_dir_vectors (file, dir_vects, DDR_NB_LOOPS (ddr));
4170 fprintf (file, "not found in Omega dir vectors:\n");
4171 print_dir_vectors (file, DDR_DIR_VECTS (ddr), DDR_NB_LOOPS (ddr));
4172 fprintf (file, "data dependence relation:\n");
4173 dump_data_dependence_relation (file, ddr);
4174 fprintf (file, ")\n");
4178 return true;
4181 /* This computes the affine dependence relation between A and B with
4182 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
4183 independence between two accesses, while CHREC_DONT_KNOW is used
4184 for representing the unknown relation.
4186 Note that it is possible to stop the computation of the dependence
4187 relation the first time we detect a CHREC_KNOWN element for a given
4188 subscript. */
4190 void
4191 compute_affine_dependence (struct data_dependence_relation *ddr,
4192 struct loop *loop_nest)
4194 struct data_reference *dra = DDR_A (ddr);
4195 struct data_reference *drb = DDR_B (ddr);
4197 if (dump_file && (dump_flags & TDF_DETAILS))
4199 fprintf (dump_file, "(compute_affine_dependence\n");
4200 fprintf (dump_file, " stmt_a: ");
4201 print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
4202 fprintf (dump_file, " stmt_b: ");
4203 print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
4206 /* Analyze only when the dependence relation is not yet known. */
4207 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4209 dependence_stats.num_dependence_tests++;
4211 if (access_functions_are_affine_or_constant_p (dra, loop_nest)
4212 && access_functions_are_affine_or_constant_p (drb, loop_nest))
4214 subscript_dependence_tester (ddr, loop_nest);
4216 if (flag_check_data_deps)
4218 /* Dump the dependences from the first algorithm. */
4219 if (dump_file && (dump_flags & TDF_DETAILS))
4221 fprintf (dump_file, "\n\nBanerjee Analyzer\n");
4222 dump_data_dependence_relation (dump_file, ddr);
4225 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4227 bool maybe_dependent;
4228 vec<lambda_vector> dir_vects, dist_vects;
4230 /* Save the result of the first DD analyzer. */
4231 dist_vects = DDR_DIST_VECTS (ddr);
4232 dir_vects = DDR_DIR_VECTS (ddr);
4234 /* Reset the information. */
4235 DDR_DIST_VECTS (ddr).create (0);
4236 DDR_DIR_VECTS (ddr).create (0);
4238 /* Compute the same information using Omega. */
4239 if (!init_omega_for_ddr (ddr, &maybe_dependent))
4240 goto csys_dont_know;
4242 if (dump_file && (dump_flags & TDF_DETAILS))
4244 fprintf (dump_file, "Omega Analyzer\n");
4245 dump_data_dependence_relation (dump_file, ddr);
4248 /* Check that we get the same information. */
4249 if (maybe_dependent)
4250 gcc_assert (ddr_consistent_p (stderr, ddr, dist_vects,
4251 dir_vects));
4256 /* As a last case, if the dependence cannot be determined, or if
4257 the dependence is considered too difficult to determine, answer
4258 "don't know". */
4259 else
4261 csys_dont_know:;
4262 dependence_stats.num_dependence_undetermined++;
4264 if (dump_file && (dump_flags & TDF_DETAILS))
4266 fprintf (dump_file, "Data ref a:\n");
4267 dump_data_reference (dump_file, dra);
4268 fprintf (dump_file, "Data ref b:\n");
4269 dump_data_reference (dump_file, drb);
4270 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
4272 finalize_ddr_dependent (ddr, chrec_dont_know);
4276 if (dump_file && (dump_flags & TDF_DETAILS))
4278 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4279 fprintf (dump_file, ") -> no dependence\n");
4280 else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
4281 fprintf (dump_file, ") -> dependence analysis failed\n");
4282 else
4283 fprintf (dump_file, ")\n");
4287 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
4288 the data references in DATAREFS, in the LOOP_NEST. When
4289 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
4290 relations. Return true when successful, i.e. data references number
4291 is small enough to be handled. */
4293 bool
4294 compute_all_dependences (vec<data_reference_p> datarefs,
4295 vec<ddr_p> *dependence_relations,
4296 vec<loop_p> loop_nest,
4297 bool compute_self_and_rr)
4299 struct data_dependence_relation *ddr;
4300 struct data_reference *a, *b;
4301 unsigned int i, j;
4303 if ((int) datarefs.length ()
4304 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
4306 struct data_dependence_relation *ddr;
4308 /* Insert a single relation into dependence_relations:
4309 chrec_dont_know. */
4310 ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
4311 dependence_relations->safe_push (ddr);
4312 return false;
4315 FOR_EACH_VEC_ELT (datarefs, i, a)
4316 for (j = i + 1; datarefs.iterate (j, &b); j++)
4317 if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
4319 ddr = initialize_data_dependence_relation (a, b, loop_nest);
4320 dependence_relations->safe_push (ddr);
4321 if (loop_nest.exists ())
4322 compute_affine_dependence (ddr, loop_nest[0]);
4325 if (compute_self_and_rr)
4326 FOR_EACH_VEC_ELT (datarefs, i, a)
4328 ddr = initialize_data_dependence_relation (a, a, loop_nest);
4329 dependence_relations->safe_push (ddr);
4330 if (loop_nest.exists ())
4331 compute_affine_dependence (ddr, loop_nest[0]);
4334 return true;
4337 /* Describes a location of a memory reference. */
4339 typedef struct data_ref_loc_d
4341 /* The memory reference. */
4342 tree ref;
4344 /* True if the memory reference is read. */
4345 bool is_read;
4346 } data_ref_loc;
4349 /* Stores the locations of memory references in STMT to REFERENCES. Returns
4350 true if STMT clobbers memory, false otherwise. */
4352 static bool
4353 get_references_in_stmt (gimple stmt, vec<data_ref_loc, va_heap> *references)
4355 bool clobbers_memory = false;
4356 data_ref_loc ref;
4357 tree op0, op1;
4358 enum gimple_code stmt_code = gimple_code (stmt);
4360 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4361 As we cannot model data-references to not spelled out
4362 accesses give up if they may occur. */
4363 if (stmt_code == GIMPLE_CALL
4364 && !(gimple_call_flags (stmt) & ECF_CONST))
4366 /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
4367 if (gimple_call_internal_p (stmt))
4368 switch (gimple_call_internal_fn (stmt))
4370 case IFN_GOMP_SIMD_LANE:
4372 struct loop *loop = gimple_bb (stmt)->loop_father;
4373 tree uid = gimple_call_arg (stmt, 0);
4374 gcc_assert (TREE_CODE (uid) == SSA_NAME);
4375 if (loop == NULL
4376 || loop->simduid != SSA_NAME_VAR (uid))
4377 clobbers_memory = true;
4378 break;
4380 case IFN_MASK_LOAD:
4381 case IFN_MASK_STORE:
4382 break;
4383 default:
4384 clobbers_memory = true;
4385 break;
4387 else
4388 clobbers_memory = true;
4390 else if (stmt_code == GIMPLE_ASM
4391 && (gimple_asm_volatile_p (stmt) || gimple_vuse (stmt)))
4392 clobbers_memory = true;
4394 if (!gimple_vuse (stmt))
4395 return clobbers_memory;
4397 if (stmt_code == GIMPLE_ASSIGN)
4399 tree base;
4400 op0 = gimple_assign_lhs (stmt);
4401 op1 = gimple_assign_rhs1 (stmt);
4403 if (DECL_P (op1)
4404 || (REFERENCE_CLASS_P (op1)
4405 && (base = get_base_address (op1))
4406 && TREE_CODE (base) != SSA_NAME))
4408 ref.ref = op1;
4409 ref.is_read = true;
4410 references->safe_push (ref);
4413 else if (stmt_code == GIMPLE_CALL)
4415 unsigned i, n;
4417 ref.is_read = false;
4418 if (gimple_call_internal_p (stmt))
4419 switch (gimple_call_internal_fn (stmt))
4421 case IFN_MASK_LOAD:
4422 if (gimple_call_lhs (stmt) == NULL_TREE)
4423 break;
4424 ref.is_read = true;
4425 case IFN_MASK_STORE:
4426 ref.ref = fold_build2 (MEM_REF,
4427 ref.is_read
4428 ? TREE_TYPE (gimple_call_lhs (stmt))
4429 : TREE_TYPE (gimple_call_arg (stmt, 3)),
4430 gimple_call_arg (stmt, 0),
4431 gimple_call_arg (stmt, 1));
4432 references->safe_push (ref);
4433 return false;
4434 default:
4435 break;
4438 op0 = gimple_call_lhs (stmt);
4439 n = gimple_call_num_args (stmt);
4440 for (i = 0; i < n; i++)
4442 op1 = gimple_call_arg (stmt, i);
4444 if (DECL_P (op1)
4445 || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
4447 ref.ref = op1;
4448 ref.is_read = true;
4449 references->safe_push (ref);
4453 else
4454 return clobbers_memory;
4456 if (op0
4457 && (DECL_P (op0)
4458 || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
4460 ref.ref = op0;
4461 ref.is_read = false;
4462 references->safe_push (ref);
4464 return clobbers_memory;
4467 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
4468 reference, returns false, otherwise returns true. NEST is the outermost
4469 loop of the loop nest in which the references should be analyzed. */
4471 bool
4472 find_data_references_in_stmt (struct loop *nest, gimple stmt,
4473 vec<data_reference_p> *datarefs)
4475 unsigned i;
4476 auto_vec<data_ref_loc, 2> references;
4477 data_ref_loc *ref;
4478 bool ret = true;
4479 data_reference_p dr;
4481 if (get_references_in_stmt (stmt, &references))
4482 return false;
4484 FOR_EACH_VEC_ELT (references, i, ref)
4486 dr = create_data_ref (nest, loop_containing_stmt (stmt),
4487 ref->ref, stmt, ref->is_read);
4488 gcc_assert (dr != NULL);
4489 datarefs->safe_push (dr);
4491 references.release ();
4492 return ret;
4495 /* Stores the data references in STMT to DATAREFS. If there is an
4496 unanalyzable reference, returns false, otherwise returns true.
4497 NEST is the outermost loop of the loop nest in which the references
4498 should be instantiated, LOOP is the loop in which the references
4499 should be analyzed. */
4501 bool
4502 graphite_find_data_references_in_stmt (loop_p nest, loop_p loop, gimple stmt,
4503 vec<data_reference_p> *datarefs)
4505 unsigned i;
4506 auto_vec<data_ref_loc, 2> references;
4507 data_ref_loc *ref;
4508 bool ret = true;
4509 data_reference_p dr;
4511 if (get_references_in_stmt (stmt, &references))
4512 return false;
4514 FOR_EACH_VEC_ELT (references, i, ref)
4516 dr = create_data_ref (nest, loop, ref->ref, stmt, ref->is_read);
4517 gcc_assert (dr != NULL);
4518 datarefs->safe_push (dr);
4521 references.release ();
4522 return ret;
4525 /* Search the data references in LOOP, and record the information into
4526 DATAREFS. Returns chrec_dont_know when failing to analyze a
4527 difficult case, returns NULL_TREE otherwise. */
4529 tree
4530 find_data_references_in_bb (struct loop *loop, basic_block bb,
4531 vec<data_reference_p> *datarefs)
4533 gimple_stmt_iterator bsi;
4535 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4537 gimple stmt = gsi_stmt (bsi);
4539 if (!find_data_references_in_stmt (loop, stmt, datarefs))
4541 struct data_reference *res;
4542 res = XCNEW (struct data_reference);
4543 datarefs->safe_push (res);
4545 return chrec_dont_know;
4549 return NULL_TREE;
4552 /* Search the data references in LOOP, and record the information into
4553 DATAREFS. Returns chrec_dont_know when failing to analyze a
4554 difficult case, returns NULL_TREE otherwise.
4556 TODO: This function should be made smarter so that it can handle address
4557 arithmetic as if they were array accesses, etc. */
4559 tree
4560 find_data_references_in_loop (struct loop *loop,
4561 vec<data_reference_p> *datarefs)
4563 basic_block bb, *bbs;
4564 unsigned int i;
4566 bbs = get_loop_body_in_dom_order (loop);
4568 for (i = 0; i < loop->num_nodes; i++)
4570 bb = bbs[i];
4572 if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
4574 free (bbs);
4575 return chrec_dont_know;
4578 free (bbs);
4580 return NULL_TREE;
4583 /* Recursive helper function. */
4585 static bool
4586 find_loop_nest_1 (struct loop *loop, vec<loop_p> *loop_nest)
4588 /* Inner loops of the nest should not contain siblings. Example:
4589 when there are two consecutive loops,
4591 | loop_0
4592 | loop_1
4593 | A[{0, +, 1}_1]
4594 | endloop_1
4595 | loop_2
4596 | A[{0, +, 1}_2]
4597 | endloop_2
4598 | endloop_0
4600 the dependence relation cannot be captured by the distance
4601 abstraction. */
4602 if (loop->next)
4603 return false;
4605 loop_nest->safe_push (loop);
4606 if (loop->inner)
4607 return find_loop_nest_1 (loop->inner, loop_nest);
4608 return true;
4611 /* Return false when the LOOP is not well nested. Otherwise return
4612 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4613 contain the loops from the outermost to the innermost, as they will
4614 appear in the classic distance vector. */
4616 bool
4617 find_loop_nest (struct loop *loop, vec<loop_p> *loop_nest)
4619 loop_nest->safe_push (loop);
4620 if (loop->inner)
4621 return find_loop_nest_1 (loop->inner, loop_nest);
4622 return true;
4625 /* Returns true when the data dependences have been computed, false otherwise.
4626 Given a loop nest LOOP, the following vectors are returned:
4627 DATAREFS is initialized to all the array elements contained in this loop,
4628 DEPENDENCE_RELATIONS contains the relations between the data references.
4629 Compute read-read and self relations if
4630 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4632 bool
4633 compute_data_dependences_for_loop (struct loop *loop,
4634 bool compute_self_and_read_read_dependences,
4635 vec<loop_p> *loop_nest,
4636 vec<data_reference_p> *datarefs,
4637 vec<ddr_p> *dependence_relations)
4639 bool res = true;
4641 memset (&dependence_stats, 0, sizeof (dependence_stats));
4643 /* If the loop nest is not well formed, or one of the data references
4644 is not computable, give up without spending time to compute other
4645 dependences. */
4646 if (!loop
4647 || !find_loop_nest (loop, loop_nest)
4648 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
4649 || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
4650 compute_self_and_read_read_dependences))
4651 res = false;
4653 if (dump_file && (dump_flags & TDF_STATS))
4655 fprintf (dump_file, "Dependence tester statistics:\n");
4657 fprintf (dump_file, "Number of dependence tests: %d\n",
4658 dependence_stats.num_dependence_tests);
4659 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
4660 dependence_stats.num_dependence_dependent);
4661 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
4662 dependence_stats.num_dependence_independent);
4663 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
4664 dependence_stats.num_dependence_undetermined);
4666 fprintf (dump_file, "Number of subscript tests: %d\n",
4667 dependence_stats.num_subscript_tests);
4668 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
4669 dependence_stats.num_subscript_undetermined);
4670 fprintf (dump_file, "Number of same subscript function: %d\n",
4671 dependence_stats.num_same_subscript_function);
4673 fprintf (dump_file, "Number of ziv tests: %d\n",
4674 dependence_stats.num_ziv);
4675 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4676 dependence_stats.num_ziv_dependent);
4677 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4678 dependence_stats.num_ziv_independent);
4679 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
4680 dependence_stats.num_ziv_unimplemented);
4682 fprintf (dump_file, "Number of siv tests: %d\n",
4683 dependence_stats.num_siv);
4684 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4685 dependence_stats.num_siv_dependent);
4686 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4687 dependence_stats.num_siv_independent);
4688 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4689 dependence_stats.num_siv_unimplemented);
4691 fprintf (dump_file, "Number of miv tests: %d\n",
4692 dependence_stats.num_miv);
4693 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4694 dependence_stats.num_miv_dependent);
4695 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4696 dependence_stats.num_miv_independent);
4697 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4698 dependence_stats.num_miv_unimplemented);
4701 return res;
4704 /* Returns true when the data dependences for the basic block BB have been
4705 computed, false otherwise.
4706 DATAREFS is initialized to all the array elements contained in this basic
4707 block, DEPENDENCE_RELATIONS contains the relations between the data
4708 references. Compute read-read and self relations if
4709 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4710 bool
4711 compute_data_dependences_for_bb (basic_block bb,
4712 bool compute_self_and_read_read_dependences,
4713 vec<data_reference_p> *datarefs,
4714 vec<ddr_p> *dependence_relations)
4716 if (find_data_references_in_bb (NULL, bb, datarefs) == chrec_dont_know)
4717 return false;
4719 return compute_all_dependences (*datarefs, dependence_relations, vNULL,
4720 compute_self_and_read_read_dependences);
4723 /* Entry point (for testing only). Analyze all the data references
4724 and the dependence relations in LOOP.
4726 The data references are computed first.
4728 A relation on these nodes is represented by a complete graph. Some
4729 of the relations could be of no interest, thus the relations can be
4730 computed on demand.
4732 In the following function we compute all the relations. This is
4733 just a first implementation that is here for:
4734 - for showing how to ask for the dependence relations,
4735 - for the debugging the whole dependence graph,
4736 - for the dejagnu testcases and maintenance.
4738 It is possible to ask only for a part of the graph, avoiding to
4739 compute the whole dependence graph. The computed dependences are
4740 stored in a knowledge base (KB) such that later queries don't
4741 recompute the same information. The implementation of this KB is
4742 transparent to the optimizer, and thus the KB can be changed with a
4743 more efficient implementation, or the KB could be disabled. */
4744 static void
4745 analyze_all_data_dependences (struct loop *loop)
4747 unsigned int i;
4748 int nb_data_refs = 10;
4749 vec<data_reference_p> datarefs;
4750 datarefs.create (nb_data_refs);
4751 vec<ddr_p> dependence_relations;
4752 dependence_relations.create (nb_data_refs * nb_data_refs);
4753 vec<loop_p> loop_nest;
4754 loop_nest.create (3);
4756 /* Compute DDs on the whole function. */
4757 compute_data_dependences_for_loop (loop, false, &loop_nest, &datarefs,
4758 &dependence_relations);
4760 if (dump_file)
4762 dump_data_dependence_relations (dump_file, dependence_relations);
4763 fprintf (dump_file, "\n\n");
4765 if (dump_flags & TDF_DETAILS)
4766 dump_dist_dir_vectors (dump_file, dependence_relations);
4768 if (dump_flags & TDF_STATS)
4770 unsigned nb_top_relations = 0;
4771 unsigned nb_bot_relations = 0;
4772 unsigned nb_chrec_relations = 0;
4773 struct data_dependence_relation *ddr;
4775 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4777 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
4778 nb_top_relations++;
4780 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4781 nb_bot_relations++;
4783 else
4784 nb_chrec_relations++;
4787 gather_stats_on_scev_database ();
4791 loop_nest.release ();
4792 free_dependence_relations (dependence_relations);
4793 free_data_refs (datarefs);
4796 /* Computes all the data dependences and check that the results of
4797 several analyzers are the same. */
4799 void
4800 tree_check_data_deps (void)
4802 struct loop *loop_nest;
4804 FOR_EACH_LOOP (loop_nest, 0)
4805 analyze_all_data_dependences (loop_nest);
4808 /* Free the memory used by a data dependence relation DDR. */
4810 void
4811 free_dependence_relation (struct data_dependence_relation *ddr)
4813 if (ddr == NULL)
4814 return;
4816 if (DDR_SUBSCRIPTS (ddr).exists ())
4817 free_subscripts (DDR_SUBSCRIPTS (ddr));
4818 DDR_DIST_VECTS (ddr).release ();
4819 DDR_DIR_VECTS (ddr).release ();
4821 free (ddr);
4824 /* Free the memory used by the data dependence relations from
4825 DEPENDENCE_RELATIONS. */
4827 void
4828 free_dependence_relations (vec<ddr_p> dependence_relations)
4830 unsigned int i;
4831 struct data_dependence_relation *ddr;
4833 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4834 if (ddr)
4835 free_dependence_relation (ddr);
4837 dependence_relations.release ();
4840 /* Free the memory used by the data references from DATAREFS. */
4842 void
4843 free_data_refs (vec<data_reference_p> datarefs)
4845 unsigned int i;
4846 struct data_reference *dr;
4848 FOR_EACH_VEC_ELT (datarefs, i, dr)
4849 free_data_ref (dr);
4850 datarefs.release ();