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