2 Copyright (C) 2000-2018 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.c -- Expression dependency analysis code. */
22 /* There's probably quite a bit of duplication in this file. We currently
23 have different dependency checking functions for different types
24 if dependencies. Ideally these would probably be merged. */
28 #include "coretypes.h"
30 #include "dependency.h"
31 #include "constructor.h"
34 /* static declarations */
36 enum range
{LHS
, RHS
, MID
};
38 /* Dependency types. These must be in reverse order of priority. */
42 GFC_DEP_EQUAL
, /* Identical Ranges. */
43 GFC_DEP_FORWARD
, /* e.g., a(1:3) = a(2:4). */
44 GFC_DEP_BACKWARD
, /* e.g. a(2:4) = a(1:3). */
45 GFC_DEP_OVERLAP
, /* May overlap in some other way. */
46 GFC_DEP_NODEP
/* Distinct ranges. */
50 #define IS_ARRAY_EXPLICIT(as) ((as->type == AS_EXPLICIT ? 1 : 0))
52 /* Forward declarations */
54 static gfc_dependency
check_section_vs_section (gfc_array_ref
*,
55 gfc_array_ref
*, int);
57 /* Returns 1 if the expr is an integer constant value 1, 0 if it is not or
58 def if the value could not be determined. */
61 gfc_expr_is_one (gfc_expr
*expr
, int def
)
63 gcc_assert (expr
!= NULL
);
65 if (expr
->expr_type
!= EXPR_CONSTANT
)
68 if (expr
->ts
.type
!= BT_INTEGER
)
71 return mpz_cmp_si (expr
->value
.integer
, 1) == 0;
74 /* Check if two array references are known to be identical. Calls
75 gfc_dep_compare_expr if necessary for comparing array indices. */
78 identical_array_ref (gfc_array_ref
*a1
, gfc_array_ref
*a2
)
82 if (a1
->type
== AR_FULL
&& a2
->type
== AR_FULL
)
85 if (a1
->type
== AR_SECTION
&& a2
->type
== AR_SECTION
)
87 gcc_assert (a1
->dimen
== a2
->dimen
);
89 for ( i
= 0; i
< a1
->dimen
; i
++)
91 /* TODO: Currently, we punt on an integer array as an index. */
92 if (a1
->dimen_type
[i
] != DIMEN_RANGE
93 || a2
->dimen_type
[i
] != DIMEN_RANGE
)
96 if (check_section_vs_section (a1
, a2
, i
) != GFC_DEP_EQUAL
)
102 if (a1
->type
== AR_ELEMENT
&& a2
->type
== AR_ELEMENT
)
104 if (a1
->dimen
!= a2
->dimen
)
105 gfc_internal_error ("identical_array_ref(): inconsistent dimensions");
107 for (i
= 0; i
< a1
->dimen
; i
++)
109 if (gfc_dep_compare_expr (a1
->start
[i
], a2
->start
[i
]) != 0)
119 /* Return true for identical variables, checking for references if
120 necessary. Calls identical_array_ref for checking array sections. */
123 are_identical_variables (gfc_expr
*e1
, gfc_expr
*e2
)
127 if (e1
->symtree
->n
.sym
->attr
.dummy
&& e2
->symtree
->n
.sym
->attr
.dummy
)
129 /* Dummy arguments: Only check for equal names. */
130 if (e1
->symtree
->n
.sym
->name
!= e2
->symtree
->n
.sym
->name
)
135 /* Check for equal symbols. */
136 if (e1
->symtree
->n
.sym
!= e2
->symtree
->n
.sym
)
140 /* Volatile variables should never compare equal to themselves. */
142 if (e1
->symtree
->n
.sym
->attr
.volatile_
)
148 while (r1
!= NULL
|| r2
!= NULL
)
151 /* Assume the variables are not equal if one has a reference and the
153 TODO: Handle full references like comparing a(:) to a.
156 if (r1
== NULL
|| r2
== NULL
)
159 if (r1
->type
!= r2
->type
)
166 if (!identical_array_ref (&r1
->u
.ar
, &r2
->u
.ar
))
172 if (r1
->u
.c
.component
!= r2
->u
.c
.component
)
177 if (gfc_dep_compare_expr (r1
->u
.ss
.start
, r2
->u
.ss
.start
) != 0)
180 /* If both are NULL, the end length compares equal, because we
181 are looking at the same variable. This can only happen for
182 assumed- or deferred-length character arguments. */
184 if (r1
->u
.ss
.end
== NULL
&& r2
->u
.ss
.end
== NULL
)
187 if (gfc_dep_compare_expr (r1
->u
.ss
.end
, r2
->u
.ss
.end
) != 0)
193 gfc_internal_error ("are_identical_variables: Bad type");
201 /* Compare two functions for equality. Returns 0 if e1==e2, -2 otherwise. If
202 impure_ok is false, only return 0 for pure functions. */
205 gfc_dep_compare_functions (gfc_expr
*e1
, gfc_expr
*e2
, bool impure_ok
)
208 gfc_actual_arglist
*args1
;
209 gfc_actual_arglist
*args2
;
211 if (e1
->expr_type
!= EXPR_FUNCTION
|| e2
->expr_type
!= EXPR_FUNCTION
)
214 if ((e1
->value
.function
.esym
&& e2
->value
.function
.esym
215 && e1
->value
.function
.esym
== e2
->value
.function
.esym
216 && (e1
->value
.function
.esym
->result
->attr
.pure
|| impure_ok
))
217 || (e1
->value
.function
.isym
&& e2
->value
.function
.isym
218 && e1
->value
.function
.isym
== e2
->value
.function
.isym
219 && (e1
->value
.function
.isym
->pure
|| impure_ok
)))
221 args1
= e1
->value
.function
.actual
;
222 args2
= e2
->value
.function
.actual
;
224 /* Compare the argument lists for equality. */
225 while (args1
&& args2
)
227 /* Bitwise xor, since C has no non-bitwise xor operator. */
228 if ((args1
->expr
== NULL
) ^ (args2
->expr
== NULL
))
231 if (args1
->expr
!= NULL
&& args2
->expr
!= NULL
)
237 if (gfc_dep_compare_expr (e1
, e2
) != 0)
240 /* Special case: String arguments which compare equal can have
241 different lengths, which makes them different in calls to
244 if (e1
->expr_type
== EXPR_CONSTANT
245 && e1
->ts
.type
== BT_CHARACTER
246 && e2
->expr_type
== EXPR_CONSTANT
247 && e2
->ts
.type
== BT_CHARACTER
248 && e1
->value
.character
.length
!= e2
->value
.character
.length
)
255 return (args1
|| args2
) ? -2 : 0;
261 /* Helper function to look through parens, unary plus and widening
262 integer conversions. */
265 gfc_discard_nops (gfc_expr
*e
)
267 gfc_actual_arglist
*arglist
;
274 if (e
->expr_type
== EXPR_OP
275 && (e
->value
.op
.op
== INTRINSIC_UPLUS
276 || e
->value
.op
.op
== INTRINSIC_PARENTHESES
))
282 if (e
->expr_type
== EXPR_FUNCTION
&& e
->value
.function
.isym
283 && e
->value
.function
.isym
->id
== GFC_ISYM_CONVERSION
284 && e
->ts
.type
== BT_INTEGER
)
286 arglist
= e
->value
.function
.actual
;
287 if (arglist
->expr
->ts
.type
== BT_INTEGER
288 && e
->ts
.kind
> arglist
->expr
->ts
.kind
)
301 /* Compare two expressions. Return values:
305 * -2 if the relationship could not be determined
306 * -3 if e1 /= e2, but we cannot tell which one is larger.
307 REAL and COMPLEX constants are only compared for equality
308 or inequality; if they are unequal, -2 is returned in all cases. */
311 gfc_dep_compare_expr (gfc_expr
*e1
, gfc_expr
*e2
)
315 if (e1
== NULL
&& e2
== NULL
)
318 e1
= gfc_discard_nops (e1
);
319 e2
= gfc_discard_nops (e2
);
321 if (e1
->expr_type
== EXPR_OP
&& e1
->value
.op
.op
== INTRINSIC_PLUS
)
323 /* Compare X+C vs. X, for INTEGER only. */
324 if (e1
->value
.op
.op2
->expr_type
== EXPR_CONSTANT
325 && e1
->value
.op
.op2
->ts
.type
== BT_INTEGER
326 && gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
) == 0)
327 return mpz_sgn (e1
->value
.op
.op2
->value
.integer
);
329 /* Compare P+Q vs. R+S. */
330 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_PLUS
)
334 l
= gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op1
);
335 r
= gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op2
);
336 if (l
== 0 && r
== 0)
338 if (l
== 0 && r
> -2)
340 if (l
> -2 && r
== 0)
342 if (l
== 1 && r
== 1)
344 if (l
== -1 && r
== -1)
347 l
= gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op2
);
348 r
= gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op1
);
349 if (l
== 0 && r
== 0)
351 if (l
== 0 && r
> -2)
353 if (l
> -2 && r
== 0)
355 if (l
== 1 && r
== 1)
357 if (l
== -1 && r
== -1)
362 /* Compare X vs. X+C, for INTEGER only. */
363 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_PLUS
)
365 if (e2
->value
.op
.op2
->expr_type
== EXPR_CONSTANT
366 && e2
->value
.op
.op2
->ts
.type
== BT_INTEGER
367 && gfc_dep_compare_expr (e1
, e2
->value
.op
.op1
) == 0)
368 return -mpz_sgn (e2
->value
.op
.op2
->value
.integer
);
371 /* Compare X-C vs. X, for INTEGER only. */
372 if (e1
->expr_type
== EXPR_OP
&& e1
->value
.op
.op
== INTRINSIC_MINUS
)
374 if (e1
->value
.op
.op2
->expr_type
== EXPR_CONSTANT
375 && e1
->value
.op
.op2
->ts
.type
== BT_INTEGER
376 && gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
) == 0)
377 return -mpz_sgn (e1
->value
.op
.op2
->value
.integer
);
379 /* Compare P-Q vs. R-S. */
380 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
384 l
= gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op1
);
385 r
= gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op2
);
386 if (l
== 0 && r
== 0)
388 if (l
> -2 && r
== 0)
390 if (l
== 0 && r
> -2)
392 if (l
== 1 && r
== -1)
394 if (l
== -1 && r
== 1)
399 /* Compare A // B vs. C // D. */
401 if (e1
->expr_type
== EXPR_OP
&& e1
->value
.op
.op
== INTRINSIC_CONCAT
402 && e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_CONCAT
)
406 l
= gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op1
);
407 r
= gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op2
);
412 /* Left expressions of // compare equal, but
413 watch out for 'A ' // x vs. 'A' // x. */
414 gfc_expr
*e1_left
= e1
->value
.op
.op1
;
415 gfc_expr
*e2_left
= e2
->value
.op
.op1
;
417 if (e1_left
->expr_type
== EXPR_CONSTANT
418 && e2_left
->expr_type
== EXPR_CONSTANT
419 && e1_left
->value
.character
.length
420 != e2_left
->value
.character
.length
)
426 /* Compare X vs. X-C, for INTEGER only. */
427 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
429 if (e2
->value
.op
.op2
->expr_type
== EXPR_CONSTANT
430 && e2
->value
.op
.op2
->ts
.type
== BT_INTEGER
431 && gfc_dep_compare_expr (e1
, e2
->value
.op
.op1
) == 0)
432 return mpz_sgn (e2
->value
.op
.op2
->value
.integer
);
435 if (e1
->expr_type
!= e2
->expr_type
)
438 switch (e1
->expr_type
)
441 /* Compare strings for equality. */
442 if (e1
->ts
.type
== BT_CHARACTER
&& e2
->ts
.type
== BT_CHARACTER
)
443 return gfc_compare_string (e1
, e2
);
445 /* Compare REAL and COMPLEX constants. Because of the
446 traps and pitfalls associated with comparing
447 a + 1.0 with a + 0.5, check for equality only. */
448 if (e2
->expr_type
== EXPR_CONSTANT
)
450 if (e1
->ts
.type
== BT_REAL
&& e2
->ts
.type
== BT_REAL
)
452 if (mpfr_cmp (e1
->value
.real
, e2
->value
.real
) == 0)
457 else if (e1
->ts
.type
== BT_COMPLEX
&& e2
->ts
.type
== BT_COMPLEX
)
459 if (mpc_cmp (e1
->value
.complex, e2
->value
.complex) == 0)
466 if (e1
->ts
.type
!= BT_INTEGER
|| e2
->ts
.type
!= BT_INTEGER
)
469 /* For INTEGER, all cases where e2 is not constant should have
470 been filtered out above. */
471 gcc_assert (e2
->expr_type
== EXPR_CONSTANT
);
473 i
= mpz_cmp (e1
->value
.integer
, e2
->value
.integer
);
481 if (are_identical_variables (e1
, e2
))
487 /* Intrinsic operators are the same if their operands are the same. */
488 if (e1
->value
.op
.op
!= e2
->value
.op
.op
)
490 if (e1
->value
.op
.op2
== 0)
492 i
= gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op1
);
493 return i
== 0 ? 0 : -2;
495 if (gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op1
) == 0
496 && gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op2
) == 0)
498 else if (e1
->value
.op
.op
== INTRINSIC_TIMES
499 && gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op2
) == 0
500 && gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op1
) == 0)
501 /* Commutativity of multiplication; addition is handled above. */
507 return gfc_dep_compare_functions (e1
, e2
, false);
515 /* Return the difference between two expressions. Integer expressions of
518 X + constant, X - constant and constant + X
520 are handled. Return true on success, false on failure. result is assumed
521 to be uninitialized on entry, and will be initialized on success.
525 gfc_dep_difference (gfc_expr
*e1
, gfc_expr
*e2
, mpz_t
*result
)
527 gfc_expr
*e1_op1
, *e1_op2
, *e2_op1
, *e2_op2
;
529 if (e1
== NULL
|| e2
== NULL
)
532 if (e1
->ts
.type
!= BT_INTEGER
|| e2
->ts
.type
!= BT_INTEGER
)
535 e1
= gfc_discard_nops (e1
);
536 e2
= gfc_discard_nops (e2
);
538 /* Inizialize tentatively, clear if we don't return anything. */
541 /* Case 1: c1 - c2 = c1 - c2, trivially. */
543 if (e1
->expr_type
== EXPR_CONSTANT
&& e2
->expr_type
== EXPR_CONSTANT
)
545 mpz_sub (*result
, e1
->value
.integer
, e2
->value
.integer
);
549 if (e1
->expr_type
== EXPR_OP
&& e1
->value
.op
.op
== INTRINSIC_PLUS
)
551 e1_op1
= gfc_discard_nops (e1
->value
.op
.op1
);
552 e1_op2
= gfc_discard_nops (e1
->value
.op
.op2
);
554 /* Case 2: (X + c1) - X = c1. */
555 if (e1_op2
->expr_type
== EXPR_CONSTANT
556 && gfc_dep_compare_expr (e1_op1
, e2
) == 0)
558 mpz_set (*result
, e1_op2
->value
.integer
);
562 /* Case 3: (c1 + X) - X = c1. */
563 if (e1_op1
->expr_type
== EXPR_CONSTANT
564 && gfc_dep_compare_expr (e1_op2
, e2
) == 0)
566 mpz_set (*result
, e1_op1
->value
.integer
);
570 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_PLUS
)
572 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
573 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
575 if (e1_op2
->expr_type
== EXPR_CONSTANT
)
577 /* Case 4: X + c1 - (X + c2) = c1 - c2. */
578 if (e2_op2
->expr_type
== EXPR_CONSTANT
579 && gfc_dep_compare_expr (e1_op1
, e2_op1
) == 0)
581 mpz_sub (*result
, e1_op2
->value
.integer
,
582 e2_op2
->value
.integer
);
585 /* Case 5: X + c1 - (c2 + X) = c1 - c2. */
586 if (e2_op1
->expr_type
== EXPR_CONSTANT
587 && gfc_dep_compare_expr (e1_op1
, e2_op2
) == 0)
589 mpz_sub (*result
, e1_op2
->value
.integer
,
590 e2_op1
->value
.integer
);
594 else if (e1_op1
->expr_type
== EXPR_CONSTANT
)
596 /* Case 6: c1 + X - (X + c2) = c1 - c2. */
597 if (e2_op2
->expr_type
== EXPR_CONSTANT
598 && gfc_dep_compare_expr (e1_op2
, e2_op1
) == 0)
600 mpz_sub (*result
, e1_op1
->value
.integer
,
601 e2_op2
->value
.integer
);
604 /* Case 7: c1 + X - (c2 + X) = c1 - c2. */
605 if (e2_op1
->expr_type
== EXPR_CONSTANT
606 && gfc_dep_compare_expr (e1_op2
, e2_op2
) == 0)
608 mpz_sub (*result
, e1_op1
->value
.integer
,
609 e2_op1
->value
.integer
);
615 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
617 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
618 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
620 if (e1_op2
->expr_type
== EXPR_CONSTANT
)
622 /* Case 8: X + c1 - (X - c2) = c1 + c2. */
623 if (e2_op2
->expr_type
== EXPR_CONSTANT
624 && gfc_dep_compare_expr (e1_op1
, e2_op1
) == 0)
626 mpz_add (*result
, e1_op2
->value
.integer
,
627 e2_op2
->value
.integer
);
631 if (e1_op1
->expr_type
== EXPR_CONSTANT
)
633 /* Case 9: c1 + X - (X - c2) = c1 + c2. */
634 if (e2_op2
->expr_type
== EXPR_CONSTANT
635 && gfc_dep_compare_expr (e1_op2
, e2_op1
) == 0)
637 mpz_add (*result
, e1_op1
->value
.integer
,
638 e2_op2
->value
.integer
);
645 if (e1
->expr_type
== EXPR_OP
&& e1
->value
.op
.op
== INTRINSIC_MINUS
)
647 e1_op1
= gfc_discard_nops (e1
->value
.op
.op1
);
648 e1_op2
= gfc_discard_nops (e1
->value
.op
.op2
);
650 if (e1_op2
->expr_type
== EXPR_CONSTANT
)
652 /* Case 10: (X - c1) - X = -c1 */
654 if (gfc_dep_compare_expr (e1_op1
, e2
) == 0)
656 mpz_neg (*result
, e1_op2
->value
.integer
);
660 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_PLUS
)
662 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
663 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
665 /* Case 11: (X - c1) - (X + c2) = -( c1 + c2). */
666 if (e2_op2
->expr_type
== EXPR_CONSTANT
667 && gfc_dep_compare_expr (e1_op1
, e2_op1
) == 0)
669 mpz_add (*result
, e1_op2
->value
.integer
,
670 e2_op2
->value
.integer
);
671 mpz_neg (*result
, *result
);
675 /* Case 12: X - c1 - (c2 + X) = - (c1 + c2). */
676 if (e2_op1
->expr_type
== EXPR_CONSTANT
677 && gfc_dep_compare_expr (e1_op1
, e2_op2
) == 0)
679 mpz_add (*result
, e1_op2
->value
.integer
,
680 e2_op1
->value
.integer
);
681 mpz_neg (*result
, *result
);
686 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
688 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
689 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
691 /* Case 13: (X - c1) - (X - c2) = c2 - c1. */
692 if (e2_op2
->expr_type
== EXPR_CONSTANT
693 && gfc_dep_compare_expr (e1_op1
, e2_op1
) == 0)
695 mpz_sub (*result
, e2_op2
->value
.integer
,
696 e1_op2
->value
.integer
);
701 if (e1_op1
->expr_type
== EXPR_CONSTANT
)
703 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
705 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
706 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
708 /* Case 14: (c1 - X) - (c2 - X) == c1 - c2. */
709 if (gfc_dep_compare_expr (e1_op2
, e2_op2
) == 0)
711 mpz_sub (*result
, e1_op1
->value
.integer
,
712 e2_op1
->value
.integer
);
720 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_PLUS
)
722 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
723 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
725 /* Case 15: X - (X + c2) = -c2. */
726 if (e2_op2
->expr_type
== EXPR_CONSTANT
727 && gfc_dep_compare_expr (e1
, e2_op1
) == 0)
729 mpz_neg (*result
, e2_op2
->value
.integer
);
732 /* Case 16: X - (c2 + X) = -c2. */
733 if (e2_op1
->expr_type
== EXPR_CONSTANT
734 && gfc_dep_compare_expr (e1
, e2_op2
) == 0)
736 mpz_neg (*result
, e2_op1
->value
.integer
);
741 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
743 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
744 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
746 /* Case 17: X - (X - c2) = c2. */
747 if (e2_op2
->expr_type
== EXPR_CONSTANT
748 && gfc_dep_compare_expr (e1
, e2_op1
) == 0)
750 mpz_set (*result
, e2_op2
->value
.integer
);
755 if (gfc_dep_compare_expr (e1
, e2
) == 0)
757 /* Case 18: X - X = 0. */
758 mpz_set_si (*result
, 0);
766 /* Returns 1 if the two ranges are the same and 0 if they are not (or if the
767 results are indeterminate). 'n' is the dimension to compare. */
770 is_same_range (gfc_array_ref
*ar1
, gfc_array_ref
*ar2
, int n
)
776 /* TODO: More sophisticated range comparison. */
777 gcc_assert (ar1
&& ar2
);
779 gcc_assert (ar1
->dimen_type
[n
] == ar2
->dimen_type
[n
]);
783 /* Check for mismatching strides. A NULL stride means a stride of 1. */
786 i
= gfc_expr_is_one (e1
, -1);
787 if (i
== -1 || i
== 0)
792 i
= gfc_expr_is_one (e2
, -1);
793 if (i
== -1 || i
== 0)
798 i
= gfc_dep_compare_expr (e1
, e2
);
802 /* The strides match. */
804 /* Check the range start. */
809 /* Use the bound of the array if no bound is specified. */
811 e1
= ar1
->as
->lower
[n
];
814 e2
= ar2
->as
->lower
[n
];
816 /* Check we have values for both. */
820 i
= gfc_dep_compare_expr (e1
, e2
);
825 /* Check the range end. */
830 /* Use the bound of the array if no bound is specified. */
832 e1
= ar1
->as
->upper
[n
];
835 e2
= ar2
->as
->upper
[n
];
837 /* Check we have values for both. */
841 i
= gfc_dep_compare_expr (e1
, e2
);
850 /* Some array-returning intrinsics can be implemented by reusing the
851 data from one of the array arguments. For example, TRANSPOSE does
852 not necessarily need to allocate new data: it can be implemented
853 by copying the original array's descriptor and simply swapping the
854 two dimension specifications.
856 If EXPR is a call to such an intrinsic, return the argument
857 whose data can be reused, otherwise return NULL. */
860 gfc_get_noncopying_intrinsic_argument (gfc_expr
*expr
)
862 if (expr
->expr_type
!= EXPR_FUNCTION
|| !expr
->value
.function
.isym
)
865 switch (expr
->value
.function
.isym
->id
)
867 case GFC_ISYM_TRANSPOSE
:
868 return expr
->value
.function
.actual
->expr
;
876 /* Return true if the result of reference REF can only be constructed
877 using a temporary array. */
880 gfc_ref_needs_temporary_p (gfc_ref
*ref
)
886 for (; ref
; ref
= ref
->next
)
890 /* Vector dimensions are generally not monotonic and must be
891 handled using a temporary. */
892 if (ref
->u
.ar
.type
== AR_SECTION
)
893 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
894 if (ref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
)
901 /* Within an array reference, character substrings generally
902 need a temporary. Character array strides are expressed as
903 multiples of the element size (consistent with other array
904 types), not in characters. */
916 gfc_is_data_pointer (gfc_expr
*e
)
920 if (e
->expr_type
!= EXPR_VARIABLE
&& e
->expr_type
!= EXPR_FUNCTION
)
923 /* No subreference if it is a function */
924 gcc_assert (e
->expr_type
== EXPR_VARIABLE
|| !e
->ref
);
926 if (e
->symtree
->n
.sym
->attr
.pointer
)
929 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
930 if (ref
->type
== REF_COMPONENT
&& ref
->u
.c
.component
->attr
.pointer
)
937 /* Return true if array variable VAR could be passed to the same function
938 as argument EXPR without interfering with EXPR. INTENT is the intent
941 This is considerably less conservative than other dependencies
942 because many function arguments will already be copied into a
946 gfc_check_argument_var_dependency (gfc_expr
*var
, sym_intent intent
,
947 gfc_expr
*expr
, gfc_dep_check elemental
)
951 gcc_assert (var
->expr_type
== EXPR_VARIABLE
);
952 gcc_assert (var
->rank
> 0);
954 switch (expr
->expr_type
)
957 /* In case of elemental subroutines, there is no dependency
958 between two same-range array references. */
959 if (gfc_ref_needs_temporary_p (expr
->ref
)
960 || gfc_check_dependency (var
, expr
, elemental
== NOT_ELEMENTAL
))
962 if (elemental
== ELEM_DONT_CHECK_VARIABLE
)
964 /* Too many false positive with pointers. */
965 if (!gfc_is_data_pointer (var
) && !gfc_is_data_pointer (expr
))
967 /* Elemental procedures forbid unspecified intents,
968 and we don't check dependencies for INTENT_IN args. */
969 gcc_assert (intent
== INTENT_OUT
|| intent
== INTENT_INOUT
);
971 /* We are told not to check dependencies.
972 We do it, however, and issue a warning in case we find one.
973 If a dependency is found in the case
974 elemental == ELEM_CHECK_VARIABLE, we will generate
975 a temporary, so we don't need to bother the user. */
976 gfc_warning (0, "INTENT(%s) actual argument at %L might "
977 "interfere with actual argument at %L.",
978 intent
== INTENT_OUT
? "OUT" : "INOUT",
979 &var
->where
, &expr
->where
);
989 /* the scalarizer always generates a temporary for array constructors,
990 so there is no dependency. */
994 if (intent
!= INTENT_IN
)
996 arg
= gfc_get_noncopying_intrinsic_argument (expr
);
998 return gfc_check_argument_var_dependency (var
, intent
, arg
,
1002 if (elemental
!= NOT_ELEMENTAL
)
1004 if ((expr
->value
.function
.esym
1005 && expr
->value
.function
.esym
->attr
.elemental
)
1006 || (expr
->value
.function
.isym
1007 && expr
->value
.function
.isym
->elemental
))
1008 return gfc_check_fncall_dependency (var
, intent
, NULL
,
1009 expr
->value
.function
.actual
,
1010 ELEM_CHECK_VARIABLE
);
1012 if (gfc_inline_intrinsic_function_p (expr
))
1014 /* The TRANSPOSE case should have been caught in the
1015 noncopying intrinsic case above. */
1016 gcc_assert (expr
->value
.function
.isym
->id
!= GFC_ISYM_TRANSPOSE
);
1018 return gfc_check_fncall_dependency (var
, intent
, NULL
,
1019 expr
->value
.function
.actual
,
1020 ELEM_CHECK_VARIABLE
);
1026 /* In case of non-elemental procedures, there is no need to catch
1027 dependencies, as we will make a temporary anyway. */
1030 /* If the actual arg EXPR is an expression, we need to catch
1031 a dependency between variables in EXPR and VAR,
1032 an intent((IN)OUT) variable. */
1033 if (expr
->value
.op
.op1
1034 && gfc_check_argument_var_dependency (var
, intent
,
1036 ELEM_CHECK_VARIABLE
))
1038 else if (expr
->value
.op
.op2
1039 && gfc_check_argument_var_dependency (var
, intent
,
1041 ELEM_CHECK_VARIABLE
))
1052 /* Like gfc_check_argument_var_dependency, but extended to any
1053 array expression OTHER, not just variables. */
1056 gfc_check_argument_dependency (gfc_expr
*other
, sym_intent intent
,
1057 gfc_expr
*expr
, gfc_dep_check elemental
)
1059 switch (other
->expr_type
)
1062 return gfc_check_argument_var_dependency (other
, intent
, expr
, elemental
);
1065 other
= gfc_get_noncopying_intrinsic_argument (other
);
1067 return gfc_check_argument_dependency (other
, INTENT_IN
, expr
,
1078 /* Like gfc_check_argument_dependency, but check all the arguments in ACTUAL.
1079 FNSYM is the function being called, or NULL if not known. */
1082 gfc_check_fncall_dependency (gfc_expr
*other
, sym_intent intent
,
1083 gfc_symbol
*fnsym
, gfc_actual_arglist
*actual
,
1084 gfc_dep_check elemental
)
1086 gfc_formal_arglist
*formal
;
1089 formal
= fnsym
? gfc_sym_get_dummy_args (fnsym
) : NULL
;
1090 for (; actual
; actual
= actual
->next
, formal
= formal
? formal
->next
: NULL
)
1092 expr
= actual
->expr
;
1094 /* Skip args which are not present. */
1098 /* Skip other itself. */
1102 /* Skip intent(in) arguments if OTHER itself is intent(in). */
1103 if (formal
&& intent
== INTENT_IN
1104 && formal
->sym
->attr
.intent
== INTENT_IN
)
1107 if (gfc_check_argument_dependency (other
, intent
, expr
, elemental
))
1115 /* Return 1 if e1 and e2 are equivalenced arrays, either
1116 directly or indirectly; i.e., equivalence (a,b) for a and b
1117 or equivalence (a,c),(b,c). This function uses the equiv_
1118 lists, generated in trans-common(add_equivalences), that are
1119 guaranteed to pick up indirect equivalences. We explicitly
1120 check for overlap using the offset and length of the equivalence.
1121 This function is symmetric.
1122 TODO: This function only checks whether the full top-level
1123 symbols overlap. An improved implementation could inspect
1124 e1->ref and e2->ref to determine whether the actually accessed
1125 portions of these variables/arrays potentially overlap. */
1128 gfc_are_equivalenced_arrays (gfc_expr
*e1
, gfc_expr
*e2
)
1131 gfc_equiv_info
*s
, *fl1
, *fl2
;
1133 gcc_assert (e1
->expr_type
== EXPR_VARIABLE
1134 && e2
->expr_type
== EXPR_VARIABLE
);
1136 if (!e1
->symtree
->n
.sym
->attr
.in_equivalence
1137 || !e2
->symtree
->n
.sym
->attr
.in_equivalence
|| !e1
->rank
|| !e2
->rank
)
1140 if (e1
->symtree
->n
.sym
->ns
1141 && e1
->symtree
->n
.sym
->ns
!= gfc_current_ns
)
1142 l
= e1
->symtree
->n
.sym
->ns
->equiv_lists
;
1144 l
= gfc_current_ns
->equiv_lists
;
1146 /* Go through the equiv_lists and return 1 if the variables
1147 e1 and e2 are members of the same group and satisfy the
1148 requirement on their relative offsets. */
1149 for (; l
; l
= l
->next
)
1153 for (s
= l
->equiv
; s
; s
= s
->next
)
1155 if (s
->sym
== e1
->symtree
->n
.sym
)
1161 if (s
->sym
== e2
->symtree
->n
.sym
)
1171 /* Can these lengths be zero? */
1172 if (fl1
->length
<= 0 || fl2
->length
<= 0)
1174 /* These can't overlap if [f11,fl1+length] is before
1175 [fl2,fl2+length], or [fl2,fl2+length] is before
1176 [fl1,fl1+length], otherwise they do overlap. */
1177 if (fl1
->offset
+ fl1
->length
> fl2
->offset
1178 && fl2
->offset
+ fl2
->length
> fl1
->offset
)
1186 /* Return true if there is no possibility of aliasing because of a type
1187 mismatch between all the possible pointer references and the
1188 potential target. Note that this function is asymmetric in the
1189 arguments and so must be called twice with the arguments exchanged. */
1192 check_data_pointer_types (gfc_expr
*expr1
, gfc_expr
*expr2
)
1198 bool seen_component_ref
;
1200 if (expr1
->expr_type
!= EXPR_VARIABLE
1201 || expr2
->expr_type
!= EXPR_VARIABLE
)
1204 sym1
= expr1
->symtree
->n
.sym
;
1205 sym2
= expr2
->symtree
->n
.sym
;
1207 /* Keep it simple for now. */
1208 if (sym1
->ts
.type
== BT_DERIVED
&& sym2
->ts
.type
== BT_DERIVED
)
1211 if (sym1
->attr
.pointer
)
1213 if (gfc_compare_types (&sym1
->ts
, &sym2
->ts
))
1217 /* This is a conservative check on the components of the derived type
1218 if no component references have been seen. Since we will not dig
1219 into the components of derived type components, we play it safe by
1220 returning false. First we check the reference chain and then, if
1221 no component references have been seen, the components. */
1222 seen_component_ref
= false;
1223 if (sym1
->ts
.type
== BT_DERIVED
)
1225 for (ref1
= expr1
->ref
; ref1
; ref1
= ref1
->next
)
1227 if (ref1
->type
!= REF_COMPONENT
)
1230 if (ref1
->u
.c
.component
->ts
.type
== BT_DERIVED
)
1233 if ((sym2
->attr
.pointer
|| ref1
->u
.c
.component
->attr
.pointer
)
1234 && gfc_compare_types (&ref1
->u
.c
.component
->ts
, &sym2
->ts
))
1237 seen_component_ref
= true;
1241 if (sym1
->ts
.type
== BT_DERIVED
&& !seen_component_ref
)
1243 for (cm1
= sym1
->ts
.u
.derived
->components
; cm1
; cm1
= cm1
->next
)
1245 if (cm1
->ts
.type
== BT_DERIVED
)
1248 if ((sym2
->attr
.pointer
|| cm1
->attr
.pointer
)
1249 && gfc_compare_types (&cm1
->ts
, &sym2
->ts
))
1258 /* Return true if the statement body redefines the condition. Returns
1259 true if expr2 depends on expr1. expr1 should be a single term
1260 suitable for the lhs of an assignment. The IDENTICAL flag indicates
1261 whether array references to the same symbol with identical range
1262 references count as a dependency or not. Used for forall and where
1263 statements. Also used with functions returning arrays without a
1267 gfc_check_dependency (gfc_expr
*expr1
, gfc_expr
*expr2
, bool identical
)
1269 gfc_actual_arglist
*actual
;
1273 /* -fcoarray=lib can end up here with expr1->expr_type set to EXPR_FUNCTION
1274 and a reference to _F.caf_get, so skip the assert. */
1275 if (expr1
->expr_type
== EXPR_FUNCTION
1276 && strcmp (expr1
->value
.function
.name
, "_F.caf_get") == 0)
1279 if (expr1
->expr_type
!= EXPR_VARIABLE
)
1280 gfc_internal_error ("gfc_check_dependency: expecting an EXPR_VARIABLE");
1282 switch (expr2
->expr_type
)
1285 n
= gfc_check_dependency (expr1
, expr2
->value
.op
.op1
, identical
);
1288 if (expr2
->value
.op
.op2
)
1289 return gfc_check_dependency (expr1
, expr2
->value
.op
.op2
, identical
);
1293 /* The interesting cases are when the symbols don't match. */
1294 if (expr1
->symtree
->n
.sym
!= expr2
->symtree
->n
.sym
)
1296 symbol_attribute attr1
, attr2
;
1297 gfc_typespec
*ts1
= &expr1
->symtree
->n
.sym
->ts
;
1298 gfc_typespec
*ts2
= &expr2
->symtree
->n
.sym
->ts
;
1300 /* Return 1 if expr1 and expr2 are equivalenced arrays. */
1301 if (gfc_are_equivalenced_arrays (expr1
, expr2
))
1304 /* Symbols can only alias if they have the same type. */
1305 if (ts1
->type
!= BT_UNKNOWN
&& ts2
->type
!= BT_UNKNOWN
1306 && ts1
->type
!= BT_DERIVED
&& ts2
->type
!= BT_DERIVED
)
1308 if (ts1
->type
!= ts2
->type
|| ts1
->kind
!= ts2
->kind
)
1312 /* We have to also include target-target as ptr%comp is not a
1313 pointer but it still alias with "dt%comp" for "ptr => dt". As
1314 subcomponents and array access to pointers retains the target
1315 attribute, that's sufficient. */
1316 attr1
= gfc_expr_attr (expr1
);
1317 attr2
= gfc_expr_attr (expr2
);
1318 if ((attr1
.pointer
|| attr1
.target
) && (attr2
.pointer
|| attr2
.target
))
1320 if (check_data_pointer_types (expr1
, expr2
)
1321 && check_data_pointer_types (expr2
, expr1
))
1328 gfc_symbol
*sym1
= expr1
->symtree
->n
.sym
;
1329 gfc_symbol
*sym2
= expr2
->symtree
->n
.sym
;
1330 if (sym1
->attr
.target
&& sym2
->attr
.target
1331 && ((sym1
->attr
.dummy
&& !sym1
->attr
.contiguous
1332 && (!sym1
->attr
.dimension
1333 || sym2
->as
->type
== AS_ASSUMED_SHAPE
))
1334 || (sym2
->attr
.dummy
&& !sym2
->attr
.contiguous
1335 && (!sym2
->attr
.dimension
1336 || sym2
->as
->type
== AS_ASSUMED_SHAPE
))))
1340 /* Otherwise distinct symbols have no dependencies. */
1347 /* Identical and disjoint ranges return 0,
1348 overlapping ranges return 1. */
1349 if (expr1
->ref
&& expr2
->ref
)
1350 return gfc_dep_resolver (expr1
->ref
, expr2
->ref
, NULL
);
1355 if (gfc_get_noncopying_intrinsic_argument (expr2
) != NULL
)
1358 /* Remember possible differences between elemental and
1359 transformational functions. All functions inside a FORALL
1361 for (actual
= expr2
->value
.function
.actual
;
1362 actual
; actual
= actual
->next
)
1366 n
= gfc_check_dependency (expr1
, actual
->expr
, identical
);
1377 /* Loop through the array constructor's elements. */
1378 for (c
= gfc_constructor_first (expr2
->value
.constructor
);
1379 c
; c
= gfc_constructor_next (c
))
1381 /* If this is an iterator, assume the worst. */
1384 /* Avoid recursion in the common case. */
1385 if (c
->expr
->expr_type
== EXPR_CONSTANT
)
1387 if (gfc_check_dependency (expr1
, c
->expr
, 1))
1398 /* Determines overlapping for two array sections. */
1400 static gfc_dependency
1401 check_section_vs_section (gfc_array_ref
*l_ar
, gfc_array_ref
*r_ar
, int n
)
1417 int stride_comparison
;
1418 int start_comparison
;
1421 /* If they are the same range, return without more ado. */
1422 if (is_same_range (l_ar
, r_ar
, n
))
1423 return GFC_DEP_EQUAL
;
1425 l_start
= l_ar
->start
[n
];
1426 l_end
= l_ar
->end
[n
];
1427 l_stride
= l_ar
->stride
[n
];
1429 r_start
= r_ar
->start
[n
];
1430 r_end
= r_ar
->end
[n
];
1431 r_stride
= r_ar
->stride
[n
];
1433 /* If l_start is NULL take it from array specifier. */
1434 if (l_start
== NULL
&& IS_ARRAY_EXPLICIT (l_ar
->as
))
1435 l_start
= l_ar
->as
->lower
[n
];
1436 /* If l_end is NULL take it from array specifier. */
1437 if (l_end
== NULL
&& IS_ARRAY_EXPLICIT (l_ar
->as
))
1438 l_end
= l_ar
->as
->upper
[n
];
1440 /* If r_start is NULL take it from array specifier. */
1441 if (r_start
== NULL
&& IS_ARRAY_EXPLICIT (r_ar
->as
))
1442 r_start
= r_ar
->as
->lower
[n
];
1443 /* If r_end is NULL take it from array specifier. */
1444 if (r_end
== NULL
&& IS_ARRAY_EXPLICIT (r_ar
->as
))
1445 r_end
= r_ar
->as
->upper
[n
];
1447 /* Determine whether the l_stride is positive or negative. */
1450 else if (l_stride
->expr_type
== EXPR_CONSTANT
1451 && l_stride
->ts
.type
== BT_INTEGER
)
1452 l_dir
= mpz_sgn (l_stride
->value
.integer
);
1453 else if (l_start
&& l_end
)
1454 l_dir
= gfc_dep_compare_expr (l_end
, l_start
);
1458 /* Determine whether the r_stride is positive or negative. */
1461 else if (r_stride
->expr_type
== EXPR_CONSTANT
1462 && r_stride
->ts
.type
== BT_INTEGER
)
1463 r_dir
= mpz_sgn (r_stride
->value
.integer
);
1464 else if (r_start
&& r_end
)
1465 r_dir
= gfc_dep_compare_expr (r_end
, r_start
);
1469 /* The strides should never be zero. */
1470 if (l_dir
== 0 || r_dir
== 0)
1471 return GFC_DEP_OVERLAP
;
1473 /* Determine the relationship between the strides. Set stride_comparison to
1474 -2 if the dependency cannot be determined
1475 -1 if l_stride < r_stride
1476 0 if l_stride == r_stride
1477 1 if l_stride > r_stride
1478 as determined by gfc_dep_compare_expr. */
1480 one_expr
= gfc_get_int_expr (gfc_index_integer_kind
, NULL
, 1);
1482 stride_comparison
= gfc_dep_compare_expr (l_stride
? l_stride
: one_expr
,
1483 r_stride
? r_stride
: one_expr
);
1485 if (l_start
&& r_start
)
1486 start_comparison
= gfc_dep_compare_expr (l_start
, r_start
);
1488 start_comparison
= -2;
1490 gfc_free_expr (one_expr
);
1492 /* Determine LHS upper and lower bounds. */
1498 else if (l_dir
== -1)
1509 /* Determine RHS upper and lower bounds. */
1515 else if (r_dir
== -1)
1526 /* Check whether the ranges are disjoint. */
1527 if (l_upper
&& r_lower
&& gfc_dep_compare_expr (l_upper
, r_lower
) == -1)
1528 return GFC_DEP_NODEP
;
1529 if (r_upper
&& l_lower
&& gfc_dep_compare_expr (r_upper
, l_lower
) == -1)
1530 return GFC_DEP_NODEP
;
1532 /* Handle cases like x:y:1 vs. x:z:-1 as GFC_DEP_EQUAL. */
1533 if (l_start
&& r_start
&& gfc_dep_compare_expr (l_start
, r_start
) == 0)
1535 if (l_dir
== 1 && r_dir
== -1)
1536 return GFC_DEP_EQUAL
;
1537 if (l_dir
== -1 && r_dir
== 1)
1538 return GFC_DEP_EQUAL
;
1541 /* Handle cases like x:y:1 vs. z:y:-1 as GFC_DEP_EQUAL. */
1542 if (l_end
&& r_end
&& gfc_dep_compare_expr (l_end
, r_end
) == 0)
1544 if (l_dir
== 1 && r_dir
== -1)
1545 return GFC_DEP_EQUAL
;
1546 if (l_dir
== -1 && r_dir
== 1)
1547 return GFC_DEP_EQUAL
;
1550 /* Handle cases like x:y:2 vs. x+1:z:4 as GFC_DEP_NODEP.
1551 There is no dependency if the remainder of
1552 (l_start - r_start) / gcd(l_stride, r_stride) is
1555 - Cases like a(1:4:2) = a(2:3) are still not handled.
1558 #define IS_CONSTANT_INTEGER(a) ((a) && ((a)->expr_type == EXPR_CONSTANT) \
1559 && (a)->ts.type == BT_INTEGER)
1561 if (IS_CONSTANT_INTEGER (l_stride
) && IS_CONSTANT_INTEGER (r_stride
)
1562 && gfc_dep_difference (l_start
, r_start
, &tmp
))
1568 mpz_gcd (gcd
, l_stride
->value
.integer
, r_stride
->value
.integer
);
1570 mpz_fdiv_r (tmp
, tmp
, gcd
);
1571 result
= mpz_cmp_si (tmp
, 0L);
1577 return GFC_DEP_NODEP
;
1580 #undef IS_CONSTANT_INTEGER
1582 /* Check for forward dependencies x:y vs. x+1:z and x:y:z vs. x:y:z+1. */
1584 if (l_dir
== 1 && r_dir
== 1 &&
1585 (start_comparison
== 0 || start_comparison
== -1)
1586 && (stride_comparison
== 0 || stride_comparison
== -1))
1587 return GFC_DEP_FORWARD
;
1589 /* Check for forward dependencies x:y:-1 vs. x-1:z:-1 and
1590 x:y:-1 vs. x:y:-2. */
1591 if (l_dir
== -1 && r_dir
== -1 &&
1592 (start_comparison
== 0 || start_comparison
== 1)
1593 && (stride_comparison
== 0 || stride_comparison
== 1))
1594 return GFC_DEP_FORWARD
;
1596 if (stride_comparison
== 0 || stride_comparison
== -1)
1598 if (l_start
&& IS_ARRAY_EXPLICIT (l_ar
->as
))
1601 /* Check for a(low:y:s) vs. a(z:x:s) or
1602 a(low:y:s) vs. a(z:x:s+1) where a has a lower bound
1603 of low, which is always at least a forward dependence. */
1606 && gfc_dep_compare_expr (l_start
, l_ar
->as
->lower
[n
]) == 0)
1607 return GFC_DEP_FORWARD
;
1611 if (stride_comparison
== 0 || stride_comparison
== 1)
1613 if (l_start
&& IS_ARRAY_EXPLICIT (l_ar
->as
))
1616 /* Check for a(high:y:-s) vs. a(z:x:-s) or
1617 a(high:y:-s vs. a(z:x:-s-1) where a has a higher bound
1618 of high, which is always at least a forward dependence. */
1621 && gfc_dep_compare_expr (l_start
, l_ar
->as
->upper
[n
]) == 0)
1622 return GFC_DEP_FORWARD
;
1627 if (stride_comparison
== 0)
1629 /* From here, check for backwards dependencies. */
1630 /* x+1:y vs. x:z. */
1631 if (l_dir
== 1 && r_dir
== 1 && start_comparison
== 1)
1632 return GFC_DEP_BACKWARD
;
1634 /* x-1:y:-1 vs. x:z:-1. */
1635 if (l_dir
== -1 && r_dir
== -1 && start_comparison
== -1)
1636 return GFC_DEP_BACKWARD
;
1639 return GFC_DEP_OVERLAP
;
1643 /* Determines overlapping for a single element and a section. */
1645 static gfc_dependency
1646 gfc_check_element_vs_section( gfc_ref
*lref
, gfc_ref
*rref
, int n
)
1655 elem
= lref
->u
.ar
.start
[n
];
1657 return GFC_DEP_OVERLAP
;
1660 start
= ref
->start
[n
] ;
1662 stride
= ref
->stride
[n
];
1664 if (!start
&& IS_ARRAY_EXPLICIT (ref
->as
))
1665 start
= ref
->as
->lower
[n
];
1666 if (!end
&& IS_ARRAY_EXPLICIT (ref
->as
))
1667 end
= ref
->as
->upper
[n
];
1669 /* Determine whether the stride is positive or negative. */
1672 else if (stride
->expr_type
== EXPR_CONSTANT
1673 && stride
->ts
.type
== BT_INTEGER
)
1674 s
= mpz_sgn (stride
->value
.integer
);
1678 /* Stride should never be zero. */
1680 return GFC_DEP_OVERLAP
;
1682 /* Positive strides. */
1685 /* Check for elem < lower. */
1686 if (start
&& gfc_dep_compare_expr (elem
, start
) == -1)
1687 return GFC_DEP_NODEP
;
1688 /* Check for elem > upper. */
1689 if (end
&& gfc_dep_compare_expr (elem
, end
) == 1)
1690 return GFC_DEP_NODEP
;
1694 s
= gfc_dep_compare_expr (start
, end
);
1695 /* Check for an empty range. */
1697 return GFC_DEP_NODEP
;
1698 if (s
== 0 && gfc_dep_compare_expr (elem
, start
) == 0)
1699 return GFC_DEP_EQUAL
;
1702 /* Negative strides. */
1705 /* Check for elem > upper. */
1706 if (end
&& gfc_dep_compare_expr (elem
, start
) == 1)
1707 return GFC_DEP_NODEP
;
1708 /* Check for elem < lower. */
1709 if (start
&& gfc_dep_compare_expr (elem
, end
) == -1)
1710 return GFC_DEP_NODEP
;
1714 s
= gfc_dep_compare_expr (start
, end
);
1715 /* Check for an empty range. */
1717 return GFC_DEP_NODEP
;
1718 if (s
== 0 && gfc_dep_compare_expr (elem
, start
) == 0)
1719 return GFC_DEP_EQUAL
;
1722 /* Unknown strides. */
1726 return GFC_DEP_OVERLAP
;
1727 s
= gfc_dep_compare_expr (start
, end
);
1729 return GFC_DEP_OVERLAP
;
1730 /* Assume positive stride. */
1733 /* Check for elem < lower. */
1734 if (gfc_dep_compare_expr (elem
, start
) == -1)
1735 return GFC_DEP_NODEP
;
1736 /* Check for elem > upper. */
1737 if (gfc_dep_compare_expr (elem
, end
) == 1)
1738 return GFC_DEP_NODEP
;
1740 /* Assume negative stride. */
1743 /* Check for elem > upper. */
1744 if (gfc_dep_compare_expr (elem
, start
) == 1)
1745 return GFC_DEP_NODEP
;
1746 /* Check for elem < lower. */
1747 if (gfc_dep_compare_expr (elem
, end
) == -1)
1748 return GFC_DEP_NODEP
;
1753 s
= gfc_dep_compare_expr (elem
, start
);
1755 return GFC_DEP_EQUAL
;
1756 if (s
== 1 || s
== -1)
1757 return GFC_DEP_NODEP
;
1761 return GFC_DEP_OVERLAP
;
1765 /* Traverse expr, checking all EXPR_VARIABLE symbols for their
1766 forall_index attribute. Return true if any variable may be
1767 being used as a FORALL index. Its safe to pessimistically
1768 return true, and assume a dependency. */
1771 contains_forall_index_p (gfc_expr
*expr
)
1773 gfc_actual_arglist
*arg
;
1781 switch (expr
->expr_type
)
1784 if (expr
->symtree
->n
.sym
->forall_index
)
1789 if (contains_forall_index_p (expr
->value
.op
.op1
)
1790 || contains_forall_index_p (expr
->value
.op
.op2
))
1795 for (arg
= expr
->value
.function
.actual
; arg
; arg
= arg
->next
)
1796 if (contains_forall_index_p (arg
->expr
))
1802 case EXPR_SUBSTRING
:
1805 case EXPR_STRUCTURE
:
1807 for (c
= gfc_constructor_first (expr
->value
.constructor
);
1808 c
; gfc_constructor_next (c
))
1809 if (contains_forall_index_p (c
->expr
))
1817 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
1821 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
1822 if (contains_forall_index_p (ref
->u
.ar
.start
[i
])
1823 || contains_forall_index_p (ref
->u
.ar
.end
[i
])
1824 || contains_forall_index_p (ref
->u
.ar
.stride
[i
]))
1832 if (contains_forall_index_p (ref
->u
.ss
.start
)
1833 || contains_forall_index_p (ref
->u
.ss
.end
))
1844 /* Determines overlapping for two single element array references. */
1846 static gfc_dependency
1847 gfc_check_element_vs_element (gfc_ref
*lref
, gfc_ref
*rref
, int n
)
1857 l_start
= l_ar
.start
[n
] ;
1858 r_start
= r_ar
.start
[n
] ;
1859 i
= gfc_dep_compare_expr (r_start
, l_start
);
1861 return GFC_DEP_EQUAL
;
1863 /* Treat two scalar variables as potentially equal. This allows
1864 us to prove that a(i,:) and a(j,:) have no dependency. See
1865 Gerald Roth, "Evaluation of Array Syntax Dependence Analysis",
1866 Proceedings of the International Conference on Parallel and
1867 Distributed Processing Techniques and Applications (PDPTA2001),
1868 Las Vegas, Nevada, June 2001. */
1869 /* However, we need to be careful when either scalar expression
1870 contains a FORALL index, as these can potentially change value
1871 during the scalarization/traversal of this array reference. */
1872 if (contains_forall_index_p (r_start
) || contains_forall_index_p (l_start
))
1873 return GFC_DEP_OVERLAP
;
1876 return GFC_DEP_NODEP
;
1877 return GFC_DEP_EQUAL
;
1880 /* Callback function for checking if an expression depends on a
1881 dummy variable which is any other than INTENT(IN). */
1884 callback_dummy_intent_not_in (gfc_expr
**ep
,
1885 int *walk_subtrees ATTRIBUTE_UNUSED
,
1886 void *data ATTRIBUTE_UNUSED
)
1890 if (e
->expr_type
== EXPR_VARIABLE
&& e
->symtree
1891 && e
->symtree
->n
.sym
->attr
.dummy
)
1892 return e
->symtree
->n
.sym
->attr
.intent
!= INTENT_IN
;
1897 /* Auxiliary function to check if subexpressions have dummy variables which
1902 dummy_intent_not_in (gfc_expr
**ep
)
1904 return gfc_expr_walker (ep
, callback_dummy_intent_not_in
, NULL
);
1907 /* Determine if an array ref, usually an array section specifies the
1908 entire array. In addition, if the second, pointer argument is
1909 provided, the function will return true if the reference is
1910 contiguous; eg. (:, 1) gives true but (1,:) gives false.
1911 If one of the bounds depends on a dummy variable which is
1912 not INTENT(IN), also return false, because the user may
1913 have changed the variable. */
1916 gfc_full_array_ref_p (gfc_ref
*ref
, bool *contiguous
)
1920 bool lbound_OK
= true;
1921 bool ubound_OK
= true;
1924 *contiguous
= false;
1926 if (ref
->type
!= REF_ARRAY
)
1929 if (ref
->u
.ar
.type
== AR_FULL
)
1936 if (ref
->u
.ar
.type
!= AR_SECTION
)
1941 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
1943 /* If we have a single element in the reference, for the reference
1944 to be full, we need to ascertain that the array has a single
1945 element in this dimension and that we actually reference the
1947 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_ELEMENT
)
1949 /* This is unconditionally a contiguous reference if all the
1950 remaining dimensions are elements. */
1954 for (n
= i
+ 1; n
< ref
->u
.ar
.dimen
; n
++)
1955 if (ref
->u
.ar
.dimen_type
[n
] != DIMEN_ELEMENT
)
1956 *contiguous
= false;
1960 || !ref
->u
.ar
.as
->lower
[i
]
1961 || !ref
->u
.ar
.as
->upper
[i
]
1962 || gfc_dep_compare_expr (ref
->u
.ar
.as
->lower
[i
],
1963 ref
->u
.ar
.as
->upper
[i
])
1964 || !ref
->u
.ar
.start
[i
]
1965 || gfc_dep_compare_expr (ref
->u
.ar
.start
[i
],
1966 ref
->u
.ar
.as
->lower
[i
]))
1972 /* Check the lower bound. */
1973 if (ref
->u
.ar
.start
[i
]
1975 || !ref
->u
.ar
.as
->lower
[i
]
1976 || gfc_dep_compare_expr (ref
->u
.ar
.start
[i
],
1977 ref
->u
.ar
.as
->lower
[i
])
1978 || dummy_intent_not_in (&ref
->u
.ar
.start
[i
])))
1980 /* Check the upper bound. */
1981 if (ref
->u
.ar
.end
[i
]
1983 || !ref
->u
.ar
.as
->upper
[i
]
1984 || gfc_dep_compare_expr (ref
->u
.ar
.end
[i
],
1985 ref
->u
.ar
.as
->upper
[i
])
1986 || dummy_intent_not_in (&ref
->u
.ar
.end
[i
])))
1988 /* Check the stride. */
1989 if (ref
->u
.ar
.stride
[i
]
1990 && !gfc_expr_is_one (ref
->u
.ar
.stride
[i
], 0))
1993 /* This is unconditionally a contiguous reference as long as all
1994 the subsequent dimensions are elements. */
1998 for (n
= i
+ 1; n
< ref
->u
.ar
.dimen
; n
++)
1999 if (ref
->u
.ar
.dimen_type
[n
] != DIMEN_ELEMENT
)
2000 *contiguous
= false;
2003 if (!lbound_OK
|| !ubound_OK
)
2010 /* Determine if a full array is the same as an array section with one
2011 variable limit. For this to be so, the strides must both be unity
2012 and one of either start == lower or end == upper must be true. */
2015 ref_same_as_full_array (gfc_ref
*full_ref
, gfc_ref
*ref
)
2018 bool upper_or_lower
;
2020 if (full_ref
->type
!= REF_ARRAY
)
2022 if (full_ref
->u
.ar
.type
!= AR_FULL
)
2024 if (ref
->type
!= REF_ARRAY
)
2026 if (ref
->u
.ar
.type
!= AR_SECTION
)
2029 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
2031 /* If we have a single element in the reference, we need to check
2032 that the array has a single element and that we actually reference
2033 the correct element. */
2034 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_ELEMENT
)
2036 if (!full_ref
->u
.ar
.as
2037 || !full_ref
->u
.ar
.as
->lower
[i
]
2038 || !full_ref
->u
.ar
.as
->upper
[i
]
2039 || gfc_dep_compare_expr (full_ref
->u
.ar
.as
->lower
[i
],
2040 full_ref
->u
.ar
.as
->upper
[i
])
2041 || !ref
->u
.ar
.start
[i
]
2042 || gfc_dep_compare_expr (ref
->u
.ar
.start
[i
],
2043 full_ref
->u
.ar
.as
->lower
[i
]))
2047 /* Check the strides. */
2048 if (full_ref
->u
.ar
.stride
[i
] && !gfc_expr_is_one (full_ref
->u
.ar
.stride
[i
], 0))
2050 if (ref
->u
.ar
.stride
[i
] && !gfc_expr_is_one (ref
->u
.ar
.stride
[i
], 0))
2053 upper_or_lower
= false;
2054 /* Check the lower bound. */
2055 if (ref
->u
.ar
.start
[i
]
2057 && full_ref
->u
.ar
.as
->lower
[i
]
2058 && gfc_dep_compare_expr (ref
->u
.ar
.start
[i
],
2059 full_ref
->u
.ar
.as
->lower
[i
]) == 0))
2060 upper_or_lower
= true;
2061 /* Check the upper bound. */
2062 if (ref
->u
.ar
.end
[i
]
2064 && full_ref
->u
.ar
.as
->upper
[i
]
2065 && gfc_dep_compare_expr (ref
->u
.ar
.end
[i
],
2066 full_ref
->u
.ar
.as
->upper
[i
]) == 0))
2067 upper_or_lower
= true;
2068 if (!upper_or_lower
)
2075 /* Finds if two array references are overlapping or not.
2077 2 : array references are overlapping but reversal of one or
2078 more dimensions will clear the dependency.
2079 1 : array references are overlapping.
2080 0 : array references are identical or not overlapping. */
2083 gfc_dep_resolver (gfc_ref
*lref
, gfc_ref
*rref
, gfc_reverse
*reverse
)
2087 gfc_dependency fin_dep
;
2088 gfc_dependency this_dep
;
2090 this_dep
= GFC_DEP_ERROR
;
2091 fin_dep
= GFC_DEP_ERROR
;
2092 /* Dependencies due to pointers should already have been identified.
2093 We only need to check for overlapping array references. */
2095 while (lref
&& rref
)
2097 /* We're resolving from the same base symbol, so both refs should be
2098 the same type. We traverse the reference chain until we find ranges
2099 that are not equal. */
2100 gcc_assert (lref
->type
== rref
->type
);
2104 /* The two ranges can't overlap if they are from different
2106 if (lref
->u
.c
.component
!= rref
->u
.c
.component
)
2111 /* Substring overlaps are handled by the string assignment code
2112 if there is not an underlying dependency. */
2113 return (fin_dep
== GFC_DEP_OVERLAP
) ? 1 : 0;
2117 if (ref_same_as_full_array (lref
, rref
))
2120 if (ref_same_as_full_array (rref
, lref
))
2123 if (lref
->u
.ar
.dimen
!= rref
->u
.ar
.dimen
)
2125 if (lref
->u
.ar
.type
== AR_FULL
)
2126 fin_dep
= gfc_full_array_ref_p (rref
, NULL
) ? GFC_DEP_EQUAL
2128 else if (rref
->u
.ar
.type
== AR_FULL
)
2129 fin_dep
= gfc_full_array_ref_p (lref
, NULL
) ? GFC_DEP_EQUAL
2136 /* Index for the reverse array. */
2138 for (n
=0; n
< lref
->u
.ar
.dimen
; n
++)
2140 /* Handle dependency when either of array reference is vector
2141 subscript. There is no dependency if the vector indices
2142 are equal or if indices are known to be different in a
2143 different dimension. */
2144 if (lref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
2145 || rref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
)
2147 if (lref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
2148 && rref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
2149 && gfc_dep_compare_expr (lref
->u
.ar
.start
[n
],
2150 rref
->u
.ar
.start
[n
]) == 0)
2151 this_dep
= GFC_DEP_EQUAL
;
2153 this_dep
= GFC_DEP_OVERLAP
;
2155 goto update_fin_dep
;
2158 if (lref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
2159 && rref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
)
2160 this_dep
= check_section_vs_section (&lref
->u
.ar
, &rref
->u
.ar
, n
);
2161 else if (lref
->u
.ar
.dimen_type
[n
] == DIMEN_ELEMENT
2162 && rref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
)
2163 this_dep
= gfc_check_element_vs_section (lref
, rref
, n
);
2164 else if (rref
->u
.ar
.dimen_type
[n
] == DIMEN_ELEMENT
2165 && lref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
)
2166 this_dep
= gfc_check_element_vs_section (rref
, lref
, n
);
2169 gcc_assert (rref
->u
.ar
.dimen_type
[n
] == DIMEN_ELEMENT
2170 && lref
->u
.ar
.dimen_type
[n
] == DIMEN_ELEMENT
);
2171 this_dep
= gfc_check_element_vs_element (rref
, lref
, n
);
2174 /* If any dimension doesn't overlap, we have no dependency. */
2175 if (this_dep
== GFC_DEP_NODEP
)
2178 /* Now deal with the loop reversal logic: This only works on
2179 ranges and is activated by setting
2180 reverse[n] == GFC_ENABLE_REVERSE
2181 The ability to reverse or not is set by previous conditions
2182 in this dimension. If reversal is not activated, the
2183 value GFC_DEP_BACKWARD is reset to GFC_DEP_OVERLAP. */
2185 /* Get the indexing right for the scalarizing loop. If this
2186 is an element, there is no corresponding loop. */
2187 if (lref
->u
.ar
.dimen_type
[n
] != DIMEN_ELEMENT
)
2190 if (rref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
2191 && lref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
)
2193 /* Set reverse if backward dependence and not inhibited. */
2194 if (reverse
&& reverse
[m
] == GFC_ENABLE_REVERSE
)
2195 reverse
[m
] = (this_dep
== GFC_DEP_BACKWARD
) ?
2196 GFC_REVERSE_SET
: reverse
[m
];
2198 /* Set forward if forward dependence and not inhibited. */
2199 if (reverse
&& reverse
[m
] == GFC_ENABLE_REVERSE
)
2200 reverse
[m
] = (this_dep
== GFC_DEP_FORWARD
) ?
2201 GFC_FORWARD_SET
: reverse
[m
];
2203 /* Flag up overlap if dependence not compatible with
2204 the overall state of the expression. */
2205 if (reverse
&& reverse
[m
] == GFC_REVERSE_SET
2206 && this_dep
== GFC_DEP_FORWARD
)
2208 reverse
[m
] = GFC_INHIBIT_REVERSE
;
2209 this_dep
= GFC_DEP_OVERLAP
;
2211 else if (reverse
&& reverse
[m
] == GFC_FORWARD_SET
2212 && this_dep
== GFC_DEP_BACKWARD
)
2214 reverse
[m
] = GFC_INHIBIT_REVERSE
;
2215 this_dep
= GFC_DEP_OVERLAP
;
2218 /* If no intention of reversing or reversing is explicitly
2219 inhibited, convert backward dependence to overlap. */
2220 if ((reverse
== NULL
&& this_dep
== GFC_DEP_BACKWARD
)
2221 || (reverse
!= NULL
&& reverse
[m
] == GFC_INHIBIT_REVERSE
))
2222 this_dep
= GFC_DEP_OVERLAP
;
2225 /* Overlap codes are in order of priority. We only need to
2226 know the worst one.*/
2229 if (this_dep
> fin_dep
)
2233 /* If this is an equal element, we have to keep going until we find
2234 the "real" array reference. */
2235 if (lref
->u
.ar
.type
== AR_ELEMENT
2236 && rref
->u
.ar
.type
== AR_ELEMENT
2237 && fin_dep
== GFC_DEP_EQUAL
)
2240 /* Exactly matching and forward overlapping ranges don't cause a
2242 if (fin_dep
< GFC_DEP_BACKWARD
)
2245 /* Keep checking. We only have a dependency if
2246 subsequent references also overlap. */
2256 /* If we haven't seen any array refs then something went wrong. */
2257 gcc_assert (fin_dep
!= GFC_DEP_ERROR
);
2259 /* Assume the worst if we nest to different depths. */
2263 return fin_dep
== GFC_DEP_OVERLAP
;