re PR target/57865 (Broken _save64gpr and _rest64gpr usage)
[official-gcc.git] / gcc / fortran / dependency.c
blob350c7bd07a2c065d1caeb408e6a14862b3cef269
1 /* Dependency analysis
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* dependency.c -- Expression dependency analysis code. */
22 /* There's probably quite a bit of duplication in this file. We currently
23 have different dependency checking functions for different types
24 if dependencies. Ideally these would probably be merged. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "gfortran.h"
30 #include "dependency.h"
31 #include "constructor.h"
32 #include "arith.h"
34 /* static declarations */
35 /* Enums */
36 enum range {LHS, RHS, MID};
38 /* Dependency types. These must be in reverse order of priority. */
39 typedef enum
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. */
48 gfc_dependency;
50 /* Macros */
51 #define IS_ARRAY_EXPLICIT(as) ((as->type == AS_EXPLICIT ? 1 : 0))
53 /* Forward declarations */
55 static gfc_dependency check_section_vs_section (gfc_array_ref *,
56 gfc_array_ref *, int);
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. */
61 int
62 gfc_expr_is_one (gfc_expr *expr, int def)
64 gcc_assert (expr != NULL);
66 if (expr->expr_type != EXPR_CONSTANT)
67 return def;
69 if (expr->ts.type != BT_INTEGER)
70 return def;
72 return mpz_cmp_si (expr->value.integer, 1) == 0;
75 /* Check if two array references are known to be identical. Calls
76 gfc_dep_compare_expr if necessary for comparing array indices. */
78 static bool
79 identical_array_ref (gfc_array_ref *a1, gfc_array_ref *a2)
81 int i;
83 if (a1->type == AR_FULL && a2->type == AR_FULL)
84 return true;
86 if (a1->type == AR_SECTION && a2->type == AR_SECTION)
88 gcc_assert (a1->dimen == a2->dimen);
90 for ( i = 0; i < a1->dimen; i++)
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;
97 if (check_section_vs_section (a1, a2, i) != GFC_DEP_EQUAL)
98 return false;
100 return true;
103 if (a1->type == AR_ELEMENT && a2->type == AR_ELEMENT)
105 gcc_assert (a1->dimen == a2->dimen);
106 for (i = 0; i < a1->dimen; i++)
108 if (gfc_dep_compare_expr (a1->start[i], a2->start[i]) != 0)
109 return false;
111 return true;
113 return false;
118 /* Return true for identical variables, checking for references if
119 necessary. Calls identical_array_ref for checking array sections. */
121 static bool
122 are_identical_variables (gfc_expr *e1, gfc_expr *e2)
124 gfc_ref *r1, *r2;
126 if (e1->symtree->n.sym->attr.dummy && e2->symtree->n.sym->attr.dummy)
128 /* Dummy arguments: Only check for equal names. */
129 if (e1->symtree->n.sym->name != e2->symtree->n.sym->name)
130 return false;
132 else
134 /* Check for equal symbols. */
135 if (e1->symtree->n.sym != e2->symtree->n.sym)
136 return false;
139 /* Volatile variables should never compare equal to themselves. */
141 if (e1->symtree->n.sym->attr.volatile_)
142 return false;
144 r1 = e1->ref;
145 r2 = e2->ref;
147 while (r1 != NULL || r2 != NULL)
150 /* Assume the variables are not equal if one has a reference and the
151 other doesn't.
152 TODO: Handle full references like comparing a(:) to a.
155 if (r1 == NULL || r2 == NULL)
156 return false;
158 if (r1->type != r2->type)
159 return false;
161 switch (r1->type)
164 case REF_ARRAY:
165 if (!identical_array_ref (&r1->u.ar, &r2->u.ar))
166 return false;
168 break;
170 case REF_COMPONENT:
171 if (r1->u.c.component != r2->u.c.component)
172 return false;
173 break;
175 case REF_SUBSTRING:
176 if (gfc_dep_compare_expr (r1->u.ss.start, r2->u.ss.start) != 0)
177 return false;
179 /* If both are NULL, the end length compares equal, because we
180 are looking at the same variable. This can only happen for
181 assumed- or deferred-length character arguments. */
183 if (r1->u.ss.end == NULL && r2->u.ss.end == NULL)
184 break;
186 if (gfc_dep_compare_expr (r1->u.ss.end, r2->u.ss.end) != 0)
187 return false;
189 break;
191 default:
192 gfc_internal_error ("are_identical_variables: Bad type");
194 r1 = r1->next;
195 r2 = r2->next;
197 return true;
200 /* Compare two functions for equality. Returns 0 if e1==e2, -2 otherwise. If
201 impure_ok is false, only return 0 for pure functions. */
204 gfc_dep_compare_functions (gfc_expr *e1, gfc_expr *e2, bool impure_ok)
207 gfc_actual_arglist *args1;
208 gfc_actual_arglist *args2;
210 if (e1->expr_type != EXPR_FUNCTION || e2->expr_type != EXPR_FUNCTION)
211 return -2;
213 if ((e1->value.function.esym && e2->value.function.esym
214 && e1->value.function.esym == e2->value.function.esym
215 && (e1->value.function.esym->result->attr.pure || impure_ok))
216 || (e1->value.function.isym && e2->value.function.isym
217 && e1->value.function.isym == e2->value.function.isym
218 && (e1->value.function.isym->pure || impure_ok)))
220 args1 = e1->value.function.actual;
221 args2 = e2->value.function.actual;
223 /* Compare the argument lists for equality. */
224 while (args1 && args2)
226 /* Bitwise xor, since C has no non-bitwise xor operator. */
227 if ((args1->expr == NULL) ^ (args2->expr == NULL))
228 return -2;
230 if (args1->expr != NULL && args2->expr != NULL
231 && gfc_dep_compare_expr (args1->expr, args2->expr) != 0)
232 return -2;
234 args1 = args1->next;
235 args2 = args2->next;
237 return (args1 || args2) ? -2 : 0;
239 else
240 return -2;
243 /* Compare two expressions. Return values:
244 * +1 if e1 > e2
245 * 0 if e1 == e2
246 * -1 if e1 < e2
247 * -2 if the relationship could not be determined
248 * -3 if e1 /= e2, but we cannot tell which one is larger.
249 REAL and COMPLEX constants are only compared for equality
250 or inequality; if they are unequal, -2 is returned in all cases. */
253 gfc_dep_compare_expr (gfc_expr *e1, gfc_expr *e2)
255 gfc_actual_arglist *args1;
256 gfc_actual_arglist *args2;
257 int i;
258 gfc_expr *n1, *n2;
260 n1 = NULL;
261 n2 = NULL;
263 if (e1 == NULL && e2 == NULL)
264 return 0;
266 /* Remove any integer conversion functions to larger types. */
267 if (e1->expr_type == EXPR_FUNCTION && e1->value.function.isym
268 && e1->value.function.isym->id == GFC_ISYM_CONVERSION
269 && e1->ts.type == BT_INTEGER)
271 args1 = e1->value.function.actual;
272 if (args1->expr->ts.type == BT_INTEGER
273 && e1->ts.kind > args1->expr->ts.kind)
274 n1 = args1->expr;
277 if (e2->expr_type == EXPR_FUNCTION && e2->value.function.isym
278 && e2->value.function.isym->id == GFC_ISYM_CONVERSION
279 && e2->ts.type == BT_INTEGER)
281 args2 = e2->value.function.actual;
282 if (args2->expr->ts.type == BT_INTEGER
283 && e2->ts.kind > args2->expr->ts.kind)
284 n2 = args2->expr;
287 if (n1 != NULL)
289 if (n2 != NULL)
290 return gfc_dep_compare_expr (n1, n2);
291 else
292 return gfc_dep_compare_expr (n1, e2);
294 else
296 if (n2 != NULL)
297 return gfc_dep_compare_expr (e1, n2);
300 if (e1->expr_type == EXPR_OP
301 && (e1->value.op.op == INTRINSIC_UPLUS
302 || e1->value.op.op == INTRINSIC_PARENTHESES))
303 return gfc_dep_compare_expr (e1->value.op.op1, e2);
304 if (e2->expr_type == EXPR_OP
305 && (e2->value.op.op == INTRINSIC_UPLUS
306 || e2->value.op.op == INTRINSIC_PARENTHESES))
307 return gfc_dep_compare_expr (e1, e2->value.op.op1);
309 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_PLUS)
311 /* Compare X+C vs. X, for INTEGER only. */
312 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
313 && e1->value.op.op2->ts.type == BT_INTEGER
314 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
315 return mpz_sgn (e1->value.op.op2->value.integer);
317 /* Compare P+Q vs. R+S. */
318 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
320 int l, r;
322 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
323 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
324 if (l == 0 && r == 0)
325 return 0;
326 if (l == 0 && r > -2)
327 return r;
328 if (l > -2 && r == 0)
329 return l;
330 if (l == 1 && r == 1)
331 return 1;
332 if (l == -1 && r == -1)
333 return -1;
335 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op2);
336 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op1);
337 if (l == 0 && r == 0)
338 return 0;
339 if (l == 0 && r > -2)
340 return r;
341 if (l > -2 && r == 0)
342 return l;
343 if (l == 1 && r == 1)
344 return 1;
345 if (l == -1 && r == -1)
346 return -1;
350 /* Compare X vs. X+C, for INTEGER only. */
351 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
353 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
354 && e2->value.op.op2->ts.type == BT_INTEGER
355 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
356 return -mpz_sgn (e2->value.op.op2->value.integer);
359 /* Compare X-C vs. X, for INTEGER only. */
360 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_MINUS)
362 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
363 && e1->value.op.op2->ts.type == BT_INTEGER
364 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
365 return -mpz_sgn (e1->value.op.op2->value.integer);
367 /* Compare P-Q vs. R-S. */
368 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
370 int l, r;
372 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
373 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
374 if (l == 0 && r == 0)
375 return 0;
376 if (l > -2 && r == 0)
377 return l;
378 if (l == 0 && r > -2)
379 return -r;
380 if (l == 1 && r == -1)
381 return 1;
382 if (l == -1 && r == 1)
383 return -1;
387 /* Compare A // B vs. C // D. */
389 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_CONCAT
390 && e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_CONCAT)
392 int l, r;
394 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
395 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
397 if (l != 0)
398 return l;
400 /* Left expressions of // compare equal, but
401 watch out for 'A ' // x vs. 'A' // x. */
402 gfc_expr *e1_left = e1->value.op.op1;
403 gfc_expr *e2_left = e2->value.op.op1;
405 if (e1_left->expr_type == EXPR_CONSTANT
406 && e2_left->expr_type == EXPR_CONSTANT
407 && e1_left->value.character.length
408 != e2_left->value.character.length)
409 return -2;
410 else
411 return r;
414 /* Compare X vs. X-C, for INTEGER only. */
415 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
417 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
418 && e2->value.op.op2->ts.type == BT_INTEGER
419 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
420 return mpz_sgn (e2->value.op.op2->value.integer);
423 if (e1->expr_type != e2->expr_type)
424 return -3;
426 switch (e1->expr_type)
428 case EXPR_CONSTANT:
429 /* Compare strings for equality. */
430 if (e1->ts.type == BT_CHARACTER && e2->ts.type == BT_CHARACTER)
431 return gfc_compare_string (e1, e2);
433 /* Compare REAL and COMPLEX constants. Because of the
434 traps and pitfalls associated with comparing
435 a + 1.0 with a + 0.5, check for equality only. */
436 if (e2->expr_type == EXPR_CONSTANT)
438 if (e1->ts.type == BT_REAL && e2->ts.type == BT_REAL)
440 if (mpfr_cmp (e1->value.real, e2->value.real) == 0)
441 return 0;
442 else
443 return -2;
445 else if (e1->ts.type == BT_COMPLEX && e2->ts.type == BT_COMPLEX)
447 if (mpc_cmp (e1->value.complex, e2->value.complex) == 0)
448 return 0;
449 else
450 return -2;
454 if (e1->ts.type != BT_INTEGER || e2->ts.type != BT_INTEGER)
455 return -2;
457 /* For INTEGER, all cases where e2 is not constant should have
458 been filtered out above. */
459 gcc_assert (e2->expr_type == EXPR_CONSTANT);
461 i = mpz_cmp (e1->value.integer, e2->value.integer);
462 if (i == 0)
463 return 0;
464 else if (i < 0)
465 return -1;
466 return 1;
468 case EXPR_VARIABLE:
469 if (are_identical_variables (e1, e2))
470 return 0;
471 else
472 return -3;
474 case EXPR_OP:
475 /* Intrinsic operators are the same if their operands are the same. */
476 if (e1->value.op.op != e2->value.op.op)
477 return -2;
478 if (e1->value.op.op2 == 0)
480 i = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
481 return i == 0 ? 0 : -2;
483 if (gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1) == 0
484 && gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2) == 0)
485 return 0;
486 else if (e1->value.op.op == INTRINSIC_TIMES
487 && gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op2) == 0
488 && gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op1) == 0)
489 /* Commutativity of multiplication; addition is handled above. */
490 return 0;
492 return -2;
494 case EXPR_FUNCTION:
495 return gfc_dep_compare_functions (e1, e2, false);
496 break;
498 default:
499 return -2;
504 /* Helper function to look through parens and unary plus. */
506 static gfc_expr*
507 discard_nops (gfc_expr *e)
510 while (e && e->expr_type == EXPR_OP
511 && (e->value.op.op == INTRINSIC_UPLUS
512 || e->value.op.op == INTRINSIC_PARENTHESES))
513 e = e->value.op.op1;
515 return e;
519 /* Return the difference between two expressions. Integer expressions of
520 the form
522 X + constant, X - constant and constant + X
524 are handled. Return true on success, false on failure. result is assumed
525 to be uninitialized on entry, and will be initialized on success.
528 bool
529 gfc_dep_difference (gfc_expr *e1, gfc_expr *e2, mpz_t *result)
531 gfc_expr *e1_op1, *e1_op2, *e2_op1, *e2_op2;
533 if (e1 == NULL || e2 == NULL)
534 return false;
536 if (e1->ts.type != BT_INTEGER || e2->ts.type != BT_INTEGER)
537 return false;
539 e1 = discard_nops (e1);
540 e2 = discard_nops (e2);
542 /* Inizialize tentatively, clear if we don't return anything. */
543 mpz_init (*result);
545 /* Case 1: c1 - c2 = c1 - c2, trivially. */
547 if (e1->expr_type == EXPR_CONSTANT && e2->expr_type == EXPR_CONSTANT)
549 mpz_sub (*result, e1->value.integer, e2->value.integer);
550 return true;
553 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_PLUS)
555 e1_op1 = discard_nops (e1->value.op.op1);
556 e1_op2 = discard_nops (e1->value.op.op2);
558 /* Case 2: (X + c1) - X = c1. */
559 if (e1_op2->expr_type == EXPR_CONSTANT
560 && gfc_dep_compare_expr (e1_op1, e2) == 0)
562 mpz_set (*result, e1_op2->value.integer);
563 return true;
566 /* Case 3: (c1 + X) - X = c1. */
567 if (e1_op1->expr_type == EXPR_CONSTANT
568 && gfc_dep_compare_expr (e1_op2, e2) == 0)
570 mpz_set (*result, e1_op1->value.integer);
571 return true;
574 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
576 e2_op1 = discard_nops (e2->value.op.op1);
577 e2_op2 = discard_nops (e2->value.op.op2);
579 if (e1_op2->expr_type == EXPR_CONSTANT)
581 /* Case 4: X + c1 - (X + c2) = c1 - c2. */
582 if (e2_op2->expr_type == EXPR_CONSTANT
583 && gfc_dep_compare_expr (e1_op1, e2_op1) == 0)
585 mpz_sub (*result, e1_op2->value.integer,
586 e2_op2->value.integer);
587 return true;
589 /* Case 5: X + c1 - (c2 + X) = c1 - c2. */
590 if (e2_op1->expr_type == EXPR_CONSTANT
591 && gfc_dep_compare_expr (e1_op1, e2_op2) == 0)
593 mpz_sub (*result, e1_op2->value.integer,
594 e2_op1->value.integer);
595 return true;
598 else if (e1_op1->expr_type == EXPR_CONSTANT)
600 /* Case 6: c1 + X - (X + c2) = c1 - c2. */
601 if (e2_op2->expr_type == EXPR_CONSTANT
602 && gfc_dep_compare_expr (e1_op2, e2_op1) == 0)
604 mpz_sub (*result, e1_op1->value.integer,
605 e2_op2->value.integer);
606 return true;
608 /* Case 7: c1 + X - (c2 + X) = c1 - c2. */
609 if (e2_op1->expr_type == EXPR_CONSTANT
610 && gfc_dep_compare_expr (e1_op2, e2_op2) == 0)
612 mpz_sub (*result, e1_op1->value.integer,
613 e2_op1->value.integer);
614 return true;
619 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
621 e2_op1 = discard_nops (e2->value.op.op1);
622 e2_op2 = discard_nops (e2->value.op.op2);
624 if (e1_op2->expr_type == EXPR_CONSTANT)
626 /* Case 8: X + c1 - (X - c2) = c1 + c2. */
627 if (e2_op2->expr_type == EXPR_CONSTANT
628 && gfc_dep_compare_expr (e1_op1, e2_op1) == 0)
630 mpz_add (*result, e1_op2->value.integer,
631 e2_op2->value.integer);
632 return true;
635 if (e1_op1->expr_type == EXPR_CONSTANT)
637 /* Case 9: c1 + X - (X - c2) = c1 + c2. */
638 if (e2_op2->expr_type == EXPR_CONSTANT
639 && gfc_dep_compare_expr (e1_op2, e2_op1) == 0)
641 mpz_add (*result, e1_op1->value.integer,
642 e2_op2->value.integer);
643 return true;
649 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_MINUS)
651 e1_op1 = discard_nops (e1->value.op.op1);
652 e1_op2 = discard_nops (e1->value.op.op2);
654 if (e1_op2->expr_type == EXPR_CONSTANT)
656 /* Case 10: (X - c1) - X = -c1 */
658 if (gfc_dep_compare_expr (e1_op1, e2) == 0)
660 mpz_neg (*result, e1_op2->value.integer);
661 return true;
664 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
666 e2_op1 = discard_nops (e2->value.op.op1);
667 e2_op2 = discard_nops (e2->value.op.op2);
669 /* Case 11: (X - c1) - (X + c2) = -( c1 + c2). */
670 if (e2_op2->expr_type == EXPR_CONSTANT
671 && gfc_dep_compare_expr (e1_op1, e2_op1) == 0)
673 mpz_add (*result, e1_op2->value.integer,
674 e2_op2->value.integer);
675 mpz_neg (*result, *result);
676 return true;
679 /* Case 12: X - c1 - (c2 + X) = - (c1 + c2). */
680 if (e2_op1->expr_type == EXPR_CONSTANT
681 && gfc_dep_compare_expr (e1_op1, e2_op2) == 0)
683 mpz_add (*result, e1_op2->value.integer,
684 e2_op1->value.integer);
685 mpz_neg (*result, *result);
686 return true;
690 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
692 e2_op1 = discard_nops (e2->value.op.op1);
693 e2_op2 = discard_nops (e2->value.op.op2);
695 /* Case 13: (X - c1) - (X - c2) = c2 - c1. */
696 if (e2_op2->expr_type == EXPR_CONSTANT
697 && gfc_dep_compare_expr (e1_op1, e2_op1) == 0)
699 mpz_sub (*result, e2_op2->value.integer,
700 e1_op2->value.integer);
701 return true;
705 if (e1_op1->expr_type == EXPR_CONSTANT)
707 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
709 e2_op1 = discard_nops (e2->value.op.op1);
710 e2_op2 = discard_nops (e2->value.op.op2);
712 /* Case 14: (c1 - X) - (c2 - X) == c1 - c2. */
713 if (gfc_dep_compare_expr (e1_op2, e2_op2) == 0)
715 mpz_sub (*result, e1_op1->value.integer,
716 e2_op1->value.integer);
717 return true;
724 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
726 e2_op1 = discard_nops (e2->value.op.op1);
727 e2_op2 = discard_nops (e2->value.op.op2);
729 /* Case 15: X - (X + c2) = -c2. */
730 if (e2_op2->expr_type == EXPR_CONSTANT
731 && gfc_dep_compare_expr (e1, e2_op1) == 0)
733 mpz_neg (*result, e2_op2->value.integer);
734 return true;
736 /* Case 16: X - (c2 + X) = -c2. */
737 if (e2_op1->expr_type == EXPR_CONSTANT
738 && gfc_dep_compare_expr (e1, e2_op2) == 0)
740 mpz_neg (*result, e2_op1->value.integer);
741 return true;
745 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
747 e2_op1 = discard_nops (e2->value.op.op1);
748 e2_op2 = discard_nops (e2->value.op.op2);
750 /* Case 17: X - (X - c2) = c2. */
751 if (e2_op2->expr_type == EXPR_CONSTANT
752 && gfc_dep_compare_expr (e1, e2_op1) == 0)
754 mpz_set (*result, e2_op2->value.integer);
755 return true;
759 if (gfc_dep_compare_expr (e1, e2) == 0)
761 /* Case 18: X - X = 0. */
762 mpz_set_si (*result, 0);
763 return true;
766 mpz_clear (*result);
767 return false;
770 /* Returns 1 if the two ranges are the same and 0 if they are not (or if the
771 results are indeterminate). 'n' is the dimension to compare. */
773 static int
774 is_same_range (gfc_array_ref *ar1, gfc_array_ref *ar2, int n)
776 gfc_expr *e1;
777 gfc_expr *e2;
778 int i;
780 /* TODO: More sophisticated range comparison. */
781 gcc_assert (ar1 && ar2);
783 gcc_assert (ar1->dimen_type[n] == ar2->dimen_type[n]);
785 e1 = ar1->stride[n];
786 e2 = ar2->stride[n];
787 /* Check for mismatching strides. A NULL stride means a stride of 1. */
788 if (e1 && !e2)
790 i = gfc_expr_is_one (e1, -1);
791 if (i == -1 || i == 0)
792 return 0;
794 else if (e2 && !e1)
796 i = gfc_expr_is_one (e2, -1);
797 if (i == -1 || i == 0)
798 return 0;
800 else if (e1 && e2)
802 i = gfc_dep_compare_expr (e1, e2);
803 if (i != 0)
804 return 0;
806 /* The strides match. */
808 /* Check the range start. */
809 e1 = ar1->start[n];
810 e2 = ar2->start[n];
811 if (e1 || e2)
813 /* Use the bound of the array if no bound is specified. */
814 if (ar1->as && !e1)
815 e1 = ar1->as->lower[n];
817 if (ar2->as && !e2)
818 e2 = ar2->as->lower[n];
820 /* Check we have values for both. */
821 if (!(e1 && e2))
822 return 0;
824 i = gfc_dep_compare_expr (e1, e2);
825 if (i != 0)
826 return 0;
829 /* Check the range end. */
830 e1 = ar1->end[n];
831 e2 = ar2->end[n];
832 if (e1 || e2)
834 /* Use the bound of the array if no bound is specified. */
835 if (ar1->as && !e1)
836 e1 = ar1->as->upper[n];
838 if (ar2->as && !e2)
839 e2 = ar2->as->upper[n];
841 /* Check we have values for both. */
842 if (!(e1 && e2))
843 return 0;
845 i = gfc_dep_compare_expr (e1, e2);
846 if (i != 0)
847 return 0;
850 return 1;
854 /* Some array-returning intrinsics can be implemented by reusing the
855 data from one of the array arguments. For example, TRANSPOSE does
856 not necessarily need to allocate new data: it can be implemented
857 by copying the original array's descriptor and simply swapping the
858 two dimension specifications.
860 If EXPR is a call to such an intrinsic, return the argument
861 whose data can be reused, otherwise return NULL. */
863 gfc_expr *
864 gfc_get_noncopying_intrinsic_argument (gfc_expr *expr)
866 if (expr->expr_type != EXPR_FUNCTION || !expr->value.function.isym)
867 return NULL;
869 switch (expr->value.function.isym->id)
871 case GFC_ISYM_TRANSPOSE:
872 return expr->value.function.actual->expr;
874 default:
875 return NULL;
880 /* Return true if the result of reference REF can only be constructed
881 using a temporary array. */
883 bool
884 gfc_ref_needs_temporary_p (gfc_ref *ref)
886 int n;
887 bool subarray_p;
889 subarray_p = false;
890 for (; ref; ref = ref->next)
891 switch (ref->type)
893 case REF_ARRAY:
894 /* Vector dimensions are generally not monotonic and must be
895 handled using a temporary. */
896 if (ref->u.ar.type == AR_SECTION)
897 for (n = 0; n < ref->u.ar.dimen; n++)
898 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR)
899 return true;
901 subarray_p = true;
902 break;
904 case REF_SUBSTRING:
905 /* Within an array reference, character substrings generally
906 need a temporary. Character array strides are expressed as
907 multiples of the element size (consistent with other array
908 types), not in characters. */
909 return subarray_p;
911 case REF_COMPONENT:
912 break;
915 return false;
919 static int
920 gfc_is_data_pointer (gfc_expr *e)
922 gfc_ref *ref;
924 if (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION)
925 return 0;
927 /* No subreference if it is a function */
928 gcc_assert (e->expr_type == EXPR_VARIABLE || !e->ref);
930 if (e->symtree->n.sym->attr.pointer)
931 return 1;
933 for (ref = e->ref; ref; ref = ref->next)
934 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
935 return 1;
937 return 0;
941 /* Return true if array variable VAR could be passed to the same function
942 as argument EXPR without interfering with EXPR. INTENT is the intent
943 of VAR.
945 This is considerably less conservative than other dependencies
946 because many function arguments will already be copied into a
947 temporary. */
949 static int
950 gfc_check_argument_var_dependency (gfc_expr *var, sym_intent intent,
951 gfc_expr *expr, gfc_dep_check elemental)
953 gfc_expr *arg;
955 gcc_assert (var->expr_type == EXPR_VARIABLE);
956 gcc_assert (var->rank > 0);
958 switch (expr->expr_type)
960 case EXPR_VARIABLE:
961 /* In case of elemental subroutines, there is no dependency
962 between two same-range array references. */
963 if (gfc_ref_needs_temporary_p (expr->ref)
964 || gfc_check_dependency (var, expr, elemental == NOT_ELEMENTAL))
966 if (elemental == ELEM_DONT_CHECK_VARIABLE)
968 /* Too many false positive with pointers. */
969 if (!gfc_is_data_pointer (var) && !gfc_is_data_pointer (expr))
971 /* Elemental procedures forbid unspecified intents,
972 and we don't check dependencies for INTENT_IN args. */
973 gcc_assert (intent == INTENT_OUT || intent == INTENT_INOUT);
975 /* We are told not to check dependencies.
976 We do it, however, and issue a warning in case we find one.
977 If a dependency is found in the case
978 elemental == ELEM_CHECK_VARIABLE, we will generate
979 a temporary, so we don't need to bother the user. */
980 gfc_warning ("INTENT(%s) actual argument at %L might "
981 "interfere with actual argument at %L.",
982 intent == INTENT_OUT ? "OUT" : "INOUT",
983 &var->where, &expr->where);
985 return 0;
987 else
988 return 1;
990 return 0;
992 case EXPR_ARRAY:
993 /* the scalarizer always generates a temporary for array constructors,
994 so there is no dependency. */
995 return 0;
997 case EXPR_FUNCTION:
998 if (intent != INTENT_IN)
1000 arg = gfc_get_noncopying_intrinsic_argument (expr);
1001 if (arg != NULL)
1002 return gfc_check_argument_var_dependency (var, intent, arg,
1003 NOT_ELEMENTAL);
1006 if (elemental != NOT_ELEMENTAL)
1008 if ((expr->value.function.esym
1009 && expr->value.function.esym->attr.elemental)
1010 || (expr->value.function.isym
1011 && expr->value.function.isym->elemental))
1012 return gfc_check_fncall_dependency (var, intent, NULL,
1013 expr->value.function.actual,
1014 ELEM_CHECK_VARIABLE);
1016 if (gfc_inline_intrinsic_function_p (expr))
1018 /* The TRANSPOSE case should have been caught in the
1019 noncopying intrinsic case above. */
1020 gcc_assert (expr->value.function.isym->id != GFC_ISYM_TRANSPOSE);
1022 return gfc_check_fncall_dependency (var, intent, NULL,
1023 expr->value.function.actual,
1024 ELEM_CHECK_VARIABLE);
1027 return 0;
1029 case EXPR_OP:
1030 /* In case of non-elemental procedures, there is no need to catch
1031 dependencies, as we will make a temporary anyway. */
1032 if (elemental)
1034 /* If the actual arg EXPR is an expression, we need to catch
1035 a dependency between variables in EXPR and VAR,
1036 an intent((IN)OUT) variable. */
1037 if (expr->value.op.op1
1038 && gfc_check_argument_var_dependency (var, intent,
1039 expr->value.op.op1,
1040 ELEM_CHECK_VARIABLE))
1041 return 1;
1042 else if (expr->value.op.op2
1043 && gfc_check_argument_var_dependency (var, intent,
1044 expr->value.op.op2,
1045 ELEM_CHECK_VARIABLE))
1046 return 1;
1048 return 0;
1050 default:
1051 return 0;
1056 /* Like gfc_check_argument_var_dependency, but extended to any
1057 array expression OTHER, not just variables. */
1059 static int
1060 gfc_check_argument_dependency (gfc_expr *other, sym_intent intent,
1061 gfc_expr *expr, gfc_dep_check elemental)
1063 switch (other->expr_type)
1065 case EXPR_VARIABLE:
1066 return gfc_check_argument_var_dependency (other, intent, expr, elemental);
1068 case EXPR_FUNCTION:
1069 other = gfc_get_noncopying_intrinsic_argument (other);
1070 if (other != NULL)
1071 return gfc_check_argument_dependency (other, INTENT_IN, expr,
1072 NOT_ELEMENTAL);
1074 return 0;
1076 default:
1077 return 0;
1082 /* Like gfc_check_argument_dependency, but check all the arguments in ACTUAL.
1083 FNSYM is the function being called, or NULL if not known. */
1086 gfc_check_fncall_dependency (gfc_expr *other, sym_intent intent,
1087 gfc_symbol *fnsym, gfc_actual_arglist *actual,
1088 gfc_dep_check elemental)
1090 gfc_formal_arglist *formal;
1091 gfc_expr *expr;
1093 formal = fnsym ? gfc_sym_get_dummy_args (fnsym) : NULL;
1094 for (; actual; actual = actual->next, formal = formal ? formal->next : NULL)
1096 expr = actual->expr;
1098 /* Skip args which are not present. */
1099 if (!expr)
1100 continue;
1102 /* Skip other itself. */
1103 if (expr == other)
1104 continue;
1106 /* Skip intent(in) arguments if OTHER itself is intent(in). */
1107 if (formal && intent == INTENT_IN
1108 && formal->sym->attr.intent == INTENT_IN)
1109 continue;
1111 if (gfc_check_argument_dependency (other, intent, expr, elemental))
1112 return 1;
1115 return 0;
1119 /* Return 1 if e1 and e2 are equivalenced arrays, either
1120 directly or indirectly; i.e., equivalence (a,b) for a and b
1121 or equivalence (a,c),(b,c). This function uses the equiv_
1122 lists, generated in trans-common(add_equivalences), that are
1123 guaranteed to pick up indirect equivalences. We explicitly
1124 check for overlap using the offset and length of the equivalence.
1125 This function is symmetric.
1126 TODO: This function only checks whether the full top-level
1127 symbols overlap. An improved implementation could inspect
1128 e1->ref and e2->ref to determine whether the actually accessed
1129 portions of these variables/arrays potentially overlap. */
1132 gfc_are_equivalenced_arrays (gfc_expr *e1, gfc_expr *e2)
1134 gfc_equiv_list *l;
1135 gfc_equiv_info *s, *fl1, *fl2;
1137 gcc_assert (e1->expr_type == EXPR_VARIABLE
1138 && e2->expr_type == EXPR_VARIABLE);
1140 if (!e1->symtree->n.sym->attr.in_equivalence
1141 || !e2->symtree->n.sym->attr.in_equivalence|| !e1->rank || !e2->rank)
1142 return 0;
1144 if (e1->symtree->n.sym->ns
1145 && e1->symtree->n.sym->ns != gfc_current_ns)
1146 l = e1->symtree->n.sym->ns->equiv_lists;
1147 else
1148 l = gfc_current_ns->equiv_lists;
1150 /* Go through the equiv_lists and return 1 if the variables
1151 e1 and e2 are members of the same group and satisfy the
1152 requirement on their relative offsets. */
1153 for (; l; l = l->next)
1155 fl1 = NULL;
1156 fl2 = NULL;
1157 for (s = l->equiv; s; s = s->next)
1159 if (s->sym == e1->symtree->n.sym)
1161 fl1 = s;
1162 if (fl2)
1163 break;
1165 if (s->sym == e2->symtree->n.sym)
1167 fl2 = s;
1168 if (fl1)
1169 break;
1173 if (s)
1175 /* Can these lengths be zero? */
1176 if (fl1->length <= 0 || fl2->length <= 0)
1177 return 1;
1178 /* These can't overlap if [f11,fl1+length] is before
1179 [fl2,fl2+length], or [fl2,fl2+length] is before
1180 [fl1,fl1+length], otherwise they do overlap. */
1181 if (fl1->offset + fl1->length > fl2->offset
1182 && fl2->offset + fl2->length > fl1->offset)
1183 return 1;
1186 return 0;
1190 /* Return true if there is no possibility of aliasing because of a type
1191 mismatch between all the possible pointer references and the
1192 potential target. Note that this function is asymmetric in the
1193 arguments and so must be called twice with the arguments exchanged. */
1195 static bool
1196 check_data_pointer_types (gfc_expr *expr1, gfc_expr *expr2)
1198 gfc_component *cm1;
1199 gfc_symbol *sym1;
1200 gfc_symbol *sym2;
1201 gfc_ref *ref1;
1202 bool seen_component_ref;
1204 if (expr1->expr_type != EXPR_VARIABLE
1205 || expr2->expr_type != EXPR_VARIABLE)
1206 return false;
1208 sym1 = expr1->symtree->n.sym;
1209 sym2 = expr2->symtree->n.sym;
1211 /* Keep it simple for now. */
1212 if (sym1->ts.type == BT_DERIVED && sym2->ts.type == BT_DERIVED)
1213 return false;
1215 if (sym1->attr.pointer)
1217 if (gfc_compare_types (&sym1->ts, &sym2->ts))
1218 return false;
1221 /* This is a conservative check on the components of the derived type
1222 if no component references have been seen. Since we will not dig
1223 into the components of derived type components, we play it safe by
1224 returning false. First we check the reference chain and then, if
1225 no component references have been seen, the components. */
1226 seen_component_ref = false;
1227 if (sym1->ts.type == BT_DERIVED)
1229 for (ref1 = expr1->ref; ref1; ref1 = ref1->next)
1231 if (ref1->type != REF_COMPONENT)
1232 continue;
1234 if (ref1->u.c.component->ts.type == BT_DERIVED)
1235 return false;
1237 if ((sym2->attr.pointer || ref1->u.c.component->attr.pointer)
1238 && gfc_compare_types (&ref1->u.c.component->ts, &sym2->ts))
1239 return false;
1241 seen_component_ref = true;
1245 if (sym1->ts.type == BT_DERIVED && !seen_component_ref)
1247 for (cm1 = sym1->ts.u.derived->components; cm1; cm1 = cm1->next)
1249 if (cm1->ts.type == BT_DERIVED)
1250 return false;
1252 if ((sym2->attr.pointer || cm1->attr.pointer)
1253 && gfc_compare_types (&cm1->ts, &sym2->ts))
1254 return false;
1258 return true;
1262 /* Return true if the statement body redefines the condition. Returns
1263 true if expr2 depends on expr1. expr1 should be a single term
1264 suitable for the lhs of an assignment. The IDENTICAL flag indicates
1265 whether array references to the same symbol with identical range
1266 references count as a dependency or not. Used for forall and where
1267 statements. Also used with functions returning arrays without a
1268 temporary. */
1271 gfc_check_dependency (gfc_expr *expr1, gfc_expr *expr2, bool identical)
1273 gfc_actual_arglist *actual;
1274 gfc_constructor *c;
1275 int n;
1277 gcc_assert (expr1->expr_type == EXPR_VARIABLE);
1279 switch (expr2->expr_type)
1281 case EXPR_OP:
1282 n = gfc_check_dependency (expr1, expr2->value.op.op1, identical);
1283 if (n)
1284 return n;
1285 if (expr2->value.op.op2)
1286 return gfc_check_dependency (expr1, expr2->value.op.op2, identical);
1287 return 0;
1289 case EXPR_VARIABLE:
1290 /* The interesting cases are when the symbols don't match. */
1291 if (expr1->symtree->n.sym != expr2->symtree->n.sym)
1293 gfc_typespec *ts1 = &expr1->symtree->n.sym->ts;
1294 gfc_typespec *ts2 = &expr2->symtree->n.sym->ts;
1296 /* Return 1 if expr1 and expr2 are equivalenced arrays. */
1297 if (gfc_are_equivalenced_arrays (expr1, expr2))
1298 return 1;
1300 /* Symbols can only alias if they have the same type. */
1301 if (ts1->type != BT_UNKNOWN && ts2->type != BT_UNKNOWN
1302 && ts1->type != BT_DERIVED && ts2->type != BT_DERIVED)
1304 if (ts1->type != ts2->type || ts1->kind != ts2->kind)
1305 return 0;
1308 /* If either variable is a pointer, assume the worst. */
1309 /* TODO: -fassume-no-pointer-aliasing */
1310 if (gfc_is_data_pointer (expr1) || gfc_is_data_pointer (expr2))
1312 if (check_data_pointer_types (expr1, expr2)
1313 && check_data_pointer_types (expr2, expr1))
1314 return 0;
1316 return 1;
1318 else
1320 gfc_symbol *sym1 = expr1->symtree->n.sym;
1321 gfc_symbol *sym2 = expr2->symtree->n.sym;
1322 if (sym1->attr.target && sym2->attr.target
1323 && ((sym1->attr.dummy && !sym1->attr.contiguous
1324 && (!sym1->attr.dimension
1325 || sym2->as->type == AS_ASSUMED_SHAPE))
1326 || (sym2->attr.dummy && !sym2->attr.contiguous
1327 && (!sym2->attr.dimension
1328 || sym2->as->type == AS_ASSUMED_SHAPE))))
1329 return 1;
1332 /* Otherwise distinct symbols have no dependencies. */
1333 return 0;
1336 if (identical)
1337 return 1;
1339 /* Identical and disjoint ranges return 0,
1340 overlapping ranges return 1. */
1341 if (expr1->ref && expr2->ref)
1342 return gfc_dep_resolver (expr1->ref, expr2->ref, NULL);
1344 return 1;
1346 case EXPR_FUNCTION:
1347 if (gfc_get_noncopying_intrinsic_argument (expr2) != NULL)
1348 identical = 1;
1350 /* Remember possible differences between elemental and
1351 transformational functions. All functions inside a FORALL
1352 will be pure. */
1353 for (actual = expr2->value.function.actual;
1354 actual; actual = actual->next)
1356 if (!actual->expr)
1357 continue;
1358 n = gfc_check_dependency (expr1, actual->expr, identical);
1359 if (n)
1360 return n;
1362 return 0;
1364 case EXPR_CONSTANT:
1365 case EXPR_NULL:
1366 return 0;
1368 case EXPR_ARRAY:
1369 /* Loop through the array constructor's elements. */
1370 for (c = gfc_constructor_first (expr2->value.constructor);
1371 c; c = gfc_constructor_next (c))
1373 /* If this is an iterator, assume the worst. */
1374 if (c->iterator)
1375 return 1;
1376 /* Avoid recursion in the common case. */
1377 if (c->expr->expr_type == EXPR_CONSTANT)
1378 continue;
1379 if (gfc_check_dependency (expr1, c->expr, 1))
1380 return 1;
1382 return 0;
1384 default:
1385 return 1;
1390 /* Determines overlapping for two array sections. */
1392 static gfc_dependency
1393 check_section_vs_section (gfc_array_ref *l_ar, gfc_array_ref *r_ar, int n)
1395 gfc_expr *l_start;
1396 gfc_expr *l_end;
1397 gfc_expr *l_stride;
1398 gfc_expr *l_lower;
1399 gfc_expr *l_upper;
1400 int l_dir;
1402 gfc_expr *r_start;
1403 gfc_expr *r_end;
1404 gfc_expr *r_stride;
1405 gfc_expr *r_lower;
1406 gfc_expr *r_upper;
1407 gfc_expr *one_expr;
1408 int r_dir;
1409 int stride_comparison;
1410 int start_comparison;
1411 mpz_t tmp;
1413 /* If they are the same range, return without more ado. */
1414 if (is_same_range (l_ar, r_ar, n))
1415 return GFC_DEP_EQUAL;
1417 l_start = l_ar->start[n];
1418 l_end = l_ar->end[n];
1419 l_stride = l_ar->stride[n];
1421 r_start = r_ar->start[n];
1422 r_end = r_ar->end[n];
1423 r_stride = r_ar->stride[n];
1425 /* If l_start is NULL take it from array specifier. */
1426 if (NULL == l_start && IS_ARRAY_EXPLICIT (l_ar->as))
1427 l_start = l_ar->as->lower[n];
1428 /* If l_end is NULL take it from array specifier. */
1429 if (NULL == l_end && IS_ARRAY_EXPLICIT (l_ar->as))
1430 l_end = l_ar->as->upper[n];
1432 /* If r_start is NULL take it from array specifier. */
1433 if (NULL == r_start && IS_ARRAY_EXPLICIT (r_ar->as))
1434 r_start = r_ar->as->lower[n];
1435 /* If r_end is NULL take it from array specifier. */
1436 if (NULL == r_end && IS_ARRAY_EXPLICIT (r_ar->as))
1437 r_end = r_ar->as->upper[n];
1439 /* Determine whether the l_stride is positive or negative. */
1440 if (!l_stride)
1441 l_dir = 1;
1442 else if (l_stride->expr_type == EXPR_CONSTANT
1443 && l_stride->ts.type == BT_INTEGER)
1444 l_dir = mpz_sgn (l_stride->value.integer);
1445 else if (l_start && l_end)
1446 l_dir = gfc_dep_compare_expr (l_end, l_start);
1447 else
1448 l_dir = -2;
1450 /* Determine whether the r_stride is positive or negative. */
1451 if (!r_stride)
1452 r_dir = 1;
1453 else if (r_stride->expr_type == EXPR_CONSTANT
1454 && r_stride->ts.type == BT_INTEGER)
1455 r_dir = mpz_sgn (r_stride->value.integer);
1456 else if (r_start && r_end)
1457 r_dir = gfc_dep_compare_expr (r_end, r_start);
1458 else
1459 r_dir = -2;
1461 /* The strides should never be zero. */
1462 if (l_dir == 0 || r_dir == 0)
1463 return GFC_DEP_OVERLAP;
1465 /* Determine the relationship between the strides. Set stride_comparison to
1466 -2 if the dependency cannot be determined
1467 -1 if l_stride < r_stride
1468 0 if l_stride == r_stride
1469 1 if l_stride > r_stride
1470 as determined by gfc_dep_compare_expr. */
1472 one_expr = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
1474 stride_comparison = gfc_dep_compare_expr (l_stride ? l_stride : one_expr,
1475 r_stride ? r_stride : one_expr);
1477 if (l_start && r_start)
1478 start_comparison = gfc_dep_compare_expr (l_start, r_start);
1479 else
1480 start_comparison = -2;
1482 gfc_free_expr (one_expr);
1484 /* Determine LHS upper and lower bounds. */
1485 if (l_dir == 1)
1487 l_lower = l_start;
1488 l_upper = l_end;
1490 else if (l_dir == -1)
1492 l_lower = l_end;
1493 l_upper = l_start;
1495 else
1497 l_lower = NULL;
1498 l_upper = NULL;
1501 /* Determine RHS upper and lower bounds. */
1502 if (r_dir == 1)
1504 r_lower = r_start;
1505 r_upper = r_end;
1507 else if (r_dir == -1)
1509 r_lower = r_end;
1510 r_upper = r_start;
1512 else
1514 r_lower = NULL;
1515 r_upper = NULL;
1518 /* Check whether the ranges are disjoint. */
1519 if (l_upper && r_lower && gfc_dep_compare_expr (l_upper, r_lower) == -1)
1520 return GFC_DEP_NODEP;
1521 if (r_upper && l_lower && gfc_dep_compare_expr (r_upper, l_lower) == -1)
1522 return GFC_DEP_NODEP;
1524 /* Handle cases like x:y:1 vs. x:z:-1 as GFC_DEP_EQUAL. */
1525 if (l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == 0)
1527 if (l_dir == 1 && r_dir == -1)
1528 return GFC_DEP_EQUAL;
1529 if (l_dir == -1 && r_dir == 1)
1530 return GFC_DEP_EQUAL;
1533 /* Handle cases like x:y:1 vs. z:y:-1 as GFC_DEP_EQUAL. */
1534 if (l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == 0)
1536 if (l_dir == 1 && r_dir == -1)
1537 return GFC_DEP_EQUAL;
1538 if (l_dir == -1 && r_dir == 1)
1539 return GFC_DEP_EQUAL;
1542 /* Handle cases like x:y:2 vs. x+1:z:4 as GFC_DEP_NODEP.
1543 There is no dependency if the remainder of
1544 (l_start - r_start) / gcd(l_stride, r_stride) is
1545 nonzero.
1546 TODO:
1547 - Cases like a(1:4:2) = a(2:3) are still not handled.
1550 #define IS_CONSTANT_INTEGER(a) ((a) && ((a)->expr_type == EXPR_CONSTANT) \
1551 && (a)->ts.type == BT_INTEGER)
1553 if (IS_CONSTANT_INTEGER (l_stride) && IS_CONSTANT_INTEGER (r_stride)
1554 && gfc_dep_difference (l_start, r_start, &tmp))
1556 mpz_t gcd;
1557 int result;
1559 mpz_init (gcd);
1560 mpz_gcd (gcd, l_stride->value.integer, r_stride->value.integer);
1562 mpz_fdiv_r (tmp, tmp, gcd);
1563 result = mpz_cmp_si (tmp, 0L);
1565 mpz_clear (gcd);
1566 mpz_clear (tmp);
1568 if (result != 0)
1569 return GFC_DEP_NODEP;
1572 #undef IS_CONSTANT_INTEGER
1574 /* Check for forward dependencies x:y vs. x+1:z and x:y:z vs. x:y:z+1. */
1576 if (l_dir == 1 && r_dir == 1 &&
1577 (start_comparison == 0 || start_comparison == -1)
1578 && (stride_comparison == 0 || stride_comparison == -1))
1579 return GFC_DEP_FORWARD;
1581 /* Check for forward dependencies x:y:-1 vs. x-1:z:-1 and
1582 x:y:-1 vs. x:y:-2. */
1583 if (l_dir == -1 && r_dir == -1 &&
1584 (start_comparison == 0 || start_comparison == 1)
1585 && (stride_comparison == 0 || stride_comparison == 1))
1586 return GFC_DEP_FORWARD;
1588 if (stride_comparison == 0 || stride_comparison == -1)
1590 if (l_start && IS_ARRAY_EXPLICIT (l_ar->as))
1593 /* Check for a(low:y:s) vs. a(z:x:s) or
1594 a(low:y:s) vs. a(z:x:s+1) where a has a lower bound
1595 of low, which is always at least a forward dependence. */
1597 if (r_dir == 1
1598 && gfc_dep_compare_expr (l_start, l_ar->as->lower[n]) == 0)
1599 return GFC_DEP_FORWARD;
1603 if (stride_comparison == 0 || stride_comparison == 1)
1605 if (l_start && IS_ARRAY_EXPLICIT (l_ar->as))
1608 /* Check for a(high:y:-s) vs. a(z:x:-s) or
1609 a(high:y:-s vs. a(z:x:-s-1) where a has a higher bound
1610 of high, which is always at least a forward dependence. */
1612 if (r_dir == -1
1613 && gfc_dep_compare_expr (l_start, l_ar->as->upper[n]) == 0)
1614 return GFC_DEP_FORWARD;
1619 if (stride_comparison == 0)
1621 /* From here, check for backwards dependencies. */
1622 /* x+1:y vs. x:z. */
1623 if (l_dir == 1 && r_dir == 1 && start_comparison == 1)
1624 return GFC_DEP_BACKWARD;
1626 /* x-1:y:-1 vs. x:z:-1. */
1627 if (l_dir == -1 && r_dir == -1 && start_comparison == -1)
1628 return GFC_DEP_BACKWARD;
1631 return GFC_DEP_OVERLAP;
1635 /* Determines overlapping for a single element and a section. */
1637 static gfc_dependency
1638 gfc_check_element_vs_section( gfc_ref *lref, gfc_ref *rref, int n)
1640 gfc_array_ref *ref;
1641 gfc_expr *elem;
1642 gfc_expr *start;
1643 gfc_expr *end;
1644 gfc_expr *stride;
1645 int s;
1647 elem = lref->u.ar.start[n];
1648 if (!elem)
1649 return GFC_DEP_OVERLAP;
1651 ref = &rref->u.ar;
1652 start = ref->start[n] ;
1653 end = ref->end[n] ;
1654 stride = ref->stride[n];
1656 if (!start && IS_ARRAY_EXPLICIT (ref->as))
1657 start = ref->as->lower[n];
1658 if (!end && IS_ARRAY_EXPLICIT (ref->as))
1659 end = ref->as->upper[n];
1661 /* Determine whether the stride is positive or negative. */
1662 if (!stride)
1663 s = 1;
1664 else if (stride->expr_type == EXPR_CONSTANT
1665 && stride->ts.type == BT_INTEGER)
1666 s = mpz_sgn (stride->value.integer);
1667 else
1668 s = -2;
1670 /* Stride should never be zero. */
1671 if (s == 0)
1672 return GFC_DEP_OVERLAP;
1674 /* Positive strides. */
1675 if (s == 1)
1677 /* Check for elem < lower. */
1678 if (start && gfc_dep_compare_expr (elem, start) == -1)
1679 return GFC_DEP_NODEP;
1680 /* Check for elem > upper. */
1681 if (end && gfc_dep_compare_expr (elem, end) == 1)
1682 return GFC_DEP_NODEP;
1684 if (start && end)
1686 s = gfc_dep_compare_expr (start, end);
1687 /* Check for an empty range. */
1688 if (s == 1)
1689 return GFC_DEP_NODEP;
1690 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1691 return GFC_DEP_EQUAL;
1694 /* Negative strides. */
1695 else if (s == -1)
1697 /* Check for elem > upper. */
1698 if (end && gfc_dep_compare_expr (elem, start) == 1)
1699 return GFC_DEP_NODEP;
1700 /* Check for elem < lower. */
1701 if (start && gfc_dep_compare_expr (elem, end) == -1)
1702 return GFC_DEP_NODEP;
1704 if (start && end)
1706 s = gfc_dep_compare_expr (start, end);
1707 /* Check for an empty range. */
1708 if (s == -1)
1709 return GFC_DEP_NODEP;
1710 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1711 return GFC_DEP_EQUAL;
1714 /* Unknown strides. */
1715 else
1717 if (!start || !end)
1718 return GFC_DEP_OVERLAP;
1719 s = gfc_dep_compare_expr (start, end);
1720 if (s <= -2)
1721 return GFC_DEP_OVERLAP;
1722 /* Assume positive stride. */
1723 if (s == -1)
1725 /* Check for elem < lower. */
1726 if (gfc_dep_compare_expr (elem, start) == -1)
1727 return GFC_DEP_NODEP;
1728 /* Check for elem > upper. */
1729 if (gfc_dep_compare_expr (elem, end) == 1)
1730 return GFC_DEP_NODEP;
1732 /* Assume negative stride. */
1733 else if (s == 1)
1735 /* Check for elem > upper. */
1736 if (gfc_dep_compare_expr (elem, start) == 1)
1737 return GFC_DEP_NODEP;
1738 /* Check for elem < lower. */
1739 if (gfc_dep_compare_expr (elem, end) == -1)
1740 return GFC_DEP_NODEP;
1742 /* Equal bounds. */
1743 else if (s == 0)
1745 s = gfc_dep_compare_expr (elem, start);
1746 if (s == 0)
1747 return GFC_DEP_EQUAL;
1748 if (s == 1 || s == -1)
1749 return GFC_DEP_NODEP;
1753 return GFC_DEP_OVERLAP;
1757 /* Traverse expr, checking all EXPR_VARIABLE symbols for their
1758 forall_index attribute. Return true if any variable may be
1759 being used as a FORALL index. Its safe to pessimistically
1760 return true, and assume a dependency. */
1762 static bool
1763 contains_forall_index_p (gfc_expr *expr)
1765 gfc_actual_arglist *arg;
1766 gfc_constructor *c;
1767 gfc_ref *ref;
1768 int i;
1770 if (!expr)
1771 return false;
1773 switch (expr->expr_type)
1775 case EXPR_VARIABLE:
1776 if (expr->symtree->n.sym->forall_index)
1777 return true;
1778 break;
1780 case EXPR_OP:
1781 if (contains_forall_index_p (expr->value.op.op1)
1782 || contains_forall_index_p (expr->value.op.op2))
1783 return true;
1784 break;
1786 case EXPR_FUNCTION:
1787 for (arg = expr->value.function.actual; arg; arg = arg->next)
1788 if (contains_forall_index_p (arg->expr))
1789 return true;
1790 break;
1792 case EXPR_CONSTANT:
1793 case EXPR_NULL:
1794 case EXPR_SUBSTRING:
1795 break;
1797 case EXPR_STRUCTURE:
1798 case EXPR_ARRAY:
1799 for (c = gfc_constructor_first (expr->value.constructor);
1800 c; gfc_constructor_next (c))
1801 if (contains_forall_index_p (c->expr))
1802 return true;
1803 break;
1805 default:
1806 gcc_unreachable ();
1809 for (ref = expr->ref; ref; ref = ref->next)
1810 switch (ref->type)
1812 case REF_ARRAY:
1813 for (i = 0; i < ref->u.ar.dimen; i++)
1814 if (contains_forall_index_p (ref->u.ar.start[i])
1815 || contains_forall_index_p (ref->u.ar.end[i])
1816 || contains_forall_index_p (ref->u.ar.stride[i]))
1817 return true;
1818 break;
1820 case REF_COMPONENT:
1821 break;
1823 case REF_SUBSTRING:
1824 if (contains_forall_index_p (ref->u.ss.start)
1825 || contains_forall_index_p (ref->u.ss.end))
1826 return true;
1827 break;
1829 default:
1830 gcc_unreachable ();
1833 return false;
1836 /* Determines overlapping for two single element array references. */
1838 static gfc_dependency
1839 gfc_check_element_vs_element (gfc_ref *lref, gfc_ref *rref, int n)
1841 gfc_array_ref l_ar;
1842 gfc_array_ref r_ar;
1843 gfc_expr *l_start;
1844 gfc_expr *r_start;
1845 int i;
1847 l_ar = lref->u.ar;
1848 r_ar = rref->u.ar;
1849 l_start = l_ar.start[n] ;
1850 r_start = r_ar.start[n] ;
1851 i = gfc_dep_compare_expr (r_start, l_start);
1852 if (i == 0)
1853 return GFC_DEP_EQUAL;
1855 /* Treat two scalar variables as potentially equal. This allows
1856 us to prove that a(i,:) and a(j,:) have no dependency. See
1857 Gerald Roth, "Evaluation of Array Syntax Dependence Analysis",
1858 Proceedings of the International Conference on Parallel and
1859 Distributed Processing Techniques and Applications (PDPTA2001),
1860 Las Vegas, Nevada, June 2001. */
1861 /* However, we need to be careful when either scalar expression
1862 contains a FORALL index, as these can potentially change value
1863 during the scalarization/traversal of this array reference. */
1864 if (contains_forall_index_p (r_start) || contains_forall_index_p (l_start))
1865 return GFC_DEP_OVERLAP;
1867 if (i > -2)
1868 return GFC_DEP_NODEP;
1869 return GFC_DEP_EQUAL;
1873 /* Determine if an array ref, usually an array section specifies the
1874 entire array. In addition, if the second, pointer argument is
1875 provided, the function will return true if the reference is
1876 contiguous; eg. (:, 1) gives true but (1,:) gives false. */
1878 bool
1879 gfc_full_array_ref_p (gfc_ref *ref, bool *contiguous)
1881 int i;
1882 int n;
1883 bool lbound_OK = true;
1884 bool ubound_OK = true;
1886 if (contiguous)
1887 *contiguous = false;
1889 if (ref->type != REF_ARRAY)
1890 return false;
1892 if (ref->u.ar.type == AR_FULL)
1894 if (contiguous)
1895 *contiguous = true;
1896 return true;
1899 if (ref->u.ar.type != AR_SECTION)
1900 return false;
1901 if (ref->next)
1902 return false;
1904 for (i = 0; i < ref->u.ar.dimen; i++)
1906 /* If we have a single element in the reference, for the reference
1907 to be full, we need to ascertain that the array has a single
1908 element in this dimension and that we actually reference the
1909 correct element. */
1910 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
1912 /* This is unconditionally a contiguous reference if all the
1913 remaining dimensions are elements. */
1914 if (contiguous)
1916 *contiguous = true;
1917 for (n = i + 1; n < ref->u.ar.dimen; n++)
1918 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
1919 *contiguous = false;
1922 if (!ref->u.ar.as
1923 || !ref->u.ar.as->lower[i]
1924 || !ref->u.ar.as->upper[i]
1925 || gfc_dep_compare_expr (ref->u.ar.as->lower[i],
1926 ref->u.ar.as->upper[i])
1927 || !ref->u.ar.start[i]
1928 || gfc_dep_compare_expr (ref->u.ar.start[i],
1929 ref->u.ar.as->lower[i]))
1930 return false;
1931 else
1932 continue;
1935 /* Check the lower bound. */
1936 if (ref->u.ar.start[i]
1937 && (!ref->u.ar.as
1938 || !ref->u.ar.as->lower[i]
1939 || gfc_dep_compare_expr (ref->u.ar.start[i],
1940 ref->u.ar.as->lower[i])))
1941 lbound_OK = false;
1942 /* Check the upper bound. */
1943 if (ref->u.ar.end[i]
1944 && (!ref->u.ar.as
1945 || !ref->u.ar.as->upper[i]
1946 || gfc_dep_compare_expr (ref->u.ar.end[i],
1947 ref->u.ar.as->upper[i])))
1948 ubound_OK = false;
1949 /* Check the stride. */
1950 if (ref->u.ar.stride[i]
1951 && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
1952 return false;
1954 /* This is unconditionally a contiguous reference as long as all
1955 the subsequent dimensions are elements. */
1956 if (contiguous)
1958 *contiguous = true;
1959 for (n = i + 1; n < ref->u.ar.dimen; n++)
1960 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
1961 *contiguous = false;
1964 if (!lbound_OK || !ubound_OK)
1965 return false;
1967 return true;
1971 /* Determine if a full array is the same as an array section with one
1972 variable limit. For this to be so, the strides must both be unity
1973 and one of either start == lower or end == upper must be true. */
1975 static bool
1976 ref_same_as_full_array (gfc_ref *full_ref, gfc_ref *ref)
1978 int i;
1979 bool upper_or_lower;
1981 if (full_ref->type != REF_ARRAY)
1982 return false;
1983 if (full_ref->u.ar.type != AR_FULL)
1984 return false;
1985 if (ref->type != REF_ARRAY)
1986 return false;
1987 if (ref->u.ar.type != AR_SECTION)
1988 return false;
1990 for (i = 0; i < ref->u.ar.dimen; i++)
1992 /* If we have a single element in the reference, we need to check
1993 that the array has a single element and that we actually reference
1994 the correct element. */
1995 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
1997 if (!full_ref->u.ar.as
1998 || !full_ref->u.ar.as->lower[i]
1999 || !full_ref->u.ar.as->upper[i]
2000 || gfc_dep_compare_expr (full_ref->u.ar.as->lower[i],
2001 full_ref->u.ar.as->upper[i])
2002 || !ref->u.ar.start[i]
2003 || gfc_dep_compare_expr (ref->u.ar.start[i],
2004 full_ref->u.ar.as->lower[i]))
2005 return false;
2008 /* Check the strides. */
2009 if (full_ref->u.ar.stride[i] && !gfc_expr_is_one (full_ref->u.ar.stride[i], 0))
2010 return false;
2011 if (ref->u.ar.stride[i] && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
2012 return false;
2014 upper_or_lower = false;
2015 /* Check the lower bound. */
2016 if (ref->u.ar.start[i]
2017 && (ref->u.ar.as
2018 && full_ref->u.ar.as->lower[i]
2019 && gfc_dep_compare_expr (ref->u.ar.start[i],
2020 full_ref->u.ar.as->lower[i]) == 0))
2021 upper_or_lower = true;
2022 /* Check the upper bound. */
2023 if (ref->u.ar.end[i]
2024 && (ref->u.ar.as
2025 && full_ref->u.ar.as->upper[i]
2026 && gfc_dep_compare_expr (ref->u.ar.end[i],
2027 full_ref->u.ar.as->upper[i]) == 0))
2028 upper_or_lower = true;
2029 if (!upper_or_lower)
2030 return false;
2032 return true;
2036 /* Finds if two array references are overlapping or not.
2037 Return value
2038 2 : array references are overlapping but reversal of one or
2039 more dimensions will clear the dependency.
2040 1 : array references are overlapping.
2041 0 : array references are identical or not overlapping. */
2044 gfc_dep_resolver (gfc_ref *lref, gfc_ref *rref, gfc_reverse *reverse)
2046 int n;
2047 gfc_dependency fin_dep;
2048 gfc_dependency this_dep;
2050 this_dep = GFC_DEP_ERROR;
2051 fin_dep = GFC_DEP_ERROR;
2052 /* Dependencies due to pointers should already have been identified.
2053 We only need to check for overlapping array references. */
2055 while (lref && rref)
2057 /* We're resolving from the same base symbol, so both refs should be
2058 the same type. We traverse the reference chain until we find ranges
2059 that are not equal. */
2060 gcc_assert (lref->type == rref->type);
2061 switch (lref->type)
2063 case REF_COMPONENT:
2064 /* The two ranges can't overlap if they are from different
2065 components. */
2066 if (lref->u.c.component != rref->u.c.component)
2067 return 0;
2068 break;
2070 case REF_SUBSTRING:
2071 /* Substring overlaps are handled by the string assignment code
2072 if there is not an underlying dependency. */
2073 return (fin_dep == GFC_DEP_OVERLAP) ? 1 : 0;
2075 case REF_ARRAY:
2077 if (ref_same_as_full_array (lref, rref))
2078 return 0;
2080 if (ref_same_as_full_array (rref, lref))
2081 return 0;
2083 if (lref->u.ar.dimen != rref->u.ar.dimen)
2085 if (lref->u.ar.type == AR_FULL)
2086 fin_dep = gfc_full_array_ref_p (rref, NULL) ? GFC_DEP_EQUAL
2087 : GFC_DEP_OVERLAP;
2088 else if (rref->u.ar.type == AR_FULL)
2089 fin_dep = gfc_full_array_ref_p (lref, NULL) ? GFC_DEP_EQUAL
2090 : GFC_DEP_OVERLAP;
2091 else
2092 return 1;
2093 break;
2096 for (n=0; n < lref->u.ar.dimen; n++)
2098 /* Handle dependency when either of array reference is vector
2099 subscript. There is no dependency if the vector indices
2100 are equal or if indices are known to be different in a
2101 different dimension. */
2102 if (lref->u.ar.dimen_type[n] == DIMEN_VECTOR
2103 || rref->u.ar.dimen_type[n] == DIMEN_VECTOR)
2105 if (lref->u.ar.dimen_type[n] == DIMEN_VECTOR
2106 && rref->u.ar.dimen_type[n] == DIMEN_VECTOR
2107 && gfc_dep_compare_expr (lref->u.ar.start[n],
2108 rref->u.ar.start[n]) == 0)
2109 this_dep = GFC_DEP_EQUAL;
2110 else
2111 this_dep = GFC_DEP_OVERLAP;
2113 goto update_fin_dep;
2116 if (lref->u.ar.dimen_type[n] == DIMEN_RANGE
2117 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
2118 this_dep = check_section_vs_section (&lref->u.ar, &rref->u.ar, n);
2119 else if (lref->u.ar.dimen_type[n] == DIMEN_ELEMENT
2120 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
2121 this_dep = gfc_check_element_vs_section (lref, rref, n);
2122 else if (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
2123 && lref->u.ar.dimen_type[n] == DIMEN_RANGE)
2124 this_dep = gfc_check_element_vs_section (rref, lref, n);
2125 else
2127 gcc_assert (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
2128 && lref->u.ar.dimen_type[n] == DIMEN_ELEMENT);
2129 this_dep = gfc_check_element_vs_element (rref, lref, n);
2132 /* If any dimension doesn't overlap, we have no dependency. */
2133 if (this_dep == GFC_DEP_NODEP)
2134 return 0;
2136 /* Now deal with the loop reversal logic: This only works on
2137 ranges and is activated by setting
2138 reverse[n] == GFC_ENABLE_REVERSE
2139 The ability to reverse or not is set by previous conditions
2140 in this dimension. If reversal is not activated, the
2141 value GFC_DEP_BACKWARD is reset to GFC_DEP_OVERLAP. */
2142 if (rref->u.ar.dimen_type[n] == DIMEN_RANGE
2143 && lref->u.ar.dimen_type[n] == DIMEN_RANGE)
2145 /* Set reverse if backward dependence and not inhibited. */
2146 if (reverse && reverse[n] == GFC_ENABLE_REVERSE)
2147 reverse[n] = (this_dep == GFC_DEP_BACKWARD) ?
2148 GFC_REVERSE_SET : reverse[n];
2150 /* Set forward if forward dependence and not inhibited. */
2151 if (reverse && reverse[n] == GFC_ENABLE_REVERSE)
2152 reverse[n] = (this_dep == GFC_DEP_FORWARD) ?
2153 GFC_FORWARD_SET : reverse[n];
2155 /* Flag up overlap if dependence not compatible with
2156 the overall state of the expression. */
2157 if (reverse && reverse[n] == GFC_REVERSE_SET
2158 && this_dep == GFC_DEP_FORWARD)
2160 reverse[n] = GFC_INHIBIT_REVERSE;
2161 this_dep = GFC_DEP_OVERLAP;
2163 else if (reverse && reverse[n] == GFC_FORWARD_SET
2164 && this_dep == GFC_DEP_BACKWARD)
2166 reverse[n] = GFC_INHIBIT_REVERSE;
2167 this_dep = GFC_DEP_OVERLAP;
2170 /* If no intention of reversing or reversing is explicitly
2171 inhibited, convert backward dependence to overlap. */
2172 if ((reverse == NULL && this_dep == GFC_DEP_BACKWARD)
2173 || (reverse != NULL && reverse[n] == GFC_INHIBIT_REVERSE))
2174 this_dep = GFC_DEP_OVERLAP;
2177 /* Overlap codes are in order of priority. We only need to
2178 know the worst one.*/
2180 update_fin_dep:
2181 if (this_dep > fin_dep)
2182 fin_dep = this_dep;
2185 /* If this is an equal element, we have to keep going until we find
2186 the "real" array reference. */
2187 if (lref->u.ar.type == AR_ELEMENT
2188 && rref->u.ar.type == AR_ELEMENT
2189 && fin_dep == GFC_DEP_EQUAL)
2190 break;
2192 /* Exactly matching and forward overlapping ranges don't cause a
2193 dependency. */
2194 if (fin_dep < GFC_DEP_BACKWARD)
2195 return 0;
2197 /* Keep checking. We only have a dependency if
2198 subsequent references also overlap. */
2199 break;
2201 default:
2202 gcc_unreachable ();
2204 lref = lref->next;
2205 rref = rref->next;
2208 /* If we haven't seen any array refs then something went wrong. */
2209 gcc_assert (fin_dep != GFC_DEP_ERROR);
2211 /* Assume the worst if we nest to different depths. */
2212 if (lref || rref)
2213 return 1;
2215 return fin_dep == GFC_DEP_OVERLAP;