libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / fortran / dependency.cc
blob15edf1af9dfff15aed9f904205c801c73380dfab
1 /* Dependency analysis
2 Copyright (C) 2000-2024 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.cc -- 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"
33 #include "options.h"
35 /* static declarations */
36 /* Enums */
37 enum range {LHS, RHS, MID};
39 /* Dependency types. These must be in reverse order of priority. */
40 enum gfc_dependency
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. */
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 if (a1->dimen != a2->dimen)
106 gfc_internal_error ("identical_array_ref(): inconsistent dimensions");
108 for (i = 0; i < a1->dimen; i++)
110 if (gfc_dep_compare_expr (a1->start[i], a2->start[i]) != 0)
111 return false;
113 return true;
115 return false;
120 /* Return true for identical variables, checking for references if
121 necessary. Calls identical_array_ref for checking array sections. */
123 static bool
124 are_identical_variables (gfc_expr *e1, gfc_expr *e2)
126 gfc_ref *r1, *r2;
128 if (e1->symtree->n.sym->attr.dummy && e2->symtree->n.sym->attr.dummy)
130 /* Dummy arguments: Only check for equal names. */
131 if (e1->symtree->n.sym->name != e2->symtree->n.sym->name)
132 return false;
134 else
136 /* Check for equal symbols. */
137 if (e1->symtree->n.sym != e2->symtree->n.sym)
138 return false;
141 /* Volatile variables should never compare equal to themselves. */
143 if (e1->symtree->n.sym->attr.volatile_)
144 return false;
146 r1 = e1->ref;
147 r2 = e2->ref;
149 while (r1 != NULL || r2 != NULL)
152 /* Assume the variables are not equal if one has a reference and the
153 other doesn't.
154 TODO: Handle full references like comparing a(:) to a.
157 if (r1 == NULL || r2 == NULL)
158 return false;
160 if (r1->type != r2->type)
161 return false;
163 switch (r1->type)
166 case REF_ARRAY:
167 if (!identical_array_ref (&r1->u.ar, &r2->u.ar))
168 return false;
170 break;
172 case REF_COMPONENT:
173 if (r1->u.c.component != r2->u.c.component)
174 return false;
175 break;
177 case REF_SUBSTRING:
178 if (gfc_dep_compare_expr (r1->u.ss.start, r2->u.ss.start) != 0)
179 return false;
181 /* If both are NULL, the end length compares equal, because we
182 are looking at the same variable. This can only happen for
183 assumed- or deferred-length character arguments. */
185 if (r1->u.ss.end == NULL && r2->u.ss.end == NULL)
186 break;
188 if (gfc_dep_compare_expr (r1->u.ss.end, r2->u.ss.end) != 0)
189 return false;
191 break;
193 case REF_INQUIRY:
194 if (r1->u.i != r2->u.i)
195 return false;
196 break;
198 default:
199 gfc_internal_error ("are_identical_variables: Bad type");
201 r1 = r1->next;
202 r2 = r2->next;
204 return true;
207 /* Compare two functions for equality. Returns 0 if e1==e2, -2 otherwise. If
208 impure_ok is false, only return 0 for pure functions. */
211 gfc_dep_compare_functions (gfc_expr *e1, gfc_expr *e2, bool impure_ok)
214 gfc_actual_arglist *args1;
215 gfc_actual_arglist *args2;
217 if (e1->expr_type != EXPR_FUNCTION || e2->expr_type != EXPR_FUNCTION)
218 return -2;
220 if ((e1->value.function.esym && e2->value.function.esym
221 && e1->value.function.esym == e2->value.function.esym
222 && (e1->value.function.esym->result->attr.pure || impure_ok))
223 || (e1->value.function.isym && e2->value.function.isym
224 && e1->value.function.isym == e2->value.function.isym
225 && (e1->value.function.isym->pure || impure_ok)))
227 args1 = e1->value.function.actual;
228 args2 = e2->value.function.actual;
230 /* Compare the argument lists for equality. */
231 while (args1 && args2)
233 /* Bitwise xor, since C has no non-bitwise xor operator. */
234 if ((args1->expr == NULL) ^ (args2->expr == NULL))
235 return -2;
237 if (args1->expr != NULL && args2->expr != NULL)
239 gfc_expr *e1, *e2;
240 e1 = args1->expr;
241 e2 = args2->expr;
243 if (gfc_dep_compare_expr (e1, e2) != 0)
244 return -2;
246 /* Special case: String arguments which compare equal can have
247 different lengths, which makes them different in calls to
248 procedures. */
250 if (e1->expr_type == EXPR_CONSTANT
251 && e1->ts.type == BT_CHARACTER
252 && e2->expr_type == EXPR_CONSTANT
253 && e2->ts.type == BT_CHARACTER
254 && e1->value.character.length != e2->value.character.length)
255 return -2;
258 args1 = args1->next;
259 args2 = args2->next;
261 return (args1 || args2) ? -2 : 0;
263 else
264 return -2;
267 /* Helper function to look through parens, unary plus and widening
268 integer conversions. */
270 gfc_expr *
271 gfc_discard_nops (gfc_expr *e)
273 gfc_actual_arglist *arglist;
275 if (e == NULL)
276 return NULL;
278 while (true)
280 if (e->expr_type == EXPR_OP
281 && (e->value.op.op == INTRINSIC_UPLUS
282 || e->value.op.op == INTRINSIC_PARENTHESES))
284 e = e->value.op.op1;
285 continue;
288 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
289 && e->value.function.isym->id == GFC_ISYM_CONVERSION
290 && e->ts.type == BT_INTEGER)
292 arglist = e->value.function.actual;
293 if (arglist->expr->ts.type == BT_INTEGER
294 && e->ts.kind > arglist->expr->ts.kind)
296 e = arglist->expr;
297 continue;
300 break;
303 return e;
307 /* Compare two expressions. Return values:
308 * +1 if e1 > e2
309 * 0 if e1 == e2
310 * -1 if e1 < e2
311 * -2 if the relationship could not be determined
312 * -3 if e1 /= e2, but we cannot tell which one is larger.
313 REAL and COMPLEX constants are only compared for equality
314 or inequality; if they are unequal, -2 is returned in all cases. */
317 gfc_dep_compare_expr (gfc_expr *e1, gfc_expr *e2)
319 int i;
321 if (e1 == NULL && e2 == NULL)
322 return 0;
323 else if (e1 == NULL || e2 == NULL)
324 return -2;
326 e1 = gfc_discard_nops (e1);
327 e2 = gfc_discard_nops (e2);
329 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_PLUS)
331 /* Compare X+C vs. X, for INTEGER only. */
332 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
333 && e1->value.op.op2->ts.type == BT_INTEGER
334 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
335 return mpz_sgn (e1->value.op.op2->value.integer);
337 /* Compare P+Q vs. R+S. */
338 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
340 int l, r;
342 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
343 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
344 if (l == 0 && r == 0)
345 return 0;
346 if (l == 0 && r > -2)
347 return r;
348 if (l > -2 && r == 0)
349 return l;
350 if (l == 1 && r == 1)
351 return 1;
352 if (l == -1 && r == -1)
353 return -1;
355 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op2);
356 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op1);
357 if (l == 0 && r == 0)
358 return 0;
359 if (l == 0 && r > -2)
360 return r;
361 if (l > -2 && r == 0)
362 return l;
363 if (l == 1 && r == 1)
364 return 1;
365 if (l == -1 && r == -1)
366 return -1;
370 /* Compare X vs. X+C, for INTEGER only. */
371 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
373 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
374 && e2->value.op.op2->ts.type == BT_INTEGER
375 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
376 return -mpz_sgn (e2->value.op.op2->value.integer);
379 /* Compare X-C vs. X, for INTEGER only. */
380 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_MINUS)
382 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
383 && e1->value.op.op2->ts.type == BT_INTEGER
384 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
385 return -mpz_sgn (e1->value.op.op2->value.integer);
387 /* Compare P-Q vs. R-S. */
388 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
390 int l, r;
392 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
393 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
394 if (l == 0 && r == 0)
395 return 0;
396 if (l > -2 && r == 0)
397 return l;
398 if (l == 0 && r > -2)
399 return -r;
400 if (l == 1 && r == -1)
401 return 1;
402 if (l == -1 && r == 1)
403 return -1;
407 /* Compare A // B vs. C // D. */
409 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_CONCAT
410 && e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_CONCAT)
412 int l, r;
414 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
415 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
417 if (l != 0)
418 return l;
420 /* Left expressions of // compare equal, but
421 watch out for 'A ' // x vs. 'A' // x. */
422 gfc_expr *e1_left = e1->value.op.op1;
423 gfc_expr *e2_left = e2->value.op.op1;
425 if (e1_left->expr_type == EXPR_CONSTANT
426 && e2_left->expr_type == EXPR_CONSTANT
427 && e1_left->value.character.length
428 != e2_left->value.character.length)
429 return -2;
430 else
431 return r;
434 /* Compare X vs. X-C, for INTEGER only. */
435 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
437 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
438 && e2->value.op.op2->ts.type == BT_INTEGER
439 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
440 return mpz_sgn (e2->value.op.op2->value.integer);
444 if (e1->expr_type == EXPR_COMPCALL)
446 /* This will have emerged from interface.cc(gfc_check_typebound_override)
447 via gfc_check_result_characteristics. It is possible that other
448 variants exist that are 'equal' but play it safe for now by setting
449 the relationship as 'indeterminate'. */
450 if (e2->expr_type == EXPR_FUNCTION && e2->ref)
452 gfc_ref *ref = e2->ref;
453 gfc_symbol *s = NULL;
455 if (e1->value.compcall.tbp->u.specific)
456 s = e1->value.compcall.tbp->u.specific->n.sym;
458 /* Check if the proc ptr points to an interface declaration and the
459 names are the same; ie. the overriden proc. of an abstract type.
460 The checking of the arguments will already have been done. */
461 for (; ref && s; ref = ref->next)
462 if (!ref->next && ref->type == REF_COMPONENT
463 && ref->u.c.component->attr.proc_pointer
464 && ref->u.c.component->ts.interface
465 && ref->u.c.component->ts.interface->attr.if_source
466 == IFSRC_IFBODY
467 && !strcmp (s->name, ref->u.c.component->name))
468 return 0;
471 /* Assume as default that TKR checking is sufficient. */
472 return -2;
475 if (e1->expr_type != e2->expr_type)
476 return -3;
478 switch (e1->expr_type)
480 case EXPR_CONSTANT:
481 /* Compare strings for equality. */
482 if (e1->ts.type == BT_CHARACTER && e2->ts.type == BT_CHARACTER)
483 return gfc_compare_string (e1, e2);
485 /* Compare REAL and COMPLEX constants. Because of the
486 traps and pitfalls associated with comparing
487 a + 1.0 with a + 0.5, check for equality only. */
488 if (e2->expr_type == EXPR_CONSTANT)
490 if (e1->ts.type == BT_REAL && e2->ts.type == BT_REAL)
492 if (mpfr_cmp (e1->value.real, e2->value.real) == 0)
493 return 0;
494 else
495 return -2;
497 else if (e1->ts.type == BT_COMPLEX && e2->ts.type == BT_COMPLEX)
499 if (mpc_cmp (e1->value.complex, e2->value.complex) == 0)
500 return 0;
501 else
502 return -2;
506 if (e1->ts.type != BT_INTEGER || e2->ts.type != BT_INTEGER)
507 return -2;
509 /* For INTEGER, all cases where e2 is not constant should have
510 been filtered out above. */
511 gcc_assert (e2->expr_type == EXPR_CONSTANT);
513 i = mpz_cmp (e1->value.integer, e2->value.integer);
514 if (i == 0)
515 return 0;
516 else if (i < 0)
517 return -1;
518 return 1;
520 case EXPR_VARIABLE:
521 if (are_identical_variables (e1, e2))
522 return 0;
523 else
524 return -3;
526 case EXPR_OP:
527 /* Intrinsic operators are the same if their operands are the same. */
528 if (e1->value.op.op != e2->value.op.op)
529 return -2;
530 if (e1->value.op.op2 == 0)
532 i = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
533 return i == 0 ? 0 : -2;
535 if (gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1) == 0
536 && gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2) == 0)
537 return 0;
538 else if (e1->value.op.op == INTRINSIC_TIMES
539 && gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op2) == 0
540 && gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op1) == 0)
541 /* Commutativity of multiplication; addition is handled above. */
542 return 0;
544 return -2;
546 case EXPR_FUNCTION:
547 return gfc_dep_compare_functions (e1, e2, false);
549 default:
550 return -2;
555 /* Return the difference between two expressions. Integer expressions of
556 the form
558 X + constant, X - constant and constant + X
560 are handled. Return true on success, false on failure. result is assumed
561 to be uninitialized on entry, and will be initialized on success.
564 bool
565 gfc_dep_difference (gfc_expr *e1, gfc_expr *e2, mpz_t *result)
567 gfc_expr *e1_op1, *e1_op2, *e2_op1, *e2_op2;
569 if (e1 == NULL || e2 == NULL)
570 return false;
572 if (e1->ts.type != BT_INTEGER || e2->ts.type != BT_INTEGER)
573 return false;
575 e1 = gfc_discard_nops (e1);
576 e2 = gfc_discard_nops (e2);
578 /* Initialize tentatively, clear if we don't return anything. */
579 mpz_init (*result);
581 /* Case 1: c1 - c2 = c1 - c2, trivially. */
583 if (e1->expr_type == EXPR_CONSTANT && e2->expr_type == EXPR_CONSTANT)
585 mpz_sub (*result, e1->value.integer, e2->value.integer);
586 return true;
589 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_PLUS)
591 e1_op1 = gfc_discard_nops (e1->value.op.op1);
592 e1_op2 = gfc_discard_nops (e1->value.op.op2);
594 /* Case 2: (X + c1) - X = c1. */
595 if (e1_op2->expr_type == EXPR_CONSTANT
596 && gfc_dep_compare_expr (e1_op1, e2) == 0)
598 mpz_set (*result, e1_op2->value.integer);
599 return true;
602 /* Case 3: (c1 + X) - X = c1. */
603 if (e1_op1->expr_type == EXPR_CONSTANT
604 && gfc_dep_compare_expr (e1_op2, e2) == 0)
606 mpz_set (*result, e1_op1->value.integer);
607 return true;
610 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
612 e2_op1 = gfc_discard_nops (e2->value.op.op1);
613 e2_op2 = gfc_discard_nops (e2->value.op.op2);
615 if (e1_op2->expr_type == EXPR_CONSTANT)
617 /* Case 4: X + c1 - (X + c2) = c1 - c2. */
618 if (e2_op2->expr_type == EXPR_CONSTANT
619 && gfc_dep_compare_expr (e1_op1, e2_op1) == 0)
621 mpz_sub (*result, e1_op2->value.integer,
622 e2_op2->value.integer);
623 return true;
625 /* Case 5: X + c1 - (c2 + X) = c1 - c2. */
626 if (e2_op1->expr_type == EXPR_CONSTANT
627 && gfc_dep_compare_expr (e1_op1, e2_op2) == 0)
629 mpz_sub (*result, e1_op2->value.integer,
630 e2_op1->value.integer);
631 return true;
634 else if (e1_op1->expr_type == EXPR_CONSTANT)
636 /* Case 6: c1 + X - (X + c2) = c1 - c2. */
637 if (e2_op2->expr_type == EXPR_CONSTANT
638 && gfc_dep_compare_expr (e1_op2, e2_op1) == 0)
640 mpz_sub (*result, e1_op1->value.integer,
641 e2_op2->value.integer);
642 return true;
644 /* Case 7: c1 + X - (c2 + X) = c1 - c2. */
645 if (e2_op1->expr_type == EXPR_CONSTANT
646 && gfc_dep_compare_expr (e1_op2, e2_op2) == 0)
648 mpz_sub (*result, e1_op1->value.integer,
649 e2_op1->value.integer);
650 return true;
655 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
657 e2_op1 = gfc_discard_nops (e2->value.op.op1);
658 e2_op2 = gfc_discard_nops (e2->value.op.op2);
660 if (e1_op2->expr_type == EXPR_CONSTANT)
662 /* Case 8: X + c1 - (X - c2) = c1 + c2. */
663 if (e2_op2->expr_type == EXPR_CONSTANT
664 && gfc_dep_compare_expr (e1_op1, e2_op1) == 0)
666 mpz_add (*result, e1_op2->value.integer,
667 e2_op2->value.integer);
668 return true;
671 if (e1_op1->expr_type == EXPR_CONSTANT)
673 /* Case 9: c1 + X - (X - c2) = c1 + c2. */
674 if (e2_op2->expr_type == EXPR_CONSTANT
675 && gfc_dep_compare_expr (e1_op2, e2_op1) == 0)
677 mpz_add (*result, e1_op1->value.integer,
678 e2_op2->value.integer);
679 return true;
685 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_MINUS)
687 e1_op1 = gfc_discard_nops (e1->value.op.op1);
688 e1_op2 = gfc_discard_nops (e1->value.op.op2);
690 if (e1_op2->expr_type == EXPR_CONSTANT)
692 /* Case 10: (X - c1) - X = -c1 */
694 if (gfc_dep_compare_expr (e1_op1, e2) == 0)
696 mpz_neg (*result, e1_op2->value.integer);
697 return true;
700 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
702 e2_op1 = gfc_discard_nops (e2->value.op.op1);
703 e2_op2 = gfc_discard_nops (e2->value.op.op2);
705 /* Case 11: (X - c1) - (X + c2) = -( c1 + c2). */
706 if (e2_op2->expr_type == EXPR_CONSTANT
707 && gfc_dep_compare_expr (e1_op1, e2_op1) == 0)
709 mpz_add (*result, e1_op2->value.integer,
710 e2_op2->value.integer);
711 mpz_neg (*result, *result);
712 return true;
715 /* Case 12: X - c1 - (c2 + X) = - (c1 + c2). */
716 if (e2_op1->expr_type == EXPR_CONSTANT
717 && gfc_dep_compare_expr (e1_op1, e2_op2) == 0)
719 mpz_add (*result, e1_op2->value.integer,
720 e2_op1->value.integer);
721 mpz_neg (*result, *result);
722 return true;
726 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
728 e2_op1 = gfc_discard_nops (e2->value.op.op1);
729 e2_op2 = gfc_discard_nops (e2->value.op.op2);
731 /* Case 13: (X - c1) - (X - c2) = c2 - c1. */
732 if (e2_op2->expr_type == EXPR_CONSTANT
733 && gfc_dep_compare_expr (e1_op1, e2_op1) == 0)
735 mpz_sub (*result, e2_op2->value.integer,
736 e1_op2->value.integer);
737 return true;
741 if (e1_op1->expr_type == EXPR_CONSTANT)
743 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
745 e2_op1 = gfc_discard_nops (e2->value.op.op1);
746 e2_op2 = gfc_discard_nops (e2->value.op.op2);
748 /* Case 14: (c1 - X) - (c2 - X) == c1 - c2. */
749 if (gfc_dep_compare_expr (e1_op2, e2_op2) == 0)
751 mpz_sub (*result, e1_op1->value.integer,
752 e2_op1->value.integer);
753 return true;
760 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
762 e2_op1 = gfc_discard_nops (e2->value.op.op1);
763 e2_op2 = gfc_discard_nops (e2->value.op.op2);
765 /* Case 15: X - (X + c2) = -c2. */
766 if (e2_op2->expr_type == EXPR_CONSTANT
767 && gfc_dep_compare_expr (e1, e2_op1) == 0)
769 mpz_neg (*result, e2_op2->value.integer);
770 return true;
772 /* Case 16: X - (c2 + X) = -c2. */
773 if (e2_op1->expr_type == EXPR_CONSTANT
774 && gfc_dep_compare_expr (e1, e2_op2) == 0)
776 mpz_neg (*result, e2_op1->value.integer);
777 return true;
781 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
783 e2_op1 = gfc_discard_nops (e2->value.op.op1);
784 e2_op2 = gfc_discard_nops (e2->value.op.op2);
786 /* Case 17: X - (X - c2) = c2. */
787 if (e2_op2->expr_type == EXPR_CONSTANT
788 && gfc_dep_compare_expr (e1, e2_op1) == 0)
790 mpz_set (*result, e2_op2->value.integer);
791 return true;
795 if (gfc_dep_compare_expr (e1, e2) == 0)
797 /* Case 18: X - X = 0. */
798 mpz_set_si (*result, 0);
799 return true;
802 mpz_clear (*result);
803 return false;
806 /* Returns 1 if the two ranges are the same and 0 if they are not (or if the
807 results are indeterminate). 'n' is the dimension to compare. */
809 static int
810 is_same_range (gfc_array_ref *ar1, gfc_array_ref *ar2, int n)
812 gfc_expr *e1;
813 gfc_expr *e2;
814 int i;
816 /* TODO: More sophisticated range comparison. */
817 gcc_assert (ar1 && ar2);
819 gcc_assert (ar1->dimen_type[n] == ar2->dimen_type[n]);
821 e1 = ar1->stride[n];
822 e2 = ar2->stride[n];
823 /* Check for mismatching strides. A NULL stride means a stride of 1. */
824 if (e1 && !e2)
826 i = gfc_expr_is_one (e1, -1);
827 if (i == -1 || i == 0)
828 return 0;
830 else if (e2 && !e1)
832 i = gfc_expr_is_one (e2, -1);
833 if (i == -1 || i == 0)
834 return 0;
836 else if (e1 && e2)
838 i = gfc_dep_compare_expr (e1, e2);
839 if (i != 0)
840 return 0;
842 /* The strides match. */
844 /* Check the range start. */
845 e1 = ar1->start[n];
846 e2 = ar2->start[n];
847 if (e1 || e2)
849 /* Use the bound of the array if no bound is specified. */
850 if (ar1->as && !e1)
851 e1 = ar1->as->lower[n];
853 if (ar2->as && !e2)
854 e2 = ar2->as->lower[n];
856 /* Check we have values for both. */
857 if (!(e1 && e2))
858 return 0;
860 i = gfc_dep_compare_expr (e1, e2);
861 if (i != 0)
862 return 0;
865 /* Check the range end. */
866 e1 = ar1->end[n];
867 e2 = ar2->end[n];
868 if (e1 || e2)
870 /* Use the bound of the array if no bound is specified. */
871 if (ar1->as && !e1)
872 e1 = ar1->as->upper[n];
874 if (ar2->as && !e2)
875 e2 = ar2->as->upper[n];
877 /* Check we have values for both. */
878 if (!(e1 && e2))
879 return 0;
881 i = gfc_dep_compare_expr (e1, e2);
882 if (i != 0)
883 return 0;
886 return 1;
890 /* Some array-returning intrinsics can be implemented by reusing the
891 data from one of the array arguments. For example, TRANSPOSE does
892 not necessarily need to allocate new data: it can be implemented
893 by copying the original array's descriptor and simply swapping the
894 two dimension specifications.
896 If EXPR is a call to such an intrinsic, return the argument
897 whose data can be reused, otherwise return NULL. */
899 gfc_expr *
900 gfc_get_noncopying_intrinsic_argument (gfc_expr *expr)
902 if (expr->expr_type != EXPR_FUNCTION || !expr->value.function.isym)
903 return NULL;
905 switch (expr->value.function.isym->id)
907 case GFC_ISYM_TRANSPOSE:
908 return expr->value.function.actual->expr;
910 default:
911 return NULL;
916 /* Return true if the result of reference REF can only be constructed
917 using a temporary array. */
919 bool
920 gfc_ref_needs_temporary_p (gfc_ref *ref)
922 int n;
923 bool subarray_p;
925 subarray_p = false;
926 for (; ref; ref = ref->next)
927 switch (ref->type)
929 case REF_ARRAY:
930 /* Vector dimensions are generally not monotonic and must be
931 handled using a temporary. */
932 if (ref->u.ar.type == AR_SECTION)
933 for (n = 0; n < ref->u.ar.dimen; n++)
934 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR)
935 return true;
937 subarray_p = true;
938 break;
940 case REF_SUBSTRING:
941 /* Within an array reference, character substrings generally
942 need a temporary. Character array strides are expressed as
943 multiples of the element size (consistent with other array
944 types), not in characters. */
945 return subarray_p;
947 case REF_COMPONENT:
948 case REF_INQUIRY:
949 break;
952 return false;
956 static bool
957 gfc_is_data_pointer (gfc_expr *e)
959 gfc_ref *ref;
961 if (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION)
962 return 0;
964 /* No subreference if it is a function */
965 gcc_assert (e->expr_type == EXPR_VARIABLE || !e->ref);
967 if (e->symtree->n.sym->attr.pointer)
968 return 1;
970 for (ref = e->ref; ref; ref = ref->next)
971 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
972 return 1;
974 return 0;
978 /* Return true if array variable VAR could be passed to the same function
979 as argument EXPR without interfering with EXPR. INTENT is the intent
980 of VAR.
982 This is considerably less conservative than other dependencies
983 because many function arguments will already be copied into a
984 temporary. */
986 static int
987 gfc_check_argument_var_dependency (gfc_expr *var, sym_intent intent,
988 gfc_expr *expr, gfc_dep_check elemental)
990 gfc_expr *arg;
992 gcc_assert (var->expr_type == EXPR_VARIABLE);
993 gcc_assert (var->rank > 0);
995 switch (expr->expr_type)
997 case EXPR_VARIABLE:
998 /* In case of elemental subroutines, there is no dependency
999 between two same-range array references. */
1000 if (gfc_ref_needs_temporary_p (expr->ref)
1001 || gfc_check_dependency (var, expr, elemental == NOT_ELEMENTAL))
1003 if (elemental == ELEM_DONT_CHECK_VARIABLE)
1005 /* Too many false positive with pointers. */
1006 if (!gfc_is_data_pointer (var) && !gfc_is_data_pointer (expr))
1008 /* Elemental procedures forbid unspecified intents,
1009 and we don't check dependencies for INTENT_IN args. */
1010 gcc_assert (intent == INTENT_OUT || intent == INTENT_INOUT);
1012 /* We are told not to check dependencies.
1013 We do it, however, and issue a warning in case we find one.
1014 If a dependency is found in the case
1015 elemental == ELEM_CHECK_VARIABLE, we will generate
1016 a temporary, so we don't need to bother the user. */
1018 if (var->expr_type == EXPR_VARIABLE
1019 && expr->expr_type == EXPR_VARIABLE
1020 && strcmp(var->symtree->name, expr->symtree->name) == 0)
1021 gfc_warning (0, "INTENT(%s) actual argument at %L might "
1022 "interfere with actual argument at %L.",
1023 intent == INTENT_OUT ? "OUT" : "INOUT",
1024 &var->where, &expr->where);
1026 return 0;
1028 else
1029 return 1;
1031 return 0;
1033 case EXPR_ARRAY:
1034 /* the scalarizer always generates a temporary for array constructors,
1035 so there is no dependency. */
1036 return 0;
1038 case EXPR_FUNCTION:
1039 if (intent != INTENT_IN)
1041 arg = gfc_get_noncopying_intrinsic_argument (expr);
1042 if (arg != NULL)
1043 return gfc_check_argument_var_dependency (var, intent, arg,
1044 NOT_ELEMENTAL);
1047 if (elemental != NOT_ELEMENTAL)
1049 if ((expr->value.function.esym
1050 && expr->value.function.esym->attr.elemental)
1051 || (expr->value.function.isym
1052 && expr->value.function.isym->elemental))
1053 return gfc_check_fncall_dependency (var, intent, NULL,
1054 expr->value.function.actual,
1055 ELEM_CHECK_VARIABLE);
1057 if (gfc_inline_intrinsic_function_p (expr))
1059 /* The TRANSPOSE case should have been caught in the
1060 noncopying intrinsic case above. */
1061 gcc_assert (expr->value.function.isym->id != GFC_ISYM_TRANSPOSE);
1063 return gfc_check_fncall_dependency (var, intent, NULL,
1064 expr->value.function.actual,
1065 ELEM_CHECK_VARIABLE);
1068 return 0;
1070 case EXPR_OP:
1071 /* In case of non-elemental procedures, there is no need to catch
1072 dependencies, as we will make a temporary anyway. */
1073 if (elemental)
1075 /* If the actual arg EXPR is an expression, we need to catch
1076 a dependency between variables in EXPR and VAR,
1077 an intent((IN)OUT) variable. */
1078 if (expr->value.op.op1
1079 && gfc_check_argument_var_dependency (var, intent,
1080 expr->value.op.op1,
1081 ELEM_CHECK_VARIABLE))
1082 return 1;
1083 else if (expr->value.op.op2
1084 && gfc_check_argument_var_dependency (var, intent,
1085 expr->value.op.op2,
1086 ELEM_CHECK_VARIABLE))
1087 return 1;
1089 return 0;
1091 default:
1092 return 0;
1097 /* Like gfc_check_argument_var_dependency, but extended to any
1098 array expression OTHER, not just variables. */
1100 static int
1101 gfc_check_argument_dependency (gfc_expr *other, sym_intent intent,
1102 gfc_expr *expr, gfc_dep_check elemental)
1104 switch (other->expr_type)
1106 case EXPR_VARIABLE:
1107 return gfc_check_argument_var_dependency (other, intent, expr, elemental);
1109 case EXPR_FUNCTION:
1110 other = gfc_get_noncopying_intrinsic_argument (other);
1111 if (other != NULL)
1112 return gfc_check_argument_dependency (other, INTENT_IN, expr,
1113 NOT_ELEMENTAL);
1115 return 0;
1117 default:
1118 return 0;
1123 /* Like gfc_check_argument_dependency, but check all the arguments in ACTUAL.
1124 FNSYM is the function being called, or NULL if not known. */
1126 bool
1127 gfc_check_fncall_dependency (gfc_expr *other, sym_intent intent,
1128 gfc_symbol *fnsym, gfc_actual_arglist *actual,
1129 gfc_dep_check elemental)
1131 gfc_formal_arglist *formal;
1132 gfc_expr *expr;
1134 formal = fnsym ? gfc_sym_get_dummy_args (fnsym) : NULL;
1135 for (; actual; actual = actual->next, formal = formal ? formal->next : NULL)
1137 expr = actual->expr;
1139 /* Skip args which are not present. */
1140 if (!expr)
1141 continue;
1143 /* Skip other itself. */
1144 if (expr == other)
1145 continue;
1147 /* Skip intent(in) arguments if OTHER itself is intent(in). */
1148 if (formal && intent == INTENT_IN
1149 && formal->sym->attr.intent == INTENT_IN)
1150 continue;
1152 if (gfc_check_argument_dependency (other, intent, expr, elemental))
1153 return 1;
1156 return 0;
1160 /* Return 1 if e1 and e2 are equivalenced arrays, either
1161 directly or indirectly; i.e., equivalence (a,b) for a and b
1162 or equivalence (a,c),(b,c). This function uses the equiv_
1163 lists, generated in trans-common(add_equivalences), that are
1164 guaranteed to pick up indirect equivalences. We explicitly
1165 check for overlap using the offset and length of the equivalence.
1166 This function is symmetric.
1167 TODO: This function only checks whether the full top-level
1168 symbols overlap. An improved implementation could inspect
1169 e1->ref and e2->ref to determine whether the actually accessed
1170 portions of these variables/arrays potentially overlap. */
1172 bool
1173 gfc_are_equivalenced_arrays (gfc_expr *e1, gfc_expr *e2)
1175 gfc_equiv_list *l;
1176 gfc_equiv_info *s, *fl1, *fl2;
1178 gcc_assert (e1->expr_type == EXPR_VARIABLE
1179 && e2->expr_type == EXPR_VARIABLE);
1181 if (!e1->symtree->n.sym->attr.in_equivalence
1182 || !e2->symtree->n.sym->attr.in_equivalence|| !e1->rank || !e2->rank)
1183 return 0;
1185 if (e1->symtree->n.sym->ns
1186 && e1->symtree->n.sym->ns != gfc_current_ns)
1187 l = e1->symtree->n.sym->ns->equiv_lists;
1188 else
1189 l = gfc_current_ns->equiv_lists;
1191 /* Go through the equiv_lists and return 1 if the variables
1192 e1 and e2 are members of the same group and satisfy the
1193 requirement on their relative offsets. */
1194 for (; l; l = l->next)
1196 fl1 = NULL;
1197 fl2 = NULL;
1198 for (s = l->equiv; s; s = s->next)
1200 if (s->sym == e1->symtree->n.sym)
1202 fl1 = s;
1203 if (fl2)
1204 break;
1206 if (s->sym == e2->symtree->n.sym)
1208 fl2 = s;
1209 if (fl1)
1210 break;
1214 if (s)
1216 /* Can these lengths be zero? */
1217 if (fl1->length <= 0 || fl2->length <= 0)
1218 return 1;
1219 /* These can't overlap if [f11,fl1+length] is before
1220 [fl2,fl2+length], or [fl2,fl2+length] is before
1221 [fl1,fl1+length], otherwise they do overlap. */
1222 if (fl1->offset + fl1->length > fl2->offset
1223 && fl2->offset + fl2->length > fl1->offset)
1224 return 1;
1227 return 0;
1231 /* Return true if there is no possibility of aliasing because of a type
1232 mismatch between all the possible pointer references and the
1233 potential target. Note that this function is asymmetric in the
1234 arguments and so must be called twice with the arguments exchanged. */
1236 static bool
1237 check_data_pointer_types (gfc_expr *expr1, gfc_expr *expr2)
1239 gfc_component *cm1;
1240 gfc_symbol *sym1;
1241 gfc_symbol *sym2;
1242 gfc_ref *ref1;
1243 bool seen_component_ref;
1245 if (expr1->expr_type != EXPR_VARIABLE
1246 || expr2->expr_type != EXPR_VARIABLE)
1247 return false;
1249 sym1 = expr1->symtree->n.sym;
1250 sym2 = expr2->symtree->n.sym;
1252 /* Keep it simple for now. */
1253 if (sym1->ts.type == BT_DERIVED && sym2->ts.type == BT_DERIVED)
1254 return false;
1256 if (sym1->attr.pointer)
1258 if (gfc_compare_types (&sym1->ts, &sym2->ts))
1259 return false;
1262 /* This is a conservative check on the components of the derived type
1263 if no component references have been seen. Since we will not dig
1264 into the components of derived type components, we play it safe by
1265 returning false. First we check the reference chain and then, if
1266 no component references have been seen, the components. */
1267 seen_component_ref = false;
1268 if (sym1->ts.type == BT_DERIVED)
1270 for (ref1 = expr1->ref; ref1; ref1 = ref1->next)
1272 if (ref1->type != REF_COMPONENT)
1273 continue;
1275 if (ref1->u.c.component->ts.type == BT_DERIVED)
1276 return false;
1278 if ((sym2->attr.pointer || ref1->u.c.component->attr.pointer)
1279 && gfc_compare_types (&ref1->u.c.component->ts, &sym2->ts))
1280 return false;
1282 seen_component_ref = true;
1286 if (sym1->ts.type == BT_DERIVED && !seen_component_ref)
1288 for (cm1 = sym1->ts.u.derived->components; cm1; cm1 = cm1->next)
1290 if (cm1->ts.type == BT_DERIVED)
1291 return false;
1293 if ((sym2->attr.pointer || cm1->attr.pointer)
1294 && gfc_compare_types (&cm1->ts, &sym2->ts))
1295 return false;
1299 return true;
1303 /* Return true if the statement body redefines the condition. Returns
1304 true if expr2 depends on expr1. expr1 should be a single term
1305 suitable for the lhs of an assignment. The IDENTICAL flag indicates
1306 whether array references to the same symbol with identical range
1307 references count as a dependency or not. Used for forall and where
1308 statements. Also used with functions returning arrays without a
1309 temporary. */
1312 gfc_check_dependency (gfc_expr *expr1, gfc_expr *expr2, bool identical)
1314 gfc_actual_arglist *actual;
1315 gfc_constructor *c;
1316 int n;
1318 /* -fcoarray=lib can end up here with expr1->expr_type set to EXPR_FUNCTION
1319 and a reference to _F.caf_get, so skip the assert. */
1320 if (expr1->expr_type == EXPR_FUNCTION
1321 && strcmp (expr1->value.function.name, "_F.caf_get") == 0)
1322 return 0;
1324 if (expr1->expr_type != EXPR_VARIABLE)
1325 gfc_internal_error ("gfc_check_dependency: expecting an EXPR_VARIABLE");
1327 /* Prevent NULL pointer dereference while recursively analyzing invalid
1328 expressions. */
1329 if (expr2 == NULL)
1330 return 0;
1332 switch (expr2->expr_type)
1334 case EXPR_OP:
1335 n = gfc_check_dependency (expr1, expr2->value.op.op1, identical);
1336 if (n)
1337 return n;
1338 if (expr2->value.op.op2)
1339 return gfc_check_dependency (expr1, expr2->value.op.op2, identical);
1340 return 0;
1342 case EXPR_VARIABLE:
1343 /* The interesting cases are when the symbols don't match. */
1344 if (expr1->symtree->n.sym != expr2->symtree->n.sym)
1346 symbol_attribute attr1, attr2;
1347 gfc_typespec *ts1 = &expr1->symtree->n.sym->ts;
1348 gfc_typespec *ts2 = &expr2->symtree->n.sym->ts;
1350 /* Return 1 if expr1 and expr2 are equivalenced arrays. */
1351 if (gfc_are_equivalenced_arrays (expr1, expr2))
1352 return 1;
1354 /* Symbols can only alias if they have the same type. */
1355 if (ts1->type != BT_UNKNOWN && ts2->type != BT_UNKNOWN
1356 && ts1->type != BT_DERIVED && ts2->type != BT_DERIVED)
1358 if (ts1->type != ts2->type || ts1->kind != ts2->kind)
1359 return 0;
1362 /* We have to also include target-target as ptr%comp is not a
1363 pointer but it still alias with "dt%comp" for "ptr => dt". As
1364 subcomponents and array access to pointers retains the target
1365 attribute, that's sufficient. */
1366 attr1 = gfc_expr_attr (expr1);
1367 attr2 = gfc_expr_attr (expr2);
1368 if ((attr1.pointer || attr1.target) && (attr2.pointer || attr2.target))
1370 if (check_data_pointer_types (expr1, expr2)
1371 && check_data_pointer_types (expr2, expr1))
1372 return 0;
1374 return 1;
1376 else
1378 gfc_symbol *sym1 = expr1->symtree->n.sym;
1379 gfc_symbol *sym2 = expr2->symtree->n.sym;
1380 if (sym1->attr.target && sym2->attr.target
1381 && ((sym1->attr.dummy && !sym1->attr.contiguous
1382 && (!sym1->attr.dimension
1383 || sym2->as->type == AS_ASSUMED_SHAPE))
1384 || (sym2->attr.dummy && !sym2->attr.contiguous
1385 && (!sym2->attr.dimension
1386 || sym2->as->type == AS_ASSUMED_SHAPE))))
1387 return 1;
1390 /* Otherwise distinct symbols have no dependencies. */
1391 return 0;
1394 /* Identical and disjoint ranges return 0,
1395 overlapping ranges return 1. */
1396 if (expr1->ref && expr2->ref)
1397 return gfc_dep_resolver (expr1->ref, expr2->ref, NULL, identical);
1399 return 1;
1401 case EXPR_FUNCTION:
1402 if (gfc_get_noncopying_intrinsic_argument (expr2) != NULL)
1403 identical = 1;
1405 /* Remember possible differences between elemental and
1406 transformational functions. All functions inside a FORALL
1407 will be pure. */
1408 for (actual = expr2->value.function.actual;
1409 actual; actual = actual->next)
1411 if (!actual->expr)
1412 continue;
1413 n = gfc_check_dependency (expr1, actual->expr, identical);
1414 if (n)
1415 return n;
1417 return 0;
1419 case EXPR_CONSTANT:
1420 case EXPR_NULL:
1421 return 0;
1423 case EXPR_ARRAY:
1424 /* Loop through the array constructor's elements. */
1425 for (c = gfc_constructor_first (expr2->value.constructor);
1426 c; c = gfc_constructor_next (c))
1428 /* If this is an iterator, assume the worst. */
1429 if (c->iterator)
1430 return 1;
1431 /* Avoid recursion in the common case. */
1432 if (c->expr->expr_type == EXPR_CONSTANT)
1433 continue;
1434 if (gfc_check_dependency (expr1, c->expr, 1))
1435 return 1;
1437 return 0;
1439 default:
1440 return 1;
1445 /* Determines overlapping for two array sections. */
1447 static gfc_dependency
1448 check_section_vs_section (gfc_array_ref *l_ar, gfc_array_ref *r_ar, int n)
1450 gfc_expr *l_start;
1451 gfc_expr *l_end;
1452 gfc_expr *l_stride;
1453 gfc_expr *l_lower;
1454 gfc_expr *l_upper;
1455 int l_dir;
1457 gfc_expr *r_start;
1458 gfc_expr *r_end;
1459 gfc_expr *r_stride;
1460 gfc_expr *r_lower;
1461 gfc_expr *r_upper;
1462 gfc_expr *one_expr;
1463 int r_dir;
1464 int stride_comparison;
1465 int start_comparison;
1466 mpz_t tmp;
1468 /* If they are the same range, return without more ado. */
1469 if (is_same_range (l_ar, r_ar, n))
1470 return GFC_DEP_EQUAL;
1472 l_start = l_ar->start[n];
1473 l_end = l_ar->end[n];
1474 l_stride = l_ar->stride[n];
1476 r_start = r_ar->start[n];
1477 r_end = r_ar->end[n];
1478 r_stride = r_ar->stride[n];
1480 /* If l_start is NULL take it from array specifier. */
1481 if (l_start == NULL && IS_ARRAY_EXPLICIT (l_ar->as))
1482 l_start = l_ar->as->lower[n];
1483 /* If l_end is NULL take it from array specifier. */
1484 if (l_end == NULL && IS_ARRAY_EXPLICIT (l_ar->as))
1485 l_end = l_ar->as->upper[n];
1487 /* If r_start is NULL take it from array specifier. */
1488 if (r_start == NULL && IS_ARRAY_EXPLICIT (r_ar->as))
1489 r_start = r_ar->as->lower[n];
1490 /* If r_end is NULL take it from array specifier. */
1491 if (r_end == NULL && IS_ARRAY_EXPLICIT (r_ar->as))
1492 r_end = r_ar->as->upper[n];
1494 /* Determine whether the l_stride is positive or negative. */
1495 if (!l_stride)
1496 l_dir = 1;
1497 else if (l_stride->expr_type == EXPR_CONSTANT
1498 && l_stride->ts.type == BT_INTEGER)
1499 l_dir = mpz_sgn (l_stride->value.integer);
1500 else if (l_start && l_end)
1501 l_dir = gfc_dep_compare_expr (l_end, l_start);
1502 else
1503 l_dir = -2;
1505 /* Determine whether the r_stride is positive or negative. */
1506 if (!r_stride)
1507 r_dir = 1;
1508 else if (r_stride->expr_type == EXPR_CONSTANT
1509 && r_stride->ts.type == BT_INTEGER)
1510 r_dir = mpz_sgn (r_stride->value.integer);
1511 else if (r_start && r_end)
1512 r_dir = gfc_dep_compare_expr (r_end, r_start);
1513 else
1514 r_dir = -2;
1516 /* The strides should never be zero. */
1517 if (l_dir == 0 || r_dir == 0)
1518 return GFC_DEP_OVERLAP;
1520 /* Determine the relationship between the strides. Set stride_comparison to
1521 -2 if the dependency cannot be determined
1522 -1 if l_stride < r_stride
1523 0 if l_stride == r_stride
1524 1 if l_stride > r_stride
1525 as determined by gfc_dep_compare_expr. */
1527 one_expr = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
1529 stride_comparison = gfc_dep_compare_expr (l_stride ? l_stride : one_expr,
1530 r_stride ? r_stride : one_expr);
1532 if (l_start && r_start)
1533 start_comparison = gfc_dep_compare_expr (l_start, r_start);
1534 else
1535 start_comparison = -2;
1537 gfc_free_expr (one_expr);
1539 /* Determine LHS upper and lower bounds. */
1540 if (l_dir == 1)
1542 l_lower = l_start;
1543 l_upper = l_end;
1545 else if (l_dir == -1)
1547 l_lower = l_end;
1548 l_upper = l_start;
1550 else
1552 l_lower = NULL;
1553 l_upper = NULL;
1556 /* Determine RHS upper and lower bounds. */
1557 if (r_dir == 1)
1559 r_lower = r_start;
1560 r_upper = r_end;
1562 else if (r_dir == -1)
1564 r_lower = r_end;
1565 r_upper = r_start;
1567 else
1569 r_lower = NULL;
1570 r_upper = NULL;
1573 /* Check whether the ranges are disjoint. */
1574 if (l_upper && r_lower && gfc_dep_compare_expr (l_upper, r_lower) == -1)
1575 return GFC_DEP_NODEP;
1576 if (r_upper && l_lower && gfc_dep_compare_expr (r_upper, l_lower) == -1)
1577 return GFC_DEP_NODEP;
1579 /* Handle cases like x:y:1 vs. x:z:-1 as GFC_DEP_EQUAL. */
1580 if (l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == 0)
1582 if (l_dir == 1 && r_dir == -1)
1583 return GFC_DEP_EQUAL;
1584 if (l_dir == -1 && r_dir == 1)
1585 return GFC_DEP_EQUAL;
1588 /* Handle cases like x:y:1 vs. z:y:-1 as GFC_DEP_EQUAL. */
1589 if (l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == 0)
1591 if (l_dir == 1 && r_dir == -1)
1592 return GFC_DEP_EQUAL;
1593 if (l_dir == -1 && r_dir == 1)
1594 return GFC_DEP_EQUAL;
1597 /* Handle cases like x:y:2 vs. x+1:z:4 as GFC_DEP_NODEP.
1598 There is no dependency if the remainder of
1599 (l_start - r_start) / gcd(l_stride, r_stride) is
1600 nonzero.
1601 TODO:
1602 - Cases like a(1:4:2) = a(2:3) are still not handled.
1605 #define IS_CONSTANT_INTEGER(a) ((a) && ((a)->expr_type == EXPR_CONSTANT) \
1606 && (a)->ts.type == BT_INTEGER)
1608 if (IS_CONSTANT_INTEGER (l_stride) && IS_CONSTANT_INTEGER (r_stride)
1609 && gfc_dep_difference (l_start, r_start, &tmp))
1611 mpz_t gcd;
1612 int result;
1614 mpz_init (gcd);
1615 mpz_gcd (gcd, l_stride->value.integer, r_stride->value.integer);
1617 mpz_fdiv_r (tmp, tmp, gcd);
1618 result = mpz_cmp_si (tmp, 0L);
1620 mpz_clear (gcd);
1621 mpz_clear (tmp);
1623 if (result != 0)
1624 return GFC_DEP_NODEP;
1627 #undef IS_CONSTANT_INTEGER
1629 /* Check for forward dependencies x:y vs. x+1:z and x:y:z vs. x:y:z+1. */
1631 if (l_dir == 1 && r_dir == 1 &&
1632 (start_comparison == 0 || start_comparison == -1)
1633 && (stride_comparison == 0 || stride_comparison == -1))
1634 return GFC_DEP_FORWARD;
1636 /* Check for forward dependencies x:y:-1 vs. x-1:z:-1 and
1637 x:y:-1 vs. x:y:-2. */
1638 if (l_dir == -1 && r_dir == -1 &&
1639 (start_comparison == 0 || start_comparison == 1)
1640 && (stride_comparison == 0 || stride_comparison == 1))
1641 return GFC_DEP_FORWARD;
1643 if (stride_comparison == 0 || stride_comparison == -1)
1645 if (l_start && IS_ARRAY_EXPLICIT (l_ar->as))
1648 /* Check for a(low:y:s) vs. a(z:x:s) or
1649 a(low:y:s) vs. a(z:x:s+1) where a has a lower bound
1650 of low, which is always at least a forward dependence. */
1652 if (r_dir == 1
1653 && gfc_dep_compare_expr (l_start, l_ar->as->lower[n]) == 0)
1654 return GFC_DEP_FORWARD;
1658 if (stride_comparison == 0 || stride_comparison == 1)
1660 if (l_start && IS_ARRAY_EXPLICIT (l_ar->as))
1663 /* Check for a(high:y:-s) vs. a(z:x:-s) or
1664 a(high:y:-s vs. a(z:x:-s-1) where a has a higher bound
1665 of high, which is always at least a forward dependence. */
1667 if (r_dir == -1
1668 && gfc_dep_compare_expr (l_start, l_ar->as->upper[n]) == 0)
1669 return GFC_DEP_FORWARD;
1674 if (stride_comparison == 0)
1676 /* From here, check for backwards dependencies. */
1677 /* x+1:y vs. x:z. */
1678 if (l_dir == 1 && r_dir == 1 && start_comparison == 1)
1679 return GFC_DEP_BACKWARD;
1681 /* x-1:y:-1 vs. x:z:-1. */
1682 if (l_dir == -1 && r_dir == -1 && start_comparison == -1)
1683 return GFC_DEP_BACKWARD;
1686 return GFC_DEP_OVERLAP;
1690 /* Determines overlapping for a single element and a section. */
1692 static gfc_dependency
1693 gfc_check_element_vs_section( gfc_ref *lref, gfc_ref *rref, int n)
1695 gfc_array_ref *ref;
1696 gfc_expr *elem;
1697 gfc_expr *start;
1698 gfc_expr *end;
1699 gfc_expr *stride;
1700 int s;
1702 elem = lref->u.ar.start[n];
1703 if (!elem)
1704 return GFC_DEP_OVERLAP;
1706 ref = &rref->u.ar;
1707 start = ref->start[n] ;
1708 end = ref->end[n] ;
1709 stride = ref->stride[n];
1711 if (!start && IS_ARRAY_EXPLICIT (ref->as))
1712 start = ref->as->lower[n];
1713 if (!end && IS_ARRAY_EXPLICIT (ref->as))
1714 end = ref->as->upper[n];
1716 /* Determine whether the stride is positive or negative. */
1717 if (!stride)
1718 s = 1;
1719 else if (stride->expr_type == EXPR_CONSTANT
1720 && stride->ts.type == BT_INTEGER)
1721 s = mpz_sgn (stride->value.integer);
1722 else
1723 s = -2;
1725 /* Stride should never be zero. */
1726 if (s == 0)
1727 return GFC_DEP_OVERLAP;
1729 /* Positive strides. */
1730 if (s == 1)
1732 /* Check for elem < lower. */
1733 if (start && gfc_dep_compare_expr (elem, start) == -1)
1734 return GFC_DEP_NODEP;
1735 /* Check for elem > upper. */
1736 if (end && gfc_dep_compare_expr (elem, end) == 1)
1737 return GFC_DEP_NODEP;
1739 if (start && end)
1741 s = gfc_dep_compare_expr (start, end);
1742 /* Check for an empty range. */
1743 if (s == 1)
1744 return GFC_DEP_NODEP;
1745 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1746 return GFC_DEP_EQUAL;
1749 /* Negative strides. */
1750 else if (s == -1)
1752 /* Check for elem > upper. */
1753 if (end && gfc_dep_compare_expr (elem, start) == 1)
1754 return GFC_DEP_NODEP;
1755 /* Check for elem < lower. */
1756 if (start && gfc_dep_compare_expr (elem, end) == -1)
1757 return GFC_DEP_NODEP;
1759 if (start && end)
1761 s = gfc_dep_compare_expr (start, end);
1762 /* Check for an empty range. */
1763 if (s == -1)
1764 return GFC_DEP_NODEP;
1765 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1766 return GFC_DEP_EQUAL;
1769 /* Unknown strides. */
1770 else
1772 if (!start || !end)
1773 return GFC_DEP_OVERLAP;
1774 s = gfc_dep_compare_expr (start, end);
1775 if (s <= -2)
1776 return GFC_DEP_OVERLAP;
1777 /* Assume positive stride. */
1778 if (s == -1)
1780 /* Check for elem < lower. */
1781 if (gfc_dep_compare_expr (elem, start) == -1)
1782 return GFC_DEP_NODEP;
1783 /* Check for elem > upper. */
1784 if (gfc_dep_compare_expr (elem, end) == 1)
1785 return GFC_DEP_NODEP;
1787 /* Assume negative stride. */
1788 else if (s == 1)
1790 /* Check for elem > upper. */
1791 if (gfc_dep_compare_expr (elem, start) == 1)
1792 return GFC_DEP_NODEP;
1793 /* Check for elem < lower. */
1794 if (gfc_dep_compare_expr (elem, end) == -1)
1795 return GFC_DEP_NODEP;
1797 /* Equal bounds. */
1798 else if (s == 0)
1800 s = gfc_dep_compare_expr (elem, start);
1801 if (s == 0)
1802 return GFC_DEP_EQUAL;
1803 if (s == 1 || s == -1)
1804 return GFC_DEP_NODEP;
1808 return GFC_DEP_OVERLAP;
1812 /* Traverse expr, checking all EXPR_VARIABLE symbols for their
1813 forall_index attribute. Return true if any variable may be
1814 being used as a FORALL index. Its safe to pessimistically
1815 return true, and assume a dependency. */
1817 static bool
1818 contains_forall_index_p (gfc_expr *expr)
1820 gfc_actual_arglist *arg;
1821 gfc_constructor *c;
1822 gfc_ref *ref;
1823 int i;
1825 if (!expr)
1826 return false;
1828 switch (expr->expr_type)
1830 case EXPR_VARIABLE:
1831 if (expr->symtree->n.sym->forall_index)
1832 return true;
1833 break;
1835 case EXPR_OP:
1836 if (contains_forall_index_p (expr->value.op.op1)
1837 || contains_forall_index_p (expr->value.op.op2))
1838 return true;
1839 break;
1841 case EXPR_FUNCTION:
1842 for (arg = expr->value.function.actual; arg; arg = arg->next)
1843 if (contains_forall_index_p (arg->expr))
1844 return true;
1845 break;
1847 case EXPR_CONSTANT:
1848 case EXPR_NULL:
1849 case EXPR_SUBSTRING:
1850 break;
1852 case EXPR_STRUCTURE:
1853 case EXPR_ARRAY:
1854 for (c = gfc_constructor_first (expr->value.constructor);
1855 c; gfc_constructor_next (c))
1856 if (contains_forall_index_p (c->expr))
1857 return true;
1858 break;
1860 default:
1861 gcc_unreachable ();
1864 for (ref = expr->ref; ref; ref = ref->next)
1865 switch (ref->type)
1867 case REF_ARRAY:
1868 for (i = 0; i < ref->u.ar.dimen; i++)
1869 if (contains_forall_index_p (ref->u.ar.start[i])
1870 || contains_forall_index_p (ref->u.ar.end[i])
1871 || contains_forall_index_p (ref->u.ar.stride[i]))
1872 return true;
1873 break;
1875 case REF_COMPONENT:
1876 break;
1878 case REF_SUBSTRING:
1879 if (contains_forall_index_p (ref->u.ss.start)
1880 || contains_forall_index_p (ref->u.ss.end))
1881 return true;
1882 break;
1884 default:
1885 gcc_unreachable ();
1888 return false;
1891 /* Determines overlapping for two single element array references. */
1893 static gfc_dependency
1894 gfc_check_element_vs_element (gfc_ref *lref, gfc_ref *rref, int n)
1896 gfc_array_ref l_ar;
1897 gfc_array_ref r_ar;
1898 gfc_expr *l_start;
1899 gfc_expr *r_start;
1900 int i;
1902 l_ar = lref->u.ar;
1903 r_ar = rref->u.ar;
1904 l_start = l_ar.start[n] ;
1905 r_start = r_ar.start[n] ;
1906 i = gfc_dep_compare_expr (r_start, l_start);
1907 if (i == 0)
1908 return GFC_DEP_EQUAL;
1910 /* Treat two scalar variables as potentially equal. This allows
1911 us to prove that a(i,:) and a(j,:) have no dependency. See
1912 Gerald Roth, "Evaluation of Array Syntax Dependence Analysis",
1913 Proceedings of the International Conference on Parallel and
1914 Distributed Processing Techniques and Applications (PDPTA2001),
1915 Las Vegas, Nevada, June 2001. */
1916 /* However, we need to be careful when either scalar expression
1917 contains a FORALL index, as these can potentially change value
1918 during the scalarization/traversal of this array reference. */
1919 if (contains_forall_index_p (r_start) || contains_forall_index_p (l_start))
1920 return GFC_DEP_OVERLAP;
1922 if (i > -2)
1923 return GFC_DEP_NODEP;
1925 return GFC_DEP_EQUAL;
1928 /* Callback function for checking if an expression depends on a
1929 dummy variable which is any other than INTENT(IN). */
1931 static int
1932 callback_dummy_intent_not_in (gfc_expr **ep,
1933 int *walk_subtrees ATTRIBUTE_UNUSED,
1934 void *data ATTRIBUTE_UNUSED)
1936 gfc_expr *e = *ep;
1938 if (e->expr_type == EXPR_VARIABLE && e->symtree
1939 && e->symtree->n.sym->attr.dummy)
1940 return e->symtree->n.sym->attr.intent != INTENT_IN;
1941 else
1942 return 0;
1945 /* Auxiliary function to check if subexpressions have dummy variables which
1946 are not intent(in).
1949 static bool
1950 dummy_intent_not_in (gfc_expr **ep)
1952 return gfc_expr_walker (ep, callback_dummy_intent_not_in, NULL);
1955 /* Determine if an array ref, usually an array section specifies the
1956 entire array. In addition, if the second, pointer argument is
1957 provided, the function will return true if the reference is
1958 contiguous; eg. (:, 1) gives true but (1,:) gives false.
1959 If one of the bounds depends on a dummy variable which is
1960 not INTENT(IN), also return false, because the user may
1961 have changed the variable. */
1963 bool
1964 gfc_full_array_ref_p (gfc_ref *ref, bool *contiguous)
1966 int i;
1967 int n;
1968 bool lbound_OK = true;
1969 bool ubound_OK = true;
1971 if (contiguous)
1972 *contiguous = false;
1974 if (ref->type != REF_ARRAY)
1975 return false;
1977 if (ref->u.ar.type == AR_FULL)
1979 if (contiguous)
1980 *contiguous = true;
1981 return true;
1984 if (ref->u.ar.type != AR_SECTION)
1985 return false;
1986 if (ref->next)
1987 return false;
1989 for (i = 0; i < ref->u.ar.dimen; i++)
1991 /* If we have a single element in the reference, for the reference
1992 to be full, we need to ascertain that the array has a single
1993 element in this dimension and that we actually reference the
1994 correct element. */
1995 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
1997 /* This is unconditionally a contiguous reference if all the
1998 remaining dimensions are elements. */
1999 if (contiguous)
2001 *contiguous = true;
2002 for (n = i + 1; n < ref->u.ar.dimen; n++)
2003 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
2004 *contiguous = false;
2007 if (!ref->u.ar.as
2008 || !ref->u.ar.as->lower[i]
2009 || !ref->u.ar.as->upper[i]
2010 || gfc_dep_compare_expr (ref->u.ar.as->lower[i],
2011 ref->u.ar.as->upper[i])
2012 || !ref->u.ar.start[i]
2013 || gfc_dep_compare_expr (ref->u.ar.start[i],
2014 ref->u.ar.as->lower[i]))
2015 return false;
2016 else
2017 continue;
2020 /* Check the lower bound. */
2021 if (ref->u.ar.start[i]
2022 && (!ref->u.ar.as
2023 || !ref->u.ar.as->lower[i]
2024 || gfc_dep_compare_expr (ref->u.ar.start[i],
2025 ref->u.ar.as->lower[i])
2026 || dummy_intent_not_in (&ref->u.ar.start[i])))
2027 lbound_OK = false;
2028 /* Check the upper bound. */
2029 if (ref->u.ar.end[i]
2030 && (!ref->u.ar.as
2031 || !ref->u.ar.as->upper[i]
2032 || gfc_dep_compare_expr (ref->u.ar.end[i],
2033 ref->u.ar.as->upper[i])
2034 || dummy_intent_not_in (&ref->u.ar.end[i])))
2035 ubound_OK = false;
2036 /* Check the stride. */
2037 if (ref->u.ar.stride[i]
2038 && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
2039 return false;
2041 /* This is unconditionally a contiguous reference as long as all
2042 the subsequent dimensions are elements. */
2043 if (contiguous)
2045 *contiguous = true;
2046 for (n = i + 1; n < ref->u.ar.dimen; n++)
2047 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
2048 *contiguous = false;
2051 if (!lbound_OK || !ubound_OK)
2052 return false;
2054 return true;
2058 /* Determine if a full array is the same as an array section with one
2059 variable limit. For this to be so, the strides must both be unity
2060 and one of either start == lower or end == upper must be true. */
2062 static bool
2063 ref_same_as_full_array (gfc_ref *full_ref, gfc_ref *ref)
2065 int i;
2066 bool upper_or_lower;
2068 if (full_ref->type != REF_ARRAY)
2069 return false;
2070 if (full_ref->u.ar.type != AR_FULL)
2071 return false;
2072 if (ref->type != REF_ARRAY)
2073 return false;
2074 if (ref->u.ar.type == AR_FULL)
2075 return true;
2076 if (ref->u.ar.type != AR_SECTION)
2077 return false;
2079 for (i = 0; i < ref->u.ar.dimen; i++)
2081 /* If we have a single element in the reference, we need to check
2082 that the array has a single element and that we actually reference
2083 the correct element. */
2084 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
2086 if (!full_ref->u.ar.as
2087 || !full_ref->u.ar.as->lower[i]
2088 || !full_ref->u.ar.as->upper[i]
2089 || gfc_dep_compare_expr (full_ref->u.ar.as->lower[i],
2090 full_ref->u.ar.as->upper[i])
2091 || !ref->u.ar.start[i]
2092 || gfc_dep_compare_expr (ref->u.ar.start[i],
2093 full_ref->u.ar.as->lower[i]))
2094 return false;
2097 /* Check the strides. */
2098 if (full_ref->u.ar.stride[i] && !gfc_expr_is_one (full_ref->u.ar.stride[i], 0))
2099 return false;
2100 if (ref->u.ar.stride[i] && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
2101 return false;
2103 upper_or_lower = false;
2104 /* Check the lower bound. */
2105 if (ref->u.ar.start[i]
2106 && (ref->u.ar.as
2107 && full_ref->u.ar.as->lower[i]
2108 && gfc_dep_compare_expr (ref->u.ar.start[i],
2109 full_ref->u.ar.as->lower[i]) == 0))
2110 upper_or_lower = true;
2111 /* Check the upper bound. */
2112 if (ref->u.ar.end[i]
2113 && (ref->u.ar.as
2114 && full_ref->u.ar.as->upper[i]
2115 && gfc_dep_compare_expr (ref->u.ar.end[i],
2116 full_ref->u.ar.as->upper[i]) == 0))
2117 upper_or_lower = true;
2118 if (!upper_or_lower)
2119 return false;
2121 return true;
2125 /* Finds if two array references are overlapping or not.
2126 Return value
2127 1 : array references are overlapping, or identical is true and
2128 there is some kind of overlap.
2129 0 : array references are identical or not overlapping. */
2131 bool
2132 gfc_dep_resolver (gfc_ref *lref, gfc_ref *rref, gfc_reverse *reverse,
2133 bool identical)
2135 int n;
2136 int m;
2137 gfc_dependency fin_dep;
2138 gfc_dependency this_dep;
2139 bool same_component = false;
2141 this_dep = GFC_DEP_ERROR;
2142 fin_dep = GFC_DEP_ERROR;
2143 /* Dependencies due to pointers should already have been identified.
2144 We only need to check for overlapping array references. */
2146 while (lref && rref)
2148 /* The refs might come in mixed, one with a _data component and one
2149 without. Look at their next reference in order to avoid an
2150 ICE. */
2152 if (lref && lref->type == REF_COMPONENT && lref->u.c.component
2153 && strcmp (lref->u.c.component->name, "_data") == 0)
2154 lref = lref->next;
2156 if (rref && rref->type == REF_COMPONENT && rref->u.c.component
2157 && strcmp (rref->u.c.component->name, "_data") == 0)
2158 rref = rref->next;
2160 /* We're resolving from the same base symbol, so both refs should be
2161 the same type. We traverse the reference chain until we find ranges
2162 that are not equal. */
2163 gcc_assert (lref->type == rref->type);
2164 switch (lref->type)
2166 case REF_COMPONENT:
2167 /* The two ranges can't overlap if they are from different
2168 components. */
2169 if (lref->u.c.component != rref->u.c.component)
2170 return 0;
2172 same_component = true;
2173 break;
2175 case REF_SUBSTRING:
2176 /* Substring overlaps are handled by the string assignment code
2177 if there is not an underlying dependency. */
2178 return (fin_dep == GFC_DEP_OVERLAP) ? 1 : 0;
2180 case REF_ARRAY:
2181 /* Coarrays: If there is a coindex, either the image differs and there
2182 is no overlap or the image is the same - then the normal analysis
2183 applies. Hence, return early if either ref is coindexed and more
2184 than one image can exist. */
2185 if (flag_coarray != GFC_FCOARRAY_SINGLE
2186 && ((lref->u.ar.codimen
2187 && lref->u.ar.dimen_type[lref->u.ar.dimen]
2188 != DIMEN_THIS_IMAGE)
2189 || (rref->u.ar.codimen
2190 && lref->u.ar.dimen_type[lref->u.ar.dimen]
2191 != DIMEN_THIS_IMAGE)))
2192 return 1;
2193 if (lref->u.ar.dimen == 0 || rref->u.ar.dimen == 0)
2195 /* Coindexed scalar coarray with GFC_FCOARRAY_SINGLE. */
2196 if (lref->u.ar.dimen || rref->u.ar.dimen)
2197 return 1; /* Just to be sure. */
2198 fin_dep = GFC_DEP_EQUAL;
2199 break;
2202 if (ref_same_as_full_array (lref, rref))
2203 return identical;
2205 if (ref_same_as_full_array (rref, lref))
2206 return identical;
2208 if (lref->u.ar.dimen != rref->u.ar.dimen)
2210 if (lref->u.ar.type == AR_FULL)
2211 fin_dep = gfc_full_array_ref_p (rref, NULL) ? GFC_DEP_EQUAL
2212 : GFC_DEP_OVERLAP;
2213 else if (rref->u.ar.type == AR_FULL)
2214 fin_dep = gfc_full_array_ref_p (lref, NULL) ? GFC_DEP_EQUAL
2215 : GFC_DEP_OVERLAP;
2216 else
2217 return 1;
2218 break;
2221 /* Index for the reverse array. */
2222 m = -1;
2223 for (n = 0; n < lref->u.ar.dimen; n++)
2225 /* Handle dependency when either of array reference is vector
2226 subscript. There is no dependency if the vector indices
2227 are equal or if indices are known to be different in a
2228 different dimension. */
2229 if (lref->u.ar.dimen_type[n] == DIMEN_VECTOR
2230 || rref->u.ar.dimen_type[n] == DIMEN_VECTOR)
2232 if (lref->u.ar.dimen_type[n] == DIMEN_VECTOR
2233 && rref->u.ar.dimen_type[n] == DIMEN_VECTOR
2234 && gfc_dep_compare_expr (lref->u.ar.start[n],
2235 rref->u.ar.start[n]) == 0)
2236 this_dep = GFC_DEP_EQUAL;
2237 else
2238 this_dep = GFC_DEP_OVERLAP;
2240 goto update_fin_dep;
2243 if (lref->u.ar.dimen_type[n] == DIMEN_RANGE
2244 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
2245 this_dep = check_section_vs_section (&lref->u.ar,
2246 &rref->u.ar, n);
2247 else if (lref->u.ar.dimen_type[n] == DIMEN_ELEMENT
2248 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
2249 this_dep = gfc_check_element_vs_section (lref, rref, n);
2250 else if (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
2251 && lref->u.ar.dimen_type[n] == DIMEN_RANGE)
2252 this_dep = gfc_check_element_vs_section (rref, lref, n);
2253 else
2255 gcc_assert (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
2256 && lref->u.ar.dimen_type[n] == DIMEN_ELEMENT);
2257 this_dep = gfc_check_element_vs_element (rref, lref, n);
2258 if (identical && this_dep == GFC_DEP_EQUAL)
2259 this_dep = GFC_DEP_OVERLAP;
2262 /* If any dimension doesn't overlap, we have no dependency. */
2263 if (this_dep == GFC_DEP_NODEP)
2264 return 0;
2266 /* Now deal with the loop reversal logic: This only works on
2267 ranges and is activated by setting
2268 reverse[n] == GFC_ENABLE_REVERSE
2269 The ability to reverse or not is set by previous conditions
2270 in this dimension. If reversal is not activated, the
2271 value GFC_DEP_BACKWARD is reset to GFC_DEP_OVERLAP. */
2273 /* Get the indexing right for the scalarizing loop. If this
2274 is an element, there is no corresponding loop. */
2275 if (lref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
2276 m++;
2278 if (rref->u.ar.dimen_type[n] == DIMEN_RANGE
2279 && lref->u.ar.dimen_type[n] == DIMEN_RANGE)
2281 if (reverse)
2283 /* Reverse if backward dependence and not inhibited. */
2284 if (reverse[m] == GFC_ENABLE_REVERSE
2285 && this_dep == GFC_DEP_BACKWARD)
2286 reverse[m] = GFC_REVERSE_SET;
2288 /* Forward if forward dependence and not inhibited. */
2289 if (reverse[m] == GFC_ENABLE_REVERSE
2290 && this_dep == GFC_DEP_FORWARD)
2291 reverse[m] = GFC_FORWARD_SET;
2293 /* Flag up overlap if dependence not compatible with
2294 the overall state of the expression. */
2295 if (reverse[m] == GFC_REVERSE_SET
2296 && this_dep == GFC_DEP_FORWARD)
2298 reverse[m] = GFC_INHIBIT_REVERSE;
2299 this_dep = GFC_DEP_OVERLAP;
2301 else if (reverse[m] == GFC_FORWARD_SET
2302 && this_dep == GFC_DEP_BACKWARD)
2304 reverse[m] = GFC_INHIBIT_REVERSE;
2305 this_dep = GFC_DEP_OVERLAP;
2309 /* If no intention of reversing or reversing is explicitly
2310 inhibited, convert backward dependence to overlap. */
2311 if ((!reverse && this_dep == GFC_DEP_BACKWARD)
2312 || (reverse && reverse[m] == GFC_INHIBIT_REVERSE))
2313 this_dep = GFC_DEP_OVERLAP;
2316 /* Overlap codes are in order of priority. We only need to
2317 know the worst one.*/
2319 update_fin_dep:
2320 if (identical && this_dep == GFC_DEP_EQUAL)
2321 this_dep = GFC_DEP_OVERLAP;
2323 if (this_dep > fin_dep)
2324 fin_dep = this_dep;
2327 /* If this is an equal element, we have to keep going until we find
2328 the "real" array reference. */
2329 if (lref->u.ar.type == AR_ELEMENT
2330 && rref->u.ar.type == AR_ELEMENT
2331 && fin_dep == GFC_DEP_EQUAL)
2332 break;
2334 /* Exactly matching and forward overlapping ranges don't cause a
2335 dependency. */
2336 if (fin_dep < GFC_DEP_BACKWARD && !identical)
2337 return 0;
2339 /* Keep checking. We only have a dependency if
2340 subsequent references also overlap. */
2341 break;
2343 case REF_INQUIRY:
2344 if (lref->u.i != rref->u.i)
2345 return 0;
2347 break;
2349 default:
2350 gcc_unreachable ();
2352 lref = lref->next;
2353 rref = rref->next;
2356 /* Assume the worst if we nest to different depths. */
2357 if (lref || rref)
2358 return 1;
2360 /* This can result from concatenation of assumed length string components. */
2361 if (same_component && fin_dep == GFC_DEP_ERROR)
2362 return 1;
2364 /* If we haven't seen any array refs then something went wrong. */
2365 gcc_assert (fin_dep != GFC_DEP_ERROR);
2367 if (identical && fin_dep != GFC_DEP_NODEP)
2368 return 1;
2370 return fin_dep == GFC_DEP_OVERLAP;
2373 /* Check if two refs are equal, for the purposes of checking if one might be
2374 the base of the other for OpenMP (target directives). Derived from
2375 gfc_dep_resolver. This function is stricter, e.g. indices arr(i) and
2376 arr(j) compare as non-equal. */
2378 bool
2379 gfc_omp_expr_prefix_same (gfc_expr *lexpr, gfc_expr *rexpr)
2381 gfc_ref *lref, *rref;
2383 if (lexpr->symtree && rexpr->symtree)
2385 /* See are_identical_variables above. */
2386 if (lexpr->symtree->n.sym->attr.dummy
2387 && rexpr->symtree->n.sym->attr.dummy)
2389 /* Dummy arguments: Only check for equal names. */
2390 if (lexpr->symtree->n.sym->name != rexpr->symtree->n.sym->name)
2391 return false;
2393 else
2395 if (lexpr->symtree->n.sym != rexpr->symtree->n.sym)
2396 return false;
2399 else if (lexpr->base_expr && rexpr->base_expr)
2401 if (gfc_dep_compare_expr (lexpr->base_expr, rexpr->base_expr) != 0)
2402 return false;
2404 else
2405 return false;
2407 lref = lexpr->ref;
2408 rref = rexpr->ref;
2410 while (lref && rref)
2412 gfc_dependency fin_dep = GFC_DEP_EQUAL;
2414 if (lref && lref->type == REF_COMPONENT && lref->u.c.component
2415 && strcmp (lref->u.c.component->name, "_data") == 0)
2416 lref = lref->next;
2418 if (rref && rref->type == REF_COMPONENT && rref->u.c.component
2419 && strcmp (rref->u.c.component->name, "_data") == 0)
2420 rref = rref->next;
2422 gcc_assert (lref->type == rref->type);
2424 switch (lref->type)
2426 case REF_COMPONENT:
2427 if (lref->u.c.component != rref->u.c.component)
2428 return false;
2429 break;
2431 case REF_ARRAY:
2432 if (ref_same_as_full_array (lref, rref))
2433 break;
2434 if (ref_same_as_full_array (rref, lref))
2435 break;
2437 if (lref->u.ar.dimen != rref->u.ar.dimen)
2439 if (lref->u.ar.type == AR_FULL
2440 && gfc_full_array_ref_p (rref, NULL))
2441 break;
2442 if (rref->u.ar.type == AR_FULL
2443 && gfc_full_array_ref_p (lref, NULL))
2444 break;
2445 return false;
2448 for (int n = 0; n < lref->u.ar.dimen; n++)
2450 if (lref->u.ar.dimen_type[n] == DIMEN_VECTOR
2451 && rref->u.ar.dimen_type[n] == DIMEN_VECTOR
2452 && gfc_dep_compare_expr (lref->u.ar.start[n],
2453 rref->u.ar.start[n]) == 0)
2454 continue;
2455 if (lref->u.ar.dimen_type[n] == DIMEN_RANGE
2456 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
2457 fin_dep = check_section_vs_section (&lref->u.ar, &rref->u.ar,
2459 else if (lref->u.ar.dimen_type[n] == DIMEN_ELEMENT
2460 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
2461 fin_dep = gfc_check_element_vs_section (lref, rref, n);
2462 else if (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
2463 && lref->u.ar.dimen_type[n] == DIMEN_RANGE)
2464 fin_dep = gfc_check_element_vs_section (rref, lref, n);
2465 else if (lref->u.ar.dimen_type[n] == DIMEN_ELEMENT
2466 && rref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
2468 gfc_array_ref l_ar = lref->u.ar;
2469 gfc_array_ref r_ar = rref->u.ar;
2470 gfc_expr *l_start = l_ar.start[n];
2471 gfc_expr *r_start = r_ar.start[n];
2472 int i = gfc_dep_compare_expr (r_start, l_start);
2473 if (i == 0)
2474 fin_dep = GFC_DEP_EQUAL;
2475 else
2476 return false;
2478 else
2479 return false;
2480 if (n + 1 < lref->u.ar.dimen
2481 && fin_dep != GFC_DEP_EQUAL)
2482 return false;
2485 if (fin_dep != GFC_DEP_EQUAL
2486 && fin_dep != GFC_DEP_OVERLAP)
2487 return false;
2489 break;
2491 default:
2492 gcc_unreachable ();
2494 lref = lref->next;
2495 rref = rref->next;
2498 return true;
2502 /* gfc_function_dependency returns true for non-dummy symbols with dependencies
2503 on an old-fashioned function result (ie. proc_name = proc_name->result).
2504 This is used to ensure that initialization code appears after the function
2505 result is treated and that any mutual dependencies between these symbols are
2506 respected. */
2508 static bool
2509 dependency_fcn (gfc_expr *e, gfc_symbol *sym,
2510 int *f ATTRIBUTE_UNUSED)
2512 if (e == NULL)
2513 return false;
2515 if (e && e->expr_type == EXPR_VARIABLE)
2517 if (e->symtree && e->symtree->n.sym == sym)
2518 return true;
2519 /* Recurse to see if this symbol is dependent on the function result. If
2520 so an indirect dependence exists, which should be handled in the same
2521 way as a direct dependence. The recursion is prevented from being
2522 infinite by statement order. */
2523 else if (e->symtree && e->symtree->n.sym)
2524 return gfc_function_dependency (e->symtree->n.sym, sym);
2527 return false;
2531 bool
2532 gfc_function_dependency (gfc_symbol *sym, gfc_symbol *proc_name)
2534 bool dep = false;
2536 if (proc_name && proc_name->attr.function
2537 && proc_name == proc_name->result
2538 && !(sym->attr.dummy || sym->attr.result))
2540 if (sym->fn_result_dep)
2541 return true;
2543 if (sym->as && sym->as->type == AS_EXPLICIT)
2545 for (int dim = 0; dim < sym->as->rank; dim++)
2547 if (sym->as->lower[dim]
2548 && sym->as->lower[dim]->expr_type != EXPR_CONSTANT)
2549 dep = gfc_traverse_expr (sym->as->lower[dim], proc_name,
2550 dependency_fcn, 0);
2551 if (dep)
2553 sym->fn_result_dep = 1;
2554 return true;
2556 if (sym->as->upper[dim]
2557 && sym->as->upper[dim]->expr_type != EXPR_CONSTANT)
2558 dep = gfc_traverse_expr (sym->as->upper[dim], proc_name,
2559 dependency_fcn, 0);
2560 if (dep)
2562 sym->fn_result_dep = 1;
2563 return true;
2568 if (sym->ts.type == BT_CHARACTER
2569 && sym->ts.u.cl && sym->ts.u.cl->length
2570 && sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
2571 dep = gfc_traverse_expr (sym->ts.u.cl->length, proc_name,
2572 dependency_fcn, 0);
2573 if (dep)
2575 sym->fn_result_dep = 1;
2576 return true;
2580 return false;