2 Copyright (C) 2000, 2001, 2002, 2005, 2006, 2007, 2008, 2009, 2010
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. */
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. */
51 #define IS_ARRAY_EXPLICIT(as) ((as->type == AS_EXPLICIT ? 1 : 0))
53 /* Forward declarations */
55 static gfc_dependency check_section_vs_section (gfc_array_ref *,
56 gfc_array_ref *, int);
58 /* Returns 1 if the expr is an integer constant value 1, 0 if it is not or
59 def if the value could not be determined. */
62 gfc_expr_is_one (gfc_expr *expr, int def)
64 gcc_assert (expr != NULL);
66 if (expr->expr_type != EXPR_CONSTANT)
69 if (expr->ts.type != BT_INTEGER)
72 return mpz_cmp_si (expr->value.integer, 1) == 0;
75 /* Check if two array references are known to be identical. Calls
76 gfc_dep_compare_expr if necessary for comparing array indices. */
79 identical_array_ref (gfc_array_ref *a1, gfc_array_ref *a2)
83 if (a1->type == AR_FULL && a2->type == AR_FULL)
86 if (a1->type == AR_SECTION && a2->type == AR_SECTION)
88 gcc_assert (a1->dimen == a2->dimen);
90 for ( i = 0; i < a1->dimen; i++)
92 /* TODO: Currently, we punt on an integer array as an index. */
93 if (a1->dimen_type[i] != DIMEN_RANGE
94 || a2->dimen_type[i] != DIMEN_RANGE)
97 if (check_section_vs_section (a1, a2, i) != GFC_DEP_EQUAL)
103 if (a1->type == AR_ELEMENT && a2->type == AR_ELEMENT)
105 gcc_assert (a1->dimen == a2->dimen);
106 for (i = 0; i < a1->dimen; i++)
108 if (gfc_dep_compare_expr (a1->start[i], a2->start[i]) != 0)
118 /* Return true for identical variables, checking for references if
119 necessary. Calls identical_array_ref for checking array sections. */
122 gfc_are_identical_variables (gfc_expr *e1, gfc_expr *e2)
126 if (e1->symtree->n.sym != e2->symtree->n.sym)
129 /* Volatile variables should never compare equal to themselves. */
131 if (e1->symtree->n.sym->attr.volatile_)
137 while (r1 != NULL || r2 != NULL)
140 /* Assume the variables are not equal if one has a reference and the
142 TODO: Handle full references like comparing a(:) to a.
145 if (r1 == NULL || r2 == NULL)
148 if (r1->type != r2->type)
155 if (!identical_array_ref (&r1->u.ar, &r2->u.ar))
161 if (r1->u.c.component != r2->u.c.component)
166 if (gfc_dep_compare_expr (r1->u.ss.start, r2->u.ss.start) != 0
167 || gfc_dep_compare_expr (r1->u.ss.end, r2->u.ss.end) != 0)
172 gfc_internal_error ("gfc_are_identical_variables: Bad type");
180 /* Compare two values. Returns 0 if e1 == e2, -1 if e1 < e2, +1 if e1 > e2,
181 and -2 if the relationship could not be determined. */
184 gfc_dep_compare_expr (gfc_expr *e1, gfc_expr *e2)
186 gfc_actual_arglist *args1;
187 gfc_actual_arglist *args2;
194 /* Remove any integer conversion functions to larger types. */
195 if (e1->expr_type == EXPR_FUNCTION && e1->value.function.isym
196 && e1->value.function.isym->id == GFC_ISYM_CONVERSION
197 && e1->ts.type == BT_INTEGER)
199 args1 = e1->value.function.actual;
200 if (args1->expr->ts.type == BT_INTEGER
201 && e1->ts.kind > args1->expr->ts.kind)
205 if (e2->expr_type == EXPR_FUNCTION && e2->value.function.isym
206 && e2->value.function.isym->id == GFC_ISYM_CONVERSION
207 && e2->ts.type == BT_INTEGER)
209 args2 = e2->value.function.actual;
210 if (args2->expr->ts.type == BT_INTEGER
211 && e2->ts.kind > args2->expr->ts.kind)
218 return gfc_dep_compare_expr (n1, n2);
220 return gfc_dep_compare_expr (n1, e2);
225 return gfc_dep_compare_expr (e1, n2);
228 if (e1->expr_type == EXPR_OP
229 && (e1->value.op.op == INTRINSIC_UPLUS
230 || e1->value.op.op == INTRINSIC_PARENTHESES))
231 return gfc_dep_compare_expr (e1->value.op.op1, e2);
232 if (e2->expr_type == EXPR_OP
233 && (e2->value.op.op == INTRINSIC_UPLUS
234 || e2->value.op.op == INTRINSIC_PARENTHESES))
235 return gfc_dep_compare_expr (e1, e2->value.op.op1);
237 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_PLUS)
239 /* Compare X+C vs. X. */
240 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
241 && e1->value.op.op2->ts.type == BT_INTEGER
242 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
243 return mpz_sgn (e1->value.op.op2->value.integer);
245 /* Compare P+Q vs. R+S. */
246 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
250 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
251 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
252 if (l == 0 && r == 0)
254 if (l == 0 && r != -2)
256 if (l != -2 && r == 0)
258 if (l == 1 && r == 1)
260 if (l == -1 && r == -1)
263 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op2);
264 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op1);
265 if (l == 0 && r == 0)
267 if (l == 0 && r != -2)
269 if (l != -2 && r == 0)
271 if (l == 1 && r == 1)
273 if (l == -1 && r == -1)
278 /* Compare X vs. X+C. */
279 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
281 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
282 && e2->value.op.op2->ts.type == BT_INTEGER
283 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
284 return -mpz_sgn (e2->value.op.op2->value.integer);
287 /* Compare X-C vs. X. */
288 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_MINUS)
290 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
291 && e1->value.op.op2->ts.type == BT_INTEGER
292 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
293 return -mpz_sgn (e1->value.op.op2->value.integer);
295 /* Compare P-Q vs. R-S. */
296 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
300 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
301 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
302 if (l == 0 && r == 0)
304 if (l != -2 && r == 0)
306 if (l == 0 && r != -2)
308 if (l == 1 && r == -1)
310 if (l == -1 && r == 1)
315 /* Compare A // B vs. C // D. */
317 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_CONCAT
318 && e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_CONCAT)
322 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
323 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
330 /* Watch out for 'A ' // x vs. 'A' // x. */
331 gfc_expr *e1_left = e1->value.op.op1;
332 gfc_expr *e2_left = e2->value.op.op1;
334 if (e1_left->expr_type == EXPR_CONSTANT
335 && e2_left->expr_type == EXPR_CONSTANT
336 && e1_left->value.character.length
337 != e2_left->value.character.length)
351 /* Compare X vs. X-C. */
352 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
354 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
355 && e2->value.op.op2->ts.type == BT_INTEGER
356 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
357 return mpz_sgn (e2->value.op.op2->value.integer);
360 if (e1->expr_type != e2->expr_type)
363 switch (e1->expr_type)
366 /* Compare strings for equality. */
367 if (e1->ts.type == BT_CHARACTER && e2->ts.type == BT_CHARACTER)
368 return gfc_compare_string (e1, e2);
370 if (e1->ts.type != BT_INTEGER || e2->ts.type != BT_INTEGER)
373 i = mpz_cmp (e1->value.integer, e2->value.integer);
381 if (gfc_are_identical_variables (e1, e2))
387 /* Intrinsic operators are the same if their operands are the same. */
388 if (e1->value.op.op != e2->value.op.op)
390 if (e1->value.op.op2 == 0)
392 i = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
393 return i == 0 ? 0 : -2;
395 if (gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1) == 0
396 && gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2) == 0)
398 /* TODO Handle commutative binary operators here? */
403 /* PURE functions can be compared for argument equality. */
404 if ((e1->value.function.esym && e2->value.function.esym
405 && e1->value.function.esym == e2->value.function.esym
406 && e1->value.function.esym->result->attr.pure)
407 || (e1->value.function.isym && e2->value.function.isym
408 && e1->value.function.isym == e2->value.function.isym
409 && e1->value.function.isym->pure))
411 args1 = e1->value.function.actual;
412 args2 = e2->value.function.actual;
414 /* Compare the argument lists for equality. */
415 while (args1 && args2)
417 /* Bitwise xor, since C has no non-bitwise xor operator. */
418 if ((args1->expr == NULL) ^ (args2->expr == NULL))
421 if (args1->expr != NULL && args2->expr != NULL
422 && gfc_dep_compare_expr (args1->expr, args2->expr) != 0)
428 return (args1 || args2) ? -2 : 0;
440 /* Returns 1 if the two ranges are the same, 0 if they are not, and def
441 if the results are indeterminate. N is the dimension to compare. */
444 gfc_is_same_range (gfc_array_ref *ar1, gfc_array_ref *ar2, int n, int def)
450 /* TODO: More sophisticated range comparison. */
451 gcc_assert (ar1 && ar2);
453 gcc_assert (ar1->dimen_type[n] == ar2->dimen_type[n]);
457 /* Check for mismatching strides. A NULL stride means a stride of 1. */
460 i = gfc_expr_is_one (e1, -1);
468 i = gfc_expr_is_one (e2, -1);
476 i = gfc_dep_compare_expr (e1, e2);
482 /* The strides match. */
484 /* Check the range start. */
489 /* Use the bound of the array if no bound is specified. */
491 e1 = ar1->as->lower[n];
494 e2 = ar2->as->lower[n];
496 /* Check we have values for both. */
500 i = gfc_dep_compare_expr (e1, e2);
507 /* Check the range end. */
512 /* Use the bound of the array if no bound is specified. */
514 e1 = ar1->as->upper[n];
517 e2 = ar2->as->upper[n];
519 /* Check we have values for both. */
523 i = gfc_dep_compare_expr (e1, e2);
534 /* Some array-returning intrinsics can be implemented by reusing the
535 data from one of the array arguments. For example, TRANSPOSE does
536 not necessarily need to allocate new data: it can be implemented
537 by copying the original array's descriptor and simply swapping the
538 two dimension specifications.
540 If EXPR is a call to such an intrinsic, return the argument
541 whose data can be reused, otherwise return NULL. */
544 gfc_get_noncopying_intrinsic_argument (gfc_expr *expr)
546 if (expr->expr_type != EXPR_FUNCTION || !expr->value.function.isym)
549 switch (expr->value.function.isym->id)
551 case GFC_ISYM_TRANSPOSE:
552 return expr->value.function.actual->expr;
560 /* Return true if the result of reference REF can only be constructed
561 using a temporary array. */
564 gfc_ref_needs_temporary_p (gfc_ref *ref)
570 for (; ref; ref = ref->next)
574 /* Vector dimensions are generally not monotonic and must be
575 handled using a temporary. */
576 if (ref->u.ar.type == AR_SECTION)
577 for (n = 0; n < ref->u.ar.dimen; n++)
578 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR)
585 /* Within an array reference, character substrings generally
586 need a temporary. Character array strides are expressed as
587 multiples of the element size (consistent with other array
588 types), not in characters. */
600 gfc_is_data_pointer (gfc_expr *e)
604 if (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION)
607 /* No subreference if it is a function */
608 gcc_assert (e->expr_type == EXPR_VARIABLE || !e->ref);
610 if (e->symtree->n.sym->attr.pointer)
613 for (ref = e->ref; ref; ref = ref->next)
614 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
621 /* Return true if array variable VAR could be passed to the same function
622 as argument EXPR without interfering with EXPR. INTENT is the intent
625 This is considerably less conservative than other dependencies
626 because many function arguments will already be copied into a
630 gfc_check_argument_var_dependency (gfc_expr *var, sym_intent intent,
631 gfc_expr *expr, gfc_dep_check elemental)
635 gcc_assert (var->expr_type == EXPR_VARIABLE);
636 gcc_assert (var->rank > 0);
638 switch (expr->expr_type)
641 /* In case of elemental subroutines, there is no dependency
642 between two same-range array references. */
643 if (gfc_ref_needs_temporary_p (expr->ref)
644 || gfc_check_dependency (var, expr, elemental == NOT_ELEMENTAL))
646 if (elemental == ELEM_DONT_CHECK_VARIABLE)
648 /* Too many false positive with pointers. */
649 if (!gfc_is_data_pointer (var) && !gfc_is_data_pointer (expr))
651 /* Elemental procedures forbid unspecified intents,
652 and we don't check dependencies for INTENT_IN args. */
653 gcc_assert (intent == INTENT_OUT || intent == INTENT_INOUT);
655 /* We are told not to check dependencies.
656 We do it, however, and issue a warning in case we find one.
657 If a dependency is found in the case
658 elemental == ELEM_CHECK_VARIABLE, we will generate
659 a temporary, so we don't need to bother the user. */
660 gfc_warning ("INTENT(%s) actual argument at %L might "
661 "interfere with actual argument at %L.",
662 intent == INTENT_OUT ? "OUT" : "INOUT",
663 &var->where, &expr->where);
673 return gfc_check_dependency (var, expr, 1);
676 if (intent != INTENT_IN)
678 arg = gfc_get_noncopying_intrinsic_argument (expr);
680 return gfc_check_argument_var_dependency (var, intent, arg,
684 if (elemental != NOT_ELEMENTAL)
686 if ((expr->value.function.esym
687 && expr->value.function.esym->attr.elemental)
688 || (expr->value.function.isym
689 && expr->value.function.isym->elemental))
690 return gfc_check_fncall_dependency (var, intent, NULL,
691 expr->value.function.actual,
692 ELEM_CHECK_VARIABLE);
697 /* In case of non-elemental procedures, there is no need to catch
698 dependencies, as we will make a temporary anyway. */
701 /* If the actual arg EXPR is an expression, we need to catch
702 a dependency between variables in EXPR and VAR,
703 an intent((IN)OUT) variable. */
704 if (expr->value.op.op1
705 && gfc_check_argument_var_dependency (var, intent,
707 ELEM_CHECK_VARIABLE))
709 else if (expr->value.op.op2
710 && gfc_check_argument_var_dependency (var, intent,
712 ELEM_CHECK_VARIABLE))
723 /* Like gfc_check_argument_var_dependency, but extended to any
724 array expression OTHER, not just variables. */
727 gfc_check_argument_dependency (gfc_expr *other, sym_intent intent,
728 gfc_expr *expr, gfc_dep_check elemental)
730 switch (other->expr_type)
733 return gfc_check_argument_var_dependency (other, intent, expr, elemental);
736 other = gfc_get_noncopying_intrinsic_argument (other);
738 return gfc_check_argument_dependency (other, INTENT_IN, expr,
749 /* Like gfc_check_argument_dependency, but check all the arguments in ACTUAL.
750 FNSYM is the function being called, or NULL if not known. */
753 gfc_check_fncall_dependency (gfc_expr *other, sym_intent intent,
754 gfc_symbol *fnsym, gfc_actual_arglist *actual,
755 gfc_dep_check elemental)
757 gfc_formal_arglist *formal;
760 formal = fnsym ? fnsym->formal : NULL;
761 for (; actual; actual = actual->next, formal = formal ? formal->next : NULL)
765 /* Skip args which are not present. */
769 /* Skip other itself. */
773 /* Skip intent(in) arguments if OTHER itself is intent(in). */
774 if (formal && intent == INTENT_IN
775 && formal->sym->attr.intent == INTENT_IN)
778 if (gfc_check_argument_dependency (other, intent, expr, elemental))
786 /* Return 1 if e1 and e2 are equivalenced arrays, either
787 directly or indirectly; i.e., equivalence (a,b) for a and b
788 or equivalence (a,c),(b,c). This function uses the equiv_
789 lists, generated in trans-common(add_equivalences), that are
790 guaranteed to pick up indirect equivalences. We explicitly
791 check for overlap using the offset and length of the equivalence.
792 This function is symmetric.
793 TODO: This function only checks whether the full top-level
794 symbols overlap. An improved implementation could inspect
795 e1->ref and e2->ref to determine whether the actually accessed
796 portions of these variables/arrays potentially overlap. */
799 gfc_are_equivalenced_arrays (gfc_expr *e1, gfc_expr *e2)
802 gfc_equiv_info *s, *fl1, *fl2;
804 gcc_assert (e1->expr_type == EXPR_VARIABLE
805 && e2->expr_type == EXPR_VARIABLE);
807 if (!e1->symtree->n.sym->attr.in_equivalence
808 || !e2->symtree->n.sym->attr.in_equivalence|| !e1->rank || !e2->rank)
811 if (e1->symtree->n.sym->ns
812 && e1->symtree->n.sym->ns != gfc_current_ns)
813 l = e1->symtree->n.sym->ns->equiv_lists;
815 l = gfc_current_ns->equiv_lists;
817 /* Go through the equiv_lists and return 1 if the variables
818 e1 and e2 are members of the same group and satisfy the
819 requirement on their relative offsets. */
820 for (; l; l = l->next)
824 for (s = l->equiv; s; s = s->next)
826 if (s->sym == e1->symtree->n.sym)
832 if (s->sym == e2->symtree->n.sym)
842 /* Can these lengths be zero? */
843 if (fl1->length <= 0 || fl2->length <= 0)
845 /* These can't overlap if [f11,fl1+length] is before
846 [fl2,fl2+length], or [fl2,fl2+length] is before
847 [fl1,fl1+length], otherwise they do overlap. */
848 if (fl1->offset + fl1->length > fl2->offset
849 && fl2->offset + fl2->length > fl1->offset)
857 /* Return true if there is no possibility of aliasing because of a type
858 mismatch between all the possible pointer references and the
859 potential target. Note that this function is asymmetric in the
860 arguments and so must be called twice with the arguments exchanged. */
863 check_data_pointer_types (gfc_expr *expr1, gfc_expr *expr2)
869 bool seen_component_ref;
871 if (expr1->expr_type != EXPR_VARIABLE
872 || expr1->expr_type != EXPR_VARIABLE)
875 sym1 = expr1->symtree->n.sym;
876 sym2 = expr2->symtree->n.sym;
878 /* Keep it simple for now. */
879 if (sym1->ts.type == BT_DERIVED && sym2->ts.type == BT_DERIVED)
882 if (sym1->attr.pointer)
884 if (gfc_compare_types (&sym1->ts, &sym2->ts))
888 /* This is a conservative check on the components of the derived type
889 if no component references have been seen. Since we will not dig
890 into the components of derived type components, we play it safe by
891 returning false. First we check the reference chain and then, if
892 no component references have been seen, the components. */
893 seen_component_ref = false;
894 if (sym1->ts.type == BT_DERIVED)
896 for (ref1 = expr1->ref; ref1; ref1 = ref1->next)
898 if (ref1->type != REF_COMPONENT)
901 if (ref1->u.c.component->ts.type == BT_DERIVED)
904 if ((sym2->attr.pointer || ref1->u.c.component->attr.pointer)
905 && gfc_compare_types (&ref1->u.c.component->ts, &sym2->ts))
908 seen_component_ref = true;
912 if (sym1->ts.type == BT_DERIVED && !seen_component_ref)
914 for (cm1 = sym1->ts.u.derived->components; cm1; cm1 = cm1->next)
916 if (cm1->ts.type == BT_DERIVED)
919 if ((sym2->attr.pointer || cm1->attr.pointer)
920 && gfc_compare_types (&cm1->ts, &sym2->ts))
929 /* Return true if the statement body redefines the condition. Returns
930 true if expr2 depends on expr1. expr1 should be a single term
931 suitable for the lhs of an assignment. The IDENTICAL flag indicates
932 whether array references to the same symbol with identical range
933 references count as a dependency or not. Used for forall and where
934 statements. Also used with functions returning arrays without a
938 gfc_check_dependency (gfc_expr *expr1, gfc_expr *expr2, bool identical)
940 gfc_actual_arglist *actual;
944 gcc_assert (expr1->expr_type == EXPR_VARIABLE);
946 switch (expr2->expr_type)
949 n = gfc_check_dependency (expr1, expr2->value.op.op1, identical);
952 if (expr2->value.op.op2)
953 return gfc_check_dependency (expr1, expr2->value.op.op2, identical);
957 /* The interesting cases are when the symbols don't match. */
958 if (expr1->symtree->n.sym != expr2->symtree->n.sym)
960 gfc_typespec *ts1 = &expr1->symtree->n.sym->ts;
961 gfc_typespec *ts2 = &expr2->symtree->n.sym->ts;
963 /* Return 1 if expr1 and expr2 are equivalenced arrays. */
964 if (gfc_are_equivalenced_arrays (expr1, expr2))
967 /* Symbols can only alias if they have the same type. */
968 if (ts1->type != BT_UNKNOWN && ts2->type != BT_UNKNOWN
969 && ts1->type != BT_DERIVED && ts2->type != BT_DERIVED)
971 if (ts1->type != ts2->type || ts1->kind != ts2->kind)
975 /* If either variable is a pointer, assume the worst. */
976 /* TODO: -fassume-no-pointer-aliasing */
977 if (gfc_is_data_pointer (expr1) || gfc_is_data_pointer (expr2))
979 if (check_data_pointer_types (expr1, expr2)
980 && check_data_pointer_types (expr2, expr1))
987 gfc_symbol *sym1 = expr1->symtree->n.sym;
988 gfc_symbol *sym2 = expr2->symtree->n.sym;
989 if (sym1->attr.target && sym2->attr.target
990 && ((sym1->attr.dummy && !sym1->attr.contiguous
991 && (!sym1->attr.dimension
992 || sym2->as->type == AS_ASSUMED_SHAPE))
993 || (sym2->attr.dummy && !sym2->attr.contiguous
994 && (!sym2->attr.dimension
995 || sym2->as->type == AS_ASSUMED_SHAPE))))
999 /* Otherwise distinct symbols have no dependencies. */
1006 /* Identical and disjoint ranges return 0,
1007 overlapping ranges return 1. */
1008 if (expr1->ref && expr2->ref)
1009 return gfc_dep_resolver (expr1->ref, expr2->ref, NULL);
1014 if (gfc_get_noncopying_intrinsic_argument (expr2) != NULL)
1017 /* Remember possible differences between elemental and
1018 transformational functions. All functions inside a FORALL
1020 for (actual = expr2->value.function.actual;
1021 actual; actual = actual->next)
1025 n = gfc_check_dependency (expr1, actual->expr, identical);
1036 /* Loop through the array constructor's elements. */
1037 for (c = gfc_constructor_first (expr2->value.constructor);
1038 c; c = gfc_constructor_next (c))
1040 /* If this is an iterator, assume the worst. */
1043 /* Avoid recursion in the common case. */
1044 if (c->expr->expr_type == EXPR_CONSTANT)
1046 if (gfc_check_dependency (expr1, c->expr, 1))
1057 /* Determines overlapping for two array sections. */
1059 static gfc_dependency
1060 check_section_vs_section (gfc_array_ref *l_ar, gfc_array_ref *r_ar, int n)
1075 bool identical_strides;
1077 /* If they are the same range, return without more ado. */
1078 if (gfc_is_same_range (l_ar, r_ar, n, 0))
1079 return GFC_DEP_EQUAL;
1081 l_start = l_ar->start[n];
1082 l_end = l_ar->end[n];
1083 l_stride = l_ar->stride[n];
1085 r_start = r_ar->start[n];
1086 r_end = r_ar->end[n];
1087 r_stride = r_ar->stride[n];
1089 /* If l_start is NULL take it from array specifier. */
1090 if (NULL == l_start && IS_ARRAY_EXPLICIT (l_ar->as))
1091 l_start = l_ar->as->lower[n];
1092 /* If l_end is NULL take it from array specifier. */
1093 if (NULL == l_end && IS_ARRAY_EXPLICIT (l_ar->as))
1094 l_end = l_ar->as->upper[n];
1096 /* If r_start is NULL take it from array specifier. */
1097 if (NULL == r_start && IS_ARRAY_EXPLICIT (r_ar->as))
1098 r_start = r_ar->as->lower[n];
1099 /* If r_end is NULL take it from array specifier. */
1100 if (NULL == r_end && IS_ARRAY_EXPLICIT (r_ar->as))
1101 r_end = r_ar->as->upper[n];
1103 /* Determine whether the l_stride is positive or negative. */
1106 else if (l_stride->expr_type == EXPR_CONSTANT
1107 && l_stride->ts.type == BT_INTEGER)
1108 l_dir = mpz_sgn (l_stride->value.integer);
1109 else if (l_start && l_end)
1110 l_dir = gfc_dep_compare_expr (l_end, l_start);
1114 /* Determine whether the r_stride is positive or negative. */
1117 else if (r_stride->expr_type == EXPR_CONSTANT
1118 && r_stride->ts.type == BT_INTEGER)
1119 r_dir = mpz_sgn (r_stride->value.integer);
1120 else if (r_start && r_end)
1121 r_dir = gfc_dep_compare_expr (r_end, r_start);
1125 /* The strides should never be zero. */
1126 if (l_dir == 0 || r_dir == 0)
1127 return GFC_DEP_OVERLAP;
1129 /* Determine if the strides are equal. */
1134 identical_strides = gfc_dep_compare_expr (l_stride, r_stride) == 0;
1136 identical_strides = gfc_expr_is_one (l_stride, 0) == 1;
1141 identical_strides = gfc_expr_is_one (r_stride, 0) == 1;
1143 identical_strides = true;
1146 /* Determine LHS upper and lower bounds. */
1152 else if (l_dir == -1)
1163 /* Determine RHS upper and lower bounds. */
1169 else if (r_dir == -1)
1180 /* Check whether the ranges are disjoint. */
1181 if (l_upper && r_lower && gfc_dep_compare_expr (l_upper, r_lower) == -1)
1182 return GFC_DEP_NODEP;
1183 if (r_upper && l_lower && gfc_dep_compare_expr (r_upper, l_lower) == -1)
1184 return GFC_DEP_NODEP;
1186 /* Handle cases like x:y:1 vs. x:z:-1 as GFC_DEP_EQUAL. */
1187 if (l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == 0)
1189 if (l_dir == 1 && r_dir == -1)
1190 return GFC_DEP_EQUAL;
1191 if (l_dir == -1 && r_dir == 1)
1192 return GFC_DEP_EQUAL;
1195 /* Handle cases like x:y:1 vs. z:y:-1 as GFC_DEP_EQUAL. */
1196 if (l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == 0)
1198 if (l_dir == 1 && r_dir == -1)
1199 return GFC_DEP_EQUAL;
1200 if (l_dir == -1 && r_dir == 1)
1201 return GFC_DEP_EQUAL;
1204 /* Handle cases like x:y:2 vs. x+1:z:4 as GFC_DEP_NODEP.
1205 There is no dependency if the remainder of
1206 (l_start - r_start) / gcd(l_stride, r_stride) is
1209 - Handle cases where x is an expression.
1210 - Cases like a(1:4:2) = a(2:3) are still not handled.
1213 #define IS_CONSTANT_INTEGER(a) ((a) && ((a)->expr_type == EXPR_CONSTANT) \
1214 && (a)->ts.type == BT_INTEGER)
1216 if (IS_CONSTANT_INTEGER(l_start) && IS_CONSTANT_INTEGER(r_start)
1217 && IS_CONSTANT_INTEGER(l_stride) && IS_CONSTANT_INTEGER(r_stride))
1225 mpz_gcd (gcd, l_stride->value.integer, r_stride->value.integer);
1226 mpz_sub (tmp, l_start->value.integer, r_start->value.integer);
1228 mpz_fdiv_r (tmp, tmp, gcd);
1229 result = mpz_cmp_si (tmp, 0L);
1235 return GFC_DEP_NODEP;
1238 #undef IS_CONSTANT_INTEGER
1240 /* Check for forward dependencies x:y vs. x+1:z. */
1241 if (l_dir == 1 && r_dir == 1
1242 && l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == -1
1243 && l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == -1)
1245 if (identical_strides)
1246 return GFC_DEP_FORWARD;
1249 /* Check for forward dependencies x:y:-1 vs. x-1:z:-1. */
1250 if (l_dir == -1 && r_dir == -1
1251 && l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == 1
1252 && l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == 1)
1254 if (identical_strides)
1255 return GFC_DEP_FORWARD;
1259 if (identical_strides)
1262 if (l_start && IS_ARRAY_EXPLICIT (l_ar->as))
1265 /* Check for a(low:y:s) vs. a(z:a:s) where a has a lower bound
1266 of low, which is always at least a forward dependence. */
1269 && gfc_dep_compare_expr (l_start, l_ar->as->lower[n]) == 0)
1270 return GFC_DEP_FORWARD;
1272 /* Check for a(high:y:-s) vs. a(z:a:-s) where a has a higher bound
1273 of high, which is always at least a forward dependence. */
1276 && gfc_dep_compare_expr (l_start, l_ar->as->upper[n]) == 0)
1277 return GFC_DEP_FORWARD;
1280 /* From here, check for backwards dependencies. */
1281 /* x:y vs. x+1:z. */
1282 if (l_dir == 1 && r_dir == 1
1283 && l_start && r_start
1284 && gfc_dep_compare_expr (l_start, r_start) == 1
1286 && gfc_dep_compare_expr (l_end, r_end) == 1)
1287 return GFC_DEP_BACKWARD;
1289 /* x:y:-1 vs. x-1:z:-1. */
1290 if (l_dir == -1 && r_dir == -1
1291 && l_start && r_start
1292 && gfc_dep_compare_expr (l_start, r_start) == -1
1294 && gfc_dep_compare_expr (l_end, r_end) == -1)
1295 return GFC_DEP_BACKWARD;
1298 return GFC_DEP_OVERLAP;
1302 /* Determines overlapping for a single element and a section. */
1304 static gfc_dependency
1305 gfc_check_element_vs_section( gfc_ref *lref, gfc_ref *rref, int n)
1314 elem = lref->u.ar.start[n];
1316 return GFC_DEP_OVERLAP;
1319 start = ref->start[n] ;
1321 stride = ref->stride[n];
1323 if (!start && IS_ARRAY_EXPLICIT (ref->as))
1324 start = ref->as->lower[n];
1325 if (!end && IS_ARRAY_EXPLICIT (ref->as))
1326 end = ref->as->upper[n];
1328 /* Determine whether the stride is positive or negative. */
1331 else if (stride->expr_type == EXPR_CONSTANT
1332 && stride->ts.type == BT_INTEGER)
1333 s = mpz_sgn (stride->value.integer);
1337 /* Stride should never be zero. */
1339 return GFC_DEP_OVERLAP;
1341 /* Positive strides. */
1344 /* Check for elem < lower. */
1345 if (start && gfc_dep_compare_expr (elem, start) == -1)
1346 return GFC_DEP_NODEP;
1347 /* Check for elem > upper. */
1348 if (end && gfc_dep_compare_expr (elem, end) == 1)
1349 return GFC_DEP_NODEP;
1353 s = gfc_dep_compare_expr (start, end);
1354 /* Check for an empty range. */
1356 return GFC_DEP_NODEP;
1357 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1358 return GFC_DEP_EQUAL;
1361 /* Negative strides. */
1364 /* Check for elem > upper. */
1365 if (end && gfc_dep_compare_expr (elem, start) == 1)
1366 return GFC_DEP_NODEP;
1367 /* Check for elem < lower. */
1368 if (start && gfc_dep_compare_expr (elem, end) == -1)
1369 return GFC_DEP_NODEP;
1373 s = gfc_dep_compare_expr (start, end);
1374 /* Check for an empty range. */
1376 return GFC_DEP_NODEP;
1377 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1378 return GFC_DEP_EQUAL;
1381 /* Unknown strides. */
1385 return GFC_DEP_OVERLAP;
1386 s = gfc_dep_compare_expr (start, end);
1388 return GFC_DEP_OVERLAP;
1389 /* Assume positive stride. */
1392 /* Check for elem < lower. */
1393 if (gfc_dep_compare_expr (elem, start) == -1)
1394 return GFC_DEP_NODEP;
1395 /* Check for elem > upper. */
1396 if (gfc_dep_compare_expr (elem, end) == 1)
1397 return GFC_DEP_NODEP;
1399 /* Assume negative stride. */
1402 /* Check for elem > upper. */
1403 if (gfc_dep_compare_expr (elem, start) == 1)
1404 return GFC_DEP_NODEP;
1405 /* Check for elem < lower. */
1406 if (gfc_dep_compare_expr (elem, end) == -1)
1407 return GFC_DEP_NODEP;
1412 s = gfc_dep_compare_expr (elem, start);
1414 return GFC_DEP_EQUAL;
1415 if (s == 1 || s == -1)
1416 return GFC_DEP_NODEP;
1420 return GFC_DEP_OVERLAP;
1424 /* Traverse expr, checking all EXPR_VARIABLE symbols for their
1425 forall_index attribute. Return true if any variable may be
1426 being used as a FORALL index. Its safe to pessimistically
1427 return true, and assume a dependency. */
1430 contains_forall_index_p (gfc_expr *expr)
1432 gfc_actual_arglist *arg;
1440 switch (expr->expr_type)
1443 if (expr->symtree->n.sym->forall_index)
1448 if (contains_forall_index_p (expr->value.op.op1)
1449 || contains_forall_index_p (expr->value.op.op2))
1454 for (arg = expr->value.function.actual; arg; arg = arg->next)
1455 if (contains_forall_index_p (arg->expr))
1461 case EXPR_SUBSTRING:
1464 case EXPR_STRUCTURE:
1466 for (c = gfc_constructor_first (expr->value.constructor);
1467 c; gfc_constructor_next (c))
1468 if (contains_forall_index_p (c->expr))
1476 for (ref = expr->ref; ref; ref = ref->next)
1480 for (i = 0; i < ref->u.ar.dimen; i++)
1481 if (contains_forall_index_p (ref->u.ar.start[i])
1482 || contains_forall_index_p (ref->u.ar.end[i])
1483 || contains_forall_index_p (ref->u.ar.stride[i]))
1491 if (contains_forall_index_p (ref->u.ss.start)
1492 || contains_forall_index_p (ref->u.ss.end))
1503 /* Determines overlapping for two single element array references. */
1505 static gfc_dependency
1506 gfc_check_element_vs_element (gfc_ref *lref, gfc_ref *rref, int n)
1516 l_start = l_ar.start[n] ;
1517 r_start = r_ar.start[n] ;
1518 i = gfc_dep_compare_expr (r_start, l_start);
1520 return GFC_DEP_EQUAL;
1522 /* Treat two scalar variables as potentially equal. This allows
1523 us to prove that a(i,:) and a(j,:) have no dependency. See
1524 Gerald Roth, "Evaluation of Array Syntax Dependence Analysis",
1525 Proceedings of the International Conference on Parallel and
1526 Distributed Processing Techniques and Applications (PDPTA2001),
1527 Las Vegas, Nevada, June 2001. */
1528 /* However, we need to be careful when either scalar expression
1529 contains a FORALL index, as these can potentially change value
1530 during the scalarization/traversal of this array reference. */
1531 if (contains_forall_index_p (r_start) || contains_forall_index_p (l_start))
1532 return GFC_DEP_OVERLAP;
1535 return GFC_DEP_NODEP;
1536 return GFC_DEP_EQUAL;
1540 /* Determine if an array ref, usually an array section specifies the
1541 entire array. In addition, if the second, pointer argument is
1542 provided, the function will return true if the reference is
1543 contiguous; eg. (:, 1) gives true but (1,:) gives false. */
1546 gfc_full_array_ref_p (gfc_ref *ref, bool *contiguous)
1550 bool lbound_OK = true;
1551 bool ubound_OK = true;
1554 *contiguous = false;
1556 if (ref->type != REF_ARRAY)
1559 if (ref->u.ar.type == AR_FULL)
1566 if (ref->u.ar.type != AR_SECTION)
1571 for (i = 0; i < ref->u.ar.dimen; i++)
1573 /* If we have a single element in the reference, for the reference
1574 to be full, we need to ascertain that the array has a single
1575 element in this dimension and that we actually reference the
1577 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
1579 /* This is unconditionally a contiguous reference if all the
1580 remaining dimensions are elements. */
1584 for (n = i + 1; n < ref->u.ar.dimen; n++)
1585 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
1586 *contiguous = false;
1590 || !ref->u.ar.as->lower[i]
1591 || !ref->u.ar.as->upper[i]
1592 || gfc_dep_compare_expr (ref->u.ar.as->lower[i],
1593 ref->u.ar.as->upper[i])
1594 || !ref->u.ar.start[i]
1595 || gfc_dep_compare_expr (ref->u.ar.start[i],
1596 ref->u.ar.as->lower[i]))
1602 /* Check the lower bound. */
1603 if (ref->u.ar.start[i]
1605 || !ref->u.ar.as->lower[i]
1606 || gfc_dep_compare_expr (ref->u.ar.start[i],
1607 ref->u.ar.as->lower[i])))
1609 /* Check the upper bound. */
1610 if (ref->u.ar.end[i]
1612 || !ref->u.ar.as->upper[i]
1613 || gfc_dep_compare_expr (ref->u.ar.end[i],
1614 ref->u.ar.as->upper[i])))
1616 /* Check the stride. */
1617 if (ref->u.ar.stride[i]
1618 && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
1621 /* This is unconditionally a contiguous reference as long as all
1622 the subsequent dimensions are elements. */
1626 for (n = i + 1; n < ref->u.ar.dimen; n++)
1627 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
1628 *contiguous = false;
1631 if (!lbound_OK || !ubound_OK)
1638 /* Determine if a full array is the same as an array section with one
1639 variable limit. For this to be so, the strides must both be unity
1640 and one of either start == lower or end == upper must be true. */
1643 ref_same_as_full_array (gfc_ref *full_ref, gfc_ref *ref)
1646 bool upper_or_lower;
1648 if (full_ref->type != REF_ARRAY)
1650 if (full_ref->u.ar.type != AR_FULL)
1652 if (ref->type != REF_ARRAY)
1654 if (ref->u.ar.type != AR_SECTION)
1657 for (i = 0; i < ref->u.ar.dimen; i++)
1659 /* If we have a single element in the reference, we need to check
1660 that the array has a single element and that we actually reference
1661 the correct element. */
1662 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
1664 if (!full_ref->u.ar.as
1665 || !full_ref->u.ar.as->lower[i]
1666 || !full_ref->u.ar.as->upper[i]
1667 || gfc_dep_compare_expr (full_ref->u.ar.as->lower[i],
1668 full_ref->u.ar.as->upper[i])
1669 || !ref->u.ar.start[i]
1670 || gfc_dep_compare_expr (ref->u.ar.start[i],
1671 full_ref->u.ar.as->lower[i]))
1675 /* Check the strides. */
1676 if (full_ref->u.ar.stride[i] && !gfc_expr_is_one (full_ref->u.ar.stride[i], 0))
1678 if (ref->u.ar.stride[i] && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
1681 upper_or_lower = false;
1682 /* Check the lower bound. */
1683 if (ref->u.ar.start[i]
1685 && full_ref->u.ar.as->lower[i]
1686 && gfc_dep_compare_expr (ref->u.ar.start[i],
1687 full_ref->u.ar.as->lower[i]) == 0))
1688 upper_or_lower = true;
1689 /* Check the upper bound. */
1690 if (ref->u.ar.end[i]
1692 && full_ref->u.ar.as->upper[i]
1693 && gfc_dep_compare_expr (ref->u.ar.end[i],
1694 full_ref->u.ar.as->upper[i]) == 0))
1695 upper_or_lower = true;
1696 if (!upper_or_lower)
1703 /* Finds if two array references are overlapping or not.
1705 2 : array references are overlapping but reversal of one or
1706 more dimensions will clear the dependency.
1707 1 : array references are overlapping.
1708 0 : array references are identical or not overlapping. */
1711 gfc_dep_resolver (gfc_ref *lref, gfc_ref *rref, gfc_reverse *reverse)
1714 gfc_dependency fin_dep;
1715 gfc_dependency this_dep;
1717 this_dep = GFC_DEP_ERROR;
1718 fin_dep = GFC_DEP_ERROR;
1719 /* Dependencies due to pointers should already have been identified.
1720 We only need to check for overlapping array references. */
1722 while (lref && rref)
1724 /* We're resolving from the same base symbol, so both refs should be
1725 the same type. We traverse the reference chain until we find ranges
1726 that are not equal. */
1727 gcc_assert (lref->type == rref->type);
1731 /* The two ranges can't overlap if they are from different
1733 if (lref->u.c.component != rref->u.c.component)
1738 /* Substring overlaps are handled by the string assignment code
1739 if there is not an underlying dependency. */
1740 return (fin_dep == GFC_DEP_OVERLAP) ? 1 : 0;
1744 if (ref_same_as_full_array (lref, rref))
1747 if (ref_same_as_full_array (rref, lref))
1750 if (lref->u.ar.dimen != rref->u.ar.dimen)
1752 if (lref->u.ar.type == AR_FULL)
1753 fin_dep = gfc_full_array_ref_p (rref, NULL) ? GFC_DEP_EQUAL
1755 else if (rref->u.ar.type == AR_FULL)
1756 fin_dep = gfc_full_array_ref_p (lref, NULL) ? GFC_DEP_EQUAL
1763 for (n=0; n < lref->u.ar.dimen; n++)
1765 /* Assume dependency when either of array reference is vector
1767 if (lref->u.ar.dimen_type[n] == DIMEN_VECTOR
1768 || rref->u.ar.dimen_type[n] == DIMEN_VECTOR)
1771 if (lref->u.ar.dimen_type[n] == DIMEN_RANGE
1772 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
1773 this_dep = check_section_vs_section (&lref->u.ar, &rref->u.ar, n);
1774 else if (lref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1775 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
1776 this_dep = gfc_check_element_vs_section (lref, rref, n);
1777 else if (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1778 && lref->u.ar.dimen_type[n] == DIMEN_RANGE)
1779 this_dep = gfc_check_element_vs_section (rref, lref, n);
1782 gcc_assert (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1783 && lref->u.ar.dimen_type[n] == DIMEN_ELEMENT);
1784 this_dep = gfc_check_element_vs_element (rref, lref, n);
1787 /* If any dimension doesn't overlap, we have no dependency. */
1788 if (this_dep == GFC_DEP_NODEP)
1791 /* Now deal with the loop reversal logic: This only works on
1792 ranges and is activated by setting
1793 reverse[n] == GFC_CAN_REVERSE
1794 The ability to reverse or not is set by previous conditions
1795 in this dimension. If reversal is not activated, the
1796 value GFC_DEP_BACKWARD is reset to GFC_DEP_OVERLAP. */
1797 if (rref->u.ar.dimen_type[n] == DIMEN_RANGE
1798 && lref->u.ar.dimen_type[n] == DIMEN_RANGE)
1800 /* Set reverse if backward dependence and not inhibited. */
1801 if (reverse && reverse[n] != GFC_CANNOT_REVERSE)
1802 reverse[n] = (this_dep == GFC_DEP_BACKWARD) ?
1803 GFC_REVERSE_SET : reverse[n];
1805 /* Inhibit loop reversal if dependence not compatible. */
1806 if (reverse && reverse[n] != GFC_REVERSE_NOT_SET
1807 && this_dep != GFC_DEP_EQUAL
1808 && this_dep != GFC_DEP_BACKWARD
1809 && this_dep != GFC_DEP_NODEP)
1811 reverse[n] = GFC_CANNOT_REVERSE;
1812 if (this_dep != GFC_DEP_FORWARD)
1813 this_dep = GFC_DEP_OVERLAP;
1816 /* If no intention of reversing or reversing is explicitly
1817 inhibited, convert backward dependence to overlap. */
1818 if (this_dep == GFC_DEP_BACKWARD
1819 && (reverse == NULL || reverse[n] == GFC_CANNOT_REVERSE))
1820 this_dep = GFC_DEP_OVERLAP;
1823 /* Overlap codes are in order of priority. We only need to
1824 know the worst one.*/
1825 if (this_dep > fin_dep)
1829 /* If this is an equal element, we have to keep going until we find
1830 the "real" array reference. */
1831 if (lref->u.ar.type == AR_ELEMENT
1832 && rref->u.ar.type == AR_ELEMENT
1833 && fin_dep == GFC_DEP_EQUAL)
1836 /* Exactly matching and forward overlapping ranges don't cause a
1838 if (fin_dep < GFC_DEP_BACKWARD)
1841 /* Keep checking. We only have a dependency if
1842 subsequent references also overlap. */
1852 /* If we haven't seen any array refs then something went wrong. */
1853 gcc_assert (fin_dep != GFC_DEP_ERROR);
1855 /* Assume the worst if we nest to different depths. */
1859 return fin_dep == GFC_DEP_OVERLAP;