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