OSDN Git Service

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