| 1 |
/* Pass manager for Fortran front end. |
| 2 |
Copyright (C) 2010-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Thomas König. |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free |
| 9 |
Software Foundation; either version 3, or (at your option) any later |
| 10 |
version. |
| 11 |
|
| 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. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with GCC; see the file COPYING3. If not see |
| 19 |
<http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
#include "system.h" |
| 23 |
#include "coretypes.h" |
| 24 |
#include "options.h" |
| 25 |
#include "gfortran.h" |
| 26 |
#include "dependency.h" |
| 27 |
#include "constructor.h" |
| 28 |
#include "intrinsic.h" |
| 29 |
|
| 30 |
/* Forward declarations. */ |
| 31 |
|
| 32 |
static void strip_function_call (gfc_expr *); |
| 33 |
static void optimize_namespace (gfc_namespace *); |
| 34 |
static void optimize_assignment (gfc_code *); |
| 35 |
static bool optimize_op (gfc_expr *); |
| 36 |
static bool optimize_comparison (gfc_expr *, gfc_intrinsic_op); |
| 37 |
static bool optimize_trim (gfc_expr *); |
| 38 |
static bool optimize_lexical_comparison (gfc_expr *); |
| 39 |
static bool is_empty_string (gfc_expr *e); |
| 40 |
static void doloop_warn (gfc_namespace *); |
| 41 |
static int do_intent (gfc_expr **); |
| 42 |
static int do_subscript (gfc_expr **); |
| 43 |
static void optimize_reduction (gfc_namespace *); |
| 44 |
static int callback_reduction (gfc_expr **, int *, void *); |
| 45 |
static void realloc_strings (gfc_namespace *); |
| 46 |
static gfc_expr *create_var (gfc_expr *, const char *vname=NULL); |
| 47 |
static int matmul_to_var_expr (gfc_expr **, int *, void *); |
| 48 |
static int matmul_to_var_code (gfc_code **, int *, void *); |
| 49 |
static int inline_matmul_assign (gfc_code **, int *, void *); |
| 50 |
static gfc_code * create_do_loop (gfc_expr *, gfc_expr *, gfc_expr *, |
| 51 |
locus *, gfc_namespace *, |
| 52 |
char *vname=NULL); |
| 53 |
static gfc_expr* check_conjg_transpose_variable (gfc_expr *, bool *, |
| 54 |
bool *); |
| 55 |
static int call_external_blas (gfc_code **, int *, void *); |
| 56 |
static int matmul_temp_args (gfc_code **, int *,void *data); |
| 57 |
static int index_interchange (gfc_code **, int*, void *); |
| 58 |
static bool is_fe_temp (gfc_expr *e); |
| 59 |
|
| 60 |
#ifdef CHECKING_P |
| 61 |
static void check_locus (gfc_namespace *); |
| 62 |
#endif |
| 63 |
|
| 64 |
/* How deep we are inside an argument list. */ |
| 65 |
|
| 66 |
static int count_arglist; |
| 67 |
|
| 68 |
/* Vector of gfc_expr ** we operate on. */ |
| 69 |
|
| 70 |
static vec<gfc_expr **> expr_array; |
| 71 |
|
| 72 |
/* Pointer to the gfc_code we currently work on - to be able to insert |
| 73 |
a block before the statement. */ |
| 74 |
|
| 75 |
static gfc_code **current_code; |
| 76 |
|
| 77 |
/* Pointer to the block to be inserted, and the statement we are |
| 78 |
changing within the block. */ |
| 79 |
|
| 80 |
static gfc_code *inserted_block, **changed_statement; |
| 81 |
|
| 82 |
/* The namespace we are currently dealing with. */ |
| 83 |
|
| 84 |
static gfc_namespace *current_ns; |
| 85 |
|
| 86 |
/* If we are within any forall loop. */ |
| 87 |
|
| 88 |
static int forall_level; |
| 89 |
|
| 90 |
/* Keep track of whether we are within an OMP workshare. */ |
| 91 |
|
| 92 |
static bool in_omp_workshare; |
| 93 |
|
| 94 |
/* Keep track of whether we are within an OMP atomic. */ |
| 95 |
|
| 96 |
static bool in_omp_atomic; |
| 97 |
|
| 98 |
/* Keep track of whether we are within a WHERE statement. */ |
| 99 |
|
| 100 |
static bool in_where; |
| 101 |
|
| 102 |
/* Keep track of iterators for array constructors. */ |
| 103 |
|
| 104 |
static int iterator_level; |
| 105 |
|
| 106 |
/* Keep track of DO loop levels. */ |
| 107 |
|
| 108 |
typedef struct { |
| 109 |
gfc_code *c; |
| 110 |
int branch_level; |
| 111 |
bool seen_goto; |
| 112 |
} do_t; |
| 113 |
|
| 114 |
static vec<do_t> doloop_list; |
| 115 |
static int doloop_level; |
| 116 |
|
| 117 |
/* Keep track of if and select case levels. */ |
| 118 |
|
| 119 |
static int if_level; |
| 120 |
static int select_level; |
| 121 |
|
| 122 |
/* Vector of gfc_expr * to keep track of DO loops. */ |
| 123 |
|
| 124 |
struct my_struct *evec; |
| 125 |
|
| 126 |
/* Keep track of association lists. */ |
| 127 |
|
| 128 |
static bool in_assoc_list; |
| 129 |
|
| 130 |
/* Counter for temporary variables. */ |
| 131 |
|
| 132 |
static int var_num = 1; |
| 133 |
|
| 134 |
/* What sort of matrix we are dealing with when inlining MATMUL. */ |
| 135 |
|
| 136 |
enum matrix_case { none=0, A2B2, A2B1, A1B2, A2B2T, A2TB2, A2TB2T }; |
| 137 |
|
| 138 |
/* Keep track of the number of expressions we have inserted so far |
| 139 |
using create_var. */ |
| 140 |
|
| 141 |
int n_vars; |
| 142 |
|
| 143 |
/* Entry point - run all passes for a namespace. */ |
| 144 |
|
| 145 |
void |
| 146 |
gfc_run_passes (gfc_namespace *ns) |
| 147 |
{ |
| 148 |
|
| 149 |
/* Warn about dubious DO loops where the index might |
| 150 |
change. */ |
| 151 |
|
| 152 |
doloop_level = 0; |
| 153 |
if_level = 0; |
| 154 |
select_level = 0; |
| 155 |
doloop_warn (ns); |
| 156 |
doloop_list.release (); |
| 157 |
int w, e; |
| 158 |
|
| 159 |
#ifdef CHECKING_P |
| 160 |
check_locus (ns); |
| 161 |
#endif |
| 162 |
|
| 163 |
gfc_get_errors (&w, &e); |
| 164 |
if (e > 0) |
| 165 |
return; |
| 166 |
|
| 167 |
if (flag_frontend_optimize || flag_frontend_loop_interchange) |
| 168 |
optimize_namespace (ns); |
| 169 |
|
| 170 |
if (flag_frontend_optimize) |
| 171 |
{ |
| 172 |
optimize_reduction (ns); |
| 173 |
if (flag_dump_fortran_optimized) |
| 174 |
gfc_dump_parse_tree (ns, stdout); |
| 175 |
|
| 176 |
expr_array.release (); |
| 177 |
} |
| 178 |
|
| 179 |
if (flag_realloc_lhs) |
| 180 |
realloc_strings (ns); |
| 181 |
} |
| 182 |
|
| 183 |
#ifdef CHECKING_P |
| 184 |
|
| 185 |
/* Callback function: Warn if there is no location information in a |
| 186 |
statement. */ |
| 187 |
|
| 188 |
static int |
| 189 |
check_locus_code (gfc_code **c, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 190 |
void *data ATTRIBUTE_UNUSED) |
| 191 |
{ |
| 192 |
current_code = c; |
| 193 |
if (c |
| 194 |
&& *c |
| 195 |
&& (((*c)->loc.nextc == NULL) |
| 196 |
|| ((*c)->loc.nextc == (gfc_char_t *) -1 |
| 197 |
&& (*c)->loc.u.location == UNKNOWN_LOCATION) |
| 198 |
|| ((*c)->loc.nextc != (gfc_char_t *) -1 |
| 199 |
&& ((*c)->loc.u.lb == NULL)))) |
| 200 |
|
| 201 |
gfc_warning_internal (0, "Inconsistent internal state: " |
| 202 |
"No location in statement"); |
| 203 |
|
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
/* Callback function: Warn if there is no location information in an |
| 209 |
expression. */ |
| 210 |
|
| 211 |
static int |
| 212 |
check_locus_expr (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 213 |
void *data ATTRIBUTE_UNUSED) |
| 214 |
{ |
| 215 |
|
| 216 |
if (e |
| 217 |
&& *e |
| 218 |
&& (((*e)->where.nextc == NULL) |
| 219 |
|| ((*e)->where.nextc == (gfc_char_t *) -1 |
| 220 |
&& (*e)->where.u.location == UNKNOWN_LOCATION) |
| 221 |
|| ((*e)->where.nextc != (gfc_char_t *) -1 |
| 222 |
&& ((*e)->where.u.lb == NULL)))) |
| 223 |
gfc_warning_internal (0, "Inconsistent internal state: " |
| 224 |
"No location in expression near %L", |
| 225 |
&((*current_code)->loc)); |
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
/* Run check for missing location information. */ |
| 230 |
|
| 231 |
static void |
| 232 |
check_locus (gfc_namespace *ns) |
| 233 |
{ |
| 234 |
gfc_code_walker (&ns->code, check_locus_code, check_locus_expr, NULL); |
| 235 |
|
| 236 |
for (ns = ns->contained; ns; ns = ns->sibling) |
| 237 |
{ |
| 238 |
if (ns->code == NULL || ns->code->op != EXEC_BLOCK) |
| 239 |
check_locus (ns); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
#endif |
| 244 |
|
| 245 |
/* Callback for each gfc_code node invoked from check_realloc_strings. |
| 246 |
For an allocatable LHS string which also appears as a variable on |
| 247 |
the RHS, replace |
| 248 |
|
| 249 |
a = a(x:y) |
| 250 |
|
| 251 |
with |
| 252 |
|
| 253 |
tmp = a(x:y) |
| 254 |
a = tmp |
| 255 |
*/ |
| 256 |
|
| 257 |
static int |
| 258 |
realloc_string_callback (gfc_code **c, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 259 |
void *data ATTRIBUTE_UNUSED) |
| 260 |
{ |
| 261 |
gfc_expr *expr1, *expr2; |
| 262 |
gfc_code *co = *c; |
| 263 |
gfc_expr *n; |
| 264 |
gfc_ref *ref; |
| 265 |
bool found_substr; |
| 266 |
|
| 267 |
if (co->op != EXEC_ASSIGN) |
| 268 |
return 0; |
| 269 |
|
| 270 |
expr1 = co->expr1; |
| 271 |
if (expr1->ts.type != BT_CHARACTER |
| 272 |
|| !gfc_expr_attr(expr1).allocatable |
| 273 |
|| !expr1->ts.deferred) |
| 274 |
return 0; |
| 275 |
|
| 276 |
if (is_fe_temp (expr1)) |
| 277 |
return 0; |
| 278 |
|
| 279 |
expr2 = gfc_discard_nops (co->expr2); |
| 280 |
|
| 281 |
if (expr2->expr_type == EXPR_VARIABLE) |
| 282 |
{ |
| 283 |
found_substr = false; |
| 284 |
for (ref = expr2->ref; ref; ref = ref->next) |
| 285 |
{ |
| 286 |
if (ref->type == REF_SUBSTRING) |
| 287 |
{ |
| 288 |
found_substr = true; |
| 289 |
break; |
| 290 |
} |
| 291 |
} |
| 292 |
if (!found_substr) |
| 293 |
return 0; |
| 294 |
} |
| 295 |
else if (expr2->expr_type != EXPR_ARRAY |
| 296 |
&& (expr2->expr_type != EXPR_OP |
| 297 |
|| expr2->value.op.op != INTRINSIC_CONCAT)) |
| 298 |
return 0; |
| 299 |
|
| 300 |
if (!gfc_check_dependency (expr1, expr2, true)) |
| 301 |
return 0; |
| 302 |
|
| 303 |
/* gfc_check_dependency doesn't always pick up identical expressions. |
| 304 |
However, eliminating the above sends the compiler into an infinite |
| 305 |
loop on valid expressions. Without this check, the gimplifier emits |
| 306 |
an ICE for a = a, where a is deferred character length. */ |
| 307 |
if (!gfc_dep_compare_expr (expr1, expr2)) |
| 308 |
return 0; |
| 309 |
|
| 310 |
current_code = c; |
| 311 |
inserted_block = NULL; |
| 312 |
changed_statement = NULL; |
| 313 |
n = create_var (expr2, "realloc_string"); |
| 314 |
co->expr2 = n; |
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
/* Callback for each gfc_code node invoked through gfc_code_walker |
| 319 |
from optimize_namespace. */ |
| 320 |
|
| 321 |
static int |
| 322 |
optimize_code (gfc_code **c, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 323 |
void *data ATTRIBUTE_UNUSED) |
| 324 |
{ |
| 325 |
|
| 326 |
gfc_exec_op op; |
| 327 |
|
| 328 |
op = (*c)->op; |
| 329 |
|
| 330 |
if (op == EXEC_CALL || op == EXEC_COMPCALL || op == EXEC_ASSIGN_CALL |
| 331 |
|| op == EXEC_CALL_PPC) |
| 332 |
count_arglist = 1; |
| 333 |
else |
| 334 |
count_arglist = 0; |
| 335 |
|
| 336 |
current_code = c; |
| 337 |
inserted_block = NULL; |
| 338 |
changed_statement = NULL; |
| 339 |
|
| 340 |
if (op == EXEC_ASSIGN) |
| 341 |
optimize_assignment (*c); |
| 342 |
return 0; |
| 343 |
} |
| 344 |
|
| 345 |
/* Callback for each gfc_expr node invoked through gfc_code_walker |
| 346 |
from optimize_namespace. */ |
| 347 |
|
| 348 |
static int |
| 349 |
optimize_expr (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 350 |
void *data ATTRIBUTE_UNUSED) |
| 351 |
{ |
| 352 |
bool function_expr; |
| 353 |
|
| 354 |
if ((*e)->expr_type == EXPR_FUNCTION) |
| 355 |
{ |
| 356 |
count_arglist ++; |
| 357 |
function_expr = true; |
| 358 |
} |
| 359 |
else |
| 360 |
function_expr = false; |
| 361 |
|
| 362 |
if (optimize_trim (*e)) |
| 363 |
gfc_simplify_expr (*e, 0); |
| 364 |
|
| 365 |
if (optimize_lexical_comparison (*e)) |
| 366 |
gfc_simplify_expr (*e, 0); |
| 367 |
|
| 368 |
if ((*e)->expr_type == EXPR_OP && optimize_op (*e)) |
| 369 |
gfc_simplify_expr (*e, 0); |
| 370 |
|
| 371 |
if (function_expr) |
| 372 |
count_arglist --; |
| 373 |
|
| 374 |
return 0; |
| 375 |
} |
| 376 |
|
| 377 |
/* Auxiliary function to handle the arguments to reduction intrinsics. If the |
| 378 |
function is a scalar, just copy it; otherwise returns the new element, the |
| 379 |
old one can be freed. */ |
| 380 |
|
| 381 |
static gfc_expr * |
| 382 |
copy_walk_reduction_arg (gfc_constructor *c, gfc_expr *fn) |
| 383 |
{ |
| 384 |
gfc_expr *fcn, *e = c->expr; |
| 385 |
|
| 386 |
fcn = gfc_copy_expr (e); |
| 387 |
if (c->iterator) |
| 388 |
{ |
| 389 |
gfc_constructor_base newbase; |
| 390 |
gfc_expr *new_expr; |
| 391 |
gfc_constructor *new_c; |
| 392 |
|
| 393 |
newbase = NULL; |
| 394 |
new_expr = gfc_get_expr (); |
| 395 |
new_expr->expr_type = EXPR_ARRAY; |
| 396 |
new_expr->ts = e->ts; |
| 397 |
new_expr->where = e->where; |
| 398 |
new_expr->rank = 1; |
| 399 |
new_c = gfc_constructor_append_expr (&newbase, fcn, &(e->where)); |
| 400 |
new_c->iterator = c->iterator; |
| 401 |
new_expr->value.constructor = newbase; |
| 402 |
c->iterator = NULL; |
| 403 |
|
| 404 |
fcn = new_expr; |
| 405 |
} |
| 406 |
|
| 407 |
if (fcn->rank != 0) |
| 408 |
{ |
| 409 |
gfc_isym_id id = fn->value.function.isym->id; |
| 410 |
|
| 411 |
if (id == GFC_ISYM_SUM || id == GFC_ISYM_PRODUCT) |
| 412 |
fcn = gfc_build_intrinsic_call (current_ns, id, |
| 413 |
fn->value.function.isym->name, |
| 414 |
fn->where, 3, fcn, NULL, NULL); |
| 415 |
else if (id == GFC_ISYM_ANY || id == GFC_ISYM_ALL) |
| 416 |
fcn = gfc_build_intrinsic_call (current_ns, id, |
| 417 |
fn->value.function.isym->name, |
| 418 |
fn->where, 2, fcn, NULL); |
| 419 |
else |
| 420 |
gfc_internal_error ("Illegal id in copy_walk_reduction_arg"); |
| 421 |
|
| 422 |
fcn->symtree->n.sym->attr.access = ACCESS_PRIVATE; |
| 423 |
} |
| 424 |
|
| 425 |
return fcn; |
| 426 |
} |
| 427 |
|
| 428 |
/* Callback function for optimization of reductions to scalars. Transform ANY |
| 429 |
([f1,f2,f3, ...]) to f1 .or. f2 .or. f3 .or. ..., with ANY, SUM and PRODUCT |
| 430 |
correspondingly. Handle only the simple cases without MASK and DIM. */ |
| 431 |
|
| 432 |
static int |
| 433 |
callback_reduction (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 434 |
void *data ATTRIBUTE_UNUSED) |
| 435 |
{ |
| 436 |
gfc_expr *fn, *arg; |
| 437 |
gfc_intrinsic_op op; |
| 438 |
gfc_isym_id id; |
| 439 |
gfc_actual_arglist *a; |
| 440 |
gfc_actual_arglist *dim; |
| 441 |
gfc_constructor *c; |
| 442 |
gfc_expr *res, *new_expr; |
| 443 |
gfc_actual_arglist *mask; |
| 444 |
|
| 445 |
fn = *e; |
| 446 |
|
| 447 |
if (fn->rank != 0 || fn->expr_type != EXPR_FUNCTION |
| 448 |
|| fn->value.function.isym == NULL) |
| 449 |
return 0; |
| 450 |
|
| 451 |
id = fn->value.function.isym->id; |
| 452 |
|
| 453 |
if (id != GFC_ISYM_SUM && id != GFC_ISYM_PRODUCT |
| 454 |
&& id != GFC_ISYM_ANY && id != GFC_ISYM_ALL) |
| 455 |
return 0; |
| 456 |
|
| 457 |
a = fn->value.function.actual; |
| 458 |
|
| 459 |
/* Don't handle MASK or DIM. */ |
| 460 |
|
| 461 |
dim = a->next; |
| 462 |
|
| 463 |
if (dim->expr != NULL) |
| 464 |
return 0; |
| 465 |
|
| 466 |
if (id == GFC_ISYM_SUM || id == GFC_ISYM_PRODUCT) |
| 467 |
{ |
| 468 |
mask = dim->next; |
| 469 |
if ( mask->expr != NULL) |
| 470 |
return 0; |
| 471 |
} |
| 472 |
|
| 473 |
arg = a->expr; |
| 474 |
|
| 475 |
if (arg->expr_type != EXPR_ARRAY) |
| 476 |
return 0; |
| 477 |
|
| 478 |
switch (id) |
| 479 |
{ |
| 480 |
case GFC_ISYM_SUM: |
| 481 |
op = INTRINSIC_PLUS; |
| 482 |
break; |
| 483 |
|
| 484 |
case GFC_ISYM_PRODUCT: |
| 485 |
op = INTRINSIC_TIMES; |
| 486 |
break; |
| 487 |
|
| 488 |
case GFC_ISYM_ANY: |
| 489 |
op = INTRINSIC_OR; |
| 490 |
break; |
| 491 |
|
| 492 |
case GFC_ISYM_ALL: |
| 493 |
op = INTRINSIC_AND; |
| 494 |
break; |
| 495 |
|
| 496 |
default: |
| 497 |
return 0; |
| 498 |
} |
| 499 |
|
| 500 |
c = gfc_constructor_first (arg->value.constructor); |
| 501 |
|
| 502 |
/* Don't do any simplififcation if we have |
| 503 |
- no element in the constructor or |
| 504 |
- only have a single element in the array which contains an |
| 505 |
iterator. */ |
| 506 |
|
| 507 |
if (c == NULL) |
| 508 |
return 0; |
| 509 |
|
| 510 |
res = copy_walk_reduction_arg (c, fn); |
| 511 |
|
| 512 |
c = gfc_constructor_next (c); |
| 513 |
while (c) |
| 514 |
{ |
| 515 |
new_expr = gfc_get_expr (); |
| 516 |
new_expr->ts = fn->ts; |
| 517 |
new_expr->expr_type = EXPR_OP; |
| 518 |
new_expr->rank = fn->rank; |
| 519 |
new_expr->corank = fn->corank; |
| 520 |
new_expr->where = fn->where; |
| 521 |
new_expr->value.op.op = op; |
| 522 |
new_expr->value.op.op1 = res; |
| 523 |
new_expr->value.op.op2 = copy_walk_reduction_arg (c, fn); |
| 524 |
res = new_expr; |
| 525 |
c = gfc_constructor_next (c); |
| 526 |
} |
| 527 |
|
| 528 |
gfc_simplify_expr (res, 0); |
| 529 |
*e = res; |
| 530 |
gfc_free_expr (fn); |
| 531 |
|
| 532 |
return 0; |
| 533 |
} |
| 534 |
|
| 535 |
/* Callback function for common function elimination, called from cfe_expr_0. |
| 536 |
Put all eligible function expressions into expr_array. */ |
| 537 |
|
| 538 |
static int |
| 539 |
cfe_register_funcs (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 540 |
void *data ATTRIBUTE_UNUSED) |
| 541 |
{ |
| 542 |
|
| 543 |
if ((*e)->expr_type != EXPR_FUNCTION) |
| 544 |
return 0; |
| 545 |
|
| 546 |
/* We don't do character functions with unknown charlens. */ |
| 547 |
if ((*e)->ts.type == BT_CHARACTER |
| 548 |
&& ((*e)->ts.u.cl == NULL || (*e)->ts.u.cl->length == NULL |
| 549 |
|| (*e)->ts.u.cl->length->expr_type != EXPR_CONSTANT)) |
| 550 |
return 0; |
| 551 |
|
| 552 |
/* We don't do function elimination within FORALL statements, it can |
| 553 |
lead to wrong-code in certain circumstances. */ |
| 554 |
|
| 555 |
if (forall_level > 0) |
| 556 |
return 0; |
| 557 |
|
| 558 |
/* Function elimination inside an iterator could lead to functions which |
| 559 |
depend on iterator variables being moved outside. FIXME: We should check |
| 560 |
if the functions do indeed depend on the iterator variable. */ |
| 561 |
|
| 562 |
if (iterator_level > 0) |
| 563 |
return 0; |
| 564 |
|
| 565 |
/* If we don't know the shape at compile time, we create an allocatable |
| 566 |
temporary variable to hold the intermediate result, but only if |
| 567 |
allocation on assignment is active. */ |
| 568 |
|
| 569 |
if ((*e)->rank > 0 && (*e)->shape == NULL && !flag_realloc_lhs) |
| 570 |
return 0; |
| 571 |
|
| 572 |
/* Skip the test for pure functions if -faggressive-function-elimination |
| 573 |
is specified. */ |
| 574 |
if ((*e)->value.function.esym) |
| 575 |
{ |
| 576 |
/* Don't create an array temporary for elemental functions. */ |
| 577 |
if ((*e)->value.function.esym->attr.elemental && (*e)->rank > 0) |
| 578 |
return 0; |
| 579 |
|
| 580 |
/* Only eliminate potentially impure functions if the |
| 581 |
user specifically requested it. */ |
| 582 |
if (!flag_aggressive_function_elimination |
| 583 |
&& !(*e)->value.function.esym->attr.pure |
| 584 |
&& !(*e)->value.function.esym->attr.implicit_pure) |
| 585 |
return 0; |
| 586 |
} |
| 587 |
|
| 588 |
if ((*e)->value.function.isym) |
| 589 |
{ |
| 590 |
/* Conversions are handled on the fly by the middle end, |
| 591 |
transpose during trans-* stages and TRANSFER by the middle end. */ |
| 592 |
if ((*e)->value.function.isym->id == GFC_ISYM_CONVERSION |
| 593 |
|| (*e)->value.function.isym->id == GFC_ISYM_TRANSFER |
| 594 |
|| gfc_inline_intrinsic_function_p (*e)) |
| 595 |
return 0; |
| 596 |
|
| 597 |
/* Don't create an array temporary for elemental functions, |
| 598 |
as this would be wasteful of memory. |
| 599 |
FIXME: Create a scalar temporary during scalarization. */ |
| 600 |
if ((*e)->value.function.isym->elemental && (*e)->rank > 0) |
| 601 |
return 0; |
| 602 |
|
| 603 |
if (!(*e)->value.function.isym->pure) |
| 604 |
return 0; |
| 605 |
} |
| 606 |
|
| 607 |
expr_array.safe_push (e); |
| 608 |
return 0; |
| 609 |
} |
| 610 |
|
| 611 |
/* Auxiliary function to check if an expression is a temporary created by |
| 612 |
create var. */ |
| 613 |
|
| 614 |
static bool |
| 615 |
is_fe_temp (gfc_expr *e) |
| 616 |
{ |
| 617 |
if (e->expr_type != EXPR_VARIABLE) |
| 618 |
return false; |
| 619 |
|
| 620 |
return e->symtree->n.sym->attr.fe_temp; |
| 621 |
} |
| 622 |
|
| 623 |
/* Determine the length of a string, if it can be evaluated as a constant |
| 624 |
expression. Return a newly allocated gfc_expr or NULL on failure. |
| 625 |
If the user specified a substring which is potentially longer than |
| 626 |
the string itself, the string will be padded with spaces, which |
| 627 |
is harmless. */ |
| 628 |
|
| 629 |
static gfc_expr * |
| 630 |
constant_string_length (gfc_expr *e) |
| 631 |
{ |
| 632 |
|
| 633 |
gfc_expr *length; |
| 634 |
gfc_ref *ref; |
| 635 |
gfc_expr *res; |
| 636 |
mpz_t value; |
| 637 |
|
| 638 |
if (e->ts.u.cl) |
| 639 |
{ |
| 640 |
length = e->ts.u.cl->length; |
| 641 |
if (length && length->expr_type == EXPR_CONSTANT) |
| 642 |
return gfc_copy_expr(length); |
| 643 |
} |
| 644 |
|
| 645 |
/* See if there is a substring. If it has a constant length, return |
| 646 |
that and NULL otherwise. */ |
| 647 |
for (ref = e->ref; ref; ref = ref->next) |
| 648 |
{ |
| 649 |
if (ref->type == REF_SUBSTRING) |
| 650 |
{ |
| 651 |
if (gfc_dep_difference (ref->u.ss.end, ref->u.ss.start, &value)) |
| 652 |
{ |
| 653 |
res = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind, |
| 654 |
&e->where); |
| 655 |
|
| 656 |
mpz_add_ui (res->value.integer, value, 1); |
| 657 |
mpz_clear (value); |
| 658 |
return res; |
| 659 |
} |
| 660 |
else |
| 661 |
return NULL; |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
/* Return length of char symbol, if constant. */ |
| 666 |
if (e->symtree && e->symtree->n.sym->ts.u.cl |
| 667 |
&& e->symtree->n.sym->ts.u.cl->length |
| 668 |
&& e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT) |
| 669 |
return gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length); |
| 670 |
|
| 671 |
return NULL; |
| 672 |
|
| 673 |
} |
| 674 |
|
| 675 |
/* Insert a block at the current position unless it has already |
| 676 |
been inserted; in this case use the one already there. */ |
| 677 |
|
| 678 |
static gfc_namespace* |
| 679 |
insert_block () |
| 680 |
{ |
| 681 |
gfc_namespace *ns; |
| 682 |
|
| 683 |
/* If the block hasn't already been created, do so. */ |
| 684 |
if (inserted_block == NULL) |
| 685 |
{ |
| 686 |
inserted_block = XCNEW (gfc_code); |
| 687 |
inserted_block->op = EXEC_BLOCK; |
| 688 |
inserted_block->loc = (*current_code)->loc; |
| 689 |
ns = gfc_build_block_ns (current_ns); |
| 690 |
inserted_block->ext.block.ns = ns; |
| 691 |
inserted_block->ext.block.assoc = NULL; |
| 692 |
|
| 693 |
ns->code = *current_code; |
| 694 |
|
| 695 |
/* If the statement has a label, make sure it is transferred to |
| 696 |
the newly created block. */ |
| 697 |
|
| 698 |
if ((*current_code)->here) |
| 699 |
{ |
| 700 |
inserted_block->here = (*current_code)->here; |
| 701 |
(*current_code)->here = NULL; |
| 702 |
} |
| 703 |
|
| 704 |
inserted_block->next = (*current_code)->next; |
| 705 |
changed_statement = &(inserted_block->ext.block.ns->code); |
| 706 |
(*current_code)->next = NULL; |
| 707 |
/* Insert the BLOCK at the right position. */ |
| 708 |
*current_code = inserted_block; |
| 709 |
ns->parent = current_ns; |
| 710 |
} |
| 711 |
else |
| 712 |
ns = inserted_block->ext.block.ns; |
| 713 |
|
| 714 |
return ns; |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
/* Insert a call to the intrinsic len. Use a different name for |
| 719 |
the symbol tree so we don't run into trouble when the user has |
| 720 |
renamed len for some reason. */ |
| 721 |
|
| 722 |
static gfc_expr* |
| 723 |
get_len_call (gfc_expr *str) |
| 724 |
{ |
| 725 |
gfc_expr *fcn; |
| 726 |
gfc_actual_arglist *actual_arglist; |
| 727 |
|
| 728 |
fcn = gfc_get_expr (); |
| 729 |
fcn->expr_type = EXPR_FUNCTION; |
| 730 |
fcn->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LEN); |
| 731 |
actual_arglist = gfc_get_actual_arglist (); |
| 732 |
actual_arglist->expr = str; |
| 733 |
|
| 734 |
fcn->value.function.actual = actual_arglist; |
| 735 |
fcn->where = str->where; |
| 736 |
fcn->ts.type = BT_INTEGER; |
| 737 |
fcn->ts.kind = gfc_charlen_int_kind; |
| 738 |
|
| 739 |
gfc_get_sym_tree ("__internal_len", current_ns, &fcn->symtree, false); |
| 740 |
fcn->symtree->n.sym->ts = fcn->ts; |
| 741 |
fcn->symtree->n.sym->attr.flavor = FL_PROCEDURE; |
| 742 |
fcn->symtree->n.sym->attr.function = 1; |
| 743 |
fcn->symtree->n.sym->attr.elemental = 1; |
| 744 |
fcn->symtree->n.sym->attr.referenced = 1; |
| 745 |
fcn->symtree->n.sym->attr.access = ACCESS_PRIVATE; |
| 746 |
gfc_commit_symbol (fcn->symtree->n.sym); |
| 747 |
|
| 748 |
return fcn; |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
/* Returns a new expression (a variable) to be used in place of the old one, |
| 753 |
with an optional assignment statement before the current statement to set |
| 754 |
the value of the variable. Creates a new BLOCK for the statement if that |
| 755 |
hasn't already been done and puts the statement, plus the newly created |
| 756 |
variables, in that block. Special cases: If the expression is constant or |
| 757 |
a temporary which has already been created, just copy it. */ |
| 758 |
|
| 759 |
static gfc_expr* |
| 760 |
create_var (gfc_expr * e, const char *vname) |
| 761 |
{ |
| 762 |
char name[GFC_MAX_SYMBOL_LEN +1]; |
| 763 |
gfc_symtree *symtree; |
| 764 |
gfc_symbol *symbol; |
| 765 |
gfc_expr *result; |
| 766 |
gfc_code *n; |
| 767 |
gfc_namespace *ns; |
| 768 |
int i; |
| 769 |
bool deferred; |
| 770 |
|
| 771 |
if (e->expr_type == EXPR_CONSTANT || is_fe_temp (e)) |
| 772 |
return gfc_copy_expr (e); |
| 773 |
|
| 774 |
/* Creation of an array of unknown size requires realloc on assignment. |
| 775 |
If that is not possible, just return NULL. */ |
| 776 |
if (flag_realloc_lhs == 0 && e->rank > 0 && e->shape == NULL) |
| 777 |
return NULL; |
| 778 |
|
| 779 |
ns = insert_block (); |
| 780 |
|
| 781 |
if (vname) |
| 782 |
snprintf (name, GFC_MAX_SYMBOL_LEN, "__var_%d_%s", var_num++, vname); |
| 783 |
else |
| 784 |
snprintf (name, GFC_MAX_SYMBOL_LEN, "__var_%d", var_num++); |
| 785 |
|
| 786 |
if (gfc_get_sym_tree (name, ns, &symtree, false) != 0) |
| 787 |
gcc_unreachable (); |
| 788 |
|
| 789 |
symbol = symtree->n.sym; |
| 790 |
symbol->ts = e->ts; |
| 791 |
|
| 792 |
if (e->rank > 0) |
| 793 |
{ |
| 794 |
symbol->as = gfc_get_array_spec (); |
| 795 |
symbol->as->rank = e->rank; |
| 796 |
symbol->as->corank = e->corank; |
| 797 |
|
| 798 |
if (e->shape == NULL) |
| 799 |
{ |
| 800 |
/* We don't know the shape at compile time, so we use an |
| 801 |
allocatable. */ |
| 802 |
symbol->as->type = AS_DEFERRED; |
| 803 |
symbol->attr.allocatable = 1; |
| 804 |
} |
| 805 |
else |
| 806 |
{ |
| 807 |
symbol->as->type = AS_EXPLICIT; |
| 808 |
/* Copy the shape. */ |
| 809 |
for (i=0; i<e->rank; i++) |
| 810 |
{ |
| 811 |
gfc_expr *p, *q; |
| 812 |
|
| 813 |
p = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind, |
| 814 |
&(e->where)); |
| 815 |
mpz_set_si (p->value.integer, 1); |
| 816 |
symbol->as->lower[i] = p; |
| 817 |
|
| 818 |
q = gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind, |
| 819 |
&(e->where)); |
| 820 |
mpz_set (q->value.integer, e->shape[i]); |
| 821 |
symbol->as->upper[i] = q; |
| 822 |
} |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
deferred = 0; |
| 827 |
if (e->ts.type == BT_CHARACTER) |
| 828 |
{ |
| 829 |
gfc_expr *length; |
| 830 |
|
| 831 |
symbol->ts.u.cl = gfc_new_charlen (ns, NULL); |
| 832 |
length = constant_string_length (e); |
| 833 |
if (length) |
| 834 |
symbol->ts.u.cl->length = length; |
| 835 |
else if (e->expr_type == EXPR_VARIABLE |
| 836 |
&& e->symtree->n.sym->ts.type == BT_CHARACTER |
| 837 |
&& e->ts.u.cl->length) |
| 838 |
symbol->ts.u.cl->length = get_len_call (gfc_copy_expr (e)); |
| 839 |
else |
| 840 |
{ |
| 841 |
symbol->attr.allocatable = 1; |
| 842 |
symbol->ts.u.cl->length = NULL; |
| 843 |
symbol->ts.deferred = 1; |
| 844 |
deferred = 1; |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
symbol->attr.flavor = FL_VARIABLE; |
| 849 |
symbol->attr.referenced = 1; |
| 850 |
symbol->attr.dimension = e->rank > 0; |
| 851 |
symbol->attr.fe_temp = 1; |
| 852 |
symbol->attr.automatic = 1; |
| 853 |
gfc_commit_symbol (symbol); |
| 854 |
|
| 855 |
result = gfc_get_expr (); |
| 856 |
result->expr_type = EXPR_VARIABLE; |
| 857 |
result->ts = symbol->ts; |
| 858 |
result->ts.deferred = deferred; |
| 859 |
result->rank = e->rank; |
| 860 |
result->corank = e->corank; |
| 861 |
result->shape = gfc_copy_shape (e->shape, e->rank); |
| 862 |
result->symtree = symtree; |
| 863 |
result->where = e->where; |
| 864 |
if (e->rank > 0) |
| 865 |
{ |
| 866 |
result->ref = gfc_get_ref (); |
| 867 |
result->ref->type = REF_ARRAY; |
| 868 |
result->ref->u.ar.type = AR_FULL; |
| 869 |
result->ref->u.ar.where = e->where; |
| 870 |
result->ref->u.ar.dimen = e->rank; |
| 871 |
result->ref->u.ar.as = symbol->ts.type == BT_CLASS |
| 872 |
? CLASS_DATA (symbol)->as : symbol->as; |
| 873 |
if (warn_array_temporaries) |
| 874 |
gfc_warning (OPT_Warray_temporaries, |
| 875 |
"Creating array temporary at %L", &(e->where)); |
| 876 |
} |
| 877 |
|
| 878 |
/* Generate the new assignment. */ |
| 879 |
n = XCNEW (gfc_code); |
| 880 |
n->op = EXEC_ASSIGN; |
| 881 |
n->loc = (*current_code)->loc; |
| 882 |
n->next = *changed_statement; |
| 883 |
n->expr1 = gfc_copy_expr (result); |
| 884 |
n->expr2 = e; |
| 885 |
*changed_statement = n; |
| 886 |
n_vars ++; |
| 887 |
|
| 888 |
return result; |
| 889 |
} |
| 890 |
|
| 891 |
/* Warn about function elimination. */ |
| 892 |
|
| 893 |
static void |
| 894 |
do_warn_function_elimination (gfc_expr *e) |
| 895 |
{ |
| 896 |
const char *name; |
| 897 |
if (e->expr_type == EXPR_FUNCTION |
| 898 |
&& !gfc_pure_function (e, &name) && !gfc_implicit_pure_function (e)) |
| 899 |
{ |
| 900 |
if (name) |
| 901 |
gfc_warning (OPT_Wfunction_elimination, |
| 902 |
"Removing call to impure function %qs at %L", name, |
| 903 |
&(e->where)); |
| 904 |
else |
| 905 |
gfc_warning (OPT_Wfunction_elimination, |
| 906 |
"Removing call to impure function at %L", |
| 907 |
&(e->where)); |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
/* Callback function for the code walker for doing common function |
| 913 |
elimination. This builds up the list of functions in the expression |
| 914 |
and goes through them to detect duplicates, which it then replaces |
| 915 |
by variables. */ |
| 916 |
|
| 917 |
static int |
| 918 |
cfe_expr_0 (gfc_expr **e, int *walk_subtrees, |
| 919 |
void *data ATTRIBUTE_UNUSED) |
| 920 |
{ |
| 921 |
int i,j; |
| 922 |
gfc_expr *newvar; |
| 923 |
gfc_expr **ei, **ej; |
| 924 |
|
| 925 |
/* Don't do this optimization within OMP workshare/atomic or ASSOC lists. */ |
| 926 |
|
| 927 |
if (in_omp_workshare || in_omp_atomic || in_assoc_list) |
| 928 |
{ |
| 929 |
*walk_subtrees = 0; |
| 930 |
return 0; |
| 931 |
} |
| 932 |
|
| 933 |
expr_array.release (); |
| 934 |
|
| 935 |
gfc_expr_walker (e, cfe_register_funcs, NULL); |
| 936 |
|
| 937 |
/* Walk through all the functions. */ |
| 938 |
|
| 939 |
FOR_EACH_VEC_ELT_FROM (expr_array, i, ei, 1) |
| 940 |
{ |
| 941 |
/* Skip if the function has been replaced by a variable already. */ |
| 942 |
if ((*ei)->expr_type == EXPR_VARIABLE) |
| 943 |
continue; |
| 944 |
|
| 945 |
newvar = NULL; |
| 946 |
for (j=0; j<i; j++) |
| 947 |
{ |
| 948 |
ej = expr_array[j]; |
| 949 |
if (gfc_dep_compare_functions (*ei, *ej, true) == 0) |
| 950 |
{ |
| 951 |
if (newvar == NULL) |
| 952 |
newvar = create_var (*ei, "fcn"); |
| 953 |
|
| 954 |
if (warn_function_elimination) |
| 955 |
do_warn_function_elimination (*ej); |
| 956 |
|
| 957 |
free (*ej); |
| 958 |
*ej = gfc_copy_expr (newvar); |
| 959 |
} |
| 960 |
} |
| 961 |
if (newvar) |
| 962 |
*ei = newvar; |
| 963 |
} |
| 964 |
|
| 965 |
/* We did all the necessary walking in this function. */ |
| 966 |
*walk_subtrees = 0; |
| 967 |
return 0; |
| 968 |
} |
| 969 |
|
| 970 |
/* Callback function for common function elimination, called from |
| 971 |
gfc_code_walker. This keeps track of the current code, in order |
| 972 |
to insert statements as needed. */ |
| 973 |
|
| 974 |
static int |
| 975 |
cfe_code (gfc_code **c, int *walk_subtrees, void *data ATTRIBUTE_UNUSED) |
| 976 |
{ |
| 977 |
current_code = c; |
| 978 |
inserted_block = NULL; |
| 979 |
changed_statement = NULL; |
| 980 |
|
| 981 |
/* Do not do anything inside a WHERE statement; scalar assignments, BLOCKs |
| 982 |
and allocation on assignment are prohibited inside WHERE, and finally |
| 983 |
masking an expression would lead to wrong-code when replacing |
| 984 |
|
| 985 |
WHERE (a>0) |
| 986 |
b = sum(foo(a) + foo(a)) |
| 987 |
END WHERE |
| 988 |
|
| 989 |
with |
| 990 |
|
| 991 |
WHERE (a > 0) |
| 992 |
tmp = foo(a) |
| 993 |
b = sum(tmp + tmp) |
| 994 |
END WHERE |
| 995 |
*/ |
| 996 |
|
| 997 |
if ((*c)->op == EXEC_WHERE) |
| 998 |
{ |
| 999 |
*walk_subtrees = 0; |
| 1000 |
return 0; |
| 1001 |
} |
| 1002 |
|
| 1003 |
|
| 1004 |
return 0; |
| 1005 |
} |
| 1006 |
|
| 1007 |
/* Dummy function for expression call back, for use when we |
| 1008 |
really don't want to do any walking. */ |
| 1009 |
|
| 1010 |
static int |
| 1011 |
dummy_expr_callback (gfc_expr **e ATTRIBUTE_UNUSED, int *walk_subtrees, |
| 1012 |
void *data ATTRIBUTE_UNUSED) |
| 1013 |
{ |
| 1014 |
*walk_subtrees = 0; |
| 1015 |
return 0; |
| 1016 |
} |
| 1017 |
|
| 1018 |
/* Dummy function for code callback, for use when we really |
| 1019 |
don't want to do anything. */ |
| 1020 |
int |
| 1021 |
gfc_dummy_code_callback (gfc_code **e ATTRIBUTE_UNUSED, |
| 1022 |
int *walk_subtrees ATTRIBUTE_UNUSED, |
| 1023 |
void *data ATTRIBUTE_UNUSED) |
| 1024 |
{ |
| 1025 |
return 0; |
| 1026 |
} |
| 1027 |
|
| 1028 |
/* Code callback function for converting |
| 1029 |
do while(a) |
| 1030 |
end do |
| 1031 |
into the equivalent |
| 1032 |
do |
| 1033 |
if (.not. a) exit |
| 1034 |
end do |
| 1035 |
This is because common function elimination would otherwise place the |
| 1036 |
temporary variables outside the loop. */ |
| 1037 |
|
| 1038 |
static int |
| 1039 |
convert_do_while (gfc_code **c, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 1040 |
void *data ATTRIBUTE_UNUSED) |
| 1041 |
{ |
| 1042 |
gfc_code *co = *c; |
| 1043 |
gfc_code *c_if1, *c_if2, *c_exit; |
| 1044 |
gfc_code *loopblock; |
| 1045 |
gfc_expr *e_not, *e_cond; |
| 1046 |
|
| 1047 |
if (co->op != EXEC_DO_WHILE) |
| 1048 |
return 0; |
| 1049 |
|
| 1050 |
if (co->expr1 == NULL || co->expr1->expr_type == EXPR_CONSTANT) |
| 1051 |
return 0; |
| 1052 |
|
| 1053 |
e_cond = co->expr1; |
| 1054 |
|
| 1055 |
/* Generate the condition of the if statement, which is .not. the original |
| 1056 |
statement. */ |
| 1057 |
e_not = gfc_get_expr (); |
| 1058 |
e_not->ts = e_cond->ts; |
| 1059 |
e_not->where = e_cond->where; |
| 1060 |
e_not->expr_type = EXPR_OP; |
| 1061 |
e_not->value.op.op = INTRINSIC_NOT; |
| 1062 |
e_not->value.op.op1 = e_cond; |
| 1063 |
|
| 1064 |
/* Generate the EXIT statement. */ |
| 1065 |
c_exit = XCNEW (gfc_code); |
| 1066 |
c_exit->op = EXEC_EXIT; |
| 1067 |
c_exit->ext.which_construct = co; |
| 1068 |
c_exit->loc = co->loc; |
| 1069 |
|
| 1070 |
/* Generate the IF statement. */ |
| 1071 |
c_if2 = XCNEW (gfc_code); |
| 1072 |
c_if2->op = EXEC_IF; |
| 1073 |
c_if2->expr1 = e_not; |
| 1074 |
c_if2->next = c_exit; |
| 1075 |
c_if2->loc = co->loc; |
| 1076 |
|
| 1077 |
/* ... plus the one to chain it to. */ |
| 1078 |
c_if1 = XCNEW (gfc_code); |
| 1079 |
c_if1->op = EXEC_IF; |
| 1080 |
c_if1->block = c_if2; |
| 1081 |
c_if1->loc = co->loc; |
| 1082 |
|
| 1083 |
/* Make the DO WHILE loop into a DO block by replacing the condition |
| 1084 |
with a true constant. */ |
| 1085 |
co->expr1 = gfc_get_logical_expr (gfc_default_integer_kind, &co->loc, true); |
| 1086 |
|
| 1087 |
/* Hang the generated if statement into the loop body. */ |
| 1088 |
|
| 1089 |
loopblock = co->block->next; |
| 1090 |
co->block->next = c_if1; |
| 1091 |
c_if1->next = loopblock; |
| 1092 |
|
| 1093 |
return 0; |
| 1094 |
} |
| 1095 |
|
| 1096 |
/* Code callback function for converting |
| 1097 |
if (a) then |
| 1098 |
... |
| 1099 |
else if (b) then |
| 1100 |
end if |
| 1101 |
|
| 1102 |
into |
| 1103 |
if (a) then |
| 1104 |
else |
| 1105 |
if (b) then |
| 1106 |
end if |
| 1107 |
end if |
| 1108 |
|
| 1109 |
because otherwise common function elimination would place the BLOCKs |
| 1110 |
into the wrong place. */ |
| 1111 |
|
| 1112 |
static int |
| 1113 |
convert_elseif (gfc_code **c, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 1114 |
void *data ATTRIBUTE_UNUSED) |
| 1115 |
{ |
| 1116 |
gfc_code *co = *c; |
| 1117 |
gfc_code *c_if1, *c_if2, *else_stmt; |
| 1118 |
|
| 1119 |
if (co->op != EXEC_IF) |
| 1120 |
return 0; |
| 1121 |
|
| 1122 |
/* This loop starts out with the first ELSE statement. */ |
| 1123 |
else_stmt = co->block->block; |
| 1124 |
|
| 1125 |
while (else_stmt != NULL) |
| 1126 |
{ |
| 1127 |
gfc_code *next_else; |
| 1128 |
|
| 1129 |
/* If there is no condition, we're done. */ |
| 1130 |
if (else_stmt->expr1 == NULL) |
| 1131 |
break; |
| 1132 |
|
| 1133 |
next_else = else_stmt->block; |
| 1134 |
|
| 1135 |
/* Generate the new IF statement. */ |
| 1136 |
c_if2 = XCNEW (gfc_code); |
| 1137 |
c_if2->op = EXEC_IF; |
| 1138 |
c_if2->expr1 = else_stmt->expr1; |
| 1139 |
c_if2->next = else_stmt->next; |
| 1140 |
c_if2->loc = else_stmt->loc; |
| 1141 |
c_if2->block = next_else; |
| 1142 |
|
| 1143 |
/* ... plus the one to chain it to. */ |
| 1144 |
c_if1 = XCNEW (gfc_code); |
| 1145 |
c_if1->op = EXEC_IF; |
| 1146 |
c_if1->block = c_if2; |
| 1147 |
c_if1->loc = else_stmt->loc; |
| 1148 |
|
| 1149 |
/* Insert the new IF after the ELSE. */ |
| 1150 |
else_stmt->expr1 = NULL; |
| 1151 |
else_stmt->next = c_if1; |
| 1152 |
else_stmt->block = NULL; |
| 1153 |
|
| 1154 |
else_stmt = next_else; |
| 1155 |
} |
| 1156 |
/* Don't walk subtrees. */ |
| 1157 |
return 0; |
| 1158 |
} |
| 1159 |
|
| 1160 |
/* Callback function to var_in_expr - return true if expr1 and |
| 1161 |
expr2 are identical variables. */ |
| 1162 |
static int |
| 1163 |
var_in_expr_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 1164 |
void *data) |
| 1165 |
{ |
| 1166 |
gfc_expr *expr1 = (gfc_expr *) data; |
| 1167 |
gfc_expr *expr2 = *e; |
| 1168 |
|
| 1169 |
if (expr2->expr_type != EXPR_VARIABLE) |
| 1170 |
return 0; |
| 1171 |
|
| 1172 |
return expr1->symtree->n.sym == expr2->symtree->n.sym; |
| 1173 |
} |
| 1174 |
|
| 1175 |
/* Return true if expr1 is found in expr2. */ |
| 1176 |
|
| 1177 |
static bool |
| 1178 |
var_in_expr (gfc_expr *expr1, gfc_expr *expr2) |
| 1179 |
{ |
| 1180 |
gcc_assert (expr1->expr_type == EXPR_VARIABLE); |
| 1181 |
|
| 1182 |
return gfc_expr_walker (&expr2, var_in_expr_callback, (void *) expr1); |
| 1183 |
} |
| 1184 |
|
| 1185 |
struct do_stack |
| 1186 |
{ |
| 1187 |
struct do_stack *prev; |
| 1188 |
gfc_iterator *iter; |
| 1189 |
gfc_code *code; |
| 1190 |
} *stack_top; |
| 1191 |
|
| 1192 |
/* Recursively traverse the block of a WRITE or READ statement, and maybe |
| 1193 |
optimize by replacing do loops with their analog array slices. For |
| 1194 |
example: |
| 1195 |
|
| 1196 |
write (*,*) (a(i), i=1,4) |
| 1197 |
|
| 1198 |
is replaced with |
| 1199 |
|
| 1200 |
write (*,*) a(1:4:1) . */ |
| 1201 |
|
| 1202 |
static bool |
| 1203 |
traverse_io_block (gfc_code *code, bool *has_reached, gfc_code *prev) |
| 1204 |
{ |
| 1205 |
gfc_code *curr; |
| 1206 |
gfc_expr *new_e, *expr, *start; |
| 1207 |
gfc_ref *ref; |
| 1208 |
struct do_stack ds_push; |
| 1209 |
int i, future_rank = 0; |
| 1210 |
gfc_iterator *iters[GFC_MAX_DIMENSIONS]; |
| 1211 |
gfc_expr *e; |
| 1212 |
|
| 1213 |
/* Find the first transfer/do statement. */ |
| 1214 |
for (curr = code; curr; curr = curr->next) |
| 1215 |
{ |
| 1216 |
if (curr->op == EXEC_DO || curr->op == EXEC_TRANSFER) |
| 1217 |
break; |
| 1218 |
} |
| 1219 |
|
| 1220 |
/* Ensure it is the only transfer/do statement because cases like |
| 1221 |
|
| 1222 |
write (*,*) (a(i), b(i), i=1,4) |
| 1223 |
|
| 1224 |
cannot be optimized. */ |
| 1225 |
|
| 1226 |
if (!curr || curr->next) |
| 1227 |
return false; |
| 1228 |
|
| 1229 |
if (curr->op == EXEC_DO) |
| 1230 |
{ |
| 1231 |
if (curr->ext.iterator->var->ref) |
| 1232 |
return false; |
| 1233 |
ds_push.prev = stack_top; |
| 1234 |
ds_push.iter = curr->ext.iterator; |
| 1235 |
ds_push.code = curr; |
| 1236 |
stack_top = &ds_push; |
| 1237 |
if (traverse_io_block (curr->block->next, has_reached, prev)) |
| 1238 |
{ |
| 1239 |
if (curr != stack_top->code && !*has_reached) |
| 1240 |
{ |
| 1241 |
curr->block->next = NULL; |
| 1242 |
gfc_free_statements (curr); |
| 1243 |
} |
| 1244 |
else |
| 1245 |
*has_reached = true; |
| 1246 |
return true; |
| 1247 |
} |
| 1248 |
return false; |
| 1249 |
} |
| 1250 |
|
| 1251 |
gcc_assert (curr->op == EXEC_TRANSFER); |
| 1252 |
|
| 1253 |
e = curr->expr1; |
| 1254 |
ref = e->ref; |
| 1255 |
if (!ref || ref->type != REF_ARRAY || ref->u.ar.codimen != 0 || ref->next) |
| 1256 |
return false; |
| 1257 |
|
| 1258 |
/* Find the iterators belonging to each variable and check conditions. */ |
| 1259 |
for (i = 0; i < ref->u.ar.dimen; i++) |
| 1260 |
{ |
| 1261 |
if (!ref->u.ar.start[i] || ref->u.ar.start[i]->ref |
| 1262 |
|| ref->u.ar.dimen_type[i] != DIMEN_ELEMENT) |
| 1263 |
return false; |
| 1264 |
|
| 1265 |
start = ref->u.ar.start[i]; |
| 1266 |
gfc_simplify_expr (start, 0); |
| 1267 |
switch (start->expr_type) |
| 1268 |
{ |
| 1269 |
case EXPR_VARIABLE: |
| 1270 |
|
| 1271 |
/* write (*,*) (a(i), i=a%b,1) not handled yet. */ |
| 1272 |
if (start->ref) |
| 1273 |
return false; |
| 1274 |
|
| 1275 |
/* Check for (a(k), i=1,4) or ((a(j, i), i=1,4), j=1,4). */ |
| 1276 |
if (!stack_top || !stack_top->iter |
| 1277 |
|| stack_top->iter->var->symtree != start->symtree) |
| 1278 |
{ |
| 1279 |
/* Check for (a(i,i), i=1,3). */ |
| 1280 |
int j; |
| 1281 |
|
| 1282 |
for (j=0; j<i; j++) |
| 1283 |
if (iters[j] && iters[j]->var->symtree == start->symtree) |
| 1284 |
return false; |
| 1285 |
|
| 1286 |
iters[i] = NULL; |
| 1287 |
} |
| 1288 |
else |
| 1289 |
{ |
| 1290 |
iters[i] = stack_top->iter; |
| 1291 |
stack_top = stack_top->prev; |
| 1292 |
future_rank++; |
| 1293 |
} |
| 1294 |
break; |
| 1295 |
case EXPR_CONSTANT: |
| 1296 |
iters[i] = NULL; |
| 1297 |
break; |
| 1298 |
case EXPR_OP: |
| 1299 |
switch (start->value.op.op) |
| 1300 |
{ |
| 1301 |
case INTRINSIC_PLUS: |
| 1302 |
case INTRINSIC_TIMES: |
| 1303 |
if (start->value.op.op1->expr_type != EXPR_VARIABLE) |
| 1304 |
std::swap (start->value.op.op1, start->value.op.op2); |
| 1305 |
gcc_fallthrough (); |
| 1306 |
case INTRINSIC_MINUS: |
| 1307 |
if (start->value.op.op1->expr_type!= EXPR_VARIABLE |
| 1308 |
|| start->value.op.op2->expr_type != EXPR_CONSTANT |
| 1309 |
|| start->value.op.op1->ref) |
| 1310 |
return false; |
| 1311 |
if (!stack_top || !stack_top->iter |
| 1312 |
|| stack_top->iter->var->symtree |
| 1313 |
!= start->value.op.op1->symtree) |
| 1314 |
return false; |
| 1315 |
iters[i] = stack_top->iter; |
| 1316 |
stack_top = stack_top->prev; |
| 1317 |
break; |
| 1318 |
default: |
| 1319 |
return false; |
| 1320 |
} |
| 1321 |
future_rank++; |
| 1322 |
break; |
| 1323 |
default: |
| 1324 |
return false; |
| 1325 |
} |
| 1326 |
} |
| 1327 |
|
| 1328 |
/* Check for cases like ((a(i, j), i=1, j), j=1, 2). */ |
| 1329 |
for (int i = 1; i < ref->u.ar.dimen; i++) |
| 1330 |
{ |
| 1331 |
if (iters[i]) |
| 1332 |
{ |
| 1333 |
gfc_expr *var = iters[i]->var; |
| 1334 |
for (int j = 0; j < i; j++) |
| 1335 |
{ |
| 1336 |
if (iters[j] |
| 1337 |
&& (var_in_expr (var, iters[j]->start) |
| 1338 |
|| var_in_expr (var, iters[j]->end) |
| 1339 |
|| var_in_expr (var, iters[j]->step))) |
| 1340 |
return false; |
| 1341 |
} |
| 1342 |
} |
| 1343 |
} |
| 1344 |
|
| 1345 |
/* Create new expr. */ |
| 1346 |
new_e = gfc_copy_expr (curr->expr1); |
| 1347 |
new_e->expr_type = EXPR_VARIABLE; |
| 1348 |
new_e->rank = future_rank; |
| 1349 |
if (curr->expr1->shape) |
| 1350 |
new_e->shape = gfc_get_shape (new_e->rank); |
| 1351 |
|
| 1352 |
/* Assign new starts, ends and strides if necessary. */ |
| 1353 |
for (i = 0; i < ref->u.ar.dimen; i++) |
| 1354 |
{ |
| 1355 |
if (!iters[i]) |
| 1356 |
continue; |
| 1357 |
start = ref->u.ar.start[i]; |
| 1358 |
switch (start->expr_type) |
| 1359 |
{ |
| 1360 |
case EXPR_CONSTANT: |
| 1361 |
gfc_internal_error ("bad expression"); |
| 1362 |
break; |
| 1363 |
case EXPR_VARIABLE: |
| 1364 |
new_e->ref->u.ar.dimen_type[i] = DIMEN_RANGE; |
| 1365 |
new_e->ref->u.ar.type = AR_SECTION; |
| 1366 |
gfc_free_expr (new_e->ref->u.ar.start[i]); |
| 1367 |
new_e->ref->u.ar.start[i] = gfc_copy_expr (iters[i]->start); |
| 1368 |
new_e->ref->u.ar.end[i] = gfc_copy_expr (iters[i]->end); |
| 1369 |
new_e->ref->u.ar.stride[i] = gfc_copy_expr (iters[i]->step); |
| 1370 |
break; |
| 1371 |
case EXPR_OP: |
| 1372 |
new_e->ref->u.ar.dimen_type[i] = DIMEN_RANGE; |
| 1373 |
new_e->ref->u.ar.type = AR_SECTION; |
| 1374 |
gfc_free_expr (new_e->ref->u.ar.start[i]); |
| 1375 |
expr = gfc_copy_expr (start); |
| 1376 |
expr->value.op.op1 = gfc_copy_expr (iters[i]->start); |
| 1377 |
new_e->ref->u.ar.start[i] = expr; |
| 1378 |
gfc_simplify_expr (new_e->ref->u.ar.start[i], 0); |
| 1379 |
expr = gfc_copy_expr (start); |
| 1380 |
expr->value.op.op1 = gfc_copy_expr (iters[i]->end); |
| 1381 |
new_e->ref->u.ar.end[i] = expr; |
| 1382 |
gfc_simplify_expr (new_e->ref->u.ar.end[i], 0); |
| 1383 |
switch (start->value.op.op) |
| 1384 |
{ |
| 1385 |
case INTRINSIC_MINUS: |
| 1386 |
case INTRINSIC_PLUS: |
| 1387 |
new_e->ref->u.ar.stride[i] = gfc_copy_expr (iters[i]->step); |
| 1388 |
break; |
| 1389 |
case INTRINSIC_TIMES: |
| 1390 |
expr = gfc_copy_expr (start); |
| 1391 |
expr->value.op.op1 = gfc_copy_expr (iters[i]->step); |
| 1392 |
new_e->ref->u.ar.stride[i] = expr; |
| 1393 |
gfc_simplify_expr (new_e->ref->u.ar.stride[i], 0); |
| 1394 |
break; |
| 1395 |
default: |
| 1396 |
gfc_internal_error ("bad op"); |
| 1397 |
} |
| 1398 |
break; |
| 1399 |
default: |
| 1400 |
gfc_internal_error ("bad expression"); |
| 1401 |
} |
| 1402 |
} |
| 1403 |
curr->expr1 = new_e; |
| 1404 |
|
| 1405 |
/* Insert modified statement. Check whether the statement needs to be |
| 1406 |
inserted at the lowest level. */ |
| 1407 |
if (!stack_top->iter) |
| 1408 |
{ |
| 1409 |
if (prev) |
| 1410 |
{ |
| 1411 |
curr->next = prev->next->next; |
| 1412 |
prev->next = curr; |
| 1413 |
} |
| 1414 |
else |
| 1415 |
{ |
| 1416 |
curr->next = stack_top->code->block->next->next->next; |
| 1417 |
stack_top->code->block->next = curr; |
| 1418 |
} |
| 1419 |
} |
| 1420 |
else |
| 1421 |
stack_top->code->block->next = curr; |
| 1422 |
return true; |
| 1423 |
} |
| 1424 |
|
| 1425 |
/* Function for the gfc_code_walker. If code is a READ or WRITE statement, it |
| 1426 |
tries to optimize its block. */ |
| 1427 |
|
| 1428 |
static int |
| 1429 |
simplify_io_impl_do (gfc_code **code, int *walk_subtrees, |
| 1430 |
void *data ATTRIBUTE_UNUSED) |
| 1431 |
{ |
| 1432 |
gfc_code **curr, *prev = NULL; |
| 1433 |
struct do_stack write, first; |
| 1434 |
bool b = false; |
| 1435 |
*walk_subtrees = 1; |
| 1436 |
if (!(*code)->block |
| 1437 |
|| ((*code)->block->op != EXEC_WRITE |
| 1438 |
&& (*code)->block->op != EXEC_READ)) |
| 1439 |
return 0; |
| 1440 |
|
| 1441 |
*walk_subtrees = 0; |
| 1442 |
write.prev = NULL; |
| 1443 |
write.iter = NULL; |
| 1444 |
write.code = *code; |
| 1445 |
|
| 1446 |
for (curr = &(*code)->block; *curr; curr = &(*curr)->next) |
| 1447 |
{ |
| 1448 |
if ((*curr)->op == EXEC_DO) |
| 1449 |
{ |
| 1450 |
first.prev = &write; |
| 1451 |
first.iter = (*curr)->ext.iterator; |
| 1452 |
first.code = *curr; |
| 1453 |
stack_top = &first; |
| 1454 |
traverse_io_block ((*curr)->block->next, &b, prev); |
| 1455 |
stack_top = NULL; |
| 1456 |
} |
| 1457 |
prev = *curr; |
| 1458 |
} |
| 1459 |
return 0; |
| 1460 |
} |
| 1461 |
|
| 1462 |
/* Optimize a namespace, including all contained namespaces. |
| 1463 |
flag_frontend_optimize and flag_frontend_loop_interchange are |
| 1464 |
handled separately. */ |
| 1465 |
|
| 1466 |
static void |
| 1467 |
optimize_namespace (gfc_namespace *ns) |
| 1468 |
{ |
| 1469 |
gfc_namespace *saved_ns = gfc_current_ns; |
| 1470 |
current_ns = ns; |
| 1471 |
gfc_current_ns = ns; |
| 1472 |
forall_level = 0; |
| 1473 |
iterator_level = 0; |
| 1474 |
in_assoc_list = false; |
| 1475 |
in_omp_workshare = false; |
| 1476 |
in_omp_atomic = false; |
| 1477 |
|
| 1478 |
if (flag_frontend_optimize) |
| 1479 |
{ |
| 1480 |
gfc_code_walker (&ns->code, simplify_io_impl_do, dummy_expr_callback, NULL); |
| 1481 |
gfc_code_walker (&ns->code, convert_do_while, dummy_expr_callback, NULL); |
| 1482 |
gfc_code_walker (&ns->code, convert_elseif, dummy_expr_callback, NULL); |
| 1483 |
gfc_code_walker (&ns->code, cfe_code, cfe_expr_0, NULL); |
| 1484 |
gfc_code_walker (&ns->code, optimize_code, optimize_expr, NULL); |
| 1485 |
if (flag_inline_matmul_limit != 0 || flag_external_blas) |
| 1486 |
{ |
| 1487 |
bool found; |
| 1488 |
do |
| 1489 |
{ |
| 1490 |
found = false; |
| 1491 |
gfc_code_walker (&ns->code, matmul_to_var_code, matmul_to_var_expr, |
| 1492 |
(void *) &found); |
| 1493 |
} |
| 1494 |
while (found); |
| 1495 |
|
| 1496 |
gfc_code_walker (&ns->code, matmul_temp_args, dummy_expr_callback, |
| 1497 |
NULL); |
| 1498 |
} |
| 1499 |
|
| 1500 |
if (flag_external_blas) |
| 1501 |
gfc_code_walker (&ns->code, call_external_blas, dummy_expr_callback, |
| 1502 |
NULL); |
| 1503 |
|
| 1504 |
if (flag_inline_matmul_limit != 0) |
| 1505 |
gfc_code_walker (&ns->code, inline_matmul_assign, dummy_expr_callback, |
| 1506 |
NULL); |
| 1507 |
} |
| 1508 |
|
| 1509 |
if (flag_frontend_loop_interchange) |
| 1510 |
gfc_code_walker (&ns->code, index_interchange, dummy_expr_callback, |
| 1511 |
NULL); |
| 1512 |
|
| 1513 |
/* BLOCKs are handled in the expression walker below. */ |
| 1514 |
for (ns = ns->contained; ns; ns = ns->sibling) |
| 1515 |
{ |
| 1516 |
if (ns->code == NULL || ns->code->op != EXEC_BLOCK) |
| 1517 |
optimize_namespace (ns); |
| 1518 |
} |
| 1519 |
gfc_current_ns = saved_ns; |
| 1520 |
} |
| 1521 |
|
| 1522 |
/* Handle dependencies for allocatable strings which potentially redefine |
| 1523 |
themselves in an assignment. */ |
| 1524 |
|
| 1525 |
static void |
| 1526 |
realloc_strings (gfc_namespace *ns) |
| 1527 |
{ |
| 1528 |
current_ns = ns; |
| 1529 |
gfc_code_walker (&ns->code, realloc_string_callback, dummy_expr_callback, NULL); |
| 1530 |
|
| 1531 |
for (ns = ns->contained; ns; ns = ns->sibling) |
| 1532 |
{ |
| 1533 |
if (ns->code == NULL || ns->code->op != EXEC_BLOCK) |
| 1534 |
realloc_strings (ns); |
| 1535 |
} |
| 1536 |
|
| 1537 |
} |
| 1538 |
|
| 1539 |
static void |
| 1540 |
optimize_reduction (gfc_namespace *ns) |
| 1541 |
{ |
| 1542 |
current_ns = ns; |
| 1543 |
gfc_code_walker (&ns->code, gfc_dummy_code_callback, |
| 1544 |
callback_reduction, NULL); |
| 1545 |
|
| 1546 |
/* BLOCKs are handled in the expression walker below. */ |
| 1547 |
for (ns = ns->contained; ns; ns = ns->sibling) |
| 1548 |
{ |
| 1549 |
if (ns->code == NULL || ns->code->op != EXEC_BLOCK) |
| 1550 |
optimize_reduction (ns); |
| 1551 |
} |
| 1552 |
} |
| 1553 |
|
| 1554 |
/* Replace code like |
| 1555 |
a = matmul(b,c) + d |
| 1556 |
with |
| 1557 |
a = matmul(b,c) ; a = a + d |
| 1558 |
where the array function is not elemental and not allocatable |
| 1559 |
and does not depend on the left-hand side. |
| 1560 |
*/ |
| 1561 |
|
| 1562 |
static bool |
| 1563 |
optimize_binop_array_assignment (gfc_code *c, gfc_expr **rhs, bool seen_op) |
| 1564 |
{ |
| 1565 |
gfc_expr *e; |
| 1566 |
|
| 1567 |
if (!*rhs) |
| 1568 |
return false; |
| 1569 |
|
| 1570 |
e = *rhs; |
| 1571 |
if (e->expr_type == EXPR_OP) |
| 1572 |
{ |
| 1573 |
switch (e->value.op.op) |
| 1574 |
{ |
| 1575 |
/* Unary operators and exponentiation: Only look at a single |
| 1576 |
operand. */ |
| 1577 |
case INTRINSIC_NOT: |
| 1578 |
case INTRINSIC_UPLUS: |
| 1579 |
case INTRINSIC_UMINUS: |
| 1580 |
case INTRINSIC_PARENTHESES: |
| 1581 |
case INTRINSIC_POWER: |
| 1582 |
if (optimize_binop_array_assignment (c, &e->value.op.op1, seen_op)) |
| 1583 |
return true; |
| 1584 |
break; |
| 1585 |
|
| 1586 |
case INTRINSIC_CONCAT: |
| 1587 |
/* Do not do string concatenations. */ |
| 1588 |
break; |
| 1589 |
|
| 1590 |
default: |
| 1591 |
/* Binary operators. */ |
| 1592 |
if (optimize_binop_array_assignment (c, &e->value.op.op1, true)) |
| 1593 |
return true; |
| 1594 |
|
| 1595 |
if (optimize_binop_array_assignment (c, &e->value.op.op2, true)) |
| 1596 |
return true; |
| 1597 |
|
| 1598 |
break; |
| 1599 |
} |
| 1600 |
} |
| 1601 |
else if (seen_op && e->expr_type == EXPR_FUNCTION && e->rank > 0 |
| 1602 |
&& ! (e->value.function.esym |
| 1603 |
&& (e->value.function.esym->attr.elemental |
| 1604 |
|| e->value.function.esym->attr.allocatable |
| 1605 |
|| e->value.function.esym->ts.type != c->expr1->ts.type |
| 1606 |
|| e->value.function.esym->ts.kind != c->expr1->ts.kind)) |
| 1607 |
&& ! (e->value.function.isym |
| 1608 |
&& (e->value.function.isym->elemental |
| 1609 |
|| e->ts.type != c->expr1->ts.type |
| 1610 |
|| e->ts.kind != c->expr1->ts.kind)) |
| 1611 |
&& ! gfc_inline_intrinsic_function_p (e)) |
| 1612 |
{ |
| 1613 |
|
| 1614 |
gfc_code *n; |
| 1615 |
gfc_expr *new_expr; |
| 1616 |
|
| 1617 |
/* Insert a new assignment statement after the current one. */ |
| 1618 |
n = XCNEW (gfc_code); |
| 1619 |
n->op = EXEC_ASSIGN; |
| 1620 |
n->loc = c->loc; |
| 1621 |
n->next = c->next; |
| 1622 |
c->next = n; |
| 1623 |
|
| 1624 |
n->expr1 = gfc_copy_expr (c->expr1); |
| 1625 |
n->expr2 = c->expr2; |
| 1626 |
new_expr = gfc_copy_expr (c->expr1); |
| 1627 |
c->expr2 = e; |
| 1628 |
*rhs = new_expr; |
| 1629 |
|
| 1630 |
return true; |
| 1631 |
|
| 1632 |
} |
| 1633 |
|
| 1634 |
/* Nothing to optimize. */ |
| 1635 |
return false; |
| 1636 |
} |
| 1637 |
|
| 1638 |
/* Remove unneeded TRIMs at the end of expressions. */ |
| 1639 |
|
| 1640 |
static bool |
| 1641 |
remove_trim (gfc_expr *rhs) |
| 1642 |
{ |
| 1643 |
bool ret; |
| 1644 |
|
| 1645 |
ret = false; |
| 1646 |
if (!rhs) |
| 1647 |
return ret; |
| 1648 |
|
| 1649 |
/* Check for a // b // trim(c). Looping is probably not |
| 1650 |
necessary because the parser usually generates |
| 1651 |
(// (// a b ) trim(c) ) , but better safe than sorry. */ |
| 1652 |
|
| 1653 |
while (rhs->expr_type == EXPR_OP |
| 1654 |
&& rhs->value.op.op == INTRINSIC_CONCAT) |
| 1655 |
rhs = rhs->value.op.op2; |
| 1656 |
|
| 1657 |
while (rhs->expr_type == EXPR_FUNCTION && rhs->value.function.isym |
| 1658 |
&& rhs->value.function.isym->id == GFC_ISYM_TRIM) |
| 1659 |
{ |
| 1660 |
strip_function_call (rhs); |
| 1661 |
/* Recursive call to catch silly stuff like trim ( a // trim(b)). */ |
| 1662 |
remove_trim (rhs); |
| 1663 |
ret = true; |
| 1664 |
} |
| 1665 |
|
| 1666 |
return ret; |
| 1667 |
} |
| 1668 |
|
| 1669 |
/* Optimizations for an assignment. */ |
| 1670 |
|
| 1671 |
static void |
| 1672 |
optimize_assignment (gfc_code * c) |
| 1673 |
{ |
| 1674 |
gfc_expr *lhs, *rhs; |
| 1675 |
|
| 1676 |
lhs = c->expr1; |
| 1677 |
rhs = c->expr2; |
| 1678 |
|
| 1679 |
if (lhs->ts.type == BT_CHARACTER && !lhs->ts.deferred) |
| 1680 |
{ |
| 1681 |
/* Optimize a = trim(b) to a = b. */ |
| 1682 |
remove_trim (rhs); |
| 1683 |
|
| 1684 |
/* Replace a = ' ' by a = '' to optimize away a memcpy. */ |
| 1685 |
if (is_empty_string (rhs)) |
| 1686 |
rhs->value.character.length = 0; |
| 1687 |
} |
| 1688 |
|
| 1689 |
if (lhs->rank > 0 && gfc_check_dependency (lhs, rhs, true) == 0) |
| 1690 |
optimize_binop_array_assignment (c, &rhs, false); |
| 1691 |
} |
| 1692 |
|
| 1693 |
|
| 1694 |
/* Remove an unneeded function call, modifying the expression. |
| 1695 |
This replaces the function call with the value of its |
| 1696 |
first argument. The rest of the argument list is freed. */ |
| 1697 |
|
| 1698 |
static void |
| 1699 |
strip_function_call (gfc_expr *e) |
| 1700 |
{ |
| 1701 |
gfc_expr *e1; |
| 1702 |
gfc_actual_arglist *a; |
| 1703 |
|
| 1704 |
a = e->value.function.actual; |
| 1705 |
|
| 1706 |
/* We should have at least one argument. */ |
| 1707 |
gcc_assert (a->expr != NULL); |
| 1708 |
|
| 1709 |
e1 = a->expr; |
| 1710 |
|
| 1711 |
/* Free the remaining arglist, if any. */ |
| 1712 |
if (a->next) |
| 1713 |
gfc_free_actual_arglist (a->next); |
| 1714 |
|
| 1715 |
/* Graft the argument expression onto the original function. */ |
| 1716 |
*e = *e1; |
| 1717 |
free (e1); |
| 1718 |
|
| 1719 |
} |
| 1720 |
|
| 1721 |
/* Optimization of lexical comparison functions. */ |
| 1722 |
|
| 1723 |
static bool |
| 1724 |
optimize_lexical_comparison (gfc_expr *e) |
| 1725 |
{ |
| 1726 |
if (e->expr_type != EXPR_FUNCTION || e->value.function.isym == NULL) |
| 1727 |
return false; |
| 1728 |
|
| 1729 |
switch (e->value.function.isym->id) |
| 1730 |
{ |
| 1731 |
case GFC_ISYM_LLE: |
| 1732 |
return optimize_comparison (e, INTRINSIC_LE); |
| 1733 |
|
| 1734 |
case GFC_ISYM_LGE: |
| 1735 |
return optimize_comparison (e, INTRINSIC_GE); |
| 1736 |
|
| 1737 |
case GFC_ISYM_LGT: |
| 1738 |
return optimize_comparison (e, INTRINSIC_GT); |
| 1739 |
|
| 1740 |
case GFC_ISYM_LLT: |
| 1741 |
return optimize_comparison (e, INTRINSIC_LT); |
| 1742 |
|
| 1743 |
default: |
| 1744 |
break; |
| 1745 |
} |
| 1746 |
return false; |
| 1747 |
} |
| 1748 |
|
| 1749 |
/* Combine stuff like [a]>b into [a>b], for easier optimization later. Do not |
| 1750 |
do CHARACTER because of possible pessimization involving character |
| 1751 |
lengths. */ |
| 1752 |
|
| 1753 |
static bool |
| 1754 |
combine_array_constructor (gfc_expr *e) |
| 1755 |
{ |
| 1756 |
|
| 1757 |
gfc_expr *op1, *op2; |
| 1758 |
gfc_expr *scalar; |
| 1759 |
gfc_expr *new_expr; |
| 1760 |
gfc_constructor *c, *new_c; |
| 1761 |
gfc_constructor_base oldbase, newbase; |
| 1762 |
bool scalar_first; |
| 1763 |
int n_elem; |
| 1764 |
bool all_const; |
| 1765 |
|
| 1766 |
/* Array constructors have rank one. */ |
| 1767 |
if (e->rank != 1) |
| 1768 |
return false; |
| 1769 |
|
| 1770 |
/* Don't try to combine association lists, this makes no sense |
| 1771 |
and leads to an ICE. */ |
| 1772 |
if (in_assoc_list) |
| 1773 |
return false; |
| 1774 |
|
| 1775 |
/* With FORALL, the BLOCKS created by create_var will cause an ICE. */ |
| 1776 |
if (forall_level > 0) |
| 1777 |
return false; |
| 1778 |
|
| 1779 |
/* Inside an iterator, things can get hairy; we are likely to create |
| 1780 |
an invalid temporary variable. */ |
| 1781 |
if (iterator_level > 0) |
| 1782 |
return false; |
| 1783 |
|
| 1784 |
/* WHERE also doesn't work. */ |
| 1785 |
if (in_where > 0) |
| 1786 |
return false; |
| 1787 |
|
| 1788 |
op1 = e->value.op.op1; |
| 1789 |
op2 = e->value.op.op2; |
| 1790 |
|
| 1791 |
if (!op1 || !op2) |
| 1792 |
return false; |
| 1793 |
|
| 1794 |
if (op1->expr_type == EXPR_ARRAY && op2->rank == 0) |
| 1795 |
scalar_first = false; |
| 1796 |
else if (op2->expr_type == EXPR_ARRAY && op1->rank == 0) |
| 1797 |
{ |
| 1798 |
scalar_first = true; |
| 1799 |
op1 = e->value.op.op2; |
| 1800 |
op2 = e->value.op.op1; |
| 1801 |
} |
| 1802 |
else |
| 1803 |
return false; |
| 1804 |
|
| 1805 |
if (op2->ts.type == BT_CHARACTER) |
| 1806 |
return false; |
| 1807 |
|
| 1808 |
/* This might be an expanded constructor with very many constant values. If |
| 1809 |
we perform the operation here, we might end up with a long compile time |
| 1810 |
and actually longer execution time, so a length bound is in order here. |
| 1811 |
If the constructor constains something which is not a constant, it did |
| 1812 |
not come from an expansion, so leave it alone. */ |
| 1813 |
|
| 1814 |
#define CONSTR_LEN_MAX 4 |
| 1815 |
|
| 1816 |
oldbase = op1->value.constructor; |
| 1817 |
|
| 1818 |
n_elem = 0; |
| 1819 |
all_const = true; |
| 1820 |
for (c = gfc_constructor_first (oldbase); c; c = gfc_constructor_next(c)) |
| 1821 |
{ |
| 1822 |
if (c->expr->expr_type != EXPR_CONSTANT) |
| 1823 |
{ |
| 1824 |
all_const = false; |
| 1825 |
break; |
| 1826 |
} |
| 1827 |
n_elem += 1; |
| 1828 |
} |
| 1829 |
|
| 1830 |
if (all_const && n_elem > CONSTR_LEN_MAX) |
| 1831 |
return false; |
| 1832 |
|
| 1833 |
#undef CONSTR_LEN_MAX |
| 1834 |
|
| 1835 |
newbase = NULL; |
| 1836 |
e->expr_type = EXPR_ARRAY; |
| 1837 |
|
| 1838 |
scalar = create_var (gfc_copy_expr (op2), "constr"); |
| 1839 |
|
| 1840 |
for (c = gfc_constructor_first (oldbase); c; |
| 1841 |
c = gfc_constructor_next (c)) |
| 1842 |
{ |
| 1843 |
new_expr = gfc_get_expr (); |
| 1844 |
new_expr->ts = e->ts; |
| 1845 |
new_expr->expr_type = EXPR_OP; |
| 1846 |
new_expr->rank = c->expr->rank; |
| 1847 |
new_expr->corank = c->expr->corank; |
| 1848 |
new_expr->where = c->expr->where; |
| 1849 |
new_expr->value.op.op = e->value.op.op; |
| 1850 |
|
| 1851 |
if (scalar_first) |
| 1852 |
{ |
| 1853 |
new_expr->value.op.op1 = gfc_copy_expr (scalar); |
| 1854 |
new_expr->value.op.op2 = gfc_copy_expr (c->expr); |
| 1855 |
} |
| 1856 |
else |
| 1857 |
{ |
| 1858 |
new_expr->value.op.op1 = gfc_copy_expr (c->expr); |
| 1859 |
new_expr->value.op.op2 = gfc_copy_expr (scalar); |
| 1860 |
} |
| 1861 |
|
| 1862 |
new_c = gfc_constructor_append_expr (&newbase, new_expr, &(e->where)); |
| 1863 |
new_c->iterator = c->iterator; |
| 1864 |
c->iterator = NULL; |
| 1865 |
} |
| 1866 |
|
| 1867 |
gfc_free_expr (op1); |
| 1868 |
gfc_free_expr (op2); |
| 1869 |
gfc_free_expr (scalar); |
| 1870 |
|
| 1871 |
e->value.constructor = newbase; |
| 1872 |
return true; |
| 1873 |
} |
| 1874 |
|
| 1875 |
/* Recursive optimization of operators. */ |
| 1876 |
|
| 1877 |
static bool |
| 1878 |
optimize_op (gfc_expr *e) |
| 1879 |
{ |
| 1880 |
bool changed; |
| 1881 |
|
| 1882 |
gfc_intrinsic_op op = e->value.op.op; |
| 1883 |
|
| 1884 |
changed = false; |
| 1885 |
|
| 1886 |
/* Only use new-style comparisons. */ |
| 1887 |
switch(op) |
| 1888 |
{ |
| 1889 |
case INTRINSIC_EQ_OS: |
| 1890 |
op = INTRINSIC_EQ; |
| 1891 |
break; |
| 1892 |
|
| 1893 |
case INTRINSIC_GE_OS: |
| 1894 |
op = INTRINSIC_GE; |
| 1895 |
break; |
| 1896 |
|
| 1897 |
case INTRINSIC_LE_OS: |
| 1898 |
op = INTRINSIC_LE; |
| 1899 |
break; |
| 1900 |
|
| 1901 |
case INTRINSIC_NE_OS: |
| 1902 |
op = INTRINSIC_NE; |
| 1903 |
break; |
| 1904 |
|
| 1905 |
case INTRINSIC_GT_OS: |
| 1906 |
op = INTRINSIC_GT; |
| 1907 |
break; |
| 1908 |
|
| 1909 |
case INTRINSIC_LT_OS: |
| 1910 |
op = INTRINSIC_LT; |
| 1911 |
break; |
| 1912 |
|
| 1913 |
default: |
| 1914 |
break; |
| 1915 |
} |
| 1916 |
|
| 1917 |
switch (op) |
| 1918 |
{ |
| 1919 |
case INTRINSIC_EQ: |
| 1920 |
case INTRINSIC_GE: |
| 1921 |
case INTRINSIC_LE: |
| 1922 |
case INTRINSIC_NE: |
| 1923 |
case INTRINSIC_GT: |
| 1924 |
case INTRINSIC_LT: |
| 1925 |
changed = optimize_comparison (e, op); |
| 1926 |
|
| 1927 |
gcc_fallthrough (); |
| 1928 |
/* Look at array constructors. */ |
| 1929 |
case INTRINSIC_PLUS: |
| 1930 |
case INTRINSIC_MINUS: |
| 1931 |
case INTRINSIC_TIMES: |
| 1932 |
case INTRINSIC_DIVIDE: |
| 1933 |
return combine_array_constructor (e) || changed; |
| 1934 |
|
| 1935 |
default: |
| 1936 |
break; |
| 1937 |
} |
| 1938 |
|
| 1939 |
return false; |
| 1940 |
} |
| 1941 |
|
| 1942 |
|
| 1943 |
/* Return true if a constant string contains only blanks. */ |
| 1944 |
|
| 1945 |
static bool |
| 1946 |
is_empty_string (gfc_expr *e) |
| 1947 |
{ |
| 1948 |
int i; |
| 1949 |
|
| 1950 |
if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT) |
| 1951 |
return false; |
| 1952 |
|
| 1953 |
for (i=0; i < e->value.character.length; i++) |
| 1954 |
{ |
| 1955 |
if (e->value.character.string[i] != ' ') |
| 1956 |
return false; |
| 1957 |
} |
| 1958 |
|
| 1959 |
return true; |
| 1960 |
} |
| 1961 |
|
| 1962 |
|
| 1963 |
/* Insert a call to the intrinsic len_trim. Use a different name for |
| 1964 |
the symbol tree so we don't run into trouble when the user has |
| 1965 |
renamed len_trim for some reason. */ |
| 1966 |
|
| 1967 |
static gfc_expr* |
| 1968 |
get_len_trim_call (gfc_expr *str, int kind) |
| 1969 |
{ |
| 1970 |
gfc_expr *fcn; |
| 1971 |
gfc_actual_arglist *actual_arglist, *next; |
| 1972 |
|
| 1973 |
fcn = gfc_get_expr (); |
| 1974 |
fcn->expr_type = EXPR_FUNCTION; |
| 1975 |
fcn->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LEN_TRIM); |
| 1976 |
actual_arglist = gfc_get_actual_arglist (); |
| 1977 |
actual_arglist->expr = str; |
| 1978 |
next = gfc_get_actual_arglist (); |
| 1979 |
next->expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, kind); |
| 1980 |
actual_arglist->next = next; |
| 1981 |
|
| 1982 |
fcn->value.function.actual = actual_arglist; |
| 1983 |
fcn->where = str->where; |
| 1984 |
fcn->ts.type = BT_INTEGER; |
| 1985 |
fcn->ts.kind = gfc_charlen_int_kind; |
| 1986 |
|
| 1987 |
gfc_get_sym_tree ("__internal_len_trim", current_ns, &fcn->symtree, false); |
| 1988 |
fcn->symtree->n.sym->ts = fcn->ts; |
| 1989 |
fcn->symtree->n.sym->attr.flavor = FL_PROCEDURE; |
| 1990 |
fcn->symtree->n.sym->attr.function = 1; |
| 1991 |
fcn->symtree->n.sym->attr.elemental = 1; |
| 1992 |
fcn->symtree->n.sym->attr.referenced = 1; |
| 1993 |
fcn->symtree->n.sym->attr.access = ACCESS_PRIVATE; |
| 1994 |
gfc_commit_symbol (fcn->symtree->n.sym); |
| 1995 |
|
| 1996 |
return fcn; |
| 1997 |
} |
| 1998 |
|
| 1999 |
|
| 2000 |
/* Optimize expressions for equality. */ |
| 2001 |
|
| 2002 |
static bool |
| 2003 |
optimize_comparison (gfc_expr *e, gfc_intrinsic_op op) |
| 2004 |
{ |
| 2005 |
gfc_expr *op1, *op2; |
| 2006 |
bool change; |
| 2007 |
int eq; |
| 2008 |
bool result; |
| 2009 |
gfc_actual_arglist *firstarg, *secondarg; |
| 2010 |
|
| 2011 |
if (e->expr_type == EXPR_OP) |
| 2012 |
{ |
| 2013 |
firstarg = NULL; |
| 2014 |
secondarg = NULL; |
| 2015 |
op1 = e->value.op.op1; |
| 2016 |
op2 = e->value.op.op2; |
| 2017 |
} |
| 2018 |
else if (e->expr_type == EXPR_FUNCTION) |
| 2019 |
{ |
| 2020 |
/* One of the lexical comparison functions. */ |
| 2021 |
firstarg = e->value.function.actual; |
| 2022 |
secondarg = firstarg->next; |
| 2023 |
op1 = firstarg->expr; |
| 2024 |
op2 = secondarg->expr; |
| 2025 |
} |
| 2026 |
else |
| 2027 |
gcc_unreachable (); |
| 2028 |
|
| 2029 |
/* Strip off unneeded TRIM calls from string comparisons. */ |
| 2030 |
|
| 2031 |
change = remove_trim (op1); |
| 2032 |
|
| 2033 |
if (remove_trim (op2)) |
| 2034 |
change = true; |
| 2035 |
|
| 2036 |
/* An expression of type EXPR_CONSTANT is only valid for scalars. */ |
| 2037 |
/* TODO: A scalar constant may be acceptable in some cases (the scalarizer |
| 2038 |
handles them well). However, there are also cases that need a non-scalar |
| 2039 |
argument. For example the any intrinsic. See PR 45380. */ |
| 2040 |
if (e->rank > 0) |
| 2041 |
return change; |
| 2042 |
|
| 2043 |
/* Replace a == '' with len_trim(a) == 0 and a /= '' with |
| 2044 |
len_trim(a) != 0 */ |
| 2045 |
if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER |
| 2046 |
&& (op == INTRINSIC_EQ || op == INTRINSIC_NE)) |
| 2047 |
{ |
| 2048 |
bool empty_op1, empty_op2; |
| 2049 |
empty_op1 = is_empty_string (op1); |
| 2050 |
empty_op2 = is_empty_string (op2); |
| 2051 |
|
| 2052 |
if (empty_op1 || empty_op2) |
| 2053 |
{ |
| 2054 |
gfc_expr *fcn; |
| 2055 |
gfc_expr *zero; |
| 2056 |
gfc_expr *str; |
| 2057 |
|
| 2058 |
/* This can only happen when an error for comparing |
| 2059 |
characters of different kinds has already been issued. */ |
| 2060 |
if (empty_op1 && empty_op2) |
| 2061 |
return false; |
| 2062 |
|
| 2063 |
zero = gfc_get_int_expr (gfc_charlen_int_kind, &e->where, 0); |
| 2064 |
str = empty_op1 ? op2 : op1; |
| 2065 |
|
| 2066 |
fcn = get_len_trim_call (str, gfc_charlen_int_kind); |
| 2067 |
|
| 2068 |
|
| 2069 |
if (empty_op1) |
| 2070 |
gfc_free_expr (op1); |
| 2071 |
else |
| 2072 |
gfc_free_expr (op2); |
| 2073 |
|
| 2074 |
op1 = fcn; |
| 2075 |
op2 = zero; |
| 2076 |
e->value.op.op1 = fcn; |
| 2077 |
e->value.op.op2 = zero; |
| 2078 |
} |
| 2079 |
} |
| 2080 |
|
| 2081 |
|
| 2082 |
/* Don't compare REAL or COMPLEX expressions when honoring NaNs. */ |
| 2083 |
|
| 2084 |
if (flag_finite_math_only |
| 2085 |
|| (op1->ts.type != BT_REAL && op2->ts.type != BT_REAL |
| 2086 |
&& op1->ts.type != BT_COMPLEX && op2->ts.type != BT_COMPLEX)) |
| 2087 |
{ |
| 2088 |
eq = gfc_dep_compare_expr (op1, op2); |
| 2089 |
if (eq <= -2) |
| 2090 |
{ |
| 2091 |
/* Replace A // B < A // C with B < C, and A // B < C // B |
| 2092 |
with A < C. */ |
| 2093 |
if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER |
| 2094 |
&& op1->expr_type == EXPR_OP |
| 2095 |
&& op1->value.op.op == INTRINSIC_CONCAT |
| 2096 |
&& op2->expr_type == EXPR_OP |
| 2097 |
&& op2->value.op.op == INTRINSIC_CONCAT) |
| 2098 |
{ |
| 2099 |
gfc_expr *op1_left = op1->value.op.op1; |
| 2100 |
gfc_expr *op2_left = op2->value.op.op1; |
| 2101 |
gfc_expr *op1_right = op1->value.op.op2; |
| 2102 |
gfc_expr *op2_right = op2->value.op.op2; |
| 2103 |
|
| 2104 |
if (gfc_dep_compare_expr (op1_left, op2_left) == 0) |
| 2105 |
{ |
| 2106 |
/* Watch out for 'A ' // x vs. 'A' // x. */ |
| 2107 |
|
| 2108 |
if (op1_left->expr_type == EXPR_CONSTANT |
| 2109 |
&& op2_left->expr_type == EXPR_CONSTANT |
| 2110 |
&& op1_left->value.character.length |
| 2111 |
!= op2_left->value.character.length) |
| 2112 |
return change; |
| 2113 |
else |
| 2114 |
{ |
| 2115 |
free (op1_left); |
| 2116 |
free (op2_left); |
| 2117 |
if (firstarg) |
| 2118 |
{ |
| 2119 |
firstarg->expr = op1_right; |
| 2120 |
secondarg->expr = op2_right; |
| 2121 |
} |
| 2122 |
else |
| 2123 |
{ |
| 2124 |
e->value.op.op1 = op1_right; |
| 2125 |
e->value.op.op2 = op2_right; |
| 2126 |
} |
| 2127 |
optimize_comparison (e, op); |
| 2128 |
return true; |
| 2129 |
} |
| 2130 |
} |
| 2131 |
if (gfc_dep_compare_expr (op1_right, op2_right) == 0) |
| 2132 |
{ |
| 2133 |
free (op1_right); |
| 2134 |
free (op2_right); |
| 2135 |
if (firstarg) |
| 2136 |
{ |
| 2137 |
firstarg->expr = op1_left; |
| 2138 |
secondarg->expr = op2_left; |
| 2139 |
} |
| 2140 |
else |
| 2141 |
{ |
| 2142 |
e->value.op.op1 = op1_left; |
| 2143 |
e->value.op.op2 = op2_left; |
| 2144 |
} |
| 2145 |
|
| 2146 |
optimize_comparison (e, op); |
| 2147 |
return true; |
| 2148 |
} |
| 2149 |
} |
| 2150 |
} |
| 2151 |
else |
| 2152 |
{ |
| 2153 |
/* eq can only be -1, 0 or 1 at this point. */ |
| 2154 |
switch (op) |
| 2155 |
{ |
| 2156 |
case INTRINSIC_EQ: |
| 2157 |
result = eq == 0; |
| 2158 |
break; |
| 2159 |
|
| 2160 |
case INTRINSIC_GE: |
| 2161 |
result = eq >= 0; |
| 2162 |
break; |
| 2163 |
|
| 2164 |
case INTRINSIC_LE: |
| 2165 |
result = eq <= 0; |
| 2166 |
break; |
| 2167 |
|
| 2168 |
case INTRINSIC_NE: |
| 2169 |
result = eq != 0; |
| 2170 |
break; |
| 2171 |
|
| 2172 |
case INTRINSIC_GT: |
| 2173 |
result = eq > 0; |
| 2174 |
break; |
| 2175 |
|
| 2176 |
case INTRINSIC_LT: |
| 2177 |
result = eq < 0; |
| 2178 |
break; |
| 2179 |
|
| 2180 |
default: |
| 2181 |
gfc_internal_error ("illegal OP in optimize_comparison"); |
| 2182 |
break; |
| 2183 |
} |
| 2184 |
|
| 2185 |
/* Replace the expression by a constant expression. The typespec |
| 2186 |
and where remains the way it is. */ |
| 2187 |
free (op1); |
| 2188 |
free (op2); |
| 2189 |
e->expr_type = EXPR_CONSTANT; |
| 2190 |
e->value.logical = result; |
| 2191 |
return true; |
| 2192 |
} |
| 2193 |
} |
| 2194 |
|
| 2195 |
return change; |
| 2196 |
} |
| 2197 |
|
| 2198 |
/* Optimize a trim function by replacing it with an equivalent substring |
| 2199 |
involving a call to len_trim. This only works for expressions where |
| 2200 |
variables are trimmed. Return true if anything was modified. */ |
| 2201 |
|
| 2202 |
static bool |
| 2203 |
optimize_trim (gfc_expr *e) |
| 2204 |
{ |
| 2205 |
gfc_expr *a; |
| 2206 |
gfc_ref *ref; |
| 2207 |
gfc_expr *fcn; |
| 2208 |
gfc_ref **rr = NULL; |
| 2209 |
|
| 2210 |
/* Don't do this optimization within an argument list, because |
| 2211 |
otherwise aliasing issues may occur. */ |
| 2212 |
|
| 2213 |
if (count_arglist != 1) |
| 2214 |
return false; |
| 2215 |
|
| 2216 |
if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_FUNCTION |
| 2217 |
|| e->value.function.isym == NULL |
| 2218 |
|| e->value.function.isym->id != GFC_ISYM_TRIM) |
| 2219 |
return false; |
| 2220 |
|
| 2221 |
a = e->value.function.actual->expr; |
| 2222 |
|
| 2223 |
if (a->expr_type != EXPR_VARIABLE) |
| 2224 |
return false; |
| 2225 |
|
| 2226 |
/* This would pessimize the idiom a = trim(a) for reallocatable strings. */ |
| 2227 |
|
| 2228 |
if (a->symtree->n.sym->attr.allocatable) |
| 2229 |
return false; |
| 2230 |
|
| 2231 |
/* Follow all references to find the correct place to put the newly |
| 2232 |
created reference. FIXME: Also handle substring references and |
| 2233 |
array references. Array references cause strange regressions at |
| 2234 |
the moment. */ |
| 2235 |
|
| 2236 |
if (a->ref) |
| 2237 |
{ |
| 2238 |
for (rr = &(a->ref); *rr; rr = &((*rr)->next)) |
| 2239 |
{ |
| 2240 |
if ((*rr)->type == REF_SUBSTRING || (*rr)->type == REF_ARRAY) |
| 2241 |
return false; |
| 2242 |
} |
| 2243 |
} |
| 2244 |
|
| 2245 |
strip_function_call (e); |
| 2246 |
|
| 2247 |
if (e->ref == NULL) |
| 2248 |
rr = &(e->ref); |
| 2249 |
|
| 2250 |
/* Create the reference. */ |
| 2251 |
|
| 2252 |
ref = gfc_get_ref (); |
| 2253 |
ref->type = REF_SUBSTRING; |
| 2254 |
|
| 2255 |
/* Set the start of the reference. */ |
| 2256 |
|
| 2257 |
ref->u.ss.start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1); |
| 2258 |
|
| 2259 |
/* Build the function call to len_trim(x, gfc_default_integer_kind). */ |
| 2260 |
|
| 2261 |
fcn = get_len_trim_call (gfc_copy_expr (e), gfc_charlen_int_kind); |
| 2262 |
|
| 2263 |
/* Set the end of the reference to the call to len_trim. */ |
| 2264 |
|
| 2265 |
ref->u.ss.end = fcn; |
| 2266 |
gcc_assert (rr != NULL && *rr == NULL); |
| 2267 |
*rr = ref; |
| 2268 |
return true; |
| 2269 |
} |
| 2270 |
|
| 2271 |
/* Data package to hand down for DO loop checks in a contained |
| 2272 |
procedure. */ |
| 2273 |
typedef struct contained_info |
| 2274 |
{ |
| 2275 |
gfc_symbol *do_var; |
| 2276 |
gfc_symbol *procedure; |
| 2277 |
locus where_do; |
| 2278 |
} contained_info; |
| 2279 |
|
| 2280 |
static enum gfc_exec_op last_io_op; |
| 2281 |
|
| 2282 |
/* Callback function to check for INTENT(OUT) and INTENT(INOUT) in a |
| 2283 |
contained function call. */ |
| 2284 |
|
| 2285 |
static int |
| 2286 |
doloop_contained_function_call (gfc_expr **e, |
| 2287 |
int *walk_subtrees ATTRIBUTE_UNUSED, void *data) |
| 2288 |
{ |
| 2289 |
gfc_expr *expr = *e; |
| 2290 |
gfc_formal_arglist *f; |
| 2291 |
gfc_actual_arglist *a; |
| 2292 |
gfc_symbol *sym, *do_var; |
| 2293 |
contained_info *info; |
| 2294 |
|
| 2295 |
if (expr->expr_type != EXPR_FUNCTION || expr->value.function.isym |
| 2296 |
|| expr->value.function.esym == NULL) |
| 2297 |
return 0; |
| 2298 |
|
| 2299 |
sym = expr->value.function.esym; |
| 2300 |
f = gfc_sym_get_dummy_args (sym); |
| 2301 |
if (f == NULL) |
| 2302 |
return 0; |
| 2303 |
|
| 2304 |
info = (contained_info *) data; |
| 2305 |
do_var = info->do_var; |
| 2306 |
a = expr->value.function.actual; |
| 2307 |
|
| 2308 |
while (a && f) |
| 2309 |
{ |
| 2310 |
if (a->expr && a->expr->symtree && a->expr->symtree->n.sym == do_var) |
| 2311 |
{ |
| 2312 |
if (f->sym->attr.intent == INTENT_OUT) |
| 2313 |
{ |
| 2314 |
gfc_error_now ("Index variable %qs set to undefined as " |
| 2315 |
"INTENT(OUT) argument at %L in procedure %qs " |
| 2316 |
"called from within DO loop at %L", do_var->name, |
| 2317 |
&a->expr->where, info->procedure->name, |
| 2318 |
&info->where_do); |
| 2319 |
return 1; |
| 2320 |
} |
| 2321 |
else if (f->sym->attr.intent == INTENT_INOUT) |
| 2322 |
{ |
| 2323 |
gfc_error_now ("Index variable %qs not definable as " |
| 2324 |
"INTENT(INOUT) argument at %L in procedure %qs " |
| 2325 |
"called from within DO loop at %L", do_var->name, |
| 2326 |
&a->expr->where, info->procedure->name, |
| 2327 |
&info->where_do); |
| 2328 |
return 1; |
| 2329 |
} |
| 2330 |
} |
| 2331 |
a = a->next; |
| 2332 |
f = f->next; |
| 2333 |
} |
| 2334 |
return 0; |
| 2335 |
} |
| 2336 |
|
| 2337 |
/* Callback function that goes through the code in a contained |
| 2338 |
procedure to make sure it does not change a variable in a DO |
| 2339 |
loop. */ |
| 2340 |
|
| 2341 |
static int |
| 2342 |
doloop_contained_procedure_code (gfc_code **c, |
| 2343 |
int *walk_subtrees ATTRIBUTE_UNUSED, |
| 2344 |
void *data) |
| 2345 |
{ |
| 2346 |
gfc_code *co = *c; |
| 2347 |
contained_info *info = (contained_info *) data; |
| 2348 |
gfc_symbol *do_var = info->do_var; |
| 2349 |
const char *errmsg = _("Index variable %qs redefined at %L in procedure %qs " |
| 2350 |
"called from within DO loop at %L"); |
| 2351 |
static enum gfc_exec_op saved_io_op; |
| 2352 |
|
| 2353 |
switch (co->op) |
| 2354 |
{ |
| 2355 |
case EXEC_ASSIGN: |
| 2356 |
if (co->expr1->symtree && co->expr1->symtree->n.sym == do_var) |
| 2357 |
gfc_error_now (errmsg, do_var->name, &co->loc, info->procedure->name, |
| 2358 |
&info->where_do); |
| 2359 |
break; |
| 2360 |
|
| 2361 |
case EXEC_DO: |
| 2362 |
if (co->ext.iterator && co->ext.iterator->var |
| 2363 |
&& co->ext.iterator->var->symtree->n.sym == do_var) |
| 2364 |
gfc_error (errmsg, do_var->name, &co->loc, info->procedure->name, |
| 2365 |
&info->where_do); |
| 2366 |
break; |
| 2367 |
|
| 2368 |
case EXEC_READ: |
| 2369 |
case EXEC_WRITE: |
| 2370 |
case EXEC_INQUIRE: |
| 2371 |
case EXEC_IOLENGTH: |
| 2372 |
saved_io_op = last_io_op; |
| 2373 |
last_io_op = co->op; |
| 2374 |
break; |
| 2375 |
|
| 2376 |
case EXEC_OPEN: |
| 2377 |
if (co->ext.open && co->ext.open->iostat |
| 2378 |
&& co->ext.open->iostat->symtree->n.sym == do_var) |
| 2379 |
gfc_error_now (errmsg, do_var->name, &co->ext.open->iostat->where, |
| 2380 |
info->procedure->name, &info->where_do); |
| 2381 |
break; |
| 2382 |
|
| 2383 |
case EXEC_CLOSE: |
| 2384 |
if (co->ext.close && co->ext.close->iostat |
| 2385 |
&& co->ext.close->iostat->symtree->n.sym == do_var) |
| 2386 |
gfc_error_now (errmsg, do_var->name, &co->ext.close->iostat->where, |
| 2387 |
info->procedure->name, &info->where_do); |
| 2388 |
break; |
| 2389 |
|
| 2390 |
case EXEC_TRANSFER: |
| 2391 |
switch (last_io_op) |
| 2392 |
{ |
| 2393 |
|
| 2394 |
case EXEC_INQUIRE: |
| 2395 |
#define CHECK_INQ(a) do { if (co->ext.inquire && \ |
| 2396 |
co->ext.inquire->a && \ |
| 2397 |
co->ext.inquire->a->symtree->n.sym == do_var) \ |
| 2398 |
gfc_error_now (errmsg, do_var->name, \ |
| 2399 |
&co->ext.inquire->a->where, \ |
| 2400 |
info->procedure->name, \ |
| 2401 |
&info->where_do); \ |
| 2402 |
} while (0) |
| 2403 |
|
| 2404 |
CHECK_INQ(iostat); |
| 2405 |
CHECK_INQ(number); |
| 2406 |
CHECK_INQ(position); |
| 2407 |
CHECK_INQ(recl); |
| 2408 |
CHECK_INQ(position); |
| 2409 |
CHECK_INQ(iolength); |
| 2410 |
CHECK_INQ(strm_pos); |
| 2411 |
break; |
| 2412 |
#undef CHECK_INQ |
| 2413 |
|
| 2414 |
case EXEC_READ: |
| 2415 |
if (co->expr1 && co->expr1->symtree |
| 2416 |
&& co->expr1->symtree->n.sym == do_var) |
| 2417 |
gfc_error_now (errmsg, do_var->name, &co->expr1->where, |
| 2418 |
info->procedure->name, &info->where_do); |
| 2419 |
|
| 2420 |
/* Fallthrough. */ |
| 2421 |
|
| 2422 |
case EXEC_WRITE: |
| 2423 |
if (co->ext.dt && co->ext.dt->iostat && co->ext.dt->iostat->symtree |
| 2424 |
&& co->ext.dt->iostat->symtree->n.sym == do_var) |
| 2425 |
gfc_error_now (errmsg, do_var->name, &co->ext.dt->iostat->where, |
| 2426 |
info->procedure->name, &info->where_do); |
| 2427 |
break; |
| 2428 |
|
| 2429 |
case EXEC_IOLENGTH: |
| 2430 |
if (co->expr1 && co->expr1->symtree |
| 2431 |
&& co->expr1->symtree->n.sym == do_var) |
| 2432 |
gfc_error_now (errmsg, do_var->name, &co->expr1->where, |
| 2433 |
info->procedure->name, &info->where_do); |
| 2434 |
break; |
| 2435 |
|
| 2436 |
default: |
| 2437 |
gcc_unreachable (); |
| 2438 |
} |
| 2439 |
break; |
| 2440 |
|
| 2441 |
case EXEC_DT_END: |
| 2442 |
last_io_op = saved_io_op; |
| 2443 |
break; |
| 2444 |
|
| 2445 |
case EXEC_CALL: |
| 2446 |
gfc_formal_arglist *f; |
| 2447 |
gfc_actual_arglist *a; |
| 2448 |
|
| 2449 |
f = gfc_sym_get_dummy_args (co->resolved_sym); |
| 2450 |
if (f == NULL) |
| 2451 |
break; |
| 2452 |
a = co->ext.actual; |
| 2453 |
/* Slightly different error message here. If there is an error, |
| 2454 |
return 1 to avoid an infinite loop. */ |
| 2455 |
while (a && f) |
| 2456 |
{ |
| 2457 |
if (a->expr && a->expr->symtree && a->expr->symtree->n.sym == do_var) |
| 2458 |
{ |
| 2459 |
if (f->sym->attr.intent == INTENT_OUT) |
| 2460 |
{ |
| 2461 |
gfc_error_now ("Index variable %qs set to undefined as " |
| 2462 |
"INTENT(OUT) argument at %L in subroutine %qs " |
| 2463 |
"called from within DO loop at %L", |
| 2464 |
do_var->name, &a->expr->where, |
| 2465 |
info->procedure->name, &info->where_do); |
| 2466 |
return 1; |
| 2467 |
} |
| 2468 |
else if (f->sym->attr.intent == INTENT_INOUT) |
| 2469 |
{ |
| 2470 |
gfc_error_now ("Index variable %qs not definable as " |
| 2471 |
"INTENT(INOUT) argument at %L in subroutine %qs " |
| 2472 |
"called from within DO loop at %L", do_var->name, |
| 2473 |
&a->expr->where, info->procedure->name, |
| 2474 |
&info->where_do); |
| 2475 |
return 1; |
| 2476 |
} |
| 2477 |
} |
| 2478 |
a = a->next; |
| 2479 |
f = f->next; |
| 2480 |
} |
| 2481 |
break; |
| 2482 |
default: |
| 2483 |
break; |
| 2484 |
} |
| 2485 |
return 0; |
| 2486 |
} |
| 2487 |
|
| 2488 |
/* Callback function for code checking that we do not pass a DO variable to an |
| 2489 |
INTENT(OUT) or INTENT(INOUT) dummy variable. */ |
| 2490 |
|
| 2491 |
static int |
| 2492 |
doloop_code (gfc_code **c, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 2493 |
void *data ATTRIBUTE_UNUSED) |
| 2494 |
{ |
| 2495 |
gfc_code *co; |
| 2496 |
int i; |
| 2497 |
gfc_formal_arglist *f; |
| 2498 |
gfc_actual_arglist *a; |
| 2499 |
gfc_code *cl; |
| 2500 |
do_t loop, *lp; |
| 2501 |
bool seen_goto; |
| 2502 |
|
| 2503 |
co = *c; |
| 2504 |
|
| 2505 |
/* If the doloop_list grew, we have to truncate it here. */ |
| 2506 |
|
| 2507 |
if ((unsigned) doloop_level < doloop_list.length()) |
| 2508 |
doloop_list.truncate (doloop_level); |
| 2509 |
|
| 2510 |
seen_goto = false; |
| 2511 |
switch (co->op) |
| 2512 |
{ |
| 2513 |
case EXEC_DO: |
| 2514 |
|
| 2515 |
if (co->ext.iterator && co->ext.iterator->var) |
| 2516 |
loop.c = co; |
| 2517 |
else |
| 2518 |
loop.c = NULL; |
| 2519 |
|
| 2520 |
loop.branch_level = if_level + select_level; |
| 2521 |
loop.seen_goto = false; |
| 2522 |
doloop_list.safe_push (loop); |
| 2523 |
break; |
| 2524 |
|
| 2525 |
/* If anything could transfer control away from a suspicious |
| 2526 |
subscript, make sure to set seen_goto in the current DO loop |
| 2527 |
(if any). */ |
| 2528 |
case EXEC_GOTO: |
| 2529 |
case EXEC_EXIT: |
| 2530 |
case EXEC_STOP: |
| 2531 |
case EXEC_ERROR_STOP: |
| 2532 |
case EXEC_CYCLE: |
| 2533 |
seen_goto = true; |
| 2534 |
break; |
| 2535 |
|
| 2536 |
case EXEC_OPEN: |
| 2537 |
if (co->ext.open->err) |
| 2538 |
seen_goto = true; |
| 2539 |
break; |
| 2540 |
|
| 2541 |
case EXEC_CLOSE: |
| 2542 |
if (co->ext.close->err) |
| 2543 |
seen_goto = true; |
| 2544 |
break; |
| 2545 |
|
| 2546 |
case EXEC_BACKSPACE: |
| 2547 |
case EXEC_ENDFILE: |
| 2548 |
case EXEC_REWIND: |
| 2549 |
case EXEC_FLUSH: |
| 2550 |
|
| 2551 |
if (co->ext.filepos->err) |
| 2552 |
seen_goto = true; |
| 2553 |
break; |
| 2554 |
|
| 2555 |
case EXEC_INQUIRE: |
| 2556 |
if (co->ext.inquire->err) |
| 2557 |
seen_goto = true; |
| 2558 |
break; |
| 2559 |
|
| 2560 |
case EXEC_READ: |
| 2561 |
case EXEC_WRITE: |
| 2562 |
if (co->ext.dt->err || co->ext.dt->end || co->ext.dt->eor) |
| 2563 |
seen_goto = true; |
| 2564 |
break; |
| 2565 |
|
| 2566 |
case EXEC_WAIT: |
| 2567 |
if (co->ext.wait->err || co->ext.wait->end || co->ext.wait->eor) |
| 2568 |
loop.seen_goto = true; |
| 2569 |
break; |
| 2570 |
|
| 2571 |
case EXEC_CALL: |
| 2572 |
if (co->resolved_sym == NULL) |
| 2573 |
break; |
| 2574 |
|
| 2575 |
/* Test if somebody stealthily changes the DO variable from |
| 2576 |
under us by changing it in a host-associated procedure. */ |
| 2577 |
if (co->resolved_sym->attr.contained) |
| 2578 |
{ |
| 2579 |
FOR_EACH_VEC_ELT (doloop_list, i, lp) |
| 2580 |
{ |
| 2581 |
gfc_symbol *sym = co->resolved_sym; |
| 2582 |
contained_info info; |
| 2583 |
gfc_namespace *ns; |
| 2584 |
|
| 2585 |
cl = lp->c; |
| 2586 |
info.do_var = cl->ext.iterator->var->symtree->n.sym; |
| 2587 |
info.procedure = co->resolved_sym; /* sym? */ |
| 2588 |
info.where_do = co->loc; |
| 2589 |
/* Look contained procedures under the namespace of the |
| 2590 |
variable. */ |
| 2591 |
for (ns = info.do_var->ns->contained; ns; ns = ns->sibling) |
| 2592 |
if (ns->proc_name && ns->proc_name == sym) |
| 2593 |
gfc_code_walker (&ns->code, doloop_contained_procedure_code, |
| 2594 |
doloop_contained_function_call, &info); |
| 2595 |
} |
| 2596 |
} |
| 2597 |
|
| 2598 |
f = gfc_sym_get_dummy_args (co->resolved_sym); |
| 2599 |
|
| 2600 |
/* Withot a formal arglist, there is only unknown INTENT, |
| 2601 |
which we don't check for. */ |
| 2602 |
if (f == NULL) |
| 2603 |
break; |
| 2604 |
|
| 2605 |
a = co->ext.actual; |
| 2606 |
|
| 2607 |
while (a && f) |
| 2608 |
{ |
| 2609 |
FOR_EACH_VEC_ELT (doloop_list, i, lp) |
| 2610 |
{ |
| 2611 |
gfc_symbol *do_sym; |
| 2612 |
cl = lp->c; |
| 2613 |
|
| 2614 |
if (cl == NULL) |
| 2615 |
break; |
| 2616 |
|
| 2617 |
do_sym = cl->ext.iterator->var->symtree->n.sym; |
| 2618 |
|
| 2619 |
if (a->expr && a->expr->symtree && f->sym |
| 2620 |
&& a->expr->symtree->n.sym == do_sym) |
| 2621 |
{ |
| 2622 |
if (f->sym->attr.intent == INTENT_OUT) |
| 2623 |
gfc_error_now ("Variable %qs at %L set to undefined " |
| 2624 |
"value inside loop beginning at %L as " |
| 2625 |
"INTENT(OUT) argument to subroutine %qs", |
| 2626 |
do_sym->name, &a->expr->where, |
| 2627 |
&(doloop_list[i].c->loc), |
| 2628 |
co->symtree->n.sym->name); |
| 2629 |
else if (f->sym->attr.intent == INTENT_INOUT) |
| 2630 |
gfc_error_now ("Variable %qs at %L not definable inside " |
| 2631 |
"loop beginning at %L as INTENT(INOUT) " |
| 2632 |
"argument to subroutine %qs", |
| 2633 |
do_sym->name, &a->expr->where, |
| 2634 |
&(doloop_list[i].c->loc), |
| 2635 |
co->symtree->n.sym->name); |
| 2636 |
} |
| 2637 |
} |
| 2638 |
a = a->next; |
| 2639 |
f = f->next; |
| 2640 |
} |
| 2641 |
|
| 2642 |
break; |
| 2643 |
|
| 2644 |
default: |
| 2645 |
break; |
| 2646 |
} |
| 2647 |
if (seen_goto && doloop_level > 0) |
| 2648 |
doloop_list[doloop_level-1].seen_goto = true; |
| 2649 |
|
| 2650 |
return 0; |
| 2651 |
} |
| 2652 |
|
| 2653 |
/* Callback function to warn about different things within DO loops. */ |
| 2654 |
|
| 2655 |
static int |
| 2656 |
do_function (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 2657 |
void *data ATTRIBUTE_UNUSED) |
| 2658 |
{ |
| 2659 |
do_t *last; |
| 2660 |
|
| 2661 |
if (doloop_list.length () == 0) |
| 2662 |
return 0; |
| 2663 |
|
| 2664 |
if ((*e)->expr_type == EXPR_FUNCTION) |
| 2665 |
do_intent (e); |
| 2666 |
|
| 2667 |
last = &doloop_list.last(); |
| 2668 |
if (last->seen_goto && !warn_do_subscript) |
| 2669 |
return 0; |
| 2670 |
|
| 2671 |
if ((*e)->expr_type == EXPR_VARIABLE) |
| 2672 |
do_subscript (e); |
| 2673 |
|
| 2674 |
return 0; |
| 2675 |
} |
| 2676 |
|
| 2677 |
typedef struct |
| 2678 |
{ |
| 2679 |
gfc_symbol *sym; |
| 2680 |
mpz_t val; |
| 2681 |
} insert_index_t; |
| 2682 |
|
| 2683 |
/* Callback function - if the expression is the variable in data->sym, |
| 2684 |
replace it with a constant from data->val. */ |
| 2685 |
|
| 2686 |
static int |
| 2687 |
callback_insert_index (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 2688 |
void *data) |
| 2689 |
{ |
| 2690 |
insert_index_t *d; |
| 2691 |
gfc_expr *ex, *n; |
| 2692 |
|
| 2693 |
ex = (*e); |
| 2694 |
if (ex->expr_type != EXPR_VARIABLE) |
| 2695 |
return 0; |
| 2696 |
|
| 2697 |
d = (insert_index_t *) data; |
| 2698 |
if (ex->symtree->n.sym != d->sym) |
| 2699 |
return 0; |
| 2700 |
|
| 2701 |
n = gfc_get_constant_expr (BT_INTEGER, ex->ts.kind, &ex->where); |
| 2702 |
mpz_set (n->value.integer, d->val); |
| 2703 |
|
| 2704 |
gfc_free_expr (ex); |
| 2705 |
*e = n; |
| 2706 |
return 0; |
| 2707 |
} |
| 2708 |
|
| 2709 |
/* In the expression e, replace occurrences of the variable sym with |
| 2710 |
val. If this results in a constant expression, return true and |
| 2711 |
return the value in ret. Return false if the expression already |
| 2712 |
is a constant. Caller has to clear ret in that case. */ |
| 2713 |
|
| 2714 |
static bool |
| 2715 |
insert_index (gfc_expr *e, gfc_symbol *sym, mpz_t val, mpz_t ret) |
| 2716 |
{ |
| 2717 |
gfc_expr *n; |
| 2718 |
insert_index_t data; |
| 2719 |
bool rc; |
| 2720 |
|
| 2721 |
if (e->expr_type == EXPR_CONSTANT) |
| 2722 |
return false; |
| 2723 |
|
| 2724 |
n = gfc_copy_expr (e); |
| 2725 |
data.sym = sym; |
| 2726 |
mpz_init_set (data.val, val); |
| 2727 |
gfc_expr_walker (&n, callback_insert_index, (void *) &data); |
| 2728 |
|
| 2729 |
/* Suppress errors here - we could get errors here such as an |
| 2730 |
out of bounds access for arrays, see PR 90563. */ |
| 2731 |
gfc_push_suppress_errors (); |
| 2732 |
gfc_simplify_expr (n, 0); |
| 2733 |
gfc_pop_suppress_errors (); |
| 2734 |
|
| 2735 |
if (n->expr_type == EXPR_CONSTANT) |
| 2736 |
{ |
| 2737 |
rc = true; |
| 2738 |
mpz_init_set (ret, n->value.integer); |
| 2739 |
} |
| 2740 |
else |
| 2741 |
rc = false; |
| 2742 |
|
| 2743 |
mpz_clear (data.val); |
| 2744 |
gfc_free_expr (n); |
| 2745 |
return rc; |
| 2746 |
|
| 2747 |
} |
| 2748 |
|
| 2749 |
static bool |
| 2750 |
evaluate_loop_bound (gfc_expr *e, gfc_symbol *sym, mpz_t val, mpz_t ret) |
| 2751 |
{ |
| 2752 |
if (e->expr_type == EXPR_CONSTANT) |
| 2753 |
{ |
| 2754 |
mpz_init_set (ret, e->value.integer); |
| 2755 |
return true; |
| 2756 |
} |
| 2757 |
|
| 2758 |
return insert_index (e, sym, val, ret); |
| 2759 |
} |
| 2760 |
|
| 2761 |
/* Return true if any loop nested inside LOOP_INDEX is not provably entered |
| 2762 |
after substituting OUTER_VAL for OUTER_SYM. In that case the guarded array |
| 2763 |
reference may never be evaluated, so do not warn from the outer loop alone. */ |
| 2764 |
|
| 2765 |
static bool |
| 2766 |
inner_loop_may_be_skipped (int loop_index, gfc_symbol *outer_sym, mpz_t outer_val) |
| 2767 |
{ |
| 2768 |
int k; |
| 2769 |
do_t *lp; |
| 2770 |
|
| 2771 |
FOR_EACH_VEC_ELT_FROM (doloop_list, k, lp, loop_index + 1) |
| 2772 |
{ |
| 2773 |
gfc_code *loop = lp->c; |
| 2774 |
int sgn, cmp; |
| 2775 |
mpz_t do_start, do_end, do_step; |
| 2776 |
|
| 2777 |
if (loop == NULL || loop->ext.iterator == NULL || loop->ext.iterator->var == NULL) |
| 2778 |
return true; |
| 2779 |
|
| 2780 |
if (!evaluate_loop_bound (loop->ext.iterator->step, outer_sym, outer_val, do_step)) |
| 2781 |
return true; |
| 2782 |
|
| 2783 |
sgn = mpz_cmp_ui (do_step, 0); |
| 2784 |
if (sgn == 0) |
| 2785 |
{ |
| 2786 |
mpz_clear (do_step); |
| 2787 |
return true; |
| 2788 |
} |
| 2789 |
|
| 2790 |
if (!evaluate_loop_bound (loop->ext.iterator->start, outer_sym, outer_val, do_start) |
| 2791 |
|| !evaluate_loop_bound (loop->ext.iterator->end, outer_sym, outer_val, do_end)) |
| 2792 |
{ |
| 2793 |
mpz_clear (do_step); |
| 2794 |
return true; |
| 2795 |
} |
| 2796 |
|
| 2797 |
cmp = mpz_cmp (do_end, do_start); |
| 2798 |
mpz_clear (do_start); |
| 2799 |
mpz_clear (do_end); |
| 2800 |
mpz_clear (do_step); |
| 2801 |
|
| 2802 |
if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)) |
| 2803 |
return true; |
| 2804 |
} |
| 2805 |
|
| 2806 |
return false; |
| 2807 |
} |
| 2808 |
|
| 2809 |
/* Check array subscripts for possible out-of-bounds accesses in DO |
| 2810 |
loops with constant bounds. */ |
| 2811 |
|
| 2812 |
static int |
| 2813 |
do_subscript (gfc_expr **e) |
| 2814 |
{ |
| 2815 |
gfc_expr *v; |
| 2816 |
gfc_array_ref *ar; |
| 2817 |
gfc_ref *ref; |
| 2818 |
int i,j; |
| 2819 |
gfc_code *dl; |
| 2820 |
do_t *lp; |
| 2821 |
|
| 2822 |
v = *e; |
| 2823 |
/* Constants are already checked. */ |
| 2824 |
if (v->expr_type == EXPR_CONSTANT) |
| 2825 |
return 0; |
| 2826 |
|
| 2827 |
/* Wrong warnings will be generated in an associate list. */ |
| 2828 |
if (in_assoc_list) |
| 2829 |
return 0; |
| 2830 |
|
| 2831 |
/* We already warned about this. */ |
| 2832 |
if (v->do_not_warn) |
| 2833 |
return 0; |
| 2834 |
|
| 2835 |
v->do_not_warn = 1; |
| 2836 |
|
| 2837 |
for (ref = v->ref; ref; ref = ref->next) |
| 2838 |
{ |
| 2839 |
if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT) |
| 2840 |
{ |
| 2841 |
ar = & ref->u.ar; |
| 2842 |
FOR_EACH_VEC_ELT (doloop_list, j, lp) |
| 2843 |
{ |
| 2844 |
gfc_symbol *do_sym; |
| 2845 |
mpz_t do_start, do_step, do_end; |
| 2846 |
bool have_do_start, have_do_end; |
| 2847 |
bool error_not_proven; |
| 2848 |
int warn; |
| 2849 |
int sgn; |
| 2850 |
|
| 2851 |
dl = lp->c; |
| 2852 |
if (dl == NULL) |
| 2853 |
break; |
| 2854 |
|
| 2855 |
/* If we are within a branch, or a goto or equivalent |
| 2856 |
was seen in the DO loop before, then we cannot prove that |
| 2857 |
this expression is actually evaluated. Don't do anything |
| 2858 |
unless we want to see it all. */ |
| 2859 |
error_not_proven = lp->seen_goto |
| 2860 |
|| lp->branch_level < if_level + select_level; |
| 2861 |
|
| 2862 |
if (error_not_proven && !warn_do_subscript) |
| 2863 |
break; |
| 2864 |
|
| 2865 |
if (error_not_proven) |
| 2866 |
warn = OPT_Wdo_subscript; |
| 2867 |
else |
| 2868 |
warn = 0; |
| 2869 |
|
| 2870 |
do_sym = dl->ext.iterator->var->symtree->n.sym; |
| 2871 |
if (do_sym->ts.type != BT_INTEGER) |
| 2872 |
continue; |
| 2873 |
|
| 2874 |
/* If we do not know about the stepsize, the loop may be zero trip. |
| 2875 |
Do not warn in this case. */ |
| 2876 |
|
| 2877 |
if (dl->ext.iterator->step->expr_type == EXPR_CONSTANT) |
| 2878 |
{ |
| 2879 |
sgn = mpz_cmp_ui (dl->ext.iterator->step->value.integer, 0); |
| 2880 |
/* This can happen, but then the error has been |
| 2881 |
reported previously. */ |
| 2882 |
if (sgn == 0) |
| 2883 |
continue; |
| 2884 |
|
| 2885 |
mpz_init_set (do_step, dl->ext.iterator->step->value.integer); |
| 2886 |
} |
| 2887 |
|
| 2888 |
else |
| 2889 |
continue; |
| 2890 |
|
| 2891 |
if (dl->ext.iterator->start->expr_type == EXPR_CONSTANT) |
| 2892 |
{ |
| 2893 |
have_do_start = true; |
| 2894 |
mpz_init_set (do_start, dl->ext.iterator->start->value.integer); |
| 2895 |
} |
| 2896 |
else |
| 2897 |
have_do_start = false; |
| 2898 |
|
| 2899 |
if (dl->ext.iterator->end->expr_type == EXPR_CONSTANT) |
| 2900 |
{ |
| 2901 |
have_do_end = true; |
| 2902 |
mpz_init_set (do_end, dl->ext.iterator->end->value.integer); |
| 2903 |
} |
| 2904 |
else |
| 2905 |
have_do_end = false; |
| 2906 |
|
| 2907 |
if (!have_do_start && !have_do_end) |
| 2908 |
{ |
| 2909 |
mpz_clear (do_step); |
| 2910 |
return 0; |
| 2911 |
} |
| 2912 |
|
| 2913 |
/* No warning inside a zero-trip loop. */ |
| 2914 |
if (have_do_start && have_do_end) |
| 2915 |
{ |
| 2916 |
int cmp; |
| 2917 |
|
| 2918 |
cmp = mpz_cmp (do_end, do_start); |
| 2919 |
if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)) |
| 2920 |
{ |
| 2921 |
mpz_clear (do_start); |
| 2922 |
mpz_clear (do_end); |
| 2923 |
mpz_clear (do_step); |
| 2924 |
break; |
| 2925 |
} |
| 2926 |
} |
| 2927 |
|
| 2928 |
/* May have to correct the end value if the step does not equal |
| 2929 |
one. */ |
| 2930 |
if (have_do_start && have_do_end && mpz_cmp_ui (do_step, 1) != 0) |
| 2931 |
{ |
| 2932 |
mpz_t diff, rem; |
| 2933 |
|
| 2934 |
mpz_init (diff); |
| 2935 |
mpz_init (rem); |
| 2936 |
mpz_sub (diff, do_end, do_start); |
| 2937 |
mpz_tdiv_r (rem, diff, do_step); |
| 2938 |
mpz_sub (do_end, do_end, rem); |
| 2939 |
mpz_clear (diff); |
| 2940 |
mpz_clear (rem); |
| 2941 |
} |
| 2942 |
|
| 2943 |
bool skip_start = have_do_start |
| 2944 |
&& inner_loop_may_be_skipped (j, do_sym, do_start); |
| 2945 |
bool skip_end = have_do_end |
| 2946 |
&& inner_loop_may_be_skipped (j, do_sym, do_end); |
| 2947 |
|
| 2948 |
for (i = 0; i< ar->dimen; i++) |
| 2949 |
{ |
| 2950 |
mpz_t val; |
| 2951 |
if (ar->dimen_type[i] == DIMEN_ELEMENT && have_do_start && !skip_start |
| 2952 |
&& insert_index (ar->start[i], do_sym, do_start, val)) |
| 2953 |
{ |
| 2954 |
if (ar->as->lower[i] |
| 2955 |
&& ar->as->lower[i]->expr_type == EXPR_CONSTANT |
| 2956 |
&& ar->as->lower[i]->ts.type == BT_INTEGER |
| 2957 |
&& mpz_cmp (val, ar->as->lower[i]->value.integer) < 0) |
| 2958 |
gfc_warning (warn, "Array reference at %L out of bounds " |
| 2959 |
"(%ld < %ld) in loop beginning at %L", |
| 2960 |
&ar->start[i]->where, mpz_get_si (val), |
| 2961 |
mpz_get_si (ar->as->lower[i]->value.integer), |
| 2962 |
&doloop_list[j].c->loc); |
| 2963 |
|
| 2964 |
if (ar->as->upper[i] |
| 2965 |
&& ar->as->upper[i]->expr_type == EXPR_CONSTANT |
| 2966 |
&& ar->as->upper[i]->ts.type == BT_INTEGER |
| 2967 |
&& mpz_cmp (val, ar->as->upper[i]->value.integer) > 0) |
| 2968 |
gfc_warning (warn, "Array reference at %L out of bounds " |
| 2969 |
"(%ld > %ld) in loop beginning at %L", |
| 2970 |
&ar->start[i]->where, mpz_get_si (val), |
| 2971 |
mpz_get_si (ar->as->upper[i]->value.integer), |
| 2972 |
&doloop_list[j].c->loc); |
| 2973 |
|
| 2974 |
mpz_clear (val); |
| 2975 |
} |
| 2976 |
|
| 2977 |
if (ar->dimen_type[i] == DIMEN_ELEMENT && have_do_end && !skip_end |
| 2978 |
&& insert_index (ar->start[i], do_sym, do_end, val)) |
| 2979 |
{ |
| 2980 |
if (ar->as->lower[i] |
| 2981 |
&& ar->as->lower[i]->expr_type == EXPR_CONSTANT |
| 2982 |
&& ar->as->lower[i]->ts.type == BT_INTEGER |
| 2983 |
&& mpz_cmp (val, ar->as->lower[i]->value.integer) < 0) |
| 2984 |
gfc_warning (warn, "Array reference at %L out of bounds " |
| 2985 |
"(%ld < %ld) in loop beginning at %L", |
| 2986 |
&ar->start[i]->where, mpz_get_si (val), |
| 2987 |
mpz_get_si (ar->as->lower[i]->value.integer), |
| 2988 |
&doloop_list[j].c->loc); |
| 2989 |
|
| 2990 |
if (ar->as->upper[i] |
| 2991 |
&& ar->as->upper[i]->expr_type == EXPR_CONSTANT |
| 2992 |
&& ar->as->upper[i]->ts.type == BT_INTEGER |
| 2993 |
&& mpz_cmp (val, ar->as->upper[i]->value.integer) > 0) |
| 2994 |
gfc_warning (warn, "Array reference at %L out of bounds " |
| 2995 |
"(%ld > %ld) in loop beginning at %L", |
| 2996 |
&ar->start[i]->where, mpz_get_si (val), |
| 2997 |
mpz_get_si (ar->as->upper[i]->value.integer), |
| 2998 |
&doloop_list[j].c->loc); |
| 2999 |
|
| 3000 |
mpz_clear (val); |
| 3001 |
} |
| 3002 |
} |
| 3003 |
|
| 3004 |
if (have_do_start) |
| 3005 |
mpz_clear (do_start); |
| 3006 |
if (have_do_end) |
| 3007 |
mpz_clear (do_end); |
| 3008 |
mpz_clear (do_step); |
| 3009 |
} |
| 3010 |
} |
| 3011 |
} |
| 3012 |
return 0; |
| 3013 |
} |
| 3014 |
/* Function for functions checking that we do not pass a DO variable |
| 3015 |
to an INTENT(OUT) or INTENT(INOUT) dummy variable. */ |
| 3016 |
|
| 3017 |
static int |
| 3018 |
do_intent (gfc_expr **e) |
| 3019 |
{ |
| 3020 |
gfc_formal_arglist *f; |
| 3021 |
gfc_actual_arglist *a; |
| 3022 |
gfc_expr *expr; |
| 3023 |
gfc_code *dl; |
| 3024 |
do_t *lp; |
| 3025 |
int i; |
| 3026 |
gfc_symbol *sym; |
| 3027 |
|
| 3028 |
expr = *e; |
| 3029 |
if (expr->expr_type != EXPR_FUNCTION) |
| 3030 |
return 0; |
| 3031 |
|
| 3032 |
/* Intrinsic functions don't modify their arguments. */ |
| 3033 |
|
| 3034 |
if (expr->value.function.isym) |
| 3035 |
return 0; |
| 3036 |
|
| 3037 |
sym = expr->value.function.esym; |
| 3038 |
if (sym == NULL) |
| 3039 |
return 0; |
| 3040 |
|
| 3041 |
if (sym->attr.contained) |
| 3042 |
{ |
| 3043 |
FOR_EACH_VEC_ELT (doloop_list, i, lp) |
| 3044 |
{ |
| 3045 |
contained_info info; |
| 3046 |
gfc_namespace *ns; |
| 3047 |
|
| 3048 |
dl = lp->c; |
| 3049 |
info.do_var = dl->ext.iterator->var->symtree->n.sym; |
| 3050 |
info.procedure = sym; |
| 3051 |
info.where_do = expr->where; |
| 3052 |
/* Look contained procedures under the namespace of the |
| 3053 |
variable. */ |
| 3054 |
for (ns = info.do_var->ns->contained; ns; ns = ns->sibling) |
| 3055 |
if (ns->proc_name && ns->proc_name == sym) |
| 3056 |
gfc_code_walker (&ns->code, doloop_contained_procedure_code, |
| 3057 |
dummy_expr_callback, &info); |
| 3058 |
} |
| 3059 |
} |
| 3060 |
|
| 3061 |
f = gfc_sym_get_dummy_args (sym); |
| 3062 |
|
| 3063 |
/* Without a formal arglist, there is only unknown INTENT, |
| 3064 |
which we don't check for. */ |
| 3065 |
if (f == NULL) |
| 3066 |
return 0; |
| 3067 |
|
| 3068 |
a = expr->value.function.actual; |
| 3069 |
|
| 3070 |
while (a && f) |
| 3071 |
{ |
| 3072 |
FOR_EACH_VEC_ELT (doloop_list, i, lp) |
| 3073 |
{ |
| 3074 |
gfc_symbol *do_sym; |
| 3075 |
dl = lp->c; |
| 3076 |
if (dl == NULL) |
| 3077 |
break; |
| 3078 |
|
| 3079 |
do_sym = dl->ext.iterator->var->symtree->n.sym; |
| 3080 |
|
| 3081 |
if (a->expr && a->expr->symtree |
| 3082 |
&& a->expr->symtree->n.sym == do_sym |
| 3083 |
&& f->sym) |
| 3084 |
{ |
| 3085 |
if (f->sym->attr.intent == INTENT_OUT) |
| 3086 |
gfc_error_now ("Variable %qs at %L set to undefined value " |
| 3087 |
"inside loop beginning at %L as INTENT(OUT) " |
| 3088 |
"argument to function %qs", do_sym->name, |
| 3089 |
&a->expr->where, &doloop_list[i].c->loc, |
| 3090 |
expr->symtree->n.sym->name); |
| 3091 |
else if (f->sym->attr.intent == INTENT_INOUT) |
| 3092 |
gfc_error_now ("Variable %qs at %L not definable inside loop" |
| 3093 |
" beginning at %L as INTENT(INOUT) argument to" |
| 3094 |
" function %qs", do_sym->name, |
| 3095 |
&a->expr->where, &doloop_list[i].c->loc, |
| 3096 |
expr->symtree->n.sym->name); |
| 3097 |
} |
| 3098 |
} |
| 3099 |
a = a->next; |
| 3100 |
f = f->next; |
| 3101 |
} |
| 3102 |
|
| 3103 |
return 0; |
| 3104 |
} |
| 3105 |
|
| 3106 |
static void |
| 3107 |
doloop_warn (gfc_namespace *ns) |
| 3108 |
{ |
| 3109 |
gfc_code_walker (&ns->code, doloop_code, do_function, NULL); |
| 3110 |
|
| 3111 |
for (ns = ns->contained; ns; ns = ns->sibling) |
| 3112 |
{ |
| 3113 |
if (ns->code == NULL || ns->code->op != EXEC_BLOCK) |
| 3114 |
doloop_warn (ns); |
| 3115 |
} |
| 3116 |
} |
| 3117 |
|
| 3118 |
/* This selction deals with inlining calls to MATMUL. */ |
| 3119 |
|
| 3120 |
/* Replace calls to matmul outside of straight assignments with a temporary |
| 3121 |
variable so that later inlining will work. */ |
| 3122 |
|
| 3123 |
static int |
| 3124 |
matmul_to_var_expr (gfc_expr **ep, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 3125 |
void *data) |
| 3126 |
{ |
| 3127 |
gfc_expr *e, *n; |
| 3128 |
bool *found = (bool *) data; |
| 3129 |
|
| 3130 |
e = *ep; |
| 3131 |
|
| 3132 |
if (e->expr_type != EXPR_FUNCTION |
| 3133 |
|| e->value.function.isym == NULL |
| 3134 |
|| e->value.function.isym->id != GFC_ISYM_MATMUL) |
| 3135 |
return 0; |
| 3136 |
|
| 3137 |
if (forall_level > 0 || iterator_level > 0 || in_omp_workshare |
| 3138 |
|| in_omp_atomic || in_where || in_assoc_list) |
| 3139 |
return 0; |
| 3140 |
|
| 3141 |
/* Check if this is already in the form c = matmul(a,b). */ |
| 3142 |
|
| 3143 |
if ((*current_code)->expr2 == e) |
| 3144 |
return 0; |
| 3145 |
|
| 3146 |
n = create_var (e, "matmul"); |
| 3147 |
|
| 3148 |
/* If create_var is unable to create a variable (for example if |
| 3149 |
-fno-realloc-lhs is in force with a variable that does not have bounds |
| 3150 |
known at compile-time), just return. */ |
| 3151 |
|
| 3152 |
if (n == NULL) |
| 3153 |
return 0; |
| 3154 |
|
| 3155 |
*ep = n; |
| 3156 |
*found = true; |
| 3157 |
return 0; |
| 3158 |
} |
| 3159 |
|
| 3160 |
/* Set current_code and associated variables so that matmul_to_var_expr can |
| 3161 |
work. */ |
| 3162 |
|
| 3163 |
static int |
| 3164 |
matmul_to_var_code (gfc_code **c, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 3165 |
void *data ATTRIBUTE_UNUSED) |
| 3166 |
{ |
| 3167 |
if (current_code != c) |
| 3168 |
{ |
| 3169 |
current_code = c; |
| 3170 |
inserted_block = NULL; |
| 3171 |
changed_statement = NULL; |
| 3172 |
} |
| 3173 |
|
| 3174 |
return 0; |
| 3175 |
} |
| 3176 |
|
| 3177 |
|
| 3178 |
/* Take a statement of the shape c = matmul(a,b) and create temporaries |
| 3179 |
for a and b if there is a dependency between the arguments and the |
| 3180 |
result variable or if a or b are the result of calculations that cannot |
| 3181 |
be handled by the inliner. */ |
| 3182 |
|
| 3183 |
static int |
| 3184 |
matmul_temp_args (gfc_code **c, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 3185 |
void *data ATTRIBUTE_UNUSED) |
| 3186 |
{ |
| 3187 |
gfc_expr *expr1, *expr2; |
| 3188 |
gfc_code *co; |
| 3189 |
gfc_actual_arglist *a, *b; |
| 3190 |
bool a_tmp, b_tmp; |
| 3191 |
gfc_expr *matrix_a, *matrix_b; |
| 3192 |
bool conjg_a, conjg_b, transpose_a, transpose_b; |
| 3193 |
|
| 3194 |
co = *c; |
| 3195 |
|
| 3196 |
if (co->op != EXEC_ASSIGN) |
| 3197 |
return 0; |
| 3198 |
|
| 3199 |
if (forall_level > 0 || iterator_level > 0 || in_omp_workshare |
| 3200 |
|| in_omp_atomic || in_where) |
| 3201 |
return 0; |
| 3202 |
|
| 3203 |
/* This has some duplication with inline_matmul_assign. This |
| 3204 |
is because the creation of temporary variables could still fail, |
| 3205 |
and inline_matmul_assign still needs to be able to handle these |
| 3206 |
cases. */ |
| 3207 |
expr1 = co->expr1; |
| 3208 |
expr2 = co->expr2; |
| 3209 |
|
| 3210 |
if (expr2->expr_type != EXPR_FUNCTION |
| 3211 |
|| expr2->value.function.isym == NULL |
| 3212 |
|| expr2->value.function.isym->id != GFC_ISYM_MATMUL) |
| 3213 |
return 0; |
| 3214 |
|
| 3215 |
a_tmp = false; |
| 3216 |
a = expr2->value.function.actual; |
| 3217 |
matrix_a = check_conjg_transpose_variable (a->expr, &conjg_a, &transpose_a); |
| 3218 |
if (matrix_a != NULL) |
| 3219 |
{ |
| 3220 |
if (matrix_a->expr_type == EXPR_VARIABLE |
| 3221 |
&& (gfc_check_dependency (matrix_a, expr1, true) |
| 3222 |
|| gfc_has_dimen_vector_ref (matrix_a))) |
| 3223 |
a_tmp = true; |
| 3224 |
} |
| 3225 |
else |
| 3226 |
a_tmp = true; |
| 3227 |
|
| 3228 |
b_tmp = false; |
| 3229 |
b = a->next; |
| 3230 |
matrix_b = check_conjg_transpose_variable (b->expr, &conjg_b, &transpose_b); |
| 3231 |
if (matrix_b != NULL) |
| 3232 |
{ |
| 3233 |
if (matrix_b->expr_type == EXPR_VARIABLE |
| 3234 |
&& (gfc_check_dependency (matrix_b, expr1, true) |
| 3235 |
|| gfc_has_dimen_vector_ref (matrix_b))) |
| 3236 |
b_tmp = true; |
| 3237 |
} |
| 3238 |
else |
| 3239 |
b_tmp = true; |
| 3240 |
|
| 3241 |
if (!a_tmp && !b_tmp) |
| 3242 |
return 0; |
| 3243 |
|
| 3244 |
current_code = c; |
| 3245 |
inserted_block = NULL; |
| 3246 |
changed_statement = NULL; |
| 3247 |
if (a_tmp) |
| 3248 |
{ |
| 3249 |
gfc_expr *at; |
| 3250 |
at = create_var (a->expr,"mma"); |
| 3251 |
if (at) |
| 3252 |
a->expr = at; |
| 3253 |
} |
| 3254 |
if (b_tmp) |
| 3255 |
{ |
| 3256 |
gfc_expr *bt; |
| 3257 |
bt = create_var (b->expr,"mmb"); |
| 3258 |
if (bt) |
| 3259 |
b->expr = bt; |
| 3260 |
} |
| 3261 |
return 0; |
| 3262 |
} |
| 3263 |
|
| 3264 |
/* Auxiliary function to build and simplify an array inquiry function. |
| 3265 |
dim is zero-based. */ |
| 3266 |
|
| 3267 |
static gfc_expr * |
| 3268 |
get_array_inq_function (gfc_isym_id id, gfc_expr *e, int dim, int okind = 0) |
| 3269 |
{ |
| 3270 |
gfc_expr *fcn; |
| 3271 |
gfc_expr *dim_arg, *kind; |
| 3272 |
const char *name; |
| 3273 |
gfc_expr *ec; |
| 3274 |
|
| 3275 |
switch (id) |
| 3276 |
{ |
| 3277 |
case GFC_ISYM_LBOUND: |
| 3278 |
name = "_gfortran_lbound"; |
| 3279 |
break; |
| 3280 |
|
| 3281 |
case GFC_ISYM_UBOUND: |
| 3282 |
name = "_gfortran_ubound"; |
| 3283 |
break; |
| 3284 |
|
| 3285 |
case GFC_ISYM_SIZE: |
| 3286 |
name = "_gfortran_size"; |
| 3287 |
break; |
| 3288 |
|
| 3289 |
default: |
| 3290 |
gcc_unreachable (); |
| 3291 |
} |
| 3292 |
|
| 3293 |
dim_arg = gfc_get_int_expr (gfc_default_integer_kind, &e->where, dim); |
| 3294 |
if (okind != 0) |
| 3295 |
kind = gfc_get_int_expr (gfc_default_integer_kind, &e->where, |
| 3296 |
okind); |
| 3297 |
else |
| 3298 |
kind = gfc_get_int_expr (gfc_default_integer_kind, &e->where, |
| 3299 |
gfc_index_integer_kind); |
| 3300 |
|
| 3301 |
ec = gfc_copy_expr (e); |
| 3302 |
|
| 3303 |
/* No bounds checking, this will be done before the loops if -fcheck=bounds |
| 3304 |
is in effect. */ |
| 3305 |
ec->no_bounds_check = 1; |
| 3306 |
fcn = gfc_build_intrinsic_call (current_ns, id, name, e->where, 3, |
| 3307 |
ec, dim_arg, kind); |
| 3308 |
gfc_simplify_expr (fcn, 0); |
| 3309 |
fcn->no_bounds_check = 1; |
| 3310 |
return fcn; |
| 3311 |
} |
| 3312 |
|
| 3313 |
/* Builds a logical expression. */ |
| 3314 |
|
| 3315 |
static gfc_expr* |
| 3316 |
build_logical_expr (gfc_intrinsic_op op, gfc_expr *e1, gfc_expr *e2) |
| 3317 |
{ |
| 3318 |
gfc_typespec ts; |
| 3319 |
gfc_expr *res; |
| 3320 |
|
| 3321 |
ts.type = BT_LOGICAL; |
| 3322 |
ts.kind = gfc_default_logical_kind; |
| 3323 |
res = gfc_get_expr (); |
| 3324 |
res->where = e1->where; |
| 3325 |
res->expr_type = EXPR_OP; |
| 3326 |
res->value.op.op = op; |
| 3327 |
res->value.op.op1 = e1; |
| 3328 |
res->value.op.op2 = e2; |
| 3329 |
res->ts = ts; |
| 3330 |
|
| 3331 |
return res; |
| 3332 |
} |
| 3333 |
|
| 3334 |
|
| 3335 |
/* Return an operation of one two gfc_expr (one if e2 is NULL). This assumes |
| 3336 |
compatible typespecs. */ |
| 3337 |
|
| 3338 |
static gfc_expr * |
| 3339 |
get_operand (gfc_intrinsic_op op, gfc_expr *e1, gfc_expr *e2) |
| 3340 |
{ |
| 3341 |
gfc_expr *res; |
| 3342 |
|
| 3343 |
res = gfc_get_expr (); |
| 3344 |
res->ts = e1->ts; |
| 3345 |
res->where = e1->where; |
| 3346 |
res->expr_type = EXPR_OP; |
| 3347 |
res->value.op.op = op; |
| 3348 |
res->value.op.op1 = e1; |
| 3349 |
res->value.op.op2 = e2; |
| 3350 |
gfc_simplify_expr (res, 0); |
| 3351 |
return res; |
| 3352 |
} |
| 3353 |
|
| 3354 |
/* Generate the IF statement for a runtime check if we want to do inlining or |
| 3355 |
not - putting in the code for both branches and putting it into the syntax |
| 3356 |
tree is the caller's responsibility. For fixed array sizes, this should be |
| 3357 |
removed by DCE. Only called for rank-two matrices A and B. */ |
| 3358 |
|
| 3359 |
static gfc_code * |
| 3360 |
inline_limit_check (gfc_expr *a, gfc_expr *b, int limit, int rank_a) |
| 3361 |
{ |
| 3362 |
gfc_expr *inline_limit; |
| 3363 |
gfc_code *if_1, *if_2, *else_2; |
| 3364 |
gfc_expr *b2, *a2, *a1, *m1, *m2; |
| 3365 |
gfc_typespec ts; |
| 3366 |
gfc_expr *cond; |
| 3367 |
|
| 3368 |
gcc_assert (rank_a == 1 || rank_a == 2); |
| 3369 |
|
| 3370 |
/* Calculation is done in real to avoid integer overflow. */ |
| 3371 |
|
| 3372 |
inline_limit = gfc_get_constant_expr (BT_REAL, gfc_default_real_kind, |
| 3373 |
&a->where); |
| 3374 |
mpfr_set_si (inline_limit->value.real, limit, GFC_RND_MODE); |
| 3375 |
|
| 3376 |
/* Set the limit according to the rank. */ |
| 3377 |
mpfr_pow_ui (inline_limit->value.real, inline_limit->value.real, rank_a + 1, |
| 3378 |
GFC_RND_MODE); |
| 3379 |
|
| 3380 |
a1 = get_array_inq_function (GFC_ISYM_SIZE, a, 1); |
| 3381 |
|
| 3382 |
/* For a_rank = 1, must use one as the size of a along the second |
| 3383 |
dimension as to avoid too much code duplication. */ |
| 3384 |
|
| 3385 |
if (rank_a == 2) |
| 3386 |
a2 = get_array_inq_function (GFC_ISYM_SIZE, a, 2); |
| 3387 |
else |
| 3388 |
a2 = gfc_get_int_expr (gfc_index_integer_kind, &a->where, 1); |
| 3389 |
|
| 3390 |
b2 = get_array_inq_function (GFC_ISYM_SIZE, b, 2); |
| 3391 |
|
| 3392 |
gfc_clear_ts (&ts); |
| 3393 |
ts.type = BT_REAL; |
| 3394 |
ts.kind = gfc_default_real_kind; |
| 3395 |
gfc_convert_type_warn (a1, &ts, 2, 0); |
| 3396 |
gfc_convert_type_warn (a2, &ts, 2, 0); |
| 3397 |
gfc_convert_type_warn (b2, &ts, 2, 0); |
| 3398 |
|
| 3399 |
m1 = get_operand (INTRINSIC_TIMES, a1, a2); |
| 3400 |
m2 = get_operand (INTRINSIC_TIMES, m1, b2); |
| 3401 |
|
| 3402 |
cond = build_logical_expr (INTRINSIC_LE, m2, inline_limit); |
| 3403 |
gfc_simplify_expr (cond, 0); |
| 3404 |
|
| 3405 |
else_2 = XCNEW (gfc_code); |
| 3406 |
else_2->op = EXEC_IF; |
| 3407 |
else_2->loc = a->where; |
| 3408 |
|
| 3409 |
if_2 = XCNEW (gfc_code); |
| 3410 |
if_2->op = EXEC_IF; |
| 3411 |
if_2->expr1 = cond; |
| 3412 |
if_2->loc = a->where; |
| 3413 |
if_2->block = else_2; |
| 3414 |
|
| 3415 |
if_1 = XCNEW (gfc_code); |
| 3416 |
if_1->op = EXEC_IF; |
| 3417 |
if_1->block = if_2; |
| 3418 |
if_1->loc = a->where; |
| 3419 |
|
| 3420 |
return if_1; |
| 3421 |
} |
| 3422 |
|
| 3423 |
|
| 3424 |
/* Insert code to issue a runtime error if the expressions are not equal. */ |
| 3425 |
|
| 3426 |
static gfc_code * |
| 3427 |
runtime_error_ne (gfc_expr *e1, gfc_expr *e2, const char *msg) |
| 3428 |
{ |
| 3429 |
gfc_expr *cond; |
| 3430 |
gfc_code *if_1, *if_2; |
| 3431 |
gfc_code *c; |
| 3432 |
gfc_actual_arglist *a1, *a2, *a3; |
| 3433 |
|
| 3434 |
gcc_assert (GFC_LOCUS_IS_SET (e1->where)); |
| 3435 |
/* Build the call to runtime_error. */ |
| 3436 |
c = XCNEW (gfc_code); |
| 3437 |
c->op = EXEC_CALL; |
| 3438 |
c->loc = e1->where; |
| 3439 |
|
| 3440 |
/* Get a null-terminated message string. */ |
| 3441 |
|
| 3442 |
a1 = gfc_get_actual_arglist (); |
| 3443 |
a1->expr = gfc_get_character_expr (gfc_default_character_kind, &e1->where, |
| 3444 |
msg, strlen(msg)+1); |
| 3445 |
c->ext.actual = a1; |
| 3446 |
|
| 3447 |
/* Pass the value of the first expression. */ |
| 3448 |
a2 = gfc_get_actual_arglist (); |
| 3449 |
a2->expr = gfc_copy_expr (e1); |
| 3450 |
a1->next = a2; |
| 3451 |
|
| 3452 |
/* Pass the value of the second expression. */ |
| 3453 |
a3 = gfc_get_actual_arglist (); |
| 3454 |
a3->expr = gfc_copy_expr (e2); |
| 3455 |
a2->next = a3; |
| 3456 |
|
| 3457 |
gfc_check_fe_runtime_error (c->ext.actual); |
| 3458 |
gfc_resolve_fe_runtime_error (c); |
| 3459 |
|
| 3460 |
if_2 = XCNEW (gfc_code); |
| 3461 |
if_2->op = EXEC_IF; |
| 3462 |
if_2->loc = e1->where; |
| 3463 |
if_2->next = c; |
| 3464 |
|
| 3465 |
if_1 = XCNEW (gfc_code); |
| 3466 |
if_1->op = EXEC_IF; |
| 3467 |
if_1->block = if_2; |
| 3468 |
if_1->loc = e1->where; |
| 3469 |
|
| 3470 |
cond = build_logical_expr (INTRINSIC_NE, e1, e2); |
| 3471 |
gfc_simplify_expr (cond, 0); |
| 3472 |
if_2->expr1 = cond; |
| 3473 |
|
| 3474 |
return if_1; |
| 3475 |
} |
| 3476 |
|
| 3477 |
/* Handle matrix reallocation. Caller is responsible to insert into |
| 3478 |
the code tree. |
| 3479 |
|
| 3480 |
For the two-dimensional case, build |
| 3481 |
|
| 3482 |
if (allocated(c)) then |
| 3483 |
if (size(c,1) /= size(a,1) .or. size(c,2) /= size(b,2)) then |
| 3484 |
deallocate(c) |
| 3485 |
allocate (c(size(a,1), size(b,2))) |
| 3486 |
end if |
| 3487 |
else |
| 3488 |
allocate (c(size(a,1),size(b,2))) |
| 3489 |
end if |
| 3490 |
|
| 3491 |
and for the other cases correspondingly. |
| 3492 |
*/ |
| 3493 |
|
| 3494 |
static gfc_code * |
| 3495 |
matmul_lhs_realloc (gfc_expr *c, gfc_expr *a, gfc_expr *b, |
| 3496 |
enum matrix_case m_case) |
| 3497 |
{ |
| 3498 |
|
| 3499 |
gfc_expr *allocated, *alloc_expr; |
| 3500 |
gfc_code *if_alloc_1, *if_alloc_2, *if_size_1, *if_size_2; |
| 3501 |
gfc_code *else_alloc; |
| 3502 |
gfc_code *deallocate, *allocate1, *allocate_else; |
| 3503 |
gfc_array_ref *ar; |
| 3504 |
gfc_expr *cond, *ne1, *ne2; |
| 3505 |
|
| 3506 |
if (warn_realloc_lhs) |
| 3507 |
gfc_warning (OPT_Wrealloc_lhs, |
| 3508 |
"Code for reallocating the allocatable array at %L will " |
| 3509 |
"be added", &c->where); |
| 3510 |
|
| 3511 |
alloc_expr = gfc_copy_expr (c); |
| 3512 |
|
| 3513 |
ar = gfc_find_array_ref (alloc_expr); |
| 3514 |
gcc_assert (ar && ar->type == AR_FULL); |
| 3515 |
|
| 3516 |
/* c comes in as a full ref. Change it into a copy and make it into an |
| 3517 |
element ref so it has the right form for ALLOCATE. In the same |
| 3518 |
switch statement, also generate the size comparison for the secod IF |
| 3519 |
statement. */ |
| 3520 |
|
| 3521 |
ar->type = AR_ELEMENT; |
| 3522 |
|
| 3523 |
switch (m_case) |
| 3524 |
{ |
| 3525 |
case A2B2: |
| 3526 |
ar->start[0] = get_array_inq_function (GFC_ISYM_SIZE, a, 1); |
| 3527 |
ar->start[1] = get_array_inq_function (GFC_ISYM_SIZE, b, 2); |
| 3528 |
ne1 = build_logical_expr (INTRINSIC_NE, |
| 3529 |
get_array_inq_function (GFC_ISYM_SIZE, c, 1), |
| 3530 |
get_array_inq_function (GFC_ISYM_SIZE, a, 1)); |
| 3531 |
ne2 = build_logical_expr (INTRINSIC_NE, |
| 3532 |
get_array_inq_function (GFC_ISYM_SIZE, c, 2), |
| 3533 |
get_array_inq_function (GFC_ISYM_SIZE, b, 2)); |
| 3534 |
cond = build_logical_expr (INTRINSIC_OR, ne1, ne2); |
| 3535 |
break; |
| 3536 |
|
| 3537 |
case A2B2T: |
| 3538 |
ar->start[0] = get_array_inq_function (GFC_ISYM_SIZE, a, 1); |
| 3539 |
ar->start[1] = get_array_inq_function (GFC_ISYM_SIZE, b, 1); |
| 3540 |
|
| 3541 |
ne1 = build_logical_expr (INTRINSIC_NE, |
| 3542 |
get_array_inq_function (GFC_ISYM_SIZE, c, 1), |
| 3543 |
get_array_inq_function (GFC_ISYM_SIZE, a, 1)); |
| 3544 |
ne2 = build_logical_expr (INTRINSIC_NE, |
| 3545 |
get_array_inq_function (GFC_ISYM_SIZE, c, 2), |
| 3546 |
get_array_inq_function (GFC_ISYM_SIZE, b, 1)); |
| 3547 |
cond = build_logical_expr (INTRINSIC_OR, ne1, ne2); |
| 3548 |
break; |
| 3549 |
|
| 3550 |
case A2TB2: |
| 3551 |
|
| 3552 |
ar->start[0] = get_array_inq_function (GFC_ISYM_SIZE, a, 2); |
| 3553 |
ar->start[1] = get_array_inq_function (GFC_ISYM_SIZE, b, 2); |
| 3554 |
|
| 3555 |
ne1 = build_logical_expr (INTRINSIC_NE, |
| 3556 |
get_array_inq_function (GFC_ISYM_SIZE, c, 1), |
| 3557 |
get_array_inq_function (GFC_ISYM_SIZE, a, 2)); |
| 3558 |
ne2 = build_logical_expr (INTRINSIC_NE, |
| 3559 |
get_array_inq_function (GFC_ISYM_SIZE, c, 2), |
| 3560 |
get_array_inq_function (GFC_ISYM_SIZE, b, 2)); |
| 3561 |
cond = build_logical_expr (INTRINSIC_OR, ne1, ne2); |
| 3562 |
break; |
| 3563 |
|
| 3564 |
case A2B1: |
| 3565 |
ar->start[0] = get_array_inq_function (GFC_ISYM_SIZE, a, 1); |
| 3566 |
cond = build_logical_expr (INTRINSIC_NE, |
| 3567 |
get_array_inq_function (GFC_ISYM_SIZE, c, 1), |
| 3568 |
get_array_inq_function (GFC_ISYM_SIZE, a, 2)); |
| 3569 |
break; |
| 3570 |
|
| 3571 |
case A1B2: |
| 3572 |
ar->start[0] = get_array_inq_function (GFC_ISYM_SIZE, b, 2); |
| 3573 |
cond = build_logical_expr (INTRINSIC_NE, |
| 3574 |
get_array_inq_function (GFC_ISYM_SIZE, c, 1), |
| 3575 |
get_array_inq_function (GFC_ISYM_SIZE, b, 2)); |
| 3576 |
break; |
| 3577 |
|
| 3578 |
case A2TB2T: |
| 3579 |
/* This can only happen for BLAS, we do not handle that case in |
| 3580 |
inline mamtul. */ |
| 3581 |
ar->start[0] = get_array_inq_function (GFC_ISYM_SIZE, a, 2); |
| 3582 |
ar->start[1] = get_array_inq_function (GFC_ISYM_SIZE, b, 1); |
| 3583 |
|
| 3584 |
ne1 = build_logical_expr (INTRINSIC_NE, |
| 3585 |
get_array_inq_function (GFC_ISYM_SIZE, c, 1), |
| 3586 |
get_array_inq_function (GFC_ISYM_SIZE, a, 2)); |
| 3587 |
ne2 = build_logical_expr (INTRINSIC_NE, |
| 3588 |
get_array_inq_function (GFC_ISYM_SIZE, c, 2), |
| 3589 |
get_array_inq_function (GFC_ISYM_SIZE, b, 1)); |
| 3590 |
|
| 3591 |
cond = build_logical_expr (INTRINSIC_OR, ne1, ne2); |
| 3592 |
break; |
| 3593 |
|
| 3594 |
default: |
| 3595 |
gcc_unreachable(); |
| 3596 |
|
| 3597 |
} |
| 3598 |
|
| 3599 |
gfc_simplify_expr (cond, 0); |
| 3600 |
|
| 3601 |
/* We need two identical allocate statements in two |
| 3602 |
branches of the IF statement. */ |
| 3603 |
|
| 3604 |
allocate1 = XCNEW (gfc_code); |
| 3605 |
allocate1->op = EXEC_ALLOCATE; |
| 3606 |
allocate1->ext.alloc.list = gfc_get_alloc (); |
| 3607 |
allocate1->loc = c->where; |
| 3608 |
allocate1->ext.alloc.list->expr = gfc_copy_expr (alloc_expr); |
| 3609 |
|
| 3610 |
allocate_else = XCNEW (gfc_code); |
| 3611 |
allocate_else->op = EXEC_ALLOCATE; |
| 3612 |
allocate_else->ext.alloc.list = gfc_get_alloc (); |
| 3613 |
allocate_else->loc = c->where; |
| 3614 |
allocate_else->ext.alloc.list->expr = alloc_expr; |
| 3615 |
|
| 3616 |
allocated = gfc_build_intrinsic_call (current_ns, GFC_ISYM_ALLOCATED, |
| 3617 |
"_gfortran_allocated", c->where, |
| 3618 |
1, gfc_copy_expr (c)); |
| 3619 |
|
| 3620 |
deallocate = XCNEW (gfc_code); |
| 3621 |
deallocate->op = EXEC_DEALLOCATE; |
| 3622 |
deallocate->ext.alloc.list = gfc_get_alloc (); |
| 3623 |
deallocate->ext.alloc.list->expr = gfc_copy_expr (c); |
| 3624 |
deallocate->next = allocate1; |
| 3625 |
deallocate->loc = c->where; |
| 3626 |
|
| 3627 |
if_size_2 = XCNEW (gfc_code); |
| 3628 |
if_size_2->op = EXEC_IF; |
| 3629 |
if_size_2->expr1 = cond; |
| 3630 |
if_size_2->loc = c->where; |
| 3631 |
if_size_2->next = deallocate; |
| 3632 |
|
| 3633 |
if_size_1 = XCNEW (gfc_code); |
| 3634 |
if_size_1->op = EXEC_IF; |
| 3635 |
if_size_1->block = if_size_2; |
| 3636 |
if_size_1->loc = c->where; |
| 3637 |
|
| 3638 |
else_alloc = XCNEW (gfc_code); |
| 3639 |
else_alloc->op = EXEC_IF; |
| 3640 |
else_alloc->loc = c->where; |
| 3641 |
else_alloc->next = allocate_else; |
| 3642 |
|
| 3643 |
if_alloc_2 = XCNEW (gfc_code); |
| 3644 |
if_alloc_2->op = EXEC_IF; |
| 3645 |
if_alloc_2->expr1 = allocated; |
| 3646 |
if_alloc_2->loc = c->where; |
| 3647 |
if_alloc_2->next = if_size_1; |
| 3648 |
if_alloc_2->block = else_alloc; |
| 3649 |
|
| 3650 |
if_alloc_1 = XCNEW (gfc_code); |
| 3651 |
if_alloc_1->op = EXEC_IF; |
| 3652 |
if_alloc_1->block = if_alloc_2; |
| 3653 |
if_alloc_1->loc = c->where; |
| 3654 |
|
| 3655 |
return if_alloc_1; |
| 3656 |
} |
| 3657 |
|
| 3658 |
/* Callback function for has_function_or_op. */ |
| 3659 |
|
| 3660 |
static int |
| 3661 |
is_function_or_op (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 3662 |
void *data ATTRIBUTE_UNUSED) |
| 3663 |
{ |
| 3664 |
if ((*e) == 0) |
| 3665 |
return 0; |
| 3666 |
else |
| 3667 |
return (*e)->expr_type == EXPR_FUNCTION |
| 3668 |
|| (*e)->expr_type == EXPR_OP; |
| 3669 |
} |
| 3670 |
|
| 3671 |
/* Returns true if the expression contains a function. */ |
| 3672 |
|
| 3673 |
static bool |
| 3674 |
has_function_or_op (gfc_expr **e) |
| 3675 |
{ |
| 3676 |
if (e == NULL) |
| 3677 |
return false; |
| 3678 |
else |
| 3679 |
return gfc_expr_walker (e, is_function_or_op, NULL); |
| 3680 |
} |
| 3681 |
|
| 3682 |
/* Freeze (assign to a temporary variable) a single expression. */ |
| 3683 |
|
| 3684 |
static void |
| 3685 |
freeze_expr (gfc_expr **ep) |
| 3686 |
{ |
| 3687 |
gfc_expr *ne; |
| 3688 |
if (has_function_or_op (ep)) |
| 3689 |
{ |
| 3690 |
ne = create_var (*ep, "freeze"); |
| 3691 |
*ep = ne; |
| 3692 |
} |
| 3693 |
} |
| 3694 |
|
| 3695 |
/* Go through an expression's references and assign them to temporary |
| 3696 |
variables if they contain functions. This is usually done prior to |
| 3697 |
front-end scalarization to avoid multiple invocations of functions. */ |
| 3698 |
|
| 3699 |
static void |
| 3700 |
freeze_references (gfc_expr *e) |
| 3701 |
{ |
| 3702 |
gfc_ref *r; |
| 3703 |
gfc_array_ref *ar; |
| 3704 |
int i; |
| 3705 |
|
| 3706 |
for (r=e->ref; r; r=r->next) |
| 3707 |
{ |
| 3708 |
if (r->type == REF_SUBSTRING) |
| 3709 |
{ |
| 3710 |
if (r->u.ss.start != NULL) |
| 3711 |
freeze_expr (&r->u.ss.start); |
| 3712 |
|
| 3713 |
if (r->u.ss.end != NULL) |
| 3714 |
freeze_expr (&r->u.ss.end); |
| 3715 |
} |
| 3716 |
else if (r->type == REF_ARRAY) |
| 3717 |
{ |
| 3718 |
ar = &r->u.ar; |
| 3719 |
switch (ar->type) |
| 3720 |
{ |
| 3721 |
case AR_FULL: |
| 3722 |
break; |
| 3723 |
|
| 3724 |
case AR_SECTION: |
| 3725 |
for (i=0; i<ar->dimen; i++) |
| 3726 |
{ |
| 3727 |
if (ar->dimen_type[i] == DIMEN_RANGE) |
| 3728 |
{ |
| 3729 |
freeze_expr (&ar->start[i]); |
| 3730 |
freeze_expr (&ar->end[i]); |
| 3731 |
freeze_expr (&ar->stride[i]); |
| 3732 |
} |
| 3733 |
else if (ar->dimen_type[i] == DIMEN_ELEMENT) |
| 3734 |
{ |
| 3735 |
freeze_expr (&ar->start[i]); |
| 3736 |
} |
| 3737 |
} |
| 3738 |
break; |
| 3739 |
|
| 3740 |
case AR_ELEMENT: |
| 3741 |
for (i=0; i<ar->dimen; i++) |
| 3742 |
freeze_expr (&ar->start[i]); |
| 3743 |
break; |
| 3744 |
|
| 3745 |
default: |
| 3746 |
break; |
| 3747 |
} |
| 3748 |
} |
| 3749 |
} |
| 3750 |
} |
| 3751 |
|
| 3752 |
/* Convert to gfc_index_integer_kind if needed, just do a copy otherwise. */ |
| 3753 |
|
| 3754 |
static gfc_expr * |
| 3755 |
convert_to_index_kind (gfc_expr *e) |
| 3756 |
{ |
| 3757 |
gfc_expr *res; |
| 3758 |
|
| 3759 |
gcc_assert (e != NULL); |
| 3760 |
|
| 3761 |
res = gfc_copy_expr (e); |
| 3762 |
|
| 3763 |
gcc_assert (e->ts.type == BT_INTEGER); |
| 3764 |
|
| 3765 |
if (res->ts.kind != gfc_index_integer_kind) |
| 3766 |
{ |
| 3767 |
gfc_typespec ts; |
| 3768 |
gfc_clear_ts (&ts); |
| 3769 |
ts.type = BT_INTEGER; |
| 3770 |
ts.kind = gfc_index_integer_kind; |
| 3771 |
|
| 3772 |
gfc_convert_type_warn (e, &ts, 2, 0); |
| 3773 |
} |
| 3774 |
|
| 3775 |
return res; |
| 3776 |
} |
| 3777 |
|
| 3778 |
/* Function to create a DO loop including creation of the |
| 3779 |
iteration variable. gfc_expr are copied.*/ |
| 3780 |
|
| 3781 |
static gfc_code * |
| 3782 |
create_do_loop (gfc_expr *start, gfc_expr *end, gfc_expr *step, locus *where, |
| 3783 |
gfc_namespace *ns, char *vname) |
| 3784 |
{ |
| 3785 |
|
| 3786 |
char name[GFC_MAX_SYMBOL_LEN +1]; |
| 3787 |
gfc_symtree *symtree; |
| 3788 |
gfc_symbol *symbol; |
| 3789 |
gfc_expr *i; |
| 3790 |
gfc_code *n, *n2; |
| 3791 |
|
| 3792 |
/* Create an expression for the iteration variable. */ |
| 3793 |
if (vname) |
| 3794 |
sprintf (name, "__var_%d_do_%s", var_num++, vname); |
| 3795 |
else |
| 3796 |
sprintf (name, "__var_%d_do", var_num++); |
| 3797 |
|
| 3798 |
|
| 3799 |
if (gfc_get_sym_tree (name, ns, &symtree, false) != 0) |
| 3800 |
gcc_unreachable (); |
| 3801 |
|
| 3802 |
/* Create the loop variable. */ |
| 3803 |
|
| 3804 |
symbol = symtree->n.sym; |
| 3805 |
symbol->ts.type = BT_INTEGER; |
| 3806 |
symbol->ts.kind = gfc_index_integer_kind; |
| 3807 |
symbol->attr.flavor = FL_VARIABLE; |
| 3808 |
symbol->attr.referenced = 1; |
| 3809 |
symbol->attr.dimension = 0; |
| 3810 |
symbol->attr.fe_temp = 1; |
| 3811 |
symbol->attr.automatic = 1; |
| 3812 |
gfc_commit_symbol (symbol); |
| 3813 |
|
| 3814 |
i = gfc_get_expr (); |
| 3815 |
i->expr_type = EXPR_VARIABLE; |
| 3816 |
i->ts = symbol->ts; |
| 3817 |
i->rank = 0; |
| 3818 |
i->where = *where; |
| 3819 |
i->symtree = symtree; |
| 3820 |
|
| 3821 |
/* ... and the nested DO statements. */ |
| 3822 |
n = XCNEW (gfc_code); |
| 3823 |
n->op = EXEC_DO; |
| 3824 |
n->loc = *where; |
| 3825 |
n->ext.iterator = gfc_get_iterator (); |
| 3826 |
n->ext.iterator->var = i; |
| 3827 |
n->ext.iterator->start = convert_to_index_kind (start); |
| 3828 |
n->ext.iterator->end = convert_to_index_kind (end); |
| 3829 |
if (step) |
| 3830 |
n->ext.iterator->step = convert_to_index_kind (step); |
| 3831 |
else |
| 3832 |
n->ext.iterator->step = gfc_get_int_expr (gfc_index_integer_kind, |
| 3833 |
where, 1); |
| 3834 |
|
| 3835 |
n2 = XCNEW (gfc_code); |
| 3836 |
n2->op = EXEC_DO; |
| 3837 |
n2->loc = *where; |
| 3838 |
n2->next = NULL; |
| 3839 |
n->block = n2; |
| 3840 |
return n; |
| 3841 |
} |
| 3842 |
|
| 3843 |
/* Get the upper bound of the DO loops for matmul along a dimension. This |
| 3844 |
is one-based. */ |
| 3845 |
|
| 3846 |
static gfc_expr* |
| 3847 |
get_size_m1 (gfc_expr *e, int dimen) |
| 3848 |
{ |
| 3849 |
mpz_t size; |
| 3850 |
gfc_expr *res; |
| 3851 |
|
| 3852 |
if (gfc_array_dimen_size (e, dimen - 1, &size)) |
| 3853 |
{ |
| 3854 |
res = gfc_get_constant_expr (BT_INTEGER, |
| 3855 |
gfc_index_integer_kind, &e->where); |
| 3856 |
mpz_sub_ui (res->value.integer, size, 1); |
| 3857 |
mpz_clear (size); |
| 3858 |
} |
| 3859 |
else |
| 3860 |
{ |
| 3861 |
res = get_operand (INTRINSIC_MINUS, |
| 3862 |
get_array_inq_function (GFC_ISYM_SIZE, e, dimen), |
| 3863 |
gfc_get_int_expr (gfc_index_integer_kind, |
| 3864 |
&e->where, 1)); |
| 3865 |
gfc_simplify_expr (res, 0); |
| 3866 |
} |
| 3867 |
|
| 3868 |
return res; |
| 3869 |
} |
| 3870 |
|
| 3871 |
/* Function to return a scalarized expression. It is assumed that indices are |
| 3872 |
zero based to make generation of DO loops easier. A zero as index will |
| 3873 |
access the first element along a dimension. Single element references will |
| 3874 |
be skipped. A NULL as an expression will be replaced by a full reference. |
| 3875 |
This assumes that the index loops have gfc_index_integer_kind, and that all |
| 3876 |
references have been frozen. */ |
| 3877 |
|
| 3878 |
static gfc_expr* |
| 3879 |
scalarized_expr (gfc_expr *e_in, gfc_expr **index, int count_index) |
| 3880 |
{ |
| 3881 |
gfc_array_ref *ar; |
| 3882 |
int i; |
| 3883 |
int rank; |
| 3884 |
gfc_expr *e; |
| 3885 |
int i_index; |
| 3886 |
bool was_fullref; |
| 3887 |
|
| 3888 |
e = gfc_copy_expr(e_in); |
| 3889 |
|
| 3890 |
rank = e->rank; |
| 3891 |
|
| 3892 |
ar = gfc_find_array_ref (e); |
| 3893 |
|
| 3894 |
/* We scalarize count_index variables, reducing the rank by count_index. */ |
| 3895 |
|
| 3896 |
e->rank = rank - count_index; |
| 3897 |
|
| 3898 |
was_fullref = ar->type == AR_FULL; |
| 3899 |
|
| 3900 |
if (e->rank == 0) |
| 3901 |
ar->type = AR_ELEMENT; |
| 3902 |
else |
| 3903 |
ar->type = AR_SECTION; |
| 3904 |
|
| 3905 |
/* Loop over the indices. For each index, create the expression |
| 3906 |
index * stride + lbound(e, dim). */ |
| 3907 |
|
| 3908 |
i_index = 0; |
| 3909 |
for (i=0; i < ar->dimen; i++) |
| 3910 |
{ |
| 3911 |
if (was_fullref || ar->dimen_type[i] == DIMEN_RANGE) |
| 3912 |
{ |
| 3913 |
if (index[i_index] != NULL) |
| 3914 |
{ |
| 3915 |
gfc_expr *lbound, *nindex; |
| 3916 |
gfc_expr *loopvar; |
| 3917 |
|
| 3918 |
loopvar = gfc_copy_expr (index[i_index]); |
| 3919 |
|
| 3920 |
if (ar->stride[i]) |
| 3921 |
{ |
| 3922 |
gfc_expr *tmp; |
| 3923 |
|
| 3924 |
tmp = gfc_copy_expr(ar->stride[i]); |
| 3925 |
if (tmp->ts.kind != gfc_index_integer_kind) |
| 3926 |
{ |
| 3927 |
gfc_typespec ts; |
| 3928 |
gfc_clear_ts (&ts); |
| 3929 |
ts.type = BT_INTEGER; |
| 3930 |
ts.kind = gfc_index_integer_kind; |
| 3931 |
gfc_convert_type (tmp, &ts, 2); |
| 3932 |
} |
| 3933 |
nindex = get_operand (INTRINSIC_TIMES, loopvar, tmp); |
| 3934 |
} |
| 3935 |
else |
| 3936 |
nindex = loopvar; |
| 3937 |
|
| 3938 |
/* Calculate the lower bound of the expression. */ |
| 3939 |
if (ar->start[i]) |
| 3940 |
{ |
| 3941 |
lbound = gfc_copy_expr (ar->start[i]); |
| 3942 |
if (lbound->ts.kind != gfc_index_integer_kind) |
| 3943 |
{ |
| 3944 |
gfc_typespec ts; |
| 3945 |
gfc_clear_ts (&ts); |
| 3946 |
ts.type = BT_INTEGER; |
| 3947 |
ts.kind = gfc_index_integer_kind; |
| 3948 |
gfc_convert_type (lbound, &ts, 2); |
| 3949 |
|
| 3950 |
} |
| 3951 |
} |
| 3952 |
else |
| 3953 |
{ |
| 3954 |
gfc_expr *lbound_e; |
| 3955 |
gfc_ref *ref; |
| 3956 |
|
| 3957 |
lbound_e = gfc_copy_expr (e_in); |
| 3958 |
|
| 3959 |
for (ref = lbound_e->ref; ref; ref = ref->next) |
| 3960 |
if (ref->type == REF_ARRAY |
| 3961 |
&& (ref->u.ar.type == AR_FULL |
| 3962 |
|| ref->u.ar.type == AR_SECTION)) |
| 3963 |
break; |
| 3964 |
|
| 3965 |
if (ref->next) |
| 3966 |
{ |
| 3967 |
gfc_free_ref_list (ref->next); |
| 3968 |
ref->next = NULL; |
| 3969 |
} |
| 3970 |
|
| 3971 |
if (!was_fullref) |
| 3972 |
{ |
| 3973 |
/* Look at full individual sections, like a(:). The first index |
| 3974 |
is the lbound of a full ref. */ |
| 3975 |
int j; |
| 3976 |
gfc_array_ref *ar; |
| 3977 |
int to; |
| 3978 |
|
| 3979 |
ar = &ref->u.ar; |
| 3980 |
|
| 3981 |
/* For assumed size, we need to keep around the final |
| 3982 |
reference in order not to get an error on resolution |
| 3983 |
below, and we cannot use AR_FULL. */ |
| 3984 |
|
| 3985 |
if (ar->as->type == AS_ASSUMED_SIZE) |
| 3986 |
{ |
| 3987 |
ar->type = AR_SECTION; |
| 3988 |
to = ar->dimen - 1; |
| 3989 |
} |
| 3990 |
else |
| 3991 |
{ |
| 3992 |
to = ar->dimen; |
| 3993 |
ar->type = AR_FULL; |
| 3994 |
} |
| 3995 |
|
| 3996 |
for (j = 0; j < to; j++) |
| 3997 |
{ |
| 3998 |
gfc_free_expr (ar->start[j]); |
| 3999 |
ar->start[j] = NULL; |
| 4000 |
gfc_free_expr (ar->end[j]); |
| 4001 |
ar->end[j] = NULL; |
| 4002 |
gfc_free_expr (ar->stride[j]); |
| 4003 |
ar->stride[j] = NULL; |
| 4004 |
} |
| 4005 |
|
| 4006 |
/* We have to get rid of the shape, if there is one. Do |
| 4007 |
so by freeing it and calling gfc_resolve to rebuild |
| 4008 |
it, if necessary. */ |
| 4009 |
|
| 4010 |
if (lbound_e->shape) |
| 4011 |
gfc_free_shape (&(lbound_e->shape), lbound_e->rank); |
| 4012 |
|
| 4013 |
lbound_e->rank = ar->dimen; |
| 4014 |
gfc_resolve_expr (lbound_e); |
| 4015 |
} |
| 4016 |
lbound = get_array_inq_function (GFC_ISYM_LBOUND, lbound_e, |
| 4017 |
i + 1); |
| 4018 |
gfc_free_expr (lbound_e); |
| 4019 |
} |
| 4020 |
|
| 4021 |
ar->dimen_type[i] = DIMEN_ELEMENT; |
| 4022 |
|
| 4023 |
gfc_free_expr (ar->start[i]); |
| 4024 |
ar->start[i] = get_operand (INTRINSIC_PLUS, nindex, lbound); |
| 4025 |
|
| 4026 |
gfc_free_expr (ar->end[i]); |
| 4027 |
ar->end[i] = NULL; |
| 4028 |
gfc_free_expr (ar->stride[i]); |
| 4029 |
ar->stride[i] = NULL; |
| 4030 |
gfc_simplify_expr (ar->start[i], 0); |
| 4031 |
} |
| 4032 |
else if (was_fullref) |
| 4033 |
{ |
| 4034 |
gfc_internal_error ("Scalarization using DIMEN_RANGE unimplemented"); |
| 4035 |
} |
| 4036 |
i_index ++; |
| 4037 |
} |
| 4038 |
} |
| 4039 |
|
| 4040 |
/* Bounds checking will be done before the loops if -fcheck=bounds |
| 4041 |
is in effect. */ |
| 4042 |
e->no_bounds_check = 1; |
| 4043 |
return e; |
| 4044 |
} |
| 4045 |
|
| 4046 |
/* Helper function to check for a dimen vector as subscript. */ |
| 4047 |
|
| 4048 |
bool |
| 4049 |
gfc_has_dimen_vector_ref (gfc_expr *e) |
| 4050 |
{ |
| 4051 |
gfc_array_ref *ar; |
| 4052 |
int i; |
| 4053 |
|
| 4054 |
ar = gfc_find_array_ref (e); |
| 4055 |
gcc_assert (ar); |
| 4056 |
if (ar->type == AR_FULL) |
| 4057 |
return false; |
| 4058 |
|
| 4059 |
for (i=0; i<ar->dimen; i++) |
| 4060 |
if (ar->dimen_type[i] == DIMEN_VECTOR) |
| 4061 |
return true; |
| 4062 |
|
| 4063 |
return false; |
| 4064 |
} |
| 4065 |
|
| 4066 |
/* If handed an expression of the form |
| 4067 |
|
| 4068 |
TRANSPOSE(CONJG(A)) |
| 4069 |
|
| 4070 |
check if A can be handled by matmul and return if there is an uneven number |
| 4071 |
of CONJG calls. Return a pointer to the array when everything is OK, NULL |
| 4072 |
otherwise. The caller has to check for the correct rank. */ |
| 4073 |
|
| 4074 |
static gfc_expr* |
| 4075 |
check_conjg_transpose_variable (gfc_expr *e, bool *conjg, bool *transpose) |
| 4076 |
{ |
| 4077 |
*conjg = false; |
| 4078 |
*transpose = false; |
| 4079 |
|
| 4080 |
do |
| 4081 |
{ |
| 4082 |
if (e->expr_type == EXPR_VARIABLE) |
| 4083 |
{ |
| 4084 |
gcc_assert (e->rank == 1 || e->rank == 2); |
| 4085 |
return e; |
| 4086 |
} |
| 4087 |
else if (e->expr_type == EXPR_FUNCTION) |
| 4088 |
{ |
| 4089 |
if (e->value.function.isym == NULL) |
| 4090 |
return NULL; |
| 4091 |
|
| 4092 |
if (e->value.function.isym->id == GFC_ISYM_CONJG) |
| 4093 |
*conjg = !*conjg; |
| 4094 |
else if (e->value.function.isym->id == GFC_ISYM_TRANSPOSE) |
| 4095 |
*transpose = !*transpose; |
| 4096 |
else return NULL; |
| 4097 |
} |
| 4098 |
else |
| 4099 |
return NULL; |
| 4100 |
|
| 4101 |
e = e->value.function.actual->expr; |
| 4102 |
} |
| 4103 |
while(1); |
| 4104 |
|
| 4105 |
return NULL; |
| 4106 |
} |
| 4107 |
|
| 4108 |
/* Macros for unified error messages. */ |
| 4109 |
|
| 4110 |
#define B_ERROR_1 _("Incorrect extent in argument B in MATMUL intrinsic in " \ |
| 4111 |
"dimension 1: is %ld, should be %ld") |
| 4112 |
|
| 4113 |
#define C_ERROR_1 _("Array bound mismatch for dimension 1 of array " \ |
| 4114 |
"(%ld/%ld)") |
| 4115 |
|
| 4116 |
#define C_ERROR_2 _("Array bound mismatch for dimension 2 of array " \ |
| 4117 |
"(%ld/%ld)") |
| 4118 |
|
| 4119 |
|
| 4120 |
/* Inline assignments of the form c = matmul(a,b). |
| 4121 |
Handle only the cases currently where b and c are rank-two arrays. |
| 4122 |
|
| 4123 |
This basically translates the code to |
| 4124 |
|
| 4125 |
BLOCK |
| 4126 |
integer i,j,k |
| 4127 |
c = 0 |
| 4128 |
do j=0, size(b,2)-1 |
| 4129 |
do k=0, size(a, 2)-1 |
| 4130 |
do i=0, size(a, 1)-1 |
| 4131 |
c(i * stride(c,1) + lbound(c,1), j * stride(c,2) + lbound(c,2)) = |
| 4132 |
c(i * stride(c,1) + lbound(c,1), j * stride(c,2) + lbound(c,2)) + |
| 4133 |
a(i * stride(a,1) + lbound(a,1), k * stride(a,2) + lbound(a,2)) * |
| 4134 |
b(k * stride(b,1) + lbound(b,1), j * stride(b,2) + lbound(b,2)) |
| 4135 |
end do |
| 4136 |
end do |
| 4137 |
end do |
| 4138 |
END BLOCK |
| 4139 |
|
| 4140 |
*/ |
| 4141 |
|
| 4142 |
static int |
| 4143 |
inline_matmul_assign (gfc_code **c, int *walk_subtrees, |
| 4144 |
void *data ATTRIBUTE_UNUSED) |
| 4145 |
{ |
| 4146 |
gfc_code *co = *c; |
| 4147 |
gfc_expr *expr1, *expr2; |
| 4148 |
gfc_expr *matrix_a, *matrix_b; |
| 4149 |
gfc_actual_arglist *a, *b; |
| 4150 |
gfc_code *do_1, *do_2, *do_3, *assign_zero, *assign_matmul; |
| 4151 |
gfc_expr *zero_e; |
| 4152 |
gfc_expr *u1, *u2, *u3; |
| 4153 |
gfc_expr *list[2]; |
| 4154 |
gfc_expr *ascalar, *bscalar, *cscalar; |
| 4155 |
gfc_expr *mult; |
| 4156 |
gfc_expr *var_1, *var_2, *var_3; |
| 4157 |
gfc_expr *zero; |
| 4158 |
gfc_namespace *ns; |
| 4159 |
gfc_intrinsic_op op_times, op_plus; |
| 4160 |
enum matrix_case m_case; |
| 4161 |
int i; |
| 4162 |
gfc_code *if_limit = NULL; |
| 4163 |
gfc_code **next_code_point; |
| 4164 |
bool conjg_a, conjg_b, transpose_a, transpose_b; |
| 4165 |
bool realloc_c; |
| 4166 |
|
| 4167 |
if (co->op != EXEC_ASSIGN) |
| 4168 |
return 0; |
| 4169 |
|
| 4170 |
if (in_where || in_assoc_list) |
| 4171 |
return 0; |
| 4172 |
|
| 4173 |
/* The BLOCKS generated for the temporary variables and FORALL don't |
| 4174 |
mix. */ |
| 4175 |
if (forall_level > 0) |
| 4176 |
return 0; |
| 4177 |
|
| 4178 |
/* For now don't do anything in OpenMP workshare, it confuses |
| 4179 |
its translation, which expects only the allowed statements in there. |
| 4180 |
We should figure out how to parallelize this eventually. */ |
| 4181 |
if (in_omp_workshare || in_omp_atomic) |
| 4182 |
return 0; |
| 4183 |
|
| 4184 |
expr1 = co->expr1; |
| 4185 |
expr2 = co->expr2; |
| 4186 |
if (expr2->expr_type != EXPR_FUNCTION |
| 4187 |
|| expr2->value.function.isym == NULL |
| 4188 |
|| expr2->value.function.isym->id != GFC_ISYM_MATMUL) |
| 4189 |
return 0; |
| 4190 |
|
| 4191 |
current_code = c; |
| 4192 |
inserted_block = NULL; |
| 4193 |
changed_statement = NULL; |
| 4194 |
|
| 4195 |
a = expr2->value.function.actual; |
| 4196 |
matrix_a = check_conjg_transpose_variable (a->expr, &conjg_a, &transpose_a); |
| 4197 |
if (matrix_a == NULL) |
| 4198 |
return 0; |
| 4199 |
|
| 4200 |
b = a->next; |
| 4201 |
matrix_b = check_conjg_transpose_variable (b->expr, &conjg_b, &transpose_b); |
| 4202 |
if (matrix_b == NULL) |
| 4203 |
return 0; |
| 4204 |
|
| 4205 |
if (gfc_has_dimen_vector_ref (expr1) || gfc_has_dimen_vector_ref (matrix_a) |
| 4206 |
|| gfc_has_dimen_vector_ref (matrix_b)) |
| 4207 |
return 0; |
| 4208 |
|
| 4209 |
/* We do not handle data dependencies yet. */ |
| 4210 |
if (gfc_check_dependency (expr1, matrix_a, true) |
| 4211 |
|| gfc_check_dependency (expr1, matrix_b, true)) |
| 4212 |
return 0; |
| 4213 |
|
| 4214 |
m_case = none; |
| 4215 |
if (matrix_a->rank == 2) |
| 4216 |
{ |
| 4217 |
if (transpose_a) |
| 4218 |
{ |
| 4219 |
if (matrix_b->rank == 2 && !transpose_b) |
| 4220 |
m_case = A2TB2; |
| 4221 |
} |
| 4222 |
else |
| 4223 |
{ |
| 4224 |
if (matrix_b->rank == 1) |
| 4225 |
m_case = A2B1; |
| 4226 |
else /* matrix_b->rank == 2 */ |
| 4227 |
{ |
| 4228 |
if (transpose_b) |
| 4229 |
m_case = A2B2T; |
| 4230 |
else |
| 4231 |
m_case = A2B2; |
| 4232 |
} |
| 4233 |
} |
| 4234 |
} |
| 4235 |
else /* matrix_a->rank == 1 */ |
| 4236 |
{ |
| 4237 |
if (matrix_b->rank == 2) |
| 4238 |
{ |
| 4239 |
if (!transpose_b) |
| 4240 |
m_case = A1B2; |
| 4241 |
} |
| 4242 |
} |
| 4243 |
|
| 4244 |
if (m_case == none) |
| 4245 |
return 0; |
| 4246 |
|
| 4247 |
/* We only handle assignment to numeric or logical variables. */ |
| 4248 |
switch(expr1->ts.type) |
| 4249 |
{ |
| 4250 |
case BT_INTEGER: |
| 4251 |
case BT_LOGICAL: |
| 4252 |
case BT_REAL: |
| 4253 |
case BT_COMPLEX: |
| 4254 |
break; |
| 4255 |
|
| 4256 |
default: |
| 4257 |
return 0; |
| 4258 |
} |
| 4259 |
|
| 4260 |
ns = insert_block (); |
| 4261 |
|
| 4262 |
/* Assign the type of the zero expression for initializing the resulting |
| 4263 |
array, and the expression (+ and * for real, integer and complex; |
| 4264 |
.and. and .or for logical. */ |
| 4265 |
|
| 4266 |
switch(expr1->ts.type) |
| 4267 |
{ |
| 4268 |
case BT_INTEGER: |
| 4269 |
zero_e = gfc_get_int_expr (expr1->ts.kind, &expr1->where, 0); |
| 4270 |
op_times = INTRINSIC_TIMES; |
| 4271 |
op_plus = INTRINSIC_PLUS; |
| 4272 |
break; |
| 4273 |
|
| 4274 |
case BT_LOGICAL: |
| 4275 |
op_times = INTRINSIC_AND; |
| 4276 |
op_plus = INTRINSIC_OR; |
| 4277 |
zero_e = gfc_get_logical_expr (expr1->ts.kind, &expr1->where, |
| 4278 |
0); |
| 4279 |
break; |
| 4280 |
case BT_REAL: |
| 4281 |
zero_e = gfc_get_constant_expr (BT_REAL, expr1->ts.kind, |
| 4282 |
&expr1->where); |
| 4283 |
mpfr_set_si (zero_e->value.real, 0, GFC_RND_MODE); |
| 4284 |
op_times = INTRINSIC_TIMES; |
| 4285 |
op_plus = INTRINSIC_PLUS; |
| 4286 |
break; |
| 4287 |
|
| 4288 |
case BT_COMPLEX: |
| 4289 |
zero_e = gfc_get_constant_expr (BT_COMPLEX, expr1->ts.kind, |
| 4290 |
&expr1->where); |
| 4291 |
mpc_set_si_si (zero_e->value.complex, 0, 0, GFC_RND_MODE); |
| 4292 |
op_times = INTRINSIC_TIMES; |
| 4293 |
op_plus = INTRINSIC_PLUS; |
| 4294 |
|
| 4295 |
break; |
| 4296 |
|
| 4297 |
default: |
| 4298 |
gcc_unreachable(); |
| 4299 |
} |
| 4300 |
|
| 4301 |
current_code = &ns->code; |
| 4302 |
|
| 4303 |
/* Freeze the references, keeping track of how many temporary variables were |
| 4304 |
created. */ |
| 4305 |
n_vars = 0; |
| 4306 |
freeze_references (matrix_a); |
| 4307 |
freeze_references (matrix_b); |
| 4308 |
freeze_references (expr1); |
| 4309 |
|
| 4310 |
if (n_vars == 0) |
| 4311 |
next_code_point = current_code; |
| 4312 |
else |
| 4313 |
{ |
| 4314 |
next_code_point = &ns->code; |
| 4315 |
for (i=0; i<n_vars; i++) |
| 4316 |
next_code_point = &(*next_code_point)->next; |
| 4317 |
} |
| 4318 |
|
| 4319 |
/* Take care of the inline flag. If the limit check evaluates to a |
| 4320 |
constant, dead code elimination will eliminate the unneeded branch. */ |
| 4321 |
|
| 4322 |
if (flag_inline_matmul_limit > 0 |
| 4323 |
&& (matrix_a->rank == 1 || matrix_a->rank == 2) |
| 4324 |
&& matrix_b->rank == 2) |
| 4325 |
{ |
| 4326 |
if_limit = inline_limit_check (matrix_a, matrix_b, |
| 4327 |
flag_inline_matmul_limit, |
| 4328 |
matrix_a->rank); |
| 4329 |
|
| 4330 |
/* Insert the original statement into the else branch. */ |
| 4331 |
if_limit->block->block->next = co; |
| 4332 |
co->next = NULL; |
| 4333 |
|
| 4334 |
/* ... and the new ones go into the original one. */ |
| 4335 |
*next_code_point = if_limit; |
| 4336 |
next_code_point = &if_limit->block->next; |
| 4337 |
} |
| 4338 |
|
| 4339 |
zero_e->no_bounds_check = 1; |
| 4340 |
|
| 4341 |
assign_zero = XCNEW (gfc_code); |
| 4342 |
assign_zero->op = EXEC_ASSIGN; |
| 4343 |
assign_zero->loc = co->loc; |
| 4344 |
assign_zero->expr1 = gfc_copy_expr (expr1); |
| 4345 |
assign_zero->expr1->no_bounds_check = 1; |
| 4346 |
assign_zero->expr2 = zero_e; |
| 4347 |
|
| 4348 |
realloc_c = flag_realloc_lhs && gfc_is_reallocatable_lhs (expr1); |
| 4349 |
|
| 4350 |
if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) |
| 4351 |
{ |
| 4352 |
gfc_code *test; |
| 4353 |
gfc_expr *a2, *b1, *c1, *c2, *a1, *b2; |
| 4354 |
|
| 4355 |
switch (m_case) |
| 4356 |
{ |
| 4357 |
case A2B1: |
| 4358 |
|
| 4359 |
b1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 1); |
| 4360 |
a2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 2); |
| 4361 |
test = runtime_error_ne (b1, a2, B_ERROR_1); |
| 4362 |
*next_code_point = test; |
| 4363 |
next_code_point = &test->next; |
| 4364 |
|
| 4365 |
if (!realloc_c) |
| 4366 |
{ |
| 4367 |
c1 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 1); |
| 4368 |
a1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 1); |
| 4369 |
test = runtime_error_ne (c1, a1, C_ERROR_1); |
| 4370 |
*next_code_point = test; |
| 4371 |
next_code_point = &test->next; |
| 4372 |
} |
| 4373 |
break; |
| 4374 |
|
| 4375 |
case A1B2: |
| 4376 |
|
| 4377 |
b1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 1); |
| 4378 |
a1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 1); |
| 4379 |
test = runtime_error_ne (b1, a1, B_ERROR_1); |
| 4380 |
*next_code_point = test; |
| 4381 |
next_code_point = &test->next; |
| 4382 |
|
| 4383 |
if (!realloc_c) |
| 4384 |
{ |
| 4385 |
c1 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 1); |
| 4386 |
b2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 2); |
| 4387 |
test = runtime_error_ne (c1, b2, C_ERROR_1); |
| 4388 |
*next_code_point = test; |
| 4389 |
next_code_point = &test->next; |
| 4390 |
} |
| 4391 |
break; |
| 4392 |
|
| 4393 |
case A2B2: |
| 4394 |
|
| 4395 |
b1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 1); |
| 4396 |
a2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 2); |
| 4397 |
test = runtime_error_ne (b1, a2, B_ERROR_1); |
| 4398 |
*next_code_point = test; |
| 4399 |
next_code_point = &test->next; |
| 4400 |
|
| 4401 |
if (!realloc_c) |
| 4402 |
{ |
| 4403 |
c1 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 1); |
| 4404 |
a1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 1); |
| 4405 |
test = runtime_error_ne (c1, a1, C_ERROR_1); |
| 4406 |
*next_code_point = test; |
| 4407 |
next_code_point = &test->next; |
| 4408 |
|
| 4409 |
c2 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 2); |
| 4410 |
b2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 2); |
| 4411 |
test = runtime_error_ne (c2, b2, C_ERROR_2); |
| 4412 |
*next_code_point = test; |
| 4413 |
next_code_point = &test->next; |
| 4414 |
} |
| 4415 |
break; |
| 4416 |
|
| 4417 |
case A2B2T: |
| 4418 |
|
| 4419 |
b2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 2); |
| 4420 |
a2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 2); |
| 4421 |
/* matrix_b is transposed, hence dimension 1 for the error message. */ |
| 4422 |
test = runtime_error_ne (b2, a2, B_ERROR_1); |
| 4423 |
*next_code_point = test; |
| 4424 |
next_code_point = &test->next; |
| 4425 |
|
| 4426 |
if (!realloc_c) |
| 4427 |
{ |
| 4428 |
c1 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 1); |
| 4429 |
a1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 1); |
| 4430 |
test = runtime_error_ne (c1, a1, C_ERROR_1); |
| 4431 |
*next_code_point = test; |
| 4432 |
next_code_point = &test->next; |
| 4433 |
|
| 4434 |
c2 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 2); |
| 4435 |
b1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 1); |
| 4436 |
test = runtime_error_ne (c2, b1, C_ERROR_2); |
| 4437 |
*next_code_point = test; |
| 4438 |
next_code_point = &test->next; |
| 4439 |
} |
| 4440 |
break; |
| 4441 |
|
| 4442 |
case A2TB2: |
| 4443 |
|
| 4444 |
b1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 1); |
| 4445 |
a1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 1); |
| 4446 |
test = runtime_error_ne (b1, a1, B_ERROR_1); |
| 4447 |
*next_code_point = test; |
| 4448 |
next_code_point = &test->next; |
| 4449 |
|
| 4450 |
if (!realloc_c) |
| 4451 |
{ |
| 4452 |
c1 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 1); |
| 4453 |
a2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 2); |
| 4454 |
test = runtime_error_ne (c1, a2, C_ERROR_1); |
| 4455 |
*next_code_point = test; |
| 4456 |
next_code_point = &test->next; |
| 4457 |
|
| 4458 |
c2 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 2); |
| 4459 |
b2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 2); |
| 4460 |
test = runtime_error_ne (c2, b2, C_ERROR_2); |
| 4461 |
*next_code_point = test; |
| 4462 |
next_code_point = &test->next; |
| 4463 |
} |
| 4464 |
break; |
| 4465 |
|
| 4466 |
default: |
| 4467 |
gcc_unreachable (); |
| 4468 |
} |
| 4469 |
} |
| 4470 |
|
| 4471 |
/* Handle the reallocation, if needed. */ |
| 4472 |
|
| 4473 |
if (realloc_c) |
| 4474 |
{ |
| 4475 |
gfc_code *lhs_alloc; |
| 4476 |
|
| 4477 |
lhs_alloc = matmul_lhs_realloc (expr1, matrix_a, matrix_b, m_case); |
| 4478 |
|
| 4479 |
*next_code_point = lhs_alloc; |
| 4480 |
next_code_point = &lhs_alloc->next; |
| 4481 |
|
| 4482 |
} |
| 4483 |
|
| 4484 |
*next_code_point = assign_zero; |
| 4485 |
|
| 4486 |
zero = gfc_get_int_expr (gfc_index_integer_kind, &co->loc, 0); |
| 4487 |
|
| 4488 |
assign_matmul = XCNEW (gfc_code); |
| 4489 |
assign_matmul->op = EXEC_ASSIGN; |
| 4490 |
assign_matmul->loc = co->loc; |
| 4491 |
|
| 4492 |
/* Get the bounds for the loops, create them and create the scalarized |
| 4493 |
expressions. */ |
| 4494 |
|
| 4495 |
switch (m_case) |
| 4496 |
{ |
| 4497 |
case A2B2: |
| 4498 |
|
| 4499 |
u1 = get_size_m1 (matrix_b, 2); |
| 4500 |
u2 = get_size_m1 (matrix_a, 2); |
| 4501 |
u3 = get_size_m1 (matrix_a, 1); |
| 4502 |
|
| 4503 |
do_1 = create_do_loop (gfc_copy_expr (zero), u1, NULL, &co->loc, ns); |
| 4504 |
do_2 = create_do_loop (gfc_copy_expr (zero), u2, NULL, &co->loc, ns); |
| 4505 |
do_3 = create_do_loop (gfc_copy_expr (zero), u3, NULL, &co->loc, ns); |
| 4506 |
|
| 4507 |
do_1->block->next = do_2; |
| 4508 |
do_2->block->next = do_3; |
| 4509 |
do_3->block->next = assign_matmul; |
| 4510 |
|
| 4511 |
var_1 = do_1->ext.iterator->var; |
| 4512 |
var_2 = do_2->ext.iterator->var; |
| 4513 |
var_3 = do_3->ext.iterator->var; |
| 4514 |
|
| 4515 |
list[0] = var_3; |
| 4516 |
list[1] = var_1; |
| 4517 |
cscalar = scalarized_expr (co->expr1, list, 2); |
| 4518 |
|
| 4519 |
list[0] = var_3; |
| 4520 |
list[1] = var_2; |
| 4521 |
ascalar = scalarized_expr (matrix_a, list, 2); |
| 4522 |
|
| 4523 |
list[0] = var_2; |
| 4524 |
list[1] = var_1; |
| 4525 |
bscalar = scalarized_expr (matrix_b, list, 2); |
| 4526 |
|
| 4527 |
break; |
| 4528 |
|
| 4529 |
case A2B2T: |
| 4530 |
|
| 4531 |
u1 = get_size_m1 (matrix_b, 1); |
| 4532 |
u2 = get_size_m1 (matrix_a, 2); |
| 4533 |
u3 = get_size_m1 (matrix_a, 1); |
| 4534 |
|
| 4535 |
do_1 = create_do_loop (gfc_copy_expr (zero), u1, NULL, &co->loc, ns); |
| 4536 |
do_2 = create_do_loop (gfc_copy_expr (zero), u2, NULL, &co->loc, ns); |
| 4537 |
do_3 = create_do_loop (gfc_copy_expr (zero), u3, NULL, &co->loc, ns); |
| 4538 |
|
| 4539 |
do_1->block->next = do_2; |
| 4540 |
do_2->block->next = do_3; |
| 4541 |
do_3->block->next = assign_matmul; |
| 4542 |
|
| 4543 |
var_1 = do_1->ext.iterator->var; |
| 4544 |
var_2 = do_2->ext.iterator->var; |
| 4545 |
var_3 = do_3->ext.iterator->var; |
| 4546 |
|
| 4547 |
list[0] = var_3; |
| 4548 |
list[1] = var_1; |
| 4549 |
cscalar = scalarized_expr (co->expr1, list, 2); |
| 4550 |
|
| 4551 |
list[0] = var_3; |
| 4552 |
list[1] = var_2; |
| 4553 |
ascalar = scalarized_expr (matrix_a, list, 2); |
| 4554 |
|
| 4555 |
list[0] = var_1; |
| 4556 |
list[1] = var_2; |
| 4557 |
bscalar = scalarized_expr (matrix_b, list, 2); |
| 4558 |
|
| 4559 |
break; |
| 4560 |
|
| 4561 |
case A2TB2: |
| 4562 |
|
| 4563 |
u1 = get_size_m1 (matrix_a, 2); |
| 4564 |
u2 = get_size_m1 (matrix_b, 2); |
| 4565 |
u3 = get_size_m1 (matrix_a, 1); |
| 4566 |
|
| 4567 |
do_1 = create_do_loop (gfc_copy_expr (zero), u1, NULL, &co->loc, ns); |
| 4568 |
do_2 = create_do_loop (gfc_copy_expr (zero), u2, NULL, &co->loc, ns); |
| 4569 |
do_3 = create_do_loop (gfc_copy_expr (zero), u3, NULL, &co->loc, ns); |
| 4570 |
|
| 4571 |
do_1->block->next = do_2; |
| 4572 |
do_2->block->next = do_3; |
| 4573 |
do_3->block->next = assign_matmul; |
| 4574 |
|
| 4575 |
var_1 = do_1->ext.iterator->var; |
| 4576 |
var_2 = do_2->ext.iterator->var; |
| 4577 |
var_3 = do_3->ext.iterator->var; |
| 4578 |
|
| 4579 |
list[0] = var_1; |
| 4580 |
list[1] = var_2; |
| 4581 |
cscalar = scalarized_expr (co->expr1, list, 2); |
| 4582 |
|
| 4583 |
list[0] = var_3; |
| 4584 |
list[1] = var_1; |
| 4585 |
ascalar = scalarized_expr (matrix_a, list, 2); |
| 4586 |
|
| 4587 |
list[0] = var_3; |
| 4588 |
list[1] = var_2; |
| 4589 |
bscalar = scalarized_expr (matrix_b, list, 2); |
| 4590 |
|
| 4591 |
break; |
| 4592 |
|
| 4593 |
case A2B1: |
| 4594 |
u1 = get_size_m1 (matrix_b, 1); |
| 4595 |
u2 = get_size_m1 (matrix_a, 1); |
| 4596 |
|
| 4597 |
do_1 = create_do_loop (gfc_copy_expr (zero), u1, NULL, &co->loc, ns); |
| 4598 |
do_2 = create_do_loop (gfc_copy_expr (zero), u2, NULL, &co->loc, ns); |
| 4599 |
|
| 4600 |
do_1->block->next = do_2; |
| 4601 |
do_2->block->next = assign_matmul; |
| 4602 |
|
| 4603 |
var_1 = do_1->ext.iterator->var; |
| 4604 |
var_2 = do_2->ext.iterator->var; |
| 4605 |
|
| 4606 |
list[0] = var_2; |
| 4607 |
cscalar = scalarized_expr (co->expr1, list, 1); |
| 4608 |
|
| 4609 |
list[0] = var_2; |
| 4610 |
list[1] = var_1; |
| 4611 |
ascalar = scalarized_expr (matrix_a, list, 2); |
| 4612 |
|
| 4613 |
list[0] = var_1; |
| 4614 |
bscalar = scalarized_expr (matrix_b, list, 1); |
| 4615 |
|
| 4616 |
break; |
| 4617 |
|
| 4618 |
case A1B2: |
| 4619 |
u1 = get_size_m1 (matrix_b, 2); |
| 4620 |
u2 = get_size_m1 (matrix_a, 1); |
| 4621 |
|
| 4622 |
do_1 = create_do_loop (gfc_copy_expr (zero), u1, NULL, &co->loc, ns); |
| 4623 |
do_2 = create_do_loop (gfc_copy_expr (zero), u2, NULL, &co->loc, ns); |
| 4624 |
|
| 4625 |
do_1->block->next = do_2; |
| 4626 |
do_2->block->next = assign_matmul; |
| 4627 |
|
| 4628 |
var_1 = do_1->ext.iterator->var; |
| 4629 |
var_2 = do_2->ext.iterator->var; |
| 4630 |
|
| 4631 |
list[0] = var_1; |
| 4632 |
cscalar = scalarized_expr (co->expr1, list, 1); |
| 4633 |
|
| 4634 |
list[0] = var_2; |
| 4635 |
ascalar = scalarized_expr (matrix_a, list, 1); |
| 4636 |
|
| 4637 |
list[0] = var_2; |
| 4638 |
list[1] = var_1; |
| 4639 |
bscalar = scalarized_expr (matrix_b, list, 2); |
| 4640 |
|
| 4641 |
break; |
| 4642 |
|
| 4643 |
default: |
| 4644 |
gcc_unreachable(); |
| 4645 |
} |
| 4646 |
|
| 4647 |
/* Build the conjg call around the variables. Set the typespec manually |
| 4648 |
because gfc_build_intrinsic_call sometimes gets this wrong. */ |
| 4649 |
if (conjg_a) |
| 4650 |
{ |
| 4651 |
gfc_typespec ts; |
| 4652 |
ts = matrix_a->ts; |
| 4653 |
ascalar = gfc_build_intrinsic_call (ns, GFC_ISYM_CONJG, "conjg", |
| 4654 |
matrix_a->where, 1, ascalar); |
| 4655 |
ascalar->ts = ts; |
| 4656 |
} |
| 4657 |
|
| 4658 |
if (conjg_b) |
| 4659 |
{ |
| 4660 |
gfc_typespec ts; |
| 4661 |
ts = matrix_b->ts; |
| 4662 |
bscalar = gfc_build_intrinsic_call (ns, GFC_ISYM_CONJG, "conjg", |
| 4663 |
matrix_b->where, 1, bscalar); |
| 4664 |
bscalar->ts = ts; |
| 4665 |
} |
| 4666 |
/* First loop comes after the zero assignment. */ |
| 4667 |
assign_zero->next = do_1; |
| 4668 |
|
| 4669 |
/* Build the assignment expression in the loop. */ |
| 4670 |
assign_matmul->expr1 = gfc_copy_expr (cscalar); |
| 4671 |
|
| 4672 |
mult = get_operand (op_times, ascalar, bscalar); |
| 4673 |
assign_matmul->expr2 = get_operand (op_plus, cscalar, mult); |
| 4674 |
|
| 4675 |
/* If we don't want to keep the original statement around in |
| 4676 |
the else branch, we can free it. */ |
| 4677 |
|
| 4678 |
if (if_limit == NULL) |
| 4679 |
gfc_free_statements(co); |
| 4680 |
else |
| 4681 |
co->next = NULL; |
| 4682 |
|
| 4683 |
gfc_free_expr (zero); |
| 4684 |
*walk_subtrees = 0; |
| 4685 |
return 0; |
| 4686 |
} |
| 4687 |
|
| 4688 |
/* Change matmul function calls in the form of |
| 4689 |
|
| 4690 |
c = matmul(a,b) |
| 4691 |
|
| 4692 |
to the corresponding call to a BLAS routine, if applicable. */ |
| 4693 |
|
| 4694 |
static int |
| 4695 |
call_external_blas (gfc_code **c, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 4696 |
void *data ATTRIBUTE_UNUSED) |
| 4697 |
{ |
| 4698 |
gfc_code *co, *co_next; |
| 4699 |
gfc_expr *expr1, *expr2; |
| 4700 |
gfc_expr *matrix_a, *matrix_b; |
| 4701 |
gfc_code *if_limit = NULL; |
| 4702 |
gfc_actual_arglist *a, *b; |
| 4703 |
bool conjg_a, conjg_b, transpose_a, transpose_b; |
| 4704 |
gfc_code *call; |
| 4705 |
const char *blas_name; |
| 4706 |
const char *transa, *transb; |
| 4707 |
gfc_expr *c1, *c2, *b1; |
| 4708 |
gfc_actual_arglist *actual, *next; |
| 4709 |
bt type; |
| 4710 |
int kind; |
| 4711 |
enum matrix_case m_case; |
| 4712 |
bool realloc_c; |
| 4713 |
gfc_code **next_code_point; |
| 4714 |
|
| 4715 |
/* Many of the tests for inline matmul also apply here. */ |
| 4716 |
|
| 4717 |
co = *c; |
| 4718 |
|
| 4719 |
if (co->op != EXEC_ASSIGN) |
| 4720 |
return 0; |
| 4721 |
|
| 4722 |
if (in_where || in_assoc_list) |
| 4723 |
return 0; |
| 4724 |
|
| 4725 |
/* The BLOCKS generated for the temporary variables and FORALL don't |
| 4726 |
mix. */ |
| 4727 |
if (forall_level > 0) |
| 4728 |
return 0; |
| 4729 |
|
| 4730 |
/* For now don't do anything in OpenMP workshare, it confuses |
| 4731 |
its translation, which expects only the allowed statements in there. */ |
| 4732 |
|
| 4733 |
if (in_omp_workshare || in_omp_atomic) |
| 4734 |
return 0; |
| 4735 |
|
| 4736 |
expr1 = co->expr1; |
| 4737 |
expr2 = co->expr2; |
| 4738 |
if (expr2->expr_type != EXPR_FUNCTION |
| 4739 |
|| expr2->value.function.isym == NULL |
| 4740 |
|| expr2->value.function.isym->id != GFC_ISYM_MATMUL) |
| 4741 |
return 0; |
| 4742 |
|
| 4743 |
type = expr2->ts.type; |
| 4744 |
kind = expr2->ts.kind; |
| 4745 |
|
| 4746 |
/* Guard against recursion. */ |
| 4747 |
|
| 4748 |
if (expr2->external_blas) |
| 4749 |
return 0; |
| 4750 |
|
| 4751 |
if (type != expr1->ts.type || kind != expr1->ts.kind) |
| 4752 |
return 0; |
| 4753 |
|
| 4754 |
if (type == BT_REAL) |
| 4755 |
{ |
| 4756 |
if (kind == 4) |
| 4757 |
blas_name = "sgemm"; |
| 4758 |
else if (kind == 8) |
| 4759 |
blas_name = "dgemm"; |
| 4760 |
else |
| 4761 |
return 0; |
| 4762 |
} |
| 4763 |
else if (type == BT_COMPLEX) |
| 4764 |
{ |
| 4765 |
if (kind == 4) |
| 4766 |
blas_name = "cgemm"; |
| 4767 |
else if (kind == 8) |
| 4768 |
blas_name = "zgemm"; |
| 4769 |
else |
| 4770 |
return 0; |
| 4771 |
} |
| 4772 |
else |
| 4773 |
return 0; |
| 4774 |
|
| 4775 |
a = expr2->value.function.actual; |
| 4776 |
if (a->expr->rank != 2) |
| 4777 |
return 0; |
| 4778 |
|
| 4779 |
b = a->next; |
| 4780 |
if (b->expr->rank != 2) |
| 4781 |
return 0; |
| 4782 |
|
| 4783 |
matrix_a = check_conjg_transpose_variable (a->expr, &conjg_a, &transpose_a); |
| 4784 |
if (matrix_a == NULL) |
| 4785 |
return 0; |
| 4786 |
|
| 4787 |
if (transpose_a) |
| 4788 |
{ |
| 4789 |
if (conjg_a) |
| 4790 |
transa = "C"; |
| 4791 |
else |
| 4792 |
transa = "T"; |
| 4793 |
} |
| 4794 |
else |
| 4795 |
transa = "N"; |
| 4796 |
|
| 4797 |
matrix_b = check_conjg_transpose_variable (b->expr, &conjg_b, &transpose_b); |
| 4798 |
if (matrix_b == NULL) |
| 4799 |
return 0; |
| 4800 |
|
| 4801 |
if (transpose_b) |
| 4802 |
{ |
| 4803 |
if (conjg_b) |
| 4804 |
transb = "C"; |
| 4805 |
else |
| 4806 |
transb = "T"; |
| 4807 |
} |
| 4808 |
else |
| 4809 |
transb = "N"; |
| 4810 |
|
| 4811 |
if (transpose_a) |
| 4812 |
{ |
| 4813 |
if (transpose_b) |
| 4814 |
m_case = A2TB2T; |
| 4815 |
else |
| 4816 |
m_case = A2TB2; |
| 4817 |
} |
| 4818 |
else |
| 4819 |
{ |
| 4820 |
if (transpose_b) |
| 4821 |
m_case = A2B2T; |
| 4822 |
else |
| 4823 |
m_case = A2B2; |
| 4824 |
} |
| 4825 |
|
| 4826 |
current_code = c; |
| 4827 |
inserted_block = NULL; |
| 4828 |
changed_statement = NULL; |
| 4829 |
|
| 4830 |
expr2->external_blas = 1; |
| 4831 |
|
| 4832 |
/* We do not handle data dependencies yet. */ |
| 4833 |
if (gfc_check_dependency (expr1, matrix_a, true) |
| 4834 |
|| gfc_check_dependency (expr1, matrix_b, true)) |
| 4835 |
return 0; |
| 4836 |
|
| 4837 |
/* Generate the if statement and hang it into the tree. */ |
| 4838 |
if_limit = inline_limit_check (matrix_a, matrix_b, flag_blas_matmul_limit, 2); |
| 4839 |
co_next = co->next; |
| 4840 |
(*current_code) = if_limit; |
| 4841 |
co->next = NULL; |
| 4842 |
if_limit->block->next = co; |
| 4843 |
|
| 4844 |
call = XCNEW (gfc_code); |
| 4845 |
call->loc = co->loc; |
| 4846 |
|
| 4847 |
/* Bounds checking - a bit simpler than for inlining since we only |
| 4848 |
have to take care of two-dimensional arrays here. */ |
| 4849 |
|
| 4850 |
realloc_c = flag_realloc_lhs && gfc_is_reallocatable_lhs (expr1); |
| 4851 |
next_code_point = &(if_limit->block->block->next); |
| 4852 |
|
| 4853 |
if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) |
| 4854 |
{ |
| 4855 |
gfc_code *test; |
| 4856 |
// gfc_expr *a2, *b1, *c1, *c2, *a1, *b2; |
| 4857 |
gfc_expr *c1, *a1, *c2, *b2, *a2; |
| 4858 |
switch (m_case) |
| 4859 |
{ |
| 4860 |
case A2B2: |
| 4861 |
b1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 1); |
| 4862 |
a2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 2); |
| 4863 |
test = runtime_error_ne (b1, a2, B_ERROR_1); |
| 4864 |
*next_code_point = test; |
| 4865 |
next_code_point = &test->next; |
| 4866 |
|
| 4867 |
if (!realloc_c) |
| 4868 |
{ |
| 4869 |
c1 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 1); |
| 4870 |
a1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 1); |
| 4871 |
test = runtime_error_ne (c1, a1, C_ERROR_1); |
| 4872 |
*next_code_point = test; |
| 4873 |
next_code_point = &test->next; |
| 4874 |
|
| 4875 |
c2 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 2); |
| 4876 |
b2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 2); |
| 4877 |
test = runtime_error_ne (c2, b2, C_ERROR_2); |
| 4878 |
*next_code_point = test; |
| 4879 |
next_code_point = &test->next; |
| 4880 |
} |
| 4881 |
break; |
| 4882 |
|
| 4883 |
case A2B2T: |
| 4884 |
|
| 4885 |
b2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 2); |
| 4886 |
a2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 2); |
| 4887 |
/* matrix_b is transposed, hence dimension 1 for the error message. */ |
| 4888 |
test = runtime_error_ne (b2, a2, B_ERROR_1); |
| 4889 |
*next_code_point = test; |
| 4890 |
next_code_point = &test->next; |
| 4891 |
|
| 4892 |
if (!realloc_c) |
| 4893 |
{ |
| 4894 |
c1 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 1); |
| 4895 |
a1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 1); |
| 4896 |
test = runtime_error_ne (c1, a1, C_ERROR_1); |
| 4897 |
*next_code_point = test; |
| 4898 |
next_code_point = &test->next; |
| 4899 |
|
| 4900 |
c2 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 2); |
| 4901 |
b1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 1); |
| 4902 |
test = runtime_error_ne (c2, b1, C_ERROR_2); |
| 4903 |
*next_code_point = test; |
| 4904 |
next_code_point = &test->next; |
| 4905 |
} |
| 4906 |
break; |
| 4907 |
|
| 4908 |
case A2TB2: |
| 4909 |
|
| 4910 |
b1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 1); |
| 4911 |
a1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 1); |
| 4912 |
test = runtime_error_ne (b1, a1, B_ERROR_1); |
| 4913 |
*next_code_point = test; |
| 4914 |
next_code_point = &test->next; |
| 4915 |
|
| 4916 |
if (!realloc_c) |
| 4917 |
{ |
| 4918 |
c1 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 1); |
| 4919 |
a2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 2); |
| 4920 |
test = runtime_error_ne (c1, a2, C_ERROR_1); |
| 4921 |
*next_code_point = test; |
| 4922 |
next_code_point = &test->next; |
| 4923 |
|
| 4924 |
c2 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 2); |
| 4925 |
b2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 2); |
| 4926 |
test = runtime_error_ne (c2, b2, C_ERROR_2); |
| 4927 |
*next_code_point = test; |
| 4928 |
next_code_point = &test->next; |
| 4929 |
} |
| 4930 |
break; |
| 4931 |
|
| 4932 |
case A2TB2T: |
| 4933 |
b2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 2); |
| 4934 |
a1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 1); |
| 4935 |
test = runtime_error_ne (b2, a1, B_ERROR_1); |
| 4936 |
*next_code_point = test; |
| 4937 |
next_code_point = &test->next; |
| 4938 |
|
| 4939 |
if (!realloc_c) |
| 4940 |
{ |
| 4941 |
c1 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 1); |
| 4942 |
a2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 2); |
| 4943 |
test = runtime_error_ne (c1, a2, C_ERROR_1); |
| 4944 |
*next_code_point = test; |
| 4945 |
next_code_point = &test->next; |
| 4946 |
|
| 4947 |
c2 = get_array_inq_function (GFC_ISYM_SIZE, expr1, 2); |
| 4948 |
b1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 1); |
| 4949 |
test = runtime_error_ne (c2, b1, C_ERROR_2); |
| 4950 |
*next_code_point = test; |
| 4951 |
next_code_point = &test->next; |
| 4952 |
} |
| 4953 |
break; |
| 4954 |
|
| 4955 |
default: |
| 4956 |
gcc_unreachable (); |
| 4957 |
} |
| 4958 |
} |
| 4959 |
|
| 4960 |
/* Handle the reallocation, if needed. */ |
| 4961 |
|
| 4962 |
if (realloc_c) |
| 4963 |
{ |
| 4964 |
gfc_code *lhs_alloc; |
| 4965 |
|
| 4966 |
lhs_alloc = matmul_lhs_realloc (expr1, matrix_a, matrix_b, m_case); |
| 4967 |
*next_code_point = lhs_alloc; |
| 4968 |
next_code_point = &lhs_alloc->next; |
| 4969 |
} |
| 4970 |
|
| 4971 |
*next_code_point = call; |
| 4972 |
if_limit->next = co_next; |
| 4973 |
|
| 4974 |
/* Set up the BLAS call. */ |
| 4975 |
|
| 4976 |
call->op = EXEC_CALL; |
| 4977 |
|
| 4978 |
gfc_get_sym_tree (blas_name, current_ns, &(call->symtree), true); |
| 4979 |
call->symtree->n.sym->attr.subroutine = 1; |
| 4980 |
call->symtree->n.sym->attr.procedure = 1; |
| 4981 |
call->symtree->n.sym->attr.flavor = FL_PROCEDURE; |
| 4982 |
call->resolved_sym = call->symtree->n.sym; |
| 4983 |
gfc_commit_symbol (call->resolved_sym); |
| 4984 |
|
| 4985 |
/* Argument TRANSA. */ |
| 4986 |
next = gfc_get_actual_arglist (); |
| 4987 |
next->expr = gfc_get_character_expr (gfc_default_character_kind, &co->loc, |
| 4988 |
transa, 1); |
| 4989 |
|
| 4990 |
call->ext.actual = next; |
| 4991 |
|
| 4992 |
/* Argument TRANSB. */ |
| 4993 |
actual = next; |
| 4994 |
next = gfc_get_actual_arglist (); |
| 4995 |
next->expr = gfc_get_character_expr (gfc_default_character_kind, &co->loc, |
| 4996 |
transb, 1); |
| 4997 |
actual->next = next; |
| 4998 |
|
| 4999 |
c1 = get_array_inq_function (GFC_ISYM_SIZE, gfc_copy_expr (a->expr), 1, |
| 5000 |
gfc_integer_4_kind); |
| 5001 |
c2 = get_array_inq_function (GFC_ISYM_SIZE, gfc_copy_expr (b->expr), 2, |
| 5002 |
gfc_integer_4_kind); |
| 5003 |
|
| 5004 |
b1 = get_array_inq_function (GFC_ISYM_SIZE, gfc_copy_expr (b->expr), 1, |
| 5005 |
gfc_integer_4_kind); |
| 5006 |
|
| 5007 |
/* Argument M. */ |
| 5008 |
actual = next; |
| 5009 |
next = gfc_get_actual_arglist (); |
| 5010 |
next->expr = c1; |
| 5011 |
actual->next = next; |
| 5012 |
|
| 5013 |
/* Argument N. */ |
| 5014 |
actual = next; |
| 5015 |
next = gfc_get_actual_arglist (); |
| 5016 |
next->expr = c2; |
| 5017 |
actual->next = next; |
| 5018 |
|
| 5019 |
/* Argument K. */ |
| 5020 |
actual = next; |
| 5021 |
next = gfc_get_actual_arglist (); |
| 5022 |
next->expr = b1; |
| 5023 |
actual->next = next; |
| 5024 |
|
| 5025 |
/* Argument ALPHA - set to one. */ |
| 5026 |
actual = next; |
| 5027 |
next = gfc_get_actual_arglist (); |
| 5028 |
next->expr = gfc_get_constant_expr (type, kind, &co->loc); |
| 5029 |
if (type == BT_REAL) |
| 5030 |
mpfr_set_ui (next->expr->value.real, 1, GFC_RND_MODE); |
| 5031 |
else |
| 5032 |
mpc_set_ui (next->expr->value.complex, 1, GFC_MPC_RND_MODE); |
| 5033 |
actual->next = next; |
| 5034 |
|
| 5035 |
/* Argument A. */ |
| 5036 |
actual = next; |
| 5037 |
next = gfc_get_actual_arglist (); |
| 5038 |
next->expr = gfc_copy_expr (matrix_a); |
| 5039 |
actual->next = next; |
| 5040 |
|
| 5041 |
/* Argument LDA. */ |
| 5042 |
actual = next; |
| 5043 |
next = gfc_get_actual_arglist (); |
| 5044 |
next->expr = get_array_inq_function (GFC_ISYM_SIZE, gfc_copy_expr (matrix_a), |
| 5045 |
1, gfc_integer_4_kind); |
| 5046 |
actual->next = next; |
| 5047 |
|
| 5048 |
/* Argument B. */ |
| 5049 |
actual = next; |
| 5050 |
next = gfc_get_actual_arglist (); |
| 5051 |
next->expr = gfc_copy_expr (matrix_b); |
| 5052 |
actual->next = next; |
| 5053 |
|
| 5054 |
/* Argument LDB. */ |
| 5055 |
actual = next; |
| 5056 |
next = gfc_get_actual_arglist (); |
| 5057 |
next->expr = get_array_inq_function (GFC_ISYM_SIZE, gfc_copy_expr (matrix_b), |
| 5058 |
1, gfc_integer_4_kind); |
| 5059 |
actual->next = next; |
| 5060 |
|
| 5061 |
/* Argument BETA - set to zero. */ |
| 5062 |
actual = next; |
| 5063 |
next = gfc_get_actual_arglist (); |
| 5064 |
next->expr = gfc_get_constant_expr (type, kind, &co->loc); |
| 5065 |
if (type == BT_REAL) |
| 5066 |
mpfr_set_ui (next->expr->value.real, 0, GFC_RND_MODE); |
| 5067 |
else |
| 5068 |
mpc_set_ui (next->expr->value.complex, 0, GFC_MPC_RND_MODE); |
| 5069 |
actual->next = next; |
| 5070 |
|
| 5071 |
/* Argument C. */ |
| 5072 |
|
| 5073 |
actual = next; |
| 5074 |
next = gfc_get_actual_arglist (); |
| 5075 |
next->expr = gfc_copy_expr (expr1); |
| 5076 |
actual->next = next; |
| 5077 |
|
| 5078 |
/* Argument LDC. */ |
| 5079 |
actual = next; |
| 5080 |
next = gfc_get_actual_arglist (); |
| 5081 |
next->expr = get_array_inq_function (GFC_ISYM_SIZE, gfc_copy_expr (expr1), |
| 5082 |
1, gfc_integer_4_kind); |
| 5083 |
actual->next = next; |
| 5084 |
|
| 5085 |
return 0; |
| 5086 |
} |
| 5087 |
|
| 5088 |
|
| 5089 |
/* Code for index interchange for loops which are grouped together in DO |
| 5090 |
CONCURRENT or FORALL statements. This is currently only applied if the |
| 5091 |
iterations are grouped together in a single statement. |
| 5092 |
|
| 5093 |
For this transformation, it is assumed that memory access in strides is |
| 5094 |
expensive, and that loops which access later indices (which access memory |
| 5095 |
in bigger strides) should be moved to the first loops. |
| 5096 |
|
| 5097 |
For this, a loop over all the statements is executed, counting the times |
| 5098 |
that the loop iteration values are accessed in each index. The loop |
| 5099 |
indices are then sorted to minimize access to later indices from inner |
| 5100 |
loops. */ |
| 5101 |
|
| 5102 |
/* Type for holding index information. */ |
| 5103 |
|
| 5104 |
typedef struct { |
| 5105 |
gfc_symbol *sym; |
| 5106 |
gfc_forall_iterator *fa; |
| 5107 |
int num; |
| 5108 |
int n[GFC_MAX_DIMENSIONS]; |
| 5109 |
} ind_type; |
| 5110 |
|
| 5111 |
/* Callback function to determine if an expression is the |
| 5112 |
corresponding variable. */ |
| 5113 |
|
| 5114 |
static int |
| 5115 |
has_var (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED, void *data) |
| 5116 |
{ |
| 5117 |
gfc_expr *expr = *e; |
| 5118 |
gfc_symbol *sym; |
| 5119 |
|
| 5120 |
if (expr->expr_type != EXPR_VARIABLE) |
| 5121 |
return 0; |
| 5122 |
|
| 5123 |
sym = (gfc_symbol *) data; |
| 5124 |
return sym == expr->symtree->n.sym; |
| 5125 |
} |
| 5126 |
|
| 5127 |
/* Callback function to calculate the cost of a certain index. */ |
| 5128 |
|
| 5129 |
static int |
| 5130 |
index_cost (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 5131 |
void *data) |
| 5132 |
{ |
| 5133 |
ind_type *ind; |
| 5134 |
gfc_expr *expr; |
| 5135 |
gfc_array_ref *ar; |
| 5136 |
gfc_ref *ref; |
| 5137 |
int i,j; |
| 5138 |
|
| 5139 |
expr = *e; |
| 5140 |
if (expr->expr_type != EXPR_VARIABLE) |
| 5141 |
return 0; |
| 5142 |
|
| 5143 |
ar = NULL; |
| 5144 |
for (ref = expr->ref; ref; ref = ref->next) |
| 5145 |
{ |
| 5146 |
if (ref->type == REF_ARRAY) |
| 5147 |
{ |
| 5148 |
ar = &ref->u.ar; |
| 5149 |
break; |
| 5150 |
} |
| 5151 |
} |
| 5152 |
if (ar == NULL || ar->type != AR_ELEMENT) |
| 5153 |
return 0; |
| 5154 |
|
| 5155 |
ind = (ind_type *) data; |
| 5156 |
for (i = 0; i < ar->dimen; i++) |
| 5157 |
{ |
| 5158 |
for (j=0; ind[j].sym != NULL; j++) |
| 5159 |
{ |
| 5160 |
if (gfc_expr_walker (&ar->start[i], has_var, (void *) (ind[j].sym))) |
| 5161 |
ind[j].n[i]++; |
| 5162 |
} |
| 5163 |
} |
| 5164 |
return 0; |
| 5165 |
} |
| 5166 |
|
| 5167 |
/* Callback function for qsort, to sort the loop indices. */ |
| 5168 |
|
| 5169 |
static int |
| 5170 |
loop_comp (const void *e1, const void *e2) |
| 5171 |
{ |
| 5172 |
const ind_type *i1 = (const ind_type *) e1; |
| 5173 |
const ind_type *i2 = (const ind_type *) e2; |
| 5174 |
int i; |
| 5175 |
|
| 5176 |
for (i=GFC_MAX_DIMENSIONS-1; i >= 0; i--) |
| 5177 |
{ |
| 5178 |
if (i1->n[i] != i2->n[i]) |
| 5179 |
return i1->n[i] - i2->n[i]; |
| 5180 |
} |
| 5181 |
/* All other things being equal, let's not change the ordering. */ |
| 5182 |
return i2->num - i1->num; |
| 5183 |
} |
| 5184 |
|
| 5185 |
/* Main function to do the index interchange. */ |
| 5186 |
|
| 5187 |
static int |
| 5188 |
index_interchange (gfc_code **c, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 5189 |
void *data ATTRIBUTE_UNUSED) |
| 5190 |
{ |
| 5191 |
gfc_code *co; |
| 5192 |
co = *c; |
| 5193 |
int n_iter; |
| 5194 |
gfc_forall_iterator *fa; |
| 5195 |
ind_type *ind; |
| 5196 |
int i, j; |
| 5197 |
|
| 5198 |
if (co->op != EXEC_FORALL && co->op != EXEC_DO_CONCURRENT) |
| 5199 |
return 0; |
| 5200 |
|
| 5201 |
n_iter = 0; |
| 5202 |
for (fa = co->ext.concur.forall_iterator; fa; fa = fa->next) |
| 5203 |
n_iter ++; |
| 5204 |
|
| 5205 |
/* Nothing to reorder. */ |
| 5206 |
if (n_iter < 2) |
| 5207 |
return 0; |
| 5208 |
|
| 5209 |
ind = XALLOCAVEC (ind_type, n_iter + 1); |
| 5210 |
|
| 5211 |
i = 0; |
| 5212 |
for (fa = co->ext.concur.forall_iterator; fa; fa = fa->next) |
| 5213 |
{ |
| 5214 |
ind[i].sym = fa->var->symtree->n.sym; |
| 5215 |
ind[i].fa = fa; |
| 5216 |
for (j=0; j<GFC_MAX_DIMENSIONS; j++) |
| 5217 |
ind[i].n[j] = 0; |
| 5218 |
ind[i].num = i; |
| 5219 |
i++; |
| 5220 |
} |
| 5221 |
ind[n_iter].sym = NULL; |
| 5222 |
ind[n_iter].fa = NULL; |
| 5223 |
|
| 5224 |
gfc_code_walker (c, gfc_dummy_code_callback, index_cost, (void *) ind); |
| 5225 |
qsort ((void *) ind, n_iter, sizeof (ind_type), loop_comp); |
| 5226 |
|
| 5227 |
/* Do the actual index interchange. */ |
| 5228 |
co->ext.concur.forall_iterator = fa = ind[0].fa; |
| 5229 |
for (i=1; i<n_iter; i++) |
| 5230 |
{ |
| 5231 |
fa->next = ind[i].fa; |
| 5232 |
fa = fa->next; |
| 5233 |
} |
| 5234 |
fa->next = NULL; |
| 5235 |
|
| 5236 |
if (flag_warn_frontend_loop_interchange) |
| 5237 |
{ |
| 5238 |
for (i=1; i<n_iter; i++) |
| 5239 |
{ |
| 5240 |
if (ind[i-1].num > ind[i].num) |
| 5241 |
{ |
| 5242 |
gfc_warning (OPT_Wfrontend_loop_interchange, |
| 5243 |
"Interchanging loops at %L", &co->loc); |
| 5244 |
break; |
| 5245 |
} |
| 5246 |
} |
| 5247 |
} |
| 5248 |
|
| 5249 |
return 0; |
| 5250 |
} |
| 5251 |
|
| 5252 |
#define WALK_SUBEXPR(NODE) \ |
| 5253 |
do \ |
| 5254 |
{ \ |
| 5255 |
result = gfc_expr_walker (&(NODE), exprfn, data); \ |
| 5256 |
if (result) \ |
| 5257 |
return result; \ |
| 5258 |
} \ |
| 5259 |
while (0) |
| 5260 |
#define WALK_SUBEXPR_TAIL(NODE) e = &(NODE); continue |
| 5261 |
|
| 5262 |
/* Walk expression *E, calling EXPRFN on each expression in it. */ |
| 5263 |
|
| 5264 |
int |
| 5265 |
gfc_expr_walker (gfc_expr **e, walk_expr_fn_t exprfn, void *data) |
| 5266 |
{ |
| 5267 |
while (*e) |
| 5268 |
{ |
| 5269 |
int walk_subtrees = 1; |
| 5270 |
gfc_actual_arglist *a; |
| 5271 |
gfc_ref *r; |
| 5272 |
gfc_constructor *c; |
| 5273 |
|
| 5274 |
int result = exprfn (e, &walk_subtrees, data); |
| 5275 |
if (result) |
| 5276 |
return result; |
| 5277 |
if (walk_subtrees) |
| 5278 |
switch ((*e)->expr_type) |
| 5279 |
{ |
| 5280 |
case EXPR_OP: |
| 5281 |
WALK_SUBEXPR ((*e)->value.op.op1); |
| 5282 |
WALK_SUBEXPR_TAIL ((*e)->value.op.op2); |
| 5283 |
/* No fallthru because of the tail recursion above. */ |
| 5284 |
case EXPR_FUNCTION: |
| 5285 |
for (a = (*e)->value.function.actual; a; a = a->next) |
| 5286 |
WALK_SUBEXPR (a->expr); |
| 5287 |
break; |
| 5288 |
case EXPR_COMPCALL: |
| 5289 |
case EXPR_PPC: |
| 5290 |
WALK_SUBEXPR ((*e)->value.compcall.base_object); |
| 5291 |
for (a = (*e)->value.compcall.actual; a; a = a->next) |
| 5292 |
WALK_SUBEXPR (a->expr); |
| 5293 |
break; |
| 5294 |
|
| 5295 |
case EXPR_STRUCTURE: |
| 5296 |
case EXPR_ARRAY: |
| 5297 |
for (c = gfc_constructor_first ((*e)->value.constructor); c; |
| 5298 |
c = gfc_constructor_next (c)) |
| 5299 |
{ |
| 5300 |
if (c->iterator == NULL) |
| 5301 |
WALK_SUBEXPR (c->expr); |
| 5302 |
else |
| 5303 |
{ |
| 5304 |
iterator_level ++; |
| 5305 |
WALK_SUBEXPR (c->expr); |
| 5306 |
iterator_level --; |
| 5307 |
WALK_SUBEXPR (c->iterator->var); |
| 5308 |
WALK_SUBEXPR (c->iterator->start); |
| 5309 |
WALK_SUBEXPR (c->iterator->end); |
| 5310 |
WALK_SUBEXPR (c->iterator->step); |
| 5311 |
} |
| 5312 |
} |
| 5313 |
|
| 5314 |
if ((*e)->expr_type != EXPR_ARRAY) |
| 5315 |
break; |
| 5316 |
|
| 5317 |
/* Fall through to the variable case in order to walk the |
| 5318 |
reference. */ |
| 5319 |
gcc_fallthrough (); |
| 5320 |
|
| 5321 |
case EXPR_SUBSTRING: |
| 5322 |
case EXPR_VARIABLE: |
| 5323 |
for (r = (*e)->ref; r; r = r->next) |
| 5324 |
{ |
| 5325 |
gfc_array_ref *ar; |
| 5326 |
int i; |
| 5327 |
|
| 5328 |
switch (r->type) |
| 5329 |
{ |
| 5330 |
case REF_ARRAY: |
| 5331 |
ar = &r->u.ar; |
| 5332 |
if (ar->type == AR_SECTION || ar->type == AR_ELEMENT) |
| 5333 |
{ |
| 5334 |
for (i=0; i< ar->dimen; i++) |
| 5335 |
{ |
| 5336 |
WALK_SUBEXPR (ar->start[i]); |
| 5337 |
WALK_SUBEXPR (ar->end[i]); |
| 5338 |
WALK_SUBEXPR (ar->stride[i]); |
| 5339 |
} |
| 5340 |
} |
| 5341 |
|
| 5342 |
break; |
| 5343 |
|
| 5344 |
case REF_SUBSTRING: |
| 5345 |
WALK_SUBEXPR (r->u.ss.start); |
| 5346 |
WALK_SUBEXPR (r->u.ss.end); |
| 5347 |
break; |
| 5348 |
|
| 5349 |
case REF_COMPONENT: |
| 5350 |
case REF_INQUIRY: |
| 5351 |
break; |
| 5352 |
} |
| 5353 |
} |
| 5354 |
|
| 5355 |
default: |
| 5356 |
break; |
| 5357 |
} |
| 5358 |
return 0; |
| 5359 |
} |
| 5360 |
return 0; |
| 5361 |
} |
| 5362 |
|
| 5363 |
#define WALK_SUBCODE(NODE) \ |
| 5364 |
do \ |
| 5365 |
{ \ |
| 5366 |
result = gfc_code_walker (&(NODE), codefn, exprfn, data); \ |
| 5367 |
if (result) \ |
| 5368 |
return result; \ |
| 5369 |
} \ |
| 5370 |
while (0) |
| 5371 |
|
| 5372 |
/* Walk code *C, calling CODEFN on each gfc_code node in it and calling EXPRFN |
| 5373 |
on each expression in it. If any of the hooks returns non-zero, that |
| 5374 |
value is immediately returned. If the hook sets *WALK_SUBTREES to 0, |
| 5375 |
no subcodes or subexpressions are traversed. */ |
| 5376 |
|
| 5377 |
int |
| 5378 |
gfc_code_walker (gfc_code **c, walk_code_fn_t codefn, walk_expr_fn_t exprfn, |
| 5379 |
void *data) |
| 5380 |
{ |
| 5381 |
for (; *c; c = &(*c)->next) |
| 5382 |
{ |
| 5383 |
int walk_subtrees = 1; |
| 5384 |
int result = codefn (c, &walk_subtrees, data); |
| 5385 |
if (result) |
| 5386 |
return result; |
| 5387 |
|
| 5388 |
if (walk_subtrees) |
| 5389 |
{ |
| 5390 |
gfc_code *b; |
| 5391 |
gfc_actual_arglist *a; |
| 5392 |
gfc_code *co; |
| 5393 |
gfc_association_list *alist; |
| 5394 |
bool saved_in_omp_workshare; |
| 5395 |
bool saved_in_omp_atomic; |
| 5396 |
bool saved_in_where; |
| 5397 |
|
| 5398 |
/* There might be statement insertions before the current code, |
| 5399 |
which must not affect the expression walker. */ |
| 5400 |
|
| 5401 |
co = *c; |
| 5402 |
saved_in_omp_workshare = in_omp_workshare; |
| 5403 |
saved_in_omp_atomic = in_omp_atomic; |
| 5404 |
saved_in_where = in_where; |
| 5405 |
|
| 5406 |
switch (co->op) |
| 5407 |
{ |
| 5408 |
|
| 5409 |
case EXEC_BLOCK: |
| 5410 |
WALK_SUBCODE (co->ext.block.ns->code); |
| 5411 |
if (co->ext.block.assoc) |
| 5412 |
{ |
| 5413 |
bool saved_in_assoc_list = in_assoc_list; |
| 5414 |
|
| 5415 |
in_assoc_list = true; |
| 5416 |
for (alist = co->ext.block.assoc; alist; alist = alist->next) |
| 5417 |
WALK_SUBEXPR (alist->target); |
| 5418 |
|
| 5419 |
in_assoc_list = saved_in_assoc_list; |
| 5420 |
} |
| 5421 |
|
| 5422 |
break; |
| 5423 |
|
| 5424 |
case EXEC_DO: |
| 5425 |
doloop_level ++; |
| 5426 |
WALK_SUBEXPR (co->ext.iterator->var); |
| 5427 |
WALK_SUBEXPR (co->ext.iterator->start); |
| 5428 |
WALK_SUBEXPR (co->ext.iterator->end); |
| 5429 |
WALK_SUBEXPR (co->ext.iterator->step); |
| 5430 |
break; |
| 5431 |
|
| 5432 |
case EXEC_IF: |
| 5433 |
if_level ++; |
| 5434 |
break; |
| 5435 |
|
| 5436 |
case EXEC_WHERE: |
| 5437 |
in_where = true; |
| 5438 |
break; |
| 5439 |
|
| 5440 |
case EXEC_CALL: |
| 5441 |
case EXEC_ASSIGN_CALL: |
| 5442 |
for (a = co->ext.actual; a; a = a->next) |
| 5443 |
WALK_SUBEXPR (a->expr); |
| 5444 |
break; |
| 5445 |
|
| 5446 |
case EXEC_CALL_PPC: |
| 5447 |
WALK_SUBEXPR (co->expr1); |
| 5448 |
for (a = co->ext.actual; a; a = a->next) |
| 5449 |
WALK_SUBEXPR (a->expr); |
| 5450 |
break; |
| 5451 |
|
| 5452 |
case EXEC_SELECT: |
| 5453 |
WALK_SUBEXPR (co->expr1); |
| 5454 |
select_level ++; |
| 5455 |
for (b = co->block; b; b = b->block) |
| 5456 |
{ |
| 5457 |
gfc_case *cp; |
| 5458 |
for (cp = b->ext.block.case_list; cp; cp = cp->next) |
| 5459 |
{ |
| 5460 |
WALK_SUBEXPR (cp->low); |
| 5461 |
WALK_SUBEXPR (cp->high); |
| 5462 |
} |
| 5463 |
WALK_SUBCODE (b->next); |
| 5464 |
} |
| 5465 |
continue; |
| 5466 |
|
| 5467 |
case EXEC_ALLOCATE: |
| 5468 |
case EXEC_DEALLOCATE: |
| 5469 |
{ |
| 5470 |
gfc_alloc *a; |
| 5471 |
for (a = co->ext.alloc.list; a; a = a->next) |
| 5472 |
WALK_SUBEXPR (a->expr); |
| 5473 |
break; |
| 5474 |
} |
| 5475 |
|
| 5476 |
case EXEC_FORALL: |
| 5477 |
case EXEC_DO_CONCURRENT: |
| 5478 |
{ |
| 5479 |
gfc_forall_iterator *fa; |
| 5480 |
for (fa = co->ext.concur.forall_iterator; fa; fa = fa->next) |
| 5481 |
{ |
| 5482 |
WALK_SUBEXPR (fa->var); |
| 5483 |
WALK_SUBEXPR (fa->start); |
| 5484 |
WALK_SUBEXPR (fa->end); |
| 5485 |
WALK_SUBEXPR (fa->stride); |
| 5486 |
} |
| 5487 |
if (co->op == EXEC_FORALL) |
| 5488 |
forall_level ++; |
| 5489 |
break; |
| 5490 |
} |
| 5491 |
|
| 5492 |
case EXEC_OPEN: |
| 5493 |
WALK_SUBEXPR (co->ext.open->unit); |
| 5494 |
WALK_SUBEXPR (co->ext.open->file); |
| 5495 |
WALK_SUBEXPR (co->ext.open->status); |
| 5496 |
WALK_SUBEXPR (co->ext.open->access); |
| 5497 |
WALK_SUBEXPR (co->ext.open->form); |
| 5498 |
WALK_SUBEXPR (co->ext.open->recl); |
| 5499 |
WALK_SUBEXPR (co->ext.open->blank); |
| 5500 |
WALK_SUBEXPR (co->ext.open->position); |
| 5501 |
WALK_SUBEXPR (co->ext.open->action); |
| 5502 |
WALK_SUBEXPR (co->ext.open->delim); |
| 5503 |
WALK_SUBEXPR (co->ext.open->pad); |
| 5504 |
WALK_SUBEXPR (co->ext.open->iostat); |
| 5505 |
WALK_SUBEXPR (co->ext.open->iomsg); |
| 5506 |
WALK_SUBEXPR (co->ext.open->convert); |
| 5507 |
WALK_SUBEXPR (co->ext.open->decimal); |
| 5508 |
WALK_SUBEXPR (co->ext.open->encoding); |
| 5509 |
WALK_SUBEXPR (co->ext.open->round); |
| 5510 |
WALK_SUBEXPR (co->ext.open->sign); |
| 5511 |
WALK_SUBEXPR (co->ext.open->asynchronous); |
| 5512 |
WALK_SUBEXPR (co->ext.open->id); |
| 5513 |
WALK_SUBEXPR (co->ext.open->newunit); |
| 5514 |
WALK_SUBEXPR (co->ext.open->share); |
| 5515 |
WALK_SUBEXPR (co->ext.open->cc); |
| 5516 |
break; |
| 5517 |
|
| 5518 |
case EXEC_CLOSE: |
| 5519 |
WALK_SUBEXPR (co->ext.close->unit); |
| 5520 |
WALK_SUBEXPR (co->ext.close->status); |
| 5521 |
WALK_SUBEXPR (co->ext.close->iostat); |
| 5522 |
WALK_SUBEXPR (co->ext.close->iomsg); |
| 5523 |
break; |
| 5524 |
|
| 5525 |
case EXEC_BACKSPACE: |
| 5526 |
case EXEC_ENDFILE: |
| 5527 |
case EXEC_REWIND: |
| 5528 |
case EXEC_FLUSH: |
| 5529 |
WALK_SUBEXPR (co->ext.filepos->unit); |
| 5530 |
WALK_SUBEXPR (co->ext.filepos->iostat); |
| 5531 |
WALK_SUBEXPR (co->ext.filepos->iomsg); |
| 5532 |
break; |
| 5533 |
|
| 5534 |
case EXEC_INQUIRE: |
| 5535 |
WALK_SUBEXPR (co->ext.inquire->unit); |
| 5536 |
WALK_SUBEXPR (co->ext.inquire->file); |
| 5537 |
WALK_SUBEXPR (co->ext.inquire->iomsg); |
| 5538 |
WALK_SUBEXPR (co->ext.inquire->iostat); |
| 5539 |
WALK_SUBEXPR (co->ext.inquire->exist); |
| 5540 |
WALK_SUBEXPR (co->ext.inquire->opened); |
| 5541 |
WALK_SUBEXPR (co->ext.inquire->number); |
| 5542 |
WALK_SUBEXPR (co->ext.inquire->named); |
| 5543 |
WALK_SUBEXPR (co->ext.inquire->name); |
| 5544 |
WALK_SUBEXPR (co->ext.inquire->access); |
| 5545 |
WALK_SUBEXPR (co->ext.inquire->sequential); |
| 5546 |
WALK_SUBEXPR (co->ext.inquire->direct); |
| 5547 |
WALK_SUBEXPR (co->ext.inquire->form); |
| 5548 |
WALK_SUBEXPR (co->ext.inquire->formatted); |
| 5549 |
WALK_SUBEXPR (co->ext.inquire->unformatted); |
| 5550 |
WALK_SUBEXPR (co->ext.inquire->recl); |
| 5551 |
WALK_SUBEXPR (co->ext.inquire->nextrec); |
| 5552 |
WALK_SUBEXPR (co->ext.inquire->blank); |
| 5553 |
WALK_SUBEXPR (co->ext.inquire->position); |
| 5554 |
WALK_SUBEXPR (co->ext.inquire->action); |
| 5555 |
WALK_SUBEXPR (co->ext.inquire->read); |
| 5556 |
WALK_SUBEXPR (co->ext.inquire->write); |
| 5557 |
WALK_SUBEXPR (co->ext.inquire->readwrite); |
| 5558 |
WALK_SUBEXPR (co->ext.inquire->delim); |
| 5559 |
WALK_SUBEXPR (co->ext.inquire->encoding); |
| 5560 |
WALK_SUBEXPR (co->ext.inquire->pad); |
| 5561 |
WALK_SUBEXPR (co->ext.inquire->iolength); |
| 5562 |
WALK_SUBEXPR (co->ext.inquire->convert); |
| 5563 |
WALK_SUBEXPR (co->ext.inquire->strm_pos); |
| 5564 |
WALK_SUBEXPR (co->ext.inquire->asynchronous); |
| 5565 |
WALK_SUBEXPR (co->ext.inquire->decimal); |
| 5566 |
WALK_SUBEXPR (co->ext.inquire->pending); |
| 5567 |
WALK_SUBEXPR (co->ext.inquire->id); |
| 5568 |
WALK_SUBEXPR (co->ext.inquire->sign); |
| 5569 |
WALK_SUBEXPR (co->ext.inquire->size); |
| 5570 |
WALK_SUBEXPR (co->ext.inquire->round); |
| 5571 |
break; |
| 5572 |
|
| 5573 |
case EXEC_WAIT: |
| 5574 |
WALK_SUBEXPR (co->ext.wait->unit); |
| 5575 |
WALK_SUBEXPR (co->ext.wait->iostat); |
| 5576 |
WALK_SUBEXPR (co->ext.wait->iomsg); |
| 5577 |
WALK_SUBEXPR (co->ext.wait->id); |
| 5578 |
break; |
| 5579 |
|
| 5580 |
case EXEC_READ: |
| 5581 |
case EXEC_WRITE: |
| 5582 |
WALK_SUBEXPR (co->ext.dt->io_unit); |
| 5583 |
WALK_SUBEXPR (co->ext.dt->format_expr); |
| 5584 |
WALK_SUBEXPR (co->ext.dt->rec); |
| 5585 |
WALK_SUBEXPR (co->ext.dt->advance); |
| 5586 |
WALK_SUBEXPR (co->ext.dt->iostat); |
| 5587 |
WALK_SUBEXPR (co->ext.dt->size); |
| 5588 |
WALK_SUBEXPR (co->ext.dt->iomsg); |
| 5589 |
WALK_SUBEXPR (co->ext.dt->id); |
| 5590 |
WALK_SUBEXPR (co->ext.dt->pos); |
| 5591 |
WALK_SUBEXPR (co->ext.dt->asynchronous); |
| 5592 |
WALK_SUBEXPR (co->ext.dt->blank); |
| 5593 |
WALK_SUBEXPR (co->ext.dt->decimal); |
| 5594 |
WALK_SUBEXPR (co->ext.dt->delim); |
| 5595 |
WALK_SUBEXPR (co->ext.dt->pad); |
| 5596 |
WALK_SUBEXPR (co->ext.dt->round); |
| 5597 |
WALK_SUBEXPR (co->ext.dt->sign); |
| 5598 |
WALK_SUBEXPR (co->ext.dt->extra_comma); |
| 5599 |
break; |
| 5600 |
|
| 5601 |
case EXEC_OACC_ATOMIC: |
| 5602 |
case EXEC_OMP_ATOMIC: |
| 5603 |
in_omp_atomic = true; |
| 5604 |
break; |
| 5605 |
|
| 5606 |
case EXEC_OMP_PARALLEL: |
| 5607 |
case EXEC_OMP_PARALLEL_DO: |
| 5608 |
case EXEC_OMP_PARALLEL_DO_SIMD: |
| 5609 |
case EXEC_OMP_PARALLEL_LOOP: |
| 5610 |
case EXEC_OMP_PARALLEL_MASKED: |
| 5611 |
case EXEC_OMP_PARALLEL_MASKED_TASKLOOP: |
| 5612 |
case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD: |
| 5613 |
case EXEC_OMP_PARALLEL_MASTER: |
| 5614 |
case EXEC_OMP_PARALLEL_MASTER_TASKLOOP: |
| 5615 |
case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD: |
| 5616 |
case EXEC_OMP_PARALLEL_SECTIONS: |
| 5617 |
|
| 5618 |
in_omp_workshare = false; |
| 5619 |
|
| 5620 |
/* This goto serves as a shortcut to avoid code |
| 5621 |
duplication or a larger if or switch statement. */ |
| 5622 |
goto check_omp_clauses; |
| 5623 |
|
| 5624 |
case EXEC_OMP_WORKSHARE: |
| 5625 |
case EXEC_OMP_PARALLEL_WORKSHARE: |
| 5626 |
|
| 5627 |
in_omp_workshare = true; |
| 5628 |
|
| 5629 |
/* Fall through */ |
| 5630 |
|
| 5631 |
case EXEC_OMP_CRITICAL: |
| 5632 |
case EXEC_OMP_DISTRIBUTE: |
| 5633 |
case EXEC_OMP_DISTRIBUTE_PARALLEL_DO: |
| 5634 |
case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: |
| 5635 |
case EXEC_OMP_DISTRIBUTE_SIMD: |
| 5636 |
case EXEC_OMP_DO: |
| 5637 |
case EXEC_OMP_DO_SIMD: |
| 5638 |
case EXEC_OMP_LOOP: |
| 5639 |
case EXEC_OMP_ORDERED: |
| 5640 |
case EXEC_OMP_SECTIONS: |
| 5641 |
case EXEC_OMP_SINGLE: |
| 5642 |
case EXEC_OMP_END_SINGLE: |
| 5643 |
case EXEC_OMP_SIMD: |
| 5644 |
case EXEC_OMP_TASKLOOP: |
| 5645 |
case EXEC_OMP_TASKLOOP_SIMD: |
| 5646 |
case EXEC_OMP_TARGET: |
| 5647 |
case EXEC_OMP_TARGET_DATA: |
| 5648 |
case EXEC_OMP_TARGET_ENTER_DATA: |
| 5649 |
case EXEC_OMP_TARGET_EXIT_DATA: |
| 5650 |
case EXEC_OMP_TARGET_PARALLEL: |
| 5651 |
case EXEC_OMP_TARGET_PARALLEL_DO: |
| 5652 |
case EXEC_OMP_TARGET_PARALLEL_DO_SIMD: |
| 5653 |
case EXEC_OMP_TARGET_PARALLEL_LOOP: |
| 5654 |
case EXEC_OMP_TARGET_SIMD: |
| 5655 |
case EXEC_OMP_TARGET_TEAMS: |
| 5656 |
case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE: |
| 5657 |
case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: |
| 5658 |
case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: |
| 5659 |
case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: |
| 5660 |
case EXEC_OMP_TARGET_TEAMS_LOOP: |
| 5661 |
case EXEC_OMP_TARGET_UPDATE: |
| 5662 |
case EXEC_OMP_TASK: |
| 5663 |
case EXEC_OMP_TEAMS: |
| 5664 |
case EXEC_OMP_TEAMS_DISTRIBUTE: |
| 5665 |
case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: |
| 5666 |
case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: |
| 5667 |
case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD: |
| 5668 |
case EXEC_OMP_TEAMS_LOOP: |
| 5669 |
|
| 5670 |
/* Come to this label only from the |
| 5671 |
EXEC_OMP_PARALLEL_* cases above. */ |
| 5672 |
|
| 5673 |
check_omp_clauses: |
| 5674 |
|
| 5675 |
if (co->ext.omp_clauses) |
| 5676 |
{ |
| 5677 |
gfc_omp_namelist *n; |
| 5678 |
static int list_types[] |
| 5679 |
= { OMP_LIST_ALIGNED, OMP_LIST_LINEAR, OMP_LIST_DEPEND, |
| 5680 |
OMP_LIST_MAP, OMP_LIST_TO, OMP_LIST_FROM }; |
| 5681 |
size_t idx; |
| 5682 |
WALK_SUBEXPR (co->ext.omp_clauses->if_expr); |
| 5683 |
for (idx = 0; idx < OMP_IF_LAST; idx++) |
| 5684 |
WALK_SUBEXPR (co->ext.omp_clauses->if_exprs[idx]); |
| 5685 |
WALK_SUBEXPR (co->ext.omp_clauses->final_expr); |
| 5686 |
WALK_SUBEXPR (co->ext.omp_clauses->num_threads); |
| 5687 |
WALK_SUBEXPR (co->ext.omp_clauses->chunk_size); |
| 5688 |
WALK_SUBEXPR (co->ext.omp_clauses->safelen_expr); |
| 5689 |
WALK_SUBEXPR (co->ext.omp_clauses->simdlen_expr); |
| 5690 |
WALK_SUBEXPR (co->ext.omp_clauses->num_teams_lower); |
| 5691 |
WALK_SUBEXPR (co->ext.omp_clauses->num_teams_upper); |
| 5692 |
WALK_SUBEXPR (co->ext.omp_clauses->device); |
| 5693 |
WALK_SUBEXPR (co->ext.omp_clauses->thread_limit); |
| 5694 |
WALK_SUBEXPR (co->ext.omp_clauses->dist_chunk_size); |
| 5695 |
WALK_SUBEXPR (co->ext.omp_clauses->grainsize); |
| 5696 |
WALK_SUBEXPR (co->ext.omp_clauses->hint); |
| 5697 |
WALK_SUBEXPR (co->ext.omp_clauses->num_tasks); |
| 5698 |
WALK_SUBEXPR (co->ext.omp_clauses->priority); |
| 5699 |
WALK_SUBEXPR (co->ext.omp_clauses->detach); |
| 5700 |
WALK_SUBEXPR (co->ext.omp_clauses->novariants); |
| 5701 |
WALK_SUBEXPR (co->ext.omp_clauses->nocontext); |
| 5702 |
for (idx = 0; idx < ARRAY_SIZE (list_types); idx++) |
| 5703 |
for (n = co->ext.omp_clauses->lists[list_types[idx]]; |
| 5704 |
n; n = n->next) |
| 5705 |
WALK_SUBEXPR (n->expr); |
| 5706 |
} |
| 5707 |
break; |
| 5708 |
default: |
| 5709 |
break; |
| 5710 |
} |
| 5711 |
|
| 5712 |
WALK_SUBEXPR (co->expr1); |
| 5713 |
WALK_SUBEXPR (co->expr2); |
| 5714 |
WALK_SUBEXPR (co->expr3); |
| 5715 |
WALK_SUBEXPR (co->expr4); |
| 5716 |
for (b = co->block; b; b = b->block) |
| 5717 |
{ |
| 5718 |
WALK_SUBEXPR (b->expr1); |
| 5719 |
WALK_SUBEXPR (b->expr2); |
| 5720 |
WALK_SUBCODE (b->next); |
| 5721 |
} |
| 5722 |
|
| 5723 |
if (co->op == EXEC_FORALL) |
| 5724 |
forall_level --; |
| 5725 |
|
| 5726 |
if (co->op == EXEC_DO) |
| 5727 |
doloop_level --; |
| 5728 |
|
| 5729 |
if (co->op == EXEC_IF) |
| 5730 |
if_level --; |
| 5731 |
|
| 5732 |
if (co->op == EXEC_SELECT) |
| 5733 |
select_level --; |
| 5734 |
|
| 5735 |
in_omp_workshare = saved_in_omp_workshare; |
| 5736 |
in_omp_atomic = saved_in_omp_atomic; |
| 5737 |
in_where = saved_in_where; |
| 5738 |
} |
| 5739 |
} |
| 5740 |
return 0; |
| 5741 |
} |
| 5742 |
|
| 5743 |
/* As a post-resolution step, check that all global symbols which are |
| 5744 |
not declared in the source file match in their call signatures. |
| 5745 |
We do this by looping over the code (and expressions). The first call |
| 5746 |
we happen to find is assumed to be canonical. */ |
| 5747 |
|
| 5748 |
|
| 5749 |
/* Common tests for argument checking for both functions and subroutines. */ |
| 5750 |
|
| 5751 |
static int |
| 5752 |
check_externals_procedure (gfc_symbol *sym, locus *loc, |
| 5753 |
gfc_actual_arglist *actual) |
| 5754 |
{ |
| 5755 |
gfc_gsymbol *gsym; |
| 5756 |
gfc_symbol *def_sym = NULL; |
| 5757 |
|
| 5758 |
if (sym == NULL || sym->attr.is_bind_c) |
| 5759 |
return 0; |
| 5760 |
|
| 5761 |
if (sym->attr.proc != PROC_EXTERNAL && sym->attr.proc != PROC_UNKNOWN) |
| 5762 |
return 0; |
| 5763 |
|
| 5764 |
if (sym->attr.if_source == IFSRC_IFBODY || sym->attr.if_source == IFSRC_DECL) |
| 5765 |
return 0; |
| 5766 |
|
| 5767 |
gsym = gfc_find_gsymbol (gfc_gsym_root, sym->name); |
| 5768 |
if (gsym == NULL) |
| 5769 |
return 0; |
| 5770 |
|
| 5771 |
if (gsym->ns) |
| 5772 |
gfc_find_symbol (sym->name, gsym->ns, 0, &def_sym); |
| 5773 |
|
| 5774 |
if (gsym->bind_c && def_sym && def_sym->binding_label == NULL) |
| 5775 |
return 0; |
| 5776 |
|
| 5777 |
if (def_sym) |
| 5778 |
{ |
| 5779 |
gfc_compare_actual_formal (&actual, def_sym->formal, 0, 0, 0, loc); |
| 5780 |
return 0; |
| 5781 |
} |
| 5782 |
|
| 5783 |
/* First time we have seen this procedure called. Let's create an |
| 5784 |
"interface" from the call and put it into a new namespace. */ |
| 5785 |
gfc_namespace *save_ns; |
| 5786 |
gfc_symbol *new_sym; |
| 5787 |
|
| 5788 |
gsym->where = *loc; |
| 5789 |
save_ns = gfc_current_ns; |
| 5790 |
gsym->ns = gfc_get_namespace (gfc_current_ns, 0); |
| 5791 |
gsym->ns->proc_name = sym; |
| 5792 |
|
| 5793 |
gfc_get_symbol (sym->name, gsym->ns, &new_sym); |
| 5794 |
gcc_assert (new_sym); |
| 5795 |
new_sym->attr = sym->attr; |
| 5796 |
new_sym->attr.if_source = IFSRC_DECL; |
| 5797 |
new_sym->ts = sym->ts; |
| 5798 |
gfc_current_ns = gsym->ns; |
| 5799 |
|
| 5800 |
gfc_get_formal_from_actual_arglist (new_sym, actual); |
| 5801 |
new_sym->declared_at = *loc; |
| 5802 |
gfc_current_ns = save_ns; |
| 5803 |
|
| 5804 |
return 0; |
| 5805 |
|
| 5806 |
} |
| 5807 |
|
| 5808 |
/* Callback for calls of external routines. */ |
| 5809 |
|
| 5810 |
static int |
| 5811 |
check_externals_code (gfc_code **c, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 5812 |
void *data ATTRIBUTE_UNUSED) |
| 5813 |
{ |
| 5814 |
gfc_code *co = *c; |
| 5815 |
gfc_symbol *sym; |
| 5816 |
locus *loc; |
| 5817 |
gfc_actual_arglist *actual; |
| 5818 |
|
| 5819 |
if (co->op != EXEC_CALL) |
| 5820 |
return 0; |
| 5821 |
|
| 5822 |
sym = co->resolved_sym; |
| 5823 |
loc = &co->loc; |
| 5824 |
actual = co->ext.actual; |
| 5825 |
|
| 5826 |
return check_externals_procedure (sym, loc, actual); |
| 5827 |
|
| 5828 |
} |
| 5829 |
|
| 5830 |
/* Callback for external functions. */ |
| 5831 |
|
| 5832 |
static int |
| 5833 |
check_externals_expr (gfc_expr **ep, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 5834 |
void *data ATTRIBUTE_UNUSED) |
| 5835 |
{ |
| 5836 |
gfc_expr *e = *ep; |
| 5837 |
gfc_symbol *sym; |
| 5838 |
locus *loc; |
| 5839 |
gfc_actual_arglist *actual; |
| 5840 |
|
| 5841 |
if (e->expr_type != EXPR_FUNCTION) |
| 5842 |
return 0; |
| 5843 |
|
| 5844 |
if (e->symtree && e->symtree->n.sym->attr.subroutine) |
| 5845 |
return 0; |
| 5846 |
|
| 5847 |
sym = e->value.function.esym; |
| 5848 |
if (sym == NULL) |
| 5849 |
return 0; |
| 5850 |
|
| 5851 |
loc = &e->where; |
| 5852 |
actual = e->value.function.actual; |
| 5853 |
|
| 5854 |
return check_externals_procedure (sym, loc, actual); |
| 5855 |
} |
| 5856 |
|
| 5857 |
/* Function to check if any interface clashes with a global |
| 5858 |
identifier, to be invoked via gfc_traverse_ns. */ |
| 5859 |
|
| 5860 |
static void |
| 5861 |
check_against_globals (gfc_symbol *sym) |
| 5862 |
{ |
| 5863 |
gfc_gsymbol *gsym; |
| 5864 |
gfc_symbol *def_sym = NULL; |
| 5865 |
const char *sym_name; |
| 5866 |
char buf [200]; |
| 5867 |
|
| 5868 |
if (sym->attr.if_source != IFSRC_IFBODY || sym->attr.flavor != FL_PROCEDURE |
| 5869 |
|| sym->attr.generic || sym->error || sym->attr.abstract |
| 5870 |
|| sym->attr.dummy) |
| 5871 |
return; |
| 5872 |
|
| 5873 |
if (sym->binding_label) |
| 5874 |
sym_name = sym->binding_label; |
| 5875 |
else if (sym->attr.use_rename |
| 5876 |
&& sym->ns->use_stmts->rename |
| 5877 |
&& sym->ns->use_stmts->rename->local_name[0] != '\0') |
| 5878 |
sym_name = sym->ns->use_stmts->rename->local_name; |
| 5879 |
else |
| 5880 |
sym_name = sym->name; |
| 5881 |
|
| 5882 |
gsym = gfc_find_gsymbol (gfc_gsym_root, sym_name); |
| 5883 |
if (gsym && gsym->ns) |
| 5884 |
gfc_find_symbol (sym->name, gsym->ns, 0, &def_sym); |
| 5885 |
|
| 5886 |
if (!def_sym || def_sym->error || def_sym->attr.generic) |
| 5887 |
return; |
| 5888 |
|
| 5889 |
buf[0] = 0; |
| 5890 |
gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1, buf, sizeof(buf), |
| 5891 |
NULL, NULL, NULL); |
| 5892 |
if (buf[0] != 0) |
| 5893 |
{ |
| 5894 |
gfc_warning (0, "%s between %L and %L", buf, &def_sym->declared_at, |
| 5895 |
&sym->declared_at); |
| 5896 |
sym->error = 1; |
| 5897 |
def_sym->error = 1; |
| 5898 |
} |
| 5899 |
|
| 5900 |
} |
| 5901 |
|
| 5902 |
/* Do the code-walkling part for gfc_check_externals. */ |
| 5903 |
|
| 5904 |
static void |
| 5905 |
gfc_check_externals0 (gfc_namespace *ns) |
| 5906 |
{ |
| 5907 |
gfc_code_walker (&ns->code, check_externals_code, check_externals_expr, NULL); |
| 5908 |
|
| 5909 |
for (ns = ns->contained; ns; ns = ns->sibling) |
| 5910 |
{ |
| 5911 |
if (ns->code == NULL || ns->code->op != EXEC_BLOCK) |
| 5912 |
gfc_check_externals0 (ns); |
| 5913 |
} |
| 5914 |
|
| 5915 |
} |
| 5916 |
|
| 5917 |
/* Called routine. */ |
| 5918 |
|
| 5919 |
void |
| 5920 |
gfc_check_externals (gfc_namespace *ns) |
| 5921 |
{ |
| 5922 |
gfc_clear_error (); |
| 5923 |
|
| 5924 |
/* Turn errors into warnings if the user indicated this. */ |
| 5925 |
|
| 5926 |
if (!pedantic && flag_allow_argument_mismatch) |
| 5927 |
gfc_errors_to_warnings (true); |
| 5928 |
|
| 5929 |
gfc_check_externals0 (ns); |
| 5930 |
gfc_traverse_ns (ns, check_against_globals); |
| 5931 |
|
| 5932 |
gfc_errors_to_warnings (false); |
| 5933 |
} |
| 5934 |
|
| 5935 |
/* Callback function. If there is a call to a subroutine which is |
| 5936 |
neither pure nor implicit_pure, unset the implicit_pure flag for |
| 5937 |
the caller and return -1. */ |
| 5938 |
|
| 5939 |
static int |
| 5940 |
implicit_pure_call (gfc_code **c, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 5941 |
void *sym_data) |
| 5942 |
{ |
| 5943 |
gfc_code *co = *c; |
| 5944 |
gfc_symbol *caller_sym; |
| 5945 |
symbol_attribute *a; |
| 5946 |
|
| 5947 |
if (co->op != EXEC_CALL || co->resolved_sym == NULL) |
| 5948 |
return 0; |
| 5949 |
|
| 5950 |
a = &co->resolved_sym->attr; |
| 5951 |
if (a->intrinsic || a->pure || a->implicit_pure) |
| 5952 |
return 0; |
| 5953 |
|
| 5954 |
caller_sym = (gfc_symbol *) sym_data; |
| 5955 |
gfc_unset_implicit_pure (caller_sym); |
| 5956 |
return 1; |
| 5957 |
} |
| 5958 |
|
| 5959 |
/* Callback function. If there is a call to a function which is |
| 5960 |
neither pure nor implicit_pure, unset the implicit_pure flag for |
| 5961 |
the caller and return 1. */ |
| 5962 |
|
| 5963 |
static int |
| 5964 |
implicit_pure_expr (gfc_expr **e, int *walk ATTRIBUTE_UNUSED, void *sym_data) |
| 5965 |
{ |
| 5966 |
gfc_expr *expr = *e; |
| 5967 |
gfc_symbol *caller_sym; |
| 5968 |
gfc_symbol *sym; |
| 5969 |
symbol_attribute *a; |
| 5970 |
|
| 5971 |
if (expr->expr_type != EXPR_FUNCTION || expr->value.function.isym) |
| 5972 |
return 0; |
| 5973 |
|
| 5974 |
sym = expr->symtree->n.sym; |
| 5975 |
a = &sym->attr; |
| 5976 |
if (a->pure || a->implicit_pure) |
| 5977 |
return 0; |
| 5978 |
|
| 5979 |
caller_sym = (gfc_symbol *) sym_data; |
| 5980 |
gfc_unset_implicit_pure (caller_sym); |
| 5981 |
return 1; |
| 5982 |
} |
| 5983 |
|
| 5984 |
/* Go through all procedures in the namespace and unset the |
| 5985 |
implicit_pure attribute for any procedure that calls something not |
| 5986 |
pure or implicit pure. */ |
| 5987 |
|
| 5988 |
bool |
| 5989 |
gfc_fix_implicit_pure (gfc_namespace *ns) |
| 5990 |
{ |
| 5991 |
bool changed = false; |
| 5992 |
gfc_symbol *proc = ns->proc_name; |
| 5993 |
|
| 5994 |
if (proc && proc->attr.flavor == FL_PROCEDURE && proc->attr.implicit_pure |
| 5995 |
&& ns->code |
| 5996 |
&& gfc_code_walker (&ns->code, implicit_pure_call, implicit_pure_expr, |
| 5997 |
(void *) ns->proc_name)) |
| 5998 |
changed = true; |
| 5999 |
|
| 6000 |
for (ns = ns->contained; ns; ns = ns->sibling) |
| 6001 |
{ |
| 6002 |
if (gfc_fix_implicit_pure (ns)) |
| 6003 |
changed = true; |
| 6004 |
} |
| 6005 |
|
| 6006 |
return changed; |
| 6007 |
} |