Remove outermost loop parameter.
[official-gcc/graphite-test-results.git] / gcc / fortran / dependency.c
blob87f60df8e2a4a01a970ad2c25513fec3a2958463
1 /* Dependency analysis
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
11 version.
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
16 for more details.
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. */
27 #include "config.h"
28 #include "system.h"
29 #include "gfortran.h"
30 #include "dependency.h"
31 #include "constructor.h"
33 /* static declarations */
34 /* Enums */
35 enum range {LHS, RHS, MID};
37 /* Dependency types. These must be in reverse order of priority. */
38 typedef enum
40 GFC_DEP_ERROR,
41 GFC_DEP_EQUAL, /* Identical Ranges. */
42 GFC_DEP_FORWARD, /* e.g., a(1:3), a(2:4). */
43 GFC_DEP_OVERLAP, /* May overlap in some other way. */
44 GFC_DEP_NODEP /* Distinct ranges. */
46 gfc_dependency;
48 /* Macros */
49 #define IS_ARRAY_EXPLICIT(as) ((as->type == AS_EXPLICIT ? 1 : 0))
52 /* Returns 1 if the expr is an integer constant value 1, 0 if it is not or
53 def if the value could not be determined. */
55 int
56 gfc_expr_is_one (gfc_expr *expr, int def)
58 gcc_assert (expr != NULL);
60 if (expr->expr_type != EXPR_CONSTANT)
61 return def;
63 if (expr->ts.type != BT_INTEGER)
64 return def;
66 return mpz_cmp_si (expr->value.integer, 1) == 0;
70 /* Compare two values. Returns 0 if e1 == e2, -1 if e1 < e2, +1 if e1 > e2,
71 and -2 if the relationship could not be determined. */
73 int
74 gfc_dep_compare_expr (gfc_expr *e1, gfc_expr *e2)
76 gfc_actual_arglist *args1;
77 gfc_actual_arglist *args2;
78 int i;
80 if (e1->expr_type == EXPR_OP
81 && (e1->value.op.op == INTRINSIC_UPLUS
82 || e1->value.op.op == INTRINSIC_PARENTHESES))
83 return gfc_dep_compare_expr (e1->value.op.op1, e2);
84 if (e2->expr_type == EXPR_OP
85 && (e2->value.op.op == INTRINSIC_UPLUS
86 || e2->value.op.op == INTRINSIC_PARENTHESES))
87 return gfc_dep_compare_expr (e1, e2->value.op.op1);
89 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_PLUS)
91 /* Compare X+C vs. X. */
92 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
93 && e1->value.op.op2->ts.type == BT_INTEGER
94 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
95 return mpz_sgn (e1->value.op.op2->value.integer);
97 /* Compare P+Q vs. R+S. */
98 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
100 int l, r;
102 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
103 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
104 if (l == 0 && r == 0)
105 return 0;
106 if (l == 0 && r != -2)
107 return r;
108 if (l != -2 && r == 0)
109 return l;
110 if (l == 1 && r == 1)
111 return 1;
112 if (l == -1 && r == -1)
113 return -1;
115 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op2);
116 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op1);
117 if (l == 0 && r == 0)
118 return 0;
119 if (l == 0 && r != -2)
120 return r;
121 if (l != -2 && r == 0)
122 return l;
123 if (l == 1 && r == 1)
124 return 1;
125 if (l == -1 && r == -1)
126 return -1;
130 /* Compare X vs. X+C. */
131 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
133 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
134 && e2->value.op.op2->ts.type == BT_INTEGER
135 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
136 return -mpz_sgn (e2->value.op.op2->value.integer);
139 /* Compare X-C vs. X. */
140 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_MINUS)
142 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
143 && e1->value.op.op2->ts.type == BT_INTEGER
144 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
145 return -mpz_sgn (e1->value.op.op2->value.integer);
147 /* Compare P-Q vs. R-S. */
148 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
150 int l, r;
152 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
153 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
154 if (l == 0 && r == 0)
155 return 0;
156 if (l != -2 && r == 0)
157 return l;
158 if (l == 0 && r != -2)
159 return -r;
160 if (l == 1 && r == -1)
161 return 1;
162 if (l == -1 && r == 1)
163 return -1;
167 /* Compare X vs. X-C. */
168 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
170 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
171 && e2->value.op.op2->ts.type == BT_INTEGER
172 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
173 return mpz_sgn (e2->value.op.op2->value.integer);
176 if (e1->expr_type != e2->expr_type)
177 return -2;
179 switch (e1->expr_type)
181 case EXPR_CONSTANT:
182 if (e1->ts.type != BT_INTEGER || e2->ts.type != BT_INTEGER)
183 return -2;
185 i = mpz_cmp (e1->value.integer, e2->value.integer);
186 if (i == 0)
187 return 0;
188 else if (i < 0)
189 return -1;
190 return 1;
192 case EXPR_VARIABLE:
193 if (e1->ref || e2->ref)
194 return -2;
195 if (e1->symtree->n.sym == e2->symtree->n.sym)
196 return 0;
197 return -2;
199 case EXPR_OP:
200 /* Intrinsic operators are the same if their operands are the same. */
201 if (e1->value.op.op != e2->value.op.op)
202 return -2;
203 if (e1->value.op.op2 == 0)
205 i = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
206 return i == 0 ? 0 : -2;
208 if (gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1) == 0
209 && gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2) == 0)
210 return 0;
211 /* TODO Handle commutative binary operators here? */
212 return -2;
214 case EXPR_FUNCTION:
215 /* We can only compare calls to the same intrinsic function. */
216 if (e1->value.function.isym == 0 || e2->value.function.isym == 0
217 || e1->value.function.isym != e2->value.function.isym)
218 return -2;
220 args1 = e1->value.function.actual;
221 args2 = e2->value.function.actual;
223 /* We should list the "constant" intrinsic functions. Those
224 without side-effects that provide equal results given equal
225 argument lists. */
226 switch (e1->value.function.isym->id)
228 case GFC_ISYM_CONVERSION:
229 /* Handle integer extensions specially, as __convert_i4_i8
230 is not only "constant" but also "unary" and "increasing". */
231 if (args1 && !args1->next
232 && args2 && !args2->next
233 && e1->ts.type == BT_INTEGER
234 && args1->expr->ts.type == BT_INTEGER
235 && e1->ts.kind > args1->expr->ts.kind
236 && e2->ts.type == e1->ts.type
237 && e2->ts.kind == e1->ts.kind
238 && args2->expr->ts.type == args1->expr->ts.type
239 && args2->expr->ts.kind == args2->expr->ts.kind)
240 return gfc_dep_compare_expr (args1->expr, args2->expr);
241 break;
243 case GFC_ISYM_REAL:
244 case GFC_ISYM_LOGICAL:
245 case GFC_ISYM_DBLE:
246 break;
248 default:
249 return -2;
252 /* Compare the argument lists for equality. */
253 while (args1 && args2)
255 if (gfc_dep_compare_expr (args1->expr, args2->expr) != 0)
256 return -2;
257 args1 = args1->next;
258 args2 = args2->next;
260 return (args1 || args2) ? -2 : 0;
262 default:
263 return -2;
268 /* Returns 1 if the two ranges are the same, 0 if they are not, and def
269 if the results are indeterminate. N is the dimension to compare. */
272 gfc_is_same_range (gfc_array_ref *ar1, gfc_array_ref *ar2, int n, int def)
274 gfc_expr *e1;
275 gfc_expr *e2;
276 int i;
278 /* TODO: More sophisticated range comparison. */
279 gcc_assert (ar1 && ar2);
281 gcc_assert (ar1->dimen_type[n] == ar2->dimen_type[n]);
283 e1 = ar1->stride[n];
284 e2 = ar2->stride[n];
285 /* Check for mismatching strides. A NULL stride means a stride of 1. */
286 if (e1 && !e2)
288 i = gfc_expr_is_one (e1, -1);
289 if (i == -1)
290 return def;
291 else if (i == 0)
292 return 0;
294 else if (e2 && !e1)
296 i = gfc_expr_is_one (e2, -1);
297 if (i == -1)
298 return def;
299 else if (i == 0)
300 return 0;
302 else if (e1 && e2)
304 i = gfc_dep_compare_expr (e1, e2);
305 if (i == -2)
306 return def;
307 else if (i != 0)
308 return 0;
310 /* The strides match. */
312 /* Check the range start. */
313 e1 = ar1->start[n];
314 e2 = ar2->start[n];
315 if (e1 || e2)
317 /* Use the bound of the array if no bound is specified. */
318 if (ar1->as && !e1)
319 e1 = ar1->as->lower[n];
321 if (ar2->as && !e2)
322 e2 = ar2->as->lower[n];
324 /* Check we have values for both. */
325 if (!(e1 && e2))
326 return def;
328 i = gfc_dep_compare_expr (e1, e2);
329 if (i == -2)
330 return def;
331 else if (i != 0)
332 return 0;
335 /* Check the range end. */
336 e1 = ar1->end[n];
337 e2 = ar2->end[n];
338 if (e1 || e2)
340 /* Use the bound of the array if no bound is specified. */
341 if (ar1->as && !e1)
342 e1 = ar1->as->upper[n];
344 if (ar2->as && !e2)
345 e2 = ar2->as->upper[n];
347 /* Check we have values for both. */
348 if (!(e1 && e2))
349 return def;
351 i = gfc_dep_compare_expr (e1, e2);
352 if (i == -2)
353 return def;
354 else if (i != 0)
355 return 0;
358 return 1;
362 /* Some array-returning intrinsics can be implemented by reusing the
363 data from one of the array arguments. For example, TRANSPOSE does
364 not necessarily need to allocate new data: it can be implemented
365 by copying the original array's descriptor and simply swapping the
366 two dimension specifications.
368 If EXPR is a call to such an intrinsic, return the argument
369 whose data can be reused, otherwise return NULL. */
371 gfc_expr *
372 gfc_get_noncopying_intrinsic_argument (gfc_expr *expr)
374 if (expr->expr_type != EXPR_FUNCTION || !expr->value.function.isym)
375 return NULL;
377 switch (expr->value.function.isym->id)
379 case GFC_ISYM_TRANSPOSE:
380 return expr->value.function.actual->expr;
382 default:
383 return NULL;
388 /* Return true if the result of reference REF can only be constructed
389 using a temporary array. */
391 bool
392 gfc_ref_needs_temporary_p (gfc_ref *ref)
394 int n;
395 bool subarray_p;
397 subarray_p = false;
398 for (; ref; ref = ref->next)
399 switch (ref->type)
401 case REF_ARRAY:
402 /* Vector dimensions are generally not monotonic and must be
403 handled using a temporary. */
404 if (ref->u.ar.type == AR_SECTION)
405 for (n = 0; n < ref->u.ar.dimen; n++)
406 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR)
407 return true;
409 subarray_p = true;
410 break;
412 case REF_SUBSTRING:
413 /* Within an array reference, character substrings generally
414 need a temporary. Character array strides are expressed as
415 multiples of the element size (consistent with other array
416 types), not in characters. */
417 return subarray_p;
419 case REF_COMPONENT:
420 break;
423 return false;
428 gfc_is_data_pointer (gfc_expr *e)
430 gfc_ref *ref;
432 if (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION)
433 return 0;
435 /* No subreference if it is a function */
436 gcc_assert (e->expr_type == EXPR_VARIABLE || !e->ref);
438 if (e->symtree->n.sym->attr.pointer)
439 return 1;
441 for (ref = e->ref; ref; ref = ref->next)
442 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
443 return 1;
445 return 0;
449 /* Return true if array variable VAR could be passed to the same function
450 as argument EXPR without interfering with EXPR. INTENT is the intent
451 of VAR.
453 This is considerably less conservative than other dependencies
454 because many function arguments will already be copied into a
455 temporary. */
457 static int
458 gfc_check_argument_var_dependency (gfc_expr *var, sym_intent intent,
459 gfc_expr *expr, gfc_dep_check elemental)
461 gfc_expr *arg;
463 gcc_assert (var->expr_type == EXPR_VARIABLE);
464 gcc_assert (var->rank > 0);
466 switch (expr->expr_type)
468 case EXPR_VARIABLE:
469 /* In case of elemental subroutines, there is no dependency
470 between two same-range array references. */
471 if (gfc_ref_needs_temporary_p (expr->ref)
472 || gfc_check_dependency (var, expr, elemental == NOT_ELEMENTAL))
474 if (elemental == ELEM_DONT_CHECK_VARIABLE)
476 /* Too many false positive with pointers. */
477 if (!gfc_is_data_pointer (var) && !gfc_is_data_pointer (expr))
479 /* Elemental procedures forbid unspecified intents,
480 and we don't check dependencies for INTENT_IN args. */
481 gcc_assert (intent == INTENT_OUT || intent == INTENT_INOUT);
483 /* We are told not to check dependencies.
484 We do it, however, and issue a warning in case we find one.
485 If a dependency is found in the case
486 elemental == ELEM_CHECK_VARIABLE, we will generate
487 a temporary, so we don't need to bother the user. */
488 gfc_warning ("INTENT(%s) actual argument at %L might "
489 "interfere with actual argument at %L.",
490 intent == INTENT_OUT ? "OUT" : "INOUT",
491 &var->where, &expr->where);
493 return 0;
495 else
496 return 1;
498 return 0;
500 case EXPR_ARRAY:
501 return gfc_check_dependency (var, expr, 1);
503 case EXPR_FUNCTION:
504 if (intent != INTENT_IN && expr->inline_noncopying_intrinsic
505 && (arg = gfc_get_noncopying_intrinsic_argument (expr))
506 && gfc_check_argument_var_dependency (var, intent, arg, elemental))
507 return 1;
508 if (elemental)
510 if ((expr->value.function.esym
511 && expr->value.function.esym->attr.elemental)
512 || (expr->value.function.isym
513 && expr->value.function.isym->elemental))
514 return gfc_check_fncall_dependency (var, intent, NULL,
515 expr->value.function.actual,
516 ELEM_CHECK_VARIABLE);
518 return 0;
520 case EXPR_OP:
521 /* In case of non-elemental procedures, there is no need to catch
522 dependencies, as we will make a temporary anyway. */
523 if (elemental)
525 /* If the actual arg EXPR is an expression, we need to catch
526 a dependency between variables in EXPR and VAR,
527 an intent((IN)OUT) variable. */
528 if (expr->value.op.op1
529 && gfc_check_argument_var_dependency (var, intent,
530 expr->value.op.op1,
531 ELEM_CHECK_VARIABLE))
532 return 1;
533 else if (expr->value.op.op2
534 && gfc_check_argument_var_dependency (var, intent,
535 expr->value.op.op2,
536 ELEM_CHECK_VARIABLE))
537 return 1;
539 return 0;
541 default:
542 return 0;
547 /* Like gfc_check_argument_var_dependency, but extended to any
548 array expression OTHER, not just variables. */
550 static int
551 gfc_check_argument_dependency (gfc_expr *other, sym_intent intent,
552 gfc_expr *expr, gfc_dep_check elemental)
554 switch (other->expr_type)
556 case EXPR_VARIABLE:
557 return gfc_check_argument_var_dependency (other, intent, expr, elemental);
559 case EXPR_FUNCTION:
560 if (other->inline_noncopying_intrinsic)
562 other = gfc_get_noncopying_intrinsic_argument (other);
563 return gfc_check_argument_dependency (other, INTENT_IN, expr,
564 elemental);
566 return 0;
568 default:
569 return 0;
574 /* Like gfc_check_argument_dependency, but check all the arguments in ACTUAL.
575 FNSYM is the function being called, or NULL if not known. */
578 gfc_check_fncall_dependency (gfc_expr *other, sym_intent intent,
579 gfc_symbol *fnsym, gfc_actual_arglist *actual,
580 gfc_dep_check elemental)
582 gfc_formal_arglist *formal;
583 gfc_expr *expr;
585 formal = fnsym ? fnsym->formal : NULL;
586 for (; actual; actual = actual->next, formal = formal ? formal->next : NULL)
588 expr = actual->expr;
590 /* Skip args which are not present. */
591 if (!expr)
592 continue;
594 /* Skip other itself. */
595 if (expr == other)
596 continue;
598 /* Skip intent(in) arguments if OTHER itself is intent(in). */
599 if (formal && intent == INTENT_IN
600 && formal->sym->attr.intent == INTENT_IN)
601 continue;
603 if (gfc_check_argument_dependency (other, intent, expr, elemental))
604 return 1;
607 return 0;
611 /* Return 1 if e1 and e2 are equivalenced arrays, either
612 directly or indirectly; i.e., equivalence (a,b) for a and b
613 or equivalence (a,c),(b,c). This function uses the equiv_
614 lists, generated in trans-common(add_equivalences), that are
615 guaranteed to pick up indirect equivalences. We explicitly
616 check for overlap using the offset and length of the equivalence.
617 This function is symmetric.
618 TODO: This function only checks whether the full top-level
619 symbols overlap. An improved implementation could inspect
620 e1->ref and e2->ref to determine whether the actually accessed
621 portions of these variables/arrays potentially overlap. */
624 gfc_are_equivalenced_arrays (gfc_expr *e1, gfc_expr *e2)
626 gfc_equiv_list *l;
627 gfc_equiv_info *s, *fl1, *fl2;
629 gcc_assert (e1->expr_type == EXPR_VARIABLE
630 && e2->expr_type == EXPR_VARIABLE);
632 if (!e1->symtree->n.sym->attr.in_equivalence
633 || !e2->symtree->n.sym->attr.in_equivalence|| !e1->rank || !e2->rank)
634 return 0;
636 if (e1->symtree->n.sym->ns
637 && e1->symtree->n.sym->ns != gfc_current_ns)
638 l = e1->symtree->n.sym->ns->equiv_lists;
639 else
640 l = gfc_current_ns->equiv_lists;
642 /* Go through the equiv_lists and return 1 if the variables
643 e1 and e2 are members of the same group and satisfy the
644 requirement on their relative offsets. */
645 for (; l; l = l->next)
647 fl1 = NULL;
648 fl2 = NULL;
649 for (s = l->equiv; s; s = s->next)
651 if (s->sym == e1->symtree->n.sym)
653 fl1 = s;
654 if (fl2)
655 break;
657 if (s->sym == e2->symtree->n.sym)
659 fl2 = s;
660 if (fl1)
661 break;
665 if (s)
667 /* Can these lengths be zero? */
668 if (fl1->length <= 0 || fl2->length <= 0)
669 return 1;
670 /* These can't overlap if [f11,fl1+length] is before
671 [fl2,fl2+length], or [fl2,fl2+length] is before
672 [fl1,fl1+length], otherwise they do overlap. */
673 if (fl1->offset + fl1->length > fl2->offset
674 && fl2->offset + fl2->length > fl1->offset)
675 return 1;
678 return 0;
682 /* Return true if there is no possibility of aliasing because of a type
683 mismatch between all the possible pointer references and the
684 potential target. Note that this function is asymmetric in the
685 arguments and so must be called twice with the arguments exchanged. */
687 static bool
688 check_data_pointer_types (gfc_expr *expr1, gfc_expr *expr2)
690 gfc_component *cm1;
691 gfc_symbol *sym1;
692 gfc_symbol *sym2;
693 gfc_ref *ref1;
694 bool seen_component_ref;
696 if (expr1->expr_type != EXPR_VARIABLE
697 || expr1->expr_type != EXPR_VARIABLE)
698 return false;
700 sym1 = expr1->symtree->n.sym;
701 sym2 = expr2->symtree->n.sym;
703 /* Keep it simple for now. */
704 if (sym1->ts.type == BT_DERIVED && sym2->ts.type == BT_DERIVED)
705 return false;
707 if (sym1->attr.pointer)
709 if (gfc_compare_types (&sym1->ts, &sym2->ts))
710 return false;
713 /* This is a conservative check on the components of the derived type
714 if no component references have been seen. Since we will not dig
715 into the components of derived type components, we play it safe by
716 returning false. First we check the reference chain and then, if
717 no component references have been seen, the components. */
718 seen_component_ref = false;
719 if (sym1->ts.type == BT_DERIVED)
721 for (ref1 = expr1->ref; ref1; ref1 = ref1->next)
723 if (ref1->type != REF_COMPONENT)
724 continue;
726 if (ref1->u.c.component->ts.type == BT_DERIVED)
727 return false;
729 if ((sym2->attr.pointer || ref1->u.c.component->attr.pointer)
730 && gfc_compare_types (&ref1->u.c.component->ts, &sym2->ts))
731 return false;
733 seen_component_ref = true;
737 if (sym1->ts.type == BT_DERIVED && !seen_component_ref)
739 for (cm1 = sym1->ts.u.derived->components; cm1; cm1 = cm1->next)
741 if (cm1->ts.type == BT_DERIVED)
742 return false;
744 if ((sym2->attr.pointer || cm1->attr.pointer)
745 && gfc_compare_types (&cm1->ts, &sym2->ts))
746 return false;
750 return true;
754 /* Return true if the statement body redefines the condition. Returns
755 true if expr2 depends on expr1. expr1 should be a single term
756 suitable for the lhs of an assignment. The IDENTICAL flag indicates
757 whether array references to the same symbol with identical range
758 references count as a dependency or not. Used for forall and where
759 statements. Also used with functions returning arrays without a
760 temporary. */
763 gfc_check_dependency (gfc_expr *expr1, gfc_expr *expr2, bool identical)
765 gfc_actual_arglist *actual;
766 gfc_constructor *c;
767 int n;
769 gcc_assert (expr1->expr_type == EXPR_VARIABLE);
771 switch (expr2->expr_type)
773 case EXPR_OP:
774 n = gfc_check_dependency (expr1, expr2->value.op.op1, identical);
775 if (n)
776 return n;
777 if (expr2->value.op.op2)
778 return gfc_check_dependency (expr1, expr2->value.op.op2, identical);
779 return 0;
781 case EXPR_VARIABLE:
782 /* The interesting cases are when the symbols don't match. */
783 if (expr1->symtree->n.sym != expr2->symtree->n.sym)
785 gfc_typespec *ts1 = &expr1->symtree->n.sym->ts;
786 gfc_typespec *ts2 = &expr2->symtree->n.sym->ts;
788 /* Return 1 if expr1 and expr2 are equivalenced arrays. */
789 if (gfc_are_equivalenced_arrays (expr1, expr2))
790 return 1;
792 /* Symbols can only alias if they have the same type. */
793 if (ts1->type != BT_UNKNOWN && ts2->type != BT_UNKNOWN
794 && ts1->type != BT_DERIVED && ts2->type != BT_DERIVED)
796 if (ts1->type != ts2->type || ts1->kind != ts2->kind)
797 return 0;
800 /* If either variable is a pointer, assume the worst. */
801 /* TODO: -fassume-no-pointer-aliasing */
802 if (gfc_is_data_pointer (expr1) || gfc_is_data_pointer (expr2))
804 if (check_data_pointer_types (expr1, expr2)
805 && check_data_pointer_types (expr2, expr1))
806 return 0;
808 return 1;
811 /* Otherwise distinct symbols have no dependencies. */
812 return 0;
815 if (identical)
816 return 1;
818 /* Identical and disjoint ranges return 0,
819 overlapping ranges return 1. */
820 if (expr1->ref && expr2->ref)
821 return gfc_dep_resolver (expr1->ref, expr2->ref);
823 return 1;
825 case EXPR_FUNCTION:
826 if (expr2->inline_noncopying_intrinsic)
827 identical = 1;
828 /* Remember possible differences between elemental and
829 transformational functions. All functions inside a FORALL
830 will be pure. */
831 for (actual = expr2->value.function.actual;
832 actual; actual = actual->next)
834 if (!actual->expr)
835 continue;
836 n = gfc_check_dependency (expr1, actual->expr, identical);
837 if (n)
838 return n;
840 return 0;
842 case EXPR_CONSTANT:
843 case EXPR_NULL:
844 return 0;
846 case EXPR_ARRAY:
847 /* Loop through the array constructor's elements. */
848 for (c = gfc_constructor_first (expr2->value.constructor);
849 c; c = gfc_constructor_next (c))
851 /* If this is an iterator, assume the worst. */
852 if (c->iterator)
853 return 1;
854 /* Avoid recursion in the common case. */
855 if (c->expr->expr_type == EXPR_CONSTANT)
856 continue;
857 if (gfc_check_dependency (expr1, c->expr, 1))
858 return 1;
860 return 0;
862 default:
863 return 1;
868 /* Determines overlapping for two array sections. */
870 static gfc_dependency
871 gfc_check_section_vs_section (gfc_ref *lref, gfc_ref *rref, int n)
873 gfc_array_ref l_ar;
874 gfc_expr *l_start;
875 gfc_expr *l_end;
876 gfc_expr *l_stride;
877 gfc_expr *l_lower;
878 gfc_expr *l_upper;
879 int l_dir;
881 gfc_array_ref r_ar;
882 gfc_expr *r_start;
883 gfc_expr *r_end;
884 gfc_expr *r_stride;
885 gfc_expr *r_lower;
886 gfc_expr *r_upper;
887 int r_dir;
889 l_ar = lref->u.ar;
890 r_ar = rref->u.ar;
892 /* If they are the same range, return without more ado. */
893 if (gfc_is_same_range (&l_ar, &r_ar, n, 0))
894 return GFC_DEP_EQUAL;
896 l_start = l_ar.start[n];
897 l_end = l_ar.end[n];
898 l_stride = l_ar.stride[n];
900 r_start = r_ar.start[n];
901 r_end = r_ar.end[n];
902 r_stride = r_ar.stride[n];
904 /* If l_start is NULL take it from array specifier. */
905 if (NULL == l_start && IS_ARRAY_EXPLICIT (l_ar.as))
906 l_start = l_ar.as->lower[n];
907 /* If l_end is NULL take it from array specifier. */
908 if (NULL == l_end && IS_ARRAY_EXPLICIT (l_ar.as))
909 l_end = l_ar.as->upper[n];
911 /* If r_start is NULL take it from array specifier. */
912 if (NULL == r_start && IS_ARRAY_EXPLICIT (r_ar.as))
913 r_start = r_ar.as->lower[n];
914 /* If r_end is NULL take it from array specifier. */
915 if (NULL == r_end && IS_ARRAY_EXPLICIT (r_ar.as))
916 r_end = r_ar.as->upper[n];
918 /* Determine whether the l_stride is positive or negative. */
919 if (!l_stride)
920 l_dir = 1;
921 else if (l_stride->expr_type == EXPR_CONSTANT
922 && l_stride->ts.type == BT_INTEGER)
923 l_dir = mpz_sgn (l_stride->value.integer);
924 else if (l_start && l_end)
925 l_dir = gfc_dep_compare_expr (l_end, l_start);
926 else
927 l_dir = -2;
929 /* Determine whether the r_stride is positive or negative. */
930 if (!r_stride)
931 r_dir = 1;
932 else if (r_stride->expr_type == EXPR_CONSTANT
933 && r_stride->ts.type == BT_INTEGER)
934 r_dir = mpz_sgn (r_stride->value.integer);
935 else if (r_start && r_end)
936 r_dir = gfc_dep_compare_expr (r_end, r_start);
937 else
938 r_dir = -2;
940 /* The strides should never be zero. */
941 if (l_dir == 0 || r_dir == 0)
942 return GFC_DEP_OVERLAP;
944 /* Determine LHS upper and lower bounds. */
945 if (l_dir == 1)
947 l_lower = l_start;
948 l_upper = l_end;
950 else if (l_dir == -1)
952 l_lower = l_end;
953 l_upper = l_start;
955 else
957 l_lower = NULL;
958 l_upper = NULL;
961 /* Determine RHS upper and lower bounds. */
962 if (r_dir == 1)
964 r_lower = r_start;
965 r_upper = r_end;
967 else if (r_dir == -1)
969 r_lower = r_end;
970 r_upper = r_start;
972 else
974 r_lower = NULL;
975 r_upper = NULL;
978 /* Check whether the ranges are disjoint. */
979 if (l_upper && r_lower && gfc_dep_compare_expr (l_upper, r_lower) == -1)
980 return GFC_DEP_NODEP;
981 if (r_upper && l_lower && gfc_dep_compare_expr (r_upper, l_lower) == -1)
982 return GFC_DEP_NODEP;
984 /* Handle cases like x:y:1 vs. x:z:-1 as GFC_DEP_EQUAL. */
985 if (l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == 0)
987 if (l_dir == 1 && r_dir == -1)
988 return GFC_DEP_EQUAL;
989 if (l_dir == -1 && r_dir == 1)
990 return GFC_DEP_EQUAL;
993 /* Handle cases like x:y:1 vs. z:y:-1 as GFC_DEP_EQUAL. */
994 if (l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == 0)
996 if (l_dir == 1 && r_dir == -1)
997 return GFC_DEP_EQUAL;
998 if (l_dir == -1 && r_dir == 1)
999 return GFC_DEP_EQUAL;
1002 /* Handle cases like x:y:2 vs. x+1:z:4 as GFC_DEP_NODEP.
1003 There is no dependency if the remainder of
1004 (l_start - r_start) / gcd(l_stride, r_stride) is
1005 nonzero.
1006 TODO:
1007 - Handle cases where x is an expression.
1008 - Cases like a(1:4:2) = a(2:3) are still not handled.
1011 #define IS_CONSTANT_INTEGER(a) ((a) && ((a)->expr_type == EXPR_CONSTANT) \
1012 && (a)->ts.type == BT_INTEGER)
1014 if (IS_CONSTANT_INTEGER(l_start) && IS_CONSTANT_INTEGER(r_start)
1015 && IS_CONSTANT_INTEGER(l_stride) && IS_CONSTANT_INTEGER(r_stride))
1017 mpz_t gcd, tmp;
1018 int result;
1020 mpz_init (gcd);
1021 mpz_init (tmp);
1023 mpz_gcd (gcd, l_stride->value.integer, r_stride->value.integer);
1024 mpz_sub (tmp, l_start->value.integer, r_start->value.integer);
1026 mpz_fdiv_r (tmp, tmp, gcd);
1027 result = mpz_cmp_si (tmp, 0L);
1029 mpz_clear (gcd);
1030 mpz_clear (tmp);
1032 if (result != 0)
1033 return GFC_DEP_NODEP;
1036 #undef IS_CONSTANT_INTEGER
1038 /* Check for forward dependencies x:y vs. x+1:z. */
1039 if (l_dir == 1 && r_dir == 1
1040 && l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == -1
1041 && l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == -1)
1043 /* Check that the strides are the same. */
1044 if (!l_stride && !r_stride)
1045 return GFC_DEP_FORWARD;
1046 if (l_stride && r_stride
1047 && gfc_dep_compare_expr (l_stride, r_stride) == 0)
1048 return GFC_DEP_FORWARD;
1051 /* Check for forward dependencies x:y:-1 vs. x-1:z:-1. */
1052 if (l_dir == -1 && r_dir == -1
1053 && l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == 1
1054 && l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == 1)
1056 /* Check that the strides are the same. */
1057 if (!l_stride && !r_stride)
1058 return GFC_DEP_FORWARD;
1059 if (l_stride && r_stride
1060 && gfc_dep_compare_expr (l_stride, r_stride) == 0)
1061 return GFC_DEP_FORWARD;
1064 return GFC_DEP_OVERLAP;
1068 /* Determines overlapping for a single element and a section. */
1070 static gfc_dependency
1071 gfc_check_element_vs_section( gfc_ref *lref, gfc_ref *rref, int n)
1073 gfc_array_ref *ref;
1074 gfc_expr *elem;
1075 gfc_expr *start;
1076 gfc_expr *end;
1077 gfc_expr *stride;
1078 int s;
1080 elem = lref->u.ar.start[n];
1081 if (!elem)
1082 return GFC_DEP_OVERLAP;
1084 ref = &rref->u.ar;
1085 start = ref->start[n] ;
1086 end = ref->end[n] ;
1087 stride = ref->stride[n];
1089 if (!start && IS_ARRAY_EXPLICIT (ref->as))
1090 start = ref->as->lower[n];
1091 if (!end && IS_ARRAY_EXPLICIT (ref->as))
1092 end = ref->as->upper[n];
1094 /* Determine whether the stride is positive or negative. */
1095 if (!stride)
1096 s = 1;
1097 else if (stride->expr_type == EXPR_CONSTANT
1098 && stride->ts.type == BT_INTEGER)
1099 s = mpz_sgn (stride->value.integer);
1100 else
1101 s = -2;
1103 /* Stride should never be zero. */
1104 if (s == 0)
1105 return GFC_DEP_OVERLAP;
1107 /* Positive strides. */
1108 if (s == 1)
1110 /* Check for elem < lower. */
1111 if (start && gfc_dep_compare_expr (elem, start) == -1)
1112 return GFC_DEP_NODEP;
1113 /* Check for elem > upper. */
1114 if (end && gfc_dep_compare_expr (elem, end) == 1)
1115 return GFC_DEP_NODEP;
1117 if (start && end)
1119 s = gfc_dep_compare_expr (start, end);
1120 /* Check for an empty range. */
1121 if (s == 1)
1122 return GFC_DEP_NODEP;
1123 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1124 return GFC_DEP_EQUAL;
1127 /* Negative strides. */
1128 else if (s == -1)
1130 /* Check for elem > upper. */
1131 if (end && gfc_dep_compare_expr (elem, start) == 1)
1132 return GFC_DEP_NODEP;
1133 /* Check for elem < lower. */
1134 if (start && gfc_dep_compare_expr (elem, end) == -1)
1135 return GFC_DEP_NODEP;
1137 if (start && end)
1139 s = gfc_dep_compare_expr (start, end);
1140 /* Check for an empty range. */
1141 if (s == -1)
1142 return GFC_DEP_NODEP;
1143 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1144 return GFC_DEP_EQUAL;
1147 /* Unknown strides. */
1148 else
1150 if (!start || !end)
1151 return GFC_DEP_OVERLAP;
1152 s = gfc_dep_compare_expr (start, end);
1153 if (s == -2)
1154 return GFC_DEP_OVERLAP;
1155 /* Assume positive stride. */
1156 if (s == -1)
1158 /* Check for elem < lower. */
1159 if (gfc_dep_compare_expr (elem, start) == -1)
1160 return GFC_DEP_NODEP;
1161 /* Check for elem > upper. */
1162 if (gfc_dep_compare_expr (elem, end) == 1)
1163 return GFC_DEP_NODEP;
1165 /* Assume negative stride. */
1166 else if (s == 1)
1168 /* Check for elem > upper. */
1169 if (gfc_dep_compare_expr (elem, start) == 1)
1170 return GFC_DEP_NODEP;
1171 /* Check for elem < lower. */
1172 if (gfc_dep_compare_expr (elem, end) == -1)
1173 return GFC_DEP_NODEP;
1175 /* Equal bounds. */
1176 else if (s == 0)
1178 s = gfc_dep_compare_expr (elem, start);
1179 if (s == 0)
1180 return GFC_DEP_EQUAL;
1181 if (s == 1 || s == -1)
1182 return GFC_DEP_NODEP;
1186 return GFC_DEP_OVERLAP;
1190 /* Traverse expr, checking all EXPR_VARIABLE symbols for their
1191 forall_index attribute. Return true if any variable may be
1192 being used as a FORALL index. Its safe to pessimistically
1193 return true, and assume a dependency. */
1195 static bool
1196 contains_forall_index_p (gfc_expr *expr)
1198 gfc_actual_arglist *arg;
1199 gfc_constructor *c;
1200 gfc_ref *ref;
1201 int i;
1203 if (!expr)
1204 return false;
1206 switch (expr->expr_type)
1208 case EXPR_VARIABLE:
1209 if (expr->symtree->n.sym->forall_index)
1210 return true;
1211 break;
1213 case EXPR_OP:
1214 if (contains_forall_index_p (expr->value.op.op1)
1215 || contains_forall_index_p (expr->value.op.op2))
1216 return true;
1217 break;
1219 case EXPR_FUNCTION:
1220 for (arg = expr->value.function.actual; arg; arg = arg->next)
1221 if (contains_forall_index_p (arg->expr))
1222 return true;
1223 break;
1225 case EXPR_CONSTANT:
1226 case EXPR_NULL:
1227 case EXPR_SUBSTRING:
1228 break;
1230 case EXPR_STRUCTURE:
1231 case EXPR_ARRAY:
1232 for (c = gfc_constructor_first (expr->value.constructor);
1233 c; gfc_constructor_next (c))
1234 if (contains_forall_index_p (c->expr))
1235 return true;
1236 break;
1238 default:
1239 gcc_unreachable ();
1242 for (ref = expr->ref; ref; ref = ref->next)
1243 switch (ref->type)
1245 case REF_ARRAY:
1246 for (i = 0; i < ref->u.ar.dimen; i++)
1247 if (contains_forall_index_p (ref->u.ar.start[i])
1248 || contains_forall_index_p (ref->u.ar.end[i])
1249 || contains_forall_index_p (ref->u.ar.stride[i]))
1250 return true;
1251 break;
1253 case REF_COMPONENT:
1254 break;
1256 case REF_SUBSTRING:
1257 if (contains_forall_index_p (ref->u.ss.start)
1258 || contains_forall_index_p (ref->u.ss.end))
1259 return true;
1260 break;
1262 default:
1263 gcc_unreachable ();
1266 return false;
1269 /* Determines overlapping for two single element array references. */
1271 static gfc_dependency
1272 gfc_check_element_vs_element (gfc_ref *lref, gfc_ref *rref, int n)
1274 gfc_array_ref l_ar;
1275 gfc_array_ref r_ar;
1276 gfc_expr *l_start;
1277 gfc_expr *r_start;
1278 int i;
1280 l_ar = lref->u.ar;
1281 r_ar = rref->u.ar;
1282 l_start = l_ar.start[n] ;
1283 r_start = r_ar.start[n] ;
1284 i = gfc_dep_compare_expr (r_start, l_start);
1285 if (i == 0)
1286 return GFC_DEP_EQUAL;
1288 /* Treat two scalar variables as potentially equal. This allows
1289 us to prove that a(i,:) and a(j,:) have no dependency. See
1290 Gerald Roth, "Evaluation of Array Syntax Dependence Analysis",
1291 Proceedings of the International Conference on Parallel and
1292 Distributed Processing Techniques and Applications (PDPTA2001),
1293 Las Vegas, Nevada, June 2001. */
1294 /* However, we need to be careful when either scalar expression
1295 contains a FORALL index, as these can potentially change value
1296 during the scalarization/traversal of this array reference. */
1297 if (contains_forall_index_p (r_start) || contains_forall_index_p (l_start))
1298 return GFC_DEP_OVERLAP;
1300 if (i != -2)
1301 return GFC_DEP_NODEP;
1302 return GFC_DEP_EQUAL;
1306 /* Determine if an array ref, usually an array section specifies the
1307 entire array. In addition, if the second, pointer argument is
1308 provided, the function will return true if the reference is
1309 contiguous; eg. (:, 1) gives true but (1,:) gives false. */
1311 bool
1312 gfc_full_array_ref_p (gfc_ref *ref, bool *contiguous)
1314 int i;
1315 int n;
1316 bool lbound_OK = true;
1317 bool ubound_OK = true;
1319 if (contiguous)
1320 *contiguous = false;
1322 if (ref->type != REF_ARRAY)
1323 return false;
1325 if (ref->u.ar.type == AR_FULL)
1327 if (contiguous)
1328 *contiguous = true;
1329 return true;
1332 if (ref->u.ar.type != AR_SECTION)
1333 return false;
1334 if (ref->next)
1335 return false;
1337 for (i = 0; i < ref->u.ar.dimen; i++)
1339 /* If we have a single element in the reference, for the reference
1340 to be full, we need to ascertain that the array has a single
1341 element in this dimension and that we actually reference the
1342 correct element. */
1343 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
1345 /* This is unconditionally a contiguous reference if all the
1346 remaining dimensions are elements. */
1347 if (contiguous)
1349 *contiguous = true;
1350 for (n = i + 1; n < ref->u.ar.dimen; n++)
1351 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
1352 *contiguous = false;
1355 if (!ref->u.ar.as
1356 || !ref->u.ar.as->lower[i]
1357 || !ref->u.ar.as->upper[i]
1358 || gfc_dep_compare_expr (ref->u.ar.as->lower[i],
1359 ref->u.ar.as->upper[i])
1360 || !ref->u.ar.start[i]
1361 || gfc_dep_compare_expr (ref->u.ar.start[i],
1362 ref->u.ar.as->lower[i]))
1363 return false;
1364 else
1365 continue;
1368 /* Check the lower bound. */
1369 if (ref->u.ar.start[i]
1370 && (!ref->u.ar.as
1371 || !ref->u.ar.as->lower[i]
1372 || gfc_dep_compare_expr (ref->u.ar.start[i],
1373 ref->u.ar.as->lower[i])))
1374 lbound_OK = false;
1375 /* Check the upper bound. */
1376 if (ref->u.ar.end[i]
1377 && (!ref->u.ar.as
1378 || !ref->u.ar.as->upper[i]
1379 || gfc_dep_compare_expr (ref->u.ar.end[i],
1380 ref->u.ar.as->upper[i])))
1381 ubound_OK = false;
1382 /* Check the stride. */
1383 if (ref->u.ar.stride[i]
1384 && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
1385 return false;
1387 /* This is unconditionally a contiguous reference as long as all
1388 the subsequent dimensions are elements. */
1389 if (contiguous)
1391 *contiguous = true;
1392 for (n = i + 1; n < ref->u.ar.dimen; n++)
1393 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
1394 *contiguous = false;
1397 if (!lbound_OK || !ubound_OK)
1398 return false;
1400 return true;
1404 /* Determine if a full array is the same as an array section with one
1405 variable limit. For this to be so, the strides must both be unity
1406 and one of either start == lower or end == upper must be true. */
1408 static bool
1409 ref_same_as_full_array (gfc_ref *full_ref, gfc_ref *ref)
1411 int i;
1412 bool upper_or_lower;
1414 if (full_ref->type != REF_ARRAY)
1415 return false;
1416 if (full_ref->u.ar.type != AR_FULL)
1417 return false;
1418 if (ref->type != REF_ARRAY)
1419 return false;
1420 if (ref->u.ar.type != AR_SECTION)
1421 return false;
1423 for (i = 0; i < ref->u.ar.dimen; i++)
1425 /* If we have a single element in the reference, we need to check
1426 that the array has a single element and that we actually reference
1427 the correct element. */
1428 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
1430 if (!full_ref->u.ar.as
1431 || !full_ref->u.ar.as->lower[i]
1432 || !full_ref->u.ar.as->upper[i]
1433 || gfc_dep_compare_expr (full_ref->u.ar.as->lower[i],
1434 full_ref->u.ar.as->upper[i])
1435 || !ref->u.ar.start[i]
1436 || gfc_dep_compare_expr (ref->u.ar.start[i],
1437 full_ref->u.ar.as->lower[i]))
1438 return false;
1441 /* Check the strides. */
1442 if (full_ref->u.ar.stride[i] && !gfc_expr_is_one (full_ref->u.ar.stride[i], 0))
1443 return false;
1444 if (ref->u.ar.stride[i] && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
1445 return false;
1447 upper_or_lower = false;
1448 /* Check the lower bound. */
1449 if (ref->u.ar.start[i]
1450 && (ref->u.ar.as
1451 && full_ref->u.ar.as->lower[i]
1452 && gfc_dep_compare_expr (ref->u.ar.start[i],
1453 full_ref->u.ar.as->lower[i]) == 0))
1454 upper_or_lower = true;
1455 /* Check the upper bound. */
1456 if (ref->u.ar.end[i]
1457 && (ref->u.ar.as
1458 && full_ref->u.ar.as->upper[i]
1459 && gfc_dep_compare_expr (ref->u.ar.end[i],
1460 full_ref->u.ar.as->upper[i]) == 0))
1461 upper_or_lower = true;
1462 if (!upper_or_lower)
1463 return false;
1465 return true;
1469 /* Finds if two array references are overlapping or not.
1470 Return value
1471 1 : array references are overlapping.
1472 0 : array references are identical or not overlapping. */
1475 gfc_dep_resolver (gfc_ref *lref, gfc_ref *rref)
1477 int n;
1478 gfc_dependency fin_dep;
1479 gfc_dependency this_dep;
1481 fin_dep = GFC_DEP_ERROR;
1482 /* Dependencies due to pointers should already have been identified.
1483 We only need to check for overlapping array references. */
1485 while (lref && rref)
1487 /* We're resolving from the same base symbol, so both refs should be
1488 the same type. We traverse the reference chain until we find ranges
1489 that are not equal. */
1490 gcc_assert (lref->type == rref->type);
1491 switch (lref->type)
1493 case REF_COMPONENT:
1494 /* The two ranges can't overlap if they are from different
1495 components. */
1496 if (lref->u.c.component != rref->u.c.component)
1497 return 0;
1498 break;
1500 case REF_SUBSTRING:
1501 /* Substring overlaps are handled by the string assignment code
1502 if there is not an underlying dependency. */
1503 return (fin_dep == GFC_DEP_OVERLAP) ? 1 : 0;
1505 case REF_ARRAY:
1507 if (ref_same_as_full_array (lref, rref))
1508 return 0;
1510 if (ref_same_as_full_array (rref, lref))
1511 return 0;
1513 if (lref->u.ar.dimen != rref->u.ar.dimen)
1515 if (lref->u.ar.type == AR_FULL)
1516 fin_dep = gfc_full_array_ref_p (rref, NULL) ? GFC_DEP_EQUAL
1517 : GFC_DEP_OVERLAP;
1518 else if (rref->u.ar.type == AR_FULL)
1519 fin_dep = gfc_full_array_ref_p (lref, NULL) ? GFC_DEP_EQUAL
1520 : GFC_DEP_OVERLAP;
1521 else
1522 return 1;
1523 break;
1526 for (n=0; n < lref->u.ar.dimen; n++)
1528 /* Assume dependency when either of array reference is vector
1529 subscript. */
1530 if (lref->u.ar.dimen_type[n] == DIMEN_VECTOR
1531 || rref->u.ar.dimen_type[n] == DIMEN_VECTOR)
1532 return 1;
1533 if (lref->u.ar.dimen_type[n] == DIMEN_RANGE
1534 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
1535 this_dep = gfc_check_section_vs_section (lref, rref, n);
1536 else if (lref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1537 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
1538 this_dep = gfc_check_element_vs_section (lref, rref, n);
1539 else if (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1540 && lref->u.ar.dimen_type[n] == DIMEN_RANGE)
1541 this_dep = gfc_check_element_vs_section (rref, lref, n);
1542 else
1544 gcc_assert (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1545 && lref->u.ar.dimen_type[n] == DIMEN_ELEMENT);
1546 this_dep = gfc_check_element_vs_element (rref, lref, n);
1549 /* If any dimension doesn't overlap, we have no dependency. */
1550 if (this_dep == GFC_DEP_NODEP)
1551 return 0;
1553 /* Overlap codes are in order of priority. We only need to
1554 know the worst one.*/
1555 if (this_dep > fin_dep)
1556 fin_dep = this_dep;
1559 /* If this is an equal element, we have to keep going until we find
1560 the "real" array reference. */
1561 if (lref->u.ar.type == AR_ELEMENT
1562 && rref->u.ar.type == AR_ELEMENT
1563 && fin_dep == GFC_DEP_EQUAL)
1564 break;
1566 /* Exactly matching and forward overlapping ranges don't cause a
1567 dependency. */
1568 if (fin_dep < GFC_DEP_OVERLAP)
1569 return 0;
1571 /* Keep checking. We only have a dependency if
1572 subsequent references also overlap. */
1573 break;
1575 default:
1576 gcc_unreachable ();
1578 lref = lref->next;
1579 rref = rref->next;
1582 /* If we haven't seen any array refs then something went wrong. */
1583 gcc_assert (fin_dep != GFC_DEP_ERROR);
1585 /* Assume the worst if we nest to different depths. */
1586 if (lref || rref)
1587 return 1;
1589 return fin_dep == GFC_DEP_OVERLAP;