Line data Source code
1 : /* Intrinsic translation
2 : Copyright (C) 2002-2026 Free Software Foundation, Inc.
3 : Contributed by Paul Brook <paul@nowt.org>
4 : and Steven Bosscher <s.bosscher@student.tudelft.nl>
5 :
6 : This file is part of GCC.
7 :
8 : GCC is free software; you can redistribute it and/or modify it under
9 : the terms of the GNU General Public License as published by the Free
10 : Software Foundation; either version 3, or (at your option) any later
11 : version.
12 :
13 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 : for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with GCC; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 : /* trans-intrinsic.cc-- generate GENERIC trees for calls to intrinsics. */
23 :
24 : #include "config.h"
25 : #include "system.h"
26 : #include "coretypes.h"
27 : #include "memmodel.h"
28 : #include "tm.h" /* For UNITS_PER_WORD. */
29 : #include "tree.h"
30 : #include "gfortran.h"
31 : #include "trans.h"
32 : #include "stringpool.h"
33 : #include "fold-const.h"
34 : #include "internal-fn.h"
35 : #include "tree-nested.h"
36 : #include "stor-layout.h"
37 : #include "toplev.h" /* For rest_of_decl_compilation. */
38 : #include "arith.h"
39 : #include "trans-const.h"
40 : #include "trans-types.h"
41 : #include "trans-array.h"
42 : #include "trans-descriptor.h"
43 : #include "dependency.h" /* For CAF array alias analysis. */
44 : #include "attribs.h"
45 : #include "realmpfr.h"
46 : #include "constructor.h"
47 :
48 : /* This maps Fortran intrinsic math functions to external library or GCC
49 : builtin functions. */
50 : typedef struct GTY(()) gfc_intrinsic_map_t {
51 : /* The explicit enum is required to work around inadequacies in the
52 : garbage collection/gengtype parsing mechanism. */
53 : enum gfc_isym_id id;
54 :
55 : /* Enum value from the "language-independent", aka C-centric, part
56 : of gcc, or END_BUILTINS of no such value set. */
57 : enum built_in_function float_built_in;
58 : enum built_in_function double_built_in;
59 : enum built_in_function long_double_built_in;
60 : enum built_in_function complex_float_built_in;
61 : enum built_in_function complex_double_built_in;
62 : enum built_in_function complex_long_double_built_in;
63 :
64 : /* True if the naming pattern is to prepend "c" for complex and
65 : append "f" for kind=4. False if the naming pattern is to
66 : prepend "_gfortran_" and append "[rc](4|8|10|16)". */
67 : bool libm_name;
68 :
69 : /* True if a complex version of the function exists. */
70 : bool complex_available;
71 :
72 : /* True if the function should be marked const. */
73 : bool is_constant;
74 :
75 : /* The base library name of this function. */
76 : const char *name;
77 :
78 : /* Cache decls created for the various operand types. */
79 : tree real4_decl;
80 : tree real8_decl;
81 : tree real10_decl;
82 : tree real16_decl;
83 : tree complex4_decl;
84 : tree complex8_decl;
85 : tree complex10_decl;
86 : tree complex16_decl;
87 : }
88 : gfc_intrinsic_map_t;
89 :
90 : /* ??? The NARGS==1 hack here is based on the fact that (c99 at least)
91 : defines complex variants of all of the entries in mathbuiltins.def
92 : except for atan2. */
93 : #define DEFINE_MATH_BUILTIN(ID, NAME, ARGTYPE) \
94 : { GFC_ISYM_ ## ID, BUILT_IN_ ## ID ## F, BUILT_IN_ ## ID, \
95 : BUILT_IN_ ## ID ## L, END_BUILTINS, END_BUILTINS, END_BUILTINS, \
96 : true, false, true, NAME, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, \
97 : NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE},
98 :
99 : #define DEFINE_MATH_BUILTIN_C(ID, NAME, ARGTYPE) \
100 : { GFC_ISYM_ ## ID, BUILT_IN_ ## ID ## F, BUILT_IN_ ## ID, \
101 : BUILT_IN_ ## ID ## L, BUILT_IN_C ## ID ## F, BUILT_IN_C ## ID, \
102 : BUILT_IN_C ## ID ## L, true, true, true, NAME, NULL_TREE, NULL_TREE, \
103 : NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE},
104 :
105 : #define LIB_FUNCTION(ID, NAME, HAVE_COMPLEX) \
106 : { GFC_ISYM_ ## ID, END_BUILTINS, END_BUILTINS, END_BUILTINS, \
107 : END_BUILTINS, END_BUILTINS, END_BUILTINS, \
108 : false, HAVE_COMPLEX, true, NAME, NULL_TREE, NULL_TREE, NULL_TREE, \
109 : NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE }
110 :
111 : #define OTHER_BUILTIN(ID, NAME, TYPE, CONST) \
112 : { GFC_ISYM_NONE, BUILT_IN_ ## ID ## F, BUILT_IN_ ## ID, \
113 : BUILT_IN_ ## ID ## L, END_BUILTINS, END_BUILTINS, END_BUILTINS, \
114 : true, false, CONST, NAME, NULL_TREE, NULL_TREE, \
115 : NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE},
116 :
117 : static GTY(()) gfc_intrinsic_map_t gfc_intrinsic_map[] =
118 : {
119 : /* Functions built into gcc itself (DEFINE_MATH_BUILTIN and
120 : DEFINE_MATH_BUILTIN_C), then the built-ins that don't correspond
121 : to any GFC_ISYM id directly, which use the OTHER_BUILTIN macro. */
122 : #include "mathbuiltins.def"
123 :
124 : /* Functions in libgfortran. */
125 : LIB_FUNCTION (ERFC_SCALED, "erfc_scaled", false),
126 : LIB_FUNCTION (SIND, "sind", false),
127 : LIB_FUNCTION (COSD, "cosd", false),
128 : LIB_FUNCTION (TAND, "tand", false),
129 :
130 : /* End the list. */
131 : LIB_FUNCTION (NONE, NULL, false)
132 :
133 : };
134 : #undef OTHER_BUILTIN
135 : #undef LIB_FUNCTION
136 : #undef DEFINE_MATH_BUILTIN
137 : #undef DEFINE_MATH_BUILTIN_C
138 :
139 :
140 : enum rounding_mode { RND_ROUND, RND_TRUNC, RND_CEIL, RND_FLOOR };
141 :
142 :
143 : /* Find the correct variant of a given builtin from its argument. */
144 : static tree
145 11472 : builtin_decl_for_precision (enum built_in_function base_built_in,
146 : int precision)
147 : {
148 11472 : enum built_in_function i = END_BUILTINS;
149 :
150 11472 : gfc_intrinsic_map_t *m;
151 491199 : for (m = gfc_intrinsic_map; m->double_built_in != base_built_in ; m++)
152 : ;
153 :
154 11472 : if (precision == TYPE_PRECISION (float_type_node))
155 5832 : i = m->float_built_in;
156 5640 : else if (precision == TYPE_PRECISION (double_type_node))
157 : i = m->double_built_in;
158 1695 : else if (precision == TYPE_PRECISION (long_double_type_node)
159 1695 : && (!gfc_real16_is_float128
160 1571 : || long_double_type_node != gfc_float128_type_node))
161 1571 : i = m->long_double_built_in;
162 124 : else if (precision == TYPE_PRECISION (gfc_float128_type_node))
163 : {
164 : /* Special treatment, because it is not exactly a built-in, but
165 : a library function. */
166 124 : return m->real16_decl;
167 : }
168 :
169 11348 : return (i == END_BUILTINS ? NULL_TREE : builtin_decl_explicit (i));
170 : }
171 :
172 :
173 : tree
174 10433 : gfc_builtin_decl_for_float_kind (enum built_in_function double_built_in,
175 : int kind)
176 : {
177 10433 : int i = gfc_validate_kind (BT_REAL, kind, false);
178 :
179 10433 : if (gfc_real_kinds[i].c_float128)
180 : {
181 : /* For _Float128, the story is a bit different, because we return
182 : a decl to a library function rather than a built-in. */
183 : gfc_intrinsic_map_t *m;
184 36328 : for (m = gfc_intrinsic_map; m->double_built_in != double_built_in ; m++)
185 : ;
186 :
187 905 : return m->real16_decl;
188 : }
189 :
190 9528 : return builtin_decl_for_precision (double_built_in,
191 9528 : gfc_real_kinds[i].mode_precision);
192 : }
193 :
194 :
195 : /* Evaluate the arguments to an intrinsic function. The value
196 : of NARGS may be less than the actual number of arguments in EXPR
197 : to allow optional "KIND" arguments that are not included in the
198 : generated code to be ignored. */
199 :
200 : static void
201 82852 : gfc_conv_intrinsic_function_args (gfc_se *se, gfc_expr *expr,
202 : tree *argarray, int nargs)
203 : {
204 82852 : gfc_actual_arglist *actual;
205 82852 : gfc_expr *e;
206 82852 : gfc_intrinsic_arg *formal;
207 82852 : gfc_se argse;
208 82852 : int curr_arg;
209 :
210 82852 : formal = expr->value.function.isym->formal;
211 82852 : actual = expr->value.function.actual;
212 :
213 186530 : for (curr_arg = 0; curr_arg < nargs; curr_arg++,
214 63852 : actual = actual->next,
215 103678 : formal = formal ? formal->next : NULL)
216 : {
217 103678 : gcc_assert (actual);
218 103678 : e = actual->expr;
219 : /* Skip omitted optional arguments. */
220 103678 : if (!e)
221 : {
222 31 : --curr_arg;
223 31 : continue;
224 : }
225 :
226 : /* Evaluate the parameter. This will substitute scalarized
227 : references automatically. */
228 103647 : gfc_init_se (&argse, se);
229 :
230 103647 : if (e->ts.type == BT_CHARACTER)
231 : {
232 9642 : gfc_conv_expr (&argse, e);
233 9642 : gfc_conv_string_parameter (&argse);
234 9642 : argarray[curr_arg++] = argse.string_length;
235 9642 : gcc_assert (curr_arg < nargs);
236 : }
237 : else
238 94005 : gfc_conv_expr_val (&argse, e);
239 :
240 : /* If an optional argument is itself an optional dummy argument,
241 : check its presence and substitute a null if absent. */
242 103647 : if (e->expr_type == EXPR_VARIABLE
243 52784 : && e->symtree->n.sym->attr.optional
244 203 : && formal
245 153 : && formal->optional)
246 80 : gfc_conv_missing_dummy (&argse, e, formal->ts, 0);
247 :
248 103647 : gfc_add_block_to_block (&se->pre, &argse.pre);
249 103647 : gfc_add_block_to_block (&se->post, &argse.post);
250 103647 : argarray[curr_arg] = argse.expr;
251 : }
252 82852 : }
253 :
254 : /* Count the number of actual arguments to the intrinsic function EXPR
255 : including any "hidden" string length arguments. */
256 :
257 : static unsigned int
258 57478 : gfc_intrinsic_argument_list_length (gfc_expr *expr)
259 : {
260 57478 : int n = 0;
261 57478 : gfc_actual_arglist *actual;
262 :
263 130218 : for (actual = expr->value.function.actual; actual; actual = actual->next)
264 : {
265 72740 : if (!actual->expr)
266 6382 : continue;
267 :
268 66358 : if (actual->expr->ts.type == BT_CHARACTER)
269 4551 : n += 2;
270 : else
271 61807 : n++;
272 : }
273 :
274 57478 : return n;
275 : }
276 :
277 :
278 : /* Conversions between different types are output by the frontend as
279 : intrinsic functions. We implement these directly with inline code. */
280 :
281 : static void
282 41275 : gfc_conv_intrinsic_conversion (gfc_se * se, gfc_expr * expr)
283 : {
284 41275 : tree type;
285 41275 : tree *args;
286 41275 : int nargs;
287 :
288 41275 : nargs = gfc_intrinsic_argument_list_length (expr);
289 41275 : args = XALLOCAVEC (tree, nargs);
290 :
291 : /* Evaluate all the arguments passed. Whilst we're only interested in the
292 : first one here, there are other parts of the front-end that assume this
293 : and will trigger an ICE if it's not the case. */
294 41275 : type = gfc_typenode_for_spec (&expr->ts);
295 41275 : gcc_assert (expr->value.function.actual->expr);
296 41275 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
297 :
298 : /* Conversion between character kinds involves a call to a library
299 : function. */
300 41275 : if (expr->ts.type == BT_CHARACTER)
301 : {
302 248 : tree fndecl, var, addr, tmp;
303 :
304 248 : if (expr->ts.kind == 1
305 97 : && expr->value.function.actual->expr->ts.kind == 4)
306 97 : fndecl = gfor_fndecl_convert_char4_to_char1;
307 151 : else if (expr->ts.kind == 4
308 151 : && expr->value.function.actual->expr->ts.kind == 1)
309 151 : fndecl = gfor_fndecl_convert_char1_to_char4;
310 : else
311 0 : gcc_unreachable ();
312 :
313 : /* Create the variable storing the converted value. */
314 248 : type = gfc_get_pchar_type (expr->ts.kind);
315 248 : var = gfc_create_var (type, "str");
316 248 : addr = gfc_build_addr_expr (build_pointer_type (type), var);
317 :
318 : /* Call the library function that will perform the conversion. */
319 248 : gcc_assert (nargs >= 2);
320 248 : tmp = build_call_expr_loc (input_location,
321 : fndecl, 3, addr, args[0], args[1]);
322 248 : gfc_add_expr_to_block (&se->pre, tmp);
323 :
324 : /* Free the temporary afterwards. */
325 248 : tmp = gfc_call_free (var);
326 248 : gfc_add_expr_to_block (&se->post, tmp);
327 :
328 248 : se->expr = var;
329 248 : se->string_length = args[0];
330 :
331 248 : return;
332 : }
333 :
334 : /* Conversion from complex to non-complex involves taking the real
335 : component of the value. */
336 41027 : if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
337 41027 : && expr->ts.type != BT_COMPLEX)
338 : {
339 583 : tree artype;
340 :
341 583 : artype = TREE_TYPE (TREE_TYPE (args[0]));
342 583 : args[0] = fold_build1_loc (input_location, REALPART_EXPR, artype,
343 : args[0]);
344 : }
345 :
346 41027 : se->expr = convert (type, args[0]);
347 : }
348 :
349 : /* This is needed because the gcc backend only implements
350 : FIX_TRUNC_EXPR, which is the same as INT() in Fortran.
351 : FLOOR(x) = INT(x) <= x ? INT(x) : INT(x) - 1
352 : Similarly for CEILING. */
353 :
354 : static tree
355 132 : build_fixbound_expr (stmtblock_t * pblock, tree arg, tree type, int up)
356 : {
357 132 : tree tmp;
358 132 : tree cond;
359 132 : tree argtype;
360 132 : tree intval;
361 :
362 132 : argtype = TREE_TYPE (arg);
363 132 : arg = gfc_evaluate_now (arg, pblock);
364 :
365 132 : intval = convert (type, arg);
366 132 : intval = gfc_evaluate_now (intval, pblock);
367 :
368 132 : tmp = convert (argtype, intval);
369 248 : cond = fold_build2_loc (input_location, up ? GE_EXPR : LE_EXPR,
370 : logical_type_node, tmp, arg);
371 :
372 248 : tmp = fold_build2_loc (input_location, up ? PLUS_EXPR : MINUS_EXPR, type,
373 : intval, build_int_cst (type, 1));
374 132 : tmp = fold_build3_loc (input_location, COND_EXPR, type, cond, intval, tmp);
375 132 : return tmp;
376 : }
377 :
378 :
379 : /* Round to nearest integer, away from zero. */
380 :
381 : static tree
382 516 : build_round_expr (tree arg, tree restype)
383 : {
384 516 : tree argtype;
385 516 : tree fn;
386 516 : int argprec, resprec;
387 :
388 516 : argtype = TREE_TYPE (arg);
389 516 : argprec = TYPE_PRECISION (argtype);
390 516 : resprec = TYPE_PRECISION (restype);
391 :
392 : /* Depending on the type of the result, choose the int intrinsic (iround,
393 : available only as a builtin, therefore cannot use it for _Float128), long
394 : int intrinsic (lround family) or long long intrinsic (llround). If we
395 : don't have an appropriate function that converts directly to the integer
396 : type (such as kind == 16), just use ROUND, and then convert the result to
397 : an integer. We might also need to convert the result afterwards. */
398 516 : if (resprec <= INT_TYPE_SIZE
399 516 : && argprec <= TYPE_PRECISION (long_double_type_node))
400 458 : fn = builtin_decl_for_precision (BUILT_IN_IROUND, argprec);
401 62 : else if (resprec <= LONG_TYPE_SIZE)
402 46 : fn = builtin_decl_for_precision (BUILT_IN_LROUND, argprec);
403 12 : else if (resprec <= LONG_LONG_TYPE_SIZE)
404 0 : fn = builtin_decl_for_precision (BUILT_IN_LLROUND, argprec);
405 12 : else if (resprec >= argprec)
406 12 : fn = builtin_decl_for_precision (BUILT_IN_ROUND, argprec);
407 : else
408 0 : gcc_unreachable ();
409 :
410 516 : return convert (restype, build_call_expr_loc (input_location,
411 516 : fn, 1, arg));
412 : }
413 :
414 :
415 : /* Convert a real to an integer using a specific rounding mode.
416 : Ideally we would just build the corresponding GENERIC node,
417 : however the RTL expander only actually supports FIX_TRUNC_EXPR. */
418 :
419 : static tree
420 1603 : build_fix_expr (stmtblock_t * pblock, tree arg, tree type,
421 : enum rounding_mode op)
422 : {
423 1603 : switch (op)
424 : {
425 116 : case RND_FLOOR:
426 116 : return build_fixbound_expr (pblock, arg, type, 0);
427 :
428 16 : case RND_CEIL:
429 16 : return build_fixbound_expr (pblock, arg, type, 1);
430 :
431 162 : case RND_ROUND:
432 162 : return build_round_expr (arg, type);
433 :
434 1309 : case RND_TRUNC:
435 1309 : return fold_build1_loc (input_location, FIX_TRUNC_EXPR, type, arg);
436 :
437 0 : default:
438 0 : gcc_unreachable ();
439 : }
440 : }
441 :
442 :
443 : /* Round a real value using the specified rounding mode.
444 : We use a temporary integer of that same kind size as the result.
445 : Values larger than those that can be represented by this kind are
446 : unchanged, as they will not be accurate enough to represent the
447 : rounding.
448 : huge = HUGE (KIND (a))
449 : aint (a) = ((a > huge) || (a < -huge)) ? a : (real)(int)a
450 : */
451 :
452 : static void
453 220 : gfc_conv_intrinsic_aint (gfc_se * se, gfc_expr * expr, enum rounding_mode op)
454 : {
455 220 : tree type;
456 220 : tree itype;
457 220 : tree arg[2];
458 220 : tree tmp;
459 220 : tree cond;
460 220 : tree decl;
461 220 : mpfr_t huge;
462 220 : int n, nargs;
463 220 : int kind;
464 :
465 220 : kind = expr->ts.kind;
466 220 : nargs = gfc_intrinsic_argument_list_length (expr);
467 :
468 220 : decl = NULL_TREE;
469 : /* We have builtin functions for some cases. */
470 220 : switch (op)
471 : {
472 74 : case RND_ROUND:
473 74 : decl = gfc_builtin_decl_for_float_kind (BUILT_IN_ROUND, kind);
474 74 : break;
475 :
476 146 : case RND_TRUNC:
477 146 : decl = gfc_builtin_decl_for_float_kind (BUILT_IN_TRUNC, kind);
478 146 : break;
479 :
480 0 : default:
481 0 : gcc_unreachable ();
482 : }
483 :
484 : /* Evaluate the argument. */
485 220 : gcc_assert (expr->value.function.actual->expr);
486 220 : gfc_conv_intrinsic_function_args (se, expr, arg, nargs);
487 :
488 : /* Use a builtin function if one exists. */
489 220 : if (decl != NULL_TREE)
490 : {
491 220 : se->expr = build_call_expr_loc (input_location, decl, 1, arg[0]);
492 220 : return;
493 : }
494 :
495 : /* This code is probably redundant, but we'll keep it lying around just
496 : in case. */
497 0 : type = gfc_typenode_for_spec (&expr->ts);
498 0 : arg[0] = gfc_evaluate_now (arg[0], &se->pre);
499 :
500 : /* Test if the value is too large to handle sensibly. */
501 0 : gfc_set_model_kind (kind);
502 0 : mpfr_init (huge);
503 0 : n = gfc_validate_kind (BT_INTEGER, kind, false);
504 0 : mpfr_set_z (huge, gfc_integer_kinds[n].huge, GFC_RND_MODE);
505 0 : tmp = gfc_conv_mpfr_to_tree (huge, kind, 0);
506 0 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, arg[0],
507 : tmp);
508 :
509 0 : mpfr_neg (huge, huge, GFC_RND_MODE);
510 0 : tmp = gfc_conv_mpfr_to_tree (huge, kind, 0);
511 0 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node, arg[0],
512 : tmp);
513 0 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
514 : cond, tmp);
515 0 : itype = gfc_get_int_type (kind);
516 :
517 0 : tmp = build_fix_expr (&se->pre, arg[0], itype, op);
518 0 : tmp = convert (type, tmp);
519 0 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond, tmp,
520 : arg[0]);
521 0 : mpfr_clear (huge);
522 : }
523 :
524 :
525 : /* Convert to an integer using the specified rounding mode. */
526 :
527 : static void
528 3130 : gfc_conv_intrinsic_int (gfc_se * se, gfc_expr * expr, enum rounding_mode op)
529 : {
530 3130 : tree type;
531 3130 : tree *args;
532 3130 : int nargs;
533 :
534 3130 : nargs = gfc_intrinsic_argument_list_length (expr);
535 3130 : args = XALLOCAVEC (tree, nargs);
536 :
537 : /* Evaluate the argument, we process all arguments even though we only
538 : use the first one for code generation purposes. */
539 3130 : type = gfc_typenode_for_spec (&expr->ts);
540 3130 : gcc_assert (expr->value.function.actual->expr);
541 3130 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
542 :
543 3130 : if (TREE_CODE (TREE_TYPE (args[0])) == INTEGER_TYPE)
544 : {
545 : /* Conversion to a different integer kind. */
546 1527 : se->expr = convert (type, args[0]);
547 : }
548 : else
549 : {
550 : /* Conversion from complex to non-complex involves taking the real
551 : component of the value. */
552 1603 : if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE
553 1603 : && expr->ts.type != BT_COMPLEX)
554 : {
555 192 : tree artype;
556 :
557 192 : artype = TREE_TYPE (TREE_TYPE (args[0]));
558 192 : args[0] = fold_build1_loc (input_location, REALPART_EXPR, artype,
559 : args[0]);
560 : }
561 :
562 1603 : se->expr = build_fix_expr (&se->pre, args[0], type, op);
563 : }
564 3130 : }
565 :
566 :
567 : /* Get the imaginary component of a value. */
568 :
569 : static void
570 440 : gfc_conv_intrinsic_imagpart (gfc_se * se, gfc_expr * expr)
571 : {
572 440 : tree arg;
573 :
574 440 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
575 440 : se->expr = fold_build1_loc (input_location, IMAGPART_EXPR,
576 440 : TREE_TYPE (TREE_TYPE (arg)), arg);
577 440 : }
578 :
579 :
580 : /* Get the complex conjugate of a value. */
581 :
582 : static void
583 257 : gfc_conv_intrinsic_conjg (gfc_se * se, gfc_expr * expr)
584 : {
585 257 : tree arg;
586 :
587 257 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
588 257 : se->expr = fold_build1_loc (input_location, CONJ_EXPR, TREE_TYPE (arg), arg);
589 257 : }
590 :
591 :
592 :
593 : static tree
594 676200 : define_quad_builtin (const char *name, tree type, bool is_const)
595 : {
596 676200 : tree fndecl;
597 676200 : fndecl = build_decl (input_location, FUNCTION_DECL, get_identifier (name),
598 : type);
599 :
600 : /* Mark the decl as external. */
601 676200 : DECL_EXTERNAL (fndecl) = 1;
602 676200 : TREE_PUBLIC (fndecl) = 1;
603 :
604 : /* Mark it __attribute__((const)). */
605 676200 : TREE_READONLY (fndecl) = is_const;
606 :
607 676200 : rest_of_decl_compilation (fndecl, 1, 0);
608 :
609 676200 : return fndecl;
610 : }
611 :
612 : /* Add SIMD attribute for FNDECL built-in if the built-in
613 : name is in VECTORIZED_BUILTINS. */
614 :
615 : static void
616 46513830 : add_simd_flag_for_built_in (tree fndecl)
617 : {
618 46513830 : if (gfc_vectorized_builtins == NULL
619 18576740 : || fndecl == NULL_TREE)
620 38484900 : return;
621 :
622 8028930 : const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
623 8028930 : int *clauses = gfc_vectorized_builtins->get (name);
624 8028930 : if (clauses)
625 : {
626 5037148 : for (unsigned i = 0; i < 3; i++)
627 3777861 : if (*clauses & (1 << i))
628 : {
629 1259292 : gfc_simd_clause simd_type = (gfc_simd_clause)*clauses;
630 1259292 : tree omp_clause = NULL_TREE;
631 1259292 : if (simd_type == SIMD_NONE)
632 : ; /* No SIMD clause. */
633 : else
634 : {
635 1259292 : omp_clause_code code
636 : = (simd_type == SIMD_INBRANCH
637 1259292 : ? OMP_CLAUSE_INBRANCH : OMP_CLAUSE_NOTINBRANCH);
638 1259292 : omp_clause = build_omp_clause (UNKNOWN_LOCATION, code);
639 1259292 : omp_clause = build_tree_list (NULL_TREE, omp_clause);
640 : }
641 :
642 1259292 : DECL_ATTRIBUTES (fndecl)
643 2518584 : = tree_cons (get_identifier ("omp declare simd"), omp_clause,
644 1259292 : DECL_ATTRIBUTES (fndecl));
645 : }
646 : }
647 : }
648 :
649 : /* Set SIMD attribute to all built-in functions that are mentioned
650 : in gfc_vectorized_builtins vector. */
651 :
652 : void
653 78837 : gfc_adjust_builtins (void)
654 : {
655 78837 : gfc_intrinsic_map_t *m;
656 4730220 : for (m = gfc_intrinsic_map;
657 4730220 : m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
658 : {
659 4651383 : add_simd_flag_for_built_in (m->real4_decl);
660 4651383 : add_simd_flag_for_built_in (m->complex4_decl);
661 4651383 : add_simd_flag_for_built_in (m->real8_decl);
662 4651383 : add_simd_flag_for_built_in (m->complex8_decl);
663 4651383 : add_simd_flag_for_built_in (m->real10_decl);
664 4651383 : add_simd_flag_for_built_in (m->complex10_decl);
665 4651383 : add_simd_flag_for_built_in (m->real16_decl);
666 4651383 : add_simd_flag_for_built_in (m->complex16_decl);
667 4651383 : add_simd_flag_for_built_in (m->real16_decl);
668 4651383 : add_simd_flag_for_built_in (m->complex16_decl);
669 : }
670 :
671 : /* Release all strings. */
672 78837 : if (gfc_vectorized_builtins != NULL)
673 : {
674 1700035 : for (hash_map<nofree_string_hash, int>::iterator it
675 31486 : = gfc_vectorized_builtins->begin ();
676 1731521 : it != gfc_vectorized_builtins->end (); ++it)
677 1700035 : free (const_cast<char *> ((*it).first));
678 :
679 62972 : delete gfc_vectorized_builtins;
680 31486 : gfc_vectorized_builtins = NULL;
681 : }
682 78837 : }
683 :
684 : /* Initialize function decls for library functions. The external functions
685 : are created as required. Builtin functions are added here. */
686 :
687 : void
688 32200 : gfc_build_intrinsic_lib_fndecls (void)
689 : {
690 32200 : gfc_intrinsic_map_t *m;
691 32200 : tree quad_decls[END_BUILTINS + 1];
692 :
693 32200 : if (gfc_real16_is_float128)
694 : {
695 : /* If we have soft-float types, we create the decls for their
696 : C99-like library functions. For now, we only handle _Float128
697 : q-suffixed or IEC 60559 f128-suffixed functions. */
698 :
699 32200 : tree type, complex_type, func_1, func_2, func_3, func_cabs, func_frexp;
700 32200 : tree func_iround, func_lround, func_llround, func_scalbn, func_cpow;
701 :
702 32200 : memset (quad_decls, 0, sizeof(tree) * (END_BUILTINS + 1));
703 :
704 32200 : type = gfc_float128_type_node;
705 32200 : complex_type = gfc_complex_float128_type_node;
706 : /* type (*) (type) */
707 32200 : func_1 = build_function_type_list (type, type, NULL_TREE);
708 : /* int (*) (type) */
709 32200 : func_iround = build_function_type_list (integer_type_node,
710 : type, NULL_TREE);
711 : /* long (*) (type) */
712 32200 : func_lround = build_function_type_list (long_integer_type_node,
713 : type, NULL_TREE);
714 : /* long long (*) (type) */
715 32200 : func_llround = build_function_type_list (long_long_integer_type_node,
716 : type, NULL_TREE);
717 : /* type (*) (type, type) */
718 32200 : func_2 = build_function_type_list (type, type, type, NULL_TREE);
719 : /* type (*) (type, type, type) */
720 32200 : func_3 = build_function_type_list (type, type, type, type, NULL_TREE);
721 : /* type (*) (type, &int) */
722 32200 : func_frexp
723 32200 : = build_function_type_list (type,
724 : type,
725 : build_pointer_type (integer_type_node),
726 : NULL_TREE);
727 : /* type (*) (type, int) */
728 32200 : func_scalbn = build_function_type_list (type,
729 : type, integer_type_node, NULL_TREE);
730 : /* type (*) (complex type) */
731 32200 : func_cabs = build_function_type_list (type, complex_type, NULL_TREE);
732 : /* complex type (*) (complex type, complex type) */
733 32200 : func_cpow
734 32200 : = build_function_type_list (complex_type,
735 : complex_type, complex_type, NULL_TREE);
736 :
737 : #define DEFINE_MATH_BUILTIN(ID, NAME, ARGTYPE)
738 : #define DEFINE_MATH_BUILTIN_C(ID, NAME, ARGTYPE)
739 : #define LIB_FUNCTION(ID, NAME, HAVE_COMPLEX)
740 :
741 : /* Only these built-ins are actually needed here. These are used directly
742 : from the code, when calling builtin_decl_for_precision() or
743 : builtin_decl_for_float_type(). The others are all constructed by
744 : gfc_get_intrinsic_lib_fndecl(). */
745 : #define OTHER_BUILTIN(ID, NAME, TYPE, CONST) \
746 : quad_decls[BUILT_IN_ ## ID] \
747 : = define_quad_builtin (gfc_real16_use_iec_60559 \
748 : ? NAME "f128" : NAME "q", func_ ## TYPE, \
749 : CONST);
750 :
751 : #include "mathbuiltins.def"
752 :
753 : #undef OTHER_BUILTIN
754 : #undef LIB_FUNCTION
755 : #undef DEFINE_MATH_BUILTIN
756 : #undef DEFINE_MATH_BUILTIN_C
757 :
758 : /* There is one built-in we defined manually, because it gets called
759 : with builtin_decl_for_precision() or builtin_decl_for_float_type()
760 : even though it is not an OTHER_BUILTIN: it is SQRT. */
761 32200 : quad_decls[BUILT_IN_SQRT]
762 32200 : = define_quad_builtin (gfc_real16_use_iec_60559
763 : ? "sqrtf128" : "sqrtq", func_1, true);
764 : }
765 :
766 : /* Add GCC builtin functions. */
767 1932000 : for (m = gfc_intrinsic_map;
768 1932000 : m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
769 : {
770 1899800 : if (m->float_built_in != END_BUILTINS)
771 1771000 : m->real4_decl = builtin_decl_explicit (m->float_built_in);
772 1899800 : if (m->complex_float_built_in != END_BUILTINS)
773 515200 : m->complex4_decl = builtin_decl_explicit (m->complex_float_built_in);
774 1899800 : if (m->double_built_in != END_BUILTINS)
775 1771000 : m->real8_decl = builtin_decl_explicit (m->double_built_in);
776 1899800 : if (m->complex_double_built_in != END_BUILTINS)
777 515200 : m->complex8_decl = builtin_decl_explicit (m->complex_double_built_in);
778 :
779 : /* If real(kind=10) exists, it is always long double. */
780 1899800 : if (m->long_double_built_in != END_BUILTINS)
781 1771000 : m->real10_decl = builtin_decl_explicit (m->long_double_built_in);
782 1899800 : if (m->complex_long_double_built_in != END_BUILTINS)
783 515200 : m->complex10_decl
784 515200 : = builtin_decl_explicit (m->complex_long_double_built_in);
785 :
786 1899800 : if (!gfc_real16_is_float128)
787 : {
788 0 : if (m->long_double_built_in != END_BUILTINS)
789 0 : m->real16_decl = builtin_decl_explicit (m->long_double_built_in);
790 0 : if (m->complex_long_double_built_in != END_BUILTINS)
791 0 : m->complex16_decl
792 0 : = builtin_decl_explicit (m->complex_long_double_built_in);
793 : }
794 1899800 : else if (quad_decls[m->double_built_in] != NULL_TREE)
795 : {
796 : /* Quad-precision function calls are constructed when first
797 : needed by builtin_decl_for_precision(), except for those
798 : that will be used directly (define by OTHER_BUILTIN). */
799 676200 : m->real16_decl = quad_decls[m->double_built_in];
800 : }
801 1223600 : else if (quad_decls[m->complex_double_built_in] != NULL_TREE)
802 : {
803 : /* Same thing for the complex ones. */
804 0 : m->complex16_decl = quad_decls[m->double_built_in];
805 : }
806 : }
807 32200 : }
808 :
809 :
810 : /* Create a fndecl for a simple intrinsic library function. */
811 :
812 : static tree
813 4547 : gfc_get_intrinsic_lib_fndecl (gfc_intrinsic_map_t * m, gfc_expr * expr)
814 : {
815 4547 : tree type;
816 4547 : vec<tree, va_gc> *argtypes;
817 4547 : tree fndecl;
818 4547 : gfc_actual_arglist *actual;
819 4547 : tree *pdecl;
820 4547 : gfc_typespec *ts;
821 4547 : char name[GFC_MAX_SYMBOL_LEN + 3];
822 :
823 4547 : ts = &expr->ts;
824 4547 : if (ts->type == BT_REAL)
825 : {
826 3685 : switch (ts->kind)
827 : {
828 1308 : case 4:
829 1308 : pdecl = &m->real4_decl;
830 1308 : break;
831 1307 : case 8:
832 1307 : pdecl = &m->real8_decl;
833 1307 : break;
834 600 : case 10:
835 600 : pdecl = &m->real10_decl;
836 600 : break;
837 470 : case 16:
838 470 : pdecl = &m->real16_decl;
839 470 : break;
840 0 : default:
841 0 : gcc_unreachable ();
842 : }
843 : }
844 862 : else if (ts->type == BT_COMPLEX)
845 : {
846 862 : gcc_assert (m->complex_available);
847 :
848 862 : switch (ts->kind)
849 : {
850 386 : case 4:
851 386 : pdecl = &m->complex4_decl;
852 386 : break;
853 405 : case 8:
854 405 : pdecl = &m->complex8_decl;
855 405 : break;
856 51 : case 10:
857 51 : pdecl = &m->complex10_decl;
858 51 : break;
859 20 : case 16:
860 20 : pdecl = &m->complex16_decl;
861 20 : break;
862 0 : default:
863 0 : gcc_unreachable ();
864 : }
865 : }
866 : else
867 0 : gcc_unreachable ();
868 :
869 4547 : if (*pdecl)
870 : return *pdecl;
871 :
872 409 : if (m->libm_name)
873 : {
874 178 : int n = gfc_validate_kind (BT_REAL, ts->kind, false);
875 178 : if (gfc_real_kinds[n].c_float)
876 0 : snprintf (name, sizeof (name), "%s%s%s",
877 0 : ts->type == BT_COMPLEX ? "c" : "", m->name, "f");
878 178 : else if (gfc_real_kinds[n].c_double)
879 0 : snprintf (name, sizeof (name), "%s%s",
880 0 : ts->type == BT_COMPLEX ? "c" : "", m->name);
881 178 : else if (gfc_real_kinds[n].c_long_double)
882 0 : snprintf (name, sizeof (name), "%s%s%s",
883 0 : ts->type == BT_COMPLEX ? "c" : "", m->name, "l");
884 178 : else if (gfc_real_kinds[n].c_float128)
885 178 : snprintf (name, sizeof (name), "%s%s%s",
886 178 : ts->type == BT_COMPLEX ? "c" : "", m->name,
887 178 : gfc_real_kinds[n].use_iec_60559 ? "f128" : "q");
888 : else
889 0 : gcc_unreachable ();
890 : }
891 : else
892 : {
893 462 : snprintf (name, sizeof (name), PREFIX ("%s_%c%d"), m->name,
894 231 : ts->type == BT_COMPLEX ? 'c' : 'r',
895 : gfc_type_abi_kind (ts));
896 : }
897 :
898 409 : argtypes = NULL;
899 838 : for (actual = expr->value.function.actual; actual; actual = actual->next)
900 : {
901 429 : type = gfc_typenode_for_spec (&actual->expr->ts);
902 429 : vec_safe_push (argtypes, type);
903 : }
904 1227 : type = build_function_type_vec (gfc_typenode_for_spec (ts), argtypes);
905 409 : fndecl = build_decl (input_location,
906 : FUNCTION_DECL, get_identifier (name), type);
907 :
908 : /* Mark the decl as external. */
909 409 : DECL_EXTERNAL (fndecl) = 1;
910 409 : TREE_PUBLIC (fndecl) = 1;
911 :
912 : /* Mark it __attribute__((const)), if possible. */
913 409 : TREE_READONLY (fndecl) = m->is_constant;
914 :
915 409 : rest_of_decl_compilation (fndecl, 1, 0);
916 :
917 409 : (*pdecl) = fndecl;
918 409 : return fndecl;
919 : }
920 :
921 :
922 : /* Convert an intrinsic function into an external or builtin call. */
923 :
924 : static void
925 3929 : gfc_conv_intrinsic_lib_function (gfc_se * se, gfc_expr * expr)
926 : {
927 3929 : gfc_intrinsic_map_t *m;
928 3929 : tree fndecl;
929 3929 : tree rettype;
930 3929 : tree *args;
931 3929 : unsigned int num_args;
932 3929 : gfc_isym_id id;
933 :
934 3929 : id = expr->value.function.isym->id;
935 : /* Find the entry for this function. */
936 82799 : for (m = gfc_intrinsic_map;
937 82799 : m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
938 : {
939 82799 : if (id == m->id)
940 : break;
941 : }
942 :
943 3929 : if (m->id == GFC_ISYM_NONE)
944 : {
945 0 : gfc_internal_error ("Intrinsic function %qs (%d) not recognized",
946 : expr->value.function.name, id);
947 : }
948 :
949 : /* Get the decl and generate the call. */
950 3929 : num_args = gfc_intrinsic_argument_list_length (expr);
951 3929 : args = XALLOCAVEC (tree, num_args);
952 :
953 3929 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
954 3929 : fndecl = gfc_get_intrinsic_lib_fndecl (m, expr);
955 3929 : rettype = TREE_TYPE (TREE_TYPE (fndecl));
956 :
957 3929 : fndecl = build_addr (fndecl);
958 3929 : se->expr = build_call_array_loc (input_location, rettype, fndecl, num_args, args);
959 3929 : }
960 :
961 :
962 : /* If bounds-checking is enabled, create code to verify at runtime that the
963 : string lengths for both expressions are the same (needed for e.g. MERGE).
964 : If bounds-checking is not enabled, does nothing. */
965 :
966 : void
967 1556 : gfc_trans_same_strlen_check (const char* intr_name, locus* where,
968 : tree a, tree b, stmtblock_t* target)
969 : {
970 1556 : tree cond;
971 1556 : tree name;
972 :
973 : /* If bounds-checking is disabled, do nothing. */
974 1556 : if (!(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
975 : return;
976 :
977 : /* Compare the two string lengths. */
978 94 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, a, b);
979 :
980 : /* Output the runtime-check. */
981 94 : name = gfc_build_cstring_const (intr_name);
982 94 : name = gfc_build_addr_expr (pchar_type_node, name);
983 94 : gfc_trans_runtime_check (true, false, cond, target, where,
984 : "Unequal character lengths (%ld/%ld) in %s",
985 : fold_convert (long_integer_type_node, a),
986 : fold_convert (long_integer_type_node, b), name);
987 : }
988 :
989 :
990 : /* The EXPONENT(X) intrinsic function is translated into
991 : int ret;
992 : return isfinite(X) ? (frexp (X, &ret) , ret) : huge
993 : so that if X is a NaN or infinity, the result is HUGE(0).
994 : */
995 :
996 : static void
997 228 : gfc_conv_intrinsic_exponent (gfc_se *se, gfc_expr *expr)
998 : {
999 228 : tree arg, type, res, tmp, frexp, cond, huge;
1000 228 : int i;
1001 :
1002 456 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP,
1003 228 : expr->value.function.actual->expr->ts.kind);
1004 :
1005 228 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
1006 228 : arg = gfc_evaluate_now (arg, &se->pre);
1007 :
1008 228 : i = gfc_validate_kind (BT_INTEGER, gfc_c_int_kind, false);
1009 228 : huge = gfc_conv_mpz_to_tree (gfc_integer_kinds[i].huge, gfc_c_int_kind);
1010 228 : cond = build_call_expr_loc (input_location,
1011 : builtin_decl_explicit (BUILT_IN_ISFINITE),
1012 : 1, arg);
1013 :
1014 228 : res = gfc_create_var (integer_type_node, NULL);
1015 228 : tmp = build_call_expr_loc (input_location, frexp, 2, arg,
1016 : gfc_build_addr_expr (NULL_TREE, res));
1017 228 : tmp = fold_build2_loc (input_location, COMPOUND_EXPR, integer_type_node,
1018 : tmp, res);
1019 228 : se->expr = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
1020 : cond, tmp, huge);
1021 :
1022 228 : type = gfc_typenode_for_spec (&expr->ts);
1023 228 : se->expr = fold_convert (type, se->expr);
1024 228 : }
1025 :
1026 :
1027 : static int caf_call_cnt = 0;
1028 :
1029 : static tree
1030 1506 : conv_caf_func_index (stmtblock_t *block, gfc_namespace *ns, const char *pat,
1031 : gfc_expr *hash)
1032 : {
1033 1506 : char *name;
1034 1506 : gfc_se argse;
1035 1506 : gfc_expr func_index;
1036 1506 : gfc_symtree *index_st;
1037 1506 : tree func_index_tree;
1038 1506 : stmtblock_t blk;
1039 :
1040 : /* Need to get namespace where static variables are possible. */
1041 1506 : while (ns && ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL)
1042 0 : ns = ns->parent;
1043 1506 : gcc_assert (ns);
1044 :
1045 1506 : name = xasprintf (pat, caf_call_cnt);
1046 1506 : gcc_assert (!gfc_get_sym_tree (name, ns, &index_st, false));
1047 1506 : free (name);
1048 :
1049 1506 : index_st->n.sym->attr.flavor = FL_VARIABLE;
1050 1506 : index_st->n.sym->attr.save = SAVE_EXPLICIT;
1051 1506 : index_st->n.sym->value
1052 1506 : = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
1053 : &gfc_current_locus);
1054 1506 : mpz_set_si (index_st->n.sym->value->value.integer, -1);
1055 1506 : index_st->n.sym->ts.type = BT_INTEGER;
1056 1506 : index_st->n.sym->ts.kind = gfc_default_integer_kind;
1057 1506 : gfc_set_sym_referenced (index_st->n.sym);
1058 1506 : memset (&func_index, 0, sizeof (gfc_expr));
1059 1506 : gfc_clear_ts (&func_index.ts);
1060 1506 : func_index.expr_type = EXPR_VARIABLE;
1061 1506 : func_index.symtree = index_st;
1062 1506 : func_index.ts = index_st->n.sym->ts;
1063 1506 : gfc_commit_symbol (index_st->n.sym);
1064 :
1065 1506 : gfc_init_se (&argse, NULL);
1066 1506 : gfc_conv_expr (&argse, &func_index);
1067 1506 : gfc_add_block_to_block (block, &argse.pre);
1068 1506 : func_index_tree = argse.expr;
1069 :
1070 1506 : gfc_init_se (&argse, NULL);
1071 1506 : gfc_conv_expr (&argse, hash);
1072 :
1073 1506 : gfc_init_block (&blk);
1074 1506 : gfc_add_modify (&blk, func_index_tree,
1075 : build_call_expr (gfor_fndecl_caf_get_remote_function_index, 1,
1076 : argse.expr));
1077 1506 : gfc_add_expr_to_block (
1078 : block,
1079 : build3 (COND_EXPR, void_type_node,
1080 : gfc_likely (build2 (EQ_EXPR, logical_type_node, func_index_tree,
1081 : build_int_cst (integer_type_node, -1)),
1082 : PRED_FIRST_MATCH),
1083 : gfc_finish_block (&blk), NULL_TREE));
1084 :
1085 1506 : return func_index_tree;
1086 : }
1087 :
1088 : static tree
1089 1506 : conv_caf_add_call_data (stmtblock_t *blk, gfc_namespace *ns, const char *pat,
1090 : gfc_symbol *data_sym, tree *data_size)
1091 : {
1092 1506 : char *name;
1093 1506 : gfc_symtree *data_st;
1094 1506 : gfc_constructor *con;
1095 1506 : gfc_expr data, data_init;
1096 1506 : gfc_se argse;
1097 1506 : tree data_tree;
1098 :
1099 1506 : memset (&data, 0, sizeof (gfc_expr));
1100 1506 : gfc_clear_ts (&data.ts);
1101 1506 : data.expr_type = EXPR_VARIABLE;
1102 1506 : name = xasprintf (pat, caf_call_cnt);
1103 1506 : gcc_assert (!gfc_get_sym_tree (name, ns, &data_st, false));
1104 1506 : free (name);
1105 1506 : data_st->n.sym->attr.flavor = FL_VARIABLE;
1106 1506 : data_st->n.sym->ts = data_sym->ts;
1107 1506 : data.symtree = data_st;
1108 1506 : gfc_set_sym_referenced (data.symtree->n.sym);
1109 1506 : data.ts = data_st->n.sym->ts;
1110 1506 : gfc_commit_symbol (data_st->n.sym);
1111 :
1112 1506 : memset (&data_init, 0, sizeof (gfc_expr));
1113 1506 : gfc_clear_ts (&data_init.ts);
1114 1506 : data_init.expr_type = EXPR_STRUCTURE;
1115 1506 : data_init.ts = data.ts;
1116 1822 : for (gfc_component *comp = data.ts.u.derived->components; comp;
1117 316 : comp = comp->next)
1118 : {
1119 316 : con = gfc_constructor_get ();
1120 316 : con->expr = comp->initializer;
1121 316 : comp->initializer = NULL;
1122 316 : gfc_constructor_append (&data_init.value.constructor, con);
1123 : }
1124 :
1125 1506 : if (data.ts.u.derived->components)
1126 : {
1127 110 : gfc_init_se (&argse, NULL);
1128 110 : gfc_conv_expr (&argse, &data);
1129 110 : data_tree = argse.expr;
1130 110 : gfc_add_expr_to_block (blk,
1131 : gfc_trans_structure_assign (data_tree, &data_init,
1132 : true, true));
1133 110 : gfc_constructor_free (data_init.value.constructor);
1134 110 : *data_size = TREE_TYPE (data_tree)->type_common.size_unit;
1135 110 : data_tree = gfc_build_addr_expr (pvoid_type_node, data_tree);
1136 : }
1137 : else
1138 : {
1139 1396 : data_tree = build_zero_cst (pvoid_type_node);
1140 1396 : *data_size = build_zero_cst (size_type_node);
1141 : }
1142 :
1143 1506 : return data_tree;
1144 : }
1145 :
1146 : static tree
1147 251 : conv_shape_to_cst (gfc_expr *e)
1148 : {
1149 251 : tree tmp = NULL;
1150 690 : for (int d = 0; d < e->rank; ++d)
1151 : {
1152 439 : if (!tmp)
1153 251 : tmp = gfc_conv_mpz_to_tree (e->shape[d], gfc_size_kind);
1154 : else
1155 188 : tmp = fold_build2 (MULT_EXPR, TREE_TYPE (tmp), tmp,
1156 : gfc_conv_mpz_to_tree (e->shape[d], gfc_size_kind));
1157 : }
1158 251 : return fold_convert (size_type_node, tmp);
1159 : }
1160 :
1161 : static void
1162 1267 : conv_stat_and_team (stmtblock_t *block, gfc_expr *expr, tree *stat, tree *team,
1163 : tree *team_no)
1164 : {
1165 1267 : gfc_expr *stat_e, *team_e;
1166 :
1167 1267 : stat_e = gfc_find_stat_co (expr);
1168 1267 : if (stat_e)
1169 : {
1170 33 : gfc_se stat_se;
1171 33 : gfc_init_se (&stat_se, NULL);
1172 33 : gfc_conv_expr_reference (&stat_se, stat_e);
1173 33 : *stat = stat_se.expr;
1174 33 : gfc_add_block_to_block (block, &stat_se.pre);
1175 33 : gfc_add_block_to_block (block, &stat_se.post);
1176 : }
1177 : else
1178 1234 : *stat = null_pointer_node;
1179 :
1180 1267 : team_e = gfc_find_team_co (expr, TEAM_TEAM);
1181 1267 : if (team_e)
1182 : {
1183 18 : gfc_se team_se;
1184 18 : gfc_init_se (&team_se, NULL);
1185 18 : gfc_conv_expr (&team_se, team_e);
1186 18 : *team
1187 18 : = gfc_build_addr_expr (NULL_TREE, gfc_trans_force_lval (&team_se.pre,
1188 : team_se.expr));
1189 18 : gfc_add_block_to_block (block, &team_se.pre);
1190 18 : gfc_add_block_to_block (block, &team_se.post);
1191 : }
1192 : else
1193 1249 : *team = null_pointer_node;
1194 :
1195 1267 : team_e = gfc_find_team_co (expr, TEAM_NUMBER);
1196 1267 : if (team_e)
1197 : {
1198 30 : gfc_se team_se;
1199 30 : gfc_init_se (&team_se, NULL);
1200 30 : gfc_conv_expr (&team_se, team_e);
1201 30 : *team_no = gfc_build_addr_expr (
1202 : NULL_TREE,
1203 : gfc_trans_force_lval (&team_se.pre,
1204 : fold_convert (integer_type_node, team_se.expr)));
1205 30 : gfc_add_block_to_block (block, &team_se.pre);
1206 30 : gfc_add_block_to_block (block, &team_se.post);
1207 : }
1208 : else
1209 1237 : *team_no = null_pointer_node;
1210 1267 : }
1211 :
1212 : /* Get data from a remote coarray. */
1213 :
1214 : static void
1215 1006 : gfc_conv_intrinsic_caf_get (gfc_se *se, gfc_expr *expr, tree lhs,
1216 : bool may_realloc, symbol_attribute *caf_attr)
1217 : {
1218 1006 : gfc_expr *array_expr;
1219 1006 : tree caf_decl, token, image_index, tmp, res_var, type, stat, dest_size,
1220 : dest_data, opt_dest_desc, get_fn_index_tree, add_data_tree, add_data_size,
1221 : opt_src_desc, opt_src_charlen, opt_dest_charlen, team, team_no;
1222 1006 : symbol_attribute caf_attr_store;
1223 1006 : gfc_namespace *ns;
1224 1006 : gfc_expr *get_fn_hash = expr->value.function.actual->next->expr,
1225 1006 : *get_fn_expr = expr->value.function.actual->next->next->expr;
1226 1006 : gfc_symbol *add_data_sym = get_fn_expr->symtree->n.sym->formal->sym;
1227 :
1228 1006 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
1229 :
1230 1006 : if (se->ss && se->ss->info->useflags)
1231 : {
1232 : /* Access the previously obtained result. */
1233 379 : gfc_conv_tmp_array_ref (se);
1234 379 : return;
1235 : }
1236 :
1237 627 : array_expr = expr->value.function.actual->expr;
1238 627 : ns = array_expr->expr_type == EXPR_VARIABLE
1239 627 : && !array_expr->symtree->n.sym->attr.associate_var
1240 571 : && !array_expr->symtree->n.sym->module
1241 627 : ? array_expr->symtree->n.sym->ns
1242 : : gfc_current_ns;
1243 627 : type = gfc_typenode_for_spec (&array_expr->ts);
1244 :
1245 627 : if (caf_attr == NULL)
1246 : {
1247 627 : caf_attr_store = gfc_caf_attr (array_expr);
1248 627 : caf_attr = &caf_attr_store;
1249 : }
1250 :
1251 627 : res_var = lhs;
1252 :
1253 627 : conv_stat_and_team (&se->pre, expr, &stat, &team, &team_no);
1254 :
1255 627 : get_fn_index_tree
1256 627 : = conv_caf_func_index (&se->pre, ns, "__caf_get_from_remote_fn_index_%d",
1257 : get_fn_hash);
1258 627 : add_data_tree
1259 627 : = conv_caf_add_call_data (&se->pre, ns, "__caf_get_from_remote_add_data_%d",
1260 : add_data_sym, &add_data_size);
1261 627 : ++caf_call_cnt;
1262 :
1263 627 : if (array_expr->rank == 0)
1264 : {
1265 246 : res_var = gfc_create_var (type, "caf_res");
1266 246 : if (array_expr->ts.type == BT_CHARACTER)
1267 : {
1268 33 : gfc_conv_string_length (array_expr->ts.u.cl, array_expr, &se->pre);
1269 33 : se->string_length = array_expr->ts.u.cl->backend_decl;
1270 33 : opt_src_charlen = gfc_build_addr_expr (
1271 : NULL_TREE, gfc_trans_force_lval (&se->pre, se->string_length));
1272 33 : dest_size = build_int_cstu (size_type_node, array_expr->ts.kind);
1273 : }
1274 : else
1275 : {
1276 213 : dest_size = res_var->typed.type->type_common.size_unit;
1277 213 : opt_src_charlen
1278 213 : = build_zero_cst (build_pointer_type (size_type_node));
1279 : }
1280 246 : dest_data
1281 246 : = gfc_evaluate_now (gfc_build_addr_expr (NULL_TREE, res_var), &se->pre);
1282 246 : res_var = build_fold_indirect_ref (dest_data);
1283 246 : dest_data = gfc_build_addr_expr (pvoid_type_node, dest_data);
1284 246 : opt_dest_desc = build_zero_cst (pvoid_type_node);
1285 : }
1286 : else
1287 : {
1288 : /* Create temporary. */
1289 381 : may_realloc = gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
1290 : type, NULL_TREE, false, false,
1291 : false, &array_expr->where)
1292 : == NULL_TREE;
1293 381 : res_var = se->ss->info->data.array.descriptor;
1294 381 : if (array_expr->ts.type == BT_CHARACTER)
1295 : {
1296 16 : se->string_length = array_expr->ts.u.cl->backend_decl;
1297 16 : opt_src_charlen = gfc_build_addr_expr (
1298 : NULL_TREE, gfc_trans_force_lval (&se->pre, se->string_length));
1299 16 : dest_size = build_int_cstu (size_type_node, array_expr->ts.kind);
1300 : }
1301 : else
1302 : {
1303 365 : opt_src_charlen
1304 365 : = build_zero_cst (build_pointer_type (size_type_node));
1305 365 : dest_size = fold_build2 (
1306 : MULT_EXPR, size_type_node,
1307 : fold_convert (size_type_node,
1308 : array_expr->shape
1309 : ? conv_shape_to_cst (array_expr)
1310 : : gfc_conv_descriptor_size (res_var,
1311 : array_expr->rank)),
1312 : fold_convert (size_type_node,
1313 : gfc_conv_descriptor_span_get (res_var)));
1314 : }
1315 381 : opt_dest_desc = res_var;
1316 381 : dest_data = gfc_conv_descriptor_data_get (res_var);
1317 381 : opt_dest_desc = gfc_build_addr_expr (NULL_TREE, opt_dest_desc);
1318 381 : if (may_realloc)
1319 : {
1320 62 : tmp = gfc_conv_descriptor_data_get (res_var);
1321 62 : tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
1322 : NULL_TREE, NULL_TREE, true, NULL,
1323 : GFC_CAF_COARRAY_NOCOARRAY);
1324 62 : gfc_add_expr_to_block (&se->post, tmp);
1325 : }
1326 381 : dest_data
1327 381 : = gfc_build_addr_expr (NULL_TREE,
1328 : gfc_trans_force_lval (&se->pre, dest_data));
1329 : }
1330 :
1331 627 : opt_dest_charlen = opt_src_charlen;
1332 627 : caf_decl = gfc_get_tree_for_caf_expr (array_expr);
1333 627 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
1334 2 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1335 :
1336 627 : if (!TYPE_LANG_SPECIFIC (TREE_TYPE (caf_decl))->rank
1337 627 : || GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl)))
1338 546 : opt_src_desc = build_zero_cst (pvoid_type_node);
1339 : else
1340 81 : opt_src_desc = gfc_build_addr_expr (pvoid_type_node, caf_decl);
1341 :
1342 627 : image_index = gfc_caf_get_image_index (&se->pre, array_expr, caf_decl);
1343 627 : gfc_get_caf_token_offset (se, &token, NULL, caf_decl, NULL, array_expr);
1344 :
1345 : /* It guarantees memory consistency within the same segment. */
1346 627 : tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
1347 627 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1348 : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1349 : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1350 627 : ASM_VOLATILE_P (tmp) = 1;
1351 627 : gfc_add_expr_to_block (&se->pre, tmp);
1352 :
1353 627 : tmp = build_call_expr_loc (
1354 : input_location, gfor_fndecl_caf_get_from_remote, 15, token, opt_src_desc,
1355 : opt_src_charlen, image_index, dest_size, dest_data, opt_dest_charlen,
1356 : opt_dest_desc, constant_boolean_node (may_realloc, boolean_type_node),
1357 : get_fn_index_tree, add_data_tree, add_data_size, stat, team, team_no);
1358 :
1359 627 : gfc_add_expr_to_block (&se->pre, tmp);
1360 :
1361 627 : if (se->ss)
1362 381 : gfc_advance_se_ss_chain (se);
1363 :
1364 627 : se->expr = res_var;
1365 :
1366 627 : return;
1367 : }
1368 :
1369 : /* Generate call to caf_is_present_on_remote for allocated (coarrary[...])
1370 : calls. */
1371 :
1372 : static void
1373 239 : gfc_conv_intrinsic_caf_is_present_remote (gfc_se *se, gfc_expr *e)
1374 : {
1375 239 : gfc_expr *caf_expr, *hash, *present_fn;
1376 239 : gfc_symbol *add_data_sym;
1377 239 : tree fn_index, add_data_tree, add_data_size, caf_decl, image_index, token;
1378 :
1379 239 : gcc_assert (e->expr_type == EXPR_FUNCTION
1380 : && e->value.function.isym->id
1381 : == GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE);
1382 239 : caf_expr = e->value.function.actual->expr;
1383 239 : hash = e->value.function.actual->next->expr;
1384 239 : present_fn = e->value.function.actual->next->next->expr;
1385 239 : add_data_sym = present_fn->symtree->n.sym->formal->sym;
1386 :
1387 239 : fn_index = conv_caf_func_index (&se->pre, e->symtree->n.sym->ns,
1388 : "__caf_present_on_remote_fn_index_%d", hash);
1389 239 : add_data_tree = conv_caf_add_call_data (&se->pre, e->symtree->n.sym->ns,
1390 : "__caf_present_on_remote_add_data_%d",
1391 : add_data_sym, &add_data_size);
1392 239 : ++caf_call_cnt;
1393 :
1394 239 : caf_decl = gfc_get_tree_for_caf_expr (caf_expr);
1395 239 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
1396 4 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1397 :
1398 239 : image_index = gfc_caf_get_image_index (&se->pre, caf_expr, caf_decl);
1399 239 : gfc_get_caf_token_offset (se, &token, NULL, caf_decl, NULL, caf_expr);
1400 :
1401 239 : se->expr
1402 239 : = fold_convert (logical_type_node,
1403 : build_call_expr_loc (input_location,
1404 : gfor_fndecl_caf_is_present_on_remote,
1405 : 5, token, image_index, fn_index,
1406 : add_data_tree, add_data_size));
1407 239 : }
1408 :
1409 : static tree
1410 360 : conv_caf_send_to_remote (gfc_code *code)
1411 : {
1412 360 : gfc_expr *lhs_expr, *rhs_expr, *lhs_hash, *receiver_fn_expr;
1413 360 : gfc_symbol *add_data_sym;
1414 360 : gfc_se lhs_se, rhs_se;
1415 360 : stmtblock_t block;
1416 360 : gfc_namespace *ns;
1417 360 : tree caf_decl, token, rhs_size, image_index, tmp, rhs_data;
1418 360 : tree lhs_stat, lhs_team, lhs_team_no, opt_lhs_charlen, opt_rhs_charlen;
1419 360 : tree opt_lhs_desc = NULL_TREE, opt_rhs_desc = NULL_TREE;
1420 360 : tree receiver_fn_index_tree, add_data_tree, add_data_size;
1421 :
1422 360 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
1423 360 : gcc_assert (code->resolved_isym->id == GFC_ISYM_CAF_SEND);
1424 :
1425 360 : lhs_expr = code->ext.actual->expr;
1426 360 : rhs_expr = code->ext.actual->next->expr;
1427 360 : lhs_hash = code->ext.actual->next->next->expr;
1428 360 : receiver_fn_expr = code->ext.actual->next->next->next->expr;
1429 360 : add_data_sym = receiver_fn_expr->symtree->n.sym->formal->sym;
1430 :
1431 360 : ns = lhs_expr->expr_type == EXPR_VARIABLE
1432 360 : && !lhs_expr->symtree->n.sym->attr.associate_var
1433 360 : ? lhs_expr->symtree->n.sym->ns
1434 : : gfc_current_ns;
1435 :
1436 360 : gfc_init_block (&block);
1437 :
1438 : /* LHS. */
1439 360 : gfc_init_se (&lhs_se, NULL);
1440 360 : caf_decl = gfc_get_tree_for_caf_expr (lhs_expr);
1441 360 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
1442 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1443 360 : if (lhs_expr->rank == 0)
1444 : {
1445 266 : if (lhs_expr->ts.type == BT_CHARACTER)
1446 : {
1447 24 : gfc_conv_string_length (lhs_expr->ts.u.cl, lhs_expr, &block);
1448 24 : lhs_se.string_length = lhs_expr->ts.u.cl->backend_decl;
1449 24 : opt_lhs_charlen = gfc_build_addr_expr (
1450 : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1451 : }
1452 : else
1453 242 : opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1454 266 : opt_lhs_desc = null_pointer_node;
1455 : }
1456 : else
1457 : {
1458 94 : gfc_conv_expr_descriptor (&lhs_se, lhs_expr);
1459 94 : gfc_add_block_to_block (&block, &lhs_se.pre);
1460 94 : opt_lhs_desc = lhs_se.expr;
1461 94 : if (lhs_expr->ts.type == BT_CHARACTER)
1462 44 : opt_lhs_charlen = gfc_build_addr_expr (
1463 : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1464 : else
1465 50 : opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1466 : /* Get the third formal argument of the receiver function. (This is the
1467 : location where to put the data on the remote image.) Need to look at
1468 : the argument in the function decl, because in the gfc_symbol's formal
1469 : argument an array may have no descriptor while in the generated
1470 : function decl it has. */
1471 94 : tmp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
1472 : TREE_TYPE (receiver_fn_expr->symtree->n.sym->backend_decl)))));
1473 94 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
1474 56 : opt_lhs_desc = null_pointer_node;
1475 : else
1476 38 : opt_lhs_desc
1477 38 : = gfc_build_addr_expr (NULL_TREE,
1478 : gfc_trans_force_lval (&block, opt_lhs_desc));
1479 : }
1480 :
1481 : /* Obtain token, offset and image index for the LHS. */
1482 360 : image_index = gfc_caf_get_image_index (&block, lhs_expr, caf_decl);
1483 360 : gfc_get_caf_token_offset (&lhs_se, &token, NULL, caf_decl, NULL, lhs_expr);
1484 :
1485 : /* RHS. */
1486 360 : gfc_init_se (&rhs_se, NULL);
1487 360 : if (rhs_expr->rank == 0)
1488 : {
1489 436 : rhs_se.want_pointer = rhs_expr->ts.type == BT_CHARACTER
1490 218 : && rhs_expr->expr_type != EXPR_CONSTANT;
1491 218 : gfc_conv_expr (&rhs_se, rhs_expr);
1492 218 : gfc_add_block_to_block (&block, &rhs_se.pre);
1493 218 : opt_rhs_desc = null_pointer_node;
1494 218 : if (rhs_expr->ts.type == BT_CHARACTER)
1495 : {
1496 40 : rhs_data
1497 40 : = rhs_expr->expr_type == EXPR_CONSTANT
1498 40 : ? gfc_build_addr_expr (NULL_TREE,
1499 : gfc_trans_force_lval (&block,
1500 : rhs_se.expr))
1501 : : rhs_se.expr;
1502 40 : opt_rhs_charlen = gfc_build_addr_expr (
1503 : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1504 40 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1505 : }
1506 : else
1507 : {
1508 178 : rhs_data
1509 178 : = gfc_build_addr_expr (NULL_TREE,
1510 : gfc_trans_force_lval (&block, rhs_se.expr));
1511 178 : opt_rhs_charlen
1512 178 : = build_zero_cst (build_pointer_type (size_type_node));
1513 178 : rhs_size = TREE_TYPE (rhs_se.expr)->type_common.size_unit;
1514 : }
1515 : }
1516 : else
1517 : {
1518 284 : rhs_se.force_tmp = rhs_expr->shape == NULL
1519 142 : || !gfc_is_simply_contiguous (rhs_expr, false, false);
1520 142 : gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
1521 142 : gfc_add_block_to_block (&block, &rhs_se.pre);
1522 142 : opt_rhs_desc = rhs_se.expr;
1523 142 : if (rhs_expr->ts.type == BT_CHARACTER)
1524 : {
1525 28 : opt_rhs_charlen = gfc_build_addr_expr (
1526 : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1527 28 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1528 : }
1529 : else
1530 : {
1531 114 : opt_rhs_charlen
1532 114 : = build_zero_cst (build_pointer_type (size_type_node));
1533 114 : rhs_size = fold_build2 (
1534 : MULT_EXPR, size_type_node,
1535 : fold_convert (size_type_node,
1536 : rhs_expr->shape
1537 : ? conv_shape_to_cst (rhs_expr)
1538 : : gfc_conv_descriptor_size (rhs_se.expr,
1539 : rhs_expr->rank)),
1540 : fold_convert (size_type_node,
1541 : gfc_conv_descriptor_span_get (rhs_se.expr)));
1542 : }
1543 :
1544 142 : rhs_data = gfc_build_addr_expr (
1545 : NULL_TREE, gfc_trans_force_lval (&block, gfc_conv_descriptor_data_get (
1546 : opt_rhs_desc)));
1547 142 : opt_rhs_desc = gfc_build_addr_expr (NULL_TREE, opt_rhs_desc);
1548 : }
1549 360 : gfc_add_block_to_block (&block, &rhs_se.pre);
1550 :
1551 360 : conv_stat_and_team (&block, lhs_expr, &lhs_stat, &lhs_team, &lhs_team_no);
1552 :
1553 360 : receiver_fn_index_tree
1554 360 : = conv_caf_func_index (&block, ns, "__caf_send_to_remote_fn_index_%d",
1555 : lhs_hash);
1556 360 : add_data_tree
1557 360 : = conv_caf_add_call_data (&block, ns, "__caf_send_to_remote_add_data_%d",
1558 : add_data_sym, &add_data_size);
1559 360 : ++caf_call_cnt;
1560 :
1561 360 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_send_to_remote, 14,
1562 : token, opt_lhs_desc, opt_lhs_charlen, image_index,
1563 : rhs_size, rhs_data, opt_rhs_charlen, opt_rhs_desc,
1564 : receiver_fn_index_tree, add_data_tree,
1565 : add_data_size, lhs_stat, lhs_team, lhs_team_no);
1566 :
1567 360 : gfc_add_expr_to_block (&block, tmp);
1568 360 : gfc_add_block_to_block (&block, &lhs_se.post);
1569 360 : gfc_add_block_to_block (&block, &rhs_se.post);
1570 :
1571 : /* It guarantees memory consistency within the same segment. */
1572 360 : tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
1573 360 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1574 : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1575 : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1576 360 : ASM_VOLATILE_P (tmp) = 1;
1577 360 : gfc_add_expr_to_block (&block, tmp);
1578 :
1579 360 : return gfc_finish_block (&block);
1580 : }
1581 :
1582 : /* Send-get data to a remote coarray. */
1583 :
1584 : static tree
1585 140 : conv_caf_sendget (gfc_code *code)
1586 : {
1587 : /* lhs stuff */
1588 140 : gfc_expr *lhs_expr, *lhs_hash, *receiver_fn_expr;
1589 140 : gfc_symbol *lhs_add_data_sym;
1590 140 : gfc_se lhs_se;
1591 140 : tree lhs_caf_decl, lhs_token, opt_lhs_charlen,
1592 140 : opt_lhs_desc = NULL_TREE, receiver_fn_index_tree, lhs_image_index,
1593 : lhs_add_data_tree, lhs_add_data_size, lhs_stat, lhs_team, lhs_team_no;
1594 140 : int transfer_rank;
1595 :
1596 : /* rhs stuff */
1597 140 : gfc_expr *rhs_expr, *rhs_hash, *sender_fn_expr;
1598 140 : gfc_symbol *rhs_add_data_sym;
1599 140 : gfc_se rhs_se;
1600 140 : tree rhs_caf_decl, rhs_token, opt_rhs_charlen,
1601 140 : opt_rhs_desc = NULL_TREE, sender_fn_index_tree, rhs_image_index,
1602 : rhs_add_data_tree, rhs_add_data_size, rhs_stat, rhs_team, rhs_team_no;
1603 :
1604 : /* shared */
1605 140 : stmtblock_t block;
1606 140 : gfc_namespace *ns;
1607 140 : tree tmp, rhs_size;
1608 :
1609 140 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
1610 140 : gcc_assert (code->resolved_isym->id == GFC_ISYM_CAF_SENDGET);
1611 :
1612 140 : lhs_expr = code->ext.actual->expr;
1613 140 : rhs_expr = code->ext.actual->next->expr;
1614 140 : lhs_hash = code->ext.actual->next->next->expr;
1615 140 : receiver_fn_expr = code->ext.actual->next->next->next->expr;
1616 140 : rhs_hash = code->ext.actual->next->next->next->next->expr;
1617 140 : sender_fn_expr = code->ext.actual->next->next->next->next->next->expr;
1618 :
1619 140 : lhs_add_data_sym = receiver_fn_expr->symtree->n.sym->formal->sym;
1620 140 : rhs_add_data_sym = sender_fn_expr->symtree->n.sym->formal->sym;
1621 :
1622 140 : ns = lhs_expr->expr_type == EXPR_VARIABLE
1623 140 : && !lhs_expr->symtree->n.sym->attr.associate_var
1624 140 : ? lhs_expr->symtree->n.sym->ns
1625 : : gfc_current_ns;
1626 :
1627 140 : gfc_init_block (&block);
1628 :
1629 140 : lhs_stat = null_pointer_node;
1630 140 : lhs_team = null_pointer_node;
1631 140 : rhs_stat = null_pointer_node;
1632 140 : rhs_team = null_pointer_node;
1633 :
1634 : /* LHS. */
1635 140 : gfc_init_se (&lhs_se, NULL);
1636 140 : lhs_caf_decl = gfc_get_tree_for_caf_expr (lhs_expr);
1637 140 : if (TREE_CODE (TREE_TYPE (lhs_caf_decl)) == REFERENCE_TYPE)
1638 0 : lhs_caf_decl = build_fold_indirect_ref_loc (input_location, lhs_caf_decl);
1639 140 : if (lhs_expr->rank == 0)
1640 : {
1641 78 : if (lhs_expr->ts.type == BT_CHARACTER)
1642 : {
1643 16 : gfc_conv_string_length (lhs_expr->ts.u.cl, lhs_expr, &block);
1644 16 : lhs_se.string_length = lhs_expr->ts.u.cl->backend_decl;
1645 16 : opt_lhs_charlen = gfc_build_addr_expr (
1646 : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1647 : }
1648 : else
1649 62 : opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1650 78 : opt_lhs_desc = null_pointer_node;
1651 : }
1652 : else
1653 : {
1654 62 : gfc_conv_expr_descriptor (&lhs_se, lhs_expr);
1655 62 : gfc_add_block_to_block (&block, &lhs_se.pre);
1656 62 : opt_lhs_desc = lhs_se.expr;
1657 62 : if (lhs_expr->ts.type == BT_CHARACTER)
1658 32 : opt_lhs_charlen = gfc_build_addr_expr (
1659 : NULL_TREE, gfc_trans_force_lval (&block, lhs_se.string_length));
1660 : else
1661 30 : opt_lhs_charlen = build_zero_cst (build_pointer_type (size_type_node));
1662 : /* Get the third formal argument of the receiver function. (This is the
1663 : location where to put the data on the remote image.) Need to look at
1664 : the argument in the function decl, because in the gfc_symbol's formal
1665 : argument an array may have no descriptor while in the generated
1666 : function decl it has. */
1667 62 : tmp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
1668 : TREE_TYPE (receiver_fn_expr->symtree->n.sym->backend_decl)))));
1669 62 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
1670 54 : opt_lhs_desc = null_pointer_node;
1671 : else
1672 8 : opt_lhs_desc
1673 8 : = gfc_build_addr_expr (NULL_TREE,
1674 : gfc_trans_force_lval (&block, opt_lhs_desc));
1675 : }
1676 :
1677 : /* Obtain token, offset and image index for the LHS. */
1678 140 : lhs_image_index = gfc_caf_get_image_index (&block, lhs_expr, lhs_caf_decl);
1679 140 : gfc_get_caf_token_offset (&lhs_se, &lhs_token, NULL, lhs_caf_decl, NULL,
1680 : lhs_expr);
1681 :
1682 : /* RHS. */
1683 140 : rhs_caf_decl = gfc_get_tree_for_caf_expr (rhs_expr);
1684 140 : if (TREE_CODE (TREE_TYPE (rhs_caf_decl)) == REFERENCE_TYPE)
1685 0 : rhs_caf_decl = build_fold_indirect_ref_loc (input_location, rhs_caf_decl);
1686 140 : transfer_rank = rhs_expr->rank;
1687 140 : gfc_expression_rank (rhs_expr);
1688 140 : gfc_init_se (&rhs_se, NULL);
1689 140 : if (rhs_expr->rank == 0)
1690 : {
1691 80 : opt_rhs_desc = null_pointer_node;
1692 80 : if (rhs_expr->ts.type == BT_CHARACTER)
1693 : {
1694 32 : gfc_conv_expr (&rhs_se, rhs_expr);
1695 32 : gfc_add_block_to_block (&block, &rhs_se.pre);
1696 32 : opt_rhs_charlen = gfc_build_addr_expr (
1697 : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1698 32 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1699 : }
1700 : else
1701 : {
1702 48 : gfc_typespec *ts
1703 48 : = &sender_fn_expr->symtree->n.sym->formal->next->next->sym->ts;
1704 :
1705 48 : opt_rhs_charlen
1706 48 : = build_zero_cst (build_pointer_type (size_type_node));
1707 48 : rhs_size = gfc_typenode_for_spec (ts)->type_common.size_unit;
1708 : }
1709 : }
1710 : /* Get the fifth formal argument of the getter function. This is the argument
1711 : pointing to the data to get on the remote image. Need to look at the
1712 : argument in the function decl, because in the gfc_symbol's formal argument
1713 : an array may have no descriptor while in the generated function decl it
1714 : has. */
1715 60 : else if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_VALUE (
1716 : TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (
1717 : TREE_TYPE (sender_fn_expr->symtree->n.sym->backend_decl))))))))))
1718 : {
1719 52 : rhs_se.data_not_needed = 1;
1720 52 : gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
1721 52 : gfc_add_block_to_block (&block, &rhs_se.pre);
1722 52 : if (rhs_expr->ts.type == BT_CHARACTER)
1723 : {
1724 16 : opt_rhs_charlen = gfc_build_addr_expr (
1725 : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1726 16 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1727 : }
1728 : else
1729 : {
1730 36 : opt_rhs_charlen
1731 36 : = build_zero_cst (build_pointer_type (size_type_node));
1732 36 : rhs_size = TREE_TYPE (rhs_se.expr)->type_common.size_unit;
1733 : }
1734 52 : opt_rhs_desc = null_pointer_node;
1735 : }
1736 : else
1737 : {
1738 8 : gfc_ref *arr_ref = rhs_expr->ref;
1739 8 : while (arr_ref && arr_ref->type != REF_ARRAY)
1740 0 : arr_ref = arr_ref->next;
1741 8 : rhs_se.force_tmp
1742 16 : = (rhs_expr->shape == NULL
1743 8 : && (!arr_ref || !gfc_full_array_ref_p (arr_ref, nullptr)))
1744 16 : || !gfc_is_simply_contiguous (rhs_expr, false, false);
1745 8 : gfc_conv_expr_descriptor (&rhs_se, rhs_expr);
1746 8 : gfc_add_block_to_block (&block, &rhs_se.pre);
1747 8 : opt_rhs_desc = rhs_se.expr;
1748 8 : if (rhs_expr->ts.type == BT_CHARACTER)
1749 : {
1750 0 : opt_rhs_charlen = gfc_build_addr_expr (
1751 : NULL_TREE, gfc_trans_force_lval (&block, rhs_se.string_length));
1752 0 : rhs_size = build_int_cstu (size_type_node, rhs_expr->ts.kind);
1753 : }
1754 : else
1755 : {
1756 8 : opt_rhs_charlen
1757 8 : = build_zero_cst (build_pointer_type (size_type_node));
1758 8 : rhs_size = fold_build2 (
1759 : MULT_EXPR, size_type_node,
1760 : fold_convert (size_type_node,
1761 : rhs_expr->shape
1762 : ? conv_shape_to_cst (rhs_expr)
1763 : : gfc_conv_descriptor_size (rhs_se.expr,
1764 : rhs_expr->rank)),
1765 : fold_convert (size_type_node,
1766 : gfc_conv_descriptor_span_get (rhs_se.expr)));
1767 : }
1768 :
1769 8 : opt_rhs_desc = gfc_build_addr_expr (NULL_TREE, opt_rhs_desc);
1770 : }
1771 140 : gfc_add_block_to_block (&block, &rhs_se.pre);
1772 :
1773 : /* Obtain token, offset and image index for the RHS. */
1774 140 : rhs_image_index = gfc_caf_get_image_index (&block, rhs_expr, rhs_caf_decl);
1775 140 : gfc_get_caf_token_offset (&rhs_se, &rhs_token, NULL, rhs_caf_decl, NULL,
1776 : rhs_expr);
1777 :
1778 : /* stat and team. */
1779 140 : conv_stat_and_team (&block, lhs_expr, &lhs_stat, &lhs_team, &lhs_team_no);
1780 140 : conv_stat_and_team (&block, rhs_expr, &rhs_stat, &rhs_team, &rhs_team_no);
1781 :
1782 140 : sender_fn_index_tree
1783 140 : = conv_caf_func_index (&block, ns, "__caf_transfer_from_fn_index_%d",
1784 : rhs_hash);
1785 140 : rhs_add_data_tree
1786 140 : = conv_caf_add_call_data (&block, ns,
1787 : "__caf_transfer_from_remote_add_data_%d",
1788 : rhs_add_data_sym, &rhs_add_data_size);
1789 140 : receiver_fn_index_tree
1790 140 : = conv_caf_func_index (&block, ns, "__caf_transfer_to_remote_fn_index_%d",
1791 : lhs_hash);
1792 140 : lhs_add_data_tree
1793 140 : = conv_caf_add_call_data (&block, ns,
1794 : "__caf_transfer_to_remote_add_data_%d",
1795 : lhs_add_data_sym, &lhs_add_data_size);
1796 140 : ++caf_call_cnt;
1797 :
1798 140 : tmp = build_call_expr_loc (
1799 : input_location, gfor_fndecl_caf_transfer_between_remotes, 22, lhs_token,
1800 : opt_lhs_desc, opt_lhs_charlen, lhs_image_index, receiver_fn_index_tree,
1801 : lhs_add_data_tree, lhs_add_data_size, rhs_token, opt_rhs_desc,
1802 : opt_rhs_charlen, rhs_image_index, sender_fn_index_tree, rhs_add_data_tree,
1803 : rhs_add_data_size, rhs_size,
1804 : transfer_rank == 0 ? boolean_true_node : boolean_false_node, lhs_stat,
1805 : rhs_stat, lhs_team, lhs_team_no, rhs_team, rhs_team_no);
1806 :
1807 140 : gfc_add_expr_to_block (&block, tmp);
1808 140 : gfc_add_block_to_block (&block, &lhs_se.post);
1809 140 : gfc_add_block_to_block (&block, &rhs_se.post);
1810 :
1811 : /* It guarantees memory consistency within the same segment. */
1812 140 : tmp = gfc_build_string_const (strlen ("memory") + 1, "memory");
1813 140 : tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1814 : gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1815 : tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1816 140 : ASM_VOLATILE_P (tmp) = 1;
1817 140 : gfc_add_expr_to_block (&block, tmp);
1818 :
1819 140 : return gfc_finish_block (&block);
1820 : }
1821 :
1822 :
1823 : static void
1824 1306 : trans_this_image (gfc_se * se, gfc_expr *expr)
1825 : {
1826 1306 : stmtblock_t loop;
1827 1306 : tree type, desc, dim_arg, cond, tmp, m, loop_var, exit_label, min_var, lbound,
1828 : ubound, extent, ml, team;
1829 1306 : gfc_se argse;
1830 1306 : int rank, corank;
1831 :
1832 : /* The case -fcoarray=single is handled elsewhere. */
1833 1306 : gcc_assert (flag_coarray != GFC_FCOARRAY_SINGLE);
1834 :
1835 : /* Translate team, if present. */
1836 1306 : if (expr->value.function.actual->next->next->expr)
1837 : {
1838 18 : gfc_init_se (&argse, NULL);
1839 18 : gfc_conv_expr_val (&argse, expr->value.function.actual->next->next->expr);
1840 18 : gfc_add_block_to_block (&se->pre, &argse.pre);
1841 18 : gfc_add_block_to_block (&se->post, &argse.post);
1842 18 : team = fold_convert (pvoid_type_node, argse.expr);
1843 : }
1844 : else
1845 1288 : team = null_pointer_node;
1846 :
1847 : /* Argument-free version: THIS_IMAGE(). */
1848 1306 : if (expr->value.function.actual->expr == NULL)
1849 : {
1850 988 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
1851 : team);
1852 988 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind),
1853 : tmp);
1854 996 : return;
1855 : }
1856 :
1857 : /* Coarray-argument version: THIS_IMAGE(coarray [, dim]). */
1858 :
1859 318 : type = gfc_get_int_type (gfc_default_integer_kind);
1860 318 : corank = expr->value.function.actual->expr->corank;
1861 318 : rank = expr->value.function.actual->expr->rank;
1862 :
1863 : /* Obtain the descriptor of the COARRAY. */
1864 318 : gfc_init_se (&argse, NULL);
1865 318 : argse.want_coarray = 1;
1866 318 : gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
1867 318 : gfc_add_block_to_block (&se->pre, &argse.pre);
1868 318 : gfc_add_block_to_block (&se->post, &argse.post);
1869 318 : desc = argse.expr;
1870 :
1871 318 : if (se->ss)
1872 : {
1873 : /* Create an implicit second parameter from the loop variable. */
1874 70 : gcc_assert (!expr->value.function.actual->next->expr);
1875 70 : gcc_assert (corank > 0);
1876 70 : gcc_assert (se->loop->dimen == 1);
1877 70 : gcc_assert (se->ss->info->expr == expr);
1878 :
1879 70 : dim_arg = fold_convert_loc (input_location, gfc_array_dim_rank_type,
1880 : se->loop->loopvar[0]);
1881 70 : dim_arg = fold_build2_loc (input_location, PLUS_EXPR,
1882 : gfc_array_dim_rank_type, dim_arg,
1883 : gfc_rank_cst[1]);
1884 70 : gfc_advance_se_ss_chain (se);
1885 : }
1886 : else
1887 : {
1888 : /* Use the passed DIM= argument. */
1889 248 : gcc_assert (expr->value.function.actual->next->expr);
1890 248 : gfc_init_se (&argse, NULL);
1891 248 : gfc_conv_expr_type (&argse, expr->value.function.actual->next->expr,
1892 : gfc_array_dim_rank_type);
1893 248 : gfc_add_block_to_block (&se->pre, &argse.pre);
1894 248 : dim_arg = argse.expr;
1895 :
1896 248 : if (INTEGER_CST_P (dim_arg))
1897 : {
1898 132 : if (wi::ltu_p (wi::to_wide (dim_arg), 1)
1899 264 : || wi::gtu_p (wi::to_wide (dim_arg),
1900 132 : GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
1901 0 : gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
1902 0 : "dimension index", expr->value.function.isym->name,
1903 : &expr->where);
1904 : }
1905 116 : else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1906 : {
1907 0 : dim_arg = gfc_evaluate_now (dim_arg, &se->pre);
1908 0 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
1909 : dim_arg, gfc_rank_cst[1]);
1910 0 : tmp = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
1911 0 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
1912 : dim_arg, tmp);
1913 0 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
1914 : logical_type_node, cond, tmp);
1915 0 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
1916 : gfc_msg_fault);
1917 : }
1918 : }
1919 :
1920 : /* Used algorithm; cf. Fortran 2008, C.10. Note, due to the scalarizer,
1921 : one always has a dim_arg argument.
1922 :
1923 : m = this_image() - 1
1924 : if (corank == 1)
1925 : {
1926 : sub(1) = m + lcobound(corank)
1927 : return;
1928 : }
1929 : i = rank
1930 : min_var = min (rank + corank - 2, rank + dim_arg - 1)
1931 : for (;;)
1932 : {
1933 : extent = gfc_extent(i)
1934 : ml = m
1935 : m = m/extent
1936 : if (i >= min_var)
1937 : goto exit_label
1938 : i++
1939 : }
1940 : exit_label:
1941 : sub(dim_arg) = (dim_arg < corank) ? ml - m*extent + lcobound(dim_arg)
1942 : : m + lcobound(corank)
1943 : */
1944 :
1945 : /* this_image () - 1. */
1946 318 : tmp
1947 318 : = build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1, team);
1948 318 : tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
1949 : fold_convert (type, tmp), build_int_cst (type, 1));
1950 318 : if (corank == 1)
1951 : {
1952 : /* sub(1) = m + lcobound(corank). */
1953 8 : lbound = gfc_conv_descriptor_lbound_get (desc,
1954 8 : build_int_cst (TREE_TYPE (gfc_array_index_type),
1955 8 : corank+rank-1));
1956 8 : lbound = fold_convert (type, lbound);
1957 8 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
1958 :
1959 8 : se->expr = tmp;
1960 8 : return;
1961 : }
1962 :
1963 310 : m = gfc_create_var (type, NULL);
1964 310 : ml = gfc_create_var (type, NULL);
1965 310 : loop_var = gfc_create_var (gfc_array_dim_rank_type, NULL);
1966 310 : min_var = gfc_create_var (gfc_array_dim_rank_type, NULL);
1967 :
1968 : /* m = this_image () - 1. */
1969 310 : gfc_add_modify (&se->pre, m, tmp);
1970 :
1971 : /* min_var = min (rank + corank-2, rank + dim_arg - 1). */
1972 310 : tmp = fold_build2_loc (input_location, PLUS_EXPR, signed_char_type_node,
1973 : fold_convert_loc (input_location,
1974 : signed_char_type_node, dim_arg),
1975 310 : build_int_cst (signed_char_type_node, rank - 1));
1976 310 : tmp = fold_convert_loc (input_location, gfc_array_dim_rank_type, tmp);
1977 620 : tmp = fold_build2_loc (input_location, MIN_EXPR, gfc_array_dim_rank_type,
1978 310 : gfc_rank_cst[rank + corank - 2], tmp);
1979 310 : gfc_add_modify (&se->pre, min_var, tmp);
1980 :
1981 : /* i = rank. */
1982 310 : tmp = gfc_rank_cst[rank];
1983 310 : gfc_add_modify (&se->pre, loop_var, tmp);
1984 :
1985 310 : exit_label = gfc_build_label_decl (NULL_TREE);
1986 310 : TREE_USED (exit_label) = 1;
1987 :
1988 : /* Loop body. */
1989 310 : gfc_init_block (&loop);
1990 :
1991 : /* ml = m. */
1992 310 : gfc_add_modify (&loop, ml, m);
1993 :
1994 : /* extent = ... */
1995 310 : lbound = gfc_conv_descriptor_lbound_get (desc, loop_var);
1996 310 : ubound = gfc_conv_descriptor_ubound_get (desc, loop_var);
1997 310 : extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
1998 310 : extent = fold_convert (type, extent);
1999 :
2000 : /* m = m/extent. */
2001 310 : gfc_add_modify (&loop, m,
2002 : fold_build2_loc (input_location, TRUNC_DIV_EXPR, type,
2003 : m, extent));
2004 :
2005 : /* Exit condition: if (i >= min_var) goto exit_label. */
2006 310 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, loop_var,
2007 : min_var);
2008 310 : tmp = build1_v (GOTO_EXPR, exit_label);
2009 310 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
2010 : build_empty_stmt (input_location));
2011 310 : gfc_add_expr_to_block (&loop, tmp);
2012 :
2013 : /* Increment loop variable: i++. */
2014 310 : gfc_add_modify (&loop, loop_var,
2015 : fold_build2_loc (input_location, PLUS_EXPR,
2016 310 : TREE_TYPE (loop_var), loop_var,
2017 : gfc_rank_cst[1]));
2018 :
2019 : /* Making the loop... actually loop! */
2020 310 : tmp = gfc_finish_block (&loop);
2021 310 : tmp = build1_v (LOOP_EXPR, tmp);
2022 310 : gfc_add_expr_to_block (&se->pre, tmp);
2023 :
2024 : /* The exit label. */
2025 310 : tmp = build1_v (LABEL_EXPR, exit_label);
2026 310 : gfc_add_expr_to_block (&se->pre, tmp);
2027 :
2028 : /* sub(co_dim) = (co_dim < corank) ? ml - m*extent + lcobound(dim_arg)
2029 : : m + lcobound(corank) */
2030 :
2031 310 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, dim_arg,
2032 310 : build_int_cst (TREE_TYPE (dim_arg), corank));
2033 :
2034 620 : lbound = gfc_conv_descriptor_lbound_get (desc,
2035 : fold_build2_loc (input_location, PLUS_EXPR,
2036 310 : TREE_TYPE (dim_arg), dim_arg,
2037 310 : build_int_cst (TREE_TYPE (dim_arg), rank-1)));
2038 310 : lbound = fold_convert (type, lbound);
2039 :
2040 310 : tmp = fold_build2_loc (input_location, MINUS_EXPR, type, ml,
2041 : fold_build2_loc (input_location, MULT_EXPR, type,
2042 : m, extent));
2043 310 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, tmp, lbound);
2044 :
2045 310 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond, tmp,
2046 : fold_build2_loc (input_location, PLUS_EXPR, type,
2047 : m, lbound));
2048 : }
2049 :
2050 :
2051 : /* Convert a call to image_status. */
2052 :
2053 : static void
2054 25 : conv_intrinsic_image_status (gfc_se *se, gfc_expr *expr)
2055 : {
2056 25 : unsigned int num_args;
2057 25 : tree *args, tmp;
2058 :
2059 25 : num_args = gfc_intrinsic_argument_list_length (expr);
2060 25 : args = XALLOCAVEC (tree, num_args);
2061 25 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
2062 : /* In args[0] the number of the image the status is desired for has to be
2063 : given. */
2064 :
2065 25 : if (flag_coarray == GFC_FCOARRAY_SINGLE)
2066 : {
2067 0 : tree arg;
2068 0 : arg = gfc_evaluate_now (args[0], &se->pre);
2069 0 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
2070 : fold_convert (integer_type_node, arg),
2071 : integer_one_node);
2072 0 : tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
2073 : tmp, integer_zero_node,
2074 : build_int_cst (integer_type_node,
2075 : GFC_STAT_STOPPED_IMAGE));
2076 : }
2077 25 : else if (flag_coarray == GFC_FCOARRAY_LIB)
2078 : /* The team is optional and therefore needs to be a pointer to the opaque
2079 : pointer. */
2080 29 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_image_status, 2,
2081 : args[0],
2082 : num_args < 2
2083 : ? null_pointer_node
2084 4 : : gfc_build_addr_expr (NULL_TREE, args[1]));
2085 : else
2086 0 : gcc_unreachable ();
2087 :
2088 25 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
2089 25 : }
2090 :
2091 : static void
2092 39 : conv_intrinsic_team_number (gfc_se *se, gfc_expr *expr)
2093 : {
2094 39 : unsigned int num_args;
2095 :
2096 39 : tree *args, tmp;
2097 :
2098 39 : num_args = gfc_intrinsic_argument_list_length (expr);
2099 39 : args = XALLOCAVEC (tree, num_args);
2100 39 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
2101 :
2102 39 : if (flag_coarray == GFC_FCOARRAY_SINGLE)
2103 : /* Only the initial team exists, and its team number is -1. */
2104 24 : tmp = build_int_cst (integer_type_node, -1);
2105 15 : else if (flag_coarray == GFC_FCOARRAY_LIB)
2106 15 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_team_number, 1,
2107 15 : expr->value.function.actual->expr
2108 : ? args[0] : null_pointer_node);
2109 : else
2110 0 : gcc_unreachable ();
2111 :
2112 39 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
2113 39 : }
2114 :
2115 :
2116 : static void
2117 223 : trans_image_index (gfc_se * se, gfc_expr *expr)
2118 : {
2119 223 : tree num_images, cond, coindex, type, lbound, ubound, desc, subdesc, tmp,
2120 223 : invalid_bound, team = null_pointer_node, team_number = null_pointer_node;
2121 223 : gfc_se argse, subse;
2122 223 : int rank, corank, codim;
2123 :
2124 223 : type = gfc_get_int_type (gfc_default_integer_kind);
2125 223 : corank = expr->value.function.actual->expr->corank;
2126 223 : rank = expr->value.function.actual->expr->rank;
2127 :
2128 : /* Obtain the descriptor of the COARRAY. */
2129 223 : gfc_init_se (&argse, NULL);
2130 223 : argse.want_coarray = 1;
2131 223 : gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
2132 223 : gfc_add_block_to_block (&se->pre, &argse.pre);
2133 223 : gfc_add_block_to_block (&se->post, &argse.post);
2134 223 : desc = argse.expr;
2135 :
2136 : /* Obtain a handle to the SUB argument. */
2137 223 : gfc_init_se (&subse, NULL);
2138 223 : gfc_conv_expr_descriptor (&subse, expr->value.function.actual->next->expr);
2139 223 : gfc_add_block_to_block (&se->pre, &subse.pre);
2140 223 : gfc_add_block_to_block (&se->post, &subse.post);
2141 223 : subdesc = build_fold_indirect_ref_loc (input_location,
2142 : gfc_conv_descriptor_data_get (subse.expr));
2143 :
2144 223 : if (expr->value.function.actual->next->next->expr)
2145 : {
2146 24 : gfc_init_se (&argse, NULL);
2147 24 : gfc_conv_expr_val (&argse, expr->value.function.actual->next->next->expr);
2148 24 : if (expr->value.function.actual->next->next->expr->ts.type == BT_DERIVED)
2149 12 : team = argse.expr;
2150 : else
2151 12 : team_number = gfc_build_addr_expr (
2152 : NULL_TREE,
2153 : gfc_trans_force_lval (&argse.pre,
2154 : fold_convert (integer_type_node, argse.expr)));
2155 24 : gfc_add_block_to_block (&se->pre, &argse.pre);
2156 24 : gfc_add_block_to_block (&se->post, &argse.post);
2157 : }
2158 :
2159 : /* Fortran 2008 does not require that the values remain in the cobounds,
2160 : thus we need explicitly check this - and return 0 if they are exceeded. */
2161 :
2162 223 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
2163 223 : tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1], NULL);
2164 223 : invalid_bound = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2165 : fold_convert (gfc_array_index_type, tmp),
2166 : lbound);
2167 :
2168 473 : for (codim = corank + rank - 2; codim >= rank; codim--)
2169 : {
2170 250 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
2171 250 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
2172 250 : tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
2173 250 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2174 : fold_convert (gfc_array_index_type, tmp),
2175 : lbound);
2176 250 : invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2177 : logical_type_node, invalid_bound, cond);
2178 250 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2179 : fold_convert (gfc_array_index_type, tmp),
2180 : ubound);
2181 250 : invalid_bound = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2182 : logical_type_node, invalid_bound, cond);
2183 : }
2184 :
2185 223 : invalid_bound = gfc_unlikely (invalid_bound, PRED_FORTRAN_INVALID_BOUND);
2186 :
2187 : /* See Fortran 2008, C.10 for the following algorithm. */
2188 :
2189 : /* coindex = sub(corank) - lcobound(n). */
2190 223 : coindex = fold_convert (gfc_array_index_type,
2191 : gfc_build_array_ref (subdesc, gfc_rank_cst[corank-1],
2192 : NULL));
2193 223 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[rank+corank-1]);
2194 223 : coindex = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2195 : fold_convert (gfc_array_index_type, coindex),
2196 : lbound);
2197 :
2198 696 : for (codim = corank + rank - 2; codim >= rank; codim--)
2199 : {
2200 250 : tree extent, ubound;
2201 :
2202 : /* coindex = coindex*extent(codim) + sub(codim) - lcobound(codim). */
2203 250 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
2204 250 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
2205 250 : extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
2206 :
2207 : /* coindex *= extent. */
2208 250 : coindex = fold_build2_loc (input_location, MULT_EXPR,
2209 : gfc_array_index_type, coindex, extent);
2210 :
2211 : /* coindex += sub(codim). */
2212 250 : tmp = gfc_build_array_ref (subdesc, gfc_rank_cst[codim-rank], NULL);
2213 250 : coindex = fold_build2_loc (input_location, PLUS_EXPR,
2214 : gfc_array_index_type, coindex,
2215 : fold_convert (gfc_array_index_type, tmp));
2216 :
2217 : /* coindex -= lbound(codim). */
2218 250 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
2219 250 : coindex = fold_build2_loc (input_location, MINUS_EXPR,
2220 : gfc_array_index_type, coindex, lbound);
2221 : }
2222 :
2223 223 : coindex = fold_build2_loc (input_location, PLUS_EXPR, type,
2224 : fold_convert(type, coindex),
2225 : build_int_cst (type, 1));
2226 :
2227 : /* Return 0 if "coindex" exceeds num_images(). */
2228 :
2229 223 : if (flag_coarray == GFC_FCOARRAY_SINGLE)
2230 118 : num_images = build_int_cst (type, 1);
2231 : else
2232 : {
2233 105 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
2234 : team, team_number);
2235 105 : num_images = fold_convert (type, tmp);
2236 : }
2237 :
2238 223 : tmp = gfc_create_var (type, NULL);
2239 223 : gfc_add_modify (&se->pre, tmp, coindex);
2240 :
2241 223 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node, tmp,
2242 : num_images);
2243 223 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, logical_type_node,
2244 : cond,
2245 : fold_convert (logical_type_node, invalid_bound));
2246 223 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
2247 : build_int_cst (type, 0), tmp);
2248 223 : }
2249 :
2250 : static void
2251 814 : trans_num_images (gfc_se * se, gfc_expr *expr)
2252 : {
2253 814 : tree tmp, team = null_pointer_node, team_number = null_pointer_node;
2254 814 : gfc_se argse;
2255 :
2256 814 : if (expr->value.function.actual->expr)
2257 : {
2258 18 : gfc_init_se (&argse, NULL);
2259 18 : gfc_conv_expr_val (&argse, expr->value.function.actual->expr);
2260 18 : if (expr->value.function.actual->expr->ts.type == BT_DERIVED)
2261 6 : team = argse.expr;
2262 : else
2263 12 : team_number = gfc_build_addr_expr (
2264 : NULL_TREE,
2265 : gfc_trans_force_lval (&argse.pre,
2266 : fold_convert (integer_type_node, argse.expr)));
2267 18 : gfc_add_block_to_block (&se->pre, &argse.pre);
2268 18 : gfc_add_block_to_block (&se->post, &argse.post);
2269 : }
2270 :
2271 814 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images, 2,
2272 : team, team_number);
2273 814 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
2274 814 : }
2275 :
2276 :
2277 : static void
2278 13376 : gfc_conv_intrinsic_rank (gfc_se *se, gfc_expr *expr)
2279 : {
2280 13376 : gfc_se argse;
2281 :
2282 13376 : gfc_init_se (&argse, NULL);
2283 13376 : argse.data_not_needed = 1;
2284 13376 : argse.descriptor_only = 1;
2285 :
2286 13376 : gfc_conv_expr_descriptor (&argse, expr->value.function.actual->expr);
2287 13376 : gfc_add_block_to_block (&se->pre, &argse.pre);
2288 13376 : gfc_add_block_to_block (&se->post, &argse.post);
2289 :
2290 13376 : se->expr = gfc_conv_descriptor_rank_get (argse.expr);
2291 13376 : se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind),
2292 : se->expr);
2293 13376 : }
2294 :
2295 :
2296 : static void
2297 748 : gfc_conv_intrinsic_is_contiguous (gfc_se * se, gfc_expr * expr)
2298 : {
2299 748 : gfc_expr *arg;
2300 748 : arg = expr->value.function.actual->expr;
2301 748 : gfc_conv_is_contiguous_expr (se, arg);
2302 748 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
2303 748 : }
2304 :
2305 : /* This function does the work for gfc_conv_intrinsic_is_contiguous,
2306 : plus it can be called directly. */
2307 :
2308 : void
2309 2098 : gfc_conv_is_contiguous_expr (gfc_se *se, gfc_expr *arg)
2310 : {
2311 2098 : gfc_ss *ss;
2312 2098 : gfc_se argse;
2313 2098 : tree desc, tmp, stride, extent, cond;
2314 2098 : int i;
2315 2098 : tree fncall0;
2316 2098 : gfc_array_spec *as;
2317 2098 : gfc_symbol *sym = NULL;
2318 :
2319 2098 : if (arg->ts.type == BT_CLASS)
2320 96 : gfc_add_class_array_ref (arg);
2321 :
2322 2098 : if (arg->expr_type == EXPR_VARIABLE)
2323 2062 : sym = arg->symtree->n.sym;
2324 :
2325 2098 : ss = gfc_walk_expr (arg);
2326 2098 : gcc_assert (ss != gfc_ss_terminator);
2327 2098 : gfc_init_se (&argse, NULL);
2328 2098 : argse.data_not_needed = 1;
2329 2098 : gfc_conv_expr_descriptor (&argse, arg);
2330 :
2331 2098 : as = gfc_get_full_arrayspec_from_expr (arg);
2332 :
2333 : /* Create: stride[0] == 1 && stride[1] == extend[0]*stride[0] && ...
2334 : Note in addition that zero-sized arrays don't count as contiguous. */
2335 :
2336 2098 : if (as && as->type == AS_ASSUMED_RANK)
2337 : {
2338 : /* Build the call to is_contiguous0. */
2339 250 : argse.want_pointer = 1;
2340 250 : gfc_conv_expr_descriptor (&argse, arg);
2341 250 : gfc_add_block_to_block (&se->pre, &argse.pre);
2342 250 : gfc_add_block_to_block (&se->post, &argse.post);
2343 250 : tree ptr = gfc_evaluate_now (argse.expr, &se->pre);
2344 250 : fncall0 = build_call_expr_loc (input_location,
2345 : gfor_fndecl_is_contiguous0, 1, ptr);
2346 250 : desc = build_fold_indirect_ref_loc (input_location, ptr);
2347 250 : se->expr = fncall0;
2348 250 : se->expr = convert (boolean_type_node, se->expr);
2349 250 : }
2350 : else
2351 : {
2352 1848 : gfc_add_block_to_block (&se->pre, &argse.pre);
2353 1848 : gfc_add_block_to_block (&se->post, &argse.post);
2354 1848 : desc = gfc_evaluate_now (argse.expr, &se->pre);
2355 :
2356 1848 : stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[0]);
2357 1848 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2358 1848 : stride, build_int_cst (TREE_TYPE (stride), 1));
2359 :
2360 2171 : for (i = 0; i < arg->rank - 1; i++)
2361 : {
2362 323 : tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
2363 323 : extent = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
2364 323 : extent = fold_build2_loc (input_location, MINUS_EXPR,
2365 : gfc_array_index_type, extent, tmp);
2366 323 : extent = fold_build2_loc (input_location, PLUS_EXPR,
2367 : gfc_array_index_type, extent,
2368 : gfc_index_one_node);
2369 323 : tmp = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[i]);
2370 323 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
2371 : tmp, extent);
2372 323 : stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[i+1]);
2373 323 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2374 : stride, tmp);
2375 323 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2376 : boolean_type_node, cond, tmp);
2377 : }
2378 1848 : se->expr = cond;
2379 : }
2380 :
2381 : /* An array that is addressed by the span of its descriptor needs to be
2382 : checked if that span differs from the element size. */
2383 827 : if (as && sym && !sym->attr.contiguous
2384 2925 : && (IS_POINTER (sym) || gfc_is_span_addressed_dummy (sym)))
2385 : {
2386 187 : tree span = gfc_conv_descriptor_span_get (desc);
2387 187 : tmp = fold_convert (TREE_TYPE (span),
2388 : gfc_conv_descriptor_elem_len_get (desc));
2389 187 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2390 : span, tmp);
2391 187 : se->expr = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2392 : boolean_type_node, cond,
2393 : convert (boolean_type_node, se->expr));
2394 : }
2395 :
2396 2098 : if (as && as->type == AS_ASSUMED_RANK)
2397 : {
2398 250 : tree rank = gfc_conv_descriptor_rank_get (desc);
2399 250 : tree scalar = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2400 : rank, gfc_rank_cst[0]);
2401 250 : se->expr = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
2402 250 : TREE_TYPE (se->expr), scalar, se->expr);
2403 : }
2404 :
2405 2098 : gfc_free_ss_chain (ss);
2406 2098 : }
2407 :
2408 :
2409 : /* Evaluate a single upper or lower bound. */
2410 : /* TODO: bound intrinsic generates way too much unnecessary code. */
2411 :
2412 : static void
2413 16349 : gfc_conv_intrinsic_bound (gfc_se * se, gfc_expr * expr, enum gfc_isym_id op)
2414 : {
2415 16349 : gfc_actual_arglist *arg;
2416 16349 : gfc_actual_arglist *arg2;
2417 16349 : tree desc;
2418 16349 : tree type;
2419 16349 : tree bound;
2420 16349 : tree tmp;
2421 16349 : tree cond, cond1;
2422 16349 : tree ubound;
2423 16349 : tree lbound;
2424 16349 : tree size;
2425 16349 : gfc_se argse;
2426 16349 : gfc_array_spec * as;
2427 16349 : bool assumed_rank_lb_one;
2428 :
2429 16349 : arg = expr->value.function.actual;
2430 16349 : arg2 = arg->next;
2431 :
2432 16349 : if (se->ss)
2433 : {
2434 : /* Create an implicit second parameter from the loop variable. */
2435 8016 : gcc_assert (!arg2->expr || op == GFC_ISYM_SHAPE);
2436 8016 : gcc_assert (se->loop->dimen == 1);
2437 8016 : gcc_assert (se->ss->info->expr == expr);
2438 8016 : gfc_advance_se_ss_chain (se);
2439 8016 : bound = se->loop->loopvar[0];
2440 8016 : bound = fold_build2_loc (input_location, MINUS_EXPR,
2441 : gfc_array_index_type, bound,
2442 : se->loop->from[0]);
2443 8016 : bound = fold_convert_loc (input_location, gfc_array_dim_rank_type,
2444 : bound);
2445 : }
2446 : else
2447 : {
2448 : /* use the passed argument. */
2449 8333 : gcc_assert (arg2->expr);
2450 8333 : gfc_init_se (&argse, NULL);
2451 8333 : gfc_conv_expr_type (&argse, arg2->expr, gfc_array_dim_rank_type);
2452 8333 : gfc_add_block_to_block (&se->pre, &argse.pre);
2453 8333 : bound = argse.expr;
2454 : /* Convert from one based to zero based. */
2455 8333 : bound = fold_build2_loc (input_location, MINUS_EXPR,
2456 : gfc_array_dim_rank_type, bound,
2457 : gfc_rank_cst[1]);
2458 : }
2459 :
2460 : /* TODO: don't re-evaluate the descriptor on each iteration. */
2461 : /* Get a descriptor for the first parameter. */
2462 16349 : gfc_init_se (&argse, NULL);
2463 16349 : gfc_conv_expr_descriptor (&argse, arg->expr);
2464 16349 : gfc_add_block_to_block (&se->pre, &argse.pre);
2465 16349 : gfc_add_block_to_block (&se->post, &argse.post);
2466 :
2467 16349 : desc = argse.expr;
2468 :
2469 16349 : as = gfc_get_full_arrayspec_from_expr (arg->expr);
2470 :
2471 16349 : if (INTEGER_CST_P (bound))
2472 : {
2473 8213 : gcc_assert (op != GFC_ISYM_SHAPE);
2474 7976 : if (((!as || as->type != AS_ASSUMED_RANK)
2475 7305 : && wi::geu_p (wi::to_wide (bound),
2476 7305 : GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))))
2477 16426 : || wi::gtu_p (wi::to_wide (bound), GFC_MAX_DIMENSIONS))
2478 0 : gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
2479 : "dimension index",
2480 : (op == GFC_ISYM_UBOUND) ? "UBOUND" : "LBOUND",
2481 : &expr->where);
2482 : }
2483 :
2484 16349 : if (!INTEGER_CST_P (bound) || (as && as->type == AS_ASSUMED_RANK))
2485 : {
2486 9044 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2487 : {
2488 651 : bound = gfc_evaluate_now (bound, &se->pre);
2489 651 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2490 : bound, gfc_rank_cst[0]);
2491 651 : if (as && as->type == AS_ASSUMED_RANK)
2492 546 : tmp = gfc_conv_descriptor_rank_get (desc);
2493 : else
2494 105 : tmp = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc))];
2495 651 : tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
2496 : bound, tmp);
2497 651 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
2498 : logical_type_node, cond, tmp);
2499 651 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
2500 : gfc_msg_fault);
2501 : }
2502 : }
2503 :
2504 : /* Take care of the lbound shift for assumed-rank arrays that are
2505 : nonallocatable and nonpointers. Those have a lbound of 1. */
2506 15765 : assumed_rank_lb_one = as && as->type == AS_ASSUMED_RANK
2507 11229 : && ((arg->expr->ts.type != BT_CLASS
2508 1987 : && !arg->expr->symtree->n.sym->attr.allocatable
2509 1644 : && !arg->expr->symtree->n.sym->attr.pointer)
2510 920 : || (arg->expr->ts.type == BT_CLASS
2511 198 : && !CLASS_DATA (arg->expr)->attr.allocatable
2512 162 : && !CLASS_DATA (arg->expr)->attr.class_pointer));
2513 :
2514 16349 : ubound = gfc_conv_descriptor_ubound_get (desc, bound);
2515 16349 : lbound = gfc_conv_descriptor_lbound_get (desc, bound);
2516 16349 : size = fold_build2_loc (input_location, MINUS_EXPR,
2517 : gfc_array_index_type, ubound, lbound);
2518 16349 : size = fold_build2_loc (input_location, PLUS_EXPR,
2519 : gfc_array_index_type, size, gfc_index_one_node);
2520 :
2521 : /* 13.14.53: Result value for LBOUND
2522 :
2523 : Case (i): For an array section or for an array expression other than a
2524 : whole array or array structure component, LBOUND(ARRAY, DIM)
2525 : has the value 1. For a whole array or array structure
2526 : component, LBOUND(ARRAY, DIM) has the value:
2527 : (a) equal to the lower bound for subscript DIM of ARRAY if
2528 : dimension DIM of ARRAY does not have extent zero
2529 : or if ARRAY is an assumed-size array of rank DIM,
2530 : or (b) 1 otherwise.
2531 :
2532 : 13.14.113: Result value for UBOUND
2533 :
2534 : Case (i): For an array section or for an array expression other than a
2535 : whole array or array structure component, UBOUND(ARRAY, DIM)
2536 : has the value equal to the number of elements in the given
2537 : dimension; otherwise, it has a value equal to the upper bound
2538 : for subscript DIM of ARRAY if dimension DIM of ARRAY does
2539 : not have size zero and has value zero if dimension DIM has
2540 : size zero. */
2541 :
2542 16349 : if (op == GFC_ISYM_LBOUND && assumed_rank_lb_one)
2543 556 : se->expr = gfc_index_one_node;
2544 15793 : else if (as)
2545 : {
2546 15209 : if (op == GFC_ISYM_UBOUND)
2547 : {
2548 5407 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2549 : size, gfc_index_zero_node);
2550 10186 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2551 : gfc_array_index_type, cond,
2552 : (assumed_rank_lb_one ? size : ubound),
2553 : gfc_index_zero_node);
2554 : }
2555 9802 : else if (op == GFC_ISYM_LBOUND)
2556 : {
2557 4931 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2558 : size, gfc_index_zero_node);
2559 4931 : if (as->type == AS_ASSUMED_SIZE)
2560 : {
2561 98 : cond1 = fold_build2_loc (input_location, EQ_EXPR,
2562 : logical_type_node, bound,
2563 98 : build_int_cst (TREE_TYPE (bound),
2564 98 : arg->expr->rank - 1));
2565 98 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2566 : logical_type_node, cond, cond1);
2567 : }
2568 4931 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2569 : gfc_array_index_type, cond,
2570 : lbound, gfc_index_one_node);
2571 : }
2572 4871 : else if (op == GFC_ISYM_SHAPE)
2573 4871 : se->expr = fold_build2_loc (input_location, MAX_EXPR,
2574 : gfc_array_index_type, size,
2575 : gfc_index_zero_node);
2576 : else
2577 0 : gcc_unreachable ();
2578 :
2579 : /* According to F2018 16.9.172, para 5, an assumed rank object,
2580 : argument associated with and assumed size array, has the ubound
2581 : of the final dimension set to -1 and UBOUND must return this.
2582 : Similarly for the SHAPE intrinsic. */
2583 15209 : if (op != GFC_ISYM_LBOUND && assumed_rank_lb_one)
2584 : {
2585 835 : tree minus_one = build_int_cst (gfc_array_index_type, -1);
2586 835 : tree rank = gfc_conv_descriptor_rank_get (desc);
2587 835 : rank = fold_build2_loc (input_location, MINUS_EXPR,
2588 : gfc_array_dim_rank_type, rank,
2589 : gfc_rank_cst[1]);
2590 :
2591 : /* Fix the expression to stop it from becoming even more
2592 : complicated. */
2593 835 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
2594 :
2595 : /* Descriptors for assumed-size arrays have ubound = -1
2596 : in the last dimension. */
2597 835 : cond1 = fold_build2_loc (input_location, EQ_EXPR,
2598 : logical_type_node, ubound, minus_one);
2599 835 : cond = fold_build2_loc (input_location, EQ_EXPR,
2600 : logical_type_node, bound, rank);
2601 835 : cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2602 : logical_type_node, cond, cond1);
2603 835 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2604 : gfc_array_index_type, cond,
2605 : minus_one, se->expr);
2606 : }
2607 : }
2608 : else /* as is null; this is an old-fashioned 1-based array. */
2609 : {
2610 584 : if (op != GFC_ISYM_LBOUND)
2611 : {
2612 482 : se->expr = fold_build2_loc (input_location, MAX_EXPR,
2613 : gfc_array_index_type, size,
2614 : gfc_index_zero_node);
2615 : }
2616 : else
2617 102 : se->expr = gfc_index_one_node;
2618 : }
2619 :
2620 :
2621 16349 : type = gfc_typenode_for_spec (&expr->ts);
2622 16349 : se->expr = convert (type, se->expr);
2623 16349 : }
2624 :
2625 :
2626 : static void
2627 666 : conv_intrinsic_cobound (gfc_se * se, gfc_expr * expr)
2628 : {
2629 666 : gfc_actual_arglist *arg;
2630 666 : gfc_actual_arglist *arg2;
2631 666 : gfc_se argse;
2632 666 : tree bound, lbound, resbound, resbound2, desc, cond, tmp;
2633 666 : tree type;
2634 666 : int corank;
2635 :
2636 666 : gcc_assert (expr->value.function.isym->id == GFC_ISYM_LCOBOUND
2637 : || expr->value.function.isym->id == GFC_ISYM_UCOBOUND
2638 : || expr->value.function.isym->id == GFC_ISYM_COSHAPE
2639 : || expr->value.function.isym->id == GFC_ISYM_THIS_IMAGE);
2640 :
2641 666 : arg = expr->value.function.actual;
2642 666 : arg2 = arg->next;
2643 :
2644 666 : gcc_assert (arg->expr->expr_type == EXPR_VARIABLE);
2645 666 : corank = arg->expr->corank;
2646 :
2647 666 : gfc_init_se (&argse, NULL);
2648 666 : argse.want_coarray = 1;
2649 :
2650 666 : gfc_conv_expr_descriptor (&argse, arg->expr);
2651 666 : gfc_add_block_to_block (&se->pre, &argse.pre);
2652 666 : gfc_add_block_to_block (&se->post, &argse.post);
2653 666 : desc = argse.expr;
2654 :
2655 666 : if (se->ss)
2656 : {
2657 : /* Create an implicit second parameter from the loop variable. */
2658 238 : gcc_assert (!arg2->expr
2659 : || expr->value.function.isym->id == GFC_ISYM_COSHAPE);
2660 238 : gcc_assert (corank > 0);
2661 238 : gcc_assert (se->loop->dimen == 1);
2662 238 : gcc_assert (se->ss->info->expr == expr);
2663 :
2664 238 : bound = fold_convert_loc (input_location, gfc_array_dim_rank_type,
2665 : se->loop->loopvar[0]);
2666 238 : tree rank = gfc_rank_cst[arg->expr->rank];
2667 238 : bound = fold_build2_loc (input_location, PLUS_EXPR,
2668 : gfc_array_dim_rank_type, bound, rank);
2669 238 : gfc_advance_se_ss_chain (se);
2670 : }
2671 428 : else if (expr->value.function.isym->id == GFC_ISYM_COSHAPE)
2672 0 : bound = gfc_rank_cst[1];
2673 : else
2674 : {
2675 428 : gcc_assert (arg2->expr);
2676 428 : gfc_init_se (&argse, NULL);
2677 428 : gfc_conv_expr_type (&argse, arg2->expr, gfc_array_dim_rank_type);
2678 428 : gfc_add_block_to_block (&se->pre, &argse.pre);
2679 428 : bound = argse.expr;
2680 :
2681 428 : if (INTEGER_CST_P (bound))
2682 : {
2683 334 : if (wi::ltu_p (wi::to_wide (bound), 1)
2684 668 : || wi::gtu_p (wi::to_wide (bound),
2685 334 : GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))))
2686 0 : gfc_error ("%<dim%> argument of %s intrinsic at %L is not a valid "
2687 0 : "dimension index", expr->value.function.isym->name,
2688 : &expr->where);
2689 : }
2690 94 : else if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2691 : {
2692 36 : bound = gfc_evaluate_now (bound, &se->pre);
2693 36 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2694 : bound, gfc_rank_cst[1]);
2695 36 : tree rank = gfc_rank_cst[GFC_TYPE_ARRAY_CORANK (TREE_TYPE (desc))];
2696 36 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2697 : bound, rank);
2698 36 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
2699 : logical_type_node, cond, tmp);
2700 36 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
2701 : gfc_msg_fault);
2702 : }
2703 :
2704 :
2705 : /* Subtract 1 to get to zero based and add dimensions. */
2706 428 : switch (arg->expr->rank)
2707 : {
2708 70 : case 0:
2709 70 : bound = fold_build2_loc (input_location, MINUS_EXPR,
2710 : gfc_array_dim_rank_type, bound,
2711 : gfc_rank_cst[1]);
2712 : case 1:
2713 : break;
2714 38 : default:
2715 38 : {
2716 38 : tree rank = gfc_rank_cst[arg->expr->rank - 1];
2717 38 : bound = fold_build2_loc (input_location, PLUS_EXPR,
2718 : gfc_array_dim_rank_type, bound, rank);
2719 : }
2720 : }
2721 : }
2722 :
2723 666 : resbound = gfc_conv_descriptor_lbound_get (desc, bound);
2724 :
2725 : /* COSHAPE needs the lower cobound and so it is stashed here before resbound
2726 : is overwritten. */
2727 666 : lbound = NULL_TREE;
2728 666 : if (expr->value.function.isym->id == GFC_ISYM_COSHAPE)
2729 4 : lbound = resbound;
2730 :
2731 : /* Handle UCOBOUND with special handling of the last codimension. */
2732 666 : if (expr->value.function.isym->id == GFC_ISYM_UCOBOUND
2733 422 : || expr->value.function.isym->id == GFC_ISYM_COSHAPE)
2734 : {
2735 : /* Last codimension: For -fcoarray=single just return
2736 : the lcobound - otherwise add
2737 : ceiling (real (num_images ()) / real (size)) - 1
2738 : = (num_images () + size - 1) / size - 1
2739 : = (num_images - 1) / size(),
2740 : where size is the product of the extent of all but the last
2741 : codimension. */
2742 :
2743 248 : if (flag_coarray != GFC_FCOARRAY_SINGLE && corank > 1)
2744 : {
2745 64 : tree cosize;
2746 :
2747 64 : cosize = gfc_conv_descriptor_cosize (desc, arg->expr->rank, corank);
2748 64 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
2749 : 2, null_pointer_node, null_pointer_node);
2750 64 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2751 : gfc_array_index_type,
2752 : fold_convert (gfc_array_index_type, tmp),
2753 : build_int_cst (gfc_array_index_type, 1));
2754 64 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
2755 : gfc_array_index_type, tmp,
2756 : fold_convert (gfc_array_index_type, cosize));
2757 64 : resbound = fold_build2_loc (input_location, PLUS_EXPR,
2758 : gfc_array_index_type, resbound, tmp);
2759 64 : }
2760 184 : else if (flag_coarray != GFC_FCOARRAY_SINGLE)
2761 : {
2762 : /* ubound = lbound + num_images() - 1. */
2763 44 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
2764 : 2, null_pointer_node, null_pointer_node);
2765 44 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2766 : gfc_array_index_type,
2767 : fold_convert (gfc_array_index_type, tmp),
2768 : build_int_cst (gfc_array_index_type, 1));
2769 44 : resbound = fold_build2_loc (input_location, PLUS_EXPR,
2770 : gfc_array_index_type, resbound, tmp);
2771 : }
2772 :
2773 248 : if (corank > 1)
2774 : {
2775 171 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
2776 : bound,
2777 171 : build_int_cst (TREE_TYPE (bound),
2778 171 : arg->expr->rank + corank - 1));
2779 :
2780 171 : resbound2 = gfc_conv_descriptor_ubound_get (desc, bound);
2781 171 : se->expr = fold_build3_loc (input_location, COND_EXPR,
2782 : gfc_array_index_type, cond,
2783 : resbound, resbound2);
2784 : }
2785 : else
2786 : se->expr = resbound;
2787 :
2788 : /* Get the coshape for this dimension. */
2789 248 : if (expr->value.function.isym->id == GFC_ISYM_COSHAPE)
2790 : {
2791 4 : gcc_assert (lbound != NULL_TREE);
2792 4 : se->expr = fold_build2_loc (input_location, MINUS_EXPR,
2793 : gfc_array_index_type,
2794 : se->expr, lbound);
2795 4 : se->expr = fold_build2_loc (input_location, PLUS_EXPR,
2796 : gfc_array_index_type,
2797 : se->expr, gfc_index_one_node);
2798 : }
2799 : }
2800 : else
2801 418 : se->expr = resbound;
2802 :
2803 666 : type = gfc_typenode_for_spec (&expr->ts);
2804 666 : se->expr = convert (type, se->expr);
2805 666 : }
2806 :
2807 :
2808 : static void
2809 2429 : conv_intrinsic_stride (gfc_se * se, gfc_expr * expr)
2810 : {
2811 2429 : gfc_actual_arglist *array_arg;
2812 2429 : gfc_actual_arglist *dim_arg;
2813 2429 : gfc_se argse;
2814 2429 : tree desc, tmp;
2815 :
2816 2429 : array_arg = expr->value.function.actual;
2817 2429 : dim_arg = array_arg->next;
2818 :
2819 2429 : gcc_assert (array_arg->expr->expr_type == EXPR_VARIABLE);
2820 :
2821 2429 : gfc_init_se (&argse, NULL);
2822 2429 : gfc_conv_expr_descriptor (&argse, array_arg->expr);
2823 2429 : gfc_add_block_to_block (&se->pre, &argse.pre);
2824 2429 : gfc_add_block_to_block (&se->post, &argse.post);
2825 2429 : desc = argse.expr;
2826 :
2827 2429 : gcc_assert (dim_arg->expr);
2828 2429 : gfc_init_se (&argse, NULL);
2829 2429 : gfc_conv_expr_type (&argse, dim_arg->expr, gfc_array_index_type);
2830 2429 : gfc_add_block_to_block (&se->pre, &argse.pre);
2831 2429 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2832 : argse.expr, gfc_index_one_node);
2833 2429 : se->expr = gfc_conv_descriptor_stride_get (desc, tmp);
2834 2429 : }
2835 :
2836 : static void
2837 8028 : gfc_conv_intrinsic_abs (gfc_se * se, gfc_expr * expr)
2838 : {
2839 8028 : tree arg, cabs;
2840 :
2841 8028 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
2842 :
2843 8028 : switch (expr->value.function.actual->expr->ts.type)
2844 : {
2845 7004 : case BT_INTEGER:
2846 7004 : case BT_REAL:
2847 7004 : se->expr = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (arg),
2848 : arg);
2849 7004 : break;
2850 :
2851 1024 : case BT_COMPLEX:
2852 1024 : cabs = gfc_builtin_decl_for_float_kind (BUILT_IN_CABS, expr->ts.kind);
2853 1024 : se->expr = build_call_expr_loc (input_location, cabs, 1, arg);
2854 1024 : break;
2855 :
2856 0 : default:
2857 0 : gcc_unreachable ();
2858 : }
2859 8028 : }
2860 :
2861 :
2862 : /* Create a complex value from one or two real components. */
2863 :
2864 : static void
2865 497 : gfc_conv_intrinsic_cmplx (gfc_se * se, gfc_expr * expr, int both)
2866 : {
2867 497 : tree real;
2868 497 : tree imag;
2869 497 : tree type;
2870 497 : tree *args;
2871 497 : unsigned int num_args;
2872 :
2873 497 : num_args = gfc_intrinsic_argument_list_length (expr);
2874 497 : args = XALLOCAVEC (tree, num_args);
2875 :
2876 497 : type = gfc_typenode_for_spec (&expr->ts);
2877 497 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
2878 497 : real = convert (TREE_TYPE (type), args[0]);
2879 497 : if (both)
2880 453 : imag = convert (TREE_TYPE (type), args[1]);
2881 44 : else if (TREE_CODE (TREE_TYPE (args[0])) == COMPLEX_TYPE)
2882 : {
2883 30 : imag = fold_build1_loc (input_location, IMAGPART_EXPR,
2884 30 : TREE_TYPE (TREE_TYPE (args[0])), args[0]);
2885 30 : imag = convert (TREE_TYPE (type), imag);
2886 : }
2887 : else
2888 14 : imag = build_real_from_int_cst (TREE_TYPE (type), integer_zero_node);
2889 :
2890 497 : se->expr = fold_build2_loc (input_location, COMPLEX_EXPR, type, real, imag);
2891 497 : }
2892 :
2893 :
2894 : /* Remainder function MOD(A, P) = A - INT(A / P) * P
2895 : MODULO(A, P) = A - FLOOR (A / P) * P
2896 :
2897 : The obvious algorithms above are numerically instable for large
2898 : arguments, hence these intrinsics are instead implemented via calls
2899 : to the fmod family of functions. It is the responsibility of the
2900 : user to ensure that the second argument is non-zero. */
2901 :
2902 : static void
2903 3827 : gfc_conv_intrinsic_mod (gfc_se * se, gfc_expr * expr, int modulo)
2904 : {
2905 3827 : tree type;
2906 3827 : tree tmp;
2907 3827 : tree test;
2908 3827 : tree test2;
2909 3827 : tree fmod;
2910 3827 : tree zero;
2911 3827 : tree args[2];
2912 :
2913 3827 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
2914 :
2915 3827 : switch (expr->ts.type)
2916 : {
2917 3674 : case BT_INTEGER:
2918 : /* Integer case is easy, we've got a builtin op. */
2919 3674 : type = TREE_TYPE (args[0]);
2920 :
2921 3674 : if (modulo)
2922 411 : se->expr = fold_build2_loc (input_location, FLOOR_MOD_EXPR, type,
2923 : args[0], args[1]);
2924 : else
2925 3263 : se->expr = fold_build2_loc (input_location, TRUNC_MOD_EXPR, type,
2926 : args[0], args[1]);
2927 : break;
2928 :
2929 30 : case BT_UNSIGNED:
2930 : /* Even easier, we only need one. */
2931 30 : type = TREE_TYPE (args[0]);
2932 30 : se->expr = fold_build2_loc (input_location, TRUNC_MOD_EXPR, type,
2933 : args[0], args[1]);
2934 30 : break;
2935 :
2936 123 : case BT_REAL:
2937 123 : fmod = NULL_TREE;
2938 : /* Check if we have a builtin fmod. */
2939 123 : fmod = gfc_builtin_decl_for_float_kind (BUILT_IN_FMOD, expr->ts.kind);
2940 :
2941 : /* The builtin should always be available. */
2942 123 : gcc_assert (fmod != NULL_TREE);
2943 :
2944 123 : tmp = build_addr (fmod);
2945 123 : se->expr = build_call_array_loc (input_location,
2946 123 : TREE_TYPE (TREE_TYPE (fmod)),
2947 : tmp, 2, args);
2948 123 : if (modulo == 0)
2949 123 : return;
2950 :
2951 25 : type = TREE_TYPE (args[0]);
2952 :
2953 25 : args[0] = gfc_evaluate_now (args[0], &se->pre);
2954 25 : args[1] = gfc_evaluate_now (args[1], &se->pre);
2955 :
2956 : /* Definition:
2957 : modulo = arg - floor (arg/arg2) * arg2
2958 :
2959 : In order to calculate the result accurately, we use the fmod
2960 : function as follows.
2961 :
2962 : res = fmod (arg, arg2);
2963 : if (res)
2964 : {
2965 : if ((arg < 0) xor (arg2 < 0))
2966 : res += arg2;
2967 : }
2968 : else
2969 : res = copysign (0., arg2);
2970 :
2971 : => As two nested ternary exprs:
2972 :
2973 : res = res ? (((arg < 0) xor (arg2 < 0)) ? res + arg2 : res)
2974 : : copysign (0., arg2);
2975 :
2976 : */
2977 :
2978 25 : zero = gfc_build_const (type, integer_zero_node);
2979 25 : tmp = gfc_evaluate_now (se->expr, &se->pre);
2980 25 : if (!flag_signed_zeros)
2981 : {
2982 1 : test = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2983 : args[0], zero);
2984 1 : test2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2985 : args[1], zero);
2986 1 : test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
2987 : logical_type_node, test, test2);
2988 1 : test = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
2989 : tmp, zero);
2990 1 : test = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2991 : logical_type_node, test, test2);
2992 1 : test = gfc_evaluate_now (test, &se->pre);
2993 1 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
2994 : fold_build2_loc (input_location,
2995 : PLUS_EXPR,
2996 : type, tmp, args[1]),
2997 : tmp);
2998 : }
2999 : else
3000 : {
3001 24 : tree expr1, copysign, cscall;
3002 24 : copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN,
3003 : expr->ts.kind);
3004 24 : test = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
3005 : args[0], zero);
3006 24 : test2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
3007 : args[1], zero);
3008 24 : test2 = fold_build2_loc (input_location, TRUTH_XOR_EXPR,
3009 : logical_type_node, test, test2);
3010 24 : expr1 = fold_build3_loc (input_location, COND_EXPR, type, test2,
3011 : fold_build2_loc (input_location,
3012 : PLUS_EXPR,
3013 : type, tmp, args[1]),
3014 : tmp);
3015 24 : test = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
3016 : tmp, zero);
3017 24 : cscall = build_call_expr_loc (input_location, copysign, 2, zero,
3018 : args[1]);
3019 24 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, test,
3020 : expr1, cscall);
3021 : }
3022 : return;
3023 :
3024 0 : default:
3025 0 : gcc_unreachable ();
3026 : }
3027 : }
3028 :
3029 : /* DSHIFTL(I,J,S) = (I << S) | (J >> (BITSIZE(J) - S))
3030 : DSHIFTR(I,J,S) = (I << (BITSIZE(I) - S)) | (J >> S)
3031 : where the right shifts are logical (i.e. 0's are shifted in).
3032 : Because SHIFT_EXPR's want shifts strictly smaller than the integral
3033 : type width, we have to special-case both S == 0 and S == BITSIZE(J):
3034 : DSHIFTL(I,J,0) = I
3035 : DSHIFTL(I,J,BITSIZE) = J
3036 : DSHIFTR(I,J,0) = J
3037 : DSHIFTR(I,J,BITSIZE) = I. */
3038 :
3039 : static void
3040 132 : gfc_conv_intrinsic_dshift (gfc_se * se, gfc_expr * expr, bool dshiftl)
3041 : {
3042 132 : tree type, utype, stype, arg1, arg2, shift, res, left, right;
3043 132 : tree args[3], cond, tmp;
3044 132 : int bitsize;
3045 :
3046 132 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
3047 :
3048 132 : gcc_assert (TREE_TYPE (args[0]) == TREE_TYPE (args[1]));
3049 132 : type = TREE_TYPE (args[0]);
3050 132 : bitsize = TYPE_PRECISION (type);
3051 132 : utype = unsigned_type_for (type);
3052 132 : stype = TREE_TYPE (args[2]);
3053 :
3054 132 : arg1 = gfc_evaluate_now (args[0], &se->pre);
3055 132 : arg2 = gfc_evaluate_now (args[1], &se->pre);
3056 132 : shift = gfc_evaluate_now (args[2], &se->pre);
3057 :
3058 : /* The generic case. */
3059 132 : tmp = fold_build2_loc (input_location, MINUS_EXPR, stype,
3060 132 : build_int_cst (stype, bitsize), shift);
3061 198 : left = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3062 : arg1, dshiftl ? shift : tmp);
3063 :
3064 198 : right = fold_build2_loc (input_location, RSHIFT_EXPR, utype,
3065 : fold_convert (utype, arg2), dshiftl ? tmp : shift);
3066 132 : right = fold_convert (type, right);
3067 :
3068 132 : res = fold_build2_loc (input_location, BIT_IOR_EXPR, type, left, right);
3069 :
3070 : /* Special cases. */
3071 132 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, shift,
3072 : build_int_cst (stype, 0));
3073 198 : res = fold_build3_loc (input_location, COND_EXPR, type, cond,
3074 : dshiftl ? arg1 : arg2, res);
3075 :
3076 132 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, shift,
3077 132 : build_int_cst (stype, bitsize));
3078 198 : res = fold_build3_loc (input_location, COND_EXPR, type, cond,
3079 : dshiftl ? arg2 : arg1, res);
3080 :
3081 132 : se->expr = res;
3082 132 : }
3083 :
3084 :
3085 : /* Positive difference DIM (x, y) = ((x - y) < 0) ? 0 : x - y. */
3086 :
3087 : static void
3088 96 : gfc_conv_intrinsic_dim (gfc_se * se, gfc_expr * expr)
3089 : {
3090 96 : tree val;
3091 96 : tree tmp;
3092 96 : tree type;
3093 96 : tree zero;
3094 96 : tree args[2];
3095 :
3096 96 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
3097 96 : type = TREE_TYPE (args[0]);
3098 :
3099 96 : val = fold_build2_loc (input_location, MINUS_EXPR, type, args[0], args[1]);
3100 96 : val = gfc_evaluate_now (val, &se->pre);
3101 :
3102 96 : zero = gfc_build_const (type, integer_zero_node);
3103 96 : tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node, val, zero);
3104 96 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, zero, val);
3105 96 : }
3106 :
3107 :
3108 : /* SIGN(A, B) is absolute value of A times sign of B.
3109 : The real value versions use library functions to ensure the correct
3110 : handling of negative zero. Integer case implemented as:
3111 : SIGN(A, B) = { tmp = (A ^ B) >> C; (A + tmp) ^ tmp }
3112 : */
3113 :
3114 : static void
3115 423 : gfc_conv_intrinsic_sign (gfc_se * se, gfc_expr * expr)
3116 : {
3117 423 : tree tmp;
3118 423 : tree type;
3119 423 : tree args[2];
3120 :
3121 423 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
3122 423 : if (expr->ts.type == BT_REAL)
3123 : {
3124 161 : tree abs;
3125 :
3126 161 : tmp = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
3127 161 : abs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
3128 :
3129 : /* We explicitly have to ignore the minus sign. We do so by using
3130 : result = (arg1 == 0) ? abs(arg0) : copysign(arg0, arg1). */
3131 161 : if (!flag_sign_zero
3132 197 : && MODE_HAS_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (args[1]))))
3133 : {
3134 12 : tree cond, zero;
3135 12 : zero = build_real_from_int_cst (TREE_TYPE (args[1]), integer_zero_node);
3136 12 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3137 : args[1], zero);
3138 24 : se->expr = fold_build3_loc (input_location, COND_EXPR,
3139 12 : TREE_TYPE (args[0]), cond,
3140 : build_call_expr_loc (input_location, abs, 1,
3141 : args[0]),
3142 : build_call_expr_loc (input_location, tmp, 2,
3143 : args[0], args[1]));
3144 : }
3145 : else
3146 149 : se->expr = build_call_expr_loc (input_location, tmp, 2,
3147 : args[0], args[1]);
3148 161 : return;
3149 : }
3150 :
3151 : /* Having excluded floating point types, we know we are now dealing
3152 : with signed integer types. */
3153 262 : type = TREE_TYPE (args[0]);
3154 :
3155 : /* Args[0] is used multiple times below. */
3156 262 : args[0] = gfc_evaluate_now (args[0], &se->pre);
3157 :
3158 : /* Construct (A ^ B) >> 31, which generates a bit mask of all zeros if
3159 : the signs of A and B are the same, and of all ones if they differ. */
3160 262 : tmp = fold_build2_loc (input_location, BIT_XOR_EXPR, type, args[0], args[1]);
3161 262 : tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, tmp,
3162 262 : build_int_cst (type, TYPE_PRECISION (type) - 1));
3163 262 : tmp = gfc_evaluate_now (tmp, &se->pre);
3164 :
3165 : /* Construct (A + tmp) ^ tmp, which is A if tmp is zero, and -A if tmp]
3166 : is all ones (i.e. -1). */
3167 262 : se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR, type,
3168 : fold_build2_loc (input_location, PLUS_EXPR,
3169 : type, args[0], tmp), tmp);
3170 : }
3171 :
3172 :
3173 : /* Test for the presence of an optional argument. */
3174 :
3175 : static void
3176 5196 : gfc_conv_intrinsic_present (gfc_se * se, gfc_expr * expr)
3177 : {
3178 5196 : gfc_expr *arg;
3179 :
3180 5196 : arg = expr->value.function.actual->expr;
3181 5196 : gcc_assert (arg->expr_type == EXPR_VARIABLE);
3182 5196 : se->expr = gfc_conv_expr_present (arg->symtree->n.sym);
3183 5196 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
3184 5196 : }
3185 :
3186 :
3187 : /* Calculate the double precision product of two single precision values. */
3188 :
3189 : static void
3190 13 : gfc_conv_intrinsic_dprod (gfc_se * se, gfc_expr * expr)
3191 : {
3192 13 : tree type;
3193 13 : tree args[2];
3194 :
3195 13 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
3196 :
3197 : /* Convert the args to double precision before multiplying. */
3198 13 : type = gfc_typenode_for_spec (&expr->ts);
3199 13 : args[0] = convert (type, args[0]);
3200 13 : args[1] = convert (type, args[1]);
3201 13 : se->expr = fold_build2_loc (input_location, MULT_EXPR, type, args[0],
3202 : args[1]);
3203 13 : }
3204 :
3205 :
3206 : /* Return a length one character string containing an ascii character. */
3207 :
3208 : static void
3209 2020 : gfc_conv_intrinsic_char (gfc_se * se, gfc_expr * expr)
3210 : {
3211 2020 : tree arg[2];
3212 2020 : tree var;
3213 2020 : tree type;
3214 2020 : unsigned int num_args;
3215 :
3216 2020 : num_args = gfc_intrinsic_argument_list_length (expr);
3217 2020 : gfc_conv_intrinsic_function_args (se, expr, arg, num_args);
3218 :
3219 2020 : type = gfc_get_char_type (expr->ts.kind);
3220 2020 : var = gfc_create_var (type, "char");
3221 :
3222 2020 : arg[0] = fold_build1_loc (input_location, NOP_EXPR, type, arg[0]);
3223 2020 : gfc_add_modify (&se->pre, var, arg[0]);
3224 2020 : se->expr = gfc_build_addr_expr (build_pointer_type (type), var);
3225 2020 : se->string_length = build_int_cst (gfc_charlen_type_node, 1);
3226 2020 : }
3227 :
3228 :
3229 : static void
3230 0 : gfc_conv_intrinsic_ctime (gfc_se * se, gfc_expr * expr)
3231 : {
3232 0 : tree var;
3233 0 : tree len;
3234 0 : tree tmp;
3235 0 : tree cond;
3236 0 : tree fndecl;
3237 0 : tree *args;
3238 0 : unsigned int num_args;
3239 :
3240 0 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
3241 0 : args = XALLOCAVEC (tree, num_args);
3242 :
3243 0 : var = gfc_create_var (pchar_type_node, "pstr");
3244 0 : len = gfc_create_var (gfc_charlen_type_node, "len");
3245 :
3246 0 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
3247 0 : args[0] = gfc_build_addr_expr (NULL_TREE, var);
3248 0 : args[1] = gfc_build_addr_expr (NULL_TREE, len);
3249 :
3250 0 : fndecl = build_addr (gfor_fndecl_ctime);
3251 0 : tmp = build_call_array_loc (input_location,
3252 0 : TREE_TYPE (TREE_TYPE (gfor_fndecl_ctime)),
3253 : fndecl, num_args, args);
3254 0 : gfc_add_expr_to_block (&se->pre, tmp);
3255 :
3256 : /* Free the temporary afterwards, if necessary. */
3257 0 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3258 0 : len, build_int_cst (TREE_TYPE (len), 0));
3259 0 : tmp = gfc_call_free (var);
3260 0 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3261 0 : gfc_add_expr_to_block (&se->post, tmp);
3262 :
3263 0 : se->expr = var;
3264 0 : se->string_length = len;
3265 0 : }
3266 :
3267 :
3268 : static void
3269 0 : gfc_conv_intrinsic_fdate (gfc_se * se, gfc_expr * expr)
3270 : {
3271 0 : tree var;
3272 0 : tree len;
3273 0 : tree tmp;
3274 0 : tree cond;
3275 0 : tree fndecl;
3276 0 : tree *args;
3277 0 : unsigned int num_args;
3278 :
3279 0 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
3280 0 : args = XALLOCAVEC (tree, num_args);
3281 :
3282 0 : var = gfc_create_var (pchar_type_node, "pstr");
3283 0 : len = gfc_create_var (gfc_charlen_type_node, "len");
3284 :
3285 0 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
3286 0 : args[0] = gfc_build_addr_expr (NULL_TREE, var);
3287 0 : args[1] = gfc_build_addr_expr (NULL_TREE, len);
3288 :
3289 0 : fndecl = build_addr (gfor_fndecl_fdate);
3290 0 : tmp = build_call_array_loc (input_location,
3291 0 : TREE_TYPE (TREE_TYPE (gfor_fndecl_fdate)),
3292 : fndecl, num_args, args);
3293 0 : gfc_add_expr_to_block (&se->pre, tmp);
3294 :
3295 : /* Free the temporary afterwards, if necessary. */
3296 0 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3297 0 : len, build_int_cst (TREE_TYPE (len), 0));
3298 0 : tmp = gfc_call_free (var);
3299 0 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3300 0 : gfc_add_expr_to_block (&se->post, tmp);
3301 :
3302 0 : se->expr = var;
3303 0 : se->string_length = len;
3304 0 : }
3305 :
3306 :
3307 : /* Generate a direct call to free() for the FREE subroutine. */
3308 :
3309 : static tree
3310 10 : conv_intrinsic_free (gfc_code *code)
3311 : {
3312 10 : stmtblock_t block;
3313 10 : gfc_se argse;
3314 10 : tree arg, call;
3315 :
3316 10 : gfc_init_se (&argse, NULL);
3317 10 : gfc_conv_expr (&argse, code->ext.actual->expr);
3318 10 : arg = fold_convert (ptr_type_node, argse.expr);
3319 :
3320 10 : gfc_init_block (&block);
3321 10 : call = build_call_expr_loc (input_location,
3322 : builtin_decl_explicit (BUILT_IN_FREE), 1, arg);
3323 10 : gfc_add_expr_to_block (&block, call);
3324 10 : return gfc_finish_block (&block);
3325 : }
3326 :
3327 :
3328 : /* Call the RANDOM_INIT library subroutine with a hidden argument for
3329 : handling seeding on coarray images. */
3330 :
3331 : static tree
3332 90 : conv_intrinsic_random_init (gfc_code *code)
3333 : {
3334 90 : stmtblock_t block;
3335 90 : gfc_se se;
3336 90 : tree arg1, arg2, tmp;
3337 : /* On none coarray == lib compiles use LOGICAL(4) else regular LOGICAL. */
3338 90 : tree used_bool_type_node = flag_coarray == GFC_FCOARRAY_LIB
3339 90 : ? logical_type_node
3340 90 : : gfc_get_logical_type (4);
3341 :
3342 : /* Make the function call. */
3343 90 : gfc_init_block (&block);
3344 90 : gfc_init_se (&se, NULL);
3345 :
3346 : /* Convert REPEATABLE to the desired LOGICAL entity. */
3347 90 : gfc_conv_expr (&se, code->ext.actual->expr);
3348 90 : gfc_add_block_to_block (&block, &se.pre);
3349 90 : arg1 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
3350 90 : gfc_add_block_to_block (&block, &se.post);
3351 :
3352 : /* Convert IMAGE_DISTINCT to the desired LOGICAL entity. */
3353 90 : gfc_conv_expr (&se, code->ext.actual->next->expr);
3354 90 : gfc_add_block_to_block (&block, &se.pre);
3355 90 : arg2 = fold_convert (used_bool_type_node, gfc_evaluate_now (se.expr, &block));
3356 90 : gfc_add_block_to_block (&block, &se.post);
3357 :
3358 90 : if (flag_coarray == GFC_FCOARRAY_LIB)
3359 : {
3360 0 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_random_init,
3361 : 2, arg1, arg2);
3362 : }
3363 : else
3364 : {
3365 : /* The ABI for libgfortran needs to be maintained, so a hidden
3366 : argument must be include if code is compiled with -fcoarray=single
3367 : or without the option. Set to 0. */
3368 90 : tree arg3 = build_int_cst (gfc_get_int_type (4), 0);
3369 90 : tmp = build_call_expr_loc (input_location, gfor_fndecl_random_init,
3370 : 3, arg1, arg2, arg3);
3371 : }
3372 :
3373 90 : gfc_add_expr_to_block (&block, tmp);
3374 :
3375 90 : return gfc_finish_block (&block);
3376 : }
3377 :
3378 :
3379 : /* Call the SYSTEM_CLOCK library functions, handling the type and kind
3380 : conversions. */
3381 :
3382 : static tree
3383 196 : conv_intrinsic_system_clock (gfc_code *code)
3384 : {
3385 196 : stmtblock_t block;
3386 196 : gfc_se count_se, count_rate_se, count_max_se;
3387 196 : tree arg1 = NULL_TREE, arg2 = NULL_TREE, arg3 = NULL_TREE;
3388 196 : tree tmp;
3389 196 : int least;
3390 :
3391 196 : gfc_expr *count = code->ext.actual->expr;
3392 196 : gfc_expr *count_rate = code->ext.actual->next->expr;
3393 196 : gfc_expr *count_max = code->ext.actual->next->next->expr;
3394 :
3395 : /* Evaluate our arguments. */
3396 196 : if (count)
3397 : {
3398 196 : gfc_init_se (&count_se, NULL);
3399 196 : gfc_conv_expr (&count_se, count);
3400 : }
3401 :
3402 196 : if (count_rate)
3403 : {
3404 181 : gfc_init_se (&count_rate_se, NULL);
3405 181 : gfc_conv_expr (&count_rate_se, count_rate);
3406 : }
3407 :
3408 196 : if (count_max)
3409 : {
3410 180 : gfc_init_se (&count_max_se, NULL);
3411 180 : gfc_conv_expr (&count_max_se, count_max);
3412 : }
3413 :
3414 : /* Find the smallest kind found of the arguments. */
3415 196 : least = 16;
3416 196 : least = (count && count->ts.kind < least) ? count->ts.kind : least;
3417 196 : least = (count_rate && count_rate->ts.kind < least) ? count_rate->ts.kind
3418 : : least;
3419 196 : least = (count_max && count_max->ts.kind < least) ? count_max->ts.kind
3420 : : least;
3421 :
3422 : /* Prepare temporary variables. */
3423 :
3424 196 : if (count)
3425 : {
3426 196 : if (least >= 8)
3427 18 : arg1 = gfc_create_var (gfc_get_int_type (8), "count");
3428 178 : else if (least == 4)
3429 154 : arg1 = gfc_create_var (gfc_get_int_type (4), "count");
3430 24 : else if (count->ts.kind == 1)
3431 12 : arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[0].pedantic_min_int,
3432 : count->ts.kind);
3433 : else
3434 12 : arg1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[1].pedantic_min_int,
3435 : count->ts.kind);
3436 : }
3437 :
3438 196 : if (count_rate)
3439 : {
3440 181 : if (least >= 8)
3441 18 : arg2 = gfc_create_var (gfc_get_int_type (8), "count_rate");
3442 163 : else if (least == 4)
3443 139 : arg2 = gfc_create_var (gfc_get_int_type (4), "count_rate");
3444 : else
3445 24 : arg2 = integer_zero_node;
3446 : }
3447 :
3448 196 : if (count_max)
3449 : {
3450 180 : if (least >= 8)
3451 18 : arg3 = gfc_create_var (gfc_get_int_type (8), "count_max");
3452 162 : else if (least == 4)
3453 138 : arg3 = gfc_create_var (gfc_get_int_type (4), "count_max");
3454 : else
3455 24 : arg3 = integer_zero_node;
3456 : }
3457 :
3458 : /* Make the function call. */
3459 196 : gfc_init_block (&block);
3460 :
3461 196 : if (least <= 2)
3462 : {
3463 24 : if (least == 1)
3464 : {
3465 12 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3466 : : null_pointer_node;
3467 12 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3468 : : null_pointer_node;
3469 12 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3470 : : null_pointer_node;
3471 : }
3472 :
3473 24 : if (least == 2)
3474 : {
3475 12 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3476 : : null_pointer_node;
3477 12 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3478 : : null_pointer_node;
3479 12 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3480 : : null_pointer_node;
3481 : }
3482 : }
3483 : else
3484 : {
3485 172 : if (least == 4)
3486 : {
3487 585 : tmp = build_call_expr_loc (input_location,
3488 : gfor_fndecl_system_clock4, 3,
3489 154 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3490 : : null_pointer_node,
3491 139 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3492 : : null_pointer_node,
3493 138 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3494 : : null_pointer_node);
3495 154 : gfc_add_expr_to_block (&block, tmp);
3496 : }
3497 : /* Handle kind>=8, 10, or 16 arguments */
3498 172 : if (least >= 8)
3499 : {
3500 72 : tmp = build_call_expr_loc (input_location,
3501 : gfor_fndecl_system_clock8, 3,
3502 18 : arg1 ? gfc_build_addr_expr (NULL_TREE, arg1)
3503 : : null_pointer_node,
3504 18 : arg2 ? gfc_build_addr_expr (NULL_TREE, arg2)
3505 : : null_pointer_node,
3506 18 : arg3 ? gfc_build_addr_expr (NULL_TREE, arg3)
3507 : : null_pointer_node);
3508 18 : gfc_add_expr_to_block (&block, tmp);
3509 : }
3510 : }
3511 :
3512 : /* And store values back if needed. */
3513 196 : if (arg1 && arg1 != count_se.expr)
3514 196 : gfc_add_modify (&block, count_se.expr,
3515 196 : fold_convert (TREE_TYPE (count_se.expr), arg1));
3516 196 : if (arg2 && arg2 != count_rate_se.expr)
3517 181 : gfc_add_modify (&block, count_rate_se.expr,
3518 181 : fold_convert (TREE_TYPE (count_rate_se.expr), arg2));
3519 196 : if (arg3 && arg3 != count_max_se.expr)
3520 180 : gfc_add_modify (&block, count_max_se.expr,
3521 180 : fold_convert (TREE_TYPE (count_max_se.expr), arg3));
3522 :
3523 196 : return gfc_finish_block (&block);
3524 : }
3525 :
3526 : static tree
3527 102 : conv_intrinsic_split (gfc_code *code)
3528 : {
3529 102 : stmtblock_t block, post_block;
3530 102 : gfc_se se;
3531 102 : gfc_expr *string_expr, *set_expr, *pos_expr, *back_expr;
3532 102 : tree string, string_len;
3533 102 : tree set, set_len;
3534 102 : tree pos, pos_for_call;
3535 102 : tree back;
3536 102 : tree fndecl, call;
3537 :
3538 102 : string_expr = code->ext.actual->expr;
3539 102 : set_expr = code->ext.actual->next->expr;
3540 102 : pos_expr = code->ext.actual->next->next->expr;
3541 102 : back_expr = code->ext.actual->next->next->next->expr;
3542 :
3543 102 : gfc_start_block (&block);
3544 102 : gfc_init_block (&post_block);
3545 :
3546 102 : gfc_init_se (&se, NULL);
3547 102 : gfc_conv_expr (&se, string_expr);
3548 102 : gfc_conv_string_parameter (&se);
3549 102 : gfc_add_block_to_block (&block, &se.pre);
3550 102 : gfc_add_block_to_block (&post_block, &se.post);
3551 102 : string = se.expr;
3552 102 : string_len = se.string_length;
3553 :
3554 102 : gfc_init_se (&se, NULL);
3555 102 : gfc_conv_expr (&se, set_expr);
3556 102 : gfc_conv_string_parameter (&se);
3557 102 : gfc_add_block_to_block (&block, &se.pre);
3558 102 : gfc_add_block_to_block (&post_block, &se.post);
3559 102 : set = se.expr;
3560 102 : set_len = se.string_length;
3561 :
3562 102 : gfc_init_se (&se, NULL);
3563 102 : gfc_conv_expr (&se, pos_expr);
3564 102 : gfc_add_block_to_block (&block, &se.pre);
3565 102 : gfc_add_block_to_block (&post_block, &se.post);
3566 102 : pos = se.expr;
3567 102 : pos_for_call = fold_convert (gfc_charlen_type_node, pos);
3568 :
3569 102 : if (back_expr)
3570 : {
3571 48 : gfc_init_se (&se, NULL);
3572 48 : gfc_conv_expr (&se, back_expr);
3573 48 : gfc_add_block_to_block (&block, &se.pre);
3574 48 : gfc_add_block_to_block (&post_block, &se.post);
3575 48 : back = se.expr;
3576 : }
3577 : else
3578 54 : back = logical_false_node;
3579 :
3580 102 : if (string_expr->ts.kind == 1)
3581 66 : fndecl = gfor_fndecl_string_split;
3582 36 : else if (string_expr->ts.kind == 4)
3583 36 : fndecl = gfor_fndecl_string_split_char4;
3584 : else
3585 0 : gcc_unreachable ();
3586 :
3587 102 : call = build_call_expr_loc (input_location, fndecl, 6, string_len, string,
3588 : set_len, set, pos_for_call, back);
3589 102 : gfc_add_modify (&block, pos, fold_convert (TREE_TYPE (pos), call));
3590 :
3591 102 : gfc_add_block_to_block (&block, &post_block);
3592 102 : return gfc_finish_block (&block);
3593 : }
3594 :
3595 : /* Return a character string containing the tty name. */
3596 :
3597 : static void
3598 0 : gfc_conv_intrinsic_ttynam (gfc_se * se, gfc_expr * expr)
3599 : {
3600 0 : tree var;
3601 0 : tree len;
3602 0 : tree tmp;
3603 0 : tree cond;
3604 0 : tree fndecl;
3605 0 : tree *args;
3606 0 : unsigned int num_args;
3607 :
3608 0 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
3609 0 : args = XALLOCAVEC (tree, num_args);
3610 :
3611 0 : var = gfc_create_var (pchar_type_node, "pstr");
3612 0 : len = gfc_create_var (gfc_charlen_type_node, "len");
3613 :
3614 0 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
3615 0 : args[0] = gfc_build_addr_expr (NULL_TREE, var);
3616 0 : args[1] = gfc_build_addr_expr (NULL_TREE, len);
3617 :
3618 0 : fndecl = build_addr (gfor_fndecl_ttynam);
3619 0 : tmp = build_call_array_loc (input_location,
3620 0 : TREE_TYPE (TREE_TYPE (gfor_fndecl_ttynam)),
3621 : fndecl, num_args, args);
3622 0 : gfc_add_expr_to_block (&se->pre, tmp);
3623 :
3624 : /* Free the temporary afterwards, if necessary. */
3625 0 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3626 0 : len, build_int_cst (TREE_TYPE (len), 0));
3627 0 : tmp = gfc_call_free (var);
3628 0 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3629 0 : gfc_add_expr_to_block (&se->post, tmp);
3630 :
3631 0 : se->expr = var;
3632 0 : se->string_length = len;
3633 0 : }
3634 :
3635 :
3636 : /* Get the minimum/maximum value of all the parameters.
3637 : minmax (a1, a2, a3, ...)
3638 : {
3639 : mvar = a1;
3640 : mvar = COMP (mvar, a2)
3641 : mvar = COMP (mvar, a3)
3642 : ...
3643 : return mvar;
3644 : }
3645 : Where COMP is MIN/MAX_EXPR for integral types or when we don't
3646 : care about NaNs, or IFN_FMIN/MAX when the target has support for
3647 : fast NaN-honouring min/max. When neither holds expand a sequence
3648 : of explicit comparisons. */
3649 :
3650 : /* TODO: Mismatching types can occur when specific names are used.
3651 : These should be handled during resolution. */
3652 : static void
3653 1365 : gfc_conv_intrinsic_minmax (gfc_se * se, gfc_expr * expr, enum tree_code op)
3654 : {
3655 1365 : tree tmp;
3656 1365 : tree mvar;
3657 1365 : tree val;
3658 1365 : tree *args;
3659 1365 : tree type;
3660 1365 : tree argtype;
3661 1365 : gfc_actual_arglist *argexpr;
3662 1365 : unsigned int i, nargs;
3663 :
3664 1365 : nargs = gfc_intrinsic_argument_list_length (expr);
3665 1365 : args = XALLOCAVEC (tree, nargs);
3666 :
3667 1365 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
3668 1365 : type = gfc_typenode_for_spec (&expr->ts);
3669 :
3670 : /* Only evaluate the argument once. */
3671 1365 : if (!VAR_P (args[0]) && !TREE_CONSTANT (args[0]))
3672 368 : args[0] = gfc_evaluate_now (args[0], &se->pre);
3673 :
3674 : /* Determine suitable type of temporary, as a GNU extension allows
3675 : different argument kinds. */
3676 1365 : argtype = TREE_TYPE (args[0]);
3677 1365 : argexpr = expr->value.function.actual;
3678 2949 : for (i = 1, argexpr = argexpr->next; i < nargs; i++, argexpr = argexpr->next)
3679 : {
3680 1584 : tree tmptype = TREE_TYPE (args[i]);
3681 1584 : if (TYPE_PRECISION (tmptype) > TYPE_PRECISION (argtype))
3682 1 : argtype = tmptype;
3683 : }
3684 1365 : mvar = gfc_create_var (argtype, "M");
3685 1365 : gfc_add_modify (&se->pre, mvar, convert (argtype, args[0]));
3686 :
3687 1365 : argexpr = expr->value.function.actual;
3688 2949 : for (i = 1, argexpr = argexpr->next; i < nargs; i++, argexpr = argexpr->next)
3689 : {
3690 1584 : tree cond = NULL_TREE;
3691 1584 : val = args[i];
3692 :
3693 : /* Handle absent optional arguments by ignoring the comparison. */
3694 1584 : if (argexpr->expr->expr_type == EXPR_VARIABLE
3695 920 : && argexpr->expr->symtree->n.sym->attr.optional
3696 45 : && INDIRECT_REF_P (val))
3697 : {
3698 84 : cond = fold_build2_loc (input_location,
3699 : NE_EXPR, logical_type_node,
3700 42 : TREE_OPERAND (val, 0),
3701 42 : build_int_cst (TREE_TYPE (TREE_OPERAND (val, 0)), 0));
3702 : }
3703 1542 : else if (!VAR_P (val) && !TREE_CONSTANT (val))
3704 : /* Only evaluate the argument once. */
3705 599 : val = gfc_evaluate_now (val, &se->pre);
3706 :
3707 1584 : tree calc;
3708 : /* For floating point types, the question is what MAX(a, NaN) or
3709 : MIN(a, NaN) should return (where "a" is a normal number).
3710 : There are valid use case for returning either one, but the
3711 : Fortran standard doesn't specify which one should be chosen.
3712 : Also, there is no consensus among other tested compilers. In
3713 : short, it's a mess. So lets just do whatever is fastest. */
3714 1584 : tree_code code = op == GT_EXPR ? MAX_EXPR : MIN_EXPR;
3715 1584 : calc = fold_build2_loc (input_location, code, argtype,
3716 : convert (argtype, val), mvar);
3717 1584 : tmp = build2_v (MODIFY_EXPR, mvar, calc);
3718 :
3719 1584 : if (cond != NULL_TREE)
3720 42 : tmp = build3_v (COND_EXPR, cond, tmp,
3721 : build_empty_stmt (input_location));
3722 1584 : gfc_add_expr_to_block (&se->pre, tmp);
3723 : }
3724 1365 : se->expr = convert (type, mvar);
3725 1365 : }
3726 :
3727 :
3728 : /* Generate library calls for MIN and MAX intrinsics for character
3729 : variables. */
3730 : static void
3731 282 : gfc_conv_intrinsic_minmax_char (gfc_se * se, gfc_expr * expr, int op)
3732 : {
3733 282 : tree *args;
3734 282 : tree var, len, fndecl, tmp, cond, function;
3735 282 : unsigned int nargs;
3736 :
3737 282 : nargs = gfc_intrinsic_argument_list_length (expr);
3738 282 : args = XALLOCAVEC (tree, nargs + 4);
3739 282 : gfc_conv_intrinsic_function_args (se, expr, &args[4], nargs);
3740 :
3741 : /* Create the result variables. */
3742 282 : len = gfc_create_var (gfc_charlen_type_node, "len");
3743 282 : args[0] = gfc_build_addr_expr (NULL_TREE, len);
3744 282 : var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
3745 282 : args[1] = gfc_build_addr_expr (ppvoid_type_node, var);
3746 282 : args[2] = build_int_cst (integer_type_node, op);
3747 282 : args[3] = build_int_cst (integer_type_node, nargs / 2);
3748 :
3749 282 : if (expr->ts.kind == 1)
3750 210 : function = gfor_fndecl_string_minmax;
3751 72 : else if (expr->ts.kind == 4)
3752 72 : function = gfor_fndecl_string_minmax_char4;
3753 : else
3754 0 : gcc_unreachable ();
3755 :
3756 : /* Make the function call. */
3757 282 : fndecl = build_addr (function);
3758 282 : tmp = build_call_array_loc (input_location,
3759 282 : TREE_TYPE (TREE_TYPE (function)), fndecl,
3760 : nargs + 4, args);
3761 282 : gfc_add_expr_to_block (&se->pre, tmp);
3762 :
3763 : /* Free the temporary afterwards, if necessary. */
3764 282 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3765 282 : len, build_int_cst (TREE_TYPE (len), 0));
3766 282 : tmp = gfc_call_free (var);
3767 282 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
3768 282 : gfc_add_expr_to_block (&se->post, tmp);
3769 :
3770 282 : se->expr = var;
3771 282 : se->string_length = len;
3772 282 : }
3773 :
3774 :
3775 : /* Create a symbol node for this intrinsic. The symbol from the frontend
3776 : has the generic name. */
3777 :
3778 : static gfc_symbol *
3779 11343 : gfc_get_symbol_for_expr (gfc_expr * expr, bool ignore_optional)
3780 : {
3781 11343 : gfc_symbol *sym;
3782 :
3783 : /* TODO: Add symbols for intrinsic function to the global namespace. */
3784 11343 : gcc_assert (strlen (expr->value.function.name) <= GFC_MAX_SYMBOL_LEN - 5);
3785 11343 : sym = gfc_new_symbol (expr->value.function.name, NULL);
3786 :
3787 11343 : sym->ts = expr->ts;
3788 11343 : if (sym->ts.type == BT_CHARACTER)
3789 1784 : sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3790 11343 : sym->attr.external = 1;
3791 11343 : sym->attr.function = 1;
3792 11343 : sym->attr.always_explicit = 1;
3793 11343 : sym->attr.proc = PROC_INTRINSIC;
3794 11343 : sym->attr.flavor = FL_PROCEDURE;
3795 11343 : sym->result = sym;
3796 11343 : if (expr->rank > 0)
3797 : {
3798 9933 : sym->attr.dimension = 1;
3799 9933 : sym->as = gfc_get_array_spec ();
3800 9933 : sym->as->type = AS_ASSUMED_SHAPE;
3801 9933 : sym->as->rank = expr->rank;
3802 : }
3803 :
3804 11343 : gfc_copy_formal_args_intr (sym, expr->value.function.isym,
3805 : ignore_optional ? expr->value.function.actual
3806 : : NULL);
3807 :
3808 11343 : return sym;
3809 : }
3810 :
3811 : /* Remove empty actual arguments. */
3812 :
3813 : static void
3814 8277 : remove_empty_actual_arguments (gfc_actual_arglist **ap)
3815 : {
3816 44456 : while (*ap)
3817 : {
3818 36179 : if ((*ap)->expr == NULL)
3819 : {
3820 11076 : gfc_actual_arglist *r = *ap;
3821 11076 : *ap = r->next;
3822 11076 : r->next = NULL;
3823 11076 : gfc_free_actual_arglist (r);
3824 : }
3825 : else
3826 25103 : ap = &((*ap)->next);
3827 : }
3828 8277 : }
3829 :
3830 : #define MAX_SPEC_ARG 12
3831 :
3832 : /* Make up an fn spec that's right for intrinsic functions that we
3833 : want to call. */
3834 :
3835 : static char *
3836 1939 : intrinsic_fnspec (gfc_expr *expr)
3837 : {
3838 1939 : static char fnspec_buf[MAX_SPEC_ARG*2+1];
3839 1939 : char *fp;
3840 1939 : int i;
3841 1939 : int num_char_args;
3842 :
3843 : #define ADD_CHAR(c) do { *fp++ = c; *fp++ = ' '; } while(0)
3844 :
3845 : /* Set the fndecl. */
3846 1939 : fp = fnspec_buf;
3847 : /* Function return value. FIXME: Check if the second letter could
3848 : be something other than a space, for further optimization. */
3849 1939 : ADD_CHAR ('.');
3850 1939 : if (expr->rank == 0)
3851 : {
3852 238 : if (expr->ts.type == BT_CHARACTER)
3853 : {
3854 84 : ADD_CHAR ('w'); /* Address of character. */
3855 84 : ADD_CHAR ('.'); /* Length of character. */
3856 : }
3857 : }
3858 : else
3859 1701 : ADD_CHAR ('w'); /* Return value is a descriptor. */
3860 :
3861 1939 : num_char_args = 0;
3862 10224 : for (gfc_actual_arglist *a = expr->value.function.actual; a; a = a->next)
3863 : {
3864 8285 : if (a->expr == NULL)
3865 2565 : continue;
3866 :
3867 5720 : if (a->name && strcmp (a->name,"%VAL") == 0)
3868 1300 : ADD_CHAR ('.');
3869 : else
3870 : {
3871 4420 : if (a->expr->rank > 0)
3872 2575 : ADD_CHAR ('r');
3873 : else
3874 1845 : ADD_CHAR ('R');
3875 : }
3876 5720 : num_char_args += a->expr->ts.type == BT_CHARACTER;
3877 5720 : gcc_assert (fp - fnspec_buf + num_char_args <= MAX_SPEC_ARG*2);
3878 : }
3879 :
3880 2743 : for (i = 0; i < num_char_args; i++)
3881 804 : ADD_CHAR ('.');
3882 :
3883 1939 : *fp = '\0';
3884 1939 : return fnspec_buf;
3885 : }
3886 :
3887 : #undef MAX_SPEC_ARG
3888 : #undef ADD_CHAR
3889 :
3890 : /* Generate the right symbol for the specific intrinsic function and
3891 : modify the expr accordingly. This assumes that absent optional
3892 : arguments should be removed. */
3893 :
3894 : gfc_symbol *
3895 8277 : specific_intrinsic_symbol (gfc_expr *expr)
3896 : {
3897 8277 : gfc_symbol *sym;
3898 :
3899 8277 : sym = gfc_find_intrinsic_symbol (expr);
3900 8277 : if (sym == NULL)
3901 : {
3902 1939 : sym = gfc_get_intrinsic_function_symbol (expr);
3903 1939 : sym->ts = expr->ts;
3904 1939 : if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl)
3905 240 : sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
3906 :
3907 1939 : gfc_copy_formal_args_intr (sym, expr->value.function.isym,
3908 : expr->value.function.actual, true);
3909 1939 : sym->backend_decl
3910 1939 : = gfc_get_extern_function_decl (sym, expr->value.function.actual,
3911 1939 : intrinsic_fnspec (expr));
3912 : }
3913 :
3914 8277 : remove_empty_actual_arguments (&(expr->value.function.actual));
3915 :
3916 8277 : return sym;
3917 : }
3918 :
3919 : /* Generate a call to an external intrinsic function. FIXME: So far,
3920 : this only works for functions which are called with well-defined
3921 : types; CSHIFT and friends will come later. */
3922 :
3923 : static void
3924 13741 : gfc_conv_intrinsic_funcall (gfc_se * se, gfc_expr * expr)
3925 : {
3926 13741 : gfc_symbol *sym;
3927 13741 : vec<tree, va_gc> *append_args;
3928 13741 : bool specific_symbol;
3929 :
3930 13741 : gcc_assert (!se->ss || se->ss->info->expr == expr);
3931 :
3932 13741 : if (se->ss)
3933 11769 : gcc_assert (expr->rank > 0);
3934 : else
3935 1972 : gcc_assert (expr->rank == 0);
3936 :
3937 13741 : switch (expr->value.function.isym->id)
3938 : {
3939 : case GFC_ISYM_ANY:
3940 : case GFC_ISYM_ALL:
3941 : case GFC_ISYM_FINDLOC:
3942 : case GFC_ISYM_MAXLOC:
3943 : case GFC_ISYM_MINLOC:
3944 : case GFC_ISYM_MAXVAL:
3945 : case GFC_ISYM_MINVAL:
3946 : case GFC_ISYM_NORM2:
3947 : case GFC_ISYM_PRODUCT:
3948 : case GFC_ISYM_SUM:
3949 : specific_symbol = true;
3950 : break;
3951 5464 : default:
3952 5464 : specific_symbol = false;
3953 : }
3954 :
3955 13741 : if (specific_symbol)
3956 : {
3957 : /* Need to copy here because specific_intrinsic_symbol modifies
3958 : expr to omit the absent optional arguments. */
3959 8277 : expr = gfc_copy_expr (expr);
3960 8277 : sym = specific_intrinsic_symbol (expr);
3961 : }
3962 : else
3963 5464 : sym = gfc_get_symbol_for_expr (expr, se->ignore_optional);
3964 :
3965 : /* Calls to libgfortran_matmul need to be appended special arguments,
3966 : to be able to call the BLAS ?gemm functions if required and possible. */
3967 13741 : append_args = NULL;
3968 13741 : if (expr->value.function.isym->id == GFC_ISYM_MATMUL
3969 860 : && !expr->external_blas
3970 822 : && sym->ts.type != BT_LOGICAL)
3971 : {
3972 806 : tree cint = gfc_get_int_type (gfc_c_int_kind);
3973 :
3974 806 : if (flag_external_blas
3975 0 : && (sym->ts.type == BT_REAL || sym->ts.type == BT_COMPLEX)
3976 0 : && (sym->ts.kind == 4 || sym->ts.kind == 8))
3977 : {
3978 0 : tree gemm_fndecl;
3979 :
3980 0 : if (sym->ts.type == BT_REAL)
3981 : {
3982 0 : if (sym->ts.kind == 4)
3983 0 : gemm_fndecl = gfor_fndecl_sgemm;
3984 : else
3985 0 : gemm_fndecl = gfor_fndecl_dgemm;
3986 : }
3987 : else
3988 : {
3989 0 : if (sym->ts.kind == 4)
3990 0 : gemm_fndecl = gfor_fndecl_cgemm;
3991 : else
3992 0 : gemm_fndecl = gfor_fndecl_zgemm;
3993 : }
3994 :
3995 0 : vec_alloc (append_args, 3);
3996 0 : append_args->quick_push (build_int_cst (cint, 1));
3997 0 : append_args->quick_push (build_int_cst (cint,
3998 0 : flag_blas_matmul_limit));
3999 0 : append_args->quick_push (gfc_build_addr_expr (NULL_TREE,
4000 : gemm_fndecl));
4001 0 : }
4002 : else
4003 : {
4004 806 : vec_alloc (append_args, 3);
4005 806 : append_args->quick_push (build_int_cst (cint, 0));
4006 806 : append_args->quick_push (build_int_cst (cint, 0));
4007 806 : append_args->quick_push (null_pointer_node);
4008 : }
4009 : }
4010 : /* Non-character scalar reduce returns a pointer to a result of size set by
4011 : the element size of 'array'. Setting 'sym' allocatable ensures that the
4012 : result is deallocated at the appropriate time. */
4013 12935 : else if (expr->value.function.isym->id == GFC_ISYM_REDUCE
4014 108 : && expr->rank == 0 && expr->ts.type != BT_CHARACTER)
4015 102 : sym->attr.allocatable = 1;
4016 :
4017 :
4018 13741 : gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
4019 : append_args);
4020 :
4021 13741 : if (specific_symbol)
4022 8277 : gfc_free_expr (expr);
4023 : else
4024 5464 : gfc_free_symbol (sym);
4025 13741 : }
4026 :
4027 : /* ANY and ALL intrinsics. ANY->op == NE_EXPR, ALL->op == EQ_EXPR.
4028 : Implemented as
4029 : any(a)
4030 : {
4031 : forall (i=...)
4032 : if (a[i] != 0)
4033 : return 1
4034 : end forall
4035 : return 0
4036 : }
4037 : all(a)
4038 : {
4039 : forall (i=...)
4040 : if (a[i] == 0)
4041 : return 0
4042 : end forall
4043 : return 1
4044 : }
4045 : */
4046 : static void
4047 39094 : gfc_conv_intrinsic_anyall (gfc_se * se, gfc_expr * expr, enum tree_code op)
4048 : {
4049 39094 : tree resvar;
4050 39094 : stmtblock_t block;
4051 39094 : stmtblock_t body;
4052 39094 : tree type;
4053 39094 : tree tmp;
4054 39094 : tree found;
4055 39094 : gfc_loopinfo loop;
4056 39094 : gfc_actual_arglist *actual;
4057 39094 : gfc_ss *arrayss;
4058 39094 : gfc_se arrayse;
4059 39094 : tree exit_label;
4060 :
4061 39094 : if (se->ss)
4062 : {
4063 0 : gfc_conv_intrinsic_funcall (se, expr);
4064 0 : return;
4065 : }
4066 :
4067 39094 : actual = expr->value.function.actual;
4068 39094 : type = gfc_typenode_for_spec (&expr->ts);
4069 : /* Initialize the result. */
4070 39094 : resvar = gfc_create_var (type, "test");
4071 39094 : if (op == EQ_EXPR)
4072 432 : tmp = convert (type, boolean_true_node);
4073 : else
4074 38662 : tmp = convert (type, boolean_false_node);
4075 39094 : gfc_add_modify (&se->pre, resvar, tmp);
4076 :
4077 : /* Walk the arguments. */
4078 39094 : arrayss = gfc_walk_expr (actual->expr);
4079 39094 : gcc_assert (arrayss != gfc_ss_terminator);
4080 :
4081 : /* Initialize the scalarizer. */
4082 39094 : gfc_init_loopinfo (&loop);
4083 39094 : exit_label = gfc_build_label_decl (NULL_TREE);
4084 39094 : TREE_USED (exit_label) = 1;
4085 39094 : gfc_add_ss_to_loop (&loop, arrayss);
4086 :
4087 : /* Initialize the loop. */
4088 39094 : gfc_conv_ss_startstride (&loop);
4089 39094 : gfc_conv_loop_setup (&loop, &expr->where);
4090 :
4091 39094 : gfc_mark_ss_chain_used (arrayss, 1);
4092 : /* Generate the loop body. */
4093 39094 : gfc_start_scalarized_body (&loop, &body);
4094 :
4095 : /* If the condition matches then set the return value. */
4096 39094 : gfc_start_block (&block);
4097 39094 : if (op == EQ_EXPR)
4098 432 : tmp = convert (type, boolean_false_node);
4099 : else
4100 38662 : tmp = convert (type, boolean_true_node);
4101 39094 : gfc_add_modify (&block, resvar, tmp);
4102 :
4103 : /* And break out of the loop. */
4104 39094 : tmp = build1_v (GOTO_EXPR, exit_label);
4105 39094 : gfc_add_expr_to_block (&block, tmp);
4106 :
4107 39094 : found = gfc_finish_block (&block);
4108 :
4109 : /* Check this element. */
4110 39094 : gfc_init_se (&arrayse, NULL);
4111 39094 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
4112 39094 : arrayse.ss = arrayss;
4113 39094 : gfc_conv_expr_val (&arrayse, actual->expr);
4114 :
4115 39094 : gfc_add_block_to_block (&body, &arrayse.pre);
4116 39094 : tmp = fold_build2_loc (input_location, op, logical_type_node, arrayse.expr,
4117 39094 : build_int_cst (TREE_TYPE (arrayse.expr), 0));
4118 39094 : tmp = build3_v (COND_EXPR, tmp, found, build_empty_stmt (input_location));
4119 39094 : gfc_add_expr_to_block (&body, tmp);
4120 39094 : gfc_add_block_to_block (&body, &arrayse.post);
4121 :
4122 39094 : gfc_trans_scalarizing_loops (&loop, &body);
4123 :
4124 : /* Add the exit label. */
4125 39094 : tmp = build1_v (LABEL_EXPR, exit_label);
4126 39094 : gfc_add_expr_to_block (&loop.pre, tmp);
4127 :
4128 39094 : gfc_add_block_to_block (&se->pre, &loop.pre);
4129 39094 : gfc_add_block_to_block (&se->pre, &loop.post);
4130 39094 : gfc_cleanup_loop (&loop);
4131 :
4132 39094 : se->expr = resvar;
4133 : }
4134 :
4135 :
4136 : /* Generate the constant 180 / pi, which is used in the conversion
4137 : of acosd(), asind(), atand(), atan2d(). */
4138 :
4139 : static tree
4140 408 : rad2deg (int kind)
4141 : {
4142 408 : tree retval;
4143 408 : mpfr_t pi, t0;
4144 :
4145 408 : gfc_set_model_kind (kind);
4146 408 : mpfr_init (pi);
4147 408 : mpfr_init (t0);
4148 408 : mpfr_set_si (t0, 180, GFC_RND_MODE);
4149 408 : mpfr_const_pi (pi, GFC_RND_MODE);
4150 408 : mpfr_div (t0, t0, pi, GFC_RND_MODE);
4151 408 : retval = gfc_conv_mpfr_to_tree (t0, kind, 0);
4152 408 : mpfr_clear (t0);
4153 408 : mpfr_clear (pi);
4154 408 : return retval;
4155 : }
4156 :
4157 :
4158 : static gfc_intrinsic_map_t *
4159 618 : gfc_lookup_intrinsic (gfc_isym_id id)
4160 : {
4161 618 : gfc_intrinsic_map_t *m = gfc_intrinsic_map;
4162 11514 : for (; m->id != GFC_ISYM_NONE || m->double_built_in != END_BUILTINS; m++)
4163 11514 : if (id == m->id)
4164 : break;
4165 618 : gcc_assert (id == m->id);
4166 618 : return m;
4167 : }
4168 :
4169 :
4170 : /* ACOSD(x) is translated into ACOS(x) * 180 / pi.
4171 : ASIND(x) is translated into ASIN(x) * 180 / pi.
4172 : ATAND(x) is translated into ATAN(x) * 180 / pi. */
4173 :
4174 : static void
4175 270 : gfc_conv_intrinsic_atrigd (gfc_se * se, gfc_expr * expr, gfc_isym_id id)
4176 : {
4177 270 : tree arg;
4178 270 : tree atrigd;
4179 270 : tree type;
4180 270 : gfc_intrinsic_map_t *m;
4181 :
4182 270 : type = gfc_typenode_for_spec (&expr->ts);
4183 :
4184 270 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
4185 :
4186 270 : switch (id)
4187 : {
4188 90 : case GFC_ISYM_ACOSD:
4189 90 : m = gfc_lookup_intrinsic (GFC_ISYM_ACOS);
4190 90 : break;
4191 90 : case GFC_ISYM_ASIND:
4192 90 : m = gfc_lookup_intrinsic (GFC_ISYM_ASIN);
4193 90 : break;
4194 90 : case GFC_ISYM_ATAND:
4195 90 : m = gfc_lookup_intrinsic (GFC_ISYM_ATAN);
4196 90 : break;
4197 0 : default:
4198 0 : gcc_unreachable ();
4199 : }
4200 270 : atrigd = gfc_get_intrinsic_lib_fndecl (m, expr);
4201 270 : atrigd = build_call_expr_loc (input_location, atrigd, 1, arg);
4202 :
4203 270 : se->expr = fold_build2_loc (input_location, MULT_EXPR, type, atrigd,
4204 : fold_convert (type, rad2deg (expr->ts.kind)));
4205 270 : }
4206 :
4207 :
4208 : /* COTAN(X) is translated into -TAN(X+PI/2) for REAL argument and
4209 : COS(X) / SIN(X) for COMPLEX argument. */
4210 :
4211 : static void
4212 102 : gfc_conv_intrinsic_cotan (gfc_se *se, gfc_expr *expr)
4213 : {
4214 102 : gfc_intrinsic_map_t *m;
4215 102 : tree arg;
4216 102 : tree type;
4217 :
4218 102 : type = gfc_typenode_for_spec (&expr->ts);
4219 102 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
4220 :
4221 102 : if (expr->ts.type == BT_REAL)
4222 : {
4223 102 : tree tan;
4224 102 : tree tmp;
4225 102 : mpfr_t pio2;
4226 :
4227 : /* Create pi/2. */
4228 102 : gfc_set_model_kind (expr->ts.kind);
4229 102 : mpfr_init (pio2);
4230 102 : mpfr_const_pi (pio2, GFC_RND_MODE);
4231 102 : mpfr_div_ui (pio2, pio2, 2, GFC_RND_MODE);
4232 102 : tmp = gfc_conv_mpfr_to_tree (pio2, expr->ts.kind, 0);
4233 102 : mpfr_clear (pio2);
4234 :
4235 : /* Find tan builtin function. */
4236 102 : m = gfc_lookup_intrinsic (GFC_ISYM_TAN);
4237 102 : tan = gfc_get_intrinsic_lib_fndecl (m, expr);
4238 102 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, arg, tmp);
4239 102 : tan = build_call_expr_loc (input_location, tan, 1, tmp);
4240 102 : se->expr = fold_build1_loc (input_location, NEGATE_EXPR, type, tan);
4241 : }
4242 : else
4243 : {
4244 0 : tree sin;
4245 0 : tree cos;
4246 :
4247 : /* Find cos builtin function. */
4248 0 : m = gfc_lookup_intrinsic (GFC_ISYM_COS);
4249 0 : cos = gfc_get_intrinsic_lib_fndecl (m, expr);
4250 0 : cos = build_call_expr_loc (input_location, cos, 1, arg);
4251 :
4252 : /* Find sin builtin function. */
4253 0 : m = gfc_lookup_intrinsic (GFC_ISYM_SIN);
4254 0 : sin = gfc_get_intrinsic_lib_fndecl (m, expr);
4255 0 : sin = build_call_expr_loc (input_location, sin, 1, arg);
4256 :
4257 : /* Divide cos by sin. */
4258 0 : se->expr = fold_build2_loc (input_location, RDIV_EXPR, type, cos, sin);
4259 : }
4260 102 : }
4261 :
4262 :
4263 : /* COTAND(X) is translated into -TAND(X+90) for REAL argument. */
4264 :
4265 : static void
4266 108 : gfc_conv_intrinsic_cotand (gfc_se *se, gfc_expr *expr)
4267 : {
4268 108 : tree arg;
4269 108 : tree type;
4270 108 : tree ninety_tree;
4271 108 : mpfr_t ninety;
4272 :
4273 108 : type = gfc_typenode_for_spec (&expr->ts);
4274 108 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
4275 :
4276 108 : gfc_set_model_kind (expr->ts.kind);
4277 :
4278 : /* Build the tree for x + 90. */
4279 108 : mpfr_init_set_ui (ninety, 90, GFC_RND_MODE);
4280 108 : ninety_tree = gfc_conv_mpfr_to_tree (ninety, expr->ts.kind, 0);
4281 108 : arg = fold_build2_loc (input_location, PLUS_EXPR, type, arg, ninety_tree);
4282 108 : mpfr_clear (ninety);
4283 :
4284 : /* Find tand. */
4285 108 : gfc_intrinsic_map_t *m = gfc_lookup_intrinsic (GFC_ISYM_TAND);
4286 108 : tree tand = gfc_get_intrinsic_lib_fndecl (m, expr);
4287 108 : tand = build_call_expr_loc (input_location, tand, 1, arg);
4288 :
4289 108 : se->expr = fold_build1_loc (input_location, NEGATE_EXPR, type, tand);
4290 108 : }
4291 :
4292 :
4293 : /* ATAN2D(Y,X) is translated into ATAN2(Y,X) * 180 / PI. */
4294 :
4295 : static void
4296 138 : gfc_conv_intrinsic_atan2d (gfc_se *se, gfc_expr *expr)
4297 : {
4298 138 : tree args[2];
4299 138 : tree atan2d;
4300 138 : tree type;
4301 :
4302 138 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
4303 138 : type = TREE_TYPE (args[0]);
4304 :
4305 138 : gfc_intrinsic_map_t *m = gfc_lookup_intrinsic (GFC_ISYM_ATAN2);
4306 138 : atan2d = gfc_get_intrinsic_lib_fndecl (m, expr);
4307 138 : atan2d = build_call_expr_loc (input_location, atan2d, 2, args[0], args[1]);
4308 :
4309 138 : se->expr = fold_build2_loc (input_location, MULT_EXPR, type, atan2d,
4310 : rad2deg (expr->ts.kind));
4311 138 : }
4312 :
4313 :
4314 : /* COUNT(A) = Number of true elements in A. */
4315 : static void
4316 143 : gfc_conv_intrinsic_count (gfc_se * se, gfc_expr * expr)
4317 : {
4318 143 : tree resvar;
4319 143 : tree type;
4320 143 : stmtblock_t body;
4321 143 : tree tmp;
4322 143 : gfc_loopinfo loop;
4323 143 : gfc_actual_arglist *actual;
4324 143 : gfc_ss *arrayss;
4325 143 : gfc_se arrayse;
4326 :
4327 143 : if (se->ss)
4328 : {
4329 0 : gfc_conv_intrinsic_funcall (se, expr);
4330 0 : return;
4331 : }
4332 :
4333 143 : actual = expr->value.function.actual;
4334 :
4335 143 : type = gfc_typenode_for_spec (&expr->ts);
4336 : /* Initialize the result. */
4337 143 : resvar = gfc_create_var (type, "count");
4338 143 : gfc_add_modify (&se->pre, resvar, build_int_cst (type, 0));
4339 :
4340 : /* Walk the arguments. */
4341 143 : arrayss = gfc_walk_expr (actual->expr);
4342 143 : gcc_assert (arrayss != gfc_ss_terminator);
4343 :
4344 : /* Initialize the scalarizer. */
4345 143 : gfc_init_loopinfo (&loop);
4346 143 : gfc_add_ss_to_loop (&loop, arrayss);
4347 :
4348 : /* Initialize the loop. */
4349 143 : gfc_conv_ss_startstride (&loop);
4350 143 : gfc_conv_loop_setup (&loop, &expr->where);
4351 :
4352 143 : gfc_mark_ss_chain_used (arrayss, 1);
4353 : /* Generate the loop body. */
4354 143 : gfc_start_scalarized_body (&loop, &body);
4355 :
4356 143 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (resvar),
4357 143 : resvar, build_int_cst (TREE_TYPE (resvar), 1));
4358 143 : tmp = build2_v (MODIFY_EXPR, resvar, tmp);
4359 :
4360 143 : gfc_init_se (&arrayse, NULL);
4361 143 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
4362 143 : arrayse.ss = arrayss;
4363 143 : gfc_conv_expr_val (&arrayse, actual->expr);
4364 143 : tmp = build3_v (COND_EXPR, arrayse.expr, tmp,
4365 : build_empty_stmt (input_location));
4366 :
4367 143 : gfc_add_block_to_block (&body, &arrayse.pre);
4368 143 : gfc_add_expr_to_block (&body, tmp);
4369 143 : gfc_add_block_to_block (&body, &arrayse.post);
4370 :
4371 143 : gfc_trans_scalarizing_loops (&loop, &body);
4372 :
4373 143 : gfc_add_block_to_block (&se->pre, &loop.pre);
4374 143 : gfc_add_block_to_block (&se->pre, &loop.post);
4375 143 : gfc_cleanup_loop (&loop);
4376 :
4377 143 : se->expr = resvar;
4378 : }
4379 :
4380 :
4381 : /* Update given gfc_se to have ss component pointing to the nested gfc_ss
4382 : struct and return the corresponding loopinfo. */
4383 :
4384 : static gfc_loopinfo *
4385 3374 : enter_nested_loop (gfc_se *se)
4386 : {
4387 3374 : se->ss = se->ss->nested_ss;
4388 3374 : gcc_assert (se->ss == se->ss->loop->ss);
4389 :
4390 3374 : return se->ss->loop;
4391 : }
4392 :
4393 : /* Build the condition for a mask, which may be optional. */
4394 :
4395 : static tree
4396 12763 : conv_mask_condition (gfc_se *maskse, gfc_expr *maskexpr,
4397 : bool optional_mask)
4398 : {
4399 12763 : tree present;
4400 12763 : tree type;
4401 :
4402 12763 : if (optional_mask)
4403 : {
4404 206 : type = TREE_TYPE (maskse->expr);
4405 206 : present = gfc_conv_expr_present (maskexpr->symtree->n.sym);
4406 206 : present = convert (type, present);
4407 206 : present = fold_build1_loc (input_location, TRUTH_NOT_EXPR, type,
4408 : present);
4409 206 : return fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
4410 206 : type, present, maskse->expr);
4411 : }
4412 : else
4413 12557 : return maskse->expr;
4414 : }
4415 :
4416 : /* Inline implementation of the sum and product intrinsics. */
4417 : static void
4418 2521 : gfc_conv_intrinsic_arith (gfc_se * se, gfc_expr * expr, enum tree_code op,
4419 : bool norm2)
4420 : {
4421 2521 : tree resvar;
4422 2521 : tree scale = NULL_TREE;
4423 2521 : tree type;
4424 2521 : stmtblock_t body;
4425 2521 : stmtblock_t block;
4426 2521 : tree tmp;
4427 2521 : gfc_loopinfo loop, *ploop;
4428 2521 : gfc_actual_arglist *arg_array, *arg_mask;
4429 2521 : gfc_ss *arrayss = NULL;
4430 2521 : gfc_ss *maskss = NULL;
4431 2521 : gfc_se arrayse;
4432 2521 : gfc_se maskse;
4433 2521 : gfc_se *parent_se;
4434 2521 : gfc_expr *arrayexpr;
4435 2521 : gfc_expr *maskexpr;
4436 2521 : bool optional_mask;
4437 :
4438 2521 : if (expr->rank > 0)
4439 : {
4440 578 : gcc_assert (gfc_inline_intrinsic_function_p (expr));
4441 : parent_se = se;
4442 : }
4443 : else
4444 : parent_se = NULL;
4445 :
4446 2521 : type = gfc_typenode_for_spec (&expr->ts);
4447 : /* Initialize the result. */
4448 2521 : resvar = gfc_create_var (type, "val");
4449 2521 : if (norm2)
4450 : {
4451 : /* result = 0.0;
4452 : scale = 1.0. */
4453 68 : scale = gfc_create_var (type, "scale");
4454 68 : gfc_add_modify (&se->pre, scale,
4455 : gfc_build_const (type, integer_one_node));
4456 68 : tmp = gfc_build_const (type, integer_zero_node);
4457 : }
4458 2453 : else if (op == PLUS_EXPR || op == BIT_IOR_EXPR || op == BIT_XOR_EXPR)
4459 2035 : tmp = gfc_build_const (type, integer_zero_node);
4460 418 : else if (op == NE_EXPR)
4461 : /* PARITY. */
4462 36 : tmp = convert (type, boolean_false_node);
4463 382 : else if (op == BIT_AND_EXPR)
4464 24 : tmp = gfc_build_const (type, fold_build1_loc (input_location, NEGATE_EXPR,
4465 : type, integer_one_node));
4466 : else
4467 358 : tmp = gfc_build_const (type, integer_one_node);
4468 :
4469 2521 : gfc_add_modify (&se->pre, resvar, tmp);
4470 :
4471 2521 : arg_array = expr->value.function.actual;
4472 :
4473 2521 : arrayexpr = arg_array->expr;
4474 :
4475 2521 : if (op == NE_EXPR || norm2)
4476 : {
4477 : /* PARITY and NORM2. */
4478 : maskexpr = NULL;
4479 : optional_mask = false;
4480 : }
4481 : else
4482 : {
4483 2417 : arg_mask = arg_array->next->next;
4484 2417 : gcc_assert (arg_mask != NULL);
4485 2417 : maskexpr = arg_mask->expr;
4486 371 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
4487 266 : && maskexpr->symtree->n.sym->attr.dummy
4488 2435 : && maskexpr->symtree->n.sym->attr.optional;
4489 : }
4490 :
4491 2521 : if (expr->rank == 0)
4492 : {
4493 : /* Walk the arguments. */
4494 1943 : arrayss = gfc_walk_expr (arrayexpr);
4495 1943 : gcc_assert (arrayss != gfc_ss_terminator);
4496 :
4497 1943 : if (maskexpr && maskexpr->rank > 0)
4498 : {
4499 223 : maskss = gfc_walk_expr (maskexpr);
4500 223 : gcc_assert (maskss != gfc_ss_terminator);
4501 : }
4502 : else
4503 : maskss = NULL;
4504 :
4505 : /* Initialize the scalarizer. */
4506 1943 : gfc_init_loopinfo (&loop);
4507 :
4508 : /* We add the mask first because the number of iterations is
4509 : taken from the last ss, and this breaks if an absent
4510 : optional argument is used for mask. */
4511 :
4512 1943 : if (maskexpr && maskexpr->rank > 0)
4513 223 : gfc_add_ss_to_loop (&loop, maskss);
4514 1943 : gfc_add_ss_to_loop (&loop, arrayss);
4515 :
4516 : /* Initialize the loop. */
4517 1943 : gfc_conv_ss_startstride (&loop);
4518 1943 : gfc_conv_loop_setup (&loop, &expr->where);
4519 :
4520 1943 : if (maskexpr && maskexpr->rank > 0)
4521 223 : gfc_mark_ss_chain_used (maskss, 1);
4522 1943 : gfc_mark_ss_chain_used (arrayss, 1);
4523 :
4524 1943 : ploop = &loop;
4525 : }
4526 : else
4527 : /* All the work has been done in the parent loops. */
4528 578 : ploop = enter_nested_loop (se);
4529 :
4530 2521 : gcc_assert (ploop);
4531 :
4532 : /* Generate the loop body. */
4533 2521 : gfc_start_scalarized_body (ploop, &body);
4534 :
4535 : /* If we have a mask, only add this element if the mask is set. */
4536 2521 : if (maskexpr && maskexpr->rank > 0)
4537 : {
4538 307 : gfc_init_se (&maskse, parent_se);
4539 307 : gfc_copy_loopinfo_to_se (&maskse, ploop);
4540 307 : if (expr->rank == 0)
4541 223 : maskse.ss = maskss;
4542 307 : gfc_conv_expr_val (&maskse, maskexpr);
4543 307 : gfc_add_block_to_block (&body, &maskse.pre);
4544 :
4545 307 : gfc_start_block (&block);
4546 : }
4547 : else
4548 2214 : gfc_init_block (&block);
4549 :
4550 : /* Do the actual summation/product. */
4551 2521 : gfc_init_se (&arrayse, parent_se);
4552 2521 : gfc_copy_loopinfo_to_se (&arrayse, ploop);
4553 2521 : if (expr->rank == 0)
4554 1943 : arrayse.ss = arrayss;
4555 2521 : gfc_conv_expr_val (&arrayse, arrayexpr);
4556 2521 : gfc_add_block_to_block (&block, &arrayse.pre);
4557 :
4558 2521 : if (norm2)
4559 : {
4560 : /* if (x (i) != 0.0)
4561 : {
4562 : absX = abs(x(i))
4563 : if (absX > scale)
4564 : {
4565 : val = scale/absX;
4566 : result = 1.0 + result * val * val;
4567 : scale = absX;
4568 : }
4569 : else
4570 : {
4571 : val = absX/scale;
4572 : result += val * val;
4573 : }
4574 : } */
4575 68 : tree res1, res2, cond, absX, val;
4576 68 : stmtblock_t ifblock1, ifblock2, ifblock3;
4577 :
4578 68 : gfc_init_block (&ifblock1);
4579 :
4580 68 : absX = gfc_create_var (type, "absX");
4581 68 : gfc_add_modify (&ifblock1, absX,
4582 : fold_build1_loc (input_location, ABS_EXPR, type,
4583 : arrayse.expr));
4584 68 : val = gfc_create_var (type, "val");
4585 68 : gfc_add_expr_to_block (&ifblock1, val);
4586 :
4587 68 : gfc_init_block (&ifblock2);
4588 68 : gfc_add_modify (&ifblock2, val,
4589 : fold_build2_loc (input_location, RDIV_EXPR, type, scale,
4590 : absX));
4591 68 : res1 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
4592 68 : res1 = fold_build2_loc (input_location, MULT_EXPR, type, resvar, res1);
4593 68 : res1 = fold_build2_loc (input_location, PLUS_EXPR, type, res1,
4594 : gfc_build_const (type, integer_one_node));
4595 68 : gfc_add_modify (&ifblock2, resvar, res1);
4596 68 : gfc_add_modify (&ifblock2, scale, absX);
4597 68 : res1 = gfc_finish_block (&ifblock2);
4598 :
4599 68 : gfc_init_block (&ifblock3);
4600 68 : gfc_add_modify (&ifblock3, val,
4601 : fold_build2_loc (input_location, RDIV_EXPR, type, absX,
4602 : scale));
4603 68 : res2 = fold_build2_loc (input_location, MULT_EXPR, type, val, val);
4604 68 : res2 = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, res2);
4605 68 : gfc_add_modify (&ifblock3, resvar, res2);
4606 68 : res2 = gfc_finish_block (&ifblock3);
4607 :
4608 68 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
4609 : absX, scale);
4610 68 : tmp = build3_v (COND_EXPR, cond, res1, res2);
4611 68 : gfc_add_expr_to_block (&ifblock1, tmp);
4612 68 : tmp = gfc_finish_block (&ifblock1);
4613 :
4614 68 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
4615 : arrayse.expr,
4616 : gfc_build_const (type, integer_zero_node));
4617 :
4618 68 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
4619 68 : gfc_add_expr_to_block (&block, tmp);
4620 : }
4621 : else
4622 : {
4623 2453 : tmp = fold_build2_loc (input_location, op, type, resvar, arrayse.expr);
4624 2453 : gfc_add_modify (&block, resvar, tmp);
4625 : }
4626 :
4627 2521 : gfc_add_block_to_block (&block, &arrayse.post);
4628 :
4629 2521 : if (maskexpr && maskexpr->rank > 0)
4630 : {
4631 : /* We enclose the above in if (mask) {...} . If the mask is an
4632 : optional argument, generate
4633 : IF (.NOT. PRESENT(MASK) .OR. MASK(I)). */
4634 307 : tree ifmask;
4635 307 : tmp = gfc_finish_block (&block);
4636 307 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
4637 307 : tmp = build3_v (COND_EXPR, ifmask, tmp,
4638 : build_empty_stmt (input_location));
4639 307 : }
4640 : else
4641 2214 : tmp = gfc_finish_block (&block);
4642 2521 : gfc_add_expr_to_block (&body, tmp);
4643 :
4644 2521 : gfc_trans_scalarizing_loops (ploop, &body);
4645 :
4646 : /* For a scalar mask, enclose the loop in an if statement. */
4647 2521 : if (maskexpr && maskexpr->rank == 0)
4648 : {
4649 64 : gfc_init_block (&block);
4650 64 : gfc_add_block_to_block (&block, &ploop->pre);
4651 64 : gfc_add_block_to_block (&block, &ploop->post);
4652 64 : tmp = gfc_finish_block (&block);
4653 :
4654 64 : if (expr->rank > 0)
4655 : {
4656 34 : tmp = build3_v (COND_EXPR, se->ss->info->data.scalar.value, tmp,
4657 : build_empty_stmt (input_location));
4658 34 : gfc_advance_se_ss_chain (se);
4659 : }
4660 : else
4661 : {
4662 30 : tree ifmask;
4663 :
4664 30 : gcc_assert (expr->rank == 0);
4665 30 : gfc_init_se (&maskse, NULL);
4666 30 : gfc_conv_expr_val (&maskse, maskexpr);
4667 30 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
4668 30 : tmp = build3_v (COND_EXPR, ifmask, tmp,
4669 : build_empty_stmt (input_location));
4670 : }
4671 :
4672 64 : gfc_add_expr_to_block (&block, tmp);
4673 64 : gfc_add_block_to_block (&se->pre, &block);
4674 64 : gcc_assert (se->post.head == NULL);
4675 : }
4676 : else
4677 : {
4678 2457 : gfc_add_block_to_block (&se->pre, &ploop->pre);
4679 2457 : gfc_add_block_to_block (&se->pre, &ploop->post);
4680 : }
4681 :
4682 2521 : if (expr->rank == 0)
4683 1943 : gfc_cleanup_loop (ploop);
4684 :
4685 2521 : if (norm2)
4686 : {
4687 : /* result = scale * sqrt(result). */
4688 68 : tree sqrt;
4689 68 : sqrt = gfc_builtin_decl_for_float_kind (BUILT_IN_SQRT, expr->ts.kind);
4690 68 : resvar = build_call_expr_loc (input_location,
4691 : sqrt, 1, resvar);
4692 68 : resvar = fold_build2_loc (input_location, MULT_EXPR, type, scale, resvar);
4693 : }
4694 :
4695 2521 : se->expr = resvar;
4696 2521 : }
4697 :
4698 :
4699 : /* Inline implementation of the dot_product intrinsic. This function
4700 : is based on gfc_conv_intrinsic_arith (the previous function). */
4701 : static void
4702 113 : gfc_conv_intrinsic_dot_product (gfc_se * se, gfc_expr * expr)
4703 : {
4704 113 : tree resvar;
4705 113 : tree type;
4706 113 : stmtblock_t body;
4707 113 : stmtblock_t block;
4708 113 : tree tmp;
4709 113 : gfc_loopinfo loop;
4710 113 : gfc_actual_arglist *actual;
4711 113 : gfc_ss *arrayss1, *arrayss2;
4712 113 : gfc_se arrayse1, arrayse2;
4713 113 : gfc_expr *arrayexpr1, *arrayexpr2;
4714 :
4715 113 : type = gfc_typenode_for_spec (&expr->ts);
4716 :
4717 : /* Initialize the result. */
4718 113 : resvar = gfc_create_var (type, "val");
4719 113 : if (expr->ts.type == BT_LOGICAL)
4720 30 : tmp = build_int_cst (type, 0);
4721 : else
4722 83 : tmp = gfc_build_const (type, integer_zero_node);
4723 :
4724 113 : gfc_add_modify (&se->pre, resvar, tmp);
4725 :
4726 : /* Walk argument #1. */
4727 113 : actual = expr->value.function.actual;
4728 113 : arrayexpr1 = actual->expr;
4729 113 : arrayss1 = gfc_walk_expr (arrayexpr1);
4730 113 : gcc_assert (arrayss1 != gfc_ss_terminator);
4731 :
4732 : /* Walk argument #2. */
4733 113 : actual = actual->next;
4734 113 : arrayexpr2 = actual->expr;
4735 113 : arrayss2 = gfc_walk_expr (arrayexpr2);
4736 113 : gcc_assert (arrayss2 != gfc_ss_terminator);
4737 :
4738 : /* Initialize the scalarizer. */
4739 113 : gfc_init_loopinfo (&loop);
4740 113 : gfc_add_ss_to_loop (&loop, arrayss1);
4741 113 : gfc_add_ss_to_loop (&loop, arrayss2);
4742 :
4743 : /* Initialize the loop. */
4744 113 : gfc_conv_ss_startstride (&loop);
4745 113 : gfc_conv_loop_setup (&loop, &expr->where);
4746 :
4747 113 : gfc_mark_ss_chain_used (arrayss1, 1);
4748 113 : gfc_mark_ss_chain_used (arrayss2, 1);
4749 :
4750 : /* Generate the loop body. */
4751 113 : gfc_start_scalarized_body (&loop, &body);
4752 113 : gfc_init_block (&block);
4753 :
4754 : /* Make the tree expression for [conjg(]array1[)]. */
4755 113 : gfc_init_se (&arrayse1, NULL);
4756 113 : gfc_copy_loopinfo_to_se (&arrayse1, &loop);
4757 113 : arrayse1.ss = arrayss1;
4758 113 : gfc_conv_expr_val (&arrayse1, arrayexpr1);
4759 113 : if (expr->ts.type == BT_COMPLEX)
4760 9 : arrayse1.expr = fold_build1_loc (input_location, CONJ_EXPR, type,
4761 : arrayse1.expr);
4762 113 : gfc_add_block_to_block (&block, &arrayse1.pre);
4763 :
4764 : /* Make the tree expression for array2. */
4765 113 : gfc_init_se (&arrayse2, NULL);
4766 113 : gfc_copy_loopinfo_to_se (&arrayse2, &loop);
4767 113 : arrayse2.ss = arrayss2;
4768 113 : gfc_conv_expr_val (&arrayse2, arrayexpr2);
4769 113 : gfc_add_block_to_block (&block, &arrayse2.pre);
4770 :
4771 : /* Do the actual product and sum. */
4772 113 : if (expr->ts.type == BT_LOGICAL)
4773 : {
4774 30 : tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, type,
4775 : arrayse1.expr, arrayse2.expr);
4776 30 : tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, type, resvar, tmp);
4777 : }
4778 : else
4779 : {
4780 83 : tmp = fold_build2_loc (input_location, MULT_EXPR, type, arrayse1.expr,
4781 : arrayse2.expr);
4782 83 : tmp = fold_build2_loc (input_location, PLUS_EXPR, type, resvar, tmp);
4783 : }
4784 113 : gfc_add_modify (&block, resvar, tmp);
4785 :
4786 : /* Finish up the loop block and the loop. */
4787 113 : tmp = gfc_finish_block (&block);
4788 113 : gfc_add_expr_to_block (&body, tmp);
4789 :
4790 113 : gfc_trans_scalarizing_loops (&loop, &body);
4791 113 : gfc_add_block_to_block (&se->pre, &loop.pre);
4792 113 : gfc_add_block_to_block (&se->pre, &loop.post);
4793 113 : gfc_cleanup_loop (&loop);
4794 :
4795 113 : se->expr = resvar;
4796 113 : }
4797 :
4798 :
4799 : /* Tells whether the expression E is a reference to an optional variable whose
4800 : presence is not known at compile time. Those are variable references without
4801 : subreference; if there is a subreference, we can assume the variable is
4802 : present. We have to special case full arrays, which we represent with a fake
4803 : "full" reference, and class descriptors for which a reference to data is not
4804 : really a subreference. */
4805 :
4806 : bool
4807 14613 : maybe_absent_optional_variable (gfc_expr *e)
4808 : {
4809 14613 : if (!(e && e->expr_type == EXPR_VARIABLE))
4810 : return false;
4811 :
4812 1716 : gfc_symbol *sym = e->symtree->n.sym;
4813 1716 : if (!sym->attr.optional)
4814 : return false;
4815 :
4816 224 : gfc_ref *ref = e->ref;
4817 224 : if (ref == nullptr)
4818 : return true;
4819 :
4820 20 : if (ref->type == REF_ARRAY
4821 20 : && ref->u.ar.type == AR_FULL
4822 20 : && ref->next == nullptr)
4823 : return true;
4824 :
4825 0 : if (!(sym->ts.type == BT_CLASS
4826 0 : && ref->type == REF_COMPONENT
4827 0 : && ref->u.c.component == CLASS_DATA (sym)))
4828 : return false;
4829 :
4830 0 : gfc_ref *next_ref = ref->next;
4831 0 : if (next_ref == nullptr)
4832 : return true;
4833 :
4834 0 : if (next_ref->type == REF_ARRAY
4835 0 : && next_ref->u.ar.type == AR_FULL
4836 0 : && next_ref->next == nullptr)
4837 0 : return true;
4838 :
4839 : return false;
4840 : }
4841 :
4842 :
4843 : /* Emit code for minloc or maxloc intrinsic. There are many different cases
4844 : we need to handle. For performance reasons we sometimes create two
4845 : loops instead of one, where the second one is much simpler.
4846 : Examples for minloc intrinsic:
4847 : A: Result is scalar.
4848 : 1) Array mask is used and NaNs need to be supported:
4849 : limit = Infinity;
4850 : pos = 0;
4851 : S = from;
4852 : while (S <= to) {
4853 : if (mask[S]) {
4854 : if (pos == 0) pos = S + (1 - from);
4855 : if (a[S] <= limit) {
4856 : limit = a[S];
4857 : pos = S + (1 - from);
4858 : goto lab1;
4859 : }
4860 : }
4861 : S++;
4862 : }
4863 : goto lab2;
4864 : lab1:;
4865 : while (S <= to) {
4866 : if (mask[S])
4867 : if (a[S] < limit) {
4868 : limit = a[S];
4869 : pos = S + (1 - from);
4870 : }
4871 : S++;
4872 : }
4873 : lab2:;
4874 : 2) NaNs need to be supported, but it is known at compile time or cheaply
4875 : at runtime whether array is nonempty or not:
4876 : limit = Infinity;
4877 : pos = 0;
4878 : S = from;
4879 : while (S <= to) {
4880 : if (a[S] <= limit) {
4881 : limit = a[S];
4882 : pos = S + (1 - from);
4883 : goto lab1;
4884 : }
4885 : S++;
4886 : }
4887 : if (from <= to) pos = 1;
4888 : goto lab2;
4889 : lab1:;
4890 : while (S <= to) {
4891 : if (a[S] < limit) {
4892 : limit = a[S];
4893 : pos = S + (1 - from);
4894 : }
4895 : S++;
4896 : }
4897 : lab2:;
4898 : 3) NaNs aren't supported, array mask is used:
4899 : limit = infinities_supported ? Infinity : huge (limit);
4900 : pos = 0;
4901 : S = from;
4902 : while (S <= to) {
4903 : if (mask[S]) {
4904 : limit = a[S];
4905 : pos = S + (1 - from);
4906 : goto lab1;
4907 : }
4908 : S++;
4909 : }
4910 : goto lab2;
4911 : lab1:;
4912 : while (S <= to) {
4913 : if (mask[S])
4914 : if (a[S] < limit) {
4915 : limit = a[S];
4916 : pos = S + (1 - from);
4917 : }
4918 : S++;
4919 : }
4920 : lab2:;
4921 : 4) Same without array mask:
4922 : limit = infinities_supported ? Infinity : huge (limit);
4923 : pos = (from <= to) ? 1 : 0;
4924 : S = from;
4925 : while (S <= to) {
4926 : if (a[S] < limit) {
4927 : limit = a[S];
4928 : pos = S + (1 - from);
4929 : }
4930 : S++;
4931 : }
4932 : B: Array result, non-CHARACTER type, DIM absent
4933 : Generate similar code as in the scalar case, using a collection of
4934 : variables (one per dimension) instead of a single variable as result.
4935 : Picking only cases 1) and 4) with ARRAY of rank 2, the generated code
4936 : becomes:
4937 : 1) Array mask is used and NaNs need to be supported:
4938 : limit = Infinity;
4939 : pos0 = 0;
4940 : pos1 = 0;
4941 : S1 = from1;
4942 : second_loop_entry = false;
4943 : while (S1 <= to1) {
4944 : S0 = from0;
4945 : while (s0 <= to0 {
4946 : if (mask[S1][S0]) {
4947 : if (pos0 == 0) {
4948 : pos0 = S0 + (1 - from0);
4949 : pos1 = S1 + (1 - from1);
4950 : }
4951 : if (a[S1][S0] <= limit) {
4952 : limit = a[S1][S0];
4953 : pos0 = S0 + (1 - from0);
4954 : pos1 = S1 + (1 - from1);
4955 : second_loop_entry = true;
4956 : goto lab1;
4957 : }
4958 : }
4959 : S0++;
4960 : }
4961 : S1++;
4962 : }
4963 : goto lab2;
4964 : lab1:;
4965 : S1 = second_loop_entry ? S1 : from1;
4966 : while (S1 <= to1) {
4967 : S0 = second_loop_entry ? S0 : from0;
4968 : while (S0 <= to0) {
4969 : if (mask[S1][S0])
4970 : if (a[S1][S0] < limit) {
4971 : limit = a[S1][S0];
4972 : pos0 = S + (1 - from0);
4973 : pos1 = S + (1 - from1);
4974 : }
4975 : second_loop_entry = false;
4976 : S0++;
4977 : }
4978 : S1++;
4979 : }
4980 : lab2:;
4981 : result = { pos0, pos1 };
4982 : ...
4983 : 4) NANs aren't supported, no array mask.
4984 : limit = infinities_supported ? Infinity : huge (limit);
4985 : pos0 = (from0 <= to0 && from1 <= to1) ? 1 : 0;
4986 : pos1 = (from0 <= to0 && from1 <= to1) ? 1 : 0;
4987 : S1 = from1;
4988 : while (S1 <= to1) {
4989 : S0 = from0;
4990 : while (S0 <= to0) {
4991 : if (a[S1][S0] < limit) {
4992 : limit = a[S1][S0];
4993 : pos0 = S + (1 - from0);
4994 : pos1 = S + (1 - from1);
4995 : }
4996 : S0++;
4997 : }
4998 : S1++;
4999 : }
5000 : result = { pos0, pos1 };
5001 : C: Otherwise, a call is generated.
5002 : For 2) and 4), if mask is scalar, this all goes into a conditional,
5003 : setting pos = 0; in the else branch.
5004 :
5005 : Since we now also support the BACK argument, instead of using
5006 : if (a[S] < limit), we now use
5007 :
5008 : if (back)
5009 : cond = a[S] <= limit;
5010 : else
5011 : cond = a[S] < limit;
5012 : if (cond) {
5013 : ....
5014 :
5015 : The optimizer is smart enough to move the condition out of the loop.
5016 : They are now marked as unlikely too for further speedup. */
5017 :
5018 : static void
5019 18898 : gfc_conv_intrinsic_minmaxloc (gfc_se * se, gfc_expr * expr, enum tree_code op)
5020 : {
5021 18898 : stmtblock_t body;
5022 18898 : stmtblock_t block;
5023 18898 : stmtblock_t ifblock;
5024 18898 : stmtblock_t elseblock;
5025 18898 : tree limit;
5026 18898 : tree type;
5027 18898 : tree tmp;
5028 18898 : tree cond;
5029 18898 : tree elsetmp;
5030 18898 : tree ifbody;
5031 18898 : tree offset[GFC_MAX_DIMENSIONS];
5032 18898 : tree nonempty;
5033 18898 : tree lab1, lab2;
5034 18898 : tree b_if, b_else;
5035 18898 : tree back;
5036 18898 : gfc_loopinfo loop, *ploop;
5037 18898 : gfc_actual_arglist *array_arg, *dim_arg, *mask_arg, *kind_arg;
5038 18898 : gfc_actual_arglist *back_arg;
5039 18898 : gfc_ss *arrayss = nullptr;
5040 18898 : gfc_ss *maskss = nullptr;
5041 18898 : gfc_ss *orig_ss = nullptr;
5042 18898 : gfc_se arrayse;
5043 18898 : gfc_se maskse;
5044 18898 : gfc_se nested_se;
5045 18898 : gfc_se *base_se;
5046 18898 : gfc_expr *arrayexpr;
5047 18898 : gfc_expr *maskexpr;
5048 18898 : gfc_expr *backexpr;
5049 18898 : gfc_se backse;
5050 18898 : tree pos[GFC_MAX_DIMENSIONS];
5051 18898 : tree idx[GFC_MAX_DIMENSIONS];
5052 18898 : tree result_var = NULL_TREE;
5053 18898 : int n;
5054 18898 : bool optional_mask;
5055 :
5056 18898 : array_arg = expr->value.function.actual;
5057 18898 : dim_arg = array_arg->next;
5058 18898 : mask_arg = dim_arg->next;
5059 18898 : kind_arg = mask_arg->next;
5060 18898 : back_arg = kind_arg->next;
5061 :
5062 18898 : bool dim_present = dim_arg->expr != nullptr;
5063 18898 : bool nested_loop = dim_present && expr->rank > 0;
5064 :
5065 : /* Remove kind. */
5066 18898 : if (kind_arg->expr)
5067 : {
5068 2240 : gfc_free_expr (kind_arg->expr);
5069 2240 : kind_arg->expr = NULL;
5070 : }
5071 :
5072 : /* Pass BACK argument by value. */
5073 18898 : back_arg->name = "%VAL";
5074 :
5075 18898 : if (se->ss)
5076 : {
5077 14732 : if (se->ss->info->useflags)
5078 : {
5079 7671 : if (!dim_present || !gfc_inline_intrinsic_function_p (expr))
5080 : {
5081 : /* The code generating and initializing the result array has been
5082 : generated already before the scalarization loop, either with a
5083 : library function call or with inline code; now we can just use
5084 : the result. */
5085 4875 : gfc_conv_tmp_array_ref (se);
5086 13822 : return;
5087 : }
5088 : }
5089 7061 : else if (!gfc_inline_intrinsic_function_p (expr))
5090 : {
5091 3780 : gfc_conv_intrinsic_funcall (se, expr);
5092 3780 : return;
5093 : }
5094 : }
5095 :
5096 10243 : arrayexpr = array_arg->expr;
5097 :
5098 : /* Special case for character maxloc. Remove unneeded "dim" actual
5099 : argument, then call a library function. */
5100 :
5101 10243 : if (arrayexpr->ts.type == BT_CHARACTER)
5102 : {
5103 292 : gcc_assert (expr->rank == 0);
5104 :
5105 292 : if (dim_arg->expr)
5106 : {
5107 292 : gfc_free_expr (dim_arg->expr);
5108 292 : dim_arg->expr = NULL;
5109 : }
5110 292 : gfc_conv_intrinsic_funcall (se, expr);
5111 292 : return;
5112 : }
5113 :
5114 9951 : type = gfc_typenode_for_spec (&expr->ts);
5115 :
5116 9951 : if (expr->rank > 0 && !dim_present)
5117 : {
5118 3281 : gfc_array_spec as;
5119 3281 : memset (&as, 0, sizeof (as));
5120 :
5121 3281 : as.rank = 1;
5122 3281 : as.lower[0] = gfc_get_int_expr (gfc_index_integer_kind,
5123 : &arrayexpr->where,
5124 : HOST_WIDE_INT_1);
5125 6562 : as.upper[0] = gfc_get_int_expr (gfc_index_integer_kind,
5126 : &arrayexpr->where,
5127 3281 : arrayexpr->rank);
5128 :
5129 3281 : tree array = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC, true);
5130 :
5131 3281 : result_var = gfc_create_var (array, "loc_result");
5132 : }
5133 :
5134 7155 : const int reduction_dimensions = dim_present ? 1 : arrayexpr->rank;
5135 :
5136 : /* Initialize the result. */
5137 22177 : for (int i = 0; i < reduction_dimensions; i++)
5138 : {
5139 12226 : pos[i] = gfc_create_var (gfc_array_index_type,
5140 : gfc_get_string ("pos%d", i));
5141 12226 : offset[i] = gfc_create_var (gfc_array_index_type,
5142 : gfc_get_string ("offset%d", i));
5143 12226 : idx[i] = gfc_create_var (gfc_array_index_type,
5144 : gfc_get_string ("idx%d", i));
5145 : }
5146 :
5147 9951 : maskexpr = mask_arg->expr;
5148 6518 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
5149 5329 : && maskexpr->symtree->n.sym->attr.dummy
5150 10116 : && maskexpr->symtree->n.sym->attr.optional;
5151 9951 : backexpr = back_arg->expr;
5152 :
5153 17106 : gfc_init_se (&backse, nested_loop ? se : nullptr);
5154 9951 : if (backexpr == nullptr)
5155 0 : back = logical_false_node;
5156 9951 : else if (maybe_absent_optional_variable (backexpr))
5157 : {
5158 : /* This should have been checked already by
5159 : maybe_absent_optional_variable. */
5160 184 : gcc_checking_assert (backexpr->expr_type == EXPR_VARIABLE);
5161 :
5162 184 : gfc_conv_expr (&backse, backexpr);
5163 184 : tree present = gfc_conv_expr_present (backexpr->symtree->n.sym, false);
5164 184 : back = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
5165 : logical_type_node, present, backse.expr);
5166 : }
5167 : else
5168 : {
5169 9767 : gfc_conv_expr (&backse, backexpr);
5170 9767 : back = backse.expr;
5171 : }
5172 9951 : gfc_add_block_to_block (&se->pre, &backse.pre);
5173 9951 : back = gfc_evaluate_now_loc (input_location, back, &se->pre);
5174 9951 : gfc_add_block_to_block (&se->pre, &backse.post);
5175 :
5176 9951 : if (nested_loop)
5177 : {
5178 2796 : gfc_init_se (&nested_se, se);
5179 2796 : base_se = &nested_se;
5180 : }
5181 : else
5182 : {
5183 : /* Walk the arguments. */
5184 7155 : arrayss = gfc_walk_expr (arrayexpr);
5185 7155 : gcc_assert (arrayss != gfc_ss_terminator);
5186 :
5187 7155 : if (maskexpr && maskexpr->rank != 0)
5188 : {
5189 2700 : maskss = gfc_walk_expr (maskexpr);
5190 2700 : gcc_assert (maskss != gfc_ss_terminator);
5191 : }
5192 :
5193 : base_se = nullptr;
5194 : }
5195 :
5196 18091 : nonempty = nullptr;
5197 7448 : if (!(maskexpr && maskexpr->rank > 0))
5198 : {
5199 6077 : mpz_t asize;
5200 6077 : bool reduction_size_known;
5201 :
5202 6077 : if (dim_present)
5203 : {
5204 4032 : int reduction_dim;
5205 4032 : if (dim_arg->expr->expr_type == EXPR_CONSTANT)
5206 4030 : reduction_dim = mpz_get_si (dim_arg->expr->value.integer) - 1;
5207 2 : else if (arrayexpr->rank == 1)
5208 : reduction_dim = 0;
5209 : else
5210 0 : gcc_unreachable ();
5211 4032 : reduction_size_known = gfc_array_dimen_size (arrayexpr, reduction_dim,
5212 : &asize);
5213 : }
5214 : else
5215 2045 : reduction_size_known = gfc_array_size (arrayexpr, &asize);
5216 :
5217 6077 : if (reduction_size_known)
5218 : {
5219 4482 : nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
5220 4482 : mpz_clear (asize);
5221 4482 : nonempty = fold_build2_loc (input_location, GT_EXPR,
5222 : logical_type_node, nonempty,
5223 : gfc_index_zero_node);
5224 : }
5225 6077 : maskss = NULL;
5226 : }
5227 :
5228 9951 : limit = gfc_create_var (gfc_typenode_for_spec (&arrayexpr->ts), "limit");
5229 9951 : switch (arrayexpr->ts.type)
5230 : {
5231 3898 : case BT_REAL:
5232 3898 : tmp = gfc_build_inf_or_huge (TREE_TYPE (limit), arrayexpr->ts.kind);
5233 3898 : break;
5234 :
5235 6029 : case BT_INTEGER:
5236 6029 : n = gfc_validate_kind (arrayexpr->ts.type, arrayexpr->ts.kind, false);
5237 6029 : tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
5238 : arrayexpr->ts.kind);
5239 6029 : break;
5240 :
5241 24 : case BT_UNSIGNED:
5242 : /* For MAXVAL, the minimum is zero, for MINVAL it is HUGE(). */
5243 24 : if (op == GT_EXPR)
5244 : {
5245 12 : tmp = gfc_get_unsigned_type (arrayexpr->ts.kind);
5246 12 : tmp = build_int_cst (tmp, 0);
5247 : }
5248 : else
5249 : {
5250 12 : n = gfc_validate_kind (arrayexpr->ts.type, arrayexpr->ts.kind, false);
5251 12 : tmp = gfc_conv_mpz_unsigned_to_tree (gfc_unsigned_kinds[n].huge,
5252 : expr->ts.kind);
5253 : }
5254 : break;
5255 :
5256 0 : default:
5257 0 : gcc_unreachable ();
5258 : }
5259 :
5260 : /* We start with the most negative possible value for MAXLOC, and the most
5261 : positive possible value for MINLOC. The most negative possible value is
5262 : -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
5263 : possible value is HUGE in both cases. BT_UNSIGNED has already been dealt
5264 : with above. */
5265 9951 : if (op == GT_EXPR && expr->ts.type != BT_UNSIGNED)
5266 4724 : tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
5267 4724 : if (op == GT_EXPR && arrayexpr->ts.type == BT_INTEGER)
5268 2914 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp), tmp,
5269 2914 : build_int_cst (TREE_TYPE (tmp), 1));
5270 :
5271 9951 : gfc_add_modify (&se->pre, limit, tmp);
5272 :
5273 : /* If we are in a case where we generate two sets of loops, the second one
5274 : should continue where the first stopped instead of restarting from the
5275 : beginning. So nested loops in the second set should have a partial range
5276 : on the first iteration, but they should start from the beginning and span
5277 : their full range on the following iterations. So we use conditionals in
5278 : the loops lower bounds, and use the following variable in those
5279 : conditionals to decide whether to use the original loop bound or to use
5280 : the index at which the loop from the first set stopped. */
5281 9951 : tree second_loop_entry = gfc_create_var (logical_type_node,
5282 : "second_loop_entry");
5283 9951 : gfc_add_modify (&se->pre, second_loop_entry, logical_false_node);
5284 :
5285 9951 : if (nested_loop)
5286 : {
5287 2796 : ploop = enter_nested_loop (&nested_se);
5288 2796 : orig_ss = nested_se.ss;
5289 2796 : ploop->temp_dim = 1;
5290 : }
5291 : else
5292 : {
5293 : /* Initialize the scalarizer. */
5294 7155 : gfc_init_loopinfo (&loop);
5295 :
5296 : /* We add the mask first because the number of iterations is taken
5297 : from the last ss, and this breaks if an absent optional argument
5298 : is used for mask. */
5299 :
5300 7155 : if (maskss)
5301 2700 : gfc_add_ss_to_loop (&loop, maskss);
5302 :
5303 7155 : gfc_add_ss_to_loop (&loop, arrayss);
5304 :
5305 : /* Initialize the loop. */
5306 7155 : gfc_conv_ss_startstride (&loop);
5307 :
5308 : /* The code generated can have more than one loop in sequence (see the
5309 : comment at the function header). This doesn't work well with the
5310 : scalarizer, which changes arrays' offset when the scalarization loops
5311 : are generated (see gfc_trans_preloop_setup). Fortunately, we can use
5312 : the scalarizer temporary code to handle multiple loops. Thus, we set
5313 : temp_dim here, we call gfc_mark_ss_chain_used with flag=3 later, and
5314 : we use gfc_trans_scalarized_loop_boundary even later to restore
5315 : offset. */
5316 7155 : loop.temp_dim = loop.dimen;
5317 7155 : gfc_conv_loop_setup (&loop, &expr->where);
5318 :
5319 7155 : ploop = &loop;
5320 : }
5321 :
5322 9951 : gcc_assert (reduction_dimensions == ploop->dimen);
5323 :
5324 9951 : if (nonempty == NULL && !(maskexpr && maskexpr->rank > 0))
5325 : {
5326 1595 : nonempty = logical_true_node;
5327 :
5328 3697 : for (int i = 0; i < ploop->dimen; i++)
5329 : {
5330 2102 : if (!(ploop->from[i] && ploop->to[i]))
5331 : {
5332 : nonempty = NULL;
5333 : break;
5334 : }
5335 :
5336 2102 : tree tmp = fold_build2_loc (input_location, LE_EXPR,
5337 : logical_type_node, ploop->from[i],
5338 : ploop->to[i]);
5339 :
5340 2102 : nonempty = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
5341 : logical_type_node, nonempty, tmp);
5342 : }
5343 : }
5344 :
5345 11546 : lab1 = NULL;
5346 11546 : lab2 = NULL;
5347 : /* Initialize the position to zero, following Fortran 2003. We are free
5348 : to do this because Fortran 95 allows the result of an entirely false
5349 : mask to be processor dependent. If we know at compile time the array
5350 : is non-empty and no MASK is used, we can initialize to 1 to simplify
5351 : the inner loop. */
5352 9951 : if (nonempty != NULL && !HONOR_NANS (DECL_MODE (limit)))
5353 : {
5354 3748 : tree init = fold_build3_loc (input_location, COND_EXPR,
5355 : gfc_array_index_type, nonempty,
5356 : gfc_index_one_node,
5357 : gfc_index_zero_node);
5358 12178 : for (int i = 0; i < ploop->dimen; i++)
5359 4682 : gfc_add_modify (&ploop->pre, pos[i], init);
5360 : }
5361 : else
5362 : {
5363 13747 : for (int i = 0; i < ploop->dimen; i++)
5364 7544 : gfc_add_modify (&ploop->pre, pos[i], gfc_index_zero_node);
5365 6203 : lab1 = gfc_build_label_decl (NULL_TREE);
5366 6203 : TREE_USED (lab1) = 1;
5367 6203 : lab2 = gfc_build_label_decl (NULL_TREE);
5368 6203 : TREE_USED (lab2) = 1;
5369 : }
5370 :
5371 : /* An offset must be added to the loop
5372 : counter to obtain the required position. */
5373 22177 : for (int i = 0; i < ploop->dimen; i++)
5374 : {
5375 12226 : gcc_assert (ploop->from[i]);
5376 :
5377 12226 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5378 : gfc_index_one_node, ploop->from[i]);
5379 12226 : gfc_add_modify (&ploop->pre, offset[i], tmp);
5380 : }
5381 :
5382 9951 : if (!nested_loop)
5383 : {
5384 9965 : gfc_mark_ss_chain_used (arrayss, lab1 ? 3 : 1);
5385 7155 : if (maskss)
5386 2700 : gfc_mark_ss_chain_used (maskss, lab1 ? 3 : 1);
5387 : }
5388 :
5389 : /* Generate the loop body. */
5390 9951 : gfc_start_scalarized_body (ploop, &body);
5391 :
5392 : /* If we have a mask, only check this element if the mask is set. */
5393 9951 : if (maskexpr && maskexpr->rank > 0)
5394 : {
5395 3874 : gfc_init_se (&maskse, base_se);
5396 3874 : gfc_copy_loopinfo_to_se (&maskse, ploop);
5397 3874 : if (!nested_loop)
5398 2700 : maskse.ss = maskss;
5399 3874 : gfc_conv_expr_val (&maskse, maskexpr);
5400 3874 : gfc_add_block_to_block (&body, &maskse.pre);
5401 :
5402 3874 : gfc_start_block (&block);
5403 : }
5404 : else
5405 6077 : gfc_init_block (&block);
5406 :
5407 : /* Compare with the current limit. */
5408 9951 : gfc_init_se (&arrayse, base_se);
5409 9951 : gfc_copy_loopinfo_to_se (&arrayse, ploop);
5410 9951 : if (!nested_loop)
5411 7155 : arrayse.ss = arrayss;
5412 9951 : gfc_conv_expr_val (&arrayse, arrayexpr);
5413 9951 : gfc_add_block_to_block (&block, &arrayse.pre);
5414 :
5415 : /* We do the following if this is a more extreme value. */
5416 9951 : gfc_start_block (&ifblock);
5417 :
5418 : /* Assign the value to the limit... */
5419 9951 : gfc_add_modify (&ifblock, limit, arrayse.expr);
5420 :
5421 9951 : if (nonempty == NULL && HONOR_NANS (DECL_MODE (limit)))
5422 : {
5423 1569 : stmtblock_t ifblock2;
5424 1569 : tree ifbody2;
5425 :
5426 1569 : gfc_start_block (&ifblock2);
5427 5008 : for (int i = 0; i < ploop->dimen; i++)
5428 : {
5429 1870 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
5430 : ploop->loopvar[i], offset[i]);
5431 1870 : gfc_add_modify (&ifblock2, pos[i], tmp);
5432 : }
5433 1569 : ifbody2 = gfc_finish_block (&ifblock2);
5434 :
5435 1569 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
5436 : pos[0], gfc_index_zero_node);
5437 1569 : tmp = build3_v (COND_EXPR, cond, ifbody2,
5438 : build_empty_stmt (input_location));
5439 1569 : gfc_add_expr_to_block (&block, tmp);
5440 : }
5441 :
5442 22177 : for (int i = 0; i < ploop->dimen; i++)
5443 : {
5444 12226 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
5445 : ploop->loopvar[i], offset[i]);
5446 12226 : gfc_add_modify (&ifblock, pos[i], tmp);
5447 12226 : gfc_add_modify (&ifblock, idx[i], ploop->loopvar[i]);
5448 : }
5449 :
5450 9951 : gfc_add_modify (&ifblock, second_loop_entry, logical_true_node);
5451 :
5452 9951 : if (lab1)
5453 6203 : gfc_add_expr_to_block (&ifblock, build1_v (GOTO_EXPR, lab1));
5454 :
5455 9951 : ifbody = gfc_finish_block (&ifblock);
5456 :
5457 9951 : if (!lab1 || HONOR_NANS (DECL_MODE (limit)))
5458 : {
5459 7646 : if (lab1)
5460 5998 : cond = fold_build2_loc (input_location,
5461 : op == GT_EXPR ? GE_EXPR : LE_EXPR,
5462 : logical_type_node, arrayse.expr, limit);
5463 : else
5464 : {
5465 3748 : tree ifbody2, elsebody2;
5466 :
5467 : /* We switch to > or >= depending on the value of the BACK argument. */
5468 3748 : cond = gfc_create_var (logical_type_node, "cond");
5469 :
5470 3748 : gfc_start_block (&ifblock);
5471 5641 : b_if = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
5472 : logical_type_node, arrayse.expr, limit);
5473 :
5474 3748 : gfc_add_modify (&ifblock, cond, b_if);
5475 3748 : ifbody2 = gfc_finish_block (&ifblock);
5476 :
5477 3748 : gfc_start_block (&elseblock);
5478 3748 : b_else = fold_build2_loc (input_location, op, logical_type_node,
5479 : arrayse.expr, limit);
5480 :
5481 3748 : gfc_add_modify (&elseblock, cond, b_else);
5482 3748 : elsebody2 = gfc_finish_block (&elseblock);
5483 :
5484 3748 : tmp = fold_build3_loc (input_location, COND_EXPR, logical_type_node,
5485 : back, ifbody2, elsebody2);
5486 :
5487 3748 : gfc_add_expr_to_block (&block, tmp);
5488 : }
5489 :
5490 7646 : cond = gfc_unlikely (cond, PRED_BUILTIN_EXPECT);
5491 7646 : ifbody = build3_v (COND_EXPR, cond, ifbody,
5492 : build_empty_stmt (input_location));
5493 : }
5494 9951 : gfc_add_expr_to_block (&block, ifbody);
5495 :
5496 9951 : if (maskexpr && maskexpr->rank > 0)
5497 : {
5498 : /* We enclose the above in if (mask) {...}. If the mask is an
5499 : optional argument, generate IF (.NOT. PRESENT(MASK)
5500 : .OR. MASK(I)). */
5501 :
5502 3874 : tree ifmask;
5503 3874 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5504 3874 : tmp = gfc_finish_block (&block);
5505 3874 : tmp = build3_v (COND_EXPR, ifmask, tmp,
5506 : build_empty_stmt (input_location));
5507 3874 : }
5508 : else
5509 6077 : tmp = gfc_finish_block (&block);
5510 9951 : gfc_add_expr_to_block (&body, tmp);
5511 :
5512 9951 : if (lab1)
5513 : {
5514 13747 : for (int i = 0; i < ploop->dimen; i++)
5515 7544 : ploop->from[i] = fold_build3_loc (input_location, COND_EXPR,
5516 7544 : TREE_TYPE (ploop->from[i]),
5517 : second_loop_entry, idx[i],
5518 : ploop->from[i]);
5519 :
5520 6203 : gfc_trans_scalarized_loop_boundary (ploop, &body);
5521 :
5522 6203 : if (nested_loop)
5523 : {
5524 : /* The first loop already advanced the parent se'ss chain, so clear
5525 : the parent now to avoid doing it a second time, making the chain
5526 : out of sync. */
5527 1858 : nested_se.parent = nullptr;
5528 1858 : nested_se.ss = orig_ss;
5529 : }
5530 :
5531 6203 : stmtblock_t * const outer_block = &ploop->code[ploop->dimen - 1];
5532 :
5533 6203 : if (HONOR_NANS (DECL_MODE (limit)))
5534 : {
5535 3898 : if (nonempty != NULL)
5536 : {
5537 2329 : stmtblock_t init_block;
5538 2329 : gfc_init_block (&init_block);
5539 :
5540 7558 : for (int i = 0; i < ploop->dimen; i++)
5541 2900 : gfc_add_modify (&init_block, pos[i], gfc_index_one_node);
5542 :
5543 2329 : tree ifbody = gfc_finish_block (&init_block);
5544 2329 : tmp = build3_v (COND_EXPR, nonempty, ifbody,
5545 : build_empty_stmt (input_location));
5546 2329 : gfc_add_expr_to_block (outer_block, tmp);
5547 : }
5548 : }
5549 :
5550 6203 : gfc_add_expr_to_block (outer_block, build1_v (GOTO_EXPR, lab2));
5551 6203 : gfc_add_expr_to_block (outer_block, build1_v (LABEL_EXPR, lab1));
5552 :
5553 : /* If we have a mask, only check this element if the mask is set. */
5554 6203 : if (maskexpr && maskexpr->rank > 0)
5555 : {
5556 3874 : gfc_init_se (&maskse, base_se);
5557 3874 : gfc_copy_loopinfo_to_se (&maskse, ploop);
5558 3874 : if (!nested_loop)
5559 2700 : maskse.ss = maskss;
5560 3874 : gfc_conv_expr_val (&maskse, maskexpr);
5561 3874 : gfc_add_block_to_block (&body, &maskse.pre);
5562 :
5563 3874 : gfc_start_block (&block);
5564 : }
5565 : else
5566 2329 : gfc_init_block (&block);
5567 :
5568 : /* Compare with the current limit. */
5569 6203 : gfc_init_se (&arrayse, base_se);
5570 6203 : gfc_copy_loopinfo_to_se (&arrayse, ploop);
5571 6203 : if (!nested_loop)
5572 4345 : arrayse.ss = arrayss;
5573 6203 : gfc_conv_expr_val (&arrayse, arrayexpr);
5574 6203 : gfc_add_block_to_block (&block, &arrayse.pre);
5575 :
5576 : /* We do the following if this is a more extreme value. */
5577 6203 : gfc_start_block (&ifblock);
5578 :
5579 : /* Assign the value to the limit... */
5580 6203 : gfc_add_modify (&ifblock, limit, arrayse.expr);
5581 :
5582 19950 : for (int i = 0; i < ploop->dimen; i++)
5583 : {
5584 7544 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (pos[i]),
5585 : ploop->loopvar[i], offset[i]);
5586 7544 : gfc_add_modify (&ifblock, pos[i], tmp);
5587 : }
5588 :
5589 6203 : ifbody = gfc_finish_block (&ifblock);
5590 :
5591 : /* We switch to > or >= depending on the value of the BACK argument. */
5592 6203 : {
5593 6203 : tree ifbody2, elsebody2;
5594 :
5595 6203 : cond = gfc_create_var (logical_type_node, "cond");
5596 :
5597 6203 : gfc_start_block (&ifblock);
5598 9537 : b_if = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
5599 : logical_type_node, arrayse.expr, limit);
5600 :
5601 6203 : gfc_add_modify (&ifblock, cond, b_if);
5602 6203 : ifbody2 = gfc_finish_block (&ifblock);
5603 :
5604 6203 : gfc_start_block (&elseblock);
5605 6203 : b_else = fold_build2_loc (input_location, op, logical_type_node,
5606 : arrayse.expr, limit);
5607 :
5608 6203 : gfc_add_modify (&elseblock, cond, b_else);
5609 6203 : elsebody2 = gfc_finish_block (&elseblock);
5610 :
5611 6203 : tmp = fold_build3_loc (input_location, COND_EXPR, logical_type_node,
5612 : back, ifbody2, elsebody2);
5613 : }
5614 :
5615 6203 : gfc_add_expr_to_block (&block, tmp);
5616 6203 : cond = gfc_unlikely (cond, PRED_BUILTIN_EXPECT);
5617 6203 : tmp = build3_v (COND_EXPR, cond, ifbody,
5618 : build_empty_stmt (input_location));
5619 :
5620 6203 : gfc_add_expr_to_block (&block, tmp);
5621 :
5622 6203 : if (maskexpr && maskexpr->rank > 0)
5623 : {
5624 : /* We enclose the above in if (mask) {...}. If the mask is
5625 : an optional argument, generate IF (.NOT. PRESENT(MASK)
5626 : .OR. MASK(I)).*/
5627 :
5628 3874 : tree ifmask;
5629 3874 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5630 3874 : tmp = gfc_finish_block (&block);
5631 3874 : tmp = build3_v (COND_EXPR, ifmask, tmp,
5632 : build_empty_stmt (input_location));
5633 3874 : }
5634 : else
5635 2329 : tmp = gfc_finish_block (&block);
5636 :
5637 6203 : gfc_add_expr_to_block (&body, tmp);
5638 6203 : gfc_add_modify (&body, second_loop_entry, logical_false_node);
5639 : }
5640 :
5641 9951 : gfc_trans_scalarizing_loops (ploop, &body);
5642 :
5643 9951 : if (lab2)
5644 6203 : gfc_add_expr_to_block (&ploop->pre, build1_v (LABEL_EXPR, lab2));
5645 :
5646 : /* For a scalar mask, enclose the loop in an if statement. */
5647 9951 : if (maskexpr && maskexpr->rank == 0)
5648 : {
5649 2644 : tree ifmask;
5650 :
5651 2644 : gfc_init_se (&maskse, nested_loop ? se : nullptr);
5652 2644 : gfc_conv_expr_val (&maskse, maskexpr);
5653 2644 : gfc_add_block_to_block (&se->pre, &maskse.pre);
5654 2644 : gfc_init_block (&block);
5655 2644 : gfc_add_block_to_block (&block, &ploop->pre);
5656 2644 : gfc_add_block_to_block (&block, &ploop->post);
5657 2644 : tmp = gfc_finish_block (&block);
5658 :
5659 : /* For the else part of the scalar mask, just initialize
5660 : the pos variable the same way as above. */
5661 :
5662 2644 : gfc_init_block (&elseblock);
5663 8224 : for (int i = 0; i < ploop->dimen; i++)
5664 2936 : gfc_add_modify (&elseblock, pos[i], gfc_index_zero_node);
5665 2644 : elsetmp = gfc_finish_block (&elseblock);
5666 2644 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5667 2644 : tmp = build3_v (COND_EXPR, ifmask, tmp, elsetmp);
5668 2644 : gfc_add_expr_to_block (&block, tmp);
5669 2644 : gfc_add_block_to_block (&se->pre, &block);
5670 2644 : }
5671 : else
5672 : {
5673 7307 : gfc_add_block_to_block (&se->pre, &ploop->pre);
5674 7307 : gfc_add_block_to_block (&se->pre, &ploop->post);
5675 : }
5676 :
5677 9951 : if (!nested_loop)
5678 7155 : gfc_cleanup_loop (&loop);
5679 :
5680 9951 : if (!dim_present)
5681 : {
5682 8837 : for (int i = 0; i < arrayexpr->rank; i++)
5683 : {
5684 5556 : tree res_idx = build_int_cst (gfc_array_index_type, i);
5685 5556 : tree res_arr_ref = gfc_build_array_ref (result_var, res_idx,
5686 : NULL_TREE, true);
5687 :
5688 5556 : tree value = convert (type, pos[i]);
5689 5556 : gfc_add_modify (&se->pre, res_arr_ref, value);
5690 : }
5691 :
5692 3281 : se->expr = result_var;
5693 : }
5694 : else
5695 6670 : se->expr = convert (type, pos[0]);
5696 : }
5697 :
5698 : /* Emit code for findloc. */
5699 :
5700 : static void
5701 1332 : gfc_conv_intrinsic_findloc (gfc_se *se, gfc_expr *expr)
5702 : {
5703 1332 : gfc_actual_arglist *array_arg, *value_arg, *dim_arg, *mask_arg,
5704 : *kind_arg, *back_arg;
5705 1332 : gfc_expr *value_expr;
5706 1332 : int ikind;
5707 1332 : tree resvar;
5708 1332 : stmtblock_t block;
5709 1332 : stmtblock_t body;
5710 1332 : stmtblock_t loopblock;
5711 1332 : tree type;
5712 1332 : tree tmp;
5713 1332 : tree found;
5714 1332 : tree forward_branch = NULL_TREE;
5715 1332 : tree back_branch;
5716 1332 : gfc_loopinfo loop;
5717 1332 : gfc_ss *arrayss;
5718 1332 : gfc_ss *maskss;
5719 1332 : gfc_se arrayse;
5720 1332 : gfc_se valuese;
5721 1332 : gfc_se maskse;
5722 1332 : gfc_se backse;
5723 1332 : tree exit_label;
5724 1332 : gfc_expr *maskexpr;
5725 1332 : tree offset;
5726 1332 : int i;
5727 1332 : bool optional_mask;
5728 :
5729 1332 : array_arg = expr->value.function.actual;
5730 1332 : value_arg = array_arg->next;
5731 1332 : dim_arg = value_arg->next;
5732 1332 : mask_arg = dim_arg->next;
5733 1332 : kind_arg = mask_arg->next;
5734 1332 : back_arg = kind_arg->next;
5735 :
5736 : /* Remove kind and set ikind. */
5737 1332 : if (kind_arg->expr)
5738 : {
5739 0 : ikind = mpz_get_si (kind_arg->expr->value.integer);
5740 0 : gfc_free_expr (kind_arg->expr);
5741 0 : kind_arg->expr = NULL;
5742 : }
5743 : else
5744 1332 : ikind = gfc_default_integer_kind;
5745 :
5746 1332 : value_expr = value_arg->expr;
5747 :
5748 : /* Unless it's a string, pass VALUE by value. */
5749 1332 : if (value_expr->ts.type != BT_CHARACTER)
5750 732 : value_arg->name = "%VAL";
5751 :
5752 : /* Pass BACK argument by value. */
5753 1332 : back_arg->name = "%VAL";
5754 :
5755 : /* Call the library if we have a character function or if
5756 : rank > 0. */
5757 1332 : if (se->ss || array_arg->expr->ts.type == BT_CHARACTER)
5758 : {
5759 1200 : se->ignore_optional = 1;
5760 1200 : if (expr->rank == 0)
5761 : {
5762 : /* Remove dim argument. */
5763 84 : gfc_free_expr (dim_arg->expr);
5764 84 : dim_arg->expr = NULL;
5765 : }
5766 1200 : gfc_conv_intrinsic_funcall (se, expr);
5767 1200 : return;
5768 : }
5769 :
5770 132 : type = gfc_get_int_type (ikind);
5771 :
5772 : /* Initialize the result. */
5773 132 : resvar = gfc_create_var (gfc_array_index_type, "pos");
5774 132 : gfc_add_modify (&se->pre, resvar, build_int_cst (gfc_array_index_type, 0));
5775 132 : offset = gfc_create_var (gfc_array_index_type, "offset");
5776 :
5777 132 : maskexpr = mask_arg->expr;
5778 72 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
5779 60 : && maskexpr->symtree->n.sym->attr.dummy
5780 144 : && maskexpr->symtree->n.sym->attr.optional;
5781 :
5782 : /* Generate two loops, one for BACK=.true. and one for BACK=.false. */
5783 :
5784 396 : for (i = 0 ; i < 2; i++)
5785 : {
5786 : /* Walk the arguments. */
5787 264 : arrayss = gfc_walk_expr (array_arg->expr);
5788 264 : gcc_assert (arrayss != gfc_ss_terminator);
5789 :
5790 264 : if (maskexpr && maskexpr->rank != 0)
5791 : {
5792 84 : maskss = gfc_walk_expr (maskexpr);
5793 84 : gcc_assert (maskss != gfc_ss_terminator);
5794 : }
5795 : else
5796 : maskss = NULL;
5797 :
5798 : /* Initialize the scalarizer. */
5799 264 : gfc_init_loopinfo (&loop);
5800 264 : exit_label = gfc_build_label_decl (NULL_TREE);
5801 264 : TREE_USED (exit_label) = 1;
5802 :
5803 : /* We add the mask first because the number of iterations is
5804 : taken from the last ss, and this breaks if an absent
5805 : optional argument is used for mask. */
5806 :
5807 264 : if (maskss)
5808 84 : gfc_add_ss_to_loop (&loop, maskss);
5809 264 : gfc_add_ss_to_loop (&loop, arrayss);
5810 :
5811 : /* Initialize the loop. */
5812 264 : gfc_conv_ss_startstride (&loop);
5813 264 : gfc_conv_loop_setup (&loop, &expr->where);
5814 :
5815 : /* Calculate the offset. */
5816 264 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5817 : gfc_index_one_node, loop.from[0]);
5818 264 : gfc_add_modify (&loop.pre, offset, tmp);
5819 :
5820 264 : gfc_mark_ss_chain_used (arrayss, 1);
5821 264 : if (maskss)
5822 84 : gfc_mark_ss_chain_used (maskss, 1);
5823 :
5824 : /* The first loop is for BACK=.true. */
5825 264 : if (i == 0)
5826 132 : loop.reverse[0] = GFC_REVERSE_SET;
5827 :
5828 : /* Generate the loop body. */
5829 264 : gfc_start_scalarized_body (&loop, &body);
5830 :
5831 : /* If we have an array mask, only add the element if it is
5832 : set. */
5833 264 : if (maskss)
5834 : {
5835 84 : gfc_init_se (&maskse, NULL);
5836 84 : gfc_copy_loopinfo_to_se (&maskse, &loop);
5837 84 : maskse.ss = maskss;
5838 84 : gfc_conv_expr_val (&maskse, maskexpr);
5839 84 : gfc_add_block_to_block (&body, &maskse.pre);
5840 : }
5841 :
5842 : /* If the condition matches then set the return value. */
5843 264 : gfc_start_block (&block);
5844 :
5845 : /* Add the offset. */
5846 264 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
5847 264 : TREE_TYPE (resvar),
5848 : loop.loopvar[0], offset);
5849 264 : gfc_add_modify (&block, resvar, tmp);
5850 : /* And break out of the loop. */
5851 264 : tmp = build1_v (GOTO_EXPR, exit_label);
5852 264 : gfc_add_expr_to_block (&block, tmp);
5853 :
5854 264 : found = gfc_finish_block (&block);
5855 :
5856 : /* Check this element. */
5857 264 : gfc_init_se (&arrayse, NULL);
5858 264 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
5859 264 : arrayse.ss = arrayss;
5860 264 : gfc_conv_expr_val (&arrayse, array_arg->expr);
5861 264 : gfc_add_block_to_block (&body, &arrayse.pre);
5862 :
5863 264 : gfc_init_se (&valuese, NULL);
5864 264 : gfc_conv_expr_val (&valuese, value_arg->expr);
5865 264 : gfc_add_block_to_block (&body, &valuese.pre);
5866 :
5867 264 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
5868 : arrayse.expr, valuese.expr);
5869 :
5870 264 : tmp = build3_v (COND_EXPR, tmp, found, build_empty_stmt (input_location));
5871 264 : if (maskss)
5872 : {
5873 : /* We enclose the above in if (mask) {...}. If the mask is
5874 : an optional argument, generate IF (.NOT. PRESENT(MASK)
5875 : .OR. MASK(I)). */
5876 :
5877 84 : tree ifmask;
5878 84 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5879 84 : tmp = build3_v (COND_EXPR, ifmask, tmp,
5880 : build_empty_stmt (input_location));
5881 : }
5882 :
5883 264 : gfc_add_expr_to_block (&body, tmp);
5884 264 : gfc_add_block_to_block (&body, &arrayse.post);
5885 :
5886 264 : gfc_trans_scalarizing_loops (&loop, &body);
5887 :
5888 : /* Add the exit label. */
5889 264 : tmp = build1_v (LABEL_EXPR, exit_label);
5890 264 : gfc_add_expr_to_block (&loop.pre, tmp);
5891 264 : gfc_start_block (&loopblock);
5892 264 : gfc_add_block_to_block (&loopblock, &loop.pre);
5893 264 : gfc_add_block_to_block (&loopblock, &loop.post);
5894 264 : if (i == 0)
5895 132 : forward_branch = gfc_finish_block (&loopblock);
5896 : else
5897 132 : back_branch = gfc_finish_block (&loopblock);
5898 :
5899 264 : gfc_cleanup_loop (&loop);
5900 : }
5901 :
5902 : /* Enclose the two loops in an IF statement. */
5903 :
5904 132 : gfc_init_se (&backse, NULL);
5905 132 : gfc_conv_expr_val (&backse, back_arg->expr);
5906 132 : gfc_add_block_to_block (&se->pre, &backse.pre);
5907 132 : tmp = build3_v (COND_EXPR, backse.expr, forward_branch, back_branch);
5908 :
5909 : /* For a scalar mask, enclose the loop in an if statement. */
5910 132 : if (maskexpr && maskss == NULL)
5911 : {
5912 30 : tree ifmask;
5913 30 : tree if_stmt;
5914 :
5915 30 : gfc_init_se (&maskse, NULL);
5916 30 : gfc_conv_expr_val (&maskse, maskexpr);
5917 30 : gfc_init_block (&block);
5918 30 : gfc_add_expr_to_block (&block, maskse.expr);
5919 30 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
5920 30 : if_stmt = build3_v (COND_EXPR, ifmask, tmp,
5921 : build_empty_stmt (input_location));
5922 30 : gfc_add_expr_to_block (&block, if_stmt);
5923 30 : tmp = gfc_finish_block (&block);
5924 : }
5925 :
5926 132 : gfc_add_expr_to_block (&se->pre, tmp);
5927 132 : se->expr = convert (type, resvar);
5928 :
5929 : }
5930 :
5931 : /* Emit code for fstat, lstat and stat intrinsic subroutines. */
5932 :
5933 : static tree
5934 55 : conv_intrinsic_fstat_lstat_stat_sub (gfc_code *code)
5935 : {
5936 55 : stmtblock_t block;
5937 55 : gfc_se se, se_stat;
5938 55 : tree unit = NULL_TREE;
5939 55 : tree name = NULL_TREE;
5940 55 : tree slen = NULL_TREE;
5941 55 : tree vals;
5942 55 : tree arg3 = NULL_TREE;
5943 55 : tree stat = NULL_TREE ;
5944 55 : tree present = NULL_TREE;
5945 55 : tree tmp;
5946 55 : int kind;
5947 :
5948 55 : gfc_init_block (&block);
5949 55 : gfc_init_se (&se, NULL);
5950 :
5951 55 : switch (code->resolved_isym->id)
5952 : {
5953 21 : case GFC_ISYM_FSTAT:
5954 : /* Deal with the UNIT argument. */
5955 21 : gfc_conv_expr (&se, code->ext.actual->expr);
5956 21 : gfc_add_block_to_block (&block, &se.pre);
5957 21 : unit = gfc_evaluate_now (se.expr, &block);
5958 21 : unit = gfc_build_addr_expr (NULL_TREE, unit);
5959 21 : gfc_add_block_to_block (&block, &se.post);
5960 21 : break;
5961 :
5962 34 : case GFC_ISYM_LSTAT:
5963 34 : case GFC_ISYM_STAT:
5964 : /* Deal with the NAME argument. */
5965 34 : gfc_conv_expr (&se, code->ext.actual->expr);
5966 34 : gfc_conv_string_parameter (&se);
5967 34 : gfc_add_block_to_block (&block, &se.pre);
5968 34 : name = se.expr;
5969 34 : slen = se.string_length;
5970 34 : gfc_add_block_to_block (&block, &se.post);
5971 34 : break;
5972 :
5973 0 : default:
5974 0 : gcc_unreachable ();
5975 : }
5976 :
5977 : /* Deal with the VALUES argument. */
5978 55 : gfc_init_se (&se, NULL);
5979 55 : gfc_conv_expr_descriptor (&se, code->ext.actual->next->expr);
5980 55 : vals = gfc_build_addr_expr (NULL_TREE, se.expr);
5981 55 : gfc_add_block_to_block (&block, &se.pre);
5982 55 : gfc_add_block_to_block (&block, &se.post);
5983 55 : kind = code->ext.actual->next->expr->ts.kind;
5984 :
5985 : /* Deal with an optional STATUS. */
5986 55 : if (code->ext.actual->next->next->expr)
5987 : {
5988 45 : gfc_init_se (&se_stat, NULL);
5989 45 : gfc_conv_expr (&se_stat, code->ext.actual->next->next->expr);
5990 45 : stat = gfc_create_var (gfc_get_int_type (kind), "_stat");
5991 45 : arg3 = gfc_build_addr_expr (NULL_TREE, stat);
5992 :
5993 : /* Handle case of status being an optional dummy. */
5994 45 : gfc_symbol *sym = code->ext.actual->next->next->expr->symtree->n.sym;
5995 45 : if (sym->attr.dummy && sym->attr.optional)
5996 : {
5997 6 : present = gfc_conv_expr_present (sym);
5998 12 : arg3 = fold_build3_loc (input_location, COND_EXPR,
5999 6 : TREE_TYPE (arg3), present, arg3,
6000 6 : fold_convert (TREE_TYPE (arg3),
6001 : null_pointer_node));
6002 : }
6003 : }
6004 :
6005 : /* Call library function depending on KIND of VALUES argument. */
6006 55 : switch (code->resolved_isym->id)
6007 : {
6008 21 : case GFC_ISYM_FSTAT:
6009 21 : tmp = (kind == 4 ? gfor_fndecl_fstat_i4_sub : gfor_fndecl_fstat_i8_sub);
6010 : break;
6011 14 : case GFC_ISYM_LSTAT:
6012 14 : tmp = (kind == 4 ? gfor_fndecl_lstat_i4_sub : gfor_fndecl_lstat_i8_sub);
6013 : break;
6014 20 : case GFC_ISYM_STAT:
6015 20 : tmp = (kind == 4 ? gfor_fndecl_stat_i4_sub : gfor_fndecl_stat_i8_sub);
6016 : break;
6017 0 : default:
6018 0 : gcc_unreachable ();
6019 : }
6020 :
6021 55 : if (code->resolved_isym->id == GFC_ISYM_FSTAT)
6022 21 : tmp = build_call_expr_loc (input_location, tmp, 3, unit, vals,
6023 : stat ? arg3 : null_pointer_node);
6024 : else
6025 34 : tmp = build_call_expr_loc (input_location, tmp, 4, name, vals,
6026 : stat ? arg3 : null_pointer_node, slen);
6027 55 : gfc_add_expr_to_block (&block, tmp);
6028 :
6029 : /* Handle kind conversion of status. */
6030 55 : if (stat && stat != se_stat.expr)
6031 : {
6032 45 : stmtblock_t block2;
6033 :
6034 45 : gfc_init_block (&block2);
6035 45 : gfc_add_modify (&block2, se_stat.expr,
6036 45 : fold_convert (TREE_TYPE (se_stat.expr), stat));
6037 :
6038 45 : if (present)
6039 : {
6040 6 : tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block2),
6041 : build_empty_stmt (input_location));
6042 6 : gfc_add_expr_to_block (&block, tmp);
6043 : }
6044 : else
6045 39 : gfc_add_block_to_block (&block, &block2);
6046 : }
6047 :
6048 55 : return gfc_finish_block (&block);
6049 : }
6050 :
6051 : /* Emit code for minval or maxval intrinsic. There are many different cases
6052 : we need to handle. For performance reasons we sometimes create two
6053 : loops instead of one, where the second one is much simpler.
6054 : Examples for minval intrinsic:
6055 : 1) Result is an array, a call is generated
6056 : 2) Array mask is used and NaNs need to be supported, rank 1:
6057 : limit = Infinity;
6058 : nonempty = false;
6059 : S = from;
6060 : while (S <= to) {
6061 : if (mask[S]) {
6062 : nonempty = true;
6063 : if (a[S] <= limit) {
6064 : limit = a[S];
6065 : S++;
6066 : goto lab;
6067 : }
6068 : else
6069 : S++;
6070 : }
6071 : }
6072 : limit = nonempty ? NaN : huge (limit);
6073 : lab:
6074 : while (S <= to) { if(mask[S]) limit = min (a[S], limit); S++; }
6075 : 3) NaNs need to be supported, but it is known at compile time or cheaply
6076 : at runtime whether array is nonempty or not, rank 1:
6077 : limit = Infinity;
6078 : S = from;
6079 : while (S <= to) {
6080 : if (a[S] <= limit) {
6081 : limit = a[S];
6082 : S++;
6083 : goto lab;
6084 : }
6085 : else
6086 : S++;
6087 : }
6088 : limit = (from <= to) ? NaN : huge (limit);
6089 : lab:
6090 : while (S <= to) { limit = min (a[S], limit); S++; }
6091 : 4) Array mask is used and NaNs need to be supported, rank > 1:
6092 : limit = Infinity;
6093 : nonempty = false;
6094 : fast = false;
6095 : S1 = from1;
6096 : while (S1 <= to1) {
6097 : S2 = from2;
6098 : while (S2 <= to2) {
6099 : if (mask[S1][S2]) {
6100 : if (fast) limit = min (a[S1][S2], limit);
6101 : else {
6102 : nonempty = true;
6103 : if (a[S1][S2] <= limit) {
6104 : limit = a[S1][S2];
6105 : fast = true;
6106 : }
6107 : }
6108 : }
6109 : S2++;
6110 : }
6111 : S1++;
6112 : }
6113 : if (!fast)
6114 : limit = nonempty ? NaN : huge (limit);
6115 : 5) NaNs need to be supported, but it is known at compile time or cheaply
6116 : at runtime whether array is nonempty or not, rank > 1:
6117 : limit = Infinity;
6118 : fast = false;
6119 : S1 = from1;
6120 : while (S1 <= to1) {
6121 : S2 = from2;
6122 : while (S2 <= to2) {
6123 : if (fast) limit = min (a[S1][S2], limit);
6124 : else {
6125 : if (a[S1][S2] <= limit) {
6126 : limit = a[S1][S2];
6127 : fast = true;
6128 : }
6129 : }
6130 : S2++;
6131 : }
6132 : S1++;
6133 : }
6134 : if (!fast)
6135 : limit = (nonempty_array) ? NaN : huge (limit);
6136 : 6) NaNs aren't supported, but infinities are. Array mask is used:
6137 : limit = Infinity;
6138 : nonempty = false;
6139 : S = from;
6140 : while (S <= to) {
6141 : if (mask[S]) { nonempty = true; limit = min (a[S], limit); }
6142 : S++;
6143 : }
6144 : limit = nonempty ? limit : huge (limit);
6145 : 7) Same without array mask:
6146 : limit = Infinity;
6147 : S = from;
6148 : while (S <= to) { limit = min (a[S], limit); S++; }
6149 : limit = (from <= to) ? limit : huge (limit);
6150 : 8) Neither NaNs nor infinities are supported (-ffast-math or BT_INTEGER):
6151 : limit = huge (limit);
6152 : S = from;
6153 : while (S <= to) { limit = min (a[S], limit); S++); }
6154 : (or
6155 : while (S <= to) { if (mask[S]) limit = min (a[S], limit); S++; }
6156 : with array mask instead).
6157 : For 3), 5), 7) and 8), if mask is scalar, this all goes into a conditional,
6158 : setting limit = huge (limit); in the else branch. */
6159 :
6160 : static void
6161 2417 : gfc_conv_intrinsic_minmaxval (gfc_se * se, gfc_expr * expr, enum tree_code op)
6162 : {
6163 2417 : tree limit;
6164 2417 : tree type;
6165 2417 : tree tmp;
6166 2417 : tree ifbody;
6167 2417 : tree nonempty;
6168 2417 : tree nonempty_var;
6169 2417 : tree lab;
6170 2417 : tree fast;
6171 2417 : tree huge_cst = NULL, nan_cst = NULL;
6172 2417 : stmtblock_t body;
6173 2417 : stmtblock_t block, block2;
6174 2417 : gfc_loopinfo loop;
6175 2417 : gfc_actual_arglist *actual;
6176 2417 : gfc_ss *arrayss;
6177 2417 : gfc_ss *maskss;
6178 2417 : gfc_se arrayse;
6179 2417 : gfc_se maskse;
6180 2417 : gfc_expr *arrayexpr;
6181 2417 : gfc_expr *maskexpr;
6182 2417 : int n;
6183 2417 : bool optional_mask;
6184 :
6185 2417 : if (se->ss)
6186 : {
6187 0 : gfc_conv_intrinsic_funcall (se, expr);
6188 186 : return;
6189 : }
6190 :
6191 2417 : actual = expr->value.function.actual;
6192 2417 : arrayexpr = actual->expr;
6193 :
6194 2417 : if (arrayexpr->ts.type == BT_CHARACTER)
6195 : {
6196 186 : gfc_actual_arglist *dim = actual->next;
6197 186 : if (expr->rank == 0 && dim->expr != 0)
6198 : {
6199 6 : gfc_free_expr (dim->expr);
6200 6 : dim->expr = NULL;
6201 : }
6202 186 : gfc_conv_intrinsic_funcall (se, expr);
6203 186 : return;
6204 : }
6205 :
6206 2231 : type = gfc_typenode_for_spec (&expr->ts);
6207 : /* Initialize the result. */
6208 2231 : limit = gfc_create_var (type, "limit");
6209 2231 : n = gfc_validate_kind (expr->ts.type, expr->ts.kind, false);
6210 2231 : switch (expr->ts.type)
6211 : {
6212 1245 : case BT_REAL:
6213 1245 : huge_cst = gfc_conv_mpfr_to_tree (gfc_real_kinds[n].huge,
6214 : expr->ts.kind, 0);
6215 1245 : if (HONOR_INFINITIES (DECL_MODE (limit)))
6216 : {
6217 1241 : REAL_VALUE_TYPE real;
6218 1241 : real_inf (&real);
6219 1241 : tmp = build_real (type, real);
6220 : }
6221 : else
6222 : tmp = huge_cst;
6223 1245 : if (HONOR_NANS (DECL_MODE (limit)))
6224 1241 : nan_cst = gfc_build_nan (type, "");
6225 : break;
6226 :
6227 956 : case BT_INTEGER:
6228 956 : tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge, expr->ts.kind);
6229 956 : break;
6230 :
6231 30 : case BT_UNSIGNED:
6232 : /* For MAXVAL, the minimum is zero, for MINVAL it is HUGE(). */
6233 30 : if (op == GT_EXPR)
6234 18 : tmp = build_int_cst (type, 0);
6235 : else
6236 12 : tmp = gfc_conv_mpz_unsigned_to_tree (gfc_unsigned_kinds[n].huge,
6237 : expr->ts.kind);
6238 : break;
6239 :
6240 0 : default:
6241 0 : gcc_unreachable ();
6242 : }
6243 :
6244 : /* We start with the most negative possible value for MAXVAL, and the most
6245 : positive possible value for MINVAL. The most negative possible value is
6246 : -HUGE for BT_REAL and (-HUGE - 1) for BT_INTEGER; the most positive
6247 : possible value is HUGE in both cases. BT_UNSIGNED has already been dealt
6248 : with above. */
6249 2231 : if (op == GT_EXPR && expr->ts.type != BT_UNSIGNED)
6250 : {
6251 987 : tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (tmp), tmp);
6252 987 : if (huge_cst)
6253 560 : huge_cst = fold_build1_loc (input_location, NEGATE_EXPR,
6254 560 : TREE_TYPE (huge_cst), huge_cst);
6255 : }
6256 :
6257 1005 : if (op == GT_EXPR && expr->ts.type == BT_INTEGER)
6258 427 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (tmp),
6259 : tmp, build_int_cst (type, 1));
6260 :
6261 2231 : gfc_add_modify (&se->pre, limit, tmp);
6262 :
6263 : /* Walk the arguments. */
6264 2231 : arrayss = gfc_walk_expr (arrayexpr);
6265 2231 : gcc_assert (arrayss != gfc_ss_terminator);
6266 :
6267 2231 : actual = actual->next->next;
6268 2231 : gcc_assert (actual);
6269 2231 : maskexpr = actual->expr;
6270 1572 : optional_mask = maskexpr && maskexpr->expr_type == EXPR_VARIABLE
6271 1560 : && maskexpr->symtree->n.sym->attr.dummy
6272 2243 : && maskexpr->symtree->n.sym->attr.optional;
6273 2777 : nonempty = NULL;
6274 1572 : if (maskexpr && maskexpr->rank != 0)
6275 : {
6276 1026 : maskss = gfc_walk_expr (maskexpr);
6277 1026 : gcc_assert (maskss != gfc_ss_terminator);
6278 : }
6279 : else
6280 : {
6281 1205 : mpz_t asize;
6282 1205 : if (gfc_array_size (arrayexpr, &asize))
6283 : {
6284 678 : nonempty = gfc_conv_mpz_to_tree (asize, gfc_index_integer_kind);
6285 678 : mpz_clear (asize);
6286 678 : nonempty = fold_build2_loc (input_location, GT_EXPR,
6287 : logical_type_node, nonempty,
6288 : gfc_index_zero_node);
6289 : }
6290 1205 : maskss = NULL;
6291 : }
6292 :
6293 : /* Initialize the scalarizer. */
6294 2231 : gfc_init_loopinfo (&loop);
6295 :
6296 : /* We add the mask first because the number of iterations is taken
6297 : from the last ss, and this breaks if an absent optional argument
6298 : is used for mask. */
6299 :
6300 2231 : if (maskss)
6301 1026 : gfc_add_ss_to_loop (&loop, maskss);
6302 2231 : gfc_add_ss_to_loop (&loop, arrayss);
6303 :
6304 : /* Initialize the loop. */
6305 2231 : gfc_conv_ss_startstride (&loop);
6306 :
6307 : /* The code generated can have more than one loop in sequence (see the
6308 : comment at the function header). This doesn't work well with the
6309 : scalarizer, which changes arrays' offset when the scalarization loops
6310 : are generated (see gfc_trans_preloop_setup). Fortunately, {min,max}val
6311 : are currently inlined in the scalar case only. As there is no dependency
6312 : to care about in that case, there is no temporary, so that we can use the
6313 : scalarizer temporary code to handle multiple loops. Thus, we set temp_dim
6314 : here, we call gfc_mark_ss_chain_used with flag=3 later, and we use
6315 : gfc_trans_scalarized_loop_boundary even later to restore offset.
6316 : TODO: this prevents inlining of rank > 0 minmaxval calls, so this
6317 : should eventually go away. We could either create two loops properly,
6318 : or find another way to save/restore the array offsets between the two
6319 : loops (without conflicting with temporary management), or use a single
6320 : loop minmaxval implementation. See PR 31067. */
6321 2231 : loop.temp_dim = loop.dimen;
6322 2231 : gfc_conv_loop_setup (&loop, &expr->where);
6323 :
6324 2231 : if (nonempty == NULL && maskss == NULL
6325 527 : && loop.dimen == 1 && loop.from[0] && loop.to[0])
6326 491 : nonempty = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
6327 : loop.from[0], loop.to[0]);
6328 2231 : nonempty_var = NULL;
6329 2231 : if (nonempty == NULL
6330 2231 : && (HONOR_INFINITIES (DECL_MODE (limit))
6331 480 : || HONOR_NANS (DECL_MODE (limit))))
6332 : {
6333 582 : nonempty_var = gfc_create_var (logical_type_node, "nonempty");
6334 582 : gfc_add_modify (&se->pre, nonempty_var, logical_false_node);
6335 582 : nonempty = nonempty_var;
6336 : }
6337 2231 : lab = NULL;
6338 2231 : fast = NULL;
6339 2231 : if (HONOR_NANS (DECL_MODE (limit)))
6340 : {
6341 1241 : if (loop.dimen == 1)
6342 : {
6343 821 : lab = gfc_build_label_decl (NULL_TREE);
6344 821 : TREE_USED (lab) = 1;
6345 : }
6346 : else
6347 : {
6348 420 : fast = gfc_create_var (logical_type_node, "fast");
6349 420 : gfc_add_modify (&se->pre, fast, logical_false_node);
6350 : }
6351 : }
6352 :
6353 2231 : gfc_mark_ss_chain_used (arrayss, lab ? 3 : 1);
6354 2231 : if (maskss)
6355 1704 : gfc_mark_ss_chain_used (maskss, lab ? 3 : 1);
6356 : /* Generate the loop body. */
6357 2231 : gfc_start_scalarized_body (&loop, &body);
6358 :
6359 : /* If we have a mask, only add this element if the mask is set. */
6360 2231 : if (maskss)
6361 : {
6362 1026 : gfc_init_se (&maskse, NULL);
6363 1026 : gfc_copy_loopinfo_to_se (&maskse, &loop);
6364 1026 : maskse.ss = maskss;
6365 1026 : gfc_conv_expr_val (&maskse, maskexpr);
6366 1026 : gfc_add_block_to_block (&body, &maskse.pre);
6367 :
6368 1026 : gfc_start_block (&block);
6369 : }
6370 : else
6371 1205 : gfc_init_block (&block);
6372 :
6373 : /* Compare with the current limit. */
6374 2231 : gfc_init_se (&arrayse, NULL);
6375 2231 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
6376 2231 : arrayse.ss = arrayss;
6377 2231 : gfc_conv_expr_val (&arrayse, arrayexpr);
6378 2231 : arrayse.expr = gfc_evaluate_now (arrayse.expr, &arrayse.pre);
6379 2231 : gfc_add_block_to_block (&block, &arrayse.pre);
6380 :
6381 2231 : gfc_init_block (&block2);
6382 :
6383 2231 : if (nonempty_var)
6384 582 : gfc_add_modify (&block2, nonempty_var, logical_true_node);
6385 :
6386 2231 : if (HONOR_NANS (DECL_MODE (limit)))
6387 : {
6388 1922 : tmp = fold_build2_loc (input_location, op == GT_EXPR ? GE_EXPR : LE_EXPR,
6389 : logical_type_node, arrayse.expr, limit);
6390 1241 : if (lab)
6391 : {
6392 821 : stmtblock_t ifblock;
6393 821 : tree inc_loop;
6394 821 : inc_loop = fold_build2_loc (input_location, PLUS_EXPR,
6395 821 : TREE_TYPE (loop.loopvar[0]),
6396 : loop.loopvar[0], gfc_index_one_node);
6397 821 : gfc_init_block (&ifblock);
6398 821 : gfc_add_modify (&ifblock, limit, arrayse.expr);
6399 821 : gfc_add_modify (&ifblock, loop.loopvar[0], inc_loop);
6400 821 : gfc_add_expr_to_block (&ifblock, build1_v (GOTO_EXPR, lab));
6401 821 : ifbody = gfc_finish_block (&ifblock);
6402 : }
6403 : else
6404 : {
6405 420 : stmtblock_t ifblock;
6406 :
6407 420 : gfc_init_block (&ifblock);
6408 420 : gfc_add_modify (&ifblock, limit, arrayse.expr);
6409 420 : gfc_add_modify (&ifblock, fast, logical_true_node);
6410 420 : ifbody = gfc_finish_block (&ifblock);
6411 : }
6412 1241 : tmp = build3_v (COND_EXPR, tmp, ifbody,
6413 : build_empty_stmt (input_location));
6414 1241 : gfc_add_expr_to_block (&block2, tmp);
6415 : }
6416 : else
6417 : {
6418 : /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
6419 : signed zeros. */
6420 1535 : tmp = fold_build2_loc (input_location,
6421 : op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
6422 : type, arrayse.expr, limit);
6423 990 : gfc_add_modify (&block2, limit, tmp);
6424 : }
6425 :
6426 2231 : if (fast)
6427 : {
6428 420 : tree elsebody = gfc_finish_block (&block2);
6429 :
6430 : /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
6431 : signed zeros. */
6432 420 : if (HONOR_NANS (DECL_MODE (limit)))
6433 : {
6434 420 : tmp = fold_build2_loc (input_location, op, logical_type_node,
6435 : arrayse.expr, limit);
6436 420 : ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
6437 420 : ifbody = build3_v (COND_EXPR, tmp, ifbody,
6438 : build_empty_stmt (input_location));
6439 : }
6440 : else
6441 : {
6442 0 : tmp = fold_build2_loc (input_location,
6443 : op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
6444 : type, arrayse.expr, limit);
6445 0 : ifbody = build2_v (MODIFY_EXPR, limit, tmp);
6446 : }
6447 420 : tmp = build3_v (COND_EXPR, fast, ifbody, elsebody);
6448 420 : gfc_add_expr_to_block (&block, tmp);
6449 : }
6450 : else
6451 1811 : gfc_add_block_to_block (&block, &block2);
6452 :
6453 2231 : gfc_add_block_to_block (&block, &arrayse.post);
6454 :
6455 2231 : tmp = gfc_finish_block (&block);
6456 2231 : if (maskss)
6457 : {
6458 : /* We enclose the above in if (mask) {...}. If the mask is an
6459 : optional argument, generate IF (.NOT. PRESENT(MASK)
6460 : .OR. MASK(I)). */
6461 1026 : tree ifmask;
6462 1026 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
6463 1026 : tmp = build3_v (COND_EXPR, ifmask, tmp,
6464 : build_empty_stmt (input_location));
6465 : }
6466 2231 : gfc_add_expr_to_block (&body, tmp);
6467 :
6468 2231 : if (lab)
6469 : {
6470 821 : gfc_trans_scalarized_loop_boundary (&loop, &body);
6471 :
6472 821 : tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
6473 : nan_cst, huge_cst);
6474 821 : gfc_add_modify (&loop.code[0], limit, tmp);
6475 821 : gfc_add_expr_to_block (&loop.code[0], build1_v (LABEL_EXPR, lab));
6476 :
6477 : /* If we have a mask, only add this element if the mask is set. */
6478 821 : if (maskss)
6479 : {
6480 348 : gfc_init_se (&maskse, NULL);
6481 348 : gfc_copy_loopinfo_to_se (&maskse, &loop);
6482 348 : maskse.ss = maskss;
6483 348 : gfc_conv_expr_val (&maskse, maskexpr);
6484 348 : gfc_add_block_to_block (&body, &maskse.pre);
6485 :
6486 348 : gfc_start_block (&block);
6487 : }
6488 : else
6489 473 : gfc_init_block (&block);
6490 :
6491 : /* Compare with the current limit. */
6492 821 : gfc_init_se (&arrayse, NULL);
6493 821 : gfc_copy_loopinfo_to_se (&arrayse, &loop);
6494 821 : arrayse.ss = arrayss;
6495 821 : gfc_conv_expr_val (&arrayse, arrayexpr);
6496 821 : arrayse.expr = gfc_evaluate_now (arrayse.expr, &arrayse.pre);
6497 821 : gfc_add_block_to_block (&block, &arrayse.pre);
6498 :
6499 : /* MIN_EXPR/MAX_EXPR has unspecified behavior with NaNs or
6500 : signed zeros. */
6501 821 : if (HONOR_NANS (DECL_MODE (limit)))
6502 : {
6503 821 : tmp = fold_build2_loc (input_location, op, logical_type_node,
6504 : arrayse.expr, limit);
6505 821 : ifbody = build2_v (MODIFY_EXPR, limit, arrayse.expr);
6506 821 : tmp = build3_v (COND_EXPR, tmp, ifbody,
6507 : build_empty_stmt (input_location));
6508 821 : gfc_add_expr_to_block (&block, tmp);
6509 : }
6510 : else
6511 : {
6512 0 : tmp = fold_build2_loc (input_location,
6513 : op == GT_EXPR ? MAX_EXPR : MIN_EXPR,
6514 : type, arrayse.expr, limit);
6515 0 : gfc_add_modify (&block, limit, tmp);
6516 : }
6517 :
6518 821 : gfc_add_block_to_block (&block, &arrayse.post);
6519 :
6520 821 : tmp = gfc_finish_block (&block);
6521 821 : if (maskss)
6522 : /* We enclose the above in if (mask) {...}. */
6523 : {
6524 348 : tree ifmask;
6525 348 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
6526 348 : tmp = build3_v (COND_EXPR, ifmask, tmp,
6527 : build_empty_stmt (input_location));
6528 : }
6529 :
6530 821 : gfc_add_expr_to_block (&body, tmp);
6531 : /* Avoid initializing loopvar[0] again, it should be left where
6532 : it finished by the first loop. */
6533 821 : loop.from[0] = loop.loopvar[0];
6534 : }
6535 2231 : gfc_trans_scalarizing_loops (&loop, &body);
6536 :
6537 2231 : if (fast)
6538 : {
6539 420 : tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty,
6540 : nan_cst, huge_cst);
6541 420 : ifbody = build2_v (MODIFY_EXPR, limit, tmp);
6542 420 : tmp = build3_v (COND_EXPR, fast, build_empty_stmt (input_location),
6543 : ifbody);
6544 420 : gfc_add_expr_to_block (&loop.pre, tmp);
6545 : }
6546 1811 : else if (HONOR_INFINITIES (DECL_MODE (limit)) && !lab)
6547 : {
6548 0 : tmp = fold_build3_loc (input_location, COND_EXPR, type, nonempty, limit,
6549 : huge_cst);
6550 0 : gfc_add_modify (&loop.pre, limit, tmp);
6551 : }
6552 :
6553 : /* For a scalar mask, enclose the loop in an if statement. */
6554 2231 : if (maskexpr && maskss == NULL)
6555 : {
6556 546 : tree else_stmt;
6557 546 : tree ifmask;
6558 :
6559 546 : gfc_init_se (&maskse, NULL);
6560 546 : gfc_conv_expr_val (&maskse, maskexpr);
6561 546 : gfc_init_block (&block);
6562 546 : gfc_add_block_to_block (&block, &loop.pre);
6563 546 : gfc_add_block_to_block (&block, &loop.post);
6564 546 : tmp = gfc_finish_block (&block);
6565 :
6566 546 : if (HONOR_INFINITIES (DECL_MODE (limit)))
6567 354 : else_stmt = build2_v (MODIFY_EXPR, limit, huge_cst);
6568 : else
6569 192 : else_stmt = build_empty_stmt (input_location);
6570 :
6571 546 : ifmask = conv_mask_condition (&maskse, maskexpr, optional_mask);
6572 546 : tmp = build3_v (COND_EXPR, ifmask, tmp, else_stmt);
6573 546 : gfc_add_expr_to_block (&block, tmp);
6574 546 : gfc_add_block_to_block (&se->pre, &block);
6575 : }
6576 : else
6577 : {
6578 1685 : gfc_add_block_to_block (&se->pre, &loop.pre);
6579 1685 : gfc_add_block_to_block (&se->pre, &loop.post);
6580 : }
6581 :
6582 2231 : gfc_cleanup_loop (&loop);
6583 :
6584 2231 : se->expr = limit;
6585 : }
6586 :
6587 : /* BTEST (i, pos) = (i & (1 << pos)) != 0. */
6588 : static void
6589 145 : gfc_conv_intrinsic_btest (gfc_se * se, gfc_expr * expr)
6590 : {
6591 145 : tree args[2];
6592 145 : tree type;
6593 145 : tree tmp;
6594 :
6595 145 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6596 145 : type = TREE_TYPE (args[0]);
6597 :
6598 : /* Optionally generate code for runtime argument check. */
6599 145 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6600 : {
6601 6 : tree below = fold_build2_loc (input_location, LT_EXPR,
6602 : logical_type_node, args[1],
6603 6 : build_int_cst (TREE_TYPE (args[1]), 0));
6604 6 : tree nbits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6605 6 : tree above = fold_build2_loc (input_location, GE_EXPR,
6606 : logical_type_node, args[1], nbits);
6607 6 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6608 : logical_type_node, below, above);
6609 6 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6610 : "POS argument (%ld) out of range 0:%ld "
6611 : "in intrinsic BTEST",
6612 : fold_convert (long_integer_type_node, args[1]),
6613 : fold_convert (long_integer_type_node, nbits));
6614 : }
6615 :
6616 145 : tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
6617 : build_int_cst (type, 1), args[1]);
6618 145 : tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], tmp);
6619 145 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
6620 : build_int_cst (type, 0));
6621 145 : type = gfc_typenode_for_spec (&expr->ts);
6622 145 : se->expr = convert (type, tmp);
6623 145 : }
6624 :
6625 :
6626 : /* Generate code for BGE, BGT, BLE and BLT intrinsics. */
6627 : static void
6628 216 : gfc_conv_intrinsic_bitcomp (gfc_se * se, gfc_expr * expr, enum tree_code op)
6629 : {
6630 216 : tree args[2];
6631 :
6632 216 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6633 :
6634 : /* Convert both arguments to the unsigned type of the same size. */
6635 216 : args[0] = fold_convert (unsigned_type_for (TREE_TYPE (args[0])), args[0]);
6636 216 : args[1] = fold_convert (unsigned_type_for (TREE_TYPE (args[1])), args[1]);
6637 :
6638 : /* If they have unequal type size, convert to the larger one. */
6639 216 : if (TYPE_PRECISION (TREE_TYPE (args[0]))
6640 216 : > TYPE_PRECISION (TREE_TYPE (args[1])))
6641 0 : args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
6642 216 : else if (TYPE_PRECISION (TREE_TYPE (args[1]))
6643 216 : > TYPE_PRECISION (TREE_TYPE (args[0])))
6644 0 : args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
6645 :
6646 : /* Now, we compare them. */
6647 216 : se->expr = fold_build2_loc (input_location, op, logical_type_node,
6648 : args[0], args[1]);
6649 216 : }
6650 :
6651 :
6652 : /* Generate code to perform the specified operation. */
6653 : static void
6654 1915 : gfc_conv_intrinsic_bitop (gfc_se * se, gfc_expr * expr, enum tree_code op)
6655 : {
6656 1915 : tree args[2];
6657 :
6658 1915 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6659 1915 : se->expr = fold_build2_loc (input_location, op, TREE_TYPE (args[0]),
6660 : args[0], args[1]);
6661 1915 : }
6662 :
6663 : /* Bitwise not. */
6664 : static void
6665 230 : gfc_conv_intrinsic_not (gfc_se * se, gfc_expr * expr)
6666 : {
6667 230 : tree arg;
6668 :
6669 230 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
6670 230 : se->expr = fold_build1_loc (input_location, BIT_NOT_EXPR,
6671 230 : TREE_TYPE (arg), arg);
6672 230 : }
6673 :
6674 :
6675 : /* Generate code for OUT_OF_RANGE. */
6676 : static void
6677 468 : gfc_conv_intrinsic_out_of_range (gfc_se * se, gfc_expr * expr)
6678 : {
6679 468 : tree *args;
6680 468 : tree type;
6681 468 : tree tmp = NULL_TREE, tmp1, tmp2;
6682 468 : unsigned int num_args;
6683 468 : int k;
6684 468 : gfc_se rnd_se;
6685 468 : gfc_actual_arglist *arg = expr->value.function.actual;
6686 468 : gfc_expr *x = arg->expr;
6687 468 : gfc_expr *mold = arg->next->expr;
6688 :
6689 468 : num_args = gfc_intrinsic_argument_list_length (expr);
6690 468 : args = XALLOCAVEC (tree, num_args);
6691 :
6692 468 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
6693 :
6694 468 : gfc_init_se (&rnd_se, NULL);
6695 :
6696 468 : if (num_args == 3)
6697 : {
6698 : /* The ROUND argument is optional and shall appear only if X is
6699 : of type real and MOLD is of type integer (see edit F23/004). */
6700 270 : gfc_expr *round = arg->next->next->expr;
6701 270 : gfc_conv_expr (&rnd_se, round);
6702 :
6703 270 : if (round->expr_type == EXPR_VARIABLE
6704 198 : && round->symtree->n.sym->attr.dummy
6705 30 : && round->symtree->n.sym->attr.optional)
6706 : {
6707 30 : tree present = gfc_conv_expr_present (round->symtree->n.sym);
6708 30 : rnd_se.expr = build3_loc (input_location, COND_EXPR,
6709 : logical_type_node, present,
6710 : rnd_se.expr, logical_false_node);
6711 30 : gfc_add_block_to_block (&se->pre, &rnd_se.pre);
6712 : }
6713 : }
6714 : else
6715 : {
6716 : /* If ROUND is absent, it is equivalent to having the value false. */
6717 198 : rnd_se.expr = logical_false_node;
6718 : }
6719 :
6720 468 : type = TREE_TYPE (args[0]);
6721 468 : k = gfc_validate_kind (mold->ts.type, mold->ts.kind, false);
6722 :
6723 468 : switch (x->ts.type)
6724 : {
6725 378 : case BT_REAL:
6726 : /* X may be IEEE infinity or NaN, but the representation of MOLD may not
6727 : support infinity or NaN. */
6728 378 : tree finite;
6729 378 : finite = build_call_expr_loc (input_location,
6730 : builtin_decl_explicit (BUILT_IN_ISFINITE),
6731 : 1, args[0]);
6732 378 : finite = convert (logical_type_node, finite);
6733 :
6734 378 : if (mold->ts.type == BT_REAL)
6735 : {
6736 24 : tmp1 = build1 (ABS_EXPR, type, args[0]);
6737 24 : tmp2 = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
6738 : mold->ts.kind, 0);
6739 24 : tmp = build2 (GT_EXPR, logical_type_node, tmp1,
6740 : convert (type, tmp2));
6741 :
6742 : /* Check if MOLD representation supports infinity or NaN. */
6743 24 : bool infnan = (HONOR_INFINITIES (TREE_TYPE (args[1]))
6744 24 : || HONOR_NANS (TREE_TYPE (args[1])));
6745 24 : tmp = build3 (COND_EXPR, logical_type_node, finite, tmp,
6746 : infnan ? logical_false_node : logical_true_node);
6747 : }
6748 : else
6749 : {
6750 354 : tree rounded;
6751 354 : tree decl;
6752 :
6753 354 : decl = gfc_builtin_decl_for_float_kind (BUILT_IN_TRUNC, x->ts.kind);
6754 354 : gcc_assert (decl != NULL_TREE);
6755 :
6756 : /* Round or truncate argument X, depending on the optional argument
6757 : ROUND (default: .false.). */
6758 354 : tmp1 = build_round_expr (args[0], type);
6759 354 : tmp2 = build_call_expr_loc (input_location, decl, 1, args[0]);
6760 354 : rounded = build3 (COND_EXPR, type, rnd_se.expr, tmp1, tmp2);
6761 :
6762 354 : if (mold->ts.type == BT_INTEGER)
6763 : {
6764 180 : tmp1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].min_int,
6765 : x->ts.kind);
6766 180 : tmp2 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
6767 : x->ts.kind);
6768 : }
6769 174 : else if (mold->ts.type == BT_UNSIGNED)
6770 : {
6771 174 : tmp1 = build_real_from_int_cst (type, integer_zero_node);
6772 174 : tmp2 = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
6773 : x->ts.kind);
6774 : }
6775 : else
6776 0 : gcc_unreachable ();
6777 :
6778 354 : tmp1 = build2 (LT_EXPR, logical_type_node, rounded,
6779 : convert (type, tmp1));
6780 354 : tmp2 = build2 (GT_EXPR, logical_type_node, rounded,
6781 : convert (type, tmp2));
6782 354 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
6783 354 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node,
6784 : build1 (TRUTH_NOT_EXPR, logical_type_node, finite),
6785 : tmp);
6786 : }
6787 : break;
6788 :
6789 48 : case BT_INTEGER:
6790 48 : if (mold->ts.type == BT_INTEGER)
6791 : {
6792 12 : tmp1 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].min_int,
6793 : x->ts.kind);
6794 12 : tmp2 = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
6795 : x->ts.kind);
6796 12 : tmp1 = build2 (LT_EXPR, logical_type_node, args[0],
6797 : convert (type, tmp1));
6798 12 : tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
6799 : convert (type, tmp2));
6800 12 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
6801 : }
6802 36 : else if (mold->ts.type == BT_UNSIGNED)
6803 : {
6804 36 : int i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
6805 36 : tmp = build_int_cst (type, 0);
6806 36 : tmp = build2 (LT_EXPR, logical_type_node, args[0], tmp);
6807 36 : if (mpz_cmp (gfc_integer_kinds[i].huge,
6808 36 : gfc_unsigned_kinds[k].huge) > 0)
6809 : {
6810 0 : tmp2 = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
6811 : x->ts.kind);
6812 0 : tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
6813 : convert (type, tmp2));
6814 0 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp, tmp2);
6815 : }
6816 : }
6817 0 : else if (mold->ts.type == BT_REAL)
6818 : {
6819 0 : tmp2 = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
6820 : mold->ts.kind, 0);
6821 0 : tmp1 = build1 (NEGATE_EXPR, TREE_TYPE (tmp2), tmp2);
6822 0 : tmp1 = build2 (LT_EXPR, logical_type_node, args[0],
6823 : convert (type, tmp1));
6824 0 : tmp2 = build2 (GT_EXPR, logical_type_node, args[0],
6825 : convert (type, tmp2));
6826 0 : tmp = build2 (TRUTH_ORIF_EXPR, logical_type_node, tmp1, tmp2);
6827 : }
6828 : else
6829 0 : gcc_unreachable ();
6830 : break;
6831 :
6832 42 : case BT_UNSIGNED:
6833 42 : if (mold->ts.type == BT_UNSIGNED)
6834 : {
6835 12 : tmp = gfc_conv_mpz_to_tree (gfc_unsigned_kinds[k].huge,
6836 : x->ts.kind);
6837 12 : tmp = build2 (GT_EXPR, logical_type_node, args[0],
6838 : convert (type, tmp));
6839 : }
6840 30 : else if (mold->ts.type == BT_INTEGER)
6841 : {
6842 18 : tmp = gfc_conv_mpz_to_tree (gfc_integer_kinds[k].huge,
6843 : x->ts.kind);
6844 18 : tmp = build2 (GT_EXPR, logical_type_node, args[0],
6845 : convert (type, tmp));
6846 : }
6847 12 : else if (mold->ts.type == BT_REAL)
6848 : {
6849 12 : tmp = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge,
6850 : mold->ts.kind, 0);
6851 12 : tmp = build2 (GT_EXPR, logical_type_node, args[0],
6852 : convert (type, tmp));
6853 : }
6854 : else
6855 0 : gcc_unreachable ();
6856 : break;
6857 :
6858 0 : default:
6859 0 : gcc_unreachable ();
6860 : }
6861 :
6862 468 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
6863 468 : }
6864 :
6865 :
6866 : /* Set or clear a single bit. */
6867 : static void
6868 306 : gfc_conv_intrinsic_singlebitop (gfc_se * se, gfc_expr * expr, int set)
6869 : {
6870 306 : tree args[2];
6871 306 : tree type;
6872 306 : tree tmp;
6873 306 : enum tree_code op;
6874 :
6875 306 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6876 306 : type = TREE_TYPE (args[0]);
6877 :
6878 : /* Optionally generate code for runtime argument check. */
6879 306 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6880 : {
6881 12 : tree below = fold_build2_loc (input_location, LT_EXPR,
6882 : logical_type_node, args[1],
6883 12 : build_int_cst (TREE_TYPE (args[1]), 0));
6884 12 : tree nbits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
6885 12 : tree above = fold_build2_loc (input_location, GE_EXPR,
6886 : logical_type_node, args[1], nbits);
6887 12 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6888 : logical_type_node, below, above);
6889 12 : size_t len_name = strlen (expr->value.function.isym->name);
6890 12 : char *name = XALLOCAVEC (char, len_name + 1);
6891 72 : for (size_t i = 0; i < len_name; i++)
6892 60 : name[i] = TOUPPER (expr->value.function.isym->name[i]);
6893 12 : name[len_name] = '\0';
6894 12 : tree iname = gfc_build_addr_expr (pchar_type_node,
6895 : gfc_build_cstring_const (name));
6896 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6897 : "POS argument (%ld) out of range 0:%ld "
6898 : "in intrinsic %s",
6899 : fold_convert (long_integer_type_node, args[1]),
6900 : fold_convert (long_integer_type_node, nbits),
6901 : iname);
6902 : }
6903 :
6904 306 : tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
6905 : build_int_cst (type, 1), args[1]);
6906 306 : if (set)
6907 : op = BIT_IOR_EXPR;
6908 : else
6909 : {
6910 168 : op = BIT_AND_EXPR;
6911 168 : tmp = fold_build1_loc (input_location, BIT_NOT_EXPR, type, tmp);
6912 : }
6913 306 : se->expr = fold_build2_loc (input_location, op, type, args[0], tmp);
6914 306 : }
6915 :
6916 : /* Extract a sequence of bits.
6917 : IBITS(I, POS, LEN) = (I >> POS) & ~((~0) << LEN). */
6918 : static void
6919 27 : gfc_conv_intrinsic_ibits (gfc_se * se, gfc_expr * expr)
6920 : {
6921 27 : tree args[3];
6922 27 : tree type;
6923 27 : tree tmp;
6924 27 : tree mask;
6925 27 : tree num_bits, cond;
6926 :
6927 27 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
6928 27 : type = TREE_TYPE (args[0]);
6929 :
6930 : /* Optionally generate code for runtime argument check. */
6931 27 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
6932 : {
6933 12 : tree tmp1 = fold_convert (long_integer_type_node, args[1]);
6934 12 : tree tmp2 = fold_convert (long_integer_type_node, args[2]);
6935 12 : tree nbits = build_int_cst (long_integer_type_node,
6936 12 : TYPE_PRECISION (type));
6937 12 : tree below = fold_build2_loc (input_location, LT_EXPR,
6938 : logical_type_node, args[1],
6939 12 : build_int_cst (TREE_TYPE (args[1]), 0));
6940 12 : tree above = fold_build2_loc (input_location, GT_EXPR,
6941 : logical_type_node, tmp1, nbits);
6942 12 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6943 : logical_type_node, below, above);
6944 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6945 : "POS argument (%ld) out of range 0:%ld "
6946 : "in intrinsic IBITS", tmp1, nbits);
6947 12 : below = fold_build2_loc (input_location, LT_EXPR,
6948 : logical_type_node, args[2],
6949 12 : build_int_cst (TREE_TYPE (args[2]), 0));
6950 12 : above = fold_build2_loc (input_location, GT_EXPR,
6951 : logical_type_node, tmp2, nbits);
6952 12 : scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6953 : logical_type_node, below, above);
6954 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6955 : "LEN argument (%ld) out of range 0:%ld "
6956 : "in intrinsic IBITS", tmp2, nbits);
6957 12 : above = fold_build2_loc (input_location, PLUS_EXPR,
6958 : long_integer_type_node, tmp1, tmp2);
6959 12 : scond = fold_build2_loc (input_location, GT_EXPR,
6960 : logical_type_node, above, nbits);
6961 12 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
6962 : "POS(%ld)+LEN(%ld)>BIT_SIZE(%ld) "
6963 : "in intrinsic IBITS", tmp1, tmp2, nbits);
6964 : }
6965 :
6966 : /* The Fortran standard allows (shift width) LEN <= BIT_SIZE(I), whereas
6967 : gcc requires a shift width < BIT_SIZE(I), so we have to catch this
6968 : special case. See also gfc_conv_intrinsic_ishft (). */
6969 27 : num_bits = build_int_cst (TREE_TYPE (args[2]), TYPE_PRECISION (type));
6970 :
6971 27 : mask = build_int_cst (type, -1);
6972 27 : mask = fold_build2_loc (input_location, LSHIFT_EXPR, type, mask, args[2]);
6973 27 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, args[2],
6974 : num_bits);
6975 27 : mask = fold_build3_loc (input_location, COND_EXPR, type, cond,
6976 : build_int_cst (type, 0), mask);
6977 27 : mask = fold_build1_loc (input_location, BIT_NOT_EXPR, type, mask);
6978 :
6979 27 : tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, args[0], args[1]);
6980 :
6981 27 : se->expr = fold_build2_loc (input_location, BIT_AND_EXPR, type, tmp, mask);
6982 27 : }
6983 :
6984 : static void
6985 492 : gfc_conv_intrinsic_shift (gfc_se * se, gfc_expr * expr, bool right_shift,
6986 : bool arithmetic)
6987 : {
6988 492 : tree args[2], type, num_bits, cond;
6989 492 : tree bigshift;
6990 492 : bool do_convert = false;
6991 :
6992 492 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
6993 :
6994 492 : args[0] = gfc_evaluate_now (args[0], &se->pre);
6995 492 : args[1] = gfc_evaluate_now (args[1], &se->pre);
6996 492 : type = TREE_TYPE (args[0]);
6997 :
6998 492 : if (!arithmetic)
6999 : {
7000 390 : args[0] = fold_convert (unsigned_type_for (type), args[0]);
7001 390 : do_convert = true;
7002 : }
7003 : else
7004 102 : gcc_assert (right_shift);
7005 :
7006 492 : if (flag_unsigned && arithmetic && expr->ts.type == BT_UNSIGNED)
7007 : {
7008 30 : do_convert = true;
7009 30 : args[0] = fold_convert (signed_type_for (type), args[0]);
7010 : }
7011 :
7012 816 : se->expr = fold_build2_loc (input_location,
7013 : right_shift ? RSHIFT_EXPR : LSHIFT_EXPR,
7014 492 : TREE_TYPE (args[0]), args[0], args[1]);
7015 :
7016 492 : if (do_convert)
7017 420 : se->expr = fold_convert (type, se->expr);
7018 :
7019 492 : if (!arithmetic)
7020 390 : bigshift = build_int_cst (type, 0);
7021 : else
7022 : {
7023 102 : tree nonneg = fold_build2_loc (input_location, GE_EXPR,
7024 : logical_type_node, args[0],
7025 102 : build_int_cst (TREE_TYPE (args[0]), 0));
7026 102 : bigshift = fold_build3_loc (input_location, COND_EXPR, type, nonneg,
7027 : build_int_cst (type, 0),
7028 : build_int_cst (type, -1));
7029 : }
7030 :
7031 : /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
7032 : gcc requires a shift width < BIT_SIZE(I), so we have to catch this
7033 : special case. */
7034 492 : num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
7035 :
7036 : /* Optionally generate code for runtime argument check. */
7037 492 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
7038 : {
7039 30 : tree below = fold_build2_loc (input_location, LT_EXPR,
7040 : logical_type_node, args[1],
7041 30 : build_int_cst (TREE_TYPE (args[1]), 0));
7042 30 : tree above = fold_build2_loc (input_location, GT_EXPR,
7043 : logical_type_node, args[1], num_bits);
7044 30 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
7045 : logical_type_node, below, above);
7046 30 : size_t len_name = strlen (expr->value.function.isym->name);
7047 30 : char *name = XALLOCAVEC (char, len_name + 1);
7048 210 : for (size_t i = 0; i < len_name; i++)
7049 180 : name[i] = TOUPPER (expr->value.function.isym->name[i]);
7050 30 : name[len_name] = '\0';
7051 30 : tree iname = gfc_build_addr_expr (pchar_type_node,
7052 : gfc_build_cstring_const (name));
7053 30 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
7054 : "SHIFT argument (%ld) out of range 0:%ld "
7055 : "in intrinsic %s",
7056 : fold_convert (long_integer_type_node, args[1]),
7057 : fold_convert (long_integer_type_node, num_bits),
7058 : iname);
7059 : }
7060 :
7061 492 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
7062 : args[1], num_bits);
7063 :
7064 492 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
7065 : bigshift, se->expr);
7066 492 : }
7067 :
7068 : /* ISHFT (I, SHIFT) = (abs (shift) >= BIT_SIZE (i))
7069 : ? 0
7070 : : ((shift >= 0) ? i << shift : i >> -shift)
7071 : where all shifts are logical shifts. */
7072 : static void
7073 318 : gfc_conv_intrinsic_ishft (gfc_se * se, gfc_expr * expr)
7074 : {
7075 318 : tree args[2];
7076 318 : tree type;
7077 318 : tree utype;
7078 318 : tree tmp;
7079 318 : tree width;
7080 318 : tree num_bits;
7081 318 : tree cond;
7082 318 : tree lshift;
7083 318 : tree rshift;
7084 :
7085 318 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
7086 :
7087 318 : args[0] = gfc_evaluate_now (args[0], &se->pre);
7088 318 : args[1] = gfc_evaluate_now (args[1], &se->pre);
7089 :
7090 318 : type = TREE_TYPE (args[0]);
7091 318 : utype = unsigned_type_for (type);
7092 :
7093 318 : width = fold_build1_loc (input_location, ABS_EXPR, TREE_TYPE (args[1]),
7094 : args[1]);
7095 :
7096 : /* Left shift if positive. */
7097 318 : lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type, args[0], width);
7098 :
7099 : /* Right shift if negative.
7100 : We convert to an unsigned type because we want a logical shift.
7101 : The standard doesn't define the case of shifting negative
7102 : numbers, and we try to be compatible with other compilers, most
7103 : notably g77, here. */
7104 318 : rshift = fold_convert (type, fold_build2_loc (input_location, RSHIFT_EXPR,
7105 : utype, convert (utype, args[0]), width));
7106 :
7107 318 : tmp = fold_build2_loc (input_location, GE_EXPR, logical_type_node, args[1],
7108 318 : build_int_cst (TREE_TYPE (args[1]), 0));
7109 318 : tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp, lshift, rshift);
7110 :
7111 : /* The Fortran standard allows shift widths <= BIT_SIZE(I), whereas
7112 : gcc requires a shift width < BIT_SIZE(I), so we have to catch this
7113 : special case. */
7114 318 : num_bits = build_int_cst (TREE_TYPE (args[1]), TYPE_PRECISION (type));
7115 :
7116 : /* Optionally generate code for runtime argument check. */
7117 318 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
7118 : {
7119 24 : tree outside = fold_build2_loc (input_location, GT_EXPR,
7120 : logical_type_node, width, num_bits);
7121 24 : gfc_trans_runtime_check (true, false, outside, &se->pre, &expr->where,
7122 : "SHIFT argument (%ld) out of range -%ld:%ld "
7123 : "in intrinsic ISHFT",
7124 : fold_convert (long_integer_type_node, args[1]),
7125 : fold_convert (long_integer_type_node, num_bits),
7126 : fold_convert (long_integer_type_node, num_bits));
7127 : }
7128 :
7129 318 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, width,
7130 : num_bits);
7131 318 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
7132 : build_int_cst (type, 0), tmp);
7133 318 : }
7134 :
7135 :
7136 : /* Circular shift. AKA rotate or barrel shift. */
7137 :
7138 : static void
7139 658 : gfc_conv_intrinsic_ishftc (gfc_se * se, gfc_expr * expr)
7140 : {
7141 658 : tree *args;
7142 658 : tree type;
7143 658 : tree tmp;
7144 658 : tree lrot;
7145 658 : tree rrot;
7146 658 : tree zero;
7147 658 : tree nbits;
7148 658 : unsigned int num_args;
7149 :
7150 658 : num_args = gfc_intrinsic_argument_list_length (expr);
7151 658 : args = XALLOCAVEC (tree, num_args);
7152 :
7153 658 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
7154 :
7155 658 : type = TREE_TYPE (args[0]);
7156 658 : nbits = build_int_cst (long_integer_type_node, TYPE_PRECISION (type));
7157 :
7158 658 : if (num_args == 3)
7159 : {
7160 550 : gfc_expr *size = expr->value.function.actual->next->next->expr;
7161 :
7162 : /* Use a library function for the 3 parameter version. */
7163 550 : tree int4type = gfc_get_int_type (4);
7164 :
7165 : /* Treat optional SIZE argument when it is passed as an optional
7166 : dummy. If SIZE is absent, the default value is BIT_SIZE(I). */
7167 550 : if (size->expr_type == EXPR_VARIABLE
7168 438 : && size->symtree->n.sym->attr.dummy
7169 36 : && size->symtree->n.sym->attr.optional)
7170 : {
7171 36 : tree type_of_size = TREE_TYPE (args[2]);
7172 72 : args[2] = build3_loc (input_location, COND_EXPR, type_of_size,
7173 36 : gfc_conv_expr_present (size->symtree->n.sym),
7174 : args[2], fold_convert (type_of_size, nbits));
7175 : }
7176 :
7177 : /* We convert the first argument to at least 4 bytes, and
7178 : convert back afterwards. This removes the need for library
7179 : functions for all argument sizes, and function will be
7180 : aligned to at least 32 bits, so there's no loss. */
7181 550 : if (expr->ts.kind < 4)
7182 242 : args[0] = convert (int4type, args[0]);
7183 :
7184 : /* Convert the SHIFT and SIZE args to INTEGER*4 otherwise we would
7185 : need loads of library functions. They cannot have values >
7186 : BIT_SIZE (I) so the conversion is safe. */
7187 550 : args[1] = convert (int4type, args[1]);
7188 550 : args[2] = convert (int4type, args[2]);
7189 :
7190 : /* Optionally generate code for runtime argument check. */
7191 550 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
7192 : {
7193 18 : tree size = fold_convert (long_integer_type_node, args[2]);
7194 18 : tree below = fold_build2_loc (input_location, LE_EXPR,
7195 : logical_type_node, size,
7196 18 : build_int_cst (TREE_TYPE (args[1]), 0));
7197 18 : tree above = fold_build2_loc (input_location, GT_EXPR,
7198 : logical_type_node, size, nbits);
7199 18 : tree scond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
7200 : logical_type_node, below, above);
7201 18 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
7202 : "SIZE argument (%ld) out of range 1:%ld "
7203 : "in intrinsic ISHFTC", size, nbits);
7204 18 : tree width = fold_convert (long_integer_type_node, args[1]);
7205 18 : width = fold_build1_loc (input_location, ABS_EXPR,
7206 : long_integer_type_node, width);
7207 18 : scond = fold_build2_loc (input_location, GT_EXPR,
7208 : logical_type_node, width, size);
7209 18 : gfc_trans_runtime_check (true, false, scond, &se->pre, &expr->where,
7210 : "SHIFT argument (%ld) out of range -%ld:%ld "
7211 : "in intrinsic ISHFTC",
7212 : fold_convert (long_integer_type_node, args[1]),
7213 : size, size);
7214 : }
7215 :
7216 550 : switch (expr->ts.kind)
7217 : {
7218 426 : case 1:
7219 426 : case 2:
7220 426 : case 4:
7221 426 : tmp = gfor_fndecl_math_ishftc4;
7222 426 : break;
7223 124 : case 8:
7224 124 : tmp = gfor_fndecl_math_ishftc8;
7225 124 : break;
7226 0 : case 16:
7227 0 : tmp = gfor_fndecl_math_ishftc16;
7228 0 : break;
7229 0 : default:
7230 0 : gcc_unreachable ();
7231 : }
7232 550 : se->expr = build_call_expr_loc (input_location,
7233 : tmp, 3, args[0], args[1], args[2]);
7234 : /* Convert the result back to the original type, if we extended
7235 : the first argument's width above. */
7236 550 : if (expr->ts.kind < 4)
7237 242 : se->expr = convert (type, se->expr);
7238 :
7239 : return;
7240 : }
7241 :
7242 : /* Evaluate arguments only once. */
7243 108 : args[0] = gfc_evaluate_now (args[0], &se->pre);
7244 108 : args[1] = gfc_evaluate_now (args[1], &se->pre);
7245 :
7246 : /* Optionally generate code for runtime argument check. */
7247 108 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
7248 : {
7249 12 : tree width = fold_convert (long_integer_type_node, args[1]);
7250 12 : width = fold_build1_loc (input_location, ABS_EXPR,
7251 : long_integer_type_node, width);
7252 12 : tree outside = fold_build2_loc (input_location, GT_EXPR,
7253 : logical_type_node, width, nbits);
7254 12 : gfc_trans_runtime_check (true, false, outside, &se->pre, &expr->where,
7255 : "SHIFT argument (%ld) out of range -%ld:%ld "
7256 : "in intrinsic ISHFTC",
7257 : fold_convert (long_integer_type_node, args[1]),
7258 : nbits, nbits);
7259 : }
7260 :
7261 : /* Rotate left if positive. */
7262 108 : lrot = fold_build2_loc (input_location, LROTATE_EXPR, type, args[0], args[1]);
7263 :
7264 : /* Rotate right if negative. */
7265 108 : tmp = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (args[1]),
7266 : args[1]);
7267 108 : rrot = fold_build2_loc (input_location,RROTATE_EXPR, type, args[0], tmp);
7268 :
7269 108 : zero = build_int_cst (TREE_TYPE (args[1]), 0);
7270 108 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node, args[1],
7271 : zero);
7272 108 : rrot = fold_build3_loc (input_location, COND_EXPR, type, tmp, lrot, rrot);
7273 :
7274 : /* Do nothing if shift == 0. */
7275 108 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, args[1],
7276 : zero);
7277 108 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, tmp, args[0],
7278 : rrot);
7279 : }
7280 :
7281 :
7282 : /* LEADZ (i) = (i == 0) ? BIT_SIZE (i)
7283 : : __builtin_clz(i) - (BIT_SIZE('int') - BIT_SIZE(i))
7284 :
7285 : The conditional expression is necessary because the result of LEADZ(0)
7286 : is defined, but the result of __builtin_clz(0) is undefined for most
7287 : targets.
7288 :
7289 : For INTEGER kinds smaller than the C 'int' type, we have to subtract the
7290 : difference in bit size between the argument of LEADZ and the C int. */
7291 :
7292 : static void
7293 270 : gfc_conv_intrinsic_leadz (gfc_se * se, gfc_expr * expr)
7294 : {
7295 270 : tree arg;
7296 270 : tree arg_type;
7297 270 : tree cond;
7298 270 : tree result_type;
7299 270 : tree leadz;
7300 270 : tree bit_size;
7301 270 : tree tmp;
7302 270 : tree func;
7303 270 : int s, argsize;
7304 :
7305 270 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7306 270 : argsize = TYPE_PRECISION (TREE_TYPE (arg));
7307 :
7308 : /* Which variant of __builtin_clz* should we call? */
7309 270 : if (argsize <= INT_TYPE_SIZE)
7310 : {
7311 183 : arg_type = unsigned_type_node;
7312 183 : func = builtin_decl_explicit (BUILT_IN_CLZ);
7313 : }
7314 87 : else if (argsize <= LONG_TYPE_SIZE)
7315 : {
7316 57 : arg_type = long_unsigned_type_node;
7317 57 : func = builtin_decl_explicit (BUILT_IN_CLZL);
7318 : }
7319 30 : else if (argsize <= LONG_LONG_TYPE_SIZE)
7320 : {
7321 0 : arg_type = long_long_unsigned_type_node;
7322 0 : func = builtin_decl_explicit (BUILT_IN_CLZLL);
7323 : }
7324 : else
7325 : {
7326 30 : gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
7327 30 : arg_type = gfc_build_uint_type (argsize);
7328 30 : func = NULL_TREE;
7329 : }
7330 :
7331 : /* Convert the actual argument twice: first, to the unsigned type of the
7332 : same size; then, to the proper argument type for the built-in
7333 : function. But the return type is of the default INTEGER kind. */
7334 270 : arg = fold_convert (gfc_build_uint_type (argsize), arg);
7335 270 : arg = fold_convert (arg_type, arg);
7336 270 : arg = gfc_evaluate_now (arg, &se->pre);
7337 270 : result_type = gfc_get_int_type (gfc_default_integer_kind);
7338 :
7339 : /* Compute LEADZ for the case i .ne. 0. */
7340 270 : if (func)
7341 : {
7342 240 : s = TYPE_PRECISION (arg_type) - argsize;
7343 240 : tmp = fold_convert (result_type,
7344 : build_call_expr_loc (input_location, func,
7345 : 1, arg));
7346 240 : leadz = fold_build2_loc (input_location, MINUS_EXPR, result_type,
7347 240 : tmp, build_int_cst (result_type, s));
7348 : }
7349 : else
7350 : {
7351 : /* We end up here if the argument type is larger than 'long long'.
7352 : We generate this code:
7353 :
7354 : if (x & (ULL_MAX << ULL_SIZE) != 0)
7355 : return clzll ((unsigned long long) (x >> ULLSIZE));
7356 : else
7357 : return ULL_SIZE + clzll ((unsigned long long) x);
7358 : where ULL_MAX is the largest value that a ULL_MAX can hold
7359 : (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
7360 : is the bit-size of the long long type (64 in this example). */
7361 30 : tree ullsize, ullmax, tmp1, tmp2, btmp;
7362 :
7363 30 : ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
7364 30 : ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
7365 : long_long_unsigned_type_node,
7366 : build_int_cst (long_long_unsigned_type_node,
7367 : 0));
7368 :
7369 30 : cond = fold_build2_loc (input_location, LSHIFT_EXPR, arg_type,
7370 : fold_convert (arg_type, ullmax), ullsize);
7371 30 : cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type,
7372 : arg, cond);
7373 30 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
7374 : cond, build_int_cst (arg_type, 0));
7375 :
7376 30 : tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
7377 : arg, ullsize);
7378 30 : tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
7379 30 : btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
7380 30 : tmp1 = fold_convert (result_type,
7381 : build_call_expr_loc (input_location, btmp, 1, tmp1));
7382 :
7383 30 : tmp2 = fold_convert (long_long_unsigned_type_node, arg);
7384 30 : btmp = builtin_decl_explicit (BUILT_IN_CLZLL);
7385 30 : tmp2 = fold_convert (result_type,
7386 : build_call_expr_loc (input_location, btmp, 1, tmp2));
7387 30 : tmp2 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
7388 : tmp2, ullsize);
7389 :
7390 30 : leadz = fold_build3_loc (input_location, COND_EXPR, result_type,
7391 : cond, tmp1, tmp2);
7392 : }
7393 :
7394 : /* Build BIT_SIZE. */
7395 270 : bit_size = build_int_cst (result_type, argsize);
7396 :
7397 270 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7398 : arg, build_int_cst (arg_type, 0));
7399 270 : se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
7400 : bit_size, leadz);
7401 270 : }
7402 :
7403 :
7404 : /* TRAILZ(i) = (i == 0) ? BIT_SIZE (i) : __builtin_ctz(i)
7405 :
7406 : The conditional expression is necessary because the result of TRAILZ(0)
7407 : is defined, but the result of __builtin_ctz(0) is undefined for most
7408 : targets. */
7409 :
7410 : static void
7411 282 : gfc_conv_intrinsic_trailz (gfc_se * se, gfc_expr *expr)
7412 : {
7413 282 : tree arg;
7414 282 : tree arg_type;
7415 282 : tree cond;
7416 282 : tree result_type;
7417 282 : tree trailz;
7418 282 : tree bit_size;
7419 282 : tree func;
7420 282 : int argsize;
7421 :
7422 282 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7423 282 : argsize = TYPE_PRECISION (TREE_TYPE (arg));
7424 :
7425 : /* Which variant of __builtin_ctz* should we call? */
7426 282 : if (argsize <= INT_TYPE_SIZE)
7427 : {
7428 195 : arg_type = unsigned_type_node;
7429 195 : func = builtin_decl_explicit (BUILT_IN_CTZ);
7430 : }
7431 87 : else if (argsize <= LONG_TYPE_SIZE)
7432 : {
7433 57 : arg_type = long_unsigned_type_node;
7434 57 : func = builtin_decl_explicit (BUILT_IN_CTZL);
7435 : }
7436 30 : else if (argsize <= LONG_LONG_TYPE_SIZE)
7437 : {
7438 0 : arg_type = long_long_unsigned_type_node;
7439 0 : func = builtin_decl_explicit (BUILT_IN_CTZLL);
7440 : }
7441 : else
7442 : {
7443 30 : gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
7444 30 : arg_type = gfc_build_uint_type (argsize);
7445 30 : func = NULL_TREE;
7446 : }
7447 :
7448 : /* Convert the actual argument twice: first, to the unsigned type of the
7449 : same size; then, to the proper argument type for the built-in
7450 : function. But the return type is of the default INTEGER kind. */
7451 282 : arg = fold_convert (gfc_build_uint_type (argsize), arg);
7452 282 : arg = fold_convert (arg_type, arg);
7453 282 : arg = gfc_evaluate_now (arg, &se->pre);
7454 282 : result_type = gfc_get_int_type (gfc_default_integer_kind);
7455 :
7456 : /* Compute TRAILZ for the case i .ne. 0. */
7457 282 : if (func)
7458 252 : trailz = fold_convert (result_type, build_call_expr_loc (input_location,
7459 : func, 1, arg));
7460 : else
7461 : {
7462 : /* We end up here if the argument type is larger than 'long long'.
7463 : We generate this code:
7464 :
7465 : if ((x & ULL_MAX) == 0)
7466 : return ULL_SIZE + ctzll ((unsigned long long) (x >> ULLSIZE));
7467 : else
7468 : return ctzll ((unsigned long long) x);
7469 :
7470 : where ULL_MAX is the largest value that a ULL_MAX can hold
7471 : (0xFFFFFFFFFFFFFFFF for a 64-bit long long type), and ULLSIZE
7472 : is the bit-size of the long long type (64 in this example). */
7473 30 : tree ullsize, ullmax, tmp1, tmp2, btmp;
7474 :
7475 30 : ullsize = build_int_cst (result_type, LONG_LONG_TYPE_SIZE);
7476 30 : ullmax = fold_build1_loc (input_location, BIT_NOT_EXPR,
7477 : long_long_unsigned_type_node,
7478 : build_int_cst (long_long_unsigned_type_node, 0));
7479 :
7480 30 : cond = fold_build2_loc (input_location, BIT_AND_EXPR, arg_type, arg,
7481 : fold_convert (arg_type, ullmax));
7482 30 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, cond,
7483 : build_int_cst (arg_type, 0));
7484 :
7485 30 : tmp1 = fold_build2_loc (input_location, RSHIFT_EXPR, arg_type,
7486 : arg, ullsize);
7487 30 : tmp1 = fold_convert (long_long_unsigned_type_node, tmp1);
7488 30 : btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
7489 30 : tmp1 = fold_convert (result_type,
7490 : build_call_expr_loc (input_location, btmp, 1, tmp1));
7491 30 : tmp1 = fold_build2_loc (input_location, PLUS_EXPR, result_type,
7492 : tmp1, ullsize);
7493 :
7494 30 : tmp2 = fold_convert (long_long_unsigned_type_node, arg);
7495 30 : btmp = builtin_decl_explicit (BUILT_IN_CTZLL);
7496 30 : tmp2 = fold_convert (result_type,
7497 : build_call_expr_loc (input_location, btmp, 1, tmp2));
7498 :
7499 30 : trailz = fold_build3_loc (input_location, COND_EXPR, result_type,
7500 : cond, tmp1, tmp2);
7501 : }
7502 :
7503 : /* Build BIT_SIZE. */
7504 282 : bit_size = build_int_cst (result_type, argsize);
7505 :
7506 282 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7507 : arg, build_int_cst (arg_type, 0));
7508 282 : se->expr = fold_build3_loc (input_location, COND_EXPR, result_type, cond,
7509 : bit_size, trailz);
7510 282 : }
7511 :
7512 : /* Using __builtin_popcount for POPCNT and __builtin_parity for POPPAR;
7513 : for types larger than "long long", we call the long long built-in for
7514 : the lower and higher bits and combine the result. */
7515 :
7516 : static void
7517 134 : gfc_conv_intrinsic_popcnt_poppar (gfc_se * se, gfc_expr *expr, int parity)
7518 : {
7519 134 : tree arg;
7520 134 : tree arg_type;
7521 134 : tree result_type;
7522 134 : tree func;
7523 134 : int argsize;
7524 :
7525 134 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7526 134 : argsize = TYPE_PRECISION (TREE_TYPE (arg));
7527 134 : result_type = gfc_get_int_type (gfc_default_integer_kind);
7528 :
7529 : /* Which variant of the builtin should we call? */
7530 134 : if (argsize <= INT_TYPE_SIZE)
7531 : {
7532 108 : arg_type = unsigned_type_node;
7533 198 : func = builtin_decl_explicit (parity
7534 : ? BUILT_IN_PARITY
7535 : : BUILT_IN_POPCOUNT);
7536 : }
7537 26 : else if (argsize <= LONG_TYPE_SIZE)
7538 : {
7539 12 : arg_type = long_unsigned_type_node;
7540 18 : func = builtin_decl_explicit (parity
7541 : ? BUILT_IN_PARITYL
7542 : : BUILT_IN_POPCOUNTL);
7543 : }
7544 14 : else if (argsize <= LONG_LONG_TYPE_SIZE)
7545 : {
7546 0 : arg_type = long_long_unsigned_type_node;
7547 0 : func = builtin_decl_explicit (parity
7548 : ? BUILT_IN_PARITYLL
7549 : : BUILT_IN_POPCOUNTLL);
7550 : }
7551 : else
7552 : {
7553 : /* Our argument type is larger than 'long long', which mean none
7554 : of the POPCOUNT builtins covers it. We thus call the 'long long'
7555 : variant multiple times, and add the results. */
7556 14 : tree utype, arg2, call1, call2;
7557 :
7558 : /* For now, we only cover the case where argsize is twice as large
7559 : as 'long long'. */
7560 14 : gcc_assert (argsize == 2 * LONG_LONG_TYPE_SIZE);
7561 :
7562 21 : func = builtin_decl_explicit (parity
7563 : ? BUILT_IN_PARITYLL
7564 : : BUILT_IN_POPCOUNTLL);
7565 :
7566 : /* Convert it to an integer, and store into a variable. */
7567 14 : utype = gfc_build_uint_type (argsize);
7568 14 : arg = fold_convert (utype, arg);
7569 14 : arg = gfc_evaluate_now (arg, &se->pre);
7570 :
7571 : /* Call the builtin twice. */
7572 14 : call1 = build_call_expr_loc (input_location, func, 1,
7573 : fold_convert (long_long_unsigned_type_node,
7574 : arg));
7575 :
7576 14 : arg2 = fold_build2_loc (input_location, RSHIFT_EXPR, utype, arg,
7577 : build_int_cst (utype, LONG_LONG_TYPE_SIZE));
7578 14 : call2 = build_call_expr_loc (input_location, func, 1,
7579 : fold_convert (long_long_unsigned_type_node,
7580 : arg2));
7581 :
7582 : /* Combine the results. */
7583 14 : if (parity)
7584 7 : se->expr = fold_build2_loc (input_location, BIT_XOR_EXPR,
7585 : integer_type_node, call1, call2);
7586 : else
7587 7 : se->expr = fold_build2_loc (input_location, PLUS_EXPR,
7588 : integer_type_node, call1, call2);
7589 :
7590 14 : se->expr = convert (result_type, se->expr);
7591 14 : return;
7592 : }
7593 :
7594 : /* Convert the actual argument twice: first, to the unsigned type of the
7595 : same size; then, to the proper argument type for the built-in
7596 : function. */
7597 120 : arg = fold_convert (gfc_build_uint_type (argsize), arg);
7598 120 : arg = fold_convert (arg_type, arg);
7599 :
7600 120 : se->expr = fold_convert (result_type,
7601 : build_call_expr_loc (input_location, func, 1, arg));
7602 : }
7603 :
7604 :
7605 : /* Process an intrinsic with unspecified argument-types that has an optional
7606 : argument (which could be of type character), e.g. EOSHIFT. For those, we
7607 : need to append the string length of the optional argument if it is not
7608 : present and the type is really character.
7609 : primary specifies the position (starting at 1) of the non-optional argument
7610 : specifying the type and optional gives the position of the optional
7611 : argument in the arglist. */
7612 :
7613 : static void
7614 5879 : conv_generic_with_optional_char_arg (gfc_se* se, gfc_expr* expr,
7615 : unsigned primary, unsigned optional)
7616 : {
7617 5879 : gfc_actual_arglist* prim_arg;
7618 5879 : gfc_actual_arglist* opt_arg;
7619 5879 : unsigned cur_pos;
7620 5879 : gfc_actual_arglist* arg;
7621 5879 : gfc_symbol* sym;
7622 5879 : vec<tree, va_gc> *append_args;
7623 :
7624 : /* Find the two arguments given as position. */
7625 5879 : cur_pos = 0;
7626 5879 : prim_arg = NULL;
7627 5879 : opt_arg = NULL;
7628 17637 : for (arg = expr->value.function.actual; arg; arg = arg->next)
7629 : {
7630 17637 : ++cur_pos;
7631 :
7632 17637 : if (cur_pos == primary)
7633 5879 : prim_arg = arg;
7634 17637 : if (cur_pos == optional)
7635 5879 : opt_arg = arg;
7636 :
7637 17637 : if (cur_pos >= primary && cur_pos >= optional)
7638 : break;
7639 : }
7640 5879 : gcc_assert (prim_arg);
7641 5879 : gcc_assert (prim_arg->expr);
7642 5879 : gcc_assert (opt_arg);
7643 :
7644 : /* If we do have type CHARACTER and the optional argument is really absent,
7645 : append a dummy 0 as string length. */
7646 5879 : append_args = NULL;
7647 5879 : if (prim_arg->expr->ts.type == BT_CHARACTER && !opt_arg->expr)
7648 : {
7649 608 : tree dummy;
7650 :
7651 608 : dummy = build_int_cst (gfc_charlen_type_node, 0);
7652 608 : vec_alloc (append_args, 1);
7653 608 : append_args->quick_push (dummy);
7654 : }
7655 :
7656 : /* Build the call itself. */
7657 5879 : gcc_assert (!se->ignore_optional);
7658 5879 : sym = gfc_get_symbol_for_expr (expr, false);
7659 5879 : gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
7660 : append_args);
7661 5879 : gfc_free_symbol (sym);
7662 5879 : }
7663 :
7664 : /* The length of a character string. */
7665 : static void
7666 5946 : gfc_conv_intrinsic_len (gfc_se * se, gfc_expr * expr)
7667 : {
7668 5946 : tree len;
7669 5946 : tree type;
7670 5946 : tree decl;
7671 5946 : gfc_symbol *sym;
7672 5946 : gfc_se argse;
7673 5946 : gfc_expr *arg;
7674 :
7675 5946 : gcc_assert (!se->ss);
7676 :
7677 5946 : arg = expr->value.function.actual->expr;
7678 :
7679 5946 : type = gfc_typenode_for_spec (&expr->ts);
7680 5946 : switch (arg->expr_type)
7681 : {
7682 0 : case EXPR_CONSTANT:
7683 0 : len = build_int_cst (gfc_charlen_type_node, arg->value.character.length);
7684 0 : break;
7685 :
7686 2 : case EXPR_ARRAY:
7687 : /* If there is an explicit type-spec, use it. */
7688 2 : if (arg->ts.u.cl->length && arg->ts.u.cl->length_from_typespec)
7689 : {
7690 0 : gfc_conv_string_length (arg->ts.u.cl, arg, &se->pre);
7691 0 : len = arg->ts.u.cl->backend_decl;
7692 0 : break;
7693 : }
7694 :
7695 : /* Obtain the string length from the function used by
7696 : trans-array.cc(gfc_trans_array_constructor). */
7697 2 : len = NULL_TREE;
7698 2 : get_array_ctor_strlen (&se->pre, arg->value.constructor, &len);
7699 2 : break;
7700 :
7701 5359 : case EXPR_VARIABLE:
7702 5359 : if (arg->ref == NULL
7703 2416 : || (arg->ref->next == NULL && arg->ref->type == REF_ARRAY))
7704 : {
7705 : /* This doesn't catch all cases.
7706 : See http://gcc.gnu.org/ml/fortran/2004-06/msg00165.html
7707 : and the surrounding thread. */
7708 4826 : sym = arg->symtree->n.sym;
7709 4826 : decl = gfc_get_symbol_decl (sym);
7710 4826 : if (decl == current_function_decl && sym->attr.function
7711 55 : && (sym->result == sym))
7712 55 : decl = gfc_get_fake_result_decl (sym, 0);
7713 :
7714 4826 : len = sym->ts.u.cl->backend_decl;
7715 4826 : gcc_assert (len);
7716 : break;
7717 : }
7718 :
7719 : /* Fall through. */
7720 :
7721 1118 : default:
7722 1118 : gfc_init_se (&argse, se);
7723 1118 : if (arg->rank == 0)
7724 996 : gfc_conv_expr (&argse, arg);
7725 : else
7726 122 : gfc_conv_expr_descriptor (&argse, arg);
7727 1118 : gfc_add_block_to_block (&se->pre, &argse.pre);
7728 1118 : gfc_add_block_to_block (&se->post, &argse.post);
7729 1118 : len = argse.string_length;
7730 1118 : break;
7731 : }
7732 5946 : se->expr = convert (type, len);
7733 5946 : }
7734 :
7735 : /* The length of a character string not including trailing blanks. */
7736 : static void
7737 2340 : gfc_conv_intrinsic_len_trim (gfc_se * se, gfc_expr * expr)
7738 : {
7739 2340 : int kind = expr->value.function.actual->expr->ts.kind;
7740 2340 : tree args[2], type, fndecl;
7741 :
7742 2340 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
7743 2340 : type = gfc_typenode_for_spec (&expr->ts);
7744 :
7745 2340 : if (kind == 1)
7746 1938 : fndecl = gfor_fndecl_string_len_trim;
7747 402 : else if (kind == 4)
7748 402 : fndecl = gfor_fndecl_string_len_trim_char4;
7749 : else
7750 0 : gcc_unreachable ();
7751 :
7752 2340 : se->expr = build_call_expr_loc (input_location,
7753 : fndecl, 2, args[0], args[1]);
7754 2340 : se->expr = convert (type, se->expr);
7755 2340 : }
7756 :
7757 :
7758 : /* Returns the starting position of a substring within a string. */
7759 :
7760 : static void
7761 751 : gfc_conv_intrinsic_index_scan_verify (gfc_se * se, gfc_expr * expr,
7762 : tree function)
7763 : {
7764 751 : tree logical4_type_node = gfc_get_logical_type (4);
7765 751 : tree type;
7766 751 : tree fndecl;
7767 751 : tree *args;
7768 751 : unsigned int num_args;
7769 :
7770 751 : args = XALLOCAVEC (tree, 5);
7771 :
7772 : /* Get number of arguments; characters count double due to the
7773 : string length argument. Kind= is not passed to the library
7774 : and thus ignored. */
7775 751 : if (expr->value.function.actual->next->next->expr == NULL)
7776 : num_args = 4;
7777 : else
7778 304 : num_args = 5;
7779 :
7780 751 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
7781 751 : type = gfc_typenode_for_spec (&expr->ts);
7782 :
7783 751 : if (num_args == 4)
7784 447 : args[4] = build_int_cst (logical4_type_node, 0);
7785 : else
7786 304 : args[4] = convert (logical4_type_node, args[4]);
7787 :
7788 751 : fndecl = build_addr (function);
7789 751 : se->expr = build_call_array_loc (input_location,
7790 751 : TREE_TYPE (TREE_TYPE (function)), fndecl,
7791 : 5, args);
7792 751 : se->expr = convert (type, se->expr);
7793 :
7794 751 : }
7795 :
7796 : /* The ascii value for a single character. */
7797 : static void
7798 2033 : gfc_conv_intrinsic_ichar (gfc_se * se, gfc_expr * expr)
7799 : {
7800 2033 : tree args[3], type, pchartype;
7801 2033 : int nargs;
7802 :
7803 2033 : nargs = gfc_intrinsic_argument_list_length (expr);
7804 2033 : gfc_conv_intrinsic_function_args (se, expr, args, nargs);
7805 2033 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (args[1])));
7806 2033 : pchartype = gfc_get_pchar_type (expr->value.function.actual->expr->ts.kind);
7807 2033 : args[1] = fold_build1_loc (input_location, NOP_EXPR, pchartype, args[1]);
7808 2033 : type = gfc_typenode_for_spec (&expr->ts);
7809 :
7810 2033 : se->expr = build_fold_indirect_ref_loc (input_location,
7811 : args[1]);
7812 2033 : se->expr = convert (type, se->expr);
7813 2033 : }
7814 :
7815 :
7816 : /* Intrinsic ISNAN calls __builtin_isnan. */
7817 :
7818 : static void
7819 432 : gfc_conv_intrinsic_isnan (gfc_se * se, gfc_expr * expr)
7820 : {
7821 432 : tree arg;
7822 :
7823 432 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7824 432 : se->expr = build_call_expr_loc (input_location,
7825 : builtin_decl_explicit (BUILT_IN_ISNAN),
7826 : 1, arg);
7827 864 : STRIP_TYPE_NOPS (se->expr);
7828 432 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
7829 432 : }
7830 :
7831 :
7832 : /* Intrinsics IS_IOSTAT_END and IS_IOSTAT_EOR just need to compare
7833 : their argument against a constant integer value. */
7834 :
7835 : static void
7836 24 : gfc_conv_has_intvalue (gfc_se * se, gfc_expr * expr, const int value)
7837 : {
7838 24 : tree arg;
7839 :
7840 24 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7841 24 : se->expr = fold_build2_loc (input_location, EQ_EXPR,
7842 : gfc_typenode_for_spec (&expr->ts),
7843 24 : arg, build_int_cst (TREE_TYPE (arg), value));
7844 24 : }
7845 :
7846 :
7847 :
7848 : /* MERGE (tsource, fsource, mask) = mask ? tsource : fsource. */
7849 :
7850 : static void
7851 949 : gfc_conv_intrinsic_merge (gfc_se * se, gfc_expr * expr)
7852 : {
7853 949 : tree tsource;
7854 949 : tree fsource;
7855 949 : tree mask;
7856 949 : tree type;
7857 949 : tree len, len2;
7858 949 : tree *args;
7859 949 : unsigned int num_args;
7860 :
7861 949 : num_args = gfc_intrinsic_argument_list_length (expr);
7862 949 : args = XALLOCAVEC (tree, num_args);
7863 :
7864 949 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
7865 949 : if (expr->ts.type != BT_CHARACTER)
7866 : {
7867 422 : tsource = args[0];
7868 422 : fsource = args[1];
7869 422 : mask = args[2];
7870 : }
7871 : else
7872 : {
7873 : /* We do the same as in the non-character case, but the argument
7874 : list is different because of the string length arguments. We
7875 : also have to set the string length for the result. */
7876 527 : len = args[0];
7877 527 : tsource = args[1];
7878 527 : len2 = args[2];
7879 527 : fsource = args[3];
7880 527 : mask = args[4];
7881 :
7882 527 : gfc_trans_same_strlen_check ("MERGE intrinsic", &expr->where, len, len2,
7883 : &se->pre);
7884 527 : se->string_length = len;
7885 : }
7886 949 : tsource = gfc_evaluate_now (tsource, &se->pre);
7887 949 : fsource = gfc_evaluate_now (fsource, &se->pre);
7888 949 : mask = gfc_evaluate_now (mask, &se->pre);
7889 949 : type = TREE_TYPE (tsource);
7890 949 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, mask, tsource,
7891 : fold_convert (type, fsource));
7892 949 : }
7893 :
7894 :
7895 : /* MERGE_BITS (I, J, MASK) = (I & MASK) | (I & (~MASK)). */
7896 :
7897 : static void
7898 42 : gfc_conv_intrinsic_merge_bits (gfc_se * se, gfc_expr * expr)
7899 : {
7900 42 : tree args[3], mask, type;
7901 :
7902 42 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
7903 42 : mask = gfc_evaluate_now (args[2], &se->pre);
7904 :
7905 42 : type = TREE_TYPE (args[0]);
7906 42 : gcc_assert (TREE_TYPE (args[1]) == type);
7907 42 : gcc_assert (TREE_TYPE (mask) == type);
7908 :
7909 42 : args[0] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[0], mask);
7910 42 : args[1] = fold_build2_loc (input_location, BIT_AND_EXPR, type, args[1],
7911 : fold_build1_loc (input_location, BIT_NOT_EXPR,
7912 : type, mask));
7913 42 : se->expr = fold_build2_loc (input_location, BIT_IOR_EXPR, type,
7914 : args[0], args[1]);
7915 42 : }
7916 :
7917 :
7918 : /* MASKL(n) = n == 0 ? 0 : (~0) << (BIT_SIZE - n)
7919 : MASKR(n) = n == BIT_SIZE ? ~0 : ~((~0) << n) */
7920 :
7921 : static void
7922 64 : gfc_conv_intrinsic_mask (gfc_se * se, gfc_expr * expr, int left)
7923 : {
7924 64 : tree arg, allones, type, utype, res, cond, bitsize;
7925 64 : int i;
7926 :
7927 64 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7928 64 : arg = gfc_evaluate_now (arg, &se->pre);
7929 :
7930 64 : type = gfc_get_int_type (expr->ts.kind);
7931 64 : utype = unsigned_type_for (type);
7932 :
7933 64 : i = gfc_validate_kind (BT_INTEGER, expr->ts.kind, false);
7934 64 : bitsize = build_int_cst (TREE_TYPE (arg), gfc_integer_kinds[i].bit_size);
7935 :
7936 64 : allones = fold_build1_loc (input_location, BIT_NOT_EXPR, utype,
7937 : build_int_cst (utype, 0));
7938 :
7939 64 : if (left)
7940 : {
7941 : /* Left-justified mask. */
7942 32 : res = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (arg),
7943 : bitsize, arg);
7944 32 : res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
7945 : fold_convert (utype, res));
7946 :
7947 : /* Special case arg == 0, because SHIFT_EXPR wants a shift strictly
7948 : smaller than type width. */
7949 32 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
7950 32 : build_int_cst (TREE_TYPE (arg), 0));
7951 32 : res = fold_build3_loc (input_location, COND_EXPR, utype, cond,
7952 : build_int_cst (utype, 0), res);
7953 : }
7954 : else
7955 : {
7956 : /* Right-justified mask. */
7957 32 : res = fold_build2_loc (input_location, LSHIFT_EXPR, utype, allones,
7958 : fold_convert (utype, arg));
7959 32 : res = fold_build1_loc (input_location, BIT_NOT_EXPR, utype, res);
7960 :
7961 : /* Special case agr == bit_size, because SHIFT_EXPR wants a shift
7962 : strictly smaller than type width. */
7963 32 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7964 : arg, bitsize);
7965 32 : res = fold_build3_loc (input_location, COND_EXPR, utype,
7966 : cond, allones, res);
7967 : }
7968 :
7969 64 : se->expr = fold_convert (type, res);
7970 64 : }
7971 :
7972 :
7973 : /* FRACTION (s) is translated into:
7974 : isfinite (s) ? frexp (s, &dummy_int) : NaN */
7975 : static void
7976 60 : gfc_conv_intrinsic_fraction (gfc_se * se, gfc_expr * expr)
7977 : {
7978 60 : tree arg, type, tmp, res, frexp, cond;
7979 :
7980 60 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
7981 :
7982 60 : type = gfc_typenode_for_spec (&expr->ts);
7983 60 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
7984 60 : arg = gfc_evaluate_now (arg, &se->pre);
7985 :
7986 60 : cond = build_call_expr_loc (input_location,
7987 : builtin_decl_explicit (BUILT_IN_ISFINITE),
7988 : 1, arg);
7989 :
7990 60 : tmp = gfc_create_var (integer_type_node, NULL);
7991 60 : res = build_call_expr_loc (input_location, frexp, 2,
7992 : fold_convert (type, arg),
7993 : gfc_build_addr_expr (NULL_TREE, tmp));
7994 60 : res = fold_convert (type, res);
7995 :
7996 60 : se->expr = fold_build3_loc (input_location, COND_EXPR, type,
7997 : cond, res, gfc_build_nan (type, ""));
7998 60 : }
7999 :
8000 :
8001 : /* NEAREST (s, dir) is translated into
8002 : tmp = copysign (HUGE_VAL, dir);
8003 : return nextafter (s, tmp);
8004 : */
8005 : static void
8006 1595 : gfc_conv_intrinsic_nearest (gfc_se * se, gfc_expr * expr)
8007 : {
8008 1595 : tree args[2], type, tmp, nextafter, copysign, huge_val;
8009 :
8010 1595 : nextafter = gfc_builtin_decl_for_float_kind (BUILT_IN_NEXTAFTER, expr->ts.kind);
8011 1595 : copysign = gfc_builtin_decl_for_float_kind (BUILT_IN_COPYSIGN, expr->ts.kind);
8012 :
8013 1595 : type = gfc_typenode_for_spec (&expr->ts);
8014 1595 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
8015 :
8016 1595 : huge_val = gfc_build_inf_or_huge (type, expr->ts.kind);
8017 1595 : tmp = build_call_expr_loc (input_location, copysign, 2, huge_val,
8018 : fold_convert (type, args[1]));
8019 1595 : se->expr = build_call_expr_loc (input_location, nextafter, 2,
8020 : fold_convert (type, args[0]), tmp);
8021 1595 : se->expr = fold_convert (type, se->expr);
8022 1595 : }
8023 :
8024 :
8025 : /* SPACING (s) is translated into
8026 : int e;
8027 : if (!isfinite (s))
8028 : res = NaN;
8029 : else if (s == 0)
8030 : res = tiny;
8031 : else
8032 : {
8033 : frexp (s, &e);
8034 : e = e - prec;
8035 : e = MAX_EXPR (e, emin);
8036 : res = scalbn (1., e);
8037 : }
8038 : return res;
8039 :
8040 : where prec is the precision of s, gfc_real_kinds[k].digits,
8041 : emin is min_exponent - 1, gfc_real_kinds[k].min_exponent - 1,
8042 : and tiny is tiny(s), gfc_real_kinds[k].tiny. */
8043 :
8044 : static void
8045 70 : gfc_conv_intrinsic_spacing (gfc_se * se, gfc_expr * expr)
8046 : {
8047 70 : tree arg, type, prec, emin, tiny, res, e;
8048 70 : tree cond, nan, tmp, frexp, scalbn;
8049 70 : int k;
8050 70 : stmtblock_t block;
8051 :
8052 70 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
8053 70 : prec = build_int_cst (integer_type_node, gfc_real_kinds[k].digits);
8054 70 : emin = build_int_cst (integer_type_node, gfc_real_kinds[k].min_exponent - 1);
8055 70 : tiny = gfc_conv_mpfr_to_tree (gfc_real_kinds[k].tiny, expr->ts.kind, 0);
8056 :
8057 70 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
8058 70 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
8059 :
8060 70 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
8061 70 : arg = gfc_evaluate_now (arg, &se->pre);
8062 :
8063 70 : type = gfc_typenode_for_spec (&expr->ts);
8064 70 : e = gfc_create_var (integer_type_node, NULL);
8065 70 : res = gfc_create_var (type, NULL);
8066 :
8067 :
8068 : /* Build the block for s /= 0. */
8069 70 : gfc_start_block (&block);
8070 70 : tmp = build_call_expr_loc (input_location, frexp, 2, arg,
8071 : gfc_build_addr_expr (NULL_TREE, e));
8072 70 : gfc_add_expr_to_block (&block, tmp);
8073 :
8074 70 : tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node, e,
8075 : prec);
8076 70 : gfc_add_modify (&block, e, fold_build2_loc (input_location, MAX_EXPR,
8077 : integer_type_node, tmp, emin));
8078 :
8079 70 : tmp = build_call_expr_loc (input_location, scalbn, 2,
8080 70 : build_real_from_int_cst (type, integer_one_node), e);
8081 70 : gfc_add_modify (&block, res, tmp);
8082 :
8083 : /* Finish by building the IF statement for value zero. */
8084 70 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
8085 70 : build_real_from_int_cst (type, integer_zero_node));
8086 70 : tmp = build3_v (COND_EXPR, cond, build2_v (MODIFY_EXPR, res, tiny),
8087 : gfc_finish_block (&block));
8088 :
8089 : /* And deal with infinities and NaNs. */
8090 70 : cond = build_call_expr_loc (input_location,
8091 : builtin_decl_explicit (BUILT_IN_ISFINITE),
8092 : 1, arg);
8093 70 : nan = gfc_build_nan (type, "");
8094 70 : tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, res, nan));
8095 :
8096 70 : gfc_add_expr_to_block (&se->pre, tmp);
8097 70 : se->expr = res;
8098 70 : }
8099 :
8100 :
8101 : /* RRSPACING (s) is translated into
8102 : int e;
8103 : real x;
8104 : x = fabs (s);
8105 : if (isfinite (x))
8106 : {
8107 : if (x != 0)
8108 : {
8109 : frexp (s, &e);
8110 : x = scalbn (x, precision - e);
8111 : }
8112 : }
8113 : else
8114 : x = NaN;
8115 : return x;
8116 :
8117 : where precision is gfc_real_kinds[k].digits. */
8118 :
8119 : static void
8120 48 : gfc_conv_intrinsic_rrspacing (gfc_se * se, gfc_expr * expr)
8121 : {
8122 48 : tree arg, type, e, x, cond, nan, stmt, tmp, frexp, scalbn, fabs;
8123 48 : int prec, k;
8124 48 : stmtblock_t block;
8125 :
8126 48 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
8127 48 : prec = gfc_real_kinds[k].digits;
8128 :
8129 48 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
8130 48 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
8131 48 : fabs = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
8132 :
8133 48 : type = gfc_typenode_for_spec (&expr->ts);
8134 48 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
8135 48 : arg = gfc_evaluate_now (arg, &se->pre);
8136 :
8137 48 : e = gfc_create_var (integer_type_node, NULL);
8138 48 : x = gfc_create_var (type, NULL);
8139 48 : gfc_add_modify (&se->pre, x,
8140 : build_call_expr_loc (input_location, fabs, 1, arg));
8141 :
8142 :
8143 48 : gfc_start_block (&block);
8144 48 : tmp = build_call_expr_loc (input_location, frexp, 2, arg,
8145 : gfc_build_addr_expr (NULL_TREE, e));
8146 48 : gfc_add_expr_to_block (&block, tmp);
8147 :
8148 48 : tmp = fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
8149 48 : build_int_cst (integer_type_node, prec), e);
8150 48 : tmp = build_call_expr_loc (input_location, scalbn, 2, x, tmp);
8151 48 : gfc_add_modify (&block, x, tmp);
8152 48 : stmt = gfc_finish_block (&block);
8153 :
8154 : /* if (x != 0) */
8155 48 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, x,
8156 48 : build_real_from_int_cst (type, integer_zero_node));
8157 48 : tmp = build3_v (COND_EXPR, cond, stmt, build_empty_stmt (input_location));
8158 :
8159 : /* And deal with infinities and NaNs. */
8160 48 : cond = build_call_expr_loc (input_location,
8161 : builtin_decl_explicit (BUILT_IN_ISFINITE),
8162 : 1, x);
8163 48 : nan = gfc_build_nan (type, "");
8164 48 : tmp = build3_v (COND_EXPR, cond, tmp, build2_v (MODIFY_EXPR, x, nan));
8165 :
8166 48 : gfc_add_expr_to_block (&se->pre, tmp);
8167 48 : se->expr = fold_convert (type, x);
8168 48 : }
8169 :
8170 :
8171 : /* SCALE (s, i) is translated into scalbn (s, i). */
8172 : static void
8173 72 : gfc_conv_intrinsic_scale (gfc_se * se, gfc_expr * expr)
8174 : {
8175 72 : tree args[2], type, scalbn;
8176 :
8177 72 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
8178 :
8179 72 : type = gfc_typenode_for_spec (&expr->ts);
8180 72 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
8181 72 : se->expr = build_call_expr_loc (input_location, scalbn, 2,
8182 : fold_convert (type, args[0]),
8183 : fold_convert (integer_type_node, args[1]));
8184 72 : se->expr = fold_convert (type, se->expr);
8185 72 : }
8186 :
8187 :
8188 : /* SET_EXPONENT (s, i) is translated into
8189 : isfinite(s) ? scalbn (frexp (s, &dummy_int), i) : NaN */
8190 : static void
8191 262 : gfc_conv_intrinsic_set_exponent (gfc_se * se, gfc_expr * expr)
8192 : {
8193 262 : tree args[2], type, tmp, frexp, scalbn, cond, nan, res;
8194 :
8195 262 : frexp = gfc_builtin_decl_for_float_kind (BUILT_IN_FREXP, expr->ts.kind);
8196 262 : scalbn = gfc_builtin_decl_for_float_kind (BUILT_IN_SCALBN, expr->ts.kind);
8197 :
8198 262 : type = gfc_typenode_for_spec (&expr->ts);
8199 262 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
8200 262 : args[0] = gfc_evaluate_now (args[0], &se->pre);
8201 :
8202 262 : tmp = gfc_create_var (integer_type_node, NULL);
8203 262 : tmp = build_call_expr_loc (input_location, frexp, 2,
8204 : fold_convert (type, args[0]),
8205 : gfc_build_addr_expr (NULL_TREE, tmp));
8206 262 : res = build_call_expr_loc (input_location, scalbn, 2, tmp,
8207 : fold_convert (integer_type_node, args[1]));
8208 262 : res = fold_convert (type, res);
8209 :
8210 : /* Call to isfinite */
8211 262 : cond = build_call_expr_loc (input_location,
8212 : builtin_decl_explicit (BUILT_IN_ISFINITE),
8213 : 1, args[0]);
8214 262 : nan = gfc_build_nan (type, "");
8215 :
8216 262 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, cond,
8217 : res, nan);
8218 262 : }
8219 :
8220 :
8221 : static void
8222 15631 : gfc_conv_intrinsic_size (gfc_se * se, gfc_expr * expr)
8223 : {
8224 15631 : gfc_actual_arglist *actual;
8225 15631 : tree arg1;
8226 15631 : tree type;
8227 15631 : tree size;
8228 15631 : gfc_se argse;
8229 15631 : gfc_expr *e;
8230 15631 : gfc_symbol *sym = NULL;
8231 :
8232 15631 : gfc_init_se (&argse, NULL);
8233 15631 : actual = expr->value.function.actual;
8234 :
8235 15631 : if (actual->expr->ts.type == BT_CLASS)
8236 627 : gfc_add_class_array_ref (actual->expr);
8237 :
8238 15631 : e = actual->expr;
8239 :
8240 : /* These are emerging from the interface mapping, when a class valued
8241 : function appears as the rhs in a realloc on assign statement, where
8242 : the size of the result is that of one of the actual arguments. */
8243 15631 : if (e->expr_type == EXPR_VARIABLE
8244 15149 : && e->symtree->n.sym->ns == NULL /* This is distinctive! */
8245 573 : && e->symtree->n.sym->ts.type == BT_CLASS
8246 62 : && e->ref && e->ref->type == REF_COMPONENT
8247 44 : && strcmp (e->ref->u.c.component->name, "_data") == 0)
8248 15631 : sym = e->symtree->n.sym;
8249 :
8250 15631 : if ((gfc_option.rtcheck & GFC_RTCHECK_POINTER)
8251 : && e
8252 854 : && (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION))
8253 : {
8254 854 : symbol_attribute attr;
8255 854 : char *msg;
8256 854 : tree temp;
8257 854 : tree cond;
8258 :
8259 854 : if (e->symtree->n.sym && IS_CLASS_ARRAY (e->symtree->n.sym))
8260 : {
8261 33 : attr = CLASS_DATA (e->symtree->n.sym)->attr;
8262 33 : attr.pointer = attr.class_pointer;
8263 : }
8264 : else
8265 821 : attr = gfc_expr_attr (e);
8266 :
8267 854 : if (attr.allocatable)
8268 100 : msg = xasprintf ("Allocatable argument '%s' is not allocated",
8269 100 : e->symtree->n.sym->name);
8270 754 : else if (attr.pointer)
8271 46 : msg = xasprintf ("Pointer argument '%s' is not associated",
8272 46 : e->symtree->n.sym->name);
8273 : else
8274 708 : goto end_arg_check;
8275 :
8276 146 : if (sym)
8277 : {
8278 0 : temp = gfc_class_data_get (sym->backend_decl);
8279 0 : temp = gfc_conv_descriptor_data_get (temp);
8280 : }
8281 : else
8282 : {
8283 146 : argse.descriptor_only = 1;
8284 146 : gfc_conv_expr_descriptor (&argse, actual->expr);
8285 146 : temp = gfc_conv_descriptor_data_get (argse.expr);
8286 : }
8287 :
8288 146 : cond = fold_build2_loc (input_location, EQ_EXPR,
8289 : logical_type_node, temp,
8290 146 : fold_convert (TREE_TYPE (temp),
8291 : null_pointer_node));
8292 146 : gfc_trans_runtime_check (true, false, cond, &argse.pre, &e->where, msg);
8293 :
8294 146 : free (msg);
8295 : }
8296 14777 : end_arg_check:
8297 :
8298 15631 : argse.data_not_needed = 1;
8299 15631 : if (gfc_is_class_array_function (e))
8300 : {
8301 : /* For functions that return a class array conv_expr_descriptor is not
8302 : able to get the descriptor right. Therefore this special case. */
8303 7 : gfc_conv_expr_reference (&argse, e);
8304 7 : argse.expr = gfc_class_data_get (argse.expr);
8305 : }
8306 15624 : else if (sym && sym->backend_decl)
8307 : {
8308 32 : gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (sym->backend_decl)));
8309 32 : argse.expr = gfc_class_data_get (sym->backend_decl);
8310 : }
8311 : else
8312 15592 : gfc_conv_expr_descriptor (&argse, actual->expr);
8313 15631 : gfc_add_block_to_block (&se->pre, &argse.pre);
8314 15631 : gfc_add_block_to_block (&se->post, &argse.post);
8315 15631 : arg1 = argse.expr;
8316 :
8317 15631 : actual = actual->next;
8318 15631 : if (actual->expr)
8319 : {
8320 9355 : stmtblock_t block;
8321 9355 : gfc_init_block (&block);
8322 9355 : gfc_init_se (&argse, NULL);
8323 9355 : gfc_conv_expr_type (&argse, actual->expr,
8324 : gfc_array_index_type);
8325 9355 : gfc_add_block_to_block (&block, &argse.pre);
8326 9355 : tree tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8327 : argse.expr, gfc_index_one_node);
8328 9355 : size = gfc_tree_array_size (&block, arg1, e, tmp);
8329 :
8330 : /* Unusually, for an intrinsic, size does not exclude
8331 : an optional arg2, so we must test for it. */
8332 9355 : if (actual->expr->expr_type == EXPR_VARIABLE
8333 2571 : && actual->expr->symtree->n.sym->attr.dummy
8334 31 : && actual->expr->symtree->n.sym->attr.optional)
8335 : {
8336 31 : tree cond;
8337 31 : stmtblock_t block2;
8338 31 : gfc_init_block (&block2);
8339 31 : gfc_init_se (&argse, NULL);
8340 31 : argse.want_pointer = 1;
8341 31 : argse.data_not_needed = 1;
8342 31 : gfc_conv_expr (&argse, actual->expr);
8343 31 : gfc_add_block_to_block (&se->pre, &argse.pre);
8344 : /* 'block2' contains the arg2 absent case, 'block' the arg2 present
8345 : case; size_var can be used in both blocks. */
8346 31 : tree size_var = gfc_create_var (TREE_TYPE (size), "size");
8347 31 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
8348 31 : TREE_TYPE (size_var), size_var, size);
8349 31 : gfc_add_expr_to_block (&block, tmp);
8350 31 : size = gfc_tree_array_size (&block2, arg1, e, NULL_TREE);
8351 31 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
8352 31 : TREE_TYPE (size_var), size_var, size);
8353 31 : gfc_add_expr_to_block (&block2, tmp);
8354 31 : cond = gfc_conv_expr_present (actual->expr->symtree->n.sym);
8355 31 : tmp = build3_v (COND_EXPR, cond, gfc_finish_block (&block),
8356 : gfc_finish_block (&block2));
8357 31 : gfc_add_expr_to_block (&se->pre, tmp);
8358 31 : size = size_var;
8359 31 : }
8360 : else
8361 9324 : gfc_add_block_to_block (&se->pre, &block);
8362 : }
8363 : else
8364 6276 : size = gfc_tree_array_size (&se->pre, arg1, e, NULL_TREE);
8365 15631 : type = gfc_typenode_for_spec (&expr->ts);
8366 15631 : se->expr = convert (type, size);
8367 15631 : }
8368 :
8369 :
8370 : /* Helper function to compute the size of a character variable,
8371 : excluding the terminating null characters. The result has
8372 : gfc_array_index_type type. */
8373 :
8374 : tree
8375 1918 : size_of_string_in_bytes (int kind, tree string_length)
8376 : {
8377 1918 : tree bytesize;
8378 1918 : int i = gfc_validate_kind (BT_CHARACTER, kind, false);
8379 :
8380 3836 : bytesize = build_int_cst (gfc_array_index_type,
8381 1918 : gfc_character_kinds[i].bit_size / 8);
8382 :
8383 1918 : return fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8384 : bytesize,
8385 1918 : fold_convert (gfc_array_index_type, string_length));
8386 : }
8387 :
8388 :
8389 : static void
8390 1309 : gfc_conv_intrinsic_sizeof (gfc_se *se, gfc_expr *expr)
8391 : {
8392 1309 : gfc_expr *arg;
8393 1309 : gfc_se argse;
8394 1309 : tree source_bytes;
8395 1309 : tree tmp;
8396 1309 : tree lower;
8397 1309 : tree upper;
8398 1309 : tree byte_size;
8399 1309 : int n;
8400 :
8401 1309 : gfc_init_se (&argse, NULL);
8402 1309 : arg = expr->value.function.actual->expr;
8403 :
8404 1309 : if (arg->rank || arg->ts.type == BT_ASSUMED)
8405 1012 : gfc_conv_expr_descriptor (&argse, arg);
8406 : else
8407 297 : gfc_conv_expr_reference (&argse, arg);
8408 :
8409 1309 : if (arg->ts.type == BT_ASSUMED)
8410 : {
8411 : /* This only works if an array descriptor has been passed; thus, extract
8412 : the size from the descriptor. */
8413 172 : gcc_assert (TYPE_PRECISION (gfc_array_index_type)
8414 : == TYPE_PRECISION (size_type_node));
8415 172 : tmp = arg->symtree->n.sym->backend_decl;
8416 172 : tmp = DECL_LANG_SPECIFIC (tmp)
8417 60 : && GFC_DECL_SAVED_DESCRIPTOR (tmp) != NULL_TREE
8418 226 : ? GFC_DECL_SAVED_DESCRIPTOR (tmp) : tmp;
8419 172 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
8420 172 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
8421 :
8422 172 : tmp = gfc_conv_descriptor_elem_len_get (tmp);
8423 :
8424 172 : byte_size = fold_convert (gfc_array_index_type, tmp);
8425 : }
8426 1137 : else if (arg->ts.type == BT_CLASS)
8427 : {
8428 : /* Conv_expr_descriptor returns a component_ref to _data component of the
8429 : class object. The class object may be a non-pointer object, e.g.
8430 : located on the stack, or a memory location pointed to, e.g. a
8431 : parameter, i.e., an indirect_ref. */
8432 959 : if (POINTER_TYPE_P (TREE_TYPE (argse.expr))
8433 589 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (argse.expr))))
8434 198 : byte_size
8435 198 : = gfc_class_vtab_size_get (build_fold_indirect_ref (argse.expr));
8436 391 : else if (GFC_CLASS_TYPE_P (TREE_TYPE (argse.expr)))
8437 0 : byte_size = gfc_class_vtab_size_get (argse.expr);
8438 391 : else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (argse.expr))
8439 391 : && TREE_CODE (argse.expr) == COMPONENT_REF)
8440 328 : byte_size = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
8441 63 : else if (arg->rank > 0
8442 21 : || (arg->rank == 0
8443 21 : && arg->ref && arg->ref->type == REF_COMPONENT))
8444 : {
8445 : /* The scalarizer added an additional temp. To get the class' vptr
8446 : one has to look at the original backend_decl. */
8447 63 : if (argse.class_container)
8448 21 : byte_size = gfc_class_vtab_size_get (argse.class_container);
8449 42 : else if (DECL_LANG_SPECIFIC (arg->symtree->n.sym->backend_decl))
8450 84 : byte_size = gfc_class_vtab_size_get (
8451 42 : GFC_DECL_SAVED_DESCRIPTOR (arg->symtree->n.sym->backend_decl));
8452 : else
8453 0 : gcc_unreachable ();
8454 : }
8455 : else
8456 0 : gcc_unreachable ();
8457 : }
8458 : else
8459 : {
8460 548 : if (arg->ts.type == BT_CHARACTER)
8461 84 : byte_size = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
8462 : else
8463 : {
8464 464 : if (arg->rank == 0)
8465 0 : byte_size = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8466 : argse.expr));
8467 : else
8468 464 : byte_size = gfc_get_element_type (TREE_TYPE (argse.expr));
8469 464 : byte_size = fold_convert (gfc_array_index_type,
8470 : size_in_bytes (byte_size));
8471 : }
8472 : }
8473 :
8474 1309 : if (arg->rank == 0)
8475 297 : se->expr = byte_size;
8476 : else
8477 : {
8478 1012 : source_bytes = gfc_create_var (gfc_array_index_type, "bytes");
8479 1012 : gfc_add_modify (&argse.pre, source_bytes, byte_size);
8480 :
8481 1012 : if (arg->rank == -1)
8482 : {
8483 365 : tree cond, loop_var, exit_label;
8484 365 : stmtblock_t body;
8485 :
8486 365 : tmp = gfc_conv_descriptor_rank_get (argse.expr);
8487 365 : loop_var = gfc_create_var (gfc_array_dim_rank_type, "i");
8488 365 : gfc_add_modify (&argse.pre, loop_var, gfc_rank_cst[0]);
8489 365 : exit_label = gfc_build_label_decl (NULL_TREE);
8490 :
8491 : /* Create loop:
8492 : for (;;)
8493 : {
8494 : if (i >= rank)
8495 : goto exit;
8496 : source_bytes = source_bytes * array.dim[i].extent;
8497 : i = i + 1;
8498 : }
8499 : exit: */
8500 365 : gfc_start_block (&body);
8501 365 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
8502 : loop_var, tmp);
8503 365 : tmp = build1_v (GOTO_EXPR, exit_label);
8504 365 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
8505 : cond, tmp, build_empty_stmt (input_location));
8506 365 : gfc_add_expr_to_block (&body, tmp);
8507 :
8508 365 : lower = gfc_conv_descriptor_lbound_get (argse.expr, loop_var);
8509 365 : upper = gfc_conv_descriptor_ubound_get (argse.expr, loop_var);
8510 365 : tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
8511 365 : tmp = fold_build2_loc (input_location, MULT_EXPR,
8512 : gfc_array_index_type, tmp, source_bytes);
8513 365 : gfc_add_modify (&body, source_bytes, tmp);
8514 :
8515 365 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
8516 : gfc_array_dim_rank_type, loop_var,
8517 : gfc_rank_cst[1]);
8518 365 : gfc_add_modify_loc (input_location, &body, loop_var, tmp);
8519 :
8520 365 : tmp = gfc_finish_block (&body);
8521 :
8522 365 : tmp = fold_build1_loc (input_location, LOOP_EXPR, void_type_node,
8523 : tmp);
8524 365 : gfc_add_expr_to_block (&argse.pre, tmp);
8525 :
8526 365 : tmp = build1_v (LABEL_EXPR, exit_label);
8527 365 : gfc_add_expr_to_block (&argse.pre, tmp);
8528 : }
8529 : else
8530 : {
8531 : /* Obtain the size of the array in bytes. */
8532 1834 : for (n = 0; n < arg->rank; n++)
8533 : {
8534 1187 : tree idx;
8535 1187 : idx = gfc_rank_cst[n];
8536 1187 : lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
8537 1187 : upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
8538 1187 : tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
8539 1187 : tmp = fold_build2_loc (input_location, MULT_EXPR,
8540 : gfc_array_index_type, tmp, source_bytes);
8541 1187 : gfc_add_modify (&argse.pre, source_bytes, tmp);
8542 : }
8543 : }
8544 1012 : se->expr = source_bytes;
8545 : }
8546 :
8547 1309 : gfc_add_block_to_block (&se->pre, &argse.pre);
8548 1309 : }
8549 :
8550 :
8551 : static void
8552 865 : gfc_conv_intrinsic_storage_size (gfc_se *se, gfc_expr *expr)
8553 : {
8554 865 : gfc_expr *arg;
8555 865 : gfc_se argse;
8556 865 : tree type, result_type, tmp, class_decl = NULL;
8557 865 : gfc_symbol *sym;
8558 865 : bool unlimited = false;
8559 :
8560 865 : arg = expr->value.function.actual->expr;
8561 :
8562 865 : gfc_init_se (&argse, NULL);
8563 865 : result_type = gfc_get_int_type (expr->ts.kind);
8564 :
8565 865 : if (arg->rank == 0)
8566 : {
8567 236 : if (arg->ts.type == BT_CLASS)
8568 : {
8569 86 : unlimited = UNLIMITED_POLY (arg);
8570 86 : gfc_add_vptr_component (arg);
8571 86 : gfc_add_size_component (arg);
8572 86 : gfc_conv_expr (&argse, arg);
8573 86 : tmp = fold_convert (result_type, argse.expr);
8574 86 : class_decl = gfc_get_class_from_expr (argse.expr);
8575 86 : goto done;
8576 : }
8577 :
8578 150 : gfc_conv_expr_reference (&argse, arg);
8579 150 : type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8580 : argse.expr));
8581 : }
8582 : else
8583 : {
8584 629 : argse.want_pointer = 0;
8585 629 : gfc_conv_expr_descriptor (&argse, arg);
8586 629 : sym = arg->expr_type == EXPR_VARIABLE ? arg->symtree->n.sym : NULL;
8587 629 : if (arg->ts.type == BT_CLASS)
8588 : {
8589 60 : unlimited = UNLIMITED_POLY (arg);
8590 60 : if (TREE_CODE (argse.expr) == COMPONENT_REF)
8591 54 : tmp = gfc_class_vtab_size_get (TREE_OPERAND (argse.expr, 0));
8592 6 : else if (arg->rank > 0 && sym
8593 12 : && DECL_LANG_SPECIFIC (sym->backend_decl))
8594 12 : tmp = gfc_class_vtab_size_get (
8595 6 : GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl));
8596 : else
8597 0 : gcc_unreachable ();
8598 60 : tmp = fold_convert (result_type, tmp);
8599 60 : class_decl = gfc_get_class_from_expr (argse.expr);
8600 60 : goto done;
8601 : }
8602 569 : type = gfc_get_element_type (TREE_TYPE (argse.expr));
8603 : }
8604 :
8605 : /* Obtain the argument's word length. */
8606 719 : if (arg->ts.type == BT_CHARACTER)
8607 241 : tmp = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
8608 : else
8609 478 : tmp = size_in_bytes (type);
8610 719 : tmp = fold_convert (result_type, tmp);
8611 :
8612 865 : done:
8613 865 : if (unlimited && class_decl)
8614 68 : tmp = gfc_resize_class_size_with_len (NULL, class_decl, tmp);
8615 :
8616 865 : se->expr = fold_build2_loc (input_location, MULT_EXPR, result_type, tmp,
8617 : build_int_cst (result_type, BITS_PER_UNIT));
8618 865 : gfc_add_block_to_block (&se->pre, &argse.pre);
8619 865 : }
8620 :
8621 :
8622 : /* Intrinsic string comparison functions. */
8623 :
8624 : static void
8625 99 : gfc_conv_intrinsic_strcmp (gfc_se * se, gfc_expr * expr, enum tree_code op)
8626 : {
8627 99 : tree args[4];
8628 :
8629 99 : gfc_conv_intrinsic_function_args (se, expr, args, 4);
8630 :
8631 99 : se->expr
8632 198 : = gfc_build_compare_string (args[0], args[1], args[2], args[3],
8633 99 : expr->value.function.actual->expr->ts.kind,
8634 : op);
8635 99 : se->expr = fold_build2_loc (input_location, op,
8636 : gfc_typenode_for_spec (&expr->ts), se->expr,
8637 99 : build_int_cst (TREE_TYPE (se->expr), 0));
8638 99 : }
8639 :
8640 : /* Generate a call to the adjustl/adjustr library function. */
8641 : static void
8642 468 : gfc_conv_intrinsic_adjust (gfc_se * se, gfc_expr * expr, tree fndecl)
8643 : {
8644 468 : tree args[3];
8645 468 : tree len;
8646 468 : tree type;
8647 468 : tree var;
8648 468 : tree tmp;
8649 :
8650 468 : gfc_conv_intrinsic_function_args (se, expr, &args[1], 2);
8651 468 : len = args[1];
8652 :
8653 468 : type = TREE_TYPE (args[2]);
8654 468 : var = gfc_conv_string_tmp (se, type, len);
8655 468 : args[0] = var;
8656 :
8657 468 : tmp = build_call_expr_loc (input_location,
8658 : fndecl, 3, args[0], args[1], args[2]);
8659 468 : gfc_add_expr_to_block (&se->pre, tmp);
8660 468 : se->expr = var;
8661 468 : se->string_length = len;
8662 468 : }
8663 :
8664 :
8665 : /* Generate code for the TRANSFER intrinsic:
8666 : For scalar results:
8667 : DEST = TRANSFER (SOURCE, MOLD)
8668 : where:
8669 : typeof<DEST> = typeof<MOLD>
8670 : and:
8671 : MOLD is scalar.
8672 :
8673 : For array results:
8674 : DEST(1:N) = TRANSFER (SOURCE, MOLD[, SIZE])
8675 : where:
8676 : typeof<DEST> = typeof<MOLD>
8677 : and:
8678 : N = min (sizeof (SOURCE(:)), sizeof (DEST(:)),
8679 : sizeof (DEST(0) * SIZE). */
8680 : static void
8681 3991 : gfc_conv_intrinsic_transfer (gfc_se * se, gfc_expr * expr)
8682 : {
8683 3991 : tree tmp;
8684 3991 : tree tmpdecl;
8685 3991 : tree ptr;
8686 3991 : tree extent;
8687 3991 : tree source;
8688 3991 : tree source_type;
8689 3991 : tree source_bytes;
8690 3991 : tree mold_type;
8691 3991 : tree dest_word_len;
8692 3991 : tree size_words;
8693 3991 : tree size_bytes;
8694 3991 : tree upper;
8695 3991 : tree lower;
8696 3991 : tree stmt;
8697 3991 : tree class_ref = NULL_TREE;
8698 3991 : gfc_actual_arglist *arg;
8699 3991 : gfc_se argse;
8700 3991 : gfc_array_info *info;
8701 3991 : stmtblock_t block;
8702 3991 : int n;
8703 3991 : bool scalar_mold;
8704 3991 : gfc_expr *source_expr, *mold_expr, *class_expr;
8705 :
8706 3991 : info = NULL;
8707 3991 : if (se->loop)
8708 478 : info = &se->ss->info->data.array;
8709 :
8710 : /* Convert SOURCE. The output from this stage is:-
8711 : source_bytes = length of the source in bytes
8712 : source = pointer to the source data. */
8713 3991 : arg = expr->value.function.actual;
8714 3991 : source_expr = arg->expr;
8715 :
8716 : /* Ensure double transfer through LOGICAL preserves all
8717 : the needed bits. */
8718 3991 : if (arg->expr->expr_type == EXPR_FUNCTION
8719 2986 : && arg->expr->value.function.esym == NULL
8720 2962 : && arg->expr->value.function.isym != NULL
8721 2962 : && arg->expr->value.function.isym->id == GFC_ISYM_TRANSFER
8722 12 : && arg->expr->ts.type == BT_LOGICAL
8723 12 : && expr->ts.type != arg->expr->ts.type)
8724 12 : arg->expr->value.function.name = "__transfer_in_transfer";
8725 :
8726 3991 : gfc_init_se (&argse, NULL);
8727 :
8728 3991 : source_bytes = gfc_create_var (gfc_array_index_type, NULL);
8729 :
8730 : /* Obtain the pointer to source and the length of source in bytes. */
8731 3991 : if (arg->expr->rank == 0)
8732 : {
8733 3635 : gfc_conv_expr_reference (&argse, arg->expr);
8734 3635 : if (arg->expr->ts.type == BT_CLASS)
8735 : {
8736 37 : tmp = build_fold_indirect_ref_loc (input_location, argse.expr);
8737 37 : if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
8738 : {
8739 19 : source = gfc_class_data_get (tmp);
8740 19 : class_ref = tmp;
8741 : }
8742 : else
8743 : {
8744 : /* Array elements are evaluated as a reference to the data.
8745 : To obtain the vptr for the element size, the argument
8746 : expression must be stripped to the class reference and
8747 : re-evaluated. The pre and post blocks are not needed. */
8748 18 : gcc_assert (arg->expr->expr_type == EXPR_VARIABLE);
8749 18 : source = argse.expr;
8750 18 : class_expr = gfc_find_and_cut_at_last_class_ref (arg->expr);
8751 18 : gfc_init_se (&argse, NULL);
8752 18 : gfc_conv_expr (&argse, class_expr);
8753 18 : class_ref = argse.expr;
8754 : }
8755 : }
8756 : else
8757 3598 : source = argse.expr;
8758 :
8759 : /* Obtain the source word length. */
8760 3635 : switch (arg->expr->ts.type)
8761 : {
8762 300 : case BT_CHARACTER:
8763 300 : tmp = size_of_string_in_bytes (arg->expr->ts.kind,
8764 : argse.string_length);
8765 300 : break;
8766 37 : case BT_CLASS:
8767 37 : if (class_ref != NULL_TREE)
8768 : {
8769 37 : tmp = gfc_class_vtab_size_get (class_ref);
8770 37 : if (UNLIMITED_POLY (source_expr))
8771 30 : tmp = gfc_resize_class_size_with_len (NULL, class_ref, tmp);
8772 : }
8773 : else
8774 : {
8775 0 : tmp = gfc_class_vtab_size_get (argse.expr);
8776 0 : if (UNLIMITED_POLY (source_expr))
8777 0 : tmp = gfc_resize_class_size_with_len (NULL, argse.expr, tmp);
8778 : }
8779 : break;
8780 3298 : default:
8781 3298 : source_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8782 : source));
8783 3298 : tmp = fold_convert (gfc_array_index_type,
8784 : size_in_bytes (source_type));
8785 3298 : break;
8786 : }
8787 : }
8788 : else
8789 : {
8790 356 : bool simply_contiguous = gfc_is_simply_contiguous (arg->expr,
8791 : false, true);
8792 356 : argse.want_pointer = 0;
8793 : /* A non-contiguous SOURCE needs packing. */
8794 356 : if (!simply_contiguous)
8795 74 : argse.force_tmp = 1;
8796 356 : gfc_conv_expr_descriptor (&argse, arg->expr);
8797 356 : source = gfc_conv_descriptor_data_get (argse.expr);
8798 356 : source_type = gfc_get_element_type (TREE_TYPE (argse.expr));
8799 :
8800 : /* Repack the source if not simply contiguous. */
8801 356 : if (!simply_contiguous)
8802 : {
8803 74 : tmp = gfc_build_addr_expr (NULL_TREE, argse.expr);
8804 :
8805 74 : if (warn_array_temporaries)
8806 0 : gfc_warning (OPT_Warray_temporaries,
8807 : "Creating array temporary at %L", &expr->where);
8808 :
8809 74 : source = build_call_expr_loc (input_location,
8810 : gfor_fndecl_in_pack, 1, tmp);
8811 74 : source = gfc_evaluate_now (source, &argse.pre);
8812 :
8813 : /* Free the temporary. */
8814 74 : gfc_start_block (&block);
8815 74 : tmp = gfc_call_free (source);
8816 74 : gfc_add_expr_to_block (&block, tmp);
8817 74 : stmt = gfc_finish_block (&block);
8818 :
8819 : /* Clean up if it was repacked. */
8820 74 : gfc_init_block (&block);
8821 74 : tmp = gfc_conv_array_data (argse.expr);
8822 74 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
8823 : source, tmp);
8824 74 : tmp = build3_v (COND_EXPR, tmp, stmt,
8825 : build_empty_stmt (input_location));
8826 74 : gfc_add_expr_to_block (&block, tmp);
8827 74 : gfc_add_block_to_block (&block, &se->post);
8828 74 : gfc_init_block (&se->post);
8829 74 : gfc_add_block_to_block (&se->post, &block);
8830 : }
8831 :
8832 : /* Obtain the source word length. */
8833 356 : if (arg->expr->ts.type == BT_CHARACTER)
8834 144 : tmp = size_of_string_in_bytes (arg->expr->ts.kind,
8835 : argse.string_length);
8836 212 : else if (arg->expr->ts.type == BT_CLASS)
8837 : {
8838 54 : if (UNLIMITED_POLY (source_expr)
8839 54 : && DECL_LANG_SPECIFIC (source_expr->symtree->n.sym->backend_decl))
8840 12 : class_ref = GFC_DECL_SAVED_DESCRIPTOR
8841 : (source_expr->symtree->n.sym->backend_decl);
8842 : else
8843 42 : class_ref = TREE_OPERAND (argse.expr, 0);
8844 54 : tmp = gfc_class_vtab_size_get (class_ref);
8845 54 : if (UNLIMITED_POLY (arg->expr))
8846 54 : tmp = gfc_resize_class_size_with_len (&argse.pre, class_ref, tmp);
8847 : }
8848 : else
8849 158 : tmp = fold_convert (gfc_array_index_type,
8850 : size_in_bytes (source_type));
8851 :
8852 : /* Obtain the size of the array in bytes. */
8853 356 : extent = gfc_create_var (gfc_array_index_type, NULL);
8854 1098 : for (n = 0; n < arg->expr->rank; n++)
8855 : {
8856 386 : tree idx;
8857 386 : idx = gfc_rank_cst[n];
8858 386 : gfc_add_modify (&argse.pre, source_bytes, tmp);
8859 386 : lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
8860 386 : upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
8861 386 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
8862 : gfc_array_index_type, upper, lower);
8863 386 : gfc_add_modify (&argse.pre, extent, tmp);
8864 386 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
8865 : gfc_array_index_type, extent,
8866 : gfc_index_one_node);
8867 386 : tmp = fold_build2_loc (input_location, MULT_EXPR,
8868 : gfc_array_index_type, tmp, source_bytes);
8869 : }
8870 : }
8871 :
8872 3991 : gfc_add_modify (&argse.pre, source_bytes, tmp);
8873 3991 : gfc_add_block_to_block (&se->pre, &argse.pre);
8874 3991 : gfc_add_block_to_block (&se->post, &argse.post);
8875 :
8876 : /* Now convert MOLD. The outputs are:
8877 : mold_type = the TREE type of MOLD
8878 : dest_word_len = destination word length in bytes. */
8879 3991 : arg = arg->next;
8880 3991 : mold_expr = arg->expr;
8881 :
8882 3991 : gfc_init_se (&argse, NULL);
8883 :
8884 3991 : scalar_mold = arg->expr->rank == 0;
8885 :
8886 3991 : if (arg->expr->rank == 0)
8887 : {
8888 3662 : gfc_conv_expr_reference (&argse, mold_expr);
8889 3662 : mold_type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
8890 : argse.expr));
8891 : }
8892 : else
8893 : {
8894 329 : argse.want_pointer = 0;
8895 329 : gfc_conv_expr_descriptor (&argse, mold_expr);
8896 329 : mold_type = gfc_get_element_type (TREE_TYPE (argse.expr));
8897 : }
8898 :
8899 3991 : gfc_add_block_to_block (&se->pre, &argse.pre);
8900 3991 : gfc_add_block_to_block (&se->post, &argse.post);
8901 :
8902 3991 : if (strcmp (expr->value.function.name, "__transfer_in_transfer") == 0)
8903 : {
8904 : /* If this TRANSFER is nested in another TRANSFER, use a type
8905 : that preserves all bits. */
8906 12 : if (mold_expr->ts.type == BT_LOGICAL)
8907 12 : mold_type = gfc_get_int_type (mold_expr->ts.kind);
8908 : }
8909 :
8910 : /* Obtain the destination word length. */
8911 3991 : switch (mold_expr->ts.type)
8912 : {
8913 473 : case BT_CHARACTER:
8914 473 : tmp = size_of_string_in_bytes (mold_expr->ts.kind, argse.string_length);
8915 473 : mold_type = gfc_get_character_type_len (mold_expr->ts.kind,
8916 : argse.string_length);
8917 473 : break;
8918 6 : case BT_CLASS:
8919 6 : if (scalar_mold)
8920 6 : class_ref = argse.expr;
8921 : else
8922 0 : class_ref = TREE_OPERAND (argse.expr, 0);
8923 6 : tmp = gfc_class_vtab_size_get (class_ref);
8924 6 : if (UNLIMITED_POLY (arg->expr))
8925 0 : tmp = gfc_resize_class_size_with_len (&argse.pre, class_ref, tmp);
8926 : break;
8927 3512 : default:
8928 3512 : tmp = fold_convert (gfc_array_index_type, size_in_bytes (mold_type));
8929 3512 : break;
8930 : }
8931 :
8932 : /* Do not fix dest_word_len if it is a variable, since the temporary can wind
8933 : up being used before the assignment. */
8934 3991 : if (mold_expr->ts.type == BT_CHARACTER && mold_expr->ts.deferred)
8935 : dest_word_len = tmp;
8936 : else
8937 : {
8938 3937 : dest_word_len = gfc_create_var (gfc_array_index_type, NULL);
8939 3937 : gfc_add_modify (&se->pre, dest_word_len, tmp);
8940 : }
8941 :
8942 : /* Finally convert SIZE, if it is present. */
8943 3991 : arg = arg->next;
8944 3991 : size_words = gfc_create_var (gfc_array_index_type, NULL);
8945 :
8946 3991 : if (arg->expr)
8947 : {
8948 222 : gfc_init_se (&argse, NULL);
8949 222 : gfc_conv_expr_reference (&argse, arg->expr);
8950 222 : tmp = convert (gfc_array_index_type,
8951 : build_fold_indirect_ref_loc (input_location,
8952 : argse.expr));
8953 222 : gfc_add_block_to_block (&se->pre, &argse.pre);
8954 222 : gfc_add_block_to_block (&se->post, &argse.post);
8955 : }
8956 : else
8957 : tmp = NULL_TREE;
8958 :
8959 : /* Separate array and scalar results. */
8960 3991 : if (scalar_mold && tmp == NULL_TREE)
8961 3513 : goto scalar_transfer;
8962 :
8963 478 : size_bytes = gfc_create_var (gfc_array_index_type, NULL);
8964 478 : if (tmp != NULL_TREE)
8965 222 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8966 : tmp, dest_word_len);
8967 : else
8968 : tmp = source_bytes;
8969 :
8970 478 : gfc_add_modify (&se->pre, size_bytes, tmp);
8971 478 : gfc_add_modify (&se->pre, size_words,
8972 : fold_build2_loc (input_location, CEIL_DIV_EXPR,
8973 : gfc_array_index_type,
8974 : size_bytes, dest_word_len));
8975 :
8976 : /* Evaluate the bounds of the result. If the loop range exists, we have
8977 : to check if it is too large. If so, we modify loop->to be consistent
8978 : with min(size, size(source)). Otherwise, size is made consistent with
8979 : the loop range, so that the right number of bytes is transferred.*/
8980 478 : n = se->loop->order[0];
8981 478 : if (se->loop->to[n] != NULL_TREE)
8982 : {
8983 205 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8984 : se->loop->to[n], se->loop->from[n]);
8985 205 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8986 : tmp, gfc_index_one_node);
8987 205 : tmp = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
8988 : tmp, size_words);
8989 205 : gfc_add_modify (&se->pre, size_words, tmp);
8990 205 : gfc_add_modify (&se->pre, size_bytes,
8991 : fold_build2_loc (input_location, MULT_EXPR,
8992 : gfc_array_index_type,
8993 : size_words, dest_word_len));
8994 410 : upper = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
8995 205 : size_words, se->loop->from[n]);
8996 205 : upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
8997 : upper, gfc_index_one_node);
8998 : }
8999 : else
9000 : {
9001 273 : upper = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
9002 : size_words, gfc_index_one_node);
9003 273 : se->loop->from[n] = gfc_index_zero_node;
9004 : }
9005 :
9006 478 : se->loop->to[n] = upper;
9007 :
9008 : /* Build a destination descriptor, using the pointer, source, as the
9009 : data field. */
9010 478 : gfc_trans_create_temp_array (&se->pre, &se->post, se->ss, mold_type,
9011 : NULL_TREE, false, true, false, &expr->where);
9012 :
9013 : /* Cast the pointer to the result. */
9014 478 : tmp = gfc_conv_descriptor_data_get (info->descriptor);
9015 478 : tmp = fold_convert (pvoid_type_node, tmp);
9016 :
9017 : /* Use memcpy to do the transfer. */
9018 478 : tmp
9019 478 : = build_call_expr_loc (input_location,
9020 : builtin_decl_explicit (BUILT_IN_MEMCPY), 3, tmp,
9021 : fold_convert (pvoid_type_node, source),
9022 : fold_convert (size_type_node,
9023 : fold_build2_loc (input_location,
9024 : MIN_EXPR,
9025 : gfc_array_index_type,
9026 : size_bytes,
9027 : source_bytes)));
9028 478 : gfc_add_expr_to_block (&se->pre, tmp);
9029 :
9030 478 : se->expr = info->descriptor;
9031 478 : if (expr->ts.type == BT_CHARACTER)
9032 : {
9033 281 : tmp = fold_convert (gfc_charlen_type_node,
9034 : TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind)));
9035 281 : se->string_length = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
9036 : gfc_charlen_type_node,
9037 : dest_word_len, tmp);
9038 : }
9039 :
9040 478 : return;
9041 :
9042 : /* Deal with scalar results. */
9043 3513 : scalar_transfer:
9044 3513 : extent = fold_build2_loc (input_location, MIN_EXPR, gfc_array_index_type,
9045 : dest_word_len, source_bytes);
9046 3513 : extent = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
9047 : extent, gfc_index_zero_node);
9048 :
9049 3513 : if (expr->ts.type == BT_CHARACTER)
9050 : {
9051 192 : tree direct, indirect, free;
9052 :
9053 192 : ptr = convert (gfc_get_pchar_type (expr->ts.kind), source);
9054 192 : tmpdecl = gfc_create_var (gfc_get_pchar_type (expr->ts.kind),
9055 : "transfer");
9056 :
9057 : /* If source is longer than the destination, use a pointer to
9058 : the source directly. */
9059 192 : gfc_init_block (&block);
9060 192 : gfc_add_modify (&block, tmpdecl, ptr);
9061 192 : direct = gfc_finish_block (&block);
9062 :
9063 : /* Otherwise, allocate a string with the length of the destination
9064 : and copy the source into it. */
9065 192 : gfc_init_block (&block);
9066 192 : tmp = gfc_get_pchar_type (expr->ts.kind);
9067 192 : tmp = gfc_call_malloc (&block, tmp, dest_word_len);
9068 192 : gfc_add_modify (&block, tmpdecl,
9069 192 : fold_convert (TREE_TYPE (ptr), tmp));
9070 192 : tmp = build_call_expr_loc (input_location,
9071 : builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
9072 : fold_convert (pvoid_type_node, tmpdecl),
9073 : fold_convert (pvoid_type_node, ptr),
9074 : fold_convert (size_type_node, extent));
9075 192 : gfc_add_expr_to_block (&block, tmp);
9076 192 : indirect = gfc_finish_block (&block);
9077 :
9078 : /* Wrap it up with the condition. */
9079 192 : tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
9080 : dest_word_len, source_bytes);
9081 192 : tmp = build3_v (COND_EXPR, tmp, direct, indirect);
9082 192 : gfc_add_expr_to_block (&se->pre, tmp);
9083 :
9084 : /* Free the temporary string, if necessary. */
9085 192 : free = gfc_call_free (tmpdecl);
9086 192 : tmp = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9087 : dest_word_len, source_bytes);
9088 192 : tmp = build3_v (COND_EXPR, tmp, free, build_empty_stmt (input_location));
9089 192 : gfc_add_expr_to_block (&se->post, tmp);
9090 :
9091 192 : se->expr = tmpdecl;
9092 192 : tmp = fold_convert (gfc_charlen_type_node,
9093 : TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind)));
9094 192 : se->string_length = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
9095 : gfc_charlen_type_node,
9096 : dest_word_len, tmp);
9097 : }
9098 : else
9099 : {
9100 3321 : tmpdecl = gfc_create_var (mold_type, "transfer");
9101 :
9102 3321 : ptr = convert (build_pointer_type (mold_type), source);
9103 :
9104 : /* For CLASS results, allocate the needed memory first. */
9105 3321 : if (mold_expr->ts.type == BT_CLASS)
9106 : {
9107 6 : tree cdata;
9108 6 : cdata = gfc_class_data_get (tmpdecl);
9109 6 : tmp = gfc_call_malloc (&se->pre, TREE_TYPE (cdata), dest_word_len);
9110 6 : gfc_add_modify (&se->pre, cdata, tmp);
9111 : }
9112 :
9113 : /* Use memcpy to do the transfer. */
9114 3321 : if (mold_expr->ts.type == BT_CLASS)
9115 6 : tmp = gfc_class_data_get (tmpdecl);
9116 : else
9117 3315 : tmp = gfc_build_addr_expr (NULL_TREE, tmpdecl);
9118 :
9119 3321 : tmp = build_call_expr_loc (input_location,
9120 : builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
9121 : fold_convert (pvoid_type_node, tmp),
9122 : fold_convert (pvoid_type_node, ptr),
9123 : fold_convert (size_type_node, extent));
9124 3321 : gfc_add_expr_to_block (&se->pre, tmp);
9125 :
9126 : /* For CLASS results, set the _vptr. */
9127 3321 : if (mold_expr->ts.type == BT_CLASS)
9128 6 : gfc_reset_vptr (&se->pre, nullptr, tmpdecl, source_expr->ts.u.derived);
9129 :
9130 3321 : se->expr = tmpdecl;
9131 : }
9132 : }
9133 :
9134 :
9135 : /* Generate code for the ALLOCATED intrinsic.
9136 : Generate inline code that directly check the address of the argument. */
9137 :
9138 : static void
9139 7530 : gfc_conv_allocated (gfc_se *se, gfc_expr *expr)
9140 : {
9141 7530 : gfc_se arg1se;
9142 7530 : tree tmp;
9143 7530 : gfc_expr *e = expr->value.function.actual->expr;
9144 :
9145 7530 : gfc_init_se (&arg1se, NULL);
9146 7530 : if (e->ts.type == BT_CLASS)
9147 : {
9148 : /* Make sure that class array expressions have both a _data
9149 : component reference and an array reference.... */
9150 923 : if (CLASS_DATA (e)->attr.dimension)
9151 424 : gfc_add_class_array_ref (e);
9152 : /* .... whilst scalars only need the _data component. */
9153 : else
9154 499 : gfc_add_data_component (e);
9155 : }
9156 :
9157 7530 : gcc_assert (flag_coarray != GFC_FCOARRAY_LIB || !gfc_is_coindexed (e));
9158 :
9159 7530 : if (e->rank == 0)
9160 : {
9161 : /* Allocatable scalar. */
9162 2974 : arg1se.want_pointer = 1;
9163 2974 : gfc_conv_expr (&arg1se, e);
9164 2974 : tmp = arg1se.expr;
9165 : }
9166 : else
9167 : {
9168 : /* Allocatable array. */
9169 4556 : arg1se.descriptor_only = 1;
9170 4556 : gfc_conv_expr_descriptor (&arg1se, e);
9171 4556 : tmp = gfc_conv_descriptor_data_get (arg1se.expr);
9172 : }
9173 :
9174 7530 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
9175 7530 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
9176 :
9177 : /* Components of pointer array references sometimes come back with a pre block. */
9178 7530 : if (arg1se.pre.head)
9179 327 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9180 :
9181 7530 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
9182 7530 : }
9183 :
9184 :
9185 : /* Generate code for the ASSOCIATED intrinsic.
9186 : If both POINTER and TARGET are arrays, generate a call to library function
9187 : _gfor_associated, and pass descriptors of POINTER and TARGET to it.
9188 : In other cases, generate inline code that directly compare the address of
9189 : POINTER with the address of TARGET. */
9190 :
9191 : static void
9192 9743 : gfc_conv_associated (gfc_se *se, gfc_expr *expr)
9193 : {
9194 9743 : gfc_actual_arglist *arg1;
9195 9743 : gfc_actual_arglist *arg2;
9196 9743 : gfc_se arg1se;
9197 9743 : gfc_se arg2se;
9198 9743 : tree tmp2;
9199 9743 : tree tmp;
9200 9743 : tree nonzero_arraylen = NULL_TREE;
9201 9743 : gfc_ss *ss;
9202 9743 : bool scalar;
9203 :
9204 9743 : gfc_init_se (&arg1se, NULL);
9205 9743 : gfc_init_se (&arg2se, NULL);
9206 9743 : arg1 = expr->value.function.actual;
9207 9743 : arg2 = arg1->next;
9208 :
9209 : /* Check whether the expression is a scalar or not; we cannot use
9210 : arg1->expr->rank as it can be nonzero for proc pointers. */
9211 9743 : ss = gfc_walk_expr (arg1->expr);
9212 9743 : scalar = ss == gfc_ss_terminator;
9213 9743 : if (!scalar)
9214 3985 : gfc_free_ss_chain (ss);
9215 :
9216 9743 : if (!arg2->expr)
9217 : {
9218 : /* No optional target. */
9219 7298 : if (scalar)
9220 : {
9221 : /* A pointer to a scalar. */
9222 4831 : arg1se.want_pointer = 1;
9223 4831 : gfc_conv_expr (&arg1se, arg1->expr);
9224 4831 : if (arg1->expr->symtree->n.sym->attr.proc_pointer
9225 185 : && arg1->expr->symtree->n.sym->attr.dummy)
9226 78 : arg1se.expr = build_fold_indirect_ref_loc (input_location,
9227 : arg1se.expr);
9228 4831 : if (arg1->expr->ts.type == BT_CLASS)
9229 : {
9230 390 : tmp2 = gfc_class_data_get (arg1se.expr);
9231 390 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
9232 0 : tmp2 = gfc_conv_descriptor_data_get (tmp2);
9233 : }
9234 : else
9235 4441 : tmp2 = arg1se.expr;
9236 : }
9237 : else
9238 : {
9239 : /* A pointer to an array. */
9240 2467 : gfc_conv_expr_descriptor (&arg1se, arg1->expr);
9241 2467 : tmp2 = gfc_conv_descriptor_data_get (arg1se.expr);
9242 : }
9243 7298 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9244 7298 : gfc_add_block_to_block (&se->post, &arg1se.post);
9245 7298 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp2,
9246 7298 : fold_convert (TREE_TYPE (tmp2), null_pointer_node));
9247 7298 : se->expr = tmp;
9248 : }
9249 : else
9250 : {
9251 : /* An optional target. */
9252 2445 : if (arg2->expr->ts.type == BT_CLASS
9253 30 : && arg2->expr->expr_type != EXPR_FUNCTION)
9254 24 : gfc_add_data_component (arg2->expr);
9255 :
9256 2445 : if (scalar)
9257 : {
9258 : /* A pointer to a scalar. */
9259 927 : arg1se.want_pointer = 1;
9260 927 : gfc_conv_expr (&arg1se, arg1->expr);
9261 927 : if (arg1->expr->symtree->n.sym->attr.proc_pointer
9262 128 : && arg1->expr->symtree->n.sym->attr.dummy)
9263 42 : arg1se.expr = build_fold_indirect_ref_loc (input_location,
9264 : arg1se.expr);
9265 927 : if (arg1->expr->ts.type == BT_CLASS)
9266 254 : arg1se.expr = gfc_class_data_get (arg1se.expr);
9267 :
9268 927 : arg2se.want_pointer = 1;
9269 927 : gfc_conv_expr (&arg2se, arg2->expr);
9270 927 : if (arg2->expr->symtree->n.sym->attr.proc_pointer
9271 36 : && arg2->expr->symtree->n.sym->attr.dummy)
9272 0 : arg2se.expr = build_fold_indirect_ref_loc (input_location,
9273 : arg2se.expr);
9274 927 : if (arg2->expr->ts.type == BT_CLASS)
9275 : {
9276 6 : arg2se.expr = gfc_evaluate_now (arg2se.expr, &arg2se.pre);
9277 6 : arg2se.expr = gfc_class_data_get (arg2se.expr);
9278 : }
9279 927 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9280 927 : gfc_add_block_to_block (&se->post, &arg1se.post);
9281 927 : gfc_add_block_to_block (&se->pre, &arg2se.pre);
9282 927 : gfc_add_block_to_block (&se->post, &arg2se.post);
9283 927 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
9284 : arg1se.expr, arg2se.expr);
9285 927 : tmp2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9286 : arg1se.expr, null_pointer_node);
9287 927 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9288 : logical_type_node, tmp, tmp2);
9289 : }
9290 : else
9291 : {
9292 : /* An array pointer of zero length is not associated if target is
9293 : present. */
9294 1518 : arg1se.descriptor_only = 1;
9295 1518 : gfc_conv_expr_lhs (&arg1se, arg1->expr);
9296 1518 : if (arg1->expr->rank == -1)
9297 : {
9298 84 : tmp = gfc_conv_descriptor_rank_get (arg1se.expr);
9299 168 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
9300 84 : TREE_TYPE (tmp), tmp,
9301 84 : build_int_cst (TREE_TYPE (tmp), 1));
9302 : }
9303 : else
9304 1434 : tmp = gfc_rank_cst[arg1->expr->rank - 1];
9305 1518 : tmp = gfc_conv_descriptor_stride_get (arg1se.expr, tmp);
9306 1518 : if (arg2->expr->rank != 0)
9307 1488 : nonzero_arraylen = fold_build2_loc (input_location, NE_EXPR,
9308 : logical_type_node, tmp,
9309 1488 : build_int_cst (TREE_TYPE (tmp), 0));
9310 :
9311 : /* A pointer to an array, call library function _gfor_associated. */
9312 1518 : arg1se.want_pointer = 1;
9313 1518 : gfc_conv_expr_descriptor (&arg1se, arg1->expr);
9314 1518 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9315 1518 : gfc_add_block_to_block (&se->post, &arg1se.post);
9316 :
9317 1518 : arg2se.want_pointer = 1;
9318 1518 : arg2se.force_no_tmp = 1;
9319 1518 : if (arg2->expr->rank != 0)
9320 1488 : gfc_conv_expr_descriptor (&arg2se, arg2->expr);
9321 : else
9322 : {
9323 30 : gfc_conv_expr (&arg2se, arg2->expr);
9324 30 : arg2se.expr
9325 30 : = gfc_conv_scalar_to_descriptor (&arg2se, arg2se.expr,
9326 30 : gfc_expr_attr (arg2->expr));
9327 30 : arg2se.expr = gfc_build_addr_expr (NULL_TREE, arg2se.expr);
9328 : }
9329 1518 : gfc_add_block_to_block (&se->pre, &arg2se.pre);
9330 1518 : gfc_add_block_to_block (&se->post, &arg2se.post);
9331 1518 : se->expr = build_call_expr_loc (input_location,
9332 : gfor_fndecl_associated, 2,
9333 : arg1se.expr, arg2se.expr);
9334 1518 : se->expr = convert (logical_type_node, se->expr);
9335 1518 : if (arg2->expr->rank != 0)
9336 1488 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9337 : logical_type_node, se->expr,
9338 : nonzero_arraylen);
9339 : }
9340 :
9341 : /* If target is present zero character length pointers cannot
9342 : be associated. */
9343 2445 : if (arg1->expr->ts.type == BT_CHARACTER)
9344 : {
9345 631 : tmp = arg1se.string_length;
9346 631 : tmp = fold_build2_loc (input_location, NE_EXPR,
9347 : logical_type_node, tmp,
9348 631 : build_zero_cst (TREE_TYPE (tmp)));
9349 631 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9350 : logical_type_node, se->expr, tmp);
9351 : }
9352 : }
9353 :
9354 9743 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), se->expr);
9355 9743 : }
9356 :
9357 :
9358 : /* Generate code for the SAME_TYPE_AS intrinsic.
9359 : Generate inline code that directly checks the vindices. */
9360 :
9361 : static void
9362 409 : gfc_conv_same_type_as (gfc_se *se, gfc_expr *expr)
9363 : {
9364 409 : gfc_expr *a, *b;
9365 409 : gfc_se se1, se2;
9366 409 : tree tmp;
9367 409 : tree conda = NULL_TREE, condb = NULL_TREE;
9368 :
9369 409 : gfc_init_se (&se1, NULL);
9370 409 : gfc_init_se (&se2, NULL);
9371 :
9372 409 : a = expr->value.function.actual->expr;
9373 409 : b = expr->value.function.actual->next->expr;
9374 :
9375 409 : bool unlimited_poly_a = UNLIMITED_POLY (a);
9376 409 : bool unlimited_poly_b = UNLIMITED_POLY (b);
9377 409 : if (unlimited_poly_a)
9378 : {
9379 111 : se1.want_pointer = 1;
9380 111 : gfc_add_vptr_component (a);
9381 : }
9382 298 : else if (a->ts.type == BT_CLASS)
9383 : {
9384 256 : gfc_add_vptr_component (a);
9385 256 : gfc_add_hash_component (a);
9386 : }
9387 42 : else if (a->ts.type == BT_DERIVED)
9388 42 : a = gfc_get_int_expr (gfc_default_integer_kind, NULL,
9389 42 : a->ts.u.derived->hash_value);
9390 :
9391 409 : if (unlimited_poly_b)
9392 : {
9393 72 : se2.want_pointer = 1;
9394 72 : gfc_add_vptr_component (b);
9395 : }
9396 337 : else if (b->ts.type == BT_CLASS)
9397 : {
9398 169 : gfc_add_vptr_component (b);
9399 169 : gfc_add_hash_component (b);
9400 : }
9401 168 : else if (b->ts.type == BT_DERIVED)
9402 168 : b = gfc_get_int_expr (gfc_default_integer_kind, NULL,
9403 168 : b->ts.u.derived->hash_value);
9404 :
9405 409 : gfc_conv_expr (&se1, a);
9406 409 : gfc_conv_expr (&se2, b);
9407 :
9408 409 : if (unlimited_poly_a)
9409 : {
9410 111 : conda = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9411 : se1.expr,
9412 111 : build_int_cst (TREE_TYPE (se1.expr), 0));
9413 111 : se1.expr = gfc_vptr_hash_get (se1.expr);
9414 : }
9415 :
9416 409 : if (unlimited_poly_b)
9417 : {
9418 72 : condb = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9419 : se2.expr,
9420 72 : build_int_cst (TREE_TYPE (se2.expr), 0));
9421 72 : se2.expr = gfc_vptr_hash_get (se2.expr);
9422 : }
9423 :
9424 409 : tmp = fold_build2_loc (input_location, EQ_EXPR,
9425 : logical_type_node, se1.expr,
9426 409 : fold_convert (TREE_TYPE (se1.expr), se2.expr));
9427 :
9428 409 : if (conda)
9429 111 : tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9430 : logical_type_node, conda, tmp);
9431 :
9432 409 : if (condb)
9433 72 : tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
9434 : logical_type_node, condb, tmp);
9435 :
9436 409 : se->expr = convert (gfc_typenode_for_spec (&expr->ts), tmp);
9437 409 : }
9438 :
9439 :
9440 : /* Generate code for SELECTED_CHAR_KIND (NAME) intrinsic function. */
9441 :
9442 : static void
9443 42 : gfc_conv_intrinsic_sc_kind (gfc_se *se, gfc_expr *expr)
9444 : {
9445 42 : tree args[2];
9446 :
9447 42 : gfc_conv_intrinsic_function_args (se, expr, args, 2);
9448 42 : se->expr = build_call_expr_loc (input_location,
9449 : gfor_fndecl_sc_kind, 2, args[0], args[1]);
9450 42 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
9451 42 : }
9452 :
9453 :
9454 : /* Generate code for SELECTED_INT_KIND (R) intrinsic function. */
9455 :
9456 : static void
9457 45 : gfc_conv_intrinsic_si_kind (gfc_se *se, gfc_expr *expr)
9458 : {
9459 45 : tree arg, type;
9460 :
9461 45 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
9462 :
9463 : /* The argument to SELECTED_INT_KIND is INTEGER(4). */
9464 45 : type = gfc_get_int_type (4);
9465 45 : arg = gfc_build_addr_expr (NULL_TREE, fold_convert (type, arg));
9466 :
9467 : /* Convert it to the required type. */
9468 45 : type = gfc_typenode_for_spec (&expr->ts);
9469 45 : se->expr = build_call_expr_loc (input_location,
9470 : gfor_fndecl_si_kind, 1, arg);
9471 45 : se->expr = fold_convert (type, se->expr);
9472 45 : }
9473 :
9474 :
9475 : /* Generate code for SELECTED_LOGICAL_KIND (BITS) intrinsic function. */
9476 :
9477 : static void
9478 6 : gfc_conv_intrinsic_sl_kind (gfc_se *se, gfc_expr *expr)
9479 : {
9480 6 : tree arg, type;
9481 :
9482 6 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
9483 :
9484 : /* The argument to SELECTED_LOGICAL_KIND is INTEGER(4). */
9485 6 : type = gfc_get_int_type (4);
9486 6 : arg = gfc_build_addr_expr (NULL_TREE, fold_convert (type, arg));
9487 :
9488 : /* Convert it to the required type. */
9489 6 : type = gfc_typenode_for_spec (&expr->ts);
9490 6 : se->expr = build_call_expr_loc (input_location,
9491 : gfor_fndecl_sl_kind, 1, arg);
9492 6 : se->expr = fold_convert (type, se->expr);
9493 6 : }
9494 :
9495 :
9496 : /* Generate code for SELECTED_REAL_KIND (P, R, RADIX) intrinsic function. */
9497 :
9498 : static void
9499 82 : gfc_conv_intrinsic_sr_kind (gfc_se *se, gfc_expr *expr)
9500 : {
9501 82 : gfc_actual_arglist *actual;
9502 82 : tree type;
9503 82 : gfc_se argse;
9504 82 : vec<tree, va_gc> *args = NULL;
9505 :
9506 328 : for (actual = expr->value.function.actual; actual; actual = actual->next)
9507 : {
9508 246 : gfc_init_se (&argse, se);
9509 :
9510 : /* Pass a NULL pointer for an absent arg. */
9511 246 : if (actual->expr == NULL)
9512 96 : argse.expr = null_pointer_node;
9513 : else
9514 : {
9515 150 : gfc_typespec ts;
9516 150 : gfc_clear_ts (&ts);
9517 :
9518 150 : if (actual->expr->ts.kind != gfc_c_int_kind)
9519 : {
9520 : /* The arguments to SELECTED_REAL_KIND are INTEGER(4). */
9521 0 : ts.type = BT_INTEGER;
9522 0 : ts.kind = gfc_c_int_kind;
9523 0 : gfc_convert_type (actual->expr, &ts, 2);
9524 : }
9525 150 : gfc_conv_expr_reference (&argse, actual->expr);
9526 : }
9527 :
9528 246 : gfc_add_block_to_block (&se->pre, &argse.pre);
9529 246 : gfc_add_block_to_block (&se->post, &argse.post);
9530 246 : vec_safe_push (args, argse.expr);
9531 : }
9532 :
9533 : /* Convert it to the required type. */
9534 82 : type = gfc_typenode_for_spec (&expr->ts);
9535 82 : se->expr = build_call_expr_loc_vec (input_location,
9536 : gfor_fndecl_sr_kind, args);
9537 82 : se->expr = fold_convert (type, se->expr);
9538 82 : }
9539 :
9540 :
9541 : /* Generate code for TRIM (A) intrinsic function. */
9542 :
9543 : static void
9544 580 : gfc_conv_intrinsic_trim (gfc_se * se, gfc_expr * expr)
9545 : {
9546 580 : tree var;
9547 580 : tree len;
9548 580 : tree addr;
9549 580 : tree tmp;
9550 580 : tree cond;
9551 580 : tree fndecl;
9552 580 : tree function;
9553 580 : tree *args;
9554 580 : unsigned int num_args;
9555 :
9556 580 : num_args = gfc_intrinsic_argument_list_length (expr) + 2;
9557 580 : args = XALLOCAVEC (tree, num_args);
9558 :
9559 580 : var = gfc_create_var (gfc_get_pchar_type (expr->ts.kind), "pstr");
9560 580 : addr = gfc_build_addr_expr (ppvoid_type_node, var);
9561 580 : len = gfc_create_var (gfc_charlen_type_node, "len");
9562 :
9563 580 : gfc_conv_intrinsic_function_args (se, expr, &args[2], num_args - 2);
9564 580 : args[0] = gfc_build_addr_expr (NULL_TREE, len);
9565 580 : args[1] = addr;
9566 :
9567 580 : if (expr->ts.kind == 1)
9568 548 : function = gfor_fndecl_string_trim;
9569 32 : else if (expr->ts.kind == 4)
9570 32 : function = gfor_fndecl_string_trim_char4;
9571 : else
9572 0 : gcc_unreachable ();
9573 :
9574 580 : fndecl = build_addr (function);
9575 580 : tmp = build_call_array_loc (input_location,
9576 580 : TREE_TYPE (TREE_TYPE (function)), fndecl,
9577 : num_args, args);
9578 580 : gfc_add_expr_to_block (&se->pre, tmp);
9579 :
9580 : /* Free the temporary afterwards, if necessary. */
9581 580 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9582 580 : len, build_int_cst (TREE_TYPE (len), 0));
9583 580 : tmp = gfc_call_free (var);
9584 580 : tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt (input_location));
9585 580 : gfc_add_expr_to_block (&se->post, tmp);
9586 :
9587 580 : se->expr = var;
9588 580 : se->string_length = len;
9589 580 : }
9590 :
9591 :
9592 : /* Generate code for REPEAT (STRING, NCOPIES) intrinsic function. */
9593 :
9594 : static void
9595 541 : gfc_conv_intrinsic_repeat (gfc_se * se, gfc_expr * expr)
9596 : {
9597 541 : tree args[3], ncopies, dest, dlen, src, slen, ncopies_type;
9598 541 : tree type, cond, tmp, count, exit_label, n, max, largest;
9599 541 : tree size;
9600 541 : stmtblock_t block, body;
9601 541 : int i;
9602 :
9603 : /* We store in charsize the size of a character. */
9604 541 : i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
9605 541 : size = build_int_cst (sizetype, gfc_character_kinds[i].bit_size / 8);
9606 :
9607 : /* Get the arguments. */
9608 541 : gfc_conv_intrinsic_function_args (se, expr, args, 3);
9609 541 : slen = fold_convert (sizetype, gfc_evaluate_now (args[0], &se->pre));
9610 541 : src = args[1];
9611 541 : ncopies = gfc_evaluate_now (args[2], &se->pre);
9612 541 : ncopies_type = TREE_TYPE (ncopies);
9613 :
9614 : /* Check that NCOPIES is not negative. */
9615 541 : cond = fold_build2_loc (input_location, LT_EXPR, logical_type_node, ncopies,
9616 : build_int_cst (ncopies_type, 0));
9617 541 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
9618 : "Argument NCOPIES of REPEAT intrinsic is negative "
9619 : "(its value is %ld)",
9620 : fold_convert (long_integer_type_node, ncopies));
9621 :
9622 : /* If the source length is zero, any non negative value of NCOPIES
9623 : is valid, and nothing happens. */
9624 541 : n = gfc_create_var (ncopies_type, "ncopies");
9625 541 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, slen,
9626 : size_zero_node);
9627 541 : tmp = fold_build3_loc (input_location, COND_EXPR, ncopies_type, cond,
9628 : build_int_cst (ncopies_type, 0), ncopies);
9629 541 : gfc_add_modify (&se->pre, n, tmp);
9630 541 : ncopies = n;
9631 :
9632 : /* Check that ncopies is not too large: ncopies should be less than
9633 : (or equal to) MAX / slen, where MAX is the maximal integer of
9634 : the gfc_charlen_type_node type. If slen == 0, we need a special
9635 : case to avoid the division by zero. */
9636 541 : max = fold_build2_loc (input_location, TRUNC_DIV_EXPR, sizetype,
9637 541 : fold_convert (sizetype,
9638 : TYPE_MAX_VALUE (gfc_charlen_type_node)),
9639 : slen);
9640 1078 : largest = TYPE_PRECISION (sizetype) > TYPE_PRECISION (ncopies_type)
9641 541 : ? sizetype : ncopies_type;
9642 541 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
9643 : fold_convert (largest, ncopies),
9644 : fold_convert (largest, max));
9645 541 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, slen,
9646 : size_zero_node);
9647 541 : cond = fold_build3_loc (input_location, COND_EXPR, logical_type_node, tmp,
9648 : logical_false_node, cond);
9649 541 : gfc_trans_runtime_check (true, false, cond, &se->pre, &expr->where,
9650 : "Argument NCOPIES of REPEAT intrinsic is too large");
9651 :
9652 : /* Compute the destination length. */
9653 541 : dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_charlen_type_node,
9654 : fold_convert (gfc_charlen_type_node, slen),
9655 : fold_convert (gfc_charlen_type_node, ncopies));
9656 541 : type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
9657 541 : dest = gfc_conv_string_tmp (se, build_pointer_type (type), dlen);
9658 :
9659 : /* Generate the code to do the repeat operation:
9660 : for (i = 0; i < ncopies; i++)
9661 : memmove (dest + (i * slen * size), src, slen*size); */
9662 541 : gfc_start_block (&block);
9663 541 : count = gfc_create_var (sizetype, "count");
9664 541 : gfc_add_modify (&block, count, size_zero_node);
9665 541 : exit_label = gfc_build_label_decl (NULL_TREE);
9666 :
9667 : /* Start the loop body. */
9668 541 : gfc_start_block (&body);
9669 :
9670 : /* Exit the loop if count >= ncopies. */
9671 541 : cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, count,
9672 : fold_convert (sizetype, ncopies));
9673 541 : tmp = build1_v (GOTO_EXPR, exit_label);
9674 541 : TREE_USED (exit_label) = 1;
9675 541 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
9676 : build_empty_stmt (input_location));
9677 541 : gfc_add_expr_to_block (&body, tmp);
9678 :
9679 : /* Call memmove (dest + (i*slen*size), src, slen*size). */
9680 541 : tmp = fold_build2_loc (input_location, MULT_EXPR, sizetype, slen,
9681 : count);
9682 541 : tmp = fold_build2_loc (input_location, MULT_EXPR, sizetype, tmp,
9683 : size);
9684 541 : tmp = fold_build_pointer_plus_loc (input_location,
9685 : fold_convert (pvoid_type_node, dest), tmp);
9686 541 : tmp = build_call_expr_loc (input_location,
9687 : builtin_decl_explicit (BUILT_IN_MEMMOVE),
9688 : 3, tmp, src,
9689 : fold_build2_loc (input_location, MULT_EXPR,
9690 : size_type_node, slen, size));
9691 541 : gfc_add_expr_to_block (&body, tmp);
9692 :
9693 : /* Increment count. */
9694 541 : tmp = fold_build2_loc (input_location, PLUS_EXPR, sizetype,
9695 : count, size_one_node);
9696 541 : gfc_add_modify (&body, count, tmp);
9697 :
9698 : /* Build the loop. */
9699 541 : tmp = build1_v (LOOP_EXPR, gfc_finish_block (&body));
9700 541 : gfc_add_expr_to_block (&block, tmp);
9701 :
9702 : /* Add the exit label. */
9703 541 : tmp = build1_v (LABEL_EXPR, exit_label);
9704 541 : gfc_add_expr_to_block (&block, tmp);
9705 :
9706 : /* Finish the block. */
9707 541 : tmp = gfc_finish_block (&block);
9708 541 : gfc_add_expr_to_block (&se->pre, tmp);
9709 :
9710 : /* Set the result value. */
9711 541 : se->expr = dest;
9712 541 : se->string_length = dlen;
9713 541 : }
9714 :
9715 :
9716 : /* Generate code for the IARGC intrinsic. */
9717 :
9718 : static void
9719 12 : gfc_conv_intrinsic_iargc (gfc_se * se, gfc_expr * expr)
9720 : {
9721 12 : tree tmp;
9722 12 : tree fndecl;
9723 12 : tree type;
9724 :
9725 : /* Call the library function. This always returns an INTEGER(4). */
9726 12 : fndecl = gfor_fndecl_iargc;
9727 12 : tmp = build_call_expr_loc (input_location,
9728 : fndecl, 0);
9729 :
9730 : /* Convert it to the required type. */
9731 12 : type = gfc_typenode_for_spec (&expr->ts);
9732 12 : tmp = fold_convert (type, tmp);
9733 :
9734 12 : se->expr = tmp;
9735 12 : }
9736 :
9737 :
9738 : /* Generate code for the KILL intrinsic. */
9739 :
9740 : static void
9741 8 : conv_intrinsic_kill (gfc_se *se, gfc_expr *expr)
9742 : {
9743 8 : tree *args;
9744 8 : tree int4_type_node = gfc_get_int_type (4);
9745 8 : tree pid;
9746 8 : tree sig;
9747 8 : tree tmp;
9748 8 : unsigned int num_args;
9749 :
9750 8 : num_args = gfc_intrinsic_argument_list_length (expr);
9751 8 : args = XALLOCAVEC (tree, num_args);
9752 8 : gfc_conv_intrinsic_function_args (se, expr, args, num_args);
9753 :
9754 : /* Convert PID to a INTEGER(4) entity. */
9755 8 : pid = convert (int4_type_node, args[0]);
9756 :
9757 : /* Convert SIG to a INTEGER(4) entity. */
9758 8 : sig = convert (int4_type_node, args[1]);
9759 :
9760 8 : tmp = build_call_expr_loc (input_location, gfor_fndecl_kill, 2, pid, sig);
9761 :
9762 8 : se->expr = fold_convert (TREE_TYPE (args[0]), tmp);
9763 8 : }
9764 :
9765 :
9766 : static tree
9767 15 : conv_intrinsic_kill_sub (gfc_code *code)
9768 : {
9769 15 : stmtblock_t block;
9770 15 : gfc_se se, se_stat;
9771 15 : tree int4_type_node = gfc_get_int_type (4);
9772 15 : tree pid;
9773 15 : tree sig;
9774 15 : tree statp;
9775 15 : tree tmp;
9776 :
9777 : /* Make the function call. */
9778 15 : gfc_init_block (&block);
9779 15 : gfc_init_se (&se, NULL);
9780 :
9781 : /* Convert PID to a INTEGER(4) entity. */
9782 15 : gfc_conv_expr (&se, code->ext.actual->expr);
9783 15 : gfc_add_block_to_block (&block, &se.pre);
9784 15 : pid = fold_convert (int4_type_node, gfc_evaluate_now (se.expr, &block));
9785 15 : gfc_add_block_to_block (&block, &se.post);
9786 :
9787 : /* Convert SIG to a INTEGER(4) entity. */
9788 15 : gfc_conv_expr (&se, code->ext.actual->next->expr);
9789 15 : gfc_add_block_to_block (&block, &se.pre);
9790 15 : sig = fold_convert (int4_type_node, gfc_evaluate_now (se.expr, &block));
9791 15 : gfc_add_block_to_block (&block, &se.post);
9792 :
9793 : /* Deal with an optional STATUS. */
9794 15 : if (code->ext.actual->next->next->expr)
9795 : {
9796 10 : gfc_init_se (&se_stat, NULL);
9797 10 : gfc_conv_expr (&se_stat, code->ext.actual->next->next->expr);
9798 10 : statp = gfc_create_var (gfc_get_int_type (4), "_statp");
9799 : }
9800 : else
9801 : statp = NULL_TREE;
9802 :
9803 25 : tmp = build_call_expr_loc (input_location, gfor_fndecl_kill_sub, 3, pid, sig,
9804 10 : statp ? gfc_build_addr_expr (NULL_TREE, statp) : null_pointer_node);
9805 :
9806 15 : gfc_add_expr_to_block (&block, tmp);
9807 :
9808 15 : if (statp && statp != se_stat.expr)
9809 10 : gfc_add_modify (&block, se_stat.expr,
9810 10 : fold_convert (TREE_TYPE (se_stat.expr), statp));
9811 :
9812 15 : return gfc_finish_block (&block);
9813 : }
9814 :
9815 :
9816 :
9817 : /* The loc intrinsic returns the address of its argument as
9818 : gfc_index_integer_kind integer. */
9819 :
9820 : static void
9821 8993 : gfc_conv_intrinsic_loc (gfc_se * se, gfc_expr * expr)
9822 : {
9823 8993 : tree temp_var;
9824 8993 : gfc_expr *arg_expr;
9825 :
9826 8993 : gcc_assert (!se->ss);
9827 :
9828 8993 : arg_expr = expr->value.function.actual->expr;
9829 8993 : if (arg_expr->rank == 0)
9830 : {
9831 6575 : if (arg_expr->ts.type == BT_CLASS)
9832 18 : gfc_add_data_component (arg_expr);
9833 6575 : gfc_conv_expr_reference (se, arg_expr);
9834 : }
9835 2418 : else if (gfc_is_simply_contiguous (arg_expr, false, false))
9836 2380 : gfc_conv_array_parameter (se, arg_expr, true, NULL, NULL, NULL);
9837 : else
9838 : {
9839 38 : gfc_conv_expr_descriptor (se, arg_expr);
9840 38 : se->expr = gfc_conv_descriptor_data_get (se->expr);
9841 : }
9842 8993 : se->expr = convert (gfc_get_int_type (gfc_index_integer_kind), se->expr);
9843 8993 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
9844 :
9845 : /* Create a temporary variable for loc return value. Without this,
9846 : we get an error an ICE in gcc/expr.cc(expand_expr_addr_expr_1). */
9847 8993 : temp_var = gfc_create_var (gfc_get_int_type (gfc_index_integer_kind), NULL);
9848 8993 : gfc_add_modify (&se->pre, temp_var, se->expr);
9849 8993 : se->expr = temp_var;
9850 8993 : }
9851 :
9852 : /* The following routine generates code for the intrinsic functions from
9853 : the ISO_C_BINDING module: C_LOC, C_FUNLOC, C_ASSOCIATED, and
9854 : F_C_STRING. */
9855 :
9856 : static void
9857 9949 : conv_isocbinding_function (gfc_se *se, gfc_expr *expr)
9858 : {
9859 9949 : gfc_actual_arglist *arg = expr->value.function.actual;
9860 :
9861 9949 : if (expr->value.function.isym->id == GFC_ISYM_C_LOC)
9862 : {
9863 7559 : if (arg->expr->rank == 0)
9864 2010 : gfc_conv_expr_reference (se, arg->expr);
9865 5549 : else if (gfc_is_simply_contiguous (arg->expr, false, false))
9866 4465 : gfc_conv_array_parameter (se, arg->expr, true, NULL, NULL, NULL);
9867 : else
9868 : {
9869 1084 : gfc_conv_expr_descriptor (se, arg->expr);
9870 1084 : se->expr = gfc_conv_descriptor_data_get (se->expr);
9871 : }
9872 :
9873 : /* TODO -- the following two lines shouldn't be necessary, but if
9874 : they're removed, a bug is exposed later in the code path.
9875 : This workaround was thus introduced, but will have to be
9876 : removed; please see PR 35150 for details about the issue. */
9877 7559 : se->expr = convert (pvoid_type_node, se->expr);
9878 7559 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
9879 : }
9880 2390 : else if (expr->value.function.isym->id == GFC_ISYM_C_FUNLOC)
9881 : {
9882 260 : gfc_conv_expr_reference (se, arg->expr);
9883 260 : if (arg->expr->symtree->n.sym->attr.proc_pointer
9884 29 : && arg->expr->symtree->n.sym->attr.dummy)
9885 7 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
9886 : /* The code below is necessary to create a reference from the calling
9887 : subprogram to the argument of C_FUNLOC() in the call graph.
9888 : Please see PR 117303 for more details. */
9889 260 : se->expr = convert (pvoid_type_node, se->expr);
9890 260 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
9891 : }
9892 2130 : else if (expr->value.function.isym->id == GFC_ISYM_C_ASSOCIATED)
9893 : {
9894 2054 : gfc_se arg1se;
9895 2054 : gfc_se arg2se;
9896 :
9897 : /* Build the addr_expr for the first argument. The argument is
9898 : already an *address* so we don't need to set want_pointer in
9899 : the gfc_se. */
9900 2054 : gfc_init_se (&arg1se, NULL);
9901 2054 : gfc_conv_expr (&arg1se, arg->expr);
9902 2054 : gfc_add_block_to_block (&se->pre, &arg1se.pre);
9903 2054 : gfc_add_block_to_block (&se->post, &arg1se.post);
9904 :
9905 : /* See if we were given two arguments. */
9906 2054 : if (arg->next->expr == NULL)
9907 : /* Only given one arg so generate a null and do a
9908 : not-equal comparison against the first arg. */
9909 1675 : se->expr = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
9910 : arg1se.expr,
9911 1675 : fold_convert (TREE_TYPE (arg1se.expr),
9912 : null_pointer_node));
9913 : else
9914 : {
9915 379 : tree eq_expr;
9916 379 : tree not_null_expr;
9917 :
9918 : /* Given two arguments so build the arg2se from second arg. */
9919 379 : gfc_init_se (&arg2se, NULL);
9920 379 : gfc_conv_expr (&arg2se, arg->next->expr);
9921 379 : gfc_add_block_to_block (&se->pre, &arg2se.pre);
9922 379 : gfc_add_block_to_block (&se->post, &arg2se.post);
9923 :
9924 : /* Generate test to compare that the two args are equal. */
9925 379 : eq_expr = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
9926 : arg1se.expr, arg2se.expr);
9927 : /* Generate test to ensure that the first arg is not null. */
9928 379 : not_null_expr = fold_build2_loc (input_location, NE_EXPR,
9929 : logical_type_node,
9930 : arg1se.expr, null_pointer_node);
9931 :
9932 : /* Finally, the generated test must check that both arg1 is not
9933 : NULL and that it is equal to the second arg. */
9934 379 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
9935 : logical_type_node,
9936 : not_null_expr, eq_expr);
9937 : }
9938 : }
9939 76 : else if (expr->value.function.isym->id == GFC_ISYM_F_C_STRING)
9940 : {
9941 : /* There are three cases:
9942 : f_c_string(string) -> trim(string) // c_null_char
9943 : f_c_string(string, .false.) -> trim(string) // c_null_char
9944 : f_c_string(string, .true.) -> string // c_null_char */
9945 :
9946 76 : gfc_expr *string = arg->expr;
9947 76 : gfc_expr *asis = arg->next->expr;
9948 76 : bool need_asis = false, need_trim = false;
9949 76 : gfc_se asis_se;
9950 :
9951 76 : if (!asis)
9952 : {
9953 : need_trim = true;
9954 : need_asis = false;
9955 : }
9956 54 : else if (asis->expr_type == EXPR_CONSTANT)
9957 : {
9958 32 : need_asis = asis->value.logical;
9959 32 : need_trim = !need_asis;
9960 : }
9961 : else
9962 : {
9963 : /* A conditional expression is needed. */
9964 22 : need_asis = true;
9965 22 : need_trim = true;
9966 22 : gfc_init_se (&asis_se, se);
9967 22 : gfc_conv_expr (&asis_se, asis);
9968 22 : if (asis->expr_type == EXPR_VARIABLE
9969 22 : && asis->symtree->n.sym->attr.dummy
9970 10 : && asis->symtree->n.sym->attr.optional)
9971 : {
9972 6 : tree present = gfc_conv_expr_present (asis->symtree->n.sym);
9973 6 : asis_se.expr
9974 6 : = build3_loc (input_location, COND_EXPR,
9975 : logical_type_node, present,
9976 : asis_se.expr, logical_false_node);
9977 : }
9978 22 : gfc_make_safe_expr (&asis_se);
9979 : }
9980 :
9981 : /* Handle the case of a constant string argument first. */
9982 76 : if (string->expr_type == EXPR_CONSTANT)
9983 : {
9984 : /* Output for the asis "then" case goes tlen/tstr, and the
9985 : trimmed case in elen/estr. */
9986 34 : tree elen, estr, tlen, tstr;
9987 34 : elen = estr = tlen = tstr = NULL_TREE;
9988 :
9989 34 : gfc_char_t *orig_string = string->value.character.string;
9990 34 : gfc_charlen_t orig_len = string->value.character.length;
9991 34 : gfc_charlen_t n;
9992 34 : gfc_char_t *buf
9993 34 : = (gfc_char_t *) alloca ((orig_len + 1) * sizeof (gfc_char_t));
9994 34 : memcpy (buf, orig_string, orig_len * sizeof (gfc_char_t));
9995 34 : buf[orig_len] = '\0';
9996 34 : int kind = gfc_default_character_kind;
9997 34 : gcc_assert (string->ts.kind == kind);
9998 :
9999 : /* Build the new string constant(s). */
10000 34 : if (need_asis)
10001 : {
10002 14 : tstr = gfc_build_wide_string_const (kind, orig_len + 1, buf);
10003 14 : tlen = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tstr)));
10004 14 : if (!need_trim)
10005 : {
10006 10 : se->expr = tstr;
10007 10 : se->string_length = tlen;
10008 10 : return;
10009 : }
10010 : }
10011 24 : if (need_trim)
10012 : {
10013 72 : for (n = orig_len; n; n--)
10014 72 : if (buf[n - 1] != ' ')
10015 : break;
10016 24 : buf[n] = '\0';
10017 24 : if (need_asis && n == orig_len)
10018 : {
10019 : /* Special case; trimming is a no-op. Add side-effects
10020 : from the condition and then just return the string
10021 : without a conditional. */
10022 2 : gfc_add_block_to_block (&se->pre, &asis_se.pre);
10023 2 : se->expr = tstr;
10024 2 : se->string_length = tlen;
10025 2 : return;
10026 : }
10027 : else
10028 : {
10029 22 : estr = gfc_build_wide_string_const (kind, n + 1, buf);
10030 22 : elen = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (estr)));
10031 : }
10032 22 : if (!need_asis)
10033 : {
10034 20 : se->expr = estr;
10035 20 : se->string_length = elen;
10036 20 : return;
10037 : }
10038 : }
10039 0 : gcc_assert (need_asis && need_trim);
10040 2 : gfc_add_block_to_block (&se->pre, &asis_se.pre);
10041 2 : se->expr
10042 2 : = fold_build3_loc (input_location, COND_EXPR,
10043 : pchar_type_node, asis_se.expr,
10044 : tstr, estr);
10045 2 : se->string_length
10046 2 : = fold_build3_loc (input_location, COND_EXPR,
10047 : gfc_charlen_type_node, asis_se.expr,
10048 : tlen, elen);
10049 2 : return;
10050 : }
10051 : else
10052 : /* We have to generate code to do the string transformation(s) at
10053 : runtime. */
10054 : {
10055 42 : tree tmp;
10056 :
10057 : /* Convert input string. */
10058 42 : gfc_se sse;
10059 42 : gfc_init_se (&sse, se);
10060 42 : gfc_conv_expr (&sse, string);
10061 42 : gfc_conv_string_parameter (&sse);
10062 42 : gfc_make_safe_expr (&sse);
10063 42 : gfc_add_block_to_block (&se->pre, &sse.pre);
10064 :
10065 : /* Use a temporary for the (possibly trimmed) string length. */
10066 42 : tree lenvar = gfc_create_var (gfc_charlen_type_node, NULL);
10067 42 : gfc_add_modify (&se->pre, lenvar, sse.string_length);
10068 :
10069 : /* Build the expression for a call to LEN_TRIM if we may need
10070 : to trim the string. If it's conditional, handle that too. */
10071 42 : if (need_trim)
10072 : {
10073 36 : tree trimlen
10074 36 : = build_call_expr_loc (input_location,
10075 : gfor_fndecl_string_len_trim, 2,
10076 : lenvar, sse.expr);
10077 36 : if (need_asis)
10078 : {
10079 18 : gfc_add_block_to_block (&se->pre, &asis_se.pre);
10080 18 : tmp = fold_build3_loc (input_location, COND_EXPR,
10081 : gfc_charlen_type_node, asis_se.expr,
10082 : lenvar, trimlen);
10083 18 : gfc_add_modify (&se->pre, lenvar, tmp);
10084 : }
10085 : else
10086 18 : gfc_add_modify (&se->pre, lenvar, trimlen);
10087 : }
10088 :
10089 : /* Allocate a new string newvar that is lenvar+1 bytes long.
10090 : memcpy the first lenvar bytes from the input string, and
10091 : add a null character. Note that lenvar, the length of
10092 : the (trimmed) original string, has type gfc_charlen_type_node,
10093 : but newlen is size_type_node. */
10094 42 : tree string_type_node = build_pointer_type (char_type_node);
10095 42 : tree newvar = gfc_create_var (string_type_node, NULL);
10096 42 : tree newlen = fold_build2_loc (input_location, PLUS_EXPR,
10097 : size_type_node,
10098 : fold_convert (size_type_node,
10099 : lenvar),
10100 : size_one_node);
10101 42 : gfc_add_modify (&se->pre, newvar,
10102 : gfc_call_malloc (&se->pre, string_type_node,
10103 : newlen));
10104 42 : tmp = build_call_expr_loc (input_location,
10105 : builtin_decl_explicit (BUILT_IN_MEMCPY),
10106 : 3,
10107 : fold_convert (pvoid_type_node, newvar),
10108 : fold_convert (pvoid_type_node, sse.expr),
10109 : fold_convert (size_type_node, lenvar));
10110 42 : gfc_add_expr_to_block (&se->pre, tmp);
10111 42 : tmp = fold_build2_loc (input_location, POINTER_PLUS_EXPR,
10112 : string_type_node, newvar,
10113 : fold_convert (size_type_node, lenvar));
10114 42 : tmp = fold_build1_loc (input_location, INDIRECT_REF,
10115 : char_type_node, tmp);
10116 42 : gfc_add_modify (&se->pre, tmp,
10117 : fold_convert (char_type_node, integer_zero_node));
10118 :
10119 : /* Remember to free the string later. */
10120 42 : tmp = gfc_call_free (newvar);
10121 42 : gfc_add_expr_to_block (&se->post, tmp);
10122 :
10123 : /* Return the result. */
10124 42 : se->expr = newvar;
10125 42 : se->string_length = fold_convert (gfc_charlen_type_node, newlen);
10126 42 : return;
10127 : }
10128 : }
10129 : else
10130 0 : gcc_unreachable ();
10131 : }
10132 :
10133 :
10134 : /* The following routine generates code for the intrinsic
10135 : subroutines from the ISO_C_BINDING module:
10136 : * C_F_POINTER
10137 : * C_F_PROCPOINTER. */
10138 :
10139 : static tree
10140 3370 : conv_isocbinding_subroutine (gfc_code *code)
10141 : {
10142 3370 : gfc_expr *cptr, *fptr, *shape, *lower;
10143 3370 : gfc_se se, cptrse, fptrse, shapese, lowerse;
10144 3370 : gfc_ss *shape_ss, *lower_ss;
10145 3370 : tree desc, dim, tmp, stride, offset, lbound, ubound;
10146 3370 : stmtblock_t body, block;
10147 3370 : gfc_loopinfo loop;
10148 3370 : gfc_actual_arglist *arg;
10149 :
10150 3370 : arg = code->ext.actual;
10151 3370 : cptr = arg->expr;
10152 3370 : fptr = arg->next->expr;
10153 3370 : shape = arg->next->next ? arg->next->next->expr : NULL;
10154 3288 : lower = shape && arg->next->next->next ? arg->next->next->next->expr : NULL;
10155 :
10156 3370 : gfc_init_se (&se, NULL);
10157 3370 : gfc_init_se (&cptrse, NULL);
10158 3370 : gfc_conv_expr (&cptrse, cptr);
10159 3370 : gfc_add_block_to_block (&se.pre, &cptrse.pre);
10160 3370 : gfc_add_block_to_block (&se.post, &cptrse.post);
10161 :
10162 3370 : gfc_init_se (&fptrse, NULL);
10163 3370 : if (fptr->rank == 0)
10164 : {
10165 2884 : fptrse.want_pointer = 1;
10166 2884 : gfc_conv_expr (&fptrse, fptr);
10167 2884 : gfc_add_block_to_block (&se.pre, &fptrse.pre);
10168 2884 : gfc_add_block_to_block (&se.post, &fptrse.post);
10169 2884 : if (fptr->symtree->n.sym->attr.proc_pointer
10170 81 : && fptr->symtree->n.sym->attr.dummy)
10171 19 : fptrse.expr = build_fold_indirect_ref_loc (input_location, fptrse.expr);
10172 2884 : se.expr
10173 2884 : = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (fptrse.expr),
10174 : fptrse.expr,
10175 2884 : fold_convert (TREE_TYPE (fptrse.expr), cptrse.expr));
10176 2884 : gfc_add_expr_to_block (&se.pre, se.expr);
10177 2884 : gfc_add_block_to_block (&se.pre, &se.post);
10178 2884 : return gfc_finish_block (&se.pre);
10179 : }
10180 :
10181 486 : gfc_start_block (&block);
10182 :
10183 : /* Get the descriptor of the Fortran pointer. */
10184 486 : fptrse.descriptor_only = 1;
10185 486 : gfc_conv_expr_descriptor (&fptrse, fptr);
10186 486 : gfc_add_block_to_block (&block, &fptrse.pre);
10187 486 : desc = fptrse.expr;
10188 :
10189 : /* Set the span field. */
10190 486 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
10191 486 : tmp = fold_convert (gfc_array_index_type, tmp);
10192 486 : gfc_conv_descriptor_span_set (&block, desc, tmp);
10193 :
10194 : /* Set data value, dtype, and offset. */
10195 486 : tmp = GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (desc));
10196 486 : gfc_conv_descriptor_data_set (&block, desc, fold_convert (tmp, cptrse.expr));
10197 486 : gfc_conv_descriptor_dtype_set (&block, desc,
10198 486 : gfc_get_dtype (TREE_TYPE (desc)));
10199 :
10200 : /* Start scalarization of the bounds, using the shape argument. */
10201 :
10202 486 : shape_ss = gfc_walk_expr (shape);
10203 486 : gcc_assert (shape_ss != gfc_ss_terminator);
10204 486 : gfc_init_se (&shapese, NULL);
10205 486 : if (lower)
10206 : {
10207 12 : lower_ss = gfc_walk_expr (lower);
10208 12 : gcc_assert (lower_ss != gfc_ss_terminator);
10209 12 : gfc_init_se (&lowerse, NULL);
10210 : }
10211 :
10212 486 : gfc_init_loopinfo (&loop);
10213 486 : gfc_add_ss_to_loop (&loop, shape_ss);
10214 486 : if (lower)
10215 12 : gfc_add_ss_to_loop (&loop, lower_ss);
10216 486 : gfc_conv_ss_startstride (&loop);
10217 486 : gfc_conv_loop_setup (&loop, &fptr->where);
10218 486 : gfc_mark_ss_chain_used (shape_ss, 1);
10219 486 : if (lower)
10220 12 : gfc_mark_ss_chain_used (lower_ss, 1);
10221 :
10222 486 : gfc_copy_loopinfo_to_se (&shapese, &loop);
10223 486 : shapese.ss = shape_ss;
10224 486 : if (lower)
10225 : {
10226 12 : gfc_copy_loopinfo_to_se (&lowerse, &loop);
10227 12 : lowerse.ss = lower_ss;
10228 : }
10229 :
10230 486 : stride = gfc_create_var (gfc_array_index_type, "stride");
10231 486 : offset = gfc_create_var (gfc_array_index_type, "offset");
10232 486 : gfc_add_modify (&block, stride, gfc_index_one_node);
10233 486 : gfc_add_modify (&block, offset, gfc_index_zero_node);
10234 :
10235 : /* Loop body. */
10236 486 : gfc_start_scalarized_body (&loop, &body);
10237 :
10238 486 : dim = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
10239 : loop.loopvar[0], loop.from[0]);
10240 :
10241 486 : if (lower)
10242 : {
10243 12 : gfc_conv_expr (&lowerse, lower);
10244 12 : gfc_add_block_to_block (&body, &lowerse.pre);
10245 12 : lbound = fold_convert (gfc_array_index_type, lowerse.expr);
10246 12 : gfc_add_block_to_block (&body, &lowerse.post);
10247 : }
10248 : else
10249 474 : lbound = gfc_index_one_node;
10250 :
10251 : /* Set bounds and stride. */
10252 486 : gfc_conv_descriptor_lbound_set (&body, desc, dim, lbound);
10253 486 : gfc_conv_descriptor_stride_set (&body, desc, dim, stride);
10254 :
10255 486 : gfc_conv_expr (&shapese, shape);
10256 486 : gfc_add_block_to_block (&body, &shapese.pre);
10257 486 : ubound = fold_build2_loc (
10258 : input_location, MINUS_EXPR, gfc_array_index_type,
10259 : fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, lbound,
10260 : fold_convert (gfc_array_index_type, shapese.expr)),
10261 : gfc_index_one_node);
10262 486 : gfc_conv_descriptor_ubound_set (&body, desc, dim, ubound);
10263 486 : gfc_add_block_to_block (&body, &shapese.post);
10264 :
10265 : /* Calculate offset. */
10266 486 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
10267 : stride, lbound);
10268 486 : gfc_add_modify (&body, offset,
10269 : fold_build2_loc (input_location, PLUS_EXPR,
10270 : gfc_array_index_type, offset, tmp));
10271 :
10272 : /* Update stride. */
10273 486 : gfc_add_modify (
10274 : &body, stride,
10275 : fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, stride,
10276 : fold_convert (gfc_array_index_type, shapese.expr)));
10277 : /* Finish scalarization loop. */
10278 486 : gfc_trans_scalarizing_loops (&loop, &body);
10279 486 : gfc_add_block_to_block (&block, &loop.pre);
10280 486 : gfc_add_block_to_block (&block, &loop.post);
10281 486 : gfc_add_block_to_block (&block, &fptrse.post);
10282 486 : gfc_cleanup_loop (&loop);
10283 :
10284 486 : gfc_add_modify (&block, offset,
10285 : fold_build1_loc (input_location, NEGATE_EXPR,
10286 : gfc_array_index_type, offset));
10287 486 : gfc_conv_descriptor_offset_set (&block, desc, offset);
10288 :
10289 486 : gfc_add_expr_to_block (&se.pre, gfc_finish_block (&block));
10290 486 : gfc_add_block_to_block (&se.pre, &se.post);
10291 486 : return gfc_finish_block (&se.pre);
10292 : }
10293 :
10294 :
10295 : /* The following routine generates code for both forms of the intrinsic
10296 : subroutine C_F_STRPOINTER from the ISO_C_BINDING module. */
10297 : static tree
10298 60 : conv_isocbinding_subroutine_strpointer (gfc_code *code)
10299 : {
10300 60 : gfc_actual_arglist *arg = code->ext.actual;
10301 60 : gfc_expr *arg0 = arg->expr;
10302 60 : gfc_expr *fstrptr = arg->next->expr;
10303 60 : gfc_expr *nchars = arg->next->next->expr;
10304 60 : tree ptr;
10305 60 : tree size = NULL_TREE;
10306 60 : tree nc = NULL_TREE;
10307 60 : tree fstrptr_ptr, fstrptr_len;
10308 60 : stmtblock_t block;
10309 60 : gfc_init_block (&block);
10310 60 : gfc_se se0, se1, se2;
10311 60 : gfc_init_se (&se0, NULL);
10312 60 : gfc_init_se (&se1, NULL);
10313 60 : gfc_init_se (&se2, NULL);
10314 :
10315 : /* arg0 can either be a simply contiguous rank-one character array,
10316 : or a scalar of type c_ptr that points to a contiguous array.
10317 : In the first case nchars may be omitted and defaults to the size
10318 : of the array. */
10319 60 : if (arg0->rank == 1)
10320 : {
10321 42 : gfc_array_ref *ar = gfc_find_array_ref (arg0);
10322 42 : if (ar->as && ar->as->type == AS_ASSUMED_SIZE
10323 12 : && (ar->type == AR_FULL || ar->end[0] == nullptr))
10324 : /* No size available. */
10325 12 : gfc_conv_array_parameter (&se0, arg0, true, NULL, NULL, NULL);
10326 : else
10327 : {
10328 30 : gfc_conv_array_parameter (&se0, arg0, true, NULL, NULL, &size);
10329 30 : gcc_assert (size);
10330 : }
10331 42 : ptr = se0.expr;
10332 : }
10333 18 : else if (arg0->rank == 0)
10334 : {
10335 : /* Scalar case. arg0 is a C pointer to the string, and the
10336 : nchars argument is required. */
10337 18 : gfc_conv_expr (&se0, arg0);
10338 18 : ptr = se0.expr;
10339 : /* We already issued a diagnostic for this in parsing. */
10340 18 : gcc_assert (nchars);
10341 : }
10342 : else
10343 0 : gcc_unreachable ();
10344 :
10345 : /* Translate the fortran array pointer argument. AFAICT the
10346 : representation here is that this returns the pointer location in
10347 : se1.expr and there is a separate decl for the length.
10348 : Of course none of this is properly documented.... :-( */
10349 60 : gfc_conv_expr (&se1, fstrptr);
10350 60 : fstrptr_ptr = se1.expr;
10351 60 : gcc_assert (fstrptr->ts.u.cl && fstrptr->ts.u.cl->backend_decl);
10352 60 : fstrptr_len = fstrptr->ts.u.cl->backend_decl;
10353 :
10354 : /* Translate nchars, if provided. If we have both the array size
10355 : and nchars, take the minimum value. NC is the tree expr to hold
10356 : the value. */
10357 60 : if (nchars)
10358 : {
10359 30 : gfc_conv_expr (&se2, nchars);
10360 30 : nc = se2.expr;
10361 30 : if (size)
10362 0 : nc = fold_build2_loc (input_location, MIN_EXPR,
10363 0 : TREE_TYPE (nc), nc, size);
10364 : /* Check for the case where an optional dummy parameter is
10365 : passed as the optional nchars argument. It's not supposed to
10366 : be omitted if we don't also have an array size; rather than
10367 : produce a run-time error, assume size 0. */
10368 30 : if (nchars->expr_type == EXPR_VARIABLE
10369 18 : && nchars->symtree->n.sym->attr.dummy
10370 18 : && nchars->symtree->n.sym->attr.optional)
10371 : {
10372 12 : tree present = gfc_conv_expr_present (nchars->symtree->n.sym);
10373 12 : nc = build3_loc (input_location, COND_EXPR,
10374 12 : TREE_TYPE (nc), present, nc,
10375 24 : size ? size : build_int_cst (TREE_TYPE (nc), 0));
10376 : }
10377 : }
10378 : else
10379 : {
10380 30 : gcc_assert (size);
10381 : nc = size;
10382 : }
10383 :
10384 : /* Collect argument side-effect statements. */
10385 60 : gfc_add_block_to_block (&block, &se0.pre);
10386 60 : gfc_add_block_to_block (&block, &se1.pre);
10387 60 : gfc_add_block_to_block (&block, &se2.pre);
10388 :
10389 : /* Generate a call to builtin_strnlen to get the C string length
10390 : for the output fstrptr. */
10391 60 : ptr = gfc_evaluate_now (ptr, &block);
10392 60 : size = build_call_expr_loc (input_location,
10393 : builtin_decl_explicit (BUILT_IN_STRNLEN), 2,
10394 : fold_convert (const_ptr_type_node, ptr),
10395 : fold_convert (size_type_node, nc));
10396 :
10397 : /* Stuff the raw C char pointer PTR and actual length SIZE into fstrptr. */
10398 60 : gfc_add_modify (&block, fstrptr_ptr,
10399 60 : fold_convert (TREE_TYPE (fstrptr_ptr), ptr));
10400 60 : gfc_add_modify (&block, fstrptr_len,
10401 : fold_convert (gfc_charlen_type_node, size));
10402 :
10403 : /* Collect argument cleanups. */
10404 60 : gfc_add_block_to_block (&block, &se2.post);
10405 60 : gfc_add_block_to_block (&block, &se1.post);
10406 60 : gfc_add_block_to_block (&block, &se0.post);
10407 :
10408 60 : return gfc_finish_block (&block);
10409 : }
10410 :
10411 : /* Save and restore floating-point state. */
10412 :
10413 : tree
10414 943 : gfc_save_fp_state (stmtblock_t *block)
10415 : {
10416 943 : tree type, fpstate, tmp;
10417 :
10418 943 : type = build_array_type (char_type_node,
10419 : build_range_type (size_type_node, size_zero_node,
10420 : size_int (GFC_FPE_STATE_BUFFER_SIZE)));
10421 943 : fpstate = gfc_create_var (type, "fpstate");
10422 943 : fpstate = gfc_build_addr_expr (pvoid_type_node, fpstate);
10423 :
10424 943 : tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_entry,
10425 : 1, fpstate);
10426 943 : gfc_add_expr_to_block (block, tmp);
10427 :
10428 943 : return fpstate;
10429 : }
10430 :
10431 :
10432 : void
10433 943 : gfc_restore_fp_state (stmtblock_t *block, tree fpstate)
10434 : {
10435 943 : tree tmp;
10436 :
10437 943 : tmp = build_call_expr_loc (input_location, gfor_fndecl_ieee_procedure_exit,
10438 : 1, fpstate);
10439 943 : gfc_add_expr_to_block (block, tmp);
10440 943 : }
10441 :
10442 :
10443 : /* Generate code for arguments of IEEE functions. */
10444 :
10445 : static void
10446 12457 : conv_ieee_function_args (gfc_se *se, gfc_expr *expr, tree *argarray,
10447 : int nargs)
10448 : {
10449 12457 : gfc_actual_arglist *actual;
10450 12457 : gfc_expr *e;
10451 12457 : gfc_se argse;
10452 12457 : int arg;
10453 :
10454 12457 : actual = expr->value.function.actual;
10455 34461 : for (arg = 0; arg < nargs; arg++, actual = actual->next)
10456 : {
10457 22004 : gcc_assert (actual);
10458 22004 : e = actual->expr;
10459 :
10460 22004 : gfc_init_se (&argse, se);
10461 22004 : gfc_conv_expr_val (&argse, e);
10462 :
10463 22004 : gfc_add_block_to_block (&se->pre, &argse.pre);
10464 22004 : gfc_add_block_to_block (&se->post, &argse.post);
10465 22004 : argarray[arg] = argse.expr;
10466 : }
10467 12457 : }
10468 :
10469 :
10470 : /* Generate code for intrinsics IEEE_IS_NAN, IEEE_IS_FINITE
10471 : and IEEE_UNORDERED, which translate directly to GCC type-generic
10472 : built-ins. */
10473 :
10474 : static void
10475 1062 : conv_intrinsic_ieee_builtin (gfc_se * se, gfc_expr * expr,
10476 : enum built_in_function code, int nargs)
10477 : {
10478 1062 : tree args[2];
10479 1062 : gcc_assert ((unsigned) nargs <= ARRAY_SIZE (args));
10480 :
10481 1062 : conv_ieee_function_args (se, expr, args, nargs);
10482 1062 : se->expr = build_call_expr_loc_array (input_location,
10483 : builtin_decl_explicit (code),
10484 : nargs, args);
10485 2388 : STRIP_TYPE_NOPS (se->expr);
10486 1062 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
10487 1062 : }
10488 :
10489 :
10490 : /* Generate code for intrinsics IEEE_SIGNBIT. */
10491 :
10492 : static void
10493 624 : conv_intrinsic_ieee_signbit (gfc_se * se, gfc_expr * expr)
10494 : {
10495 624 : tree arg, signbit;
10496 :
10497 624 : conv_ieee_function_args (se, expr, &arg, 1);
10498 624 : signbit = build_call_expr_loc (input_location,
10499 : builtin_decl_explicit (BUILT_IN_SIGNBIT),
10500 : 1, arg);
10501 624 : signbit = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10502 : signbit, integer_zero_node);
10503 624 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), signbit);
10504 624 : }
10505 :
10506 :
10507 : /* Generate code for IEEE_IS_NORMAL intrinsic:
10508 : IEEE_IS_NORMAL(x) --> (__builtin_isnormal(x) || x == 0) */
10509 :
10510 : static void
10511 312 : conv_intrinsic_ieee_is_normal (gfc_se * se, gfc_expr * expr)
10512 : {
10513 312 : tree arg, isnormal, iszero;
10514 :
10515 : /* Convert arg, evaluate it only once. */
10516 312 : conv_ieee_function_args (se, expr, &arg, 1);
10517 312 : arg = gfc_evaluate_now (arg, &se->pre);
10518 :
10519 312 : isnormal = build_call_expr_loc (input_location,
10520 : builtin_decl_explicit (BUILT_IN_ISNORMAL),
10521 : 1, arg);
10522 312 : iszero = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, arg,
10523 312 : build_real_from_int_cst (TREE_TYPE (arg),
10524 312 : integer_zero_node));
10525 312 : se->expr = fold_build2_loc (input_location, TRUTH_OR_EXPR,
10526 : logical_type_node, isnormal, iszero);
10527 312 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
10528 312 : }
10529 :
10530 :
10531 : /* Generate code for IEEE_IS_NEGATIVE intrinsic:
10532 : IEEE_IS_NEGATIVE(x) --> (__builtin_signbit(x) && !__builtin_isnan(x)) */
10533 :
10534 : static void
10535 312 : conv_intrinsic_ieee_is_negative (gfc_se * se, gfc_expr * expr)
10536 : {
10537 312 : tree arg, signbit, isnan;
10538 :
10539 : /* Convert arg, evaluate it only once. */
10540 312 : conv_ieee_function_args (se, expr, &arg, 1);
10541 312 : arg = gfc_evaluate_now (arg, &se->pre);
10542 :
10543 312 : isnan = build_call_expr_loc (input_location,
10544 : builtin_decl_explicit (BUILT_IN_ISNAN),
10545 : 1, arg);
10546 936 : STRIP_TYPE_NOPS (isnan);
10547 :
10548 312 : signbit = build_call_expr_loc (input_location,
10549 : builtin_decl_explicit (BUILT_IN_SIGNBIT),
10550 : 1, arg);
10551 312 : signbit = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10552 : signbit, integer_zero_node);
10553 :
10554 312 : se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10555 : logical_type_node, signbit,
10556 : fold_build1_loc (input_location, TRUTH_NOT_EXPR,
10557 312 : TREE_TYPE(isnan), isnan));
10558 :
10559 312 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), se->expr);
10560 312 : }
10561 :
10562 :
10563 : /* Generate code for IEEE_LOGB and IEEE_RINT. */
10564 :
10565 : static void
10566 240 : conv_intrinsic_ieee_logb_rint (gfc_se * se, gfc_expr * expr,
10567 : enum built_in_function code)
10568 : {
10569 240 : tree arg, decl, call, fpstate;
10570 240 : int argprec;
10571 :
10572 240 : conv_ieee_function_args (se, expr, &arg, 1);
10573 240 : argprec = TYPE_PRECISION (TREE_TYPE (arg));
10574 240 : decl = builtin_decl_for_precision (code, argprec);
10575 :
10576 : /* Save floating-point state. */
10577 240 : fpstate = gfc_save_fp_state (&se->pre);
10578 :
10579 : /* Make the function call. */
10580 240 : call = build_call_expr_loc (input_location, decl, 1, arg);
10581 240 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), call);
10582 :
10583 : /* Restore floating-point state. */
10584 240 : gfc_restore_fp_state (&se->post, fpstate);
10585 240 : }
10586 :
10587 :
10588 : /* Generate code for IEEE_REM. */
10589 :
10590 : static void
10591 84 : conv_intrinsic_ieee_rem (gfc_se * se, gfc_expr * expr)
10592 : {
10593 84 : tree args[2], decl, call, fpstate;
10594 84 : int argprec;
10595 :
10596 84 : conv_ieee_function_args (se, expr, args, 2);
10597 :
10598 : /* If arguments have unequal size, convert them to the larger. */
10599 84 : if (TYPE_PRECISION (TREE_TYPE (args[0]))
10600 84 : > TYPE_PRECISION (TREE_TYPE (args[1])))
10601 6 : args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
10602 78 : else if (TYPE_PRECISION (TREE_TYPE (args[1]))
10603 78 : > TYPE_PRECISION (TREE_TYPE (args[0])))
10604 24 : args[0] = fold_convert (TREE_TYPE (args[1]), args[0]);
10605 :
10606 84 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10607 84 : decl = builtin_decl_for_precision (BUILT_IN_REMAINDER, argprec);
10608 :
10609 : /* Save floating-point state. */
10610 84 : fpstate = gfc_save_fp_state (&se->pre);
10611 :
10612 : /* Make the function call. */
10613 84 : call = build_call_expr_loc_array (input_location, decl, 2, args);
10614 84 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10615 :
10616 : /* Restore floating-point state. */
10617 84 : gfc_restore_fp_state (&se->post, fpstate);
10618 84 : }
10619 :
10620 :
10621 : /* Generate code for IEEE_NEXT_AFTER. */
10622 :
10623 : static void
10624 180 : conv_intrinsic_ieee_next_after (gfc_se * se, gfc_expr * expr)
10625 : {
10626 180 : tree args[2], decl, call, fpstate;
10627 180 : int argprec;
10628 :
10629 180 : conv_ieee_function_args (se, expr, args, 2);
10630 :
10631 : /* Result has the characteristics of first argument. */
10632 180 : args[1] = fold_convert (TREE_TYPE (args[0]), args[1]);
10633 180 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10634 180 : decl = builtin_decl_for_precision (BUILT_IN_NEXTAFTER, argprec);
10635 :
10636 : /* Save floating-point state. */
10637 180 : fpstate = gfc_save_fp_state (&se->pre);
10638 :
10639 : /* Make the function call. */
10640 180 : call = build_call_expr_loc_array (input_location, decl, 2, args);
10641 180 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10642 :
10643 : /* Restore floating-point state. */
10644 180 : gfc_restore_fp_state (&se->post, fpstate);
10645 180 : }
10646 :
10647 :
10648 : /* Generate code for IEEE_SCALB. */
10649 :
10650 : static void
10651 228 : conv_intrinsic_ieee_scalb (gfc_se * se, gfc_expr * expr)
10652 : {
10653 228 : tree args[2], decl, call, huge, type;
10654 228 : int argprec, n;
10655 :
10656 228 : conv_ieee_function_args (se, expr, args, 2);
10657 :
10658 : /* Result has the characteristics of first argument. */
10659 228 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10660 228 : decl = builtin_decl_for_precision (BUILT_IN_SCALBN, argprec);
10661 :
10662 228 : if (TYPE_PRECISION (TREE_TYPE (args[1])) > TYPE_PRECISION (integer_type_node))
10663 : {
10664 : /* We need to fold the integer into the range of a C int. */
10665 18 : args[1] = gfc_evaluate_now (args[1], &se->pre);
10666 18 : type = TREE_TYPE (args[1]);
10667 :
10668 18 : n = gfc_validate_kind (BT_INTEGER, gfc_c_int_kind, false);
10669 18 : huge = gfc_conv_mpz_to_tree (gfc_integer_kinds[n].huge,
10670 : gfc_c_int_kind);
10671 18 : huge = fold_convert (type, huge);
10672 18 : args[1] = fold_build2_loc (input_location, MIN_EXPR, type, args[1],
10673 : huge);
10674 18 : args[1] = fold_build2_loc (input_location, MAX_EXPR, type, args[1],
10675 : fold_build1_loc (input_location, NEGATE_EXPR,
10676 : type, huge));
10677 : }
10678 :
10679 228 : args[1] = fold_convert (integer_type_node, args[1]);
10680 :
10681 : /* Make the function call. */
10682 228 : call = build_call_expr_loc_array (input_location, decl, 2, args);
10683 228 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10684 228 : }
10685 :
10686 :
10687 : /* Generate code for IEEE_COPY_SIGN. */
10688 :
10689 : static void
10690 576 : conv_intrinsic_ieee_copy_sign (gfc_se * se, gfc_expr * expr)
10691 : {
10692 576 : tree args[2], decl, sign;
10693 576 : int argprec;
10694 :
10695 576 : conv_ieee_function_args (se, expr, args, 2);
10696 :
10697 : /* Get the sign of the second argument. */
10698 576 : sign = build_call_expr_loc (input_location,
10699 : builtin_decl_explicit (BUILT_IN_SIGNBIT),
10700 : 1, args[1]);
10701 576 : sign = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10702 : sign, integer_zero_node);
10703 :
10704 : /* Create a value of one, with the right sign. */
10705 576 : sign = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
10706 : sign,
10707 : fold_build1_loc (input_location, NEGATE_EXPR,
10708 : integer_type_node,
10709 : integer_one_node),
10710 : integer_one_node);
10711 576 : args[1] = fold_convert (TREE_TYPE (args[0]), sign);
10712 :
10713 576 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10714 576 : decl = builtin_decl_for_precision (BUILT_IN_COPYSIGN, argprec);
10715 :
10716 576 : se->expr = build_call_expr_loc_array (input_location, decl, 2, args);
10717 576 : }
10718 :
10719 :
10720 : /* Generate code for IEEE_CLASS. */
10721 :
10722 : static void
10723 648 : conv_intrinsic_ieee_class (gfc_se *se, gfc_expr *expr)
10724 : {
10725 648 : tree arg, c, t1, t2, t3, t4;
10726 :
10727 : /* Convert arg, evaluate it only once. */
10728 648 : conv_ieee_function_args (se, expr, &arg, 1);
10729 648 : arg = gfc_evaluate_now (arg, &se->pre);
10730 :
10731 648 : c = build_call_expr_loc (input_location,
10732 : builtin_decl_explicit (BUILT_IN_FPCLASSIFY), 6,
10733 : build_int_cst (integer_type_node, IEEE_QUIET_NAN),
10734 : build_int_cst (integer_type_node,
10735 : IEEE_POSITIVE_INF),
10736 : build_int_cst (integer_type_node,
10737 : IEEE_POSITIVE_NORMAL),
10738 : build_int_cst (integer_type_node,
10739 : IEEE_POSITIVE_DENORMAL),
10740 : build_int_cst (integer_type_node,
10741 : IEEE_POSITIVE_ZERO),
10742 : arg);
10743 648 : c = gfc_evaluate_now (c, &se->pre);
10744 648 : t1 = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10745 : c, build_int_cst (integer_type_node,
10746 : IEEE_QUIET_NAN));
10747 648 : t2 = build_call_expr_loc (input_location,
10748 : builtin_decl_explicit (BUILT_IN_ISSIGNALING), 1,
10749 : arg);
10750 648 : t2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10751 648 : t2, build_zero_cst (TREE_TYPE (t2)));
10752 648 : t1 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10753 : logical_type_node, t1, t2);
10754 648 : t3 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
10755 : c, build_int_cst (integer_type_node,
10756 : IEEE_POSITIVE_ZERO));
10757 648 : t4 = build_call_expr_loc (input_location,
10758 : builtin_decl_explicit (BUILT_IN_SIGNBIT), 1,
10759 : arg);
10760 648 : t4 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10761 648 : t4, build_zero_cst (TREE_TYPE (t4)));
10762 648 : t3 = fold_build2_loc (input_location, TRUTH_AND_EXPR,
10763 : logical_type_node, t3, t4);
10764 648 : int s = IEEE_NEGATIVE_ZERO + IEEE_POSITIVE_ZERO;
10765 648 : gcc_assert (IEEE_NEGATIVE_INF == s - IEEE_POSITIVE_INF);
10766 648 : gcc_assert (IEEE_NEGATIVE_NORMAL == s - IEEE_POSITIVE_NORMAL);
10767 648 : gcc_assert (IEEE_NEGATIVE_DENORMAL == s - IEEE_POSITIVE_DENORMAL);
10768 648 : gcc_assert (IEEE_NEGATIVE_SUBNORMAL == s - IEEE_POSITIVE_SUBNORMAL);
10769 648 : gcc_assert (IEEE_NEGATIVE_ZERO == s - IEEE_POSITIVE_ZERO);
10770 648 : t4 = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (c),
10771 648 : build_int_cst (TREE_TYPE (c), s), c);
10772 648 : t3 = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (c),
10773 : t3, t4, c);
10774 648 : t1 = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (c), t1,
10775 648 : build_int_cst (TREE_TYPE (c), IEEE_SIGNALING_NAN),
10776 : t3);
10777 648 : tree type = gfc_typenode_for_spec (&expr->ts);
10778 : /* Perform a quick sanity check that the return type is
10779 : IEEE_CLASS_TYPE derived type defined in
10780 : libgfortran/ieee/ieee_arithmetic.F90
10781 : Primarily check that it is a derived type with a single
10782 : member in it. */
10783 648 : gcc_assert (TREE_CODE (type) == RECORD_TYPE);
10784 648 : tree field = NULL_TREE;
10785 1296 : for (tree f = TYPE_FIELDS (type); f != NULL_TREE; f = DECL_CHAIN (f))
10786 648 : if (TREE_CODE (f) == FIELD_DECL)
10787 : {
10788 648 : gcc_assert (field == NULL_TREE);
10789 : field = f;
10790 : }
10791 648 : gcc_assert (field);
10792 648 : t1 = fold_convert (TREE_TYPE (field), t1);
10793 648 : se->expr = build_constructor_single (type, field, t1);
10794 648 : }
10795 :
10796 :
10797 : /* Generate code for IEEE_VALUE. */
10798 :
10799 : static void
10800 1111 : conv_intrinsic_ieee_value (gfc_se *se, gfc_expr *expr)
10801 : {
10802 1111 : tree args[2], arg, ret, tmp;
10803 1111 : stmtblock_t body;
10804 :
10805 : /* Convert args, evaluate the second one only once. */
10806 1111 : conv_ieee_function_args (se, expr, args, 2);
10807 1111 : arg = gfc_evaluate_now (args[1], &se->pre);
10808 :
10809 1111 : tree type = TREE_TYPE (arg);
10810 : /* Perform a quick sanity check that the second argument's type is
10811 : IEEE_CLASS_TYPE derived type defined in
10812 : libgfortran/ieee/ieee_arithmetic.F90
10813 : Primarily check that it is a derived type with a single
10814 : member in it. */
10815 1111 : gcc_assert (TREE_CODE (type) == RECORD_TYPE);
10816 1111 : tree field = NULL_TREE;
10817 2222 : for (tree f = TYPE_FIELDS (type); f != NULL_TREE; f = DECL_CHAIN (f))
10818 1111 : if (TREE_CODE (f) == FIELD_DECL)
10819 : {
10820 1111 : gcc_assert (field == NULL_TREE);
10821 : field = f;
10822 : }
10823 1111 : gcc_assert (field);
10824 1111 : arg = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
10825 : arg, field, NULL_TREE);
10826 1111 : arg = gfc_evaluate_now (arg, &se->pre);
10827 :
10828 1111 : type = gfc_typenode_for_spec (&expr->ts);
10829 1111 : gcc_assert (SCALAR_FLOAT_TYPE_P (type));
10830 1111 : ret = gfc_create_var (type, NULL);
10831 :
10832 1111 : gfc_init_block (&body);
10833 :
10834 1111 : tree end_label = gfc_build_label_decl (NULL_TREE);
10835 13332 : for (int c = IEEE_SIGNALING_NAN; c <= IEEE_POSITIVE_INF; ++c)
10836 : {
10837 11110 : tree label = gfc_build_label_decl (NULL_TREE);
10838 11110 : tree low = build_int_cst (TREE_TYPE (arg), c);
10839 11110 : tmp = build_case_label (low, low, label);
10840 11110 : gfc_add_expr_to_block (&body, tmp);
10841 :
10842 11110 : REAL_VALUE_TYPE real;
10843 11110 : int k;
10844 11110 : switch (c)
10845 : {
10846 1111 : case IEEE_SIGNALING_NAN:
10847 1111 : real_nan (&real, "", 0, TYPE_MODE (type));
10848 1111 : break;
10849 1111 : case IEEE_QUIET_NAN:
10850 1111 : real_nan (&real, "", 1, TYPE_MODE (type));
10851 1111 : break;
10852 1111 : case IEEE_NEGATIVE_INF:
10853 1111 : real_inf (&real);
10854 1111 : real = real_value_negate (&real);
10855 1111 : break;
10856 1111 : case IEEE_NEGATIVE_NORMAL:
10857 1111 : real_from_integer (&real, TYPE_MODE (type), -42, SIGNED);
10858 1111 : break;
10859 1111 : case IEEE_NEGATIVE_DENORMAL:
10860 1111 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
10861 1111 : real_from_mpfr (&real, gfc_real_kinds[k].tiny,
10862 : type, GFC_RND_MODE);
10863 1111 : real_arithmetic (&real, RDIV_EXPR, &real, &dconst2);
10864 1111 : real = real_value_negate (&real);
10865 1111 : break;
10866 1111 : case IEEE_NEGATIVE_ZERO:
10867 1111 : real_from_integer (&real, TYPE_MODE (type), 0, SIGNED);
10868 1111 : real = real_value_negate (&real);
10869 1111 : break;
10870 1111 : case IEEE_POSITIVE_ZERO:
10871 : /* Make this also the default: label. The other possibility
10872 : would be to add a separate default: label followed by
10873 : __builtin_unreachable (). */
10874 1111 : label = gfc_build_label_decl (NULL_TREE);
10875 1111 : tmp = build_case_label (NULL_TREE, NULL_TREE, label);
10876 1111 : gfc_add_expr_to_block (&body, tmp);
10877 1111 : real_from_integer (&real, TYPE_MODE (type), 0, SIGNED);
10878 1111 : break;
10879 1111 : case IEEE_POSITIVE_DENORMAL:
10880 1111 : k = gfc_validate_kind (BT_REAL, expr->ts.kind, false);
10881 1111 : real_from_mpfr (&real, gfc_real_kinds[k].tiny,
10882 : type, GFC_RND_MODE);
10883 1111 : real_arithmetic (&real, RDIV_EXPR, &real, &dconst2);
10884 1111 : break;
10885 1111 : case IEEE_POSITIVE_NORMAL:
10886 1111 : real_from_integer (&real, TYPE_MODE (type), 42, SIGNED);
10887 1111 : break;
10888 1111 : case IEEE_POSITIVE_INF:
10889 1111 : real_inf (&real);
10890 1111 : break;
10891 : default:
10892 : gcc_unreachable ();
10893 : }
10894 :
10895 11110 : tree val = build_real (type, real);
10896 11110 : gfc_add_modify (&body, ret, val);
10897 :
10898 11110 : tmp = build1_v (GOTO_EXPR, end_label);
10899 11110 : gfc_add_expr_to_block (&body, tmp);
10900 : }
10901 :
10902 1111 : tmp = gfc_finish_block (&body);
10903 1111 : tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, arg, tmp);
10904 1111 : gfc_add_expr_to_block (&se->pre, tmp);
10905 :
10906 1111 : tmp = build1_v (LABEL_EXPR, end_label);
10907 1111 : gfc_add_expr_to_block (&se->pre, tmp);
10908 :
10909 1111 : se->expr = ret;
10910 1111 : }
10911 :
10912 :
10913 : /* Generate code for IEEE_FMA. */
10914 :
10915 : static void
10916 120 : conv_intrinsic_ieee_fma (gfc_se * se, gfc_expr * expr)
10917 : {
10918 120 : tree args[3], decl, call;
10919 120 : int argprec;
10920 :
10921 120 : conv_ieee_function_args (se, expr, args, 3);
10922 :
10923 : /* All three arguments should have the same type. */
10924 120 : gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[1])));
10925 120 : gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[2])));
10926 :
10927 : /* Call the type-generic FMA built-in. */
10928 120 : argprec = TYPE_PRECISION (TREE_TYPE (args[0]));
10929 120 : decl = builtin_decl_for_precision (BUILT_IN_FMA, argprec);
10930 120 : call = build_call_expr_loc_array (input_location, decl, 3, args);
10931 :
10932 : /* Convert to the final type. */
10933 120 : se->expr = fold_convert (TREE_TYPE (args[0]), call);
10934 120 : }
10935 :
10936 :
10937 : /* Generate code for IEEE_{MIN,MAX}_NUM{,_MAG}. */
10938 :
10939 : static void
10940 3072 : conv_intrinsic_ieee_minmax (gfc_se * se, gfc_expr * expr, int max,
10941 : const char *name)
10942 : {
10943 3072 : tree args[2], func;
10944 3072 : built_in_function fn;
10945 :
10946 3072 : conv_ieee_function_args (se, expr, args, 2);
10947 3072 : gcc_assert (TYPE_PRECISION (TREE_TYPE (args[0])) == TYPE_PRECISION (TREE_TYPE (args[1])));
10948 3072 : args[0] = gfc_evaluate_now (args[0], &se->pre);
10949 3072 : args[1] = gfc_evaluate_now (args[1], &se->pre);
10950 :
10951 3072 : if (startswith (name, "mag"))
10952 : {
10953 : /* IEEE_MIN_NUM_MAG and IEEE_MAX_NUM_MAG translate to C functions
10954 : fminmag() and fmaxmag(), which do not exist as built-ins.
10955 :
10956 : Following glibc, we emit this:
10957 :
10958 : fminmag (x, y) {
10959 : ax = ABS (x);
10960 : ay = ABS (y);
10961 : if (isless (ax, ay))
10962 : return x;
10963 : else if (isgreater (ax, ay))
10964 : return y;
10965 : else if (ax == ay)
10966 : return x < y ? x : y;
10967 : else if (issignaling (x) || issignaling (y))
10968 : return x + y;
10969 : else
10970 : return isnan (y) ? x : y;
10971 : }
10972 :
10973 : fmaxmag (x, y) {
10974 : ax = ABS (x);
10975 : ay = ABS (y);
10976 : if (isgreater (ax, ay))
10977 : return x;
10978 : else if (isless (ax, ay))
10979 : return y;
10980 : else if (ax == ay)
10981 : return x > y ? x : y;
10982 : else if (issignaling (x) || issignaling (y))
10983 : return x + y;
10984 : else
10985 : return isnan (y) ? x : y;
10986 : }
10987 :
10988 : */
10989 :
10990 1536 : tree abs0, abs1, sig0, sig1;
10991 1536 : tree cond1, cond2, cond3, cond4, cond5;
10992 1536 : tree res;
10993 1536 : tree type = TREE_TYPE (args[0]);
10994 :
10995 1536 : func = gfc_builtin_decl_for_float_kind (BUILT_IN_FABS, expr->ts.kind);
10996 1536 : abs0 = build_call_expr_loc (input_location, func, 1, args[0]);
10997 1536 : abs1 = build_call_expr_loc (input_location, func, 1, args[1]);
10998 1536 : abs0 = gfc_evaluate_now (abs0, &se->pre);
10999 1536 : abs1 = gfc_evaluate_now (abs1, &se->pre);
11000 :
11001 1536 : cond5 = build_call_expr_loc (input_location,
11002 : builtin_decl_explicit (BUILT_IN_ISNAN),
11003 : 1, args[1]);
11004 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond5,
11005 : args[0], args[1]);
11006 :
11007 1536 : sig0 = build_call_expr_loc (input_location,
11008 : builtin_decl_explicit (BUILT_IN_ISSIGNALING),
11009 : 1, args[0]);
11010 1536 : sig1 = build_call_expr_loc (input_location,
11011 : builtin_decl_explicit (BUILT_IN_ISSIGNALING),
11012 : 1, args[1]);
11013 1536 : cond4 = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
11014 : logical_type_node, sig0, sig1);
11015 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond4,
11016 : fold_build2_loc (input_location, PLUS_EXPR,
11017 : type, args[0], args[1]),
11018 : res);
11019 :
11020 1536 : cond3 = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
11021 : abs0, abs1);
11022 2304 : res = fold_build3_loc (input_location, COND_EXPR, type, cond3,
11023 : fold_build2_loc (input_location,
11024 : max ? MAX_EXPR : MIN_EXPR,
11025 : type, args[0], args[1]),
11026 : res);
11027 :
11028 2304 : func = builtin_decl_explicit (max ? BUILT_IN_ISLESS : BUILT_IN_ISGREATER);
11029 1536 : cond2 = build_call_expr_loc (input_location, func, 2, abs0, abs1);
11030 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond2,
11031 : args[1], res);
11032 :
11033 2304 : func = builtin_decl_explicit (max ? BUILT_IN_ISGREATER : BUILT_IN_ISLESS);
11034 1536 : cond1 = build_call_expr_loc (input_location, func, 2, abs0, abs1);
11035 1536 : res = fold_build3_loc (input_location, COND_EXPR, type, cond1,
11036 : args[0], res);
11037 :
11038 1536 : se->expr = res;
11039 : }
11040 : else
11041 : {
11042 : /* IEEE_MIN_NUM and IEEE_MAX_NUM translate to fmin() and fmax(). */
11043 1536 : fn = max ? BUILT_IN_FMAX : BUILT_IN_FMIN;
11044 1536 : func = gfc_builtin_decl_for_float_kind (fn, expr->ts.kind);
11045 1536 : se->expr = build_call_expr_loc_array (input_location, func, 2, args);
11046 : }
11047 3072 : }
11048 :
11049 :
11050 : /* Generate code for comparison functions IEEE_QUIET_* and
11051 : IEEE_SIGNALING_*. */
11052 :
11053 : static void
11054 3888 : conv_intrinsic_ieee_comparison (gfc_se * se, gfc_expr * expr, int signaling,
11055 : const char *name)
11056 : {
11057 3888 : tree args[2];
11058 3888 : tree arg1, arg2, res;
11059 :
11060 : /* Evaluate arguments only once. */
11061 3888 : conv_ieee_function_args (se, expr, args, 2);
11062 3888 : arg1 = gfc_evaluate_now (args[0], &se->pre);
11063 3888 : arg2 = gfc_evaluate_now (args[1], &se->pre);
11064 :
11065 3888 : if (startswith (name, "eq"))
11066 : {
11067 648 : if (signaling)
11068 324 : res = build_call_expr_loc (input_location,
11069 : builtin_decl_explicit (BUILT_IN_ISEQSIG),
11070 : 2, arg1, arg2);
11071 : else
11072 324 : res = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
11073 : arg1, arg2);
11074 : }
11075 3240 : else if (startswith (name, "ne"))
11076 : {
11077 648 : if (signaling)
11078 : {
11079 324 : res = build_call_expr_loc (input_location,
11080 : builtin_decl_explicit (BUILT_IN_ISEQSIG),
11081 : 2, arg1, arg2);
11082 324 : res = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
11083 : logical_type_node, res);
11084 : }
11085 : else
11086 324 : res = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
11087 : arg1, arg2);
11088 : }
11089 2592 : else if (startswith (name, "ge"))
11090 : {
11091 648 : if (signaling)
11092 324 : res = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
11093 : arg1, arg2);
11094 : else
11095 324 : res = build_call_expr_loc (input_location,
11096 : builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL),
11097 : 2, arg1, arg2);
11098 : }
11099 1944 : else if (startswith (name, "gt"))
11100 : {
11101 648 : if (signaling)
11102 324 : res = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
11103 : arg1, arg2);
11104 : else
11105 324 : res = build_call_expr_loc (input_location,
11106 : builtin_decl_explicit (BUILT_IN_ISGREATER),
11107 : 2, arg1, arg2);
11108 : }
11109 1296 : else if (startswith (name, "le"))
11110 : {
11111 648 : if (signaling)
11112 324 : res = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
11113 : arg1, arg2);
11114 : else
11115 324 : res = build_call_expr_loc (input_location,
11116 : builtin_decl_explicit (BUILT_IN_ISLESSEQUAL),
11117 : 2, arg1, arg2);
11118 : }
11119 648 : else if (startswith (name, "lt"))
11120 : {
11121 648 : if (signaling)
11122 324 : res = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
11123 : arg1, arg2);
11124 : else
11125 324 : res = build_call_expr_loc (input_location,
11126 : builtin_decl_explicit (BUILT_IN_ISLESS),
11127 : 2, arg1, arg2);
11128 : }
11129 : else
11130 0 : gcc_unreachable ();
11131 :
11132 3888 : se->expr = fold_convert (gfc_typenode_for_spec (&expr->ts), res);
11133 3888 : }
11134 :
11135 :
11136 : /* Generate code for an intrinsic function from the IEEE_ARITHMETIC
11137 : module. */
11138 :
11139 : bool
11140 13939 : gfc_conv_ieee_arithmetic_function (gfc_se * se, gfc_expr * expr)
11141 : {
11142 13939 : const char *name = expr->value.function.name;
11143 :
11144 13939 : if (startswith (name, "_gfortran_ieee_is_nan"))
11145 522 : conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISNAN, 1);
11146 13417 : else if (startswith (name, "_gfortran_ieee_is_finite"))
11147 372 : conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISFINITE, 1);
11148 13045 : else if (startswith (name, "_gfortran_ieee_unordered"))
11149 168 : conv_intrinsic_ieee_builtin (se, expr, BUILT_IN_ISUNORDERED, 2);
11150 12877 : else if (startswith (name, "_gfortran_ieee_signbit"))
11151 624 : conv_intrinsic_ieee_signbit (se, expr);
11152 12253 : else if (startswith (name, "_gfortran_ieee_is_normal"))
11153 312 : conv_intrinsic_ieee_is_normal (se, expr);
11154 11941 : else if (startswith (name, "_gfortran_ieee_is_negative"))
11155 312 : conv_intrinsic_ieee_is_negative (se, expr);
11156 11629 : else if (startswith (name, "_gfortran_ieee_copy_sign"))
11157 576 : conv_intrinsic_ieee_copy_sign (se, expr);
11158 11053 : else if (startswith (name, "_gfortran_ieee_scalb"))
11159 228 : conv_intrinsic_ieee_scalb (se, expr);
11160 10825 : else if (startswith (name, "_gfortran_ieee_next_after"))
11161 180 : conv_intrinsic_ieee_next_after (se, expr);
11162 10645 : else if (startswith (name, "_gfortran_ieee_rem"))
11163 84 : conv_intrinsic_ieee_rem (se, expr);
11164 10561 : else if (startswith (name, "_gfortran_ieee_logb"))
11165 144 : conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_LOGB);
11166 10417 : else if (startswith (name, "_gfortran_ieee_rint"))
11167 96 : conv_intrinsic_ieee_logb_rint (se, expr, BUILT_IN_RINT);
11168 10321 : else if (startswith (name, "ieee_class_") && ISDIGIT (name[11]))
11169 648 : conv_intrinsic_ieee_class (se, expr);
11170 9673 : else if (startswith (name, "ieee_value_") && ISDIGIT (name[11]))
11171 1111 : conv_intrinsic_ieee_value (se, expr);
11172 8562 : else if (startswith (name, "_gfortran_ieee_fma"))
11173 120 : conv_intrinsic_ieee_fma (se, expr);
11174 8442 : else if (startswith (name, "_gfortran_ieee_min_num_"))
11175 1536 : conv_intrinsic_ieee_minmax (se, expr, 0, name + 23);
11176 6906 : else if (startswith (name, "_gfortran_ieee_max_num_"))
11177 1536 : conv_intrinsic_ieee_minmax (se, expr, 1, name + 23);
11178 5370 : else if (startswith (name, "_gfortran_ieee_quiet_"))
11179 1944 : conv_intrinsic_ieee_comparison (se, expr, 0, name + 21);
11180 3426 : else if (startswith (name, "_gfortran_ieee_signaling_"))
11181 1944 : conv_intrinsic_ieee_comparison (se, expr, 1, name + 25);
11182 : else
11183 : /* It is not among the functions we translate directly. We return
11184 : false, so a library function call is emitted. */
11185 : return false;
11186 :
11187 : return true;
11188 : }
11189 :
11190 :
11191 : /* Generate a direct call to malloc() for the MALLOC intrinsic. */
11192 :
11193 : static void
11194 16 : gfc_conv_intrinsic_malloc (gfc_se * se, gfc_expr * expr)
11195 : {
11196 16 : tree arg, res, restype;
11197 :
11198 16 : gfc_conv_intrinsic_function_args (se, expr, &arg, 1);
11199 16 : arg = fold_convert (size_type_node, arg);
11200 16 : res = build_call_expr_loc (input_location,
11201 : builtin_decl_explicit (BUILT_IN_MALLOC), 1, arg);
11202 16 : restype = gfc_typenode_for_spec (&expr->ts);
11203 16 : se->expr = fold_convert (restype, res);
11204 16 : }
11205 :
11206 :
11207 : /* Generate code for an intrinsic function. Some map directly to library
11208 : calls, others get special handling. In some cases the name of the function
11209 : used depends on the type specifiers. */
11210 :
11211 : void
11212 269399 : gfc_conv_intrinsic_function (gfc_se * se, gfc_expr * expr)
11213 : {
11214 269399 : const char *name;
11215 269399 : int lib, kind;
11216 269399 : tree fndecl;
11217 :
11218 269399 : name = &expr->value.function.name[2];
11219 :
11220 269399 : if (expr->rank > 0)
11221 : {
11222 50610 : lib = gfc_is_intrinsic_libcall (expr);
11223 50610 : if (lib != 0)
11224 : {
11225 19242 : if (lib == 1)
11226 11792 : se->ignore_optional = 1;
11227 :
11228 19242 : switch (expr->value.function.isym->id)
11229 : {
11230 5879 : case GFC_ISYM_EOSHIFT:
11231 5879 : case GFC_ISYM_PACK:
11232 5879 : case GFC_ISYM_RESHAPE:
11233 5879 : case GFC_ISYM_REDUCE:
11234 : /* For all of those the first argument specifies the type and the
11235 : third is optional. */
11236 5879 : conv_generic_with_optional_char_arg (se, expr, 1, 3);
11237 5879 : break;
11238 :
11239 1116 : case GFC_ISYM_FINDLOC:
11240 1116 : gfc_conv_intrinsic_findloc (se, expr);
11241 1116 : break;
11242 :
11243 2935 : case GFC_ISYM_MINLOC:
11244 2935 : gfc_conv_intrinsic_minmaxloc (se, expr, LT_EXPR);
11245 2935 : break;
11246 :
11247 2439 : case GFC_ISYM_MAXLOC:
11248 2439 : gfc_conv_intrinsic_minmaxloc (se, expr, GT_EXPR);
11249 2439 : break;
11250 :
11251 6873 : default:
11252 6873 : gfc_conv_intrinsic_funcall (se, expr);
11253 6873 : break;
11254 : }
11255 :
11256 : return;
11257 : }
11258 : }
11259 :
11260 250157 : switch (expr->value.function.isym->id)
11261 : {
11262 0 : case GFC_ISYM_NONE:
11263 0 : gcc_unreachable ();
11264 :
11265 541 : case GFC_ISYM_REPEAT:
11266 541 : gfc_conv_intrinsic_repeat (se, expr);
11267 541 : break;
11268 :
11269 580 : case GFC_ISYM_TRIM:
11270 580 : gfc_conv_intrinsic_trim (se, expr);
11271 580 : break;
11272 :
11273 42 : case GFC_ISYM_SC_KIND:
11274 42 : gfc_conv_intrinsic_sc_kind (se, expr);
11275 42 : break;
11276 :
11277 45 : case GFC_ISYM_SI_KIND:
11278 45 : gfc_conv_intrinsic_si_kind (se, expr);
11279 45 : break;
11280 :
11281 6 : case GFC_ISYM_SL_KIND:
11282 6 : gfc_conv_intrinsic_sl_kind (se, expr);
11283 6 : break;
11284 :
11285 82 : case GFC_ISYM_SR_KIND:
11286 82 : gfc_conv_intrinsic_sr_kind (se, expr);
11287 82 : break;
11288 :
11289 228 : case GFC_ISYM_EXPONENT:
11290 228 : gfc_conv_intrinsic_exponent (se, expr);
11291 228 : break;
11292 :
11293 316 : case GFC_ISYM_SCAN:
11294 316 : kind = expr->value.function.actual->expr->ts.kind;
11295 316 : if (kind == 1)
11296 250 : fndecl = gfor_fndecl_string_scan;
11297 66 : else if (kind == 4)
11298 66 : fndecl = gfor_fndecl_string_scan_char4;
11299 : else
11300 0 : gcc_unreachable ();
11301 :
11302 316 : gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
11303 316 : break;
11304 :
11305 94 : case GFC_ISYM_VERIFY:
11306 94 : kind = expr->value.function.actual->expr->ts.kind;
11307 94 : if (kind == 1)
11308 70 : fndecl = gfor_fndecl_string_verify;
11309 24 : else if (kind == 4)
11310 24 : fndecl = gfor_fndecl_string_verify_char4;
11311 : else
11312 0 : gcc_unreachable ();
11313 :
11314 94 : gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
11315 94 : break;
11316 :
11317 7530 : case GFC_ISYM_ALLOCATED:
11318 7530 : gfc_conv_allocated (se, expr);
11319 7530 : break;
11320 :
11321 9743 : case GFC_ISYM_ASSOCIATED:
11322 9743 : gfc_conv_associated(se, expr);
11323 9743 : break;
11324 :
11325 409 : case GFC_ISYM_SAME_TYPE_AS:
11326 409 : gfc_conv_same_type_as (se, expr);
11327 409 : break;
11328 :
11329 8028 : case GFC_ISYM_ABS:
11330 8028 : gfc_conv_intrinsic_abs (se, expr);
11331 8028 : break;
11332 :
11333 345 : case GFC_ISYM_ADJUSTL:
11334 345 : if (expr->ts.kind == 1)
11335 291 : fndecl = gfor_fndecl_adjustl;
11336 54 : else if (expr->ts.kind == 4)
11337 54 : fndecl = gfor_fndecl_adjustl_char4;
11338 : else
11339 0 : gcc_unreachable ();
11340 :
11341 345 : gfc_conv_intrinsic_adjust (se, expr, fndecl);
11342 345 : break;
11343 :
11344 123 : case GFC_ISYM_ADJUSTR:
11345 123 : if (expr->ts.kind == 1)
11346 68 : fndecl = gfor_fndecl_adjustr;
11347 55 : else if (expr->ts.kind == 4)
11348 55 : fndecl = gfor_fndecl_adjustr_char4;
11349 : else
11350 0 : gcc_unreachable ();
11351 :
11352 123 : gfc_conv_intrinsic_adjust (se, expr, fndecl);
11353 123 : break;
11354 :
11355 440 : case GFC_ISYM_AIMAG:
11356 440 : gfc_conv_intrinsic_imagpart (se, expr);
11357 440 : break;
11358 :
11359 146 : case GFC_ISYM_AINT:
11360 146 : gfc_conv_intrinsic_aint (se, expr, RND_TRUNC);
11361 146 : break;
11362 :
11363 432 : case GFC_ISYM_ALL:
11364 432 : gfc_conv_intrinsic_anyall (se, expr, EQ_EXPR);
11365 432 : break;
11366 :
11367 74 : case GFC_ISYM_ANINT:
11368 74 : gfc_conv_intrinsic_aint (se, expr, RND_ROUND);
11369 74 : break;
11370 :
11371 90 : case GFC_ISYM_AND:
11372 90 : gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
11373 90 : break;
11374 :
11375 38662 : case GFC_ISYM_ANY:
11376 38662 : gfc_conv_intrinsic_anyall (se, expr, NE_EXPR);
11377 38662 : break;
11378 :
11379 270 : case GFC_ISYM_ACOSD:
11380 270 : case GFC_ISYM_ASIND:
11381 270 : case GFC_ISYM_ATAND:
11382 270 : gfc_conv_intrinsic_atrigd (se, expr, expr->value.function.isym->id);
11383 270 : break;
11384 :
11385 102 : case GFC_ISYM_COTAN:
11386 102 : gfc_conv_intrinsic_cotan (se, expr);
11387 102 : break;
11388 :
11389 108 : case GFC_ISYM_COTAND:
11390 108 : gfc_conv_intrinsic_cotand (se, expr);
11391 108 : break;
11392 :
11393 138 : case GFC_ISYM_ATAN2D:
11394 138 : gfc_conv_intrinsic_atan2d (se, expr);
11395 138 : break;
11396 :
11397 145 : case GFC_ISYM_BTEST:
11398 145 : gfc_conv_intrinsic_btest (se, expr);
11399 145 : break;
11400 :
11401 54 : case GFC_ISYM_BGE:
11402 54 : gfc_conv_intrinsic_bitcomp (se, expr, GE_EXPR);
11403 54 : break;
11404 :
11405 54 : case GFC_ISYM_BGT:
11406 54 : gfc_conv_intrinsic_bitcomp (se, expr, GT_EXPR);
11407 54 : break;
11408 :
11409 54 : case GFC_ISYM_BLE:
11410 54 : gfc_conv_intrinsic_bitcomp (se, expr, LE_EXPR);
11411 54 : break;
11412 :
11413 54 : case GFC_ISYM_BLT:
11414 54 : gfc_conv_intrinsic_bitcomp (se, expr, LT_EXPR);
11415 54 : break;
11416 :
11417 9949 : case GFC_ISYM_C_ASSOCIATED:
11418 9949 : case GFC_ISYM_C_FUNLOC:
11419 9949 : case GFC_ISYM_C_LOC:
11420 9949 : case GFC_ISYM_F_C_STRING:
11421 9949 : conv_isocbinding_function (se, expr);
11422 9949 : break;
11423 :
11424 2020 : case GFC_ISYM_ACHAR:
11425 2020 : case GFC_ISYM_CHAR:
11426 2020 : gfc_conv_intrinsic_char (se, expr);
11427 2020 : break;
11428 :
11429 41275 : case GFC_ISYM_CONVERSION:
11430 41275 : case GFC_ISYM_DBLE:
11431 41275 : case GFC_ISYM_DFLOAT:
11432 41275 : case GFC_ISYM_FLOAT:
11433 41275 : case GFC_ISYM_LOGICAL:
11434 41275 : case GFC_ISYM_REAL:
11435 41275 : case GFC_ISYM_REALPART:
11436 41275 : case GFC_ISYM_SNGL:
11437 41275 : gfc_conv_intrinsic_conversion (se, expr);
11438 41275 : break;
11439 :
11440 : /* Integer conversions are handled separately to make sure we get the
11441 : correct rounding mode. */
11442 2836 : case GFC_ISYM_INT:
11443 2836 : case GFC_ISYM_INT2:
11444 2836 : case GFC_ISYM_INT8:
11445 2836 : case GFC_ISYM_LONG:
11446 2836 : case GFC_ISYM_UINT:
11447 2836 : gfc_conv_intrinsic_int (se, expr, RND_TRUNC);
11448 2836 : break;
11449 :
11450 162 : case GFC_ISYM_NINT:
11451 162 : gfc_conv_intrinsic_int (se, expr, RND_ROUND);
11452 162 : break;
11453 :
11454 16 : case GFC_ISYM_CEILING:
11455 16 : gfc_conv_intrinsic_int (se, expr, RND_CEIL);
11456 16 : break;
11457 :
11458 116 : case GFC_ISYM_FLOOR:
11459 116 : gfc_conv_intrinsic_int (se, expr, RND_FLOOR);
11460 116 : break;
11461 :
11462 3385 : case GFC_ISYM_MOD:
11463 3385 : gfc_conv_intrinsic_mod (se, expr, 0);
11464 3385 : break;
11465 :
11466 442 : case GFC_ISYM_MODULO:
11467 442 : gfc_conv_intrinsic_mod (se, expr, 1);
11468 442 : break;
11469 :
11470 1006 : case GFC_ISYM_CAF_GET:
11471 1006 : gfc_conv_intrinsic_caf_get (se, expr, NULL_TREE, false, NULL);
11472 1006 : break;
11473 :
11474 239 : case GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE:
11475 239 : gfc_conv_intrinsic_caf_is_present_remote (se, expr);
11476 239 : break;
11477 :
11478 491 : case GFC_ISYM_CMPLX:
11479 491 : gfc_conv_intrinsic_cmplx (se, expr, name[5] == '1');
11480 491 : break;
11481 :
11482 10 : case GFC_ISYM_COMMAND_ARGUMENT_COUNT:
11483 10 : gfc_conv_intrinsic_iargc (se, expr);
11484 10 : break;
11485 :
11486 6 : case GFC_ISYM_COMPLEX:
11487 6 : gfc_conv_intrinsic_cmplx (se, expr, 1);
11488 6 : break;
11489 :
11490 257 : case GFC_ISYM_CONJG:
11491 257 : gfc_conv_intrinsic_conjg (se, expr);
11492 257 : break;
11493 :
11494 4 : case GFC_ISYM_COSHAPE:
11495 4 : conv_intrinsic_cobound (se, expr);
11496 4 : break;
11497 :
11498 143 : case GFC_ISYM_COUNT:
11499 143 : gfc_conv_intrinsic_count (se, expr);
11500 143 : break;
11501 :
11502 0 : case GFC_ISYM_CTIME:
11503 0 : gfc_conv_intrinsic_ctime (se, expr);
11504 0 : break;
11505 :
11506 96 : case GFC_ISYM_DIM:
11507 96 : gfc_conv_intrinsic_dim (se, expr);
11508 96 : break;
11509 :
11510 113 : case GFC_ISYM_DOT_PRODUCT:
11511 113 : gfc_conv_intrinsic_dot_product (se, expr);
11512 113 : break;
11513 :
11514 13 : case GFC_ISYM_DPROD:
11515 13 : gfc_conv_intrinsic_dprod (se, expr);
11516 13 : break;
11517 :
11518 66 : case GFC_ISYM_DSHIFTL:
11519 66 : gfc_conv_intrinsic_dshift (se, expr, true);
11520 66 : break;
11521 :
11522 66 : case GFC_ISYM_DSHIFTR:
11523 66 : gfc_conv_intrinsic_dshift (se, expr, false);
11524 66 : break;
11525 :
11526 0 : case GFC_ISYM_FDATE:
11527 0 : gfc_conv_intrinsic_fdate (se, expr);
11528 0 : break;
11529 :
11530 60 : case GFC_ISYM_FRACTION:
11531 60 : gfc_conv_intrinsic_fraction (se, expr);
11532 60 : break;
11533 :
11534 24 : case GFC_ISYM_IALL:
11535 24 : gfc_conv_intrinsic_arith (se, expr, BIT_AND_EXPR, false);
11536 24 : break;
11537 :
11538 606 : case GFC_ISYM_IAND:
11539 606 : gfc_conv_intrinsic_bitop (se, expr, BIT_AND_EXPR);
11540 606 : break;
11541 :
11542 12 : case GFC_ISYM_IANY:
11543 12 : gfc_conv_intrinsic_arith (se, expr, BIT_IOR_EXPR, false);
11544 12 : break;
11545 :
11546 168 : case GFC_ISYM_IBCLR:
11547 168 : gfc_conv_intrinsic_singlebitop (se, expr, 0);
11548 168 : break;
11549 :
11550 27 : case GFC_ISYM_IBITS:
11551 27 : gfc_conv_intrinsic_ibits (se, expr);
11552 27 : break;
11553 :
11554 138 : case GFC_ISYM_IBSET:
11555 138 : gfc_conv_intrinsic_singlebitop (se, expr, 1);
11556 138 : break;
11557 :
11558 2033 : case GFC_ISYM_IACHAR:
11559 2033 : case GFC_ISYM_ICHAR:
11560 : /* We assume ASCII character sequence. */
11561 2033 : gfc_conv_intrinsic_ichar (se, expr);
11562 2033 : break;
11563 :
11564 2 : case GFC_ISYM_IARGC:
11565 2 : gfc_conv_intrinsic_iargc (se, expr);
11566 2 : break;
11567 :
11568 694 : case GFC_ISYM_IEOR:
11569 694 : gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
11570 694 : break;
11571 :
11572 341 : case GFC_ISYM_INDEX:
11573 341 : kind = expr->value.function.actual->expr->ts.kind;
11574 341 : if (kind == 1)
11575 275 : fndecl = gfor_fndecl_string_index;
11576 66 : else if (kind == 4)
11577 66 : fndecl = gfor_fndecl_string_index_char4;
11578 : else
11579 0 : gcc_unreachable ();
11580 :
11581 341 : gfc_conv_intrinsic_index_scan_verify (se, expr, fndecl);
11582 341 : break;
11583 :
11584 495 : case GFC_ISYM_IOR:
11585 495 : gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
11586 495 : break;
11587 :
11588 12 : case GFC_ISYM_IPARITY:
11589 12 : gfc_conv_intrinsic_arith (se, expr, BIT_XOR_EXPR, false);
11590 12 : break;
11591 :
11592 6 : case GFC_ISYM_IS_IOSTAT_END:
11593 6 : gfc_conv_has_intvalue (se, expr, LIBERROR_END);
11594 6 : break;
11595 :
11596 18 : case GFC_ISYM_IS_IOSTAT_EOR:
11597 18 : gfc_conv_has_intvalue (se, expr, LIBERROR_EOR);
11598 18 : break;
11599 :
11600 748 : case GFC_ISYM_IS_CONTIGUOUS:
11601 748 : gfc_conv_intrinsic_is_contiguous (se, expr);
11602 748 : break;
11603 :
11604 432 : case GFC_ISYM_ISNAN:
11605 432 : gfc_conv_intrinsic_isnan (se, expr);
11606 432 : break;
11607 :
11608 8 : case GFC_ISYM_KILL:
11609 8 : conv_intrinsic_kill (se, expr);
11610 8 : break;
11611 :
11612 90 : case GFC_ISYM_LSHIFT:
11613 90 : gfc_conv_intrinsic_shift (se, expr, false, false);
11614 90 : break;
11615 :
11616 24 : case GFC_ISYM_RSHIFT:
11617 24 : gfc_conv_intrinsic_shift (se, expr, true, true);
11618 24 : break;
11619 :
11620 78 : case GFC_ISYM_SHIFTA:
11621 78 : gfc_conv_intrinsic_shift (se, expr, true, true);
11622 78 : break;
11623 :
11624 234 : case GFC_ISYM_SHIFTL:
11625 234 : gfc_conv_intrinsic_shift (se, expr, false, false);
11626 234 : break;
11627 :
11628 66 : case GFC_ISYM_SHIFTR:
11629 66 : gfc_conv_intrinsic_shift (se, expr, true, false);
11630 66 : break;
11631 :
11632 318 : case GFC_ISYM_ISHFT:
11633 318 : gfc_conv_intrinsic_ishft (se, expr);
11634 318 : break;
11635 :
11636 658 : case GFC_ISYM_ISHFTC:
11637 658 : gfc_conv_intrinsic_ishftc (se, expr);
11638 658 : break;
11639 :
11640 270 : case GFC_ISYM_LEADZ:
11641 270 : gfc_conv_intrinsic_leadz (se, expr);
11642 270 : break;
11643 :
11644 282 : case GFC_ISYM_TRAILZ:
11645 282 : gfc_conv_intrinsic_trailz (se, expr);
11646 282 : break;
11647 :
11648 103 : case GFC_ISYM_POPCNT:
11649 103 : gfc_conv_intrinsic_popcnt_poppar (se, expr, 0);
11650 103 : break;
11651 :
11652 31 : case GFC_ISYM_POPPAR:
11653 31 : gfc_conv_intrinsic_popcnt_poppar (se, expr, 1);
11654 31 : break;
11655 :
11656 5589 : case GFC_ISYM_LBOUND:
11657 5589 : gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_LBOUND);
11658 5589 : break;
11659 :
11660 210 : case GFC_ISYM_LCOBOUND:
11661 210 : conv_intrinsic_cobound (se, expr);
11662 210 : break;
11663 :
11664 774 : case GFC_ISYM_TRANSPOSE:
11665 : /* The scalarizer has already been set up for reversed dimension access
11666 : order ; now we just get the argument value normally. */
11667 774 : gfc_conv_expr (se, expr->value.function.actual->expr);
11668 774 : break;
11669 :
11670 5946 : case GFC_ISYM_LEN:
11671 5946 : gfc_conv_intrinsic_len (se, expr);
11672 5946 : break;
11673 :
11674 2340 : case GFC_ISYM_LEN_TRIM:
11675 2340 : gfc_conv_intrinsic_len_trim (se, expr);
11676 2340 : break;
11677 :
11678 18 : case GFC_ISYM_LGE:
11679 18 : gfc_conv_intrinsic_strcmp (se, expr, GE_EXPR);
11680 18 : break;
11681 :
11682 36 : case GFC_ISYM_LGT:
11683 36 : gfc_conv_intrinsic_strcmp (se, expr, GT_EXPR);
11684 36 : break;
11685 :
11686 18 : case GFC_ISYM_LLE:
11687 18 : gfc_conv_intrinsic_strcmp (se, expr, LE_EXPR);
11688 18 : break;
11689 :
11690 27 : case GFC_ISYM_LLT:
11691 27 : gfc_conv_intrinsic_strcmp (se, expr, LT_EXPR);
11692 27 : break;
11693 :
11694 16 : case GFC_ISYM_MALLOC:
11695 16 : gfc_conv_intrinsic_malloc (se, expr);
11696 16 : break;
11697 :
11698 32 : case GFC_ISYM_MASKL:
11699 32 : gfc_conv_intrinsic_mask (se, expr, 1);
11700 32 : break;
11701 :
11702 32 : case GFC_ISYM_MASKR:
11703 32 : gfc_conv_intrinsic_mask (se, expr, 0);
11704 32 : break;
11705 :
11706 1049 : case GFC_ISYM_MAX:
11707 1049 : if (expr->ts.type == BT_CHARACTER)
11708 138 : gfc_conv_intrinsic_minmax_char (se, expr, 1);
11709 : else
11710 911 : gfc_conv_intrinsic_minmax (se, expr, GT_EXPR);
11711 : break;
11712 :
11713 6348 : case GFC_ISYM_MAXLOC:
11714 6348 : gfc_conv_intrinsic_minmaxloc (se, expr, GT_EXPR);
11715 6348 : break;
11716 :
11717 216 : case GFC_ISYM_FINDLOC:
11718 216 : gfc_conv_intrinsic_findloc (se, expr);
11719 216 : break;
11720 :
11721 1101 : case GFC_ISYM_MAXVAL:
11722 1101 : gfc_conv_intrinsic_minmaxval (se, expr, GT_EXPR);
11723 1101 : break;
11724 :
11725 949 : case GFC_ISYM_MERGE:
11726 949 : gfc_conv_intrinsic_merge (se, expr);
11727 949 : break;
11728 :
11729 42 : case GFC_ISYM_MERGE_BITS:
11730 42 : gfc_conv_intrinsic_merge_bits (se, expr);
11731 42 : break;
11732 :
11733 598 : case GFC_ISYM_MIN:
11734 598 : if (expr->ts.type == BT_CHARACTER)
11735 144 : gfc_conv_intrinsic_minmax_char (se, expr, -1);
11736 : else
11737 454 : gfc_conv_intrinsic_minmax (se, expr, LT_EXPR);
11738 : break;
11739 :
11740 7176 : case GFC_ISYM_MINLOC:
11741 7176 : gfc_conv_intrinsic_minmaxloc (se, expr, LT_EXPR);
11742 7176 : break;
11743 :
11744 1316 : case GFC_ISYM_MINVAL:
11745 1316 : gfc_conv_intrinsic_minmaxval (se, expr, LT_EXPR);
11746 1316 : break;
11747 :
11748 1595 : case GFC_ISYM_NEAREST:
11749 1595 : gfc_conv_intrinsic_nearest (se, expr);
11750 1595 : break;
11751 :
11752 68 : case GFC_ISYM_NORM2:
11753 68 : gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, true);
11754 68 : break;
11755 :
11756 230 : case GFC_ISYM_NOT:
11757 230 : gfc_conv_intrinsic_not (se, expr);
11758 230 : break;
11759 :
11760 12 : case GFC_ISYM_OR:
11761 12 : gfc_conv_intrinsic_bitop (se, expr, BIT_IOR_EXPR);
11762 12 : break;
11763 :
11764 468 : case GFC_ISYM_OUT_OF_RANGE:
11765 468 : gfc_conv_intrinsic_out_of_range (se, expr);
11766 468 : break;
11767 :
11768 36 : case GFC_ISYM_PARITY:
11769 36 : gfc_conv_intrinsic_arith (se, expr, NE_EXPR, false);
11770 36 : break;
11771 :
11772 5196 : case GFC_ISYM_PRESENT:
11773 5196 : gfc_conv_intrinsic_present (se, expr);
11774 5196 : break;
11775 :
11776 358 : case GFC_ISYM_PRODUCT:
11777 358 : gfc_conv_intrinsic_arith (se, expr, MULT_EXPR, false);
11778 358 : break;
11779 :
11780 13376 : case GFC_ISYM_RANK:
11781 13376 : gfc_conv_intrinsic_rank (se, expr);
11782 13376 : break;
11783 :
11784 48 : case GFC_ISYM_RRSPACING:
11785 48 : gfc_conv_intrinsic_rrspacing (se, expr);
11786 48 : break;
11787 :
11788 262 : case GFC_ISYM_SET_EXPONENT:
11789 262 : gfc_conv_intrinsic_set_exponent (se, expr);
11790 262 : break;
11791 :
11792 72 : case GFC_ISYM_SCALE:
11793 72 : gfc_conv_intrinsic_scale (se, expr);
11794 72 : break;
11795 :
11796 5012 : case GFC_ISYM_SHAPE:
11797 5012 : gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_SHAPE);
11798 5012 : break;
11799 :
11800 423 : case GFC_ISYM_SIGN:
11801 423 : gfc_conv_intrinsic_sign (se, expr);
11802 423 : break;
11803 :
11804 15631 : case GFC_ISYM_SIZE:
11805 15631 : gfc_conv_intrinsic_size (se, expr);
11806 15631 : break;
11807 :
11808 1309 : case GFC_ISYM_SIZEOF:
11809 1309 : case GFC_ISYM_C_SIZEOF:
11810 1309 : gfc_conv_intrinsic_sizeof (se, expr);
11811 1309 : break;
11812 :
11813 865 : case GFC_ISYM_STORAGE_SIZE:
11814 865 : gfc_conv_intrinsic_storage_size (se, expr);
11815 865 : break;
11816 :
11817 70 : case GFC_ISYM_SPACING:
11818 70 : gfc_conv_intrinsic_spacing (se, expr);
11819 70 : break;
11820 :
11821 2429 : case GFC_ISYM_STRIDE:
11822 2429 : conv_intrinsic_stride (se, expr);
11823 2429 : break;
11824 :
11825 2011 : case GFC_ISYM_SUM:
11826 2011 : gfc_conv_intrinsic_arith (se, expr, PLUS_EXPR, false);
11827 2011 : break;
11828 :
11829 39 : case GFC_ISYM_TEAM_NUMBER:
11830 39 : conv_intrinsic_team_number (se, expr);
11831 39 : break;
11832 :
11833 4272 : case GFC_ISYM_TRANSFER:
11834 4272 : if (se->ss && se->ss->info->useflags)
11835 : /* Access the previously obtained result. */
11836 281 : gfc_conv_tmp_array_ref (se);
11837 : else
11838 3991 : gfc_conv_intrinsic_transfer (se, expr);
11839 : break;
11840 :
11841 0 : case GFC_ISYM_TTYNAM:
11842 0 : gfc_conv_intrinsic_ttynam (se, expr);
11843 0 : break;
11844 :
11845 5748 : case GFC_ISYM_UBOUND:
11846 5748 : gfc_conv_intrinsic_bound (se, expr, GFC_ISYM_UBOUND);
11847 5748 : break;
11848 :
11849 244 : case GFC_ISYM_UCOBOUND:
11850 244 : conv_intrinsic_cobound (se, expr);
11851 244 : break;
11852 :
11853 18 : case GFC_ISYM_XOR:
11854 18 : gfc_conv_intrinsic_bitop (se, expr, BIT_XOR_EXPR);
11855 18 : break;
11856 :
11857 8993 : case GFC_ISYM_LOC:
11858 8993 : gfc_conv_intrinsic_loc (se, expr);
11859 8993 : break;
11860 :
11861 1514 : case GFC_ISYM_THIS_IMAGE:
11862 : /* For num_images() == 1, handle as LCOBOUND. */
11863 1514 : if (expr->value.function.actual->expr
11864 526 : && flag_coarray == GFC_FCOARRAY_SINGLE)
11865 208 : conv_intrinsic_cobound (se, expr);
11866 : else
11867 1306 : trans_this_image (se, expr);
11868 : break;
11869 :
11870 223 : case GFC_ISYM_IMAGE_INDEX:
11871 223 : trans_image_index (se, expr);
11872 223 : break;
11873 :
11874 25 : case GFC_ISYM_IMAGE_STATUS:
11875 25 : conv_intrinsic_image_status (se, expr);
11876 25 : break;
11877 :
11878 814 : case GFC_ISYM_NUM_IMAGES:
11879 814 : trans_num_images (se, expr);
11880 814 : break;
11881 :
11882 1410 : case GFC_ISYM_ACCESS:
11883 1410 : case GFC_ISYM_CHDIR:
11884 1410 : case GFC_ISYM_CHMOD:
11885 1410 : case GFC_ISYM_DTIME:
11886 1410 : case GFC_ISYM_ETIME:
11887 1410 : case GFC_ISYM_EXTENDS_TYPE_OF:
11888 1410 : case GFC_ISYM_FGET:
11889 1410 : case GFC_ISYM_FGETC:
11890 1410 : case GFC_ISYM_FNUM:
11891 1410 : case GFC_ISYM_FPUT:
11892 1410 : case GFC_ISYM_FPUTC:
11893 1410 : case GFC_ISYM_FSTAT:
11894 1410 : case GFC_ISYM_FTELL:
11895 1410 : case GFC_ISYM_GETCWD:
11896 1410 : case GFC_ISYM_GETGID:
11897 1410 : case GFC_ISYM_GETPID:
11898 1410 : case GFC_ISYM_GETUID:
11899 1410 : case GFC_ISYM_GET_TEAM:
11900 1410 : case GFC_ISYM_HOSTNM:
11901 1410 : case GFC_ISYM_IERRNO:
11902 1410 : case GFC_ISYM_IRAND:
11903 1410 : case GFC_ISYM_ISATTY:
11904 1410 : case GFC_ISYM_JN2:
11905 1410 : case GFC_ISYM_LINK:
11906 1410 : case GFC_ISYM_LSTAT:
11907 1410 : case GFC_ISYM_MATMUL:
11908 1410 : case GFC_ISYM_MCLOCK:
11909 1410 : case GFC_ISYM_MCLOCK8:
11910 1410 : case GFC_ISYM_RAND:
11911 1410 : case GFC_ISYM_REDUCE:
11912 1410 : case GFC_ISYM_RENAME:
11913 1410 : case GFC_ISYM_SECOND:
11914 1410 : case GFC_ISYM_SECNDS:
11915 1410 : case GFC_ISYM_SIGNAL:
11916 1410 : case GFC_ISYM_STAT:
11917 1410 : case GFC_ISYM_SYMLNK:
11918 1410 : case GFC_ISYM_SYSTEM:
11919 1410 : case GFC_ISYM_TIME:
11920 1410 : case GFC_ISYM_TIME8:
11921 1410 : case GFC_ISYM_UMASK:
11922 1410 : case GFC_ISYM_UNLINK:
11923 1410 : case GFC_ISYM_YN2:
11924 1410 : gfc_conv_intrinsic_funcall (se, expr);
11925 1410 : break;
11926 :
11927 0 : case GFC_ISYM_EOSHIFT:
11928 0 : case GFC_ISYM_PACK:
11929 0 : case GFC_ISYM_RESHAPE:
11930 : /* For those, expr->rank should always be >0 and thus the if above the
11931 : switch should have matched. */
11932 0 : gcc_unreachable ();
11933 3929 : break;
11934 :
11935 3929 : default:
11936 3929 : gfc_conv_intrinsic_lib_function (se, expr);
11937 3929 : break;
11938 : }
11939 : }
11940 :
11941 :
11942 : static gfc_ss *
11943 1674 : walk_inline_intrinsic_transpose (gfc_ss *ss, gfc_expr *expr)
11944 : {
11945 1674 : gfc_ss *arg_ss, *tmp_ss;
11946 1674 : gfc_actual_arglist *arg;
11947 :
11948 1674 : arg = expr->value.function.actual;
11949 :
11950 1674 : gcc_assert (arg->expr);
11951 :
11952 1674 : arg_ss = gfc_walk_subexpr (gfc_ss_terminator, arg->expr);
11953 1674 : gcc_assert (arg_ss != gfc_ss_terminator);
11954 :
11955 : for (tmp_ss = arg_ss; ; tmp_ss = tmp_ss->next)
11956 : {
11957 1785 : if (tmp_ss->info->type != GFC_SS_SCALAR
11958 : && tmp_ss->info->type != GFC_SS_REFERENCE)
11959 : {
11960 1742 : gcc_assert (tmp_ss->dimen == 2);
11961 :
11962 : /* We just invert dimensions. */
11963 1742 : std::swap (tmp_ss->dim[0], tmp_ss->dim[1]);
11964 : }
11965 :
11966 : /* Stop when tmp_ss points to the last valid element of the chain... */
11967 1785 : if (tmp_ss->next == gfc_ss_terminator)
11968 : break;
11969 : }
11970 :
11971 : /* ... so that we can attach the rest of the chain to it. */
11972 1674 : tmp_ss->next = ss;
11973 :
11974 1674 : return arg_ss;
11975 : }
11976 :
11977 :
11978 : /* Move the given dimension of the given gfc_ss list to a nested gfc_ss list.
11979 : This has the side effect of reversing the nested list, so there is no
11980 : need to call gfc_reverse_ss on it (the given list is assumed not to be
11981 : reversed yet). */
11982 :
11983 : static gfc_ss *
11984 3371 : nest_loop_dimension (gfc_ss *ss, int dim)
11985 : {
11986 3371 : int ss_dim, i;
11987 3371 : gfc_ss *new_ss, *prev_ss = gfc_ss_terminator;
11988 3371 : gfc_loopinfo *new_loop;
11989 :
11990 3371 : gcc_assert (ss != gfc_ss_terminator);
11991 :
11992 8118 : for (; ss != gfc_ss_terminator; ss = ss->next)
11993 : {
11994 4747 : new_ss = gfc_get_ss ();
11995 4747 : new_ss->next = prev_ss;
11996 4747 : new_ss->parent = ss;
11997 4747 : new_ss->info = ss->info;
11998 4747 : new_ss->info->refcount++;
11999 4747 : if (ss->dimen != 0)
12000 : {
12001 4684 : gcc_assert (ss->info->type != GFC_SS_SCALAR
12002 : && ss->info->type != GFC_SS_REFERENCE);
12003 :
12004 4684 : new_ss->dimen = 1;
12005 4684 : new_ss->dim[0] = ss->dim[dim];
12006 :
12007 4684 : gcc_assert (dim < ss->dimen);
12008 :
12009 4684 : ss_dim = --ss->dimen;
12010 10430 : for (i = dim; i < ss_dim; i++)
12011 5746 : ss->dim[i] = ss->dim[i + 1];
12012 :
12013 4684 : ss->dim[ss_dim] = 0;
12014 : }
12015 4747 : prev_ss = new_ss;
12016 :
12017 4747 : if (ss->nested_ss)
12018 : {
12019 81 : ss->nested_ss->parent = new_ss;
12020 81 : new_ss->nested_ss = ss->nested_ss;
12021 : }
12022 4747 : ss->nested_ss = new_ss;
12023 : }
12024 :
12025 3371 : new_loop = gfc_get_loopinfo ();
12026 3371 : gfc_init_loopinfo (new_loop);
12027 :
12028 3371 : gcc_assert (prev_ss != NULL);
12029 3371 : gcc_assert (prev_ss != gfc_ss_terminator);
12030 3371 : gfc_add_ss_to_loop (new_loop, prev_ss);
12031 3371 : return new_ss->parent;
12032 : }
12033 :
12034 :
12035 : /* Create the gfc_ss list for the SUM/PRODUCT arguments when the function
12036 : is to be inlined. */
12037 :
12038 : static gfc_ss *
12039 575 : walk_inline_intrinsic_arith (gfc_ss *ss, gfc_expr *expr)
12040 : {
12041 575 : gfc_ss *tmp_ss, *tail, *array_ss;
12042 575 : gfc_actual_arglist *arg1, *arg2, *arg3;
12043 575 : int sum_dim;
12044 575 : bool scalar_mask = false;
12045 :
12046 : /* The rank of the result will be determined later. */
12047 575 : arg1 = expr->value.function.actual;
12048 575 : arg2 = arg1->next;
12049 575 : arg3 = arg2->next;
12050 575 : gcc_assert (arg3 != NULL);
12051 :
12052 575 : if (expr->rank == 0)
12053 : return ss;
12054 :
12055 575 : tmp_ss = gfc_ss_terminator;
12056 :
12057 575 : if (arg3->expr)
12058 : {
12059 118 : gfc_ss *mask_ss;
12060 :
12061 118 : mask_ss = gfc_walk_subexpr (tmp_ss, arg3->expr);
12062 118 : if (mask_ss == tmp_ss)
12063 34 : scalar_mask = 1;
12064 :
12065 : tmp_ss = mask_ss;
12066 : }
12067 :
12068 575 : array_ss = gfc_walk_subexpr (tmp_ss, arg1->expr);
12069 575 : gcc_assert (array_ss != tmp_ss);
12070 :
12071 : /* Odd thing: If the mask is scalar, it is used by the frontend after
12072 : the array (to make an if around the nested loop). Thus it shall
12073 : be after array_ss once the gfc_ss list is reversed. */
12074 575 : if (scalar_mask)
12075 34 : tmp_ss = gfc_get_scalar_ss (array_ss, arg3->expr);
12076 : else
12077 : tmp_ss = array_ss;
12078 :
12079 : /* "Hide" the dimension on which we will sum in the first arg's scalarization
12080 : chain. */
12081 575 : sum_dim = mpz_get_si (arg2->expr->value.integer) - 1;
12082 575 : tail = nest_loop_dimension (tmp_ss, sum_dim);
12083 575 : tail->next = ss;
12084 :
12085 575 : return tmp_ss;
12086 : }
12087 :
12088 :
12089 : /* Create the gfc_ss list for the arguments to MINLOC or MAXLOC when the
12090 : function is to be inlined. */
12091 :
12092 : static gfc_ss *
12093 6085 : walk_inline_intrinsic_minmaxloc (gfc_ss *ss, gfc_expr *expr ATTRIBUTE_UNUSED)
12094 : {
12095 6085 : if (expr->rank == 0)
12096 : return ss;
12097 :
12098 6085 : gfc_actual_arglist *array_arg = expr->value.function.actual;
12099 6085 : gfc_actual_arglist *dim_arg = array_arg->next;
12100 6085 : gfc_actual_arglist *mask_arg = dim_arg->next;
12101 6085 : gfc_actual_arglist *kind_arg = mask_arg->next;
12102 6085 : gfc_actual_arglist *back_arg = kind_arg->next;
12103 :
12104 6085 : gfc_expr *array = array_arg->expr;
12105 6085 : gfc_expr *dim = dim_arg->expr;
12106 6085 : gfc_expr *mask = mask_arg->expr;
12107 6085 : gfc_expr *back = back_arg->expr;
12108 :
12109 6085 : if (dim == nullptr)
12110 3289 : return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC);
12111 :
12112 2796 : gfc_ss *tmp_ss = gfc_ss_terminator;
12113 :
12114 2796 : bool scalar_mask = false;
12115 2796 : if (mask)
12116 : {
12117 1866 : gfc_ss *mask_ss = gfc_walk_subexpr (tmp_ss, mask);
12118 1866 : if (mask_ss == tmp_ss)
12119 : scalar_mask = true;
12120 1174 : else if (maybe_absent_optional_variable (mask))
12121 20 : mask_ss->info->can_be_null_ref = true;
12122 :
12123 : tmp_ss = mask_ss;
12124 : }
12125 :
12126 2796 : gfc_ss *array_ss = gfc_walk_subexpr (tmp_ss, array);
12127 2796 : gcc_assert (array_ss != tmp_ss);
12128 :
12129 2796 : tmp_ss = array_ss;
12130 :
12131 : /* Move the dimension on which we will sum to a separate nested scalarization
12132 : chain, "hiding" that dimension from the outer scalarization. */
12133 2796 : int dim_val = mpz_get_si (dim->value.integer);
12134 2796 : gfc_ss *tail = nest_loop_dimension (tmp_ss, dim_val - 1);
12135 :
12136 2796 : if (back && array->rank > 1)
12137 : {
12138 : /* If there are nested scalarization loops, include BACK in the
12139 : scalarization chains to avoid evaluating it multiple times in a loop.
12140 : Otherwise, prefer to handle it outside of scalarization. */
12141 2796 : gfc_ss *back_ss = gfc_get_scalar_ss (ss, back);
12142 2796 : back_ss->info->type = GFC_SS_REFERENCE;
12143 2796 : if (maybe_absent_optional_variable (back))
12144 16 : back_ss->info->can_be_null_ref = true;
12145 :
12146 2796 : tail->next = back_ss;
12147 2796 : }
12148 : else
12149 0 : tail->next = ss;
12150 :
12151 2796 : if (scalar_mask)
12152 : {
12153 692 : tmp_ss = gfc_get_scalar_ss (tmp_ss, mask);
12154 : /* MASK can be a forwarded optional argument, so make the necessary setup
12155 : to avoid the scalarizer generating any unguarded pointer dereference in
12156 : that case. */
12157 692 : tmp_ss->info->type = GFC_SS_REFERENCE;
12158 692 : if (maybe_absent_optional_variable (mask))
12159 4 : tmp_ss->info->can_be_null_ref = true;
12160 : }
12161 :
12162 : return tmp_ss;
12163 : }
12164 :
12165 :
12166 : static gfc_ss *
12167 8334 : walk_inline_intrinsic_function (gfc_ss * ss, gfc_expr * expr)
12168 : {
12169 :
12170 8334 : switch (expr->value.function.isym->id)
12171 : {
12172 575 : case GFC_ISYM_PRODUCT:
12173 575 : case GFC_ISYM_SUM:
12174 575 : return walk_inline_intrinsic_arith (ss, expr);
12175 :
12176 1674 : case GFC_ISYM_TRANSPOSE:
12177 1674 : return walk_inline_intrinsic_transpose (ss, expr);
12178 :
12179 6085 : case GFC_ISYM_MAXLOC:
12180 6085 : case GFC_ISYM_MINLOC:
12181 6085 : return walk_inline_intrinsic_minmaxloc (ss, expr);
12182 :
12183 0 : default:
12184 0 : gcc_unreachable ();
12185 : }
12186 : gcc_unreachable ();
12187 : }
12188 :
12189 :
12190 : /* This generates code to execute before entering the scalarization loop.
12191 : Currently does nothing. */
12192 :
12193 : void
12194 11605 : gfc_add_intrinsic_ss_code (gfc_loopinfo * loop ATTRIBUTE_UNUSED, gfc_ss * ss)
12195 : {
12196 11605 : switch (ss->info->expr->value.function.isym->id)
12197 : {
12198 11605 : case GFC_ISYM_UBOUND:
12199 11605 : case GFC_ISYM_LBOUND:
12200 11605 : case GFC_ISYM_COSHAPE:
12201 11605 : case GFC_ISYM_UCOBOUND:
12202 11605 : case GFC_ISYM_LCOBOUND:
12203 11605 : case GFC_ISYM_MAXLOC:
12204 11605 : case GFC_ISYM_MINLOC:
12205 11605 : case GFC_ISYM_THIS_IMAGE:
12206 11605 : case GFC_ISYM_SHAPE:
12207 11605 : break;
12208 :
12209 0 : default:
12210 0 : gcc_unreachable ();
12211 : }
12212 11605 : }
12213 :
12214 :
12215 : /* The LBOUND, LCOBOUND, UBOUND, UCOBOUND, and SHAPE intrinsics with
12216 : one parameter are expanded into code inside the scalarization loop. */
12217 :
12218 : static gfc_ss *
12219 10185 : gfc_walk_intrinsic_bound (gfc_ss * ss, gfc_expr * expr)
12220 : {
12221 10185 : if (expr->value.function.actual->expr->ts.type == BT_CLASS)
12222 438 : gfc_add_class_array_ref (expr->value.function.actual->expr);
12223 :
12224 : /* The two argument version returns a scalar. */
12225 10185 : if (expr->value.function.isym->id != GFC_ISYM_SHAPE
12226 3522 : && expr->value.function.isym->id != GFC_ISYM_COSHAPE
12227 3518 : && expr->value.function.actual->next->expr)
12228 : return ss;
12229 :
12230 10185 : return gfc_get_array_ss (ss, expr, 1, GFC_SS_INTRINSIC);
12231 : }
12232 :
12233 :
12234 : /* Walk an intrinsic array libcall. */
12235 :
12236 : static gfc_ss *
12237 14518 : gfc_walk_intrinsic_libfunc (gfc_ss * ss, gfc_expr * expr)
12238 : {
12239 14518 : gcc_assert (expr->rank > 0);
12240 14518 : return gfc_get_array_ss (ss, expr, expr->rank, GFC_SS_FUNCTION);
12241 : }
12242 :
12243 :
12244 : /* Return whether the function call expression EXPR will be expanded
12245 : inline by gfc_conv_intrinsic_function. */
12246 :
12247 : bool
12248 305180 : gfc_inline_intrinsic_function_p (gfc_expr *expr)
12249 : {
12250 305180 : gfc_actual_arglist *args, *dim_arg, *mask_arg;
12251 305180 : gfc_expr *maskexpr;
12252 :
12253 305180 : gfc_intrinsic_sym *isym = expr->value.function.isym;
12254 305180 : if (!isym)
12255 : return false;
12256 :
12257 305138 : switch (isym->id)
12258 : {
12259 5111 : case GFC_ISYM_PRODUCT:
12260 5111 : case GFC_ISYM_SUM:
12261 : /* Disable inline expansion if code size matters. */
12262 5111 : if (optimize_size)
12263 : return false;
12264 :
12265 4255 : args = expr->value.function.actual;
12266 4255 : dim_arg = args->next;
12267 :
12268 : /* We need to be able to subset the SUM argument at compile-time. */
12269 4255 : if (dim_arg->expr && dim_arg->expr->expr_type != EXPR_CONSTANT)
12270 : return false;
12271 :
12272 : /* FIXME: If MASK is optional for a more than two-dimensional
12273 : argument, the scalarizer gets confused if the mask is
12274 : absent. See PR 82995. For now, fall back to the library
12275 : function. */
12276 :
12277 3643 : mask_arg = dim_arg->next;
12278 3643 : maskexpr = mask_arg->expr;
12279 :
12280 3643 : if (expr->rank > 0 && maskexpr && maskexpr->expr_type == EXPR_VARIABLE
12281 276 : && maskexpr->symtree->n.sym->attr.dummy
12282 48 : && maskexpr->symtree->n.sym->attr.optional)
12283 : return false;
12284 :
12285 : return true;
12286 :
12287 : case GFC_ISYM_TRANSPOSE:
12288 : return true;
12289 :
12290 57188 : case GFC_ISYM_MINLOC:
12291 57188 : case GFC_ISYM_MAXLOC:
12292 57188 : {
12293 57188 : if ((isym->id == GFC_ISYM_MINLOC
12294 30521 : && (flag_inline_intrinsics
12295 30521 : & GFC_FLAG_INLINE_INTRINSIC_MINLOC) == 0)
12296 46611 : || (isym->id == GFC_ISYM_MAXLOC
12297 26667 : && (flag_inline_intrinsics
12298 26667 : & GFC_FLAG_INLINE_INTRINSIC_MAXLOC) == 0))
12299 : return false;
12300 :
12301 37638 : gfc_actual_arglist *array_arg = expr->value.function.actual;
12302 37638 : gfc_actual_arglist *dim_arg = array_arg->next;
12303 :
12304 37638 : gfc_expr *array = array_arg->expr;
12305 37638 : gfc_expr *dim = dim_arg->expr;
12306 :
12307 37638 : if (!(array->ts.type == BT_INTEGER
12308 : || array->ts.type == BT_REAL))
12309 : return false;
12310 :
12311 34658 : if (array->rank == 1)
12312 : return true;
12313 :
12314 20711 : if (dim != nullptr
12315 13372 : && dim->expr_type != EXPR_CONSTANT)
12316 : return false;
12317 :
12318 : return true;
12319 : }
12320 :
12321 : default:
12322 : return false;
12323 : }
12324 : }
12325 :
12326 :
12327 : /* Returns nonzero if the specified intrinsic function call maps directly to
12328 : an external library call. Should only be used for functions that return
12329 : arrays. */
12330 :
12331 : int
12332 88133 : gfc_is_intrinsic_libcall (gfc_expr * expr)
12333 : {
12334 88133 : gcc_assert (expr->expr_type == EXPR_FUNCTION && expr->value.function.isym);
12335 88133 : gcc_assert (expr->rank > 0);
12336 :
12337 88133 : if (gfc_inline_intrinsic_function_p (expr))
12338 : return 0;
12339 :
12340 73516 : switch (expr->value.function.isym->id)
12341 : {
12342 : case GFC_ISYM_ALL:
12343 : case GFC_ISYM_ANY:
12344 : case GFC_ISYM_COUNT:
12345 : case GFC_ISYM_FINDLOC:
12346 : case GFC_ISYM_JN2:
12347 : case GFC_ISYM_IANY:
12348 : case GFC_ISYM_IALL:
12349 : case GFC_ISYM_IPARITY:
12350 : case GFC_ISYM_MATMUL:
12351 : case GFC_ISYM_MAXLOC:
12352 : case GFC_ISYM_MAXVAL:
12353 : case GFC_ISYM_MINLOC:
12354 : case GFC_ISYM_MINVAL:
12355 : case GFC_ISYM_NORM2:
12356 : case GFC_ISYM_PARITY:
12357 : case GFC_ISYM_PRODUCT:
12358 : case GFC_ISYM_SUM:
12359 : case GFC_ISYM_SPREAD:
12360 : case GFC_ISYM_YN2:
12361 : /* Ignore absent optional parameters. */
12362 : return 1;
12363 :
12364 15873 : case GFC_ISYM_CSHIFT:
12365 15873 : case GFC_ISYM_EOSHIFT:
12366 15873 : case GFC_ISYM_GET_TEAM:
12367 15873 : case GFC_ISYM_FAILED_IMAGES:
12368 15873 : case GFC_ISYM_STOPPED_IMAGES:
12369 15873 : case GFC_ISYM_PACK:
12370 15873 : case GFC_ISYM_REDUCE:
12371 15873 : case GFC_ISYM_RESHAPE:
12372 15873 : case GFC_ISYM_UNPACK:
12373 : /* Pass absent optional parameters. */
12374 15873 : return 2;
12375 :
12376 : default:
12377 : return 0;
12378 : }
12379 : }
12380 :
12381 : /* Walk an intrinsic function. */
12382 : gfc_ss *
12383 55967 : gfc_walk_intrinsic_function (gfc_ss * ss, gfc_expr * expr,
12384 : gfc_intrinsic_sym * isym)
12385 : {
12386 55967 : gcc_assert (isym);
12387 :
12388 55967 : if (isym->elemental)
12389 18432 : return gfc_walk_elemental_function_args (ss, expr->value.function.actual,
12390 : expr->value.function.isym,
12391 18432 : GFC_SS_SCALAR);
12392 :
12393 37535 : if (expr->rank == 0 && expr->corank == 0)
12394 : return ss;
12395 :
12396 33037 : if (gfc_inline_intrinsic_function_p (expr))
12397 8334 : return walk_inline_intrinsic_function (ss, expr);
12398 :
12399 24703 : if (expr->rank != 0 && gfc_is_intrinsic_libcall (expr))
12400 13529 : return gfc_walk_intrinsic_libfunc (ss, expr);
12401 :
12402 : /* Special cases. */
12403 11174 : switch (isym->id)
12404 : {
12405 10185 : case GFC_ISYM_COSHAPE:
12406 10185 : case GFC_ISYM_LBOUND:
12407 10185 : case GFC_ISYM_LCOBOUND:
12408 10185 : case GFC_ISYM_UBOUND:
12409 10185 : case GFC_ISYM_UCOBOUND:
12410 10185 : case GFC_ISYM_THIS_IMAGE:
12411 10185 : case GFC_ISYM_SHAPE:
12412 10185 : return gfc_walk_intrinsic_bound (ss, expr);
12413 :
12414 989 : case GFC_ISYM_TRANSFER:
12415 989 : case GFC_ISYM_CAF_GET:
12416 989 : return gfc_walk_intrinsic_libfunc (ss, expr);
12417 :
12418 0 : default:
12419 : /* This probably meant someone forgot to add an intrinsic to the above
12420 : list(s) when they implemented it, or something's gone horribly
12421 : wrong. */
12422 0 : gcc_unreachable ();
12423 : }
12424 : }
12425 :
12426 : static tree
12427 91 : conv_co_collective (gfc_code *code)
12428 : {
12429 91 : gfc_se argse;
12430 91 : stmtblock_t block, post_block;
12431 91 : tree fndecl, array = NULL_TREE, strlen, image_index, stat, errmsg, errmsg_len;
12432 91 : gfc_expr *image_idx_expr, *stat_expr, *errmsg_expr, *opr_expr;
12433 :
12434 91 : gfc_start_block (&block);
12435 91 : gfc_init_block (&post_block);
12436 :
12437 91 : if (code->resolved_isym->id == GFC_ISYM_CO_REDUCE)
12438 : {
12439 17 : opr_expr = code->ext.actual->next->expr;
12440 17 : image_idx_expr = code->ext.actual->next->next->expr;
12441 17 : stat_expr = code->ext.actual->next->next->next->expr;
12442 17 : errmsg_expr = code->ext.actual->next->next->next->next->expr;
12443 : }
12444 : else
12445 : {
12446 74 : opr_expr = NULL;
12447 74 : image_idx_expr = code->ext.actual->next->expr;
12448 74 : stat_expr = code->ext.actual->next->next->expr;
12449 74 : errmsg_expr = code->ext.actual->next->next->next->expr;
12450 : }
12451 :
12452 : /* stat. */
12453 91 : if (stat_expr)
12454 : {
12455 59 : gfc_init_se (&argse, NULL);
12456 59 : gfc_conv_expr (&argse, stat_expr);
12457 59 : gfc_add_block_to_block (&block, &argse.pre);
12458 59 : gfc_add_block_to_block (&post_block, &argse.post);
12459 59 : stat = argse.expr;
12460 59 : if (flag_coarray != GFC_FCOARRAY_SINGLE)
12461 32 : stat = gfc_build_addr_expr (NULL_TREE, stat);
12462 : }
12463 32 : else if (flag_coarray == GFC_FCOARRAY_SINGLE)
12464 : stat = NULL_TREE;
12465 : else
12466 22 : stat = null_pointer_node;
12467 :
12468 : /* Early exit for GFC_FCOARRAY_SINGLE. */
12469 91 : if (flag_coarray == GFC_FCOARRAY_SINGLE)
12470 : {
12471 37 : if (stat != NULL_TREE)
12472 : {
12473 : /* For optional stats, check the pointer is valid before zero'ing. */
12474 27 : if (gfc_expr_attr (stat_expr).optional)
12475 : {
12476 12 : tree tmp;
12477 12 : stmtblock_t ass_block;
12478 12 : gfc_start_block (&ass_block);
12479 12 : gfc_add_modify (&ass_block, stat,
12480 12 : fold_convert (TREE_TYPE (stat),
12481 : integer_zero_node));
12482 12 : tmp = fold_build2 (NE_EXPR, logical_type_node,
12483 : gfc_build_addr_expr (NULL_TREE, stat),
12484 : null_pointer_node);
12485 12 : tmp = fold_build3 (COND_EXPR, void_type_node, tmp,
12486 : gfc_finish_block (&ass_block),
12487 : build_empty_stmt (input_location));
12488 12 : gfc_add_expr_to_block (&block, tmp);
12489 : }
12490 : else
12491 15 : gfc_add_modify (&block, stat,
12492 15 : fold_convert (TREE_TYPE (stat), integer_zero_node));
12493 : }
12494 37 : return gfc_finish_block (&block);
12495 : }
12496 :
12497 5 : gfc_symbol *derived = code->ext.actual->expr->ts.type == BT_DERIVED
12498 54 : ? code->ext.actual->expr->ts.u.derived : NULL;
12499 :
12500 : /* Handle the array. */
12501 54 : gfc_init_se (&argse, NULL);
12502 54 : if (!derived || !derived->attr.alloc_comp
12503 1 : || code->resolved_isym->id != GFC_ISYM_CO_BROADCAST)
12504 : {
12505 53 : if (code->ext.actual->expr->rank == 0)
12506 : {
12507 22 : symbol_attribute attr;
12508 22 : gfc_clear_attr (&attr);
12509 22 : gfc_init_se (&argse, NULL);
12510 22 : gfc_conv_expr (&argse, code->ext.actual->expr);
12511 22 : gfc_add_block_to_block (&block, &argse.pre);
12512 22 : gfc_add_block_to_block (&post_block, &argse.post);
12513 22 : array = gfc_conv_scalar_to_descriptor (&argse, argse.expr, attr);
12514 22 : array = gfc_build_addr_expr (NULL_TREE, array);
12515 : }
12516 : else
12517 : {
12518 31 : argse.want_pointer = 1;
12519 31 : gfc_conv_expr_descriptor (&argse, code->ext.actual->expr);
12520 31 : array = argse.expr;
12521 : }
12522 : }
12523 :
12524 54 : gfc_add_block_to_block (&block, &argse.pre);
12525 54 : gfc_add_block_to_block (&post_block, &argse.post);
12526 :
12527 54 : if (code->ext.actual->expr->ts.type == BT_CHARACTER)
12528 15 : strlen = argse.string_length;
12529 : else
12530 39 : strlen = integer_zero_node;
12531 :
12532 : /* image_index. */
12533 54 : if (image_idx_expr)
12534 : {
12535 35 : gfc_init_se (&argse, NULL);
12536 35 : gfc_conv_expr (&argse, image_idx_expr);
12537 35 : gfc_add_block_to_block (&block, &argse.pre);
12538 35 : gfc_add_block_to_block (&post_block, &argse.post);
12539 35 : image_index = fold_convert (integer_type_node, argse.expr);
12540 : }
12541 : else
12542 19 : image_index = integer_zero_node;
12543 :
12544 : /* errmsg. */
12545 54 : if (errmsg_expr)
12546 : {
12547 25 : gfc_init_se (&argse, NULL);
12548 25 : gfc_conv_expr (&argse, errmsg_expr);
12549 25 : gfc_add_block_to_block (&block, &argse.pre);
12550 25 : gfc_add_block_to_block (&post_block, &argse.post);
12551 25 : errmsg = argse.expr;
12552 25 : errmsg_len = fold_convert (size_type_node, argse.string_length);
12553 : }
12554 : else
12555 : {
12556 29 : errmsg = null_pointer_node;
12557 29 : errmsg_len = build_zero_cst (size_type_node);
12558 : }
12559 :
12560 : /* Generate the function call. */
12561 54 : switch (code->resolved_isym->id)
12562 : {
12563 20 : case GFC_ISYM_CO_BROADCAST:
12564 20 : fndecl = gfor_fndecl_co_broadcast;
12565 20 : break;
12566 8 : case GFC_ISYM_CO_MAX:
12567 8 : fndecl = gfor_fndecl_co_max;
12568 8 : break;
12569 6 : case GFC_ISYM_CO_MIN:
12570 6 : fndecl = gfor_fndecl_co_min;
12571 6 : break;
12572 12 : case GFC_ISYM_CO_REDUCE:
12573 12 : fndecl = gfor_fndecl_co_reduce;
12574 12 : break;
12575 8 : case GFC_ISYM_CO_SUM:
12576 8 : fndecl = gfor_fndecl_co_sum;
12577 8 : break;
12578 0 : default:
12579 0 : gcc_unreachable ();
12580 : }
12581 :
12582 54 : if (derived && derived->attr.alloc_comp
12583 1 : && code->resolved_isym->id == GFC_ISYM_CO_BROADCAST)
12584 : /* The derived type has the attribute 'alloc_comp'. */
12585 : {
12586 2 : tree tmp = gfc_bcast_alloc_comp (derived, code->ext.actual->expr,
12587 1 : code->ext.actual->expr->rank,
12588 : image_index, stat, errmsg, errmsg_len);
12589 1 : gfc_add_expr_to_block (&block, tmp);
12590 1 : }
12591 : else
12592 : {
12593 53 : if (code->resolved_isym->id == GFC_ISYM_CO_SUM
12594 45 : || code->resolved_isym->id == GFC_ISYM_CO_BROADCAST)
12595 27 : fndecl = build_call_expr_loc (input_location, fndecl, 5, array,
12596 : image_index, stat, errmsg, errmsg_len);
12597 26 : else if (code->resolved_isym->id != GFC_ISYM_CO_REDUCE)
12598 14 : fndecl = build_call_expr_loc (input_location, fndecl, 6, array,
12599 : image_index, stat, errmsg,
12600 : strlen, errmsg_len);
12601 : else
12602 : {
12603 12 : tree opr, opr_flags;
12604 :
12605 : // FIXME: Handle TS29113's bind(C) strings with descriptor.
12606 12 : int opr_flag_int;
12607 12 : if (gfc_is_proc_ptr_comp (opr_expr))
12608 : {
12609 0 : gfc_symbol *sym = gfc_get_proc_ptr_comp (opr_expr)->ts.interface;
12610 0 : opr_flag_int = sym->attr.dimension
12611 0 : || (sym->ts.type == BT_CHARACTER
12612 0 : && !sym->attr.is_bind_c)
12613 0 : ? GFC_CAF_BYREF : 0;
12614 0 : opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
12615 0 : && !sym->attr.is_bind_c
12616 0 : ? GFC_CAF_HIDDENLEN : 0;
12617 0 : opr_flag_int |= sym->formal->sym->attr.value
12618 0 : ? GFC_CAF_ARG_VALUE : 0;
12619 : }
12620 : else
12621 : {
12622 12 : opr_flag_int = gfc_return_by_reference (opr_expr->symtree->n.sym)
12623 12 : ? GFC_CAF_BYREF : 0;
12624 24 : opr_flag_int |= opr_expr->ts.type == BT_CHARACTER
12625 0 : && !opr_expr->symtree->n.sym->attr.is_bind_c
12626 12 : ? GFC_CAF_HIDDENLEN : 0;
12627 12 : opr_flag_int |= opr_expr->symtree->n.sym->formal->sym->attr.value
12628 12 : ? GFC_CAF_ARG_VALUE : 0;
12629 : }
12630 12 : opr_flags = build_int_cst (integer_type_node, opr_flag_int);
12631 12 : gfc_conv_expr (&argse, opr_expr);
12632 12 : opr = argse.expr;
12633 12 : fndecl = build_call_expr_loc (input_location, fndecl, 8, array, opr,
12634 : opr_flags, image_index, stat, errmsg,
12635 : strlen, errmsg_len);
12636 : }
12637 : }
12638 :
12639 54 : gfc_add_expr_to_block (&block, fndecl);
12640 54 : gfc_add_block_to_block (&block, &post_block);
12641 :
12642 54 : return gfc_finish_block (&block);
12643 : }
12644 :
12645 :
12646 : static tree
12647 95 : conv_intrinsic_atomic_op (gfc_code *code)
12648 : {
12649 95 : gfc_se argse;
12650 95 : tree tmp, atom, value, old = NULL_TREE, stat = NULL_TREE;
12651 95 : stmtblock_t block, post_block;
12652 95 : gfc_expr *atom_expr = code->ext.actual->expr;
12653 95 : gfc_expr *stat_expr;
12654 95 : built_in_function fn;
12655 :
12656 95 : if (atom_expr->expr_type == EXPR_FUNCTION
12657 0 : && atom_expr->value.function.isym
12658 0 : && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12659 0 : atom_expr = atom_expr->value.function.actual->expr;
12660 :
12661 95 : gfc_start_block (&block);
12662 95 : gfc_init_block (&post_block);
12663 :
12664 95 : gfc_init_se (&argse, NULL);
12665 95 : argse.want_pointer = 1;
12666 95 : gfc_conv_expr (&argse, atom_expr);
12667 95 : gfc_add_block_to_block (&block, &argse.pre);
12668 95 : gfc_add_block_to_block (&post_block, &argse.post);
12669 95 : atom = argse.expr;
12670 :
12671 95 : gfc_init_se (&argse, NULL);
12672 95 : if (flag_coarray == GFC_FCOARRAY_LIB
12673 56 : && code->ext.actual->next->expr->ts.kind == atom_expr->ts.kind)
12674 54 : argse.want_pointer = 1;
12675 95 : gfc_conv_expr (&argse, code->ext.actual->next->expr);
12676 95 : gfc_add_block_to_block (&block, &argse.pre);
12677 95 : gfc_add_block_to_block (&post_block, &argse.post);
12678 95 : value = argse.expr;
12679 :
12680 95 : switch (code->resolved_isym->id)
12681 : {
12682 58 : case GFC_ISYM_ATOMIC_ADD:
12683 58 : case GFC_ISYM_ATOMIC_AND:
12684 58 : case GFC_ISYM_ATOMIC_DEF:
12685 58 : case GFC_ISYM_ATOMIC_OR:
12686 58 : case GFC_ISYM_ATOMIC_XOR:
12687 58 : stat_expr = code->ext.actual->next->next->expr;
12688 58 : if (flag_coarray == GFC_FCOARRAY_LIB)
12689 34 : old = null_pointer_node;
12690 : break;
12691 37 : default:
12692 37 : gfc_init_se (&argse, NULL);
12693 37 : if (flag_coarray == GFC_FCOARRAY_LIB)
12694 22 : argse.want_pointer = 1;
12695 37 : gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
12696 37 : gfc_add_block_to_block (&block, &argse.pre);
12697 37 : gfc_add_block_to_block (&post_block, &argse.post);
12698 37 : old = argse.expr;
12699 37 : stat_expr = code->ext.actual->next->next->next->expr;
12700 : }
12701 :
12702 : /* STAT= */
12703 95 : if (stat_expr != NULL)
12704 : {
12705 82 : gcc_assert (stat_expr->expr_type == EXPR_VARIABLE);
12706 82 : gfc_init_se (&argse, NULL);
12707 82 : if (flag_coarray == GFC_FCOARRAY_LIB)
12708 48 : argse.want_pointer = 1;
12709 82 : gfc_conv_expr_val (&argse, stat_expr);
12710 82 : gfc_add_block_to_block (&block, &argse.pre);
12711 82 : gfc_add_block_to_block (&post_block, &argse.post);
12712 82 : stat = argse.expr;
12713 : }
12714 13 : else if (flag_coarray == GFC_FCOARRAY_LIB)
12715 8 : stat = null_pointer_node;
12716 :
12717 95 : if (flag_coarray == GFC_FCOARRAY_LIB)
12718 : {
12719 56 : tree image_index, caf_decl, offset, token;
12720 56 : int op;
12721 :
12722 56 : switch (code->resolved_isym->id)
12723 : {
12724 : case GFC_ISYM_ATOMIC_ADD:
12725 : case GFC_ISYM_ATOMIC_FETCH_ADD:
12726 : op = (int) GFC_CAF_ATOMIC_ADD;
12727 : break;
12728 12 : case GFC_ISYM_ATOMIC_AND:
12729 12 : case GFC_ISYM_ATOMIC_FETCH_AND:
12730 12 : op = (int) GFC_CAF_ATOMIC_AND;
12731 12 : break;
12732 12 : case GFC_ISYM_ATOMIC_OR:
12733 12 : case GFC_ISYM_ATOMIC_FETCH_OR:
12734 12 : op = (int) GFC_CAF_ATOMIC_OR;
12735 12 : break;
12736 12 : case GFC_ISYM_ATOMIC_XOR:
12737 12 : case GFC_ISYM_ATOMIC_FETCH_XOR:
12738 12 : op = (int) GFC_CAF_ATOMIC_XOR;
12739 12 : break;
12740 11 : case GFC_ISYM_ATOMIC_DEF:
12741 11 : op = 0; /* Unused. */
12742 11 : break;
12743 0 : default:
12744 0 : gcc_unreachable ();
12745 : }
12746 :
12747 56 : caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
12748 56 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
12749 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
12750 :
12751 56 : if (gfc_is_coindexed (atom_expr))
12752 48 : image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
12753 : else
12754 8 : image_index = integer_zero_node;
12755 :
12756 : /* Ensure VALUE names addressable storage: taking the address of a
12757 : constant is invalid in C, and scalars need a temporary as well. */
12758 56 : if (!POINTER_TYPE_P (TREE_TYPE (value)))
12759 : {
12760 42 : tree elem
12761 42 : = fold_convert (TREE_TYPE (TREE_TYPE (atom)), value);
12762 42 : elem = gfc_trans_force_lval (&block, elem);
12763 42 : value = gfc_build_addr_expr (NULL_TREE, elem);
12764 : }
12765 14 : else if (TREE_CODE (value) == ADDR_EXPR
12766 14 : && TREE_CONSTANT (TREE_OPERAND (value, 0)))
12767 : {
12768 0 : tree elem
12769 0 : = fold_convert (TREE_TYPE (TREE_TYPE (atom)),
12770 : build_fold_indirect_ref (value));
12771 0 : elem = gfc_trans_force_lval (&block, elem);
12772 0 : value = gfc_build_addr_expr (NULL_TREE, elem);
12773 : }
12774 :
12775 56 : gfc_init_se (&argse, NULL);
12776 56 : gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
12777 : atom_expr);
12778 :
12779 56 : gfc_add_block_to_block (&block, &argse.pre);
12780 56 : if (code->resolved_isym->id == GFC_ISYM_ATOMIC_DEF)
12781 11 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_def, 7,
12782 : token, offset, image_index, value, stat,
12783 : build_int_cst (integer_type_node,
12784 11 : (int) atom_expr->ts.type),
12785 : build_int_cst (integer_type_node,
12786 11 : (int) atom_expr->ts.kind));
12787 : else
12788 45 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_op, 9,
12789 45 : build_int_cst (integer_type_node, op),
12790 : token, offset, image_index, value, old, stat,
12791 : build_int_cst (integer_type_node,
12792 45 : (int) atom_expr->ts.type),
12793 : build_int_cst (integer_type_node,
12794 45 : (int) atom_expr->ts.kind));
12795 :
12796 56 : gfc_add_expr_to_block (&block, tmp);
12797 56 : gfc_add_block_to_block (&block, &argse.post);
12798 56 : gfc_add_block_to_block (&block, &post_block);
12799 56 : return gfc_finish_block (&block);
12800 : }
12801 :
12802 :
12803 39 : switch (code->resolved_isym->id)
12804 : {
12805 : case GFC_ISYM_ATOMIC_ADD:
12806 : case GFC_ISYM_ATOMIC_FETCH_ADD:
12807 : fn = BUILT_IN_ATOMIC_FETCH_ADD_N;
12808 : break;
12809 8 : case GFC_ISYM_ATOMIC_AND:
12810 8 : case GFC_ISYM_ATOMIC_FETCH_AND:
12811 8 : fn = BUILT_IN_ATOMIC_FETCH_AND_N;
12812 8 : break;
12813 9 : case GFC_ISYM_ATOMIC_DEF:
12814 9 : fn = BUILT_IN_ATOMIC_STORE_N;
12815 9 : break;
12816 8 : case GFC_ISYM_ATOMIC_OR:
12817 8 : case GFC_ISYM_ATOMIC_FETCH_OR:
12818 8 : fn = BUILT_IN_ATOMIC_FETCH_OR_N;
12819 8 : break;
12820 8 : case GFC_ISYM_ATOMIC_XOR:
12821 8 : case GFC_ISYM_ATOMIC_FETCH_XOR:
12822 8 : fn = BUILT_IN_ATOMIC_FETCH_XOR_N;
12823 8 : break;
12824 0 : default:
12825 0 : gcc_unreachable ();
12826 : }
12827 :
12828 39 : tmp = TREE_TYPE (TREE_TYPE (atom));
12829 78 : fn = (built_in_function) ((int) fn
12830 39 : + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
12831 39 : + 1);
12832 39 : tree itype = TREE_TYPE (TREE_TYPE (atom));
12833 39 : tmp = builtin_decl_explicit (fn);
12834 :
12835 39 : switch (code->resolved_isym->id)
12836 : {
12837 24 : case GFC_ISYM_ATOMIC_ADD:
12838 24 : case GFC_ISYM_ATOMIC_AND:
12839 24 : case GFC_ISYM_ATOMIC_DEF:
12840 24 : case GFC_ISYM_ATOMIC_OR:
12841 24 : case GFC_ISYM_ATOMIC_XOR:
12842 24 : tmp = build_call_expr_loc (input_location, tmp, 3, atom,
12843 : fold_convert (itype, value),
12844 : build_int_cst (integer_type_node, MEMMODEL_RELAXED));
12845 24 : gfc_add_expr_to_block (&block, tmp);
12846 24 : break;
12847 15 : default:
12848 15 : tmp = build_call_expr_loc (input_location, tmp, 3, atom,
12849 : fold_convert (itype, value),
12850 : build_int_cst (integer_type_node, MEMMODEL_RELAXED));
12851 15 : gfc_add_modify (&block, old, fold_convert (TREE_TYPE (old), tmp));
12852 15 : break;
12853 : }
12854 :
12855 39 : if (stat != NULL_TREE)
12856 34 : gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
12857 39 : gfc_add_block_to_block (&block, &post_block);
12858 39 : return gfc_finish_block (&block);
12859 : }
12860 :
12861 :
12862 : static tree
12863 176 : conv_intrinsic_atomic_ref (gfc_code *code)
12864 : {
12865 176 : gfc_se argse;
12866 176 : tree tmp, atom, value, stat = NULL_TREE;
12867 176 : stmtblock_t block, post_block;
12868 176 : built_in_function fn;
12869 176 : gfc_expr *atom_expr = code->ext.actual->next->expr;
12870 :
12871 176 : if (atom_expr->expr_type == EXPR_FUNCTION
12872 0 : && atom_expr->value.function.isym
12873 0 : && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12874 0 : atom_expr = atom_expr->value.function.actual->expr;
12875 :
12876 176 : gfc_start_block (&block);
12877 176 : gfc_init_block (&post_block);
12878 176 : gfc_init_se (&argse, NULL);
12879 176 : argse.want_pointer = 1;
12880 176 : gfc_conv_expr (&argse, atom_expr);
12881 176 : gfc_add_block_to_block (&block, &argse.pre);
12882 176 : gfc_add_block_to_block (&post_block, &argse.post);
12883 176 : atom = argse.expr;
12884 :
12885 176 : gfc_init_se (&argse, NULL);
12886 176 : if (flag_coarray == GFC_FCOARRAY_LIB
12887 115 : && code->ext.actual->expr->ts.kind == atom_expr->ts.kind)
12888 109 : argse.want_pointer = 1;
12889 176 : gfc_conv_expr (&argse, code->ext.actual->expr);
12890 176 : gfc_add_block_to_block (&block, &argse.pre);
12891 176 : gfc_add_block_to_block (&post_block, &argse.post);
12892 176 : value = argse.expr;
12893 :
12894 : /* STAT= */
12895 176 : if (code->ext.actual->next->next->expr != NULL)
12896 : {
12897 164 : gcc_assert (code->ext.actual->next->next->expr->expr_type
12898 : == EXPR_VARIABLE);
12899 164 : gfc_init_se (&argse, NULL);
12900 164 : if (flag_coarray == GFC_FCOARRAY_LIB)
12901 108 : argse.want_pointer = 1;
12902 164 : gfc_conv_expr_val (&argse, code->ext.actual->next->next->expr);
12903 164 : gfc_add_block_to_block (&block, &argse.pre);
12904 164 : gfc_add_block_to_block (&post_block, &argse.post);
12905 164 : stat = argse.expr;
12906 : }
12907 12 : else if (flag_coarray == GFC_FCOARRAY_LIB)
12908 7 : stat = null_pointer_node;
12909 :
12910 176 : if (flag_coarray == GFC_FCOARRAY_LIB)
12911 : {
12912 115 : tree image_index, caf_decl, offset, token;
12913 115 : tree orig_value = NULL_TREE, vardecl = NULL_TREE;
12914 :
12915 115 : caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
12916 115 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
12917 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
12918 :
12919 115 : if (gfc_is_coindexed (atom_expr))
12920 103 : image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
12921 : else
12922 12 : image_index = integer_zero_node;
12923 :
12924 115 : gfc_init_se (&argse, NULL);
12925 115 : gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
12926 : atom_expr);
12927 115 : gfc_add_block_to_block (&block, &argse.pre);
12928 :
12929 : /* Different type, need type conversion. */
12930 115 : if (!POINTER_TYPE_P (TREE_TYPE (value)))
12931 : {
12932 6 : vardecl = gfc_create_var (TREE_TYPE (TREE_TYPE (atom)), "value");
12933 6 : orig_value = value;
12934 6 : value = gfc_build_addr_expr (NULL_TREE, vardecl);
12935 : }
12936 :
12937 115 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_ref, 7,
12938 : token, offset, image_index, value, stat,
12939 : build_int_cst (integer_type_node,
12940 115 : (int) atom_expr->ts.type),
12941 : build_int_cst (integer_type_node,
12942 115 : (int) atom_expr->ts.kind));
12943 115 : gfc_add_expr_to_block (&block, tmp);
12944 115 : if (vardecl != NULL_TREE)
12945 6 : gfc_add_modify (&block, orig_value,
12946 6 : fold_convert (TREE_TYPE (orig_value), vardecl));
12947 115 : gfc_add_block_to_block (&block, &argse.post);
12948 115 : gfc_add_block_to_block (&block, &post_block);
12949 115 : return gfc_finish_block (&block);
12950 : }
12951 :
12952 61 : tmp = TREE_TYPE (TREE_TYPE (atom));
12953 122 : fn = (built_in_function) ((int) BUILT_IN_ATOMIC_LOAD_N
12954 61 : + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
12955 61 : + 1);
12956 61 : tmp = builtin_decl_explicit (fn);
12957 61 : tmp = build_call_expr_loc (input_location, tmp, 2, atom,
12958 : build_int_cst (integer_type_node,
12959 : MEMMODEL_RELAXED));
12960 61 : gfc_add_modify (&block, value, fold_convert (TREE_TYPE (value), tmp));
12961 :
12962 61 : if (stat != NULL_TREE)
12963 56 : gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
12964 61 : gfc_add_block_to_block (&block, &post_block);
12965 61 : return gfc_finish_block (&block);
12966 : }
12967 :
12968 :
12969 : static tree
12970 14 : conv_intrinsic_atomic_cas (gfc_code *code)
12971 : {
12972 14 : gfc_se argse;
12973 14 : tree tmp, atom, old, new_val, comp, stat = NULL_TREE;
12974 14 : stmtblock_t block, post_block;
12975 14 : built_in_function fn;
12976 14 : gfc_expr *atom_expr = code->ext.actual->expr;
12977 :
12978 14 : if (atom_expr->expr_type == EXPR_FUNCTION
12979 0 : && atom_expr->value.function.isym
12980 0 : && atom_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
12981 0 : atom_expr = atom_expr->value.function.actual->expr;
12982 :
12983 14 : gfc_init_block (&block);
12984 14 : gfc_init_block (&post_block);
12985 14 : gfc_init_se (&argse, NULL);
12986 14 : argse.want_pointer = 1;
12987 14 : gfc_conv_expr (&argse, atom_expr);
12988 14 : atom = argse.expr;
12989 :
12990 14 : gfc_init_se (&argse, NULL);
12991 14 : if (flag_coarray == GFC_FCOARRAY_LIB)
12992 8 : argse.want_pointer = 1;
12993 14 : gfc_conv_expr (&argse, code->ext.actual->next->expr);
12994 14 : gfc_add_block_to_block (&block, &argse.pre);
12995 14 : gfc_add_block_to_block (&post_block, &argse.post);
12996 14 : old = argse.expr;
12997 :
12998 14 : gfc_init_se (&argse, NULL);
12999 14 : if (flag_coarray == GFC_FCOARRAY_LIB)
13000 8 : argse.want_pointer = 1;
13001 14 : gfc_conv_expr (&argse, code->ext.actual->next->next->expr);
13002 14 : gfc_add_block_to_block (&block, &argse.pre);
13003 14 : gfc_add_block_to_block (&post_block, &argse.post);
13004 14 : comp = argse.expr;
13005 :
13006 14 : gfc_init_se (&argse, NULL);
13007 14 : if (flag_coarray == GFC_FCOARRAY_LIB
13008 8 : && code->ext.actual->next->next->next->expr->ts.kind
13009 8 : == atom_expr->ts.kind)
13010 8 : argse.want_pointer = 1;
13011 14 : gfc_conv_expr (&argse, code->ext.actual->next->next->next->expr);
13012 14 : gfc_add_block_to_block (&block, &argse.pre);
13013 14 : gfc_add_block_to_block (&post_block, &argse.post);
13014 14 : new_val = argse.expr;
13015 :
13016 : /* STAT= */
13017 14 : if (code->ext.actual->next->next->next->next->expr != NULL)
13018 : {
13019 14 : gcc_assert (code->ext.actual->next->next->next->next->expr->expr_type
13020 : == EXPR_VARIABLE);
13021 14 : gfc_init_se (&argse, NULL);
13022 14 : if (flag_coarray == GFC_FCOARRAY_LIB)
13023 8 : argse.want_pointer = 1;
13024 14 : gfc_conv_expr_val (&argse,
13025 14 : code->ext.actual->next->next->next->next->expr);
13026 14 : gfc_add_block_to_block (&block, &argse.pre);
13027 14 : gfc_add_block_to_block (&post_block, &argse.post);
13028 14 : stat = argse.expr;
13029 : }
13030 0 : else if (flag_coarray == GFC_FCOARRAY_LIB)
13031 0 : stat = null_pointer_node;
13032 :
13033 14 : if (flag_coarray == GFC_FCOARRAY_LIB)
13034 : {
13035 8 : tree image_index, caf_decl, offset, token;
13036 :
13037 8 : caf_decl = gfc_get_tree_for_caf_expr (atom_expr);
13038 8 : if (TREE_CODE (TREE_TYPE (caf_decl)) == REFERENCE_TYPE)
13039 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
13040 :
13041 8 : if (gfc_is_coindexed (atom_expr))
13042 8 : image_index = gfc_caf_get_image_index (&block, atom_expr, caf_decl);
13043 : else
13044 0 : image_index = integer_zero_node;
13045 :
13046 8 : if (TREE_TYPE (TREE_TYPE (new_val)) != TREE_TYPE (TREE_TYPE (old)))
13047 : {
13048 0 : tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (old)), "new");
13049 0 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), new_val));
13050 0 : new_val = gfc_build_addr_expr (NULL_TREE, tmp);
13051 : }
13052 :
13053 8 : gfc_init_se (&argse, NULL);
13054 8 : gfc_get_caf_token_offset (&argse, &token, &offset, caf_decl, atom,
13055 : atom_expr);
13056 8 : gfc_add_block_to_block (&block, &argse.pre);
13057 :
13058 8 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_atomic_cas, 9,
13059 : token, offset, image_index, old, comp, new_val,
13060 : stat, build_int_cst (integer_type_node,
13061 8 : (int) atom_expr->ts.type),
13062 : build_int_cst (integer_type_node,
13063 8 : (int) atom_expr->ts.kind));
13064 8 : gfc_add_expr_to_block (&block, tmp);
13065 8 : gfc_add_block_to_block (&block, &argse.post);
13066 8 : gfc_add_block_to_block (&block, &post_block);
13067 8 : return gfc_finish_block (&block);
13068 : }
13069 :
13070 6 : tmp = TREE_TYPE (TREE_TYPE (atom));
13071 12 : fn = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
13072 6 : + exact_log2 (tree_to_uhwi (TYPE_SIZE_UNIT (tmp)))
13073 6 : + 1);
13074 6 : tmp = builtin_decl_explicit (fn);
13075 :
13076 6 : gfc_add_modify (&block, old, comp);
13077 12 : tmp = build_call_expr_loc (input_location, tmp, 6, atom,
13078 : gfc_build_addr_expr (NULL, old),
13079 6 : fold_convert (TREE_TYPE (old), new_val),
13080 : boolean_false_node,
13081 : build_int_cst (integer_type_node, MEMMODEL_RELAXED),
13082 : build_int_cst (integer_type_node, MEMMODEL_RELAXED));
13083 6 : gfc_add_expr_to_block (&block, tmp);
13084 :
13085 6 : if (stat != NULL_TREE)
13086 6 : gfc_add_modify (&block, stat, build_int_cst (TREE_TYPE (stat), 0));
13087 6 : gfc_add_block_to_block (&block, &post_block);
13088 6 : return gfc_finish_block (&block);
13089 : }
13090 :
13091 : static tree
13092 105 : conv_intrinsic_event_query (gfc_code *code)
13093 : {
13094 105 : gfc_se se, argse;
13095 105 : tree stat = NULL_TREE, stat2 = NULL_TREE;
13096 105 : tree count = NULL_TREE, count2 = NULL_TREE;
13097 :
13098 105 : gfc_expr *event_expr = code->ext.actual->expr;
13099 :
13100 105 : if (code->ext.actual->next->next->expr)
13101 : {
13102 18 : gcc_assert (code->ext.actual->next->next->expr->expr_type
13103 : == EXPR_VARIABLE);
13104 18 : gfc_init_se (&argse, NULL);
13105 18 : gfc_conv_expr_val (&argse, code->ext.actual->next->next->expr);
13106 18 : stat = argse.expr;
13107 : }
13108 87 : else if (flag_coarray == GFC_FCOARRAY_LIB)
13109 58 : stat = null_pointer_node;
13110 :
13111 105 : if (code->ext.actual->next->expr)
13112 : {
13113 105 : gcc_assert (code->ext.actual->next->expr->expr_type == EXPR_VARIABLE);
13114 105 : gfc_init_se (&argse, NULL);
13115 105 : gfc_conv_expr_val (&argse, code->ext.actual->next->expr);
13116 105 : count = argse.expr;
13117 : }
13118 :
13119 105 : gfc_start_block (&se.pre);
13120 105 : if (flag_coarray == GFC_FCOARRAY_LIB)
13121 : {
13122 70 : tree tmp, token, image_index;
13123 70 : tree index = build_zero_cst (gfc_array_index_type);
13124 :
13125 70 : if (event_expr->expr_type == EXPR_FUNCTION
13126 0 : && event_expr->value.function.isym
13127 0 : && event_expr->value.function.isym->id == GFC_ISYM_CAF_GET)
13128 0 : event_expr = event_expr->value.function.actual->expr;
13129 :
13130 70 : tree caf_decl = gfc_get_tree_for_caf_expr (event_expr);
13131 :
13132 70 : if (event_expr->symtree->n.sym->ts.type != BT_DERIVED
13133 70 : || event_expr->symtree->n.sym->ts.u.derived->from_intmod
13134 : != INTMOD_ISO_FORTRAN_ENV
13135 70 : || event_expr->symtree->n.sym->ts.u.derived->intmod_sym_id
13136 : != ISOFORTRAN_EVENT_TYPE)
13137 : {
13138 0 : gfc_error ("Sorry, the event component of derived type at %L is not "
13139 : "yet supported", &event_expr->where);
13140 0 : return NULL_TREE;
13141 : }
13142 :
13143 70 : if (gfc_is_coindexed (event_expr))
13144 : {
13145 0 : gfc_error ("The event variable at %L shall not be coindexed",
13146 : &event_expr->where);
13147 0 : return NULL_TREE;
13148 : }
13149 :
13150 70 : image_index = integer_zero_node;
13151 :
13152 70 : gfc_get_caf_token_offset (&se, &token, NULL, caf_decl, NULL_TREE,
13153 : event_expr);
13154 :
13155 : /* For arrays, obtain the array index. */
13156 70 : if (gfc_expr_attr (event_expr).dimension)
13157 : {
13158 52 : tree desc, tmp, extent, lbound, ubound;
13159 52 : gfc_array_ref *ar, ar2;
13160 52 : int i;
13161 :
13162 : /* TODO: Extend this, once DT components are supported. */
13163 52 : ar = &event_expr->ref->u.ar;
13164 52 : ar2 = *ar;
13165 52 : memset (ar, '\0', sizeof (*ar));
13166 52 : ar->as = ar2.as;
13167 52 : ar->type = AR_FULL;
13168 :
13169 52 : gfc_init_se (&argse, NULL);
13170 52 : argse.descriptor_only = 1;
13171 52 : gfc_conv_expr_descriptor (&argse, event_expr);
13172 52 : gfc_add_block_to_block (&se.pre, &argse.pre);
13173 52 : desc = argse.expr;
13174 52 : *ar = ar2;
13175 :
13176 52 : extent = build_one_cst (gfc_array_index_type);
13177 156 : for (i = 0; i < ar->dimen; i++)
13178 : {
13179 52 : gfc_init_se (&argse, NULL);
13180 52 : gfc_conv_expr_type (&argse, ar->start[i], gfc_array_index_type);
13181 52 : gfc_add_block_to_block (&argse.pre, &argse.pre);
13182 52 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
13183 52 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
13184 52 : TREE_TYPE (lbound), argse.expr, lbound);
13185 52 : tmp = fold_build2_loc (input_location, MULT_EXPR,
13186 52 : TREE_TYPE (tmp), extent, tmp);
13187 52 : index = fold_build2_loc (input_location, PLUS_EXPR,
13188 52 : TREE_TYPE (tmp), index, tmp);
13189 52 : if (i < ar->dimen - 1)
13190 : {
13191 0 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
13192 0 : tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
13193 0 : extent = fold_build2_loc (input_location, MULT_EXPR,
13194 0 : TREE_TYPE (tmp), extent, tmp);
13195 : }
13196 : }
13197 : }
13198 :
13199 70 : if (count != null_pointer_node && TREE_TYPE (count) != integer_type_node)
13200 : {
13201 0 : count2 = count;
13202 0 : count = gfc_create_var (integer_type_node, "count");
13203 : }
13204 :
13205 70 : if (stat != null_pointer_node && TREE_TYPE (stat) != integer_type_node)
13206 : {
13207 0 : stat2 = stat;
13208 0 : stat = gfc_create_var (integer_type_node, "stat");
13209 : }
13210 :
13211 70 : index = fold_convert (size_type_node, index);
13212 140 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_event_query, 5,
13213 : token, index, image_index, count
13214 70 : ? gfc_build_addr_expr (NULL, count) : count,
13215 70 : stat != null_pointer_node
13216 12 : ? gfc_build_addr_expr (NULL, stat) : stat);
13217 70 : gfc_add_expr_to_block (&se.pre, tmp);
13218 :
13219 70 : if (count2 != NULL_TREE)
13220 0 : gfc_add_modify (&se.pre, count2,
13221 0 : fold_convert (TREE_TYPE (count2), count));
13222 :
13223 70 : if (stat2 != NULL_TREE)
13224 0 : gfc_add_modify (&se.pre, stat2,
13225 0 : fold_convert (TREE_TYPE (stat2), stat));
13226 :
13227 70 : return gfc_finish_block (&se.pre);
13228 : }
13229 :
13230 35 : gfc_init_se (&argse, NULL);
13231 35 : gfc_conv_expr_val (&argse, code->ext.actual->expr);
13232 35 : gfc_add_modify (&se.pre, count, fold_convert (TREE_TYPE (count), argse.expr));
13233 :
13234 35 : if (stat != NULL_TREE)
13235 6 : gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
13236 :
13237 35 : return gfc_finish_block (&se.pre);
13238 : }
13239 :
13240 :
13241 : /* This is a peculiar case because of the need to do dependency checking.
13242 : It is called via trans-stmt.cc(gfc_trans_call), where it is picked out as
13243 : a special case and this function called instead of
13244 : gfc_conv_procedure_call. */
13245 : void
13246 197 : gfc_conv_intrinsic_mvbits (gfc_se *se, gfc_actual_arglist *actual_args,
13247 : gfc_loopinfo *loop)
13248 : {
13249 197 : gfc_actual_arglist *actual;
13250 197 : gfc_se argse[5];
13251 197 : gfc_expr *arg[5];
13252 197 : gfc_ss *lss;
13253 197 : int n;
13254 :
13255 197 : tree from, frompos, len, to, topos;
13256 197 : tree lenmask, oldbits, newbits, bitsize;
13257 197 : tree type, utype, above, mask1, mask2;
13258 :
13259 197 : if (loop)
13260 67 : lss = loop->ss;
13261 : else
13262 130 : lss = gfc_ss_terminator;
13263 :
13264 197 : actual = actual_args;
13265 1182 : for (n = 0; n < 5; n++, actual = actual->next)
13266 : {
13267 985 : arg[n] = actual->expr;
13268 985 : gfc_init_se (&argse[n], NULL);
13269 :
13270 985 : if (lss != gfc_ss_terminator)
13271 : {
13272 335 : gfc_copy_loopinfo_to_se (&argse[n], loop);
13273 : /* Find the ss for the expression if it is there. */
13274 335 : argse[n].ss = lss;
13275 335 : gfc_mark_ss_chain_used (lss, 1);
13276 : }
13277 :
13278 985 : gfc_conv_expr (&argse[n], arg[n]);
13279 :
13280 985 : if (loop)
13281 335 : lss = argse[n].ss;
13282 : }
13283 :
13284 197 : from = argse[0].expr;
13285 197 : frompos = argse[1].expr;
13286 197 : len = argse[2].expr;
13287 197 : to = argse[3].expr;
13288 197 : topos = argse[4].expr;
13289 :
13290 : /* The type of the result (TO). */
13291 197 : type = TREE_TYPE (to);
13292 197 : bitsize = build_int_cst (integer_type_node, TYPE_PRECISION (type));
13293 :
13294 : /* Optionally generate code for runtime argument check. */
13295 197 : if (gfc_option.rtcheck & GFC_RTCHECK_BITS)
13296 : {
13297 18 : tree nbits, below, ccond;
13298 18 : tree fp = fold_convert (long_integer_type_node, frompos);
13299 18 : tree ln = fold_convert (long_integer_type_node, len);
13300 18 : tree tp = fold_convert (long_integer_type_node, topos);
13301 18 : below = fold_build2_loc (input_location, LT_EXPR,
13302 : logical_type_node, frompos,
13303 18 : build_int_cst (TREE_TYPE (frompos), 0));
13304 18 : above = fold_build2_loc (input_location, GT_EXPR,
13305 : logical_type_node, frompos,
13306 18 : fold_convert (TREE_TYPE (frompos), bitsize));
13307 18 : ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
13308 : logical_type_node, below, above);
13309 18 : gfc_trans_runtime_check (true, false, ccond, &argse[1].pre,
13310 18 : &arg[1]->where,
13311 : "FROMPOS argument (%ld) out of range 0:%d "
13312 : "in intrinsic MVBITS", fp, bitsize);
13313 18 : below = fold_build2_loc (input_location, LT_EXPR,
13314 : logical_type_node, len,
13315 18 : build_int_cst (TREE_TYPE (len), 0));
13316 18 : above = fold_build2_loc (input_location, GT_EXPR,
13317 : logical_type_node, len,
13318 18 : fold_convert (TREE_TYPE (len), bitsize));
13319 18 : ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
13320 : logical_type_node, below, above);
13321 18 : gfc_trans_runtime_check (true, false, ccond, &argse[2].pre,
13322 18 : &arg[2]->where,
13323 : "LEN argument (%ld) out of range 0:%d "
13324 : "in intrinsic MVBITS", ln, bitsize);
13325 18 : below = fold_build2_loc (input_location, LT_EXPR,
13326 : logical_type_node, topos,
13327 18 : build_int_cst (TREE_TYPE (topos), 0));
13328 18 : above = fold_build2_loc (input_location, GT_EXPR,
13329 : logical_type_node, topos,
13330 18 : fold_convert (TREE_TYPE (topos), bitsize));
13331 18 : ccond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
13332 : logical_type_node, below, above);
13333 18 : gfc_trans_runtime_check (true, false, ccond, &argse[4].pre,
13334 18 : &arg[4]->where,
13335 : "TOPOS argument (%ld) out of range 0:%d "
13336 : "in intrinsic MVBITS", tp, bitsize);
13337 :
13338 : /* The tests above ensure that FROMPOS, LEN and TOPOS fit into short
13339 : integers. Additions below cannot overflow. */
13340 18 : nbits = fold_convert (long_integer_type_node, bitsize);
13341 18 : above = fold_build2_loc (input_location, PLUS_EXPR,
13342 : long_integer_type_node, fp, ln);
13343 18 : ccond = fold_build2_loc (input_location, GT_EXPR,
13344 : logical_type_node, above, nbits);
13345 18 : gfc_trans_runtime_check (true, false, ccond, &argse[1].pre,
13346 : &arg[1]->where,
13347 : "FROMPOS(%ld)+LEN(%ld)>BIT_SIZE(%d) "
13348 : "in intrinsic MVBITS", fp, ln, bitsize);
13349 18 : above = fold_build2_loc (input_location, PLUS_EXPR,
13350 : long_integer_type_node, tp, ln);
13351 18 : ccond = fold_build2_loc (input_location, GT_EXPR,
13352 : logical_type_node, above, nbits);
13353 18 : gfc_trans_runtime_check (true, false, ccond, &argse[4].pre,
13354 : &arg[4]->where,
13355 : "TOPOS(%ld)+LEN(%ld)>BIT_SIZE(%d) "
13356 : "in intrinsic MVBITS", tp, ln, bitsize);
13357 : }
13358 :
13359 1182 : for (n = 0; n < 5; n++)
13360 : {
13361 985 : gfc_add_block_to_block (&se->pre, &argse[n].pre);
13362 985 : gfc_add_block_to_block (&se->post, &argse[n].post);
13363 : }
13364 :
13365 : /* lenmask = (LEN >= bit_size (TYPE)) ? ~(TYPE)0 : ((TYPE)1 << LEN) - 1 */
13366 197 : above = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
13367 197 : len, fold_convert (TREE_TYPE (len), bitsize));
13368 197 : mask1 = build_int_cst (type, -1);
13369 197 : mask2 = fold_build2_loc (input_location, LSHIFT_EXPR, type,
13370 : build_int_cst (type, 1), len);
13371 197 : mask2 = fold_build2_loc (input_location, MINUS_EXPR, type,
13372 : mask2, build_int_cst (type, 1));
13373 197 : lenmask = fold_build3_loc (input_location, COND_EXPR, type,
13374 : above, mask1, mask2);
13375 :
13376 : /* newbits = (((UTYPE)(FROM) >> FROMPOS) & lenmask) << TOPOS.
13377 : * For valid frompos+len <= bit_size(FROM) the conversion to unsigned is
13378 : * not strictly necessary; artificial bits from rshift will be masked. */
13379 197 : utype = unsigned_type_for (type);
13380 197 : newbits = fold_build2_loc (input_location, RSHIFT_EXPR, utype,
13381 : fold_convert (utype, from), frompos);
13382 197 : newbits = fold_build2_loc (input_location, BIT_AND_EXPR, type,
13383 : fold_convert (type, newbits), lenmask);
13384 197 : newbits = fold_build2_loc (input_location, LSHIFT_EXPR, type,
13385 : newbits, topos);
13386 :
13387 : /* oldbits = TO & (~(lenmask << TOPOS)). */
13388 197 : oldbits = fold_build2_loc (input_location, LSHIFT_EXPR, type,
13389 : lenmask, topos);
13390 197 : oldbits = fold_build1_loc (input_location, BIT_NOT_EXPR, type, oldbits);
13391 197 : oldbits = fold_build2_loc (input_location, BIT_AND_EXPR, type, oldbits, to);
13392 :
13393 : /* TO = newbits | oldbits. */
13394 197 : se->expr = fold_build2_loc (input_location, BIT_IOR_EXPR, type,
13395 : oldbits, newbits);
13396 :
13397 : /* Return the assignment. */
13398 197 : se->expr = fold_build2_loc (input_location, MODIFY_EXPR,
13399 : void_type_node, to, se->expr);
13400 197 : }
13401 :
13402 : /* Comes from trans-stmt.cc, but we don't want the whole header included. */
13403 : extern void gfc_trans_sync_stat (struct sync_stat *sync_stat, gfc_se *se,
13404 : tree *stat, tree *errmsg, tree *errmsg_len);
13405 :
13406 : static tree
13407 269 : conv_intrinsic_move_alloc (gfc_code *code)
13408 : {
13409 269 : stmtblock_t block;
13410 269 : gfc_expr *from_expr, *to_expr;
13411 269 : gfc_se from_se, to_se;
13412 269 : tree tmp, to_tree, from_tree, stat, errmsg, errmsg_len, fin_label = NULL_TREE;
13413 269 : bool coarray, from_is_class, from_is_scalar;
13414 269 : gfc_actual_arglist *arg = code->ext.actual;
13415 269 : sync_stat tmp_sync_stat = {nullptr, nullptr};
13416 :
13417 269 : gfc_start_block (&block);
13418 :
13419 269 : from_expr = arg->expr;
13420 269 : arg = arg->next;
13421 269 : to_expr = arg->expr;
13422 269 : arg = arg->next;
13423 :
13424 807 : while (arg)
13425 : {
13426 538 : if (arg->expr)
13427 : {
13428 0 : if (!strcmp ("stat", arg->name))
13429 0 : tmp_sync_stat.stat = arg->expr;
13430 0 : else if (!strcmp ("errmsg", arg->name))
13431 0 : tmp_sync_stat.errmsg = arg->expr;
13432 : }
13433 538 : arg = arg->next;
13434 : }
13435 :
13436 269 : gfc_init_se (&from_se, NULL);
13437 269 : gfc_init_se (&to_se, NULL);
13438 :
13439 269 : gfc_trans_sync_stat (&tmp_sync_stat, &from_se, &stat, &errmsg, &errmsg_len);
13440 269 : if (stat != null_pointer_node)
13441 0 : fin_label = gfc_build_label_decl (NULL_TREE);
13442 :
13443 269 : gcc_assert (from_expr->ts.type != BT_CLASS || to_expr->ts.type == BT_CLASS);
13444 269 : coarray = from_expr->corank != 0;
13445 :
13446 269 : from_is_class = from_expr->ts.type == BT_CLASS;
13447 269 : from_is_scalar = from_expr->rank == 0 && !coarray;
13448 269 : if (to_expr->ts.type == BT_CLASS || from_is_scalar)
13449 : {
13450 169 : from_se.want_pointer = 1;
13451 169 : if (from_is_scalar)
13452 121 : gfc_conv_expr (&from_se, from_expr);
13453 : else
13454 48 : gfc_conv_expr_descriptor (&from_se, from_expr);
13455 169 : if (from_is_class)
13456 64 : from_tree = gfc_class_data_get (from_se.expr);
13457 : else
13458 : {
13459 105 : gfc_symbol *vtab;
13460 105 : from_tree = from_se.expr;
13461 :
13462 105 : if (to_expr->ts.type == BT_CLASS)
13463 : {
13464 42 : vtab = gfc_find_vtab (&from_expr->ts);
13465 42 : gcc_assert (vtab);
13466 42 : from_se.expr = gfc_get_symbol_decl (vtab);
13467 : }
13468 : }
13469 169 : gfc_add_block_to_block (&block, &from_se.pre);
13470 :
13471 169 : to_se.want_pointer = 1;
13472 169 : if (to_expr->rank == 0)
13473 121 : gfc_conv_expr (&to_se, to_expr);
13474 : else
13475 48 : gfc_conv_expr_descriptor (&to_se, to_expr);
13476 169 : if (to_expr->ts.type == BT_CLASS)
13477 106 : to_tree = gfc_class_data_get (to_se.expr);
13478 : else
13479 63 : to_tree = to_se.expr;
13480 169 : gfc_add_block_to_block (&block, &to_se.pre);
13481 :
13482 : /* Deallocate "to". */
13483 169 : if (to_expr->rank == 0)
13484 : {
13485 121 : tmp = gfc_deallocate_scalar_with_status (to_tree, stat, fin_label,
13486 : true, to_expr, to_expr->ts,
13487 : NULL_TREE, false, true,
13488 : errmsg, errmsg_len);
13489 121 : gfc_add_expr_to_block (&block, tmp);
13490 : }
13491 :
13492 169 : if (from_is_scalar)
13493 : {
13494 : /* Assign (_data) pointers. */
13495 121 : gfc_add_modify_loc (input_location, &block, to_tree,
13496 121 : fold_convert (TREE_TYPE (to_tree), from_tree));
13497 :
13498 : /* Set "from" to NULL. */
13499 121 : gfc_add_modify_loc (input_location, &block, from_tree,
13500 121 : fold_convert (TREE_TYPE (from_tree),
13501 : null_pointer_node));
13502 :
13503 121 : gfc_add_block_to_block (&block, &from_se.post);
13504 : }
13505 169 : gfc_add_block_to_block (&block, &to_se.post);
13506 :
13507 : /* Set _vptr. */
13508 169 : if (to_expr->ts.type == BT_CLASS)
13509 : {
13510 106 : gfc_class_set_vptr (&block, to_se.expr, from_se.expr);
13511 106 : if (from_is_class)
13512 64 : gfc_reset_vptr (&block, from_expr);
13513 106 : if (UNLIMITED_POLY (to_expr))
13514 : {
13515 20 : tree to_len = gfc_class_len_get (to_se.class_container);
13516 20 : tmp = from_expr->ts.type == BT_CHARACTER && from_se.string_length
13517 20 : ? from_se.string_length
13518 : : size_zero_node;
13519 20 : gfc_add_modify_loc (input_location, &block, to_len,
13520 20 : fold_convert (TREE_TYPE (to_len), tmp));
13521 : }
13522 : }
13523 :
13524 169 : if (from_is_scalar)
13525 : {
13526 121 : if (to_expr->ts.type == BT_CHARACTER && to_expr->ts.deferred)
13527 : {
13528 6 : gfc_add_modify_loc (input_location, &block, to_se.string_length,
13529 6 : fold_convert (TREE_TYPE (to_se.string_length),
13530 : from_se.string_length));
13531 6 : if (from_expr->ts.deferred)
13532 6 : gfc_add_modify_loc (
13533 : input_location, &block, from_se.string_length,
13534 6 : build_int_cst (TREE_TYPE (from_se.string_length), 0));
13535 : }
13536 121 : if (UNLIMITED_POLY (from_expr))
13537 2 : gfc_reset_len (&block, from_expr);
13538 :
13539 121 : return gfc_finish_block (&block);
13540 : }
13541 :
13542 48 : gfc_init_se (&to_se, NULL);
13543 48 : gfc_init_se (&from_se, NULL);
13544 : }
13545 :
13546 : /* Deallocate "to". */
13547 148 : if (from_expr->rank == 0)
13548 : {
13549 4 : to_se.want_coarray = 1;
13550 4 : from_se.want_coarray = 1;
13551 : }
13552 148 : gfc_conv_expr_descriptor (&to_se, to_expr);
13553 148 : gfc_conv_expr_descriptor (&from_se, from_expr);
13554 148 : gfc_add_block_to_block (&block, &to_se.pre);
13555 148 : gfc_add_block_to_block (&block, &from_se.pre);
13556 :
13557 : /* For coarrays, call SYNC ALL if TO is already deallocated as MOVE_ALLOC
13558 : is an image control "statement", cf. IR F08/0040 in 12-006A. */
13559 148 : if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
13560 : {
13561 6 : tree cond;
13562 :
13563 6 : tmp = gfc_deallocate_with_status (to_se.expr, stat, errmsg, errmsg_len,
13564 : fin_label, true, to_expr,
13565 : GFC_CAF_COARRAY_DEALLOCATE_ONLY,
13566 : NULL_TREE, NULL_TREE,
13567 : gfc_conv_descriptor_token (to_se.expr),
13568 : true);
13569 6 : gfc_add_expr_to_block (&block, tmp);
13570 :
13571 6 : tmp = gfc_conv_descriptor_data_get (to_se.expr);
13572 6 : cond = fold_build2_loc (input_location, EQ_EXPR,
13573 : logical_type_node, tmp,
13574 6 : fold_convert (TREE_TYPE (tmp),
13575 : null_pointer_node));
13576 6 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
13577 : 3, null_pointer_node, null_pointer_node,
13578 : integer_zero_node);
13579 :
13580 6 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
13581 : tmp, build_empty_stmt (input_location));
13582 6 : gfc_add_expr_to_block (&block, tmp);
13583 6 : }
13584 : else
13585 : {
13586 142 : if (to_expr->ts.type == BT_DERIVED
13587 25 : && to_expr->ts.u.derived->attr.alloc_comp)
13588 : {
13589 19 : tmp = gfc_deallocate_alloc_comp (to_expr->ts.u.derived,
13590 : to_se.expr, to_expr->rank);
13591 19 : gfc_add_expr_to_block (&block, tmp);
13592 : }
13593 :
13594 142 : tmp = gfc_deallocate_with_status (to_se.expr, stat, errmsg, errmsg_len,
13595 : fin_label, true, to_expr,
13596 : GFC_CAF_COARRAY_NOCOARRAY, NULL_TREE,
13597 : NULL_TREE, NULL_TREE, true);
13598 142 : gfc_add_expr_to_block (&block, tmp);
13599 : }
13600 :
13601 : /* Copy the array descriptor data. */
13602 148 : gfc_add_modify_loc (input_location, &block, to_se.expr, from_se.expr);
13603 :
13604 : /* Set "from" to NULL. */
13605 148 : gfc_conv_descriptor_data_set (&block, from_se.expr, null_pointer_node);
13606 :
13607 148 : if (coarray && flag_coarray == GFC_FCOARRAY_LIB)
13608 : {
13609 : /* Copy the array descriptor data has overwritten the to-token and cleared
13610 : from.data. Now also clear the from.token. */
13611 6 : gfc_conv_descriptor_token_set (&block, from_se.expr, null_pointer_node);
13612 : }
13613 :
13614 148 : if (to_expr->ts.type == BT_CHARACTER && to_expr->ts.deferred)
13615 : {
13616 7 : gfc_add_modify_loc (input_location, &block, to_se.string_length,
13617 7 : fold_convert (TREE_TYPE (to_se.string_length),
13618 : from_se.string_length));
13619 7 : if (from_expr->ts.deferred)
13620 6 : gfc_add_modify_loc (input_location, &block, from_se.string_length,
13621 6 : build_int_cst (TREE_TYPE (from_se.string_length), 0));
13622 : }
13623 148 : if (fin_label)
13624 0 : gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, fin_label));
13625 :
13626 148 : gfc_add_block_to_block (&block, &to_se.post);
13627 148 : gfc_add_block_to_block (&block, &from_se.post);
13628 :
13629 148 : return gfc_finish_block (&block);
13630 : }
13631 :
13632 :
13633 : tree
13634 6996 : gfc_conv_intrinsic_subroutine (gfc_code *code)
13635 : {
13636 6996 : tree res;
13637 :
13638 6996 : gcc_assert (code->resolved_isym);
13639 :
13640 6996 : switch (code->resolved_isym->id)
13641 : {
13642 269 : case GFC_ISYM_MOVE_ALLOC:
13643 269 : res = conv_intrinsic_move_alloc (code);
13644 269 : break;
13645 :
13646 14 : case GFC_ISYM_ATOMIC_CAS:
13647 14 : res = conv_intrinsic_atomic_cas (code);
13648 14 : break;
13649 :
13650 95 : case GFC_ISYM_ATOMIC_ADD:
13651 95 : case GFC_ISYM_ATOMIC_AND:
13652 95 : case GFC_ISYM_ATOMIC_DEF:
13653 95 : case GFC_ISYM_ATOMIC_OR:
13654 95 : case GFC_ISYM_ATOMIC_XOR:
13655 95 : case GFC_ISYM_ATOMIC_FETCH_ADD:
13656 95 : case GFC_ISYM_ATOMIC_FETCH_AND:
13657 95 : case GFC_ISYM_ATOMIC_FETCH_OR:
13658 95 : case GFC_ISYM_ATOMIC_FETCH_XOR:
13659 95 : res = conv_intrinsic_atomic_op (code);
13660 95 : break;
13661 :
13662 176 : case GFC_ISYM_ATOMIC_REF:
13663 176 : res = conv_intrinsic_atomic_ref (code);
13664 176 : break;
13665 :
13666 105 : case GFC_ISYM_EVENT_QUERY:
13667 105 : res = conv_intrinsic_event_query (code);
13668 105 : break;
13669 :
13670 3370 : case GFC_ISYM_C_F_POINTER:
13671 3370 : case GFC_ISYM_C_F_PROCPOINTER:
13672 3370 : res = conv_isocbinding_subroutine (code);
13673 3370 : break;
13674 :
13675 60 : case GFC_ISYM_C_F_STRPOINTER:
13676 60 : res = conv_isocbinding_subroutine_strpointer (code);
13677 60 : break;
13678 :
13679 360 : case GFC_ISYM_CAF_SEND:
13680 360 : res = conv_caf_send_to_remote (code);
13681 360 : break;
13682 :
13683 140 : case GFC_ISYM_CAF_SENDGET:
13684 140 : res = conv_caf_sendget (code);
13685 140 : break;
13686 :
13687 91 : case GFC_ISYM_CO_BROADCAST:
13688 91 : case GFC_ISYM_CO_MIN:
13689 91 : case GFC_ISYM_CO_MAX:
13690 91 : case GFC_ISYM_CO_REDUCE:
13691 91 : case GFC_ISYM_CO_SUM:
13692 91 : res = conv_co_collective (code);
13693 91 : break;
13694 :
13695 10 : case GFC_ISYM_FREE:
13696 10 : res = conv_intrinsic_free (code);
13697 10 : break;
13698 :
13699 55 : case GFC_ISYM_FSTAT:
13700 55 : case GFC_ISYM_LSTAT:
13701 55 : case GFC_ISYM_STAT:
13702 55 : res = conv_intrinsic_fstat_lstat_stat_sub (code);
13703 55 : break;
13704 :
13705 90 : case GFC_ISYM_RANDOM_INIT:
13706 90 : res = conv_intrinsic_random_init (code);
13707 90 : break;
13708 :
13709 15 : case GFC_ISYM_KILL:
13710 15 : res = conv_intrinsic_kill_sub (code);
13711 15 : break;
13712 :
13713 : case GFC_ISYM_MVBITS:
13714 : res = NULL_TREE;
13715 : break;
13716 :
13717 196 : case GFC_ISYM_SYSTEM_CLOCK:
13718 196 : res = conv_intrinsic_system_clock (code);
13719 196 : break;
13720 :
13721 102 : case GFC_ISYM_SPLIT:
13722 102 : res = conv_intrinsic_split (code);
13723 102 : break;
13724 :
13725 : default:
13726 : res = NULL_TREE;
13727 : break;
13728 : }
13729 :
13730 6996 : return res;
13731 : }
13732 :
13733 : #include "gt-fortran-trans-intrinsic.h"
|