Add GCC support to ENQCMD.
[official-gcc.git] / gcc / tree-data-ref.c
blob67b960d5c6d945d2b44f2a158fdc93c24eef3c09
1 /* Data references and dependences detectors.
2 Copyright (C) 2003-2019 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This pass walks a given loop structure searching for array
22 references. The information about the array accesses is recorded
23 in DATA_REFERENCE structures.
25 The basic test for determining the dependences is:
26 given two access functions chrec1 and chrec2 to a same array, and
27 x and y two vectors from the iteration domain, the same element of
28 the array is accessed twice at iterations x and y if and only if:
29 | chrec1 (x) == chrec2 (y).
31 The goals of this analysis are:
33 - to determine the independence: the relation between two
34 independent accesses is qualified with the chrec_known (this
35 information allows a loop parallelization),
37 - when two data references access the same data, to qualify the
38 dependence relation with classic dependence representations:
40 - distance vectors
41 - direction vectors
42 - loop carried level dependence
43 - polyhedron dependence
44 or with the chains of recurrences based representation,
46 - to define a knowledge base for storing the data dependence
47 information,
49 - to define an interface to access this data.
52 Definitions:
54 - subscript: given two array accesses a subscript is the tuple
55 composed of the access functions for a given dimension. Example:
56 Given A[f1][f2][f3] and B[g1][g2][g3], there are three subscripts:
57 (f1, g1), (f2, g2), (f3, g3).
59 - Diophantine equation: an equation whose coefficients and
60 solutions are integer constants, for example the equation
61 | 3*x + 2*y = 1
62 has an integer solution x = 1 and y = -1.
64 References:
66 - "Advanced Compilation for High Performance Computing" by Randy
67 Allen and Ken Kennedy.
68 http://citeseer.ist.psu.edu/goff91practical.html
70 - "Loop Transformations for Restructuring Compilers - The Foundations"
71 by Utpal Banerjee.
76 #include "config.h"
77 #include "system.h"
78 #include "coretypes.h"
79 #include "backend.h"
80 #include "rtl.h"
81 #include "tree.h"
82 #include "gimple.h"
83 #include "gimple-pretty-print.h"
84 #include "alias.h"
85 #include "fold-const.h"
86 #include "expr.h"
87 #include "gimple-iterator.h"
88 #include "tree-ssa-loop-niter.h"
89 #include "tree-ssa-loop.h"
90 #include "tree-ssa.h"
91 #include "cfgloop.h"
92 #include "tree-data-ref.h"
93 #include "tree-scalar-evolution.h"
94 #include "dumpfile.h"
95 #include "tree-affine.h"
96 #include "params.h"
97 #include "builtins.h"
98 #include "tree-eh.h"
99 #include "ssa.h"
101 static struct datadep_stats
103 int num_dependence_tests;
104 int num_dependence_dependent;
105 int num_dependence_independent;
106 int num_dependence_undetermined;
108 int num_subscript_tests;
109 int num_subscript_undetermined;
110 int num_same_subscript_function;
112 int num_ziv;
113 int num_ziv_independent;
114 int num_ziv_dependent;
115 int num_ziv_unimplemented;
117 int num_siv;
118 int num_siv_independent;
119 int num_siv_dependent;
120 int num_siv_unimplemented;
122 int num_miv;
123 int num_miv_independent;
124 int num_miv_dependent;
125 int num_miv_unimplemented;
126 } dependence_stats;
128 static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
129 unsigned int, unsigned int,
130 struct loop *);
131 /* Returns true iff A divides B. */
133 static inline bool
134 tree_fold_divides_p (const_tree a, const_tree b)
136 gcc_assert (TREE_CODE (a) == INTEGER_CST);
137 gcc_assert (TREE_CODE (b) == INTEGER_CST);
138 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a));
141 /* Returns true iff A divides B. */
143 static inline bool
144 int_divides_p (int a, int b)
146 return ((b % a) == 0);
149 /* Return true if reference REF contains a union access. */
151 static bool
152 ref_contains_union_access_p (tree ref)
154 while (handled_component_p (ref))
156 ref = TREE_OPERAND (ref, 0);
157 if (TREE_CODE (TREE_TYPE (ref)) == UNION_TYPE
158 || TREE_CODE (TREE_TYPE (ref)) == QUAL_UNION_TYPE)
159 return true;
161 return false;
166 /* Dump into FILE all the data references from DATAREFS. */
168 static void
169 dump_data_references (FILE *file, vec<data_reference_p> datarefs)
171 unsigned int i;
172 struct data_reference *dr;
174 FOR_EACH_VEC_ELT (datarefs, i, dr)
175 dump_data_reference (file, dr);
178 /* Unified dump into FILE all the data references from DATAREFS. */
180 DEBUG_FUNCTION void
181 debug (vec<data_reference_p> &ref)
183 dump_data_references (stderr, ref);
186 DEBUG_FUNCTION void
187 debug (vec<data_reference_p> *ptr)
189 if (ptr)
190 debug (*ptr);
191 else
192 fprintf (stderr, "<nil>\n");
196 /* Dump into STDERR all the data references from DATAREFS. */
198 DEBUG_FUNCTION void
199 debug_data_references (vec<data_reference_p> datarefs)
201 dump_data_references (stderr, datarefs);
204 /* Print to STDERR the data_reference DR. */
206 DEBUG_FUNCTION void
207 debug_data_reference (struct data_reference *dr)
209 dump_data_reference (stderr, dr);
212 /* Dump function for a DATA_REFERENCE structure. */
214 void
215 dump_data_reference (FILE *outf,
216 struct data_reference *dr)
218 unsigned int i;
220 fprintf (outf, "#(Data Ref: \n");
221 fprintf (outf, "# bb: %d \n", gimple_bb (DR_STMT (dr))->index);
222 fprintf (outf, "# stmt: ");
223 print_gimple_stmt (outf, DR_STMT (dr), 0);
224 fprintf (outf, "# ref: ");
225 print_generic_stmt (outf, DR_REF (dr));
226 fprintf (outf, "# base_object: ");
227 print_generic_stmt (outf, DR_BASE_OBJECT (dr));
229 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
231 fprintf (outf, "# Access function %d: ", i);
232 print_generic_stmt (outf, DR_ACCESS_FN (dr, i));
234 fprintf (outf, "#)\n");
237 /* Unified dump function for a DATA_REFERENCE structure. */
239 DEBUG_FUNCTION void
240 debug (data_reference &ref)
242 dump_data_reference (stderr, &ref);
245 DEBUG_FUNCTION void
246 debug (data_reference *ptr)
248 if (ptr)
249 debug (*ptr);
250 else
251 fprintf (stderr, "<nil>\n");
255 /* Dumps the affine function described by FN to the file OUTF. */
257 DEBUG_FUNCTION void
258 dump_affine_function (FILE *outf, affine_fn fn)
260 unsigned i;
261 tree coef;
263 print_generic_expr (outf, fn[0], TDF_SLIM);
264 for (i = 1; fn.iterate (i, &coef); i++)
266 fprintf (outf, " + ");
267 print_generic_expr (outf, coef, TDF_SLIM);
268 fprintf (outf, " * x_%u", i);
272 /* Dumps the conflict function CF to the file OUTF. */
274 DEBUG_FUNCTION void
275 dump_conflict_function (FILE *outf, conflict_function *cf)
277 unsigned i;
279 if (cf->n == NO_DEPENDENCE)
280 fprintf (outf, "no dependence");
281 else if (cf->n == NOT_KNOWN)
282 fprintf (outf, "not known");
283 else
285 for (i = 0; i < cf->n; i++)
287 if (i != 0)
288 fprintf (outf, " ");
289 fprintf (outf, "[");
290 dump_affine_function (outf, cf->fns[i]);
291 fprintf (outf, "]");
296 /* Dump function for a SUBSCRIPT structure. */
298 DEBUG_FUNCTION void
299 dump_subscript (FILE *outf, struct subscript *subscript)
301 conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
303 fprintf (outf, "\n (subscript \n");
304 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
305 dump_conflict_function (outf, cf);
306 if (CF_NONTRIVIAL_P (cf))
308 tree last_iteration = SUB_LAST_CONFLICT (subscript);
309 fprintf (outf, "\n last_conflict: ");
310 print_generic_expr (outf, last_iteration);
313 cf = SUB_CONFLICTS_IN_B (subscript);
314 fprintf (outf, "\n iterations_that_access_an_element_twice_in_B: ");
315 dump_conflict_function (outf, cf);
316 if (CF_NONTRIVIAL_P (cf))
318 tree last_iteration = SUB_LAST_CONFLICT (subscript);
319 fprintf (outf, "\n last_conflict: ");
320 print_generic_expr (outf, last_iteration);
323 fprintf (outf, "\n (Subscript distance: ");
324 print_generic_expr (outf, SUB_DISTANCE (subscript));
325 fprintf (outf, " ))\n");
328 /* Print the classic direction vector DIRV to OUTF. */
330 DEBUG_FUNCTION void
331 print_direction_vector (FILE *outf,
332 lambda_vector dirv,
333 int length)
335 int eq;
337 for (eq = 0; eq < length; eq++)
339 enum data_dependence_direction dir = ((enum data_dependence_direction)
340 dirv[eq]);
342 switch (dir)
344 case dir_positive:
345 fprintf (outf, " +");
346 break;
347 case dir_negative:
348 fprintf (outf, " -");
349 break;
350 case dir_equal:
351 fprintf (outf, " =");
352 break;
353 case dir_positive_or_equal:
354 fprintf (outf, " +=");
355 break;
356 case dir_positive_or_negative:
357 fprintf (outf, " +-");
358 break;
359 case dir_negative_or_equal:
360 fprintf (outf, " -=");
361 break;
362 case dir_star:
363 fprintf (outf, " *");
364 break;
365 default:
366 fprintf (outf, "indep");
367 break;
370 fprintf (outf, "\n");
373 /* Print a vector of direction vectors. */
375 DEBUG_FUNCTION void
376 print_dir_vectors (FILE *outf, vec<lambda_vector> dir_vects,
377 int length)
379 unsigned j;
380 lambda_vector v;
382 FOR_EACH_VEC_ELT (dir_vects, j, v)
383 print_direction_vector (outf, v, length);
386 /* Print out a vector VEC of length N to OUTFILE. */
388 DEBUG_FUNCTION void
389 print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
391 int i;
393 for (i = 0; i < n; i++)
394 fprintf (outfile, "%3d ", (int)vector[i]);
395 fprintf (outfile, "\n");
398 /* Print a vector of distance vectors. */
400 DEBUG_FUNCTION void
401 print_dist_vectors (FILE *outf, vec<lambda_vector> dist_vects,
402 int length)
404 unsigned j;
405 lambda_vector v;
407 FOR_EACH_VEC_ELT (dist_vects, j, v)
408 print_lambda_vector (outf, v, length);
411 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
413 DEBUG_FUNCTION void
414 dump_data_dependence_relation (FILE *outf,
415 struct data_dependence_relation *ddr)
417 struct data_reference *dra, *drb;
419 fprintf (outf, "(Data Dep: \n");
421 if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
423 if (ddr)
425 dra = DDR_A (ddr);
426 drb = DDR_B (ddr);
427 if (dra)
428 dump_data_reference (outf, dra);
429 else
430 fprintf (outf, " (nil)\n");
431 if (drb)
432 dump_data_reference (outf, drb);
433 else
434 fprintf (outf, " (nil)\n");
436 fprintf (outf, " (don't know)\n)\n");
437 return;
440 dra = DDR_A (ddr);
441 drb = DDR_B (ddr);
442 dump_data_reference (outf, dra);
443 dump_data_reference (outf, drb);
445 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
446 fprintf (outf, " (no dependence)\n");
448 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
450 unsigned int i;
451 struct loop *loopi;
453 subscript *sub;
454 FOR_EACH_VEC_ELT (DDR_SUBSCRIPTS (ddr), i, sub)
456 fprintf (outf, " access_fn_A: ");
457 print_generic_stmt (outf, SUB_ACCESS_FN (sub, 0));
458 fprintf (outf, " access_fn_B: ");
459 print_generic_stmt (outf, SUB_ACCESS_FN (sub, 1));
460 dump_subscript (outf, sub);
463 fprintf (outf, " loop nest: (");
464 FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
465 fprintf (outf, "%d ", loopi->num);
466 fprintf (outf, ")\n");
468 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
470 fprintf (outf, " distance_vector: ");
471 print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
472 DDR_NB_LOOPS (ddr));
475 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
477 fprintf (outf, " direction_vector: ");
478 print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
479 DDR_NB_LOOPS (ddr));
483 fprintf (outf, ")\n");
486 /* Debug version. */
488 DEBUG_FUNCTION void
489 debug_data_dependence_relation (struct data_dependence_relation *ddr)
491 dump_data_dependence_relation (stderr, ddr);
494 /* Dump into FILE all the dependence relations from DDRS. */
496 DEBUG_FUNCTION void
497 dump_data_dependence_relations (FILE *file,
498 vec<ddr_p> ddrs)
500 unsigned int i;
501 struct data_dependence_relation *ddr;
503 FOR_EACH_VEC_ELT (ddrs, i, ddr)
504 dump_data_dependence_relation (file, ddr);
507 DEBUG_FUNCTION void
508 debug (vec<ddr_p> &ref)
510 dump_data_dependence_relations (stderr, ref);
513 DEBUG_FUNCTION void
514 debug (vec<ddr_p> *ptr)
516 if (ptr)
517 debug (*ptr);
518 else
519 fprintf (stderr, "<nil>\n");
523 /* Dump to STDERR all the dependence relations from DDRS. */
525 DEBUG_FUNCTION void
526 debug_data_dependence_relations (vec<ddr_p> ddrs)
528 dump_data_dependence_relations (stderr, ddrs);
531 /* Dumps the distance and direction vectors in FILE. DDRS contains
532 the dependence relations, and VECT_SIZE is the size of the
533 dependence vectors, or in other words the number of loops in the
534 considered nest. */
536 DEBUG_FUNCTION void
537 dump_dist_dir_vectors (FILE *file, vec<ddr_p> ddrs)
539 unsigned int i, j;
540 struct data_dependence_relation *ddr;
541 lambda_vector v;
543 FOR_EACH_VEC_ELT (ddrs, i, ddr)
544 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
546 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), j, v)
548 fprintf (file, "DISTANCE_V (");
549 print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
550 fprintf (file, ")\n");
553 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), j, v)
555 fprintf (file, "DIRECTION_V (");
556 print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
557 fprintf (file, ")\n");
561 fprintf (file, "\n\n");
564 /* Dumps the data dependence relations DDRS in FILE. */
566 DEBUG_FUNCTION void
567 dump_ddrs (FILE *file, vec<ddr_p> ddrs)
569 unsigned int i;
570 struct data_dependence_relation *ddr;
572 FOR_EACH_VEC_ELT (ddrs, i, ddr)
573 dump_data_dependence_relation (file, ddr);
575 fprintf (file, "\n\n");
578 DEBUG_FUNCTION void
579 debug_ddrs (vec<ddr_p> ddrs)
581 dump_ddrs (stderr, ddrs);
584 static void
585 split_constant_offset (tree exp, tree *var, tree *off,
586 hash_map<tree, std::pair<tree, tree> > &cache);
588 /* Helper function for split_constant_offset. Expresses OP0 CODE OP1
589 (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
590 constant of type ssizetype, and returns true. If we cannot do this
591 with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
592 is returned. */
594 static bool
595 split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
596 tree *var, tree *off,
597 hash_map<tree, std::pair<tree, tree> > &cache)
599 tree var0, var1;
600 tree off0, off1;
601 enum tree_code ocode = code;
603 *var = NULL_TREE;
604 *off = NULL_TREE;
606 switch (code)
608 case INTEGER_CST:
609 *var = build_int_cst (type, 0);
610 *off = fold_convert (ssizetype, op0);
611 return true;
613 case POINTER_PLUS_EXPR:
614 ocode = PLUS_EXPR;
615 /* FALLTHROUGH */
616 case PLUS_EXPR:
617 case MINUS_EXPR:
618 split_constant_offset (op0, &var0, &off0, cache);
619 split_constant_offset (op1, &var1, &off1, cache);
620 *var = fold_build2 (code, type, var0, var1);
621 *off = size_binop (ocode, off0, off1);
622 return true;
624 case MULT_EXPR:
625 if (TREE_CODE (op1) != INTEGER_CST)
626 return false;
628 split_constant_offset (op0, &var0, &off0, cache);
629 *var = fold_build2 (MULT_EXPR, type, var0, op1);
630 *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
631 return true;
633 case ADDR_EXPR:
635 tree base, poffset;
636 poly_int64 pbitsize, pbitpos, pbytepos;
637 machine_mode pmode;
638 int punsignedp, preversep, pvolatilep;
640 op0 = TREE_OPERAND (op0, 0);
641 base
642 = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset, &pmode,
643 &punsignedp, &preversep, &pvolatilep);
645 if (!multiple_p (pbitpos, BITS_PER_UNIT, &pbytepos))
646 return false;
647 base = build_fold_addr_expr (base);
648 off0 = ssize_int (pbytepos);
650 if (poffset)
652 split_constant_offset (poffset, &poffset, &off1, cache);
653 off0 = size_binop (PLUS_EXPR, off0, off1);
654 if (POINTER_TYPE_P (TREE_TYPE (base)))
655 base = fold_build_pointer_plus (base, poffset);
656 else
657 base = fold_build2 (PLUS_EXPR, TREE_TYPE (base), base,
658 fold_convert (TREE_TYPE (base), poffset));
661 var0 = fold_convert (type, base);
663 /* If variable length types are involved, punt, otherwise casts
664 might be converted into ARRAY_REFs in gimplify_conversion.
665 To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
666 possibly no longer appears in current GIMPLE, might resurface.
667 This perhaps could run
668 if (CONVERT_EXPR_P (var0))
670 gimplify_conversion (&var0);
671 // Attempt to fill in any within var0 found ARRAY_REF's
672 // element size from corresponding op embedded ARRAY_REF,
673 // if unsuccessful, just punt.
674 } */
675 while (POINTER_TYPE_P (type))
676 type = TREE_TYPE (type);
677 if (int_size_in_bytes (type) < 0)
678 return false;
680 *var = var0;
681 *off = off0;
682 return true;
685 case SSA_NAME:
687 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
688 return false;
690 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
691 enum tree_code subcode;
693 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
694 return false;
696 subcode = gimple_assign_rhs_code (def_stmt);
698 /* We are using a cache to avoid un-CSEing large amounts of code. */
699 bool use_cache = false;
700 if (!has_single_use (op0)
701 && (subcode == POINTER_PLUS_EXPR
702 || subcode == PLUS_EXPR
703 || subcode == MINUS_EXPR
704 || subcode == MULT_EXPR
705 || subcode == ADDR_EXPR
706 || CONVERT_EXPR_CODE_P (subcode)))
708 use_cache = true;
709 bool existed;
710 std::pair<tree, tree> &e = cache.get_or_insert (op0, &existed);
711 if (existed)
713 if (integer_zerop (e.second))
714 return false;
715 *var = e.first;
716 *off = e.second;
717 return true;
719 e = std::make_pair (op0, ssize_int (0));
722 var0 = gimple_assign_rhs1 (def_stmt);
723 var1 = gimple_assign_rhs2 (def_stmt);
725 bool res = split_constant_offset_1 (type, var0, subcode, var1,
726 var, off, cache);
727 if (res && use_cache)
728 *cache.get (op0) = std::make_pair (*var, *off);
729 return res;
731 CASE_CONVERT:
733 /* We must not introduce undefined overflow, and we must not change
734 the value. Hence we're okay if the inner type doesn't overflow
735 to start with (pointer or signed), the outer type also is an
736 integer or pointer and the outer precision is at least as large
737 as the inner. */
738 tree itype = TREE_TYPE (op0);
739 if ((POINTER_TYPE_P (itype)
740 || (INTEGRAL_TYPE_P (itype) && !TYPE_OVERFLOW_TRAPS (itype)))
741 && TYPE_PRECISION (type) >= TYPE_PRECISION (itype)
742 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)))
744 if (INTEGRAL_TYPE_P (itype) && TYPE_OVERFLOW_WRAPS (itype))
746 /* Split the unconverted operand and try to prove that
747 wrapping isn't a problem. */
748 tree tmp_var, tmp_off;
749 split_constant_offset (op0, &tmp_var, &tmp_off, cache);
751 /* See whether we have an SSA_NAME whose range is known
752 to be [A, B]. */
753 if (TREE_CODE (tmp_var) != SSA_NAME)
754 return false;
755 wide_int var_min, var_max;
756 value_range_kind vr_type = get_range_info (tmp_var, &var_min,
757 &var_max);
758 wide_int var_nonzero = get_nonzero_bits (tmp_var);
759 signop sgn = TYPE_SIGN (itype);
760 if (intersect_range_with_nonzero_bits (vr_type, &var_min,
761 &var_max, var_nonzero,
762 sgn) != VR_RANGE)
763 return false;
765 /* See whether the range of OP0 (i.e. TMP_VAR + TMP_OFF)
766 is known to be [A + TMP_OFF, B + TMP_OFF], with all
767 operations done in ITYPE. The addition must overflow
768 at both ends of the range or at neither. */
769 wi::overflow_type overflow[2];
770 unsigned int prec = TYPE_PRECISION (itype);
771 wide_int woff = wi::to_wide (tmp_off, prec);
772 wide_int op0_min = wi::add (var_min, woff, sgn, &overflow[0]);
773 wi::add (var_max, woff, sgn, &overflow[1]);
774 if ((overflow[0] != wi::OVF_NONE) != (overflow[1] != wi::OVF_NONE))
775 return false;
777 /* Calculate (ssizetype) OP0 - (ssizetype) TMP_VAR. */
778 widest_int diff = (widest_int::from (op0_min, sgn)
779 - widest_int::from (var_min, sgn));
780 var0 = tmp_var;
781 *off = wide_int_to_tree (ssizetype, diff);
783 else
784 split_constant_offset (op0, &var0, off, cache);
785 *var = fold_convert (type, var0);
786 return true;
788 return false;
791 default:
792 return false;
796 /* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
797 will be ssizetype. */
799 static void
800 split_constant_offset (tree exp, tree *var, tree *off,
801 hash_map<tree, std::pair<tree, tree> > &cache)
803 tree type = TREE_TYPE (exp), op0, op1, e, o;
804 enum tree_code code;
806 *var = exp;
807 *off = ssize_int (0);
809 if (tree_is_chrec (exp)
810 || get_gimple_rhs_class (TREE_CODE (exp)) == GIMPLE_TERNARY_RHS)
811 return;
813 code = TREE_CODE (exp);
814 extract_ops_from_tree (exp, &code, &op0, &op1);
815 if (split_constant_offset_1 (type, op0, code, op1, &e, &o, cache))
817 *var = e;
818 *off = o;
822 void
823 split_constant_offset (tree exp, tree *var, tree *off)
825 static hash_map<tree, std::pair<tree, tree> > *cache;
826 if (!cache)
827 cache = new hash_map<tree, std::pair<tree, tree> > (37);
828 split_constant_offset (exp, var, off, *cache);
829 cache->empty ();
832 /* Returns the address ADDR of an object in a canonical shape (without nop
833 casts, and with type of pointer to the object). */
835 static tree
836 canonicalize_base_object_address (tree addr)
838 tree orig = addr;
840 STRIP_NOPS (addr);
842 /* The base address may be obtained by casting from integer, in that case
843 keep the cast. */
844 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
845 return orig;
847 if (TREE_CODE (addr) != ADDR_EXPR)
848 return addr;
850 return build_fold_addr_expr (TREE_OPERAND (addr, 0));
853 /* Analyze the behavior of memory reference REF within STMT.
854 There are two modes:
856 - BB analysis. In this case we simply split the address into base,
857 init and offset components, without reference to any containing loop.
858 The resulting base and offset are general expressions and they can
859 vary arbitrarily from one iteration of the containing loop to the next.
860 The step is always zero.
862 - loop analysis. In this case we analyze the reference both wrt LOOP
863 and on the basis that the reference occurs (is "used") in LOOP;
864 see the comment above analyze_scalar_evolution_in_loop for more
865 information about this distinction. The base, init, offset and
866 step fields are all invariant in LOOP.
868 Perform BB analysis if LOOP is null, or if LOOP is the function's
869 dummy outermost loop. In other cases perform loop analysis.
871 Return true if the analysis succeeded and store the results in DRB if so.
872 BB analysis can only fail for bitfield or reversed-storage accesses. */
874 opt_result
875 dr_analyze_innermost (innermost_loop_behavior *drb, tree ref,
876 struct loop *loop, const gimple *stmt)
878 poly_int64 pbitsize, pbitpos;
879 tree base, poffset;
880 machine_mode pmode;
881 int punsignedp, preversep, pvolatilep;
882 affine_iv base_iv, offset_iv;
883 tree init, dinit, step;
884 bool in_loop = (loop && loop->num);
886 if (dump_file && (dump_flags & TDF_DETAILS))
887 fprintf (dump_file, "analyze_innermost: ");
889 base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset, &pmode,
890 &punsignedp, &preversep, &pvolatilep);
891 gcc_assert (base != NULL_TREE);
893 poly_int64 pbytepos;
894 if (!multiple_p (pbitpos, BITS_PER_UNIT, &pbytepos))
895 return opt_result::failure_at (stmt,
896 "failed: bit offset alignment.\n");
898 if (preversep)
899 return opt_result::failure_at (stmt,
900 "failed: reverse storage order.\n");
902 /* Calculate the alignment and misalignment for the inner reference. */
903 unsigned int HOST_WIDE_INT bit_base_misalignment;
904 unsigned int bit_base_alignment;
905 get_object_alignment_1 (base, &bit_base_alignment, &bit_base_misalignment);
907 /* There are no bitfield references remaining in BASE, so the values
908 we got back must be whole bytes. */
909 gcc_assert (bit_base_alignment % BITS_PER_UNIT == 0
910 && bit_base_misalignment % BITS_PER_UNIT == 0);
911 unsigned int base_alignment = bit_base_alignment / BITS_PER_UNIT;
912 poly_int64 base_misalignment = bit_base_misalignment / BITS_PER_UNIT;
914 if (TREE_CODE (base) == MEM_REF)
916 if (!integer_zerop (TREE_OPERAND (base, 1)))
918 /* Subtract MOFF from the base and add it to POFFSET instead.
919 Adjust the misalignment to reflect the amount we subtracted. */
920 poly_offset_int moff = mem_ref_offset (base);
921 base_misalignment -= moff.force_shwi ();
922 tree mofft = wide_int_to_tree (sizetype, moff);
923 if (!poffset)
924 poffset = mofft;
925 else
926 poffset = size_binop (PLUS_EXPR, poffset, mofft);
928 base = TREE_OPERAND (base, 0);
930 else
931 base = build_fold_addr_expr (base);
933 if (in_loop)
935 if (!simple_iv (loop, loop, base, &base_iv, true))
936 return opt_result::failure_at
937 (stmt, "failed: evolution of base is not affine.\n");
939 else
941 base_iv.base = base;
942 base_iv.step = ssize_int (0);
943 base_iv.no_overflow = true;
946 if (!poffset)
948 offset_iv.base = ssize_int (0);
949 offset_iv.step = ssize_int (0);
951 else
953 if (!in_loop)
955 offset_iv.base = poffset;
956 offset_iv.step = ssize_int (0);
958 else if (!simple_iv (loop, loop, poffset, &offset_iv, true))
959 return opt_result::failure_at
960 (stmt, "failed: evolution of offset is not affine.\n");
963 init = ssize_int (pbytepos);
965 /* Subtract any constant component from the base and add it to INIT instead.
966 Adjust the misalignment to reflect the amount we subtracted. */
967 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
968 init = size_binop (PLUS_EXPR, init, dinit);
969 base_misalignment -= TREE_INT_CST_LOW (dinit);
971 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
972 init = size_binop (PLUS_EXPR, init, dinit);
974 step = size_binop (PLUS_EXPR,
975 fold_convert (ssizetype, base_iv.step),
976 fold_convert (ssizetype, offset_iv.step));
978 base = canonicalize_base_object_address (base_iv.base);
980 /* See if get_pointer_alignment can guarantee a higher alignment than
981 the one we calculated above. */
982 unsigned int HOST_WIDE_INT alt_misalignment;
983 unsigned int alt_alignment;
984 get_pointer_alignment_1 (base, &alt_alignment, &alt_misalignment);
986 /* As above, these values must be whole bytes. */
987 gcc_assert (alt_alignment % BITS_PER_UNIT == 0
988 && alt_misalignment % BITS_PER_UNIT == 0);
989 alt_alignment /= BITS_PER_UNIT;
990 alt_misalignment /= BITS_PER_UNIT;
992 if (base_alignment < alt_alignment)
994 base_alignment = alt_alignment;
995 base_misalignment = alt_misalignment;
998 drb->base_address = base;
999 drb->offset = fold_convert (ssizetype, offset_iv.base);
1000 drb->init = init;
1001 drb->step = step;
1002 if (known_misalignment (base_misalignment, base_alignment,
1003 &drb->base_misalignment))
1004 drb->base_alignment = base_alignment;
1005 else
1007 drb->base_alignment = known_alignment (base_misalignment);
1008 drb->base_misalignment = 0;
1010 drb->offset_alignment = highest_pow2_factor (offset_iv.base);
1011 drb->step_alignment = highest_pow2_factor (step);
1013 if (dump_file && (dump_flags & TDF_DETAILS))
1014 fprintf (dump_file, "success.\n");
1016 return opt_result::success ();
1019 /* Return true if OP is a valid component reference for a DR access
1020 function. This accepts a subset of what handled_component_p accepts. */
1022 static bool
1023 access_fn_component_p (tree op)
1025 switch (TREE_CODE (op))
1027 case REALPART_EXPR:
1028 case IMAGPART_EXPR:
1029 case ARRAY_REF:
1030 return true;
1032 case COMPONENT_REF:
1033 return TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == RECORD_TYPE;
1035 default:
1036 return false;
1040 /* Determines the base object and the list of indices of memory reference
1041 DR, analyzed in LOOP and instantiated before NEST. */
1043 static void
1044 dr_analyze_indices (struct data_reference *dr, edge nest, loop_p loop)
1046 vec<tree> access_fns = vNULL;
1047 tree ref, op;
1048 tree base, off, access_fn;
1050 /* If analyzing a basic-block there are no indices to analyze
1051 and thus no access functions. */
1052 if (!nest)
1054 DR_BASE_OBJECT (dr) = DR_REF (dr);
1055 DR_ACCESS_FNS (dr).create (0);
1056 return;
1059 ref = DR_REF (dr);
1061 /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
1062 into a two element array with a constant index. The base is
1063 then just the immediate underlying object. */
1064 if (TREE_CODE (ref) == REALPART_EXPR)
1066 ref = TREE_OPERAND (ref, 0);
1067 access_fns.safe_push (integer_zero_node);
1069 else if (TREE_CODE (ref) == IMAGPART_EXPR)
1071 ref = TREE_OPERAND (ref, 0);
1072 access_fns.safe_push (integer_one_node);
1075 /* Analyze access functions of dimensions we know to be independent.
1076 The list of component references handled here should be kept in
1077 sync with access_fn_component_p. */
1078 while (handled_component_p (ref))
1080 if (TREE_CODE (ref) == ARRAY_REF)
1082 op = TREE_OPERAND (ref, 1);
1083 access_fn = analyze_scalar_evolution (loop, op);
1084 access_fn = instantiate_scev (nest, loop, access_fn);
1085 access_fns.safe_push (access_fn);
1087 else if (TREE_CODE (ref) == COMPONENT_REF
1088 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
1090 /* For COMPONENT_REFs of records (but not unions!) use the
1091 FIELD_DECL offset as constant access function so we can
1092 disambiguate a[i].f1 and a[i].f2. */
1093 tree off = component_ref_field_offset (ref);
1094 off = size_binop (PLUS_EXPR,
1095 size_binop (MULT_EXPR,
1096 fold_convert (bitsizetype, off),
1097 bitsize_int (BITS_PER_UNIT)),
1098 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
1099 access_fns.safe_push (off);
1101 else
1102 /* If we have an unhandled component we could not translate
1103 to an access function stop analyzing. We have determined
1104 our base object in this case. */
1105 break;
1107 ref = TREE_OPERAND (ref, 0);
1110 /* If the address operand of a MEM_REF base has an evolution in the
1111 analyzed nest, add it as an additional independent access-function. */
1112 if (TREE_CODE (ref) == MEM_REF)
1114 op = TREE_OPERAND (ref, 0);
1115 access_fn = analyze_scalar_evolution (loop, op);
1116 access_fn = instantiate_scev (nest, loop, access_fn);
1117 if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
1119 tree orig_type;
1120 tree memoff = TREE_OPERAND (ref, 1);
1121 base = initial_condition (access_fn);
1122 orig_type = TREE_TYPE (base);
1123 STRIP_USELESS_TYPE_CONVERSION (base);
1124 split_constant_offset (base, &base, &off);
1125 STRIP_USELESS_TYPE_CONVERSION (base);
1126 /* Fold the MEM_REF offset into the evolutions initial
1127 value to make more bases comparable. */
1128 if (!integer_zerop (memoff))
1130 off = size_binop (PLUS_EXPR, off,
1131 fold_convert (ssizetype, memoff));
1132 memoff = build_int_cst (TREE_TYPE (memoff), 0);
1134 /* Adjust the offset so it is a multiple of the access type
1135 size and thus we separate bases that can possibly be used
1136 to produce partial overlaps (which the access_fn machinery
1137 cannot handle). */
1138 wide_int rem;
1139 if (TYPE_SIZE_UNIT (TREE_TYPE (ref))
1140 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref))) == INTEGER_CST
1141 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (ref))))
1142 rem = wi::mod_trunc
1143 (wi::to_wide (off),
1144 wi::to_wide (TYPE_SIZE_UNIT (TREE_TYPE (ref))),
1145 SIGNED);
1146 else
1147 /* If we can't compute the remainder simply force the initial
1148 condition to zero. */
1149 rem = wi::to_wide (off);
1150 off = wide_int_to_tree (ssizetype, wi::to_wide (off) - rem);
1151 memoff = wide_int_to_tree (TREE_TYPE (memoff), rem);
1152 /* And finally replace the initial condition. */
1153 access_fn = chrec_replace_initial_condition
1154 (access_fn, fold_convert (orig_type, off));
1155 /* ??? This is still not a suitable base object for
1156 dr_may_alias_p - the base object needs to be an
1157 access that covers the object as whole. With
1158 an evolution in the pointer this cannot be
1159 guaranteed.
1160 As a band-aid, mark the access so we can special-case
1161 it in dr_may_alias_p. */
1162 tree old = ref;
1163 ref = fold_build2_loc (EXPR_LOCATION (ref),
1164 MEM_REF, TREE_TYPE (ref),
1165 base, memoff);
1166 MR_DEPENDENCE_CLIQUE (ref) = MR_DEPENDENCE_CLIQUE (old);
1167 MR_DEPENDENCE_BASE (ref) = MR_DEPENDENCE_BASE (old);
1168 DR_UNCONSTRAINED_BASE (dr) = true;
1169 access_fns.safe_push (access_fn);
1172 else if (DECL_P (ref))
1174 /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
1175 ref = build2 (MEM_REF, TREE_TYPE (ref),
1176 build_fold_addr_expr (ref),
1177 build_int_cst (reference_alias_ptr_type (ref), 0));
1180 DR_BASE_OBJECT (dr) = ref;
1181 DR_ACCESS_FNS (dr) = access_fns;
1184 /* Extracts the alias analysis information from the memory reference DR. */
1186 static void
1187 dr_analyze_alias (struct data_reference *dr)
1189 tree ref = DR_REF (dr);
1190 tree base = get_base_address (ref), addr;
1192 if (INDIRECT_REF_P (base)
1193 || TREE_CODE (base) == MEM_REF)
1195 addr = TREE_OPERAND (base, 0);
1196 if (TREE_CODE (addr) == SSA_NAME)
1197 DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
1201 /* Frees data reference DR. */
1203 void
1204 free_data_ref (data_reference_p dr)
1206 DR_ACCESS_FNS (dr).release ();
1207 free (dr);
1210 /* Analyze memory reference MEMREF, which is accessed in STMT.
1211 The reference is a read if IS_READ is true, otherwise it is a write.
1212 IS_CONDITIONAL_IN_STMT indicates that the reference is conditional
1213 within STMT, i.e. that it might not occur even if STMT is executed
1214 and runs to completion.
1216 Return the data_reference description of MEMREF. NEST is the outermost
1217 loop in which the reference should be instantiated, LOOP is the loop
1218 in which the data reference should be analyzed. */
1220 struct data_reference *
1221 create_data_ref (edge nest, loop_p loop, tree memref, gimple *stmt,
1222 bool is_read, bool is_conditional_in_stmt)
1224 struct data_reference *dr;
1226 if (dump_file && (dump_flags & TDF_DETAILS))
1228 fprintf (dump_file, "Creating dr for ");
1229 print_generic_expr (dump_file, memref, TDF_SLIM);
1230 fprintf (dump_file, "\n");
1233 dr = XCNEW (struct data_reference);
1234 DR_STMT (dr) = stmt;
1235 DR_REF (dr) = memref;
1236 DR_IS_READ (dr) = is_read;
1237 DR_IS_CONDITIONAL_IN_STMT (dr) = is_conditional_in_stmt;
1239 dr_analyze_innermost (&DR_INNERMOST (dr), memref,
1240 nest != NULL ? loop : NULL, stmt);
1241 dr_analyze_indices (dr, nest, loop);
1242 dr_analyze_alias (dr);
1244 if (dump_file && (dump_flags & TDF_DETAILS))
1246 unsigned i;
1247 fprintf (dump_file, "\tbase_address: ");
1248 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1249 fprintf (dump_file, "\n\toffset from base address: ");
1250 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1251 fprintf (dump_file, "\n\tconstant offset from base address: ");
1252 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1253 fprintf (dump_file, "\n\tstep: ");
1254 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1255 fprintf (dump_file, "\n\tbase alignment: %d", DR_BASE_ALIGNMENT (dr));
1256 fprintf (dump_file, "\n\tbase misalignment: %d",
1257 DR_BASE_MISALIGNMENT (dr));
1258 fprintf (dump_file, "\n\toffset alignment: %d",
1259 DR_OFFSET_ALIGNMENT (dr));
1260 fprintf (dump_file, "\n\tstep alignment: %d", DR_STEP_ALIGNMENT (dr));
1261 fprintf (dump_file, "\n\tbase_object: ");
1262 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1263 fprintf (dump_file, "\n");
1264 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
1266 fprintf (dump_file, "\tAccess function %d: ", i);
1267 print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
1271 return dr;
1274 /* A helper function computes order between two tree epxressions T1 and T2.
1275 This is used in comparator functions sorting objects based on the order
1276 of tree expressions. The function returns -1, 0, or 1. */
1279 data_ref_compare_tree (tree t1, tree t2)
1281 int i, cmp;
1282 enum tree_code code;
1283 char tclass;
1285 if (t1 == t2)
1286 return 0;
1287 if (t1 == NULL)
1288 return -1;
1289 if (t2 == NULL)
1290 return 1;
1292 STRIP_USELESS_TYPE_CONVERSION (t1);
1293 STRIP_USELESS_TYPE_CONVERSION (t2);
1294 if (t1 == t2)
1295 return 0;
1297 if (TREE_CODE (t1) != TREE_CODE (t2)
1298 && ! (CONVERT_EXPR_P (t1) && CONVERT_EXPR_P (t2)))
1299 return TREE_CODE (t1) < TREE_CODE (t2) ? -1 : 1;
1301 code = TREE_CODE (t1);
1302 switch (code)
1304 case INTEGER_CST:
1305 return tree_int_cst_compare (t1, t2);
1307 case STRING_CST:
1308 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2))
1309 return TREE_STRING_LENGTH (t1) < TREE_STRING_LENGTH (t2) ? -1 : 1;
1310 return memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1311 TREE_STRING_LENGTH (t1));
1313 case SSA_NAME:
1314 if (SSA_NAME_VERSION (t1) != SSA_NAME_VERSION (t2))
1315 return SSA_NAME_VERSION (t1) < SSA_NAME_VERSION (t2) ? -1 : 1;
1316 break;
1318 default:
1319 if (POLY_INT_CST_P (t1))
1320 return compare_sizes_for_sort (wi::to_poly_widest (t1),
1321 wi::to_poly_widest (t2));
1323 tclass = TREE_CODE_CLASS (code);
1325 /* For decls, compare their UIDs. */
1326 if (tclass == tcc_declaration)
1328 if (DECL_UID (t1) != DECL_UID (t2))
1329 return DECL_UID (t1) < DECL_UID (t2) ? -1 : 1;
1330 break;
1332 /* For expressions, compare their operands recursively. */
1333 else if (IS_EXPR_CODE_CLASS (tclass))
1335 for (i = TREE_OPERAND_LENGTH (t1) - 1; i >= 0; --i)
1337 cmp = data_ref_compare_tree (TREE_OPERAND (t1, i),
1338 TREE_OPERAND (t2, i));
1339 if (cmp != 0)
1340 return cmp;
1343 else
1344 gcc_unreachable ();
1347 return 0;
1350 /* Return TRUE it's possible to resolve data dependence DDR by runtime alias
1351 check. */
1353 opt_result
1354 runtime_alias_check_p (ddr_p ddr, struct loop *loop, bool speed_p)
1356 if (dump_enabled_p ())
1357 dump_printf (MSG_NOTE,
1358 "consider run-time aliasing test between %T and %T\n",
1359 DR_REF (DDR_A (ddr)), DR_REF (DDR_B (ddr)));
1361 if (!speed_p)
1362 return opt_result::failure_at (DR_STMT (DDR_A (ddr)),
1363 "runtime alias check not supported when"
1364 " optimizing for size.\n");
1366 /* FORNOW: We don't support versioning with outer-loop in either
1367 vectorization or loop distribution. */
1368 if (loop != NULL && loop->inner != NULL)
1369 return opt_result::failure_at (DR_STMT (DDR_A (ddr)),
1370 "runtime alias check not supported for"
1371 " outer loop.\n");
1373 return opt_result::success ();
1376 /* Operator == between two dr_with_seg_len objects.
1378 This equality operator is used to make sure two data refs
1379 are the same one so that we will consider to combine the
1380 aliasing checks of those two pairs of data dependent data
1381 refs. */
1383 static bool
1384 operator == (const dr_with_seg_len& d1,
1385 const dr_with_seg_len& d2)
1387 return (operand_equal_p (DR_BASE_ADDRESS (d1.dr),
1388 DR_BASE_ADDRESS (d2.dr), 0)
1389 && data_ref_compare_tree (DR_OFFSET (d1.dr), DR_OFFSET (d2.dr)) == 0
1390 && data_ref_compare_tree (DR_INIT (d1.dr), DR_INIT (d2.dr)) == 0
1391 && data_ref_compare_tree (d1.seg_len, d2.seg_len) == 0
1392 && known_eq (d1.access_size, d2.access_size)
1393 && d1.align == d2.align);
1396 /* Comparison function for sorting objects of dr_with_seg_len_pair_t
1397 so that we can combine aliasing checks in one scan. */
1399 static int
1400 comp_dr_with_seg_len_pair (const void *pa_, const void *pb_)
1402 const dr_with_seg_len_pair_t* pa = (const dr_with_seg_len_pair_t *) pa_;
1403 const dr_with_seg_len_pair_t* pb = (const dr_with_seg_len_pair_t *) pb_;
1404 const dr_with_seg_len &a1 = pa->first, &a2 = pa->second;
1405 const dr_with_seg_len &b1 = pb->first, &b2 = pb->second;
1407 /* For DR pairs (a, b) and (c, d), we only consider to merge the alias checks
1408 if a and c have the same basic address snd step, and b and d have the same
1409 address and step. Therefore, if any a&c or b&d don't have the same address
1410 and step, we don't care the order of those two pairs after sorting. */
1411 int comp_res;
1413 if ((comp_res = data_ref_compare_tree (DR_BASE_ADDRESS (a1.dr),
1414 DR_BASE_ADDRESS (b1.dr))) != 0)
1415 return comp_res;
1416 if ((comp_res = data_ref_compare_tree (DR_BASE_ADDRESS (a2.dr),
1417 DR_BASE_ADDRESS (b2.dr))) != 0)
1418 return comp_res;
1419 if ((comp_res = data_ref_compare_tree (DR_STEP (a1.dr),
1420 DR_STEP (b1.dr))) != 0)
1421 return comp_res;
1422 if ((comp_res = data_ref_compare_tree (DR_STEP (a2.dr),
1423 DR_STEP (b2.dr))) != 0)
1424 return comp_res;
1425 if ((comp_res = data_ref_compare_tree (DR_OFFSET (a1.dr),
1426 DR_OFFSET (b1.dr))) != 0)
1427 return comp_res;
1428 if ((comp_res = data_ref_compare_tree (DR_INIT (a1.dr),
1429 DR_INIT (b1.dr))) != 0)
1430 return comp_res;
1431 if ((comp_res = data_ref_compare_tree (DR_OFFSET (a2.dr),
1432 DR_OFFSET (b2.dr))) != 0)
1433 return comp_res;
1434 if ((comp_res = data_ref_compare_tree (DR_INIT (a2.dr),
1435 DR_INIT (b2.dr))) != 0)
1436 return comp_res;
1438 return 0;
1441 /* Merge alias checks recorded in ALIAS_PAIRS and remove redundant ones.
1442 FACTOR is number of iterations that each data reference is accessed.
1444 Basically, for each pair of dependent data refs store_ptr_0 & load_ptr_0,
1445 we create an expression:
1447 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
1448 || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
1450 for aliasing checks. However, in some cases we can decrease the number
1451 of checks by combining two checks into one. For example, suppose we have
1452 another pair of data refs store_ptr_0 & load_ptr_1, and if the following
1453 condition is satisfied:
1455 load_ptr_0 < load_ptr_1 &&
1456 load_ptr_1 - load_ptr_0 - load_segment_length_0 < store_segment_length_0
1458 (this condition means, in each iteration of vectorized loop, the accessed
1459 memory of store_ptr_0 cannot be between the memory of load_ptr_0 and
1460 load_ptr_1.)
1462 we then can use only the following expression to finish the alising checks
1463 between store_ptr_0 & load_ptr_0 and store_ptr_0 & load_ptr_1:
1465 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
1466 || (load_ptr_1 + load_segment_length_1 <= store_ptr_0))
1468 Note that we only consider that load_ptr_0 and load_ptr_1 have the same
1469 basic address. */
1471 void
1472 prune_runtime_alias_test_list (vec<dr_with_seg_len_pair_t> *alias_pairs,
1473 poly_uint64)
1475 /* Sort the collected data ref pairs so that we can scan them once to
1476 combine all possible aliasing checks. */
1477 alias_pairs->qsort (comp_dr_with_seg_len_pair);
1479 /* Scan the sorted dr pairs and check if we can combine alias checks
1480 of two neighboring dr pairs. */
1481 for (size_t i = 1; i < alias_pairs->length (); ++i)
1483 /* Deal with two ddrs (dr_a1, dr_b1) and (dr_a2, dr_b2). */
1484 dr_with_seg_len *dr_a1 = &(*alias_pairs)[i-1].first,
1485 *dr_b1 = &(*alias_pairs)[i-1].second,
1486 *dr_a2 = &(*alias_pairs)[i].first,
1487 *dr_b2 = &(*alias_pairs)[i].second;
1489 /* Remove duplicate data ref pairs. */
1490 if (*dr_a1 == *dr_a2 && *dr_b1 == *dr_b2)
1492 if (dump_enabled_p ())
1493 dump_printf (MSG_NOTE, "found equal ranges %T, %T and %T, %T\n",
1494 DR_REF (dr_a1->dr), DR_REF (dr_b1->dr),
1495 DR_REF (dr_a2->dr), DR_REF (dr_b2->dr));
1496 alias_pairs->ordered_remove (i--);
1497 continue;
1500 if (*dr_a1 == *dr_a2 || *dr_b1 == *dr_b2)
1502 /* We consider the case that DR_B1 and DR_B2 are same memrefs,
1503 and DR_A1 and DR_A2 are two consecutive memrefs. */
1504 if (*dr_a1 == *dr_a2)
1506 std::swap (dr_a1, dr_b1);
1507 std::swap (dr_a2, dr_b2);
1510 poly_int64 init_a1, init_a2;
1511 /* Only consider cases in which the distance between the initial
1512 DR_A1 and the initial DR_A2 is known at compile time. */
1513 if (!operand_equal_p (DR_BASE_ADDRESS (dr_a1->dr),
1514 DR_BASE_ADDRESS (dr_a2->dr), 0)
1515 || !operand_equal_p (DR_OFFSET (dr_a1->dr),
1516 DR_OFFSET (dr_a2->dr), 0)
1517 || !poly_int_tree_p (DR_INIT (dr_a1->dr), &init_a1)
1518 || !poly_int_tree_p (DR_INIT (dr_a2->dr), &init_a2))
1519 continue;
1521 /* Don't combine if we can't tell which one comes first. */
1522 if (!ordered_p (init_a1, init_a2))
1523 continue;
1525 /* Make sure dr_a1 starts left of dr_a2. */
1526 if (maybe_gt (init_a1, init_a2))
1528 std::swap (*dr_a1, *dr_a2);
1529 std::swap (init_a1, init_a2);
1532 /* Work out what the segment length would be if we did combine
1533 DR_A1 and DR_A2:
1535 - If DR_A1 and DR_A2 have equal lengths, that length is
1536 also the combined length.
1538 - If DR_A1 and DR_A2 both have negative "lengths", the combined
1539 length is the lower bound on those lengths.
1541 - If DR_A1 and DR_A2 both have positive lengths, the combined
1542 length is the upper bound on those lengths.
1544 Other cases are unlikely to give a useful combination.
1546 The lengths both have sizetype, so the sign is taken from
1547 the step instead. */
1548 if (!operand_equal_p (dr_a1->seg_len, dr_a2->seg_len, 0))
1550 poly_uint64 seg_len_a1, seg_len_a2;
1551 if (!poly_int_tree_p (dr_a1->seg_len, &seg_len_a1)
1552 || !poly_int_tree_p (dr_a2->seg_len, &seg_len_a2))
1553 continue;
1555 tree indicator_a = dr_direction_indicator (dr_a1->dr);
1556 if (TREE_CODE (indicator_a) != INTEGER_CST)
1557 continue;
1559 tree indicator_b = dr_direction_indicator (dr_a2->dr);
1560 if (TREE_CODE (indicator_b) != INTEGER_CST)
1561 continue;
1563 int sign_a = tree_int_cst_sgn (indicator_a);
1564 int sign_b = tree_int_cst_sgn (indicator_b);
1566 poly_uint64 new_seg_len;
1567 if (sign_a <= 0 && sign_b <= 0)
1568 new_seg_len = lower_bound (seg_len_a1, seg_len_a2);
1569 else if (sign_a >= 0 && sign_b >= 0)
1570 new_seg_len = upper_bound (seg_len_a1, seg_len_a2);
1571 else
1572 continue;
1574 dr_a1->seg_len = build_int_cst (TREE_TYPE (dr_a1->seg_len),
1575 new_seg_len);
1576 dr_a1->align = MIN (dr_a1->align, known_alignment (new_seg_len));
1579 /* This is always positive due to the swap above. */
1580 poly_uint64 diff = init_a2 - init_a1;
1582 /* The new check will start at DR_A1. Make sure that its access
1583 size encompasses the initial DR_A2. */
1584 if (maybe_lt (dr_a1->access_size, diff + dr_a2->access_size))
1586 dr_a1->access_size = upper_bound (dr_a1->access_size,
1587 diff + dr_a2->access_size);
1588 unsigned int new_align = known_alignment (dr_a1->access_size);
1589 dr_a1->align = MIN (dr_a1->align, new_align);
1591 if (dump_enabled_p ())
1592 dump_printf (MSG_NOTE, "merging ranges for %T, %T and %T, %T\n",
1593 DR_REF (dr_a1->dr), DR_REF (dr_b1->dr),
1594 DR_REF (dr_a2->dr), DR_REF (dr_b2->dr));
1595 alias_pairs->ordered_remove (i);
1596 i--;
1601 /* Given LOOP's two data references and segment lengths described by DR_A
1602 and DR_B, create expression checking if the two addresses ranges intersect
1603 with each other based on index of the two addresses. This can only be
1604 done if DR_A and DR_B referring to the same (array) object and the index
1605 is the only difference. For example:
1607 DR_A DR_B
1608 data-ref arr[i] arr[j]
1609 base_object arr arr
1610 index {i_0, +, 1}_loop {j_0, +, 1}_loop
1612 The addresses and their index are like:
1614 |<- ADDR_A ->| |<- ADDR_B ->|
1615 ------------------------------------------------------->
1616 | | | | | | | | | |
1617 ------------------------------------------------------->
1618 i_0 ... i_0+4 j_0 ... j_0+4
1620 We can create expression based on index rather than address:
1622 (i_0 + 4 < j_0 || j_0 + 4 < i_0)
1624 Note evolution step of index needs to be considered in comparison. */
1626 static bool
1627 create_intersect_range_checks_index (struct loop *loop, tree *cond_expr,
1628 const dr_with_seg_len& dr_a,
1629 const dr_with_seg_len& dr_b)
1631 if (integer_zerop (DR_STEP (dr_a.dr))
1632 || integer_zerop (DR_STEP (dr_b.dr))
1633 || DR_NUM_DIMENSIONS (dr_a.dr) != DR_NUM_DIMENSIONS (dr_b.dr))
1634 return false;
1636 poly_uint64 seg_len1, seg_len2;
1637 if (!poly_int_tree_p (dr_a.seg_len, &seg_len1)
1638 || !poly_int_tree_p (dr_b.seg_len, &seg_len2))
1639 return false;
1641 if (!tree_fits_shwi_p (DR_STEP (dr_a.dr)))
1642 return false;
1644 if (!operand_equal_p (DR_BASE_OBJECT (dr_a.dr), DR_BASE_OBJECT (dr_b.dr), 0))
1645 return false;
1647 if (!operand_equal_p (DR_STEP (dr_a.dr), DR_STEP (dr_b.dr), 0))
1648 return false;
1650 gcc_assert (TREE_CODE (DR_STEP (dr_a.dr)) == INTEGER_CST);
1652 bool neg_step = tree_int_cst_compare (DR_STEP (dr_a.dr), size_zero_node) < 0;
1653 unsigned HOST_WIDE_INT abs_step = tree_to_shwi (DR_STEP (dr_a.dr));
1654 if (neg_step)
1656 abs_step = -abs_step;
1657 seg_len1 = -seg_len1;
1658 seg_len2 = -seg_len2;
1660 else
1662 /* Include the access size in the length, so that we only have one
1663 tree addition below. */
1664 seg_len1 += dr_a.access_size;
1665 seg_len2 += dr_b.access_size;
1668 /* Infer the number of iterations with which the memory segment is accessed
1669 by DR. In other words, alias is checked if memory segment accessed by
1670 DR_A in some iterations intersect with memory segment accessed by DR_B
1671 in the same amount iterations.
1672 Note segnment length is a linear function of number of iterations with
1673 DR_STEP as the coefficient. */
1674 poly_uint64 niter_len1, niter_len2;
1675 if (!can_div_trunc_p (seg_len1 + abs_step - 1, abs_step, &niter_len1)
1676 || !can_div_trunc_p (seg_len2 + abs_step - 1, abs_step, &niter_len2))
1677 return false;
1679 poly_uint64 niter_access1 = 0, niter_access2 = 0;
1680 if (neg_step)
1682 /* Divide each access size by the byte step, rounding up. */
1683 if (!can_div_trunc_p (dr_a.access_size - abs_step - 1,
1684 abs_step, &niter_access1)
1685 || !can_div_trunc_p (dr_b.access_size + abs_step - 1,
1686 abs_step, &niter_access2))
1687 return false;
1690 unsigned int i;
1691 for (i = 0; i < DR_NUM_DIMENSIONS (dr_a.dr); i++)
1693 tree access1 = DR_ACCESS_FN (dr_a.dr, i);
1694 tree access2 = DR_ACCESS_FN (dr_b.dr, i);
1695 /* Two indices must be the same if they are not scev, or not scev wrto
1696 current loop being vecorized. */
1697 if (TREE_CODE (access1) != POLYNOMIAL_CHREC
1698 || TREE_CODE (access2) != POLYNOMIAL_CHREC
1699 || CHREC_VARIABLE (access1) != (unsigned)loop->num
1700 || CHREC_VARIABLE (access2) != (unsigned)loop->num)
1702 if (operand_equal_p (access1, access2, 0))
1703 continue;
1705 return false;
1707 /* The two indices must have the same step. */
1708 if (!operand_equal_p (CHREC_RIGHT (access1), CHREC_RIGHT (access2), 0))
1709 return false;
1711 tree idx_step = CHREC_RIGHT (access1);
1712 /* Index must have const step, otherwise DR_STEP won't be constant. */
1713 gcc_assert (TREE_CODE (idx_step) == INTEGER_CST);
1714 /* Index must evaluate in the same direction as DR. */
1715 gcc_assert (!neg_step || tree_int_cst_sign_bit (idx_step) == 1);
1717 tree min1 = CHREC_LEFT (access1);
1718 tree min2 = CHREC_LEFT (access2);
1719 if (!types_compatible_p (TREE_TYPE (min1), TREE_TYPE (min2)))
1720 return false;
1722 /* Ideally, alias can be checked against loop's control IV, but we
1723 need to prove linear mapping between control IV and reference
1724 index. Although that should be true, we check against (array)
1725 index of data reference. Like segment length, index length is
1726 linear function of the number of iterations with index_step as
1727 the coefficient, i.e, niter_len * idx_step. */
1728 tree idx_len1 = fold_build2 (MULT_EXPR, TREE_TYPE (min1), idx_step,
1729 build_int_cst (TREE_TYPE (min1),
1730 niter_len1));
1731 tree idx_len2 = fold_build2 (MULT_EXPR, TREE_TYPE (min2), idx_step,
1732 build_int_cst (TREE_TYPE (min2),
1733 niter_len2));
1734 tree max1 = fold_build2 (PLUS_EXPR, TREE_TYPE (min1), min1, idx_len1);
1735 tree max2 = fold_build2 (PLUS_EXPR, TREE_TYPE (min2), min2, idx_len2);
1736 /* Adjust ranges for negative step. */
1737 if (neg_step)
1739 /* IDX_LEN1 and IDX_LEN2 are negative in this case. */
1740 std::swap (min1, max1);
1741 std::swap (min2, max2);
1743 /* As with the lengths just calculated, we've measured the access
1744 sizes in iterations, so multiply them by the index step. */
1745 tree idx_access1
1746 = fold_build2 (MULT_EXPR, TREE_TYPE (min1), idx_step,
1747 build_int_cst (TREE_TYPE (min1), niter_access1));
1748 tree idx_access2
1749 = fold_build2 (MULT_EXPR, TREE_TYPE (min2), idx_step,
1750 build_int_cst (TREE_TYPE (min2), niter_access2));
1752 /* MINUS_EXPR because the above values are negative. */
1753 max1 = fold_build2 (MINUS_EXPR, TREE_TYPE (max1), max1, idx_access1);
1754 max2 = fold_build2 (MINUS_EXPR, TREE_TYPE (max2), max2, idx_access2);
1756 tree part_cond_expr
1757 = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
1758 fold_build2 (LE_EXPR, boolean_type_node, max1, min2),
1759 fold_build2 (LE_EXPR, boolean_type_node, max2, min1));
1760 if (*cond_expr)
1761 *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
1762 *cond_expr, part_cond_expr);
1763 else
1764 *cond_expr = part_cond_expr;
1766 return true;
1769 /* If ALIGN is nonzero, set up *SEQ_MIN_OUT and *SEQ_MAX_OUT so that for
1770 every address ADDR accessed by D:
1772 *SEQ_MIN_OUT <= ADDR (== ADDR & -ALIGN) <= *SEQ_MAX_OUT
1774 In this case, every element accessed by D is aligned to at least
1775 ALIGN bytes.
1777 If ALIGN is zero then instead set *SEG_MAX_OUT so that:
1779 *SEQ_MIN_OUT <= ADDR < *SEQ_MAX_OUT. */
1781 static void
1782 get_segment_min_max (const dr_with_seg_len &d, tree *seg_min_out,
1783 tree *seg_max_out, HOST_WIDE_INT align)
1785 /* Each access has the following pattern:
1787 <- |seg_len| ->
1788 <--- A: -ve step --->
1789 +-----+-------+-----+-------+-----+
1790 | n-1 | ,.... | 0 | ..... | n-1 |
1791 +-----+-------+-----+-------+-----+
1792 <--- B: +ve step --->
1793 <- |seg_len| ->
1795 base address
1797 where "n" is the number of scalar iterations covered by the segment.
1798 (This should be VF for a particular pair if we know that both steps
1799 are the same, otherwise it will be the full number of scalar loop
1800 iterations.)
1802 A is the range of bytes accessed when the step is negative,
1803 B is the range when the step is positive.
1805 If the access size is "access_size" bytes, the lowest addressed byte is:
1807 base + (step < 0 ? seg_len : 0) [LB]
1809 and the highest addressed byte is always below:
1811 base + (step < 0 ? 0 : seg_len) + access_size [UB]
1813 Thus:
1815 LB <= ADDR < UB
1817 If ALIGN is nonzero, all three values are aligned to at least ALIGN
1818 bytes, so:
1820 LB <= ADDR <= UB - ALIGN
1822 where "- ALIGN" folds naturally with the "+ access_size" and often
1823 cancels it out.
1825 We don't try to simplify LB and UB beyond this (e.g. by using
1826 MIN and MAX based on whether seg_len rather than the stride is
1827 negative) because it is possible for the absolute size of the
1828 segment to overflow the range of a ssize_t.
1830 Keeping the pointer_plus outside of the cond_expr should allow
1831 the cond_exprs to be shared with other alias checks. */
1832 tree indicator = dr_direction_indicator (d.dr);
1833 tree neg_step = fold_build2 (LT_EXPR, boolean_type_node,
1834 fold_convert (ssizetype, indicator),
1835 ssize_int (0));
1836 tree addr_base = fold_build_pointer_plus (DR_BASE_ADDRESS (d.dr),
1837 DR_OFFSET (d.dr));
1838 addr_base = fold_build_pointer_plus (addr_base, DR_INIT (d.dr));
1839 tree seg_len
1840 = fold_convert (sizetype, rewrite_to_non_trapping_overflow (d.seg_len));
1842 tree min_reach = fold_build3 (COND_EXPR, sizetype, neg_step,
1843 seg_len, size_zero_node);
1844 tree max_reach = fold_build3 (COND_EXPR, sizetype, neg_step,
1845 size_zero_node, seg_len);
1846 max_reach = fold_build2 (PLUS_EXPR, sizetype, max_reach,
1847 size_int (d.access_size - align));
1849 *seg_min_out = fold_build_pointer_plus (addr_base, min_reach);
1850 *seg_max_out = fold_build_pointer_plus (addr_base, max_reach);
1853 /* Given two data references and segment lengths described by DR_A and DR_B,
1854 create expression checking if the two addresses ranges intersect with
1855 each other:
1857 ((DR_A_addr_0 + DR_A_segment_length_0) <= DR_B_addr_0)
1858 || (DR_B_addr_0 + DER_B_segment_length_0) <= DR_A_addr_0)) */
1860 static void
1861 create_intersect_range_checks (struct loop *loop, tree *cond_expr,
1862 const dr_with_seg_len& dr_a,
1863 const dr_with_seg_len& dr_b)
1865 *cond_expr = NULL_TREE;
1866 if (create_intersect_range_checks_index (loop, cond_expr, dr_a, dr_b))
1867 return;
1869 unsigned HOST_WIDE_INT min_align;
1870 tree_code cmp_code;
1871 if (TREE_CODE (DR_STEP (dr_a.dr)) == INTEGER_CST
1872 && TREE_CODE (DR_STEP (dr_b.dr)) == INTEGER_CST)
1874 /* In this case adding access_size to seg_len is likely to give
1875 a simple X * step, where X is either the number of scalar
1876 iterations or the vectorization factor. We're better off
1877 keeping that, rather than subtracting an alignment from it.
1879 In this case the maximum values are exclusive and so there is
1880 no alias if the maximum of one segment equals the minimum
1881 of another. */
1882 min_align = 0;
1883 cmp_code = LE_EXPR;
1885 else
1887 /* Calculate the minimum alignment shared by all four pointers,
1888 then arrange for this alignment to be subtracted from the
1889 exclusive maximum values to get inclusive maximum values.
1890 This "- min_align" is cumulative with a "+ access_size"
1891 in the calculation of the maximum values. In the best
1892 (and common) case, the two cancel each other out, leaving
1893 us with an inclusive bound based only on seg_len. In the
1894 worst case we're simply adding a smaller number than before.
1896 Because the maximum values are inclusive, there is an alias
1897 if the maximum value of one segment is equal to the minimum
1898 value of the other. */
1899 min_align = MIN (dr_a.align, dr_b.align);
1900 cmp_code = LT_EXPR;
1903 tree seg_a_min, seg_a_max, seg_b_min, seg_b_max;
1904 get_segment_min_max (dr_a, &seg_a_min, &seg_a_max, min_align);
1905 get_segment_min_max (dr_b, &seg_b_min, &seg_b_max, min_align);
1907 *cond_expr
1908 = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
1909 fold_build2 (cmp_code, boolean_type_node, seg_a_max, seg_b_min),
1910 fold_build2 (cmp_code, boolean_type_node, seg_b_max, seg_a_min));
1913 /* Create a conditional expression that represents the run-time checks for
1914 overlapping of address ranges represented by a list of data references
1915 pairs passed in ALIAS_PAIRS. Data references are in LOOP. The returned
1916 COND_EXPR is the conditional expression to be used in the if statement
1917 that controls which version of the loop gets executed at runtime. */
1919 void
1920 create_runtime_alias_checks (struct loop *loop,
1921 vec<dr_with_seg_len_pair_t> *alias_pairs,
1922 tree * cond_expr)
1924 tree part_cond_expr;
1926 fold_defer_overflow_warnings ();
1927 for (size_t i = 0, s = alias_pairs->length (); i < s; ++i)
1929 const dr_with_seg_len& dr_a = (*alias_pairs)[i].first;
1930 const dr_with_seg_len& dr_b = (*alias_pairs)[i].second;
1932 if (dump_enabled_p ())
1933 dump_printf (MSG_NOTE,
1934 "create runtime check for data references %T and %T\n",
1935 DR_REF (dr_a.dr), DR_REF (dr_b.dr));
1937 /* Create condition expression for each pair data references. */
1938 create_intersect_range_checks (loop, &part_cond_expr, dr_a, dr_b);
1939 if (*cond_expr)
1940 *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
1941 *cond_expr, part_cond_expr);
1942 else
1943 *cond_expr = part_cond_expr;
1945 fold_undefer_and_ignore_overflow_warnings ();
1948 /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
1949 expressions. */
1950 static bool
1951 dr_equal_offsets_p1 (tree offset1, tree offset2)
1953 bool res;
1955 STRIP_NOPS (offset1);
1956 STRIP_NOPS (offset2);
1958 if (offset1 == offset2)
1959 return true;
1961 if (TREE_CODE (offset1) != TREE_CODE (offset2)
1962 || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
1963 return false;
1965 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
1966 TREE_OPERAND (offset2, 0));
1968 if (!res || !BINARY_CLASS_P (offset1))
1969 return res;
1971 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
1972 TREE_OPERAND (offset2, 1));
1974 return res;
1977 /* Check if DRA and DRB have equal offsets. */
1978 bool
1979 dr_equal_offsets_p (struct data_reference *dra,
1980 struct data_reference *drb)
1982 tree offset1, offset2;
1984 offset1 = DR_OFFSET (dra);
1985 offset2 = DR_OFFSET (drb);
1987 return dr_equal_offsets_p1 (offset1, offset2);
1990 /* Returns true if FNA == FNB. */
1992 static bool
1993 affine_function_equal_p (affine_fn fna, affine_fn fnb)
1995 unsigned i, n = fna.length ();
1997 if (n != fnb.length ())
1998 return false;
2000 for (i = 0; i < n; i++)
2001 if (!operand_equal_p (fna[i], fnb[i], 0))
2002 return false;
2004 return true;
2007 /* If all the functions in CF are the same, returns one of them,
2008 otherwise returns NULL. */
2010 static affine_fn
2011 common_affine_function (conflict_function *cf)
2013 unsigned i;
2014 affine_fn comm;
2016 if (!CF_NONTRIVIAL_P (cf))
2017 return affine_fn ();
2019 comm = cf->fns[0];
2021 for (i = 1; i < cf->n; i++)
2022 if (!affine_function_equal_p (comm, cf->fns[i]))
2023 return affine_fn ();
2025 return comm;
2028 /* Returns the base of the affine function FN. */
2030 static tree
2031 affine_function_base (affine_fn fn)
2033 return fn[0];
2036 /* Returns true if FN is a constant. */
2038 static bool
2039 affine_function_constant_p (affine_fn fn)
2041 unsigned i;
2042 tree coef;
2044 for (i = 1; fn.iterate (i, &coef); i++)
2045 if (!integer_zerop (coef))
2046 return false;
2048 return true;
2051 /* Returns true if FN is the zero constant function. */
2053 static bool
2054 affine_function_zero_p (affine_fn fn)
2056 return (integer_zerop (affine_function_base (fn))
2057 && affine_function_constant_p (fn));
2060 /* Returns a signed integer type with the largest precision from TA
2061 and TB. */
2063 static tree
2064 signed_type_for_types (tree ta, tree tb)
2066 if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
2067 return signed_type_for (ta);
2068 else
2069 return signed_type_for (tb);
2072 /* Applies operation OP on affine functions FNA and FNB, and returns the
2073 result. */
2075 static affine_fn
2076 affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
2078 unsigned i, n, m;
2079 affine_fn ret;
2080 tree coef;
2082 if (fnb.length () > fna.length ())
2084 n = fna.length ();
2085 m = fnb.length ();
2087 else
2089 n = fnb.length ();
2090 m = fna.length ();
2093 ret.create (m);
2094 for (i = 0; i < n; i++)
2096 tree type = signed_type_for_types (TREE_TYPE (fna[i]),
2097 TREE_TYPE (fnb[i]));
2098 ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
2101 for (; fna.iterate (i, &coef); i++)
2102 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
2103 coef, integer_zero_node));
2104 for (; fnb.iterate (i, &coef); i++)
2105 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
2106 integer_zero_node, coef));
2108 return ret;
2111 /* Returns the sum of affine functions FNA and FNB. */
2113 static affine_fn
2114 affine_fn_plus (affine_fn fna, affine_fn fnb)
2116 return affine_fn_op (PLUS_EXPR, fna, fnb);
2119 /* Returns the difference of affine functions FNA and FNB. */
2121 static affine_fn
2122 affine_fn_minus (affine_fn fna, affine_fn fnb)
2124 return affine_fn_op (MINUS_EXPR, fna, fnb);
2127 /* Frees affine function FN. */
2129 static void
2130 affine_fn_free (affine_fn fn)
2132 fn.release ();
2135 /* Determine for each subscript in the data dependence relation DDR
2136 the distance. */
2138 static void
2139 compute_subscript_distance (struct data_dependence_relation *ddr)
2141 conflict_function *cf_a, *cf_b;
2142 affine_fn fn_a, fn_b, diff;
2144 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
2146 unsigned int i;
2148 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
2150 struct subscript *subscript;
2152 subscript = DDR_SUBSCRIPT (ddr, i);
2153 cf_a = SUB_CONFLICTS_IN_A (subscript);
2154 cf_b = SUB_CONFLICTS_IN_B (subscript);
2156 fn_a = common_affine_function (cf_a);
2157 fn_b = common_affine_function (cf_b);
2158 if (!fn_a.exists () || !fn_b.exists ())
2160 SUB_DISTANCE (subscript) = chrec_dont_know;
2161 return;
2163 diff = affine_fn_minus (fn_a, fn_b);
2165 if (affine_function_constant_p (diff))
2166 SUB_DISTANCE (subscript) = affine_function_base (diff);
2167 else
2168 SUB_DISTANCE (subscript) = chrec_dont_know;
2170 affine_fn_free (diff);
2175 /* Returns the conflict function for "unknown". */
2177 static conflict_function *
2178 conflict_fn_not_known (void)
2180 conflict_function *fn = XCNEW (conflict_function);
2181 fn->n = NOT_KNOWN;
2183 return fn;
2186 /* Returns the conflict function for "independent". */
2188 static conflict_function *
2189 conflict_fn_no_dependence (void)
2191 conflict_function *fn = XCNEW (conflict_function);
2192 fn->n = NO_DEPENDENCE;
2194 return fn;
2197 /* Returns true if the address of OBJ is invariant in LOOP. */
2199 static bool
2200 object_address_invariant_in_loop_p (const struct loop *loop, const_tree obj)
2202 while (handled_component_p (obj))
2204 if (TREE_CODE (obj) == ARRAY_REF)
2206 for (int i = 1; i < 4; ++i)
2207 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, i),
2208 loop->num))
2209 return false;
2211 else if (TREE_CODE (obj) == COMPONENT_REF)
2213 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
2214 loop->num))
2215 return false;
2217 obj = TREE_OPERAND (obj, 0);
2220 if (!INDIRECT_REF_P (obj)
2221 && TREE_CODE (obj) != MEM_REF)
2222 return true;
2224 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
2225 loop->num);
2228 /* Returns false if we can prove that data references A and B do not alias,
2229 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
2230 considered. */
2232 bool
2233 dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
2234 struct loop *loop_nest)
2236 tree addr_a = DR_BASE_OBJECT (a);
2237 tree addr_b = DR_BASE_OBJECT (b);
2239 /* If we are not processing a loop nest but scalar code we
2240 do not need to care about possible cross-iteration dependences
2241 and thus can process the full original reference. Do so,
2242 similar to how loop invariant motion applies extra offset-based
2243 disambiguation. */
2244 if (!loop_nest)
2246 aff_tree off1, off2;
2247 poly_widest_int size1, size2;
2248 get_inner_reference_aff (DR_REF (a), &off1, &size1);
2249 get_inner_reference_aff (DR_REF (b), &off2, &size2);
2250 aff_combination_scale (&off1, -1);
2251 aff_combination_add (&off2, &off1);
2252 if (aff_comb_cannot_overlap_p (&off2, size1, size2))
2253 return false;
2256 if ((TREE_CODE (addr_a) == MEM_REF || TREE_CODE (addr_a) == TARGET_MEM_REF)
2257 && (TREE_CODE (addr_b) == MEM_REF || TREE_CODE (addr_b) == TARGET_MEM_REF)
2258 /* For cross-iteration dependences the cliques must be valid for the
2259 whole loop, not just individual iterations. */
2260 && (!loop_nest
2261 || MR_DEPENDENCE_CLIQUE (addr_a) == 1
2262 || MR_DEPENDENCE_CLIQUE (addr_a) == loop_nest->owned_clique)
2263 && MR_DEPENDENCE_CLIQUE (addr_a) == MR_DEPENDENCE_CLIQUE (addr_b)
2264 && MR_DEPENDENCE_BASE (addr_a) != MR_DEPENDENCE_BASE (addr_b))
2265 return false;
2267 /* If we had an evolution in a pointer-based MEM_REF BASE_OBJECT we
2268 do not know the size of the base-object. So we cannot do any
2269 offset/overlap based analysis but have to rely on points-to
2270 information only. */
2271 if (TREE_CODE (addr_a) == MEM_REF
2272 && (DR_UNCONSTRAINED_BASE (a)
2273 || TREE_CODE (TREE_OPERAND (addr_a, 0)) == SSA_NAME))
2275 /* For true dependences we can apply TBAA. */
2276 if (flag_strict_aliasing
2277 && DR_IS_WRITE (a) && DR_IS_READ (b)
2278 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
2279 get_alias_set (DR_REF (b))))
2280 return false;
2281 if (TREE_CODE (addr_b) == MEM_REF)
2282 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
2283 TREE_OPERAND (addr_b, 0));
2284 else
2285 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
2286 build_fold_addr_expr (addr_b));
2288 else if (TREE_CODE (addr_b) == MEM_REF
2289 && (DR_UNCONSTRAINED_BASE (b)
2290 || TREE_CODE (TREE_OPERAND (addr_b, 0)) == SSA_NAME))
2292 /* For true dependences we can apply TBAA. */
2293 if (flag_strict_aliasing
2294 && DR_IS_WRITE (a) && DR_IS_READ (b)
2295 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
2296 get_alias_set (DR_REF (b))))
2297 return false;
2298 if (TREE_CODE (addr_a) == MEM_REF)
2299 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
2300 TREE_OPERAND (addr_b, 0));
2301 else
2302 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
2303 TREE_OPERAND (addr_b, 0));
2306 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
2307 that is being subsetted in the loop nest. */
2308 if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
2309 return refs_output_dependent_p (addr_a, addr_b);
2310 else if (DR_IS_READ (a) && DR_IS_WRITE (b))
2311 return refs_anti_dependent_p (addr_a, addr_b);
2312 return refs_may_alias_p (addr_a, addr_b);
2315 /* REF_A and REF_B both satisfy access_fn_component_p. Return true
2316 if it is meaningful to compare their associated access functions
2317 when checking for dependencies. */
2319 static bool
2320 access_fn_components_comparable_p (tree ref_a, tree ref_b)
2322 /* Allow pairs of component refs from the following sets:
2324 { REALPART_EXPR, IMAGPART_EXPR }
2325 { COMPONENT_REF }
2326 { ARRAY_REF }. */
2327 tree_code code_a = TREE_CODE (ref_a);
2328 tree_code code_b = TREE_CODE (ref_b);
2329 if (code_a == IMAGPART_EXPR)
2330 code_a = REALPART_EXPR;
2331 if (code_b == IMAGPART_EXPR)
2332 code_b = REALPART_EXPR;
2333 if (code_a != code_b)
2334 return false;
2336 if (TREE_CODE (ref_a) == COMPONENT_REF)
2337 /* ??? We cannot simply use the type of operand #0 of the refs here as
2338 the Fortran compiler smuggles type punning into COMPONENT_REFs.
2339 Use the DECL_CONTEXT of the FIELD_DECLs instead. */
2340 return (DECL_CONTEXT (TREE_OPERAND (ref_a, 1))
2341 == DECL_CONTEXT (TREE_OPERAND (ref_b, 1)));
2343 return types_compatible_p (TREE_TYPE (TREE_OPERAND (ref_a, 0)),
2344 TREE_TYPE (TREE_OPERAND (ref_b, 0)));
2347 /* Initialize a data dependence relation between data accesses A and
2348 B. NB_LOOPS is the number of loops surrounding the references: the
2349 size of the classic distance/direction vectors. */
2351 struct data_dependence_relation *
2352 initialize_data_dependence_relation (struct data_reference *a,
2353 struct data_reference *b,
2354 vec<loop_p> loop_nest)
2356 struct data_dependence_relation *res;
2357 unsigned int i;
2359 res = XCNEW (struct data_dependence_relation);
2360 DDR_A (res) = a;
2361 DDR_B (res) = b;
2362 DDR_LOOP_NEST (res).create (0);
2363 DDR_SUBSCRIPTS (res).create (0);
2364 DDR_DIR_VECTS (res).create (0);
2365 DDR_DIST_VECTS (res).create (0);
2367 if (a == NULL || b == NULL)
2369 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
2370 return res;
2373 /* If the data references do not alias, then they are independent. */
2374 if (!dr_may_alias_p (a, b, loop_nest.exists () ? loop_nest[0] : NULL))
2376 DDR_ARE_DEPENDENT (res) = chrec_known;
2377 return res;
2380 unsigned int num_dimensions_a = DR_NUM_DIMENSIONS (a);
2381 unsigned int num_dimensions_b = DR_NUM_DIMENSIONS (b);
2382 if (num_dimensions_a == 0 || num_dimensions_b == 0)
2384 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
2385 return res;
2388 /* For unconstrained bases, the root (highest-indexed) subscript
2389 describes a variation in the base of the original DR_REF rather
2390 than a component access. We have no type that accurately describes
2391 the new DR_BASE_OBJECT (whose TREE_TYPE describes the type *after*
2392 applying this subscript) so limit the search to the last real
2393 component access.
2395 E.g. for:
2397 void
2398 f (int a[][8], int b[][8])
2400 for (int i = 0; i < 8; ++i)
2401 a[i * 2][0] = b[i][0];
2404 the a and b accesses have a single ARRAY_REF component reference [0]
2405 but have two subscripts. */
2406 if (DR_UNCONSTRAINED_BASE (a))
2407 num_dimensions_a -= 1;
2408 if (DR_UNCONSTRAINED_BASE (b))
2409 num_dimensions_b -= 1;
2411 /* These structures describe sequences of component references in
2412 DR_REF (A) and DR_REF (B). Each component reference is tied to a
2413 specific access function. */
2414 struct {
2415 /* The sequence starts at DR_ACCESS_FN (A, START_A) of A and
2416 DR_ACCESS_FN (B, START_B) of B (inclusive) and extends to higher
2417 indices. In C notation, these are the indices of the rightmost
2418 component references; e.g. for a sequence .b.c.d, the start
2419 index is for .d. */
2420 unsigned int start_a;
2421 unsigned int start_b;
2423 /* The sequence contains LENGTH consecutive access functions from
2424 each DR. */
2425 unsigned int length;
2427 /* The enclosing objects for the A and B sequences respectively,
2428 i.e. the objects to which DR_ACCESS_FN (A, START_A + LENGTH - 1)
2429 and DR_ACCESS_FN (B, START_B + LENGTH - 1) are applied. */
2430 tree object_a;
2431 tree object_b;
2432 } full_seq = {}, struct_seq = {};
2434 /* Before each iteration of the loop:
2436 - REF_A is what you get after applying DR_ACCESS_FN (A, INDEX_A) and
2437 - REF_B is what you get after applying DR_ACCESS_FN (B, INDEX_B). */
2438 unsigned int index_a = 0;
2439 unsigned int index_b = 0;
2440 tree ref_a = DR_REF (a);
2441 tree ref_b = DR_REF (b);
2443 /* Now walk the component references from the final DR_REFs back up to
2444 the enclosing base objects. Each component reference corresponds
2445 to one access function in the DR, with access function 0 being for
2446 the final DR_REF and the highest-indexed access function being the
2447 one that is applied to the base of the DR.
2449 Look for a sequence of component references whose access functions
2450 are comparable (see access_fn_components_comparable_p). If more
2451 than one such sequence exists, pick the one nearest the base
2452 (which is the leftmost sequence in C notation). Store this sequence
2453 in FULL_SEQ.
2455 For example, if we have:
2457 struct foo { struct bar s; ... } (*a)[10], (*b)[10];
2459 A: a[0][i].s.c.d
2460 B: __real b[0][i].s.e[i].f
2462 (where d is the same type as the real component of f) then the access
2463 functions would be:
2465 0 1 2 3
2466 A: .d .c .s [i]
2468 0 1 2 3 4 5
2469 B: __real .f [i] .e .s [i]
2471 The A0/B2 column isn't comparable, since .d is a COMPONENT_REF
2472 and [i] is an ARRAY_REF. However, the A1/B3 column contains two
2473 COMPONENT_REF accesses for struct bar, so is comparable. Likewise
2474 the A2/B4 column contains two COMPONENT_REF accesses for struct foo,
2475 so is comparable. The A3/B5 column contains two ARRAY_REFs that
2476 index foo[10] arrays, so is again comparable. The sequence is
2477 therefore:
2479 A: [1, 3] (i.e. [i].s.c)
2480 B: [3, 5] (i.e. [i].s.e)
2482 Also look for sequences of component references whose access
2483 functions are comparable and whose enclosing objects have the same
2484 RECORD_TYPE. Store this sequence in STRUCT_SEQ. In the above
2485 example, STRUCT_SEQ would be:
2487 A: [1, 2] (i.e. s.c)
2488 B: [3, 4] (i.e. s.e) */
2489 while (index_a < num_dimensions_a && index_b < num_dimensions_b)
2491 /* REF_A and REF_B must be one of the component access types
2492 allowed by dr_analyze_indices. */
2493 gcc_checking_assert (access_fn_component_p (ref_a));
2494 gcc_checking_assert (access_fn_component_p (ref_b));
2496 /* Get the immediately-enclosing objects for REF_A and REF_B,
2497 i.e. the references *before* applying DR_ACCESS_FN (A, INDEX_A)
2498 and DR_ACCESS_FN (B, INDEX_B). */
2499 tree object_a = TREE_OPERAND (ref_a, 0);
2500 tree object_b = TREE_OPERAND (ref_b, 0);
2502 tree type_a = TREE_TYPE (object_a);
2503 tree type_b = TREE_TYPE (object_b);
2504 if (access_fn_components_comparable_p (ref_a, ref_b))
2506 /* This pair of component accesses is comparable for dependence
2507 analysis, so we can include DR_ACCESS_FN (A, INDEX_A) and
2508 DR_ACCESS_FN (B, INDEX_B) in the sequence. */
2509 if (full_seq.start_a + full_seq.length != index_a
2510 || full_seq.start_b + full_seq.length != index_b)
2512 /* The accesses don't extend the current sequence,
2513 so start a new one here. */
2514 full_seq.start_a = index_a;
2515 full_seq.start_b = index_b;
2516 full_seq.length = 0;
2519 /* Add this pair of references to the sequence. */
2520 full_seq.length += 1;
2521 full_seq.object_a = object_a;
2522 full_seq.object_b = object_b;
2524 /* If the enclosing objects are structures (and thus have the
2525 same RECORD_TYPE), record the new sequence in STRUCT_SEQ. */
2526 if (TREE_CODE (type_a) == RECORD_TYPE)
2527 struct_seq = full_seq;
2529 /* Move to the next containing reference for both A and B. */
2530 ref_a = object_a;
2531 ref_b = object_b;
2532 index_a += 1;
2533 index_b += 1;
2534 continue;
2537 /* Try to approach equal type sizes. */
2538 if (!COMPLETE_TYPE_P (type_a)
2539 || !COMPLETE_TYPE_P (type_b)
2540 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type_a))
2541 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type_b)))
2542 break;
2544 unsigned HOST_WIDE_INT size_a = tree_to_uhwi (TYPE_SIZE_UNIT (type_a));
2545 unsigned HOST_WIDE_INT size_b = tree_to_uhwi (TYPE_SIZE_UNIT (type_b));
2546 if (size_a <= size_b)
2548 index_a += 1;
2549 ref_a = object_a;
2551 if (size_b <= size_a)
2553 index_b += 1;
2554 ref_b = object_b;
2558 /* See whether FULL_SEQ ends at the base and whether the two bases
2559 are equal. We do not care about TBAA or alignment info so we can
2560 use OEP_ADDRESS_OF to avoid false negatives. */
2561 tree base_a = DR_BASE_OBJECT (a);
2562 tree base_b = DR_BASE_OBJECT (b);
2563 bool same_base_p = (full_seq.start_a + full_seq.length == num_dimensions_a
2564 && full_seq.start_b + full_seq.length == num_dimensions_b
2565 && DR_UNCONSTRAINED_BASE (a) == DR_UNCONSTRAINED_BASE (b)
2566 && operand_equal_p (base_a, base_b, OEP_ADDRESS_OF)
2567 && types_compatible_p (TREE_TYPE (base_a),
2568 TREE_TYPE (base_b))
2569 && (!loop_nest.exists ()
2570 || (object_address_invariant_in_loop_p
2571 (loop_nest[0], base_a))));
2573 /* If the bases are the same, we can include the base variation too.
2574 E.g. the b accesses in:
2576 for (int i = 0; i < n; ++i)
2577 b[i + 4][0] = b[i][0];
2579 have a definite dependence distance of 4, while for:
2581 for (int i = 0; i < n; ++i)
2582 a[i + 4][0] = b[i][0];
2584 the dependence distance depends on the gap between a and b.
2586 If the bases are different then we can only rely on the sequence
2587 rooted at a structure access, since arrays are allowed to overlap
2588 arbitrarily and change shape arbitrarily. E.g. we treat this as
2589 valid code:
2591 int a[256];
2593 ((int (*)[4][3]) &a[1])[i][0] += ((int (*)[4][3]) &a[2])[i][0];
2595 where two lvalues with the same int[4][3] type overlap, and where
2596 both lvalues are distinct from the object's declared type. */
2597 if (same_base_p)
2599 if (DR_UNCONSTRAINED_BASE (a))
2600 full_seq.length += 1;
2602 else
2603 full_seq = struct_seq;
2605 /* Punt if we didn't find a suitable sequence. */
2606 if (full_seq.length == 0)
2608 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
2609 return res;
2612 if (!same_base_p)
2614 /* Partial overlap is possible for different bases when strict aliasing
2615 is not in effect. It's also possible if either base involves a union
2616 access; e.g. for:
2618 struct s1 { int a[2]; };
2619 struct s2 { struct s1 b; int c; };
2620 struct s3 { int d; struct s1 e; };
2621 union u { struct s2 f; struct s3 g; } *p, *q;
2623 the s1 at "p->f.b" (base "p->f") partially overlaps the s1 at
2624 "p->g.e" (base "p->g") and might partially overlap the s1 at
2625 "q->g.e" (base "q->g"). */
2626 if (!flag_strict_aliasing
2627 || ref_contains_union_access_p (full_seq.object_a)
2628 || ref_contains_union_access_p (full_seq.object_b))
2630 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
2631 return res;
2634 DDR_COULD_BE_INDEPENDENT_P (res) = true;
2635 if (!loop_nest.exists ()
2636 || (object_address_invariant_in_loop_p (loop_nest[0],
2637 full_seq.object_a)
2638 && object_address_invariant_in_loop_p (loop_nest[0],
2639 full_seq.object_b)))
2641 DDR_OBJECT_A (res) = full_seq.object_a;
2642 DDR_OBJECT_B (res) = full_seq.object_b;
2646 DDR_AFFINE_P (res) = true;
2647 DDR_ARE_DEPENDENT (res) = NULL_TREE;
2648 DDR_SUBSCRIPTS (res).create (full_seq.length);
2649 DDR_LOOP_NEST (res) = loop_nest;
2650 DDR_SELF_REFERENCE (res) = false;
2652 for (i = 0; i < full_seq.length; ++i)
2654 struct subscript *subscript;
2656 subscript = XNEW (struct subscript);
2657 SUB_ACCESS_FN (subscript, 0) = DR_ACCESS_FN (a, full_seq.start_a + i);
2658 SUB_ACCESS_FN (subscript, 1) = DR_ACCESS_FN (b, full_seq.start_b + i);
2659 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
2660 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
2661 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
2662 SUB_DISTANCE (subscript) = chrec_dont_know;
2663 DDR_SUBSCRIPTS (res).safe_push (subscript);
2666 return res;
2669 /* Frees memory used by the conflict function F. */
2671 static void
2672 free_conflict_function (conflict_function *f)
2674 unsigned i;
2676 if (CF_NONTRIVIAL_P (f))
2678 for (i = 0; i < f->n; i++)
2679 affine_fn_free (f->fns[i]);
2681 free (f);
2684 /* Frees memory used by SUBSCRIPTS. */
2686 static void
2687 free_subscripts (vec<subscript_p> subscripts)
2689 unsigned i;
2690 subscript_p s;
2692 FOR_EACH_VEC_ELT (subscripts, i, s)
2694 free_conflict_function (s->conflicting_iterations_in_a);
2695 free_conflict_function (s->conflicting_iterations_in_b);
2696 free (s);
2698 subscripts.release ();
2701 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
2702 description. */
2704 static inline void
2705 finalize_ddr_dependent (struct data_dependence_relation *ddr,
2706 tree chrec)
2708 DDR_ARE_DEPENDENT (ddr) = chrec;
2709 free_subscripts (DDR_SUBSCRIPTS (ddr));
2710 DDR_SUBSCRIPTS (ddr).create (0);
2713 /* The dependence relation DDR cannot be represented by a distance
2714 vector. */
2716 static inline void
2717 non_affine_dependence_relation (struct data_dependence_relation *ddr)
2719 if (dump_file && (dump_flags & TDF_DETAILS))
2720 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
2722 DDR_AFFINE_P (ddr) = false;
2727 /* This section contains the classic Banerjee tests. */
2729 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
2730 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
2732 static inline bool
2733 ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
2735 return (evolution_function_is_constant_p (chrec_a)
2736 && evolution_function_is_constant_p (chrec_b));
2739 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
2740 variable, i.e., if the SIV (Single Index Variable) test is true. */
2742 static bool
2743 siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
2745 if ((evolution_function_is_constant_p (chrec_a)
2746 && evolution_function_is_univariate_p (chrec_b))
2747 || (evolution_function_is_constant_p (chrec_b)
2748 && evolution_function_is_univariate_p (chrec_a)))
2749 return true;
2751 if (evolution_function_is_univariate_p (chrec_a)
2752 && evolution_function_is_univariate_p (chrec_b))
2754 switch (TREE_CODE (chrec_a))
2756 case POLYNOMIAL_CHREC:
2757 switch (TREE_CODE (chrec_b))
2759 case POLYNOMIAL_CHREC:
2760 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
2761 return false;
2762 /* FALLTHRU */
2764 default:
2765 return true;
2768 default:
2769 return true;
2773 return false;
2776 /* Creates a conflict function with N dimensions. The affine functions
2777 in each dimension follow. */
2779 static conflict_function *
2780 conflict_fn (unsigned n, ...)
2782 unsigned i;
2783 conflict_function *ret = XCNEW (conflict_function);
2784 va_list ap;
2786 gcc_assert (n > 0 && n <= MAX_DIM);
2787 va_start (ap, n);
2789 ret->n = n;
2790 for (i = 0; i < n; i++)
2791 ret->fns[i] = va_arg (ap, affine_fn);
2792 va_end (ap);
2794 return ret;
2797 /* Returns constant affine function with value CST. */
2799 static affine_fn
2800 affine_fn_cst (tree cst)
2802 affine_fn fn;
2803 fn.create (1);
2804 fn.quick_push (cst);
2805 return fn;
2808 /* Returns affine function with single variable, CST + COEF * x_DIM. */
2810 static affine_fn
2811 affine_fn_univar (tree cst, unsigned dim, tree coef)
2813 affine_fn fn;
2814 fn.create (dim + 1);
2815 unsigned i;
2817 gcc_assert (dim > 0);
2818 fn.quick_push (cst);
2819 for (i = 1; i < dim; i++)
2820 fn.quick_push (integer_zero_node);
2821 fn.quick_push (coef);
2822 return fn;
2825 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
2826 *OVERLAPS_B are initialized to the functions that describe the
2827 relation between the elements accessed twice by CHREC_A and
2828 CHREC_B. For k >= 0, the following property is verified:
2830 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2832 static void
2833 analyze_ziv_subscript (tree chrec_a,
2834 tree chrec_b,
2835 conflict_function **overlaps_a,
2836 conflict_function **overlaps_b,
2837 tree *last_conflicts)
2839 tree type, difference;
2840 dependence_stats.num_ziv++;
2842 if (dump_file && (dump_flags & TDF_DETAILS))
2843 fprintf (dump_file, "(analyze_ziv_subscript \n");
2845 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
2846 chrec_a = chrec_convert (type, chrec_a, NULL);
2847 chrec_b = chrec_convert (type, chrec_b, NULL);
2848 difference = chrec_fold_minus (type, chrec_a, chrec_b);
2850 switch (TREE_CODE (difference))
2852 case INTEGER_CST:
2853 if (integer_zerop (difference))
2855 /* The difference is equal to zero: the accessed index
2856 overlaps for each iteration in the loop. */
2857 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2858 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2859 *last_conflicts = chrec_dont_know;
2860 dependence_stats.num_ziv_dependent++;
2862 else
2864 /* The accesses do not overlap. */
2865 *overlaps_a = conflict_fn_no_dependence ();
2866 *overlaps_b = conflict_fn_no_dependence ();
2867 *last_conflicts = integer_zero_node;
2868 dependence_stats.num_ziv_independent++;
2870 break;
2872 default:
2873 /* We're not sure whether the indexes overlap. For the moment,
2874 conservatively answer "don't know". */
2875 if (dump_file && (dump_flags & TDF_DETAILS))
2876 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
2878 *overlaps_a = conflict_fn_not_known ();
2879 *overlaps_b = conflict_fn_not_known ();
2880 *last_conflicts = chrec_dont_know;
2881 dependence_stats.num_ziv_unimplemented++;
2882 break;
2885 if (dump_file && (dump_flags & TDF_DETAILS))
2886 fprintf (dump_file, ")\n");
2889 /* Similar to max_stmt_executions_int, but returns the bound as a tree,
2890 and only if it fits to the int type. If this is not the case, or the
2891 bound on the number of iterations of LOOP could not be derived, returns
2892 chrec_dont_know. */
2894 static tree
2895 max_stmt_executions_tree (struct loop *loop)
2897 widest_int nit;
2899 if (!max_stmt_executions (loop, &nit))
2900 return chrec_dont_know;
2902 if (!wi::fits_to_tree_p (nit, unsigned_type_node))
2903 return chrec_dont_know;
2905 return wide_int_to_tree (unsigned_type_node, nit);
2908 /* Determine whether the CHREC is always positive/negative. If the expression
2909 cannot be statically analyzed, return false, otherwise set the answer into
2910 VALUE. */
2912 static bool
2913 chrec_is_positive (tree chrec, bool *value)
2915 bool value0, value1, value2;
2916 tree end_value, nb_iter;
2918 switch (TREE_CODE (chrec))
2920 case POLYNOMIAL_CHREC:
2921 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
2922 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
2923 return false;
2925 /* FIXME -- overflows. */
2926 if (value0 == value1)
2928 *value = value0;
2929 return true;
2932 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
2933 and the proof consists in showing that the sign never
2934 changes during the execution of the loop, from 0 to
2935 loop->nb_iterations. */
2936 if (!evolution_function_is_affine_p (chrec))
2937 return false;
2939 nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
2940 if (chrec_contains_undetermined (nb_iter))
2941 return false;
2943 #if 0
2944 /* TODO -- If the test is after the exit, we may decrease the number of
2945 iterations by one. */
2946 if (after_exit)
2947 nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
2948 #endif
2950 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
2952 if (!chrec_is_positive (end_value, &value2))
2953 return false;
2955 *value = value0;
2956 return value0 == value1;
2958 case INTEGER_CST:
2959 switch (tree_int_cst_sgn (chrec))
2961 case -1:
2962 *value = false;
2963 break;
2964 case 1:
2965 *value = true;
2966 break;
2967 default:
2968 return false;
2970 return true;
2972 default:
2973 return false;
2978 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
2979 constant, and CHREC_B is an affine function. *OVERLAPS_A and
2980 *OVERLAPS_B are initialized to the functions that describe the
2981 relation between the elements accessed twice by CHREC_A and
2982 CHREC_B. For k >= 0, the following property is verified:
2984 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2986 static void
2987 analyze_siv_subscript_cst_affine (tree chrec_a,
2988 tree chrec_b,
2989 conflict_function **overlaps_a,
2990 conflict_function **overlaps_b,
2991 tree *last_conflicts)
2993 bool value0, value1, value2;
2994 tree type, difference, tmp;
2996 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
2997 chrec_a = chrec_convert (type, chrec_a, NULL);
2998 chrec_b = chrec_convert (type, chrec_b, NULL);
2999 difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
3001 /* Special case overlap in the first iteration. */
3002 if (integer_zerop (difference))
3004 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3005 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3006 *last_conflicts = integer_one_node;
3007 return;
3010 if (!chrec_is_positive (initial_condition (difference), &value0))
3012 if (dump_file && (dump_flags & TDF_DETAILS))
3013 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
3015 dependence_stats.num_siv_unimplemented++;
3016 *overlaps_a = conflict_fn_not_known ();
3017 *overlaps_b = conflict_fn_not_known ();
3018 *last_conflicts = chrec_dont_know;
3019 return;
3021 else
3023 if (value0 == false)
3025 if (TREE_CODE (chrec_b) != POLYNOMIAL_CHREC
3026 || !chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
3028 if (dump_file && (dump_flags & TDF_DETAILS))
3029 fprintf (dump_file, "siv test failed: chrec not positive.\n");
3031 *overlaps_a = conflict_fn_not_known ();
3032 *overlaps_b = conflict_fn_not_known ();
3033 *last_conflicts = chrec_dont_know;
3034 dependence_stats.num_siv_unimplemented++;
3035 return;
3037 else
3039 if (value1 == true)
3041 /* Example:
3042 chrec_a = 12
3043 chrec_b = {10, +, 1}
3046 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
3048 HOST_WIDE_INT numiter;
3049 struct loop *loop = get_chrec_loop (chrec_b);
3051 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3052 tmp = fold_build2 (EXACT_DIV_EXPR, type,
3053 fold_build1 (ABS_EXPR, type, difference),
3054 CHREC_RIGHT (chrec_b));
3055 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
3056 *last_conflicts = integer_one_node;
3059 /* Perform weak-zero siv test to see if overlap is
3060 outside the loop bounds. */
3061 numiter = max_stmt_executions_int (loop);
3063 if (numiter >= 0
3064 && compare_tree_int (tmp, numiter) > 0)
3066 free_conflict_function (*overlaps_a);
3067 free_conflict_function (*overlaps_b);
3068 *overlaps_a = conflict_fn_no_dependence ();
3069 *overlaps_b = conflict_fn_no_dependence ();
3070 *last_conflicts = integer_zero_node;
3071 dependence_stats.num_siv_independent++;
3072 return;
3074 dependence_stats.num_siv_dependent++;
3075 return;
3078 /* When the step does not divide the difference, there are
3079 no overlaps. */
3080 else
3082 *overlaps_a = conflict_fn_no_dependence ();
3083 *overlaps_b = conflict_fn_no_dependence ();
3084 *last_conflicts = integer_zero_node;
3085 dependence_stats.num_siv_independent++;
3086 return;
3090 else
3092 /* Example:
3093 chrec_a = 12
3094 chrec_b = {10, +, -1}
3096 In this case, chrec_a will not overlap with chrec_b. */
3097 *overlaps_a = conflict_fn_no_dependence ();
3098 *overlaps_b = conflict_fn_no_dependence ();
3099 *last_conflicts = integer_zero_node;
3100 dependence_stats.num_siv_independent++;
3101 return;
3105 else
3107 if (TREE_CODE (chrec_b) != POLYNOMIAL_CHREC
3108 || !chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
3110 if (dump_file && (dump_flags & TDF_DETAILS))
3111 fprintf (dump_file, "siv test failed: chrec not positive.\n");
3113 *overlaps_a = conflict_fn_not_known ();
3114 *overlaps_b = conflict_fn_not_known ();
3115 *last_conflicts = chrec_dont_know;
3116 dependence_stats.num_siv_unimplemented++;
3117 return;
3119 else
3121 if (value2 == false)
3123 /* Example:
3124 chrec_a = 3
3125 chrec_b = {10, +, -1}
3127 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
3129 HOST_WIDE_INT numiter;
3130 struct loop *loop = get_chrec_loop (chrec_b);
3132 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3133 tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
3134 CHREC_RIGHT (chrec_b));
3135 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
3136 *last_conflicts = integer_one_node;
3138 /* Perform weak-zero siv test to see if overlap is
3139 outside the loop bounds. */
3140 numiter = max_stmt_executions_int (loop);
3142 if (numiter >= 0
3143 && compare_tree_int (tmp, numiter) > 0)
3145 free_conflict_function (*overlaps_a);
3146 free_conflict_function (*overlaps_b);
3147 *overlaps_a = conflict_fn_no_dependence ();
3148 *overlaps_b = conflict_fn_no_dependence ();
3149 *last_conflicts = integer_zero_node;
3150 dependence_stats.num_siv_independent++;
3151 return;
3153 dependence_stats.num_siv_dependent++;
3154 return;
3157 /* When the step does not divide the difference, there
3158 are no overlaps. */
3159 else
3161 *overlaps_a = conflict_fn_no_dependence ();
3162 *overlaps_b = conflict_fn_no_dependence ();
3163 *last_conflicts = integer_zero_node;
3164 dependence_stats.num_siv_independent++;
3165 return;
3168 else
3170 /* Example:
3171 chrec_a = 3
3172 chrec_b = {4, +, 1}
3174 In this case, chrec_a will not overlap with chrec_b. */
3175 *overlaps_a = conflict_fn_no_dependence ();
3176 *overlaps_b = conflict_fn_no_dependence ();
3177 *last_conflicts = integer_zero_node;
3178 dependence_stats.num_siv_independent++;
3179 return;
3186 /* Helper recursive function for initializing the matrix A. Returns
3187 the initial value of CHREC. */
3189 static tree
3190 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
3192 gcc_assert (chrec);
3194 switch (TREE_CODE (chrec))
3196 case POLYNOMIAL_CHREC:
3197 if (!cst_and_fits_in_hwi (CHREC_RIGHT (chrec)))
3198 return chrec_dont_know;
3199 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
3200 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
3202 case PLUS_EXPR:
3203 case MULT_EXPR:
3204 case MINUS_EXPR:
3206 tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
3207 tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
3209 return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
3212 CASE_CONVERT:
3214 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
3215 return chrec_convert (chrec_type (chrec), op, NULL);
3218 case BIT_NOT_EXPR:
3220 /* Handle ~X as -1 - X. */
3221 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
3222 return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
3223 build_int_cst (TREE_TYPE (chrec), -1), op);
3226 case INTEGER_CST:
3227 return chrec;
3229 default:
3230 gcc_unreachable ();
3231 return NULL_TREE;
3235 #define FLOOR_DIV(x,y) ((x) / (y))
3237 /* Solves the special case of the Diophantine equation:
3238 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
3240 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
3241 number of iterations that loops X and Y run. The overlaps will be
3242 constructed as evolutions in dimension DIM. */
3244 static void
3245 compute_overlap_steps_for_affine_univar (HOST_WIDE_INT niter,
3246 HOST_WIDE_INT step_a,
3247 HOST_WIDE_INT step_b,
3248 affine_fn *overlaps_a,
3249 affine_fn *overlaps_b,
3250 tree *last_conflicts, int dim)
3252 if (((step_a > 0 && step_b > 0)
3253 || (step_a < 0 && step_b < 0)))
3255 HOST_WIDE_INT step_overlaps_a, step_overlaps_b;
3256 HOST_WIDE_INT gcd_steps_a_b, last_conflict, tau2;
3258 gcd_steps_a_b = gcd (step_a, step_b);
3259 step_overlaps_a = step_b / gcd_steps_a_b;
3260 step_overlaps_b = step_a / gcd_steps_a_b;
3262 if (niter > 0)
3264 tau2 = FLOOR_DIV (niter, step_overlaps_a);
3265 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
3266 last_conflict = tau2;
3267 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
3269 else
3270 *last_conflicts = chrec_dont_know;
3272 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
3273 build_int_cst (NULL_TREE,
3274 step_overlaps_a));
3275 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
3276 build_int_cst (NULL_TREE,
3277 step_overlaps_b));
3280 else
3282 *overlaps_a = affine_fn_cst (integer_zero_node);
3283 *overlaps_b = affine_fn_cst (integer_zero_node);
3284 *last_conflicts = integer_zero_node;
3288 /* Solves the special case of a Diophantine equation where CHREC_A is
3289 an affine bivariate function, and CHREC_B is an affine univariate
3290 function. For example,
3292 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
3294 has the following overlapping functions:
3296 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
3297 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
3298 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
3300 FORNOW: This is a specialized implementation for a case occurring in
3301 a common benchmark. Implement the general algorithm. */
3303 static void
3304 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
3305 conflict_function **overlaps_a,
3306 conflict_function **overlaps_b,
3307 tree *last_conflicts)
3309 bool xz_p, yz_p, xyz_p;
3310 HOST_WIDE_INT step_x, step_y, step_z;
3311 HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
3312 affine_fn overlaps_a_xz, overlaps_b_xz;
3313 affine_fn overlaps_a_yz, overlaps_b_yz;
3314 affine_fn overlaps_a_xyz, overlaps_b_xyz;
3315 affine_fn ova1, ova2, ovb;
3316 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
3318 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
3319 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
3320 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
3322 niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
3323 niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
3324 niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
3326 if (niter_x < 0 || niter_y < 0 || niter_z < 0)
3328 if (dump_file && (dump_flags & TDF_DETAILS))
3329 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
3331 *overlaps_a = conflict_fn_not_known ();
3332 *overlaps_b = conflict_fn_not_known ();
3333 *last_conflicts = chrec_dont_know;
3334 return;
3337 niter = MIN (niter_x, niter_z);
3338 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
3339 &overlaps_a_xz,
3340 &overlaps_b_xz,
3341 &last_conflicts_xz, 1);
3342 niter = MIN (niter_y, niter_z);
3343 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
3344 &overlaps_a_yz,
3345 &overlaps_b_yz,
3346 &last_conflicts_yz, 2);
3347 niter = MIN (niter_x, niter_z);
3348 niter = MIN (niter_y, niter);
3349 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
3350 &overlaps_a_xyz,
3351 &overlaps_b_xyz,
3352 &last_conflicts_xyz, 3);
3354 xz_p = !integer_zerop (last_conflicts_xz);
3355 yz_p = !integer_zerop (last_conflicts_yz);
3356 xyz_p = !integer_zerop (last_conflicts_xyz);
3358 if (xz_p || yz_p || xyz_p)
3360 ova1 = affine_fn_cst (integer_zero_node);
3361 ova2 = affine_fn_cst (integer_zero_node);
3362 ovb = affine_fn_cst (integer_zero_node);
3363 if (xz_p)
3365 affine_fn t0 = ova1;
3366 affine_fn t2 = ovb;
3368 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
3369 ovb = affine_fn_plus (ovb, overlaps_b_xz);
3370 affine_fn_free (t0);
3371 affine_fn_free (t2);
3372 *last_conflicts = last_conflicts_xz;
3374 if (yz_p)
3376 affine_fn t0 = ova2;
3377 affine_fn t2 = ovb;
3379 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
3380 ovb = affine_fn_plus (ovb, overlaps_b_yz);
3381 affine_fn_free (t0);
3382 affine_fn_free (t2);
3383 *last_conflicts = last_conflicts_yz;
3385 if (xyz_p)
3387 affine_fn t0 = ova1;
3388 affine_fn t2 = ova2;
3389 affine_fn t4 = ovb;
3391 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
3392 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
3393 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
3394 affine_fn_free (t0);
3395 affine_fn_free (t2);
3396 affine_fn_free (t4);
3397 *last_conflicts = last_conflicts_xyz;
3399 *overlaps_a = conflict_fn (2, ova1, ova2);
3400 *overlaps_b = conflict_fn (1, ovb);
3402 else
3404 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3405 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3406 *last_conflicts = integer_zero_node;
3409 affine_fn_free (overlaps_a_xz);
3410 affine_fn_free (overlaps_b_xz);
3411 affine_fn_free (overlaps_a_yz);
3412 affine_fn_free (overlaps_b_yz);
3413 affine_fn_free (overlaps_a_xyz);
3414 affine_fn_free (overlaps_b_xyz);
3417 /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
3419 static void
3420 lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
3421 int size)
3423 memcpy (vec2, vec1, size * sizeof (*vec1));
3426 /* Copy the elements of M x N matrix MAT1 to MAT2. */
3428 static void
3429 lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
3430 int m, int n)
3432 int i;
3434 for (i = 0; i < m; i++)
3435 lambda_vector_copy (mat1[i], mat2[i], n);
3438 /* Store the N x N identity matrix in MAT. */
3440 static void
3441 lambda_matrix_id (lambda_matrix mat, int size)
3443 int i, j;
3445 for (i = 0; i < size; i++)
3446 for (j = 0; j < size; j++)
3447 mat[i][j] = (i == j) ? 1 : 0;
3450 /* Return the index of the first nonzero element of vector VEC1 between
3451 START and N. We must have START <= N.
3452 Returns N if VEC1 is the zero vector. */
3454 static int
3455 lambda_vector_first_nz (lambda_vector vec1, int n, int start)
3457 int j = start;
3458 while (j < n && vec1[j] == 0)
3459 j++;
3460 return j;
3463 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
3464 R2 = R2 + CONST1 * R1. */
3466 static void
3467 lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2,
3468 lambda_int const1)
3470 int i;
3472 if (const1 == 0)
3473 return;
3475 for (i = 0; i < n; i++)
3476 mat[r2][i] += const1 * mat[r1][i];
3479 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
3480 and store the result in VEC2. */
3482 static void
3483 lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
3484 int size, lambda_int const1)
3486 int i;
3488 if (const1 == 0)
3489 lambda_vector_clear (vec2, size);
3490 else
3491 for (i = 0; i < size; i++)
3492 vec2[i] = const1 * vec1[i];
3495 /* Negate vector VEC1 with length SIZE and store it in VEC2. */
3497 static void
3498 lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
3499 int size)
3501 lambda_vector_mult_const (vec1, vec2, size, -1);
3504 /* Negate row R1 of matrix MAT which has N columns. */
3506 static void
3507 lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
3509 lambda_vector_negate (mat[r1], mat[r1], n);
3512 /* Return true if two vectors are equal. */
3514 static bool
3515 lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
3517 int i;
3518 for (i = 0; i < size; i++)
3519 if (vec1[i] != vec2[i])
3520 return false;
3521 return true;
3524 /* Given an M x N integer matrix A, this function determines an M x
3525 M unimodular matrix U, and an M x N echelon matrix S such that
3526 "U.A = S". This decomposition is also known as "right Hermite".
3528 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
3529 Restructuring Compilers" Utpal Banerjee. */
3531 static void
3532 lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
3533 lambda_matrix S, lambda_matrix U)
3535 int i, j, i0 = 0;
3537 lambda_matrix_copy (A, S, m, n);
3538 lambda_matrix_id (U, m);
3540 for (j = 0; j < n; j++)
3542 if (lambda_vector_first_nz (S[j], m, i0) < m)
3544 ++i0;
3545 for (i = m - 1; i >= i0; i--)
3547 while (S[i][j] != 0)
3549 lambda_int sigma, factor, a, b;
3551 a = S[i-1][j];
3552 b = S[i][j];
3553 sigma = (a * b < 0) ? -1: 1;
3554 a = abs_hwi (a);
3555 b = abs_hwi (b);
3556 factor = sigma * (a / b);
3558 lambda_matrix_row_add (S, n, i, i-1, -factor);
3559 std::swap (S[i], S[i-1]);
3561 lambda_matrix_row_add (U, m, i, i-1, -factor);
3562 std::swap (U[i], U[i-1]);
3569 /* Determines the overlapping elements due to accesses CHREC_A and
3570 CHREC_B, that are affine functions. This function cannot handle
3571 symbolic evolution functions, ie. when initial conditions are
3572 parameters, because it uses lambda matrices of integers. */
3574 static void
3575 analyze_subscript_affine_affine (tree chrec_a,
3576 tree chrec_b,
3577 conflict_function **overlaps_a,
3578 conflict_function **overlaps_b,
3579 tree *last_conflicts)
3581 unsigned nb_vars_a, nb_vars_b, dim;
3582 HOST_WIDE_INT gamma, gcd_alpha_beta;
3583 lambda_matrix A, U, S;
3584 struct obstack scratch_obstack;
3586 if (eq_evolutions_p (chrec_a, chrec_b))
3588 /* The accessed index overlaps for each iteration in the
3589 loop. */
3590 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3591 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3592 *last_conflicts = chrec_dont_know;
3593 return;
3595 if (dump_file && (dump_flags & TDF_DETAILS))
3596 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
3598 /* For determining the initial intersection, we have to solve a
3599 Diophantine equation. This is the most time consuming part.
3601 For answering to the question: "Is there a dependence?" we have
3602 to prove that there exists a solution to the Diophantine
3603 equation, and that the solution is in the iteration domain,
3604 i.e. the solution is positive or zero, and that the solution
3605 happens before the upper bound loop.nb_iterations. Otherwise
3606 there is no dependence. This function outputs a description of
3607 the iterations that hold the intersections. */
3609 nb_vars_a = nb_vars_in_chrec (chrec_a);
3610 nb_vars_b = nb_vars_in_chrec (chrec_b);
3612 gcc_obstack_init (&scratch_obstack);
3614 dim = nb_vars_a + nb_vars_b;
3615 U = lambda_matrix_new (dim, dim, &scratch_obstack);
3616 A = lambda_matrix_new (dim, 1, &scratch_obstack);
3617 S = lambda_matrix_new (dim, 1, &scratch_obstack);
3619 tree init_a = initialize_matrix_A (A, chrec_a, 0, 1);
3620 tree init_b = initialize_matrix_A (A, chrec_b, nb_vars_a, -1);
3621 if (init_a == chrec_dont_know
3622 || init_b == chrec_dont_know)
3624 if (dump_file && (dump_flags & TDF_DETAILS))
3625 fprintf (dump_file, "affine-affine test failed: "
3626 "representation issue.\n");
3627 *overlaps_a = conflict_fn_not_known ();
3628 *overlaps_b = conflict_fn_not_known ();
3629 *last_conflicts = chrec_dont_know;
3630 goto end_analyze_subs_aa;
3632 gamma = int_cst_value (init_b) - int_cst_value (init_a);
3634 /* Don't do all the hard work of solving the Diophantine equation
3635 when we already know the solution: for example,
3636 | {3, +, 1}_1
3637 | {3, +, 4}_2
3638 | gamma = 3 - 3 = 0.
3639 Then the first overlap occurs during the first iterations:
3640 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
3642 if (gamma == 0)
3644 if (nb_vars_a == 1 && nb_vars_b == 1)
3646 HOST_WIDE_INT step_a, step_b;
3647 HOST_WIDE_INT niter, niter_a, niter_b;
3648 affine_fn ova, ovb;
3650 niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
3651 niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
3652 niter = MIN (niter_a, niter_b);
3653 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
3654 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
3656 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
3657 &ova, &ovb,
3658 last_conflicts, 1);
3659 *overlaps_a = conflict_fn (1, ova);
3660 *overlaps_b = conflict_fn (1, ovb);
3663 else if (nb_vars_a == 2 && nb_vars_b == 1)
3664 compute_overlap_steps_for_affine_1_2
3665 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
3667 else if (nb_vars_a == 1 && nb_vars_b == 2)
3668 compute_overlap_steps_for_affine_1_2
3669 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
3671 else
3673 if (dump_file && (dump_flags & TDF_DETAILS))
3674 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
3675 *overlaps_a = conflict_fn_not_known ();
3676 *overlaps_b = conflict_fn_not_known ();
3677 *last_conflicts = chrec_dont_know;
3679 goto end_analyze_subs_aa;
3682 /* U.A = S */
3683 lambda_matrix_right_hermite (A, dim, 1, S, U);
3685 if (S[0][0] < 0)
3687 S[0][0] *= -1;
3688 lambda_matrix_row_negate (U, dim, 0);
3690 gcd_alpha_beta = S[0][0];
3692 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
3693 but that is a quite strange case. Instead of ICEing, answer
3694 don't know. */
3695 if (gcd_alpha_beta == 0)
3697 *overlaps_a = conflict_fn_not_known ();
3698 *overlaps_b = conflict_fn_not_known ();
3699 *last_conflicts = chrec_dont_know;
3700 goto end_analyze_subs_aa;
3703 /* The classic "gcd-test". */
3704 if (!int_divides_p (gcd_alpha_beta, gamma))
3706 /* The "gcd-test" has determined that there is no integer
3707 solution, i.e. there is no dependence. */
3708 *overlaps_a = conflict_fn_no_dependence ();
3709 *overlaps_b = conflict_fn_no_dependence ();
3710 *last_conflicts = integer_zero_node;
3713 /* Both access functions are univariate. This includes SIV and MIV cases. */
3714 else if (nb_vars_a == 1 && nb_vars_b == 1)
3716 /* Both functions should have the same evolution sign. */
3717 if (((A[0][0] > 0 && -A[1][0] > 0)
3718 || (A[0][0] < 0 && -A[1][0] < 0)))
3720 /* The solutions are given by:
3722 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
3723 | [u21 u22] [y0]
3725 For a given integer t. Using the following variables,
3727 | i0 = u11 * gamma / gcd_alpha_beta
3728 | j0 = u12 * gamma / gcd_alpha_beta
3729 | i1 = u21
3730 | j1 = u22
3732 the solutions are:
3734 | x0 = i0 + i1 * t,
3735 | y0 = j0 + j1 * t. */
3736 HOST_WIDE_INT i0, j0, i1, j1;
3738 i0 = U[0][0] * gamma / gcd_alpha_beta;
3739 j0 = U[0][1] * gamma / gcd_alpha_beta;
3740 i1 = U[1][0];
3741 j1 = U[1][1];
3743 if ((i1 == 0 && i0 < 0)
3744 || (j1 == 0 && j0 < 0))
3746 /* There is no solution.
3747 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
3748 falls in here, but for the moment we don't look at the
3749 upper bound of the iteration domain. */
3750 *overlaps_a = conflict_fn_no_dependence ();
3751 *overlaps_b = conflict_fn_no_dependence ();
3752 *last_conflicts = integer_zero_node;
3753 goto end_analyze_subs_aa;
3756 if (i1 > 0 && j1 > 0)
3758 HOST_WIDE_INT niter_a
3759 = max_stmt_executions_int (get_chrec_loop (chrec_a));
3760 HOST_WIDE_INT niter_b
3761 = max_stmt_executions_int (get_chrec_loop (chrec_b));
3762 HOST_WIDE_INT niter = MIN (niter_a, niter_b);
3764 /* (X0, Y0) is a solution of the Diophantine equation:
3765 "chrec_a (X0) = chrec_b (Y0)". */
3766 HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
3767 CEIL (-j0, j1));
3768 HOST_WIDE_INT x0 = i1 * tau1 + i0;
3769 HOST_WIDE_INT y0 = j1 * tau1 + j0;
3771 /* (X1, Y1) is the smallest positive solution of the eq
3772 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
3773 first conflict occurs. */
3774 HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
3775 HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
3776 HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
3778 if (niter > 0)
3780 /* If the overlap occurs outside of the bounds of the
3781 loop, there is no dependence. */
3782 if (x1 >= niter_a || y1 >= niter_b)
3784 *overlaps_a = conflict_fn_no_dependence ();
3785 *overlaps_b = conflict_fn_no_dependence ();
3786 *last_conflicts = integer_zero_node;
3787 goto end_analyze_subs_aa;
3790 /* max stmt executions can get quite large, avoid
3791 overflows by using wide ints here. */
3792 widest_int tau2
3793 = wi::smin (wi::sdiv_floor (wi::sub (niter_a, i0), i1),
3794 wi::sdiv_floor (wi::sub (niter_b, j0), j1));
3795 widest_int last_conflict = wi::sub (tau2, (x1 - i0)/i1);
3796 if (wi::min_precision (last_conflict, SIGNED)
3797 <= TYPE_PRECISION (integer_type_node))
3798 *last_conflicts
3799 = build_int_cst (integer_type_node,
3800 last_conflict.to_shwi ());
3801 else
3802 *last_conflicts = chrec_dont_know;
3804 else
3805 *last_conflicts = chrec_dont_know;
3807 *overlaps_a
3808 = conflict_fn (1,
3809 affine_fn_univar (build_int_cst (NULL_TREE, x1),
3811 build_int_cst (NULL_TREE, i1)));
3812 *overlaps_b
3813 = conflict_fn (1,
3814 affine_fn_univar (build_int_cst (NULL_TREE, y1),
3816 build_int_cst (NULL_TREE, j1)));
3818 else
3820 /* FIXME: For the moment, the upper bound of the
3821 iteration domain for i and j is not checked. */
3822 if (dump_file && (dump_flags & TDF_DETAILS))
3823 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
3824 *overlaps_a = conflict_fn_not_known ();
3825 *overlaps_b = conflict_fn_not_known ();
3826 *last_conflicts = chrec_dont_know;
3829 else
3831 if (dump_file && (dump_flags & TDF_DETAILS))
3832 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
3833 *overlaps_a = conflict_fn_not_known ();
3834 *overlaps_b = conflict_fn_not_known ();
3835 *last_conflicts = chrec_dont_know;
3838 else
3840 if (dump_file && (dump_flags & TDF_DETAILS))
3841 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
3842 *overlaps_a = conflict_fn_not_known ();
3843 *overlaps_b = conflict_fn_not_known ();
3844 *last_conflicts = chrec_dont_know;
3847 end_analyze_subs_aa:
3848 obstack_free (&scratch_obstack, NULL);
3849 if (dump_file && (dump_flags & TDF_DETAILS))
3851 fprintf (dump_file, " (overlaps_a = ");
3852 dump_conflict_function (dump_file, *overlaps_a);
3853 fprintf (dump_file, ")\n (overlaps_b = ");
3854 dump_conflict_function (dump_file, *overlaps_b);
3855 fprintf (dump_file, "))\n");
3859 /* Returns true when analyze_subscript_affine_affine can be used for
3860 determining the dependence relation between chrec_a and chrec_b,
3861 that contain symbols. This function modifies chrec_a and chrec_b
3862 such that the analysis result is the same, and such that they don't
3863 contain symbols, and then can safely be passed to the analyzer.
3865 Example: The analysis of the following tuples of evolutions produce
3866 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
3867 vs. {0, +, 1}_1
3869 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
3870 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
3873 static bool
3874 can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
3876 tree diff, type, left_a, left_b, right_b;
3878 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
3879 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
3880 /* FIXME: For the moment not handled. Might be refined later. */
3881 return false;
3883 type = chrec_type (*chrec_a);
3884 left_a = CHREC_LEFT (*chrec_a);
3885 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
3886 diff = chrec_fold_minus (type, left_a, left_b);
3888 if (!evolution_function_is_constant_p (diff))
3889 return false;
3891 if (dump_file && (dump_flags & TDF_DETAILS))
3892 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
3894 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
3895 diff, CHREC_RIGHT (*chrec_a));
3896 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
3897 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
3898 build_int_cst (type, 0),
3899 right_b);
3900 return true;
3903 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
3904 *OVERLAPS_B are initialized to the functions that describe the
3905 relation between the elements accessed twice by CHREC_A and
3906 CHREC_B. For k >= 0, the following property is verified:
3908 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
3910 static void
3911 analyze_siv_subscript (tree chrec_a,
3912 tree chrec_b,
3913 conflict_function **overlaps_a,
3914 conflict_function **overlaps_b,
3915 tree *last_conflicts,
3916 int loop_nest_num)
3918 dependence_stats.num_siv++;
3920 if (dump_file && (dump_flags & TDF_DETAILS))
3921 fprintf (dump_file, "(analyze_siv_subscript \n");
3923 if (evolution_function_is_constant_p (chrec_a)
3924 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
3925 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
3926 overlaps_a, overlaps_b, last_conflicts);
3928 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
3929 && evolution_function_is_constant_p (chrec_b))
3930 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
3931 overlaps_b, overlaps_a, last_conflicts);
3933 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
3934 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
3936 if (!chrec_contains_symbols (chrec_a)
3937 && !chrec_contains_symbols (chrec_b))
3939 analyze_subscript_affine_affine (chrec_a, chrec_b,
3940 overlaps_a, overlaps_b,
3941 last_conflicts);
3943 if (CF_NOT_KNOWN_P (*overlaps_a)
3944 || CF_NOT_KNOWN_P (*overlaps_b))
3945 dependence_stats.num_siv_unimplemented++;
3946 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
3947 || CF_NO_DEPENDENCE_P (*overlaps_b))
3948 dependence_stats.num_siv_independent++;
3949 else
3950 dependence_stats.num_siv_dependent++;
3952 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
3953 &chrec_b))
3955 analyze_subscript_affine_affine (chrec_a, chrec_b,
3956 overlaps_a, overlaps_b,
3957 last_conflicts);
3959 if (CF_NOT_KNOWN_P (*overlaps_a)
3960 || CF_NOT_KNOWN_P (*overlaps_b))
3961 dependence_stats.num_siv_unimplemented++;
3962 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
3963 || CF_NO_DEPENDENCE_P (*overlaps_b))
3964 dependence_stats.num_siv_independent++;
3965 else
3966 dependence_stats.num_siv_dependent++;
3968 else
3969 goto siv_subscript_dontknow;
3972 else
3974 siv_subscript_dontknow:;
3975 if (dump_file && (dump_flags & TDF_DETAILS))
3976 fprintf (dump_file, " siv test failed: unimplemented");
3977 *overlaps_a = conflict_fn_not_known ();
3978 *overlaps_b = conflict_fn_not_known ();
3979 *last_conflicts = chrec_dont_know;
3980 dependence_stats.num_siv_unimplemented++;
3983 if (dump_file && (dump_flags & TDF_DETAILS))
3984 fprintf (dump_file, ")\n");
3987 /* Returns false if we can prove that the greatest common divisor of the steps
3988 of CHREC does not divide CST, false otherwise. */
3990 static bool
3991 gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
3993 HOST_WIDE_INT cd = 0, val;
3994 tree step;
3996 if (!tree_fits_shwi_p (cst))
3997 return true;
3998 val = tree_to_shwi (cst);
4000 while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
4002 step = CHREC_RIGHT (chrec);
4003 if (!tree_fits_shwi_p (step))
4004 return true;
4005 cd = gcd (cd, tree_to_shwi (step));
4006 chrec = CHREC_LEFT (chrec);
4009 return val % cd == 0;
4012 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
4013 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
4014 functions that describe the relation between the elements accessed
4015 twice by CHREC_A and CHREC_B. For k >= 0, the following property
4016 is verified:
4018 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
4020 static void
4021 analyze_miv_subscript (tree chrec_a,
4022 tree chrec_b,
4023 conflict_function **overlaps_a,
4024 conflict_function **overlaps_b,
4025 tree *last_conflicts,
4026 struct loop *loop_nest)
4028 tree type, difference;
4030 dependence_stats.num_miv++;
4031 if (dump_file && (dump_flags & TDF_DETAILS))
4032 fprintf (dump_file, "(analyze_miv_subscript \n");
4034 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
4035 chrec_a = chrec_convert (type, chrec_a, NULL);
4036 chrec_b = chrec_convert (type, chrec_b, NULL);
4037 difference = chrec_fold_minus (type, chrec_a, chrec_b);
4039 if (eq_evolutions_p (chrec_a, chrec_b))
4041 /* Access functions are the same: all the elements are accessed
4042 in the same order. */
4043 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
4044 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
4045 *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
4046 dependence_stats.num_miv_dependent++;
4049 else if (evolution_function_is_constant_p (difference)
4050 && evolution_function_is_affine_multivariate_p (chrec_a,
4051 loop_nest->num)
4052 && !gcd_of_steps_may_divide_p (chrec_a, difference))
4054 /* testsuite/.../ssa-chrec-33.c
4055 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
4057 The difference is 1, and all the evolution steps are multiples
4058 of 2, consequently there are no overlapping elements. */
4059 *overlaps_a = conflict_fn_no_dependence ();
4060 *overlaps_b = conflict_fn_no_dependence ();
4061 *last_conflicts = integer_zero_node;
4062 dependence_stats.num_miv_independent++;
4065 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest->num)
4066 && !chrec_contains_symbols (chrec_a, loop_nest)
4067 && evolution_function_is_affine_in_loop (chrec_b, loop_nest->num)
4068 && !chrec_contains_symbols (chrec_b, loop_nest))
4070 /* testsuite/.../ssa-chrec-35.c
4071 {0, +, 1}_2 vs. {0, +, 1}_3
4072 the overlapping elements are respectively located at iterations:
4073 {0, +, 1}_x and {0, +, 1}_x,
4074 in other words, we have the equality:
4075 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
4077 Other examples:
4078 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
4079 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
4081 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
4082 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
4084 analyze_subscript_affine_affine (chrec_a, chrec_b,
4085 overlaps_a, overlaps_b, last_conflicts);
4087 if (CF_NOT_KNOWN_P (*overlaps_a)
4088 || CF_NOT_KNOWN_P (*overlaps_b))
4089 dependence_stats.num_miv_unimplemented++;
4090 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
4091 || CF_NO_DEPENDENCE_P (*overlaps_b))
4092 dependence_stats.num_miv_independent++;
4093 else
4094 dependence_stats.num_miv_dependent++;
4097 else
4099 /* When the analysis is too difficult, answer "don't know". */
4100 if (dump_file && (dump_flags & TDF_DETAILS))
4101 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
4103 *overlaps_a = conflict_fn_not_known ();
4104 *overlaps_b = conflict_fn_not_known ();
4105 *last_conflicts = chrec_dont_know;
4106 dependence_stats.num_miv_unimplemented++;
4109 if (dump_file && (dump_flags & TDF_DETAILS))
4110 fprintf (dump_file, ")\n");
4113 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
4114 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
4115 OVERLAP_ITERATIONS_B are initialized with two functions that
4116 describe the iterations that contain conflicting elements.
4118 Remark: For an integer k >= 0, the following equality is true:
4120 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
4123 static void
4124 analyze_overlapping_iterations (tree chrec_a,
4125 tree chrec_b,
4126 conflict_function **overlap_iterations_a,
4127 conflict_function **overlap_iterations_b,
4128 tree *last_conflicts, struct loop *loop_nest)
4130 unsigned int lnn = loop_nest->num;
4132 dependence_stats.num_subscript_tests++;
4134 if (dump_file && (dump_flags & TDF_DETAILS))
4136 fprintf (dump_file, "(analyze_overlapping_iterations \n");
4137 fprintf (dump_file, " (chrec_a = ");
4138 print_generic_expr (dump_file, chrec_a);
4139 fprintf (dump_file, ")\n (chrec_b = ");
4140 print_generic_expr (dump_file, chrec_b);
4141 fprintf (dump_file, ")\n");
4144 if (chrec_a == NULL_TREE
4145 || chrec_b == NULL_TREE
4146 || chrec_contains_undetermined (chrec_a)
4147 || chrec_contains_undetermined (chrec_b))
4149 dependence_stats.num_subscript_undetermined++;
4151 *overlap_iterations_a = conflict_fn_not_known ();
4152 *overlap_iterations_b = conflict_fn_not_known ();
4155 /* If they are the same chrec, and are affine, they overlap
4156 on every iteration. */
4157 else if (eq_evolutions_p (chrec_a, chrec_b)
4158 && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
4159 || operand_equal_p (chrec_a, chrec_b, 0)))
4161 dependence_stats.num_same_subscript_function++;
4162 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
4163 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
4164 *last_conflicts = chrec_dont_know;
4167 /* If they aren't the same, and aren't affine, we can't do anything
4168 yet. */
4169 else if ((chrec_contains_symbols (chrec_a)
4170 || chrec_contains_symbols (chrec_b))
4171 && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
4172 || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
4174 dependence_stats.num_subscript_undetermined++;
4175 *overlap_iterations_a = conflict_fn_not_known ();
4176 *overlap_iterations_b = conflict_fn_not_known ();
4179 else if (ziv_subscript_p (chrec_a, chrec_b))
4180 analyze_ziv_subscript (chrec_a, chrec_b,
4181 overlap_iterations_a, overlap_iterations_b,
4182 last_conflicts);
4184 else if (siv_subscript_p (chrec_a, chrec_b))
4185 analyze_siv_subscript (chrec_a, chrec_b,
4186 overlap_iterations_a, overlap_iterations_b,
4187 last_conflicts, lnn);
4189 else
4190 analyze_miv_subscript (chrec_a, chrec_b,
4191 overlap_iterations_a, overlap_iterations_b,
4192 last_conflicts, loop_nest);
4194 if (dump_file && (dump_flags & TDF_DETAILS))
4196 fprintf (dump_file, " (overlap_iterations_a = ");
4197 dump_conflict_function (dump_file, *overlap_iterations_a);
4198 fprintf (dump_file, ")\n (overlap_iterations_b = ");
4199 dump_conflict_function (dump_file, *overlap_iterations_b);
4200 fprintf (dump_file, "))\n");
4204 /* Helper function for uniquely inserting distance vectors. */
4206 static void
4207 save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
4209 unsigned i;
4210 lambda_vector v;
4212 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, v)
4213 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
4214 return;
4216 DDR_DIST_VECTS (ddr).safe_push (dist_v);
4219 /* Helper function for uniquely inserting direction vectors. */
4221 static void
4222 save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
4224 unsigned i;
4225 lambda_vector v;
4227 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), i, v)
4228 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
4229 return;
4231 DDR_DIR_VECTS (ddr).safe_push (dir_v);
4234 /* Add a distance of 1 on all the loops outer than INDEX. If we
4235 haven't yet determined a distance for this outer loop, push a new
4236 distance vector composed of the previous distance, and a distance
4237 of 1 for this outer loop. Example:
4239 | loop_1
4240 | loop_2
4241 | A[10]
4242 | endloop_2
4243 | endloop_1
4245 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
4246 save (0, 1), then we have to save (1, 0). */
4248 static void
4249 add_outer_distances (struct data_dependence_relation *ddr,
4250 lambda_vector dist_v, int index)
4252 /* For each outer loop where init_v is not set, the accesses are
4253 in dependence of distance 1 in the loop. */
4254 while (--index >= 0)
4256 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4257 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
4258 save_v[index] = 1;
4259 save_dist_v (ddr, save_v);
4263 /* Return false when fail to represent the data dependence as a
4264 distance vector. A_INDEX is the index of the first reference
4265 (0 for DDR_A, 1 for DDR_B) and B_INDEX is the index of the
4266 second reference. INIT_B is set to true when a component has been
4267 added to the distance vector DIST_V. INDEX_CARRY is then set to
4268 the index in DIST_V that carries the dependence. */
4270 static bool
4271 build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
4272 unsigned int a_index, unsigned int b_index,
4273 lambda_vector dist_v, bool *init_b,
4274 int *index_carry)
4276 unsigned i;
4277 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4278 struct loop *loop = DDR_LOOP_NEST (ddr)[0];
4280 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
4282 tree access_fn_a, access_fn_b;
4283 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
4285 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
4287 non_affine_dependence_relation (ddr);
4288 return false;
4291 access_fn_a = SUB_ACCESS_FN (subscript, a_index);
4292 access_fn_b = SUB_ACCESS_FN (subscript, b_index);
4294 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
4295 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
4297 HOST_WIDE_INT dist;
4298 int index;
4299 int var_a = CHREC_VARIABLE (access_fn_a);
4300 int var_b = CHREC_VARIABLE (access_fn_b);
4302 if (var_a != var_b
4303 || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
4305 non_affine_dependence_relation (ddr);
4306 return false;
4309 /* When data references are collected in a loop while data
4310 dependences are analyzed in loop nest nested in the loop, we
4311 would have more number of access functions than number of
4312 loops. Skip access functions of loops not in the loop nest.
4314 See PR89725 for more information. */
4315 if (flow_loop_nested_p (get_loop (cfun, var_a), loop))
4316 continue;
4318 dist = int_cst_value (SUB_DISTANCE (subscript));
4319 index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
4320 *index_carry = MIN (index, *index_carry);
4322 /* This is the subscript coupling test. If we have already
4323 recorded a distance for this loop (a distance coming from
4324 another subscript), it should be the same. For example,
4325 in the following code, there is no dependence:
4327 | loop i = 0, N, 1
4328 | T[i+1][i] = ...
4329 | ... = T[i][i]
4330 | endloop
4332 if (init_v[index] != 0 && dist_v[index] != dist)
4334 finalize_ddr_dependent (ddr, chrec_known);
4335 return false;
4338 dist_v[index] = dist;
4339 init_v[index] = 1;
4340 *init_b = true;
4342 else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
4344 /* This can be for example an affine vs. constant dependence
4345 (T[i] vs. T[3]) that is not an affine dependence and is
4346 not representable as a distance vector. */
4347 non_affine_dependence_relation (ddr);
4348 return false;
4352 return true;
4355 /* Return true when the DDR contains only constant access functions. */
4357 static bool
4358 constant_access_functions (const struct data_dependence_relation *ddr)
4360 unsigned i;
4361 subscript *sub;
4363 FOR_EACH_VEC_ELT (DDR_SUBSCRIPTS (ddr), i, sub)
4364 if (!evolution_function_is_constant_p (SUB_ACCESS_FN (sub, 0))
4365 || !evolution_function_is_constant_p (SUB_ACCESS_FN (sub, 1)))
4366 return false;
4368 return true;
4371 /* Helper function for the case where DDR_A and DDR_B are the same
4372 multivariate access function with a constant step. For an example
4373 see pr34635-1.c. */
4375 static void
4376 add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
4378 int x_1, x_2;
4379 tree c_1 = CHREC_LEFT (c_2);
4380 tree c_0 = CHREC_LEFT (c_1);
4381 lambda_vector dist_v;
4382 HOST_WIDE_INT v1, v2, cd;
4384 /* Polynomials with more than 2 variables are not handled yet. When
4385 the evolution steps are parameters, it is not possible to
4386 represent the dependence using classical distance vectors. */
4387 if (TREE_CODE (c_0) != INTEGER_CST
4388 || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
4389 || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
4391 DDR_AFFINE_P (ddr) = false;
4392 return;
4395 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
4396 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
4398 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
4399 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4400 v1 = int_cst_value (CHREC_RIGHT (c_1));
4401 v2 = int_cst_value (CHREC_RIGHT (c_2));
4402 cd = gcd (v1, v2);
4403 v1 /= cd;
4404 v2 /= cd;
4406 if (v2 < 0)
4408 v2 = -v2;
4409 v1 = -v1;
4412 dist_v[x_1] = v2;
4413 dist_v[x_2] = -v1;
4414 save_dist_v (ddr, dist_v);
4416 add_outer_distances (ddr, dist_v, x_1);
4419 /* Helper function for the case where DDR_A and DDR_B are the same
4420 access functions. */
4422 static void
4423 add_other_self_distances (struct data_dependence_relation *ddr)
4425 lambda_vector dist_v;
4426 unsigned i;
4427 int index_carry = DDR_NB_LOOPS (ddr);
4428 subscript *sub;
4429 struct loop *loop = DDR_LOOP_NEST (ddr)[0];
4431 FOR_EACH_VEC_ELT (DDR_SUBSCRIPTS (ddr), i, sub)
4433 tree access_fun = SUB_ACCESS_FN (sub, 0);
4435 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
4437 if (!evolution_function_is_univariate_p (access_fun, loop->num))
4439 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
4441 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
4442 return;
4445 access_fun = SUB_ACCESS_FN (DDR_SUBSCRIPT (ddr, 0), 0);
4447 if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
4448 add_multivariate_self_dist (ddr, access_fun);
4449 else
4450 /* The evolution step is not constant: it varies in
4451 the outer loop, so this cannot be represented by a
4452 distance vector. For example in pr34635.c the
4453 evolution is {0, +, {0, +, 4}_1}_2. */
4454 DDR_AFFINE_P (ddr) = false;
4456 return;
4459 /* When data references are collected in a loop while data
4460 dependences are analyzed in loop nest nested in the loop, we
4461 would have more number of access functions than number of
4462 loops. Skip access functions of loops not in the loop nest.
4464 See PR89725 for more information. */
4465 if (flow_loop_nested_p (get_loop (cfun, CHREC_VARIABLE (access_fun)),
4466 loop))
4467 continue;
4469 index_carry = MIN (index_carry,
4470 index_in_loop_nest (CHREC_VARIABLE (access_fun),
4471 DDR_LOOP_NEST (ddr)));
4475 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4476 add_outer_distances (ddr, dist_v, index_carry);
4479 static void
4480 insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
4482 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4484 dist_v[0] = 1;
4485 save_dist_v (ddr, dist_v);
4488 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
4489 is the case for example when access functions are the same and
4490 equal to a constant, as in:
4492 | loop_1
4493 | A[3] = ...
4494 | ... = A[3]
4495 | endloop_1
4497 in which case the distance vectors are (0) and (1). */
4499 static void
4500 add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
4502 unsigned i, j;
4504 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
4506 subscript_p sub = DDR_SUBSCRIPT (ddr, i);
4507 conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
4508 conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
4510 for (j = 0; j < ca->n; j++)
4511 if (affine_function_zero_p (ca->fns[j]))
4513 insert_innermost_unit_dist_vector (ddr);
4514 return;
4517 for (j = 0; j < cb->n; j++)
4518 if (affine_function_zero_p (cb->fns[j]))
4520 insert_innermost_unit_dist_vector (ddr);
4521 return;
4526 /* Return true when the DDR contains two data references that have the
4527 same access functions. */
4529 static inline bool
4530 same_access_functions (const struct data_dependence_relation *ddr)
4532 unsigned i;
4533 subscript *sub;
4535 FOR_EACH_VEC_ELT (DDR_SUBSCRIPTS (ddr), i, sub)
4536 if (!eq_evolutions_p (SUB_ACCESS_FN (sub, 0),
4537 SUB_ACCESS_FN (sub, 1)))
4538 return false;
4540 return true;
4543 /* Compute the classic per loop distance vector. DDR is the data
4544 dependence relation to build a vector from. Return false when fail
4545 to represent the data dependence as a distance vector. */
4547 static bool
4548 build_classic_dist_vector (struct data_dependence_relation *ddr,
4549 struct loop *loop_nest)
4551 bool init_b = false;
4552 int index_carry = DDR_NB_LOOPS (ddr);
4553 lambda_vector dist_v;
4555 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
4556 return false;
4558 if (same_access_functions (ddr))
4560 /* Save the 0 vector. */
4561 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4562 save_dist_v (ddr, dist_v);
4564 if (constant_access_functions (ddr))
4565 add_distance_for_zero_overlaps (ddr);
4567 if (DDR_NB_LOOPS (ddr) > 1)
4568 add_other_self_distances (ddr);
4570 return true;
4573 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4574 if (!build_classic_dist_vector_1 (ddr, 0, 1, dist_v, &init_b, &index_carry))
4575 return false;
4577 /* Save the distance vector if we initialized one. */
4578 if (init_b)
4580 /* Verify a basic constraint: classic distance vectors should
4581 always be lexicographically positive.
4583 Data references are collected in the order of execution of
4584 the program, thus for the following loop
4586 | for (i = 1; i < 100; i++)
4587 | for (j = 1; j < 100; j++)
4589 | t = T[j+1][i-1]; // A
4590 | T[j][i] = t + 2; // B
4593 references are collected following the direction of the wind:
4594 A then B. The data dependence tests are performed also
4595 following this order, such that we're looking at the distance
4596 separating the elements accessed by A from the elements later
4597 accessed by B. But in this example, the distance returned by
4598 test_dep (A, B) is lexicographically negative (-1, 1), that
4599 means that the access A occurs later than B with respect to
4600 the outer loop, ie. we're actually looking upwind. In this
4601 case we solve test_dep (B, A) looking downwind to the
4602 lexicographically positive solution, that returns the
4603 distance vector (1, -1). */
4604 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
4606 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4607 if (!subscript_dependence_tester_1 (ddr, 1, 0, loop_nest))
4608 return false;
4609 compute_subscript_distance (ddr);
4610 if (!build_classic_dist_vector_1 (ddr, 1, 0, save_v, &init_b,
4611 &index_carry))
4612 return false;
4613 save_dist_v (ddr, save_v);
4614 DDR_REVERSED_P (ddr) = true;
4616 /* In this case there is a dependence forward for all the
4617 outer loops:
4619 | for (k = 1; k < 100; k++)
4620 | for (i = 1; i < 100; i++)
4621 | for (j = 1; j < 100; j++)
4623 | t = T[j+1][i-1]; // A
4624 | T[j][i] = t + 2; // B
4627 the vectors are:
4628 (0, 1, -1)
4629 (1, 1, -1)
4630 (1, -1, 1)
4632 if (DDR_NB_LOOPS (ddr) > 1)
4634 add_outer_distances (ddr, save_v, index_carry);
4635 add_outer_distances (ddr, dist_v, index_carry);
4638 else
4640 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4641 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
4643 if (DDR_NB_LOOPS (ddr) > 1)
4645 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4647 if (!subscript_dependence_tester_1 (ddr, 1, 0, loop_nest))
4648 return false;
4649 compute_subscript_distance (ddr);
4650 if (!build_classic_dist_vector_1 (ddr, 1, 0, opposite_v, &init_b,
4651 &index_carry))
4652 return false;
4654 save_dist_v (ddr, save_v);
4655 add_outer_distances (ddr, dist_v, index_carry);
4656 add_outer_distances (ddr, opposite_v, index_carry);
4658 else
4659 save_dist_v (ddr, save_v);
4662 else
4664 /* There is a distance of 1 on all the outer loops: Example:
4665 there is a dependence of distance 1 on loop_1 for the array A.
4667 | loop_1
4668 | A[5] = ...
4669 | endloop
4671 add_outer_distances (ddr, dist_v,
4672 lambda_vector_first_nz (dist_v,
4673 DDR_NB_LOOPS (ddr), 0));
4676 if (dump_file && (dump_flags & TDF_DETAILS))
4678 unsigned i;
4680 fprintf (dump_file, "(build_classic_dist_vector\n");
4681 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4683 fprintf (dump_file, " dist_vector = (");
4684 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
4685 DDR_NB_LOOPS (ddr));
4686 fprintf (dump_file, " )\n");
4688 fprintf (dump_file, ")\n");
4691 return true;
4694 /* Return the direction for a given distance.
4695 FIXME: Computing dir this way is suboptimal, since dir can catch
4696 cases that dist is unable to represent. */
4698 static inline enum data_dependence_direction
4699 dir_from_dist (int dist)
4701 if (dist > 0)
4702 return dir_positive;
4703 else if (dist < 0)
4704 return dir_negative;
4705 else
4706 return dir_equal;
4709 /* Compute the classic per loop direction vector. DDR is the data
4710 dependence relation to build a vector from. */
4712 static void
4713 build_classic_dir_vector (struct data_dependence_relation *ddr)
4715 unsigned i, j;
4716 lambda_vector dist_v;
4718 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
4720 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4722 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
4723 dir_v[j] = dir_from_dist (dist_v[j]);
4725 save_dir_v (ddr, dir_v);
4729 /* Helper function. Returns true when there is a dependence between the
4730 data references. A_INDEX is the index of the first reference (0 for
4731 DDR_A, 1 for DDR_B) and B_INDEX is the index of the second reference. */
4733 static bool
4734 subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
4735 unsigned int a_index, unsigned int b_index,
4736 struct loop *loop_nest)
4738 unsigned int i;
4739 tree last_conflicts;
4740 struct subscript *subscript;
4741 tree res = NULL_TREE;
4743 for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
4745 conflict_function *overlaps_a, *overlaps_b;
4747 analyze_overlapping_iterations (SUB_ACCESS_FN (subscript, a_index),
4748 SUB_ACCESS_FN (subscript, b_index),
4749 &overlaps_a, &overlaps_b,
4750 &last_conflicts, loop_nest);
4752 if (SUB_CONFLICTS_IN_A (subscript))
4753 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
4754 if (SUB_CONFLICTS_IN_B (subscript))
4755 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
4757 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
4758 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
4759 SUB_LAST_CONFLICT (subscript) = last_conflicts;
4761 /* If there is any undetermined conflict function we have to
4762 give a conservative answer in case we cannot prove that
4763 no dependence exists when analyzing another subscript. */
4764 if (CF_NOT_KNOWN_P (overlaps_a)
4765 || CF_NOT_KNOWN_P (overlaps_b))
4767 res = chrec_dont_know;
4768 continue;
4771 /* When there is a subscript with no dependence we can stop. */
4772 else if (CF_NO_DEPENDENCE_P (overlaps_a)
4773 || CF_NO_DEPENDENCE_P (overlaps_b))
4775 res = chrec_known;
4776 break;
4780 if (res == NULL_TREE)
4781 return true;
4783 if (res == chrec_known)
4784 dependence_stats.num_dependence_independent++;
4785 else
4786 dependence_stats.num_dependence_undetermined++;
4787 finalize_ddr_dependent (ddr, res);
4788 return false;
4791 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
4793 static void
4794 subscript_dependence_tester (struct data_dependence_relation *ddr,
4795 struct loop *loop_nest)
4797 if (subscript_dependence_tester_1 (ddr, 0, 1, loop_nest))
4798 dependence_stats.num_dependence_dependent++;
4800 compute_subscript_distance (ddr);
4801 if (build_classic_dist_vector (ddr, loop_nest))
4802 build_classic_dir_vector (ddr);
4805 /* Returns true when all the access functions of A are affine or
4806 constant with respect to LOOP_NEST. */
4808 static bool
4809 access_functions_are_affine_or_constant_p (const struct data_reference *a,
4810 const struct loop *loop_nest)
4812 unsigned int i;
4813 vec<tree> fns = DR_ACCESS_FNS (a);
4814 tree t;
4816 FOR_EACH_VEC_ELT (fns, i, t)
4817 if (!evolution_function_is_invariant_p (t, loop_nest->num)
4818 && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
4819 return false;
4821 return true;
4824 /* This computes the affine dependence relation between A and B with
4825 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
4826 independence between two accesses, while CHREC_DONT_KNOW is used
4827 for representing the unknown relation.
4829 Note that it is possible to stop the computation of the dependence
4830 relation the first time we detect a CHREC_KNOWN element for a given
4831 subscript. */
4833 void
4834 compute_affine_dependence (struct data_dependence_relation *ddr,
4835 struct loop *loop_nest)
4837 struct data_reference *dra = DDR_A (ddr);
4838 struct data_reference *drb = DDR_B (ddr);
4840 if (dump_file && (dump_flags & TDF_DETAILS))
4842 fprintf (dump_file, "(compute_affine_dependence\n");
4843 fprintf (dump_file, " stmt_a: ");
4844 print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
4845 fprintf (dump_file, " stmt_b: ");
4846 print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
4849 /* Analyze only when the dependence relation is not yet known. */
4850 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4852 dependence_stats.num_dependence_tests++;
4854 if (access_functions_are_affine_or_constant_p (dra, loop_nest)
4855 && access_functions_are_affine_or_constant_p (drb, loop_nest))
4856 subscript_dependence_tester (ddr, loop_nest);
4858 /* As a last case, if the dependence cannot be determined, or if
4859 the dependence is considered too difficult to determine, answer
4860 "don't know". */
4861 else
4863 dependence_stats.num_dependence_undetermined++;
4865 if (dump_file && (dump_flags & TDF_DETAILS))
4867 fprintf (dump_file, "Data ref a:\n");
4868 dump_data_reference (dump_file, dra);
4869 fprintf (dump_file, "Data ref b:\n");
4870 dump_data_reference (dump_file, drb);
4871 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
4873 finalize_ddr_dependent (ddr, chrec_dont_know);
4877 if (dump_file && (dump_flags & TDF_DETAILS))
4879 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4880 fprintf (dump_file, ") -> no dependence\n");
4881 else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
4882 fprintf (dump_file, ") -> dependence analysis failed\n");
4883 else
4884 fprintf (dump_file, ")\n");
4888 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
4889 the data references in DATAREFS, in the LOOP_NEST. When
4890 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
4891 relations. Return true when successful, i.e. data references number
4892 is small enough to be handled. */
4894 bool
4895 compute_all_dependences (vec<data_reference_p> datarefs,
4896 vec<ddr_p> *dependence_relations,
4897 vec<loop_p> loop_nest,
4898 bool compute_self_and_rr)
4900 struct data_dependence_relation *ddr;
4901 struct data_reference *a, *b;
4902 unsigned int i, j;
4904 if ((int) datarefs.length ()
4905 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
4907 struct data_dependence_relation *ddr;
4909 /* Insert a single relation into dependence_relations:
4910 chrec_dont_know. */
4911 ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
4912 dependence_relations->safe_push (ddr);
4913 return false;
4916 FOR_EACH_VEC_ELT (datarefs, i, a)
4917 for (j = i + 1; datarefs.iterate (j, &b); j++)
4918 if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
4920 ddr = initialize_data_dependence_relation (a, b, loop_nest);
4921 dependence_relations->safe_push (ddr);
4922 if (loop_nest.exists ())
4923 compute_affine_dependence (ddr, loop_nest[0]);
4926 if (compute_self_and_rr)
4927 FOR_EACH_VEC_ELT (datarefs, i, a)
4929 ddr = initialize_data_dependence_relation (a, a, loop_nest);
4930 dependence_relations->safe_push (ddr);
4931 if (loop_nest.exists ())
4932 compute_affine_dependence (ddr, loop_nest[0]);
4935 return true;
4938 /* Describes a location of a memory reference. */
4940 struct data_ref_loc
4942 /* The memory reference. */
4943 tree ref;
4945 /* True if the memory reference is read. */
4946 bool is_read;
4948 /* True if the data reference is conditional within the containing
4949 statement, i.e. if it might not occur even when the statement
4950 is executed and runs to completion. */
4951 bool is_conditional_in_stmt;
4955 /* Stores the locations of memory references in STMT to REFERENCES. Returns
4956 true if STMT clobbers memory, false otherwise. */
4958 static bool
4959 get_references_in_stmt (gimple *stmt, vec<data_ref_loc, va_heap> *references)
4961 bool clobbers_memory = false;
4962 data_ref_loc ref;
4963 tree op0, op1;
4964 enum gimple_code stmt_code = gimple_code (stmt);
4966 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4967 As we cannot model data-references to not spelled out
4968 accesses give up if they may occur. */
4969 if (stmt_code == GIMPLE_CALL
4970 && !(gimple_call_flags (stmt) & ECF_CONST))
4972 /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
4973 if (gimple_call_internal_p (stmt))
4974 switch (gimple_call_internal_fn (stmt))
4976 case IFN_GOMP_SIMD_LANE:
4978 struct loop *loop = gimple_bb (stmt)->loop_father;
4979 tree uid = gimple_call_arg (stmt, 0);
4980 gcc_assert (TREE_CODE (uid) == SSA_NAME);
4981 if (loop == NULL
4982 || loop->simduid != SSA_NAME_VAR (uid))
4983 clobbers_memory = true;
4984 break;
4986 case IFN_MASK_LOAD:
4987 case IFN_MASK_STORE:
4988 break;
4989 default:
4990 clobbers_memory = true;
4991 break;
4993 else
4994 clobbers_memory = true;
4996 else if (stmt_code == GIMPLE_ASM
4997 && (gimple_asm_volatile_p (as_a <gasm *> (stmt))
4998 || gimple_vuse (stmt)))
4999 clobbers_memory = true;
5001 if (!gimple_vuse (stmt))
5002 return clobbers_memory;
5004 if (stmt_code == GIMPLE_ASSIGN)
5006 tree base;
5007 op0 = gimple_assign_lhs (stmt);
5008 op1 = gimple_assign_rhs1 (stmt);
5010 if (DECL_P (op1)
5011 || (REFERENCE_CLASS_P (op1)
5012 && (base = get_base_address (op1))
5013 && TREE_CODE (base) != SSA_NAME
5014 && !is_gimple_min_invariant (base)))
5016 ref.ref = op1;
5017 ref.is_read = true;
5018 ref.is_conditional_in_stmt = false;
5019 references->safe_push (ref);
5022 else if (stmt_code == GIMPLE_CALL)
5024 unsigned i, n;
5025 tree ptr, type;
5026 unsigned int align;
5028 ref.is_read = false;
5029 if (gimple_call_internal_p (stmt))
5030 switch (gimple_call_internal_fn (stmt))
5032 case IFN_MASK_LOAD:
5033 if (gimple_call_lhs (stmt) == NULL_TREE)
5034 break;
5035 ref.is_read = true;
5036 /* FALLTHRU */
5037 case IFN_MASK_STORE:
5038 ptr = build_int_cst (TREE_TYPE (gimple_call_arg (stmt, 1)), 0);
5039 align = tree_to_shwi (gimple_call_arg (stmt, 1));
5040 if (ref.is_read)
5041 type = TREE_TYPE (gimple_call_lhs (stmt));
5042 else
5043 type = TREE_TYPE (gimple_call_arg (stmt, 3));
5044 if (TYPE_ALIGN (type) != align)
5045 type = build_aligned_type (type, align);
5046 ref.is_conditional_in_stmt = true;
5047 ref.ref = fold_build2 (MEM_REF, type, gimple_call_arg (stmt, 0),
5048 ptr);
5049 references->safe_push (ref);
5050 return false;
5051 default:
5052 break;
5055 op0 = gimple_call_lhs (stmt);
5056 n = gimple_call_num_args (stmt);
5057 for (i = 0; i < n; i++)
5059 op1 = gimple_call_arg (stmt, i);
5061 if (DECL_P (op1)
5062 || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
5064 ref.ref = op1;
5065 ref.is_read = true;
5066 ref.is_conditional_in_stmt = false;
5067 references->safe_push (ref);
5071 else
5072 return clobbers_memory;
5074 if (op0
5075 && (DECL_P (op0)
5076 || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
5078 ref.ref = op0;
5079 ref.is_read = false;
5080 ref.is_conditional_in_stmt = false;
5081 references->safe_push (ref);
5083 return clobbers_memory;
5087 /* Returns true if the loop-nest has any data reference. */
5089 bool
5090 loop_nest_has_data_refs (loop_p loop)
5092 basic_block *bbs = get_loop_body (loop);
5093 auto_vec<data_ref_loc, 3> references;
5095 for (unsigned i = 0; i < loop->num_nodes; i++)
5097 basic_block bb = bbs[i];
5098 gimple_stmt_iterator bsi;
5100 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
5102 gimple *stmt = gsi_stmt (bsi);
5103 get_references_in_stmt (stmt, &references);
5104 if (references.length ())
5106 free (bbs);
5107 return true;
5111 free (bbs);
5112 return false;
5115 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
5116 reference, returns false, otherwise returns true. NEST is the outermost
5117 loop of the loop nest in which the references should be analyzed. */
5119 opt_result
5120 find_data_references_in_stmt (struct loop *nest, gimple *stmt,
5121 vec<data_reference_p> *datarefs)
5123 unsigned i;
5124 auto_vec<data_ref_loc, 2> references;
5125 data_ref_loc *ref;
5126 data_reference_p dr;
5128 if (get_references_in_stmt (stmt, &references))
5129 return opt_result::failure_at (stmt, "statement clobbers memory: %G",
5130 stmt);
5132 FOR_EACH_VEC_ELT (references, i, ref)
5134 dr = create_data_ref (nest ? loop_preheader_edge (nest) : NULL,
5135 loop_containing_stmt (stmt), ref->ref,
5136 stmt, ref->is_read, ref->is_conditional_in_stmt);
5137 gcc_assert (dr != NULL);
5138 datarefs->safe_push (dr);
5141 return opt_result::success ();
5144 /* Stores the data references in STMT to DATAREFS. If there is an
5145 unanalyzable reference, returns false, otherwise returns true.
5146 NEST is the outermost loop of the loop nest in which the references
5147 should be instantiated, LOOP is the loop in which the references
5148 should be analyzed. */
5150 bool
5151 graphite_find_data_references_in_stmt (edge nest, loop_p loop, gimple *stmt,
5152 vec<data_reference_p> *datarefs)
5154 unsigned i;
5155 auto_vec<data_ref_loc, 2> references;
5156 data_ref_loc *ref;
5157 bool ret = true;
5158 data_reference_p dr;
5160 if (get_references_in_stmt (stmt, &references))
5161 return false;
5163 FOR_EACH_VEC_ELT (references, i, ref)
5165 dr = create_data_ref (nest, loop, ref->ref, stmt, ref->is_read,
5166 ref->is_conditional_in_stmt);
5167 gcc_assert (dr != NULL);
5168 datarefs->safe_push (dr);
5171 return ret;
5174 /* Search the data references in LOOP, and record the information into
5175 DATAREFS. Returns chrec_dont_know when failing to analyze a
5176 difficult case, returns NULL_TREE otherwise. */
5178 tree
5179 find_data_references_in_bb (struct loop *loop, basic_block bb,
5180 vec<data_reference_p> *datarefs)
5182 gimple_stmt_iterator bsi;
5184 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
5186 gimple *stmt = gsi_stmt (bsi);
5188 if (!find_data_references_in_stmt (loop, stmt, datarefs))
5190 struct data_reference *res;
5191 res = XCNEW (struct data_reference);
5192 datarefs->safe_push (res);
5194 return chrec_dont_know;
5198 return NULL_TREE;
5201 /* Search the data references in LOOP, and record the information into
5202 DATAREFS. Returns chrec_dont_know when failing to analyze a
5203 difficult case, returns NULL_TREE otherwise.
5205 TODO: This function should be made smarter so that it can handle address
5206 arithmetic as if they were array accesses, etc. */
5208 tree
5209 find_data_references_in_loop (struct loop *loop,
5210 vec<data_reference_p> *datarefs)
5212 basic_block bb, *bbs;
5213 unsigned int i;
5215 bbs = get_loop_body_in_dom_order (loop);
5217 for (i = 0; i < loop->num_nodes; i++)
5219 bb = bbs[i];
5221 if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
5223 free (bbs);
5224 return chrec_dont_know;
5227 free (bbs);
5229 return NULL_TREE;
5232 /* Return the alignment in bytes that DRB is guaranteed to have at all
5233 times. */
5235 unsigned int
5236 dr_alignment (innermost_loop_behavior *drb)
5238 /* Get the alignment of BASE_ADDRESS + INIT. */
5239 unsigned int alignment = drb->base_alignment;
5240 unsigned int misalignment = (drb->base_misalignment
5241 + TREE_INT_CST_LOW (drb->init));
5242 if (misalignment != 0)
5243 alignment = MIN (alignment, misalignment & -misalignment);
5245 /* Cap it to the alignment of OFFSET. */
5246 if (!integer_zerop (drb->offset))
5247 alignment = MIN (alignment, drb->offset_alignment);
5249 /* Cap it to the alignment of STEP. */
5250 if (!integer_zerop (drb->step))
5251 alignment = MIN (alignment, drb->step_alignment);
5253 return alignment;
5256 /* If BASE is a pointer-typed SSA name, try to find the object that it
5257 is based on. Return this object X on success and store the alignment
5258 in bytes of BASE - &X in *ALIGNMENT_OUT. */
5260 static tree
5261 get_base_for_alignment_1 (tree base, unsigned int *alignment_out)
5263 if (TREE_CODE (base) != SSA_NAME || !POINTER_TYPE_P (TREE_TYPE (base)))
5264 return NULL_TREE;
5266 gimple *def = SSA_NAME_DEF_STMT (base);
5267 base = analyze_scalar_evolution (loop_containing_stmt (def), base);
5269 /* Peel chrecs and record the minimum alignment preserved by
5270 all steps. */
5271 unsigned int alignment = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
5272 while (TREE_CODE (base) == POLYNOMIAL_CHREC)
5274 unsigned int step_alignment = highest_pow2_factor (CHREC_RIGHT (base));
5275 alignment = MIN (alignment, step_alignment);
5276 base = CHREC_LEFT (base);
5279 /* Punt if the expression is too complicated to handle. */
5280 if (tree_contains_chrecs (base, NULL) || !POINTER_TYPE_P (TREE_TYPE (base)))
5281 return NULL_TREE;
5283 /* The only useful cases are those for which a dereference folds to something
5284 other than an INDIRECT_REF. */
5285 tree ref_type = TREE_TYPE (TREE_TYPE (base));
5286 tree ref = fold_indirect_ref_1 (UNKNOWN_LOCATION, ref_type, base);
5287 if (!ref)
5288 return NULL_TREE;
5290 /* Analyze the base to which the steps we peeled were applied. */
5291 poly_int64 bitsize, bitpos, bytepos;
5292 machine_mode mode;
5293 int unsignedp, reversep, volatilep;
5294 tree offset;
5295 base = get_inner_reference (ref, &bitsize, &bitpos, &offset, &mode,
5296 &unsignedp, &reversep, &volatilep);
5297 if (!base || !multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
5298 return NULL_TREE;
5300 /* Restrict the alignment to that guaranteed by the offsets. */
5301 unsigned int bytepos_alignment = known_alignment (bytepos);
5302 if (bytepos_alignment != 0)
5303 alignment = MIN (alignment, bytepos_alignment);
5304 if (offset)
5306 unsigned int offset_alignment = highest_pow2_factor (offset);
5307 alignment = MIN (alignment, offset_alignment);
5310 *alignment_out = alignment;
5311 return base;
5314 /* Return the object whose alignment would need to be changed in order
5315 to increase the alignment of ADDR. Store the maximum achievable
5316 alignment in *MAX_ALIGNMENT. */
5318 tree
5319 get_base_for_alignment (tree addr, unsigned int *max_alignment)
5321 tree base = get_base_for_alignment_1 (addr, max_alignment);
5322 if (base)
5323 return base;
5325 if (TREE_CODE (addr) == ADDR_EXPR)
5326 addr = TREE_OPERAND (addr, 0);
5327 *max_alignment = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
5328 return addr;
5331 /* Recursive helper function. */
5333 static bool
5334 find_loop_nest_1 (struct loop *loop, vec<loop_p> *loop_nest)
5336 /* Inner loops of the nest should not contain siblings. Example:
5337 when there are two consecutive loops,
5339 | loop_0
5340 | loop_1
5341 | A[{0, +, 1}_1]
5342 | endloop_1
5343 | loop_2
5344 | A[{0, +, 1}_2]
5345 | endloop_2
5346 | endloop_0
5348 the dependence relation cannot be captured by the distance
5349 abstraction. */
5350 if (loop->next)
5351 return false;
5353 loop_nest->safe_push (loop);
5354 if (loop->inner)
5355 return find_loop_nest_1 (loop->inner, loop_nest);
5356 return true;
5359 /* Return false when the LOOP is not well nested. Otherwise return
5360 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
5361 contain the loops from the outermost to the innermost, as they will
5362 appear in the classic distance vector. */
5364 bool
5365 find_loop_nest (struct loop *loop, vec<loop_p> *loop_nest)
5367 loop_nest->safe_push (loop);
5368 if (loop->inner)
5369 return find_loop_nest_1 (loop->inner, loop_nest);
5370 return true;
5373 /* Returns true when the data dependences have been computed, false otherwise.
5374 Given a loop nest LOOP, the following vectors are returned:
5375 DATAREFS is initialized to all the array elements contained in this loop,
5376 DEPENDENCE_RELATIONS contains the relations between the data references.
5377 Compute read-read and self relations if
5378 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
5380 bool
5381 compute_data_dependences_for_loop (struct loop *loop,
5382 bool compute_self_and_read_read_dependences,
5383 vec<loop_p> *loop_nest,
5384 vec<data_reference_p> *datarefs,
5385 vec<ddr_p> *dependence_relations)
5387 bool res = true;
5389 memset (&dependence_stats, 0, sizeof (dependence_stats));
5391 /* If the loop nest is not well formed, or one of the data references
5392 is not computable, give up without spending time to compute other
5393 dependences. */
5394 if (!loop
5395 || !find_loop_nest (loop, loop_nest)
5396 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
5397 || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
5398 compute_self_and_read_read_dependences))
5399 res = false;
5401 if (dump_file && (dump_flags & TDF_STATS))
5403 fprintf (dump_file, "Dependence tester statistics:\n");
5405 fprintf (dump_file, "Number of dependence tests: %d\n",
5406 dependence_stats.num_dependence_tests);
5407 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
5408 dependence_stats.num_dependence_dependent);
5409 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
5410 dependence_stats.num_dependence_independent);
5411 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
5412 dependence_stats.num_dependence_undetermined);
5414 fprintf (dump_file, "Number of subscript tests: %d\n",
5415 dependence_stats.num_subscript_tests);
5416 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
5417 dependence_stats.num_subscript_undetermined);
5418 fprintf (dump_file, "Number of same subscript function: %d\n",
5419 dependence_stats.num_same_subscript_function);
5421 fprintf (dump_file, "Number of ziv tests: %d\n",
5422 dependence_stats.num_ziv);
5423 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
5424 dependence_stats.num_ziv_dependent);
5425 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
5426 dependence_stats.num_ziv_independent);
5427 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
5428 dependence_stats.num_ziv_unimplemented);
5430 fprintf (dump_file, "Number of siv tests: %d\n",
5431 dependence_stats.num_siv);
5432 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
5433 dependence_stats.num_siv_dependent);
5434 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
5435 dependence_stats.num_siv_independent);
5436 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
5437 dependence_stats.num_siv_unimplemented);
5439 fprintf (dump_file, "Number of miv tests: %d\n",
5440 dependence_stats.num_miv);
5441 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
5442 dependence_stats.num_miv_dependent);
5443 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
5444 dependence_stats.num_miv_independent);
5445 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
5446 dependence_stats.num_miv_unimplemented);
5449 return res;
5452 /* Free the memory used by a data dependence relation DDR. */
5454 void
5455 free_dependence_relation (struct data_dependence_relation *ddr)
5457 if (ddr == NULL)
5458 return;
5460 if (DDR_SUBSCRIPTS (ddr).exists ())
5461 free_subscripts (DDR_SUBSCRIPTS (ddr));
5462 DDR_DIST_VECTS (ddr).release ();
5463 DDR_DIR_VECTS (ddr).release ();
5465 free (ddr);
5468 /* Free the memory used by the data dependence relations from
5469 DEPENDENCE_RELATIONS. */
5471 void
5472 free_dependence_relations (vec<ddr_p> dependence_relations)
5474 unsigned int i;
5475 struct data_dependence_relation *ddr;
5477 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
5478 if (ddr)
5479 free_dependence_relation (ddr);
5481 dependence_relations.release ();
5484 /* Free the memory used by the data references from DATAREFS. */
5486 void
5487 free_data_refs (vec<data_reference_p> datarefs)
5489 unsigned int i;
5490 struct data_reference *dr;
5492 FOR_EACH_VEC_ELT (datarefs, i, dr)
5493 free_data_ref (dr);
5494 datarefs.release ();
5497 /* Common routine implementing both dr_direction_indicator and
5498 dr_zero_step_indicator. Return USEFUL_MIN if the indicator is known
5499 to be >= USEFUL_MIN and -1 if the indicator is known to be negative.
5500 Return the step as the indicator otherwise. */
5502 static tree
5503 dr_step_indicator (struct data_reference *dr, int useful_min)
5505 tree step = DR_STEP (dr);
5506 if (!step)
5507 return NULL_TREE;
5508 STRIP_NOPS (step);
5509 /* Look for cases where the step is scaled by a positive constant
5510 integer, which will often be the access size. If the multiplication
5511 doesn't change the sign (due to overflow effects) then we can
5512 test the unscaled value instead. */
5513 if (TREE_CODE (step) == MULT_EXPR
5514 && TREE_CODE (TREE_OPERAND (step, 1)) == INTEGER_CST
5515 && tree_int_cst_sgn (TREE_OPERAND (step, 1)) > 0)
5517 tree factor = TREE_OPERAND (step, 1);
5518 step = TREE_OPERAND (step, 0);
5520 /* Strip widening and truncating conversions as well as nops. */
5521 if (CONVERT_EXPR_P (step)
5522 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (step, 0))))
5523 step = TREE_OPERAND (step, 0);
5524 tree type = TREE_TYPE (step);
5526 /* Get the range of step values that would not cause overflow. */
5527 widest_int minv = (wi::to_widest (TYPE_MIN_VALUE (ssizetype))
5528 / wi::to_widest (factor));
5529 widest_int maxv = (wi::to_widest (TYPE_MAX_VALUE (ssizetype))
5530 / wi::to_widest (factor));
5532 /* Get the range of values that the unconverted step actually has. */
5533 wide_int step_min, step_max;
5534 if (TREE_CODE (step) != SSA_NAME
5535 || get_range_info (step, &step_min, &step_max) != VR_RANGE)
5537 step_min = wi::to_wide (TYPE_MIN_VALUE (type));
5538 step_max = wi::to_wide (TYPE_MAX_VALUE (type));
5541 /* Check whether the unconverted step has an acceptable range. */
5542 signop sgn = TYPE_SIGN (type);
5543 if (wi::les_p (minv, widest_int::from (step_min, sgn))
5544 && wi::ges_p (maxv, widest_int::from (step_max, sgn)))
5546 if (wi::ge_p (step_min, useful_min, sgn))
5547 return ssize_int (useful_min);
5548 else if (wi::lt_p (step_max, 0, sgn))
5549 return ssize_int (-1);
5550 else
5551 return fold_convert (ssizetype, step);
5554 return DR_STEP (dr);
5557 /* Return a value that is negative iff DR has a negative step. */
5559 tree
5560 dr_direction_indicator (struct data_reference *dr)
5562 return dr_step_indicator (dr, 0);
5565 /* Return a value that is zero iff DR has a zero step. */
5567 tree
5568 dr_zero_step_indicator (struct data_reference *dr)
5570 return dr_step_indicator (dr, 1);
5573 /* Return true if DR is known to have a nonnegative (but possibly zero)
5574 step. */
5576 bool
5577 dr_known_forward_stride_p (struct data_reference *dr)
5579 tree indicator = dr_direction_indicator (dr);
5580 tree neg_step_val = fold_binary (LT_EXPR, boolean_type_node,
5581 fold_convert (ssizetype, indicator),
5582 ssize_int (0));
5583 return neg_step_val && integer_zerop (neg_step_val);