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