Line data Source code
1 : /* Data references and dependences detectors.
2 : Copyright (C) 2003-2026 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 : #define INCLUDE_ALGORITHM
77 : #include "config.h"
78 : #include "system.h"
79 : #include "coretypes.h"
80 : #include "backend.h"
81 : #include "rtl.h"
82 : #include "tree.h"
83 : #include "gimple.h"
84 : #include "gimple-pretty-print.h"
85 : #include "alias.h"
86 : #include "fold-const.h"
87 : #include "expr.h"
88 : #include "gimple-iterator.h"
89 : #include "tree-ssa-loop-niter.h"
90 : #include "tree-ssa-loop.h"
91 : #include "tree-ssa.h"
92 : #include "cfgloop.h"
93 : #include "tree-data-ref.h"
94 : #include "tree-scalar-evolution.h"
95 : #include "dumpfile.h"
96 : #include "tree-affine.h"
97 : #include "builtins.h"
98 : #include "tree-eh.h"
99 : #include "ssa.h"
100 : #include "internal-fn.h"
101 : #include "vr-values.h"
102 : #include "range-op.h"
103 : #include "tree-ssa-loop-ivopts.h"
104 : #include "calls.h"
105 :
106 : static struct datadep_stats
107 : {
108 : int num_dependence_tests;
109 : int num_dependence_dependent;
110 : int num_dependence_independent;
111 : int num_dependence_undetermined;
112 :
113 : int num_subscript_tests;
114 : int num_subscript_undetermined;
115 : int num_same_subscript_function;
116 :
117 : int num_ziv;
118 : int num_ziv_independent;
119 : int num_ziv_dependent;
120 : int num_ziv_unimplemented;
121 :
122 : int num_siv;
123 : int num_siv_independent;
124 : int num_siv_dependent;
125 : int num_siv_unimplemented;
126 :
127 : int num_miv;
128 : int num_miv_independent;
129 : int num_miv_dependent;
130 : int num_miv_unimplemented;
131 : } dependence_stats;
132 :
133 : static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
134 : unsigned int, unsigned int,
135 : class loop *);
136 : /* Returns true iff A divides B. */
137 :
138 : static inline bool
139 2075 : tree_fold_divides_p (const_tree a, const_tree b)
140 : {
141 2075 : gcc_assert (TREE_CODE (a) == INTEGER_CST);
142 2075 : gcc_assert (TREE_CODE (b) == INTEGER_CST);
143 2075 : return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a));
144 : }
145 :
146 : /* Returns true iff A divides B. */
147 :
148 : static inline bool
149 1799436 : int_divides_p (lambda_int a, lambda_int b)
150 : {
151 1799436 : return ((b % a) == 0);
152 : }
153 :
154 : /* Return true if reference REF contains a union access. */
155 :
156 : static bool
157 502572 : ref_contains_union_access_p (tree ref)
158 : {
159 549222 : while (handled_component_p (ref))
160 : {
161 105188 : ref = TREE_OPERAND (ref, 0);
162 210376 : if (TREE_CODE (TREE_TYPE (ref)) == UNION_TYPE
163 105188 : || TREE_CODE (TREE_TYPE (ref)) == QUAL_UNION_TYPE)
164 : return true;
165 : }
166 : return false;
167 : }
168 :
169 :
170 :
171 : /* Dump into FILE all the data references from DATAREFS. */
172 :
173 : static void
174 0 : dump_data_references (FILE *file, vec<data_reference_p> datarefs)
175 : {
176 0 : for (data_reference *dr : datarefs)
177 0 : dump_data_reference (file, dr);
178 0 : }
179 :
180 : /* Unified dump into FILE all the data references from DATAREFS. */
181 :
182 : DEBUG_FUNCTION void
183 0 : debug (vec<data_reference_p> &ref)
184 : {
185 0 : dump_data_references (stderr, ref);
186 0 : }
187 :
188 : DEBUG_FUNCTION void
189 0 : debug (vec<data_reference_p> *ptr)
190 : {
191 0 : if (ptr)
192 0 : debug (*ptr);
193 : else
194 0 : fprintf (stderr, "<nil>\n");
195 0 : }
196 :
197 :
198 : /* Dump into STDERR all the data references from DATAREFS. */
199 :
200 : DEBUG_FUNCTION void
201 0 : debug_data_references (vec<data_reference_p> datarefs)
202 : {
203 0 : dump_data_references (stderr, datarefs);
204 0 : }
205 :
206 : /* Print to STDERR the data_reference DR. */
207 :
208 : DEBUG_FUNCTION void
209 0 : debug_data_reference (struct data_reference *dr)
210 : {
211 0 : dump_data_reference (stderr, dr);
212 0 : }
213 :
214 : /* Dump function for a DATA_REFERENCE structure. */
215 :
216 : void
217 3486 : dump_data_reference (FILE *outf,
218 : struct data_reference *dr)
219 : {
220 3486 : unsigned int i;
221 :
222 3486 : fprintf (outf, "#(Data Ref: \n");
223 3486 : fprintf (outf, "# bb: %d \n", gimple_bb (DR_STMT (dr))->index);
224 3486 : fprintf (outf, "# stmt: ");
225 3486 : print_gimple_stmt (outf, DR_STMT (dr), 0);
226 3486 : fprintf (outf, "# ref: ");
227 3486 : print_generic_stmt (outf, DR_REF (dr));
228 3486 : fprintf (outf, "# base_object: ");
229 3486 : print_generic_stmt (outf, DR_BASE_OBJECT (dr));
230 :
231 10810 : for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
232 : {
233 3838 : fprintf (outf, "# Access function %d: ", i);
234 3838 : print_generic_stmt (outf, DR_ACCESS_FN (dr, i));
235 : }
236 3486 : fprintf (outf, "#)\n");
237 3486 : }
238 :
239 : /* Unified dump function for a DATA_REFERENCE structure. */
240 :
241 : DEBUG_FUNCTION void
242 0 : debug (data_reference &ref)
243 : {
244 0 : dump_data_reference (stderr, &ref);
245 0 : }
246 :
247 : DEBUG_FUNCTION void
248 0 : debug (data_reference *ptr)
249 : {
250 0 : if (ptr)
251 0 : debug (*ptr);
252 : else
253 0 : fprintf (stderr, "<nil>\n");
254 0 : }
255 :
256 :
257 : /* Dumps the affine function described by FN to the file OUTF. */
258 :
259 : DEBUG_FUNCTION void
260 33356 : dump_affine_function (FILE *outf, affine_fn fn)
261 : {
262 33356 : unsigned i;
263 33356 : tree coef;
264 :
265 33356 : print_generic_expr (outf, fn[0], TDF_SLIM);
266 70452 : for (i = 1; fn.iterate (i, &coef); i++)
267 : {
268 3740 : fprintf (outf, " + ");
269 3740 : print_generic_expr (outf, coef, TDF_SLIM);
270 3740 : fprintf (outf, " * x_%u", i);
271 : }
272 33356 : }
273 :
274 : /* Dumps the conflict function CF to the file OUTF. */
275 :
276 : DEBUG_FUNCTION void
277 170160 : dump_conflict_function (FILE *outf, conflict_function *cf)
278 : {
279 170160 : unsigned i;
280 :
281 170160 : if (cf->n == NO_DEPENDENCE)
282 130660 : fprintf (outf, "no dependence");
283 39500 : else if (cf->n == NOT_KNOWN)
284 6144 : fprintf (outf, "not known");
285 : else
286 : {
287 66712 : for (i = 0; i < cf->n; i++)
288 : {
289 33356 : if (i != 0)
290 0 : fprintf (outf, " ");
291 33356 : fprintf (outf, "[");
292 33356 : dump_affine_function (outf, cf->fns[i]);
293 33356 : fprintf (outf, "]");
294 : }
295 : }
296 170160 : }
297 :
298 : /* Dump function for a SUBSCRIPT structure. */
299 :
300 : DEBUG_FUNCTION void
301 844 : dump_subscript (FILE *outf, struct subscript *subscript)
302 : {
303 844 : conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
304 :
305 844 : fprintf (outf, "\n (subscript \n");
306 844 : fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
307 844 : dump_conflict_function (outf, cf);
308 844 : if (CF_NONTRIVIAL_P (cf))
309 : {
310 844 : tree last_iteration = SUB_LAST_CONFLICT (subscript);
311 844 : fprintf (outf, "\n last_conflict: ");
312 844 : print_generic_expr (outf, last_iteration);
313 : }
314 :
315 844 : cf = SUB_CONFLICTS_IN_B (subscript);
316 844 : fprintf (outf, "\n iterations_that_access_an_element_twice_in_B: ");
317 844 : dump_conflict_function (outf, cf);
318 844 : if (CF_NONTRIVIAL_P (cf))
319 : {
320 844 : tree last_iteration = SUB_LAST_CONFLICT (subscript);
321 844 : fprintf (outf, "\n last_conflict: ");
322 844 : print_generic_expr (outf, last_iteration);
323 : }
324 :
325 844 : fprintf (outf, "\n (Subscript distance: ");
326 844 : print_generic_expr (outf, SUB_DISTANCE (subscript));
327 844 : fprintf (outf, " ))\n");
328 844 : }
329 :
330 : /* Print the classic direction vector DIRV to OUTF. */
331 :
332 : DEBUG_FUNCTION void
333 784 : print_direction_vector (FILE *outf,
334 : lambda_vector dirv,
335 : int length)
336 : {
337 784 : int eq;
338 :
339 1704 : for (eq = 0; eq < length; eq++)
340 : {
341 920 : enum data_dependence_direction dir = ((enum data_dependence_direction)
342 920 : dirv[eq]);
343 :
344 920 : switch (dir)
345 : {
346 146 : case dir_positive:
347 146 : fprintf (outf, " +");
348 146 : break;
349 8 : case dir_negative:
350 8 : fprintf (outf, " -");
351 8 : break;
352 766 : case dir_equal:
353 766 : fprintf (outf, " =");
354 766 : break;
355 0 : case dir_positive_or_equal:
356 0 : fprintf (outf, " +=");
357 0 : break;
358 0 : case dir_positive_or_negative:
359 0 : fprintf (outf, " +-");
360 0 : break;
361 0 : case dir_negative_or_equal:
362 0 : fprintf (outf, " -=");
363 0 : break;
364 0 : case dir_star:
365 0 : fprintf (outf, " *");
366 0 : break;
367 0 : default:
368 0 : fprintf (outf, "indep");
369 0 : break;
370 : }
371 : }
372 784 : fprintf (outf, "\n");
373 784 : }
374 :
375 : /* Print a vector of direction vectors. */
376 :
377 : DEBUG_FUNCTION void
378 0 : print_dir_vectors (FILE *outf, vec<lambda_vector> dir_vects,
379 : int length)
380 : {
381 0 : for (lambda_vector v : dir_vects)
382 0 : print_direction_vector (outf, v, length);
383 0 : }
384 :
385 : /* Print out a vector VEC of length N to OUTFILE. */
386 :
387 : DEBUG_FUNCTION void
388 5061 : print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
389 : {
390 5061 : int i;
391 :
392 10644 : for (i = 0; i < n; i++)
393 5583 : fprintf (outfile, HOST_WIDE_INT_PRINT_DEC " ", vector[i]);
394 5061 : fprintf (outfile, "\n");
395 5061 : }
396 :
397 : /* Print a vector of distance vectors. */
398 :
399 : DEBUG_FUNCTION void
400 0 : print_dist_vectors (FILE *outf, vec<lambda_vector> dist_vects,
401 : int length)
402 : {
403 0 : for (lambda_vector v : dist_vects)
404 0 : print_lambda_vector (outf, v, length);
405 0 : }
406 :
407 : /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
408 :
409 : DEBUG_FUNCTION void
410 1585 : dump_data_dependence_relation (FILE *outf, const data_dependence_relation *ddr)
411 : {
412 1585 : struct data_reference *dra, *drb;
413 :
414 1585 : fprintf (outf, "(Data Dep: \n");
415 :
416 1585 : if (!ddr || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
417 : {
418 399 : if (ddr)
419 : {
420 399 : dra = DDR_A (ddr);
421 399 : drb = DDR_B (ddr);
422 399 : if (dra)
423 399 : dump_data_reference (outf, dra);
424 : else
425 0 : fprintf (outf, " (nil)\n");
426 399 : if (drb)
427 399 : dump_data_reference (outf, drb);
428 : else
429 0 : fprintf (outf, " (nil)\n");
430 : }
431 399 : fprintf (outf, " (don't know)\n)\n");
432 399 : return;
433 : }
434 :
435 1186 : dra = DDR_A (ddr);
436 1186 : drb = DDR_B (ddr);
437 1186 : dump_data_reference (outf, dra);
438 1186 : dump_data_reference (outf, drb);
439 :
440 1186 : if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
441 426 : fprintf (outf, " (no dependence)\n");
442 :
443 760 : else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
444 : {
445 : unsigned int i;
446 : class loop *loopi;
447 :
448 : subscript *sub;
449 1604 : FOR_EACH_VEC_ELT (DDR_SUBSCRIPTS (ddr), i, sub)
450 : {
451 844 : fprintf (outf, " access_fn_A: ");
452 844 : print_generic_stmt (outf, SUB_ACCESS_FN (sub, 0));
453 844 : fprintf (outf, " access_fn_B: ");
454 844 : print_generic_stmt (outf, SUB_ACCESS_FN (sub, 1));
455 844 : dump_subscript (outf, sub);
456 : }
457 :
458 760 : fprintf (outf, " loop nest: (");
459 2386 : FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
460 866 : fprintf (outf, "%d ", loopi->num);
461 760 : fprintf (outf, ")\n");
462 :
463 3843 : for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
464 : {
465 784 : fprintf (outf, " distance_vector: ");
466 784 : print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
467 1568 : DDR_NB_LOOPS (ddr));
468 : }
469 :
470 1544 : for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
471 : {
472 784 : fprintf (outf, " direction_vector: ");
473 784 : print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
474 1568 : DDR_NB_LOOPS (ddr));
475 : }
476 : }
477 :
478 1186 : fprintf (outf, ")\n");
479 : }
480 :
481 : /* Debug version. */
482 :
483 : DEBUG_FUNCTION void
484 0 : debug_data_dependence_relation (const struct data_dependence_relation *ddr)
485 : {
486 0 : dump_data_dependence_relation (stderr, ddr);
487 0 : }
488 :
489 : /* Dump into FILE all the dependence relations from DDRS. */
490 :
491 : DEBUG_FUNCTION void
492 308 : dump_data_dependence_relations (FILE *file, const vec<ddr_p> &ddrs)
493 : {
494 2479 : for (auto ddr : ddrs)
495 1585 : dump_data_dependence_relation (file, ddr);
496 308 : }
497 :
498 : DEBUG_FUNCTION void
499 0 : debug (vec<ddr_p> &ref)
500 : {
501 0 : dump_data_dependence_relations (stderr, ref);
502 0 : }
503 :
504 : DEBUG_FUNCTION void
505 0 : debug (vec<ddr_p> *ptr)
506 : {
507 0 : if (ptr)
508 0 : debug (*ptr);
509 : else
510 0 : fprintf (stderr, "<nil>\n");
511 0 : }
512 :
513 :
514 : /* Dump to STDERR all the dependence relations from DDRS. */
515 :
516 : DEBUG_FUNCTION void
517 0 : debug_data_dependence_relations (vec<ddr_p> ddrs)
518 : {
519 0 : dump_data_dependence_relations (stderr, ddrs);
520 0 : }
521 :
522 : /* Dumps the distance and direction vectors in FILE. DDRS contains
523 : the dependence relations, and VECT_SIZE is the size of the
524 : dependence vectors, or in other words the number of loops in the
525 : considered nest. */
526 :
527 : DEBUG_FUNCTION void
528 0 : dump_dist_dir_vectors (FILE *file, vec<ddr_p> ddrs)
529 : {
530 0 : for (data_dependence_relation *ddr : ddrs)
531 0 : if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
532 : {
533 0 : for (lambda_vector v : DDR_DIST_VECTS (ddr))
534 : {
535 0 : fprintf (file, "DISTANCE_V (");
536 0 : print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
537 0 : fprintf (file, ")\n");
538 : }
539 :
540 0 : for (lambda_vector v : DDR_DIR_VECTS (ddr))
541 : {
542 0 : fprintf (file, "DIRECTION_V (");
543 0 : print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
544 0 : fprintf (file, ")\n");
545 : }
546 : }
547 :
548 0 : fprintf (file, "\n\n");
549 0 : }
550 :
551 : /* Dumps the data dependence relations DDRS in FILE. */
552 :
553 : DEBUG_FUNCTION void
554 0 : dump_ddrs (FILE *file, vec<ddr_p> ddrs)
555 : {
556 0 : for (data_dependence_relation *ddr : ddrs)
557 0 : dump_data_dependence_relation (file, ddr);
558 :
559 0 : fprintf (file, "\n\n");
560 0 : }
561 :
562 : DEBUG_FUNCTION void
563 0 : debug_ddrs (vec<ddr_p> ddrs)
564 : {
565 0 : dump_ddrs (stderr, ddrs);
566 0 : }
567 :
568 : /* If RESULT_RANGE is nonnull, set *RESULT_RANGE to the range of
569 : OP0 CODE OP1, where:
570 :
571 : - OP0 CODE OP1 has integral type TYPE
572 : - the range of OP0 is given by OP0_RANGE and
573 : - the range of OP1 is given by OP1_RANGE.
574 :
575 : Independently of RESULT_RANGE, try to compute:
576 :
577 : DELTA = ((sizetype) OP0 CODE (sizetype) OP1)
578 : - (sizetype) (OP0 CODE OP1)
579 :
580 : as a constant and subtract DELTA from the ssizetype constant in *OFF.
581 : Return true on success, or false if DELTA is not known at compile time.
582 :
583 : Truncation and sign changes are known to distribute over CODE, i.e.
584 :
585 : (itype) (A CODE B) == (itype) A CODE (itype) B
586 :
587 : for any integral type ITYPE whose precision is no greater than the
588 : precision of A and B. */
589 :
590 : static bool
591 4685154 : compute_distributive_range (tree type, irange &op0_range,
592 : tree_code code, irange &op1_range,
593 : tree *off, irange *result_range)
594 : {
595 4685154 : gcc_assert (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_TRAPS (type));
596 4685154 : if (result_range)
597 : {
598 1077080 : range_op_handler op (code);
599 1077080 : if (!op.fold_range (*result_range, type, op0_range, op1_range))
600 0 : result_range->set_varying (type);
601 : }
602 :
603 : /* The distributive property guarantees that if TYPE is no narrower
604 : than SIZETYPE,
605 :
606 : (sizetype) (OP0 CODE OP1) == (sizetype) OP0 CODE (sizetype) OP1
607 :
608 : and so we can treat DELTA as zero. */
609 4685154 : if (TYPE_PRECISION (type) >= TYPE_PRECISION (sizetype))
610 : return true;
611 :
612 : /* If overflow is undefined, we can assume that:
613 :
614 : X == (ssizetype) OP0 CODE (ssizetype) OP1
615 :
616 : is within the range of TYPE, i.e.:
617 :
618 : X == (ssizetype) (TYPE) X
619 :
620 : Distributing the (TYPE) truncation over X gives:
621 :
622 : X == (ssizetype) (OP0 CODE OP1)
623 :
624 : Casting both sides to sizetype and distributing the sizetype cast
625 : over X gives:
626 :
627 : (sizetype) OP0 CODE (sizetype) OP1 == (sizetype) (OP0 CODE OP1)
628 :
629 : and so we can treat DELTA as zero. */
630 283794 : if (TYPE_OVERFLOW_UNDEFINED (type))
631 : return true;
632 :
633 : /* Compute the range of:
634 :
635 : (ssizetype) OP0 CODE (ssizetype) OP1
636 :
637 : The distributive property guarantees that this has the same bitpattern as:
638 :
639 : (sizetype) OP0 CODE (sizetype) OP1
640 :
641 : but its range is more conducive to analysis. */
642 103614 : range_cast (op0_range, ssizetype);
643 103614 : range_cast (op1_range, ssizetype);
644 103614 : int_range_max wide_range;
645 103614 : range_op_handler op (code);
646 103614 : bool saved_flag_wrapv = flag_wrapv;
647 103614 : flag_wrapv = 1;
648 103614 : if (!op.fold_range (wide_range, ssizetype, op0_range, op1_range))
649 0 : wide_range.set_varying (ssizetype);;
650 103614 : flag_wrapv = saved_flag_wrapv;
651 103614 : if (wide_range.num_pairs () != 1
652 103614 : || wide_range.varying_p () || wide_range.undefined_p ())
653 : return false;
654 :
655 83543 : wide_int lb = wide_range.lower_bound ();
656 83543 : wide_int ub = wide_range.upper_bound ();
657 :
658 : /* Calculate the number of times that each end of the range overflows or
659 : underflows TYPE. We can only calculate DELTA if the numbers match. */
660 83543 : unsigned int precision = TYPE_PRECISION (type);
661 83543 : if (!TYPE_UNSIGNED (type))
662 : {
663 206 : wide_int type_min = wi::mask (precision - 1, true, lb.get_precision ());
664 206 : lb -= type_min;
665 206 : ub -= type_min;
666 206 : }
667 83543 : wide_int upper_bits = wi::mask (precision, true, lb.get_precision ());
668 83543 : lb &= upper_bits;
669 83543 : ub &= upper_bits;
670 83543 : if (lb != ub)
671 : return false;
672 :
673 : /* OP0 CODE OP1 overflows exactly arshift (LB, PRECISION) times, with
674 : negative values indicating underflow. The low PRECISION bits of LB
675 : are clear, so DELTA is therefore LB (== UB). */
676 22224 : *off = wide_int_to_tree (ssizetype, wi::to_wide (*off) - lb);
677 22224 : return true;
678 103614 : }
679 :
680 : /* Return true if (sizetype) OP == (sizetype) (TO_TYPE) OP,
681 : given that OP has type FROM_TYPE and range RANGE. Both TO_TYPE and
682 : FROM_TYPE are integral types. */
683 :
684 : static bool
685 2662454 : nop_conversion_for_offset_p (tree to_type, tree from_type, irange &range)
686 : {
687 2662454 : gcc_assert (INTEGRAL_TYPE_P (to_type)
688 : && INTEGRAL_TYPE_P (from_type)
689 : && !TYPE_OVERFLOW_TRAPS (to_type)
690 : && !TYPE_OVERFLOW_TRAPS (from_type));
691 :
692 : /* Converting to something no narrower than sizetype and then to sizetype
693 : is equivalent to converting directly to sizetype. */
694 2662454 : if (TYPE_PRECISION (to_type) >= TYPE_PRECISION (sizetype))
695 : return true;
696 :
697 : /* Check whether TO_TYPE can represent all values that FROM_TYPE can. */
698 87983 : if (TYPE_PRECISION (from_type) < TYPE_PRECISION (to_type)
699 87983 : && (TYPE_UNSIGNED (from_type) || !TYPE_UNSIGNED (to_type)))
700 : return true;
701 :
702 : /* For narrowing conversions, we could in principle test whether
703 : the bits in FROM_TYPE but not in TO_TYPE have a fixed value
704 : and apply a constant adjustment.
705 :
706 : For other conversions (which involve a sign change) we could
707 : check that the signs are always equal, and apply a constant
708 : adjustment if the signs are negative.
709 :
710 : However, both cases should be rare. */
711 72298 : return range_fits_type_p (&range, TYPE_PRECISION (to_type),
712 144596 : TYPE_SIGN (to_type));
713 : }
714 :
715 : static void
716 : split_constant_offset (tree type, tree *var, tree *off,
717 : irange *result_range,
718 : hash_map<tree, std::pair<tree, tree> > &cache,
719 : unsigned *limit);
720 :
721 : /* Helper function for split_constant_offset. If TYPE is a pointer type,
722 : try to express OP0 CODE OP1 as:
723 :
724 : POINTER_PLUS <*VAR, (sizetype) *OFF>
725 :
726 : where:
727 :
728 : - *VAR has type TYPE
729 : - *OFF is a constant of type ssizetype.
730 :
731 : If TYPE is an integral type, try to express (sizetype) (OP0 CODE OP1) as:
732 :
733 : *VAR + (sizetype) *OFF
734 :
735 : where:
736 :
737 : - *VAR has type sizetype
738 : - *OFF is a constant of type ssizetype.
739 :
740 : In both cases, OP0 CODE OP1 has type TYPE.
741 :
742 : Return true on success. A false return value indicates that we can't
743 : do better than set *OFF to zero.
744 :
745 : When returning true, set RESULT_RANGE to the range of OP0 CODE OP1,
746 : if RESULT_RANGE is nonnull and if we can do better than assume VR_VARYING.
747 :
748 : CACHE caches {*VAR, *OFF} pairs for SSA names that we've previously
749 : visited. LIMIT counts down the number of SSA names that we are
750 : allowed to process before giving up. */
751 :
752 : static bool
753 60198427 : split_constant_offset_1 (tree type, tree op0, enum tree_code code, tree op1,
754 : tree *var, tree *off, irange *result_range,
755 : hash_map<tree, std::pair<tree, tree> > &cache,
756 : unsigned *limit)
757 : {
758 60198427 : tree var0, var1;
759 60198427 : tree off0, off1;
760 60198427 : int_range_max op0_range, op1_range;
761 :
762 60198427 : *var = NULL_TREE;
763 60198427 : *off = NULL_TREE;
764 :
765 60198427 : if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
766 : return false;
767 :
768 60197743 : if (TREE_CODE (op0) == SSA_NAME
769 60197743 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
770 : return false;
771 60197354 : if (op1
772 7707099 : && TREE_CODE (op1) == SSA_NAME
773 62605571 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))
774 : return false;
775 :
776 60197354 : switch (code)
777 : {
778 17981323 : case INTEGER_CST:
779 17981323 : *var = size_int (0);
780 17981323 : *off = fold_convert (ssizetype, op0);
781 17981323 : if (result_range)
782 : {
783 1450972 : wide_int w = wi::to_wide (op0);
784 1450972 : result_range->set (TREE_TYPE (op0), w, w);
785 1450972 : }
786 : return true;
787 :
788 2209973 : case POINTER_PLUS_EXPR:
789 2209973 : split_constant_offset (op0, &var0, &off0, nullptr, cache, limit);
790 2209973 : split_constant_offset (op1, &var1, &off1, nullptr, cache, limit);
791 2209973 : *var = fold_build2 (POINTER_PLUS_EXPR, type, var0, var1);
792 2209973 : *off = size_binop (PLUS_EXPR, off0, off1);
793 2209973 : return true;
794 :
795 2477294 : case PLUS_EXPR:
796 2477294 : case MINUS_EXPR:
797 2477294 : split_constant_offset (op0, &var0, &off0, &op0_range, cache, limit);
798 2477294 : split_constant_offset (op1, &var1, &off1, &op1_range, cache, limit);
799 2477294 : *off = size_binop (code, off0, off1);
800 2477294 : if (!compute_distributive_range (type, op0_range, code, op1_range,
801 : off, result_range))
802 : return false;
803 2415992 : *var = fold_build2 (code, sizetype, var0, var1);
804 2415992 : return true;
805 :
806 2674050 : case MULT_EXPR:
807 2674050 : if (TREE_CODE (op1) != INTEGER_CST)
808 : return false;
809 :
810 2207860 : split_constant_offset (op0, &var0, &off0, &op0_range, cache, limit);
811 2207860 : op1_range.set (TREE_TYPE (op1), wi::to_wide (op1), wi::to_wide (op1));
812 2207860 : *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
813 2207860 : if (!compute_distributive_range (type, op0_range, code, op1_range,
814 : off, result_range))
815 : return false;
816 2187772 : *var = fold_build2 (MULT_EXPR, sizetype, var0,
817 : fold_convert (sizetype, op1));
818 2187772 : return true;
819 :
820 10608327 : case ADDR_EXPR:
821 10608327 : {
822 10608327 : tree base, poffset;
823 10608327 : poly_int64 pbitsize, pbitpos, pbytepos;
824 10608327 : machine_mode pmode;
825 10608327 : int punsignedp, preversep, pvolatilep;
826 :
827 10608327 : op0 = TREE_OPERAND (op0, 0);
828 10608327 : base
829 10608327 : = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset, &pmode,
830 : &punsignedp, &preversep, &pvolatilep);
831 :
832 21216654 : if (!multiple_p (pbitpos, BITS_PER_UNIT, &pbytepos))
833 : return false;
834 10608327 : base = build_fold_addr_expr (base);
835 10608327 : off0 = ssize_int (pbytepos);
836 :
837 10608327 : if (poffset)
838 : {
839 1531 : split_constant_offset (poffset, &poffset, &off1, nullptr,
840 : cache, limit);
841 1531 : off0 = size_binop (PLUS_EXPR, off0, off1);
842 1531 : base = fold_build_pointer_plus (base, poffset);
843 : }
844 :
845 10608327 : var0 = fold_convert (type, base);
846 :
847 : /* If variable length types are involved, punt, otherwise casts
848 : might be converted into ARRAY_REFs in gimplify_conversion.
849 : To compute that ARRAY_REF's element size TYPE_SIZE_UNIT, which
850 : possibly no longer appears in current GIMPLE, might resurface.
851 : This perhaps could run
852 : if (CONVERT_EXPR_P (var0))
853 : {
854 : gimplify_conversion (&var0);
855 : // Attempt to fill in any within var0 found ARRAY_REF's
856 : // element size from corresponding op embedded ARRAY_REF,
857 : // if unsuccessful, just punt.
858 : } */
859 21631587 : while (POINTER_TYPE_P (type))
860 11023260 : type = TREE_TYPE (type);
861 10608327 : if (int_size_in_bytes (type) < 0)
862 : return false;
863 :
864 10581806 : *var = var0;
865 10581806 : *off = off0;
866 10581806 : return true;
867 : }
868 :
869 16300197 : case SSA_NAME:
870 16300197 : {
871 16300197 : auto *def_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (op0));
872 8901716 : if (!def_stmt)
873 : return false;
874 :
875 8901716 : tree_code subcode = gimple_assign_rhs_code (def_stmt);
876 :
877 : /* We are using a cache to avoid un-CSEing large amounts of code. */
878 8901716 : bool use_cache = false;
879 8901716 : if (!has_single_use (op0)
880 8901716 : && (subcode == POINTER_PLUS_EXPR
881 4600324 : || subcode == PLUS_EXPR
882 : || subcode == MINUS_EXPR
883 : || subcode == MULT_EXPR
884 : || subcode == ADDR_EXPR
885 : || CONVERT_EXPR_CODE_P (subcode)))
886 : {
887 2232198 : use_cache = true;
888 2232198 : bool existed;
889 2232198 : std::pair<tree, tree> &e = cache.get_or_insert (op0, &existed);
890 2232198 : if (existed)
891 : {
892 30118 : if (integer_zerop (e.second))
893 30118 : return false;
894 1203 : *var = e.first;
895 1203 : *off = e.second;
896 : /* The caller sets the range in this case. */
897 1203 : return true;
898 : }
899 2202080 : e = std::make_pair (op0, ssize_int (0));
900 : }
901 :
902 8871598 : if (*limit == 0)
903 : return false;
904 8870562 : --*limit;
905 :
906 8870562 : var0 = gimple_assign_rhs1 (def_stmt);
907 8870562 : var1 = gimple_assign_rhs2 (def_stmt);
908 :
909 8870562 : bool res = split_constant_offset_1 (type, var0, subcode, var1,
910 : var, off, nullptr, cache, limit);
911 8870562 : if (res && use_cache)
912 1979683 : *cache.get (op0) = std::make_pair (*var, *off);
913 : /* The caller sets the range in this case. */
914 : return res;
915 : }
916 4437823 : CASE_CONVERT:
917 4437823 : {
918 : /* We can only handle the following conversions:
919 :
920 : - Conversions from one pointer type to another pointer type.
921 :
922 : - Conversions from one non-trapping integral type to another
923 : non-trapping integral type. In this case, the recursive
924 : call makes sure that:
925 :
926 : (sizetype) OP0
927 :
928 : can be expressed as a sizetype operation involving VAR and OFF,
929 : and all we need to do is check whether:
930 :
931 : (sizetype) OP0 == (sizetype) (TYPE) OP0
932 :
933 : - Conversions from a non-trapping sizetype-size integral type to
934 : a like-sized pointer type. In this case, the recursive call
935 : makes sure that:
936 :
937 : (sizetype) OP0 == *VAR + (sizetype) *OFF
938 :
939 : and we can convert that to:
940 :
941 : POINTER_PLUS <(TYPE) *VAR, (sizetype) *OFF>
942 :
943 : - Conversions from a sizetype-sized pointer type to a like-sized
944 : non-trapping integral type. In this case, the recursive call
945 : makes sure that:
946 :
947 : OP0 == POINTER_PLUS <*VAR, (sizetype) *OFF>
948 :
949 : where the POINTER_PLUS and *VAR have the same precision as
950 : TYPE (and the same precision as sizetype). Then:
951 :
952 : (sizetype) (TYPE) OP0 == (sizetype) *VAR + (sizetype) *OFF. */
953 4437823 : tree itype = TREE_TYPE (op0);
954 4437823 : if ((POINTER_TYPE_P (itype)
955 3238285 : || (INTEGRAL_TYPE_P (itype) && !TYPE_OVERFLOW_TRAPS (itype)))
956 4437400 : && (POINTER_TYPE_P (type)
957 3184384 : || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_TRAPS (type)))
958 8875223 : && (POINTER_TYPE_P (type) == POINTER_TYPE_P (itype)
959 1097338 : || (TYPE_PRECISION (type) == TYPE_PRECISION (sizetype)
960 1097338 : && TYPE_PRECISION (itype) == TYPE_PRECISION (sizetype))))
961 : {
962 4437393 : if (POINTER_TYPE_P (type))
963 : {
964 1253009 : split_constant_offset (op0, var, off, nullptr, cache, limit);
965 1253009 : *var = fold_convert (type, *var);
966 : }
967 3184384 : else if (POINTER_TYPE_P (itype))
968 : {
969 521930 : split_constant_offset (op0, var, off, nullptr, cache, limit);
970 521930 : *var = fold_convert (sizetype, *var);
971 : }
972 : else
973 : {
974 2662454 : split_constant_offset (op0, var, off, &op0_range,
975 : cache, limit);
976 2662454 : if (!nop_conversion_for_offset_p (type, itype, op0_range))
977 : return false;
978 2608999 : if (result_range)
979 : {
980 1342326 : *result_range = op0_range;
981 1342326 : range_cast (*result_range, type);
982 : }
983 : }
984 : return true;
985 : }
986 : return false;
987 : }
988 :
989 : default:
990 : return false;
991 : }
992 60198427 : }
993 :
994 : /* If EXP has pointer type, try to express it as:
995 :
996 : POINTER_PLUS <*VAR, (sizetype) *OFF>
997 :
998 : where:
999 :
1000 : - *VAR has the same type as EXP
1001 : - *OFF is a constant of type ssizetype.
1002 :
1003 : If EXP has an integral type, try to express (sizetype) EXP as:
1004 :
1005 : *VAR + (sizetype) *OFF
1006 :
1007 : where:
1008 :
1009 : - *VAR has type sizetype
1010 : - *OFF is a constant of type ssizetype.
1011 :
1012 : If EXP_RANGE is nonnull, set it to the range of EXP.
1013 :
1014 : CACHE caches {*VAR, *OFF} pairs for SSA names that we've previously
1015 : visited. LIMIT counts down the number of SSA names that we are
1016 : allowed to process before giving up. */
1017 :
1018 : static void
1019 51327882 : split_constant_offset (tree exp, tree *var, tree *off, irange *exp_range,
1020 : hash_map<tree, std::pair<tree, tree> > &cache,
1021 : unsigned *limit)
1022 : {
1023 51327882 : tree type = TREE_TYPE (exp), op0, op1;
1024 51327882 : enum tree_code code;
1025 :
1026 51327882 : code = TREE_CODE (exp);
1027 51327882 : if (exp_range)
1028 : {
1029 9824902 : exp_range->set_varying (type);
1030 9824902 : if (code == SSA_NAME)
1031 : {
1032 5434607 : int_range_max vr;
1033 10869214 : get_range_query (cfun)->range_of_expr (vr, exp);
1034 5434607 : if (vr.undefined_p ())
1035 4924 : vr.set_varying (TREE_TYPE (exp));
1036 5434607 : tree vr_min, vr_max;
1037 5434607 : value_range_kind vr_kind = get_legacy_range (vr, vr_min, vr_max);
1038 5434607 : wide_int var_min = wi::to_wide (vr_min);
1039 5434607 : wide_int var_max = wi::to_wide (vr_max);
1040 5434607 : wide_int var_nonzero = get_nonzero_bits (exp);
1041 16303821 : vr_kind = intersect_range_with_nonzero_bits (vr_kind,
1042 : &var_min, &var_max,
1043 : var_nonzero,
1044 5434607 : TYPE_SIGN (type));
1045 : /* This check for VR_VARYING is here because the old code
1046 : using get_range_info would return VR_RANGE for the entire
1047 : domain, instead of VR_VARYING. The new code normalizes
1048 : full-domain ranges to VR_VARYING. */
1049 5434607 : if (vr_kind == VR_RANGE || vr_kind == VR_VARYING)
1050 5287637 : exp_range->set (type, var_min, var_max);
1051 5434607 : }
1052 : }
1053 :
1054 51327882 : if (!tree_is_chrec (exp)
1055 51327876 : && get_gimple_rhs_class (TREE_CODE (exp)) != GIMPLE_TERNARY_RHS)
1056 : {
1057 51327865 : extract_ops_from_tree (exp, &code, &op0, &op1);
1058 51327865 : if (split_constant_offset_1 (type, op0, code, op1, var, off,
1059 : exp_range, cache, limit))
1060 39762007 : return;
1061 : }
1062 :
1063 11565875 : *var = exp;
1064 11565875 : if (INTEGRAL_TYPE_P (type))
1065 3518334 : *var = fold_convert (sizetype, *var);
1066 11565875 : *off = ssize_int (0);
1067 :
1068 11565875 : int_range_max r;
1069 3205974 : if (exp_range && code != SSA_NAME
1070 115748 : && get_range_query (cfun)->range_of_expr (r, exp)
1071 11623749 : && !r.undefined_p ())
1072 57874 : *exp_range = r;
1073 11565875 : }
1074 :
1075 : /* Expresses EXP as VAR + OFF, where OFF is a constant. VAR has the same
1076 : type as EXP while OFF has type ssizetype. */
1077 :
1078 : void
1079 35306564 : split_constant_offset (tree exp, tree *var, tree *off)
1080 : {
1081 35306564 : unsigned limit = param_ssa_name_def_chain_limit;
1082 35306564 : static hash_map<tree, std::pair<tree, tree> > *cache;
1083 35306564 : if (!cache)
1084 83138 : cache = new hash_map<tree, std::pair<tree, tree> > (37);
1085 35306564 : split_constant_offset (exp, var, off, nullptr, *cache, &limit);
1086 35306564 : *var = fold_convert (TREE_TYPE (exp), *var);
1087 35306564 : cache->empty ();
1088 35306564 : }
1089 :
1090 : /* Returns the address ADDR of an object in a canonical shape (without nop
1091 : casts, and with type of pointer to the object). */
1092 :
1093 : static tree
1094 16612452 : canonicalize_base_object_address (tree addr)
1095 : {
1096 16612452 : tree orig = addr;
1097 :
1098 16612452 : STRIP_NOPS (addr);
1099 :
1100 : /* The base address may be obtained by casting from integer, in that case
1101 : keep the cast. */
1102 16612452 : if (!POINTER_TYPE_P (TREE_TYPE (addr)))
1103 : return orig;
1104 :
1105 16537626 : if (TREE_CODE (addr) != ADDR_EXPR)
1106 : return addr;
1107 :
1108 9864870 : return build_fold_addr_expr (TREE_OPERAND (addr, 0));
1109 : }
1110 :
1111 : /* Analyze the behavior of memory reference REF within STMT.
1112 : There are two modes:
1113 :
1114 : - BB analysis. In this case we simply split the address into base,
1115 : init and offset components, without reference to any containing loop.
1116 : The resulting base and offset are general expressions and they can
1117 : vary arbitrarily from one iteration of the containing loop to the next.
1118 : The step is always zero.
1119 :
1120 : - loop analysis. In this case we analyze the reference both wrt LOOP
1121 : and on the basis that the reference occurs (is "used") in LOOP;
1122 : see the comment above analyze_scalar_evolution_in_loop for more
1123 : information about this distinction. The base, init, offset and
1124 : step fields are all invariant in LOOP.
1125 :
1126 : Perform BB analysis if LOOP is null, or if LOOP is the function's
1127 : dummy outermost loop. In other cases perform loop analysis.
1128 :
1129 : Return true if the analysis succeeded and store the results in DRB if so.
1130 : BB analysis can only fail for bitfield or reversed-storage accesses.
1131 :
1132 : If NOWRAP_BOUND is nonzero, allow simple_iv to record no-wrap bounds.
1133 : Then, store non-trivial bounds in NOWRAP_BOUND. */
1134 :
1135 : opt_result
1136 17188084 : dr_analyze_innermost (innermost_loop_behavior *drb, tree ref,
1137 : class loop *loop, const gimple *stmt,
1138 : tree *nowrap_bound)
1139 : {
1140 17188084 : poly_int64 pbitsize, pbitpos;
1141 17188084 : tree base, poffset;
1142 17188084 : machine_mode pmode;
1143 17188084 : int punsignedp, preversep, pvolatilep;
1144 17188084 : affine_iv base_iv, offset_iv;
1145 17188084 : tree init, dinit, step;
1146 17188084 : bool in_loop = (loop && loop->num);
1147 :
1148 17188084 : if (dump_file && (dump_flags & TDF_DETAILS))
1149 71375 : fprintf (dump_file, "analyze_innermost: ");
1150 :
1151 17188084 : base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset, &pmode,
1152 : &punsignedp, &preversep, &pvolatilep);
1153 17188084 : gcc_assert (base != NULL_TREE);
1154 :
1155 17188084 : poly_int64 pbytepos;
1156 17188084 : if (!multiple_p (pbitpos, BITS_PER_UNIT, &pbytepos))
1157 39773 : return opt_result::failure_at (stmt,
1158 : "failed: bit offset alignment.\n");
1159 :
1160 17148311 : if (preversep)
1161 752 : return opt_result::failure_at (stmt,
1162 : "failed: reverse storage order.\n");
1163 :
1164 : /* Calculate the alignment and misalignment for the inner reference. */
1165 17147559 : unsigned int HOST_WIDE_INT bit_base_misalignment;
1166 17147559 : unsigned int bit_base_alignment;
1167 17147559 : get_object_alignment_1 (base, &bit_base_alignment, &bit_base_misalignment);
1168 :
1169 : /* There are no bitfield references remaining in BASE, so the values
1170 : we got back must be whole bytes. */
1171 17147559 : gcc_assert (bit_base_alignment % BITS_PER_UNIT == 0
1172 : && bit_base_misalignment % BITS_PER_UNIT == 0);
1173 17147559 : unsigned int base_alignment = bit_base_alignment / BITS_PER_UNIT;
1174 17147559 : poly_int64 base_misalignment = bit_base_misalignment / BITS_PER_UNIT;
1175 :
1176 17147559 : if (TREE_CODE (base) == MEM_REF)
1177 : {
1178 7425062 : if (!integer_zerop (TREE_OPERAND (base, 1)))
1179 : {
1180 : /* Subtract MOFF from the base and add it to POFFSET instead.
1181 : Adjust the misalignment to reflect the amount we subtracted. */
1182 1303347 : poly_offset_int moff = mem_ref_offset (base);
1183 1303347 : base_misalignment -= moff.force_shwi ();
1184 1303347 : tree mofft = wide_int_to_tree (sizetype, moff);
1185 1303347 : if (!poffset)
1186 : poffset = mofft;
1187 : else
1188 10374 : poffset = size_binop (PLUS_EXPR, poffset, mofft);
1189 : }
1190 7425062 : base = TREE_OPERAND (base, 0);
1191 : }
1192 : else
1193 : {
1194 9722497 : if (may_be_nonaddressable_p (base))
1195 2110 : return opt_result::failure_at (stmt,
1196 : "failed: base not addressable.\n");
1197 9720387 : base = build_fold_addr_expr (base);
1198 : }
1199 :
1200 17145449 : bool record_nowrap_assumptions_p = (nowrap_bound != nullptr);
1201 :
1202 17145449 : if (in_loop)
1203 : {
1204 3310128 : if (!simple_iv (loop, loop, base, &base_iv, true,
1205 : record_nowrap_assumptions_p))
1206 446996 : return opt_result::failure_at
1207 446996 : (stmt, "failed: evolution of base is not affine.\n");
1208 : }
1209 : else
1210 : {
1211 13835321 : base_iv.base = base;
1212 13835321 : base_iv.step = ssize_int (0);
1213 13835321 : base_iv.no_overflow = true;
1214 13835321 : base_iv.nowrap_bound = NULL_TREE;
1215 : }
1216 :
1217 16698453 : if (!poffset)
1218 : {
1219 13798829 : offset_iv.base = ssize_int (0);
1220 13798829 : offset_iv.step = ssize_int (0);
1221 13798829 : offset_iv.nowrap_bound = NULL_TREE;
1222 : }
1223 : else
1224 : {
1225 2899624 : if (!in_loop)
1226 : {
1227 1536455 : offset_iv.base = poffset;
1228 1536455 : offset_iv.step = ssize_int (0);
1229 1536455 : offset_iv.nowrap_bound = NULL_TREE;
1230 : }
1231 1363169 : else if (!simple_iv (loop, loop, poffset, &offset_iv, true,
1232 : record_nowrap_assumptions_p))
1233 86001 : return opt_result::failure_at
1234 86001 : (stmt, "failed: evolution of offset is not affine.\n");
1235 : }
1236 :
1237 : /* simple_iv might store bounds for BASE_IV and OFFSET_IV. Combine them
1238 : and store the result in NOWRAP_BOUND. */
1239 16612452 : if (nowrap_bound)
1240 : {
1241 493160 : *nowrap_bound = base_iv.nowrap_bound;
1242 493160 : scev_combine_nowrap_bounds (nowrap_bound, offset_iv.nowrap_bound);
1243 : }
1244 :
1245 16612452 : init = ssize_int (pbytepos);
1246 :
1247 : /* Subtract any constant component from the base and add it to INIT instead.
1248 : Adjust the misalignment to reflect the amount we subtracted. */
1249 16612452 : split_constant_offset (base_iv.base, &base_iv.base, &dinit);
1250 16612452 : init = size_binop (PLUS_EXPR, init, dinit);
1251 16612452 : base_misalignment -= TREE_INT_CST_LOW (dinit);
1252 :
1253 16612452 : split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
1254 16612452 : init = size_binop (PLUS_EXPR, init, dinit);
1255 :
1256 16612452 : step = size_binop (PLUS_EXPR,
1257 : fold_convert (ssizetype, base_iv.step),
1258 : fold_convert (ssizetype, offset_iv.step));
1259 :
1260 16612452 : base = canonicalize_base_object_address (base_iv.base);
1261 :
1262 : /* See if get_pointer_alignment can guarantee a higher alignment than
1263 : the one we calculated above. */
1264 16612452 : unsigned int HOST_WIDE_INT alt_misalignment;
1265 16612452 : unsigned int alt_alignment;
1266 16612452 : get_pointer_alignment_1 (base, &alt_alignment, &alt_misalignment);
1267 :
1268 : /* As above, these values must be whole bytes. */
1269 16612452 : gcc_assert (alt_alignment % BITS_PER_UNIT == 0
1270 : && alt_misalignment % BITS_PER_UNIT == 0);
1271 16612452 : alt_alignment /= BITS_PER_UNIT;
1272 16612452 : alt_misalignment /= BITS_PER_UNIT;
1273 :
1274 16612452 : if (base_alignment < alt_alignment)
1275 : {
1276 153329 : base_alignment = alt_alignment;
1277 153329 : base_misalignment = alt_misalignment;
1278 : }
1279 :
1280 16612452 : drb->base_address = base;
1281 16612452 : drb->offset = fold_convert (ssizetype, offset_iv.base);
1282 16612452 : drb->init = init;
1283 16612452 : drb->step = step;
1284 16612452 : if (known_misalignment (base_misalignment, base_alignment,
1285 : &drb->base_misalignment))
1286 16612452 : drb->base_alignment = base_alignment;
1287 : else
1288 : {
1289 : drb->base_alignment = known_alignment (base_misalignment);
1290 : drb->base_misalignment = 0;
1291 : }
1292 16612452 : drb->offset_alignment = highest_pow2_factor (offset_iv.base);
1293 16612452 : drb->step_alignment = highest_pow2_factor (step);
1294 :
1295 16612452 : if (dump_file && (dump_flags & TDF_DETAILS))
1296 67854 : fprintf (dump_file, "success.\n");
1297 :
1298 16612452 : return opt_result::success ();
1299 : }
1300 :
1301 : /* Return true if OP is a valid component reference for a DR access
1302 : function. This accepts a subset of what handled_component_p accepts. */
1303 :
1304 : static bool
1305 6139492 : access_fn_component_p (tree op)
1306 : {
1307 6139492 : switch (TREE_CODE (op))
1308 : {
1309 : case REALPART_EXPR:
1310 : case IMAGPART_EXPR:
1311 : case ARRAY_REF:
1312 : return true;
1313 :
1314 2054116 : case COMPONENT_REF:
1315 2054116 : return (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == RECORD_TYPE
1316 2054116 : || (!AGGREGATE_TYPE_P (TREE_TYPE (op))
1317 1545 : && TREE_CODE (TREE_TYPE (op)) != COMPLEX_TYPE));
1318 :
1319 : default:
1320 : return false;
1321 : }
1322 : }
1323 :
1324 : /* Returns whether BASE can have a access_fn_component_p with BASE
1325 : as base. */
1326 :
1327 : static bool
1328 1722572 : base_supports_access_fn_components_p (tree base)
1329 : {
1330 1722572 : switch (TREE_CODE (TREE_TYPE (base)))
1331 : {
1332 : case COMPLEX_TYPE:
1333 : case ARRAY_TYPE:
1334 : case RECORD_TYPE:
1335 : return true;
1336 1715653 : default:
1337 1715653 : return false;
1338 : }
1339 : }
1340 :
1341 : /* Determines the base object and the list of indices of memory reference
1342 : DR, analyzed in LOOP and instantiated before NEST.
1343 :
1344 : If NOWRAP_BOUND is nonzero, pass it to scev which will then store
1345 : the bound under which we do not wrap. */
1346 :
1347 : static void
1348 17291188 : dr_analyze_indices (struct indices *dri, tree ref, edge nest, loop_p loop,
1349 : tree *nowrap_bound = nullptr)
1350 : {
1351 : /* If analyzing a basic-block there are no indices to analyze
1352 : and thus no access functions. */
1353 17291188 : if (!nest)
1354 : {
1355 13877230 : dri->base_object = ref;
1356 13877230 : dri->access_fns.create (0);
1357 13877230 : return;
1358 : }
1359 :
1360 3413958 : vec<tree> access_fns = vNULL;
1361 :
1362 : /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
1363 : into a two element array with a constant index. The base is
1364 : then just the immediate underlying object. */
1365 3413958 : if (TREE_CODE (ref) == REALPART_EXPR)
1366 : {
1367 42513 : ref = TREE_OPERAND (ref, 0);
1368 42513 : access_fns.safe_push (integer_zero_node);
1369 : }
1370 3371445 : else if (TREE_CODE (ref) == IMAGPART_EXPR)
1371 : {
1372 40704 : ref = TREE_OPERAND (ref, 0);
1373 40704 : access_fns.safe_push (integer_one_node);
1374 : }
1375 :
1376 : /* Analyze access functions of dimensions we know to be independent.
1377 : The list of component references handled here should be kept in
1378 : sync with access_fn_component_p. */
1379 6055358 : while (handled_component_p (ref))
1380 : {
1381 2790127 : if (TREE_CODE (ref) == ARRAY_REF)
1382 : {
1383 1371569 : tree op = TREE_OPERAND (ref, 1);
1384 1371569 : tree access_fn = analyze_scalar_evolution (loop, op, nowrap_bound);
1385 1371569 : access_fn = instantiate_scev (nest, loop, access_fn);
1386 1371569 : access_fns.safe_push (access_fn);
1387 : }
1388 1418558 : else if (TREE_CODE (ref) == COMPONENT_REF
1389 1418558 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE
1390 98769 : || (!AGGREGATE_TYPE_P (TREE_TYPE (ref))
1391 14407 : && TREE_CODE (TREE_TYPE (ref)) != COMPLEX_TYPE)))
1392 : {
1393 : /* For COMPONENT_REFs of records (but not unions!) use the
1394 : FIELD_DECL offset as constant access function so we can
1395 : disambiguate a[i].f1 and a[i].f2. For unions and accesses
1396 : we do not create further access functions for just use
1397 : zero. */
1398 1269831 : tree off;
1399 1269831 : if (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
1400 : {
1401 1255424 : off = component_ref_field_offset (ref);
1402 1255424 : off = size_binop (PLUS_EXPR,
1403 : size_binop (MULT_EXPR,
1404 : fold_convert (bitsizetype, off),
1405 : bitsize_int (BITS_PER_UNIT)),
1406 : DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
1407 : }
1408 : else
1409 14407 : off = bitsize_zero_node;
1410 1269831 : access_fns.safe_push (off);
1411 : }
1412 : else
1413 : /* If we have an unhandled component we could not translate
1414 : to an access function stop analyzing. We have determined
1415 : our base object in this case. */
1416 : break;
1417 :
1418 2641400 : ref = TREE_OPERAND (ref, 0);
1419 : }
1420 :
1421 : /* If the address operand of a MEM_REF base has an evolution in the
1422 : analyzed nest, add it as an additional independent access-function. */
1423 3413958 : if (TREE_CODE (ref) == MEM_REF)
1424 : {
1425 2387067 : tree op = TREE_OPERAND (ref, 0);
1426 2387067 : tree access_fn = analyze_scalar_evolution (loop, op, nowrap_bound);
1427 2387067 : access_fn = instantiate_scev (nest, loop, access_fn);
1428 2387067 : STRIP_NOPS (access_fn);
1429 2387067 : if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
1430 : {
1431 1181172 : tree memoff = TREE_OPERAND (ref, 1);
1432 1181172 : tree base = initial_condition (access_fn);
1433 1181172 : tree orig_type = TREE_TYPE (base);
1434 1181172 : STRIP_USELESS_TYPE_CONVERSION (base);
1435 1181172 : tree off;
1436 1181172 : split_constant_offset (base, &base, &off);
1437 1181172 : STRIP_USELESS_TYPE_CONVERSION (base);
1438 : /* Fold the MEM_REF offset into the evolutions initial
1439 : value to make more bases comparable. */
1440 1181172 : if (!integer_zerop (memoff))
1441 : {
1442 128643 : off = size_binop (PLUS_EXPR, off,
1443 : fold_convert (ssizetype, memoff));
1444 128643 : memoff = build_int_cst (TREE_TYPE (memoff), 0);
1445 : }
1446 : /* Adjust the offset so it is a multiple of the access type
1447 : size and thus we separate bases that can possibly be used
1448 : to produce partial overlaps (which the access_fn machinery
1449 : cannot handle). */
1450 1181172 : wide_int rem;
1451 1181172 : if (TYPE_SIZE_UNIT (TREE_TYPE (ref))
1452 1181042 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref))) == INTEGER_CST
1453 2361897 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (ref))))
1454 1180725 : rem = wi::mod_trunc
1455 1180725 : (wi::to_wide (off),
1456 2361450 : wi::to_wide (TYPE_SIZE_UNIT (TREE_TYPE (ref))),
1457 1180725 : SIGNED);
1458 : else
1459 : /* If we can't compute the remainder simply force the initial
1460 : condition to zero. */
1461 447 : rem = wi::to_wide (off);
1462 1181172 : off = wide_int_to_tree (ssizetype, wi::to_wide (off) - rem);
1463 1181172 : memoff = wide_int_to_tree (TREE_TYPE (memoff), rem);
1464 : /* And finally replace the initial condition. */
1465 2362344 : access_fn = chrec_replace_initial_condition
1466 1181172 : (access_fn, fold_convert (orig_type, off));
1467 : /* ??? This is still not a suitable base object for
1468 : dr_may_alias_p - the base object needs to be an
1469 : access that covers the object as whole. With
1470 : an evolution in the pointer this cannot be
1471 : guaranteed.
1472 : As a band-aid, mark the access so we can special-case
1473 : it in dr_may_alias_p. */
1474 1181172 : tree old = ref;
1475 1181172 : ref = fold_build2_loc (EXPR_LOCATION (ref),
1476 1181172 : MEM_REF, TREE_TYPE (ref),
1477 : base, memoff);
1478 1181172 : MR_DEPENDENCE_CLIQUE (ref) = MR_DEPENDENCE_CLIQUE (old);
1479 1181172 : MR_DEPENDENCE_BASE (ref) = MR_DEPENDENCE_BASE (old);
1480 1181172 : dri->unconstrained_base = true;
1481 1181172 : access_fns.safe_push (access_fn);
1482 1181172 : }
1483 : }
1484 1026891 : else if (DECL_P (ref))
1485 : {
1486 : /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
1487 878164 : ref = build2 (MEM_REF, TREE_TYPE (ref),
1488 : build_fold_addr_expr (ref),
1489 : build_int_cst (reference_alias_ptr_type (ref), 0));
1490 : }
1491 :
1492 3413958 : dri->base_object = ref;
1493 3413958 : dri->access_fns = access_fns;
1494 : }
1495 :
1496 : /* Extracts the alias analysis information from the memory reference DR. */
1497 :
1498 : static void
1499 17175464 : dr_analyze_alias (struct data_reference *dr)
1500 : {
1501 17175464 : tree ref = DR_REF (dr);
1502 17175464 : tree base = get_base_address (ref), addr;
1503 :
1504 17175464 : if (INDIRECT_REF_P (base)
1505 17175464 : || TREE_CODE (base) == MEM_REF)
1506 : {
1507 7420059 : addr = TREE_OPERAND (base, 0);
1508 7420059 : if (TREE_CODE (addr) == SSA_NAME)
1509 14837364 : DR_PTR_INFO (dr) = ssa_name_pt_solution (addr);
1510 : }
1511 17175464 : }
1512 :
1513 : /* Frees data reference DR. */
1514 :
1515 : void
1516 17670087 : free_data_ref (data_reference_p dr)
1517 : {
1518 17670087 : DR_ACCESS_FNS (dr).release ();
1519 17670087 : if (dr->alt_indices.base_object)
1520 115724 : dr->alt_indices.access_fns.release ();
1521 17670087 : free (dr);
1522 17670087 : }
1523 :
1524 : /* Analyze memory reference MEMREF, which is accessed in STMT.
1525 : The reference is a read if IS_READ is true, otherwise it is a write.
1526 : IS_CONDITIONAL_IN_STMT indicates that the reference is conditional
1527 : within STMT, i.e. that it might not occur even if STMT is executed
1528 : and runs to completion.
1529 :
1530 : Return the data_reference description of MEMREF. NEST is the outermost
1531 : loop in which the reference should be instantiated, LOOP is the loop
1532 : in which the data reference should be analyzed.
1533 :
1534 : If RECORD_NOWRAP_ASSUMPTIONS is true, pass an empty no-wrap bound to
1535 : scev and have it store its assumptions there. If we got a nontrivial
1536 : assumption, save it in DR's DR_NOWRAP_BOUND. */
1537 :
1538 : struct data_reference *
1539 17175464 : create_data_ref (edge nest, loop_p loop, tree memref, gimple *stmt,
1540 : bool is_read, bool is_conditional_in_stmt,
1541 : bool record_nowrap_assumptions)
1542 : {
1543 17175464 : struct data_reference *dr;
1544 :
1545 17175464 : if (dump_file && (dump_flags & TDF_DETAILS))
1546 : {
1547 69994 : fprintf (dump_file, "Creating dr for ");
1548 69994 : print_generic_expr (dump_file, memref, TDF_SLIM);
1549 69994 : fprintf (dump_file, "\n");
1550 : }
1551 :
1552 17175464 : dr = XCNEW (struct data_reference);
1553 17175464 : DR_STMT (dr) = stmt;
1554 17175464 : DR_REF (dr) = memref;
1555 17175464 : DR_IS_READ (dr) = is_read;
1556 17175464 : DR_IS_CONDITIONAL_IN_STMT (dr) = is_conditional_in_stmt;
1557 :
1558 17175464 : tree nowrap_bound = NULL_TREE;
1559 47702273 : dr_analyze_innermost (&DR_INNERMOST (dr), memref,
1560 : nest != NULL ? loop : NULL, stmt,
1561 : record_nowrap_assumptions ? &nowrap_bound : nullptr);
1562 17175464 : dr_analyze_indices (&dr->indices, DR_REF (dr), nest, loop,
1563 : record_nowrap_assumptions ? &nowrap_bound : nullptr);
1564 17175464 : dr_analyze_alias (dr);
1565 :
1566 17175464 : if (nowrap_bound)
1567 : {
1568 : /* If there is a bound but it is zero, the assumption we build with it
1569 : would never hold and we would always fall back to the scalar loop.
1570 : Reset the DR so we can still get gather/scatter. */
1571 1410 : if (integer_zerop (nowrap_bound))
1572 103 : dr_reset (dr);
1573 : else
1574 1307 : DR_NOWRAP_BOUND (dr) = nowrap_bound;
1575 : }
1576 :
1577 17175464 : if (dump_file && (dump_flags & TDF_DETAILS))
1578 : {
1579 69994 : unsigned i;
1580 69994 : fprintf (dump_file, "\tbase_address: ");
1581 69994 : print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1582 69994 : fprintf (dump_file, "\n\toffset from base address: ");
1583 69994 : print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1584 69994 : fprintf (dump_file, "\n\tconstant offset from base address: ");
1585 69994 : print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1586 69994 : fprintf (dump_file, "\n\tstep: ");
1587 69994 : print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1588 69994 : fprintf (dump_file, "\n\tbase alignment: %d", DR_BASE_ALIGNMENT (dr));
1589 69994 : fprintf (dump_file, "\n\tbase misalignment: %d",
1590 : DR_BASE_MISALIGNMENT (dr));
1591 69994 : fprintf (dump_file, "\n\toffset alignment: %d",
1592 : DR_OFFSET_ALIGNMENT (dr));
1593 69994 : fprintf (dump_file, "\n\tstep alignment: %d", DR_STEP_ALIGNMENT (dr));
1594 69994 : fprintf (dump_file, "\n\tbase_object: ");
1595 69994 : print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1596 69994 : if (DR_NOWRAP_BOUND (dr))
1597 : {
1598 98 : fprintf (dump_file, "\n\tnowrap bound: ");
1599 98 : print_generic_expr (dump_file, DR_NOWRAP_BOUND (dr), TDF_SLIM);
1600 : }
1601 69994 : fprintf (dump_file, "\n");
1602 200585 : for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
1603 : {
1604 60597 : fprintf (dump_file, "\tAccess function %d: ", i);
1605 60597 : print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
1606 : }
1607 : }
1608 :
1609 17175464 : return dr;
1610 : }
1611 :
1612 : /* A helper function computes order between two tree expressions T1 and T2.
1613 : This is used in comparator functions sorting objects based on the order
1614 : of tree expressions. The function returns -1, 0, or 1. */
1615 :
1616 : int
1617 453179920 : data_ref_compare_tree (tree t1, tree t2)
1618 : {
1619 453179920 : int i, cmp;
1620 453179920 : enum tree_code code;
1621 453179920 : char tclass;
1622 :
1623 453179920 : if (t1 == t2)
1624 : return 0;
1625 205959512 : if (t1 == NULL)
1626 : return -1;
1627 205827088 : if (t2 == NULL)
1628 : return 1;
1629 :
1630 205744943 : STRIP_USELESS_TYPE_CONVERSION (t1);
1631 205744943 : STRIP_USELESS_TYPE_CONVERSION (t2);
1632 205744943 : if (t1 == t2)
1633 : return 0;
1634 :
1635 205146708 : if (TREE_CODE (t1) != TREE_CODE (t2)
1636 14268322 : && ! (CONVERT_EXPR_P (t1) && CONVERT_EXPR_P (t2)))
1637 14268322 : return TREE_CODE (t1) < TREE_CODE (t2) ? -1 : 1;
1638 :
1639 190878386 : code = TREE_CODE (t1);
1640 190878386 : switch (code)
1641 : {
1642 56235098 : case INTEGER_CST:
1643 56235098 : return tree_int_cst_compare (t1, t2);
1644 :
1645 16 : case STRING_CST:
1646 16 : if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2))
1647 16 : return TREE_STRING_LENGTH (t1) < TREE_STRING_LENGTH (t2) ? -1 : 1;
1648 0 : return memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1649 0 : TREE_STRING_LENGTH (t1));
1650 :
1651 18456230 : case SSA_NAME:
1652 18456230 : if (SSA_NAME_VERSION (t1) != SSA_NAME_VERSION (t2))
1653 18456230 : return SSA_NAME_VERSION (t1) < SSA_NAME_VERSION (t2) ? -1 : 1;
1654 : break;
1655 :
1656 116187042 : default:
1657 116187042 : if (POLY_INT_CST_P (t1))
1658 : return compare_sizes_for_sort (wi::to_poly_widest (t1),
1659 : wi::to_poly_widest (t2));
1660 :
1661 116187042 : tclass = TREE_CODE_CLASS (code);
1662 :
1663 : /* For decls, compare their UIDs. */
1664 116187042 : if (tclass == tcc_declaration)
1665 : {
1666 21928868 : if (DECL_UID (t1) != DECL_UID (t2))
1667 21928381 : return DECL_UID (t1) < DECL_UID (t2) ? -1 : 1;
1668 : break;
1669 : }
1670 : /* For expressions, compare their operands recursively. */
1671 94258174 : else if (IS_EXPR_CODE_CLASS (tclass))
1672 : {
1673 168465877 : for (i = TREE_OPERAND_LENGTH (t1) - 1; i >= 0; --i)
1674 : {
1675 109077916 : cmp = data_ref_compare_tree (TREE_OPERAND (t1, i),
1676 109077916 : TREE_OPERAND (t2, i));
1677 109077916 : if (cmp != 0)
1678 : return cmp;
1679 : }
1680 : }
1681 : else
1682 0 : gcc_unreachable ();
1683 : }
1684 :
1685 : return 0;
1686 : }
1687 :
1688 : /* Return TRUE it's possible to resolve data dependence DDR by runtime alias
1689 : check. */
1690 :
1691 : opt_result
1692 238959 : runtime_alias_check_p (ddr_p ddr, class loop *loop, bool speed_p)
1693 : {
1694 238959 : if (dump_enabled_p ())
1695 8406 : dump_printf (MSG_NOTE,
1696 : "consider run-time aliasing test between %T and %T\n",
1697 8406 : DR_REF (DDR_A (ddr)), DR_REF (DDR_B (ddr)));
1698 :
1699 238959 : if (!speed_p)
1700 0 : return opt_result::failure_at (DR_STMT (DDR_A (ddr)),
1701 : "runtime alias check not supported when"
1702 : " optimizing for size.\n");
1703 :
1704 : /* FORNOW: We don't support versioning with outer-loop in either
1705 : vectorization or loop distribution. */
1706 238959 : if (loop != NULL && loop->inner != NULL)
1707 159 : return opt_result::failure_at (DR_STMT (DDR_A (ddr)),
1708 : "runtime alias check not supported for"
1709 : " outer loop.\n");
1710 :
1711 : /* FORNOW: We don't support handling different address spaces. */
1712 238800 : if (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (DR_BASE_ADDRESS (DDR_A (ddr)))))
1713 238800 : != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (DR_BASE_ADDRESS (DDR_B (ddr))))))
1714 1 : return opt_result::failure_at (DR_STMT (DDR_A (ddr)),
1715 : "runtime alias check between different "
1716 : "address spaces not supported.\n");
1717 :
1718 238799 : return opt_result::success ();
1719 : }
1720 :
1721 : /* Operator == between two dr_with_seg_len objects.
1722 :
1723 : This equality operator is used to make sure two data refs
1724 : are the same one so that we will consider to combine the
1725 : aliasing checks of those two pairs of data dependent data
1726 : refs. */
1727 :
1728 : static bool
1729 152425 : operator == (const dr_with_seg_len& d1,
1730 : const dr_with_seg_len& d2)
1731 : {
1732 152425 : return (operand_equal_p (DR_BASE_ADDRESS (d1.dr),
1733 152425 : DR_BASE_ADDRESS (d2.dr), 0)
1734 117075 : && data_ref_compare_tree (DR_OFFSET (d1.dr), DR_OFFSET (d2.dr)) == 0
1735 116311 : && data_ref_compare_tree (DR_INIT (d1.dr), DR_INIT (d2.dr)) == 0
1736 105260 : && data_ref_compare_tree (d1.seg_len, d2.seg_len) == 0
1737 104312 : && known_eq (d1.access_size, d2.access_size)
1738 253273 : && d1.align == d2.align);
1739 : }
1740 :
1741 : /* Comparison function for sorting objects of dr_with_seg_len_pair_t
1742 : so that we can combine aliasing checks in one scan. */
1743 :
1744 : static int
1745 1221118 : comp_dr_with_seg_len_pair (const void *pa_, const void *pb_)
1746 : {
1747 1221118 : const dr_with_seg_len_pair_t* pa = (const dr_with_seg_len_pair_t *) pa_;
1748 1221118 : const dr_with_seg_len_pair_t* pb = (const dr_with_seg_len_pair_t *) pb_;
1749 1221118 : const dr_with_seg_len &a1 = pa->first, &a2 = pa->second;
1750 1221118 : const dr_with_seg_len &b1 = pb->first, &b2 = pb->second;
1751 :
1752 : /* For DR pairs (a, b) and (c, d), we only consider to merge the alias checks
1753 : if a and c have the same basic address snd step, and b and d have the same
1754 : address and step. Therefore, if any a&c or b&d don't have the same address
1755 : and step, we don't care the order of those two pairs after sorting. */
1756 1221118 : int comp_res;
1757 :
1758 1221118 : if ((comp_res = data_ref_compare_tree (DR_BASE_ADDRESS (a1.dr),
1759 1221118 : DR_BASE_ADDRESS (b1.dr))) != 0)
1760 : return comp_res;
1761 645689 : if ((comp_res = data_ref_compare_tree (DR_BASE_ADDRESS (a2.dr),
1762 645689 : DR_BASE_ADDRESS (b2.dr))) != 0)
1763 : return comp_res;
1764 440235 : if ((comp_res = data_ref_compare_tree (DR_STEP (a1.dr),
1765 440235 : DR_STEP (b1.dr))) != 0)
1766 : return comp_res;
1767 439599 : if ((comp_res = data_ref_compare_tree (DR_STEP (a2.dr),
1768 439599 : DR_STEP (b2.dr))) != 0)
1769 : return comp_res;
1770 432004 : if ((comp_res = data_ref_compare_tree (DR_OFFSET (a1.dr),
1771 432004 : DR_OFFSET (b1.dr))) != 0)
1772 : return comp_res;
1773 416637 : if ((comp_res = data_ref_compare_tree (DR_INIT (a1.dr),
1774 416637 : DR_INIT (b1.dr))) != 0)
1775 : return comp_res;
1776 312395 : if ((comp_res = data_ref_compare_tree (DR_OFFSET (a2.dr),
1777 312395 : DR_OFFSET (b2.dr))) != 0)
1778 : return comp_res;
1779 297324 : if ((comp_res = data_ref_compare_tree (DR_INIT (a2.dr),
1780 297324 : DR_INIT (b2.dr))) != 0)
1781 : return comp_res;
1782 :
1783 : return 0;
1784 : }
1785 :
1786 : /* Dump information about ALIAS_PAIR, indenting each line by INDENT. */
1787 :
1788 : static void
1789 1198 : dump_alias_pair (dr_with_seg_len_pair_t *alias_pair, const char *indent)
1790 : {
1791 2396 : dump_printf (MSG_NOTE, "%sreference: %T vs. %T\n", indent,
1792 1198 : DR_REF (alias_pair->first.dr),
1793 1198 : DR_REF (alias_pair->second.dr));
1794 :
1795 1198 : dump_printf (MSG_NOTE, "%ssegment length: %T", indent,
1796 : alias_pair->first.seg_len);
1797 1198 : if (!operand_equal_p (alias_pair->first.seg_len,
1798 1198 : alias_pair->second.seg_len, 0))
1799 300 : dump_printf (MSG_NOTE, " vs. %T", alias_pair->second.seg_len);
1800 :
1801 1198 : dump_printf (MSG_NOTE, "\n%saccess size: ", indent);
1802 1198 : dump_dec (MSG_NOTE, alias_pair->first.access_size);
1803 1198 : if (maybe_ne (alias_pair->first.access_size, alias_pair->second.access_size))
1804 : {
1805 272 : dump_printf (MSG_NOTE, " vs. ");
1806 272 : dump_dec (MSG_NOTE, alias_pair->second.access_size);
1807 : }
1808 :
1809 1198 : dump_printf (MSG_NOTE, "\n%salignment: %d", indent,
1810 : alias_pair->first.align);
1811 1198 : if (alias_pair->first.align != alias_pair->second.align)
1812 109 : dump_printf (MSG_NOTE, " vs. %d", alias_pair->second.align);
1813 :
1814 1198 : dump_printf (MSG_NOTE, "\n%sflags: ", indent);
1815 1198 : if (alias_pair->flags & DR_ALIAS_RAW)
1816 174 : dump_printf (MSG_NOTE, " RAW");
1817 1198 : if (alias_pair->flags & DR_ALIAS_WAR)
1818 960 : dump_printf (MSG_NOTE, " WAR");
1819 1198 : if (alias_pair->flags & DR_ALIAS_WAW)
1820 196 : dump_printf (MSG_NOTE, " WAW");
1821 1198 : if (alias_pair->flags & DR_ALIAS_ARBITRARY)
1822 263 : dump_printf (MSG_NOTE, " ARBITRARY");
1823 1198 : if (alias_pair->flags & DR_ALIAS_SWAPPED)
1824 0 : dump_printf (MSG_NOTE, " SWAPPED");
1825 1198 : if (alias_pair->flags & DR_ALIAS_UNSWAPPED)
1826 0 : dump_printf (MSG_NOTE, " UNSWAPPED");
1827 1198 : if (alias_pair->flags & DR_ALIAS_MIXED_STEPS)
1828 0 : dump_printf (MSG_NOTE, " MIXED_STEPS");
1829 1198 : if (alias_pair->flags == 0)
1830 0 : dump_printf (MSG_NOTE, " <none>");
1831 1198 : dump_printf (MSG_NOTE, "\n");
1832 1198 : }
1833 :
1834 : /* Merge alias checks recorded in ALIAS_PAIRS and remove redundant ones.
1835 : FACTOR is number of iterations that each data reference is accessed.
1836 :
1837 : Basically, for each pair of dependent data refs store_ptr_0 & load_ptr_0,
1838 : we create an expression:
1839 :
1840 : ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
1841 : || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
1842 :
1843 : for aliasing checks. However, in some cases we can decrease the number
1844 : of checks by combining two checks into one. For example, suppose we have
1845 : another pair of data refs store_ptr_0 & load_ptr_1, and if the following
1846 : condition is satisfied:
1847 :
1848 : load_ptr_0 < load_ptr_1 &&
1849 : load_ptr_1 - load_ptr_0 - load_segment_length_0 < store_segment_length_0
1850 :
1851 : (this condition means, in each iteration of vectorized loop, the accessed
1852 : memory of store_ptr_0 cannot be between the memory of load_ptr_0 and
1853 : load_ptr_1.)
1854 :
1855 : we then can use only the following expression to finish the aliasing checks
1856 : between store_ptr_0 & load_ptr_0 and store_ptr_0 & load_ptr_1:
1857 :
1858 : ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
1859 : || (load_ptr_1 + load_segment_length_1 <= store_ptr_0))
1860 :
1861 : Note that we only consider that load_ptr_0 and load_ptr_1 have the same
1862 : basic address. */
1863 :
1864 : void
1865 23688 : prune_runtime_alias_test_list (vec<dr_with_seg_len_pair_t> *alias_pairs,
1866 : poly_uint64)
1867 : {
1868 23688 : if (alias_pairs->is_empty ())
1869 23688 : return;
1870 :
1871 : /* Canonicalize each pair so that the base components are ordered wrt
1872 : data_ref_compare_tree. This allows the loop below to merge more
1873 : cases. */
1874 : unsigned int i;
1875 : dr_with_seg_len_pair_t *alias_pair;
1876 96500 : FOR_EACH_VEC_ELT (*alias_pairs, i, alias_pair)
1877 : {
1878 73673 : data_reference_p dr_a = alias_pair->first.dr;
1879 73673 : data_reference_p dr_b = alias_pair->second.dr;
1880 73673 : int comp_res = data_ref_compare_tree (DR_BASE_ADDRESS (dr_a),
1881 : DR_BASE_ADDRESS (dr_b));
1882 73673 : if (comp_res == 0)
1883 1768 : comp_res = data_ref_compare_tree (DR_OFFSET (dr_a), DR_OFFSET (dr_b));
1884 1768 : if (comp_res == 0)
1885 136 : comp_res = data_ref_compare_tree (DR_INIT (dr_a), DR_INIT (dr_b));
1886 73673 : if (comp_res > 0)
1887 : {
1888 26570 : std::swap (alias_pair->first, alias_pair->second);
1889 26570 : alias_pair->flags |= DR_ALIAS_SWAPPED;
1890 : }
1891 : else
1892 47103 : alias_pair->flags |= DR_ALIAS_UNSWAPPED;
1893 : }
1894 :
1895 : /* Sort the collected data ref pairs so that we can scan them once to
1896 : combine all possible aliasing checks. */
1897 22827 : alias_pairs->qsort (comp_dr_with_seg_len_pair);
1898 :
1899 : /* Scan the sorted dr pairs and check if we can combine alias checks
1900 : of two neighboring dr pairs. */
1901 22827 : unsigned int last = 0;
1902 73673 : for (i = 1; i < alias_pairs->length (); ++i)
1903 : {
1904 : /* Deal with two ddrs (dr_a1, dr_b1) and (dr_a2, dr_b2). */
1905 50846 : dr_with_seg_len_pair_t *alias_pair1 = &(*alias_pairs)[last];
1906 50846 : dr_with_seg_len_pair_t *alias_pair2 = &(*alias_pairs)[i];
1907 :
1908 50846 : dr_with_seg_len *dr_a1 = &alias_pair1->first;
1909 50846 : dr_with_seg_len *dr_b1 = &alias_pair1->second;
1910 50846 : dr_with_seg_len *dr_a2 = &alias_pair2->first;
1911 50846 : dr_with_seg_len *dr_b2 = &alias_pair2->second;
1912 :
1913 : /* Remove duplicate data ref pairs. */
1914 50846 : if (*dr_a1 == *dr_a2 && *dr_b1 == *dr_b2)
1915 : {
1916 23423 : if (dump_enabled_p ())
1917 2173 : dump_printf (MSG_NOTE, "found equal ranges %T, %T and %T, %T\n",
1918 2173 : DR_REF (dr_a1->dr), DR_REF (dr_b1->dr),
1919 2173 : DR_REF (dr_a2->dr), DR_REF (dr_b2->dr));
1920 23423 : alias_pair1->flags |= alias_pair2->flags;
1921 23423 : continue;
1922 : }
1923 :
1924 : /* Assume that we won't be able to merge the pairs, then correct
1925 : if we do. */
1926 27423 : last += 1;
1927 27423 : if (last != i)
1928 8058 : (*alias_pairs)[last] = (*alias_pairs)[i];
1929 :
1930 27423 : if (*dr_a1 == *dr_a2 || *dr_b1 == *dr_b2)
1931 : {
1932 : /* We consider the case that DR_B1 and DR_B2 are same memrefs,
1933 : and DR_A1 and DR_A2 are two consecutive memrefs. */
1934 23310 : if (*dr_a1 == *dr_a2)
1935 : {
1936 15346 : std::swap (dr_a1, dr_b1);
1937 15346 : std::swap (dr_a2, dr_b2);
1938 : }
1939 :
1940 23310 : poly_int64 init_a1, init_a2;
1941 : /* Only consider cases in which the distance between the initial
1942 : DR_A1 and the initial DR_A2 is known at compile time. */
1943 41529 : if (!operand_equal_p (DR_BASE_ADDRESS (dr_a1->dr),
1944 23310 : DR_BASE_ADDRESS (dr_a2->dr), 0)
1945 5532 : || !operand_equal_p (DR_OFFSET (dr_a1->dr),
1946 5532 : DR_OFFSET (dr_a2->dr), 0)
1947 5091 : || !poly_int_tree_p (DR_INIT (dr_a1->dr), &init_a1)
1948 28401 : || !poly_int_tree_p (DR_INIT (dr_a2->dr), &init_a2))
1949 18246 : continue;
1950 :
1951 : /* Don't combine if we can't tell which one comes first. */
1952 5091 : if (!ordered_p (init_a1, init_a2))
1953 : continue;
1954 :
1955 : /* Work out what the segment length would be if we did combine
1956 : DR_A1 and DR_A2:
1957 :
1958 : - If DR_A1 and DR_A2 have equal lengths, that length is
1959 : also the combined length.
1960 :
1961 : - If DR_A1 and DR_A2 both have negative "lengths", the combined
1962 : length is the lower bound on those lengths.
1963 :
1964 : - If DR_A1 and DR_A2 both have positive lengths, the combined
1965 : length is the upper bound on those lengths.
1966 :
1967 : Other cases are unlikely to give a useful combination.
1968 :
1969 : The lengths both have sizetype, so the sign is taken from
1970 : the step instead. */
1971 5091 : poly_uint64 new_seg_len = 0;
1972 5091 : bool new_seg_len_p = !operand_equal_p (dr_a1->seg_len,
1973 5091 : dr_a2->seg_len, 0);
1974 5091 : if (new_seg_len_p)
1975 : {
1976 27 : poly_uint64 seg_len_a1, seg_len_a2;
1977 27 : if (!poly_int_tree_p (dr_a1->seg_len, &seg_len_a1)
1978 27 : || !poly_int_tree_p (dr_a2->seg_len, &seg_len_a2))
1979 27 : continue;
1980 :
1981 0 : tree indicator_a = dr_direction_indicator (dr_a1->dr);
1982 0 : if (TREE_CODE (indicator_a) != INTEGER_CST)
1983 0 : continue;
1984 :
1985 0 : tree indicator_b = dr_direction_indicator (dr_a2->dr);
1986 0 : if (TREE_CODE (indicator_b) != INTEGER_CST)
1987 0 : continue;
1988 :
1989 0 : int sign_a = tree_int_cst_sgn (indicator_a);
1990 0 : int sign_b = tree_int_cst_sgn (indicator_b);
1991 :
1992 0 : if (sign_a <= 0 && sign_b <= 0)
1993 0 : new_seg_len = lower_bound (seg_len_a1, seg_len_a2);
1994 0 : else if (sign_a >= 0 && sign_b >= 0)
1995 0 : new_seg_len = upper_bound (seg_len_a1, seg_len_a2);
1996 : else
1997 0 : continue;
1998 : }
1999 : /* At this point we're committed to merging the refs. */
2000 :
2001 : /* Make sure dr_a1 starts left of dr_a2. */
2002 5064 : if (maybe_gt (init_a1, init_a2))
2003 : {
2004 0 : std::swap (*dr_a1, *dr_a2);
2005 0 : std::swap (init_a1, init_a2);
2006 : }
2007 :
2008 : /* The DR_Bs are equal, so only the DR_As can introduce
2009 : mixed steps. */
2010 5064 : if (!operand_equal_p (DR_STEP (dr_a1->dr), DR_STEP (dr_a2->dr), 0))
2011 0 : alias_pair1->flags |= DR_ALIAS_MIXED_STEPS;
2012 :
2013 5064 : if (new_seg_len_p)
2014 : {
2015 0 : dr_a1->seg_len = build_int_cst (TREE_TYPE (dr_a1->seg_len),
2016 0 : new_seg_len);
2017 0 : dr_a1->align = MIN (dr_a1->align, known_alignment (new_seg_len));
2018 : }
2019 :
2020 : /* This is always positive due to the swap above. */
2021 5064 : poly_uint64 diff = init_a2 - init_a1;
2022 :
2023 : /* The new check will start at DR_A1. Make sure that its access
2024 : size encompasses the initial DR_A2. */
2025 5064 : if (maybe_lt (dr_a1->access_size, diff + dr_a2->access_size))
2026 : {
2027 1893 : dr_a1->access_size = upper_bound (dr_a1->access_size,
2028 1893 : diff + dr_a2->access_size);
2029 1893 : unsigned int new_align = known_alignment (dr_a1->access_size);
2030 1893 : dr_a1->align = MIN (dr_a1->align, new_align);
2031 : }
2032 5064 : if (dump_enabled_p ())
2033 1027 : dump_printf (MSG_NOTE, "merging ranges for %T, %T and %T, %T\n",
2034 1027 : DR_REF (dr_a1->dr), DR_REF (dr_b1->dr),
2035 1027 : DR_REF (dr_a2->dr), DR_REF (dr_b2->dr));
2036 5064 : alias_pair1->flags |= alias_pair2->flags;
2037 5064 : last -= 1;
2038 : }
2039 : }
2040 22827 : alias_pairs->truncate (last + 1);
2041 :
2042 : /* Try to restore the original dr_with_seg_len order within each
2043 : dr_with_seg_len_pair_t. If we ended up combining swapped and
2044 : unswapped pairs into the same check, we have to invalidate any
2045 : RAW, WAR and WAW information for it. */
2046 22827 : if (dump_enabled_p ())
2047 888 : dump_printf (MSG_NOTE, "merged alias checks:\n");
2048 68013 : FOR_EACH_VEC_ELT (*alias_pairs, i, alias_pair)
2049 : {
2050 45186 : unsigned int swap_mask = (DR_ALIAS_SWAPPED | DR_ALIAS_UNSWAPPED);
2051 45186 : unsigned int swapped = (alias_pair->flags & swap_mask);
2052 45186 : if (swapped == DR_ALIAS_SWAPPED)
2053 13862 : std::swap (alias_pair->first, alias_pair->second);
2054 31324 : else if (swapped != DR_ALIAS_UNSWAPPED)
2055 3627 : alias_pair->flags |= DR_ALIAS_ARBITRARY;
2056 45186 : alias_pair->flags &= ~swap_mask;
2057 45186 : if (dump_enabled_p ())
2058 1198 : dump_alias_pair (alias_pair, " ");
2059 : }
2060 : }
2061 :
2062 : /* A subroutine of create_intersect_range_checks, with a subset of the
2063 : same arguments. Try to use IFN_CHECK_RAW_PTRS and IFN_CHECK_WAR_PTRS
2064 : to optimize cases in which the references form a simple RAW, WAR or
2065 : WAR dependence. */
2066 :
2067 : static bool
2068 4895 : create_ifn_alias_checks (tree *cond_expr,
2069 : const dr_with_seg_len_pair_t &alias_pair)
2070 : {
2071 4895 : const dr_with_seg_len& dr_a = alias_pair.first;
2072 4895 : const dr_with_seg_len& dr_b = alias_pair.second;
2073 :
2074 : /* Check for cases in which:
2075 :
2076 : (a) we have a known RAW, WAR or WAR dependence
2077 : (b) the accesses are well-ordered in both the original and new code
2078 : (see the comment above the DR_ALIAS_* flags for details); and
2079 : (c) the DR_STEPs describe all access pairs covered by ALIAS_PAIR. */
2080 4895 : if (alias_pair.flags & ~(DR_ALIAS_RAW | DR_ALIAS_WAR | DR_ALIAS_WAW))
2081 : return false;
2082 :
2083 : /* Make sure that both DRs access the same pattern of bytes,
2084 : with a constant length and step. */
2085 3174 : poly_uint64 seg_len;
2086 3174 : if (!operand_equal_p (dr_a.seg_len, dr_b.seg_len, 0)
2087 2764 : || !poly_int_tree_p (dr_a.seg_len, &seg_len)
2088 2757 : || maybe_ne (dr_a.access_size, dr_b.access_size)
2089 2716 : || !operand_equal_p (DR_STEP (dr_a.dr), DR_STEP (dr_b.dr), 0)
2090 5890 : || !tree_fits_uhwi_p (DR_STEP (dr_a.dr)))
2091 : return false;
2092 :
2093 2701 : unsigned HOST_WIDE_INT bytes = tree_to_uhwi (DR_STEP (dr_a.dr));
2094 2701 : tree addr_a = DR_BASE_ADDRESS (dr_a.dr);
2095 2701 : tree addr_b = DR_BASE_ADDRESS (dr_b.dr);
2096 :
2097 : /* See whether the target supports what we want to do. WAW checks are
2098 : equivalent to WAR checks here. */
2099 2661 : internal_fn ifn = (alias_pair.flags & DR_ALIAS_RAW
2100 2701 : ? IFN_CHECK_RAW_PTRS
2101 : : IFN_CHECK_WAR_PTRS);
2102 2701 : unsigned int align = MIN (dr_a.align, dr_b.align);
2103 2701 : poly_uint64 full_length = seg_len + bytes;
2104 2701 : if (!internal_check_ptrs_fn_supported_p (ifn, TREE_TYPE (addr_a),
2105 : full_length, align))
2106 : {
2107 2701 : full_length = seg_len + dr_a.access_size;
2108 2701 : if (!internal_check_ptrs_fn_supported_p (ifn, TREE_TYPE (addr_a),
2109 : full_length, align))
2110 : return false;
2111 : }
2112 :
2113 : /* Commit to using this form of test. */
2114 0 : addr_a = fold_build_pointer_plus (addr_a, DR_OFFSET (dr_a.dr));
2115 0 : addr_a = fold_build_pointer_plus (addr_a, DR_INIT (dr_a.dr));
2116 :
2117 0 : addr_b = fold_build_pointer_plus (addr_b, DR_OFFSET (dr_b.dr));
2118 0 : addr_b = fold_build_pointer_plus (addr_b, DR_INIT (dr_b.dr));
2119 :
2120 0 : *cond_expr = build_call_expr_internal_loc (UNKNOWN_LOCATION,
2121 : ifn, boolean_type_node,
2122 : 4, addr_a, addr_b,
2123 0 : size_int (full_length),
2124 0 : size_int (align));
2125 :
2126 0 : if (dump_enabled_p ())
2127 : {
2128 0 : if (ifn == IFN_CHECK_RAW_PTRS)
2129 0 : dump_printf (MSG_NOTE, "using an IFN_CHECK_RAW_PTRS test\n");
2130 : else
2131 0 : dump_printf (MSG_NOTE, "using an IFN_CHECK_WAR_PTRS test\n");
2132 : }
2133 : return true;
2134 : }
2135 :
2136 : /* Try to generate a runtime condition that is true if ALIAS_PAIR is
2137 : free of aliases, using a condition based on index values instead
2138 : of a condition based on addresses. Return true on success,
2139 : storing the condition in *COND_EXPR.
2140 :
2141 : This can only be done if the two data references in ALIAS_PAIR access
2142 : the same array object and the index is the only difference. For example,
2143 : if the two data references are DR_A and DR_B:
2144 :
2145 : DR_A DR_B
2146 : data-ref arr[i] arr[j]
2147 : base_object arr arr
2148 : index {i_0, +, 1}_loop {j_0, +, 1}_loop
2149 :
2150 : The addresses and their index are like:
2151 :
2152 : |<- ADDR_A ->| |<- ADDR_B ->|
2153 : ------------------------------------------------------->
2154 : | | | | | | | | | |
2155 : ------------------------------------------------------->
2156 : i_0 ... i_0+4 j_0 ... j_0+4
2157 :
2158 : We can create expression based on index rather than address:
2159 :
2160 : (unsigned) (i_0 - j_0 + 3) <= 6
2161 :
2162 : i.e. the indices are less than 4 apart.
2163 :
2164 : Note evolution step of index needs to be considered in comparison. */
2165 :
2166 : static bool
2167 5046 : create_intersect_range_checks_index (class loop *loop, tree *cond_expr,
2168 : const dr_with_seg_len_pair_t &alias_pair)
2169 : {
2170 5046 : const dr_with_seg_len &dr_a = alias_pair.first;
2171 5046 : const dr_with_seg_len &dr_b = alias_pair.second;
2172 5046 : if ((alias_pair.flags & DR_ALIAS_MIXED_STEPS)
2173 5046 : || integer_zerop (DR_STEP (dr_a.dr))
2174 4789 : || integer_zerop (DR_STEP (dr_b.dr))
2175 19260 : || DR_NUM_DIMENSIONS (dr_a.dr) != DR_NUM_DIMENSIONS (dr_b.dr))
2176 : return false;
2177 :
2178 4649 : poly_uint64 seg_len1, seg_len2;
2179 4649 : if (!poly_int_tree_p (dr_a.seg_len, &seg_len1)
2180 4649 : || !poly_int_tree_p (dr_b.seg_len, &seg_len2))
2181 : return false;
2182 :
2183 4361 : if (!tree_fits_shwi_p (DR_STEP (dr_a.dr)))
2184 : return false;
2185 :
2186 4361 : if (!operand_equal_p (DR_BASE_OBJECT (dr_a.dr), DR_BASE_OBJECT (dr_b.dr), 0))
2187 : return false;
2188 :
2189 152 : if (!operand_equal_p (DR_STEP (dr_a.dr), DR_STEP (dr_b.dr), 0))
2190 : return false;
2191 :
2192 152 : gcc_assert (TREE_CODE (DR_STEP (dr_a.dr)) == INTEGER_CST);
2193 :
2194 152 : bool neg_step = tree_int_cst_compare (DR_STEP (dr_a.dr), size_zero_node) < 0;
2195 152 : unsigned HOST_WIDE_INT abs_step = tree_to_shwi (DR_STEP (dr_a.dr));
2196 152 : if (neg_step)
2197 : {
2198 30 : abs_step = -abs_step;
2199 30 : seg_len1 = (-wi::to_poly_wide (dr_a.seg_len)).force_uhwi ();
2200 30 : seg_len2 = (-wi::to_poly_wide (dr_b.seg_len)).force_uhwi ();
2201 : }
2202 :
2203 : /* Infer the number of iterations with which the memory segment is accessed
2204 : by DR. In other words, alias is checked if memory segment accessed by
2205 : DR_A in some iterations intersect with memory segment accessed by DR_B
2206 : in the same amount iterations.
2207 : Note segnment length is a linear function of number of iterations with
2208 : DR_STEP as the coefficient. */
2209 152 : poly_uint64 niter_len1, niter_len2;
2210 152 : if (!can_div_trunc_p (seg_len1 + abs_step - 1, abs_step, &niter_len1)
2211 152 : || !can_div_trunc_p (seg_len2 + abs_step - 1, abs_step, &niter_len2))
2212 : return false;
2213 :
2214 : /* Divide each access size by the byte step, rounding up. */
2215 152 : poly_uint64 niter_access1, niter_access2;
2216 152 : if (!can_div_trunc_p (dr_a.access_size + abs_step - 1,
2217 : abs_step, &niter_access1)
2218 152 : || !can_div_trunc_p (dr_b.access_size + abs_step - 1,
2219 : abs_step, &niter_access2))
2220 : return false;
2221 :
2222 152 : bool waw_or_war_p = (alias_pair.flags & ~(DR_ALIAS_WAR | DR_ALIAS_WAW)) == 0;
2223 :
2224 152 : int found = -1;
2225 311 : for (unsigned int i = 0; i < DR_NUM_DIMENSIONS (dr_a.dr); i++)
2226 : {
2227 160 : tree access1 = DR_ACCESS_FN (dr_a.dr, i);
2228 160 : tree access2 = DR_ACCESS_FN (dr_b.dr, i);
2229 : /* Two indices must be the same if they are not scev, or not scev wrto
2230 : current loop being vecorized. */
2231 160 : if (TREE_CODE (access1) != POLYNOMIAL_CHREC
2232 152 : || TREE_CODE (access2) != POLYNOMIAL_CHREC
2233 152 : || CHREC_VARIABLE (access1) != (unsigned)loop->num
2234 312 : || CHREC_VARIABLE (access2) != (unsigned)loop->num)
2235 : {
2236 8 : if (operand_equal_p (access1, access2, 0))
2237 7 : continue;
2238 :
2239 : return false;
2240 : }
2241 152 : if (found >= 0)
2242 : return false;
2243 152 : found = i;
2244 : }
2245 :
2246 : /* Ought not to happen in practice, since if all accesses are equal then the
2247 : alias should be decidable at compile time. */
2248 151 : if (found < 0)
2249 : return false;
2250 :
2251 : /* The two indices must have the same step. */
2252 151 : tree access1 = DR_ACCESS_FN (dr_a.dr, found);
2253 151 : tree access2 = DR_ACCESS_FN (dr_b.dr, found);
2254 151 : if (!operand_equal_p (CHREC_RIGHT (access1), CHREC_RIGHT (access2), 0))
2255 : return false;
2256 :
2257 151 : tree idx_step = CHREC_RIGHT (access1);
2258 : /* Index must have const step, otherwise DR_STEP won't be constant. */
2259 151 : gcc_assert (TREE_CODE (idx_step) == INTEGER_CST);
2260 : /* Index must evaluate in the same direction as DR. */
2261 151 : gcc_assert (!neg_step || tree_int_cst_sign_bit (idx_step) == 1);
2262 :
2263 151 : tree min1 = CHREC_LEFT (access1);
2264 151 : tree min2 = CHREC_LEFT (access2);
2265 151 : if (!types_compatible_p (TREE_TYPE (min1), TREE_TYPE (min2)))
2266 : return false;
2267 :
2268 : /* Ideally, alias can be checked against loop's control IV, but we
2269 : need to prove linear mapping between control IV and reference
2270 : index. Although that should be true, we check against (array)
2271 : index of data reference. Like segment length, index length is
2272 : linear function of the number of iterations with index_step as
2273 : the coefficient, i.e, niter_len * idx_step. */
2274 151 : offset_int abs_idx_step = offset_int::from (wi::to_wide (idx_step),
2275 : SIGNED);
2276 151 : if (neg_step)
2277 30 : abs_idx_step = -abs_idx_step;
2278 151 : poly_offset_int idx_len1 = abs_idx_step * niter_len1;
2279 151 : poly_offset_int idx_len2 = abs_idx_step * niter_len2;
2280 151 : poly_offset_int idx_access1 = abs_idx_step * niter_access1;
2281 151 : poly_offset_int idx_access2 = abs_idx_step * niter_access2;
2282 :
2283 151 : gcc_assert (known_ge (idx_len1, 0)
2284 : && known_ge (idx_len2, 0)
2285 : && known_ge (idx_access1, 0)
2286 : && known_ge (idx_access2, 0));
2287 :
2288 : /* Each access has the following pattern, with lengths measured
2289 : in units of INDEX:
2290 :
2291 : <-- idx_len -->
2292 : <--- A: -ve step --->
2293 : +-----+-------+-----+-------+-----+
2294 : | n-1 | ..... | 0 | ..... | n-1 |
2295 : +-----+-------+-----+-------+-----+
2296 : <--- B: +ve step --->
2297 : <-- idx_len -->
2298 : |
2299 : min
2300 :
2301 : where "n" is the number of scalar iterations covered by the segment
2302 : and where each access spans idx_access units.
2303 :
2304 : A is the range of bytes accessed when the step is negative,
2305 : B is the range when the step is positive.
2306 :
2307 : When checking for general overlap, we need to test whether
2308 : the range:
2309 :
2310 : [min1 + low_offset1, min1 + high_offset1 + idx_access1 - 1]
2311 :
2312 : overlaps:
2313 :
2314 : [min2 + low_offset2, min2 + high_offset2 + idx_access2 - 1]
2315 :
2316 : where:
2317 :
2318 : low_offsetN = +ve step ? 0 : -idx_lenN;
2319 : high_offsetN = +ve step ? idx_lenN : 0;
2320 :
2321 : This is equivalent to testing whether:
2322 :
2323 : min1 + low_offset1 <= min2 + high_offset2 + idx_access2 - 1
2324 : && min2 + low_offset2 <= min1 + high_offset1 + idx_access1 - 1
2325 :
2326 : Converting this into a single test, there is an overlap if:
2327 :
2328 : 0 <= min2 - min1 + bias <= limit
2329 :
2330 : where bias = high_offset2 + idx_access2 - 1 - low_offset1
2331 : limit = (high_offset1 - low_offset1 + idx_access1 - 1)
2332 : + (high_offset2 - low_offset2 + idx_access2 - 1)
2333 : i.e. limit = idx_len1 + idx_access1 - 1 + idx_len2 + idx_access2 - 1
2334 :
2335 : Combining the tests requires limit to be computable in an unsigned
2336 : form of the index type; if it isn't, we fall back to the usual
2337 : pointer-based checks.
2338 :
2339 : We can do better if DR_B is a write and if DR_A and DR_B are
2340 : well-ordered in both the original and the new code (see the
2341 : comment above the DR_ALIAS_* flags for details). In this case
2342 : we know that for each i in [0, n-1], the write performed by
2343 : access i of DR_B occurs after access numbers j<=i of DR_A in
2344 : both the original and the new code. Any write or anti
2345 : dependencies wrt those DR_A accesses are therefore maintained.
2346 :
2347 : We just need to make sure that each individual write in DR_B does not
2348 : overlap any higher-indexed access in DR_A; such DR_A accesses happen
2349 : after the DR_B access in the original code but happen before it in
2350 : the new code.
2351 :
2352 : We know the steps for both accesses are equal, so by induction, we
2353 : just need to test whether the first write of DR_B overlaps a later
2354 : access of DR_A. In other words, we need to move min1 along by
2355 : one iteration:
2356 :
2357 : min1' = min1 + idx_step
2358 :
2359 : and use the ranges:
2360 :
2361 : [min1' + low_offset1', min1' + high_offset1' + idx_access1 - 1]
2362 :
2363 : and:
2364 :
2365 : [min2, min2 + idx_access2 - 1]
2366 :
2367 : where:
2368 :
2369 : low_offset1' = +ve step ? 0 : -(idx_len1 - |idx_step|)
2370 : high_offset1' = +ve_step ? idx_len1 - |idx_step| : 0. */
2371 151 : if (waw_or_war_p)
2372 120 : idx_len1 -= abs_idx_step;
2373 :
2374 151 : poly_offset_int limit = idx_len1 + idx_access1 - 1 + idx_access2 - 1;
2375 151 : if (!waw_or_war_p)
2376 151 : limit += idx_len2;
2377 :
2378 151 : tree utype = unsigned_type_for (TREE_TYPE (min1));
2379 151 : if (!wi::fits_to_tree_p (limit, utype))
2380 : return false;
2381 :
2382 151 : poly_offset_int low_offset1 = neg_step ? -idx_len1 : 0;
2383 151 : poly_offset_int high_offset2 = neg_step || waw_or_war_p ? 0 : idx_len2;
2384 151 : poly_offset_int bias = high_offset2 + idx_access2 - 1 - low_offset1;
2385 : /* Equivalent to adding IDX_STEP to MIN1. */
2386 151 : if (waw_or_war_p)
2387 120 : bias -= wi::to_offset (idx_step);
2388 :
2389 151 : tree subject = fold_build2 (MINUS_EXPR, utype,
2390 : fold_convert (utype, min2),
2391 : fold_convert (utype, min1));
2392 151 : subject = fold_build2 (PLUS_EXPR, utype, subject,
2393 : wide_int_to_tree (utype, bias));
2394 151 : tree part_cond_expr = fold_build2 (GT_EXPR, boolean_type_node, subject,
2395 : wide_int_to_tree (utype, limit));
2396 151 : if (*cond_expr)
2397 0 : *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
2398 : *cond_expr, part_cond_expr);
2399 : else
2400 : *cond_expr = part_cond_expr;
2401 151 : if (dump_enabled_p ())
2402 : {
2403 133 : if (waw_or_war_p)
2404 103 : dump_printf (MSG_NOTE, "using an index-based WAR/WAW test\n");
2405 : else
2406 30 : dump_printf (MSG_NOTE, "using an index-based overlap test\n");
2407 : }
2408 : return true;
2409 : }
2410 :
2411 : /* A subroutine of create_intersect_range_checks, with a subset of the
2412 : same arguments. Try to optimize cases in which the second access
2413 : is a write and in which some overlap is valid. */
2414 :
2415 : static bool
2416 4895 : create_waw_or_war_checks (tree *cond_expr,
2417 : const dr_with_seg_len_pair_t &alias_pair)
2418 : {
2419 4895 : const dr_with_seg_len& dr_a = alias_pair.first;
2420 4895 : const dr_with_seg_len& dr_b = alias_pair.second;
2421 :
2422 : /* Check for cases in which:
2423 :
2424 : (a) DR_B is always a write;
2425 : (b) the accesses are well-ordered in both the original and new code
2426 : (see the comment above the DR_ALIAS_* flags for details); and
2427 : (c) the DR_STEPs describe all access pairs covered by ALIAS_PAIR. */
2428 4895 : if (alias_pair.flags & ~(DR_ALIAS_WAR | DR_ALIAS_WAW))
2429 : return false;
2430 :
2431 : /* Check for equal (but possibly variable) steps. */
2432 3125 : tree step = DR_STEP (dr_a.dr);
2433 3125 : if (!operand_equal_p (step, DR_STEP (dr_b.dr)))
2434 : return false;
2435 :
2436 : /* Make sure that we can operate on sizetype without loss of precision. */
2437 2724 : tree addr_type = TREE_TYPE (DR_BASE_ADDRESS (dr_a.dr));
2438 2724 : if (TYPE_PRECISION (addr_type) != TYPE_PRECISION (sizetype))
2439 : return false;
2440 :
2441 : /* All addresses involved are known to have a common alignment ALIGN.
2442 : We can therefore subtract ALIGN from an exclusive endpoint to get
2443 : an inclusive endpoint. In the best (and common) case, ALIGN is the
2444 : same as the access sizes of both DRs, and so subtracting ALIGN
2445 : cancels out the addition of an access size. */
2446 2724 : unsigned int align = MIN (dr_a.align, dr_b.align);
2447 2724 : poly_uint64 last_chunk_a = dr_a.access_size - align;
2448 2724 : poly_uint64 last_chunk_b = dr_b.access_size - align;
2449 :
2450 : /* Get a boolean expression that is true when the step is negative. */
2451 2724 : tree indicator = dr_direction_indicator (dr_a.dr);
2452 2724 : tree neg_step = fold_build2 (LT_EXPR, boolean_type_node,
2453 : fold_convert (ssizetype, indicator),
2454 : ssize_int (0));
2455 :
2456 : /* Get lengths in sizetype. */
2457 2724 : tree seg_len_a
2458 2724 : = fold_convert (sizetype, rewrite_to_non_trapping_overflow (dr_a.seg_len));
2459 2724 : step = fold_convert (sizetype, rewrite_to_non_trapping_overflow (step));
2460 :
2461 : /* Each access has the following pattern:
2462 :
2463 : <- |seg_len| ->
2464 : <--- A: -ve step --->
2465 : +-----+-------+-----+-------+-----+
2466 : | n-1 | ..... | 0 | ..... | n-1 |
2467 : +-----+-------+-----+-------+-----+
2468 : <--- B: +ve step --->
2469 : <- |seg_len| ->
2470 : |
2471 : base address
2472 :
2473 : where "n" is the number of scalar iterations covered by the segment.
2474 :
2475 : A is the range of bytes accessed when the step is negative,
2476 : B is the range when the step is positive.
2477 :
2478 : We know that DR_B is a write. We also know (from checking that
2479 : DR_A and DR_B are well-ordered) that for each i in [0, n-1],
2480 : the write performed by access i of DR_B occurs after access numbers
2481 : j<=i of DR_A in both the original and the new code. Any write or
2482 : anti dependencies wrt those DR_A accesses are therefore maintained.
2483 :
2484 : We just need to make sure that each individual write in DR_B does not
2485 : overlap any higher-indexed access in DR_A; such DR_A accesses happen
2486 : after the DR_B access in the original code but happen before it in
2487 : the new code.
2488 :
2489 : We know the steps for both accesses are equal, so by induction, we
2490 : just need to test whether the first write of DR_B overlaps a later
2491 : access of DR_A. In other words, we need to move addr_a along by
2492 : one iteration:
2493 :
2494 : addr_a' = addr_a + step
2495 :
2496 : and check whether:
2497 :
2498 : [addr_b, addr_b + last_chunk_b]
2499 :
2500 : overlaps:
2501 :
2502 : [addr_a' + low_offset_a, addr_a' + high_offset_a + last_chunk_a]
2503 :
2504 : where [low_offset_a, high_offset_a] spans accesses [1, n-1]. I.e.:
2505 :
2506 : low_offset_a = +ve step ? 0 : seg_len_a - step
2507 : high_offset_a = +ve step ? seg_len_a - step : 0
2508 :
2509 : This is equivalent to testing whether:
2510 :
2511 : addr_a' + low_offset_a <= addr_b + last_chunk_b
2512 : && addr_b <= addr_a' + high_offset_a + last_chunk_a
2513 :
2514 : Converting this into a single test, there is an overlap if:
2515 :
2516 : 0 <= addr_b + last_chunk_b - addr_a' - low_offset_a <= limit
2517 :
2518 : where limit = high_offset_a - low_offset_a + last_chunk_a + last_chunk_b
2519 :
2520 : If DR_A is performed, limit + |step| - last_chunk_b is known to be
2521 : less than the size of the object underlying DR_A. We also know
2522 : that last_chunk_b <= |step|; this is checked elsewhere if it isn't
2523 : guaranteed at compile time. There can therefore be no overflow if
2524 : "limit" is calculated in an unsigned type with pointer precision. */
2525 2724 : tree addr_a = fold_build_pointer_plus (DR_BASE_ADDRESS (dr_a.dr),
2526 : DR_OFFSET (dr_a.dr));
2527 2724 : addr_a = fold_build_pointer_plus (addr_a, DR_INIT (dr_a.dr));
2528 :
2529 2724 : tree addr_b = fold_build_pointer_plus (DR_BASE_ADDRESS (dr_b.dr),
2530 : DR_OFFSET (dr_b.dr));
2531 2724 : addr_b = fold_build_pointer_plus (addr_b, DR_INIT (dr_b.dr));
2532 :
2533 : /* Advance ADDR_A by one iteration and adjust the length to compensate. */
2534 2724 : addr_a = fold_build_pointer_plus (addr_a, step);
2535 2724 : tree seg_len_a_minus_step = fold_build2 (MINUS_EXPR, sizetype,
2536 : seg_len_a, step);
2537 2724 : if (!CONSTANT_CLASS_P (seg_len_a_minus_step))
2538 3 : seg_len_a_minus_step = build1 (SAVE_EXPR, sizetype, seg_len_a_minus_step);
2539 :
2540 2724 : tree low_offset_a = fold_build3 (COND_EXPR, sizetype, neg_step,
2541 : seg_len_a_minus_step, size_zero_node);
2542 2724 : if (!CONSTANT_CLASS_P (low_offset_a))
2543 3 : low_offset_a = build1 (SAVE_EXPR, sizetype, low_offset_a);
2544 :
2545 : /* We could use COND_EXPR <neg_step, size_zero_node, seg_len_a_minus_step>,
2546 : but it's usually more efficient to reuse the LOW_OFFSET_A result. */
2547 2724 : tree high_offset_a = fold_build2 (MINUS_EXPR, sizetype, seg_len_a_minus_step,
2548 : low_offset_a);
2549 :
2550 : /* The amount added to addr_b - addr_a'. */
2551 2724 : tree bias = fold_build2 (MINUS_EXPR, sizetype,
2552 : size_int (last_chunk_b), low_offset_a);
2553 :
2554 2724 : tree limit = fold_build2 (MINUS_EXPR, sizetype, high_offset_a, low_offset_a);
2555 2724 : limit = fold_build2 (PLUS_EXPR, sizetype, limit,
2556 : size_int (last_chunk_a + last_chunk_b));
2557 :
2558 2724 : tree subject = fold_build2 (MINUS_EXPR, sizetype,
2559 : fold_convert (sizetype, addr_b),
2560 : fold_convert (sizetype, addr_a));
2561 2724 : subject = fold_build2 (PLUS_EXPR, sizetype, subject, bias);
2562 :
2563 2724 : *cond_expr = fold_build2 (GT_EXPR, boolean_type_node, subject, limit);
2564 2724 : if (dump_enabled_p ())
2565 374 : dump_printf (MSG_NOTE, "using an address-based WAR/WAW test\n");
2566 : return true;
2567 : }
2568 :
2569 : /* If ALIGN is nonzero, set up *SEQ_MIN_OUT and *SEQ_MAX_OUT so that for
2570 : every address ADDR accessed by D:
2571 :
2572 : *SEQ_MIN_OUT <= ADDR (== ADDR & -ALIGN) <= *SEQ_MAX_OUT
2573 :
2574 : In this case, every element accessed by D is aligned to at least
2575 : ALIGN bytes.
2576 :
2577 : If ALIGN is zero then instead set *SEG_MAX_OUT so that:
2578 :
2579 : *SEQ_MIN_OUT <= ADDR < *SEQ_MAX_OUT. */
2580 :
2581 : static void
2582 4342 : get_segment_min_max (const dr_with_seg_len &d, tree *seg_min_out,
2583 : tree *seg_max_out, HOST_WIDE_INT align)
2584 : {
2585 : /* Each access has the following pattern:
2586 :
2587 : <- |seg_len| ->
2588 : <--- A: -ve step --->
2589 : +-----+-------+-----+-------+-----+
2590 : | n-1 | ,.... | 0 | ..... | n-1 |
2591 : +-----+-------+-----+-------+-----+
2592 : <--- B: +ve step --->
2593 : <- |seg_len| ->
2594 : |
2595 : base address
2596 :
2597 : where "n" is the number of scalar iterations covered by the segment.
2598 : (This should be VF for a particular pair if we know that both steps
2599 : are the same, otherwise it will be the full number of scalar loop
2600 : iterations.)
2601 :
2602 : A is the range of bytes accessed when the step is negative,
2603 : B is the range when the step is positive.
2604 :
2605 : If the access size is "access_size" bytes, the lowest addressed byte is:
2606 :
2607 : base + (step < 0 ? seg_len : 0) [LB]
2608 :
2609 : and the highest addressed byte is always below:
2610 :
2611 : base + (step < 0 ? 0 : seg_len) + access_size [UB]
2612 :
2613 : Thus:
2614 :
2615 : LB <= ADDR < UB
2616 :
2617 : If ALIGN is nonzero, all three values are aligned to at least ALIGN
2618 : bytes, so:
2619 :
2620 : LB <= ADDR <= UB - ALIGN
2621 :
2622 : where "- ALIGN" folds naturally with the "+ access_size" and often
2623 : cancels it out.
2624 :
2625 : We don't try to simplify LB and UB beyond this (e.g. by using
2626 : MIN and MAX based on whether seg_len rather than the stride is
2627 : negative) because it is possible for the absolute size of the
2628 : segment to overflow the range of a ssize_t.
2629 :
2630 : Keeping the pointer_plus outside of the cond_expr should allow
2631 : the cond_exprs to be shared with other alias checks. */
2632 4342 : tree indicator = dr_direction_indicator (d.dr);
2633 4342 : tree neg_step = fold_build2 (LT_EXPR, boolean_type_node,
2634 : fold_convert (ssizetype, indicator),
2635 : ssize_int (0));
2636 4342 : tree addr_base = fold_build_pointer_plus (DR_BASE_ADDRESS (d.dr),
2637 : DR_OFFSET (d.dr));
2638 4342 : addr_base = fold_build_pointer_plus (addr_base, DR_INIT (d.dr));
2639 4342 : tree seg_len
2640 4342 : = fold_convert (sizetype, rewrite_to_non_trapping_overflow (d.seg_len));
2641 :
2642 4342 : tree min_reach = fold_build3 (COND_EXPR, sizetype, neg_step,
2643 : seg_len, size_zero_node);
2644 4342 : tree max_reach = fold_build3 (COND_EXPR, sizetype, neg_step,
2645 : size_zero_node, seg_len);
2646 4342 : max_reach = fold_build2 (PLUS_EXPR, sizetype, max_reach,
2647 : size_int (d.access_size - align));
2648 :
2649 4342 : *seg_min_out = fold_build_pointer_plus (addr_base, min_reach);
2650 4342 : *seg_max_out = fold_build_pointer_plus (addr_base, max_reach);
2651 4342 : }
2652 :
2653 : /* Generate a runtime condition that is true if ALIAS_PAIR is free of aliases,
2654 : storing the condition in *COND_EXPR. The fallback is to generate a
2655 : a test that the two accesses do not overlap:
2656 :
2657 : end_a <= start_b || end_b <= start_a. */
2658 :
2659 : static void
2660 5046 : create_intersect_range_checks (class loop *loop, tree *cond_expr,
2661 : const dr_with_seg_len_pair_t &alias_pair)
2662 : {
2663 5046 : const dr_with_seg_len& dr_a = alias_pair.first;
2664 5046 : const dr_with_seg_len& dr_b = alias_pair.second;
2665 5046 : *cond_expr = NULL_TREE;
2666 5046 : if (create_intersect_range_checks_index (loop, cond_expr, alias_pair))
2667 2875 : return;
2668 :
2669 4895 : if (create_ifn_alias_checks (cond_expr, alias_pair))
2670 : return;
2671 :
2672 4895 : if (create_waw_or_war_checks (cond_expr, alias_pair))
2673 : return;
2674 :
2675 2171 : unsigned HOST_WIDE_INT min_align;
2676 2171 : tree_code cmp_code;
2677 : /* We don't have to check DR_ALIAS_MIXED_STEPS here, since both versions
2678 : are equivalent. This is just an optimization heuristic. */
2679 2171 : if (TREE_CODE (DR_STEP (dr_a.dr)) == INTEGER_CST
2680 2073 : && TREE_CODE (DR_STEP (dr_b.dr)) == INTEGER_CST)
2681 : {
2682 : /* In this case adding access_size to seg_len is likely to give
2683 : a simple X * step, where X is either the number of scalar
2684 : iterations or the vectorization factor. We're better off
2685 : keeping that, rather than subtracting an alignment from it.
2686 :
2687 : In this case the maximum values are exclusive and so there is
2688 : no alias if the maximum of one segment equals the minimum
2689 : of another. */
2690 : min_align = 0;
2691 : cmp_code = LE_EXPR;
2692 : }
2693 : else
2694 : {
2695 : /* Calculate the minimum alignment shared by all four pointers,
2696 : then arrange for this alignment to be subtracted from the
2697 : exclusive maximum values to get inclusive maximum values.
2698 : This "- min_align" is cumulative with a "+ access_size"
2699 : in the calculation of the maximum values. In the best
2700 : (and common) case, the two cancel each other out, leaving
2701 : us with an inclusive bound based only on seg_len. In the
2702 : worst case we're simply adding a smaller number than before.
2703 :
2704 : Because the maximum values are inclusive, there is an alias
2705 : if the maximum value of one segment is equal to the minimum
2706 : value of the other. */
2707 206 : min_align = std::min (dr_a.align, dr_b.align);
2708 206 : cmp_code = LT_EXPR;
2709 : }
2710 :
2711 2171 : tree seg_a_min, seg_a_max, seg_b_min, seg_b_max;
2712 2171 : get_segment_min_max (dr_a, &seg_a_min, &seg_a_max, min_align);
2713 2171 : get_segment_min_max (dr_b, &seg_b_min, &seg_b_max, min_align);
2714 :
2715 2171 : *cond_expr
2716 2171 : = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
2717 : fold_build2 (cmp_code, boolean_type_node, seg_a_max, seg_b_min),
2718 : fold_build2 (cmp_code, boolean_type_node, seg_b_max, seg_a_min));
2719 2171 : if (dump_enabled_p ())
2720 322 : dump_printf (MSG_NOTE, "using an address-based overlap test\n");
2721 : }
2722 :
2723 : /* Create a conditional expression that represents the run-time checks for
2724 : overlapping of address ranges represented by a list of data references
2725 : pairs passed in ALIAS_PAIRS. Data references are in LOOP. The returned
2726 : COND_EXPR is the conditional expression to be used in the if statement
2727 : that controls which version of the loop gets executed at runtime. */
2728 :
2729 : void
2730 3359 : create_runtime_alias_checks (class loop *loop,
2731 : const vec<dr_with_seg_len_pair_t> *alias_pairs,
2732 : tree * cond_expr)
2733 : {
2734 3359 : tree part_cond_expr;
2735 :
2736 15123 : for (const dr_with_seg_len_pair_t &alias_pair : alias_pairs)
2737 : {
2738 5046 : gcc_assert (alias_pair.flags);
2739 5046 : if (dump_enabled_p ())
2740 829 : dump_printf (MSG_NOTE,
2741 : "create runtime check for data references %T and %T\n",
2742 829 : DR_REF (alias_pair.first.dr),
2743 829 : DR_REF (alias_pair.second.dr));
2744 :
2745 : /* Create condition expression for each pair data references. */
2746 5046 : create_intersect_range_checks (loop, &part_cond_expr, alias_pair);
2747 5046 : if (*cond_expr)
2748 4966 : *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
2749 : *cond_expr, part_cond_expr);
2750 : else
2751 80 : *cond_expr = part_cond_expr;
2752 : }
2753 3359 : }
2754 :
2755 : /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
2756 : expressions. */
2757 : static bool
2758 0 : dr_equal_offsets_p1 (tree offset1, tree offset2)
2759 : {
2760 0 : bool res;
2761 :
2762 0 : STRIP_NOPS (offset1);
2763 0 : STRIP_NOPS (offset2);
2764 :
2765 0 : if (offset1 == offset2)
2766 : return true;
2767 :
2768 0 : if (TREE_CODE (offset1) != TREE_CODE (offset2)
2769 0 : || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
2770 : return false;
2771 :
2772 0 : res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
2773 0 : TREE_OPERAND (offset2, 0));
2774 :
2775 0 : if (!res || !BINARY_CLASS_P (offset1))
2776 : return res;
2777 :
2778 0 : res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
2779 0 : TREE_OPERAND (offset2, 1));
2780 :
2781 0 : return res;
2782 : }
2783 :
2784 : /* Check if DRA and DRB have equal offsets. */
2785 : bool
2786 0 : dr_equal_offsets_p (struct data_reference *dra,
2787 : struct data_reference *drb)
2788 : {
2789 0 : tree offset1, offset2;
2790 :
2791 0 : offset1 = DR_OFFSET (dra);
2792 0 : offset2 = DR_OFFSET (drb);
2793 :
2794 0 : return dr_equal_offsets_p1 (offset1, offset2);
2795 : }
2796 :
2797 : /* Returns true if FNA == FNB. */
2798 :
2799 : static bool
2800 0 : affine_function_equal_p (affine_fn fna, affine_fn fnb)
2801 : {
2802 0 : unsigned i, n = fna.length ();
2803 :
2804 0 : if (n != fnb.length ())
2805 : return false;
2806 :
2807 0 : for (i = 0; i < n; i++)
2808 0 : if (!operand_equal_p (fna[i], fnb[i], 0))
2809 : return false;
2810 :
2811 : return true;
2812 : }
2813 :
2814 : /* If all the functions in CF are the same, returns one of them,
2815 : otherwise returns NULL. */
2816 :
2817 : static affine_fn
2818 2352028 : common_affine_function (conflict_function *cf)
2819 : {
2820 2352028 : unsigned i;
2821 2352028 : affine_fn comm;
2822 :
2823 2352028 : if (!CF_NONTRIVIAL_P (cf))
2824 0 : return affine_fn ();
2825 :
2826 2352028 : comm = cf->fns[0];
2827 :
2828 2352028 : for (i = 1; i < cf->n; i++)
2829 0 : if (!affine_function_equal_p (comm, cf->fns[i]))
2830 0 : return affine_fn ();
2831 :
2832 2352028 : return comm;
2833 : }
2834 :
2835 : /* Returns the base of the affine function FN. */
2836 :
2837 : static tree
2838 1355228 : affine_function_base (affine_fn fn)
2839 : {
2840 0 : return fn[0];
2841 : }
2842 :
2843 : /* Returns true if FN is a constant. */
2844 :
2845 : static bool
2846 1355537 : affine_function_constant_p (affine_fn fn)
2847 : {
2848 1355537 : unsigned i;
2849 1355537 : tree coef;
2850 :
2851 1414365 : for (i = 1; fn.iterate (i, &coef); i++)
2852 59137 : if (!integer_zerop (coef))
2853 : return false;
2854 :
2855 : return true;
2856 : }
2857 :
2858 : /* Returns true if FN is the zero constant function. */
2859 :
2860 : static bool
2861 179523 : affine_function_zero_p (affine_fn fn)
2862 : {
2863 179523 : return (integer_zerop (affine_function_base (fn))
2864 179523 : && affine_function_constant_p (fn));
2865 : }
2866 :
2867 : /* Returns a signed integer type with the largest precision from TA
2868 : and TB. */
2869 :
2870 : static tree
2871 1787868 : signed_type_for_types (tree ta, tree tb)
2872 : {
2873 1787868 : if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
2874 565 : return signed_type_for (ta);
2875 : else
2876 1787303 : return signed_type_for (tb);
2877 : }
2878 :
2879 : /* Applies operation OP on affine functions FNA and FNB, and returns the
2880 : result. */
2881 :
2882 : static affine_fn
2883 1176014 : affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
2884 : {
2885 1176014 : unsigned i, n, m;
2886 1176014 : affine_fn ret;
2887 1176014 : tree coef;
2888 :
2889 3528042 : if (fnb.length () > fna.length ())
2890 : {
2891 0 : n = fna.length ();
2892 0 : m = fnb.length ();
2893 : }
2894 : else
2895 : {
2896 1176014 : n = fnb.length ();
2897 1176014 : m = fna.length ();
2898 : }
2899 :
2900 1176014 : ret.create (m);
2901 3587179 : for (i = 0; i < n; i++)
2902 : {
2903 2470302 : tree type = signed_type_for_types (TREE_TYPE (fna[i]),
2904 1235151 : TREE_TYPE (fnb[i]));
2905 1235151 : ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
2906 : }
2907 :
2908 1176014 : for (; fna.iterate (i, &coef); i++)
2909 0 : ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
2910 : coef, integer_zero_node));
2911 1176014 : for (; fnb.iterate (i, &coef); i++)
2912 0 : ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
2913 : integer_zero_node, coef));
2914 :
2915 1176014 : return ret;
2916 : }
2917 :
2918 : /* Returns the sum of affine functions FNA and FNB. */
2919 :
2920 : static affine_fn
2921 0 : affine_fn_plus (affine_fn fna, affine_fn fnb)
2922 : {
2923 0 : return affine_fn_op (PLUS_EXPR, fna, fnb);
2924 : }
2925 :
2926 : /* Returns the difference of affine functions FNA and FNB. */
2927 :
2928 : static affine_fn
2929 1176014 : affine_fn_minus (affine_fn fna, affine_fn fnb)
2930 : {
2931 0 : return affine_fn_op (MINUS_EXPR, fna, fnb);
2932 : }
2933 :
2934 : /* Frees affine function FN. */
2935 :
2936 : static void
2937 3732358 : affine_fn_free (affine_fn fn)
2938 : {
2939 0 : fn.release ();
2940 0 : }
2941 :
2942 : /* Determine for each subscript in the data dependence relation DDR
2943 : the distance. */
2944 :
2945 : static void
2946 3250808 : compute_subscript_distance (struct data_dependence_relation *ddr)
2947 : {
2948 3250808 : conflict_function *cf_a, *cf_b;
2949 3250808 : affine_fn fn_a, fn_b, diff;
2950 :
2951 3250808 : if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
2952 : {
2953 : unsigned int i;
2954 :
2955 4426822 : for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
2956 : {
2957 1176014 : struct subscript *subscript;
2958 :
2959 1176014 : subscript = DDR_SUBSCRIPT (ddr, i);
2960 1176014 : cf_a = SUB_CONFLICTS_IN_A (subscript);
2961 1176014 : cf_b = SUB_CONFLICTS_IN_B (subscript);
2962 :
2963 1176014 : fn_a = common_affine_function (cf_a);
2964 1176014 : fn_b = common_affine_function (cf_b);
2965 1176014 : if (!fn_a.exists () || !fn_b.exists ())
2966 : {
2967 0 : SUB_DISTANCE (subscript) = chrec_dont_know;
2968 0 : return;
2969 : }
2970 1176014 : diff = affine_fn_minus (fn_a, fn_b);
2971 :
2972 1176014 : if (affine_function_constant_p (diff))
2973 1175705 : SUB_DISTANCE (subscript) = affine_function_base (diff);
2974 : else
2975 309 : SUB_DISTANCE (subscript) = chrec_dont_know;
2976 :
2977 1176014 : affine_fn_free (diff);
2978 : }
2979 : }
2980 : }
2981 :
2982 : /* Returns the conflict function for "unknown". */
2983 :
2984 : static conflict_function *
2985 8389636 : conflict_fn_not_known (void)
2986 : {
2987 0 : conflict_function *fn = XCNEW (conflict_function);
2988 8389636 : fn->n = NOT_KNOWN;
2989 :
2990 8389636 : return fn;
2991 : }
2992 :
2993 : /* Returns the conflict function for "independent". */
2994 :
2995 : static conflict_function *
2996 4567938 : conflict_fn_no_dependence (void)
2997 : {
2998 0 : conflict_function *fn = XCNEW (conflict_function);
2999 4567938 : fn->n = NO_DEPENDENCE;
3000 :
3001 4567938 : return fn;
3002 : }
3003 :
3004 : /* Returns true if the address of OBJ is invariant in LOOP. */
3005 :
3006 : static bool
3007 3460481 : object_address_invariant_in_loop_p (const class loop *loop, const_tree obj)
3008 : {
3009 3627562 : while (handled_component_p (obj))
3010 : {
3011 172499 : if (TREE_CODE (obj) == ARRAY_REF)
3012 : {
3013 9818 : for (int i = 1; i < 4; ++i)
3014 8718 : if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, i),
3015 8718 : loop->num))
3016 : return false;
3017 : }
3018 165981 : else if (TREE_CODE (obj) == COMPONENT_REF)
3019 : {
3020 143404 : if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
3021 143404 : loop->num))
3022 : return false;
3023 : }
3024 167081 : obj = TREE_OPERAND (obj, 0);
3025 : }
3026 :
3027 3455063 : if (!INDIRECT_REF_P (obj)
3028 3455063 : && TREE_CODE (obj) != MEM_REF)
3029 : return true;
3030 :
3031 3429161 : return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
3032 6858322 : loop->num);
3033 : }
3034 :
3035 : /* Helper for contains_ssa_ref_p. */
3036 :
3037 : static bool
3038 102496 : contains_ssa_ref_p_1 (tree, tree *idx, void *data)
3039 : {
3040 102496 : if (TREE_CODE (*idx) == SSA_NAME)
3041 : {
3042 95743 : *(bool *)data = true;
3043 95743 : return false;
3044 : }
3045 : return true;
3046 : }
3047 :
3048 : /* Returns true if the reference REF contains a SSA index. */
3049 :
3050 : static bool
3051 267022 : contains_ssa_ref_p (tree ref)
3052 : {
3053 267022 : bool res = false;
3054 0 : for_each_index (&ref, contains_ssa_ref_p_1, &res);
3055 267022 : return res;
3056 : }
3057 :
3058 : /* Returns false if we can prove that data references A and B do not alias,
3059 : true otherwise. If LOOP_NEST is false no cross-iteration aliases are
3060 : considered. */
3061 :
3062 : bool
3063 14828631 : dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
3064 : class loop *loop_nest)
3065 : {
3066 14828631 : tree addr_a = DR_BASE_OBJECT (a);
3067 14828631 : tree addr_b = DR_BASE_OBJECT (b);
3068 :
3069 : /* If we are not processing a loop nest but scalar code we
3070 : do not need to care about possible cross-iteration dependences
3071 : and thus can process the full original reference. Do so,
3072 : similar to how loop invariant motion applies extra offset-based
3073 : disambiguation. */
3074 14828631 : if (!loop_nest)
3075 : {
3076 8143103 : tree tree_size_a = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (a)));
3077 8143103 : tree tree_size_b = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (b)));
3078 :
3079 8143103 : if (DR_BASE_ADDRESS (a)
3080 8134299 : && DR_BASE_ADDRESS (b)
3081 8133951 : && operand_equal_p (DR_BASE_ADDRESS (a), DR_BASE_ADDRESS (b))
3082 7294468 : && operand_equal_p (DR_OFFSET (a), DR_OFFSET (b))
3083 7200822 : && tree_size_a
3084 7200822 : && tree_size_b
3085 7200813 : && poly_int_tree_p (tree_size_a)
3086 7200786 : && poly_int_tree_p (tree_size_b)
3087 15343889 : && !ranges_maybe_overlap_p (wi::to_poly_widest (DR_INIT (a)),
3088 7200786 : wi::to_poly_widest (tree_size_a),
3089 7200786 : wi::to_poly_widest (DR_INIT (b)),
3090 7200786 : wi::to_poly_widest (tree_size_b)))
3091 : {
3092 5369531 : gcc_assert (integer_zerop (DR_STEP (a))
3093 : && integer_zerop (DR_STEP (b)));
3094 5370192 : return false;
3095 : }
3096 :
3097 5547144 : aff_tree off1, off2;
3098 2773572 : poly_widest_int size1, size2;
3099 2773572 : get_inner_reference_aff (DR_REF (a), &off1, &size1);
3100 2773572 : get_inner_reference_aff (DR_REF (b), &off2, &size2);
3101 2773572 : aff_combination_scale (&off1, -1);
3102 2773572 : aff_combination_add (&off2, &off1);
3103 2773572 : if (aff_comb_cannot_overlap_p (&off2, size1, size2))
3104 661 : return false;
3105 2773572 : }
3106 :
3107 : /* Try the points-to information recorded for the base pointers the
3108 : references were originally analyzed from. DR_BASE_OBJECT can be less
3109 : precise, rooting at another SSA name or at one created after points-to
3110 : information was computed and thus without points-to info. A
3111 : recorded solution is not revalidated. It covers every dynamic value
3112 : of its SSA name, so it holds for cross-iteration queries as well. */
3113 9458439 : const pt_solution *pt_a = DR_PTR_INFO (a);
3114 9458439 : const pt_solution *pt_b = DR_PTR_INFO (b);
3115 9458439 : if (pt_a && pt_b && !pt_solutions_intersect (pt_a, pt_b))
3116 : return false;
3117 :
3118 9039531 : if ((TREE_CODE (addr_a) == MEM_REF || TREE_CODE (addr_a) == TARGET_MEM_REF)
3119 6690353 : && (TREE_CODE (addr_b) == MEM_REF || TREE_CODE (addr_b) == TARGET_MEM_REF)
3120 : /* For cross-iteration dependences the cliques must be valid for the
3121 : whole loop, not just individual iterations. */
3122 6432888 : && (!loop_nest
3123 6125408 : || MR_DEPENDENCE_CLIQUE (addr_a) == 1
3124 5447858 : || MR_DEPENDENCE_CLIQUE (addr_a) == loop_nest->owned_clique)
3125 6154398 : && MR_DEPENDENCE_CLIQUE (addr_a) == MR_DEPENDENCE_CLIQUE (addr_b)
3126 14999167 : && MR_DEPENDENCE_BASE (addr_a) != MR_DEPENDENCE_BASE (addr_b))
3127 : return false;
3128 :
3129 : /* If we had an evolution in a pointer-based MEM_REF BASE_OBJECT we
3130 : do not know the size of the base-object. So we cannot do any
3131 : offset/overlap based analysis but have to rely on points-to
3132 : information only. */
3133 9009984 : if (TREE_CODE (addr_a) == MEM_REF
3134 9009984 : && (DR_UNCONSTRAINED_BASE (a)
3135 4298337 : || TREE_CODE (TREE_OPERAND (addr_a, 0)) == SSA_NAME))
3136 : {
3137 : /* For true dependences we can apply TBAA. */
3138 4051079 : if (!loop_nest
3139 203538 : && flag_strict_aliasing
3140 151500 : && DR_IS_WRITE (a) && DR_IS_READ (b)
3141 4055651 : && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
3142 4572 : get_alias_set (DR_REF (b))))
3143 : return false;
3144 4049918 : if (TREE_CODE (addr_b) == MEM_REF)
3145 3945269 : return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
3146 7890538 : TREE_OPERAND (addr_b, 0));
3147 : else
3148 104649 : return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
3149 104649 : build_fold_addr_expr (addr_b));
3150 : }
3151 4958905 : else if (TREE_CODE (addr_b) == MEM_REF
3152 4958905 : && (DR_UNCONSTRAINED_BASE (b)
3153 2670861 : || TREE_CODE (TREE_OPERAND (addr_b, 0)) == SSA_NAME))
3154 : {
3155 : /* For true dependences we can apply TBAA. */
3156 325938 : if (!loop_nest
3157 149279 : && flag_strict_aliasing
3158 98446 : && DR_IS_WRITE (a) && DR_IS_READ (b)
3159 330856 : && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
3160 4918 : get_alias_set (DR_REF (b))))
3161 : return false;
3162 324033 : if (TREE_CODE (addr_a) == MEM_REF)
3163 195606 : return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
3164 391212 : TREE_OPERAND (addr_b, 0));
3165 : else
3166 128427 : return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
3167 256854 : TREE_OPERAND (addr_b, 0));
3168 : }
3169 : /* If dr_analyze_innermost failed to handle a component we are
3170 : possibly left with a non-base in which case we didn't analyze
3171 : a possible evolution of the base when analyzing a loop. */
3172 4632967 : else if (loop_nest
3173 6926955 : && ((handled_component_p (addr_a) && contains_ssa_ref_p (addr_a))
3174 88312 : || (handled_component_p (addr_b) && contains_ssa_ref_p (addr_b))))
3175 : {
3176 95743 : if (TREE_CODE (addr_a) == MEM_REF)
3177 4333 : return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
3178 4333 : build_fold_addr_expr (addr_b));
3179 91410 : else if (TREE_CODE (addr_b) == MEM_REF)
3180 9763 : return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
3181 19526 : TREE_OPERAND (addr_b, 0));
3182 : else
3183 81647 : return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
3184 81647 : build_fold_addr_expr (addr_b));
3185 : }
3186 :
3187 : /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
3188 : that is being subsetted in the loop nest. */
3189 6876203 : return refs_may_alias_p (addr_a, addr_b,
3190 : /* For true dependences we can apply TBAA. */
3191 6876203 : !loop_nest && DR_IS_WRITE (a) && DR_IS_READ (b));
3192 : }
3193 :
3194 : /* REF_A and REF_B both satisfy access_fn_component_p. Return true
3195 : if it is meaningful to compare their associated access functions
3196 : when checking for dependencies. */
3197 :
3198 : static bool
3199 3069746 : access_fn_components_comparable_p (tree ref_a, tree ref_b)
3200 : {
3201 : /* Allow pairs of component refs from the following sets:
3202 :
3203 : { REALPART_EXPR, IMAGPART_EXPR }
3204 : { COMPONENT_REF }
3205 : { ARRAY_REF }. */
3206 3069746 : tree_code code_a = TREE_CODE (ref_a);
3207 3069746 : tree_code code_b = TREE_CODE (ref_b);
3208 3069746 : if (code_a == IMAGPART_EXPR)
3209 36651 : code_a = REALPART_EXPR;
3210 3069746 : if (code_b == IMAGPART_EXPR)
3211 43006 : code_b = REALPART_EXPR;
3212 3069746 : if (code_a != code_b)
3213 : return false;
3214 :
3215 3042842 : if (TREE_CODE (ref_a) == COMPONENT_REF)
3216 : /* ??? We cannot simply use the type of operand #0 of the refs here as
3217 : the Fortran compiler smuggles type punning into COMPONENT_REFs.
3218 : Use the DECL_CONTEXT of the FIELD_DECLs instead. */
3219 1013648 : return (DECL_CONTEXT (TREE_OPERAND (ref_a, 1))
3220 1013648 : == DECL_CONTEXT (TREE_OPERAND (ref_b, 1)));
3221 :
3222 2029194 : return types_compatible_p (TREE_TYPE (TREE_OPERAND (ref_a, 0)),
3223 4058388 : TREE_TYPE (TREE_OPERAND (ref_b, 0)));
3224 : }
3225 :
3226 : /* Initialize a data dependence relation RES in LOOP_NEST. USE_ALT_INDICES
3227 : is true when the main indices of A and B were not comparable so we try again
3228 : with alternate indices computed on an indirect reference. */
3229 :
3230 : struct data_dependence_relation *
3231 6432840 : initialize_data_dependence_relation (struct data_dependence_relation *res,
3232 : vec<loop_p> loop_nest,
3233 : bool use_alt_indices)
3234 : {
3235 6813604 : struct data_reference *a = DDR_A (res);
3236 6813604 : struct data_reference *b = DDR_B (res);
3237 6813604 : unsigned int i;
3238 :
3239 6813604 : struct indices *indices_a = &a->indices;
3240 6813604 : struct indices *indices_b = &b->indices;
3241 6813604 : if (use_alt_indices)
3242 : {
3243 380764 : if (TREE_CODE (DR_REF (a)) != MEM_REF)
3244 245615 : indices_a = &a->alt_indices;
3245 380764 : if (TREE_CODE (DR_REF (b)) != MEM_REF)
3246 269040 : indices_b = &b->alt_indices;
3247 : }
3248 6813604 : unsigned int num_dimensions_a = indices_a->access_fns.length ();
3249 6813604 : unsigned int num_dimensions_b = indices_b->access_fns.length ();
3250 6813604 : if (num_dimensions_a == 0 || num_dimensions_b == 0)
3251 : {
3252 2272200 : DDR_ARE_DEPENDENT (res) = chrec_dont_know;
3253 2272200 : return res;
3254 : }
3255 :
3256 : /* For unconstrained bases, the root (highest-indexed) subscript
3257 : describes a variation in the base of the original DR_REF rather
3258 : than a component access. We have no type that accurately describes
3259 : the new DR_BASE_OBJECT (whose TREE_TYPE describes the type *after*
3260 : applying this subscript) so limit the search to the last real
3261 : component access.
3262 :
3263 : E.g. for:
3264 :
3265 : void
3266 : f (int a[][8], int b[][8])
3267 : {
3268 : for (int i = 0; i < 8; ++i)
3269 : a[i * 2][0] = b[i][0];
3270 : }
3271 :
3272 : the a and b accesses have a single ARRAY_REF component reference [0]
3273 : but have two subscripts. */
3274 4541404 : if (indices_a->unconstrained_base)
3275 2512513 : num_dimensions_a -= 1;
3276 4541404 : if (indices_b->unconstrained_base)
3277 2475277 : num_dimensions_b -= 1;
3278 :
3279 : /* These structures describe sequences of component references in
3280 : DR_REF (A) and DR_REF (B). Each component reference is tied to a
3281 : specific access function. */
3282 4541404 : struct {
3283 : /* The sequence starts at DR_ACCESS_FN (A, START_A) of A and
3284 : DR_ACCESS_FN (B, START_B) of B (inclusive) and extends to higher
3285 : indices. In C notation, these are the indices of the rightmost
3286 : component references; e.g. for a sequence .b.c.d, the start
3287 : index is for .d. */
3288 : unsigned int start_a;
3289 : unsigned int start_b;
3290 :
3291 : /* The sequence contains LENGTH consecutive access functions from
3292 : each DR. */
3293 : unsigned int length;
3294 :
3295 : /* The enclosing objects for the A and B sequences respectively,
3296 : i.e. the objects to which DR_ACCESS_FN (A, START_A + LENGTH - 1)
3297 : and DR_ACCESS_FN (B, START_B + LENGTH - 1) are applied. */
3298 : tree object_a;
3299 : tree object_b;
3300 4541404 : } full_seq = {}, struct_seq = {};
3301 :
3302 : /* Before each iteration of the loop:
3303 :
3304 : - REF_A is what you get after applying DR_ACCESS_FN (A, INDEX_A) and
3305 : - REF_B is what you get after applying DR_ACCESS_FN (B, INDEX_B). */
3306 4541404 : unsigned int index_a = 0;
3307 4541404 : unsigned int index_b = 0;
3308 4541404 : tree ref_a = DR_REF (a);
3309 4541404 : tree ref_b = DR_REF (b);
3310 :
3311 : /* Now walk the component references from the final DR_REFs back up to
3312 : the enclosing base objects. Each component reference corresponds
3313 : to one access function in the DR, with access function 0 being for
3314 : the final DR_REF and the highest-indexed access function being the
3315 : one that is applied to the base of the DR.
3316 :
3317 : Look for a sequence of component references whose access functions
3318 : are comparable (see access_fn_components_comparable_p). If more
3319 : than one such sequence exists, pick the one nearest the base
3320 : (which is the leftmost sequence in C notation). Store this sequence
3321 : in FULL_SEQ.
3322 :
3323 : For example, if we have:
3324 :
3325 : struct foo { struct bar s; ... } (*a)[10], (*b)[10];
3326 :
3327 : A: a[0][i].s.c.d
3328 : B: __real b[0][i].s.e[i].f
3329 :
3330 : (where d is the same type as the real component of f) then the access
3331 : functions would be:
3332 :
3333 : 0 1 2 3
3334 : A: .d .c .s [i]
3335 :
3336 : 0 1 2 3 4 5
3337 : B: __real .f [i] .e .s [i]
3338 :
3339 : The A0/B2 column isn't comparable, since .d is a COMPONENT_REF
3340 : and [i] is an ARRAY_REF. However, the A1/B3 column contains two
3341 : COMPONENT_REF accesses for struct bar, so is comparable. Likewise
3342 : the A2/B4 column contains two COMPONENT_REF accesses for struct foo,
3343 : so is comparable. The A3/B5 column contains two ARRAY_REFs that
3344 : index foo[10] arrays, so is again comparable. The sequence is
3345 : therefore:
3346 :
3347 : A: [1, 3] (i.e. [i].s.c)
3348 : B: [3, 5] (i.e. [i].s.e)
3349 :
3350 : Also look for sequences of component references whose access
3351 : functions are comparable and whose enclosing objects have the same
3352 : RECORD_TYPE. Store this sequence in STRUCT_SEQ. In the above
3353 : example, STRUCT_SEQ would be:
3354 :
3355 : A: [1, 2] (i.e. s.c)
3356 : B: [3, 4] (i.e. s.e) */
3357 7593783 : while (index_a < num_dimensions_a && index_b < num_dimensions_b)
3358 : {
3359 : /* The alternate indices form always has a single dimension
3360 : with unconstrained base. */
3361 3069746 : gcc_assert (!use_alt_indices);
3362 :
3363 : /* REF_A and REF_B must be one of the component access types
3364 : allowed by dr_analyze_indices. */
3365 3069746 : gcc_checking_assert (access_fn_component_p (ref_a));
3366 3069746 : gcc_checking_assert (access_fn_component_p (ref_b));
3367 :
3368 : /* Get the immediately-enclosing objects for REF_A and REF_B,
3369 : i.e. the references *before* applying DR_ACCESS_FN (A, INDEX_A)
3370 : and DR_ACCESS_FN (B, INDEX_B). */
3371 3069746 : tree object_a = TREE_OPERAND (ref_a, 0);
3372 3069746 : tree object_b = TREE_OPERAND (ref_b, 0);
3373 :
3374 3069746 : tree type_a = TREE_TYPE (object_a);
3375 3069746 : tree type_b = TREE_TYPE (object_b);
3376 3069746 : if (access_fn_components_comparable_p (ref_a, ref_b))
3377 : {
3378 : /* This pair of component accesses is comparable for dependence
3379 : analysis, so we can include DR_ACCESS_FN (A, INDEX_A) and
3380 : DR_ACCESS_FN (B, INDEX_B) in the sequence. */
3381 2804115 : if (full_seq.start_a + full_seq.length != index_a
3382 2747182 : || full_seq.start_b + full_seq.length != index_b)
3383 : {
3384 : /* The accesses don't extend the current sequence,
3385 : so start a new one here. */
3386 64713 : full_seq.start_a = index_a;
3387 64713 : full_seq.start_b = index_b;
3388 64713 : full_seq.length = 0;
3389 : }
3390 :
3391 : /* Add this pair of references to the sequence. */
3392 2804115 : full_seq.length += 1;
3393 2804115 : full_seq.object_a = object_a;
3394 2804115 : full_seq.object_b = object_b;
3395 :
3396 : /* If the enclosing objects are structures (and thus have the
3397 : same RECORD_TYPE), record the new sequence in STRUCT_SEQ. */
3398 2804115 : if (TREE_CODE (type_a) == RECORD_TYPE)
3399 796139 : struct_seq = full_seq;
3400 :
3401 : /* Move to the next containing reference for both A and B. */
3402 2804115 : ref_a = object_a;
3403 2804115 : ref_b = object_b;
3404 2804115 : index_a += 1;
3405 2804115 : index_b += 1;
3406 2804115 : continue;
3407 : }
3408 :
3409 : /* Try to approach equal type sizes. */
3410 265631 : if (!COMPLETE_TYPE_P (type_a)
3411 258503 : || !COMPLETE_TYPE_P (type_b)
3412 250192 : || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type_a))
3413 514219 : || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type_b)))
3414 : break;
3415 :
3416 248264 : unsigned HOST_WIDE_INT size_a = tree_to_uhwi (TYPE_SIZE_UNIT (type_a));
3417 248264 : unsigned HOST_WIDE_INT size_b = tree_to_uhwi (TYPE_SIZE_UNIT (type_b));
3418 248264 : if (size_a <= size_b)
3419 : {
3420 148990 : index_a += 1;
3421 148990 : ref_a = object_a;
3422 : }
3423 248264 : if (size_b <= size_a)
3424 : {
3425 114697 : index_b += 1;
3426 114697 : ref_b = object_b;
3427 : }
3428 : }
3429 :
3430 : /* See whether FULL_SEQ ends at the base and whether the two bases
3431 : are equal. We do not care about TBAA or alignment info so we can
3432 : use OEP_ADDRESS_OF to avoid false negatives. */
3433 4541404 : tree base_a = indices_a->base_object;
3434 4541404 : tree base_b = indices_b->base_object;
3435 4541404 : bool same_base_p = (full_seq.start_a + full_seq.length == num_dimensions_a
3436 4323362 : && full_seq.start_b + full_seq.length == num_dimensions_b
3437 4174855 : && (indices_a->unconstrained_base
3438 4174855 : == indices_b->unconstrained_base)
3439 4169484 : && operand_equal_p (base_a, base_b, OEP_ADDRESS_OF)
3440 3672761 : && (types_compatible_p (TREE_TYPE (base_a),
3441 3672761 : TREE_TYPE (base_b))
3442 863952 : || (!base_supports_access_fn_components_p (base_a)
3443 858620 : && !base_supports_access_fn_components_p (base_b)
3444 857033 : && operand_equal_p
3445 857033 : (TYPE_SIZE (TREE_TYPE (base_a)),
3446 857033 : TYPE_SIZE (TREE_TYPE (base_b)), 0)))
3447 7760817 : && (!loop_nest.exists ()
3448 3219413 : || (object_address_invariant_in_loop_p
3449 3219413 : (loop_nest[0], base_a))));
3450 :
3451 : /* If the bases are the same, we can include the base variation too.
3452 : E.g. the b accesses in:
3453 :
3454 : for (int i = 0; i < n; ++i)
3455 : b[i + 4][0] = b[i][0];
3456 :
3457 : have a definite dependence distance of 4, while for:
3458 :
3459 : for (int i = 0; i < n; ++i)
3460 : a[i + 4][0] = b[i][0];
3461 :
3462 : the dependence distance depends on the gap between a and b.
3463 :
3464 : If the bases are different then we can only rely on the sequence
3465 : rooted at a structure access, since arrays are allowed to overlap
3466 : arbitrarily and change shape arbitrarily. E.g. we treat this as
3467 : valid code:
3468 :
3469 : int a[256];
3470 : ...
3471 : ((int (*)[4][3]) &a[1])[i][0] += ((int (*)[4][3]) &a[2])[i][0];
3472 :
3473 : where two lvalues with the same int[4][3] type overlap, and where
3474 : both lvalues are distinct from the object's declared type. */
3475 : if (same_base_p)
3476 : {
3477 3092334 : if (indices_a->unconstrained_base)
3478 1510182 : full_seq.length += 1;
3479 : }
3480 : else
3481 : full_seq = struct_seq;
3482 :
3483 : /* Punt if we didn't find a suitable sequence. */
3484 4541404 : if (full_seq.length == 0)
3485 : {
3486 1156814 : if (use_alt_indices
3487 1031247 : || (TREE_CODE (DR_REF (a)) == MEM_REF
3488 784821 : && TREE_CODE (DR_REF (b)) == MEM_REF)
3489 383315 : || may_be_nonaddressable_p (DR_REF (a))
3490 1539708 : || may_be_nonaddressable_p (DR_REF (b)))
3491 : {
3492 : /* Fully exhausted possibilities. */
3493 776050 : DDR_ARE_DEPENDENT (res) = chrec_dont_know;
3494 776050 : return res;
3495 : }
3496 :
3497 : /* Try evaluating both DRs as dereferences of pointers. */
3498 380764 : if (!a->alt_indices.base_object
3499 169376 : && TREE_CODE (DR_REF (a)) != MEM_REF)
3500 : {
3501 34227 : tree alt_ref = build2 (MEM_REF, TREE_TYPE (DR_REF (a)),
3502 : build1 (ADDR_EXPR, ptr_type_node, DR_REF (a)),
3503 : build_int_cst
3504 : (reference_alias_ptr_type (DR_REF (a)), 0));
3505 102681 : dr_analyze_indices (&a->alt_indices, alt_ref,
3506 34227 : loop_preheader_edge (loop_nest[0]),
3507 : loop_containing_stmt (DR_STMT (a)));
3508 : }
3509 380764 : if (!b->alt_indices.base_object
3510 193221 : && TREE_CODE (DR_REF (b)) != MEM_REF)
3511 : {
3512 81497 : tree alt_ref = build2 (MEM_REF, TREE_TYPE (DR_REF (b)),
3513 : build1 (ADDR_EXPR, ptr_type_node, DR_REF (b)),
3514 : build_int_cst
3515 : (reference_alias_ptr_type (DR_REF (b)), 0));
3516 244491 : dr_analyze_indices (&b->alt_indices, alt_ref,
3517 81497 : loop_preheader_edge (loop_nest[0]),
3518 : loop_containing_stmt (DR_STMT (b)));
3519 : }
3520 380764 : return initialize_data_dependence_relation (res, loop_nest, true);
3521 : }
3522 :
3523 3384590 : if (!same_base_p)
3524 : {
3525 : /* Partial overlap is possible for different bases when strict aliasing
3526 : is not in effect. It's also possible if either base involves a union
3527 : access; e.g. for:
3528 :
3529 : struct s1 { int a[2]; };
3530 : struct s2 { struct s1 b; int c; };
3531 : struct s3 { int d; struct s1 e; };
3532 : union u { struct s2 f; struct s3 g; } *p, *q;
3533 :
3534 : the s1 at "p->f.b" (base "p->f") partially overlaps the s1 at
3535 : "p->g.e" (base "p->g") and might partially overlap the s1 at
3536 : "q->g.e" (base "q->g"). */
3537 292256 : if (!flag_strict_aliasing
3538 280542 : || ref_contains_union_access_p (full_seq.object_a)
3539 514286 : || ref_contains_union_access_p (full_seq.object_b))
3540 : {
3541 70252 : DDR_ARE_DEPENDENT (res) = chrec_dont_know;
3542 70252 : return res;
3543 : }
3544 :
3545 222004 : DDR_COULD_BE_INDEPENDENT_P (res) = true;
3546 222004 : if (!loop_nest.exists ()
3547 444008 : || (object_address_invariant_in_loop_p (loop_nest[0],
3548 222004 : full_seq.object_a)
3549 19064 : && object_address_invariant_in_loop_p (loop_nest[0],
3550 19064 : full_seq.object_b)))
3551 : {
3552 9579 : DDR_OBJECT_A (res) = full_seq.object_a;
3553 9579 : DDR_OBJECT_B (res) = full_seq.object_b;
3554 : }
3555 : }
3556 :
3557 3314338 : DDR_AFFINE_P (res) = true;
3558 3314338 : DDR_ARE_DEPENDENT (res) = NULL_TREE;
3559 3314338 : DDR_SUBSCRIPTS (res).create (full_seq.length);
3560 3314338 : DDR_LOOP_NEST (res) = loop_nest;
3561 3314338 : DDR_SELF_REFERENCE (res) = false;
3562 :
3563 7480945 : for (i = 0; i < full_seq.length; ++i)
3564 : {
3565 4166607 : struct subscript *subscript;
3566 :
3567 4166607 : subscript = XNEW (struct subscript);
3568 4166607 : SUB_ACCESS_FN (subscript, 0) = indices_a->access_fns[full_seq.start_a + i];
3569 4166607 : SUB_ACCESS_FN (subscript, 1) = indices_b->access_fns[full_seq.start_b + i];
3570 4166607 : SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
3571 4166607 : SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
3572 4166607 : SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
3573 4166607 : SUB_DISTANCE (subscript) = chrec_dont_know;
3574 4166607 : DDR_SUBSCRIPTS (res).safe_push (subscript);
3575 : }
3576 :
3577 : return res;
3578 : }
3579 :
3580 : /* Initialize a data dependence relation between data accesses A and
3581 : B. NB_LOOPS is the number of loops surrounding the references: the
3582 : size of the classic distance/direction vectors. */
3583 :
3584 : struct data_dependence_relation *
3585 13574339 : initialize_data_dependence_relation (struct data_reference *a,
3586 : struct data_reference *b,
3587 : vec<loop_p> loop_nest)
3588 : {
3589 13574339 : data_dependence_relation *res = XCNEW (struct data_dependence_relation);
3590 13574339 : DDR_A (res) = a;
3591 13574339 : DDR_B (res) = b;
3592 13574339 : DDR_LOOP_NEST (res).create (0);
3593 13574339 : DDR_SUBSCRIPTS (res).create (0);
3594 13574339 : DDR_DIR_VECTS (res).create (0);
3595 13574339 : DDR_DIST_VECTS (res).create (0);
3596 :
3597 13574339 : if (a == NULL || b == NULL)
3598 : {
3599 0 : DDR_ARE_DEPENDENT (res) = chrec_dont_know;
3600 0 : return res;
3601 : }
3602 :
3603 : /* If the data references do not alias, then they are independent. */
3604 20242285 : if (!dr_may_alias_p (a, b, loop_nest.exists () ? loop_nest[0] : NULL))
3605 : {
3606 7141499 : DDR_ARE_DEPENDENT (res) = chrec_known;
3607 7141499 : return res;
3608 : }
3609 :
3610 6432840 : return initialize_data_dependence_relation (res, loop_nest, false);
3611 : }
3612 :
3613 :
3614 : /* Frees memory used by the conflict function F. */
3615 :
3616 : static void
3617 15513918 : free_conflict_function (conflict_function *f)
3618 : {
3619 15513918 : unsigned i;
3620 :
3621 15513918 : if (CF_NONTRIVIAL_P (f))
3622 : {
3623 5112688 : for (i = 0; i < f->n; i++)
3624 2556344 : affine_fn_free (f->fns[i]);
3625 : }
3626 15513918 : free (f);
3627 15513918 : }
3628 :
3629 : /* Frees memory used by SUBSCRIPTS. */
3630 :
3631 : static void
3632 3314338 : free_subscripts (vec<subscript_p> subscripts)
3633 : {
3634 14109621 : for (subscript_p s : subscripts)
3635 : {
3636 4166607 : free_conflict_function (s->conflicting_iterations_in_a);
3637 4166607 : free_conflict_function (s->conflicting_iterations_in_b);
3638 4166607 : free (s);
3639 : }
3640 3314338 : subscripts.release ();
3641 3314338 : }
3642 :
3643 : /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
3644 : description. */
3645 :
3646 : static inline void
3647 2382441 : finalize_ddr_dependent (struct data_dependence_relation *ddr,
3648 : tree chrec)
3649 : {
3650 2382441 : DDR_ARE_DEPENDENT (ddr) = chrec;
3651 2382441 : free_subscripts (DDR_SUBSCRIPTS (ddr));
3652 2382441 : DDR_SUBSCRIPTS (ddr).create (0);
3653 : }
3654 :
3655 : /* The dependence relation DDR cannot be represented by a distance
3656 : vector. */
3657 :
3658 : static inline void
3659 2184 : non_affine_dependence_relation (struct data_dependence_relation *ddr)
3660 : {
3661 2184 : if (dump_file && (dump_flags & TDF_DETAILS))
3662 92 : fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
3663 :
3664 2184 : DDR_AFFINE_P (ddr) = false;
3665 2184 : }
3666 :
3667 :
3668 :
3669 : /* This section contains the classic Banerjee tests. */
3670 :
3671 : /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
3672 : variables, i.e., if the ZIV (Zero Index Variable) test is true. */
3673 :
3674 : static inline bool
3675 2371388 : ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
3676 : {
3677 2371388 : return (evolution_function_is_constant_p (chrec_a)
3678 2897009 : && evolution_function_is_constant_p (chrec_b));
3679 : }
3680 :
3681 : /* Returns true iff CHREC_A and CHREC_B are dependent on an index
3682 : variable, i.e., if the SIV (Single Index Variable) test is true. */
3683 :
3684 : static bool
3685 1847554 : siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
3686 : {
3687 3693324 : if ((evolution_function_is_constant_p (chrec_a)
3688 1787 : && evolution_function_is_univariate_p (chrec_b))
3689 3693324 : || (evolution_function_is_constant_p (chrec_b)
3690 1270 : && evolution_function_is_univariate_p (chrec_a)))
3691 : return true;
3692 :
3693 1844503 : if (evolution_function_is_univariate_p (chrec_a)
3694 1844503 : && evolution_function_is_univariate_p (chrec_b))
3695 : {
3696 1818753 : switch (TREE_CODE (chrec_a))
3697 : {
3698 1818753 : case POLYNOMIAL_CHREC:
3699 1818753 : switch (TREE_CODE (chrec_b))
3700 : {
3701 1818753 : case POLYNOMIAL_CHREC:
3702 1818753 : if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
3703 : return false;
3704 : /* FALLTHRU */
3705 :
3706 1818671 : default:
3707 1818671 : return true;
3708 : }
3709 :
3710 : default:
3711 : return true;
3712 : }
3713 : }
3714 :
3715 : return false;
3716 : }
3717 :
3718 : /* Creates a conflict function with N dimensions. The affine functions
3719 : in each dimension follow. */
3720 :
3721 : static conflict_function *
3722 2556344 : conflict_fn (unsigned n, ...)
3723 : {
3724 2556344 : unsigned i;
3725 2556344 : conflict_function *ret = XCNEW (conflict_function);
3726 2556344 : va_list ap;
3727 :
3728 2556344 : gcc_assert (n > 0 && n <= MAX_DIM);
3729 2556344 : va_start (ap, n);
3730 :
3731 2556344 : ret->n = n;
3732 5112688 : for (i = 0; i < n; i++)
3733 2556344 : ret->fns[i] = va_arg (ap, affine_fn);
3734 2556344 : va_end (ap);
3735 :
3736 2556344 : return ret;
3737 : }
3738 :
3739 : /* Returns constant affine function with value CST. */
3740 :
3741 : static affine_fn
3742 2437602 : affine_fn_cst (tree cst)
3743 : {
3744 2437602 : affine_fn fn;
3745 2437602 : fn.create (1);
3746 2437602 : fn.quick_push (cst);
3747 2437602 : return fn;
3748 : }
3749 :
3750 : /* Returns affine function with single variable, CST + COEF * x_DIM. */
3751 :
3752 : static affine_fn
3753 118742 : affine_fn_univar (tree cst, unsigned dim, tree coef)
3754 : {
3755 118742 : affine_fn fn;
3756 118742 : fn.create (dim + 1);
3757 118742 : unsigned i;
3758 :
3759 118742 : gcc_assert (dim > 0);
3760 118742 : fn.quick_push (cst);
3761 237484 : for (i = 1; i < dim; i++)
3762 0 : fn.quick_push (integer_zero_node);
3763 118742 : fn.quick_push (coef);
3764 118742 : return fn;
3765 : }
3766 :
3767 : /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
3768 : *OVERLAPS_B are initialized to the functions that describe the
3769 : relation between the elements accessed twice by CHREC_A and
3770 : CHREC_B. For k >= 0, the following property is verified:
3771 :
3772 : CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
3773 :
3774 : static void
3775 523834 : analyze_ziv_subscript (tree chrec_a,
3776 : tree chrec_b,
3777 : conflict_function **overlaps_a,
3778 : conflict_function **overlaps_b,
3779 : tree *last_conflicts)
3780 : {
3781 523834 : tree type, difference;
3782 523834 : dependence_stats.num_ziv++;
3783 :
3784 523834 : if (dump_file && (dump_flags & TDF_DETAILS))
3785 22606 : fprintf (dump_file, "(analyze_ziv_subscript \n");
3786 :
3787 523834 : type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
3788 523834 : chrec_a = chrec_convert (type, chrec_a, NULL);
3789 523834 : chrec_b = chrec_convert (type, chrec_b, NULL);
3790 523834 : difference = chrec_fold_minus (type, chrec_a, chrec_b);
3791 :
3792 523834 : switch (TREE_CODE (difference))
3793 : {
3794 523834 : case INTEGER_CST:
3795 523834 : if (integer_zerop (difference))
3796 : {
3797 : /* The difference is equal to zero: the accessed index
3798 : overlaps for each iteration in the loop. */
3799 0 : *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3800 0 : *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3801 0 : *last_conflicts = chrec_dont_know;
3802 0 : dependence_stats.num_ziv_dependent++;
3803 : }
3804 : else
3805 : {
3806 : /* The accesses do not overlap. */
3807 523834 : *overlaps_a = conflict_fn_no_dependence ();
3808 523834 : *overlaps_b = conflict_fn_no_dependence ();
3809 523834 : *last_conflicts = integer_zero_node;
3810 523834 : dependence_stats.num_ziv_independent++;
3811 : }
3812 : break;
3813 :
3814 0 : default:
3815 : /* We're not sure whether the indexes overlap. For the moment,
3816 : conservatively answer "don't know". */
3817 0 : if (dump_file && (dump_flags & TDF_DETAILS))
3818 0 : fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
3819 :
3820 0 : *overlaps_a = conflict_fn_not_known ();
3821 0 : *overlaps_b = conflict_fn_not_known ();
3822 0 : *last_conflicts = chrec_dont_know;
3823 0 : dependence_stats.num_ziv_unimplemented++;
3824 0 : break;
3825 : }
3826 :
3827 523834 : if (dump_file && (dump_flags & TDF_DETAILS))
3828 22606 : fprintf (dump_file, ")\n");
3829 523834 : }
3830 :
3831 : /* Similar to max_stmt_executions_int, but returns the bound as a tree,
3832 : and only if it fits to the int type. If this is not the case, or the
3833 : bound on the number of iterations of LOOP could not be derived, returns
3834 : chrec_dont_know. */
3835 :
3836 : static tree
3837 0 : max_stmt_executions_tree (class loop *loop)
3838 : {
3839 0 : widest_int nit;
3840 :
3841 0 : if (!max_stmt_executions (loop, &nit))
3842 0 : return chrec_dont_know;
3843 :
3844 0 : if (!wi::fits_to_tree_p (nit, unsigned_type_node))
3845 0 : return chrec_dont_know;
3846 :
3847 0 : return wide_int_to_tree (unsigned_type_node, nit);
3848 0 : }
3849 :
3850 : /* Determine whether the CHREC is always positive/negative. If the expression
3851 : cannot be statically analyzed, return false, otherwise set the answer into
3852 : VALUE. */
3853 :
3854 : static bool
3855 4642 : chrec_is_positive (tree chrec, bool *value)
3856 : {
3857 4642 : bool value0, value1, value2;
3858 4642 : tree end_value, nb_iter;
3859 :
3860 4642 : switch (TREE_CODE (chrec))
3861 : {
3862 0 : case POLYNOMIAL_CHREC:
3863 0 : if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
3864 0 : || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
3865 : return false;
3866 :
3867 : /* FIXME -- overflows. */
3868 0 : if (value0 == value1)
3869 : {
3870 0 : *value = value0;
3871 0 : return true;
3872 : }
3873 :
3874 : /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
3875 : and the proof consists in showing that the sign never
3876 : changes during the execution of the loop, from 0 to
3877 : loop->nb_iterations. */
3878 0 : if (!evolution_function_is_affine_p (chrec))
3879 : return false;
3880 :
3881 0 : nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
3882 0 : if (chrec_contains_undetermined (nb_iter))
3883 : return false;
3884 :
3885 : #if 0
3886 : /* TODO -- If the test is after the exit, we may decrease the number of
3887 : iterations by one. */
3888 : if (after_exit)
3889 : nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
3890 : #endif
3891 :
3892 0 : end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
3893 :
3894 0 : if (!chrec_is_positive (end_value, &value2))
3895 : return false;
3896 :
3897 0 : *value = value0;
3898 0 : return value0 == value1;
3899 :
3900 4642 : case INTEGER_CST:
3901 4642 : switch (tree_int_cst_sgn (chrec))
3902 : {
3903 2079 : case -1:
3904 2079 : *value = false;
3905 2079 : break;
3906 2563 : case 1:
3907 2563 : *value = true;
3908 2563 : break;
3909 : default:
3910 : return false;
3911 : }
3912 : return true;
3913 :
3914 : default:
3915 : return false;
3916 : }
3917 : }
3918 :
3919 :
3920 : /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
3921 : constant, and CHREC_B is an affine function. *OVERLAPS_A and
3922 : *OVERLAPS_B are initialized to the functions that describe the
3923 : relation between the elements accessed twice by CHREC_A and
3924 : CHREC_B. For k >= 0, the following property is verified:
3925 :
3926 : CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
3927 :
3928 : static void
3929 3051 : analyze_siv_subscript_cst_affine (tree chrec_a,
3930 : tree chrec_b,
3931 : conflict_function **overlaps_a,
3932 : conflict_function **overlaps_b,
3933 : tree *last_conflicts)
3934 : {
3935 3051 : bool value0, value1, value2;
3936 3051 : tree type, difference, tmp;
3937 :
3938 3051 : type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
3939 3051 : chrec_a = chrec_convert (type, chrec_a, NULL);
3940 3051 : chrec_b = chrec_convert (type, chrec_b, NULL);
3941 3051 : difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
3942 :
3943 : /* Special case overlap in the first iteration. */
3944 3051 : if (integer_zerop (difference))
3945 : {
3946 728 : *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3947 728 : *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3948 728 : *last_conflicts = integer_one_node;
3949 728 : return;
3950 : }
3951 :
3952 2323 : if (!chrec_is_positive (initial_condition (difference), &value0))
3953 : {
3954 0 : if (dump_file && (dump_flags & TDF_DETAILS))
3955 0 : fprintf (dump_file, "siv test failed: chrec is not positive.\n");
3956 :
3957 0 : dependence_stats.num_siv_unimplemented++;
3958 0 : *overlaps_a = conflict_fn_not_known ();
3959 0 : *overlaps_b = conflict_fn_not_known ();
3960 0 : *last_conflicts = chrec_dont_know;
3961 0 : return;
3962 : }
3963 : else
3964 : {
3965 2323 : if (value0 == false)
3966 : {
3967 1865 : if (TREE_CODE (chrec_b) != POLYNOMIAL_CHREC
3968 1865 : || !chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
3969 : {
3970 4 : if (dump_file && (dump_flags & TDF_DETAILS))
3971 0 : fprintf (dump_file, "siv test failed: chrec not positive.\n");
3972 :
3973 4 : *overlaps_a = conflict_fn_not_known ();
3974 4 : *overlaps_b = conflict_fn_not_known ();
3975 4 : *last_conflicts = chrec_dont_know;
3976 4 : dependence_stats.num_siv_unimplemented++;
3977 4 : return;
3978 : }
3979 : else
3980 : {
3981 1861 : if (value1 == true)
3982 : {
3983 : /* Example:
3984 : chrec_a = 12
3985 : chrec_b = {10, +, 1}
3986 : */
3987 :
3988 1861 : if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
3989 : {
3990 1563 : HOST_WIDE_INT numiter;
3991 1563 : class loop *loop = get_chrec_loop (chrec_b);
3992 :
3993 1563 : *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3994 1563 : tmp = fold_build2 (EXACT_DIV_EXPR, type,
3995 : fold_build1 (ABS_EXPR, type, difference),
3996 : CHREC_RIGHT (chrec_b));
3997 1563 : *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
3998 1563 : *last_conflicts = integer_one_node;
3999 :
4000 :
4001 : /* Perform weak-zero siv test to see if overlap is
4002 : outside the loop bounds. */
4003 1563 : numiter = max_stmt_executions_int (loop);
4004 :
4005 1563 : if (numiter >= 0
4006 1563 : && compare_tree_int (tmp, numiter) > 0)
4007 : {
4008 0 : free_conflict_function (*overlaps_a);
4009 0 : free_conflict_function (*overlaps_b);
4010 0 : *overlaps_a = conflict_fn_no_dependence ();
4011 0 : *overlaps_b = conflict_fn_no_dependence ();
4012 0 : *last_conflicts = integer_zero_node;
4013 0 : dependence_stats.num_siv_independent++;
4014 0 : return;
4015 : }
4016 1563 : dependence_stats.num_siv_dependent++;
4017 1563 : return;
4018 : }
4019 :
4020 : /* When the step does not divide the difference, there are
4021 : no overlaps. */
4022 : else
4023 : {
4024 298 : *overlaps_a = conflict_fn_no_dependence ();
4025 298 : *overlaps_b = conflict_fn_no_dependence ();
4026 298 : *last_conflicts = integer_zero_node;
4027 298 : dependence_stats.num_siv_independent++;
4028 298 : return;
4029 : }
4030 : }
4031 :
4032 : else
4033 : {
4034 : /* Example:
4035 : chrec_a = 12
4036 : chrec_b = {10, +, -1}
4037 :
4038 : In this case, chrec_a will not overlap with chrec_b. */
4039 0 : *overlaps_a = conflict_fn_no_dependence ();
4040 0 : *overlaps_b = conflict_fn_no_dependence ();
4041 0 : *last_conflicts = integer_zero_node;
4042 0 : dependence_stats.num_siv_independent++;
4043 0 : return;
4044 : }
4045 : }
4046 : }
4047 : else
4048 : {
4049 458 : if (TREE_CODE (chrec_b) != POLYNOMIAL_CHREC
4050 458 : || !chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
4051 : {
4052 0 : if (dump_file && (dump_flags & TDF_DETAILS))
4053 0 : fprintf (dump_file, "siv test failed: chrec not positive.\n");
4054 :
4055 0 : *overlaps_a = conflict_fn_not_known ();
4056 0 : *overlaps_b = conflict_fn_not_known ();
4057 0 : *last_conflicts = chrec_dont_know;
4058 0 : dependence_stats.num_siv_unimplemented++;
4059 0 : return;
4060 : }
4061 : else
4062 : {
4063 458 : if (value2 == false)
4064 : {
4065 : /* Example:
4066 : chrec_a = 3
4067 : chrec_b = {10, +, -1}
4068 : */
4069 214 : if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
4070 : {
4071 109 : HOST_WIDE_INT numiter;
4072 109 : class loop *loop = get_chrec_loop (chrec_b);
4073 :
4074 109 : *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
4075 109 : tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
4076 : CHREC_RIGHT (chrec_b));
4077 109 : *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
4078 109 : *last_conflicts = integer_one_node;
4079 :
4080 : /* Perform weak-zero siv test to see if overlap is
4081 : outside the loop bounds. */
4082 109 : numiter = max_stmt_executions_int (loop);
4083 :
4084 109 : if (numiter >= 0
4085 109 : && compare_tree_int (tmp, numiter) > 0)
4086 : {
4087 0 : free_conflict_function (*overlaps_a);
4088 0 : free_conflict_function (*overlaps_b);
4089 0 : *overlaps_a = conflict_fn_no_dependence ();
4090 0 : *overlaps_b = conflict_fn_no_dependence ();
4091 0 : *last_conflicts = integer_zero_node;
4092 0 : dependence_stats.num_siv_independent++;
4093 0 : return;
4094 : }
4095 109 : dependence_stats.num_siv_dependent++;
4096 109 : return;
4097 : }
4098 :
4099 : /* When the step does not divide the difference, there
4100 : are no overlaps. */
4101 : else
4102 : {
4103 105 : *overlaps_a = conflict_fn_no_dependence ();
4104 105 : *overlaps_b = conflict_fn_no_dependence ();
4105 105 : *last_conflicts = integer_zero_node;
4106 105 : dependence_stats.num_siv_independent++;
4107 105 : return;
4108 : }
4109 : }
4110 : else
4111 : {
4112 : /* Example:
4113 : chrec_a = 3
4114 : chrec_b = {4, +, 1}
4115 :
4116 : In this case, chrec_a will not overlap with chrec_b. */
4117 244 : *overlaps_a = conflict_fn_no_dependence ();
4118 244 : *overlaps_b = conflict_fn_no_dependence ();
4119 244 : *last_conflicts = integer_zero_node;
4120 244 : dependence_stats.num_siv_independent++;
4121 244 : return;
4122 : }
4123 : }
4124 : }
4125 : }
4126 : }
4127 :
4128 : /* Helper recursive function for initializing the matrix A. Returns
4129 : the initial value of CHREC. */
4130 :
4131 : static tree
4132 3599024 : initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
4133 : {
4134 7198040 : gcc_assert (chrec);
4135 :
4136 7198040 : switch (TREE_CODE (chrec))
4137 : {
4138 3599024 : case POLYNOMIAL_CHREC:
4139 3599024 : HOST_WIDE_INT chrec_right;
4140 3599024 : if (!cst_and_fits_in_hwi (CHREC_RIGHT (chrec)))
4141 8 : return chrec_dont_know;
4142 3599016 : chrec_right = int_cst_value (CHREC_RIGHT (chrec));
4143 : /* We want to be able to negate without overflow. */
4144 3599016 : if (chrec_right == HOST_WIDE_INT_MIN)
4145 0 : return chrec_dont_know;
4146 3599016 : A[index][0] = mult * chrec_right;
4147 3599016 : return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
4148 :
4149 0 : case PLUS_EXPR:
4150 0 : case MULT_EXPR:
4151 0 : case MINUS_EXPR:
4152 0 : {
4153 0 : tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
4154 0 : tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
4155 :
4156 0 : return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
4157 : }
4158 :
4159 0 : CASE_CONVERT:
4160 0 : {
4161 0 : tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
4162 0 : return chrec_convert (chrec_type (chrec), op, NULL);
4163 : }
4164 :
4165 0 : case BIT_NOT_EXPR:
4166 0 : {
4167 : /* Handle ~X as -1 - X. */
4168 0 : tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
4169 0 : return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
4170 0 : build_int_cst (TREE_TYPE (chrec), -1), op);
4171 : }
4172 :
4173 3599016 : case INTEGER_CST:
4174 3599016 : return cst_and_fits_in_hwi (chrec) ? chrec : chrec_dont_know;
4175 :
4176 0 : default:
4177 0 : gcc_unreachable ();
4178 : return NULL_TREE;
4179 : }
4180 : }
4181 :
4182 : #define FLOOR_DIV(x,y) ((x) / (y))
4183 :
4184 : /* Solves the special case of the Diophantine equation:
4185 : | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
4186 :
4187 : Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
4188 : number of iterations that loops X and Y run. The overlaps will be
4189 : constructed as evolutions in dimension DIM. */
4190 :
4191 : static void
4192 64 : compute_overlap_steps_for_affine_univar (HOST_WIDE_INT niter,
4193 : HOST_WIDE_INT step_a,
4194 : HOST_WIDE_INT step_b,
4195 : affine_fn *overlaps_a,
4196 : affine_fn *overlaps_b,
4197 : tree *last_conflicts, int dim)
4198 : {
4199 64 : if (((step_a > 0 && step_b > 0)
4200 8 : || (step_a < 0 && step_b < 0)))
4201 : {
4202 60 : HOST_WIDE_INT step_overlaps_a, step_overlaps_b;
4203 60 : HOST_WIDE_INT gcd_steps_a_b, last_conflict, tau2;
4204 :
4205 60 : gcd_steps_a_b = gcd (step_a, step_b);
4206 60 : step_overlaps_a = step_b / gcd_steps_a_b;
4207 60 : step_overlaps_b = step_a / gcd_steps_a_b;
4208 :
4209 60 : if (niter > 0)
4210 : {
4211 60 : tau2 = FLOOR_DIV (niter, step_overlaps_a);
4212 60 : tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
4213 60 : last_conflict = tau2;
4214 60 : *last_conflicts = build_int_cst (integer_type_node, last_conflict);
4215 : }
4216 : else
4217 0 : *last_conflicts = chrec_dont_know;
4218 :
4219 60 : *overlaps_a = affine_fn_univar (integer_zero_node, dim,
4220 : build_int_cst (integer_type_node,
4221 60 : step_overlaps_a));
4222 60 : *overlaps_b = affine_fn_univar (integer_zero_node, dim,
4223 : build_int_cst (integer_type_node,
4224 60 : step_overlaps_b));
4225 60 : }
4226 :
4227 : else
4228 : {
4229 4 : *overlaps_a = affine_fn_cst (integer_zero_node);
4230 4 : *overlaps_b = affine_fn_cst (integer_zero_node);
4231 4 : *last_conflicts = integer_zero_node;
4232 : }
4233 64 : }
4234 :
4235 : /* Solves the special case of a Diophantine equation where CHREC_A is
4236 : an affine bivariate function, and CHREC_B is an affine univariate
4237 : function. For example,
4238 :
4239 : | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
4240 :
4241 : has the following overlapping functions:
4242 :
4243 : | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
4244 : | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
4245 : | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
4246 :
4247 : FORNOW: This is a specialized implementation for a case occurring in
4248 : a common benchmark. Implement the general algorithm. */
4249 :
4250 : static void
4251 0 : compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
4252 : conflict_function **overlaps_a,
4253 : conflict_function **overlaps_b,
4254 : tree *last_conflicts)
4255 : {
4256 0 : bool xz_p, yz_p, xyz_p;
4257 0 : HOST_WIDE_INT step_x, step_y, step_z;
4258 0 : HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
4259 0 : affine_fn overlaps_a_xz, overlaps_b_xz;
4260 0 : affine_fn overlaps_a_yz, overlaps_b_yz;
4261 0 : affine_fn overlaps_a_xyz, overlaps_b_xyz;
4262 0 : affine_fn ova1, ova2, ovb;
4263 0 : tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
4264 :
4265 0 : step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
4266 0 : step_y = int_cst_value (CHREC_RIGHT (chrec_a));
4267 0 : step_z = int_cst_value (CHREC_RIGHT (chrec_b));
4268 0 : bool overflow_p;
4269 0 : HOST_WIDE_INT step_xy = add_hwi (step_x, step_y, &overflow_p);
4270 0 : if (overflow_p || step_xy == HOST_WIDE_INT_MIN)
4271 : {
4272 0 : if (dump_file && (dump_flags & TDF_DETAILS))
4273 0 : fprintf (dump_file, "overlap steps test failed: out of bound "
4274 : "combined step.\n");
4275 0 : *overlaps_a = conflict_fn_not_known ();
4276 0 : *overlaps_b = conflict_fn_not_known ();
4277 0 : *last_conflicts = chrec_dont_know;
4278 0 : return;
4279 : }
4280 :
4281 0 : niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
4282 0 : niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
4283 0 : niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
4284 :
4285 0 : if (niter_x < 0 || niter_y < 0 || niter_z < 0)
4286 : {
4287 0 : if (dump_file && (dump_flags & TDF_DETAILS))
4288 0 : fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
4289 :
4290 0 : *overlaps_a = conflict_fn_not_known ();
4291 0 : *overlaps_b = conflict_fn_not_known ();
4292 0 : *last_conflicts = chrec_dont_know;
4293 0 : return;
4294 : }
4295 :
4296 0 : niter = MIN (niter_x, niter_z);
4297 0 : compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
4298 : &overlaps_a_xz,
4299 : &overlaps_b_xz,
4300 : &last_conflicts_xz, 1);
4301 0 : niter = MIN (niter_y, niter_z);
4302 0 : compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
4303 : &overlaps_a_yz,
4304 : &overlaps_b_yz,
4305 : &last_conflicts_yz, 2);
4306 0 : niter = MIN (niter_x, niter_z);
4307 0 : niter = MIN (niter_y, niter);
4308 0 : compute_overlap_steps_for_affine_univar (niter, step_xy, step_z,
4309 : &overlaps_a_xyz,
4310 : &overlaps_b_xyz,
4311 : &last_conflicts_xyz, 3);
4312 :
4313 0 : xz_p = !integer_zerop (last_conflicts_xz);
4314 0 : yz_p = !integer_zerop (last_conflicts_yz);
4315 0 : xyz_p = !integer_zerop (last_conflicts_xyz);
4316 :
4317 0 : if (xz_p || yz_p || xyz_p)
4318 : {
4319 0 : ova1 = affine_fn_cst (integer_zero_node);
4320 0 : ova2 = affine_fn_cst (integer_zero_node);
4321 0 : ovb = affine_fn_cst (integer_zero_node);
4322 0 : if (xz_p)
4323 : {
4324 0 : affine_fn t0 = ova1;
4325 0 : affine_fn t2 = ovb;
4326 :
4327 0 : ova1 = affine_fn_plus (ova1, overlaps_a_xz);
4328 0 : ovb = affine_fn_plus (ovb, overlaps_b_xz);
4329 0 : affine_fn_free (t0);
4330 0 : affine_fn_free (t2);
4331 0 : *last_conflicts = last_conflicts_xz;
4332 : }
4333 0 : if (yz_p)
4334 : {
4335 0 : affine_fn t0 = ova2;
4336 0 : affine_fn t2 = ovb;
4337 :
4338 0 : ova2 = affine_fn_plus (ova2, overlaps_a_yz);
4339 0 : ovb = affine_fn_plus (ovb, overlaps_b_yz);
4340 0 : affine_fn_free (t0);
4341 0 : affine_fn_free (t2);
4342 0 : *last_conflicts = last_conflicts_yz;
4343 : }
4344 0 : if (xyz_p)
4345 : {
4346 0 : affine_fn t0 = ova1;
4347 0 : affine_fn t2 = ova2;
4348 0 : affine_fn t4 = ovb;
4349 :
4350 0 : ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
4351 0 : ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
4352 0 : ovb = affine_fn_plus (ovb, overlaps_b_xyz);
4353 0 : affine_fn_free (t0);
4354 0 : affine_fn_free (t2);
4355 0 : affine_fn_free (t4);
4356 0 : *last_conflicts = last_conflicts_xyz;
4357 : }
4358 0 : *overlaps_a = conflict_fn (2, ova1, ova2);
4359 0 : *overlaps_b = conflict_fn (1, ovb);
4360 0 : }
4361 : else
4362 : {
4363 0 : *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
4364 0 : *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
4365 0 : *last_conflicts = integer_zero_node;
4366 : }
4367 :
4368 0 : affine_fn_free (overlaps_a_xz);
4369 0 : affine_fn_free (overlaps_b_xz);
4370 0 : affine_fn_free (overlaps_a_yz);
4371 0 : affine_fn_free (overlaps_b_yz);
4372 0 : affine_fn_free (overlaps_a_xyz);
4373 0 : affine_fn_free (overlaps_b_xyz);
4374 : }
4375 :
4376 : /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
4377 :
4378 : static void
4379 3645635 : lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
4380 : int size)
4381 : {
4382 3645635 : memcpy (vec2, vec1, size * sizeof (*vec1));
4383 0 : }
4384 :
4385 : /* Copy the elements of M x N matrix MAT1 to MAT2. */
4386 :
4387 : static void
4388 1799436 : lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
4389 : int m, int n)
4390 : {
4391 1799436 : int i;
4392 :
4393 5398308 : for (i = 0; i < m; i++)
4394 3598872 : lambda_vector_copy (mat1[i], mat2[i], n);
4395 1799436 : }
4396 :
4397 : /* Store the N x N identity matrix in MAT. */
4398 :
4399 : static void
4400 1799436 : lambda_matrix_id (lambda_matrix mat, int size)
4401 : {
4402 1799436 : int i, j;
4403 :
4404 5398308 : for (i = 0; i < size; i++)
4405 10796616 : for (j = 0; j < size; j++)
4406 10796616 : mat[i][j] = (i == j) ? 1 : 0;
4407 1799436 : }
4408 :
4409 : /* Return the index of the first nonzero element of vector VEC1 between
4410 : START and N. We must have START <= N.
4411 : Returns N if VEC1 is the zero vector. */
4412 :
4413 : static int
4414 1799436 : lambda_vector_first_nz (lambda_vector vec1, int n, int start)
4415 : {
4416 1799436 : int j = start;
4417 1799436 : while (j < n && vec1[j] == 0)
4418 0 : j++;
4419 1799436 : return j;
4420 : }
4421 :
4422 : /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
4423 : R2 = R2 + CONST1 * R1. */
4424 :
4425 : static bool
4426 3599146 : lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2,
4427 : lambda_int const1)
4428 : {
4429 3599146 : int i;
4430 :
4431 3599146 : if (const1 == 0)
4432 : return true;
4433 :
4434 8997270 : for (i = 0; i < n; i++)
4435 : {
4436 5398362 : bool ovf;
4437 5398362 : lambda_int tem = mul_hwi (mat[r1][i], const1, &ovf);
4438 5398362 : if (ovf)
4439 3599146 : return false;
4440 5398362 : lambda_int tem2 = add_hwi (mat[r2][i], tem, &ovf);
4441 5398362 : if (ovf || tem2 == HOST_WIDE_INT_MIN)
4442 : return false;
4443 5398362 : mat[r2][i] = tem2;
4444 : }
4445 :
4446 : return true;
4447 : }
4448 :
4449 : /* Multiply vector VEC1 of length SIZE by a constant CONST1,
4450 : and store the result in VEC2. */
4451 :
4452 : static void
4453 1789034 : lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
4454 : int size, lambda_int const1)
4455 : {
4456 1789034 : int i;
4457 :
4458 1789034 : if (const1 == 0)
4459 0 : lambda_vector_clear (vec2, size);
4460 : else
4461 5367102 : for (i = 0; i < size; i++)
4462 3578068 : vec2[i] = const1 * vec1[i];
4463 1789034 : }
4464 :
4465 : /* Negate vector VEC1 with length SIZE and store it in VEC2. */
4466 :
4467 : static void
4468 1789034 : lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
4469 : int size)
4470 : {
4471 0 : lambda_vector_mult_const (vec1, vec2, size, -1);
4472 0 : }
4473 :
4474 : /* Negate row R1 of matrix MAT which has N columns. */
4475 :
4476 : static void
4477 1789034 : lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
4478 : {
4479 0 : lambda_vector_negate (mat[r1], mat[r1], n);
4480 0 : }
4481 :
4482 : /* Return true if two vectors are equal. */
4483 :
4484 : static bool
4485 378392 : lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
4486 : {
4487 378392 : int i;
4488 384968 : for (i = 0; i < size; i++)
4489 383229 : if (vec1[i] != vec2[i])
4490 : return false;
4491 : return true;
4492 : }
4493 :
4494 : /* Given an M x N integer matrix A, this function determines an M x
4495 : M unimodular matrix U, and an M x N echelon matrix S such that
4496 : "U.A = S". This decomposition is also known as "right Hermite".
4497 :
4498 : Ref: Algorithm 2.1 page 33 in "Loop Transformations for
4499 : Restructuring Compilers" Utpal Banerjee. */
4500 :
4501 : static bool
4502 1799436 : lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
4503 : lambda_matrix S, lambda_matrix U)
4504 : {
4505 1799436 : int i, j, i0 = 0;
4506 :
4507 1799436 : lambda_matrix_copy (A, S, m, n);
4508 1799436 : lambda_matrix_id (U, m);
4509 :
4510 5398308 : for (j = 0; j < n; j++)
4511 : {
4512 3598872 : if (lambda_vector_first_nz (S[j], m, i0) < m)
4513 : {
4514 1799436 : ++i0;
4515 3598872 : for (i = m - 1; i >= i0; i--)
4516 : {
4517 3599009 : while (S[i][j] != 0)
4518 : {
4519 1799573 : lambda_int factor, a, b;
4520 :
4521 1799573 : a = S[i-1][j];
4522 1799573 : b = S[i][j];
4523 1799573 : gcc_assert (a != HOST_WIDE_INT_MIN);
4524 1799573 : factor = a / b;
4525 :
4526 1799573 : if (!lambda_matrix_row_add (S, n, i, i-1, -factor))
4527 : return false;
4528 1799573 : std::swap (S[i], S[i-1]);
4529 :
4530 1799573 : if (!lambda_matrix_row_add (U, m, i, i-1, -factor))
4531 : return false;
4532 1799573 : std::swap (U[i], U[i-1]);
4533 : }
4534 : }
4535 : }
4536 : }
4537 :
4538 : return true;
4539 : }
4540 :
4541 : /* Determines the overlapping elements due to accesses CHREC_A and
4542 : CHREC_B, that are affine functions. This function cannot handle
4543 : symbolic evolution functions, ie. when initial conditions are
4544 : parameters, because it uses lambda matrices of integers. */
4545 :
4546 : static void
4547 1799512 : analyze_subscript_affine_affine (tree chrec_a,
4548 : tree chrec_b,
4549 : conflict_function **overlaps_a,
4550 : conflict_function **overlaps_b,
4551 : tree *last_conflicts)
4552 : {
4553 1799512 : unsigned nb_vars_a, nb_vars_b, dim;
4554 1799512 : lambda_int gamma, gcd_alpha_beta;
4555 1799512 : lambda_matrix A, U, S;
4556 1799512 : struct obstack scratch_obstack;
4557 :
4558 1799512 : if (eq_evolutions_p (chrec_a, chrec_b))
4559 : {
4560 : /* The accessed index overlaps for each iteration in the
4561 : loop. */
4562 0 : *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
4563 0 : *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
4564 0 : *last_conflicts = chrec_dont_know;
4565 0 : return;
4566 : }
4567 1799512 : if (dump_file && (dump_flags & TDF_DETAILS))
4568 22257 : fprintf (dump_file, "(analyze_subscript_affine_affine \n");
4569 :
4570 : /* For determining the initial intersection, we have to solve a
4571 : Diophantine equation. This is the most time consuming part.
4572 :
4573 : For answering to the question: "Is there a dependence?" we have
4574 : to prove that there exists a solution to the Diophantine
4575 : equation, and that the solution is in the iteration domain,
4576 : i.e. the solution is positive or zero, and that the solution
4577 : happens before the upper bound loop.nb_iterations. Otherwise
4578 : there is no dependence. This function outputs a description of
4579 : the iterations that hold the intersections. */
4580 :
4581 1799512 : nb_vars_a = nb_vars_in_chrec (chrec_a);
4582 1799512 : nb_vars_b = nb_vars_in_chrec (chrec_b);
4583 :
4584 1799512 : gcc_obstack_init (&scratch_obstack);
4585 :
4586 1799512 : dim = nb_vars_a + nb_vars_b;
4587 1799512 : U = lambda_matrix_new (dim, dim, &scratch_obstack);
4588 1799512 : A = lambda_matrix_new (dim, 1, &scratch_obstack);
4589 1799512 : S = lambda_matrix_new (dim, 1, &scratch_obstack);
4590 :
4591 1799512 : tree init_a = initialize_matrix_A (A, chrec_a, 0, 1);
4592 1799512 : tree init_b = initialize_matrix_A (A, chrec_b, nb_vars_a, -1);
4593 1799512 : if (init_a == chrec_dont_know
4594 1799500 : || init_b == chrec_dont_know)
4595 : {
4596 12 : if (dump_file && (dump_flags & TDF_DETAILS))
4597 0 : fprintf (dump_file, "affine-affine test failed: "
4598 : "representation issue.\n");
4599 12 : *overlaps_a = conflict_fn_not_known ();
4600 12 : *overlaps_b = conflict_fn_not_known ();
4601 12 : *last_conflicts = chrec_dont_know;
4602 12 : goto end_analyze_subs_aa;
4603 : }
4604 1799500 : gamma = int_cst_value (init_b) - int_cst_value (init_a);
4605 :
4606 : /* Don't do all the hard work of solving the Diophantine equation
4607 : when we already know the solution: for example,
4608 : | {3, +, 1}_1
4609 : | {3, +, 4}_2
4610 : | gamma = 3 - 3 = 0.
4611 : Then the first overlap occurs during the first iterations:
4612 : | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
4613 : */
4614 1799500 : if (gamma == 0)
4615 : {
4616 64 : if (nb_vars_a == 1 && nb_vars_b == 1)
4617 : {
4618 64 : HOST_WIDE_INT step_a, step_b;
4619 64 : HOST_WIDE_INT niter, niter_a, niter_b;
4620 64 : affine_fn ova, ovb;
4621 :
4622 64 : niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
4623 64 : niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
4624 64 : niter = MIN (niter_a, niter_b);
4625 64 : step_a = int_cst_value (CHREC_RIGHT (chrec_a));
4626 64 : step_b = int_cst_value (CHREC_RIGHT (chrec_b));
4627 :
4628 64 : compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
4629 : &ova, &ovb,
4630 : last_conflicts, 1);
4631 64 : *overlaps_a = conflict_fn (1, ova);
4632 64 : *overlaps_b = conflict_fn (1, ovb);
4633 : }
4634 :
4635 0 : else if (nb_vars_a == 2 && nb_vars_b == 1)
4636 0 : compute_overlap_steps_for_affine_1_2
4637 0 : (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
4638 :
4639 0 : else if (nb_vars_a == 1 && nb_vars_b == 2)
4640 0 : compute_overlap_steps_for_affine_1_2
4641 0 : (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
4642 :
4643 : else
4644 : {
4645 0 : if (dump_file && (dump_flags & TDF_DETAILS))
4646 0 : fprintf (dump_file, "affine-affine test failed: too many variables.\n");
4647 0 : *overlaps_a = conflict_fn_not_known ();
4648 0 : *overlaps_b = conflict_fn_not_known ();
4649 0 : *last_conflicts = chrec_dont_know;
4650 : }
4651 64 : goto end_analyze_subs_aa;
4652 : }
4653 :
4654 : /* U.A = S */
4655 1799436 : if (!lambda_matrix_right_hermite (A, dim, 1, S, U))
4656 : {
4657 0 : *overlaps_a = conflict_fn_not_known ();
4658 0 : *overlaps_b = conflict_fn_not_known ();
4659 0 : *last_conflicts = chrec_dont_know;
4660 0 : goto end_analyze_subs_aa;
4661 : }
4662 :
4663 1799436 : if (S[0][0] < 0)
4664 : {
4665 1789034 : S[0][0] *= -1;
4666 1789034 : lambda_matrix_row_negate (U, dim, 0);
4667 : }
4668 1799436 : gcd_alpha_beta = S[0][0];
4669 :
4670 : /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
4671 : but that is a quite strange case. Instead of ICEing, answer
4672 : don't know. */
4673 1799436 : if (gcd_alpha_beta == 0)
4674 : {
4675 0 : *overlaps_a = conflict_fn_not_known ();
4676 0 : *overlaps_b = conflict_fn_not_known ();
4677 0 : *last_conflicts = chrec_dont_know;
4678 0 : goto end_analyze_subs_aa;
4679 : }
4680 :
4681 : /* The classic "gcd-test". */
4682 1799436 : if (!int_divides_p (gcd_alpha_beta, gamma))
4683 : {
4684 : /* The "gcd-test" has determined that there is no integer
4685 : solution, i.e. there is no dependence. */
4686 1684197 : *overlaps_a = conflict_fn_no_dependence ();
4687 1684197 : *overlaps_b = conflict_fn_no_dependence ();
4688 1684197 : *last_conflicts = integer_zero_node;
4689 : }
4690 :
4691 : /* Both access functions are univariate. This includes SIV and MIV cases. */
4692 115239 : else if (nb_vars_a == 1 && nb_vars_b == 1)
4693 : {
4694 : /* Both functions should have the same evolution sign. */
4695 115239 : if (((A[0][0] > 0 && -A[1][0] > 0)
4696 5852 : || (A[0][0] < 0 && -A[1][0] < 0)))
4697 : {
4698 : /* The solutions are given by:
4699 : |
4700 : | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
4701 : | [u21 u22] [y0]
4702 :
4703 : For a given integer t. Using the following variables,
4704 :
4705 : | i0 = u11 * gamma / gcd_alpha_beta
4706 : | j0 = u12 * gamma / gcd_alpha_beta
4707 : | i1 = u21
4708 : | j1 = u22
4709 :
4710 : the solutions are:
4711 :
4712 : | x0 = i0 + i1 * t,
4713 : | y0 = j0 + j1 * t. */
4714 114832 : HOST_WIDE_INT i0, j0, i1, j1;
4715 :
4716 114832 : i0 = U[0][0] * gamma / gcd_alpha_beta;
4717 114832 : j0 = U[0][1] * gamma / gcd_alpha_beta;
4718 114832 : i1 = U[1][0];
4719 114832 : j1 = U[1][1];
4720 :
4721 114832 : if ((i1 == 0 && i0 < 0)
4722 114832 : || (j1 == 0 && j0 < 0))
4723 : {
4724 : /* There is no solution.
4725 : FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
4726 : falls in here, but for the moment we don't look at the
4727 : upper bound of the iteration domain. */
4728 0 : *overlaps_a = conflict_fn_no_dependence ();
4729 0 : *overlaps_b = conflict_fn_no_dependence ();
4730 0 : *last_conflicts = integer_zero_node;
4731 55521 : goto end_analyze_subs_aa;
4732 : }
4733 :
4734 114832 : if (i1 > 0 && j1 > 0)
4735 : {
4736 114832 : HOST_WIDE_INT niter_a
4737 114832 : = max_stmt_executions_int (get_chrec_loop (chrec_a));
4738 114832 : HOST_WIDE_INT niter_b
4739 114832 : = max_stmt_executions_int (get_chrec_loop (chrec_b));
4740 114832 : HOST_WIDE_INT niter = MIN (niter_a, niter_b);
4741 :
4742 : /* (X0, Y0) is a solution of the Diophantine equation:
4743 : "chrec_a (X0) = chrec_b (Y0)". */
4744 114832 : HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
4745 : CEIL (-j0, j1));
4746 114832 : HOST_WIDE_INT x0 = i1 * tau1 + i0;
4747 114832 : HOST_WIDE_INT y0 = j1 * tau1 + j0;
4748 :
4749 : /* (X1, Y1) is the smallest positive solution of the eq
4750 : "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
4751 : first conflict occurs. */
4752 114832 : HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
4753 114832 : HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
4754 114832 : HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
4755 :
4756 114832 : if (niter > 0)
4757 : {
4758 : /* If the overlap occurs outside of the bounds of the
4759 : loop, there is no dependence. */
4760 108249 : if (x1 >= niter_a || y1 >= niter_b)
4761 : {
4762 55521 : *overlaps_a = conflict_fn_no_dependence ();
4763 55521 : *overlaps_b = conflict_fn_no_dependence ();
4764 55521 : *last_conflicts = integer_zero_node;
4765 55521 : goto end_analyze_subs_aa;
4766 : }
4767 :
4768 : /* max stmt executions can get quite large, avoid
4769 : overflows by using wide ints here. */
4770 52728 : widest_int tau2
4771 105456 : = wi::smin (wi::sdiv_floor (wi::sub (niter_a, i0), i1),
4772 158184 : wi::sdiv_floor (wi::sub (niter_b, j0), j1));
4773 52728 : widest_int last_conflict = wi::sub (tau2, (x1 - i0)/i1);
4774 52728 : if (wi::min_precision (last_conflict, SIGNED)
4775 52728 : <= TYPE_PRECISION (integer_type_node))
4776 47782 : *last_conflicts
4777 47782 : = build_int_cst (integer_type_node,
4778 47782 : last_conflict.to_shwi ());
4779 : else
4780 4946 : *last_conflicts = chrec_dont_know;
4781 52728 : }
4782 : else
4783 6583 : *last_conflicts = chrec_dont_know;
4784 :
4785 59311 : *overlaps_a
4786 59311 : = conflict_fn (1,
4787 59311 : affine_fn_univar (build_int_cst (integer_type_node, x1),
4788 : 1,
4789 59311 : build_int_cst (integer_type_node, i1)));
4790 59311 : *overlaps_b
4791 59311 : = conflict_fn (1,
4792 59311 : affine_fn_univar (build_int_cst (integer_type_node, y1),
4793 : 1,
4794 59311 : build_int_cst (integer_type_node, j1)));
4795 59311 : }
4796 : else
4797 : {
4798 : /* FIXME: For the moment, the upper bound of the
4799 : iteration domain for i and j is not checked. */
4800 0 : if (dump_file && (dump_flags & TDF_DETAILS))
4801 0 : fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
4802 0 : *overlaps_a = conflict_fn_not_known ();
4803 0 : *overlaps_b = conflict_fn_not_known ();
4804 0 : *last_conflicts = chrec_dont_know;
4805 : }
4806 59311 : }
4807 : else
4808 : {
4809 407 : if (dump_file && (dump_flags & TDF_DETAILS))
4810 19 : fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
4811 407 : *overlaps_a = conflict_fn_not_known ();
4812 407 : *overlaps_b = conflict_fn_not_known ();
4813 407 : *last_conflicts = chrec_dont_know;
4814 : }
4815 : }
4816 : else
4817 : {
4818 0 : if (dump_file && (dump_flags & TDF_DETAILS))
4819 0 : fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
4820 0 : *overlaps_a = conflict_fn_not_known ();
4821 0 : *overlaps_b = conflict_fn_not_known ();
4822 0 : *last_conflicts = chrec_dont_know;
4823 : }
4824 :
4825 1799512 : end_analyze_subs_aa:
4826 1799512 : obstack_free (&scratch_obstack, NULL);
4827 1799512 : if (dump_file && (dump_flags & TDF_DETAILS))
4828 : {
4829 22257 : fprintf (dump_file, " (overlaps_a = ");
4830 22257 : dump_conflict_function (dump_file, *overlaps_a);
4831 22257 : fprintf (dump_file, ")\n (overlaps_b = ");
4832 22257 : dump_conflict_function (dump_file, *overlaps_b);
4833 22257 : fprintf (dump_file, "))\n");
4834 : }
4835 : }
4836 :
4837 : /* Returns true when analyze_subscript_affine_affine can be used for
4838 : determining the dependence relation between chrec_a and chrec_b,
4839 : that contain symbols. This function modifies chrec_a and chrec_b
4840 : such that the analysis result is the same, and such that they don't
4841 : contain symbols, and then can safely be passed to the analyzer.
4842 :
4843 : Example: The analysis of the following tuples of evolutions produce
4844 : the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
4845 : vs. {0, +, 1}_1
4846 :
4847 : {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
4848 : {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
4849 : */
4850 :
4851 : static bool
4852 43536 : can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
4853 : {
4854 43536 : tree diff, type, left_a, left_b, right_b;
4855 :
4856 43536 : if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
4857 43536 : || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
4858 : /* FIXME: For the moment not handled. Might be refined later. */
4859 : return false;
4860 :
4861 29734 : type = chrec_type (*chrec_a);
4862 29734 : left_a = CHREC_LEFT (*chrec_a);
4863 29734 : left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
4864 29734 : diff = chrec_fold_minus (type, left_a, left_b);
4865 :
4866 59468 : if (!evolution_function_is_constant_p (diff))
4867 : return false;
4868 :
4869 24297 : if (dump_file && (dump_flags & TDF_DETAILS))
4870 105 : fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
4871 :
4872 24297 : *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
4873 24297 : diff, CHREC_RIGHT (*chrec_a));
4874 24297 : right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
4875 24297 : *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
4876 : build_int_cst (type, 0),
4877 : right_b);
4878 24297 : return true;
4879 : }
4880 :
4881 : /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
4882 : *OVERLAPS_B are initialized to the functions that describe the
4883 : relation between the elements accessed twice by CHREC_A and
4884 : CHREC_B. For k >= 0, the following property is verified:
4885 :
4886 : CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
4887 :
4888 : static void
4889 1821722 : analyze_siv_subscript (tree chrec_a,
4890 : tree chrec_b,
4891 : conflict_function **overlaps_a,
4892 : conflict_function **overlaps_b,
4893 : tree *last_conflicts,
4894 : int loop_nest_num)
4895 : {
4896 1821722 : dependence_stats.num_siv++;
4897 :
4898 1821722 : if (dump_file && (dump_flags & TDF_DETAILS))
4899 25388 : fprintf (dump_file, "(analyze_siv_subscript \n");
4900 :
4901 1821722 : if (evolution_function_is_constant_p (chrec_a)
4902 1821722 : && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
4903 1784 : analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
4904 : overlaps_a, overlaps_b, last_conflicts);
4905 :
4906 1819938 : else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
4907 3639876 : && evolution_function_is_constant_p (chrec_b))
4908 1267 : analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
4909 : overlaps_b, overlaps_a, last_conflicts);
4910 :
4911 1818671 : else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
4912 1818671 : && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
4913 : {
4914 1818671 : if (!chrec_contains_symbols (chrec_a)
4915 1818671 : && !chrec_contains_symbols (chrec_b))
4916 : {
4917 1775135 : analyze_subscript_affine_affine (chrec_a, chrec_b,
4918 : overlaps_a, overlaps_b,
4919 : last_conflicts);
4920 :
4921 1775135 : if (CF_NOT_KNOWN_P (*overlaps_a)
4922 1774736 : || CF_NOT_KNOWN_P (*overlaps_b))
4923 399 : dependence_stats.num_siv_unimplemented++;
4924 1774736 : else if (CF_NO_DEPENDENCE_P (*overlaps_a)
4925 58441 : || CF_NO_DEPENDENCE_P (*overlaps_b))
4926 1716295 : dependence_stats.num_siv_independent++;
4927 : else
4928 58441 : dependence_stats.num_siv_dependent++;
4929 : }
4930 43536 : else if (can_use_analyze_subscript_affine_affine (&chrec_a,
4931 : &chrec_b))
4932 : {
4933 24297 : analyze_subscript_affine_affine (chrec_a, chrec_b,
4934 : overlaps_a, overlaps_b,
4935 : last_conflicts);
4936 :
4937 24297 : if (CF_NOT_KNOWN_P (*overlaps_a)
4938 24281 : || CF_NOT_KNOWN_P (*overlaps_b))
4939 16 : dependence_stats.num_siv_unimplemented++;
4940 24281 : else if (CF_NO_DEPENDENCE_P (*overlaps_a)
4941 872 : || CF_NO_DEPENDENCE_P (*overlaps_b))
4942 23409 : dependence_stats.num_siv_independent++;
4943 : else
4944 872 : dependence_stats.num_siv_dependent++;
4945 : }
4946 : else
4947 19239 : goto siv_subscript_dontknow;
4948 : }
4949 :
4950 : else
4951 : {
4952 19239 : siv_subscript_dontknow:;
4953 19239 : if (dump_file && (dump_flags & TDF_DETAILS))
4954 2946 : fprintf (dump_file, " siv test failed: unimplemented");
4955 19239 : *overlaps_a = conflict_fn_not_known ();
4956 19239 : *overlaps_b = conflict_fn_not_known ();
4957 19239 : *last_conflicts = chrec_dont_know;
4958 19239 : dependence_stats.num_siv_unimplemented++;
4959 : }
4960 :
4961 1821722 : if (dump_file && (dump_flags & TDF_DETAILS))
4962 25388 : fprintf (dump_file, ")\n");
4963 1821722 : }
4964 :
4965 : /* Returns false if we can prove that the greatest common divisor of the steps
4966 : of CHREC does not divide CST, false otherwise. */
4967 :
4968 : static bool
4969 20762 : gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
4970 : {
4971 20762 : HOST_WIDE_INT cd = 0, val;
4972 20762 : tree step;
4973 :
4974 20762 : if (!tree_fits_shwi_p (cst))
4975 : return true;
4976 20762 : val = tree_to_shwi (cst);
4977 :
4978 62138 : while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
4979 : {
4980 41522 : step = CHREC_RIGHT (chrec);
4981 41522 : if (!tree_fits_shwi_p (step))
4982 : return true;
4983 41376 : HOST_WIDE_INT step_hwi = tree_to_shwi (step);
4984 41376 : if (step_hwi == HOST_WIDE_INT_MIN)
4985 : return true;
4986 41376 : cd = gcd (cd, step_hwi);
4987 41376 : chrec = CHREC_LEFT (chrec);
4988 : }
4989 :
4990 20616 : return val % cd == 0;
4991 : }
4992 :
4993 : /* Analyze a MIV (Multiple Index Variable) subscript with respect to
4994 : LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
4995 : functions that describe the relation between the elements accessed
4996 : twice by CHREC_A and CHREC_B. For k >= 0, the following property
4997 : is verified:
4998 :
4999 : CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
5000 :
5001 : static void
5002 25832 : analyze_miv_subscript (tree chrec_a,
5003 : tree chrec_b,
5004 : conflict_function **overlaps_a,
5005 : conflict_function **overlaps_b,
5006 : tree *last_conflicts,
5007 : class loop *loop_nest)
5008 : {
5009 25832 : tree type, difference;
5010 :
5011 25832 : dependence_stats.num_miv++;
5012 25832 : if (dump_file && (dump_flags & TDF_DETAILS))
5013 30 : fprintf (dump_file, "(analyze_miv_subscript \n");
5014 :
5015 25832 : type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
5016 25832 : chrec_a = chrec_convert (type, chrec_a, NULL);
5017 25832 : chrec_b = chrec_convert (type, chrec_b, NULL);
5018 25832 : difference = chrec_fold_minus (type, chrec_a, chrec_b);
5019 :
5020 25832 : if (eq_evolutions_p (chrec_a, chrec_b))
5021 : {
5022 : /* Access functions are the same: all the elements are accessed
5023 : in the same order. */
5024 0 : *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
5025 0 : *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
5026 0 : *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
5027 0 : dependence_stats.num_miv_dependent++;
5028 : }
5029 :
5030 25832 : else if (evolution_function_is_constant_p (difference)
5031 20792 : && evolution_function_is_affine_multivariate_p (chrec_a,
5032 : loop_nest->num)
5033 46594 : && !gcd_of_steps_may_divide_p (chrec_a, difference))
5034 : {
5035 : /* testsuite/.../ssa-chrec-33.c
5036 : {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
5037 :
5038 : The difference is 1, and all the evolution steps are multiples
5039 : of 2, consequently there are no overlapping elements. */
5040 19770 : *overlaps_a = conflict_fn_no_dependence ();
5041 19770 : *overlaps_b = conflict_fn_no_dependence ();
5042 19770 : *last_conflicts = integer_zero_node;
5043 19770 : dependence_stats.num_miv_independent++;
5044 : }
5045 :
5046 6062 : else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest->num)
5047 122 : && !chrec_contains_symbols (chrec_a, loop_nest)
5048 110 : && evolution_function_is_affine_in_loop (chrec_b, loop_nest->num)
5049 6142 : && !chrec_contains_symbols (chrec_b, loop_nest))
5050 : {
5051 : /* testsuite/.../ssa-chrec-35.c
5052 : {0, +, 1}_2 vs. {0, +, 1}_3
5053 : the overlapping elements are respectively located at iterations:
5054 : {0, +, 1}_x and {0, +, 1}_x,
5055 : in other words, we have the equality:
5056 : {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
5057 :
5058 : Other examples:
5059 : {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
5060 : {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
5061 :
5062 : {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
5063 : {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
5064 : */
5065 80 : analyze_subscript_affine_affine (chrec_a, chrec_b,
5066 : overlaps_a, overlaps_b, last_conflicts);
5067 :
5068 80 : if (CF_NOT_KNOWN_P (*overlaps_a)
5069 76 : || CF_NOT_KNOWN_P (*overlaps_b))
5070 4 : dependence_stats.num_miv_unimplemented++;
5071 76 : else if (CF_NO_DEPENDENCE_P (*overlaps_a)
5072 62 : || CF_NO_DEPENDENCE_P (*overlaps_b))
5073 14 : dependence_stats.num_miv_independent++;
5074 : else
5075 62 : dependence_stats.num_miv_dependent++;
5076 : }
5077 :
5078 : else
5079 : {
5080 : /* When the analysis is too difficult, answer "don't know". */
5081 5982 : if (dump_file && (dump_flags & TDF_DETAILS))
5082 26 : fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
5083 :
5084 5982 : *overlaps_a = conflict_fn_not_known ();
5085 5982 : *overlaps_b = conflict_fn_not_known ();
5086 5982 : *last_conflicts = chrec_dont_know;
5087 5982 : dependence_stats.num_miv_unimplemented++;
5088 : }
5089 :
5090 25832 : if (dump_file && (dump_flags & TDF_DETAILS))
5091 30 : fprintf (dump_file, ")\n");
5092 25832 : }
5093 :
5094 : /* Determines the iterations for which CHREC_A is equal to CHREC_B in
5095 : with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
5096 : OVERLAP_ITERATIONS_B are initialized with two functions that
5097 : describe the iterations that contain conflicting elements.
5098 :
5099 : Remark: For an integer k >= 0, the following equality is true:
5100 :
5101 : CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
5102 : */
5103 :
5104 : static void
5105 3590352 : analyze_overlapping_iterations (tree chrec_a,
5106 : tree chrec_b,
5107 : conflict_function **overlap_iterations_a,
5108 : conflict_function **overlap_iterations_b,
5109 : tree *last_conflicts, class loop *loop_nest)
5110 : {
5111 3590352 : unsigned int lnn = loop_nest->num;
5112 :
5113 3590352 : dependence_stats.num_subscript_tests++;
5114 :
5115 3590352 : if (dump_file && (dump_flags & TDF_DETAILS))
5116 : {
5117 61979 : fprintf (dump_file, "(analyze_overlapping_iterations \n");
5118 61979 : fprintf (dump_file, " (chrec_a = ");
5119 61979 : print_generic_expr (dump_file, chrec_a);
5120 61979 : fprintf (dump_file, ")\n (chrec_b = ");
5121 61979 : print_generic_expr (dump_file, chrec_b);
5122 61979 : fprintf (dump_file, ")\n");
5123 : }
5124 :
5125 3590352 : if (chrec_a == NULL_TREE
5126 3590352 : || chrec_b == NULL_TREE
5127 3590352 : || chrec_contains_undetermined (chrec_a)
5128 7180704 : || chrec_contains_undetermined (chrec_b))
5129 : {
5130 0 : dependence_stats.num_subscript_undetermined++;
5131 :
5132 0 : *overlap_iterations_a = conflict_fn_not_known ();
5133 0 : *overlap_iterations_b = conflict_fn_not_known ();
5134 : }
5135 :
5136 : /* If they are the same chrec, and are affine, they overlap
5137 : on every iteration. */
5138 3590352 : else if (eq_evolutions_p (chrec_a, chrec_b)
5139 3590352 : && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
5140 497954 : || operand_equal_p (chrec_a, chrec_b, 0)))
5141 : {
5142 1216397 : dependence_stats.num_same_subscript_function++;
5143 1216397 : *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
5144 1216397 : *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
5145 1216397 : *last_conflicts = chrec_dont_know;
5146 : }
5147 :
5148 : /* If they aren't the same, and aren't affine, we can't do anything
5149 : yet. */
5150 2373955 : else if ((chrec_contains_symbols (chrec_a)
5151 2324786 : || chrec_contains_symbols (chrec_b))
5152 2374792 : && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
5153 47737 : || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
5154 : {
5155 2567 : dependence_stats.num_subscript_undetermined++;
5156 2567 : *overlap_iterations_a = conflict_fn_not_known ();
5157 2567 : *overlap_iterations_b = conflict_fn_not_known ();
5158 : }
5159 :
5160 2371388 : else if (ziv_subscript_p (chrec_a, chrec_b))
5161 523834 : analyze_ziv_subscript (chrec_a, chrec_b,
5162 : overlap_iterations_a, overlap_iterations_b,
5163 : last_conflicts);
5164 :
5165 1847554 : else if (siv_subscript_p (chrec_a, chrec_b))
5166 1821722 : analyze_siv_subscript (chrec_a, chrec_b,
5167 : overlap_iterations_a, overlap_iterations_b,
5168 : last_conflicts, lnn);
5169 :
5170 : else
5171 25832 : analyze_miv_subscript (chrec_a, chrec_b,
5172 : overlap_iterations_a, overlap_iterations_b,
5173 : last_conflicts, loop_nest);
5174 :
5175 3590352 : if (dump_file && (dump_flags & TDF_DETAILS))
5176 : {
5177 61979 : fprintf (dump_file, " (overlap_iterations_a = ");
5178 61979 : dump_conflict_function (dump_file, *overlap_iterations_a);
5179 61979 : fprintf (dump_file, ")\n (overlap_iterations_b = ");
5180 61979 : dump_conflict_function (dump_file, *overlap_iterations_b);
5181 61979 : fprintf (dump_file, "))\n");
5182 : }
5183 3590352 : }
5184 :
5185 : /* Helper function for uniquely inserting distance vectors. */
5186 :
5187 : static void
5188 1117328 : save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
5189 : {
5190 1682271 : for (lambda_vector v : DDR_DIST_VECTS (ddr))
5191 574356 : if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
5192 : return;
5193 :
5194 1115589 : DDR_DIST_VECTS (ddr).safe_push (dist_v);
5195 : }
5196 :
5197 : /* Helper function for uniquely inserting direction vectors. */
5198 :
5199 : static void
5200 1112956 : save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
5201 : {
5202 1671372 : for (lambda_vector v : DDR_DIR_VECTS (ddr))
5203 560820 : if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
5204 : return;
5205 :
5206 1112956 : DDR_DIR_VECTS (ddr).safe_push (dir_v);
5207 : }
5208 :
5209 : /* Add a distance of 1 on all the loops outer than INDEX. If we
5210 : haven't yet determined a distance for this outer loop, push a new
5211 : distance vector composed of the previous distance, and a distance
5212 : of 1 for this outer loop. Example:
5213 :
5214 : | loop_1
5215 : | loop_2
5216 : | A[10]
5217 : | endloop_2
5218 : | endloop_1
5219 :
5220 : Saved vectors are of the form (dist_in_1, dist_in_2). First, we
5221 : save (0, 1), then we have to save (1, 0). */
5222 :
5223 : static void
5224 17462 : add_outer_distances (struct data_dependence_relation *ddr,
5225 : lambda_vector dist_v, int index)
5226 : {
5227 : /* For each outer loop where init_v is not set, the accesses are
5228 : in dependence of distance 1 in the loop. */
5229 21013 : while (--index >= 0)
5230 : {
5231 7102 : lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5232 3551 : lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
5233 3551 : save_v[index] = 1;
5234 3551 : save_dist_v (ddr, save_v);
5235 : }
5236 17462 : }
5237 :
5238 : /* Return false when fail to represent the data dependence as a
5239 : distance vector. A_INDEX is the index of the first reference
5240 : (0 for DDR_A, 1 for DDR_B) and B_INDEX is the index of the
5241 : second reference. INIT_B is set to true when a component has been
5242 : added to the distance vector DIST_V. INDEX_CARRY is then set to
5243 : the index in DIST_V that carries the dependence. */
5244 :
5245 : static bool
5246 60903 : build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
5247 : unsigned int a_index, unsigned int b_index,
5248 : lambda_vector dist_v, bool *init_b,
5249 : int *index_carry)
5250 : {
5251 60903 : unsigned i;
5252 121806 : lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5253 60903 : class loop *loop = DDR_LOOP_NEST (ddr)[0];
5254 :
5255 136866 : for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
5256 : {
5257 78149 : tree access_fn_a, access_fn_b;
5258 78149 : struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
5259 :
5260 78149 : if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
5261 : {
5262 309 : non_affine_dependence_relation (ddr);
5263 309 : return false;
5264 : }
5265 :
5266 77840 : access_fn_a = SUB_ACCESS_FN (subscript, a_index);
5267 77840 : access_fn_b = SUB_ACCESS_FN (subscript, b_index);
5268 :
5269 77840 : if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
5270 59537 : && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
5271 : {
5272 58911 : HOST_WIDE_INT dist;
5273 58911 : int index;
5274 58911 : int var_a = CHREC_VARIABLE (access_fn_a);
5275 58911 : int var_b = CHREC_VARIABLE (access_fn_b);
5276 :
5277 58911 : if (var_a != var_b
5278 58911 : || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
5279 : {
5280 34 : non_affine_dependence_relation (ddr);
5281 34 : return false;
5282 : }
5283 :
5284 : /* When data references are collected in a loop while data
5285 : dependences are analyzed in loop nest nested in the loop, we
5286 : would have more number of access functions than number of
5287 : loops. Skip access functions of loops not in the loop nest.
5288 :
5289 : See PR89725 for more information. */
5290 58877 : if (flow_loop_nested_p (get_loop (cfun, var_a), loop))
5291 2 : continue;
5292 :
5293 58875 : dist = int_cst_value (SUB_DISTANCE (subscript));
5294 58875 : index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
5295 58875 : *index_carry = MIN (index, *index_carry);
5296 :
5297 : /* This is the subscript coupling test. If we have already
5298 : recorded a distance for this loop (a distance coming from
5299 : another subscript), it should be the same. For example,
5300 : in the following code, there is no dependence:
5301 :
5302 : | loop i = 0, N, 1
5303 : | T[i+1][i] = ...
5304 : | ... = T[i][i]
5305 : | endloop
5306 : */
5307 58875 : if (init_v[index] != 0 && dist_v[index] != dist)
5308 : {
5309 2 : finalize_ddr_dependent (ddr, chrec_known);
5310 2 : return false;
5311 : }
5312 :
5313 58873 : dist_v[index] = dist;
5314 58873 : init_v[index] = 1;
5315 58873 : *init_b = true;
5316 58873 : }
5317 18929 : else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
5318 : {
5319 : /* This can be for example an affine vs. constant dependence
5320 : (T[i] vs. T[3]) that is not an affine dependence and is
5321 : not representable as a distance vector. */
5322 1841 : non_affine_dependence_relation (ddr);
5323 1841 : return false;
5324 : }
5325 : }
5326 :
5327 : return true;
5328 : }
5329 :
5330 : /* Return true when the DDR contains only invariant access functions wrto. loop
5331 : number LNUM. */
5332 :
5333 : static bool
5334 878808 : invariant_access_functions (const struct data_dependence_relation *ddr,
5335 : int lnum)
5336 : {
5337 2964570 : for (subscript *sub : DDR_SUBSCRIPTS (ddr))
5338 1027431 : if (!evolution_function_is_invariant_p (SUB_ACCESS_FN (sub, 0), lnum)
5339 1027431 : || !evolution_function_is_invariant_p (SUB_ACCESS_FN (sub, 1), lnum))
5340 : return false;
5341 :
5342 : return true;
5343 : }
5344 :
5345 : /* Helper function for the case where DDR_A and DDR_B are the same
5346 : multivariate access function with a constant step. For an example
5347 : see pr34635-1.c. */
5348 :
5349 : static void
5350 4572 : add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
5351 : {
5352 4572 : int x_1, x_2;
5353 4572 : tree c_1 = CHREC_LEFT (c_2);
5354 4572 : tree c_0 = CHREC_LEFT (c_1);
5355 4572 : lambda_vector dist_v;
5356 4572 : HOST_WIDE_INT v1, v2, cd;
5357 :
5358 : /* Polynomials with more than 2 variables are not handled yet. When
5359 : the evolution steps are parameters or do not fit a HOST_WIDE_INT,
5360 : it is not possible to represent the dependence using classical
5361 : distance vectors. */
5362 4572 : if (TREE_CODE (c_0) != INTEGER_CST
5363 3377 : || !cst_and_fits_in_hwi (CHREC_RIGHT (c_1))
5364 2683 : || !cst_and_fits_in_hwi (CHREC_RIGHT (c_2))
5365 : /* We want to be able to negate without overflow. */
5366 2675 : || int_cst_value (CHREC_RIGHT (c_1)) == HOST_WIDE_INT_MIN
5367 7247 : || int_cst_value (CHREC_RIGHT (c_2)) == HOST_WIDE_INT_MIN)
5368 : {
5369 1904 : DDR_AFFINE_P (ddr) = false;
5370 1904 : DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
5371 1904 : DDR_DIST_VECTS (ddr).release ();
5372 1904 : return;
5373 : }
5374 :
5375 2668 : x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
5376 2668 : x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
5377 :
5378 : /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
5379 5336 : dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5380 2668 : v1 = int_cst_value (CHREC_RIGHT (c_1));
5381 2668 : v2 = int_cst_value (CHREC_RIGHT (c_2));
5382 2668 : cd = gcd (v1, v2);
5383 2668 : v1 /= cd;
5384 2668 : v2 /= cd;
5385 :
5386 2668 : if (v2 < 0)
5387 : {
5388 19 : v2 = -v2;
5389 19 : v1 = -v1;
5390 : }
5391 :
5392 2668 : dist_v[x_1] = v2;
5393 2668 : dist_v[x_2] = -v1;
5394 2668 : save_dist_v (ddr, dist_v);
5395 :
5396 2668 : add_outer_distances (ddr, dist_v, x_1);
5397 : }
5398 :
5399 : /* Helper function for the case where DDR_A and DDR_B are the same
5400 : access functions. */
5401 :
5402 : static void
5403 19509 : add_other_self_distances (struct data_dependence_relation *ddr)
5404 : {
5405 19509 : lambda_vector dist_v;
5406 19509 : unsigned i;
5407 19509 : int index_carry = DDR_NB_LOOPS (ddr);
5408 19509 : subscript *sub;
5409 19509 : class loop *loop = DDR_LOOP_NEST (ddr)[0];
5410 :
5411 41665 : FOR_EACH_VEC_ELT (DDR_SUBSCRIPTS (ddr), i, sub)
5412 : {
5413 27249 : tree access_fun = SUB_ACCESS_FN (sub, 0);
5414 :
5415 27249 : if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
5416 : {
5417 19739 : if (!evolution_function_is_univariate_p (access_fun, loop->num))
5418 : {
5419 5093 : if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
5420 : {
5421 521 : DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
5422 521 : DDR_DIST_VECTS (ddr).release ();
5423 521 : return;
5424 : }
5425 :
5426 4572 : access_fun = SUB_ACCESS_FN (DDR_SUBSCRIPT (ddr, 0), 0);
5427 :
5428 4572 : if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
5429 4572 : add_multivariate_self_dist (ddr, access_fun);
5430 : else
5431 : {
5432 : /* The evolution step is not constant: it varies in
5433 : the outer loop, so this cannot be represented by a
5434 : distance vector. For example in pr34635.c the
5435 : evolution is {0, +, {0, +, 4}_1}_2. */
5436 0 : DDR_AFFINE_P (ddr) = false;
5437 0 : DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
5438 0 : DDR_DIST_VECTS (ddr).release ();
5439 : }
5440 :
5441 : return;
5442 : }
5443 :
5444 : /* When data references are collected in a loop while data
5445 : dependences are analyzed in loop nest nested in the loop, we
5446 : would have more number of access functions than number of
5447 : loops. Skip access functions of loops not in the loop nest.
5448 :
5449 : See PR89725 for more information. */
5450 14646 : if (flow_loop_nested_p (get_loop (cfun, CHREC_VARIABLE (access_fun)),
5451 : loop))
5452 0 : continue;
5453 :
5454 22445 : index_carry = MIN (index_carry,
5455 : index_in_loop_nest (CHREC_VARIABLE (access_fun),
5456 : DDR_LOOP_NEST (ddr)));
5457 : }
5458 : }
5459 :
5460 28832 : dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5461 14416 : add_outer_distances (ddr, dist_v, index_carry);
5462 : }
5463 :
5464 : static void
5465 179523 : insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
5466 : {
5467 359046 : lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5468 :
5469 179523 : dist_v[0] = 1;
5470 179523 : save_dist_v (ddr, dist_v);
5471 179523 : }
5472 :
5473 : /* Adds a unit distance vector to DDR when there is a 0 overlap. This
5474 : is the case for example when access functions are the same and
5475 : equal to a constant, as in:
5476 :
5477 : | loop_1
5478 : | A[3] = ...
5479 : | ... = A[3]
5480 : | endloop_1
5481 :
5482 : in which case the distance vectors are (0) and (1). */
5483 :
5484 : static void
5485 179523 : add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
5486 : {
5487 179523 : unsigned i, j;
5488 :
5489 179523 : for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
5490 : {
5491 179523 : subscript_p sub = DDR_SUBSCRIPT (ddr, i);
5492 179523 : conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
5493 179523 : conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
5494 :
5495 179523 : for (j = 0; j < ca->n; j++)
5496 179523 : if (affine_function_zero_p (ca->fns[j]))
5497 : {
5498 179523 : insert_innermost_unit_dist_vector (ddr);
5499 179523 : return;
5500 : }
5501 :
5502 0 : for (j = 0; j < cb->n; j++)
5503 0 : if (affine_function_zero_p (cb->fns[j]))
5504 : {
5505 0 : insert_innermost_unit_dist_vector (ddr);
5506 0 : return;
5507 : }
5508 : }
5509 : }
5510 :
5511 : /* Return true when the DDR contains two data references that have the
5512 : same access functions. */
5513 :
5514 : static inline bool
5515 931903 : same_access_functions (const struct data_dependence_relation *ddr)
5516 : {
5517 3900015 : for (subscript *sub : DDR_SUBSCRIPTS (ddr))
5518 1157401 : if (!eq_evolutions_p (SUB_ACCESS_FN (sub, 0),
5519 1157401 : SUB_ACCESS_FN (sub, 1)))
5520 : return false;
5521 :
5522 : return true;
5523 : }
5524 :
5525 : /* Compute the classic per loop distance vector. DDR is the data
5526 : dependence relation to build a vector from. Return false when fail
5527 : to represent the data dependence as a distance vector. */
5528 :
5529 : static bool
5530 3243000 : build_classic_dist_vector (struct data_dependence_relation *ddr,
5531 : class loop *loop_nest)
5532 : {
5533 3243000 : bool init_b = false;
5534 3243000 : int index_carry = DDR_NB_LOOPS (ddr);
5535 3243000 : lambda_vector dist_v;
5536 :
5537 3243000 : if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
5538 : return false;
5539 :
5540 931903 : if (same_access_functions (ddr))
5541 : {
5542 : /* Save the 0 vector. */
5543 1757616 : dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5544 878808 : save_dist_v (ddr, dist_v);
5545 :
5546 878808 : if (invariant_access_functions (ddr, loop_nest->num))
5547 179523 : add_distance_for_zero_overlaps (ddr);
5548 :
5549 878808 : if (DDR_NB_LOOPS (ddr) > 1)
5550 19509 : add_other_self_distances (ddr);
5551 :
5552 : return true;
5553 : }
5554 :
5555 106190 : dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5556 53095 : if (!build_classic_dist_vector_1 (ddr, 0, 1, dist_v, &init_b, &index_carry))
5557 : return false;
5558 :
5559 : /* Save the distance vector if we initialized one. */
5560 50909 : if (init_b)
5561 : {
5562 : /* Verify a basic constraint: classic distance vectors should
5563 : always be lexicographically positive.
5564 :
5565 : Data references are collected in the order of execution of
5566 : the program, thus for the following loop
5567 :
5568 : | for (i = 1; i < 100; i++)
5569 : | for (j = 1; j < 100; j++)
5570 : | {
5571 : | t = T[j+1][i-1]; // A
5572 : | T[j][i] = t + 2; // B
5573 : | }
5574 :
5575 : references are collected following the direction of the wind:
5576 : A then B. The data dependence tests are performed also
5577 : following this order, such that we're looking at the distance
5578 : separating the elements accessed by A from the elements later
5579 : accessed by B. But in this example, the distance returned by
5580 : test_dep (A, B) is lexicographically negative (-1, 1), that
5581 : means that the access A occurs later than B with respect to
5582 : the outer loop, ie. we're actually looking upwind. In this
5583 : case we solve test_dep (B, A) looking downwind to the
5584 : lexicographically positive solution, that returns the
5585 : distance vector (1, -1). */
5586 101818 : if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
5587 : {
5588 7697 : lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5589 7697 : if (!subscript_dependence_tester_1 (ddr, 1, 0, loop_nest))
5590 : return false;
5591 7693 : compute_subscript_distance (ddr);
5592 7693 : if (!build_classic_dist_vector_1 (ddr, 1, 0, save_v, &init_b,
5593 : &index_carry))
5594 : return false;
5595 7693 : save_dist_v (ddr, save_v);
5596 7693 : DDR_REVERSED_P (ddr) = true;
5597 :
5598 : /* In this case there is a dependence forward for all the
5599 : outer loops:
5600 :
5601 : | for (k = 1; k < 100; k++)
5602 : | for (i = 1; i < 100; i++)
5603 : | for (j = 1; j < 100; j++)
5604 : | {
5605 : | t = T[j+1][i-1]; // A
5606 : | T[j][i] = t + 2; // B
5607 : | }
5608 :
5609 : the vectors are:
5610 : (0, 1, -1)
5611 : (1, 1, -1)
5612 : (1, -1, 1)
5613 : */
5614 7693 : if (DDR_NB_LOOPS (ddr) > 1)
5615 : {
5616 74 : add_outer_distances (ddr, save_v, index_carry);
5617 74 : add_outer_distances (ddr, dist_v, index_carry);
5618 : }
5619 : }
5620 : else
5621 : {
5622 43212 : lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5623 43212 : lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
5624 :
5625 43212 : if (DDR_NB_LOOPS (ddr) > 1)
5626 : {
5627 115 : lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5628 :
5629 115 : if (!subscript_dependence_tester_1 (ddr, 1, 0, loop_nest))
5630 : return false;
5631 115 : compute_subscript_distance (ddr);
5632 115 : if (!build_classic_dist_vector_1 (ddr, 1, 0, opposite_v, &init_b,
5633 : &index_carry))
5634 : return false;
5635 :
5636 115 : save_dist_v (ddr, save_v);
5637 115 : add_outer_distances (ddr, dist_v, index_carry);
5638 115 : add_outer_distances (ddr, opposite_v, index_carry);
5639 : }
5640 : else
5641 43097 : save_dist_v (ddr, save_v);
5642 : }
5643 : }
5644 : else
5645 : {
5646 : /* There is a distance of 1 on all the outer loops: Example:
5647 : there is a dependence of distance 1 on loop_1 for the array A.
5648 :
5649 : | loop_1
5650 : | A[5] = ...
5651 : | endloop
5652 : */
5653 0 : add_outer_distances (ddr, dist_v,
5654 : lambda_vector_first_nz (dist_v,
5655 0 : DDR_NB_LOOPS (ddr), 0));
5656 : }
5657 :
5658 : return true;
5659 : }
5660 :
5661 : /* Return the direction for a given distance.
5662 : FIXME: Computing dir this way is suboptimal, since dir can catch
5663 : cases that dist is unable to represent. */
5664 :
5665 : static inline enum data_dependence_direction
5666 1136879 : dir_from_dist (lambda_int dist)
5667 : {
5668 1136879 : if (dist > 0)
5669 : return dir_positive;
5670 899962 : else if (dist < 0)
5671 : return dir_negative;
5672 : else
5673 897067 : return dir_equal;
5674 : }
5675 :
5676 : /* Compute the classic per loop direction vector. DDR is the data
5677 : dependence relation to build a vector from. */
5678 :
5679 : static void
5680 929639 : build_classic_dir_vector (struct data_dependence_relation *ddr)
5681 : {
5682 929639 : unsigned i, j;
5683 929639 : lambda_vector dist_v;
5684 :
5685 2042595 : FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
5686 : {
5687 2225912 : lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5688 :
5689 3362791 : for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
5690 2036841 : dir_v[j] = dir_from_dist (dist_v[j]);
5691 :
5692 1112956 : save_dir_v (ddr, dir_v);
5693 : }
5694 929639 : }
5695 :
5696 : /* Helper function. Returns true when there is a dependence between the
5697 : data references. A_INDEX is the index of the first reference (0 for
5698 : DDR_A, 1 for DDR_B) and B_INDEX is the index of the second reference. */
5699 :
5700 : static bool
5701 3250812 : subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
5702 : unsigned int a_index, unsigned int b_index,
5703 : class loop *loop_nest)
5704 : {
5705 3250812 : unsigned int i;
5706 3250812 : tree last_conflicts;
5707 3250812 : struct subscript *subscript;
5708 3250812 : tree res = NULL_TREE;
5709 :
5710 4557195 : for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
5711 : {
5712 3590352 : conflict_function *overlaps_a, *overlaps_b;
5713 :
5714 3590352 : analyze_overlapping_iterations (SUB_ACCESS_FN (subscript, a_index),
5715 : SUB_ACCESS_FN (subscript, b_index),
5716 : &overlaps_a, &overlaps_b,
5717 : &last_conflicts, loop_nest);
5718 :
5719 3590352 : if (SUB_CONFLICTS_IN_A (subscript))
5720 3590352 : free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
5721 3590352 : if (SUB_CONFLICTS_IN_B (subscript))
5722 3590352 : free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
5723 :
5724 3590352 : SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
5725 3590352 : SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
5726 3590352 : SUB_LAST_CONFLICT (subscript) = last_conflicts;
5727 :
5728 : /* If there is any undetermined conflict function we have to
5729 : give a conservative answer in case we cannot prove that
5730 : no dependence exists when analyzing another subscript. */
5731 3590352 : if (CF_NOT_KNOWN_P (overlaps_a)
5732 3562141 : || CF_NOT_KNOWN_P (overlaps_b))
5733 : {
5734 28211 : res = chrec_dont_know;
5735 28211 : continue;
5736 : }
5737 :
5738 : /* When there is a subscript with no dependence we can stop. */
5739 3562141 : else if (CF_NO_DEPENDENCE_P (overlaps_a)
5740 1278172 : || CF_NO_DEPENDENCE_P (overlaps_b))
5741 : {
5742 2283969 : res = chrec_known;
5743 2283969 : break;
5744 : }
5745 : }
5746 :
5747 3250812 : if (res == NULL_TREE)
5748 : return true;
5749 :
5750 2311101 : if (res == chrec_known)
5751 2283969 : dependence_stats.num_dependence_independent++;
5752 : else
5753 27132 : dependence_stats.num_dependence_undetermined++;
5754 2311101 : finalize_ddr_dependent (ddr, res);
5755 2311101 : return false;
5756 : }
5757 :
5758 : /* Return true if every distance vector of DDR has, in the loops marked in
5759 : VARIES, the distances of the first vector or all of them negated. */
5760 :
5761 : static bool
5762 3434 : dist_vectors_agree_p (const struct data_dependence_relation *ddr,
5763 : const vec<bool> &varies)
5764 : {
5765 3434 : if (DDR_NUM_DIST_VECTS (ddr) == 0)
5766 : return true;
5767 3425 : lambda_vector first = DDR_DIST_VECT (ddr, 0);
5768 10399 : for (lambda_vector v : DDR_DIST_VECTS (ddr))
5769 : {
5770 6978 : bool same = true, opposite = true;
5771 21046 : for (unsigned i = 0; i < DDR_NB_LOOPS (ddr); i++)
5772 14068 : if (varies[i])
5773 : {
5774 6113 : same &= v[i] == first[i];
5775 6113 : opposite &= v[i] == -first[i];
5776 : }
5777 6978 : if (!same && !opposite)
5778 : return false;
5779 : }
5780 : return true;
5781 : }
5782 :
5783 : /* Set VARIES[I] for each loop I of the nest of DDR in which CHREC has an
5784 : evolution part, as evolution_part_in_loop_num finds it, walking CHREC
5785 : once. The nest is a chain of loops at consecutive depths, as
5786 : find_loop_nest builds it, so loop I is at the depth of loop 0 plus I. */
5787 :
5788 : static void
5789 55156 : mark_evolving_loops (const struct data_dependence_relation *ddr, tree chrec,
5790 : vec<bool> *varies)
5791 : {
5792 55156 : unsigned n = DDR_NB_LOOPS (ddr);
5793 55156 : if (automatically_generated_chrec_p (chrec))
5794 0 : for (unsigned i = 0; i < n; i++)
5795 0 : (*varies)[i] = true;
5796 55156 : unsigned depth0 = loop_depth (DDR_LOOP_NEST (ddr)[0]);
5797 105394 : for (; TREE_CODE (chrec) == POLYNOMIAL_CHREC; chrec = CHREC_LEFT (chrec))
5798 : {
5799 50238 : class loop *loop = get_chrec_loop (chrec);
5800 50238 : unsigned i = loop_depth (loop) - depth0;
5801 50238 : if (i < n && DDR_LOOP_NEST (ddr)[i] == loop)
5802 50204 : (*varies)[i] = true;
5803 : }
5804 55156 : }
5805 :
5806 : /* What the distance vectors of a dependence miss in its free loops. */
5807 :
5808 : enum free_loop_distances
5809 : {
5810 : /* They miss nothing. */
5811 : FREE_LOOP_DISTANCES_KEPT,
5812 : /* They miss distances, and agree in the loops the subscripts constrain,
5813 : so they can be completed. */
5814 : FREE_LOOP_DISTANCES_LOST,
5815 : /* They miss distances and do not agree there. */
5816 : FREE_LOOP_DISTANCES_DISAGREE
5817 : };
5818 :
5819 : /* Return whether the distance vectors of DDR miss distances because of a
5820 : free loop, a loop of the nest that contains both references and in
5821 : which no compared access function evolves, so that the dependence can
5822 : have any distance in it. The vectors give a free loop a distance of
5823 : its own only when it is the only free loop and lies outside every loop
5824 : the subscripts constrain, and the vectors agree in the loops the
5825 : subscripts constrain. Otherwise its distance is taken as zero, which
5826 : misses the distances of opposite signs in it and in another free loop
5827 : or in a loop with a nonzero distance. Set FREE_LOOPS[I] when loop I of
5828 : the nest is free and VARIES[I] when a compared access function evolves
5829 : in it; both have room for DDR_NB_LOOPS (DDR) elements. */
5830 :
5831 : static enum free_loop_distances
5832 929713 : check_free_loop_distances (const struct data_dependence_relation *ddr,
5833 : vec<bool> *free_loops, vec<bool> *varies)
5834 : {
5835 929713 : unsigned n = DDR_NB_LOOPS (ddr);
5836 929713 : if (n < 2)
5837 : return FREE_LOOP_DISTANCES_KEPT;
5838 19698 : free_loops->quick_grow_cleared (n);
5839 :
5840 19698 : varies->quick_grow_cleared (n);
5841 86672 : for (subscript *sub : DDR_SUBSCRIPTS (ddr))
5842 82734 : for (unsigned k = 0; k < 2; k++)
5843 55156 : mark_evolving_loops (ddr, SUB_ACCESS_FN (sub, k), varies);
5844 :
5845 19698 : basic_block bb_a = gimple_bb (DR_STMT (DDR_A (ddr)));
5846 19698 : basic_block bb_b = gimple_bb (DR_STMT (DDR_B (ddr)));
5847 19698 : unsigned first_varying = n, nfree = 0, free_idx = 0;
5848 59182 : for (unsigned i = 0; i < n; i++)
5849 39484 : if ((*varies)[i])
5850 45186 : first_varying = MIN (first_varying, i);
5851 14388 : else if (flow_bb_inside_loop_p (DDR_LOOP_NEST (ddr)[i], bb_a)
5852 14388 : && flow_bb_inside_loop_p (DDR_LOOP_NEST (ddr)[i], bb_b))
5853 : {
5854 3679 : nfree++;
5855 3679 : free_idx = i;
5856 3679 : (*free_loops)[i] = true;
5857 : }
5858 19698 : if (nfree == 0)
5859 : return FREE_LOOP_DISTANCES_KEPT;
5860 3434 : if (!dist_vectors_agree_p (ddr, *varies))
5861 : return FREE_LOOP_DISTANCES_DISAGREE;
5862 3430 : if (nfree == 1 && free_idx < first_varying)
5863 : return FREE_LOOP_DISTANCES_KEPT;
5864 462 : if (nfree > 1)
5865 : return FREE_LOOP_DISTANCES_LOST;
5866 :
5867 : unsigned j;
5868 : lambda_vector v;
5869 610 : FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), j, v)
5870 593 : for (unsigned i = 0; i < n; i++)
5871 417 : if ((*varies)[i] && v[i] != 0)
5872 : return FREE_LOOP_DISTANCES_LOST;
5873 : return FREE_LOOP_DISTANCES_KEPT;
5874 : }
5875 :
5876 : /* Add the distance vectors of DDR that its free loops FREE_LOOPS miss,
5877 : when its vectors agree in the loops the subscripts constrain: every
5878 : lexicographically positive vector with the distances of the first
5879 : vector, or their opposite, in the other loops and -1, 0 or 1 in each
5880 : free loop. That is not possible when the bases differ and the objects
5881 : vary in the nest; return false then, when there is no vector to start
5882 : from, or as soon as more than 32 vectors would be needed. */
5883 :
5884 : static bool
5885 286 : add_free_loop_distances (struct data_dependence_relation *ddr,
5886 : const vec<bool> &free_loops)
5887 : {
5888 66 : if ((DDR_COULD_BE_INDEPENDENT_P (ddr) && !DDR_OBJECT_A (ddr))
5889 286 : || DDR_NUM_DIST_VECTS (ddr) == 0)
5890 : return false;
5891 216 : unsigned n = DDR_NB_LOOPS (ddr), nfree = 0, ncomb = 1;
5892 1115 : for (bool f : free_loops)
5893 467 : if (f)
5894 : {
5895 391 : nfree++;
5896 391 : ncomb *= 3;
5897 : }
5898 216 : if (nfree > 3)
5899 : return false;
5900 :
5901 216 : lambda_vector first = DDR_DIST_VECT (ddr, 0);
5902 648 : for (int sign = 1; sign >= -1; sign -= 2)
5903 3828 : for (unsigned c = 0; c < ncomb; c++)
5904 : {
5905 3396 : lambda_vector v = lambda_vector_new (n);
5906 14094 : for (unsigned i = 0, rest = c; i < n; i++)
5907 7302 : if (free_loops[i])
5908 : {
5909 6546 : v[i] = (int) (rest % 3) - 1;
5910 6546 : rest /= 3;
5911 : }
5912 : else
5913 756 : v[i] = sign * first[i];
5914 5269 : if (lambda_vector_lexico_pos (v, n))
5915 : {
5916 1873 : save_dist_v (ddr, v);
5917 3396 : if (DDR_NUM_DIST_VECTS (ddr) > 32)
5918 : return false;
5919 : }
5920 : }
5921 : return true;
5922 : }
5923 :
5924 : /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
5925 :
5926 : static void
5927 3243000 : subscript_dependence_tester (struct data_dependence_relation *ddr,
5928 : class loop *loop_nest)
5929 : {
5930 3243000 : if (subscript_dependence_tester_1 (ddr, 0, 1, loop_nest))
5931 931903 : dependence_stats.num_dependence_dependent++;
5932 :
5933 3243000 : compute_subscript_distance (ddr);
5934 3243000 : if (build_classic_dist_vector (ddr, loop_nest))
5935 : {
5936 929713 : unsigned n = DDR_NB_LOOPS (ddr);
5937 929713 : auto_vec<bool, 8> free_loops (n), varies (n);
5938 929713 : enum free_loop_distances lost
5939 929713 : = check_free_loop_distances (ddr, &free_loops, &varies);
5940 929713 : if (lost == FREE_LOOP_DISTANCES_DISAGREE
5941 929713 : || (lost == FREE_LOOP_DISTANCES_LOST
5942 286 : && !add_free_loop_distances (ddr, free_loops)))
5943 : {
5944 74 : if (dump_file && (dump_flags & TDF_DETAILS))
5945 0 : fprintf (dump_file, "(distance vectors miss a free loop)\n");
5946 74 : DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
5947 74 : DDR_DIST_VECTS (ddr).release ();
5948 74 : return;
5949 : }
5950 :
5951 929639 : if (dump_file && (dump_flags & TDF_DETAILS))
5952 : {
5953 4192 : unsigned i;
5954 :
5955 4192 : fprintf (dump_file, "(build_classic_dist_vector\n");
5956 12661 : for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
5957 : {
5958 4277 : fprintf (dump_file, " dist_vector = (");
5959 4277 : print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
5960 8554 : DDR_NB_LOOPS (ddr));
5961 4277 : fprintf (dump_file, " )\n");
5962 : }
5963 4192 : fprintf (dump_file, ")\n");
5964 : }
5965 :
5966 929639 : build_classic_dir_vector (ddr);
5967 929713 : }
5968 : }
5969 :
5970 : /* Returns true when all the access functions of A are affine or
5971 : constant with respect to LOOP_NEST. */
5972 :
5973 : static bool
5974 6560463 : access_functions_are_affine_or_constant_p (const struct data_reference *a,
5975 : const class loop *loop_nest)
5976 : {
5977 6560463 : vec<tree> fns = DR_ACCESS_FNS (a);
5978 28422028 : for (tree t : fns)
5979 8811977 : if (!evolution_function_is_invariant_p (t, loop_nest->num)
5980 8811977 : && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
5981 : return false;
5982 :
5983 : return true;
5984 : }
5985 :
5986 : /* This computes the affine dependence relation between A and B with
5987 : respect to LOOP_NEST. CHREC_KNOWN is used for representing the
5988 : independence between two accesses, while CHREC_DONT_KNOW is used
5989 : for representing the unknown relation.
5990 :
5991 : Note that it is possible to stop the computation of the dependence
5992 : relation the first time we detect a CHREC_KNOWN element for a given
5993 : subscript. */
5994 :
5995 : void
5996 6667946 : compute_affine_dependence (struct data_dependence_relation *ddr,
5997 : class loop *loop_nest)
5998 : {
5999 6667946 : struct data_reference *dra = DDR_A (ddr);
6000 6667946 : struct data_reference *drb = DDR_B (ddr);
6001 :
6002 6667946 : if (dump_file && (dump_flags & TDF_DETAILS))
6003 : {
6004 138842 : fprintf (dump_file, "(compute_affine_dependence\n");
6005 138842 : fprintf (dump_file, " ref_a: ");
6006 138842 : print_generic_expr (dump_file, DR_REF (dra));
6007 138842 : fprintf (dump_file, ", stmt_a: ");
6008 138842 : print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
6009 138842 : fprintf (dump_file, " ref_b: ");
6010 138842 : print_generic_expr (dump_file, DR_REF (drb));
6011 138842 : fprintf (dump_file, ", stmt_b: ");
6012 138842 : print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
6013 : }
6014 :
6015 : /* Analyze only when the dependence relation is not yet known. */
6016 6667946 : if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
6017 : {
6018 3314338 : dependence_stats.num_dependence_tests++;
6019 :
6020 3314338 : if (access_functions_are_affine_or_constant_p (dra, loop_nest)
6021 3314338 : && access_functions_are_affine_or_constant_p (drb, loop_nest))
6022 3243000 : subscript_dependence_tester (ddr, loop_nest);
6023 :
6024 : /* As a last case, if the dependence cannot be determined, or if
6025 : the dependence is considered too difficult to determine, answer
6026 : "don't know". */
6027 : else
6028 : {
6029 71338 : dependence_stats.num_dependence_undetermined++;
6030 :
6031 71338 : if (dump_file && (dump_flags & TDF_DETAILS))
6032 : {
6033 158 : fprintf (dump_file, "Data ref a:\n");
6034 158 : dump_data_reference (dump_file, dra);
6035 158 : fprintf (dump_file, "Data ref b:\n");
6036 158 : dump_data_reference (dump_file, drb);
6037 158 : fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
6038 : }
6039 71338 : finalize_ddr_dependent (ddr, chrec_dont_know);
6040 : }
6041 : }
6042 :
6043 6667946 : if (dump_file && (dump_flags & TDF_DETAILS))
6044 : {
6045 138842 : if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
6046 122728 : fprintf (dump_file, ") -> no dependence\n");
6047 16114 : else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
6048 11838 : fprintf (dump_file, ") -> dependence analysis failed\n");
6049 : else
6050 4276 : fprintf (dump_file, ")\n");
6051 : }
6052 6667946 : }
6053 :
6054 : /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
6055 : the data references in DATAREFS, in the LOOP_NEST. When
6056 : COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
6057 : relations. Return true when successful, i.e. data references number
6058 : is small enough to be handled. */
6059 :
6060 : bool
6061 451533 : compute_all_dependences (const vec<data_reference_p> &datarefs,
6062 : vec<ddr_p> *dependence_relations,
6063 : const vec<loop_p> &loop_nest,
6064 : bool compute_self_and_rr)
6065 : {
6066 451533 : struct data_dependence_relation *ddr;
6067 451533 : struct data_reference *a, *b;
6068 451533 : unsigned int i, j;
6069 :
6070 451533 : if ((int) datarefs.length ()
6071 451533 : > param_loop_max_datarefs_for_datadeps)
6072 : {
6073 0 : struct data_dependence_relation *ddr;
6074 :
6075 : /* Insert a single relation into dependence_relations:
6076 : chrec_dont_know. */
6077 0 : ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
6078 0 : dependence_relations->safe_push (ddr);
6079 0 : return false;
6080 : }
6081 :
6082 1661387 : FOR_EACH_VEC_ELT (datarefs, i, a)
6083 7458542 : for (j = i + 1; datarefs.iterate (j, &b); j++)
6084 5038834 : if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
6085 : {
6086 4656425 : ddr = initialize_data_dependence_relation (a, b, loop_nest);
6087 4656425 : dependence_relations->safe_push (ddr);
6088 4656425 : if (loop_nest.exists ())
6089 4634371 : compute_affine_dependence (ddr, loop_nest[0]);
6090 : }
6091 :
6092 451533 : if (compute_self_and_rr)
6093 1059312 : FOR_EACH_VEC_ELT (datarefs, i, a)
6094 : {
6095 784962 : ddr = initialize_data_dependence_relation (a, a, loop_nest);
6096 784962 : dependence_relations->safe_push (ddr);
6097 784962 : if (loop_nest.exists ())
6098 784962 : compute_affine_dependence (ddr, loop_nest[0]);
6099 : }
6100 :
6101 : return true;
6102 : }
6103 :
6104 : /* Describes a location of a memory reference. */
6105 :
6106 : struct data_ref_loc
6107 : {
6108 : /* The memory reference. */
6109 : tree ref;
6110 :
6111 : /* True if the memory reference is read. */
6112 : bool is_read;
6113 :
6114 : /* True if the data reference is conditional within the containing
6115 : statement, i.e. if it might not occur even when the statement
6116 : is executed and runs to completion. */
6117 : bool is_conditional_in_stmt;
6118 : };
6119 :
6120 :
6121 : /* Stores the locations of memory references in STMT to REFERENCES. Returns
6122 : true if STMT clobbers memory, false otherwise. */
6123 :
6124 : static bool
6125 51620489 : get_references_in_stmt (gimple *stmt, vec<data_ref_loc, va_heap> *references)
6126 : {
6127 51620489 : bool clobbers_memory = false;
6128 51620489 : data_ref_loc ref;
6129 51620489 : tree op0, op1;
6130 51620489 : enum gimple_code stmt_code = gimple_code (stmt);
6131 :
6132 : /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
6133 : As we cannot model data-references to not spelled out
6134 : accesses give up if they may occur. */
6135 51620489 : if (stmt_code == GIMPLE_CALL
6136 51620489 : && !(gimple_call_flags (stmt) & ECF_CONST))
6137 : {
6138 : /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
6139 4246872 : if (gimple_call_internal_p (stmt))
6140 66939 : switch (gimple_call_internal_fn (stmt))
6141 : {
6142 6731 : case IFN_GOMP_SIMD_LANE:
6143 6731 : {
6144 6731 : class loop *loop = gimple_bb (stmt)->loop_father;
6145 6731 : tree uid = gimple_call_arg (stmt, 0);
6146 6731 : gcc_assert (TREE_CODE (uid) == SSA_NAME);
6147 6731 : if (loop == NULL
6148 6731 : || loop->simduid != SSA_NAME_VAR (uid))
6149 : clobbers_memory = true;
6150 : break;
6151 : }
6152 : case IFN_MASK_LOAD:
6153 : case IFN_MASK_STORE:
6154 : break;
6155 1007 : case IFN_MASK_CALL:
6156 1007 : {
6157 1007 : tree orig_fndecl
6158 1007 : = gimple_call_addr_fndecl (gimple_call_arg (stmt, 0));
6159 1007 : if (!orig_fndecl
6160 1007 : || (flags_from_decl_or_type (orig_fndecl) & ECF_CONST) == 0)
6161 : clobbers_memory = true;
6162 : }
6163 : break;
6164 : default:
6165 4281783 : clobbers_memory = true;
6166 : break;
6167 : }
6168 4179933 : else if (gimple_call_builtin_p (stmt, BUILT_IN_PREFETCH))
6169 : clobbers_memory = false;
6170 : else
6171 4281783 : clobbers_memory = true;
6172 : }
6173 47373617 : else if (stmt_code == GIMPLE_ASM
6174 47373617 : && (gimple_asm_volatile_p (as_a <gasm *> (stmt))
6175 8558 : || gimple_vuse (stmt)))
6176 : clobbers_memory = true;
6177 :
6178 87409566 : if (!gimple_vuse (stmt))
6179 : return clobbers_memory;
6180 :
6181 19752704 : if (stmt_code == GIMPLE_ASSIGN)
6182 : {
6183 14565882 : tree base;
6184 14565882 : op0 = gimple_assign_lhs (stmt);
6185 14565882 : op1 = gimple_assign_rhs1 (stmt);
6186 :
6187 14565882 : if (DECL_P (op1)
6188 14565882 : || (REFERENCE_CLASS_P (op1)
6189 6967231 : && (base = get_base_address (op1))
6190 6967231 : && TREE_CODE (base) != SSA_NAME
6191 6967185 : && !is_gimple_min_invariant (base)))
6192 : {
6193 7863474 : ref.ref = op1;
6194 7863474 : ref.is_read = true;
6195 7863474 : ref.is_conditional_in_stmt = false;
6196 7863474 : references->safe_push (ref);
6197 : }
6198 : }
6199 5186822 : else if (stmt_code == GIMPLE_CALL)
6200 : {
6201 4260987 : unsigned i = 0, n;
6202 4260987 : tree ptr, type;
6203 4260987 : unsigned int align;
6204 :
6205 4260987 : ref.is_read = false;
6206 4260987 : if (gimple_call_internal_p (stmt))
6207 80610 : switch (gimple_call_internal_fn (stmt))
6208 : {
6209 2260 : case IFN_MASK_LOAD:
6210 2260 : if (gimple_call_lhs (stmt) == NULL_TREE)
6211 : break;
6212 2260 : ref.is_read = true;
6213 : /* FALLTHRU */
6214 4237 : case IFN_MASK_STORE:
6215 4237 : ptr = build_int_cst (TREE_TYPE (gimple_call_arg (stmt, 1)), 0);
6216 4237 : align = tree_to_shwi (gimple_call_arg (stmt, 1));
6217 4237 : if (ref.is_read)
6218 2260 : type = TREE_TYPE (gimple_call_lhs (stmt));
6219 : else
6220 1977 : type = TREE_TYPE (gimple_call_arg (stmt, 3));
6221 4237 : if (TYPE_ALIGN (type) != align)
6222 1580 : type = build_aligned_type (type, align);
6223 4237 : ref.is_conditional_in_stmt = true;
6224 4237 : ref.ref = fold_build2 (MEM_REF, type, gimple_call_arg (stmt, 0),
6225 : ptr);
6226 4237 : references->safe_push (ref);
6227 4237 : return false;
6228 : case IFN_MASK_CALL:
6229 4256750 : i = 1;
6230 : gcc_fallthrough ();
6231 : default:
6232 : break;
6233 : }
6234 :
6235 4256750 : op0 = gimple_call_lhs (stmt);
6236 4256750 : n = gimple_call_num_args (stmt);
6237 17269438 : for (; i < n; i++)
6238 : {
6239 8755938 : op1 = gimple_call_arg (stmt, i);
6240 :
6241 8755938 : if (DECL_P (op1)
6242 8755938 : || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
6243 : {
6244 518295 : ref.ref = op1;
6245 518295 : ref.is_read = true;
6246 518295 : ref.is_conditional_in_stmt = false;
6247 518295 : references->safe_push (ref);
6248 : }
6249 : }
6250 : }
6251 : else
6252 : return clobbers_memory;
6253 :
6254 18822632 : if (op0
6255 18822632 : && (DECL_P (op0)
6256 15285821 : || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
6257 : {
6258 7547875 : ref.ref = op0;
6259 7547875 : ref.is_read = false;
6260 7547875 : ref.is_conditional_in_stmt = false;
6261 7547875 : references->safe_push (ref);
6262 : }
6263 : return clobbers_memory;
6264 : }
6265 :
6266 :
6267 : /* Returns true if the loop-nest has any data reference. */
6268 :
6269 : bool
6270 754 : loop_nest_has_data_refs (loop_p loop)
6271 : {
6272 754 : basic_block *bbs = get_loop_body (loop);
6273 754 : auto_vec<data_ref_loc, 3> references;
6274 :
6275 1003 : for (unsigned i = 0; i < loop->num_nodes; i++)
6276 : {
6277 933 : basic_block bb = bbs[i];
6278 933 : gimple_stmt_iterator bsi;
6279 :
6280 3220 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
6281 : {
6282 2038 : gimple *stmt = gsi_stmt (bsi);
6283 2038 : get_references_in_stmt (stmt, &references);
6284 2038 : if (references.length ())
6285 : {
6286 684 : free (bbs);
6287 684 : return true;
6288 : }
6289 : }
6290 : }
6291 70 : free (bbs);
6292 70 : return false;
6293 754 : }
6294 :
6295 : /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
6296 : reference, returns false, otherwise returns true. NEST is the outermost
6297 : loop of the loop nest in which the references should be analyzed.
6298 : RECORD_NOWRAP_ASSUMPTIONS determines whether or not no-wrap assumptions
6299 : are allowed and recorded. */
6300 :
6301 : opt_result
6302 51604302 : find_data_references_in_stmt (class loop *nest, gimple *stmt,
6303 : vec<data_reference_p> *datarefs,
6304 : bool record_nowrap_assumptions)
6305 : {
6306 51604302 : auto_vec<data_ref_loc, 2> references;
6307 51604302 : data_reference_p dr;
6308 :
6309 51604302 : if (get_references_in_stmt (stmt, &references))
6310 4281779 : return opt_result::failure_at (stmt, "statement clobbers memory: %G",
6311 : stmt);
6312 :
6313 157116596 : for (const data_ref_loc &ref : references)
6314 : {
6315 15149027 : dr = create_data_ref (nest ? loop_preheader_edge (nest) : NULL,
6316 15149027 : loop_containing_stmt (stmt), ref.ref,
6317 15149027 : stmt, ref.is_read, ref.is_conditional_in_stmt,
6318 : record_nowrap_assumptions);
6319 15149027 : gcc_assert (dr != NULL);
6320 15149027 : datarefs->safe_push (dr);
6321 : }
6322 :
6323 47322523 : return opt_result::success ();
6324 51604302 : }
6325 :
6326 : /* Stores the data references in STMT to DATAREFS. If there is an
6327 : unanalyzable reference, returns false, otherwise returns true.
6328 : NEST is the outermost loop of the loop nest in which the references
6329 : should be instantiated, LOOP is the loop in which the references
6330 : should be analyzed. */
6331 :
6332 : bool
6333 14149 : graphite_find_data_references_in_stmt (edge nest, loop_p loop, gimple *stmt,
6334 : vec<data_reference_p> *datarefs)
6335 : {
6336 14149 : auto_vec<data_ref_loc, 2> references;
6337 14149 : bool ret = true;
6338 14149 : data_reference_p dr;
6339 :
6340 14149 : if (get_references_in_stmt (stmt, &references))
6341 : return false;
6342 :
6343 45297 : for (const data_ref_loc &ref : references)
6344 : {
6345 5724 : dr = create_data_ref (nest, loop, ref.ref, stmt, ref.is_read,
6346 2862 : ref.is_conditional_in_stmt);
6347 2862 : gcc_assert (dr != NULL);
6348 2862 : datarefs->safe_push (dr);
6349 : }
6350 :
6351 : return ret;
6352 14149 : }
6353 :
6354 : /* Search the data references in LOOP, and record the information into
6355 : DATAREFS. Returns chrec_dont_know when failing to analyze a
6356 : difficult case, returns NULL_TREE otherwise. */
6357 :
6358 : tree
6359 2762302 : find_data_references_in_bb (class loop *loop, basic_block bb,
6360 : vec<data_reference_p> *datarefs)
6361 : {
6362 2762302 : gimple_stmt_iterator bsi;
6363 :
6364 23370658 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
6365 : {
6366 18342746 : gimple *stmt = gsi_stmt (bsi);
6367 :
6368 18342746 : if (!find_data_references_in_stmt (loop, stmt, datarefs))
6369 : {
6370 496692 : struct data_reference *res;
6371 496692 : res = XCNEW (struct data_reference);
6372 496692 : datarefs->safe_push (res);
6373 :
6374 496692 : return chrec_dont_know;
6375 : }
6376 : }
6377 :
6378 : return NULL_TREE;
6379 : }
6380 :
6381 : /* Search the data references in LOOP, and record the information into
6382 : DATAREFS. Returns chrec_dont_know when failing to analyze a
6383 : difficult case, returns NULL_TREE otherwise.
6384 :
6385 : TODO: This function should be made smarter so that it can handle address
6386 : arithmetic as if they were array accesses, etc. */
6387 :
6388 : tree
6389 838067 : find_data_references_in_loop (class loop *loop,
6390 : vec<data_reference_p> *datarefs)
6391 : {
6392 838067 : basic_block bb, *bbs;
6393 838067 : unsigned int i;
6394 :
6395 838067 : bbs = get_loop_body_in_dom_order (loop);
6396 :
6397 3725363 : for (i = 0; i < loop->num_nodes; i++)
6398 : {
6399 2348125 : bb = bbs[i];
6400 :
6401 2348125 : if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
6402 : {
6403 298896 : free (bbs);
6404 298896 : return chrec_dont_know;
6405 : }
6406 : }
6407 539171 : free (bbs);
6408 :
6409 539171 : return NULL_TREE;
6410 : }
6411 :
6412 : /* Return the alignment in bytes that DRB is guaranteed to have at all
6413 : times. */
6414 :
6415 : unsigned int
6416 489281 : dr_alignment (innermost_loop_behavior *drb)
6417 : {
6418 : /* Get the alignment of BASE_ADDRESS + INIT. */
6419 489281 : unsigned int alignment = drb->base_alignment;
6420 489281 : unsigned int misalignment = (drb->base_misalignment
6421 489281 : + TREE_INT_CST_LOW (drb->init));
6422 489281 : if (misalignment != 0)
6423 214517 : alignment = MIN (alignment, misalignment & -misalignment);
6424 :
6425 : /* Cap it to the alignment of OFFSET. */
6426 489281 : if (!integer_zerop (drb->offset))
6427 37916 : alignment = MIN (alignment, drb->offset_alignment);
6428 :
6429 : /* Cap it to the alignment of STEP. */
6430 489281 : if (!integer_zerop (drb->step))
6431 301359 : alignment = MIN (alignment, drb->step_alignment);
6432 :
6433 489281 : return alignment;
6434 : }
6435 :
6436 : /* If BASE is a pointer-typed SSA name, try to find the object that it
6437 : is based on. Return this object X on success and store the alignment
6438 : in bytes of BASE - &X in *ALIGNMENT_OUT. */
6439 :
6440 : static tree
6441 773738 : get_base_for_alignment_1 (tree base, unsigned int *alignment_out)
6442 : {
6443 773738 : if (TREE_CODE (base) != SSA_NAME || !POINTER_TYPE_P (TREE_TYPE (base)))
6444 : return NULL_TREE;
6445 :
6446 381664 : gimple *def = SSA_NAME_DEF_STMT (base);
6447 381664 : base = analyze_scalar_evolution (loop_containing_stmt (def), base);
6448 :
6449 : /* Peel chrecs and record the minimum alignment preserved by
6450 : all steps. */
6451 381664 : unsigned int alignment = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
6452 776380 : while (TREE_CODE (base) == POLYNOMIAL_CHREC)
6453 : {
6454 13052 : unsigned int step_alignment = highest_pow2_factor (CHREC_RIGHT (base));
6455 13052 : alignment = MIN (alignment, step_alignment);
6456 13052 : base = CHREC_LEFT (base);
6457 : }
6458 :
6459 : /* Punt if the expression is too complicated to handle. */
6460 381664 : if (tree_contains_chrecs (base, NULL) || !POINTER_TYPE_P (TREE_TYPE (base)))
6461 : return NULL_TREE;
6462 :
6463 : /* The only useful cases are those for which a dereference folds to something
6464 : other than an INDIRECT_REF. */
6465 381622 : tree ref_type = TREE_TYPE (TREE_TYPE (base));
6466 381622 : tree ref = fold_indirect_ref_1 (UNKNOWN_LOCATION, ref_type, base);
6467 381622 : if (!ref)
6468 : return NULL_TREE;
6469 :
6470 : /* Analyze the base to which the steps we peeled were applied. */
6471 2648 : poly_int64 bitsize, bitpos, bytepos;
6472 2648 : machine_mode mode;
6473 2648 : int unsignedp, reversep, volatilep;
6474 2648 : tree offset;
6475 2648 : base = get_inner_reference (ref, &bitsize, &bitpos, &offset, &mode,
6476 : &unsignedp, &reversep, &volatilep);
6477 2648 : if (!base || !multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
6478 : return NULL_TREE;
6479 :
6480 : /* Restrict the alignment to that guaranteed by the offsets. */
6481 2648 : unsigned int bytepos_alignment = known_alignment (bytepos);
6482 2648 : if (bytepos_alignment != 0)
6483 2489 : alignment = MIN (alignment, bytepos_alignment);
6484 2648 : if (offset)
6485 : {
6486 0 : unsigned int offset_alignment = highest_pow2_factor (offset);
6487 0 : alignment = MIN (alignment, offset_alignment);
6488 : }
6489 :
6490 2648 : *alignment_out = alignment;
6491 2648 : return base;
6492 : }
6493 :
6494 : /* Return the object whose alignment would need to be changed in order
6495 : to increase the alignment of ADDR. Store the maximum achievable
6496 : alignment in *MAX_ALIGNMENT. */
6497 :
6498 : tree
6499 773738 : get_base_for_alignment (tree addr, unsigned int *max_alignment)
6500 : {
6501 773738 : tree base = get_base_for_alignment_1 (addr, max_alignment);
6502 773738 : if (base)
6503 : return base;
6504 :
6505 771090 : if (TREE_CODE (addr) == ADDR_EXPR)
6506 292080 : addr = TREE_OPERAND (addr, 0);
6507 771090 : *max_alignment = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
6508 771090 : return addr;
6509 : }
6510 :
6511 : /* Recursive helper function. */
6512 :
6513 : static bool
6514 146728 : find_loop_nest_1 (class loop *loop, vec<loop_p> *loop_nest)
6515 : {
6516 : /* Inner loops of the nest should not contain siblings. Example:
6517 : when there are two consecutive loops,
6518 :
6519 : | loop_0
6520 : | loop_1
6521 : | A[{0, +, 1}_1]
6522 : | endloop_1
6523 : | loop_2
6524 : | A[{0, +, 1}_2]
6525 : | endloop_2
6526 : | endloop_0
6527 :
6528 : the dependence relation cannot be captured by the distance
6529 : abstraction. */
6530 146728 : if (loop->next)
6531 : return false;
6532 :
6533 123367 : loop_nest->safe_push (loop);
6534 123367 : if (loop->inner)
6535 42980 : return find_loop_nest_1 (loop->inner, loop_nest);
6536 : return true;
6537 : }
6538 :
6539 : /* Return false when the LOOP is not well nested. Otherwise return
6540 : true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
6541 : contain the loops from the outermost to the innermost, as they will
6542 : appear in the classic distance vector. */
6543 :
6544 : bool
6545 1059035 : find_loop_nest (class loop *loop, vec<loop_p> *loop_nest)
6546 : {
6547 1059035 : loop_nest->safe_push (loop);
6548 1059035 : if (loop->inner)
6549 103748 : return find_loop_nest_1 (loop->inner, loop_nest);
6550 : return true;
6551 : }
6552 :
6553 : /* Returns true when the data dependences have been computed, false otherwise.
6554 : Given a loop nest LOOP, the following vectors are returned:
6555 : DATAREFS is initialized to all the array elements contained in this loop,
6556 : DEPENDENCE_RELATIONS contains the relations between the data references.
6557 : Compute read-read and self relations if
6558 : COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
6559 :
6560 : bool
6561 421174 : compute_data_dependences_for_loop (class loop *loop,
6562 : bool compute_self_and_read_read_dependences,
6563 : vec<loop_p> *loop_nest,
6564 : vec<data_reference_p> *datarefs,
6565 : vec<ddr_p> *dependence_relations)
6566 : {
6567 421174 : bool res = true;
6568 :
6569 421174 : memset (&dependence_stats, 0, sizeof (dependence_stats));
6570 :
6571 : /* If the loop nest is not well formed, or one of the data references
6572 : is not computable, give up without spending time to compute other
6573 : dependences. */
6574 421174 : if (!loop
6575 421174 : || !find_loop_nest (loop, loop_nest)
6576 421172 : || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
6577 695456 : || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
6578 : compute_self_and_read_read_dependences))
6579 : res = false;
6580 :
6581 421174 : if (dump_file && (dump_flags & TDF_STATS))
6582 : {
6583 157 : fprintf (dump_file, "Dependence tester statistics:\n");
6584 :
6585 157 : fprintf (dump_file, "Number of dependence tests: %d\n",
6586 : dependence_stats.num_dependence_tests);
6587 157 : fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
6588 : dependence_stats.num_dependence_dependent);
6589 157 : fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
6590 : dependence_stats.num_dependence_independent);
6591 157 : fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
6592 : dependence_stats.num_dependence_undetermined);
6593 :
6594 157 : fprintf (dump_file, "Number of subscript tests: %d\n",
6595 : dependence_stats.num_subscript_tests);
6596 157 : fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
6597 : dependence_stats.num_subscript_undetermined);
6598 157 : fprintf (dump_file, "Number of same subscript function: %d\n",
6599 : dependence_stats.num_same_subscript_function);
6600 :
6601 157 : fprintf (dump_file, "Number of ziv tests: %d\n",
6602 : dependence_stats.num_ziv);
6603 157 : fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
6604 : dependence_stats.num_ziv_dependent);
6605 157 : fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
6606 : dependence_stats.num_ziv_independent);
6607 157 : fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
6608 : dependence_stats.num_ziv_unimplemented);
6609 :
6610 157 : fprintf (dump_file, "Number of siv tests: %d\n",
6611 : dependence_stats.num_siv);
6612 157 : fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
6613 : dependence_stats.num_siv_dependent);
6614 157 : fprintf (dump_file, "Number of siv tests returning independent: %d\n",
6615 : dependence_stats.num_siv_independent);
6616 157 : fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
6617 : dependence_stats.num_siv_unimplemented);
6618 :
6619 157 : fprintf (dump_file, "Number of miv tests: %d\n",
6620 : dependence_stats.num_miv);
6621 157 : fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
6622 : dependence_stats.num_miv_dependent);
6623 157 : fprintf (dump_file, "Number of miv tests returning independent: %d\n",
6624 : dependence_stats.num_miv_independent);
6625 157 : fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
6626 : dependence_stats.num_miv_unimplemented);
6627 : }
6628 :
6629 421174 : return res;
6630 : }
6631 :
6632 : /* Free the memory used by a data dependence relation DDR. */
6633 :
6634 : void
6635 13574339 : free_dependence_relation (struct data_dependence_relation *ddr)
6636 : {
6637 13574339 : if (ddr == NULL)
6638 : return;
6639 :
6640 13574339 : if (DDR_SUBSCRIPTS (ddr).exists ())
6641 931897 : free_subscripts (DDR_SUBSCRIPTS (ddr));
6642 13574339 : DDR_DIST_VECTS (ddr).release ();
6643 13574339 : DDR_DIR_VECTS (ddr).release ();
6644 :
6645 13574339 : free (ddr);
6646 : }
6647 :
6648 : /* Free the memory used by the data dependence relations from
6649 : DEPENDENCE_RELATIONS. */
6650 :
6651 : void
6652 2946881 : free_dependence_relations (vec<ddr_p>& dependence_relations)
6653 : {
6654 9607984 : for (data_dependence_relation *ddr : dependence_relations)
6655 5444979 : if (ddr)
6656 5444979 : free_dependence_relation (ddr);
6657 :
6658 2946881 : dependence_relations.release ();
6659 2946881 : }
6660 :
6661 : /* Free the memory used by the data references from DATAREFS. */
6662 :
6663 : void
6664 3618394 : free_data_refs (vec<data_reference_p>& datarefs)
6665 : {
6666 21649047 : for (data_reference *dr : datarefs)
6667 13539141 : free_data_ref (dr);
6668 3618394 : datarefs.release ();
6669 3618394 : }
6670 :
6671 : /* Reset DR_INNERMOST, DR_NOWRAP_BOUND, and access functions of DR. */
6672 :
6673 : void
6674 103 : dr_reset (data_reference *dr)
6675 : {
6676 103 : DR_INNERMOST (dr) = innermost_loop_behavior ();
6677 103 : DR_NOWRAP_BOUND (dr) = NULL_TREE;
6678 103 : dr->indices.access_fns.release ();
6679 103 : }
6680 :
6681 : /* Common routine implementing both dr_direction_indicator and
6682 : dr_zero_step_indicator. Return USEFUL_MIN if the indicator is known
6683 : to be >= USEFUL_MIN and -1 if the indicator is known to be negative.
6684 : Return the step as the indicator otherwise. */
6685 :
6686 : static tree
6687 68066 : dr_step_indicator (struct data_reference *dr, int useful_min)
6688 : {
6689 68066 : tree step = DR_STEP (dr);
6690 68066 : if (!step)
6691 : return NULL_TREE;
6692 68066 : STRIP_NOPS (step);
6693 : /* Look for cases where the step is scaled by a positive constant
6694 : integer, which will often be the access size. If the multiplication
6695 : doesn't change the sign (due to overflow effects) then we can
6696 : test the unscaled value instead. */
6697 68066 : if (TREE_CODE (step) == MULT_EXPR
6698 5160 : && TREE_CODE (TREE_OPERAND (step, 1)) == INTEGER_CST
6699 73170 : && tree_int_cst_sgn (TREE_OPERAND (step, 1)) > 0)
6700 : {
6701 5104 : tree factor = TREE_OPERAND (step, 1);
6702 5104 : step = TREE_OPERAND (step, 0);
6703 :
6704 : /* Strip widening and truncating conversions as well as nops. */
6705 1226 : if (CONVERT_EXPR_P (step)
6706 5104 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (step, 0))))
6707 3878 : step = TREE_OPERAND (step, 0);
6708 5104 : tree type = TREE_TYPE (step);
6709 :
6710 : /* Get the range of step values that would not cause overflow. */
6711 10208 : widest_int minv = (wi::to_widest (TYPE_MIN_VALUE (ssizetype))
6712 5104 : / wi::to_widest (factor));
6713 10208 : widest_int maxv = (wi::to_widest (TYPE_MAX_VALUE (ssizetype))
6714 5104 : / wi::to_widest (factor));
6715 :
6716 : /* Get the range of values that the unconverted step actually has. */
6717 5104 : wide_int step_min, step_max;
6718 5104 : int_range_max vr;
6719 5104 : if (TREE_CODE (step) != SSA_NAME
6720 10100 : || !get_range_query (cfun)->range_of_expr (vr, step)
6721 10154 : || vr.undefined_p ())
6722 : {
6723 54 : step_min = wi::to_wide (TYPE_MIN_VALUE (type));
6724 54 : step_max = wi::to_wide (TYPE_MAX_VALUE (type));
6725 : }
6726 : else
6727 : {
6728 5050 : step_min = vr.lower_bound ();
6729 5050 : step_max = vr.upper_bound ();
6730 : }
6731 :
6732 : /* Check whether the unconverted step has an acceptable range. */
6733 5104 : signop sgn = TYPE_SIGN (type);
6734 10208 : if (wi::les_p (minv, widest_int::from (step_min, sgn))
6735 13356 : && wi::ges_p (maxv, widest_int::from (step_max, sgn)))
6736 : {
6737 1565 : if (wi::ge_p (step_min, useful_min, sgn))
6738 440 : return ssize_int (useful_min);
6739 1125 : else if (wi::lt_p (step_max, 0, sgn))
6740 0 : return ssize_int (-1);
6741 : else
6742 1125 : return fold_convert (ssizetype, step);
6743 : }
6744 5104 : }
6745 66501 : return DR_STEP (dr);
6746 : }
6747 :
6748 : /* Return a value that is negative iff DR has a negative step. */
6749 :
6750 : tree
6751 11789 : dr_direction_indicator (struct data_reference *dr)
6752 : {
6753 11789 : return dr_step_indicator (dr, 0);
6754 : }
6755 :
6756 : /* Return a value that is zero iff DR has a zero step. */
6757 :
6758 : tree
6759 56277 : dr_zero_step_indicator (struct data_reference *dr)
6760 : {
6761 56277 : return dr_step_indicator (dr, 1);
6762 : }
6763 :
6764 : /* Return true if DR is known to have a nonnegative (but possibly zero)
6765 : step. */
6766 :
6767 : bool
6768 4723 : dr_known_forward_stride_p (struct data_reference *dr)
6769 : {
6770 4723 : tree indicator = dr_direction_indicator (dr);
6771 4723 : tree neg_step_val = fold_binary (LT_EXPR, boolean_type_node,
6772 : fold_convert (ssizetype, indicator),
6773 : ssize_int (0));
6774 4723 : return neg_step_val && integer_zerop (neg_step_val);
6775 : }
|