]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-data-ref.c
tree-core.h: Include symtab.h.
[thirdparty/gcc.git] / gcc / tree-data-ref.c
1 /* Data references and dependences detectors.
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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.
24
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).
30
31 The goals of this analysis are:
32
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),
36
37 - when two data references access the same data, to qualify the
38 dependence relation with classic dependence representations:
39
40 - distance vectors
41 - direction vectors
42 - loop carried level dependence
43 - polyhedron dependence
44 or with the chains of recurrences based representation,
45
46 - to define a knowledge base for storing the data dependence
47 information,
48
49 - to define an interface to access this data.
50
51
52 Definitions:
53
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).
58
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.
63
64 References:
65
66 - "Advanced Compilation for High Performance Computing" by Randy
67 Allen and Ken Kennedy.
68 http://citeseer.ist.psu.edu/goff91practical.html
69
70 - "Loop Transformations for Restructuring Compilers - The Foundations"
71 by Utpal Banerjee.
72
73
74 */
75
76 #include "config.h"
77 #include "system.h"
78 #include "coretypes.h"
79 #include "alias.h"
80 #include "backend.h"
81 #include "tree.h"
82 #include "gimple.h"
83 #include "rtl.h"
84 #include "options.h"
85 #include "fold-const.h"
86 #include "flags.h"
87 #include "insn-config.h"
88 #include "expmed.h"
89 #include "dojump.h"
90 #include "explow.h"
91 #include "calls.h"
92 #include "emit-rtl.h"
93 #include "varasm.h"
94 #include "stmt.h"
95 #include "expr.h"
96 #include "gimple-pretty-print.h"
97 #include "internal-fn.h"
98 #include "gimple-iterator.h"
99 #include "tree-ssa-loop-niter.h"
100 #include "tree-ssa-loop.h"
101 #include "tree-ssa.h"
102 #include "cfgloop.h"
103 #include "tree-data-ref.h"
104 #include "tree-scalar-evolution.h"
105 #include "dumpfile.h"
106 #include "langhooks.h"
107 #include "tree-affine.h"
108 #include "params.h"
109
110 static struct datadep_stats
111 {
112 int num_dependence_tests;
113 int num_dependence_dependent;
114 int num_dependence_independent;
115 int num_dependence_undetermined;
116
117 int num_subscript_tests;
118 int num_subscript_undetermined;
119 int num_same_subscript_function;
120
121 int num_ziv;
122 int num_ziv_independent;
123 int num_ziv_dependent;
124 int num_ziv_unimplemented;
125
126 int num_siv;
127 int num_siv_independent;
128 int num_siv_dependent;
129 int num_siv_unimplemented;
130
131 int num_miv;
132 int num_miv_independent;
133 int num_miv_dependent;
134 int num_miv_unimplemented;
135 } dependence_stats;
136
137 static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
138 struct data_reference *,
139 struct data_reference *,
140 struct loop *);
141 /* Returns true iff A divides B. */
142
143 static inline bool
144 tree_fold_divides_p (const_tree a, const_tree b)
145 {
146 gcc_assert (TREE_CODE (a) == INTEGER_CST);
147 gcc_assert (TREE_CODE (b) == INTEGER_CST);
148 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a));
149 }
150
151 /* Returns true iff A divides B. */
152
153 static inline bool
154 int_divides_p (int a, int b)
155 {
156 return ((b % a) == 0);
157 }
158
159 \f
160
161 /* Dump into FILE all the data references from DATAREFS. */
162
163 static void
164 dump_data_references (FILE *file, vec<data_reference_p> datarefs)
165 {
166 unsigned int i;
167 struct data_reference *dr;
168
169 FOR_EACH_VEC_ELT (datarefs, i, dr)
170 dump_data_reference (file, dr);
171 }
172
173 /* Unified dump into FILE all the data references from DATAREFS. */
174
175 DEBUG_FUNCTION void
176 debug (vec<data_reference_p> &ref)
177 {
178 dump_data_references (stderr, ref);
179 }
180
181 DEBUG_FUNCTION void
182 debug (vec<data_reference_p> *ptr)
183 {
184 if (ptr)
185 debug (*ptr);
186 else
187 fprintf (stderr, "<nil>\n");
188 }
189
190
191 /* Dump into STDERR all the data references from DATAREFS. */
192
193 DEBUG_FUNCTION void
194 debug_data_references (vec<data_reference_p> datarefs)
195 {
196 dump_data_references (stderr, datarefs);
197 }
198
199 /* Print to STDERR the data_reference DR. */
200
201 DEBUG_FUNCTION void
202 debug_data_reference (struct data_reference *dr)
203 {
204 dump_data_reference (stderr, dr);
205 }
206
207 /* Dump function for a DATA_REFERENCE structure. */
208
209 void
210 dump_data_reference (FILE *outf,
211 struct data_reference *dr)
212 {
213 unsigned int i;
214
215 fprintf (outf, "#(Data Ref: \n");
216 fprintf (outf, "# bb: %d \n", gimple_bb (DR_STMT (dr))->index);
217 fprintf (outf, "# stmt: ");
218 print_gimple_stmt (outf, DR_STMT (dr), 0, 0);
219 fprintf (outf, "# ref: ");
220 print_generic_stmt (outf, DR_REF (dr), 0);
221 fprintf (outf, "# base_object: ");
222 print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
223
224 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
225 {
226 fprintf (outf, "# Access function %d: ", i);
227 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
228 }
229 fprintf (outf, "#)\n");
230 }
231
232 /* Unified dump function for a DATA_REFERENCE structure. */
233
234 DEBUG_FUNCTION void
235 debug (data_reference &ref)
236 {
237 dump_data_reference (stderr, &ref);
238 }
239
240 DEBUG_FUNCTION void
241 debug (data_reference *ptr)
242 {
243 if (ptr)
244 debug (*ptr);
245 else
246 fprintf (stderr, "<nil>\n");
247 }
248
249
250 /* Dumps the affine function described by FN to the file OUTF. */
251
252 static void
253 dump_affine_function (FILE *outf, affine_fn fn)
254 {
255 unsigned i;
256 tree coef;
257
258 print_generic_expr (outf, fn[0], TDF_SLIM);
259 for (i = 1; fn.iterate (i, &coef); i++)
260 {
261 fprintf (outf, " + ");
262 print_generic_expr (outf, coef, TDF_SLIM);
263 fprintf (outf, " * x_%u", i);
264 }
265 }
266
267 /* Dumps the conflict function CF to the file OUTF. */
268
269 static void
270 dump_conflict_function (FILE *outf, conflict_function *cf)
271 {
272 unsigned i;
273
274 if (cf->n == NO_DEPENDENCE)
275 fprintf (outf, "no dependence");
276 else if (cf->n == NOT_KNOWN)
277 fprintf (outf, "not known");
278 else
279 {
280 for (i = 0; i < cf->n; i++)
281 {
282 if (i != 0)
283 fprintf (outf, " ");
284 fprintf (outf, "[");
285 dump_affine_function (outf, cf->fns[i]);
286 fprintf (outf, "]");
287 }
288 }
289 }
290
291 /* Dump function for a SUBSCRIPT structure. */
292
293 static void
294 dump_subscript (FILE *outf, struct subscript *subscript)
295 {
296 conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
297
298 fprintf (outf, "\n (subscript \n");
299 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
300 dump_conflict_function (outf, cf);
301 if (CF_NONTRIVIAL_P (cf))
302 {
303 tree last_iteration = SUB_LAST_CONFLICT (subscript);
304 fprintf (outf, "\n last_conflict: ");
305 print_generic_expr (outf, last_iteration, 0);
306 }
307
308 cf = SUB_CONFLICTS_IN_B (subscript);
309 fprintf (outf, "\n iterations_that_access_an_element_twice_in_B: ");
310 dump_conflict_function (outf, cf);
311 if (CF_NONTRIVIAL_P (cf))
312 {
313 tree last_iteration = SUB_LAST_CONFLICT (subscript);
314 fprintf (outf, "\n last_conflict: ");
315 print_generic_expr (outf, last_iteration, 0);
316 }
317
318 fprintf (outf, "\n (Subscript distance: ");
319 print_generic_expr (outf, SUB_DISTANCE (subscript), 0);
320 fprintf (outf, " ))\n");
321 }
322
323 /* Print the classic direction vector DIRV to OUTF. */
324
325 static void
326 print_direction_vector (FILE *outf,
327 lambda_vector dirv,
328 int length)
329 {
330 int eq;
331
332 for (eq = 0; eq < length; eq++)
333 {
334 enum data_dependence_direction dir = ((enum data_dependence_direction)
335 dirv[eq]);
336
337 switch (dir)
338 {
339 case dir_positive:
340 fprintf (outf, " +");
341 break;
342 case dir_negative:
343 fprintf (outf, " -");
344 break;
345 case dir_equal:
346 fprintf (outf, " =");
347 break;
348 case dir_positive_or_equal:
349 fprintf (outf, " +=");
350 break;
351 case dir_positive_or_negative:
352 fprintf (outf, " +-");
353 break;
354 case dir_negative_or_equal:
355 fprintf (outf, " -=");
356 break;
357 case dir_star:
358 fprintf (outf, " *");
359 break;
360 default:
361 fprintf (outf, "indep");
362 break;
363 }
364 }
365 fprintf (outf, "\n");
366 }
367
368 /* Print a vector of direction vectors. */
369
370 static void
371 print_dir_vectors (FILE *outf, vec<lambda_vector> dir_vects,
372 int length)
373 {
374 unsigned j;
375 lambda_vector v;
376
377 FOR_EACH_VEC_ELT (dir_vects, j, v)
378 print_direction_vector (outf, v, length);
379 }
380
381 /* Print out a vector VEC of length N to OUTFILE. */
382
383 static inline void
384 print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
385 {
386 int i;
387
388 for (i = 0; i < n; i++)
389 fprintf (outfile, "%3d ", vector[i]);
390 fprintf (outfile, "\n");
391 }
392
393 /* Print a vector of distance vectors. */
394
395 static void
396 print_dist_vectors (FILE *outf, vec<lambda_vector> dist_vects,
397 int length)
398 {
399 unsigned j;
400 lambda_vector v;
401
402 FOR_EACH_VEC_ELT (dist_vects, j, v)
403 print_lambda_vector (outf, v, length);
404 }
405
406 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
407
408 static void
409 dump_data_dependence_relation (FILE *outf,
410 struct data_dependence_relation *ddr)
411 {
412 struct data_reference *dra, *drb;
413
414 fprintf (outf, "(Data Dep: \n");
415
416 if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
417 {
418 if (ddr)
419 {
420 dra = DDR_A (ddr);
421 drb = DDR_B (ddr);
422 if (dra)
423 dump_data_reference (outf, dra);
424 else
425 fprintf (outf, " (nil)\n");
426 if (drb)
427 dump_data_reference (outf, drb);
428 else
429 fprintf (outf, " (nil)\n");
430 }
431 fprintf (outf, " (don't know)\n)\n");
432 return;
433 }
434
435 dra = DDR_A (ddr);
436 drb = DDR_B (ddr);
437 dump_data_reference (outf, dra);
438 dump_data_reference (outf, drb);
439
440 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
441 fprintf (outf, " (no dependence)\n");
442
443 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
444 {
445 unsigned int i;
446 struct loop *loopi;
447
448 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
449 {
450 fprintf (outf, " access_fn_A: ");
451 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
452 fprintf (outf, " access_fn_B: ");
453 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
454 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
455 }
456
457 fprintf (outf, " inner loop index: %d\n", DDR_INNER_LOOP (ddr));
458 fprintf (outf, " loop nest: (");
459 FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
460 fprintf (outf, "%d ", loopi->num);
461 fprintf (outf, ")\n");
462
463 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
464 {
465 fprintf (outf, " distance_vector: ");
466 print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
467 DDR_NB_LOOPS (ddr));
468 }
469
470 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
471 {
472 fprintf (outf, " direction_vector: ");
473 print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
474 DDR_NB_LOOPS (ddr));
475 }
476 }
477
478 fprintf (outf, ")\n");
479 }
480
481 /* Debug version. */
482
483 DEBUG_FUNCTION void
484 debug_data_dependence_relation (struct data_dependence_relation *ddr)
485 {
486 dump_data_dependence_relation (stderr, ddr);
487 }
488
489 /* Dump into FILE all the dependence relations from DDRS. */
490
491 void
492 dump_data_dependence_relations (FILE *file,
493 vec<ddr_p> ddrs)
494 {
495 unsigned int i;
496 struct data_dependence_relation *ddr;
497
498 FOR_EACH_VEC_ELT (ddrs, i, ddr)
499 dump_data_dependence_relation (file, ddr);
500 }
501
502 DEBUG_FUNCTION void
503 debug (vec<ddr_p> &ref)
504 {
505 dump_data_dependence_relations (stderr, ref);
506 }
507
508 DEBUG_FUNCTION void
509 debug (vec<ddr_p> *ptr)
510 {
511 if (ptr)
512 debug (*ptr);
513 else
514 fprintf (stderr, "<nil>\n");
515 }
516
517
518 /* Dump to STDERR all the dependence relations from DDRS. */
519
520 DEBUG_FUNCTION void
521 debug_data_dependence_relations (vec<ddr_p> ddrs)
522 {
523 dump_data_dependence_relations (stderr, ddrs);
524 }
525
526 /* Dumps the distance and direction vectors in FILE. DDRS contains
527 the dependence relations, and VECT_SIZE is the size of the
528 dependence vectors, or in other words the number of loops in the
529 considered nest. */
530
531 static void
532 dump_dist_dir_vectors (FILE *file, vec<ddr_p> ddrs)
533 {
534 unsigned int i, j;
535 struct data_dependence_relation *ddr;
536 lambda_vector v;
537
538 FOR_EACH_VEC_ELT (ddrs, i, ddr)
539 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
540 {
541 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), j, v)
542 {
543 fprintf (file, "DISTANCE_V (");
544 print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
545 fprintf (file, ")\n");
546 }
547
548 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), j, v)
549 {
550 fprintf (file, "DIRECTION_V (");
551 print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
552 fprintf (file, ")\n");
553 }
554 }
555
556 fprintf (file, "\n\n");
557 }
558
559 /* Dumps the data dependence relations DDRS in FILE. */
560
561 static void
562 dump_ddrs (FILE *file, vec<ddr_p> ddrs)
563 {
564 unsigned int i;
565 struct data_dependence_relation *ddr;
566
567 FOR_EACH_VEC_ELT (ddrs, i, ddr)
568 dump_data_dependence_relation (file, ddr);
569
570 fprintf (file, "\n\n");
571 }
572
573 DEBUG_FUNCTION void
574 debug_ddrs (vec<ddr_p> ddrs)
575 {
576 dump_ddrs (stderr, ddrs);
577 }
578
579 /* Helper function for split_constant_offset. Expresses OP0 CODE OP1
580 (the type of the result is TYPE) as VAR + OFF, where OFF is a nonzero
581 constant of type ssizetype, and returns true. If we cannot do this
582 with OFF nonzero, OFF and VAR are set to NULL_TREE instead and false
583 is returned. */
584
585 static bool
586 split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
587 tree *var, tree *off)
588 {
589 tree var0, var1;
590 tree off0, off1;
591 enum tree_code ocode = code;
592
593 *var = NULL_TREE;
594 *off = NULL_TREE;
595
596 switch (code)
597 {
598 case INTEGER_CST:
599 *var = build_int_cst (type, 0);
600 *off = fold_convert (ssizetype, op0);
601 return true;
602
603 case POINTER_PLUS_EXPR:
604 ocode = PLUS_EXPR;
605 /* FALLTHROUGH */
606 case PLUS_EXPR:
607 case MINUS_EXPR:
608 split_constant_offset (op0, &var0, &off0);
609 split_constant_offset (op1, &var1, &off1);
610 *var = fold_build2 (code, type, var0, var1);
611 *off = size_binop (ocode, off0, off1);
612 return true;
613
614 case MULT_EXPR:
615 if (TREE_CODE (op1) != INTEGER_CST)
616 return false;
617
618 split_constant_offset (op0, &var0, &off0);
619 *var = fold_build2 (MULT_EXPR, type, var0, op1);
620 *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
621 return true;
622
623 case ADDR_EXPR:
624 {
625 tree base, poffset;
626 HOST_WIDE_INT pbitsize, pbitpos;
627 machine_mode pmode;
628 int punsignedp, pvolatilep;
629
630 op0 = TREE_OPERAND (op0, 0);
631 base = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset,
632 &pmode, &punsignedp, &pvolatilep, false);
633
634 if (pbitpos % BITS_PER_UNIT != 0)
635 return false;
636 base = build_fold_addr_expr (base);
637 off0 = ssize_int (pbitpos / BITS_PER_UNIT);
638
639 if (poffset)
640 {
641 split_constant_offset (poffset, &poffset, &off1);
642 off0 = size_binop (PLUS_EXPR, off0, off1);
643 if (POINTER_TYPE_P (TREE_TYPE (base)))
644 base = fold_build_pointer_plus (base, poffset);
645 else
646 base = fold_build2 (PLUS_EXPR, TREE_TYPE (base), base,
647 fold_convert (TREE_TYPE (base), poffset));
648 }
649
650 var0 = fold_convert (type, base);
651
652 /* If variable length types are involved, punt, otherwise casts
653 might be converted into ARRAY_REFs in gimplify_conversion.
654 To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
655 possibly no longer appears in current GIMPLE, might resurface.
656 This perhaps could run
657 if (CONVERT_EXPR_P (var0))
658 {
659 gimplify_conversion (&var0);
660 // Attempt to fill in any within var0 found ARRAY_REF's
661 // element size from corresponding op embedded ARRAY_REF,
662 // if unsuccessful, just punt.
663 } */
664 while (POINTER_TYPE_P (type))
665 type = TREE_TYPE (type);
666 if (int_size_in_bytes (type) < 0)
667 return false;
668
669 *var = var0;
670 *off = off0;
671 return true;
672 }
673
674 case SSA_NAME:
675 {
676 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
677 return false;
678
679 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
680 enum tree_code subcode;
681
682 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
683 return false;
684
685 var0 = gimple_assign_rhs1 (def_stmt);
686 subcode = gimple_assign_rhs_code (def_stmt);
687 var1 = gimple_assign_rhs2 (def_stmt);
688
689 return split_constant_offset_1 (type, var0, subcode, var1, var, off);
690 }
691 CASE_CONVERT:
692 {
693 /* We must not introduce undefined overflow, and we must not change the value.
694 Hence we're okay if the inner type doesn't overflow to start with
695 (pointer or signed), the outer type also is an integer or pointer
696 and the outer precision is at least as large as the inner. */
697 tree itype = TREE_TYPE (op0);
698 if ((POINTER_TYPE_P (itype)
699 || (INTEGRAL_TYPE_P (itype) && TYPE_OVERFLOW_UNDEFINED (itype)))
700 && TYPE_PRECISION (type) >= TYPE_PRECISION (itype)
701 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)))
702 {
703 split_constant_offset (op0, &var0, off);
704 *var = fold_convert (type, var0);
705 return true;
706 }
707 return false;
708 }
709
710 default:
711 return false;
712 }
713 }
714
715 /* Expresses EXP as VAR + OFF, where off is a constant. The type of OFF
716 will be ssizetype. */
717
718 void
719 split_constant_offset (tree exp, tree *var, tree *off)
720 {
721 tree type = TREE_TYPE (exp), otype, op0, op1, e, o;
722 enum tree_code code;
723
724 *var = exp;
725 *off = ssize_int (0);
726 STRIP_NOPS (exp);
727
728 if (tree_is_chrec (exp)
729 || get_gimple_rhs_class (TREE_CODE (exp)) == GIMPLE_TERNARY_RHS)
730 return;
731
732 otype = TREE_TYPE (exp);
733 code = TREE_CODE (exp);
734 extract_ops_from_tree (exp, &code, &op0, &op1);
735 if (split_constant_offset_1 (otype, op0, code, op1, &e, &o))
736 {
737 *var = fold_convert (type, e);
738 *off = o;
739 }
740 }
741
742 /* Returns the address ADDR of an object in a canonical shape (without nop
743 casts, and with type of pointer to the object). */
744
745 static tree
746 canonicalize_base_object_address (tree addr)
747 {
748 tree orig = addr;
749
750 STRIP_NOPS (addr);
751
752 /* The base address may be obtained by casting from integer, in that case
753 keep the cast. */
754 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
755 return orig;
756
757 if (TREE_CODE (addr) != ADDR_EXPR)
758 return addr;
759
760 return build_fold_addr_expr (TREE_OPERAND (addr, 0));
761 }
762
763 /* Analyzes the behavior of the memory reference DR in the innermost loop or
764 basic block that contains it. Returns true if analysis succeed or false
765 otherwise. */
766
767 bool
768 dr_analyze_innermost (struct data_reference *dr, struct loop *nest)
769 {
770 gimple stmt = DR_STMT (dr);
771 struct loop *loop = loop_containing_stmt (stmt);
772 tree ref = DR_REF (dr);
773 HOST_WIDE_INT pbitsize, pbitpos;
774 tree base, poffset;
775 machine_mode pmode;
776 int punsignedp, pvolatilep;
777 affine_iv base_iv, offset_iv;
778 tree init, dinit, step;
779 bool in_loop = (loop && loop->num);
780
781 if (dump_file && (dump_flags & TDF_DETAILS))
782 fprintf (dump_file, "analyze_innermost: ");
783
784 base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset,
785 &pmode, &punsignedp, &pvolatilep, false);
786 gcc_assert (base != NULL_TREE);
787
788 if (pbitpos % BITS_PER_UNIT != 0)
789 {
790 if (dump_file && (dump_flags & TDF_DETAILS))
791 fprintf (dump_file, "failed: bit offset alignment.\n");
792 return false;
793 }
794
795 if (TREE_CODE (base) == MEM_REF)
796 {
797 if (!integer_zerop (TREE_OPERAND (base, 1)))
798 {
799 offset_int moff = mem_ref_offset (base);
800 tree mofft = wide_int_to_tree (sizetype, moff);
801 if (!poffset)
802 poffset = mofft;
803 else
804 poffset = size_binop (PLUS_EXPR, poffset, mofft);
805 }
806 base = TREE_OPERAND (base, 0);
807 }
808 else
809 base = build_fold_addr_expr (base);
810
811 if (in_loop)
812 {
813 if (!simple_iv (loop, loop_containing_stmt (stmt), base, &base_iv,
814 nest ? true : false))
815 {
816 if (nest)
817 {
818 if (dump_file && (dump_flags & TDF_DETAILS))
819 fprintf (dump_file, "failed: evolution of base is not"
820 " affine.\n");
821 return false;
822 }
823 else
824 {
825 base_iv.base = base;
826 base_iv.step = ssize_int (0);
827 base_iv.no_overflow = true;
828 }
829 }
830 }
831 else
832 {
833 base_iv.base = base;
834 base_iv.step = ssize_int (0);
835 base_iv.no_overflow = true;
836 }
837
838 if (!poffset)
839 {
840 offset_iv.base = ssize_int (0);
841 offset_iv.step = ssize_int (0);
842 }
843 else
844 {
845 if (!in_loop)
846 {
847 offset_iv.base = poffset;
848 offset_iv.step = ssize_int (0);
849 }
850 else if (!simple_iv (loop, loop_containing_stmt (stmt),
851 poffset, &offset_iv,
852 nest ? true : false))
853 {
854 if (nest)
855 {
856 if (dump_file && (dump_flags & TDF_DETAILS))
857 fprintf (dump_file, "failed: evolution of offset is not"
858 " affine.\n");
859 return false;
860 }
861 else
862 {
863 offset_iv.base = poffset;
864 offset_iv.step = ssize_int (0);
865 }
866 }
867 }
868
869 init = ssize_int (pbitpos / BITS_PER_UNIT);
870 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
871 init = size_binop (PLUS_EXPR, init, dinit);
872 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
873 init = size_binop (PLUS_EXPR, init, dinit);
874
875 step = size_binop (PLUS_EXPR,
876 fold_convert (ssizetype, base_iv.step),
877 fold_convert (ssizetype, offset_iv.step));
878
879 DR_BASE_ADDRESS (dr) = canonicalize_base_object_address (base_iv.base);
880
881 DR_OFFSET (dr) = fold_convert (ssizetype, offset_iv.base);
882 DR_INIT (dr) = init;
883 DR_STEP (dr) = step;
884
885 DR_ALIGNED_TO (dr) = size_int (highest_pow2_factor (offset_iv.base));
886
887 if (dump_file && (dump_flags & TDF_DETAILS))
888 fprintf (dump_file, "success.\n");
889
890 return true;
891 }
892
893 /* Determines the base object and the list of indices of memory reference
894 DR, analyzed in LOOP and instantiated in loop nest NEST. */
895
896 static void
897 dr_analyze_indices (struct data_reference *dr, loop_p nest, loop_p loop)
898 {
899 vec<tree> access_fns = vNULL;
900 tree ref, op;
901 tree base, off, access_fn;
902 basic_block before_loop;
903
904 /* If analyzing a basic-block there are no indices to analyze
905 and thus no access functions. */
906 if (!nest)
907 {
908 DR_BASE_OBJECT (dr) = DR_REF (dr);
909 DR_ACCESS_FNS (dr).create (0);
910 return;
911 }
912
913 ref = DR_REF (dr);
914 before_loop = block_before_loop (nest);
915
916 /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
917 into a two element array with a constant index. The base is
918 then just the immediate underlying object. */
919 if (TREE_CODE (ref) == REALPART_EXPR)
920 {
921 ref = TREE_OPERAND (ref, 0);
922 access_fns.safe_push (integer_zero_node);
923 }
924 else if (TREE_CODE (ref) == IMAGPART_EXPR)
925 {
926 ref = TREE_OPERAND (ref, 0);
927 access_fns.safe_push (integer_one_node);
928 }
929
930 /* Analyze access functions of dimensions we know to be independent. */
931 while (handled_component_p (ref))
932 {
933 if (TREE_CODE (ref) == ARRAY_REF)
934 {
935 op = TREE_OPERAND (ref, 1);
936 access_fn = analyze_scalar_evolution (loop, op);
937 access_fn = instantiate_scev (before_loop, loop, access_fn);
938 access_fns.safe_push (access_fn);
939 }
940 else if (TREE_CODE (ref) == COMPONENT_REF
941 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
942 {
943 /* For COMPONENT_REFs of records (but not unions!) use the
944 FIELD_DECL offset as constant access function so we can
945 disambiguate a[i].f1 and a[i].f2. */
946 tree off = component_ref_field_offset (ref);
947 off = size_binop (PLUS_EXPR,
948 size_binop (MULT_EXPR,
949 fold_convert (bitsizetype, off),
950 bitsize_int (BITS_PER_UNIT)),
951 DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
952 access_fns.safe_push (off);
953 }
954 else
955 /* If we have an unhandled component we could not translate
956 to an access function stop analyzing. We have determined
957 our base object in this case. */
958 break;
959
960 ref = TREE_OPERAND (ref, 0);
961 }
962
963 /* If the address operand of a MEM_REF base has an evolution in the
964 analyzed nest, add it as an additional independent access-function. */
965 if (TREE_CODE (ref) == MEM_REF)
966 {
967 op = TREE_OPERAND (ref, 0);
968 access_fn = analyze_scalar_evolution (loop, op);
969 access_fn = instantiate_scev (before_loop, loop, access_fn);
970 if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
971 {
972 tree orig_type;
973 tree memoff = TREE_OPERAND (ref, 1);
974 base = initial_condition (access_fn);
975 orig_type = TREE_TYPE (base);
976 STRIP_USELESS_TYPE_CONVERSION (base);
977 split_constant_offset (base, &base, &off);
978 STRIP_USELESS_TYPE_CONVERSION (base);
979 /* Fold the MEM_REF offset into the evolutions initial
980 value to make more bases comparable. */
981 if (!integer_zerop (memoff))
982 {
983 off = size_binop (PLUS_EXPR, off,
984 fold_convert (ssizetype, memoff));
985 memoff = build_int_cst (TREE_TYPE (memoff), 0);
986 }
987 /* Adjust the offset so it is a multiple of the access type
988 size and thus we separate bases that can possibly be used
989 to produce partial overlaps (which the access_fn machinery
990 cannot handle). */
991 wide_int rem;
992 if (TYPE_SIZE_UNIT (TREE_TYPE (ref))
993 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref))) == INTEGER_CST
994 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (ref))))
995 rem = wi::mod_trunc (off, TYPE_SIZE_UNIT (TREE_TYPE (ref)), SIGNED);
996 else
997 /* If we can't compute the remainder simply force the initial
998 condition to zero. */
999 rem = off;
1000 off = wide_int_to_tree (ssizetype, wi::sub (off, rem));
1001 memoff = wide_int_to_tree (TREE_TYPE (memoff), rem);
1002 /* And finally replace the initial condition. */
1003 access_fn = chrec_replace_initial_condition
1004 (access_fn, fold_convert (orig_type, off));
1005 /* ??? This is still not a suitable base object for
1006 dr_may_alias_p - the base object needs to be an
1007 access that covers the object as whole. With
1008 an evolution in the pointer this cannot be
1009 guaranteed.
1010 As a band-aid, mark the access so we can special-case
1011 it in dr_may_alias_p. */
1012 tree old = ref;
1013 ref = fold_build2_loc (EXPR_LOCATION (ref),
1014 MEM_REF, TREE_TYPE (ref),
1015 base, memoff);
1016 MR_DEPENDENCE_CLIQUE (ref) = MR_DEPENDENCE_CLIQUE (old);
1017 MR_DEPENDENCE_BASE (ref) = MR_DEPENDENCE_BASE (old);
1018 DR_UNCONSTRAINED_BASE (dr) = true;
1019 access_fns.safe_push (access_fn);
1020 }
1021 }
1022 else if (DECL_P (ref))
1023 {
1024 /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
1025 ref = build2 (MEM_REF, TREE_TYPE (ref),
1026 build_fold_addr_expr (ref),
1027 build_int_cst (reference_alias_ptr_type (ref), 0));
1028 }
1029
1030 DR_BASE_OBJECT (dr) = ref;
1031 DR_ACCESS_FNS (dr) = access_fns;
1032 }
1033
1034 /* Extracts the alias analysis information from the memory reference DR. */
1035
1036 static void
1037 dr_analyze_alias (struct data_reference *dr)
1038 {
1039 tree ref = DR_REF (dr);
1040 tree base = get_base_address (ref), addr;
1041
1042 if (INDIRECT_REF_P (base)
1043 || TREE_CODE (base) == MEM_REF)
1044 {
1045 addr = TREE_OPERAND (base, 0);
1046 if (TREE_CODE (addr) == SSA_NAME)
1047 DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
1048 }
1049 }
1050
1051 /* Frees data reference DR. */
1052
1053 void
1054 free_data_ref (data_reference_p dr)
1055 {
1056 DR_ACCESS_FNS (dr).release ();
1057 free (dr);
1058 }
1059
1060 /* Analyzes memory reference MEMREF accessed in STMT. The reference
1061 is read if IS_READ is true, write otherwise. Returns the
1062 data_reference description of MEMREF. NEST is the outermost loop
1063 in which the reference should be instantiated, LOOP is the loop in
1064 which the data reference should be analyzed. */
1065
1066 struct data_reference *
1067 create_data_ref (loop_p nest, loop_p loop, tree memref, gimple stmt,
1068 bool is_read)
1069 {
1070 struct data_reference *dr;
1071
1072 if (dump_file && (dump_flags & TDF_DETAILS))
1073 {
1074 fprintf (dump_file, "Creating dr for ");
1075 print_generic_expr (dump_file, memref, TDF_SLIM);
1076 fprintf (dump_file, "\n");
1077 }
1078
1079 dr = XCNEW (struct data_reference);
1080 DR_STMT (dr) = stmt;
1081 DR_REF (dr) = memref;
1082 DR_IS_READ (dr) = is_read;
1083
1084 dr_analyze_innermost (dr, nest);
1085 dr_analyze_indices (dr, nest, loop);
1086 dr_analyze_alias (dr);
1087
1088 if (dump_file && (dump_flags & TDF_DETAILS))
1089 {
1090 unsigned i;
1091 fprintf (dump_file, "\tbase_address: ");
1092 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1093 fprintf (dump_file, "\n\toffset from base address: ");
1094 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1095 fprintf (dump_file, "\n\tconstant offset from base address: ");
1096 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1097 fprintf (dump_file, "\n\tstep: ");
1098 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1099 fprintf (dump_file, "\n\taligned to: ");
1100 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
1101 fprintf (dump_file, "\n\tbase_object: ");
1102 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1103 fprintf (dump_file, "\n");
1104 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
1105 {
1106 fprintf (dump_file, "\tAccess function %d: ", i);
1107 print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
1108 }
1109 }
1110
1111 return dr;
1112 }
1113
1114 /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
1115 expressions. */
1116 static bool
1117 dr_equal_offsets_p1 (tree offset1, tree offset2)
1118 {
1119 bool res;
1120
1121 STRIP_NOPS (offset1);
1122 STRIP_NOPS (offset2);
1123
1124 if (offset1 == offset2)
1125 return true;
1126
1127 if (TREE_CODE (offset1) != TREE_CODE (offset2)
1128 || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
1129 return false;
1130
1131 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
1132 TREE_OPERAND (offset2, 0));
1133
1134 if (!res || !BINARY_CLASS_P (offset1))
1135 return res;
1136
1137 res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
1138 TREE_OPERAND (offset2, 1));
1139
1140 return res;
1141 }
1142
1143 /* Check if DRA and DRB have equal offsets. */
1144 bool
1145 dr_equal_offsets_p (struct data_reference *dra,
1146 struct data_reference *drb)
1147 {
1148 tree offset1, offset2;
1149
1150 offset1 = DR_OFFSET (dra);
1151 offset2 = DR_OFFSET (drb);
1152
1153 return dr_equal_offsets_p1 (offset1, offset2);
1154 }
1155
1156 /* Returns true if FNA == FNB. */
1157
1158 static bool
1159 affine_function_equal_p (affine_fn fna, affine_fn fnb)
1160 {
1161 unsigned i, n = fna.length ();
1162
1163 if (n != fnb.length ())
1164 return false;
1165
1166 for (i = 0; i < n; i++)
1167 if (!operand_equal_p (fna[i], fnb[i], 0))
1168 return false;
1169
1170 return true;
1171 }
1172
1173 /* If all the functions in CF are the same, returns one of them,
1174 otherwise returns NULL. */
1175
1176 static affine_fn
1177 common_affine_function (conflict_function *cf)
1178 {
1179 unsigned i;
1180 affine_fn comm;
1181
1182 if (!CF_NONTRIVIAL_P (cf))
1183 return affine_fn ();
1184
1185 comm = cf->fns[0];
1186
1187 for (i = 1; i < cf->n; i++)
1188 if (!affine_function_equal_p (comm, cf->fns[i]))
1189 return affine_fn ();
1190
1191 return comm;
1192 }
1193
1194 /* Returns the base of the affine function FN. */
1195
1196 static tree
1197 affine_function_base (affine_fn fn)
1198 {
1199 return fn[0];
1200 }
1201
1202 /* Returns true if FN is a constant. */
1203
1204 static bool
1205 affine_function_constant_p (affine_fn fn)
1206 {
1207 unsigned i;
1208 tree coef;
1209
1210 for (i = 1; fn.iterate (i, &coef); i++)
1211 if (!integer_zerop (coef))
1212 return false;
1213
1214 return true;
1215 }
1216
1217 /* Returns true if FN is the zero constant function. */
1218
1219 static bool
1220 affine_function_zero_p (affine_fn fn)
1221 {
1222 return (integer_zerop (affine_function_base (fn))
1223 && affine_function_constant_p (fn));
1224 }
1225
1226 /* Returns a signed integer type with the largest precision from TA
1227 and TB. */
1228
1229 static tree
1230 signed_type_for_types (tree ta, tree tb)
1231 {
1232 if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
1233 return signed_type_for (ta);
1234 else
1235 return signed_type_for (tb);
1236 }
1237
1238 /* Applies operation OP on affine functions FNA and FNB, and returns the
1239 result. */
1240
1241 static affine_fn
1242 affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
1243 {
1244 unsigned i, n, m;
1245 affine_fn ret;
1246 tree coef;
1247
1248 if (fnb.length () > fna.length ())
1249 {
1250 n = fna.length ();
1251 m = fnb.length ();
1252 }
1253 else
1254 {
1255 n = fnb.length ();
1256 m = fna.length ();
1257 }
1258
1259 ret.create (m);
1260 for (i = 0; i < n; i++)
1261 {
1262 tree type = signed_type_for_types (TREE_TYPE (fna[i]),
1263 TREE_TYPE (fnb[i]));
1264 ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
1265 }
1266
1267 for (; fna.iterate (i, &coef); i++)
1268 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1269 coef, integer_zero_node));
1270 for (; fnb.iterate (i, &coef); i++)
1271 ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
1272 integer_zero_node, coef));
1273
1274 return ret;
1275 }
1276
1277 /* Returns the sum of affine functions FNA and FNB. */
1278
1279 static affine_fn
1280 affine_fn_plus (affine_fn fna, affine_fn fnb)
1281 {
1282 return affine_fn_op (PLUS_EXPR, fna, fnb);
1283 }
1284
1285 /* Returns the difference of affine functions FNA and FNB. */
1286
1287 static affine_fn
1288 affine_fn_minus (affine_fn fna, affine_fn fnb)
1289 {
1290 return affine_fn_op (MINUS_EXPR, fna, fnb);
1291 }
1292
1293 /* Frees affine function FN. */
1294
1295 static void
1296 affine_fn_free (affine_fn fn)
1297 {
1298 fn.release ();
1299 }
1300
1301 /* Determine for each subscript in the data dependence relation DDR
1302 the distance. */
1303
1304 static void
1305 compute_subscript_distance (struct data_dependence_relation *ddr)
1306 {
1307 conflict_function *cf_a, *cf_b;
1308 affine_fn fn_a, fn_b, diff;
1309
1310 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1311 {
1312 unsigned int i;
1313
1314 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1315 {
1316 struct subscript *subscript;
1317
1318 subscript = DDR_SUBSCRIPT (ddr, i);
1319 cf_a = SUB_CONFLICTS_IN_A (subscript);
1320 cf_b = SUB_CONFLICTS_IN_B (subscript);
1321
1322 fn_a = common_affine_function (cf_a);
1323 fn_b = common_affine_function (cf_b);
1324 if (!fn_a.exists () || !fn_b.exists ())
1325 {
1326 SUB_DISTANCE (subscript) = chrec_dont_know;
1327 return;
1328 }
1329 diff = affine_fn_minus (fn_a, fn_b);
1330
1331 if (affine_function_constant_p (diff))
1332 SUB_DISTANCE (subscript) = affine_function_base (diff);
1333 else
1334 SUB_DISTANCE (subscript) = chrec_dont_know;
1335
1336 affine_fn_free (diff);
1337 }
1338 }
1339 }
1340
1341 /* Returns the conflict function for "unknown". */
1342
1343 static conflict_function *
1344 conflict_fn_not_known (void)
1345 {
1346 conflict_function *fn = XCNEW (conflict_function);
1347 fn->n = NOT_KNOWN;
1348
1349 return fn;
1350 }
1351
1352 /* Returns the conflict function for "independent". */
1353
1354 static conflict_function *
1355 conflict_fn_no_dependence (void)
1356 {
1357 conflict_function *fn = XCNEW (conflict_function);
1358 fn->n = NO_DEPENDENCE;
1359
1360 return fn;
1361 }
1362
1363 /* Returns true if the address of OBJ is invariant in LOOP. */
1364
1365 static bool
1366 object_address_invariant_in_loop_p (const struct loop *loop, const_tree obj)
1367 {
1368 while (handled_component_p (obj))
1369 {
1370 if (TREE_CODE (obj) == ARRAY_REF)
1371 {
1372 /* Index of the ARRAY_REF was zeroed in analyze_indices, thus we only
1373 need to check the stride and the lower bound of the reference. */
1374 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1375 loop->num)
1376 || chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 3),
1377 loop->num))
1378 return false;
1379 }
1380 else if (TREE_CODE (obj) == COMPONENT_REF)
1381 {
1382 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
1383 loop->num))
1384 return false;
1385 }
1386 obj = TREE_OPERAND (obj, 0);
1387 }
1388
1389 if (!INDIRECT_REF_P (obj)
1390 && TREE_CODE (obj) != MEM_REF)
1391 return true;
1392
1393 return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
1394 loop->num);
1395 }
1396
1397 /* Returns false if we can prove that data references A and B do not alias,
1398 true otherwise. If LOOP_NEST is false no cross-iteration aliases are
1399 considered. */
1400
1401 bool
1402 dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
1403 bool loop_nest)
1404 {
1405 tree addr_a = DR_BASE_OBJECT (a);
1406 tree addr_b = DR_BASE_OBJECT (b);
1407
1408 /* If we are not processing a loop nest but scalar code we
1409 do not need to care about possible cross-iteration dependences
1410 and thus can process the full original reference. Do so,
1411 similar to how loop invariant motion applies extra offset-based
1412 disambiguation. */
1413 if (!loop_nest)
1414 {
1415 aff_tree off1, off2;
1416 widest_int size1, size2;
1417 get_inner_reference_aff (DR_REF (a), &off1, &size1);
1418 get_inner_reference_aff (DR_REF (b), &off2, &size2);
1419 aff_combination_scale (&off1, -1);
1420 aff_combination_add (&off2, &off1);
1421 if (aff_comb_cannot_overlap_p (&off2, size1, size2))
1422 return false;
1423 }
1424
1425 if ((TREE_CODE (addr_a) == MEM_REF || TREE_CODE (addr_a) == TARGET_MEM_REF)
1426 && (TREE_CODE (addr_b) == MEM_REF || TREE_CODE (addr_b) == TARGET_MEM_REF)
1427 && MR_DEPENDENCE_CLIQUE (addr_a) == MR_DEPENDENCE_CLIQUE (addr_b)
1428 && MR_DEPENDENCE_BASE (addr_a) != MR_DEPENDENCE_BASE (addr_b))
1429 return false;
1430
1431 /* If we had an evolution in a pointer-based MEM_REF BASE_OBJECT we
1432 do not know the size of the base-object. So we cannot do any
1433 offset/overlap based analysis but have to rely on points-to
1434 information only. */
1435 if (TREE_CODE (addr_a) == MEM_REF
1436 && (DR_UNCONSTRAINED_BASE (a)
1437 || TREE_CODE (TREE_OPERAND (addr_a, 0)) == SSA_NAME))
1438 {
1439 /* For true dependences we can apply TBAA. */
1440 if (flag_strict_aliasing
1441 && DR_IS_WRITE (a) && DR_IS_READ (b)
1442 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1443 get_alias_set (DR_REF (b))))
1444 return false;
1445 if (TREE_CODE (addr_b) == MEM_REF)
1446 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1447 TREE_OPERAND (addr_b, 0));
1448 else
1449 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1450 build_fold_addr_expr (addr_b));
1451 }
1452 else if (TREE_CODE (addr_b) == MEM_REF
1453 && (DR_UNCONSTRAINED_BASE (b)
1454 || TREE_CODE (TREE_OPERAND (addr_b, 0)) == SSA_NAME))
1455 {
1456 /* For true dependences we can apply TBAA. */
1457 if (flag_strict_aliasing
1458 && DR_IS_WRITE (a) && DR_IS_READ (b)
1459 && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
1460 get_alias_set (DR_REF (b))))
1461 return false;
1462 if (TREE_CODE (addr_a) == MEM_REF)
1463 return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
1464 TREE_OPERAND (addr_b, 0));
1465 else
1466 return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
1467 TREE_OPERAND (addr_b, 0));
1468 }
1469
1470 /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
1471 that is being subsetted in the loop nest. */
1472 if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
1473 return refs_output_dependent_p (addr_a, addr_b);
1474 else if (DR_IS_READ (a) && DR_IS_WRITE (b))
1475 return refs_anti_dependent_p (addr_a, addr_b);
1476 return refs_may_alias_p (addr_a, addr_b);
1477 }
1478
1479 /* Initialize a data dependence relation between data accesses A and
1480 B. NB_LOOPS is the number of loops surrounding the references: the
1481 size of the classic distance/direction vectors. */
1482
1483 struct data_dependence_relation *
1484 initialize_data_dependence_relation (struct data_reference *a,
1485 struct data_reference *b,
1486 vec<loop_p> loop_nest)
1487 {
1488 struct data_dependence_relation *res;
1489 unsigned int i;
1490
1491 res = XNEW (struct data_dependence_relation);
1492 DDR_A (res) = a;
1493 DDR_B (res) = b;
1494 DDR_LOOP_NEST (res).create (0);
1495 DDR_REVERSED_P (res) = false;
1496 DDR_SUBSCRIPTS (res).create (0);
1497 DDR_DIR_VECTS (res).create (0);
1498 DDR_DIST_VECTS (res).create (0);
1499
1500 if (a == NULL || b == NULL)
1501 {
1502 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1503 return res;
1504 }
1505
1506 /* If the data references do not alias, then they are independent. */
1507 if (!dr_may_alias_p (a, b, loop_nest.exists ()))
1508 {
1509 DDR_ARE_DEPENDENT (res) = chrec_known;
1510 return res;
1511 }
1512
1513 /* The case where the references are exactly the same. */
1514 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
1515 {
1516 if (loop_nest.exists ()
1517 && !object_address_invariant_in_loop_p (loop_nest[0],
1518 DR_BASE_OBJECT (a)))
1519 {
1520 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1521 return res;
1522 }
1523 DDR_AFFINE_P (res) = true;
1524 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1525 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1526 DDR_LOOP_NEST (res) = loop_nest;
1527 DDR_INNER_LOOP (res) = 0;
1528 DDR_SELF_REFERENCE (res) = true;
1529 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1530 {
1531 struct subscript *subscript;
1532
1533 subscript = XNEW (struct subscript);
1534 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1535 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1536 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1537 SUB_DISTANCE (subscript) = chrec_dont_know;
1538 DDR_SUBSCRIPTS (res).safe_push (subscript);
1539 }
1540 return res;
1541 }
1542
1543 /* If the references do not access the same object, we do not know
1544 whether they alias or not. */
1545 if (!operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0))
1546 {
1547 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1548 return res;
1549 }
1550
1551 /* If the base of the object is not invariant in the loop nest, we cannot
1552 analyze it. TODO -- in fact, it would suffice to record that there may
1553 be arbitrary dependences in the loops where the base object varies. */
1554 if (loop_nest.exists ()
1555 && !object_address_invariant_in_loop_p (loop_nest[0],
1556 DR_BASE_OBJECT (a)))
1557 {
1558 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1559 return res;
1560 }
1561
1562 /* If the number of dimensions of the access to not agree we can have
1563 a pointer access to a component of the array element type and an
1564 array access while the base-objects are still the same. Punt. */
1565 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1566 {
1567 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1568 return res;
1569 }
1570
1571 DDR_AFFINE_P (res) = true;
1572 DDR_ARE_DEPENDENT (res) = NULL_TREE;
1573 DDR_SUBSCRIPTS (res).create (DR_NUM_DIMENSIONS (a));
1574 DDR_LOOP_NEST (res) = loop_nest;
1575 DDR_INNER_LOOP (res) = 0;
1576 DDR_SELF_REFERENCE (res) = false;
1577
1578 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
1579 {
1580 struct subscript *subscript;
1581
1582 subscript = XNEW (struct subscript);
1583 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
1584 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
1585 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
1586 SUB_DISTANCE (subscript) = chrec_dont_know;
1587 DDR_SUBSCRIPTS (res).safe_push (subscript);
1588 }
1589
1590 return res;
1591 }
1592
1593 /* Frees memory used by the conflict function F. */
1594
1595 static void
1596 free_conflict_function (conflict_function *f)
1597 {
1598 unsigned i;
1599
1600 if (CF_NONTRIVIAL_P (f))
1601 {
1602 for (i = 0; i < f->n; i++)
1603 affine_fn_free (f->fns[i]);
1604 }
1605 free (f);
1606 }
1607
1608 /* Frees memory used by SUBSCRIPTS. */
1609
1610 static void
1611 free_subscripts (vec<subscript_p> subscripts)
1612 {
1613 unsigned i;
1614 subscript_p s;
1615
1616 FOR_EACH_VEC_ELT (subscripts, i, s)
1617 {
1618 free_conflict_function (s->conflicting_iterations_in_a);
1619 free_conflict_function (s->conflicting_iterations_in_b);
1620 free (s);
1621 }
1622 subscripts.release ();
1623 }
1624
1625 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
1626 description. */
1627
1628 static inline void
1629 finalize_ddr_dependent (struct data_dependence_relation *ddr,
1630 tree chrec)
1631 {
1632 DDR_ARE_DEPENDENT (ddr) = chrec;
1633 free_subscripts (DDR_SUBSCRIPTS (ddr));
1634 DDR_SUBSCRIPTS (ddr).create (0);
1635 }
1636
1637 /* The dependence relation DDR cannot be represented by a distance
1638 vector. */
1639
1640 static inline void
1641 non_affine_dependence_relation (struct data_dependence_relation *ddr)
1642 {
1643 if (dump_file && (dump_flags & TDF_DETAILS))
1644 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
1645
1646 DDR_AFFINE_P (ddr) = false;
1647 }
1648
1649 \f
1650
1651 /* This section contains the classic Banerjee tests. */
1652
1653 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
1654 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
1655
1656 static inline bool
1657 ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1658 {
1659 return (evolution_function_is_constant_p (chrec_a)
1660 && evolution_function_is_constant_p (chrec_b));
1661 }
1662
1663 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
1664 variable, i.e., if the SIV (Single Index Variable) test is true. */
1665
1666 static bool
1667 siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
1668 {
1669 if ((evolution_function_is_constant_p (chrec_a)
1670 && evolution_function_is_univariate_p (chrec_b))
1671 || (evolution_function_is_constant_p (chrec_b)
1672 && evolution_function_is_univariate_p (chrec_a)))
1673 return true;
1674
1675 if (evolution_function_is_univariate_p (chrec_a)
1676 && evolution_function_is_univariate_p (chrec_b))
1677 {
1678 switch (TREE_CODE (chrec_a))
1679 {
1680 case POLYNOMIAL_CHREC:
1681 switch (TREE_CODE (chrec_b))
1682 {
1683 case POLYNOMIAL_CHREC:
1684 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
1685 return false;
1686
1687 default:
1688 return true;
1689 }
1690
1691 default:
1692 return true;
1693 }
1694 }
1695
1696 return false;
1697 }
1698
1699 /* Creates a conflict function with N dimensions. The affine functions
1700 in each dimension follow. */
1701
1702 static conflict_function *
1703 conflict_fn (unsigned n, ...)
1704 {
1705 unsigned i;
1706 conflict_function *ret = XCNEW (conflict_function);
1707 va_list ap;
1708
1709 gcc_assert (0 < n && n <= MAX_DIM);
1710 va_start (ap, n);
1711
1712 ret->n = n;
1713 for (i = 0; i < n; i++)
1714 ret->fns[i] = va_arg (ap, affine_fn);
1715 va_end (ap);
1716
1717 return ret;
1718 }
1719
1720 /* Returns constant affine function with value CST. */
1721
1722 static affine_fn
1723 affine_fn_cst (tree cst)
1724 {
1725 affine_fn fn;
1726 fn.create (1);
1727 fn.quick_push (cst);
1728 return fn;
1729 }
1730
1731 /* Returns affine function with single variable, CST + COEF * x_DIM. */
1732
1733 static affine_fn
1734 affine_fn_univar (tree cst, unsigned dim, tree coef)
1735 {
1736 affine_fn fn;
1737 fn.create (dim + 1);
1738 unsigned i;
1739
1740 gcc_assert (dim > 0);
1741 fn.quick_push (cst);
1742 for (i = 1; i < dim; i++)
1743 fn.quick_push (integer_zero_node);
1744 fn.quick_push (coef);
1745 return fn;
1746 }
1747
1748 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
1749 *OVERLAPS_B are initialized to the functions that describe the
1750 relation between the elements accessed twice by CHREC_A and
1751 CHREC_B. For k >= 0, the following property is verified:
1752
1753 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1754
1755 static void
1756 analyze_ziv_subscript (tree chrec_a,
1757 tree chrec_b,
1758 conflict_function **overlaps_a,
1759 conflict_function **overlaps_b,
1760 tree *last_conflicts)
1761 {
1762 tree type, difference;
1763 dependence_stats.num_ziv++;
1764
1765 if (dump_file && (dump_flags & TDF_DETAILS))
1766 fprintf (dump_file, "(analyze_ziv_subscript \n");
1767
1768 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1769 chrec_a = chrec_convert (type, chrec_a, NULL);
1770 chrec_b = chrec_convert (type, chrec_b, NULL);
1771 difference = chrec_fold_minus (type, chrec_a, chrec_b);
1772
1773 switch (TREE_CODE (difference))
1774 {
1775 case INTEGER_CST:
1776 if (integer_zerop (difference))
1777 {
1778 /* The difference is equal to zero: the accessed index
1779 overlaps for each iteration in the loop. */
1780 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1781 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1782 *last_conflicts = chrec_dont_know;
1783 dependence_stats.num_ziv_dependent++;
1784 }
1785 else
1786 {
1787 /* The accesses do not overlap. */
1788 *overlaps_a = conflict_fn_no_dependence ();
1789 *overlaps_b = conflict_fn_no_dependence ();
1790 *last_conflicts = integer_zero_node;
1791 dependence_stats.num_ziv_independent++;
1792 }
1793 break;
1794
1795 default:
1796 /* We're not sure whether the indexes overlap. For the moment,
1797 conservatively answer "don't know". */
1798 if (dump_file && (dump_flags & TDF_DETAILS))
1799 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
1800
1801 *overlaps_a = conflict_fn_not_known ();
1802 *overlaps_b = conflict_fn_not_known ();
1803 *last_conflicts = chrec_dont_know;
1804 dependence_stats.num_ziv_unimplemented++;
1805 break;
1806 }
1807
1808 if (dump_file && (dump_flags & TDF_DETAILS))
1809 fprintf (dump_file, ")\n");
1810 }
1811
1812 /* Similar to max_stmt_executions_int, but returns the bound as a tree,
1813 and only if it fits to the int type. If this is not the case, or the
1814 bound on the number of iterations of LOOP could not be derived, returns
1815 chrec_dont_know. */
1816
1817 static tree
1818 max_stmt_executions_tree (struct loop *loop)
1819 {
1820 widest_int nit;
1821
1822 if (!max_stmt_executions (loop, &nit))
1823 return chrec_dont_know;
1824
1825 if (!wi::fits_to_tree_p (nit, unsigned_type_node))
1826 return chrec_dont_know;
1827
1828 return wide_int_to_tree (unsigned_type_node, nit);
1829 }
1830
1831 /* Determine whether the CHREC is always positive/negative. If the expression
1832 cannot be statically analyzed, return false, otherwise set the answer into
1833 VALUE. */
1834
1835 static bool
1836 chrec_is_positive (tree chrec, bool *value)
1837 {
1838 bool value0, value1, value2;
1839 tree end_value, nb_iter;
1840
1841 switch (TREE_CODE (chrec))
1842 {
1843 case POLYNOMIAL_CHREC:
1844 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
1845 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
1846 return false;
1847
1848 /* FIXME -- overflows. */
1849 if (value0 == value1)
1850 {
1851 *value = value0;
1852 return true;
1853 }
1854
1855 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
1856 and the proof consists in showing that the sign never
1857 changes during the execution of the loop, from 0 to
1858 loop->nb_iterations. */
1859 if (!evolution_function_is_affine_p (chrec))
1860 return false;
1861
1862 nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
1863 if (chrec_contains_undetermined (nb_iter))
1864 return false;
1865
1866 #if 0
1867 /* TODO -- If the test is after the exit, we may decrease the number of
1868 iterations by one. */
1869 if (after_exit)
1870 nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
1871 #endif
1872
1873 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
1874
1875 if (!chrec_is_positive (end_value, &value2))
1876 return false;
1877
1878 *value = value0;
1879 return value0 == value1;
1880
1881 case INTEGER_CST:
1882 switch (tree_int_cst_sgn (chrec))
1883 {
1884 case -1:
1885 *value = false;
1886 break;
1887 case 1:
1888 *value = true;
1889 break;
1890 default:
1891 return false;
1892 }
1893 return true;
1894
1895 default:
1896 return false;
1897 }
1898 }
1899
1900
1901 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
1902 constant, and CHREC_B is an affine function. *OVERLAPS_A and
1903 *OVERLAPS_B are initialized to the functions that describe the
1904 relation between the elements accessed twice by CHREC_A and
1905 CHREC_B. For k >= 0, the following property is verified:
1906
1907 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1908
1909 static void
1910 analyze_siv_subscript_cst_affine (tree chrec_a,
1911 tree chrec_b,
1912 conflict_function **overlaps_a,
1913 conflict_function **overlaps_b,
1914 tree *last_conflicts)
1915 {
1916 bool value0, value1, value2;
1917 tree type, difference, tmp;
1918
1919 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
1920 chrec_a = chrec_convert (type, chrec_a, NULL);
1921 chrec_b = chrec_convert (type, chrec_b, NULL);
1922 difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
1923
1924 /* Special case overlap in the first iteration. */
1925 if (integer_zerop (difference))
1926 {
1927 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1928 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
1929 *last_conflicts = integer_one_node;
1930 return;
1931 }
1932
1933 if (!chrec_is_positive (initial_condition (difference), &value0))
1934 {
1935 if (dump_file && (dump_flags & TDF_DETAILS))
1936 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
1937
1938 dependence_stats.num_siv_unimplemented++;
1939 *overlaps_a = conflict_fn_not_known ();
1940 *overlaps_b = conflict_fn_not_known ();
1941 *last_conflicts = chrec_dont_know;
1942 return;
1943 }
1944 else
1945 {
1946 if (value0 == false)
1947 {
1948 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
1949 {
1950 if (dump_file && (dump_flags & TDF_DETAILS))
1951 fprintf (dump_file, "siv test failed: chrec not positive.\n");
1952
1953 *overlaps_a = conflict_fn_not_known ();
1954 *overlaps_b = conflict_fn_not_known ();
1955 *last_conflicts = chrec_dont_know;
1956 dependence_stats.num_siv_unimplemented++;
1957 return;
1958 }
1959 else
1960 {
1961 if (value1 == true)
1962 {
1963 /* Example:
1964 chrec_a = 12
1965 chrec_b = {10, +, 1}
1966 */
1967
1968 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
1969 {
1970 HOST_WIDE_INT numiter;
1971 struct loop *loop = get_chrec_loop (chrec_b);
1972
1973 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
1974 tmp = fold_build2 (EXACT_DIV_EXPR, type,
1975 fold_build1 (ABS_EXPR, type, difference),
1976 CHREC_RIGHT (chrec_b));
1977 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
1978 *last_conflicts = integer_one_node;
1979
1980
1981 /* Perform weak-zero siv test to see if overlap is
1982 outside the loop bounds. */
1983 numiter = max_stmt_executions_int (loop);
1984
1985 if (numiter >= 0
1986 && compare_tree_int (tmp, numiter) > 0)
1987 {
1988 free_conflict_function (*overlaps_a);
1989 free_conflict_function (*overlaps_b);
1990 *overlaps_a = conflict_fn_no_dependence ();
1991 *overlaps_b = conflict_fn_no_dependence ();
1992 *last_conflicts = integer_zero_node;
1993 dependence_stats.num_siv_independent++;
1994 return;
1995 }
1996 dependence_stats.num_siv_dependent++;
1997 return;
1998 }
1999
2000 /* When the step does not divide the difference, there are
2001 no overlaps. */
2002 else
2003 {
2004 *overlaps_a = conflict_fn_no_dependence ();
2005 *overlaps_b = conflict_fn_no_dependence ();
2006 *last_conflicts = integer_zero_node;
2007 dependence_stats.num_siv_independent++;
2008 return;
2009 }
2010 }
2011
2012 else
2013 {
2014 /* Example:
2015 chrec_a = 12
2016 chrec_b = {10, +, -1}
2017
2018 In this case, chrec_a will not overlap with chrec_b. */
2019 *overlaps_a = conflict_fn_no_dependence ();
2020 *overlaps_b = conflict_fn_no_dependence ();
2021 *last_conflicts = integer_zero_node;
2022 dependence_stats.num_siv_independent++;
2023 return;
2024 }
2025 }
2026 }
2027 else
2028 {
2029 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
2030 {
2031 if (dump_file && (dump_flags & TDF_DETAILS))
2032 fprintf (dump_file, "siv test failed: chrec not positive.\n");
2033
2034 *overlaps_a = conflict_fn_not_known ();
2035 *overlaps_b = conflict_fn_not_known ();
2036 *last_conflicts = chrec_dont_know;
2037 dependence_stats.num_siv_unimplemented++;
2038 return;
2039 }
2040 else
2041 {
2042 if (value2 == false)
2043 {
2044 /* Example:
2045 chrec_a = 3
2046 chrec_b = {10, +, -1}
2047 */
2048 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2049 {
2050 HOST_WIDE_INT numiter;
2051 struct loop *loop = get_chrec_loop (chrec_b);
2052
2053 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2054 tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
2055 CHREC_RIGHT (chrec_b));
2056 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
2057 *last_conflicts = integer_one_node;
2058
2059 /* Perform weak-zero siv test to see if overlap is
2060 outside the loop bounds. */
2061 numiter = max_stmt_executions_int (loop);
2062
2063 if (numiter >= 0
2064 && compare_tree_int (tmp, numiter) > 0)
2065 {
2066 free_conflict_function (*overlaps_a);
2067 free_conflict_function (*overlaps_b);
2068 *overlaps_a = conflict_fn_no_dependence ();
2069 *overlaps_b = conflict_fn_no_dependence ();
2070 *last_conflicts = integer_zero_node;
2071 dependence_stats.num_siv_independent++;
2072 return;
2073 }
2074 dependence_stats.num_siv_dependent++;
2075 return;
2076 }
2077
2078 /* When the step does not divide the difference, there
2079 are no overlaps. */
2080 else
2081 {
2082 *overlaps_a = conflict_fn_no_dependence ();
2083 *overlaps_b = conflict_fn_no_dependence ();
2084 *last_conflicts = integer_zero_node;
2085 dependence_stats.num_siv_independent++;
2086 return;
2087 }
2088 }
2089 else
2090 {
2091 /* Example:
2092 chrec_a = 3
2093 chrec_b = {4, +, 1}
2094
2095 In this case, chrec_a will not overlap with chrec_b. */
2096 *overlaps_a = conflict_fn_no_dependence ();
2097 *overlaps_b = conflict_fn_no_dependence ();
2098 *last_conflicts = integer_zero_node;
2099 dependence_stats.num_siv_independent++;
2100 return;
2101 }
2102 }
2103 }
2104 }
2105 }
2106
2107 /* Helper recursive function for initializing the matrix A. Returns
2108 the initial value of CHREC. */
2109
2110 static tree
2111 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2112 {
2113 gcc_assert (chrec);
2114
2115 switch (TREE_CODE (chrec))
2116 {
2117 case POLYNOMIAL_CHREC:
2118 gcc_assert (TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST);
2119
2120 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2121 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2122
2123 case PLUS_EXPR:
2124 case MULT_EXPR:
2125 case MINUS_EXPR:
2126 {
2127 tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2128 tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
2129
2130 return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
2131 }
2132
2133 CASE_CONVERT:
2134 {
2135 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2136 return chrec_convert (chrec_type (chrec), op, NULL);
2137 }
2138
2139 case BIT_NOT_EXPR:
2140 {
2141 /* Handle ~X as -1 - X. */
2142 tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
2143 return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
2144 build_int_cst (TREE_TYPE (chrec), -1), op);
2145 }
2146
2147 case INTEGER_CST:
2148 return chrec;
2149
2150 default:
2151 gcc_unreachable ();
2152 return NULL_TREE;
2153 }
2154 }
2155
2156 #define FLOOR_DIV(x,y) ((x) / (y))
2157
2158 /* Solves the special case of the Diophantine equation:
2159 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2160
2161 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2162 number of iterations that loops X and Y run. The overlaps will be
2163 constructed as evolutions in dimension DIM. */
2164
2165 static void
2166 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2167 affine_fn *overlaps_a,
2168 affine_fn *overlaps_b,
2169 tree *last_conflicts, int dim)
2170 {
2171 if (((step_a > 0 && step_b > 0)
2172 || (step_a < 0 && step_b < 0)))
2173 {
2174 int step_overlaps_a, step_overlaps_b;
2175 int gcd_steps_a_b, last_conflict, tau2;
2176
2177 gcd_steps_a_b = gcd (step_a, step_b);
2178 step_overlaps_a = step_b / gcd_steps_a_b;
2179 step_overlaps_b = step_a / gcd_steps_a_b;
2180
2181 if (niter > 0)
2182 {
2183 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2184 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2185 last_conflict = tau2;
2186 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2187 }
2188 else
2189 *last_conflicts = chrec_dont_know;
2190
2191 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
2192 build_int_cst (NULL_TREE,
2193 step_overlaps_a));
2194 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
2195 build_int_cst (NULL_TREE,
2196 step_overlaps_b));
2197 }
2198
2199 else
2200 {
2201 *overlaps_a = affine_fn_cst (integer_zero_node);
2202 *overlaps_b = affine_fn_cst (integer_zero_node);
2203 *last_conflicts = integer_zero_node;
2204 }
2205 }
2206
2207 /* Solves the special case of a Diophantine equation where CHREC_A is
2208 an affine bivariate function, and CHREC_B is an affine univariate
2209 function. For example,
2210
2211 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2212
2213 has the following overlapping functions:
2214
2215 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2216 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2217 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2218
2219 FORNOW: This is a specialized implementation for a case occurring in
2220 a common benchmark. Implement the general algorithm. */
2221
2222 static void
2223 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2224 conflict_function **overlaps_a,
2225 conflict_function **overlaps_b,
2226 tree *last_conflicts)
2227 {
2228 bool xz_p, yz_p, xyz_p;
2229 int step_x, step_y, step_z;
2230 HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
2231 affine_fn overlaps_a_xz, overlaps_b_xz;
2232 affine_fn overlaps_a_yz, overlaps_b_yz;
2233 affine_fn overlaps_a_xyz, overlaps_b_xyz;
2234 affine_fn ova1, ova2, ovb;
2235 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
2236
2237 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2238 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2239 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2240
2241 niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
2242 niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
2243 niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
2244
2245 if (niter_x < 0 || niter_y < 0 || niter_z < 0)
2246 {
2247 if (dump_file && (dump_flags & TDF_DETAILS))
2248 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
2249
2250 *overlaps_a = conflict_fn_not_known ();
2251 *overlaps_b = conflict_fn_not_known ();
2252 *last_conflicts = chrec_dont_know;
2253 return;
2254 }
2255
2256 niter = MIN (niter_x, niter_z);
2257 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2258 &overlaps_a_xz,
2259 &overlaps_b_xz,
2260 &last_conflicts_xz, 1);
2261 niter = MIN (niter_y, niter_z);
2262 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2263 &overlaps_a_yz,
2264 &overlaps_b_yz,
2265 &last_conflicts_yz, 2);
2266 niter = MIN (niter_x, niter_z);
2267 niter = MIN (niter_y, niter);
2268 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2269 &overlaps_a_xyz,
2270 &overlaps_b_xyz,
2271 &last_conflicts_xyz, 3);
2272
2273 xz_p = !integer_zerop (last_conflicts_xz);
2274 yz_p = !integer_zerop (last_conflicts_yz);
2275 xyz_p = !integer_zerop (last_conflicts_xyz);
2276
2277 if (xz_p || yz_p || xyz_p)
2278 {
2279 ova1 = affine_fn_cst (integer_zero_node);
2280 ova2 = affine_fn_cst (integer_zero_node);
2281 ovb = affine_fn_cst (integer_zero_node);
2282 if (xz_p)
2283 {
2284 affine_fn t0 = ova1;
2285 affine_fn t2 = ovb;
2286
2287 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
2288 ovb = affine_fn_plus (ovb, overlaps_b_xz);
2289 affine_fn_free (t0);
2290 affine_fn_free (t2);
2291 *last_conflicts = last_conflicts_xz;
2292 }
2293 if (yz_p)
2294 {
2295 affine_fn t0 = ova2;
2296 affine_fn t2 = ovb;
2297
2298 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
2299 ovb = affine_fn_plus (ovb, overlaps_b_yz);
2300 affine_fn_free (t0);
2301 affine_fn_free (t2);
2302 *last_conflicts = last_conflicts_yz;
2303 }
2304 if (xyz_p)
2305 {
2306 affine_fn t0 = ova1;
2307 affine_fn t2 = ova2;
2308 affine_fn t4 = ovb;
2309
2310 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
2311 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
2312 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
2313 affine_fn_free (t0);
2314 affine_fn_free (t2);
2315 affine_fn_free (t4);
2316 *last_conflicts = last_conflicts_xyz;
2317 }
2318 *overlaps_a = conflict_fn (2, ova1, ova2);
2319 *overlaps_b = conflict_fn (1, ovb);
2320 }
2321 else
2322 {
2323 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2324 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2325 *last_conflicts = integer_zero_node;
2326 }
2327
2328 affine_fn_free (overlaps_a_xz);
2329 affine_fn_free (overlaps_b_xz);
2330 affine_fn_free (overlaps_a_yz);
2331 affine_fn_free (overlaps_b_yz);
2332 affine_fn_free (overlaps_a_xyz);
2333 affine_fn_free (overlaps_b_xyz);
2334 }
2335
2336 /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
2337
2338 static void
2339 lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
2340 int size)
2341 {
2342 memcpy (vec2, vec1, size * sizeof (*vec1));
2343 }
2344
2345 /* Copy the elements of M x N matrix MAT1 to MAT2. */
2346
2347 static void
2348 lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
2349 int m, int n)
2350 {
2351 int i;
2352
2353 for (i = 0; i < m; i++)
2354 lambda_vector_copy (mat1[i], mat2[i], n);
2355 }
2356
2357 /* Store the N x N identity matrix in MAT. */
2358
2359 static void
2360 lambda_matrix_id (lambda_matrix mat, int size)
2361 {
2362 int i, j;
2363
2364 for (i = 0; i < size; i++)
2365 for (j = 0; j < size; j++)
2366 mat[i][j] = (i == j) ? 1 : 0;
2367 }
2368
2369 /* Return the first nonzero element of vector VEC1 between START and N.
2370 We must have START <= N. Returns N if VEC1 is the zero vector. */
2371
2372 static int
2373 lambda_vector_first_nz (lambda_vector vec1, int n, int start)
2374 {
2375 int j = start;
2376 while (j < n && vec1[j] == 0)
2377 j++;
2378 return j;
2379 }
2380
2381 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
2382 R2 = R2 + CONST1 * R1. */
2383
2384 static void
2385 lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2, int const1)
2386 {
2387 int i;
2388
2389 if (const1 == 0)
2390 return;
2391
2392 for (i = 0; i < n; i++)
2393 mat[r2][i] += const1 * mat[r1][i];
2394 }
2395
2396 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
2397 and store the result in VEC2. */
2398
2399 static void
2400 lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
2401 int size, int const1)
2402 {
2403 int i;
2404
2405 if (const1 == 0)
2406 lambda_vector_clear (vec2, size);
2407 else
2408 for (i = 0; i < size; i++)
2409 vec2[i] = const1 * vec1[i];
2410 }
2411
2412 /* Negate vector VEC1 with length SIZE and store it in VEC2. */
2413
2414 static void
2415 lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
2416 int size)
2417 {
2418 lambda_vector_mult_const (vec1, vec2, size, -1);
2419 }
2420
2421 /* Negate row R1 of matrix MAT which has N columns. */
2422
2423 static void
2424 lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
2425 {
2426 lambda_vector_negate (mat[r1], mat[r1], n);
2427 }
2428
2429 /* Return true if two vectors are equal. */
2430
2431 static bool
2432 lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
2433 {
2434 int i;
2435 for (i = 0; i < size; i++)
2436 if (vec1[i] != vec2[i])
2437 return false;
2438 return true;
2439 }
2440
2441 /* Given an M x N integer matrix A, this function determines an M x
2442 M unimodular matrix U, and an M x N echelon matrix S such that
2443 "U.A = S". This decomposition is also known as "right Hermite".
2444
2445 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
2446 Restructuring Compilers" Utpal Banerjee. */
2447
2448 static void
2449 lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
2450 lambda_matrix S, lambda_matrix U)
2451 {
2452 int i, j, i0 = 0;
2453
2454 lambda_matrix_copy (A, S, m, n);
2455 lambda_matrix_id (U, m);
2456
2457 for (j = 0; j < n; j++)
2458 {
2459 if (lambda_vector_first_nz (S[j], m, i0) < m)
2460 {
2461 ++i0;
2462 for (i = m - 1; i >= i0; i--)
2463 {
2464 while (S[i][j] != 0)
2465 {
2466 int sigma, factor, a, b;
2467
2468 a = S[i-1][j];
2469 b = S[i][j];
2470 sigma = (a * b < 0) ? -1: 1;
2471 a = abs (a);
2472 b = abs (b);
2473 factor = sigma * (a / b);
2474
2475 lambda_matrix_row_add (S, n, i, i-1, -factor);
2476 std::swap (S[i], S[i-1]);
2477
2478 lambda_matrix_row_add (U, m, i, i-1, -factor);
2479 std::swap (U[i], U[i-1]);
2480 }
2481 }
2482 }
2483 }
2484 }
2485
2486 /* Determines the overlapping elements due to accesses CHREC_A and
2487 CHREC_B, that are affine functions. This function cannot handle
2488 symbolic evolution functions, ie. when initial conditions are
2489 parameters, because it uses lambda matrices of integers. */
2490
2491 static void
2492 analyze_subscript_affine_affine (tree chrec_a,
2493 tree chrec_b,
2494 conflict_function **overlaps_a,
2495 conflict_function **overlaps_b,
2496 tree *last_conflicts)
2497 {
2498 unsigned nb_vars_a, nb_vars_b, dim;
2499 HOST_WIDE_INT init_a, init_b, gamma, gcd_alpha_beta;
2500 lambda_matrix A, U, S;
2501 struct obstack scratch_obstack;
2502
2503 if (eq_evolutions_p (chrec_a, chrec_b))
2504 {
2505 /* The accessed index overlaps for each iteration in the
2506 loop. */
2507 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2508 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2509 *last_conflicts = chrec_dont_know;
2510 return;
2511 }
2512 if (dump_file && (dump_flags & TDF_DETAILS))
2513 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2514
2515 /* For determining the initial intersection, we have to solve a
2516 Diophantine equation. This is the most time consuming part.
2517
2518 For answering to the question: "Is there a dependence?" we have
2519 to prove that there exists a solution to the Diophantine
2520 equation, and that the solution is in the iteration domain,
2521 i.e. the solution is positive or zero, and that the solution
2522 happens before the upper bound loop.nb_iterations. Otherwise
2523 there is no dependence. This function outputs a description of
2524 the iterations that hold the intersections. */
2525
2526 nb_vars_a = nb_vars_in_chrec (chrec_a);
2527 nb_vars_b = nb_vars_in_chrec (chrec_b);
2528
2529 gcc_obstack_init (&scratch_obstack);
2530
2531 dim = nb_vars_a + nb_vars_b;
2532 U = lambda_matrix_new (dim, dim, &scratch_obstack);
2533 A = lambda_matrix_new (dim, 1, &scratch_obstack);
2534 S = lambda_matrix_new (dim, 1, &scratch_obstack);
2535
2536 init_a = int_cst_value (initialize_matrix_A (A, chrec_a, 0, 1));
2537 init_b = int_cst_value (initialize_matrix_A (A, chrec_b, nb_vars_a, -1));
2538 gamma = init_b - init_a;
2539
2540 /* Don't do all the hard work of solving the Diophantine equation
2541 when we already know the solution: for example,
2542 | {3, +, 1}_1
2543 | {3, +, 4}_2
2544 | gamma = 3 - 3 = 0.
2545 Then the first overlap occurs during the first iterations:
2546 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2547 */
2548 if (gamma == 0)
2549 {
2550 if (nb_vars_a == 1 && nb_vars_b == 1)
2551 {
2552 HOST_WIDE_INT step_a, step_b;
2553 HOST_WIDE_INT niter, niter_a, niter_b;
2554 affine_fn ova, ovb;
2555
2556 niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
2557 niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
2558 niter = MIN (niter_a, niter_b);
2559 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2560 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2561
2562 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2563 &ova, &ovb,
2564 last_conflicts, 1);
2565 *overlaps_a = conflict_fn (1, ova);
2566 *overlaps_b = conflict_fn (1, ovb);
2567 }
2568
2569 else if (nb_vars_a == 2 && nb_vars_b == 1)
2570 compute_overlap_steps_for_affine_1_2
2571 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2572
2573 else if (nb_vars_a == 1 && nb_vars_b == 2)
2574 compute_overlap_steps_for_affine_1_2
2575 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2576
2577 else
2578 {
2579 if (dump_file && (dump_flags & TDF_DETAILS))
2580 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
2581 *overlaps_a = conflict_fn_not_known ();
2582 *overlaps_b = conflict_fn_not_known ();
2583 *last_conflicts = chrec_dont_know;
2584 }
2585 goto end_analyze_subs_aa;
2586 }
2587
2588 /* U.A = S */
2589 lambda_matrix_right_hermite (A, dim, 1, S, U);
2590
2591 if (S[0][0] < 0)
2592 {
2593 S[0][0] *= -1;
2594 lambda_matrix_row_negate (U, dim, 0);
2595 }
2596 gcd_alpha_beta = S[0][0];
2597
2598 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2599 but that is a quite strange case. Instead of ICEing, answer
2600 don't know. */
2601 if (gcd_alpha_beta == 0)
2602 {
2603 *overlaps_a = conflict_fn_not_known ();
2604 *overlaps_b = conflict_fn_not_known ();
2605 *last_conflicts = chrec_dont_know;
2606 goto end_analyze_subs_aa;
2607 }
2608
2609 /* The classic "gcd-test". */
2610 if (!int_divides_p (gcd_alpha_beta, gamma))
2611 {
2612 /* The "gcd-test" has determined that there is no integer
2613 solution, i.e. there is no dependence. */
2614 *overlaps_a = conflict_fn_no_dependence ();
2615 *overlaps_b = conflict_fn_no_dependence ();
2616 *last_conflicts = integer_zero_node;
2617 }
2618
2619 /* Both access functions are univariate. This includes SIV and MIV cases. */
2620 else if (nb_vars_a == 1 && nb_vars_b == 1)
2621 {
2622 /* Both functions should have the same evolution sign. */
2623 if (((A[0][0] > 0 && -A[1][0] > 0)
2624 || (A[0][0] < 0 && -A[1][0] < 0)))
2625 {
2626 /* The solutions are given by:
2627 |
2628 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2629 | [u21 u22] [y0]
2630
2631 For a given integer t. Using the following variables,
2632
2633 | i0 = u11 * gamma / gcd_alpha_beta
2634 | j0 = u12 * gamma / gcd_alpha_beta
2635 | i1 = u21
2636 | j1 = u22
2637
2638 the solutions are:
2639
2640 | x0 = i0 + i1 * t,
2641 | y0 = j0 + j1 * t. */
2642 HOST_WIDE_INT i0, j0, i1, j1;
2643
2644 i0 = U[0][0] * gamma / gcd_alpha_beta;
2645 j0 = U[0][1] * gamma / gcd_alpha_beta;
2646 i1 = U[1][0];
2647 j1 = U[1][1];
2648
2649 if ((i1 == 0 && i0 < 0)
2650 || (j1 == 0 && j0 < 0))
2651 {
2652 /* There is no solution.
2653 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2654 falls in here, but for the moment we don't look at the
2655 upper bound of the iteration domain. */
2656 *overlaps_a = conflict_fn_no_dependence ();
2657 *overlaps_b = conflict_fn_no_dependence ();
2658 *last_conflicts = integer_zero_node;
2659 goto end_analyze_subs_aa;
2660 }
2661
2662 if (i1 > 0 && j1 > 0)
2663 {
2664 HOST_WIDE_INT niter_a
2665 = max_stmt_executions_int (get_chrec_loop (chrec_a));
2666 HOST_WIDE_INT niter_b
2667 = max_stmt_executions_int (get_chrec_loop (chrec_b));
2668 HOST_WIDE_INT niter = MIN (niter_a, niter_b);
2669
2670 /* (X0, Y0) is a solution of the Diophantine equation:
2671 "chrec_a (X0) = chrec_b (Y0)". */
2672 HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
2673 CEIL (-j0, j1));
2674 HOST_WIDE_INT x0 = i1 * tau1 + i0;
2675 HOST_WIDE_INT y0 = j1 * tau1 + j0;
2676
2677 /* (X1, Y1) is the smallest positive solution of the eq
2678 "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
2679 first conflict occurs. */
2680 HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
2681 HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
2682 HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
2683
2684 if (niter > 0)
2685 {
2686 HOST_WIDE_INT tau2 = MIN (FLOOR_DIV (niter - i0, i1),
2687 FLOOR_DIV (niter - j0, j1));
2688 HOST_WIDE_INT last_conflict = tau2 - (x1 - i0)/i1;
2689
2690 /* If the overlap occurs outside of the bounds of the
2691 loop, there is no dependence. */
2692 if (x1 >= niter || y1 >= niter)
2693 {
2694 *overlaps_a = conflict_fn_no_dependence ();
2695 *overlaps_b = conflict_fn_no_dependence ();
2696 *last_conflicts = integer_zero_node;
2697 goto end_analyze_subs_aa;
2698 }
2699 else
2700 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2701 }
2702 else
2703 *last_conflicts = chrec_dont_know;
2704
2705 *overlaps_a
2706 = conflict_fn (1,
2707 affine_fn_univar (build_int_cst (NULL_TREE, x1),
2708 1,
2709 build_int_cst (NULL_TREE, i1)));
2710 *overlaps_b
2711 = conflict_fn (1,
2712 affine_fn_univar (build_int_cst (NULL_TREE, y1),
2713 1,
2714 build_int_cst (NULL_TREE, j1)));
2715 }
2716 else
2717 {
2718 /* FIXME: For the moment, the upper bound of the
2719 iteration domain for i and j is not checked. */
2720 if (dump_file && (dump_flags & TDF_DETAILS))
2721 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2722 *overlaps_a = conflict_fn_not_known ();
2723 *overlaps_b = conflict_fn_not_known ();
2724 *last_conflicts = chrec_dont_know;
2725 }
2726 }
2727 else
2728 {
2729 if (dump_file && (dump_flags & TDF_DETAILS))
2730 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2731 *overlaps_a = conflict_fn_not_known ();
2732 *overlaps_b = conflict_fn_not_known ();
2733 *last_conflicts = chrec_dont_know;
2734 }
2735 }
2736 else
2737 {
2738 if (dump_file && (dump_flags & TDF_DETAILS))
2739 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2740 *overlaps_a = conflict_fn_not_known ();
2741 *overlaps_b = conflict_fn_not_known ();
2742 *last_conflicts = chrec_dont_know;
2743 }
2744
2745 end_analyze_subs_aa:
2746 obstack_free (&scratch_obstack, NULL);
2747 if (dump_file && (dump_flags & TDF_DETAILS))
2748 {
2749 fprintf (dump_file, " (overlaps_a = ");
2750 dump_conflict_function (dump_file, *overlaps_a);
2751 fprintf (dump_file, ")\n (overlaps_b = ");
2752 dump_conflict_function (dump_file, *overlaps_b);
2753 fprintf (dump_file, "))\n");
2754 }
2755 }
2756
2757 /* Returns true when analyze_subscript_affine_affine can be used for
2758 determining the dependence relation between chrec_a and chrec_b,
2759 that contain symbols. This function modifies chrec_a and chrec_b
2760 such that the analysis result is the same, and such that they don't
2761 contain symbols, and then can safely be passed to the analyzer.
2762
2763 Example: The analysis of the following tuples of evolutions produce
2764 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
2765 vs. {0, +, 1}_1
2766
2767 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
2768 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
2769 */
2770
2771 static bool
2772 can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
2773 {
2774 tree diff, type, left_a, left_b, right_b;
2775
2776 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
2777 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
2778 /* FIXME: For the moment not handled. Might be refined later. */
2779 return false;
2780
2781 type = chrec_type (*chrec_a);
2782 left_a = CHREC_LEFT (*chrec_a);
2783 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
2784 diff = chrec_fold_minus (type, left_a, left_b);
2785
2786 if (!evolution_function_is_constant_p (diff))
2787 return false;
2788
2789 if (dump_file && (dump_flags & TDF_DETAILS))
2790 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
2791
2792 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
2793 diff, CHREC_RIGHT (*chrec_a));
2794 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
2795 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
2796 build_int_cst (type, 0),
2797 right_b);
2798 return true;
2799 }
2800
2801 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2802 *OVERLAPS_B are initialized to the functions that describe the
2803 relation between the elements accessed twice by CHREC_A and
2804 CHREC_B. For k >= 0, the following property is verified:
2805
2806 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2807
2808 static void
2809 analyze_siv_subscript (tree chrec_a,
2810 tree chrec_b,
2811 conflict_function **overlaps_a,
2812 conflict_function **overlaps_b,
2813 tree *last_conflicts,
2814 int loop_nest_num)
2815 {
2816 dependence_stats.num_siv++;
2817
2818 if (dump_file && (dump_flags & TDF_DETAILS))
2819 fprintf (dump_file, "(analyze_siv_subscript \n");
2820
2821 if (evolution_function_is_constant_p (chrec_a)
2822 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2823 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
2824 overlaps_a, overlaps_b, last_conflicts);
2825
2826 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2827 && evolution_function_is_constant_p (chrec_b))
2828 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
2829 overlaps_b, overlaps_a, last_conflicts);
2830
2831 else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
2832 && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
2833 {
2834 if (!chrec_contains_symbols (chrec_a)
2835 && !chrec_contains_symbols (chrec_b))
2836 {
2837 analyze_subscript_affine_affine (chrec_a, chrec_b,
2838 overlaps_a, overlaps_b,
2839 last_conflicts);
2840
2841 if (CF_NOT_KNOWN_P (*overlaps_a)
2842 || CF_NOT_KNOWN_P (*overlaps_b))
2843 dependence_stats.num_siv_unimplemented++;
2844 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2845 || CF_NO_DEPENDENCE_P (*overlaps_b))
2846 dependence_stats.num_siv_independent++;
2847 else
2848 dependence_stats.num_siv_dependent++;
2849 }
2850 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
2851 &chrec_b))
2852 {
2853 analyze_subscript_affine_affine (chrec_a, chrec_b,
2854 overlaps_a, overlaps_b,
2855 last_conflicts);
2856
2857 if (CF_NOT_KNOWN_P (*overlaps_a)
2858 || CF_NOT_KNOWN_P (*overlaps_b))
2859 dependence_stats.num_siv_unimplemented++;
2860 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2861 || CF_NO_DEPENDENCE_P (*overlaps_b))
2862 dependence_stats.num_siv_independent++;
2863 else
2864 dependence_stats.num_siv_dependent++;
2865 }
2866 else
2867 goto siv_subscript_dontknow;
2868 }
2869
2870 else
2871 {
2872 siv_subscript_dontknow:;
2873 if (dump_file && (dump_flags & TDF_DETAILS))
2874 fprintf (dump_file, " siv test failed: unimplemented");
2875 *overlaps_a = conflict_fn_not_known ();
2876 *overlaps_b = conflict_fn_not_known ();
2877 *last_conflicts = chrec_dont_know;
2878 dependence_stats.num_siv_unimplemented++;
2879 }
2880
2881 if (dump_file && (dump_flags & TDF_DETAILS))
2882 fprintf (dump_file, ")\n");
2883 }
2884
2885 /* Returns false if we can prove that the greatest common divisor of the steps
2886 of CHREC does not divide CST, false otherwise. */
2887
2888 static bool
2889 gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
2890 {
2891 HOST_WIDE_INT cd = 0, val;
2892 tree step;
2893
2894 if (!tree_fits_shwi_p (cst))
2895 return true;
2896 val = tree_to_shwi (cst);
2897
2898 while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
2899 {
2900 step = CHREC_RIGHT (chrec);
2901 if (!tree_fits_shwi_p (step))
2902 return true;
2903 cd = gcd (cd, tree_to_shwi (step));
2904 chrec = CHREC_LEFT (chrec);
2905 }
2906
2907 return val % cd == 0;
2908 }
2909
2910 /* Analyze a MIV (Multiple Index Variable) subscript with respect to
2911 LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
2912 functions that describe the relation between the elements accessed
2913 twice by CHREC_A and CHREC_B. For k >= 0, the following property
2914 is verified:
2915
2916 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2917
2918 static void
2919 analyze_miv_subscript (tree chrec_a,
2920 tree chrec_b,
2921 conflict_function **overlaps_a,
2922 conflict_function **overlaps_b,
2923 tree *last_conflicts,
2924 struct loop *loop_nest)
2925 {
2926 tree type, difference;
2927
2928 dependence_stats.num_miv++;
2929 if (dump_file && (dump_flags & TDF_DETAILS))
2930 fprintf (dump_file, "(analyze_miv_subscript \n");
2931
2932 type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
2933 chrec_a = chrec_convert (type, chrec_a, NULL);
2934 chrec_b = chrec_convert (type, chrec_b, NULL);
2935 difference = chrec_fold_minus (type, chrec_a, chrec_b);
2936
2937 if (eq_evolutions_p (chrec_a, chrec_b))
2938 {
2939 /* Access functions are the same: all the elements are accessed
2940 in the same order. */
2941 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2942 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2943 *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
2944 dependence_stats.num_miv_dependent++;
2945 }
2946
2947 else if (evolution_function_is_constant_p (difference)
2948 /* For the moment, the following is verified:
2949 evolution_function_is_affine_multivariate_p (chrec_a,
2950 loop_nest->num) */
2951 && !gcd_of_steps_may_divide_p (chrec_a, difference))
2952 {
2953 /* testsuite/.../ssa-chrec-33.c
2954 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2955
2956 The difference is 1, and all the evolution steps are multiples
2957 of 2, consequently there are no overlapping elements. */
2958 *overlaps_a = conflict_fn_no_dependence ();
2959 *overlaps_b = conflict_fn_no_dependence ();
2960 *last_conflicts = integer_zero_node;
2961 dependence_stats.num_miv_independent++;
2962 }
2963
2964 else if (evolution_function_is_affine_multivariate_p (chrec_a, loop_nest->num)
2965 && !chrec_contains_symbols (chrec_a)
2966 && evolution_function_is_affine_multivariate_p (chrec_b, loop_nest->num)
2967 && !chrec_contains_symbols (chrec_b))
2968 {
2969 /* testsuite/.../ssa-chrec-35.c
2970 {0, +, 1}_2 vs. {0, +, 1}_3
2971 the overlapping elements are respectively located at iterations:
2972 {0, +, 1}_x and {0, +, 1}_x,
2973 in other words, we have the equality:
2974 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2975
2976 Other examples:
2977 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2978 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2979
2980 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2981 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2982 */
2983 analyze_subscript_affine_affine (chrec_a, chrec_b,
2984 overlaps_a, overlaps_b, last_conflicts);
2985
2986 if (CF_NOT_KNOWN_P (*overlaps_a)
2987 || CF_NOT_KNOWN_P (*overlaps_b))
2988 dependence_stats.num_miv_unimplemented++;
2989 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
2990 || CF_NO_DEPENDENCE_P (*overlaps_b))
2991 dependence_stats.num_miv_independent++;
2992 else
2993 dependence_stats.num_miv_dependent++;
2994 }
2995
2996 else
2997 {
2998 /* When the analysis is too difficult, answer "don't know". */
2999 if (dump_file && (dump_flags & TDF_DETAILS))
3000 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
3001
3002 *overlaps_a = conflict_fn_not_known ();
3003 *overlaps_b = conflict_fn_not_known ();
3004 *last_conflicts = chrec_dont_know;
3005 dependence_stats.num_miv_unimplemented++;
3006 }
3007
3008 if (dump_file && (dump_flags & TDF_DETAILS))
3009 fprintf (dump_file, ")\n");
3010 }
3011
3012 /* Determines the iterations for which CHREC_A is equal to CHREC_B in
3013 with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
3014 OVERLAP_ITERATIONS_B are initialized with two functions that
3015 describe the iterations that contain conflicting elements.
3016
3017 Remark: For an integer k >= 0, the following equality is true:
3018
3019 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
3020 */
3021
3022 static void
3023 analyze_overlapping_iterations (tree chrec_a,
3024 tree chrec_b,
3025 conflict_function **overlap_iterations_a,
3026 conflict_function **overlap_iterations_b,
3027 tree *last_conflicts, struct loop *loop_nest)
3028 {
3029 unsigned int lnn = loop_nest->num;
3030
3031 dependence_stats.num_subscript_tests++;
3032
3033 if (dump_file && (dump_flags & TDF_DETAILS))
3034 {
3035 fprintf (dump_file, "(analyze_overlapping_iterations \n");
3036 fprintf (dump_file, " (chrec_a = ");
3037 print_generic_expr (dump_file, chrec_a, 0);
3038 fprintf (dump_file, ")\n (chrec_b = ");
3039 print_generic_expr (dump_file, chrec_b, 0);
3040 fprintf (dump_file, ")\n");
3041 }
3042
3043 if (chrec_a == NULL_TREE
3044 || chrec_b == NULL_TREE
3045 || chrec_contains_undetermined (chrec_a)
3046 || chrec_contains_undetermined (chrec_b))
3047 {
3048 dependence_stats.num_subscript_undetermined++;
3049
3050 *overlap_iterations_a = conflict_fn_not_known ();
3051 *overlap_iterations_b = conflict_fn_not_known ();
3052 }
3053
3054 /* If they are the same chrec, and are affine, they overlap
3055 on every iteration. */
3056 else if (eq_evolutions_p (chrec_a, chrec_b)
3057 && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3058 || operand_equal_p (chrec_a, chrec_b, 0)))
3059 {
3060 dependence_stats.num_same_subscript_function++;
3061 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3062 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3063 *last_conflicts = chrec_dont_know;
3064 }
3065
3066 /* If they aren't the same, and aren't affine, we can't do anything
3067 yet. */
3068 else if ((chrec_contains_symbols (chrec_a)
3069 || chrec_contains_symbols (chrec_b))
3070 && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
3071 || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
3072 {
3073 dependence_stats.num_subscript_undetermined++;
3074 *overlap_iterations_a = conflict_fn_not_known ();
3075 *overlap_iterations_b = conflict_fn_not_known ();
3076 }
3077
3078 else if (ziv_subscript_p (chrec_a, chrec_b))
3079 analyze_ziv_subscript (chrec_a, chrec_b,
3080 overlap_iterations_a, overlap_iterations_b,
3081 last_conflicts);
3082
3083 else if (siv_subscript_p (chrec_a, chrec_b))
3084 analyze_siv_subscript (chrec_a, chrec_b,
3085 overlap_iterations_a, overlap_iterations_b,
3086 last_conflicts, lnn);
3087
3088 else
3089 analyze_miv_subscript (chrec_a, chrec_b,
3090 overlap_iterations_a, overlap_iterations_b,
3091 last_conflicts, loop_nest);
3092
3093 if (dump_file && (dump_flags & TDF_DETAILS))
3094 {
3095 fprintf (dump_file, " (overlap_iterations_a = ");
3096 dump_conflict_function (dump_file, *overlap_iterations_a);
3097 fprintf (dump_file, ")\n (overlap_iterations_b = ");
3098 dump_conflict_function (dump_file, *overlap_iterations_b);
3099 fprintf (dump_file, "))\n");
3100 }
3101 }
3102
3103 /* Helper function for uniquely inserting distance vectors. */
3104
3105 static void
3106 save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
3107 {
3108 unsigned i;
3109 lambda_vector v;
3110
3111 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, v)
3112 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
3113 return;
3114
3115 DDR_DIST_VECTS (ddr).safe_push (dist_v);
3116 }
3117
3118 /* Helper function for uniquely inserting direction vectors. */
3119
3120 static void
3121 save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
3122 {
3123 unsigned i;
3124 lambda_vector v;
3125
3126 FOR_EACH_VEC_ELT (DDR_DIR_VECTS (ddr), i, v)
3127 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
3128 return;
3129
3130 DDR_DIR_VECTS (ddr).safe_push (dir_v);
3131 }
3132
3133 /* Add a distance of 1 on all the loops outer than INDEX. If we
3134 haven't yet determined a distance for this outer loop, push a new
3135 distance vector composed of the previous distance, and a distance
3136 of 1 for this outer loop. Example:
3137
3138 | loop_1
3139 | loop_2
3140 | A[10]
3141 | endloop_2
3142 | endloop_1
3143
3144 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3145 save (0, 1), then we have to save (1, 0). */
3146
3147 static void
3148 add_outer_distances (struct data_dependence_relation *ddr,
3149 lambda_vector dist_v, int index)
3150 {
3151 /* For each outer loop where init_v is not set, the accesses are
3152 in dependence of distance 1 in the loop. */
3153 while (--index >= 0)
3154 {
3155 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3156 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3157 save_v[index] = 1;
3158 save_dist_v (ddr, save_v);
3159 }
3160 }
3161
3162 /* Return false when fail to represent the data dependence as a
3163 distance vector. INIT_B is set to true when a component has been
3164 added to the distance vector DIST_V. INDEX_CARRY is then set to
3165 the index in DIST_V that carries the dependence. */
3166
3167 static bool
3168 build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
3169 struct data_reference *ddr_a,
3170 struct data_reference *ddr_b,
3171 lambda_vector dist_v, bool *init_b,
3172 int *index_carry)
3173 {
3174 unsigned i;
3175 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3176
3177 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3178 {
3179 tree access_fn_a, access_fn_b;
3180 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3181
3182 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3183 {
3184 non_affine_dependence_relation (ddr);
3185 return false;
3186 }
3187
3188 access_fn_a = DR_ACCESS_FN (ddr_a, i);
3189 access_fn_b = DR_ACCESS_FN (ddr_b, i);
3190
3191 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3192 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3193 {
3194 int dist, index;
3195 int var_a = CHREC_VARIABLE (access_fn_a);
3196 int var_b = CHREC_VARIABLE (access_fn_b);
3197
3198 if (var_a != var_b
3199 || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3200 {
3201 non_affine_dependence_relation (ddr);
3202 return false;
3203 }
3204
3205 dist = int_cst_value (SUB_DISTANCE (subscript));
3206 index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
3207 *index_carry = MIN (index, *index_carry);
3208
3209 /* This is the subscript coupling test. If we have already
3210 recorded a distance for this loop (a distance coming from
3211 another subscript), it should be the same. For example,
3212 in the following code, there is no dependence:
3213
3214 | loop i = 0, N, 1
3215 | T[i+1][i] = ...
3216 | ... = T[i][i]
3217 | endloop
3218 */
3219 if (init_v[index] != 0 && dist_v[index] != dist)
3220 {
3221 finalize_ddr_dependent (ddr, chrec_known);
3222 return false;
3223 }
3224
3225 dist_v[index] = dist;
3226 init_v[index] = 1;
3227 *init_b = true;
3228 }
3229 else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
3230 {
3231 /* This can be for example an affine vs. constant dependence
3232 (T[i] vs. T[3]) that is not an affine dependence and is
3233 not representable as a distance vector. */
3234 non_affine_dependence_relation (ddr);
3235 return false;
3236 }
3237 }
3238
3239 return true;
3240 }
3241
3242 /* Return true when the DDR contains only constant access functions. */
3243
3244 static bool
3245 constant_access_functions (const struct data_dependence_relation *ddr)
3246 {
3247 unsigned i;
3248
3249 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3250 if (!evolution_function_is_constant_p (DR_ACCESS_FN (DDR_A (ddr), i))
3251 || !evolution_function_is_constant_p (DR_ACCESS_FN (DDR_B (ddr), i)))
3252 return false;
3253
3254 return true;
3255 }
3256
3257 /* Helper function for the case where DDR_A and DDR_B are the same
3258 multivariate access function with a constant step. For an example
3259 see pr34635-1.c. */
3260
3261 static void
3262 add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
3263 {
3264 int x_1, x_2;
3265 tree c_1 = CHREC_LEFT (c_2);
3266 tree c_0 = CHREC_LEFT (c_1);
3267 lambda_vector dist_v;
3268 int v1, v2, cd;
3269
3270 /* Polynomials with more than 2 variables are not handled yet. When
3271 the evolution steps are parameters, it is not possible to
3272 represent the dependence using classical distance vectors. */
3273 if (TREE_CODE (c_0) != INTEGER_CST
3274 || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
3275 || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
3276 {
3277 DDR_AFFINE_P (ddr) = false;
3278 return;
3279 }
3280
3281 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
3282 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
3283
3284 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3285 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3286 v1 = int_cst_value (CHREC_RIGHT (c_1));
3287 v2 = int_cst_value (CHREC_RIGHT (c_2));
3288 cd = gcd (v1, v2);
3289 v1 /= cd;
3290 v2 /= cd;
3291
3292 if (v2 < 0)
3293 {
3294 v2 = -v2;
3295 v1 = -v1;
3296 }
3297
3298 dist_v[x_1] = v2;
3299 dist_v[x_2] = -v1;
3300 save_dist_v (ddr, dist_v);
3301
3302 add_outer_distances (ddr, dist_v, x_1);
3303 }
3304
3305 /* Helper function for the case where DDR_A and DDR_B are the same
3306 access functions. */
3307
3308 static void
3309 add_other_self_distances (struct data_dependence_relation *ddr)
3310 {
3311 lambda_vector dist_v;
3312 unsigned i;
3313 int index_carry = DDR_NB_LOOPS (ddr);
3314
3315 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3316 {
3317 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
3318
3319 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
3320 {
3321 if (!evolution_function_is_univariate_p (access_fun))
3322 {
3323 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
3324 {
3325 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3326 return;
3327 }
3328
3329 access_fun = DR_ACCESS_FN (DDR_A (ddr), 0);
3330
3331 if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
3332 add_multivariate_self_dist (ddr, access_fun);
3333 else
3334 /* The evolution step is not constant: it varies in
3335 the outer loop, so this cannot be represented by a
3336 distance vector. For example in pr34635.c the
3337 evolution is {0, +, {0, +, 4}_1}_2. */
3338 DDR_AFFINE_P (ddr) = false;
3339
3340 return;
3341 }
3342
3343 index_carry = MIN (index_carry,
3344 index_in_loop_nest (CHREC_VARIABLE (access_fun),
3345 DDR_LOOP_NEST (ddr)));
3346 }
3347 }
3348
3349 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3350 add_outer_distances (ddr, dist_v, index_carry);
3351 }
3352
3353 static void
3354 insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
3355 {
3356 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3357
3358 dist_v[DDR_INNER_LOOP (ddr)] = 1;
3359 save_dist_v (ddr, dist_v);
3360 }
3361
3362 /* Adds a unit distance vector to DDR when there is a 0 overlap. This
3363 is the case for example when access functions are the same and
3364 equal to a constant, as in:
3365
3366 | loop_1
3367 | A[3] = ...
3368 | ... = A[3]
3369 | endloop_1
3370
3371 in which case the distance vectors are (0) and (1). */
3372
3373 static void
3374 add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
3375 {
3376 unsigned i, j;
3377
3378 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3379 {
3380 subscript_p sub = DDR_SUBSCRIPT (ddr, i);
3381 conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
3382 conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
3383
3384 for (j = 0; j < ca->n; j++)
3385 if (affine_function_zero_p (ca->fns[j]))
3386 {
3387 insert_innermost_unit_dist_vector (ddr);
3388 return;
3389 }
3390
3391 for (j = 0; j < cb->n; j++)
3392 if (affine_function_zero_p (cb->fns[j]))
3393 {
3394 insert_innermost_unit_dist_vector (ddr);
3395 return;
3396 }
3397 }
3398 }
3399
3400 /* Compute the classic per loop distance vector. DDR is the data
3401 dependence relation to build a vector from. Return false when fail
3402 to represent the data dependence as a distance vector. */
3403
3404 static bool
3405 build_classic_dist_vector (struct data_dependence_relation *ddr,
3406 struct loop *loop_nest)
3407 {
3408 bool init_b = false;
3409 int index_carry = DDR_NB_LOOPS (ddr);
3410 lambda_vector dist_v;
3411
3412 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3413 return false;
3414
3415 if (same_access_functions (ddr))
3416 {
3417 /* Save the 0 vector. */
3418 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3419 save_dist_v (ddr, dist_v);
3420
3421 if (constant_access_functions (ddr))
3422 add_distance_for_zero_overlaps (ddr);
3423
3424 if (DDR_NB_LOOPS (ddr) > 1)
3425 add_other_self_distances (ddr);
3426
3427 return true;
3428 }
3429
3430 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3431 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3432 dist_v, &init_b, &index_carry))
3433 return false;
3434
3435 /* Save the distance vector if we initialized one. */
3436 if (init_b)
3437 {
3438 /* Verify a basic constraint: classic distance vectors should
3439 always be lexicographically positive.
3440
3441 Data references are collected in the order of execution of
3442 the program, thus for the following loop
3443
3444 | for (i = 1; i < 100; i++)
3445 | for (j = 1; j < 100; j++)
3446 | {
3447 | t = T[j+1][i-1]; // A
3448 | T[j][i] = t + 2; // B
3449 | }
3450
3451 references are collected following the direction of the wind:
3452 A then B. The data dependence tests are performed also
3453 following this order, such that we're looking at the distance
3454 separating the elements accessed by A from the elements later
3455 accessed by B. But in this example, the distance returned by
3456 test_dep (A, B) is lexicographically negative (-1, 1), that
3457 means that the access A occurs later than B with respect to
3458 the outer loop, ie. we're actually looking upwind. In this
3459 case we solve test_dep (B, A) looking downwind to the
3460 lexicographically positive solution, that returns the
3461 distance vector (1, -1). */
3462 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3463 {
3464 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3465 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3466 loop_nest))
3467 return false;
3468 compute_subscript_distance (ddr);
3469 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3470 save_v, &init_b, &index_carry))
3471 return false;
3472 save_dist_v (ddr, save_v);
3473 DDR_REVERSED_P (ddr) = true;
3474
3475 /* In this case there is a dependence forward for all the
3476 outer loops:
3477
3478 | for (k = 1; k < 100; k++)
3479 | for (i = 1; i < 100; i++)
3480 | for (j = 1; j < 100; j++)
3481 | {
3482 | t = T[j+1][i-1]; // A
3483 | T[j][i] = t + 2; // B
3484 | }
3485
3486 the vectors are:
3487 (0, 1, -1)
3488 (1, 1, -1)
3489 (1, -1, 1)
3490 */
3491 if (DDR_NB_LOOPS (ddr) > 1)
3492 {
3493 add_outer_distances (ddr, save_v, index_carry);
3494 add_outer_distances (ddr, dist_v, index_carry);
3495 }
3496 }
3497 else
3498 {
3499 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3500 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3501
3502 if (DDR_NB_LOOPS (ddr) > 1)
3503 {
3504 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3505
3506 if (!subscript_dependence_tester_1 (ddr, DDR_B (ddr),
3507 DDR_A (ddr), loop_nest))
3508 return false;
3509 compute_subscript_distance (ddr);
3510 if (!build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3511 opposite_v, &init_b,
3512 &index_carry))
3513 return false;
3514
3515 save_dist_v (ddr, save_v);
3516 add_outer_distances (ddr, dist_v, index_carry);
3517 add_outer_distances (ddr, opposite_v, index_carry);
3518 }
3519 else
3520 save_dist_v (ddr, save_v);
3521 }
3522 }
3523 else
3524 {
3525 /* There is a distance of 1 on all the outer loops: Example:
3526 there is a dependence of distance 1 on loop_1 for the array A.
3527
3528 | loop_1
3529 | A[5] = ...
3530 | endloop
3531 */
3532 add_outer_distances (ddr, dist_v,
3533 lambda_vector_first_nz (dist_v,
3534 DDR_NB_LOOPS (ddr), 0));
3535 }
3536
3537 if (dump_file && (dump_flags & TDF_DETAILS))
3538 {
3539 unsigned i;
3540
3541 fprintf (dump_file, "(build_classic_dist_vector\n");
3542 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3543 {
3544 fprintf (dump_file, " dist_vector = (");
3545 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3546 DDR_NB_LOOPS (ddr));
3547 fprintf (dump_file, " )\n");
3548 }
3549 fprintf (dump_file, ")\n");
3550 }
3551
3552 return true;
3553 }
3554
3555 /* Return the direction for a given distance.
3556 FIXME: Computing dir this way is suboptimal, since dir can catch
3557 cases that dist is unable to represent. */
3558
3559 static inline enum data_dependence_direction
3560 dir_from_dist (int dist)
3561 {
3562 if (dist > 0)
3563 return dir_positive;
3564 else if (dist < 0)
3565 return dir_negative;
3566 else
3567 return dir_equal;
3568 }
3569
3570 /* Compute the classic per loop direction vector. DDR is the data
3571 dependence relation to build a vector from. */
3572
3573 static void
3574 build_classic_dir_vector (struct data_dependence_relation *ddr)
3575 {
3576 unsigned i, j;
3577 lambda_vector dist_v;
3578
3579 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
3580 {
3581 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3582
3583 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3584 dir_v[j] = dir_from_dist (dist_v[j]);
3585
3586 save_dir_v (ddr, dir_v);
3587 }
3588 }
3589
3590 /* Helper function. Returns true when there is a dependence between
3591 data references DRA and DRB. */
3592
3593 static bool
3594 subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
3595 struct data_reference *dra,
3596 struct data_reference *drb,
3597 struct loop *loop_nest)
3598 {
3599 unsigned int i;
3600 tree last_conflicts;
3601 struct subscript *subscript;
3602 tree res = NULL_TREE;
3603
3604 for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
3605 {
3606 conflict_function *overlaps_a, *overlaps_b;
3607
3608 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3609 DR_ACCESS_FN (drb, i),
3610 &overlaps_a, &overlaps_b,
3611 &last_conflicts, loop_nest);
3612
3613 if (SUB_CONFLICTS_IN_A (subscript))
3614 free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
3615 if (SUB_CONFLICTS_IN_B (subscript))
3616 free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
3617
3618 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3619 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3620 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3621
3622 /* If there is any undetermined conflict function we have to
3623 give a conservative answer in case we cannot prove that
3624 no dependence exists when analyzing another subscript. */
3625 if (CF_NOT_KNOWN_P (overlaps_a)
3626 || CF_NOT_KNOWN_P (overlaps_b))
3627 {
3628 res = chrec_dont_know;
3629 continue;
3630 }
3631
3632 /* When there is a subscript with no dependence we can stop. */
3633 else if (CF_NO_DEPENDENCE_P (overlaps_a)
3634 || CF_NO_DEPENDENCE_P (overlaps_b))
3635 {
3636 res = chrec_known;
3637 break;
3638 }
3639 }
3640
3641 if (res == NULL_TREE)
3642 return true;
3643
3644 if (res == chrec_known)
3645 dependence_stats.num_dependence_independent++;
3646 else
3647 dependence_stats.num_dependence_undetermined++;
3648 finalize_ddr_dependent (ddr, res);
3649 return false;
3650 }
3651
3652 /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
3653
3654 static void
3655 subscript_dependence_tester (struct data_dependence_relation *ddr,
3656 struct loop *loop_nest)
3657 {
3658 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr), loop_nest))
3659 dependence_stats.num_dependence_dependent++;
3660
3661 compute_subscript_distance (ddr);
3662 if (build_classic_dist_vector (ddr, loop_nest))
3663 build_classic_dir_vector (ddr);
3664 }
3665
3666 /* Returns true when all the access functions of A are affine or
3667 constant with respect to LOOP_NEST. */
3668
3669 static bool
3670 access_functions_are_affine_or_constant_p (const struct data_reference *a,
3671 const struct loop *loop_nest)
3672 {
3673 unsigned int i;
3674 vec<tree> fns = DR_ACCESS_FNS (a);
3675 tree t;
3676
3677 FOR_EACH_VEC_ELT (fns, i, t)
3678 if (!evolution_function_is_invariant_p (t, loop_nest->num)
3679 && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
3680 return false;
3681
3682 return true;
3683 }
3684
3685 /* Initializes an equation for an OMEGA problem using the information
3686 contained in the ACCESS_FUN. Returns true when the operation
3687 succeeded.
3688
3689 PB is the omega constraint system.
3690 EQ is the number of the equation to be initialized.
3691 OFFSET is used for shifting the variables names in the constraints:
3692 a constrain is composed of 2 * the number of variables surrounding
3693 dependence accesses. OFFSET is set either to 0 for the first n variables,
3694 then it is set to n.
3695 ACCESS_FUN is expected to be an affine chrec. */
3696
3697 static bool
3698 init_omega_eq_with_af (omega_pb pb, unsigned eq,
3699 unsigned int offset, tree access_fun,
3700 struct data_dependence_relation *ddr)
3701 {
3702 switch (TREE_CODE (access_fun))
3703 {
3704 case POLYNOMIAL_CHREC:
3705 {
3706 tree left = CHREC_LEFT (access_fun);
3707 tree right = CHREC_RIGHT (access_fun);
3708 int var = CHREC_VARIABLE (access_fun);
3709 unsigned var_idx;
3710
3711 if (TREE_CODE (right) != INTEGER_CST)
3712 return false;
3713
3714 var_idx = index_in_loop_nest (var, DDR_LOOP_NEST (ddr));
3715 pb->eqs[eq].coef[offset + var_idx + 1] = int_cst_value (right);
3716
3717 /* Compute the innermost loop index. */
3718 DDR_INNER_LOOP (ddr) = MAX (DDR_INNER_LOOP (ddr), var_idx);
3719
3720 if (offset == 0)
3721 pb->eqs[eq].coef[var_idx + DDR_NB_LOOPS (ddr) + 1]
3722 += int_cst_value (right);
3723
3724 switch (TREE_CODE (left))
3725 {
3726 case POLYNOMIAL_CHREC:
3727 return init_omega_eq_with_af (pb, eq, offset, left, ddr);
3728
3729 case INTEGER_CST:
3730 pb->eqs[eq].coef[0] += int_cst_value (left);
3731 return true;
3732
3733 default:
3734 return false;
3735 }
3736 }
3737
3738 case INTEGER_CST:
3739 pb->eqs[eq].coef[0] += int_cst_value (access_fun);
3740 return true;
3741
3742 default:
3743 return false;
3744 }
3745 }
3746
3747 /* As explained in the comments preceding init_omega_for_ddr, we have
3748 to set up a system for each loop level, setting outer loops
3749 variation to zero, and current loop variation to positive or zero.
3750 Save each lexico positive distance vector. */
3751
3752 static void
3753 omega_extract_distance_vectors (omega_pb pb,
3754 struct data_dependence_relation *ddr)
3755 {
3756 int eq, geq;
3757 unsigned i, j;
3758 struct loop *loopi, *loopj;
3759 enum omega_result res;
3760
3761 /* Set a new problem for each loop in the nest. The basis is the
3762 problem that we have initialized until now. On top of this we
3763 add new constraints. */
3764 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3765 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3766 {
3767 int dist = 0;
3768 omega_pb copy = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr),
3769 DDR_NB_LOOPS (ddr));
3770
3771 omega_copy_problem (copy, pb);
3772
3773 /* For all the outer loops "loop_j", add "dj = 0". */
3774 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3775 {
3776 eq = omega_add_zero_eq (copy, omega_black);
3777 copy->eqs[eq].coef[j + 1] = 1;
3778 }
3779
3780 /* For "loop_i", add "0 <= di". */
3781 geq = omega_add_zero_geq (copy, omega_black);
3782 copy->geqs[geq].coef[i + 1] = 1;
3783
3784 /* Reduce the constraint system, and test that the current
3785 problem is feasible. */
3786 res = omega_simplify_problem (copy);
3787 if (res == omega_false
3788 || res == omega_unknown
3789 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3790 goto next_problem;
3791
3792 for (eq = 0; eq < copy->num_subs; eq++)
3793 if (copy->subs[eq].key == (int) i + 1)
3794 {
3795 dist = copy->subs[eq].coef[0];
3796 goto found_dist;
3797 }
3798
3799 if (dist == 0)
3800 {
3801 /* Reinitialize problem... */
3802 omega_copy_problem (copy, pb);
3803 for (j = 0; j < i && DDR_LOOP_NEST (ddr).iterate (j, &loopj); j++)
3804 {
3805 eq = omega_add_zero_eq (copy, omega_black);
3806 copy->eqs[eq].coef[j + 1] = 1;
3807 }
3808
3809 /* ..., but this time "di = 1". */
3810 eq = omega_add_zero_eq (copy, omega_black);
3811 copy->eqs[eq].coef[i + 1] = 1;
3812 copy->eqs[eq].coef[0] = -1;
3813
3814 res = omega_simplify_problem (copy);
3815 if (res == omega_false
3816 || res == omega_unknown
3817 || copy->num_geqs > (int) DDR_NB_LOOPS (ddr))
3818 goto next_problem;
3819
3820 for (eq = 0; eq < copy->num_subs; eq++)
3821 if (copy->subs[eq].key == (int) i + 1)
3822 {
3823 dist = copy->subs[eq].coef[0];
3824 goto found_dist;
3825 }
3826 }
3827
3828 found_dist:;
3829 /* Save the lexicographically positive distance vector. */
3830 if (dist >= 0)
3831 {
3832 lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3833 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3834
3835 dist_v[i] = dist;
3836
3837 for (eq = 0; eq < copy->num_subs; eq++)
3838 if (copy->subs[eq].key > 0)
3839 {
3840 dist = copy->subs[eq].coef[0];
3841 dist_v[copy->subs[eq].key - 1] = dist;
3842 }
3843
3844 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3845 dir_v[j] = dir_from_dist (dist_v[j]);
3846
3847 save_dist_v (ddr, dist_v);
3848 save_dir_v (ddr, dir_v);
3849 }
3850
3851 next_problem:;
3852 omega_free_problem (copy);
3853 }
3854 }
3855
3856 /* This is called for each subscript of a tuple of data references:
3857 insert an equality for representing the conflicts. */
3858
3859 static bool
3860 omega_setup_subscript (tree access_fun_a, tree access_fun_b,
3861 struct data_dependence_relation *ddr,
3862 omega_pb pb, bool *maybe_dependent)
3863 {
3864 int eq;
3865 tree type = signed_type_for_types (TREE_TYPE (access_fun_a),
3866 TREE_TYPE (access_fun_b));
3867 tree fun_a = chrec_convert (type, access_fun_a, NULL);
3868 tree fun_b = chrec_convert (type, access_fun_b, NULL);
3869 tree difference = chrec_fold_minus (type, fun_a, fun_b);
3870 tree minus_one;
3871
3872 /* When the fun_a - fun_b is not constant, the dependence is not
3873 captured by the classic distance vector representation. */
3874 if (TREE_CODE (difference) != INTEGER_CST)
3875 return false;
3876
3877 /* ZIV test. */
3878 if (ziv_subscript_p (fun_a, fun_b) && !integer_zerop (difference))
3879 {
3880 /* There is no dependence. */
3881 *maybe_dependent = false;
3882 return true;
3883 }
3884
3885 minus_one = build_int_cst (type, -1);
3886 fun_b = chrec_fold_multiply (type, fun_b, minus_one);
3887
3888 eq = omega_add_zero_eq (pb, omega_black);
3889 if (!init_omega_eq_with_af (pb, eq, DDR_NB_LOOPS (ddr), fun_a, ddr)
3890 || !init_omega_eq_with_af (pb, eq, 0, fun_b, ddr))
3891 /* There is probably a dependence, but the system of
3892 constraints cannot be built: answer "don't know". */
3893 return false;
3894
3895 /* GCD test. */
3896 if (DDR_NB_LOOPS (ddr) != 0 && pb->eqs[eq].coef[0]
3897 && !int_divides_p (lambda_vector_gcd
3898 ((lambda_vector) &(pb->eqs[eq].coef[1]),
3899 2 * DDR_NB_LOOPS (ddr)),
3900 pb->eqs[eq].coef[0]))
3901 {
3902 /* There is no dependence. */
3903 *maybe_dependent = false;
3904 return true;
3905 }
3906
3907 return true;
3908 }
3909
3910 /* Helper function, same as init_omega_for_ddr but specialized for
3911 data references A and B. */
3912
3913 static bool
3914 init_omega_for_ddr_1 (struct data_reference *dra, struct data_reference *drb,
3915 struct data_dependence_relation *ddr,
3916 omega_pb pb, bool *maybe_dependent)
3917 {
3918 unsigned i;
3919 int ineq;
3920 struct loop *loopi;
3921 unsigned nb_loops = DDR_NB_LOOPS (ddr);
3922
3923 /* Insert an equality per subscript. */
3924 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3925 {
3926 if (!omega_setup_subscript (DR_ACCESS_FN (dra, i), DR_ACCESS_FN (drb, i),
3927 ddr, pb, maybe_dependent))
3928 return false;
3929 else if (*maybe_dependent == false)
3930 {
3931 /* There is no dependence. */
3932 DDR_ARE_DEPENDENT (ddr) = chrec_known;
3933 return true;
3934 }
3935 }
3936
3937 /* Insert inequalities: constraints corresponding to the iteration
3938 domain, i.e. the loops surrounding the references "loop_x" and
3939 the distance variables "dx". The layout of the OMEGA
3940 representation is as follows:
3941 - coef[0] is the constant
3942 - coef[1..nb_loops] are the protected variables that will not be
3943 removed by the solver: the "dx"
3944 - coef[nb_loops + 1, 2*nb_loops] are the loop variables: "loop_x".
3945 */
3946 for (i = 0; i <= DDR_INNER_LOOP (ddr)
3947 && DDR_LOOP_NEST (ddr).iterate (i, &loopi); i++)
3948 {
3949 HOST_WIDE_INT nbi = max_stmt_executions_int (loopi);
3950
3951 /* 0 <= loop_x */
3952 ineq = omega_add_zero_geq (pb, omega_black);
3953 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3954
3955 /* 0 <= loop_x + dx */
3956 ineq = omega_add_zero_geq (pb, omega_black);
3957 pb->geqs[ineq].coef[i + nb_loops + 1] = 1;
3958 pb->geqs[ineq].coef[i + 1] = 1;
3959
3960 if (nbi != -1)
3961 {
3962 /* loop_x <= nb_iters */
3963 ineq = omega_add_zero_geq (pb, omega_black);
3964 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3965 pb->geqs[ineq].coef[0] = nbi;
3966
3967 /* loop_x + dx <= nb_iters */
3968 ineq = omega_add_zero_geq (pb, omega_black);
3969 pb->geqs[ineq].coef[i + nb_loops + 1] = -1;
3970 pb->geqs[ineq].coef[i + 1] = -1;
3971 pb->geqs[ineq].coef[0] = nbi;
3972
3973 /* A step "dx" bigger than nb_iters is not feasible, so
3974 add "0 <= nb_iters + dx", */
3975 ineq = omega_add_zero_geq (pb, omega_black);
3976 pb->geqs[ineq].coef[i + 1] = 1;
3977 pb->geqs[ineq].coef[0] = nbi;
3978 /* and "dx <= nb_iters". */
3979 ineq = omega_add_zero_geq (pb, omega_black);
3980 pb->geqs[ineq].coef[i + 1] = -1;
3981 pb->geqs[ineq].coef[0] = nbi;
3982 }
3983 }
3984
3985 omega_extract_distance_vectors (pb, ddr);
3986
3987 return true;
3988 }
3989
3990 /* Sets up the Omega dependence problem for the data dependence
3991 relation DDR. Returns false when the constraint system cannot be
3992 built, ie. when the test answers "don't know". Returns true
3993 otherwise, and when independence has been proved (using one of the
3994 trivial dependence test), set MAYBE_DEPENDENT to false, otherwise
3995 set MAYBE_DEPENDENT to true.
3996
3997 Example: for setting up the dependence system corresponding to the
3998 conflicting accesses
3999
4000 | loop_i
4001 | loop_j
4002 | A[i, i+1] = ...
4003 | ... A[2*j, 2*(i + j)]
4004 | endloop_j
4005 | endloop_i
4006
4007 the following constraints come from the iteration domain:
4008
4009 0 <= i <= Ni
4010 0 <= i + di <= Ni
4011 0 <= j <= Nj
4012 0 <= j + dj <= Nj
4013
4014 where di, dj are the distance variables. The constraints
4015 representing the conflicting elements are:
4016
4017 i = 2 * (j + dj)
4018 i + 1 = 2 * (i + di + j + dj)
4019
4020 For asking that the resulting distance vector (di, dj) be
4021 lexicographically positive, we insert the constraint "di >= 0". If
4022 "di = 0" in the solution, we fix that component to zero, and we
4023 look at the inner loops: we set a new problem where all the outer
4024 loop distances are zero, and fix this inner component to be
4025 positive. When one of the components is positive, we save that
4026 distance, and set a new problem where the distance on this loop is
4027 zero, searching for other distances in the inner loops. Here is
4028 the classic example that illustrates that we have to set for each
4029 inner loop a new problem:
4030
4031 | loop_1
4032 | loop_2
4033 | A[10]
4034 | endloop_2
4035 | endloop_1
4036
4037 we have to save two distances (1, 0) and (0, 1).
4038
4039 Given two array references, refA and refB, we have to set the
4040 dependence problem twice, refA vs. refB and refB vs. refA, and we
4041 cannot do a single test, as refB might occur before refA in the
4042 inner loops, and the contrary when considering outer loops: ex.
4043
4044 | loop_0
4045 | loop_1
4046 | loop_2
4047 | T[{1,+,1}_2][{1,+,1}_1] // refA
4048 | T[{2,+,1}_2][{0,+,1}_1] // refB
4049 | endloop_2
4050 | endloop_1
4051 | endloop_0
4052
4053 refB touches the elements in T before refA, and thus for the same
4054 loop_0 refB precedes refA: ie. the distance vector (0, 1, -1)
4055 but for successive loop_0 iterations, we have (1, -1, 1)
4056
4057 The Omega solver expects the distance variables ("di" in the
4058 previous example) to come first in the constraint system (as
4059 variables to be protected, or "safe" variables), the constraint
4060 system is built using the following layout:
4061
4062 "cst | distance vars | index vars".
4063 */
4064
4065 static bool
4066 init_omega_for_ddr (struct data_dependence_relation *ddr,
4067 bool *maybe_dependent)
4068 {
4069 omega_pb pb;
4070 bool res = false;
4071
4072 *maybe_dependent = true;
4073
4074 if (same_access_functions (ddr))
4075 {
4076 unsigned j;
4077 lambda_vector dir_v;
4078
4079 /* Save the 0 vector. */
4080 save_dist_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
4081 dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4082 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
4083 dir_v[j] = dir_equal;
4084 save_dir_v (ddr, dir_v);
4085
4086 /* Save the dependences carried by outer loops. */
4087 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4088 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4089 maybe_dependent);
4090 omega_free_problem (pb);
4091 return res;
4092 }
4093
4094 /* Omega expects the protected variables (those that have to be kept
4095 after elimination) to appear first in the constraint system.
4096 These variables are the distance variables. In the following
4097 initialization we declare NB_LOOPS safe variables, and the total
4098 number of variables for the constraint system is 2*NB_LOOPS. */
4099 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4100 res = init_omega_for_ddr_1 (DDR_A (ddr), DDR_B (ddr), ddr, pb,
4101 maybe_dependent);
4102 omega_free_problem (pb);
4103
4104 /* Stop computation if not decidable, or no dependence. */
4105 if (res == false || *maybe_dependent == false)
4106 return res;
4107
4108 pb = omega_alloc_problem (2 * DDR_NB_LOOPS (ddr), DDR_NB_LOOPS (ddr));
4109 res = init_omega_for_ddr_1 (DDR_B (ddr), DDR_A (ddr), ddr, pb,
4110 maybe_dependent);
4111 omega_free_problem (pb);
4112
4113 return res;
4114 }
4115
4116 /* Return true when DDR contains the same information as that stored
4117 in DIR_VECTS and in DIST_VECTS, return false otherwise. */
4118
4119 static bool
4120 ddr_consistent_p (FILE *file,
4121 struct data_dependence_relation *ddr,
4122 vec<lambda_vector> dist_vects,
4123 vec<lambda_vector> dir_vects)
4124 {
4125 unsigned int i, j;
4126
4127 /* If dump_file is set, output there. */
4128 if (dump_file && (dump_flags & TDF_DETAILS))
4129 file = dump_file;
4130
4131 if (dist_vects.length () != DDR_NUM_DIST_VECTS (ddr))
4132 {
4133 lambda_vector b_dist_v;
4134 fprintf (file, "\n(Number of distance vectors differ: Banerjee has %d, Omega has %d.\n",
4135 dist_vects.length (),
4136 DDR_NUM_DIST_VECTS (ddr));
4137
4138 fprintf (file, "Banerjee dist vectors:\n");
4139 FOR_EACH_VEC_ELT (dist_vects, i, b_dist_v)
4140 print_lambda_vector (file, b_dist_v, DDR_NB_LOOPS (ddr));
4141
4142 fprintf (file, "Omega dist vectors:\n");
4143 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4144 print_lambda_vector (file, DDR_DIST_VECT (ddr, i), DDR_NB_LOOPS (ddr));
4145
4146 fprintf (file, "data dependence relation:\n");
4147 dump_data_dependence_relation (file, ddr);
4148
4149 fprintf (file, ")\n");
4150 return false;
4151 }
4152
4153 if (dir_vects.length () != DDR_NUM_DIR_VECTS (ddr))
4154 {
4155 fprintf (file, "\n(Number of direction vectors differ: Banerjee has %d, Omega has %d.)\n",
4156 dir_vects.length (),
4157 DDR_NUM_DIR_VECTS (ddr));
4158 return false;
4159 }
4160
4161 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
4162 {
4163 lambda_vector a_dist_v;
4164 lambda_vector b_dist_v = DDR_DIST_VECT (ddr, i);
4165
4166 /* Distance vectors are not ordered in the same way in the DDR
4167 and in the DIST_VECTS: search for a matching vector. */
4168 FOR_EACH_VEC_ELT (dist_vects, j, a_dist_v)
4169 if (lambda_vector_equal (a_dist_v, b_dist_v, DDR_NB_LOOPS (ddr)))
4170 break;
4171
4172 if (j == dist_vects.length ())
4173 {
4174 fprintf (file, "\n(Dist vectors from the first dependence analyzer:\n");
4175 print_dist_vectors (file, dist_vects, DDR_NB_LOOPS (ddr));
4176 fprintf (file, "not found in Omega dist vectors:\n");
4177 print_dist_vectors (file, DDR_DIST_VECTS (ddr), DDR_NB_LOOPS (ddr));
4178 fprintf (file, "data dependence relation:\n");
4179 dump_data_dependence_relation (file, ddr);
4180 fprintf (file, ")\n");
4181 }
4182 }
4183
4184 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
4185 {
4186 lambda_vector a_dir_v;
4187 lambda_vector b_dir_v = DDR_DIR_VECT (ddr, i);
4188
4189 /* Direction vectors are not ordered in the same way in the DDR
4190 and in the DIR_VECTS: search for a matching vector. */
4191 FOR_EACH_VEC_ELT (dir_vects, j, a_dir_v)
4192 if (lambda_vector_equal (a_dir_v, b_dir_v, DDR_NB_LOOPS (ddr)))
4193 break;
4194
4195 if (j == dist_vects.length ())
4196 {
4197 fprintf (file, "\n(Dir vectors from the first dependence analyzer:\n");
4198 print_dir_vectors (file, dir_vects, DDR_NB_LOOPS (ddr));
4199 fprintf (file, "not found in Omega dir vectors:\n");
4200 print_dir_vectors (file, DDR_DIR_VECTS (ddr), DDR_NB_LOOPS (ddr));
4201 fprintf (file, "data dependence relation:\n");
4202 dump_data_dependence_relation (file, ddr);
4203 fprintf (file, ")\n");
4204 }
4205 }
4206
4207 return true;
4208 }
4209
4210 /* This computes the affine dependence relation between A and B with
4211 respect to LOOP_NEST. CHREC_KNOWN is used for representing the
4212 independence between two accesses, while CHREC_DONT_KNOW is used
4213 for representing the unknown relation.
4214
4215 Note that it is possible to stop the computation of the dependence
4216 relation the first time we detect a CHREC_KNOWN element for a given
4217 subscript. */
4218
4219 void
4220 compute_affine_dependence (struct data_dependence_relation *ddr,
4221 struct loop *loop_nest)
4222 {
4223 struct data_reference *dra = DDR_A (ddr);
4224 struct data_reference *drb = DDR_B (ddr);
4225
4226 if (dump_file && (dump_flags & TDF_DETAILS))
4227 {
4228 fprintf (dump_file, "(compute_affine_dependence\n");
4229 fprintf (dump_file, " stmt_a: ");
4230 print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
4231 fprintf (dump_file, " stmt_b: ");
4232 print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
4233 }
4234
4235 /* Analyze only when the dependence relation is not yet known. */
4236 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4237 {
4238 dependence_stats.num_dependence_tests++;
4239
4240 if (access_functions_are_affine_or_constant_p (dra, loop_nest)
4241 && access_functions_are_affine_or_constant_p (drb, loop_nest))
4242 {
4243 subscript_dependence_tester (ddr, loop_nest);
4244
4245 if (flag_check_data_deps)
4246 {
4247 /* Dump the dependences from the first algorithm. */
4248 if (dump_file && (dump_flags & TDF_DETAILS))
4249 {
4250 fprintf (dump_file, "\n\nBanerjee Analyzer\n");
4251 dump_data_dependence_relation (dump_file, ddr);
4252 }
4253
4254 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4255 {
4256 bool maybe_dependent;
4257 vec<lambda_vector> dir_vects, dist_vects;
4258
4259 /* Save the result of the first DD analyzer. */
4260 dist_vects = DDR_DIST_VECTS (ddr);
4261 dir_vects = DDR_DIR_VECTS (ddr);
4262
4263 /* Reset the information. */
4264 DDR_DIST_VECTS (ddr).create (0);
4265 DDR_DIR_VECTS (ddr).create (0);
4266
4267 /* Compute the same information using Omega. */
4268 if (!init_omega_for_ddr (ddr, &maybe_dependent))
4269 goto csys_dont_know;
4270
4271 if (dump_file && (dump_flags & TDF_DETAILS))
4272 {
4273 fprintf (dump_file, "Omega Analyzer\n");
4274 dump_data_dependence_relation (dump_file, ddr);
4275 }
4276
4277 /* Check that we get the same information. */
4278 if (maybe_dependent)
4279 gcc_assert (ddr_consistent_p (stderr, ddr, dist_vects,
4280 dir_vects));
4281 }
4282 }
4283 }
4284
4285 /* As a last case, if the dependence cannot be determined, or if
4286 the dependence is considered too difficult to determine, answer
4287 "don't know". */
4288 else
4289 {
4290 csys_dont_know:;
4291 dependence_stats.num_dependence_undetermined++;
4292
4293 if (dump_file && (dump_flags & TDF_DETAILS))
4294 {
4295 fprintf (dump_file, "Data ref a:\n");
4296 dump_data_reference (dump_file, dra);
4297 fprintf (dump_file, "Data ref b:\n");
4298 dump_data_reference (dump_file, drb);
4299 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
4300 }
4301 finalize_ddr_dependent (ddr, chrec_dont_know);
4302 }
4303 }
4304
4305 if (dump_file && (dump_flags & TDF_DETAILS))
4306 {
4307 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4308 fprintf (dump_file, ") -> no dependence\n");
4309 else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
4310 fprintf (dump_file, ") -> dependence analysis failed\n");
4311 else
4312 fprintf (dump_file, ")\n");
4313 }
4314 }
4315
4316 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
4317 the data references in DATAREFS, in the LOOP_NEST. When
4318 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
4319 relations. Return true when successful, i.e. data references number
4320 is small enough to be handled. */
4321
4322 bool
4323 compute_all_dependences (vec<data_reference_p> datarefs,
4324 vec<ddr_p> *dependence_relations,
4325 vec<loop_p> loop_nest,
4326 bool compute_self_and_rr)
4327 {
4328 struct data_dependence_relation *ddr;
4329 struct data_reference *a, *b;
4330 unsigned int i, j;
4331
4332 if ((int) datarefs.length ()
4333 > PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
4334 {
4335 struct data_dependence_relation *ddr;
4336
4337 /* Insert a single relation into dependence_relations:
4338 chrec_dont_know. */
4339 ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
4340 dependence_relations->safe_push (ddr);
4341 return false;
4342 }
4343
4344 FOR_EACH_VEC_ELT (datarefs, i, a)
4345 for (j = i + 1; datarefs.iterate (j, &b); j++)
4346 if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
4347 {
4348 ddr = initialize_data_dependence_relation (a, b, loop_nest);
4349 dependence_relations->safe_push (ddr);
4350 if (loop_nest.exists ())
4351 compute_affine_dependence (ddr, loop_nest[0]);
4352 }
4353
4354 if (compute_self_and_rr)
4355 FOR_EACH_VEC_ELT (datarefs, i, a)
4356 {
4357 ddr = initialize_data_dependence_relation (a, a, loop_nest);
4358 dependence_relations->safe_push (ddr);
4359 if (loop_nest.exists ())
4360 compute_affine_dependence (ddr, loop_nest[0]);
4361 }
4362
4363 return true;
4364 }
4365
4366 /* Describes a location of a memory reference. */
4367
4368 typedef struct data_ref_loc_d
4369 {
4370 /* The memory reference. */
4371 tree ref;
4372
4373 /* True if the memory reference is read. */
4374 bool is_read;
4375 } data_ref_loc;
4376
4377
4378 /* Stores the locations of memory references in STMT to REFERENCES. Returns
4379 true if STMT clobbers memory, false otherwise. */
4380
4381 static bool
4382 get_references_in_stmt (gimple stmt, vec<data_ref_loc, va_heap> *references)
4383 {
4384 bool clobbers_memory = false;
4385 data_ref_loc ref;
4386 tree op0, op1;
4387 enum gimple_code stmt_code = gimple_code (stmt);
4388
4389 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4390 As we cannot model data-references to not spelled out
4391 accesses give up if they may occur. */
4392 if (stmt_code == GIMPLE_CALL
4393 && !(gimple_call_flags (stmt) & ECF_CONST))
4394 {
4395 /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
4396 if (gimple_call_internal_p (stmt))
4397 switch (gimple_call_internal_fn (stmt))
4398 {
4399 case IFN_GOMP_SIMD_LANE:
4400 {
4401 struct loop *loop = gimple_bb (stmt)->loop_father;
4402 tree uid = gimple_call_arg (stmt, 0);
4403 gcc_assert (TREE_CODE (uid) == SSA_NAME);
4404 if (loop == NULL
4405 || loop->simduid != SSA_NAME_VAR (uid))
4406 clobbers_memory = true;
4407 break;
4408 }
4409 case IFN_MASK_LOAD:
4410 case IFN_MASK_STORE:
4411 break;
4412 default:
4413 clobbers_memory = true;
4414 break;
4415 }
4416 else
4417 clobbers_memory = true;
4418 }
4419 else if (stmt_code == GIMPLE_ASM
4420 && (gimple_asm_volatile_p (as_a <gasm *> (stmt))
4421 || gimple_vuse (stmt)))
4422 clobbers_memory = true;
4423
4424 if (!gimple_vuse (stmt))
4425 return clobbers_memory;
4426
4427 if (stmt_code == GIMPLE_ASSIGN)
4428 {
4429 tree base;
4430 op0 = gimple_assign_lhs (stmt);
4431 op1 = gimple_assign_rhs1 (stmt);
4432
4433 if (DECL_P (op1)
4434 || (REFERENCE_CLASS_P (op1)
4435 && (base = get_base_address (op1))
4436 && TREE_CODE (base) != SSA_NAME))
4437 {
4438 ref.ref = op1;
4439 ref.is_read = true;
4440 references->safe_push (ref);
4441 }
4442 }
4443 else if (stmt_code == GIMPLE_CALL)
4444 {
4445 unsigned i, n;
4446
4447 ref.is_read = false;
4448 if (gimple_call_internal_p (stmt))
4449 switch (gimple_call_internal_fn (stmt))
4450 {
4451 case IFN_MASK_LOAD:
4452 if (gimple_call_lhs (stmt) == NULL_TREE)
4453 break;
4454 ref.is_read = true;
4455 case IFN_MASK_STORE:
4456 ref.ref = fold_build2 (MEM_REF,
4457 ref.is_read
4458 ? TREE_TYPE (gimple_call_lhs (stmt))
4459 : TREE_TYPE (gimple_call_arg (stmt, 3)),
4460 gimple_call_arg (stmt, 0),
4461 gimple_call_arg (stmt, 1));
4462 references->safe_push (ref);
4463 return false;
4464 default:
4465 break;
4466 }
4467
4468 op0 = gimple_call_lhs (stmt);
4469 n = gimple_call_num_args (stmt);
4470 for (i = 0; i < n; i++)
4471 {
4472 op1 = gimple_call_arg (stmt, i);
4473
4474 if (DECL_P (op1)
4475 || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
4476 {
4477 ref.ref = op1;
4478 ref.is_read = true;
4479 references->safe_push (ref);
4480 }
4481 }
4482 }
4483 else
4484 return clobbers_memory;
4485
4486 if (op0
4487 && (DECL_P (op0)
4488 || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
4489 {
4490 ref.ref = op0;
4491 ref.is_read = false;
4492 references->safe_push (ref);
4493 }
4494 return clobbers_memory;
4495 }
4496
4497 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
4498 reference, returns false, otherwise returns true. NEST is the outermost
4499 loop of the loop nest in which the references should be analyzed. */
4500
4501 bool
4502 find_data_references_in_stmt (struct loop *nest, gimple stmt,
4503 vec<data_reference_p> *datarefs)
4504 {
4505 unsigned i;
4506 auto_vec<data_ref_loc, 2> references;
4507 data_ref_loc *ref;
4508 bool ret = true;
4509 data_reference_p dr;
4510
4511 if (get_references_in_stmt (stmt, &references))
4512 return false;
4513
4514 FOR_EACH_VEC_ELT (references, i, ref)
4515 {
4516 dr = create_data_ref (nest, loop_containing_stmt (stmt),
4517 ref->ref, stmt, ref->is_read);
4518 gcc_assert (dr != NULL);
4519 datarefs->safe_push (dr);
4520 }
4521 references.release ();
4522 return ret;
4523 }
4524
4525 /* Stores the data references in STMT to DATAREFS. If there is an
4526 unanalyzable reference, returns false, otherwise returns true.
4527 NEST is the outermost loop of the loop nest in which the references
4528 should be instantiated, LOOP is the loop in which the references
4529 should be analyzed. */
4530
4531 bool
4532 graphite_find_data_references_in_stmt (loop_p nest, loop_p loop, gimple stmt,
4533 vec<data_reference_p> *datarefs)
4534 {
4535 unsigned i;
4536 auto_vec<data_ref_loc, 2> references;
4537 data_ref_loc *ref;
4538 bool ret = true;
4539 data_reference_p dr;
4540
4541 if (get_references_in_stmt (stmt, &references))
4542 return false;
4543
4544 FOR_EACH_VEC_ELT (references, i, ref)
4545 {
4546 dr = create_data_ref (nest, loop, ref->ref, stmt, ref->is_read);
4547 gcc_assert (dr != NULL);
4548 datarefs->safe_push (dr);
4549 }
4550
4551 references.release ();
4552 return ret;
4553 }
4554
4555 /* Search the data references in LOOP, and record the information into
4556 DATAREFS. Returns chrec_dont_know when failing to analyze a
4557 difficult case, returns NULL_TREE otherwise. */
4558
4559 tree
4560 find_data_references_in_bb (struct loop *loop, basic_block bb,
4561 vec<data_reference_p> *datarefs)
4562 {
4563 gimple_stmt_iterator bsi;
4564
4565 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4566 {
4567 gimple stmt = gsi_stmt (bsi);
4568
4569 if (!find_data_references_in_stmt (loop, stmt, datarefs))
4570 {
4571 struct data_reference *res;
4572 res = XCNEW (struct data_reference);
4573 datarefs->safe_push (res);
4574
4575 return chrec_dont_know;
4576 }
4577 }
4578
4579 return NULL_TREE;
4580 }
4581
4582 /* Search the data references in LOOP, and record the information into
4583 DATAREFS. Returns chrec_dont_know when failing to analyze a
4584 difficult case, returns NULL_TREE otherwise.
4585
4586 TODO: This function should be made smarter so that it can handle address
4587 arithmetic as if they were array accesses, etc. */
4588
4589 tree
4590 find_data_references_in_loop (struct loop *loop,
4591 vec<data_reference_p> *datarefs)
4592 {
4593 basic_block bb, *bbs;
4594 unsigned int i;
4595
4596 bbs = get_loop_body_in_dom_order (loop);
4597
4598 for (i = 0; i < loop->num_nodes; i++)
4599 {
4600 bb = bbs[i];
4601
4602 if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
4603 {
4604 free (bbs);
4605 return chrec_dont_know;
4606 }
4607 }
4608 free (bbs);
4609
4610 return NULL_TREE;
4611 }
4612
4613 /* Recursive helper function. */
4614
4615 static bool
4616 find_loop_nest_1 (struct loop *loop, vec<loop_p> *loop_nest)
4617 {
4618 /* Inner loops of the nest should not contain siblings. Example:
4619 when there are two consecutive loops,
4620
4621 | loop_0
4622 | loop_1
4623 | A[{0, +, 1}_1]
4624 | endloop_1
4625 | loop_2
4626 | A[{0, +, 1}_2]
4627 | endloop_2
4628 | endloop_0
4629
4630 the dependence relation cannot be captured by the distance
4631 abstraction. */
4632 if (loop->next)
4633 return false;
4634
4635 loop_nest->safe_push (loop);
4636 if (loop->inner)
4637 return find_loop_nest_1 (loop->inner, loop_nest);
4638 return true;
4639 }
4640
4641 /* Return false when the LOOP is not well nested. Otherwise return
4642 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4643 contain the loops from the outermost to the innermost, as they will
4644 appear in the classic distance vector. */
4645
4646 bool
4647 find_loop_nest (struct loop *loop, vec<loop_p> *loop_nest)
4648 {
4649 loop_nest->safe_push (loop);
4650 if (loop->inner)
4651 return find_loop_nest_1 (loop->inner, loop_nest);
4652 return true;
4653 }
4654
4655 /* Returns true when the data dependences have been computed, false otherwise.
4656 Given a loop nest LOOP, the following vectors are returned:
4657 DATAREFS is initialized to all the array elements contained in this loop,
4658 DEPENDENCE_RELATIONS contains the relations between the data references.
4659 Compute read-read and self relations if
4660 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4661
4662 bool
4663 compute_data_dependences_for_loop (struct loop *loop,
4664 bool compute_self_and_read_read_dependences,
4665 vec<loop_p> *loop_nest,
4666 vec<data_reference_p> *datarefs,
4667 vec<ddr_p> *dependence_relations)
4668 {
4669 bool res = true;
4670
4671 memset (&dependence_stats, 0, sizeof (dependence_stats));
4672
4673 /* If the loop nest is not well formed, or one of the data references
4674 is not computable, give up without spending time to compute other
4675 dependences. */
4676 if (!loop
4677 || !find_loop_nest (loop, loop_nest)
4678 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
4679 || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
4680 compute_self_and_read_read_dependences))
4681 res = false;
4682
4683 if (dump_file && (dump_flags & TDF_STATS))
4684 {
4685 fprintf (dump_file, "Dependence tester statistics:\n");
4686
4687 fprintf (dump_file, "Number of dependence tests: %d\n",
4688 dependence_stats.num_dependence_tests);
4689 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
4690 dependence_stats.num_dependence_dependent);
4691 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
4692 dependence_stats.num_dependence_independent);
4693 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
4694 dependence_stats.num_dependence_undetermined);
4695
4696 fprintf (dump_file, "Number of subscript tests: %d\n",
4697 dependence_stats.num_subscript_tests);
4698 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
4699 dependence_stats.num_subscript_undetermined);
4700 fprintf (dump_file, "Number of same subscript function: %d\n",
4701 dependence_stats.num_same_subscript_function);
4702
4703 fprintf (dump_file, "Number of ziv tests: %d\n",
4704 dependence_stats.num_ziv);
4705 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4706 dependence_stats.num_ziv_dependent);
4707 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4708 dependence_stats.num_ziv_independent);
4709 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
4710 dependence_stats.num_ziv_unimplemented);
4711
4712 fprintf (dump_file, "Number of siv tests: %d\n",
4713 dependence_stats.num_siv);
4714 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4715 dependence_stats.num_siv_dependent);
4716 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4717 dependence_stats.num_siv_independent);
4718 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4719 dependence_stats.num_siv_unimplemented);
4720
4721 fprintf (dump_file, "Number of miv tests: %d\n",
4722 dependence_stats.num_miv);
4723 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4724 dependence_stats.num_miv_dependent);
4725 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4726 dependence_stats.num_miv_independent);
4727 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4728 dependence_stats.num_miv_unimplemented);
4729 }
4730
4731 return res;
4732 }
4733
4734 /* Returns true when the data dependences for the basic block BB have been
4735 computed, false otherwise.
4736 DATAREFS is initialized to all the array elements contained in this basic
4737 block, DEPENDENCE_RELATIONS contains the relations between the data
4738 references. Compute read-read and self relations if
4739 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4740 bool
4741 compute_data_dependences_for_bb (basic_block bb,
4742 bool compute_self_and_read_read_dependences,
4743 vec<data_reference_p> *datarefs,
4744 vec<ddr_p> *dependence_relations)
4745 {
4746 if (find_data_references_in_bb (NULL, bb, datarefs) == chrec_dont_know)
4747 return false;
4748
4749 return compute_all_dependences (*datarefs, dependence_relations, vNULL,
4750 compute_self_and_read_read_dependences);
4751 }
4752
4753 /* Entry point (for testing only). Analyze all the data references
4754 and the dependence relations in LOOP.
4755
4756 The data references are computed first.
4757
4758 A relation on these nodes is represented by a complete graph. Some
4759 of the relations could be of no interest, thus the relations can be
4760 computed on demand.
4761
4762 In the following function we compute all the relations. This is
4763 just a first implementation that is here for:
4764 - for showing how to ask for the dependence relations,
4765 - for the debugging the whole dependence graph,
4766 - for the dejagnu testcases and maintenance.
4767
4768 It is possible to ask only for a part of the graph, avoiding to
4769 compute the whole dependence graph. The computed dependences are
4770 stored in a knowledge base (KB) such that later queries don't
4771 recompute the same information. The implementation of this KB is
4772 transparent to the optimizer, and thus the KB can be changed with a
4773 more efficient implementation, or the KB could be disabled. */
4774 static void
4775 analyze_all_data_dependences (struct loop *loop)
4776 {
4777 unsigned int i;
4778 int nb_data_refs = 10;
4779 vec<data_reference_p> datarefs;
4780 datarefs.create (nb_data_refs);
4781 vec<ddr_p> dependence_relations;
4782 dependence_relations.create (nb_data_refs * nb_data_refs);
4783 vec<loop_p> loop_nest;
4784 loop_nest.create (3);
4785
4786 /* Compute DDs on the whole function. */
4787 compute_data_dependences_for_loop (loop, false, &loop_nest, &datarefs,
4788 &dependence_relations);
4789
4790 if (dump_file)
4791 {
4792 dump_data_dependence_relations (dump_file, dependence_relations);
4793 fprintf (dump_file, "\n\n");
4794
4795 if (dump_flags & TDF_DETAILS)
4796 dump_dist_dir_vectors (dump_file, dependence_relations);
4797
4798 if (dump_flags & TDF_STATS)
4799 {
4800 unsigned nb_top_relations = 0;
4801 unsigned nb_bot_relations = 0;
4802 unsigned nb_chrec_relations = 0;
4803 struct data_dependence_relation *ddr;
4804
4805 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4806 {
4807 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
4808 nb_top_relations++;
4809
4810 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4811 nb_bot_relations++;
4812
4813 else
4814 nb_chrec_relations++;
4815 }
4816
4817 gather_stats_on_scev_database ();
4818 }
4819 }
4820
4821 loop_nest.release ();
4822 free_dependence_relations (dependence_relations);
4823 free_data_refs (datarefs);
4824 }
4825
4826 /* Computes all the data dependences and check that the results of
4827 several analyzers are the same. */
4828
4829 void
4830 tree_check_data_deps (void)
4831 {
4832 struct loop *loop_nest;
4833
4834 FOR_EACH_LOOP (loop_nest, 0)
4835 analyze_all_data_dependences (loop_nest);
4836 }
4837
4838 /* Free the memory used by a data dependence relation DDR. */
4839
4840 void
4841 free_dependence_relation (struct data_dependence_relation *ddr)
4842 {
4843 if (ddr == NULL)
4844 return;
4845
4846 if (DDR_SUBSCRIPTS (ddr).exists ())
4847 free_subscripts (DDR_SUBSCRIPTS (ddr));
4848 DDR_DIST_VECTS (ddr).release ();
4849 DDR_DIR_VECTS (ddr).release ();
4850
4851 free (ddr);
4852 }
4853
4854 /* Free the memory used by the data dependence relations from
4855 DEPENDENCE_RELATIONS. */
4856
4857 void
4858 free_dependence_relations (vec<ddr_p> dependence_relations)
4859 {
4860 unsigned int i;
4861 struct data_dependence_relation *ddr;
4862
4863 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
4864 if (ddr)
4865 free_dependence_relation (ddr);
4866
4867 dependence_relations.release ();
4868 }
4869
4870 /* Free the memory used by the data references from DATAREFS. */
4871
4872 void
4873 free_data_refs (vec<data_reference_p> datarefs)
4874 {
4875 unsigned int i;
4876 struct data_reference *dr;
4877
4878 FOR_EACH_VEC_ELT (datarefs, i, dr)
4879 free_data_ref (dr);
4880 datarefs.release ();
4881 }