OSDN Git Service

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