2 Copyright (C) 2000, 2001, 2002, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Paul Brook <paul@nowt.org>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* dependency.c -- Expression dependency analysis code. */
23 /* There's probably quite a bit of duplication in this file. We currently
24 have different dependency checking functions for different types
25 if dependencies. Ideally these would probably be merged. */
29 #include "dependency.h"
31 /* static declarations */
33 enum range {LHS, RHS, MID};
35 /* Dependency types. These must be in reverse order of priority. */
39 GFC_DEP_EQUAL, /* Identical Ranges. */
40 GFC_DEP_FORWARD, /* e.g., a(1:3), a(2:4). */
41 GFC_DEP_OVERLAP, /* May overlap in some other way. */
42 GFC_DEP_NODEP /* Distinct ranges. */
47 #define IS_ARRAY_EXPLICIT(as) ((as->type == AS_EXPLICIT ? 1 : 0))
50 /* Returns 1 if the expr is an integer constant value 1, 0 if it is not or
51 def if the value could not be determined. */
54 gfc_expr_is_one (gfc_expr *expr, int def)
56 gcc_assert (expr != NULL);
58 if (expr->expr_type != EXPR_CONSTANT)
61 if (expr->ts.type != BT_INTEGER)
64 return mpz_cmp_si (expr->value.integer, 1) == 0;
68 /* Compare two values. Returns 0 if e1 == e2, -1 if e1 < e2, +1 if e1 > e2,
69 and -2 if the relationship could not be determined. */
72 gfc_dep_compare_expr (gfc_expr *e1, gfc_expr *e2)
74 gfc_actual_arglist *args1;
75 gfc_actual_arglist *args2;
78 if (e1->expr_type == EXPR_OP
79 && (e1->value.op.op == INTRINSIC_UPLUS
80 || e1->value.op.op == INTRINSIC_PARENTHESES))
81 return gfc_dep_compare_expr (e1->value.op.op1, e2);
82 if (e2->expr_type == EXPR_OP
83 && (e2->value.op.op == INTRINSIC_UPLUS
84 || e2->value.op.op == INTRINSIC_PARENTHESES))
85 return gfc_dep_compare_expr (e1, e2->value.op.op1);
87 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_PLUS)
89 /* Compare X+C vs. X. */
90 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
91 && e1->value.op.op2->ts.type == BT_INTEGER
92 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
93 return mpz_sgn (e1->value.op.op2->value.integer);
95 /* Compare P+Q vs. R+S. */
96 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
100 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
101 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
102 if (l == 0 && r == 0)
104 if (l == 0 && r != -2)
106 if (l != -2 && r == 0)
108 if (l == 1 && r == 1)
110 if (l == -1 && r == -1)
113 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op2);
114 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op1);
115 if (l == 0 && r == 0)
117 if (l == 0 && r != -2)
119 if (l != -2 && r == 0)
121 if (l == 1 && r == 1)
123 if (l == -1 && r == -1)
128 /* Compare X vs. X+C. */
129 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
131 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
132 && e2->value.op.op2->ts.type == BT_INTEGER
133 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
134 return -mpz_sgn (e2->value.op.op2->value.integer);
137 /* Compare X-C vs. X. */
138 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_MINUS)
140 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
141 && e1->value.op.op2->ts.type == BT_INTEGER
142 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
143 return -mpz_sgn (e1->value.op.op2->value.integer);
145 /* Compare P-Q vs. R-S. */
146 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
150 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
151 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
152 if (l == 0 && r == 0)
154 if (l != -2 && r == 0)
156 if (l == 0 && r != -2)
158 if (l == 1 && r == -1)
160 if (l == -1 && r == 1)
165 /* Compare X vs. X-C. */
166 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
168 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
169 && e2->value.op.op2->ts.type == BT_INTEGER
170 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
171 return mpz_sgn (e2->value.op.op2->value.integer);
174 if (e1->expr_type != e2->expr_type)
177 switch (e1->expr_type)
180 if (e1->ts.type != BT_INTEGER || e2->ts.type != BT_INTEGER)
183 i = mpz_cmp (e1->value.integer, e2->value.integer);
191 if (e1->ref || e2->ref)
193 if (e1->symtree->n.sym == e2->symtree->n.sym)
198 /* Intrinsic operators are the same if their operands are the same. */
199 if (e1->value.op.op != e2->value.op.op)
201 if (e1->value.op.op2 == 0)
203 i = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
204 return i == 0 ? 0 : -2;
206 if (gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1) == 0
207 && gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2) == 0)
209 /* TODO Handle commutative binary operators here? */
213 /* We can only compare calls to the same intrinsic function. */
214 if (e1->value.function.isym == 0 || e2->value.function.isym == 0
215 || e1->value.function.isym != e2->value.function.isym)
218 args1 = e1->value.function.actual;
219 args2 = e2->value.function.actual;
221 /* We should list the "constant" intrinsic functions. Those
222 without side-effects that provide equal results given equal
224 switch (e1->value.function.isym->id)
226 case GFC_ISYM_CONVERSION:
227 /* Handle integer extensions specially, as __convert_i4_i8
228 is not only "constant" but also "unary" and "increasing". */
229 if (args1 && !args1->next
230 && args2 && !args2->next
231 && e1->ts.type == BT_INTEGER
232 && args1->expr->ts.type == BT_INTEGER
233 && e1->ts.kind > args1->expr->ts.kind
234 && e2->ts.type == e1->ts.type
235 && e2->ts.kind == e1->ts.kind
236 && args2->expr->ts.type == args1->expr->ts.type
237 && args2->expr->ts.kind == args2->expr->ts.kind)
238 return gfc_dep_compare_expr (args1->expr, args2->expr);
242 case GFC_ISYM_LOGICAL:
250 /* Compare the argument lists for equality. */
251 while (args1 && args2)
253 if (gfc_dep_compare_expr (args1->expr, args2->expr) != 0)
258 return (args1 || args2) ? -2 : 0;
266 /* Returns 1 if the two ranges are the same, 0 if they are not, and def
267 if the results are indeterminate. N is the dimension to compare. */
270 gfc_is_same_range (gfc_array_ref *ar1, gfc_array_ref *ar2, int n, int def)
276 /* TODO: More sophisticated range comparison. */
277 gcc_assert (ar1 && ar2);
279 gcc_assert (ar1->dimen_type[n] == ar2->dimen_type[n]);
283 /* Check for mismatching strides. A NULL stride means a stride of 1. */
286 i = gfc_expr_is_one (e1, -1);
294 i = gfc_expr_is_one (e2, -1);
302 i = gfc_dep_compare_expr (e1, e2);
308 /* The strides match. */
310 /* Check the range start. */
315 /* Use the bound of the array if no bound is specified. */
317 e1 = ar1->as->lower[n];
320 e2 = ar2->as->lower[n];
322 /* Check we have values for both. */
326 i = gfc_dep_compare_expr (e1, e2);
333 /* Check the range end. */
338 /* Use the bound of the array if no bound is specified. */
340 e1 = ar1->as->upper[n];
343 e2 = ar2->as->upper[n];
345 /* Check we have values for both. */
349 i = gfc_dep_compare_expr (e1, e2);
360 /* Some array-returning intrinsics can be implemented by reusing the
361 data from one of the array arguments. For example, TRANSPOSE does
362 not necessarily need to allocate new data: it can be implemented
363 by copying the original array's descriptor and simply swapping the
364 two dimension specifications.
366 If EXPR is a call to such an intrinsic, return the argument
367 whose data can be reused, otherwise return NULL. */
370 gfc_get_noncopying_intrinsic_argument (gfc_expr *expr)
372 if (expr->expr_type != EXPR_FUNCTION || !expr->value.function.isym)
375 switch (expr->value.function.isym->id)
377 case GFC_ISYM_TRANSPOSE:
378 return expr->value.function.actual->expr;
386 /* Return true if the result of reference REF can only be constructed
387 using a temporary array. */
390 gfc_ref_needs_temporary_p (gfc_ref *ref)
396 for (; ref; ref = ref->next)
400 /* Vector dimensions are generally not monotonic and must be
401 handled using a temporary. */
402 if (ref->u.ar.type == AR_SECTION)
403 for (n = 0; n < ref->u.ar.dimen; n++)
404 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR)
411 /* Within an array reference, character substrings generally
412 need a temporary. Character array strides are expressed as
413 multiples of the element size (consistent with other array
414 types), not in characters. */
426 gfc_is_data_pointer (gfc_expr *e)
430 if (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION)
433 /* No subreference if it is a function */
434 gcc_assert (e->expr_type == EXPR_VARIABLE || !e->ref);
436 if (e->symtree->n.sym->attr.pointer)
439 for (ref = e->ref; ref; ref = ref->next)
440 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
447 /* Return true if array variable VAR could be passed to the same function
448 as argument EXPR without interfering with EXPR. INTENT is the intent
451 This is considerably less conservative than other dependencies
452 because many function arguments will already be copied into a
456 gfc_check_argument_var_dependency (gfc_expr *var, sym_intent intent,
457 gfc_expr *expr, gfc_dep_check elemental)
461 gcc_assert (var->expr_type == EXPR_VARIABLE);
462 gcc_assert (var->rank > 0);
464 switch (expr->expr_type)
467 /* In case of elemental subroutines, there is no dependency
468 between two same-range array references. */
469 if (gfc_ref_needs_temporary_p (expr->ref)
470 || gfc_check_dependency (var, expr, elemental == NOT_ELEMENTAL))
472 if (elemental == ELEM_DONT_CHECK_VARIABLE)
474 /* Too many false positive with pointers. */
475 if (!gfc_is_data_pointer (var) && !gfc_is_data_pointer (expr))
477 /* Elemental procedures forbid unspecified intents,
478 and we don't check dependencies for INTENT_IN args. */
479 gcc_assert (intent == INTENT_OUT || intent == INTENT_INOUT);
481 /* We are told not to check dependencies.
482 We do it, however, and issue a warning in case we find one.
483 If a dependency is found in the case
484 elemental == ELEM_CHECK_VARIABLE, we will generate
485 a temporary, so we don't need to bother the user. */
486 gfc_warning ("INTENT(%s) actual argument at %L might "
487 "interfere with actual argument at %L.",
488 intent == INTENT_OUT ? "OUT" : "INOUT",
489 &var->where, &expr->where);
499 return gfc_check_dependency (var, expr, 1);
502 if (intent != INTENT_IN && expr->inline_noncopying_intrinsic
503 && (arg = gfc_get_noncopying_intrinsic_argument (expr))
504 && gfc_check_argument_var_dependency (var, intent, arg, elemental))
508 if ((expr->value.function.esym
509 && expr->value.function.esym->attr.elemental)
510 || (expr->value.function.isym
511 && expr->value.function.isym->elemental))
512 return gfc_check_fncall_dependency (var, intent, NULL,
513 expr->value.function.actual,
514 ELEM_CHECK_VARIABLE);
519 /* In case of non-elemental procedures, there is no need to catch
520 dependencies, as we will make a temporary anyway. */
523 /* If the actual arg EXPR is an expression, we need to catch
524 a dependency between variables in EXPR and VAR,
525 an intent((IN)OUT) variable. */
526 if (expr->value.op.op1
527 && gfc_check_argument_var_dependency (var, intent,
529 ELEM_CHECK_VARIABLE))
531 else if (expr->value.op.op2
532 && gfc_check_argument_var_dependency (var, intent,
534 ELEM_CHECK_VARIABLE))
545 /* Like gfc_check_argument_var_dependency, but extended to any
546 array expression OTHER, not just variables. */
549 gfc_check_argument_dependency (gfc_expr *other, sym_intent intent,
550 gfc_expr *expr, gfc_dep_check elemental)
552 switch (other->expr_type)
555 return gfc_check_argument_var_dependency (other, intent, expr, elemental);
558 if (other->inline_noncopying_intrinsic)
560 other = gfc_get_noncopying_intrinsic_argument (other);
561 return gfc_check_argument_dependency (other, INTENT_IN, expr,
572 /* Like gfc_check_argument_dependency, but check all the arguments in ACTUAL.
573 FNSYM is the function being called, or NULL if not known. */
576 gfc_check_fncall_dependency (gfc_expr *other, sym_intent intent,
577 gfc_symbol *fnsym, gfc_actual_arglist *actual,
578 gfc_dep_check elemental)
580 gfc_formal_arglist *formal;
583 formal = fnsym ? fnsym->formal : NULL;
584 for (; actual; actual = actual->next, formal = formal ? formal->next : NULL)
588 /* Skip args which are not present. */
592 /* Skip other itself. */
596 /* Skip intent(in) arguments if OTHER itself is intent(in). */
597 if (formal && intent == INTENT_IN
598 && formal->sym->attr.intent == INTENT_IN)
601 if (gfc_check_argument_dependency (other, intent, expr, elemental))
609 /* Return 1 if e1 and e2 are equivalenced arrays, either
610 directly or indirectly; i.e., equivalence (a,b) for a and b
611 or equivalence (a,c),(b,c). This function uses the equiv_
612 lists, generated in trans-common(add_equivalences), that are
613 guaranteed to pick up indirect equivalences. We explicitly
614 check for overlap using the offset and length of the equivalence.
615 This function is symmetric.
616 TODO: This function only checks whether the full top-level
617 symbols overlap. An improved implementation could inspect
618 e1->ref and e2->ref to determine whether the actually accessed
619 portions of these variables/arrays potentially overlap. */
622 gfc_are_equivalenced_arrays (gfc_expr *e1, gfc_expr *e2)
625 gfc_equiv_info *s, *fl1, *fl2;
627 gcc_assert (e1->expr_type == EXPR_VARIABLE
628 && e2->expr_type == EXPR_VARIABLE);
630 if (!e1->symtree->n.sym->attr.in_equivalence
631 || !e2->symtree->n.sym->attr.in_equivalence|| !e1->rank || !e2->rank)
634 if (e1->symtree->n.sym->ns
635 && e1->symtree->n.sym->ns != gfc_current_ns)
636 l = e1->symtree->n.sym->ns->equiv_lists;
638 l = gfc_current_ns->equiv_lists;
640 /* Go through the equiv_lists and return 1 if the variables
641 e1 and e2 are members of the same group and satisfy the
642 requirement on their relative offsets. */
643 for (; l; l = l->next)
647 for (s = l->equiv; s; s = s->next)
649 if (s->sym == e1->symtree->n.sym)
655 if (s->sym == e2->symtree->n.sym)
665 /* Can these lengths be zero? */
666 if (fl1->length <= 0 || fl2->length <= 0)
668 /* These can't overlap if [f11,fl1+length] is before
669 [fl2,fl2+length], or [fl2,fl2+length] is before
670 [fl1,fl1+length], otherwise they do overlap. */
671 if (fl1->offset + fl1->length > fl2->offset
672 && fl2->offset + fl2->length > fl1->offset)
680 /* Return true if there is no possibility of aliasing because of a type
681 mismatch between all the possible pointer references and the
682 potential target. Note that this function is asymmetric in the
683 arguments and so must be called twice with the arguments exchanged. */
686 check_data_pointer_types (gfc_expr *expr1, gfc_expr *expr2)
692 bool seen_component_ref;
694 if (expr1->expr_type != EXPR_VARIABLE
695 || expr1->expr_type != EXPR_VARIABLE)
698 sym1 = expr1->symtree->n.sym;
699 sym2 = expr2->symtree->n.sym;
701 /* Keep it simple for now. */
702 if (sym1->ts.type == BT_DERIVED && sym2->ts.type == BT_DERIVED)
705 if (sym1->attr.pointer)
707 if (gfc_compare_types (&sym1->ts, &sym2->ts))
711 /* This is a conservative check on the components of the derived type
712 if no component references have been seen. Since we will not dig
713 into the components of derived type components, we play it safe by
714 returning false. First we check the reference chain and then, if
715 no component references have been seen, the components. */
716 seen_component_ref = false;
717 if (sym1->ts.type == BT_DERIVED)
719 for (ref1 = expr1->ref; ref1; ref1 = ref1->next)
721 if (ref1->type != REF_COMPONENT)
724 if (ref1->u.c.component->ts.type == BT_DERIVED)
727 if ((sym2->attr.pointer || ref1->u.c.component->attr.pointer)
728 && gfc_compare_types (&ref1->u.c.component->ts, &sym2->ts))
731 seen_component_ref = true;
735 if (sym1->ts.type == BT_DERIVED && !seen_component_ref)
737 for (cm1 = sym1->ts.u.derived->components; cm1; cm1 = cm1->next)
739 if (cm1->ts.type == BT_DERIVED)
742 if ((sym2->attr.pointer || cm1->attr.pointer)
743 && gfc_compare_types (&cm1->ts, &sym2->ts))
752 /* Return true if the statement body redefines the condition. Returns
753 true if expr2 depends on expr1. expr1 should be a single term
754 suitable for the lhs of an assignment. The IDENTICAL flag indicates
755 whether array references to the same symbol with identical range
756 references count as a dependency or not. Used for forall and where
757 statements. Also used with functions returning arrays without a
761 gfc_check_dependency (gfc_expr *expr1, gfc_expr *expr2, bool identical)
763 gfc_actual_arglist *actual;
767 gcc_assert (expr1->expr_type == EXPR_VARIABLE);
769 switch (expr2->expr_type)
772 n = gfc_check_dependency (expr1, expr2->value.op.op1, identical);
775 if (expr2->value.op.op2)
776 return gfc_check_dependency (expr1, expr2->value.op.op2, identical);
780 /* The interesting cases are when the symbols don't match. */
781 if (expr1->symtree->n.sym != expr2->symtree->n.sym)
783 gfc_typespec *ts1 = &expr1->symtree->n.sym->ts;
784 gfc_typespec *ts2 = &expr2->symtree->n.sym->ts;
786 /* Return 1 if expr1 and expr2 are equivalenced arrays. */
787 if (gfc_are_equivalenced_arrays (expr1, expr2))
790 /* Symbols can only alias if they have the same type. */
791 if (ts1->type != BT_UNKNOWN && ts2->type != BT_UNKNOWN
792 && ts1->type != BT_DERIVED && ts2->type != BT_DERIVED)
794 if (ts1->type != ts2->type || ts1->kind != ts2->kind)
798 /* If either variable is a pointer, assume the worst. */
799 /* TODO: -fassume-no-pointer-aliasing */
800 if (gfc_is_data_pointer (expr1) || gfc_is_data_pointer (expr2))
802 if (check_data_pointer_types (expr1, expr2)
803 && check_data_pointer_types (expr2, expr1))
809 /* Otherwise distinct symbols have no dependencies. */
816 /* Identical and disjoint ranges return 0,
817 overlapping ranges return 1. */
818 if (expr1->ref && expr2->ref)
819 return gfc_dep_resolver (expr1->ref, expr2->ref);
824 if (expr2->inline_noncopying_intrinsic)
826 /* Remember possible differences between elemental and
827 transformational functions. All functions inside a FORALL
829 for (actual = expr2->value.function.actual;
830 actual; actual = actual->next)
834 n = gfc_check_dependency (expr1, actual->expr, identical);
845 /* Loop through the array constructor's elements. */
846 for (c = expr2->value.constructor; c; c = c->next)
848 /* If this is an iterator, assume the worst. */
851 /* Avoid recursion in the common case. */
852 if (c->expr->expr_type == EXPR_CONSTANT)
854 if (gfc_check_dependency (expr1, c->expr, 1))
865 /* Determines overlapping for two array sections. */
867 static gfc_dependency
868 gfc_check_section_vs_section (gfc_ref *lref, gfc_ref *rref, int n)
889 /* If they are the same range, return without more ado. */
890 if (gfc_is_same_range (&l_ar, &r_ar, n, 0))
891 return GFC_DEP_EQUAL;
893 l_start = l_ar.start[n];
895 l_stride = l_ar.stride[n];
897 r_start = r_ar.start[n];
899 r_stride = r_ar.stride[n];
901 /* If l_start is NULL take it from array specifier. */
902 if (NULL == l_start && IS_ARRAY_EXPLICIT (l_ar.as))
903 l_start = l_ar.as->lower[n];
904 /* If l_end is NULL take it from array specifier. */
905 if (NULL == l_end && IS_ARRAY_EXPLICIT (l_ar.as))
906 l_end = l_ar.as->upper[n];
908 /* If r_start is NULL take it from array specifier. */
909 if (NULL == r_start && IS_ARRAY_EXPLICIT (r_ar.as))
910 r_start = r_ar.as->lower[n];
911 /* If r_end is NULL take it from array specifier. */
912 if (NULL == r_end && IS_ARRAY_EXPLICIT (r_ar.as))
913 r_end = r_ar.as->upper[n];
915 /* Determine whether the l_stride is positive or negative. */
918 else if (l_stride->expr_type == EXPR_CONSTANT
919 && l_stride->ts.type == BT_INTEGER)
920 l_dir = mpz_sgn (l_stride->value.integer);
921 else if (l_start && l_end)
922 l_dir = gfc_dep_compare_expr (l_end, l_start);
926 /* Determine whether the r_stride is positive or negative. */
929 else if (r_stride->expr_type == EXPR_CONSTANT
930 && r_stride->ts.type == BT_INTEGER)
931 r_dir = mpz_sgn (r_stride->value.integer);
932 else if (r_start && r_end)
933 r_dir = gfc_dep_compare_expr (r_end, r_start);
937 /* The strides should never be zero. */
938 if (l_dir == 0 || r_dir == 0)
939 return GFC_DEP_OVERLAP;
941 /* Determine LHS upper and lower bounds. */
947 else if (l_dir == -1)
958 /* Determine RHS upper and lower bounds. */
964 else if (r_dir == -1)
975 /* Check whether the ranges are disjoint. */
976 if (l_upper && r_lower && gfc_dep_compare_expr (l_upper, r_lower) == -1)
977 return GFC_DEP_NODEP;
978 if (r_upper && l_lower && gfc_dep_compare_expr (r_upper, l_lower) == -1)
979 return GFC_DEP_NODEP;
981 /* Handle cases like x:y:1 vs. x:z:-1 as GFC_DEP_EQUAL. */
982 if (l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == 0)
984 if (l_dir == 1 && r_dir == -1)
985 return GFC_DEP_EQUAL;
986 if (l_dir == -1 && r_dir == 1)
987 return GFC_DEP_EQUAL;
990 /* Handle cases like x:y:1 vs. z:y:-1 as GFC_DEP_EQUAL. */
991 if (l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == 0)
993 if (l_dir == 1 && r_dir == -1)
994 return GFC_DEP_EQUAL;
995 if (l_dir == -1 && r_dir == 1)
996 return GFC_DEP_EQUAL;
999 /* Check for forward dependencies x:y vs. x+1:z. */
1000 if (l_dir == 1 && r_dir == 1
1001 && l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == -1
1002 && l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == -1)
1004 /* Check that the strides are the same. */
1005 if (!l_stride && !r_stride)
1006 return GFC_DEP_FORWARD;
1007 if (l_stride && r_stride
1008 && gfc_dep_compare_expr (l_stride, r_stride) == 0)
1009 return GFC_DEP_FORWARD;
1012 /* Check for forward dependencies x:y:-1 vs. x-1:z:-1. */
1013 if (l_dir == -1 && r_dir == -1
1014 && l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == 1
1015 && l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == 1)
1017 /* Check that the strides are the same. */
1018 if (!l_stride && !r_stride)
1019 return GFC_DEP_FORWARD;
1020 if (l_stride && r_stride
1021 && gfc_dep_compare_expr (l_stride, r_stride) == 0)
1022 return GFC_DEP_FORWARD;
1025 return GFC_DEP_OVERLAP;
1029 /* Determines overlapping for a single element and a section. */
1031 static gfc_dependency
1032 gfc_check_element_vs_section( gfc_ref *lref, gfc_ref *rref, int n)
1041 elem = lref->u.ar.start[n];
1043 return GFC_DEP_OVERLAP;
1046 start = ref->start[n] ;
1048 stride = ref->stride[n];
1050 if (!start && IS_ARRAY_EXPLICIT (ref->as))
1051 start = ref->as->lower[n];
1052 if (!end && IS_ARRAY_EXPLICIT (ref->as))
1053 end = ref->as->upper[n];
1055 /* Determine whether the stride is positive or negative. */
1058 else if (stride->expr_type == EXPR_CONSTANT
1059 && stride->ts.type == BT_INTEGER)
1060 s = mpz_sgn (stride->value.integer);
1064 /* Stride should never be zero. */
1066 return GFC_DEP_OVERLAP;
1068 /* Positive strides. */
1071 /* Check for elem < lower. */
1072 if (start && gfc_dep_compare_expr (elem, start) == -1)
1073 return GFC_DEP_NODEP;
1074 /* Check for elem > upper. */
1075 if (end && gfc_dep_compare_expr (elem, end) == 1)
1076 return GFC_DEP_NODEP;
1080 s = gfc_dep_compare_expr (start, end);
1081 /* Check for an empty range. */
1083 return GFC_DEP_NODEP;
1084 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1085 return GFC_DEP_EQUAL;
1088 /* Negative strides. */
1091 /* Check for elem > upper. */
1092 if (end && gfc_dep_compare_expr (elem, start) == 1)
1093 return GFC_DEP_NODEP;
1094 /* Check for elem < lower. */
1095 if (start && gfc_dep_compare_expr (elem, end) == -1)
1096 return GFC_DEP_NODEP;
1100 s = gfc_dep_compare_expr (start, end);
1101 /* Check for an empty range. */
1103 return GFC_DEP_NODEP;
1104 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1105 return GFC_DEP_EQUAL;
1108 /* Unknown strides. */
1112 return GFC_DEP_OVERLAP;
1113 s = gfc_dep_compare_expr (start, end);
1115 return GFC_DEP_OVERLAP;
1116 /* Assume positive stride. */
1119 /* Check for elem < lower. */
1120 if (gfc_dep_compare_expr (elem, start) == -1)
1121 return GFC_DEP_NODEP;
1122 /* Check for elem > upper. */
1123 if (gfc_dep_compare_expr (elem, end) == 1)
1124 return GFC_DEP_NODEP;
1126 /* Assume negative stride. */
1129 /* Check for elem > upper. */
1130 if (gfc_dep_compare_expr (elem, start) == 1)
1131 return GFC_DEP_NODEP;
1132 /* Check for elem < lower. */
1133 if (gfc_dep_compare_expr (elem, end) == -1)
1134 return GFC_DEP_NODEP;
1139 s = gfc_dep_compare_expr (elem, start);
1141 return GFC_DEP_EQUAL;
1142 if (s == 1 || s == -1)
1143 return GFC_DEP_NODEP;
1147 return GFC_DEP_OVERLAP;
1151 /* Traverse expr, checking all EXPR_VARIABLE symbols for their
1152 forall_index attribute. Return true if any variable may be
1153 being used as a FORALL index. Its safe to pessimistically
1154 return true, and assume a dependency. */
1157 contains_forall_index_p (gfc_expr *expr)
1159 gfc_actual_arglist *arg;
1167 switch (expr->expr_type)
1170 if (expr->symtree->n.sym->forall_index)
1175 if (contains_forall_index_p (expr->value.op.op1)
1176 || contains_forall_index_p (expr->value.op.op2))
1181 for (arg = expr->value.function.actual; arg; arg = arg->next)
1182 if (contains_forall_index_p (arg->expr))
1188 case EXPR_SUBSTRING:
1191 case EXPR_STRUCTURE:
1193 for (c = expr->value.constructor; c; c = c->next)
1194 if (contains_forall_index_p (c->expr))
1202 for (ref = expr->ref; ref; ref = ref->next)
1206 for (i = 0; i < ref->u.ar.dimen; i++)
1207 if (contains_forall_index_p (ref->u.ar.start[i])
1208 || contains_forall_index_p (ref->u.ar.end[i])
1209 || contains_forall_index_p (ref->u.ar.stride[i]))
1217 if (contains_forall_index_p (ref->u.ss.start)
1218 || contains_forall_index_p (ref->u.ss.end))
1229 /* Determines overlapping for two single element array references. */
1231 static gfc_dependency
1232 gfc_check_element_vs_element (gfc_ref *lref, gfc_ref *rref, int n)
1242 l_start = l_ar.start[n] ;
1243 r_start = r_ar.start[n] ;
1244 i = gfc_dep_compare_expr (r_start, l_start);
1246 return GFC_DEP_EQUAL;
1248 /* Treat two scalar variables as potentially equal. This allows
1249 us to prove that a(i,:) and a(j,:) have no dependency. See
1250 Gerald Roth, "Evaluation of Array Syntax Dependence Analysis",
1251 Proceedings of the International Conference on Parallel and
1252 Distributed Processing Techniques and Applications (PDPTA2001),
1253 Las Vegas, Nevada, June 2001. */
1254 /* However, we need to be careful when either scalar expression
1255 contains a FORALL index, as these can potentially change value
1256 during the scalarization/traversal of this array reference. */
1257 if (contains_forall_index_p (r_start) || contains_forall_index_p (l_start))
1258 return GFC_DEP_OVERLAP;
1261 return GFC_DEP_NODEP;
1262 return GFC_DEP_EQUAL;
1266 /* Determine if an array ref, usually an array section specifies the
1267 entire array. In addition, if the second, pointer argument is
1268 provided, the function will return true if the reference is
1269 contiguous; eg. (:, 1) gives true but (1,:) gives false. */
1272 gfc_full_array_ref_p (gfc_ref *ref, bool *contiguous)
1275 bool lbound_OK = true;
1276 bool ubound_OK = true;
1279 *contiguous = false;
1281 if (ref->type != REF_ARRAY)
1283 if (ref->u.ar.type == AR_FULL)
1289 if (ref->u.ar.type != AR_SECTION)
1294 for (i = 0; i < ref->u.ar.dimen; i++)
1296 /* If we have a single element in the reference, we need to check
1297 that the array has a single element and that we actually reference
1298 the correct element. */
1299 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
1301 /* This is a contiguous reference. */
1303 *contiguous = (i + 1 == ref->u.ar.dimen);
1306 || !ref->u.ar.as->lower[i]
1307 || !ref->u.ar.as->upper[i]
1308 || gfc_dep_compare_expr (ref->u.ar.as->lower[i],
1309 ref->u.ar.as->upper[i])
1310 || !ref->u.ar.start[i]
1311 || gfc_dep_compare_expr (ref->u.ar.start[i],
1312 ref->u.ar.as->lower[i]))
1318 /* Check the lower bound. */
1319 if (ref->u.ar.start[i]
1321 || !ref->u.ar.as->lower[i]
1322 || gfc_dep_compare_expr (ref->u.ar.start[i],
1323 ref->u.ar.as->lower[i])))
1325 /* Check the upper bound. */
1326 if (ref->u.ar.end[i]
1328 || !ref->u.ar.as->upper[i]
1329 || gfc_dep_compare_expr (ref->u.ar.end[i],
1330 ref->u.ar.as->upper[i])))
1332 /* Check the stride. */
1333 if (ref->u.ar.stride[i] && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
1336 /* This is a contiguous reference. */
1338 *contiguous = (i + 1 == ref->u.ar.dimen);
1340 if (!lbound_OK || !ubound_OK)
1347 /* Determine if a full array is the same as an array section with one
1348 variable limit. For this to be so, the strides must both be unity
1349 and one of either start == lower or end == upper must be true. */
1352 ref_same_as_full_array (gfc_ref *full_ref, gfc_ref *ref)
1355 bool upper_or_lower;
1357 if (full_ref->type != REF_ARRAY)
1359 if (full_ref->u.ar.type != AR_FULL)
1361 if (ref->type != REF_ARRAY)
1363 if (ref->u.ar.type != AR_SECTION)
1366 for (i = 0; i < ref->u.ar.dimen; i++)
1368 /* If we have a single element in the reference, we need to check
1369 that the array has a single element and that we actually reference
1370 the correct element. */
1371 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
1373 if (!full_ref->u.ar.as
1374 || !full_ref->u.ar.as->lower[i]
1375 || !full_ref->u.ar.as->upper[i]
1376 || gfc_dep_compare_expr (full_ref->u.ar.as->lower[i],
1377 full_ref->u.ar.as->upper[i])
1378 || !ref->u.ar.start[i]
1379 || gfc_dep_compare_expr (ref->u.ar.start[i],
1380 full_ref->u.ar.as->lower[i]))
1384 /* Check the strides. */
1385 if (full_ref->u.ar.stride[i] && !gfc_expr_is_one (full_ref->u.ar.stride[i], 0))
1387 if (ref->u.ar.stride[i] && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
1390 upper_or_lower = false;
1391 /* Check the lower bound. */
1392 if (ref->u.ar.start[i]
1394 && full_ref->u.ar.as->lower[i]
1395 && gfc_dep_compare_expr (ref->u.ar.start[i],
1396 full_ref->u.ar.as->lower[i]) == 0))
1397 upper_or_lower = true;
1398 /* Check the upper bound. */
1399 if (ref->u.ar.end[i]
1401 && full_ref->u.ar.as->upper[i]
1402 && gfc_dep_compare_expr (ref->u.ar.end[i],
1403 full_ref->u.ar.as->upper[i]) == 0))
1404 upper_or_lower = true;
1405 if (!upper_or_lower)
1412 /* Finds if two array references are overlapping or not.
1414 1 : array references are overlapping.
1415 0 : array references are identical or not overlapping. */
1418 gfc_dep_resolver (gfc_ref *lref, gfc_ref *rref)
1421 gfc_dependency fin_dep;
1422 gfc_dependency this_dep;
1424 fin_dep = GFC_DEP_ERROR;
1425 /* Dependencies due to pointers should already have been identified.
1426 We only need to check for overlapping array references. */
1428 while (lref && rref)
1430 /* We're resolving from the same base symbol, so both refs should be
1431 the same type. We traverse the reference chain until we find ranges
1432 that are not equal. */
1433 gcc_assert (lref->type == rref->type);
1437 /* The two ranges can't overlap if they are from different
1439 if (lref->u.c.component != rref->u.c.component)
1444 /* Substring overlaps are handled by the string assignment code
1445 if there is not an underlying dependency. */
1446 return (fin_dep == GFC_DEP_OVERLAP) ? 1 : 0;
1450 if (ref_same_as_full_array (lref, rref))
1453 if (ref_same_as_full_array (rref, lref))
1456 if (lref->u.ar.dimen != rref->u.ar.dimen)
1458 if (lref->u.ar.type == AR_FULL)
1459 fin_dep = gfc_full_array_ref_p (rref, NULL) ? GFC_DEP_EQUAL
1461 else if (rref->u.ar.type == AR_FULL)
1462 fin_dep = gfc_full_array_ref_p (lref, NULL) ? GFC_DEP_EQUAL
1469 for (n=0; n < lref->u.ar.dimen; n++)
1471 /* Assume dependency when either of array reference is vector
1473 if (lref->u.ar.dimen_type[n] == DIMEN_VECTOR
1474 || rref->u.ar.dimen_type[n] == DIMEN_VECTOR)
1476 if (lref->u.ar.dimen_type[n] == DIMEN_RANGE
1477 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
1478 this_dep = gfc_check_section_vs_section (lref, rref, n);
1479 else if (lref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1480 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
1481 this_dep = gfc_check_element_vs_section (lref, rref, n);
1482 else if (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1483 && lref->u.ar.dimen_type[n] == DIMEN_RANGE)
1484 this_dep = gfc_check_element_vs_section (rref, lref, n);
1487 gcc_assert (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1488 && lref->u.ar.dimen_type[n] == DIMEN_ELEMENT);
1489 this_dep = gfc_check_element_vs_element (rref, lref, n);
1492 /* If any dimension doesn't overlap, we have no dependency. */
1493 if (this_dep == GFC_DEP_NODEP)
1496 /* Overlap codes are in order of priority. We only need to
1497 know the worst one.*/
1498 if (this_dep > fin_dep)
1502 /* If this is an equal element, we have to keep going until we find
1503 the "real" array reference. */
1504 if (lref->u.ar.type == AR_ELEMENT
1505 && rref->u.ar.type == AR_ELEMENT
1506 && fin_dep == GFC_DEP_EQUAL)
1509 /* Exactly matching and forward overlapping ranges don't cause a
1511 if (fin_dep < GFC_DEP_OVERLAP)
1514 /* Keep checking. We only have a dependency if
1515 subsequent references also overlap. */
1525 /* If we haven't seen any array refs then something went wrong. */
1526 gcc_assert (fin_dep != GFC_DEP_ERROR);
1528 /* Assume the worst if we nest to different depths. */
1532 return fin_dep == GFC_DEP_OVERLAP;