]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/dependency.c
2016-08-27 Steven G. Kargl <kargl@gcc.gnu.org>
[thirdparty/gcc.git] / gcc / fortran / dependency.c
CommitLineData
4ee9c684 1/* Dependency analysis
f1717362 2 Copyright (C) 2000-2016 Free Software Foundation, Inc.
4ee9c684 3 Contributed by Paul Brook <paul@nowt.org>
4
c84b470d 5This file is part of GCC.
4ee9c684 6
c84b470d 7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
bdabe786 9Software Foundation; either version 3, or (at your option) any later
c84b470d 10version.
4ee9c684 11
c84b470d 12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
4ee9c684 16
17You should have received a copy of the GNU General Public License
bdabe786 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
4ee9c684 20
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. */
9caa6670 25
4ee9c684 26#include "config.h"
dca58d21 27#include "system.h"
e4d6c7fc 28#include "coretypes.h"
4ee9c684 29#include "gfortran.h"
30#include "dependency.h"
126387b5 31#include "constructor.h"
80f5c112 32#include "arith.h"
4ee9c684 33
34/* static declarations */
35/* Enums */
36enum range {LHS, RHS, MID};
37
38/* Dependency types. These must be in reverse order of priority. */
6dc50383 39enum gfc_dependency
4ee9c684 40{
41 GFC_DEP_ERROR,
42 GFC_DEP_EQUAL, /* Identical Ranges. */
25f9f93d 43 GFC_DEP_FORWARD, /* e.g., a(1:3) = a(2:4). */
44 GFC_DEP_BACKWARD, /* e.g. a(2:4) = a(1:3). */
4ee9c684 45 GFC_DEP_OVERLAP, /* May overlap in some other way. */
46 GFC_DEP_NODEP /* Distinct ranges. */
6dc50383 47};
4ee9c684 48
49/* Macros */
50#define IS_ARRAY_EXPLICIT(as) ((as->type == AS_EXPLICIT ? 1 : 0))
51
cca7236e 52/* Forward declarations */
53
54static gfc_dependency check_section_vs_section (gfc_array_ref *,
55 gfc_array_ref *, int);
4ee9c684 56
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. */
59
60int
1a9745d2 61gfc_expr_is_one (gfc_expr *expr, int def)
4ee9c684 62{
22d678e8 63 gcc_assert (expr != NULL);
4ee9c684 64
65 if (expr->expr_type != EXPR_CONSTANT)
66 return def;
67
68 if (expr->ts.type != BT_INTEGER)
69 return def;
70
71 return mpz_cmp_si (expr->value.integer, 1) == 0;
72}
73
cca7236e 74/* Check if two array references are known to be identical. Calls
75 gfc_dep_compare_expr if necessary for comparing array indices. */
76
77static bool
78identical_array_ref (gfc_array_ref *a1, gfc_array_ref *a2)
79{
80 int i;
81
82 if (a1->type == AR_FULL && a2->type == AR_FULL)
83 return true;
84
85 if (a1->type == AR_SECTION && a2->type == AR_SECTION)
86 {
87 gcc_assert (a1->dimen == a2->dimen);
88
89 for ( i = 0; i < a1->dimen; i++)
90 {
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)
94 return false;
95
96 if (check_section_vs_section (a1, a2, i) != GFC_DEP_EQUAL)
97 return false;
98 }
99 return true;
100 }
101
102 if (a1->type == AR_ELEMENT && a2->type == AR_ELEMENT)
103 {
104 gcc_assert (a1->dimen == a2->dimen);
105 for (i = 0; i < a1->dimen; i++)
106 {
107 if (gfc_dep_compare_expr (a1->start[i], a2->start[i]) != 0)
108 return false;
109 }
110 return true;
111 }
112 return false;
113}
114
115
116
117/* Return true for identical variables, checking for references if
118 necessary. Calls identical_array_ref for checking array sections. */
119
9fcec394 120static bool
121are_identical_variables (gfc_expr *e1, gfc_expr *e2)
cca7236e 122{
123 gfc_ref *r1, *r2;
124
39c7c892 125 if (e1->symtree->n.sym->attr.dummy && e2->symtree->n.sym->attr.dummy)
126 {
127 /* Dummy arguments: Only check for equal names. */
128 if (e1->symtree->n.sym->name != e2->symtree->n.sym->name)
129 return false;
130 }
131 else
132 {
133 /* Check for equal symbols. */
134 if (e1->symtree->n.sym != e2->symtree->n.sym)
135 return false;
136 }
cca7236e 137
80f5c112 138 /* Volatile variables should never compare equal to themselves. */
139
140 if (e1->symtree->n.sym->attr.volatile_)
141 return false;
142
cca7236e 143 r1 = e1->ref;
144 r2 = e2->ref;
145
146 while (r1 != NULL || r2 != NULL)
147 {
148
149 /* Assume the variables are not equal if one has a reference and the
150 other doesn't.
151 TODO: Handle full references like comparing a(:) to a.
152 */
153
154 if (r1 == NULL || r2 == NULL)
155 return false;
156
157 if (r1->type != r2->type)
158 return false;
159
160 switch (r1->type)
161 {
162
163 case REF_ARRAY:
164 if (!identical_array_ref (&r1->u.ar, &r2->u.ar))
165 return false;
166
167 break;
168
169 case REF_COMPONENT:
170 if (r1->u.c.component != r2->u.c.component)
171 return false;
172 break;
173
174 case REF_SUBSTRING:
9bcaa3f0 175 if (gfc_dep_compare_expr (r1->u.ss.start, r2->u.ss.start) != 0)
cca7236e 176 return false;
9bcaa3f0 177
178 /* If both are NULL, the end length compares equal, because we
179 are looking at the same variable. This can only happen for
9caa6670 180 assumed- or deferred-length character arguments. */
9bcaa3f0 181
182 if (r1->u.ss.end == NULL && r2->u.ss.end == NULL)
183 break;
184
185 if (gfc_dep_compare_expr (r1->u.ss.end, r2->u.ss.end) != 0)
186 return false;
9caa6670 187
cca7236e 188 break;
189
190 default:
9fcec394 191 gfc_internal_error ("are_identical_variables: Bad type");
cca7236e 192 }
193 r1 = r1->next;
194 r2 = r2->next;
195 }
196 return true;
197}
4ee9c684 198
bf48f16e 199/* Compare two functions for equality. Returns 0 if e1==e2, -2 otherwise. If
200 impure_ok is false, only return 0 for pure functions. */
201
202int
203gfc_dep_compare_functions (gfc_expr *e1, gfc_expr *e2, bool impure_ok)
204{
205
206 gfc_actual_arglist *args1;
207 gfc_actual_arglist *args2;
9caa6670 208
bf48f16e 209 if (e1->expr_type != EXPR_FUNCTION || e2->expr_type != EXPR_FUNCTION)
210 return -2;
211
212 if ((e1->value.function.esym && e2->value.function.esym
213 && e1->value.function.esym == e2->value.function.esym
214 && (e1->value.function.esym->result->attr.pure || impure_ok))
215 || (e1->value.function.isym && e2->value.function.isym
216 && e1->value.function.isym == e2->value.function.isym
217 && (e1->value.function.isym->pure || impure_ok)))
218 {
219 args1 = e1->value.function.actual;
220 args2 = e2->value.function.actual;
221
222 /* Compare the argument lists for equality. */
223 while (args1 && args2)
224 {
225 /* Bitwise xor, since C has no non-bitwise xor operator. */
226 if ((args1->expr == NULL) ^ (args2->expr == NULL))
227 return -2;
9caa6670 228
bf48f16e 229 if (args1->expr != NULL && args2->expr != NULL
230 && gfc_dep_compare_expr (args1->expr, args2->expr) != 0)
231 return -2;
9caa6670 232
bf48f16e 233 args1 = args1->next;
234 args2 = args2->next;
235 }
236 return (args1 || args2) ? -2 : 0;
237 }
238 else
9caa6670 239 return -2;
bf48f16e 240}
241
02e6443e 242/* Helper function to look through parens, unary plus and widening
243 integer conversions. */
244
a03c61f8 245gfc_expr *
246gfc_discard_nops (gfc_expr *e)
02e6443e 247{
248 gfc_actual_arglist *arglist;
249
250 if (e == NULL)
251 return NULL;
252
253 while (true)
254 {
255 if (e->expr_type == EXPR_OP
256 && (e->value.op.op == INTRINSIC_UPLUS
257 || e->value.op.op == INTRINSIC_PARENTHESES))
258 {
259 e = e->value.op.op1;
260 continue;
261 }
262
263 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
264 && e->value.function.isym->id == GFC_ISYM_CONVERSION
265 && e->ts.type == BT_INTEGER)
266 {
267 arglist = e->value.function.actual;
268 if (arglist->expr->ts.type == BT_INTEGER
269 && e->ts.kind > arglist->expr->ts.kind)
270 {
271 e = arglist->expr;
272 continue;
273 }
274 }
275 break;
276 }
277
278 return e;
279}
280
281
61bc1860 282/* Compare two expressions. Return values:
283 * +1 if e1 > e2
284 * 0 if e1 == e2
285 * -1 if e1 < e2
286 * -2 if the relationship could not be determined
013ac408 287 * -3 if e1 /= e2, but we cannot tell which one is larger.
288 REAL and COMPLEX constants are only compared for equality
289 or inequality; if they are unequal, -2 is returned in all cases. */
4ee9c684 290
291int
1a9745d2 292gfc_dep_compare_expr (gfc_expr *e1, gfc_expr *e2)
4ee9c684 293{
294 int i;
1c0efcba 295
a9f097cb 296 if (e1 == NULL && e2 == NULL)
297 return 0;
298
a03c61f8 299 e1 = gfc_discard_nops (e1);
300 e2 = gfc_discard_nops (e2);
4d4677fd 301
dcb1b019 302 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_PLUS)
4d4677fd 303 {
013ac408 304 /* Compare X+C vs. X, for INTEGER only. */
4d4677fd 305 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
306 && e1->value.op.op2->ts.type == BT_INTEGER
307 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
308 return mpz_sgn (e1->value.op.op2->value.integer);
309
310 /* Compare P+Q vs. R+S. */
dcb1b019 311 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
4d4677fd 312 {
313 int l, r;
314
315 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
316 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
317 if (l == 0 && r == 0)
318 return 0;
61bc1860 319 if (l == 0 && r > -2)
4d4677fd 320 return r;
61bc1860 321 if (l > -2 && r == 0)
4d4677fd 322 return l;
323 if (l == 1 && r == 1)
324 return 1;
325 if (l == -1 && r == -1)
326 return -1;
327
328 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op2);
329 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op1);
330 if (l == 0 && r == 0)
331 return 0;
61bc1860 332 if (l == 0 && r > -2)
4d4677fd 333 return r;
61bc1860 334 if (l > -2 && r == 0)
4d4677fd 335 return l;
336 if (l == 1 && r == 1)
337 return 1;
338 if (l == -1 && r == -1)
339 return -1;
340 }
341 }
342
013ac408 343 /* Compare X vs. X+C, for INTEGER only. */
dcb1b019 344 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
4d4677fd 345 {
346 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
347 && e2->value.op.op2->ts.type == BT_INTEGER
348 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
349 return -mpz_sgn (e2->value.op.op2->value.integer);
350 }
351
013ac408 352 /* Compare X-C vs. X, for INTEGER only. */
dcb1b019 353 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_MINUS)
4d4677fd 354 {
355 if (e1->value.op.op2->expr_type == EXPR_CONSTANT
356 && e1->value.op.op2->ts.type == BT_INTEGER
357 && gfc_dep_compare_expr (e1->value.op.op1, e2) == 0)
358 return -mpz_sgn (e1->value.op.op2->value.integer);
359
360 /* Compare P-Q vs. R-S. */
dcb1b019 361 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
4d4677fd 362 {
363 int l, r;
364
365 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
366 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
367 if (l == 0 && r == 0)
368 return 0;
61bc1860 369 if (l > -2 && r == 0)
4d4677fd 370 return l;
61bc1860 371 if (l == 0 && r > -2)
4d4677fd 372 return -r;
373 if (l == 1 && r == -1)
374 return 1;
375 if (l == -1 && r == 1)
376 return -1;
377 }
378 }
379
80f5c112 380 /* Compare A // B vs. C // D. */
381
382 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_CONCAT
383 && e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_CONCAT)
384 {
385 int l, r;
386
387 l = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
388 r = gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2);
389
b34ed2ef 390 if (l != 0)
61bc1860 391 return l;
80f5c112 392
b34ed2ef 393 /* Left expressions of // compare equal, but
394 watch out for 'A ' // x vs. 'A' // x. */
395 gfc_expr *e1_left = e1->value.op.op1;
396 gfc_expr *e2_left = e2->value.op.op1;
397
398 if (e1_left->expr_type == EXPR_CONSTANT
399 && e2_left->expr_type == EXPR_CONSTANT
400 && e1_left->value.character.length
401 != e2_left->value.character.length)
402 return -2;
80f5c112 403 else
b34ed2ef 404 return r;
80f5c112 405 }
406
013ac408 407 /* Compare X vs. X-C, for INTEGER only. */
dcb1b019 408 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
4d4677fd 409 {
410 if (e2->value.op.op2->expr_type == EXPR_CONSTANT
411 && e2->value.op.op2->ts.type == BT_INTEGER
412 && gfc_dep_compare_expr (e1, e2->value.op.op1) == 0)
413 return mpz_sgn (e2->value.op.op2->value.integer);
414 }
415
4ee9c684 416 if (e1->expr_type != e2->expr_type)
61bc1860 417 return -3;
4ee9c684 418
419 switch (e1->expr_type)
420 {
421 case EXPR_CONSTANT:
80f5c112 422 /* Compare strings for equality. */
423 if (e1->ts.type == BT_CHARACTER && e2->ts.type == BT_CHARACTER)
424 return gfc_compare_string (e1, e2);
425
013ac408 426 /* Compare REAL and COMPLEX constants. Because of the
427 traps and pitfalls associated with comparing
428 a + 1.0 with a + 0.5, check for equality only. */
429 if (e2->expr_type == EXPR_CONSTANT)
430 {
431 if (e1->ts.type == BT_REAL && e2->ts.type == BT_REAL)
432 {
433 if (mpfr_cmp (e1->value.real, e2->value.real) == 0)
434 return 0;
435 else
436 return -2;
437 }
438 else if (e1->ts.type == BT_COMPLEX && e2->ts.type == BT_COMPLEX)
439 {
440 if (mpc_cmp (e1->value.complex, e2->value.complex) == 0)
441 return 0;
442 else
443 return -2;
444 }
445 }
446
4ee9c684 447 if (e1->ts.type != BT_INTEGER || e2->ts.type != BT_INTEGER)
448 return -2;
449
013ac408 450 /* For INTEGER, all cases where e2 is not constant should have
451 been filtered out above. */
452 gcc_assert (e2->expr_type == EXPR_CONSTANT);
453
4ee9c684 454 i = mpz_cmp (e1->value.integer, e2->value.integer);
455 if (i == 0)
456 return 0;
457 else if (i < 0)
458 return -1;
459 return 1;
460
461 case EXPR_VARIABLE:
9fcec394 462 if (are_identical_variables (e1, e2))
4ee9c684 463 return 0;
cca7236e 464 else
61bc1860 465 return -3;
4ee9c684 466
bee621f2 467 case EXPR_OP:
468 /* Intrinsic operators are the same if their operands are the same. */
dcb1b019 469 if (e1->value.op.op != e2->value.op.op)
bee621f2 470 return -2;
471 if (e1->value.op.op2 == 0)
472 {
473 i = gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1);
474 return i == 0 ? 0 : -2;
475 }
476 if (gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op1) == 0
477 && gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op2) == 0)
478 return 0;
9fcec394 479 else if (e1->value.op.op == INTRINSIC_TIMES
480 && gfc_dep_compare_expr (e1->value.op.op1, e2->value.op.op2) == 0
481 && gfc_dep_compare_expr (e1->value.op.op2, e2->value.op.op1) == 0)
013ac408 482 /* Commutativity of multiplication; addition is handled above. */
9fcec394 483 return 0;
484
bee621f2 485 return -2;
486
487 case EXPR_FUNCTION:
bf48f16e 488 return gfc_dep_compare_functions (e1, e2, false);
f33bc6d4 489 break;
bee621f2 490
4ee9c684 491 default:
492 return -2;
493 }
494}
495
496
a642efd6 497/* Return the difference between two expressions. Integer expressions of
9caa6670 498 the form
a642efd6 499
500 X + constant, X - constant and constant + X
501
502 are handled. Return true on success, false on failure. result is assumed
503 to be uninitialized on entry, and will be initialized on success.
504*/
505
506bool
507gfc_dep_difference (gfc_expr *e1, gfc_expr *e2, mpz_t *result)
508{
509 gfc_expr *e1_op1, *e1_op2, *e2_op1, *e2_op2;
510
511 if (e1 == NULL || e2 == NULL)
512 return false;
513
514 if (e1->ts.type != BT_INTEGER || e2->ts.type != BT_INTEGER)
515 return false;
516
a03c61f8 517 e1 = gfc_discard_nops (e1);
518 e2 = gfc_discard_nops (e2);
a642efd6 519
520 /* Inizialize tentatively, clear if we don't return anything. */
521 mpz_init (*result);
522
523 /* Case 1: c1 - c2 = c1 - c2, trivially. */
524
525 if (e1->expr_type == EXPR_CONSTANT && e2->expr_type == EXPR_CONSTANT)
526 {
527 mpz_sub (*result, e1->value.integer, e2->value.integer);
528 return true;
529 }
530
531 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_PLUS)
532 {
a03c61f8 533 e1_op1 = gfc_discard_nops (e1->value.op.op1);
534 e1_op2 = gfc_discard_nops (e1->value.op.op2);
a642efd6 535
536 /* Case 2: (X + c1) - X = c1. */
537 if (e1_op2->expr_type == EXPR_CONSTANT
538 && gfc_dep_compare_expr (e1_op1, e2) == 0)
539 {
540 mpz_set (*result, e1_op2->value.integer);
541 return true;
542 }
543
293d72e0 544 /* Case 3: (c1 + X) - X = c1. */
a642efd6 545 if (e1_op1->expr_type == EXPR_CONSTANT
546 && gfc_dep_compare_expr (e1_op2, e2) == 0)
547 {
548 mpz_set (*result, e1_op1->value.integer);
549 return true;
550 }
551
552 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
553 {
a03c61f8 554 e2_op1 = gfc_discard_nops (e2->value.op.op1);
555 e2_op2 = gfc_discard_nops (e2->value.op.op2);
a642efd6 556
557 if (e1_op2->expr_type == EXPR_CONSTANT)
558 {
559 /* Case 4: X + c1 - (X + c2) = c1 - c2. */
560 if (e2_op2->expr_type == EXPR_CONSTANT
561 && gfc_dep_compare_expr (e1_op1, e2_op1) == 0)
562 {
563 mpz_sub (*result, e1_op2->value.integer,
564 e2_op2->value.integer);
565 return true;
566 }
567 /* Case 5: X + c1 - (c2 + X) = c1 - c2. */
568 if (e2_op1->expr_type == EXPR_CONSTANT
569 && gfc_dep_compare_expr (e1_op1, e2_op2) == 0)
570 {
571 mpz_sub (*result, e1_op2->value.integer,
572 e2_op1->value.integer);
573 return true;
574 }
575 }
576 else if (e1_op1->expr_type == EXPR_CONSTANT)
577 {
578 /* Case 6: c1 + X - (X + c2) = c1 - c2. */
579 if (e2_op2->expr_type == EXPR_CONSTANT
580 && gfc_dep_compare_expr (e1_op2, e2_op1) == 0)
581 {
582 mpz_sub (*result, e1_op1->value.integer,
583 e2_op2->value.integer);
584 return true;
585 }
586 /* Case 7: c1 + X - (c2 + X) = c1 - c2. */
587 if (e2_op1->expr_type == EXPR_CONSTANT
588 && gfc_dep_compare_expr (e1_op2, e2_op2) == 0)
589 {
590 mpz_sub (*result, e1_op1->value.integer,
591 e2_op1->value.integer);
592 return true;
593 }
594 }
595 }
596
597 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
598 {
a03c61f8 599 e2_op1 = gfc_discard_nops (e2->value.op.op1);
600 e2_op2 = gfc_discard_nops (e2->value.op.op2);
a642efd6 601
602 if (e1_op2->expr_type == EXPR_CONSTANT)
603 {
604 /* Case 8: X + c1 - (X - c2) = c1 + c2. */
605 if (e2_op2->expr_type == EXPR_CONSTANT
606 && gfc_dep_compare_expr (e1_op1, e2_op1) == 0)
607 {
608 mpz_add (*result, e1_op2->value.integer,
609 e2_op2->value.integer);
610 return true;
611 }
612 }
613 if (e1_op1->expr_type == EXPR_CONSTANT)
614 {
615 /* Case 9: c1 + X - (X - c2) = c1 + c2. */
616 if (e2_op2->expr_type == EXPR_CONSTANT
617 && gfc_dep_compare_expr (e1_op2, e2_op1) == 0)
618 {
619 mpz_add (*result, e1_op1->value.integer,
620 e2_op2->value.integer);
621 return true;
622 }
623 }
624 }
625 }
626
627 if (e1->expr_type == EXPR_OP && e1->value.op.op == INTRINSIC_MINUS)
628 {
a03c61f8 629 e1_op1 = gfc_discard_nops (e1->value.op.op1);
630 e1_op2 = gfc_discard_nops (e1->value.op.op2);
a642efd6 631
632 if (e1_op2->expr_type == EXPR_CONSTANT)
633 {
634 /* Case 10: (X - c1) - X = -c1 */
635
636 if (gfc_dep_compare_expr (e1_op1, e2) == 0)
637 {
638 mpz_neg (*result, e1_op2->value.integer);
639 return true;
640 }
641
642 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
643 {
a03c61f8 644 e2_op1 = gfc_discard_nops (e2->value.op.op1);
645 e2_op2 = gfc_discard_nops (e2->value.op.op2);
a642efd6 646
647 /* Case 11: (X - c1) - (X + c2) = -( c1 + c2). */
648 if (e2_op2->expr_type == EXPR_CONSTANT
649 && gfc_dep_compare_expr (e1_op1, e2_op1) == 0)
650 {
651 mpz_add (*result, e1_op2->value.integer,
652 e2_op2->value.integer);
653 mpz_neg (*result, *result);
654 return true;
655 }
656
657 /* Case 12: X - c1 - (c2 + X) = - (c1 + c2). */
658 if (e2_op1->expr_type == EXPR_CONSTANT
659 && gfc_dep_compare_expr (e1_op1, e2_op2) == 0)
660 {
661 mpz_add (*result, e1_op2->value.integer,
662 e2_op1->value.integer);
663 mpz_neg (*result, *result);
664 return true;
665 }
666 }
667
668 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
669 {
a03c61f8 670 e2_op1 = gfc_discard_nops (e2->value.op.op1);
671 e2_op2 = gfc_discard_nops (e2->value.op.op2);
a642efd6 672
673 /* Case 13: (X - c1) - (X - c2) = c2 - c1. */
674 if (e2_op2->expr_type == EXPR_CONSTANT
675 && gfc_dep_compare_expr (e1_op1, e2_op1) == 0)
676 {
677 mpz_sub (*result, e2_op2->value.integer,
678 e1_op2->value.integer);
679 return true;
680 }
681 }
682 }
683 if (e1_op1->expr_type == EXPR_CONSTANT)
684 {
685 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
686 {
a03c61f8 687 e2_op1 = gfc_discard_nops (e2->value.op.op1);
688 e2_op2 = gfc_discard_nops (e2->value.op.op2);
9caa6670 689
a642efd6 690 /* Case 14: (c1 - X) - (c2 - X) == c1 - c2. */
691 if (gfc_dep_compare_expr (e1_op2, e2_op2) == 0)
692 {
693 mpz_sub (*result, e1_op1->value.integer,
694 e2_op1->value.integer);
695 return true;
696 }
697 }
698
699 }
700 }
701
702 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_PLUS)
703 {
a03c61f8 704 e2_op1 = gfc_discard_nops (e2->value.op.op1);
705 e2_op2 = gfc_discard_nops (e2->value.op.op2);
a642efd6 706
707 /* Case 15: X - (X + c2) = -c2. */
708 if (e2_op2->expr_type == EXPR_CONSTANT
709 && gfc_dep_compare_expr (e1, e2_op1) == 0)
710 {
711 mpz_neg (*result, e2_op2->value.integer);
712 return true;
713 }
714 /* Case 16: X - (c2 + X) = -c2. */
715 if (e2_op1->expr_type == EXPR_CONSTANT
716 && gfc_dep_compare_expr (e1, e2_op2) == 0)
717 {
718 mpz_neg (*result, e2_op1->value.integer);
719 return true;
720 }
721 }
722
723 if (e2->expr_type == EXPR_OP && e2->value.op.op == INTRINSIC_MINUS)
724 {
a03c61f8 725 e2_op1 = gfc_discard_nops (e2->value.op.op1);
726 e2_op2 = gfc_discard_nops (e2->value.op.op2);
a642efd6 727
728 /* Case 17: X - (X - c2) = c2. */
729 if (e2_op2->expr_type == EXPR_CONSTANT
730 && gfc_dep_compare_expr (e1, e2_op1) == 0)
731 {
732 mpz_set (*result, e2_op2->value.integer);
733 return true;
734 }
735 }
736
60e19868 737 if (gfc_dep_compare_expr (e1, e2) == 0)
a642efd6 738 {
739 /* Case 18: X - X = 0. */
740 mpz_set_si (*result, 0);
741 return true;
742 }
743
744 mpz_clear (*result);
745 return false;
746}
747
9fcec394 748/* Returns 1 if the two ranges are the same and 0 if they are not (or if the
749 results are indeterminate). 'n' is the dimension to compare. */
4ee9c684 750
9fcec394 751static int
752is_same_range (gfc_array_ref *ar1, gfc_array_ref *ar2, int n)
4ee9c684 753{
754 gfc_expr *e1;
755 gfc_expr *e2;
756 int i;
757
758 /* TODO: More sophisticated range comparison. */
22d678e8 759 gcc_assert (ar1 && ar2);
4ee9c684 760
22d678e8 761 gcc_assert (ar1->dimen_type[n] == ar2->dimen_type[n]);
4ee9c684 762
763 e1 = ar1->stride[n];
764 e2 = ar2->stride[n];
765 /* Check for mismatching strides. A NULL stride means a stride of 1. */
766 if (e1 && !e2)
767 {
768 i = gfc_expr_is_one (e1, -1);
9fcec394 769 if (i == -1 || i == 0)
4ee9c684 770 return 0;
771 }
772 else if (e2 && !e1)
773 {
774 i = gfc_expr_is_one (e2, -1);
9fcec394 775 if (i == -1 || i == 0)
4ee9c684 776 return 0;
777 }
778 else if (e1 && e2)
779 {
780 i = gfc_dep_compare_expr (e1, e2);
9fcec394 781 if (i != 0)
4ee9c684 782 return 0;
783 }
784 /* The strides match. */
785
786 /* Check the range start. */
787 e1 = ar1->start[n];
788 e2 = ar2->start[n];
a7455f80 789 if (e1 || e2)
790 {
791 /* Use the bound of the array if no bound is specified. */
792 if (ar1->as && !e1)
793 e1 = ar1->as->lower[n];
4ee9c684 794
a7455f80 795 if (ar2->as && !e2)
796 e2 = ar2->as->lower[n];
4ee9c684 797
a7455f80 798 /* Check we have values for both. */
799 if (!(e1 && e2))
9fcec394 800 return 0;
4ee9c684 801
a7455f80 802 i = gfc_dep_compare_expr (e1, e2);
9fcec394 803 if (i != 0)
a7455f80 804 return 0;
805 }
4ee9c684 806
a7455f80 807 /* Check the range end. */
808 e1 = ar1->end[n];
809 e2 = ar2->end[n];
810 if (e1 || e2)
811 {
812 /* Use the bound of the array if no bound is specified. */
813 if (ar1->as && !e1)
814 e1 = ar1->as->upper[n];
4ee9c684 815
a7455f80 816 if (ar2->as && !e2)
817 e2 = ar2->as->upper[n];
4ee9c684 818
a7455f80 819 /* Check we have values for both. */
820 if (!(e1 && e2))
9fcec394 821 return 0;
a7455f80 822
823 i = gfc_dep_compare_expr (e1, e2);
9fcec394 824 if (i != 0)
a7455f80 825 return 0;
826 }
827
828 return 1;
4ee9c684 829}
830
831
018ef8b8 832/* Some array-returning intrinsics can be implemented by reusing the
22046c26 833 data from one of the array arguments. For example, TRANSPOSE does
018ef8b8 834 not necessarily need to allocate new data: it can be implemented
835 by copying the original array's descriptor and simply swapping the
836 two dimension specifications.
837
838 If EXPR is a call to such an intrinsic, return the argument
839 whose data can be reused, otherwise return NULL. */
840
841gfc_expr *
1a9745d2 842gfc_get_noncopying_intrinsic_argument (gfc_expr *expr)
018ef8b8 843{
844 if (expr->expr_type != EXPR_FUNCTION || !expr->value.function.isym)
845 return NULL;
846
55cb4417 847 switch (expr->value.function.isym->id)
018ef8b8 848 {
849 case GFC_ISYM_TRANSPOSE:
850 return expr->value.function.actual->expr;
851
852 default:
853 return NULL;
854 }
855}
856
857
c99d633f 858/* Return true if the result of reference REF can only be constructed
859 using a temporary array. */
860
861bool
862gfc_ref_needs_temporary_p (gfc_ref *ref)
863{
864 int n;
865 bool subarray_p;
866
867 subarray_p = false;
868 for (; ref; ref = ref->next)
869 switch (ref->type)
870 {
871 case REF_ARRAY:
872 /* Vector dimensions are generally not monotonic and must be
873 handled using a temporary. */
874 if (ref->u.ar.type == AR_SECTION)
875 for (n = 0; n < ref->u.ar.dimen; n++)
876 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR)
877 return true;
878
879 subarray_p = true;
880 break;
881
882 case REF_SUBSTRING:
883 /* Within an array reference, character substrings generally
884 need a temporary. Character array strides are expressed as
885 multiples of the element size (consistent with other array
886 types), not in characters. */
887 return subarray_p;
888
889 case REF_COMPONENT:
890 break;
891 }
892
893 return false;
894}
895
896
f10a970e 897static int
0ed083d9 898gfc_is_data_pointer (gfc_expr *e)
899{
900 gfc_ref *ref;
901
1b922cfc 902 if (e->expr_type != EXPR_VARIABLE && e->expr_type != EXPR_FUNCTION)
0ed083d9 903 return 0;
904
1b922cfc 905 /* No subreference if it is a function */
906 gcc_assert (e->expr_type == EXPR_VARIABLE || !e->ref);
907
0ed083d9 908 if (e->symtree->n.sym->attr.pointer)
909 return 1;
1b922cfc 910
0ed083d9 911 for (ref = e->ref; ref; ref = ref->next)
912 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
913 return 1;
914
915 return 0;
916}
917
918
018ef8b8 919/* Return true if array variable VAR could be passed to the same function
920 as argument EXPR without interfering with EXPR. INTENT is the intent
921 of VAR.
922
923 This is considerably less conservative than other dependencies
924 because many function arguments will already be copied into a
925 temporary. */
926
927static int
1a9745d2 928gfc_check_argument_var_dependency (gfc_expr *var, sym_intent intent,
74e83bb9 929 gfc_expr *expr, gfc_dep_check elemental)
018ef8b8 930{
74e83bb9 931 gfc_expr *arg;
932
018ef8b8 933 gcc_assert (var->expr_type == EXPR_VARIABLE);
934 gcc_assert (var->rank > 0);
935
936 switch (expr->expr_type)
937 {
938 case EXPR_VARIABLE:
9caa6670 939 /* In case of elemental subroutines, there is no dependency
74e83bb9 940 between two same-range array references. */
941 if (gfc_ref_needs_temporary_p (expr->ref)
08803898 942 || gfc_check_dependency (var, expr, elemental == NOT_ELEMENTAL))
74e83bb9 943 {
9ed3ad48 944 if (elemental == ELEM_DONT_CHECK_VARIABLE)
74e83bb9 945 {
9ed3ad48 946 /* Too many false positive with pointers. */
947 if (!gfc_is_data_pointer (var) && !gfc_is_data_pointer (expr))
948 {
9caa6670 949 /* Elemental procedures forbid unspecified intents,
9ed3ad48 950 and we don't check dependencies for INTENT_IN args. */
951 gcc_assert (intent == INTENT_OUT || intent == INTENT_INOUT);
952
9caa6670 953 /* We are told not to check dependencies.
9ed3ad48 954 We do it, however, and issue a warning in case we find one.
9caa6670 955 If a dependency is found in the case
9ed3ad48 956 elemental == ELEM_CHECK_VARIABLE, we will generate
957 a temporary, so we don't need to bother the user. */
389a9009 958 gfc_warning (0, "INTENT(%s) actual argument at %L might "
9caa6670 959 "interfere with actual argument at %L.",
960 intent == INTENT_OUT ? "OUT" : "INOUT",
9ed3ad48 961 &var->where, &expr->where);
962 }
74e83bb9 963 return 0;
964 }
965 else
9caa6670 966 return 1;
74e83bb9 967 }
968 return 0;
018ef8b8 969
970 case EXPR_ARRAY:
14460286 971 /* the scalarizer always generates a temporary for array constructors,
972 so there is no dependency. */
973 return 0;
018ef8b8 974
975 case EXPR_FUNCTION:
1274527b 976 if (intent != INTENT_IN)
977 {
978 arg = gfc_get_noncopying_intrinsic_argument (expr);
979 if (arg != NULL)
980 return gfc_check_argument_var_dependency (var, intent, arg,
981 NOT_ELEMENTAL);
982 }
983
984 if (elemental != NOT_ELEMENTAL)
74e83bb9 985 {
986 if ((expr->value.function.esym
987 && expr->value.function.esym->attr.elemental)
988 || (expr->value.function.isym
989 && expr->value.function.isym->elemental))
990 return gfc_check_fncall_dependency (var, intent, NULL,
991 expr->value.function.actual,
992 ELEM_CHECK_VARIABLE);
efeb2b16 993
994 if (gfc_inline_intrinsic_function_p (expr))
995 {
996 /* The TRANSPOSE case should have been caught in the
997 noncopying intrinsic case above. */
998 gcc_assert (expr->value.function.isym->id != GFC_ISYM_TRANSPOSE);
999
1000 return gfc_check_fncall_dependency (var, intent, NULL,
1001 expr->value.function.actual,
1002 ELEM_CHECK_VARIABLE);
1003 }
74e83bb9 1004 }
1005 return 0;
1006
1007 case EXPR_OP:
1008 /* In case of non-elemental procedures, there is no need to catch
1009 dependencies, as we will make a temporary anyway. */
1010 if (elemental)
018ef8b8 1011 {
9caa6670 1012 /* If the actual arg EXPR is an expression, we need to catch
1013 a dependency between variables in EXPR and VAR,
74e83bb9 1014 an intent((IN)OUT) variable. */
1015 if (expr->value.op.op1
9caa6670 1016 && gfc_check_argument_var_dependency (var, intent,
1017 expr->value.op.op1,
74e83bb9 1018 ELEM_CHECK_VARIABLE))
1019 return 1;
1020 else if (expr->value.op.op2
9caa6670 1021 && gfc_check_argument_var_dependency (var, intent,
1022 expr->value.op.op2,
74e83bb9 1023 ELEM_CHECK_VARIABLE))
1024 return 1;
018ef8b8 1025 }
1026 return 0;
1027
1028 default:
1029 return 0;
1030 }
1031}
9caa6670 1032
1033
018ef8b8 1034/* Like gfc_check_argument_var_dependency, but extended to any
1035 array expression OTHER, not just variables. */
1036
1037static int
1a9745d2 1038gfc_check_argument_dependency (gfc_expr *other, sym_intent intent,
74e83bb9 1039 gfc_expr *expr, gfc_dep_check elemental)
018ef8b8 1040{
1041 switch (other->expr_type)
1042 {
1043 case EXPR_VARIABLE:
74e83bb9 1044 return gfc_check_argument_var_dependency (other, intent, expr, elemental);
018ef8b8 1045
1046 case EXPR_FUNCTION:
1274527b 1047 other = gfc_get_noncopying_intrinsic_argument (other);
1048 if (other != NULL)
1049 return gfc_check_argument_dependency (other, INTENT_IN, expr,
1050 NOT_ELEMENTAL);
1051
018ef8b8 1052 return 0;
1053
1054 default:
1055 return 0;
1056 }
1057}
1058
1059
1060/* Like gfc_check_argument_dependency, but check all the arguments in ACTUAL.
1061 FNSYM is the function being called, or NULL if not known. */
4ee9c684 1062
1063int
1a9745d2 1064gfc_check_fncall_dependency (gfc_expr *other, sym_intent intent,
74e83bb9 1065 gfc_symbol *fnsym, gfc_actual_arglist *actual,
1066 gfc_dep_check elemental)
4ee9c684 1067{
018ef8b8 1068 gfc_formal_arglist *formal;
4ee9c684 1069 gfc_expr *expr;
4ee9c684 1070
6777213b 1071 formal = fnsym ? gfc_sym_get_dummy_args (fnsym) : NULL;
018ef8b8 1072 for (; actual; actual = actual->next, formal = formal ? formal->next : NULL)
4ee9c684 1073 {
1074 expr = actual->expr;
1075
1076 /* Skip args which are not present. */
1077 if (!expr)
1078 continue;
9960dc89 1079
1080 /* Skip other itself. */
1081 if (expr == other)
1082 continue;
4ee9c684 1083
018ef8b8 1084 /* Skip intent(in) arguments if OTHER itself is intent(in). */
1a9745d2 1085 if (formal && intent == INTENT_IN
018ef8b8 1086 && formal->sym->attr.intent == INTENT_IN)
1087 continue;
1088
74e83bb9 1089 if (gfc_check_argument_dependency (other, intent, expr, elemental))
018ef8b8 1090 return 1;
4ee9c684 1091 }
1092
1093 return 0;
1094}
1095
1096
0b5dc8b5 1097/* Return 1 if e1 and e2 are equivalenced arrays, either
69b1505f 1098 directly or indirectly; i.e., equivalence (a,b) for a and b
0b5dc8b5 1099 or equivalence (a,c),(b,c). This function uses the equiv_
1100 lists, generated in trans-common(add_equivalences), that are
78787e4b 1101 guaranteed to pick up indirect equivalences. We explicitly
1102 check for overlap using the offset and length of the equivalence.
1103 This function is symmetric.
1104 TODO: This function only checks whether the full top-level
1105 symbols overlap. An improved implementation could inspect
1106 e1->ref and e2->ref to determine whether the actually accessed
1107 portions of these variables/arrays potentially overlap. */
0b5dc8b5 1108
1109int
1110gfc_are_equivalenced_arrays (gfc_expr *e1, gfc_expr *e2)
1111{
1112 gfc_equiv_list *l;
1113 gfc_equiv_info *s, *fl1, *fl2;
1114
1115 gcc_assert (e1->expr_type == EXPR_VARIABLE
1a9745d2 1116 && e2->expr_type == EXPR_VARIABLE);
0b5dc8b5 1117
1118 if (!e1->symtree->n.sym->attr.in_equivalence
1a9745d2 1119 || !e2->symtree->n.sym->attr.in_equivalence|| !e1->rank || !e2->rank)
0b5dc8b5 1120 return 0;
1121
d60fed4c 1122 if (e1->symtree->n.sym->ns
1123 && e1->symtree->n.sym->ns != gfc_current_ns)
1124 l = e1->symtree->n.sym->ns->equiv_lists;
1125 else
1126 l = gfc_current_ns->equiv_lists;
1127
0b5dc8b5 1128 /* Go through the equiv_lists and return 1 if the variables
1129 e1 and e2 are members of the same group and satisfy the
1130 requirement on their relative offsets. */
d60fed4c 1131 for (; l; l = l->next)
0b5dc8b5 1132 {
1133 fl1 = NULL;
1134 fl2 = NULL;
1135 for (s = l->equiv; s; s = s->next)
1136 {
1137 if (s->sym == e1->symtree->n.sym)
78787e4b 1138 {
1139 fl1 = s;
1140 if (fl2)
1141 break;
1142 }
0b5dc8b5 1143 if (s->sym == e2->symtree->n.sym)
78787e4b 1144 {
1145 fl2 = s;
1146 if (fl1)
1147 break;
1148 }
1149 }
1150
1151 if (s)
1152 {
1153 /* Can these lengths be zero? */
1154 if (fl1->length <= 0 || fl2->length <= 0)
1155 return 1;
9caa6670 1156 /* These can't overlap if [f11,fl1+length] is before
78787e4b 1157 [fl2,fl2+length], or [fl2,fl2+length] is before
1158 [fl1,fl1+length], otherwise they do overlap. */
1159 if (fl1->offset + fl1->length > fl2->offset
1160 && fl2->offset + fl2->length > fl1->offset)
0b5dc8b5 1161 return 1;
1162 }
1163 }
78787e4b 1164 return 0;
0b5dc8b5 1165}
1166
1167
08803898 1168/* Return true if there is no possibility of aliasing because of a type
1169 mismatch between all the possible pointer references and the
1170 potential target. Note that this function is asymmetric in the
1171 arguments and so must be called twice with the arguments exchanged. */
1172
1173static bool
1174check_data_pointer_types (gfc_expr *expr1, gfc_expr *expr2)
1175{
1176 gfc_component *cm1;
1177 gfc_symbol *sym1;
1178 gfc_symbol *sym2;
1179 gfc_ref *ref1;
1180 bool seen_component_ref;
1181
1182 if (expr1->expr_type != EXPR_VARIABLE
68cc384e 1183 || expr2->expr_type != EXPR_VARIABLE)
08803898 1184 return false;
1185
1186 sym1 = expr1->symtree->n.sym;
1187 sym2 = expr2->symtree->n.sym;
1188
1189 /* Keep it simple for now. */
1190 if (sym1->ts.type == BT_DERIVED && sym2->ts.type == BT_DERIVED)
1191 return false;
1192
1193 if (sym1->attr.pointer)
1194 {
1195 if (gfc_compare_types (&sym1->ts, &sym2->ts))
1196 return false;
1197 }
1198
1199 /* This is a conservative check on the components of the derived type
1200 if no component references have been seen. Since we will not dig
1201 into the components of derived type components, we play it safe by
1202 returning false. First we check the reference chain and then, if
1203 no component references have been seen, the components. */
1204 seen_component_ref = false;
1205 if (sym1->ts.type == BT_DERIVED)
1206 {
1207 for (ref1 = expr1->ref; ref1; ref1 = ref1->next)
1208 {
1209 if (ref1->type != REF_COMPONENT)
1210 continue;
1211
1212 if (ref1->u.c.component->ts.type == BT_DERIVED)
1213 return false;
1214
1215 if ((sym2->attr.pointer || ref1->u.c.component->attr.pointer)
1216 && gfc_compare_types (&ref1->u.c.component->ts, &sym2->ts))
1217 return false;
1218
1219 seen_component_ref = true;
1220 }
1221 }
1222
1223 if (sym1->ts.type == BT_DERIVED && !seen_component_ref)
1224 {
1225 for (cm1 = sym1->ts.u.derived->components; cm1; cm1 = cm1->next)
1226 {
1227 if (cm1->ts.type == BT_DERIVED)
1228 return false;
1229
1230 if ((sym2->attr.pointer || cm1->attr.pointer)
1231 && gfc_compare_types (&cm1->ts, &sym2->ts))
1232 return false;
1233 }
1234 }
1235
1236 return true;
1237}
1238
1239
4ee9c684 1240/* Return true if the statement body redefines the condition. Returns
1241 true if expr2 depends on expr1. expr1 should be a single term
dded0b23 1242 suitable for the lhs of an assignment. The IDENTICAL flag indicates
1243 whether array references to the same symbol with identical range
1244 references count as a dependency or not. Used for forall and where
4ee9c684 1245 statements. Also used with functions returning arrays without a
1246 temporary. */
1247
1248int
1a9745d2 1249gfc_check_dependency (gfc_expr *expr1, gfc_expr *expr2, bool identical)
4ee9c684 1250{
c5918475 1251 gfc_actual_arglist *actual;
1252 gfc_constructor *c;
4ee9c684 1253 int n;
4ee9c684 1254
22d678e8 1255 gcc_assert (expr1->expr_type == EXPR_VARIABLE);
4ee9c684 1256
4ee9c684 1257 switch (expr2->expr_type)
1258 {
1259 case EXPR_OP:
dded0b23 1260 n = gfc_check_dependency (expr1, expr2->value.op.op1, identical);
4ee9c684 1261 if (n)
1262 return n;
9b773341 1263 if (expr2->value.op.op2)
dded0b23 1264 return gfc_check_dependency (expr1, expr2->value.op.op2, identical);
4ee9c684 1265 return 0;
1266
1267 case EXPR_VARIABLE:
e33c5890 1268 /* The interesting cases are when the symbols don't match. */
1269 if (expr1->symtree->n.sym != expr2->symtree->n.sym)
4ee9c684 1270 {
1869527c 1271 symbol_attribute attr1, attr2;
e33c5890 1272 gfc_typespec *ts1 = &expr1->symtree->n.sym->ts;
1273 gfc_typespec *ts2 = &expr2->symtree->n.sym->ts;
1274
1275 /* Return 1 if expr1 and expr2 are equivalenced arrays. */
1276 if (gfc_are_equivalenced_arrays (expr1, expr2))
4ee9c684 1277 return 1;
4ee9c684 1278
e33c5890 1279 /* Symbols can only alias if they have the same type. */
1a9745d2 1280 if (ts1->type != BT_UNKNOWN && ts2->type != BT_UNKNOWN
1281 && ts1->type != BT_DERIVED && ts2->type != BT_DERIVED)
e33c5890 1282 {
1a9745d2 1283 if (ts1->type != ts2->type || ts1->kind != ts2->kind)
e33c5890 1284 return 0;
1285 }
0b5dc8b5 1286
1869527c 1287 /* We have to also include target-target as ptr%comp is not a
1288 pointer but it still alias with "dt%comp" for "ptr => dt". As
1289 subcomponents and array access to pointers retains the target
1290 attribute, that's sufficient. */
1291 attr1 = gfc_expr_attr (expr1);
1292 attr2 = gfc_expr_attr (expr2);
1293 if ((attr1.pointer || attr1.target) && (attr2.pointer || attr2.target))
08803898 1294 {
1295 if (check_data_pointer_types (expr1, expr2)
1296 && check_data_pointer_types (expr2, expr1))
1297 return 0;
1298
1299 return 1;
1300 }
ec1eb337 1301 else
1302 {
1303 gfc_symbol *sym1 = expr1->symtree->n.sym;
1304 gfc_symbol *sym2 = expr2->symtree->n.sym;
1305 if (sym1->attr.target && sym2->attr.target
1306 && ((sym1->attr.dummy && !sym1->attr.contiguous
1307 && (!sym1->attr.dimension
1308 || sym2->as->type == AS_ASSUMED_SHAPE))
1309 || (sym2->attr.dummy && !sym2->attr.contiguous
1310 && (!sym2->attr.dimension
1311 || sym2->as->type == AS_ASSUMED_SHAPE))))
1312 return 1;
1313 }
e33c5890 1314
1315 /* Otherwise distinct symbols have no dependencies. */
1316 return 0;
1317 }
4ee9c684 1318
8311fe26 1319 if (identical)
1320 return 1;
1321
80425127 1322 /* Identical and disjoint ranges return 0,
1323 overlapping ranges return 1. */
791d4123 1324 if (expr1->ref && expr2->ref)
8311fe26 1325 return gfc_dep_resolver (expr1->ref, expr2->ref, NULL);
dded0b23 1326
4ee9c684 1327 return 1;
1328
1329 case EXPR_FUNCTION:
1274527b 1330 if (gfc_get_noncopying_intrinsic_argument (expr2) != NULL)
dded0b23 1331 identical = 1;
1274527b 1332
231e961a 1333 /* Remember possible differences between elemental and
a7455f80 1334 transformational functions. All functions inside a FORALL
1335 will be pure. */
4ee9c684 1336 for (actual = expr2->value.function.actual;
1337 actual; actual = actual->next)
1338 {
1339 if (!actual->expr)
1340 continue;
dded0b23 1341 n = gfc_check_dependency (expr1, actual->expr, identical);
4ee9c684 1342 if (n)
1343 return n;
1344 }
1345 return 0;
1346
1347 case EXPR_CONSTANT:
11c3ed2a 1348 case EXPR_NULL:
4ee9c684 1349 return 0;
1350
1351 case EXPR_ARRAY:
c5918475 1352 /* Loop through the array constructor's elements. */
126387b5 1353 for (c = gfc_constructor_first (expr2->value.constructor);
1354 c; c = gfc_constructor_next (c))
c5918475 1355 {
1356 /* If this is an iterator, assume the worst. */
1357 if (c->iterator)
1358 return 1;
1359 /* Avoid recursion in the common case. */
1360 if (c->expr->expr_type == EXPR_CONSTANT)
1361 continue;
1362 if (gfc_check_dependency (expr1, c->expr, 1))
1363 return 1;
1364 }
1365 return 0;
4ee9c684 1366
1367 default:
1368 return 1;
1369 }
1370}
1371
1372
4ee9c684 1373/* Determines overlapping for two array sections. */
1374
1375static gfc_dependency
cca7236e 1376check_section_vs_section (gfc_array_ref *l_ar, gfc_array_ref *r_ar, int n)
4ee9c684 1377{
1378 gfc_expr *l_start;
1379 gfc_expr *l_end;
1380 gfc_expr *l_stride;
3f4feb44 1381 gfc_expr *l_lower;
1382 gfc_expr *l_upper;
1383 int l_dir;
4ee9c684 1384
1385 gfc_expr *r_start;
3f4feb44 1386 gfc_expr *r_end;
4ee9c684 1387 gfc_expr *r_stride;
3f4feb44 1388 gfc_expr *r_lower;
1389 gfc_expr *r_upper;
65a182bc 1390 gfc_expr *one_expr;
3f4feb44 1391 int r_dir;
65a182bc 1392 int stride_comparison;
1393 int start_comparison;
a642efd6 1394 mpz_t tmp;
4ee9c684 1395
477c2f87 1396 /* If they are the same range, return without more ado. */
9fcec394 1397 if (is_same_range (l_ar, r_ar, n))
477c2f87 1398 return GFC_DEP_EQUAL;
4ee9c684 1399
cca7236e 1400 l_start = l_ar->start[n];
1401 l_end = l_ar->end[n];
1402 l_stride = l_ar->stride[n];
3f4feb44 1403
cca7236e 1404 r_start = r_ar->start[n];
1405 r_end = r_ar->end[n];
1406 r_stride = r_ar->stride[n];
4ee9c684 1407
3f4feb44 1408 /* If l_start is NULL take it from array specifier. */
cca7236e 1409 if (NULL == l_start && IS_ARRAY_EXPLICIT (l_ar->as))
1410 l_start = l_ar->as->lower[n];
3f4feb44 1411 /* If l_end is NULL take it from array specifier. */
cca7236e 1412 if (NULL == l_end && IS_ARRAY_EXPLICIT (l_ar->as))
1413 l_end = l_ar->as->upper[n];
4ee9c684 1414
3f4feb44 1415 /* If r_start is NULL take it from array specifier. */
cca7236e 1416 if (NULL == r_start && IS_ARRAY_EXPLICIT (r_ar->as))
1417 r_start = r_ar->as->lower[n];
3f4feb44 1418 /* If r_end is NULL take it from array specifier. */
cca7236e 1419 if (NULL == r_end && IS_ARRAY_EXPLICIT (r_ar->as))
1420 r_end = r_ar->as->upper[n];
3f4feb44 1421
1422 /* Determine whether the l_stride is positive or negative. */
1423 if (!l_stride)
1424 l_dir = 1;
1425 else if (l_stride->expr_type == EXPR_CONSTANT
1a9745d2 1426 && l_stride->ts.type == BT_INTEGER)
3f4feb44 1427 l_dir = mpz_sgn (l_stride->value.integer);
1428 else if (l_start && l_end)
1429 l_dir = gfc_dep_compare_expr (l_end, l_start);
1430 else
1431 l_dir = -2;
1432
1433 /* Determine whether the r_stride is positive or negative. */
1434 if (!r_stride)
1435 r_dir = 1;
1436 else if (r_stride->expr_type == EXPR_CONSTANT
1a9745d2 1437 && r_stride->ts.type == BT_INTEGER)
3f4feb44 1438 r_dir = mpz_sgn (r_stride->value.integer);
1439 else if (r_start && r_end)
1440 r_dir = gfc_dep_compare_expr (r_end, r_start);
1441 else
1442 r_dir = -2;
4ee9c684 1443
3f4feb44 1444 /* The strides should never be zero. */
1445 if (l_dir == 0 || r_dir == 0)
1446 return GFC_DEP_OVERLAP;
4ee9c684 1447
65a182bc 1448 /* Determine the relationship between the strides. Set stride_comparison to
1449 -2 if the dependency cannot be determined
1450 -1 if l_stride < r_stride
1451 0 if l_stride == r_stride
1452 1 if l_stride > r_stride
1453 as determined by gfc_dep_compare_expr. */
60dd952b 1454
65a182bc 1455 one_expr = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
1456
1457 stride_comparison = gfc_dep_compare_expr (l_stride ? l_stride : one_expr,
1458 r_stride ? r_stride : one_expr);
1459
1460 if (l_start && r_start)
1461 start_comparison = gfc_dep_compare_expr (l_start, r_start);
60dd952b 1462 else
65a182bc 1463 start_comparison = -2;
9caa6670 1464
a89260f1 1465 gfc_free_expr (one_expr);
60dd952b 1466
3f4feb44 1467 /* Determine LHS upper and lower bounds. */
1468 if (l_dir == 1)
1469 {
1470 l_lower = l_start;
1471 l_upper = l_end;
1472 }
1473 else if (l_dir == -1)
1474 {
1475 l_lower = l_end;
1476 l_upper = l_start;
1477 }
4ee9c684 1478 else
3f4feb44 1479 {
1480 l_lower = NULL;
1481 l_upper = NULL;
1482 }
4ee9c684 1483
3f4feb44 1484 /* Determine RHS upper and lower bounds. */
1485 if (r_dir == 1)
1486 {
1487 r_lower = r_start;
1488 r_upper = r_end;
1489 }
1490 else if (r_dir == -1)
1491 {
1492 r_lower = r_end;
1493 r_upper = r_start;
1494 }
1495 else
1496 {
1497 r_lower = NULL;
1498 r_upper = NULL;
1499 }
1500
1501 /* Check whether the ranges are disjoint. */
1502 if (l_upper && r_lower && gfc_dep_compare_expr (l_upper, r_lower) == -1)
1503 return GFC_DEP_NODEP;
1504 if (r_upper && l_lower && gfc_dep_compare_expr (r_upper, l_lower) == -1)
1505 return GFC_DEP_NODEP;
1506
1507 /* Handle cases like x:y:1 vs. x:z:-1 as GFC_DEP_EQUAL. */
1508 if (l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == 0)
1509 {
1510 if (l_dir == 1 && r_dir == -1)
1a9745d2 1511 return GFC_DEP_EQUAL;
3f4feb44 1512 if (l_dir == -1 && r_dir == 1)
1a9745d2 1513 return GFC_DEP_EQUAL;
3f4feb44 1514 }
1515
1516 /* Handle cases like x:y:1 vs. z:y:-1 as GFC_DEP_EQUAL. */
1517 if (l_end && r_end && gfc_dep_compare_expr (l_end, r_end) == 0)
1518 {
1519 if (l_dir == 1 && r_dir == -1)
1a9745d2 1520 return GFC_DEP_EQUAL;
3f4feb44 1521 if (l_dir == -1 && r_dir == 1)
1a9745d2 1522 return GFC_DEP_EQUAL;
3f4feb44 1523 }
1524
55f066ca 1525 /* Handle cases like x:y:2 vs. x+1:z:4 as GFC_DEP_NODEP.
1526 There is no dependency if the remainder of
1527 (l_start - r_start) / gcd(l_stride, r_stride) is
1528 nonzero.
1529 TODO:
55f066ca 1530 - Cases like a(1:4:2) = a(2:3) are still not handled.
1531 */
1532
1533#define IS_CONSTANT_INTEGER(a) ((a) && ((a)->expr_type == EXPR_CONSTANT) \
1534 && (a)->ts.type == BT_INTEGER)
1535
60e19868 1536 if (IS_CONSTANT_INTEGER (l_stride) && IS_CONSTANT_INTEGER (r_stride)
a642efd6 1537 && gfc_dep_difference (l_start, r_start, &tmp))
55f066ca 1538 {
a642efd6 1539 mpz_t gcd;
55f066ca 1540 int result;
1541
1542 mpz_init (gcd);
55f066ca 1543 mpz_gcd (gcd, l_stride->value.integer, r_stride->value.integer);
55f066ca 1544
1545 mpz_fdiv_r (tmp, tmp, gcd);
1546 result = mpz_cmp_si (tmp, 0L);
1547
1548 mpz_clear (gcd);
1549 mpz_clear (tmp);
1550
1551 if (result != 0)
1552 return GFC_DEP_NODEP;
1553 }
1554
1555#undef IS_CONSTANT_INTEGER
1556
293d72e0 1557 /* Check for forward dependencies x:y vs. x+1:z and x:y:z vs. x:y:z+1. */
3f4feb44 1558
65a182bc 1559 if (l_dir == 1 && r_dir == 1 &&
1560 (start_comparison == 0 || start_comparison == -1)
1561 && (stride_comparison == 0 || stride_comparison == -1))
1562 return GFC_DEP_FORWARD;
3f4feb44 1563
65a182bc 1564 /* Check for forward dependencies x:y:-1 vs. x-1:z:-1 and
1565 x:y:-1 vs. x:y:-2. */
9caa6670 1566 if (l_dir == -1 && r_dir == -1 &&
65a182bc 1567 (start_comparison == 0 || start_comparison == 1)
1568 && (stride_comparison == 0 || stride_comparison == 1))
1569 return GFC_DEP_FORWARD;
1c9b57d0 1570
65a182bc 1571 if (stride_comparison == 0 || stride_comparison == -1)
25f9f93d 1572 {
1c9b57d0 1573 if (l_start && IS_ARRAY_EXPLICIT (l_ar->as))
1574 {
1575
65a182bc 1576 /* Check for a(low:y:s) vs. a(z:x:s) or
1577 a(low:y:s) vs. a(z:x:s+1) where a has a lower bound
1c9b57d0 1578 of low, which is always at least a forward dependence. */
1579
1580 if (r_dir == 1
1581 && gfc_dep_compare_expr (l_start, l_ar->as->lower[n]) == 0)
1582 return GFC_DEP_FORWARD;
65a182bc 1583 }
1584 }
1c9b57d0 1585
65a182bc 1586 if (stride_comparison == 0 || stride_comparison == 1)
1587 {
1588 if (l_start && IS_ARRAY_EXPLICIT (l_ar->as))
1589 {
9caa6670 1590
65a182bc 1591 /* Check for a(high:y:-s) vs. a(z:x:-s) or
1592 a(high:y:-s vs. a(z:x:-s-1) where a has a higher bound
1c9b57d0 1593 of high, which is always at least a forward dependence. */
1594
1595 if (r_dir == -1
1596 && gfc_dep_compare_expr (l_start, l_ar->as->upper[n]) == 0)
1597 return GFC_DEP_FORWARD;
1598 }
65a182bc 1599 }
1c9b57d0 1600
65a182bc 1601
1602 if (stride_comparison == 0)
1603 {
1c9b57d0 1604 /* From here, check for backwards dependencies. */
65a182bc 1605 /* x+1:y vs. x:z. */
1606 if (l_dir == 1 && r_dir == 1 && start_comparison == 1)
25f9f93d 1607 return GFC_DEP_BACKWARD;
1608
65a182bc 1609 /* x-1:y:-1 vs. x:z:-1. */
1610 if (l_dir == -1 && r_dir == -1 && start_comparison == -1)
25f9f93d 1611 return GFC_DEP_BACKWARD;
1612 }
1613
3f4feb44 1614 return GFC_DEP_OVERLAP;
4ee9c684 1615}
1616
1617
a6c8790e 1618/* Determines overlapping for a single element and a section. */
4ee9c684 1619
1620static gfc_dependency
1a9745d2 1621gfc_check_element_vs_section( gfc_ref *lref, gfc_ref *rref, int n)
4ee9c684 1622{
a6c8790e 1623 gfc_array_ref *ref;
1624 gfc_expr *elem;
1625 gfc_expr *start;
1626 gfc_expr *end;
1627 gfc_expr *stride;
4ee9c684 1628 int s;
1629
a6c8790e 1630 elem = lref->u.ar.start[n];
1631 if (!elem)
4ee9c684 1632 return GFC_DEP_OVERLAP;
1633
a6c8790e 1634 ref = &rref->u.ar;
1635 start = ref->start[n] ;
1636 end = ref->end[n] ;
1637 stride = ref->stride[n];
1638
1639 if (!start && IS_ARRAY_EXPLICIT (ref->as))
1640 start = ref->as->lower[n];
1641 if (!end && IS_ARRAY_EXPLICIT (ref->as))
1642 end = ref->as->upper[n];
1643
1644 /* Determine whether the stride is positive or negative. */
1645 if (!stride)
1646 s = 1;
1647 else if (stride->expr_type == EXPR_CONSTANT
1648 && stride->ts.type == BT_INTEGER)
1649 s = mpz_sgn (stride->value.integer);
1650 else
1651 s = -2;
4ee9c684 1652
a6c8790e 1653 /* Stride should never be zero. */
1654 if (s == 0)
4ee9c684 1655 return GFC_DEP_OVERLAP;
1656
a6c8790e 1657 /* Positive strides. */
4ee9c684 1658 if (s == 1)
1659 {
a6c8790e 1660 /* Check for elem < lower. */
1661 if (start && gfc_dep_compare_expr (elem, start) == -1)
1662 return GFC_DEP_NODEP;
1663 /* Check for elem > upper. */
1664 if (end && gfc_dep_compare_expr (elem, end) == 1)
1665 return GFC_DEP_NODEP;
1666
1667 if (start && end)
1668 {
1669 s = gfc_dep_compare_expr (start, end);
1670 /* Check for an empty range. */
1671 if (s == 1)
1672 return GFC_DEP_NODEP;
1673 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1674 return GFC_DEP_EQUAL;
1675 }
1676 }
1677 /* Negative strides. */
1678 else if (s == -1)
1679 {
1680 /* Check for elem > upper. */
1681 if (end && gfc_dep_compare_expr (elem, start) == 1)
1682 return GFC_DEP_NODEP;
1683 /* Check for elem < lower. */
1684 if (start && gfc_dep_compare_expr (elem, end) == -1)
1685 return GFC_DEP_NODEP;
1686
1687 if (start && end)
1688 {
1689 s = gfc_dep_compare_expr (start, end);
1690 /* Check for an empty range. */
1691 if (s == -1)
1692 return GFC_DEP_NODEP;
1693 if (s == 0 && gfc_dep_compare_expr (elem, start) == 0)
1694 return GFC_DEP_EQUAL;
1695 }
4ee9c684 1696 }
a6c8790e 1697 /* Unknown strides. */
4ee9c684 1698 else
1699 {
a6c8790e 1700 if (!start || !end)
1701 return GFC_DEP_OVERLAP;
1702 s = gfc_dep_compare_expr (start, end);
61bc1860 1703 if (s <= -2)
4ee9c684 1704 return GFC_DEP_OVERLAP;
a6c8790e 1705 /* Assume positive stride. */
1706 if (s == -1)
1707 {
1708 /* Check for elem < lower. */
1709 if (gfc_dep_compare_expr (elem, start) == -1)
1710 return GFC_DEP_NODEP;
1711 /* Check for elem > upper. */
1712 if (gfc_dep_compare_expr (elem, end) == 1)
1713 return GFC_DEP_NODEP;
1714 }
1715 /* Assume negative stride. */
1716 else if (s == 1)
1717 {
1718 /* Check for elem > upper. */
1719 if (gfc_dep_compare_expr (elem, start) == 1)
1720 return GFC_DEP_NODEP;
1721 /* Check for elem < lower. */
1722 if (gfc_dep_compare_expr (elem, end) == -1)
1723 return GFC_DEP_NODEP;
1724 }
1725 /* Equal bounds. */
1726 else if (s == 0)
1727 {
1728 s = gfc_dep_compare_expr (elem, start);
1729 if (s == 0)
1730 return GFC_DEP_EQUAL;
1731 if (s == 1 || s == -1)
1732 return GFC_DEP_NODEP;
1733 }
4ee9c684 1734 }
4ee9c684 1735
a6c8790e 1736 return GFC_DEP_OVERLAP;
4ee9c684 1737}
1738
1739
bf0a0eb6 1740/* Traverse expr, checking all EXPR_VARIABLE symbols for their
1741 forall_index attribute. Return true if any variable may be
1742 being used as a FORALL index. Its safe to pessimistically
1743 return true, and assume a dependency. */
1744
1745static bool
1a9745d2 1746contains_forall_index_p (gfc_expr *expr)
bf0a0eb6 1747{
1748 gfc_actual_arglist *arg;
1749 gfc_constructor *c;
1750 gfc_ref *ref;
1751 int i;
1752
1753 if (!expr)
1754 return false;
1755
1756 switch (expr->expr_type)
1757 {
1758 case EXPR_VARIABLE:
1759 if (expr->symtree->n.sym->forall_index)
1760 return true;
1761 break;
1762
1763 case EXPR_OP:
1764 if (contains_forall_index_p (expr->value.op.op1)
1765 || contains_forall_index_p (expr->value.op.op2))
1766 return true;
1767 break;
1768
1769 case EXPR_FUNCTION:
1770 for (arg = expr->value.function.actual; arg; arg = arg->next)
1771 if (contains_forall_index_p (arg->expr))
1772 return true;
1773 break;
1774
1775 case EXPR_CONSTANT:
1776 case EXPR_NULL:
1777 case EXPR_SUBSTRING:
1778 break;
1779
1780 case EXPR_STRUCTURE:
1781 case EXPR_ARRAY:
126387b5 1782 for (c = gfc_constructor_first (expr->value.constructor);
1783 c; gfc_constructor_next (c))
bf0a0eb6 1784 if (contains_forall_index_p (c->expr))
1785 return true;
1786 break;
1787
1788 default:
1789 gcc_unreachable ();
1790 }
1791
1792 for (ref = expr->ref; ref; ref = ref->next)
1793 switch (ref->type)
1794 {
1795 case REF_ARRAY:
1796 for (i = 0; i < ref->u.ar.dimen; i++)
1797 if (contains_forall_index_p (ref->u.ar.start[i])
1798 || contains_forall_index_p (ref->u.ar.end[i])
1799 || contains_forall_index_p (ref->u.ar.stride[i]))
1800 return true;
1801 break;
1802
1803 case REF_COMPONENT:
1804 break;
1805
1806 case REF_SUBSTRING:
1807 if (contains_forall_index_p (ref->u.ss.start)
1808 || contains_forall_index_p (ref->u.ss.end))
1809 return true;
1810 break;
1811
1812 default:
1813 gcc_unreachable ();
1814 }
1815
1816 return false;
1817}
1818
4ee9c684 1819/* Determines overlapping for two single element array references. */
1820
1821static gfc_dependency
1a9745d2 1822gfc_check_element_vs_element (gfc_ref *lref, gfc_ref *rref, int n)
4ee9c684 1823{
1824 gfc_array_ref l_ar;
1825 gfc_array_ref r_ar;
1826 gfc_expr *l_start;
1827 gfc_expr *r_start;
80425127 1828 int i;
4ee9c684 1829
80425127 1830 l_ar = lref->u.ar;
1831 r_ar = rref->u.ar;
1832 l_start = l_ar.start[n] ;
1833 r_start = r_ar.start[n] ;
1834 i = gfc_dep_compare_expr (r_start, l_start);
1835 if (i == 0)
1836 return GFC_DEP_EQUAL;
bf0a0eb6 1837
1838 /* Treat two scalar variables as potentially equal. This allows
1839 us to prove that a(i,:) and a(j,:) have no dependency. See
1840 Gerald Roth, "Evaluation of Array Syntax Dependence Analysis",
1841 Proceedings of the International Conference on Parallel and
1842 Distributed Processing Techniques and Applications (PDPTA2001),
1843 Las Vegas, Nevada, June 2001. */
1844 /* However, we need to be careful when either scalar expression
1845 contains a FORALL index, as these can potentially change value
1846 during the scalarization/traversal of this array reference. */
1a9745d2 1847 if (contains_forall_index_p (r_start) || contains_forall_index_p (l_start))
17c67d08 1848 return GFC_DEP_OVERLAP;
bf0a0eb6 1849
61bc1860 1850 if (i > -2)
4d4677fd 1851 return GFC_DEP_NODEP;
bf0a0eb6 1852 return GFC_DEP_EQUAL;
4ee9c684 1853}
1854
2c9c0c1f 1855/* Callback function for checking if an expression depends on a
1856 dummy variable which is any other than INTENT(IN). */
1857
1858static int
1859callback_dummy_intent_not_in (gfc_expr **ep,
1860 int *walk_subtrees ATTRIBUTE_UNUSED,
1861 void *data ATTRIBUTE_UNUSED)
1862{
1863 gfc_expr *e = *ep;
1864
1865 if (e->expr_type == EXPR_VARIABLE && e->symtree
1866 && e->symtree->n.sym->attr.dummy)
1867 return e->symtree->n.sym->attr.intent != INTENT_IN;
1868 else
1869 return 0;
1870}
1871
1872/* Auxiliary function to check if subexpressions have dummy variables which
1873 are not intent(in).
1874*/
1875
1876static bool
1877dummy_intent_not_in (gfc_expr **ep)
1878{
1879 return gfc_expr_walker (ep, callback_dummy_intent_not_in, NULL);
1880}
4ee9c684 1881
eb89ca84 1882/* Determine if an array ref, usually an array section specifies the
8d60cc46 1883 entire array. In addition, if the second, pointer argument is
1884 provided, the function will return true if the reference is
2c9c0c1f 1885 contiguous; eg. (:, 1) gives true but (1,:) gives false.
1886 If one of the bounds depends on a dummy variable which is
1887 not INTENT(IN), also return false, because the user may
1888 have changed the variable. */
eb89ca84 1889
1890bool
8d60cc46 1891gfc_full_array_ref_p (gfc_ref *ref, bool *contiguous)
eb89ca84 1892{
1893 int i;
9bf78879 1894 int n;
8d60cc46 1895 bool lbound_OK = true;
1896 bool ubound_OK = true;
eb89ca84 1897
88a37d69 1898 if (contiguous)
1899 *contiguous = false;
1900
eb89ca84 1901 if (ref->type != REF_ARRAY)
1902 return false;
9bf78879 1903
eb89ca84 1904 if (ref->u.ar.type == AR_FULL)
88a37d69 1905 {
1906 if (contiguous)
1907 *contiguous = true;
1908 return true;
1909 }
9bf78879 1910
eb89ca84 1911 if (ref->u.ar.type != AR_SECTION)
1912 return false;
538374c5 1913 if (ref->next)
1914 return false;
eb89ca84 1915
1916 for (i = 0; i < ref->u.ar.dimen; i++)
1917 {
9bf78879 1918 /* If we have a single element in the reference, for the reference
1919 to be full, we need to ascertain that the array has a single
1920 element in this dimension and that we actually reference the
1921 correct element. */
3d3e0f7d 1922 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
1923 {
9bf78879 1924 /* This is unconditionally a contiguous reference if all the
1925 remaining dimensions are elements. */
8d60cc46 1926 if (contiguous)
9bf78879 1927 {
1928 *contiguous = true;
1929 for (n = i + 1; n < ref->u.ar.dimen; n++)
1930 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
1931 *contiguous = false;
1932 }
8d60cc46 1933
3d3e0f7d 1934 if (!ref->u.ar.as
1935 || !ref->u.ar.as->lower[i]
1936 || !ref->u.ar.as->upper[i]
1937 || gfc_dep_compare_expr (ref->u.ar.as->lower[i],
1938 ref->u.ar.as->upper[i])
1939 || !ref->u.ar.start[i]
1940 || gfc_dep_compare_expr (ref->u.ar.start[i],
1941 ref->u.ar.as->lower[i]))
1942 return false;
1943 else
1944 continue;
1945 }
1946
eb89ca84 1947 /* Check the lower bound. */
1948 if (ref->u.ar.start[i]
1949 && (!ref->u.ar.as
1950 || !ref->u.ar.as->lower[i]
1951 || gfc_dep_compare_expr (ref->u.ar.start[i],
2c9c0c1f 1952 ref->u.ar.as->lower[i])
1953 || dummy_intent_not_in (&ref->u.ar.start[i])))
8d60cc46 1954 lbound_OK = false;
eb89ca84 1955 /* Check the upper bound. */
1956 if (ref->u.ar.end[i]
1957 && (!ref->u.ar.as
1958 || !ref->u.ar.as->upper[i]
1959 || gfc_dep_compare_expr (ref->u.ar.end[i],
2c9c0c1f 1960 ref->u.ar.as->upper[i])
1961 || dummy_intent_not_in (&ref->u.ar.end[i])))
8d60cc46 1962 ubound_OK = false;
eb89ca84 1963 /* Check the stride. */
9bf78879 1964 if (ref->u.ar.stride[i]
1965 && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
eb89ca84 1966 return false;
8d60cc46 1967
9bf78879 1968 /* This is unconditionally a contiguous reference as long as all
1969 the subsequent dimensions are elements. */
8d60cc46 1970 if (contiguous)
9bf78879 1971 {
1972 *contiguous = true;
1973 for (n = i + 1; n < ref->u.ar.dimen; n++)
1974 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
1975 *contiguous = false;
1976 }
8d60cc46 1977
1978 if (!lbound_OK || !ubound_OK)
1979 return false;
eb89ca84 1980 }
1981 return true;
1982}
1983
1984
4992c81a 1985/* Determine if a full array is the same as an array section with one
1986 variable limit. For this to be so, the strides must both be unity
1987 and one of either start == lower or end == upper must be true. */
1988
1989static bool
1990ref_same_as_full_array (gfc_ref *full_ref, gfc_ref *ref)
1991{
1992 int i;
1993 bool upper_or_lower;
1994
1995 if (full_ref->type != REF_ARRAY)
1996 return false;
1997 if (full_ref->u.ar.type != AR_FULL)
1998 return false;
1999 if (ref->type != REF_ARRAY)
2000 return false;
2001 if (ref->u.ar.type != AR_SECTION)
2002 return false;
2003
2004 for (i = 0; i < ref->u.ar.dimen; i++)
2005 {
2006 /* If we have a single element in the reference, we need to check
2007 that the array has a single element and that we actually reference
2008 the correct element. */
2009 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT)
2010 {
2011 if (!full_ref->u.ar.as
2012 || !full_ref->u.ar.as->lower[i]
2013 || !full_ref->u.ar.as->upper[i]
2014 || gfc_dep_compare_expr (full_ref->u.ar.as->lower[i],
2015 full_ref->u.ar.as->upper[i])
2016 || !ref->u.ar.start[i]
2017 || gfc_dep_compare_expr (ref->u.ar.start[i],
2018 full_ref->u.ar.as->lower[i]))
2019 return false;
2020 }
2021
2022 /* Check the strides. */
2023 if (full_ref->u.ar.stride[i] && !gfc_expr_is_one (full_ref->u.ar.stride[i], 0))
2024 return false;
2025 if (ref->u.ar.stride[i] && !gfc_expr_is_one (ref->u.ar.stride[i], 0))
2026 return false;
2027
2028 upper_or_lower = false;
2029 /* Check the lower bound. */
2030 if (ref->u.ar.start[i]
2031 && (ref->u.ar.as
2032 && full_ref->u.ar.as->lower[i]
2033 && gfc_dep_compare_expr (ref->u.ar.start[i],
2034 full_ref->u.ar.as->lower[i]) == 0))
2035 upper_or_lower = true;
2036 /* Check the upper bound. */
2037 if (ref->u.ar.end[i]
2038 && (ref->u.ar.as
2039 && full_ref->u.ar.as->upper[i]
2040 && gfc_dep_compare_expr (ref->u.ar.end[i],
2041 full_ref->u.ar.as->upper[i]) == 0))
2042 upper_or_lower = true;
2043 if (!upper_or_lower)
2044 return false;
2045 }
2046 return true;
2047}
2048
2049
4ee9c684 2050/* Finds if two array references are overlapping or not.
2051 Return value
25f9f93d 2052 2 : array references are overlapping but reversal of one or
2053 more dimensions will clear the dependency.
4ee9c684 2054 1 : array references are overlapping.
8311fe26 2055 0 : array references are identical or not overlapping. */
4ee9c684 2056
2057int
25f9f93d 2058gfc_dep_resolver (gfc_ref *lref, gfc_ref *rref, gfc_reverse *reverse)
4ee9c684 2059{
2060 int n;
9caa6670 2061 int m;
4ee9c684 2062 gfc_dependency fin_dep;
2063 gfc_dependency this_dep;
2064
25f9f93d 2065 this_dep = GFC_DEP_ERROR;
4ee9c684 2066 fin_dep = GFC_DEP_ERROR;
2067 /* Dependencies due to pointers should already have been identified.
2068 We only need to check for overlapping array references. */
2069
2070 while (lref && rref)
2071 {
2072 /* We're resolving from the same base symbol, so both refs should be
69b1505f 2073 the same type. We traverse the reference chain until we find ranges
4ee9c684 2074 that are not equal. */
22d678e8 2075 gcc_assert (lref->type == rref->type);
4ee9c684 2076 switch (lref->type)
2077 {
2078 case REF_COMPONENT:
2079 /* The two ranges can't overlap if they are from different
2080 components. */
2081 if (lref->u.c.component != rref->u.c.component)
8311fe26 2082 return 0;
4ee9c684 2083 break;
9caa6670 2084
4ee9c684 2085 case REF_SUBSTRING:
791d4123 2086 /* Substring overlaps are handled by the string assignment code
2087 if there is not an underlying dependency. */
8311fe26 2088 return (fin_dep == GFC_DEP_OVERLAP) ? 1 : 0;
9caa6670 2089
4ee9c684 2090 case REF_ARRAY:
4992c81a 2091
2092 if (ref_same_as_full_array (lref, rref))
8311fe26 2093 return 0;
4992c81a 2094
2095 if (ref_same_as_full_array (rref, lref))
8311fe26 2096 return 0;
4992c81a 2097
1a9745d2 2098 if (lref->u.ar.dimen != rref->u.ar.dimen)
eb89ca84 2099 {
2100 if (lref->u.ar.type == AR_FULL)
8d60cc46 2101 fin_dep = gfc_full_array_ref_p (rref, NULL) ? GFC_DEP_EQUAL
2102 : GFC_DEP_OVERLAP;
eb89ca84 2103 else if (rref->u.ar.type == AR_FULL)
8d60cc46 2104 fin_dep = gfc_full_array_ref_p (lref, NULL) ? GFC_DEP_EQUAL
2105 : GFC_DEP_OVERLAP;
eb89ca84 2106 else
8311fe26 2107 return 1;
eb89ca84 2108 break;
2109 }
2110
9caa6670 2111 /* Index for the reverse array. */
2112 m = -1;
4ee9c684 2113 for (n=0; n < lref->u.ar.dimen; n++)
2114 {
7243e979 2115 /* Handle dependency when either of array reference is vector
2116 subscript. There is no dependency if the vector indices
2117 are equal or if indices are known to be different in a
2118 different dimension. */
4ee9c684 2119 if (lref->u.ar.dimen_type[n] == DIMEN_VECTOR
2120 || rref->u.ar.dimen_type[n] == DIMEN_VECTOR)
7243e979 2121 {
9caa6670 2122 if (lref->u.ar.dimen_type[n] == DIMEN_VECTOR
7243e979 2123 && rref->u.ar.dimen_type[n] == DIMEN_VECTOR
2124 && gfc_dep_compare_expr (lref->u.ar.start[n],
2125 rref->u.ar.start[n]) == 0)
2126 this_dep = GFC_DEP_EQUAL;
2127 else
2128 this_dep = GFC_DEP_OVERLAP;
2129
2130 goto update_fin_dep;
2131 }
25f9f93d 2132
4ee9c684 2133 if (lref->u.ar.dimen_type[n] == DIMEN_RANGE
2134 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
cca7236e 2135 this_dep = check_section_vs_section (&lref->u.ar, &rref->u.ar, n);
4ee9c684 2136 else if (lref->u.ar.dimen_type[n] == DIMEN_ELEMENT
2137 && rref->u.ar.dimen_type[n] == DIMEN_RANGE)
2138 this_dep = gfc_check_element_vs_section (lref, rref, n);
2139 else if (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
2140 && lref->u.ar.dimen_type[n] == DIMEN_RANGE)
2141 this_dep = gfc_check_element_vs_section (rref, lref, n);
9caa6670 2142 else
4ee9c684 2143 {
22d678e8 2144 gcc_assert (rref->u.ar.dimen_type[n] == DIMEN_ELEMENT
a7455f80 2145 && lref->u.ar.dimen_type[n] == DIMEN_ELEMENT);
4ee9c684 2146 this_dep = gfc_check_element_vs_element (rref, lref, n);
2147 }
2148
2149 /* If any dimension doesn't overlap, we have no dependency. */
2150 if (this_dep == GFC_DEP_NODEP)
8311fe26 2151 return 0;
4ee9c684 2152
25f9f93d 2153 /* Now deal with the loop reversal logic: This only works on
2154 ranges and is activated by setting
66c674b6 2155 reverse[n] == GFC_ENABLE_REVERSE
25f9f93d 2156 The ability to reverse or not is set by previous conditions
2157 in this dimension. If reversal is not activated, the
2158 value GFC_DEP_BACKWARD is reset to GFC_DEP_OVERLAP. */
9caa6670 2159
2160 /* Get the indexing right for the scalarizing loop. If this
2161 is an element, there is no corresponding loop. */
2162 if (lref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
2163 m++;
2164
25f9f93d 2165 if (rref->u.ar.dimen_type[n] == DIMEN_RANGE
2166 && lref->u.ar.dimen_type[n] == DIMEN_RANGE)
2167 {
2168 /* Set reverse if backward dependence and not inhibited. */
9caa6670 2169 if (reverse && reverse[m] == GFC_ENABLE_REVERSE)
2170 reverse[m] = (this_dep == GFC_DEP_BACKWARD) ?
2171 GFC_REVERSE_SET : reverse[m];
25f9f93d 2172
66c674b6 2173 /* Set forward if forward dependence and not inhibited. */
9caa6670 2174 if (reverse && reverse[m] == GFC_ENABLE_REVERSE)
2175 reverse[m] = (this_dep == GFC_DEP_FORWARD) ?
2176 GFC_FORWARD_SET : reverse[m];
66c674b6 2177
2178 /* Flag up overlap if dependence not compatible with
2179 the overall state of the expression. */
9caa6670 2180 if (reverse && reverse[m] == GFC_REVERSE_SET
66c674b6 2181 && this_dep == GFC_DEP_FORWARD)
2182 {
9caa6670 2183 reverse[m] = GFC_INHIBIT_REVERSE;
66c674b6 2184 this_dep = GFC_DEP_OVERLAP;
2185 }
9caa6670 2186 else if (reverse && reverse[m] == GFC_FORWARD_SET
66c674b6 2187 && this_dep == GFC_DEP_BACKWARD)
25f9f93d 2188 {
9caa6670 2189 reverse[m] = GFC_INHIBIT_REVERSE;
66c674b6 2190 this_dep = GFC_DEP_OVERLAP;
25f9f93d 2191 }
2192
2193 /* If no intention of reversing or reversing is explicitly
2194 inhibited, convert backward dependence to overlap. */
66c674b6 2195 if ((reverse == NULL && this_dep == GFC_DEP_BACKWARD)
9caa6670 2196 || (reverse != NULL && reverse[m] == GFC_INHIBIT_REVERSE))
25f9f93d 2197 this_dep = GFC_DEP_OVERLAP;
2198 }
2199
4ee9c684 2200 /* Overlap codes are in order of priority. We only need to
a7455f80 2201 know the worst one.*/
7243e979 2202
2203 update_fin_dep:
4ee9c684 2204 if (this_dep > fin_dep)
2205 fin_dep = this_dep;
2206 }
caea6886 2207
2208 /* If this is an equal element, we have to keep going until we find
2209 the "real" array reference. */
2210 if (lref->u.ar.type == AR_ELEMENT
2211 && rref->u.ar.type == AR_ELEMENT
2212 && fin_dep == GFC_DEP_EQUAL)
2213 break;
2214
4ee9c684 2215 /* Exactly matching and forward overlapping ranges don't cause a
2216 dependency. */
25f9f93d 2217 if (fin_dep < GFC_DEP_BACKWARD)
8311fe26 2218 return 0;
4ee9c684 2219
2220 /* Keep checking. We only have a dependency if
2221 subsequent references also overlap. */
2222 break;
2223
2224 default:
22d678e8 2225 gcc_unreachable ();
4ee9c684 2226 }
2227 lref = lref->next;
2228 rref = rref->next;
2229 }
2230
2231 /* If we haven't seen any array refs then something went wrong. */
22d678e8 2232 gcc_assert (fin_dep != GFC_DEP_ERROR);
4ee9c684 2233
80425127 2234 /* Assume the worst if we nest to different depths. */
2235 if (lref || rref)
8311fe26 2236 return 1;
80425127 2237
8311fe26 2238 return fin_dep == GFC_DEP_OVERLAP;
4ee9c684 2239}