Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS hook.
[official-gcc.git] / gcc / tree-data-ref.c
blob5b4f0928ffe17b0bd4e5805e1686782a281deda6
1 /* Data references and dependences detectors.
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This pass walks a given loop structure searching for array
22 references. The information about the array accesses is recorded
23 in DATA_REFERENCE structures.
25 The basic test for determining the dependences is:
26 given two access functions chrec1 and chrec2 to a same array, and
27 x and y two vectors from the iteration domain, the same element of
28 the array is accessed twice at iterations x and y if and only if:
29 | chrec1 (x) == chrec2 (y).
31 The goals of this analysis are:
33 - to determine the independence: the relation between two
34 independent accesses is qualified with the chrec_known (this
35 information allows a loop parallelization),
37 - when two data references access the same data, to qualify the
38 dependence relation with classic dependence representations:
40 - distance vectors
41 - direction vectors
42 - loop carried level dependence
43 - polyhedron dependence
44 or with the chains of recurrences based representation,
46 - to define a knowledge base for storing the data dependence
47 information,
49 - to define an interface to access this data.
52 Definitions:
54 - subscript: given two array accesses a subscript is the tuple
55 composed of the access functions for a given dimension. Example:
56 Given A[f1][f2][f3] and B[g1][g2][g3], there are three subscripts:
57 (f1, g1), (f2, g2), (f3, g3).
59 - Diophantine equation: an equation whose coefficients and
60 solutions are integer constants, for example the equation
61 | 3*x + 2*y = 1
62 has an integer solution x = 1 and y = -1.
64 References:
66 - "Advanced Compilation for High Performance Computing" by Randy
67 Allen and Ken Kennedy.
68 http://citeseer.ist.psu.edu/goff91practical.html
70 - "Loop Transformations for Restructuring Compilers - The Foundations"
71 by Utpal Banerjee.
76 #include "config.h"
77 #include "system.h"
78 #include "coretypes.h"
79 #include "input.h"
80 #include "alias.h"
81 #include "symtab.h"
82 #include "options.h"
83 #include "tree.h"
84 #include "fold-const.h"
85 #include "tm.h"
86 #include "hard-reg-set.h"
87 #include "function.h"
88 #include "rtl.h"
89 #include "flags.h"
90 #include "insn-config.h"
91 #include "expmed.h"
92 #include "dojump.h"
93 #include "explow.h"
94 #include "calls.h"
95 #include "emit-rtl.h"
96 #include "varasm.h"
97 #include "stmt.h"
98 #include "expr.h"
99 #include "gimple-pretty-print.h"
100 #include "predict.h"
101 #include "dominance.h"
102 #include "cfg.h"
103 #include "basic-block.h"
104 #include "tree-ssa-alias.h"
105 #include "internal-fn.h"
106 #include "gimple-expr.h"
107 #include "is-a.h"
108 #include "gimple.h"
109 #include "gimple-iterator.h"
110 #include "tree-ssa-loop-niter.h"
111 #include "tree-ssa-loop.h"
112 #include "tree-ssa.h"
113 #include "cfgloop.h"
114 #include "tree-data-ref.h"
115 #include "tree-scalar-evolution.h"
116 #include "dumpfile.h"
117 #include "langhooks.h"
118 #include "tree-affine.h"
119 #include "params.h"
121 static struct datadep_stats
123 int num_dependence_tests;
124 int num_dependence_dependent;
125 int num_dependence_independent;
126 int num_dependence_undetermined;
128 int num_subscript_tests;
129 int num_subscript_undetermined;
130 int num_same_subscript_function;
132 int num_ziv;
133 int num_ziv_independent;
134 int num_ziv_dependent;
135 int num_ziv_unimplemented;
137 int num_siv;
138 int num_siv_independent;
139 int num_siv_dependent;
140 int num_siv_unimplemented;
142 int num_miv;
143 int num_miv_independent;
144 int num_miv_dependent;
145 int num_miv_unimplemented;
146 } dependence_stats;
148 static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
149 struct data_reference *,
150 struct data_reference *,
151 struct loop *);
152 /* Returns true iff A divides B. */
154 static inline bool
155 tree_fold_divides_p (const_tree a, const_tree b)
157 gcc_assert (TREE_CODE (a) == INTEGER_CST);
158 gcc_assert (TREE_CODE (b) == INTEGER_CST);
159 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a));
162 /* Returns true iff A divides B. */
164 static inline bool
165 int_divides_p (int a, int b)
167 return ((b % a) == 0);
172 /* Dump into FILE all the data references from DATAREFS. */
174 static void
175 dump_data_references (FILE *file, vec<data_reference_p> datarefs)
177 unsigned int i;
178 struct data_reference *dr;
180 FOR_EACH_VEC_ELT (datarefs, i, dr)
181 dump_data_reference (file, dr);
184 /* Unified dump into FILE all the data references from DATAREFS. */
186 DEBUG_FUNCTION void
187 debug (vec<data_reference_p> &ref)
189 dump_data_references (stderr, ref);
192 DEBUG_FUNCTION void
193 debug (vec<data_reference_p> *ptr)
195 if (ptr)
196 debug (*ptr);
197 else
198 fprintf (stderr, "<nil>\n");
202 /* Dump into STDERR all the data references from DATAREFS. */
204 DEBUG_FUNCTION void
205 debug_data_references (vec<data_reference_p> datarefs)
207 dump_data_references (stderr, datarefs);
210 /* Print to STDERR the data_reference DR. */
212 DEBUG_FUNCTION void
213 debug_data_reference (struct data_reference *dr)
215 dump_data_reference (stderr, dr);
218 /* Dump function for a DATA_REFERENCE structure. */
220 void
221 dump_data_reference (FILE *outf,
222 struct data_reference *dr)
224 unsigned int i;
226 fprintf (outf, "#(Data Ref: \n");
227 fprintf (outf, "# bb: %d \n", gimple_bb (DR_STMT (dr))->index);
228 fprintf (outf, "# stmt: ");
229 print_gimple_stmt (outf, DR_STMT (dr), 0, 0);
230 fprintf (outf, "# ref: ");
231 print_generic_stmt (outf, DR_REF (dr), 0);
232 fprintf (outf, "# base_object: ");
233 print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
235 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
237 fprintf (outf, "# Access function %d: ", i);
238 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
240 fprintf (outf, "#)\n");
243 /* Unified dump function for a DATA_REFERENCE structure. */
245 DEBUG_FUNCTION void
246 debug (data_reference &ref)
248 dump_data_reference (stderr, &ref);
251 DEBUG_FUNCTION void
252 debug (data_reference *ptr)
254 if (ptr)
255 debug (*ptr);
256 else
257 fprintf (stderr, "<nil>\n");
261 /* Dumps the affine function described by FN to the file OUTF. */
263 static void
264 dump_affine_function (FILE *outf, affine_fn fn)
266 unsigned i;
267 tree coef;
269 print_generic_expr (outf, fn[0], TDF_SLIM);
270 for (i = 1; fn.iterate (i, &coef); i++)
272 fprintf (outf, " + ");
273 print_generic_expr (outf, coef, TDF_SLIM);
274 fprintf (outf, " * x_%u", i);
278 /* Dumps the conflict function CF to the file OUTF. */
280 static void
281 dump_conflict_function (FILE *outf, conflict_function *cf)
283 unsigned i;
285 if (cf->n == NO_DEPENDENCE)
286 fprintf (outf, "no dependence");
287 else if (cf->n == NOT_KNOWN)
288 fprintf (outf, "not known");
289 else
291 for (i = 0; i < cf->n; i++)
293 if (i != 0)
294 fprintf (outf, " ");
295 fprintf (outf, "[");
296 dump_affine_function (outf, cf->fns[i]);
297 fprintf (outf, "]");
302 /* Dump function for a SUBSCRIPT structure. */
304 static void
305 dump_subscript (FILE *outf, struct subscript *subscript)
307 conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
309 fprintf (outf, "\n (subscript \n");
310 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
311 dump_conflict_function (outf, cf);
312 if (CF_NONTRIVIAL_P (cf))
314 tree last_iteration = SUB_LAST_CONFLICT (subscript);
315 fprintf (outf, "\n last_conflict: ");
316 print_generic_expr (outf, last_iteration, 0);
319 cf = SUB_CONFLICTS_IN_B (subscript);
320 fprintf (outf, "\n iterations_that_access_an_element_twice_in_B: ");
321 dump_conflict_function (outf, cf);
322 if (CF_NONTRIVIAL_P (cf))
324 tree last_iteration = SUB_LAST_CONFLICT (subscript);
325 fprintf (outf, "\n last_conflict: ");
326 print_generic_expr (outf, last_iteration, 0);
329 fprintf (outf, "\n (Subscript distance: ");
330 print_generic_expr (outf, SUB_DISTANCE (subscript), 0);
331 fprintf (outf, " ))\n");
334 /* Print the classic direction vector DIRV to OUTF. */
336 static void
337 print_direction_vector (FILE *outf,
338 lambda_vector dirv,
339 int length)
341 int eq;
343 for (eq = 0; eq < length; eq++)
345 enum data_dependence_direction dir = ((enum data_dependence_direction)
346 dirv[eq]);
348 switch (dir)
350 case dir_positive:
351 fprintf (outf, " +");
352 break;
353 case dir_negative:
354 fprintf (outf, " -");
355 break;
356 case dir_equal:
357 fprintf (outf, " =");
358 break;
359 case dir_positive_or_equal:
360 fprintf (outf, " +=");
361 break;
362 case dir_positive_or_negative:
363 fprintf (outf, " +-");
364 break;
365 case dir_negative_or_equal:
366 fprintf (outf, " -=");
367 break;
368 case dir_star:
369 fprintf (outf, " *");
370 break;
371 default:
372 fprintf (outf, "indep");
373 break;
376 fprintf (outf, "\n");
379 /* Print a vector of direction vectors. */
381 static void
382 print_dir_vectors (FILE *outf, vec<lambda_vector> dir_vects,
383 int length)
385 unsigned j;
386 lambda_vector v;
388 FOR_EACH_VEC_ELT (dir_vects, j, v)
389 print_direction_vector (outf, v, length);
392 /* Print out a vector VEC of length N to OUTFILE. */
394 static inline void
395 print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
397 int i;
399 for (i = 0; i < n; i++)
400 fprintf (outfile, "%3d ", vector[i]);
401 fprintf (outfile, "\n");
404 /* Print a vector of distance vectors. */
406 static void
407 print_dist_vectors (FILE *outf, vec<lambda_vector> dist_vects,
408 int length)
410 unsigned j;
411 lambda_vector v;
413 FOR_EACH_VEC_ELT (dist_vects, j, v)
414 print_lambda_vector (outf, v, length);
417 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
419 static void
420 dump_data_dependence_relation (FILE *outf,
421 struct data_dependence_relation *ddr)
423 struct data_reference *dra, *drb;
425 fprintf (outf, "(Data Dep: \n");
427 if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
429 if (ddr)
431 dra = DDR_A (ddr);
432 drb = DDR_B (ddr);
433 if (dra)
434 dump_data_reference (outf, dra);
435 else
436 fprintf (outf, " (nil)\n");
437 if (drb)
438 dump_data_reference (outf, drb);
439 else
440 fprintf (outf, " (nil)\n");
442 fprintf (outf, " (don't know)\n)\n");
443 return;
446 dra = DDR_A (ddr);
447 drb = DDR_B (ddr);
448 dump_data_reference (outf, dra);
449 dump_data_reference (outf, drb);
451 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
452 fprintf (outf, " (no dependence)\n");
454 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
456 unsigned int i;
457 struct loop *loopi;
459 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
461 fprintf (outf, " access_fn_A: ");
462 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
463 fprintf (outf, " access_fn_B: ");
464 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
465 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
468 fprintf (outf, " inner loop index: %d\n", DDR_INNER_LOOP (ddr));
469 fprintf (outf, " loop nest: (");
470 FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
471 fprintf (outf, "%d ", loopi->num);
472 fprintf (outf, ")\n");
474 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
476 fprintf (outf, " distance_vector: ");
477 print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
478 DDR_NB_LOOPS (ddr));
481 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
483 fprintf (outf, " direction_vector: ");
484 print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
485 DDR_NB_LOOPS (ddr));
489 fprintf (outf, ")\n");
492 /* Debug version. */
494 DEBUG_FUNCTION void
495 debug_data_dependence_relation (struct data_dependence_relation *ddr)
497 dump_data_dependence_relation (stderr, ddr);
500 /* Dump into FILE all the dependence relations from DDRS. */
502 void
503 dump_data_dependence_relations (FILE *file,
504 vec<ddr_p> ddrs)
506 unsigned int i;
507 struct data_dependence_relation *ddr;
509 FOR_EACH_VEC_ELT (ddrs, i, ddr)
510 dump_data_dependence_relation (file, ddr);
513 DEBUG_FUNCTION void
514 debug (vec<ddr_p> &ref)
516 dump_data_dependence_relations (stderr, ref);
519 DEBUG_FUNCTION void
520 debug (vec<ddr_p> *ptr)
522 if (ptr)
523 debug (*ptr);
524 else
525 fprintf (stderr, "<nil>\n");
529 /* Dump to STDERR all the dependence relations from DDRS. */
531 DEBUG_FUNCTION void
532 debug_data_dependence_relations (vec<ddr_p> ddrs)
534 dump_data_dependence_relations (stderr, ddrs);
537 /* Dumps the distance and direction vectors in FILE. DDRS contains
538 the dependence relations, and VECT_SIZE is the size of the
539 dependence vectors, or in other words the number of loops in the
540 considered nest. */
542 static void
543 dump_dist_dir_vectors (FILE *file, vec<ddr_p> ddrs)
545 unsigned int i, j;
546 struct data_dependence_relation *ddr;
547 lambda_vector v;
549 FOR_EACH_VEC_ELT (ddrs, i, ddr)
550 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
552 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), j, v)
554 fprintf (file, "DISTANCE_V (");
555 print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
556 fprintf (file, ")\n");
559 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), j, v)
561 fprintf (file, "DIRECTION_V (");
562 print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
563 fprintf (file, ")\n");
567 fprintf (file, "\n\n");
570 /* Dumps the data dependence relations DDRS in FILE. */
572 static void
573 dump_ddrs (FILE *file, vec<ddr_p> ddrs)
575 unsigned int i;
576 struct data_dependence_relation *ddr;
578 FOR_EACH_VEC_ELT (ddrs, i, ddr)
579 dump_data_dependence_relation (file, ddr);
581 fprintf (file, "\n\n");
584 DEBUG_FUNCTION void
585 debug_ddrs (vec<ddr_p> ddrs)
587 dump_ddrs (stderr, ddrs);
590 /* Helper function for split_constant_offset. Expresses OP0 CODE OP1
591 (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
592 constant of type ssizetype, and returns true. If we cannot do this
593 with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
594 is returned. */
596 static bool
597 split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
598 tree *var, tree *off)
600 tree var0, var1;
601 tree off0, off1;
602 enum tree_code ocode = code;
604 *var = NULL_TREE;
605 *off = NULL_TREE;
607 switch (code)
609 case INTEGER_CST:
610 *var = build_int_cst (type, 0);
611 *off = fold_convert (ssizetype, op0);
612 return true;
614 case POINTER_PLUS_EXPR:
615 ocode = PLUS_EXPR;
616 /* FALLTHROUGH */
617 case PLUS_EXPR:
618 case MINUS_EXPR:
619 split_constant_offset (op0, &var0, &off0);
620 split_constant_offset (op1, &var1, &off1);
621 *var = fold_build2 (code, type, var0, var1);
622 *off = size_binop (ocode, off0, off1);
623 return true;
625 case MULT_EXPR:
626 if (TREE_CODE (op1) != INTEGER_CST)
627 return false;
629 split_constant_offset (op0, &var0, &off0);
630 *var = fold_build2 (MULT_EXPR, type, var0, op1);
631 *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
632 return true;
634 case ADDR_EXPR:
636 tree base, poffset;
637 HOST_WIDE_INT pbitsize, pbitpos;
638 machine_mode pmode;
639 int punsignedp, pvolatilep;
641 op0 = TREE_OPERAND (op0, 0);
642 base = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset,
643 &pmode, &punsignedp, &pvolatilep, false);
645 if (pbitpos % BITS_PER_UNIT != 0)
646 return false;
647 base = build_fold_addr_expr (base);
648 off0 = ssize_int (pbitpos / BITS_PER_UNIT);
650 if (poffset)
652 split_constant_offset (poffset, &poffset, &off1);
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 var0 = gimple_assign_rhs1 (def_stmt);
697 subcode = gimple_assign_rhs_code (def_stmt);
698 var1 = gimple_assign_rhs2 (def_stmt);
700 return split_constant_offset_1 (type, var0, subcode, var1, var, off);
702 CASE_CONVERT:
704 /* We must not introduce undefined overflow, and we must not change the value.
705 Hence we're okay if the inner type doesn't overflow to start with
706 (pointer or signed), the outer type also is an integer or pointer
707 and the outer precision is at least as large as the inner. */
708 tree itype = TREE_TYPE (op0);
709 if ((POINTER_TYPE_P (itype)
710 || (INTEGRAL_TYPE_P (itype) && TYPE_OVERFLOW_UNDEFINED (itype)))
711 && TYPE_PRECISION (type) >= TYPE_PRECISION (itype)
712 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)))
714 split_constant_offset (op0, &var0, off);
715 *var = fold_convert (type, var0);
716 return true;
718 return false;
721 default:
722 return false;
726 /* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
727 will be ssizetype. */
729 void
730 split_constant_offset (tree exp, tree *var, tree *off)
732 tree type = TREE_TYPE (exp), otype, op0, op1, e, o;
733 enum tree_code code;
735 *var = exp;
736 *off = ssize_int (0);
737 STRIP_NOPS (exp);
739 if (tree_is_chrec (exp)
740 || get_gimple_rhs_class (TREE_CODE (exp)) == GIMPLE_TERNARY_RHS)
741 return;
743 otype = TREE_TYPE (exp);
744 code = TREE_CODE (exp);
745 extract_ops_from_tree (exp, &code, &op0, &op1);
746 if (split_constant_offset_1 (otype, op0, code, op1, &e, &o))
748 *var = fold_convert (type, e);
749 *off = o;
753 /* Returns the address ADDR of an object in a canonical shape (without nop
754 casts, and with type of pointer to the object). */
756 static tree
757 canonicalize_base_object_address (tree addr)
759 tree orig = addr;
761 STRIP_NOPS (addr);
763 /* The base address may be obtained by casting from integer, in that case
764 keep the cast. */
765 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
766 return orig;
768 if (TREE_CODE (addr) != ADDR_EXPR)
769 return addr;
771 return build_fold_addr_expr (TREE_OPERAND (addr, 0));
774 /* Analyzes the behavior of the memory reference DR in the innermost loop or
775 basic block that contains it. Returns true if analysis succeed or false
776 otherwise. */
778 bool
779 dr_analyze_innermost (struct data_reference *dr, struct loop *nest)
781 gimple stmt = DR_STMT (dr);
782 struct loop *loop = loop_containing_stmt (stmt);
783 tree ref = DR_REF (dr);
784 HOST_WIDE_INT pbitsize, pbitpos;
785 tree base, poffset;
786 machine_mode pmode;
787 int punsignedp, pvolatilep;
788 affine_iv base_iv, offset_iv;
789 tree init, dinit, step;
790 bool in_loop = (loop && loop->num);
792 if (dump_file && (dump_flags & TDF_DETAILS))
793 fprintf (dump_file, "analyze_innermost: ");
795 base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset,
796 &pmode, &punsignedp, &pvolatilep, false);
797 gcc_assert (base != NULL_TREE);
799 if (pbitpos % BITS_PER_UNIT != 0)
801 if (dump_file && (dump_flags & TDF_DETAILS))
802 fprintf (dump_file, "failed: bit offset alignment.\n");
803 return false;
806 if (TREE_CODE (base) == MEM_REF)
808 if (!integer_zerop (TREE_OPERAND (base, 1)))
810 offset_int moff = mem_ref_offset (base);
811 tree mofft = wide_int_to_tree (sizetype, moff);
812 if (!poffset)
813 poffset = mofft;
814 else
815 poffset = size_binop (PLUS_EXPR, poffset, mofft);
817 base = TREE_OPERAND (base, 0);
819 else
820 base = build_fold_addr_expr (base);
822 if (in_loop)
824 if (!simple_iv (loop, loop_containing_stmt (stmt), base, &base_iv,
825 nest ? true : false))
827 if (nest)
829 if (dump_file && (dump_flags & TDF_DETAILS))
830 fprintf (dump_file, "failed: evolution of base is not"
831 " affine.\n");
832 return false;
834 else
836 base_iv.base = base;
837 base_iv.step = ssize_int (0);
838 base_iv.no_overflow = true;
842 else
844 base_iv.base = base;
845 base_iv.step = ssize_int (0);
846 base_iv.no_overflow = true;
849 if (!poffset)
851 offset_iv.base = ssize_int (0);
852 offset_iv.step = ssize_int (0);
854 else
856 if (!in_loop)
858 offset_iv.base = poffset;
859 offset_iv.step = ssize_int (0);
861 else if (!simple_iv (loop, loop_containing_stmt (stmt),
862 poffset, &offset_iv,
863 nest ? true : false))
865 if (nest)
867 if (dump_file && (dump_flags & TDF_DETAILS))
868 fprintf (dump_file, "failed: evolution of offset is not"
869 " affine.\n");
870 return false;
872 else
874 offset_iv.base = poffset;
875 offset_iv.step = ssize_int (0);
880 init = ssize_int (pbitpos / BITS_PER_UNIT);
881 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
882 init = size_binop (PLUS_EXPR, init, dinit);
883 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
884 init = size_binop (PLUS_EXPR, init, dinit);
886 step = size_binop (PLUS_EXPR,
887 fold_convert (ssizetype, base_iv.step),
888 fold_convert (ssizetype, offset_iv.step));
890 DR_BASE_ADDRESS (dr) = canonicalize_base_object_address (base_iv.base);
892 DR_OFFSET (dr) = fold_convert (ssizetype, offset_iv.base);
893 DR_INIT (dr) = init;
894 DR_STEP (dr) = step;
896 DR_ALIGNED_TO (dr) = size_int (highest_pow2_factor (offset_iv.base));
898 if (dump_file && (dump_flags & TDF_DETAILS))
899 fprintf (dump_file, "success.\n");
901 return true;
904 /* Determines the base object and the list of indices of memory reference
905 DR, analyzed in LOOP and instantiated in loop nest NEST. */
907 static void
908 dr_analyze_indices (struct data_reference *dr, loop_p nest, loop_p loop)
910 vec<tree> access_fns = vNULL;
911 tree ref, op;
912 tree base, off, access_fn;
913 basic_block before_loop;
915 /* If analyzing a basic-block there are no indices to analyze
916 and thus no access functions. */
917 if (!nest)
919 DR_BASE_OBJECT (dr) = DR_REF (dr);
920 DR_ACCESS_FNS (dr).create (0);
921 return;
924 ref = DR_REF (dr);
925 before_loop = block_before_loop (nest);
927 /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
928 into a two element array with a constant index. The base is
929 then just the immediate underlying object. */
930 if (TREE_CODE (ref) == REALPART_EXPR)
932 ref = TREE_OPERAND (ref, 0);
933 access_fns.safe_push (integer_zero_node);
935 else if (TREE_CODE (ref) == IMAGPART_EXPR)
937 ref = TREE_OPERAND (ref, 0);
938 access_fns.safe_push (integer_one_node);
941 /* Analyze access functions of dimensions we know to be independent. */
942 while (handled_component_p (ref))
944 if (TREE_CODE (ref) == ARRAY_REF)
946 op = TREE_OPERAND (ref, 1);
947 access_fn = analyze_scalar_evolution (loop, op);
948 access_fn = instantiate_scev (before_loop, loop, access_fn);
949 access_fns.safe_push (access_fn);
951 else if (TREE_CODE (ref) == COMPONENT_REF
952 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
954 /* For COMPONENT_REFs of records (but not unions!) use the
955 FIELD_DECL offset as constant access function so we can
956 disambiguate a[i].f1 and a[i].f2. */
957 tree off = component_ref_field_offset (ref);
958 off = size_binop (PLUS_EXPR,
959 size_binop (MULT_EXPR,
960 fold_convert (bitsizetype, off),
961 bitsize_int (BITS_PER_UNIT)),
962 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
963 access_fns.safe_push (off);
965 else
966 /* If we have an unhandled component we could not translate
967 to an access function stop analyzing. We have determined
968 our base object in this case. */
969 break;
971 ref = TREE_OPERAND (ref, 0);
974 /* If the address operand of a MEM_REF base has an evolution in the
975 analyzed nest, add it as an additional independent access-function. */
976 if (TREE_CODE (ref) == MEM_REF)
978 op = TREE_OPERAND (ref, 0);
979 access_fn = analyze_scalar_evolution (loop, op);
980 access_fn = instantiate_scev (before_loop, loop, access_fn);
981 if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
983 tree orig_type;
984 tree memoff = TREE_OPERAND (ref, 1);
985 base = initial_condition (access_fn);
986 orig_type = TREE_TYPE (base);
987 STRIP_USELESS_TYPE_CONVERSION (base);
988 split_constant_offset (base, &base, &off);
989 STRIP_USELESS_TYPE_CONVERSION (base);
990 /* Fold the MEM_REF offset into the evolutions initial
991 value to make more bases comparable. */
992 if (!integer_zerop (memoff))
994 off = size_binop (PLUS_EXPR, off,
995 fold_convert (ssizetype, memoff));
996 memoff = build_int_cst (TREE_TYPE (memoff), 0);
998 /* Adjust the offset so it is a multiple of the access type
999 size and thus we separate bases that can possibly be used
1000 to produce partial overlaps (which the access_fn machinery
1001 cannot handle). */
1002 wide_int rem;
1003 if (TYPE_SIZE_UNIT (TREE_TYPE (ref))
1004 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref))) == INTEGER_CST
1005 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (ref))))
1006 rem = wi::mod_trunc (off, TYPE_SIZE_UNIT (TREE_TYPE (ref)), SIGNED);
1007 else
1008 /* If we can't compute the remainder simply force the initial
1009 condition to zero. */
1010 rem = off;
1011 off = wide_int_to_tree (ssizetype, wi::sub (off, rem));
1012 memoff = wide_int_to_tree (TREE_TYPE (memoff), rem);
1013 /* And finally replace the initial condition. */
1014 access_fn = chrec_replace_initial_condition
1015 (access_fn, fold_convert (orig_type, off));
1016 /* ??? This is still not a suitable base object for
1017 dr_may_alias_p - the base object needs to be an
1018 access that covers the object as whole. With
1019 an evolution in the pointer this cannot be
1020 guaranteed.
1021 As a band-aid, mark the access so we can special-case
1022 it in dr_may_alias_p. */
1023 tree old = ref;
1024 ref = fold_build2_loc (EXPR_LOCATION (ref),
1025 MEM_REF, TREE_TYPE (ref),
1026 base, memoff);
1027 MR_DEPENDENCE_CLIQUE (ref) = MR_DEPENDENCE_CLIQUE (old);
1028 MR_DEPENDENCE_BASE (ref) = MR_DEPENDENCE_BASE (old);
1029 DR_UNCONSTRAINED_BASE (dr) = true;
1030 access_fns.safe_push (access_fn);
1033 else if (DECL_P (ref))
1035 /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
1036 ref = build2 (MEM_REF, TREE_TYPE (ref),
1037 build_fold_addr_expr (ref),
1038 build_int_cst (reference_alias_ptr_type (ref), 0));
1041 DR_BASE_OBJECT (dr) = ref;
1042 DR_ACCESS_FNS (dr) = access_fns;
1045 /* Extracts the alias analysis information from the memory reference DR. */
1047 static void
1048 dr_analyze_alias (struct data_reference *dr)
1050 tree ref = DR_REF (dr);
1051 tree base = get_base_address (ref), addr;
1053 if (INDIRECT_REF_P (base)
1054 || TREE_CODE (base) == MEM_REF)
1056 addr = TREE_OPERAND (base, 0);
1057 if (TREE_CODE (addr) == SSA_NAME)
1058 DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
1062 /* Frees data reference DR. */
1064 void
1065 free_data_ref (data_reference_p dr)
1067 DR_ACCESS_FNS (dr).release ();
1068 free (dr);
1071 /* Analyzes memory reference MEMREF accessed in STMT. The reference
1072 is read if IS_READ is true, write otherwise. Returns the
1073 data_reference description of MEMREF. NEST is the outermost loop
1074 in which the reference should be instantiated, LOOP is the loop in
1075 which the data reference should be analyzed. */
1077 struct data_reference *
1078 create_data_ref (loop_p nest, loop_p loop, tree memref, gimple stmt,
1079 bool is_read)
1081 struct data_reference *dr;
1083 if (dump_file && (dump_flags & TDF_DETAILS))
1085 fprintf (dump_file, "Creating dr for ");
1086 print_generic_expr (dump_file, memref, TDF_SLIM);
1087 fprintf (dump_file, "\n");
1090 dr = XCNEW (struct data_reference);
1091 DR_STMT (dr) = stmt;
1092 DR_REF (dr) = memref;
1093 DR_IS_READ (dr) = is_read;
1095 dr_analyze_innermost (dr, nest);
1096 dr_analyze_indices (dr, nest, loop);
1097 dr_analyze_alias (dr);
1099 if (dump_file && (dump_flags & TDF_DETAILS))
1101 unsigned i;
1102 fprintf (dump_file, "\tbase_address: ");
1103 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1104 fprintf (dump_file, "\n\toffset from base address: ");
1105 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1106 fprintf (dump_file, "\n\tconstant offset from base address: ");
1107 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1108 fprintf (dump_file, "\n\tstep: ");
1109 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1110 fprintf (dump_file, "\n\taligned to: ");
1111 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
1112 fprintf (dump_file, "\n\tbase_object: ");
1113 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1114 fprintf (dump_file, "\n");
1115 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
1117 fprintf (dump_file, "\tAccess function %d: ", i);
1118 print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
1122 return dr;
1125 /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
1126 expressions. */
1127 static bool
1128 dr_equal_offsets_p1 (tree offset1, tree offset2)
1130 bool res;
1132 STRIP_NOPS (offset1);
1133 STRIP_NOPS (offset2);
1135 if (offset1 == offset2)
1136 return true;
1138 if (TREE_CODE (offset1) != TREE_CODE (offset2)
1139 || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
1140 return false;
1142 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
1143 TREE_OPERAND (offset2, 0));
1145 if (!res || !BINARY_CLASS_P (offset1))
1146 return res;
1148 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
1149 TREE_OPERAND (offset2, 1));
1151 return res;
1154 /* Check if DRA and DRB have equal offsets. */
1155 bool
1156 dr_equal_offsets_p (struct data_reference *dra,
1157 struct data_reference *drb)
1159 tree offset1, offset2;
1161 offset1 = DR_OFFSET (dra);
1162 offset2 = DR_OFFSET (drb);
1164 return dr_equal_offsets_p1 (offset1, offset2);
1167 /* Returns true if FNA == FNB. */
1169 static bool
1170 affine_function_equal_p (affine_fn fna, affine_fn fnb)
1172 unsigned i, n = fna.length ();
1174 if (n != fnb.length ())
1175 return false;
1177 for (i = 0; i < n; i++)
1178 if (!operand_equal_p (fna[i], fnb[i], 0))
1179 return false;
1181 return true;
1184 /* If all the functions in CF are the same, returns one of them,
1185 otherwise returns NULL. */
1187 static affine_fn
1188 common_affine_function (conflict_function *cf)
1190 unsigned i;
1191 affine_fn comm;
1193 if (!CF_NONTRIVIAL_P (cf))
1194 return affine_fn ();
1196 comm = cf->fns[0];
1198 for (i = 1; i < cf->n; i++)
1199 if (!affine_function_equal_p (comm, cf->fns[i]))
1200 return affine_fn ();
1202 return comm;
1205 /* Returns the base of the affine function FN. */
1207 static tree
1208 affine_function_base (affine_fn fn)
1210 return fn[0];
1213 /* Returns true if FN is a constant. */
1215 static bool
1216 affine_function_constant_p (affine_fn fn)
1218 unsigned i;
1219 tree coef;
1221 for (i = 1; fn.iterate (i, &coef); i++)
1222 if (!integer_zerop (coef))
1223 return false;
1225 return true;
1228 /* Returns true if FN is the zero constant function. */
1230 static bool
1231 affine_function_zero_p (affine_fn fn)
1233 return (integer_zerop (affine_function_base (fn))
1234 && affine_function_constant_p (fn));
1237 /* Returns a signed integer type with the largest precision from TA
1238 and TB. */
1240 static tree
1241 signed_type_for_types (tree ta, tree tb)
1243 if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
1244 return signed_type_for (ta);
1245 else
1246 return signed_type_for (tb);
1249 /* Applies operation OP on affine functions FNA and FNB, and returns the
1250 result. */
1252 static affine_fn
1253 affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
1255 unsigned i, n, m;
1256 affine_fn ret;
1257 tree coef;
1259 if (fnb.length () > fna.length ())
1261 n = fna.length ();
1262 m = fnb.length ();
1264 else
1266 n = fnb.length ();
1267 m = fna.length ();
1270 ret.create (m);
1271 for (i = 0; i < n; i++)
1273 tree type = signed_type_for_types (TREE_TYPE (fna[i]),
1274 TREE_TYPE (fnb[i]));
1275 ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
1278 for (; fna.iterate (i, &coef); i++)
1279 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1280 coef, integer_zero_node));
1281 for (; fnb.iterate (i, &coef); i++)
1282 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1283 integer_zero_node, coef));
1285 return ret;
1288 /* Returns the sum of affine functions FNA and FNB. */
1290 static affine_fn
1291 affine_fn_plus (affine_fn fna, affine_fn fnb)
1293 return affine_fn_op (PLUS_EXPR, fna, fnb);
1296 /* Returns the difference of affine functions FNA and FNB. */
1298 static affine_fn
1299 affine_fn_minus (affine_fn fna, affine_fn fnb)
1301 return affine_fn_op (MINUS_EXPR, fna, fnb);
1304 /* Frees affine function FN. */
1306 static void
1307 affine_fn_free (affine_fn fn)
1309 fn.release ();
1312 /* Determine for each subscript in the data dependence relation DDR
1313 the distance. */
1315 static void
1316 compute_subscript_distance (struct data_dependence_relation *ddr)
1318 conflict_function *cf_a, *cf_b;
1319 affine_fn fn_a, fn_b, diff;
1321 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1323 unsigned int i;
1325 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1327 struct subscript *subscript;
1329 subscript = DDR_SUBSCRIPT (ddr, i);
1330 cf_a = SUB_CONFLICTS_IN_A (subscript);
1331 cf_b = SUB_CONFLICTS_IN_B (subscript);
1333 fn_a = common_affine_function (cf_a);
1334 fn_b = common_affine_function (cf_b);
1335 if (!fn_a.exists () || !fn_b.exists ())
1337 SUB_DISTANCE (subscript) = chrec_dont_know;
1338 return;
1340 diff = affine_fn_minus (fn_a, fn_b);
1342 if (affine_function_constant_p (diff))
1343 SUB_DISTANCE (subscript) = affine_function_base (diff);
1344 else
1345 SUB_DISTANCE (subscript) = chrec_dont_know;
1347 affine_fn_free (diff);
1352 /* Returns the conflict function for "unknown". */
1354 static conflict_function *
1355 conflict_fn_not_known (void)
1357 conflict_function *fn = XCNEW (conflict_function);
1358 fn->n = NOT_KNOWN;
1360 return fn;
1363 /* Returns the conflict function for "independent". */
1365 static conflict_function *
1366 conflict_fn_no_dependence (void)
1368 conflict_function *fn = XCNEW (conflict_function);
1369 fn->n = NO_DEPENDENCE;
1371 return fn;
1374 /* Returns true if the address of OBJ is invariant in LOOP. */
1376 static bool
1377 object_address_invariant_in_loop_p (const struct loop *loop, const_tree obj)
1379 while (handled_component_p (obj))
1381 if (TREE_CODE (obj) == ARRAY_REF)
1383 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1384 need to check the stride and the lower bound of the reference. */
1385 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1386 loop->num)
1387 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 3),
1388 loop->num))
1389 return false;
1391 else if (TREE_CODE (obj) == COMPONENT_REF)
1393 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1394 loop->num))
1395 return false;
1397 obj = TREE_OPERAND (obj, 0);
1400 if (!INDIRECT_REF_P (obj)
1401 && TREE_CODE (obj) != MEM_REF)
1402 return true;
1404 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
1405 loop->num);
1408 /* Returns false if we can prove that data references A and B do not alias,
1409 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
1410 considered. */
1412 bool
1413 dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
1414 bool loop_nest)
1416 tree addr_a = DR_BASE_OBJECT (a);
1417 tree addr_b = DR_BASE_OBJECT (b);
1419 /* If we are not processing a loop nest but scalar code we
1420 do not need to care about possible cross-iteration dependences
1421 and thus can process the full original reference. Do so,
1422 similar to how loop invariant motion applies extra offset-based
1423 disambiguation. */
1424 if (!loop_nest)
1426 aff_tree off1, off2;
1427 widest_int size1, size2;
1428 get_inner_reference_aff (DR_REF (a), &off1, &size1);
1429 get_inner_reference_aff (DR_REF (b), &off2, &size2);
1430 aff_combination_scale (&off1, -1);
1431 aff_combination_add (&off2, &off1);
1432 if (aff_comb_cannot_overlap_p (&off2, size1, size2))
1433 return false;
1436 if ((TREE_CODE (addr_a) == MEM_REF || TREE_CODE (addr_a) == TARGET_MEM_REF)
1437 && (TREE_CODE (addr_b) == MEM_REF || TREE_CODE (addr_b) == TARGET_MEM_REF)
1438 && MR_DEPENDENCE_CLIQUE (addr_a) == MR_DEPENDENCE_CLIQUE (addr_b)
1439 && MR_DEPENDENCE_BASE (addr_a) != MR_DEPENDENCE_BASE (addr_b))
1440 return false;
1442 /* If we had an evolution in a pointer-based MEM_REF BASE_OBJECT we
1443 do not know the size of the base-object. So we cannot do any
1444 offset/overlap based analysis but have to rely on points-to
1445 information only. */
1446 if (TREE_CODE (addr_a) == MEM_REF
1447 && (DR_UNCONSTRAINED_BASE (a)
1448 || TREE_CODE (TREE_OPERAND (addr_a, 0)) == SSA_NAME))
1450 /* For true dependences we can apply TBAA. */
1451 if (flag_strict_aliasing
1452 && DR_IS_WRITE (a) && DR_IS_READ (b)
1453 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1454 get_alias_set (DR_REF (b))))
1455 return false;
1456 if (TREE_CODE (addr_b) == MEM_REF)
1457 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1458 TREE_OPERAND (addr_b, 0));
1459 else
1460 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1461 build_fold_addr_expr (addr_b));
1463 else if (TREE_CODE (addr_b) == MEM_REF
1464 && (DR_UNCONSTRAINED_BASE (b)
1465 || TREE_CODE (TREE_OPERAND (addr_b, 0)) == SSA_NAME))
1467 /* For true dependences we can apply TBAA. */
1468 if (flag_strict_aliasing
1469 && DR_IS_WRITE (a) && DR_IS_READ (b)
1470 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1471 get_alias_set (DR_REF (b))))
1472 return false;
1473 if (TREE_CODE (addr_a) == MEM_REF)
1474 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1475 TREE_OPERAND (addr_b, 0));
1476 else
1477 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
1478 TREE_OPERAND (addr_b, 0));
1481 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1482 that is being subsetted in the loop nest. */
1483 if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
1484 return refs_output_dependent_p (addr_a, addr_b);
1485 else if (DR_IS_READ (a) && DR_IS_WRITE (b))
1486 return refs_anti_dependent_p (addr_a, addr_b);
1487 return refs_may_alias_p (addr_a, addr_b);
1490 /* Initialize a data dependence relation between data accesses A and
1491 B. NB_LOOPS is the number of loops surrounding the references: the
1492 size of the classic distance/direction vectors. */
1494 struct data_dependence_relation *
1495 initialize_data_dependence_relation (struct data_reference *a,
1496 struct data_reference *b,
1497 vec<loop_p> loop_nest)
1499 struct data_dependence_relation *res;
1500 unsigned int i;
1502 res = XNEW (struct data_dependence_relation);
1503 DDR_A (res) = a;
1504 DDR_B (res) = b;
1505 DDR_LOOP_NEST (res).create (0);
1506 DDR_REVERSED_P (res) = false;
1507 DDR_SUBSCRIPTS (res).create (0);
1508 DDR_DIR_VECTS (res).create (0);
1509 DDR_DIST_VECTS (res).create (0);
1511 if (a == NULL || b == NULL)
1513 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1514 return res;
1517 /* If the data references do not alias, then they are independent. */
1518 if (!dr_may_alias_p (a, b, loop_nest.exists ()))
1520 DDR_ARE_DEPENDENT (res) = chrec_known;
1521 return res;
1524 /* The case where the references are exactly the same. */
1525 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
1527 if (loop_nest.exists ()
1528 && !object_address_invariant_in_loop_p (loop_nest[0],
1529 DR_BASE_OBJECT (a)))
1531 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1532 return res;
1534 DDR_AFFINE_P (res) = true;
1535 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1536 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1537 DDR_LOOP_NEST (res) = loop_nest;
1538 DDR_INNER_LOOP (res) = 0;
1539 DDR_SELF_REFERENCE (res) = true;
1540 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1542 struct subscript *subscript;
1544 subscript = XNEW (struct subscript);
1545 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1546 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1547 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1548 SUB_DISTANCE (subscript) = chrec_dont_know;
1549 DDR_SUBSCRIPTS (res).safe_push (subscript);
1551 return res;
1554 /* If the references do not access the same object, we do not know
1555 whether they alias or not. */
1556 if (!operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0))
1558 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1559 return res;
1562 /* If the base of the object is not invariant in the loop nest, we cannot
1563 analyze it. TODO -- in fact, it would suffice to record that there may
1564 be arbitrary dependences in the loops where the base object varies. */
1565 if (loop_nest.exists ()
1566 && !object_address_invariant_in_loop_p (loop_nest[0],
1567 DR_BASE_OBJECT (a)))
1569 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1570 return res;
1573 /* If the number of dimensions of the access to not agree we can have
1574 a pointer access to a component of the array element type and an
1575 array access while the base-objects are still the same. Punt. */
1576 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1578 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1579 return res;
1582 DDR_AFFINE_P (res) = true;
1583 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1584 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1585 DDR_LOOP_NEST (res) = loop_nest;
1586 DDR_INNER_LOOP (res) = 0;
1587 DDR_SELF_REFERENCE (res) = false;
1589 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1591 struct subscript *subscript;
1593 subscript = XNEW (struct subscript);
1594 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1595 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1596 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1597 SUB_DISTANCE (subscript) = chrec_dont_know;
1598 DDR_SUBSCRIPTS (res).safe_push (subscript);
1601 return res;
1604 /* Frees memory used by the conflict function F. */
1606 static void
1607 free_conflict_function (conflict_function *f)
1609 unsigned i;
1611 if (CF_NONTRIVIAL_P (f))
1613 for (i = 0; i < f->n; i++)
1614 affine_fn_free (f->fns[i]);
1616 free (f);
1619 /* Frees memory used by SUBSCRIPTS. */
1621 static void
1622 free_subscripts (vec<subscript_p> subscripts)
1624 unsigned i;
1625 subscript_p s;
1627 FOR_EACH_VEC_ELT (subscripts, i, s)
1629 free_conflict_function (s->conflicting_iterations_in_a);
1630 free_conflict_function (s->conflicting_iterations_in_b);
1631 free (s);
1633 subscripts.release ();
1636 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1637 description. */
1639 static inline void
1640 finalize_ddr_dependent (struct data_dependence_relation *ddr,
1641 tree chrec)
1643 DDR_ARE_DEPENDENT (ddr) = chrec;
1644 free_subscripts (DDR_SUBSCRIPTS (ddr));
1645 DDR_SUBSCRIPTS (ddr).create (0);
1648 /* The dependence relation DDR cannot be represented by a distance
1649 vector. */
1651 static inline void
1652 non_affine_dependence_relation (struct data_dependence_relation *ddr)
1654 if (dump_file && (dump_flags & TDF_DETAILS))
1655 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
1657 DDR_AFFINE_P (ddr) = false;
1662 /* This section contains the classic Banerjee tests. */
1664 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1665 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1667 static inline bool
1668 ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1670 return (evolution_function_is_constant_p (chrec_a)
1671 && evolution_function_is_constant_p (chrec_b));
1674 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
1675 variable, i.e., if the SIV (Single Index Variable) test is true. */
1677 static bool
1678 siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1680 if ((evolution_function_is_constant_p (chrec_a)
1681 && evolution_function_is_univariate_p (chrec_b))
1682 || (evolution_function_is_constant_p (chrec_b)
1683 && evolution_function_is_univariate_p (chrec_a)))
1684 return true;
1686 if (evolution_function_is_univariate_p (chrec_a)
1687 && evolution_function_is_univariate_p (chrec_b))
1689 switch (TREE_CODE (chrec_a))
1691 case POLYNOMIAL_CHREC:
1692 switch (TREE_CODE (chrec_b))
1694 case POLYNOMIAL_CHREC:
1695 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
1696 return false;
1698 default:
1699 return true;
1702 default:
1703 return true;
1707 return false;
1710 /* Creates a conflict function with N dimensions. The affine functions
1711 in each dimension follow. */
1713 static conflict_function *
1714 conflict_fn (unsigned n, ...)
1716 unsigned i;
1717 conflict_function *ret = XCNEW (conflict_function);
1718 va_list ap;
1720 gcc_assert (0 < n && n <= MAX_DIM);
1721 va_start (ap, n);
1723 ret->n = n;
1724 for (i = 0; i < n; i++)
1725 ret->fns[i] = va_arg (ap, affine_fn);
1726 va_end (ap);
1728 return ret;
1731 /* Returns constant affine function with value CST. */
1733 static affine_fn
1734 affine_fn_cst (tree cst)
1736 affine_fn fn;
1737 fn.create (1);
1738 fn.quick_push (cst);
1739 return fn;
1742 /* Returns affine function with single variable, CST + COEF * x_DIM. */
1744 static affine_fn
1745 affine_fn_univar (tree cst, unsigned dim, tree coef)
1747 affine_fn fn;
1748 fn.create (dim + 1);
1749 unsigned i;
1751 gcc_assert (dim > 0);
1752 fn.quick_push (cst);
1753 for (i = 1; i < dim; i++)
1754 fn.quick_push (integer_zero_node);
1755 fn.quick_push (coef);
1756 return fn;
1759 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1760 *OVERLAPS_B are initialized to the functions that describe the
1761 relation between the elements accessed twice by CHREC_A and
1762 CHREC_B. For k >= 0, the following property is verified:
1764 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1766 static void
1767 analyze_ziv_subscript (tree chrec_a,
1768 tree chrec_b,
1769 conflict_function **overlaps_a,
1770 conflict_function **overlaps_b,
1771 tree *last_conflicts)
1773 tree type, difference;
1774 dependence_stats.num_ziv++;
1776 if (dump_file && (dump_flags & TDF_DETAILS))
1777 fprintf (dump_file, "(analyze_ziv_subscript \n");
1779 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1780 chrec_a = chrec_convert (type, chrec_a, NULL);
1781 chrec_b = chrec_convert (type, chrec_b, NULL);
1782 difference = chrec_fold_minus (type, chrec_a, chrec_b);
1784 switch (TREE_CODE (difference))
1786 case INTEGER_CST:
1787 if (integer_zerop (difference))
1789 /* The difference is equal to zero: the accessed index
1790 overlaps for each iteration in the loop. */
1791 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1792 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1793 *last_conflicts = chrec_dont_know;
1794 dependence_stats.num_ziv_dependent++;
1796 else
1798 /* The accesses do not overlap. */
1799 *overlaps_a = conflict_fn_no_dependence ();
1800 *overlaps_b = conflict_fn_no_dependence ();
1801 *last_conflicts = integer_zero_node;
1802 dependence_stats.num_ziv_independent++;
1804 break;
1806 default:
1807 /* We're not sure whether the indexes overlap. For the moment,
1808 conservatively answer "don't know". */
1809 if (dump_file && (dump_flags & TDF_DETAILS))
1810 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
1812 *overlaps_a = conflict_fn_not_known ();
1813 *overlaps_b = conflict_fn_not_known ();
1814 *last_conflicts = chrec_dont_know;
1815 dependence_stats.num_ziv_unimplemented++;
1816 break;
1819 if (dump_file && (dump_flags & TDF_DETAILS))
1820 fprintf (dump_file, ")\n");
1823 /* Similar to max_stmt_executions_int, but returns the bound as a tree,
1824 and only if it fits to the int type. If this is not the case, or the
1825 bound on the number of iterations of LOOP could not be derived, returns
1826 chrec_dont_know. */
1828 static tree
1829 max_stmt_executions_tree (struct loop *loop)
1831 widest_int nit;
1833 if (!max_stmt_executions (loop, &nit))
1834 return chrec_dont_know;
1836 if (!wi::fits_to_tree_p (nit, unsigned_type_node))
1837 return chrec_dont_know;
1839 return wide_int_to_tree (unsigned_type_node, nit);
1842 /* Determine whether the CHREC is always positive/negative. If the expression
1843 cannot be statically analyzed, return false, otherwise set the answer into
1844 VALUE. */
1846 static bool
1847 chrec_is_positive (tree chrec, bool *value)
1849 bool value0, value1, value2;
1850 tree end_value, nb_iter;
1852 switch (TREE_CODE (chrec))
1854 case POLYNOMIAL_CHREC:
1855 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
1856 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
1857 return false;
1859 /* FIXME -- overflows. */
1860 if (value0 == value1)
1862 *value = value0;
1863 return true;
1866 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1867 and the proof consists in showing that the sign never
1868 changes during the execution of the loop, from 0 to
1869 loop->nb_iterations. */
1870 if (!evolution_function_is_affine_p (chrec))
1871 return false;
1873 nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
1874 if (chrec_contains_undetermined (nb_iter))
1875 return false;
1877 #if 0
1878 /* TODO -- If the test is after the exit, we may decrease the number of
1879 iterations by one. */
1880 if (after_exit)
1881 nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
1882 #endif
1884 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
1886 if (!chrec_is_positive (end_value, &value2))
1887 return false;
1889 *value = value0;
1890 return value0 == value1;
1892 case INTEGER_CST:
1893 switch (tree_int_cst_sgn (chrec))
1895 case -1:
1896 *value = false;
1897 break;
1898 case 1:
1899 *value = true;
1900 break;
1901 default:
1902 return false;
1904 return true;
1906 default:
1907 return false;
1912 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1913 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1914 *OVERLAPS_B are initialized to the functions that describe the
1915 relation between the elements accessed twice by CHREC_A and
1916 CHREC_B. For k >= 0, the following property is verified:
1918 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1920 static void
1921 analyze_siv_subscript_cst_affine (tree chrec_a,
1922 tree chrec_b,
1923 conflict_function **overlaps_a,
1924 conflict_function **overlaps_b,
1925 tree *last_conflicts)
1927 bool value0, value1, value2;
1928 tree type, difference, tmp;
1930 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1931 chrec_a = chrec_convert (type, chrec_a, NULL);
1932 chrec_b = chrec_convert (type, chrec_b, NULL);
1933 difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
1935 /* Special case overlap in the first iteration. */
1936 if (integer_zerop (difference))
1938 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1939 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1940 *last_conflicts = integer_one_node;
1941 return;
1944 if (!chrec_is_positive (initial_condition (difference), &value0))
1946 if (dump_file && (dump_flags & TDF_DETAILS))
1947 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
1949 dependence_stats.num_siv_unimplemented++;
1950 *overlaps_a = conflict_fn_not_known ();
1951 *overlaps_b = conflict_fn_not_known ();
1952 *last_conflicts = chrec_dont_know;
1953 return;
1955 else
1957 if (value0 == false)
1959 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
1961 if (dump_file && (dump_flags & TDF_DETAILS))
1962 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1964 *overlaps_a = conflict_fn_not_known ();
1965 *overlaps_b = conflict_fn_not_known ();
1966 *last_conflicts = chrec_dont_know;
1967 dependence_stats.num_siv_unimplemented++;
1968 return;
1970 else
1972 if (value1 == true)
1974 /* Example:
1975 chrec_a = 12
1976 chrec_b = {10, +, 1}
1979 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1981 HOST_WIDE_INT numiter;
1982 struct loop *loop = get_chrec_loop (chrec_b);
1984 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1985 tmp = fold_build2 (EXACT_DIV_EXPR, type,
1986 fold_build1 (ABS_EXPR, type, difference),
1987 CHREC_RIGHT (chrec_b));
1988 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1989 *last_conflicts = integer_one_node;
1992 /* Perform weak-zero siv test to see if overlap is
1993 outside the loop bounds. */
1994 numiter = max_stmt_executions_int (loop);
1996 if (numiter >= 0
1997 && compare_tree_int (tmp, numiter) > 0)
1999 free_conflict_function (*overlaps_a);
2000 free_conflict_function (*overlaps_b);
2001 *overlaps_a = conflict_fn_no_dependence ();
2002 *overlaps_b = conflict_fn_no_dependence ();
2003 *last_conflicts = integer_zero_node;
2004 dependence_stats.num_siv_independent++;
2005 return;
2007 dependence_stats.num_siv_dependent++;
2008 return;
2011 /* When the step does not divide the difference, there are
2012 no overlaps. */
2013 else
2015 *overlaps_a = conflict_fn_no_dependence ();
2016 *overlaps_b = conflict_fn_no_dependence ();
2017 *last_conflicts = integer_zero_node;
2018 dependence_stats.num_siv_independent++;
2019 return;
2023 else
2025 /* Example:
2026 chrec_a = 12
2027 chrec_b = {10, +, -1}
2029 In this case, chrec_a will not overlap with chrec_b. */
2030 *overlaps_a = conflict_fn_no_dependence ();
2031 *overlaps_b = conflict_fn_no_dependence ();
2032 *last_conflicts = integer_zero_node;
2033 dependence_stats.num_siv_independent++;
2034 return;
2038 else
2040 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
2042 if (dump_file && (dump_flags & TDF_DETAILS))
2043 fprintf (dump_file, "siv test failed: chrec not positive.\n");
2045 *overlaps_a = conflict_fn_not_known ();
2046 *overlaps_b = conflict_fn_not_known ();
2047 *last_conflicts = chrec_dont_know;
2048 dependence_stats.num_siv_unimplemented++;
2049 return;
2051 else
2053 if (value2 == false)
2055 /* Example:
2056 chrec_a = 3
2057 chrec_b = {10, +, -1}
2059 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2061 HOST_WIDE_INT numiter;
2062 struct loop *loop = get_chrec_loop (chrec_b);
2064 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2065 tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
2066 CHREC_RIGHT (chrec_b));
2067 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
2068 *last_conflicts = integer_one_node;
2070 /* Perform weak-zero siv test to see if overlap is
2071 outside the loop bounds. */
2072 numiter = max_stmt_executions_int (loop);
2074 if (numiter >= 0
2075 && compare_tree_int (tmp, numiter) > 0)
2077 free_conflict_function (*overlaps_a);
2078 free_conflict_function (*overlaps_b);
2079 *overlaps_a = conflict_fn_no_dependence ();
2080 *overlaps_b = conflict_fn_no_dependence ();
2081 *last_conflicts = integer_zero_node;
2082 dependence_stats.num_siv_independent++;
2083 return;
2085 dependence_stats.num_siv_dependent++;
2086 return;
2089 /* When the step does not divide the difference, there
2090 are no overlaps. */
2091 else
2093 *overlaps_a = conflict_fn_no_dependence ();
2094 *overlaps_b = conflict_fn_no_dependence ();
2095 *last_conflicts = integer_zero_node;
2096 dependence_stats.num_siv_independent++;
2097 return;
2100 else
2102 /* Example:
2103 chrec_a = 3
2104 chrec_b = {4, +, 1}
2106 In this case, chrec_a will not overlap with chrec_b. */
2107 *overlaps_a = conflict_fn_no_dependence ();
2108 *overlaps_b = conflict_fn_no_dependence ();
2109 *last_conflicts = integer_zero_node;
2110 dependence_stats.num_siv_independent++;
2111 return;
2118 /* Helper recursive function for initializing the matrix A. Returns
2119 the initial value of CHREC. */
2121 static tree
2122 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2124 gcc_assert (chrec);
2126 switch (TREE_CODE (chrec))
2128 case POLYNOMIAL_CHREC:
2129 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST);
2131 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2132 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2134 case PLUS_EXPR:
2135 case MULT_EXPR:
2136 case MINUS_EXPR:
2138 tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2139 tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
2141 return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
2144 CASE_CONVERT:
2146 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2147 return chrec_convert (chrec_type (chrec), op, NULL);
2150 case BIT_NOT_EXPR:
2152 /* Handle ~X as -1 - X. */
2153 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2154 return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
2155 build_int_cst (TREE_TYPE (chrec), -1), op);
2158 case INTEGER_CST:
2159 return chrec;
2161 default:
2162 gcc_unreachable ();
2163 return NULL_TREE;
2167 #define FLOOR_DIV(x,y) ((x) / (y))
2169 /* Solves the special case of the Diophantine equation:
2170 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2172 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2173 number of iterations that loops X and Y run. The overlaps will be
2174 constructed as evolutions in dimension DIM. */
2176 static void
2177 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2178 affine_fn *overlaps_a,
2179 affine_fn *overlaps_b,
2180 tree *last_conflicts, int dim)
2182 if (((step_a > 0 && step_b > 0)
2183 || (step_a < 0 && step_b < 0)))
2185 int step_overlaps_a, step_overlaps_b;
2186 int gcd_steps_a_b, last_conflict, tau2;
2188 gcd_steps_a_b = gcd (step_a, step_b);
2189 step_overlaps_a = step_b / gcd_steps_a_b;
2190 step_overlaps_b = step_a / gcd_steps_a_b;
2192 if (niter > 0)
2194 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2195 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2196 last_conflict = tau2;
2197 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2199 else
2200 *last_conflicts = chrec_dont_know;
2202 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
2203 build_int_cst (NULL_TREE,
2204 step_overlaps_a));
2205 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
2206 build_int_cst (NULL_TREE,
2207 step_overlaps_b));
2210 else
2212 *overlaps_a = affine_fn_cst (integer_zero_node);
2213 *overlaps_b = affine_fn_cst (integer_zero_node);
2214 *last_conflicts = integer_zero_node;
2218 /* Solves the special case of a Diophantine equation where CHREC_A is
2219 an affine bivariate function, and CHREC_B is an affine univariate
2220 function. For example,
2222 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2224 has the following overlapping functions:
2226 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2227 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2228 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2230 FORNOW: This is a specialized implementation for a case occurring in
2231 a common benchmark. Implement the general algorithm. */
2233 static void
2234 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2235 conflict_function **overlaps_a,
2236 conflict_function **overlaps_b,
2237 tree *last_conflicts)
2239 bool xz_p, yz_p, xyz_p;
2240 int step_x, step_y, step_z;
2241 HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
2242 affine_fn overlaps_a_xz, overlaps_b_xz;
2243 affine_fn overlaps_a_yz, overlaps_b_yz;
2244 affine_fn overlaps_a_xyz, overlaps_b_xyz;
2245 affine_fn ova1, ova2, ovb;
2246 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
2248 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2249 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2250 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2252 niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
2253 niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
2254 niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
2256 if (niter_x < 0 || niter_y < 0 || niter_z < 0)
2258 if (dump_file && (dump_flags & TDF_DETAILS))
2259 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
2261 *overlaps_a = conflict_fn_not_known ();
2262 *overlaps_b = conflict_fn_not_known ();
2263 *last_conflicts = chrec_dont_know;
2264 return;
2267 niter = MIN (niter_x, niter_z);
2268 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2269 &overlaps_a_xz,
2270 &overlaps_b_xz,
2271 &last_conflicts_xz, 1);
2272 niter = MIN (niter_y, niter_z);
2273 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2274 &overlaps_a_yz,
2275 &overlaps_b_yz,
2276 &last_conflicts_yz, 2);
2277 niter = MIN (niter_x, niter_z);
2278 niter = MIN (niter_y, niter);
2279 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2280 &overlaps_a_xyz,
2281 &overlaps_b_xyz,
2282 &last_conflicts_xyz, 3);
2284 xz_p = !integer_zerop (last_conflicts_xz);
2285 yz_p = !integer_zerop (last_conflicts_yz);
2286 xyz_p = !integer_zerop (last_conflicts_xyz);
2288 if (xz_p || yz_p || xyz_p)
2290 ova1 = affine_fn_cst (integer_zero_node);
2291 ova2 = affine_fn_cst (integer_zero_node);
2292 ovb = affine_fn_cst (integer_zero_node);
2293 if (xz_p)
2295 affine_fn t0 = ova1;
2296 affine_fn t2 = ovb;
2298 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
2299 ovb = affine_fn_plus (ovb, overlaps_b_xz);
2300 affine_fn_free (t0);
2301 affine_fn_free (t2);
2302 *last_conflicts = last_conflicts_xz;
2304 if (yz_p)
2306 affine_fn t0 = ova2;
2307 affine_fn t2 = ovb;
2309 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
2310 ovb = affine_fn_plus (ovb, overlaps_b_yz);
2311 affine_fn_free (t0);
2312 affine_fn_free (t2);
2313 *last_conflicts = last_conflicts_yz;
2315 if (xyz_p)
2317 affine_fn t0 = ova1;
2318 affine_fn t2 = ova2;
2319 affine_fn t4 = ovb;
2321 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
2322 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
2323 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
2324 affine_fn_free (t0);
2325 affine_fn_free (t2);
2326 affine_fn_free (t4);
2327 *last_conflicts = last_conflicts_xyz;
2329 *overlaps_a = conflict_fn (2, ova1, ova2);
2330 *overlaps_b = conflict_fn (1, ovb);
2332 else
2334 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2335 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2336 *last_conflicts = integer_zero_node;
2339 affine_fn_free (overlaps_a_xz);
2340 affine_fn_free (overlaps_b_xz);
2341 affine_fn_free (overlaps_a_yz);
2342 affine_fn_free (overlaps_b_yz);
2343 affine_fn_free (overlaps_a_xyz);
2344 affine_fn_free (overlaps_b_xyz);
2347 /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2349 static void
2350 lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
2351 int size)
2353 memcpy (vec2, vec1, size * sizeof (*vec1));
2356 /* Copy the elements of M x N matrix MAT1 to MAT2. */
2358 static void
2359 lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
2360 int m, int n)
2362 int i;
2364 for (i = 0; i < m; i++)
2365 lambda_vector_copy (mat1[i], mat2[i], n);
2368 /* Store the N x N identity matrix in MAT. */
2370 static void
2371 lambda_matrix_id (lambda_matrix mat, int size)
2373 int i, j;
2375 for (i = 0; i < size; i++)
2376 for (j = 0; j < size; j++)
2377 mat[i][j] = (i == j) ? 1 : 0;
2380 /* Return the first nonzero element of vector VEC1 between START and N.
2381 We must have START <= N. Returns N if VEC1 is the zero vector. */
2383 static int
2384 lambda_vector_first_nz (lambda_vector vec1, int n, int start)
2386 int j = start;
2387 while (j < n && vec1[j] == 0)
2388 j++;
2389 return j;
2392 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2393 R2 = R2 + CONST1 * R1. */
2395 static void
2396 lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2, int const1)
2398 int i;
2400 if (const1 == 0)
2401 return;
2403 for (i = 0; i < n; i++)
2404 mat[r2][i] += const1 * mat[r1][i];
2407 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
2408 and store the result in VEC2. */
2410 static void
2411 lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
2412 int size, int const1)
2414 int i;
2416 if (const1 == 0)
2417 lambda_vector_clear (vec2, size);
2418 else
2419 for (i = 0; i < size; i++)
2420 vec2[i] = const1 * vec1[i];
2423 /* Negate vector VEC1 with length SIZE and store it in VEC2. */
2425 static void
2426 lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
2427 int size)
2429 lambda_vector_mult_const (vec1, vec2, size, -1);
2432 /* Negate row R1 of matrix MAT which has N columns. */
2434 static void
2435 lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
2437 lambda_vector_negate (mat[r1], mat[r1], n);
2440 /* Return true if two vectors are equal. */
2442 static bool
2443 lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
2445 int i;
2446 for (i = 0; i < size; i++)
2447 if (vec1[i] != vec2[i])
2448 return false;
2449 return true;
2452 /* Given an M x N integer matrix A, this function determines an M x
2453 M unimodular matrix U, and an M x N echelon matrix S such that
2454 "U.A = S". This decomposition is also known as "right Hermite".
2456 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2457 Restructuring Compilers" Utpal Banerjee. */
2459 static void
2460 lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
2461 lambda_matrix S, lambda_matrix U)
2463 int i, j, i0 = 0;
2465 lambda_matrix_copy (A, S, m, n);
2466 lambda_matrix_id (U, m);
2468 for (j = 0; j < n; j++)
2470 if (lambda_vector_first_nz (S[j], m, i0) < m)
2472 ++i0;
2473 for (i = m - 1; i >= i0; i--)
2475 while (S[i][j] != 0)
2477 int sigma, factor, a, b;
2479 a = S[i-1][j];
2480 b = S[i][j];
2481 sigma = (a * b < 0) ? -1: 1;
2482 a = abs (a);
2483 b = abs (b);
2484 factor = sigma * (a / b);
2486 lambda_matrix_row_add (S, n, i, i-1, -factor);
2487 std::swap (S[i], S[i-1]);
2489 lambda_matrix_row_add (U, m, i, i-1, -factor);
2490 std::swap (U[i], U[i-1]);
2497 /* Determines the overlapping elements due to accesses CHREC_A and
2498 CHREC_B, that are affine functions. This function cannot handle
2499 symbolic evolution functions, ie. when initial conditions are
2500 parameters, because it uses lambda matrices of integers. */
2502 static void
2503 analyze_subscript_affine_affine (tree chrec_a,
2504 tree chrec_b,
2505 conflict_function **overlaps_a,
2506 conflict_function **overlaps_b,
2507 tree *last_conflicts)
2509 unsigned nb_vars_a, nb_vars_b, dim;
2510 HOST_WIDE_INT init_a, init_b, gamma, gcd_alpha_beta;
2511 lambda_matrix A, U, S;
2512 struct obstack scratch_obstack;
2514 if (eq_evolutions_p (chrec_a, chrec_b))
2516 /* The accessed index overlaps for each iteration in the
2517 loop. */
2518 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2519 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2520 *last_conflicts = chrec_dont_know;
2521 return;
2523 if (dump_file && (dump_flags & TDF_DETAILS))
2524 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2526 /* For determining the initial intersection, we have to solve a
2527 Diophantine equation. This is the most time consuming part.
2529 For answering to the question: "Is there a dependence?" we have
2530 to prove that there exists a solution to the Diophantine
2531 equation, and that the solution is in the iteration domain,
2532 i.e. the solution is positive or zero, and that the solution
2533 happens before the upper bound loop.nb_iterations. Otherwise
2534 there is no dependence. This function outputs a description of
2535 the iterations that hold the intersections. */
2537 nb_vars_a = nb_vars_in_chrec (chrec_a);
2538 nb_vars_b = nb_vars_in_chrec (chrec_b);
2540 gcc_obstack_init (&scratch_obstack);
2542 dim = nb_vars_a + nb_vars_b;
2543 U = lambda_matrix_new (dim, dim, &scratch_obstack);
2544 A = lambda_matrix_new (dim, 1, &scratch_obstack);
2545 S = lambda_matrix_new (dim, 1, &scratch_obstack);
2547 init_a = int_cst_value (initialize_matrix_A (A, chrec_a, 0, 1));
2548 init_b = int_cst_value (initialize_matrix_A (A, chrec_b, nb_vars_a, -1));
2549 gamma = init_b - init_a;
2551 /* Don't do all the hard work of solving the Diophantine equation
2552 when we already know the solution: for example,
2553 | {3, +, 1}_1
2554 | {3, +, 4}_2
2555 | gamma = 3 - 3 = 0.
2556 Then the first overlap occurs during the first iterations:
2557 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2559 if (gamma == 0)
2561 if (nb_vars_a == 1 && nb_vars_b == 1)
2563 HOST_WIDE_INT step_a, step_b;
2564 HOST_WIDE_INT niter, niter_a, niter_b;
2565 affine_fn ova, ovb;
2567 niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
2568 niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
2569 niter = MIN (niter_a, niter_b);
2570 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2571 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2573 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2574 &ova, &ovb,
2575 last_conflicts, 1);
2576 *overlaps_a = conflict_fn (1, ova);
2577 *overlaps_b = conflict_fn (1, ovb);
2580 else if (nb_vars_a == 2 && nb_vars_b == 1)
2581 compute_overlap_steps_for_affine_1_2
2582 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2584 else if (nb_vars_a == 1 && nb_vars_b == 2)
2585 compute_overlap_steps_for_affine_1_2
2586 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2588 else
2590 if (dump_file && (dump_flags & TDF_DETAILS))
2591 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
2592 *overlaps_a = conflict_fn_not_known ();
2593 *overlaps_b = conflict_fn_not_known ();
2594 *last_conflicts = chrec_dont_know;
2596 goto end_analyze_subs_aa;
2599 /* U.A = S */
2600 lambda_matrix_right_hermite (A, dim, 1, S, U);
2602 if (S[0][0] < 0)
2604 S[0][0] *= -1;
2605 lambda_matrix_row_negate (U, dim, 0);
2607 gcd_alpha_beta = S[0][0];
2609 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2610 but that is a quite strange case. Instead of ICEing, answer
2611 don't know. */
2612 if (gcd_alpha_beta == 0)
2614 *overlaps_a = conflict_fn_not_known ();
2615 *overlaps_b = conflict_fn_not_known ();
2616 *last_conflicts = chrec_dont_know;
2617 goto end_analyze_subs_aa;
2620 /* The classic "gcd-test". */
2621 if (!int_divides_p (gcd_alpha_beta, gamma))
2623 /* The "gcd-test" has determined that there is no integer
2624 solution, i.e. there is no dependence. */
2625 *overlaps_a = conflict_fn_no_dependence ();
2626 *overlaps_b = conflict_fn_no_dependence ();
2627 *last_conflicts = integer_zero_node;
2630 /* Both access functions are univariate. This includes SIV and MIV cases. */
2631 else if (nb_vars_a == 1 && nb_vars_b == 1)
2633 /* Both functions should have the same evolution sign. */
2634 if (((A[0][0] > 0 && -A[1][0] > 0)
2635 || (A[0][0] < 0 && -A[1][0] < 0)))
2637 /* The solutions are given by:
2639 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2640 | [u21 u22] [y0]
2642 For a given integer t. Using the following variables,
2644 | i0 = u11 * gamma / gcd_alpha_beta
2645 | j0 = u12 * gamma / gcd_alpha_beta
2646 | i1 = u21
2647 | j1 = u22
2649 the solutions are:
2651 | x0 = i0 + i1 * t,
2652 | y0 = j0 + j1 * t. */
2653 HOST_WIDE_INT i0, j0, i1, j1;
2655 i0 = U[0][0] * gamma / gcd_alpha_beta;
2656 j0 = U[0][1] * gamma / gcd_alpha_beta;
2657 i1 = U[1][0];
2658 j1 = U[1][1];
2660 if ((i1 == 0 && i0 < 0)
2661 || (j1 == 0 && j0 < 0))
2663 /* There is no solution.
2664 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2665 falls in here, but for the moment we don't look at the
2666 upper bound of the iteration domain. */
2667 *overlaps_a = conflict_fn_no_dependence ();
2668 *overlaps_b = conflict_fn_no_dependence ();
2669 *last_conflicts = integer_zero_node;
2670 goto end_analyze_subs_aa;
2673 if (i1 > 0 && j1 > 0)
2675 HOST_WIDE_INT niter_a
2676 = max_stmt_executions_int (get_chrec_loop (chrec_a));
2677 HOST_WIDE_INT niter_b
2678 = max_stmt_executions_int (get_chrec_loop (chrec_b));
2679 HOST_WIDE_INT niter = MIN (niter_a, niter_b);
2681 /* (X0, Y0) is a solution of the Diophantine equation:
2682 "chrec_a (X0) = chrec_b (Y0)". */
2683 HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
2684 CEIL (-j0, j1));
2685 HOST_WIDE_INT x0 = i1 * tau1 + i0;
2686 HOST_WIDE_INT y0 = j1 * tau1 + j0;
2688 /* (X1, Y1) is the smallest positive solution of the eq
2689 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2690 first conflict occurs. */
2691 HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
2692 HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
2693 HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
2695 if (niter > 0)
2697 HOST_WIDE_INT tau2 = MIN (FLOOR_DIV (niter - i0, i1),
2698 FLOOR_DIV (niter - j0, j1));
2699 HOST_WIDE_INT last_conflict = tau2 - (x1 - i0)/i1;
2701 /* If the overlap occurs outside of the bounds of the
2702 loop, there is no dependence. */
2703 if (x1 >= niter || y1 >= niter)
2705 *overlaps_a = conflict_fn_no_dependence ();
2706 *overlaps_b = conflict_fn_no_dependence ();
2707 *last_conflicts = integer_zero_node;
2708 goto end_analyze_subs_aa;
2710 else
2711 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2713 else
2714 *last_conflicts = chrec_dont_know;
2716 *overlaps_a
2717 = conflict_fn (1,
2718 affine_fn_univar (build_int_cst (NULL_TREE, x1),
2720 build_int_cst (NULL_TREE, i1)));
2721 *overlaps_b
2722 = conflict_fn (1,
2723 affine_fn_univar (build_int_cst (NULL_TREE, y1),
2725 build_int_cst (NULL_TREE, j1)));
2727 else
2729 /* FIXME: For the moment, the upper bound of the
2730 iteration domain for i and j is not checked. */
2731 if (dump_file && (dump_flags & TDF_DETAILS))
2732 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2733 *overlaps_a = conflict_fn_not_known ();
2734 *overlaps_b = conflict_fn_not_known ();
2735 *last_conflicts = chrec_dont_know;
2738 else
2740 if (dump_file && (dump_flags & TDF_DETAILS))
2741 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2742 *overlaps_a = conflict_fn_not_known ();
2743 *overlaps_b = conflict_fn_not_known ();
2744 *last_conflicts = chrec_dont_know;
2747 else
2749 if (dump_file && (dump_flags & TDF_DETAILS))
2750 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2751 *overlaps_a = conflict_fn_not_known ();
2752 *overlaps_b = conflict_fn_not_known ();
2753 *last_conflicts = chrec_dont_know;
2756 end_analyze_subs_aa:
2757 obstack_free (&scratch_obstack, NULL);
2758 if (dump_file && (dump_flags & TDF_DETAILS))
2760 fprintf (dump_file, " (overlaps_a = ");
2761 dump_conflict_function (dump_file, *overlaps_a);
2762 fprintf (dump_file, ")\n (overlaps_b = ");
2763 dump_conflict_function (dump_file, *overlaps_b);
2764 fprintf (dump_file, "))\n");
2768 /* Returns true when analyze_subscript_affine_affine can be used for
2769 determining the dependence relation between chrec_a and chrec_b,
2770 that contain symbols. This function modifies chrec_a and chrec_b
2771 such that the analysis result is the same, and such that they don't
2772 contain symbols, and then can safely be passed to the analyzer.
2774 Example: The analysis of the following tuples of evolutions produce
2775 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2776 vs. {0, +, 1}_1
2778 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2779 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2782 static bool
2783 can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
2785 tree diff, type, left_a, left_b, right_b;
2787 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
2788 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
2789 /* FIXME: For the moment not handled. Might be refined later. */
2790 return false;
2792 type = chrec_type (*chrec_a);
2793 left_a = CHREC_LEFT (*chrec_a);
2794 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
2795 diff = chrec_fold_minus (type, left_a, left_b);
2797 if (!evolution_function_is_constant_p (diff))
2798 return false;
2800 if (dump_file && (dump_flags & TDF_DETAILS))
2801 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
2803 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
2804 diff, CHREC_RIGHT (*chrec_a));
2805 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
2806 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
2807 build_int_cst (type, 0),
2808 right_b);
2809 return true;
2812 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2813 *OVERLAPS_B are initialized to the functions that describe the
2814 relation between the elements accessed twice by CHREC_A and
2815 CHREC_B. For k >= 0, the following property is verified:
2817 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2819 static void
2820 analyze_siv_subscript (tree chrec_a,
2821 tree chrec_b,
2822 conflict_function **overlaps_a,
2823 conflict_function **overlaps_b,
2824 tree *last_conflicts,
2825 int loop_nest_num)
2827 dependence_stats.num_siv++;
2829 if (dump_file && (dump_flags & TDF_DETAILS))
2830 fprintf (dump_file, "(analyze_siv_subscript \n");
2832 if (evolution_function_is_constant_p (chrec_a)
2833 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2834 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
2835 overlaps_a, overlaps_b, last_conflicts);
2837 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2838 && evolution_function_is_constant_p (chrec_b))
2839 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
2840 overlaps_b, overlaps_a, last_conflicts);
2842 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2843 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2845 if (!chrec_contains_symbols (chrec_a)
2846 && !chrec_contains_symbols (chrec_b))
2848 analyze_subscript_affine_affine (chrec_a, chrec_b,
2849 overlaps_a, overlaps_b,
2850 last_conflicts);
2852 if (CF_NOT_KNOWN_P (*overlaps_a)
2853 || CF_NOT_KNOWN_P (*overlaps_b))
2854 dependence_stats.num_siv_unimplemented++;
2855 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2856 || CF_NO_DEPENDENCE_P (*overlaps_b))
2857 dependence_stats.num_siv_independent++;
2858 else
2859 dependence_stats.num_siv_dependent++;
2861 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
2862 &chrec_b))
2864 analyze_subscript_affine_affine (chrec_a, chrec_b,
2865 overlaps_a, overlaps_b,
2866 last_conflicts);
2868 if (CF_NOT_KNOWN_P (*overlaps_a)
2869 || CF_NOT_KNOWN_P (*overlaps_b))
2870 dependence_stats.num_siv_unimplemented++;
2871 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2872 || CF_NO_DEPENDENCE_P (*overlaps_b))
2873 dependence_stats.num_siv_independent++;
2874 else
2875 dependence_stats.num_siv_dependent++;
2877 else
2878 goto siv_subscript_dontknow;
2881 else
2883 siv_subscript_dontknow:;
2884 if (dump_file && (dump_flags & TDF_DETAILS))
2885 fprintf (dump_file, " siv test failed: unimplemented");
2886 *overlaps_a = conflict_fn_not_known ();
2887 *overlaps_b = conflict_fn_not_known ();
2888 *last_conflicts = chrec_dont_know;
2889 dependence_stats.num_siv_unimplemented++;
2892 if (dump_file && (dump_flags & TDF_DETAILS))
2893 fprintf (dump_file, ")\n");
2896 /* Returns false if we can prove that the greatest common divisor of the steps
2897 of CHREC does not divide CST, false otherwise. */
2899 static bool
2900 gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
2902 HOST_WIDE_INT cd = 0, val;
2903 tree step;
2905 if (!tree_fits_shwi_p (cst))
2906 return true;
2907 val = tree_to_shwi (cst);
2909 while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
2911 step = CHREC_RIGHT (chrec);
2912 if (!tree_fits_shwi_p (step))
2913 return true;
2914 cd = gcd (cd, tree_to_shwi (step));
2915 chrec = CHREC_LEFT (chrec);
2918 return val % cd == 0;
2921 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
2922 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2923 functions that describe the relation between the elements accessed
2924 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2925 is verified:
2927 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2929 static void
2930 analyze_miv_subscript (tree chrec_a,
2931 tree chrec_b,
2932 conflict_function **overlaps_a,
2933 conflict_function **overlaps_b,
2934 tree *last_conflicts,
2935 struct loop *loop_nest)
2937 tree type, difference;
2939 dependence_stats.num_miv++;
2940 if (dump_file && (dump_flags & TDF_DETAILS))
2941 fprintf (dump_file, "(analyze_miv_subscript \n");
2943 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
2944 chrec_a = chrec_convert (type, chrec_a, NULL);
2945 chrec_b = chrec_convert (type, chrec_b, NULL);
2946 difference = chrec_fold_minus (type, chrec_a, chrec_b);
2948 if (eq_evolutions_p (chrec_a, chrec_b))
2950 /* Access functions are the same: all the elements are accessed
2951 in the same order. */
2952 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2953 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2954 *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
2955 dependence_stats.num_miv_dependent++;
2958 else if (evolution_function_is_constant_p (difference)
2959 /* For the moment, the following is verified:
2960 evolution_function_is_affine_multivariate_p (chrec_a,
2961 loop_nest->num) */
2962 && !gcd_of_steps_may_divide_p (chrec_a, difference))
2964 /* testsuite/.../ssa-chrec-33.c
2965 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2967 The difference is 1, and all the evolution steps are multiples
2968 of 2, consequently there are no overlapping elements. */
2969 *overlaps_a = conflict_fn_no_dependence ();
2970 *overlaps_b = conflict_fn_no_dependence ();
2971 *last_conflicts = integer_zero_node;
2972 dependence_stats.num_miv_independent++;
2975 else if (evolution_function_is_affine_multivariate_p (chrec_a, loop_nest->num)
2976 && !chrec_contains_symbols (chrec_a)
2977 && evolution_function_is_affine_multivariate_p (chrec_b, loop_nest->num)
2978 && !chrec_contains_symbols (chrec_b))
2980 /* testsuite/.../ssa-chrec-35.c
2981 {0, +, 1}_2 vs. {0, +, 1}_3
2982 the overlapping elements are respectively located at iterations:
2983 {0, +, 1}_x and {0, +, 1}_x,
2984 in other words, we have the equality:
2985 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2987 Other examples:
2988 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2989 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2991 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2992 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2994 analyze_subscript_affine_affine (chrec_a, chrec_b,
2995 overlaps_a, overlaps_b, last_conflicts);
2997 if (CF_NOT_KNOWN_P (*overlaps_a)
2998 || CF_NOT_KNOWN_P (*overlaps_b))
2999 dependence_stats.num_miv_unimplemented++;
3000 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
3001 || CF_NO_DEPENDENCE_P (*overlaps_b))
3002 dependence_stats.num_miv_independent++;
3003 else
3004 dependence_stats.num_miv_dependent++;
3007 else
3009 /* When the analysis is too difficult, answer "don't know". */
3010 if (dump_file && (dump_flags & TDF_DETAILS))
3011 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
3013 *overlaps_a = conflict_fn_not_known ();
3014 *overlaps_b = conflict_fn_not_known ();
3015 *last_conflicts = chrec_dont_know;
3016 dependence_stats.num_miv_unimplemented++;
3019 if (dump_file && (dump_flags & TDF_DETAILS))
3020 fprintf (dump_file, ")\n");
3023 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
3024 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
3025 OVERLAP_ITERATIONS_B are initialized with two functions that
3026 describe the iterations that contain conflicting elements.
3028 Remark: For an integer k >= 0, the following equality is true:
3030 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
3033 static void
3034 analyze_overlapping_iterations (tree chrec_a,
3035 tree chrec_b,
3036 conflict_function **overlap_iterations_a,
3037 conflict_function **overlap_iterations_b,
3038 tree *last_conflicts, struct loop *loop_nest)
3040 unsigned int lnn = loop_nest->num;
3042 dependence_stats.num_subscript_tests++;
3044 if (dump_file && (dump_flags & TDF_DETAILS))
3046 fprintf (dump_file, "(analyze_overlapping_iterations \n");
3047 fprintf (dump_file, " (chrec_a = ");
3048 print_generic_expr (dump_file, chrec_a, 0);
3049 fprintf (dump_file, ")\n (chrec_b = ");
3050 print_generic_expr (dump_file, chrec_b, 0);
3051 fprintf (dump_file, ")\n");
3054 if (chrec_a == NULL_TREE
3055 || chrec_b == NULL_TREE
3056 || chrec_contains_undetermined (chrec_a)
3057 || chrec_contains_undetermined (chrec_b))
3059 dependence_stats.num_subscript_undetermined++;
3061 *overlap_iterations_a = conflict_fn_not_known ();
3062 *overlap_iterations_b = conflict_fn_not_known ();
3065 /* If they are the same chrec, and are affine, they overlap
3066 on every iteration. */
3067 else if (eq_evolutions_p (chrec_a, chrec_b)
3068 && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3069 || operand_equal_p (chrec_a, chrec_b, 0)))
3071 dependence_stats.num_same_subscript_function++;
3072 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3073 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3074 *last_conflicts = chrec_dont_know;
3077 /* If they aren't the same, and aren't affine, we can't do anything
3078 yet. */
3079 else if ((chrec_contains_symbols (chrec_a)
3080 || chrec_contains_symbols (chrec_b))
3081 && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3082 || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
3084 dependence_stats.num_subscript_undetermined++;
3085 *overlap_iterations_a = conflict_fn_not_known ();
3086 *overlap_iterations_b = conflict_fn_not_known ();
3089 else if (ziv_subscript_p (chrec_a, chrec_b))
3090 analyze_ziv_subscript (chrec_a, chrec_b,
3091 overlap_iterations_a, overlap_iterations_b,
3092 last_conflicts);
3094 else if (siv_subscript_p (chrec_a, chrec_b))
3095 analyze_siv_subscript (chrec_a, chrec_b,
3096 overlap_iterations_a, overlap_iterations_b,
3097 last_conflicts, lnn);
3099 else
3100 analyze_miv_subscript (chrec_a, chrec_b,
3101 overlap_iterations_a, overlap_iterations_b,
3102 last_conflicts, loop_nest);
3104 if (dump_file && (dump_flags & TDF_DETAILS))
3106 fprintf (dump_file, " (overlap_iterations_a = ");
3107 dump_conflict_function (dump_file, *overlap_iterations_a);
3108 fprintf (dump_file, ")\n (overlap_iterations_b = ");
3109 dump_conflict_function (dump_file, *overlap_iterations_b);
3110 fprintf (dump_file, "))\n");
3114 /* Helper function for uniquely inserting distance vectors. */
3116 static void
3117 save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
3119 unsigned i;
3120 lambda_vector v;
3122 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, v)
3123 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
3124 return;
3126 DDR_DIST_VECTS (ddr).safe_push (dist_v);
3129 /* Helper function for uniquely inserting direction vectors. */
3131 static void
3132 save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
3134 unsigned i;
3135 lambda_vector v;
3137 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), i, v)
3138 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
3139 return;
3141 DDR_DIR_VECTS (ddr).safe_push (dir_v);
3144 /* Add a distance of 1 on all the loops outer than INDEX. If we
3145 haven't yet determined a distance for this outer loop, push a new
3146 distance vector composed of the previous distance, and a distance
3147 of 1 for this outer loop. Example:
3149 | loop_1
3150 | loop_2
3151 | A[10]
3152 | endloop_2
3153 | endloop_1
3155 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3156 save (0, 1), then we have to save (1, 0). */
3158 static void
3159 add_outer_distances (struct data_dependence_relation *ddr,
3160 lambda_vector dist_v, int index)
3162 /* For each outer loop where init_v is not set, the accesses are
3163 in dependence of distance 1 in the loop. */
3164 while (--index >= 0)
3166 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3167 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3168 save_v[index] = 1;
3169 save_dist_v (ddr, save_v);
3173 /* Return false when fail to represent the data dependence as a
3174 distance vector. INIT_B is set to true when a component has been
3175 added to the distance vector DIST_V. INDEX_CARRY is then set to
3176 the index in DIST_V that carries the dependence. */
3178 static bool
3179 build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
3180 struct data_reference *ddr_a,
3181 struct data_reference *ddr_b,
3182 lambda_vector dist_v, bool *init_b,
3183 int *index_carry)
3185 unsigned i;
3186 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3188 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3190 tree access_fn_a, access_fn_b;
3191 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3193 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3195 non_affine_dependence_relation (ddr);
3196 return false;
3199 access_fn_a = DR_ACCESS_FN (ddr_a, i);
3200 access_fn_b = DR_ACCESS_FN (ddr_b, i);
3202 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3203 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3205 int dist, index;
3206 int var_a = CHREC_VARIABLE (access_fn_a);
3207 int var_b = CHREC_VARIABLE (access_fn_b);
3209 if (var_a != var_b
3210 || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3212 non_affine_dependence_relation (ddr);
3213 return false;
3216 dist = int_cst_value (SUB_DISTANCE (subscript));
3217 index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
3218 *index_carry = MIN (index, *index_carry);
3220 /* This is the subscript coupling test. If we have already
3221 recorded a distance for this loop (a distance coming from
3222 another subscript), it should be the same. For example,
3223 in the following code, there is no dependence:
3225 | loop i = 0, N, 1
3226 | T[i+1][i] = ...
3227 | ... = T[i][i]
3228 | endloop
3230 if (init_v[index] != 0 && dist_v[index] != dist)
3232 finalize_ddr_dependent (ddr, chrec_known);
3233 return false;
3236 dist_v[index] = dist;
3237 init_v[index] = 1;
3238 *init_b = true;
3240 else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
3242 /* This can be for example an affine vs. constant dependence
3243 (T[i] vs. T[3]) that is not an affine dependence and is
3244 not representable as a distance vector. */
3245 non_affine_dependence_relation (ddr);
3246 return false;
3250 return true;
3253 /* Return true when the DDR contains only constant access functions. */
3255 static bool
3256 constant_access_functions (const struct data_dependence_relation *ddr)
3258 unsigned i;
3260 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3261 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr), i))
3262 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr), i)))
3263 return false;
3265 return true;
3268 /* Helper function for the case where DDR_A and DDR_B are the same
3269 multivariate access function with a constant step. For an example
3270 see pr34635-1.c. */
3272 static void
3273 add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
3275 int x_1, x_2;
3276 tree c_1 = CHREC_LEFT (c_2);
3277 tree c_0 = CHREC_LEFT (c_1);
3278 lambda_vector dist_v;
3279 int v1, v2, cd;
3281 /* Polynomials with more than 2 variables are not handled yet. When
3282 the evolution steps are parameters, it is not possible to
3283 represent the dependence using classical distance vectors. */
3284 if (TREE_CODE (c_0) != INTEGER_CST
3285 || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
3286 || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
3288 DDR_AFFINE_P (ddr) = false;
3289 return;
3292 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
3293 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
3295 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3296 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3297 v1 = int_cst_value (CHREC_RIGHT (c_1));
3298 v2 = int_cst_value (CHREC_RIGHT (c_2));
3299 cd = gcd (v1, v2);
3300 v1 /= cd;
3301 v2 /= cd;
3303 if (v2 < 0)
3305 v2 = -v2;
3306 v1 = -v1;
3309 dist_v[x_1] = v2;
3310 dist_v[x_2] = -v1;
3311 save_dist_v (ddr, dist_v);
3313 add_outer_distances (ddr, dist_v, x_1);
3316 /* Helper function for the case where DDR_A and DDR_B are the same
3317 access functions. */
3319 static void
3320 add_other_self_distances (struct data_dependence_relation *ddr)
3322 lambda_vector dist_v;
3323 unsigned i;
3324 int index_carry = DDR_NB_LOOPS (ddr);
3326 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3328 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
3330 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
3332 if (!evolution_function_is_univariate_p (access_fun))
3334 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
3336 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3337 return;
3340 access_fun = DR_ACCESS_FN (DDR_A (ddr), 0);
3342 if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
3343 add_multivariate_self_dist (ddr, access_fun);
3344 else
3345 /* The evolution step is not constant: it varies in
3346 the outer loop, so this cannot be represented by a
3347 distance vector. For example in pr34635.c the
3348 evolution is {0, +, {0, +, 4}_1}_2. */
3349 DDR_AFFINE_P (ddr) = false;
3351 return;
3354 index_carry = MIN (index_carry,
3355 index_in_loop_nest (CHREC_VARIABLE (access_fun),
3356 DDR_LOOP_NEST (ddr)));
3360 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3361 add_outer_distances (ddr, dist_v, index_carry);
3364 static void
3365 insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
3367 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3369 dist_v[DDR_INNER_LOOP (ddr)] = 1;
3370 save_dist_v (ddr, dist_v);
3373 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
3374 is the case for example when access functions are the same and
3375 equal to a constant, as in:
3377 | loop_1
3378 | A[3] = ...
3379 | ... = A[3]
3380 | endloop_1
3382 in which case the distance vectors are (0) and (1). */
3384 static void
3385 add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
3387 unsigned i, j;
3389 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3391 subscript_p sub = DDR_SUBSCRIPT (ddr, i);
3392 conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
3393 conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
3395 for (j = 0; j < ca->n; j++)
3396 if (affine_function_zero_p (ca->fns[j]))
3398 insert_innermost_unit_dist_vector (ddr);
3399 return;
3402 for (j = 0; j < cb->n; j++)
3403 if (affine_function_zero_p (cb->fns[j]))
3405 insert_innermost_unit_dist_vector (ddr);
3406 return;
3411 /* Compute the classic per loop distance vector. DDR is the data
3412 dependence relation to build a vector from. Return false when fail
3413 to represent the data dependence as a distance vector. */
3415 static bool
3416 build_classic_dist_vector (struct data_dependence_relation *ddr,
3417 struct loop *loop_nest)
3419 bool init_b = false;
3420 int index_carry = DDR_NB_LOOPS (ddr);
3421 lambda_vector dist_v;
3423 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3424 return false;
3426 if (same_access_functions (ddr))
3428 /* Save the 0 vector. */
3429 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3430 save_dist_v (ddr, dist_v);
3432 if (constant_access_functions (ddr))
3433 add_distance_for_zero_overlaps (ddr);
3435 if (DDR_NB_LOOPS (ddr) > 1)
3436 add_other_self_distances (ddr);
3438 return true;
3441 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3442 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3443 dist_v, &init_b, &index_carry))
3444 return false;
3446 /* Save the distance vector if we initialized one. */
3447 if (init_b)
3449 /* Verify a basic constraint: classic distance vectors should
3450 always be lexicographically positive.
3452 Data references are collected in the order of execution of
3453 the program, thus for the following loop
3455 | for (i = 1; i < 100; i++)
3456 | for (j = 1; j < 100; j++)
3458 | t = T[j+1][i-1]; // A
3459 | T[j][i] = t + 2; // B
3462 references are collected following the direction of the wind:
3463 A then B. The data dependence tests are performed also
3464 following this order, such that we're looking at the distance
3465 separating the elements accessed by A from the elements later
3466 accessed by B. But in this example, the distance returned by
3467 test_dep (A, B) is lexicographically negative (-1, 1), that
3468 means that the access A occurs later than B with respect to
3469 the outer loop, ie. we're actually looking upwind. In this
3470 case we solve test_dep (B, A) looking downwind to the
3471 lexicographically positive solution, that returns the
3472 distance vector (1, -1). */
3473 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3475 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3476 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3477 loop_nest))
3478 return false;
3479 compute_subscript_distance (ddr);
3480 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3481 save_v, &init_b, &index_carry))
3482 return false;
3483 save_dist_v (ddr, save_v);
3484 DDR_REVERSED_P (ddr) = true;
3486 /* In this case there is a dependence forward for all the
3487 outer loops:
3489 | for (k = 1; k < 100; k++)
3490 | for (i = 1; i < 100; i++)
3491 | for (j = 1; j < 100; j++)
3493 | t = T[j+1][i-1]; // A
3494 | T[j][i] = t + 2; // B
3497 the vectors are:
3498 (0, 1, -1)
3499 (1, 1, -1)
3500 (1, -1, 1)
3502 if (DDR_NB_LOOPS (ddr) > 1)
3504 add_outer_distances (ddr, save_v, index_carry);
3505 add_outer_distances (ddr, dist_v, index_carry);
3508 else
3510 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3511 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3513 if (DDR_NB_LOOPS (ddr) > 1)
3515 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3517 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr),
3518 DDR_A (ddr), loop_nest))
3519 return false;
3520 compute_subscript_distance (ddr);
3521 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3522 opposite_v, &init_b,
3523 &index_carry))
3524 return false;
3526 save_dist_v (ddr, save_v);
3527 add_outer_distances (ddr, dist_v, index_carry);
3528 add_outer_distances (ddr, opposite_v, index_carry);
3530 else
3531 save_dist_v (ddr, save_v);
3534 else
3536 /* There is a distance of 1 on all the outer loops: Example:
3537 there is a dependence of distance 1 on loop_1 for the array A.
3539 | loop_1
3540 | A[5] = ...
3541 | endloop
3543 add_outer_distances (ddr, dist_v,
3544 lambda_vector_first_nz (dist_v,
3545 DDR_NB_LOOPS (ddr), 0));
3548 if (dump_file && (dump_flags & TDF_DETAILS))
3550 unsigned i;
3552 fprintf (dump_file, "(build_classic_dist_vector\n");
3553 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3555 fprintf (dump_file, " dist_vector = (");
3556 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3557 DDR_NB_LOOPS (ddr));
3558 fprintf (dump_file, " )\n");
3560 fprintf (dump_file, ")\n");
3563 return true;
3566 /* Return the direction for a given distance.
3567 FIXME: Computing dir this way is suboptimal, since dir can catch
3568 cases that dist is unable to represent. */
3570 static inline enum data_dependence_direction
3571 dir_from_dist (int dist)
3573 if (dist > 0)
3574 return dir_positive;
3575 else if (dist < 0)
3576 return dir_negative;
3577 else
3578 return dir_equal;
3581 /* Compute the classic per loop direction vector. DDR is the data
3582 dependence relation to build a vector from. */
3584 static void
3585 build_classic_dir_vector (struct data_dependence_relation *ddr)
3587 unsigned i, j;
3588 lambda_vector dist_v;
3590 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
3592 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3594 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3595 dir_v[j] = dir_from_dist (dist_v[j]);
3597 save_dir_v (ddr, dir_v);
3601 /* Helper function. Returns true when there is a dependence between
3602 data references DRA and DRB. */
3604 static bool
3605 subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
3606 struct data_reference *dra,
3607 struct data_reference *drb,
3608 struct loop *loop_nest)
3610 unsigned int i;
3611 tree last_conflicts;
3612 struct subscript *subscript;
3613 tree res = NULL_TREE;
3615 for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
3617 conflict_function *overlaps_a, *overlaps_b;
3619 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3620 DR_ACCESS_FN (drb, i),
3621 &overlaps_a, &overlaps_b,
3622 &last_conflicts, loop_nest);
3624 if (SUB_CONFLICTS_IN_A (subscript))
3625 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
3626 if (SUB_CONFLICTS_IN_B (subscript))
3627 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
3629 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3630 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3631 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3633 /* If there is any undetermined conflict function we have to
3634 give a conservative answer in case we cannot prove that
3635 no dependence exists when analyzing another subscript. */
3636 if (CF_NOT_KNOWN_P (overlaps_a)
3637 || CF_NOT_KNOWN_P (overlaps_b))
3639 res = chrec_dont_know;
3640 continue;
3643 /* When there is a subscript with no dependence we can stop. */
3644 else if (CF_NO_DEPENDENCE_P (overlaps_a)
3645 || CF_NO_DEPENDENCE_P (overlaps_b))
3647 res = chrec_known;
3648 break;
3652 if (res == NULL_TREE)
3653 return true;
3655 if (res == chrec_known)
3656 dependence_stats.num_dependence_independent++;
3657 else
3658 dependence_stats.num_dependence_undetermined++;
3659 finalize_ddr_dependent (ddr, res);
3660 return false;
3663 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
3665 static void
3666 subscript_dependence_tester (struct data_dependence_relation *ddr,
3667 struct loop *loop_nest)
3669 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr), loop_nest))
3670 dependence_stats.num_dependence_dependent++;
3672 compute_subscript_distance (ddr);
3673 if (build_classic_dist_vector (ddr, loop_nest))
3674 build_classic_dir_vector (ddr);
3677 /* Returns true when all the access functions of A are affine or
3678 constant with respect to LOOP_NEST. */
3680 static bool
3681 access_functions_are_affine_or_constant_p (const struct data_reference *a,
3682 const struct loop *loop_nest)
3684 unsigned int i;
3685 vec<tree> fns = DR_ACCESS_FNS (a);
3686 tree t;
3688 FOR_EACH_VEC_ELT (fns, i, t)
3689 if (!evolution_function_is_invariant_p (t, loop_nest->num)
3690 && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
3691 return false;
3693 return true;
3696 /* Initializes an equation for an OMEGA problem using the information
3697 contained in the ACCESS_FUN. Returns true when the operation
3698 succeeded.
3700 PB is the omega constraint system.
3701 EQ is the number of the equation to be initialized.
3702 OFFSET is used for shifting the variables names in the constraints:
3703 a constrain is composed of 2 * the number of variables surrounding
3704 dependence accesses. OFFSET is set either to 0 for the first n variables,
3705 then it is set to n.
3706 ACCESS_FUN is expected to be an affine chrec. */
3708 static bool
3709 init_omega_eq_with_af (omega_pb pb, unsigned eq,
3710 unsigned int offset, tree access_fun,
3711 struct data_dependence_relation *ddr)
3713 switch (TREE_CODE (access_fun))
3715 case POLYNOMIAL_CHREC:
3717 tree left = CHREC_LEFT (access_fun);
3718 tree right = CHREC_RIGHT (access_fun);
3719 int var = CHREC_VARIABLE (access_fun);
3720 unsigned var_idx;
3722 if (TREE_CODE (right) != INTEGER_CST)
3723 return false;
3725 var_idx = index_in_loop_nest (var, DDR_LOOP_NEST (ddr));
3726 pb->eqs[eq].coef[offset + var_idx + 1] = int_cst_value (right);
3728 /* Compute the innermost loop index. */
3729 DDR_INNER_LOOP (ddr) = MAX (DDR_INNER_LOOP (ddr), var_idx);
3731 if (offset == 0)
3732 pb->eqs[eq].coef[var_idx + DDR_NB_LOOPS (ddr) + 1]
3733 += int_cst_value (right);
3735 switch (TREE_CODE (left))
3737 case POLYNOMIAL_CHREC:
3738 return init_omega_eq_with_af (pb, eq, offset, left, ddr);
3740 case INTEGER_CST:
3741 pb->eqs[eq].coef[0] += int_cst_value (left);
3742 return true;
3744 default:
3745 return false;
3749 case INTEGER_CST:
3750 pb->eqs[eq].coef[0] += int_cst_value (access_fun);
3751 return true;
3753 default:
3754 return false;
3758 /* As explained in the comments preceding init_omega_for_ddr, we have
3759 to set up a system for each loop level, setting outer loops
3760 variation to zero, and current loop variation to positive or zero.
3761 Save each lexico positive distance vector. */
3763 static void
3764 omega_extract_distance_vectors (omega_pb pb,
3765 struct data_dependence_relation *ddr)
3767 int eq, geq;
3768 unsigned i, j;
3769 struct loop *loopi, *loopj;
3770 enum omega_result res;
3772 /* Set a new problem for each loop in the nest. The basis is the
3773 problem that we have initialized until now. On top of this we
3774 add new constraints. */
3775 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3776 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3778 int dist = 0;
3779 omega_pb copy = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr),
3780 DDR_NB_LOOPS (ddr));
3782 omega_copy_problem (copy, pb);
3784 /* For all the outer loops "loop_j", add "dj = 0". */
3785 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3787 eq = omega_add_zero_eq (copy, omega_black);
3788 copy->eqs[eq].coef[j + 1] = 1;
3791 /* For "loop_i", add "0 <= di". */
3792 geq = omega_add_zero_geq (copy, omega_black);
3793 copy->geqs[geq].coef[i + 1] = 1;
3795 /* Reduce the constraint system, and test that the current
3796 problem is feasible. */
3797 res = omega_simplify_problem (copy);
3798 if (res == omega_false
3799 || res == omega_unknown
3800 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3801 goto next_problem;
3803 for (eq = 0; eq < copy->num_subs; eq++)
3804 if (copy->subs[eq].key == (int) i + 1)
3806 dist = copy->subs[eq].coef[0];
3807 goto found_dist;
3810 if (dist == 0)
3812 /* Reinitialize problem... */
3813 omega_copy_problem (copy, pb);
3814 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3816 eq = omega_add_zero_eq (copy, omega_black);
3817 copy->eqs[eq].coef[j + 1] = 1;
3820 /* ..., but this time "di = 1". */
3821 eq = omega_add_zero_eq (copy, omega_black);
3822 copy->eqs[eq].coef[i + 1] = 1;
3823 copy->eqs[eq].coef[0] = -1;
3825 res = omega_simplify_problem (copy);
3826 if (res == omega_false
3827 || res == omega_unknown
3828 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3829 goto next_problem;
3831 for (eq = 0; eq < copy->num_subs; eq++)
3832 if (copy->subs[eq].key == (int) i + 1)
3834 dist = copy->subs[eq].coef[0];
3835 goto found_dist;
3839 found_dist:;
3840 /* Save the lexicographically positive distance vector. */
3841 if (dist >= 0)
3843 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3844 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3846 dist_v[i] = dist;
3848 for (eq = 0; eq < copy->num_subs; eq++)
3849 if (copy->subs[eq].key > 0)
3851 dist = copy->subs[eq].coef[0];
3852 dist_v[copy->subs[eq].key - 1] = dist;
3855 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3856 dir_v[j] = dir_from_dist (dist_v[j]);
3858 save_dist_v (ddr, dist_v);
3859 save_dir_v (ddr, dir_v);
3862 next_problem:;
3863 omega_free_problem (copy);
3867 /* This is called for each subscript of a tuple of data references:
3868 insert an equality for representing the conflicts. */
3870 static bool
3871 omega_setup_subscript (tree access_fun_a, tree access_fun_b,
3872 struct data_dependence_relation *ddr,
3873 omega_pb pb, bool *maybe_dependent)
3875 int eq;
3876 tree type = signed_type_for_types (TREE_TYPE (access_fun_a),
3877 TREE_TYPE (access_fun_b));
3878 tree fun_a = chrec_convert (type, access_fun_a, NULL);
3879 tree fun_b = chrec_convert (type, access_fun_b, NULL);
3880 tree difference = chrec_fold_minus (type, fun_a, fun_b);
3881 tree minus_one;
3883 /* When the fun_a - fun_b is not constant, the dependence is not
3884 captured by the classic distance vector representation. */
3885 if (TREE_CODE (difference) != INTEGER_CST)
3886 return false;
3888 /* ZIV test. */
3889 if (ziv_subscript_p (fun_a, fun_b) && !integer_zerop (difference))
3891 /* There is no dependence. */
3892 *maybe_dependent = false;
3893 return true;
3896 minus_one = build_int_cst (type, -1);
3897 fun_b = chrec_fold_multiply (type, fun_b, minus_one);
3899 eq = omega_add_zero_eq (pb, omega_black);
3900 if (!init_omega_eq_with_af (pb, eq, DDR_NB_LOOPS (ddr), fun_a, ddr)
3901 || !init_omega_eq_with_af (pb, eq, 0, fun_b, ddr))
3902 /* There is probably a dependence, but the system of
3903 constraints cannot be built: answer "don't know". */
3904 return false;
3906 /* GCD test. */
3907 if (DDR_NB_LOOPS (ddr) != 0 && pb->eqs[eq].coef[0]
3908 && !int_divides_p (lambda_vector_gcd
3909 ((lambda_vector) &(pb->eqs[eq].coef[1]),
3910 2 * DDR_NB_LOOPS (ddr)),
3911 pb->eqs[eq].coef[0]))
3913 /* There is no dependence. */
3914 *maybe_dependent = false;
3915 return true;
3918 return true;
3921 /* Helper function, same as init_omega_for_ddr but specialized for
3922 data references A and B. */
3924 static bool
3925 init_omega_for_ddr_1 (struct data_reference *dra, struct data_reference *drb,
3926 struct data_dependence_relation *ddr,
3927 omega_pb pb, bool *maybe_dependent)
3929 unsigned i;
3930 int ineq;
3931 struct loop *loopi;
3932 unsigned nb_loops = DDR_NB_LOOPS (ddr);
3934 /* Insert an equality per subscript. */
3935 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3937 if (!omega_setup_subscript (DR_ACCESS_FN (dra, i), DR_ACCESS_FN (drb, i),
3938 ddr, pb, maybe_dependent))
3939 return false;
3940 else if (*maybe_dependent == false)
3942 /* There is no dependence. */
3943 DDR_ARE_DEPENDENT (ddr) = chrec_known;
3944 return true;
3948 /* Insert inequalities: constraints corresponding to the iteration
3949 domain, i.e. the loops surrounding the references "loop_x" and
3950 the distance variables "dx". The layout of the OMEGA
3951 representation is as follows:
3952 - coef[0] is the constant
3953 - coef[1..nb_loops] are the protected variables that will not be
3954 removed by the solver: the "dx"
3955 - coef[nb_loops + 1, 2*nb_loops] are the loop variables: "loop_x".
3957 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3958 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3960 HOST_WIDE_INT nbi = max_stmt_executions_int (loopi);
3962 /* 0 <= loop_x */
3963 ineq = omega_add_zero_geq (pb, omega_black);
3964 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3966 /* 0 <= loop_x + dx */
3967 ineq = omega_add_zero_geq (pb, omega_black);
3968 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3969 pb->geqs[ineq].coef[i + 1] = 1;
3971 if (nbi != -1)
3973 /* loop_x <= nb_iters */
3974 ineq = omega_add_zero_geq (pb, omega_black);
3975 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3976 pb->geqs[ineq].coef[0] = nbi;
3978 /* loop_x + dx <= nb_iters */
3979 ineq = omega_add_zero_geq (pb, omega_black);
3980 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3981 pb->geqs[ineq].coef[i + 1] = -1;
3982 pb->geqs[ineq].coef[0] = nbi;
3984 /* A step "dx" bigger than nb_iters is not feasible, so
3985 add "0 <= nb_iters + dx", */
3986 ineq = omega_add_zero_geq (pb, omega_black);
3987 pb->geqs[ineq].coef[i + 1] = 1;
3988 pb->geqs[ineq].coef[0] = nbi;
3989 /* and "dx <= nb_iters". */
3990 ineq = omega_add_zero_geq (pb, omega_black);
3991 pb->geqs[ineq].coef[i + 1] = -1;
3992 pb->geqs[ineq].coef[0] = nbi;
3996 omega_extract_distance_vectors (pb, ddr);
3998 return true;
4001 /* Sets up the Omega dependence problem for the data dependence
4002 relation DDR. Returns false when the constraint system cannot be
4003 built, ie. when the test answers "don't know". Returns true
4004 otherwise, and when independence has been proved (using one of the
4005 trivial dependence test), set MAYBE_DEPENDENT to false, otherwise
4006 set MAYBE_DEPENDENT to true.
4008 Example: for setting up the dependence system corresponding to the
4009 conflicting accesses
4011 | loop_i
4012 | loop_j
4013 | A[i, i+1] = ...
4014 | ... A[2*j, 2*(i + j)]
4015 | endloop_j
4016 | endloop_i
4018 the following constraints come from the iteration domain:
4020 0 <= i <= Ni
4021 0 <= i + di <= Ni
4022 0 <= j <= Nj
4023 0 <= j + dj <= Nj
4025 where di, dj are the distance variables. The constraints
4026 representing the conflicting elements are:
4028 i = 2 * (j + dj)
4029 i + 1 = 2 * (i + di + j + dj)
4031 For asking that the resulting distance vector (di, dj) be
4032 lexicographically positive, we insert the constraint "di >= 0". If
4033 "di = 0" in the solution, we fix that component to zero, and we
4034 look at the inner loops: we set a new problem where all the outer
4035 loop distances are zero, and fix this inner component to be
4036 positive. When one of the components is positive, we save that
4037 distance, and set a new problem where the distance on this loop is
4038 zero, searching for other distances in the inner loops. Here is
4039 the classic example that illustrates that we have to set for each
4040 inner loop a new problem:
4042 | loop_1
4043 | loop_2
4044 | A[10]
4045 | endloop_2
4046 | endloop_1
4048 we have to save two distances (1, 0) and (0, 1).
4050 Given two array references, refA and refB, we have to set the
4051 dependence problem twice, refA vs. refB and refB vs. refA, and we
4052 cannot do a single test, as refB might occur before refA in the
4053 inner loops, and the contrary when considering outer loops: ex.
4055 | loop_0
4056 | loop_1
4057 | loop_2
4058 | T[{1,+,1}_2][{1,+,1}_1] // refA
4059 | T[{2,+,1}_2][{0,+,1}_1] // refB
4060 | endloop_2
4061 | endloop_1
4062 | endloop_0
4064 refB touches the elements in T before refA, and thus for the same
4065 loop_0 refB precedes refA: ie. the distance vector (0, 1, -1)
4066 but for successive loop_0 iterations, we have (1, -1, 1)
4068 The Omega solver expects the distance variables ("di" in the
4069 previous example) to come first in the constraint system (as
4070 variables to be protected, or "safe" variables), the constraint
4071 system is built using the following layout:
4073 "cst | distance vars | index vars".
4076 static bool
4077 init_omega_for_ddr (struct data_dependence_relation *ddr,
4078 bool *maybe_dependent)
4080 omega_pb pb;
4081 bool res = false;
4083 *maybe_dependent = true;
4085 if (same_access_functions (ddr))
4087 unsigned j;
4088 lambda_vector dir_v;
4090 /* Save the 0 vector. */
4091 save_dist_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
4092 dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4093 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
4094 dir_v[j] = dir_equal;
4095 save_dir_v (ddr, dir_v);
4097 /* Save the dependences carried by outer loops. */
4098 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4099 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4100 maybe_dependent);
4101 omega_free_problem (pb);
4102 return res;
4105 /* Omega expects the protected variables (those that have to be kept
4106 after elimination) to appear first in the constraint system.
4107 These variables are the distance variables. In the following
4108 initialization we declare NB_LOOPS safe variables, and the total
4109 number of variables for the constraint system is 2*NB_LOOPS. */
4110 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4111 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4112 maybe_dependent);
4113 omega_free_problem (pb);
4115 /* Stop computation if not decidable, or no dependence. */
4116 if (res == false || *maybe_dependent == false)
4117 return res;
4119 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4120 res = init_omega_for_ddr_1 (DDR_B (ddr), DDR_A (ddr), ddr, pb,
4121 maybe_dependent);
4122 omega_free_problem (pb);
4124 return res;
4127 /* Return true when DDR contains the same information as that stored
4128 in DIR_VECTS and in DIST_VECTS, return false otherwise. */
4130 static bool
4131 ddr_consistent_p (FILE *file,
4132 struct data_dependence_relation *ddr,
4133 vec<lambda_vector> dist_vects,
4134 vec<lambda_vector> dir_vects)
4136 unsigned int i, j;
4138 /* If dump_file is set, output there. */
4139 if (dump_file && (dump_flags & TDF_DETAILS))
4140 file = dump_file;
4142 if (dist_vects.length () != DDR_NUM_DIST_VECTS (ddr))
4144 lambda_vector b_dist_v;
4145 fprintf (file, "\n(Number of distance vectors differ: Banerjee has %d, Omega has %d.\n",
4146 dist_vects.length (),
4147 DDR_NUM_DIST_VECTS (ddr));
4149 fprintf (file, "Banerjee dist vectors:\n");
4150 FOR_EACH_VEC_ELT (dist_vects, i, b_dist_v)
4151 print_lambda_vector (file, b_dist_v, DDR_NB_LOOPS (ddr));
4153 fprintf (file, "Omega dist vectors:\n");
4154 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4155 print_lambda_vector (file, DDR_DIST_VECT (ddr, i), DDR_NB_LOOPS (ddr));
4157 fprintf (file, "data dependence relation:\n");
4158 dump_data_dependence_relation (file, ddr);
4160 fprintf (file, ")\n");
4161 return false;
4164 if (dir_vects.length () != DDR_NUM_DIR_VECTS (ddr))
4166 fprintf (file, "\n(Number of direction vectors differ: Banerjee has %d, Omega has %d.)\n",
4167 dir_vects.length (),
4168 DDR_NUM_DIR_VECTS (ddr));
4169 return false;
4172 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4174 lambda_vector a_dist_v;
4175 lambda_vector b_dist_v = DDR_DIST_VECT (ddr, i);
4177 /* Distance vectors are not ordered in the same way in the DDR
4178 and in the DIST_VECTS: search for a matching vector. */
4179 FOR_EACH_VEC_ELT (dist_vects, j, a_dist_v)
4180 if (lambda_vector_equal (a_dist_v, b_dist_v, DDR_NB_LOOPS (ddr)))
4181 break;
4183 if (j == dist_vects.length ())
4185 fprintf (file, "\n(Dist vectors from the first dependence analyzer:\n");
4186 print_dist_vectors (file, dist_vects, DDR_NB_LOOPS (ddr));
4187 fprintf (file, "not found in Omega dist vectors:\n");
4188 print_dist_vectors (file, DDR_DIST_VECTS (ddr), DDR_NB_LOOPS (ddr));
4189 fprintf (file, "data dependence relation:\n");
4190 dump_data_dependence_relation (file, ddr);
4191 fprintf (file, ")\n");
4195 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
4197 lambda_vector a_dir_v;
4198 lambda_vector b_dir_v = DDR_DIR_VECT (ddr, i);
4200 /* Direction vectors are not ordered in the same way in the DDR
4201 and in the DIR_VECTS: search for a matching vector. */
4202 FOR_EACH_VEC_ELT (dir_vects, j, a_dir_v)
4203 if (lambda_vector_equal (a_dir_v, b_dir_v, DDR_NB_LOOPS (ddr)))
4204 break;
4206 if (j == dist_vects.length ())
4208 fprintf (file, "\n(Dir vectors from the first dependence analyzer:\n");
4209 print_dir_vectors (file, dir_vects, DDR_NB_LOOPS (ddr));
4210 fprintf (file, "not found in Omega dir vectors:\n");
4211 print_dir_vectors (file, DDR_DIR_VECTS (ddr), DDR_NB_LOOPS (ddr));
4212 fprintf (file, "data dependence relation:\n");
4213 dump_data_dependence_relation (file, ddr);
4214 fprintf (file, ")\n");
4218 return true;
4221 /* This computes the affine dependence relation between A and B with
4222 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
4223 independence between two accesses, while CHREC_DONT_KNOW is used
4224 for representing the unknown relation.
4226 Note that it is possible to stop the computation of the dependence
4227 relation the first time we detect a CHREC_KNOWN element for a given
4228 subscript. */
4230 void
4231 compute_affine_dependence (struct data_dependence_relation *ddr,
4232 struct loop *loop_nest)
4234 struct data_reference *dra = DDR_A (ddr);
4235 struct data_reference *drb = DDR_B (ddr);
4237 if (dump_file && (dump_flags & TDF_DETAILS))
4239 fprintf (dump_file, "(compute_affine_dependence\n");
4240 fprintf (dump_file, " stmt_a: ");
4241 print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
4242 fprintf (dump_file, " stmt_b: ");
4243 print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
4246 /* Analyze only when the dependence relation is not yet known. */
4247 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4249 dependence_stats.num_dependence_tests++;
4251 if (access_functions_are_affine_or_constant_p (dra, loop_nest)
4252 && access_functions_are_affine_or_constant_p (drb, loop_nest))
4254 subscript_dependence_tester (ddr, loop_nest);
4256 if (flag_check_data_deps)
4258 /* Dump the dependences from the first algorithm. */
4259 if (dump_file && (dump_flags & TDF_DETAILS))
4261 fprintf (dump_file, "\n\nBanerjee Analyzer\n");
4262 dump_data_dependence_relation (dump_file, ddr);
4265 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4267 bool maybe_dependent;
4268 vec<lambda_vector> dir_vects, dist_vects;
4270 /* Save the result of the first DD analyzer. */
4271 dist_vects = DDR_DIST_VECTS (ddr);
4272 dir_vects = DDR_DIR_VECTS (ddr);
4274 /* Reset the information. */
4275 DDR_DIST_VECTS (ddr).create (0);
4276 DDR_DIR_VECTS (ddr).create (0);
4278 /* Compute the same information using Omega. */
4279 if (!init_omega_for_ddr (ddr, &maybe_dependent))
4280 goto csys_dont_know;
4282 if (dump_file && (dump_flags & TDF_DETAILS))
4284 fprintf (dump_file, "Omega Analyzer\n");
4285 dump_data_dependence_relation (dump_file, ddr);
4288 /* Check that we get the same information. */
4289 if (maybe_dependent)
4290 gcc_assert (ddr_consistent_p (stderr, ddr, dist_vects,
4291 dir_vects));
4296 /* As a last case, if the dependence cannot be determined, or if
4297 the dependence is considered too difficult to determine, answer
4298 "don't know". */
4299 else
4301 csys_dont_know:;
4302 dependence_stats.num_dependence_undetermined++;
4304 if (dump_file && (dump_flags & TDF_DETAILS))
4306 fprintf (dump_file, "Data ref a:\n");
4307 dump_data_reference (dump_file, dra);
4308 fprintf (dump_file, "Data ref b:\n");
4309 dump_data_reference (dump_file, drb);
4310 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
4312 finalize_ddr_dependent (ddr, chrec_dont_know);
4316 if (dump_file && (dump_flags & TDF_DETAILS))
4318 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4319 fprintf (dump_file, ") -> no dependence\n");
4320 else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
4321 fprintf (dump_file, ") -> dependence analysis failed\n");
4322 else
4323 fprintf (dump_file, ")\n");
4327 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
4328 the data references in DATAREFS, in the LOOP_NEST. When
4329 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
4330 relations. Return true when successful, i.e. data references number
4331 is small enough to be handled. */
4333 bool
4334 compute_all_dependences (vec<data_reference_p> datarefs,
4335 vec<ddr_p> *dependence_relations,
4336 vec<loop_p> loop_nest,
4337 bool compute_self_and_rr)
4339 struct data_dependence_relation *ddr;
4340 struct data_reference *a, *b;
4341 unsigned int i, j;
4343 if ((int) datarefs.length ()
4344 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
4346 struct data_dependence_relation *ddr;
4348 /* Insert a single relation into dependence_relations:
4349 chrec_dont_know. */
4350 ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
4351 dependence_relations->safe_push (ddr);
4352 return false;
4355 FOR_EACH_VEC_ELT (datarefs, i, a)
4356 for (j = i + 1; datarefs.iterate (j, &b); j++)
4357 if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
4359 ddr = initialize_data_dependence_relation (a, b, loop_nest);
4360 dependence_relations->safe_push (ddr);
4361 if (loop_nest.exists ())
4362 compute_affine_dependence (ddr, loop_nest[0]);
4365 if (compute_self_and_rr)
4366 FOR_EACH_VEC_ELT (datarefs, i, a)
4368 ddr = initialize_data_dependence_relation (a, a, loop_nest);
4369 dependence_relations->safe_push (ddr);
4370 if (loop_nest.exists ())
4371 compute_affine_dependence (ddr, loop_nest[0]);
4374 return true;
4377 /* Describes a location of a memory reference. */
4379 typedef struct data_ref_loc_d
4381 /* The memory reference. */
4382 tree ref;
4384 /* True if the memory reference is read. */
4385 bool is_read;
4386 } data_ref_loc;
4389 /* Stores the locations of memory references in STMT to REFERENCES. Returns
4390 true if STMT clobbers memory, false otherwise. */
4392 static bool
4393 get_references_in_stmt (gimple stmt, vec<data_ref_loc, va_heap> *references)
4395 bool clobbers_memory = false;
4396 data_ref_loc ref;
4397 tree op0, op1;
4398 enum gimple_code stmt_code = gimple_code (stmt);
4400 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4401 As we cannot model data-references to not spelled out
4402 accesses give up if they may occur. */
4403 if (stmt_code == GIMPLE_CALL
4404 && !(gimple_call_flags (stmt) & ECF_CONST))
4406 /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
4407 if (gimple_call_internal_p (stmt))
4408 switch (gimple_call_internal_fn (stmt))
4410 case IFN_GOMP_SIMD_LANE:
4412 struct loop *loop = gimple_bb (stmt)->loop_father;
4413 tree uid = gimple_call_arg (stmt, 0);
4414 gcc_assert (TREE_CODE (uid) == SSA_NAME);
4415 if (loop == NULL
4416 || loop->simduid != SSA_NAME_VAR (uid))
4417 clobbers_memory = true;
4418 break;
4420 case IFN_MASK_LOAD:
4421 case IFN_MASK_STORE:
4422 break;
4423 default:
4424 clobbers_memory = true;
4425 break;
4427 else
4428 clobbers_memory = true;
4430 else if (stmt_code == GIMPLE_ASM
4431 && (gimple_asm_volatile_p (as_a <gasm *> (stmt))
4432 || gimple_vuse (stmt)))
4433 clobbers_memory = true;
4435 if (!gimple_vuse (stmt))
4436 return clobbers_memory;
4438 if (stmt_code == GIMPLE_ASSIGN)
4440 tree base;
4441 op0 = gimple_assign_lhs (stmt);
4442 op1 = gimple_assign_rhs1 (stmt);
4444 if (DECL_P (op1)
4445 || (REFERENCE_CLASS_P (op1)
4446 && (base = get_base_address (op1))
4447 && TREE_CODE (base) != SSA_NAME))
4449 ref.ref = op1;
4450 ref.is_read = true;
4451 references->safe_push (ref);
4454 else if (stmt_code == GIMPLE_CALL)
4456 unsigned i, n;
4458 ref.is_read = false;
4459 if (gimple_call_internal_p (stmt))
4460 switch (gimple_call_internal_fn (stmt))
4462 case IFN_MASK_LOAD:
4463 if (gimple_call_lhs (stmt) == NULL_TREE)
4464 break;
4465 ref.is_read = true;
4466 case IFN_MASK_STORE:
4467 ref.ref = fold_build2 (MEM_REF,
4468 ref.is_read
4469 ? TREE_TYPE (gimple_call_lhs (stmt))
4470 : TREE_TYPE (gimple_call_arg (stmt, 3)),
4471 gimple_call_arg (stmt, 0),
4472 gimple_call_arg (stmt, 1));
4473 references->safe_push (ref);
4474 return false;
4475 default:
4476 break;
4479 op0 = gimple_call_lhs (stmt);
4480 n = gimple_call_num_args (stmt);
4481 for (i = 0; i < n; i++)
4483 op1 = gimple_call_arg (stmt, i);
4485 if (DECL_P (op1)
4486 || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
4488 ref.ref = op1;
4489 ref.is_read = true;
4490 references->safe_push (ref);
4494 else
4495 return clobbers_memory;
4497 if (op0
4498 && (DECL_P (op0)
4499 || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
4501 ref.ref = op0;
4502 ref.is_read = false;
4503 references->safe_push (ref);
4505 return clobbers_memory;
4508 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
4509 reference, returns false, otherwise returns true. NEST is the outermost
4510 loop of the loop nest in which the references should be analyzed. */
4512 bool
4513 find_data_references_in_stmt (struct loop *nest, gimple stmt,
4514 vec<data_reference_p> *datarefs)
4516 unsigned i;
4517 auto_vec<data_ref_loc, 2> references;
4518 data_ref_loc *ref;
4519 bool ret = true;
4520 data_reference_p dr;
4522 if (get_references_in_stmt (stmt, &references))
4523 return false;
4525 FOR_EACH_VEC_ELT (references, i, ref)
4527 dr = create_data_ref (nest, loop_containing_stmt (stmt),
4528 ref->ref, stmt, ref->is_read);
4529 gcc_assert (dr != NULL);
4530 datarefs->safe_push (dr);
4532 references.release ();
4533 return ret;
4536 /* Stores the data references in STMT to DATAREFS. If there is an
4537 unanalyzable reference, returns false, otherwise returns true.
4538 NEST is the outermost loop of the loop nest in which the references
4539 should be instantiated, LOOP is the loop in which the references
4540 should be analyzed. */
4542 bool
4543 graphite_find_data_references_in_stmt (loop_p nest, loop_p loop, gimple stmt,
4544 vec<data_reference_p> *datarefs)
4546 unsigned i;
4547 auto_vec<data_ref_loc, 2> references;
4548 data_ref_loc *ref;
4549 bool ret = true;
4550 data_reference_p dr;
4552 if (get_references_in_stmt (stmt, &references))
4553 return false;
4555 FOR_EACH_VEC_ELT (references, i, ref)
4557 dr = create_data_ref (nest, loop, ref->ref, stmt, ref->is_read);
4558 gcc_assert (dr != NULL);
4559 datarefs->safe_push (dr);
4562 references.release ();
4563 return ret;
4566 /* Search the data references in LOOP, and record the information into
4567 DATAREFS. Returns chrec_dont_know when failing to analyze a
4568 difficult case, returns NULL_TREE otherwise. */
4570 tree
4571 find_data_references_in_bb (struct loop *loop, basic_block bb,
4572 vec<data_reference_p> *datarefs)
4574 gimple_stmt_iterator bsi;
4576 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4578 gimple stmt = gsi_stmt (bsi);
4580 if (!find_data_references_in_stmt (loop, stmt, datarefs))
4582 struct data_reference *res;
4583 res = XCNEW (struct data_reference);
4584 datarefs->safe_push (res);
4586 return chrec_dont_know;
4590 return NULL_TREE;
4593 /* Search the data references in LOOP, and record the information into
4594 DATAREFS. Returns chrec_dont_know when failing to analyze a
4595 difficult case, returns NULL_TREE otherwise.
4597 TODO: This function should be made smarter so that it can handle address
4598 arithmetic as if they were array accesses, etc. */
4600 tree
4601 find_data_references_in_loop (struct loop *loop,
4602 vec<data_reference_p> *datarefs)
4604 basic_block bb, *bbs;
4605 unsigned int i;
4607 bbs = get_loop_body_in_dom_order (loop);
4609 for (i = 0; i < loop->num_nodes; i++)
4611 bb = bbs[i];
4613 if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
4615 free (bbs);
4616 return chrec_dont_know;
4619 free (bbs);
4621 return NULL_TREE;
4624 /* Recursive helper function. */
4626 static bool
4627 find_loop_nest_1 (struct loop *loop, vec<loop_p> *loop_nest)
4629 /* Inner loops of the nest should not contain siblings. Example:
4630 when there are two consecutive loops,
4632 | loop_0
4633 | loop_1
4634 | A[{0, +, 1}_1]
4635 | endloop_1
4636 | loop_2
4637 | A[{0, +, 1}_2]
4638 | endloop_2
4639 | endloop_0
4641 the dependence relation cannot be captured by the distance
4642 abstraction. */
4643 if (loop->next)
4644 return false;
4646 loop_nest->safe_push (loop);
4647 if (loop->inner)
4648 return find_loop_nest_1 (loop->inner, loop_nest);
4649 return true;
4652 /* Return false when the LOOP is not well nested. Otherwise return
4653 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4654 contain the loops from the outermost to the innermost, as they will
4655 appear in the classic distance vector. */
4657 bool
4658 find_loop_nest (struct loop *loop, vec<loop_p> *loop_nest)
4660 loop_nest->safe_push (loop);
4661 if (loop->inner)
4662 return find_loop_nest_1 (loop->inner, loop_nest);
4663 return true;
4666 /* Returns true when the data dependences have been computed, false otherwise.
4667 Given a loop nest LOOP, the following vectors are returned:
4668 DATAREFS is initialized to all the array elements contained in this loop,
4669 DEPENDENCE_RELATIONS contains the relations between the data references.
4670 Compute read-read and self relations if
4671 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4673 bool
4674 compute_data_dependences_for_loop (struct loop *loop,
4675 bool compute_self_and_read_read_dependences,
4676 vec<loop_p> *loop_nest,
4677 vec<data_reference_p> *datarefs,
4678 vec<ddr_p> *dependence_relations)
4680 bool res = true;
4682 memset (&dependence_stats, 0, sizeof (dependence_stats));
4684 /* If the loop nest is not well formed, or one of the data references
4685 is not computable, give up without spending time to compute other
4686 dependences. */
4687 if (!loop
4688 || !find_loop_nest (loop, loop_nest)
4689 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
4690 || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
4691 compute_self_and_read_read_dependences))
4692 res = false;
4694 if (dump_file && (dump_flags & TDF_STATS))
4696 fprintf (dump_file, "Dependence tester statistics:\n");
4698 fprintf (dump_file, "Number of dependence tests: %d\n",
4699 dependence_stats.num_dependence_tests);
4700 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
4701 dependence_stats.num_dependence_dependent);
4702 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
4703 dependence_stats.num_dependence_independent);
4704 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
4705 dependence_stats.num_dependence_undetermined);
4707 fprintf (dump_file, "Number of subscript tests: %d\n",
4708 dependence_stats.num_subscript_tests);
4709 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
4710 dependence_stats.num_subscript_undetermined);
4711 fprintf (dump_file, "Number of same subscript function: %d\n",
4712 dependence_stats.num_same_subscript_function);
4714 fprintf (dump_file, "Number of ziv tests: %d\n",
4715 dependence_stats.num_ziv);
4716 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4717 dependence_stats.num_ziv_dependent);
4718 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4719 dependence_stats.num_ziv_independent);
4720 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
4721 dependence_stats.num_ziv_unimplemented);
4723 fprintf (dump_file, "Number of siv tests: %d\n",
4724 dependence_stats.num_siv);
4725 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4726 dependence_stats.num_siv_dependent);
4727 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4728 dependence_stats.num_siv_independent);
4729 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4730 dependence_stats.num_siv_unimplemented);
4732 fprintf (dump_file, "Number of miv tests: %d\n",
4733 dependence_stats.num_miv);
4734 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4735 dependence_stats.num_miv_dependent);
4736 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4737 dependence_stats.num_miv_independent);
4738 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4739 dependence_stats.num_miv_unimplemented);
4742 return res;
4745 /* Returns true when the data dependences for the basic block BB have been
4746 computed, false otherwise.
4747 DATAREFS is initialized to all the array elements contained in this basic
4748 block, DEPENDENCE_RELATIONS contains the relations between the data
4749 references. Compute read-read and self relations if
4750 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4751 bool
4752 compute_data_dependences_for_bb (basic_block bb,
4753 bool compute_self_and_read_read_dependences,
4754 vec<data_reference_p> *datarefs,
4755 vec<ddr_p> *dependence_relations)
4757 if (find_data_references_in_bb (NULL, bb, datarefs) == chrec_dont_know)
4758 return false;
4760 return compute_all_dependences (*datarefs, dependence_relations, vNULL,
4761 compute_self_and_read_read_dependences);
4764 /* Entry point (for testing only). Analyze all the data references
4765 and the dependence relations in LOOP.
4767 The data references are computed first.
4769 A relation on these nodes is represented by a complete graph. Some
4770 of the relations could be of no interest, thus the relations can be
4771 computed on demand.
4773 In the following function we compute all the relations. This is
4774 just a first implementation that is here for:
4775 - for showing how to ask for the dependence relations,
4776 - for the debugging the whole dependence graph,
4777 - for the dejagnu testcases and maintenance.
4779 It is possible to ask only for a part of the graph, avoiding to
4780 compute the whole dependence graph. The computed dependences are
4781 stored in a knowledge base (KB) such that later queries don't
4782 recompute the same information. The implementation of this KB is
4783 transparent to the optimizer, and thus the KB can be changed with a
4784 more efficient implementation, or the KB could be disabled. */
4785 static void
4786 analyze_all_data_dependences (struct loop *loop)
4788 unsigned int i;
4789 int nb_data_refs = 10;
4790 vec<data_reference_p> datarefs;
4791 datarefs.create (nb_data_refs);
4792 vec<ddr_p> dependence_relations;
4793 dependence_relations.create (nb_data_refs * nb_data_refs);
4794 vec<loop_p> loop_nest;
4795 loop_nest.create (3);
4797 /* Compute DDs on the whole function. */
4798 compute_data_dependences_for_loop (loop, false, &loop_nest, &datarefs,
4799 &dependence_relations);
4801 if (dump_file)
4803 dump_data_dependence_relations (dump_file, dependence_relations);
4804 fprintf (dump_file, "\n\n");
4806 if (dump_flags & TDF_DETAILS)
4807 dump_dist_dir_vectors (dump_file, dependence_relations);
4809 if (dump_flags & TDF_STATS)
4811 unsigned nb_top_relations = 0;
4812 unsigned nb_bot_relations = 0;
4813 unsigned nb_chrec_relations = 0;
4814 struct data_dependence_relation *ddr;
4816 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4818 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
4819 nb_top_relations++;
4821 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4822 nb_bot_relations++;
4824 else
4825 nb_chrec_relations++;
4828 gather_stats_on_scev_database ();
4832 loop_nest.release ();
4833 free_dependence_relations (dependence_relations);
4834 free_data_refs (datarefs);
4837 /* Computes all the data dependences and check that the results of
4838 several analyzers are the same. */
4840 void
4841 tree_check_data_deps (void)
4843 struct loop *loop_nest;
4845 FOR_EACH_LOOP (loop_nest, 0)
4846 analyze_all_data_dependences (loop_nest);
4849 /* Free the memory used by a data dependence relation DDR. */
4851 void
4852 free_dependence_relation (struct data_dependence_relation *ddr)
4854 if (ddr == NULL)
4855 return;
4857 if (DDR_SUBSCRIPTS (ddr).exists ())
4858 free_subscripts (DDR_SUBSCRIPTS (ddr));
4859 DDR_DIST_VECTS (ddr).release ();
4860 DDR_DIR_VECTS (ddr).release ();
4862 free (ddr);
4865 /* Free the memory used by the data dependence relations from
4866 DEPENDENCE_RELATIONS. */
4868 void
4869 free_dependence_relations (vec<ddr_p> dependence_relations)
4871 unsigned int i;
4872 struct data_dependence_relation *ddr;
4874 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4875 if (ddr)
4876 free_dependence_relation (ddr);
4878 dependence_relations.release ();
4881 /* Free the memory used by the data references from DATAREFS. */
4883 void
4884 free_data_refs (vec<data_reference_p> datarefs)
4886 unsigned int i;
4887 struct data_reference *dr;
4889 FOR_EACH_VEC_ELT (datarefs, i, dr)
4890 free_data_ref (dr);
4891 datarefs.release ();