OSDN Git Service

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