Merge from mainline (163495:164578).
[official-gcc/graphite-test-results.git] / gcc / fortran / dependency.c
blobee66d216ab567cd2785aa339f617d2b268fc2663
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_BACKWARD, /* e.g. a(2:4) = a(1:3). */
44 GFC_DEP_OVERLAP, /* May overlap in some other way. */
45 GFC_DEP_NODEP /* Distinct ranges. */
47 gfc_dependency;
49 /* Macros */
50 #define IS_ARRAY_EXPLICIT(as) ((as->type == AS_EXPLICIT ? 1 : 0))
52 /* Forward declarations */
54 static gfc_dependency check_section_vs_section (gfc_array_ref *,
55 gfc_array_ref *, int);
57 /* Returns 1 if the expr is an integer constant value 1, 0 if it is not or
58 def if the value could not be determined. */
60 int
61 gfc_expr_is_one (gfc_expr *expr, int def)
63 gcc_assert (expr != NULL);
65 if (expr->expr_type != EXPR_CONSTANT)
66 return def;
68 if (expr->ts.type != BT_INTEGER)
69 return def;
71 return mpz_cmp_si (expr->value.integer, 1) == 0;
74 /* Check if two array references are known to be identical. Calls
75 gfc_dep_compare_expr if necessary for comparing array indices. */
77 static bool
78 identical_array_ref (gfc_array_ref *a1, gfc_array_ref *a2)
80 int i;
82 if (a1->type == AR_FULL && a2->type == AR_FULL)
83 return true;
85 if (a1->type == AR_SECTION && a2->type == AR_SECTION)
87 gcc_assert (a1->dimen == a2->dimen);
89 for ( i = 0; i < a1->dimen; i++)
91 /* TODO: Currently, we punt on an integer array as an index. */
92 if (a1->dimen_type[i] != DIMEN_RANGE
93 || a2->dimen_type[i] != DIMEN_RANGE)
94 return false;
96 if (check_section_vs_section (a1, a2, i) != GFC_DEP_EQUAL)
97 return false;
99 return true;
102 if (a1->type == AR_ELEMENT && a2->type == AR_ELEMENT)
104 gcc_assert (a1->dimen == a2->dimen);
105 for (i = 0; i < a1->dimen; i++)
107 if (gfc_dep_compare_expr (a1->start[i], a2->start[i]) != 0)
108 return false;
110 return true;
112 return false;
117 /* Return true for identical variables, checking for references if
118 necessary. Calls identical_array_ref for checking array sections. */
120 bool
121 gfc_are_identical_variables (gfc_expr *e1, gfc_expr *e2)
123 gfc_ref *r1, *r2;
125 if (e1->symtree->n.sym != e2->symtree->n.sym)
126 return false;
128 r1 = e1->ref;
129 r2 = e2->ref;
131 while (r1 != NULL || r2 != NULL)
134 /* Assume the variables are not equal if one has a reference and the
135 other doesn't.
136 TODO: Handle full references like comparing a(:) to a.
139 if (r1 == NULL || r2 == NULL)
140 return false;
142 if (r1->type != r2->type)
143 return false;
145 switch (r1->type)
148 case REF_ARRAY:
149 if (!identical_array_ref (&r1->u.ar, &r2->u.ar))
150 return false;
152 break;
154 case REF_COMPONENT:
155 if (r1->u.c.component != r2->u.c.component)
156 return false;
157 break;
159 case REF_SUBSTRING:
160 if (gfc_dep_compare_expr (r1->u.ss.start, r2->u.ss.start) != 0
161 || gfc_dep_compare_expr (r1->u.ss.end, r2->u.ss.end) != 0)
162 return false;
163 break;
165 default:
166 gfc_internal_error ("gfc_are_identical_variables: Bad type");
168 r1 = r1->next;
169 r2 = r2->next;
171 return true;
174 /* Compare two values. Returns 0 if e1 == e2, -1 if e1 < e2, +1 if e1 > e2,
175 and -2 if the relationship could not be determined. */
178 gfc_dep_compare_expr (gfc_expr *e1, gfc_expr *e2)
180 gfc_actual_arglist *args1;
181 gfc_actual_arglist *args2;
182 int i;
183 gfc_expr *n1, *n2;
185 n1 = NULL;
186 n2 = NULL;
188 /* Remove any integer conversion functions to larger types. */
189 if (e1->expr_type == EXPR_FUNCTION && e1->value.function.isym
190 && e1->value.function.isym->id == GFC_ISYM_CONVERSION
191 && e1->ts.type == BT_INTEGER)
193 args1 = e1->value.function.actual;
194 if (args1->expr->ts.type == BT_INTEGER
195 && e1->ts.kind > args1->expr->ts.kind)
196 n1 = args1->expr;
199 if (e2->expr_type == EXPR_FUNCTION && e2->value.function.isym
200 && e2->value.function.isym->id == GFC_ISYM_CONVERSION
201 && e2->ts.type == BT_INTEGER)
203 args2 = e2->value.function.actual;
204 if (args2->expr->ts.type == BT_INTEGER
205 && e2->ts.kind > args2->expr->ts.kind)
206 n2 = args2->expr;
209 if (n1 != NULL)
211 if (n2 != NULL)
212 return gfc_dep_compare_expr (n1, n2);
213 else
214 return gfc_dep_compare_expr (n1, e2);
216 else
218 if (n2 != NULL)
219 return gfc_dep_compare_expr (e1, n2);
222 if (e1->expr_type == EXPR_OP
223 && (e1->value.op.op == INTRINSIC_UPLUS
224 || e1->value.op.op == INTRINSIC_PARENTHESES))
225 return gfc_dep_compare_expr (e1->value.op.op1, e2);
226 if (e2->expr_type == EXPR_OP
227 && (e2->value.op.op == INTRINSIC_UPLUS
228 || e2->value.op.op == INTRINSIC_PARENTHESES))
229 return gfc_dep_compare_expr (e1, e2->value.op.op1);
231 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_PLUS)
233 /* Compare X+C vs. X. */
234 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
235 && e1->value.op.op2->ts.type == BT_INTEGER
236 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
237 return mpz_sgn (e1->value.op.op2->value.integer);
239 /* Compare P+Q vs. R+S. */
240 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
242 int l, r;
244 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
245 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
246 if (l == 0 && r == 0)
247 return 0;
248 if (l == 0 && r != -2)
249 return r;
250 if (l != -2 && r == 0)
251 return l;
252 if (l == 1 && r == 1)
253 return 1;
254 if (l == -1 && r == -1)
255 return -1;
257 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op2);
258 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op1);
259 if (l == 0 && r == 0)
260 return 0;
261 if (l == 0 && r != -2)
262 return r;
263 if (l != -2 && r == 0)
264 return l;
265 if (l == 1 && r == 1)
266 return 1;
267 if (l == -1 && r == -1)
268 return -1;
272 /* Compare X vs. X+C. */
273 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
275 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
276 && e2->value.op.op2->ts.type == BT_INTEGER
277 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
278 return -mpz_sgn (e2->value.op.op2->value.integer);
281 /* Compare X-C vs. X. */
282 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_MINUS)
284 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
285 && e1->value.op.op2->ts.type == BT_INTEGER
286 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
287 return -mpz_sgn (e1->value.op.op2->value.integer);
289 /* Compare P-Q vs. R-S. */
290 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
292 int l, r;
294 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
295 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
296 if (l == 0 && r == 0)
297 return 0;
298 if (l != -2 && r == 0)
299 return l;
300 if (l == 0 && r != -2)
301 return -r;
302 if (l == 1 && r == -1)
303 return 1;
304 if (l == -1 && r == 1)
305 return -1;
309 /* Compare X vs. X-C. */
310 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
312 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
313 && e2->value.op.op2->ts.type == BT_INTEGER
314 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
315 return mpz_sgn (e2->value.op.op2->value.integer);
318 if (e1->expr_type != e2->expr_type)
319 return -2;
321 switch (e1->expr_type)
323 case EXPR_CONSTANT:
324 if (e1->ts.type != BT_INTEGER || e2->ts.type != BT_INTEGER)
325 return -2;
327 i = mpz_cmp (e1->value.integer, e2->value.integer);
328 if (i == 0)
329 return 0;
330 else if (i < 0)
331 return -1;
332 return 1;
334 case EXPR_VARIABLE:
335 if (gfc_are_identical_variables (e1, e2))
336 return 0;
337 else
338 return -2;
340 case EXPR_OP:
341 /* Intrinsic operators are the same if their operands are the same. */
342 if (e1->value.op.op != e2->value.op.op)
343 return -2;
344 if (e1->value.op.op2 == 0)
346 i = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
347 return i == 0 ? 0 : -2;
349 if (gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1) == 0
350 && gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2) == 0)
351 return 0;
352 /* TODO Handle commutative binary operators here? */
353 return -2;
355 case EXPR_FUNCTION:
357 /* PURE functions can be compared for argument equality. */
358 if ((e1->value.function.esym && e2->value.function.esym
359 && e1->value.function.esym == e2->value.function.esym
360 && e1->value.function.esym->result->attr.pure)
361 || (e1->value.function.isym && e2->value.function.isym
362 && e1->value.function.isym == e2->value.function.isym
363 && e1->value.function.isym->pure))
365 args1 = e1->value.function.actual;
366 args2 = e2->value.function.actual;
368 /* Compare the argument lists for equality. */
369 while (args1 && args2)
371 /* Bitwise xor, since C has no non-bitwise xor operator. */
372 if ((args1->expr == NULL) ^ (args2->expr == NULL))
373 return -2;
375 if (args1->expr != NULL && args2->expr != NULL
376 && gfc_dep_compare_expr (args1->expr, args2->expr) != 0)
377 return -2;
379 args1 = args1->next;
380 args2 = args2->next;
382 return (args1 || args2) ? -2 : 0;
384 else
385 return -2;
386 break;
388 default:
389 return -2;
394 /* Returns 1 if the two ranges are the same, 0 if they are not, and def
395 if the results are indeterminate. N is the dimension to compare. */
398 gfc_is_same_range (gfc_array_ref *ar1, gfc_array_ref *ar2, int n, int def)
400 gfc_expr *e1;
401 gfc_expr *e2;
402 int i;
404 /* TODO: More sophisticated range comparison. */
405 gcc_assert (ar1 && ar2);
407 gcc_assert (ar1->dimen_type[n] == ar2->dimen_type[n]);
409 e1 = ar1->stride[n];
410 e2 = ar2->stride[n];
411 /* Check for mismatching strides. A NULL stride means a stride of 1. */
412 if (e1 && !e2)
414 i = gfc_expr_is_one (e1, -1);
415 if (i == -1)
416 return def;
417 else if (i == 0)
418 return 0;
420 else if (e2 && !e1)
422 i = gfc_expr_is_one (e2, -1);
423 if (i == -1)
424 return def;
425 else if (i == 0)
426 return 0;
428 else if (e1 && e2)
430 i = gfc_dep_compare_expr (e1, e2);
431 if (i == -2)
432 return def;
433 else if (i != 0)
434 return 0;
436 /* The strides match. */
438 /* Check the range start. */
439 e1 = ar1->start[n];
440 e2 = ar2->start[n];
441 if (e1 || e2)
443 /* Use the bound of the array if no bound is specified. */
444 if (ar1->as && !e1)
445 e1 = ar1->as->lower[n];
447 if (ar2->as && !e2)
448 e2 = ar2->as->lower[n];
450 /* Check we have values for both. */
451 if (!(e1 && e2))
452 return def;
454 i = gfc_dep_compare_expr (e1, e2);
455 if (i == -2)
456 return def;
457 else if (i != 0)
458 return 0;
461 /* Check the range end. */
462 e1 = ar1->end[n];
463 e2 = ar2->end[n];
464 if (e1 || e2)
466 /* Use the bound of the array if no bound is specified. */
467 if (ar1->as && !e1)
468 e1 = ar1->as->upper[n];
470 if (ar2->as && !e2)
471 e2 = ar2->as->upper[n];
473 /* Check we have values for both. */
474 if (!(e1 && e2))
475 return def;
477 i = gfc_dep_compare_expr (e1, e2);
478 if (i == -2)
479 return def;
480 else if (i != 0)
481 return 0;
484 return 1;
488 /* Some array-returning intrinsics can be implemented by reusing the
489 data from one of the array arguments. For example, TRANSPOSE does
490 not necessarily need to allocate new data: it can be implemented
491 by copying the original array's descriptor and simply swapping the
492 two dimension specifications.
494 If EXPR is a call to such an intrinsic, return the argument
495 whose data can be reused, otherwise return NULL. */
497 gfc_expr *
498 gfc_get_noncopying_intrinsic_argument (gfc_expr *expr)
500 if (expr->expr_type != EXPR_FUNCTION || !expr->value.function.isym)
501 return NULL;
503 switch (expr->value.function.isym->id)
505 case GFC_ISYM_TRANSPOSE:
506 return expr->value.function.actual->expr;
508 default:
509 return NULL;
514 /* Return true if the result of reference REF can only be constructed
515 using a temporary array. */
517 bool
518 gfc_ref_needs_temporary_p (gfc_ref *ref)
520 int n;
521 bool subarray_p;
523 subarray_p = false;
524 for (; ref; ref = ref->next)
525 switch (ref->type)
527 case REF_ARRAY:
528 /* Vector dimensions are generally not monotonic and must be
529 handled using a temporary. */
530 if (ref->u.ar.type == AR_SECTION)
531 for (n = 0; n < ref->u.ar.dimen; n++)
532 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR)
533 return true;
535 subarray_p = true;
536 break;
538 case REF_SUBSTRING:
539 /* Within an array reference, character substrings generally
540 need a temporary. Character array strides are expressed as
541 multiples of the element size (consistent with other array
542 types), not in characters. */
543 return subarray_p;
545 case REF_COMPONENT:
546 break;
549 return false;
553 static int
554 gfc_is_data_pointer (gfc_expr *e)
556 gfc_ref *ref;
558 if (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION)
559 return 0;
561 /* No subreference if it is a function */
562 gcc_assert (e->expr_type == EXPR_VARIABLE || !e->ref);
564 if (e->symtree->n.sym->attr.pointer)
565 return 1;
567 for (ref = e->ref; ref; ref = ref->next)
568 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
569 return 1;
571 return 0;
575 /* Return true if array variable VAR could be passed to the same function
576 as argument EXPR without interfering with EXPR. INTENT is the intent
577 of VAR.
579 This is considerably less conservative than other dependencies
580 because many function arguments will already be copied into a
581 temporary. */
583 static int
584 gfc_check_argument_var_dependency (gfc_expr *var, sym_intent intent,
585 gfc_expr *expr, gfc_dep_check elemental)
587 gfc_expr *arg;
589 gcc_assert (var->expr_type == EXPR_VARIABLE);
590 gcc_assert (var->rank > 0);
592 switch (expr->expr_type)
594 case EXPR_VARIABLE:
595 /* In case of elemental subroutines, there is no dependency
596 between two same-range array references. */
597 if (gfc_ref_needs_temporary_p (expr->ref)
598 || gfc_check_dependency (var, expr, elemental == NOT_ELEMENTAL))
600 if (elemental == ELEM_DONT_CHECK_VARIABLE)
602 /* Too many false positive with pointers. */
603 if (!gfc_is_data_pointer (var) && !gfc_is_data_pointer (expr))
605 /* Elemental procedures forbid unspecified intents,
606 and we don't check dependencies for INTENT_IN args. */
607 gcc_assert (intent == INTENT_OUT || intent == INTENT_INOUT);
609 /* We are told not to check dependencies.
610 We do it, however, and issue a warning in case we find one.
611 If a dependency is found in the case
612 elemental == ELEM_CHECK_VARIABLE, we will generate
613 a temporary, so we don't need to bother the user. */
614 gfc_warning ("INTENT(%s) actual argument at %L might "
615 "interfere with actual argument at %L.",
616 intent == INTENT_OUT ? "OUT" : "INOUT",
617 &var->where, &expr->where);
619 return 0;
621 else
622 return 1;
624 return 0;
626 case EXPR_ARRAY:
627 return gfc_check_dependency (var, expr, 1);
629 case EXPR_FUNCTION:
630 if (intent != INTENT_IN)
632 arg = gfc_get_noncopying_intrinsic_argument (expr);
633 if (arg != NULL)
634 return gfc_check_argument_var_dependency (var, intent, arg,
635 NOT_ELEMENTAL);
638 if (elemental != NOT_ELEMENTAL)
640 if ((expr->value.function.esym
641 && expr->value.function.esym->attr.elemental)
642 || (expr->value.function.isym
643 && expr->value.function.isym->elemental))
644 return gfc_check_fncall_dependency (var, intent, NULL,
645 expr->value.function.actual,
646 ELEM_CHECK_VARIABLE);
648 return 0;
650 case EXPR_OP:
651 /* In case of non-elemental procedures, there is no need to catch
652 dependencies, as we will make a temporary anyway. */
653 if (elemental)
655 /* If the actual arg EXPR is an expression, we need to catch
656 a dependency between variables in EXPR and VAR,
657 an intent((IN)OUT) variable. */
658 if (expr->value.op.op1
659 && gfc_check_argument_var_dependency (var, intent,
660 expr->value.op.op1,
661 ELEM_CHECK_VARIABLE))
662 return 1;
663 else if (expr->value.op.op2
664 && gfc_check_argument_var_dependency (var, intent,
665 expr->value.op.op2,
666 ELEM_CHECK_VARIABLE))
667 return 1;
669 return 0;
671 default:
672 return 0;
677 /* Like gfc_check_argument_var_dependency, but extended to any
678 array expression OTHER, not just variables. */
680 static int
681 gfc_check_argument_dependency (gfc_expr *other, sym_intent intent,
682 gfc_expr *expr, gfc_dep_check elemental)
684 switch (other->expr_type)
686 case EXPR_VARIABLE:
687 return gfc_check_argument_var_dependency (other, intent, expr, elemental);
689 case EXPR_FUNCTION:
690 other = gfc_get_noncopying_intrinsic_argument (other);
691 if (other != NULL)
692 return gfc_check_argument_dependency (other, INTENT_IN, expr,
693 NOT_ELEMENTAL);
695 return 0;
697 default:
698 return 0;
703 /* Like gfc_check_argument_dependency, but check all the arguments in ACTUAL.
704 FNSYM is the function being called, or NULL if not known. */
707 gfc_check_fncall_dependency (gfc_expr *other, sym_intent intent,
708 gfc_symbol *fnsym, gfc_actual_arglist *actual,
709 gfc_dep_check elemental)
711 gfc_formal_arglist *formal;
712 gfc_expr *expr;
714 formal = fnsym ? fnsym->formal : NULL;
715 for (; actual; actual = actual->next, formal = formal ? formal->next : NULL)
717 expr = actual->expr;
719 /* Skip args which are not present. */
720 if (!expr)
721 continue;
723 /* Skip other itself. */
724 if (expr == other)
725 continue;
727 /* Skip intent(in) arguments if OTHER itself is intent(in). */
728 if (formal && intent == INTENT_IN
729 && formal->sym->attr.intent == INTENT_IN)
730 continue;
732 if (gfc_check_argument_dependency (other, intent, expr, elemental))
733 return 1;
736 return 0;
740 /* Return 1 if e1 and e2 are equivalenced arrays, either
741 directly or indirectly; i.e., equivalence (a,b) for a and b
742 or equivalence (a,c),(b,c). This function uses the equiv_
743 lists, generated in trans-common(add_equivalences), that are
744 guaranteed to pick up indirect equivalences. We explicitly
745 check for overlap using the offset and length of the equivalence.
746 This function is symmetric.
747 TODO: This function only checks whether the full top-level
748 symbols overlap. An improved implementation could inspect
749 e1->ref and e2->ref to determine whether the actually accessed
750 portions of these variables/arrays potentially overlap. */
753 gfc_are_equivalenced_arrays (gfc_expr *e1, gfc_expr *e2)
755 gfc_equiv_list *l;
756 gfc_equiv_info *s, *fl1, *fl2;
758 gcc_assert (e1->expr_type == EXPR_VARIABLE
759 && e2->expr_type == EXPR_VARIABLE);
761 if (!e1->symtree->n.sym->attr.in_equivalence
762 || !e2->symtree->n.sym->attr.in_equivalence|| !e1->rank || !e2->rank)
763 return 0;
765 if (e1->symtree->n.sym->ns
766 && e1->symtree->n.sym->ns != gfc_current_ns)
767 l = e1->symtree->n.sym->ns->equiv_lists;
768 else
769 l = gfc_current_ns->equiv_lists;
771 /* Go through the equiv_lists and return 1 if the variables
772 e1 and e2 are members of the same group and satisfy the
773 requirement on their relative offsets. */
774 for (; l; l = l->next)
776 fl1 = NULL;
777 fl2 = NULL;
778 for (s = l->equiv; s; s = s->next)
780 if (s->sym == e1->symtree->n.sym)
782 fl1 = s;
783 if (fl2)
784 break;
786 if (s->sym == e2->symtree->n.sym)
788 fl2 = s;
789 if (fl1)
790 break;
794 if (s)
796 /* Can these lengths be zero? */
797 if (fl1->length <= 0 || fl2->length <= 0)
798 return 1;
799 /* These can't overlap if [f11,fl1+length] is before
800 [fl2,fl2+length], or [fl2,fl2+length] is before
801 [fl1,fl1+length], otherwise they do overlap. */
802 if (fl1->offset + fl1->length > fl2->offset
803 && fl2->offset + fl2->length > fl1->offset)
804 return 1;
807 return 0;
811 /* Return true if there is no possibility of aliasing because of a type
812 mismatch between all the possible pointer references and the
813 potential target. Note that this function is asymmetric in the
814 arguments and so must be called twice with the arguments exchanged. */
816 static bool
817 check_data_pointer_types (gfc_expr *expr1, gfc_expr *expr2)
819 gfc_component *cm1;
820 gfc_symbol *sym1;
821 gfc_symbol *sym2;
822 gfc_ref *ref1;
823 bool seen_component_ref;
825 if (expr1->expr_type != EXPR_VARIABLE
826 || expr1->expr_type != EXPR_VARIABLE)
827 return false;
829 sym1 = expr1->symtree->n.sym;
830 sym2 = expr2->symtree->n.sym;
832 /* Keep it simple for now. */
833 if (sym1->ts.type == BT_DERIVED && sym2->ts.type == BT_DERIVED)
834 return false;
836 if (sym1->attr.pointer)
838 if (gfc_compare_types (&sym1->ts, &sym2->ts))
839 return false;
842 /* This is a conservative check on the components of the derived type
843 if no component references have been seen. Since we will not dig
844 into the components of derived type components, we play it safe by
845 returning false. First we check the reference chain and then, if
846 no component references have been seen, the components. */
847 seen_component_ref = false;
848 if (sym1->ts.type == BT_DERIVED)
850 for (ref1 = expr1->ref; ref1; ref1 = ref1->next)
852 if (ref1->type != REF_COMPONENT)
853 continue;
855 if (ref1->u.c.component->ts.type == BT_DERIVED)
856 return false;
858 if ((sym2->attr.pointer || ref1->u.c.component->attr.pointer)
859 && gfc_compare_types (&ref1->u.c.component->ts, &sym2->ts))
860 return false;
862 seen_component_ref = true;
866 if (sym1->ts.type == BT_DERIVED && !seen_component_ref)
868 for (cm1 = sym1->ts.u.derived->components; cm1; cm1 = cm1->next)
870 if (cm1->ts.type == BT_DERIVED)
871 return false;
873 if ((sym2->attr.pointer || cm1->attr.pointer)
874 && gfc_compare_types (&cm1->ts, &sym2->ts))
875 return false;
879 return true;
883 /* Return true if the statement body redefines the condition. Returns
884 true if expr2 depends on expr1. expr1 should be a single term
885 suitable for the lhs of an assignment. The IDENTICAL flag indicates
886 whether array references to the same symbol with identical range
887 references count as a dependency or not. Used for forall and where
888 statements. Also used with functions returning arrays without a
889 temporary. */
892 gfc_check_dependency (gfc_expr *expr1, gfc_expr *expr2, bool identical)
894 gfc_actual_arglist *actual;
895 gfc_constructor *c;
896 int n;
898 gcc_assert (expr1->expr_type == EXPR_VARIABLE);
900 switch (expr2->expr_type)
902 case EXPR_OP:
903 n = gfc_check_dependency (expr1, expr2->value.op.op1, identical);
904 if (n)
905 return n;
906 if (expr2->value.op.op2)
907 return gfc_check_dependency (expr1, expr2->value.op.op2, identical);
908 return 0;
910 case EXPR_VARIABLE:
911 /* The interesting cases are when the symbols don't match. */
912 if (expr1->symtree->n.sym != expr2->symtree->n.sym)
914 gfc_typespec *ts1 = &expr1->symtree->n.sym->ts;
915 gfc_typespec *ts2 = &expr2->symtree->n.sym->ts;
917 /* Return 1 if expr1 and expr2 are equivalenced arrays. */
918 if (gfc_are_equivalenced_arrays (expr1, expr2))
919 return 1;
921 /* Symbols can only alias if they have the same type. */
922 if (ts1->type != BT_UNKNOWN && ts2->type != BT_UNKNOWN
923 && ts1->type != BT_DERIVED && ts2->type != BT_DERIVED)
925 if (ts1->type != ts2->type || ts1->kind != ts2->kind)
926 return 0;
929 /* If either variable is a pointer, assume the worst. */
930 /* TODO: -fassume-no-pointer-aliasing */
931 if (gfc_is_data_pointer (expr1) || gfc_is_data_pointer (expr2))
933 if (check_data_pointer_types (expr1, expr2)
934 && check_data_pointer_types (expr2, expr1))
935 return 0;
937 return 1;
939 else
941 gfc_symbol *sym1 = expr1->symtree->n.sym;
942 gfc_symbol *sym2 = expr2->symtree->n.sym;
943 if (sym1->attr.target && sym2->attr.target
944 && ((sym1->attr.dummy && !sym1->attr.contiguous
945 && (!sym1->attr.dimension
946 || sym2->as->type == AS_ASSUMED_SHAPE))
947 || (sym2->attr.dummy && !sym2->attr.contiguous
948 && (!sym2->attr.dimension
949 || sym2->as->type == AS_ASSUMED_SHAPE))))
950 return 1;
953 /* Otherwise distinct symbols have no dependencies. */
954 return 0;
957 if (identical)
958 return 1;
960 /* Identical and disjoint ranges return 0,
961 overlapping ranges return 1. */
962 if (expr1->ref && expr2->ref)
963 return gfc_dep_resolver (expr1->ref, expr2->ref, NULL);
965 return 1;
967 case EXPR_FUNCTION:
968 if (gfc_get_noncopying_intrinsic_argument (expr2) != NULL)
969 identical = 1;
971 /* Remember possible differences between elemental and
972 transformational functions. All functions inside a FORALL
973 will be pure. */
974 for (actual = expr2->value.function.actual;
975 actual; actual = actual->next)
977 if (!actual->expr)
978 continue;
979 n = gfc_check_dependency (expr1, actual->expr, identical);
980 if (n)
981 return n;
983 return 0;
985 case EXPR_CONSTANT:
986 case EXPR_NULL:
987 return 0;
989 case EXPR_ARRAY:
990 /* Loop through the array constructor's elements. */
991 for (c = gfc_constructor_first (expr2->value.constructor);
992 c; c = gfc_constructor_next (c))
994 /* If this is an iterator, assume the worst. */
995 if (c->iterator)
996 return 1;
997 /* Avoid recursion in the common case. */
998 if (c->expr->expr_type == EXPR_CONSTANT)
999 continue;
1000 if (gfc_check_dependency (expr1, c->expr, 1))
1001 return 1;
1003 return 0;
1005 default:
1006 return 1;
1011 /* Determines overlapping for two array sections. */
1013 static gfc_dependency
1014 check_section_vs_section (gfc_array_ref *l_ar, gfc_array_ref *r_ar, int n)
1016 gfc_expr *l_start;
1017 gfc_expr *l_end;
1018 gfc_expr *l_stride;
1019 gfc_expr *l_lower;
1020 gfc_expr *l_upper;
1021 int l_dir;
1023 gfc_expr *r_start;
1024 gfc_expr *r_end;
1025 gfc_expr *r_stride;
1026 gfc_expr *r_lower;
1027 gfc_expr *r_upper;
1028 int r_dir;
1029 bool identical_strides;
1031 /* If they are the same range, return without more ado. */
1032 if (gfc_is_same_range (l_ar, r_ar, n, 0))
1033 return GFC_DEP_EQUAL;
1035 l_start = l_ar->start[n];
1036 l_end = l_ar->end[n];
1037 l_stride = l_ar->stride[n];
1039 r_start = r_ar->start[n];
1040 r_end = r_ar->end[n];
1041 r_stride = r_ar->stride[n];
1043 /* If l_start is NULL take it from array specifier. */
1044 if (NULL == l_start && IS_ARRAY_EXPLICIT (l_ar->as))
1045 l_start = l_ar->as->lower[n];
1046 /* If l_end is NULL take it from array specifier. */
1047 if (NULL == l_end && IS_ARRAY_EXPLICIT (l_ar->as))
1048 l_end = l_ar->as->upper[n];
1050 /* If r_start is NULL take it from array specifier. */
1051 if (NULL == r_start && IS_ARRAY_EXPLICIT (r_ar->as))
1052 r_start = r_ar->as->lower[n];
1053 /* If r_end is NULL take it from array specifier. */
1054 if (NULL == r_end && IS_ARRAY_EXPLICIT (r_ar->as))
1055 r_end = r_ar->as->upper[n];
1057 /* Determine whether the l_stride is positive or negative. */
1058 if (!l_stride)
1059 l_dir = 1;
1060 else if (l_stride->expr_type == EXPR_CONSTANT
1061 && l_stride->ts.type == BT_INTEGER)
1062 l_dir = mpz_sgn (l_stride->value.integer);
1063 else if (l_start && l_end)
1064 l_dir = gfc_dep_compare_expr (l_end, l_start);
1065 else
1066 l_dir = -2;
1068 /* Determine whether the r_stride is positive or negative. */
1069 if (!r_stride)
1070 r_dir = 1;
1071 else if (r_stride->expr_type == EXPR_CONSTANT
1072 && r_stride->ts.type == BT_INTEGER)
1073 r_dir = mpz_sgn (r_stride->value.integer);
1074 else if (r_start && r_end)
1075 r_dir = gfc_dep_compare_expr (r_end, r_start);
1076 else
1077 r_dir = -2;
1079 /* The strides should never be zero. */
1080 if (l_dir == 0 || r_dir == 0)
1081 return GFC_DEP_OVERLAP;
1083 /* Determine if the strides are equal. */
1085 if (l_stride)
1087 if (r_stride)
1088 identical_strides = gfc_dep_compare_expr (l_stride, r_stride) == 0;
1089 else
1090 identical_strides = gfc_expr_is_one (l_stride, 0) == 1;
1092 else
1094 if (r_stride)
1095 identical_strides = gfc_expr_is_one (r_stride, 0) == 1;
1096 else
1097 identical_strides = true;
1100 /* Determine LHS upper and lower bounds. */
1101 if (l_dir == 1)
1103 l_lower = l_start;
1104 l_upper = l_end;
1106 else if (l_dir == -1)
1108 l_lower = l_end;
1109 l_upper = l_start;
1111 else
1113 l_lower = NULL;
1114 l_upper = NULL;
1117 /* Determine RHS upper and lower bounds. */
1118 if (r_dir == 1)
1120 r_lower = r_start;
1121 r_upper = r_end;
1123 else if (r_dir == -1)
1125 r_lower = r_end;
1126 r_upper = r_start;
1128 else
1130 r_lower = NULL;
1131 r_upper = NULL;
1134 /* Check whether the ranges are disjoint. */
1135 if (l_upper && r_lower && gfc_dep_compare_expr (l_upper, r_lower) == -1)
1136 return GFC_DEP_NODEP;
1137 if (r_upper && l_lower && gfc_dep_compare_expr (r_upper, l_lower) == -1)
1138 return GFC_DEP_NODEP;
1140 /* Handle cases like x:y:1 vs. x:z:-1 as GFC_DEP_EQUAL. */
1141 if (l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == 0)
1143 if (l_dir == 1 && r_dir == -1)
1144 return GFC_DEP_EQUAL;
1145 if (l_dir == -1 && r_dir == 1)
1146 return GFC_DEP_EQUAL;
1149 /* Handle cases like x:y:1 vs. z:y:-1 as GFC_DEP_EQUAL. */
1150 if (l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == 0)
1152 if (l_dir == 1 && r_dir == -1)
1153 return GFC_DEP_EQUAL;
1154 if (l_dir == -1 && r_dir == 1)
1155 return GFC_DEP_EQUAL;
1158 /* Handle cases like x:y:2 vs. x+1:z:4 as GFC_DEP_NODEP.
1159 There is no dependency if the remainder of
1160 (l_start - r_start) / gcd(l_stride, r_stride) is
1161 nonzero.
1162 TODO:
1163 - Handle cases where x is an expression.
1164 - Cases like a(1:4:2) = a(2:3) are still not handled.
1167 #define IS_CONSTANT_INTEGER(a) ((a) && ((a)->expr_type == EXPR_CONSTANT) \
1168 && (a)->ts.type == BT_INTEGER)
1170 if (IS_CONSTANT_INTEGER(l_start) && IS_CONSTANT_INTEGER(r_start)
1171 && IS_CONSTANT_INTEGER(l_stride) && IS_CONSTANT_INTEGER(r_stride))
1173 mpz_t gcd, tmp;
1174 int result;
1176 mpz_init (gcd);
1177 mpz_init (tmp);
1179 mpz_gcd (gcd, l_stride->value.integer, r_stride->value.integer);
1180 mpz_sub (tmp, l_start->value.integer, r_start->value.integer);
1182 mpz_fdiv_r (tmp, tmp, gcd);
1183 result = mpz_cmp_si (tmp, 0L);
1185 mpz_clear (gcd);
1186 mpz_clear (tmp);
1188 if (result != 0)
1189 return GFC_DEP_NODEP;
1192 #undef IS_CONSTANT_INTEGER
1194 /* Check for forward dependencies x:y vs. x+1:z. */
1195 if (l_dir == 1 && r_dir == 1
1196 && l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == -1
1197 && l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == -1)
1199 if (identical_strides)
1200 return GFC_DEP_FORWARD;
1203 /* Check for forward dependencies x:y:-1 vs. x-1:z:-1. */
1204 if (l_dir == -1 && r_dir == -1
1205 && l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == 1
1206 && l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == 1)
1208 if (identical_strides)
1209 return GFC_DEP_FORWARD;
1213 if (identical_strides)
1216 if (l_start && IS_ARRAY_EXPLICIT (l_ar->as))
1219 /* Check for a(low:y:s) vs. a(z:a:s) where a has a lower bound
1220 of low, which is always at least a forward dependence. */
1222 if (r_dir == 1
1223 && gfc_dep_compare_expr (l_start, l_ar->as->lower[n]) == 0)
1224 return GFC_DEP_FORWARD;
1226 /* Check for a(high:y:-s) vs. a(z:a:-s) where a has a higher bound
1227 of high, which is always at least a forward dependence. */
1229 if (r_dir == -1
1230 && gfc_dep_compare_expr (l_start, l_ar->as->upper[n]) == 0)
1231 return GFC_DEP_FORWARD;
1234 /* From here, check for backwards dependencies. */
1235 /* x:y vs. x+1:z. */
1236 if (l_dir == 1 && r_dir == 1
1237 && l_start && r_start
1238 && gfc_dep_compare_expr (l_start, r_start) == 1
1239 && l_end && r_end
1240 && gfc_dep_compare_expr (l_end, r_end) == 1)
1241 return GFC_DEP_BACKWARD;
1243 /* x:y:-1 vs. x-1:z:-1. */
1244 if (l_dir == -1 && r_dir == -1
1245 && l_start && r_start
1246 && gfc_dep_compare_expr (l_start, r_start) == -1
1247 && l_end && r_end
1248 && gfc_dep_compare_expr (l_end, r_end) == -1)
1249 return GFC_DEP_BACKWARD;
1252 return GFC_DEP_OVERLAP;
1256 /* Determines overlapping for a single element and a section. */
1258 static gfc_dependency
1259 gfc_check_element_vs_section( gfc_ref *lref, gfc_ref *rref, int n)
1261 gfc_array_ref *ref;
1262 gfc_expr *elem;
1263 gfc_expr *start;
1264 gfc_expr *end;
1265 gfc_expr *stride;
1266 int s;
1268 elem = lref->u.ar.start[n];
1269 if (!elem)
1270 return GFC_DEP_OVERLAP;
1272 ref = &rref->u.ar;
1273 start = ref->start[n] ;
1274 end = ref->end[n] ;
1275 stride = ref->stride[n];
1277 if (!start && IS_ARRAY_EXPLICIT (ref->as))
1278 start = ref->as->lower[n];
1279 if (!end && IS_ARRAY_EXPLICIT (ref->as))
1280 end = ref->as->upper[n];
1282 /* Determine whether the stride is positive or negative. */
1283 if (!stride)
1284 s = 1;
1285 else if (stride->expr_type == EXPR_CONSTANT
1286 && stride->ts.type == BT_INTEGER)
1287 s = mpz_sgn (stride->value.integer);
1288 else
1289 s = -2;
1291 /* Stride should never be zero. */
1292 if (s == 0)
1293 return GFC_DEP_OVERLAP;
1295 /* Positive strides. */
1296 if (s == 1)
1298 /* Check for elem < lower. */
1299 if (start && gfc_dep_compare_expr (elem, start) == -1)
1300 return GFC_DEP_NODEP;
1301 /* Check for elem > upper. */
1302 if (end && gfc_dep_compare_expr (elem, end) == 1)
1303 return GFC_DEP_NODEP;
1305 if (start && end)
1307 s = gfc_dep_compare_expr (start, end);
1308 /* Check for an empty range. */
1309 if (s == 1)
1310 return GFC_DEP_NODEP;
1311 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1312 return GFC_DEP_EQUAL;
1315 /* Negative strides. */
1316 else if (s == -1)
1318 /* Check for elem > upper. */
1319 if (end && gfc_dep_compare_expr (elem, start) == 1)
1320 return GFC_DEP_NODEP;
1321 /* Check for elem < lower. */
1322 if (start && gfc_dep_compare_expr (elem, end) == -1)
1323 return GFC_DEP_NODEP;
1325 if (start && end)
1327 s = gfc_dep_compare_expr (start, end);
1328 /* Check for an empty range. */
1329 if (s == -1)
1330 return GFC_DEP_NODEP;
1331 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1332 return GFC_DEP_EQUAL;
1335 /* Unknown strides. */
1336 else
1338 if (!start || !end)
1339 return GFC_DEP_OVERLAP;
1340 s = gfc_dep_compare_expr (start, end);
1341 if (s == -2)
1342 return GFC_DEP_OVERLAP;
1343 /* Assume positive stride. */
1344 if (s == -1)
1346 /* Check for elem < lower. */
1347 if (gfc_dep_compare_expr (elem, start) == -1)
1348 return GFC_DEP_NODEP;
1349 /* Check for elem > upper. */
1350 if (gfc_dep_compare_expr (elem, end) == 1)
1351 return GFC_DEP_NODEP;
1353 /* Assume negative stride. */
1354 else if (s == 1)
1356 /* Check for elem > upper. */
1357 if (gfc_dep_compare_expr (elem, start) == 1)
1358 return GFC_DEP_NODEP;
1359 /* Check for elem < lower. */
1360 if (gfc_dep_compare_expr (elem, end) == -1)
1361 return GFC_DEP_NODEP;
1363 /* Equal bounds. */
1364 else if (s == 0)
1366 s = gfc_dep_compare_expr (elem, start);
1367 if (s == 0)
1368 return GFC_DEP_EQUAL;
1369 if (s == 1 || s == -1)
1370 return GFC_DEP_NODEP;
1374 return GFC_DEP_OVERLAP;
1378 /* Traverse expr, checking all EXPR_VARIABLE symbols for their
1379 forall_index attribute. Return true if any variable may be
1380 being used as a FORALL index. Its safe to pessimistically
1381 return true, and assume a dependency. */
1383 static bool
1384 contains_forall_index_p (gfc_expr *expr)
1386 gfc_actual_arglist *arg;
1387 gfc_constructor *c;
1388 gfc_ref *ref;
1389 int i;
1391 if (!expr)
1392 return false;
1394 switch (expr->expr_type)
1396 case EXPR_VARIABLE:
1397 if (expr->symtree->n.sym->forall_index)
1398 return true;
1399 break;
1401 case EXPR_OP:
1402 if (contains_forall_index_p (expr->value.op.op1)
1403 || contains_forall_index_p (expr->value.op.op2))
1404 return true;
1405 break;
1407 case EXPR_FUNCTION:
1408 for (arg = expr->value.function.actual; arg; arg = arg->next)
1409 if (contains_forall_index_p (arg->expr))
1410 return true;
1411 break;
1413 case EXPR_CONSTANT:
1414 case EXPR_NULL:
1415 case EXPR_SUBSTRING:
1416 break;
1418 case EXPR_STRUCTURE:
1419 case EXPR_ARRAY:
1420 for (c = gfc_constructor_first (expr->value.constructor);
1421 c; gfc_constructor_next (c))
1422 if (contains_forall_index_p (c->expr))
1423 return true;
1424 break;
1426 default:
1427 gcc_unreachable ();
1430 for (ref = expr->ref; ref; ref = ref->next)
1431 switch (ref->type)
1433 case REF_ARRAY:
1434 for (i = 0; i < ref->u.ar.dimen; i++)
1435 if (contains_forall_index_p (ref->u.ar.start[i])
1436 || contains_forall_index_p (ref->u.ar.end[i])
1437 || contains_forall_index_p (ref->u.ar.stride[i]))
1438 return true;
1439 break;
1441 case REF_COMPONENT:
1442 break;
1444 case REF_SUBSTRING:
1445 if (contains_forall_index_p (ref->u.ss.start)
1446 || contains_forall_index_p (ref->u.ss.end))
1447 return true;
1448 break;
1450 default:
1451 gcc_unreachable ();
1454 return false;
1457 /* Determines overlapping for two single element array references. */
1459 static gfc_dependency
1460 gfc_check_element_vs_element (gfc_ref *lref, gfc_ref *rref, int n)
1462 gfc_array_ref l_ar;
1463 gfc_array_ref r_ar;
1464 gfc_expr *l_start;
1465 gfc_expr *r_start;
1466 int i;
1468 l_ar = lref->u.ar;
1469 r_ar = rref->u.ar;
1470 l_start = l_ar.start[n] ;
1471 r_start = r_ar.start[n] ;
1472 i = gfc_dep_compare_expr (r_start, l_start);
1473 if (i == 0)
1474 return GFC_DEP_EQUAL;
1476 /* Treat two scalar variables as potentially equal. This allows
1477 us to prove that a(i,:) and a(j,:) have no dependency. See
1478 Gerald Roth, "Evaluation of Array Syntax Dependence Analysis",
1479 Proceedings of the International Conference on Parallel and
1480 Distributed Processing Techniques and Applications (PDPTA2001),
1481 Las Vegas, Nevada, June 2001. */
1482 /* However, we need to be careful when either scalar expression
1483 contains a FORALL index, as these can potentially change value
1484 during the scalarization/traversal of this array reference. */
1485 if (contains_forall_index_p (r_start) || contains_forall_index_p (l_start))
1486 return GFC_DEP_OVERLAP;
1488 if (i != -2)
1489 return GFC_DEP_NODEP;
1490 return GFC_DEP_EQUAL;
1494 /* Determine if an array ref, usually an array section specifies the
1495 entire array. In addition, if the second, pointer argument is
1496 provided, the function will return true if the reference is
1497 contiguous; eg. (:, 1) gives true but (1,:) gives false. */
1499 bool
1500 gfc_full_array_ref_p (gfc_ref *ref, bool *contiguous)
1502 int i;
1503 int n;
1504 bool lbound_OK = true;
1505 bool ubound_OK = true;
1507 if (contiguous)
1508 *contiguous = false;
1510 if (ref->type != REF_ARRAY)
1511 return false;
1513 if (ref->u.ar.type == AR_FULL)
1515 if (contiguous)
1516 *contiguous = true;
1517 return true;
1520 if (ref->u.ar.type != AR_SECTION)
1521 return false;
1522 if (ref->next)
1523 return false;
1525 for (i = 0; i < ref->u.ar.dimen; i++)
1527 /* If we have a single element in the reference, for the reference
1528 to be full, we need to ascertain that the array has a single
1529 element in this dimension and that we actually reference the
1530 correct element. */
1531 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
1533 /* This is unconditionally a contiguous reference if all the
1534 remaining dimensions are elements. */
1535 if (contiguous)
1537 *contiguous = true;
1538 for (n = i + 1; n < ref->u.ar.dimen; n++)
1539 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
1540 *contiguous = false;
1543 if (!ref->u.ar.as
1544 || !ref->u.ar.as->lower[i]
1545 || !ref->u.ar.as->upper[i]
1546 || gfc_dep_compare_expr (ref->u.ar.as->lower[i],
1547 ref->u.ar.as->upper[i])
1548 || !ref->u.ar.start[i]
1549 || gfc_dep_compare_expr (ref->u.ar.start[i],
1550 ref->u.ar.as->lower[i]))
1551 return false;
1552 else
1553 continue;
1556 /* Check the lower bound. */
1557 if (ref->u.ar.start[i]
1558 && (!ref->u.ar.as
1559 || !ref->u.ar.as->lower[i]
1560 || gfc_dep_compare_expr (ref->u.ar.start[i],
1561 ref->u.ar.as->lower[i])))
1562 lbound_OK = false;
1563 /* Check the upper bound. */
1564 if (ref->u.ar.end[i]
1565 && (!ref->u.ar.as
1566 || !ref->u.ar.as->upper[i]
1567 || gfc_dep_compare_expr (ref->u.ar.end[i],
1568 ref->u.ar.as->upper[i])))
1569 ubound_OK = false;
1570 /* Check the stride. */
1571 if (ref->u.ar.stride[i]
1572 && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
1573 return false;
1575 /* This is unconditionally a contiguous reference as long as all
1576 the subsequent dimensions are elements. */
1577 if (contiguous)
1579 *contiguous = true;
1580 for (n = i + 1; n < ref->u.ar.dimen; n++)
1581 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
1582 *contiguous = false;
1585 if (!lbound_OK || !ubound_OK)
1586 return false;
1588 return true;
1592 /* Determine if a full array is the same as an array section with one
1593 variable limit. For this to be so, the strides must both be unity
1594 and one of either start == lower or end == upper must be true. */
1596 static bool
1597 ref_same_as_full_array (gfc_ref *full_ref, gfc_ref *ref)
1599 int i;
1600 bool upper_or_lower;
1602 if (full_ref->type != REF_ARRAY)
1603 return false;
1604 if (full_ref->u.ar.type != AR_FULL)
1605 return false;
1606 if (ref->type != REF_ARRAY)
1607 return false;
1608 if (ref->u.ar.type != AR_SECTION)
1609 return false;
1611 for (i = 0; i < ref->u.ar.dimen; i++)
1613 /* If we have a single element in the reference, we need to check
1614 that the array has a single element and that we actually reference
1615 the correct element. */
1616 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
1618 if (!full_ref->u.ar.as
1619 || !full_ref->u.ar.as->lower[i]
1620 || !full_ref->u.ar.as->upper[i]
1621 || gfc_dep_compare_expr (full_ref->u.ar.as->lower[i],
1622 full_ref->u.ar.as->upper[i])
1623 || !ref->u.ar.start[i]
1624 || gfc_dep_compare_expr (ref->u.ar.start[i],
1625 full_ref->u.ar.as->lower[i]))
1626 return false;
1629 /* Check the strides. */
1630 if (full_ref->u.ar.stride[i] && !gfc_expr_is_one (full_ref->u.ar.stride[i], 0))
1631 return false;
1632 if (ref->u.ar.stride[i] && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
1633 return false;
1635 upper_or_lower = false;
1636 /* Check the lower bound. */
1637 if (ref->u.ar.start[i]
1638 && (ref->u.ar.as
1639 && full_ref->u.ar.as->lower[i]
1640 && gfc_dep_compare_expr (ref->u.ar.start[i],
1641 full_ref->u.ar.as->lower[i]) == 0))
1642 upper_or_lower = true;
1643 /* Check the upper bound. */
1644 if (ref->u.ar.end[i]
1645 && (ref->u.ar.as
1646 && full_ref->u.ar.as->upper[i]
1647 && gfc_dep_compare_expr (ref->u.ar.end[i],
1648 full_ref->u.ar.as->upper[i]) == 0))
1649 upper_or_lower = true;
1650 if (!upper_or_lower)
1651 return false;
1653 return true;
1657 /* Finds if two array references are overlapping or not.
1658 Return value
1659 2 : array references are overlapping but reversal of one or
1660 more dimensions will clear the dependency.
1661 1 : array references are overlapping.
1662 0 : array references are identical or not overlapping. */
1665 gfc_dep_resolver (gfc_ref *lref, gfc_ref *rref, gfc_reverse *reverse)
1667 int n;
1668 gfc_dependency fin_dep;
1669 gfc_dependency this_dep;
1671 this_dep = GFC_DEP_ERROR;
1672 fin_dep = GFC_DEP_ERROR;
1673 /* Dependencies due to pointers should already have been identified.
1674 We only need to check for overlapping array references. */
1676 while (lref && rref)
1678 /* We're resolving from the same base symbol, so both refs should be
1679 the same type. We traverse the reference chain until we find ranges
1680 that are not equal. */
1681 gcc_assert (lref->type == rref->type);
1682 switch (lref->type)
1684 case REF_COMPONENT:
1685 /* The two ranges can't overlap if they are from different
1686 components. */
1687 if (lref->u.c.component != rref->u.c.component)
1688 return 0;
1689 break;
1691 case REF_SUBSTRING:
1692 /* Substring overlaps are handled by the string assignment code
1693 if there is not an underlying dependency. */
1694 return (fin_dep == GFC_DEP_OVERLAP) ? 1 : 0;
1696 case REF_ARRAY:
1698 if (ref_same_as_full_array (lref, rref))
1699 return 0;
1701 if (ref_same_as_full_array (rref, lref))
1702 return 0;
1704 if (lref->u.ar.dimen != rref->u.ar.dimen)
1706 if (lref->u.ar.type == AR_FULL)
1707 fin_dep = gfc_full_array_ref_p (rref, NULL) ? GFC_DEP_EQUAL
1708 : GFC_DEP_OVERLAP;
1709 else if (rref->u.ar.type == AR_FULL)
1710 fin_dep = gfc_full_array_ref_p (lref, NULL) ? GFC_DEP_EQUAL
1711 : GFC_DEP_OVERLAP;
1712 else
1713 return 1;
1714 break;
1717 for (n=0; n < lref->u.ar.dimen; n++)
1719 /* Assume dependency when either of array reference is vector
1720 subscript. */
1721 if (lref->u.ar.dimen_type[n] == DIMEN_VECTOR
1722 || rref->u.ar.dimen_type[n] == DIMEN_VECTOR)
1723 return 1;
1725 if (lref->u.ar.dimen_type[n] == DIMEN_RANGE
1726 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
1727 this_dep = check_section_vs_section (&lref->u.ar, &rref->u.ar, n);
1728 else if (lref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1729 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
1730 this_dep = gfc_check_element_vs_section (lref, rref, n);
1731 else if (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1732 && lref->u.ar.dimen_type[n] == DIMEN_RANGE)
1733 this_dep = gfc_check_element_vs_section (rref, lref, n);
1734 else
1736 gcc_assert (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1737 && lref->u.ar.dimen_type[n] == DIMEN_ELEMENT);
1738 this_dep = gfc_check_element_vs_element (rref, lref, n);
1741 /* If any dimension doesn't overlap, we have no dependency. */
1742 if (this_dep == GFC_DEP_NODEP)
1743 return 0;
1745 /* Now deal with the loop reversal logic: This only works on
1746 ranges and is activated by setting
1747 reverse[n] == GFC_CAN_REVERSE
1748 The ability to reverse or not is set by previous conditions
1749 in this dimension. If reversal is not activated, the
1750 value GFC_DEP_BACKWARD is reset to GFC_DEP_OVERLAP. */
1751 if (rref->u.ar.dimen_type[n] == DIMEN_RANGE
1752 && lref->u.ar.dimen_type[n] == DIMEN_RANGE)
1754 /* Set reverse if backward dependence and not inhibited. */
1755 if (reverse && reverse[n] != GFC_CANNOT_REVERSE)
1756 reverse[n] = (this_dep == GFC_DEP_BACKWARD) ?
1757 GFC_REVERSE_SET : reverse[n];
1759 /* Inhibit loop reversal if dependence not compatible. */
1760 if (reverse && reverse[n] != GFC_REVERSE_NOT_SET
1761 && this_dep != GFC_DEP_EQUAL
1762 && this_dep != GFC_DEP_BACKWARD
1763 && this_dep != GFC_DEP_NODEP)
1765 reverse[n] = GFC_CANNOT_REVERSE;
1766 if (this_dep != GFC_DEP_FORWARD)
1767 this_dep = GFC_DEP_OVERLAP;
1770 /* If no intention of reversing or reversing is explicitly
1771 inhibited, convert backward dependence to overlap. */
1772 if (this_dep == GFC_DEP_BACKWARD
1773 && (reverse == NULL || reverse[n] == GFC_CANNOT_REVERSE))
1774 this_dep = GFC_DEP_OVERLAP;
1777 /* Overlap codes are in order of priority. We only need to
1778 know the worst one.*/
1779 if (this_dep > fin_dep)
1780 fin_dep = this_dep;
1783 /* If this is an equal element, we have to keep going until we find
1784 the "real" array reference. */
1785 if (lref->u.ar.type == AR_ELEMENT
1786 && rref->u.ar.type == AR_ELEMENT
1787 && fin_dep == GFC_DEP_EQUAL)
1788 break;
1790 /* Exactly matching and forward overlapping ranges don't cause a
1791 dependency. */
1792 if (fin_dep < GFC_DEP_BACKWARD)
1793 return 0;
1795 /* Keep checking. We only have a dependency if
1796 subsequent references also overlap. */
1797 break;
1799 default:
1800 gcc_unreachable ();
1802 lref = lref->next;
1803 rref = rref->next;
1806 /* If we haven't seen any array refs then something went wrong. */
1807 gcc_assert (fin_dep != GFC_DEP_ERROR);
1809 /* Assume the worst if we nest to different depths. */
1810 if (lref || rref)
1811 return 1;
1813 return fin_dep == GFC_DEP_OVERLAP;