2 Copyright (C) 2000-2016 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of GCC.
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
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
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/>. */
21 /* dependency.c -- Expression dependency analysis code. */
22 /* There's probably quite a bit of duplication in this file. We currently
23 have different dependency checking functions for different types
24 if dependencies. Ideally these would probably be merged. */
28 #include "coretypes.h"
30 #include "dependency.h"
31 #include "constructor.h"
34 /* static declarations */
36 enum range
{LHS
, RHS
, MID
};
38 /* Dependency types. These must be in reverse order of priority. */
42 GFC_DEP_EQUAL
, /* Identical Ranges. */
43 GFC_DEP_FORWARD
, /* e.g., a(1:3) = a(2:4). */
44 GFC_DEP_BACKWARD
, /* e.g. a(2:4) = a(1:3). */
45 GFC_DEP_OVERLAP
, /* May overlap in some other way. */
46 GFC_DEP_NODEP
/* Distinct ranges. */
50 #define IS_ARRAY_EXPLICIT(as) ((as->type == AS_EXPLICIT ? 1 : 0))
52 /* Forward declarations */
54 static gfc_dependency
check_section_vs_section (gfc_array_ref
*,
55 gfc_array_ref
*, int);
57 /* Returns 1 if the expr is an integer constant value 1, 0 if it is not or
58 def if the value could not be determined. */
61 gfc_expr_is_one (gfc_expr
*expr
, int def
)
63 gcc_assert (expr
!= NULL
);
65 if (expr
->expr_type
!= EXPR_CONSTANT
)
68 if (expr
->ts
.type
!= BT_INTEGER
)
71 return mpz_cmp_si (expr
->value
.integer
, 1) == 0;
74 /* Check if two array references are known to be identical. Calls
75 gfc_dep_compare_expr if necessary for comparing array indices. */
78 identical_array_ref (gfc_array_ref
*a1
, gfc_array_ref
*a2
)
82 if (a1
->type
== AR_FULL
&& a2
->type
== AR_FULL
)
85 if (a1
->type
== AR_SECTION
&& a2
->type
== AR_SECTION
)
87 gcc_assert (a1
->dimen
== a2
->dimen
);
89 for ( i
= 0; i
< a1
->dimen
; i
++)
91 /* TODO: Currently, we punt on an integer array as an index. */
92 if (a1
->dimen_type
[i
] != DIMEN_RANGE
93 || a2
->dimen_type
[i
] != DIMEN_RANGE
)
96 if (check_section_vs_section (a1
, a2
, i
) != GFC_DEP_EQUAL
)
102 if (a1
->type
== AR_ELEMENT
&& a2
->type
== AR_ELEMENT
)
104 gcc_assert (a1
->dimen
== a2
->dimen
);
105 for (i
= 0; i
< a1
->dimen
; i
++)
107 if (gfc_dep_compare_expr (a1
->start
[i
], a2
->start
[i
]) != 0)
117 /* Return true for identical variables, checking for references if
118 necessary. Calls identical_array_ref for checking array sections. */
121 are_identical_variables (gfc_expr
*e1
, gfc_expr
*e2
)
125 if (e1
->symtree
->n
.sym
->attr
.dummy
&& e2
->symtree
->n
.sym
->attr
.dummy
)
127 /* Dummy arguments: Only check for equal names. */
128 if (e1
->symtree
->n
.sym
->name
!= e2
->symtree
->n
.sym
->name
)
133 /* Check for equal symbols. */
134 if (e1
->symtree
->n
.sym
!= e2
->symtree
->n
.sym
)
138 /* Volatile variables should never compare equal to themselves. */
140 if (e1
->symtree
->n
.sym
->attr
.volatile_
)
146 while (r1
!= NULL
|| r2
!= NULL
)
149 /* Assume the variables are not equal if one has a reference and the
151 TODO: Handle full references like comparing a(:) to a.
154 if (r1
== NULL
|| r2
== NULL
)
157 if (r1
->type
!= r2
->type
)
164 if (!identical_array_ref (&r1
->u
.ar
, &r2
->u
.ar
))
170 if (r1
->u
.c
.component
!= r2
->u
.c
.component
)
175 if (gfc_dep_compare_expr (r1
->u
.ss
.start
, r2
->u
.ss
.start
) != 0)
178 /* If both are NULL, the end length compares equal, because we
179 are looking at the same variable. This can only happen for
180 assumed- or deferred-length character arguments. */
182 if (r1
->u
.ss
.end
== NULL
&& r2
->u
.ss
.end
== NULL
)
185 if (gfc_dep_compare_expr (r1
->u
.ss
.end
, r2
->u
.ss
.end
) != 0)
191 gfc_internal_error ("are_identical_variables: Bad type");
199 /* Compare two functions for equality. Returns 0 if e1==e2, -2 otherwise. If
200 impure_ok is false, only return 0 for pure functions. */
203 gfc_dep_compare_functions (gfc_expr
*e1
, gfc_expr
*e2
, bool impure_ok
)
206 gfc_actual_arglist
*args1
;
207 gfc_actual_arglist
*args2
;
209 if (e1
->expr_type
!= EXPR_FUNCTION
|| e2
->expr_type
!= EXPR_FUNCTION
)
212 if ((e1
->value
.function
.esym
&& e2
->value
.function
.esym
213 && e1
->value
.function
.esym
== e2
->value
.function
.esym
214 && (e1
->value
.function
.esym
->result
->attr
.pure
|| impure_ok
))
215 || (e1
->value
.function
.isym
&& e2
->value
.function
.isym
216 && e1
->value
.function
.isym
== e2
->value
.function
.isym
217 && (e1
->value
.function
.isym
->pure
|| impure_ok
)))
219 args1
= e1
->value
.function
.actual
;
220 args2
= e2
->value
.function
.actual
;
222 /* Compare the argument lists for equality. */
223 while (args1
&& args2
)
225 /* Bitwise xor, since C has no non-bitwise xor operator. */
226 if ((args1
->expr
== NULL
) ^ (args2
->expr
== NULL
))
229 if (args1
->expr
!= NULL
&& args2
->expr
!= NULL
)
235 if (gfc_dep_compare_expr (e1
, e2
) != 0)
238 /* Special case: String arguments which compare equal can have
239 different lengths, which makes them different in calls to
242 if (e1
->expr_type
== EXPR_CONSTANT
243 && e1
->ts
.type
== BT_CHARACTER
244 && e2
->expr_type
== EXPR_CONSTANT
245 && e2
->ts
.type
== BT_CHARACTER
246 && e1
->value
.character
.length
!= e2
->value
.character
.length
)
253 return (args1
|| args2
) ? -2 : 0;
259 /* Helper function to look through parens, unary plus and widening
260 integer conversions. */
263 gfc_discard_nops (gfc_expr
*e
)
265 gfc_actual_arglist
*arglist
;
272 if (e
->expr_type
== EXPR_OP
273 && (e
->value
.op
.op
== INTRINSIC_UPLUS
274 || e
->value
.op
.op
== INTRINSIC_PARENTHESES
))
280 if (e
->expr_type
== EXPR_FUNCTION
&& e
->value
.function
.isym
281 && e
->value
.function
.isym
->id
== GFC_ISYM_CONVERSION
282 && e
->ts
.type
== BT_INTEGER
)
284 arglist
= e
->value
.function
.actual
;
285 if (arglist
->expr
->ts
.type
== BT_INTEGER
286 && e
->ts
.kind
> arglist
->expr
->ts
.kind
)
299 /* Compare two expressions. Return values:
303 * -2 if the relationship could not be determined
304 * -3 if e1 /= e2, but we cannot tell which one is larger.
305 REAL and COMPLEX constants are only compared for equality
306 or inequality; if they are unequal, -2 is returned in all cases. */
309 gfc_dep_compare_expr (gfc_expr
*e1
, gfc_expr
*e2
)
313 if (e1
== NULL
&& e2
== NULL
)
316 e1
= gfc_discard_nops (e1
);
317 e2
= gfc_discard_nops (e2
);
319 if (e1
->expr_type
== EXPR_OP
&& e1
->value
.op
.op
== INTRINSIC_PLUS
)
321 /* Compare X+C vs. X, for INTEGER only. */
322 if (e1
->value
.op
.op2
->expr_type
== EXPR_CONSTANT
323 && e1
->value
.op
.op2
->ts
.type
== BT_INTEGER
324 && gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
) == 0)
325 return mpz_sgn (e1
->value
.op
.op2
->value
.integer
);
327 /* Compare P+Q vs. R+S. */
328 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_PLUS
)
332 l
= gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op1
);
333 r
= gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op2
);
334 if (l
== 0 && r
== 0)
336 if (l
== 0 && r
> -2)
338 if (l
> -2 && r
== 0)
340 if (l
== 1 && r
== 1)
342 if (l
== -1 && r
== -1)
345 l
= gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op2
);
346 r
= gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op1
);
347 if (l
== 0 && r
== 0)
349 if (l
== 0 && r
> -2)
351 if (l
> -2 && r
== 0)
353 if (l
== 1 && r
== 1)
355 if (l
== -1 && r
== -1)
360 /* Compare X vs. X+C, for INTEGER only. */
361 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_PLUS
)
363 if (e2
->value
.op
.op2
->expr_type
== EXPR_CONSTANT
364 && e2
->value
.op
.op2
->ts
.type
== BT_INTEGER
365 && gfc_dep_compare_expr (e1
, e2
->value
.op
.op1
) == 0)
366 return -mpz_sgn (e2
->value
.op
.op2
->value
.integer
);
369 /* Compare X-C vs. X, for INTEGER only. */
370 if (e1
->expr_type
== EXPR_OP
&& e1
->value
.op
.op
== INTRINSIC_MINUS
)
372 if (e1
->value
.op
.op2
->expr_type
== EXPR_CONSTANT
373 && e1
->value
.op
.op2
->ts
.type
== BT_INTEGER
374 && gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
) == 0)
375 return -mpz_sgn (e1
->value
.op
.op2
->value
.integer
);
377 /* Compare P-Q vs. R-S. */
378 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
382 l
= gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op1
);
383 r
= gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op2
);
384 if (l
== 0 && r
== 0)
386 if (l
> -2 && r
== 0)
388 if (l
== 0 && r
> -2)
390 if (l
== 1 && r
== -1)
392 if (l
== -1 && r
== 1)
397 /* Compare A // B vs. C // D. */
399 if (e1
->expr_type
== EXPR_OP
&& e1
->value
.op
.op
== INTRINSIC_CONCAT
400 && e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_CONCAT
)
404 l
= gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op1
);
405 r
= gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op2
);
410 /* Left expressions of // compare equal, but
411 watch out for 'A ' // x vs. 'A' // x. */
412 gfc_expr
*e1_left
= e1
->value
.op
.op1
;
413 gfc_expr
*e2_left
= e2
->value
.op
.op1
;
415 if (e1_left
->expr_type
== EXPR_CONSTANT
416 && e2_left
->expr_type
== EXPR_CONSTANT
417 && e1_left
->value
.character
.length
418 != e2_left
->value
.character
.length
)
424 /* Compare X vs. X-C, for INTEGER only. */
425 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
427 if (e2
->value
.op
.op2
->expr_type
== EXPR_CONSTANT
428 && e2
->value
.op
.op2
->ts
.type
== BT_INTEGER
429 && gfc_dep_compare_expr (e1
, e2
->value
.op
.op1
) == 0)
430 return mpz_sgn (e2
->value
.op
.op2
->value
.integer
);
433 if (e1
->expr_type
!= e2
->expr_type
)
436 switch (e1
->expr_type
)
439 /* Compare strings for equality. */
440 if (e1
->ts
.type
== BT_CHARACTER
&& e2
->ts
.type
== BT_CHARACTER
)
441 return gfc_compare_string (e1
, e2
);
443 /* Compare REAL and COMPLEX constants. Because of the
444 traps and pitfalls associated with comparing
445 a + 1.0 with a + 0.5, check for equality only. */
446 if (e2
->expr_type
== EXPR_CONSTANT
)
448 if (e1
->ts
.type
== BT_REAL
&& e2
->ts
.type
== BT_REAL
)
450 if (mpfr_cmp (e1
->value
.real
, e2
->value
.real
) == 0)
455 else if (e1
->ts
.type
== BT_COMPLEX
&& e2
->ts
.type
== BT_COMPLEX
)
457 if (mpc_cmp (e1
->value
.complex, e2
->value
.complex) == 0)
464 if (e1
->ts
.type
!= BT_INTEGER
|| e2
->ts
.type
!= BT_INTEGER
)
467 /* For INTEGER, all cases where e2 is not constant should have
468 been filtered out above. */
469 gcc_assert (e2
->expr_type
== EXPR_CONSTANT
);
471 i
= mpz_cmp (e1
->value
.integer
, e2
->value
.integer
);
479 if (are_identical_variables (e1
, e2
))
485 /* Intrinsic operators are the same if their operands are the same. */
486 if (e1
->value
.op
.op
!= e2
->value
.op
.op
)
488 if (e1
->value
.op
.op2
== 0)
490 i
= gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op1
);
491 return i
== 0 ? 0 : -2;
493 if (gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op1
) == 0
494 && gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op2
) == 0)
496 else if (e1
->value
.op
.op
== INTRINSIC_TIMES
497 && gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op2
) == 0
498 && gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op1
) == 0)
499 /* Commutativity of multiplication; addition is handled above. */
505 return gfc_dep_compare_functions (e1
, e2
, false);
513 /* Return the difference between two expressions. Integer expressions of
516 X + constant, X - constant and constant + X
518 are handled. Return true on success, false on failure. result is assumed
519 to be uninitialized on entry, and will be initialized on success.
523 gfc_dep_difference (gfc_expr
*e1
, gfc_expr
*e2
, mpz_t
*result
)
525 gfc_expr
*e1_op1
, *e1_op2
, *e2_op1
, *e2_op2
;
527 if (e1
== NULL
|| e2
== NULL
)
530 if (e1
->ts
.type
!= BT_INTEGER
|| e2
->ts
.type
!= BT_INTEGER
)
533 e1
= gfc_discard_nops (e1
);
534 e2
= gfc_discard_nops (e2
);
536 /* Inizialize tentatively, clear if we don't return anything. */
539 /* Case 1: c1 - c2 = c1 - c2, trivially. */
541 if (e1
->expr_type
== EXPR_CONSTANT
&& e2
->expr_type
== EXPR_CONSTANT
)
543 mpz_sub (*result
, e1
->value
.integer
, e2
->value
.integer
);
547 if (e1
->expr_type
== EXPR_OP
&& e1
->value
.op
.op
== INTRINSIC_PLUS
)
549 e1_op1
= gfc_discard_nops (e1
->value
.op
.op1
);
550 e1_op2
= gfc_discard_nops (e1
->value
.op
.op2
);
552 /* Case 2: (X + c1) - X = c1. */
553 if (e1_op2
->expr_type
== EXPR_CONSTANT
554 && gfc_dep_compare_expr (e1_op1
, e2
) == 0)
556 mpz_set (*result
, e1_op2
->value
.integer
);
560 /* Case 3: (c1 + X) - X = c1. */
561 if (e1_op1
->expr_type
== EXPR_CONSTANT
562 && gfc_dep_compare_expr (e1_op2
, e2
) == 0)
564 mpz_set (*result
, e1_op1
->value
.integer
);
568 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_PLUS
)
570 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
571 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
573 if (e1_op2
->expr_type
== EXPR_CONSTANT
)
575 /* Case 4: X + c1 - (X + c2) = c1 - c2. */
576 if (e2_op2
->expr_type
== EXPR_CONSTANT
577 && gfc_dep_compare_expr (e1_op1
, e2_op1
) == 0)
579 mpz_sub (*result
, e1_op2
->value
.integer
,
580 e2_op2
->value
.integer
);
583 /* Case 5: X + c1 - (c2 + X) = c1 - c2. */
584 if (e2_op1
->expr_type
== EXPR_CONSTANT
585 && gfc_dep_compare_expr (e1_op1
, e2_op2
) == 0)
587 mpz_sub (*result
, e1_op2
->value
.integer
,
588 e2_op1
->value
.integer
);
592 else if (e1_op1
->expr_type
== EXPR_CONSTANT
)
594 /* Case 6: c1 + X - (X + c2) = c1 - c2. */
595 if (e2_op2
->expr_type
== EXPR_CONSTANT
596 && gfc_dep_compare_expr (e1_op2
, e2_op1
) == 0)
598 mpz_sub (*result
, e1_op1
->value
.integer
,
599 e2_op2
->value
.integer
);
602 /* Case 7: c1 + X - (c2 + X) = c1 - c2. */
603 if (e2_op1
->expr_type
== EXPR_CONSTANT
604 && gfc_dep_compare_expr (e1_op2
, e2_op2
) == 0)
606 mpz_sub (*result
, e1_op1
->value
.integer
,
607 e2_op1
->value
.integer
);
613 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
615 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
616 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
618 if (e1_op2
->expr_type
== EXPR_CONSTANT
)
620 /* Case 8: X + c1 - (X - c2) = c1 + c2. */
621 if (e2_op2
->expr_type
== EXPR_CONSTANT
622 && gfc_dep_compare_expr (e1_op1
, e2_op1
) == 0)
624 mpz_add (*result
, e1_op2
->value
.integer
,
625 e2_op2
->value
.integer
);
629 if (e1_op1
->expr_type
== EXPR_CONSTANT
)
631 /* Case 9: c1 + X - (X - c2) = c1 + c2. */
632 if (e2_op2
->expr_type
== EXPR_CONSTANT
633 && gfc_dep_compare_expr (e1_op2
, e2_op1
) == 0)
635 mpz_add (*result
, e1_op1
->value
.integer
,
636 e2_op2
->value
.integer
);
643 if (e1
->expr_type
== EXPR_OP
&& e1
->value
.op
.op
== INTRINSIC_MINUS
)
645 e1_op1
= gfc_discard_nops (e1
->value
.op
.op1
);
646 e1_op2
= gfc_discard_nops (e1
->value
.op
.op2
);
648 if (e1_op2
->expr_type
== EXPR_CONSTANT
)
650 /* Case 10: (X - c1) - X = -c1 */
652 if (gfc_dep_compare_expr (e1_op1
, e2
) == 0)
654 mpz_neg (*result
, e1_op2
->value
.integer
);
658 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_PLUS
)
660 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
661 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
663 /* Case 11: (X - c1) - (X + c2) = -( c1 + c2). */
664 if (e2_op2
->expr_type
== EXPR_CONSTANT
665 && gfc_dep_compare_expr (e1_op1
, e2_op1
) == 0)
667 mpz_add (*result
, e1_op2
->value
.integer
,
668 e2_op2
->value
.integer
);
669 mpz_neg (*result
, *result
);
673 /* Case 12: X - c1 - (c2 + X) = - (c1 + c2). */
674 if (e2_op1
->expr_type
== EXPR_CONSTANT
675 && gfc_dep_compare_expr (e1_op1
, e2_op2
) == 0)
677 mpz_add (*result
, e1_op2
->value
.integer
,
678 e2_op1
->value
.integer
);
679 mpz_neg (*result
, *result
);
684 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
686 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
687 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
689 /* Case 13: (X - c1) - (X - c2) = c2 - c1. */
690 if (e2_op2
->expr_type
== EXPR_CONSTANT
691 && gfc_dep_compare_expr (e1_op1
, e2_op1
) == 0)
693 mpz_sub (*result
, e2_op2
->value
.integer
,
694 e1_op2
->value
.integer
);
699 if (e1_op1
->expr_type
== EXPR_CONSTANT
)
701 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
703 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
704 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
706 /* Case 14: (c1 - X) - (c2 - X) == c1 - c2. */
707 if (gfc_dep_compare_expr (e1_op2
, e2_op2
) == 0)
709 mpz_sub (*result
, e1_op1
->value
.integer
,
710 e2_op1
->value
.integer
);
718 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_PLUS
)
720 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
721 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
723 /* Case 15: X - (X + c2) = -c2. */
724 if (e2_op2
->expr_type
== EXPR_CONSTANT
725 && gfc_dep_compare_expr (e1
, e2_op1
) == 0)
727 mpz_neg (*result
, e2_op2
->value
.integer
);
730 /* Case 16: X - (c2 + X) = -c2. */
731 if (e2_op1
->expr_type
== EXPR_CONSTANT
732 && gfc_dep_compare_expr (e1
, e2_op2
) == 0)
734 mpz_neg (*result
, e2_op1
->value
.integer
);
739 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
741 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
742 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
744 /* Case 17: X - (X - c2) = c2. */
745 if (e2_op2
->expr_type
== EXPR_CONSTANT
746 && gfc_dep_compare_expr (e1
, e2_op1
) == 0)
748 mpz_set (*result
, e2_op2
->value
.integer
);
753 if (gfc_dep_compare_expr (e1
, e2
) == 0)
755 /* Case 18: X - X = 0. */
756 mpz_set_si (*result
, 0);
764 /* Returns 1 if the two ranges are the same and 0 if they are not (or if the
765 results are indeterminate). 'n' is the dimension to compare. */
768 is_same_range (gfc_array_ref
*ar1
, gfc_array_ref
*ar2
, int n
)
774 /* TODO: More sophisticated range comparison. */
775 gcc_assert (ar1
&& ar2
);
777 gcc_assert (ar1
->dimen_type
[n
] == ar2
->dimen_type
[n
]);
781 /* Check for mismatching strides. A NULL stride means a stride of 1. */
784 i
= gfc_expr_is_one (e1
, -1);
785 if (i
== -1 || i
== 0)
790 i
= gfc_expr_is_one (e2
, -1);
791 if (i
== -1 || i
== 0)
796 i
= gfc_dep_compare_expr (e1
, e2
);
800 /* The strides match. */
802 /* Check the range start. */
807 /* Use the bound of the array if no bound is specified. */
809 e1
= ar1
->as
->lower
[n
];
812 e2
= ar2
->as
->lower
[n
];
814 /* Check we have values for both. */
818 i
= gfc_dep_compare_expr (e1
, e2
);
823 /* Check the range end. */
828 /* Use the bound of the array if no bound is specified. */
830 e1
= ar1
->as
->upper
[n
];
833 e2
= ar2
->as
->upper
[n
];
835 /* Check we have values for both. */
839 i
= gfc_dep_compare_expr (e1
, e2
);
848 /* Some array-returning intrinsics can be implemented by reusing the
849 data from one of the array arguments. For example, TRANSPOSE does
850 not necessarily need to allocate new data: it can be implemented
851 by copying the original array's descriptor and simply swapping the
852 two dimension specifications.
854 If EXPR is a call to such an intrinsic, return the argument
855 whose data can be reused, otherwise return NULL. */
858 gfc_get_noncopying_intrinsic_argument (gfc_expr
*expr
)
860 if (expr
->expr_type
!= EXPR_FUNCTION
|| !expr
->value
.function
.isym
)
863 switch (expr
->value
.function
.isym
->id
)
865 case GFC_ISYM_TRANSPOSE
:
866 return expr
->value
.function
.actual
->expr
;
874 /* Return true if the result of reference REF can only be constructed
875 using a temporary array. */
878 gfc_ref_needs_temporary_p (gfc_ref
*ref
)
884 for (; ref
; ref
= ref
->next
)
888 /* Vector dimensions are generally not monotonic and must be
889 handled using a temporary. */
890 if (ref
->u
.ar
.type
== AR_SECTION
)
891 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
892 if (ref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
)
899 /* Within an array reference, character substrings generally
900 need a temporary. Character array strides are expressed as
901 multiples of the element size (consistent with other array
902 types), not in characters. */
914 gfc_is_data_pointer (gfc_expr
*e
)
918 if (e
->expr_type
!= EXPR_VARIABLE
&& e
->expr_type
!= EXPR_FUNCTION
)
921 /* No subreference if it is a function */
922 gcc_assert (e
->expr_type
== EXPR_VARIABLE
|| !e
->ref
);
924 if (e
->symtree
->n
.sym
->attr
.pointer
)
927 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
928 if (ref
->type
== REF_COMPONENT
&& ref
->u
.c
.component
->attr
.pointer
)
935 /* Return true if array variable VAR could be passed to the same function
936 as argument EXPR without interfering with EXPR. INTENT is the intent
939 This is considerably less conservative than other dependencies
940 because many function arguments will already be copied into a
944 gfc_check_argument_var_dependency (gfc_expr
*var
, sym_intent intent
,
945 gfc_expr
*expr
, gfc_dep_check elemental
)
949 gcc_assert (var
->expr_type
== EXPR_VARIABLE
);
950 gcc_assert (var
->rank
> 0);
952 switch (expr
->expr_type
)
955 /* In case of elemental subroutines, there is no dependency
956 between two same-range array references. */
957 if (gfc_ref_needs_temporary_p (expr
->ref
)
958 || gfc_check_dependency (var
, expr
, elemental
== NOT_ELEMENTAL
))
960 if (elemental
== ELEM_DONT_CHECK_VARIABLE
)
962 /* Too many false positive with pointers. */
963 if (!gfc_is_data_pointer (var
) && !gfc_is_data_pointer (expr
))
965 /* Elemental procedures forbid unspecified intents,
966 and we don't check dependencies for INTENT_IN args. */
967 gcc_assert (intent
== INTENT_OUT
|| intent
== INTENT_INOUT
);
969 /* We are told not to check dependencies.
970 We do it, however, and issue a warning in case we find one.
971 If a dependency is found in the case
972 elemental == ELEM_CHECK_VARIABLE, we will generate
973 a temporary, so we don't need to bother the user. */
974 gfc_warning (0, "INTENT(%s) actual argument at %L might "
975 "interfere with actual argument at %L.",
976 intent
== INTENT_OUT
? "OUT" : "INOUT",
977 &var
->where
, &expr
->where
);
987 /* the scalarizer always generates a temporary for array constructors,
988 so there is no dependency. */
992 if (intent
!= INTENT_IN
)
994 arg
= gfc_get_noncopying_intrinsic_argument (expr
);
996 return gfc_check_argument_var_dependency (var
, intent
, arg
,
1000 if (elemental
!= NOT_ELEMENTAL
)
1002 if ((expr
->value
.function
.esym
1003 && expr
->value
.function
.esym
->attr
.elemental
)
1004 || (expr
->value
.function
.isym
1005 && expr
->value
.function
.isym
->elemental
))
1006 return gfc_check_fncall_dependency (var
, intent
, NULL
,
1007 expr
->value
.function
.actual
,
1008 ELEM_CHECK_VARIABLE
);
1010 if (gfc_inline_intrinsic_function_p (expr
))
1012 /* The TRANSPOSE case should have been caught in the
1013 noncopying intrinsic case above. */
1014 gcc_assert (expr
->value
.function
.isym
->id
!= GFC_ISYM_TRANSPOSE
);
1016 return gfc_check_fncall_dependency (var
, intent
, NULL
,
1017 expr
->value
.function
.actual
,
1018 ELEM_CHECK_VARIABLE
);
1024 /* In case of non-elemental procedures, there is no need to catch
1025 dependencies, as we will make a temporary anyway. */
1028 /* If the actual arg EXPR is an expression, we need to catch
1029 a dependency between variables in EXPR and VAR,
1030 an intent((IN)OUT) variable. */
1031 if (expr
->value
.op
.op1
1032 && gfc_check_argument_var_dependency (var
, intent
,
1034 ELEM_CHECK_VARIABLE
))
1036 else if (expr
->value
.op
.op2
1037 && gfc_check_argument_var_dependency (var
, intent
,
1039 ELEM_CHECK_VARIABLE
))
1050 /* Like gfc_check_argument_var_dependency, but extended to any
1051 array expression OTHER, not just variables. */
1054 gfc_check_argument_dependency (gfc_expr
*other
, sym_intent intent
,
1055 gfc_expr
*expr
, gfc_dep_check elemental
)
1057 switch (other
->expr_type
)
1060 return gfc_check_argument_var_dependency (other
, intent
, expr
, elemental
);
1063 other
= gfc_get_noncopying_intrinsic_argument (other
);
1065 return gfc_check_argument_dependency (other
, INTENT_IN
, expr
,
1076 /* Like gfc_check_argument_dependency, but check all the arguments in ACTUAL.
1077 FNSYM is the function being called, or NULL if not known. */
1080 gfc_check_fncall_dependency (gfc_expr
*other
, sym_intent intent
,
1081 gfc_symbol
*fnsym
, gfc_actual_arglist
*actual
,
1082 gfc_dep_check elemental
)
1084 gfc_formal_arglist
*formal
;
1087 formal
= fnsym
? gfc_sym_get_dummy_args (fnsym
) : NULL
;
1088 for (; actual
; actual
= actual
->next
, formal
= formal
? formal
->next
: NULL
)
1090 expr
= actual
->expr
;
1092 /* Skip args which are not present. */
1096 /* Skip other itself. */
1100 /* Skip intent(in) arguments if OTHER itself is intent(in). */
1101 if (formal
&& intent
== INTENT_IN
1102 && formal
->sym
->attr
.intent
== INTENT_IN
)
1105 if (gfc_check_argument_dependency (other
, intent
, expr
, elemental
))
1113 /* Return 1 if e1 and e2 are equivalenced arrays, either
1114 directly or indirectly; i.e., equivalence (a,b) for a and b
1115 or equivalence (a,c),(b,c). This function uses the equiv_
1116 lists, generated in trans-common(add_equivalences), that are
1117 guaranteed to pick up indirect equivalences. We explicitly
1118 check for overlap using the offset and length of the equivalence.
1119 This function is symmetric.
1120 TODO: This function only checks whether the full top-level
1121 symbols overlap. An improved implementation could inspect
1122 e1->ref and e2->ref to determine whether the actually accessed
1123 portions of these variables/arrays potentially overlap. */
1126 gfc_are_equivalenced_arrays (gfc_expr
*e1
, gfc_expr
*e2
)
1129 gfc_equiv_info
*s
, *fl1
, *fl2
;
1131 gcc_assert (e1
->expr_type
== EXPR_VARIABLE
1132 && e2
->expr_type
== EXPR_VARIABLE
);
1134 if (!e1
->symtree
->n
.sym
->attr
.in_equivalence
1135 || !e2
->symtree
->n
.sym
->attr
.in_equivalence
|| !e1
->rank
|| !e2
->rank
)
1138 if (e1
->symtree
->n
.sym
->ns
1139 && e1
->symtree
->n
.sym
->ns
!= gfc_current_ns
)
1140 l
= e1
->symtree
->n
.sym
->ns
->equiv_lists
;
1142 l
= gfc_current_ns
->equiv_lists
;
1144 /* Go through the equiv_lists and return 1 if the variables
1145 e1 and e2 are members of the same group and satisfy the
1146 requirement on their relative offsets. */
1147 for (; l
; l
= l
->next
)
1151 for (s
= l
->equiv
; s
; s
= s
->next
)
1153 if (s
->sym
== e1
->symtree
->n
.sym
)
1159 if (s
->sym
== e2
->symtree
->n
.sym
)
1169 /* Can these lengths be zero? */
1170 if (fl1
->length
<= 0 || fl2
->length
<= 0)
1172 /* These can't overlap if [f11,fl1+length] is before
1173 [fl2,fl2+length], or [fl2,fl2+length] is before
1174 [fl1,fl1+length], otherwise they do overlap. */
1175 if (fl1
->offset
+ fl1
->length
> fl2
->offset
1176 && fl2
->offset
+ fl2
->length
> fl1
->offset
)
1184 /* Return true if there is no possibility of aliasing because of a type
1185 mismatch between all the possible pointer references and the
1186 potential target. Note that this function is asymmetric in the
1187 arguments and so must be called twice with the arguments exchanged. */
1190 check_data_pointer_types (gfc_expr
*expr1
, gfc_expr
*expr2
)
1196 bool seen_component_ref
;
1198 if (expr1
->expr_type
!= EXPR_VARIABLE
1199 || expr2
->expr_type
!= EXPR_VARIABLE
)
1202 sym1
= expr1
->symtree
->n
.sym
;
1203 sym2
= expr2
->symtree
->n
.sym
;
1205 /* Keep it simple for now. */
1206 if (sym1
->ts
.type
== BT_DERIVED
&& sym2
->ts
.type
== BT_DERIVED
)
1209 if (sym1
->attr
.pointer
)
1211 if (gfc_compare_types (&sym1
->ts
, &sym2
->ts
))
1215 /* This is a conservative check on the components of the derived type
1216 if no component references have been seen. Since we will not dig
1217 into the components of derived type components, we play it safe by
1218 returning false. First we check the reference chain and then, if
1219 no component references have been seen, the components. */
1220 seen_component_ref
= false;
1221 if (sym1
->ts
.type
== BT_DERIVED
)
1223 for (ref1
= expr1
->ref
; ref1
; ref1
= ref1
->next
)
1225 if (ref1
->type
!= REF_COMPONENT
)
1228 if (ref1
->u
.c
.component
->ts
.type
== BT_DERIVED
)
1231 if ((sym2
->attr
.pointer
|| ref1
->u
.c
.component
->attr
.pointer
)
1232 && gfc_compare_types (&ref1
->u
.c
.component
->ts
, &sym2
->ts
))
1235 seen_component_ref
= true;
1239 if (sym1
->ts
.type
== BT_DERIVED
&& !seen_component_ref
)
1241 for (cm1
= sym1
->ts
.u
.derived
->components
; cm1
; cm1
= cm1
->next
)
1243 if (cm1
->ts
.type
== BT_DERIVED
)
1246 if ((sym2
->attr
.pointer
|| cm1
->attr
.pointer
)
1247 && gfc_compare_types (&cm1
->ts
, &sym2
->ts
))
1256 /* Return true if the statement body redefines the condition. Returns
1257 true if expr2 depends on expr1. expr1 should be a single term
1258 suitable for the lhs of an assignment. The IDENTICAL flag indicates
1259 whether array references to the same symbol with identical range
1260 references count as a dependency or not. Used for forall and where
1261 statements. Also used with functions returning arrays without a
1265 gfc_check_dependency (gfc_expr
*expr1
, gfc_expr
*expr2
, bool identical
)
1267 gfc_actual_arglist
*actual
;
1271 /* -fcoarray=lib can end up here with expr1->expr_type set to EXPR_FUNCTION
1272 and a reference to _F.caf_get, so skip the assert. */
1273 if (expr1
->expr_type
== EXPR_FUNCTION
1274 && strcmp (expr1
->value
.function
.name
, "_F.caf_get") == 0)
1277 if (expr1
->expr_type
!= EXPR_VARIABLE
)
1278 gfc_internal_error ("gfc_check_dependency: expecting an EXPR_VARIABLE");
1280 switch (expr2
->expr_type
)
1283 n
= gfc_check_dependency (expr1
, expr2
->value
.op
.op1
, identical
);
1286 if (expr2
->value
.op
.op2
)
1287 return gfc_check_dependency (expr1
, expr2
->value
.op
.op2
, identical
);
1291 /* The interesting cases are when the symbols don't match. */
1292 if (expr1
->symtree
->n
.sym
!= expr2
->symtree
->n
.sym
)
1294 symbol_attribute attr1
, attr2
;
1295 gfc_typespec
*ts1
= &expr1
->symtree
->n
.sym
->ts
;
1296 gfc_typespec
*ts2
= &expr2
->symtree
->n
.sym
->ts
;
1298 /* Return 1 if expr1 and expr2 are equivalenced arrays. */
1299 if (gfc_are_equivalenced_arrays (expr1
, expr2
))
1302 /* Symbols can only alias if they have the same type. */
1303 if (ts1
->type
!= BT_UNKNOWN
&& ts2
->type
!= BT_UNKNOWN
1304 && ts1
->type
!= BT_DERIVED
&& ts2
->type
!= BT_DERIVED
)
1306 if (ts1
->type
!= ts2
->type
|| ts1
->kind
!= ts2
->kind
)
1310 /* We have to also include target-target as ptr%comp is not a
1311 pointer but it still alias with "dt%comp" for "ptr => dt". As
1312 subcomponents and array access to pointers retains the target
1313 attribute, that's sufficient. */
1314 attr1
= gfc_expr_attr (expr1
);
1315 attr2
= gfc_expr_attr (expr2
);
1316 if ((attr1
.pointer
|| attr1
.target
) && (attr2
.pointer
|| attr2
.target
))
1318 if (check_data_pointer_types (expr1
, expr2
)
1319 && check_data_pointer_types (expr2
, expr1
))
1326 gfc_symbol
*sym1
= expr1
->symtree
->n
.sym
;
1327 gfc_symbol
*sym2
= expr2
->symtree
->n
.sym
;
1328 if (sym1
->attr
.target
&& sym2
->attr
.target
1329 && ((sym1
->attr
.dummy
&& !sym1
->attr
.contiguous
1330 && (!sym1
->attr
.dimension
1331 || sym2
->as
->type
== AS_ASSUMED_SHAPE
))
1332 || (sym2
->attr
.dummy
&& !sym2
->attr
.contiguous
1333 && (!sym2
->attr
.dimension
1334 || sym2
->as
->type
== AS_ASSUMED_SHAPE
))))
1338 /* Otherwise distinct symbols have no dependencies. */
1345 /* Identical and disjoint ranges return 0,
1346 overlapping ranges return 1. */
1347 if (expr1
->ref
&& expr2
->ref
)
1348 return gfc_dep_resolver (expr1
->ref
, expr2
->ref
, NULL
);
1353 if (gfc_get_noncopying_intrinsic_argument (expr2
) != NULL
)
1356 /* Remember possible differences between elemental and
1357 transformational functions. All functions inside a FORALL
1359 for (actual
= expr2
->value
.function
.actual
;
1360 actual
; actual
= actual
->next
)
1364 n
= gfc_check_dependency (expr1
, actual
->expr
, identical
);
1375 /* Loop through the array constructor's elements. */
1376 for (c
= gfc_constructor_first (expr2
->value
.constructor
);
1377 c
; c
= gfc_constructor_next (c
))
1379 /* If this is an iterator, assume the worst. */
1382 /* Avoid recursion in the common case. */
1383 if (c
->expr
->expr_type
== EXPR_CONSTANT
)
1385 if (gfc_check_dependency (expr1
, c
->expr
, 1))
1396 /* Determines overlapping for two array sections. */
1398 static gfc_dependency
1399 check_section_vs_section (gfc_array_ref
*l_ar
, gfc_array_ref
*r_ar
, int n
)
1415 int stride_comparison
;
1416 int start_comparison
;
1419 /* If they are the same range, return without more ado. */
1420 if (is_same_range (l_ar
, r_ar
, n
))
1421 return GFC_DEP_EQUAL
;
1423 l_start
= l_ar
->start
[n
];
1424 l_end
= l_ar
->end
[n
];
1425 l_stride
= l_ar
->stride
[n
];
1427 r_start
= r_ar
->start
[n
];
1428 r_end
= r_ar
->end
[n
];
1429 r_stride
= r_ar
->stride
[n
];
1431 /* If l_start is NULL take it from array specifier. */
1432 if (NULL
== l_start
&& IS_ARRAY_EXPLICIT (l_ar
->as
))
1433 l_start
= l_ar
->as
->lower
[n
];
1434 /* If l_end is NULL take it from array specifier. */
1435 if (NULL
== l_end
&& IS_ARRAY_EXPLICIT (l_ar
->as
))
1436 l_end
= l_ar
->as
->upper
[n
];
1438 /* If r_start is NULL take it from array specifier. */
1439 if (NULL
== r_start
&& IS_ARRAY_EXPLICIT (r_ar
->as
))
1440 r_start
= r_ar
->as
->lower
[n
];
1441 /* If r_end is NULL take it from array specifier. */
1442 if (NULL
== r_end
&& IS_ARRAY_EXPLICIT (r_ar
->as
))
1443 r_end
= r_ar
->as
->upper
[n
];
1445 /* Determine whether the l_stride is positive or negative. */
1448 else if (l_stride
->expr_type
== EXPR_CONSTANT
1449 && l_stride
->ts
.type
== BT_INTEGER
)
1450 l_dir
= mpz_sgn (l_stride
->value
.integer
);
1451 else if (l_start
&& l_end
)
1452 l_dir
= gfc_dep_compare_expr (l_end
, l_start
);
1456 /* Determine whether the r_stride is positive or negative. */
1459 else if (r_stride
->expr_type
== EXPR_CONSTANT
1460 && r_stride
->ts
.type
== BT_INTEGER
)
1461 r_dir
= mpz_sgn (r_stride
->value
.integer
);
1462 else if (r_start
&& r_end
)
1463 r_dir
= gfc_dep_compare_expr (r_end
, r_start
);
1467 /* The strides should never be zero. */
1468 if (l_dir
== 0 || r_dir
== 0)
1469 return GFC_DEP_OVERLAP
;
1471 /* Determine the relationship between the strides. Set stride_comparison to
1472 -2 if the dependency cannot be determined
1473 -1 if l_stride < r_stride
1474 0 if l_stride == r_stride
1475 1 if l_stride > r_stride
1476 as determined by gfc_dep_compare_expr. */
1478 one_expr
= gfc_get_int_expr (gfc_index_integer_kind
, NULL
, 1);
1480 stride_comparison
= gfc_dep_compare_expr (l_stride
? l_stride
: one_expr
,
1481 r_stride
? r_stride
: one_expr
);
1483 if (l_start
&& r_start
)
1484 start_comparison
= gfc_dep_compare_expr (l_start
, r_start
);
1486 start_comparison
= -2;
1488 gfc_free_expr (one_expr
);
1490 /* Determine LHS upper and lower bounds. */
1496 else if (l_dir
== -1)
1507 /* Determine RHS upper and lower bounds. */
1513 else if (r_dir
== -1)
1524 /* Check whether the ranges are disjoint. */
1525 if (l_upper
&& r_lower
&& gfc_dep_compare_expr (l_upper
, r_lower
) == -1)
1526 return GFC_DEP_NODEP
;
1527 if (r_upper
&& l_lower
&& gfc_dep_compare_expr (r_upper
, l_lower
) == -1)
1528 return GFC_DEP_NODEP
;
1530 /* Handle cases like x:y:1 vs. x:z:-1 as GFC_DEP_EQUAL. */
1531 if (l_start
&& r_start
&& gfc_dep_compare_expr (l_start
, r_start
) == 0)
1533 if (l_dir
== 1 && r_dir
== -1)
1534 return GFC_DEP_EQUAL
;
1535 if (l_dir
== -1 && r_dir
== 1)
1536 return GFC_DEP_EQUAL
;
1539 /* Handle cases like x:y:1 vs. z:y:-1 as GFC_DEP_EQUAL. */
1540 if (l_end
&& r_end
&& gfc_dep_compare_expr (l_end
, r_end
) == 0)
1542 if (l_dir
== 1 && r_dir
== -1)
1543 return GFC_DEP_EQUAL
;
1544 if (l_dir
== -1 && r_dir
== 1)
1545 return GFC_DEP_EQUAL
;
1548 /* Handle cases like x:y:2 vs. x+1:z:4 as GFC_DEP_NODEP.
1549 There is no dependency if the remainder of
1550 (l_start - r_start) / gcd(l_stride, r_stride) is
1553 - Cases like a(1:4:2) = a(2:3) are still not handled.
1556 #define IS_CONSTANT_INTEGER(a) ((a) && ((a)->expr_type == EXPR_CONSTANT) \
1557 && (a)->ts.type == BT_INTEGER)
1559 if (IS_CONSTANT_INTEGER (l_stride
) && IS_CONSTANT_INTEGER (r_stride
)
1560 && gfc_dep_difference (l_start
, r_start
, &tmp
))
1566 mpz_gcd (gcd
, l_stride
->value
.integer
, r_stride
->value
.integer
);
1568 mpz_fdiv_r (tmp
, tmp
, gcd
);
1569 result
= mpz_cmp_si (tmp
, 0L);
1575 return GFC_DEP_NODEP
;
1578 #undef IS_CONSTANT_INTEGER
1580 /* Check for forward dependencies x:y vs. x+1:z and x:y:z vs. x:y:z+1. */
1582 if (l_dir
== 1 && r_dir
== 1 &&
1583 (start_comparison
== 0 || start_comparison
== -1)
1584 && (stride_comparison
== 0 || stride_comparison
== -1))
1585 return GFC_DEP_FORWARD
;
1587 /* Check for forward dependencies x:y:-1 vs. x-1:z:-1 and
1588 x:y:-1 vs. x:y:-2. */
1589 if (l_dir
== -1 && r_dir
== -1 &&
1590 (start_comparison
== 0 || start_comparison
== 1)
1591 && (stride_comparison
== 0 || stride_comparison
== 1))
1592 return GFC_DEP_FORWARD
;
1594 if (stride_comparison
== 0 || stride_comparison
== -1)
1596 if (l_start
&& IS_ARRAY_EXPLICIT (l_ar
->as
))
1599 /* Check for a(low:y:s) vs. a(z:x:s) or
1600 a(low:y:s) vs. a(z:x:s+1) where a has a lower bound
1601 of low, which is always at least a forward dependence. */
1604 && gfc_dep_compare_expr (l_start
, l_ar
->as
->lower
[n
]) == 0)
1605 return GFC_DEP_FORWARD
;
1609 if (stride_comparison
== 0 || stride_comparison
== 1)
1611 if (l_start
&& IS_ARRAY_EXPLICIT (l_ar
->as
))
1614 /* Check for a(high:y:-s) vs. a(z:x:-s) or
1615 a(high:y:-s vs. a(z:x:-s-1) where a has a higher bound
1616 of high, which is always at least a forward dependence. */
1619 && gfc_dep_compare_expr (l_start
, l_ar
->as
->upper
[n
]) == 0)
1620 return GFC_DEP_FORWARD
;
1625 if (stride_comparison
== 0)
1627 /* From here, check for backwards dependencies. */
1628 /* x+1:y vs. x:z. */
1629 if (l_dir
== 1 && r_dir
== 1 && start_comparison
== 1)
1630 return GFC_DEP_BACKWARD
;
1632 /* x-1:y:-1 vs. x:z:-1. */
1633 if (l_dir
== -1 && r_dir
== -1 && start_comparison
== -1)
1634 return GFC_DEP_BACKWARD
;
1637 return GFC_DEP_OVERLAP
;
1641 /* Determines overlapping for a single element and a section. */
1643 static gfc_dependency
1644 gfc_check_element_vs_section( gfc_ref
*lref
, gfc_ref
*rref
, int n
)
1653 elem
= lref
->u
.ar
.start
[n
];
1655 return GFC_DEP_OVERLAP
;
1658 start
= ref
->start
[n
] ;
1660 stride
= ref
->stride
[n
];
1662 if (!start
&& IS_ARRAY_EXPLICIT (ref
->as
))
1663 start
= ref
->as
->lower
[n
];
1664 if (!end
&& IS_ARRAY_EXPLICIT (ref
->as
))
1665 end
= ref
->as
->upper
[n
];
1667 /* Determine whether the stride is positive or negative. */
1670 else if (stride
->expr_type
== EXPR_CONSTANT
1671 && stride
->ts
.type
== BT_INTEGER
)
1672 s
= mpz_sgn (stride
->value
.integer
);
1676 /* Stride should never be zero. */
1678 return GFC_DEP_OVERLAP
;
1680 /* Positive strides. */
1683 /* Check for elem < lower. */
1684 if (start
&& gfc_dep_compare_expr (elem
, start
) == -1)
1685 return GFC_DEP_NODEP
;
1686 /* Check for elem > upper. */
1687 if (end
&& gfc_dep_compare_expr (elem
, end
) == 1)
1688 return GFC_DEP_NODEP
;
1692 s
= gfc_dep_compare_expr (start
, end
);
1693 /* Check for an empty range. */
1695 return GFC_DEP_NODEP
;
1696 if (s
== 0 && gfc_dep_compare_expr (elem
, start
) == 0)
1697 return GFC_DEP_EQUAL
;
1700 /* Negative strides. */
1703 /* Check for elem > upper. */
1704 if (end
&& gfc_dep_compare_expr (elem
, start
) == 1)
1705 return GFC_DEP_NODEP
;
1706 /* Check for elem < lower. */
1707 if (start
&& gfc_dep_compare_expr (elem
, end
) == -1)
1708 return GFC_DEP_NODEP
;
1712 s
= gfc_dep_compare_expr (start
, end
);
1713 /* Check for an empty range. */
1715 return GFC_DEP_NODEP
;
1716 if (s
== 0 && gfc_dep_compare_expr (elem
, start
) == 0)
1717 return GFC_DEP_EQUAL
;
1720 /* Unknown strides. */
1724 return GFC_DEP_OVERLAP
;
1725 s
= gfc_dep_compare_expr (start
, end
);
1727 return GFC_DEP_OVERLAP
;
1728 /* Assume positive stride. */
1731 /* Check for elem < lower. */
1732 if (gfc_dep_compare_expr (elem
, start
) == -1)
1733 return GFC_DEP_NODEP
;
1734 /* Check for elem > upper. */
1735 if (gfc_dep_compare_expr (elem
, end
) == 1)
1736 return GFC_DEP_NODEP
;
1738 /* Assume negative stride. */
1741 /* Check for elem > upper. */
1742 if (gfc_dep_compare_expr (elem
, start
) == 1)
1743 return GFC_DEP_NODEP
;
1744 /* Check for elem < lower. */
1745 if (gfc_dep_compare_expr (elem
, end
) == -1)
1746 return GFC_DEP_NODEP
;
1751 s
= gfc_dep_compare_expr (elem
, start
);
1753 return GFC_DEP_EQUAL
;
1754 if (s
== 1 || s
== -1)
1755 return GFC_DEP_NODEP
;
1759 return GFC_DEP_OVERLAP
;
1763 /* Traverse expr, checking all EXPR_VARIABLE symbols for their
1764 forall_index attribute. Return true if any variable may be
1765 being used as a FORALL index. Its safe to pessimistically
1766 return true, and assume a dependency. */
1769 contains_forall_index_p (gfc_expr
*expr
)
1771 gfc_actual_arglist
*arg
;
1779 switch (expr
->expr_type
)
1782 if (expr
->symtree
->n
.sym
->forall_index
)
1787 if (contains_forall_index_p (expr
->value
.op
.op1
)
1788 || contains_forall_index_p (expr
->value
.op
.op2
))
1793 for (arg
= expr
->value
.function
.actual
; arg
; arg
= arg
->next
)
1794 if (contains_forall_index_p (arg
->expr
))
1800 case EXPR_SUBSTRING
:
1803 case EXPR_STRUCTURE
:
1805 for (c
= gfc_constructor_first (expr
->value
.constructor
);
1806 c
; gfc_constructor_next (c
))
1807 if (contains_forall_index_p (c
->expr
))
1815 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
1819 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
1820 if (contains_forall_index_p (ref
->u
.ar
.start
[i
])
1821 || contains_forall_index_p (ref
->u
.ar
.end
[i
])
1822 || contains_forall_index_p (ref
->u
.ar
.stride
[i
]))
1830 if (contains_forall_index_p (ref
->u
.ss
.start
)
1831 || contains_forall_index_p (ref
->u
.ss
.end
))
1842 /* Determines overlapping for two single element array references. */
1844 static gfc_dependency
1845 gfc_check_element_vs_element (gfc_ref
*lref
, gfc_ref
*rref
, int n
)
1855 l_start
= l_ar
.start
[n
] ;
1856 r_start
= r_ar
.start
[n
] ;
1857 i
= gfc_dep_compare_expr (r_start
, l_start
);
1859 return GFC_DEP_EQUAL
;
1861 /* Treat two scalar variables as potentially equal. This allows
1862 us to prove that a(i,:) and a(j,:) have no dependency. See
1863 Gerald Roth, "Evaluation of Array Syntax Dependence Analysis",
1864 Proceedings of the International Conference on Parallel and
1865 Distributed Processing Techniques and Applications (PDPTA2001),
1866 Las Vegas, Nevada, June 2001. */
1867 /* However, we need to be careful when either scalar expression
1868 contains a FORALL index, as these can potentially change value
1869 during the scalarization/traversal of this array reference. */
1870 if (contains_forall_index_p (r_start
) || contains_forall_index_p (l_start
))
1871 return GFC_DEP_OVERLAP
;
1874 return GFC_DEP_NODEP
;
1875 return GFC_DEP_EQUAL
;
1878 /* Callback function for checking if an expression depends on a
1879 dummy variable which is any other than INTENT(IN). */
1882 callback_dummy_intent_not_in (gfc_expr
**ep
,
1883 int *walk_subtrees ATTRIBUTE_UNUSED
,
1884 void *data ATTRIBUTE_UNUSED
)
1888 if (e
->expr_type
== EXPR_VARIABLE
&& e
->symtree
1889 && e
->symtree
->n
.sym
->attr
.dummy
)
1890 return e
->symtree
->n
.sym
->attr
.intent
!= INTENT_IN
;
1895 /* Auxiliary function to check if subexpressions have dummy variables which
1900 dummy_intent_not_in (gfc_expr
**ep
)
1902 return gfc_expr_walker (ep
, callback_dummy_intent_not_in
, NULL
);
1905 /* Determine if an array ref, usually an array section specifies the
1906 entire array. In addition, if the second, pointer argument is
1907 provided, the function will return true if the reference is
1908 contiguous; eg. (:, 1) gives true but (1,:) gives false.
1909 If one of the bounds depends on a dummy variable which is
1910 not INTENT(IN), also return false, because the user may
1911 have changed the variable. */
1914 gfc_full_array_ref_p (gfc_ref
*ref
, bool *contiguous
)
1918 bool lbound_OK
= true;
1919 bool ubound_OK
= true;
1922 *contiguous
= false;
1924 if (ref
->type
!= REF_ARRAY
)
1927 if (ref
->u
.ar
.type
== AR_FULL
)
1934 if (ref
->u
.ar
.type
!= AR_SECTION
)
1939 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
1941 /* If we have a single element in the reference, for the reference
1942 to be full, we need to ascertain that the array has a single
1943 element in this dimension and that we actually reference the
1945 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_ELEMENT
)
1947 /* This is unconditionally a contiguous reference if all the
1948 remaining dimensions are elements. */
1952 for (n
= i
+ 1; n
< ref
->u
.ar
.dimen
; n
++)
1953 if (ref
->u
.ar
.dimen_type
[n
] != DIMEN_ELEMENT
)
1954 *contiguous
= false;
1958 || !ref
->u
.ar
.as
->lower
[i
]
1959 || !ref
->u
.ar
.as
->upper
[i
]
1960 || gfc_dep_compare_expr (ref
->u
.ar
.as
->lower
[i
],
1961 ref
->u
.ar
.as
->upper
[i
])
1962 || !ref
->u
.ar
.start
[i
]
1963 || gfc_dep_compare_expr (ref
->u
.ar
.start
[i
],
1964 ref
->u
.ar
.as
->lower
[i
]))
1970 /* Check the lower bound. */
1971 if (ref
->u
.ar
.start
[i
]
1973 || !ref
->u
.ar
.as
->lower
[i
]
1974 || gfc_dep_compare_expr (ref
->u
.ar
.start
[i
],
1975 ref
->u
.ar
.as
->lower
[i
])
1976 || dummy_intent_not_in (&ref
->u
.ar
.start
[i
])))
1978 /* Check the upper bound. */
1979 if (ref
->u
.ar
.end
[i
]
1981 || !ref
->u
.ar
.as
->upper
[i
]
1982 || gfc_dep_compare_expr (ref
->u
.ar
.end
[i
],
1983 ref
->u
.ar
.as
->upper
[i
])
1984 || dummy_intent_not_in (&ref
->u
.ar
.end
[i
])))
1986 /* Check the stride. */
1987 if (ref
->u
.ar
.stride
[i
]
1988 && !gfc_expr_is_one (ref
->u
.ar
.stride
[i
], 0))
1991 /* This is unconditionally a contiguous reference as long as all
1992 the subsequent dimensions are elements. */
1996 for (n
= i
+ 1; n
< ref
->u
.ar
.dimen
; n
++)
1997 if (ref
->u
.ar
.dimen_type
[n
] != DIMEN_ELEMENT
)
1998 *contiguous
= false;
2001 if (!lbound_OK
|| !ubound_OK
)
2008 /* Determine if a full array is the same as an array section with one
2009 variable limit. For this to be so, the strides must both be unity
2010 and one of either start == lower or end == upper must be true. */
2013 ref_same_as_full_array (gfc_ref
*full_ref
, gfc_ref
*ref
)
2016 bool upper_or_lower
;
2018 if (full_ref
->type
!= REF_ARRAY
)
2020 if (full_ref
->u
.ar
.type
!= AR_FULL
)
2022 if (ref
->type
!= REF_ARRAY
)
2024 if (ref
->u
.ar
.type
!= AR_SECTION
)
2027 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
2029 /* If we have a single element in the reference, we need to check
2030 that the array has a single element and that we actually reference
2031 the correct element. */
2032 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_ELEMENT
)
2034 if (!full_ref
->u
.ar
.as
2035 || !full_ref
->u
.ar
.as
->lower
[i
]
2036 || !full_ref
->u
.ar
.as
->upper
[i
]
2037 || gfc_dep_compare_expr (full_ref
->u
.ar
.as
->lower
[i
],
2038 full_ref
->u
.ar
.as
->upper
[i
])
2039 || !ref
->u
.ar
.start
[i
]
2040 || gfc_dep_compare_expr (ref
->u
.ar
.start
[i
],
2041 full_ref
->u
.ar
.as
->lower
[i
]))
2045 /* Check the strides. */
2046 if (full_ref
->u
.ar
.stride
[i
] && !gfc_expr_is_one (full_ref
->u
.ar
.stride
[i
], 0))
2048 if (ref
->u
.ar
.stride
[i
] && !gfc_expr_is_one (ref
->u
.ar
.stride
[i
], 0))
2051 upper_or_lower
= false;
2052 /* Check the lower bound. */
2053 if (ref
->u
.ar
.start
[i
]
2055 && full_ref
->u
.ar
.as
->lower
[i
]
2056 && gfc_dep_compare_expr (ref
->u
.ar
.start
[i
],
2057 full_ref
->u
.ar
.as
->lower
[i
]) == 0))
2058 upper_or_lower
= true;
2059 /* Check the upper bound. */
2060 if (ref
->u
.ar
.end
[i
]
2062 && full_ref
->u
.ar
.as
->upper
[i
]
2063 && gfc_dep_compare_expr (ref
->u
.ar
.end
[i
],
2064 full_ref
->u
.ar
.as
->upper
[i
]) == 0))
2065 upper_or_lower
= true;
2066 if (!upper_or_lower
)
2073 /* Finds if two array references are overlapping or not.
2075 2 : array references are overlapping but reversal of one or
2076 more dimensions will clear the dependency.
2077 1 : array references are overlapping.
2078 0 : array references are identical or not overlapping. */
2081 gfc_dep_resolver (gfc_ref
*lref
, gfc_ref
*rref
, gfc_reverse
*reverse
)
2085 gfc_dependency fin_dep
;
2086 gfc_dependency this_dep
;
2088 this_dep
= GFC_DEP_ERROR
;
2089 fin_dep
= GFC_DEP_ERROR
;
2090 /* Dependencies due to pointers should already have been identified.
2091 We only need to check for overlapping array references. */
2093 while (lref
&& rref
)
2095 /* We're resolving from the same base symbol, so both refs should be
2096 the same type. We traverse the reference chain until we find ranges
2097 that are not equal. */
2098 gcc_assert (lref
->type
== rref
->type
);
2102 /* The two ranges can't overlap if they are from different
2104 if (lref
->u
.c
.component
!= rref
->u
.c
.component
)
2109 /* Substring overlaps are handled by the string assignment code
2110 if there is not an underlying dependency. */
2111 return (fin_dep
== GFC_DEP_OVERLAP
) ? 1 : 0;
2115 if (ref_same_as_full_array (lref
, rref
))
2118 if (ref_same_as_full_array (rref
, lref
))
2121 if (lref
->u
.ar
.dimen
!= rref
->u
.ar
.dimen
)
2123 if (lref
->u
.ar
.type
== AR_FULL
)
2124 fin_dep
= gfc_full_array_ref_p (rref
, NULL
) ? GFC_DEP_EQUAL
2126 else if (rref
->u
.ar
.type
== AR_FULL
)
2127 fin_dep
= gfc_full_array_ref_p (lref
, NULL
) ? GFC_DEP_EQUAL
2134 /* Index for the reverse array. */
2136 for (n
=0; n
< lref
->u
.ar
.dimen
; n
++)
2138 /* Handle dependency when either of array reference is vector
2139 subscript. There is no dependency if the vector indices
2140 are equal or if indices are known to be different in a
2141 different dimension. */
2142 if (lref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
2143 || rref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
)
2145 if (lref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
2146 && rref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
2147 && gfc_dep_compare_expr (lref
->u
.ar
.start
[n
],
2148 rref
->u
.ar
.start
[n
]) == 0)
2149 this_dep
= GFC_DEP_EQUAL
;
2151 this_dep
= GFC_DEP_OVERLAP
;
2153 goto update_fin_dep
;
2156 if (lref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
2157 && rref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
)
2158 this_dep
= check_section_vs_section (&lref
->u
.ar
, &rref
->u
.ar
, n
);
2159 else if (lref
->u
.ar
.dimen_type
[n
] == DIMEN_ELEMENT
2160 && rref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
)
2161 this_dep
= gfc_check_element_vs_section (lref
, rref
, n
);
2162 else if (rref
->u
.ar
.dimen_type
[n
] == DIMEN_ELEMENT
2163 && lref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
)
2164 this_dep
= gfc_check_element_vs_section (rref
, lref
, n
);
2167 gcc_assert (rref
->u
.ar
.dimen_type
[n
] == DIMEN_ELEMENT
2168 && lref
->u
.ar
.dimen_type
[n
] == DIMEN_ELEMENT
);
2169 this_dep
= gfc_check_element_vs_element (rref
, lref
, n
);
2172 /* If any dimension doesn't overlap, we have no dependency. */
2173 if (this_dep
== GFC_DEP_NODEP
)
2176 /* Now deal with the loop reversal logic: This only works on
2177 ranges and is activated by setting
2178 reverse[n] == GFC_ENABLE_REVERSE
2179 The ability to reverse or not is set by previous conditions
2180 in this dimension. If reversal is not activated, the
2181 value GFC_DEP_BACKWARD is reset to GFC_DEP_OVERLAP. */
2183 /* Get the indexing right for the scalarizing loop. If this
2184 is an element, there is no corresponding loop. */
2185 if (lref
->u
.ar
.dimen_type
[n
] != DIMEN_ELEMENT
)
2188 if (rref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
2189 && lref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
)
2191 /* Set reverse if backward dependence and not inhibited. */
2192 if (reverse
&& reverse
[m
] == GFC_ENABLE_REVERSE
)
2193 reverse
[m
] = (this_dep
== GFC_DEP_BACKWARD
) ?
2194 GFC_REVERSE_SET
: reverse
[m
];
2196 /* Set forward if forward dependence and not inhibited. */
2197 if (reverse
&& reverse
[m
] == GFC_ENABLE_REVERSE
)
2198 reverse
[m
] = (this_dep
== GFC_DEP_FORWARD
) ?
2199 GFC_FORWARD_SET
: reverse
[m
];
2201 /* Flag up overlap if dependence not compatible with
2202 the overall state of the expression. */
2203 if (reverse
&& reverse
[m
] == GFC_REVERSE_SET
2204 && this_dep
== GFC_DEP_FORWARD
)
2206 reverse
[m
] = GFC_INHIBIT_REVERSE
;
2207 this_dep
= GFC_DEP_OVERLAP
;
2209 else if (reverse
&& reverse
[m
] == GFC_FORWARD_SET
2210 && this_dep
== GFC_DEP_BACKWARD
)
2212 reverse
[m
] = GFC_INHIBIT_REVERSE
;
2213 this_dep
= GFC_DEP_OVERLAP
;
2216 /* If no intention of reversing or reversing is explicitly
2217 inhibited, convert backward dependence to overlap. */
2218 if ((reverse
== NULL
&& this_dep
== GFC_DEP_BACKWARD
)
2219 || (reverse
!= NULL
&& reverse
[m
] == GFC_INHIBIT_REVERSE
))
2220 this_dep
= GFC_DEP_OVERLAP
;
2223 /* Overlap codes are in order of priority. We only need to
2224 know the worst one.*/
2227 if (this_dep
> fin_dep
)
2231 /* If this is an equal element, we have to keep going until we find
2232 the "real" array reference. */
2233 if (lref
->u
.ar
.type
== AR_ELEMENT
2234 && rref
->u
.ar
.type
== AR_ELEMENT
2235 && fin_dep
== GFC_DEP_EQUAL
)
2238 /* Exactly matching and forward overlapping ranges don't cause a
2240 if (fin_dep
< GFC_DEP_BACKWARD
)
2243 /* Keep checking. We only have a dependency if
2244 subsequent references also overlap. */
2254 /* If we haven't seen any array refs then something went wrong. */
2255 gcc_assert (fin_dep
!= GFC_DEP_ERROR
);
2257 /* Assume the worst if we nest to different depths. */
2261 return fin_dep
== GFC_DEP_OVERLAP
;