Merge from trunk @217148.
[official-gcc.git] / gcc / tree-data-ref.c
blob57d3a2ddf6750546e8e5959b3907d023b3f5aae7
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 "predict.h"
83 #include "vec.h"
84 #include "hashtab.h"
85 #include "hash-set.h"
86 #include "machmode.h"
87 #include "tm.h"
88 #include "hard-reg-set.h"
89 #include "input.h"
90 #include "function.h"
91 #include "dominance.h"
92 #include "cfg.h"
93 #include "basic-block.h"
94 #include "tree-ssa-alias.h"
95 #include "internal-fn.h"
96 #include "gimple-expr.h"
97 #include "is-a.h"
98 #include "gimple.h"
99 #include "gimple-iterator.h"
100 #include "tree-ssa-loop-niter.h"
101 #include "tree-ssa-loop.h"
102 #include "tree-ssa.h"
103 #include "cfgloop.h"
104 #include "tree-data-ref.h"
105 #include "tree-scalar-evolution.h"
106 #include "dumpfile.h"
107 #include "langhooks.h"
108 #include "tree-affine.h"
109 #include "params.h"
111 static struct datadep_stats
113 int num_dependence_tests;
114 int num_dependence_dependent;
115 int num_dependence_independent;
116 int num_dependence_undetermined;
118 int num_subscript_tests;
119 int num_subscript_undetermined;
120 int num_same_subscript_function;
122 int num_ziv;
123 int num_ziv_independent;
124 int num_ziv_dependent;
125 int num_ziv_unimplemented;
127 int num_siv;
128 int num_siv_independent;
129 int num_siv_dependent;
130 int num_siv_unimplemented;
132 int num_miv;
133 int num_miv_independent;
134 int num_miv_dependent;
135 int num_miv_unimplemented;
136 } dependence_stats;
138 static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
139 struct data_reference *,
140 struct data_reference *,
141 struct loop *);
142 /* Returns true iff A divides B. */
144 static inline bool
145 tree_fold_divides_p (const_tree a, const_tree b)
147 gcc_assert (TREE_CODE (a) == INTEGER_CST);
148 gcc_assert (TREE_CODE (b) == INTEGER_CST);
149 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a));
152 /* Returns true iff A divides B. */
154 static inline bool
155 int_divides_p (int a, int b)
157 return ((b % a) == 0);
162 /* Dump into FILE all the data references from DATAREFS. */
164 static void
165 dump_data_references (FILE *file, vec<data_reference_p> datarefs)
167 unsigned int i;
168 struct data_reference *dr;
170 FOR_EACH_VEC_ELT (datarefs, i, dr)
171 dump_data_reference (file, dr);
174 /* Unified dump into FILE all the data references from DATAREFS. */
176 DEBUG_FUNCTION void
177 debug (vec<data_reference_p> &ref)
179 dump_data_references (stderr, ref);
182 DEBUG_FUNCTION void
183 debug (vec<data_reference_p> *ptr)
185 if (ptr)
186 debug (*ptr);
187 else
188 fprintf (stderr, "<nil>\n");
192 /* Dump into STDERR all the data references from DATAREFS. */
194 DEBUG_FUNCTION void
195 debug_data_references (vec<data_reference_p> datarefs)
197 dump_data_references (stderr, datarefs);
200 /* Print to STDERR the data_reference DR. */
202 DEBUG_FUNCTION void
203 debug_data_reference (struct data_reference *dr)
205 dump_data_reference (stderr, dr);
208 /* Dump function for a DATA_REFERENCE structure. */
210 void
211 dump_data_reference (FILE *outf,
212 struct data_reference *dr)
214 unsigned int i;
216 fprintf (outf, "#(Data Ref: \n");
217 fprintf (outf, "# bb: %d \n", gimple_bb (DR_STMT (dr))->index);
218 fprintf (outf, "# stmt: ");
219 print_gimple_stmt (outf, DR_STMT (dr), 0, 0);
220 fprintf (outf, "# ref: ");
221 print_generic_stmt (outf, DR_REF (dr), 0);
222 fprintf (outf, "# base_object: ");
223 print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
225 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
227 fprintf (outf, "# Access function %d: ", i);
228 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
230 fprintf (outf, "#)\n");
233 /* Unified dump function for a DATA_REFERENCE structure. */
235 DEBUG_FUNCTION void
236 debug (data_reference &ref)
238 dump_data_reference (stderr, &ref);
241 DEBUG_FUNCTION void
242 debug (data_reference *ptr)
244 if (ptr)
245 debug (*ptr);
246 else
247 fprintf (stderr, "<nil>\n");
251 /* Dumps the affine function described by FN to the file OUTF. */
253 static void
254 dump_affine_function (FILE *outf, affine_fn fn)
256 unsigned i;
257 tree coef;
259 print_generic_expr (outf, fn[0], TDF_SLIM);
260 for (i = 1; fn.iterate (i, &coef); i++)
262 fprintf (outf, " + ");
263 print_generic_expr (outf, coef, TDF_SLIM);
264 fprintf (outf, " * x_%u", i);
268 /* Dumps the conflict function CF to the file OUTF. */
270 static void
271 dump_conflict_function (FILE *outf, conflict_function *cf)
273 unsigned i;
275 if (cf->n == NO_DEPENDENCE)
276 fprintf (outf, "no dependence");
277 else if (cf->n == NOT_KNOWN)
278 fprintf (outf, "not known");
279 else
281 for (i = 0; i < cf->n; i++)
283 if (i != 0)
284 fprintf (outf, " ");
285 fprintf (outf, "[");
286 dump_affine_function (outf, cf->fns[i]);
287 fprintf (outf, "]");
292 /* Dump function for a SUBSCRIPT structure. */
294 static void
295 dump_subscript (FILE *outf, struct subscript *subscript)
297 conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
299 fprintf (outf, "\n (subscript \n");
300 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
301 dump_conflict_function (outf, cf);
302 if (CF_NONTRIVIAL_P (cf))
304 tree last_iteration = SUB_LAST_CONFLICT (subscript);
305 fprintf (outf, "\n last_conflict: ");
306 print_generic_expr (outf, last_iteration, 0);
309 cf = SUB_CONFLICTS_IN_B (subscript);
310 fprintf (outf, "\n iterations_that_access_an_element_twice_in_B: ");
311 dump_conflict_function (outf, cf);
312 if (CF_NONTRIVIAL_P (cf))
314 tree last_iteration = SUB_LAST_CONFLICT (subscript);
315 fprintf (outf, "\n last_conflict: ");
316 print_generic_expr (outf, last_iteration, 0);
319 fprintf (outf, "\n (Subscript distance: ");
320 print_generic_expr (outf, SUB_DISTANCE (subscript), 0);
321 fprintf (outf, " ))\n");
324 /* Print the classic direction vector DIRV to OUTF. */
326 static void
327 print_direction_vector (FILE *outf,
328 lambda_vector dirv,
329 int length)
331 int eq;
333 for (eq = 0; eq < length; eq++)
335 enum data_dependence_direction dir = ((enum data_dependence_direction)
336 dirv[eq]);
338 switch (dir)
340 case dir_positive:
341 fprintf (outf, " +");
342 break;
343 case dir_negative:
344 fprintf (outf, " -");
345 break;
346 case dir_equal:
347 fprintf (outf, " =");
348 break;
349 case dir_positive_or_equal:
350 fprintf (outf, " +=");
351 break;
352 case dir_positive_or_negative:
353 fprintf (outf, " +-");
354 break;
355 case dir_negative_or_equal:
356 fprintf (outf, " -=");
357 break;
358 case dir_star:
359 fprintf (outf, " *");
360 break;
361 default:
362 fprintf (outf, "indep");
363 break;
366 fprintf (outf, "\n");
369 /* Print a vector of direction vectors. */
371 static void
372 print_dir_vectors (FILE *outf, vec<lambda_vector> dir_vects,
373 int length)
375 unsigned j;
376 lambda_vector v;
378 FOR_EACH_VEC_ELT (dir_vects, j, v)
379 print_direction_vector (outf, v, length);
382 /* Print out a vector VEC of length N to OUTFILE. */
384 static inline void
385 print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
387 int i;
389 for (i = 0; i < n; i++)
390 fprintf (outfile, "%3d ", vector[i]);
391 fprintf (outfile, "\n");
394 /* Print a vector of distance vectors. */
396 static void
397 print_dist_vectors (FILE *outf, vec<lambda_vector> dist_vects,
398 int length)
400 unsigned j;
401 lambda_vector v;
403 FOR_EACH_VEC_ELT (dist_vects, j, v)
404 print_lambda_vector (outf, v, length);
407 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
409 static void
410 dump_data_dependence_relation (FILE *outf,
411 struct data_dependence_relation *ddr)
413 struct data_reference *dra, *drb;
415 fprintf (outf, "(Data Dep: \n");
417 if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
419 if (ddr)
421 dra = DDR_A (ddr);
422 drb = DDR_B (ddr);
423 if (dra)
424 dump_data_reference (outf, dra);
425 else
426 fprintf (outf, " (nil)\n");
427 if (drb)
428 dump_data_reference (outf, drb);
429 else
430 fprintf (outf, " (nil)\n");
432 fprintf (outf, " (don't know)\n)\n");
433 return;
436 dra = DDR_A (ddr);
437 drb = DDR_B (ddr);
438 dump_data_reference (outf, dra);
439 dump_data_reference (outf, drb);
441 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
442 fprintf (outf, " (no dependence)\n");
444 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
446 unsigned int i;
447 struct loop *loopi;
449 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
451 fprintf (outf, " access_fn_A: ");
452 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
453 fprintf (outf, " access_fn_B: ");
454 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
455 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
458 fprintf (outf, " inner loop index: %d\n", DDR_INNER_LOOP (ddr));
459 fprintf (outf, " loop nest: (");
460 FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
461 fprintf (outf, "%d ", loopi->num);
462 fprintf (outf, ")\n");
464 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
466 fprintf (outf, " distance_vector: ");
467 print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
468 DDR_NB_LOOPS (ddr));
471 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
473 fprintf (outf, " direction_vector: ");
474 print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
475 DDR_NB_LOOPS (ddr));
479 fprintf (outf, ")\n");
482 /* Debug version. */
484 DEBUG_FUNCTION void
485 debug_data_dependence_relation (struct data_dependence_relation *ddr)
487 dump_data_dependence_relation (stderr, ddr);
490 /* Dump into FILE all the dependence relations from DDRS. */
492 void
493 dump_data_dependence_relations (FILE *file,
494 vec<ddr_p> ddrs)
496 unsigned int i;
497 struct data_dependence_relation *ddr;
499 FOR_EACH_VEC_ELT (ddrs, i, ddr)
500 dump_data_dependence_relation (file, ddr);
503 DEBUG_FUNCTION void
504 debug (vec<ddr_p> &ref)
506 dump_data_dependence_relations (stderr, ref);
509 DEBUG_FUNCTION void
510 debug (vec<ddr_p> *ptr)
512 if (ptr)
513 debug (*ptr);
514 else
515 fprintf (stderr, "<nil>\n");
519 /* Dump to STDERR all the dependence relations from DDRS. */
521 DEBUG_FUNCTION void
522 debug_data_dependence_relations (vec<ddr_p> ddrs)
524 dump_data_dependence_relations (stderr, ddrs);
527 /* Dumps the distance and direction vectors in FILE. DDRS contains
528 the dependence relations, and VECT_SIZE is the size of the
529 dependence vectors, or in other words the number of loops in the
530 considered nest. */
532 static void
533 dump_dist_dir_vectors (FILE *file, vec<ddr_p> ddrs)
535 unsigned int i, j;
536 struct data_dependence_relation *ddr;
537 lambda_vector v;
539 FOR_EACH_VEC_ELT (ddrs, i, ddr)
540 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
542 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), j, v)
544 fprintf (file, "DISTANCE_V (");
545 print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
546 fprintf (file, ")\n");
549 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), j, v)
551 fprintf (file, "DIRECTION_V (");
552 print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
553 fprintf (file, ")\n");
557 fprintf (file, "\n\n");
560 /* Dumps the data dependence relations DDRS in FILE. */
562 static void
563 dump_ddrs (FILE *file, vec<ddr_p> ddrs)
565 unsigned int i;
566 struct data_dependence_relation *ddr;
568 FOR_EACH_VEC_ELT (ddrs, i, ddr)
569 dump_data_dependence_relation (file, ddr);
571 fprintf (file, "\n\n");
574 DEBUG_FUNCTION void
575 debug_ddrs (vec<ddr_p> ddrs)
577 dump_ddrs (stderr, ddrs);
580 /* Helper function for split_constant_offset. Expresses OP0 CODE OP1
581 (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
582 constant of type ssizetype, and returns true. If we cannot do this
583 with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
584 is returned. */
586 static bool
587 split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
588 tree *var, tree *off)
590 tree var0, var1;
591 tree off0, off1;
592 enum tree_code ocode = code;
594 *var = NULL_TREE;
595 *off = NULL_TREE;
597 switch (code)
599 case INTEGER_CST:
600 *var = build_int_cst (type, 0);
601 *off = fold_convert (ssizetype, op0);
602 return true;
604 case POINTER_PLUS_EXPR:
605 ocode = PLUS_EXPR;
606 /* FALLTHROUGH */
607 case PLUS_EXPR:
608 case MINUS_EXPR:
609 split_constant_offset (op0, &var0, &off0);
610 split_constant_offset (op1, &var1, &off1);
611 *var = fold_build2 (code, type, var0, var1);
612 *off = size_binop (ocode, off0, off1);
613 return true;
615 case MULT_EXPR:
616 if (TREE_CODE (op1) != INTEGER_CST)
617 return false;
619 split_constant_offset (op0, &var0, &off0);
620 *var = fold_build2 (MULT_EXPR, type, var0, op1);
621 *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
622 return true;
624 case ADDR_EXPR:
626 tree base, poffset;
627 HOST_WIDE_INT pbitsize, pbitpos;
628 machine_mode pmode;
629 int punsignedp, preversep, pvolatilep;
631 op0 = TREE_OPERAND (op0, 0);
632 base
633 = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset, &pmode,
634 &punsignedp, &preversep, &pvolatilep, false);
636 if (pbitpos % BITS_PER_UNIT != 0)
637 return false;
638 base = build_fold_addr_expr (base);
639 off0 = ssize_int (pbitpos / BITS_PER_UNIT);
641 if (poffset)
643 split_constant_offset (poffset, &poffset, &off1);
644 off0 = size_binop (PLUS_EXPR, off0, off1);
645 if (POINTER_TYPE_P (TREE_TYPE (base)))
646 base = fold_build_pointer_plus (base, poffset);
647 else
648 base = fold_build2 (PLUS_EXPR, TREE_TYPE (base), base,
649 fold_convert (TREE_TYPE (base), poffset));
652 var0 = fold_convert (type, base);
654 /* If variable length types are involved, punt, otherwise casts
655 might be converted into ARRAY_REFs in gimplify_conversion.
656 To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
657 possibly no longer appears in current GIMPLE, might resurface.
658 This perhaps could run
659 if (CONVERT_EXPR_P (var0))
661 gimplify_conversion (&var0);
662 // Attempt to fill in any within var0 found ARRAY_REF's
663 // element size from corresponding op embedded ARRAY_REF,
664 // if unsuccessful, just punt.
665 } */
666 while (POINTER_TYPE_P (type))
667 type = TREE_TYPE (type);
668 if (int_size_in_bytes (type) < 0)
669 return false;
671 *var = var0;
672 *off = off0;
673 return true;
676 case SSA_NAME:
678 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
679 enum tree_code subcode;
681 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
682 return false;
684 var0 = gimple_assign_rhs1 (def_stmt);
685 subcode = gimple_assign_rhs_code (def_stmt);
686 var1 = gimple_assign_rhs2 (def_stmt);
688 return split_constant_offset_1 (type, var0, subcode, var1, var, off);
690 CASE_CONVERT:
692 /* We must not introduce undefined overflow, and we must not change the value.
693 Hence we're okay if the inner type doesn't overflow to start with
694 (pointer or signed), the outer type also is an integer or pointer
695 and the outer precision is at least as large as the inner. */
696 tree itype = TREE_TYPE (op0);
697 if ((POINTER_TYPE_P (itype)
698 || (INTEGRAL_TYPE_P (itype) && TYPE_OVERFLOW_UNDEFINED (itype)))
699 && TYPE_PRECISION (type) >= TYPE_PRECISION (itype)
700 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)))
702 split_constant_offset (op0, &var0, off);
703 *var = fold_convert (type, var0);
704 return true;
706 return false;
709 default:
710 return false;
714 /* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
715 will be ssizetype. */
717 void
718 split_constant_offset (tree exp, tree *var, tree *off)
720 tree type = TREE_TYPE (exp), otype, op0, op1, e, o;
721 enum tree_code code;
723 *var = exp;
724 *off = ssize_int (0);
725 STRIP_NOPS (exp);
727 if (tree_is_chrec (exp)
728 || get_gimple_rhs_class (TREE_CODE (exp)) == GIMPLE_TERNARY_RHS)
729 return;
731 otype = TREE_TYPE (exp);
732 code = TREE_CODE (exp);
733 extract_ops_from_tree (exp, &code, &op0, &op1);
734 if (split_constant_offset_1 (otype, op0, code, op1, &e, &o))
736 *var = fold_convert (type, e);
737 *off = o;
741 /* Returns the address ADDR of an object in a canonical shape (without nop
742 casts, and with type of pointer to the object). */
744 static tree
745 canonicalize_base_object_address (tree addr)
747 tree orig = addr;
749 STRIP_NOPS (addr);
751 /* The base address may be obtained by casting from integer, in that case
752 keep the cast. */
753 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
754 return orig;
756 if (TREE_CODE (addr) != ADDR_EXPR)
757 return addr;
759 return build_fold_addr_expr (TREE_OPERAND (addr, 0));
762 /* Analyzes the behavior of the memory reference DR in the innermost loop or
763 basic block that contains it. Returns true if analysis succeed or false
764 otherwise. */
766 bool
767 dr_analyze_innermost (struct data_reference *dr, struct loop *nest)
769 gimple stmt = DR_STMT (dr);
770 struct loop *loop = loop_containing_stmt (stmt);
771 tree ref = DR_REF (dr);
772 HOST_WIDE_INT pbitsize, pbitpos;
773 tree base, poffset;
774 machine_mode pmode;
775 int punsignedp, preversep, pvolatilep;
776 affine_iv base_iv, offset_iv;
777 tree init, dinit, step;
778 bool in_loop = (loop && loop->num);
780 if (dump_file && (dump_flags & TDF_DETAILS))
781 fprintf (dump_file, "analyze_innermost: ");
783 base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset, &pmode,
784 &punsignedp, &preversep, &pvolatilep, false);
785 gcc_assert (base != NULL_TREE);
787 if (pbitpos % BITS_PER_UNIT != 0)
789 if (dump_file && (dump_flags & TDF_DETAILS))
790 fprintf (dump_file, "failed: bit offset alignment.\n");
791 return false;
794 if (preversep)
796 if (dump_file && (dump_flags & TDF_DETAILS))
797 fprintf (dump_file, "failed: reverse storage order.\n");
798 return false;
801 if (TREE_CODE (base) == MEM_REF)
803 if (!integer_zerop (TREE_OPERAND (base, 1)))
805 offset_int moff = mem_ref_offset (base);
806 tree mofft = wide_int_to_tree (sizetype, moff);
807 if (!poffset)
808 poffset = mofft;
809 else
810 poffset = size_binop (PLUS_EXPR, poffset, mofft);
812 base = TREE_OPERAND (base, 0);
814 else
815 base = build_fold_addr_expr (base);
817 if (in_loop)
819 if (!simple_iv (loop, loop_containing_stmt (stmt), base, &base_iv,
820 nest ? true : false))
822 if (nest)
824 if (dump_file && (dump_flags & TDF_DETAILS))
825 fprintf (dump_file, "failed: evolution of base is not"
826 " affine.\n");
827 return false;
829 else
831 base_iv.base = base;
832 base_iv.step = ssize_int (0);
833 base_iv.no_overflow = true;
837 else
839 base_iv.base = base;
840 base_iv.step = ssize_int (0);
841 base_iv.no_overflow = true;
844 if (!poffset)
846 offset_iv.base = ssize_int (0);
847 offset_iv.step = ssize_int (0);
849 else
851 if (!in_loop)
853 offset_iv.base = poffset;
854 offset_iv.step = ssize_int (0);
856 else if (!simple_iv (loop, loop_containing_stmt (stmt),
857 poffset, &offset_iv,
858 nest ? true : false))
860 if (nest)
862 if (dump_file && (dump_flags & TDF_DETAILS))
863 fprintf (dump_file, "failed: evolution of offset is not"
864 " affine.\n");
865 return false;
867 else
869 offset_iv.base = poffset;
870 offset_iv.step = ssize_int (0);
875 init = ssize_int (pbitpos / BITS_PER_UNIT);
876 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
877 init = size_binop (PLUS_EXPR, init, dinit);
878 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
879 init = size_binop (PLUS_EXPR, init, dinit);
881 step = size_binop (PLUS_EXPR,
882 fold_convert (ssizetype, base_iv.step),
883 fold_convert (ssizetype, offset_iv.step));
885 DR_BASE_ADDRESS (dr) = canonicalize_base_object_address (base_iv.base);
887 DR_OFFSET (dr) = fold_convert (ssizetype, offset_iv.base);
888 DR_INIT (dr) = init;
889 DR_STEP (dr) = step;
891 DR_ALIGNED_TO (dr) = size_int (highest_pow2_factor (offset_iv.base));
893 if (dump_file && (dump_flags & TDF_DETAILS))
894 fprintf (dump_file, "success.\n");
896 return true;
899 /* Determines the base object and the list of indices of memory reference
900 DR, analyzed in LOOP and instantiated in loop nest NEST. */
902 static void
903 dr_analyze_indices (struct data_reference *dr, loop_p nest, loop_p loop)
905 vec<tree> access_fns = vNULL;
906 tree ref, op;
907 tree base, off, access_fn;
908 basic_block before_loop;
910 /* If analyzing a basic-block there are no indices to analyze
911 and thus no access functions. */
912 if (!nest)
914 DR_BASE_OBJECT (dr) = DR_REF (dr);
915 DR_ACCESS_FNS (dr).create (0);
916 return;
919 ref = DR_REF (dr);
920 before_loop = block_before_loop (nest);
922 /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
923 into a two element array with a constant index. The base is
924 then just the immediate underlying object. */
925 if (TREE_CODE (ref) == REALPART_EXPR)
927 ref = TREE_OPERAND (ref, 0);
928 access_fns.safe_push (integer_zero_node);
930 else if (TREE_CODE (ref) == IMAGPART_EXPR)
932 ref = TREE_OPERAND (ref, 0);
933 access_fns.safe_push (integer_one_node);
936 /* Analyze access functions of dimensions we know to be independent. */
937 while (handled_component_p (ref))
939 if (TREE_CODE (ref) == ARRAY_REF)
941 op = TREE_OPERAND (ref, 1);
942 access_fn = analyze_scalar_evolution (loop, op);
943 access_fn = instantiate_scev (before_loop, loop, access_fn);
944 access_fns.safe_push (access_fn);
946 else if (TREE_CODE (ref) == COMPONENT_REF
947 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
949 /* For COMPONENT_REFs of records (but not unions!) use the
950 FIELD_DECL offset as constant access function so we can
951 disambiguate a[i].f1 and a[i].f2. */
952 tree off = component_ref_field_offset (ref);
953 off = size_binop (PLUS_EXPR,
954 size_binop (MULT_EXPR,
955 fold_convert (bitsizetype, off),
956 bitsize_int (BITS_PER_UNIT)),
957 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
958 access_fns.safe_push (off);
960 else
961 /* If we have an unhandled component we could not translate
962 to an access function stop analyzing. We have determined
963 our base object in this case. */
964 break;
966 ref = TREE_OPERAND (ref, 0);
969 /* If the address operand of a MEM_REF base has an evolution in the
970 analyzed nest, add it as an additional independent access-function. */
971 if (TREE_CODE (ref) == MEM_REF)
973 op = TREE_OPERAND (ref, 0);
974 access_fn = analyze_scalar_evolution (loop, op);
975 access_fn = instantiate_scev (before_loop, loop, access_fn);
976 if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
978 tree orig_type;
979 tree memoff = TREE_OPERAND (ref, 1);
980 base = initial_condition (access_fn);
981 orig_type = TREE_TYPE (base);
982 STRIP_USELESS_TYPE_CONVERSION (base);
983 split_constant_offset (base, &base, &off);
984 STRIP_USELESS_TYPE_CONVERSION (base);
985 /* Fold the MEM_REF offset into the evolutions initial
986 value to make more bases comparable. */
987 if (!integer_zerop (memoff))
989 off = size_binop (PLUS_EXPR, off,
990 fold_convert (ssizetype, memoff));
991 memoff = build_int_cst (TREE_TYPE (memoff), 0);
993 access_fn = chrec_replace_initial_condition
994 (access_fn, fold_convert (orig_type, off));
995 /* ??? This is still not a suitable base object for
996 dr_may_alias_p - the base object needs to be an
997 access that covers the object as whole. With
998 an evolution in the pointer this cannot be
999 guaranteed.
1000 As a band-aid, mark the access so we can special-case
1001 it in dr_may_alias_p. */
1002 ref = fold_build2_loc (EXPR_LOCATION (ref),
1003 MEM_REF, TREE_TYPE (ref),
1004 base, memoff);
1005 access_fns.safe_push (access_fn);
1008 else if (DECL_P (ref))
1010 /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
1011 ref = build2 (MEM_REF, TREE_TYPE (ref),
1012 build_fold_addr_expr (ref),
1013 build_int_cst (reference_alias_ptr_type (ref), 0));
1016 DR_BASE_OBJECT (dr) = ref;
1017 DR_ACCESS_FNS (dr) = access_fns;
1020 /* Extracts the alias analysis information from the memory reference DR. */
1022 static void
1023 dr_analyze_alias (struct data_reference *dr)
1025 tree ref = DR_REF (dr);
1026 tree base = get_base_address (ref), addr;
1028 if (INDIRECT_REF_P (base)
1029 || TREE_CODE (base) == MEM_REF)
1031 addr = TREE_OPERAND (base, 0);
1032 if (TREE_CODE (addr) == SSA_NAME)
1033 DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
1037 /* Frees data reference DR. */
1039 void
1040 free_data_ref (data_reference_p dr)
1042 DR_ACCESS_FNS (dr).release ();
1043 free (dr);
1046 /* Analyzes memory reference MEMREF accessed in STMT. The reference
1047 is read if IS_READ is true, write otherwise. Returns the
1048 data_reference description of MEMREF. NEST is the outermost loop
1049 in which the reference should be instantiated, LOOP is the loop in
1050 which the data reference should be analyzed. */
1052 struct data_reference *
1053 create_data_ref (loop_p nest, loop_p loop, tree memref, gimple stmt,
1054 bool is_read)
1056 struct data_reference *dr;
1058 if (dump_file && (dump_flags & TDF_DETAILS))
1060 fprintf (dump_file, "Creating dr for ");
1061 print_generic_expr (dump_file, memref, TDF_SLIM);
1062 fprintf (dump_file, "\n");
1065 dr = XCNEW (struct data_reference);
1066 DR_STMT (dr) = stmt;
1067 DR_REF (dr) = memref;
1068 DR_IS_READ (dr) = is_read;
1070 dr_analyze_innermost (dr, nest);
1071 dr_analyze_indices (dr, nest, loop);
1072 dr_analyze_alias (dr);
1074 if (dump_file && (dump_flags & TDF_DETAILS))
1076 unsigned i;
1077 fprintf (dump_file, "\tbase_address: ");
1078 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1079 fprintf (dump_file, "\n\toffset from base address: ");
1080 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1081 fprintf (dump_file, "\n\tconstant offset from base address: ");
1082 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1083 fprintf (dump_file, "\n\tstep: ");
1084 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1085 fprintf (dump_file, "\n\taligned to: ");
1086 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
1087 fprintf (dump_file, "\n\tbase_object: ");
1088 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1089 fprintf (dump_file, "\n");
1090 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
1092 fprintf (dump_file, "\tAccess function %d: ", i);
1093 print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
1097 return dr;
1100 /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
1101 expressions. */
1102 static bool
1103 dr_equal_offsets_p1 (tree offset1, tree offset2)
1105 bool res;
1107 STRIP_NOPS (offset1);
1108 STRIP_NOPS (offset2);
1110 if (offset1 == offset2)
1111 return true;
1113 if (TREE_CODE (offset1) != TREE_CODE (offset2)
1114 || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
1115 return false;
1117 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
1118 TREE_OPERAND (offset2, 0));
1120 if (!res || !BINARY_CLASS_P (offset1))
1121 return res;
1123 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
1124 TREE_OPERAND (offset2, 1));
1126 return res;
1129 /* Check if DRA and DRB have equal offsets. */
1130 bool
1131 dr_equal_offsets_p (struct data_reference *dra,
1132 struct data_reference *drb)
1134 tree offset1, offset2;
1136 offset1 = DR_OFFSET (dra);
1137 offset2 = DR_OFFSET (drb);
1139 return dr_equal_offsets_p1 (offset1, offset2);
1142 /* Returns true if FNA == FNB. */
1144 static bool
1145 affine_function_equal_p (affine_fn fna, affine_fn fnb)
1147 unsigned i, n = fna.length ();
1149 if (n != fnb.length ())
1150 return false;
1152 for (i = 0; i < n; i++)
1153 if (!operand_equal_p (fna[i], fnb[i], 0))
1154 return false;
1156 return true;
1159 /* If all the functions in CF are the same, returns one of them,
1160 otherwise returns NULL. */
1162 static affine_fn
1163 common_affine_function (conflict_function *cf)
1165 unsigned i;
1166 affine_fn comm;
1168 if (!CF_NONTRIVIAL_P (cf))
1169 return affine_fn ();
1171 comm = cf->fns[0];
1173 for (i = 1; i < cf->n; i++)
1174 if (!affine_function_equal_p (comm, cf->fns[i]))
1175 return affine_fn ();
1177 return comm;
1180 /* Returns the base of the affine function FN. */
1182 static tree
1183 affine_function_base (affine_fn fn)
1185 return fn[0];
1188 /* Returns true if FN is a constant. */
1190 static bool
1191 affine_function_constant_p (affine_fn fn)
1193 unsigned i;
1194 tree coef;
1196 for (i = 1; fn.iterate (i, &coef); i++)
1197 if (!integer_zerop (coef))
1198 return false;
1200 return true;
1203 /* Returns true if FN is the zero constant function. */
1205 static bool
1206 affine_function_zero_p (affine_fn fn)
1208 return (integer_zerop (affine_function_base (fn))
1209 && affine_function_constant_p (fn));
1212 /* Returns a signed integer type with the largest precision from TA
1213 and TB. */
1215 static tree
1216 signed_type_for_types (tree ta, tree tb)
1218 if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
1219 return signed_type_for (ta);
1220 else
1221 return signed_type_for (tb);
1224 /* Applies operation OP on affine functions FNA and FNB, and returns the
1225 result. */
1227 static affine_fn
1228 affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
1230 unsigned i, n, m;
1231 affine_fn ret;
1232 tree coef;
1234 if (fnb.length () > fna.length ())
1236 n = fna.length ();
1237 m = fnb.length ();
1239 else
1241 n = fnb.length ();
1242 m = fna.length ();
1245 ret.create (m);
1246 for (i = 0; i < n; i++)
1248 tree type = signed_type_for_types (TREE_TYPE (fna[i]),
1249 TREE_TYPE (fnb[i]));
1250 ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
1253 for (; fna.iterate (i, &coef); i++)
1254 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1255 coef, integer_zero_node));
1256 for (; fnb.iterate (i, &coef); i++)
1257 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1258 integer_zero_node, coef));
1260 return ret;
1263 /* Returns the sum of affine functions FNA and FNB. */
1265 static affine_fn
1266 affine_fn_plus (affine_fn fna, affine_fn fnb)
1268 return affine_fn_op (PLUS_EXPR, fna, fnb);
1271 /* Returns the difference of affine functions FNA and FNB. */
1273 static affine_fn
1274 affine_fn_minus (affine_fn fna, affine_fn fnb)
1276 return affine_fn_op (MINUS_EXPR, fna, fnb);
1279 /* Frees affine function FN. */
1281 static void
1282 affine_fn_free (affine_fn fn)
1284 fn.release ();
1287 /* Determine for each subscript in the data dependence relation DDR
1288 the distance. */
1290 static void
1291 compute_subscript_distance (struct data_dependence_relation *ddr)
1293 conflict_function *cf_a, *cf_b;
1294 affine_fn fn_a, fn_b, diff;
1296 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1298 unsigned int i;
1300 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1302 struct subscript *subscript;
1304 subscript = DDR_SUBSCRIPT (ddr, i);
1305 cf_a = SUB_CONFLICTS_IN_A (subscript);
1306 cf_b = SUB_CONFLICTS_IN_B (subscript);
1308 fn_a = common_affine_function (cf_a);
1309 fn_b = common_affine_function (cf_b);
1310 if (!fn_a.exists () || !fn_b.exists ())
1312 SUB_DISTANCE (subscript) = chrec_dont_know;
1313 return;
1315 diff = affine_fn_minus (fn_a, fn_b);
1317 if (affine_function_constant_p (diff))
1318 SUB_DISTANCE (subscript) = affine_function_base (diff);
1319 else
1320 SUB_DISTANCE (subscript) = chrec_dont_know;
1322 affine_fn_free (diff);
1327 /* Returns the conflict function for "unknown". */
1329 static conflict_function *
1330 conflict_fn_not_known (void)
1332 conflict_function *fn = XCNEW (conflict_function);
1333 fn->n = NOT_KNOWN;
1335 return fn;
1338 /* Returns the conflict function for "independent". */
1340 static conflict_function *
1341 conflict_fn_no_dependence (void)
1343 conflict_function *fn = XCNEW (conflict_function);
1344 fn->n = NO_DEPENDENCE;
1346 return fn;
1349 /* Returns true if the address of OBJ is invariant in LOOP. */
1351 static bool
1352 object_address_invariant_in_loop_p (const struct loop *loop, const_tree obj)
1354 while (handled_component_p (obj))
1356 if (TREE_CODE (obj) == ARRAY_REF)
1358 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1359 need to check the stride and the lower bound of the reference. */
1360 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1361 loop->num)
1362 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 3),
1363 loop->num))
1364 return false;
1366 else if (TREE_CODE (obj) == COMPONENT_REF)
1368 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1369 loop->num))
1370 return false;
1372 obj = TREE_OPERAND (obj, 0);
1375 if (!INDIRECT_REF_P (obj)
1376 && TREE_CODE (obj) != MEM_REF)
1377 return true;
1379 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
1380 loop->num);
1383 /* Returns false if we can prove that data references A and B do not alias,
1384 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
1385 considered. */
1387 bool
1388 dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
1389 bool loop_nest)
1391 tree addr_a = DR_BASE_OBJECT (a);
1392 tree addr_b = DR_BASE_OBJECT (b);
1394 /* If we are not processing a loop nest but scalar code we
1395 do not need to care about possible cross-iteration dependences
1396 and thus can process the full original reference. Do so,
1397 similar to how loop invariant motion applies extra offset-based
1398 disambiguation. */
1399 if (!loop_nest)
1401 aff_tree off1, off2;
1402 widest_int size1, size2;
1403 get_inner_reference_aff (DR_REF (a), &off1, &size1);
1404 get_inner_reference_aff (DR_REF (b), &off2, &size2);
1405 aff_combination_scale (&off1, -1);
1406 aff_combination_add (&off2, &off1);
1407 if (aff_comb_cannot_overlap_p (&off2, size1, size2))
1408 return false;
1411 /* If we had an evolution in a pointer-based MEM_REF BASE_OBJECT we
1412 do not know the size of the base-object. So we cannot do any
1413 offset/overlap based analysis but have to rely on points-to
1414 information only. */
1415 if (TREE_CODE (addr_a) == MEM_REF
1416 && TREE_CODE (TREE_OPERAND (addr_a, 0)) == SSA_NAME)
1418 /* For true dependences we can apply TBAA. */
1419 if (flag_strict_aliasing
1420 && DR_IS_WRITE (a) && DR_IS_READ (b)
1421 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1422 get_alias_set (DR_REF (b))))
1423 return false;
1424 if (TREE_CODE (addr_b) == MEM_REF)
1425 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1426 TREE_OPERAND (addr_b, 0));
1427 else
1428 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1429 build_fold_addr_expr (addr_b));
1431 else if (TREE_CODE (addr_b) == MEM_REF
1432 && TREE_CODE (TREE_OPERAND (addr_b, 0)) == SSA_NAME)
1434 /* For true dependences we can apply TBAA. */
1435 if (flag_strict_aliasing
1436 && DR_IS_WRITE (a) && DR_IS_READ (b)
1437 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1438 get_alias_set (DR_REF (b))))
1439 return false;
1440 if (TREE_CODE (addr_a) == MEM_REF)
1441 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1442 TREE_OPERAND (addr_b, 0));
1443 else
1444 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
1445 TREE_OPERAND (addr_b, 0));
1448 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1449 that is being subsetted in the loop nest. */
1450 if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
1451 return refs_output_dependent_p (addr_a, addr_b);
1452 else if (DR_IS_READ (a) && DR_IS_WRITE (b))
1453 return refs_anti_dependent_p (addr_a, addr_b);
1454 return refs_may_alias_p (addr_a, addr_b);
1457 /* Initialize a data dependence relation between data accesses A and
1458 B. NB_LOOPS is the number of loops surrounding the references: the
1459 size of the classic distance/direction vectors. */
1461 struct data_dependence_relation *
1462 initialize_data_dependence_relation (struct data_reference *a,
1463 struct data_reference *b,
1464 vec<loop_p> loop_nest)
1466 struct data_dependence_relation *res;
1467 unsigned int i;
1469 res = XNEW (struct data_dependence_relation);
1470 DDR_A (res) = a;
1471 DDR_B (res) = b;
1472 DDR_LOOP_NEST (res).create (0);
1473 DDR_REVERSED_P (res) = false;
1474 DDR_SUBSCRIPTS (res).create (0);
1475 DDR_DIR_VECTS (res).create (0);
1476 DDR_DIST_VECTS (res).create (0);
1478 if (a == NULL || b == NULL)
1480 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1481 return res;
1484 /* If the data references do not alias, then they are independent. */
1485 if (!dr_may_alias_p (a, b, loop_nest.exists ()))
1487 DDR_ARE_DEPENDENT (res) = chrec_known;
1488 return res;
1491 /* The case where the references are exactly the same. */
1492 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
1494 if (loop_nest.exists ()
1495 && !object_address_invariant_in_loop_p (loop_nest[0],
1496 DR_BASE_OBJECT (a)))
1498 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1499 return res;
1501 DDR_AFFINE_P (res) = true;
1502 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1503 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1504 DDR_LOOP_NEST (res) = loop_nest;
1505 DDR_INNER_LOOP (res) = 0;
1506 DDR_SELF_REFERENCE (res) = true;
1507 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1509 struct subscript *subscript;
1511 subscript = XNEW (struct subscript);
1512 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1513 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1514 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1515 SUB_DISTANCE (subscript) = chrec_dont_know;
1516 DDR_SUBSCRIPTS (res).safe_push (subscript);
1518 return res;
1521 /* If the references do not access the same object, we do not know
1522 whether they alias or not. */
1523 if (!operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0))
1525 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1526 return res;
1529 /* If the base of the object is not invariant in the loop nest, we cannot
1530 analyze it. TODO -- in fact, it would suffice to record that there may
1531 be arbitrary dependences in the loops where the base object varies. */
1532 if (loop_nest.exists ()
1533 && !object_address_invariant_in_loop_p (loop_nest[0],
1534 DR_BASE_OBJECT (a)))
1536 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1537 return res;
1540 /* If the number of dimensions of the access to not agree we can have
1541 a pointer access to a component of the array element type and an
1542 array access while the base-objects are still the same. Punt. */
1543 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1545 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1546 return res;
1549 DDR_AFFINE_P (res) = true;
1550 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1551 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1552 DDR_LOOP_NEST (res) = loop_nest;
1553 DDR_INNER_LOOP (res) = 0;
1554 DDR_SELF_REFERENCE (res) = false;
1556 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1558 struct subscript *subscript;
1560 subscript = XNEW (struct subscript);
1561 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1562 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1563 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1564 SUB_DISTANCE (subscript) = chrec_dont_know;
1565 DDR_SUBSCRIPTS (res).safe_push (subscript);
1568 return res;
1571 /* Frees memory used by the conflict function F. */
1573 static void
1574 free_conflict_function (conflict_function *f)
1576 unsigned i;
1578 if (CF_NONTRIVIAL_P (f))
1580 for (i = 0; i < f->n; i++)
1581 affine_fn_free (f->fns[i]);
1583 free (f);
1586 /* Frees memory used by SUBSCRIPTS. */
1588 static void
1589 free_subscripts (vec<subscript_p> subscripts)
1591 unsigned i;
1592 subscript_p s;
1594 FOR_EACH_VEC_ELT (subscripts, i, s)
1596 free_conflict_function (s->conflicting_iterations_in_a);
1597 free_conflict_function (s->conflicting_iterations_in_b);
1598 free (s);
1600 subscripts.release ();
1603 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1604 description. */
1606 static inline void
1607 finalize_ddr_dependent (struct data_dependence_relation *ddr,
1608 tree chrec)
1610 DDR_ARE_DEPENDENT (ddr) = chrec;
1611 free_subscripts (DDR_SUBSCRIPTS (ddr));
1612 DDR_SUBSCRIPTS (ddr).create (0);
1615 /* The dependence relation DDR cannot be represented by a distance
1616 vector. */
1618 static inline void
1619 non_affine_dependence_relation (struct data_dependence_relation *ddr)
1621 if (dump_file && (dump_flags & TDF_DETAILS))
1622 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
1624 DDR_AFFINE_P (ddr) = false;
1629 /* This section contains the classic Banerjee tests. */
1631 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1632 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1634 static inline bool
1635 ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1637 return (evolution_function_is_constant_p (chrec_a)
1638 && evolution_function_is_constant_p (chrec_b));
1641 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
1642 variable, i.e., if the SIV (Single Index Variable) test is true. */
1644 static bool
1645 siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1647 if ((evolution_function_is_constant_p (chrec_a)
1648 && evolution_function_is_univariate_p (chrec_b))
1649 || (evolution_function_is_constant_p (chrec_b)
1650 && evolution_function_is_univariate_p (chrec_a)))
1651 return true;
1653 if (evolution_function_is_univariate_p (chrec_a)
1654 && evolution_function_is_univariate_p (chrec_b))
1656 switch (TREE_CODE (chrec_a))
1658 case POLYNOMIAL_CHREC:
1659 switch (TREE_CODE (chrec_b))
1661 case POLYNOMIAL_CHREC:
1662 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
1663 return false;
1665 default:
1666 return true;
1669 default:
1670 return true;
1674 return false;
1677 /* Creates a conflict function with N dimensions. The affine functions
1678 in each dimension follow. */
1680 static conflict_function *
1681 conflict_fn (unsigned n, ...)
1683 unsigned i;
1684 conflict_function *ret = XCNEW (conflict_function);
1685 va_list ap;
1687 gcc_assert (0 < n && n <= MAX_DIM);
1688 va_start (ap, n);
1690 ret->n = n;
1691 for (i = 0; i < n; i++)
1692 ret->fns[i] = va_arg (ap, affine_fn);
1693 va_end (ap);
1695 return ret;
1698 /* Returns constant affine function with value CST. */
1700 static affine_fn
1701 affine_fn_cst (tree cst)
1703 affine_fn fn;
1704 fn.create (1);
1705 fn.quick_push (cst);
1706 return fn;
1709 /* Returns affine function with single variable, CST + COEF * x_DIM. */
1711 static affine_fn
1712 affine_fn_univar (tree cst, unsigned dim, tree coef)
1714 affine_fn fn;
1715 fn.create (dim + 1);
1716 unsigned i;
1718 gcc_assert (dim > 0);
1719 fn.quick_push (cst);
1720 for (i = 1; i < dim; i++)
1721 fn.quick_push (integer_zero_node);
1722 fn.quick_push (coef);
1723 return fn;
1726 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1727 *OVERLAPS_B are initialized to the functions that describe the
1728 relation between the elements accessed twice by CHREC_A and
1729 CHREC_B. For k >= 0, the following property is verified:
1731 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1733 static void
1734 analyze_ziv_subscript (tree chrec_a,
1735 tree chrec_b,
1736 conflict_function **overlaps_a,
1737 conflict_function **overlaps_b,
1738 tree *last_conflicts)
1740 tree type, difference;
1741 dependence_stats.num_ziv++;
1743 if (dump_file && (dump_flags & TDF_DETAILS))
1744 fprintf (dump_file, "(analyze_ziv_subscript \n");
1746 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1747 chrec_a = chrec_convert (type, chrec_a, NULL);
1748 chrec_b = chrec_convert (type, chrec_b, NULL);
1749 difference = chrec_fold_minus (type, chrec_a, chrec_b);
1751 switch (TREE_CODE (difference))
1753 case INTEGER_CST:
1754 if (integer_zerop (difference))
1756 /* The difference is equal to zero: the accessed index
1757 overlaps for each iteration in the loop. */
1758 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1759 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1760 *last_conflicts = chrec_dont_know;
1761 dependence_stats.num_ziv_dependent++;
1763 else
1765 /* The accesses do not overlap. */
1766 *overlaps_a = conflict_fn_no_dependence ();
1767 *overlaps_b = conflict_fn_no_dependence ();
1768 *last_conflicts = integer_zero_node;
1769 dependence_stats.num_ziv_independent++;
1771 break;
1773 default:
1774 /* We're not sure whether the indexes overlap. For the moment,
1775 conservatively answer "don't know". */
1776 if (dump_file && (dump_flags & TDF_DETAILS))
1777 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
1779 *overlaps_a = conflict_fn_not_known ();
1780 *overlaps_b = conflict_fn_not_known ();
1781 *last_conflicts = chrec_dont_know;
1782 dependence_stats.num_ziv_unimplemented++;
1783 break;
1786 if (dump_file && (dump_flags & TDF_DETAILS))
1787 fprintf (dump_file, ")\n");
1790 /* Similar to max_stmt_executions_int, but returns the bound as a tree,
1791 and only if it fits to the int type. If this is not the case, or the
1792 bound on the number of iterations of LOOP could not be derived, returns
1793 chrec_dont_know. */
1795 static tree
1796 max_stmt_executions_tree (struct loop *loop)
1798 widest_int nit;
1800 if (!max_stmt_executions (loop, &nit))
1801 return chrec_dont_know;
1803 if (!wi::fits_to_tree_p (nit, unsigned_type_node))
1804 return chrec_dont_know;
1806 return wide_int_to_tree (unsigned_type_node, nit);
1809 /* Determine whether the CHREC is always positive/negative. If the expression
1810 cannot be statically analyzed, return false, otherwise set the answer into
1811 VALUE. */
1813 static bool
1814 chrec_is_positive (tree chrec, bool *value)
1816 bool value0, value1, value2;
1817 tree end_value, nb_iter;
1819 switch (TREE_CODE (chrec))
1821 case POLYNOMIAL_CHREC:
1822 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
1823 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
1824 return false;
1826 /* FIXME -- overflows. */
1827 if (value0 == value1)
1829 *value = value0;
1830 return true;
1833 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1834 and the proof consists in showing that the sign never
1835 changes during the execution of the loop, from 0 to
1836 loop->nb_iterations. */
1837 if (!evolution_function_is_affine_p (chrec))
1838 return false;
1840 nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
1841 if (chrec_contains_undetermined (nb_iter))
1842 return false;
1844 #if 0
1845 /* TODO -- If the test is after the exit, we may decrease the number of
1846 iterations by one. */
1847 if (after_exit)
1848 nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
1849 #endif
1851 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
1853 if (!chrec_is_positive (end_value, &value2))
1854 return false;
1856 *value = value0;
1857 return value0 == value1;
1859 case INTEGER_CST:
1860 switch (tree_int_cst_sgn (chrec))
1862 case -1:
1863 *value = false;
1864 break;
1865 case 1:
1866 *value = true;
1867 break;
1868 default:
1869 return false;
1871 return true;
1873 default:
1874 return false;
1879 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1880 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1881 *OVERLAPS_B are initialized to the functions that describe the
1882 relation between the elements accessed twice by CHREC_A and
1883 CHREC_B. For k >= 0, the following property is verified:
1885 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1887 static void
1888 analyze_siv_subscript_cst_affine (tree chrec_a,
1889 tree chrec_b,
1890 conflict_function **overlaps_a,
1891 conflict_function **overlaps_b,
1892 tree *last_conflicts)
1894 bool value0, value1, value2;
1895 tree type, difference, tmp;
1897 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1898 chrec_a = chrec_convert (type, chrec_a, NULL);
1899 chrec_b = chrec_convert (type, chrec_b, NULL);
1900 difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
1902 /* Special case overlap in the first iteration. */
1903 if (integer_zerop (difference))
1905 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1906 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1907 *last_conflicts = integer_one_node;
1908 return;
1911 if (!chrec_is_positive (initial_condition (difference), &value0))
1913 if (dump_file && (dump_flags & TDF_DETAILS))
1914 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
1916 dependence_stats.num_siv_unimplemented++;
1917 *overlaps_a = conflict_fn_not_known ();
1918 *overlaps_b = conflict_fn_not_known ();
1919 *last_conflicts = chrec_dont_know;
1920 return;
1922 else
1924 if (value0 == false)
1926 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
1928 if (dump_file && (dump_flags & TDF_DETAILS))
1929 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1931 *overlaps_a = conflict_fn_not_known ();
1932 *overlaps_b = conflict_fn_not_known ();
1933 *last_conflicts = chrec_dont_know;
1934 dependence_stats.num_siv_unimplemented++;
1935 return;
1937 else
1939 if (value1 == true)
1941 /* Example:
1942 chrec_a = 12
1943 chrec_b = {10, +, 1}
1946 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1948 HOST_WIDE_INT numiter;
1949 struct loop *loop = get_chrec_loop (chrec_b);
1951 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1952 tmp = fold_build2 (EXACT_DIV_EXPR, type,
1953 fold_build1 (ABS_EXPR, type, difference),
1954 CHREC_RIGHT (chrec_b));
1955 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1956 *last_conflicts = integer_one_node;
1959 /* Perform weak-zero siv test to see if overlap is
1960 outside the loop bounds. */
1961 numiter = max_stmt_executions_int (loop);
1963 if (numiter >= 0
1964 && compare_tree_int (tmp, numiter) > 0)
1966 free_conflict_function (*overlaps_a);
1967 free_conflict_function (*overlaps_b);
1968 *overlaps_a = conflict_fn_no_dependence ();
1969 *overlaps_b = conflict_fn_no_dependence ();
1970 *last_conflicts = integer_zero_node;
1971 dependence_stats.num_siv_independent++;
1972 return;
1974 dependence_stats.num_siv_dependent++;
1975 return;
1978 /* When the step does not divide the difference, there are
1979 no overlaps. */
1980 else
1982 *overlaps_a = conflict_fn_no_dependence ();
1983 *overlaps_b = conflict_fn_no_dependence ();
1984 *last_conflicts = integer_zero_node;
1985 dependence_stats.num_siv_independent++;
1986 return;
1990 else
1992 /* Example:
1993 chrec_a = 12
1994 chrec_b = {10, +, -1}
1996 In this case, chrec_a will not overlap with chrec_b. */
1997 *overlaps_a = conflict_fn_no_dependence ();
1998 *overlaps_b = conflict_fn_no_dependence ();
1999 *last_conflicts = integer_zero_node;
2000 dependence_stats.num_siv_independent++;
2001 return;
2005 else
2007 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
2009 if (dump_file && (dump_flags & TDF_DETAILS))
2010 fprintf (dump_file, "siv test failed: chrec not positive.\n");
2012 *overlaps_a = conflict_fn_not_known ();
2013 *overlaps_b = conflict_fn_not_known ();
2014 *last_conflicts = chrec_dont_know;
2015 dependence_stats.num_siv_unimplemented++;
2016 return;
2018 else
2020 if (value2 == false)
2022 /* Example:
2023 chrec_a = 3
2024 chrec_b = {10, +, -1}
2026 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2028 HOST_WIDE_INT numiter;
2029 struct loop *loop = get_chrec_loop (chrec_b);
2031 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2032 tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
2033 CHREC_RIGHT (chrec_b));
2034 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
2035 *last_conflicts = integer_one_node;
2037 /* Perform weak-zero siv test to see if overlap is
2038 outside the loop bounds. */
2039 numiter = max_stmt_executions_int (loop);
2041 if (numiter >= 0
2042 && compare_tree_int (tmp, numiter) > 0)
2044 free_conflict_function (*overlaps_a);
2045 free_conflict_function (*overlaps_b);
2046 *overlaps_a = conflict_fn_no_dependence ();
2047 *overlaps_b = conflict_fn_no_dependence ();
2048 *last_conflicts = integer_zero_node;
2049 dependence_stats.num_siv_independent++;
2050 return;
2052 dependence_stats.num_siv_dependent++;
2053 return;
2056 /* When the step does not divide the difference, there
2057 are no overlaps. */
2058 else
2060 *overlaps_a = conflict_fn_no_dependence ();
2061 *overlaps_b = conflict_fn_no_dependence ();
2062 *last_conflicts = integer_zero_node;
2063 dependence_stats.num_siv_independent++;
2064 return;
2067 else
2069 /* Example:
2070 chrec_a = 3
2071 chrec_b = {4, +, 1}
2073 In this case, chrec_a will not overlap with chrec_b. */
2074 *overlaps_a = conflict_fn_no_dependence ();
2075 *overlaps_b = conflict_fn_no_dependence ();
2076 *last_conflicts = integer_zero_node;
2077 dependence_stats.num_siv_independent++;
2078 return;
2085 /* Helper recursive function for initializing the matrix A. Returns
2086 the initial value of CHREC. */
2088 static tree
2089 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2091 gcc_assert (chrec);
2093 switch (TREE_CODE (chrec))
2095 case POLYNOMIAL_CHREC:
2096 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST);
2098 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2099 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2101 case PLUS_EXPR:
2102 case MULT_EXPR:
2103 case MINUS_EXPR:
2105 tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2106 tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
2108 return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
2111 CASE_CONVERT:
2113 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2114 return chrec_convert (chrec_type (chrec), op, NULL);
2117 case BIT_NOT_EXPR:
2119 /* Handle ~X as -1 - X. */
2120 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2121 return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
2122 build_int_cst (TREE_TYPE (chrec), -1), op);
2125 case INTEGER_CST:
2126 return chrec;
2128 default:
2129 gcc_unreachable ();
2130 return NULL_TREE;
2134 #define FLOOR_DIV(x,y) ((x) / (y))
2136 /* Solves the special case of the Diophantine equation:
2137 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2139 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2140 number of iterations that loops X and Y run. The overlaps will be
2141 constructed as evolutions in dimension DIM. */
2143 static void
2144 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2145 affine_fn *overlaps_a,
2146 affine_fn *overlaps_b,
2147 tree *last_conflicts, int dim)
2149 if (((step_a > 0 && step_b > 0)
2150 || (step_a < 0 && step_b < 0)))
2152 int step_overlaps_a, step_overlaps_b;
2153 int gcd_steps_a_b, last_conflict, tau2;
2155 gcd_steps_a_b = gcd (step_a, step_b);
2156 step_overlaps_a = step_b / gcd_steps_a_b;
2157 step_overlaps_b = step_a / gcd_steps_a_b;
2159 if (niter > 0)
2161 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2162 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2163 last_conflict = tau2;
2164 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2166 else
2167 *last_conflicts = chrec_dont_know;
2169 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
2170 build_int_cst (NULL_TREE,
2171 step_overlaps_a));
2172 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
2173 build_int_cst (NULL_TREE,
2174 step_overlaps_b));
2177 else
2179 *overlaps_a = affine_fn_cst (integer_zero_node);
2180 *overlaps_b = affine_fn_cst (integer_zero_node);
2181 *last_conflicts = integer_zero_node;
2185 /* Solves the special case of a Diophantine equation where CHREC_A is
2186 an affine bivariate function, and CHREC_B is an affine univariate
2187 function. For example,
2189 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2191 has the following overlapping functions:
2193 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2194 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2195 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2197 FORNOW: This is a specialized implementation for a case occurring in
2198 a common benchmark. Implement the general algorithm. */
2200 static void
2201 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2202 conflict_function **overlaps_a,
2203 conflict_function **overlaps_b,
2204 tree *last_conflicts)
2206 bool xz_p, yz_p, xyz_p;
2207 int step_x, step_y, step_z;
2208 HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
2209 affine_fn overlaps_a_xz, overlaps_b_xz;
2210 affine_fn overlaps_a_yz, overlaps_b_yz;
2211 affine_fn overlaps_a_xyz, overlaps_b_xyz;
2212 affine_fn ova1, ova2, ovb;
2213 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
2215 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2216 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2217 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2219 niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
2220 niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
2221 niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
2223 if (niter_x < 0 || niter_y < 0 || niter_z < 0)
2225 if (dump_file && (dump_flags & TDF_DETAILS))
2226 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
2228 *overlaps_a = conflict_fn_not_known ();
2229 *overlaps_b = conflict_fn_not_known ();
2230 *last_conflicts = chrec_dont_know;
2231 return;
2234 niter = MIN (niter_x, niter_z);
2235 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2236 &overlaps_a_xz,
2237 &overlaps_b_xz,
2238 &last_conflicts_xz, 1);
2239 niter = MIN (niter_y, niter_z);
2240 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2241 &overlaps_a_yz,
2242 &overlaps_b_yz,
2243 &last_conflicts_yz, 2);
2244 niter = MIN (niter_x, niter_z);
2245 niter = MIN (niter_y, niter);
2246 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2247 &overlaps_a_xyz,
2248 &overlaps_b_xyz,
2249 &last_conflicts_xyz, 3);
2251 xz_p = !integer_zerop (last_conflicts_xz);
2252 yz_p = !integer_zerop (last_conflicts_yz);
2253 xyz_p = !integer_zerop (last_conflicts_xyz);
2255 if (xz_p || yz_p || xyz_p)
2257 ova1 = affine_fn_cst (integer_zero_node);
2258 ova2 = affine_fn_cst (integer_zero_node);
2259 ovb = affine_fn_cst (integer_zero_node);
2260 if (xz_p)
2262 affine_fn t0 = ova1;
2263 affine_fn t2 = ovb;
2265 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
2266 ovb = affine_fn_plus (ovb, overlaps_b_xz);
2267 affine_fn_free (t0);
2268 affine_fn_free (t2);
2269 *last_conflicts = last_conflicts_xz;
2271 if (yz_p)
2273 affine_fn t0 = ova2;
2274 affine_fn t2 = ovb;
2276 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
2277 ovb = affine_fn_plus (ovb, overlaps_b_yz);
2278 affine_fn_free (t0);
2279 affine_fn_free (t2);
2280 *last_conflicts = last_conflicts_yz;
2282 if (xyz_p)
2284 affine_fn t0 = ova1;
2285 affine_fn t2 = ova2;
2286 affine_fn t4 = ovb;
2288 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
2289 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
2290 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
2291 affine_fn_free (t0);
2292 affine_fn_free (t2);
2293 affine_fn_free (t4);
2294 *last_conflicts = last_conflicts_xyz;
2296 *overlaps_a = conflict_fn (2, ova1, ova2);
2297 *overlaps_b = conflict_fn (1, ovb);
2299 else
2301 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2302 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2303 *last_conflicts = integer_zero_node;
2306 affine_fn_free (overlaps_a_xz);
2307 affine_fn_free (overlaps_b_xz);
2308 affine_fn_free (overlaps_a_yz);
2309 affine_fn_free (overlaps_b_yz);
2310 affine_fn_free (overlaps_a_xyz);
2311 affine_fn_free (overlaps_b_xyz);
2314 /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2316 static void
2317 lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
2318 int size)
2320 memcpy (vec2, vec1, size * sizeof (*vec1));
2323 /* Copy the elements of M x N matrix MAT1 to MAT2. */
2325 static void
2326 lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
2327 int m, int n)
2329 int i;
2331 for (i = 0; i < m; i++)
2332 lambda_vector_copy (mat1[i], mat2[i], n);
2335 /* Store the N x N identity matrix in MAT. */
2337 static void
2338 lambda_matrix_id (lambda_matrix mat, int size)
2340 int i, j;
2342 for (i = 0; i < size; i++)
2343 for (j = 0; j < size; j++)
2344 mat[i][j] = (i == j) ? 1 : 0;
2347 /* Return the first nonzero element of vector VEC1 between START and N.
2348 We must have START <= N. Returns N if VEC1 is the zero vector. */
2350 static int
2351 lambda_vector_first_nz (lambda_vector vec1, int n, int start)
2353 int j = start;
2354 while (j < n && vec1[j] == 0)
2355 j++;
2356 return j;
2359 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2360 R2 = R2 + CONST1 * R1. */
2362 static void
2363 lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2, int const1)
2365 int i;
2367 if (const1 == 0)
2368 return;
2370 for (i = 0; i < n; i++)
2371 mat[r2][i] += const1 * mat[r1][i];
2374 /* Swap rows R1 and R2 in matrix MAT. */
2376 static void
2377 lambda_matrix_row_exchange (lambda_matrix mat, int r1, int r2)
2379 lambda_vector row;
2381 row = mat[r1];
2382 mat[r1] = mat[r2];
2383 mat[r2] = row;
2386 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
2387 and store the result in VEC2. */
2389 static void
2390 lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
2391 int size, int const1)
2393 int i;
2395 if (const1 == 0)
2396 lambda_vector_clear (vec2, size);
2397 else
2398 for (i = 0; i < size; i++)
2399 vec2[i] = const1 * vec1[i];
2402 /* Negate vector VEC1 with length SIZE and store it in VEC2. */
2404 static void
2405 lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
2406 int size)
2408 lambda_vector_mult_const (vec1, vec2, size, -1);
2411 /* Negate row R1 of matrix MAT which has N columns. */
2413 static void
2414 lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
2416 lambda_vector_negate (mat[r1], mat[r1], n);
2419 /* Return true if two vectors are equal. */
2421 static bool
2422 lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
2424 int i;
2425 for (i = 0; i < size; i++)
2426 if (vec1[i] != vec2[i])
2427 return false;
2428 return true;
2431 /* Given an M x N integer matrix A, this function determines an M x
2432 M unimodular matrix U, and an M x N echelon matrix S such that
2433 "U.A = S". This decomposition is also known as "right Hermite".
2435 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2436 Restructuring Compilers" Utpal Banerjee. */
2438 static void
2439 lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
2440 lambda_matrix S, lambda_matrix U)
2442 int i, j, i0 = 0;
2444 lambda_matrix_copy (A, S, m, n);
2445 lambda_matrix_id (U, m);
2447 for (j = 0; j < n; j++)
2449 if (lambda_vector_first_nz (S[j], m, i0) < m)
2451 ++i0;
2452 for (i = m - 1; i >= i0; i--)
2454 while (S[i][j] != 0)
2456 int sigma, factor, a, b;
2458 a = S[i-1][j];
2459 b = S[i][j];
2460 sigma = (a * b < 0) ? -1: 1;
2461 a = abs (a);
2462 b = abs (b);
2463 factor = sigma * (a / b);
2465 lambda_matrix_row_add (S, n, i, i-1, -factor);
2466 lambda_matrix_row_exchange (S, i, i-1);
2468 lambda_matrix_row_add (U, m, i, i-1, -factor);
2469 lambda_matrix_row_exchange (U, i, i-1);
2476 /* Determines the overlapping elements due to accesses CHREC_A and
2477 CHREC_B, that are affine functions. This function cannot handle
2478 symbolic evolution functions, ie. when initial conditions are
2479 parameters, because it uses lambda matrices of integers. */
2481 static void
2482 analyze_subscript_affine_affine (tree chrec_a,
2483 tree chrec_b,
2484 conflict_function **overlaps_a,
2485 conflict_function **overlaps_b,
2486 tree *last_conflicts)
2488 unsigned nb_vars_a, nb_vars_b, dim;
2489 HOST_WIDE_INT init_a, init_b, gamma, gcd_alpha_beta;
2490 lambda_matrix A, U, S;
2491 struct obstack scratch_obstack;
2493 if (eq_evolutions_p (chrec_a, chrec_b))
2495 /* The accessed index overlaps for each iteration in the
2496 loop. */
2497 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2498 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2499 *last_conflicts = chrec_dont_know;
2500 return;
2502 if (dump_file && (dump_flags & TDF_DETAILS))
2503 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2505 /* For determining the initial intersection, we have to solve a
2506 Diophantine equation. This is the most time consuming part.
2508 For answering to the question: "Is there a dependence?" we have
2509 to prove that there exists a solution to the Diophantine
2510 equation, and that the solution is in the iteration domain,
2511 i.e. the solution is positive or zero, and that the solution
2512 happens before the upper bound loop.nb_iterations. Otherwise
2513 there is no dependence. This function outputs a description of
2514 the iterations that hold the intersections. */
2516 nb_vars_a = nb_vars_in_chrec (chrec_a);
2517 nb_vars_b = nb_vars_in_chrec (chrec_b);
2519 gcc_obstack_init (&scratch_obstack);
2521 dim = nb_vars_a + nb_vars_b;
2522 U = lambda_matrix_new (dim, dim, &scratch_obstack);
2523 A = lambda_matrix_new (dim, 1, &scratch_obstack);
2524 S = lambda_matrix_new (dim, 1, &scratch_obstack);
2526 init_a = int_cst_value (initialize_matrix_A (A, chrec_a, 0, 1));
2527 init_b = int_cst_value (initialize_matrix_A (A, chrec_b, nb_vars_a, -1));
2528 gamma = init_b - init_a;
2530 /* Don't do all the hard work of solving the Diophantine equation
2531 when we already know the solution: for example,
2532 | {3, +, 1}_1
2533 | {3, +, 4}_2
2534 | gamma = 3 - 3 = 0.
2535 Then the first overlap occurs during the first iterations:
2536 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2538 if (gamma == 0)
2540 if (nb_vars_a == 1 && nb_vars_b == 1)
2542 HOST_WIDE_INT step_a, step_b;
2543 HOST_WIDE_INT niter, niter_a, niter_b;
2544 affine_fn ova, ovb;
2546 niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
2547 niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
2548 niter = MIN (niter_a, niter_b);
2549 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2550 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2552 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2553 &ova, &ovb,
2554 last_conflicts, 1);
2555 *overlaps_a = conflict_fn (1, ova);
2556 *overlaps_b = conflict_fn (1, ovb);
2559 else if (nb_vars_a == 2 && nb_vars_b == 1)
2560 compute_overlap_steps_for_affine_1_2
2561 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2563 else if (nb_vars_a == 1 && nb_vars_b == 2)
2564 compute_overlap_steps_for_affine_1_2
2565 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2567 else
2569 if (dump_file && (dump_flags & TDF_DETAILS))
2570 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
2571 *overlaps_a = conflict_fn_not_known ();
2572 *overlaps_b = conflict_fn_not_known ();
2573 *last_conflicts = chrec_dont_know;
2575 goto end_analyze_subs_aa;
2578 /* U.A = S */
2579 lambda_matrix_right_hermite (A, dim, 1, S, U);
2581 if (S[0][0] < 0)
2583 S[0][0] *= -1;
2584 lambda_matrix_row_negate (U, dim, 0);
2586 gcd_alpha_beta = S[0][0];
2588 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2589 but that is a quite strange case. Instead of ICEing, answer
2590 don't know. */
2591 if (gcd_alpha_beta == 0)
2593 *overlaps_a = conflict_fn_not_known ();
2594 *overlaps_b = conflict_fn_not_known ();
2595 *last_conflicts = chrec_dont_know;
2596 goto end_analyze_subs_aa;
2599 /* The classic "gcd-test". */
2600 if (!int_divides_p (gcd_alpha_beta, gamma))
2602 /* The "gcd-test" has determined that there is no integer
2603 solution, i.e. there is no dependence. */
2604 *overlaps_a = conflict_fn_no_dependence ();
2605 *overlaps_b = conflict_fn_no_dependence ();
2606 *last_conflicts = integer_zero_node;
2609 /* Both access functions are univariate. This includes SIV and MIV cases. */
2610 else if (nb_vars_a == 1 && nb_vars_b == 1)
2612 /* Both functions should have the same evolution sign. */
2613 if (((A[0][0] > 0 && -A[1][0] > 0)
2614 || (A[0][0] < 0 && -A[1][0] < 0)))
2616 /* The solutions are given by:
2618 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2619 | [u21 u22] [y0]
2621 For a given integer t. Using the following variables,
2623 | i0 = u11 * gamma / gcd_alpha_beta
2624 | j0 = u12 * gamma / gcd_alpha_beta
2625 | i1 = u21
2626 | j1 = u22
2628 the solutions are:
2630 | x0 = i0 + i1 * t,
2631 | y0 = j0 + j1 * t. */
2632 HOST_WIDE_INT i0, j0, i1, j1;
2634 i0 = U[0][0] * gamma / gcd_alpha_beta;
2635 j0 = U[0][1] * gamma / gcd_alpha_beta;
2636 i1 = U[1][0];
2637 j1 = U[1][1];
2639 if ((i1 == 0 && i0 < 0)
2640 || (j1 == 0 && j0 < 0))
2642 /* There is no solution.
2643 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2644 falls in here, but for the moment we don't look at the
2645 upper bound of the iteration domain. */
2646 *overlaps_a = conflict_fn_no_dependence ();
2647 *overlaps_b = conflict_fn_no_dependence ();
2648 *last_conflicts = integer_zero_node;
2649 goto end_analyze_subs_aa;
2652 if (i1 > 0 && j1 > 0)
2654 HOST_WIDE_INT niter_a
2655 = max_stmt_executions_int (get_chrec_loop (chrec_a));
2656 HOST_WIDE_INT niter_b
2657 = max_stmt_executions_int (get_chrec_loop (chrec_b));
2658 HOST_WIDE_INT niter = MIN (niter_a, niter_b);
2660 /* (X0, Y0) is a solution of the Diophantine equation:
2661 "chrec_a (X0) = chrec_b (Y0)". */
2662 HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
2663 CEIL (-j0, j1));
2664 HOST_WIDE_INT x0 = i1 * tau1 + i0;
2665 HOST_WIDE_INT y0 = j1 * tau1 + j0;
2667 /* (X1, Y1) is the smallest positive solution of the eq
2668 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2669 first conflict occurs. */
2670 HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
2671 HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
2672 HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
2674 if (niter > 0)
2676 HOST_WIDE_INT tau2 = MIN (FLOOR_DIV (niter - i0, i1),
2677 FLOOR_DIV (niter - j0, j1));
2678 HOST_WIDE_INT last_conflict = tau2 - (x1 - i0)/i1;
2680 /* If the overlap occurs outside of the bounds of the
2681 loop, there is no dependence. */
2682 if (x1 >= niter || y1 >= niter)
2684 *overlaps_a = conflict_fn_no_dependence ();
2685 *overlaps_b = conflict_fn_no_dependence ();
2686 *last_conflicts = integer_zero_node;
2687 goto end_analyze_subs_aa;
2689 else
2690 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2692 else
2693 *last_conflicts = chrec_dont_know;
2695 *overlaps_a
2696 = conflict_fn (1,
2697 affine_fn_univar (build_int_cst (NULL_TREE, x1),
2699 build_int_cst (NULL_TREE, i1)));
2700 *overlaps_b
2701 = conflict_fn (1,
2702 affine_fn_univar (build_int_cst (NULL_TREE, y1),
2704 build_int_cst (NULL_TREE, j1)));
2706 else
2708 /* FIXME: For the moment, the upper bound of the
2709 iteration domain for i and j is not checked. */
2710 if (dump_file && (dump_flags & TDF_DETAILS))
2711 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2712 *overlaps_a = conflict_fn_not_known ();
2713 *overlaps_b = conflict_fn_not_known ();
2714 *last_conflicts = chrec_dont_know;
2717 else
2719 if (dump_file && (dump_flags & TDF_DETAILS))
2720 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2721 *overlaps_a = conflict_fn_not_known ();
2722 *overlaps_b = conflict_fn_not_known ();
2723 *last_conflicts = chrec_dont_know;
2726 else
2728 if (dump_file && (dump_flags & TDF_DETAILS))
2729 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2730 *overlaps_a = conflict_fn_not_known ();
2731 *overlaps_b = conflict_fn_not_known ();
2732 *last_conflicts = chrec_dont_know;
2735 end_analyze_subs_aa:
2736 obstack_free (&scratch_obstack, NULL);
2737 if (dump_file && (dump_flags & TDF_DETAILS))
2739 fprintf (dump_file, " (overlaps_a = ");
2740 dump_conflict_function (dump_file, *overlaps_a);
2741 fprintf (dump_file, ")\n (overlaps_b = ");
2742 dump_conflict_function (dump_file, *overlaps_b);
2743 fprintf (dump_file, "))\n");
2747 /* Returns true when analyze_subscript_affine_affine can be used for
2748 determining the dependence relation between chrec_a and chrec_b,
2749 that contain symbols. This function modifies chrec_a and chrec_b
2750 such that the analysis result is the same, and such that they don't
2751 contain symbols, and then can safely be passed to the analyzer.
2753 Example: The analysis of the following tuples of evolutions produce
2754 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2755 vs. {0, +, 1}_1
2757 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2758 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2761 static bool
2762 can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
2764 tree diff, type, left_a, left_b, right_b;
2766 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
2767 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
2768 /* FIXME: For the moment not handled. Might be refined later. */
2769 return false;
2771 type = chrec_type (*chrec_a);
2772 left_a = CHREC_LEFT (*chrec_a);
2773 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
2774 diff = chrec_fold_minus (type, left_a, left_b);
2776 if (!evolution_function_is_constant_p (diff))
2777 return false;
2779 if (dump_file && (dump_flags & TDF_DETAILS))
2780 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
2782 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
2783 diff, CHREC_RIGHT (*chrec_a));
2784 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
2785 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
2786 build_int_cst (type, 0),
2787 right_b);
2788 return true;
2791 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2792 *OVERLAPS_B are initialized to the functions that describe the
2793 relation between the elements accessed twice by CHREC_A and
2794 CHREC_B. For k >= 0, the following property is verified:
2796 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2798 static void
2799 analyze_siv_subscript (tree chrec_a,
2800 tree chrec_b,
2801 conflict_function **overlaps_a,
2802 conflict_function **overlaps_b,
2803 tree *last_conflicts,
2804 int loop_nest_num)
2806 dependence_stats.num_siv++;
2808 if (dump_file && (dump_flags & TDF_DETAILS))
2809 fprintf (dump_file, "(analyze_siv_subscript \n");
2811 if (evolution_function_is_constant_p (chrec_a)
2812 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2813 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
2814 overlaps_a, overlaps_b, last_conflicts);
2816 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2817 && evolution_function_is_constant_p (chrec_b))
2818 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
2819 overlaps_b, overlaps_a, last_conflicts);
2821 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2822 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2824 if (!chrec_contains_symbols (chrec_a)
2825 && !chrec_contains_symbols (chrec_b))
2827 analyze_subscript_affine_affine (chrec_a, chrec_b,
2828 overlaps_a, overlaps_b,
2829 last_conflicts);
2831 if (CF_NOT_KNOWN_P (*overlaps_a)
2832 || CF_NOT_KNOWN_P (*overlaps_b))
2833 dependence_stats.num_siv_unimplemented++;
2834 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2835 || CF_NO_DEPENDENCE_P (*overlaps_b))
2836 dependence_stats.num_siv_independent++;
2837 else
2838 dependence_stats.num_siv_dependent++;
2840 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
2841 &chrec_b))
2843 analyze_subscript_affine_affine (chrec_a, chrec_b,
2844 overlaps_a, overlaps_b,
2845 last_conflicts);
2847 if (CF_NOT_KNOWN_P (*overlaps_a)
2848 || CF_NOT_KNOWN_P (*overlaps_b))
2849 dependence_stats.num_siv_unimplemented++;
2850 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2851 || CF_NO_DEPENDENCE_P (*overlaps_b))
2852 dependence_stats.num_siv_independent++;
2853 else
2854 dependence_stats.num_siv_dependent++;
2856 else
2857 goto siv_subscript_dontknow;
2860 else
2862 siv_subscript_dontknow:;
2863 if (dump_file && (dump_flags & TDF_DETAILS))
2864 fprintf (dump_file, " siv test failed: unimplemented");
2865 *overlaps_a = conflict_fn_not_known ();
2866 *overlaps_b = conflict_fn_not_known ();
2867 *last_conflicts = chrec_dont_know;
2868 dependence_stats.num_siv_unimplemented++;
2871 if (dump_file && (dump_flags & TDF_DETAILS))
2872 fprintf (dump_file, ")\n");
2875 /* Returns false if we can prove that the greatest common divisor of the steps
2876 of CHREC does not divide CST, false otherwise. */
2878 static bool
2879 gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
2881 HOST_WIDE_INT cd = 0, val;
2882 tree step;
2884 if (!tree_fits_shwi_p (cst))
2885 return true;
2886 val = tree_to_shwi (cst);
2888 while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
2890 step = CHREC_RIGHT (chrec);
2891 if (!tree_fits_shwi_p (step))
2892 return true;
2893 cd = gcd (cd, tree_to_shwi (step));
2894 chrec = CHREC_LEFT (chrec);
2897 return val % cd == 0;
2900 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
2901 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2902 functions that describe the relation between the elements accessed
2903 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2904 is verified:
2906 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2908 static void
2909 analyze_miv_subscript (tree chrec_a,
2910 tree chrec_b,
2911 conflict_function **overlaps_a,
2912 conflict_function **overlaps_b,
2913 tree *last_conflicts,
2914 struct loop *loop_nest)
2916 tree type, difference;
2918 dependence_stats.num_miv++;
2919 if (dump_file && (dump_flags & TDF_DETAILS))
2920 fprintf (dump_file, "(analyze_miv_subscript \n");
2922 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
2923 chrec_a = chrec_convert (type, chrec_a, NULL);
2924 chrec_b = chrec_convert (type, chrec_b, NULL);
2925 difference = chrec_fold_minus (type, chrec_a, chrec_b);
2927 if (eq_evolutions_p (chrec_a, chrec_b))
2929 /* Access functions are the same: all the elements are accessed
2930 in the same order. */
2931 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2932 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2933 *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
2934 dependence_stats.num_miv_dependent++;
2937 else if (evolution_function_is_constant_p (difference)
2938 /* For the moment, the following is verified:
2939 evolution_function_is_affine_multivariate_p (chrec_a,
2940 loop_nest->num) */
2941 && !gcd_of_steps_may_divide_p (chrec_a, difference))
2943 /* testsuite/.../ssa-chrec-33.c
2944 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2946 The difference is 1, and all the evolution steps are multiples
2947 of 2, consequently there are no overlapping elements. */
2948 *overlaps_a = conflict_fn_no_dependence ();
2949 *overlaps_b = conflict_fn_no_dependence ();
2950 *last_conflicts = integer_zero_node;
2951 dependence_stats.num_miv_independent++;
2954 else if (evolution_function_is_affine_multivariate_p (chrec_a, loop_nest->num)
2955 && !chrec_contains_symbols (chrec_a)
2956 && evolution_function_is_affine_multivariate_p (chrec_b, loop_nest->num)
2957 && !chrec_contains_symbols (chrec_b))
2959 /* testsuite/.../ssa-chrec-35.c
2960 {0, +, 1}_2 vs. {0, +, 1}_3
2961 the overlapping elements are respectively located at iterations:
2962 {0, +, 1}_x and {0, +, 1}_x,
2963 in other words, we have the equality:
2964 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2966 Other examples:
2967 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2968 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2970 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2971 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2973 analyze_subscript_affine_affine (chrec_a, chrec_b,
2974 overlaps_a, overlaps_b, last_conflicts);
2976 if (CF_NOT_KNOWN_P (*overlaps_a)
2977 || CF_NOT_KNOWN_P (*overlaps_b))
2978 dependence_stats.num_miv_unimplemented++;
2979 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2980 || CF_NO_DEPENDENCE_P (*overlaps_b))
2981 dependence_stats.num_miv_independent++;
2982 else
2983 dependence_stats.num_miv_dependent++;
2986 else
2988 /* When the analysis is too difficult, answer "don't know". */
2989 if (dump_file && (dump_flags & TDF_DETAILS))
2990 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
2992 *overlaps_a = conflict_fn_not_known ();
2993 *overlaps_b = conflict_fn_not_known ();
2994 *last_conflicts = chrec_dont_know;
2995 dependence_stats.num_miv_unimplemented++;
2998 if (dump_file && (dump_flags & TDF_DETAILS))
2999 fprintf (dump_file, ")\n");
3002 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
3003 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
3004 OVERLAP_ITERATIONS_B are initialized with two functions that
3005 describe the iterations that contain conflicting elements.
3007 Remark: For an integer k >= 0, the following equality is true:
3009 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
3012 static void
3013 analyze_overlapping_iterations (tree chrec_a,
3014 tree chrec_b,
3015 conflict_function **overlap_iterations_a,
3016 conflict_function **overlap_iterations_b,
3017 tree *last_conflicts, struct loop *loop_nest)
3019 unsigned int lnn = loop_nest->num;
3021 dependence_stats.num_subscript_tests++;
3023 if (dump_file && (dump_flags & TDF_DETAILS))
3025 fprintf (dump_file, "(analyze_overlapping_iterations \n");
3026 fprintf (dump_file, " (chrec_a = ");
3027 print_generic_expr (dump_file, chrec_a, 0);
3028 fprintf (dump_file, ")\n (chrec_b = ");
3029 print_generic_expr (dump_file, chrec_b, 0);
3030 fprintf (dump_file, ")\n");
3033 if (chrec_a == NULL_TREE
3034 || chrec_b == NULL_TREE
3035 || chrec_contains_undetermined (chrec_a)
3036 || chrec_contains_undetermined (chrec_b))
3038 dependence_stats.num_subscript_undetermined++;
3040 *overlap_iterations_a = conflict_fn_not_known ();
3041 *overlap_iterations_b = conflict_fn_not_known ();
3044 /* If they are the same chrec, and are affine, they overlap
3045 on every iteration. */
3046 else if (eq_evolutions_p (chrec_a, chrec_b)
3047 && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3048 || operand_equal_p (chrec_a, chrec_b, 0)))
3050 dependence_stats.num_same_subscript_function++;
3051 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3052 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3053 *last_conflicts = chrec_dont_know;
3056 /* If they aren't the same, and aren't affine, we can't do anything
3057 yet. */
3058 else if ((chrec_contains_symbols (chrec_a)
3059 || chrec_contains_symbols (chrec_b))
3060 && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3061 || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
3063 dependence_stats.num_subscript_undetermined++;
3064 *overlap_iterations_a = conflict_fn_not_known ();
3065 *overlap_iterations_b = conflict_fn_not_known ();
3068 else if (ziv_subscript_p (chrec_a, chrec_b))
3069 analyze_ziv_subscript (chrec_a, chrec_b,
3070 overlap_iterations_a, overlap_iterations_b,
3071 last_conflicts);
3073 else if (siv_subscript_p (chrec_a, chrec_b))
3074 analyze_siv_subscript (chrec_a, chrec_b,
3075 overlap_iterations_a, overlap_iterations_b,
3076 last_conflicts, lnn);
3078 else
3079 analyze_miv_subscript (chrec_a, chrec_b,
3080 overlap_iterations_a, overlap_iterations_b,
3081 last_conflicts, loop_nest);
3083 if (dump_file && (dump_flags & TDF_DETAILS))
3085 fprintf (dump_file, " (overlap_iterations_a = ");
3086 dump_conflict_function (dump_file, *overlap_iterations_a);
3087 fprintf (dump_file, ")\n (overlap_iterations_b = ");
3088 dump_conflict_function (dump_file, *overlap_iterations_b);
3089 fprintf (dump_file, "))\n");
3093 /* Helper function for uniquely inserting distance vectors. */
3095 static void
3096 save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
3098 unsigned i;
3099 lambda_vector v;
3101 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, v)
3102 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
3103 return;
3105 DDR_DIST_VECTS (ddr).safe_push (dist_v);
3108 /* Helper function for uniquely inserting direction vectors. */
3110 static void
3111 save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
3113 unsigned i;
3114 lambda_vector v;
3116 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), i, v)
3117 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
3118 return;
3120 DDR_DIR_VECTS (ddr).safe_push (dir_v);
3123 /* Add a distance of 1 on all the loops outer than INDEX. If we
3124 haven't yet determined a distance for this outer loop, push a new
3125 distance vector composed of the previous distance, and a distance
3126 of 1 for this outer loop. Example:
3128 | loop_1
3129 | loop_2
3130 | A[10]
3131 | endloop_2
3132 | endloop_1
3134 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3135 save (0, 1), then we have to save (1, 0). */
3137 static void
3138 add_outer_distances (struct data_dependence_relation *ddr,
3139 lambda_vector dist_v, int index)
3141 /* For each outer loop where init_v is not set, the accesses are
3142 in dependence of distance 1 in the loop. */
3143 while (--index >= 0)
3145 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3146 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3147 save_v[index] = 1;
3148 save_dist_v (ddr, save_v);
3152 /* Return false when fail to represent the data dependence as a
3153 distance vector. INIT_B is set to true when a component has been
3154 added to the distance vector DIST_V. INDEX_CARRY is then set to
3155 the index in DIST_V that carries the dependence. */
3157 static bool
3158 build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
3159 struct data_reference *ddr_a,
3160 struct data_reference *ddr_b,
3161 lambda_vector dist_v, bool *init_b,
3162 int *index_carry)
3164 unsigned i;
3165 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3167 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3169 tree access_fn_a, access_fn_b;
3170 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3172 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3174 non_affine_dependence_relation (ddr);
3175 return false;
3178 access_fn_a = DR_ACCESS_FN (ddr_a, i);
3179 access_fn_b = DR_ACCESS_FN (ddr_b, i);
3181 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3182 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3184 int dist, index;
3185 int var_a = CHREC_VARIABLE (access_fn_a);
3186 int var_b = CHREC_VARIABLE (access_fn_b);
3188 if (var_a != var_b
3189 || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3191 non_affine_dependence_relation (ddr);
3192 return false;
3195 dist = int_cst_value (SUB_DISTANCE (subscript));
3196 index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
3197 *index_carry = MIN (index, *index_carry);
3199 /* This is the subscript coupling test. If we have already
3200 recorded a distance for this loop (a distance coming from
3201 another subscript), it should be the same. For example,
3202 in the following code, there is no dependence:
3204 | loop i = 0, N, 1
3205 | T[i+1][i] = ...
3206 | ... = T[i][i]
3207 | endloop
3209 if (init_v[index] != 0 && dist_v[index] != dist)
3211 finalize_ddr_dependent (ddr, chrec_known);
3212 return false;
3215 dist_v[index] = dist;
3216 init_v[index] = 1;
3217 *init_b = true;
3219 else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
3221 /* This can be for example an affine vs. constant dependence
3222 (T[i] vs. T[3]) that is not an affine dependence and is
3223 not representable as a distance vector. */
3224 non_affine_dependence_relation (ddr);
3225 return false;
3229 return true;
3232 /* Return true when the DDR contains only constant access functions. */
3234 static bool
3235 constant_access_functions (const struct data_dependence_relation *ddr)
3237 unsigned i;
3239 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3240 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr), i))
3241 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr), i)))
3242 return false;
3244 return true;
3247 /* Helper function for the case where DDR_A and DDR_B are the same
3248 multivariate access function with a constant step. For an example
3249 see pr34635-1.c. */
3251 static void
3252 add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
3254 int x_1, x_2;
3255 tree c_1 = CHREC_LEFT (c_2);
3256 tree c_0 = CHREC_LEFT (c_1);
3257 lambda_vector dist_v;
3258 int v1, v2, cd;
3260 /* Polynomials with more than 2 variables are not handled yet. When
3261 the evolution steps are parameters, it is not possible to
3262 represent the dependence using classical distance vectors. */
3263 if (TREE_CODE (c_0) != INTEGER_CST
3264 || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
3265 || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
3267 DDR_AFFINE_P (ddr) = false;
3268 return;
3271 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
3272 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
3274 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3275 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3276 v1 = int_cst_value (CHREC_RIGHT (c_1));
3277 v2 = int_cst_value (CHREC_RIGHT (c_2));
3278 cd = gcd (v1, v2);
3279 v1 /= cd;
3280 v2 /= cd;
3282 if (v2 < 0)
3284 v2 = -v2;
3285 v1 = -v1;
3288 dist_v[x_1] = v2;
3289 dist_v[x_2] = -v1;
3290 save_dist_v (ddr, dist_v);
3292 add_outer_distances (ddr, dist_v, x_1);
3295 /* Helper function for the case where DDR_A and DDR_B are the same
3296 access functions. */
3298 static void
3299 add_other_self_distances (struct data_dependence_relation *ddr)
3301 lambda_vector dist_v;
3302 unsigned i;
3303 int index_carry = DDR_NB_LOOPS (ddr);
3305 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3307 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
3309 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
3311 if (!evolution_function_is_univariate_p (access_fun))
3313 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
3315 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3316 return;
3319 access_fun = DR_ACCESS_FN (DDR_A (ddr), 0);
3321 if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
3322 add_multivariate_self_dist (ddr, access_fun);
3323 else
3324 /* The evolution step is not constant: it varies in
3325 the outer loop, so this cannot be represented by a
3326 distance vector. For example in pr34635.c the
3327 evolution is {0, +, {0, +, 4}_1}_2. */
3328 DDR_AFFINE_P (ddr) = false;
3330 return;
3333 index_carry = MIN (index_carry,
3334 index_in_loop_nest (CHREC_VARIABLE (access_fun),
3335 DDR_LOOP_NEST (ddr)));
3339 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3340 add_outer_distances (ddr, dist_v, index_carry);
3343 static void
3344 insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
3346 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3348 dist_v[DDR_INNER_LOOP (ddr)] = 1;
3349 save_dist_v (ddr, dist_v);
3352 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
3353 is the case for example when access functions are the same and
3354 equal to a constant, as in:
3356 | loop_1
3357 | A[3] = ...
3358 | ... = A[3]
3359 | endloop_1
3361 in which case the distance vectors are (0) and (1). */
3363 static void
3364 add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
3366 unsigned i, j;
3368 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3370 subscript_p sub = DDR_SUBSCRIPT (ddr, i);
3371 conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
3372 conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
3374 for (j = 0; j < ca->n; j++)
3375 if (affine_function_zero_p (ca->fns[j]))
3377 insert_innermost_unit_dist_vector (ddr);
3378 return;
3381 for (j = 0; j < cb->n; j++)
3382 if (affine_function_zero_p (cb->fns[j]))
3384 insert_innermost_unit_dist_vector (ddr);
3385 return;
3390 /* Compute the classic per loop distance vector. DDR is the data
3391 dependence relation to build a vector from. Return false when fail
3392 to represent the data dependence as a distance vector. */
3394 static bool
3395 build_classic_dist_vector (struct data_dependence_relation *ddr,
3396 struct loop *loop_nest)
3398 bool init_b = false;
3399 int index_carry = DDR_NB_LOOPS (ddr);
3400 lambda_vector dist_v;
3402 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3403 return false;
3405 if (same_access_functions (ddr))
3407 /* Save the 0 vector. */
3408 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3409 save_dist_v (ddr, dist_v);
3411 if (constant_access_functions (ddr))
3412 add_distance_for_zero_overlaps (ddr);
3414 if (DDR_NB_LOOPS (ddr) > 1)
3415 add_other_self_distances (ddr);
3417 return true;
3420 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3421 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3422 dist_v, &init_b, &index_carry))
3423 return false;
3425 /* Save the distance vector if we initialized one. */
3426 if (init_b)
3428 /* Verify a basic constraint: classic distance vectors should
3429 always be lexicographically positive.
3431 Data references are collected in the order of execution of
3432 the program, thus for the following loop
3434 | for (i = 1; i < 100; i++)
3435 | for (j = 1; j < 100; j++)
3437 | t = T[j+1][i-1]; // A
3438 | T[j][i] = t + 2; // B
3441 references are collected following the direction of the wind:
3442 A then B. The data dependence tests are performed also
3443 following this order, such that we're looking at the distance
3444 separating the elements accessed by A from the elements later
3445 accessed by B. But in this example, the distance returned by
3446 test_dep (A, B) is lexicographically negative (-1, 1), that
3447 means that the access A occurs later than B with respect to
3448 the outer loop, ie. we're actually looking upwind. In this
3449 case we solve test_dep (B, A) looking downwind to the
3450 lexicographically positive solution, that returns the
3451 distance vector (1, -1). */
3452 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3454 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3455 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3456 loop_nest))
3457 return false;
3458 compute_subscript_distance (ddr);
3459 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3460 save_v, &init_b, &index_carry))
3461 return false;
3462 save_dist_v (ddr, save_v);
3463 DDR_REVERSED_P (ddr) = true;
3465 /* In this case there is a dependence forward for all the
3466 outer loops:
3468 | for (k = 1; k < 100; k++)
3469 | for (i = 1; i < 100; i++)
3470 | for (j = 1; j < 100; j++)
3472 | t = T[j+1][i-1]; // A
3473 | T[j][i] = t + 2; // B
3476 the vectors are:
3477 (0, 1, -1)
3478 (1, 1, -1)
3479 (1, -1, 1)
3481 if (DDR_NB_LOOPS (ddr) > 1)
3483 add_outer_distances (ddr, save_v, index_carry);
3484 add_outer_distances (ddr, dist_v, index_carry);
3487 else
3489 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3490 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3492 if (DDR_NB_LOOPS (ddr) > 1)
3494 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3496 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr),
3497 DDR_A (ddr), loop_nest))
3498 return false;
3499 compute_subscript_distance (ddr);
3500 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3501 opposite_v, &init_b,
3502 &index_carry))
3503 return false;
3505 save_dist_v (ddr, save_v);
3506 add_outer_distances (ddr, dist_v, index_carry);
3507 add_outer_distances (ddr, opposite_v, index_carry);
3509 else
3510 save_dist_v (ddr, save_v);
3513 else
3515 /* There is a distance of 1 on all the outer loops: Example:
3516 there is a dependence of distance 1 on loop_1 for the array A.
3518 | loop_1
3519 | A[5] = ...
3520 | endloop
3522 add_outer_distances (ddr, dist_v,
3523 lambda_vector_first_nz (dist_v,
3524 DDR_NB_LOOPS (ddr), 0));
3527 if (dump_file && (dump_flags & TDF_DETAILS))
3529 unsigned i;
3531 fprintf (dump_file, "(build_classic_dist_vector\n");
3532 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3534 fprintf (dump_file, " dist_vector = (");
3535 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3536 DDR_NB_LOOPS (ddr));
3537 fprintf (dump_file, " )\n");
3539 fprintf (dump_file, ")\n");
3542 return true;
3545 /* Return the direction for a given distance.
3546 FIXME: Computing dir this way is suboptimal, since dir can catch
3547 cases that dist is unable to represent. */
3549 static inline enum data_dependence_direction
3550 dir_from_dist (int dist)
3552 if (dist > 0)
3553 return dir_positive;
3554 else if (dist < 0)
3555 return dir_negative;
3556 else
3557 return dir_equal;
3560 /* Compute the classic per loop direction vector. DDR is the data
3561 dependence relation to build a vector from. */
3563 static void
3564 build_classic_dir_vector (struct data_dependence_relation *ddr)
3566 unsigned i, j;
3567 lambda_vector dist_v;
3569 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
3571 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3573 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3574 dir_v[j] = dir_from_dist (dist_v[j]);
3576 save_dir_v (ddr, dir_v);
3580 /* Helper function. Returns true when there is a dependence between
3581 data references DRA and DRB. */
3583 static bool
3584 subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
3585 struct data_reference *dra,
3586 struct data_reference *drb,
3587 struct loop *loop_nest)
3589 unsigned int i;
3590 tree last_conflicts;
3591 struct subscript *subscript;
3592 tree res = NULL_TREE;
3594 for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
3596 conflict_function *overlaps_a, *overlaps_b;
3598 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3599 DR_ACCESS_FN (drb, i),
3600 &overlaps_a, &overlaps_b,
3601 &last_conflicts, loop_nest);
3603 if (SUB_CONFLICTS_IN_A (subscript))
3604 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
3605 if (SUB_CONFLICTS_IN_B (subscript))
3606 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
3608 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3609 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3610 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3612 /* If there is any undetermined conflict function we have to
3613 give a conservative answer in case we cannot prove that
3614 no dependence exists when analyzing another subscript. */
3615 if (CF_NOT_KNOWN_P (overlaps_a)
3616 || CF_NOT_KNOWN_P (overlaps_b))
3618 res = chrec_dont_know;
3619 continue;
3622 /* When there is a subscript with no dependence we can stop. */
3623 else if (CF_NO_DEPENDENCE_P (overlaps_a)
3624 || CF_NO_DEPENDENCE_P (overlaps_b))
3626 res = chrec_known;
3627 break;
3631 if (res == NULL_TREE)
3632 return true;
3634 if (res == chrec_known)
3635 dependence_stats.num_dependence_independent++;
3636 else
3637 dependence_stats.num_dependence_undetermined++;
3638 finalize_ddr_dependent (ddr, res);
3639 return false;
3642 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
3644 static void
3645 subscript_dependence_tester (struct data_dependence_relation *ddr,
3646 struct loop *loop_nest)
3648 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr), loop_nest))
3649 dependence_stats.num_dependence_dependent++;
3651 compute_subscript_distance (ddr);
3652 if (build_classic_dist_vector (ddr, loop_nest))
3653 build_classic_dir_vector (ddr);
3656 /* Returns true when all the access functions of A are affine or
3657 constant with respect to LOOP_NEST. */
3659 static bool
3660 access_functions_are_affine_or_constant_p (const struct data_reference *a,
3661 const struct loop *loop_nest)
3663 unsigned int i;
3664 vec<tree> fns = DR_ACCESS_FNS (a);
3665 tree t;
3667 FOR_EACH_VEC_ELT (fns, i, t)
3668 if (!evolution_function_is_invariant_p (t, loop_nest->num)
3669 && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
3670 return false;
3672 return true;
3675 /* Initializes an equation for an OMEGA problem using the information
3676 contained in the ACCESS_FUN. Returns true when the operation
3677 succeeded.
3679 PB is the omega constraint system.
3680 EQ is the number of the equation to be initialized.
3681 OFFSET is used for shifting the variables names in the constraints:
3682 a constrain is composed of 2 * the number of variables surrounding
3683 dependence accesses. OFFSET is set either to 0 for the first n variables,
3684 then it is set to n.
3685 ACCESS_FUN is expected to be an affine chrec. */
3687 static bool
3688 init_omega_eq_with_af (omega_pb pb, unsigned eq,
3689 unsigned int offset, tree access_fun,
3690 struct data_dependence_relation *ddr)
3692 switch (TREE_CODE (access_fun))
3694 case POLYNOMIAL_CHREC:
3696 tree left = CHREC_LEFT (access_fun);
3697 tree right = CHREC_RIGHT (access_fun);
3698 int var = CHREC_VARIABLE (access_fun);
3699 unsigned var_idx;
3701 if (TREE_CODE (right) != INTEGER_CST)
3702 return false;
3704 var_idx = index_in_loop_nest (var, DDR_LOOP_NEST (ddr));
3705 pb->eqs[eq].coef[offset + var_idx + 1] = int_cst_value (right);
3707 /* Compute the innermost loop index. */
3708 DDR_INNER_LOOP (ddr) = MAX (DDR_INNER_LOOP (ddr), var_idx);
3710 if (offset == 0)
3711 pb->eqs[eq].coef[var_idx + DDR_NB_LOOPS (ddr) + 1]
3712 += int_cst_value (right);
3714 switch (TREE_CODE (left))
3716 case POLYNOMIAL_CHREC:
3717 return init_omega_eq_with_af (pb, eq, offset, left, ddr);
3719 case INTEGER_CST:
3720 pb->eqs[eq].coef[0] += int_cst_value (left);
3721 return true;
3723 default:
3724 return false;
3728 case INTEGER_CST:
3729 pb->eqs[eq].coef[0] += int_cst_value (access_fun);
3730 return true;
3732 default:
3733 return false;
3737 /* As explained in the comments preceding init_omega_for_ddr, we have
3738 to set up a system for each loop level, setting outer loops
3739 variation to zero, and current loop variation to positive or zero.
3740 Save each lexico positive distance vector. */
3742 static void
3743 omega_extract_distance_vectors (omega_pb pb,
3744 struct data_dependence_relation *ddr)
3746 int eq, geq;
3747 unsigned i, j;
3748 struct loop *loopi, *loopj;
3749 enum omega_result res;
3751 /* Set a new problem for each loop in the nest. The basis is the
3752 problem that we have initialized until now. On top of this we
3753 add new constraints. */
3754 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3755 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3757 int dist = 0;
3758 omega_pb copy = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr),
3759 DDR_NB_LOOPS (ddr));
3761 omega_copy_problem (copy, pb);
3763 /* For all the outer loops "loop_j", add "dj = 0". */
3764 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3766 eq = omega_add_zero_eq (copy, omega_black);
3767 copy->eqs[eq].coef[j + 1] = 1;
3770 /* For "loop_i", add "0 <= di". */
3771 geq = omega_add_zero_geq (copy, omega_black);
3772 copy->geqs[geq].coef[i + 1] = 1;
3774 /* Reduce the constraint system, and test that the current
3775 problem is feasible. */
3776 res = omega_simplify_problem (copy);
3777 if (res == omega_false
3778 || res == omega_unknown
3779 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3780 goto next_problem;
3782 for (eq = 0; eq < copy->num_subs; eq++)
3783 if (copy->subs[eq].key == (int) i + 1)
3785 dist = copy->subs[eq].coef[0];
3786 goto found_dist;
3789 if (dist == 0)
3791 /* Reinitialize problem... */
3792 omega_copy_problem (copy, pb);
3793 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3795 eq = omega_add_zero_eq (copy, omega_black);
3796 copy->eqs[eq].coef[j + 1] = 1;
3799 /* ..., but this time "di = 1". */
3800 eq = omega_add_zero_eq (copy, omega_black);
3801 copy->eqs[eq].coef[i + 1] = 1;
3802 copy->eqs[eq].coef[0] = -1;
3804 res = omega_simplify_problem (copy);
3805 if (res == omega_false
3806 || res == omega_unknown
3807 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3808 goto next_problem;
3810 for (eq = 0; eq < copy->num_subs; eq++)
3811 if (copy->subs[eq].key == (int) i + 1)
3813 dist = copy->subs[eq].coef[0];
3814 goto found_dist;
3818 found_dist:;
3819 /* Save the lexicographically positive distance vector. */
3820 if (dist >= 0)
3822 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3823 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3825 dist_v[i] = dist;
3827 for (eq = 0; eq < copy->num_subs; eq++)
3828 if (copy->subs[eq].key > 0)
3830 dist = copy->subs[eq].coef[0];
3831 dist_v[copy->subs[eq].key - 1] = dist;
3834 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3835 dir_v[j] = dir_from_dist (dist_v[j]);
3837 save_dist_v (ddr, dist_v);
3838 save_dir_v (ddr, dir_v);
3841 next_problem:;
3842 omega_free_problem (copy);
3846 /* This is called for each subscript of a tuple of data references:
3847 insert an equality for representing the conflicts. */
3849 static bool
3850 omega_setup_subscript (tree access_fun_a, tree access_fun_b,
3851 struct data_dependence_relation *ddr,
3852 omega_pb pb, bool *maybe_dependent)
3854 int eq;
3855 tree type = signed_type_for_types (TREE_TYPE (access_fun_a),
3856 TREE_TYPE (access_fun_b));
3857 tree fun_a = chrec_convert (type, access_fun_a, NULL);
3858 tree fun_b = chrec_convert (type, access_fun_b, NULL);
3859 tree difference = chrec_fold_minus (type, fun_a, fun_b);
3860 tree minus_one;
3862 /* When the fun_a - fun_b is not constant, the dependence is not
3863 captured by the classic distance vector representation. */
3864 if (TREE_CODE (difference) != INTEGER_CST)
3865 return false;
3867 /* ZIV test. */
3868 if (ziv_subscript_p (fun_a, fun_b) && !integer_zerop (difference))
3870 /* There is no dependence. */
3871 *maybe_dependent = false;
3872 return true;
3875 minus_one = build_int_cst (type, -1);
3876 fun_b = chrec_fold_multiply (type, fun_b, minus_one);
3878 eq = omega_add_zero_eq (pb, omega_black);
3879 if (!init_omega_eq_with_af (pb, eq, DDR_NB_LOOPS (ddr), fun_a, ddr)
3880 || !init_omega_eq_with_af (pb, eq, 0, fun_b, ddr))
3881 /* There is probably a dependence, but the system of
3882 constraints cannot be built: answer "don't know". */
3883 return false;
3885 /* GCD test. */
3886 if (DDR_NB_LOOPS (ddr) != 0 && pb->eqs[eq].coef[0]
3887 && !int_divides_p (lambda_vector_gcd
3888 ((lambda_vector) &(pb->eqs[eq].coef[1]),
3889 2 * DDR_NB_LOOPS (ddr)),
3890 pb->eqs[eq].coef[0]))
3892 /* There is no dependence. */
3893 *maybe_dependent = false;
3894 return true;
3897 return true;
3900 /* Helper function, same as init_omega_for_ddr but specialized for
3901 data references A and B. */
3903 static bool
3904 init_omega_for_ddr_1 (struct data_reference *dra, struct data_reference *drb,
3905 struct data_dependence_relation *ddr,
3906 omega_pb pb, bool *maybe_dependent)
3908 unsigned i;
3909 int ineq;
3910 struct loop *loopi;
3911 unsigned nb_loops = DDR_NB_LOOPS (ddr);
3913 /* Insert an equality per subscript. */
3914 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3916 if (!omega_setup_subscript (DR_ACCESS_FN (dra, i), DR_ACCESS_FN (drb, i),
3917 ddr, pb, maybe_dependent))
3918 return false;
3919 else if (*maybe_dependent == false)
3921 /* There is no dependence. */
3922 DDR_ARE_DEPENDENT (ddr) = chrec_known;
3923 return true;
3927 /* Insert inequalities: constraints corresponding to the iteration
3928 domain, i.e. the loops surrounding the references "loop_x" and
3929 the distance variables "dx". The layout of the OMEGA
3930 representation is as follows:
3931 - coef[0] is the constant
3932 - coef[1..nb_loops] are the protected variables that will not be
3933 removed by the solver: the "dx"
3934 - coef[nb_loops + 1, 2*nb_loops] are the loop variables: "loop_x".
3936 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3937 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3939 HOST_WIDE_INT nbi = max_stmt_executions_int (loopi);
3941 /* 0 <= loop_x */
3942 ineq = omega_add_zero_geq (pb, omega_black);
3943 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3945 /* 0 <= loop_x + dx */
3946 ineq = omega_add_zero_geq (pb, omega_black);
3947 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3948 pb->geqs[ineq].coef[i + 1] = 1;
3950 if (nbi != -1)
3952 /* loop_x <= nb_iters */
3953 ineq = omega_add_zero_geq (pb, omega_black);
3954 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3955 pb->geqs[ineq].coef[0] = nbi;
3957 /* loop_x + dx <= nb_iters */
3958 ineq = omega_add_zero_geq (pb, omega_black);
3959 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3960 pb->geqs[ineq].coef[i + 1] = -1;
3961 pb->geqs[ineq].coef[0] = nbi;
3963 /* A step "dx" bigger than nb_iters is not feasible, so
3964 add "0 <= nb_iters + dx", */
3965 ineq = omega_add_zero_geq (pb, omega_black);
3966 pb->geqs[ineq].coef[i + 1] = 1;
3967 pb->geqs[ineq].coef[0] = nbi;
3968 /* and "dx <= nb_iters". */
3969 ineq = omega_add_zero_geq (pb, omega_black);
3970 pb->geqs[ineq].coef[i + 1] = -1;
3971 pb->geqs[ineq].coef[0] = nbi;
3975 omega_extract_distance_vectors (pb, ddr);
3977 return true;
3980 /* Sets up the Omega dependence problem for the data dependence
3981 relation DDR. Returns false when the constraint system cannot be
3982 built, ie. when the test answers "don't know". Returns true
3983 otherwise, and when independence has been proved (using one of the
3984 trivial dependence test), set MAYBE_DEPENDENT to false, otherwise
3985 set MAYBE_DEPENDENT to true.
3987 Example: for setting up the dependence system corresponding to the
3988 conflicting accesses
3990 | loop_i
3991 | loop_j
3992 | A[i, i+1] = ...
3993 | ... A[2*j, 2*(i + j)]
3994 | endloop_j
3995 | endloop_i
3997 the following constraints come from the iteration domain:
3999 0 <= i <= Ni
4000 0 <= i + di <= Ni
4001 0 <= j <= Nj
4002 0 <= j + dj <= Nj
4004 where di, dj are the distance variables. The constraints
4005 representing the conflicting elements are:
4007 i = 2 * (j + dj)
4008 i + 1 = 2 * (i + di + j + dj)
4010 For asking that the resulting distance vector (di, dj) be
4011 lexicographically positive, we insert the constraint "di >= 0". If
4012 "di = 0" in the solution, we fix that component to zero, and we
4013 look at the inner loops: we set a new problem where all the outer
4014 loop distances are zero, and fix this inner component to be
4015 positive. When one of the components is positive, we save that
4016 distance, and set a new problem where the distance on this loop is
4017 zero, searching for other distances in the inner loops. Here is
4018 the classic example that illustrates that we have to set for each
4019 inner loop a new problem:
4021 | loop_1
4022 | loop_2
4023 | A[10]
4024 | endloop_2
4025 | endloop_1
4027 we have to save two distances (1, 0) and (0, 1).
4029 Given two array references, refA and refB, we have to set the
4030 dependence problem twice, refA vs. refB and refB vs. refA, and we
4031 cannot do a single test, as refB might occur before refA in the
4032 inner loops, and the contrary when considering outer loops: ex.
4034 | loop_0
4035 | loop_1
4036 | loop_2
4037 | T[{1,+,1}_2][{1,+,1}_1] // refA
4038 | T[{2,+,1}_2][{0,+,1}_1] // refB
4039 | endloop_2
4040 | endloop_1
4041 | endloop_0
4043 refB touches the elements in T before refA, and thus for the same
4044 loop_0 refB precedes refA: ie. the distance vector (0, 1, -1)
4045 but for successive loop_0 iterations, we have (1, -1, 1)
4047 The Omega solver expects the distance variables ("di" in the
4048 previous example) to come first in the constraint system (as
4049 variables to be protected, or "safe" variables), the constraint
4050 system is built using the following layout:
4052 "cst | distance vars | index vars".
4055 static bool
4056 init_omega_for_ddr (struct data_dependence_relation *ddr,
4057 bool *maybe_dependent)
4059 omega_pb pb;
4060 bool res = false;
4062 *maybe_dependent = true;
4064 if (same_access_functions (ddr))
4066 unsigned j;
4067 lambda_vector dir_v;
4069 /* Save the 0 vector. */
4070 save_dist_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
4071 dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4072 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
4073 dir_v[j] = dir_equal;
4074 save_dir_v (ddr, dir_v);
4076 /* Save the dependences carried by outer loops. */
4077 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4078 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4079 maybe_dependent);
4080 omega_free_problem (pb);
4081 return res;
4084 /* Omega expects the protected variables (those that have to be kept
4085 after elimination) to appear first in the constraint system.
4086 These variables are the distance variables. In the following
4087 initialization we declare NB_LOOPS safe variables, and the total
4088 number of variables for the constraint system is 2*NB_LOOPS. */
4089 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4090 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4091 maybe_dependent);
4092 omega_free_problem (pb);
4094 /* Stop computation if not decidable, or no dependence. */
4095 if (res == false || *maybe_dependent == false)
4096 return res;
4098 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4099 res = init_omega_for_ddr_1 (DDR_B (ddr), DDR_A (ddr), ddr, pb,
4100 maybe_dependent);
4101 omega_free_problem (pb);
4103 return res;
4106 /* Return true when DDR contains the same information as that stored
4107 in DIR_VECTS and in DIST_VECTS, return false otherwise. */
4109 static bool
4110 ddr_consistent_p (FILE *file,
4111 struct data_dependence_relation *ddr,
4112 vec<lambda_vector> dist_vects,
4113 vec<lambda_vector> dir_vects)
4115 unsigned int i, j;
4117 /* If dump_file is set, output there. */
4118 if (dump_file && (dump_flags & TDF_DETAILS))
4119 file = dump_file;
4121 if (dist_vects.length () != DDR_NUM_DIST_VECTS (ddr))
4123 lambda_vector b_dist_v;
4124 fprintf (file, "\n(Number of distance vectors differ: Banerjee has %d, Omega has %d.\n",
4125 dist_vects.length (),
4126 DDR_NUM_DIST_VECTS (ddr));
4128 fprintf (file, "Banerjee dist vectors:\n");
4129 FOR_EACH_VEC_ELT (dist_vects, i, b_dist_v)
4130 print_lambda_vector (file, b_dist_v, DDR_NB_LOOPS (ddr));
4132 fprintf (file, "Omega dist vectors:\n");
4133 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4134 print_lambda_vector (file, DDR_DIST_VECT (ddr, i), DDR_NB_LOOPS (ddr));
4136 fprintf (file, "data dependence relation:\n");
4137 dump_data_dependence_relation (file, ddr);
4139 fprintf (file, ")\n");
4140 return false;
4143 if (dir_vects.length () != DDR_NUM_DIR_VECTS (ddr))
4145 fprintf (file, "\n(Number of direction vectors differ: Banerjee has %d, Omega has %d.)\n",
4146 dir_vects.length (),
4147 DDR_NUM_DIR_VECTS (ddr));
4148 return false;
4151 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4153 lambda_vector a_dist_v;
4154 lambda_vector b_dist_v = DDR_DIST_VECT (ddr, i);
4156 /* Distance vectors are not ordered in the same way in the DDR
4157 and in the DIST_VECTS: search for a matching vector. */
4158 FOR_EACH_VEC_ELT (dist_vects, j, a_dist_v)
4159 if (lambda_vector_equal (a_dist_v, b_dist_v, DDR_NB_LOOPS (ddr)))
4160 break;
4162 if (j == dist_vects.length ())
4164 fprintf (file, "\n(Dist vectors from the first dependence analyzer:\n");
4165 print_dist_vectors (file, dist_vects, DDR_NB_LOOPS (ddr));
4166 fprintf (file, "not found in Omega dist vectors:\n");
4167 print_dist_vectors (file, DDR_DIST_VECTS (ddr), DDR_NB_LOOPS (ddr));
4168 fprintf (file, "data dependence relation:\n");
4169 dump_data_dependence_relation (file, ddr);
4170 fprintf (file, ")\n");
4174 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
4176 lambda_vector a_dir_v;
4177 lambda_vector b_dir_v = DDR_DIR_VECT (ddr, i);
4179 /* Direction vectors are not ordered in the same way in the DDR
4180 and in the DIR_VECTS: search for a matching vector. */
4181 FOR_EACH_VEC_ELT (dir_vects, j, a_dir_v)
4182 if (lambda_vector_equal (a_dir_v, b_dir_v, DDR_NB_LOOPS (ddr)))
4183 break;
4185 if (j == dist_vects.length ())
4187 fprintf (file, "\n(Dir vectors from the first dependence analyzer:\n");
4188 print_dir_vectors (file, dir_vects, DDR_NB_LOOPS (ddr));
4189 fprintf (file, "not found in Omega dir vectors:\n");
4190 print_dir_vectors (file, DDR_DIR_VECTS (ddr), DDR_NB_LOOPS (ddr));
4191 fprintf (file, "data dependence relation:\n");
4192 dump_data_dependence_relation (file, ddr);
4193 fprintf (file, ")\n");
4197 return true;
4200 /* This computes the affine dependence relation between A and B with
4201 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
4202 independence between two accesses, while CHREC_DONT_KNOW is used
4203 for representing the unknown relation.
4205 Note that it is possible to stop the computation of the dependence
4206 relation the first time we detect a CHREC_KNOWN element for a given
4207 subscript. */
4209 void
4210 compute_affine_dependence (struct data_dependence_relation *ddr,
4211 struct loop *loop_nest)
4213 struct data_reference *dra = DDR_A (ddr);
4214 struct data_reference *drb = DDR_B (ddr);
4216 if (dump_file && (dump_flags & TDF_DETAILS))
4218 fprintf (dump_file, "(compute_affine_dependence\n");
4219 fprintf (dump_file, " stmt_a: ");
4220 print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
4221 fprintf (dump_file, " stmt_b: ");
4222 print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
4225 /* Analyze only when the dependence relation is not yet known. */
4226 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4228 dependence_stats.num_dependence_tests++;
4230 if (access_functions_are_affine_or_constant_p (dra, loop_nest)
4231 && access_functions_are_affine_or_constant_p (drb, loop_nest))
4233 subscript_dependence_tester (ddr, loop_nest);
4235 if (flag_check_data_deps)
4237 /* Dump the dependences from the first algorithm. */
4238 if (dump_file && (dump_flags & TDF_DETAILS))
4240 fprintf (dump_file, "\n\nBanerjee Analyzer\n");
4241 dump_data_dependence_relation (dump_file, ddr);
4244 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4246 bool maybe_dependent;
4247 vec<lambda_vector> dir_vects, dist_vects;
4249 /* Save the result of the first DD analyzer. */
4250 dist_vects = DDR_DIST_VECTS (ddr);
4251 dir_vects = DDR_DIR_VECTS (ddr);
4253 /* Reset the information. */
4254 DDR_DIST_VECTS (ddr).create (0);
4255 DDR_DIR_VECTS (ddr).create (0);
4257 /* Compute the same information using Omega. */
4258 if (!init_omega_for_ddr (ddr, &maybe_dependent))
4259 goto csys_dont_know;
4261 if (dump_file && (dump_flags & TDF_DETAILS))
4263 fprintf (dump_file, "Omega Analyzer\n");
4264 dump_data_dependence_relation (dump_file, ddr);
4267 /* Check that we get the same information. */
4268 if (maybe_dependent)
4269 gcc_assert (ddr_consistent_p (stderr, ddr, dist_vects,
4270 dir_vects));
4275 /* As a last case, if the dependence cannot be determined, or if
4276 the dependence is considered too difficult to determine, answer
4277 "don't know". */
4278 else
4280 csys_dont_know:;
4281 dependence_stats.num_dependence_undetermined++;
4283 if (dump_file && (dump_flags & TDF_DETAILS))
4285 fprintf (dump_file, "Data ref a:\n");
4286 dump_data_reference (dump_file, dra);
4287 fprintf (dump_file, "Data ref b:\n");
4288 dump_data_reference (dump_file, drb);
4289 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
4291 finalize_ddr_dependent (ddr, chrec_dont_know);
4295 if (dump_file && (dump_flags & TDF_DETAILS))
4297 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4298 fprintf (dump_file, ") -> no dependence\n");
4299 else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
4300 fprintf (dump_file, ") -> dependence analysis failed\n");
4301 else
4302 fprintf (dump_file, ")\n");
4306 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
4307 the data references in DATAREFS, in the LOOP_NEST. When
4308 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
4309 relations. Return true when successful, i.e. data references number
4310 is small enough to be handled. */
4312 bool
4313 compute_all_dependences (vec<data_reference_p> datarefs,
4314 vec<ddr_p> *dependence_relations,
4315 vec<loop_p> loop_nest,
4316 bool compute_self_and_rr)
4318 struct data_dependence_relation *ddr;
4319 struct data_reference *a, *b;
4320 unsigned int i, j;
4322 if ((int) datarefs.length ()
4323 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
4325 struct data_dependence_relation *ddr;
4327 /* Insert a single relation into dependence_relations:
4328 chrec_dont_know. */
4329 ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
4330 dependence_relations->safe_push (ddr);
4331 return false;
4334 FOR_EACH_VEC_ELT (datarefs, i, a)
4335 for (j = i + 1; datarefs.iterate (j, &b); j++)
4336 if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
4338 ddr = initialize_data_dependence_relation (a, b, loop_nest);
4339 dependence_relations->safe_push (ddr);
4340 if (loop_nest.exists ())
4341 compute_affine_dependence (ddr, loop_nest[0]);
4344 if (compute_self_and_rr)
4345 FOR_EACH_VEC_ELT (datarefs, i, a)
4347 ddr = initialize_data_dependence_relation (a, a, loop_nest);
4348 dependence_relations->safe_push (ddr);
4349 if (loop_nest.exists ())
4350 compute_affine_dependence (ddr, loop_nest[0]);
4353 return true;
4356 /* Describes a location of a memory reference. */
4358 typedef struct data_ref_loc_d
4360 /* The memory reference. */
4361 tree ref;
4363 /* True if the memory reference is read. */
4364 bool is_read;
4365 } data_ref_loc;
4368 /* Stores the locations of memory references in STMT to REFERENCES. Returns
4369 true if STMT clobbers memory, false otherwise. */
4371 static bool
4372 get_references_in_stmt (gimple stmt, vec<data_ref_loc, va_heap> *references)
4374 bool clobbers_memory = false;
4375 data_ref_loc ref;
4376 tree op0, op1;
4377 enum gimple_code stmt_code = gimple_code (stmt);
4379 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4380 As we cannot model data-references to not spelled out
4381 accesses give up if they may occur. */
4382 if (stmt_code == GIMPLE_CALL
4383 && !(gimple_call_flags (stmt) & ECF_CONST))
4385 /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
4386 if (gimple_call_internal_p (stmt))
4387 switch (gimple_call_internal_fn (stmt))
4389 case IFN_GOMP_SIMD_LANE:
4391 struct loop *loop = gimple_bb (stmt)->loop_father;
4392 tree uid = gimple_call_arg (stmt, 0);
4393 gcc_assert (TREE_CODE (uid) == SSA_NAME);
4394 if (loop == NULL
4395 || loop->simduid != SSA_NAME_VAR (uid))
4396 clobbers_memory = true;
4397 break;
4399 case IFN_MASK_LOAD:
4400 case IFN_MASK_STORE:
4401 break;
4402 default:
4403 clobbers_memory = true;
4404 break;
4406 else
4407 clobbers_memory = true;
4409 else if (stmt_code == GIMPLE_ASM
4410 && (gimple_asm_volatile_p (stmt) || gimple_vuse (stmt)))
4411 clobbers_memory = true;
4413 if (!gimple_vuse (stmt))
4414 return clobbers_memory;
4416 if (stmt_code == GIMPLE_ASSIGN)
4418 tree base;
4419 op0 = gimple_assign_lhs (stmt);
4420 op1 = gimple_assign_rhs1 (stmt);
4422 if (DECL_P (op1)
4423 || (REFERENCE_CLASS_P (op1)
4424 && (base = get_base_address (op1))
4425 && TREE_CODE (base) != SSA_NAME))
4427 ref.ref = op1;
4428 ref.is_read = true;
4429 references->safe_push (ref);
4432 else if (stmt_code == GIMPLE_CALL)
4434 unsigned i, n;
4436 ref.is_read = false;
4437 if (gimple_call_internal_p (stmt))
4438 switch (gimple_call_internal_fn (stmt))
4440 case IFN_MASK_LOAD:
4441 if (gimple_call_lhs (stmt) == NULL_TREE)
4442 break;
4443 ref.is_read = true;
4444 case IFN_MASK_STORE:
4445 ref.ref = fold_build2 (MEM_REF,
4446 ref.is_read
4447 ? TREE_TYPE (gimple_call_lhs (stmt))
4448 : TREE_TYPE (gimple_call_arg (stmt, 3)),
4449 gimple_call_arg (stmt, 0),
4450 gimple_call_arg (stmt, 1));
4451 references->safe_push (ref);
4452 return false;
4453 default:
4454 break;
4457 op0 = gimple_call_lhs (stmt);
4458 n = gimple_call_num_args (stmt);
4459 for (i = 0; i < n; i++)
4461 op1 = gimple_call_arg (stmt, i);
4463 if (DECL_P (op1)
4464 || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
4466 ref.ref = op1;
4467 ref.is_read = true;
4468 references->safe_push (ref);
4472 else
4473 return clobbers_memory;
4475 if (op0
4476 && (DECL_P (op0)
4477 || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
4479 ref.ref = op0;
4480 ref.is_read = false;
4481 references->safe_push (ref);
4483 return clobbers_memory;
4486 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
4487 reference, returns false, otherwise returns true. NEST is the outermost
4488 loop of the loop nest in which the references should be analyzed. */
4490 bool
4491 find_data_references_in_stmt (struct loop *nest, gimple stmt,
4492 vec<data_reference_p> *datarefs)
4494 unsigned i;
4495 auto_vec<data_ref_loc, 2> references;
4496 data_ref_loc *ref;
4497 bool ret = true;
4498 data_reference_p dr;
4500 if (get_references_in_stmt (stmt, &references))
4501 return false;
4503 FOR_EACH_VEC_ELT (references, i, ref)
4505 dr = create_data_ref (nest, loop_containing_stmt (stmt),
4506 ref->ref, stmt, ref->is_read);
4507 gcc_assert (dr != NULL);
4508 datarefs->safe_push (dr);
4510 references.release ();
4511 return ret;
4514 /* Stores the data references in STMT to DATAREFS. If there is an
4515 unanalyzable reference, returns false, otherwise returns true.
4516 NEST is the outermost loop of the loop nest in which the references
4517 should be instantiated, LOOP is the loop in which the references
4518 should be analyzed. */
4520 bool
4521 graphite_find_data_references_in_stmt (loop_p nest, loop_p loop, gimple stmt,
4522 vec<data_reference_p> *datarefs)
4524 unsigned i;
4525 auto_vec<data_ref_loc, 2> references;
4526 data_ref_loc *ref;
4527 bool ret = true;
4528 data_reference_p dr;
4530 if (get_references_in_stmt (stmt, &references))
4531 return false;
4533 FOR_EACH_VEC_ELT (references, i, ref)
4535 dr = create_data_ref (nest, loop, ref->ref, stmt, ref->is_read);
4536 gcc_assert (dr != NULL);
4537 datarefs->safe_push (dr);
4540 references.release ();
4541 return ret;
4544 /* Search the data references in LOOP, and record the information into
4545 DATAREFS. Returns chrec_dont_know when failing to analyze a
4546 difficult case, returns NULL_TREE otherwise. */
4548 tree
4549 find_data_references_in_bb (struct loop *loop, basic_block bb,
4550 vec<data_reference_p> *datarefs)
4552 gimple_stmt_iterator bsi;
4554 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4556 gimple stmt = gsi_stmt (bsi);
4558 if (!find_data_references_in_stmt (loop, stmt, datarefs))
4560 struct data_reference *res;
4561 res = XCNEW (struct data_reference);
4562 datarefs->safe_push (res);
4564 return chrec_dont_know;
4568 return NULL_TREE;
4571 /* Search the data references in LOOP, and record the information into
4572 DATAREFS. Returns chrec_dont_know when failing to analyze a
4573 difficult case, returns NULL_TREE otherwise.
4575 TODO: This function should be made smarter so that it can handle address
4576 arithmetic as if they were array accesses, etc. */
4578 tree
4579 find_data_references_in_loop (struct loop *loop,
4580 vec<data_reference_p> *datarefs)
4582 basic_block bb, *bbs;
4583 unsigned int i;
4585 bbs = get_loop_body_in_dom_order (loop);
4587 for (i = 0; i < loop->num_nodes; i++)
4589 bb = bbs[i];
4591 if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
4593 free (bbs);
4594 return chrec_dont_know;
4597 free (bbs);
4599 return NULL_TREE;
4602 /* Recursive helper function. */
4604 static bool
4605 find_loop_nest_1 (struct loop *loop, vec<loop_p> *loop_nest)
4607 /* Inner loops of the nest should not contain siblings. Example:
4608 when there are two consecutive loops,
4610 | loop_0
4611 | loop_1
4612 | A[{0, +, 1}_1]
4613 | endloop_1
4614 | loop_2
4615 | A[{0, +, 1}_2]
4616 | endloop_2
4617 | endloop_0
4619 the dependence relation cannot be captured by the distance
4620 abstraction. */
4621 if (loop->next)
4622 return false;
4624 loop_nest->safe_push (loop);
4625 if (loop->inner)
4626 return find_loop_nest_1 (loop->inner, loop_nest);
4627 return true;
4630 /* Return false when the LOOP is not well nested. Otherwise return
4631 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4632 contain the loops from the outermost to the innermost, as they will
4633 appear in the classic distance vector. */
4635 bool
4636 find_loop_nest (struct loop *loop, vec<loop_p> *loop_nest)
4638 loop_nest->safe_push (loop);
4639 if (loop->inner)
4640 return find_loop_nest_1 (loop->inner, loop_nest);
4641 return true;
4644 /* Returns true when the data dependences have been computed, false otherwise.
4645 Given a loop nest LOOP, the following vectors are returned:
4646 DATAREFS is initialized to all the array elements contained in this loop,
4647 DEPENDENCE_RELATIONS contains the relations between the data references.
4648 Compute read-read and self relations if
4649 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4651 bool
4652 compute_data_dependences_for_loop (struct loop *loop,
4653 bool compute_self_and_read_read_dependences,
4654 vec<loop_p> *loop_nest,
4655 vec<data_reference_p> *datarefs,
4656 vec<ddr_p> *dependence_relations)
4658 bool res = true;
4660 memset (&dependence_stats, 0, sizeof (dependence_stats));
4662 /* If the loop nest is not well formed, or one of the data references
4663 is not computable, give up without spending time to compute other
4664 dependences. */
4665 if (!loop
4666 || !find_loop_nest (loop, loop_nest)
4667 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
4668 || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
4669 compute_self_and_read_read_dependences))
4670 res = false;
4672 if (dump_file && (dump_flags & TDF_STATS))
4674 fprintf (dump_file, "Dependence tester statistics:\n");
4676 fprintf (dump_file, "Number of dependence tests: %d\n",
4677 dependence_stats.num_dependence_tests);
4678 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
4679 dependence_stats.num_dependence_dependent);
4680 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
4681 dependence_stats.num_dependence_independent);
4682 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
4683 dependence_stats.num_dependence_undetermined);
4685 fprintf (dump_file, "Number of subscript tests: %d\n",
4686 dependence_stats.num_subscript_tests);
4687 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
4688 dependence_stats.num_subscript_undetermined);
4689 fprintf (dump_file, "Number of same subscript function: %d\n",
4690 dependence_stats.num_same_subscript_function);
4692 fprintf (dump_file, "Number of ziv tests: %d\n",
4693 dependence_stats.num_ziv);
4694 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4695 dependence_stats.num_ziv_dependent);
4696 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4697 dependence_stats.num_ziv_independent);
4698 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
4699 dependence_stats.num_ziv_unimplemented);
4701 fprintf (dump_file, "Number of siv tests: %d\n",
4702 dependence_stats.num_siv);
4703 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4704 dependence_stats.num_siv_dependent);
4705 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4706 dependence_stats.num_siv_independent);
4707 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4708 dependence_stats.num_siv_unimplemented);
4710 fprintf (dump_file, "Number of miv tests: %d\n",
4711 dependence_stats.num_miv);
4712 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4713 dependence_stats.num_miv_dependent);
4714 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4715 dependence_stats.num_miv_independent);
4716 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4717 dependence_stats.num_miv_unimplemented);
4720 return res;
4723 /* Returns true when the data dependences for the basic block BB have been
4724 computed, false otherwise.
4725 DATAREFS is initialized to all the array elements contained in this basic
4726 block, DEPENDENCE_RELATIONS contains the relations between the data
4727 references. Compute read-read and self relations if
4728 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4729 bool
4730 compute_data_dependences_for_bb (basic_block bb,
4731 bool compute_self_and_read_read_dependences,
4732 vec<data_reference_p> *datarefs,
4733 vec<ddr_p> *dependence_relations)
4735 if (find_data_references_in_bb (NULL, bb, datarefs) == chrec_dont_know)
4736 return false;
4738 return compute_all_dependences (*datarefs, dependence_relations, vNULL,
4739 compute_self_and_read_read_dependences);
4742 /* Entry point (for testing only). Analyze all the data references
4743 and the dependence relations in LOOP.
4745 The data references are computed first.
4747 A relation on these nodes is represented by a complete graph. Some
4748 of the relations could be of no interest, thus the relations can be
4749 computed on demand.
4751 In the following function we compute all the relations. This is
4752 just a first implementation that is here for:
4753 - for showing how to ask for the dependence relations,
4754 - for the debugging the whole dependence graph,
4755 - for the dejagnu testcases and maintenance.
4757 It is possible to ask only for a part of the graph, avoiding to
4758 compute the whole dependence graph. The computed dependences are
4759 stored in a knowledge base (KB) such that later queries don't
4760 recompute the same information. The implementation of this KB is
4761 transparent to the optimizer, and thus the KB can be changed with a
4762 more efficient implementation, or the KB could be disabled. */
4763 static void
4764 analyze_all_data_dependences (struct loop *loop)
4766 unsigned int i;
4767 int nb_data_refs = 10;
4768 vec<data_reference_p> datarefs;
4769 datarefs.create (nb_data_refs);
4770 vec<ddr_p> dependence_relations;
4771 dependence_relations.create (nb_data_refs * nb_data_refs);
4772 vec<loop_p> loop_nest;
4773 loop_nest.create (3);
4775 /* Compute DDs on the whole function. */
4776 compute_data_dependences_for_loop (loop, false, &loop_nest, &datarefs,
4777 &dependence_relations);
4779 if (dump_file)
4781 dump_data_dependence_relations (dump_file, dependence_relations);
4782 fprintf (dump_file, "\n\n");
4784 if (dump_flags & TDF_DETAILS)
4785 dump_dist_dir_vectors (dump_file, dependence_relations);
4787 if (dump_flags & TDF_STATS)
4789 unsigned nb_top_relations = 0;
4790 unsigned nb_bot_relations = 0;
4791 unsigned nb_chrec_relations = 0;
4792 struct data_dependence_relation *ddr;
4794 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4796 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
4797 nb_top_relations++;
4799 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4800 nb_bot_relations++;
4802 else
4803 nb_chrec_relations++;
4806 gather_stats_on_scev_database ();
4810 loop_nest.release ();
4811 free_dependence_relations (dependence_relations);
4812 free_data_refs (datarefs);
4815 /* Computes all the data dependences and check that the results of
4816 several analyzers are the same. */
4818 void
4819 tree_check_data_deps (void)
4821 struct loop *loop_nest;
4823 FOR_EACH_LOOP (loop_nest, 0)
4824 analyze_all_data_dependences (loop_nest);
4827 /* Free the memory used by a data dependence relation DDR. */
4829 void
4830 free_dependence_relation (struct data_dependence_relation *ddr)
4832 if (ddr == NULL)
4833 return;
4835 if (DDR_SUBSCRIPTS (ddr).exists ())
4836 free_subscripts (DDR_SUBSCRIPTS (ddr));
4837 DDR_DIST_VECTS (ddr).release ();
4838 DDR_DIR_VECTS (ddr).release ();
4840 free (ddr);
4843 /* Free the memory used by the data dependence relations from
4844 DEPENDENCE_RELATIONS. */
4846 void
4847 free_dependence_relations (vec<ddr_p> dependence_relations)
4849 unsigned int i;
4850 struct data_dependence_relation *ddr;
4852 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4853 if (ddr)
4854 free_dependence_relation (ddr);
4856 dependence_relations.release ();
4859 /* Free the memory used by the data references from DATAREFS. */
4861 void
4862 free_data_refs (vec<data_reference_p> datarefs)
4864 unsigned int i;
4865 struct data_reference *dr;
4867 FOR_EACH_VEC_ELT (datarefs, i, dr)
4868 free_data_ref (dr);
4869 datarefs.release ();