2 Copyright (C) 2000, 2001, 2002, 2005, 2006, 2007
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 /* dependency.c -- Expression dependency analysis code. */
24 /* There's probably quite a bit of duplication in this file. We currently
25 have different dependency checking functions for different types
26 if dependencies. Ideally these would probably be merged. */
30 #include "dependency.h"
32 /* static declarations */
34 enum range {LHS, RHS, MID};
36 /* Dependency types. These must be in reverse order of priority. */
40 GFC_DEP_EQUAL, /* Identical Ranges. */
41 GFC_DEP_FORWARD, /* eg. a(1:3), a(2:4). */
42 GFC_DEP_OVERLAP, /* May overlap in some other way. */
43 GFC_DEP_NODEP /* Distinct ranges. */
48 #define IS_ARRAY_EXPLICIT(as) ((as->type == AS_EXPLICIT ? 1 : 0))
51 /* Returns 1 if the expr is an integer constant value 1, 0 if it is not or
52 def if the value could not be determined. */
55 gfc_expr_is_one (gfc_expr *expr, int def)
57 gcc_assert (expr != NULL);
59 if (expr->expr_type != EXPR_CONSTANT)
62 if (expr->ts.type != BT_INTEGER)
65 return mpz_cmp_si (expr->value.integer, 1) == 0;
69 /* Compare two values. Returns 0 if e1 == e2, -1 if e1 < e2, +1 if e1 > e2,
70 and -2 if the relationship could not be determined. */
73 gfc_dep_compare_expr (gfc_expr *e1, gfc_expr *e2)
75 gfc_actual_arglist *args1;
76 gfc_actual_arglist *args2;
79 if (e1->expr_type == EXPR_OP
80 && (e1->value.op.operator == INTRINSIC_UPLUS
81 || e1->value.op.operator == INTRINSIC_PARENTHESES))
82 return gfc_dep_compare_expr (e1->value.op.op1, e2);
83 if (e2->expr_type == EXPR_OP
84 && (e2->value.op.operator == INTRINSIC_UPLUS
85 || e2->value.op.operator == INTRINSIC_PARENTHESES))
86 return gfc_dep_compare_expr (e1, e2->value.op.op1);
88 if (e1->expr_type == EXPR_OP && e1->value.op.operator == INTRINSIC_PLUS)
90 /* Compare X+C vs. X. */
91 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
92 && e1->value.op.op2->ts.type == BT_INTEGER
93 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
94 return mpz_sgn (e1->value.op.op2->value.integer);
96 /* Compare P+Q vs. R+S. */
97 if (e2->expr_type == EXPR_OP && e2->value.op.operator == INTRINSIC_PLUS)
101 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
102 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
103 if (l == 0 && r == 0)
105 if (l == 0 && r != -2)
107 if (l != -2 && r == 0)
109 if (l == 1 && r == 1)
111 if (l == -1 && r == -1)
114 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op2);
115 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op1);
116 if (l == 0 && r == 0)
118 if (l == 0 && r != -2)
120 if (l != -2 && r == 0)
122 if (l == 1 && r == 1)
124 if (l == -1 && r == -1)
129 /* Compare X vs. X+C. */
130 if (e2->expr_type == EXPR_OP && e2->value.op.operator == INTRINSIC_PLUS)
132 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
133 && e2->value.op.op2->ts.type == BT_INTEGER
134 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
135 return -mpz_sgn (e2->value.op.op2->value.integer);
138 /* Compare X-C vs. X. */
139 if (e1->expr_type == EXPR_OP && e1->value.op.operator == INTRINSIC_MINUS)
141 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
142 && e1->value.op.op2->ts.type == BT_INTEGER
143 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
144 return -mpz_sgn (e1->value.op.op2->value.integer);
146 /* Compare P-Q vs. R-S. */
147 if (e2->expr_type == EXPR_OP && e2->value.op.operator == INTRINSIC_MINUS)
151 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
152 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
153 if (l == 0 && r == 0)
155 if (l != -2 && r == 0)
157 if (l == 0 && r != -2)
159 if (l == 1 && r == -1)
161 if (l == -1 && r == 1)
166 /* Compare X vs. X-C. */
167 if (e2->expr_type == EXPR_OP && e2->value.op.operator == INTRINSIC_MINUS)
169 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
170 && e2->value.op.op2->ts.type == BT_INTEGER
171 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
172 return mpz_sgn (e2->value.op.op2->value.integer);
175 if (e1->expr_type != e2->expr_type)
178 switch (e1->expr_type)
181 if (e1->ts.type != BT_INTEGER || e2->ts.type != BT_INTEGER)
184 i = mpz_cmp (e1->value.integer, e2->value.integer);
192 if (e1->ref || e2->ref)
194 if (e1->symtree->n.sym == e2->symtree->n.sym)
199 /* Intrinsic operators are the same if their operands are the same. */
200 if (e1->value.op.operator != e2->value.op.operator)
202 if (e1->value.op.op2 == 0)
204 i = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
205 return i == 0 ? 0 : -2;
207 if (gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1) == 0
208 && gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2) == 0)
210 /* TODO Handle commutative binary operators here? */
214 /* We can only compare calls to the same intrinsic function. */
215 if (e1->value.function.isym == 0 || e2->value.function.isym == 0
216 || e1->value.function.isym != e2->value.function.isym)
219 args1 = e1->value.function.actual;
220 args2 = e2->value.function.actual;
222 /* We should list the "constant" intrinsic functions. Those
223 without side-effects that provide equal results given equal
225 switch (e1->value.function.isym->generic_id)
227 case GFC_ISYM_CONVERSION:
228 /* Handle integer extensions specially, as __convert_i4_i8
229 is not only "constant" but also "unary" and "increasing". */
230 if (args1 && !args1->next
231 && args2 && !args2->next
232 && e1->ts.type == BT_INTEGER
233 && args1->expr->ts.type == BT_INTEGER
234 && e1->ts.kind > args1->expr->ts.kind
235 && e2->ts.type == e1->ts.type
236 && e2->ts.kind == e1->ts.kind
237 && args2->expr->ts.type == args1->expr->ts.type
238 && args2->expr->ts.kind == args2->expr->ts.kind)
239 return gfc_dep_compare_expr (args1->expr, args2->expr);
243 case GFC_ISYM_LOGICAL:
251 /* Compare the argument lists for equality. */
252 while (args1 && args2)
254 if (gfc_dep_compare_expr (args1->expr, args2->expr) != 0)
259 return (args1 || args2) ? -2 : 0;
267 /* Returns 1 if the two ranges are the same, 0 if they are not, and def
268 if the results are indeterminate. N is the dimension to compare. */
271 gfc_is_same_range (gfc_array_ref *ar1, gfc_array_ref *ar2, int n, int def)
277 /* TODO: More sophisticated range comparison. */
278 gcc_assert (ar1 && ar2);
280 gcc_assert (ar1->dimen_type[n] == ar2->dimen_type[n]);
284 /* Check for mismatching strides. A NULL stride means a stride of 1. */
287 i = gfc_expr_is_one (e1, -1);
295 i = gfc_expr_is_one (e2, -1);
303 i = gfc_dep_compare_expr (e1, e2);
309 /* The strides match. */
311 /* Check the range start. */
316 /* Use the bound of the array if no bound is specified. */
318 e1 = ar1->as->lower[n];
321 e2 = ar2->as->lower[n];
323 /* Check we have values for both. */
327 i = gfc_dep_compare_expr (e1, e2);
334 /* Check the range end. */
339 /* Use the bound of the array if no bound is specified. */
341 e1 = ar1->as->upper[n];
344 e2 = ar2->as->upper[n];
346 /* Check we have values for both. */
350 i = gfc_dep_compare_expr (e1, e2);
361 /* Some array-returning intrinsics can be implemented by reusing the
362 data from one of the array arguments. For example, TRANSPOSE does
363 not necessarily need to allocate new data: it can be implemented
364 by copying the original array's descriptor and simply swapping the
365 two dimension specifications.
367 If EXPR is a call to such an intrinsic, return the argument
368 whose data can be reused, otherwise return NULL. */
371 gfc_get_noncopying_intrinsic_argument (gfc_expr *expr)
373 if (expr->expr_type != EXPR_FUNCTION || !expr->value.function.isym)
376 switch (expr->value.function.isym->generic_id)
378 case GFC_ISYM_TRANSPOSE:
379 return expr->value.function.actual->expr;
387 /* Return true if the result of reference REF can only be constructed
388 using a temporary array. */
391 gfc_ref_needs_temporary_p (gfc_ref *ref)
397 for (; ref; ref = ref->next)
401 /* Vector dimensions are generally not monotonic and must be
402 handled using a temporary. */
403 if (ref->u.ar.type == AR_SECTION)
404 for (n = 0; n < ref->u.ar.dimen; n++)
405 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR)
412 /* Within an array reference, character substrings generally
413 need a temporary. Character array strides are expressed as
414 multiples of the element size (consistent with other array
415 types), not in characters. */
426 /* Return true if array variable VAR could be passed to the same function
427 as argument EXPR without interfering with EXPR. INTENT is the intent
430 This is considerably less conservative than other dependencies
431 because many function arguments will already be copied into a
435 gfc_check_argument_var_dependency (gfc_expr *var, sym_intent intent,
438 gcc_assert (var->expr_type == EXPR_VARIABLE);
439 gcc_assert (var->rank > 0);
441 switch (expr->expr_type)
444 return (gfc_ref_needs_temporary_p (expr->ref)
445 || gfc_check_dependency (var, expr, 1));
448 return gfc_check_dependency (var, expr, 1);
451 if (intent != INTENT_IN && expr->inline_noncopying_intrinsic)
453 expr = gfc_get_noncopying_intrinsic_argument (expr);
454 return gfc_check_argument_var_dependency (var, intent, expr);
464 /* Like gfc_check_argument_var_dependency, but extended to any
465 array expression OTHER, not just variables. */
468 gfc_check_argument_dependency (gfc_expr *other, sym_intent intent,
471 switch (other->expr_type)
474 return gfc_check_argument_var_dependency (other, intent, expr);
477 if (other->inline_noncopying_intrinsic)
479 other = gfc_get_noncopying_intrinsic_argument (other);
480 return gfc_check_argument_dependency (other, INTENT_IN, expr);
490 /* Like gfc_check_argument_dependency, but check all the arguments in ACTUAL.
491 FNSYM is the function being called, or NULL if not known. */
494 gfc_check_fncall_dependency (gfc_expr *other, sym_intent intent,
495 gfc_symbol *fnsym, gfc_actual_arglist *actual)
497 gfc_formal_arglist *formal;
500 formal = fnsym ? fnsym->formal : NULL;
501 for (; actual; actual = actual->next, formal = formal ? formal->next : NULL)
505 /* Skip args which are not present. */
509 /* Skip other itself. */
513 /* Skip intent(in) arguments if OTHER itself is intent(in). */
514 if (formal && intent == INTENT_IN
515 && formal->sym->attr.intent == INTENT_IN)
518 if (gfc_check_argument_dependency (other, intent, expr))
526 /* Return 1 if e1 and e2 are equivalenced arrays, either
527 directly or indirectly; ie. equivalence (a,b) for a and b
528 or equivalence (a,c),(b,c). This function uses the equiv_
529 lists, generated in trans-common(add_equivalences), that are
530 guaranteed to pick up indirect equivalences. We explicitly
531 check for overlap using the offset and length of the equivalence.
532 This function is symmetric.
533 TODO: This function only checks whether the full top-level
534 symbols overlap. An improved implementation could inspect
535 e1->ref and e2->ref to determine whether the actually accessed
536 portions of these variables/arrays potentially overlap. */
539 gfc_are_equivalenced_arrays (gfc_expr *e1, gfc_expr *e2)
542 gfc_equiv_info *s, *fl1, *fl2;
544 gcc_assert (e1->expr_type == EXPR_VARIABLE
545 && e2->expr_type == EXPR_VARIABLE);
547 if (!e1->symtree->n.sym->attr.in_equivalence
548 || !e2->symtree->n.sym->attr.in_equivalence|| !e1->rank || !e2->rank)
551 /* Go through the equiv_lists and return 1 if the variables
552 e1 and e2 are members of the same group and satisfy the
553 requirement on their relative offsets. */
554 for (l = gfc_current_ns->equiv_lists; l; l = l->next)
558 for (s = l->equiv; s; s = s->next)
560 if (s->sym == e1->symtree->n.sym)
566 if (s->sym == e2->symtree->n.sym)
576 /* Can these lengths be zero? */
577 if (fl1->length <= 0 || fl2->length <= 0)
579 /* These can't overlap if [f11,fl1+length] is before
580 [fl2,fl2+length], or [fl2,fl2+length] is before
581 [fl1,fl1+length], otherwise they do overlap. */
582 if (fl1->offset + fl1->length > fl2->offset
583 && fl2->offset + fl2->length > fl1->offset)
591 /* Return true if the statement body redefines the condition. Returns
592 true if expr2 depends on expr1. expr1 should be a single term
593 suitable for the lhs of an assignment. The IDENTICAL flag indicates
594 whether array references to the same symbol with identical range
595 references count as a dependency or not. Used for forall and where
596 statements. Also used with functions returning arrays without a
600 gfc_check_dependency (gfc_expr *expr1, gfc_expr *expr2, bool identical)
604 gfc_actual_arglist *actual;
606 gcc_assert (expr1->expr_type == EXPR_VARIABLE);
608 switch (expr2->expr_type)
611 n = gfc_check_dependency (expr1, expr2->value.op.op1, identical);
614 if (expr2->value.op.op2)
615 return gfc_check_dependency (expr1, expr2->value.op.op2, identical);
619 /* The interesting cases are when the symbols don't match. */
620 if (expr1->symtree->n.sym != expr2->symtree->n.sym)
622 gfc_typespec *ts1 = &expr1->symtree->n.sym->ts;
623 gfc_typespec *ts2 = &expr2->symtree->n.sym->ts;
625 /* Return 1 if expr1 and expr2 are equivalenced arrays. */
626 if (gfc_are_equivalenced_arrays (expr1, expr2))
629 /* Symbols can only alias if they have the same type. */
630 if (ts1->type != BT_UNKNOWN && ts2->type != BT_UNKNOWN
631 && ts1->type != BT_DERIVED && ts2->type != BT_DERIVED)
633 if (ts1->type != ts2->type || ts1->kind != ts2->kind)
637 /* If either variable is a pointer, assume the worst. */
638 /* TODO: -fassume-no-pointer-aliasing */
639 if (expr1->symtree->n.sym->attr.pointer)
641 for (ref = expr1->ref; ref; ref = ref->next)
642 if (ref->type == REF_COMPONENT && ref->u.c.component->pointer)
645 if (expr2->symtree->n.sym->attr.pointer)
647 for (ref = expr2->ref; ref; ref = ref->next)
648 if (ref->type == REF_COMPONENT && ref->u.c.component->pointer)
651 /* Otherwise distinct symbols have no dependencies. */
658 /* Identical and disjoint ranges return 0,
659 overlapping ranges return 1. */
660 /* Return zero if we refer to the same full arrays. */
661 if (expr1->ref->type == REF_ARRAY && expr2->ref->type == REF_ARRAY)
662 return gfc_dep_resolver (expr1->ref, expr2->ref);
667 if (expr2->inline_noncopying_intrinsic)
669 /* Remember possible differences between elemental and
670 transformational functions. All functions inside a FORALL
672 for (actual = expr2->value.function.actual;
673 actual; actual = actual->next)
677 n = gfc_check_dependency (expr1, actual->expr, identical);
688 /* Probably ok in the majority of (constant) cases. */
697 /* Determines overlapping for two array sections. */
699 static gfc_dependency
700 gfc_check_section_vs_section (gfc_ref *lref, gfc_ref *rref, int n)
721 /* If they are the same range, return without more ado. */
722 if (gfc_is_same_range (&l_ar, &r_ar, n, 0))
723 return GFC_DEP_EQUAL;
725 l_start = l_ar.start[n];
727 l_stride = l_ar.stride[n];
729 r_start = r_ar.start[n];
731 r_stride = r_ar.stride[n];
733 /* If l_start is NULL take it from array specifier. */
734 if (NULL == l_start && IS_ARRAY_EXPLICIT (l_ar.as))
735 l_start = l_ar.as->lower[n];
736 /* If l_end is NULL take it from array specifier. */
737 if (NULL == l_end && IS_ARRAY_EXPLICIT (l_ar.as))
738 l_end = l_ar.as->upper[n];
740 /* If r_start is NULL take it from array specifier. */
741 if (NULL == r_start && IS_ARRAY_EXPLICIT (r_ar.as))
742 r_start = r_ar.as->lower[n];
743 /* If r_end is NULL take it from array specifier. */
744 if (NULL == r_end && IS_ARRAY_EXPLICIT (r_ar.as))
745 r_end = r_ar.as->upper[n];
747 /* Determine whether the l_stride is positive or negative. */
750 else if (l_stride->expr_type == EXPR_CONSTANT
751 && l_stride->ts.type == BT_INTEGER)
752 l_dir = mpz_sgn (l_stride->value.integer);
753 else if (l_start && l_end)
754 l_dir = gfc_dep_compare_expr (l_end, l_start);
758 /* Determine whether the r_stride is positive or negative. */
761 else if (r_stride->expr_type == EXPR_CONSTANT
762 && r_stride->ts.type == BT_INTEGER)
763 r_dir = mpz_sgn (r_stride->value.integer);
764 else if (r_start && r_end)
765 r_dir = gfc_dep_compare_expr (r_end, r_start);
769 /* The strides should never be zero. */
770 if (l_dir == 0 || r_dir == 0)
771 return GFC_DEP_OVERLAP;
773 /* Determine LHS upper and lower bounds. */
779 else if (l_dir == -1)
790 /* Determine RHS upper and lower bounds. */
796 else if (r_dir == -1)
807 /* Check whether the ranges are disjoint. */
808 if (l_upper && r_lower && gfc_dep_compare_expr (l_upper, r_lower) == -1)
809 return GFC_DEP_NODEP;
810 if (r_upper && l_lower && gfc_dep_compare_expr (r_upper, l_lower) == -1)
811 return GFC_DEP_NODEP;
813 /* Handle cases like x:y:1 vs. x:z:-1 as GFC_DEP_EQUAL. */
814 if (l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == 0)
816 if (l_dir == 1 && r_dir == -1)
817 return GFC_DEP_EQUAL;
818 if (l_dir == -1 && r_dir == 1)
819 return GFC_DEP_EQUAL;
822 /* Handle cases like x:y:1 vs. z:y:-1 as GFC_DEP_EQUAL. */
823 if (l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == 0)
825 if (l_dir == 1 && r_dir == -1)
826 return GFC_DEP_EQUAL;
827 if (l_dir == -1 && r_dir == 1)
828 return GFC_DEP_EQUAL;
831 /* Check for forward dependencies x:y vs. x+1:z. */
832 if (l_dir == 1 && r_dir == 1
833 && l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == -1
834 && l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == -1)
836 /* Check that the strides are the same. */
837 if (!l_stride && !r_stride)
838 return GFC_DEP_FORWARD;
839 if (l_stride && r_stride
840 && gfc_dep_compare_expr (l_stride, r_stride) == 0)
841 return GFC_DEP_FORWARD;
844 /* Check for forward dependencies x:y:-1 vs. x-1:z:-1. */
845 if (l_dir == -1 && r_dir == -1
846 && l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == 1
847 && l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == 1)
849 /* Check that the strides are the same. */
850 if (!l_stride && !r_stride)
851 return GFC_DEP_FORWARD;
852 if (l_stride && r_stride
853 && gfc_dep_compare_expr (l_stride, r_stride) == 0)
854 return GFC_DEP_FORWARD;
857 return GFC_DEP_OVERLAP;
861 /* Determines overlapping for a single element and a section. */
863 static gfc_dependency
864 gfc_check_element_vs_section( gfc_ref *lref, gfc_ref *rref, int n)
873 elem = lref->u.ar.start[n];
875 return GFC_DEP_OVERLAP;
878 start = ref->start[n] ;
880 stride = ref->stride[n];
882 if (!start && IS_ARRAY_EXPLICIT (ref->as))
883 start = ref->as->lower[n];
884 if (!end && IS_ARRAY_EXPLICIT (ref->as))
885 end = ref->as->upper[n];
887 /* Determine whether the stride is positive or negative. */
890 else if (stride->expr_type == EXPR_CONSTANT
891 && stride->ts.type == BT_INTEGER)
892 s = mpz_sgn (stride->value.integer);
896 /* Stride should never be zero. */
898 return GFC_DEP_OVERLAP;
900 /* Positive strides. */
903 /* Check for elem < lower. */
904 if (start && gfc_dep_compare_expr (elem, start) == -1)
905 return GFC_DEP_NODEP;
906 /* Check for elem > upper. */
907 if (end && gfc_dep_compare_expr (elem, end) == 1)
908 return GFC_DEP_NODEP;
912 s = gfc_dep_compare_expr (start, end);
913 /* Check for an empty range. */
915 return GFC_DEP_NODEP;
916 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
917 return GFC_DEP_EQUAL;
920 /* Negative strides. */
923 /* Check for elem > upper. */
924 if (end && gfc_dep_compare_expr (elem, start) == 1)
925 return GFC_DEP_NODEP;
926 /* Check for elem < lower. */
927 if (start && gfc_dep_compare_expr (elem, end) == -1)
928 return GFC_DEP_NODEP;
932 s = gfc_dep_compare_expr (start, end);
933 /* Check for an empty range. */
935 return GFC_DEP_NODEP;
936 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
937 return GFC_DEP_EQUAL;
940 /* Unknown strides. */
944 return GFC_DEP_OVERLAP;
945 s = gfc_dep_compare_expr (start, end);
947 return GFC_DEP_OVERLAP;
948 /* Assume positive stride. */
951 /* Check for elem < lower. */
952 if (gfc_dep_compare_expr (elem, start) == -1)
953 return GFC_DEP_NODEP;
954 /* Check for elem > upper. */
955 if (gfc_dep_compare_expr (elem, end) == 1)
956 return GFC_DEP_NODEP;
958 /* Assume negative stride. */
961 /* Check for elem > upper. */
962 if (gfc_dep_compare_expr (elem, start) == 1)
963 return GFC_DEP_NODEP;
964 /* Check for elem < lower. */
965 if (gfc_dep_compare_expr (elem, end) == -1)
966 return GFC_DEP_NODEP;
971 s = gfc_dep_compare_expr (elem, start);
973 return GFC_DEP_EQUAL;
974 if (s == 1 || s == -1)
975 return GFC_DEP_NODEP;
979 return GFC_DEP_OVERLAP;
983 /* Traverse expr, checking all EXPR_VARIABLE symbols for their
984 forall_index attribute. Return true if any variable may be
985 being used as a FORALL index. Its safe to pessimistically
986 return true, and assume a dependency. */
989 contains_forall_index_p (gfc_expr *expr)
991 gfc_actual_arglist *arg;
999 switch (expr->expr_type)
1002 if (expr->symtree->n.sym->forall_index)
1007 if (contains_forall_index_p (expr->value.op.op1)
1008 || contains_forall_index_p (expr->value.op.op2))
1013 for (arg = expr->value.function.actual; arg; arg = arg->next)
1014 if (contains_forall_index_p (arg->expr))
1020 case EXPR_SUBSTRING:
1023 case EXPR_STRUCTURE:
1025 for (c = expr->value.constructor; c; c = c->next)
1026 if (contains_forall_index_p (c->expr))
1034 for (ref = expr->ref; ref; ref = ref->next)
1038 for (i = 0; i < ref->u.ar.dimen; i++)
1039 if (contains_forall_index_p (ref->u.ar.start[i])
1040 || contains_forall_index_p (ref->u.ar.end[i])
1041 || contains_forall_index_p (ref->u.ar.stride[i]))
1049 if (contains_forall_index_p (ref->u.ss.start)
1050 || contains_forall_index_p (ref->u.ss.end))
1061 /* Determines overlapping for two single element array references. */
1063 static gfc_dependency
1064 gfc_check_element_vs_element (gfc_ref *lref, gfc_ref *rref, int n)
1074 l_start = l_ar.start[n] ;
1075 r_start = r_ar.start[n] ;
1076 i = gfc_dep_compare_expr (r_start, l_start);
1078 return GFC_DEP_EQUAL;
1080 /* Treat two scalar variables as potentially equal. This allows
1081 us to prove that a(i,:) and a(j,:) have no dependency. See
1082 Gerald Roth, "Evaluation of Array Syntax Dependence Analysis",
1083 Proceedings of the International Conference on Parallel and
1084 Distributed Processing Techniques and Applications (PDPTA2001),
1085 Las Vegas, Nevada, June 2001. */
1086 /* However, we need to be careful when either scalar expression
1087 contains a FORALL index, as these can potentially change value
1088 during the scalarization/traversal of this array reference. */
1089 if (contains_forall_index_p (r_start) || contains_forall_index_p (l_start))
1090 return GFC_DEP_OVERLAP;
1093 return GFC_DEP_NODEP;
1094 return GFC_DEP_EQUAL;
1098 /* Determine if an array ref, usually an array section specifies the
1102 gfc_full_array_ref_p (gfc_ref *ref)
1106 if (ref->type != REF_ARRAY)
1108 if (ref->u.ar.type == AR_FULL)
1110 if (ref->u.ar.type != AR_SECTION)
1115 for (i = 0; i < ref->u.ar.dimen; i++)
1117 /* Check the lower bound. */
1118 if (ref->u.ar.start[i]
1120 || !ref->u.ar.as->lower[i]
1121 || gfc_dep_compare_expr (ref->u.ar.start[i],
1122 ref->u.ar.as->lower[i])))
1124 /* Check the upper bound. */
1125 if (ref->u.ar.end[i]
1127 || !ref->u.ar.as->upper[i]
1128 || gfc_dep_compare_expr (ref->u.ar.end[i],
1129 ref->u.ar.as->upper[i])))
1131 /* Check the stride. */
1132 if (ref->u.ar.stride[i] && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
1139 /* Finds if two array references are overlapping or not.
1141 1 : array references are overlapping.
1142 0 : array references are identical or not overlapping. */
1145 gfc_dep_resolver (gfc_ref *lref, gfc_ref *rref)
1148 gfc_dependency fin_dep;
1149 gfc_dependency this_dep;
1151 fin_dep = GFC_DEP_ERROR;
1152 /* Dependencies due to pointers should already have been identified.
1153 We only need to check for overlapping array references. */
1155 while (lref && rref)
1157 /* We're resolving from the same base symbol, so both refs should be
1158 the same type. We traverse the reference chain intil we find ranges
1159 that are not equal. */
1160 gcc_assert (lref->type == rref->type);
1164 /* The two ranges can't overlap if they are from different
1166 if (lref->u.c.component != rref->u.c.component)
1171 /* Substring overlaps are handled by the string assignment code. */
1175 if (lref->u.ar.dimen != rref->u.ar.dimen)
1177 if (lref->u.ar.type == AR_FULL)
1178 fin_dep = gfc_full_array_ref_p (rref) ? GFC_DEP_EQUAL
1180 else if (rref->u.ar.type == AR_FULL)
1181 fin_dep = gfc_full_array_ref_p (lref) ? GFC_DEP_EQUAL
1188 for (n=0; n < lref->u.ar.dimen; n++)
1190 /* Assume dependency when either of array reference is vector
1192 if (lref->u.ar.dimen_type[n] == DIMEN_VECTOR
1193 || rref->u.ar.dimen_type[n] == DIMEN_VECTOR)
1195 if (lref->u.ar.dimen_type[n] == DIMEN_RANGE
1196 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
1197 this_dep = gfc_check_section_vs_section (lref, rref, n);
1198 else if (lref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1199 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
1200 this_dep = gfc_check_element_vs_section (lref, rref, n);
1201 else if (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1202 && lref->u.ar.dimen_type[n] == DIMEN_RANGE)
1203 this_dep = gfc_check_element_vs_section (rref, lref, n);
1206 gcc_assert (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1207 && lref->u.ar.dimen_type[n] == DIMEN_ELEMENT);
1208 this_dep = gfc_check_element_vs_element (rref, lref, n);
1211 /* If any dimension doesn't overlap, we have no dependency. */
1212 if (this_dep == GFC_DEP_NODEP)
1215 /* Overlap codes are in order of priority. We only need to
1216 know the worst one.*/
1217 if (this_dep > fin_dep)
1220 /* Exactly matching and forward overlapping ranges don't cause a
1222 if (fin_dep < GFC_DEP_OVERLAP)
1225 /* Keep checking. We only have a dependency if
1226 subsequent references also overlap. */
1236 /* If we haven't seen any array refs then something went wrong. */
1237 gcc_assert (fin_dep != GFC_DEP_ERROR);
1239 /* Assume the worst if we nest to different depths. */
1243 return fin_dep == GFC_DEP_OVERLAP;