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
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
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. */
28 #include "coretypes.h"
30 #include "dependency.h"
31 #include "constructor.h"
35 /* static declarations */
37 enum range
{LHS
, RHS
, MID
};
39 /* Dependency types. These must be in reverse order of priority. */
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. */
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. */
62 gfc_expr_is_one (gfc_expr
*expr
, int def
)
64 gcc_assert (expr
!= NULL
);
66 if (expr
->expr_type
!= EXPR_CONSTANT
)
69 if (expr
->ts
.type
!= BT_INTEGER
)
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. */
79 identical_array_ref (gfc_array_ref
*a1
, gfc_array_ref
*a2
)
83 if (a1
->type
== AR_FULL
&& a2
->type
== AR_FULL
)
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
)
97 if (check_section_vs_section (a1
, a2
, i
) != GFC_DEP_EQUAL
)
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)
120 /* Return true for identical variables, checking for references if
121 necessary. Calls identical_array_ref for checking array sections. */
124 are_identical_variables (gfc_expr
*e1
, gfc_expr
*e2
)
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
)
136 /* Check for equal symbols. */
137 if (e1
->symtree
->n
.sym
!= e2
->symtree
->n
.sym
)
141 /* Volatile variables should never compare equal to themselves. */
143 if (e1
->symtree
->n
.sym
->attr
.volatile_
)
149 while (r1
!= NULL
|| r2
!= NULL
)
152 /* Assume the variables are not equal if one has a reference and the
154 TODO: Handle full references like comparing a(:) to a.
157 if (r1
== NULL
|| r2
== NULL
)
160 if (r1
->type
!= r2
->type
)
167 if (!identical_array_ref (&r1
->u
.ar
, &r2
->u
.ar
))
173 if (r1
->u
.c
.component
!= r2
->u
.c
.component
)
178 if (gfc_dep_compare_expr (r1
->u
.ss
.start
, r2
->u
.ss
.start
) != 0)
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
)
188 if (gfc_dep_compare_expr (r1
->u
.ss
.end
, r2
->u
.ss
.end
) != 0)
194 if (r1
->u
.i
!= r2
->u
.i
)
199 gfc_internal_error ("are_identical_variables: Bad type");
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
)
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
))
237 if (args1
->expr
!= NULL
&& args2
->expr
!= NULL
)
243 if (gfc_dep_compare_expr (e1
, e2
) != 0)
246 /* Special case: String arguments which compare equal can have
247 different lengths, which makes them different in calls to
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
)
261 return (args1
|| args2
) ? -2 : 0;
267 /* Helper function to look through parens, unary plus and widening
268 integer conversions. */
271 gfc_discard_nops (gfc_expr
*e
)
273 gfc_actual_arglist
*arglist
;
280 if (e
->expr_type
== EXPR_OP
281 && (e
->value
.op
.op
== INTRINSIC_UPLUS
282 || e
->value
.op
.op
== INTRINSIC_PARENTHESES
))
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
)
307 /* Compare two expressions. Return values:
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
)
321 if (e1
== NULL
&& e2
== NULL
)
323 else if (e1
== NULL
|| e2
== NULL
)
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
)
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)
346 if (l
== 0 && r
> -2)
348 if (l
> -2 && r
== 0)
350 if (l
== 1 && r
== 1)
352 if (l
== -1 && r
== -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)
359 if (l
== 0 && r
> -2)
361 if (l
> -2 && r
== 0)
363 if (l
== 1 && r
== 1)
365 if (l
== -1 && r
== -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
)
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)
396 if (l
> -2 && r
== 0)
398 if (l
== 0 && r
> -2)
400 if (l
== 1 && r
== -1)
402 if (l
== -1 && r
== 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
)
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
);
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
)
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
467 && !strcmp (s
->name
, ref
->u
.c
.component
->name
))
471 /* Assume as default that TKR checking is sufficient. */
475 if (e1
->expr_type
!= e2
->expr_type
)
478 switch (e1
->expr_type
)
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)
497 else if (e1
->ts
.type
== BT_COMPLEX
&& e2
->ts
.type
== BT_COMPLEX
)
499 if (mpc_cmp (e1
->value
.complex, e2
->value
.complex) == 0)
506 if (e1
->ts
.type
!= BT_INTEGER
|| e2
->ts
.type
!= BT_INTEGER
)
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
);
521 if (are_identical_variables (e1
, e2
))
527 /* Intrinsic operators are the same if their operands are the same. */
528 if (e1
->value
.op
.op
!= e2
->value
.op
.op
)
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)
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. */
547 return gfc_dep_compare_functions (e1
, e2
, false);
555 /* Return the difference between two expressions. Integer expressions of
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.
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
)
572 if (e1
->ts
.type
!= BT_INTEGER
|| e2
->ts
.type
!= BT_INTEGER
)
575 e1
= gfc_discard_nops (e1
);
576 e2
= gfc_discard_nops (e2
);
578 /* Initialize tentatively, clear if we don't return anything. */
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
795 if (gfc_dep_compare_expr (e1
, e2
) == 0)
797 /* Case 18: X - X = 0. */
798 mpz_set_si (*result
, 0);
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. */
810 is_same_range (gfc_array_ref
*ar1
, gfc_array_ref
*ar2
, int n
)
816 /* TODO: More sophisticated range comparison. */
817 gcc_assert (ar1
&& ar2
);
819 gcc_assert (ar1
->dimen_type
[n
] == ar2
->dimen_type
[n
]);
823 /* Check for mismatching strides. A NULL stride means a stride of 1. */
826 i
= gfc_expr_is_one (e1
, -1);
827 if (i
== -1 || i
== 0)
832 i
= gfc_expr_is_one (e2
, -1);
833 if (i
== -1 || i
== 0)
838 i
= gfc_dep_compare_expr (e1
, e2
);
842 /* The strides match. */
844 /* Check the range start. */
849 /* Use the bound of the array if no bound is specified. */
851 e1
= ar1
->as
->lower
[n
];
854 e2
= ar2
->as
->lower
[n
];
856 /* Check we have values for both. */
860 i
= gfc_dep_compare_expr (e1
, e2
);
865 /* Check the range end. */
870 /* Use the bound of the array if no bound is specified. */
872 e1
= ar1
->as
->upper
[n
];
875 e2
= ar2
->as
->upper
[n
];
877 /* Check we have values for both. */
881 i
= gfc_dep_compare_expr (e1
, e2
);
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. */
900 gfc_get_noncopying_intrinsic_argument (gfc_expr
*expr
)
902 if (expr
->expr_type
!= EXPR_FUNCTION
|| !expr
->value
.function
.isym
)
905 switch (expr
->value
.function
.isym
->id
)
907 case GFC_ISYM_TRANSPOSE
:
908 return expr
->value
.function
.actual
->expr
;
916 /* Return true if the result of reference REF can only be constructed
917 using a temporary array. */
920 gfc_ref_needs_temporary_p (gfc_ref
*ref
)
926 for (; ref
; ref
= ref
->next
)
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
)
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. */
957 gfc_is_data_pointer (gfc_expr
*e
)
961 if (e
->expr_type
!= EXPR_VARIABLE
&& e
->expr_type
!= EXPR_FUNCTION
)
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
)
970 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
971 if (ref
->type
== REF_COMPONENT
&& ref
->u
.c
.component
->attr
.pointer
)
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
982 This is considerably less conservative than other dependencies
983 because many function arguments will already be copied into a
987 gfc_check_argument_var_dependency (gfc_expr
*var
, sym_intent intent
,
988 gfc_expr
*expr
, gfc_dep_check elemental
)
992 gcc_assert (var
->expr_type
== EXPR_VARIABLE
);
993 gcc_assert (var
->rank
> 0);
995 switch (expr
->expr_type
)
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
);
1034 /* the scalarizer always generates a temporary for array constructors,
1035 so there is no dependency. */
1039 if (intent
!= INTENT_IN
)
1041 arg
= gfc_get_noncopying_intrinsic_argument (expr
);
1043 return gfc_check_argument_var_dependency (var
, intent
, arg
,
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
);
1071 /* In case of non-elemental procedures, there is no need to catch
1072 dependencies, as we will make a temporary anyway. */
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
,
1081 ELEM_CHECK_VARIABLE
))
1083 else if (expr
->value
.op
.op2
1084 && gfc_check_argument_var_dependency (var
, intent
,
1086 ELEM_CHECK_VARIABLE
))
1097 /* Like gfc_check_argument_var_dependency, but extended to any
1098 array expression OTHER, not just variables. */
1101 gfc_check_argument_dependency (gfc_expr
*other
, sym_intent intent
,
1102 gfc_expr
*expr
, gfc_dep_check elemental
)
1104 switch (other
->expr_type
)
1107 return gfc_check_argument_var_dependency (other
, intent
, expr
, elemental
);
1110 other
= gfc_get_noncopying_intrinsic_argument (other
);
1112 return gfc_check_argument_dependency (other
, INTENT_IN
, expr
,
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. */
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
;
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. */
1143 /* Skip other itself. */
1147 /* Skip intent(in) arguments if OTHER itself is intent(in). */
1148 if (formal
&& intent
== INTENT_IN
1149 && formal
->sym
->attr
.intent
== INTENT_IN
)
1152 if (gfc_check_argument_dependency (other
, intent
, expr
, elemental
))
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. */
1173 gfc_are_equivalenced_arrays (gfc_expr
*e1
, gfc_expr
*e2
)
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
)
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
;
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
)
1198 for (s
= l
->equiv
; s
; s
= s
->next
)
1200 if (s
->sym
== e1
->symtree
->n
.sym
)
1206 if (s
->sym
== e2
->symtree
->n
.sym
)
1216 /* Can these lengths be zero? */
1217 if (fl1
->length
<= 0 || fl2
->length
<= 0)
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
)
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. */
1237 check_data_pointer_types (gfc_expr
*expr1
, gfc_expr
*expr2
)
1243 bool seen_component_ref
;
1245 if (expr1
->expr_type
!= EXPR_VARIABLE
1246 || expr2
->expr_type
!= EXPR_VARIABLE
)
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
)
1256 if (sym1
->attr
.pointer
)
1258 if (gfc_compare_types (&sym1
->ts
, &sym2
->ts
))
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
)
1275 if (ref1
->u
.c
.component
->ts
.type
== BT_DERIVED
)
1278 if ((sym2
->attr
.pointer
|| ref1
->u
.c
.component
->attr
.pointer
)
1279 && gfc_compare_types (&ref1
->u
.c
.component
->ts
, &sym2
->ts
))
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
)
1293 if ((sym2
->attr
.pointer
|| cm1
->attr
.pointer
)
1294 && gfc_compare_types (&cm1
->ts
, &sym2
->ts
))
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
1312 gfc_check_dependency (gfc_expr
*expr1
, gfc_expr
*expr2
, bool identical
)
1314 gfc_actual_arglist
*actual
;
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)
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
1332 switch (expr2
->expr_type
)
1335 n
= gfc_check_dependency (expr1
, expr2
->value
.op
.op1
, identical
);
1338 if (expr2
->value
.op
.op2
)
1339 return gfc_check_dependency (expr1
, expr2
->value
.op
.op2
, identical
);
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
))
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
)
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
))
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
))))
1390 /* Otherwise distinct symbols have no dependencies. */
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
);
1402 if (gfc_get_noncopying_intrinsic_argument (expr2
) != NULL
)
1405 /* Remember possible differences between elemental and
1406 transformational functions. All functions inside a FORALL
1408 for (actual
= expr2
->value
.function
.actual
;
1409 actual
; actual
= actual
->next
)
1413 n
= gfc_check_dependency (expr1
, actual
->expr
, identical
);
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. */
1431 /* Avoid recursion in the common case. */
1432 if (c
->expr
->expr_type
== EXPR_CONSTANT
)
1434 if (gfc_check_dependency (expr1
, c
->expr
, 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
)
1464 int stride_comparison
;
1465 int start_comparison
;
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. */
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
);
1505 /* Determine whether the r_stride is positive or negative. */
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
);
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
);
1535 start_comparison
= -2;
1537 gfc_free_expr (one_expr
);
1539 /* Determine LHS upper and lower bounds. */
1545 else if (l_dir
== -1)
1556 /* Determine RHS upper and lower bounds. */
1562 else if (r_dir
== -1)
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
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
))
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);
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. */
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. */
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
)
1702 elem
= lref
->u
.ar
.start
[n
];
1704 return GFC_DEP_OVERLAP
;
1707 start
= ref
->start
[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. */
1719 else if (stride
->expr_type
== EXPR_CONSTANT
1720 && stride
->ts
.type
== BT_INTEGER
)
1721 s
= mpz_sgn (stride
->value
.integer
);
1725 /* Stride should never be zero. */
1727 return GFC_DEP_OVERLAP
;
1729 /* Positive strides. */
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
;
1741 s
= gfc_dep_compare_expr (start
, end
);
1742 /* Check for an empty range. */
1744 return GFC_DEP_NODEP
;
1745 if (s
== 0 && gfc_dep_compare_expr (elem
, start
) == 0)
1746 return GFC_DEP_EQUAL
;
1749 /* Negative strides. */
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
;
1761 s
= gfc_dep_compare_expr (start
, end
);
1762 /* Check for an empty range. */
1764 return GFC_DEP_NODEP
;
1765 if (s
== 0 && gfc_dep_compare_expr (elem
, start
) == 0)
1766 return GFC_DEP_EQUAL
;
1769 /* Unknown strides. */
1773 return GFC_DEP_OVERLAP
;
1774 s
= gfc_dep_compare_expr (start
, end
);
1776 return GFC_DEP_OVERLAP
;
1777 /* Assume positive stride. */
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. */
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
;
1800 s
= gfc_dep_compare_expr (elem
, start
);
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. */
1818 contains_forall_index_p (gfc_expr
*expr
)
1820 gfc_actual_arglist
*arg
;
1828 switch (expr
->expr_type
)
1831 if (expr
->symtree
->n
.sym
->forall_index
)
1836 if (contains_forall_index_p (expr
->value
.op
.op1
)
1837 || contains_forall_index_p (expr
->value
.op
.op2
))
1842 for (arg
= expr
->value
.function
.actual
; arg
; arg
= arg
->next
)
1843 if (contains_forall_index_p (arg
->expr
))
1849 case EXPR_SUBSTRING
:
1852 case EXPR_STRUCTURE
:
1854 for (c
= gfc_constructor_first (expr
->value
.constructor
);
1855 c
; gfc_constructor_next (c
))
1856 if (contains_forall_index_p (c
->expr
))
1864 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
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
]))
1879 if (contains_forall_index_p (ref
->u
.ss
.start
)
1880 || contains_forall_index_p (ref
->u
.ss
.end
))
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
)
1904 l_start
= l_ar
.start
[n
] ;
1905 r_start
= r_ar
.start
[n
] ;
1906 i
= gfc_dep_compare_expr (r_start
, l_start
);
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
;
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). */
1932 callback_dummy_intent_not_in (gfc_expr
**ep
,
1933 int *walk_subtrees ATTRIBUTE_UNUSED
,
1934 void *data ATTRIBUTE_UNUSED
)
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
;
1945 /* Auxiliary function to check if subexpressions have dummy variables which
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. */
1964 gfc_full_array_ref_p (gfc_ref
*ref
, bool *contiguous
)
1968 bool lbound_OK
= true;
1969 bool ubound_OK
= true;
1972 *contiguous
= false;
1974 if (ref
->type
!= REF_ARRAY
)
1977 if (ref
->u
.ar
.type
== AR_FULL
)
1984 if (ref
->u
.ar
.type
!= AR_SECTION
)
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
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. */
2002 for (n
= i
+ 1; n
< ref
->u
.ar
.dimen
; n
++)
2003 if (ref
->u
.ar
.dimen_type
[n
] != DIMEN_ELEMENT
)
2004 *contiguous
= false;
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
]))
2020 /* Check the lower bound. */
2021 if (ref
->u
.ar
.start
[i
]
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
])))
2028 /* Check the upper bound. */
2029 if (ref
->u
.ar
.end
[i
]
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
])))
2036 /* Check the stride. */
2037 if (ref
->u
.ar
.stride
[i
]
2038 && !gfc_expr_is_one (ref
->u
.ar
.stride
[i
], 0))
2041 /* This is unconditionally a contiguous reference as long as all
2042 the subsequent dimensions are elements. */
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
)
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. */
2063 ref_same_as_full_array (gfc_ref
*full_ref
, gfc_ref
*ref
)
2066 bool upper_or_lower
;
2068 if (full_ref
->type
!= REF_ARRAY
)
2070 if (full_ref
->u
.ar
.type
!= AR_FULL
)
2072 if (ref
->type
!= REF_ARRAY
)
2074 if (ref
->u
.ar
.type
== AR_FULL
)
2076 if (ref
->u
.ar
.type
!= AR_SECTION
)
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
]))
2097 /* Check the strides. */
2098 if (full_ref
->u
.ar
.stride
[i
] && !gfc_expr_is_one (full_ref
->u
.ar
.stride
[i
], 0))
2100 if (ref
->u
.ar
.stride
[i
] && !gfc_expr_is_one (ref
->u
.ar
.stride
[i
], 0))
2103 upper_or_lower
= false;
2104 /* Check the lower bound. */
2105 if (ref
->u
.ar
.start
[i
]
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
]
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
)
2125 /* Finds if two array references are overlapping or not.
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. */
2132 gfc_dep_resolver (gfc_ref
*lref
, gfc_ref
*rref
, gfc_reverse
*reverse
,
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
2152 if (lref
&& lref
->type
== REF_COMPONENT
&& lref
->u
.c
.component
2153 && strcmp (lref
->u
.c
.component
->name
, "_data") == 0)
2156 if (rref
&& rref
->type
== REF_COMPONENT
&& rref
->u
.c
.component
2157 && strcmp (rref
->u
.c
.component
->name
, "_data") == 0)
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
);
2167 /* The two ranges can't overlap if they are from different
2169 if (lref
->u
.c
.component
!= rref
->u
.c
.component
)
2172 same_component
= true;
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;
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
)))
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
;
2202 if (ref_same_as_full_array (lref
, rref
))
2205 if (ref_same_as_full_array (rref
, lref
))
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
2213 else if (rref
->u
.ar
.type
== AR_FULL
)
2214 fin_dep
= gfc_full_array_ref_p (lref
, NULL
) ? GFC_DEP_EQUAL
2221 /* Index for the reverse array. */
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
;
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
,
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
);
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
)
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
)
2278 if (rref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
2279 && lref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
)
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.*/
2320 if (identical
&& this_dep
== GFC_DEP_EQUAL
)
2321 this_dep
= GFC_DEP_OVERLAP
;
2323 if (this_dep
> fin_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
)
2334 /* Exactly matching and forward overlapping ranges don't cause a
2336 if (fin_dep
< GFC_DEP_BACKWARD
&& !identical
)
2339 /* Keep checking. We only have a dependency if
2340 subsequent references also overlap. */
2344 if (lref
->u
.i
!= rref
->u
.i
)
2356 /* Assume the worst if we nest to different depths. */
2360 /* This can result from concatenation of assumed length string components. */
2361 if (same_component
&& fin_dep
== GFC_DEP_ERROR
)
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
)
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. */
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
)
2395 if (lexpr
->symtree
->n
.sym
!= rexpr
->symtree
->n
.sym
)
2399 else if (lexpr
->base_expr
&& rexpr
->base_expr
)
2401 if (gfc_dep_compare_expr (lexpr
->base_expr
, rexpr
->base_expr
) != 0)
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)
2418 if (rref
&& rref
->type
== REF_COMPONENT
&& rref
->u
.c
.component
2419 && strcmp (rref
->u
.c
.component
->name
, "_data") == 0)
2422 gcc_assert (lref
->type
== rref
->type
);
2427 if (lref
->u
.c
.component
!= rref
->u
.c
.component
)
2432 if (ref_same_as_full_array (lref
, rref
))
2434 if (ref_same_as_full_array (rref
, lref
))
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
))
2442 if (rref
->u
.ar
.type
== AR_FULL
2443 && gfc_full_array_ref_p (lref
, NULL
))
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)
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
);
2474 fin_dep
= GFC_DEP_EQUAL
;
2480 if (n
+ 1 < lref
->u
.ar
.dimen
2481 && fin_dep
!= GFC_DEP_EQUAL
)
2485 if (fin_dep
!= GFC_DEP_EQUAL
2486 && fin_dep
!= GFC_DEP_OVERLAP
)
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
2509 dependency_fcn (gfc_expr
*e
, gfc_symbol
*sym
,
2510 int *f ATTRIBUTE_UNUSED
)
2515 if (e
&& e
->expr_type
== EXPR_VARIABLE
)
2517 if (e
->symtree
&& e
->symtree
->n
.sym
== sym
)
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
);
2532 gfc_function_dependency (gfc_symbol
*sym
, gfc_symbol
*proc_name
)
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
)
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
,
2553 sym
->fn_result_dep
= 1;
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
,
2562 sym
->fn_result_dep
= 1;
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
,
2575 sym
->fn_result_dep
= 1;