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