OSDN Git Service

* trans-decl.c (gfc_build_qualified_array): Clear DECL_IGNORED_P
[pf3gnuchains/gcc-fork.git] / gcc / fortran / dependency.c
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>
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>.  */
21
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.  */
26    
27 #include "config.h"
28 #include "gfortran.h"
29 #include "dependency.h"
30 #include "constructor.h"
31
32 /* static declarations */
33 /* Enums  */
34 enum range {LHS, RHS, MID};
35
36 /* Dependency types.  These must be in reverse order of priority.  */
37 typedef enum
38 {
39   GFC_DEP_ERROR,
40   GFC_DEP_EQUAL,        /* Identical Ranges.  */
41   GFC_DEP_FORWARD,      /* e.g., a(1:3), a(2:4).  */
42   GFC_DEP_OVERLAP,      /* May overlap in some other way.  */
43   GFC_DEP_NODEP         /* Distinct ranges.  */
44 }
45 gfc_dependency;
46
47 /* Macros */
48 #define IS_ARRAY_EXPLICIT(as) ((as->type == AS_EXPLICIT ? 1 : 0))
49
50
51 /* Returns 1 if the expr is an integer constant value 1, 0 if it is not or
52    def if the value could not be determined.  */
53
54 int
55 gfc_expr_is_one (gfc_expr *expr, int def)
56 {
57   gcc_assert (expr != NULL);
58
59   if (expr->expr_type != EXPR_CONSTANT)
60     return def;
61
62   if (expr->ts.type != BT_INTEGER)
63     return def;
64
65   return mpz_cmp_si (expr->value.integer, 1) == 0;
66 }
67
68
69 /* Compare two values.  Returns 0 if e1 == e2, -1 if e1 < e2, +1 if e1 > e2,
70    and -2 if the relationship could not be determined.  */
71
72 int
73 gfc_dep_compare_expr (gfc_expr *e1, gfc_expr *e2)
74 {
75   gfc_actual_arglist *args1;
76   gfc_actual_arglist *args2;
77   int i;
78
79   if (e1->expr_type == EXPR_OP
80       && (e1->value.op.op == INTRINSIC_UPLUS
81           || e1->value.op.op == INTRINSIC_PARENTHESES))
82     return gfc_dep_compare_expr (e1->value.op.op1, e2);
83   if (e2->expr_type == EXPR_OP
84       && (e2->value.op.op == INTRINSIC_UPLUS
85           || e2->value.op.op == INTRINSIC_PARENTHESES))
86     return gfc_dep_compare_expr (e1, e2->value.op.op1);
87
88   if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_PLUS)
89     {
90       /* Compare X+C vs. X.  */
91       if (e1->value.op.op2->expr_type == EXPR_CONSTANT
92           && e1->value.op.op2->ts.type == BT_INTEGER
93           && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
94         return mpz_sgn (e1->value.op.op2->value.integer);
95
96       /* Compare P+Q vs. R+S.  */
97       if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
98         {
99           int l, r;
100
101           l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
102           r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
103           if (l == 0 && r == 0)
104             return 0;
105           if (l == 0 && r != -2)
106             return r;
107           if (l != -2 && r == 0)
108             return l;
109           if (l == 1 && r == 1)
110             return 1;
111           if (l == -1 && r == -1)
112             return -1;
113
114           l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op2);
115           r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op1);
116           if (l == 0 && r == 0)
117             return 0;
118           if (l == 0 && r != -2)
119             return r;
120           if (l != -2 && r == 0)
121             return l;
122           if (l == 1 && r == 1)
123             return 1;
124           if (l == -1 && r == -1)
125             return -1;
126         }
127     }
128
129   /* Compare X vs. X+C.  */
130   if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
131     {
132       if (e2->value.op.op2->expr_type == EXPR_CONSTANT
133           && e2->value.op.op2->ts.type == BT_INTEGER
134           && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
135         return -mpz_sgn (e2->value.op.op2->value.integer);
136     }
137
138   /* Compare X-C vs. X.  */
139   if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_MINUS)
140     {
141       if (e1->value.op.op2->expr_type == EXPR_CONSTANT
142           && e1->value.op.op2->ts.type == BT_INTEGER
143           && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
144         return -mpz_sgn (e1->value.op.op2->value.integer);
145
146       /* Compare P-Q vs. R-S.  */
147       if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
148         {
149           int l, r;
150
151           l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
152           r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
153           if (l == 0 && r == 0)
154             return 0;
155           if (l != -2 && r == 0)
156             return l;
157           if (l == 0 && r != -2)
158             return -r;
159           if (l == 1 && r == -1)
160             return 1;
161           if (l == -1 && r == 1)
162             return -1;
163         }
164     }
165
166   /* Compare X vs. X-C.  */
167   if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
168     {
169       if (e2->value.op.op2->expr_type == EXPR_CONSTANT
170           && e2->value.op.op2->ts.type == BT_INTEGER
171           && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
172         return mpz_sgn (e2->value.op.op2->value.integer);
173     }
174
175   if (e1->expr_type != e2->expr_type)
176     return -2;
177
178   switch (e1->expr_type)
179     {
180     case EXPR_CONSTANT:
181       if (e1->ts.type != BT_INTEGER || e2->ts.type != BT_INTEGER)
182         return -2;
183
184       i = mpz_cmp (e1->value.integer, e2->value.integer);
185       if (i == 0)
186         return 0;
187       else if (i < 0)
188         return -1;
189       return 1;
190
191     case EXPR_VARIABLE:
192       if (e1->ref || e2->ref)
193         return -2;
194       if (e1->symtree->n.sym == e2->symtree->n.sym)
195         return 0;
196       return -2;
197
198     case EXPR_OP:
199       /* Intrinsic operators are the same if their operands are the same.  */
200       if (e1->value.op.op != e2->value.op.op)
201         return -2;
202       if (e1->value.op.op2 == 0)
203         {
204           i = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
205           return i == 0 ? 0 : -2;
206         }
207       if (gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1) == 0
208           && gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2) == 0)
209         return 0;
210       /* TODO Handle commutative binary operators here?  */
211       return -2;
212
213     case EXPR_FUNCTION:
214       /* We can only compare calls to the same intrinsic function.  */
215       if (e1->value.function.isym == 0 || e2->value.function.isym == 0
216           || e1->value.function.isym != e2->value.function.isym)
217         return -2;
218
219       args1 = e1->value.function.actual;
220       args2 = e2->value.function.actual;
221
222       /* We should list the "constant" intrinsic functions.  Those
223          without side-effects that provide equal results given equal
224          argument lists.  */
225       switch (e1->value.function.isym->id)
226         {
227         case GFC_ISYM_CONVERSION:
228           /* Handle integer extensions specially, as __convert_i4_i8
229              is not only "constant" but also "unary" and "increasing".  */
230           if (args1 && !args1->next
231               && args2 && !args2->next
232               && e1->ts.type == BT_INTEGER
233               && args1->expr->ts.type == BT_INTEGER
234               && e1->ts.kind > args1->expr->ts.kind
235               && e2->ts.type == e1->ts.type
236               && e2->ts.kind == e1->ts.kind
237               && args2->expr->ts.type == args1->expr->ts.type
238               && args2->expr->ts.kind == args2->expr->ts.kind)
239             return gfc_dep_compare_expr (args1->expr, args2->expr);
240           break;
241
242         case GFC_ISYM_REAL:
243         case GFC_ISYM_LOGICAL:
244         case GFC_ISYM_DBLE:
245           break;
246
247         default:
248           return -2;
249         }
250
251       /* Compare the argument lists for equality.  */
252       while (args1 && args2)
253         {
254           if (gfc_dep_compare_expr (args1->expr, args2->expr) != 0)
255             return -2;
256           args1 = args1->next;
257           args2 = args2->next;
258         }
259       return (args1 || args2) ? -2 : 0;
260       
261     default:
262       return -2;
263     }
264 }
265
266
267 /* Returns 1 if the two ranges are the same, 0 if they are not, and def
268    if the results are indeterminate.  N is the dimension to compare.  */
269
270 int
271 gfc_is_same_range (gfc_array_ref *ar1, gfc_array_ref *ar2, int n, int def)
272 {
273   gfc_expr *e1;
274   gfc_expr *e2;
275   int i;
276
277   /* TODO: More sophisticated range comparison.  */
278   gcc_assert (ar1 && ar2);
279
280   gcc_assert (ar1->dimen_type[n] == ar2->dimen_type[n]);
281
282   e1 = ar1->stride[n];
283   e2 = ar2->stride[n];
284   /* Check for mismatching strides.  A NULL stride means a stride of 1.  */
285   if (e1 && !e2)
286     {
287       i = gfc_expr_is_one (e1, -1);
288       if (i == -1)
289         return def;
290       else if (i == 0)
291         return 0;
292     }
293   else if (e2 && !e1)
294     {
295       i = gfc_expr_is_one (e2, -1);
296       if (i == -1)
297         return def;
298       else if (i == 0)
299         return 0;
300     }
301   else if (e1 && e2)
302     {
303       i = gfc_dep_compare_expr (e1, e2);
304       if (i == -2)
305         return def;
306       else if (i != 0)
307         return 0;
308     }
309   /* The strides match.  */
310
311   /* Check the range start.  */
312   e1 = ar1->start[n];
313   e2 = ar2->start[n];
314   if (e1 || e2)
315     {
316       /* Use the bound of the array if no bound is specified.  */
317       if (ar1->as && !e1)
318         e1 = ar1->as->lower[n];
319
320       if (ar2->as && !e2)
321         e2 = ar2->as->lower[n];
322
323       /* Check we have values for both.  */
324       if (!(e1 && e2))
325         return def;
326
327       i = gfc_dep_compare_expr (e1, e2);
328       if (i == -2)
329         return def;
330       else if (i != 0)
331         return 0;
332     }
333
334   /* Check the range end.  */
335   e1 = ar1->end[n];
336   e2 = ar2->end[n];
337   if (e1 || e2)
338     {
339       /* Use the bound of the array if no bound is specified.  */
340       if (ar1->as && !e1)
341         e1 = ar1->as->upper[n];
342
343       if (ar2->as && !e2)
344         e2 = ar2->as->upper[n];
345
346       /* Check we have values for both.  */
347       if (!(e1 && e2))
348         return def;
349
350       i = gfc_dep_compare_expr (e1, e2);
351       if (i == -2)
352         return def;
353       else if (i != 0)
354         return 0;
355     }
356
357   return 1;
358 }
359
360
361 /* Some array-returning intrinsics can be implemented by reusing the
362    data from one of the array arguments.  For example, TRANSPOSE does
363    not necessarily need to allocate new data: it can be implemented
364    by copying the original array's descriptor and simply swapping the
365    two dimension specifications.
366
367    If EXPR is a call to such an intrinsic, return the argument
368    whose data can be reused, otherwise return NULL.  */
369
370 gfc_expr *
371 gfc_get_noncopying_intrinsic_argument (gfc_expr *expr)
372 {
373   if (expr->expr_type != EXPR_FUNCTION || !expr->value.function.isym)
374     return NULL;
375
376   switch (expr->value.function.isym->id)
377     {
378     case GFC_ISYM_TRANSPOSE:
379       return expr->value.function.actual->expr;
380
381     default:
382       return NULL;
383     }
384 }
385
386
387 /* Return true if the result of reference REF can only be constructed
388    using a temporary array.  */
389
390 bool
391 gfc_ref_needs_temporary_p (gfc_ref *ref)
392 {
393   int n;
394   bool subarray_p;
395
396   subarray_p = false;
397   for (; ref; ref = ref->next)
398     switch (ref->type)
399       {
400       case REF_ARRAY:
401         /* Vector dimensions are generally not monotonic and must be
402            handled using a temporary.  */
403         if (ref->u.ar.type == AR_SECTION)
404           for (n = 0; n < ref->u.ar.dimen; n++)
405             if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR)
406               return true;
407
408         subarray_p = true;
409         break;
410
411       case REF_SUBSTRING:
412         /* Within an array reference, character substrings generally
413            need a temporary.  Character array strides are expressed as
414            multiples of the element size (consistent with other array
415            types), not in characters.  */
416         return subarray_p;
417
418       case REF_COMPONENT:
419         break;
420       }
421
422   return false;
423 }
424
425
426 int
427 gfc_is_data_pointer (gfc_expr *e)
428 {
429   gfc_ref *ref;
430
431   if (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION)
432     return 0;
433
434   /* No subreference if it is a function  */
435   gcc_assert (e->expr_type == EXPR_VARIABLE || !e->ref);
436
437   if (e->symtree->n.sym->attr.pointer)
438     return 1;
439
440   for (ref = e->ref; ref; ref = ref->next)
441     if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
442       return 1;
443
444   return 0;
445 }
446
447
448 /* Return true if array variable VAR could be passed to the same function
449    as argument EXPR without interfering with EXPR.  INTENT is the intent
450    of VAR.
451
452    This is considerably less conservative than other dependencies
453    because many function arguments will already be copied into a
454    temporary.  */
455
456 static int
457 gfc_check_argument_var_dependency (gfc_expr *var, sym_intent intent,
458                                    gfc_expr *expr, gfc_dep_check elemental)
459 {
460   gfc_expr *arg;
461
462   gcc_assert (var->expr_type == EXPR_VARIABLE);
463   gcc_assert (var->rank > 0);
464
465   switch (expr->expr_type)
466     {
467     case EXPR_VARIABLE:
468       /* In case of elemental subroutines, there is no dependency 
469          between two same-range array references.  */
470       if (gfc_ref_needs_temporary_p (expr->ref)
471           || gfc_check_dependency (var, expr, elemental == NOT_ELEMENTAL))
472         {
473           if (elemental == ELEM_DONT_CHECK_VARIABLE)
474             {
475               /* Too many false positive with pointers.  */
476               if (!gfc_is_data_pointer (var) && !gfc_is_data_pointer (expr))
477                 {
478                   /* Elemental procedures forbid unspecified intents, 
479                      and we don't check dependencies for INTENT_IN args.  */
480                   gcc_assert (intent == INTENT_OUT || intent == INTENT_INOUT);
481
482                   /* We are told not to check dependencies. 
483                      We do it, however, and issue a warning in case we find one.
484                      If a dependency is found in the case 
485                      elemental == ELEM_CHECK_VARIABLE, we will generate
486                      a temporary, so we don't need to bother the user.  */
487                   gfc_warning ("INTENT(%s) actual argument at %L might "
488                                "interfere with actual argument at %L.", 
489                                intent == INTENT_OUT ? "OUT" : "INOUT", 
490                                &var->where, &expr->where);
491                 }
492               return 0;
493             }
494           else
495             return 1; 
496         }
497       return 0;
498
499     case EXPR_ARRAY:
500       return gfc_check_dependency (var, expr, 1);
501
502     case EXPR_FUNCTION:
503       if (intent != INTENT_IN && expr->inline_noncopying_intrinsic
504           && (arg = gfc_get_noncopying_intrinsic_argument (expr))
505           && gfc_check_argument_var_dependency (var, intent, arg, elemental))
506         return 1;
507       if (elemental)
508         {
509           if ((expr->value.function.esym
510                && expr->value.function.esym->attr.elemental)
511               || (expr->value.function.isym
512                   && expr->value.function.isym->elemental))
513             return gfc_check_fncall_dependency (var, intent, NULL,
514                                                 expr->value.function.actual,
515                                                 ELEM_CHECK_VARIABLE);
516         }
517       return 0;
518
519     case EXPR_OP:
520       /* In case of non-elemental procedures, there is no need to catch
521          dependencies, as we will make a temporary anyway.  */
522       if (elemental)
523         {
524           /* If the actual arg EXPR is an expression, we need to catch 
525              a dependency between variables in EXPR and VAR, 
526              an intent((IN)OUT) variable.  */
527           if (expr->value.op.op1
528               && gfc_check_argument_var_dependency (var, intent, 
529                                                     expr->value.op.op1, 
530                                                     ELEM_CHECK_VARIABLE))
531             return 1;
532           else if (expr->value.op.op2
533                    && gfc_check_argument_var_dependency (var, intent, 
534                                                          expr->value.op.op2, 
535                                                          ELEM_CHECK_VARIABLE))
536             return 1;
537         }
538       return 0;
539
540     default:
541       return 0;
542     }
543 }
544   
545   
546 /* Like gfc_check_argument_var_dependency, but extended to any
547    array expression OTHER, not just variables.  */
548
549 static int
550 gfc_check_argument_dependency (gfc_expr *other, sym_intent intent,
551                                gfc_expr *expr, gfc_dep_check elemental)
552 {
553   switch (other->expr_type)
554     {
555     case EXPR_VARIABLE:
556       return gfc_check_argument_var_dependency (other, intent, expr, elemental);
557
558     case EXPR_FUNCTION:
559       if (other->inline_noncopying_intrinsic)
560         {
561           other = gfc_get_noncopying_intrinsic_argument (other);
562           return gfc_check_argument_dependency (other, INTENT_IN, expr, 
563                                                 elemental);
564         }
565       return 0;
566
567     default:
568       return 0;
569     }
570 }
571
572
573 /* Like gfc_check_argument_dependency, but check all the arguments in ACTUAL.
574    FNSYM is the function being called, or NULL if not known.  */
575
576 int
577 gfc_check_fncall_dependency (gfc_expr *other, sym_intent intent,
578                              gfc_symbol *fnsym, gfc_actual_arglist *actual,
579                              gfc_dep_check elemental)
580 {
581   gfc_formal_arglist *formal;
582   gfc_expr *expr;
583
584   formal = fnsym ? fnsym->formal : NULL;
585   for (; actual; actual = actual->next, formal = formal ? formal->next : NULL)
586     {
587       expr = actual->expr;
588
589       /* Skip args which are not present.  */
590       if (!expr)
591         continue;
592
593       /* Skip other itself.  */
594       if (expr == other)
595         continue;
596
597       /* Skip intent(in) arguments if OTHER itself is intent(in).  */
598       if (formal && intent == INTENT_IN
599           && formal->sym->attr.intent == INTENT_IN)
600         continue;
601
602       if (gfc_check_argument_dependency (other, intent, expr, elemental))
603         return 1;
604     }
605
606   return 0;
607 }
608
609
610 /* Return 1 if e1 and e2 are equivalenced arrays, either
611    directly or indirectly; i.e., equivalence (a,b) for a and b
612    or equivalence (a,c),(b,c).  This function uses the equiv_
613    lists, generated in trans-common(add_equivalences), that are
614    guaranteed to pick up indirect equivalences.  We explicitly
615    check for overlap using the offset and length of the equivalence.
616    This function is symmetric.
617    TODO: This function only checks whether the full top-level
618    symbols overlap.  An improved implementation could inspect
619    e1->ref and e2->ref to determine whether the actually accessed
620    portions of these variables/arrays potentially overlap.  */
621
622 int
623 gfc_are_equivalenced_arrays (gfc_expr *e1, gfc_expr *e2)
624 {
625   gfc_equiv_list *l;
626   gfc_equiv_info *s, *fl1, *fl2;
627
628   gcc_assert (e1->expr_type == EXPR_VARIABLE
629               && e2->expr_type == EXPR_VARIABLE);
630
631   if (!e1->symtree->n.sym->attr.in_equivalence
632       || !e2->symtree->n.sym->attr.in_equivalence|| !e1->rank || !e2->rank)
633     return 0;
634
635   if (e1->symtree->n.sym->ns
636         && e1->symtree->n.sym->ns != gfc_current_ns)
637     l = e1->symtree->n.sym->ns->equiv_lists;
638   else
639     l = gfc_current_ns->equiv_lists;
640
641   /* Go through the equiv_lists and return 1 if the variables
642      e1 and e2 are members of the same group and satisfy the
643      requirement on their relative offsets.  */
644   for (; l; l = l->next)
645     {
646       fl1 = NULL;
647       fl2 = NULL;
648       for (s = l->equiv; s; s = s->next)
649         {
650           if (s->sym == e1->symtree->n.sym)
651             {
652               fl1 = s;
653               if (fl2)
654                 break;
655             }
656           if (s->sym == e2->symtree->n.sym)
657             {
658               fl2 = s;
659               if (fl1)
660                 break;
661             }
662         }
663
664       if (s)
665         {
666           /* Can these lengths be zero?  */
667           if (fl1->length <= 0 || fl2->length <= 0)
668             return 1;
669           /* These can't overlap if [f11,fl1+length] is before 
670              [fl2,fl2+length], or [fl2,fl2+length] is before
671              [fl1,fl1+length], otherwise they do overlap.  */
672           if (fl1->offset + fl1->length > fl2->offset
673               && fl2->offset + fl2->length > fl1->offset)
674             return 1;
675         }
676     }
677   return 0;
678 }
679
680
681 /* Return true if there is no possibility of aliasing because of a type
682    mismatch between all the possible pointer references and the
683    potential target.  Note that this function is asymmetric in the
684    arguments and so must be called twice with the arguments exchanged.  */
685
686 static bool
687 check_data_pointer_types (gfc_expr *expr1, gfc_expr *expr2)
688 {
689   gfc_component *cm1;
690   gfc_symbol *sym1;
691   gfc_symbol *sym2;
692   gfc_ref *ref1;
693   bool seen_component_ref;
694
695   if (expr1->expr_type != EXPR_VARIABLE
696         || expr1->expr_type != EXPR_VARIABLE)
697     return false;
698
699   sym1 = expr1->symtree->n.sym;
700   sym2 = expr2->symtree->n.sym;
701
702   /* Keep it simple for now.  */
703   if (sym1->ts.type == BT_DERIVED && sym2->ts.type == BT_DERIVED)
704     return false;
705
706   if (sym1->attr.pointer)
707     {
708       if (gfc_compare_types (&sym1->ts, &sym2->ts))
709         return false;
710     }
711
712   /* This is a conservative check on the components of the derived type
713      if no component references have been seen.  Since we will not dig
714      into the components of derived type components, we play it safe by
715      returning false.  First we check the reference chain and then, if
716      no component references have been seen, the components.  */
717   seen_component_ref = false;
718   if (sym1->ts.type == BT_DERIVED)
719     {
720       for (ref1 = expr1->ref; ref1; ref1 = ref1->next)
721         {
722           if (ref1->type != REF_COMPONENT)
723             continue;
724
725           if (ref1->u.c.component->ts.type == BT_DERIVED)
726             return false;
727
728           if ((sym2->attr.pointer || ref1->u.c.component->attr.pointer)
729                 && gfc_compare_types (&ref1->u.c.component->ts, &sym2->ts))
730             return false;
731
732           seen_component_ref = true;
733         }
734     }
735
736   if (sym1->ts.type == BT_DERIVED && !seen_component_ref)
737     {
738       for (cm1 = sym1->ts.u.derived->components; cm1; cm1 = cm1->next)
739         {
740           if (cm1->ts.type == BT_DERIVED)
741             return false;
742
743           if ((sym2->attr.pointer || cm1->attr.pointer)
744                 && gfc_compare_types (&cm1->ts, &sym2->ts))
745             return false;
746         }
747     }
748
749   return true;
750 }
751
752
753 /* Return true if the statement body redefines the condition.  Returns
754    true if expr2 depends on expr1.  expr1 should be a single term
755    suitable for the lhs of an assignment.  The IDENTICAL flag indicates
756    whether array references to the same symbol with identical range
757    references count as a dependency or not.  Used for forall and where
758    statements.  Also used with functions returning arrays without a
759    temporary.  */
760
761 int
762 gfc_check_dependency (gfc_expr *expr1, gfc_expr *expr2, bool identical)
763 {
764   gfc_actual_arglist *actual;
765   gfc_constructor *c;
766   int n;
767
768   gcc_assert (expr1->expr_type == EXPR_VARIABLE);
769
770   switch (expr2->expr_type)
771     {
772     case EXPR_OP:
773       n = gfc_check_dependency (expr1, expr2->value.op.op1, identical);
774       if (n)
775         return n;
776       if (expr2->value.op.op2)
777         return gfc_check_dependency (expr1, expr2->value.op.op2, identical);
778       return 0;
779
780     case EXPR_VARIABLE:
781       /* The interesting cases are when the symbols don't match.  */
782       if (expr1->symtree->n.sym != expr2->symtree->n.sym)
783         {
784           gfc_typespec *ts1 = &expr1->symtree->n.sym->ts;
785           gfc_typespec *ts2 = &expr2->symtree->n.sym->ts;
786
787           /* Return 1 if expr1 and expr2 are equivalenced arrays.  */
788           if (gfc_are_equivalenced_arrays (expr1, expr2))
789             return 1;
790
791           /* Symbols can only alias if they have the same type.  */
792           if (ts1->type != BT_UNKNOWN && ts2->type != BT_UNKNOWN
793               && ts1->type != BT_DERIVED && ts2->type != BT_DERIVED)
794             {
795               if (ts1->type != ts2->type || ts1->kind != ts2->kind)
796                 return 0;
797             }
798
799           /* If either variable is a pointer, assume the worst.  */
800           /* TODO: -fassume-no-pointer-aliasing */
801           if (gfc_is_data_pointer (expr1) || gfc_is_data_pointer (expr2))
802             {
803               if (check_data_pointer_types (expr1, expr2)
804                     && check_data_pointer_types (expr2, expr1))
805                 return 0;
806
807               return 1;
808             }
809
810           /* Otherwise distinct symbols have no dependencies.  */
811           return 0;
812         }
813
814       if (identical)
815         return 1;
816
817       /* Identical and disjoint ranges return 0,
818          overlapping ranges return 1.  */
819       if (expr1->ref && expr2->ref)
820         return gfc_dep_resolver (expr1->ref, expr2->ref);
821
822       return 1;
823
824     case EXPR_FUNCTION:
825       if (expr2->inline_noncopying_intrinsic)
826         identical = 1;
827       /* Remember possible differences between elemental and
828          transformational functions.  All functions inside a FORALL
829          will be pure.  */
830       for (actual = expr2->value.function.actual;
831            actual; actual = actual->next)
832         {
833           if (!actual->expr)
834             continue;
835           n = gfc_check_dependency (expr1, actual->expr, identical);
836           if (n)
837             return n;
838         }
839       return 0;
840
841     case EXPR_CONSTANT:
842     case EXPR_NULL:
843       return 0;
844
845     case EXPR_ARRAY:
846       /* Loop through the array constructor's elements.  */
847       for (c = gfc_constructor_first (expr2->value.constructor);
848            c; c = gfc_constructor_next (c))
849         {
850           /* If this is an iterator, assume the worst.  */
851           if (c->iterator)
852             return 1;
853           /* Avoid recursion in the common case.  */
854           if (c->expr->expr_type == EXPR_CONSTANT)
855             continue;
856           if (gfc_check_dependency (expr1, c->expr, 1))
857             return 1;
858         }
859       return 0;
860
861     default:
862       return 1;
863     }
864 }
865
866
867 /* Determines overlapping for two array sections.  */
868
869 static gfc_dependency
870 gfc_check_section_vs_section (gfc_ref *lref, gfc_ref *rref, int n)
871 {
872   gfc_array_ref l_ar;
873   gfc_expr *l_start;
874   gfc_expr *l_end;
875   gfc_expr *l_stride;
876   gfc_expr *l_lower;
877   gfc_expr *l_upper;
878   int l_dir;
879
880   gfc_array_ref r_ar;
881   gfc_expr *r_start;
882   gfc_expr *r_end;
883   gfc_expr *r_stride;
884   gfc_expr *r_lower;
885   gfc_expr *r_upper;
886   int r_dir;
887
888   l_ar = lref->u.ar;
889   r_ar = rref->u.ar;
890   
891   /* If they are the same range, return without more ado.  */
892   if (gfc_is_same_range (&l_ar, &r_ar, n, 0))
893     return GFC_DEP_EQUAL;
894
895   l_start = l_ar.start[n];
896   l_end = l_ar.end[n];
897   l_stride = l_ar.stride[n];
898
899   r_start = r_ar.start[n];
900   r_end = r_ar.end[n];
901   r_stride = r_ar.stride[n];
902
903   /* If l_start is NULL take it from array specifier.  */
904   if (NULL == l_start && IS_ARRAY_EXPLICIT (l_ar.as))
905     l_start = l_ar.as->lower[n];
906   /* If l_end is NULL take it from array specifier.  */
907   if (NULL == l_end && IS_ARRAY_EXPLICIT (l_ar.as))
908     l_end = l_ar.as->upper[n];
909
910   /* If r_start is NULL take it from array specifier.  */
911   if (NULL == r_start && IS_ARRAY_EXPLICIT (r_ar.as))
912     r_start = r_ar.as->lower[n];
913   /* If r_end is NULL take it from array specifier.  */
914   if (NULL == r_end && IS_ARRAY_EXPLICIT (r_ar.as))
915     r_end = r_ar.as->upper[n];
916
917   /* Determine whether the l_stride is positive or negative.  */
918   if (!l_stride)
919     l_dir = 1;
920   else if (l_stride->expr_type == EXPR_CONSTANT
921            && l_stride->ts.type == BT_INTEGER)
922     l_dir = mpz_sgn (l_stride->value.integer);
923   else if (l_start && l_end)
924     l_dir = gfc_dep_compare_expr (l_end, l_start);
925   else
926     l_dir = -2;
927
928   /* Determine whether the r_stride is positive or negative.  */
929   if (!r_stride)
930     r_dir = 1;
931   else if (r_stride->expr_type == EXPR_CONSTANT
932            && r_stride->ts.type == BT_INTEGER)
933     r_dir = mpz_sgn (r_stride->value.integer);
934   else if (r_start && r_end)
935     r_dir = gfc_dep_compare_expr (r_end, r_start);
936   else
937     r_dir = -2;
938
939   /* The strides should never be zero.  */
940   if (l_dir == 0 || r_dir == 0)
941     return GFC_DEP_OVERLAP;
942
943   /* Determine LHS upper and lower bounds.  */
944   if (l_dir == 1)
945     {
946       l_lower = l_start;
947       l_upper = l_end;
948     }
949   else if (l_dir == -1)
950     {
951       l_lower = l_end;
952       l_upper = l_start;
953     }
954   else
955     {
956       l_lower = NULL;
957       l_upper = NULL;
958     }
959
960   /* Determine RHS upper and lower bounds.  */
961   if (r_dir == 1)
962     {
963       r_lower = r_start;
964       r_upper = r_end;
965     }
966   else if (r_dir == -1)
967     {
968       r_lower = r_end;
969       r_upper = r_start;
970     }
971   else
972     {
973       r_lower = NULL;
974       r_upper = NULL;
975     }
976
977   /* Check whether the ranges are disjoint.  */
978   if (l_upper && r_lower && gfc_dep_compare_expr (l_upper, r_lower) == -1)
979     return GFC_DEP_NODEP;
980   if (r_upper && l_lower && gfc_dep_compare_expr (r_upper, l_lower) == -1)
981     return GFC_DEP_NODEP;
982
983   /* Handle cases like x:y:1 vs. x:z:-1 as GFC_DEP_EQUAL.  */
984   if (l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == 0)
985     {
986       if (l_dir == 1 && r_dir == -1)
987         return GFC_DEP_EQUAL;
988       if (l_dir == -1 && r_dir == 1)
989         return GFC_DEP_EQUAL;
990     }
991
992   /* Handle cases like x:y:1 vs. z:y:-1 as GFC_DEP_EQUAL.  */
993   if (l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == 0)
994     {
995       if (l_dir == 1 && r_dir == -1)
996         return GFC_DEP_EQUAL;
997       if (l_dir == -1 && r_dir == 1)
998         return GFC_DEP_EQUAL;
999     }
1000
1001   /* Check for forward dependencies x:y vs. x+1:z.  */
1002   if (l_dir == 1 && r_dir == 1
1003       && l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == -1
1004       && l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == -1)
1005     {
1006       /* Check that the strides are the same.  */
1007       if (!l_stride && !r_stride)
1008         return GFC_DEP_FORWARD;
1009       if (l_stride && r_stride
1010           && gfc_dep_compare_expr (l_stride, r_stride) == 0)
1011         return GFC_DEP_FORWARD;
1012     }
1013
1014   /* Check for forward dependencies x:y:-1 vs. x-1:z:-1.  */
1015   if (l_dir == -1 && r_dir == -1
1016       && l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == 1
1017       && l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == 1)
1018     {
1019       /* Check that the strides are the same.  */
1020       if (!l_stride && !r_stride)
1021         return GFC_DEP_FORWARD;
1022       if (l_stride && r_stride
1023           && gfc_dep_compare_expr (l_stride, r_stride) == 0)
1024         return GFC_DEP_FORWARD;
1025     }
1026
1027   return GFC_DEP_OVERLAP;
1028 }
1029
1030
1031 /* Determines overlapping for a single element and a section.  */
1032
1033 static gfc_dependency
1034 gfc_check_element_vs_section( gfc_ref *lref, gfc_ref *rref, int n)
1035 {
1036   gfc_array_ref *ref;
1037   gfc_expr *elem;
1038   gfc_expr *start;
1039   gfc_expr *end;
1040   gfc_expr *stride;
1041   int s;
1042
1043   elem = lref->u.ar.start[n];
1044   if (!elem)
1045     return GFC_DEP_OVERLAP;
1046
1047   ref = &rref->u.ar;
1048   start = ref->start[n] ;
1049   end = ref->end[n] ;
1050   stride = ref->stride[n];
1051
1052   if (!start && IS_ARRAY_EXPLICIT (ref->as))
1053     start = ref->as->lower[n];
1054   if (!end && IS_ARRAY_EXPLICIT (ref->as))
1055     end = ref->as->upper[n];
1056
1057   /* Determine whether the stride is positive or negative.  */
1058   if (!stride)
1059     s = 1;
1060   else if (stride->expr_type == EXPR_CONSTANT
1061            && stride->ts.type == BT_INTEGER)
1062     s = mpz_sgn (stride->value.integer);
1063   else
1064     s = -2;
1065
1066   /* Stride should never be zero.  */
1067   if (s == 0)
1068     return GFC_DEP_OVERLAP;
1069
1070   /* Positive strides.  */
1071   if (s == 1)
1072     {
1073       /* Check for elem < lower.  */
1074       if (start && gfc_dep_compare_expr (elem, start) == -1)
1075         return GFC_DEP_NODEP;
1076       /* Check for elem > upper.  */
1077       if (end && gfc_dep_compare_expr (elem, end) == 1)
1078         return GFC_DEP_NODEP;
1079
1080       if (start && end)
1081         {
1082           s = gfc_dep_compare_expr (start, end);
1083           /* Check for an empty range.  */
1084           if (s == 1)
1085             return GFC_DEP_NODEP;
1086           if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1087             return GFC_DEP_EQUAL;
1088         }
1089     }
1090   /* Negative strides.  */
1091   else if (s == -1)
1092     {
1093       /* Check for elem > upper.  */
1094       if (end && gfc_dep_compare_expr (elem, start) == 1)
1095         return GFC_DEP_NODEP;
1096       /* Check for elem < lower.  */
1097       if (start && gfc_dep_compare_expr (elem, end) == -1)
1098         return GFC_DEP_NODEP;
1099
1100       if (start && end)
1101         {
1102           s = gfc_dep_compare_expr (start, end);
1103           /* Check for an empty range.  */
1104           if (s == -1)
1105             return GFC_DEP_NODEP;
1106           if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1107             return GFC_DEP_EQUAL;
1108         }
1109     }
1110   /* Unknown strides.  */
1111   else
1112     {
1113       if (!start || !end)
1114         return GFC_DEP_OVERLAP;
1115       s = gfc_dep_compare_expr (start, end);
1116       if (s == -2)
1117         return GFC_DEP_OVERLAP;
1118       /* Assume positive stride.  */
1119       if (s == -1)
1120         {
1121           /* Check for elem < lower.  */
1122           if (gfc_dep_compare_expr (elem, start) == -1)
1123             return GFC_DEP_NODEP;
1124           /* Check for elem > upper.  */
1125           if (gfc_dep_compare_expr (elem, end) == 1)
1126             return GFC_DEP_NODEP;
1127         }
1128       /* Assume negative stride.  */
1129       else if (s == 1)
1130         {
1131           /* Check for elem > upper.  */
1132           if (gfc_dep_compare_expr (elem, start) == 1)
1133             return GFC_DEP_NODEP;
1134           /* Check for elem < lower.  */
1135           if (gfc_dep_compare_expr (elem, end) == -1)
1136             return GFC_DEP_NODEP;
1137         }
1138       /* Equal bounds.  */
1139       else if (s == 0)
1140         {
1141           s = gfc_dep_compare_expr (elem, start);
1142           if (s == 0)
1143             return GFC_DEP_EQUAL;
1144           if (s == 1 || s == -1)
1145             return GFC_DEP_NODEP;
1146         }
1147     }
1148
1149   return GFC_DEP_OVERLAP;
1150 }
1151
1152
1153 /* Traverse expr, checking all EXPR_VARIABLE symbols for their
1154    forall_index attribute.  Return true if any variable may be
1155    being used as a FORALL index.  Its safe to pessimistically
1156    return true, and assume a dependency.  */
1157
1158 static bool
1159 contains_forall_index_p (gfc_expr *expr)
1160 {
1161   gfc_actual_arglist *arg;
1162   gfc_constructor *c;
1163   gfc_ref *ref;
1164   int i;
1165
1166   if (!expr)
1167     return false;
1168
1169   switch (expr->expr_type)
1170     {
1171     case EXPR_VARIABLE:
1172       if (expr->symtree->n.sym->forall_index)
1173         return true;
1174       break;
1175
1176     case EXPR_OP:
1177       if (contains_forall_index_p (expr->value.op.op1)
1178           || contains_forall_index_p (expr->value.op.op2))
1179         return true;
1180       break;
1181
1182     case EXPR_FUNCTION:
1183       for (arg = expr->value.function.actual; arg; arg = arg->next)
1184         if (contains_forall_index_p (arg->expr))
1185           return true;
1186       break;
1187
1188     case EXPR_CONSTANT:
1189     case EXPR_NULL:
1190     case EXPR_SUBSTRING:
1191       break;
1192
1193     case EXPR_STRUCTURE:
1194     case EXPR_ARRAY:
1195       for (c = gfc_constructor_first (expr->value.constructor);
1196            c; gfc_constructor_next (c))
1197         if (contains_forall_index_p (c->expr))
1198           return true;
1199       break;
1200
1201     default:
1202       gcc_unreachable ();
1203     }
1204
1205   for (ref = expr->ref; ref; ref = ref->next)
1206     switch (ref->type)
1207       {
1208       case REF_ARRAY:
1209         for (i = 0; i < ref->u.ar.dimen; i++)
1210           if (contains_forall_index_p (ref->u.ar.start[i])
1211               || contains_forall_index_p (ref->u.ar.end[i])
1212               || contains_forall_index_p (ref->u.ar.stride[i]))
1213             return true;
1214         break;
1215
1216       case REF_COMPONENT:
1217         break;
1218
1219       case REF_SUBSTRING:
1220         if (contains_forall_index_p (ref->u.ss.start)
1221             || contains_forall_index_p (ref->u.ss.end))
1222           return true;
1223         break;
1224
1225       default:
1226         gcc_unreachable ();
1227       }
1228
1229   return false;
1230 }
1231
1232 /* Determines overlapping for two single element array references.  */
1233
1234 static gfc_dependency
1235 gfc_check_element_vs_element (gfc_ref *lref, gfc_ref *rref, int n)
1236 {
1237   gfc_array_ref l_ar;
1238   gfc_array_ref r_ar;
1239   gfc_expr *l_start;
1240   gfc_expr *r_start;
1241   int i;
1242
1243   l_ar = lref->u.ar;
1244   r_ar = rref->u.ar;
1245   l_start = l_ar.start[n] ;
1246   r_start = r_ar.start[n] ;
1247   i = gfc_dep_compare_expr (r_start, l_start);
1248   if (i == 0)
1249     return GFC_DEP_EQUAL;
1250
1251   /* Treat two scalar variables as potentially equal.  This allows
1252      us to prove that a(i,:) and a(j,:) have no dependency.  See
1253      Gerald Roth, "Evaluation of Array Syntax Dependence Analysis",
1254      Proceedings of the International Conference on Parallel and
1255      Distributed Processing Techniques and Applications (PDPTA2001),
1256      Las Vegas, Nevada, June 2001.  */
1257   /* However, we need to be careful when either scalar expression
1258      contains a FORALL index, as these can potentially change value
1259      during the scalarization/traversal of this array reference.  */
1260   if (contains_forall_index_p (r_start) || contains_forall_index_p (l_start))
1261     return GFC_DEP_OVERLAP;
1262
1263   if (i != -2)
1264     return GFC_DEP_NODEP;
1265   return GFC_DEP_EQUAL;
1266 }
1267
1268
1269 /* Determine if an array ref, usually an array section specifies the
1270    entire array.  In addition, if the second, pointer argument is
1271    provided, the function will return true if the reference is
1272    contiguous; eg. (:, 1) gives true but (1,:) gives false.  */
1273
1274 bool
1275 gfc_full_array_ref_p (gfc_ref *ref, bool *contiguous)
1276 {
1277   int i;
1278   int n;
1279   bool lbound_OK = true;
1280   bool ubound_OK = true;
1281
1282   if (contiguous)
1283     *contiguous = false;
1284
1285   if (ref->type != REF_ARRAY)
1286     return false;
1287
1288   if (ref->u.ar.type == AR_FULL)
1289     {
1290       if (contiguous)
1291         *contiguous = true;
1292       return true;
1293     }
1294
1295   if (ref->u.ar.type != AR_SECTION)
1296     return false;
1297   if (ref->next)
1298     return false;
1299
1300   for (i = 0; i < ref->u.ar.dimen; i++)
1301     {
1302       /* If we have a single element in the reference, for the reference
1303          to be full, we need to ascertain that the array has a single
1304          element in this dimension and that we actually reference the
1305          correct element.  */
1306       if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
1307         {
1308           /* This is unconditionally a contiguous reference if all the
1309              remaining dimensions are elements.  */
1310           if (contiguous)
1311             {
1312               *contiguous = true;
1313               for (n = i + 1; n < ref->u.ar.dimen; n++)
1314                 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
1315                   *contiguous = false;
1316             }
1317
1318           if (!ref->u.ar.as
1319               || !ref->u.ar.as->lower[i]
1320               || !ref->u.ar.as->upper[i]
1321               || gfc_dep_compare_expr (ref->u.ar.as->lower[i],
1322                                        ref->u.ar.as->upper[i])
1323               || !ref->u.ar.start[i]
1324               || gfc_dep_compare_expr (ref->u.ar.start[i],
1325                                        ref->u.ar.as->lower[i]))
1326             return false;
1327           else
1328             continue;
1329         }
1330
1331       /* Check the lower bound.  */
1332       if (ref->u.ar.start[i]
1333           && (!ref->u.ar.as
1334               || !ref->u.ar.as->lower[i]
1335               || gfc_dep_compare_expr (ref->u.ar.start[i],
1336                                        ref->u.ar.as->lower[i])))
1337         lbound_OK = false;
1338       /* Check the upper bound.  */
1339       if (ref->u.ar.end[i]
1340           && (!ref->u.ar.as
1341               || !ref->u.ar.as->upper[i]
1342               || gfc_dep_compare_expr (ref->u.ar.end[i],
1343                                        ref->u.ar.as->upper[i])))
1344         ubound_OK = false;
1345       /* Check the stride.  */
1346       if (ref->u.ar.stride[i]
1347             && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
1348         return false;
1349
1350       /* This is unconditionally a contiguous reference as long as all
1351          the subsequent dimensions are elements.  */
1352       if (contiguous)
1353         {
1354           *contiguous = true;
1355           for (n = i + 1; n < ref->u.ar.dimen; n++)
1356             if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
1357               *contiguous = false;
1358         }
1359
1360       if (!lbound_OK || !ubound_OK)
1361         return false;
1362     }
1363   return true;
1364 }
1365
1366
1367 /* Determine if a full array is the same as an array section with one
1368    variable limit.  For this to be so, the strides must both be unity
1369    and one of either start == lower or end == upper must be true.  */
1370
1371 static bool
1372 ref_same_as_full_array (gfc_ref *full_ref, gfc_ref *ref)
1373 {
1374   int i;
1375   bool upper_or_lower;
1376
1377   if (full_ref->type != REF_ARRAY)
1378     return false;
1379   if (full_ref->u.ar.type != AR_FULL)
1380     return false;
1381   if (ref->type != REF_ARRAY)
1382     return false;
1383   if (ref->u.ar.type != AR_SECTION)
1384     return false;
1385
1386   for (i = 0; i < ref->u.ar.dimen; i++)
1387     {
1388       /* If we have a single element in the reference, we need to check
1389          that the array has a single element and that we actually reference
1390          the correct element.  */
1391       if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
1392         {
1393           if (!full_ref->u.ar.as
1394               || !full_ref->u.ar.as->lower[i]
1395               || !full_ref->u.ar.as->upper[i]
1396               || gfc_dep_compare_expr (full_ref->u.ar.as->lower[i],
1397                                        full_ref->u.ar.as->upper[i])
1398               || !ref->u.ar.start[i]
1399               || gfc_dep_compare_expr (ref->u.ar.start[i],
1400                                        full_ref->u.ar.as->lower[i]))
1401             return false;
1402         }
1403
1404       /* Check the strides.  */
1405       if (full_ref->u.ar.stride[i] && !gfc_expr_is_one (full_ref->u.ar.stride[i], 0))
1406         return false;
1407       if (ref->u.ar.stride[i] && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
1408         return false;
1409
1410       upper_or_lower = false;
1411       /* Check the lower bound.  */
1412       if (ref->u.ar.start[i]
1413           && (ref->u.ar.as
1414                 && full_ref->u.ar.as->lower[i]
1415                 && gfc_dep_compare_expr (ref->u.ar.start[i],
1416                                          full_ref->u.ar.as->lower[i]) == 0))
1417         upper_or_lower =  true;
1418       /* Check the upper bound.  */
1419       if (ref->u.ar.end[i]
1420           && (ref->u.ar.as
1421                 && full_ref->u.ar.as->upper[i]
1422                 && gfc_dep_compare_expr (ref->u.ar.end[i],
1423                                          full_ref->u.ar.as->upper[i]) == 0))
1424         upper_or_lower =  true;
1425       if (!upper_or_lower)
1426         return false;
1427     }
1428   return true;
1429 }
1430
1431
1432 /* Finds if two array references are overlapping or not.
1433    Return value
1434         1 : array references are overlapping.
1435         0 : array references are identical or not overlapping.  */
1436
1437 int
1438 gfc_dep_resolver (gfc_ref *lref, gfc_ref *rref)
1439 {
1440   int n;
1441   gfc_dependency fin_dep;
1442   gfc_dependency this_dep;
1443
1444   fin_dep = GFC_DEP_ERROR;
1445   /* Dependencies due to pointers should already have been identified.
1446      We only need to check for overlapping array references.  */
1447
1448   while (lref && rref)
1449     {
1450       /* We're resolving from the same base symbol, so both refs should be
1451          the same type.  We traverse the reference chain until we find ranges
1452          that are not equal.  */
1453       gcc_assert (lref->type == rref->type);
1454       switch (lref->type)
1455         {
1456         case REF_COMPONENT:
1457           /* The two ranges can't overlap if they are from different
1458              components.  */
1459           if (lref->u.c.component != rref->u.c.component)
1460             return 0;
1461           break;
1462           
1463         case REF_SUBSTRING:
1464           /* Substring overlaps are handled by the string assignment code
1465              if there is not an underlying dependency.  */
1466           return (fin_dep == GFC_DEP_OVERLAP) ? 1 : 0;
1467         
1468         case REF_ARRAY:
1469
1470           if (ref_same_as_full_array (lref, rref))
1471             return 0;
1472
1473           if (ref_same_as_full_array (rref, lref))
1474             return 0;
1475
1476           if (lref->u.ar.dimen != rref->u.ar.dimen)
1477             {
1478               if (lref->u.ar.type == AR_FULL)
1479                 fin_dep = gfc_full_array_ref_p (rref, NULL) ? GFC_DEP_EQUAL
1480                                                             : GFC_DEP_OVERLAP;
1481               else if (rref->u.ar.type == AR_FULL)
1482                 fin_dep = gfc_full_array_ref_p (lref, NULL) ? GFC_DEP_EQUAL
1483                                                             : GFC_DEP_OVERLAP;
1484               else
1485                 return 1;
1486               break;
1487             }
1488
1489           for (n=0; n < lref->u.ar.dimen; n++)
1490             {
1491               /* Assume dependency when either of array reference is vector
1492                  subscript.  */
1493               if (lref->u.ar.dimen_type[n] == DIMEN_VECTOR
1494                   || rref->u.ar.dimen_type[n] == DIMEN_VECTOR)
1495                 return 1;
1496               if (lref->u.ar.dimen_type[n] == DIMEN_RANGE
1497                   && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
1498                 this_dep = gfc_check_section_vs_section (lref, rref, n);
1499               else if (lref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1500                        && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
1501                 this_dep = gfc_check_element_vs_section (lref, rref, n);
1502               else if (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1503                        && lref->u.ar.dimen_type[n] == DIMEN_RANGE)
1504                 this_dep = gfc_check_element_vs_section (rref, lref, n);
1505               else 
1506                 {
1507                   gcc_assert (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
1508                               && lref->u.ar.dimen_type[n] == DIMEN_ELEMENT);
1509                   this_dep = gfc_check_element_vs_element (rref, lref, n);
1510                 }
1511
1512               /* If any dimension doesn't overlap, we have no dependency.  */
1513               if (this_dep == GFC_DEP_NODEP)
1514                 return 0;
1515
1516               /* Overlap codes are in order of priority.  We only need to
1517                  know the worst one.*/
1518               if (this_dep > fin_dep)
1519                 fin_dep = this_dep;
1520             }
1521
1522           /* If this is an equal element, we have to keep going until we find
1523              the "real" array reference.  */
1524           if (lref->u.ar.type == AR_ELEMENT
1525                 && rref->u.ar.type == AR_ELEMENT
1526                 && fin_dep == GFC_DEP_EQUAL)
1527             break;
1528
1529           /* Exactly matching and forward overlapping ranges don't cause a
1530              dependency.  */
1531           if (fin_dep < GFC_DEP_OVERLAP)
1532             return 0;
1533
1534           /* Keep checking.  We only have a dependency if
1535              subsequent references also overlap.  */
1536           break;
1537
1538         default:
1539           gcc_unreachable ();
1540         }
1541       lref = lref->next;
1542       rref = rref->next;
1543     }
1544
1545   /* If we haven't seen any array refs then something went wrong.  */
1546   gcc_assert (fin_dep != GFC_DEP_ERROR);
1547
1548   /* Assume the worst if we nest to different depths.  */
1549   if (lref || rref)
1550     return 1;
1551
1552   return fin_dep == GFC_DEP_OVERLAP;
1553 }
1554