2 Copyright (C) 2000-2020 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 if (r1
->u
.i
!= r2
->u
.i
)
198 gfc_internal_error ("are_identical_variables: Bad type");
206 /* Compare two functions for equality. Returns 0 if e1==e2, -2 otherwise. If
207 impure_ok is false, only return 0 for pure functions. */
210 gfc_dep_compare_functions (gfc_expr
*e1
, gfc_expr
*e2
, bool impure_ok
)
213 gfc_actual_arglist
*args1
;
214 gfc_actual_arglist
*args2
;
216 if (e1
->expr_type
!= EXPR_FUNCTION
|| e2
->expr_type
!= EXPR_FUNCTION
)
219 if ((e1
->value
.function
.esym
&& e2
->value
.function
.esym
220 && e1
->value
.function
.esym
== e2
->value
.function
.esym
221 && (e1
->value
.function
.esym
->result
->attr
.pure
|| impure_ok
))
222 || (e1
->value
.function
.isym
&& e2
->value
.function
.isym
223 && e1
->value
.function
.isym
== e2
->value
.function
.isym
224 && (e1
->value
.function
.isym
->pure
|| impure_ok
)))
226 args1
= e1
->value
.function
.actual
;
227 args2
= e2
->value
.function
.actual
;
229 /* Compare the argument lists for equality. */
230 while (args1
&& args2
)
232 /* Bitwise xor, since C has no non-bitwise xor operator. */
233 if ((args1
->expr
== NULL
) ^ (args2
->expr
== NULL
))
236 if (args1
->expr
!= NULL
&& args2
->expr
!= NULL
)
242 if (gfc_dep_compare_expr (e1
, e2
) != 0)
245 /* Special case: String arguments which compare equal can have
246 different lengths, which makes them different in calls to
249 if (e1
->expr_type
== EXPR_CONSTANT
250 && e1
->ts
.type
== BT_CHARACTER
251 && e2
->expr_type
== EXPR_CONSTANT
252 && e2
->ts
.type
== BT_CHARACTER
253 && e1
->value
.character
.length
!= e2
->value
.character
.length
)
260 return (args1
|| args2
) ? -2 : 0;
266 /* Helper function to look through parens, unary plus and widening
267 integer conversions. */
270 gfc_discard_nops (gfc_expr
*e
)
272 gfc_actual_arglist
*arglist
;
279 if (e
->expr_type
== EXPR_OP
280 && (e
->value
.op
.op
== INTRINSIC_UPLUS
281 || e
->value
.op
.op
== INTRINSIC_PARENTHESES
))
287 if (e
->expr_type
== EXPR_FUNCTION
&& e
->value
.function
.isym
288 && e
->value
.function
.isym
->id
== GFC_ISYM_CONVERSION
289 && e
->ts
.type
== BT_INTEGER
)
291 arglist
= e
->value
.function
.actual
;
292 if (arglist
->expr
->ts
.type
== BT_INTEGER
293 && e
->ts
.kind
> arglist
->expr
->ts
.kind
)
306 /* Compare two expressions. Return values:
310 * -2 if the relationship could not be determined
311 * -3 if e1 /= e2, but we cannot tell which one is larger.
312 REAL and COMPLEX constants are only compared for equality
313 or inequality; if they are unequal, -2 is returned in all cases. */
316 gfc_dep_compare_expr (gfc_expr
*e1
, gfc_expr
*e2
)
320 if (e1
== NULL
&& e2
== NULL
)
322 else if (e1
== NULL
|| e2
== NULL
)
325 e1
= gfc_discard_nops (e1
);
326 e2
= gfc_discard_nops (e2
);
328 if (e1
->expr_type
== EXPR_OP
&& e1
->value
.op
.op
== INTRINSIC_PLUS
)
330 /* Compare X+C vs. X, for INTEGER only. */
331 if (e1
->value
.op
.op2
->expr_type
== EXPR_CONSTANT
332 && e1
->value
.op
.op2
->ts
.type
== BT_INTEGER
333 && gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
) == 0)
334 return mpz_sgn (e1
->value
.op
.op2
->value
.integer
);
336 /* Compare P+Q vs. R+S. */
337 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_PLUS
)
341 l
= gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op1
);
342 r
= gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op2
);
343 if (l
== 0 && r
== 0)
345 if (l
== 0 && r
> -2)
347 if (l
> -2 && r
== 0)
349 if (l
== 1 && r
== 1)
351 if (l
== -1 && r
== -1)
354 l
= gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op2
);
355 r
= gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op1
);
356 if (l
== 0 && r
== 0)
358 if (l
== 0 && r
> -2)
360 if (l
> -2 && r
== 0)
362 if (l
== 1 && r
== 1)
364 if (l
== -1 && r
== -1)
369 /* Compare X vs. X+C, for INTEGER only. */
370 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_PLUS
)
372 if (e2
->value
.op
.op2
->expr_type
== EXPR_CONSTANT
373 && e2
->value
.op
.op2
->ts
.type
== BT_INTEGER
374 && gfc_dep_compare_expr (e1
, e2
->value
.op
.op1
) == 0)
375 return -mpz_sgn (e2
->value
.op
.op2
->value
.integer
);
378 /* Compare X-C vs. X, for INTEGER only. */
379 if (e1
->expr_type
== EXPR_OP
&& e1
->value
.op
.op
== INTRINSIC_MINUS
)
381 if (e1
->value
.op
.op2
->expr_type
== EXPR_CONSTANT
382 && e1
->value
.op
.op2
->ts
.type
== BT_INTEGER
383 && gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
) == 0)
384 return -mpz_sgn (e1
->value
.op
.op2
->value
.integer
);
386 /* Compare P-Q vs. R-S. */
387 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
391 l
= gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op1
);
392 r
= gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op2
);
393 if (l
== 0 && r
== 0)
395 if (l
> -2 && r
== 0)
397 if (l
== 0 && r
> -2)
399 if (l
== 1 && r
== -1)
401 if (l
== -1 && r
== 1)
406 /* Compare A // B vs. C // D. */
408 if (e1
->expr_type
== EXPR_OP
&& e1
->value
.op
.op
== INTRINSIC_CONCAT
409 && e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_CONCAT
)
413 l
= gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op1
);
414 r
= gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op2
);
419 /* Left expressions of // compare equal, but
420 watch out for 'A ' // x vs. 'A' // x. */
421 gfc_expr
*e1_left
= e1
->value
.op
.op1
;
422 gfc_expr
*e2_left
= e2
->value
.op
.op1
;
424 if (e1_left
->expr_type
== EXPR_CONSTANT
425 && e2_left
->expr_type
== EXPR_CONSTANT
426 && e1_left
->value
.character
.length
427 != e2_left
->value
.character
.length
)
433 /* Compare X vs. X-C, for INTEGER only. */
434 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
436 if (e2
->value
.op
.op2
->expr_type
== EXPR_CONSTANT
437 && e2
->value
.op
.op2
->ts
.type
== BT_INTEGER
438 && gfc_dep_compare_expr (e1
, e2
->value
.op
.op1
) == 0)
439 return mpz_sgn (e2
->value
.op
.op2
->value
.integer
);
442 if (e1
->expr_type
!= e2
->expr_type
)
445 switch (e1
->expr_type
)
448 /* Compare strings for equality. */
449 if (e1
->ts
.type
== BT_CHARACTER
&& e2
->ts
.type
== BT_CHARACTER
)
450 return gfc_compare_string (e1
, e2
);
452 /* Compare REAL and COMPLEX constants. Because of the
453 traps and pitfalls associated with comparing
454 a + 1.0 with a + 0.5, check for equality only. */
455 if (e2
->expr_type
== EXPR_CONSTANT
)
457 if (e1
->ts
.type
== BT_REAL
&& e2
->ts
.type
== BT_REAL
)
459 if (mpfr_cmp (e1
->value
.real
, e2
->value
.real
) == 0)
464 else if (e1
->ts
.type
== BT_COMPLEX
&& e2
->ts
.type
== BT_COMPLEX
)
466 if (mpc_cmp (e1
->value
.complex, e2
->value
.complex) == 0)
473 if (e1
->ts
.type
!= BT_INTEGER
|| e2
->ts
.type
!= BT_INTEGER
)
476 /* For INTEGER, all cases where e2 is not constant should have
477 been filtered out above. */
478 gcc_assert (e2
->expr_type
== EXPR_CONSTANT
);
480 i
= mpz_cmp (e1
->value
.integer
, e2
->value
.integer
);
488 if (are_identical_variables (e1
, e2
))
494 /* Intrinsic operators are the same if their operands are the same. */
495 if (e1
->value
.op
.op
!= e2
->value
.op
.op
)
497 if (e1
->value
.op
.op2
== 0)
499 i
= gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op1
);
500 return i
== 0 ? 0 : -2;
502 if (gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op1
) == 0
503 && gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op2
) == 0)
505 else if (e1
->value
.op
.op
== INTRINSIC_TIMES
506 && gfc_dep_compare_expr (e1
->value
.op
.op1
, e2
->value
.op
.op2
) == 0
507 && gfc_dep_compare_expr (e1
->value
.op
.op2
, e2
->value
.op
.op1
) == 0)
508 /* Commutativity of multiplication; addition is handled above. */
514 return gfc_dep_compare_functions (e1
, e2
, false);
522 /* Return the difference between two expressions. Integer expressions of
525 X + constant, X - constant and constant + X
527 are handled. Return true on success, false on failure. result is assumed
528 to be uninitialized on entry, and will be initialized on success.
532 gfc_dep_difference (gfc_expr
*e1
, gfc_expr
*e2
, mpz_t
*result
)
534 gfc_expr
*e1_op1
, *e1_op2
, *e2_op1
, *e2_op2
;
536 if (e1
== NULL
|| e2
== NULL
)
539 if (e1
->ts
.type
!= BT_INTEGER
|| e2
->ts
.type
!= BT_INTEGER
)
542 e1
= gfc_discard_nops (e1
);
543 e2
= gfc_discard_nops (e2
);
545 /* Inizialize tentatively, clear if we don't return anything. */
548 /* Case 1: c1 - c2 = c1 - c2, trivially. */
550 if (e1
->expr_type
== EXPR_CONSTANT
&& e2
->expr_type
== EXPR_CONSTANT
)
552 mpz_sub (*result
, e1
->value
.integer
, e2
->value
.integer
);
556 if (e1
->expr_type
== EXPR_OP
&& e1
->value
.op
.op
== INTRINSIC_PLUS
)
558 e1_op1
= gfc_discard_nops (e1
->value
.op
.op1
);
559 e1_op2
= gfc_discard_nops (e1
->value
.op
.op2
);
561 /* Case 2: (X + c1) - X = c1. */
562 if (e1_op2
->expr_type
== EXPR_CONSTANT
563 && gfc_dep_compare_expr (e1_op1
, e2
) == 0)
565 mpz_set (*result
, e1_op2
->value
.integer
);
569 /* Case 3: (c1 + X) - X = c1. */
570 if (e1_op1
->expr_type
== EXPR_CONSTANT
571 && gfc_dep_compare_expr (e1_op2
, e2
) == 0)
573 mpz_set (*result
, e1_op1
->value
.integer
);
577 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_PLUS
)
579 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
580 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
582 if (e1_op2
->expr_type
== EXPR_CONSTANT
)
584 /* Case 4: X + c1 - (X + c2) = c1 - c2. */
585 if (e2_op2
->expr_type
== EXPR_CONSTANT
586 && gfc_dep_compare_expr (e1_op1
, e2_op1
) == 0)
588 mpz_sub (*result
, e1_op2
->value
.integer
,
589 e2_op2
->value
.integer
);
592 /* Case 5: X + c1 - (c2 + X) = c1 - c2. */
593 if (e2_op1
->expr_type
== EXPR_CONSTANT
594 && gfc_dep_compare_expr (e1_op1
, e2_op2
) == 0)
596 mpz_sub (*result
, e1_op2
->value
.integer
,
597 e2_op1
->value
.integer
);
601 else if (e1_op1
->expr_type
== EXPR_CONSTANT
)
603 /* Case 6: c1 + X - (X + c2) = c1 - c2. */
604 if (e2_op2
->expr_type
== EXPR_CONSTANT
605 && gfc_dep_compare_expr (e1_op2
, e2_op1
) == 0)
607 mpz_sub (*result
, e1_op1
->value
.integer
,
608 e2_op2
->value
.integer
);
611 /* Case 7: c1 + X - (c2 + X) = c1 - c2. */
612 if (e2_op1
->expr_type
== EXPR_CONSTANT
613 && gfc_dep_compare_expr (e1_op2
, e2_op2
) == 0)
615 mpz_sub (*result
, e1_op1
->value
.integer
,
616 e2_op1
->value
.integer
);
622 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
624 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
625 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
627 if (e1_op2
->expr_type
== EXPR_CONSTANT
)
629 /* Case 8: X + c1 - (X - c2) = c1 + c2. */
630 if (e2_op2
->expr_type
== EXPR_CONSTANT
631 && gfc_dep_compare_expr (e1_op1
, e2_op1
) == 0)
633 mpz_add (*result
, e1_op2
->value
.integer
,
634 e2_op2
->value
.integer
);
638 if (e1_op1
->expr_type
== EXPR_CONSTANT
)
640 /* Case 9: c1 + X - (X - c2) = c1 + c2. */
641 if (e2_op2
->expr_type
== EXPR_CONSTANT
642 && gfc_dep_compare_expr (e1_op2
, e2_op1
) == 0)
644 mpz_add (*result
, e1_op1
->value
.integer
,
645 e2_op2
->value
.integer
);
652 if (e1
->expr_type
== EXPR_OP
&& e1
->value
.op
.op
== INTRINSIC_MINUS
)
654 e1_op1
= gfc_discard_nops (e1
->value
.op
.op1
);
655 e1_op2
= gfc_discard_nops (e1
->value
.op
.op2
);
657 if (e1_op2
->expr_type
== EXPR_CONSTANT
)
659 /* Case 10: (X - c1) - X = -c1 */
661 if (gfc_dep_compare_expr (e1_op1
, e2
) == 0)
663 mpz_neg (*result
, e1_op2
->value
.integer
);
667 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_PLUS
)
669 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
670 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
672 /* Case 11: (X - c1) - (X + c2) = -( c1 + c2). */
673 if (e2_op2
->expr_type
== EXPR_CONSTANT
674 && gfc_dep_compare_expr (e1_op1
, e2_op1
) == 0)
676 mpz_add (*result
, e1_op2
->value
.integer
,
677 e2_op2
->value
.integer
);
678 mpz_neg (*result
, *result
);
682 /* Case 12: X - c1 - (c2 + X) = - (c1 + c2). */
683 if (e2_op1
->expr_type
== EXPR_CONSTANT
684 && gfc_dep_compare_expr (e1_op1
, e2_op2
) == 0)
686 mpz_add (*result
, e1_op2
->value
.integer
,
687 e2_op1
->value
.integer
);
688 mpz_neg (*result
, *result
);
693 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
695 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
696 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
698 /* Case 13: (X - c1) - (X - c2) = c2 - c1. */
699 if (e2_op2
->expr_type
== EXPR_CONSTANT
700 && gfc_dep_compare_expr (e1_op1
, e2_op1
) == 0)
702 mpz_sub (*result
, e2_op2
->value
.integer
,
703 e1_op2
->value
.integer
);
708 if (e1_op1
->expr_type
== EXPR_CONSTANT
)
710 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
712 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
713 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
715 /* Case 14: (c1 - X) - (c2 - X) == c1 - c2. */
716 if (gfc_dep_compare_expr (e1_op2
, e2_op2
) == 0)
718 mpz_sub (*result
, e1_op1
->value
.integer
,
719 e2_op1
->value
.integer
);
727 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_PLUS
)
729 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
730 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
732 /* Case 15: X - (X + c2) = -c2. */
733 if (e2_op2
->expr_type
== EXPR_CONSTANT
734 && gfc_dep_compare_expr (e1
, e2_op1
) == 0)
736 mpz_neg (*result
, e2_op2
->value
.integer
);
739 /* Case 16: X - (c2 + X) = -c2. */
740 if (e2_op1
->expr_type
== EXPR_CONSTANT
741 && gfc_dep_compare_expr (e1
, e2_op2
) == 0)
743 mpz_neg (*result
, e2_op1
->value
.integer
);
748 if (e2
->expr_type
== EXPR_OP
&& e2
->value
.op
.op
== INTRINSIC_MINUS
)
750 e2_op1
= gfc_discard_nops (e2
->value
.op
.op1
);
751 e2_op2
= gfc_discard_nops (e2
->value
.op
.op2
);
753 /* Case 17: X - (X - c2) = c2. */
754 if (e2_op2
->expr_type
== EXPR_CONSTANT
755 && gfc_dep_compare_expr (e1
, e2_op1
) == 0)
757 mpz_set (*result
, e2_op2
->value
.integer
);
762 if (gfc_dep_compare_expr (e1
, e2
) == 0)
764 /* Case 18: X - X = 0. */
765 mpz_set_si (*result
, 0);
773 /* Returns 1 if the two ranges are the same and 0 if they are not (or if the
774 results are indeterminate). 'n' is the dimension to compare. */
777 is_same_range (gfc_array_ref
*ar1
, gfc_array_ref
*ar2
, int n
)
783 /* TODO: More sophisticated range comparison. */
784 gcc_assert (ar1
&& ar2
);
786 gcc_assert (ar1
->dimen_type
[n
] == ar2
->dimen_type
[n
]);
790 /* Check for mismatching strides. A NULL stride means a stride of 1. */
793 i
= gfc_expr_is_one (e1
, -1);
794 if (i
== -1 || i
== 0)
799 i
= gfc_expr_is_one (e2
, -1);
800 if (i
== -1 || i
== 0)
805 i
= gfc_dep_compare_expr (e1
, e2
);
809 /* The strides match. */
811 /* Check the range start. */
816 /* Use the bound of the array if no bound is specified. */
818 e1
= ar1
->as
->lower
[n
];
821 e2
= ar2
->as
->lower
[n
];
823 /* Check we have values for both. */
827 i
= gfc_dep_compare_expr (e1
, e2
);
832 /* Check the range end. */
837 /* Use the bound of the array if no bound is specified. */
839 e1
= ar1
->as
->upper
[n
];
842 e2
= ar2
->as
->upper
[n
];
844 /* Check we have values for both. */
848 i
= gfc_dep_compare_expr (e1
, e2
);
857 /* Some array-returning intrinsics can be implemented by reusing the
858 data from one of the array arguments. For example, TRANSPOSE does
859 not necessarily need to allocate new data: it can be implemented
860 by copying the original array's descriptor and simply swapping the
861 two dimension specifications.
863 If EXPR is a call to such an intrinsic, return the argument
864 whose data can be reused, otherwise return NULL. */
867 gfc_get_noncopying_intrinsic_argument (gfc_expr
*expr
)
869 if (expr
->expr_type
!= EXPR_FUNCTION
|| !expr
->value
.function
.isym
)
872 switch (expr
->value
.function
.isym
->id
)
874 case GFC_ISYM_TRANSPOSE
:
875 return expr
->value
.function
.actual
->expr
;
883 /* Return true if the result of reference REF can only be constructed
884 using a temporary array. */
887 gfc_ref_needs_temporary_p (gfc_ref
*ref
)
893 for (; ref
; ref
= ref
->next
)
897 /* Vector dimensions are generally not monotonic and must be
898 handled using a temporary. */
899 if (ref
->u
.ar
.type
== AR_SECTION
)
900 for (n
= 0; n
< ref
->u
.ar
.dimen
; n
++)
901 if (ref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
)
908 /* Within an array reference, character substrings generally
909 need a temporary. Character array strides are expressed as
910 multiples of the element size (consistent with other array
911 types), not in characters. */
924 gfc_is_data_pointer (gfc_expr
*e
)
928 if (e
->expr_type
!= EXPR_VARIABLE
&& e
->expr_type
!= EXPR_FUNCTION
)
931 /* No subreference if it is a function */
932 gcc_assert (e
->expr_type
== EXPR_VARIABLE
|| !e
->ref
);
934 if (e
->symtree
->n
.sym
->attr
.pointer
)
937 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
938 if (ref
->type
== REF_COMPONENT
&& ref
->u
.c
.component
->attr
.pointer
)
945 /* Return true if array variable VAR could be passed to the same function
946 as argument EXPR without interfering with EXPR. INTENT is the intent
949 This is considerably less conservative than other dependencies
950 because many function arguments will already be copied into a
954 gfc_check_argument_var_dependency (gfc_expr
*var
, sym_intent intent
,
955 gfc_expr
*expr
, gfc_dep_check elemental
)
959 gcc_assert (var
->expr_type
== EXPR_VARIABLE
);
960 gcc_assert (var
->rank
> 0);
962 switch (expr
->expr_type
)
965 /* In case of elemental subroutines, there is no dependency
966 between two same-range array references. */
967 if (gfc_ref_needs_temporary_p (expr
->ref
)
968 || gfc_check_dependency (var
, expr
, elemental
== NOT_ELEMENTAL
))
970 if (elemental
== ELEM_DONT_CHECK_VARIABLE
)
972 /* Too many false positive with pointers. */
973 if (!gfc_is_data_pointer (var
) && !gfc_is_data_pointer (expr
))
975 /* Elemental procedures forbid unspecified intents,
976 and we don't check dependencies for INTENT_IN args. */
977 gcc_assert (intent
== INTENT_OUT
|| intent
== INTENT_INOUT
);
979 /* We are told not to check dependencies.
980 We do it, however, and issue a warning in case we find one.
981 If a dependency is found in the case
982 elemental == ELEM_CHECK_VARIABLE, we will generate
983 a temporary, so we don't need to bother the user. */
985 if (var
->expr_type
== EXPR_VARIABLE
986 && expr
->expr_type
== EXPR_VARIABLE
987 && strcmp(var
->symtree
->name
, expr
->symtree
->name
) == 0)
988 gfc_warning (0, "INTENT(%s) actual argument at %L might "
989 "interfere with actual argument at %L.",
990 intent
== INTENT_OUT
? "OUT" : "INOUT",
991 &var
->where
, &expr
->where
);
1001 /* the scalarizer always generates a temporary for array constructors,
1002 so there is no dependency. */
1006 if (intent
!= INTENT_IN
)
1008 arg
= gfc_get_noncopying_intrinsic_argument (expr
);
1010 return gfc_check_argument_var_dependency (var
, intent
, arg
,
1014 if (elemental
!= NOT_ELEMENTAL
)
1016 if ((expr
->value
.function
.esym
1017 && expr
->value
.function
.esym
->attr
.elemental
)
1018 || (expr
->value
.function
.isym
1019 && expr
->value
.function
.isym
->elemental
))
1020 return gfc_check_fncall_dependency (var
, intent
, NULL
,
1021 expr
->value
.function
.actual
,
1022 ELEM_CHECK_VARIABLE
);
1024 if (gfc_inline_intrinsic_function_p (expr
))
1026 /* The TRANSPOSE case should have been caught in the
1027 noncopying intrinsic case above. */
1028 gcc_assert (expr
->value
.function
.isym
->id
!= GFC_ISYM_TRANSPOSE
);
1030 return gfc_check_fncall_dependency (var
, intent
, NULL
,
1031 expr
->value
.function
.actual
,
1032 ELEM_CHECK_VARIABLE
);
1038 /* In case of non-elemental procedures, there is no need to catch
1039 dependencies, as we will make a temporary anyway. */
1042 /* If the actual arg EXPR is an expression, we need to catch
1043 a dependency between variables in EXPR and VAR,
1044 an intent((IN)OUT) variable. */
1045 if (expr
->value
.op
.op1
1046 && gfc_check_argument_var_dependency (var
, intent
,
1048 ELEM_CHECK_VARIABLE
))
1050 else if (expr
->value
.op
.op2
1051 && gfc_check_argument_var_dependency (var
, intent
,
1053 ELEM_CHECK_VARIABLE
))
1064 /* Like gfc_check_argument_var_dependency, but extended to any
1065 array expression OTHER, not just variables. */
1068 gfc_check_argument_dependency (gfc_expr
*other
, sym_intent intent
,
1069 gfc_expr
*expr
, gfc_dep_check elemental
)
1071 switch (other
->expr_type
)
1074 return gfc_check_argument_var_dependency (other
, intent
, expr
, elemental
);
1077 other
= gfc_get_noncopying_intrinsic_argument (other
);
1079 return gfc_check_argument_dependency (other
, INTENT_IN
, expr
,
1090 /* Like gfc_check_argument_dependency, but check all the arguments in ACTUAL.
1091 FNSYM is the function being called, or NULL if not known. */
1094 gfc_check_fncall_dependency (gfc_expr
*other
, sym_intent intent
,
1095 gfc_symbol
*fnsym
, gfc_actual_arglist
*actual
,
1096 gfc_dep_check elemental
)
1098 gfc_formal_arglist
*formal
;
1101 formal
= fnsym
? gfc_sym_get_dummy_args (fnsym
) : NULL
;
1102 for (; actual
; actual
= actual
->next
, formal
= formal
? formal
->next
: NULL
)
1104 expr
= actual
->expr
;
1106 /* Skip args which are not present. */
1110 /* Skip other itself. */
1114 /* Skip intent(in) arguments if OTHER itself is intent(in). */
1115 if (formal
&& intent
== INTENT_IN
1116 && formal
->sym
->attr
.intent
== INTENT_IN
)
1119 if (gfc_check_argument_dependency (other
, intent
, expr
, elemental
))
1127 /* Return 1 if e1 and e2 are equivalenced arrays, either
1128 directly or indirectly; i.e., equivalence (a,b) for a and b
1129 or equivalence (a,c),(b,c). This function uses the equiv_
1130 lists, generated in trans-common(add_equivalences), that are
1131 guaranteed to pick up indirect equivalences. We explicitly
1132 check for overlap using the offset and length of the equivalence.
1133 This function is symmetric.
1134 TODO: This function only checks whether the full top-level
1135 symbols overlap. An improved implementation could inspect
1136 e1->ref and e2->ref to determine whether the actually accessed
1137 portions of these variables/arrays potentially overlap. */
1140 gfc_are_equivalenced_arrays (gfc_expr
*e1
, gfc_expr
*e2
)
1143 gfc_equiv_info
*s
, *fl1
, *fl2
;
1145 gcc_assert (e1
->expr_type
== EXPR_VARIABLE
1146 && e2
->expr_type
== EXPR_VARIABLE
);
1148 if (!e1
->symtree
->n
.sym
->attr
.in_equivalence
1149 || !e2
->symtree
->n
.sym
->attr
.in_equivalence
|| !e1
->rank
|| !e2
->rank
)
1152 if (e1
->symtree
->n
.sym
->ns
1153 && e1
->symtree
->n
.sym
->ns
!= gfc_current_ns
)
1154 l
= e1
->symtree
->n
.sym
->ns
->equiv_lists
;
1156 l
= gfc_current_ns
->equiv_lists
;
1158 /* Go through the equiv_lists and return 1 if the variables
1159 e1 and e2 are members of the same group and satisfy the
1160 requirement on their relative offsets. */
1161 for (; l
; l
= l
->next
)
1165 for (s
= l
->equiv
; s
; s
= s
->next
)
1167 if (s
->sym
== e1
->symtree
->n
.sym
)
1173 if (s
->sym
== e2
->symtree
->n
.sym
)
1183 /* Can these lengths be zero? */
1184 if (fl1
->length
<= 0 || fl2
->length
<= 0)
1186 /* These can't overlap if [f11,fl1+length] is before
1187 [fl2,fl2+length], or [fl2,fl2+length] is before
1188 [fl1,fl1+length], otherwise they do overlap. */
1189 if (fl1
->offset
+ fl1
->length
> fl2
->offset
1190 && fl2
->offset
+ fl2
->length
> fl1
->offset
)
1198 /* Return true if there is no possibility of aliasing because of a type
1199 mismatch between all the possible pointer references and the
1200 potential target. Note that this function is asymmetric in the
1201 arguments and so must be called twice with the arguments exchanged. */
1204 check_data_pointer_types (gfc_expr
*expr1
, gfc_expr
*expr2
)
1210 bool seen_component_ref
;
1212 if (expr1
->expr_type
!= EXPR_VARIABLE
1213 || expr2
->expr_type
!= EXPR_VARIABLE
)
1216 sym1
= expr1
->symtree
->n
.sym
;
1217 sym2
= expr2
->symtree
->n
.sym
;
1219 /* Keep it simple for now. */
1220 if (sym1
->ts
.type
== BT_DERIVED
&& sym2
->ts
.type
== BT_DERIVED
)
1223 if (sym1
->attr
.pointer
)
1225 if (gfc_compare_types (&sym1
->ts
, &sym2
->ts
))
1229 /* This is a conservative check on the components of the derived type
1230 if no component references have been seen. Since we will not dig
1231 into the components of derived type components, we play it safe by
1232 returning false. First we check the reference chain and then, if
1233 no component references have been seen, the components. */
1234 seen_component_ref
= false;
1235 if (sym1
->ts
.type
== BT_DERIVED
)
1237 for (ref1
= expr1
->ref
; ref1
; ref1
= ref1
->next
)
1239 if (ref1
->type
!= REF_COMPONENT
)
1242 if (ref1
->u
.c
.component
->ts
.type
== BT_DERIVED
)
1245 if ((sym2
->attr
.pointer
|| ref1
->u
.c
.component
->attr
.pointer
)
1246 && gfc_compare_types (&ref1
->u
.c
.component
->ts
, &sym2
->ts
))
1249 seen_component_ref
= true;
1253 if (sym1
->ts
.type
== BT_DERIVED
&& !seen_component_ref
)
1255 for (cm1
= sym1
->ts
.u
.derived
->components
; cm1
; cm1
= cm1
->next
)
1257 if (cm1
->ts
.type
== BT_DERIVED
)
1260 if ((sym2
->attr
.pointer
|| cm1
->attr
.pointer
)
1261 && gfc_compare_types (&cm1
->ts
, &sym2
->ts
))
1270 /* Return true if the statement body redefines the condition. Returns
1271 true if expr2 depends on expr1. expr1 should be a single term
1272 suitable for the lhs of an assignment. The IDENTICAL flag indicates
1273 whether array references to the same symbol with identical range
1274 references count as a dependency or not. Used for forall and where
1275 statements. Also used with functions returning arrays without a
1279 gfc_check_dependency (gfc_expr
*expr1
, gfc_expr
*expr2
, bool identical
)
1281 gfc_actual_arglist
*actual
;
1285 /* -fcoarray=lib can end up here with expr1->expr_type set to EXPR_FUNCTION
1286 and a reference to _F.caf_get, so skip the assert. */
1287 if (expr1
->expr_type
== EXPR_FUNCTION
1288 && strcmp (expr1
->value
.function
.name
, "_F.caf_get") == 0)
1291 if (expr1
->expr_type
!= EXPR_VARIABLE
)
1292 gfc_internal_error ("gfc_check_dependency: expecting an EXPR_VARIABLE");
1294 switch (expr2
->expr_type
)
1297 n
= gfc_check_dependency (expr1
, expr2
->value
.op
.op1
, identical
);
1300 if (expr2
->value
.op
.op2
)
1301 return gfc_check_dependency (expr1
, expr2
->value
.op
.op2
, identical
);
1305 /* The interesting cases are when the symbols don't match. */
1306 if (expr1
->symtree
->n
.sym
!= expr2
->symtree
->n
.sym
)
1308 symbol_attribute attr1
, attr2
;
1309 gfc_typespec
*ts1
= &expr1
->symtree
->n
.sym
->ts
;
1310 gfc_typespec
*ts2
= &expr2
->symtree
->n
.sym
->ts
;
1312 /* Return 1 if expr1 and expr2 are equivalenced arrays. */
1313 if (gfc_are_equivalenced_arrays (expr1
, expr2
))
1316 /* Symbols can only alias if they have the same type. */
1317 if (ts1
->type
!= BT_UNKNOWN
&& ts2
->type
!= BT_UNKNOWN
1318 && ts1
->type
!= BT_DERIVED
&& ts2
->type
!= BT_DERIVED
)
1320 if (ts1
->type
!= ts2
->type
|| ts1
->kind
!= ts2
->kind
)
1324 /* We have to also include target-target as ptr%comp is not a
1325 pointer but it still alias with "dt%comp" for "ptr => dt". As
1326 subcomponents and array access to pointers retains the target
1327 attribute, that's sufficient. */
1328 attr1
= gfc_expr_attr (expr1
);
1329 attr2
= gfc_expr_attr (expr2
);
1330 if ((attr1
.pointer
|| attr1
.target
) && (attr2
.pointer
|| attr2
.target
))
1332 if (check_data_pointer_types (expr1
, expr2
)
1333 && check_data_pointer_types (expr2
, expr1
))
1340 gfc_symbol
*sym1
= expr1
->symtree
->n
.sym
;
1341 gfc_symbol
*sym2
= expr2
->symtree
->n
.sym
;
1342 if (sym1
->attr
.target
&& sym2
->attr
.target
1343 && ((sym1
->attr
.dummy
&& !sym1
->attr
.contiguous
1344 && (!sym1
->attr
.dimension
1345 || sym2
->as
->type
== AS_ASSUMED_SHAPE
))
1346 || (sym2
->attr
.dummy
&& !sym2
->attr
.contiguous
1347 && (!sym2
->attr
.dimension
1348 || sym2
->as
->type
== AS_ASSUMED_SHAPE
))))
1352 /* Otherwise distinct symbols have no dependencies. */
1356 /* Identical and disjoint ranges return 0,
1357 overlapping ranges return 1. */
1358 if (expr1
->ref
&& expr2
->ref
)
1359 return gfc_dep_resolver (expr1
->ref
, expr2
->ref
, NULL
, identical
);
1364 if (gfc_get_noncopying_intrinsic_argument (expr2
) != NULL
)
1367 /* Remember possible differences between elemental and
1368 transformational functions. All functions inside a FORALL
1370 for (actual
= expr2
->value
.function
.actual
;
1371 actual
; actual
= actual
->next
)
1375 n
= gfc_check_dependency (expr1
, actual
->expr
, identical
);
1386 /* Loop through the array constructor's elements. */
1387 for (c
= gfc_constructor_first (expr2
->value
.constructor
);
1388 c
; c
= gfc_constructor_next (c
))
1390 /* If this is an iterator, assume the worst. */
1393 /* Avoid recursion in the common case. */
1394 if (c
->expr
->expr_type
== EXPR_CONSTANT
)
1396 if (gfc_check_dependency (expr1
, c
->expr
, 1))
1407 /* Determines overlapping for two array sections. */
1409 static gfc_dependency
1410 check_section_vs_section (gfc_array_ref
*l_ar
, gfc_array_ref
*r_ar
, int n
)
1426 int stride_comparison
;
1427 int start_comparison
;
1430 /* If they are the same range, return without more ado. */
1431 if (is_same_range (l_ar
, r_ar
, n
))
1432 return GFC_DEP_EQUAL
;
1434 l_start
= l_ar
->start
[n
];
1435 l_end
= l_ar
->end
[n
];
1436 l_stride
= l_ar
->stride
[n
];
1438 r_start
= r_ar
->start
[n
];
1439 r_end
= r_ar
->end
[n
];
1440 r_stride
= r_ar
->stride
[n
];
1442 /* If l_start is NULL take it from array specifier. */
1443 if (l_start
== NULL
&& IS_ARRAY_EXPLICIT (l_ar
->as
))
1444 l_start
= l_ar
->as
->lower
[n
];
1445 /* If l_end is NULL take it from array specifier. */
1446 if (l_end
== NULL
&& IS_ARRAY_EXPLICIT (l_ar
->as
))
1447 l_end
= l_ar
->as
->upper
[n
];
1449 /* If r_start is NULL take it from array specifier. */
1450 if (r_start
== NULL
&& IS_ARRAY_EXPLICIT (r_ar
->as
))
1451 r_start
= r_ar
->as
->lower
[n
];
1452 /* If r_end is NULL take it from array specifier. */
1453 if (r_end
== NULL
&& IS_ARRAY_EXPLICIT (r_ar
->as
))
1454 r_end
= r_ar
->as
->upper
[n
];
1456 /* Determine whether the l_stride is positive or negative. */
1459 else if (l_stride
->expr_type
== EXPR_CONSTANT
1460 && l_stride
->ts
.type
== BT_INTEGER
)
1461 l_dir
= mpz_sgn (l_stride
->value
.integer
);
1462 else if (l_start
&& l_end
)
1463 l_dir
= gfc_dep_compare_expr (l_end
, l_start
);
1467 /* Determine whether the r_stride is positive or negative. */
1470 else if (r_stride
->expr_type
== EXPR_CONSTANT
1471 && r_stride
->ts
.type
== BT_INTEGER
)
1472 r_dir
= mpz_sgn (r_stride
->value
.integer
);
1473 else if (r_start
&& r_end
)
1474 r_dir
= gfc_dep_compare_expr (r_end
, r_start
);
1478 /* The strides should never be zero. */
1479 if (l_dir
== 0 || r_dir
== 0)
1480 return GFC_DEP_OVERLAP
;
1482 /* Determine the relationship between the strides. Set stride_comparison to
1483 -2 if the dependency cannot be determined
1484 -1 if l_stride < r_stride
1485 0 if l_stride == r_stride
1486 1 if l_stride > r_stride
1487 as determined by gfc_dep_compare_expr. */
1489 one_expr
= gfc_get_int_expr (gfc_index_integer_kind
, NULL
, 1);
1491 stride_comparison
= gfc_dep_compare_expr (l_stride
? l_stride
: one_expr
,
1492 r_stride
? r_stride
: one_expr
);
1494 if (l_start
&& r_start
)
1495 start_comparison
= gfc_dep_compare_expr (l_start
, r_start
);
1497 start_comparison
= -2;
1499 gfc_free_expr (one_expr
);
1501 /* Determine LHS upper and lower bounds. */
1507 else if (l_dir
== -1)
1518 /* Determine RHS upper and lower bounds. */
1524 else if (r_dir
== -1)
1535 /* Check whether the ranges are disjoint. */
1536 if (l_upper
&& r_lower
&& gfc_dep_compare_expr (l_upper
, r_lower
) == -1)
1537 return GFC_DEP_NODEP
;
1538 if (r_upper
&& l_lower
&& gfc_dep_compare_expr (r_upper
, l_lower
) == -1)
1539 return GFC_DEP_NODEP
;
1541 /* Handle cases like x:y:1 vs. x:z:-1 as GFC_DEP_EQUAL. */
1542 if (l_start
&& r_start
&& gfc_dep_compare_expr (l_start
, r_start
) == 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:1 vs. z:y:-1 as GFC_DEP_EQUAL. */
1551 if (l_end
&& r_end
&& gfc_dep_compare_expr (l_end
, r_end
) == 0)
1553 if (l_dir
== 1 && r_dir
== -1)
1554 return GFC_DEP_EQUAL
;
1555 if (l_dir
== -1 && r_dir
== 1)
1556 return GFC_DEP_EQUAL
;
1559 /* Handle cases like x:y:2 vs. x+1:z:4 as GFC_DEP_NODEP.
1560 There is no dependency if the remainder of
1561 (l_start - r_start) / gcd(l_stride, r_stride) is
1564 - Cases like a(1:4:2) = a(2:3) are still not handled.
1567 #define IS_CONSTANT_INTEGER(a) ((a) && ((a)->expr_type == EXPR_CONSTANT) \
1568 && (a)->ts.type == BT_INTEGER)
1570 if (IS_CONSTANT_INTEGER (l_stride
) && IS_CONSTANT_INTEGER (r_stride
)
1571 && gfc_dep_difference (l_start
, r_start
, &tmp
))
1577 mpz_gcd (gcd
, l_stride
->value
.integer
, r_stride
->value
.integer
);
1579 mpz_fdiv_r (tmp
, tmp
, gcd
);
1580 result
= mpz_cmp_si (tmp
, 0L);
1586 return GFC_DEP_NODEP
;
1589 #undef IS_CONSTANT_INTEGER
1591 /* Check for forward dependencies x:y vs. x+1:z and x:y:z vs. x:y:z+1. */
1593 if (l_dir
== 1 && r_dir
== 1 &&
1594 (start_comparison
== 0 || start_comparison
== -1)
1595 && (stride_comparison
== 0 || stride_comparison
== -1))
1596 return GFC_DEP_FORWARD
;
1598 /* Check for forward dependencies x:y:-1 vs. x-1:z:-1 and
1599 x:y:-1 vs. x:y:-2. */
1600 if (l_dir
== -1 && r_dir
== -1 &&
1601 (start_comparison
== 0 || start_comparison
== 1)
1602 && (stride_comparison
== 0 || stride_comparison
== 1))
1603 return GFC_DEP_FORWARD
;
1605 if (stride_comparison
== 0 || stride_comparison
== -1)
1607 if (l_start
&& IS_ARRAY_EXPLICIT (l_ar
->as
))
1610 /* Check for a(low:y:s) vs. a(z:x:s) or
1611 a(low:y:s) vs. a(z:x:s+1) where a has a lower bound
1612 of low, which is always at least a forward dependence. */
1615 && gfc_dep_compare_expr (l_start
, l_ar
->as
->lower
[n
]) == 0)
1616 return GFC_DEP_FORWARD
;
1620 if (stride_comparison
== 0 || stride_comparison
== 1)
1622 if (l_start
&& IS_ARRAY_EXPLICIT (l_ar
->as
))
1625 /* Check for a(high:y:-s) vs. a(z:x:-s) or
1626 a(high:y:-s vs. a(z:x:-s-1) where a has a higher bound
1627 of high, which is always at least a forward dependence. */
1630 && gfc_dep_compare_expr (l_start
, l_ar
->as
->upper
[n
]) == 0)
1631 return GFC_DEP_FORWARD
;
1636 if (stride_comparison
== 0)
1638 /* From here, check for backwards dependencies. */
1639 /* x+1:y vs. x:z. */
1640 if (l_dir
== 1 && r_dir
== 1 && start_comparison
== 1)
1641 return GFC_DEP_BACKWARD
;
1643 /* x-1:y:-1 vs. x:z:-1. */
1644 if (l_dir
== -1 && r_dir
== -1 && start_comparison
== -1)
1645 return GFC_DEP_BACKWARD
;
1648 return GFC_DEP_OVERLAP
;
1652 /* Determines overlapping for a single element and a section. */
1654 static gfc_dependency
1655 gfc_check_element_vs_section( gfc_ref
*lref
, gfc_ref
*rref
, int n
)
1664 elem
= lref
->u
.ar
.start
[n
];
1666 return GFC_DEP_OVERLAP
;
1669 start
= ref
->start
[n
] ;
1671 stride
= ref
->stride
[n
];
1673 if (!start
&& IS_ARRAY_EXPLICIT (ref
->as
))
1674 start
= ref
->as
->lower
[n
];
1675 if (!end
&& IS_ARRAY_EXPLICIT (ref
->as
))
1676 end
= ref
->as
->upper
[n
];
1678 /* Determine whether the stride is positive or negative. */
1681 else if (stride
->expr_type
== EXPR_CONSTANT
1682 && stride
->ts
.type
== BT_INTEGER
)
1683 s
= mpz_sgn (stride
->value
.integer
);
1687 /* Stride should never be zero. */
1689 return GFC_DEP_OVERLAP
;
1691 /* Positive strides. */
1694 /* Check for elem < lower. */
1695 if (start
&& gfc_dep_compare_expr (elem
, start
) == -1)
1696 return GFC_DEP_NODEP
;
1697 /* Check for elem > upper. */
1698 if (end
&& gfc_dep_compare_expr (elem
, end
) == 1)
1699 return GFC_DEP_NODEP
;
1703 s
= gfc_dep_compare_expr (start
, end
);
1704 /* Check for an empty range. */
1706 return GFC_DEP_NODEP
;
1707 if (s
== 0 && gfc_dep_compare_expr (elem
, start
) == 0)
1708 return GFC_DEP_EQUAL
;
1711 /* Negative strides. */
1714 /* Check for elem > upper. */
1715 if (end
&& gfc_dep_compare_expr (elem
, start
) == 1)
1716 return GFC_DEP_NODEP
;
1717 /* Check for elem < lower. */
1718 if (start
&& gfc_dep_compare_expr (elem
, end
) == -1)
1719 return GFC_DEP_NODEP
;
1723 s
= gfc_dep_compare_expr (start
, end
);
1724 /* Check for an empty range. */
1726 return GFC_DEP_NODEP
;
1727 if (s
== 0 && gfc_dep_compare_expr (elem
, start
) == 0)
1728 return GFC_DEP_EQUAL
;
1731 /* Unknown strides. */
1735 return GFC_DEP_OVERLAP
;
1736 s
= gfc_dep_compare_expr (start
, end
);
1738 return GFC_DEP_OVERLAP
;
1739 /* Assume positive stride. */
1742 /* Check for elem < lower. */
1743 if (gfc_dep_compare_expr (elem
, start
) == -1)
1744 return GFC_DEP_NODEP
;
1745 /* Check for elem > upper. */
1746 if (gfc_dep_compare_expr (elem
, end
) == 1)
1747 return GFC_DEP_NODEP
;
1749 /* Assume negative stride. */
1752 /* Check for elem > upper. */
1753 if (gfc_dep_compare_expr (elem
, start
) == 1)
1754 return GFC_DEP_NODEP
;
1755 /* Check for elem < lower. */
1756 if (gfc_dep_compare_expr (elem
, end
) == -1)
1757 return GFC_DEP_NODEP
;
1762 s
= gfc_dep_compare_expr (elem
, start
);
1764 return GFC_DEP_EQUAL
;
1765 if (s
== 1 || s
== -1)
1766 return GFC_DEP_NODEP
;
1770 return GFC_DEP_OVERLAP
;
1774 /* Traverse expr, checking all EXPR_VARIABLE symbols for their
1775 forall_index attribute. Return true if any variable may be
1776 being used as a FORALL index. Its safe to pessimistically
1777 return true, and assume a dependency. */
1780 contains_forall_index_p (gfc_expr
*expr
)
1782 gfc_actual_arglist
*arg
;
1790 switch (expr
->expr_type
)
1793 if (expr
->symtree
->n
.sym
->forall_index
)
1798 if (contains_forall_index_p (expr
->value
.op
.op1
)
1799 || contains_forall_index_p (expr
->value
.op
.op2
))
1804 for (arg
= expr
->value
.function
.actual
; arg
; arg
= arg
->next
)
1805 if (contains_forall_index_p (arg
->expr
))
1811 case EXPR_SUBSTRING
:
1814 case EXPR_STRUCTURE
:
1816 for (c
= gfc_constructor_first (expr
->value
.constructor
);
1817 c
; gfc_constructor_next (c
))
1818 if (contains_forall_index_p (c
->expr
))
1826 for (ref
= expr
->ref
; ref
; ref
= ref
->next
)
1830 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
1831 if (contains_forall_index_p (ref
->u
.ar
.start
[i
])
1832 || contains_forall_index_p (ref
->u
.ar
.end
[i
])
1833 || contains_forall_index_p (ref
->u
.ar
.stride
[i
]))
1841 if (contains_forall_index_p (ref
->u
.ss
.start
)
1842 || contains_forall_index_p (ref
->u
.ss
.end
))
1853 /* Determines overlapping for two single element array references. */
1855 static gfc_dependency
1856 gfc_check_element_vs_element (gfc_ref
*lref
, gfc_ref
*rref
, int n
)
1866 l_start
= l_ar
.start
[n
] ;
1867 r_start
= r_ar
.start
[n
] ;
1868 i
= gfc_dep_compare_expr (r_start
, l_start
);
1870 return GFC_DEP_EQUAL
;
1872 /* Treat two scalar variables as potentially equal. This allows
1873 us to prove that a(i,:) and a(j,:) have no dependency. See
1874 Gerald Roth, "Evaluation of Array Syntax Dependence Analysis",
1875 Proceedings of the International Conference on Parallel and
1876 Distributed Processing Techniques and Applications (PDPTA2001),
1877 Las Vegas, Nevada, June 2001. */
1878 /* However, we need to be careful when either scalar expression
1879 contains a FORALL index, as these can potentially change value
1880 during the scalarization/traversal of this array reference. */
1881 if (contains_forall_index_p (r_start
) || contains_forall_index_p (l_start
))
1882 return GFC_DEP_OVERLAP
;
1885 return GFC_DEP_NODEP
;
1887 return GFC_DEP_EQUAL
;
1890 /* Callback function for checking if an expression depends on a
1891 dummy variable which is any other than INTENT(IN). */
1894 callback_dummy_intent_not_in (gfc_expr
**ep
,
1895 int *walk_subtrees ATTRIBUTE_UNUSED
,
1896 void *data ATTRIBUTE_UNUSED
)
1900 if (e
->expr_type
== EXPR_VARIABLE
&& e
->symtree
1901 && e
->symtree
->n
.sym
->attr
.dummy
)
1902 return e
->symtree
->n
.sym
->attr
.intent
!= INTENT_IN
;
1907 /* Auxiliary function to check if subexpressions have dummy variables which
1912 dummy_intent_not_in (gfc_expr
**ep
)
1914 return gfc_expr_walker (ep
, callback_dummy_intent_not_in
, NULL
);
1917 /* Determine if an array ref, usually an array section specifies the
1918 entire array. In addition, if the second, pointer argument is
1919 provided, the function will return true if the reference is
1920 contiguous; eg. (:, 1) gives true but (1,:) gives false.
1921 If one of the bounds depends on a dummy variable which is
1922 not INTENT(IN), also return false, because the user may
1923 have changed the variable. */
1926 gfc_full_array_ref_p (gfc_ref
*ref
, bool *contiguous
)
1930 bool lbound_OK
= true;
1931 bool ubound_OK
= true;
1934 *contiguous
= false;
1936 if (ref
->type
!= REF_ARRAY
)
1939 if (ref
->u
.ar
.type
== AR_FULL
)
1946 if (ref
->u
.ar
.type
!= AR_SECTION
)
1951 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
1953 /* If we have a single element in the reference, for the reference
1954 to be full, we need to ascertain that the array has a single
1955 element in this dimension and that we actually reference the
1957 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_ELEMENT
)
1959 /* This is unconditionally a contiguous reference if all the
1960 remaining dimensions are elements. */
1964 for (n
= i
+ 1; n
< ref
->u
.ar
.dimen
; n
++)
1965 if (ref
->u
.ar
.dimen_type
[n
] != DIMEN_ELEMENT
)
1966 *contiguous
= false;
1970 || !ref
->u
.ar
.as
->lower
[i
]
1971 || !ref
->u
.ar
.as
->upper
[i
]
1972 || gfc_dep_compare_expr (ref
->u
.ar
.as
->lower
[i
],
1973 ref
->u
.ar
.as
->upper
[i
])
1974 || !ref
->u
.ar
.start
[i
]
1975 || gfc_dep_compare_expr (ref
->u
.ar
.start
[i
],
1976 ref
->u
.ar
.as
->lower
[i
]))
1982 /* Check the lower bound. */
1983 if (ref
->u
.ar
.start
[i
]
1985 || !ref
->u
.ar
.as
->lower
[i
]
1986 || gfc_dep_compare_expr (ref
->u
.ar
.start
[i
],
1987 ref
->u
.ar
.as
->lower
[i
])
1988 || dummy_intent_not_in (&ref
->u
.ar
.start
[i
])))
1990 /* Check the upper bound. */
1991 if (ref
->u
.ar
.end
[i
]
1993 || !ref
->u
.ar
.as
->upper
[i
]
1994 || gfc_dep_compare_expr (ref
->u
.ar
.end
[i
],
1995 ref
->u
.ar
.as
->upper
[i
])
1996 || dummy_intent_not_in (&ref
->u
.ar
.end
[i
])))
1998 /* Check the stride. */
1999 if (ref
->u
.ar
.stride
[i
]
2000 && !gfc_expr_is_one (ref
->u
.ar
.stride
[i
], 0))
2003 /* This is unconditionally a contiguous reference as long as all
2004 the subsequent dimensions are elements. */
2008 for (n
= i
+ 1; n
< ref
->u
.ar
.dimen
; n
++)
2009 if (ref
->u
.ar
.dimen_type
[n
] != DIMEN_ELEMENT
)
2010 *contiguous
= false;
2013 if (!lbound_OK
|| !ubound_OK
)
2020 /* Determine if a full array is the same as an array section with one
2021 variable limit. For this to be so, the strides must both be unity
2022 and one of either start == lower or end == upper must be true. */
2025 ref_same_as_full_array (gfc_ref
*full_ref
, gfc_ref
*ref
)
2028 bool upper_or_lower
;
2030 if (full_ref
->type
!= REF_ARRAY
)
2032 if (full_ref
->u
.ar
.type
!= AR_FULL
)
2034 if (ref
->type
!= REF_ARRAY
)
2036 if (ref
->u
.ar
.type
== AR_FULL
)
2038 if (ref
->u
.ar
.type
!= AR_SECTION
)
2041 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
2043 /* If we have a single element in the reference, we need to check
2044 that the array has a single element and that we actually reference
2045 the correct element. */
2046 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_ELEMENT
)
2048 if (!full_ref
->u
.ar
.as
2049 || !full_ref
->u
.ar
.as
->lower
[i
]
2050 || !full_ref
->u
.ar
.as
->upper
[i
]
2051 || gfc_dep_compare_expr (full_ref
->u
.ar
.as
->lower
[i
],
2052 full_ref
->u
.ar
.as
->upper
[i
])
2053 || !ref
->u
.ar
.start
[i
]
2054 || gfc_dep_compare_expr (ref
->u
.ar
.start
[i
],
2055 full_ref
->u
.ar
.as
->lower
[i
]))
2059 /* Check the strides. */
2060 if (full_ref
->u
.ar
.stride
[i
] && !gfc_expr_is_one (full_ref
->u
.ar
.stride
[i
], 0))
2062 if (ref
->u
.ar
.stride
[i
] && !gfc_expr_is_one (ref
->u
.ar
.stride
[i
], 0))
2065 upper_or_lower
= false;
2066 /* Check the lower bound. */
2067 if (ref
->u
.ar
.start
[i
]
2069 && full_ref
->u
.ar
.as
->lower
[i
]
2070 && gfc_dep_compare_expr (ref
->u
.ar
.start
[i
],
2071 full_ref
->u
.ar
.as
->lower
[i
]) == 0))
2072 upper_or_lower
= true;
2073 /* Check the upper bound. */
2074 if (ref
->u
.ar
.end
[i
]
2076 && full_ref
->u
.ar
.as
->upper
[i
]
2077 && gfc_dep_compare_expr (ref
->u
.ar
.end
[i
],
2078 full_ref
->u
.ar
.as
->upper
[i
]) == 0))
2079 upper_or_lower
= true;
2080 if (!upper_or_lower
)
2087 /* Finds if two array references are overlapping or not.
2089 2 : array references are overlapping but reversal of one or
2090 more dimensions will clear the dependency.
2091 1 : array references are overlapping, or identical is true and
2092 there is some kind of overlap.
2093 0 : array references are identical or not overlapping. */
2096 gfc_dep_resolver (gfc_ref
*lref
, gfc_ref
*rref
, gfc_reverse
*reverse
,
2101 gfc_dependency fin_dep
;
2102 gfc_dependency this_dep
;
2103 bool same_component
= false;
2105 this_dep
= GFC_DEP_ERROR
;
2106 fin_dep
= GFC_DEP_ERROR
;
2107 /* Dependencies due to pointers should already have been identified.
2108 We only need to check for overlapping array references. */
2110 while (lref
&& rref
)
2112 /* The refs might come in mixed, one with a _data component and one
2113 without. Look at their next reference in order to avoid an
2116 if (lref
&& lref
->type
== REF_COMPONENT
&& lref
->u
.c
.component
2117 && strcmp (lref
->u
.c
.component
->name
, "_data") == 0)
2120 if (rref
&& rref
->type
== REF_COMPONENT
&& rref
->u
.c
.component
2121 && strcmp (rref
->u
.c
.component
->name
, "_data") == 0)
2124 /* We're resolving from the same base symbol, so both refs should be
2125 the same type. We traverse the reference chain until we find ranges
2126 that are not equal. */
2127 gcc_assert (lref
->type
== rref
->type
);
2131 /* The two ranges can't overlap if they are from different
2133 if (lref
->u
.c
.component
!= rref
->u
.c
.component
)
2136 same_component
= true;
2140 /* Substring overlaps are handled by the string assignment code
2141 if there is not an underlying dependency. */
2142 return (fin_dep
== GFC_DEP_OVERLAP
) ? 1 : 0;
2146 /* For now, treat all coarrays as dangerous. */
2147 if (lref
->u
.ar
.codimen
|| rref
->u
.ar
.codimen
)
2150 if (ref_same_as_full_array (lref
, rref
))
2153 if (ref_same_as_full_array (rref
, lref
))
2156 if (lref
->u
.ar
.dimen
!= rref
->u
.ar
.dimen
)
2158 if (lref
->u
.ar
.type
== AR_FULL
)
2159 fin_dep
= gfc_full_array_ref_p (rref
, NULL
) ? GFC_DEP_EQUAL
2161 else if (rref
->u
.ar
.type
== AR_FULL
)
2162 fin_dep
= gfc_full_array_ref_p (lref
, NULL
) ? GFC_DEP_EQUAL
2169 /* Index for the reverse array. */
2171 for (n
= 0; n
< lref
->u
.ar
.dimen
; n
++)
2173 /* Handle dependency when either of array reference is vector
2174 subscript. There is no dependency if the vector indices
2175 are equal or if indices are known to be different in a
2176 different dimension. */
2177 if (lref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
2178 || rref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
)
2180 if (lref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
2181 && rref
->u
.ar
.dimen_type
[n
] == DIMEN_VECTOR
2182 && gfc_dep_compare_expr (lref
->u
.ar
.start
[n
],
2183 rref
->u
.ar
.start
[n
]) == 0)
2184 this_dep
= GFC_DEP_EQUAL
;
2186 this_dep
= GFC_DEP_OVERLAP
;
2188 goto update_fin_dep
;
2191 if (lref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
2192 && rref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
)
2193 this_dep
= check_section_vs_section (&lref
->u
.ar
,
2195 else if (lref
->u
.ar
.dimen_type
[n
] == DIMEN_ELEMENT
2196 && rref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
)
2197 this_dep
= gfc_check_element_vs_section (lref
, rref
, n
);
2198 else if (rref
->u
.ar
.dimen_type
[n
] == DIMEN_ELEMENT
2199 && lref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
)
2200 this_dep
= gfc_check_element_vs_section (rref
, lref
, n
);
2203 gcc_assert (rref
->u
.ar
.dimen_type
[n
] == DIMEN_ELEMENT
2204 && lref
->u
.ar
.dimen_type
[n
] == DIMEN_ELEMENT
);
2205 this_dep
= gfc_check_element_vs_element (rref
, lref
, n
);
2206 if (identical
&& this_dep
== GFC_DEP_EQUAL
)
2207 this_dep
= GFC_DEP_OVERLAP
;
2210 /* If any dimension doesn't overlap, we have no dependency. */
2211 if (this_dep
== GFC_DEP_NODEP
)
2214 /* Now deal with the loop reversal logic: This only works on
2215 ranges and is activated by setting
2216 reverse[n] == GFC_ENABLE_REVERSE
2217 The ability to reverse or not is set by previous conditions
2218 in this dimension. If reversal is not activated, the
2219 value GFC_DEP_BACKWARD is reset to GFC_DEP_OVERLAP. */
2221 /* Get the indexing right for the scalarizing loop. If this
2222 is an element, there is no corresponding loop. */
2223 if (lref
->u
.ar
.dimen_type
[n
] != DIMEN_ELEMENT
)
2226 if (rref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
2227 && lref
->u
.ar
.dimen_type
[n
] == DIMEN_RANGE
)
2231 /* Reverse if backward dependence and not inhibited. */
2232 if (reverse
[m
] == GFC_ENABLE_REVERSE
2233 && this_dep
== GFC_DEP_BACKWARD
)
2234 reverse
[m
] = GFC_REVERSE_SET
;
2236 /* Forward if forward dependence and not inhibited. */
2237 if (reverse
[m
] == GFC_ENABLE_REVERSE
2238 && this_dep
== GFC_DEP_FORWARD
)
2239 reverse
[m
] = GFC_FORWARD_SET
;
2241 /* Flag up overlap if dependence not compatible with
2242 the overall state of the expression. */
2243 if (reverse
[m
] == GFC_REVERSE_SET
2244 && this_dep
== GFC_DEP_FORWARD
)
2246 reverse
[m
] = GFC_INHIBIT_REVERSE
;
2247 this_dep
= GFC_DEP_OVERLAP
;
2249 else if (reverse
[m
] == GFC_FORWARD_SET
2250 && this_dep
== GFC_DEP_BACKWARD
)
2252 reverse
[m
] = GFC_INHIBIT_REVERSE
;
2253 this_dep
= GFC_DEP_OVERLAP
;
2257 /* If no intention of reversing or reversing is explicitly
2258 inhibited, convert backward dependence to overlap. */
2259 if ((!reverse
&& this_dep
== GFC_DEP_BACKWARD
)
2260 || (reverse
&& reverse
[m
] == GFC_INHIBIT_REVERSE
))
2261 this_dep
= GFC_DEP_OVERLAP
;
2264 /* Overlap codes are in order of priority. We only need to
2265 know the worst one.*/
2268 if (identical
&& this_dep
== GFC_DEP_EQUAL
)
2269 this_dep
= GFC_DEP_OVERLAP
;
2271 if (this_dep
> fin_dep
)
2275 /* If this is an equal element, we have to keep going until we find
2276 the "real" array reference. */
2277 if (lref
->u
.ar
.type
== AR_ELEMENT
2278 && rref
->u
.ar
.type
== AR_ELEMENT
2279 && fin_dep
== GFC_DEP_EQUAL
)
2282 /* Exactly matching and forward overlapping ranges don't cause a
2284 if (fin_dep
< GFC_DEP_BACKWARD
&& !identical
)
2287 /* Keep checking. We only have a dependency if
2288 subsequent references also overlap. */
2292 if (lref
->u
.i
!= rref
->u
.i
)
2304 /* Assume the worst if we nest to different depths. */
2308 /* This can result from concatenation of assumed length string components. */
2309 if (same_component
&& fin_dep
== GFC_DEP_ERROR
)
2312 /* If we haven't seen any array refs then something went wrong. */
2313 gcc_assert (fin_dep
!= GFC_DEP_ERROR
);
2315 if (identical
&& fin_dep
!= GFC_DEP_NODEP
)
2318 return fin_dep
== GFC_DEP_OVERLAP
;