don't compare ARG_FRAME_POINTER_REGNUM and FRAME_POINTER_REGNUM with the preprocessor
[official-gcc.git] / gcc / tree-data-ref.c
blob505d63b00832d4aed0e0a6782a067f2596abf956
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 "hash-set.h"
80 #include "machmode.h"
81 #include "vec.h"
82 #include "double-int.h"
83 #include "input.h"
84 #include "alias.h"
85 #include "symtab.h"
86 #include "options.h"
87 #include "wide-int.h"
88 #include "inchash.h"
89 #include "tree.h"
90 #include "fold-const.h"
91 #include "hashtab.h"
92 #include "tm.h"
93 #include "hard-reg-set.h"
94 #include "function.h"
95 #include "rtl.h"
96 #include "flags.h"
97 #include "statistics.h"
98 #include "real.h"
99 #include "fixed-value.h"
100 #include "insn-config.h"
101 #include "expmed.h"
102 #include "dojump.h"
103 #include "explow.h"
104 #include "calls.h"
105 #include "emit-rtl.h"
106 #include "varasm.h"
107 #include "stmt.h"
108 #include "expr.h"
109 #include "gimple-pretty-print.h"
110 #include "predict.h"
111 #include "dominance.h"
112 #include "cfg.h"
113 #include "basic-block.h"
114 #include "tree-ssa-alias.h"
115 #include "internal-fn.h"
116 #include "gimple-expr.h"
117 #include "is-a.h"
118 #include "gimple.h"
119 #include "gimple-iterator.h"
120 #include "tree-ssa-loop-niter.h"
121 #include "tree-ssa-loop.h"
122 #include "tree-ssa.h"
123 #include "cfgloop.h"
124 #include "tree-data-ref.h"
125 #include "tree-scalar-evolution.h"
126 #include "dumpfile.h"
127 #include "langhooks.h"
128 #include "tree-affine.h"
129 #include "params.h"
131 static struct datadep_stats
133 int num_dependence_tests;
134 int num_dependence_dependent;
135 int num_dependence_independent;
136 int num_dependence_undetermined;
138 int num_subscript_tests;
139 int num_subscript_undetermined;
140 int num_same_subscript_function;
142 int num_ziv;
143 int num_ziv_independent;
144 int num_ziv_dependent;
145 int num_ziv_unimplemented;
147 int num_siv;
148 int num_siv_independent;
149 int num_siv_dependent;
150 int num_siv_unimplemented;
152 int num_miv;
153 int num_miv_independent;
154 int num_miv_dependent;
155 int num_miv_unimplemented;
156 } dependence_stats;
158 static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
159 struct data_reference *,
160 struct data_reference *,
161 struct loop *);
162 /* Returns true iff A divides B. */
164 static inline bool
165 tree_fold_divides_p (const_tree a, const_tree b)
167 gcc_assert (TREE_CODE (a) == INTEGER_CST);
168 gcc_assert (TREE_CODE (b) == INTEGER_CST);
169 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a));
172 /* Returns true iff A divides B. */
174 static inline bool
175 int_divides_p (int a, int b)
177 return ((b % a) == 0);
182 /* Dump into FILE all the data references from DATAREFS. */
184 static void
185 dump_data_references (FILE *file, vec<data_reference_p> datarefs)
187 unsigned int i;
188 struct data_reference *dr;
190 FOR_EACH_VEC_ELT (datarefs, i, dr)
191 dump_data_reference (file, dr);
194 /* Unified dump into FILE all the data references from DATAREFS. */
196 DEBUG_FUNCTION void
197 debug (vec<data_reference_p> &ref)
199 dump_data_references (stderr, ref);
202 DEBUG_FUNCTION void
203 debug (vec<data_reference_p> *ptr)
205 if (ptr)
206 debug (*ptr);
207 else
208 fprintf (stderr, "<nil>\n");
212 /* Dump into STDERR all the data references from DATAREFS. */
214 DEBUG_FUNCTION void
215 debug_data_references (vec<data_reference_p> datarefs)
217 dump_data_references (stderr, datarefs);
220 /* Print to STDERR the data_reference DR. */
222 DEBUG_FUNCTION void
223 debug_data_reference (struct data_reference *dr)
225 dump_data_reference (stderr, dr);
228 /* Dump function for a DATA_REFERENCE structure. */
230 void
231 dump_data_reference (FILE *outf,
232 struct data_reference *dr)
234 unsigned int i;
236 fprintf (outf, "#(Data Ref: \n");
237 fprintf (outf, "# bb: %d \n", gimple_bb (DR_STMT (dr))->index);
238 fprintf (outf, "# stmt: ");
239 print_gimple_stmt (outf, DR_STMT (dr), 0, 0);
240 fprintf (outf, "# ref: ");
241 print_generic_stmt (outf, DR_REF (dr), 0);
242 fprintf (outf, "# base_object: ");
243 print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
245 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
247 fprintf (outf, "# Access function %d: ", i);
248 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
250 fprintf (outf, "#)\n");
253 /* Unified dump function for a DATA_REFERENCE structure. */
255 DEBUG_FUNCTION void
256 debug (data_reference &ref)
258 dump_data_reference (stderr, &ref);
261 DEBUG_FUNCTION void
262 debug (data_reference *ptr)
264 if (ptr)
265 debug (*ptr);
266 else
267 fprintf (stderr, "<nil>\n");
271 /* Dumps the affine function described by FN to the file OUTF. */
273 static void
274 dump_affine_function (FILE *outf, affine_fn fn)
276 unsigned i;
277 tree coef;
279 print_generic_expr (outf, fn[0], TDF_SLIM);
280 for (i = 1; fn.iterate (i, &coef); i++)
282 fprintf (outf, " + ");
283 print_generic_expr (outf, coef, TDF_SLIM);
284 fprintf (outf, " * x_%u", i);
288 /* Dumps the conflict function CF to the file OUTF. */
290 static void
291 dump_conflict_function (FILE *outf, conflict_function *cf)
293 unsigned i;
295 if (cf->n == NO_DEPENDENCE)
296 fprintf (outf, "no dependence");
297 else if (cf->n == NOT_KNOWN)
298 fprintf (outf, "not known");
299 else
301 for (i = 0; i < cf->n; i++)
303 if (i != 0)
304 fprintf (outf, " ");
305 fprintf (outf, "[");
306 dump_affine_function (outf, cf->fns[i]);
307 fprintf (outf, "]");
312 /* Dump function for a SUBSCRIPT structure. */
314 static void
315 dump_subscript (FILE *outf, struct subscript *subscript)
317 conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
319 fprintf (outf, "\n (subscript \n");
320 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
321 dump_conflict_function (outf, cf);
322 if (CF_NONTRIVIAL_P (cf))
324 tree last_iteration = SUB_LAST_CONFLICT (subscript);
325 fprintf (outf, "\n last_conflict: ");
326 print_generic_expr (outf, last_iteration, 0);
329 cf = SUB_CONFLICTS_IN_B (subscript);
330 fprintf (outf, "\n iterations_that_access_an_element_twice_in_B: ");
331 dump_conflict_function (outf, cf);
332 if (CF_NONTRIVIAL_P (cf))
334 tree last_iteration = SUB_LAST_CONFLICT (subscript);
335 fprintf (outf, "\n last_conflict: ");
336 print_generic_expr (outf, last_iteration, 0);
339 fprintf (outf, "\n (Subscript distance: ");
340 print_generic_expr (outf, SUB_DISTANCE (subscript), 0);
341 fprintf (outf, " ))\n");
344 /* Print the classic direction vector DIRV to OUTF. */
346 static void
347 print_direction_vector (FILE *outf,
348 lambda_vector dirv,
349 int length)
351 int eq;
353 for (eq = 0; eq < length; eq++)
355 enum data_dependence_direction dir = ((enum data_dependence_direction)
356 dirv[eq]);
358 switch (dir)
360 case dir_positive:
361 fprintf (outf, " +");
362 break;
363 case dir_negative:
364 fprintf (outf, " -");
365 break;
366 case dir_equal:
367 fprintf (outf, " =");
368 break;
369 case dir_positive_or_equal:
370 fprintf (outf, " +=");
371 break;
372 case dir_positive_or_negative:
373 fprintf (outf, " +-");
374 break;
375 case dir_negative_or_equal:
376 fprintf (outf, " -=");
377 break;
378 case dir_star:
379 fprintf (outf, " *");
380 break;
381 default:
382 fprintf (outf, "indep");
383 break;
386 fprintf (outf, "\n");
389 /* Print a vector of direction vectors. */
391 static void
392 print_dir_vectors (FILE *outf, vec<lambda_vector> dir_vects,
393 int length)
395 unsigned j;
396 lambda_vector v;
398 FOR_EACH_VEC_ELT (dir_vects, j, v)
399 print_direction_vector (outf, v, length);
402 /* Print out a vector VEC of length N to OUTFILE. */
404 static inline void
405 print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
407 int i;
409 for (i = 0; i < n; i++)
410 fprintf (outfile, "%3d ", vector[i]);
411 fprintf (outfile, "\n");
414 /* Print a vector of distance vectors. */
416 static void
417 print_dist_vectors (FILE *outf, vec<lambda_vector> dist_vects,
418 int length)
420 unsigned j;
421 lambda_vector v;
423 FOR_EACH_VEC_ELT (dist_vects, j, v)
424 print_lambda_vector (outf, v, length);
427 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
429 static void
430 dump_data_dependence_relation (FILE *outf,
431 struct data_dependence_relation *ddr)
433 struct data_reference *dra, *drb;
435 fprintf (outf, "(Data Dep: \n");
437 if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
439 if (ddr)
441 dra = DDR_A (ddr);
442 drb = DDR_B (ddr);
443 if (dra)
444 dump_data_reference (outf, dra);
445 else
446 fprintf (outf, " (nil)\n");
447 if (drb)
448 dump_data_reference (outf, drb);
449 else
450 fprintf (outf, " (nil)\n");
452 fprintf (outf, " (don't know)\n)\n");
453 return;
456 dra = DDR_A (ddr);
457 drb = DDR_B (ddr);
458 dump_data_reference (outf, dra);
459 dump_data_reference (outf, drb);
461 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
462 fprintf (outf, " (no dependence)\n");
464 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
466 unsigned int i;
467 struct loop *loopi;
469 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
471 fprintf (outf, " access_fn_A: ");
472 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
473 fprintf (outf, " access_fn_B: ");
474 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
475 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
478 fprintf (outf, " inner loop index: %d\n", DDR_INNER_LOOP (ddr));
479 fprintf (outf, " loop nest: (");
480 FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
481 fprintf (outf, "%d ", loopi->num);
482 fprintf (outf, ")\n");
484 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
486 fprintf (outf, " distance_vector: ");
487 print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
488 DDR_NB_LOOPS (ddr));
491 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
493 fprintf (outf, " direction_vector: ");
494 print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
495 DDR_NB_LOOPS (ddr));
499 fprintf (outf, ")\n");
502 /* Debug version. */
504 DEBUG_FUNCTION void
505 debug_data_dependence_relation (struct data_dependence_relation *ddr)
507 dump_data_dependence_relation (stderr, ddr);
510 /* Dump into FILE all the dependence relations from DDRS. */
512 void
513 dump_data_dependence_relations (FILE *file,
514 vec<ddr_p> ddrs)
516 unsigned int i;
517 struct data_dependence_relation *ddr;
519 FOR_EACH_VEC_ELT (ddrs, i, ddr)
520 dump_data_dependence_relation (file, ddr);
523 DEBUG_FUNCTION void
524 debug (vec<ddr_p> &ref)
526 dump_data_dependence_relations (stderr, ref);
529 DEBUG_FUNCTION void
530 debug (vec<ddr_p> *ptr)
532 if (ptr)
533 debug (*ptr);
534 else
535 fprintf (stderr, "<nil>\n");
539 /* Dump to STDERR all the dependence relations from DDRS. */
541 DEBUG_FUNCTION void
542 debug_data_dependence_relations (vec<ddr_p> ddrs)
544 dump_data_dependence_relations (stderr, ddrs);
547 /* Dumps the distance and direction vectors in FILE. DDRS contains
548 the dependence relations, and VECT_SIZE is the size of the
549 dependence vectors, or in other words the number of loops in the
550 considered nest. */
552 static void
553 dump_dist_dir_vectors (FILE *file, vec<ddr_p> ddrs)
555 unsigned int i, j;
556 struct data_dependence_relation *ddr;
557 lambda_vector v;
559 FOR_EACH_VEC_ELT (ddrs, i, ddr)
560 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
562 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), j, v)
564 fprintf (file, "DISTANCE_V (");
565 print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
566 fprintf (file, ")\n");
569 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), j, v)
571 fprintf (file, "DIRECTION_V (");
572 print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
573 fprintf (file, ")\n");
577 fprintf (file, "\n\n");
580 /* Dumps the data dependence relations DDRS in FILE. */
582 static void
583 dump_ddrs (FILE *file, vec<ddr_p> ddrs)
585 unsigned int i;
586 struct data_dependence_relation *ddr;
588 FOR_EACH_VEC_ELT (ddrs, i, ddr)
589 dump_data_dependence_relation (file, ddr);
591 fprintf (file, "\n\n");
594 DEBUG_FUNCTION void
595 debug_ddrs (vec<ddr_p> ddrs)
597 dump_ddrs (stderr, ddrs);
600 /* Helper function for split_constant_offset. Expresses OP0 CODE OP1
601 (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
602 constant of type ssizetype, and returns true. If we cannot do this
603 with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
604 is returned. */
606 static bool
607 split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
608 tree *var, tree *off)
610 tree var0, var1;
611 tree off0, off1;
612 enum tree_code ocode = code;
614 *var = NULL_TREE;
615 *off = NULL_TREE;
617 switch (code)
619 case INTEGER_CST:
620 *var = build_int_cst (type, 0);
621 *off = fold_convert (ssizetype, op0);
622 return true;
624 case POINTER_PLUS_EXPR:
625 ocode = PLUS_EXPR;
626 /* FALLTHROUGH */
627 case PLUS_EXPR:
628 case MINUS_EXPR:
629 split_constant_offset (op0, &var0, &off0);
630 split_constant_offset (op1, &var1, &off1);
631 *var = fold_build2 (code, type, var0, var1);
632 *off = size_binop (ocode, off0, off1);
633 return true;
635 case MULT_EXPR:
636 if (TREE_CODE (op1) != INTEGER_CST)
637 return false;
639 split_constant_offset (op0, &var0, &off0);
640 *var = fold_build2 (MULT_EXPR, type, var0, op1);
641 *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
642 return true;
644 case ADDR_EXPR:
646 tree base, poffset;
647 HOST_WIDE_INT pbitsize, pbitpos;
648 machine_mode pmode;
649 int punsignedp, pvolatilep;
651 op0 = TREE_OPERAND (op0, 0);
652 base = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset,
653 &pmode, &punsignedp, &pvolatilep, false);
655 if (pbitpos % BITS_PER_UNIT != 0)
656 return false;
657 base = build_fold_addr_expr (base);
658 off0 = ssize_int (pbitpos / BITS_PER_UNIT);
660 if (poffset)
662 split_constant_offset (poffset, &poffset, &off1);
663 off0 = size_binop (PLUS_EXPR, off0, off1);
664 if (POINTER_TYPE_P (TREE_TYPE (base)))
665 base = fold_build_pointer_plus (base, poffset);
666 else
667 base = fold_build2 (PLUS_EXPR, TREE_TYPE (base), base,
668 fold_convert (TREE_TYPE (base), poffset));
671 var0 = fold_convert (type, base);
673 /* If variable length types are involved, punt, otherwise casts
674 might be converted into ARRAY_REFs in gimplify_conversion.
675 To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
676 possibly no longer appears in current GIMPLE, might resurface.
677 This perhaps could run
678 if (CONVERT_EXPR_P (var0))
680 gimplify_conversion (&var0);
681 // Attempt to fill in any within var0 found ARRAY_REF's
682 // element size from corresponding op embedded ARRAY_REF,
683 // if unsuccessful, just punt.
684 } */
685 while (POINTER_TYPE_P (type))
686 type = TREE_TYPE (type);
687 if (int_size_in_bytes (type) < 0)
688 return false;
690 *var = var0;
691 *off = off0;
692 return true;
695 case SSA_NAME:
697 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
698 return false;
700 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
701 enum tree_code subcode;
703 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
704 return false;
706 var0 = gimple_assign_rhs1 (def_stmt);
707 subcode = gimple_assign_rhs_code (def_stmt);
708 var1 = gimple_assign_rhs2 (def_stmt);
710 return split_constant_offset_1 (type, var0, subcode, var1, var, off);
712 CASE_CONVERT:
714 /* We must not introduce undefined overflow, and we must not change the value.
715 Hence we're okay if the inner type doesn't overflow to start with
716 (pointer or signed), the outer type also is an integer or pointer
717 and the outer precision is at least as large as the inner. */
718 tree itype = TREE_TYPE (op0);
719 if ((POINTER_TYPE_P (itype)
720 || (INTEGRAL_TYPE_P (itype) && TYPE_OVERFLOW_UNDEFINED (itype)))
721 && TYPE_PRECISION (type) >= TYPE_PRECISION (itype)
722 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)))
724 split_constant_offset (op0, &var0, off);
725 *var = fold_convert (type, var0);
726 return true;
728 return false;
731 default:
732 return false;
736 /* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
737 will be ssizetype. */
739 void
740 split_constant_offset (tree exp, tree *var, tree *off)
742 tree type = TREE_TYPE (exp), otype, op0, op1, e, o;
743 enum tree_code code;
745 *var = exp;
746 *off = ssize_int (0);
747 STRIP_NOPS (exp);
749 if (tree_is_chrec (exp)
750 || get_gimple_rhs_class (TREE_CODE (exp)) == GIMPLE_TERNARY_RHS)
751 return;
753 otype = TREE_TYPE (exp);
754 code = TREE_CODE (exp);
755 extract_ops_from_tree (exp, &code, &op0, &op1);
756 if (split_constant_offset_1 (otype, op0, code, op1, &e, &o))
758 *var = fold_convert (type, e);
759 *off = o;
763 /* Returns the address ADDR of an object in a canonical shape (without nop
764 casts, and with type of pointer to the object). */
766 static tree
767 canonicalize_base_object_address (tree addr)
769 tree orig = addr;
771 STRIP_NOPS (addr);
773 /* The base address may be obtained by casting from integer, in that case
774 keep the cast. */
775 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
776 return orig;
778 if (TREE_CODE (addr) != ADDR_EXPR)
779 return addr;
781 return build_fold_addr_expr (TREE_OPERAND (addr, 0));
784 /* Analyzes the behavior of the memory reference DR in the innermost loop or
785 basic block that contains it. Returns true if analysis succeed or false
786 otherwise. */
788 bool
789 dr_analyze_innermost (struct data_reference *dr, struct loop *nest)
791 gimple stmt = DR_STMT (dr);
792 struct loop *loop = loop_containing_stmt (stmt);
793 tree ref = DR_REF (dr);
794 HOST_WIDE_INT pbitsize, pbitpos;
795 tree base, poffset;
796 machine_mode pmode;
797 int punsignedp, pvolatilep;
798 affine_iv base_iv, offset_iv;
799 tree init, dinit, step;
800 bool in_loop = (loop && loop->num);
802 if (dump_file && (dump_flags & TDF_DETAILS))
803 fprintf (dump_file, "analyze_innermost: ");
805 base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset,
806 &pmode, &punsignedp, &pvolatilep, false);
807 gcc_assert (base != NULL_TREE);
809 if (pbitpos % BITS_PER_UNIT != 0)
811 if (dump_file && (dump_flags & TDF_DETAILS))
812 fprintf (dump_file, "failed: bit offset alignment.\n");
813 return false;
816 if (TREE_CODE (base) == MEM_REF)
818 if (!integer_zerop (TREE_OPERAND (base, 1)))
820 offset_int moff = mem_ref_offset (base);
821 tree mofft = wide_int_to_tree (sizetype, moff);
822 if (!poffset)
823 poffset = mofft;
824 else
825 poffset = size_binop (PLUS_EXPR, poffset, mofft);
827 base = TREE_OPERAND (base, 0);
829 else
830 base = build_fold_addr_expr (base);
832 if (in_loop)
834 if (!simple_iv (loop, loop_containing_stmt (stmt), base, &base_iv,
835 nest ? true : false))
837 if (nest)
839 if (dump_file && (dump_flags & TDF_DETAILS))
840 fprintf (dump_file, "failed: evolution of base is not"
841 " affine.\n");
842 return false;
844 else
846 base_iv.base = base;
847 base_iv.step = ssize_int (0);
848 base_iv.no_overflow = true;
852 else
854 base_iv.base = base;
855 base_iv.step = ssize_int (0);
856 base_iv.no_overflow = true;
859 if (!poffset)
861 offset_iv.base = ssize_int (0);
862 offset_iv.step = ssize_int (0);
864 else
866 if (!in_loop)
868 offset_iv.base = poffset;
869 offset_iv.step = ssize_int (0);
871 else if (!simple_iv (loop, loop_containing_stmt (stmt),
872 poffset, &offset_iv,
873 nest ? true : false))
875 if (nest)
877 if (dump_file && (dump_flags & TDF_DETAILS))
878 fprintf (dump_file, "failed: evolution of offset is not"
879 " affine.\n");
880 return false;
882 else
884 offset_iv.base = poffset;
885 offset_iv.step = ssize_int (0);
890 init = ssize_int (pbitpos / BITS_PER_UNIT);
891 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
892 init = size_binop (PLUS_EXPR, init, dinit);
893 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
894 init = size_binop (PLUS_EXPR, init, dinit);
896 step = size_binop (PLUS_EXPR,
897 fold_convert (ssizetype, base_iv.step),
898 fold_convert (ssizetype, offset_iv.step));
900 DR_BASE_ADDRESS (dr) = canonicalize_base_object_address (base_iv.base);
902 DR_OFFSET (dr) = fold_convert (ssizetype, offset_iv.base);
903 DR_INIT (dr) = init;
904 DR_STEP (dr) = step;
906 DR_ALIGNED_TO (dr) = size_int (highest_pow2_factor (offset_iv.base));
908 if (dump_file && (dump_flags & TDF_DETAILS))
909 fprintf (dump_file, "success.\n");
911 return true;
914 /* Determines the base object and the list of indices of memory reference
915 DR, analyzed in LOOP and instantiated in loop nest NEST. */
917 static void
918 dr_analyze_indices (struct data_reference *dr, loop_p nest, loop_p loop)
920 vec<tree> access_fns = vNULL;
921 tree ref, op;
922 tree base, off, access_fn;
923 basic_block before_loop;
925 /* If analyzing a basic-block there are no indices to analyze
926 and thus no access functions. */
927 if (!nest)
929 DR_BASE_OBJECT (dr) = DR_REF (dr);
930 DR_ACCESS_FNS (dr).create (0);
931 return;
934 ref = DR_REF (dr);
935 before_loop = block_before_loop (nest);
937 /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
938 into a two element array with a constant index. The base is
939 then just the immediate underlying object. */
940 if (TREE_CODE (ref) == REALPART_EXPR)
942 ref = TREE_OPERAND (ref, 0);
943 access_fns.safe_push (integer_zero_node);
945 else if (TREE_CODE (ref) == IMAGPART_EXPR)
947 ref = TREE_OPERAND (ref, 0);
948 access_fns.safe_push (integer_one_node);
951 /* Analyze access functions of dimensions we know to be independent. */
952 while (handled_component_p (ref))
954 if (TREE_CODE (ref) == ARRAY_REF)
956 op = TREE_OPERAND (ref, 1);
957 access_fn = analyze_scalar_evolution (loop, op);
958 access_fn = instantiate_scev (before_loop, loop, access_fn);
959 access_fns.safe_push (access_fn);
961 else if (TREE_CODE (ref) == COMPONENT_REF
962 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
964 /* For COMPONENT_REFs of records (but not unions!) use the
965 FIELD_DECL offset as constant access function so we can
966 disambiguate a[i].f1 and a[i].f2. */
967 tree off = component_ref_field_offset (ref);
968 off = size_binop (PLUS_EXPR,
969 size_binop (MULT_EXPR,
970 fold_convert (bitsizetype, off),
971 bitsize_int (BITS_PER_UNIT)),
972 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
973 access_fns.safe_push (off);
975 else
976 /* If we have an unhandled component we could not translate
977 to an access function stop analyzing. We have determined
978 our base object in this case. */
979 break;
981 ref = TREE_OPERAND (ref, 0);
984 /* If the address operand of a MEM_REF base has an evolution in the
985 analyzed nest, add it as an additional independent access-function. */
986 if (TREE_CODE (ref) == MEM_REF)
988 op = TREE_OPERAND (ref, 0);
989 access_fn = analyze_scalar_evolution (loop, op);
990 access_fn = instantiate_scev (before_loop, loop, access_fn);
991 if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
993 tree orig_type;
994 tree memoff = TREE_OPERAND (ref, 1);
995 base = initial_condition (access_fn);
996 orig_type = TREE_TYPE (base);
997 STRIP_USELESS_TYPE_CONVERSION (base);
998 split_constant_offset (base, &base, &off);
999 STRIP_USELESS_TYPE_CONVERSION (base);
1000 /* Fold the MEM_REF offset into the evolutions initial
1001 value to make more bases comparable. */
1002 if (!integer_zerop (memoff))
1004 off = size_binop (PLUS_EXPR, off,
1005 fold_convert (ssizetype, memoff));
1006 memoff = build_int_cst (TREE_TYPE (memoff), 0);
1008 /* Adjust the offset so it is a multiple of the access type
1009 size and thus we separate bases that can possibly be used
1010 to produce partial overlaps (which the access_fn machinery
1011 cannot handle). */
1012 wide_int rem;
1013 if (TYPE_SIZE_UNIT (TREE_TYPE (ref))
1014 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref))) == INTEGER_CST
1015 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (ref))))
1016 rem = wi::mod_trunc (off, TYPE_SIZE_UNIT (TREE_TYPE (ref)), SIGNED);
1017 else
1018 /* If we can't compute the remainder simply force the initial
1019 condition to zero. */
1020 rem = off;
1021 off = wide_int_to_tree (ssizetype, wi::sub (off, rem));
1022 memoff = wide_int_to_tree (TREE_TYPE (memoff), rem);
1023 /* And finally replace the initial condition. */
1024 access_fn = chrec_replace_initial_condition
1025 (access_fn, fold_convert (orig_type, off));
1026 /* ??? This is still not a suitable base object for
1027 dr_may_alias_p - the base object needs to be an
1028 access that covers the object as whole. With
1029 an evolution in the pointer this cannot be
1030 guaranteed.
1031 As a band-aid, mark the access so we can special-case
1032 it in dr_may_alias_p. */
1033 tree old = ref;
1034 ref = fold_build2_loc (EXPR_LOCATION (ref),
1035 MEM_REF, TREE_TYPE (ref),
1036 base, memoff);
1037 MR_DEPENDENCE_CLIQUE (ref) = MR_DEPENDENCE_CLIQUE (old);
1038 MR_DEPENDENCE_BASE (ref) = MR_DEPENDENCE_BASE (old);
1039 access_fns.safe_push (access_fn);
1042 else if (DECL_P (ref))
1044 /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
1045 ref = build2 (MEM_REF, TREE_TYPE (ref),
1046 build_fold_addr_expr (ref),
1047 build_int_cst (reference_alias_ptr_type (ref), 0));
1050 DR_BASE_OBJECT (dr) = ref;
1051 DR_ACCESS_FNS (dr) = access_fns;
1054 /* Extracts the alias analysis information from the memory reference DR. */
1056 static void
1057 dr_analyze_alias (struct data_reference *dr)
1059 tree ref = DR_REF (dr);
1060 tree base = get_base_address (ref), addr;
1062 if (INDIRECT_REF_P (base)
1063 || TREE_CODE (base) == MEM_REF)
1065 addr = TREE_OPERAND (base, 0);
1066 if (TREE_CODE (addr) == SSA_NAME)
1067 DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
1071 /* Frees data reference DR. */
1073 void
1074 free_data_ref (data_reference_p dr)
1076 DR_ACCESS_FNS (dr).release ();
1077 free (dr);
1080 /* Analyzes memory reference MEMREF accessed in STMT. The reference
1081 is read if IS_READ is true, write otherwise. Returns the
1082 data_reference description of MEMREF. NEST is the outermost loop
1083 in which the reference should be instantiated, LOOP is the loop in
1084 which the data reference should be analyzed. */
1086 struct data_reference *
1087 create_data_ref (loop_p nest, loop_p loop, tree memref, gimple stmt,
1088 bool is_read)
1090 struct data_reference *dr;
1092 if (dump_file && (dump_flags & TDF_DETAILS))
1094 fprintf (dump_file, "Creating dr for ");
1095 print_generic_expr (dump_file, memref, TDF_SLIM);
1096 fprintf (dump_file, "\n");
1099 dr = XCNEW (struct data_reference);
1100 DR_STMT (dr) = stmt;
1101 DR_REF (dr) = memref;
1102 DR_IS_READ (dr) = is_read;
1104 dr_analyze_innermost (dr, nest);
1105 dr_analyze_indices (dr, nest, loop);
1106 dr_analyze_alias (dr);
1108 if (dump_file && (dump_flags & TDF_DETAILS))
1110 unsigned i;
1111 fprintf (dump_file, "\tbase_address: ");
1112 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1113 fprintf (dump_file, "\n\toffset from base address: ");
1114 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1115 fprintf (dump_file, "\n\tconstant offset from base address: ");
1116 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1117 fprintf (dump_file, "\n\tstep: ");
1118 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1119 fprintf (dump_file, "\n\taligned to: ");
1120 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
1121 fprintf (dump_file, "\n\tbase_object: ");
1122 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1123 fprintf (dump_file, "\n");
1124 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
1126 fprintf (dump_file, "\tAccess function %d: ", i);
1127 print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
1131 return dr;
1134 /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
1135 expressions. */
1136 static bool
1137 dr_equal_offsets_p1 (tree offset1, tree offset2)
1139 bool res;
1141 STRIP_NOPS (offset1);
1142 STRIP_NOPS (offset2);
1144 if (offset1 == offset2)
1145 return true;
1147 if (TREE_CODE (offset1) != TREE_CODE (offset2)
1148 || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
1149 return false;
1151 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
1152 TREE_OPERAND (offset2, 0));
1154 if (!res || !BINARY_CLASS_P (offset1))
1155 return res;
1157 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
1158 TREE_OPERAND (offset2, 1));
1160 return res;
1163 /* Check if DRA and DRB have equal offsets. */
1164 bool
1165 dr_equal_offsets_p (struct data_reference *dra,
1166 struct data_reference *drb)
1168 tree offset1, offset2;
1170 offset1 = DR_OFFSET (dra);
1171 offset2 = DR_OFFSET (drb);
1173 return dr_equal_offsets_p1 (offset1, offset2);
1176 /* Returns true if FNA == FNB. */
1178 static bool
1179 affine_function_equal_p (affine_fn fna, affine_fn fnb)
1181 unsigned i, n = fna.length ();
1183 if (n != fnb.length ())
1184 return false;
1186 for (i = 0; i < n; i++)
1187 if (!operand_equal_p (fna[i], fnb[i], 0))
1188 return false;
1190 return true;
1193 /* If all the functions in CF are the same, returns one of them,
1194 otherwise returns NULL. */
1196 static affine_fn
1197 common_affine_function (conflict_function *cf)
1199 unsigned i;
1200 affine_fn comm;
1202 if (!CF_NONTRIVIAL_P (cf))
1203 return affine_fn ();
1205 comm = cf->fns[0];
1207 for (i = 1; i < cf->n; i++)
1208 if (!affine_function_equal_p (comm, cf->fns[i]))
1209 return affine_fn ();
1211 return comm;
1214 /* Returns the base of the affine function FN. */
1216 static tree
1217 affine_function_base (affine_fn fn)
1219 return fn[0];
1222 /* Returns true if FN is a constant. */
1224 static bool
1225 affine_function_constant_p (affine_fn fn)
1227 unsigned i;
1228 tree coef;
1230 for (i = 1; fn.iterate (i, &coef); i++)
1231 if (!integer_zerop (coef))
1232 return false;
1234 return true;
1237 /* Returns true if FN is the zero constant function. */
1239 static bool
1240 affine_function_zero_p (affine_fn fn)
1242 return (integer_zerop (affine_function_base (fn))
1243 && affine_function_constant_p (fn));
1246 /* Returns a signed integer type with the largest precision from TA
1247 and TB. */
1249 static tree
1250 signed_type_for_types (tree ta, tree tb)
1252 if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
1253 return signed_type_for (ta);
1254 else
1255 return signed_type_for (tb);
1258 /* Applies operation OP on affine functions FNA and FNB, and returns the
1259 result. */
1261 static affine_fn
1262 affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
1264 unsigned i, n, m;
1265 affine_fn ret;
1266 tree coef;
1268 if (fnb.length () > fna.length ())
1270 n = fna.length ();
1271 m = fnb.length ();
1273 else
1275 n = fnb.length ();
1276 m = fna.length ();
1279 ret.create (m);
1280 for (i = 0; i < n; i++)
1282 tree type = signed_type_for_types (TREE_TYPE (fna[i]),
1283 TREE_TYPE (fnb[i]));
1284 ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
1287 for (; fna.iterate (i, &coef); i++)
1288 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1289 coef, integer_zero_node));
1290 for (; fnb.iterate (i, &coef); i++)
1291 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1292 integer_zero_node, coef));
1294 return ret;
1297 /* Returns the sum of affine functions FNA and FNB. */
1299 static affine_fn
1300 affine_fn_plus (affine_fn fna, affine_fn fnb)
1302 return affine_fn_op (PLUS_EXPR, fna, fnb);
1305 /* Returns the difference of affine functions FNA and FNB. */
1307 static affine_fn
1308 affine_fn_minus (affine_fn fna, affine_fn fnb)
1310 return affine_fn_op (MINUS_EXPR, fna, fnb);
1313 /* Frees affine function FN. */
1315 static void
1316 affine_fn_free (affine_fn fn)
1318 fn.release ();
1321 /* Determine for each subscript in the data dependence relation DDR
1322 the distance. */
1324 static void
1325 compute_subscript_distance (struct data_dependence_relation *ddr)
1327 conflict_function *cf_a, *cf_b;
1328 affine_fn fn_a, fn_b, diff;
1330 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1332 unsigned int i;
1334 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1336 struct subscript *subscript;
1338 subscript = DDR_SUBSCRIPT (ddr, i);
1339 cf_a = SUB_CONFLICTS_IN_A (subscript);
1340 cf_b = SUB_CONFLICTS_IN_B (subscript);
1342 fn_a = common_affine_function (cf_a);
1343 fn_b = common_affine_function (cf_b);
1344 if (!fn_a.exists () || !fn_b.exists ())
1346 SUB_DISTANCE (subscript) = chrec_dont_know;
1347 return;
1349 diff = affine_fn_minus (fn_a, fn_b);
1351 if (affine_function_constant_p (diff))
1352 SUB_DISTANCE (subscript) = affine_function_base (diff);
1353 else
1354 SUB_DISTANCE (subscript) = chrec_dont_know;
1356 affine_fn_free (diff);
1361 /* Returns the conflict function for "unknown". */
1363 static conflict_function *
1364 conflict_fn_not_known (void)
1366 conflict_function *fn = XCNEW (conflict_function);
1367 fn->n = NOT_KNOWN;
1369 return fn;
1372 /* Returns the conflict function for "independent". */
1374 static conflict_function *
1375 conflict_fn_no_dependence (void)
1377 conflict_function *fn = XCNEW (conflict_function);
1378 fn->n = NO_DEPENDENCE;
1380 return fn;
1383 /* Returns true if the address of OBJ is invariant in LOOP. */
1385 static bool
1386 object_address_invariant_in_loop_p (const struct loop *loop, const_tree obj)
1388 while (handled_component_p (obj))
1390 if (TREE_CODE (obj) == ARRAY_REF)
1392 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1393 need to check the stride and the lower bound of the reference. */
1394 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1395 loop->num)
1396 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 3),
1397 loop->num))
1398 return false;
1400 else if (TREE_CODE (obj) == COMPONENT_REF)
1402 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1403 loop->num))
1404 return false;
1406 obj = TREE_OPERAND (obj, 0);
1409 if (!INDIRECT_REF_P (obj)
1410 && TREE_CODE (obj) != MEM_REF)
1411 return true;
1413 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
1414 loop->num);
1417 /* Returns false if we can prove that data references A and B do not alias,
1418 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
1419 considered. */
1421 bool
1422 dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
1423 bool loop_nest)
1425 tree addr_a = DR_BASE_OBJECT (a);
1426 tree addr_b = DR_BASE_OBJECT (b);
1428 /* If we are not processing a loop nest but scalar code we
1429 do not need to care about possible cross-iteration dependences
1430 and thus can process the full original reference. Do so,
1431 similar to how loop invariant motion applies extra offset-based
1432 disambiguation. */
1433 if (!loop_nest)
1435 aff_tree off1, off2;
1436 widest_int size1, size2;
1437 get_inner_reference_aff (DR_REF (a), &off1, &size1);
1438 get_inner_reference_aff (DR_REF (b), &off2, &size2);
1439 aff_combination_scale (&off1, -1);
1440 aff_combination_add (&off2, &off1);
1441 if (aff_comb_cannot_overlap_p (&off2, size1, size2))
1442 return false;
1445 if ((TREE_CODE (addr_a) == MEM_REF || TREE_CODE (addr_a) == TARGET_MEM_REF)
1446 && (TREE_CODE (addr_b) == MEM_REF || TREE_CODE (addr_b) == TARGET_MEM_REF)
1447 && MR_DEPENDENCE_CLIQUE (addr_a) == MR_DEPENDENCE_CLIQUE (addr_b)
1448 && MR_DEPENDENCE_BASE (addr_a) != MR_DEPENDENCE_BASE (addr_b))
1449 return false;
1451 /* If we had an evolution in a pointer-based MEM_REF BASE_OBJECT we
1452 do not know the size of the base-object. So we cannot do any
1453 offset/overlap based analysis but have to rely on points-to
1454 information only. */
1455 if (TREE_CODE (addr_a) == MEM_REF
1456 && TREE_CODE (TREE_OPERAND (addr_a, 0)) == SSA_NAME)
1458 /* For true dependences we can apply TBAA. */
1459 if (flag_strict_aliasing
1460 && DR_IS_WRITE (a) && DR_IS_READ (b)
1461 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1462 get_alias_set (DR_REF (b))))
1463 return false;
1464 if (TREE_CODE (addr_b) == MEM_REF)
1465 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1466 TREE_OPERAND (addr_b, 0));
1467 else
1468 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1469 build_fold_addr_expr (addr_b));
1471 else if (TREE_CODE (addr_b) == MEM_REF
1472 && TREE_CODE (TREE_OPERAND (addr_b, 0)) == SSA_NAME)
1474 /* For true dependences we can apply TBAA. */
1475 if (flag_strict_aliasing
1476 && DR_IS_WRITE (a) && DR_IS_READ (b)
1477 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1478 get_alias_set (DR_REF (b))))
1479 return false;
1480 if (TREE_CODE (addr_a) == MEM_REF)
1481 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1482 TREE_OPERAND (addr_b, 0));
1483 else
1484 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
1485 TREE_OPERAND (addr_b, 0));
1488 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1489 that is being subsetted in the loop nest. */
1490 if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
1491 return refs_output_dependent_p (addr_a, addr_b);
1492 else if (DR_IS_READ (a) && DR_IS_WRITE (b))
1493 return refs_anti_dependent_p (addr_a, addr_b);
1494 return refs_may_alias_p (addr_a, addr_b);
1497 /* Initialize a data dependence relation between data accesses A and
1498 B. NB_LOOPS is the number of loops surrounding the references: the
1499 size of the classic distance/direction vectors. */
1501 struct data_dependence_relation *
1502 initialize_data_dependence_relation (struct data_reference *a,
1503 struct data_reference *b,
1504 vec<loop_p> loop_nest)
1506 struct data_dependence_relation *res;
1507 unsigned int i;
1509 res = XNEW (struct data_dependence_relation);
1510 DDR_A (res) = a;
1511 DDR_B (res) = b;
1512 DDR_LOOP_NEST (res).create (0);
1513 DDR_REVERSED_P (res) = false;
1514 DDR_SUBSCRIPTS (res).create (0);
1515 DDR_DIR_VECTS (res).create (0);
1516 DDR_DIST_VECTS (res).create (0);
1518 if (a == NULL || b == NULL)
1520 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1521 return res;
1524 /* If the data references do not alias, then they are independent. */
1525 if (!dr_may_alias_p (a, b, loop_nest.exists ()))
1527 DDR_ARE_DEPENDENT (res) = chrec_known;
1528 return res;
1531 /* The case where the references are exactly the same. */
1532 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
1534 if (loop_nest.exists ()
1535 && !object_address_invariant_in_loop_p (loop_nest[0],
1536 DR_BASE_OBJECT (a)))
1538 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1539 return res;
1541 DDR_AFFINE_P (res) = true;
1542 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1543 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1544 DDR_LOOP_NEST (res) = loop_nest;
1545 DDR_INNER_LOOP (res) = 0;
1546 DDR_SELF_REFERENCE (res) = true;
1547 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1549 struct subscript *subscript;
1551 subscript = XNEW (struct subscript);
1552 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1553 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1554 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1555 SUB_DISTANCE (subscript) = chrec_dont_know;
1556 DDR_SUBSCRIPTS (res).safe_push (subscript);
1558 return res;
1561 /* If the references do not access the same object, we do not know
1562 whether they alias or not. */
1563 if (!operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0))
1565 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1566 return res;
1569 /* If the base of the object is not invariant in the loop nest, we cannot
1570 analyze it. TODO -- in fact, it would suffice to record that there may
1571 be arbitrary dependences in the loops where the base object varies. */
1572 if (loop_nest.exists ()
1573 && !object_address_invariant_in_loop_p (loop_nest[0],
1574 DR_BASE_OBJECT (a)))
1576 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1577 return res;
1580 /* If the number of dimensions of the access to not agree we can have
1581 a pointer access to a component of the array element type and an
1582 array access while the base-objects are still the same. Punt. */
1583 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1585 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1586 return res;
1589 DDR_AFFINE_P (res) = true;
1590 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1591 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1592 DDR_LOOP_NEST (res) = loop_nest;
1593 DDR_INNER_LOOP (res) = 0;
1594 DDR_SELF_REFERENCE (res) = false;
1596 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1598 struct subscript *subscript;
1600 subscript = XNEW (struct subscript);
1601 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1602 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1603 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1604 SUB_DISTANCE (subscript) = chrec_dont_know;
1605 DDR_SUBSCRIPTS (res).safe_push (subscript);
1608 return res;
1611 /* Frees memory used by the conflict function F. */
1613 static void
1614 free_conflict_function (conflict_function *f)
1616 unsigned i;
1618 if (CF_NONTRIVIAL_P (f))
1620 for (i = 0; i < f->n; i++)
1621 affine_fn_free (f->fns[i]);
1623 free (f);
1626 /* Frees memory used by SUBSCRIPTS. */
1628 static void
1629 free_subscripts (vec<subscript_p> subscripts)
1631 unsigned i;
1632 subscript_p s;
1634 FOR_EACH_VEC_ELT (subscripts, i, s)
1636 free_conflict_function (s->conflicting_iterations_in_a);
1637 free_conflict_function (s->conflicting_iterations_in_b);
1638 free (s);
1640 subscripts.release ();
1643 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1644 description. */
1646 static inline void
1647 finalize_ddr_dependent (struct data_dependence_relation *ddr,
1648 tree chrec)
1650 DDR_ARE_DEPENDENT (ddr) = chrec;
1651 free_subscripts (DDR_SUBSCRIPTS (ddr));
1652 DDR_SUBSCRIPTS (ddr).create (0);
1655 /* The dependence relation DDR cannot be represented by a distance
1656 vector. */
1658 static inline void
1659 non_affine_dependence_relation (struct data_dependence_relation *ddr)
1661 if (dump_file && (dump_flags & TDF_DETAILS))
1662 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
1664 DDR_AFFINE_P (ddr) = false;
1669 /* This section contains the classic Banerjee tests. */
1671 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1672 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1674 static inline bool
1675 ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1677 return (evolution_function_is_constant_p (chrec_a)
1678 && evolution_function_is_constant_p (chrec_b));
1681 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
1682 variable, i.e., if the SIV (Single Index Variable) test is true. */
1684 static bool
1685 siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1687 if ((evolution_function_is_constant_p (chrec_a)
1688 && evolution_function_is_univariate_p (chrec_b))
1689 || (evolution_function_is_constant_p (chrec_b)
1690 && evolution_function_is_univariate_p (chrec_a)))
1691 return true;
1693 if (evolution_function_is_univariate_p (chrec_a)
1694 && evolution_function_is_univariate_p (chrec_b))
1696 switch (TREE_CODE (chrec_a))
1698 case POLYNOMIAL_CHREC:
1699 switch (TREE_CODE (chrec_b))
1701 case POLYNOMIAL_CHREC:
1702 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
1703 return false;
1705 default:
1706 return true;
1709 default:
1710 return true;
1714 return false;
1717 /* Creates a conflict function with N dimensions. The affine functions
1718 in each dimension follow. */
1720 static conflict_function *
1721 conflict_fn (unsigned n, ...)
1723 unsigned i;
1724 conflict_function *ret = XCNEW (conflict_function);
1725 va_list ap;
1727 gcc_assert (0 < n && n <= MAX_DIM);
1728 va_start (ap, n);
1730 ret->n = n;
1731 for (i = 0; i < n; i++)
1732 ret->fns[i] = va_arg (ap, affine_fn);
1733 va_end (ap);
1735 return ret;
1738 /* Returns constant affine function with value CST. */
1740 static affine_fn
1741 affine_fn_cst (tree cst)
1743 affine_fn fn;
1744 fn.create (1);
1745 fn.quick_push (cst);
1746 return fn;
1749 /* Returns affine function with single variable, CST + COEF * x_DIM. */
1751 static affine_fn
1752 affine_fn_univar (tree cst, unsigned dim, tree coef)
1754 affine_fn fn;
1755 fn.create (dim + 1);
1756 unsigned i;
1758 gcc_assert (dim > 0);
1759 fn.quick_push (cst);
1760 for (i = 1; i < dim; i++)
1761 fn.quick_push (integer_zero_node);
1762 fn.quick_push (coef);
1763 return fn;
1766 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1767 *OVERLAPS_B are initialized to the functions that describe the
1768 relation between the elements accessed twice by CHREC_A and
1769 CHREC_B. For k >= 0, the following property is verified:
1771 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1773 static void
1774 analyze_ziv_subscript (tree chrec_a,
1775 tree chrec_b,
1776 conflict_function **overlaps_a,
1777 conflict_function **overlaps_b,
1778 tree *last_conflicts)
1780 tree type, difference;
1781 dependence_stats.num_ziv++;
1783 if (dump_file && (dump_flags & TDF_DETAILS))
1784 fprintf (dump_file, "(analyze_ziv_subscript \n");
1786 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1787 chrec_a = chrec_convert (type, chrec_a, NULL);
1788 chrec_b = chrec_convert (type, chrec_b, NULL);
1789 difference = chrec_fold_minus (type, chrec_a, chrec_b);
1791 switch (TREE_CODE (difference))
1793 case INTEGER_CST:
1794 if (integer_zerop (difference))
1796 /* The difference is equal to zero: the accessed index
1797 overlaps for each iteration in the loop. */
1798 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1799 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1800 *last_conflicts = chrec_dont_know;
1801 dependence_stats.num_ziv_dependent++;
1803 else
1805 /* The accesses do not overlap. */
1806 *overlaps_a = conflict_fn_no_dependence ();
1807 *overlaps_b = conflict_fn_no_dependence ();
1808 *last_conflicts = integer_zero_node;
1809 dependence_stats.num_ziv_independent++;
1811 break;
1813 default:
1814 /* We're not sure whether the indexes overlap. For the moment,
1815 conservatively answer "don't know". */
1816 if (dump_file && (dump_flags & TDF_DETAILS))
1817 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
1819 *overlaps_a = conflict_fn_not_known ();
1820 *overlaps_b = conflict_fn_not_known ();
1821 *last_conflicts = chrec_dont_know;
1822 dependence_stats.num_ziv_unimplemented++;
1823 break;
1826 if (dump_file && (dump_flags & TDF_DETAILS))
1827 fprintf (dump_file, ")\n");
1830 /* Similar to max_stmt_executions_int, but returns the bound as a tree,
1831 and only if it fits to the int type. If this is not the case, or the
1832 bound on the number of iterations of LOOP could not be derived, returns
1833 chrec_dont_know. */
1835 static tree
1836 max_stmt_executions_tree (struct loop *loop)
1838 widest_int nit;
1840 if (!max_stmt_executions (loop, &nit))
1841 return chrec_dont_know;
1843 if (!wi::fits_to_tree_p (nit, unsigned_type_node))
1844 return chrec_dont_know;
1846 return wide_int_to_tree (unsigned_type_node, nit);
1849 /* Determine whether the CHREC is always positive/negative. If the expression
1850 cannot be statically analyzed, return false, otherwise set the answer into
1851 VALUE. */
1853 static bool
1854 chrec_is_positive (tree chrec, bool *value)
1856 bool value0, value1, value2;
1857 tree end_value, nb_iter;
1859 switch (TREE_CODE (chrec))
1861 case POLYNOMIAL_CHREC:
1862 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
1863 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
1864 return false;
1866 /* FIXME -- overflows. */
1867 if (value0 == value1)
1869 *value = value0;
1870 return true;
1873 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1874 and the proof consists in showing that the sign never
1875 changes during the execution of the loop, from 0 to
1876 loop->nb_iterations. */
1877 if (!evolution_function_is_affine_p (chrec))
1878 return false;
1880 nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
1881 if (chrec_contains_undetermined (nb_iter))
1882 return false;
1884 #if 0
1885 /* TODO -- If the test is after the exit, we may decrease the number of
1886 iterations by one. */
1887 if (after_exit)
1888 nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
1889 #endif
1891 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
1893 if (!chrec_is_positive (end_value, &value2))
1894 return false;
1896 *value = value0;
1897 return value0 == value1;
1899 case INTEGER_CST:
1900 switch (tree_int_cst_sgn (chrec))
1902 case -1:
1903 *value = false;
1904 break;
1905 case 1:
1906 *value = true;
1907 break;
1908 default:
1909 return false;
1911 return true;
1913 default:
1914 return false;
1919 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1920 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1921 *OVERLAPS_B are initialized to the functions that describe the
1922 relation between the elements accessed twice by CHREC_A and
1923 CHREC_B. For k >= 0, the following property is verified:
1925 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1927 static void
1928 analyze_siv_subscript_cst_affine (tree chrec_a,
1929 tree chrec_b,
1930 conflict_function **overlaps_a,
1931 conflict_function **overlaps_b,
1932 tree *last_conflicts)
1934 bool value0, value1, value2;
1935 tree type, difference, tmp;
1937 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1938 chrec_a = chrec_convert (type, chrec_a, NULL);
1939 chrec_b = chrec_convert (type, chrec_b, NULL);
1940 difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
1942 /* Special case overlap in the first iteration. */
1943 if (integer_zerop (difference))
1945 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1946 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1947 *last_conflicts = integer_one_node;
1948 return;
1951 if (!chrec_is_positive (initial_condition (difference), &value0))
1953 if (dump_file && (dump_flags & TDF_DETAILS))
1954 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
1956 dependence_stats.num_siv_unimplemented++;
1957 *overlaps_a = conflict_fn_not_known ();
1958 *overlaps_b = conflict_fn_not_known ();
1959 *last_conflicts = chrec_dont_know;
1960 return;
1962 else
1964 if (value0 == false)
1966 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
1968 if (dump_file && (dump_flags & TDF_DETAILS))
1969 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1971 *overlaps_a = conflict_fn_not_known ();
1972 *overlaps_b = conflict_fn_not_known ();
1973 *last_conflicts = chrec_dont_know;
1974 dependence_stats.num_siv_unimplemented++;
1975 return;
1977 else
1979 if (value1 == true)
1981 /* Example:
1982 chrec_a = 12
1983 chrec_b = {10, +, 1}
1986 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1988 HOST_WIDE_INT numiter;
1989 struct loop *loop = get_chrec_loop (chrec_b);
1991 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1992 tmp = fold_build2 (EXACT_DIV_EXPR, type,
1993 fold_build1 (ABS_EXPR, type, difference),
1994 CHREC_RIGHT (chrec_b));
1995 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1996 *last_conflicts = integer_one_node;
1999 /* Perform weak-zero siv test to see if overlap is
2000 outside the loop bounds. */
2001 numiter = max_stmt_executions_int (loop);
2003 if (numiter >= 0
2004 && compare_tree_int (tmp, numiter) > 0)
2006 free_conflict_function (*overlaps_a);
2007 free_conflict_function (*overlaps_b);
2008 *overlaps_a = conflict_fn_no_dependence ();
2009 *overlaps_b = conflict_fn_no_dependence ();
2010 *last_conflicts = integer_zero_node;
2011 dependence_stats.num_siv_independent++;
2012 return;
2014 dependence_stats.num_siv_dependent++;
2015 return;
2018 /* When the step does not divide the difference, there are
2019 no overlaps. */
2020 else
2022 *overlaps_a = conflict_fn_no_dependence ();
2023 *overlaps_b = conflict_fn_no_dependence ();
2024 *last_conflicts = integer_zero_node;
2025 dependence_stats.num_siv_independent++;
2026 return;
2030 else
2032 /* Example:
2033 chrec_a = 12
2034 chrec_b = {10, +, -1}
2036 In this case, chrec_a will not overlap with chrec_b. */
2037 *overlaps_a = conflict_fn_no_dependence ();
2038 *overlaps_b = conflict_fn_no_dependence ();
2039 *last_conflicts = integer_zero_node;
2040 dependence_stats.num_siv_independent++;
2041 return;
2045 else
2047 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
2049 if (dump_file && (dump_flags & TDF_DETAILS))
2050 fprintf (dump_file, "siv test failed: chrec not positive.\n");
2052 *overlaps_a = conflict_fn_not_known ();
2053 *overlaps_b = conflict_fn_not_known ();
2054 *last_conflicts = chrec_dont_know;
2055 dependence_stats.num_siv_unimplemented++;
2056 return;
2058 else
2060 if (value2 == false)
2062 /* Example:
2063 chrec_a = 3
2064 chrec_b = {10, +, -1}
2066 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2068 HOST_WIDE_INT numiter;
2069 struct loop *loop = get_chrec_loop (chrec_b);
2071 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2072 tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
2073 CHREC_RIGHT (chrec_b));
2074 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
2075 *last_conflicts = integer_one_node;
2077 /* Perform weak-zero siv test to see if overlap is
2078 outside the loop bounds. */
2079 numiter = max_stmt_executions_int (loop);
2081 if (numiter >= 0
2082 && compare_tree_int (tmp, numiter) > 0)
2084 free_conflict_function (*overlaps_a);
2085 free_conflict_function (*overlaps_b);
2086 *overlaps_a = conflict_fn_no_dependence ();
2087 *overlaps_b = conflict_fn_no_dependence ();
2088 *last_conflicts = integer_zero_node;
2089 dependence_stats.num_siv_independent++;
2090 return;
2092 dependence_stats.num_siv_dependent++;
2093 return;
2096 /* When the step does not divide the difference, there
2097 are no overlaps. */
2098 else
2100 *overlaps_a = conflict_fn_no_dependence ();
2101 *overlaps_b = conflict_fn_no_dependence ();
2102 *last_conflicts = integer_zero_node;
2103 dependence_stats.num_siv_independent++;
2104 return;
2107 else
2109 /* Example:
2110 chrec_a = 3
2111 chrec_b = {4, +, 1}
2113 In this case, chrec_a will not overlap with chrec_b. */
2114 *overlaps_a = conflict_fn_no_dependence ();
2115 *overlaps_b = conflict_fn_no_dependence ();
2116 *last_conflicts = integer_zero_node;
2117 dependence_stats.num_siv_independent++;
2118 return;
2125 /* Helper recursive function for initializing the matrix A. Returns
2126 the initial value of CHREC. */
2128 static tree
2129 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2131 gcc_assert (chrec);
2133 switch (TREE_CODE (chrec))
2135 case POLYNOMIAL_CHREC:
2136 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST);
2138 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2139 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2141 case PLUS_EXPR:
2142 case MULT_EXPR:
2143 case MINUS_EXPR:
2145 tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2146 tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
2148 return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
2151 CASE_CONVERT:
2153 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2154 return chrec_convert (chrec_type (chrec), op, NULL);
2157 case BIT_NOT_EXPR:
2159 /* Handle ~X as -1 - X. */
2160 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2161 return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
2162 build_int_cst (TREE_TYPE (chrec), -1), op);
2165 case INTEGER_CST:
2166 return chrec;
2168 default:
2169 gcc_unreachable ();
2170 return NULL_TREE;
2174 #define FLOOR_DIV(x,y) ((x) / (y))
2176 /* Solves the special case of the Diophantine equation:
2177 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2179 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2180 number of iterations that loops X and Y run. The overlaps will be
2181 constructed as evolutions in dimension DIM. */
2183 static void
2184 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2185 affine_fn *overlaps_a,
2186 affine_fn *overlaps_b,
2187 tree *last_conflicts, int dim)
2189 if (((step_a > 0 && step_b > 0)
2190 || (step_a < 0 && step_b < 0)))
2192 int step_overlaps_a, step_overlaps_b;
2193 int gcd_steps_a_b, last_conflict, tau2;
2195 gcd_steps_a_b = gcd (step_a, step_b);
2196 step_overlaps_a = step_b / gcd_steps_a_b;
2197 step_overlaps_b = step_a / gcd_steps_a_b;
2199 if (niter > 0)
2201 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2202 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2203 last_conflict = tau2;
2204 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2206 else
2207 *last_conflicts = chrec_dont_know;
2209 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
2210 build_int_cst (NULL_TREE,
2211 step_overlaps_a));
2212 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
2213 build_int_cst (NULL_TREE,
2214 step_overlaps_b));
2217 else
2219 *overlaps_a = affine_fn_cst (integer_zero_node);
2220 *overlaps_b = affine_fn_cst (integer_zero_node);
2221 *last_conflicts = integer_zero_node;
2225 /* Solves the special case of a Diophantine equation where CHREC_A is
2226 an affine bivariate function, and CHREC_B is an affine univariate
2227 function. For example,
2229 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2231 has the following overlapping functions:
2233 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2234 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2235 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2237 FORNOW: This is a specialized implementation for a case occurring in
2238 a common benchmark. Implement the general algorithm. */
2240 static void
2241 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2242 conflict_function **overlaps_a,
2243 conflict_function **overlaps_b,
2244 tree *last_conflicts)
2246 bool xz_p, yz_p, xyz_p;
2247 int step_x, step_y, step_z;
2248 HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
2249 affine_fn overlaps_a_xz, overlaps_b_xz;
2250 affine_fn overlaps_a_yz, overlaps_b_yz;
2251 affine_fn overlaps_a_xyz, overlaps_b_xyz;
2252 affine_fn ova1, ova2, ovb;
2253 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
2255 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2256 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2257 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2259 niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
2260 niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
2261 niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
2263 if (niter_x < 0 || niter_y < 0 || niter_z < 0)
2265 if (dump_file && (dump_flags & TDF_DETAILS))
2266 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
2268 *overlaps_a = conflict_fn_not_known ();
2269 *overlaps_b = conflict_fn_not_known ();
2270 *last_conflicts = chrec_dont_know;
2271 return;
2274 niter = MIN (niter_x, niter_z);
2275 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2276 &overlaps_a_xz,
2277 &overlaps_b_xz,
2278 &last_conflicts_xz, 1);
2279 niter = MIN (niter_y, niter_z);
2280 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2281 &overlaps_a_yz,
2282 &overlaps_b_yz,
2283 &last_conflicts_yz, 2);
2284 niter = MIN (niter_x, niter_z);
2285 niter = MIN (niter_y, niter);
2286 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2287 &overlaps_a_xyz,
2288 &overlaps_b_xyz,
2289 &last_conflicts_xyz, 3);
2291 xz_p = !integer_zerop (last_conflicts_xz);
2292 yz_p = !integer_zerop (last_conflicts_yz);
2293 xyz_p = !integer_zerop (last_conflicts_xyz);
2295 if (xz_p || yz_p || xyz_p)
2297 ova1 = affine_fn_cst (integer_zero_node);
2298 ova2 = affine_fn_cst (integer_zero_node);
2299 ovb = affine_fn_cst (integer_zero_node);
2300 if (xz_p)
2302 affine_fn t0 = ova1;
2303 affine_fn t2 = ovb;
2305 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
2306 ovb = affine_fn_plus (ovb, overlaps_b_xz);
2307 affine_fn_free (t0);
2308 affine_fn_free (t2);
2309 *last_conflicts = last_conflicts_xz;
2311 if (yz_p)
2313 affine_fn t0 = ova2;
2314 affine_fn t2 = ovb;
2316 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
2317 ovb = affine_fn_plus (ovb, overlaps_b_yz);
2318 affine_fn_free (t0);
2319 affine_fn_free (t2);
2320 *last_conflicts = last_conflicts_yz;
2322 if (xyz_p)
2324 affine_fn t0 = ova1;
2325 affine_fn t2 = ova2;
2326 affine_fn t4 = ovb;
2328 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
2329 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
2330 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
2331 affine_fn_free (t0);
2332 affine_fn_free (t2);
2333 affine_fn_free (t4);
2334 *last_conflicts = last_conflicts_xyz;
2336 *overlaps_a = conflict_fn (2, ova1, ova2);
2337 *overlaps_b = conflict_fn (1, ovb);
2339 else
2341 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2342 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2343 *last_conflicts = integer_zero_node;
2346 affine_fn_free (overlaps_a_xz);
2347 affine_fn_free (overlaps_b_xz);
2348 affine_fn_free (overlaps_a_yz);
2349 affine_fn_free (overlaps_b_yz);
2350 affine_fn_free (overlaps_a_xyz);
2351 affine_fn_free (overlaps_b_xyz);
2354 /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2356 static void
2357 lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
2358 int size)
2360 memcpy (vec2, vec1, size * sizeof (*vec1));
2363 /* Copy the elements of M x N matrix MAT1 to MAT2. */
2365 static void
2366 lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
2367 int m, int n)
2369 int i;
2371 for (i = 0; i < m; i++)
2372 lambda_vector_copy (mat1[i], mat2[i], n);
2375 /* Store the N x N identity matrix in MAT. */
2377 static void
2378 lambda_matrix_id (lambda_matrix mat, int size)
2380 int i, j;
2382 for (i = 0; i < size; i++)
2383 for (j = 0; j < size; j++)
2384 mat[i][j] = (i == j) ? 1 : 0;
2387 /* Return the first nonzero element of vector VEC1 between START and N.
2388 We must have START <= N. Returns N if VEC1 is the zero vector. */
2390 static int
2391 lambda_vector_first_nz (lambda_vector vec1, int n, int start)
2393 int j = start;
2394 while (j < n && vec1[j] == 0)
2395 j++;
2396 return j;
2399 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2400 R2 = R2 + CONST1 * R1. */
2402 static void
2403 lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2, int const1)
2405 int i;
2407 if (const1 == 0)
2408 return;
2410 for (i = 0; i < n; i++)
2411 mat[r2][i] += const1 * mat[r1][i];
2414 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
2415 and store the result in VEC2. */
2417 static void
2418 lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
2419 int size, int const1)
2421 int i;
2423 if (const1 == 0)
2424 lambda_vector_clear (vec2, size);
2425 else
2426 for (i = 0; i < size; i++)
2427 vec2[i] = const1 * vec1[i];
2430 /* Negate vector VEC1 with length SIZE and store it in VEC2. */
2432 static void
2433 lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
2434 int size)
2436 lambda_vector_mult_const (vec1, vec2, size, -1);
2439 /* Negate row R1 of matrix MAT which has N columns. */
2441 static void
2442 lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
2444 lambda_vector_negate (mat[r1], mat[r1], n);
2447 /* Return true if two vectors are equal. */
2449 static bool
2450 lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
2452 int i;
2453 for (i = 0; i < size; i++)
2454 if (vec1[i] != vec2[i])
2455 return false;
2456 return true;
2459 /* Given an M x N integer matrix A, this function determines an M x
2460 M unimodular matrix U, and an M x N echelon matrix S such that
2461 "U.A = S". This decomposition is also known as "right Hermite".
2463 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2464 Restructuring Compilers" Utpal Banerjee. */
2466 static void
2467 lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
2468 lambda_matrix S, lambda_matrix U)
2470 int i, j, i0 = 0;
2472 lambda_matrix_copy (A, S, m, n);
2473 lambda_matrix_id (U, m);
2475 for (j = 0; j < n; j++)
2477 if (lambda_vector_first_nz (S[j], m, i0) < m)
2479 ++i0;
2480 for (i = m - 1; i >= i0; i--)
2482 while (S[i][j] != 0)
2484 int sigma, factor, a, b;
2486 a = S[i-1][j];
2487 b = S[i][j];
2488 sigma = (a * b < 0) ? -1: 1;
2489 a = abs (a);
2490 b = abs (b);
2491 factor = sigma * (a / b);
2493 lambda_matrix_row_add (S, n, i, i-1, -factor);
2494 std::swap (S[i], S[i-1]);
2496 lambda_matrix_row_add (U, m, i, i-1, -factor);
2497 std::swap (U[i], U[i-1]);
2504 /* Determines the overlapping elements due to accesses CHREC_A and
2505 CHREC_B, that are affine functions. This function cannot handle
2506 symbolic evolution functions, ie. when initial conditions are
2507 parameters, because it uses lambda matrices of integers. */
2509 static void
2510 analyze_subscript_affine_affine (tree chrec_a,
2511 tree chrec_b,
2512 conflict_function **overlaps_a,
2513 conflict_function **overlaps_b,
2514 tree *last_conflicts)
2516 unsigned nb_vars_a, nb_vars_b, dim;
2517 HOST_WIDE_INT init_a, init_b, gamma, gcd_alpha_beta;
2518 lambda_matrix A, U, S;
2519 struct obstack scratch_obstack;
2521 if (eq_evolutions_p (chrec_a, chrec_b))
2523 /* The accessed index overlaps for each iteration in the
2524 loop. */
2525 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2526 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2527 *last_conflicts = chrec_dont_know;
2528 return;
2530 if (dump_file && (dump_flags & TDF_DETAILS))
2531 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2533 /* For determining the initial intersection, we have to solve a
2534 Diophantine equation. This is the most time consuming part.
2536 For answering to the question: "Is there a dependence?" we have
2537 to prove that there exists a solution to the Diophantine
2538 equation, and that the solution is in the iteration domain,
2539 i.e. the solution is positive or zero, and that the solution
2540 happens before the upper bound loop.nb_iterations. Otherwise
2541 there is no dependence. This function outputs a description of
2542 the iterations that hold the intersections. */
2544 nb_vars_a = nb_vars_in_chrec (chrec_a);
2545 nb_vars_b = nb_vars_in_chrec (chrec_b);
2547 gcc_obstack_init (&scratch_obstack);
2549 dim = nb_vars_a + nb_vars_b;
2550 U = lambda_matrix_new (dim, dim, &scratch_obstack);
2551 A = lambda_matrix_new (dim, 1, &scratch_obstack);
2552 S = lambda_matrix_new (dim, 1, &scratch_obstack);
2554 init_a = int_cst_value (initialize_matrix_A (A, chrec_a, 0, 1));
2555 init_b = int_cst_value (initialize_matrix_A (A, chrec_b, nb_vars_a, -1));
2556 gamma = init_b - init_a;
2558 /* Don't do all the hard work of solving the Diophantine equation
2559 when we already know the solution: for example,
2560 | {3, +, 1}_1
2561 | {3, +, 4}_2
2562 | gamma = 3 - 3 = 0.
2563 Then the first overlap occurs during the first iterations:
2564 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2566 if (gamma == 0)
2568 if (nb_vars_a == 1 && nb_vars_b == 1)
2570 HOST_WIDE_INT step_a, step_b;
2571 HOST_WIDE_INT niter, niter_a, niter_b;
2572 affine_fn ova, ovb;
2574 niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
2575 niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
2576 niter = MIN (niter_a, niter_b);
2577 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2578 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2580 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2581 &ova, &ovb,
2582 last_conflicts, 1);
2583 *overlaps_a = conflict_fn (1, ova);
2584 *overlaps_b = conflict_fn (1, ovb);
2587 else if (nb_vars_a == 2 && nb_vars_b == 1)
2588 compute_overlap_steps_for_affine_1_2
2589 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2591 else if (nb_vars_a == 1 && nb_vars_b == 2)
2592 compute_overlap_steps_for_affine_1_2
2593 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2595 else
2597 if (dump_file && (dump_flags & TDF_DETAILS))
2598 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
2599 *overlaps_a = conflict_fn_not_known ();
2600 *overlaps_b = conflict_fn_not_known ();
2601 *last_conflicts = chrec_dont_know;
2603 goto end_analyze_subs_aa;
2606 /* U.A = S */
2607 lambda_matrix_right_hermite (A, dim, 1, S, U);
2609 if (S[0][0] < 0)
2611 S[0][0] *= -1;
2612 lambda_matrix_row_negate (U, dim, 0);
2614 gcd_alpha_beta = S[0][0];
2616 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2617 but that is a quite strange case. Instead of ICEing, answer
2618 don't know. */
2619 if (gcd_alpha_beta == 0)
2621 *overlaps_a = conflict_fn_not_known ();
2622 *overlaps_b = conflict_fn_not_known ();
2623 *last_conflicts = chrec_dont_know;
2624 goto end_analyze_subs_aa;
2627 /* The classic "gcd-test". */
2628 if (!int_divides_p (gcd_alpha_beta, gamma))
2630 /* The "gcd-test" has determined that there is no integer
2631 solution, i.e. there is no dependence. */
2632 *overlaps_a = conflict_fn_no_dependence ();
2633 *overlaps_b = conflict_fn_no_dependence ();
2634 *last_conflicts = integer_zero_node;
2637 /* Both access functions are univariate. This includes SIV and MIV cases. */
2638 else if (nb_vars_a == 1 && nb_vars_b == 1)
2640 /* Both functions should have the same evolution sign. */
2641 if (((A[0][0] > 0 && -A[1][0] > 0)
2642 || (A[0][0] < 0 && -A[1][0] < 0)))
2644 /* The solutions are given by:
2646 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2647 | [u21 u22] [y0]
2649 For a given integer t. Using the following variables,
2651 | i0 = u11 * gamma / gcd_alpha_beta
2652 | j0 = u12 * gamma / gcd_alpha_beta
2653 | i1 = u21
2654 | j1 = u22
2656 the solutions are:
2658 | x0 = i0 + i1 * t,
2659 | y0 = j0 + j1 * t. */
2660 HOST_WIDE_INT i0, j0, i1, j1;
2662 i0 = U[0][0] * gamma / gcd_alpha_beta;
2663 j0 = U[0][1] * gamma / gcd_alpha_beta;
2664 i1 = U[1][0];
2665 j1 = U[1][1];
2667 if ((i1 == 0 && i0 < 0)
2668 || (j1 == 0 && j0 < 0))
2670 /* There is no solution.
2671 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2672 falls in here, but for the moment we don't look at the
2673 upper bound of the iteration domain. */
2674 *overlaps_a = conflict_fn_no_dependence ();
2675 *overlaps_b = conflict_fn_no_dependence ();
2676 *last_conflicts = integer_zero_node;
2677 goto end_analyze_subs_aa;
2680 if (i1 > 0 && j1 > 0)
2682 HOST_WIDE_INT niter_a
2683 = max_stmt_executions_int (get_chrec_loop (chrec_a));
2684 HOST_WIDE_INT niter_b
2685 = max_stmt_executions_int (get_chrec_loop (chrec_b));
2686 HOST_WIDE_INT niter = MIN (niter_a, niter_b);
2688 /* (X0, Y0) is a solution of the Diophantine equation:
2689 "chrec_a (X0) = chrec_b (Y0)". */
2690 HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
2691 CEIL (-j0, j1));
2692 HOST_WIDE_INT x0 = i1 * tau1 + i0;
2693 HOST_WIDE_INT y0 = j1 * tau1 + j0;
2695 /* (X1, Y1) is the smallest positive solution of the eq
2696 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2697 first conflict occurs. */
2698 HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
2699 HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
2700 HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
2702 if (niter > 0)
2704 HOST_WIDE_INT tau2 = MIN (FLOOR_DIV (niter - i0, i1),
2705 FLOOR_DIV (niter - j0, j1));
2706 HOST_WIDE_INT last_conflict = tau2 - (x1 - i0)/i1;
2708 /* If the overlap occurs outside of the bounds of the
2709 loop, there is no dependence. */
2710 if (x1 >= niter || y1 >= niter)
2712 *overlaps_a = conflict_fn_no_dependence ();
2713 *overlaps_b = conflict_fn_no_dependence ();
2714 *last_conflicts = integer_zero_node;
2715 goto end_analyze_subs_aa;
2717 else
2718 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2720 else
2721 *last_conflicts = chrec_dont_know;
2723 *overlaps_a
2724 = conflict_fn (1,
2725 affine_fn_univar (build_int_cst (NULL_TREE, x1),
2727 build_int_cst (NULL_TREE, i1)));
2728 *overlaps_b
2729 = conflict_fn (1,
2730 affine_fn_univar (build_int_cst (NULL_TREE, y1),
2732 build_int_cst (NULL_TREE, j1)));
2734 else
2736 /* FIXME: For the moment, the upper bound of the
2737 iteration domain for i and j is not checked. */
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 else
2756 if (dump_file && (dump_flags & TDF_DETAILS))
2757 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2758 *overlaps_a = conflict_fn_not_known ();
2759 *overlaps_b = conflict_fn_not_known ();
2760 *last_conflicts = chrec_dont_know;
2763 end_analyze_subs_aa:
2764 obstack_free (&scratch_obstack, NULL);
2765 if (dump_file && (dump_flags & TDF_DETAILS))
2767 fprintf (dump_file, " (overlaps_a = ");
2768 dump_conflict_function (dump_file, *overlaps_a);
2769 fprintf (dump_file, ")\n (overlaps_b = ");
2770 dump_conflict_function (dump_file, *overlaps_b);
2771 fprintf (dump_file, "))\n");
2775 /* Returns true when analyze_subscript_affine_affine can be used for
2776 determining the dependence relation between chrec_a and chrec_b,
2777 that contain symbols. This function modifies chrec_a and chrec_b
2778 such that the analysis result is the same, and such that they don't
2779 contain symbols, and then can safely be passed to the analyzer.
2781 Example: The analysis of the following tuples of evolutions produce
2782 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2783 vs. {0, +, 1}_1
2785 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2786 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2789 static bool
2790 can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
2792 tree diff, type, left_a, left_b, right_b;
2794 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
2795 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
2796 /* FIXME: For the moment not handled. Might be refined later. */
2797 return false;
2799 type = chrec_type (*chrec_a);
2800 left_a = CHREC_LEFT (*chrec_a);
2801 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
2802 diff = chrec_fold_minus (type, left_a, left_b);
2804 if (!evolution_function_is_constant_p (diff))
2805 return false;
2807 if (dump_file && (dump_flags & TDF_DETAILS))
2808 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
2810 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
2811 diff, CHREC_RIGHT (*chrec_a));
2812 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
2813 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
2814 build_int_cst (type, 0),
2815 right_b);
2816 return true;
2819 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2820 *OVERLAPS_B are initialized to the functions that describe the
2821 relation between the elements accessed twice by CHREC_A and
2822 CHREC_B. For k >= 0, the following property is verified:
2824 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2826 static void
2827 analyze_siv_subscript (tree chrec_a,
2828 tree chrec_b,
2829 conflict_function **overlaps_a,
2830 conflict_function **overlaps_b,
2831 tree *last_conflicts,
2832 int loop_nest_num)
2834 dependence_stats.num_siv++;
2836 if (dump_file && (dump_flags & TDF_DETAILS))
2837 fprintf (dump_file, "(analyze_siv_subscript \n");
2839 if (evolution_function_is_constant_p (chrec_a)
2840 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2841 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
2842 overlaps_a, overlaps_b, last_conflicts);
2844 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2845 && evolution_function_is_constant_p (chrec_b))
2846 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
2847 overlaps_b, overlaps_a, last_conflicts);
2849 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2850 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2852 if (!chrec_contains_symbols (chrec_a)
2853 && !chrec_contains_symbols (chrec_b))
2855 analyze_subscript_affine_affine (chrec_a, chrec_b,
2856 overlaps_a, overlaps_b,
2857 last_conflicts);
2859 if (CF_NOT_KNOWN_P (*overlaps_a)
2860 || CF_NOT_KNOWN_P (*overlaps_b))
2861 dependence_stats.num_siv_unimplemented++;
2862 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2863 || CF_NO_DEPENDENCE_P (*overlaps_b))
2864 dependence_stats.num_siv_independent++;
2865 else
2866 dependence_stats.num_siv_dependent++;
2868 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
2869 &chrec_b))
2871 analyze_subscript_affine_affine (chrec_a, chrec_b,
2872 overlaps_a, overlaps_b,
2873 last_conflicts);
2875 if (CF_NOT_KNOWN_P (*overlaps_a)
2876 || CF_NOT_KNOWN_P (*overlaps_b))
2877 dependence_stats.num_siv_unimplemented++;
2878 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2879 || CF_NO_DEPENDENCE_P (*overlaps_b))
2880 dependence_stats.num_siv_independent++;
2881 else
2882 dependence_stats.num_siv_dependent++;
2884 else
2885 goto siv_subscript_dontknow;
2888 else
2890 siv_subscript_dontknow:;
2891 if (dump_file && (dump_flags & TDF_DETAILS))
2892 fprintf (dump_file, " siv test failed: unimplemented");
2893 *overlaps_a = conflict_fn_not_known ();
2894 *overlaps_b = conflict_fn_not_known ();
2895 *last_conflicts = chrec_dont_know;
2896 dependence_stats.num_siv_unimplemented++;
2899 if (dump_file && (dump_flags & TDF_DETAILS))
2900 fprintf (dump_file, ")\n");
2903 /* Returns false if we can prove that the greatest common divisor of the steps
2904 of CHREC does not divide CST, false otherwise. */
2906 static bool
2907 gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
2909 HOST_WIDE_INT cd = 0, val;
2910 tree step;
2912 if (!tree_fits_shwi_p (cst))
2913 return true;
2914 val = tree_to_shwi (cst);
2916 while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
2918 step = CHREC_RIGHT (chrec);
2919 if (!tree_fits_shwi_p (step))
2920 return true;
2921 cd = gcd (cd, tree_to_shwi (step));
2922 chrec = CHREC_LEFT (chrec);
2925 return val % cd == 0;
2928 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
2929 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2930 functions that describe the relation between the elements accessed
2931 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2932 is verified:
2934 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2936 static void
2937 analyze_miv_subscript (tree chrec_a,
2938 tree chrec_b,
2939 conflict_function **overlaps_a,
2940 conflict_function **overlaps_b,
2941 tree *last_conflicts,
2942 struct loop *loop_nest)
2944 tree type, difference;
2946 dependence_stats.num_miv++;
2947 if (dump_file && (dump_flags & TDF_DETAILS))
2948 fprintf (dump_file, "(analyze_miv_subscript \n");
2950 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
2951 chrec_a = chrec_convert (type, chrec_a, NULL);
2952 chrec_b = chrec_convert (type, chrec_b, NULL);
2953 difference = chrec_fold_minus (type, chrec_a, chrec_b);
2955 if (eq_evolutions_p (chrec_a, chrec_b))
2957 /* Access functions are the same: all the elements are accessed
2958 in the same order. */
2959 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2960 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2961 *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
2962 dependence_stats.num_miv_dependent++;
2965 else if (evolution_function_is_constant_p (difference)
2966 /* For the moment, the following is verified:
2967 evolution_function_is_affine_multivariate_p (chrec_a,
2968 loop_nest->num) */
2969 && !gcd_of_steps_may_divide_p (chrec_a, difference))
2971 /* testsuite/.../ssa-chrec-33.c
2972 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2974 The difference is 1, and all the evolution steps are multiples
2975 of 2, consequently there are no overlapping elements. */
2976 *overlaps_a = conflict_fn_no_dependence ();
2977 *overlaps_b = conflict_fn_no_dependence ();
2978 *last_conflicts = integer_zero_node;
2979 dependence_stats.num_miv_independent++;
2982 else if (evolution_function_is_affine_multivariate_p (chrec_a, loop_nest->num)
2983 && !chrec_contains_symbols (chrec_a)
2984 && evolution_function_is_affine_multivariate_p (chrec_b, loop_nest->num)
2985 && !chrec_contains_symbols (chrec_b))
2987 /* testsuite/.../ssa-chrec-35.c
2988 {0, +, 1}_2 vs. {0, +, 1}_3
2989 the overlapping elements are respectively located at iterations:
2990 {0, +, 1}_x and {0, +, 1}_x,
2991 in other words, we have the equality:
2992 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2994 Other examples:
2995 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2996 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2998 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2999 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
3001 analyze_subscript_affine_affine (chrec_a, chrec_b,
3002 overlaps_a, overlaps_b, last_conflicts);
3004 if (CF_NOT_KNOWN_P (*overlaps_a)
3005 || CF_NOT_KNOWN_P (*overlaps_b))
3006 dependence_stats.num_miv_unimplemented++;
3007 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
3008 || CF_NO_DEPENDENCE_P (*overlaps_b))
3009 dependence_stats.num_miv_independent++;
3010 else
3011 dependence_stats.num_miv_dependent++;
3014 else
3016 /* When the analysis is too difficult, answer "don't know". */
3017 if (dump_file && (dump_flags & TDF_DETAILS))
3018 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
3020 *overlaps_a = conflict_fn_not_known ();
3021 *overlaps_b = conflict_fn_not_known ();
3022 *last_conflicts = chrec_dont_know;
3023 dependence_stats.num_miv_unimplemented++;
3026 if (dump_file && (dump_flags & TDF_DETAILS))
3027 fprintf (dump_file, ")\n");
3030 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
3031 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
3032 OVERLAP_ITERATIONS_B are initialized with two functions that
3033 describe the iterations that contain conflicting elements.
3035 Remark: For an integer k >= 0, the following equality is true:
3037 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
3040 static void
3041 analyze_overlapping_iterations (tree chrec_a,
3042 tree chrec_b,
3043 conflict_function **overlap_iterations_a,
3044 conflict_function **overlap_iterations_b,
3045 tree *last_conflicts, struct loop *loop_nest)
3047 unsigned int lnn = loop_nest->num;
3049 dependence_stats.num_subscript_tests++;
3051 if (dump_file && (dump_flags & TDF_DETAILS))
3053 fprintf (dump_file, "(analyze_overlapping_iterations \n");
3054 fprintf (dump_file, " (chrec_a = ");
3055 print_generic_expr (dump_file, chrec_a, 0);
3056 fprintf (dump_file, ")\n (chrec_b = ");
3057 print_generic_expr (dump_file, chrec_b, 0);
3058 fprintf (dump_file, ")\n");
3061 if (chrec_a == NULL_TREE
3062 || chrec_b == NULL_TREE
3063 || chrec_contains_undetermined (chrec_a)
3064 || chrec_contains_undetermined (chrec_b))
3066 dependence_stats.num_subscript_undetermined++;
3068 *overlap_iterations_a = conflict_fn_not_known ();
3069 *overlap_iterations_b = conflict_fn_not_known ();
3072 /* If they are the same chrec, and are affine, they overlap
3073 on every iteration. */
3074 else if (eq_evolutions_p (chrec_a, chrec_b)
3075 && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3076 || operand_equal_p (chrec_a, chrec_b, 0)))
3078 dependence_stats.num_same_subscript_function++;
3079 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3080 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3081 *last_conflicts = chrec_dont_know;
3084 /* If they aren't the same, and aren't affine, we can't do anything
3085 yet. */
3086 else if ((chrec_contains_symbols (chrec_a)
3087 || chrec_contains_symbols (chrec_b))
3088 && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3089 || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
3091 dependence_stats.num_subscript_undetermined++;
3092 *overlap_iterations_a = conflict_fn_not_known ();
3093 *overlap_iterations_b = conflict_fn_not_known ();
3096 else if (ziv_subscript_p (chrec_a, chrec_b))
3097 analyze_ziv_subscript (chrec_a, chrec_b,
3098 overlap_iterations_a, overlap_iterations_b,
3099 last_conflicts);
3101 else if (siv_subscript_p (chrec_a, chrec_b))
3102 analyze_siv_subscript (chrec_a, chrec_b,
3103 overlap_iterations_a, overlap_iterations_b,
3104 last_conflicts, lnn);
3106 else
3107 analyze_miv_subscript (chrec_a, chrec_b,
3108 overlap_iterations_a, overlap_iterations_b,
3109 last_conflicts, loop_nest);
3111 if (dump_file && (dump_flags & TDF_DETAILS))
3113 fprintf (dump_file, " (overlap_iterations_a = ");
3114 dump_conflict_function (dump_file, *overlap_iterations_a);
3115 fprintf (dump_file, ")\n (overlap_iterations_b = ");
3116 dump_conflict_function (dump_file, *overlap_iterations_b);
3117 fprintf (dump_file, "))\n");
3121 /* Helper function for uniquely inserting distance vectors. */
3123 static void
3124 save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
3126 unsigned i;
3127 lambda_vector v;
3129 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, v)
3130 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
3131 return;
3133 DDR_DIST_VECTS (ddr).safe_push (dist_v);
3136 /* Helper function for uniquely inserting direction vectors. */
3138 static void
3139 save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
3141 unsigned i;
3142 lambda_vector v;
3144 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), i, v)
3145 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
3146 return;
3148 DDR_DIR_VECTS (ddr).safe_push (dir_v);
3151 /* Add a distance of 1 on all the loops outer than INDEX. If we
3152 haven't yet determined a distance for this outer loop, push a new
3153 distance vector composed of the previous distance, and a distance
3154 of 1 for this outer loop. Example:
3156 | loop_1
3157 | loop_2
3158 | A[10]
3159 | endloop_2
3160 | endloop_1
3162 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3163 save (0, 1), then we have to save (1, 0). */
3165 static void
3166 add_outer_distances (struct data_dependence_relation *ddr,
3167 lambda_vector dist_v, int index)
3169 /* For each outer loop where init_v is not set, the accesses are
3170 in dependence of distance 1 in the loop. */
3171 while (--index >= 0)
3173 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3174 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3175 save_v[index] = 1;
3176 save_dist_v (ddr, save_v);
3180 /* Return false when fail to represent the data dependence as a
3181 distance vector. INIT_B is set to true when a component has been
3182 added to the distance vector DIST_V. INDEX_CARRY is then set to
3183 the index in DIST_V that carries the dependence. */
3185 static bool
3186 build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
3187 struct data_reference *ddr_a,
3188 struct data_reference *ddr_b,
3189 lambda_vector dist_v, bool *init_b,
3190 int *index_carry)
3192 unsigned i;
3193 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3195 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3197 tree access_fn_a, access_fn_b;
3198 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3200 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3202 non_affine_dependence_relation (ddr);
3203 return false;
3206 access_fn_a = DR_ACCESS_FN (ddr_a, i);
3207 access_fn_b = DR_ACCESS_FN (ddr_b, i);
3209 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3210 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3212 int dist, index;
3213 int var_a = CHREC_VARIABLE (access_fn_a);
3214 int var_b = CHREC_VARIABLE (access_fn_b);
3216 if (var_a != var_b
3217 || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3219 non_affine_dependence_relation (ddr);
3220 return false;
3223 dist = int_cst_value (SUB_DISTANCE (subscript));
3224 index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
3225 *index_carry = MIN (index, *index_carry);
3227 /* This is the subscript coupling test. If we have already
3228 recorded a distance for this loop (a distance coming from
3229 another subscript), it should be the same. For example,
3230 in the following code, there is no dependence:
3232 | loop i = 0, N, 1
3233 | T[i+1][i] = ...
3234 | ... = T[i][i]
3235 | endloop
3237 if (init_v[index] != 0 && dist_v[index] != dist)
3239 finalize_ddr_dependent (ddr, chrec_known);
3240 return false;
3243 dist_v[index] = dist;
3244 init_v[index] = 1;
3245 *init_b = true;
3247 else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
3249 /* This can be for example an affine vs. constant dependence
3250 (T[i] vs. T[3]) that is not an affine dependence and is
3251 not representable as a distance vector. */
3252 non_affine_dependence_relation (ddr);
3253 return false;
3257 return true;
3260 /* Return true when the DDR contains only constant access functions. */
3262 static bool
3263 constant_access_functions (const struct data_dependence_relation *ddr)
3265 unsigned i;
3267 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3268 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr), i))
3269 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr), i)))
3270 return false;
3272 return true;
3275 /* Helper function for the case where DDR_A and DDR_B are the same
3276 multivariate access function with a constant step. For an example
3277 see pr34635-1.c. */
3279 static void
3280 add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
3282 int x_1, x_2;
3283 tree c_1 = CHREC_LEFT (c_2);
3284 tree c_0 = CHREC_LEFT (c_1);
3285 lambda_vector dist_v;
3286 int v1, v2, cd;
3288 /* Polynomials with more than 2 variables are not handled yet. When
3289 the evolution steps are parameters, it is not possible to
3290 represent the dependence using classical distance vectors. */
3291 if (TREE_CODE (c_0) != INTEGER_CST
3292 || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
3293 || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
3295 DDR_AFFINE_P (ddr) = false;
3296 return;
3299 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
3300 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
3302 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3303 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3304 v1 = int_cst_value (CHREC_RIGHT (c_1));
3305 v2 = int_cst_value (CHREC_RIGHT (c_2));
3306 cd = gcd (v1, v2);
3307 v1 /= cd;
3308 v2 /= cd;
3310 if (v2 < 0)
3312 v2 = -v2;
3313 v1 = -v1;
3316 dist_v[x_1] = v2;
3317 dist_v[x_2] = -v1;
3318 save_dist_v (ddr, dist_v);
3320 add_outer_distances (ddr, dist_v, x_1);
3323 /* Helper function for the case where DDR_A and DDR_B are the same
3324 access functions. */
3326 static void
3327 add_other_self_distances (struct data_dependence_relation *ddr)
3329 lambda_vector dist_v;
3330 unsigned i;
3331 int index_carry = DDR_NB_LOOPS (ddr);
3333 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3335 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
3337 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
3339 if (!evolution_function_is_univariate_p (access_fun))
3341 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
3343 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3344 return;
3347 access_fun = DR_ACCESS_FN (DDR_A (ddr), 0);
3349 if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
3350 add_multivariate_self_dist (ddr, access_fun);
3351 else
3352 /* The evolution step is not constant: it varies in
3353 the outer loop, so this cannot be represented by a
3354 distance vector. For example in pr34635.c the
3355 evolution is {0, +, {0, +, 4}_1}_2. */
3356 DDR_AFFINE_P (ddr) = false;
3358 return;
3361 index_carry = MIN (index_carry,
3362 index_in_loop_nest (CHREC_VARIABLE (access_fun),
3363 DDR_LOOP_NEST (ddr)));
3367 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3368 add_outer_distances (ddr, dist_v, index_carry);
3371 static void
3372 insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
3374 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3376 dist_v[DDR_INNER_LOOP (ddr)] = 1;
3377 save_dist_v (ddr, dist_v);
3380 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
3381 is the case for example when access functions are the same and
3382 equal to a constant, as in:
3384 | loop_1
3385 | A[3] = ...
3386 | ... = A[3]
3387 | endloop_1
3389 in which case the distance vectors are (0) and (1). */
3391 static void
3392 add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
3394 unsigned i, j;
3396 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3398 subscript_p sub = DDR_SUBSCRIPT (ddr, i);
3399 conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
3400 conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
3402 for (j = 0; j < ca->n; j++)
3403 if (affine_function_zero_p (ca->fns[j]))
3405 insert_innermost_unit_dist_vector (ddr);
3406 return;
3409 for (j = 0; j < cb->n; j++)
3410 if (affine_function_zero_p (cb->fns[j]))
3412 insert_innermost_unit_dist_vector (ddr);
3413 return;
3418 /* Compute the classic per loop distance vector. DDR is the data
3419 dependence relation to build a vector from. Return false when fail
3420 to represent the data dependence as a distance vector. */
3422 static bool
3423 build_classic_dist_vector (struct data_dependence_relation *ddr,
3424 struct loop *loop_nest)
3426 bool init_b = false;
3427 int index_carry = DDR_NB_LOOPS (ddr);
3428 lambda_vector dist_v;
3430 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3431 return false;
3433 if (same_access_functions (ddr))
3435 /* Save the 0 vector. */
3436 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3437 save_dist_v (ddr, dist_v);
3439 if (constant_access_functions (ddr))
3440 add_distance_for_zero_overlaps (ddr);
3442 if (DDR_NB_LOOPS (ddr) > 1)
3443 add_other_self_distances (ddr);
3445 return true;
3448 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3449 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3450 dist_v, &init_b, &index_carry))
3451 return false;
3453 /* Save the distance vector if we initialized one. */
3454 if (init_b)
3456 /* Verify a basic constraint: classic distance vectors should
3457 always be lexicographically positive.
3459 Data references are collected in the order of execution of
3460 the program, thus for the following loop
3462 | for (i = 1; i < 100; i++)
3463 | for (j = 1; j < 100; j++)
3465 | t = T[j+1][i-1]; // A
3466 | T[j][i] = t + 2; // B
3469 references are collected following the direction of the wind:
3470 A then B. The data dependence tests are performed also
3471 following this order, such that we're looking at the distance
3472 separating the elements accessed by A from the elements later
3473 accessed by B. But in this example, the distance returned by
3474 test_dep (A, B) is lexicographically negative (-1, 1), that
3475 means that the access A occurs later than B with respect to
3476 the outer loop, ie. we're actually looking upwind. In this
3477 case we solve test_dep (B, A) looking downwind to the
3478 lexicographically positive solution, that returns the
3479 distance vector (1, -1). */
3480 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3482 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3483 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3484 loop_nest))
3485 return false;
3486 compute_subscript_distance (ddr);
3487 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3488 save_v, &init_b, &index_carry))
3489 return false;
3490 save_dist_v (ddr, save_v);
3491 DDR_REVERSED_P (ddr) = true;
3493 /* In this case there is a dependence forward for all the
3494 outer loops:
3496 | for (k = 1; k < 100; k++)
3497 | for (i = 1; i < 100; i++)
3498 | for (j = 1; j < 100; j++)
3500 | t = T[j+1][i-1]; // A
3501 | T[j][i] = t + 2; // B
3504 the vectors are:
3505 (0, 1, -1)
3506 (1, 1, -1)
3507 (1, -1, 1)
3509 if (DDR_NB_LOOPS (ddr) > 1)
3511 add_outer_distances (ddr, save_v, index_carry);
3512 add_outer_distances (ddr, dist_v, index_carry);
3515 else
3517 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3518 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3520 if (DDR_NB_LOOPS (ddr) > 1)
3522 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3524 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr),
3525 DDR_A (ddr), loop_nest))
3526 return false;
3527 compute_subscript_distance (ddr);
3528 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3529 opposite_v, &init_b,
3530 &index_carry))
3531 return false;
3533 save_dist_v (ddr, save_v);
3534 add_outer_distances (ddr, dist_v, index_carry);
3535 add_outer_distances (ddr, opposite_v, index_carry);
3537 else
3538 save_dist_v (ddr, save_v);
3541 else
3543 /* There is a distance of 1 on all the outer loops: Example:
3544 there is a dependence of distance 1 on loop_1 for the array A.
3546 | loop_1
3547 | A[5] = ...
3548 | endloop
3550 add_outer_distances (ddr, dist_v,
3551 lambda_vector_first_nz (dist_v,
3552 DDR_NB_LOOPS (ddr), 0));
3555 if (dump_file && (dump_flags & TDF_DETAILS))
3557 unsigned i;
3559 fprintf (dump_file, "(build_classic_dist_vector\n");
3560 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3562 fprintf (dump_file, " dist_vector = (");
3563 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3564 DDR_NB_LOOPS (ddr));
3565 fprintf (dump_file, " )\n");
3567 fprintf (dump_file, ")\n");
3570 return true;
3573 /* Return the direction for a given distance.
3574 FIXME: Computing dir this way is suboptimal, since dir can catch
3575 cases that dist is unable to represent. */
3577 static inline enum data_dependence_direction
3578 dir_from_dist (int dist)
3580 if (dist > 0)
3581 return dir_positive;
3582 else if (dist < 0)
3583 return dir_negative;
3584 else
3585 return dir_equal;
3588 /* Compute the classic per loop direction vector. DDR is the data
3589 dependence relation to build a vector from. */
3591 static void
3592 build_classic_dir_vector (struct data_dependence_relation *ddr)
3594 unsigned i, j;
3595 lambda_vector dist_v;
3597 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
3599 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3601 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3602 dir_v[j] = dir_from_dist (dist_v[j]);
3604 save_dir_v (ddr, dir_v);
3608 /* Helper function. Returns true when there is a dependence between
3609 data references DRA and DRB. */
3611 static bool
3612 subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
3613 struct data_reference *dra,
3614 struct data_reference *drb,
3615 struct loop *loop_nest)
3617 unsigned int i;
3618 tree last_conflicts;
3619 struct subscript *subscript;
3620 tree res = NULL_TREE;
3622 for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
3624 conflict_function *overlaps_a, *overlaps_b;
3626 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3627 DR_ACCESS_FN (drb, i),
3628 &overlaps_a, &overlaps_b,
3629 &last_conflicts, loop_nest);
3631 if (SUB_CONFLICTS_IN_A (subscript))
3632 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
3633 if (SUB_CONFLICTS_IN_B (subscript))
3634 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
3636 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3637 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3638 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3640 /* If there is any undetermined conflict function we have to
3641 give a conservative answer in case we cannot prove that
3642 no dependence exists when analyzing another subscript. */
3643 if (CF_NOT_KNOWN_P (overlaps_a)
3644 || CF_NOT_KNOWN_P (overlaps_b))
3646 res = chrec_dont_know;
3647 continue;
3650 /* When there is a subscript with no dependence we can stop. */
3651 else if (CF_NO_DEPENDENCE_P (overlaps_a)
3652 || CF_NO_DEPENDENCE_P (overlaps_b))
3654 res = chrec_known;
3655 break;
3659 if (res == NULL_TREE)
3660 return true;
3662 if (res == chrec_known)
3663 dependence_stats.num_dependence_independent++;
3664 else
3665 dependence_stats.num_dependence_undetermined++;
3666 finalize_ddr_dependent (ddr, res);
3667 return false;
3670 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
3672 static void
3673 subscript_dependence_tester (struct data_dependence_relation *ddr,
3674 struct loop *loop_nest)
3676 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr), loop_nest))
3677 dependence_stats.num_dependence_dependent++;
3679 compute_subscript_distance (ddr);
3680 if (build_classic_dist_vector (ddr, loop_nest))
3681 build_classic_dir_vector (ddr);
3684 /* Returns true when all the access functions of A are affine or
3685 constant with respect to LOOP_NEST. */
3687 static bool
3688 access_functions_are_affine_or_constant_p (const struct data_reference *a,
3689 const struct loop *loop_nest)
3691 unsigned int i;
3692 vec<tree> fns = DR_ACCESS_FNS (a);
3693 tree t;
3695 FOR_EACH_VEC_ELT (fns, i, t)
3696 if (!evolution_function_is_invariant_p (t, loop_nest->num)
3697 && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
3698 return false;
3700 return true;
3703 /* Initializes an equation for an OMEGA problem using the information
3704 contained in the ACCESS_FUN. Returns true when the operation
3705 succeeded.
3707 PB is the omega constraint system.
3708 EQ is the number of the equation to be initialized.
3709 OFFSET is used for shifting the variables names in the constraints:
3710 a constrain is composed of 2 * the number of variables surrounding
3711 dependence accesses. OFFSET is set either to 0 for the first n variables,
3712 then it is set to n.
3713 ACCESS_FUN is expected to be an affine chrec. */
3715 static bool
3716 init_omega_eq_with_af (omega_pb pb, unsigned eq,
3717 unsigned int offset, tree access_fun,
3718 struct data_dependence_relation *ddr)
3720 switch (TREE_CODE (access_fun))
3722 case POLYNOMIAL_CHREC:
3724 tree left = CHREC_LEFT (access_fun);
3725 tree right = CHREC_RIGHT (access_fun);
3726 int var = CHREC_VARIABLE (access_fun);
3727 unsigned var_idx;
3729 if (TREE_CODE (right) != INTEGER_CST)
3730 return false;
3732 var_idx = index_in_loop_nest (var, DDR_LOOP_NEST (ddr));
3733 pb->eqs[eq].coef[offset + var_idx + 1] = int_cst_value (right);
3735 /* Compute the innermost loop index. */
3736 DDR_INNER_LOOP (ddr) = MAX (DDR_INNER_LOOP (ddr), var_idx);
3738 if (offset == 0)
3739 pb->eqs[eq].coef[var_idx + DDR_NB_LOOPS (ddr) + 1]
3740 += int_cst_value (right);
3742 switch (TREE_CODE (left))
3744 case POLYNOMIAL_CHREC:
3745 return init_omega_eq_with_af (pb, eq, offset, left, ddr);
3747 case INTEGER_CST:
3748 pb->eqs[eq].coef[0] += int_cst_value (left);
3749 return true;
3751 default:
3752 return false;
3756 case INTEGER_CST:
3757 pb->eqs[eq].coef[0] += int_cst_value (access_fun);
3758 return true;
3760 default:
3761 return false;
3765 /* As explained in the comments preceding init_omega_for_ddr, we have
3766 to set up a system for each loop level, setting outer loops
3767 variation to zero, and current loop variation to positive or zero.
3768 Save each lexico positive distance vector. */
3770 static void
3771 omega_extract_distance_vectors (omega_pb pb,
3772 struct data_dependence_relation *ddr)
3774 int eq, geq;
3775 unsigned i, j;
3776 struct loop *loopi, *loopj;
3777 enum omega_result res;
3779 /* Set a new problem for each loop in the nest. The basis is the
3780 problem that we have initialized until now. On top of this we
3781 add new constraints. */
3782 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3783 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3785 int dist = 0;
3786 omega_pb copy = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr),
3787 DDR_NB_LOOPS (ddr));
3789 omega_copy_problem (copy, pb);
3791 /* For all the outer loops "loop_j", add "dj = 0". */
3792 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3794 eq = omega_add_zero_eq (copy, omega_black);
3795 copy->eqs[eq].coef[j + 1] = 1;
3798 /* For "loop_i", add "0 <= di". */
3799 geq = omega_add_zero_geq (copy, omega_black);
3800 copy->geqs[geq].coef[i + 1] = 1;
3802 /* Reduce the constraint system, and test that the current
3803 problem is feasible. */
3804 res = omega_simplify_problem (copy);
3805 if (res == omega_false
3806 || res == omega_unknown
3807 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3808 goto next_problem;
3810 for (eq = 0; eq < copy->num_subs; eq++)
3811 if (copy->subs[eq].key == (int) i + 1)
3813 dist = copy->subs[eq].coef[0];
3814 goto found_dist;
3817 if (dist == 0)
3819 /* Reinitialize problem... */
3820 omega_copy_problem (copy, pb);
3821 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3823 eq = omega_add_zero_eq (copy, omega_black);
3824 copy->eqs[eq].coef[j + 1] = 1;
3827 /* ..., but this time "di = 1". */
3828 eq = omega_add_zero_eq (copy, omega_black);
3829 copy->eqs[eq].coef[i + 1] = 1;
3830 copy->eqs[eq].coef[0] = -1;
3832 res = omega_simplify_problem (copy);
3833 if (res == omega_false
3834 || res == omega_unknown
3835 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3836 goto next_problem;
3838 for (eq = 0; eq < copy->num_subs; eq++)
3839 if (copy->subs[eq].key == (int) i + 1)
3841 dist = copy->subs[eq].coef[0];
3842 goto found_dist;
3846 found_dist:;
3847 /* Save the lexicographically positive distance vector. */
3848 if (dist >= 0)
3850 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3851 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3853 dist_v[i] = dist;
3855 for (eq = 0; eq < copy->num_subs; eq++)
3856 if (copy->subs[eq].key > 0)
3858 dist = copy->subs[eq].coef[0];
3859 dist_v[copy->subs[eq].key - 1] = dist;
3862 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3863 dir_v[j] = dir_from_dist (dist_v[j]);
3865 save_dist_v (ddr, dist_v);
3866 save_dir_v (ddr, dir_v);
3869 next_problem:;
3870 omega_free_problem (copy);
3874 /* This is called for each subscript of a tuple of data references:
3875 insert an equality for representing the conflicts. */
3877 static bool
3878 omega_setup_subscript (tree access_fun_a, tree access_fun_b,
3879 struct data_dependence_relation *ddr,
3880 omega_pb pb, bool *maybe_dependent)
3882 int eq;
3883 tree type = signed_type_for_types (TREE_TYPE (access_fun_a),
3884 TREE_TYPE (access_fun_b));
3885 tree fun_a = chrec_convert (type, access_fun_a, NULL);
3886 tree fun_b = chrec_convert (type, access_fun_b, NULL);
3887 tree difference = chrec_fold_minus (type, fun_a, fun_b);
3888 tree minus_one;
3890 /* When the fun_a - fun_b is not constant, the dependence is not
3891 captured by the classic distance vector representation. */
3892 if (TREE_CODE (difference) != INTEGER_CST)
3893 return false;
3895 /* ZIV test. */
3896 if (ziv_subscript_p (fun_a, fun_b) && !integer_zerop (difference))
3898 /* There is no dependence. */
3899 *maybe_dependent = false;
3900 return true;
3903 minus_one = build_int_cst (type, -1);
3904 fun_b = chrec_fold_multiply (type, fun_b, minus_one);
3906 eq = omega_add_zero_eq (pb, omega_black);
3907 if (!init_omega_eq_with_af (pb, eq, DDR_NB_LOOPS (ddr), fun_a, ddr)
3908 || !init_omega_eq_with_af (pb, eq, 0, fun_b, ddr))
3909 /* There is probably a dependence, but the system of
3910 constraints cannot be built: answer "don't know". */
3911 return false;
3913 /* GCD test. */
3914 if (DDR_NB_LOOPS (ddr) != 0 && pb->eqs[eq].coef[0]
3915 && !int_divides_p (lambda_vector_gcd
3916 ((lambda_vector) &(pb->eqs[eq].coef[1]),
3917 2 * DDR_NB_LOOPS (ddr)),
3918 pb->eqs[eq].coef[0]))
3920 /* There is no dependence. */
3921 *maybe_dependent = false;
3922 return true;
3925 return true;
3928 /* Helper function, same as init_omega_for_ddr but specialized for
3929 data references A and B. */
3931 static bool
3932 init_omega_for_ddr_1 (struct data_reference *dra, struct data_reference *drb,
3933 struct data_dependence_relation *ddr,
3934 omega_pb pb, bool *maybe_dependent)
3936 unsigned i;
3937 int ineq;
3938 struct loop *loopi;
3939 unsigned nb_loops = DDR_NB_LOOPS (ddr);
3941 /* Insert an equality per subscript. */
3942 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3944 if (!omega_setup_subscript (DR_ACCESS_FN (dra, i), DR_ACCESS_FN (drb, i),
3945 ddr, pb, maybe_dependent))
3946 return false;
3947 else if (*maybe_dependent == false)
3949 /* There is no dependence. */
3950 DDR_ARE_DEPENDENT (ddr) = chrec_known;
3951 return true;
3955 /* Insert inequalities: constraints corresponding to the iteration
3956 domain, i.e. the loops surrounding the references "loop_x" and
3957 the distance variables "dx". The layout of the OMEGA
3958 representation is as follows:
3959 - coef[0] is the constant
3960 - coef[1..nb_loops] are the protected variables that will not be
3961 removed by the solver: the "dx"
3962 - coef[nb_loops + 1, 2*nb_loops] are the loop variables: "loop_x".
3964 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3965 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3967 HOST_WIDE_INT nbi = max_stmt_executions_int (loopi);
3969 /* 0 <= loop_x */
3970 ineq = omega_add_zero_geq (pb, omega_black);
3971 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3973 /* 0 <= loop_x + dx */
3974 ineq = omega_add_zero_geq (pb, omega_black);
3975 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3976 pb->geqs[ineq].coef[i + 1] = 1;
3978 if (nbi != -1)
3980 /* loop_x <= nb_iters */
3981 ineq = omega_add_zero_geq (pb, omega_black);
3982 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3983 pb->geqs[ineq].coef[0] = nbi;
3985 /* loop_x + dx <= nb_iters */
3986 ineq = omega_add_zero_geq (pb, omega_black);
3987 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3988 pb->geqs[ineq].coef[i + 1] = -1;
3989 pb->geqs[ineq].coef[0] = nbi;
3991 /* A step "dx" bigger than nb_iters is not feasible, so
3992 add "0 <= nb_iters + dx", */
3993 ineq = omega_add_zero_geq (pb, omega_black);
3994 pb->geqs[ineq].coef[i + 1] = 1;
3995 pb->geqs[ineq].coef[0] = nbi;
3996 /* and "dx <= nb_iters". */
3997 ineq = omega_add_zero_geq (pb, omega_black);
3998 pb->geqs[ineq].coef[i + 1] = -1;
3999 pb->geqs[ineq].coef[0] = nbi;
4003 omega_extract_distance_vectors (pb, ddr);
4005 return true;
4008 /* Sets up the Omega dependence problem for the data dependence
4009 relation DDR. Returns false when the constraint system cannot be
4010 built, ie. when the test answers "don't know". Returns true
4011 otherwise, and when independence has been proved (using one of the
4012 trivial dependence test), set MAYBE_DEPENDENT to false, otherwise
4013 set MAYBE_DEPENDENT to true.
4015 Example: for setting up the dependence system corresponding to the
4016 conflicting accesses
4018 | loop_i
4019 | loop_j
4020 | A[i, i+1] = ...
4021 | ... A[2*j, 2*(i + j)]
4022 | endloop_j
4023 | endloop_i
4025 the following constraints come from the iteration domain:
4027 0 <= i <= Ni
4028 0 <= i + di <= Ni
4029 0 <= j <= Nj
4030 0 <= j + dj <= Nj
4032 where di, dj are the distance variables. The constraints
4033 representing the conflicting elements are:
4035 i = 2 * (j + dj)
4036 i + 1 = 2 * (i + di + j + dj)
4038 For asking that the resulting distance vector (di, dj) be
4039 lexicographically positive, we insert the constraint "di >= 0". If
4040 "di = 0" in the solution, we fix that component to zero, and we
4041 look at the inner loops: we set a new problem where all the outer
4042 loop distances are zero, and fix this inner component to be
4043 positive. When one of the components is positive, we save that
4044 distance, and set a new problem where the distance on this loop is
4045 zero, searching for other distances in the inner loops. Here is
4046 the classic example that illustrates that we have to set for each
4047 inner loop a new problem:
4049 | loop_1
4050 | loop_2
4051 | A[10]
4052 | endloop_2
4053 | endloop_1
4055 we have to save two distances (1, 0) and (0, 1).
4057 Given two array references, refA and refB, we have to set the
4058 dependence problem twice, refA vs. refB and refB vs. refA, and we
4059 cannot do a single test, as refB might occur before refA in the
4060 inner loops, and the contrary when considering outer loops: ex.
4062 | loop_0
4063 | loop_1
4064 | loop_2
4065 | T[{1,+,1}_2][{1,+,1}_1] // refA
4066 | T[{2,+,1}_2][{0,+,1}_1] // refB
4067 | endloop_2
4068 | endloop_1
4069 | endloop_0
4071 refB touches the elements in T before refA, and thus for the same
4072 loop_0 refB precedes refA: ie. the distance vector (0, 1, -1)
4073 but for successive loop_0 iterations, we have (1, -1, 1)
4075 The Omega solver expects the distance variables ("di" in the
4076 previous example) to come first in the constraint system (as
4077 variables to be protected, or "safe" variables), the constraint
4078 system is built using the following layout:
4080 "cst | distance vars | index vars".
4083 static bool
4084 init_omega_for_ddr (struct data_dependence_relation *ddr,
4085 bool *maybe_dependent)
4087 omega_pb pb;
4088 bool res = false;
4090 *maybe_dependent = true;
4092 if (same_access_functions (ddr))
4094 unsigned j;
4095 lambda_vector dir_v;
4097 /* Save the 0 vector. */
4098 save_dist_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
4099 dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4100 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
4101 dir_v[j] = dir_equal;
4102 save_dir_v (ddr, dir_v);
4104 /* Save the dependences carried by outer loops. */
4105 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4106 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4107 maybe_dependent);
4108 omega_free_problem (pb);
4109 return res;
4112 /* Omega expects the protected variables (those that have to be kept
4113 after elimination) to appear first in the constraint system.
4114 These variables are the distance variables. In the following
4115 initialization we declare NB_LOOPS safe variables, and the total
4116 number of variables for the constraint system is 2*NB_LOOPS. */
4117 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4118 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4119 maybe_dependent);
4120 omega_free_problem (pb);
4122 /* Stop computation if not decidable, or no dependence. */
4123 if (res == false || *maybe_dependent == false)
4124 return res;
4126 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4127 res = init_omega_for_ddr_1 (DDR_B (ddr), DDR_A (ddr), ddr, pb,
4128 maybe_dependent);
4129 omega_free_problem (pb);
4131 return res;
4134 /* Return true when DDR contains the same information as that stored
4135 in DIR_VECTS and in DIST_VECTS, return false otherwise. */
4137 static bool
4138 ddr_consistent_p (FILE *file,
4139 struct data_dependence_relation *ddr,
4140 vec<lambda_vector> dist_vects,
4141 vec<lambda_vector> dir_vects)
4143 unsigned int i, j;
4145 /* If dump_file is set, output there. */
4146 if (dump_file && (dump_flags & TDF_DETAILS))
4147 file = dump_file;
4149 if (dist_vects.length () != DDR_NUM_DIST_VECTS (ddr))
4151 lambda_vector b_dist_v;
4152 fprintf (file, "\n(Number of distance vectors differ: Banerjee has %d, Omega has %d.\n",
4153 dist_vects.length (),
4154 DDR_NUM_DIST_VECTS (ddr));
4156 fprintf (file, "Banerjee dist vectors:\n");
4157 FOR_EACH_VEC_ELT (dist_vects, i, b_dist_v)
4158 print_lambda_vector (file, b_dist_v, DDR_NB_LOOPS (ddr));
4160 fprintf (file, "Omega dist vectors:\n");
4161 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4162 print_lambda_vector (file, DDR_DIST_VECT (ddr, i), DDR_NB_LOOPS (ddr));
4164 fprintf (file, "data dependence relation:\n");
4165 dump_data_dependence_relation (file, ddr);
4167 fprintf (file, ")\n");
4168 return false;
4171 if (dir_vects.length () != DDR_NUM_DIR_VECTS (ddr))
4173 fprintf (file, "\n(Number of direction vectors differ: Banerjee has %d, Omega has %d.)\n",
4174 dir_vects.length (),
4175 DDR_NUM_DIR_VECTS (ddr));
4176 return false;
4179 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4181 lambda_vector a_dist_v;
4182 lambda_vector b_dist_v = DDR_DIST_VECT (ddr, i);
4184 /* Distance vectors are not ordered in the same way in the DDR
4185 and in the DIST_VECTS: search for a matching vector. */
4186 FOR_EACH_VEC_ELT (dist_vects, j, a_dist_v)
4187 if (lambda_vector_equal (a_dist_v, b_dist_v, DDR_NB_LOOPS (ddr)))
4188 break;
4190 if (j == dist_vects.length ())
4192 fprintf (file, "\n(Dist vectors from the first dependence analyzer:\n");
4193 print_dist_vectors (file, dist_vects, DDR_NB_LOOPS (ddr));
4194 fprintf (file, "not found in Omega dist vectors:\n");
4195 print_dist_vectors (file, DDR_DIST_VECTS (ddr), DDR_NB_LOOPS (ddr));
4196 fprintf (file, "data dependence relation:\n");
4197 dump_data_dependence_relation (file, ddr);
4198 fprintf (file, ")\n");
4202 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
4204 lambda_vector a_dir_v;
4205 lambda_vector b_dir_v = DDR_DIR_VECT (ddr, i);
4207 /* Direction vectors are not ordered in the same way in the DDR
4208 and in the DIR_VECTS: search for a matching vector. */
4209 FOR_EACH_VEC_ELT (dir_vects, j, a_dir_v)
4210 if (lambda_vector_equal (a_dir_v, b_dir_v, DDR_NB_LOOPS (ddr)))
4211 break;
4213 if (j == dist_vects.length ())
4215 fprintf (file, "\n(Dir vectors from the first dependence analyzer:\n");
4216 print_dir_vectors (file, dir_vects, DDR_NB_LOOPS (ddr));
4217 fprintf (file, "not found in Omega dir vectors:\n");
4218 print_dir_vectors (file, DDR_DIR_VECTS (ddr), DDR_NB_LOOPS (ddr));
4219 fprintf (file, "data dependence relation:\n");
4220 dump_data_dependence_relation (file, ddr);
4221 fprintf (file, ")\n");
4225 return true;
4228 /* This computes the affine dependence relation between A and B with
4229 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
4230 independence between two accesses, while CHREC_DONT_KNOW is used
4231 for representing the unknown relation.
4233 Note that it is possible to stop the computation of the dependence
4234 relation the first time we detect a CHREC_KNOWN element for a given
4235 subscript. */
4237 void
4238 compute_affine_dependence (struct data_dependence_relation *ddr,
4239 struct loop *loop_nest)
4241 struct data_reference *dra = DDR_A (ddr);
4242 struct data_reference *drb = DDR_B (ddr);
4244 if (dump_file && (dump_flags & TDF_DETAILS))
4246 fprintf (dump_file, "(compute_affine_dependence\n");
4247 fprintf (dump_file, " stmt_a: ");
4248 print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
4249 fprintf (dump_file, " stmt_b: ");
4250 print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
4253 /* Analyze only when the dependence relation is not yet known. */
4254 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4256 dependence_stats.num_dependence_tests++;
4258 if (access_functions_are_affine_or_constant_p (dra, loop_nest)
4259 && access_functions_are_affine_or_constant_p (drb, loop_nest))
4261 subscript_dependence_tester (ddr, loop_nest);
4263 if (flag_check_data_deps)
4265 /* Dump the dependences from the first algorithm. */
4266 if (dump_file && (dump_flags & TDF_DETAILS))
4268 fprintf (dump_file, "\n\nBanerjee Analyzer\n");
4269 dump_data_dependence_relation (dump_file, ddr);
4272 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4274 bool maybe_dependent;
4275 vec<lambda_vector> dir_vects, dist_vects;
4277 /* Save the result of the first DD analyzer. */
4278 dist_vects = DDR_DIST_VECTS (ddr);
4279 dir_vects = DDR_DIR_VECTS (ddr);
4281 /* Reset the information. */
4282 DDR_DIST_VECTS (ddr).create (0);
4283 DDR_DIR_VECTS (ddr).create (0);
4285 /* Compute the same information using Omega. */
4286 if (!init_omega_for_ddr (ddr, &maybe_dependent))
4287 goto csys_dont_know;
4289 if (dump_file && (dump_flags & TDF_DETAILS))
4291 fprintf (dump_file, "Omega Analyzer\n");
4292 dump_data_dependence_relation (dump_file, ddr);
4295 /* Check that we get the same information. */
4296 if (maybe_dependent)
4297 gcc_assert (ddr_consistent_p (stderr, ddr, dist_vects,
4298 dir_vects));
4303 /* As a last case, if the dependence cannot be determined, or if
4304 the dependence is considered too difficult to determine, answer
4305 "don't know". */
4306 else
4308 csys_dont_know:;
4309 dependence_stats.num_dependence_undetermined++;
4311 if (dump_file && (dump_flags & TDF_DETAILS))
4313 fprintf (dump_file, "Data ref a:\n");
4314 dump_data_reference (dump_file, dra);
4315 fprintf (dump_file, "Data ref b:\n");
4316 dump_data_reference (dump_file, drb);
4317 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
4319 finalize_ddr_dependent (ddr, chrec_dont_know);
4323 if (dump_file && (dump_flags & TDF_DETAILS))
4325 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4326 fprintf (dump_file, ") -> no dependence\n");
4327 else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
4328 fprintf (dump_file, ") -> dependence analysis failed\n");
4329 else
4330 fprintf (dump_file, ")\n");
4334 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
4335 the data references in DATAREFS, in the LOOP_NEST. When
4336 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
4337 relations. Return true when successful, i.e. data references number
4338 is small enough to be handled. */
4340 bool
4341 compute_all_dependences (vec<data_reference_p> datarefs,
4342 vec<ddr_p> *dependence_relations,
4343 vec<loop_p> loop_nest,
4344 bool compute_self_and_rr)
4346 struct data_dependence_relation *ddr;
4347 struct data_reference *a, *b;
4348 unsigned int i, j;
4350 if ((int) datarefs.length ()
4351 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
4353 struct data_dependence_relation *ddr;
4355 /* Insert a single relation into dependence_relations:
4356 chrec_dont_know. */
4357 ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
4358 dependence_relations->safe_push (ddr);
4359 return false;
4362 FOR_EACH_VEC_ELT (datarefs, i, a)
4363 for (j = i + 1; datarefs.iterate (j, &b); j++)
4364 if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
4366 ddr = initialize_data_dependence_relation (a, b, loop_nest);
4367 dependence_relations->safe_push (ddr);
4368 if (loop_nest.exists ())
4369 compute_affine_dependence (ddr, loop_nest[0]);
4372 if (compute_self_and_rr)
4373 FOR_EACH_VEC_ELT (datarefs, i, a)
4375 ddr = initialize_data_dependence_relation (a, a, loop_nest);
4376 dependence_relations->safe_push (ddr);
4377 if (loop_nest.exists ())
4378 compute_affine_dependence (ddr, loop_nest[0]);
4381 return true;
4384 /* Describes a location of a memory reference. */
4386 typedef struct data_ref_loc_d
4388 /* The memory reference. */
4389 tree ref;
4391 /* True if the memory reference is read. */
4392 bool is_read;
4393 } data_ref_loc;
4396 /* Stores the locations of memory references in STMT to REFERENCES. Returns
4397 true if STMT clobbers memory, false otherwise. */
4399 static bool
4400 get_references_in_stmt (gimple stmt, vec<data_ref_loc, va_heap> *references)
4402 bool clobbers_memory = false;
4403 data_ref_loc ref;
4404 tree op0, op1;
4405 enum gimple_code stmt_code = gimple_code (stmt);
4407 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4408 As we cannot model data-references to not spelled out
4409 accesses give up if they may occur. */
4410 if (stmt_code == GIMPLE_CALL
4411 && !(gimple_call_flags (stmt) & ECF_CONST))
4413 /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
4414 if (gimple_call_internal_p (stmt))
4415 switch (gimple_call_internal_fn (stmt))
4417 case IFN_GOMP_SIMD_LANE:
4419 struct loop *loop = gimple_bb (stmt)->loop_father;
4420 tree uid = gimple_call_arg (stmt, 0);
4421 gcc_assert (TREE_CODE (uid) == SSA_NAME);
4422 if (loop == NULL
4423 || loop->simduid != SSA_NAME_VAR (uid))
4424 clobbers_memory = true;
4425 break;
4427 case IFN_MASK_LOAD:
4428 case IFN_MASK_STORE:
4429 break;
4430 default:
4431 clobbers_memory = true;
4432 break;
4434 else
4435 clobbers_memory = true;
4437 else if (stmt_code == GIMPLE_ASM
4438 && (gimple_asm_volatile_p (as_a <gasm *> (stmt))
4439 || gimple_vuse (stmt)))
4440 clobbers_memory = true;
4442 if (!gimple_vuse (stmt))
4443 return clobbers_memory;
4445 if (stmt_code == GIMPLE_ASSIGN)
4447 tree base;
4448 op0 = gimple_assign_lhs (stmt);
4449 op1 = gimple_assign_rhs1 (stmt);
4451 if (DECL_P (op1)
4452 || (REFERENCE_CLASS_P (op1)
4453 && (base = get_base_address (op1))
4454 && TREE_CODE (base) != SSA_NAME))
4456 ref.ref = op1;
4457 ref.is_read = true;
4458 references->safe_push (ref);
4461 else if (stmt_code == GIMPLE_CALL)
4463 unsigned i, n;
4465 ref.is_read = false;
4466 if (gimple_call_internal_p (stmt))
4467 switch (gimple_call_internal_fn (stmt))
4469 case IFN_MASK_LOAD:
4470 if (gimple_call_lhs (stmt) == NULL_TREE)
4471 break;
4472 ref.is_read = true;
4473 case IFN_MASK_STORE:
4474 ref.ref = fold_build2 (MEM_REF,
4475 ref.is_read
4476 ? TREE_TYPE (gimple_call_lhs (stmt))
4477 : TREE_TYPE (gimple_call_arg (stmt, 3)),
4478 gimple_call_arg (stmt, 0),
4479 gimple_call_arg (stmt, 1));
4480 references->safe_push (ref);
4481 return false;
4482 default:
4483 break;
4486 op0 = gimple_call_lhs (stmt);
4487 n = gimple_call_num_args (stmt);
4488 for (i = 0; i < n; i++)
4490 op1 = gimple_call_arg (stmt, i);
4492 if (DECL_P (op1)
4493 || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
4495 ref.ref = op1;
4496 ref.is_read = true;
4497 references->safe_push (ref);
4501 else
4502 return clobbers_memory;
4504 if (op0
4505 && (DECL_P (op0)
4506 || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
4508 ref.ref = op0;
4509 ref.is_read = false;
4510 references->safe_push (ref);
4512 return clobbers_memory;
4515 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
4516 reference, returns false, otherwise returns true. NEST is the outermost
4517 loop of the loop nest in which the references should be analyzed. */
4519 bool
4520 find_data_references_in_stmt (struct loop *nest, gimple stmt,
4521 vec<data_reference_p> *datarefs)
4523 unsigned i;
4524 auto_vec<data_ref_loc, 2> references;
4525 data_ref_loc *ref;
4526 bool ret = true;
4527 data_reference_p dr;
4529 if (get_references_in_stmt (stmt, &references))
4530 return false;
4532 FOR_EACH_VEC_ELT (references, i, ref)
4534 dr = create_data_ref (nest, loop_containing_stmt (stmt),
4535 ref->ref, stmt, ref->is_read);
4536 gcc_assert (dr != NULL);
4537 datarefs->safe_push (dr);
4539 references.release ();
4540 return ret;
4543 /* Stores the data references in STMT to DATAREFS. If there is an
4544 unanalyzable reference, returns false, otherwise returns true.
4545 NEST is the outermost loop of the loop nest in which the references
4546 should be instantiated, LOOP is the loop in which the references
4547 should be analyzed. */
4549 bool
4550 graphite_find_data_references_in_stmt (loop_p nest, loop_p loop, gimple stmt,
4551 vec<data_reference_p> *datarefs)
4553 unsigned i;
4554 auto_vec<data_ref_loc, 2> references;
4555 data_ref_loc *ref;
4556 bool ret = true;
4557 data_reference_p dr;
4559 if (get_references_in_stmt (stmt, &references))
4560 return false;
4562 FOR_EACH_VEC_ELT (references, i, ref)
4564 dr = create_data_ref (nest, loop, ref->ref, stmt, ref->is_read);
4565 gcc_assert (dr != NULL);
4566 datarefs->safe_push (dr);
4569 references.release ();
4570 return ret;
4573 /* Search the data references in LOOP, and record the information into
4574 DATAREFS. Returns chrec_dont_know when failing to analyze a
4575 difficult case, returns NULL_TREE otherwise. */
4577 tree
4578 find_data_references_in_bb (struct loop *loop, basic_block bb,
4579 vec<data_reference_p> *datarefs)
4581 gimple_stmt_iterator bsi;
4583 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4585 gimple stmt = gsi_stmt (bsi);
4587 if (!find_data_references_in_stmt (loop, stmt, datarefs))
4589 struct data_reference *res;
4590 res = XCNEW (struct data_reference);
4591 datarefs->safe_push (res);
4593 return chrec_dont_know;
4597 return NULL_TREE;
4600 /* Search the data references in LOOP, and record the information into
4601 DATAREFS. Returns chrec_dont_know when failing to analyze a
4602 difficult case, returns NULL_TREE otherwise.
4604 TODO: This function should be made smarter so that it can handle address
4605 arithmetic as if they were array accesses, etc. */
4607 tree
4608 find_data_references_in_loop (struct loop *loop,
4609 vec<data_reference_p> *datarefs)
4611 basic_block bb, *bbs;
4612 unsigned int i;
4614 bbs = get_loop_body_in_dom_order (loop);
4616 for (i = 0; i < loop->num_nodes; i++)
4618 bb = bbs[i];
4620 if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
4622 free (bbs);
4623 return chrec_dont_know;
4626 free (bbs);
4628 return NULL_TREE;
4631 /* Recursive helper function. */
4633 static bool
4634 find_loop_nest_1 (struct loop *loop, vec<loop_p> *loop_nest)
4636 /* Inner loops of the nest should not contain siblings. Example:
4637 when there are two consecutive loops,
4639 | loop_0
4640 | loop_1
4641 | A[{0, +, 1}_1]
4642 | endloop_1
4643 | loop_2
4644 | A[{0, +, 1}_2]
4645 | endloop_2
4646 | endloop_0
4648 the dependence relation cannot be captured by the distance
4649 abstraction. */
4650 if (loop->next)
4651 return false;
4653 loop_nest->safe_push (loop);
4654 if (loop->inner)
4655 return find_loop_nest_1 (loop->inner, loop_nest);
4656 return true;
4659 /* Return false when the LOOP is not well nested. Otherwise return
4660 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4661 contain the loops from the outermost to the innermost, as they will
4662 appear in the classic distance vector. */
4664 bool
4665 find_loop_nest (struct loop *loop, vec<loop_p> *loop_nest)
4667 loop_nest->safe_push (loop);
4668 if (loop->inner)
4669 return find_loop_nest_1 (loop->inner, loop_nest);
4670 return true;
4673 /* Returns true when the data dependences have been computed, false otherwise.
4674 Given a loop nest LOOP, the following vectors are returned:
4675 DATAREFS is initialized to all the array elements contained in this loop,
4676 DEPENDENCE_RELATIONS contains the relations between the data references.
4677 Compute read-read and self relations if
4678 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4680 bool
4681 compute_data_dependences_for_loop (struct loop *loop,
4682 bool compute_self_and_read_read_dependences,
4683 vec<loop_p> *loop_nest,
4684 vec<data_reference_p> *datarefs,
4685 vec<ddr_p> *dependence_relations)
4687 bool res = true;
4689 memset (&dependence_stats, 0, sizeof (dependence_stats));
4691 /* If the loop nest is not well formed, or one of the data references
4692 is not computable, give up without spending time to compute other
4693 dependences. */
4694 if (!loop
4695 || !find_loop_nest (loop, loop_nest)
4696 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
4697 || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
4698 compute_self_and_read_read_dependences))
4699 res = false;
4701 if (dump_file && (dump_flags & TDF_STATS))
4703 fprintf (dump_file, "Dependence tester statistics:\n");
4705 fprintf (dump_file, "Number of dependence tests: %d\n",
4706 dependence_stats.num_dependence_tests);
4707 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
4708 dependence_stats.num_dependence_dependent);
4709 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
4710 dependence_stats.num_dependence_independent);
4711 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
4712 dependence_stats.num_dependence_undetermined);
4714 fprintf (dump_file, "Number of subscript tests: %d\n",
4715 dependence_stats.num_subscript_tests);
4716 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
4717 dependence_stats.num_subscript_undetermined);
4718 fprintf (dump_file, "Number of same subscript function: %d\n",
4719 dependence_stats.num_same_subscript_function);
4721 fprintf (dump_file, "Number of ziv tests: %d\n",
4722 dependence_stats.num_ziv);
4723 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4724 dependence_stats.num_ziv_dependent);
4725 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4726 dependence_stats.num_ziv_independent);
4727 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
4728 dependence_stats.num_ziv_unimplemented);
4730 fprintf (dump_file, "Number of siv tests: %d\n",
4731 dependence_stats.num_siv);
4732 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4733 dependence_stats.num_siv_dependent);
4734 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4735 dependence_stats.num_siv_independent);
4736 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4737 dependence_stats.num_siv_unimplemented);
4739 fprintf (dump_file, "Number of miv tests: %d\n",
4740 dependence_stats.num_miv);
4741 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4742 dependence_stats.num_miv_dependent);
4743 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4744 dependence_stats.num_miv_independent);
4745 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4746 dependence_stats.num_miv_unimplemented);
4749 return res;
4752 /* Returns true when the data dependences for the basic block BB have been
4753 computed, false otherwise.
4754 DATAREFS is initialized to all the array elements contained in this basic
4755 block, DEPENDENCE_RELATIONS contains the relations between the data
4756 references. Compute read-read and self relations if
4757 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4758 bool
4759 compute_data_dependences_for_bb (basic_block bb,
4760 bool compute_self_and_read_read_dependences,
4761 vec<data_reference_p> *datarefs,
4762 vec<ddr_p> *dependence_relations)
4764 if (find_data_references_in_bb (NULL, bb, datarefs) == chrec_dont_know)
4765 return false;
4767 return compute_all_dependences (*datarefs, dependence_relations, vNULL,
4768 compute_self_and_read_read_dependences);
4771 /* Entry point (for testing only). Analyze all the data references
4772 and the dependence relations in LOOP.
4774 The data references are computed first.
4776 A relation on these nodes is represented by a complete graph. Some
4777 of the relations could be of no interest, thus the relations can be
4778 computed on demand.
4780 In the following function we compute all the relations. This is
4781 just a first implementation that is here for:
4782 - for showing how to ask for the dependence relations,
4783 - for the debugging the whole dependence graph,
4784 - for the dejagnu testcases and maintenance.
4786 It is possible to ask only for a part of the graph, avoiding to
4787 compute the whole dependence graph. The computed dependences are
4788 stored in a knowledge base (KB) such that later queries don't
4789 recompute the same information. The implementation of this KB is
4790 transparent to the optimizer, and thus the KB can be changed with a
4791 more efficient implementation, or the KB could be disabled. */
4792 static void
4793 analyze_all_data_dependences (struct loop *loop)
4795 unsigned int i;
4796 int nb_data_refs = 10;
4797 vec<data_reference_p> datarefs;
4798 datarefs.create (nb_data_refs);
4799 vec<ddr_p> dependence_relations;
4800 dependence_relations.create (nb_data_refs * nb_data_refs);
4801 vec<loop_p> loop_nest;
4802 loop_nest.create (3);
4804 /* Compute DDs on the whole function. */
4805 compute_data_dependences_for_loop (loop, false, &loop_nest, &datarefs,
4806 &dependence_relations);
4808 if (dump_file)
4810 dump_data_dependence_relations (dump_file, dependence_relations);
4811 fprintf (dump_file, "\n\n");
4813 if (dump_flags & TDF_DETAILS)
4814 dump_dist_dir_vectors (dump_file, dependence_relations);
4816 if (dump_flags & TDF_STATS)
4818 unsigned nb_top_relations = 0;
4819 unsigned nb_bot_relations = 0;
4820 unsigned nb_chrec_relations = 0;
4821 struct data_dependence_relation *ddr;
4823 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4825 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
4826 nb_top_relations++;
4828 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4829 nb_bot_relations++;
4831 else
4832 nb_chrec_relations++;
4835 gather_stats_on_scev_database ();
4839 loop_nest.release ();
4840 free_dependence_relations (dependence_relations);
4841 free_data_refs (datarefs);
4844 /* Computes all the data dependences and check that the results of
4845 several analyzers are the same. */
4847 void
4848 tree_check_data_deps (void)
4850 struct loop *loop_nest;
4852 FOR_EACH_LOOP (loop_nest, 0)
4853 analyze_all_data_dependences (loop_nest);
4856 /* Free the memory used by a data dependence relation DDR. */
4858 void
4859 free_dependence_relation (struct data_dependence_relation *ddr)
4861 if (ddr == NULL)
4862 return;
4864 if (DDR_SUBSCRIPTS (ddr).exists ())
4865 free_subscripts (DDR_SUBSCRIPTS (ddr));
4866 DDR_DIST_VECTS (ddr).release ();
4867 DDR_DIR_VECTS (ddr).release ();
4869 free (ddr);
4872 /* Free the memory used by the data dependence relations from
4873 DEPENDENCE_RELATIONS. */
4875 void
4876 free_dependence_relations (vec<ddr_p> dependence_relations)
4878 unsigned int i;
4879 struct data_dependence_relation *ddr;
4881 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4882 if (ddr)
4883 free_dependence_relation (ddr);
4885 dependence_relations.release ();
4888 /* Free the memory used by the data references from DATAREFS. */
4890 void
4891 free_data_refs (vec<data_reference_p> datarefs)
4893 unsigned int i;
4894 struct data_reference *dr;
4896 FOR_EACH_VEC_ELT (datarefs, i, dr)
4897 free_data_ref (dr);
4898 datarefs.release ();