HSA: reduce dump output w/o -details flag
[official-gcc.git] / gcc / tree-data-ref.c
blobd6d9ffcaa3424992c7203712a9c16969bc50ad02
1 /* Data references and dependences detectors.
2 Copyright (C) 2003-2016 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 "backend.h"
80 #include "rtl.h"
81 #include "tree.h"
82 #include "gimple.h"
83 #include "gimple-pretty-print.h"
84 #include "alias.h"
85 #include "fold-const.h"
86 #include "expr.h"
87 #include "gimple-iterator.h"
88 #include "tree-ssa-loop-niter.h"
89 #include "tree-ssa-loop.h"
90 #include "tree-ssa.h"
91 #include "cfgloop.h"
92 #include "tree-data-ref.h"
93 #include "tree-scalar-evolution.h"
94 #include "dumpfile.h"
95 #include "tree-affine.h"
96 #include "params.h"
98 static struct datadep_stats
100 int num_dependence_tests;
101 int num_dependence_dependent;
102 int num_dependence_independent;
103 int num_dependence_undetermined;
105 int num_subscript_tests;
106 int num_subscript_undetermined;
107 int num_same_subscript_function;
109 int num_ziv;
110 int num_ziv_independent;
111 int num_ziv_dependent;
112 int num_ziv_unimplemented;
114 int num_siv;
115 int num_siv_independent;
116 int num_siv_dependent;
117 int num_siv_unimplemented;
119 int num_miv;
120 int num_miv_independent;
121 int num_miv_dependent;
122 int num_miv_unimplemented;
123 } dependence_stats;
125 static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
126 struct data_reference *,
127 struct data_reference *,
128 struct loop *);
129 /* Returns true iff A divides B. */
131 static inline bool
132 tree_fold_divides_p (const_tree a, const_tree b)
134 gcc_assert (TREE_CODE (a) == INTEGER_CST);
135 gcc_assert (TREE_CODE (b) == INTEGER_CST);
136 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a));
139 /* Returns true iff A divides B. */
141 static inline bool
142 int_divides_p (int a, int b)
144 return ((b % a) == 0);
149 /* Dump into FILE all the data references from DATAREFS. */
151 static void
152 dump_data_references (FILE *file, vec<data_reference_p> datarefs)
154 unsigned int i;
155 struct data_reference *dr;
157 FOR_EACH_VEC_ELT (datarefs, i, dr)
158 dump_data_reference (file, dr);
161 /* Unified dump into FILE all the data references from DATAREFS. */
163 DEBUG_FUNCTION void
164 debug (vec<data_reference_p> &ref)
166 dump_data_references (stderr, ref);
169 DEBUG_FUNCTION void
170 debug (vec<data_reference_p> *ptr)
172 if (ptr)
173 debug (*ptr);
174 else
175 fprintf (stderr, "<nil>\n");
179 /* Dump into STDERR all the data references from DATAREFS. */
181 DEBUG_FUNCTION void
182 debug_data_references (vec<data_reference_p> datarefs)
184 dump_data_references (stderr, datarefs);
187 /* Print to STDERR the data_reference DR. */
189 DEBUG_FUNCTION void
190 debug_data_reference (struct data_reference *dr)
192 dump_data_reference (stderr, dr);
195 /* Dump function for a DATA_REFERENCE structure. */
197 void
198 dump_data_reference (FILE *outf,
199 struct data_reference *dr)
201 unsigned int i;
203 fprintf (outf, "#(Data Ref: \n");
204 fprintf (outf, "# bb: %d \n", gimple_bb (DR_STMT (dr))->index);
205 fprintf (outf, "# stmt: ");
206 print_gimple_stmt (outf, DR_STMT (dr), 0, 0);
207 fprintf (outf, "# ref: ");
208 print_generic_stmt (outf, DR_REF (dr), 0);
209 fprintf (outf, "# base_object: ");
210 print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
212 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
214 fprintf (outf, "# Access function %d: ", i);
215 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
217 fprintf (outf, "#)\n");
220 /* Unified dump function for a DATA_REFERENCE structure. */
222 DEBUG_FUNCTION void
223 debug (data_reference &ref)
225 dump_data_reference (stderr, &ref);
228 DEBUG_FUNCTION void
229 debug (data_reference *ptr)
231 if (ptr)
232 debug (*ptr);
233 else
234 fprintf (stderr, "<nil>\n");
238 /* Dumps the affine function described by FN to the file OUTF. */
240 DEBUG_FUNCTION void
241 dump_affine_function (FILE *outf, affine_fn fn)
243 unsigned i;
244 tree coef;
246 print_generic_expr (outf, fn[0], TDF_SLIM);
247 for (i = 1; fn.iterate (i, &coef); i++)
249 fprintf (outf, " + ");
250 print_generic_expr (outf, coef, TDF_SLIM);
251 fprintf (outf, " * x_%u", i);
255 /* Dumps the conflict function CF to the file OUTF. */
257 DEBUG_FUNCTION void
258 dump_conflict_function (FILE *outf, conflict_function *cf)
260 unsigned i;
262 if (cf->n == NO_DEPENDENCE)
263 fprintf (outf, "no dependence");
264 else if (cf->n == NOT_KNOWN)
265 fprintf (outf, "not known");
266 else
268 for (i = 0; i < cf->n; i++)
270 if (i != 0)
271 fprintf (outf, " ");
272 fprintf (outf, "[");
273 dump_affine_function (outf, cf->fns[i]);
274 fprintf (outf, "]");
279 /* Dump function for a SUBSCRIPT structure. */
281 DEBUG_FUNCTION void
282 dump_subscript (FILE *outf, struct subscript *subscript)
284 conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
286 fprintf (outf, "\n (subscript \n");
287 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
288 dump_conflict_function (outf, cf);
289 if (CF_NONTRIVIAL_P (cf))
291 tree last_iteration = SUB_LAST_CONFLICT (subscript);
292 fprintf (outf, "\n last_conflict: ");
293 print_generic_expr (outf, last_iteration, 0);
296 cf = SUB_CONFLICTS_IN_B (subscript);
297 fprintf (outf, "\n iterations_that_access_an_element_twice_in_B: ");
298 dump_conflict_function (outf, cf);
299 if (CF_NONTRIVIAL_P (cf))
301 tree last_iteration = SUB_LAST_CONFLICT (subscript);
302 fprintf (outf, "\n last_conflict: ");
303 print_generic_expr (outf, last_iteration, 0);
306 fprintf (outf, "\n (Subscript distance: ");
307 print_generic_expr (outf, SUB_DISTANCE (subscript), 0);
308 fprintf (outf, " ))\n");
311 /* Print the classic direction vector DIRV to OUTF. */
313 DEBUG_FUNCTION void
314 print_direction_vector (FILE *outf,
315 lambda_vector dirv,
316 int length)
318 int eq;
320 for (eq = 0; eq < length; eq++)
322 enum data_dependence_direction dir = ((enum data_dependence_direction)
323 dirv[eq]);
325 switch (dir)
327 case dir_positive:
328 fprintf (outf, " +");
329 break;
330 case dir_negative:
331 fprintf (outf, " -");
332 break;
333 case dir_equal:
334 fprintf (outf, " =");
335 break;
336 case dir_positive_or_equal:
337 fprintf (outf, " +=");
338 break;
339 case dir_positive_or_negative:
340 fprintf (outf, " +-");
341 break;
342 case dir_negative_or_equal:
343 fprintf (outf, " -=");
344 break;
345 case dir_star:
346 fprintf (outf, " *");
347 break;
348 default:
349 fprintf (outf, "indep");
350 break;
353 fprintf (outf, "\n");
356 /* Print a vector of direction vectors. */
358 DEBUG_FUNCTION void
359 print_dir_vectors (FILE *outf, vec<lambda_vector> dir_vects,
360 int length)
362 unsigned j;
363 lambda_vector v;
365 FOR_EACH_VEC_ELT (dir_vects, j, v)
366 print_direction_vector (outf, v, length);
369 /* Print out a vector VEC of length N to OUTFILE. */
371 DEBUG_FUNCTION void
372 print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
374 int i;
376 for (i = 0; i < n; i++)
377 fprintf (outfile, "%3d ", vector[i]);
378 fprintf (outfile, "\n");
381 /* Print a vector of distance vectors. */
383 DEBUG_FUNCTION void
384 print_dist_vectors (FILE *outf, vec<lambda_vector> dist_vects,
385 int length)
387 unsigned j;
388 lambda_vector v;
390 FOR_EACH_VEC_ELT (dist_vects, j, v)
391 print_lambda_vector (outf, v, length);
394 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
396 DEBUG_FUNCTION void
397 dump_data_dependence_relation (FILE *outf,
398 struct data_dependence_relation *ddr)
400 struct data_reference *dra, *drb;
402 fprintf (outf, "(Data Dep: \n");
404 if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
406 if (ddr)
408 dra = DDR_A (ddr);
409 drb = DDR_B (ddr);
410 if (dra)
411 dump_data_reference (outf, dra);
412 else
413 fprintf (outf, " (nil)\n");
414 if (drb)
415 dump_data_reference (outf, drb);
416 else
417 fprintf (outf, " (nil)\n");
419 fprintf (outf, " (don't know)\n)\n");
420 return;
423 dra = DDR_A (ddr);
424 drb = DDR_B (ddr);
425 dump_data_reference (outf, dra);
426 dump_data_reference (outf, drb);
428 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
429 fprintf (outf, " (no dependence)\n");
431 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
433 unsigned int i;
434 struct loop *loopi;
436 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
438 fprintf (outf, " access_fn_A: ");
439 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
440 fprintf (outf, " access_fn_B: ");
441 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
442 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
445 fprintf (outf, " inner loop index: %d\n", DDR_INNER_LOOP (ddr));
446 fprintf (outf, " loop nest: (");
447 FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
448 fprintf (outf, "%d ", loopi->num);
449 fprintf (outf, ")\n");
451 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
453 fprintf (outf, " distance_vector: ");
454 print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
455 DDR_NB_LOOPS (ddr));
458 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
460 fprintf (outf, " direction_vector: ");
461 print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
462 DDR_NB_LOOPS (ddr));
466 fprintf (outf, ")\n");
469 /* Debug version. */
471 DEBUG_FUNCTION void
472 debug_data_dependence_relation (struct data_dependence_relation *ddr)
474 dump_data_dependence_relation (stderr, ddr);
477 /* Dump into FILE all the dependence relations from DDRS. */
479 DEBUG_FUNCTION void
480 dump_data_dependence_relations (FILE *file,
481 vec<ddr_p> ddrs)
483 unsigned int i;
484 struct data_dependence_relation *ddr;
486 FOR_EACH_VEC_ELT (ddrs, i, ddr)
487 dump_data_dependence_relation (file, ddr);
490 DEBUG_FUNCTION void
491 debug (vec<ddr_p> &ref)
493 dump_data_dependence_relations (stderr, ref);
496 DEBUG_FUNCTION void
497 debug (vec<ddr_p> *ptr)
499 if (ptr)
500 debug (*ptr);
501 else
502 fprintf (stderr, "<nil>\n");
506 /* Dump to STDERR all the dependence relations from DDRS. */
508 DEBUG_FUNCTION void
509 debug_data_dependence_relations (vec<ddr_p> ddrs)
511 dump_data_dependence_relations (stderr, ddrs);
514 /* Dumps the distance and direction vectors in FILE. DDRS contains
515 the dependence relations, and VECT_SIZE is the size of the
516 dependence vectors, or in other words the number of loops in the
517 considered nest. */
519 DEBUG_FUNCTION void
520 dump_dist_dir_vectors (FILE *file, vec<ddr_p> ddrs)
522 unsigned int i, j;
523 struct data_dependence_relation *ddr;
524 lambda_vector v;
526 FOR_EACH_VEC_ELT (ddrs, i, ddr)
527 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
529 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), j, v)
531 fprintf (file, "DISTANCE_V (");
532 print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
533 fprintf (file, ")\n");
536 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), j, v)
538 fprintf (file, "DIRECTION_V (");
539 print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
540 fprintf (file, ")\n");
544 fprintf (file, "\n\n");
547 /* Dumps the data dependence relations DDRS in FILE. */
549 DEBUG_FUNCTION void
550 dump_ddrs (FILE *file, vec<ddr_p> ddrs)
552 unsigned int i;
553 struct data_dependence_relation *ddr;
555 FOR_EACH_VEC_ELT (ddrs, i, ddr)
556 dump_data_dependence_relation (file, ddr);
558 fprintf (file, "\n\n");
561 DEBUG_FUNCTION void
562 debug_ddrs (vec<ddr_p> ddrs)
564 dump_ddrs (stderr, ddrs);
567 /* Helper function for split_constant_offset. Expresses OP0 CODE OP1
568 (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
569 constant of type ssizetype, and returns true. If we cannot do this
570 with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
571 is returned. */
573 static bool
574 split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
575 tree *var, tree *off)
577 tree var0, var1;
578 tree off0, off1;
579 enum tree_code ocode = code;
581 *var = NULL_TREE;
582 *off = NULL_TREE;
584 switch (code)
586 case INTEGER_CST:
587 *var = build_int_cst (type, 0);
588 *off = fold_convert (ssizetype, op0);
589 return true;
591 case POINTER_PLUS_EXPR:
592 ocode = PLUS_EXPR;
593 /* FALLTHROUGH */
594 case PLUS_EXPR:
595 case MINUS_EXPR:
596 split_constant_offset (op0, &var0, &off0);
597 split_constant_offset (op1, &var1, &off1);
598 *var = fold_build2 (code, type, var0, var1);
599 *off = size_binop (ocode, off0, off1);
600 return true;
602 case MULT_EXPR:
603 if (TREE_CODE (op1) != INTEGER_CST)
604 return false;
606 split_constant_offset (op0, &var0, &off0);
607 *var = fold_build2 (MULT_EXPR, type, var0, op1);
608 *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
609 return true;
611 case ADDR_EXPR:
613 tree base, poffset;
614 HOST_WIDE_INT pbitsize, pbitpos;
615 machine_mode pmode;
616 int punsignedp, preversep, pvolatilep;
618 op0 = TREE_OPERAND (op0, 0);
619 base
620 = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset, &pmode,
621 &punsignedp, &preversep, &pvolatilep, false);
623 if (pbitpos % BITS_PER_UNIT != 0)
624 return false;
625 base = build_fold_addr_expr (base);
626 off0 = ssize_int (pbitpos / BITS_PER_UNIT);
628 if (poffset)
630 split_constant_offset (poffset, &poffset, &off1);
631 off0 = size_binop (PLUS_EXPR, off0, off1);
632 if (POINTER_TYPE_P (TREE_TYPE (base)))
633 base = fold_build_pointer_plus (base, poffset);
634 else
635 base = fold_build2 (PLUS_EXPR, TREE_TYPE (base), base,
636 fold_convert (TREE_TYPE (base), poffset));
639 var0 = fold_convert (type, base);
641 /* If variable length types are involved, punt, otherwise casts
642 might be converted into ARRAY_REFs in gimplify_conversion.
643 To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
644 possibly no longer appears in current GIMPLE, might resurface.
645 This perhaps could run
646 if (CONVERT_EXPR_P (var0))
648 gimplify_conversion (&var0);
649 // Attempt to fill in any within var0 found ARRAY_REF's
650 // element size from corresponding op embedded ARRAY_REF,
651 // if unsuccessful, just punt.
652 } */
653 while (POINTER_TYPE_P (type))
654 type = TREE_TYPE (type);
655 if (int_size_in_bytes (type) < 0)
656 return false;
658 *var = var0;
659 *off = off0;
660 return true;
663 case SSA_NAME:
665 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
666 return false;
668 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
669 enum tree_code subcode;
671 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
672 return false;
674 var0 = gimple_assign_rhs1 (def_stmt);
675 subcode = gimple_assign_rhs_code (def_stmt);
676 var1 = gimple_assign_rhs2 (def_stmt);
678 return split_constant_offset_1 (type, var0, subcode, var1, var, off);
680 CASE_CONVERT:
682 /* We must not introduce undefined overflow, and we must not change the value.
683 Hence we're okay if the inner type doesn't overflow to start with
684 (pointer or signed), the outer type also is an integer or pointer
685 and the outer precision is at least as large as the inner. */
686 tree itype = TREE_TYPE (op0);
687 if ((POINTER_TYPE_P (itype)
688 || (INTEGRAL_TYPE_P (itype) && TYPE_OVERFLOW_UNDEFINED (itype)))
689 && TYPE_PRECISION (type) >= TYPE_PRECISION (itype)
690 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)))
692 split_constant_offset (op0, &var0, off);
693 *var = fold_convert (type, var0);
694 return true;
696 return false;
699 default:
700 return false;
704 /* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
705 will be ssizetype. */
707 void
708 split_constant_offset (tree exp, tree *var, tree *off)
710 tree type = TREE_TYPE (exp), otype, op0, op1, e, o;
711 enum tree_code code;
713 *var = exp;
714 *off = ssize_int (0);
715 STRIP_NOPS (exp);
717 if (tree_is_chrec (exp)
718 || get_gimple_rhs_class (TREE_CODE (exp)) == GIMPLE_TERNARY_RHS)
719 return;
721 otype = TREE_TYPE (exp);
722 code = TREE_CODE (exp);
723 extract_ops_from_tree (exp, &code, &op0, &op1);
724 if (split_constant_offset_1 (otype, op0, code, op1, &e, &o))
726 *var = fold_convert (type, e);
727 *off = o;
731 /* Returns the address ADDR of an object in a canonical shape (without nop
732 casts, and with type of pointer to the object). */
734 static tree
735 canonicalize_base_object_address (tree addr)
737 tree orig = addr;
739 STRIP_NOPS (addr);
741 /* The base address may be obtained by casting from integer, in that case
742 keep the cast. */
743 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
744 return orig;
746 if (TREE_CODE (addr) != ADDR_EXPR)
747 return addr;
749 return build_fold_addr_expr (TREE_OPERAND (addr, 0));
752 /* Analyzes the behavior of the memory reference DR in the innermost loop or
753 basic block that contains it. Returns true if analysis succeed or false
754 otherwise. */
756 bool
757 dr_analyze_innermost (struct data_reference *dr, struct loop *nest)
759 gimple *stmt = DR_STMT (dr);
760 struct loop *loop = loop_containing_stmt (stmt);
761 tree ref = DR_REF (dr);
762 HOST_WIDE_INT pbitsize, pbitpos;
763 tree base, poffset;
764 machine_mode pmode;
765 int punsignedp, preversep, pvolatilep;
766 affine_iv base_iv, offset_iv;
767 tree init, dinit, step;
768 bool in_loop = (loop && loop->num);
770 if (dump_file && (dump_flags & TDF_DETAILS))
771 fprintf (dump_file, "analyze_innermost: ");
773 base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset, &pmode,
774 &punsignedp, &preversep, &pvolatilep, false);
775 gcc_assert (base != NULL_TREE);
777 if (pbitpos % BITS_PER_UNIT != 0)
779 if (dump_file && (dump_flags & TDF_DETAILS))
780 fprintf (dump_file, "failed: bit offset alignment.\n");
781 return false;
784 if (preversep)
786 if (dump_file && (dump_flags & TDF_DETAILS))
787 fprintf (dump_file, "failed: reverse storage order.\n");
788 return false;
791 if (TREE_CODE (base) == MEM_REF)
793 if (!integer_zerop (TREE_OPERAND (base, 1)))
795 offset_int moff = mem_ref_offset (base);
796 tree mofft = wide_int_to_tree (sizetype, moff);
797 if (!poffset)
798 poffset = mofft;
799 else
800 poffset = size_binop (PLUS_EXPR, poffset, mofft);
802 base = TREE_OPERAND (base, 0);
804 else
805 base = build_fold_addr_expr (base);
807 if (in_loop)
809 if (!simple_iv (loop, loop_containing_stmt (stmt), base, &base_iv,
810 nest ? true : false))
812 if (nest)
814 if (dump_file && (dump_flags & TDF_DETAILS))
815 fprintf (dump_file, "failed: evolution of base is not"
816 " affine.\n");
817 return false;
819 else
821 base_iv.base = base;
822 base_iv.step = ssize_int (0);
823 base_iv.no_overflow = true;
827 else
829 base_iv.base = base;
830 base_iv.step = ssize_int (0);
831 base_iv.no_overflow = true;
834 if (!poffset)
836 offset_iv.base = ssize_int (0);
837 offset_iv.step = ssize_int (0);
839 else
841 if (!in_loop)
843 offset_iv.base = poffset;
844 offset_iv.step = ssize_int (0);
846 else if (!simple_iv (loop, loop_containing_stmt (stmt),
847 poffset, &offset_iv,
848 nest ? true : false))
850 if (nest)
852 if (dump_file && (dump_flags & TDF_DETAILS))
853 fprintf (dump_file, "failed: evolution of offset is not"
854 " affine.\n");
855 return false;
857 else
859 offset_iv.base = poffset;
860 offset_iv.step = ssize_int (0);
865 init = ssize_int (pbitpos / BITS_PER_UNIT);
866 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
867 init = size_binop (PLUS_EXPR, init, dinit);
868 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
869 init = size_binop (PLUS_EXPR, init, dinit);
871 step = size_binop (PLUS_EXPR,
872 fold_convert (ssizetype, base_iv.step),
873 fold_convert (ssizetype, offset_iv.step));
875 DR_BASE_ADDRESS (dr) = canonicalize_base_object_address (base_iv.base);
877 DR_OFFSET (dr) = fold_convert (ssizetype, offset_iv.base);
878 DR_INIT (dr) = init;
879 DR_STEP (dr) = step;
881 DR_ALIGNED_TO (dr) = size_int (highest_pow2_factor (offset_iv.base));
883 if (dump_file && (dump_flags & TDF_DETAILS))
884 fprintf (dump_file, "success.\n");
886 return true;
889 /* Determines the base object and the list of indices of memory reference
890 DR, analyzed in LOOP and instantiated in loop nest NEST. */
892 static void
893 dr_analyze_indices (struct data_reference *dr, loop_p nest, loop_p loop)
895 vec<tree> access_fns = vNULL;
896 tree ref, op;
897 tree base, off, access_fn;
898 basic_block before_loop;
900 /* If analyzing a basic-block there are no indices to analyze
901 and thus no access functions. */
902 if (!nest)
904 DR_BASE_OBJECT (dr) = DR_REF (dr);
905 DR_ACCESS_FNS (dr).create (0);
906 return;
909 ref = DR_REF (dr);
910 before_loop = block_before_loop (nest);
912 /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
913 into a two element array with a constant index. The base is
914 then just the immediate underlying object. */
915 if (TREE_CODE (ref) == REALPART_EXPR)
917 ref = TREE_OPERAND (ref, 0);
918 access_fns.safe_push (integer_zero_node);
920 else if (TREE_CODE (ref) == IMAGPART_EXPR)
922 ref = TREE_OPERAND (ref, 0);
923 access_fns.safe_push (integer_one_node);
926 /* Analyze access functions of dimensions we know to be independent. */
927 while (handled_component_p (ref))
929 if (TREE_CODE (ref) == ARRAY_REF)
931 op = TREE_OPERAND (ref, 1);
932 access_fn = analyze_scalar_evolution (loop, op);
933 access_fn = instantiate_scev (before_loop, loop, access_fn);
934 access_fns.safe_push (access_fn);
936 else if (TREE_CODE (ref) == COMPONENT_REF
937 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
939 /* For COMPONENT_REFs of records (but not unions!) use the
940 FIELD_DECL offset as constant access function so we can
941 disambiguate a[i].f1 and a[i].f2. */
942 tree off = component_ref_field_offset (ref);
943 off = size_binop (PLUS_EXPR,
944 size_binop (MULT_EXPR,
945 fold_convert (bitsizetype, off),
946 bitsize_int (BITS_PER_UNIT)),
947 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
948 access_fns.safe_push (off);
950 else
951 /* If we have an unhandled component we could not translate
952 to an access function stop analyzing. We have determined
953 our base object in this case. */
954 break;
956 ref = TREE_OPERAND (ref, 0);
959 /* If the address operand of a MEM_REF base has an evolution in the
960 analyzed nest, add it as an additional independent access-function. */
961 if (TREE_CODE (ref) == MEM_REF)
963 op = TREE_OPERAND (ref, 0);
964 access_fn = analyze_scalar_evolution (loop, op);
965 access_fn = instantiate_scev (before_loop, loop, access_fn);
966 if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
968 tree orig_type;
969 tree memoff = TREE_OPERAND (ref, 1);
970 base = initial_condition (access_fn);
971 orig_type = TREE_TYPE (base);
972 STRIP_USELESS_TYPE_CONVERSION (base);
973 split_constant_offset (base, &base, &off);
974 STRIP_USELESS_TYPE_CONVERSION (base);
975 /* Fold the MEM_REF offset into the evolutions initial
976 value to make more bases comparable. */
977 if (!integer_zerop (memoff))
979 off = size_binop (PLUS_EXPR, off,
980 fold_convert (ssizetype, memoff));
981 memoff = build_int_cst (TREE_TYPE (memoff), 0);
983 /* Adjust the offset so it is a multiple of the access type
984 size and thus we separate bases that can possibly be used
985 to produce partial overlaps (which the access_fn machinery
986 cannot handle). */
987 wide_int rem;
988 if (TYPE_SIZE_UNIT (TREE_TYPE (ref))
989 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref))) == INTEGER_CST
990 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (ref))))
991 rem = wi::mod_trunc (off, TYPE_SIZE_UNIT (TREE_TYPE (ref)), SIGNED);
992 else
993 /* If we can't compute the remainder simply force the initial
994 condition to zero. */
995 rem = off;
996 off = wide_int_to_tree (ssizetype, wi::sub (off, rem));
997 memoff = wide_int_to_tree (TREE_TYPE (memoff), rem);
998 /* And finally replace the initial condition. */
999 access_fn = chrec_replace_initial_condition
1000 (access_fn, fold_convert (orig_type, off));
1001 /* ??? This is still not a suitable base object for
1002 dr_may_alias_p - the base object needs to be an
1003 access that covers the object as whole. With
1004 an evolution in the pointer this cannot be
1005 guaranteed.
1006 As a band-aid, mark the access so we can special-case
1007 it in dr_may_alias_p. */
1008 tree old = ref;
1009 ref = fold_build2_loc (EXPR_LOCATION (ref),
1010 MEM_REF, TREE_TYPE (ref),
1011 base, memoff);
1012 MR_DEPENDENCE_CLIQUE (ref) = MR_DEPENDENCE_CLIQUE (old);
1013 MR_DEPENDENCE_BASE (ref) = MR_DEPENDENCE_BASE (old);
1014 DR_UNCONSTRAINED_BASE (dr) = true;
1015 access_fns.safe_push (access_fn);
1018 else if (DECL_P (ref))
1020 /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
1021 ref = build2 (MEM_REF, TREE_TYPE (ref),
1022 build_fold_addr_expr (ref),
1023 build_int_cst (reference_alias_ptr_type (ref), 0));
1026 DR_BASE_OBJECT (dr) = ref;
1027 DR_ACCESS_FNS (dr) = access_fns;
1030 /* Extracts the alias analysis information from the memory reference DR. */
1032 static void
1033 dr_analyze_alias (struct data_reference *dr)
1035 tree ref = DR_REF (dr);
1036 tree base = get_base_address (ref), addr;
1038 if (INDIRECT_REF_P (base)
1039 || TREE_CODE (base) == MEM_REF)
1041 addr = TREE_OPERAND (base, 0);
1042 if (TREE_CODE (addr) == SSA_NAME)
1043 DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
1047 /* Frees data reference DR. */
1049 void
1050 free_data_ref (data_reference_p dr)
1052 DR_ACCESS_FNS (dr).release ();
1053 free (dr);
1056 /* Analyzes memory reference MEMREF accessed in STMT. The reference
1057 is read if IS_READ is true, write otherwise. Returns the
1058 data_reference description of MEMREF. NEST is the outermost loop
1059 in which the reference should be instantiated, LOOP is the loop in
1060 which the data reference should be analyzed. */
1062 struct data_reference *
1063 create_data_ref (loop_p nest, loop_p loop, tree memref, gimple *stmt,
1064 bool is_read)
1066 struct data_reference *dr;
1068 if (dump_file && (dump_flags & TDF_DETAILS))
1070 fprintf (dump_file, "Creating dr for ");
1071 print_generic_expr (dump_file, memref, TDF_SLIM);
1072 fprintf (dump_file, "\n");
1075 dr = XCNEW (struct data_reference);
1076 DR_STMT (dr) = stmt;
1077 DR_REF (dr) = memref;
1078 DR_IS_READ (dr) = is_read;
1080 dr_analyze_innermost (dr, nest);
1081 dr_analyze_indices (dr, nest, loop);
1082 dr_analyze_alias (dr);
1084 if (dump_file && (dump_flags & TDF_DETAILS))
1086 unsigned i;
1087 fprintf (dump_file, "\tbase_address: ");
1088 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1089 fprintf (dump_file, "\n\toffset from base address: ");
1090 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1091 fprintf (dump_file, "\n\tconstant offset from base address: ");
1092 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1093 fprintf (dump_file, "\n\tstep: ");
1094 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1095 fprintf (dump_file, "\n\taligned to: ");
1096 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
1097 fprintf (dump_file, "\n\tbase_object: ");
1098 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1099 fprintf (dump_file, "\n");
1100 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
1102 fprintf (dump_file, "\tAccess function %d: ", i);
1103 print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
1107 return dr;
1110 /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
1111 expressions. */
1112 static bool
1113 dr_equal_offsets_p1 (tree offset1, tree offset2)
1115 bool res;
1117 STRIP_NOPS (offset1);
1118 STRIP_NOPS (offset2);
1120 if (offset1 == offset2)
1121 return true;
1123 if (TREE_CODE (offset1) != TREE_CODE (offset2)
1124 || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
1125 return false;
1127 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
1128 TREE_OPERAND (offset2, 0));
1130 if (!res || !BINARY_CLASS_P (offset1))
1131 return res;
1133 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
1134 TREE_OPERAND (offset2, 1));
1136 return res;
1139 /* Check if DRA and DRB have equal offsets. */
1140 bool
1141 dr_equal_offsets_p (struct data_reference *dra,
1142 struct data_reference *drb)
1144 tree offset1, offset2;
1146 offset1 = DR_OFFSET (dra);
1147 offset2 = DR_OFFSET (drb);
1149 return dr_equal_offsets_p1 (offset1, offset2);
1152 /* Returns true if FNA == FNB. */
1154 static bool
1155 affine_function_equal_p (affine_fn fna, affine_fn fnb)
1157 unsigned i, n = fna.length ();
1159 if (n != fnb.length ())
1160 return false;
1162 for (i = 0; i < n; i++)
1163 if (!operand_equal_p (fna[i], fnb[i], 0))
1164 return false;
1166 return true;
1169 /* If all the functions in CF are the same, returns one of them,
1170 otherwise returns NULL. */
1172 static affine_fn
1173 common_affine_function (conflict_function *cf)
1175 unsigned i;
1176 affine_fn comm;
1178 if (!CF_NONTRIVIAL_P (cf))
1179 return affine_fn ();
1181 comm = cf->fns[0];
1183 for (i = 1; i < cf->n; i++)
1184 if (!affine_function_equal_p (comm, cf->fns[i]))
1185 return affine_fn ();
1187 return comm;
1190 /* Returns the base of the affine function FN. */
1192 static tree
1193 affine_function_base (affine_fn fn)
1195 return fn[0];
1198 /* Returns true if FN is a constant. */
1200 static bool
1201 affine_function_constant_p (affine_fn fn)
1203 unsigned i;
1204 tree coef;
1206 for (i = 1; fn.iterate (i, &coef); i++)
1207 if (!integer_zerop (coef))
1208 return false;
1210 return true;
1213 /* Returns true if FN is the zero constant function. */
1215 static bool
1216 affine_function_zero_p (affine_fn fn)
1218 return (integer_zerop (affine_function_base (fn))
1219 && affine_function_constant_p (fn));
1222 /* Returns a signed integer type with the largest precision from TA
1223 and TB. */
1225 static tree
1226 signed_type_for_types (tree ta, tree tb)
1228 if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
1229 return signed_type_for (ta);
1230 else
1231 return signed_type_for (tb);
1234 /* Applies operation OP on affine functions FNA and FNB, and returns the
1235 result. */
1237 static affine_fn
1238 affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
1240 unsigned i, n, m;
1241 affine_fn ret;
1242 tree coef;
1244 if (fnb.length () > fna.length ())
1246 n = fna.length ();
1247 m = fnb.length ();
1249 else
1251 n = fnb.length ();
1252 m = fna.length ();
1255 ret.create (m);
1256 for (i = 0; i < n; i++)
1258 tree type = signed_type_for_types (TREE_TYPE (fna[i]),
1259 TREE_TYPE (fnb[i]));
1260 ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
1263 for (; fna.iterate (i, &coef); i++)
1264 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1265 coef, integer_zero_node));
1266 for (; fnb.iterate (i, &coef); i++)
1267 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1268 integer_zero_node, coef));
1270 return ret;
1273 /* Returns the sum of affine functions FNA and FNB. */
1275 static affine_fn
1276 affine_fn_plus (affine_fn fna, affine_fn fnb)
1278 return affine_fn_op (PLUS_EXPR, fna, fnb);
1281 /* Returns the difference of affine functions FNA and FNB. */
1283 static affine_fn
1284 affine_fn_minus (affine_fn fna, affine_fn fnb)
1286 return affine_fn_op (MINUS_EXPR, fna, fnb);
1289 /* Frees affine function FN. */
1291 static void
1292 affine_fn_free (affine_fn fn)
1294 fn.release ();
1297 /* Determine for each subscript in the data dependence relation DDR
1298 the distance. */
1300 static void
1301 compute_subscript_distance (struct data_dependence_relation *ddr)
1303 conflict_function *cf_a, *cf_b;
1304 affine_fn fn_a, fn_b, diff;
1306 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1308 unsigned int i;
1310 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1312 struct subscript *subscript;
1314 subscript = DDR_SUBSCRIPT (ddr, i);
1315 cf_a = SUB_CONFLICTS_IN_A (subscript);
1316 cf_b = SUB_CONFLICTS_IN_B (subscript);
1318 fn_a = common_affine_function (cf_a);
1319 fn_b = common_affine_function (cf_b);
1320 if (!fn_a.exists () || !fn_b.exists ())
1322 SUB_DISTANCE (subscript) = chrec_dont_know;
1323 return;
1325 diff = affine_fn_minus (fn_a, fn_b);
1327 if (affine_function_constant_p (diff))
1328 SUB_DISTANCE (subscript) = affine_function_base (diff);
1329 else
1330 SUB_DISTANCE (subscript) = chrec_dont_know;
1332 affine_fn_free (diff);
1337 /* Returns the conflict function for "unknown". */
1339 static conflict_function *
1340 conflict_fn_not_known (void)
1342 conflict_function *fn = XCNEW (conflict_function);
1343 fn->n = NOT_KNOWN;
1345 return fn;
1348 /* Returns the conflict function for "independent". */
1350 static conflict_function *
1351 conflict_fn_no_dependence (void)
1353 conflict_function *fn = XCNEW (conflict_function);
1354 fn->n = NO_DEPENDENCE;
1356 return fn;
1359 /* Returns true if the address of OBJ is invariant in LOOP. */
1361 static bool
1362 object_address_invariant_in_loop_p (const struct loop *loop, const_tree obj)
1364 while (handled_component_p (obj))
1366 if (TREE_CODE (obj) == ARRAY_REF)
1368 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1369 need to check the stride and the lower bound of the reference. */
1370 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1371 loop->num)
1372 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 3),
1373 loop->num))
1374 return false;
1376 else if (TREE_CODE (obj) == COMPONENT_REF)
1378 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1379 loop->num))
1380 return false;
1382 obj = TREE_OPERAND (obj, 0);
1385 if (!INDIRECT_REF_P (obj)
1386 && TREE_CODE (obj) != MEM_REF)
1387 return true;
1389 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
1390 loop->num);
1393 /* Returns false if we can prove that data references A and B do not alias,
1394 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
1395 considered. */
1397 bool
1398 dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
1399 bool loop_nest)
1401 tree addr_a = DR_BASE_OBJECT (a);
1402 tree addr_b = DR_BASE_OBJECT (b);
1404 /* If we are not processing a loop nest but scalar code we
1405 do not need to care about possible cross-iteration dependences
1406 and thus can process the full original reference. Do so,
1407 similar to how loop invariant motion applies extra offset-based
1408 disambiguation. */
1409 if (!loop_nest)
1411 aff_tree off1, off2;
1412 widest_int size1, size2;
1413 get_inner_reference_aff (DR_REF (a), &off1, &size1);
1414 get_inner_reference_aff (DR_REF (b), &off2, &size2);
1415 aff_combination_scale (&off1, -1);
1416 aff_combination_add (&off2, &off1);
1417 if (aff_comb_cannot_overlap_p (&off2, size1, size2))
1418 return false;
1421 if ((TREE_CODE (addr_a) == MEM_REF || TREE_CODE (addr_a) == TARGET_MEM_REF)
1422 && (TREE_CODE (addr_b) == MEM_REF || TREE_CODE (addr_b) == TARGET_MEM_REF)
1423 && MR_DEPENDENCE_CLIQUE (addr_a) == MR_DEPENDENCE_CLIQUE (addr_b)
1424 && MR_DEPENDENCE_BASE (addr_a) != MR_DEPENDENCE_BASE (addr_b))
1425 return false;
1427 /* If we had an evolution in a pointer-based MEM_REF BASE_OBJECT we
1428 do not know the size of the base-object. So we cannot do any
1429 offset/overlap based analysis but have to rely on points-to
1430 information only. */
1431 if (TREE_CODE (addr_a) == MEM_REF
1432 && (DR_UNCONSTRAINED_BASE (a)
1433 || TREE_CODE (TREE_OPERAND (addr_a, 0)) == SSA_NAME))
1435 /* For true dependences we can apply TBAA. */
1436 if (flag_strict_aliasing
1437 && DR_IS_WRITE (a) && DR_IS_READ (b)
1438 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1439 get_alias_set (DR_REF (b))))
1440 return false;
1441 if (TREE_CODE (addr_b) == MEM_REF)
1442 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1443 TREE_OPERAND (addr_b, 0));
1444 else
1445 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1446 build_fold_addr_expr (addr_b));
1448 else if (TREE_CODE (addr_b) == MEM_REF
1449 && (DR_UNCONSTRAINED_BASE (b)
1450 || TREE_CODE (TREE_OPERAND (addr_b, 0)) == SSA_NAME))
1452 /* For true dependences we can apply TBAA. */
1453 if (flag_strict_aliasing
1454 && DR_IS_WRITE (a) && DR_IS_READ (b)
1455 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1456 get_alias_set (DR_REF (b))))
1457 return false;
1458 if (TREE_CODE (addr_a) == MEM_REF)
1459 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1460 TREE_OPERAND (addr_b, 0));
1461 else
1462 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
1463 TREE_OPERAND (addr_b, 0));
1466 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1467 that is being subsetted in the loop nest. */
1468 if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
1469 return refs_output_dependent_p (addr_a, addr_b);
1470 else if (DR_IS_READ (a) && DR_IS_WRITE (b))
1471 return refs_anti_dependent_p (addr_a, addr_b);
1472 return refs_may_alias_p (addr_a, addr_b);
1475 /* Initialize a data dependence relation between data accesses A and
1476 B. NB_LOOPS is the number of loops surrounding the references: the
1477 size of the classic distance/direction vectors. */
1479 struct data_dependence_relation *
1480 initialize_data_dependence_relation (struct data_reference *a,
1481 struct data_reference *b,
1482 vec<loop_p> loop_nest)
1484 struct data_dependence_relation *res;
1485 unsigned int i;
1487 res = XNEW (struct data_dependence_relation);
1488 DDR_A (res) = a;
1489 DDR_B (res) = b;
1490 DDR_LOOP_NEST (res).create (0);
1491 DDR_REVERSED_P (res) = false;
1492 DDR_SUBSCRIPTS (res).create (0);
1493 DDR_DIR_VECTS (res).create (0);
1494 DDR_DIST_VECTS (res).create (0);
1496 if (a == NULL || b == NULL)
1498 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1499 return res;
1502 /* If the data references do not alias, then they are independent. */
1503 if (!dr_may_alias_p (a, b, loop_nest.exists ()))
1505 DDR_ARE_DEPENDENT (res) = chrec_known;
1506 return res;
1509 /* The case where the references are exactly the same. */
1510 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
1512 if ((loop_nest.exists ()
1513 && !object_address_invariant_in_loop_p (loop_nest[0],
1514 DR_BASE_OBJECT (a)))
1515 || DR_NUM_DIMENSIONS (a) == 0)
1517 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1518 return res;
1520 DDR_AFFINE_P (res) = true;
1521 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1522 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1523 DDR_LOOP_NEST (res) = loop_nest;
1524 DDR_INNER_LOOP (res) = 0;
1525 DDR_SELF_REFERENCE (res) = true;
1526 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1528 struct subscript *subscript;
1530 subscript = XNEW (struct subscript);
1531 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1532 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1533 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1534 SUB_DISTANCE (subscript) = chrec_dont_know;
1535 DDR_SUBSCRIPTS (res).safe_push (subscript);
1537 return res;
1540 /* If the references do not access the same object, we do not know
1541 whether they alias or not. */
1542 if (!operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0))
1544 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1545 return res;
1548 /* If the base of the object is not invariant in the loop nest, we cannot
1549 analyze it. TODO -- in fact, it would suffice to record that there may
1550 be arbitrary dependences in the loops where the base object varies. */
1551 if ((loop_nest.exists ()
1552 && !object_address_invariant_in_loop_p (loop_nest[0], DR_BASE_OBJECT (a)))
1553 || DR_NUM_DIMENSIONS (a) == 0)
1555 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1556 return res;
1559 /* If the number of dimensions of the access to not agree we can have
1560 a pointer access to a component of the array element type and an
1561 array access while the base-objects are still the same. Punt. */
1562 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1564 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1565 return res;
1568 DDR_AFFINE_P (res) = true;
1569 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1570 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1571 DDR_LOOP_NEST (res) = loop_nest;
1572 DDR_INNER_LOOP (res) = 0;
1573 DDR_SELF_REFERENCE (res) = false;
1575 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1577 struct subscript *subscript;
1579 subscript = XNEW (struct subscript);
1580 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1581 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1582 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1583 SUB_DISTANCE (subscript) = chrec_dont_know;
1584 DDR_SUBSCRIPTS (res).safe_push (subscript);
1587 return res;
1590 /* Frees memory used by the conflict function F. */
1592 static void
1593 free_conflict_function (conflict_function *f)
1595 unsigned i;
1597 if (CF_NONTRIVIAL_P (f))
1599 for (i = 0; i < f->n; i++)
1600 affine_fn_free (f->fns[i]);
1602 free (f);
1605 /* Frees memory used by SUBSCRIPTS. */
1607 static void
1608 free_subscripts (vec<subscript_p> subscripts)
1610 unsigned i;
1611 subscript_p s;
1613 FOR_EACH_VEC_ELT (subscripts, i, s)
1615 free_conflict_function (s->conflicting_iterations_in_a);
1616 free_conflict_function (s->conflicting_iterations_in_b);
1617 free (s);
1619 subscripts.release ();
1622 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1623 description. */
1625 static inline void
1626 finalize_ddr_dependent (struct data_dependence_relation *ddr,
1627 tree chrec)
1629 DDR_ARE_DEPENDENT (ddr) = chrec;
1630 free_subscripts (DDR_SUBSCRIPTS (ddr));
1631 DDR_SUBSCRIPTS (ddr).create (0);
1634 /* The dependence relation DDR cannot be represented by a distance
1635 vector. */
1637 static inline void
1638 non_affine_dependence_relation (struct data_dependence_relation *ddr)
1640 if (dump_file && (dump_flags & TDF_DETAILS))
1641 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
1643 DDR_AFFINE_P (ddr) = false;
1648 /* This section contains the classic Banerjee tests. */
1650 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1651 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1653 static inline bool
1654 ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1656 return (evolution_function_is_constant_p (chrec_a)
1657 && evolution_function_is_constant_p (chrec_b));
1660 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
1661 variable, i.e., if the SIV (Single Index Variable) test is true. */
1663 static bool
1664 siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1666 if ((evolution_function_is_constant_p (chrec_a)
1667 && evolution_function_is_univariate_p (chrec_b))
1668 || (evolution_function_is_constant_p (chrec_b)
1669 && evolution_function_is_univariate_p (chrec_a)))
1670 return true;
1672 if (evolution_function_is_univariate_p (chrec_a)
1673 && evolution_function_is_univariate_p (chrec_b))
1675 switch (TREE_CODE (chrec_a))
1677 case POLYNOMIAL_CHREC:
1678 switch (TREE_CODE (chrec_b))
1680 case POLYNOMIAL_CHREC:
1681 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
1682 return false;
1684 default:
1685 return true;
1688 default:
1689 return true;
1693 return false;
1696 /* Creates a conflict function with N dimensions. The affine functions
1697 in each dimension follow. */
1699 static conflict_function *
1700 conflict_fn (unsigned n, ...)
1702 unsigned i;
1703 conflict_function *ret = XCNEW (conflict_function);
1704 va_list ap;
1706 gcc_assert (0 < n && n <= MAX_DIM);
1707 va_start (ap, n);
1709 ret->n = n;
1710 for (i = 0; i < n; i++)
1711 ret->fns[i] = va_arg (ap, affine_fn);
1712 va_end (ap);
1714 return ret;
1717 /* Returns constant affine function with value CST. */
1719 static affine_fn
1720 affine_fn_cst (tree cst)
1722 affine_fn fn;
1723 fn.create (1);
1724 fn.quick_push (cst);
1725 return fn;
1728 /* Returns affine function with single variable, CST + COEF * x_DIM. */
1730 static affine_fn
1731 affine_fn_univar (tree cst, unsigned dim, tree coef)
1733 affine_fn fn;
1734 fn.create (dim + 1);
1735 unsigned i;
1737 gcc_assert (dim > 0);
1738 fn.quick_push (cst);
1739 for (i = 1; i < dim; i++)
1740 fn.quick_push (integer_zero_node);
1741 fn.quick_push (coef);
1742 return fn;
1745 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1746 *OVERLAPS_B are initialized to the functions that describe the
1747 relation between the elements accessed twice by CHREC_A and
1748 CHREC_B. For k >= 0, the following property is verified:
1750 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1752 static void
1753 analyze_ziv_subscript (tree chrec_a,
1754 tree chrec_b,
1755 conflict_function **overlaps_a,
1756 conflict_function **overlaps_b,
1757 tree *last_conflicts)
1759 tree type, difference;
1760 dependence_stats.num_ziv++;
1762 if (dump_file && (dump_flags & TDF_DETAILS))
1763 fprintf (dump_file, "(analyze_ziv_subscript \n");
1765 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1766 chrec_a = chrec_convert (type, chrec_a, NULL);
1767 chrec_b = chrec_convert (type, chrec_b, NULL);
1768 difference = chrec_fold_minus (type, chrec_a, chrec_b);
1770 switch (TREE_CODE (difference))
1772 case INTEGER_CST:
1773 if (integer_zerop (difference))
1775 /* The difference is equal to zero: the accessed index
1776 overlaps for each iteration in the loop. */
1777 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1778 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1779 *last_conflicts = chrec_dont_know;
1780 dependence_stats.num_ziv_dependent++;
1782 else
1784 /* The accesses do not overlap. */
1785 *overlaps_a = conflict_fn_no_dependence ();
1786 *overlaps_b = conflict_fn_no_dependence ();
1787 *last_conflicts = integer_zero_node;
1788 dependence_stats.num_ziv_independent++;
1790 break;
1792 default:
1793 /* We're not sure whether the indexes overlap. For the moment,
1794 conservatively answer "don't know". */
1795 if (dump_file && (dump_flags & TDF_DETAILS))
1796 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
1798 *overlaps_a = conflict_fn_not_known ();
1799 *overlaps_b = conflict_fn_not_known ();
1800 *last_conflicts = chrec_dont_know;
1801 dependence_stats.num_ziv_unimplemented++;
1802 break;
1805 if (dump_file && (dump_flags & TDF_DETAILS))
1806 fprintf (dump_file, ")\n");
1809 /* Similar to max_stmt_executions_int, but returns the bound as a tree,
1810 and only if it fits to the int type. If this is not the case, or the
1811 bound on the number of iterations of LOOP could not be derived, returns
1812 chrec_dont_know. */
1814 static tree
1815 max_stmt_executions_tree (struct loop *loop)
1817 widest_int nit;
1819 if (!max_stmt_executions (loop, &nit))
1820 return chrec_dont_know;
1822 if (!wi::fits_to_tree_p (nit, unsigned_type_node))
1823 return chrec_dont_know;
1825 return wide_int_to_tree (unsigned_type_node, nit);
1828 /* Determine whether the CHREC is always positive/negative. If the expression
1829 cannot be statically analyzed, return false, otherwise set the answer into
1830 VALUE. */
1832 static bool
1833 chrec_is_positive (tree chrec, bool *value)
1835 bool value0, value1, value2;
1836 tree end_value, nb_iter;
1838 switch (TREE_CODE (chrec))
1840 case POLYNOMIAL_CHREC:
1841 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
1842 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
1843 return false;
1845 /* FIXME -- overflows. */
1846 if (value0 == value1)
1848 *value = value0;
1849 return true;
1852 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1853 and the proof consists in showing that the sign never
1854 changes during the execution of the loop, from 0 to
1855 loop->nb_iterations. */
1856 if (!evolution_function_is_affine_p (chrec))
1857 return false;
1859 nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
1860 if (chrec_contains_undetermined (nb_iter))
1861 return false;
1863 #if 0
1864 /* TODO -- If the test is after the exit, we may decrease the number of
1865 iterations by one. */
1866 if (after_exit)
1867 nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
1868 #endif
1870 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
1872 if (!chrec_is_positive (end_value, &value2))
1873 return false;
1875 *value = value0;
1876 return value0 == value1;
1878 case INTEGER_CST:
1879 switch (tree_int_cst_sgn (chrec))
1881 case -1:
1882 *value = false;
1883 break;
1884 case 1:
1885 *value = true;
1886 break;
1887 default:
1888 return false;
1890 return true;
1892 default:
1893 return false;
1898 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1899 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1900 *OVERLAPS_B are initialized to the functions that describe the
1901 relation between the elements accessed twice by CHREC_A and
1902 CHREC_B. For k >= 0, the following property is verified:
1904 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1906 static void
1907 analyze_siv_subscript_cst_affine (tree chrec_a,
1908 tree chrec_b,
1909 conflict_function **overlaps_a,
1910 conflict_function **overlaps_b,
1911 tree *last_conflicts)
1913 bool value0, value1, value2;
1914 tree type, difference, tmp;
1916 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1917 chrec_a = chrec_convert (type, chrec_a, NULL);
1918 chrec_b = chrec_convert (type, chrec_b, NULL);
1919 difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
1921 /* Special case overlap in the first iteration. */
1922 if (integer_zerop (difference))
1924 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1925 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1926 *last_conflicts = integer_one_node;
1927 return;
1930 if (!chrec_is_positive (initial_condition (difference), &value0))
1932 if (dump_file && (dump_flags & TDF_DETAILS))
1933 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
1935 dependence_stats.num_siv_unimplemented++;
1936 *overlaps_a = conflict_fn_not_known ();
1937 *overlaps_b = conflict_fn_not_known ();
1938 *last_conflicts = chrec_dont_know;
1939 return;
1941 else
1943 if (value0 == false)
1945 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
1947 if (dump_file && (dump_flags & TDF_DETAILS))
1948 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1950 *overlaps_a = conflict_fn_not_known ();
1951 *overlaps_b = conflict_fn_not_known ();
1952 *last_conflicts = chrec_dont_know;
1953 dependence_stats.num_siv_unimplemented++;
1954 return;
1956 else
1958 if (value1 == true)
1960 /* Example:
1961 chrec_a = 12
1962 chrec_b = {10, +, 1}
1965 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1967 HOST_WIDE_INT numiter;
1968 struct loop *loop = get_chrec_loop (chrec_b);
1970 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1971 tmp = fold_build2 (EXACT_DIV_EXPR, type,
1972 fold_build1 (ABS_EXPR, type, difference),
1973 CHREC_RIGHT (chrec_b));
1974 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1975 *last_conflicts = integer_one_node;
1978 /* Perform weak-zero siv test to see if overlap is
1979 outside the loop bounds. */
1980 numiter = max_stmt_executions_int (loop);
1982 if (numiter >= 0
1983 && compare_tree_int (tmp, numiter) > 0)
1985 free_conflict_function (*overlaps_a);
1986 free_conflict_function (*overlaps_b);
1987 *overlaps_a = conflict_fn_no_dependence ();
1988 *overlaps_b = conflict_fn_no_dependence ();
1989 *last_conflicts = integer_zero_node;
1990 dependence_stats.num_siv_independent++;
1991 return;
1993 dependence_stats.num_siv_dependent++;
1994 return;
1997 /* When the step does not divide the difference, there are
1998 no overlaps. */
1999 else
2001 *overlaps_a = conflict_fn_no_dependence ();
2002 *overlaps_b = conflict_fn_no_dependence ();
2003 *last_conflicts = integer_zero_node;
2004 dependence_stats.num_siv_independent++;
2005 return;
2009 else
2011 /* Example:
2012 chrec_a = 12
2013 chrec_b = {10, +, -1}
2015 In this case, chrec_a will not overlap with chrec_b. */
2016 *overlaps_a = conflict_fn_no_dependence ();
2017 *overlaps_b = conflict_fn_no_dependence ();
2018 *last_conflicts = integer_zero_node;
2019 dependence_stats.num_siv_independent++;
2020 return;
2024 else
2026 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
2028 if (dump_file && (dump_flags & TDF_DETAILS))
2029 fprintf (dump_file, "siv test failed: chrec not positive.\n");
2031 *overlaps_a = conflict_fn_not_known ();
2032 *overlaps_b = conflict_fn_not_known ();
2033 *last_conflicts = chrec_dont_know;
2034 dependence_stats.num_siv_unimplemented++;
2035 return;
2037 else
2039 if (value2 == false)
2041 /* Example:
2042 chrec_a = 3
2043 chrec_b = {10, +, -1}
2045 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2047 HOST_WIDE_INT numiter;
2048 struct loop *loop = get_chrec_loop (chrec_b);
2050 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2051 tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
2052 CHREC_RIGHT (chrec_b));
2053 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
2054 *last_conflicts = integer_one_node;
2056 /* Perform weak-zero siv test to see if overlap is
2057 outside the loop bounds. */
2058 numiter = max_stmt_executions_int (loop);
2060 if (numiter >= 0
2061 && compare_tree_int (tmp, numiter) > 0)
2063 free_conflict_function (*overlaps_a);
2064 free_conflict_function (*overlaps_b);
2065 *overlaps_a = conflict_fn_no_dependence ();
2066 *overlaps_b = conflict_fn_no_dependence ();
2067 *last_conflicts = integer_zero_node;
2068 dependence_stats.num_siv_independent++;
2069 return;
2071 dependence_stats.num_siv_dependent++;
2072 return;
2075 /* When the step does not divide the difference, there
2076 are no overlaps. */
2077 else
2079 *overlaps_a = conflict_fn_no_dependence ();
2080 *overlaps_b = conflict_fn_no_dependence ();
2081 *last_conflicts = integer_zero_node;
2082 dependence_stats.num_siv_independent++;
2083 return;
2086 else
2088 /* Example:
2089 chrec_a = 3
2090 chrec_b = {4, +, 1}
2092 In this case, chrec_a will not overlap with chrec_b. */
2093 *overlaps_a = conflict_fn_no_dependence ();
2094 *overlaps_b = conflict_fn_no_dependence ();
2095 *last_conflicts = integer_zero_node;
2096 dependence_stats.num_siv_independent++;
2097 return;
2104 /* Helper recursive function for initializing the matrix A. Returns
2105 the initial value of CHREC. */
2107 static tree
2108 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2110 gcc_assert (chrec);
2112 switch (TREE_CODE (chrec))
2114 case POLYNOMIAL_CHREC:
2115 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST);
2117 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2118 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2120 case PLUS_EXPR:
2121 case MULT_EXPR:
2122 case MINUS_EXPR:
2124 tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2125 tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
2127 return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
2130 CASE_CONVERT:
2132 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2133 return chrec_convert (chrec_type (chrec), op, NULL);
2136 case BIT_NOT_EXPR:
2138 /* Handle ~X as -1 - X. */
2139 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2140 return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
2141 build_int_cst (TREE_TYPE (chrec), -1), op);
2144 case INTEGER_CST:
2145 return chrec;
2147 default:
2148 gcc_unreachable ();
2149 return NULL_TREE;
2153 #define FLOOR_DIV(x,y) ((x) / (y))
2155 /* Solves the special case of the Diophantine equation:
2156 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2158 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2159 number of iterations that loops X and Y run. The overlaps will be
2160 constructed as evolutions in dimension DIM. */
2162 static void
2163 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2164 affine_fn *overlaps_a,
2165 affine_fn *overlaps_b,
2166 tree *last_conflicts, int dim)
2168 if (((step_a > 0 && step_b > 0)
2169 || (step_a < 0 && step_b < 0)))
2171 int step_overlaps_a, step_overlaps_b;
2172 int gcd_steps_a_b, last_conflict, tau2;
2174 gcd_steps_a_b = gcd (step_a, step_b);
2175 step_overlaps_a = step_b / gcd_steps_a_b;
2176 step_overlaps_b = step_a / gcd_steps_a_b;
2178 if (niter > 0)
2180 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2181 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2182 last_conflict = tau2;
2183 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2185 else
2186 *last_conflicts = chrec_dont_know;
2188 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
2189 build_int_cst (NULL_TREE,
2190 step_overlaps_a));
2191 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
2192 build_int_cst (NULL_TREE,
2193 step_overlaps_b));
2196 else
2198 *overlaps_a = affine_fn_cst (integer_zero_node);
2199 *overlaps_b = affine_fn_cst (integer_zero_node);
2200 *last_conflicts = integer_zero_node;
2204 /* Solves the special case of a Diophantine equation where CHREC_A is
2205 an affine bivariate function, and CHREC_B is an affine univariate
2206 function. For example,
2208 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2210 has the following overlapping functions:
2212 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2213 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2214 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2216 FORNOW: This is a specialized implementation for a case occurring in
2217 a common benchmark. Implement the general algorithm. */
2219 static void
2220 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2221 conflict_function **overlaps_a,
2222 conflict_function **overlaps_b,
2223 tree *last_conflicts)
2225 bool xz_p, yz_p, xyz_p;
2226 int step_x, step_y, step_z;
2227 HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
2228 affine_fn overlaps_a_xz, overlaps_b_xz;
2229 affine_fn overlaps_a_yz, overlaps_b_yz;
2230 affine_fn overlaps_a_xyz, overlaps_b_xyz;
2231 affine_fn ova1, ova2, ovb;
2232 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
2234 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2235 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2236 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2238 niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
2239 niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
2240 niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
2242 if (niter_x < 0 || niter_y < 0 || niter_z < 0)
2244 if (dump_file && (dump_flags & TDF_DETAILS))
2245 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
2247 *overlaps_a = conflict_fn_not_known ();
2248 *overlaps_b = conflict_fn_not_known ();
2249 *last_conflicts = chrec_dont_know;
2250 return;
2253 niter = MIN (niter_x, niter_z);
2254 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2255 &overlaps_a_xz,
2256 &overlaps_b_xz,
2257 &last_conflicts_xz, 1);
2258 niter = MIN (niter_y, niter_z);
2259 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2260 &overlaps_a_yz,
2261 &overlaps_b_yz,
2262 &last_conflicts_yz, 2);
2263 niter = MIN (niter_x, niter_z);
2264 niter = MIN (niter_y, niter);
2265 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2266 &overlaps_a_xyz,
2267 &overlaps_b_xyz,
2268 &last_conflicts_xyz, 3);
2270 xz_p = !integer_zerop (last_conflicts_xz);
2271 yz_p = !integer_zerop (last_conflicts_yz);
2272 xyz_p = !integer_zerop (last_conflicts_xyz);
2274 if (xz_p || yz_p || xyz_p)
2276 ova1 = affine_fn_cst (integer_zero_node);
2277 ova2 = affine_fn_cst (integer_zero_node);
2278 ovb = affine_fn_cst (integer_zero_node);
2279 if (xz_p)
2281 affine_fn t0 = ova1;
2282 affine_fn t2 = ovb;
2284 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
2285 ovb = affine_fn_plus (ovb, overlaps_b_xz);
2286 affine_fn_free (t0);
2287 affine_fn_free (t2);
2288 *last_conflicts = last_conflicts_xz;
2290 if (yz_p)
2292 affine_fn t0 = ova2;
2293 affine_fn t2 = ovb;
2295 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
2296 ovb = affine_fn_plus (ovb, overlaps_b_yz);
2297 affine_fn_free (t0);
2298 affine_fn_free (t2);
2299 *last_conflicts = last_conflicts_yz;
2301 if (xyz_p)
2303 affine_fn t0 = ova1;
2304 affine_fn t2 = ova2;
2305 affine_fn t4 = ovb;
2307 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
2308 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
2309 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
2310 affine_fn_free (t0);
2311 affine_fn_free (t2);
2312 affine_fn_free (t4);
2313 *last_conflicts = last_conflicts_xyz;
2315 *overlaps_a = conflict_fn (2, ova1, ova2);
2316 *overlaps_b = conflict_fn (1, ovb);
2318 else
2320 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2321 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2322 *last_conflicts = integer_zero_node;
2325 affine_fn_free (overlaps_a_xz);
2326 affine_fn_free (overlaps_b_xz);
2327 affine_fn_free (overlaps_a_yz);
2328 affine_fn_free (overlaps_b_yz);
2329 affine_fn_free (overlaps_a_xyz);
2330 affine_fn_free (overlaps_b_xyz);
2333 /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2335 static void
2336 lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
2337 int size)
2339 memcpy (vec2, vec1, size * sizeof (*vec1));
2342 /* Copy the elements of M x N matrix MAT1 to MAT2. */
2344 static void
2345 lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
2346 int m, int n)
2348 int i;
2350 for (i = 0; i < m; i++)
2351 lambda_vector_copy (mat1[i], mat2[i], n);
2354 /* Store the N x N identity matrix in MAT. */
2356 static void
2357 lambda_matrix_id (lambda_matrix mat, int size)
2359 int i, j;
2361 for (i = 0; i < size; i++)
2362 for (j = 0; j < size; j++)
2363 mat[i][j] = (i == j) ? 1 : 0;
2366 /* Return the first nonzero element of vector VEC1 between START and N.
2367 We must have START <= N. Returns N if VEC1 is the zero vector. */
2369 static int
2370 lambda_vector_first_nz (lambda_vector vec1, int n, int start)
2372 int j = start;
2373 while (j < n && vec1[j] == 0)
2374 j++;
2375 return j;
2378 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2379 R2 = R2 + CONST1 * R1. */
2381 static void
2382 lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2, int const1)
2384 int i;
2386 if (const1 == 0)
2387 return;
2389 for (i = 0; i < n; i++)
2390 mat[r2][i] += const1 * mat[r1][i];
2393 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
2394 and store the result in VEC2. */
2396 static void
2397 lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
2398 int size, int const1)
2400 int i;
2402 if (const1 == 0)
2403 lambda_vector_clear (vec2, size);
2404 else
2405 for (i = 0; i < size; i++)
2406 vec2[i] = const1 * vec1[i];
2409 /* Negate vector VEC1 with length SIZE and store it in VEC2. */
2411 static void
2412 lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
2413 int size)
2415 lambda_vector_mult_const (vec1, vec2, size, -1);
2418 /* Negate row R1 of matrix MAT which has N columns. */
2420 static void
2421 lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
2423 lambda_vector_negate (mat[r1], mat[r1], n);
2426 /* Return true if two vectors are equal. */
2428 static bool
2429 lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
2431 int i;
2432 for (i = 0; i < size; i++)
2433 if (vec1[i] != vec2[i])
2434 return false;
2435 return true;
2438 /* Given an M x N integer matrix A, this function determines an M x
2439 M unimodular matrix U, and an M x N echelon matrix S such that
2440 "U.A = S". This decomposition is also known as "right Hermite".
2442 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2443 Restructuring Compilers" Utpal Banerjee. */
2445 static void
2446 lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
2447 lambda_matrix S, lambda_matrix U)
2449 int i, j, i0 = 0;
2451 lambda_matrix_copy (A, S, m, n);
2452 lambda_matrix_id (U, m);
2454 for (j = 0; j < n; j++)
2456 if (lambda_vector_first_nz (S[j], m, i0) < m)
2458 ++i0;
2459 for (i = m - 1; i >= i0; i--)
2461 while (S[i][j] != 0)
2463 int sigma, factor, a, b;
2465 a = S[i-1][j];
2466 b = S[i][j];
2467 sigma = (a * b < 0) ? -1: 1;
2468 a = abs (a);
2469 b = abs (b);
2470 factor = sigma * (a / b);
2472 lambda_matrix_row_add (S, n, i, i-1, -factor);
2473 std::swap (S[i], S[i-1]);
2475 lambda_matrix_row_add (U, m, i, i-1, -factor);
2476 std::swap (U[i], U[i-1]);
2483 /* Determines the overlapping elements due to accesses CHREC_A and
2484 CHREC_B, that are affine functions. This function cannot handle
2485 symbolic evolution functions, ie. when initial conditions are
2486 parameters, because it uses lambda matrices of integers. */
2488 static void
2489 analyze_subscript_affine_affine (tree chrec_a,
2490 tree chrec_b,
2491 conflict_function **overlaps_a,
2492 conflict_function **overlaps_b,
2493 tree *last_conflicts)
2495 unsigned nb_vars_a, nb_vars_b, dim;
2496 HOST_WIDE_INT init_a, init_b, gamma, gcd_alpha_beta;
2497 lambda_matrix A, U, S;
2498 struct obstack scratch_obstack;
2500 if (eq_evolutions_p (chrec_a, chrec_b))
2502 /* The accessed index overlaps for each iteration in the
2503 loop. */
2504 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2505 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2506 *last_conflicts = chrec_dont_know;
2507 return;
2509 if (dump_file && (dump_flags & TDF_DETAILS))
2510 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2512 /* For determining the initial intersection, we have to solve a
2513 Diophantine equation. This is the most time consuming part.
2515 For answering to the question: "Is there a dependence?" we have
2516 to prove that there exists a solution to the Diophantine
2517 equation, and that the solution is in the iteration domain,
2518 i.e. the solution is positive or zero, and that the solution
2519 happens before the upper bound loop.nb_iterations. Otherwise
2520 there is no dependence. This function outputs a description of
2521 the iterations that hold the intersections. */
2523 nb_vars_a = nb_vars_in_chrec (chrec_a);
2524 nb_vars_b = nb_vars_in_chrec (chrec_b);
2526 gcc_obstack_init (&scratch_obstack);
2528 dim = nb_vars_a + nb_vars_b;
2529 U = lambda_matrix_new (dim, dim, &scratch_obstack);
2530 A = lambda_matrix_new (dim, 1, &scratch_obstack);
2531 S = lambda_matrix_new (dim, 1, &scratch_obstack);
2533 init_a = int_cst_value (initialize_matrix_A (A, chrec_a, 0, 1));
2534 init_b = int_cst_value (initialize_matrix_A (A, chrec_b, nb_vars_a, -1));
2535 gamma = init_b - init_a;
2537 /* Don't do all the hard work of solving the Diophantine equation
2538 when we already know the solution: for example,
2539 | {3, +, 1}_1
2540 | {3, +, 4}_2
2541 | gamma = 3 - 3 = 0.
2542 Then the first overlap occurs during the first iterations:
2543 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2545 if (gamma == 0)
2547 if (nb_vars_a == 1 && nb_vars_b == 1)
2549 HOST_WIDE_INT step_a, step_b;
2550 HOST_WIDE_INT niter, niter_a, niter_b;
2551 affine_fn ova, ovb;
2553 niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
2554 niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
2555 niter = MIN (niter_a, niter_b);
2556 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2557 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2559 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2560 &ova, &ovb,
2561 last_conflicts, 1);
2562 *overlaps_a = conflict_fn (1, ova);
2563 *overlaps_b = conflict_fn (1, ovb);
2566 else if (nb_vars_a == 2 && nb_vars_b == 1)
2567 compute_overlap_steps_for_affine_1_2
2568 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2570 else if (nb_vars_a == 1 && nb_vars_b == 2)
2571 compute_overlap_steps_for_affine_1_2
2572 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2574 else
2576 if (dump_file && (dump_flags & TDF_DETAILS))
2577 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
2578 *overlaps_a = conflict_fn_not_known ();
2579 *overlaps_b = conflict_fn_not_known ();
2580 *last_conflicts = chrec_dont_know;
2582 goto end_analyze_subs_aa;
2585 /* U.A = S */
2586 lambda_matrix_right_hermite (A, dim, 1, S, U);
2588 if (S[0][0] < 0)
2590 S[0][0] *= -1;
2591 lambda_matrix_row_negate (U, dim, 0);
2593 gcd_alpha_beta = S[0][0];
2595 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2596 but that is a quite strange case. Instead of ICEing, answer
2597 don't know. */
2598 if (gcd_alpha_beta == 0)
2600 *overlaps_a = conflict_fn_not_known ();
2601 *overlaps_b = conflict_fn_not_known ();
2602 *last_conflicts = chrec_dont_know;
2603 goto end_analyze_subs_aa;
2606 /* The classic "gcd-test". */
2607 if (!int_divides_p (gcd_alpha_beta, gamma))
2609 /* The "gcd-test" has determined that there is no integer
2610 solution, i.e. there is no dependence. */
2611 *overlaps_a = conflict_fn_no_dependence ();
2612 *overlaps_b = conflict_fn_no_dependence ();
2613 *last_conflicts = integer_zero_node;
2616 /* Both access functions are univariate. This includes SIV and MIV cases. */
2617 else if (nb_vars_a == 1 && nb_vars_b == 1)
2619 /* Both functions should have the same evolution sign. */
2620 if (((A[0][0] > 0 && -A[1][0] > 0)
2621 || (A[0][0] < 0 && -A[1][0] < 0)))
2623 /* The solutions are given by:
2625 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2626 | [u21 u22] [y0]
2628 For a given integer t. Using the following variables,
2630 | i0 = u11 * gamma / gcd_alpha_beta
2631 | j0 = u12 * gamma / gcd_alpha_beta
2632 | i1 = u21
2633 | j1 = u22
2635 the solutions are:
2637 | x0 = i0 + i1 * t,
2638 | y0 = j0 + j1 * t. */
2639 HOST_WIDE_INT i0, j0, i1, j1;
2641 i0 = U[0][0] * gamma / gcd_alpha_beta;
2642 j0 = U[0][1] * gamma / gcd_alpha_beta;
2643 i1 = U[1][0];
2644 j1 = U[1][1];
2646 if ((i1 == 0 && i0 < 0)
2647 || (j1 == 0 && j0 < 0))
2649 /* There is no solution.
2650 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2651 falls in here, but for the moment we don't look at the
2652 upper bound of the iteration domain. */
2653 *overlaps_a = conflict_fn_no_dependence ();
2654 *overlaps_b = conflict_fn_no_dependence ();
2655 *last_conflicts = integer_zero_node;
2656 goto end_analyze_subs_aa;
2659 if (i1 > 0 && j1 > 0)
2661 HOST_WIDE_INT niter_a
2662 = max_stmt_executions_int (get_chrec_loop (chrec_a));
2663 HOST_WIDE_INT niter_b
2664 = max_stmt_executions_int (get_chrec_loop (chrec_b));
2665 HOST_WIDE_INT niter = MIN (niter_a, niter_b);
2667 /* (X0, Y0) is a solution of the Diophantine equation:
2668 "chrec_a (X0) = chrec_b (Y0)". */
2669 HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
2670 CEIL (-j0, j1));
2671 HOST_WIDE_INT x0 = i1 * tau1 + i0;
2672 HOST_WIDE_INT y0 = j1 * tau1 + j0;
2674 /* (X1, Y1) is the smallest positive solution of the eq
2675 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2676 first conflict occurs. */
2677 HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
2678 HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
2679 HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
2681 if (niter > 0)
2683 HOST_WIDE_INT tau2 = MIN (FLOOR_DIV (niter - i0, i1),
2684 FLOOR_DIV (niter - j0, j1));
2685 HOST_WIDE_INT last_conflict = tau2 - (x1 - i0)/i1;
2687 /* If the overlap occurs outside of the bounds of the
2688 loop, there is no dependence. */
2689 if (x1 >= niter || y1 >= niter)
2691 *overlaps_a = conflict_fn_no_dependence ();
2692 *overlaps_b = conflict_fn_no_dependence ();
2693 *last_conflicts = integer_zero_node;
2694 goto end_analyze_subs_aa;
2696 else
2697 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2699 else
2700 *last_conflicts = chrec_dont_know;
2702 *overlaps_a
2703 = conflict_fn (1,
2704 affine_fn_univar (build_int_cst (NULL_TREE, x1),
2706 build_int_cst (NULL_TREE, i1)));
2707 *overlaps_b
2708 = conflict_fn (1,
2709 affine_fn_univar (build_int_cst (NULL_TREE, y1),
2711 build_int_cst (NULL_TREE, j1)));
2713 else
2715 /* FIXME: For the moment, the upper bound of the
2716 iteration domain for i and j is not checked. */
2717 if (dump_file && (dump_flags & TDF_DETAILS))
2718 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2719 *overlaps_a = conflict_fn_not_known ();
2720 *overlaps_b = conflict_fn_not_known ();
2721 *last_conflicts = chrec_dont_know;
2724 else
2726 if (dump_file && (dump_flags & TDF_DETAILS))
2727 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2728 *overlaps_a = conflict_fn_not_known ();
2729 *overlaps_b = conflict_fn_not_known ();
2730 *last_conflicts = chrec_dont_know;
2733 else
2735 if (dump_file && (dump_flags & TDF_DETAILS))
2736 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2737 *overlaps_a = conflict_fn_not_known ();
2738 *overlaps_b = conflict_fn_not_known ();
2739 *last_conflicts = chrec_dont_know;
2742 end_analyze_subs_aa:
2743 obstack_free (&scratch_obstack, NULL);
2744 if (dump_file && (dump_flags & TDF_DETAILS))
2746 fprintf (dump_file, " (overlaps_a = ");
2747 dump_conflict_function (dump_file, *overlaps_a);
2748 fprintf (dump_file, ")\n (overlaps_b = ");
2749 dump_conflict_function (dump_file, *overlaps_b);
2750 fprintf (dump_file, "))\n");
2754 /* Returns true when analyze_subscript_affine_affine can be used for
2755 determining the dependence relation between chrec_a and chrec_b,
2756 that contain symbols. This function modifies chrec_a and chrec_b
2757 such that the analysis result is the same, and such that they don't
2758 contain symbols, and then can safely be passed to the analyzer.
2760 Example: The analysis of the following tuples of evolutions produce
2761 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2762 vs. {0, +, 1}_1
2764 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2765 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2768 static bool
2769 can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
2771 tree diff, type, left_a, left_b, right_b;
2773 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
2774 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
2775 /* FIXME: For the moment not handled. Might be refined later. */
2776 return false;
2778 type = chrec_type (*chrec_a);
2779 left_a = CHREC_LEFT (*chrec_a);
2780 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
2781 diff = chrec_fold_minus (type, left_a, left_b);
2783 if (!evolution_function_is_constant_p (diff))
2784 return false;
2786 if (dump_file && (dump_flags & TDF_DETAILS))
2787 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
2789 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
2790 diff, CHREC_RIGHT (*chrec_a));
2791 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
2792 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
2793 build_int_cst (type, 0),
2794 right_b);
2795 return true;
2798 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2799 *OVERLAPS_B are initialized to the functions that describe the
2800 relation between the elements accessed twice by CHREC_A and
2801 CHREC_B. For k >= 0, the following property is verified:
2803 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2805 static void
2806 analyze_siv_subscript (tree chrec_a,
2807 tree chrec_b,
2808 conflict_function **overlaps_a,
2809 conflict_function **overlaps_b,
2810 tree *last_conflicts,
2811 int loop_nest_num)
2813 dependence_stats.num_siv++;
2815 if (dump_file && (dump_flags & TDF_DETAILS))
2816 fprintf (dump_file, "(analyze_siv_subscript \n");
2818 if (evolution_function_is_constant_p (chrec_a)
2819 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2820 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
2821 overlaps_a, overlaps_b, last_conflicts);
2823 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2824 && evolution_function_is_constant_p (chrec_b))
2825 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
2826 overlaps_b, overlaps_a, last_conflicts);
2828 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2829 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2831 if (!chrec_contains_symbols (chrec_a)
2832 && !chrec_contains_symbols (chrec_b))
2834 analyze_subscript_affine_affine (chrec_a, chrec_b,
2835 overlaps_a, overlaps_b,
2836 last_conflicts);
2838 if (CF_NOT_KNOWN_P (*overlaps_a)
2839 || CF_NOT_KNOWN_P (*overlaps_b))
2840 dependence_stats.num_siv_unimplemented++;
2841 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2842 || CF_NO_DEPENDENCE_P (*overlaps_b))
2843 dependence_stats.num_siv_independent++;
2844 else
2845 dependence_stats.num_siv_dependent++;
2847 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
2848 &chrec_b))
2850 analyze_subscript_affine_affine (chrec_a, chrec_b,
2851 overlaps_a, overlaps_b,
2852 last_conflicts);
2854 if (CF_NOT_KNOWN_P (*overlaps_a)
2855 || CF_NOT_KNOWN_P (*overlaps_b))
2856 dependence_stats.num_siv_unimplemented++;
2857 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2858 || CF_NO_DEPENDENCE_P (*overlaps_b))
2859 dependence_stats.num_siv_independent++;
2860 else
2861 dependence_stats.num_siv_dependent++;
2863 else
2864 goto siv_subscript_dontknow;
2867 else
2869 siv_subscript_dontknow:;
2870 if (dump_file && (dump_flags & TDF_DETAILS))
2871 fprintf (dump_file, " siv test failed: unimplemented");
2872 *overlaps_a = conflict_fn_not_known ();
2873 *overlaps_b = conflict_fn_not_known ();
2874 *last_conflicts = chrec_dont_know;
2875 dependence_stats.num_siv_unimplemented++;
2878 if (dump_file && (dump_flags & TDF_DETAILS))
2879 fprintf (dump_file, ")\n");
2882 /* Returns false if we can prove that the greatest common divisor of the steps
2883 of CHREC does not divide CST, false otherwise. */
2885 static bool
2886 gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
2888 HOST_WIDE_INT cd = 0, val;
2889 tree step;
2891 if (!tree_fits_shwi_p (cst))
2892 return true;
2893 val = tree_to_shwi (cst);
2895 while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
2897 step = CHREC_RIGHT (chrec);
2898 if (!tree_fits_shwi_p (step))
2899 return true;
2900 cd = gcd (cd, tree_to_shwi (step));
2901 chrec = CHREC_LEFT (chrec);
2904 return val % cd == 0;
2907 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
2908 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2909 functions that describe the relation between the elements accessed
2910 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2911 is verified:
2913 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2915 static void
2916 analyze_miv_subscript (tree chrec_a,
2917 tree chrec_b,
2918 conflict_function **overlaps_a,
2919 conflict_function **overlaps_b,
2920 tree *last_conflicts,
2921 struct loop *loop_nest)
2923 tree type, difference;
2925 dependence_stats.num_miv++;
2926 if (dump_file && (dump_flags & TDF_DETAILS))
2927 fprintf (dump_file, "(analyze_miv_subscript \n");
2929 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
2930 chrec_a = chrec_convert (type, chrec_a, NULL);
2931 chrec_b = chrec_convert (type, chrec_b, NULL);
2932 difference = chrec_fold_minus (type, chrec_a, chrec_b);
2934 if (eq_evolutions_p (chrec_a, chrec_b))
2936 /* Access functions are the same: all the elements are accessed
2937 in the same order. */
2938 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2939 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2940 *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
2941 dependence_stats.num_miv_dependent++;
2944 else if (evolution_function_is_constant_p (difference)
2945 /* For the moment, the following is verified:
2946 evolution_function_is_affine_multivariate_p (chrec_a,
2947 loop_nest->num) */
2948 && !gcd_of_steps_may_divide_p (chrec_a, difference))
2950 /* testsuite/.../ssa-chrec-33.c
2951 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2953 The difference is 1, and all the evolution steps are multiples
2954 of 2, consequently there are no overlapping elements. */
2955 *overlaps_a = conflict_fn_no_dependence ();
2956 *overlaps_b = conflict_fn_no_dependence ();
2957 *last_conflicts = integer_zero_node;
2958 dependence_stats.num_miv_independent++;
2961 else if (evolution_function_is_affine_multivariate_p (chrec_a, loop_nest->num)
2962 && !chrec_contains_symbols (chrec_a)
2963 && evolution_function_is_affine_multivariate_p (chrec_b, loop_nest->num)
2964 && !chrec_contains_symbols (chrec_b))
2966 /* testsuite/.../ssa-chrec-35.c
2967 {0, +, 1}_2 vs. {0, +, 1}_3
2968 the overlapping elements are respectively located at iterations:
2969 {0, +, 1}_x and {0, +, 1}_x,
2970 in other words, we have the equality:
2971 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2973 Other examples:
2974 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2975 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2977 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2978 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2980 analyze_subscript_affine_affine (chrec_a, chrec_b,
2981 overlaps_a, overlaps_b, last_conflicts);
2983 if (CF_NOT_KNOWN_P (*overlaps_a)
2984 || CF_NOT_KNOWN_P (*overlaps_b))
2985 dependence_stats.num_miv_unimplemented++;
2986 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2987 || CF_NO_DEPENDENCE_P (*overlaps_b))
2988 dependence_stats.num_miv_independent++;
2989 else
2990 dependence_stats.num_miv_dependent++;
2993 else
2995 /* When the analysis is too difficult, answer "don't know". */
2996 if (dump_file && (dump_flags & TDF_DETAILS))
2997 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
2999 *overlaps_a = conflict_fn_not_known ();
3000 *overlaps_b = conflict_fn_not_known ();
3001 *last_conflicts = chrec_dont_know;
3002 dependence_stats.num_miv_unimplemented++;
3005 if (dump_file && (dump_flags & TDF_DETAILS))
3006 fprintf (dump_file, ")\n");
3009 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
3010 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
3011 OVERLAP_ITERATIONS_B are initialized with two functions that
3012 describe the iterations that contain conflicting elements.
3014 Remark: For an integer k >= 0, the following equality is true:
3016 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
3019 static void
3020 analyze_overlapping_iterations (tree chrec_a,
3021 tree chrec_b,
3022 conflict_function **overlap_iterations_a,
3023 conflict_function **overlap_iterations_b,
3024 tree *last_conflicts, struct loop *loop_nest)
3026 unsigned int lnn = loop_nest->num;
3028 dependence_stats.num_subscript_tests++;
3030 if (dump_file && (dump_flags & TDF_DETAILS))
3032 fprintf (dump_file, "(analyze_overlapping_iterations \n");
3033 fprintf (dump_file, " (chrec_a = ");
3034 print_generic_expr (dump_file, chrec_a, 0);
3035 fprintf (dump_file, ")\n (chrec_b = ");
3036 print_generic_expr (dump_file, chrec_b, 0);
3037 fprintf (dump_file, ")\n");
3040 if (chrec_a == NULL_TREE
3041 || chrec_b == NULL_TREE
3042 || chrec_contains_undetermined (chrec_a)
3043 || chrec_contains_undetermined (chrec_b))
3045 dependence_stats.num_subscript_undetermined++;
3047 *overlap_iterations_a = conflict_fn_not_known ();
3048 *overlap_iterations_b = conflict_fn_not_known ();
3051 /* If they are the same chrec, and are affine, they overlap
3052 on every iteration. */
3053 else if (eq_evolutions_p (chrec_a, chrec_b)
3054 && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3055 || operand_equal_p (chrec_a, chrec_b, 0)))
3057 dependence_stats.num_same_subscript_function++;
3058 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3059 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3060 *last_conflicts = chrec_dont_know;
3063 /* If they aren't the same, and aren't affine, we can't do anything
3064 yet. */
3065 else if ((chrec_contains_symbols (chrec_a)
3066 || chrec_contains_symbols (chrec_b))
3067 && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3068 || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
3070 dependence_stats.num_subscript_undetermined++;
3071 *overlap_iterations_a = conflict_fn_not_known ();
3072 *overlap_iterations_b = conflict_fn_not_known ();
3075 else if (ziv_subscript_p (chrec_a, chrec_b))
3076 analyze_ziv_subscript (chrec_a, chrec_b,
3077 overlap_iterations_a, overlap_iterations_b,
3078 last_conflicts);
3080 else if (siv_subscript_p (chrec_a, chrec_b))
3081 analyze_siv_subscript (chrec_a, chrec_b,
3082 overlap_iterations_a, overlap_iterations_b,
3083 last_conflicts, lnn);
3085 else
3086 analyze_miv_subscript (chrec_a, chrec_b,
3087 overlap_iterations_a, overlap_iterations_b,
3088 last_conflicts, loop_nest);
3090 if (dump_file && (dump_flags & TDF_DETAILS))
3092 fprintf (dump_file, " (overlap_iterations_a = ");
3093 dump_conflict_function (dump_file, *overlap_iterations_a);
3094 fprintf (dump_file, ")\n (overlap_iterations_b = ");
3095 dump_conflict_function (dump_file, *overlap_iterations_b);
3096 fprintf (dump_file, "))\n");
3100 /* Helper function for uniquely inserting distance vectors. */
3102 static void
3103 save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
3105 unsigned i;
3106 lambda_vector v;
3108 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, v)
3109 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
3110 return;
3112 DDR_DIST_VECTS (ddr).safe_push (dist_v);
3115 /* Helper function for uniquely inserting direction vectors. */
3117 static void
3118 save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
3120 unsigned i;
3121 lambda_vector v;
3123 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), i, v)
3124 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
3125 return;
3127 DDR_DIR_VECTS (ddr).safe_push (dir_v);
3130 /* Add a distance of 1 on all the loops outer than INDEX. If we
3131 haven't yet determined a distance for this outer loop, push a new
3132 distance vector composed of the previous distance, and a distance
3133 of 1 for this outer loop. Example:
3135 | loop_1
3136 | loop_2
3137 | A[10]
3138 | endloop_2
3139 | endloop_1
3141 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3142 save (0, 1), then we have to save (1, 0). */
3144 static void
3145 add_outer_distances (struct data_dependence_relation *ddr,
3146 lambda_vector dist_v, int index)
3148 /* For each outer loop where init_v is not set, the accesses are
3149 in dependence of distance 1 in the loop. */
3150 while (--index >= 0)
3152 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3153 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3154 save_v[index] = 1;
3155 save_dist_v (ddr, save_v);
3159 /* Return false when fail to represent the data dependence as a
3160 distance vector. INIT_B is set to true when a component has been
3161 added to the distance vector DIST_V. INDEX_CARRY is then set to
3162 the index in DIST_V that carries the dependence. */
3164 static bool
3165 build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
3166 struct data_reference *ddr_a,
3167 struct data_reference *ddr_b,
3168 lambda_vector dist_v, bool *init_b,
3169 int *index_carry)
3171 unsigned i;
3172 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3174 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3176 tree access_fn_a, access_fn_b;
3177 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3179 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3181 non_affine_dependence_relation (ddr);
3182 return false;
3185 access_fn_a = DR_ACCESS_FN (ddr_a, i);
3186 access_fn_b = DR_ACCESS_FN (ddr_b, i);
3188 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3189 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3191 int dist, index;
3192 int var_a = CHREC_VARIABLE (access_fn_a);
3193 int var_b = CHREC_VARIABLE (access_fn_b);
3195 if (var_a != var_b
3196 || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3198 non_affine_dependence_relation (ddr);
3199 return false;
3202 dist = int_cst_value (SUB_DISTANCE (subscript));
3203 index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
3204 *index_carry = MIN (index, *index_carry);
3206 /* This is the subscript coupling test. If we have already
3207 recorded a distance for this loop (a distance coming from
3208 another subscript), it should be the same. For example,
3209 in the following code, there is no dependence:
3211 | loop i = 0, N, 1
3212 | T[i+1][i] = ...
3213 | ... = T[i][i]
3214 | endloop
3216 if (init_v[index] != 0 && dist_v[index] != dist)
3218 finalize_ddr_dependent (ddr, chrec_known);
3219 return false;
3222 dist_v[index] = dist;
3223 init_v[index] = 1;
3224 *init_b = true;
3226 else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
3228 /* This can be for example an affine vs. constant dependence
3229 (T[i] vs. T[3]) that is not an affine dependence and is
3230 not representable as a distance vector. */
3231 non_affine_dependence_relation (ddr);
3232 return false;
3236 return true;
3239 /* Return true when the DDR contains only constant access functions. */
3241 static bool
3242 constant_access_functions (const struct data_dependence_relation *ddr)
3244 unsigned i;
3246 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3247 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr), i))
3248 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr), i)))
3249 return false;
3251 return true;
3254 /* Helper function for the case where DDR_A and DDR_B are the same
3255 multivariate access function with a constant step. For an example
3256 see pr34635-1.c. */
3258 static void
3259 add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
3261 int x_1, x_2;
3262 tree c_1 = CHREC_LEFT (c_2);
3263 tree c_0 = CHREC_LEFT (c_1);
3264 lambda_vector dist_v;
3265 int v1, v2, cd;
3267 /* Polynomials with more than 2 variables are not handled yet. When
3268 the evolution steps are parameters, it is not possible to
3269 represent the dependence using classical distance vectors. */
3270 if (TREE_CODE (c_0) != INTEGER_CST
3271 || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
3272 || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
3274 DDR_AFFINE_P (ddr) = false;
3275 return;
3278 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
3279 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
3281 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3282 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3283 v1 = int_cst_value (CHREC_RIGHT (c_1));
3284 v2 = int_cst_value (CHREC_RIGHT (c_2));
3285 cd = gcd (v1, v2);
3286 v1 /= cd;
3287 v2 /= cd;
3289 if (v2 < 0)
3291 v2 = -v2;
3292 v1 = -v1;
3295 dist_v[x_1] = v2;
3296 dist_v[x_2] = -v1;
3297 save_dist_v (ddr, dist_v);
3299 add_outer_distances (ddr, dist_v, x_1);
3302 /* Helper function for the case where DDR_A and DDR_B are the same
3303 access functions. */
3305 static void
3306 add_other_self_distances (struct data_dependence_relation *ddr)
3308 lambda_vector dist_v;
3309 unsigned i;
3310 int index_carry = DDR_NB_LOOPS (ddr);
3312 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3314 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
3316 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
3318 if (!evolution_function_is_univariate_p (access_fun))
3320 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
3322 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3323 return;
3326 access_fun = DR_ACCESS_FN (DDR_A (ddr), 0);
3328 if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
3329 add_multivariate_self_dist (ddr, access_fun);
3330 else
3331 /* The evolution step is not constant: it varies in
3332 the outer loop, so this cannot be represented by a
3333 distance vector. For example in pr34635.c the
3334 evolution is {0, +, {0, +, 4}_1}_2. */
3335 DDR_AFFINE_P (ddr) = false;
3337 return;
3340 index_carry = MIN (index_carry,
3341 index_in_loop_nest (CHREC_VARIABLE (access_fun),
3342 DDR_LOOP_NEST (ddr)));
3346 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3347 add_outer_distances (ddr, dist_v, index_carry);
3350 static void
3351 insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
3353 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3355 dist_v[DDR_INNER_LOOP (ddr)] = 1;
3356 save_dist_v (ddr, dist_v);
3359 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
3360 is the case for example when access functions are the same and
3361 equal to a constant, as in:
3363 | loop_1
3364 | A[3] = ...
3365 | ... = A[3]
3366 | endloop_1
3368 in which case the distance vectors are (0) and (1). */
3370 static void
3371 add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
3373 unsigned i, j;
3375 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3377 subscript_p sub = DDR_SUBSCRIPT (ddr, i);
3378 conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
3379 conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
3381 for (j = 0; j < ca->n; j++)
3382 if (affine_function_zero_p (ca->fns[j]))
3384 insert_innermost_unit_dist_vector (ddr);
3385 return;
3388 for (j = 0; j < cb->n; j++)
3389 if (affine_function_zero_p (cb->fns[j]))
3391 insert_innermost_unit_dist_vector (ddr);
3392 return;
3397 /* Compute the classic per loop distance vector. DDR is the data
3398 dependence relation to build a vector from. Return false when fail
3399 to represent the data dependence as a distance vector. */
3401 static bool
3402 build_classic_dist_vector (struct data_dependence_relation *ddr,
3403 struct loop *loop_nest)
3405 bool init_b = false;
3406 int index_carry = DDR_NB_LOOPS (ddr);
3407 lambda_vector dist_v;
3409 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3410 return false;
3412 if (same_access_functions (ddr))
3414 /* Save the 0 vector. */
3415 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3416 save_dist_v (ddr, dist_v);
3418 if (constant_access_functions (ddr))
3419 add_distance_for_zero_overlaps (ddr);
3421 if (DDR_NB_LOOPS (ddr) > 1)
3422 add_other_self_distances (ddr);
3424 return true;
3427 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3428 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3429 dist_v, &init_b, &index_carry))
3430 return false;
3432 /* Save the distance vector if we initialized one. */
3433 if (init_b)
3435 /* Verify a basic constraint: classic distance vectors should
3436 always be lexicographically positive.
3438 Data references are collected in the order of execution of
3439 the program, thus for the following loop
3441 | for (i = 1; i < 100; i++)
3442 | for (j = 1; j < 100; j++)
3444 | t = T[j+1][i-1]; // A
3445 | T[j][i] = t + 2; // B
3448 references are collected following the direction of the wind:
3449 A then B. The data dependence tests are performed also
3450 following this order, such that we're looking at the distance
3451 separating the elements accessed by A from the elements later
3452 accessed by B. But in this example, the distance returned by
3453 test_dep (A, B) is lexicographically negative (-1, 1), that
3454 means that the access A occurs later than B with respect to
3455 the outer loop, ie. we're actually looking upwind. In this
3456 case we solve test_dep (B, A) looking downwind to the
3457 lexicographically positive solution, that returns the
3458 distance vector (1, -1). */
3459 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3461 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3462 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3463 loop_nest))
3464 return false;
3465 compute_subscript_distance (ddr);
3466 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3467 save_v, &init_b, &index_carry))
3468 return false;
3469 save_dist_v (ddr, save_v);
3470 DDR_REVERSED_P (ddr) = true;
3472 /* In this case there is a dependence forward for all the
3473 outer loops:
3475 | for (k = 1; k < 100; k++)
3476 | for (i = 1; i < 100; i++)
3477 | for (j = 1; j < 100; j++)
3479 | t = T[j+1][i-1]; // A
3480 | T[j][i] = t + 2; // B
3483 the vectors are:
3484 (0, 1, -1)
3485 (1, 1, -1)
3486 (1, -1, 1)
3488 if (DDR_NB_LOOPS (ddr) > 1)
3490 add_outer_distances (ddr, save_v, index_carry);
3491 add_outer_distances (ddr, dist_v, index_carry);
3494 else
3496 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3497 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3499 if (DDR_NB_LOOPS (ddr) > 1)
3501 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3503 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr),
3504 DDR_A (ddr), loop_nest))
3505 return false;
3506 compute_subscript_distance (ddr);
3507 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3508 opposite_v, &init_b,
3509 &index_carry))
3510 return false;
3512 save_dist_v (ddr, save_v);
3513 add_outer_distances (ddr, dist_v, index_carry);
3514 add_outer_distances (ddr, opposite_v, index_carry);
3516 else
3517 save_dist_v (ddr, save_v);
3520 else
3522 /* There is a distance of 1 on all the outer loops: Example:
3523 there is a dependence of distance 1 on loop_1 for the array A.
3525 | loop_1
3526 | A[5] = ...
3527 | endloop
3529 add_outer_distances (ddr, dist_v,
3530 lambda_vector_first_nz (dist_v,
3531 DDR_NB_LOOPS (ddr), 0));
3534 if (dump_file && (dump_flags & TDF_DETAILS))
3536 unsigned i;
3538 fprintf (dump_file, "(build_classic_dist_vector\n");
3539 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3541 fprintf (dump_file, " dist_vector = (");
3542 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3543 DDR_NB_LOOPS (ddr));
3544 fprintf (dump_file, " )\n");
3546 fprintf (dump_file, ")\n");
3549 return true;
3552 /* Return the direction for a given distance.
3553 FIXME: Computing dir this way is suboptimal, since dir can catch
3554 cases that dist is unable to represent. */
3556 static inline enum data_dependence_direction
3557 dir_from_dist (int dist)
3559 if (dist > 0)
3560 return dir_positive;
3561 else if (dist < 0)
3562 return dir_negative;
3563 else
3564 return dir_equal;
3567 /* Compute the classic per loop direction vector. DDR is the data
3568 dependence relation to build a vector from. */
3570 static void
3571 build_classic_dir_vector (struct data_dependence_relation *ddr)
3573 unsigned i, j;
3574 lambda_vector dist_v;
3576 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
3578 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3580 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3581 dir_v[j] = dir_from_dist (dist_v[j]);
3583 save_dir_v (ddr, dir_v);
3587 /* Helper function. Returns true when there is a dependence between
3588 data references DRA and DRB. */
3590 static bool
3591 subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
3592 struct data_reference *dra,
3593 struct data_reference *drb,
3594 struct loop *loop_nest)
3596 unsigned int i;
3597 tree last_conflicts;
3598 struct subscript *subscript;
3599 tree res = NULL_TREE;
3601 for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
3603 conflict_function *overlaps_a, *overlaps_b;
3605 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3606 DR_ACCESS_FN (drb, i),
3607 &overlaps_a, &overlaps_b,
3608 &last_conflicts, loop_nest);
3610 if (SUB_CONFLICTS_IN_A (subscript))
3611 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
3612 if (SUB_CONFLICTS_IN_B (subscript))
3613 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
3615 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3616 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3617 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3619 /* If there is any undetermined conflict function we have to
3620 give a conservative answer in case we cannot prove that
3621 no dependence exists when analyzing another subscript. */
3622 if (CF_NOT_KNOWN_P (overlaps_a)
3623 || CF_NOT_KNOWN_P (overlaps_b))
3625 res = chrec_dont_know;
3626 continue;
3629 /* When there is a subscript with no dependence we can stop. */
3630 else if (CF_NO_DEPENDENCE_P (overlaps_a)
3631 || CF_NO_DEPENDENCE_P (overlaps_b))
3633 res = chrec_known;
3634 break;
3638 if (res == NULL_TREE)
3639 return true;
3641 if (res == chrec_known)
3642 dependence_stats.num_dependence_independent++;
3643 else
3644 dependence_stats.num_dependence_undetermined++;
3645 finalize_ddr_dependent (ddr, res);
3646 return false;
3649 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
3651 static void
3652 subscript_dependence_tester (struct data_dependence_relation *ddr,
3653 struct loop *loop_nest)
3655 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr), loop_nest))
3656 dependence_stats.num_dependence_dependent++;
3658 compute_subscript_distance (ddr);
3659 if (build_classic_dist_vector (ddr, loop_nest))
3660 build_classic_dir_vector (ddr);
3663 /* Returns true when all the access functions of A are affine or
3664 constant with respect to LOOP_NEST. */
3666 static bool
3667 access_functions_are_affine_or_constant_p (const struct data_reference *a,
3668 const struct loop *loop_nest)
3670 unsigned int i;
3671 vec<tree> fns = DR_ACCESS_FNS (a);
3672 tree t;
3674 FOR_EACH_VEC_ELT (fns, i, t)
3675 if (!evolution_function_is_invariant_p (t, loop_nest->num)
3676 && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
3677 return false;
3679 return true;
3682 /* This computes the affine dependence relation between A and B with
3683 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
3684 independence between two accesses, while CHREC_DONT_KNOW is used
3685 for representing the unknown relation.
3687 Note that it is possible to stop the computation of the dependence
3688 relation the first time we detect a CHREC_KNOWN element for a given
3689 subscript. */
3691 void
3692 compute_affine_dependence (struct data_dependence_relation *ddr,
3693 struct loop *loop_nest)
3695 struct data_reference *dra = DDR_A (ddr);
3696 struct data_reference *drb = DDR_B (ddr);
3698 if (dump_file && (dump_flags & TDF_DETAILS))
3700 fprintf (dump_file, "(compute_affine_dependence\n");
3701 fprintf (dump_file, " stmt_a: ");
3702 print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
3703 fprintf (dump_file, " stmt_b: ");
3704 print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
3707 /* Analyze only when the dependence relation is not yet known. */
3708 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
3710 dependence_stats.num_dependence_tests++;
3712 if (access_functions_are_affine_or_constant_p (dra, loop_nest)
3713 && access_functions_are_affine_or_constant_p (drb, loop_nest))
3714 subscript_dependence_tester (ddr, loop_nest);
3716 /* As a last case, if the dependence cannot be determined, or if
3717 the dependence is considered too difficult to determine, answer
3718 "don't know". */
3719 else
3721 dependence_stats.num_dependence_undetermined++;
3723 if (dump_file && (dump_flags & TDF_DETAILS))
3725 fprintf (dump_file, "Data ref a:\n");
3726 dump_data_reference (dump_file, dra);
3727 fprintf (dump_file, "Data ref b:\n");
3728 dump_data_reference (dump_file, drb);
3729 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
3731 finalize_ddr_dependent (ddr, chrec_dont_know);
3735 if (dump_file && (dump_flags & TDF_DETAILS))
3737 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
3738 fprintf (dump_file, ") -> no dependence\n");
3739 else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
3740 fprintf (dump_file, ") -> dependence analysis failed\n");
3741 else
3742 fprintf (dump_file, ")\n");
3746 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
3747 the data references in DATAREFS, in the LOOP_NEST. When
3748 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
3749 relations. Return true when successful, i.e. data references number
3750 is small enough to be handled. */
3752 bool
3753 compute_all_dependences (vec<data_reference_p> datarefs,
3754 vec<ddr_p> *dependence_relations,
3755 vec<loop_p> loop_nest,
3756 bool compute_self_and_rr)
3758 struct data_dependence_relation *ddr;
3759 struct data_reference *a, *b;
3760 unsigned int i, j;
3762 if ((int) datarefs.length ()
3763 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
3765 struct data_dependence_relation *ddr;
3767 /* Insert a single relation into dependence_relations:
3768 chrec_dont_know. */
3769 ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
3770 dependence_relations->safe_push (ddr);
3771 return false;
3774 FOR_EACH_VEC_ELT (datarefs, i, a)
3775 for (j = i + 1; datarefs.iterate (j, &b); j++)
3776 if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
3778 ddr = initialize_data_dependence_relation (a, b, loop_nest);
3779 dependence_relations->safe_push (ddr);
3780 if (loop_nest.exists ())
3781 compute_affine_dependence (ddr, loop_nest[0]);
3784 if (compute_self_and_rr)
3785 FOR_EACH_VEC_ELT (datarefs, i, a)
3787 ddr = initialize_data_dependence_relation (a, a, loop_nest);
3788 dependence_relations->safe_push (ddr);
3789 if (loop_nest.exists ())
3790 compute_affine_dependence (ddr, loop_nest[0]);
3793 return true;
3796 /* Describes a location of a memory reference. */
3798 struct data_ref_loc
3800 /* The memory reference. */
3801 tree ref;
3803 /* True if the memory reference is read. */
3804 bool is_read;
3808 /* Stores the locations of memory references in STMT to REFERENCES. Returns
3809 true if STMT clobbers memory, false otherwise. */
3811 static bool
3812 get_references_in_stmt (gimple *stmt, vec<data_ref_loc, va_heap> *references)
3814 bool clobbers_memory = false;
3815 data_ref_loc ref;
3816 tree op0, op1;
3817 enum gimple_code stmt_code = gimple_code (stmt);
3819 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
3820 As we cannot model data-references to not spelled out
3821 accesses give up if they may occur. */
3822 if (stmt_code == GIMPLE_CALL
3823 && !(gimple_call_flags (stmt) & ECF_CONST))
3825 /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
3826 if (gimple_call_internal_p (stmt))
3827 switch (gimple_call_internal_fn (stmt))
3829 case IFN_GOMP_SIMD_LANE:
3831 struct loop *loop = gimple_bb (stmt)->loop_father;
3832 tree uid = gimple_call_arg (stmt, 0);
3833 gcc_assert (TREE_CODE (uid) == SSA_NAME);
3834 if (loop == NULL
3835 || loop->simduid != SSA_NAME_VAR (uid))
3836 clobbers_memory = true;
3837 break;
3839 case IFN_MASK_LOAD:
3840 case IFN_MASK_STORE:
3841 break;
3842 default:
3843 clobbers_memory = true;
3844 break;
3846 else
3847 clobbers_memory = true;
3849 else if (stmt_code == GIMPLE_ASM
3850 && (gimple_asm_volatile_p (as_a <gasm *> (stmt))
3851 || gimple_vuse (stmt)))
3852 clobbers_memory = true;
3854 if (!gimple_vuse (stmt))
3855 return clobbers_memory;
3857 if (stmt_code == GIMPLE_ASSIGN)
3859 tree base;
3860 op0 = gimple_assign_lhs (stmt);
3861 op1 = gimple_assign_rhs1 (stmt);
3863 if (DECL_P (op1)
3864 || (REFERENCE_CLASS_P (op1)
3865 && (base = get_base_address (op1))
3866 && TREE_CODE (base) != SSA_NAME))
3868 ref.ref = op1;
3869 ref.is_read = true;
3870 references->safe_push (ref);
3873 else if (stmt_code == GIMPLE_CALL)
3875 unsigned i, n;
3876 tree ptr, type;
3877 unsigned int align;
3879 ref.is_read = false;
3880 if (gimple_call_internal_p (stmt))
3881 switch (gimple_call_internal_fn (stmt))
3883 case IFN_MASK_LOAD:
3884 if (gimple_call_lhs (stmt) == NULL_TREE)
3885 break;
3886 ref.is_read = true;
3887 case IFN_MASK_STORE:
3888 ptr = build_int_cst (TREE_TYPE (gimple_call_arg (stmt, 1)), 0);
3889 align = tree_to_shwi (gimple_call_arg (stmt, 1));
3890 if (ref.is_read)
3891 type = TREE_TYPE (gimple_call_lhs (stmt));
3892 else
3893 type = TREE_TYPE (gimple_call_arg (stmt, 3));
3894 if (TYPE_ALIGN (type) != align)
3895 type = build_aligned_type (type, align);
3896 ref.ref = fold_build2 (MEM_REF, type, gimple_call_arg (stmt, 0),
3897 ptr);
3898 references->safe_push (ref);
3899 return false;
3900 default:
3901 break;
3904 op0 = gimple_call_lhs (stmt);
3905 n = gimple_call_num_args (stmt);
3906 for (i = 0; i < n; i++)
3908 op1 = gimple_call_arg (stmt, i);
3910 if (DECL_P (op1)
3911 || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
3913 ref.ref = op1;
3914 ref.is_read = true;
3915 references->safe_push (ref);
3919 else
3920 return clobbers_memory;
3922 if (op0
3923 && (DECL_P (op0)
3924 || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
3926 ref.ref = op0;
3927 ref.is_read = false;
3928 references->safe_push (ref);
3930 return clobbers_memory;
3934 /* Returns true if the loop-nest has any data reference. */
3936 bool
3937 loop_nest_has_data_refs (loop_p loop)
3939 basic_block *bbs = get_loop_body (loop);
3940 vec<data_ref_loc> references;
3941 references.create (3);
3943 for (unsigned i = 0; i < loop->num_nodes; i++)
3945 basic_block bb = bbs[i];
3946 gimple_stmt_iterator bsi;
3948 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
3950 gimple *stmt = gsi_stmt (bsi);
3951 get_references_in_stmt (stmt, &references);
3952 if (references.length ())
3954 free (bbs);
3955 references.release ();
3956 return true;
3960 free (bbs);
3961 references.release ();
3963 if (loop->inner)
3965 loop = loop->inner;
3966 while (loop)
3968 if (loop_nest_has_data_refs (loop))
3969 return true;
3970 loop = loop->next;
3973 return false;
3976 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
3977 reference, returns false, otherwise returns true. NEST is the outermost
3978 loop of the loop nest in which the references should be analyzed. */
3980 bool
3981 find_data_references_in_stmt (struct loop *nest, gimple *stmt,
3982 vec<data_reference_p> *datarefs)
3984 unsigned i;
3985 auto_vec<data_ref_loc, 2> references;
3986 data_ref_loc *ref;
3987 bool ret = true;
3988 data_reference_p dr;
3990 if (get_references_in_stmt (stmt, &references))
3991 return false;
3993 FOR_EACH_VEC_ELT (references, i, ref)
3995 dr = create_data_ref (nest, loop_containing_stmt (stmt),
3996 ref->ref, stmt, ref->is_read);
3997 gcc_assert (dr != NULL);
3998 datarefs->safe_push (dr);
4000 references.release ();
4001 return ret;
4004 /* Stores the data references in STMT to DATAREFS. If there is an
4005 unanalyzable reference, returns false, otherwise returns true.
4006 NEST is the outermost loop of the loop nest in which the references
4007 should be instantiated, LOOP is the loop in which the references
4008 should be analyzed. */
4010 bool
4011 graphite_find_data_references_in_stmt (loop_p nest, loop_p loop, gimple *stmt,
4012 vec<data_reference_p> *datarefs)
4014 unsigned i;
4015 auto_vec<data_ref_loc, 2> references;
4016 data_ref_loc *ref;
4017 bool ret = true;
4018 data_reference_p dr;
4020 if (get_references_in_stmt (stmt, &references))
4021 return false;
4023 FOR_EACH_VEC_ELT (references, i, ref)
4025 dr = create_data_ref (nest, loop, ref->ref, stmt, ref->is_read);
4026 gcc_assert (dr != NULL);
4027 datarefs->safe_push (dr);
4030 references.release ();
4031 return ret;
4034 /* Search the data references in LOOP, and record the information into
4035 DATAREFS. Returns chrec_dont_know when failing to analyze a
4036 difficult case, returns NULL_TREE otherwise. */
4038 tree
4039 find_data_references_in_bb (struct loop *loop, basic_block bb,
4040 vec<data_reference_p> *datarefs)
4042 gimple_stmt_iterator bsi;
4044 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4046 gimple *stmt = gsi_stmt (bsi);
4048 if (!find_data_references_in_stmt (loop, stmt, datarefs))
4050 struct data_reference *res;
4051 res = XCNEW (struct data_reference);
4052 datarefs->safe_push (res);
4054 return chrec_dont_know;
4058 return NULL_TREE;
4061 /* Search the data references in LOOP, and record the information into
4062 DATAREFS. Returns chrec_dont_know when failing to analyze a
4063 difficult case, returns NULL_TREE otherwise.
4065 TODO: This function should be made smarter so that it can handle address
4066 arithmetic as if they were array accesses, etc. */
4068 tree
4069 find_data_references_in_loop (struct loop *loop,
4070 vec<data_reference_p> *datarefs)
4072 basic_block bb, *bbs;
4073 unsigned int i;
4075 bbs = get_loop_body_in_dom_order (loop);
4077 for (i = 0; i < loop->num_nodes; i++)
4079 bb = bbs[i];
4081 if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
4083 free (bbs);
4084 return chrec_dont_know;
4087 free (bbs);
4089 return NULL_TREE;
4092 /* Recursive helper function. */
4094 static bool
4095 find_loop_nest_1 (struct loop *loop, vec<loop_p> *loop_nest)
4097 /* Inner loops of the nest should not contain siblings. Example:
4098 when there are two consecutive loops,
4100 | loop_0
4101 | loop_1
4102 | A[{0, +, 1}_1]
4103 | endloop_1
4104 | loop_2
4105 | A[{0, +, 1}_2]
4106 | endloop_2
4107 | endloop_0
4109 the dependence relation cannot be captured by the distance
4110 abstraction. */
4111 if (loop->next)
4112 return false;
4114 loop_nest->safe_push (loop);
4115 if (loop->inner)
4116 return find_loop_nest_1 (loop->inner, loop_nest);
4117 return true;
4120 /* Return false when the LOOP is not well nested. Otherwise return
4121 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4122 contain the loops from the outermost to the innermost, as they will
4123 appear in the classic distance vector. */
4125 bool
4126 find_loop_nest (struct loop *loop, vec<loop_p> *loop_nest)
4128 loop_nest->safe_push (loop);
4129 if (loop->inner)
4130 return find_loop_nest_1 (loop->inner, loop_nest);
4131 return true;
4134 /* Returns true when the data dependences have been computed, false otherwise.
4135 Given a loop nest LOOP, the following vectors are returned:
4136 DATAREFS is initialized to all the array elements contained in this loop,
4137 DEPENDENCE_RELATIONS contains the relations between the data references.
4138 Compute read-read and self relations if
4139 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4141 bool
4142 compute_data_dependences_for_loop (struct loop *loop,
4143 bool compute_self_and_read_read_dependences,
4144 vec<loop_p> *loop_nest,
4145 vec<data_reference_p> *datarefs,
4146 vec<ddr_p> *dependence_relations)
4148 bool res = true;
4150 memset (&dependence_stats, 0, sizeof (dependence_stats));
4152 /* If the loop nest is not well formed, or one of the data references
4153 is not computable, give up without spending time to compute other
4154 dependences. */
4155 if (!loop
4156 || !find_loop_nest (loop, loop_nest)
4157 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
4158 || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
4159 compute_self_and_read_read_dependences))
4160 res = false;
4162 if (dump_file && (dump_flags & TDF_STATS))
4164 fprintf (dump_file, "Dependence tester statistics:\n");
4166 fprintf (dump_file, "Number of dependence tests: %d\n",
4167 dependence_stats.num_dependence_tests);
4168 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
4169 dependence_stats.num_dependence_dependent);
4170 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
4171 dependence_stats.num_dependence_independent);
4172 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
4173 dependence_stats.num_dependence_undetermined);
4175 fprintf (dump_file, "Number of subscript tests: %d\n",
4176 dependence_stats.num_subscript_tests);
4177 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
4178 dependence_stats.num_subscript_undetermined);
4179 fprintf (dump_file, "Number of same subscript function: %d\n",
4180 dependence_stats.num_same_subscript_function);
4182 fprintf (dump_file, "Number of ziv tests: %d\n",
4183 dependence_stats.num_ziv);
4184 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4185 dependence_stats.num_ziv_dependent);
4186 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4187 dependence_stats.num_ziv_independent);
4188 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
4189 dependence_stats.num_ziv_unimplemented);
4191 fprintf (dump_file, "Number of siv tests: %d\n",
4192 dependence_stats.num_siv);
4193 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4194 dependence_stats.num_siv_dependent);
4195 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4196 dependence_stats.num_siv_independent);
4197 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4198 dependence_stats.num_siv_unimplemented);
4200 fprintf (dump_file, "Number of miv tests: %d\n",
4201 dependence_stats.num_miv);
4202 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4203 dependence_stats.num_miv_dependent);
4204 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4205 dependence_stats.num_miv_independent);
4206 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4207 dependence_stats.num_miv_unimplemented);
4210 return res;
4213 /* Free the memory used by a data dependence relation DDR. */
4215 void
4216 free_dependence_relation (struct data_dependence_relation *ddr)
4218 if (ddr == NULL)
4219 return;
4221 if (DDR_SUBSCRIPTS (ddr).exists ())
4222 free_subscripts (DDR_SUBSCRIPTS (ddr));
4223 DDR_DIST_VECTS (ddr).release ();
4224 DDR_DIR_VECTS (ddr).release ();
4226 free (ddr);
4229 /* Free the memory used by the data dependence relations from
4230 DEPENDENCE_RELATIONS. */
4232 void
4233 free_dependence_relations (vec<ddr_p> dependence_relations)
4235 unsigned int i;
4236 struct data_dependence_relation *ddr;
4238 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4239 if (ddr)
4240 free_dependence_relation (ddr);
4242 dependence_relations.release ();
4245 /* Free the memory used by the data references from DATAREFS. */
4247 void
4248 free_data_refs (vec<data_reference_p> datarefs)
4250 unsigned int i;
4251 struct data_reference *dr;
4253 FOR_EACH_VEC_ELT (datarefs, i, dr)
4254 free_data_ref (dr);
4255 datarefs.release ();