* config/rs6000/rs6000.c (rs6000_xcoff_asm_named_section): Place
[official-gcc.git] / gcc / tree-data-ref.c
blobe7087d7ebec94322a95d9ab3ce4cfb5d01762507
1 /* Data references and dependences detectors.
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This pass walks a given loop structure searching for array
22 references. The information about the array accesses is recorded
23 in DATA_REFERENCE structures.
25 The basic test for determining the dependences is:
26 given two access functions chrec1 and chrec2 to a same array, and
27 x and y two vectors from the iteration domain, the same element of
28 the array is accessed twice at iterations x and y if and only if:
29 | chrec1 (x) == chrec2 (y).
31 The goals of this analysis are:
33 - to determine the independence: the relation between two
34 independent accesses is qualified with the chrec_known (this
35 information allows a loop parallelization),
37 - when two data references access the same data, to qualify the
38 dependence relation with classic dependence representations:
40 - distance vectors
41 - direction vectors
42 - loop carried level dependence
43 - polyhedron dependence
44 or with the chains of recurrences based representation,
46 - to define a knowledge base for storing the data dependence
47 information,
49 - to define an interface to access this data.
52 Definitions:
54 - subscript: given two array accesses a subscript is the tuple
55 composed of the access functions for a given dimension. Example:
56 Given A[f1][f2][f3] and B[g1][g2][g3], there are three subscripts:
57 (f1, g1), (f2, g2), (f3, g3).
59 - Diophantine equation: an equation whose coefficients and
60 solutions are integer constants, for example the equation
61 | 3*x + 2*y = 1
62 has an integer solution x = 1 and y = -1.
64 References:
66 - "Advanced Compilation for High Performance Computing" by Randy
67 Allen and Ken Kennedy.
68 http://citeseer.ist.psu.edu/goff91practical.html
70 - "Loop Transformations for Restructuring Compilers - The Foundations"
71 by Utpal Banerjee.
76 #include "config.h"
77 #include "system.h"
78 #include "coretypes.h"
79 #include "alias.h"
80 #include "backend.h"
81 #include "tree.h"
82 #include "gimple.h"
83 #include "rtl.h"
84 #include "options.h"
85 #include "fold-const.h"
86 #include "flags.h"
87 #include "insn-config.h"
88 #include "expmed.h"
89 #include "dojump.h"
90 #include "explow.h"
91 #include "calls.h"
92 #include "emit-rtl.h"
93 #include "varasm.h"
94 #include "stmt.h"
95 #include "expr.h"
96 #include "gimple-pretty-print.h"
97 #include "internal-fn.h"
98 #include "gimple-iterator.h"
99 #include "tree-ssa-loop-niter.h"
100 #include "tree-ssa-loop.h"
101 #include "tree-ssa.h"
102 #include "cfgloop.h"
103 #include "tree-data-ref.h"
104 #include "tree-scalar-evolution.h"
105 #include "dumpfile.h"
106 #include "langhooks.h"
107 #include "tree-affine.h"
108 #include "params.h"
110 static struct datadep_stats
112 int num_dependence_tests;
113 int num_dependence_dependent;
114 int num_dependence_independent;
115 int num_dependence_undetermined;
117 int num_subscript_tests;
118 int num_subscript_undetermined;
119 int num_same_subscript_function;
121 int num_ziv;
122 int num_ziv_independent;
123 int num_ziv_dependent;
124 int num_ziv_unimplemented;
126 int num_siv;
127 int num_siv_independent;
128 int num_siv_dependent;
129 int num_siv_unimplemented;
131 int num_miv;
132 int num_miv_independent;
133 int num_miv_dependent;
134 int num_miv_unimplemented;
135 } dependence_stats;
137 static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
138 struct data_reference *,
139 struct data_reference *,
140 struct loop *);
141 /* Returns true iff A divides B. */
143 static inline bool
144 tree_fold_divides_p (const_tree a, const_tree b)
146 gcc_assert (TREE_CODE (a) == INTEGER_CST);
147 gcc_assert (TREE_CODE (b) == INTEGER_CST);
148 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a));
151 /* Returns true iff A divides B. */
153 static inline bool
154 int_divides_p (int a, int b)
156 return ((b % a) == 0);
161 /* Dump into FILE all the data references from DATAREFS. */
163 static void
164 dump_data_references (FILE *file, vec<data_reference_p> datarefs)
166 unsigned int i;
167 struct data_reference *dr;
169 FOR_EACH_VEC_ELT (datarefs, i, dr)
170 dump_data_reference (file, dr);
173 /* Unified dump into FILE all the data references from DATAREFS. */
175 DEBUG_FUNCTION void
176 debug (vec<data_reference_p> &ref)
178 dump_data_references (stderr, ref);
181 DEBUG_FUNCTION void
182 debug (vec<data_reference_p> *ptr)
184 if (ptr)
185 debug (*ptr);
186 else
187 fprintf (stderr, "<nil>\n");
191 /* Dump into STDERR all the data references from DATAREFS. */
193 DEBUG_FUNCTION void
194 debug_data_references (vec<data_reference_p> datarefs)
196 dump_data_references (stderr, datarefs);
199 /* Print to STDERR the data_reference DR. */
201 DEBUG_FUNCTION void
202 debug_data_reference (struct data_reference *dr)
204 dump_data_reference (stderr, dr);
207 /* Dump function for a DATA_REFERENCE structure. */
209 void
210 dump_data_reference (FILE *outf,
211 struct data_reference *dr)
213 unsigned int i;
215 fprintf (outf, "#(Data Ref: \n");
216 fprintf (outf, "# bb: %d \n", gimple_bb (DR_STMT (dr))->index);
217 fprintf (outf, "# stmt: ");
218 print_gimple_stmt (outf, DR_STMT (dr), 0, 0);
219 fprintf (outf, "# ref: ");
220 print_generic_stmt (outf, DR_REF (dr), 0);
221 fprintf (outf, "# base_object: ");
222 print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
224 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
226 fprintf (outf, "# Access function %d: ", i);
227 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
229 fprintf (outf, "#)\n");
232 /* Unified dump function for a DATA_REFERENCE structure. */
234 DEBUG_FUNCTION void
235 debug (data_reference &ref)
237 dump_data_reference (stderr, &ref);
240 DEBUG_FUNCTION void
241 debug (data_reference *ptr)
243 if (ptr)
244 debug (*ptr);
245 else
246 fprintf (stderr, "<nil>\n");
250 /* Dumps the affine function described by FN to the file OUTF. */
252 DEBUG_FUNCTION void
253 dump_affine_function (FILE *outf, affine_fn fn)
255 unsigned i;
256 tree coef;
258 print_generic_expr (outf, fn[0], TDF_SLIM);
259 for (i = 1; fn.iterate (i, &coef); i++)
261 fprintf (outf, " + ");
262 print_generic_expr (outf, coef, TDF_SLIM);
263 fprintf (outf, " * x_%u", i);
267 /* Dumps the conflict function CF to the file OUTF. */
269 DEBUG_FUNCTION void
270 dump_conflict_function (FILE *outf, conflict_function *cf)
272 unsigned i;
274 if (cf->n == NO_DEPENDENCE)
275 fprintf (outf, "no dependence");
276 else if (cf->n == NOT_KNOWN)
277 fprintf (outf, "not known");
278 else
280 for (i = 0; i < cf->n; i++)
282 if (i != 0)
283 fprintf (outf, " ");
284 fprintf (outf, "[");
285 dump_affine_function (outf, cf->fns[i]);
286 fprintf (outf, "]");
291 /* Dump function for a SUBSCRIPT structure. */
293 DEBUG_FUNCTION void
294 dump_subscript (FILE *outf, struct subscript *subscript)
296 conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
298 fprintf (outf, "\n (subscript \n");
299 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
300 dump_conflict_function (outf, cf);
301 if (CF_NONTRIVIAL_P (cf))
303 tree last_iteration = SUB_LAST_CONFLICT (subscript);
304 fprintf (outf, "\n last_conflict: ");
305 print_generic_expr (outf, last_iteration, 0);
308 cf = SUB_CONFLICTS_IN_B (subscript);
309 fprintf (outf, "\n iterations_that_access_an_element_twice_in_B: ");
310 dump_conflict_function (outf, cf);
311 if (CF_NONTRIVIAL_P (cf))
313 tree last_iteration = SUB_LAST_CONFLICT (subscript);
314 fprintf (outf, "\n last_conflict: ");
315 print_generic_expr (outf, last_iteration, 0);
318 fprintf (outf, "\n (Subscript distance: ");
319 print_generic_expr (outf, SUB_DISTANCE (subscript), 0);
320 fprintf (outf, " ))\n");
323 /* Print the classic direction vector DIRV to OUTF. */
325 DEBUG_FUNCTION void
326 print_direction_vector (FILE *outf,
327 lambda_vector dirv,
328 int length)
330 int eq;
332 for (eq = 0; eq < length; eq++)
334 enum data_dependence_direction dir = ((enum data_dependence_direction)
335 dirv[eq]);
337 switch (dir)
339 case dir_positive:
340 fprintf (outf, " +");
341 break;
342 case dir_negative:
343 fprintf (outf, " -");
344 break;
345 case dir_equal:
346 fprintf (outf, " =");
347 break;
348 case dir_positive_or_equal:
349 fprintf (outf, " +=");
350 break;
351 case dir_positive_or_negative:
352 fprintf (outf, " +-");
353 break;
354 case dir_negative_or_equal:
355 fprintf (outf, " -=");
356 break;
357 case dir_star:
358 fprintf (outf, " *");
359 break;
360 default:
361 fprintf (outf, "indep");
362 break;
365 fprintf (outf, "\n");
368 /* Print a vector of direction vectors. */
370 DEBUG_FUNCTION void
371 print_dir_vectors (FILE *outf, vec<lambda_vector> dir_vects,
372 int length)
374 unsigned j;
375 lambda_vector v;
377 FOR_EACH_VEC_ELT (dir_vects, j, v)
378 print_direction_vector (outf, v, length);
381 /* Print out a vector VEC of length N to OUTFILE. */
383 DEBUG_FUNCTION void
384 print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
386 int i;
388 for (i = 0; i < n; i++)
389 fprintf (outfile, "%3d ", vector[i]);
390 fprintf (outfile, "\n");
393 /* Print a vector of distance vectors. */
395 DEBUG_FUNCTION void
396 print_dist_vectors (FILE *outf, vec<lambda_vector> dist_vects,
397 int length)
399 unsigned j;
400 lambda_vector v;
402 FOR_EACH_VEC_ELT (dist_vects, j, v)
403 print_lambda_vector (outf, v, length);
406 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
408 DEBUG_FUNCTION void
409 dump_data_dependence_relation (FILE *outf,
410 struct data_dependence_relation *ddr)
412 struct data_reference *dra, *drb;
414 fprintf (outf, "(Data Dep: \n");
416 if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
418 if (ddr)
420 dra = DDR_A (ddr);
421 drb = DDR_B (ddr);
422 if (dra)
423 dump_data_reference (outf, dra);
424 else
425 fprintf (outf, " (nil)\n");
426 if (drb)
427 dump_data_reference (outf, drb);
428 else
429 fprintf (outf, " (nil)\n");
431 fprintf (outf, " (don't know)\n)\n");
432 return;
435 dra = DDR_A (ddr);
436 drb = DDR_B (ddr);
437 dump_data_reference (outf, dra);
438 dump_data_reference (outf, drb);
440 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
441 fprintf (outf, " (no dependence)\n");
443 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
445 unsigned int i;
446 struct loop *loopi;
448 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
450 fprintf (outf, " access_fn_A: ");
451 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
452 fprintf (outf, " access_fn_B: ");
453 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
454 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
457 fprintf (outf, " inner loop index: %d\n", DDR_INNER_LOOP (ddr));
458 fprintf (outf, " loop nest: (");
459 FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
460 fprintf (outf, "%d ", loopi->num);
461 fprintf (outf, ")\n");
463 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
465 fprintf (outf, " distance_vector: ");
466 print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
467 DDR_NB_LOOPS (ddr));
470 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
472 fprintf (outf, " direction_vector: ");
473 print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
474 DDR_NB_LOOPS (ddr));
478 fprintf (outf, ")\n");
481 /* Debug version. */
483 DEBUG_FUNCTION void
484 debug_data_dependence_relation (struct data_dependence_relation *ddr)
486 dump_data_dependence_relation (stderr, ddr);
489 /* Dump into FILE all the dependence relations from DDRS. */
491 DEBUG_FUNCTION void
492 dump_data_dependence_relations (FILE *file,
493 vec<ddr_p> ddrs)
495 unsigned int i;
496 struct data_dependence_relation *ddr;
498 FOR_EACH_VEC_ELT (ddrs, i, ddr)
499 dump_data_dependence_relation (file, ddr);
502 DEBUG_FUNCTION void
503 debug (vec<ddr_p> &ref)
505 dump_data_dependence_relations (stderr, ref);
508 DEBUG_FUNCTION void
509 debug (vec<ddr_p> *ptr)
511 if (ptr)
512 debug (*ptr);
513 else
514 fprintf (stderr, "<nil>\n");
518 /* Dump to STDERR all the dependence relations from DDRS. */
520 DEBUG_FUNCTION void
521 debug_data_dependence_relations (vec<ddr_p> ddrs)
523 dump_data_dependence_relations (stderr, ddrs);
526 /* Dumps the distance and direction vectors in FILE. DDRS contains
527 the dependence relations, and VECT_SIZE is the size of the
528 dependence vectors, or in other words the number of loops in the
529 considered nest. */
531 DEBUG_FUNCTION void
532 dump_dist_dir_vectors (FILE *file, vec<ddr_p> ddrs)
534 unsigned int i, j;
535 struct data_dependence_relation *ddr;
536 lambda_vector v;
538 FOR_EACH_VEC_ELT (ddrs, i, ddr)
539 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
541 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), j, v)
543 fprintf (file, "DISTANCE_V (");
544 print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
545 fprintf (file, ")\n");
548 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), j, v)
550 fprintf (file, "DIRECTION_V (");
551 print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
552 fprintf (file, ")\n");
556 fprintf (file, "\n\n");
559 /* Dumps the data dependence relations DDRS in FILE. */
561 DEBUG_FUNCTION void
562 dump_ddrs (FILE *file, vec<ddr_p> ddrs)
564 unsigned int i;
565 struct data_dependence_relation *ddr;
567 FOR_EACH_VEC_ELT (ddrs, i, ddr)
568 dump_data_dependence_relation (file, ddr);
570 fprintf (file, "\n\n");
573 DEBUG_FUNCTION void
574 debug_ddrs (vec<ddr_p> ddrs)
576 dump_ddrs (stderr, ddrs);
579 /* Helper function for split_constant_offset. Expresses OP0 CODE OP1
580 (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
581 constant of type ssizetype, and returns true. If we cannot do this
582 with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
583 is returned. */
585 static bool
586 split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
587 tree *var, tree *off)
589 tree var0, var1;
590 tree off0, off1;
591 enum tree_code ocode = code;
593 *var = NULL_TREE;
594 *off = NULL_TREE;
596 switch (code)
598 case INTEGER_CST:
599 *var = build_int_cst (type, 0);
600 *off = fold_convert (ssizetype, op0);
601 return true;
603 case POINTER_PLUS_EXPR:
604 ocode = PLUS_EXPR;
605 /* FALLTHROUGH */
606 case PLUS_EXPR:
607 case MINUS_EXPR:
608 split_constant_offset (op0, &var0, &off0);
609 split_constant_offset (op1, &var1, &off1);
610 *var = fold_build2 (code, type, var0, var1);
611 *off = size_binop (ocode, off0, off1);
612 return true;
614 case MULT_EXPR:
615 if (TREE_CODE (op1) != INTEGER_CST)
616 return false;
618 split_constant_offset (op0, &var0, &off0);
619 *var = fold_build2 (MULT_EXPR, type, var0, op1);
620 *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
621 return true;
623 case ADDR_EXPR:
625 tree base, poffset;
626 HOST_WIDE_INT pbitsize, pbitpos;
627 machine_mode pmode;
628 int punsignedp, pvolatilep;
630 op0 = TREE_OPERAND (op0, 0);
631 base = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset,
632 &pmode, &punsignedp, &pvolatilep, false);
634 if (pbitpos % BITS_PER_UNIT != 0)
635 return false;
636 base = build_fold_addr_expr (base);
637 off0 = ssize_int (pbitpos / BITS_PER_UNIT);
639 if (poffset)
641 split_constant_offset (poffset, &poffset, &off1);
642 off0 = size_binop (PLUS_EXPR, off0, off1);
643 if (POINTER_TYPE_P (TREE_TYPE (base)))
644 base = fold_build_pointer_plus (base, poffset);
645 else
646 base = fold_build2 (PLUS_EXPR, TREE_TYPE (base), base,
647 fold_convert (TREE_TYPE (base), poffset));
650 var0 = fold_convert (type, base);
652 /* If variable length types are involved, punt, otherwise casts
653 might be converted into ARRAY_REFs in gimplify_conversion.
654 To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
655 possibly no longer appears in current GIMPLE, might resurface.
656 This perhaps could run
657 if (CONVERT_EXPR_P (var0))
659 gimplify_conversion (&var0);
660 // Attempt to fill in any within var0 found ARRAY_REF's
661 // element size from corresponding op embedded ARRAY_REF,
662 // if unsuccessful, just punt.
663 } */
664 while (POINTER_TYPE_P (type))
665 type = TREE_TYPE (type);
666 if (int_size_in_bytes (type) < 0)
667 return false;
669 *var = var0;
670 *off = off0;
671 return true;
674 case SSA_NAME:
676 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
677 return false;
679 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
680 enum tree_code subcode;
682 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
683 return false;
685 var0 = gimple_assign_rhs1 (def_stmt);
686 subcode = gimple_assign_rhs_code (def_stmt);
687 var1 = gimple_assign_rhs2 (def_stmt);
689 return split_constant_offset_1 (type, var0, subcode, var1, var, off);
691 CASE_CONVERT:
693 /* We must not introduce undefined overflow, and we must not change the value.
694 Hence we're okay if the inner type doesn't overflow to start with
695 (pointer or signed), the outer type also is an integer or pointer
696 and the outer precision is at least as large as the inner. */
697 tree itype = TREE_TYPE (op0);
698 if ((POINTER_TYPE_P (itype)
699 || (INTEGRAL_TYPE_P (itype) && TYPE_OVERFLOW_UNDEFINED (itype)))
700 && TYPE_PRECISION (type) >= TYPE_PRECISION (itype)
701 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)))
703 split_constant_offset (op0, &var0, off);
704 *var = fold_convert (type, var0);
705 return true;
707 return false;
710 default:
711 return false;
715 /* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
716 will be ssizetype. */
718 void
719 split_constant_offset (tree exp, tree *var, tree *off)
721 tree type = TREE_TYPE (exp), otype, op0, op1, e, o;
722 enum tree_code code;
724 *var = exp;
725 *off = ssize_int (0);
726 STRIP_NOPS (exp);
728 if (tree_is_chrec (exp)
729 || get_gimple_rhs_class (TREE_CODE (exp)) == GIMPLE_TERNARY_RHS)
730 return;
732 otype = TREE_TYPE (exp);
733 code = TREE_CODE (exp);
734 extract_ops_from_tree (exp, &code, &op0, &op1);
735 if (split_constant_offset_1 (otype, op0, code, op1, &e, &o))
737 *var = fold_convert (type, e);
738 *off = o;
742 /* Returns the address ADDR of an object in a canonical shape (without nop
743 casts, and with type of pointer to the object). */
745 static tree
746 canonicalize_base_object_address (tree addr)
748 tree orig = addr;
750 STRIP_NOPS (addr);
752 /* The base address may be obtained by casting from integer, in that case
753 keep the cast. */
754 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
755 return orig;
757 if (TREE_CODE (addr) != ADDR_EXPR)
758 return addr;
760 return build_fold_addr_expr (TREE_OPERAND (addr, 0));
763 /* Analyzes the behavior of the memory reference DR in the innermost loop or
764 basic block that contains it. Returns true if analysis succeed or false
765 otherwise. */
767 bool
768 dr_analyze_innermost (struct data_reference *dr, struct loop *nest)
770 gimple *stmt = DR_STMT (dr);
771 struct loop *loop = loop_containing_stmt (stmt);
772 tree ref = DR_REF (dr);
773 HOST_WIDE_INT pbitsize, pbitpos;
774 tree base, poffset;
775 machine_mode pmode;
776 int punsignedp, pvolatilep;
777 affine_iv base_iv, offset_iv;
778 tree init, dinit, step;
779 bool in_loop = (loop && loop->num);
781 if (dump_file && (dump_flags & TDF_DETAILS))
782 fprintf (dump_file, "analyze_innermost: ");
784 base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset,
785 &pmode, &punsignedp, &pvolatilep, false);
786 gcc_assert (base != NULL_TREE);
788 if (pbitpos % BITS_PER_UNIT != 0)
790 if (dump_file && (dump_flags & TDF_DETAILS))
791 fprintf (dump_file, "failed: bit offset alignment.\n");
792 return false;
795 if (TREE_CODE (base) == MEM_REF)
797 if (!integer_zerop (TREE_OPERAND (base, 1)))
799 offset_int moff = mem_ref_offset (base);
800 tree mofft = wide_int_to_tree (sizetype, moff);
801 if (!poffset)
802 poffset = mofft;
803 else
804 poffset = size_binop (PLUS_EXPR, poffset, mofft);
806 base = TREE_OPERAND (base, 0);
808 else
809 base = build_fold_addr_expr (base);
811 if (in_loop)
813 if (!simple_iv (loop, loop_containing_stmt (stmt), base, &base_iv,
814 nest ? true : false))
816 if (nest)
818 if (dump_file && (dump_flags & TDF_DETAILS))
819 fprintf (dump_file, "failed: evolution of base is not"
820 " affine.\n");
821 return false;
823 else
825 base_iv.base = base;
826 base_iv.step = ssize_int (0);
827 base_iv.no_overflow = true;
831 else
833 base_iv.base = base;
834 base_iv.step = ssize_int (0);
835 base_iv.no_overflow = true;
838 if (!poffset)
840 offset_iv.base = ssize_int (0);
841 offset_iv.step = ssize_int (0);
843 else
845 if (!in_loop)
847 offset_iv.base = poffset;
848 offset_iv.step = ssize_int (0);
850 else if (!simple_iv (loop, loop_containing_stmt (stmt),
851 poffset, &offset_iv,
852 nest ? true : false))
854 if (nest)
856 if (dump_file && (dump_flags & TDF_DETAILS))
857 fprintf (dump_file, "failed: evolution of offset is not"
858 " affine.\n");
859 return false;
861 else
863 offset_iv.base = poffset;
864 offset_iv.step = ssize_int (0);
869 init = ssize_int (pbitpos / BITS_PER_UNIT);
870 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
871 init = size_binop (PLUS_EXPR, init, dinit);
872 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
873 init = size_binop (PLUS_EXPR, init, dinit);
875 step = size_binop (PLUS_EXPR,
876 fold_convert (ssizetype, base_iv.step),
877 fold_convert (ssizetype, offset_iv.step));
879 DR_BASE_ADDRESS (dr) = canonicalize_base_object_address (base_iv.base);
881 DR_OFFSET (dr) = fold_convert (ssizetype, offset_iv.base);
882 DR_INIT (dr) = init;
883 DR_STEP (dr) = step;
885 DR_ALIGNED_TO (dr) = size_int (highest_pow2_factor (offset_iv.base));
887 if (dump_file && (dump_flags & TDF_DETAILS))
888 fprintf (dump_file, "success.\n");
890 return true;
893 /* Determines the base object and the list of indices of memory reference
894 DR, analyzed in LOOP and instantiated in loop nest NEST. */
896 static void
897 dr_analyze_indices (struct data_reference *dr, loop_p nest, loop_p loop)
899 vec<tree> access_fns = vNULL;
900 tree ref, op;
901 tree base, off, access_fn;
902 basic_block before_loop;
904 /* If analyzing a basic-block there are no indices to analyze
905 and thus no access functions. */
906 if (!nest)
908 DR_BASE_OBJECT (dr) = DR_REF (dr);
909 DR_ACCESS_FNS (dr).create (0);
910 return;
913 ref = DR_REF (dr);
914 before_loop = block_before_loop (nest);
916 /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
917 into a two element array with a constant index. The base is
918 then just the immediate underlying object. */
919 if (TREE_CODE (ref) == REALPART_EXPR)
921 ref = TREE_OPERAND (ref, 0);
922 access_fns.safe_push (integer_zero_node);
924 else if (TREE_CODE (ref) == IMAGPART_EXPR)
926 ref = TREE_OPERAND (ref, 0);
927 access_fns.safe_push (integer_one_node);
930 /* Analyze access functions of dimensions we know to be independent. */
931 while (handled_component_p (ref))
933 if (TREE_CODE (ref) == ARRAY_REF)
935 op = TREE_OPERAND (ref, 1);
936 access_fn = analyze_scalar_evolution (loop, op);
937 access_fn = instantiate_scev (before_loop, loop, access_fn);
938 access_fns.safe_push (access_fn);
940 else if (TREE_CODE (ref) == COMPONENT_REF
941 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
943 /* For COMPONENT_REFs of records (but not unions!) use the
944 FIELD_DECL offset as constant access function so we can
945 disambiguate a[i].f1 and a[i].f2. */
946 tree off = component_ref_field_offset (ref);
947 off = size_binop (PLUS_EXPR,
948 size_binop (MULT_EXPR,
949 fold_convert (bitsizetype, off),
950 bitsize_int (BITS_PER_UNIT)),
951 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
952 access_fns.safe_push (off);
954 else
955 /* If we have an unhandled component we could not translate
956 to an access function stop analyzing. We have determined
957 our base object in this case. */
958 break;
960 ref = TREE_OPERAND (ref, 0);
963 /* If the address operand of a MEM_REF base has an evolution in the
964 analyzed nest, add it as an additional independent access-function. */
965 if (TREE_CODE (ref) == MEM_REF)
967 op = TREE_OPERAND (ref, 0);
968 access_fn = analyze_scalar_evolution (loop, op);
969 access_fn = instantiate_scev (before_loop, loop, access_fn);
970 if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
972 tree orig_type;
973 tree memoff = TREE_OPERAND (ref, 1);
974 base = initial_condition (access_fn);
975 orig_type = TREE_TYPE (base);
976 STRIP_USELESS_TYPE_CONVERSION (base);
977 split_constant_offset (base, &base, &off);
978 STRIP_USELESS_TYPE_CONVERSION (base);
979 /* Fold the MEM_REF offset into the evolutions initial
980 value to make more bases comparable. */
981 if (!integer_zerop (memoff))
983 off = size_binop (PLUS_EXPR, off,
984 fold_convert (ssizetype, memoff));
985 memoff = build_int_cst (TREE_TYPE (memoff), 0);
987 /* Adjust the offset so it is a multiple of the access type
988 size and thus we separate bases that can possibly be used
989 to produce partial overlaps (which the access_fn machinery
990 cannot handle). */
991 wide_int rem;
992 if (TYPE_SIZE_UNIT (TREE_TYPE (ref))
993 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref))) == INTEGER_CST
994 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (ref))))
995 rem = wi::mod_trunc (off, TYPE_SIZE_UNIT (TREE_TYPE (ref)), SIGNED);
996 else
997 /* If we can't compute the remainder simply force the initial
998 condition to zero. */
999 rem = off;
1000 off = wide_int_to_tree (ssizetype, wi::sub (off, rem));
1001 memoff = wide_int_to_tree (TREE_TYPE (memoff), rem);
1002 /* And finally replace the initial condition. */
1003 access_fn = chrec_replace_initial_condition
1004 (access_fn, fold_convert (orig_type, off));
1005 /* ??? This is still not a suitable base object for
1006 dr_may_alias_p - the base object needs to be an
1007 access that covers the object as whole. With
1008 an evolution in the pointer this cannot be
1009 guaranteed.
1010 As a band-aid, mark the access so we can special-case
1011 it in dr_may_alias_p. */
1012 tree old = ref;
1013 ref = fold_build2_loc (EXPR_LOCATION (ref),
1014 MEM_REF, TREE_TYPE (ref),
1015 base, memoff);
1016 MR_DEPENDENCE_CLIQUE (ref) = MR_DEPENDENCE_CLIQUE (old);
1017 MR_DEPENDENCE_BASE (ref) = MR_DEPENDENCE_BASE (old);
1018 DR_UNCONSTRAINED_BASE (dr) = true;
1019 access_fns.safe_push (access_fn);
1022 else if (DECL_P (ref))
1024 /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
1025 ref = build2 (MEM_REF, TREE_TYPE (ref),
1026 build_fold_addr_expr (ref),
1027 build_int_cst (reference_alias_ptr_type (ref), 0));
1030 DR_BASE_OBJECT (dr) = ref;
1031 DR_ACCESS_FNS (dr) = access_fns;
1034 /* Extracts the alias analysis information from the memory reference DR. */
1036 static void
1037 dr_analyze_alias (struct data_reference *dr)
1039 tree ref = DR_REF (dr);
1040 tree base = get_base_address (ref), addr;
1042 if (INDIRECT_REF_P (base)
1043 || TREE_CODE (base) == MEM_REF)
1045 addr = TREE_OPERAND (base, 0);
1046 if (TREE_CODE (addr) == SSA_NAME)
1047 DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
1051 /* Frees data reference DR. */
1053 void
1054 free_data_ref (data_reference_p dr)
1056 DR_ACCESS_FNS (dr).release ();
1057 free (dr);
1060 /* Analyzes memory reference MEMREF accessed in STMT. The reference
1061 is read if IS_READ is true, write otherwise. Returns the
1062 data_reference description of MEMREF. NEST is the outermost loop
1063 in which the reference should be instantiated, LOOP is the loop in
1064 which the data reference should be analyzed. */
1066 struct data_reference *
1067 create_data_ref (loop_p nest, loop_p loop, tree memref, gimple *stmt,
1068 bool is_read)
1070 struct data_reference *dr;
1072 if (dump_file && (dump_flags & TDF_DETAILS))
1074 fprintf (dump_file, "Creating dr for ");
1075 print_generic_expr (dump_file, memref, TDF_SLIM);
1076 fprintf (dump_file, "\n");
1079 dr = XCNEW (struct data_reference);
1080 DR_STMT (dr) = stmt;
1081 DR_REF (dr) = memref;
1082 DR_IS_READ (dr) = is_read;
1084 dr_analyze_innermost (dr, nest);
1085 dr_analyze_indices (dr, nest, loop);
1086 dr_analyze_alias (dr);
1088 if (dump_file && (dump_flags & TDF_DETAILS))
1090 unsigned i;
1091 fprintf (dump_file, "\tbase_address: ");
1092 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1093 fprintf (dump_file, "\n\toffset from base address: ");
1094 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1095 fprintf (dump_file, "\n\tconstant offset from base address: ");
1096 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1097 fprintf (dump_file, "\n\tstep: ");
1098 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1099 fprintf (dump_file, "\n\taligned to: ");
1100 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
1101 fprintf (dump_file, "\n\tbase_object: ");
1102 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1103 fprintf (dump_file, "\n");
1104 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
1106 fprintf (dump_file, "\tAccess function %d: ", i);
1107 print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
1111 return dr;
1114 /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
1115 expressions. */
1116 static bool
1117 dr_equal_offsets_p1 (tree offset1, tree offset2)
1119 bool res;
1121 STRIP_NOPS (offset1);
1122 STRIP_NOPS (offset2);
1124 if (offset1 == offset2)
1125 return true;
1127 if (TREE_CODE (offset1) != TREE_CODE (offset2)
1128 || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
1129 return false;
1131 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
1132 TREE_OPERAND (offset2, 0));
1134 if (!res || !BINARY_CLASS_P (offset1))
1135 return res;
1137 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
1138 TREE_OPERAND (offset2, 1));
1140 return res;
1143 /* Check if DRA and DRB have equal offsets. */
1144 bool
1145 dr_equal_offsets_p (struct data_reference *dra,
1146 struct data_reference *drb)
1148 tree offset1, offset2;
1150 offset1 = DR_OFFSET (dra);
1151 offset2 = DR_OFFSET (drb);
1153 return dr_equal_offsets_p1 (offset1, offset2);
1156 /* Returns true if FNA == FNB. */
1158 static bool
1159 affine_function_equal_p (affine_fn fna, affine_fn fnb)
1161 unsigned i, n = fna.length ();
1163 if (n != fnb.length ())
1164 return false;
1166 for (i = 0; i < n; i++)
1167 if (!operand_equal_p (fna[i], fnb[i], 0))
1168 return false;
1170 return true;
1173 /* If all the functions in CF are the same, returns one of them,
1174 otherwise returns NULL. */
1176 static affine_fn
1177 common_affine_function (conflict_function *cf)
1179 unsigned i;
1180 affine_fn comm;
1182 if (!CF_NONTRIVIAL_P (cf))
1183 return affine_fn ();
1185 comm = cf->fns[0];
1187 for (i = 1; i < cf->n; i++)
1188 if (!affine_function_equal_p (comm, cf->fns[i]))
1189 return affine_fn ();
1191 return comm;
1194 /* Returns the base of the affine function FN. */
1196 static tree
1197 affine_function_base (affine_fn fn)
1199 return fn[0];
1202 /* Returns true if FN is a constant. */
1204 static bool
1205 affine_function_constant_p (affine_fn fn)
1207 unsigned i;
1208 tree coef;
1210 for (i = 1; fn.iterate (i, &coef); i++)
1211 if (!integer_zerop (coef))
1212 return false;
1214 return true;
1217 /* Returns true if FN is the zero constant function. */
1219 static bool
1220 affine_function_zero_p (affine_fn fn)
1222 return (integer_zerop (affine_function_base (fn))
1223 && affine_function_constant_p (fn));
1226 /* Returns a signed integer type with the largest precision from TA
1227 and TB. */
1229 static tree
1230 signed_type_for_types (tree ta, tree tb)
1232 if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
1233 return signed_type_for (ta);
1234 else
1235 return signed_type_for (tb);
1238 /* Applies operation OP on affine functions FNA and FNB, and returns the
1239 result. */
1241 static affine_fn
1242 affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
1244 unsigned i, n, m;
1245 affine_fn ret;
1246 tree coef;
1248 if (fnb.length () > fna.length ())
1250 n = fna.length ();
1251 m = fnb.length ();
1253 else
1255 n = fnb.length ();
1256 m = fna.length ();
1259 ret.create (m);
1260 for (i = 0; i < n; i++)
1262 tree type = signed_type_for_types (TREE_TYPE (fna[i]),
1263 TREE_TYPE (fnb[i]));
1264 ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
1267 for (; fna.iterate (i, &coef); i++)
1268 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1269 coef, integer_zero_node));
1270 for (; fnb.iterate (i, &coef); i++)
1271 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1272 integer_zero_node, coef));
1274 return ret;
1277 /* Returns the sum of affine functions FNA and FNB. */
1279 static affine_fn
1280 affine_fn_plus (affine_fn fna, affine_fn fnb)
1282 return affine_fn_op (PLUS_EXPR, fna, fnb);
1285 /* Returns the difference of affine functions FNA and FNB. */
1287 static affine_fn
1288 affine_fn_minus (affine_fn fna, affine_fn fnb)
1290 return affine_fn_op (MINUS_EXPR, fna, fnb);
1293 /* Frees affine function FN. */
1295 static void
1296 affine_fn_free (affine_fn fn)
1298 fn.release ();
1301 /* Determine for each subscript in the data dependence relation DDR
1302 the distance. */
1304 static void
1305 compute_subscript_distance (struct data_dependence_relation *ddr)
1307 conflict_function *cf_a, *cf_b;
1308 affine_fn fn_a, fn_b, diff;
1310 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1312 unsigned int i;
1314 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1316 struct subscript *subscript;
1318 subscript = DDR_SUBSCRIPT (ddr, i);
1319 cf_a = SUB_CONFLICTS_IN_A (subscript);
1320 cf_b = SUB_CONFLICTS_IN_B (subscript);
1322 fn_a = common_affine_function (cf_a);
1323 fn_b = common_affine_function (cf_b);
1324 if (!fn_a.exists () || !fn_b.exists ())
1326 SUB_DISTANCE (subscript) = chrec_dont_know;
1327 return;
1329 diff = affine_fn_minus (fn_a, fn_b);
1331 if (affine_function_constant_p (diff))
1332 SUB_DISTANCE (subscript) = affine_function_base (diff);
1333 else
1334 SUB_DISTANCE (subscript) = chrec_dont_know;
1336 affine_fn_free (diff);
1341 /* Returns the conflict function for "unknown". */
1343 static conflict_function *
1344 conflict_fn_not_known (void)
1346 conflict_function *fn = XCNEW (conflict_function);
1347 fn->n = NOT_KNOWN;
1349 return fn;
1352 /* Returns the conflict function for "independent". */
1354 static conflict_function *
1355 conflict_fn_no_dependence (void)
1357 conflict_function *fn = XCNEW (conflict_function);
1358 fn->n = NO_DEPENDENCE;
1360 return fn;
1363 /* Returns true if the address of OBJ is invariant in LOOP. */
1365 static bool
1366 object_address_invariant_in_loop_p (const struct loop *loop, const_tree obj)
1368 while (handled_component_p (obj))
1370 if (TREE_CODE (obj) == ARRAY_REF)
1372 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1373 need to check the stride and the lower bound of the reference. */
1374 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1375 loop->num)
1376 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 3),
1377 loop->num))
1378 return false;
1380 else if (TREE_CODE (obj) == COMPONENT_REF)
1382 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1383 loop->num))
1384 return false;
1386 obj = TREE_OPERAND (obj, 0);
1389 if (!INDIRECT_REF_P (obj)
1390 && TREE_CODE (obj) != MEM_REF)
1391 return true;
1393 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
1394 loop->num);
1397 /* Returns false if we can prove that data references A and B do not alias,
1398 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
1399 considered. */
1401 bool
1402 dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
1403 bool loop_nest)
1405 tree addr_a = DR_BASE_OBJECT (a);
1406 tree addr_b = DR_BASE_OBJECT (b);
1408 /* If we are not processing a loop nest but scalar code we
1409 do not need to care about possible cross-iteration dependences
1410 and thus can process the full original reference. Do so,
1411 similar to how loop invariant motion applies extra offset-based
1412 disambiguation. */
1413 if (!loop_nest)
1415 aff_tree off1, off2;
1416 widest_int size1, size2;
1417 get_inner_reference_aff (DR_REF (a), &off1, &size1);
1418 get_inner_reference_aff (DR_REF (b), &off2, &size2);
1419 aff_combination_scale (&off1, -1);
1420 aff_combination_add (&off2, &off1);
1421 if (aff_comb_cannot_overlap_p (&off2, size1, size2))
1422 return false;
1425 if ((TREE_CODE (addr_a) == MEM_REF || TREE_CODE (addr_a) == TARGET_MEM_REF)
1426 && (TREE_CODE (addr_b) == MEM_REF || TREE_CODE (addr_b) == TARGET_MEM_REF)
1427 && MR_DEPENDENCE_CLIQUE (addr_a) == MR_DEPENDENCE_CLIQUE (addr_b)
1428 && MR_DEPENDENCE_BASE (addr_a) != MR_DEPENDENCE_BASE (addr_b))
1429 return false;
1431 /* If we had an evolution in a pointer-based MEM_REF BASE_OBJECT we
1432 do not know the size of the base-object. So we cannot do any
1433 offset/overlap based analysis but have to rely on points-to
1434 information only. */
1435 if (TREE_CODE (addr_a) == MEM_REF
1436 && (DR_UNCONSTRAINED_BASE (a)
1437 || TREE_CODE (TREE_OPERAND (addr_a, 0)) == SSA_NAME))
1439 /* For true dependences we can apply TBAA. */
1440 if (flag_strict_aliasing
1441 && DR_IS_WRITE (a) && DR_IS_READ (b)
1442 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1443 get_alias_set (DR_REF (b))))
1444 return false;
1445 if (TREE_CODE (addr_b) == MEM_REF)
1446 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1447 TREE_OPERAND (addr_b, 0));
1448 else
1449 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1450 build_fold_addr_expr (addr_b));
1452 else if (TREE_CODE (addr_b) == MEM_REF
1453 && (DR_UNCONSTRAINED_BASE (b)
1454 || TREE_CODE (TREE_OPERAND (addr_b, 0)) == SSA_NAME))
1456 /* For true dependences we can apply TBAA. */
1457 if (flag_strict_aliasing
1458 && DR_IS_WRITE (a) && DR_IS_READ (b)
1459 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1460 get_alias_set (DR_REF (b))))
1461 return false;
1462 if (TREE_CODE (addr_a) == MEM_REF)
1463 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1464 TREE_OPERAND (addr_b, 0));
1465 else
1466 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
1467 TREE_OPERAND (addr_b, 0));
1470 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1471 that is being subsetted in the loop nest. */
1472 if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
1473 return refs_output_dependent_p (addr_a, addr_b);
1474 else if (DR_IS_READ (a) && DR_IS_WRITE (b))
1475 return refs_anti_dependent_p (addr_a, addr_b);
1476 return refs_may_alias_p (addr_a, addr_b);
1479 /* Initialize a data dependence relation between data accesses A and
1480 B. NB_LOOPS is the number of loops surrounding the references: the
1481 size of the classic distance/direction vectors. */
1483 struct data_dependence_relation *
1484 initialize_data_dependence_relation (struct data_reference *a,
1485 struct data_reference *b,
1486 vec<loop_p> loop_nest)
1488 struct data_dependence_relation *res;
1489 unsigned int i;
1491 res = XNEW (struct data_dependence_relation);
1492 DDR_A (res) = a;
1493 DDR_B (res) = b;
1494 DDR_LOOP_NEST (res).create (0);
1495 DDR_REVERSED_P (res) = false;
1496 DDR_SUBSCRIPTS (res).create (0);
1497 DDR_DIR_VECTS (res).create (0);
1498 DDR_DIST_VECTS (res).create (0);
1500 if (a == NULL || b == NULL)
1502 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1503 return res;
1506 /* If the data references do not alias, then they are independent. */
1507 if (!dr_may_alias_p (a, b, loop_nest.exists ()))
1509 DDR_ARE_DEPENDENT (res) = chrec_known;
1510 return res;
1513 /* The case where the references are exactly the same. */
1514 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
1516 if (loop_nest.exists ()
1517 && !object_address_invariant_in_loop_p (loop_nest[0],
1518 DR_BASE_OBJECT (a)))
1520 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1521 return res;
1523 DDR_AFFINE_P (res) = true;
1524 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1525 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1526 DDR_LOOP_NEST (res) = loop_nest;
1527 DDR_INNER_LOOP (res) = 0;
1528 DDR_SELF_REFERENCE (res) = true;
1529 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1531 struct subscript *subscript;
1533 subscript = XNEW (struct subscript);
1534 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1535 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1536 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1537 SUB_DISTANCE (subscript) = chrec_dont_know;
1538 DDR_SUBSCRIPTS (res).safe_push (subscript);
1540 return res;
1543 /* If the references do not access the same object, we do not know
1544 whether they alias or not. */
1545 if (!operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0))
1547 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1548 return res;
1551 /* If the base of the object is not invariant in the loop nest, we cannot
1552 analyze it. TODO -- in fact, it would suffice to record that there may
1553 be arbitrary dependences in the loops where the base object varies. */
1554 if (loop_nest.exists ()
1555 && !object_address_invariant_in_loop_p (loop_nest[0],
1556 DR_BASE_OBJECT (a)))
1558 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1559 return res;
1562 /* If the number of dimensions of the access to not agree we can have
1563 a pointer access to a component of the array element type and an
1564 array access while the base-objects are still the same. Punt. */
1565 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1567 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1568 return res;
1571 DDR_AFFINE_P (res) = true;
1572 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1573 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1574 DDR_LOOP_NEST (res) = loop_nest;
1575 DDR_INNER_LOOP (res) = 0;
1576 DDR_SELF_REFERENCE (res) = false;
1578 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1580 struct subscript *subscript;
1582 subscript = XNEW (struct subscript);
1583 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1584 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1585 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1586 SUB_DISTANCE (subscript) = chrec_dont_know;
1587 DDR_SUBSCRIPTS (res).safe_push (subscript);
1590 return res;
1593 /* Frees memory used by the conflict function F. */
1595 static void
1596 free_conflict_function (conflict_function *f)
1598 unsigned i;
1600 if (CF_NONTRIVIAL_P (f))
1602 for (i = 0; i < f->n; i++)
1603 affine_fn_free (f->fns[i]);
1605 free (f);
1608 /* Frees memory used by SUBSCRIPTS. */
1610 static void
1611 free_subscripts (vec<subscript_p> subscripts)
1613 unsigned i;
1614 subscript_p s;
1616 FOR_EACH_VEC_ELT (subscripts, i, s)
1618 free_conflict_function (s->conflicting_iterations_in_a);
1619 free_conflict_function (s->conflicting_iterations_in_b);
1620 free (s);
1622 subscripts.release ();
1625 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1626 description. */
1628 static inline void
1629 finalize_ddr_dependent (struct data_dependence_relation *ddr,
1630 tree chrec)
1632 DDR_ARE_DEPENDENT (ddr) = chrec;
1633 free_subscripts (DDR_SUBSCRIPTS (ddr));
1634 DDR_SUBSCRIPTS (ddr).create (0);
1637 /* The dependence relation DDR cannot be represented by a distance
1638 vector. */
1640 static inline void
1641 non_affine_dependence_relation (struct data_dependence_relation *ddr)
1643 if (dump_file && (dump_flags & TDF_DETAILS))
1644 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
1646 DDR_AFFINE_P (ddr) = false;
1651 /* This section contains the classic Banerjee tests. */
1653 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1654 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1656 static inline bool
1657 ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1659 return (evolution_function_is_constant_p (chrec_a)
1660 && evolution_function_is_constant_p (chrec_b));
1663 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
1664 variable, i.e., if the SIV (Single Index Variable) test is true. */
1666 static bool
1667 siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1669 if ((evolution_function_is_constant_p (chrec_a)
1670 && evolution_function_is_univariate_p (chrec_b))
1671 || (evolution_function_is_constant_p (chrec_b)
1672 && evolution_function_is_univariate_p (chrec_a)))
1673 return true;
1675 if (evolution_function_is_univariate_p (chrec_a)
1676 && evolution_function_is_univariate_p (chrec_b))
1678 switch (TREE_CODE (chrec_a))
1680 case POLYNOMIAL_CHREC:
1681 switch (TREE_CODE (chrec_b))
1683 case POLYNOMIAL_CHREC:
1684 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
1685 return false;
1687 default:
1688 return true;
1691 default:
1692 return true;
1696 return false;
1699 /* Creates a conflict function with N dimensions. The affine functions
1700 in each dimension follow. */
1702 static conflict_function *
1703 conflict_fn (unsigned n, ...)
1705 unsigned i;
1706 conflict_function *ret = XCNEW (conflict_function);
1707 va_list ap;
1709 gcc_assert (0 < n && n <= MAX_DIM);
1710 va_start (ap, n);
1712 ret->n = n;
1713 for (i = 0; i < n; i++)
1714 ret->fns[i] = va_arg (ap, affine_fn);
1715 va_end (ap);
1717 return ret;
1720 /* Returns constant affine function with value CST. */
1722 static affine_fn
1723 affine_fn_cst (tree cst)
1725 affine_fn fn;
1726 fn.create (1);
1727 fn.quick_push (cst);
1728 return fn;
1731 /* Returns affine function with single variable, CST + COEF * x_DIM. */
1733 static affine_fn
1734 affine_fn_univar (tree cst, unsigned dim, tree coef)
1736 affine_fn fn;
1737 fn.create (dim + 1);
1738 unsigned i;
1740 gcc_assert (dim > 0);
1741 fn.quick_push (cst);
1742 for (i = 1; i < dim; i++)
1743 fn.quick_push (integer_zero_node);
1744 fn.quick_push (coef);
1745 return fn;
1748 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1749 *OVERLAPS_B are initialized to the functions that describe the
1750 relation between the elements accessed twice by CHREC_A and
1751 CHREC_B. For k >= 0, the following property is verified:
1753 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1755 static void
1756 analyze_ziv_subscript (tree chrec_a,
1757 tree chrec_b,
1758 conflict_function **overlaps_a,
1759 conflict_function **overlaps_b,
1760 tree *last_conflicts)
1762 tree type, difference;
1763 dependence_stats.num_ziv++;
1765 if (dump_file && (dump_flags & TDF_DETAILS))
1766 fprintf (dump_file, "(analyze_ziv_subscript \n");
1768 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1769 chrec_a = chrec_convert (type, chrec_a, NULL);
1770 chrec_b = chrec_convert (type, chrec_b, NULL);
1771 difference = chrec_fold_minus (type, chrec_a, chrec_b);
1773 switch (TREE_CODE (difference))
1775 case INTEGER_CST:
1776 if (integer_zerop (difference))
1778 /* The difference is equal to zero: the accessed index
1779 overlaps for each iteration in the loop. */
1780 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1781 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1782 *last_conflicts = chrec_dont_know;
1783 dependence_stats.num_ziv_dependent++;
1785 else
1787 /* The accesses do not overlap. */
1788 *overlaps_a = conflict_fn_no_dependence ();
1789 *overlaps_b = conflict_fn_no_dependence ();
1790 *last_conflicts = integer_zero_node;
1791 dependence_stats.num_ziv_independent++;
1793 break;
1795 default:
1796 /* We're not sure whether the indexes overlap. For the moment,
1797 conservatively answer "don't know". */
1798 if (dump_file && (dump_flags & TDF_DETAILS))
1799 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
1801 *overlaps_a = conflict_fn_not_known ();
1802 *overlaps_b = conflict_fn_not_known ();
1803 *last_conflicts = chrec_dont_know;
1804 dependence_stats.num_ziv_unimplemented++;
1805 break;
1808 if (dump_file && (dump_flags & TDF_DETAILS))
1809 fprintf (dump_file, ")\n");
1812 /* Similar to max_stmt_executions_int, but returns the bound as a tree,
1813 and only if it fits to the int type. If this is not the case, or the
1814 bound on the number of iterations of LOOP could not be derived, returns
1815 chrec_dont_know. */
1817 static tree
1818 max_stmt_executions_tree (struct loop *loop)
1820 widest_int nit;
1822 if (!max_stmt_executions (loop, &nit))
1823 return chrec_dont_know;
1825 if (!wi::fits_to_tree_p (nit, unsigned_type_node))
1826 return chrec_dont_know;
1828 return wide_int_to_tree (unsigned_type_node, nit);
1831 /* Determine whether the CHREC is always positive/negative. If the expression
1832 cannot be statically analyzed, return false, otherwise set the answer into
1833 VALUE. */
1835 static bool
1836 chrec_is_positive (tree chrec, bool *value)
1838 bool value0, value1, value2;
1839 tree end_value, nb_iter;
1841 switch (TREE_CODE (chrec))
1843 case POLYNOMIAL_CHREC:
1844 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
1845 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
1846 return false;
1848 /* FIXME -- overflows. */
1849 if (value0 == value1)
1851 *value = value0;
1852 return true;
1855 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1856 and the proof consists in showing that the sign never
1857 changes during the execution of the loop, from 0 to
1858 loop->nb_iterations. */
1859 if (!evolution_function_is_affine_p (chrec))
1860 return false;
1862 nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
1863 if (chrec_contains_undetermined (nb_iter))
1864 return false;
1866 #if 0
1867 /* TODO -- If the test is after the exit, we may decrease the number of
1868 iterations by one. */
1869 if (after_exit)
1870 nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
1871 #endif
1873 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
1875 if (!chrec_is_positive (end_value, &value2))
1876 return false;
1878 *value = value0;
1879 return value0 == value1;
1881 case INTEGER_CST:
1882 switch (tree_int_cst_sgn (chrec))
1884 case -1:
1885 *value = false;
1886 break;
1887 case 1:
1888 *value = true;
1889 break;
1890 default:
1891 return false;
1893 return true;
1895 default:
1896 return false;
1901 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1902 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1903 *OVERLAPS_B are initialized to the functions that describe the
1904 relation between the elements accessed twice by CHREC_A and
1905 CHREC_B. For k >= 0, the following property is verified:
1907 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1909 static void
1910 analyze_siv_subscript_cst_affine (tree chrec_a,
1911 tree chrec_b,
1912 conflict_function **overlaps_a,
1913 conflict_function **overlaps_b,
1914 tree *last_conflicts)
1916 bool value0, value1, value2;
1917 tree type, difference, tmp;
1919 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1920 chrec_a = chrec_convert (type, chrec_a, NULL);
1921 chrec_b = chrec_convert (type, chrec_b, NULL);
1922 difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
1924 /* Special case overlap in the first iteration. */
1925 if (integer_zerop (difference))
1927 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1928 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1929 *last_conflicts = integer_one_node;
1930 return;
1933 if (!chrec_is_positive (initial_condition (difference), &value0))
1935 if (dump_file && (dump_flags & TDF_DETAILS))
1936 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
1938 dependence_stats.num_siv_unimplemented++;
1939 *overlaps_a = conflict_fn_not_known ();
1940 *overlaps_b = conflict_fn_not_known ();
1941 *last_conflicts = chrec_dont_know;
1942 return;
1944 else
1946 if (value0 == false)
1948 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
1950 if (dump_file && (dump_flags & TDF_DETAILS))
1951 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1953 *overlaps_a = conflict_fn_not_known ();
1954 *overlaps_b = conflict_fn_not_known ();
1955 *last_conflicts = chrec_dont_know;
1956 dependence_stats.num_siv_unimplemented++;
1957 return;
1959 else
1961 if (value1 == true)
1963 /* Example:
1964 chrec_a = 12
1965 chrec_b = {10, +, 1}
1968 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1970 HOST_WIDE_INT numiter;
1971 struct loop *loop = get_chrec_loop (chrec_b);
1973 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1974 tmp = fold_build2 (EXACT_DIV_EXPR, type,
1975 fold_build1 (ABS_EXPR, type, difference),
1976 CHREC_RIGHT (chrec_b));
1977 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1978 *last_conflicts = integer_one_node;
1981 /* Perform weak-zero siv test to see if overlap is
1982 outside the loop bounds. */
1983 numiter = max_stmt_executions_int (loop);
1985 if (numiter >= 0
1986 && compare_tree_int (tmp, numiter) > 0)
1988 free_conflict_function (*overlaps_a);
1989 free_conflict_function (*overlaps_b);
1990 *overlaps_a = conflict_fn_no_dependence ();
1991 *overlaps_b = conflict_fn_no_dependence ();
1992 *last_conflicts = integer_zero_node;
1993 dependence_stats.num_siv_independent++;
1994 return;
1996 dependence_stats.num_siv_dependent++;
1997 return;
2000 /* When the step does not divide the difference, there are
2001 no overlaps. */
2002 else
2004 *overlaps_a = conflict_fn_no_dependence ();
2005 *overlaps_b = conflict_fn_no_dependence ();
2006 *last_conflicts = integer_zero_node;
2007 dependence_stats.num_siv_independent++;
2008 return;
2012 else
2014 /* Example:
2015 chrec_a = 12
2016 chrec_b = {10, +, -1}
2018 In this case, chrec_a will not overlap with chrec_b. */
2019 *overlaps_a = conflict_fn_no_dependence ();
2020 *overlaps_b = conflict_fn_no_dependence ();
2021 *last_conflicts = integer_zero_node;
2022 dependence_stats.num_siv_independent++;
2023 return;
2027 else
2029 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
2031 if (dump_file && (dump_flags & TDF_DETAILS))
2032 fprintf (dump_file, "siv test failed: chrec not positive.\n");
2034 *overlaps_a = conflict_fn_not_known ();
2035 *overlaps_b = conflict_fn_not_known ();
2036 *last_conflicts = chrec_dont_know;
2037 dependence_stats.num_siv_unimplemented++;
2038 return;
2040 else
2042 if (value2 == false)
2044 /* Example:
2045 chrec_a = 3
2046 chrec_b = {10, +, -1}
2048 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2050 HOST_WIDE_INT numiter;
2051 struct loop *loop = get_chrec_loop (chrec_b);
2053 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2054 tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
2055 CHREC_RIGHT (chrec_b));
2056 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
2057 *last_conflicts = integer_one_node;
2059 /* Perform weak-zero siv test to see if overlap is
2060 outside the loop bounds. */
2061 numiter = max_stmt_executions_int (loop);
2063 if (numiter >= 0
2064 && compare_tree_int (tmp, numiter) > 0)
2066 free_conflict_function (*overlaps_a);
2067 free_conflict_function (*overlaps_b);
2068 *overlaps_a = conflict_fn_no_dependence ();
2069 *overlaps_b = conflict_fn_no_dependence ();
2070 *last_conflicts = integer_zero_node;
2071 dependence_stats.num_siv_independent++;
2072 return;
2074 dependence_stats.num_siv_dependent++;
2075 return;
2078 /* When the step does not divide the difference, there
2079 are no overlaps. */
2080 else
2082 *overlaps_a = conflict_fn_no_dependence ();
2083 *overlaps_b = conflict_fn_no_dependence ();
2084 *last_conflicts = integer_zero_node;
2085 dependence_stats.num_siv_independent++;
2086 return;
2089 else
2091 /* Example:
2092 chrec_a = 3
2093 chrec_b = {4, +, 1}
2095 In this case, chrec_a will not overlap with chrec_b. */
2096 *overlaps_a = conflict_fn_no_dependence ();
2097 *overlaps_b = conflict_fn_no_dependence ();
2098 *last_conflicts = integer_zero_node;
2099 dependence_stats.num_siv_independent++;
2100 return;
2107 /* Helper recursive function for initializing the matrix A. Returns
2108 the initial value of CHREC. */
2110 static tree
2111 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2113 gcc_assert (chrec);
2115 switch (TREE_CODE (chrec))
2117 case POLYNOMIAL_CHREC:
2118 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST);
2120 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2121 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2123 case PLUS_EXPR:
2124 case MULT_EXPR:
2125 case MINUS_EXPR:
2127 tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2128 tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
2130 return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
2133 CASE_CONVERT:
2135 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2136 return chrec_convert (chrec_type (chrec), op, NULL);
2139 case BIT_NOT_EXPR:
2141 /* Handle ~X as -1 - X. */
2142 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2143 return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
2144 build_int_cst (TREE_TYPE (chrec), -1), op);
2147 case INTEGER_CST:
2148 return chrec;
2150 default:
2151 gcc_unreachable ();
2152 return NULL_TREE;
2156 #define FLOOR_DIV(x,y) ((x) / (y))
2158 /* Solves the special case of the Diophantine equation:
2159 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2161 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2162 number of iterations that loops X and Y run. The overlaps will be
2163 constructed as evolutions in dimension DIM. */
2165 static void
2166 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2167 affine_fn *overlaps_a,
2168 affine_fn *overlaps_b,
2169 tree *last_conflicts, int dim)
2171 if (((step_a > 0 && step_b > 0)
2172 || (step_a < 0 && step_b < 0)))
2174 int step_overlaps_a, step_overlaps_b;
2175 int gcd_steps_a_b, last_conflict, tau2;
2177 gcd_steps_a_b = gcd (step_a, step_b);
2178 step_overlaps_a = step_b / gcd_steps_a_b;
2179 step_overlaps_b = step_a / gcd_steps_a_b;
2181 if (niter > 0)
2183 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2184 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2185 last_conflict = tau2;
2186 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2188 else
2189 *last_conflicts = chrec_dont_know;
2191 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
2192 build_int_cst (NULL_TREE,
2193 step_overlaps_a));
2194 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
2195 build_int_cst (NULL_TREE,
2196 step_overlaps_b));
2199 else
2201 *overlaps_a = affine_fn_cst (integer_zero_node);
2202 *overlaps_b = affine_fn_cst (integer_zero_node);
2203 *last_conflicts = integer_zero_node;
2207 /* Solves the special case of a Diophantine equation where CHREC_A is
2208 an affine bivariate function, and CHREC_B is an affine univariate
2209 function. For example,
2211 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2213 has the following overlapping functions:
2215 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2216 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2217 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2219 FORNOW: This is a specialized implementation for a case occurring in
2220 a common benchmark. Implement the general algorithm. */
2222 static void
2223 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2224 conflict_function **overlaps_a,
2225 conflict_function **overlaps_b,
2226 tree *last_conflicts)
2228 bool xz_p, yz_p, xyz_p;
2229 int step_x, step_y, step_z;
2230 HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
2231 affine_fn overlaps_a_xz, overlaps_b_xz;
2232 affine_fn overlaps_a_yz, overlaps_b_yz;
2233 affine_fn overlaps_a_xyz, overlaps_b_xyz;
2234 affine_fn ova1, ova2, ovb;
2235 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
2237 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2238 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2239 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2241 niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
2242 niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
2243 niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
2245 if (niter_x < 0 || niter_y < 0 || niter_z < 0)
2247 if (dump_file && (dump_flags & TDF_DETAILS))
2248 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
2250 *overlaps_a = conflict_fn_not_known ();
2251 *overlaps_b = conflict_fn_not_known ();
2252 *last_conflicts = chrec_dont_know;
2253 return;
2256 niter = MIN (niter_x, niter_z);
2257 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2258 &overlaps_a_xz,
2259 &overlaps_b_xz,
2260 &last_conflicts_xz, 1);
2261 niter = MIN (niter_y, niter_z);
2262 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2263 &overlaps_a_yz,
2264 &overlaps_b_yz,
2265 &last_conflicts_yz, 2);
2266 niter = MIN (niter_x, niter_z);
2267 niter = MIN (niter_y, niter);
2268 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2269 &overlaps_a_xyz,
2270 &overlaps_b_xyz,
2271 &last_conflicts_xyz, 3);
2273 xz_p = !integer_zerop (last_conflicts_xz);
2274 yz_p = !integer_zerop (last_conflicts_yz);
2275 xyz_p = !integer_zerop (last_conflicts_xyz);
2277 if (xz_p || yz_p || xyz_p)
2279 ova1 = affine_fn_cst (integer_zero_node);
2280 ova2 = affine_fn_cst (integer_zero_node);
2281 ovb = affine_fn_cst (integer_zero_node);
2282 if (xz_p)
2284 affine_fn t0 = ova1;
2285 affine_fn t2 = ovb;
2287 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
2288 ovb = affine_fn_plus (ovb, overlaps_b_xz);
2289 affine_fn_free (t0);
2290 affine_fn_free (t2);
2291 *last_conflicts = last_conflicts_xz;
2293 if (yz_p)
2295 affine_fn t0 = ova2;
2296 affine_fn t2 = ovb;
2298 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
2299 ovb = affine_fn_plus (ovb, overlaps_b_yz);
2300 affine_fn_free (t0);
2301 affine_fn_free (t2);
2302 *last_conflicts = last_conflicts_yz;
2304 if (xyz_p)
2306 affine_fn t0 = ova1;
2307 affine_fn t2 = ova2;
2308 affine_fn t4 = ovb;
2310 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
2311 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
2312 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
2313 affine_fn_free (t0);
2314 affine_fn_free (t2);
2315 affine_fn_free (t4);
2316 *last_conflicts = last_conflicts_xyz;
2318 *overlaps_a = conflict_fn (2, ova1, ova2);
2319 *overlaps_b = conflict_fn (1, ovb);
2321 else
2323 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2324 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2325 *last_conflicts = integer_zero_node;
2328 affine_fn_free (overlaps_a_xz);
2329 affine_fn_free (overlaps_b_xz);
2330 affine_fn_free (overlaps_a_yz);
2331 affine_fn_free (overlaps_b_yz);
2332 affine_fn_free (overlaps_a_xyz);
2333 affine_fn_free (overlaps_b_xyz);
2336 /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2338 static void
2339 lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
2340 int size)
2342 memcpy (vec2, vec1, size * sizeof (*vec1));
2345 /* Copy the elements of M x N matrix MAT1 to MAT2. */
2347 static void
2348 lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
2349 int m, int n)
2351 int i;
2353 for (i = 0; i < m; i++)
2354 lambda_vector_copy (mat1[i], mat2[i], n);
2357 /* Store the N x N identity matrix in MAT. */
2359 static void
2360 lambda_matrix_id (lambda_matrix mat, int size)
2362 int i, j;
2364 for (i = 0; i < size; i++)
2365 for (j = 0; j < size; j++)
2366 mat[i][j] = (i == j) ? 1 : 0;
2369 /* Return the first nonzero element of vector VEC1 between START and N.
2370 We must have START <= N. Returns N if VEC1 is the zero vector. */
2372 static int
2373 lambda_vector_first_nz (lambda_vector vec1, int n, int start)
2375 int j = start;
2376 while (j < n && vec1[j] == 0)
2377 j++;
2378 return j;
2381 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2382 R2 = R2 + CONST1 * R1. */
2384 static void
2385 lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2, int const1)
2387 int i;
2389 if (const1 == 0)
2390 return;
2392 for (i = 0; i < n; i++)
2393 mat[r2][i] += const1 * mat[r1][i];
2396 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
2397 and store the result in VEC2. */
2399 static void
2400 lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
2401 int size, int const1)
2403 int i;
2405 if (const1 == 0)
2406 lambda_vector_clear (vec2, size);
2407 else
2408 for (i = 0; i < size; i++)
2409 vec2[i] = const1 * vec1[i];
2412 /* Negate vector VEC1 with length SIZE and store it in VEC2. */
2414 static void
2415 lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
2416 int size)
2418 lambda_vector_mult_const (vec1, vec2, size, -1);
2421 /* Negate row R1 of matrix MAT which has N columns. */
2423 static void
2424 lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
2426 lambda_vector_negate (mat[r1], mat[r1], n);
2429 /* Return true if two vectors are equal. */
2431 static bool
2432 lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
2434 int i;
2435 for (i = 0; i < size; i++)
2436 if (vec1[i] != vec2[i])
2437 return false;
2438 return true;
2441 /* Given an M x N integer matrix A, this function determines an M x
2442 M unimodular matrix U, and an M x N echelon matrix S such that
2443 "U.A = S". This decomposition is also known as "right Hermite".
2445 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2446 Restructuring Compilers" Utpal Banerjee. */
2448 static void
2449 lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
2450 lambda_matrix S, lambda_matrix U)
2452 int i, j, i0 = 0;
2454 lambda_matrix_copy (A, S, m, n);
2455 lambda_matrix_id (U, m);
2457 for (j = 0; j < n; j++)
2459 if (lambda_vector_first_nz (S[j], m, i0) < m)
2461 ++i0;
2462 for (i = m - 1; i >= i0; i--)
2464 while (S[i][j] != 0)
2466 int sigma, factor, a, b;
2468 a = S[i-1][j];
2469 b = S[i][j];
2470 sigma = (a * b < 0) ? -1: 1;
2471 a = abs (a);
2472 b = abs (b);
2473 factor = sigma * (a / b);
2475 lambda_matrix_row_add (S, n, i, i-1, -factor);
2476 std::swap (S[i], S[i-1]);
2478 lambda_matrix_row_add (U, m, i, i-1, -factor);
2479 std::swap (U[i], U[i-1]);
2486 /* Determines the overlapping elements due to accesses CHREC_A and
2487 CHREC_B, that are affine functions. This function cannot handle
2488 symbolic evolution functions, ie. when initial conditions are
2489 parameters, because it uses lambda matrices of integers. */
2491 static void
2492 analyze_subscript_affine_affine (tree chrec_a,
2493 tree chrec_b,
2494 conflict_function **overlaps_a,
2495 conflict_function **overlaps_b,
2496 tree *last_conflicts)
2498 unsigned nb_vars_a, nb_vars_b, dim;
2499 HOST_WIDE_INT init_a, init_b, gamma, gcd_alpha_beta;
2500 lambda_matrix A, U, S;
2501 struct obstack scratch_obstack;
2503 if (eq_evolutions_p (chrec_a, chrec_b))
2505 /* The accessed index overlaps for each iteration in the
2506 loop. */
2507 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2508 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2509 *last_conflicts = chrec_dont_know;
2510 return;
2512 if (dump_file && (dump_flags & TDF_DETAILS))
2513 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2515 /* For determining the initial intersection, we have to solve a
2516 Diophantine equation. This is the most time consuming part.
2518 For answering to the question: "Is there a dependence?" we have
2519 to prove that there exists a solution to the Diophantine
2520 equation, and that the solution is in the iteration domain,
2521 i.e. the solution is positive or zero, and that the solution
2522 happens before the upper bound loop.nb_iterations. Otherwise
2523 there is no dependence. This function outputs a description of
2524 the iterations that hold the intersections. */
2526 nb_vars_a = nb_vars_in_chrec (chrec_a);
2527 nb_vars_b = nb_vars_in_chrec (chrec_b);
2529 gcc_obstack_init (&scratch_obstack);
2531 dim = nb_vars_a + nb_vars_b;
2532 U = lambda_matrix_new (dim, dim, &scratch_obstack);
2533 A = lambda_matrix_new (dim, 1, &scratch_obstack);
2534 S = lambda_matrix_new (dim, 1, &scratch_obstack);
2536 init_a = int_cst_value (initialize_matrix_A (A, chrec_a, 0, 1));
2537 init_b = int_cst_value (initialize_matrix_A (A, chrec_b, nb_vars_a, -1));
2538 gamma = init_b - init_a;
2540 /* Don't do all the hard work of solving the Diophantine equation
2541 when we already know the solution: for example,
2542 | {3, +, 1}_1
2543 | {3, +, 4}_2
2544 | gamma = 3 - 3 = 0.
2545 Then the first overlap occurs during the first iterations:
2546 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2548 if (gamma == 0)
2550 if (nb_vars_a == 1 && nb_vars_b == 1)
2552 HOST_WIDE_INT step_a, step_b;
2553 HOST_WIDE_INT niter, niter_a, niter_b;
2554 affine_fn ova, ovb;
2556 niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
2557 niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
2558 niter = MIN (niter_a, niter_b);
2559 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2560 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2562 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2563 &ova, &ovb,
2564 last_conflicts, 1);
2565 *overlaps_a = conflict_fn (1, ova);
2566 *overlaps_b = conflict_fn (1, ovb);
2569 else if (nb_vars_a == 2 && nb_vars_b == 1)
2570 compute_overlap_steps_for_affine_1_2
2571 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2573 else if (nb_vars_a == 1 && nb_vars_b == 2)
2574 compute_overlap_steps_for_affine_1_2
2575 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2577 else
2579 if (dump_file && (dump_flags & TDF_DETAILS))
2580 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
2581 *overlaps_a = conflict_fn_not_known ();
2582 *overlaps_b = conflict_fn_not_known ();
2583 *last_conflicts = chrec_dont_know;
2585 goto end_analyze_subs_aa;
2588 /* U.A = S */
2589 lambda_matrix_right_hermite (A, dim, 1, S, U);
2591 if (S[0][0] < 0)
2593 S[0][0] *= -1;
2594 lambda_matrix_row_negate (U, dim, 0);
2596 gcd_alpha_beta = S[0][0];
2598 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2599 but that is a quite strange case. Instead of ICEing, answer
2600 don't know. */
2601 if (gcd_alpha_beta == 0)
2603 *overlaps_a = conflict_fn_not_known ();
2604 *overlaps_b = conflict_fn_not_known ();
2605 *last_conflicts = chrec_dont_know;
2606 goto end_analyze_subs_aa;
2609 /* The classic "gcd-test". */
2610 if (!int_divides_p (gcd_alpha_beta, gamma))
2612 /* The "gcd-test" has determined that there is no integer
2613 solution, i.e. there is no dependence. */
2614 *overlaps_a = conflict_fn_no_dependence ();
2615 *overlaps_b = conflict_fn_no_dependence ();
2616 *last_conflicts = integer_zero_node;
2619 /* Both access functions are univariate. This includes SIV and MIV cases. */
2620 else if (nb_vars_a == 1 && nb_vars_b == 1)
2622 /* Both functions should have the same evolution sign. */
2623 if (((A[0][0] > 0 && -A[1][0] > 0)
2624 || (A[0][0] < 0 && -A[1][0] < 0)))
2626 /* The solutions are given by:
2628 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2629 | [u21 u22] [y0]
2631 For a given integer t. Using the following variables,
2633 | i0 = u11 * gamma / gcd_alpha_beta
2634 | j0 = u12 * gamma / gcd_alpha_beta
2635 | i1 = u21
2636 | j1 = u22
2638 the solutions are:
2640 | x0 = i0 + i1 * t,
2641 | y0 = j0 + j1 * t. */
2642 HOST_WIDE_INT i0, j0, i1, j1;
2644 i0 = U[0][0] * gamma / gcd_alpha_beta;
2645 j0 = U[0][1] * gamma / gcd_alpha_beta;
2646 i1 = U[1][0];
2647 j1 = U[1][1];
2649 if ((i1 == 0 && i0 < 0)
2650 || (j1 == 0 && j0 < 0))
2652 /* There is no solution.
2653 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2654 falls in here, but for the moment we don't look at the
2655 upper bound of the iteration domain. */
2656 *overlaps_a = conflict_fn_no_dependence ();
2657 *overlaps_b = conflict_fn_no_dependence ();
2658 *last_conflicts = integer_zero_node;
2659 goto end_analyze_subs_aa;
2662 if (i1 > 0 && j1 > 0)
2664 HOST_WIDE_INT niter_a
2665 = max_stmt_executions_int (get_chrec_loop (chrec_a));
2666 HOST_WIDE_INT niter_b
2667 = max_stmt_executions_int (get_chrec_loop (chrec_b));
2668 HOST_WIDE_INT niter = MIN (niter_a, niter_b);
2670 /* (X0, Y0) is a solution of the Diophantine equation:
2671 "chrec_a (X0) = chrec_b (Y0)". */
2672 HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
2673 CEIL (-j0, j1));
2674 HOST_WIDE_INT x0 = i1 * tau1 + i0;
2675 HOST_WIDE_INT y0 = j1 * tau1 + j0;
2677 /* (X1, Y1) is the smallest positive solution of the eq
2678 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2679 first conflict occurs. */
2680 HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
2681 HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
2682 HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
2684 if (niter > 0)
2686 HOST_WIDE_INT tau2 = MIN (FLOOR_DIV (niter - i0, i1),
2687 FLOOR_DIV (niter - j0, j1));
2688 HOST_WIDE_INT last_conflict = tau2 - (x1 - i0)/i1;
2690 /* If the overlap occurs outside of the bounds of the
2691 loop, there is no dependence. */
2692 if (x1 >= niter || y1 >= niter)
2694 *overlaps_a = conflict_fn_no_dependence ();
2695 *overlaps_b = conflict_fn_no_dependence ();
2696 *last_conflicts = integer_zero_node;
2697 goto end_analyze_subs_aa;
2699 else
2700 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2702 else
2703 *last_conflicts = chrec_dont_know;
2705 *overlaps_a
2706 = conflict_fn (1,
2707 affine_fn_univar (build_int_cst (NULL_TREE, x1),
2709 build_int_cst (NULL_TREE, i1)));
2710 *overlaps_b
2711 = conflict_fn (1,
2712 affine_fn_univar (build_int_cst (NULL_TREE, y1),
2714 build_int_cst (NULL_TREE, j1)));
2716 else
2718 /* FIXME: For the moment, the upper bound of the
2719 iteration domain for i and j is not checked. */
2720 if (dump_file && (dump_flags & TDF_DETAILS))
2721 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2722 *overlaps_a = conflict_fn_not_known ();
2723 *overlaps_b = conflict_fn_not_known ();
2724 *last_conflicts = chrec_dont_know;
2727 else
2729 if (dump_file && (dump_flags & TDF_DETAILS))
2730 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2731 *overlaps_a = conflict_fn_not_known ();
2732 *overlaps_b = conflict_fn_not_known ();
2733 *last_conflicts = chrec_dont_know;
2736 else
2738 if (dump_file && (dump_flags & TDF_DETAILS))
2739 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2740 *overlaps_a = conflict_fn_not_known ();
2741 *overlaps_b = conflict_fn_not_known ();
2742 *last_conflicts = chrec_dont_know;
2745 end_analyze_subs_aa:
2746 obstack_free (&scratch_obstack, NULL);
2747 if (dump_file && (dump_flags & TDF_DETAILS))
2749 fprintf (dump_file, " (overlaps_a = ");
2750 dump_conflict_function (dump_file, *overlaps_a);
2751 fprintf (dump_file, ")\n (overlaps_b = ");
2752 dump_conflict_function (dump_file, *overlaps_b);
2753 fprintf (dump_file, "))\n");
2757 /* Returns true when analyze_subscript_affine_affine can be used for
2758 determining the dependence relation between chrec_a and chrec_b,
2759 that contain symbols. This function modifies chrec_a and chrec_b
2760 such that the analysis result is the same, and such that they don't
2761 contain symbols, and then can safely be passed to the analyzer.
2763 Example: The analysis of the following tuples of evolutions produce
2764 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2765 vs. {0, +, 1}_1
2767 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2768 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2771 static bool
2772 can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
2774 tree diff, type, left_a, left_b, right_b;
2776 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
2777 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
2778 /* FIXME: For the moment not handled. Might be refined later. */
2779 return false;
2781 type = chrec_type (*chrec_a);
2782 left_a = CHREC_LEFT (*chrec_a);
2783 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
2784 diff = chrec_fold_minus (type, left_a, left_b);
2786 if (!evolution_function_is_constant_p (diff))
2787 return false;
2789 if (dump_file && (dump_flags & TDF_DETAILS))
2790 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
2792 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
2793 diff, CHREC_RIGHT (*chrec_a));
2794 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
2795 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
2796 build_int_cst (type, 0),
2797 right_b);
2798 return true;
2801 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2802 *OVERLAPS_B are initialized to the functions that describe the
2803 relation between the elements accessed twice by CHREC_A and
2804 CHREC_B. For k >= 0, the following property is verified:
2806 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2808 static void
2809 analyze_siv_subscript (tree chrec_a,
2810 tree chrec_b,
2811 conflict_function **overlaps_a,
2812 conflict_function **overlaps_b,
2813 tree *last_conflicts,
2814 int loop_nest_num)
2816 dependence_stats.num_siv++;
2818 if (dump_file && (dump_flags & TDF_DETAILS))
2819 fprintf (dump_file, "(analyze_siv_subscript \n");
2821 if (evolution_function_is_constant_p (chrec_a)
2822 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2823 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
2824 overlaps_a, overlaps_b, last_conflicts);
2826 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2827 && evolution_function_is_constant_p (chrec_b))
2828 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
2829 overlaps_b, overlaps_a, last_conflicts);
2831 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2832 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2834 if (!chrec_contains_symbols (chrec_a)
2835 && !chrec_contains_symbols (chrec_b))
2837 analyze_subscript_affine_affine (chrec_a, chrec_b,
2838 overlaps_a, overlaps_b,
2839 last_conflicts);
2841 if (CF_NOT_KNOWN_P (*overlaps_a)
2842 || CF_NOT_KNOWN_P (*overlaps_b))
2843 dependence_stats.num_siv_unimplemented++;
2844 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2845 || CF_NO_DEPENDENCE_P (*overlaps_b))
2846 dependence_stats.num_siv_independent++;
2847 else
2848 dependence_stats.num_siv_dependent++;
2850 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
2851 &chrec_b))
2853 analyze_subscript_affine_affine (chrec_a, chrec_b,
2854 overlaps_a, overlaps_b,
2855 last_conflicts);
2857 if (CF_NOT_KNOWN_P (*overlaps_a)
2858 || CF_NOT_KNOWN_P (*overlaps_b))
2859 dependence_stats.num_siv_unimplemented++;
2860 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2861 || CF_NO_DEPENDENCE_P (*overlaps_b))
2862 dependence_stats.num_siv_independent++;
2863 else
2864 dependence_stats.num_siv_dependent++;
2866 else
2867 goto siv_subscript_dontknow;
2870 else
2872 siv_subscript_dontknow:;
2873 if (dump_file && (dump_flags & TDF_DETAILS))
2874 fprintf (dump_file, " siv test failed: unimplemented");
2875 *overlaps_a = conflict_fn_not_known ();
2876 *overlaps_b = conflict_fn_not_known ();
2877 *last_conflicts = chrec_dont_know;
2878 dependence_stats.num_siv_unimplemented++;
2881 if (dump_file && (dump_flags & TDF_DETAILS))
2882 fprintf (dump_file, ")\n");
2885 /* Returns false if we can prove that the greatest common divisor of the steps
2886 of CHREC does not divide CST, false otherwise. */
2888 static bool
2889 gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
2891 HOST_WIDE_INT cd = 0, val;
2892 tree step;
2894 if (!tree_fits_shwi_p (cst))
2895 return true;
2896 val = tree_to_shwi (cst);
2898 while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
2900 step = CHREC_RIGHT (chrec);
2901 if (!tree_fits_shwi_p (step))
2902 return true;
2903 cd = gcd (cd, tree_to_shwi (step));
2904 chrec = CHREC_LEFT (chrec);
2907 return val % cd == 0;
2910 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
2911 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2912 functions that describe the relation between the elements accessed
2913 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2914 is verified:
2916 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2918 static void
2919 analyze_miv_subscript (tree chrec_a,
2920 tree chrec_b,
2921 conflict_function **overlaps_a,
2922 conflict_function **overlaps_b,
2923 tree *last_conflicts,
2924 struct loop *loop_nest)
2926 tree type, difference;
2928 dependence_stats.num_miv++;
2929 if (dump_file && (dump_flags & TDF_DETAILS))
2930 fprintf (dump_file, "(analyze_miv_subscript \n");
2932 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
2933 chrec_a = chrec_convert (type, chrec_a, NULL);
2934 chrec_b = chrec_convert (type, chrec_b, NULL);
2935 difference = chrec_fold_minus (type, chrec_a, chrec_b);
2937 if (eq_evolutions_p (chrec_a, chrec_b))
2939 /* Access functions are the same: all the elements are accessed
2940 in the same order. */
2941 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2942 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2943 *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
2944 dependence_stats.num_miv_dependent++;
2947 else if (evolution_function_is_constant_p (difference)
2948 /* For the moment, the following is verified:
2949 evolution_function_is_affine_multivariate_p (chrec_a,
2950 loop_nest->num) */
2951 && !gcd_of_steps_may_divide_p (chrec_a, difference))
2953 /* testsuite/.../ssa-chrec-33.c
2954 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2956 The difference is 1, and all the evolution steps are multiples
2957 of 2, consequently there are no overlapping elements. */
2958 *overlaps_a = conflict_fn_no_dependence ();
2959 *overlaps_b = conflict_fn_no_dependence ();
2960 *last_conflicts = integer_zero_node;
2961 dependence_stats.num_miv_independent++;
2964 else if (evolution_function_is_affine_multivariate_p (chrec_a, loop_nest->num)
2965 && !chrec_contains_symbols (chrec_a)
2966 && evolution_function_is_affine_multivariate_p (chrec_b, loop_nest->num)
2967 && !chrec_contains_symbols (chrec_b))
2969 /* testsuite/.../ssa-chrec-35.c
2970 {0, +, 1}_2 vs. {0, +, 1}_3
2971 the overlapping elements are respectively located at iterations:
2972 {0, +, 1}_x and {0, +, 1}_x,
2973 in other words, we have the equality:
2974 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2976 Other examples:
2977 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2978 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2980 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2981 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2983 analyze_subscript_affine_affine (chrec_a, chrec_b,
2984 overlaps_a, overlaps_b, last_conflicts);
2986 if (CF_NOT_KNOWN_P (*overlaps_a)
2987 || CF_NOT_KNOWN_P (*overlaps_b))
2988 dependence_stats.num_miv_unimplemented++;
2989 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2990 || CF_NO_DEPENDENCE_P (*overlaps_b))
2991 dependence_stats.num_miv_independent++;
2992 else
2993 dependence_stats.num_miv_dependent++;
2996 else
2998 /* When the analysis is too difficult, answer "don't know". */
2999 if (dump_file && (dump_flags & TDF_DETAILS))
3000 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
3002 *overlaps_a = conflict_fn_not_known ();
3003 *overlaps_b = conflict_fn_not_known ();
3004 *last_conflicts = chrec_dont_know;
3005 dependence_stats.num_miv_unimplemented++;
3008 if (dump_file && (dump_flags & TDF_DETAILS))
3009 fprintf (dump_file, ")\n");
3012 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
3013 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
3014 OVERLAP_ITERATIONS_B are initialized with two functions that
3015 describe the iterations that contain conflicting elements.
3017 Remark: For an integer k >= 0, the following equality is true:
3019 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
3022 static void
3023 analyze_overlapping_iterations (tree chrec_a,
3024 tree chrec_b,
3025 conflict_function **overlap_iterations_a,
3026 conflict_function **overlap_iterations_b,
3027 tree *last_conflicts, struct loop *loop_nest)
3029 unsigned int lnn = loop_nest->num;
3031 dependence_stats.num_subscript_tests++;
3033 if (dump_file && (dump_flags & TDF_DETAILS))
3035 fprintf (dump_file, "(analyze_overlapping_iterations \n");
3036 fprintf (dump_file, " (chrec_a = ");
3037 print_generic_expr (dump_file, chrec_a, 0);
3038 fprintf (dump_file, ")\n (chrec_b = ");
3039 print_generic_expr (dump_file, chrec_b, 0);
3040 fprintf (dump_file, ")\n");
3043 if (chrec_a == NULL_TREE
3044 || chrec_b == NULL_TREE
3045 || chrec_contains_undetermined (chrec_a)
3046 || chrec_contains_undetermined (chrec_b))
3048 dependence_stats.num_subscript_undetermined++;
3050 *overlap_iterations_a = conflict_fn_not_known ();
3051 *overlap_iterations_b = conflict_fn_not_known ();
3054 /* If they are the same chrec, and are affine, they overlap
3055 on every iteration. */
3056 else if (eq_evolutions_p (chrec_a, chrec_b)
3057 && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3058 || operand_equal_p (chrec_a, chrec_b, 0)))
3060 dependence_stats.num_same_subscript_function++;
3061 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3062 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3063 *last_conflicts = chrec_dont_know;
3066 /* If they aren't the same, and aren't affine, we can't do anything
3067 yet. */
3068 else if ((chrec_contains_symbols (chrec_a)
3069 || chrec_contains_symbols (chrec_b))
3070 && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3071 || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
3073 dependence_stats.num_subscript_undetermined++;
3074 *overlap_iterations_a = conflict_fn_not_known ();
3075 *overlap_iterations_b = conflict_fn_not_known ();
3078 else if (ziv_subscript_p (chrec_a, chrec_b))
3079 analyze_ziv_subscript (chrec_a, chrec_b,
3080 overlap_iterations_a, overlap_iterations_b,
3081 last_conflicts);
3083 else if (siv_subscript_p (chrec_a, chrec_b))
3084 analyze_siv_subscript (chrec_a, chrec_b,
3085 overlap_iterations_a, overlap_iterations_b,
3086 last_conflicts, lnn);
3088 else
3089 analyze_miv_subscript (chrec_a, chrec_b,
3090 overlap_iterations_a, overlap_iterations_b,
3091 last_conflicts, loop_nest);
3093 if (dump_file && (dump_flags & TDF_DETAILS))
3095 fprintf (dump_file, " (overlap_iterations_a = ");
3096 dump_conflict_function (dump_file, *overlap_iterations_a);
3097 fprintf (dump_file, ")\n (overlap_iterations_b = ");
3098 dump_conflict_function (dump_file, *overlap_iterations_b);
3099 fprintf (dump_file, "))\n");
3103 /* Helper function for uniquely inserting distance vectors. */
3105 static void
3106 save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
3108 unsigned i;
3109 lambda_vector v;
3111 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, v)
3112 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
3113 return;
3115 DDR_DIST_VECTS (ddr).safe_push (dist_v);
3118 /* Helper function for uniquely inserting direction vectors. */
3120 static void
3121 save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
3123 unsigned i;
3124 lambda_vector v;
3126 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), i, v)
3127 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
3128 return;
3130 DDR_DIR_VECTS (ddr).safe_push (dir_v);
3133 /* Add a distance of 1 on all the loops outer than INDEX. If we
3134 haven't yet determined a distance for this outer loop, push a new
3135 distance vector composed of the previous distance, and a distance
3136 of 1 for this outer loop. Example:
3138 | loop_1
3139 | loop_2
3140 | A[10]
3141 | endloop_2
3142 | endloop_1
3144 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3145 save (0, 1), then we have to save (1, 0). */
3147 static void
3148 add_outer_distances (struct data_dependence_relation *ddr,
3149 lambda_vector dist_v, int index)
3151 /* For each outer loop where init_v is not set, the accesses are
3152 in dependence of distance 1 in the loop. */
3153 while (--index >= 0)
3155 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3156 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3157 save_v[index] = 1;
3158 save_dist_v (ddr, save_v);
3162 /* Return false when fail to represent the data dependence as a
3163 distance vector. INIT_B is set to true when a component has been
3164 added to the distance vector DIST_V. INDEX_CARRY is then set to
3165 the index in DIST_V that carries the dependence. */
3167 static bool
3168 build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
3169 struct data_reference *ddr_a,
3170 struct data_reference *ddr_b,
3171 lambda_vector dist_v, bool *init_b,
3172 int *index_carry)
3174 unsigned i;
3175 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3177 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3179 tree access_fn_a, access_fn_b;
3180 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3182 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3184 non_affine_dependence_relation (ddr);
3185 return false;
3188 access_fn_a = DR_ACCESS_FN (ddr_a, i);
3189 access_fn_b = DR_ACCESS_FN (ddr_b, i);
3191 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3192 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3194 int dist, index;
3195 int var_a = CHREC_VARIABLE (access_fn_a);
3196 int var_b = CHREC_VARIABLE (access_fn_b);
3198 if (var_a != var_b
3199 || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3201 non_affine_dependence_relation (ddr);
3202 return false;
3205 dist = int_cst_value (SUB_DISTANCE (subscript));
3206 index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
3207 *index_carry = MIN (index, *index_carry);
3209 /* This is the subscript coupling test. If we have already
3210 recorded a distance for this loop (a distance coming from
3211 another subscript), it should be the same. For example,
3212 in the following code, there is no dependence:
3214 | loop i = 0, N, 1
3215 | T[i+1][i] = ...
3216 | ... = T[i][i]
3217 | endloop
3219 if (init_v[index] != 0 && dist_v[index] != dist)
3221 finalize_ddr_dependent (ddr, chrec_known);
3222 return false;
3225 dist_v[index] = dist;
3226 init_v[index] = 1;
3227 *init_b = true;
3229 else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
3231 /* This can be for example an affine vs. constant dependence
3232 (T[i] vs. T[3]) that is not an affine dependence and is
3233 not representable as a distance vector. */
3234 non_affine_dependence_relation (ddr);
3235 return false;
3239 return true;
3242 /* Return true when the DDR contains only constant access functions. */
3244 static bool
3245 constant_access_functions (const struct data_dependence_relation *ddr)
3247 unsigned i;
3249 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3250 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr), i))
3251 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr), i)))
3252 return false;
3254 return true;
3257 /* Helper function for the case where DDR_A and DDR_B are the same
3258 multivariate access function with a constant step. For an example
3259 see pr34635-1.c. */
3261 static void
3262 add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
3264 int x_1, x_2;
3265 tree c_1 = CHREC_LEFT (c_2);
3266 tree c_0 = CHREC_LEFT (c_1);
3267 lambda_vector dist_v;
3268 int v1, v2, cd;
3270 /* Polynomials with more than 2 variables are not handled yet. When
3271 the evolution steps are parameters, it is not possible to
3272 represent the dependence using classical distance vectors. */
3273 if (TREE_CODE (c_0) != INTEGER_CST
3274 || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
3275 || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
3277 DDR_AFFINE_P (ddr) = false;
3278 return;
3281 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
3282 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
3284 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3285 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3286 v1 = int_cst_value (CHREC_RIGHT (c_1));
3287 v2 = int_cst_value (CHREC_RIGHT (c_2));
3288 cd = gcd (v1, v2);
3289 v1 /= cd;
3290 v2 /= cd;
3292 if (v2 < 0)
3294 v2 = -v2;
3295 v1 = -v1;
3298 dist_v[x_1] = v2;
3299 dist_v[x_2] = -v1;
3300 save_dist_v (ddr, dist_v);
3302 add_outer_distances (ddr, dist_v, x_1);
3305 /* Helper function for the case where DDR_A and DDR_B are the same
3306 access functions. */
3308 static void
3309 add_other_self_distances (struct data_dependence_relation *ddr)
3311 lambda_vector dist_v;
3312 unsigned i;
3313 int index_carry = DDR_NB_LOOPS (ddr);
3315 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3317 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
3319 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
3321 if (!evolution_function_is_univariate_p (access_fun))
3323 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
3325 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3326 return;
3329 access_fun = DR_ACCESS_FN (DDR_A (ddr), 0);
3331 if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
3332 add_multivariate_self_dist (ddr, access_fun);
3333 else
3334 /* The evolution step is not constant: it varies in
3335 the outer loop, so this cannot be represented by a
3336 distance vector. For example in pr34635.c the
3337 evolution is {0, +, {0, +, 4}_1}_2. */
3338 DDR_AFFINE_P (ddr) = false;
3340 return;
3343 index_carry = MIN (index_carry,
3344 index_in_loop_nest (CHREC_VARIABLE (access_fun),
3345 DDR_LOOP_NEST (ddr)));
3349 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3350 add_outer_distances (ddr, dist_v, index_carry);
3353 static void
3354 insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
3356 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3358 dist_v[DDR_INNER_LOOP (ddr)] = 1;
3359 save_dist_v (ddr, dist_v);
3362 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
3363 is the case for example when access functions are the same and
3364 equal to a constant, as in:
3366 | loop_1
3367 | A[3] = ...
3368 | ... = A[3]
3369 | endloop_1
3371 in which case the distance vectors are (0) and (1). */
3373 static void
3374 add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
3376 unsigned i, j;
3378 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3380 subscript_p sub = DDR_SUBSCRIPT (ddr, i);
3381 conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
3382 conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
3384 for (j = 0; j < ca->n; j++)
3385 if (affine_function_zero_p (ca->fns[j]))
3387 insert_innermost_unit_dist_vector (ddr);
3388 return;
3391 for (j = 0; j < cb->n; j++)
3392 if (affine_function_zero_p (cb->fns[j]))
3394 insert_innermost_unit_dist_vector (ddr);
3395 return;
3400 /* Compute the classic per loop distance vector. DDR is the data
3401 dependence relation to build a vector from. Return false when fail
3402 to represent the data dependence as a distance vector. */
3404 static bool
3405 build_classic_dist_vector (struct data_dependence_relation *ddr,
3406 struct loop *loop_nest)
3408 bool init_b = false;
3409 int index_carry = DDR_NB_LOOPS (ddr);
3410 lambda_vector dist_v;
3412 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3413 return false;
3415 if (same_access_functions (ddr))
3417 /* Save the 0 vector. */
3418 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3419 save_dist_v (ddr, dist_v);
3421 if (constant_access_functions (ddr))
3422 add_distance_for_zero_overlaps (ddr);
3424 if (DDR_NB_LOOPS (ddr) > 1)
3425 add_other_self_distances (ddr);
3427 return true;
3430 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3431 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3432 dist_v, &init_b, &index_carry))
3433 return false;
3435 /* Save the distance vector if we initialized one. */
3436 if (init_b)
3438 /* Verify a basic constraint: classic distance vectors should
3439 always be lexicographically positive.
3441 Data references are collected in the order of execution of
3442 the program, thus for the following loop
3444 | for (i = 1; i < 100; i++)
3445 | for (j = 1; j < 100; j++)
3447 | t = T[j+1][i-1]; // A
3448 | T[j][i] = t + 2; // B
3451 references are collected following the direction of the wind:
3452 A then B. The data dependence tests are performed also
3453 following this order, such that we're looking at the distance
3454 separating the elements accessed by A from the elements later
3455 accessed by B. But in this example, the distance returned by
3456 test_dep (A, B) is lexicographically negative (-1, 1), that
3457 means that the access A occurs later than B with respect to
3458 the outer loop, ie. we're actually looking upwind. In this
3459 case we solve test_dep (B, A) looking downwind to the
3460 lexicographically positive solution, that returns the
3461 distance vector (1, -1). */
3462 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3464 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3465 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3466 loop_nest))
3467 return false;
3468 compute_subscript_distance (ddr);
3469 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3470 save_v, &init_b, &index_carry))
3471 return false;
3472 save_dist_v (ddr, save_v);
3473 DDR_REVERSED_P (ddr) = true;
3475 /* In this case there is a dependence forward for all the
3476 outer loops:
3478 | for (k = 1; k < 100; k++)
3479 | for (i = 1; i < 100; i++)
3480 | for (j = 1; j < 100; j++)
3482 | t = T[j+1][i-1]; // A
3483 | T[j][i] = t + 2; // B
3486 the vectors are:
3487 (0, 1, -1)
3488 (1, 1, -1)
3489 (1, -1, 1)
3491 if (DDR_NB_LOOPS (ddr) > 1)
3493 add_outer_distances (ddr, save_v, index_carry);
3494 add_outer_distances (ddr, dist_v, index_carry);
3497 else
3499 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3500 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3502 if (DDR_NB_LOOPS (ddr) > 1)
3504 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3506 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr),
3507 DDR_A (ddr), loop_nest))
3508 return false;
3509 compute_subscript_distance (ddr);
3510 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3511 opposite_v, &init_b,
3512 &index_carry))
3513 return false;
3515 save_dist_v (ddr, save_v);
3516 add_outer_distances (ddr, dist_v, index_carry);
3517 add_outer_distances (ddr, opposite_v, index_carry);
3519 else
3520 save_dist_v (ddr, save_v);
3523 else
3525 /* There is a distance of 1 on all the outer loops: Example:
3526 there is a dependence of distance 1 on loop_1 for the array A.
3528 | loop_1
3529 | A[5] = ...
3530 | endloop
3532 add_outer_distances (ddr, dist_v,
3533 lambda_vector_first_nz (dist_v,
3534 DDR_NB_LOOPS (ddr), 0));
3537 if (dump_file && (dump_flags & TDF_DETAILS))
3539 unsigned i;
3541 fprintf (dump_file, "(build_classic_dist_vector\n");
3542 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3544 fprintf (dump_file, " dist_vector = (");
3545 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3546 DDR_NB_LOOPS (ddr));
3547 fprintf (dump_file, " )\n");
3549 fprintf (dump_file, ")\n");
3552 return true;
3555 /* Return the direction for a given distance.
3556 FIXME: Computing dir this way is suboptimal, since dir can catch
3557 cases that dist is unable to represent. */
3559 static inline enum data_dependence_direction
3560 dir_from_dist (int dist)
3562 if (dist > 0)
3563 return dir_positive;
3564 else if (dist < 0)
3565 return dir_negative;
3566 else
3567 return dir_equal;
3570 /* Compute the classic per loop direction vector. DDR is the data
3571 dependence relation to build a vector from. */
3573 static void
3574 build_classic_dir_vector (struct data_dependence_relation *ddr)
3576 unsigned i, j;
3577 lambda_vector dist_v;
3579 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
3581 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3583 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3584 dir_v[j] = dir_from_dist (dist_v[j]);
3586 save_dir_v (ddr, dir_v);
3590 /* Helper function. Returns true when there is a dependence between
3591 data references DRA and DRB. */
3593 static bool
3594 subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
3595 struct data_reference *dra,
3596 struct data_reference *drb,
3597 struct loop *loop_nest)
3599 unsigned int i;
3600 tree last_conflicts;
3601 struct subscript *subscript;
3602 tree res = NULL_TREE;
3604 for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
3606 conflict_function *overlaps_a, *overlaps_b;
3608 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3609 DR_ACCESS_FN (drb, i),
3610 &overlaps_a, &overlaps_b,
3611 &last_conflicts, loop_nest);
3613 if (SUB_CONFLICTS_IN_A (subscript))
3614 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
3615 if (SUB_CONFLICTS_IN_B (subscript))
3616 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
3618 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3619 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3620 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3622 /* If there is any undetermined conflict function we have to
3623 give a conservative answer in case we cannot prove that
3624 no dependence exists when analyzing another subscript. */
3625 if (CF_NOT_KNOWN_P (overlaps_a)
3626 || CF_NOT_KNOWN_P (overlaps_b))
3628 res = chrec_dont_know;
3629 continue;
3632 /* When there is a subscript with no dependence we can stop. */
3633 else if (CF_NO_DEPENDENCE_P (overlaps_a)
3634 || CF_NO_DEPENDENCE_P (overlaps_b))
3636 res = chrec_known;
3637 break;
3641 if (res == NULL_TREE)
3642 return true;
3644 if (res == chrec_known)
3645 dependence_stats.num_dependence_independent++;
3646 else
3647 dependence_stats.num_dependence_undetermined++;
3648 finalize_ddr_dependent (ddr, res);
3649 return false;
3652 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
3654 static void
3655 subscript_dependence_tester (struct data_dependence_relation *ddr,
3656 struct loop *loop_nest)
3658 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr), loop_nest))
3659 dependence_stats.num_dependence_dependent++;
3661 compute_subscript_distance (ddr);
3662 if (build_classic_dist_vector (ddr, loop_nest))
3663 build_classic_dir_vector (ddr);
3666 /* Returns true when all the access functions of A are affine or
3667 constant with respect to LOOP_NEST. */
3669 static bool
3670 access_functions_are_affine_or_constant_p (const struct data_reference *a,
3671 const struct loop *loop_nest)
3673 unsigned int i;
3674 vec<tree> fns = DR_ACCESS_FNS (a);
3675 tree t;
3677 FOR_EACH_VEC_ELT (fns, i, t)
3678 if (!evolution_function_is_invariant_p (t, loop_nest->num)
3679 && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
3680 return false;
3682 return true;
3685 /* This computes the affine dependence relation between A and B with
3686 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
3687 independence between two accesses, while CHREC_DONT_KNOW is used
3688 for representing the unknown relation.
3690 Note that it is possible to stop the computation of the dependence
3691 relation the first time we detect a CHREC_KNOWN element for a given
3692 subscript. */
3694 void
3695 compute_affine_dependence (struct data_dependence_relation *ddr,
3696 struct loop *loop_nest)
3698 struct data_reference *dra = DDR_A (ddr);
3699 struct data_reference *drb = DDR_B (ddr);
3701 if (dump_file && (dump_flags & TDF_DETAILS))
3703 fprintf (dump_file, "(compute_affine_dependence\n");
3704 fprintf (dump_file, " stmt_a: ");
3705 print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
3706 fprintf (dump_file, " stmt_b: ");
3707 print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
3710 /* Analyze only when the dependence relation is not yet known. */
3711 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
3713 dependence_stats.num_dependence_tests++;
3715 if (access_functions_are_affine_or_constant_p (dra, loop_nest)
3716 && access_functions_are_affine_or_constant_p (drb, loop_nest))
3717 subscript_dependence_tester (ddr, loop_nest);
3719 /* As a last case, if the dependence cannot be determined, or if
3720 the dependence is considered too difficult to determine, answer
3721 "don't know". */
3722 else
3724 dependence_stats.num_dependence_undetermined++;
3726 if (dump_file && (dump_flags & TDF_DETAILS))
3728 fprintf (dump_file, "Data ref a:\n");
3729 dump_data_reference (dump_file, dra);
3730 fprintf (dump_file, "Data ref b:\n");
3731 dump_data_reference (dump_file, drb);
3732 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
3734 finalize_ddr_dependent (ddr, chrec_dont_know);
3738 if (dump_file && (dump_flags & TDF_DETAILS))
3740 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
3741 fprintf (dump_file, ") -> no dependence\n");
3742 else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
3743 fprintf (dump_file, ") -> dependence analysis failed\n");
3744 else
3745 fprintf (dump_file, ")\n");
3749 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
3750 the data references in DATAREFS, in the LOOP_NEST. When
3751 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
3752 relations. Return true when successful, i.e. data references number
3753 is small enough to be handled. */
3755 bool
3756 compute_all_dependences (vec<data_reference_p> datarefs,
3757 vec<ddr_p> *dependence_relations,
3758 vec<loop_p> loop_nest,
3759 bool compute_self_and_rr)
3761 struct data_dependence_relation *ddr;
3762 struct data_reference *a, *b;
3763 unsigned int i, j;
3765 if ((int) datarefs.length ()
3766 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
3768 struct data_dependence_relation *ddr;
3770 /* Insert a single relation into dependence_relations:
3771 chrec_dont_know. */
3772 ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
3773 dependence_relations->safe_push (ddr);
3774 return false;
3777 FOR_EACH_VEC_ELT (datarefs, i, a)
3778 for (j = i + 1; datarefs.iterate (j, &b); j++)
3779 if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
3781 ddr = initialize_data_dependence_relation (a, b, loop_nest);
3782 dependence_relations->safe_push (ddr);
3783 if (loop_nest.exists ())
3784 compute_affine_dependence (ddr, loop_nest[0]);
3787 if (compute_self_and_rr)
3788 FOR_EACH_VEC_ELT (datarefs, i, a)
3790 ddr = initialize_data_dependence_relation (a, a, loop_nest);
3791 dependence_relations->safe_push (ddr);
3792 if (loop_nest.exists ())
3793 compute_affine_dependence (ddr, loop_nest[0]);
3796 return true;
3799 /* Describes a location of a memory reference. */
3801 struct data_ref_loc
3803 /* The memory reference. */
3804 tree ref;
3806 /* True if the memory reference is read. */
3807 bool is_read;
3811 /* Stores the locations of memory references in STMT to REFERENCES. Returns
3812 true if STMT clobbers memory, false otherwise. */
3814 static bool
3815 get_references_in_stmt (gimple *stmt, vec<data_ref_loc, va_heap> *references)
3817 bool clobbers_memory = false;
3818 data_ref_loc ref;
3819 tree op0, op1;
3820 enum gimple_code stmt_code = gimple_code (stmt);
3822 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
3823 As we cannot model data-references to not spelled out
3824 accesses give up if they may occur. */
3825 if (stmt_code == GIMPLE_CALL
3826 && !(gimple_call_flags (stmt) & ECF_CONST))
3828 /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
3829 if (gimple_call_internal_p (stmt))
3830 switch (gimple_call_internal_fn (stmt))
3832 case IFN_GOMP_SIMD_LANE:
3834 struct loop *loop = gimple_bb (stmt)->loop_father;
3835 tree uid = gimple_call_arg (stmt, 0);
3836 gcc_assert (TREE_CODE (uid) == SSA_NAME);
3837 if (loop == NULL
3838 || loop->simduid != SSA_NAME_VAR (uid))
3839 clobbers_memory = true;
3840 break;
3842 case IFN_MASK_LOAD:
3843 case IFN_MASK_STORE:
3844 break;
3845 default:
3846 clobbers_memory = true;
3847 break;
3849 else
3850 clobbers_memory = true;
3852 else if (stmt_code == GIMPLE_ASM
3853 && (gimple_asm_volatile_p (as_a <gasm *> (stmt))
3854 || gimple_vuse (stmt)))
3855 clobbers_memory = true;
3857 if (!gimple_vuse (stmt))
3858 return clobbers_memory;
3860 if (stmt_code == GIMPLE_ASSIGN)
3862 tree base;
3863 op0 = gimple_assign_lhs (stmt);
3864 op1 = gimple_assign_rhs1 (stmt);
3866 if (DECL_P (op1)
3867 || (REFERENCE_CLASS_P (op1)
3868 && (base = get_base_address (op1))
3869 && TREE_CODE (base) != SSA_NAME))
3871 ref.ref = op1;
3872 ref.is_read = true;
3873 references->safe_push (ref);
3876 else if (stmt_code == GIMPLE_CALL)
3878 unsigned i, n;
3880 ref.is_read = false;
3881 if (gimple_call_internal_p (stmt))
3882 switch (gimple_call_internal_fn (stmt))
3884 case IFN_MASK_LOAD:
3885 if (gimple_call_lhs (stmt) == NULL_TREE)
3886 break;
3887 ref.is_read = true;
3888 case IFN_MASK_STORE:
3889 ref.ref = fold_build2 (MEM_REF,
3890 ref.is_read
3891 ? TREE_TYPE (gimple_call_lhs (stmt))
3892 : TREE_TYPE (gimple_call_arg (stmt, 3)),
3893 gimple_call_arg (stmt, 0),
3894 gimple_call_arg (stmt, 1));
3895 references->safe_push (ref);
3896 return false;
3897 default:
3898 break;
3901 op0 = gimple_call_lhs (stmt);
3902 n = gimple_call_num_args (stmt);
3903 for (i = 0; i < n; i++)
3905 op1 = gimple_call_arg (stmt, i);
3907 if (DECL_P (op1)
3908 || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
3910 ref.ref = op1;
3911 ref.is_read = true;
3912 references->safe_push (ref);
3916 else
3917 return clobbers_memory;
3919 if (op0
3920 && (DECL_P (op0)
3921 || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
3923 ref.ref = op0;
3924 ref.is_read = false;
3925 references->safe_push (ref);
3927 return clobbers_memory;
3931 /* Returns true if the loop-nest has any data reference. */
3933 bool
3934 loop_nest_has_data_refs (loop_p loop)
3936 basic_block *bbs = get_loop_body (loop);
3937 vec<data_ref_loc> references;
3938 references.create (3);
3940 for (unsigned i = 0; i < loop->num_nodes; i++)
3942 basic_block bb = bbs[i];
3943 gimple_stmt_iterator bsi;
3945 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
3947 gimple *stmt = gsi_stmt (bsi);
3948 get_references_in_stmt (stmt, &references);
3949 if (references.length ())
3951 free (bbs);
3952 references.release ();
3953 return true;
3957 free (bbs);
3958 references.release ();
3960 if (loop->inner)
3962 loop = loop->inner;
3963 while (loop)
3965 if (loop_nest_has_data_refs (loop))
3966 return true;
3967 loop = loop->next;
3970 return false;
3973 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
3974 reference, returns false, otherwise returns true. NEST is the outermost
3975 loop of the loop nest in which the references should be analyzed. */
3977 bool
3978 find_data_references_in_stmt (struct loop *nest, gimple *stmt,
3979 vec<data_reference_p> *datarefs)
3981 unsigned i;
3982 auto_vec<data_ref_loc, 2> references;
3983 data_ref_loc *ref;
3984 bool ret = true;
3985 data_reference_p dr;
3987 if (get_references_in_stmt (stmt, &references))
3988 return false;
3990 FOR_EACH_VEC_ELT (references, i, ref)
3992 dr = create_data_ref (nest, loop_containing_stmt (stmt),
3993 ref->ref, stmt, ref->is_read);
3994 gcc_assert (dr != NULL);
3995 datarefs->safe_push (dr);
3997 references.release ();
3998 return ret;
4001 /* Stores the data references in STMT to DATAREFS. If there is an
4002 unanalyzable reference, returns false, otherwise returns true.
4003 NEST is the outermost loop of the loop nest in which the references
4004 should be instantiated, LOOP is the loop in which the references
4005 should be analyzed. */
4007 bool
4008 graphite_find_data_references_in_stmt (loop_p nest, loop_p loop, gimple *stmt,
4009 vec<data_reference_p> *datarefs)
4011 unsigned i;
4012 auto_vec<data_ref_loc, 2> references;
4013 data_ref_loc *ref;
4014 bool ret = true;
4015 data_reference_p dr;
4017 if (get_references_in_stmt (stmt, &references))
4018 return false;
4020 FOR_EACH_VEC_ELT (references, i, ref)
4022 dr = create_data_ref (nest, loop, ref->ref, stmt, ref->is_read);
4023 gcc_assert (dr != NULL);
4024 datarefs->safe_push (dr);
4027 references.release ();
4028 return ret;
4031 /* Search the data references in LOOP, and record the information into
4032 DATAREFS. Returns chrec_dont_know when failing to analyze a
4033 difficult case, returns NULL_TREE otherwise. */
4035 tree
4036 find_data_references_in_bb (struct loop *loop, basic_block bb,
4037 vec<data_reference_p> *datarefs)
4039 gimple_stmt_iterator bsi;
4041 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4043 gimple *stmt = gsi_stmt (bsi);
4045 if (!find_data_references_in_stmt (loop, stmt, datarefs))
4047 struct data_reference *res;
4048 res = XCNEW (struct data_reference);
4049 datarefs->safe_push (res);
4051 return chrec_dont_know;
4055 return NULL_TREE;
4058 /* Search the data references in LOOP, and record the information into
4059 DATAREFS. Returns chrec_dont_know when failing to analyze a
4060 difficult case, returns NULL_TREE otherwise.
4062 TODO: This function should be made smarter so that it can handle address
4063 arithmetic as if they were array accesses, etc. */
4065 tree
4066 find_data_references_in_loop (struct loop *loop,
4067 vec<data_reference_p> *datarefs)
4069 basic_block bb, *bbs;
4070 unsigned int i;
4072 bbs = get_loop_body_in_dom_order (loop);
4074 for (i = 0; i < loop->num_nodes; i++)
4076 bb = bbs[i];
4078 if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
4080 free (bbs);
4081 return chrec_dont_know;
4084 free (bbs);
4086 return NULL_TREE;
4089 /* Recursive helper function. */
4091 static bool
4092 find_loop_nest_1 (struct loop *loop, vec<loop_p> *loop_nest)
4094 /* Inner loops of the nest should not contain siblings. Example:
4095 when there are two consecutive loops,
4097 | loop_0
4098 | loop_1
4099 | A[{0, +, 1}_1]
4100 | endloop_1
4101 | loop_2
4102 | A[{0, +, 1}_2]
4103 | endloop_2
4104 | endloop_0
4106 the dependence relation cannot be captured by the distance
4107 abstraction. */
4108 if (loop->next)
4109 return false;
4111 loop_nest->safe_push (loop);
4112 if (loop->inner)
4113 return find_loop_nest_1 (loop->inner, loop_nest);
4114 return true;
4117 /* Return false when the LOOP is not well nested. Otherwise return
4118 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4119 contain the loops from the outermost to the innermost, as they will
4120 appear in the classic distance vector. */
4122 bool
4123 find_loop_nest (struct loop *loop, vec<loop_p> *loop_nest)
4125 loop_nest->safe_push (loop);
4126 if (loop->inner)
4127 return find_loop_nest_1 (loop->inner, loop_nest);
4128 return true;
4131 /* Returns true when the data dependences have been computed, false otherwise.
4132 Given a loop nest LOOP, the following vectors are returned:
4133 DATAREFS is initialized to all the array elements contained in this loop,
4134 DEPENDENCE_RELATIONS contains the relations between the data references.
4135 Compute read-read and self relations if
4136 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4138 bool
4139 compute_data_dependences_for_loop (struct loop *loop,
4140 bool compute_self_and_read_read_dependences,
4141 vec<loop_p> *loop_nest,
4142 vec<data_reference_p> *datarefs,
4143 vec<ddr_p> *dependence_relations)
4145 bool res = true;
4147 memset (&dependence_stats, 0, sizeof (dependence_stats));
4149 /* If the loop nest is not well formed, or one of the data references
4150 is not computable, give up without spending time to compute other
4151 dependences. */
4152 if (!loop
4153 || !find_loop_nest (loop, loop_nest)
4154 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
4155 || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
4156 compute_self_and_read_read_dependences))
4157 res = false;
4159 if (dump_file && (dump_flags & TDF_STATS))
4161 fprintf (dump_file, "Dependence tester statistics:\n");
4163 fprintf (dump_file, "Number of dependence tests: %d\n",
4164 dependence_stats.num_dependence_tests);
4165 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
4166 dependence_stats.num_dependence_dependent);
4167 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
4168 dependence_stats.num_dependence_independent);
4169 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
4170 dependence_stats.num_dependence_undetermined);
4172 fprintf (dump_file, "Number of subscript tests: %d\n",
4173 dependence_stats.num_subscript_tests);
4174 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
4175 dependence_stats.num_subscript_undetermined);
4176 fprintf (dump_file, "Number of same subscript function: %d\n",
4177 dependence_stats.num_same_subscript_function);
4179 fprintf (dump_file, "Number of ziv tests: %d\n",
4180 dependence_stats.num_ziv);
4181 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4182 dependence_stats.num_ziv_dependent);
4183 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4184 dependence_stats.num_ziv_independent);
4185 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
4186 dependence_stats.num_ziv_unimplemented);
4188 fprintf (dump_file, "Number of siv tests: %d\n",
4189 dependence_stats.num_siv);
4190 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4191 dependence_stats.num_siv_dependent);
4192 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4193 dependence_stats.num_siv_independent);
4194 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4195 dependence_stats.num_siv_unimplemented);
4197 fprintf (dump_file, "Number of miv tests: %d\n",
4198 dependence_stats.num_miv);
4199 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4200 dependence_stats.num_miv_dependent);
4201 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4202 dependence_stats.num_miv_independent);
4203 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4204 dependence_stats.num_miv_unimplemented);
4207 return res;
4210 /* Free the memory used by a data dependence relation DDR. */
4212 void
4213 free_dependence_relation (struct data_dependence_relation *ddr)
4215 if (ddr == NULL)
4216 return;
4218 if (DDR_SUBSCRIPTS (ddr).exists ())
4219 free_subscripts (DDR_SUBSCRIPTS (ddr));
4220 DDR_DIST_VECTS (ddr).release ();
4221 DDR_DIR_VECTS (ddr).release ();
4223 free (ddr);
4226 /* Free the memory used by the data dependence relations from
4227 DEPENDENCE_RELATIONS. */
4229 void
4230 free_dependence_relations (vec<ddr_p> dependence_relations)
4232 unsigned int i;
4233 struct data_dependence_relation *ddr;
4235 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4236 if (ddr)
4237 free_dependence_relation (ddr);
4239 dependence_relations.release ();
4242 /* Free the memory used by the data references from DATAREFS. */
4244 void
4245 free_data_refs (vec<data_reference_p> datarefs)
4247 unsigned int i;
4248 struct data_reference *dr;
4250 FOR_EACH_VEC_ELT (datarefs, i, dr)
4251 free_data_ref (dr);
4252 datarefs.release ();