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 2074 : tree_fold_divides_p (const_tree a, const_tree b)
140 : {
141 2074 : gcc_assert (TREE_CODE (a) == INTEGER_CST);
142 2074 : gcc_assert (TREE_CODE (b) == INTEGER_CST);
143 2074 : 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 1685877 : int_divides_p (lambda_int a, lambda_int b)
150 : {
151 1685877 : return ((b % a) == 0);
152 : }
153 :
154 : /* Return true if reference REF contains a union access. */
155 :
156 : static bool
157 468256 : ref_contains_union_access_p (tree ref)
158 : {
159 516594 : while (handled_component_p (ref))
160 : {
161 106416 : ref = TREE_OPERAND (ref, 0);
162 212832 : if (TREE_CODE (TREE_TYPE (ref)) == UNION_TYPE
163 106416 : || 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 3480 : dump_data_reference (FILE *outf,
218 : struct data_reference *dr)
219 : {
220 3480 : unsigned int i;
221 :
222 3480 : fprintf (outf, "#(Data Ref: \n");
223 3480 : fprintf (outf, "# bb: %d \n", gimple_bb (DR_STMT (dr))->index);
224 3480 : fprintf (outf, "# stmt: ");
225 3480 : print_gimple_stmt (outf, DR_STMT (dr), 0);
226 3480 : fprintf (outf, "# ref: ");
227 3480 : print_generic_stmt (outf, DR_REF (dr));
228 3480 : fprintf (outf, "# base_object: ");
229 3480 : print_generic_stmt (outf, DR_BASE_OBJECT (dr));
230 :
231 10786 : for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
232 : {
233 3826 : fprintf (outf, "# Access function %d: ", i);
234 3826 : print_generic_stmt (outf, DR_ACCESS_FN (dr, i));
235 : }
236 3480 : fprintf (outf, "#)\n");
237 3480 : }
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 32790 : dump_affine_function (FILE *outf, affine_fn fn)
261 : {
262 32790 : unsigned i;
263 32790 : tree coef;
264 :
265 32790 : print_generic_expr (outf, fn[0], TDF_SLIM);
266 69298 : for (i = 1; fn.iterate (i, &coef); i++)
267 : {
268 3718 : fprintf (outf, " + ");
269 3718 : print_generic_expr (outf, coef, TDF_SLIM);
270 3718 : fprintf (outf, " * x_%u", i);
271 : }
272 32790 : }
273 :
274 : /* Dumps the conflict function CF to the file OUTF. */
275 :
276 : DEBUG_FUNCTION void
277 164362 : dump_conflict_function (FILE *outf, conflict_function *cf)
278 : {
279 164362 : unsigned i;
280 :
281 164362 : if (cf->n == NO_DEPENDENCE)
282 125434 : fprintf (outf, "no dependence");
283 38928 : else if (cf->n == NOT_KNOWN)
284 6138 : fprintf (outf, "not known");
285 : else
286 : {
287 65580 : for (i = 0; i < cf->n; i++)
288 : {
289 32790 : if (i != 0)
290 0 : fprintf (outf, " ");
291 32790 : fprintf (outf, "[");
292 32790 : dump_affine_function (outf, cf->fns[i]);
293 32790 : fprintf (outf, "]");
294 : }
295 : }
296 164362 : }
297 :
298 : /* Dump function for a SUBSCRIPT structure. */
299 :
300 : DEBUG_FUNCTION void
301 838 : dump_subscript (FILE *outf, struct subscript *subscript)
302 : {
303 838 : conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
304 :
305 838 : fprintf (outf, "\n (subscript \n");
306 838 : fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
307 838 : dump_conflict_function (outf, cf);
308 838 : if (CF_NONTRIVIAL_P (cf))
309 : {
310 838 : tree last_iteration = SUB_LAST_CONFLICT (subscript);
311 838 : fprintf (outf, "\n last_conflict: ");
312 838 : print_generic_expr (outf, last_iteration);
313 : }
314 :
315 838 : cf = SUB_CONFLICTS_IN_B (subscript);
316 838 : fprintf (outf, "\n iterations_that_access_an_element_twice_in_B: ");
317 838 : dump_conflict_function (outf, cf);
318 838 : if (CF_NONTRIVIAL_P (cf))
319 : {
320 838 : tree last_iteration = SUB_LAST_CONFLICT (subscript);
321 838 : fprintf (outf, "\n last_conflict: ");
322 838 : print_generic_expr (outf, last_iteration);
323 : }
324 :
325 838 : fprintf (outf, "\n (Subscript distance: ");
326 838 : print_generic_expr (outf, SUB_DISTANCE (subscript));
327 838 : fprintf (outf, " ))\n");
328 838 : }
329 :
330 : /* Print the classic direction vector DIRV to OUTF. */
331 :
332 : DEBUG_FUNCTION void
333 777 : print_direction_vector (FILE *outf,
334 : lambda_vector dirv,
335 : int length)
336 : {
337 777 : int eq;
338 :
339 1683 : for (eq = 0; eq < length; eq++)
340 : {
341 906 : enum data_dependence_direction dir = ((enum data_dependence_direction)
342 906 : dirv[eq]);
343 :
344 906 : switch (dir)
345 : {
346 139 : case dir_positive:
347 139 : fprintf (outf, " +");
348 139 : break;
349 6 : case dir_negative:
350 6 : fprintf (outf, " -");
351 6 : break;
352 761 : case dir_equal:
353 761 : fprintf (outf, " =");
354 761 : 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 777 : fprintf (outf, "\n");
373 777 : }
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 4883 : print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
389 : {
390 4883 : int i;
391 :
392 10178 : for (i = 0; i < n; i++)
393 5295 : fprintf (outfile, HOST_WIDE_INT_PRINT_DEC " ", vector[i]);
394 4883 : fprintf (outfile, "\n");
395 4883 : }
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 1582 : dump_data_dependence_relation (FILE *outf, const data_dependence_relation *ddr)
411 : {
412 1582 : struct data_reference *dra, *drb;
413 :
414 1582 : fprintf (outf, "(Data Dep: \n");
415 :
416 1582 : 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 1183 : dra = DDR_A (ddr);
436 1183 : drb = DDR_B (ddr);
437 1183 : dump_data_reference (outf, dra);
438 1183 : dump_data_reference (outf, drb);
439 :
440 1183 : if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
441 426 : fprintf (outf, " (no dependence)\n");
442 :
443 757 : else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
444 : {
445 : unsigned int i;
446 : class loop *loopi;
447 :
448 : subscript *sub;
449 1595 : FOR_EACH_VEC_ELT (DDR_SUBSCRIPTS (ddr), i, sub)
450 : {
451 838 : fprintf (outf, " access_fn_A: ");
452 838 : print_generic_stmt (outf, SUB_ACCESS_FN (sub, 0));
453 838 : fprintf (outf, " access_fn_B: ");
454 838 : print_generic_stmt (outf, SUB_ACCESS_FN (sub, 1));
455 838 : dump_subscript (outf, sub);
456 : }
457 :
458 757 : fprintf (outf, " loop nest: (");
459 2374 : FOR_EACH_VEC_ELT (DDR_LOOP_NEST (ddr), i, loopi)
460 860 : fprintf (outf, "%d ", loopi->num);
461 757 : fprintf (outf, ")\n");
462 :
463 3820 : for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
464 : {
465 777 : fprintf (outf, " distance_vector: ");
466 777 : print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
467 1554 : DDR_NB_LOOPS (ddr));
468 : }
469 :
470 1534 : for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
471 : {
472 777 : fprintf (outf, " direction_vector: ");
473 777 : print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
474 1554 : DDR_NB_LOOPS (ddr));
475 : }
476 : }
477 :
478 1183 : 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 307 : dump_data_dependence_relations (FILE *file, const vec<ddr_p> &ddrs)
493 : {
494 2473 : for (auto ddr : ddrs)
495 1582 : dump_data_dependence_relation (file, ddr);
496 307 : }
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 4613502 : compute_distributive_range (tree type, irange &op0_range,
592 : tree_code code, irange &op1_range,
593 : tree *off, irange *result_range)
594 : {
595 4613502 : gcc_assert (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_TRAPS (type));
596 4613502 : if (result_range)
597 : {
598 1064595 : range_op_handler op (code);
599 1064595 : 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 4613502 : 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 276163 : 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 103282 : range_cast (op0_range, ssizetype);
643 103282 : range_cast (op1_range, ssizetype);
644 103282 : int_range_max wide_range;
645 103282 : range_op_handler op (code);
646 103282 : bool saved_flag_wrapv = flag_wrapv;
647 103282 : flag_wrapv = 1;
648 103282 : if (!op.fold_range (wide_range, ssizetype, op0_range, op1_range))
649 0 : wide_range.set_varying (ssizetype);;
650 103282 : flag_wrapv = saved_flag_wrapv;
651 103282 : if (wide_range.num_pairs () != 1
652 103282 : || wide_range.varying_p () || wide_range.undefined_p ())
653 : return false;
654 :
655 82852 : wide_int lb = wide_range.lower_bound ();
656 82852 : 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 82852 : unsigned int precision = TYPE_PRECISION (type);
661 82852 : 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 82852 : wide_int upper_bits = wi::mask (precision, true, lb.get_precision ());
668 82852 : lb &= upper_bits;
669 82852 : ub &= upper_bits;
670 82852 : 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 24746 : *off = wide_int_to_tree (ssizetype, wi::to_wide (*off) - lb);
677 24746 : return true;
678 103282 : }
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 2618514 : nop_conversion_for_offset_p (tree to_type, tree from_type, irange &range)
686 : {
687 2618514 : 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 2618514 : 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 88782 : if (TYPE_PRECISION (from_type) < TYPE_PRECISION (to_type)
699 88782 : && (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 73825 : return range_fits_type_p (&range, TYPE_PRECISION (to_type),
712 147650 : 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 59535527 : 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 59535527 : tree var0, var1;
759 59535527 : tree off0, off1;
760 59535527 : int_range_max op0_range, op1_range;
761 :
762 59535527 : *var = NULL_TREE;
763 59535527 : *off = NULL_TREE;
764 :
765 59535527 : if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
766 : return false;
767 :
768 59534905 : if (TREE_CODE (op0) == SSA_NAME
769 59534905 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
770 : return false;
771 59534516 : if (op1
772 7593595 : && TREE_CODE (op1) == SSA_NAME
773 61917174 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))
774 : return false;
775 :
776 59534516 : switch (code)
777 : {
778 17832760 : case INTEGER_CST:
779 17832760 : *var = size_int (0);
780 17832760 : *off = fold_convert (ssizetype, op0);
781 17832760 : if (result_range)
782 : {
783 1435824 : wide_int w = wi::to_wide (op0);
784 1435824 : result_range->set (TREE_TYPE (op0), w, w);
785 1435824 : }
786 : return true;
787 :
788 2177960 : case POINTER_PLUS_EXPR:
789 2177960 : split_constant_offset (op0, &var0, &off0, nullptr, cache, limit);
790 2177960 : split_constant_offset (op1, &var1, &off1, nullptr, cache, limit);
791 2177960 : *var = fold_build2 (POINTER_PLUS_EXPR, type, var0, var1);
792 2177960 : *off = size_binop (PLUS_EXPR, off0, off1);
793 2177960 : return true;
794 :
795 2452212 : case PLUS_EXPR:
796 2452212 : case MINUS_EXPR:
797 2452212 : split_constant_offset (op0, &var0, &off0, &op0_range, cache, limit);
798 2452212 : split_constant_offset (op1, &var1, &off1, &op1_range, cache, limit);
799 2452212 : *off = size_binop (code, off0, off1);
800 2452212 : if (!compute_distributive_range (type, op0_range, code, op1_range,
801 : off, result_range))
802 : return false;
803 2393938 : *var = fold_build2 (code, sizetype, var0, var1);
804 2393938 : return true;
805 :
806 2619908 : case MULT_EXPR:
807 2619908 : if (TREE_CODE (op1) != INTEGER_CST)
808 : return false;
809 :
810 2161290 : split_constant_offset (op0, &var0, &off0, &op0_range, cache, limit);
811 2161290 : op1_range.set (TREE_TYPE (op1), wi::to_wide (op1), wi::to_wide (op1));
812 2161290 : *off = size_binop (MULT_EXPR, off0, fold_convert (ssizetype, op1));
813 2161290 : if (!compute_distributive_range (type, op0_range, code, op1_range,
814 : off, result_range))
815 : return false;
816 2141028 : *var = fold_build2 (MULT_EXPR, sizetype, var0,
817 : fold_convert (sizetype, op1));
818 2141028 : return true;
819 :
820 10507942 : case ADDR_EXPR:
821 10507942 : {
822 10507942 : tree base, poffset;
823 10507942 : poly_int64 pbitsize, pbitpos, pbytepos;
824 10507942 : machine_mode pmode;
825 10507942 : int punsignedp, preversep, pvolatilep;
826 :
827 10507942 : op0 = TREE_OPERAND (op0, 0);
828 10507942 : base
829 10507942 : = get_inner_reference (op0, &pbitsize, &pbitpos, &poffset, &pmode,
830 : &punsignedp, &preversep, &pvolatilep);
831 :
832 21015884 : if (!multiple_p (pbitpos, BITS_PER_UNIT, &pbytepos))
833 : return false;
834 10507942 : base = build_fold_addr_expr (base);
835 10507942 : off0 = ssize_int (pbytepos);
836 :
837 10507942 : if (poffset)
838 : {
839 1532 : split_constant_offset (poffset, &poffset, &off1, nullptr,
840 : cache, limit);
841 1532 : off0 = size_binop (PLUS_EXPR, off0, off1);
842 1532 : base = fold_build_pointer_plus (base, poffset);
843 : }
844 :
845 10507942 : 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 21428701 : while (POINTER_TYPE_P (type))
860 10920759 : type = TREE_TYPE (type);
861 10507942 : if (int_size_in_bytes (type) < 0)
862 : return false;
863 :
864 10481835 : *var = var0;
865 10481835 : *off = off0;
866 10481835 : return true;
867 : }
868 :
869 16099697 : case SSA_NAME:
870 16099697 : {
871 16099697 : gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
872 16099697 : enum tree_code subcode;
873 :
874 16099697 : if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
875 : return false;
876 :
877 8762945 : subcode = gimple_assign_rhs_code (def_stmt);
878 :
879 : /* We are using a cache to avoid un-CSEing large amounts of code. */
880 8762945 : bool use_cache = false;
881 8762945 : if (!has_single_use (op0)
882 8762945 : && (subcode == POINTER_PLUS_EXPR
883 4526268 : || subcode == PLUS_EXPR
884 : || subcode == MINUS_EXPR
885 : || subcode == MULT_EXPR
886 : || subcode == ADDR_EXPR
887 : || CONVERT_EXPR_CODE_P (subcode)))
888 : {
889 2187846 : use_cache = true;
890 2187846 : bool existed;
891 2187846 : std::pair<tree, tree> &e = cache.get_or_insert (op0, &existed);
892 2187846 : if (existed)
893 : {
894 31827 : if (integer_zerop (e.second))
895 31827 : return false;
896 1197 : *var = e.first;
897 1197 : *off = e.second;
898 : /* The caller sets the range in this case. */
899 1197 : return true;
900 : }
901 2156019 : e = std::make_pair (op0, ssize_int (0));
902 : }
903 :
904 8731118 : if (*limit == 0)
905 : return false;
906 8730082 : --*limit;
907 :
908 8730082 : var0 = gimple_assign_rhs1 (def_stmt);
909 8730082 : var1 = gimple_assign_rhs2 (def_stmt);
910 :
911 8730082 : bool res = split_constant_offset_1 (type, var0, subcode, var1,
912 : var, off, nullptr, cache, limit);
913 8730082 : if (res && use_cache)
914 1939887 : *cache.get (op0) = std::make_pair (*var, *off);
915 : /* The caller sets the range in this case. */
916 : return res;
917 : }
918 4375143 : CASE_CONVERT:
919 4375143 : {
920 : /* We can only handle the following conversions:
921 :
922 : - Conversions from one pointer type to another pointer type.
923 :
924 : - Conversions from one non-trapping integral type to another
925 : non-trapping integral type. In this case, the recursive
926 : call makes sure that:
927 :
928 : (sizetype) OP0
929 :
930 : can be expressed as a sizetype operation involving VAR and OFF,
931 : and all we need to do is check whether:
932 :
933 : (sizetype) OP0 == (sizetype) (TYPE) OP0
934 :
935 : - Conversions from a non-trapping sizetype-size integral type to
936 : a like-sized pointer type. In this case, the recursive call
937 : makes sure that:
938 :
939 : (sizetype) OP0 == *VAR + (sizetype) *OFF
940 :
941 : and we can convert that to:
942 :
943 : POINTER_PLUS <(TYPE) *VAR, (sizetype) *OFF>
944 :
945 : - Conversions from a sizetype-sized pointer type to a like-sized
946 : non-trapping integral type. In this case, the recursive call
947 : makes sure that:
948 :
949 : OP0 == POINTER_PLUS <*VAR, (sizetype) *OFF>
950 :
951 : where the POINTER_PLUS and *VAR have the same precision as
952 : TYPE (and the same precision as sizetype). Then:
953 :
954 : (sizetype) (TYPE) OP0 == (sizetype) *VAR + (sizetype) *OFF. */
955 4375143 : tree itype = TREE_TYPE (op0);
956 4375143 : if ((POINTER_TYPE_P (itype)
957 3190979 : || (INTEGRAL_TYPE_P (itype) && !TYPE_OVERFLOW_TRAPS (itype)))
958 4374726 : && (POINTER_TYPE_P (type)
959 3136687 : || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_TRAPS (type)))
960 8749869 : && (POINTER_TYPE_P (type) == POINTER_TYPE_P (itype)
961 1090221 : || (TYPE_PRECISION (type) == TYPE_PRECISION (sizetype)
962 1090221 : && TYPE_PRECISION (itype) == TYPE_PRECISION (sizetype))))
963 : {
964 4374719 : if (POINTER_TYPE_P (type))
965 : {
966 1238032 : split_constant_offset (op0, var, off, nullptr, cache, limit);
967 1238032 : *var = fold_convert (type, *var);
968 : }
969 3136687 : else if (POINTER_TYPE_P (itype))
970 : {
971 518173 : split_constant_offset (op0, var, off, nullptr, cache, limit);
972 518173 : *var = fold_convert (sizetype, *var);
973 : }
974 : else
975 : {
976 2618514 : split_constant_offset (op0, var, off, &op0_range,
977 : cache, limit);
978 2618514 : if (!nop_conversion_for_offset_p (type, itype, op0_range))
979 : return false;
980 2566114 : if (result_range)
981 : {
982 1325020 : *result_range = op0_range;
983 1325020 : range_cast (*result_range, type);
984 : }
985 : }
986 : return true;
987 : }
988 : return false;
989 : }
990 :
991 : default:
992 : return false;
993 : }
994 59535527 : }
995 :
996 : /* If EXP has pointer type, try to express it as:
997 :
998 : POINTER_PLUS <*VAR, (sizetype) *OFF>
999 :
1000 : where:
1001 :
1002 : - *VAR has the same type as EXP
1003 : - *OFF is a constant of type ssizetype.
1004 :
1005 : If EXP has an integral type, try to express (sizetype) EXP as:
1006 :
1007 : *VAR + (sizetype) *OFF
1008 :
1009 : where:
1010 :
1011 : - *VAR has type sizetype
1012 : - *OFF is a constant of type ssizetype.
1013 :
1014 : If EXP_RANGE is nonnull, set it to the range of EXP.
1015 :
1016 : CACHE caches {*VAR, *OFF} pairs for SSA names that we've previously
1017 : visited. LIMIT counts down the number of SSA names that we are
1018 : allowed to process before giving up. */
1019 :
1020 : static void
1021 50805466 : split_constant_offset (tree exp, tree *var, tree *off, irange *exp_range,
1022 : hash_map<tree, std::pair<tree, tree> > &cache,
1023 : unsigned *limit)
1024 : {
1025 50805466 : tree type = TREE_TYPE (exp), op0, op1;
1026 50805466 : enum tree_code code;
1027 :
1028 50805466 : code = TREE_CODE (exp);
1029 50805466 : if (exp_range)
1030 : {
1031 9684228 : exp_range->set_varying (type);
1032 9684228 : if (code == SSA_NAME)
1033 : {
1034 5337450 : int_range_max vr;
1035 10674900 : get_range_query (cfun)->range_of_expr (vr, exp);
1036 5337450 : if (vr.undefined_p ())
1037 4941 : vr.set_varying (TREE_TYPE (exp));
1038 5337450 : tree vr_min, vr_max;
1039 5337450 : value_range_kind vr_kind = get_legacy_range (vr, vr_min, vr_max);
1040 5337450 : wide_int var_min = wi::to_wide (vr_min);
1041 5337450 : wide_int var_max = wi::to_wide (vr_max);
1042 5337450 : wide_int var_nonzero = get_nonzero_bits (exp);
1043 16012350 : vr_kind = intersect_range_with_nonzero_bits (vr_kind,
1044 : &var_min, &var_max,
1045 : var_nonzero,
1046 5337450 : TYPE_SIGN (type));
1047 : /* This check for VR_VARYING is here because the old code
1048 : using get_range_info would return VR_RANGE for the entire
1049 : domain, instead of VR_VARYING. The new code normalizes
1050 : full-domain ranges to VR_VARYING. */
1051 5337450 : if (vr_kind == VR_RANGE || vr_kind == VR_VARYING)
1052 5218442 : exp_range->set (type, var_min, var_max);
1053 5337450 : }
1054 : }
1055 :
1056 50805466 : if (!tree_is_chrec (exp)
1057 50805460 : && get_gimple_rhs_class (TREE_CODE (exp)) != GIMPLE_TERNARY_RHS)
1058 : {
1059 50805445 : extract_ops_from_tree (exp, &code, &op0, &op1);
1060 50805445 : if (split_constant_offset_1 (type, op0, code, op1, var, off,
1061 : exp_range, cache, limit))
1062 39351037 : return;
1063 : }
1064 :
1065 11454429 : *var = exp;
1066 11454429 : if (INTEGRAL_TYPE_P (type))
1067 3468635 : *var = fold_convert (sizetype, *var);
1068 11454429 : *off = ssize_int (0);
1069 :
1070 11454429 : int_range_max r;
1071 3153548 : if (exp_range && code != SSA_NAME
1072 115672 : && get_range_query (cfun)->range_of_expr (r, exp)
1073 11512265 : && !r.undefined_p ())
1074 57836 : *exp_range = r;
1075 11454429 : }
1076 :
1077 : /* Expresses EXP as VAR + OFF, where OFF is a constant. VAR has the same
1078 : type as EXP while OFF has type ssizetype. */
1079 :
1080 : void
1081 35007581 : split_constant_offset (tree exp, tree *var, tree *off)
1082 : {
1083 35007581 : unsigned limit = param_ssa_name_def_chain_limit;
1084 35007581 : static hash_map<tree, std::pair<tree, tree> > *cache;
1085 35007581 : if (!cache)
1086 81705 : cache = new hash_map<tree, std::pair<tree, tree> > (37);
1087 35007581 : split_constant_offset (exp, var, off, nullptr, *cache, &limit);
1088 35007581 : *var = fold_convert (TREE_TYPE (exp), *var);
1089 35007581 : cache->empty ();
1090 35007581 : }
1091 :
1092 : /* Returns the address ADDR of an object in a canonical shape (without nop
1093 : casts, and with type of pointer to the object). */
1094 :
1095 : static tree
1096 16475035 : canonicalize_base_object_address (tree addr)
1097 : {
1098 16475035 : tree orig = addr;
1099 :
1100 16475035 : STRIP_NOPS (addr);
1101 :
1102 : /* The base address may be obtained by casting from integer, in that case
1103 : keep the cast. */
1104 16475035 : if (!POINTER_TYPE_P (TREE_TYPE (addr)))
1105 : return orig;
1106 :
1107 16402421 : if (TREE_CODE (addr) != ADDR_EXPR)
1108 : return addr;
1109 :
1110 9782179 : return build_fold_addr_expr (TREE_OPERAND (addr, 0));
1111 : }
1112 :
1113 : /* Analyze the behavior of memory reference REF within STMT.
1114 : There are two modes:
1115 :
1116 : - BB analysis. In this case we simply split the address into base,
1117 : init and offset components, without reference to any containing loop.
1118 : The resulting base and offset are general expressions and they can
1119 : vary arbitrarily from one iteration of the containing loop to the next.
1120 : The step is always zero.
1121 :
1122 : - loop analysis. In this case we analyze the reference both wrt LOOP
1123 : and on the basis that the reference occurs (is "used") in LOOP;
1124 : see the comment above analyze_scalar_evolution_in_loop for more
1125 : information about this distinction. The base, init, offset and
1126 : step fields are all invariant in LOOP.
1127 :
1128 : Perform BB analysis if LOOP is null, or if LOOP is the function's
1129 : dummy outermost loop. In other cases perform loop analysis.
1130 :
1131 : Return true if the analysis succeeded and store the results in DRB if so.
1132 : BB analysis can only fail for bitfield or reversed-storage accesses. */
1133 :
1134 : opt_result
1135 17042667 : dr_analyze_innermost (innermost_loop_behavior *drb, tree ref,
1136 : class loop *loop, const gimple *stmt)
1137 : {
1138 17042667 : poly_int64 pbitsize, pbitpos;
1139 17042667 : tree base, poffset;
1140 17042667 : machine_mode pmode;
1141 17042667 : int punsignedp, preversep, pvolatilep;
1142 17042667 : affine_iv base_iv, offset_iv;
1143 17042667 : tree init, dinit, step;
1144 17042667 : bool in_loop = (loop && loop->num);
1145 :
1146 17042667 : if (dump_file && (dump_flags & TDF_DETAILS))
1147 69219 : fprintf (dump_file, "analyze_innermost: ");
1148 :
1149 17042667 : base = get_inner_reference (ref, &pbitsize, &pbitpos, &poffset, &pmode,
1150 : &punsignedp, &preversep, &pvolatilep);
1151 17042667 : gcc_assert (base != NULL_TREE);
1152 :
1153 17042667 : poly_int64 pbytepos;
1154 17042667 : if (!multiple_p (pbitpos, BITS_PER_UNIT, &pbytepos))
1155 39080 : return opt_result::failure_at (stmt,
1156 : "failed: bit offset alignment.\n");
1157 :
1158 17003587 : if (preversep)
1159 693 : return opt_result::failure_at (stmt,
1160 : "failed: reverse storage order.\n");
1161 :
1162 : /* Calculate the alignment and misalignment for the inner reference. */
1163 17002894 : unsigned int HOST_WIDE_INT bit_base_misalignment;
1164 17002894 : unsigned int bit_base_alignment;
1165 17002894 : get_object_alignment_1 (base, &bit_base_alignment, &bit_base_misalignment);
1166 :
1167 : /* There are no bitfield references remaining in BASE, so the values
1168 : we got back must be whole bytes. */
1169 17002894 : gcc_assert (bit_base_alignment % BITS_PER_UNIT == 0
1170 : && bit_base_misalignment % BITS_PER_UNIT == 0);
1171 17002894 : unsigned int base_alignment = bit_base_alignment / BITS_PER_UNIT;
1172 17002894 : poly_int64 base_misalignment = bit_base_misalignment / BITS_PER_UNIT;
1173 :
1174 17002894 : if (TREE_CODE (base) == MEM_REF)
1175 : {
1176 7358776 : if (!integer_zerop (TREE_OPERAND (base, 1)))
1177 : {
1178 : /* Subtract MOFF from the base and add it to POFFSET instead.
1179 : Adjust the misalignment to reflect the amount we subtracted. */
1180 1323259 : poly_offset_int moff = mem_ref_offset (base);
1181 1323259 : base_misalignment -= moff.force_shwi ();
1182 1323259 : tree mofft = wide_int_to_tree (sizetype, moff);
1183 1323259 : if (!poffset)
1184 : poffset = mofft;
1185 : else
1186 9997 : poffset = size_binop (PLUS_EXPR, poffset, mofft);
1187 : }
1188 7358776 : base = TREE_OPERAND (base, 0);
1189 : }
1190 : else
1191 : {
1192 9644118 : if (may_be_nonaddressable_p (base))
1193 2072 : return opt_result::failure_at (stmt,
1194 : "failed: base not addressable.\n");
1195 9642046 : base = build_fold_addr_expr (base);
1196 : }
1197 :
1198 17000822 : if (in_loop)
1199 : {
1200 3237207 : if (!simple_iv (loop, loop, base, &base_iv, true))
1201 443317 : return opt_result::failure_at
1202 443317 : (stmt, "failed: evolution of base is not affine.\n");
1203 : }
1204 : else
1205 : {
1206 13763615 : base_iv.base = base;
1207 13763615 : base_iv.step = ssize_int (0);
1208 13763615 : base_iv.no_overflow = true;
1209 : }
1210 :
1211 16557505 : if (!poffset)
1212 : {
1213 13698164 : offset_iv.base = ssize_int (0);
1214 13698164 : offset_iv.step = ssize_int (0);
1215 : }
1216 : else
1217 : {
1218 2859341 : if (!in_loop)
1219 : {
1220 1545865 : offset_iv.base = poffset;
1221 1545865 : offset_iv.step = ssize_int (0);
1222 : }
1223 1313476 : else if (!simple_iv (loop, loop, poffset, &offset_iv, true))
1224 82470 : return opt_result::failure_at
1225 82470 : (stmt, "failed: evolution of offset is not affine.\n");
1226 : }
1227 :
1228 16475035 : init = ssize_int (pbytepos);
1229 :
1230 : /* Subtract any constant component from the base and add it to INIT instead.
1231 : Adjust the misalignment to reflect the amount we subtracted. */
1232 16475035 : split_constant_offset (base_iv.base, &base_iv.base, &dinit);
1233 16475035 : init = size_binop (PLUS_EXPR, init, dinit);
1234 16475035 : base_misalignment -= TREE_INT_CST_LOW (dinit);
1235 :
1236 16475035 : split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
1237 16475035 : init = size_binop (PLUS_EXPR, init, dinit);
1238 :
1239 16475035 : step = size_binop (PLUS_EXPR,
1240 : fold_convert (ssizetype, base_iv.step),
1241 : fold_convert (ssizetype, offset_iv.step));
1242 :
1243 16475035 : base = canonicalize_base_object_address (base_iv.base);
1244 :
1245 : /* See if get_pointer_alignment can guarantee a higher alignment than
1246 : the one we calculated above. */
1247 16475035 : unsigned int HOST_WIDE_INT alt_misalignment;
1248 16475035 : unsigned int alt_alignment;
1249 16475035 : get_pointer_alignment_1 (base, &alt_alignment, &alt_misalignment);
1250 :
1251 : /* As above, these values must be whole bytes. */
1252 16475035 : gcc_assert (alt_alignment % BITS_PER_UNIT == 0
1253 : && alt_misalignment % BITS_PER_UNIT == 0);
1254 16475035 : alt_alignment /= BITS_PER_UNIT;
1255 16475035 : alt_misalignment /= BITS_PER_UNIT;
1256 :
1257 16475035 : if (base_alignment < alt_alignment)
1258 : {
1259 147630 : base_alignment = alt_alignment;
1260 147630 : base_misalignment = alt_misalignment;
1261 : }
1262 :
1263 16475035 : drb->base_address = base;
1264 16475035 : drb->offset = fold_convert (ssizetype, offset_iv.base);
1265 16475035 : drb->init = init;
1266 16475035 : drb->step = step;
1267 16475035 : if (known_misalignment (base_misalignment, base_alignment,
1268 : &drb->base_misalignment))
1269 16475035 : drb->base_alignment = base_alignment;
1270 : else
1271 : {
1272 : drb->base_alignment = known_alignment (base_misalignment);
1273 : drb->base_misalignment = 0;
1274 : }
1275 16475035 : drb->offset_alignment = highest_pow2_factor (offset_iv.base);
1276 16475035 : drb->step_alignment = highest_pow2_factor (step);
1277 :
1278 16475035 : if (dump_file && (dump_flags & TDF_DETAILS))
1279 65722 : fprintf (dump_file, "success.\n");
1280 :
1281 16475035 : return opt_result::success ();
1282 : }
1283 :
1284 : /* Return true if OP is a valid component reference for a DR access
1285 : function. This accepts a subset of what handled_component_p accepts. */
1286 :
1287 : static bool
1288 5799434 : access_fn_component_p (tree op)
1289 : {
1290 5799434 : switch (TREE_CODE (op))
1291 : {
1292 : case REALPART_EXPR:
1293 : case IMAGPART_EXPR:
1294 : case ARRAY_REF:
1295 : return true;
1296 :
1297 1989838 : case COMPONENT_REF:
1298 1989838 : return (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == RECORD_TYPE
1299 1989838 : || (!AGGREGATE_TYPE_P (TREE_TYPE (op))
1300 1545 : && TREE_CODE (TREE_TYPE (op)) != COMPLEX_TYPE));
1301 :
1302 : default:
1303 : return false;
1304 : }
1305 : }
1306 :
1307 : /* Returns whether BASE can have a access_fn_component_p with BASE
1308 : as base. */
1309 :
1310 : static bool
1311 1721333 : base_supports_access_fn_components_p (tree base)
1312 : {
1313 1721333 : switch (TREE_CODE (TREE_TYPE (base)))
1314 : {
1315 : case COMPLEX_TYPE:
1316 : case ARRAY_TYPE:
1317 : case RECORD_TYPE:
1318 : return true;
1319 1714436 : default:
1320 1714436 : return false;
1321 : }
1322 : }
1323 :
1324 : /* Determines the base object and the list of indices of memory reference
1325 : DR, analyzed in LOOP and instantiated before NEST. */
1326 :
1327 : static void
1328 17145717 : dr_analyze_indices (struct indices *dri, tree ref, edge nest, loop_p loop)
1329 : {
1330 : /* If analyzing a basic-block there are no indices to analyze
1331 : and thus no access functions. */
1332 17145717 : if (!nest)
1333 : {
1334 13804770 : dri->base_object = ref;
1335 13804770 : dri->access_fns.create (0);
1336 13804770 : return;
1337 : }
1338 :
1339 3340947 : vec<tree> access_fns = vNULL;
1340 :
1341 : /* REALPART_EXPR and IMAGPART_EXPR can be handled like accesses
1342 : into a two element array with a constant index. The base is
1343 : then just the immediate underlying object. */
1344 3340947 : if (TREE_CODE (ref) == REALPART_EXPR)
1345 : {
1346 42381 : ref = TREE_OPERAND (ref, 0);
1347 42381 : access_fns.safe_push (integer_zero_node);
1348 : }
1349 3298566 : else if (TREE_CODE (ref) == IMAGPART_EXPR)
1350 : {
1351 40514 : ref = TREE_OPERAND (ref, 0);
1352 40514 : access_fns.safe_push (integer_one_node);
1353 : }
1354 :
1355 : /* Analyze access functions of dimensions we know to be independent.
1356 : The list of component references handled here should be kept in
1357 : sync with access_fn_component_p. */
1358 5925608 : while (handled_component_p (ref))
1359 : {
1360 2726575 : if (TREE_CODE (ref) == ARRAY_REF)
1361 : {
1362 1320172 : tree op = TREE_OPERAND (ref, 1);
1363 1320172 : tree access_fn = analyze_scalar_evolution (loop, op);
1364 1320172 : access_fn = instantiate_scev (nest, loop, access_fn);
1365 1320172 : access_fns.safe_push (access_fn);
1366 : }
1367 1406403 : else if (TREE_CODE (ref) == COMPONENT_REF
1368 1406403 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE
1369 100137 : || (!AGGREGATE_TYPE_P (TREE_TYPE (ref))
1370 16298 : && TREE_CODE (TREE_TYPE (ref)) != COMPLEX_TYPE)))
1371 : {
1372 : /* For COMPONENT_REFs of records (but not unions!) use the
1373 : FIELD_DECL offset as constant access function so we can
1374 : disambiguate a[i].f1 and a[i].f2. For unions and accesses
1375 : we do not create further access functions for just use
1376 : zero. */
1377 1264489 : tree off;
1378 1264489 : if (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
1379 : {
1380 1248191 : off = component_ref_field_offset (ref);
1381 1248191 : off = size_binop (PLUS_EXPR,
1382 : size_binop (MULT_EXPR,
1383 : fold_convert (bitsizetype, off),
1384 : bitsize_int (BITS_PER_UNIT)),
1385 : DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)));
1386 : }
1387 : else
1388 16298 : off = bitsize_zero_node;
1389 1264489 : access_fns.safe_push (off);
1390 : }
1391 : else
1392 : /* If we have an unhandled component we could not translate
1393 : to an access function stop analyzing. We have determined
1394 : our base object in this case. */
1395 : break;
1396 :
1397 2584661 : ref = TREE_OPERAND (ref, 0);
1398 : }
1399 :
1400 : /* If the address operand of a MEM_REF base has an evolution in the
1401 : analyzed nest, add it as an additional independent access-function. */
1402 3340947 : if (TREE_CODE (ref) == MEM_REF)
1403 : {
1404 2347212 : tree op = TREE_OPERAND (ref, 0);
1405 2347212 : tree access_fn = analyze_scalar_evolution (loop, op);
1406 2347212 : access_fn = instantiate_scev (nest, loop, access_fn);
1407 2347212 : STRIP_NOPS (access_fn);
1408 2347212 : if (TREE_CODE (access_fn) == POLYNOMIAL_CHREC)
1409 : {
1410 1165253 : tree memoff = TREE_OPERAND (ref, 1);
1411 1165253 : tree base = initial_condition (access_fn);
1412 1165253 : tree orig_type = TREE_TYPE (base);
1413 1165253 : STRIP_USELESS_TYPE_CONVERSION (base);
1414 1165253 : tree off;
1415 1165253 : split_constant_offset (base, &base, &off);
1416 1165253 : STRIP_USELESS_TYPE_CONVERSION (base);
1417 : /* Fold the MEM_REF offset into the evolutions initial
1418 : value to make more bases comparable. */
1419 1165253 : if (!integer_zerop (memoff))
1420 : {
1421 126786 : off = size_binop (PLUS_EXPR, off,
1422 : fold_convert (ssizetype, memoff));
1423 126786 : memoff = build_int_cst (TREE_TYPE (memoff), 0);
1424 : }
1425 : /* Adjust the offset so it is a multiple of the access type
1426 : size and thus we separate bases that can possibly be used
1427 : to produce partial overlaps (which the access_fn machinery
1428 : cannot handle). */
1429 1165253 : wide_int rem;
1430 1165253 : if (TYPE_SIZE_UNIT (TREE_TYPE (ref))
1431 1165117 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref))) == INTEGER_CST
1432 2330053 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (ref))))
1433 1164800 : rem = wi::mod_trunc
1434 1164800 : (wi::to_wide (off),
1435 2329600 : wi::to_wide (TYPE_SIZE_UNIT (TREE_TYPE (ref))),
1436 1164800 : SIGNED);
1437 : else
1438 : /* If we can't compute the remainder simply force the initial
1439 : condition to zero. */
1440 453 : rem = wi::to_wide (off);
1441 1165253 : off = wide_int_to_tree (ssizetype, wi::to_wide (off) - rem);
1442 1165253 : memoff = wide_int_to_tree (TREE_TYPE (memoff), rem);
1443 : /* And finally replace the initial condition. */
1444 2330506 : access_fn = chrec_replace_initial_condition
1445 1165253 : (access_fn, fold_convert (orig_type, off));
1446 : /* ??? This is still not a suitable base object for
1447 : dr_may_alias_p - the base object needs to be an
1448 : access that covers the object as whole. With
1449 : an evolution in the pointer this cannot be
1450 : guaranteed.
1451 : As a band-aid, mark the access so we can special-case
1452 : it in dr_may_alias_p. */
1453 1165253 : tree old = ref;
1454 1165253 : ref = fold_build2_loc (EXPR_LOCATION (ref),
1455 1165253 : MEM_REF, TREE_TYPE (ref),
1456 : base, memoff);
1457 1165253 : MR_DEPENDENCE_CLIQUE (ref) = MR_DEPENDENCE_CLIQUE (old);
1458 1165253 : MR_DEPENDENCE_BASE (ref) = MR_DEPENDENCE_BASE (old);
1459 1165253 : dri->unconstrained_base = true;
1460 1165253 : access_fns.safe_push (access_fn);
1461 1165253 : }
1462 : }
1463 993735 : else if (DECL_P (ref))
1464 : {
1465 : /* Canonicalize DR_BASE_OBJECT to MEM_REF form. */
1466 851821 : ref = build2 (MEM_REF, TREE_TYPE (ref),
1467 : build_fold_addr_expr (ref),
1468 : build_int_cst (reference_alias_ptr_type (ref), 0));
1469 : }
1470 :
1471 3340947 : dri->base_object = ref;
1472 3340947 : dri->access_fns = access_fns;
1473 : }
1474 :
1475 : /* Extracts the alias analysis information from the memory reference DR. */
1476 :
1477 : static void
1478 17030634 : dr_analyze_alias (struct data_reference *dr)
1479 : {
1480 17030634 : tree ref = DR_REF (dr);
1481 17030634 : tree base = get_base_address (ref), addr;
1482 :
1483 17030634 : if (INDIRECT_REF_P (base)
1484 17030634 : || TREE_CODE (base) == MEM_REF)
1485 : {
1486 7354216 : addr = TREE_OPERAND (base, 0);
1487 7354216 : if (TREE_CODE (addr) == SSA_NAME)
1488 7352841 : DR_PTR_INFO (dr) = SSA_NAME_PTR_INFO (addr);
1489 : }
1490 17030634 : }
1491 :
1492 : /* Frees data reference DR. */
1493 :
1494 : void
1495 17526642 : free_data_ref (data_reference_p dr)
1496 : {
1497 17526642 : DR_ACCESS_FNS (dr).release ();
1498 17526642 : if (dr->alt_indices.base_object)
1499 115083 : dr->alt_indices.access_fns.release ();
1500 17526642 : free (dr);
1501 17526642 : }
1502 :
1503 : /* Analyze memory reference MEMREF, which is accessed in STMT.
1504 : The reference is a read if IS_READ is true, otherwise it is a write.
1505 : IS_CONDITIONAL_IN_STMT indicates that the reference is conditional
1506 : within STMT, i.e. that it might not occur even if STMT is executed
1507 : and runs to completion.
1508 :
1509 : Return the data_reference description of MEMREF. NEST is the outermost
1510 : loop in which the reference should be instantiated, LOOP is the loop
1511 : in which the data reference should be analyzed. */
1512 :
1513 : struct data_reference *
1514 17030634 : create_data_ref (edge nest, loop_p loop, tree memref, gimple *stmt,
1515 : bool is_read, bool is_conditional_in_stmt)
1516 : {
1517 17030634 : struct data_reference *dr;
1518 :
1519 17030634 : if (dump_file && (dump_flags & TDF_DETAILS))
1520 : {
1521 67994 : fprintf (dump_file, "Creating dr for ");
1522 67994 : print_generic_expr (dump_file, memref, TDF_SLIM);
1523 67994 : fprintf (dump_file, "\n");
1524 : }
1525 :
1526 17030634 : dr = XCNEW (struct data_reference);
1527 17030634 : DR_STMT (dr) = stmt;
1528 17030634 : DR_REF (dr) = memref;
1529 17030634 : DR_IS_READ (dr) = is_read;
1530 17030634 : DR_IS_CONDITIONAL_IN_STMT (dr) = is_conditional_in_stmt;
1531 :
1532 30835404 : dr_analyze_innermost (&DR_INNERMOST (dr), memref,
1533 : nest != NULL ? loop : NULL, stmt);
1534 17030634 : dr_analyze_indices (&dr->indices, DR_REF (dr), nest, loop);
1535 17030634 : dr_analyze_alias (dr);
1536 :
1537 17030634 : if (dump_file && (dump_flags & TDF_DETAILS))
1538 : {
1539 67994 : unsigned i;
1540 67994 : fprintf (dump_file, "\tbase_address: ");
1541 67994 : print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1542 67994 : fprintf (dump_file, "\n\toffset from base address: ");
1543 67994 : print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1544 67994 : fprintf (dump_file, "\n\tconstant offset from base address: ");
1545 67994 : print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1546 67994 : fprintf (dump_file, "\n\tstep: ");
1547 67994 : print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1548 67994 : fprintf (dump_file, "\n\tbase alignment: %d", DR_BASE_ALIGNMENT (dr));
1549 67994 : fprintf (dump_file, "\n\tbase misalignment: %d",
1550 : DR_BASE_MISALIGNMENT (dr));
1551 67994 : fprintf (dump_file, "\n\toffset alignment: %d",
1552 : DR_OFFSET_ALIGNMENT (dr));
1553 67994 : fprintf (dump_file, "\n\tstep alignment: %d", DR_STEP_ALIGNMENT (dr));
1554 67994 : fprintf (dump_file, "\n\tbase_object: ");
1555 67994 : print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1556 67994 : fprintf (dump_file, "\n");
1557 194878 : for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
1558 : {
1559 58890 : fprintf (dump_file, "\tAccess function %d: ", i);
1560 58890 : print_generic_stmt (dump_file, DR_ACCESS_FN (dr, i), TDF_SLIM);
1561 : }
1562 : }
1563 :
1564 17030634 : return dr;
1565 : }
1566 :
1567 : /* A helper function computes order between two tree expressions T1 and T2.
1568 : This is used in comparator functions sorting objects based on the order
1569 : of tree expressions. The function returns -1, 0, or 1. */
1570 :
1571 : int
1572 440136805 : data_ref_compare_tree (tree t1, tree t2)
1573 : {
1574 440136805 : int i, cmp;
1575 440136805 : enum tree_code code;
1576 440136805 : char tclass;
1577 :
1578 440136805 : if (t1 == t2)
1579 : return 0;
1580 200836081 : if (t1 == NULL)
1581 : return -1;
1582 200700951 : if (t2 == NULL)
1583 : return 1;
1584 :
1585 200618141 : STRIP_USELESS_TYPE_CONVERSION (t1);
1586 200618141 : STRIP_USELESS_TYPE_CONVERSION (t2);
1587 200618141 : if (t1 == t2)
1588 : return 0;
1589 :
1590 200032092 : if (TREE_CODE (t1) != TREE_CODE (t2)
1591 14103924 : && ! (CONVERT_EXPR_P (t1) && CONVERT_EXPR_P (t2)))
1592 14103924 : return TREE_CODE (t1) < TREE_CODE (t2) ? -1 : 1;
1593 :
1594 185928168 : code = TREE_CODE (t1);
1595 185928168 : switch (code)
1596 : {
1597 54316263 : case INTEGER_CST:
1598 54316263 : return tree_int_cst_compare (t1, t2);
1599 :
1600 16 : case STRING_CST:
1601 16 : if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2))
1602 16 : return TREE_STRING_LENGTH (t1) < TREE_STRING_LENGTH (t2) ? -1 : 1;
1603 0 : return memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1604 0 : TREE_STRING_LENGTH (t1));
1605 :
1606 17913130 : case SSA_NAME:
1607 17913130 : if (SSA_NAME_VERSION (t1) != SSA_NAME_VERSION (t2))
1608 17913130 : return SSA_NAME_VERSION (t1) < SSA_NAME_VERSION (t2) ? -1 : 1;
1609 : break;
1610 :
1611 113698759 : default:
1612 113698759 : if (POLY_INT_CST_P (t1))
1613 : return compare_sizes_for_sort (wi::to_poly_widest (t1),
1614 : wi::to_poly_widest (t2));
1615 :
1616 113698759 : tclass = TREE_CODE_CLASS (code);
1617 :
1618 : /* For decls, compare their UIDs. */
1619 113698759 : if (tclass == tcc_declaration)
1620 : {
1621 21421756 : if (DECL_UID (t1) != DECL_UID (t2))
1622 21421269 : return DECL_UID (t1) < DECL_UID (t2) ? -1 : 1;
1623 : break;
1624 : }
1625 : /* For expressions, compare their operands recursively. */
1626 92277003 : else if (IS_EXPR_CODE_CLASS (tclass))
1627 : {
1628 165079128 : for (i = TREE_OPERAND_LENGTH (t1) - 1; i >= 0; --i)
1629 : {
1630 107016950 : cmp = data_ref_compare_tree (TREE_OPERAND (t1, i),
1631 107016950 : TREE_OPERAND (t2, i));
1632 107016950 : if (cmp != 0)
1633 : return cmp;
1634 : }
1635 : }
1636 : else
1637 0 : gcc_unreachable ();
1638 : }
1639 :
1640 : return 0;
1641 : }
1642 :
1643 : /* Return TRUE it's possible to resolve data dependence DDR by runtime alias
1644 : check. */
1645 :
1646 : opt_result
1647 230937 : runtime_alias_check_p (ddr_p ddr, class loop *loop, bool speed_p)
1648 : {
1649 230937 : if (dump_enabled_p ())
1650 7727 : dump_printf (MSG_NOTE,
1651 : "consider run-time aliasing test between %T and %T\n",
1652 7727 : DR_REF (DDR_A (ddr)), DR_REF (DDR_B (ddr)));
1653 :
1654 230937 : if (!speed_p)
1655 0 : return opt_result::failure_at (DR_STMT (DDR_A (ddr)),
1656 : "runtime alias check not supported when"
1657 : " optimizing for size.\n");
1658 :
1659 : /* FORNOW: We don't support versioning with outer-loop in either
1660 : vectorization or loop distribution. */
1661 230937 : if (loop != NULL && loop->inner != NULL)
1662 143 : return opt_result::failure_at (DR_STMT (DDR_A (ddr)),
1663 : "runtime alias check not supported for"
1664 : " outer loop.\n");
1665 :
1666 : /* FORNOW: We don't support handling different address spaces. */
1667 230794 : if (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (DR_BASE_ADDRESS (DDR_A (ddr)))))
1668 230794 : != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (DR_BASE_ADDRESS (DDR_B (ddr))))))
1669 1 : return opt_result::failure_at (DR_STMT (DDR_A (ddr)),
1670 : "runtime alias check between different "
1671 : "address spaces not supported.\n");
1672 :
1673 230793 : return opt_result::success ();
1674 : }
1675 :
1676 : /* Operator == between two dr_with_seg_len objects.
1677 :
1678 : This equality operator is used to make sure two data refs
1679 : are the same one so that we will consider to combine the
1680 : aliasing checks of those two pairs of data dependent data
1681 : refs. */
1682 :
1683 : static bool
1684 146097 : operator == (const dr_with_seg_len& d1,
1685 : const dr_with_seg_len& d2)
1686 : {
1687 146097 : return (operand_equal_p (DR_BASE_ADDRESS (d1.dr),
1688 146097 : DR_BASE_ADDRESS (d2.dr), 0)
1689 111042 : && data_ref_compare_tree (DR_OFFSET (d1.dr), DR_OFFSET (d2.dr)) == 0
1690 110142 : && data_ref_compare_tree (DR_INIT (d1.dr), DR_INIT (d2.dr)) == 0
1691 100947 : && data_ref_compare_tree (d1.seg_len, d2.seg_len) == 0
1692 100063 : && known_eq (d1.access_size, d2.access_size)
1693 242788 : && d1.align == d2.align);
1694 : }
1695 :
1696 : /* Comparison function for sorting objects of dr_with_seg_len_pair_t
1697 : so that we can combine aliasing checks in one scan. */
1698 :
1699 : static int
1700 1179645 : comp_dr_with_seg_len_pair (const void *pa_, const void *pb_)
1701 : {
1702 1179645 : const dr_with_seg_len_pair_t* pa = (const dr_with_seg_len_pair_t *) pa_;
1703 1179645 : const dr_with_seg_len_pair_t* pb = (const dr_with_seg_len_pair_t *) pb_;
1704 1179645 : const dr_with_seg_len &a1 = pa->first, &a2 = pa->second;
1705 1179645 : const dr_with_seg_len &b1 = pb->first, &b2 = pb->second;
1706 :
1707 : /* For DR pairs (a, b) and (c, d), we only consider to merge the alias checks
1708 : if a and c have the same basic address snd step, and b and d have the same
1709 : address and step. Therefore, if any a&c or b&d don't have the same address
1710 : and step, we don't care the order of those two pairs after sorting. */
1711 1179645 : int comp_res;
1712 :
1713 1179645 : if ((comp_res = data_ref_compare_tree (DR_BASE_ADDRESS (a1.dr),
1714 1179645 : DR_BASE_ADDRESS (b1.dr))) != 0)
1715 : return comp_res;
1716 612869 : if ((comp_res = data_ref_compare_tree (DR_BASE_ADDRESS (a2.dr),
1717 612869 : DR_BASE_ADDRESS (b2.dr))) != 0)
1718 : return comp_res;
1719 414527 : if ((comp_res = data_ref_compare_tree (DR_STEP (a1.dr),
1720 414527 : DR_STEP (b1.dr))) != 0)
1721 : return comp_res;
1722 413907 : if ((comp_res = data_ref_compare_tree (DR_STEP (a2.dr),
1723 413907 : DR_STEP (b2.dr))) != 0)
1724 : return comp_res;
1725 406312 : if ((comp_res = data_ref_compare_tree (DR_OFFSET (a1.dr),
1726 406312 : DR_OFFSET (b1.dr))) != 0)
1727 : return comp_res;
1728 390233 : if ((comp_res = data_ref_compare_tree (DR_INIT (a1.dr),
1729 390233 : DR_INIT (b1.dr))) != 0)
1730 : return comp_res;
1731 289397 : if ((comp_res = data_ref_compare_tree (DR_OFFSET (a2.dr),
1732 289397 : DR_OFFSET (b2.dr))) != 0)
1733 : return comp_res;
1734 274094 : if ((comp_res = data_ref_compare_tree (DR_INIT (a2.dr),
1735 274094 : DR_INIT (b2.dr))) != 0)
1736 : return comp_res;
1737 :
1738 : return 0;
1739 : }
1740 :
1741 : /* Dump information about ALIAS_PAIR, indenting each line by INDENT. */
1742 :
1743 : static void
1744 1017 : dump_alias_pair (dr_with_seg_len_pair_t *alias_pair, const char *indent)
1745 : {
1746 2034 : dump_printf (MSG_NOTE, "%sreference: %T vs. %T\n", indent,
1747 1017 : DR_REF (alias_pair->first.dr),
1748 1017 : DR_REF (alias_pair->second.dr));
1749 :
1750 1017 : dump_printf (MSG_NOTE, "%ssegment length: %T", indent,
1751 : alias_pair->first.seg_len);
1752 1017 : if (!operand_equal_p (alias_pair->first.seg_len,
1753 1017 : alias_pair->second.seg_len, 0))
1754 261 : dump_printf (MSG_NOTE, " vs. %T", alias_pair->second.seg_len);
1755 :
1756 1017 : dump_printf (MSG_NOTE, "\n%saccess size: ", indent);
1757 1017 : dump_dec (MSG_NOTE, alias_pair->first.access_size);
1758 1017 : if (maybe_ne (alias_pair->first.access_size, alias_pair->second.access_size))
1759 : {
1760 247 : dump_printf (MSG_NOTE, " vs. ");
1761 247 : dump_dec (MSG_NOTE, alias_pair->second.access_size);
1762 : }
1763 :
1764 1017 : dump_printf (MSG_NOTE, "\n%salignment: %d", indent,
1765 : alias_pair->first.align);
1766 1017 : if (alias_pair->first.align != alias_pair->second.align)
1767 75 : dump_printf (MSG_NOTE, " vs. %d", alias_pair->second.align);
1768 :
1769 1017 : dump_printf (MSG_NOTE, "\n%sflags: ", indent);
1770 1017 : if (alias_pair->flags & DR_ALIAS_RAW)
1771 167 : dump_printf (MSG_NOTE, " RAW");
1772 1017 : if (alias_pair->flags & DR_ALIAS_WAR)
1773 808 : dump_printf (MSG_NOTE, " WAR");
1774 1017 : if (alias_pair->flags & DR_ALIAS_WAW)
1775 174 : dump_printf (MSG_NOTE, " WAW");
1776 1017 : if (alias_pair->flags & DR_ALIAS_ARBITRARY)
1777 226 : dump_printf (MSG_NOTE, " ARBITRARY");
1778 1017 : if (alias_pair->flags & DR_ALIAS_SWAPPED)
1779 0 : dump_printf (MSG_NOTE, " SWAPPED");
1780 1017 : if (alias_pair->flags & DR_ALIAS_UNSWAPPED)
1781 0 : dump_printf (MSG_NOTE, " UNSWAPPED");
1782 1017 : if (alias_pair->flags & DR_ALIAS_MIXED_STEPS)
1783 0 : dump_printf (MSG_NOTE, " MIXED_STEPS");
1784 1017 : if (alias_pair->flags == 0)
1785 0 : dump_printf (MSG_NOTE, " <none>");
1786 1017 : dump_printf (MSG_NOTE, "\n");
1787 1017 : }
1788 :
1789 : /* Merge alias checks recorded in ALIAS_PAIRS and remove redundant ones.
1790 : FACTOR is number of iterations that each data reference is accessed.
1791 :
1792 : Basically, for each pair of dependent data refs store_ptr_0 & load_ptr_0,
1793 : we create an expression:
1794 :
1795 : ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
1796 : || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
1797 :
1798 : for aliasing checks. However, in some cases we can decrease the number
1799 : of checks by combining two checks into one. For example, suppose we have
1800 : another pair of data refs store_ptr_0 & load_ptr_1, and if the following
1801 : condition is satisfied:
1802 :
1803 : load_ptr_0 < load_ptr_1 &&
1804 : load_ptr_1 - load_ptr_0 - load_segment_length_0 < store_segment_length_0
1805 :
1806 : (this condition means, in each iteration of vectorized loop, the accessed
1807 : memory of store_ptr_0 cannot be between the memory of load_ptr_0 and
1808 : load_ptr_1.)
1809 :
1810 : we then can use only the following expression to finish the aliasing checks
1811 : between store_ptr_0 & load_ptr_0 and store_ptr_0 & load_ptr_1:
1812 :
1813 : ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
1814 : || (load_ptr_1 + load_segment_length_1 <= store_ptr_0))
1815 :
1816 : Note that we only consider that load_ptr_0 and load_ptr_1 have the same
1817 : basic address. */
1818 :
1819 : void
1820 23653 : prune_runtime_alias_test_list (vec<dr_with_seg_len_pair_t> *alias_pairs,
1821 : poly_uint64)
1822 : {
1823 23653 : if (alias_pairs->is_empty ())
1824 23653 : return;
1825 :
1826 : /* Canonicalize each pair so that the base components are ordered wrt
1827 : data_ref_compare_tree. This allows the loop below to merge more
1828 : cases. */
1829 : unsigned int i;
1830 : dr_with_seg_len_pair_t *alias_pair;
1831 94322 : FOR_EACH_VEC_ELT (*alias_pairs, i, alias_pair)
1832 : {
1833 71553 : data_reference_p dr_a = alias_pair->first.dr;
1834 71553 : data_reference_p dr_b = alias_pair->second.dr;
1835 71553 : int comp_res = data_ref_compare_tree (DR_BASE_ADDRESS (dr_a),
1836 : DR_BASE_ADDRESS (dr_b));
1837 71553 : if (comp_res == 0)
1838 1828 : comp_res = data_ref_compare_tree (DR_OFFSET (dr_a), DR_OFFSET (dr_b));
1839 1828 : if (comp_res == 0)
1840 136 : comp_res = data_ref_compare_tree (DR_INIT (dr_a), DR_INIT (dr_b));
1841 71553 : if (comp_res > 0)
1842 : {
1843 25535 : std::swap (alias_pair->first, alias_pair->second);
1844 25535 : alias_pair->flags |= DR_ALIAS_SWAPPED;
1845 : }
1846 : else
1847 46018 : alias_pair->flags |= DR_ALIAS_UNSWAPPED;
1848 : }
1849 :
1850 : /* Sort the collected data ref pairs so that we can scan them once to
1851 : combine all possible aliasing checks. */
1852 22769 : alias_pairs->qsort (comp_dr_with_seg_len_pair);
1853 :
1854 : /* Scan the sorted dr pairs and check if we can combine alias checks
1855 : of two neighboring dr pairs. */
1856 22769 : unsigned int last = 0;
1857 71553 : for (i = 1; i < alias_pairs->length (); ++i)
1858 : {
1859 : /* Deal with two ddrs (dr_a1, dr_b1) and (dr_a2, dr_b2). */
1860 48784 : dr_with_seg_len_pair_t *alias_pair1 = &(*alias_pairs)[last];
1861 48784 : dr_with_seg_len_pair_t *alias_pair2 = &(*alias_pairs)[i];
1862 :
1863 48784 : dr_with_seg_len *dr_a1 = &alias_pair1->first;
1864 48784 : dr_with_seg_len *dr_b1 = &alias_pair1->second;
1865 48784 : dr_with_seg_len *dr_a2 = &alias_pair2->first;
1866 48784 : dr_with_seg_len *dr_b2 = &alias_pair2->second;
1867 :
1868 : /* Remove duplicate data ref pairs. */
1869 48784 : if (*dr_a1 == *dr_a2 && *dr_b1 == *dr_b2)
1870 : {
1871 22446 : if (dump_enabled_p ())
1872 1693 : dump_printf (MSG_NOTE, "found equal ranges %T, %T and %T, %T\n",
1873 1693 : DR_REF (dr_a1->dr), DR_REF (dr_b1->dr),
1874 1693 : DR_REF (dr_a2->dr), DR_REF (dr_b2->dr));
1875 22446 : alias_pair1->flags |= alias_pair2->flags;
1876 22446 : continue;
1877 : }
1878 :
1879 : /* Assume that we won't be able to merge the pairs, then correct
1880 : if we do. */
1881 26338 : last += 1;
1882 26338 : if (last != i)
1883 7044 : (*alias_pairs)[last] = (*alias_pairs)[i];
1884 :
1885 26338 : if (*dr_a1 == *dr_a2 || *dr_b1 == *dr_b2)
1886 : {
1887 : /* We consider the case that DR_B1 and DR_B2 are same memrefs,
1888 : and DR_A1 and DR_A2 are two consecutive memrefs. */
1889 22191 : if (*dr_a1 == *dr_a2)
1890 : {
1891 14804 : std::swap (dr_a1, dr_b1);
1892 14804 : std::swap (dr_a2, dr_b2);
1893 : }
1894 :
1895 22191 : poly_int64 init_a1, init_a2;
1896 : /* Only consider cases in which the distance between the initial
1897 : DR_A1 and the initial DR_A2 is known at compile time. */
1898 40261 : if (!operand_equal_p (DR_BASE_ADDRESS (dr_a1->dr),
1899 22191 : DR_BASE_ADDRESS (dr_a2->dr), 0)
1900 4618 : || !operand_equal_p (DR_OFFSET (dr_a1->dr),
1901 4618 : DR_OFFSET (dr_a2->dr), 0)
1902 4121 : || !poly_int_tree_p (DR_INIT (dr_a1->dr), &init_a1)
1903 26312 : || !poly_int_tree_p (DR_INIT (dr_a2->dr), &init_a2))
1904 18097 : continue;
1905 :
1906 : /* Don't combine if we can't tell which one comes first. */
1907 4121 : if (!ordered_p (init_a1, init_a2))
1908 : continue;
1909 :
1910 : /* Work out what the segment length would be if we did combine
1911 : DR_A1 and DR_A2:
1912 :
1913 : - If DR_A1 and DR_A2 have equal lengths, that length is
1914 : also the combined length.
1915 :
1916 : - If DR_A1 and DR_A2 both have negative "lengths", the combined
1917 : length is the lower bound on those lengths.
1918 :
1919 : - If DR_A1 and DR_A2 both have positive lengths, the combined
1920 : length is the upper bound on those lengths.
1921 :
1922 : Other cases are unlikely to give a useful combination.
1923 :
1924 : The lengths both have sizetype, so the sign is taken from
1925 : the step instead. */
1926 4121 : poly_uint64 new_seg_len = 0;
1927 4121 : bool new_seg_len_p = !operand_equal_p (dr_a1->seg_len,
1928 4121 : dr_a2->seg_len, 0);
1929 4121 : if (new_seg_len_p)
1930 : {
1931 27 : poly_uint64 seg_len_a1, seg_len_a2;
1932 27 : if (!poly_int_tree_p (dr_a1->seg_len, &seg_len_a1)
1933 27 : || !poly_int_tree_p (dr_a2->seg_len, &seg_len_a2))
1934 27 : continue;
1935 :
1936 0 : tree indicator_a = dr_direction_indicator (dr_a1->dr);
1937 0 : if (TREE_CODE (indicator_a) != INTEGER_CST)
1938 0 : continue;
1939 :
1940 0 : tree indicator_b = dr_direction_indicator (dr_a2->dr);
1941 0 : if (TREE_CODE (indicator_b) != INTEGER_CST)
1942 0 : continue;
1943 :
1944 0 : int sign_a = tree_int_cst_sgn (indicator_a);
1945 0 : int sign_b = tree_int_cst_sgn (indicator_b);
1946 :
1947 0 : if (sign_a <= 0 && sign_b <= 0)
1948 0 : new_seg_len = lower_bound (seg_len_a1, seg_len_a2);
1949 0 : else if (sign_a >= 0 && sign_b >= 0)
1950 0 : new_seg_len = upper_bound (seg_len_a1, seg_len_a2);
1951 : else
1952 0 : continue;
1953 : }
1954 : /* At this point we're committed to merging the refs. */
1955 :
1956 : /* Make sure dr_a1 starts left of dr_a2. */
1957 4094 : if (maybe_gt (init_a1, init_a2))
1958 : {
1959 0 : std::swap (*dr_a1, *dr_a2);
1960 0 : std::swap (init_a1, init_a2);
1961 : }
1962 :
1963 : /* The DR_Bs are equal, so only the DR_As can introduce
1964 : mixed steps. */
1965 4094 : if (!operand_equal_p (DR_STEP (dr_a1->dr), DR_STEP (dr_a2->dr), 0))
1966 0 : alias_pair1->flags |= DR_ALIAS_MIXED_STEPS;
1967 :
1968 4094 : if (new_seg_len_p)
1969 : {
1970 0 : dr_a1->seg_len = build_int_cst (TREE_TYPE (dr_a1->seg_len),
1971 0 : new_seg_len);
1972 0 : dr_a1->align = MIN (dr_a1->align, known_alignment (new_seg_len));
1973 : }
1974 :
1975 : /* This is always positive due to the swap above. */
1976 4094 : poly_uint64 diff = init_a2 - init_a1;
1977 :
1978 : /* The new check will start at DR_A1. Make sure that its access
1979 : size encompasses the initial DR_A2. */
1980 4094 : if (maybe_lt (dr_a1->access_size, diff + dr_a2->access_size))
1981 : {
1982 1455 : dr_a1->access_size = upper_bound (dr_a1->access_size,
1983 1455 : diff + dr_a2->access_size);
1984 1455 : unsigned int new_align = known_alignment (dr_a1->access_size);
1985 1455 : dr_a1->align = MIN (dr_a1->align, new_align);
1986 : }
1987 4094 : if (dump_enabled_p ())
1988 1027 : dump_printf (MSG_NOTE, "merging ranges for %T, %T and %T, %T\n",
1989 1027 : DR_REF (dr_a1->dr), DR_REF (dr_b1->dr),
1990 1027 : DR_REF (dr_a2->dr), DR_REF (dr_b2->dr));
1991 4094 : alias_pair1->flags |= alias_pair2->flags;
1992 4094 : last -= 1;
1993 : }
1994 : }
1995 22769 : alias_pairs->truncate (last + 1);
1996 :
1997 : /* Try to restore the original dr_with_seg_len order within each
1998 : dr_with_seg_len_pair_t. If we ended up combining swapped and
1999 : unswapped pairs into the same check, we have to invalidate any
2000 : RAW, WAR and WAW information for it. */
2001 22769 : if (dump_enabled_p ())
2002 805 : dump_printf (MSG_NOTE, "merged alias checks:\n");
2003 67782 : FOR_EACH_VEC_ELT (*alias_pairs, i, alias_pair)
2004 : {
2005 45013 : unsigned int swap_mask = (DR_ALIAS_SWAPPED | DR_ALIAS_UNSWAPPED);
2006 45013 : unsigned int swapped = (alias_pair->flags & swap_mask);
2007 45013 : if (swapped == DR_ALIAS_SWAPPED)
2008 13704 : std::swap (alias_pair->first, alias_pair->second);
2009 31309 : else if (swapped != DR_ALIAS_UNSWAPPED)
2010 3299 : alias_pair->flags |= DR_ALIAS_ARBITRARY;
2011 45013 : alias_pair->flags &= ~swap_mask;
2012 45013 : if (dump_enabled_p ())
2013 1017 : dump_alias_pair (alias_pair, " ");
2014 : }
2015 : }
2016 :
2017 : /* A subroutine of create_intersect_range_checks, with a subset of the
2018 : same arguments. Try to use IFN_CHECK_RAW_PTRS and IFN_CHECK_WAR_PTRS
2019 : to optimize cases in which the references form a simple RAW, WAR or
2020 : WAR dependence. */
2021 :
2022 : static bool
2023 4795 : create_ifn_alias_checks (tree *cond_expr,
2024 : const dr_with_seg_len_pair_t &alias_pair)
2025 : {
2026 4795 : const dr_with_seg_len& dr_a = alias_pair.first;
2027 4795 : const dr_with_seg_len& dr_b = alias_pair.second;
2028 :
2029 : /* Check for cases in which:
2030 :
2031 : (a) we have a known RAW, WAR or WAR dependence
2032 : (b) the accesses are well-ordered in both the original and new code
2033 : (see the comment above the DR_ALIAS_* flags for details); and
2034 : (c) the DR_STEPs describe all access pairs covered by ALIAS_PAIR. */
2035 4795 : if (alias_pair.flags & ~(DR_ALIAS_RAW | DR_ALIAS_WAR | DR_ALIAS_WAW))
2036 : return false;
2037 :
2038 : /* Make sure that both DRs access the same pattern of bytes,
2039 : with a constant length and step. */
2040 3106 : poly_uint64 seg_len;
2041 3106 : if (!operand_equal_p (dr_a.seg_len, dr_b.seg_len, 0)
2042 2701 : || !poly_int_tree_p (dr_a.seg_len, &seg_len)
2043 2694 : || maybe_ne (dr_a.access_size, dr_b.access_size)
2044 2653 : || !operand_equal_p (DR_STEP (dr_a.dr), DR_STEP (dr_b.dr), 0)
2045 5759 : || !tree_fits_uhwi_p (DR_STEP (dr_a.dr)))
2046 : return false;
2047 :
2048 2638 : unsigned HOST_WIDE_INT bytes = tree_to_uhwi (DR_STEP (dr_a.dr));
2049 2638 : tree addr_a = DR_BASE_ADDRESS (dr_a.dr);
2050 2638 : tree addr_b = DR_BASE_ADDRESS (dr_b.dr);
2051 :
2052 : /* See whether the target supports what we want to do. WAW checks are
2053 : equivalent to WAR checks here. */
2054 2600 : internal_fn ifn = (alias_pair.flags & DR_ALIAS_RAW
2055 2638 : ? IFN_CHECK_RAW_PTRS
2056 : : IFN_CHECK_WAR_PTRS);
2057 2638 : unsigned int align = MIN (dr_a.align, dr_b.align);
2058 2638 : poly_uint64 full_length = seg_len + bytes;
2059 2638 : if (!internal_check_ptrs_fn_supported_p (ifn, TREE_TYPE (addr_a),
2060 : full_length, align))
2061 : {
2062 2638 : full_length = seg_len + dr_a.access_size;
2063 2638 : if (!internal_check_ptrs_fn_supported_p (ifn, TREE_TYPE (addr_a),
2064 : full_length, align))
2065 : return false;
2066 : }
2067 :
2068 : /* Commit to using this form of test. */
2069 0 : addr_a = fold_build_pointer_plus (addr_a, DR_OFFSET (dr_a.dr));
2070 0 : addr_a = fold_build_pointer_plus (addr_a, DR_INIT (dr_a.dr));
2071 :
2072 0 : addr_b = fold_build_pointer_plus (addr_b, DR_OFFSET (dr_b.dr));
2073 0 : addr_b = fold_build_pointer_plus (addr_b, DR_INIT (dr_b.dr));
2074 :
2075 0 : *cond_expr = build_call_expr_internal_loc (UNKNOWN_LOCATION,
2076 : ifn, boolean_type_node,
2077 : 4, addr_a, addr_b,
2078 0 : size_int (full_length),
2079 0 : size_int (align));
2080 :
2081 0 : if (dump_enabled_p ())
2082 : {
2083 0 : if (ifn == IFN_CHECK_RAW_PTRS)
2084 0 : dump_printf (MSG_NOTE, "using an IFN_CHECK_RAW_PTRS test\n");
2085 : else
2086 0 : dump_printf (MSG_NOTE, "using an IFN_CHECK_WAR_PTRS test\n");
2087 : }
2088 : return true;
2089 : }
2090 :
2091 : /* Try to generate a runtime condition that is true if ALIAS_PAIR is
2092 : free of aliases, using a condition based on index values instead
2093 : of a condition based on addresses. Return true on success,
2094 : storing the condition in *COND_EXPR.
2095 :
2096 : This can only be done if the two data references in ALIAS_PAIR access
2097 : the same array object and the index is the only difference. For example,
2098 : if the two data references are DR_A and DR_B:
2099 :
2100 : DR_A DR_B
2101 : data-ref arr[i] arr[j]
2102 : base_object arr arr
2103 : index {i_0, +, 1}_loop {j_0, +, 1}_loop
2104 :
2105 : The addresses and their index are like:
2106 :
2107 : |<- ADDR_A ->| |<- ADDR_B ->|
2108 : ------------------------------------------------------->
2109 : | | | | | | | | | |
2110 : ------------------------------------------------------->
2111 : i_0 ... i_0+4 j_0 ... j_0+4
2112 :
2113 : We can create expression based on index rather than address:
2114 :
2115 : (unsigned) (i_0 - j_0 + 3) <= 6
2116 :
2117 : i.e. the indices are less than 4 apart.
2118 :
2119 : Note evolution step of index needs to be considered in comparison. */
2120 :
2121 : static bool
2122 4946 : create_intersect_range_checks_index (class loop *loop, tree *cond_expr,
2123 : const dr_with_seg_len_pair_t &alias_pair)
2124 : {
2125 4946 : const dr_with_seg_len &dr_a = alias_pair.first;
2126 4946 : const dr_with_seg_len &dr_b = alias_pair.second;
2127 4946 : if ((alias_pair.flags & DR_ALIAS_MIXED_STEPS)
2128 4946 : || integer_zerop (DR_STEP (dr_a.dr))
2129 4687 : || integer_zerop (DR_STEP (dr_b.dr))
2130 18848 : || DR_NUM_DIMENSIONS (dr_a.dr) != DR_NUM_DIMENSIONS (dr_b.dr))
2131 : return false;
2132 :
2133 4568 : poly_uint64 seg_len1, seg_len2;
2134 4568 : if (!poly_int_tree_p (dr_a.seg_len, &seg_len1)
2135 4568 : || !poly_int_tree_p (dr_b.seg_len, &seg_len2))
2136 : return false;
2137 :
2138 4295 : if (!tree_fits_shwi_p (DR_STEP (dr_a.dr)))
2139 : return false;
2140 :
2141 4295 : if (!operand_equal_p (DR_BASE_OBJECT (dr_a.dr), DR_BASE_OBJECT (dr_b.dr), 0))
2142 : return false;
2143 :
2144 154 : if (!operand_equal_p (DR_STEP (dr_a.dr), DR_STEP (dr_b.dr), 0))
2145 : return false;
2146 :
2147 152 : gcc_assert (TREE_CODE (DR_STEP (dr_a.dr)) == INTEGER_CST);
2148 :
2149 152 : bool neg_step = tree_int_cst_compare (DR_STEP (dr_a.dr), size_zero_node) < 0;
2150 152 : unsigned HOST_WIDE_INT abs_step = tree_to_shwi (DR_STEP (dr_a.dr));
2151 152 : if (neg_step)
2152 : {
2153 30 : abs_step = -abs_step;
2154 30 : seg_len1 = (-wi::to_poly_wide (dr_a.seg_len)).force_uhwi ();
2155 30 : seg_len2 = (-wi::to_poly_wide (dr_b.seg_len)).force_uhwi ();
2156 : }
2157 :
2158 : /* Infer the number of iterations with which the memory segment is accessed
2159 : by DR. In other words, alias is checked if memory segment accessed by
2160 : DR_A in some iterations intersect with memory segment accessed by DR_B
2161 : in the same amount iterations.
2162 : Note segnment length is a linear function of number of iterations with
2163 : DR_STEP as the coefficient. */
2164 152 : poly_uint64 niter_len1, niter_len2;
2165 152 : if (!can_div_trunc_p (seg_len1 + abs_step - 1, abs_step, &niter_len1)
2166 152 : || !can_div_trunc_p (seg_len2 + abs_step - 1, abs_step, &niter_len2))
2167 : return false;
2168 :
2169 : /* Divide each access size by the byte step, rounding up. */
2170 152 : poly_uint64 niter_access1, niter_access2;
2171 152 : if (!can_div_trunc_p (dr_a.access_size + abs_step - 1,
2172 : abs_step, &niter_access1)
2173 152 : || !can_div_trunc_p (dr_b.access_size + abs_step - 1,
2174 : abs_step, &niter_access2))
2175 : return false;
2176 :
2177 152 : bool waw_or_war_p = (alias_pair.flags & ~(DR_ALIAS_WAR | DR_ALIAS_WAW)) == 0;
2178 :
2179 152 : int found = -1;
2180 311 : for (unsigned int i = 0; i < DR_NUM_DIMENSIONS (dr_a.dr); i++)
2181 : {
2182 160 : tree access1 = DR_ACCESS_FN (dr_a.dr, i);
2183 160 : tree access2 = DR_ACCESS_FN (dr_b.dr, i);
2184 : /* Two indices must be the same if they are not scev, or not scev wrto
2185 : current loop being vecorized. */
2186 160 : if (TREE_CODE (access1) != POLYNOMIAL_CHREC
2187 152 : || TREE_CODE (access2) != POLYNOMIAL_CHREC
2188 152 : || CHREC_VARIABLE (access1) != (unsigned)loop->num
2189 312 : || CHREC_VARIABLE (access2) != (unsigned)loop->num)
2190 : {
2191 8 : if (operand_equal_p (access1, access2, 0))
2192 7 : continue;
2193 :
2194 : return false;
2195 : }
2196 152 : if (found >= 0)
2197 : return false;
2198 152 : found = i;
2199 : }
2200 :
2201 : /* Ought not to happen in practice, since if all accesses are equal then the
2202 : alias should be decidable at compile time. */
2203 151 : if (found < 0)
2204 : return false;
2205 :
2206 : /* The two indices must have the same step. */
2207 151 : tree access1 = DR_ACCESS_FN (dr_a.dr, found);
2208 151 : tree access2 = DR_ACCESS_FN (dr_b.dr, found);
2209 151 : if (!operand_equal_p (CHREC_RIGHT (access1), CHREC_RIGHT (access2), 0))
2210 : return false;
2211 :
2212 151 : tree idx_step = CHREC_RIGHT (access1);
2213 : /* Index must have const step, otherwise DR_STEP won't be constant. */
2214 151 : gcc_assert (TREE_CODE (idx_step) == INTEGER_CST);
2215 : /* Index must evaluate in the same direction as DR. */
2216 151 : gcc_assert (!neg_step || tree_int_cst_sign_bit (idx_step) == 1);
2217 :
2218 151 : tree min1 = CHREC_LEFT (access1);
2219 151 : tree min2 = CHREC_LEFT (access2);
2220 151 : if (!types_compatible_p (TREE_TYPE (min1), TREE_TYPE (min2)))
2221 : return false;
2222 :
2223 : /* Ideally, alias can be checked against loop's control IV, but we
2224 : need to prove linear mapping between control IV and reference
2225 : index. Although that should be true, we check against (array)
2226 : index of data reference. Like segment length, index length is
2227 : linear function of the number of iterations with index_step as
2228 : the coefficient, i.e, niter_len * idx_step. */
2229 151 : offset_int abs_idx_step = offset_int::from (wi::to_wide (idx_step),
2230 : SIGNED);
2231 151 : if (neg_step)
2232 30 : abs_idx_step = -abs_idx_step;
2233 151 : poly_offset_int idx_len1 = abs_idx_step * niter_len1;
2234 151 : poly_offset_int idx_len2 = abs_idx_step * niter_len2;
2235 151 : poly_offset_int idx_access1 = abs_idx_step * niter_access1;
2236 151 : poly_offset_int idx_access2 = abs_idx_step * niter_access2;
2237 :
2238 151 : gcc_assert (known_ge (idx_len1, 0)
2239 : && known_ge (idx_len2, 0)
2240 : && known_ge (idx_access1, 0)
2241 : && known_ge (idx_access2, 0));
2242 :
2243 : /* Each access has the following pattern, with lengths measured
2244 : in units of INDEX:
2245 :
2246 : <-- idx_len -->
2247 : <--- A: -ve step --->
2248 : +-----+-------+-----+-------+-----+
2249 : | n-1 | ..... | 0 | ..... | n-1 |
2250 : +-----+-------+-----+-------+-----+
2251 : <--- B: +ve step --->
2252 : <-- idx_len -->
2253 : |
2254 : min
2255 :
2256 : where "n" is the number of scalar iterations covered by the segment
2257 : and where each access spans idx_access units.
2258 :
2259 : A is the range of bytes accessed when the step is negative,
2260 : B is the range when the step is positive.
2261 :
2262 : When checking for general overlap, we need to test whether
2263 : the range:
2264 :
2265 : [min1 + low_offset1, min1 + high_offset1 + idx_access1 - 1]
2266 :
2267 : overlaps:
2268 :
2269 : [min2 + low_offset2, min2 + high_offset2 + idx_access2 - 1]
2270 :
2271 : where:
2272 :
2273 : low_offsetN = +ve step ? 0 : -idx_lenN;
2274 : high_offsetN = +ve step ? idx_lenN : 0;
2275 :
2276 : This is equivalent to testing whether:
2277 :
2278 : min1 + low_offset1 <= min2 + high_offset2 + idx_access2 - 1
2279 : && min2 + low_offset2 <= min1 + high_offset1 + idx_access1 - 1
2280 :
2281 : Converting this into a single test, there is an overlap if:
2282 :
2283 : 0 <= min2 - min1 + bias <= limit
2284 :
2285 : where bias = high_offset2 + idx_access2 - 1 - low_offset1
2286 : limit = (high_offset1 - low_offset1 + idx_access1 - 1)
2287 : + (high_offset2 - low_offset2 + idx_access2 - 1)
2288 : i.e. limit = idx_len1 + idx_access1 - 1 + idx_len2 + idx_access2 - 1
2289 :
2290 : Combining the tests requires limit to be computable in an unsigned
2291 : form of the index type; if it isn't, we fall back to the usual
2292 : pointer-based checks.
2293 :
2294 : We can do better if DR_B is a write and if DR_A and DR_B are
2295 : well-ordered in both the original and the new code (see the
2296 : comment above the DR_ALIAS_* flags for details). In this case
2297 : we know that for each i in [0, n-1], the write performed by
2298 : access i of DR_B occurs after access numbers j<=i of DR_A in
2299 : both the original and the new code. Any write or anti
2300 : dependencies wrt those DR_A accesses are therefore maintained.
2301 :
2302 : We just need to make sure that each individual write in DR_B does not
2303 : overlap any higher-indexed access in DR_A; such DR_A accesses happen
2304 : after the DR_B access in the original code but happen before it in
2305 : the new code.
2306 :
2307 : We know the steps for both accesses are equal, so by induction, we
2308 : just need to test whether the first write of DR_B overlaps a later
2309 : access of DR_A. In other words, we need to move min1 along by
2310 : one iteration:
2311 :
2312 : min1' = min1 + idx_step
2313 :
2314 : and use the ranges:
2315 :
2316 : [min1' + low_offset1', min1' + high_offset1' + idx_access1 - 1]
2317 :
2318 : and:
2319 :
2320 : [min2, min2 + idx_access2 - 1]
2321 :
2322 : where:
2323 :
2324 : low_offset1' = +ve step ? 0 : -(idx_len1 - |idx_step|)
2325 : high_offset1' = +ve_step ? idx_len1 - |idx_step| : 0. */
2326 151 : if (waw_or_war_p)
2327 120 : idx_len1 -= abs_idx_step;
2328 :
2329 151 : poly_offset_int limit = idx_len1 + idx_access1 - 1 + idx_access2 - 1;
2330 151 : if (!waw_or_war_p)
2331 151 : limit += idx_len2;
2332 :
2333 151 : tree utype = unsigned_type_for (TREE_TYPE (min1));
2334 151 : if (!wi::fits_to_tree_p (limit, utype))
2335 : return false;
2336 :
2337 151 : poly_offset_int low_offset1 = neg_step ? -idx_len1 : 0;
2338 151 : poly_offset_int high_offset2 = neg_step || waw_or_war_p ? 0 : idx_len2;
2339 151 : poly_offset_int bias = high_offset2 + idx_access2 - 1 - low_offset1;
2340 : /* Equivalent to adding IDX_STEP to MIN1. */
2341 151 : if (waw_or_war_p)
2342 120 : bias -= wi::to_offset (idx_step);
2343 :
2344 151 : tree subject = fold_build2 (MINUS_EXPR, utype,
2345 : fold_convert (utype, min2),
2346 : fold_convert (utype, min1));
2347 151 : subject = fold_build2 (PLUS_EXPR, utype, subject,
2348 : wide_int_to_tree (utype, bias));
2349 151 : tree part_cond_expr = fold_build2 (GT_EXPR, boolean_type_node, subject,
2350 : wide_int_to_tree (utype, limit));
2351 151 : if (*cond_expr)
2352 0 : *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
2353 : *cond_expr, part_cond_expr);
2354 : else
2355 : *cond_expr = part_cond_expr;
2356 151 : if (dump_enabled_p ())
2357 : {
2358 133 : if (waw_or_war_p)
2359 103 : dump_printf (MSG_NOTE, "using an index-based WAR/WAW test\n");
2360 : else
2361 30 : dump_printf (MSG_NOTE, "using an index-based overlap test\n");
2362 : }
2363 : return true;
2364 : }
2365 :
2366 : /* A subroutine of create_intersect_range_checks, with a subset of the
2367 : same arguments. Try to optimize cases in which the second access
2368 : is a write and in which some overlap is valid. */
2369 :
2370 : static bool
2371 4795 : create_waw_or_war_checks (tree *cond_expr,
2372 : const dr_with_seg_len_pair_t &alias_pair)
2373 : {
2374 4795 : const dr_with_seg_len& dr_a = alias_pair.first;
2375 4795 : const dr_with_seg_len& dr_b = alias_pair.second;
2376 :
2377 : /* Check for cases in which:
2378 :
2379 : (a) DR_B is always a write;
2380 : (b) the accesses are well-ordered in both the original and new code
2381 : (see the comment above the DR_ALIAS_* flags for details); and
2382 : (c) the DR_STEPs describe all access pairs covered by ALIAS_PAIR. */
2383 4795 : if (alias_pair.flags & ~(DR_ALIAS_WAR | DR_ALIAS_WAW))
2384 : return false;
2385 :
2386 : /* Check for equal (but possibly variable) steps. */
2387 3061 : tree step = DR_STEP (dr_a.dr);
2388 3061 : if (!operand_equal_p (step, DR_STEP (dr_b.dr)))
2389 : return false;
2390 :
2391 : /* Make sure that we can operate on sizetype without loss of precision. */
2392 2663 : tree addr_type = TREE_TYPE (DR_BASE_ADDRESS (dr_a.dr));
2393 2663 : if (TYPE_PRECISION (addr_type) != TYPE_PRECISION (sizetype))
2394 : return false;
2395 :
2396 : /* All addresses involved are known to have a common alignment ALIGN.
2397 : We can therefore subtract ALIGN from an exclusive endpoint to get
2398 : an inclusive endpoint. In the best (and common) case, ALIGN is the
2399 : same as the access sizes of both DRs, and so subtracting ALIGN
2400 : cancels out the addition of an access size. */
2401 2663 : unsigned int align = MIN (dr_a.align, dr_b.align);
2402 2663 : poly_uint64 last_chunk_a = dr_a.access_size - align;
2403 2663 : poly_uint64 last_chunk_b = dr_b.access_size - align;
2404 :
2405 : /* Get a boolean expression that is true when the step is negative. */
2406 2663 : tree indicator = dr_direction_indicator (dr_a.dr);
2407 2663 : tree neg_step = fold_build2 (LT_EXPR, boolean_type_node,
2408 : fold_convert (ssizetype, indicator),
2409 : ssize_int (0));
2410 :
2411 : /* Get lengths in sizetype. */
2412 2663 : tree seg_len_a
2413 2663 : = fold_convert (sizetype, rewrite_to_non_trapping_overflow (dr_a.seg_len));
2414 2663 : step = fold_convert (sizetype, rewrite_to_non_trapping_overflow (step));
2415 :
2416 : /* Each access has the following pattern:
2417 :
2418 : <- |seg_len| ->
2419 : <--- A: -ve step --->
2420 : +-----+-------+-----+-------+-----+
2421 : | n-1 | ..... | 0 | ..... | n-1 |
2422 : +-----+-------+-----+-------+-----+
2423 : <--- B: +ve step --->
2424 : <- |seg_len| ->
2425 : |
2426 : base address
2427 :
2428 : where "n" is the number of scalar iterations covered by the segment.
2429 :
2430 : A is the range of bytes accessed when the step is negative,
2431 : B is the range when the step is positive.
2432 :
2433 : We know that DR_B is a write. We also know (from checking that
2434 : DR_A and DR_B are well-ordered) that for each i in [0, n-1],
2435 : the write performed by access i of DR_B occurs after access numbers
2436 : j<=i of DR_A in both the original and the new code. Any write or
2437 : anti dependencies wrt those DR_A accesses are therefore maintained.
2438 :
2439 : We just need to make sure that each individual write in DR_B does not
2440 : overlap any higher-indexed access in DR_A; such DR_A accesses happen
2441 : after the DR_B access in the original code but happen before it in
2442 : the new code.
2443 :
2444 : We know the steps for both accesses are equal, so by induction, we
2445 : just need to test whether the first write of DR_B overlaps a later
2446 : access of DR_A. In other words, we need to move addr_a along by
2447 : one iteration:
2448 :
2449 : addr_a' = addr_a + step
2450 :
2451 : and check whether:
2452 :
2453 : [addr_b, addr_b + last_chunk_b]
2454 :
2455 : overlaps:
2456 :
2457 : [addr_a' + low_offset_a, addr_a' + high_offset_a + last_chunk_a]
2458 :
2459 : where [low_offset_a, high_offset_a] spans accesses [1, n-1]. I.e.:
2460 :
2461 : low_offset_a = +ve step ? 0 : seg_len_a - step
2462 : high_offset_a = +ve step ? seg_len_a - step : 0
2463 :
2464 : This is equivalent to testing whether:
2465 :
2466 : addr_a' + low_offset_a <= addr_b + last_chunk_b
2467 : && addr_b <= addr_a' + high_offset_a + last_chunk_a
2468 :
2469 : Converting this into a single test, there is an overlap if:
2470 :
2471 : 0 <= addr_b + last_chunk_b - addr_a' - low_offset_a <= limit
2472 :
2473 : where limit = high_offset_a - low_offset_a + last_chunk_a + last_chunk_b
2474 :
2475 : If DR_A is performed, limit + |step| - last_chunk_b is known to be
2476 : less than the size of the object underlying DR_A. We also know
2477 : that last_chunk_b <= |step|; this is checked elsewhere if it isn't
2478 : guaranteed at compile time. There can therefore be no overflow if
2479 : "limit" is calculated in an unsigned type with pointer precision. */
2480 2663 : tree addr_a = fold_build_pointer_plus (DR_BASE_ADDRESS (dr_a.dr),
2481 : DR_OFFSET (dr_a.dr));
2482 2663 : addr_a = fold_build_pointer_plus (addr_a, DR_INIT (dr_a.dr));
2483 :
2484 2663 : tree addr_b = fold_build_pointer_plus (DR_BASE_ADDRESS (dr_b.dr),
2485 : DR_OFFSET (dr_b.dr));
2486 2663 : addr_b = fold_build_pointer_plus (addr_b, DR_INIT (dr_b.dr));
2487 :
2488 : /* Advance ADDR_A by one iteration and adjust the length to compensate. */
2489 2663 : addr_a = fold_build_pointer_plus (addr_a, step);
2490 2663 : tree seg_len_a_minus_step = fold_build2 (MINUS_EXPR, sizetype,
2491 : seg_len_a, step);
2492 2663 : if (!CONSTANT_CLASS_P (seg_len_a_minus_step))
2493 3 : seg_len_a_minus_step = build1 (SAVE_EXPR, sizetype, seg_len_a_minus_step);
2494 :
2495 2663 : tree low_offset_a = fold_build3 (COND_EXPR, sizetype, neg_step,
2496 : seg_len_a_minus_step, size_zero_node);
2497 2663 : if (!CONSTANT_CLASS_P (low_offset_a))
2498 3 : low_offset_a = build1 (SAVE_EXPR, sizetype, low_offset_a);
2499 :
2500 : /* We could use COND_EXPR <neg_step, size_zero_node, seg_len_a_minus_step>,
2501 : but it's usually more efficient to reuse the LOW_OFFSET_A result. */
2502 2663 : tree high_offset_a = fold_build2 (MINUS_EXPR, sizetype, seg_len_a_minus_step,
2503 : low_offset_a);
2504 :
2505 : /* The amount added to addr_b - addr_a'. */
2506 2663 : tree bias = fold_build2 (MINUS_EXPR, sizetype,
2507 : size_int (last_chunk_b), low_offset_a);
2508 :
2509 2663 : tree limit = fold_build2 (MINUS_EXPR, sizetype, high_offset_a, low_offset_a);
2510 2663 : limit = fold_build2 (PLUS_EXPR, sizetype, limit,
2511 : size_int (last_chunk_a + last_chunk_b));
2512 :
2513 2663 : tree subject = fold_build2 (MINUS_EXPR, sizetype,
2514 : fold_convert (sizetype, addr_b),
2515 : fold_convert (sizetype, addr_a));
2516 2663 : subject = fold_build2 (PLUS_EXPR, sizetype, subject, bias);
2517 :
2518 2663 : *cond_expr = fold_build2 (GT_EXPR, boolean_type_node, subject, limit);
2519 2663 : if (dump_enabled_p ())
2520 320 : dump_printf (MSG_NOTE, "using an address-based WAR/WAW test\n");
2521 : return true;
2522 : }
2523 :
2524 : /* If ALIGN is nonzero, set up *SEQ_MIN_OUT and *SEQ_MAX_OUT so that for
2525 : every address ADDR accessed by D:
2526 :
2527 : *SEQ_MIN_OUT <= ADDR (== ADDR & -ALIGN) <= *SEQ_MAX_OUT
2528 :
2529 : In this case, every element accessed by D is aligned to at least
2530 : ALIGN bytes.
2531 :
2532 : If ALIGN is zero then instead set *SEG_MAX_OUT so that:
2533 :
2534 : *SEQ_MIN_OUT <= ADDR < *SEQ_MAX_OUT. */
2535 :
2536 : static void
2537 4264 : get_segment_min_max (const dr_with_seg_len &d, tree *seg_min_out,
2538 : tree *seg_max_out, HOST_WIDE_INT align)
2539 : {
2540 : /* Each access has the following pattern:
2541 :
2542 : <- |seg_len| ->
2543 : <--- A: -ve step --->
2544 : +-----+-------+-----+-------+-----+
2545 : | n-1 | ,.... | 0 | ..... | n-1 |
2546 : +-----+-------+-----+-------+-----+
2547 : <--- B: +ve step --->
2548 : <- |seg_len| ->
2549 : |
2550 : base address
2551 :
2552 : where "n" is the number of scalar iterations covered by the segment.
2553 : (This should be VF for a particular pair if we know that both steps
2554 : are the same, otherwise it will be the full number of scalar loop
2555 : iterations.)
2556 :
2557 : A is the range of bytes accessed when the step is negative,
2558 : B is the range when the step is positive.
2559 :
2560 : If the access size is "access_size" bytes, the lowest addressed byte is:
2561 :
2562 : base + (step < 0 ? seg_len : 0) [LB]
2563 :
2564 : and the highest addressed byte is always below:
2565 :
2566 : base + (step < 0 ? 0 : seg_len) + access_size [UB]
2567 :
2568 : Thus:
2569 :
2570 : LB <= ADDR < UB
2571 :
2572 : If ALIGN is nonzero, all three values are aligned to at least ALIGN
2573 : bytes, so:
2574 :
2575 : LB <= ADDR <= UB - ALIGN
2576 :
2577 : where "- ALIGN" folds naturally with the "+ access_size" and often
2578 : cancels it out.
2579 :
2580 : We don't try to simplify LB and UB beyond this (e.g. by using
2581 : MIN and MAX based on whether seg_len rather than the stride is
2582 : negative) because it is possible for the absolute size of the
2583 : segment to overflow the range of a ssize_t.
2584 :
2585 : Keeping the pointer_plus outside of the cond_expr should allow
2586 : the cond_exprs to be shared with other alias checks. */
2587 4264 : tree indicator = dr_direction_indicator (d.dr);
2588 4264 : tree neg_step = fold_build2 (LT_EXPR, boolean_type_node,
2589 : fold_convert (ssizetype, indicator),
2590 : ssize_int (0));
2591 4264 : tree addr_base = fold_build_pointer_plus (DR_BASE_ADDRESS (d.dr),
2592 : DR_OFFSET (d.dr));
2593 4264 : addr_base = fold_build_pointer_plus (addr_base, DR_INIT (d.dr));
2594 4264 : tree seg_len
2595 4264 : = fold_convert (sizetype, rewrite_to_non_trapping_overflow (d.seg_len));
2596 :
2597 4264 : tree min_reach = fold_build3 (COND_EXPR, sizetype, neg_step,
2598 : seg_len, size_zero_node);
2599 4264 : tree max_reach = fold_build3 (COND_EXPR, sizetype, neg_step,
2600 : size_zero_node, seg_len);
2601 4264 : max_reach = fold_build2 (PLUS_EXPR, sizetype, max_reach,
2602 : size_int (d.access_size - align));
2603 :
2604 4264 : *seg_min_out = fold_build_pointer_plus (addr_base, min_reach);
2605 4264 : *seg_max_out = fold_build_pointer_plus (addr_base, max_reach);
2606 4264 : }
2607 :
2608 : /* Generate a runtime condition that is true if ALIAS_PAIR is free of aliases,
2609 : storing the condition in *COND_EXPR. The fallback is to generate a
2610 : a test that the two accesses do not overlap:
2611 :
2612 : end_a <= start_b || end_b <= start_a. */
2613 :
2614 : static void
2615 4946 : create_intersect_range_checks (class loop *loop, tree *cond_expr,
2616 : const dr_with_seg_len_pair_t &alias_pair)
2617 : {
2618 4946 : const dr_with_seg_len& dr_a = alias_pair.first;
2619 4946 : const dr_with_seg_len& dr_b = alias_pair.second;
2620 4946 : *cond_expr = NULL_TREE;
2621 4946 : if (create_intersect_range_checks_index (loop, cond_expr, alias_pair))
2622 2814 : return;
2623 :
2624 4795 : if (create_ifn_alias_checks (cond_expr, alias_pair))
2625 : return;
2626 :
2627 4795 : if (create_waw_or_war_checks (cond_expr, alias_pair))
2628 : return;
2629 :
2630 2132 : unsigned HOST_WIDE_INT min_align;
2631 2132 : tree_code cmp_code;
2632 : /* We don't have to check DR_ALIAS_MIXED_STEPS here, since both versions
2633 : are equivalent. This is just an optimization heuristic. */
2634 2132 : if (TREE_CODE (DR_STEP (dr_a.dr)) == INTEGER_CST
2635 2040 : && TREE_CODE (DR_STEP (dr_b.dr)) == INTEGER_CST)
2636 : {
2637 : /* In this case adding access_size to seg_len is likely to give
2638 : a simple X * step, where X is either the number of scalar
2639 : iterations or the vectorization factor. We're better off
2640 : keeping that, rather than subtracting an alignment from it.
2641 :
2642 : In this case the maximum values are exclusive and so there is
2643 : no alias if the maximum of one segment equals the minimum
2644 : of another. */
2645 : min_align = 0;
2646 : cmp_code = LE_EXPR;
2647 : }
2648 : else
2649 : {
2650 : /* Calculate the minimum alignment shared by all four pointers,
2651 : then arrange for this alignment to be subtracted from the
2652 : exclusive maximum values to get inclusive maximum values.
2653 : This "- min_align" is cumulative with a "+ access_size"
2654 : in the calculation of the maximum values. In the best
2655 : (and common) case, the two cancel each other out, leaving
2656 : us with an inclusive bound based only on seg_len. In the
2657 : worst case we're simply adding a smaller number than before.
2658 :
2659 : Because the maximum values are inclusive, there is an alias
2660 : if the maximum value of one segment is equal to the minimum
2661 : value of the other. */
2662 200 : min_align = std::min (dr_a.align, dr_b.align);
2663 200 : cmp_code = LT_EXPR;
2664 : }
2665 :
2666 2132 : tree seg_a_min, seg_a_max, seg_b_min, seg_b_max;
2667 2132 : get_segment_min_max (dr_a, &seg_a_min, &seg_a_max, min_align);
2668 2132 : get_segment_min_max (dr_b, &seg_b_min, &seg_b_max, min_align);
2669 :
2670 2132 : *cond_expr
2671 2132 : = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
2672 : fold_build2 (cmp_code, boolean_type_node, seg_a_max, seg_b_min),
2673 : fold_build2 (cmp_code, boolean_type_node, seg_b_max, seg_a_min));
2674 2132 : if (dump_enabled_p ())
2675 297 : dump_printf (MSG_NOTE, "using an address-based overlap test\n");
2676 : }
2677 :
2678 : /* Create a conditional expression that represents the run-time checks for
2679 : overlapping of address ranges represented by a list of data references
2680 : pairs passed in ALIAS_PAIRS. Data references are in LOOP. The returned
2681 : COND_EXPR is the conditional expression to be used in the if statement
2682 : that controls which version of the loop gets executed at runtime. */
2683 :
2684 : void
2685 3294 : create_runtime_alias_checks (class loop *loop,
2686 : const vec<dr_with_seg_len_pair_t> *alias_pairs,
2687 : tree * cond_expr)
2688 : {
2689 3294 : tree part_cond_expr;
2690 :
2691 14828 : for (const dr_with_seg_len_pair_t &alias_pair : alias_pairs)
2692 : {
2693 4946 : gcc_assert (alias_pair.flags);
2694 4946 : if (dump_enabled_p ())
2695 750 : dump_printf (MSG_NOTE,
2696 : "create runtime check for data references %T and %T\n",
2697 750 : DR_REF (alias_pair.first.dr),
2698 750 : DR_REF (alias_pair.second.dr));
2699 :
2700 : /* Create condition expression for each pair data references. */
2701 4946 : create_intersect_range_checks (loop, &part_cond_expr, alias_pair);
2702 4946 : if (*cond_expr)
2703 4861 : *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
2704 : *cond_expr, part_cond_expr);
2705 : else
2706 85 : *cond_expr = part_cond_expr;
2707 : }
2708 3294 : }
2709 :
2710 : /* Check if OFFSET1 and OFFSET2 (DR_OFFSETs of some data-refs) are identical
2711 : expressions. */
2712 : static bool
2713 0 : dr_equal_offsets_p1 (tree offset1, tree offset2)
2714 : {
2715 0 : bool res;
2716 :
2717 0 : STRIP_NOPS (offset1);
2718 0 : STRIP_NOPS (offset2);
2719 :
2720 0 : if (offset1 == offset2)
2721 : return true;
2722 :
2723 0 : if (TREE_CODE (offset1) != TREE_CODE (offset2)
2724 0 : || (!BINARY_CLASS_P (offset1) && !UNARY_CLASS_P (offset1)))
2725 : return false;
2726 :
2727 0 : res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 0),
2728 0 : TREE_OPERAND (offset2, 0));
2729 :
2730 0 : if (!res || !BINARY_CLASS_P (offset1))
2731 : return res;
2732 :
2733 0 : res = dr_equal_offsets_p1 (TREE_OPERAND (offset1, 1),
2734 0 : TREE_OPERAND (offset2, 1));
2735 :
2736 0 : return res;
2737 : }
2738 :
2739 : /* Check if DRA and DRB have equal offsets. */
2740 : bool
2741 0 : dr_equal_offsets_p (struct data_reference *dra,
2742 : struct data_reference *drb)
2743 : {
2744 0 : tree offset1, offset2;
2745 :
2746 0 : offset1 = DR_OFFSET (dra);
2747 0 : offset2 = DR_OFFSET (drb);
2748 :
2749 0 : return dr_equal_offsets_p1 (offset1, offset2);
2750 : }
2751 :
2752 : /* Returns true if FNA == FNB. */
2753 :
2754 : static bool
2755 0 : affine_function_equal_p (affine_fn fna, affine_fn fnb)
2756 : {
2757 0 : unsigned i, n = fna.length ();
2758 :
2759 0 : if (n != fnb.length ())
2760 : return false;
2761 :
2762 0 : for (i = 0; i < n; i++)
2763 0 : if (!operand_equal_p (fna[i], fnb[i], 0))
2764 : return false;
2765 :
2766 : return true;
2767 : }
2768 :
2769 : /* If all the functions in CF are the same, returns one of them,
2770 : otherwise returns NULL. */
2771 :
2772 : static affine_fn
2773 2302282 : common_affine_function (conflict_function *cf)
2774 : {
2775 2302282 : unsigned i;
2776 2302282 : affine_fn comm;
2777 :
2778 2302282 : if (!CF_NONTRIVIAL_P (cf))
2779 0 : return affine_fn ();
2780 :
2781 2302282 : comm = cf->fns[0];
2782 :
2783 2302282 : for (i = 1; i < cf->n; i++)
2784 0 : if (!affine_function_equal_p (comm, cf->fns[i]))
2785 0 : return affine_fn ();
2786 :
2787 2302282 : return comm;
2788 : }
2789 :
2790 : /* Returns the base of the affine function FN. */
2791 :
2792 : static tree
2793 1326488 : affine_function_base (affine_fn fn)
2794 : {
2795 0 : return fn[0];
2796 : }
2797 :
2798 : /* Returns true if FN is a constant. */
2799 :
2800 : static bool
2801 1326797 : affine_function_constant_p (affine_fn fn)
2802 : {
2803 1326797 : unsigned i;
2804 1326797 : tree coef;
2805 :
2806 1384548 : for (i = 1; fn.iterate (i, &coef); i++)
2807 58060 : if (!integer_zerop (coef))
2808 : return false;
2809 :
2810 : return true;
2811 : }
2812 :
2813 : /* Returns true if FN is the zero constant function. */
2814 :
2815 : static bool
2816 175656 : affine_function_zero_p (affine_fn fn)
2817 : {
2818 175656 : return (integer_zerop (affine_function_base (fn))
2819 175656 : && affine_function_constant_p (fn));
2820 : }
2821 :
2822 : /* Returns a signed integer type with the largest precision from TA
2823 : and TB. */
2824 :
2825 : static tree
2826 1740017 : signed_type_for_types (tree ta, tree tb)
2827 : {
2828 1740017 : if (TYPE_PRECISION (ta) > TYPE_PRECISION (tb))
2829 565 : return signed_type_for (ta);
2830 : else
2831 1739452 : return signed_type_for (tb);
2832 : }
2833 :
2834 : /* Applies operation OP on affine functions FNA and FNB, and returns the
2835 : result. */
2836 :
2837 : static affine_fn
2838 1151141 : affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
2839 : {
2840 1151141 : unsigned i, n, m;
2841 1151141 : affine_fn ret;
2842 1151141 : tree coef;
2843 :
2844 3453423 : if (fnb.length () > fna.length ())
2845 : {
2846 0 : n = fna.length ();
2847 0 : m = fnb.length ();
2848 : }
2849 : else
2850 : {
2851 1151141 : n = fnb.length ();
2852 1151141 : m = fna.length ();
2853 : }
2854 :
2855 1151141 : ret.create (m);
2856 3511483 : for (i = 0; i < n; i++)
2857 : {
2858 2418402 : tree type = signed_type_for_types (TREE_TYPE (fna[i]),
2859 1209201 : TREE_TYPE (fnb[i]));
2860 1209201 : ret.quick_push (fold_build2 (op, type, fna[i], fnb[i]));
2861 : }
2862 :
2863 1151141 : for (; fna.iterate (i, &coef); i++)
2864 0 : ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
2865 : coef, integer_zero_node));
2866 1151141 : for (; fnb.iterate (i, &coef); i++)
2867 0 : ret.quick_push (fold_build2 (op, signed_type_for (TREE_TYPE (coef)),
2868 : integer_zero_node, coef));
2869 :
2870 1151141 : return ret;
2871 : }
2872 :
2873 : /* Returns the sum of affine functions FNA and FNB. */
2874 :
2875 : static affine_fn
2876 0 : affine_fn_plus (affine_fn fna, affine_fn fnb)
2877 : {
2878 0 : return affine_fn_op (PLUS_EXPR, fna, fnb);
2879 : }
2880 :
2881 : /* Returns the difference of affine functions FNA and FNB. */
2882 :
2883 : static affine_fn
2884 1151141 : affine_fn_minus (affine_fn fna, affine_fn fnb)
2885 : {
2886 0 : return affine_fn_op (MINUS_EXPR, fna, fnb);
2887 : }
2888 :
2889 : /* Frees affine function FN. */
2890 :
2891 : static void
2892 3655123 : affine_fn_free (affine_fn fn)
2893 : {
2894 0 : fn.release ();
2895 0 : }
2896 :
2897 : /* Determine for each subscript in the data dependence relation DDR
2898 : the distance. */
2899 :
2900 : static void
2901 3093831 : compute_subscript_distance (struct data_dependence_relation *ddr)
2902 : {
2903 3093831 : conflict_function *cf_a, *cf_b;
2904 3093831 : affine_fn fn_a, fn_b, diff;
2905 :
2906 3093831 : if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
2907 : {
2908 : unsigned int i;
2909 :
2910 4244972 : for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
2911 : {
2912 1151141 : struct subscript *subscript;
2913 :
2914 1151141 : subscript = DDR_SUBSCRIPT (ddr, i);
2915 1151141 : cf_a = SUB_CONFLICTS_IN_A (subscript);
2916 1151141 : cf_b = SUB_CONFLICTS_IN_B (subscript);
2917 :
2918 1151141 : fn_a = common_affine_function (cf_a);
2919 1151141 : fn_b = common_affine_function (cf_b);
2920 1151141 : if (!fn_a.exists () || !fn_b.exists ())
2921 : {
2922 0 : SUB_DISTANCE (subscript) = chrec_dont_know;
2923 0 : return;
2924 : }
2925 1151141 : diff = affine_fn_minus (fn_a, fn_b);
2926 :
2927 1151141 : if (affine_function_constant_p (diff))
2928 1150832 : SUB_DISTANCE (subscript) = affine_function_base (diff);
2929 : else
2930 309 : SUB_DISTANCE (subscript) = chrec_dont_know;
2931 :
2932 1151141 : affine_fn_free (diff);
2933 : }
2934 : }
2935 : }
2936 :
2937 : /* Returns the conflict function for "unknown". */
2938 :
2939 : static conflict_function *
2940 8031092 : conflict_fn_not_known (void)
2941 : {
2942 0 : conflict_function *fn = XCNEW (conflict_function);
2943 8031092 : fn->n = NOT_KNOWN;
2944 :
2945 8031092 : return fn;
2946 : }
2947 :
2948 : /* Returns the conflict function for "independent". */
2949 :
2950 : static conflict_function *
2951 4297780 : conflict_fn_no_dependence (void)
2952 : {
2953 0 : conflict_function *fn = XCNEW (conflict_function);
2954 4297780 : fn->n = NO_DEPENDENCE;
2955 :
2956 4297780 : return fn;
2957 : }
2958 :
2959 : /* Returns true if the address of OBJ is invariant in LOOP. */
2960 :
2961 : static bool
2962 3292589 : object_address_invariant_in_loop_p (const class loop *loop, const_tree obj)
2963 : {
2964 3458213 : while (handled_component_p (obj))
2965 : {
2966 171039 : if (TREE_CODE (obj) == ARRAY_REF)
2967 : {
2968 9747 : for (int i = 1; i < 4; ++i)
2969 8664 : if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, i),
2970 8664 : loop->num))
2971 : return false;
2972 : }
2973 164541 : else if (TREE_CODE (obj) == COMPONENT_REF)
2974 : {
2975 143312 : if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 2),
2976 143312 : loop->num))
2977 : return false;
2978 : }
2979 165624 : obj = TREE_OPERAND (obj, 0);
2980 : }
2981 :
2982 3287174 : if (!INDIRECT_REF_P (obj)
2983 3287174 : && TREE_CODE (obj) != MEM_REF)
2984 : return true;
2985 :
2986 3262631 : return !chrec_contains_symbols_defined_in_loop (TREE_OPERAND (obj, 0),
2987 6525262 : loop->num);
2988 : }
2989 :
2990 : /* Helper for contains_ssa_ref_p. */
2991 :
2992 : static bool
2993 100452 : contains_ssa_ref_p_1 (tree, tree *idx, void *data)
2994 : {
2995 100452 : if (TREE_CODE (*idx) == SSA_NAME)
2996 : {
2997 93831 : *(bool *)data = true;
2998 93831 : return false;
2999 : }
3000 : return true;
3001 : }
3002 :
3003 : /* Returns true if the reference REF contains a SSA index. */
3004 :
3005 : static bool
3006 256347 : contains_ssa_ref_p (tree ref)
3007 : {
3008 256347 : bool res = false;
3009 0 : for_each_index (&ref, contains_ssa_ref_p_1, &res);
3010 256347 : return res;
3011 : }
3012 :
3013 : /* Returns false if we can prove that data references A and B do not alias,
3014 : true otherwise. If LOOP_NEST is false no cross-iteration aliases are
3015 : considered. */
3016 :
3017 : bool
3018 14694084 : dr_may_alias_p (const struct data_reference *a, const struct data_reference *b,
3019 : class loop *loop_nest)
3020 : {
3021 14694084 : tree addr_a = DR_BASE_OBJECT (a);
3022 14694084 : tree addr_b = DR_BASE_OBJECT (b);
3023 :
3024 : /* If we are not processing a loop nest but scalar code we
3025 : do not need to care about possible cross-iteration dependences
3026 : and thus can process the full original reference. Do so,
3027 : similar to how loop invariant motion applies extra offset-based
3028 : disambiguation. */
3029 14694084 : if (!loop_nest)
3030 : {
3031 8196588 : tree tree_size_a = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (a)));
3032 8196588 : tree tree_size_b = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (b)));
3033 :
3034 8196588 : if (DR_BASE_ADDRESS (a)
3035 8188007 : && DR_BASE_ADDRESS (b)
3036 8187660 : && operand_equal_p (DR_BASE_ADDRESS (a), DR_BASE_ADDRESS (b))
3037 7333433 : && operand_equal_p (DR_OFFSET (a), DR_OFFSET (b))
3038 7245223 : && tree_size_a
3039 7245223 : && tree_size_b
3040 7245214 : && poly_int_tree_p (tree_size_a)
3041 7245188 : && poly_int_tree_p (tree_size_b)
3042 15441776 : && !ranges_maybe_overlap_p (wi::to_poly_widest (DR_INIT (a)),
3043 7245188 : wi::to_poly_widest (tree_size_a),
3044 7245188 : wi::to_poly_widest (DR_INIT (b)),
3045 7245188 : wi::to_poly_widest (tree_size_b)))
3046 : {
3047 5405624 : gcc_assert (integer_zerop (DR_STEP (a))
3048 : && integer_zerop (DR_STEP (b)));
3049 5405657 : return false;
3050 : }
3051 :
3052 5581928 : aff_tree off1, off2;
3053 2790964 : poly_widest_int size1, size2;
3054 2790964 : get_inner_reference_aff (DR_REF (a), &off1, &size1);
3055 2790964 : get_inner_reference_aff (DR_REF (b), &off2, &size2);
3056 2790964 : aff_combination_scale (&off1, -1);
3057 2790964 : aff_combination_add (&off2, &off1);
3058 2790964 : if (aff_comb_cannot_overlap_p (&off2, size1, size2))
3059 33 : return false;
3060 2790964 : }
3061 :
3062 : /* Try the points-to information recorded for the base pointers the
3063 : references were originally analyzed from. DR_BASE_OBJECT can be less
3064 : precise, rooting at another SSA name or at one created after points-to
3065 : information was computed and thus without SSA_NAME_PTR_INFO. A
3066 : recorded solution is not revalidated. It covers every dynamic value
3067 : of its SSA name, so it holds for cross-iteration queries as well. */
3068 9288427 : struct ptr_info_def *pi_a = DR_PTR_INFO (a);
3069 9288427 : struct ptr_info_def *pi_b = DR_PTR_INFO (b);
3070 9288427 : if (pi_a && pi_b
3071 9288427 : && !pt_solutions_intersect (&pi_a->pt, &pi_b->pt))
3072 : return false;
3073 :
3074 8877116 : if ((TREE_CODE (addr_a) == MEM_REF || TREE_CODE (addr_a) == TARGET_MEM_REF)
3075 6521177 : && (TREE_CODE (addr_b) == MEM_REF || TREE_CODE (addr_b) == TARGET_MEM_REF)
3076 : /* For cross-iteration dependences the cliques must be valid for the
3077 : whole loop, not just individual iterations. */
3078 6263425 : && (!loop_nest
3079 5954252 : || MR_DEPENDENCE_CLIQUE (addr_a) == 1
3080 5286725 : || MR_DEPENDENCE_CLIQUE (addr_a) == loop_nest->owned_clique)
3081 6042914 : && MR_DEPENDENCE_CLIQUE (addr_a) == MR_DEPENDENCE_CLIQUE (addr_b)
3082 14726584 : && MR_DEPENDENCE_BASE (addr_a) != MR_DEPENDENCE_BASE (addr_b))
3083 : return false;
3084 :
3085 : /* If we had an evolution in a pointer-based MEM_REF BASE_OBJECT we
3086 : do not know the size of the base-object. So we cannot do any
3087 : offset/overlap based analysis but have to rely on points-to
3088 : information only. */
3089 8854255 : if (TREE_CODE (addr_a) == MEM_REF
3090 8854255 : && (DR_UNCONSTRAINED_BASE (a)
3091 4157633 : || TREE_CODE (TREE_OPERAND (addr_a, 0)) == SSA_NAME))
3092 : {
3093 : /* For true dependences we can apply TBAA. */
3094 4013844 : if (flag_strict_aliasing
3095 3838964 : && DR_IS_WRITE (a) && DR_IS_READ (b)
3096 4167006 : && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
3097 153162 : get_alias_set (DR_REF (b))))
3098 : return false;
3099 3984882 : if (TREE_CODE (addr_b) == MEM_REF)
3100 3877225 : return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
3101 7754450 : TREE_OPERAND (addr_b, 0));
3102 : else
3103 107657 : return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
3104 107657 : build_fold_addr_expr (addr_b));
3105 : }
3106 4840411 : else if (TREE_CODE (addr_b) == MEM_REF
3107 4840411 : && (DR_UNCONSTRAINED_BASE (b)
3108 2548164 : || TREE_CODE (TREE_OPERAND (addr_b, 0)) == SSA_NAME))
3109 : {
3110 : /* For true dependences we can apply TBAA. */
3111 329038 : if (flag_strict_aliasing
3112 270979 : && DR_IS_WRITE (a) && DR_IS_READ (b)
3113 405776 : && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
3114 76738 : get_alias_set (DR_REF (b))))
3115 : return false;
3116 313802 : if (TREE_CODE (addr_a) == MEM_REF)
3117 183519 : return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
3118 367038 : TREE_OPERAND (addr_b, 0));
3119 : else
3120 130283 : return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
3121 260566 : TREE_OPERAND (addr_b, 0));
3122 : }
3123 : /* If dr_analyze_innermost failed to handle a component we are
3124 : possibly left with a non-base in which case we didn't analyze
3125 : a possible evolution of the base when analyzing a loop. */
3126 4511373 : else if (loop_nest
3127 6670450 : && ((handled_component_p (addr_a) && contains_ssa_ref_p (addr_a))
3128 83670 : || (handled_component_p (addr_b) && contains_ssa_ref_p (addr_b))))
3129 : {
3130 : /* For true dependences we can apply TBAA. */
3131 93831 : if (flag_strict_aliasing
3132 93202 : && DR_IS_WRITE (a) && DR_IS_READ (b)
3133 103273 : && !alias_sets_conflict_p (get_alias_set (DR_REF (a)),
3134 9442 : get_alias_set (DR_REF (b))))
3135 : return false;
3136 89708 : if (TREE_CODE (addr_a) == MEM_REF)
3137 3845 : return ptr_derefs_may_alias_p (TREE_OPERAND (addr_a, 0),
3138 3845 : build_fold_addr_expr (addr_b));
3139 85863 : else if (TREE_CODE (addr_b) == MEM_REF)
3140 6366 : return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
3141 12732 : TREE_OPERAND (addr_b, 0));
3142 : else
3143 79497 : return ptr_derefs_may_alias_p (build_fold_addr_expr (addr_a),
3144 79497 : build_fold_addr_expr (addr_b));
3145 : }
3146 :
3147 : /* Otherwise DR_BASE_OBJECT is an access that covers the whole object
3148 : that is being subsetted in the loop nest. */
3149 4417542 : if (DR_IS_WRITE (a) && DR_IS_WRITE (b))
3150 2983652 : return refs_output_dependent_p (addr_a, addr_b);
3151 1433890 : else if (DR_IS_READ (a) && DR_IS_WRITE (b))
3152 406932 : return refs_anti_dependent_p (addr_a, addr_b);
3153 1026958 : return refs_may_alias_p (addr_a, addr_b);
3154 : }
3155 :
3156 : /* REF_A and REF_B both satisfy access_fn_component_p. Return true
3157 : if it is meaningful to compare their associated access functions
3158 : when checking for dependencies. */
3159 :
3160 : static bool
3161 2899717 : access_fn_components_comparable_p (tree ref_a, tree ref_b)
3162 : {
3163 : /* Allow pairs of component refs from the following sets:
3164 :
3165 : { REALPART_EXPR, IMAGPART_EXPR }
3166 : { COMPONENT_REF }
3167 : { ARRAY_REF }. */
3168 2899717 : tree_code code_a = TREE_CODE (ref_a);
3169 2899717 : tree_code code_b = TREE_CODE (ref_b);
3170 2899717 : if (code_a == IMAGPART_EXPR)
3171 36593 : code_a = REALPART_EXPR;
3172 2899717 : if (code_b == IMAGPART_EXPR)
3173 42911 : code_b = REALPART_EXPR;
3174 2899717 : if (code_a != code_b)
3175 : return false;
3176 :
3177 2875115 : if (TREE_CODE (ref_a) == COMPONENT_REF)
3178 : /* ??? We cannot simply use the type of operand #0 of the refs here as
3179 : the Fortran compiler smuggles type punning into COMPONENT_REFs.
3180 : Use the DECL_CONTEXT of the FIELD_DECLs instead. */
3181 982660 : return (DECL_CONTEXT (TREE_OPERAND (ref_a, 1))
3182 982660 : == DECL_CONTEXT (TREE_OPERAND (ref_b, 1)));
3183 :
3184 1892455 : return types_compatible_p (TREE_TYPE (TREE_OPERAND (ref_a, 0)),
3185 3784910 : TREE_TYPE (TREE_OPERAND (ref_b, 0)));
3186 : }
3187 :
3188 : /* Initialize a data dependence relation RES in LOOP_NEST. USE_ALT_INDICES
3189 : is true when the main indices of A and B were not comparable so we try again
3190 : with alternate indices computed on an indirect reference. */
3191 :
3192 : struct data_dependence_relation *
3193 6253392 : initialize_data_dependence_relation (struct data_dependence_relation *res,
3194 : vec<loop_p> loop_nest,
3195 : bool use_alt_indices)
3196 : {
3197 6623717 : struct data_reference *a = DDR_A (res);
3198 6623717 : struct data_reference *b = DDR_B (res);
3199 6623717 : unsigned int i;
3200 :
3201 6623717 : struct indices *indices_a = &a->indices;
3202 6623717 : struct indices *indices_b = &b->indices;
3203 6623717 : if (use_alt_indices)
3204 : {
3205 370325 : if (TREE_CODE (DR_REF (a)) != MEM_REF)
3206 231547 : indices_a = &a->alt_indices;
3207 370325 : if (TREE_CODE (DR_REF (b)) != MEM_REF)
3208 263080 : indices_b = &b->alt_indices;
3209 : }
3210 6623717 : unsigned int num_dimensions_a = indices_a->access_fns.length ();
3211 6623717 : unsigned int num_dimensions_b = indices_b->access_fns.length ();
3212 6623717 : if (num_dimensions_a == 0 || num_dimensions_b == 0)
3213 : {
3214 2264437 : DDR_ARE_DEPENDENT (res) = chrec_dont_know;
3215 2264437 : return res;
3216 : }
3217 :
3218 : /* For unconstrained bases, the root (highest-indexed) subscript
3219 : describes a variation in the base of the original DR_REF rather
3220 : than a component access. We have no type that accurately describes
3221 : the new DR_BASE_OBJECT (whose TREE_TYPE describes the type *after*
3222 : applying this subscript) so limit the search to the last real
3223 : component access.
3224 :
3225 : E.g. for:
3226 :
3227 : void
3228 : f (int a[][8], int b[][8])
3229 : {
3230 : for (int i = 0; i < 8; ++i)
3231 : a[i * 2][0] = b[i][0];
3232 : }
3233 :
3234 : the a and b accesses have a single ARRAY_REF component reference [0]
3235 : but have two subscripts. */
3236 4359280 : if (indices_a->unconstrained_base)
3237 2481498 : num_dimensions_a -= 1;
3238 4359280 : if (indices_b->unconstrained_base)
3239 2434796 : num_dimensions_b -= 1;
3240 :
3241 : /* These structures describe sequences of component references in
3242 : DR_REF (A) and DR_REF (B). Each component reference is tied to a
3243 : specific access function. */
3244 4359280 : struct {
3245 : /* The sequence starts at DR_ACCESS_FN (A, START_A) of A and
3246 : DR_ACCESS_FN (B, START_B) of B (inclusive) and extends to higher
3247 : indices. In C notation, these are the indices of the rightmost
3248 : component references; e.g. for a sequence .b.c.d, the start
3249 : index is for .d. */
3250 : unsigned int start_a;
3251 : unsigned int start_b;
3252 :
3253 : /* The sequence contains LENGTH consecutive access functions from
3254 : each DR. */
3255 : unsigned int length;
3256 :
3257 : /* The enclosing objects for the A and B sequences respectively,
3258 : i.e. the objects to which DR_ACCESS_FN (A, START_A + LENGTH - 1)
3259 : and DR_ACCESS_FN (B, START_B + LENGTH - 1) are applied. */
3260 : tree object_a;
3261 : tree object_b;
3262 4359280 : } full_seq = {}, struct_seq = {};
3263 :
3264 : /* Before each iteration of the loop:
3265 :
3266 : - REF_A is what you get after applying DR_ACCESS_FN (A, INDEX_A) and
3267 : - REF_B is what you get after applying DR_ACCESS_FN (B, INDEX_B). */
3268 4359280 : unsigned int index_a = 0;
3269 4359280 : unsigned int index_b = 0;
3270 4359280 : tree ref_a = DR_REF (a);
3271 4359280 : tree ref_b = DR_REF (b);
3272 :
3273 : /* Now walk the component references from the final DR_REFs back up to
3274 : the enclosing base objects. Each component reference corresponds
3275 : to one access function in the DR, with access function 0 being for
3276 : the final DR_REF and the highest-indexed access function being the
3277 : one that is applied to the base of the DR.
3278 :
3279 : Look for a sequence of component references whose access functions
3280 : are comparable (see access_fn_components_comparable_p). If more
3281 : than one such sequence exists, pick the one nearest the base
3282 : (which is the leftmost sequence in C notation). Store this sequence
3283 : in FULL_SEQ.
3284 :
3285 : For example, if we have:
3286 :
3287 : struct foo { struct bar s; ... } (*a)[10], (*b)[10];
3288 :
3289 : A: a[0][i].s.c.d
3290 : B: __real b[0][i].s.e[i].f
3291 :
3292 : (where d is the same type as the real component of f) then the access
3293 : functions would be:
3294 :
3295 : 0 1 2 3
3296 : A: .d .c .s [i]
3297 :
3298 : 0 1 2 3 4 5
3299 : B: __real .f [i] .e .s [i]
3300 :
3301 : The A0/B2 column isn't comparable, since .d is a COMPONENT_REF
3302 : and [i] is an ARRAY_REF. However, the A1/B3 column contains two
3303 : COMPONENT_REF accesses for struct bar, so is comparable. Likewise
3304 : the A2/B4 column contains two COMPONENT_REF accesses for struct foo,
3305 : so is comparable. The A3/B5 column contains two ARRAY_REFs that
3306 : index foo[10] arrays, so is again comparable. The sequence is
3307 : therefore:
3308 :
3309 : A: [1, 3] (i.e. [i].s.c)
3310 : B: [3, 5] (i.e. [i].s.e)
3311 :
3312 : Also look for sequences of component references whose access
3313 : functions are comparable and whose enclosing objects have the same
3314 : RECORD_TYPE. Store this sequence in STRUCT_SEQ. In the above
3315 : example, STRUCT_SEQ would be:
3316 :
3317 : A: [1, 2] (i.e. s.c)
3318 : B: [3, 4] (i.e. s.e) */
3319 7246087 : while (index_a < num_dimensions_a && index_b < num_dimensions_b)
3320 : {
3321 : /* The alternate indices form always has a single dimension
3322 : with unconstrained base. */
3323 2899717 : gcc_assert (!use_alt_indices);
3324 :
3325 : /* REF_A and REF_B must be one of the component access types
3326 : allowed by dr_analyze_indices. */
3327 2899717 : gcc_checking_assert (access_fn_component_p (ref_a));
3328 2899717 : gcc_checking_assert (access_fn_component_p (ref_b));
3329 :
3330 : /* Get the immediately-enclosing objects for REF_A and REF_B,
3331 : i.e. the references *before* applying DR_ACCESS_FN (A, INDEX_A)
3332 : and DR_ACCESS_FN (B, INDEX_B). */
3333 2899717 : tree object_a = TREE_OPERAND (ref_a, 0);
3334 2899717 : tree object_b = TREE_OPERAND (ref_b, 0);
3335 :
3336 2899717 : tree type_a = TREE_TYPE (object_a);
3337 2899717 : tree type_b = TREE_TYPE (object_b);
3338 2899717 : if (access_fn_components_comparable_p (ref_a, ref_b))
3339 : {
3340 : /* This pair of component accesses is comparable for dependence
3341 : analysis, so we can include DR_ACCESS_FN (A, INDEX_A) and
3342 : DR_ACCESS_FN (B, INDEX_B) in the sequence. */
3343 2647049 : if (full_seq.start_a + full_seq.length != index_a
3344 2590517 : || full_seq.start_b + full_seq.length != index_b)
3345 : {
3346 : /* The accesses don't extend the current sequence,
3347 : so start a new one here. */
3348 64192 : full_seq.start_a = index_a;
3349 64192 : full_seq.start_b = index_b;
3350 64192 : full_seq.length = 0;
3351 : }
3352 :
3353 : /* Add this pair of references to the sequence. */
3354 2647049 : full_seq.length += 1;
3355 2647049 : full_seq.object_a = object_a;
3356 2647049 : full_seq.object_b = object_b;
3357 :
3358 : /* If the enclosing objects are structures (and thus have the
3359 : same RECORD_TYPE), record the new sequence in STRUCT_SEQ. */
3360 2647049 : if (TREE_CODE (type_a) == RECORD_TYPE)
3361 770978 : struct_seq = full_seq;
3362 :
3363 : /* Move to the next containing reference for both A and B. */
3364 2647049 : ref_a = object_a;
3365 2647049 : ref_b = object_b;
3366 2647049 : index_a += 1;
3367 2647049 : index_b += 1;
3368 2647049 : continue;
3369 : }
3370 :
3371 : /* Try to approach equal type sizes. */
3372 252668 : if (!COMPLETE_TYPE_P (type_a)
3373 249627 : || !COMPLETE_TYPE_P (type_b)
3374 241633 : || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type_a))
3375 492705 : || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type_b)))
3376 : break;
3377 :
3378 239758 : unsigned HOST_WIDE_INT size_a = tree_to_uhwi (TYPE_SIZE_UNIT (type_a));
3379 239758 : unsigned HOST_WIDE_INT size_b = tree_to_uhwi (TYPE_SIZE_UNIT (type_b));
3380 239758 : if (size_a <= size_b)
3381 : {
3382 144980 : index_a += 1;
3383 144980 : ref_a = object_a;
3384 : }
3385 239758 : if (size_b <= size_a)
3386 : {
3387 109916 : index_b += 1;
3388 109916 : ref_b = object_b;
3389 : }
3390 : }
3391 :
3392 : /* See whether FULL_SEQ ends at the base and whether the two bases
3393 : are equal. We do not care about TBAA or alignment info so we can
3394 : use OEP_ADDRESS_OF to avoid false negatives. */
3395 4359280 : tree base_a = indices_a->base_object;
3396 4359280 : tree base_b = indices_b->base_object;
3397 4359280 : bool same_base_p = (full_seq.start_a + full_seq.length == num_dimensions_a
3398 4154329 : && full_seq.start_b + full_seq.length == num_dimensions_b
3399 4002005 : && (indices_a->unconstrained_base
3400 4002005 : == indices_b->unconstrained_base)
3401 3996807 : && operand_equal_p (base_a, base_b, OEP_ADDRESS_OF)
3402 3522848 : && (types_compatible_p (TREE_TYPE (base_a),
3403 3522848 : TREE_TYPE (base_b))
3404 863303 : || (!base_supports_access_fn_components_p (base_a)
3405 858030 : && !base_supports_access_fn_components_p (base_b)
3406 856406 : && operand_equal_p
3407 856406 : (TYPE_SIZE (TREE_TYPE (base_a)),
3408 856406 : TYPE_SIZE (TREE_TYPE (base_b)), 0)))
3409 7427712 : && (!loop_nest.exists ()
3410 3068432 : || (object_address_invariant_in_loop_p
3411 3068432 : (loop_nest[0], base_a))));
3412 :
3413 : /* If the bases are the same, we can include the base variation too.
3414 : E.g. the b accesses in:
3415 :
3416 : for (int i = 0; i < n; ++i)
3417 : b[i + 4][0] = b[i][0];
3418 :
3419 : have a definite dependence distance of 4, while for:
3420 :
3421 : for (int i = 0; i < n; ++i)
3422 : a[i + 4][0] = b[i][0];
3423 :
3424 : the dependence distance depends on the gap between a and b.
3425 :
3426 : If the bases are different then we can only rely on the sequence
3427 : rooted at a structure access, since arrays are allowed to overlap
3428 : arbitrarily and change shape arbitrarily. E.g. we treat this as
3429 : valid code:
3430 :
3431 : int a[256];
3432 : ...
3433 : ((int (*)[4][3]) &a[1])[i][0] += ((int (*)[4][3]) &a[2])[i][0];
3434 :
3435 : where two lvalues with the same int[4][3] type overlap, and where
3436 : both lvalues are distinct from the object's declared type. */
3437 : if (same_base_p)
3438 : {
3439 2944986 : if (indices_a->unconstrained_base)
3440 1485605 : full_seq.length += 1;
3441 : }
3442 : else
3443 : full_seq = struct_seq;
3444 :
3445 : /* Punt if we didn't find a suitable sequence. */
3446 4359280 : if (full_seq.length == 0)
3447 : {
3448 1139493 : if (use_alt_indices
3449 1017817 : || (TREE_CODE (DR_REF (a)) == MEM_REF
3450 785474 : && TREE_CODE (DR_REF (b)) == MEM_REF)
3451 372501 : || may_be_nonaddressable_p (DR_REF (a))
3452 1511585 : || may_be_nonaddressable_p (DR_REF (b)))
3453 : {
3454 : /* Fully exhausted possibilities. */
3455 769168 : DDR_ARE_DEPENDENT (res) = chrec_dont_know;
3456 769168 : return res;
3457 : }
3458 :
3459 : /* Try evaluating both DRs as dereferences of pointers. */
3460 370325 : if (!a->alt_indices.base_object
3461 173312 : && TREE_CODE (DR_REF (a)) != MEM_REF)
3462 : {
3463 34534 : tree alt_ref = build2 (MEM_REF, TREE_TYPE (DR_REF (a)),
3464 : build1 (ADDR_EXPR, ptr_type_node, DR_REF (a)),
3465 : build_int_cst
3466 : (reference_alias_ptr_type (DR_REF (a)), 0));
3467 103602 : dr_analyze_indices (&a->alt_indices, alt_ref,
3468 34534 : loop_preheader_edge (loop_nest[0]),
3469 : loop_containing_stmt (DR_STMT (a)));
3470 : }
3471 370325 : if (!b->alt_indices.base_object
3472 187794 : && TREE_CODE (DR_REF (b)) != MEM_REF)
3473 : {
3474 80549 : tree alt_ref = build2 (MEM_REF, TREE_TYPE (DR_REF (b)),
3475 : build1 (ADDR_EXPR, ptr_type_node, DR_REF (b)),
3476 : build_int_cst
3477 : (reference_alias_ptr_type (DR_REF (b)), 0));
3478 241647 : dr_analyze_indices (&b->alt_indices, alt_ref,
3479 80549 : loop_preheader_edge (loop_nest[0]),
3480 : loop_containing_stmt (DR_STMT (b)));
3481 : }
3482 370325 : return initialize_data_dependence_relation (res, loop_nest, true);
3483 : }
3484 :
3485 3219787 : if (!same_base_p)
3486 : {
3487 : /* Partial overlap is possible for different bases when strict aliasing
3488 : is not in effect. It's also possible if either base involves a union
3489 : access; e.g. for:
3490 :
3491 : struct s1 { int a[2]; };
3492 : struct s2 { struct s1 b; int c; };
3493 : struct s3 { int d; struct s1 e; };
3494 : union u { struct s2 f; struct s3 g; } *p, *q;
3495 :
3496 : the s1 at "p->f.b" (base "p->f") partially overlaps the s1 at
3497 : "p->g.e" (base "p->g") and might partially overlap the s1 at
3498 : "q->g.e" (base "q->g"). */
3499 274801 : if (!flag_strict_aliasing
3500 263148 : || ref_contains_union_access_p (full_seq.object_a)
3501 479909 : || ref_contains_union_access_p (full_seq.object_b))
3502 : {
3503 69731 : DDR_ARE_DEPENDENT (res) = chrec_dont_know;
3504 69731 : return res;
3505 : }
3506 :
3507 205070 : DDR_COULD_BE_INDEPENDENT_P (res) = true;
3508 205070 : if (!loop_nest.exists ()
3509 410140 : || (object_address_invariant_in_loop_p (loop_nest[0],
3510 205070 : full_seq.object_a)
3511 19087 : && object_address_invariant_in_loop_p (loop_nest[0],
3512 19087 : full_seq.object_b)))
3513 : {
3514 9301 : DDR_OBJECT_A (res) = full_seq.object_a;
3515 9301 : DDR_OBJECT_B (res) = full_seq.object_b;
3516 : }
3517 : }
3518 :
3519 3150056 : DDR_AFFINE_P (res) = true;
3520 3150056 : DDR_ARE_DEPENDENT (res) = NULL_TREE;
3521 3150056 : DDR_SUBSCRIPTS (res).create (full_seq.length);
3522 3150056 : DDR_LOOP_NEST (res) = loop_nest;
3523 3150056 : DDR_SELF_REFERENCE (res) = false;
3524 :
3525 7136019 : for (i = 0; i < full_seq.length; ++i)
3526 : {
3527 3985963 : struct subscript *subscript;
3528 :
3529 3985963 : subscript = XNEW (struct subscript);
3530 3985963 : SUB_ACCESS_FN (subscript, 0) = indices_a->access_fns[full_seq.start_a + i];
3531 3985963 : SUB_ACCESS_FN (subscript, 1) = indices_b->access_fns[full_seq.start_b + i];
3532 3985963 : SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
3533 3985963 : SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
3534 3985963 : SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
3535 3985963 : SUB_DISTANCE (subscript) = chrec_dont_know;
3536 3985963 : DDR_SUBSCRIPTS (res).safe_push (subscript);
3537 : }
3538 :
3539 : return res;
3540 : }
3541 :
3542 : /* Initialize a data dependence relation between data accesses A and
3543 : B. NB_LOOPS is the number of loops surrounding the references: the
3544 : size of the classic distance/direction vectors. */
3545 :
3546 : struct data_dependence_relation *
3547 13446231 : initialize_data_dependence_relation (struct data_reference *a,
3548 : struct data_reference *b,
3549 : vec<loop_p> loop_nest)
3550 : {
3551 13446231 : data_dependence_relation *res = XCNEW (struct data_dependence_relation);
3552 13446231 : DDR_A (res) = a;
3553 13446231 : DDR_B (res) = b;
3554 13446231 : DDR_LOOP_NEST (res).create (0);
3555 13446231 : DDR_SUBSCRIPTS (res).create (0);
3556 13446231 : DDR_DIR_VECTS (res).create (0);
3557 13446231 : DDR_DIST_VECTS (res).create (0);
3558 :
3559 13446231 : if (a == NULL || b == NULL)
3560 : {
3561 0 : DDR_ARE_DEPENDENT (res) = chrec_dont_know;
3562 0 : return res;
3563 : }
3564 :
3565 : /* If the data references do not alias, then they are independent. */
3566 19926041 : if (!dr_may_alias_p (a, b, loop_nest.exists () ? loop_nest[0] : NULL))
3567 : {
3568 7192839 : DDR_ARE_DEPENDENT (res) = chrec_known;
3569 7192839 : return res;
3570 : }
3571 :
3572 6253392 : return initialize_data_dependence_relation (res, loop_nest, false);
3573 : }
3574 :
3575 :
3576 : /* Frees memory used by the conflict function F. */
3577 :
3578 : static void
3579 14832854 : free_conflict_function (conflict_function *f)
3580 : {
3581 14832854 : unsigned i;
3582 :
3583 14832854 : if (CF_NONTRIVIAL_P (f))
3584 : {
3585 5007964 : for (i = 0; i < f->n; i++)
3586 2503982 : affine_fn_free (f->fns[i]);
3587 : }
3588 14832854 : free (f);
3589 14832854 : }
3590 :
3591 : /* Frees memory used by SUBSCRIPTS. */
3592 :
3593 : static void
3594 3150056 : free_subscripts (vec<subscript_p> subscripts)
3595 : {
3596 13436131 : for (subscript_p s : subscripts)
3597 : {
3598 3985963 : free_conflict_function (s->conflicting_iterations_in_a);
3599 3985963 : free_conflict_function (s->conflicting_iterations_in_b);
3600 3985963 : free (s);
3601 : }
3602 3150056 : subscripts.release ();
3603 3150056 : }
3604 :
3605 : /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
3606 : description. */
3607 :
3608 : static inline void
3609 2241371 : finalize_ddr_dependent (struct data_dependence_relation *ddr,
3610 : tree chrec)
3611 : {
3612 2241371 : DDR_ARE_DEPENDENT (ddr) = chrec;
3613 2241371 : free_subscripts (DDR_SUBSCRIPTS (ddr));
3614 2241371 : DDR_SUBSCRIPTS (ddr).create (0);
3615 : }
3616 :
3617 : /* The dependence relation DDR cannot be represented by a distance
3618 : vector. */
3619 :
3620 : static inline void
3621 2184 : non_affine_dependence_relation (struct data_dependence_relation *ddr)
3622 : {
3623 2184 : if (dump_file && (dump_flags & TDF_DETAILS))
3624 92 : fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
3625 :
3626 2184 : DDR_AFFINE_P (ddr) = false;
3627 2184 : }
3628 :
3629 :
3630 :
3631 : /* This section contains the classic Banerjee tests. */
3632 :
3633 : /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
3634 : variables, i.e., if the ZIV (Zero Index Variable) test is true. */
3635 :
3636 : static inline bool
3637 2236624 : ziv_subscript_p (const_tree chrec_a, const_tree chrec_b)
3638 : {
3639 2236624 : return (evolution_function_is_constant_p (chrec_a)
3640 2739729 : && evolution_function_is_constant_p (chrec_b));
3641 : }
3642 :
3643 : /* Returns true iff CHREC_A and CHREC_B are dependent on an index
3644 : variable, i.e., if the SIV (Single Index Variable) test is true. */
3645 :
3646 : static bool
3647 1735306 : siv_subscript_p (const_tree chrec_a, const_tree chrec_b)
3648 : {
3649 3468828 : if ((evolution_function_is_constant_p (chrec_a)
3650 1787 : && evolution_function_is_univariate_p (chrec_b))
3651 3468828 : || (evolution_function_is_constant_p (chrec_b)
3652 1268 : && evolution_function_is_univariate_p (chrec_a)))
3653 : return true;
3654 :
3655 1732257 : if (evolution_function_is_univariate_p (chrec_a)
3656 1732257 : && evolution_function_is_univariate_p (chrec_b))
3657 : {
3658 1705890 : switch (TREE_CODE (chrec_a))
3659 : {
3660 1705890 : case POLYNOMIAL_CHREC:
3661 1705890 : switch (TREE_CODE (chrec_b))
3662 : {
3663 1705890 : case POLYNOMIAL_CHREC:
3664 1705890 : if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
3665 : return false;
3666 : /* FALLTHRU */
3667 :
3668 1705808 : default:
3669 1705808 : return true;
3670 : }
3671 :
3672 : default:
3673 : return true;
3674 : }
3675 : }
3676 :
3677 : return false;
3678 : }
3679 :
3680 : /* Creates a conflict function with N dimensions. The affine functions
3681 : in each dimension follow. */
3682 :
3683 : static conflict_function *
3684 2503982 : conflict_fn (unsigned n, ...)
3685 : {
3686 2503982 : unsigned i;
3687 2503982 : conflict_function *ret = XCNEW (conflict_function);
3688 2503982 : va_list ap;
3689 :
3690 2503982 : gcc_assert (n > 0 && n <= MAX_DIM);
3691 2503982 : va_start (ap, n);
3692 :
3693 2503982 : ret->n = n;
3694 5007964 : for (i = 0; i < n; i++)
3695 2503982 : ret->fns[i] = va_arg (ap, affine_fn);
3696 2503982 : va_end (ap);
3697 :
3698 2503982 : return ret;
3699 : }
3700 :
3701 : /* Returns constant affine function with value CST. */
3702 :
3703 : static affine_fn
3704 2387410 : affine_fn_cst (tree cst)
3705 : {
3706 2387410 : affine_fn fn;
3707 2387410 : fn.create (1);
3708 2387410 : fn.quick_push (cst);
3709 2387410 : return fn;
3710 : }
3711 :
3712 : /* Returns affine function with single variable, CST + COEF * x_DIM. */
3713 :
3714 : static affine_fn
3715 116572 : affine_fn_univar (tree cst, unsigned dim, tree coef)
3716 : {
3717 116572 : affine_fn fn;
3718 116572 : fn.create (dim + 1);
3719 116572 : unsigned i;
3720 :
3721 116572 : gcc_assert (dim > 0);
3722 116572 : fn.quick_push (cst);
3723 233144 : for (i = 1; i < dim; i++)
3724 0 : fn.quick_push (integer_zero_node);
3725 116572 : fn.quick_push (coef);
3726 116572 : return fn;
3727 : }
3728 :
3729 : /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
3730 : *OVERLAPS_B are initialized to the functions that describe the
3731 : relation between the elements accessed twice by CHREC_A and
3732 : CHREC_B. For k >= 0, the following property is verified:
3733 :
3734 : CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
3735 :
3736 : static void
3737 501318 : analyze_ziv_subscript (tree chrec_a,
3738 : tree chrec_b,
3739 : conflict_function **overlaps_a,
3740 : conflict_function **overlaps_b,
3741 : tree *last_conflicts)
3742 : {
3743 501318 : tree type, difference;
3744 501318 : dependence_stats.num_ziv++;
3745 :
3746 501318 : if (dump_file && (dump_flags & TDF_DETAILS))
3747 22441 : fprintf (dump_file, "(analyze_ziv_subscript \n");
3748 :
3749 501318 : type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
3750 501318 : chrec_a = chrec_convert (type, chrec_a, NULL);
3751 501318 : chrec_b = chrec_convert (type, chrec_b, NULL);
3752 501318 : difference = chrec_fold_minus (type, chrec_a, chrec_b);
3753 :
3754 501318 : switch (TREE_CODE (difference))
3755 : {
3756 501318 : case INTEGER_CST:
3757 501318 : if (integer_zerop (difference))
3758 : {
3759 : /* The difference is equal to zero: the accessed index
3760 : overlaps for each iteration in the loop. */
3761 0 : *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3762 0 : *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3763 0 : *last_conflicts = chrec_dont_know;
3764 0 : dependence_stats.num_ziv_dependent++;
3765 : }
3766 : else
3767 : {
3768 : /* The accesses do not overlap. */
3769 501318 : *overlaps_a = conflict_fn_no_dependence ();
3770 501318 : *overlaps_b = conflict_fn_no_dependence ();
3771 501318 : *last_conflicts = integer_zero_node;
3772 501318 : dependence_stats.num_ziv_independent++;
3773 : }
3774 : break;
3775 :
3776 0 : default:
3777 : /* We're not sure whether the indexes overlap. For the moment,
3778 : conservatively answer "don't know". */
3779 0 : if (dump_file && (dump_flags & TDF_DETAILS))
3780 0 : fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
3781 :
3782 0 : *overlaps_a = conflict_fn_not_known ();
3783 0 : *overlaps_b = conflict_fn_not_known ();
3784 0 : *last_conflicts = chrec_dont_know;
3785 0 : dependence_stats.num_ziv_unimplemented++;
3786 0 : break;
3787 : }
3788 :
3789 501318 : if (dump_file && (dump_flags & TDF_DETAILS))
3790 22441 : fprintf (dump_file, ")\n");
3791 501318 : }
3792 :
3793 : /* Similar to max_stmt_executions_int, but returns the bound as a tree,
3794 : and only if it fits to the int type. If this is not the case, or the
3795 : bound on the number of iterations of LOOP could not be derived, returns
3796 : chrec_dont_know. */
3797 :
3798 : static tree
3799 0 : max_stmt_executions_tree (class loop *loop)
3800 : {
3801 0 : widest_int nit;
3802 :
3803 0 : if (!max_stmt_executions (loop, &nit))
3804 0 : return chrec_dont_know;
3805 :
3806 0 : if (!wi::fits_to_tree_p (nit, unsigned_type_node))
3807 0 : return chrec_dont_know;
3808 :
3809 0 : return wide_int_to_tree (unsigned_type_node, nit);
3810 0 : }
3811 :
3812 : /* Determine whether the CHREC is always positive/negative. If the expression
3813 : cannot be statically analyzed, return false, otherwise set the answer into
3814 : VALUE. */
3815 :
3816 : static bool
3817 4638 : chrec_is_positive (tree chrec, bool *value)
3818 : {
3819 4638 : bool value0, value1, value2;
3820 4638 : tree end_value, nb_iter;
3821 :
3822 4638 : switch (TREE_CODE (chrec))
3823 : {
3824 0 : case POLYNOMIAL_CHREC:
3825 0 : if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
3826 0 : || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
3827 : return false;
3828 :
3829 : /* FIXME -- overflows. */
3830 0 : if (value0 == value1)
3831 : {
3832 0 : *value = value0;
3833 0 : return true;
3834 : }
3835 :
3836 : /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
3837 : and the proof consists in showing that the sign never
3838 : changes during the execution of the loop, from 0 to
3839 : loop->nb_iterations. */
3840 0 : if (!evolution_function_is_affine_p (chrec))
3841 : return false;
3842 :
3843 0 : nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
3844 0 : if (chrec_contains_undetermined (nb_iter))
3845 : return false;
3846 :
3847 : #if 0
3848 : /* TODO -- If the test is after the exit, we may decrease the number of
3849 : iterations by one. */
3850 : if (after_exit)
3851 : nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
3852 : #endif
3853 :
3854 0 : end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
3855 :
3856 0 : if (!chrec_is_positive (end_value, &value2))
3857 : return false;
3858 :
3859 0 : *value = value0;
3860 0 : return value0 == value1;
3861 :
3862 4638 : case INTEGER_CST:
3863 4638 : switch (tree_int_cst_sgn (chrec))
3864 : {
3865 2078 : case -1:
3866 2078 : *value = false;
3867 2078 : break;
3868 2560 : case 1:
3869 2560 : *value = true;
3870 2560 : break;
3871 : default:
3872 : return false;
3873 : }
3874 : return true;
3875 :
3876 : default:
3877 : return false;
3878 : }
3879 : }
3880 :
3881 :
3882 : /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
3883 : constant, and CHREC_B is an affine function. *OVERLAPS_A and
3884 : *OVERLAPS_B are initialized to the functions that describe the
3885 : relation between the elements accessed twice by CHREC_A and
3886 : CHREC_B. For k >= 0, the following property is verified:
3887 :
3888 : CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
3889 :
3890 : static void
3891 3049 : analyze_siv_subscript_cst_affine (tree chrec_a,
3892 : tree chrec_b,
3893 : conflict_function **overlaps_a,
3894 : conflict_function **overlaps_b,
3895 : tree *last_conflicts)
3896 : {
3897 3049 : bool value0, value1, value2;
3898 3049 : tree type, difference, tmp;
3899 :
3900 3049 : type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
3901 3049 : chrec_a = chrec_convert (type, chrec_a, NULL);
3902 3049 : chrec_b = chrec_convert (type, chrec_b, NULL);
3903 3049 : difference = chrec_fold_minus (type, initial_condition (chrec_b), chrec_a);
3904 :
3905 : /* Special case overlap in the first iteration. */
3906 3049 : if (integer_zerop (difference))
3907 : {
3908 728 : *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3909 728 : *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3910 728 : *last_conflicts = integer_one_node;
3911 728 : return;
3912 : }
3913 :
3914 2321 : if (!chrec_is_positive (initial_condition (difference), &value0))
3915 : {
3916 0 : if (dump_file && (dump_flags & TDF_DETAILS))
3917 0 : fprintf (dump_file, "siv test failed: chrec is not positive.\n");
3918 :
3919 0 : dependence_stats.num_siv_unimplemented++;
3920 0 : *overlaps_a = conflict_fn_not_known ();
3921 0 : *overlaps_b = conflict_fn_not_known ();
3922 0 : *last_conflicts = chrec_dont_know;
3923 0 : return;
3924 : }
3925 : else
3926 : {
3927 2321 : if (value0 == false)
3928 : {
3929 1864 : if (TREE_CODE (chrec_b) != POLYNOMIAL_CHREC
3930 1864 : || !chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
3931 : {
3932 4 : if (dump_file && (dump_flags & TDF_DETAILS))
3933 0 : fprintf (dump_file, "siv test failed: chrec not positive.\n");
3934 :
3935 4 : *overlaps_a = conflict_fn_not_known ();
3936 4 : *overlaps_b = conflict_fn_not_known ();
3937 4 : *last_conflicts = chrec_dont_know;
3938 4 : dependence_stats.num_siv_unimplemented++;
3939 4 : return;
3940 : }
3941 : else
3942 : {
3943 1860 : if (value1 == true)
3944 : {
3945 : /* Example:
3946 : chrec_a = 12
3947 : chrec_b = {10, +, 1}
3948 : */
3949 :
3950 1860 : if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
3951 : {
3952 1563 : HOST_WIDE_INT numiter;
3953 1563 : class loop *loop = get_chrec_loop (chrec_b);
3954 :
3955 1563 : *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3956 1563 : tmp = fold_build2 (EXACT_DIV_EXPR, type,
3957 : fold_build1 (ABS_EXPR, type, difference),
3958 : CHREC_RIGHT (chrec_b));
3959 1563 : *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
3960 1563 : *last_conflicts = integer_one_node;
3961 :
3962 :
3963 : /* Perform weak-zero siv test to see if overlap is
3964 : outside the loop bounds. */
3965 1563 : numiter = max_stmt_executions_int (loop);
3966 :
3967 1563 : if (numiter >= 0
3968 1563 : && compare_tree_int (tmp, numiter) > 0)
3969 : {
3970 0 : free_conflict_function (*overlaps_a);
3971 0 : free_conflict_function (*overlaps_b);
3972 0 : *overlaps_a = conflict_fn_no_dependence ();
3973 0 : *overlaps_b = conflict_fn_no_dependence ();
3974 0 : *last_conflicts = integer_zero_node;
3975 0 : dependence_stats.num_siv_independent++;
3976 0 : return;
3977 : }
3978 1563 : dependence_stats.num_siv_dependent++;
3979 1563 : return;
3980 : }
3981 :
3982 : /* When the step does not divide the difference, there are
3983 : no overlaps. */
3984 : else
3985 : {
3986 297 : *overlaps_a = conflict_fn_no_dependence ();
3987 297 : *overlaps_b = conflict_fn_no_dependence ();
3988 297 : *last_conflicts = integer_zero_node;
3989 297 : dependence_stats.num_siv_independent++;
3990 297 : return;
3991 : }
3992 : }
3993 :
3994 : else
3995 : {
3996 : /* Example:
3997 : chrec_a = 12
3998 : chrec_b = {10, +, -1}
3999 :
4000 : In this case, chrec_a will not overlap with chrec_b. */
4001 0 : *overlaps_a = conflict_fn_no_dependence ();
4002 0 : *overlaps_b = conflict_fn_no_dependence ();
4003 0 : *last_conflicts = integer_zero_node;
4004 0 : dependence_stats.num_siv_independent++;
4005 0 : return;
4006 : }
4007 : }
4008 : }
4009 : else
4010 : {
4011 457 : if (TREE_CODE (chrec_b) != POLYNOMIAL_CHREC
4012 457 : || !chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
4013 : {
4014 0 : if (dump_file && (dump_flags & TDF_DETAILS))
4015 0 : fprintf (dump_file, "siv test failed: chrec not positive.\n");
4016 :
4017 0 : *overlaps_a = conflict_fn_not_known ();
4018 0 : *overlaps_b = conflict_fn_not_known ();
4019 0 : *last_conflicts = chrec_dont_know;
4020 0 : dependence_stats.num_siv_unimplemented++;
4021 0 : return;
4022 : }
4023 : else
4024 : {
4025 457 : if (value2 == false)
4026 : {
4027 : /* Example:
4028 : chrec_a = 3
4029 : chrec_b = {10, +, -1}
4030 : */
4031 214 : if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
4032 : {
4033 109 : HOST_WIDE_INT numiter;
4034 109 : class loop *loop = get_chrec_loop (chrec_b);
4035 :
4036 109 : *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
4037 109 : tmp = fold_build2 (EXACT_DIV_EXPR, type, difference,
4038 : CHREC_RIGHT (chrec_b));
4039 109 : *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
4040 109 : *last_conflicts = integer_one_node;
4041 :
4042 : /* Perform weak-zero siv test to see if overlap is
4043 : outside the loop bounds. */
4044 109 : numiter = max_stmt_executions_int (loop);
4045 :
4046 109 : if (numiter >= 0
4047 109 : && compare_tree_int (tmp, numiter) > 0)
4048 : {
4049 0 : free_conflict_function (*overlaps_a);
4050 0 : free_conflict_function (*overlaps_b);
4051 0 : *overlaps_a = conflict_fn_no_dependence ();
4052 0 : *overlaps_b = conflict_fn_no_dependence ();
4053 0 : *last_conflicts = integer_zero_node;
4054 0 : dependence_stats.num_siv_independent++;
4055 0 : return;
4056 : }
4057 109 : dependence_stats.num_siv_dependent++;
4058 109 : return;
4059 : }
4060 :
4061 : /* When the step does not divide the difference, there
4062 : are no overlaps. */
4063 : else
4064 : {
4065 105 : *overlaps_a = conflict_fn_no_dependence ();
4066 105 : *overlaps_b = conflict_fn_no_dependence ();
4067 105 : *last_conflicts = integer_zero_node;
4068 105 : dependence_stats.num_siv_independent++;
4069 105 : return;
4070 : }
4071 : }
4072 : else
4073 : {
4074 : /* Example:
4075 : chrec_a = 3
4076 : chrec_b = {4, +, 1}
4077 :
4078 : In this case, chrec_a will not overlap with chrec_b. */
4079 243 : *overlaps_a = conflict_fn_no_dependence ();
4080 243 : *overlaps_b = conflict_fn_no_dependence ();
4081 243 : *last_conflicts = integer_zero_node;
4082 243 : dependence_stats.num_siv_independent++;
4083 243 : return;
4084 : }
4085 : }
4086 : }
4087 : }
4088 : }
4089 :
4090 : /* Helper recursive function for initializing the matrix A. Returns
4091 : the initial value of CHREC. */
4092 :
4093 : static tree
4094 3371906 : initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
4095 : {
4096 6743804 : gcc_assert (chrec);
4097 :
4098 6743804 : switch (TREE_CODE (chrec))
4099 : {
4100 3371906 : case POLYNOMIAL_CHREC:
4101 3371906 : HOST_WIDE_INT chrec_right;
4102 3371906 : if (!cst_and_fits_in_hwi (CHREC_RIGHT (chrec)))
4103 8 : return chrec_dont_know;
4104 3371898 : chrec_right = int_cst_value (CHREC_RIGHT (chrec));
4105 : /* We want to be able to negate without overflow. */
4106 3371898 : if (chrec_right == HOST_WIDE_INT_MIN)
4107 0 : return chrec_dont_know;
4108 3371898 : A[index][0] = mult * chrec_right;
4109 3371898 : return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
4110 :
4111 0 : case PLUS_EXPR:
4112 0 : case MULT_EXPR:
4113 0 : case MINUS_EXPR:
4114 0 : {
4115 0 : tree op0 = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
4116 0 : tree op1 = initialize_matrix_A (A, TREE_OPERAND (chrec, 1), index, mult);
4117 :
4118 0 : return chrec_fold_op (TREE_CODE (chrec), chrec_type (chrec), op0, op1);
4119 : }
4120 :
4121 0 : CASE_CONVERT:
4122 0 : {
4123 0 : tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
4124 0 : return chrec_convert (chrec_type (chrec), op, NULL);
4125 : }
4126 :
4127 0 : case BIT_NOT_EXPR:
4128 0 : {
4129 : /* Handle ~X as -1 - X. */
4130 0 : tree op = initialize_matrix_A (A, TREE_OPERAND (chrec, 0), index, mult);
4131 0 : return chrec_fold_op (MINUS_EXPR, chrec_type (chrec),
4132 0 : build_int_cst (TREE_TYPE (chrec), -1), op);
4133 : }
4134 :
4135 3371898 : case INTEGER_CST:
4136 3371898 : return cst_and_fits_in_hwi (chrec) ? chrec : chrec_dont_know;
4137 :
4138 0 : default:
4139 0 : gcc_unreachable ();
4140 : return NULL_TREE;
4141 : }
4142 : }
4143 :
4144 : #define FLOOR_DIV(x,y) ((x) / (y))
4145 :
4146 : /* Solves the special case of the Diophantine equation:
4147 : | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
4148 :
4149 : Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
4150 : number of iterations that loops X and Y run. The overlaps will be
4151 : constructed as evolutions in dimension DIM. */
4152 :
4153 : static void
4154 64 : compute_overlap_steps_for_affine_univar (HOST_WIDE_INT niter,
4155 : HOST_WIDE_INT step_a,
4156 : HOST_WIDE_INT step_b,
4157 : affine_fn *overlaps_a,
4158 : affine_fn *overlaps_b,
4159 : tree *last_conflicts, int dim)
4160 : {
4161 64 : if (((step_a > 0 && step_b > 0)
4162 8 : || (step_a < 0 && step_b < 0)))
4163 : {
4164 60 : HOST_WIDE_INT step_overlaps_a, step_overlaps_b;
4165 60 : HOST_WIDE_INT gcd_steps_a_b, last_conflict, tau2;
4166 :
4167 60 : gcd_steps_a_b = gcd (step_a, step_b);
4168 60 : step_overlaps_a = step_b / gcd_steps_a_b;
4169 60 : step_overlaps_b = step_a / gcd_steps_a_b;
4170 :
4171 60 : if (niter > 0)
4172 : {
4173 60 : tau2 = FLOOR_DIV (niter, step_overlaps_a);
4174 60 : tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
4175 60 : last_conflict = tau2;
4176 60 : *last_conflicts = build_int_cst (integer_type_node, last_conflict);
4177 : }
4178 : else
4179 0 : *last_conflicts = chrec_dont_know;
4180 :
4181 60 : *overlaps_a = affine_fn_univar (integer_zero_node, dim,
4182 : build_int_cst (integer_type_node,
4183 60 : step_overlaps_a));
4184 60 : *overlaps_b = affine_fn_univar (integer_zero_node, dim,
4185 : build_int_cst (integer_type_node,
4186 60 : step_overlaps_b));
4187 60 : }
4188 :
4189 : else
4190 : {
4191 4 : *overlaps_a = affine_fn_cst (integer_zero_node);
4192 4 : *overlaps_b = affine_fn_cst (integer_zero_node);
4193 4 : *last_conflicts = integer_zero_node;
4194 : }
4195 64 : }
4196 :
4197 : /* Solves the special case of a Diophantine equation where CHREC_A is
4198 : an affine bivariate function, and CHREC_B is an affine univariate
4199 : function. For example,
4200 :
4201 : | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
4202 :
4203 : has the following overlapping functions:
4204 :
4205 : | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
4206 : | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
4207 : | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
4208 :
4209 : FORNOW: This is a specialized implementation for a case occurring in
4210 : a common benchmark. Implement the general algorithm. */
4211 :
4212 : static void
4213 0 : compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
4214 : conflict_function **overlaps_a,
4215 : conflict_function **overlaps_b,
4216 : tree *last_conflicts)
4217 : {
4218 0 : bool xz_p, yz_p, xyz_p;
4219 0 : HOST_WIDE_INT step_x, step_y, step_z;
4220 0 : HOST_WIDE_INT niter_x, niter_y, niter_z, niter;
4221 0 : affine_fn overlaps_a_xz, overlaps_b_xz;
4222 0 : affine_fn overlaps_a_yz, overlaps_b_yz;
4223 0 : affine_fn overlaps_a_xyz, overlaps_b_xyz;
4224 0 : affine_fn ova1, ova2, ovb;
4225 0 : tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
4226 :
4227 0 : step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
4228 0 : step_y = int_cst_value (CHREC_RIGHT (chrec_a));
4229 0 : step_z = int_cst_value (CHREC_RIGHT (chrec_b));
4230 :
4231 0 : niter_x = max_stmt_executions_int (get_chrec_loop (CHREC_LEFT (chrec_a)));
4232 0 : niter_y = max_stmt_executions_int (get_chrec_loop (chrec_a));
4233 0 : niter_z = max_stmt_executions_int (get_chrec_loop (chrec_b));
4234 :
4235 0 : if (niter_x < 0 || niter_y < 0 || niter_z < 0)
4236 : {
4237 0 : if (dump_file && (dump_flags & TDF_DETAILS))
4238 0 : fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
4239 :
4240 0 : *overlaps_a = conflict_fn_not_known ();
4241 0 : *overlaps_b = conflict_fn_not_known ();
4242 0 : *last_conflicts = chrec_dont_know;
4243 0 : return;
4244 : }
4245 :
4246 0 : niter = MIN (niter_x, niter_z);
4247 0 : compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
4248 : &overlaps_a_xz,
4249 : &overlaps_b_xz,
4250 : &last_conflicts_xz, 1);
4251 0 : niter = MIN (niter_y, niter_z);
4252 0 : compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
4253 : &overlaps_a_yz,
4254 : &overlaps_b_yz,
4255 : &last_conflicts_yz, 2);
4256 0 : niter = MIN (niter_x, niter_z);
4257 0 : niter = MIN (niter_y, niter);
4258 0 : compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
4259 : &overlaps_a_xyz,
4260 : &overlaps_b_xyz,
4261 : &last_conflicts_xyz, 3);
4262 :
4263 0 : xz_p = !integer_zerop (last_conflicts_xz);
4264 0 : yz_p = !integer_zerop (last_conflicts_yz);
4265 0 : xyz_p = !integer_zerop (last_conflicts_xyz);
4266 :
4267 0 : if (xz_p || yz_p || xyz_p)
4268 : {
4269 0 : ova1 = affine_fn_cst (integer_zero_node);
4270 0 : ova2 = affine_fn_cst (integer_zero_node);
4271 0 : ovb = affine_fn_cst (integer_zero_node);
4272 0 : if (xz_p)
4273 : {
4274 0 : affine_fn t0 = ova1;
4275 0 : affine_fn t2 = ovb;
4276 :
4277 0 : ova1 = affine_fn_plus (ova1, overlaps_a_xz);
4278 0 : ovb = affine_fn_plus (ovb, overlaps_b_xz);
4279 0 : affine_fn_free (t0);
4280 0 : affine_fn_free (t2);
4281 0 : *last_conflicts = last_conflicts_xz;
4282 : }
4283 0 : if (yz_p)
4284 : {
4285 0 : affine_fn t0 = ova2;
4286 0 : affine_fn t2 = ovb;
4287 :
4288 0 : ova2 = affine_fn_plus (ova2, overlaps_a_yz);
4289 0 : ovb = affine_fn_plus (ovb, overlaps_b_yz);
4290 0 : affine_fn_free (t0);
4291 0 : affine_fn_free (t2);
4292 0 : *last_conflicts = last_conflicts_yz;
4293 : }
4294 0 : if (xyz_p)
4295 : {
4296 0 : affine_fn t0 = ova1;
4297 0 : affine_fn t2 = ova2;
4298 0 : affine_fn t4 = ovb;
4299 :
4300 0 : ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
4301 0 : ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
4302 0 : ovb = affine_fn_plus (ovb, overlaps_b_xyz);
4303 0 : affine_fn_free (t0);
4304 0 : affine_fn_free (t2);
4305 0 : affine_fn_free (t4);
4306 0 : *last_conflicts = last_conflicts_xyz;
4307 : }
4308 0 : *overlaps_a = conflict_fn (2, ova1, ova2);
4309 0 : *overlaps_b = conflict_fn (1, ovb);
4310 0 : }
4311 : else
4312 : {
4313 0 : *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
4314 0 : *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
4315 0 : *last_conflicts = integer_zero_node;
4316 : }
4317 :
4318 0 : affine_fn_free (overlaps_a_xz);
4319 0 : affine_fn_free (overlaps_b_xz);
4320 0 : affine_fn_free (overlaps_a_yz);
4321 0 : affine_fn_free (overlaps_b_yz);
4322 0 : affine_fn_free (overlaps_a_xyz);
4323 0 : affine_fn_free (overlaps_b_xyz);
4324 : }
4325 :
4326 : /* Copy the elements of vector VEC1 with length SIZE to VEC2. */
4327 :
4328 : static void
4329 3417162 : lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
4330 : int size)
4331 : {
4332 3417162 : memcpy (vec2, vec1, size * sizeof (*vec1));
4333 0 : }
4334 :
4335 : /* Copy the elements of M x N matrix MAT1 to MAT2. */
4336 :
4337 : static void
4338 1685877 : lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
4339 : int m, int n)
4340 : {
4341 1685877 : int i;
4342 :
4343 5057631 : for (i = 0; i < m; i++)
4344 3371754 : lambda_vector_copy (mat1[i], mat2[i], n);
4345 1685877 : }
4346 :
4347 : /* Store the N x N identity matrix in MAT. */
4348 :
4349 : static void
4350 1685877 : lambda_matrix_id (lambda_matrix mat, int size)
4351 : {
4352 1685877 : int i, j;
4353 :
4354 5057631 : for (i = 0; i < size; i++)
4355 10115262 : for (j = 0; j < size; j++)
4356 10115262 : mat[i][j] = (i == j) ? 1 : 0;
4357 1685877 : }
4358 :
4359 : /* Return the index of the first nonzero element of vector VEC1 between
4360 : START and N. We must have START <= N.
4361 : Returns N if VEC1 is the zero vector. */
4362 :
4363 : static int
4364 1685877 : lambda_vector_first_nz (lambda_vector vec1, int n, int start)
4365 : {
4366 1685877 : int j = start;
4367 1685877 : while (j < n && vec1[j] == 0)
4368 0 : j++;
4369 1685877 : return j;
4370 : }
4371 :
4372 : /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
4373 : R2 = R2 + CONST1 * R1. */
4374 :
4375 : static bool
4376 3372028 : lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2,
4377 : lambda_int const1)
4378 : {
4379 3372028 : int i;
4380 :
4381 3372028 : if (const1 == 0)
4382 : return true;
4383 :
4384 8429475 : for (i = 0; i < n; i++)
4385 : {
4386 5057685 : bool ovf;
4387 5057685 : lambda_int tem = mul_hwi (mat[r1][i], const1, &ovf);
4388 5057685 : if (ovf)
4389 3372028 : return false;
4390 5057685 : lambda_int tem2 = add_hwi (mat[r2][i], tem, &ovf);
4391 5057685 : if (ovf || tem2 == HOST_WIDE_INT_MIN)
4392 : return false;
4393 5057685 : mat[r2][i] = tem2;
4394 : }
4395 :
4396 : return true;
4397 : }
4398 :
4399 : /* Multiply vector VEC1 of length SIZE by a constant CONST1,
4400 : and store the result in VEC2. */
4401 :
4402 : static void
4403 1676017 : lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
4404 : int size, lambda_int const1)
4405 : {
4406 1676017 : int i;
4407 :
4408 1676017 : if (const1 == 0)
4409 0 : lambda_vector_clear (vec2, size);
4410 : else
4411 5028051 : for (i = 0; i < size; i++)
4412 3352034 : vec2[i] = const1 * vec1[i];
4413 1676017 : }
4414 :
4415 : /* Negate vector VEC1 with length SIZE and store it in VEC2. */
4416 :
4417 : static void
4418 1676017 : lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
4419 : int size)
4420 : {
4421 0 : lambda_vector_mult_const (vec1, vec2, size, -1);
4422 0 : }
4423 :
4424 : /* Negate row R1 of matrix MAT which has N columns. */
4425 :
4426 : static void
4427 1676017 : lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
4428 : {
4429 0 : lambda_vector_negate (mat[r1], mat[r1], n);
4430 0 : }
4431 :
4432 : /* Return true if two vectors are equal. */
4433 :
4434 : static bool
4435 362972 : lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
4436 : {
4437 362972 : int i;
4438 364019 : for (i = 0; i < size; i++)
4439 363771 : if (vec1[i] != vec2[i])
4440 : return false;
4441 : return true;
4442 : }
4443 :
4444 : /* Given an M x N integer matrix A, this function determines an M x
4445 : M unimodular matrix U, and an M x N echelon matrix S such that
4446 : "U.A = S". This decomposition is also known as "right Hermite".
4447 :
4448 : Ref: Algorithm 2.1 page 33 in "Loop Transformations for
4449 : Restructuring Compilers" Utpal Banerjee. */
4450 :
4451 : static bool
4452 1685877 : lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
4453 : lambda_matrix S, lambda_matrix U)
4454 : {
4455 1685877 : int i, j, i0 = 0;
4456 :
4457 1685877 : lambda_matrix_copy (A, S, m, n);
4458 1685877 : lambda_matrix_id (U, m);
4459 :
4460 5057631 : for (j = 0; j < n; j++)
4461 : {
4462 3371754 : if (lambda_vector_first_nz (S[j], m, i0) < m)
4463 : {
4464 1685877 : ++i0;
4465 3371754 : for (i = m - 1; i >= i0; i--)
4466 : {
4467 3371891 : while (S[i][j] != 0)
4468 : {
4469 1686014 : lambda_int factor, a, b;
4470 :
4471 1686014 : a = S[i-1][j];
4472 1686014 : b = S[i][j];
4473 1686014 : gcc_assert (a != HOST_WIDE_INT_MIN);
4474 1686014 : factor = a / b;
4475 :
4476 1686014 : if (!lambda_matrix_row_add (S, n, i, i-1, -factor))
4477 : return false;
4478 1686014 : std::swap (S[i], S[i-1]);
4479 :
4480 1686014 : if (!lambda_matrix_row_add (U, m, i, i-1, -factor))
4481 : return false;
4482 1686014 : std::swap (U[i], U[i-1]);
4483 : }
4484 : }
4485 : }
4486 : }
4487 :
4488 : return true;
4489 : }
4490 :
4491 : /* Determines the overlapping elements due to accesses CHREC_A and
4492 : CHREC_B, that are affine functions. This function cannot handle
4493 : symbolic evolution functions, ie. when initial conditions are
4494 : parameters, because it uses lambda matrices of integers. */
4495 :
4496 : static void
4497 1685953 : analyze_subscript_affine_affine (tree chrec_a,
4498 : tree chrec_b,
4499 : conflict_function **overlaps_a,
4500 : conflict_function **overlaps_b,
4501 : tree *last_conflicts)
4502 : {
4503 1685953 : unsigned nb_vars_a, nb_vars_b, dim;
4504 1685953 : lambda_int gamma, gcd_alpha_beta;
4505 1685953 : lambda_matrix A, U, S;
4506 1685953 : struct obstack scratch_obstack;
4507 :
4508 1685953 : if (eq_evolutions_p (chrec_a, chrec_b))
4509 : {
4510 : /* The accessed index overlaps for each iteration in the
4511 : loop. */
4512 0 : *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
4513 0 : *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
4514 0 : *last_conflicts = chrec_dont_know;
4515 0 : return;
4516 : }
4517 1685953 : if (dump_file && (dump_flags & TDF_DETAILS))
4518 21028 : fprintf (dump_file, "(analyze_subscript_affine_affine \n");
4519 :
4520 : /* For determining the initial intersection, we have to solve a
4521 : Diophantine equation. This is the most time consuming part.
4522 :
4523 : For answering to the question: "Is there a dependence?" we have
4524 : to prove that there exists a solution to the Diophantine
4525 : equation, and that the solution is in the iteration domain,
4526 : i.e. the solution is positive or zero, and that the solution
4527 : happens before the upper bound loop.nb_iterations. Otherwise
4528 : there is no dependence. This function outputs a description of
4529 : the iterations that hold the intersections. */
4530 :
4531 1685953 : nb_vars_a = nb_vars_in_chrec (chrec_a);
4532 1685953 : nb_vars_b = nb_vars_in_chrec (chrec_b);
4533 :
4534 1685953 : gcc_obstack_init (&scratch_obstack);
4535 :
4536 1685953 : dim = nb_vars_a + nb_vars_b;
4537 1685953 : U = lambda_matrix_new (dim, dim, &scratch_obstack);
4538 1685953 : A = lambda_matrix_new (dim, 1, &scratch_obstack);
4539 1685953 : S = lambda_matrix_new (dim, 1, &scratch_obstack);
4540 :
4541 1685953 : tree init_a = initialize_matrix_A (A, chrec_a, 0, 1);
4542 1685953 : tree init_b = initialize_matrix_A (A, chrec_b, nb_vars_a, -1);
4543 1685953 : if (init_a == chrec_dont_know
4544 1685941 : || init_b == chrec_dont_know)
4545 : {
4546 12 : if (dump_file && (dump_flags & TDF_DETAILS))
4547 0 : fprintf (dump_file, "affine-affine test failed: "
4548 : "representation issue.\n");
4549 12 : *overlaps_a = conflict_fn_not_known ();
4550 12 : *overlaps_b = conflict_fn_not_known ();
4551 12 : *last_conflicts = chrec_dont_know;
4552 12 : goto end_analyze_subs_aa;
4553 : }
4554 1685941 : gamma = int_cst_value (init_b) - int_cst_value (init_a);
4555 :
4556 : /* Don't do all the hard work of solving the Diophantine equation
4557 : when we already know the solution: for example,
4558 : | {3, +, 1}_1
4559 : | {3, +, 4}_2
4560 : | gamma = 3 - 3 = 0.
4561 : Then the first overlap occurs during the first iterations:
4562 : | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
4563 : */
4564 1685941 : if (gamma == 0)
4565 : {
4566 64 : if (nb_vars_a == 1 && nb_vars_b == 1)
4567 : {
4568 64 : HOST_WIDE_INT step_a, step_b;
4569 64 : HOST_WIDE_INT niter, niter_a, niter_b;
4570 64 : affine_fn ova, ovb;
4571 :
4572 64 : niter_a = max_stmt_executions_int (get_chrec_loop (chrec_a));
4573 64 : niter_b = max_stmt_executions_int (get_chrec_loop (chrec_b));
4574 64 : niter = MIN (niter_a, niter_b);
4575 64 : step_a = int_cst_value (CHREC_RIGHT (chrec_a));
4576 64 : step_b = int_cst_value (CHREC_RIGHT (chrec_b));
4577 :
4578 64 : compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
4579 : &ova, &ovb,
4580 : last_conflicts, 1);
4581 64 : *overlaps_a = conflict_fn (1, ova);
4582 64 : *overlaps_b = conflict_fn (1, ovb);
4583 : }
4584 :
4585 0 : else if (nb_vars_a == 2 && nb_vars_b == 1)
4586 0 : compute_overlap_steps_for_affine_1_2
4587 0 : (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
4588 :
4589 0 : else if (nb_vars_a == 1 && nb_vars_b == 2)
4590 0 : compute_overlap_steps_for_affine_1_2
4591 0 : (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
4592 :
4593 : else
4594 : {
4595 0 : if (dump_file && (dump_flags & TDF_DETAILS))
4596 0 : fprintf (dump_file, "affine-affine test failed: too many variables.\n");
4597 0 : *overlaps_a = conflict_fn_not_known ();
4598 0 : *overlaps_b = conflict_fn_not_known ();
4599 0 : *last_conflicts = chrec_dont_know;
4600 : }
4601 64 : goto end_analyze_subs_aa;
4602 : }
4603 :
4604 : /* U.A = S */
4605 1685877 : if (!lambda_matrix_right_hermite (A, dim, 1, S, U))
4606 : {
4607 0 : *overlaps_a = conflict_fn_not_known ();
4608 0 : *overlaps_b = conflict_fn_not_known ();
4609 0 : *last_conflicts = chrec_dont_know;
4610 0 : goto end_analyze_subs_aa;
4611 : }
4612 :
4613 1685877 : if (S[0][0] < 0)
4614 : {
4615 1676017 : S[0][0] *= -1;
4616 1676017 : lambda_matrix_row_negate (U, dim, 0);
4617 : }
4618 1685877 : gcd_alpha_beta = S[0][0];
4619 :
4620 : /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
4621 : but that is a quite strange case. Instead of ICEing, answer
4622 : don't know. */
4623 1685877 : if (gcd_alpha_beta == 0)
4624 : {
4625 0 : *overlaps_a = conflict_fn_not_known ();
4626 0 : *overlaps_b = conflict_fn_not_known ();
4627 0 : *last_conflicts = chrec_dont_know;
4628 0 : goto end_analyze_subs_aa;
4629 : }
4630 :
4631 : /* The classic "gcd-test". */
4632 1685877 : if (!int_divides_p (gcd_alpha_beta, gamma))
4633 : {
4634 : /* The "gcd-test" has determined that there is no integer
4635 : solution, i.e. there is no dependence. */
4636 1571831 : *overlaps_a = conflict_fn_no_dependence ();
4637 1571831 : *overlaps_b = conflict_fn_no_dependence ();
4638 1571831 : *last_conflicts = integer_zero_node;
4639 : }
4640 :
4641 : /* Both access functions are univariate. This includes SIV and MIV cases. */
4642 114046 : else if (nb_vars_a == 1 && nb_vars_b == 1)
4643 : {
4644 : /* Both functions should have the same evolution sign. */
4645 114046 : if (((A[0][0] > 0 && -A[1][0] > 0)
4646 5833 : || (A[0][0] < 0 && -A[1][0] < 0)))
4647 : {
4648 : /* The solutions are given by:
4649 : |
4650 : | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
4651 : | [u21 u22] [y0]
4652 :
4653 : For a given integer t. Using the following variables,
4654 :
4655 : | i0 = u11 * gamma / gcd_alpha_beta
4656 : | j0 = u12 * gamma / gcd_alpha_beta
4657 : | i1 = u21
4658 : | j1 = u22
4659 :
4660 : the solutions are:
4661 :
4662 : | x0 = i0 + i1 * t,
4663 : | y0 = j0 + j1 * t. */
4664 113652 : HOST_WIDE_INT i0, j0, i1, j1;
4665 :
4666 113652 : i0 = U[0][0] * gamma / gcd_alpha_beta;
4667 113652 : j0 = U[0][1] * gamma / gcd_alpha_beta;
4668 113652 : i1 = U[1][0];
4669 113652 : j1 = U[1][1];
4670 :
4671 113652 : if ((i1 == 0 && i0 < 0)
4672 113652 : || (j1 == 0 && j0 < 0))
4673 : {
4674 : /* There is no solution.
4675 : FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
4676 : falls in here, but for the moment we don't look at the
4677 : upper bound of the iteration domain. */
4678 0 : *overlaps_a = conflict_fn_no_dependence ();
4679 0 : *overlaps_b = conflict_fn_no_dependence ();
4680 0 : *last_conflicts = integer_zero_node;
4681 55426 : goto end_analyze_subs_aa;
4682 : }
4683 :
4684 113652 : if (i1 > 0 && j1 > 0)
4685 : {
4686 113652 : HOST_WIDE_INT niter_a
4687 113652 : = max_stmt_executions_int (get_chrec_loop (chrec_a));
4688 113652 : HOST_WIDE_INT niter_b
4689 113652 : = max_stmt_executions_int (get_chrec_loop (chrec_b));
4690 113652 : HOST_WIDE_INT niter = MIN (niter_a, niter_b);
4691 :
4692 : /* (X0, Y0) is a solution of the Diophantine equation:
4693 : "chrec_a (X0) = chrec_b (Y0)". */
4694 113652 : HOST_WIDE_INT tau1 = MAX (CEIL (-i0, i1),
4695 : CEIL (-j0, j1));
4696 113652 : HOST_WIDE_INT x0 = i1 * tau1 + i0;
4697 113652 : HOST_WIDE_INT y0 = j1 * tau1 + j0;
4698 :
4699 : /* (X1, Y1) is the smallest positive solution of the eq
4700 : "chrec_a (X1) = chrec_b (Y1)", i.e. this is where the
4701 : first conflict occurs. */
4702 113652 : HOST_WIDE_INT min_multiple = MIN (x0 / i1, y0 / j1);
4703 113652 : HOST_WIDE_INT x1 = x0 - i1 * min_multiple;
4704 113652 : HOST_WIDE_INT y1 = y0 - j1 * min_multiple;
4705 :
4706 113652 : if (niter > 0)
4707 : {
4708 : /* If the overlap occurs outside of the bounds of the
4709 : loop, there is no dependence. */
4710 107083 : if (x1 >= niter_a || y1 >= niter_b)
4711 : {
4712 55426 : *overlaps_a = conflict_fn_no_dependence ();
4713 55426 : *overlaps_b = conflict_fn_no_dependence ();
4714 55426 : *last_conflicts = integer_zero_node;
4715 55426 : goto end_analyze_subs_aa;
4716 : }
4717 :
4718 : /* max stmt executions can get quite large, avoid
4719 : overflows by using wide ints here. */
4720 51657 : widest_int tau2
4721 103314 : = wi::smin (wi::sdiv_floor (wi::sub (niter_a, i0), i1),
4722 154971 : wi::sdiv_floor (wi::sub (niter_b, j0), j1));
4723 51657 : widest_int last_conflict = wi::sub (tau2, (x1 - i0)/i1);
4724 51657 : if (wi::min_precision (last_conflict, SIGNED)
4725 51657 : <= TYPE_PRECISION (integer_type_node))
4726 46727 : *last_conflicts
4727 46727 : = build_int_cst (integer_type_node,
4728 46727 : last_conflict.to_shwi ());
4729 : else
4730 4930 : *last_conflicts = chrec_dont_know;
4731 51657 : }
4732 : else
4733 6569 : *last_conflicts = chrec_dont_know;
4734 :
4735 58226 : *overlaps_a
4736 58226 : = conflict_fn (1,
4737 58226 : affine_fn_univar (build_int_cst (integer_type_node, x1),
4738 : 1,
4739 58226 : build_int_cst (integer_type_node, i1)));
4740 58226 : *overlaps_b
4741 58226 : = conflict_fn (1,
4742 58226 : affine_fn_univar (build_int_cst (integer_type_node, y1),
4743 : 1,
4744 58226 : build_int_cst (integer_type_node, j1)));
4745 58226 : }
4746 : else
4747 : {
4748 : /* FIXME: For the moment, the upper bound of the
4749 : iteration domain for i and j is not checked. */
4750 0 : if (dump_file && (dump_flags & TDF_DETAILS))
4751 0 : fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
4752 0 : *overlaps_a = conflict_fn_not_known ();
4753 0 : *overlaps_b = conflict_fn_not_known ();
4754 0 : *last_conflicts = chrec_dont_know;
4755 : }
4756 58226 : }
4757 : else
4758 : {
4759 394 : if (dump_file && (dump_flags & TDF_DETAILS))
4760 19 : fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
4761 394 : *overlaps_a = conflict_fn_not_known ();
4762 394 : *overlaps_b = conflict_fn_not_known ();
4763 394 : *last_conflicts = chrec_dont_know;
4764 : }
4765 : }
4766 : else
4767 : {
4768 0 : if (dump_file && (dump_flags & TDF_DETAILS))
4769 0 : fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
4770 0 : *overlaps_a = conflict_fn_not_known ();
4771 0 : *overlaps_b = conflict_fn_not_known ();
4772 0 : *last_conflicts = chrec_dont_know;
4773 : }
4774 :
4775 1685953 : end_analyze_subs_aa:
4776 1685953 : obstack_free (&scratch_obstack, NULL);
4777 1685953 : if (dump_file && (dump_flags & TDF_DETAILS))
4778 : {
4779 21028 : fprintf (dump_file, " (overlaps_a = ");
4780 21028 : dump_conflict_function (dump_file, *overlaps_a);
4781 21028 : fprintf (dump_file, ")\n (overlaps_b = ");
4782 21028 : dump_conflict_function (dump_file, *overlaps_b);
4783 21028 : fprintf (dump_file, "))\n");
4784 : }
4785 : }
4786 :
4787 : /* Returns true when analyze_subscript_affine_affine can be used for
4788 : determining the dependence relation between chrec_a and chrec_b,
4789 : that contain symbols. This function modifies chrec_a and chrec_b
4790 : such that the analysis result is the same, and such that they don't
4791 : contain symbols, and then can safely be passed to the analyzer.
4792 :
4793 : Example: The analysis of the following tuples of evolutions produce
4794 : the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
4795 : vs. {0, +, 1}_1
4796 :
4797 : {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
4798 : {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
4799 : */
4800 :
4801 : static bool
4802 43595 : can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
4803 : {
4804 43595 : tree diff, type, left_a, left_b, right_b;
4805 :
4806 43595 : if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
4807 43595 : || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
4808 : /* FIXME: For the moment not handled. Might be refined later. */
4809 : return false;
4810 :
4811 29016 : type = chrec_type (*chrec_a);
4812 29016 : left_a = CHREC_LEFT (*chrec_a);
4813 29016 : left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL);
4814 29016 : diff = chrec_fold_minus (type, left_a, left_b);
4815 :
4816 58032 : if (!evolution_function_is_constant_p (diff))
4817 : return false;
4818 :
4819 23660 : if (dump_file && (dump_flags & TDF_DETAILS))
4820 105 : fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
4821 :
4822 23660 : *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
4823 23660 : diff, CHREC_RIGHT (*chrec_a));
4824 23660 : right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL);
4825 23660 : *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
4826 : build_int_cst (type, 0),
4827 : right_b);
4828 23660 : return true;
4829 : }
4830 :
4831 : /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
4832 : *OVERLAPS_B are initialized to the functions that describe the
4833 : relation between the elements accessed twice by CHREC_A and
4834 : CHREC_B. For k >= 0, the following property is verified:
4835 :
4836 : CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
4837 :
4838 : static void
4839 1708857 : analyze_siv_subscript (tree chrec_a,
4840 : tree chrec_b,
4841 : conflict_function **overlaps_a,
4842 : conflict_function **overlaps_b,
4843 : tree *last_conflicts,
4844 : int loop_nest_num)
4845 : {
4846 1708857 : dependence_stats.num_siv++;
4847 :
4848 1708857 : if (dump_file && (dump_flags & TDF_DETAILS))
4849 24159 : fprintf (dump_file, "(analyze_siv_subscript \n");
4850 :
4851 1708857 : if (evolution_function_is_constant_p (chrec_a)
4852 1708857 : && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
4853 1784 : analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
4854 : overlaps_a, overlaps_b, last_conflicts);
4855 :
4856 1707073 : else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
4857 3414146 : && evolution_function_is_constant_p (chrec_b))
4858 1265 : analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
4859 : overlaps_b, overlaps_a, last_conflicts);
4860 :
4861 1705808 : else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest_num)
4862 1705808 : && evolution_function_is_affine_in_loop (chrec_b, loop_nest_num))
4863 : {
4864 1705808 : if (!chrec_contains_symbols (chrec_a)
4865 1705808 : && !chrec_contains_symbols (chrec_b))
4866 : {
4867 1662213 : analyze_subscript_affine_affine (chrec_a, chrec_b,
4868 : overlaps_a, overlaps_b,
4869 : last_conflicts);
4870 :
4871 1662213 : if (CF_NOT_KNOWN_P (*overlaps_a)
4872 1661827 : || CF_NOT_KNOWN_P (*overlaps_b))
4873 386 : dependence_stats.num_siv_unimplemented++;
4874 1661827 : else if (CF_NO_DEPENDENCE_P (*overlaps_a)
4875 57394 : || CF_NO_DEPENDENCE_P (*overlaps_b))
4876 1604433 : dependence_stats.num_siv_independent++;
4877 : else
4878 57394 : dependence_stats.num_siv_dependent++;
4879 : }
4880 43595 : else if (can_use_analyze_subscript_affine_affine (&chrec_a,
4881 : &chrec_b))
4882 : {
4883 23660 : analyze_subscript_affine_affine (chrec_a, chrec_b,
4884 : overlaps_a, overlaps_b,
4885 : last_conflicts);
4886 :
4887 23660 : if (CF_NOT_KNOWN_P (*overlaps_a)
4888 23644 : || CF_NOT_KNOWN_P (*overlaps_b))
4889 16 : dependence_stats.num_siv_unimplemented++;
4890 23644 : else if (CF_NO_DEPENDENCE_P (*overlaps_a)
4891 834 : || CF_NO_DEPENDENCE_P (*overlaps_b))
4892 22810 : dependence_stats.num_siv_independent++;
4893 : else
4894 834 : dependence_stats.num_siv_dependent++;
4895 : }
4896 : else
4897 19935 : goto siv_subscript_dontknow;
4898 : }
4899 :
4900 : else
4901 : {
4902 19935 : siv_subscript_dontknow:;
4903 19935 : if (dump_file && (dump_flags & TDF_DETAILS))
4904 2946 : fprintf (dump_file, " siv test failed: unimplemented");
4905 19935 : *overlaps_a = conflict_fn_not_known ();
4906 19935 : *overlaps_b = conflict_fn_not_known ();
4907 19935 : *last_conflicts = chrec_dont_know;
4908 19935 : dependence_stats.num_siv_unimplemented++;
4909 : }
4910 :
4911 1708857 : if (dump_file && (dump_flags & TDF_DETAILS))
4912 24159 : fprintf (dump_file, ")\n");
4913 1708857 : }
4914 :
4915 : /* Returns false if we can prove that the greatest common divisor of the steps
4916 : of CHREC does not divide CST, false otherwise. */
4917 :
4918 : static bool
4919 20662 : gcd_of_steps_may_divide_p (const_tree chrec, const_tree cst)
4920 : {
4921 20662 : HOST_WIDE_INT cd = 0, val;
4922 20662 : tree step;
4923 :
4924 20662 : if (!tree_fits_shwi_p (cst))
4925 : return true;
4926 20662 : val = tree_to_shwi (cst);
4927 :
4928 61838 : while (TREE_CODE (chrec) == POLYNOMIAL_CHREC)
4929 : {
4930 41322 : step = CHREC_RIGHT (chrec);
4931 41322 : if (!tree_fits_shwi_p (step))
4932 : return true;
4933 41176 : cd = gcd (cd, tree_to_shwi (step));
4934 41176 : chrec = CHREC_LEFT (chrec);
4935 : }
4936 :
4937 20516 : return val % cd == 0;
4938 : }
4939 :
4940 : /* Analyze a MIV (Multiple Index Variable) subscript with respect to
4941 : LOOP_NEST. *OVERLAPS_A and *OVERLAPS_B are initialized to the
4942 : functions that describe the relation between the elements accessed
4943 : twice by CHREC_A and CHREC_B. For k >= 0, the following property
4944 : is verified:
4945 :
4946 : CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
4947 :
4948 : static void
4949 26449 : analyze_miv_subscript (tree chrec_a,
4950 : tree chrec_b,
4951 : conflict_function **overlaps_a,
4952 : conflict_function **overlaps_b,
4953 : tree *last_conflicts,
4954 : class loop *loop_nest)
4955 : {
4956 26449 : tree type, difference;
4957 :
4958 26449 : dependence_stats.num_miv++;
4959 26449 : if (dump_file && (dump_flags & TDF_DETAILS))
4960 27 : fprintf (dump_file, "(analyze_miv_subscript \n");
4961 :
4962 26449 : type = signed_type_for_types (TREE_TYPE (chrec_a), TREE_TYPE (chrec_b));
4963 26449 : chrec_a = chrec_convert (type, chrec_a, NULL);
4964 26449 : chrec_b = chrec_convert (type, chrec_b, NULL);
4965 26449 : difference = chrec_fold_minus (type, chrec_a, chrec_b);
4966 :
4967 26449 : if (eq_evolutions_p (chrec_a, chrec_b))
4968 : {
4969 : /* Access functions are the same: all the elements are accessed
4970 : in the same order. */
4971 0 : *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
4972 0 : *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
4973 0 : *last_conflicts = max_stmt_executions_tree (get_chrec_loop (chrec_a));
4974 0 : dependence_stats.num_miv_dependent++;
4975 : }
4976 :
4977 26449 : else if (evolution_function_is_constant_p (difference)
4978 20692 : && evolution_function_is_affine_multivariate_p (chrec_a,
4979 : loop_nest->num)
4980 47111 : && !gcd_of_steps_may_divide_p (chrec_a, difference))
4981 : {
4982 : /* testsuite/.../ssa-chrec-33.c
4983 : {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
4984 :
4985 : The difference is 1, and all the evolution steps are multiples
4986 : of 2, consequently there are no overlapping elements. */
4987 19670 : *overlaps_a = conflict_fn_no_dependence ();
4988 19670 : *overlaps_b = conflict_fn_no_dependence ();
4989 19670 : *last_conflicts = integer_zero_node;
4990 19670 : dependence_stats.num_miv_independent++;
4991 : }
4992 :
4993 6779 : else if (evolution_function_is_affine_in_loop (chrec_a, loop_nest->num)
4994 122 : && !chrec_contains_symbols (chrec_a, loop_nest)
4995 110 : && evolution_function_is_affine_in_loop (chrec_b, loop_nest->num)
4996 6859 : && !chrec_contains_symbols (chrec_b, loop_nest))
4997 : {
4998 : /* testsuite/.../ssa-chrec-35.c
4999 : {0, +, 1}_2 vs. {0, +, 1}_3
5000 : the overlapping elements are respectively located at iterations:
5001 : {0, +, 1}_x and {0, +, 1}_x,
5002 : in other words, we have the equality:
5003 : {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
5004 :
5005 : Other examples:
5006 : {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
5007 : {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
5008 :
5009 : {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
5010 : {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
5011 : */
5012 80 : analyze_subscript_affine_affine (chrec_a, chrec_b,
5013 : overlaps_a, overlaps_b, last_conflicts);
5014 :
5015 80 : if (CF_NOT_KNOWN_P (*overlaps_a)
5016 76 : || CF_NOT_KNOWN_P (*overlaps_b))
5017 4 : dependence_stats.num_miv_unimplemented++;
5018 76 : else if (CF_NO_DEPENDENCE_P (*overlaps_a)
5019 62 : || CF_NO_DEPENDENCE_P (*overlaps_b))
5020 14 : dependence_stats.num_miv_independent++;
5021 : else
5022 62 : dependence_stats.num_miv_dependent++;
5023 : }
5024 :
5025 : else
5026 : {
5027 : /* When the analysis is too difficult, answer "don't know". */
5028 6699 : if (dump_file && (dump_flags & TDF_DETAILS))
5029 23 : fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
5030 :
5031 6699 : *overlaps_a = conflict_fn_not_known ();
5032 6699 : *overlaps_b = conflict_fn_not_known ();
5033 6699 : *last_conflicts = chrec_dont_know;
5034 6699 : dependence_stats.num_miv_unimplemented++;
5035 : }
5036 :
5037 26449 : if (dump_file && (dump_flags & TDF_DETAILS))
5038 27 : fprintf (dump_file, ")\n");
5039 26449 : }
5040 :
5041 : /* Determines the iterations for which CHREC_A is equal to CHREC_B in
5042 : with respect to LOOP_NEST. OVERLAP_ITERATIONS_A and
5043 : OVERLAP_ITERATIONS_B are initialized with two functions that
5044 : describe the iterations that contain conflicting elements.
5045 :
5046 : Remark: For an integer k >= 0, the following equality is true:
5047 :
5048 : CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
5049 : */
5050 :
5051 : static void
5052 3430464 : analyze_overlapping_iterations (tree chrec_a,
5053 : tree chrec_b,
5054 : conflict_function **overlap_iterations_a,
5055 : conflict_function **overlap_iterations_b,
5056 : tree *last_conflicts, class loop *loop_nest)
5057 : {
5058 3430464 : unsigned int lnn = loop_nest->num;
5059 :
5060 3430464 : dependence_stats.num_subscript_tests++;
5061 :
5062 3430464 : if (dump_file && (dump_flags & TDF_DETAILS))
5063 : {
5064 60315 : fprintf (dump_file, "(analyze_overlapping_iterations \n");
5065 60315 : fprintf (dump_file, " (chrec_a = ");
5066 60315 : print_generic_expr (dump_file, chrec_a);
5067 60315 : fprintf (dump_file, ")\n (chrec_b = ");
5068 60315 : print_generic_expr (dump_file, chrec_b);
5069 60315 : fprintf (dump_file, ")\n");
5070 : }
5071 :
5072 3430464 : if (chrec_a == NULL_TREE
5073 3430464 : || chrec_b == NULL_TREE
5074 3430464 : || chrec_contains_undetermined (chrec_a)
5075 6860928 : || chrec_contains_undetermined (chrec_b))
5076 : {
5077 0 : dependence_stats.num_subscript_undetermined++;
5078 :
5079 0 : *overlap_iterations_a = conflict_fn_not_known ();
5080 0 : *overlap_iterations_b = conflict_fn_not_known ();
5081 : }
5082 :
5083 : /* If they are the same chrec, and are affine, they overlap
5084 : on every iteration. */
5085 3430464 : else if (eq_evolutions_p (chrec_a, chrec_b)
5086 3430464 : && (evolution_function_is_affine_multivariate_p (chrec_a, lnn)
5087 491528 : || operand_equal_p (chrec_a, chrec_b, 0)))
5088 : {
5089 1191301 : dependence_stats.num_same_subscript_function++;
5090 1191301 : *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
5091 1191301 : *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
5092 1191301 : *last_conflicts = chrec_dont_know;
5093 : }
5094 :
5095 : /* If they aren't the same, and aren't affine, we can't do anything
5096 : yet. */
5097 2239163 : else if ((chrec_contains_symbols (chrec_a)
5098 2187812 : || chrec_contains_symbols (chrec_b))
5099 2240009 : && (!evolution_function_is_affine_multivariate_p (chrec_a, lnn)
5100 49956 : || !evolution_function_is_affine_multivariate_p (chrec_b, lnn)))
5101 : {
5102 2539 : dependence_stats.num_subscript_undetermined++;
5103 2539 : *overlap_iterations_a = conflict_fn_not_known ();
5104 2539 : *overlap_iterations_b = conflict_fn_not_known ();
5105 : }
5106 :
5107 2236624 : else if (ziv_subscript_p (chrec_a, chrec_b))
5108 501318 : analyze_ziv_subscript (chrec_a, chrec_b,
5109 : overlap_iterations_a, overlap_iterations_b,
5110 : last_conflicts);
5111 :
5112 1735306 : else if (siv_subscript_p (chrec_a, chrec_b))
5113 1708857 : analyze_siv_subscript (chrec_a, chrec_b,
5114 : overlap_iterations_a, overlap_iterations_b,
5115 : last_conflicts, lnn);
5116 :
5117 : else
5118 26449 : analyze_miv_subscript (chrec_a, chrec_b,
5119 : overlap_iterations_a, overlap_iterations_b,
5120 : last_conflicts, loop_nest);
5121 :
5122 3430464 : if (dump_file && (dump_flags & TDF_DETAILS))
5123 : {
5124 60315 : fprintf (dump_file, " (overlap_iterations_a = ");
5125 60315 : dump_conflict_function (dump_file, *overlap_iterations_a);
5126 60315 : fprintf (dump_file, ")\n (overlap_iterations_b = ");
5127 60315 : dump_conflict_function (dump_file, *overlap_iterations_b);
5128 60315 : fprintf (dump_file, "))\n");
5129 : }
5130 3430464 : }
5131 :
5132 : /* Helper function for uniquely inserting distance vectors. */
5133 :
5134 : static void
5135 1087713 : save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
5136 : {
5137 1631623 : for (lambda_vector v : DDR_DIST_VECTS (ddr))
5138 545202 : if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
5139 : return;
5140 :
5141 1087465 : DDR_DIST_VECTS (ddr).safe_push (dist_v);
5142 : }
5143 :
5144 : /* Helper function for uniquely inserting direction vectors. */
5145 :
5146 : static void
5147 1087465 : save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
5148 : {
5149 1630631 : for (lambda_vector v : DDR_DIR_VECTS (ddr))
5150 543714 : if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
5151 : return;
5152 :
5153 1087465 : DDR_DIR_VECTS (ddr).safe_push (dir_v);
5154 : }
5155 :
5156 : /* Add a distance of 1 on all the loops outer than INDEX. If we
5157 : haven't yet determined a distance for this outer loop, push a new
5158 : distance vector composed of the previous distance, and a distance
5159 : of 1 for this outer loop. Example:
5160 :
5161 : | loop_1
5162 : | loop_2
5163 : | A[10]
5164 : | endloop_2
5165 : | endloop_1
5166 :
5167 : Saved vectors are of the form (dist_in_1, dist_in_2). First, we
5168 : save (0, 1), then we have to save (1, 0). */
5169 :
5170 : static void
5171 16682 : add_outer_distances (struct data_dependence_relation *ddr,
5172 : lambda_vector dist_v, int index)
5173 : {
5174 : /* For each outer loop where init_v is not set, the accesses are
5175 : in dependence of distance 1 in the loop. */
5176 19859 : while (--index >= 0)
5177 : {
5178 6354 : lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5179 3177 : lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
5180 3177 : save_v[index] = 1;
5181 3177 : save_dist_v (ddr, save_v);
5182 : }
5183 16682 : }
5184 :
5185 : /* Return false when fail to represent the data dependence as a
5186 : distance vector. A_INDEX is the index of the first reference
5187 : (0 for DDR_A, 1 for DDR_B) and B_INDEX is the index of the
5188 : second reference. INIT_B is set to true when a component has been
5189 : added to the distance vector DIST_V. INDEX_CARRY is then set to
5190 : the index in DIST_V that carries the dependence. */
5191 :
5192 : static bool
5193 59826 : build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
5194 : unsigned int a_index, unsigned int b_index,
5195 : lambda_vector dist_v, bool *init_b,
5196 : int *index_carry)
5197 : {
5198 59826 : unsigned i;
5199 119652 : lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5200 59826 : class loop *loop = DDR_LOOP_NEST (ddr)[0];
5201 :
5202 134760 : for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
5203 : {
5204 77118 : tree access_fn_a, access_fn_b;
5205 77118 : struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
5206 :
5207 77118 : if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
5208 : {
5209 309 : non_affine_dependence_relation (ddr);
5210 309 : return false;
5211 : }
5212 :
5213 76809 : access_fn_a = SUB_ACCESS_FN (subscript, a_index);
5214 76809 : access_fn_b = SUB_ACCESS_FN (subscript, b_index);
5215 :
5216 76809 : if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
5217 58446 : && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
5218 : {
5219 57820 : HOST_WIDE_INT dist;
5220 57820 : int index;
5221 57820 : int var_a = CHREC_VARIABLE (access_fn_a);
5222 57820 : int var_b = CHREC_VARIABLE (access_fn_b);
5223 :
5224 57820 : if (var_a != var_b
5225 57820 : || chrec_contains_undetermined (SUB_DISTANCE (subscript)))
5226 : {
5227 34 : non_affine_dependence_relation (ddr);
5228 34 : return false;
5229 : }
5230 :
5231 : /* When data references are collected in a loop while data
5232 : dependences are analyzed in loop nest nested in the loop, we
5233 : would have more number of access functions than number of
5234 : loops. Skip access functions of loops not in the loop nest.
5235 :
5236 : See PR89725 for more information. */
5237 57786 : if (flow_loop_nested_p (get_loop (cfun, var_a), loop))
5238 2 : continue;
5239 :
5240 57784 : dist = int_cst_value (SUB_DISTANCE (subscript));
5241 57784 : index = index_in_loop_nest (var_a, DDR_LOOP_NEST (ddr));
5242 57784 : *index_carry = MIN (index, *index_carry);
5243 :
5244 : /* This is the subscript coupling test. If we have already
5245 : recorded a distance for this loop (a distance coming from
5246 : another subscript), it should be the same. For example,
5247 : in the following code, there is no dependence:
5248 :
5249 : | loop i = 0, N, 1
5250 : | T[i+1][i] = ...
5251 : | ... = T[i][i]
5252 : | endloop
5253 : */
5254 57784 : if (init_v[index] != 0 && dist_v[index] != dist)
5255 : {
5256 0 : finalize_ddr_dependent (ddr, chrec_known);
5257 0 : return false;
5258 : }
5259 :
5260 57784 : dist_v[index] = dist;
5261 57784 : init_v[index] = 1;
5262 57784 : *init_b = true;
5263 57784 : }
5264 18989 : else if (!operand_equal_p (access_fn_a, access_fn_b, 0))
5265 : {
5266 : /* This can be for example an affine vs. constant dependence
5267 : (T[i] vs. T[3]) that is not an affine dependence and is
5268 : not representable as a distance vector. */
5269 1841 : non_affine_dependence_relation (ddr);
5270 1841 : return false;
5271 : }
5272 : }
5273 :
5274 : return true;
5275 : }
5276 :
5277 : /* Return true when the DDR contains only invariant access functions wrto. loop
5278 : number LNUM. */
5279 :
5280 : static bool
5281 856621 : invariant_access_functions (const struct data_dependence_relation *ddr,
5282 : int lnum)
5283 : {
5284 2893332 : for (subscript *sub : DDR_SUBSCRIPTS (ddr))
5285 1004434 : if (!evolution_function_is_invariant_p (SUB_ACCESS_FN (sub, 0), lnum)
5286 1004434 : || !evolution_function_is_invariant_p (SUB_ACCESS_FN (sub, 1), lnum))
5287 : return false;
5288 :
5289 : return true;
5290 : }
5291 :
5292 : /* Helper function for the case where DDR_A and DDR_B are the same
5293 : multivariate access function with a constant step. For an example
5294 : see pr34635-1.c. */
5295 :
5296 : static void
5297 4576 : add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
5298 : {
5299 4576 : int x_1, x_2;
5300 4576 : tree c_1 = CHREC_LEFT (c_2);
5301 4576 : tree c_0 = CHREC_LEFT (c_1);
5302 4576 : lambda_vector dist_v;
5303 4576 : HOST_WIDE_INT v1, v2, cd;
5304 :
5305 : /* Polynomials with more than 2 variables are not handled yet. When
5306 : the evolution steps are parameters, it is not possible to
5307 : represent the dependence using classical distance vectors. */
5308 4576 : if (TREE_CODE (c_0) != INTEGER_CST
5309 3048 : || TREE_CODE (CHREC_RIGHT (c_1)) != INTEGER_CST
5310 6963 : || TREE_CODE (CHREC_RIGHT (c_2)) != INTEGER_CST)
5311 : {
5312 2197 : DDR_AFFINE_P (ddr) = false;
5313 2197 : return;
5314 : }
5315 :
5316 2379 : x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
5317 2379 : x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
5318 :
5319 : /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
5320 4758 : dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5321 2379 : v1 = int_cst_value (CHREC_RIGHT (c_1));
5322 2379 : v2 = int_cst_value (CHREC_RIGHT (c_2));
5323 2379 : cd = gcd (v1, v2);
5324 2379 : v1 /= cd;
5325 2379 : v2 /= cd;
5326 :
5327 2379 : if (v2 < 0)
5328 : {
5329 2 : v2 = -v2;
5330 2 : v1 = -v1;
5331 : }
5332 :
5333 2379 : dist_v[x_1] = v2;
5334 2379 : dist_v[x_2] = -v1;
5335 2379 : save_dist_v (ddr, dist_v);
5336 :
5337 2379 : add_outer_distances (ddr, dist_v, x_1);
5338 : }
5339 :
5340 : /* Helper function for the case where DDR_A and DDR_B are the same
5341 : access functions. */
5342 :
5343 : static void
5344 19034 : add_other_self_distances (struct data_dependence_relation *ddr)
5345 : {
5346 19034 : lambda_vector dist_v;
5347 19034 : unsigned i;
5348 19034 : int index_carry = DDR_NB_LOOPS (ddr);
5349 19034 : subscript *sub;
5350 19034 : class loop *loop = DDR_LOOP_NEST (ddr)[0];
5351 :
5352 40421 : FOR_EACH_VEC_ELT (DDR_SUBSCRIPTS (ddr), i, sub)
5353 : {
5354 26480 : tree access_fun = SUB_ACCESS_FN (sub, 0);
5355 :
5356 26480 : if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
5357 : {
5358 19174 : if (!evolution_function_is_univariate_p (access_fun, loop->num))
5359 : {
5360 5093 : if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
5361 : {
5362 517 : DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
5363 517 : return;
5364 : }
5365 :
5366 4576 : access_fun = SUB_ACCESS_FN (DDR_SUBSCRIPT (ddr, 0), 0);
5367 :
5368 4576 : if (TREE_CODE (CHREC_LEFT (access_fun)) == POLYNOMIAL_CHREC)
5369 4576 : add_multivariate_self_dist (ddr, access_fun);
5370 : else
5371 : /* The evolution step is not constant: it varies in
5372 : the outer loop, so this cannot be represented by a
5373 : distance vector. For example in pr34635.c the
5374 : evolution is {0, +, {0, +, 4}_1}_2. */
5375 0 : DDR_AFFINE_P (ddr) = false;
5376 :
5377 : return;
5378 : }
5379 :
5380 : /* When data references are collected in a loop while data
5381 : dependences are analyzed in loop nest nested in the loop, we
5382 : would have more number of access functions than number of
5383 : loops. Skip access functions of loops not in the loop nest.
5384 :
5385 : See PR89725 for more information. */
5386 14081 : if (flow_loop_nested_p (get_loop (cfun, CHREC_VARIABLE (access_fun)),
5387 : loop))
5388 0 : continue;
5389 :
5390 21544 : index_carry = MIN (index_carry,
5391 : index_in_loop_nest (CHREC_VARIABLE (access_fun),
5392 : DDR_LOOP_NEST (ddr)));
5393 : }
5394 : }
5395 :
5396 27882 : dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5397 13941 : add_outer_distances (ddr, dist_v, index_carry);
5398 : }
5399 :
5400 : static void
5401 175656 : insert_innermost_unit_dist_vector (struct data_dependence_relation *ddr)
5402 : {
5403 351312 : lambda_vector dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5404 :
5405 175656 : dist_v[0] = 1;
5406 175656 : save_dist_v (ddr, dist_v);
5407 175656 : }
5408 :
5409 : /* Adds a unit distance vector to DDR when there is a 0 overlap. This
5410 : is the case for example when access functions are the same and
5411 : equal to a constant, as in:
5412 :
5413 : | loop_1
5414 : | A[3] = ...
5415 : | ... = A[3]
5416 : | endloop_1
5417 :
5418 : in which case the distance vectors are (0) and (1). */
5419 :
5420 : static void
5421 175656 : add_distance_for_zero_overlaps (struct data_dependence_relation *ddr)
5422 : {
5423 175656 : unsigned i, j;
5424 :
5425 175656 : for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
5426 : {
5427 175656 : subscript_p sub = DDR_SUBSCRIPT (ddr, i);
5428 175656 : conflict_function *ca = SUB_CONFLICTS_IN_A (sub);
5429 175656 : conflict_function *cb = SUB_CONFLICTS_IN_B (sub);
5430 :
5431 175656 : for (j = 0; j < ca->n; j++)
5432 175656 : if (affine_function_zero_p (ca->fns[j]))
5433 : {
5434 175656 : insert_innermost_unit_dist_vector (ddr);
5435 175656 : return;
5436 : }
5437 :
5438 0 : for (j = 0; j < cb->n; j++)
5439 0 : if (affine_function_zero_p (cb->fns[j]))
5440 : {
5441 0 : insert_innermost_unit_dist_vector (ddr);
5442 0 : return;
5443 : }
5444 : }
5445 : }
5446 :
5447 : /* Return true when the DDR contains two data references that have the
5448 : same access functions. */
5449 :
5450 : static inline bool
5451 908689 : same_access_functions (const struct data_dependence_relation *ddr)
5452 : {
5453 3806456 : for (subscript *sub : DDR_SUBSCRIPTS (ddr))
5454 1132457 : if (!eq_evolutions_p (SUB_ACCESS_FN (sub, 0),
5455 1132457 : SUB_ACCESS_FN (sub, 1)))
5456 : return false;
5457 :
5458 : return true;
5459 : }
5460 :
5461 : /* Compute the classic per loop distance vector. DDR is the data
5462 : dependence relation to build a vector from. Return false when fail
5463 : to represent the data dependence as a distance vector. */
5464 :
5465 : static bool
5466 3086073 : build_classic_dist_vector (struct data_dependence_relation *ddr,
5467 : class loop *loop_nest)
5468 : {
5469 3086073 : bool init_b = false;
5470 3086073 : int index_carry = DDR_NB_LOOPS (ddr);
5471 3086073 : lambda_vector dist_v;
5472 :
5473 3086073 : if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
5474 : return false;
5475 :
5476 908689 : if (same_access_functions (ddr))
5477 : {
5478 : /* Save the 0 vector. */
5479 1713242 : dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5480 856621 : save_dist_v (ddr, dist_v);
5481 :
5482 856621 : if (invariant_access_functions (ddr, loop_nest->num))
5483 175656 : add_distance_for_zero_overlaps (ddr);
5484 :
5485 856621 : if (DDR_NB_LOOPS (ddr) > 1)
5486 19034 : add_other_self_distances (ddr);
5487 :
5488 : return true;
5489 : }
5490 :
5491 104136 : dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5492 52068 : if (!build_classic_dist_vector_1 (ddr, 0, 1, dist_v, &init_b, &index_carry))
5493 : return false;
5494 :
5495 : /* Save the distance vector if we initialized one. */
5496 49884 : if (init_b)
5497 : {
5498 : /* Verify a basic constraint: classic distance vectors should
5499 : always be lexicographically positive.
5500 :
5501 : Data references are collected in the order of execution of
5502 : the program, thus for the following loop
5503 :
5504 : | for (i = 1; i < 100; i++)
5505 : | for (j = 1; j < 100; j++)
5506 : | {
5507 : | t = T[j+1][i-1]; // A
5508 : | T[j][i] = t + 2; // B
5509 : | }
5510 :
5511 : references are collected following the direction of the wind:
5512 : A then B. The data dependence tests are performed also
5513 : following this order, such that we're looking at the distance
5514 : separating the elements accessed by A from the elements later
5515 : accessed by B. But in this example, the distance returned by
5516 : test_dep (A, B) is lexicographically negative (-1, 1), that
5517 : means that the access A occurs later than B with respect to
5518 : the outer loop, ie. we're actually looking upwind. In this
5519 : case we solve test_dep (B, A) looking downwind to the
5520 : lexicographically positive solution, that returns the
5521 : distance vector (1, -1). */
5522 99768 : if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
5523 : {
5524 7653 : lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5525 7653 : if (!subscript_dependence_tester_1 (ddr, 1, 0, loop_nest))
5526 : return false;
5527 7649 : compute_subscript_distance (ddr);
5528 7649 : if (!build_classic_dist_vector_1 (ddr, 1, 0, save_v, &init_b,
5529 : &index_carry))
5530 : return false;
5531 7649 : save_dist_v (ddr, save_v);
5532 7649 : DDR_REVERSED_P (ddr) = true;
5533 :
5534 : /* In this case there is a dependence forward for all the
5535 : outer loops:
5536 :
5537 : | for (k = 1; k < 100; k++)
5538 : | for (i = 1; i < 100; i++)
5539 : | for (j = 1; j < 100; j++)
5540 : | {
5541 : | t = T[j+1][i-1]; // A
5542 : | T[j][i] = t + 2; // B
5543 : | }
5544 :
5545 : the vectors are:
5546 : (0, 1, -1)
5547 : (1, 1, -1)
5548 : (1, -1, 1)
5549 : */
5550 7649 : if (DDR_NB_LOOPS (ddr) > 1)
5551 : {
5552 72 : add_outer_distances (ddr, save_v, index_carry);
5553 72 : add_outer_distances (ddr, dist_v, index_carry);
5554 : }
5555 : }
5556 : else
5557 : {
5558 42231 : lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5559 42231 : lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
5560 :
5561 42231 : if (DDR_NB_LOOPS (ddr) > 1)
5562 : {
5563 109 : lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5564 :
5565 109 : if (!subscript_dependence_tester_1 (ddr, 1, 0, loop_nest))
5566 : return false;
5567 109 : compute_subscript_distance (ddr);
5568 109 : if (!build_classic_dist_vector_1 (ddr, 1, 0, opposite_v, &init_b,
5569 : &index_carry))
5570 : return false;
5571 :
5572 109 : save_dist_v (ddr, save_v);
5573 109 : add_outer_distances (ddr, dist_v, index_carry);
5574 109 : add_outer_distances (ddr, opposite_v, index_carry);
5575 : }
5576 : else
5577 42122 : save_dist_v (ddr, save_v);
5578 : }
5579 : }
5580 : else
5581 : {
5582 : /* There is a distance of 1 on all the outer loops: Example:
5583 : there is a dependence of distance 1 on loop_1 for the array A.
5584 :
5585 : | loop_1
5586 : | A[5] = ...
5587 : | endloop
5588 : */
5589 0 : add_outer_distances (ddr, dist_v,
5590 : lambda_vector_first_nz (dist_v,
5591 0 : DDR_NB_LOOPS (ddr), 0));
5592 : }
5593 :
5594 : return true;
5595 : }
5596 :
5597 : /* Return the direction for a given distance.
5598 : FIXME: Computing dir this way is suboptimal, since dir can catch
5599 : cases that dist is unable to represent. */
5600 :
5601 : static inline enum data_dependence_direction
5602 1112335 : dir_from_dist (int dist)
5603 : {
5604 1112335 : if (dist > 0)
5605 : return dir_positive;
5606 881458 : else if (dist < 0)
5607 : return dir_negative;
5608 : else
5609 879047 : return dir_equal;
5610 : }
5611 :
5612 : /* Compute the classic per loop direction vector. DDR is the data
5613 : dependence relation to build a vector from. */
5614 :
5615 : static void
5616 906501 : build_classic_dir_vector (struct data_dependence_relation *ddr)
5617 : {
5618 906501 : unsigned i, j;
5619 906501 : lambda_vector dist_v;
5620 :
5621 1993966 : FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
5622 : {
5623 2174930 : lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
5624 :
5625 3287265 : for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
5626 1993793 : dir_v[j] = dir_from_dist (dist_v[j]);
5627 :
5628 1087465 : save_dir_v (ddr, dir_v);
5629 : }
5630 906501 : }
5631 :
5632 : /* Helper function. Returns true when there is a dependence between the
5633 : data references. A_INDEX is the index of the first reference (0 for
5634 : DDR_A, 1 for DDR_B) and B_INDEX is the index of the second reference. */
5635 :
5636 : static bool
5637 3093835 : subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
5638 : unsigned int a_index, unsigned int b_index,
5639 : class loop *loop_nest)
5640 : {
5641 3093835 : unsigned int i;
5642 3093835 : tree last_conflicts;
5643 3093835 : struct subscript *subscript;
5644 3093835 : tree res = NULL_TREE;
5645 :
5646 4375409 : for (i = 0; DDR_SUBSCRIPTS (ddr).iterate (i, &subscript); i++)
5647 : {
5648 3430464 : conflict_function *overlaps_a, *overlaps_b;
5649 :
5650 3430464 : analyze_overlapping_iterations (SUB_ACCESS_FN (subscript, a_index),
5651 : SUB_ACCESS_FN (subscript, b_index),
5652 : &overlaps_a, &overlaps_b,
5653 : &last_conflicts, loop_nest);
5654 :
5655 3430464 : if (SUB_CONFLICTS_IN_A (subscript))
5656 3430464 : free_conflict_function (SUB_CONFLICTS_IN_A (subscript));
5657 3430464 : if (SUB_CONFLICTS_IN_B (subscript))
5658 3430464 : free_conflict_function (SUB_CONFLICTS_IN_B (subscript));
5659 :
5660 3430464 : SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
5661 3430464 : SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
5662 3430464 : SUB_LAST_CONFLICT (subscript) = last_conflicts;
5663 :
5664 : /* If there is any undetermined conflict function we have to
5665 : give a conservative answer in case we cannot prove that
5666 : no dependence exists when analyzing another subscript. */
5667 3430464 : if (CF_NOT_KNOWN_P (overlaps_a)
5668 3400881 : || CF_NOT_KNOWN_P (overlaps_b))
5669 : {
5670 29583 : res = chrec_dont_know;
5671 29583 : continue;
5672 : }
5673 :
5674 : /* When there is a subscript with no dependence we can stop. */
5675 3400881 : else if (CF_NO_DEPENDENCE_P (overlaps_a)
5676 1251991 : || CF_NO_DEPENDENCE_P (overlaps_b))
5677 : {
5678 2148890 : res = chrec_known;
5679 2148890 : break;
5680 : }
5681 : }
5682 :
5683 3093835 : if (res == NULL_TREE)
5684 : return true;
5685 :
5686 2177388 : if (res == chrec_known)
5687 2148890 : dependence_stats.num_dependence_independent++;
5688 : else
5689 28498 : dependence_stats.num_dependence_undetermined++;
5690 2177388 : finalize_ddr_dependent (ddr, res);
5691 2177388 : return false;
5692 : }
5693 :
5694 : /* Computes the conflicting iterations in LOOP_NEST, and initialize DDR. */
5695 :
5696 : static void
5697 3086073 : subscript_dependence_tester (struct data_dependence_relation *ddr,
5698 : class loop *loop_nest)
5699 : {
5700 3086073 : if (subscript_dependence_tester_1 (ddr, 0, 1, loop_nest))
5701 908689 : dependence_stats.num_dependence_dependent++;
5702 :
5703 3086073 : compute_subscript_distance (ddr);
5704 3086073 : if (build_classic_dist_vector (ddr, loop_nest))
5705 : {
5706 906501 : if (dump_file && (dump_flags & TDF_DETAILS))
5707 : {
5708 4031 : unsigned i;
5709 :
5710 4031 : fprintf (dump_file, "(build_classic_dist_vector\n");
5711 12168 : for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
5712 : {
5713 4106 : fprintf (dump_file, " dist_vector = (");
5714 4106 : print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
5715 8212 : DDR_NB_LOOPS (ddr));
5716 4106 : fprintf (dump_file, " )\n");
5717 : }
5718 4031 : fprintf (dump_file, ")\n");
5719 : }
5720 :
5721 906501 : build_classic_dir_vector (ddr);
5722 : }
5723 3086073 : }
5724 :
5725 : /* Returns true when all the access functions of A are affine or
5726 : constant with respect to LOOP_NEST. */
5727 :
5728 : static bool
5729 6239197 : access_functions_are_affine_or_constant_p (const struct data_reference *a,
5730 : const class loop *loop_nest)
5731 : {
5732 6239197 : vec<tree> fns = DR_ACCESS_FNS (a);
5733 27112549 : for (tree t : fns)
5734 8458941 : if (!evolution_function_is_invariant_p (t, loop_nest->num)
5735 8458941 : && !evolution_function_is_affine_multivariate_p (t, loop_nest->num))
5736 : return false;
5737 :
5738 : return true;
5739 : }
5740 :
5741 : /* This computes the affine dependence relation between A and B with
5742 : respect to LOOP_NEST. CHREC_KNOWN is used for representing the
5743 : independence between two accesses, while CHREC_DONT_KNOW is used
5744 : for representing the unknown relation.
5745 :
5746 : Note that it is possible to stop the computation of the dependence
5747 : relation the first time we detect a CHREC_KNOWN element for a given
5748 : subscript. */
5749 :
5750 : void
5751 6479810 : compute_affine_dependence (struct data_dependence_relation *ddr,
5752 : class loop *loop_nest)
5753 : {
5754 6479810 : struct data_reference *dra = DDR_A (ddr);
5755 6479810 : struct data_reference *drb = DDR_B (ddr);
5756 :
5757 6479810 : if (dump_file && (dump_flags & TDF_DETAILS))
5758 : {
5759 135539 : fprintf (dump_file, "(compute_affine_dependence\n");
5760 135539 : fprintf (dump_file, " ref_a: ");
5761 135539 : print_generic_expr (dump_file, DR_REF (dra));
5762 135539 : fprintf (dump_file, ", stmt_a: ");
5763 135539 : print_gimple_stmt (dump_file, DR_STMT (dra), 0, TDF_SLIM);
5764 135539 : fprintf (dump_file, " ref_b: ");
5765 135539 : print_generic_expr (dump_file, DR_REF (drb));
5766 135539 : fprintf (dump_file, ", stmt_b: ");
5767 135539 : print_gimple_stmt (dump_file, DR_STMT (drb), 0, TDF_SLIM);
5768 : }
5769 :
5770 : /* Analyze only when the dependence relation is not yet known. */
5771 6479810 : if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
5772 : {
5773 3150056 : dependence_stats.num_dependence_tests++;
5774 :
5775 3150056 : if (access_functions_are_affine_or_constant_p (dra, loop_nest)
5776 3150056 : && access_functions_are_affine_or_constant_p (drb, loop_nest))
5777 3086073 : subscript_dependence_tester (ddr, loop_nest);
5778 :
5779 : /* As a last case, if the dependence cannot be determined, or if
5780 : the dependence is considered too difficult to determine, answer
5781 : "don't know". */
5782 : else
5783 : {
5784 63983 : dependence_stats.num_dependence_undetermined++;
5785 :
5786 63983 : if (dump_file && (dump_flags & TDF_DETAILS))
5787 : {
5788 158 : fprintf (dump_file, "Data ref a:\n");
5789 158 : dump_data_reference (dump_file, dra);
5790 158 : fprintf (dump_file, "Data ref b:\n");
5791 158 : dump_data_reference (dump_file, drb);
5792 158 : fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
5793 : }
5794 63983 : finalize_ddr_dependent (ddr, chrec_dont_know);
5795 : }
5796 : }
5797 :
5798 6479810 : if (dump_file && (dump_flags & TDF_DETAILS))
5799 : {
5800 135539 : if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
5801 120117 : fprintf (dump_file, ") -> no dependence\n");
5802 15422 : else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
5803 11301 : fprintf (dump_file, ") -> dependence analysis failed\n");
5804 : else
5805 4121 : fprintf (dump_file, ")\n");
5806 : }
5807 6479810 : }
5808 :
5809 : /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
5810 : the data references in DATAREFS, in the LOOP_NEST. When
5811 : COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
5812 : relations. Return true when successful, i.e. data references number
5813 : is small enough to be handled. */
5814 :
5815 : bool
5816 434165 : compute_all_dependences (const vec<data_reference_p> &datarefs,
5817 : vec<ddr_p> *dependence_relations,
5818 : const vec<loop_p> &loop_nest,
5819 : bool compute_self_and_rr)
5820 : {
5821 434165 : struct data_dependence_relation *ddr;
5822 434165 : struct data_reference *a, *b;
5823 434165 : unsigned int i, j;
5824 :
5825 434165 : if ((int) datarefs.length ()
5826 434165 : > param_loop_max_datarefs_for_datadeps)
5827 : {
5828 0 : struct data_dependence_relation *ddr;
5829 :
5830 : /* Insert a single relation into dependence_relations:
5831 : chrec_dont_know. */
5832 0 : ddr = initialize_data_dependence_relation (NULL, NULL, loop_nest);
5833 0 : dependence_relations->safe_push (ddr);
5834 0 : return false;
5835 : }
5836 :
5837 1608822 : FOR_EACH_VEC_ELT (datarefs, i, a)
5838 7272015 : for (j = i + 1; datarefs.iterate (j, &b); j++)
5839 4922701 : if (DR_IS_WRITE (a) || DR_IS_WRITE (b) || compute_self_and_rr)
5840 : {
5841 4547049 : ddr = initialize_data_dependence_relation (a, b, loop_nest);
5842 4547049 : dependence_relations->safe_push (ddr);
5843 4547049 : if (loop_nest.exists ())
5844 4524634 : compute_affine_dependence (ddr, loop_nest[0]);
5845 : }
5846 :
5847 434165 : if (compute_self_and_rr)
5848 1028270 : FOR_EACH_VEC_ELT (datarefs, i, a)
5849 : {
5850 763948 : ddr = initialize_data_dependence_relation (a, a, loop_nest);
5851 763948 : dependence_relations->safe_push (ddr);
5852 763948 : if (loop_nest.exists ())
5853 763948 : compute_affine_dependence (ddr, loop_nest[0]);
5854 : }
5855 :
5856 : return true;
5857 : }
5858 :
5859 : /* Describes a location of a memory reference. */
5860 :
5861 : struct data_ref_loc
5862 : {
5863 : /* The memory reference. */
5864 : tree ref;
5865 :
5866 : /* True if the memory reference is read. */
5867 : bool is_read;
5868 :
5869 : /* True if the data reference is conditional within the containing
5870 : statement, i.e. if it might not occur even when the statement
5871 : is executed and runs to completion. */
5872 : bool is_conditional_in_stmt;
5873 : };
5874 :
5875 :
5876 : /* Stores the locations of memory references in STMT to REFERENCES. Returns
5877 : true if STMT clobbers memory, false otherwise. */
5878 :
5879 : static bool
5880 51145382 : get_references_in_stmt (gimple *stmt, vec<data_ref_loc, va_heap> *references)
5881 : {
5882 51145382 : bool clobbers_memory = false;
5883 51145382 : data_ref_loc ref;
5884 51145382 : tree op0, op1;
5885 51145382 : enum gimple_code stmt_code = gimple_code (stmt);
5886 :
5887 : /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
5888 : As we cannot model data-references to not spelled out
5889 : accesses give up if they may occur. */
5890 51145382 : if (stmt_code == GIMPLE_CALL
5891 51145382 : && !(gimple_call_flags (stmt) & ECF_CONST))
5892 : {
5893 : /* Allow IFN_GOMP_SIMD_LANE in their own loops. */
5894 4226023 : if (gimple_call_internal_p (stmt))
5895 60201 : switch (gimple_call_internal_fn (stmt))
5896 : {
5897 5605 : case IFN_GOMP_SIMD_LANE:
5898 5605 : {
5899 5605 : class loop *loop = gimple_bb (stmt)->loop_father;
5900 5605 : tree uid = gimple_call_arg (stmt, 0);
5901 5605 : gcc_assert (TREE_CODE (uid) == SSA_NAME);
5902 5605 : if (loop == NULL
5903 5605 : || loop->simduid != SSA_NAME_VAR (uid))
5904 : clobbers_memory = true;
5905 : break;
5906 : }
5907 : case IFN_MASK_LOAD:
5908 : case IFN_MASK_STORE:
5909 : break;
5910 999 : case IFN_MASK_CALL:
5911 999 : {
5912 999 : tree orig_fndecl
5913 999 : = gimple_call_addr_fndecl (gimple_call_arg (stmt, 0));
5914 999 : if (!orig_fndecl
5915 999 : || (flags_from_decl_or_type (orig_fndecl) & ECF_CONST) == 0)
5916 : clobbers_memory = true;
5917 : }
5918 : break;
5919 : default:
5920 4261741 : clobbers_memory = true;
5921 : break;
5922 : }
5923 4165822 : else if (gimple_call_builtin_p (stmt, BUILT_IN_PREFETCH))
5924 : clobbers_memory = false;
5925 : else
5926 4261741 : clobbers_memory = true;
5927 : }
5928 46919359 : else if (stmt_code == GIMPLE_ASM
5929 46919359 : && (gimple_asm_volatile_p (as_a <gasm *> (stmt))
5930 8535 : || gimple_vuse (stmt)))
5931 : clobbers_memory = true;
5932 :
5933 86422188 : if (!gimple_vuse (stmt))
5934 : return clobbers_memory;
5935 :
5936 19596601 : if (stmt_code == GIMPLE_ASSIGN)
5937 : {
5938 14432648 : tree base;
5939 14432648 : op0 = gimple_assign_lhs (stmt);
5940 14432648 : op1 = gimple_assign_rhs1 (stmt);
5941 :
5942 14432648 : if (DECL_P (op1)
5943 14432648 : || (REFERENCE_CLASS_P (op1)
5944 6915397 : && (base = get_base_address (op1))
5945 6915397 : && TREE_CODE (base) != SSA_NAME
5946 6915327 : && !is_gimple_min_invariant (base)))
5947 : {
5948 7801168 : ref.ref = op1;
5949 7801168 : ref.is_read = true;
5950 7801168 : ref.is_conditional_in_stmt = false;
5951 7801168 : references->safe_push (ref);
5952 : }
5953 : }
5954 5163953 : else if (stmt_code == GIMPLE_CALL)
5955 : {
5956 4241611 : unsigned i = 0, n;
5957 4241611 : tree ptr, type;
5958 4241611 : unsigned int align;
5959 :
5960 4241611 : ref.is_read = false;
5961 4241611 : if (gimple_call_internal_p (stmt))
5962 75357 : switch (gimple_call_internal_fn (stmt))
5963 : {
5964 2042 : case IFN_MASK_LOAD:
5965 2042 : if (gimple_call_lhs (stmt) == NULL_TREE)
5966 : break;
5967 2042 : ref.is_read = true;
5968 : /* FALLTHRU */
5969 3849 : case IFN_MASK_STORE:
5970 3849 : ptr = build_int_cst (TREE_TYPE (gimple_call_arg (stmt, 1)), 0);
5971 3849 : align = tree_to_shwi (gimple_call_arg (stmt, 1));
5972 3849 : if (ref.is_read)
5973 2042 : type = TREE_TYPE (gimple_call_lhs (stmt));
5974 : else
5975 1807 : type = TREE_TYPE (gimple_call_arg (stmt, 3));
5976 3849 : if (TYPE_ALIGN (type) != align)
5977 1500 : type = build_aligned_type (type, align);
5978 3849 : ref.is_conditional_in_stmt = true;
5979 3849 : ref.ref = fold_build2 (MEM_REF, type, gimple_call_arg (stmt, 0),
5980 : ptr);
5981 3849 : references->safe_push (ref);
5982 3849 : return false;
5983 : case IFN_MASK_CALL:
5984 4237762 : i = 1;
5985 : gcc_fallthrough ();
5986 : default:
5987 : break;
5988 : }
5989 :
5990 4237762 : op0 = gimple_call_lhs (stmt);
5991 4237762 : n = gimple_call_num_args (stmt);
5992 17196246 : for (; i < n; i++)
5993 : {
5994 8720722 : op1 = gimple_call_arg (stmt, i);
5995 :
5996 8720722 : if (DECL_P (op1)
5997 8720722 : || (REFERENCE_CLASS_P (op1) && get_base_address (op1)))
5998 : {
5999 518109 : ref.ref = op1;
6000 518109 : ref.is_read = true;
6001 518109 : ref.is_conditional_in_stmt = false;
6002 518109 : references->safe_push (ref);
6003 : }
6004 : }
6005 : }
6006 : else
6007 : return clobbers_memory;
6008 :
6009 18670410 : if (op0
6010 18670410 : && (DECL_P (op0)
6011 15151769 : || (REFERENCE_CLASS_P (op0) && get_base_address (op0))))
6012 : {
6013 7474295 : ref.ref = op0;
6014 7474295 : ref.is_read = false;
6015 7474295 : ref.is_conditional_in_stmt = false;
6016 7474295 : references->safe_push (ref);
6017 : }
6018 : return clobbers_memory;
6019 : }
6020 :
6021 :
6022 : /* Returns true if the loop-nest has any data reference. */
6023 :
6024 : bool
6025 752 : loop_nest_has_data_refs (loop_p loop)
6026 : {
6027 752 : basic_block *bbs = get_loop_body (loop);
6028 752 : auto_vec<data_ref_loc, 3> references;
6029 :
6030 1001 : for (unsigned i = 0; i < loop->num_nodes; i++)
6031 : {
6032 931 : basic_block bb = bbs[i];
6033 931 : gimple_stmt_iterator bsi;
6034 :
6035 3224 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
6036 : {
6037 2044 : gimple *stmt = gsi_stmt (bsi);
6038 2044 : get_references_in_stmt (stmt, &references);
6039 2044 : if (references.length ())
6040 : {
6041 682 : free (bbs);
6042 682 : return true;
6043 : }
6044 : }
6045 : }
6046 70 : free (bbs);
6047 70 : return false;
6048 752 : }
6049 :
6050 : /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
6051 : reference, returns false, otherwise returns true. NEST is the outermost
6052 : loop of the loop nest in which the references should be analyzed. */
6053 :
6054 : opt_result
6055 51129018 : find_data_references_in_stmt (class loop *nest, gimple *stmt,
6056 : vec<data_reference_p> *datarefs)
6057 : {
6058 51129018 : auto_vec<data_ref_loc, 2> references;
6059 51129018 : data_reference_p dr;
6060 :
6061 51129018 : if (get_references_in_stmt (stmt, &references))
6062 4261737 : return opt_result::failure_at (stmt, "statement clobbers memory: %G",
6063 : stmt);
6064 :
6065 155614310 : for (const data_ref_loc &ref : references)
6066 : {
6067 15012467 : dr = create_data_ref (nest ? loop_preheader_edge (nest) : NULL,
6068 15012467 : loop_containing_stmt (stmt), ref.ref,
6069 15012467 : stmt, ref.is_read, ref.is_conditional_in_stmt);
6070 15012467 : gcc_assert (dr != NULL);
6071 15012467 : datarefs->safe_push (dr);
6072 : }
6073 :
6074 46867281 : return opt_result::success ();
6075 51129018 : }
6076 :
6077 : /* Stores the data references in STMT to DATAREFS. If there is an
6078 : unanalyzable reference, returns false, otherwise returns true.
6079 : NEST is the outermost loop of the loop nest in which the references
6080 : should be instantiated, LOOP is the loop in which the references
6081 : should be analyzed. */
6082 :
6083 : bool
6084 14320 : graphite_find_data_references_in_stmt (edge nest, loop_p loop, gimple *stmt,
6085 : vec<data_reference_p> *datarefs)
6086 : {
6087 14320 : auto_vec<data_ref_loc, 2> references;
6088 14320 : bool ret = true;
6089 14320 : data_reference_p dr;
6090 :
6091 14320 : if (get_references_in_stmt (stmt, &references))
6092 : return false;
6093 :
6094 45850 : for (const data_ref_loc &ref : references)
6095 : {
6096 5804 : dr = create_data_ref (nest, loop, ref.ref, stmt, ref.is_read,
6097 2902 : ref.is_conditional_in_stmt);
6098 2902 : gcc_assert (dr != NULL);
6099 2902 : datarefs->safe_push (dr);
6100 : }
6101 :
6102 : return ret;
6103 14320 : }
6104 :
6105 : /* Search the data references in LOOP, and record the information into
6106 : DATAREFS. Returns chrec_dont_know when failing to analyze a
6107 : difficult case, returns NULL_TREE otherwise. */
6108 :
6109 : tree
6110 2711370 : find_data_references_in_bb (class loop *loop, basic_block bb,
6111 : vec<data_reference_p> *datarefs)
6112 : {
6113 2711370 : gimple_stmt_iterator bsi;
6114 :
6115 23176528 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
6116 : {
6117 18251900 : gimple *stmt = gsi_stmt (bsi);
6118 :
6119 18251900 : if (!find_data_references_in_stmt (loop, stmt, datarefs))
6120 : {
6121 498112 : struct data_reference *res;
6122 498112 : res = XCNEW (struct data_reference);
6123 498112 : datarefs->safe_push (res);
6124 :
6125 498112 : return chrec_dont_know;
6126 : }
6127 : }
6128 :
6129 : return NULL_TREE;
6130 : }
6131 :
6132 : /* Search the data references in LOOP, and record the information into
6133 : DATAREFS. Returns chrec_dont_know when failing to analyze a
6134 : difficult case, returns NULL_TREE otherwise.
6135 :
6136 : TODO: This function should be made smarter so that it can handle address
6137 : arithmetic as if they were array accesses, etc. */
6138 :
6139 : tree
6140 819486 : find_data_references_in_loop (class loop *loop,
6141 : vec<data_reference_p> *datarefs)
6142 : {
6143 819486 : basic_block bb, *bbs;
6144 819486 : unsigned int i;
6145 :
6146 819486 : bbs = get_loop_body_in_dom_order (loop);
6147 :
6148 3637202 : for (i = 0; i < loop->num_nodes; i++)
6149 : {
6150 2298175 : bb = bbs[i];
6151 :
6152 2298175 : if (find_data_references_in_bb (loop, bb, datarefs) == chrec_dont_know)
6153 : {
6154 299945 : free (bbs);
6155 299945 : return chrec_dont_know;
6156 : }
6157 : }
6158 519541 : free (bbs);
6159 :
6160 519541 : return NULL_TREE;
6161 : }
6162 :
6163 : /* Return the alignment in bytes that DRB is guaranteed to have at all
6164 : times. */
6165 :
6166 : unsigned int
6167 490474 : dr_alignment (innermost_loop_behavior *drb)
6168 : {
6169 : /* Get the alignment of BASE_ADDRESS + INIT. */
6170 490474 : unsigned int alignment = drb->base_alignment;
6171 490474 : unsigned int misalignment = (drb->base_misalignment
6172 490474 : + TREE_INT_CST_LOW (drb->init));
6173 490474 : if (misalignment != 0)
6174 214428 : alignment = MIN (alignment, misalignment & -misalignment);
6175 :
6176 : /* Cap it to the alignment of OFFSET. */
6177 490474 : if (!integer_zerop (drb->offset))
6178 36166 : alignment = MIN (alignment, drb->offset_alignment);
6179 :
6180 : /* Cap it to the alignment of STEP. */
6181 490474 : if (!integer_zerop (drb->step))
6182 291347 : alignment = MIN (alignment, drb->step_alignment);
6183 :
6184 490474 : return alignment;
6185 : }
6186 :
6187 : /* If BASE is a pointer-typed SSA name, try to find the object that it
6188 : is based on. Return this object X on success and store the alignment
6189 : in bytes of BASE - &X in *ALIGNMENT_OUT. */
6190 :
6191 : static tree
6192 770437 : get_base_for_alignment_1 (tree base, unsigned int *alignment_out)
6193 : {
6194 770437 : if (TREE_CODE (base) != SSA_NAME || !POINTER_TYPE_P (TREE_TYPE (base)))
6195 : return NULL_TREE;
6196 :
6197 379123 : gimple *def = SSA_NAME_DEF_STMT (base);
6198 379123 : base = analyze_scalar_evolution (loop_containing_stmt (def), base);
6199 :
6200 : /* Peel chrecs and record the minimum alignment preserved by
6201 : all steps. */
6202 379123 : unsigned int alignment = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
6203 768157 : while (TREE_CODE (base) == POLYNOMIAL_CHREC)
6204 : {
6205 9911 : unsigned int step_alignment = highest_pow2_factor (CHREC_RIGHT (base));
6206 9911 : alignment = MIN (alignment, step_alignment);
6207 9911 : base = CHREC_LEFT (base);
6208 : }
6209 :
6210 : /* Punt if the expression is too complicated to handle. */
6211 379123 : if (tree_contains_chrecs (base, NULL) || !POINTER_TYPE_P (TREE_TYPE (base)))
6212 : return NULL_TREE;
6213 :
6214 : /* The only useful cases are those for which a dereference folds to something
6215 : other than an INDIRECT_REF. */
6216 379081 : tree ref_type = TREE_TYPE (TREE_TYPE (base));
6217 379081 : tree ref = fold_indirect_ref_1 (UNKNOWN_LOCATION, ref_type, base);
6218 379081 : if (!ref)
6219 : return NULL_TREE;
6220 :
6221 : /* Analyze the base to which the steps we peeled were applied. */
6222 2609 : poly_int64 bitsize, bitpos, bytepos;
6223 2609 : machine_mode mode;
6224 2609 : int unsignedp, reversep, volatilep;
6225 2609 : tree offset;
6226 2609 : base = get_inner_reference (ref, &bitsize, &bitpos, &offset, &mode,
6227 : &unsignedp, &reversep, &volatilep);
6228 2609 : if (!base || !multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
6229 : return NULL_TREE;
6230 :
6231 : /* Restrict the alignment to that guaranteed by the offsets. */
6232 2609 : unsigned int bytepos_alignment = known_alignment (bytepos);
6233 2609 : if (bytepos_alignment != 0)
6234 2458 : alignment = MIN (alignment, bytepos_alignment);
6235 2609 : if (offset)
6236 : {
6237 0 : unsigned int offset_alignment = highest_pow2_factor (offset);
6238 0 : alignment = MIN (alignment, offset_alignment);
6239 : }
6240 :
6241 2609 : *alignment_out = alignment;
6242 2609 : return base;
6243 : }
6244 :
6245 : /* Return the object whose alignment would need to be changed in order
6246 : to increase the alignment of ADDR. Store the maximum achievable
6247 : alignment in *MAX_ALIGNMENT. */
6248 :
6249 : tree
6250 770437 : get_base_for_alignment (tree addr, unsigned int *max_alignment)
6251 : {
6252 770437 : tree base = get_base_for_alignment_1 (addr, max_alignment);
6253 770437 : if (base)
6254 : return base;
6255 :
6256 767828 : if (TREE_CODE (addr) == ADDR_EXPR)
6257 290523 : addr = TREE_OPERAND (addr, 0);
6258 767828 : *max_alignment = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
6259 767828 : return addr;
6260 : }
6261 :
6262 : /* Recursive helper function. */
6263 :
6264 : static bool
6265 137085 : find_loop_nest_1 (class loop *loop, vec<loop_p> *loop_nest)
6266 : {
6267 : /* Inner loops of the nest should not contain siblings. Example:
6268 : when there are two consecutive loops,
6269 :
6270 : | loop_0
6271 : | loop_1
6272 : | A[{0, +, 1}_1]
6273 : | endloop_1
6274 : | loop_2
6275 : | A[{0, +, 1}_2]
6276 : | endloop_2
6277 : | endloop_0
6278 :
6279 : the dependence relation cannot be captured by the distance
6280 : abstraction. */
6281 137085 : if (loop->next)
6282 : return false;
6283 :
6284 116142 : loop_nest->safe_push (loop);
6285 116142 : if (loop->inner)
6286 40185 : return find_loop_nest_1 (loop->inner, loop_nest);
6287 : return true;
6288 : }
6289 :
6290 : /* Return false when the LOOP is not well nested. Otherwise return
6291 : true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
6292 : contain the loops from the outermost to the innermost, as they will
6293 : appear in the classic distance vector. */
6294 :
6295 : bool
6296 1028196 : find_loop_nest (class loop *loop, vec<loop_p> *loop_nest)
6297 : {
6298 1028196 : loop_nest->safe_push (loop);
6299 1028196 : if (loop->inner)
6300 96900 : return find_loop_nest_1 (loop->inner, loop_nest);
6301 : return true;
6302 : }
6303 :
6304 : /* Returns true when the data dependences have been computed, false otherwise.
6305 : Given a loop nest LOOP, the following vectors are returned:
6306 : DATAREFS is initialized to all the array elements contained in this loop,
6307 : DEPENDENCE_RELATIONS contains the relations between the data references.
6308 : Compute read-read and self relations if
6309 : COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
6310 :
6311 : bool
6312 411655 : compute_data_dependences_for_loop (class loop *loop,
6313 : bool compute_self_and_read_read_dependences,
6314 : vec<loop_p> *loop_nest,
6315 : vec<data_reference_p> *datarefs,
6316 : vec<ddr_p> *dependence_relations)
6317 : {
6318 411655 : bool res = true;
6319 :
6320 411655 : memset (&dependence_stats, 0, sizeof (dependence_stats));
6321 :
6322 : /* If the loop nest is not well formed, or one of the data references
6323 : is not computable, give up without spending time to compute other
6324 : dependences. */
6325 411655 : if (!loop
6326 411655 : || !find_loop_nest (loop, loop_nest)
6327 411653 : || find_data_references_in_loop (loop, datarefs) == chrec_dont_know
6328 675911 : || !compute_all_dependences (*datarefs, dependence_relations, *loop_nest,
6329 : compute_self_and_read_read_dependences))
6330 : res = false;
6331 :
6332 411655 : if (dump_file && (dump_flags & TDF_STATS))
6333 : {
6334 157 : fprintf (dump_file, "Dependence tester statistics:\n");
6335 :
6336 157 : fprintf (dump_file, "Number of dependence tests: %d\n",
6337 : dependence_stats.num_dependence_tests);
6338 157 : fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
6339 : dependence_stats.num_dependence_dependent);
6340 157 : fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
6341 : dependence_stats.num_dependence_independent);
6342 157 : fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
6343 : dependence_stats.num_dependence_undetermined);
6344 :
6345 157 : fprintf (dump_file, "Number of subscript tests: %d\n",
6346 : dependence_stats.num_subscript_tests);
6347 157 : fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
6348 : dependence_stats.num_subscript_undetermined);
6349 157 : fprintf (dump_file, "Number of same subscript function: %d\n",
6350 : dependence_stats.num_same_subscript_function);
6351 :
6352 157 : fprintf (dump_file, "Number of ziv tests: %d\n",
6353 : dependence_stats.num_ziv);
6354 157 : fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
6355 : dependence_stats.num_ziv_dependent);
6356 157 : fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
6357 : dependence_stats.num_ziv_independent);
6358 157 : fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
6359 : dependence_stats.num_ziv_unimplemented);
6360 :
6361 157 : fprintf (dump_file, "Number of siv tests: %d\n",
6362 : dependence_stats.num_siv);
6363 157 : fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
6364 : dependence_stats.num_siv_dependent);
6365 157 : fprintf (dump_file, "Number of siv tests returning independent: %d\n",
6366 : dependence_stats.num_siv_independent);
6367 157 : fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
6368 : dependence_stats.num_siv_unimplemented);
6369 :
6370 157 : fprintf (dump_file, "Number of miv tests: %d\n",
6371 : dependence_stats.num_miv);
6372 157 : fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
6373 : dependence_stats.num_miv_dependent);
6374 157 : fprintf (dump_file, "Number of miv tests returning independent: %d\n",
6375 : dependence_stats.num_miv_independent);
6376 157 : fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
6377 : dependence_stats.num_miv_unimplemented);
6378 : }
6379 :
6380 411655 : return res;
6381 : }
6382 :
6383 : /* Free the memory used by a data dependence relation DDR. */
6384 :
6385 : void
6386 13446231 : free_dependence_relation (struct data_dependence_relation *ddr)
6387 : {
6388 13446231 : if (ddr == NULL)
6389 : return;
6390 :
6391 13446231 : if (DDR_SUBSCRIPTS (ddr).exists ())
6392 908685 : free_subscripts (DDR_SUBSCRIPTS (ddr));
6393 13446231 : DDR_DIST_VECTS (ddr).release ();
6394 13446231 : DDR_DIR_VECTS (ddr).release ();
6395 :
6396 13446231 : free (ddr);
6397 : }
6398 :
6399 : /* Free the memory used by the data dependence relations from
6400 : DEPENDENCE_RELATIONS. */
6401 :
6402 : void
6403 2908659 : free_dependence_relations (vec<ddr_p>& dependence_relations)
6404 : {
6405 9403177 : for (data_dependence_relation *ddr : dependence_relations)
6406 5314256 : if (ddr)
6407 5314256 : free_dependence_relation (ddr);
6408 :
6409 2908659 : dependence_relations.release ();
6410 2908659 : }
6411 :
6412 : /* Free the memory used by the data references from DATAREFS. */
6413 :
6414 : void
6415 3571004 : free_data_refs (vec<data_reference_p>& datarefs)
6416 : {
6417 21441657 : for (data_reference *dr : datarefs)
6418 13427541 : free_data_ref (dr);
6419 3571004 : datarefs.release ();
6420 3571004 : }
6421 :
6422 : /* Common routine implementing both dr_direction_indicator and
6423 : dr_zero_step_indicator. Return USEFUL_MIN if the indicator is known
6424 : to be >= USEFUL_MIN and -1 if the indicator is known to be negative.
6425 : Return the step as the indicator otherwise. */
6426 :
6427 : static tree
6428 66827 : dr_step_indicator (struct data_reference *dr, int useful_min)
6429 : {
6430 66827 : tree step = DR_STEP (dr);
6431 66827 : if (!step)
6432 : return NULL_TREE;
6433 66827 : STRIP_NOPS (step);
6434 : /* Look for cases where the step is scaled by a positive constant
6435 : integer, which will often be the access size. If the multiplication
6436 : doesn't change the sign (due to overflow effects) then we can
6437 : test the unscaled value instead. */
6438 66827 : if (TREE_CODE (step) == MULT_EXPR
6439 5499 : && TREE_CODE (TREE_OPERAND (step, 1)) == INTEGER_CST
6440 72270 : && tree_int_cst_sgn (TREE_OPERAND (step, 1)) > 0)
6441 : {
6442 5443 : tree factor = TREE_OPERAND (step, 1);
6443 5443 : step = TREE_OPERAND (step, 0);
6444 :
6445 : /* Strip widening and truncating conversions as well as nops. */
6446 1217 : if (CONVERT_EXPR_P (step)
6447 5443 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (step, 0))))
6448 4226 : step = TREE_OPERAND (step, 0);
6449 5443 : tree type = TREE_TYPE (step);
6450 :
6451 : /* Get the range of step values that would not cause overflow. */
6452 10886 : widest_int minv = (wi::to_widest (TYPE_MIN_VALUE (ssizetype))
6453 5443 : / wi::to_widest (factor));
6454 10886 : widest_int maxv = (wi::to_widest (TYPE_MAX_VALUE (ssizetype))
6455 5443 : / wi::to_widest (factor));
6456 :
6457 : /* Get the range of values that the unconverted step actually has. */
6458 5443 : wide_int step_min, step_max;
6459 5443 : int_range_max vr;
6460 5443 : if (TREE_CODE (step) != SSA_NAME
6461 10778 : || !get_range_query (cfun)->range_of_expr (vr, step)
6462 10832 : || vr.undefined_p ())
6463 : {
6464 54 : step_min = wi::to_wide (TYPE_MIN_VALUE (type));
6465 54 : step_max = wi::to_wide (TYPE_MAX_VALUE (type));
6466 : }
6467 : else
6468 : {
6469 5389 : step_min = vr.lower_bound ();
6470 5389 : step_max = vr.upper_bound ();
6471 : }
6472 :
6473 : /* Check whether the unconverted step has an acceptable range. */
6474 5443 : signop sgn = TYPE_SIGN (type);
6475 10886 : if (wi::les_p (minv, widest_int::from (step_min, sgn))
6476 14010 : && wi::ges_p (maxv, widest_int::from (step_max, sgn)))
6477 : {
6478 1553 : if (wi::ge_p (step_min, useful_min, sgn))
6479 440 : return ssize_int (useful_min);
6480 1113 : else if (wi::lt_p (step_max, 0, sgn))
6481 0 : return ssize_int (-1);
6482 : else
6483 1113 : return fold_convert (ssizetype, step);
6484 : }
6485 5443 : }
6486 65274 : return DR_STEP (dr);
6487 : }
6488 :
6489 : /* Return a value that is negative iff DR has a negative step. */
6490 :
6491 : tree
6492 12010 : dr_direction_indicator (struct data_reference *dr)
6493 : {
6494 12010 : return dr_step_indicator (dr, 0);
6495 : }
6496 :
6497 : /* Return a value that is zero iff DR has a zero step. */
6498 :
6499 : tree
6500 54817 : dr_zero_step_indicator (struct data_reference *dr)
6501 : {
6502 54817 : return dr_step_indicator (dr, 1);
6503 : }
6504 :
6505 : /* Return true if DR is known to have a nonnegative (but possibly zero)
6506 : step. */
6507 :
6508 : bool
6509 5083 : dr_known_forward_stride_p (struct data_reference *dr)
6510 : {
6511 5083 : tree indicator = dr_direction_indicator (dr);
6512 5083 : tree neg_step_val = fold_binary (LT_EXPR, boolean_type_node,
6513 : fold_convert (ssizetype, indicator),
6514 : ssize_int (0));
6515 5083 : return neg_step_val && integer_zerop (neg_step_val);
6516 : }
|