PR middle-end/66633
[official-gcc.git] / gcc / tree-data-ref.c
blobcc79a7f6fb58a09549aca36f37ba9bcb69f22afd
1 /* Data references and dependences detectors.
2 Copyright (C) 2003-2015 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 "alias.h"
80 #include "symtab.h"
81 #include "options.h"
82 #include "tree.h"
83 #include "fold-const.h"
84 #include "tm.h"
85 #include "hard-reg-set.h"
86 #include "function.h"
87 #include "rtl.h"
88 #include "flags.h"
89 #include "insn-config.h"
90 #include "expmed.h"
91 #include "dojump.h"
92 #include "explow.h"
93 #include "calls.h"
94 #include "emit-rtl.h"
95 #include "varasm.h"
96 #include "stmt.h"
97 #include "expr.h"
98 #include "gimple-pretty-print.h"
99 #include "predict.h"
100 #include "dominance.h"
101 #include "cfg.h"
102 #include "basic-block.h"
103 #include "tree-ssa-alias.h"
104 #include "internal-fn.h"
105 #include "gimple-expr.h"
106 #include "gimple.h"
107 #include "gimple-iterator.h"
108 #include "tree-ssa-loop-niter.h"
109 #include "tree-ssa-loop.h"
110 #include "tree-ssa.h"
111 #include "cfgloop.h"
112 #include "tree-data-ref.h"
113 #include "tree-scalar-evolution.h"
114 #include "dumpfile.h"
115 #include "langhooks.h"
116 #include "tree-affine.h"
117 #include "params.h"
119 static struct datadep_stats
121 int num_dependence_tests;
122 int num_dependence_dependent;
123 int num_dependence_independent;
124 int num_dependence_undetermined;
126 int num_subscript_tests;
127 int num_subscript_undetermined;
128 int num_same_subscript_function;
130 int num_ziv;
131 int num_ziv_independent;
132 int num_ziv_dependent;
133 int num_ziv_unimplemented;
135 int num_siv;
136 int num_siv_independent;
137 int num_siv_dependent;
138 int num_siv_unimplemented;
140 int num_miv;
141 int num_miv_independent;
142 int num_miv_dependent;
143 int num_miv_unimplemented;
144 } dependence_stats;
146 static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
147 struct data_reference *,
148 struct data_reference *,
149 struct loop *);
150 /* Returns true iff A divides B. */
152 static inline bool
153 tree_fold_divides_p (const_tree a, const_tree b)
155 gcc_assert (TREE_CODE (a) == INTEGER_CST);
156 gcc_assert (TREE_CODE (b) == INTEGER_CST);
157 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a));
160 /* Returns true iff A divides B. */
162 static inline bool
163 int_divides_p (int a, int b)
165 return ((b % a) == 0);
170 /* Dump into FILE all the data references from DATAREFS. */
172 static void
173 dump_data_references (FILE *file, vec<data_reference_p> datarefs)
175 unsigned int i;
176 struct data_reference *dr;
178 FOR_EACH_VEC_ELT (datarefs, i, dr)
179 dump_data_reference (file, dr);
182 /* Unified dump into FILE all the data references from DATAREFS. */
184 DEBUG_FUNCTION void
185 debug (vec<data_reference_p> &ref)
187 dump_data_references (stderr, ref);
190 DEBUG_FUNCTION void
191 debug (vec<data_reference_p> *ptr)
193 if (ptr)
194 debug (*ptr);
195 else
196 fprintf (stderr, "<nil>\n");
200 /* Dump into STDERR all the data references from DATAREFS. */
202 DEBUG_FUNCTION void
203 debug_data_references (vec<data_reference_p> datarefs)
205 dump_data_references (stderr, datarefs);
208 /* Print to STDERR the data_reference DR. */
210 DEBUG_FUNCTION void
211 debug_data_reference (struct data_reference *dr)
213 dump_data_reference (stderr, dr);
216 /* Dump function for a DATA_REFERENCE structure. */
218 void
219 dump_data_reference (FILE *outf,
220 struct data_reference *dr)
222 unsigned int i;
224 fprintf (outf, "#(Data Ref: \n");
225 fprintf (outf, "# bb: %d \n", gimple_bb (DR_STMT (dr))->index);
226 fprintf (outf, "# stmt: ");
227 print_gimple_stmt (outf, DR_STMT (dr), 0, 0);
228 fprintf (outf, "# ref: ");
229 print_generic_stmt (outf, DR_REF (dr), 0);
230 fprintf (outf, "# base_object: ");
231 print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
233 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
235 fprintf (outf, "# Access function %d: ", i);
236 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
238 fprintf (outf, "#)\n");
241 /* Unified dump function for a DATA_REFERENCE structure. */
243 DEBUG_FUNCTION void
244 debug (data_reference &ref)
246 dump_data_reference (stderr, &ref);
249 DEBUG_FUNCTION void
250 debug (data_reference *ptr)
252 if (ptr)
253 debug (*ptr);
254 else
255 fprintf (stderr, "<nil>\n");
259 /* Dumps the affine function described by FN to the file OUTF. */
261 static void
262 dump_affine_function (FILE *outf, affine_fn fn)
264 unsigned i;
265 tree coef;
267 print_generic_expr (outf, fn[0], TDF_SLIM);
268 for (i = 1; fn.iterate (i, &coef); i++)
270 fprintf (outf, " + ");
271 print_generic_expr (outf, coef, TDF_SLIM);
272 fprintf (outf, " * x_%u", i);
276 /* Dumps the conflict function CF to the file OUTF. */
278 static void
279 dump_conflict_function (FILE *outf, conflict_function *cf)
281 unsigned i;
283 if (cf->n == NO_DEPENDENCE)
284 fprintf (outf, "no dependence");
285 else if (cf->n == NOT_KNOWN)
286 fprintf (outf, "not known");
287 else
289 for (i = 0; i < cf->n; i++)
291 if (i != 0)
292 fprintf (outf, " ");
293 fprintf (outf, "[");
294 dump_affine_function (outf, cf->fns[i]);
295 fprintf (outf, "]");
300 /* Dump function for a SUBSCRIPT structure. */
302 static void
303 dump_subscript (FILE *outf, struct subscript *subscript)
305 conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
307 fprintf (outf, "\n (subscript \n");
308 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
309 dump_conflict_function (outf, cf);
310 if (CF_NONTRIVIAL_P (cf))
312 tree last_iteration = SUB_LAST_CONFLICT (subscript);
313 fprintf (outf, "\n last_conflict: ");
314 print_generic_expr (outf, last_iteration, 0);
317 cf = SUB_CONFLICTS_IN_B (subscript);
318 fprintf (outf, "\n iterations_that_access_an_element_twice_in_B: ");
319 dump_conflict_function (outf, cf);
320 if (CF_NONTRIVIAL_P (cf))
322 tree last_iteration = SUB_LAST_CONFLICT (subscript);
323 fprintf (outf, "\n last_conflict: ");
324 print_generic_expr (outf, last_iteration, 0);
327 fprintf (outf, "\n (Subscript distance: ");
328 print_generic_expr (outf, SUB_DISTANCE (subscript), 0);
329 fprintf (outf, " ))\n");
332 /* Print the classic direction vector DIRV to OUTF. */
334 static void
335 print_direction_vector (FILE *outf,
336 lambda_vector dirv,
337 int length)
339 int eq;
341 for (eq = 0; eq < length; eq++)
343 enum data_dependence_direction dir = ((enum data_dependence_direction)
344 dirv[eq]);
346 switch (dir)
348 case dir_positive:
349 fprintf (outf, " +");
350 break;
351 case dir_negative:
352 fprintf (outf, " -");
353 break;
354 case dir_equal:
355 fprintf (outf, " =");
356 break;
357 case dir_positive_or_equal:
358 fprintf (outf, " +=");
359 break;
360 case dir_positive_or_negative:
361 fprintf (outf, " +-");
362 break;
363 case dir_negative_or_equal:
364 fprintf (outf, " -=");
365 break;
366 case dir_star:
367 fprintf (outf, " *");
368 break;
369 default:
370 fprintf (outf, "indep");
371 break;
374 fprintf (outf, "\n");
377 /* Print a vector of direction vectors. */
379 static void
380 print_dir_vectors (FILE *outf, vec<lambda_vector> dir_vects,
381 int length)
383 unsigned j;
384 lambda_vector v;
386 FOR_EACH_VEC_ELT (dir_vects, j, v)
387 print_direction_vector (outf, v, length);
390 /* Print out a vector VEC of length N to OUTFILE. */
392 static inline void
393 print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
395 int i;
397 for (i = 0; i < n; i++)
398 fprintf (outfile, "%3d ", vector[i]);
399 fprintf (outfile, "\n");
402 /* Print a vector of distance vectors. */
404 static void
405 print_dist_vectors (FILE *outf, vec<lambda_vector> dist_vects,
406 int length)
408 unsigned j;
409 lambda_vector v;
411 FOR_EACH_VEC_ELT (dist_vects, j, v)
412 print_lambda_vector (outf, v, length);
415 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
417 static void
418 dump_data_dependence_relation (FILE *outf,
419 struct data_dependence_relation *ddr)
421 struct data_reference *dra, *drb;
423 fprintf (outf, "(Data Dep: \n");
425 if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
427 if (ddr)
429 dra = DDR_A (ddr);
430 drb = DDR_B (ddr);
431 if (dra)
432 dump_data_reference (outf, dra);
433 else
434 fprintf (outf, " (nil)\n");
435 if (drb)
436 dump_data_reference (outf, drb);
437 else
438 fprintf (outf, " (nil)\n");
440 fprintf (outf, " (don't know)\n)\n");
441 return;
444 dra = DDR_A (ddr);
445 drb = DDR_B (ddr);
446 dump_data_reference (outf, dra);
447 dump_data_reference (outf, drb);
449 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
450 fprintf (outf, " (no dependence)\n");
452 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
454 unsigned int i;
455 struct loop *loopi;
457 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
459 fprintf (outf, " access_fn_A: ");
460 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
461 fprintf (outf, " access_fn_B: ");
462 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
463 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
466 fprintf (outf, " inner loop index: %d\n", DDR_INNER_LOOP (ddr));
467 fprintf (outf, " loop nest: (");
468 FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
469 fprintf (outf, "%d ", loopi->num);
470 fprintf (outf, ")\n");
472 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
474 fprintf (outf, " distance_vector: ");
475 print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
476 DDR_NB_LOOPS (ddr));
479 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
481 fprintf (outf, " direction_vector: ");
482 print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
483 DDR_NB_LOOPS (ddr));
487 fprintf (outf, ")\n");
490 /* Debug version. */
492 DEBUG_FUNCTION void
493 debug_data_dependence_relation (struct data_dependence_relation *ddr)
495 dump_data_dependence_relation (stderr, ddr);
498 /* Dump into FILE all the dependence relations from DDRS. */
500 void
501 dump_data_dependence_relations (FILE *file,
502 vec<ddr_p> ddrs)
504 unsigned int i;
505 struct data_dependence_relation *ddr;
507 FOR_EACH_VEC_ELT (ddrs, i, ddr)
508 dump_data_dependence_relation (file, ddr);
511 DEBUG_FUNCTION void
512 debug (vec<ddr_p> &ref)
514 dump_data_dependence_relations (stderr, ref);
517 DEBUG_FUNCTION void
518 debug (vec<ddr_p> *ptr)
520 if (ptr)
521 debug (*ptr);
522 else
523 fprintf (stderr, "<nil>\n");
527 /* Dump to STDERR all the dependence relations from DDRS. */
529 DEBUG_FUNCTION void
530 debug_data_dependence_relations (vec<ddr_p> ddrs)
532 dump_data_dependence_relations (stderr, ddrs);
535 /* Dumps the distance and direction vectors in FILE. DDRS contains
536 the dependence relations, and VECT_SIZE is the size of the
537 dependence vectors, or in other words the number of loops in the
538 considered nest. */
540 static void
541 dump_dist_dir_vectors (FILE *file, vec<ddr_p> ddrs)
543 unsigned int i, j;
544 struct data_dependence_relation *ddr;
545 lambda_vector v;
547 FOR_EACH_VEC_ELT (ddrs, i, ddr)
548 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
550 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), j, v)
552 fprintf (file, "DISTANCE_V (");
553 print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
554 fprintf (file, ")\n");
557 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), j, v)
559 fprintf (file, "DIRECTION_V (");
560 print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
561 fprintf (file, ")\n");
565 fprintf (file, "\n\n");
568 /* Dumps the data dependence relations DDRS in FILE. */
570 static void
571 dump_ddrs (FILE *file, vec<ddr_p> ddrs)
573 unsigned int i;
574 struct data_dependence_relation *ddr;
576 FOR_EACH_VEC_ELT (ddrs, i, ddr)
577 dump_data_dependence_relation (file, ddr);
579 fprintf (file, "\n\n");
582 DEBUG_FUNCTION void
583 debug_ddrs (vec<ddr_p> ddrs)
585 dump_ddrs (stderr, ddrs);
588 /* Helper function for split_constant_offset. Expresses OP0 CODE OP1
589 (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
590 constant of type ssizetype, and returns true. If we cannot do this
591 with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
592 is returned. */
594 static bool
595 split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
596 tree *var, tree *off)
598 tree var0, var1;
599 tree off0, off1;
600 enum tree_code ocode = code;
602 *var = NULL_TREE;
603 *off = NULL_TREE;
605 switch (code)
607 case INTEGER_CST:
608 *var = build_int_cst (type, 0);
609 *off = fold_convert (ssizetype, op0);
610 return true;
612 case POINTER_PLUS_EXPR:
613 ocode = PLUS_EXPR;
614 /* FALLTHROUGH */
615 case PLUS_EXPR:
616 case MINUS_EXPR:
617 split_constant_offset (op0, &var0, &off0);
618 split_constant_offset (op1, &var1, &off1);
619 *var = fold_build2 (code, type, var0, var1);
620 *off = size_binop (ocode, off0, off1);
621 return true;
623 case MULT_EXPR:
624 if (TREE_CODE (op1) != INTEGER_CST)
625 return false;
627 split_constant_offset (op0, &var0, &off0);
628 *var = fold_build2 (MULT_EXPR, type, var0, op1);
629 *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
630 return true;
632 case ADDR_EXPR:
634 tree base, poffset;
635 HOST_WIDE_INT pbitsize, pbitpos;
636 machine_mode pmode;
637 int punsignedp, pvolatilep;
639 op0 = TREE_OPERAND (op0, 0);
640 base = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset,
641 &pmode, &punsignedp, &pvolatilep, false);
643 if (pbitpos % BITS_PER_UNIT != 0)
644 return false;
645 base = build_fold_addr_expr (base);
646 off0 = ssize_int (pbitpos / BITS_PER_UNIT);
648 if (poffset)
650 split_constant_offset (poffset, &poffset, &off1);
651 off0 = size_binop (PLUS_EXPR, off0, off1);
652 if (POINTER_TYPE_P (TREE_TYPE (base)))
653 base = fold_build_pointer_plus (base, poffset);
654 else
655 base = fold_build2 (PLUS_EXPR, TREE_TYPE (base), base,
656 fold_convert (TREE_TYPE (base), poffset));
659 var0 = fold_convert (type, base);
661 /* If variable length types are involved, punt, otherwise casts
662 might be converted into ARRAY_REFs in gimplify_conversion.
663 To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
664 possibly no longer appears in current GIMPLE, might resurface.
665 This perhaps could run
666 if (CONVERT_EXPR_P (var0))
668 gimplify_conversion (&var0);
669 // Attempt to fill in any within var0 found ARRAY_REF's
670 // element size from corresponding op embedded ARRAY_REF,
671 // if unsuccessful, just punt.
672 } */
673 while (POINTER_TYPE_P (type))
674 type = TREE_TYPE (type);
675 if (int_size_in_bytes (type) < 0)
676 return false;
678 *var = var0;
679 *off = off0;
680 return true;
683 case SSA_NAME:
685 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
686 return false;
688 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
689 enum tree_code subcode;
691 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
692 return false;
694 var0 = gimple_assign_rhs1 (def_stmt);
695 subcode = gimple_assign_rhs_code (def_stmt);
696 var1 = gimple_assign_rhs2 (def_stmt);
698 return split_constant_offset_1 (type, var0, subcode, var1, var, off);
700 CASE_CONVERT:
702 /* We must not introduce undefined overflow, and we must not change the value.
703 Hence we're okay if the inner type doesn't overflow to start with
704 (pointer or signed), the outer type also is an integer or pointer
705 and the outer precision is at least as large as the inner. */
706 tree itype = TREE_TYPE (op0);
707 if ((POINTER_TYPE_P (itype)
708 || (INTEGRAL_TYPE_P (itype) && TYPE_OVERFLOW_UNDEFINED (itype)))
709 && TYPE_PRECISION (type) >= TYPE_PRECISION (itype)
710 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)))
712 split_constant_offset (op0, &var0, off);
713 *var = fold_convert (type, var0);
714 return true;
716 return false;
719 default:
720 return false;
724 /* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
725 will be ssizetype. */
727 void
728 split_constant_offset (tree exp, tree *var, tree *off)
730 tree type = TREE_TYPE (exp), otype, op0, op1, e, o;
731 enum tree_code code;
733 *var = exp;
734 *off = ssize_int (0);
735 STRIP_NOPS (exp);
737 if (tree_is_chrec (exp)
738 || get_gimple_rhs_class (TREE_CODE (exp)) == GIMPLE_TERNARY_RHS)
739 return;
741 otype = TREE_TYPE (exp);
742 code = TREE_CODE (exp);
743 extract_ops_from_tree (exp, &code, &op0, &op1);
744 if (split_constant_offset_1 (otype, op0, code, op1, &e, &o))
746 *var = fold_convert (type, e);
747 *off = o;
751 /* Returns the address ADDR of an object in a canonical shape (without nop
752 casts, and with type of pointer to the object). */
754 static tree
755 canonicalize_base_object_address (tree addr)
757 tree orig = addr;
759 STRIP_NOPS (addr);
761 /* The base address may be obtained by casting from integer, in that case
762 keep the cast. */
763 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
764 return orig;
766 if (TREE_CODE (addr) != ADDR_EXPR)
767 return addr;
769 return build_fold_addr_expr (TREE_OPERAND (addr, 0));
772 /* Analyzes the behavior of the memory reference DR in the innermost loop or
773 basic block that contains it. Returns true if analysis succeed or false
774 otherwise. */
776 bool
777 dr_analyze_innermost (struct data_reference *dr, struct loop *nest)
779 gimple stmt = DR_STMT (dr);
780 struct loop *loop = loop_containing_stmt (stmt);
781 tree ref = DR_REF (dr);
782 HOST_WIDE_INT pbitsize, pbitpos;
783 tree base, poffset;
784 machine_mode pmode;
785 int punsignedp, pvolatilep;
786 affine_iv base_iv, offset_iv;
787 tree init, dinit, step;
788 bool in_loop = (loop && loop->num);
790 if (dump_file && (dump_flags & TDF_DETAILS))
791 fprintf (dump_file, "analyze_innermost: ");
793 base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset,
794 &pmode, &punsignedp, &pvolatilep, false);
795 gcc_assert (base != NULL_TREE);
797 if (pbitpos % BITS_PER_UNIT != 0)
799 if (dump_file && (dump_flags & TDF_DETAILS))
800 fprintf (dump_file, "failed: bit offset alignment.\n");
801 return false;
804 if (TREE_CODE (base) == MEM_REF)
806 if (!integer_zerop (TREE_OPERAND (base, 1)))
808 offset_int moff = mem_ref_offset (base);
809 tree mofft = wide_int_to_tree (sizetype, moff);
810 if (!poffset)
811 poffset = mofft;
812 else
813 poffset = size_binop (PLUS_EXPR, poffset, mofft);
815 base = TREE_OPERAND (base, 0);
817 else
818 base = build_fold_addr_expr (base);
820 if (in_loop)
822 if (!simple_iv (loop, loop_containing_stmt (stmt), base, &base_iv,
823 nest ? true : false))
825 if (nest)
827 if (dump_file && (dump_flags & TDF_DETAILS))
828 fprintf (dump_file, "failed: evolution of base is not"
829 " affine.\n");
830 return false;
832 else
834 base_iv.base = base;
835 base_iv.step = ssize_int (0);
836 base_iv.no_overflow = true;
840 else
842 base_iv.base = base;
843 base_iv.step = ssize_int (0);
844 base_iv.no_overflow = true;
847 if (!poffset)
849 offset_iv.base = ssize_int (0);
850 offset_iv.step = ssize_int (0);
852 else
854 if (!in_loop)
856 offset_iv.base = poffset;
857 offset_iv.step = ssize_int (0);
859 else if (!simple_iv (loop, loop_containing_stmt (stmt),
860 poffset, &offset_iv,
861 nest ? true : false))
863 if (nest)
865 if (dump_file && (dump_flags & TDF_DETAILS))
866 fprintf (dump_file, "failed: evolution of offset is not"
867 " affine.\n");
868 return false;
870 else
872 offset_iv.base = poffset;
873 offset_iv.step = ssize_int (0);
878 init = ssize_int (pbitpos / BITS_PER_UNIT);
879 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
880 init = size_binop (PLUS_EXPR, init, dinit);
881 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
882 init = size_binop (PLUS_EXPR, init, dinit);
884 step = size_binop (PLUS_EXPR,
885 fold_convert (ssizetype, base_iv.step),
886 fold_convert (ssizetype, offset_iv.step));
888 DR_BASE_ADDRESS (dr) = canonicalize_base_object_address (base_iv.base);
890 DR_OFFSET (dr) = fold_convert (ssizetype, offset_iv.base);
891 DR_INIT (dr) = init;
892 DR_STEP (dr) = step;
894 DR_ALIGNED_TO (dr) = size_int (highest_pow2_factor (offset_iv.base));
896 if (dump_file && (dump_flags & TDF_DETAILS))
897 fprintf (dump_file, "success.\n");
899 return true;
902 /* Determines the base object and the list of indices of memory reference
903 DR, analyzed in LOOP and instantiated in loop nest NEST. */
905 static void
906 dr_analyze_indices (struct data_reference *dr, loop_p nest, loop_p loop)
908 vec<tree> access_fns = vNULL;
909 tree ref, op;
910 tree base, off, access_fn;
911 basic_block before_loop;
913 /* If analyzing a basic-block there are no indices to analyze
914 and thus no access functions. */
915 if (!nest)
917 DR_BASE_OBJECT (dr) = DR_REF (dr);
918 DR_ACCESS_FNS (dr).create (0);
919 return;
922 ref = DR_REF (dr);
923 before_loop = block_before_loop (nest);
925 /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
926 into a two element array with a constant index. The base is
927 then just the immediate underlying object. */
928 if (TREE_CODE (ref) == REALPART_EXPR)
930 ref = TREE_OPERAND (ref, 0);
931 access_fns.safe_push (integer_zero_node);
933 else if (TREE_CODE (ref) == IMAGPART_EXPR)
935 ref = TREE_OPERAND (ref, 0);
936 access_fns.safe_push (integer_one_node);
939 /* Analyze access functions of dimensions we know to be independent. */
940 while (handled_component_p (ref))
942 if (TREE_CODE (ref) == ARRAY_REF)
944 op = TREE_OPERAND (ref, 1);
945 access_fn = analyze_scalar_evolution (loop, op);
946 access_fn = instantiate_scev (before_loop, loop, access_fn);
947 access_fns.safe_push (access_fn);
949 else if (TREE_CODE (ref) == COMPONENT_REF
950 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
952 /* For COMPONENT_REFs of records (but not unions!) use the
953 FIELD_DECL offset as constant access function so we can
954 disambiguate a[i].f1 and a[i].f2. */
955 tree off = component_ref_field_offset (ref);
956 off = size_binop (PLUS_EXPR,
957 size_binop (MULT_EXPR,
958 fold_convert (bitsizetype, off),
959 bitsize_int (BITS_PER_UNIT)),
960 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
961 access_fns.safe_push (off);
963 else
964 /* If we have an unhandled component we could not translate
965 to an access function stop analyzing. We have determined
966 our base object in this case. */
967 break;
969 ref = TREE_OPERAND (ref, 0);
972 /* If the address operand of a MEM_REF base has an evolution in the
973 analyzed nest, add it as an additional independent access-function. */
974 if (TREE_CODE (ref) == MEM_REF)
976 op = TREE_OPERAND (ref, 0);
977 access_fn = analyze_scalar_evolution (loop, op);
978 access_fn = instantiate_scev (before_loop, loop, access_fn);
979 if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
981 tree orig_type;
982 tree memoff = TREE_OPERAND (ref, 1);
983 base = initial_condition (access_fn);
984 orig_type = TREE_TYPE (base);
985 STRIP_USELESS_TYPE_CONVERSION (base);
986 split_constant_offset (base, &base, &off);
987 STRIP_USELESS_TYPE_CONVERSION (base);
988 /* Fold the MEM_REF offset into the evolutions initial
989 value to make more bases comparable. */
990 if (!integer_zerop (memoff))
992 off = size_binop (PLUS_EXPR, off,
993 fold_convert (ssizetype, memoff));
994 memoff = build_int_cst (TREE_TYPE (memoff), 0);
996 /* Adjust the offset so it is a multiple of the access type
997 size and thus we separate bases that can possibly be used
998 to produce partial overlaps (which the access_fn machinery
999 cannot handle). */
1000 wide_int rem;
1001 if (TYPE_SIZE_UNIT (TREE_TYPE (ref))
1002 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref))) == INTEGER_CST
1003 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (ref))))
1004 rem = wi::mod_trunc (off, TYPE_SIZE_UNIT (TREE_TYPE (ref)), SIGNED);
1005 else
1006 /* If we can't compute the remainder simply force the initial
1007 condition to zero. */
1008 rem = off;
1009 off = wide_int_to_tree (ssizetype, wi::sub (off, rem));
1010 memoff = wide_int_to_tree (TREE_TYPE (memoff), rem);
1011 /* And finally replace the initial condition. */
1012 access_fn = chrec_replace_initial_condition
1013 (access_fn, fold_convert (orig_type, off));
1014 /* ??? This is still not a suitable base object for
1015 dr_may_alias_p - the base object needs to be an
1016 access that covers the object as whole. With
1017 an evolution in the pointer this cannot be
1018 guaranteed.
1019 As a band-aid, mark the access so we can special-case
1020 it in dr_may_alias_p. */
1021 tree old = ref;
1022 ref = fold_build2_loc (EXPR_LOCATION (ref),
1023 MEM_REF, TREE_TYPE (ref),
1024 base, memoff);
1025 MR_DEPENDENCE_CLIQUE (ref) = MR_DEPENDENCE_CLIQUE (old);
1026 MR_DEPENDENCE_BASE (ref) = MR_DEPENDENCE_BASE (old);
1027 DR_UNCONSTRAINED_BASE (dr) = true;
1028 access_fns.safe_push (access_fn);
1031 else if (DECL_P (ref))
1033 /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
1034 ref = build2 (MEM_REF, TREE_TYPE (ref),
1035 build_fold_addr_expr (ref),
1036 build_int_cst (reference_alias_ptr_type (ref), 0));
1039 DR_BASE_OBJECT (dr) = ref;
1040 DR_ACCESS_FNS (dr) = access_fns;
1043 /* Extracts the alias analysis information from the memory reference DR. */
1045 static void
1046 dr_analyze_alias (struct data_reference *dr)
1048 tree ref = DR_REF (dr);
1049 tree base = get_base_address (ref), addr;
1051 if (INDIRECT_REF_P (base)
1052 || TREE_CODE (base) == MEM_REF)
1054 addr = TREE_OPERAND (base, 0);
1055 if (TREE_CODE (addr) == SSA_NAME)
1056 DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
1060 /* Frees data reference DR. */
1062 void
1063 free_data_ref (data_reference_p dr)
1065 DR_ACCESS_FNS (dr).release ();
1066 free (dr);
1069 /* Analyzes memory reference MEMREF accessed in STMT. The reference
1070 is read if IS_READ is true, write otherwise. Returns the
1071 data_reference description of MEMREF. NEST is the outermost loop
1072 in which the reference should be instantiated, LOOP is the loop in
1073 which the data reference should be analyzed. */
1075 struct data_reference *
1076 create_data_ref (loop_p nest, loop_p loop, tree memref, gimple stmt,
1077 bool is_read)
1079 struct data_reference *dr;
1081 if (dump_file && (dump_flags & TDF_DETAILS))
1083 fprintf (dump_file, "Creating dr for ");
1084 print_generic_expr (dump_file, memref, TDF_SLIM);
1085 fprintf (dump_file, "\n");
1088 dr = XCNEW (struct data_reference);
1089 DR_STMT (dr) = stmt;
1090 DR_REF (dr) = memref;
1091 DR_IS_READ (dr) = is_read;
1093 dr_analyze_innermost (dr, nest);
1094 dr_analyze_indices (dr, nest, loop);
1095 dr_analyze_alias (dr);
1097 if (dump_file && (dump_flags & TDF_DETAILS))
1099 unsigned i;
1100 fprintf (dump_file, "\tbase_address: ");
1101 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1102 fprintf (dump_file, "\n\toffset from base address: ");
1103 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1104 fprintf (dump_file, "\n\tconstant offset from base address: ");
1105 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1106 fprintf (dump_file, "\n\tstep: ");
1107 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1108 fprintf (dump_file, "\n\taligned to: ");
1109 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
1110 fprintf (dump_file, "\n\tbase_object: ");
1111 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1112 fprintf (dump_file, "\n");
1113 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
1115 fprintf (dump_file, "\tAccess function %d: ", i);
1116 print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
1120 return dr;
1123 /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
1124 expressions. */
1125 static bool
1126 dr_equal_offsets_p1 (tree offset1, tree offset2)
1128 bool res;
1130 STRIP_NOPS (offset1);
1131 STRIP_NOPS (offset2);
1133 if (offset1 == offset2)
1134 return true;
1136 if (TREE_CODE (offset1) != TREE_CODE (offset2)
1137 || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
1138 return false;
1140 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
1141 TREE_OPERAND (offset2, 0));
1143 if (!res || !BINARY_CLASS_P (offset1))
1144 return res;
1146 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
1147 TREE_OPERAND (offset2, 1));
1149 return res;
1152 /* Check if DRA and DRB have equal offsets. */
1153 bool
1154 dr_equal_offsets_p (struct data_reference *dra,
1155 struct data_reference *drb)
1157 tree offset1, offset2;
1159 offset1 = DR_OFFSET (dra);
1160 offset2 = DR_OFFSET (drb);
1162 return dr_equal_offsets_p1 (offset1, offset2);
1165 /* Returns true if FNA == FNB. */
1167 static bool
1168 affine_function_equal_p (affine_fn fna, affine_fn fnb)
1170 unsigned i, n = fna.length ();
1172 if (n != fnb.length ())
1173 return false;
1175 for (i = 0; i < n; i++)
1176 if (!operand_equal_p (fna[i], fnb[i], 0))
1177 return false;
1179 return true;
1182 /* If all the functions in CF are the same, returns one of them,
1183 otherwise returns NULL. */
1185 static affine_fn
1186 common_affine_function (conflict_function *cf)
1188 unsigned i;
1189 affine_fn comm;
1191 if (!CF_NONTRIVIAL_P (cf))
1192 return affine_fn ();
1194 comm = cf->fns[0];
1196 for (i = 1; i < cf->n; i++)
1197 if (!affine_function_equal_p (comm, cf->fns[i]))
1198 return affine_fn ();
1200 return comm;
1203 /* Returns the base of the affine function FN. */
1205 static tree
1206 affine_function_base (affine_fn fn)
1208 return fn[0];
1211 /* Returns true if FN is a constant. */
1213 static bool
1214 affine_function_constant_p (affine_fn fn)
1216 unsigned i;
1217 tree coef;
1219 for (i = 1; fn.iterate (i, &coef); i++)
1220 if (!integer_zerop (coef))
1221 return false;
1223 return true;
1226 /* Returns true if FN is the zero constant function. */
1228 static bool
1229 affine_function_zero_p (affine_fn fn)
1231 return (integer_zerop (affine_function_base (fn))
1232 && affine_function_constant_p (fn));
1235 /* Returns a signed integer type with the largest precision from TA
1236 and TB. */
1238 static tree
1239 signed_type_for_types (tree ta, tree tb)
1241 if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
1242 return signed_type_for (ta);
1243 else
1244 return signed_type_for (tb);
1247 /* Applies operation OP on affine functions FNA and FNB, and returns the
1248 result. */
1250 static affine_fn
1251 affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
1253 unsigned i, n, m;
1254 affine_fn ret;
1255 tree coef;
1257 if (fnb.length () > fna.length ())
1259 n = fna.length ();
1260 m = fnb.length ();
1262 else
1264 n = fnb.length ();
1265 m = fna.length ();
1268 ret.create (m);
1269 for (i = 0; i < n; i++)
1271 tree type = signed_type_for_types (TREE_TYPE (fna[i]),
1272 TREE_TYPE (fnb[i]));
1273 ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
1276 for (; fna.iterate (i, &coef); i++)
1277 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1278 coef, integer_zero_node));
1279 for (; fnb.iterate (i, &coef); i++)
1280 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1281 integer_zero_node, coef));
1283 return ret;
1286 /* Returns the sum of affine functions FNA and FNB. */
1288 static affine_fn
1289 affine_fn_plus (affine_fn fna, affine_fn fnb)
1291 return affine_fn_op (PLUS_EXPR, fna, fnb);
1294 /* Returns the difference of affine functions FNA and FNB. */
1296 static affine_fn
1297 affine_fn_minus (affine_fn fna, affine_fn fnb)
1299 return affine_fn_op (MINUS_EXPR, fna, fnb);
1302 /* Frees affine function FN. */
1304 static void
1305 affine_fn_free (affine_fn fn)
1307 fn.release ();
1310 /* Determine for each subscript in the data dependence relation DDR
1311 the distance. */
1313 static void
1314 compute_subscript_distance (struct data_dependence_relation *ddr)
1316 conflict_function *cf_a, *cf_b;
1317 affine_fn fn_a, fn_b, diff;
1319 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1321 unsigned int i;
1323 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1325 struct subscript *subscript;
1327 subscript = DDR_SUBSCRIPT (ddr, i);
1328 cf_a = SUB_CONFLICTS_IN_A (subscript);
1329 cf_b = SUB_CONFLICTS_IN_B (subscript);
1331 fn_a = common_affine_function (cf_a);
1332 fn_b = common_affine_function (cf_b);
1333 if (!fn_a.exists () || !fn_b.exists ())
1335 SUB_DISTANCE (subscript) = chrec_dont_know;
1336 return;
1338 diff = affine_fn_minus (fn_a, fn_b);
1340 if (affine_function_constant_p (diff))
1341 SUB_DISTANCE (subscript) = affine_function_base (diff);
1342 else
1343 SUB_DISTANCE (subscript) = chrec_dont_know;
1345 affine_fn_free (diff);
1350 /* Returns the conflict function for "unknown". */
1352 static conflict_function *
1353 conflict_fn_not_known (void)
1355 conflict_function *fn = XCNEW (conflict_function);
1356 fn->n = NOT_KNOWN;
1358 return fn;
1361 /* Returns the conflict function for "independent". */
1363 static conflict_function *
1364 conflict_fn_no_dependence (void)
1366 conflict_function *fn = XCNEW (conflict_function);
1367 fn->n = NO_DEPENDENCE;
1369 return fn;
1372 /* Returns true if the address of OBJ is invariant in LOOP. */
1374 static bool
1375 object_address_invariant_in_loop_p (const struct loop *loop, const_tree obj)
1377 while (handled_component_p (obj))
1379 if (TREE_CODE (obj) == ARRAY_REF)
1381 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1382 need to check the stride and the lower bound of the reference. */
1383 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1384 loop->num)
1385 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 3),
1386 loop->num))
1387 return false;
1389 else if (TREE_CODE (obj) == COMPONENT_REF)
1391 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1392 loop->num))
1393 return false;
1395 obj = TREE_OPERAND (obj, 0);
1398 if (!INDIRECT_REF_P (obj)
1399 && TREE_CODE (obj) != MEM_REF)
1400 return true;
1402 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
1403 loop->num);
1406 /* Returns false if we can prove that data references A and B do not alias,
1407 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
1408 considered. */
1410 bool
1411 dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
1412 bool loop_nest)
1414 tree addr_a = DR_BASE_OBJECT (a);
1415 tree addr_b = DR_BASE_OBJECT (b);
1417 /* If we are not processing a loop nest but scalar code we
1418 do not need to care about possible cross-iteration dependences
1419 and thus can process the full original reference. Do so,
1420 similar to how loop invariant motion applies extra offset-based
1421 disambiguation. */
1422 if (!loop_nest)
1424 aff_tree off1, off2;
1425 widest_int size1, size2;
1426 get_inner_reference_aff (DR_REF (a), &off1, &size1);
1427 get_inner_reference_aff (DR_REF (b), &off2, &size2);
1428 aff_combination_scale (&off1, -1);
1429 aff_combination_add (&off2, &off1);
1430 if (aff_comb_cannot_overlap_p (&off2, size1, size2))
1431 return false;
1434 if ((TREE_CODE (addr_a) == MEM_REF || TREE_CODE (addr_a) == TARGET_MEM_REF)
1435 && (TREE_CODE (addr_b) == MEM_REF || TREE_CODE (addr_b) == TARGET_MEM_REF)
1436 && MR_DEPENDENCE_CLIQUE (addr_a) == MR_DEPENDENCE_CLIQUE (addr_b)
1437 && MR_DEPENDENCE_BASE (addr_a) != MR_DEPENDENCE_BASE (addr_b))
1438 return false;
1440 /* If we had an evolution in a pointer-based MEM_REF BASE_OBJECT we
1441 do not know the size of the base-object. So we cannot do any
1442 offset/overlap based analysis but have to rely on points-to
1443 information only. */
1444 if (TREE_CODE (addr_a) == MEM_REF
1445 && (DR_UNCONSTRAINED_BASE (a)
1446 || TREE_CODE (TREE_OPERAND (addr_a, 0)) == SSA_NAME))
1448 /* For true dependences we can apply TBAA. */
1449 if (flag_strict_aliasing
1450 && DR_IS_WRITE (a) && DR_IS_READ (b)
1451 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1452 get_alias_set (DR_REF (b))))
1453 return false;
1454 if (TREE_CODE (addr_b) == MEM_REF)
1455 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1456 TREE_OPERAND (addr_b, 0));
1457 else
1458 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1459 build_fold_addr_expr (addr_b));
1461 else if (TREE_CODE (addr_b) == MEM_REF
1462 && (DR_UNCONSTRAINED_BASE (b)
1463 || TREE_CODE (TREE_OPERAND (addr_b, 0)) == SSA_NAME))
1465 /* For true dependences we can apply TBAA. */
1466 if (flag_strict_aliasing
1467 && DR_IS_WRITE (a) && DR_IS_READ (b)
1468 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1469 get_alias_set (DR_REF (b))))
1470 return false;
1471 if (TREE_CODE (addr_a) == MEM_REF)
1472 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1473 TREE_OPERAND (addr_b, 0));
1474 else
1475 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
1476 TREE_OPERAND (addr_b, 0));
1479 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1480 that is being subsetted in the loop nest. */
1481 if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
1482 return refs_output_dependent_p (addr_a, addr_b);
1483 else if (DR_IS_READ (a) && DR_IS_WRITE (b))
1484 return refs_anti_dependent_p (addr_a, addr_b);
1485 return refs_may_alias_p (addr_a, addr_b);
1488 /* Initialize a data dependence relation between data accesses A and
1489 B. NB_LOOPS is the number of loops surrounding the references: the
1490 size of the classic distance/direction vectors. */
1492 struct data_dependence_relation *
1493 initialize_data_dependence_relation (struct data_reference *a,
1494 struct data_reference *b,
1495 vec<loop_p> loop_nest)
1497 struct data_dependence_relation *res;
1498 unsigned int i;
1500 res = XNEW (struct data_dependence_relation);
1501 DDR_A (res) = a;
1502 DDR_B (res) = b;
1503 DDR_LOOP_NEST (res).create (0);
1504 DDR_REVERSED_P (res) = false;
1505 DDR_SUBSCRIPTS (res).create (0);
1506 DDR_DIR_VECTS (res).create (0);
1507 DDR_DIST_VECTS (res).create (0);
1509 if (a == NULL || b == NULL)
1511 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1512 return res;
1515 /* If the data references do not alias, then they are independent. */
1516 if (!dr_may_alias_p (a, b, loop_nest.exists ()))
1518 DDR_ARE_DEPENDENT (res) = chrec_known;
1519 return res;
1522 /* The case where the references are exactly the same. */
1523 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
1525 if (loop_nest.exists ()
1526 && !object_address_invariant_in_loop_p (loop_nest[0],
1527 DR_BASE_OBJECT (a)))
1529 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1530 return res;
1532 DDR_AFFINE_P (res) = true;
1533 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1534 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1535 DDR_LOOP_NEST (res) = loop_nest;
1536 DDR_INNER_LOOP (res) = 0;
1537 DDR_SELF_REFERENCE (res) = true;
1538 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1540 struct subscript *subscript;
1542 subscript = XNEW (struct subscript);
1543 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1544 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1545 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1546 SUB_DISTANCE (subscript) = chrec_dont_know;
1547 DDR_SUBSCRIPTS (res).safe_push (subscript);
1549 return res;
1552 /* If the references do not access the same object, we do not know
1553 whether they alias or not. */
1554 if (!operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0))
1556 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1557 return res;
1560 /* If the base of the object is not invariant in the loop nest, we cannot
1561 analyze it. TODO -- in fact, it would suffice to record that there may
1562 be arbitrary dependences in the loops where the base object varies. */
1563 if (loop_nest.exists ()
1564 && !object_address_invariant_in_loop_p (loop_nest[0],
1565 DR_BASE_OBJECT (a)))
1567 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1568 return res;
1571 /* If the number of dimensions of the access to not agree we can have
1572 a pointer access to a component of the array element type and an
1573 array access while the base-objects are still the same. Punt. */
1574 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1576 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1577 return res;
1580 DDR_AFFINE_P (res) = true;
1581 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1582 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1583 DDR_LOOP_NEST (res) = loop_nest;
1584 DDR_INNER_LOOP (res) = 0;
1585 DDR_SELF_REFERENCE (res) = false;
1587 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1589 struct subscript *subscript;
1591 subscript = XNEW (struct subscript);
1592 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1593 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1594 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1595 SUB_DISTANCE (subscript) = chrec_dont_know;
1596 DDR_SUBSCRIPTS (res).safe_push (subscript);
1599 return res;
1602 /* Frees memory used by the conflict function F. */
1604 static void
1605 free_conflict_function (conflict_function *f)
1607 unsigned i;
1609 if (CF_NONTRIVIAL_P (f))
1611 for (i = 0; i < f->n; i++)
1612 affine_fn_free (f->fns[i]);
1614 free (f);
1617 /* Frees memory used by SUBSCRIPTS. */
1619 static void
1620 free_subscripts (vec<subscript_p> subscripts)
1622 unsigned i;
1623 subscript_p s;
1625 FOR_EACH_VEC_ELT (subscripts, i, s)
1627 free_conflict_function (s->conflicting_iterations_in_a);
1628 free_conflict_function (s->conflicting_iterations_in_b);
1629 free (s);
1631 subscripts.release ();
1634 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1635 description. */
1637 static inline void
1638 finalize_ddr_dependent (struct data_dependence_relation *ddr,
1639 tree chrec)
1641 DDR_ARE_DEPENDENT (ddr) = chrec;
1642 free_subscripts (DDR_SUBSCRIPTS (ddr));
1643 DDR_SUBSCRIPTS (ddr).create (0);
1646 /* The dependence relation DDR cannot be represented by a distance
1647 vector. */
1649 static inline void
1650 non_affine_dependence_relation (struct data_dependence_relation *ddr)
1652 if (dump_file && (dump_flags & TDF_DETAILS))
1653 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
1655 DDR_AFFINE_P (ddr) = false;
1660 /* This section contains the classic Banerjee tests. */
1662 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1663 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1665 static inline bool
1666 ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1668 return (evolution_function_is_constant_p (chrec_a)
1669 && evolution_function_is_constant_p (chrec_b));
1672 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
1673 variable, i.e., if the SIV (Single Index Variable) test is true. */
1675 static bool
1676 siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1678 if ((evolution_function_is_constant_p (chrec_a)
1679 && evolution_function_is_univariate_p (chrec_b))
1680 || (evolution_function_is_constant_p (chrec_b)
1681 && evolution_function_is_univariate_p (chrec_a)))
1682 return true;
1684 if (evolution_function_is_univariate_p (chrec_a)
1685 && evolution_function_is_univariate_p (chrec_b))
1687 switch (TREE_CODE (chrec_a))
1689 case POLYNOMIAL_CHREC:
1690 switch (TREE_CODE (chrec_b))
1692 case POLYNOMIAL_CHREC:
1693 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
1694 return false;
1696 default:
1697 return true;
1700 default:
1701 return true;
1705 return false;
1708 /* Creates a conflict function with N dimensions. The affine functions
1709 in each dimension follow. */
1711 static conflict_function *
1712 conflict_fn (unsigned n, ...)
1714 unsigned i;
1715 conflict_function *ret = XCNEW (conflict_function);
1716 va_list ap;
1718 gcc_assert (0 < n && n <= MAX_DIM);
1719 va_start (ap, n);
1721 ret->n = n;
1722 for (i = 0; i < n; i++)
1723 ret->fns[i] = va_arg (ap, affine_fn);
1724 va_end (ap);
1726 return ret;
1729 /* Returns constant affine function with value CST. */
1731 static affine_fn
1732 affine_fn_cst (tree cst)
1734 affine_fn fn;
1735 fn.create (1);
1736 fn.quick_push (cst);
1737 return fn;
1740 /* Returns affine function with single variable, CST + COEF * x_DIM. */
1742 static affine_fn
1743 affine_fn_univar (tree cst, unsigned dim, tree coef)
1745 affine_fn fn;
1746 fn.create (dim + 1);
1747 unsigned i;
1749 gcc_assert (dim > 0);
1750 fn.quick_push (cst);
1751 for (i = 1; i < dim; i++)
1752 fn.quick_push (integer_zero_node);
1753 fn.quick_push (coef);
1754 return fn;
1757 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1758 *OVERLAPS_B are initialized to the functions that describe the
1759 relation between the elements accessed twice by CHREC_A and
1760 CHREC_B. For k >= 0, the following property is verified:
1762 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1764 static void
1765 analyze_ziv_subscript (tree chrec_a,
1766 tree chrec_b,
1767 conflict_function **overlaps_a,
1768 conflict_function **overlaps_b,
1769 tree *last_conflicts)
1771 tree type, difference;
1772 dependence_stats.num_ziv++;
1774 if (dump_file && (dump_flags & TDF_DETAILS))
1775 fprintf (dump_file, "(analyze_ziv_subscript \n");
1777 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1778 chrec_a = chrec_convert (type, chrec_a, NULL);
1779 chrec_b = chrec_convert (type, chrec_b, NULL);
1780 difference = chrec_fold_minus (type, chrec_a, chrec_b);
1782 switch (TREE_CODE (difference))
1784 case INTEGER_CST:
1785 if (integer_zerop (difference))
1787 /* The difference is equal to zero: the accessed index
1788 overlaps for each iteration in the loop. */
1789 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1790 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1791 *last_conflicts = chrec_dont_know;
1792 dependence_stats.num_ziv_dependent++;
1794 else
1796 /* The accesses do not overlap. */
1797 *overlaps_a = conflict_fn_no_dependence ();
1798 *overlaps_b = conflict_fn_no_dependence ();
1799 *last_conflicts = integer_zero_node;
1800 dependence_stats.num_ziv_independent++;
1802 break;
1804 default:
1805 /* We're not sure whether the indexes overlap. For the moment,
1806 conservatively answer "don't know". */
1807 if (dump_file && (dump_flags & TDF_DETAILS))
1808 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
1810 *overlaps_a = conflict_fn_not_known ();
1811 *overlaps_b = conflict_fn_not_known ();
1812 *last_conflicts = chrec_dont_know;
1813 dependence_stats.num_ziv_unimplemented++;
1814 break;
1817 if (dump_file && (dump_flags & TDF_DETAILS))
1818 fprintf (dump_file, ")\n");
1821 /* Similar to max_stmt_executions_int, but returns the bound as a tree,
1822 and only if it fits to the int type. If this is not the case, or the
1823 bound on the number of iterations of LOOP could not be derived, returns
1824 chrec_dont_know. */
1826 static tree
1827 max_stmt_executions_tree (struct loop *loop)
1829 widest_int nit;
1831 if (!max_stmt_executions (loop, &nit))
1832 return chrec_dont_know;
1834 if (!wi::fits_to_tree_p (nit, unsigned_type_node))
1835 return chrec_dont_know;
1837 return wide_int_to_tree (unsigned_type_node, nit);
1840 /* Determine whether the CHREC is always positive/negative. If the expression
1841 cannot be statically analyzed, return false, otherwise set the answer into
1842 VALUE. */
1844 static bool
1845 chrec_is_positive (tree chrec, bool *value)
1847 bool value0, value1, value2;
1848 tree end_value, nb_iter;
1850 switch (TREE_CODE (chrec))
1852 case POLYNOMIAL_CHREC:
1853 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
1854 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
1855 return false;
1857 /* FIXME -- overflows. */
1858 if (value0 == value1)
1860 *value = value0;
1861 return true;
1864 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1865 and the proof consists in showing that the sign never
1866 changes during the execution of the loop, from 0 to
1867 loop->nb_iterations. */
1868 if (!evolution_function_is_affine_p (chrec))
1869 return false;
1871 nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
1872 if (chrec_contains_undetermined (nb_iter))
1873 return false;
1875 #if 0
1876 /* TODO -- If the test is after the exit, we may decrease the number of
1877 iterations by one. */
1878 if (after_exit)
1879 nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
1880 #endif
1882 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
1884 if (!chrec_is_positive (end_value, &value2))
1885 return false;
1887 *value = value0;
1888 return value0 == value1;
1890 case INTEGER_CST:
1891 switch (tree_int_cst_sgn (chrec))
1893 case -1:
1894 *value = false;
1895 break;
1896 case 1:
1897 *value = true;
1898 break;
1899 default:
1900 return false;
1902 return true;
1904 default:
1905 return false;
1910 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1911 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1912 *OVERLAPS_B are initialized to the functions that describe the
1913 relation between the elements accessed twice by CHREC_A and
1914 CHREC_B. For k >= 0, the following property is verified:
1916 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1918 static void
1919 analyze_siv_subscript_cst_affine (tree chrec_a,
1920 tree chrec_b,
1921 conflict_function **overlaps_a,
1922 conflict_function **overlaps_b,
1923 tree *last_conflicts)
1925 bool value0, value1, value2;
1926 tree type, difference, tmp;
1928 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1929 chrec_a = chrec_convert (type, chrec_a, NULL);
1930 chrec_b = chrec_convert (type, chrec_b, NULL);
1931 difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
1933 /* Special case overlap in the first iteration. */
1934 if (integer_zerop (difference))
1936 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1937 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1938 *last_conflicts = integer_one_node;
1939 return;
1942 if (!chrec_is_positive (initial_condition (difference), &value0))
1944 if (dump_file && (dump_flags & TDF_DETAILS))
1945 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
1947 dependence_stats.num_siv_unimplemented++;
1948 *overlaps_a = conflict_fn_not_known ();
1949 *overlaps_b = conflict_fn_not_known ();
1950 *last_conflicts = chrec_dont_know;
1951 return;
1953 else
1955 if (value0 == false)
1957 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
1959 if (dump_file && (dump_flags & TDF_DETAILS))
1960 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1962 *overlaps_a = conflict_fn_not_known ();
1963 *overlaps_b = conflict_fn_not_known ();
1964 *last_conflicts = chrec_dont_know;
1965 dependence_stats.num_siv_unimplemented++;
1966 return;
1968 else
1970 if (value1 == true)
1972 /* Example:
1973 chrec_a = 12
1974 chrec_b = {10, +, 1}
1977 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1979 HOST_WIDE_INT numiter;
1980 struct loop *loop = get_chrec_loop (chrec_b);
1982 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1983 tmp = fold_build2 (EXACT_DIV_EXPR, type,
1984 fold_build1 (ABS_EXPR, type, difference),
1985 CHREC_RIGHT (chrec_b));
1986 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1987 *last_conflicts = integer_one_node;
1990 /* Perform weak-zero siv test to see if overlap is
1991 outside the loop bounds. */
1992 numiter = max_stmt_executions_int (loop);
1994 if (numiter >= 0
1995 && compare_tree_int (tmp, numiter) > 0)
1997 free_conflict_function (*overlaps_a);
1998 free_conflict_function (*overlaps_b);
1999 *overlaps_a = conflict_fn_no_dependence ();
2000 *overlaps_b = conflict_fn_no_dependence ();
2001 *last_conflicts = integer_zero_node;
2002 dependence_stats.num_siv_independent++;
2003 return;
2005 dependence_stats.num_siv_dependent++;
2006 return;
2009 /* When the step does not divide the difference, there are
2010 no overlaps. */
2011 else
2013 *overlaps_a = conflict_fn_no_dependence ();
2014 *overlaps_b = conflict_fn_no_dependence ();
2015 *last_conflicts = integer_zero_node;
2016 dependence_stats.num_siv_independent++;
2017 return;
2021 else
2023 /* Example:
2024 chrec_a = 12
2025 chrec_b = {10, +, -1}
2027 In this case, chrec_a will not overlap with chrec_b. */
2028 *overlaps_a = conflict_fn_no_dependence ();
2029 *overlaps_b = conflict_fn_no_dependence ();
2030 *last_conflicts = integer_zero_node;
2031 dependence_stats.num_siv_independent++;
2032 return;
2036 else
2038 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
2040 if (dump_file && (dump_flags & TDF_DETAILS))
2041 fprintf (dump_file, "siv test failed: chrec not positive.\n");
2043 *overlaps_a = conflict_fn_not_known ();
2044 *overlaps_b = conflict_fn_not_known ();
2045 *last_conflicts = chrec_dont_know;
2046 dependence_stats.num_siv_unimplemented++;
2047 return;
2049 else
2051 if (value2 == false)
2053 /* Example:
2054 chrec_a = 3
2055 chrec_b = {10, +, -1}
2057 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2059 HOST_WIDE_INT numiter;
2060 struct loop *loop = get_chrec_loop (chrec_b);
2062 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2063 tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
2064 CHREC_RIGHT (chrec_b));
2065 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
2066 *last_conflicts = integer_one_node;
2068 /* Perform weak-zero siv test to see if overlap is
2069 outside the loop bounds. */
2070 numiter = max_stmt_executions_int (loop);
2072 if (numiter >= 0
2073 && compare_tree_int (tmp, numiter) > 0)
2075 free_conflict_function (*overlaps_a);
2076 free_conflict_function (*overlaps_b);
2077 *overlaps_a = conflict_fn_no_dependence ();
2078 *overlaps_b = conflict_fn_no_dependence ();
2079 *last_conflicts = integer_zero_node;
2080 dependence_stats.num_siv_independent++;
2081 return;
2083 dependence_stats.num_siv_dependent++;
2084 return;
2087 /* When the step does not divide the difference, there
2088 are no overlaps. */
2089 else
2091 *overlaps_a = conflict_fn_no_dependence ();
2092 *overlaps_b = conflict_fn_no_dependence ();
2093 *last_conflicts = integer_zero_node;
2094 dependence_stats.num_siv_independent++;
2095 return;
2098 else
2100 /* Example:
2101 chrec_a = 3
2102 chrec_b = {4, +, 1}
2104 In this case, chrec_a will not overlap with chrec_b. */
2105 *overlaps_a = conflict_fn_no_dependence ();
2106 *overlaps_b = conflict_fn_no_dependence ();
2107 *last_conflicts = integer_zero_node;
2108 dependence_stats.num_siv_independent++;
2109 return;
2116 /* Helper recursive function for initializing the matrix A. Returns
2117 the initial value of CHREC. */
2119 static tree
2120 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2122 gcc_assert (chrec);
2124 switch (TREE_CODE (chrec))
2126 case POLYNOMIAL_CHREC:
2127 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST);
2129 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2130 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2132 case PLUS_EXPR:
2133 case MULT_EXPR:
2134 case MINUS_EXPR:
2136 tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2137 tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
2139 return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
2142 CASE_CONVERT:
2144 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2145 return chrec_convert (chrec_type (chrec), op, NULL);
2148 case BIT_NOT_EXPR:
2150 /* Handle ~X as -1 - X. */
2151 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2152 return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
2153 build_int_cst (TREE_TYPE (chrec), -1), op);
2156 case INTEGER_CST:
2157 return chrec;
2159 default:
2160 gcc_unreachable ();
2161 return NULL_TREE;
2165 #define FLOOR_DIV(x,y) ((x) / (y))
2167 /* Solves the special case of the Diophantine equation:
2168 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2170 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2171 number of iterations that loops X and Y run. The overlaps will be
2172 constructed as evolutions in dimension DIM. */
2174 static void
2175 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2176 affine_fn *overlaps_a,
2177 affine_fn *overlaps_b,
2178 tree *last_conflicts, int dim)
2180 if (((step_a > 0 && step_b > 0)
2181 || (step_a < 0 && step_b < 0)))
2183 int step_overlaps_a, step_overlaps_b;
2184 int gcd_steps_a_b, last_conflict, tau2;
2186 gcd_steps_a_b = gcd (step_a, step_b);
2187 step_overlaps_a = step_b / gcd_steps_a_b;
2188 step_overlaps_b = step_a / gcd_steps_a_b;
2190 if (niter > 0)
2192 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2193 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2194 last_conflict = tau2;
2195 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2197 else
2198 *last_conflicts = chrec_dont_know;
2200 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
2201 build_int_cst (NULL_TREE,
2202 step_overlaps_a));
2203 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
2204 build_int_cst (NULL_TREE,
2205 step_overlaps_b));
2208 else
2210 *overlaps_a = affine_fn_cst (integer_zero_node);
2211 *overlaps_b = affine_fn_cst (integer_zero_node);
2212 *last_conflicts = integer_zero_node;
2216 /* Solves the special case of a Diophantine equation where CHREC_A is
2217 an affine bivariate function, and CHREC_B is an affine univariate
2218 function. For example,
2220 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2222 has the following overlapping functions:
2224 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2225 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2226 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2228 FORNOW: This is a specialized implementation for a case occurring in
2229 a common benchmark. Implement the general algorithm. */
2231 static void
2232 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2233 conflict_function **overlaps_a,
2234 conflict_function **overlaps_b,
2235 tree *last_conflicts)
2237 bool xz_p, yz_p, xyz_p;
2238 int step_x, step_y, step_z;
2239 HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
2240 affine_fn overlaps_a_xz, overlaps_b_xz;
2241 affine_fn overlaps_a_yz, overlaps_b_yz;
2242 affine_fn overlaps_a_xyz, overlaps_b_xyz;
2243 affine_fn ova1, ova2, ovb;
2244 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
2246 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2247 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2248 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2250 niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
2251 niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
2252 niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
2254 if (niter_x < 0 || niter_y < 0 || niter_z < 0)
2256 if (dump_file && (dump_flags & TDF_DETAILS))
2257 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
2259 *overlaps_a = conflict_fn_not_known ();
2260 *overlaps_b = conflict_fn_not_known ();
2261 *last_conflicts = chrec_dont_know;
2262 return;
2265 niter = MIN (niter_x, niter_z);
2266 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2267 &overlaps_a_xz,
2268 &overlaps_b_xz,
2269 &last_conflicts_xz, 1);
2270 niter = MIN (niter_y, niter_z);
2271 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2272 &overlaps_a_yz,
2273 &overlaps_b_yz,
2274 &last_conflicts_yz, 2);
2275 niter = MIN (niter_x, niter_z);
2276 niter = MIN (niter_y, niter);
2277 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2278 &overlaps_a_xyz,
2279 &overlaps_b_xyz,
2280 &last_conflicts_xyz, 3);
2282 xz_p = !integer_zerop (last_conflicts_xz);
2283 yz_p = !integer_zerop (last_conflicts_yz);
2284 xyz_p = !integer_zerop (last_conflicts_xyz);
2286 if (xz_p || yz_p || xyz_p)
2288 ova1 = affine_fn_cst (integer_zero_node);
2289 ova2 = affine_fn_cst (integer_zero_node);
2290 ovb = affine_fn_cst (integer_zero_node);
2291 if (xz_p)
2293 affine_fn t0 = ova1;
2294 affine_fn t2 = ovb;
2296 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
2297 ovb = affine_fn_plus (ovb, overlaps_b_xz);
2298 affine_fn_free (t0);
2299 affine_fn_free (t2);
2300 *last_conflicts = last_conflicts_xz;
2302 if (yz_p)
2304 affine_fn t0 = ova2;
2305 affine_fn t2 = ovb;
2307 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
2308 ovb = affine_fn_plus (ovb, overlaps_b_yz);
2309 affine_fn_free (t0);
2310 affine_fn_free (t2);
2311 *last_conflicts = last_conflicts_yz;
2313 if (xyz_p)
2315 affine_fn t0 = ova1;
2316 affine_fn t2 = ova2;
2317 affine_fn t4 = ovb;
2319 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
2320 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
2321 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
2322 affine_fn_free (t0);
2323 affine_fn_free (t2);
2324 affine_fn_free (t4);
2325 *last_conflicts = last_conflicts_xyz;
2327 *overlaps_a = conflict_fn (2, ova1, ova2);
2328 *overlaps_b = conflict_fn (1, ovb);
2330 else
2332 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2333 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2334 *last_conflicts = integer_zero_node;
2337 affine_fn_free (overlaps_a_xz);
2338 affine_fn_free (overlaps_b_xz);
2339 affine_fn_free (overlaps_a_yz);
2340 affine_fn_free (overlaps_b_yz);
2341 affine_fn_free (overlaps_a_xyz);
2342 affine_fn_free (overlaps_b_xyz);
2345 /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2347 static void
2348 lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
2349 int size)
2351 memcpy (vec2, vec1, size * sizeof (*vec1));
2354 /* Copy the elements of M x N matrix MAT1 to MAT2. */
2356 static void
2357 lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
2358 int m, int n)
2360 int i;
2362 for (i = 0; i < m; i++)
2363 lambda_vector_copy (mat1[i], mat2[i], n);
2366 /* Store the N x N identity matrix in MAT. */
2368 static void
2369 lambda_matrix_id (lambda_matrix mat, int size)
2371 int i, j;
2373 for (i = 0; i < size; i++)
2374 for (j = 0; j < size; j++)
2375 mat[i][j] = (i == j) ? 1 : 0;
2378 /* Return the first nonzero element of vector VEC1 between START and N.
2379 We must have START <= N. Returns N if VEC1 is the zero vector. */
2381 static int
2382 lambda_vector_first_nz (lambda_vector vec1, int n, int start)
2384 int j = start;
2385 while (j < n && vec1[j] == 0)
2386 j++;
2387 return j;
2390 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2391 R2 = R2 + CONST1 * R1. */
2393 static void
2394 lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2, int const1)
2396 int i;
2398 if (const1 == 0)
2399 return;
2401 for (i = 0; i < n; i++)
2402 mat[r2][i] += const1 * mat[r1][i];
2405 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
2406 and store the result in VEC2. */
2408 static void
2409 lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
2410 int size, int const1)
2412 int i;
2414 if (const1 == 0)
2415 lambda_vector_clear (vec2, size);
2416 else
2417 for (i = 0; i < size; i++)
2418 vec2[i] = const1 * vec1[i];
2421 /* Negate vector VEC1 with length SIZE and store it in VEC2. */
2423 static void
2424 lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
2425 int size)
2427 lambda_vector_mult_const (vec1, vec2, size, -1);
2430 /* Negate row R1 of matrix MAT which has N columns. */
2432 static void
2433 lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
2435 lambda_vector_negate (mat[r1], mat[r1], n);
2438 /* Return true if two vectors are equal. */
2440 static bool
2441 lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
2443 int i;
2444 for (i = 0; i < size; i++)
2445 if (vec1[i] != vec2[i])
2446 return false;
2447 return true;
2450 /* Given an M x N integer matrix A, this function determines an M x
2451 M unimodular matrix U, and an M x N echelon matrix S such that
2452 "U.A = S". This decomposition is also known as "right Hermite".
2454 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2455 Restructuring Compilers" Utpal Banerjee. */
2457 static void
2458 lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
2459 lambda_matrix S, lambda_matrix U)
2461 int i, j, i0 = 0;
2463 lambda_matrix_copy (A, S, m, n);
2464 lambda_matrix_id (U, m);
2466 for (j = 0; j < n; j++)
2468 if (lambda_vector_first_nz (S[j], m, i0) < m)
2470 ++i0;
2471 for (i = m - 1; i >= i0; i--)
2473 while (S[i][j] != 0)
2475 int sigma, factor, a, b;
2477 a = S[i-1][j];
2478 b = S[i][j];
2479 sigma = (a * b < 0) ? -1: 1;
2480 a = abs (a);
2481 b = abs (b);
2482 factor = sigma * (a / b);
2484 lambda_matrix_row_add (S, n, i, i-1, -factor);
2485 std::swap (S[i], S[i-1]);
2487 lambda_matrix_row_add (U, m, i, i-1, -factor);
2488 std::swap (U[i], U[i-1]);
2495 /* Determines the overlapping elements due to accesses CHREC_A and
2496 CHREC_B, that are affine functions. This function cannot handle
2497 symbolic evolution functions, ie. when initial conditions are
2498 parameters, because it uses lambda matrices of integers. */
2500 static void
2501 analyze_subscript_affine_affine (tree chrec_a,
2502 tree chrec_b,
2503 conflict_function **overlaps_a,
2504 conflict_function **overlaps_b,
2505 tree *last_conflicts)
2507 unsigned nb_vars_a, nb_vars_b, dim;
2508 HOST_WIDE_INT init_a, init_b, gamma, gcd_alpha_beta;
2509 lambda_matrix A, U, S;
2510 struct obstack scratch_obstack;
2512 if (eq_evolutions_p (chrec_a, chrec_b))
2514 /* The accessed index overlaps for each iteration in the
2515 loop. */
2516 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2517 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2518 *last_conflicts = chrec_dont_know;
2519 return;
2521 if (dump_file && (dump_flags & TDF_DETAILS))
2522 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2524 /* For determining the initial intersection, we have to solve a
2525 Diophantine equation. This is the most time consuming part.
2527 For answering to the question: "Is there a dependence?" we have
2528 to prove that there exists a solution to the Diophantine
2529 equation, and that the solution is in the iteration domain,
2530 i.e. the solution is positive or zero, and that the solution
2531 happens before the upper bound loop.nb_iterations. Otherwise
2532 there is no dependence. This function outputs a description of
2533 the iterations that hold the intersections. */
2535 nb_vars_a = nb_vars_in_chrec (chrec_a);
2536 nb_vars_b = nb_vars_in_chrec (chrec_b);
2538 gcc_obstack_init (&scratch_obstack);
2540 dim = nb_vars_a + nb_vars_b;
2541 U = lambda_matrix_new (dim, dim, &scratch_obstack);
2542 A = lambda_matrix_new (dim, 1, &scratch_obstack);
2543 S = lambda_matrix_new (dim, 1, &scratch_obstack);
2545 init_a = int_cst_value (initialize_matrix_A (A, chrec_a, 0, 1));
2546 init_b = int_cst_value (initialize_matrix_A (A, chrec_b, nb_vars_a, -1));
2547 gamma = init_b - init_a;
2549 /* Don't do all the hard work of solving the Diophantine equation
2550 when we already know the solution: for example,
2551 | {3, +, 1}_1
2552 | {3, +, 4}_2
2553 | gamma = 3 - 3 = 0.
2554 Then the first overlap occurs during the first iterations:
2555 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2557 if (gamma == 0)
2559 if (nb_vars_a == 1 && nb_vars_b == 1)
2561 HOST_WIDE_INT step_a, step_b;
2562 HOST_WIDE_INT niter, niter_a, niter_b;
2563 affine_fn ova, ovb;
2565 niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
2566 niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
2567 niter = MIN (niter_a, niter_b);
2568 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2569 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2571 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2572 &ova, &ovb,
2573 last_conflicts, 1);
2574 *overlaps_a = conflict_fn (1, ova);
2575 *overlaps_b = conflict_fn (1, ovb);
2578 else if (nb_vars_a == 2 && nb_vars_b == 1)
2579 compute_overlap_steps_for_affine_1_2
2580 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2582 else if (nb_vars_a == 1 && nb_vars_b == 2)
2583 compute_overlap_steps_for_affine_1_2
2584 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2586 else
2588 if (dump_file && (dump_flags & TDF_DETAILS))
2589 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
2590 *overlaps_a = conflict_fn_not_known ();
2591 *overlaps_b = conflict_fn_not_known ();
2592 *last_conflicts = chrec_dont_know;
2594 goto end_analyze_subs_aa;
2597 /* U.A = S */
2598 lambda_matrix_right_hermite (A, dim, 1, S, U);
2600 if (S[0][0] < 0)
2602 S[0][0] *= -1;
2603 lambda_matrix_row_negate (U, dim, 0);
2605 gcd_alpha_beta = S[0][0];
2607 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2608 but that is a quite strange case. Instead of ICEing, answer
2609 don't know. */
2610 if (gcd_alpha_beta == 0)
2612 *overlaps_a = conflict_fn_not_known ();
2613 *overlaps_b = conflict_fn_not_known ();
2614 *last_conflicts = chrec_dont_know;
2615 goto end_analyze_subs_aa;
2618 /* The classic "gcd-test". */
2619 if (!int_divides_p (gcd_alpha_beta, gamma))
2621 /* The "gcd-test" has determined that there is no integer
2622 solution, i.e. there is no dependence. */
2623 *overlaps_a = conflict_fn_no_dependence ();
2624 *overlaps_b = conflict_fn_no_dependence ();
2625 *last_conflicts = integer_zero_node;
2628 /* Both access functions are univariate. This includes SIV and MIV cases. */
2629 else if (nb_vars_a == 1 && nb_vars_b == 1)
2631 /* Both functions should have the same evolution sign. */
2632 if (((A[0][0] > 0 && -A[1][0] > 0)
2633 || (A[0][0] < 0 && -A[1][0] < 0)))
2635 /* The solutions are given by:
2637 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2638 | [u21 u22] [y0]
2640 For a given integer t. Using the following variables,
2642 | i0 = u11 * gamma / gcd_alpha_beta
2643 | j0 = u12 * gamma / gcd_alpha_beta
2644 | i1 = u21
2645 | j1 = u22
2647 the solutions are:
2649 | x0 = i0 + i1 * t,
2650 | y0 = j0 + j1 * t. */
2651 HOST_WIDE_INT i0, j0, i1, j1;
2653 i0 = U[0][0] * gamma / gcd_alpha_beta;
2654 j0 = U[0][1] * gamma / gcd_alpha_beta;
2655 i1 = U[1][0];
2656 j1 = U[1][1];
2658 if ((i1 == 0 && i0 < 0)
2659 || (j1 == 0 && j0 < 0))
2661 /* There is no solution.
2662 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2663 falls in here, but for the moment we don't look at the
2664 upper bound of the iteration domain. */
2665 *overlaps_a = conflict_fn_no_dependence ();
2666 *overlaps_b = conflict_fn_no_dependence ();
2667 *last_conflicts = integer_zero_node;
2668 goto end_analyze_subs_aa;
2671 if (i1 > 0 && j1 > 0)
2673 HOST_WIDE_INT niter_a
2674 = max_stmt_executions_int (get_chrec_loop (chrec_a));
2675 HOST_WIDE_INT niter_b
2676 = max_stmt_executions_int (get_chrec_loop (chrec_b));
2677 HOST_WIDE_INT niter = MIN (niter_a, niter_b);
2679 /* (X0, Y0) is a solution of the Diophantine equation:
2680 "chrec_a (X0) = chrec_b (Y0)". */
2681 HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
2682 CEIL (-j0, j1));
2683 HOST_WIDE_INT x0 = i1 * tau1 + i0;
2684 HOST_WIDE_INT y0 = j1 * tau1 + j0;
2686 /* (X1, Y1) is the smallest positive solution of the eq
2687 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2688 first conflict occurs. */
2689 HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
2690 HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
2691 HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
2693 if (niter > 0)
2695 HOST_WIDE_INT tau2 = MIN (FLOOR_DIV (niter - i0, i1),
2696 FLOOR_DIV (niter - j0, j1));
2697 HOST_WIDE_INT last_conflict = tau2 - (x1 - i0)/i1;
2699 /* If the overlap occurs outside of the bounds of the
2700 loop, there is no dependence. */
2701 if (x1 >= niter || y1 >= niter)
2703 *overlaps_a = conflict_fn_no_dependence ();
2704 *overlaps_b = conflict_fn_no_dependence ();
2705 *last_conflicts = integer_zero_node;
2706 goto end_analyze_subs_aa;
2708 else
2709 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2711 else
2712 *last_conflicts = chrec_dont_know;
2714 *overlaps_a
2715 = conflict_fn (1,
2716 affine_fn_univar (build_int_cst (NULL_TREE, x1),
2718 build_int_cst (NULL_TREE, i1)));
2719 *overlaps_b
2720 = conflict_fn (1,
2721 affine_fn_univar (build_int_cst (NULL_TREE, y1),
2723 build_int_cst (NULL_TREE, j1)));
2725 else
2727 /* FIXME: For the moment, the upper bound of the
2728 iteration domain for i and j is not checked. */
2729 if (dump_file && (dump_flags & TDF_DETAILS))
2730 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2731 *overlaps_a = conflict_fn_not_known ();
2732 *overlaps_b = conflict_fn_not_known ();
2733 *last_conflicts = chrec_dont_know;
2736 else
2738 if (dump_file && (dump_flags & TDF_DETAILS))
2739 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2740 *overlaps_a = conflict_fn_not_known ();
2741 *overlaps_b = conflict_fn_not_known ();
2742 *last_conflicts = chrec_dont_know;
2745 else
2747 if (dump_file && (dump_flags & TDF_DETAILS))
2748 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2749 *overlaps_a = conflict_fn_not_known ();
2750 *overlaps_b = conflict_fn_not_known ();
2751 *last_conflicts = chrec_dont_know;
2754 end_analyze_subs_aa:
2755 obstack_free (&scratch_obstack, NULL);
2756 if (dump_file && (dump_flags & TDF_DETAILS))
2758 fprintf (dump_file, " (overlaps_a = ");
2759 dump_conflict_function (dump_file, *overlaps_a);
2760 fprintf (dump_file, ")\n (overlaps_b = ");
2761 dump_conflict_function (dump_file, *overlaps_b);
2762 fprintf (dump_file, "))\n");
2766 /* Returns true when analyze_subscript_affine_affine can be used for
2767 determining the dependence relation between chrec_a and chrec_b,
2768 that contain symbols. This function modifies chrec_a and chrec_b
2769 such that the analysis result is the same, and such that they don't
2770 contain symbols, and then can safely be passed to the analyzer.
2772 Example: The analysis of the following tuples of evolutions produce
2773 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2774 vs. {0, +, 1}_1
2776 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2777 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2780 static bool
2781 can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
2783 tree diff, type, left_a, left_b, right_b;
2785 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
2786 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
2787 /* FIXME: For the moment not handled. Might be refined later. */
2788 return false;
2790 type = chrec_type (*chrec_a);
2791 left_a = CHREC_LEFT (*chrec_a);
2792 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
2793 diff = chrec_fold_minus (type, left_a, left_b);
2795 if (!evolution_function_is_constant_p (diff))
2796 return false;
2798 if (dump_file && (dump_flags & TDF_DETAILS))
2799 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
2801 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
2802 diff, CHREC_RIGHT (*chrec_a));
2803 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
2804 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
2805 build_int_cst (type, 0),
2806 right_b);
2807 return true;
2810 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2811 *OVERLAPS_B are initialized to the functions that describe the
2812 relation between the elements accessed twice by CHREC_A and
2813 CHREC_B. For k >= 0, the following property is verified:
2815 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2817 static void
2818 analyze_siv_subscript (tree chrec_a,
2819 tree chrec_b,
2820 conflict_function **overlaps_a,
2821 conflict_function **overlaps_b,
2822 tree *last_conflicts,
2823 int loop_nest_num)
2825 dependence_stats.num_siv++;
2827 if (dump_file && (dump_flags & TDF_DETAILS))
2828 fprintf (dump_file, "(analyze_siv_subscript \n");
2830 if (evolution_function_is_constant_p (chrec_a)
2831 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2832 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
2833 overlaps_a, overlaps_b, last_conflicts);
2835 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2836 && evolution_function_is_constant_p (chrec_b))
2837 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
2838 overlaps_b, overlaps_a, last_conflicts);
2840 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2841 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2843 if (!chrec_contains_symbols (chrec_a)
2844 && !chrec_contains_symbols (chrec_b))
2846 analyze_subscript_affine_affine (chrec_a, chrec_b,
2847 overlaps_a, overlaps_b,
2848 last_conflicts);
2850 if (CF_NOT_KNOWN_P (*overlaps_a)
2851 || CF_NOT_KNOWN_P (*overlaps_b))
2852 dependence_stats.num_siv_unimplemented++;
2853 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2854 || CF_NO_DEPENDENCE_P (*overlaps_b))
2855 dependence_stats.num_siv_independent++;
2856 else
2857 dependence_stats.num_siv_dependent++;
2859 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
2860 &chrec_b))
2862 analyze_subscript_affine_affine (chrec_a, chrec_b,
2863 overlaps_a, overlaps_b,
2864 last_conflicts);
2866 if (CF_NOT_KNOWN_P (*overlaps_a)
2867 || CF_NOT_KNOWN_P (*overlaps_b))
2868 dependence_stats.num_siv_unimplemented++;
2869 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2870 || CF_NO_DEPENDENCE_P (*overlaps_b))
2871 dependence_stats.num_siv_independent++;
2872 else
2873 dependence_stats.num_siv_dependent++;
2875 else
2876 goto siv_subscript_dontknow;
2879 else
2881 siv_subscript_dontknow:;
2882 if (dump_file && (dump_flags & TDF_DETAILS))
2883 fprintf (dump_file, " siv test failed: unimplemented");
2884 *overlaps_a = conflict_fn_not_known ();
2885 *overlaps_b = conflict_fn_not_known ();
2886 *last_conflicts = chrec_dont_know;
2887 dependence_stats.num_siv_unimplemented++;
2890 if (dump_file && (dump_flags & TDF_DETAILS))
2891 fprintf (dump_file, ")\n");
2894 /* Returns false if we can prove that the greatest common divisor of the steps
2895 of CHREC does not divide CST, false otherwise. */
2897 static bool
2898 gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
2900 HOST_WIDE_INT cd = 0, val;
2901 tree step;
2903 if (!tree_fits_shwi_p (cst))
2904 return true;
2905 val = tree_to_shwi (cst);
2907 while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
2909 step = CHREC_RIGHT (chrec);
2910 if (!tree_fits_shwi_p (step))
2911 return true;
2912 cd = gcd (cd, tree_to_shwi (step));
2913 chrec = CHREC_LEFT (chrec);
2916 return val % cd == 0;
2919 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
2920 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2921 functions that describe the relation between the elements accessed
2922 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2923 is verified:
2925 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2927 static void
2928 analyze_miv_subscript (tree chrec_a,
2929 tree chrec_b,
2930 conflict_function **overlaps_a,
2931 conflict_function **overlaps_b,
2932 tree *last_conflicts,
2933 struct loop *loop_nest)
2935 tree type, difference;
2937 dependence_stats.num_miv++;
2938 if (dump_file && (dump_flags & TDF_DETAILS))
2939 fprintf (dump_file, "(analyze_miv_subscript \n");
2941 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
2942 chrec_a = chrec_convert (type, chrec_a, NULL);
2943 chrec_b = chrec_convert (type, chrec_b, NULL);
2944 difference = chrec_fold_minus (type, chrec_a, chrec_b);
2946 if (eq_evolutions_p (chrec_a, chrec_b))
2948 /* Access functions are the same: all the elements are accessed
2949 in the same order. */
2950 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2951 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2952 *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
2953 dependence_stats.num_miv_dependent++;
2956 else if (evolution_function_is_constant_p (difference)
2957 /* For the moment, the following is verified:
2958 evolution_function_is_affine_multivariate_p (chrec_a,
2959 loop_nest->num) */
2960 && !gcd_of_steps_may_divide_p (chrec_a, difference))
2962 /* testsuite/.../ssa-chrec-33.c
2963 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2965 The difference is 1, and all the evolution steps are multiples
2966 of 2, consequently there are no overlapping elements. */
2967 *overlaps_a = conflict_fn_no_dependence ();
2968 *overlaps_b = conflict_fn_no_dependence ();
2969 *last_conflicts = integer_zero_node;
2970 dependence_stats.num_miv_independent++;
2973 else if (evolution_function_is_affine_multivariate_p (chrec_a, loop_nest->num)
2974 && !chrec_contains_symbols (chrec_a)
2975 && evolution_function_is_affine_multivariate_p (chrec_b, loop_nest->num)
2976 && !chrec_contains_symbols (chrec_b))
2978 /* testsuite/.../ssa-chrec-35.c
2979 {0, +, 1}_2 vs. {0, +, 1}_3
2980 the overlapping elements are respectively located at iterations:
2981 {0, +, 1}_x and {0, +, 1}_x,
2982 in other words, we have the equality:
2983 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2985 Other examples:
2986 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2987 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2989 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2990 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2992 analyze_subscript_affine_affine (chrec_a, chrec_b,
2993 overlaps_a, overlaps_b, last_conflicts);
2995 if (CF_NOT_KNOWN_P (*overlaps_a)
2996 || CF_NOT_KNOWN_P (*overlaps_b))
2997 dependence_stats.num_miv_unimplemented++;
2998 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2999 || CF_NO_DEPENDENCE_P (*overlaps_b))
3000 dependence_stats.num_miv_independent++;
3001 else
3002 dependence_stats.num_miv_dependent++;
3005 else
3007 /* When the analysis is too difficult, answer "don't know". */
3008 if (dump_file && (dump_flags & TDF_DETAILS))
3009 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
3011 *overlaps_a = conflict_fn_not_known ();
3012 *overlaps_b = conflict_fn_not_known ();
3013 *last_conflicts = chrec_dont_know;
3014 dependence_stats.num_miv_unimplemented++;
3017 if (dump_file && (dump_flags & TDF_DETAILS))
3018 fprintf (dump_file, ")\n");
3021 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
3022 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
3023 OVERLAP_ITERATIONS_B are initialized with two functions that
3024 describe the iterations that contain conflicting elements.
3026 Remark: For an integer k >= 0, the following equality is true:
3028 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
3031 static void
3032 analyze_overlapping_iterations (tree chrec_a,
3033 tree chrec_b,
3034 conflict_function **overlap_iterations_a,
3035 conflict_function **overlap_iterations_b,
3036 tree *last_conflicts, struct loop *loop_nest)
3038 unsigned int lnn = loop_nest->num;
3040 dependence_stats.num_subscript_tests++;
3042 if (dump_file && (dump_flags & TDF_DETAILS))
3044 fprintf (dump_file, "(analyze_overlapping_iterations \n");
3045 fprintf (dump_file, " (chrec_a = ");
3046 print_generic_expr (dump_file, chrec_a, 0);
3047 fprintf (dump_file, ")\n (chrec_b = ");
3048 print_generic_expr (dump_file, chrec_b, 0);
3049 fprintf (dump_file, ")\n");
3052 if (chrec_a == NULL_TREE
3053 || chrec_b == NULL_TREE
3054 || chrec_contains_undetermined (chrec_a)
3055 || chrec_contains_undetermined (chrec_b))
3057 dependence_stats.num_subscript_undetermined++;
3059 *overlap_iterations_a = conflict_fn_not_known ();
3060 *overlap_iterations_b = conflict_fn_not_known ();
3063 /* If they are the same chrec, and are affine, they overlap
3064 on every iteration. */
3065 else if (eq_evolutions_p (chrec_a, chrec_b)
3066 && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3067 || operand_equal_p (chrec_a, chrec_b, 0)))
3069 dependence_stats.num_same_subscript_function++;
3070 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3071 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3072 *last_conflicts = chrec_dont_know;
3075 /* If they aren't the same, and aren't affine, we can't do anything
3076 yet. */
3077 else if ((chrec_contains_symbols (chrec_a)
3078 || chrec_contains_symbols (chrec_b))
3079 && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3080 || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
3082 dependence_stats.num_subscript_undetermined++;
3083 *overlap_iterations_a = conflict_fn_not_known ();
3084 *overlap_iterations_b = conflict_fn_not_known ();
3087 else if (ziv_subscript_p (chrec_a, chrec_b))
3088 analyze_ziv_subscript (chrec_a, chrec_b,
3089 overlap_iterations_a, overlap_iterations_b,
3090 last_conflicts);
3092 else if (siv_subscript_p (chrec_a, chrec_b))
3093 analyze_siv_subscript (chrec_a, chrec_b,
3094 overlap_iterations_a, overlap_iterations_b,
3095 last_conflicts, lnn);
3097 else
3098 analyze_miv_subscript (chrec_a, chrec_b,
3099 overlap_iterations_a, overlap_iterations_b,
3100 last_conflicts, loop_nest);
3102 if (dump_file && (dump_flags & TDF_DETAILS))
3104 fprintf (dump_file, " (overlap_iterations_a = ");
3105 dump_conflict_function (dump_file, *overlap_iterations_a);
3106 fprintf (dump_file, ")\n (overlap_iterations_b = ");
3107 dump_conflict_function (dump_file, *overlap_iterations_b);
3108 fprintf (dump_file, "))\n");
3112 /* Helper function for uniquely inserting distance vectors. */
3114 static void
3115 save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
3117 unsigned i;
3118 lambda_vector v;
3120 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, v)
3121 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
3122 return;
3124 DDR_DIST_VECTS (ddr).safe_push (dist_v);
3127 /* Helper function for uniquely inserting direction vectors. */
3129 static void
3130 save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
3132 unsigned i;
3133 lambda_vector v;
3135 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), i, v)
3136 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
3137 return;
3139 DDR_DIR_VECTS (ddr).safe_push (dir_v);
3142 /* Add a distance of 1 on all the loops outer than INDEX. If we
3143 haven't yet determined a distance for this outer loop, push a new
3144 distance vector composed of the previous distance, and a distance
3145 of 1 for this outer loop. Example:
3147 | loop_1
3148 | loop_2
3149 | A[10]
3150 | endloop_2
3151 | endloop_1
3153 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3154 save (0, 1), then we have to save (1, 0). */
3156 static void
3157 add_outer_distances (struct data_dependence_relation *ddr,
3158 lambda_vector dist_v, int index)
3160 /* For each outer loop where init_v is not set, the accesses are
3161 in dependence of distance 1 in the loop. */
3162 while (--index >= 0)
3164 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3165 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3166 save_v[index] = 1;
3167 save_dist_v (ddr, save_v);
3171 /* Return false when fail to represent the data dependence as a
3172 distance vector. INIT_B is set to true when a component has been
3173 added to the distance vector DIST_V. INDEX_CARRY is then set to
3174 the index in DIST_V that carries the dependence. */
3176 static bool
3177 build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
3178 struct data_reference *ddr_a,
3179 struct data_reference *ddr_b,
3180 lambda_vector dist_v, bool *init_b,
3181 int *index_carry)
3183 unsigned i;
3184 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3186 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3188 tree access_fn_a, access_fn_b;
3189 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3191 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3193 non_affine_dependence_relation (ddr);
3194 return false;
3197 access_fn_a = DR_ACCESS_FN (ddr_a, i);
3198 access_fn_b = DR_ACCESS_FN (ddr_b, i);
3200 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3201 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3203 int dist, index;
3204 int var_a = CHREC_VARIABLE (access_fn_a);
3205 int var_b = CHREC_VARIABLE (access_fn_b);
3207 if (var_a != var_b
3208 || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3210 non_affine_dependence_relation (ddr);
3211 return false;
3214 dist = int_cst_value (SUB_DISTANCE (subscript));
3215 index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
3216 *index_carry = MIN (index, *index_carry);
3218 /* This is the subscript coupling test. If we have already
3219 recorded a distance for this loop (a distance coming from
3220 another subscript), it should be the same. For example,
3221 in the following code, there is no dependence:
3223 | loop i = 0, N, 1
3224 | T[i+1][i] = ...
3225 | ... = T[i][i]
3226 | endloop
3228 if (init_v[index] != 0 && dist_v[index] != dist)
3230 finalize_ddr_dependent (ddr, chrec_known);
3231 return false;
3234 dist_v[index] = dist;
3235 init_v[index] = 1;
3236 *init_b = true;
3238 else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
3240 /* This can be for example an affine vs. constant dependence
3241 (T[i] vs. T[3]) that is not an affine dependence and is
3242 not representable as a distance vector. */
3243 non_affine_dependence_relation (ddr);
3244 return false;
3248 return true;
3251 /* Return true when the DDR contains only constant access functions. */
3253 static bool
3254 constant_access_functions (const struct data_dependence_relation *ddr)
3256 unsigned i;
3258 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3259 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr), i))
3260 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr), i)))
3261 return false;
3263 return true;
3266 /* Helper function for the case where DDR_A and DDR_B are the same
3267 multivariate access function with a constant step. For an example
3268 see pr34635-1.c. */
3270 static void
3271 add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
3273 int x_1, x_2;
3274 tree c_1 = CHREC_LEFT (c_2);
3275 tree c_0 = CHREC_LEFT (c_1);
3276 lambda_vector dist_v;
3277 int v1, v2, cd;
3279 /* Polynomials with more than 2 variables are not handled yet. When
3280 the evolution steps are parameters, it is not possible to
3281 represent the dependence using classical distance vectors. */
3282 if (TREE_CODE (c_0) != INTEGER_CST
3283 || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
3284 || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
3286 DDR_AFFINE_P (ddr) = false;
3287 return;
3290 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
3291 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
3293 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3294 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3295 v1 = int_cst_value (CHREC_RIGHT (c_1));
3296 v2 = int_cst_value (CHREC_RIGHT (c_2));
3297 cd = gcd (v1, v2);
3298 v1 /= cd;
3299 v2 /= cd;
3301 if (v2 < 0)
3303 v2 = -v2;
3304 v1 = -v1;
3307 dist_v[x_1] = v2;
3308 dist_v[x_2] = -v1;
3309 save_dist_v (ddr, dist_v);
3311 add_outer_distances (ddr, dist_v, x_1);
3314 /* Helper function for the case where DDR_A and DDR_B are the same
3315 access functions. */
3317 static void
3318 add_other_self_distances (struct data_dependence_relation *ddr)
3320 lambda_vector dist_v;
3321 unsigned i;
3322 int index_carry = DDR_NB_LOOPS (ddr);
3324 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3326 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
3328 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
3330 if (!evolution_function_is_univariate_p (access_fun))
3332 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
3334 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3335 return;
3338 access_fun = DR_ACCESS_FN (DDR_A (ddr), 0);
3340 if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
3341 add_multivariate_self_dist (ddr, access_fun);
3342 else
3343 /* The evolution step is not constant: it varies in
3344 the outer loop, so this cannot be represented by a
3345 distance vector. For example in pr34635.c the
3346 evolution is {0, +, {0, +, 4}_1}_2. */
3347 DDR_AFFINE_P (ddr) = false;
3349 return;
3352 index_carry = MIN (index_carry,
3353 index_in_loop_nest (CHREC_VARIABLE (access_fun),
3354 DDR_LOOP_NEST (ddr)));
3358 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3359 add_outer_distances (ddr, dist_v, index_carry);
3362 static void
3363 insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
3365 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3367 dist_v[DDR_INNER_LOOP (ddr)] = 1;
3368 save_dist_v (ddr, dist_v);
3371 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
3372 is the case for example when access functions are the same and
3373 equal to a constant, as in:
3375 | loop_1
3376 | A[3] = ...
3377 | ... = A[3]
3378 | endloop_1
3380 in which case the distance vectors are (0) and (1). */
3382 static void
3383 add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
3385 unsigned i, j;
3387 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3389 subscript_p sub = DDR_SUBSCRIPT (ddr, i);
3390 conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
3391 conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
3393 for (j = 0; j < ca->n; j++)
3394 if (affine_function_zero_p (ca->fns[j]))
3396 insert_innermost_unit_dist_vector (ddr);
3397 return;
3400 for (j = 0; j < cb->n; j++)
3401 if (affine_function_zero_p (cb->fns[j]))
3403 insert_innermost_unit_dist_vector (ddr);
3404 return;
3409 /* Compute the classic per loop distance vector. DDR is the data
3410 dependence relation to build a vector from. Return false when fail
3411 to represent the data dependence as a distance vector. */
3413 static bool
3414 build_classic_dist_vector (struct data_dependence_relation *ddr,
3415 struct loop *loop_nest)
3417 bool init_b = false;
3418 int index_carry = DDR_NB_LOOPS (ddr);
3419 lambda_vector dist_v;
3421 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3422 return false;
3424 if (same_access_functions (ddr))
3426 /* Save the 0 vector. */
3427 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3428 save_dist_v (ddr, dist_v);
3430 if (constant_access_functions (ddr))
3431 add_distance_for_zero_overlaps (ddr);
3433 if (DDR_NB_LOOPS (ddr) > 1)
3434 add_other_self_distances (ddr);
3436 return true;
3439 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3440 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3441 dist_v, &init_b, &index_carry))
3442 return false;
3444 /* Save the distance vector if we initialized one. */
3445 if (init_b)
3447 /* Verify a basic constraint: classic distance vectors should
3448 always be lexicographically positive.
3450 Data references are collected in the order of execution of
3451 the program, thus for the following loop
3453 | for (i = 1; i < 100; i++)
3454 | for (j = 1; j < 100; j++)
3456 | t = T[j+1][i-1]; // A
3457 | T[j][i] = t + 2; // B
3460 references are collected following the direction of the wind:
3461 A then B. The data dependence tests are performed also
3462 following this order, such that we're looking at the distance
3463 separating the elements accessed by A from the elements later
3464 accessed by B. But in this example, the distance returned by
3465 test_dep (A, B) is lexicographically negative (-1, 1), that
3466 means that the access A occurs later than B with respect to
3467 the outer loop, ie. we're actually looking upwind. In this
3468 case we solve test_dep (B, A) looking downwind to the
3469 lexicographically positive solution, that returns the
3470 distance vector (1, -1). */
3471 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3473 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3474 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3475 loop_nest))
3476 return false;
3477 compute_subscript_distance (ddr);
3478 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3479 save_v, &init_b, &index_carry))
3480 return false;
3481 save_dist_v (ddr, save_v);
3482 DDR_REVERSED_P (ddr) = true;
3484 /* In this case there is a dependence forward for all the
3485 outer loops:
3487 | for (k = 1; k < 100; k++)
3488 | for (i = 1; i < 100; i++)
3489 | for (j = 1; j < 100; j++)
3491 | t = T[j+1][i-1]; // A
3492 | T[j][i] = t + 2; // B
3495 the vectors are:
3496 (0, 1, -1)
3497 (1, 1, -1)
3498 (1, -1, 1)
3500 if (DDR_NB_LOOPS (ddr) > 1)
3502 add_outer_distances (ddr, save_v, index_carry);
3503 add_outer_distances (ddr, dist_v, index_carry);
3506 else
3508 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3509 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3511 if (DDR_NB_LOOPS (ddr) > 1)
3513 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3515 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr),
3516 DDR_A (ddr), loop_nest))
3517 return false;
3518 compute_subscript_distance (ddr);
3519 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3520 opposite_v, &init_b,
3521 &index_carry))
3522 return false;
3524 save_dist_v (ddr, save_v);
3525 add_outer_distances (ddr, dist_v, index_carry);
3526 add_outer_distances (ddr, opposite_v, index_carry);
3528 else
3529 save_dist_v (ddr, save_v);
3532 else
3534 /* There is a distance of 1 on all the outer loops: Example:
3535 there is a dependence of distance 1 on loop_1 for the array A.
3537 | loop_1
3538 | A[5] = ...
3539 | endloop
3541 add_outer_distances (ddr, dist_v,
3542 lambda_vector_first_nz (dist_v,
3543 DDR_NB_LOOPS (ddr), 0));
3546 if (dump_file && (dump_flags & TDF_DETAILS))
3548 unsigned i;
3550 fprintf (dump_file, "(build_classic_dist_vector\n");
3551 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3553 fprintf (dump_file, " dist_vector = (");
3554 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3555 DDR_NB_LOOPS (ddr));
3556 fprintf (dump_file, " )\n");
3558 fprintf (dump_file, ")\n");
3561 return true;
3564 /* Return the direction for a given distance.
3565 FIXME: Computing dir this way is suboptimal, since dir can catch
3566 cases that dist is unable to represent. */
3568 static inline enum data_dependence_direction
3569 dir_from_dist (int dist)
3571 if (dist > 0)
3572 return dir_positive;
3573 else if (dist < 0)
3574 return dir_negative;
3575 else
3576 return dir_equal;
3579 /* Compute the classic per loop direction vector. DDR is the data
3580 dependence relation to build a vector from. */
3582 static void
3583 build_classic_dir_vector (struct data_dependence_relation *ddr)
3585 unsigned i, j;
3586 lambda_vector dist_v;
3588 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
3590 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3592 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3593 dir_v[j] = dir_from_dist (dist_v[j]);
3595 save_dir_v (ddr, dir_v);
3599 /* Helper function. Returns true when there is a dependence between
3600 data references DRA and DRB. */
3602 static bool
3603 subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
3604 struct data_reference *dra,
3605 struct data_reference *drb,
3606 struct loop *loop_nest)
3608 unsigned int i;
3609 tree last_conflicts;
3610 struct subscript *subscript;
3611 tree res = NULL_TREE;
3613 for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
3615 conflict_function *overlaps_a, *overlaps_b;
3617 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3618 DR_ACCESS_FN (drb, i),
3619 &overlaps_a, &overlaps_b,
3620 &last_conflicts, loop_nest);
3622 if (SUB_CONFLICTS_IN_A (subscript))
3623 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
3624 if (SUB_CONFLICTS_IN_B (subscript))
3625 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
3627 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3628 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3629 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3631 /* If there is any undetermined conflict function we have to
3632 give a conservative answer in case we cannot prove that
3633 no dependence exists when analyzing another subscript. */
3634 if (CF_NOT_KNOWN_P (overlaps_a)
3635 || CF_NOT_KNOWN_P (overlaps_b))
3637 res = chrec_dont_know;
3638 continue;
3641 /* When there is a subscript with no dependence we can stop. */
3642 else if (CF_NO_DEPENDENCE_P (overlaps_a)
3643 || CF_NO_DEPENDENCE_P (overlaps_b))
3645 res = chrec_known;
3646 break;
3650 if (res == NULL_TREE)
3651 return true;
3653 if (res == chrec_known)
3654 dependence_stats.num_dependence_independent++;
3655 else
3656 dependence_stats.num_dependence_undetermined++;
3657 finalize_ddr_dependent (ddr, res);
3658 return false;
3661 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
3663 static void
3664 subscript_dependence_tester (struct data_dependence_relation *ddr,
3665 struct loop *loop_nest)
3667 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr), loop_nest))
3668 dependence_stats.num_dependence_dependent++;
3670 compute_subscript_distance (ddr);
3671 if (build_classic_dist_vector (ddr, loop_nest))
3672 build_classic_dir_vector (ddr);
3675 /* Returns true when all the access functions of A are affine or
3676 constant with respect to LOOP_NEST. */
3678 static bool
3679 access_functions_are_affine_or_constant_p (const struct data_reference *a,
3680 const struct loop *loop_nest)
3682 unsigned int i;
3683 vec<tree> fns = DR_ACCESS_FNS (a);
3684 tree t;
3686 FOR_EACH_VEC_ELT (fns, i, t)
3687 if (!evolution_function_is_invariant_p (t, loop_nest->num)
3688 && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
3689 return false;
3691 return true;
3694 /* Initializes an equation for an OMEGA problem using the information
3695 contained in the ACCESS_FUN. Returns true when the operation
3696 succeeded.
3698 PB is the omega constraint system.
3699 EQ is the number of the equation to be initialized.
3700 OFFSET is used for shifting the variables names in the constraints:
3701 a constrain is composed of 2 * the number of variables surrounding
3702 dependence accesses. OFFSET is set either to 0 for the first n variables,
3703 then it is set to n.
3704 ACCESS_FUN is expected to be an affine chrec. */
3706 static bool
3707 init_omega_eq_with_af (omega_pb pb, unsigned eq,
3708 unsigned int offset, tree access_fun,
3709 struct data_dependence_relation *ddr)
3711 switch (TREE_CODE (access_fun))
3713 case POLYNOMIAL_CHREC:
3715 tree left = CHREC_LEFT (access_fun);
3716 tree right = CHREC_RIGHT (access_fun);
3717 int var = CHREC_VARIABLE (access_fun);
3718 unsigned var_idx;
3720 if (TREE_CODE (right) != INTEGER_CST)
3721 return false;
3723 var_idx = index_in_loop_nest (var, DDR_LOOP_NEST (ddr));
3724 pb->eqs[eq].coef[offset + var_idx + 1] = int_cst_value (right);
3726 /* Compute the innermost loop index. */
3727 DDR_INNER_LOOP (ddr) = MAX (DDR_INNER_LOOP (ddr), var_idx);
3729 if (offset == 0)
3730 pb->eqs[eq].coef[var_idx + DDR_NB_LOOPS (ddr) + 1]
3731 += int_cst_value (right);
3733 switch (TREE_CODE (left))
3735 case POLYNOMIAL_CHREC:
3736 return init_omega_eq_with_af (pb, eq, offset, left, ddr);
3738 case INTEGER_CST:
3739 pb->eqs[eq].coef[0] += int_cst_value (left);
3740 return true;
3742 default:
3743 return false;
3747 case INTEGER_CST:
3748 pb->eqs[eq].coef[0] += int_cst_value (access_fun);
3749 return true;
3751 default:
3752 return false;
3756 /* As explained in the comments preceding init_omega_for_ddr, we have
3757 to set up a system for each loop level, setting outer loops
3758 variation to zero, and current loop variation to positive or zero.
3759 Save each lexico positive distance vector. */
3761 static void
3762 omega_extract_distance_vectors (omega_pb pb,
3763 struct data_dependence_relation *ddr)
3765 int eq, geq;
3766 unsigned i, j;
3767 struct loop *loopi, *loopj;
3768 enum omega_result res;
3770 /* Set a new problem for each loop in the nest. The basis is the
3771 problem that we have initialized until now. On top of this we
3772 add new constraints. */
3773 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3774 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3776 int dist = 0;
3777 omega_pb copy = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr),
3778 DDR_NB_LOOPS (ddr));
3780 omega_copy_problem (copy, pb);
3782 /* For all the outer loops "loop_j", add "dj = 0". */
3783 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3785 eq = omega_add_zero_eq (copy, omega_black);
3786 copy->eqs[eq].coef[j + 1] = 1;
3789 /* For "loop_i", add "0 <= di". */
3790 geq = omega_add_zero_geq (copy, omega_black);
3791 copy->geqs[geq].coef[i + 1] = 1;
3793 /* Reduce the constraint system, and test that the current
3794 problem is feasible. */
3795 res = omega_simplify_problem (copy);
3796 if (res == omega_false
3797 || res == omega_unknown
3798 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3799 goto next_problem;
3801 for (eq = 0; eq < copy->num_subs; eq++)
3802 if (copy->subs[eq].key == (int) i + 1)
3804 dist = copy->subs[eq].coef[0];
3805 goto found_dist;
3808 if (dist == 0)
3810 /* Reinitialize problem... */
3811 omega_copy_problem (copy, pb);
3812 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3814 eq = omega_add_zero_eq (copy, omega_black);
3815 copy->eqs[eq].coef[j + 1] = 1;
3818 /* ..., but this time "di = 1". */
3819 eq = omega_add_zero_eq (copy, omega_black);
3820 copy->eqs[eq].coef[i + 1] = 1;
3821 copy->eqs[eq].coef[0] = -1;
3823 res = omega_simplify_problem (copy);
3824 if (res == omega_false
3825 || res == omega_unknown
3826 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3827 goto next_problem;
3829 for (eq = 0; eq < copy->num_subs; eq++)
3830 if (copy->subs[eq].key == (int) i + 1)
3832 dist = copy->subs[eq].coef[0];
3833 goto found_dist;
3837 found_dist:;
3838 /* Save the lexicographically positive distance vector. */
3839 if (dist >= 0)
3841 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3842 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3844 dist_v[i] = dist;
3846 for (eq = 0; eq < copy->num_subs; eq++)
3847 if (copy->subs[eq].key > 0)
3849 dist = copy->subs[eq].coef[0];
3850 dist_v[copy->subs[eq].key - 1] = dist;
3853 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3854 dir_v[j] = dir_from_dist (dist_v[j]);
3856 save_dist_v (ddr, dist_v);
3857 save_dir_v (ddr, dir_v);
3860 next_problem:;
3861 omega_free_problem (copy);
3865 /* This is called for each subscript of a tuple of data references:
3866 insert an equality for representing the conflicts. */
3868 static bool
3869 omega_setup_subscript (tree access_fun_a, tree access_fun_b,
3870 struct data_dependence_relation *ddr,
3871 omega_pb pb, bool *maybe_dependent)
3873 int eq;
3874 tree type = signed_type_for_types (TREE_TYPE (access_fun_a),
3875 TREE_TYPE (access_fun_b));
3876 tree fun_a = chrec_convert (type, access_fun_a, NULL);
3877 tree fun_b = chrec_convert (type, access_fun_b, NULL);
3878 tree difference = chrec_fold_minus (type, fun_a, fun_b);
3879 tree minus_one;
3881 /* When the fun_a - fun_b is not constant, the dependence is not
3882 captured by the classic distance vector representation. */
3883 if (TREE_CODE (difference) != INTEGER_CST)
3884 return false;
3886 /* ZIV test. */
3887 if (ziv_subscript_p (fun_a, fun_b) && !integer_zerop (difference))
3889 /* There is no dependence. */
3890 *maybe_dependent = false;
3891 return true;
3894 minus_one = build_int_cst (type, -1);
3895 fun_b = chrec_fold_multiply (type, fun_b, minus_one);
3897 eq = omega_add_zero_eq (pb, omega_black);
3898 if (!init_omega_eq_with_af (pb, eq, DDR_NB_LOOPS (ddr), fun_a, ddr)
3899 || !init_omega_eq_with_af (pb, eq, 0, fun_b, ddr))
3900 /* There is probably a dependence, but the system of
3901 constraints cannot be built: answer "don't know". */
3902 return false;
3904 /* GCD test. */
3905 if (DDR_NB_LOOPS (ddr) != 0 && pb->eqs[eq].coef[0]
3906 && !int_divides_p (lambda_vector_gcd
3907 ((lambda_vector) &(pb->eqs[eq].coef[1]),
3908 2 * DDR_NB_LOOPS (ddr)),
3909 pb->eqs[eq].coef[0]))
3911 /* There is no dependence. */
3912 *maybe_dependent = false;
3913 return true;
3916 return true;
3919 /* Helper function, same as init_omega_for_ddr but specialized for
3920 data references A and B. */
3922 static bool
3923 init_omega_for_ddr_1 (struct data_reference *dra, struct data_reference *drb,
3924 struct data_dependence_relation *ddr,
3925 omega_pb pb, bool *maybe_dependent)
3927 unsigned i;
3928 int ineq;
3929 struct loop *loopi;
3930 unsigned nb_loops = DDR_NB_LOOPS (ddr);
3932 /* Insert an equality per subscript. */
3933 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3935 if (!omega_setup_subscript (DR_ACCESS_FN (dra, i), DR_ACCESS_FN (drb, i),
3936 ddr, pb, maybe_dependent))
3937 return false;
3938 else if (*maybe_dependent == false)
3940 /* There is no dependence. */
3941 DDR_ARE_DEPENDENT (ddr) = chrec_known;
3942 return true;
3946 /* Insert inequalities: constraints corresponding to the iteration
3947 domain, i.e. the loops surrounding the references "loop_x" and
3948 the distance variables "dx". The layout of the OMEGA
3949 representation is as follows:
3950 - coef[0] is the constant
3951 - coef[1..nb_loops] are the protected variables that will not be
3952 removed by the solver: the "dx"
3953 - coef[nb_loops + 1, 2*nb_loops] are the loop variables: "loop_x".
3955 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3956 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3958 HOST_WIDE_INT nbi = max_stmt_executions_int (loopi);
3960 /* 0 <= loop_x */
3961 ineq = omega_add_zero_geq (pb, omega_black);
3962 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3964 /* 0 <= loop_x + dx */
3965 ineq = omega_add_zero_geq (pb, omega_black);
3966 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3967 pb->geqs[ineq].coef[i + 1] = 1;
3969 if (nbi != -1)
3971 /* loop_x <= nb_iters */
3972 ineq = omega_add_zero_geq (pb, omega_black);
3973 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3974 pb->geqs[ineq].coef[0] = nbi;
3976 /* loop_x + dx <= nb_iters */
3977 ineq = omega_add_zero_geq (pb, omega_black);
3978 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3979 pb->geqs[ineq].coef[i + 1] = -1;
3980 pb->geqs[ineq].coef[0] = nbi;
3982 /* A step "dx" bigger than nb_iters is not feasible, so
3983 add "0 <= nb_iters + dx", */
3984 ineq = omega_add_zero_geq (pb, omega_black);
3985 pb->geqs[ineq].coef[i + 1] = 1;
3986 pb->geqs[ineq].coef[0] = nbi;
3987 /* and "dx <= nb_iters". */
3988 ineq = omega_add_zero_geq (pb, omega_black);
3989 pb->geqs[ineq].coef[i + 1] = -1;
3990 pb->geqs[ineq].coef[0] = nbi;
3994 omega_extract_distance_vectors (pb, ddr);
3996 return true;
3999 /* Sets up the Omega dependence problem for the data dependence
4000 relation DDR. Returns false when the constraint system cannot be
4001 built, ie. when the test answers "don't know". Returns true
4002 otherwise, and when independence has been proved (using one of the
4003 trivial dependence test), set MAYBE_DEPENDENT to false, otherwise
4004 set MAYBE_DEPENDENT to true.
4006 Example: for setting up the dependence system corresponding to the
4007 conflicting accesses
4009 | loop_i
4010 | loop_j
4011 | A[i, i+1] = ...
4012 | ... A[2*j, 2*(i + j)]
4013 | endloop_j
4014 | endloop_i
4016 the following constraints come from the iteration domain:
4018 0 <= i <= Ni
4019 0 <= i + di <= Ni
4020 0 <= j <= Nj
4021 0 <= j + dj <= Nj
4023 where di, dj are the distance variables. The constraints
4024 representing the conflicting elements are:
4026 i = 2 * (j + dj)
4027 i + 1 = 2 * (i + di + j + dj)
4029 For asking that the resulting distance vector (di, dj) be
4030 lexicographically positive, we insert the constraint "di >= 0". If
4031 "di = 0" in the solution, we fix that component to zero, and we
4032 look at the inner loops: we set a new problem where all the outer
4033 loop distances are zero, and fix this inner component to be
4034 positive. When one of the components is positive, we save that
4035 distance, and set a new problem where the distance on this loop is
4036 zero, searching for other distances in the inner loops. Here is
4037 the classic example that illustrates that we have to set for each
4038 inner loop a new problem:
4040 | loop_1
4041 | loop_2
4042 | A[10]
4043 | endloop_2
4044 | endloop_1
4046 we have to save two distances (1, 0) and (0, 1).
4048 Given two array references, refA and refB, we have to set the
4049 dependence problem twice, refA vs. refB and refB vs. refA, and we
4050 cannot do a single test, as refB might occur before refA in the
4051 inner loops, and the contrary when considering outer loops: ex.
4053 | loop_0
4054 | loop_1
4055 | loop_2
4056 | T[{1,+,1}_2][{1,+,1}_1] // refA
4057 | T[{2,+,1}_2][{0,+,1}_1] // refB
4058 | endloop_2
4059 | endloop_1
4060 | endloop_0
4062 refB touches the elements in T before refA, and thus for the same
4063 loop_0 refB precedes refA: ie. the distance vector (0, 1, -1)
4064 but for successive loop_0 iterations, we have (1, -1, 1)
4066 The Omega solver expects the distance variables ("di" in the
4067 previous example) to come first in the constraint system (as
4068 variables to be protected, or "safe" variables), the constraint
4069 system is built using the following layout:
4071 "cst | distance vars | index vars".
4074 static bool
4075 init_omega_for_ddr (struct data_dependence_relation *ddr,
4076 bool *maybe_dependent)
4078 omega_pb pb;
4079 bool res = false;
4081 *maybe_dependent = true;
4083 if (same_access_functions (ddr))
4085 unsigned j;
4086 lambda_vector dir_v;
4088 /* Save the 0 vector. */
4089 save_dist_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
4090 dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4091 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
4092 dir_v[j] = dir_equal;
4093 save_dir_v (ddr, dir_v);
4095 /* Save the dependences carried by outer loops. */
4096 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4097 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4098 maybe_dependent);
4099 omega_free_problem (pb);
4100 return res;
4103 /* Omega expects the protected variables (those that have to be kept
4104 after elimination) to appear first in the constraint system.
4105 These variables are the distance variables. In the following
4106 initialization we declare NB_LOOPS safe variables, and the total
4107 number of variables for the constraint system is 2*NB_LOOPS. */
4108 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4109 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4110 maybe_dependent);
4111 omega_free_problem (pb);
4113 /* Stop computation if not decidable, or no dependence. */
4114 if (res == false || *maybe_dependent == false)
4115 return res;
4117 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4118 res = init_omega_for_ddr_1 (DDR_B (ddr), DDR_A (ddr), ddr, pb,
4119 maybe_dependent);
4120 omega_free_problem (pb);
4122 return res;
4125 /* Return true when DDR contains the same information as that stored
4126 in DIR_VECTS and in DIST_VECTS, return false otherwise. */
4128 static bool
4129 ddr_consistent_p (FILE *file,
4130 struct data_dependence_relation *ddr,
4131 vec<lambda_vector> dist_vects,
4132 vec<lambda_vector> dir_vects)
4134 unsigned int i, j;
4136 /* If dump_file is set, output there. */
4137 if (dump_file && (dump_flags & TDF_DETAILS))
4138 file = dump_file;
4140 if (dist_vects.length () != DDR_NUM_DIST_VECTS (ddr))
4142 lambda_vector b_dist_v;
4143 fprintf (file, "\n(Number of distance vectors differ: Banerjee has %d, Omega has %d.\n",
4144 dist_vects.length (),
4145 DDR_NUM_DIST_VECTS (ddr));
4147 fprintf (file, "Banerjee dist vectors:\n");
4148 FOR_EACH_VEC_ELT (dist_vects, i, b_dist_v)
4149 print_lambda_vector (file, b_dist_v, DDR_NB_LOOPS (ddr));
4151 fprintf (file, "Omega dist vectors:\n");
4152 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4153 print_lambda_vector (file, DDR_DIST_VECT (ddr, i), DDR_NB_LOOPS (ddr));
4155 fprintf (file, "data dependence relation:\n");
4156 dump_data_dependence_relation (file, ddr);
4158 fprintf (file, ")\n");
4159 return false;
4162 if (dir_vects.length () != DDR_NUM_DIR_VECTS (ddr))
4164 fprintf (file, "\n(Number of direction vectors differ: Banerjee has %d, Omega has %d.)\n",
4165 dir_vects.length (),
4166 DDR_NUM_DIR_VECTS (ddr));
4167 return false;
4170 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4172 lambda_vector a_dist_v;
4173 lambda_vector b_dist_v = DDR_DIST_VECT (ddr, i);
4175 /* Distance vectors are not ordered in the same way in the DDR
4176 and in the DIST_VECTS: search for a matching vector. */
4177 FOR_EACH_VEC_ELT (dist_vects, j, a_dist_v)
4178 if (lambda_vector_equal (a_dist_v, b_dist_v, DDR_NB_LOOPS (ddr)))
4179 break;
4181 if (j == dist_vects.length ())
4183 fprintf (file, "\n(Dist vectors from the first dependence analyzer:\n");
4184 print_dist_vectors (file, dist_vects, DDR_NB_LOOPS (ddr));
4185 fprintf (file, "not found in Omega dist vectors:\n");
4186 print_dist_vectors (file, DDR_DIST_VECTS (ddr), DDR_NB_LOOPS (ddr));
4187 fprintf (file, "data dependence relation:\n");
4188 dump_data_dependence_relation (file, ddr);
4189 fprintf (file, ")\n");
4193 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
4195 lambda_vector a_dir_v;
4196 lambda_vector b_dir_v = DDR_DIR_VECT (ddr, i);
4198 /* Direction vectors are not ordered in the same way in the DDR
4199 and in the DIR_VECTS: search for a matching vector. */
4200 FOR_EACH_VEC_ELT (dir_vects, j, a_dir_v)
4201 if (lambda_vector_equal (a_dir_v, b_dir_v, DDR_NB_LOOPS (ddr)))
4202 break;
4204 if (j == dist_vects.length ())
4206 fprintf (file, "\n(Dir vectors from the first dependence analyzer:\n");
4207 print_dir_vectors (file, dir_vects, DDR_NB_LOOPS (ddr));
4208 fprintf (file, "not found in Omega dir vectors:\n");
4209 print_dir_vectors (file, DDR_DIR_VECTS (ddr), DDR_NB_LOOPS (ddr));
4210 fprintf (file, "data dependence relation:\n");
4211 dump_data_dependence_relation (file, ddr);
4212 fprintf (file, ")\n");
4216 return true;
4219 /* This computes the affine dependence relation between A and B with
4220 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
4221 independence between two accesses, while CHREC_DONT_KNOW is used
4222 for representing the unknown relation.
4224 Note that it is possible to stop the computation of the dependence
4225 relation the first time we detect a CHREC_KNOWN element for a given
4226 subscript. */
4228 void
4229 compute_affine_dependence (struct data_dependence_relation *ddr,
4230 struct loop *loop_nest)
4232 struct data_reference *dra = DDR_A (ddr);
4233 struct data_reference *drb = DDR_B (ddr);
4235 if (dump_file && (dump_flags & TDF_DETAILS))
4237 fprintf (dump_file, "(compute_affine_dependence\n");
4238 fprintf (dump_file, " stmt_a: ");
4239 print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
4240 fprintf (dump_file, " stmt_b: ");
4241 print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
4244 /* Analyze only when the dependence relation is not yet known. */
4245 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4247 dependence_stats.num_dependence_tests++;
4249 if (access_functions_are_affine_or_constant_p (dra, loop_nest)
4250 && access_functions_are_affine_or_constant_p (drb, loop_nest))
4252 subscript_dependence_tester (ddr, loop_nest);
4254 if (flag_check_data_deps)
4256 /* Dump the dependences from the first algorithm. */
4257 if (dump_file && (dump_flags & TDF_DETAILS))
4259 fprintf (dump_file, "\n\nBanerjee Analyzer\n");
4260 dump_data_dependence_relation (dump_file, ddr);
4263 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4265 bool maybe_dependent;
4266 vec<lambda_vector> dir_vects, dist_vects;
4268 /* Save the result of the first DD analyzer. */
4269 dist_vects = DDR_DIST_VECTS (ddr);
4270 dir_vects = DDR_DIR_VECTS (ddr);
4272 /* Reset the information. */
4273 DDR_DIST_VECTS (ddr).create (0);
4274 DDR_DIR_VECTS (ddr).create (0);
4276 /* Compute the same information using Omega. */
4277 if (!init_omega_for_ddr (ddr, &maybe_dependent))
4278 goto csys_dont_know;
4280 if (dump_file && (dump_flags & TDF_DETAILS))
4282 fprintf (dump_file, "Omega Analyzer\n");
4283 dump_data_dependence_relation (dump_file, ddr);
4286 /* Check that we get the same information. */
4287 if (maybe_dependent)
4288 gcc_assert (ddr_consistent_p (stderr, ddr, dist_vects,
4289 dir_vects));
4294 /* As a last case, if the dependence cannot be determined, or if
4295 the dependence is considered too difficult to determine, answer
4296 "don't know". */
4297 else
4299 csys_dont_know:;
4300 dependence_stats.num_dependence_undetermined++;
4302 if (dump_file && (dump_flags & TDF_DETAILS))
4304 fprintf (dump_file, "Data ref a:\n");
4305 dump_data_reference (dump_file, dra);
4306 fprintf (dump_file, "Data ref b:\n");
4307 dump_data_reference (dump_file, drb);
4308 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
4310 finalize_ddr_dependent (ddr, chrec_dont_know);
4314 if (dump_file && (dump_flags & TDF_DETAILS))
4316 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4317 fprintf (dump_file, ") -> no dependence\n");
4318 else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
4319 fprintf (dump_file, ") -> dependence analysis failed\n");
4320 else
4321 fprintf (dump_file, ")\n");
4325 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
4326 the data references in DATAREFS, in the LOOP_NEST. When
4327 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
4328 relations. Return true when successful, i.e. data references number
4329 is small enough to be handled. */
4331 bool
4332 compute_all_dependences (vec<data_reference_p> datarefs,
4333 vec<ddr_p> *dependence_relations,
4334 vec<loop_p> loop_nest,
4335 bool compute_self_and_rr)
4337 struct data_dependence_relation *ddr;
4338 struct data_reference *a, *b;
4339 unsigned int i, j;
4341 if ((int) datarefs.length ()
4342 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
4344 struct data_dependence_relation *ddr;
4346 /* Insert a single relation into dependence_relations:
4347 chrec_dont_know. */
4348 ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
4349 dependence_relations->safe_push (ddr);
4350 return false;
4353 FOR_EACH_VEC_ELT (datarefs, i, a)
4354 for (j = i + 1; datarefs.iterate (j, &b); j++)
4355 if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
4357 ddr = initialize_data_dependence_relation (a, b, loop_nest);
4358 dependence_relations->safe_push (ddr);
4359 if (loop_nest.exists ())
4360 compute_affine_dependence (ddr, loop_nest[0]);
4363 if (compute_self_and_rr)
4364 FOR_EACH_VEC_ELT (datarefs, i, a)
4366 ddr = initialize_data_dependence_relation (a, a, loop_nest);
4367 dependence_relations->safe_push (ddr);
4368 if (loop_nest.exists ())
4369 compute_affine_dependence (ddr, loop_nest[0]);
4372 return true;
4375 /* Describes a location of a memory reference. */
4377 typedef struct data_ref_loc_d
4379 /* The memory reference. */
4380 tree ref;
4382 /* True if the memory reference is read. */
4383 bool is_read;
4384 } data_ref_loc;
4387 /* Stores the locations of memory references in STMT to REFERENCES. Returns
4388 true if STMT clobbers memory, false otherwise. */
4390 static bool
4391 get_references_in_stmt (gimple stmt, vec<data_ref_loc, va_heap> *references)
4393 bool clobbers_memory = false;
4394 data_ref_loc ref;
4395 tree op0, op1;
4396 enum gimple_code stmt_code = gimple_code (stmt);
4398 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4399 As we cannot model data-references to not spelled out
4400 accesses give up if they may occur. */
4401 if (stmt_code == GIMPLE_CALL
4402 && !(gimple_call_flags (stmt) & ECF_CONST))
4404 /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
4405 if (gimple_call_internal_p (stmt))
4406 switch (gimple_call_internal_fn (stmt))
4408 case IFN_GOMP_SIMD_LANE:
4410 struct loop *loop = gimple_bb (stmt)->loop_father;
4411 tree uid = gimple_call_arg (stmt, 0);
4412 gcc_assert (TREE_CODE (uid) == SSA_NAME);
4413 if (loop == NULL
4414 || loop->simduid != SSA_NAME_VAR (uid))
4415 clobbers_memory = true;
4416 break;
4418 case IFN_MASK_LOAD:
4419 case IFN_MASK_STORE:
4420 break;
4421 default:
4422 clobbers_memory = true;
4423 break;
4425 else
4426 clobbers_memory = true;
4428 else if (stmt_code == GIMPLE_ASM
4429 && (gimple_asm_volatile_p (as_a <gasm *> (stmt))
4430 || gimple_vuse (stmt)))
4431 clobbers_memory = true;
4433 if (!gimple_vuse (stmt))
4434 return clobbers_memory;
4436 if (stmt_code == GIMPLE_ASSIGN)
4438 tree base;
4439 op0 = gimple_assign_lhs (stmt);
4440 op1 = gimple_assign_rhs1 (stmt);
4442 if (DECL_P (op1)
4443 || (REFERENCE_CLASS_P (op1)
4444 && (base = get_base_address (op1))
4445 && TREE_CODE (base) != SSA_NAME))
4447 ref.ref = op1;
4448 ref.is_read = true;
4449 references->safe_push (ref);
4452 else if (stmt_code == GIMPLE_CALL)
4454 unsigned i, n;
4456 ref.is_read = false;
4457 if (gimple_call_internal_p (stmt))
4458 switch (gimple_call_internal_fn (stmt))
4460 case IFN_MASK_LOAD:
4461 if (gimple_call_lhs (stmt) == NULL_TREE)
4462 break;
4463 ref.is_read = true;
4464 case IFN_MASK_STORE:
4465 ref.ref = fold_build2 (MEM_REF,
4466 ref.is_read
4467 ? TREE_TYPE (gimple_call_lhs (stmt))
4468 : TREE_TYPE (gimple_call_arg (stmt, 3)),
4469 gimple_call_arg (stmt, 0),
4470 gimple_call_arg (stmt, 1));
4471 references->safe_push (ref);
4472 return false;
4473 default:
4474 break;
4477 op0 = gimple_call_lhs (stmt);
4478 n = gimple_call_num_args (stmt);
4479 for (i = 0; i < n; i++)
4481 op1 = gimple_call_arg (stmt, i);
4483 if (DECL_P (op1)
4484 || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
4486 ref.ref = op1;
4487 ref.is_read = true;
4488 references->safe_push (ref);
4492 else
4493 return clobbers_memory;
4495 if (op0
4496 && (DECL_P (op0)
4497 || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
4499 ref.ref = op0;
4500 ref.is_read = false;
4501 references->safe_push (ref);
4503 return clobbers_memory;
4506 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
4507 reference, returns false, otherwise returns true. NEST is the outermost
4508 loop of the loop nest in which the references should be analyzed. */
4510 bool
4511 find_data_references_in_stmt (struct loop *nest, gimple stmt,
4512 vec<data_reference_p> *datarefs)
4514 unsigned i;
4515 auto_vec<data_ref_loc, 2> references;
4516 data_ref_loc *ref;
4517 bool ret = true;
4518 data_reference_p dr;
4520 if (get_references_in_stmt (stmt, &references))
4521 return false;
4523 FOR_EACH_VEC_ELT (references, i, ref)
4525 dr = create_data_ref (nest, loop_containing_stmt (stmt),
4526 ref->ref, stmt, ref->is_read);
4527 gcc_assert (dr != NULL);
4528 datarefs->safe_push (dr);
4530 references.release ();
4531 return ret;
4534 /* Stores the data references in STMT to DATAREFS. If there is an
4535 unanalyzable reference, returns false, otherwise returns true.
4536 NEST is the outermost loop of the loop nest in which the references
4537 should be instantiated, LOOP is the loop in which the references
4538 should be analyzed. */
4540 bool
4541 graphite_find_data_references_in_stmt (loop_p nest, loop_p loop, gimple stmt,
4542 vec<data_reference_p> *datarefs)
4544 unsigned i;
4545 auto_vec<data_ref_loc, 2> references;
4546 data_ref_loc *ref;
4547 bool ret = true;
4548 data_reference_p dr;
4550 if (get_references_in_stmt (stmt, &references))
4551 return false;
4553 FOR_EACH_VEC_ELT (references, i, ref)
4555 dr = create_data_ref (nest, loop, ref->ref, stmt, ref->is_read);
4556 gcc_assert (dr != NULL);
4557 datarefs->safe_push (dr);
4560 references.release ();
4561 return ret;
4564 /* Search the data references in LOOP, and record the information into
4565 DATAREFS. Returns chrec_dont_know when failing to analyze a
4566 difficult case, returns NULL_TREE otherwise. */
4568 tree
4569 find_data_references_in_bb (struct loop *loop, basic_block bb,
4570 vec<data_reference_p> *datarefs)
4572 gimple_stmt_iterator bsi;
4574 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4576 gimple stmt = gsi_stmt (bsi);
4578 if (!find_data_references_in_stmt (loop, stmt, datarefs))
4580 struct data_reference *res;
4581 res = XCNEW (struct data_reference);
4582 datarefs->safe_push (res);
4584 return chrec_dont_know;
4588 return NULL_TREE;
4591 /* Search the data references in LOOP, and record the information into
4592 DATAREFS. Returns chrec_dont_know when failing to analyze a
4593 difficult case, returns NULL_TREE otherwise.
4595 TODO: This function should be made smarter so that it can handle address
4596 arithmetic as if they were array accesses, etc. */
4598 tree
4599 find_data_references_in_loop (struct loop *loop,
4600 vec<data_reference_p> *datarefs)
4602 basic_block bb, *bbs;
4603 unsigned int i;
4605 bbs = get_loop_body_in_dom_order (loop);
4607 for (i = 0; i < loop->num_nodes; i++)
4609 bb = bbs[i];
4611 if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
4613 free (bbs);
4614 return chrec_dont_know;
4617 free (bbs);
4619 return NULL_TREE;
4622 /* Recursive helper function. */
4624 static bool
4625 find_loop_nest_1 (struct loop *loop, vec<loop_p> *loop_nest)
4627 /* Inner loops of the nest should not contain siblings. Example:
4628 when there are two consecutive loops,
4630 | loop_0
4631 | loop_1
4632 | A[{0, +, 1}_1]
4633 | endloop_1
4634 | loop_2
4635 | A[{0, +, 1}_2]
4636 | endloop_2
4637 | endloop_0
4639 the dependence relation cannot be captured by the distance
4640 abstraction. */
4641 if (loop->next)
4642 return false;
4644 loop_nest->safe_push (loop);
4645 if (loop->inner)
4646 return find_loop_nest_1 (loop->inner, loop_nest);
4647 return true;
4650 /* Return false when the LOOP is not well nested. Otherwise return
4651 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4652 contain the loops from the outermost to the innermost, as they will
4653 appear in the classic distance vector. */
4655 bool
4656 find_loop_nest (struct loop *loop, vec<loop_p> *loop_nest)
4658 loop_nest->safe_push (loop);
4659 if (loop->inner)
4660 return find_loop_nest_1 (loop->inner, loop_nest);
4661 return true;
4664 /* Returns true when the data dependences have been computed, false otherwise.
4665 Given a loop nest LOOP, the following vectors are returned:
4666 DATAREFS is initialized to all the array elements contained in this loop,
4667 DEPENDENCE_RELATIONS contains the relations between the data references.
4668 Compute read-read and self relations if
4669 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4671 bool
4672 compute_data_dependences_for_loop (struct loop *loop,
4673 bool compute_self_and_read_read_dependences,
4674 vec<loop_p> *loop_nest,
4675 vec<data_reference_p> *datarefs,
4676 vec<ddr_p> *dependence_relations)
4678 bool res = true;
4680 memset (&dependence_stats, 0, sizeof (dependence_stats));
4682 /* If the loop nest is not well formed, or one of the data references
4683 is not computable, give up without spending time to compute other
4684 dependences. */
4685 if (!loop
4686 || !find_loop_nest (loop, loop_nest)
4687 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
4688 || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
4689 compute_self_and_read_read_dependences))
4690 res = false;
4692 if (dump_file && (dump_flags & TDF_STATS))
4694 fprintf (dump_file, "Dependence tester statistics:\n");
4696 fprintf (dump_file, "Number of dependence tests: %d\n",
4697 dependence_stats.num_dependence_tests);
4698 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
4699 dependence_stats.num_dependence_dependent);
4700 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
4701 dependence_stats.num_dependence_independent);
4702 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
4703 dependence_stats.num_dependence_undetermined);
4705 fprintf (dump_file, "Number of subscript tests: %d\n",
4706 dependence_stats.num_subscript_tests);
4707 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
4708 dependence_stats.num_subscript_undetermined);
4709 fprintf (dump_file, "Number of same subscript function: %d\n",
4710 dependence_stats.num_same_subscript_function);
4712 fprintf (dump_file, "Number of ziv tests: %d\n",
4713 dependence_stats.num_ziv);
4714 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4715 dependence_stats.num_ziv_dependent);
4716 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4717 dependence_stats.num_ziv_independent);
4718 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
4719 dependence_stats.num_ziv_unimplemented);
4721 fprintf (dump_file, "Number of siv tests: %d\n",
4722 dependence_stats.num_siv);
4723 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4724 dependence_stats.num_siv_dependent);
4725 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4726 dependence_stats.num_siv_independent);
4727 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4728 dependence_stats.num_siv_unimplemented);
4730 fprintf (dump_file, "Number of miv tests: %d\n",
4731 dependence_stats.num_miv);
4732 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4733 dependence_stats.num_miv_dependent);
4734 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4735 dependence_stats.num_miv_independent);
4736 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4737 dependence_stats.num_miv_unimplemented);
4740 return res;
4743 /* Returns true when the data dependences for the basic block BB have been
4744 computed, false otherwise.
4745 DATAREFS is initialized to all the array elements contained in this basic
4746 block, DEPENDENCE_RELATIONS contains the relations between the data
4747 references. Compute read-read and self relations if
4748 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4749 bool
4750 compute_data_dependences_for_bb (basic_block bb,
4751 bool compute_self_and_read_read_dependences,
4752 vec<data_reference_p> *datarefs,
4753 vec<ddr_p> *dependence_relations)
4755 if (find_data_references_in_bb (NULL, bb, datarefs) == chrec_dont_know)
4756 return false;
4758 return compute_all_dependences (*datarefs, dependence_relations, vNULL,
4759 compute_self_and_read_read_dependences);
4762 /* Entry point (for testing only). Analyze all the data references
4763 and the dependence relations in LOOP.
4765 The data references are computed first.
4767 A relation on these nodes is represented by a complete graph. Some
4768 of the relations could be of no interest, thus the relations can be
4769 computed on demand.
4771 In the following function we compute all the relations. This is
4772 just a first implementation that is here for:
4773 - for showing how to ask for the dependence relations,
4774 - for the debugging the whole dependence graph,
4775 - for the dejagnu testcases and maintenance.
4777 It is possible to ask only for a part of the graph, avoiding to
4778 compute the whole dependence graph. The computed dependences are
4779 stored in a knowledge base (KB) such that later queries don't
4780 recompute the same information. The implementation of this KB is
4781 transparent to the optimizer, and thus the KB can be changed with a
4782 more efficient implementation, or the KB could be disabled. */
4783 static void
4784 analyze_all_data_dependences (struct loop *loop)
4786 unsigned int i;
4787 int nb_data_refs = 10;
4788 vec<data_reference_p> datarefs;
4789 datarefs.create (nb_data_refs);
4790 vec<ddr_p> dependence_relations;
4791 dependence_relations.create (nb_data_refs * nb_data_refs);
4792 vec<loop_p> loop_nest;
4793 loop_nest.create (3);
4795 /* Compute DDs on the whole function. */
4796 compute_data_dependences_for_loop (loop, false, &loop_nest, &datarefs,
4797 &dependence_relations);
4799 if (dump_file)
4801 dump_data_dependence_relations (dump_file, dependence_relations);
4802 fprintf (dump_file, "\n\n");
4804 if (dump_flags & TDF_DETAILS)
4805 dump_dist_dir_vectors (dump_file, dependence_relations);
4807 if (dump_flags & TDF_STATS)
4809 unsigned nb_top_relations = 0;
4810 unsigned nb_bot_relations = 0;
4811 unsigned nb_chrec_relations = 0;
4812 struct data_dependence_relation *ddr;
4814 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4816 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
4817 nb_top_relations++;
4819 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4820 nb_bot_relations++;
4822 else
4823 nb_chrec_relations++;
4826 gather_stats_on_scev_database ();
4830 loop_nest.release ();
4831 free_dependence_relations (dependence_relations);
4832 free_data_refs (datarefs);
4835 /* Computes all the data dependences and check that the results of
4836 several analyzers are the same. */
4838 void
4839 tree_check_data_deps (void)
4841 struct loop *loop_nest;
4843 FOR_EACH_LOOP (loop_nest, 0)
4844 analyze_all_data_dependences (loop_nest);
4847 /* Free the memory used by a data dependence relation DDR. */
4849 void
4850 free_dependence_relation (struct data_dependence_relation *ddr)
4852 if (ddr == NULL)
4853 return;
4855 if (DDR_SUBSCRIPTS (ddr).exists ())
4856 free_subscripts (DDR_SUBSCRIPTS (ddr));
4857 DDR_DIST_VECTS (ddr).release ();
4858 DDR_DIR_VECTS (ddr).release ();
4860 free (ddr);
4863 /* Free the memory used by the data dependence relations from
4864 DEPENDENCE_RELATIONS. */
4866 void
4867 free_dependence_relations (vec<ddr_p> dependence_relations)
4869 unsigned int i;
4870 struct data_dependence_relation *ddr;
4872 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4873 if (ddr)
4874 free_dependence_relation (ddr);
4876 dependence_relations.release ();
4879 /* Free the memory used by the data references from DATAREFS. */
4881 void
4882 free_data_refs (vec<data_reference_p> datarefs)
4884 unsigned int i;
4885 struct data_reference *dr;
4887 FOR_EACH_VEC_ELT (datarefs, i, dr)
4888 free_data_ref (dr);
4889 datarefs.release ();